2006-04-26 22:58:56 +00:00
|
|
|
/*
|
2006-10-19 16:02:16 +00:00
|
|
|
* Copyright 2006 Freescale Semiconductor, Inc.
|
2006-04-26 22:58:56 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
2006-10-19 16:02:16 +00:00
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* Version 2 as published by the Free Software Foundation.
|
2006-04-26 22:58:56 +00:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
|
|
|
* MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
|
2006-10-20 20:50:15 +00:00
|
|
|
#ifdef CONFIG_FSL_I2C
|
2006-04-26 22:58:56 +00:00
|
|
|
#ifdef CONFIG_HARD_I2C
|
|
|
|
|
2006-10-19 17:02:24 +00:00
|
|
|
#include <command.h>
|
2006-10-20 20:50:15 +00:00
|
|
|
#include <i2c.h> /* Functional interface */
|
|
|
|
|
2006-10-19 16:02:16 +00:00
|
|
|
#include <asm/io.h>
|
2006-10-20 20:50:15 +00:00
|
|
|
#include <asm/fsl_i2c.h> /* HW definitions */
|
2006-04-26 22:58:56 +00:00
|
|
|
|
2006-10-19 16:02:16 +00:00
|
|
|
#define I2C_TIMEOUT (CFG_HZ / 4)
|
2006-10-19 17:02:24 +00:00
|
|
|
#define I2C ((struct fsl_i2c *)(CFG_IMMR + CFG_I2C_OFFSET))
|
2006-04-26 22:58:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
i2c_init(int speed, int slaveadd)
|
|
|
|
{
|
|
|
|
/* stop I2C controller */
|
2006-10-20 20:50:15 +00:00
|
|
|
writeb(0x0, &I2C->cr);
|
2006-04-26 22:58:56 +00:00
|
|
|
|
|
|
|
/* set clock */
|
2006-10-19 16:02:16 +00:00
|
|
|
writeb(0x3f, &I2C->fdr);
|
2006-04-26 22:58:56 +00:00
|
|
|
|
|
|
|
/* set default filter */
|
2006-10-19 16:02:16 +00:00
|
|
|
writeb(0x10, &I2C->dfsrr);
|
2006-04-26 22:58:56 +00:00
|
|
|
|
|
|
|
/* write slave address */
|
2006-10-19 16:02:16 +00:00
|
|
|
writeb(slaveadd, &I2C->adr);
|
2006-04-26 22:58:56 +00:00
|
|
|
|
|
|
|
/* clear status register */
|
2006-10-19 16:02:16 +00:00
|
|
|
writeb(0x0, &I2C->sr);
|
2006-04-26 22:58:56 +00:00
|
|
|
|
|
|
|
/* start I2C controller */
|
2006-10-19 16:02:16 +00:00
|
|
|
writeb(I2C_CR_MEN, &I2C->cr);
|
2006-04-26 22:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ int
|
2006-04-27 15:15:16 +00:00
|
|
|
i2c_wait4bus(void)
|
2006-04-26 22:58:56 +00:00
|
|
|
{
|
2006-10-20 20:50:15 +00:00
|
|
|
ulong timeval = get_timer(0);
|
2006-04-26 22:58:56 +00:00
|
|
|
|
2006-10-19 16:02:16 +00:00
|
|
|
while (readb(&I2C->sr) & I2C_SR_MBB) {
|
|
|
|
if (get_timer(timeval) > I2C_TIMEOUT) {
|
2006-04-26 22:58:56 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-27 15:15:16 +00:00
|
|
|
return 0;
|
2006-04-26 22:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ int
|
2006-04-27 15:15:16 +00:00
|
|
|
i2c_wait(int write)
|
2006-04-26 22:58:56 +00:00
|
|
|
{
|
|
|
|
u32 csr;
|
2006-08-22 17:06:18 +00:00
|
|
|
ulong timeval = get_timer(0);
|
2006-04-26 22:58:56 +00:00
|
|
|
|
|
|
|
do {
|
2006-10-19 16:02:16 +00:00
|
|
|
csr = readb(&I2C->sr);
|
|
|
|
if (!(csr & I2C_SR_MIF))
|
2006-04-26 22:58:56 +00:00
|
|
|
continue;
|
|
|
|
|
2006-10-19 16:02:16 +00:00
|
|
|
writeb(0x0, &I2C->sr);
|
2006-04-26 22:58:56 +00:00
|
|
|
|
2006-10-19 16:02:16 +00:00
|
|
|
if (csr & I2C_SR_MAL) {
|
2006-04-26 22:58:56 +00:00
|
|
|
debug("i2c_wait: MAL\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-10-19 16:02:16 +00:00
|
|
|
if (!(csr & I2C_SR_MCF)) {
|
2006-04-26 22:58:56 +00:00
|
|
|
debug("i2c_wait: unfinished\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-10-19 16:02:16 +00:00
|
|
|
if (write == I2C_WRITE && (csr & I2C_SR_RXAK)) {
|
2006-04-26 22:58:56 +00:00
|
|
|
debug("i2c_wait: No RXACK\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2006-10-19 16:02:16 +00:00
|
|
|
} while (get_timer (timeval) < I2C_TIMEOUT);
|
2006-04-26 22:58:56 +00:00
|
|
|
|
|
|
|
debug("i2c_wait: timed out\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ int
|
2006-10-19 16:02:16 +00:00
|
|
|
i2c_write_addr (u8 dev, u8 dir, int rsta)
|
2006-04-26 22:58:56 +00:00
|
|
|
{
|
2006-10-19 16:02:16 +00:00
|
|
|
writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX
|
|
|
|
| (rsta ? I2C_CR_RSTA : 0),
|
|
|
|
&I2C->cr);
|
2006-04-26 22:58:56 +00:00
|
|
|
|
2006-10-19 16:02:16 +00:00
|
|
|
writeb((dev << 1) | dir, &I2C->dr);
|
2006-04-26 22:58:56 +00:00
|
|
|
|
2006-04-27 15:15:16 +00:00
|
|
|
if (i2c_wait(I2C_WRITE) < 0)
|
2006-04-26 22:58:56 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ int
|
2006-08-22 17:06:18 +00:00
|
|
|
__i2c_write(u8 *data, int length)
|
2006-04-26 22:58:56 +00:00
|
|
|
{
|
|
|
|
int i;
|
2006-04-27 15:15:16 +00:00
|
|
|
|
2006-10-19 16:02:16 +00:00
|
|
|
writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX,
|
|
|
|
&I2C->cr);
|
2006-04-26 22:58:56 +00:00
|
|
|
|
2006-04-27 15:15:16 +00:00
|
|
|
for (i = 0; i < length; i++) {
|
2006-10-19 16:02:16 +00:00
|
|
|
writeb(data[i], &I2C->dr);
|
2006-04-26 22:58:56 +00:00
|
|
|
|
2006-04-27 15:15:16 +00:00
|
|
|
if (i2c_wait(I2C_WRITE) < 0)
|
2006-04-26 22:58:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ int
|
2006-08-22 17:06:18 +00:00
|
|
|
__i2c_read(u8 *data, int length)
|
2006-04-26 22:58:56 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2006-10-19 16:02:16 +00:00
|
|
|
writeb(I2C_CR_MEN | I2C_CR_MSTA | ((length == 1) ? I2C_CR_TXAK : 0),
|
|
|
|
&I2C->cr);
|
2006-04-26 22:58:56 +00:00
|
|
|
|
|
|
|
/* dummy read */
|
2006-10-19 16:02:16 +00:00
|
|
|
readb(&I2C->dr);
|
2006-04-26 22:58:56 +00:00
|
|
|
|
2006-04-27 15:15:16 +00:00
|
|
|
for (i = 0; i < length; i++) {
|
|
|
|
if (i2c_wait(I2C_READ) < 0)
|
2006-04-26 22:58:56 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Generate ack on last next to last byte */
|
|
|
|
if (i == length - 2)
|
2006-10-19 16:02:16 +00:00
|
|
|
writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_TXAK,
|
|
|
|
&I2C->cr);
|
2006-04-26 22:58:56 +00:00
|
|
|
|
|
|
|
/* Generate stop on last byte */
|
|
|
|
if (i == length - 1)
|
2006-10-19 16:02:16 +00:00
|
|
|
writeb(I2C_CR_MEN | I2C_CR_TXAK, &I2C->cr);
|
2006-04-26 22:58:56 +00:00
|
|
|
|
2006-10-19 16:02:16 +00:00
|
|
|
data[i] = readb(&I2C->dr);
|
2006-04-26 22:58:56 +00:00
|
|
|
}
|
2006-04-27 15:15:16 +00:00
|
|
|
|
2006-04-26 22:58:56 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2006-08-22 17:06:18 +00:00
|
|
|
i2c_read(u8 dev, uint addr, int alen, u8 *data, int length)
|
2006-04-26 22:58:56 +00:00
|
|
|
{
|
|
|
|
int i = 0;
|
2006-10-19 16:02:16 +00:00
|
|
|
u8 *a = (u8*)&addr;
|
2006-04-26 22:58:56 +00:00
|
|
|
|
2006-10-19 17:02:24 +00:00
|
|
|
if (i2c_wait4bus() >= 0
|
|
|
|
&& i2c_write_addr(dev, I2C_WRITE, 0) != 0
|
|
|
|
&& __i2c_write(&a[4 - alen], alen) == alen
|
|
|
|
&& i2c_write_addr(dev, I2C_READ, 1) != 0) {
|
|
|
|
i = __i2c_read(data, length);
|
|
|
|
}
|
2006-04-26 22:58:56 +00:00
|
|
|
|
2006-10-19 16:02:16 +00:00
|
|
|
writeb(I2C_CR_MEN, &I2C->cr);
|
2006-04-26 22:58:56 +00:00
|
|
|
|
2006-10-19 17:02:24 +00:00
|
|
|
if (i == length)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return -1;
|
2006-04-26 22:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2006-08-22 17:06:18 +00:00
|
|
|
i2c_write(u8 dev, uint addr, int alen, u8 *data, int length)
|
2006-04-26 22:58:56 +00:00
|
|
|
{
|
|
|
|
int i = 0;
|
2006-10-19 16:02:16 +00:00
|
|
|
u8 *a = (u8*)&addr;
|
2006-04-26 22:58:56 +00:00
|
|
|
|
2006-10-19 17:02:24 +00:00
|
|
|
if (i2c_wait4bus() >= 0
|
|
|
|
&& i2c_write_addr(dev, I2C_WRITE, 0) != 0
|
|
|
|
&& __i2c_write(&a[4 - alen], alen) == alen) {
|
|
|
|
i = __i2c_write(data, length);
|
|
|
|
}
|
2006-04-26 22:58:56 +00:00
|
|
|
|
2006-10-19 16:02:16 +00:00
|
|
|
writeb(I2C_CR_MEN, &I2C->cr);
|
2006-04-26 22:58:56 +00:00
|
|
|
|
2006-10-19 17:02:24 +00:00
|
|
|
if (i == length)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return -1;
|
2006-04-26 22:58:56 +00:00
|
|
|
}
|
|
|
|
|
2006-08-22 17:06:18 +00:00
|
|
|
int
|
|
|
|
i2c_probe(uchar chip)
|
2006-04-26 22:58:56 +00:00
|
|
|
{
|
|
|
|
int tmp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to read the first location of the chip. The underlying
|
|
|
|
* driver doesn't appear to support sending just the chip address
|
|
|
|
* and looking for an <ACK> back.
|
|
|
|
*/
|
|
|
|
udelay(10000);
|
|
|
|
|
2006-10-19 16:02:16 +00:00
|
|
|
return i2c_read(chip, 0, 1, (uchar *)&tmp, 1);
|
2006-04-26 22:58:56 +00:00
|
|
|
}
|
|
|
|
|
2006-08-22 17:06:18 +00:00
|
|
|
uchar
|
|
|
|
i2c_reg_read(uchar i2c_addr, uchar reg)
|
2006-04-26 22:58:56 +00:00
|
|
|
{
|
2006-10-19 16:02:16 +00:00
|
|
|
uchar buf[1];
|
2006-04-26 22:58:56 +00:00
|
|
|
|
2006-04-27 15:15:16 +00:00
|
|
|
i2c_read(i2c_addr, reg, 1, buf, 1);
|
2006-04-26 22:58:56 +00:00
|
|
|
|
2006-04-27 15:15:16 +00:00
|
|
|
return buf[0];
|
2006-04-26 22:58:56 +00:00
|
|
|
}
|
|
|
|
|
2006-08-22 17:06:18 +00:00
|
|
|
void
|
|
|
|
i2c_reg_write(uchar i2c_addr, uchar reg, uchar val)
|
2006-04-26 22:58:56 +00:00
|
|
|
{
|
2006-04-27 15:15:16 +00:00
|
|
|
i2c_write(i2c_addr, reg, 1, &val, 1);
|
2006-04-26 22:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_HARD_I2C */
|
2006-10-20 20:50:15 +00:00
|
|
|
#endif /* CONFIG_FSL_I2C */
|