3 * David Mueller, ELSOFT AG, d.mueller@elsoft.ch
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 /* This code should work for both the S3C2400 and the S3C2410
25 * as they seem to have the same I2C controller inside.
26 * The different address mapping is handled by the s3c24xx.h files below.
30 #include <asm/arch/s3c24x0_cpu.h>
35 #ifdef CONFIG_HARD_I2C
43 #define I2C_NOK_LA 3 /* Lost arbitration */
44 #define I2C_NOK_TOUT 4 /* time out */
46 #define I2CSTAT_BSY 0x20 /* Busy bit */
47 #define I2CSTAT_NACK 0x01 /* Nack bit */
48 #define I2CCON_IRPND 0x10 /* Interrupt pending bit */
49 #define I2C_MODE_MT 0xC0 /* Master Transmit Mode */
50 #define I2C_MODE_MR 0x80 /* Master Receive Mode */
51 #define I2C_START_STOP 0x20 /* START / STOP */
52 #define I2C_TXRX_ENA 0x10 /* I2C Tx/Rx enable */
54 #define I2C_TIMEOUT 1 /* 1 second */
56 static int GetI2CSDA(void)
58 struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
61 return (readl(&gpio->gpedat) & 0x8000) >> 15;
64 return (readl(&gpio->pgdat) & 0x0020) >> 5;
69 static void SetI2CSDA(int x)
71 rGPEDAT = (rGPEDAT & ~0x8000) | (x & 1) << 15;
75 static void SetI2CSCL(int x)
77 struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
80 writel((readl(&gpio->gpedat) & ~0x4000) | (x & 1) << 14, &gpio->gpedat);
83 writel((readl(&gpio->pgdat) & ~0x0040) | (x & 1) << 6, &gpio->pgdat);
87 static int WaitForXfer(void)
89 struct s3c24x0_i2c *i2c = s3c24x0_get_base_i2c();
92 i = I2C_TIMEOUT * 10000;
93 while (!(readl(&i2c->iiccon) & I2CCON_IRPND) && (i > 0)) {
98 return (readl(&i2c->iiccon) & I2CCON_IRPND) ? I2C_OK : I2C_NOK_TOUT;
101 static int IsACK(void)
103 struct s3c24x0_i2c *i2c = s3c24x0_get_base_i2c();
105 return !(readl(&i2c->iicstat) & I2CSTAT_NACK);
108 static void ReadWriteByte(void)
110 struct s3c24x0_i2c *i2c = s3c24x0_get_base_i2c();
112 writel(readl(&i2c->iiccon) & ~I2CCON_IRPND, &i2c->iiccon);
115 void i2c_init(int speed, int slaveadd)
117 struct s3c24x0_i2c *i2c = s3c24x0_get_base_i2c();
118 struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
119 ulong freq, pres = 16, div;
122 /* wait for some time to give previous transfer a chance to finish */
124 i = I2C_TIMEOUT * 1000;
125 while ((readl(&i2c->iicstat) && I2CSTAT_BSY) && (i > 0)) {
130 if ((readl(&i2c->iicstat) & I2CSTAT_BSY) || GetI2CSDA() == 0) {
131 #ifdef CONFIG_S3C2410
132 ulong old_gpecon = readl(&gpio->gpecon);
134 #ifdef CONFIG_S3C2400
135 ulong old_gpecon = readl(&gpio->pgcon);
137 /* bus still busy probably by (most) previously interrupted
140 #ifdef CONFIG_S3C2410
141 /* set I2CSDA and I2CSCL (GPE15, GPE14) to GPIO */
142 writel((readl(&gpio->gpecon) & ~0xF0000000) | 0x10000000,
145 #ifdef CONFIG_S3C2400
146 /* set I2CSDA and I2CSCL (PG5, PG6) to GPIO */
147 writel((readl(&gpio->pgcon) & ~0x00003c00) | 0x00001000,
151 /* toggle I2CSCL until bus idle */
155 while ((i > 0) && (GetI2CSDA() != 1)) {
165 /* restore pin functions */
166 #ifdef CONFIG_S3C2410
167 writel(old_gpecon, &gpio->gpecon);
169 #ifdef CONFIG_S3C2400
170 writel(old_gpecon, &gpio->pgcon);
174 /* calculate prescaler and divisor values */
176 if ((freq / pres / (16 + 1)) > speed)
177 /* set prescaler to 512 */
181 while ((freq / pres / (div + 1)) > speed)
184 /* set prescaler, divisor according to freq, also set
186 writel((div & 0x0F) | 0xA0 | ((pres == 512) ? 0x40 : 0), &i2c->iiccon);
188 /* init to SLAVE REVEIVE and set slaveaddr */
189 writel(0, &i2c->iicstat);
190 writel(slaveadd, &i2c->iicadd);
191 /* program Master Transmit (and implicit STOP) */
192 writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->iicstat);
197 * cmd_type is 0 for write, 1 for read.
199 * addr_len can take any value from 0-255, it is only limited
200 * by the char, we could make it larger if needed. If it is
201 * 0 we skip the address write cycle.
204 int i2c_transfer(unsigned char cmd_type,
206 unsigned char addr[],
207 unsigned char addr_len,
208 unsigned char data[], unsigned short data_len)
210 struct s3c24x0_i2c *i2c = s3c24x0_get_base_i2c();
213 if (data == 0 || data_len == 0) {
214 /*Don't support data transfer of no length or to address 0 */
215 printf("i2c_transfer: bad call\n");
219 /* Check I2C bus idle */
220 i = I2C_TIMEOUT * 1000;
221 while ((readl(&i2c->iicstat) & I2CSTAT_BSY) && (i > 0)) {
226 if (readl(&i2c->iicstat) & I2CSTAT_BSY)
229 writel(readl(&i2c->iiccon) | 0x80, &i2c->iiccon);
234 if (addr && addr_len) {
235 writel(chip, &i2c->iicds);
237 writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP,
240 while ((i < addr_len) && (result == I2C_OK)) {
241 result = WaitForXfer();
242 writel(addr[i], &i2c->iicds);
247 while ((i < data_len) && (result == I2C_OK)) {
248 result = WaitForXfer();
249 writel(data[i], &i2c->iicds);
254 writel(chip, &i2c->iicds);
256 writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP,
259 while ((i < data_len) && (result = I2C_OK)) {
260 result = WaitForXfer();
261 writel(data[i], &i2c->iicds);
267 if (result == I2C_OK)
268 result = WaitForXfer();
271 writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
276 if (addr && addr_len) {
277 writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->iicstat);
278 writel(chip, &i2c->iicds);
280 writel(readl(&i2c->iicstat) | I2C_START_STOP,
282 result = WaitForXfer();
285 while ((i < addr_len) && (result == I2C_OK)) {
286 writel(addr[i], &i2c->iicds);
288 result = WaitForXfer();
292 writel(chip, &i2c->iicds);
294 writel(I2C_MODE_MR | I2C_TXRX_ENA |
295 I2C_START_STOP, &i2c->iicstat);
297 result = WaitForXfer();
299 while ((i < data_len) && (result == I2C_OK)) {
300 /* disable ACK for final READ */
301 if (i == data_len - 1)
302 writel(readl(&i2c->iiccon)
303 & ~0x80, &i2c->iiccon);
305 result = WaitForXfer();
306 data[i] = readl(&i2c->iicds);
314 writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
315 writel(chip, &i2c->iicds);
317 writel(readl(&i2c->iicstat) | I2C_START_STOP,
319 result = WaitForXfer();
323 while ((i < data_len) && (result == I2C_OK)) {
324 /* disable ACK for final READ */
325 if (i == data_len - 1)
326 writel(readl(&i2c->iiccon) &
327 ~0x80, &i2c->iiccon);
329 result = WaitForXfer();
330 data[i] = readl(&i2c->iicds);
339 writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
344 printf("i2c_transfer: bad call\n");
352 int i2c_probe(uchar chip)
359 * What is needed is to send the chip address and verify that the
360 * address was <ACK>ed (i.e. there was a chip at that address which
361 * drove the data line low).
363 return i2c_transfer(I2C_READ, chip << 1, 0, 0, buf, 1) != I2C_OK;
366 int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
372 printf("I2C read: addr len %d not supported\n", alen);
377 xaddr[0] = (addr >> 24) & 0xFF;
378 xaddr[1] = (addr >> 16) & 0xFF;
379 xaddr[2] = (addr >> 8) & 0xFF;
380 xaddr[3] = addr & 0xFF;
383 #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
385 * EEPROM chips that implement "address overflow" are ones
386 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
387 * address and the extra bits end up in the "chip address"
388 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
389 * four 256 byte chips.
391 * Note that we consider the length of the address field to
392 * still be one byte because the extra address bits are
393 * hidden in the chip address.
396 chip |= ((addr >> (alen * 8)) &
397 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
400 i2c_transfer(I2C_READ, chip << 1, &xaddr[4 - alen], alen,
401 buffer, len)) != 0) {
402 printf("I2c read: failed %d\n", ret);
408 int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
413 printf("I2C write: addr len %d not supported\n", alen);
418 xaddr[0] = (addr >> 24) & 0xFF;
419 xaddr[1] = (addr >> 16) & 0xFF;
420 xaddr[2] = (addr >> 8) & 0xFF;
421 xaddr[3] = addr & 0xFF;
423 #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
425 * EEPROM chips that implement "address overflow" are ones
426 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
427 * address and the extra bits end up in the "chip address"
428 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
429 * four 256 byte chips.
431 * Note that we consider the length of the address field to
432 * still be one byte because the extra address bits are
433 * hidden in the chip address.
436 chip |= ((addr >> (alen * 8)) &
437 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
440 (I2C_WRITE, chip << 1, &xaddr[4 - alen], alen, buffer,
443 #endif /* CONFIG_HARD_I2C */