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.
31 #include <asm/arch/clk.h>
32 #include <asm/arch/cpu.h>
34 #include <asm/arch/s3c24x0_cpu.h>
38 #include "s3c24x0_i2c.h"
40 #ifdef CONFIG_HARD_I2C
48 #define I2C_NOK_LA 3 /* Lost arbitration */
49 #define I2C_NOK_TOUT 4 /* time out */
51 #define I2CSTAT_BSY 0x20 /* Busy bit */
52 #define I2CSTAT_NACK 0x01 /* Nack bit */
53 #define I2CCON_ACKGEN 0x80 /* Acknowledge generation */
54 #define I2CCON_IRPND 0x10 /* Interrupt pending bit */
55 #define I2C_MODE_MT 0xC0 /* Master Transmit Mode */
56 #define I2C_MODE_MR 0x80 /* Master Receive Mode */
57 #define I2C_START_STOP 0x20 /* START / STOP */
58 #define I2C_TXRX_ENA 0x10 /* I2C Tx/Rx enable */
60 #define I2C_TIMEOUT 1 /* 1 second */
63 static unsigned int g_current_bus; /* Stores Current I2C Bus */
65 #ifndef CONFIG_EXYNOS5
66 static int GetI2CSDA(void)
68 struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
71 return (readl(&gpio->gpedat) & 0x8000) >> 15;
74 return (readl(&gpio->pgdat) & 0x0020) >> 5;
79 static void SetI2CSDA(int x)
81 rGPEDAT = (rGPEDAT & ~0x8000) | (x & 1) << 15;
85 static void SetI2CSCL(int x)
87 struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
90 writel((readl(&gpio->gpedat) & ~0x4000) |
91 (x & 1) << 14, &gpio->gpedat);
94 writel((readl(&gpio->pgdat) & ~0x0040) | (x & 1) << 6, &gpio->pgdat);
99 static int WaitForXfer(struct s3c24x0_i2c *i2c)
103 i = I2C_TIMEOUT * 10000;
104 while (!(readl(&i2c->iiccon) & I2CCON_IRPND) && (i > 0)) {
109 return (readl(&i2c->iiccon) & I2CCON_IRPND) ? I2C_OK : I2C_NOK_TOUT;
112 static int IsACK(struct s3c24x0_i2c *i2c)
114 return !(readl(&i2c->iicstat) & I2CSTAT_NACK);
117 static void ReadWriteByte(struct s3c24x0_i2c *i2c)
119 writel(readl(&i2c->iiccon) & ~I2CCON_IRPND, &i2c->iiccon);
122 static struct s3c24x0_i2c *get_base_i2c(void)
124 #ifdef CONFIG_EXYNOS5
125 struct s3c24x0_i2c *i2c = (struct s3c24x0_i2c *)(samsung_get_base_i2c()
126 + (EXYNOS5_I2C_SPACING
130 return s3c24x0_get_base_i2c();
134 static void i2c_ch_init(struct s3c24x0_i2c *i2c, int speed, int slaveadd)
136 ulong freq, pres = 16, div;
137 #ifdef CONFIG_EXYNOS5
138 freq = get_i2c_clk();
142 /* calculate prescaler and divisor values */
143 if ((freq / pres / (16 + 1)) > speed)
144 /* set prescaler to 512 */
148 while ((freq / pres / (div + 1)) > speed)
151 /* set prescaler, divisor according to freq, also set ACKGEN, IRQ */
152 writel((div & 0x0F) | 0xA0 | ((pres == 512) ? 0x40 : 0), &i2c->iiccon);
154 /* init to SLAVE REVEIVE and set slaveaddr */
155 writel(0, &i2c->iicstat);
156 writel(slaveadd, &i2c->iicadd);
157 /* program Master Transmit (and implicit STOP) */
158 writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->iicstat);
161 void i2c_init(int speed, int slaveadd)
163 struct s3c24x0_i2c *i2c;
164 #ifndef CONFIG_EXYNOS5
165 struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
169 /* By default i2c channel 0 is the current bus */
171 i2c = get_base_i2c();
173 /* wait for some time to give previous transfer a chance to finish */
174 i = I2C_TIMEOUT * 1000;
175 while ((readl(&i2c->iicstat) & I2CSTAT_BSY) && (i > 0)) {
180 #ifndef CONFIG_EXYNOS5
181 if ((readl(&i2c->iicstat) & I2CSTAT_BSY) || GetI2CSDA() == 0) {
182 #ifdef CONFIG_S3C2410
183 ulong old_gpecon = readl(&gpio->gpecon);
185 #ifdef CONFIG_S3C2400
186 ulong old_gpecon = readl(&gpio->pgcon);
188 /* bus still busy probably by (most) previously interrupted
191 #ifdef CONFIG_S3C2410
192 /* set I2CSDA and I2CSCL (GPE15, GPE14) to GPIO */
193 writel((readl(&gpio->gpecon) & ~0xF0000000) | 0x10000000,
196 #ifdef CONFIG_S3C2400
197 /* set I2CSDA and I2CSCL (PG5, PG6) to GPIO */
198 writel((readl(&gpio->pgcon) & ~0x00003c00) | 0x00001000,
202 /* toggle I2CSCL until bus idle */
206 while ((i > 0) && (GetI2CSDA() != 1)) {
216 /* restore pin functions */
217 #ifdef CONFIG_S3C2410
218 writel(old_gpecon, &gpio->gpecon);
220 #ifdef CONFIG_S3C2400
221 writel(old_gpecon, &gpio->pgcon);
224 #endif /* #ifndef CONFIG_EXYNOS5 */
225 i2c_ch_init(i2c, speed, slaveadd);
229 * cmd_type is 0 for write, 1 for read.
231 * addr_len can take any value from 0-255, it is only limited
232 * by the char, we could make it larger if needed. If it is
233 * 0 we skip the address write cycle.
235 static int i2c_transfer(struct s3c24x0_i2c *i2c,
236 unsigned char cmd_type,
238 unsigned char addr[],
239 unsigned char addr_len,
240 unsigned char data[],
241 unsigned short data_len)
245 if (data == 0 || data_len == 0) {
246 /*Don't support data transfer of no length or to address 0 */
247 debug("i2c_transfer: bad call\n");
251 /* Check I2C bus idle */
252 i = I2C_TIMEOUT * 1000;
253 while ((readl(&i2c->iicstat) & I2CSTAT_BSY) && (i > 0)) {
258 if (readl(&i2c->iicstat) & I2CSTAT_BSY)
261 writel(readl(&i2c->iiccon) | I2CCON_ACKGEN, &i2c->iiccon);
266 if (addr && addr_len) {
267 writel(chip, &i2c->iicds);
269 writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP,
272 while ((i < addr_len) && (result == I2C_OK)) {
273 result = WaitForXfer(i2c);
274 writel(addr[i], &i2c->iicds);
279 while ((i < data_len) && (result == I2C_OK)) {
280 result = WaitForXfer(i2c);
281 writel(data[i], &i2c->iicds);
286 writel(chip, &i2c->iicds);
288 writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP,
291 while ((i < data_len) && (result = I2C_OK)) {
292 result = WaitForXfer(i2c);
293 writel(data[i], &i2c->iicds);
299 if (result == I2C_OK)
300 result = WaitForXfer(i2c);
303 writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
308 if (addr && addr_len) {
309 writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->iicstat);
310 writel(chip, &i2c->iicds);
312 writel(readl(&i2c->iicstat) | I2C_START_STOP,
314 result = WaitForXfer(i2c);
317 while ((i < addr_len) && (result == I2C_OK)) {
318 writel(addr[i], &i2c->iicds);
320 result = WaitForXfer(i2c);
324 writel(chip, &i2c->iicds);
326 writel(I2C_MODE_MR | I2C_TXRX_ENA |
327 I2C_START_STOP, &i2c->iicstat);
329 result = WaitForXfer(i2c);
331 while ((i < data_len) && (result == I2C_OK)) {
332 /* disable ACK for final READ */
333 if (i == data_len - 1)
334 writel(readl(&i2c->iiccon)
338 result = WaitForXfer(i2c);
339 data[i] = readl(&i2c->iicds);
347 writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
348 writel(chip, &i2c->iicds);
350 writel(readl(&i2c->iicstat) | I2C_START_STOP,
352 result = WaitForXfer(i2c);
356 while ((i < data_len) && (result == I2C_OK)) {
357 /* disable ACK for final READ */
358 if (i == data_len - 1)
359 writel(readl(&i2c->iiccon) &
363 result = WaitForXfer(i2c);
364 data[i] = readl(&i2c->iicds);
373 writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
378 debug("i2c_transfer: bad call\n");
386 int i2c_probe(uchar chip)
388 struct s3c24x0_i2c *i2c;
391 i2c = get_base_i2c();
395 * What is needed is to send the chip address and verify that the
396 * address was <ACK>ed (i.e. there was a chip at that address which
397 * drove the data line low).
399 return i2c_transfer(i2c, I2C_READ, chip << 1, 0, 0, buf, 1) != I2C_OK;
402 int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
404 struct s3c24x0_i2c *i2c;
409 debug("I2C read: addr len %d not supported\n", alen);
414 xaddr[0] = (addr >> 24) & 0xFF;
415 xaddr[1] = (addr >> 16) & 0xFF;
416 xaddr[2] = (addr >> 8) & 0xFF;
417 xaddr[3] = addr & 0xFF;
420 #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
422 * EEPROM chips that implement "address overflow" are ones
423 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
424 * address and the extra bits end up in the "chip address"
425 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
426 * four 256 byte chips.
428 * Note that we consider the length of the address field to
429 * still be one byte because the extra address bits are
430 * hidden in the chip address.
433 chip |= ((addr >> (alen * 8)) &
434 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
436 i2c = get_base_i2c();
437 ret = i2c_transfer(i2c, I2C_READ, chip << 1, &xaddr[4 - alen], alen,
440 debug("I2c read: failed %d\n", ret);
446 int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
448 struct s3c24x0_i2c *i2c;
452 debug("I2C write: addr len %d not supported\n", alen);
457 xaddr[0] = (addr >> 24) & 0xFF;
458 xaddr[1] = (addr >> 16) & 0xFF;
459 xaddr[2] = (addr >> 8) & 0xFF;
460 xaddr[3] = addr & 0xFF;
462 #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
464 * EEPROM chips that implement "address overflow" are ones
465 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
466 * address and the extra bits end up in the "chip address"
467 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
468 * four 256 byte chips.
470 * Note that we consider the length of the address field to
471 * still be one byte because the extra address bits are
472 * hidden in the chip address.
475 chip |= ((addr >> (alen * 8)) &
476 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
478 i2c = get_base_i2c();
480 (i2c, I2C_WRITE, chip << 1, &xaddr[4 - alen], alen, buffer,
483 #endif /* CONFIG_HARD_I2C */