2 * Copyright 2006 Freescale Semiconductor, Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * Version 2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22 #ifdef CONFIG_HARD_I2C
25 #include <i2c.h> /* Functional interface */
28 #include <asm/fsl_i2c.h> /* HW definitions */
30 #define I2C_TIMEOUT (CFG_HZ / 4)
32 #define I2C_READ_BIT 1
33 #define I2C_WRITE_BIT 0
35 /* Initialize the bus pointer to whatever one the SPD EEPROM is on.
36 * Default is bus 0. This is necessary because the DDR initialization
37 * runs from ROM, and we can't switch buses because we can't modify
38 * the global variables.
40 #ifdef CFG_SPD_BUS_NUM
41 static unsigned int i2c_bus_num __attribute__ ((section ("data"))) = CFG_SPD_BUS_NUM;
43 static unsigned int i2c_bus_num __attribute__ ((section ("data"))) = 0;
46 static volatile struct fsl_i2c *i2c_dev[2] = {
47 (struct fsl_i2c *) (CFG_IMMR + CFG_I2C_OFFSET),
48 #ifdef CFG_I2C2_OFFSET
49 (struct fsl_i2c *) (CFG_IMMR + CFG_I2C2_OFFSET)
54 i2c_init(int speed, int slaveadd)
56 volatile struct fsl_i2c *dev;
58 dev = (struct fsl_i2c *) (CFG_IMMR + CFG_I2C_OFFSET);
60 writeb(0, &dev->cr); /* stop I2C controller */
61 udelay(5); /* let it shutdown in peace */
62 writeb(0x3F, &dev->fdr); /* set bus speed */
63 writeb(0x3F, &dev->dfsrr); /* set default filter */
64 writeb(slaveadd << 1, &dev->adr); /* write slave address */
65 writeb(0x0, &dev->sr); /* clear status register */
66 writeb(I2C_CR_MEN, &dev->cr); /* start I2C controller */
68 #ifdef CFG_I2C2_OFFSET
69 dev = (struct fsl_i2c *) (CFG_IMMR + CFG_I2C2_OFFSET);
71 writeb(0, &dev->cr); /* stop I2C controller */
72 udelay(5); /* let it shutdown in peace */
73 writeb(0x3F, &dev->fdr); /* set bus speed */
74 writeb(0x3F, &dev->dfsrr); /* set default filter */
75 writeb(slaveadd << 1, &dev->adr); /* write slave address */
76 writeb(0x0, &dev->sr); /* clear status register */
77 writeb(I2C_CR_MEN, &dev->cr); /* start I2C controller */
78 #endif /* CFG_I2C2_OFFSET */
84 ulong timeval = get_timer(0);
86 while (readb(&i2c_dev[i2c_bus_num]->sr) & I2C_SR_MBB) {
87 if (get_timer(timeval) > I2C_TIMEOUT) {
99 ulong timeval = get_timer(0);
102 csr = readb(&i2c_dev[i2c_bus_num]->sr);
103 if (!(csr & I2C_SR_MIF))
106 writeb(0x0, &i2c_dev[i2c_bus_num]->sr);
108 if (csr & I2C_SR_MAL) {
109 debug("i2c_wait: MAL\n");
113 if (!(csr & I2C_SR_MCF)) {
114 debug("i2c_wait: unfinished\n");
118 if (write == I2C_WRITE_BIT && (csr & I2C_SR_RXAK)) {
119 debug("i2c_wait: No RXACK\n");
124 } while (get_timer (timeval) < I2C_TIMEOUT);
126 debug("i2c_wait: timed out\n");
130 static __inline__ int
131 i2c_write_addr (u8 dev, u8 dir, int rsta)
133 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX
134 | (rsta ? I2C_CR_RSTA : 0),
135 &i2c_dev[i2c_bus_num]->cr);
137 writeb((dev << 1) | dir, &i2c_dev[i2c_bus_num]->dr);
139 if (i2c_wait(I2C_WRITE_BIT) < 0)
145 static __inline__ int
146 __i2c_write(u8 *data, int length)
150 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_MTX,
151 &i2c_dev[i2c_bus_num]->cr);
153 for (i = 0; i < length; i++) {
154 writeb(data[i], &i2c_dev[i2c_bus_num]->dr);
156 if (i2c_wait(I2C_WRITE_BIT) < 0)
163 static __inline__ int
164 __i2c_read(u8 *data, int length)
168 writeb(I2C_CR_MEN | I2C_CR_MSTA | ((length == 1) ? I2C_CR_TXAK : 0),
169 &i2c_dev[i2c_bus_num]->cr);
172 readb(&i2c_dev[i2c_bus_num]->dr);
174 for (i = 0; i < length; i++) {
175 if (i2c_wait(I2C_READ_BIT) < 0)
178 /* Generate ack on last next to last byte */
180 writeb(I2C_CR_MEN | I2C_CR_MSTA | I2C_CR_TXAK,
181 &i2c_dev[i2c_bus_num]->cr);
183 /* Generate stop on last byte */
185 writeb(I2C_CR_MEN | I2C_CR_TXAK, &i2c_dev[i2c_bus_num]->cr);
187 data[i] = readb(&i2c_dev[i2c_bus_num]->dr);
194 i2c_read(u8 dev, uint addr, int alen, u8 *data, int length)
196 int i = -1; /* signal error */
199 if (i2c_wait4bus() >= 0
200 && i2c_write_addr(dev, I2C_WRITE_BIT, 0) != 0
201 && __i2c_write(&a[4 - alen], alen) == alen)
202 i = 0; /* No error so far */
205 && i2c_write_addr(dev, I2C_READ_BIT, 1) != 0)
206 i = __i2c_read(data, length);
208 writeb(I2C_CR_MEN, &i2c_dev[i2c_bus_num]->cr);
217 i2c_write(u8 dev, uint addr, int alen, u8 *data, int length)
219 int i = -1; /* signal error */
222 if (i2c_wait4bus() >= 0
223 && i2c_write_addr(dev, I2C_WRITE_BIT, 0) != 0
224 && __i2c_write(&a[4 - alen], alen) == alen) {
225 i = __i2c_write(data, length);
228 writeb(I2C_CR_MEN, &i2c_dev[i2c_bus_num]->cr);
237 i2c_probe(uchar chip)
239 /* For unknow reason the controller will ACK when
240 * probing for a slave with the same address, so skip
243 if (chip == (readb(&i2c_dev[i2c_bus_num]->adr) >> 1))
246 return i2c_read(chip, 0, 0, NULL, 0);
250 i2c_reg_read(uchar i2c_addr, uchar reg)
254 i2c_read(i2c_addr, reg, 1, buf, 1);
260 i2c_reg_write(uchar i2c_addr, uchar reg, uchar val)
262 i2c_write(i2c_addr, reg, 1, &val, 1);
265 int i2c_set_bus_num(unsigned int bus)
267 #ifdef CFG_I2C2_OFFSET
280 int i2c_set_bus_speed(unsigned int speed)
285 unsigned int i2c_get_bus_num(void)
290 unsigned int i2c_get_bus_speed(void)
294 #endif /* CONFIG_HARD_I2C */
295 #endif /* CONFIG_FSL_I2C */