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 #if (defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
32 #include <asm/arch/clk.h>
33 #include <asm/arch/cpu.h>
34 #include <asm/arch/pinmux.h>
36 #include <asm/arch/s3c24x0_cpu.h>
40 #include "s3c24x0_i2c.h"
42 #ifdef CONFIG_HARD_I2C
50 #define I2C_NOK_LA 3 /* Lost arbitration */
51 #define I2C_NOK_TOUT 4 /* time out */
53 #define I2CSTAT_BSY 0x20 /* Busy bit */
54 #define I2CSTAT_NACK 0x01 /* Nack bit */
55 #define I2CCON_ACKGEN 0x80 /* Acknowledge generation */
56 #define I2CCON_IRPND 0x10 /* Interrupt pending bit */
57 #define I2C_MODE_MT 0xC0 /* Master Transmit Mode */
58 #define I2C_MODE_MR 0x80 /* Master Receive Mode */
59 #define I2C_START_STOP 0x20 /* START / STOP */
60 #define I2C_TXRX_ENA 0x10 /* I2C Tx/Rx enable */
62 #define I2C_TIMEOUT 1 /* 1 second */
66 * For SPL boot some boards need i2c before SDRAM is initialised so force
67 * variables to live in SRAM
69 static unsigned int g_current_bus __attribute__((section(".data")));
70 static struct s3c24x0_i2c_bus i2c_bus[CONFIG_MAX_I2C_NUM]
71 __attribute__((section(".data")));
72 static int i2c_busses __attribute__((section(".data")));
74 #if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
75 static int GetI2CSDA(void)
77 struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
80 return (readl(&gpio->gpedat) & 0x8000) >> 15;
83 return (readl(&gpio->pgdat) & 0x0020) >> 5;
88 static void SetI2CSDA(int x)
90 rGPEDAT = (rGPEDAT & ~0x8000) | (x & 1) << 15;
94 static void SetI2CSCL(int x)
96 struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
99 writel((readl(&gpio->gpedat) & ~0x4000) |
100 (x & 1) << 14, &gpio->gpedat);
102 #ifdef CONFIG_S3C2400
103 writel((readl(&gpio->pgdat) & ~0x0040) | (x & 1) << 6, &gpio->pgdat);
108 static int WaitForXfer(struct s3c24x0_i2c *i2c)
112 i = I2C_TIMEOUT * 10000;
113 while (!(readl(&i2c->iiccon) & I2CCON_IRPND) && (i > 0)) {
118 return (readl(&i2c->iiccon) & I2CCON_IRPND) ? I2C_OK : I2C_NOK_TOUT;
121 static int IsACK(struct s3c24x0_i2c *i2c)
123 return !(readl(&i2c->iicstat) & I2CSTAT_NACK);
126 static void ReadWriteByte(struct s3c24x0_i2c *i2c)
128 writel(readl(&i2c->iiccon) & ~I2CCON_IRPND, &i2c->iiccon);
131 static struct s3c24x0_i2c *get_base_i2c(void)
133 #ifdef CONFIG_EXYNOS4
134 struct s3c24x0_i2c *i2c = (struct s3c24x0_i2c *)(samsung_get_base_i2c()
135 + (EXYNOS4_I2C_SPACING
138 #elif defined CONFIG_EXYNOS5
139 struct s3c24x0_i2c *i2c = (struct s3c24x0_i2c *)(samsung_get_base_i2c()
140 + (EXYNOS5_I2C_SPACING
144 return s3c24x0_get_base_i2c();
148 static void i2c_ch_init(struct s3c24x0_i2c *i2c, int speed, int slaveadd)
150 ulong freq, pres = 16, div;
151 #if (defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
152 freq = get_i2c_clk();
156 /* calculate prescaler and divisor values */
157 if ((freq / pres / (16 + 1)) > speed)
158 /* set prescaler to 512 */
162 while ((freq / pres / (div + 1)) > speed)
165 /* set prescaler, divisor according to freq, also set ACKGEN, IRQ */
166 writel((div & 0x0F) | 0xA0 | ((pres == 512) ? 0x40 : 0), &i2c->iiccon);
168 /* init to SLAVE REVEIVE and set slaveaddr */
169 writel(0, &i2c->iicstat);
170 writel(slaveadd, &i2c->iicadd);
171 /* program Master Transmit (and implicit STOP) */
172 writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->iicstat);
176 * MULTI BUS I2C support
179 #ifdef CONFIG_I2C_MULTI_BUS
180 int i2c_set_bus_num(unsigned int bus)
182 struct s3c24x0_i2c *i2c;
184 if ((bus < 0) || (bus >= CONFIG_MAX_I2C_NUM)) {
185 debug("Bad bus: %d\n", bus);
190 i2c = get_base_i2c();
191 i2c_ch_init(i2c, CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
196 unsigned int i2c_get_bus_num(void)
198 return g_current_bus;
202 void i2c_init(int speed, int slaveadd)
204 struct s3c24x0_i2c *i2c;
205 #if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
206 struct s3c24x0_gpio *gpio = s3c24x0_get_base_gpio();
210 /* By default i2c channel 0 is the current bus */
212 i2c = get_base_i2c();
214 /* wait for some time to give previous transfer a chance to finish */
215 i = I2C_TIMEOUT * 1000;
216 while ((readl(&i2c->iicstat) & I2CSTAT_BSY) && (i > 0)) {
221 #if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
222 if ((readl(&i2c->iicstat) & I2CSTAT_BSY) || GetI2CSDA() == 0) {
223 #ifdef CONFIG_S3C2410
224 ulong old_gpecon = readl(&gpio->gpecon);
226 #ifdef CONFIG_S3C2400
227 ulong old_gpecon = readl(&gpio->pgcon);
229 /* bus still busy probably by (most) previously interrupted
232 #ifdef CONFIG_S3C2410
233 /* set I2CSDA and I2CSCL (GPE15, GPE14) to GPIO */
234 writel((readl(&gpio->gpecon) & ~0xF0000000) | 0x10000000,
237 #ifdef CONFIG_S3C2400
238 /* set I2CSDA and I2CSCL (PG5, PG6) to GPIO */
239 writel((readl(&gpio->pgcon) & ~0x00003c00) | 0x00001000,
243 /* toggle I2CSCL until bus idle */
247 while ((i > 0) && (GetI2CSDA() != 1)) {
257 /* restore pin functions */
258 #ifdef CONFIG_S3C2410
259 writel(old_gpecon, &gpio->gpecon);
261 #ifdef CONFIG_S3C2400
262 writel(old_gpecon, &gpio->pgcon);
265 #endif /* #if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5) */
266 i2c_ch_init(i2c, speed, slaveadd);
270 * cmd_type is 0 for write, 1 for read.
272 * addr_len can take any value from 0-255, it is only limited
273 * by the char, we could make it larger if needed. If it is
274 * 0 we skip the address write cycle.
276 static int i2c_transfer(struct s3c24x0_i2c *i2c,
277 unsigned char cmd_type,
279 unsigned char addr[],
280 unsigned char addr_len,
281 unsigned char data[],
282 unsigned short data_len)
286 if (data == 0 || data_len == 0) {
287 /*Don't support data transfer of no length or to address 0 */
288 debug("i2c_transfer: bad call\n");
292 /* Check I2C bus idle */
293 i = I2C_TIMEOUT * 1000;
294 while ((readl(&i2c->iicstat) & I2CSTAT_BSY) && (i > 0)) {
299 if (readl(&i2c->iicstat) & I2CSTAT_BSY)
302 writel(readl(&i2c->iiccon) | I2CCON_ACKGEN, &i2c->iiccon);
307 if (addr && addr_len) {
308 writel(chip, &i2c->iicds);
310 writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP,
313 while ((i < addr_len) && (result == I2C_OK)) {
314 result = WaitForXfer(i2c);
315 writel(addr[i], &i2c->iicds);
320 while ((i < data_len) && (result == I2C_OK)) {
321 result = WaitForXfer(i2c);
322 writel(data[i], &i2c->iicds);
327 writel(chip, &i2c->iicds);
329 writel(I2C_MODE_MT | I2C_TXRX_ENA | I2C_START_STOP,
332 while ((i < data_len) && (result = I2C_OK)) {
333 result = WaitForXfer(i2c);
334 writel(data[i], &i2c->iicds);
340 if (result == I2C_OK)
341 result = WaitForXfer(i2c);
344 writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
349 if (addr && addr_len) {
350 writel(I2C_MODE_MT | I2C_TXRX_ENA, &i2c->iicstat);
351 writel(chip, &i2c->iicds);
353 writel(readl(&i2c->iicstat) | I2C_START_STOP,
355 result = WaitForXfer(i2c);
358 while ((i < addr_len) && (result == I2C_OK)) {
359 writel(addr[i], &i2c->iicds);
361 result = WaitForXfer(i2c);
365 writel(chip, &i2c->iicds);
367 writel(I2C_MODE_MR | I2C_TXRX_ENA |
368 I2C_START_STOP, &i2c->iicstat);
370 result = WaitForXfer(i2c);
372 while ((i < data_len) && (result == I2C_OK)) {
373 /* disable ACK for final READ */
374 if (i == data_len - 1)
375 writel(readl(&i2c->iiccon)
379 result = WaitForXfer(i2c);
380 data[i] = readl(&i2c->iicds);
388 writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
389 writel(chip, &i2c->iicds);
391 writel(readl(&i2c->iicstat) | I2C_START_STOP,
393 result = WaitForXfer(i2c);
397 while ((i < data_len) && (result == I2C_OK)) {
398 /* disable ACK for final READ */
399 if (i == data_len - 1)
400 writel(readl(&i2c->iiccon) &
404 result = WaitForXfer(i2c);
405 data[i] = readl(&i2c->iicds);
414 writel(I2C_MODE_MR | I2C_TXRX_ENA, &i2c->iicstat);
419 debug("i2c_transfer: bad call\n");
427 int i2c_probe(uchar chip)
429 struct s3c24x0_i2c *i2c;
432 i2c = get_base_i2c();
436 * What is needed is to send the chip address and verify that the
437 * address was <ACK>ed (i.e. there was a chip at that address which
438 * drove the data line low).
440 return i2c_transfer(i2c, I2C_READ, chip << 1, 0, 0, buf, 1) != I2C_OK;
443 int i2c_read(uchar chip, uint addr, int alen, uchar *buffer, int len)
445 struct s3c24x0_i2c *i2c;
450 debug("I2C read: addr len %d not supported\n", alen);
455 xaddr[0] = (addr >> 24) & 0xFF;
456 xaddr[1] = (addr >> 16) & 0xFF;
457 xaddr[2] = (addr >> 8) & 0xFF;
458 xaddr[3] = addr & 0xFF;
461 #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
463 * EEPROM chips that implement "address overflow" are ones
464 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
465 * address and the extra bits end up in the "chip address"
466 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
467 * four 256 byte chips.
469 * Note that we consider the length of the address field to
470 * still be one byte because the extra address bits are
471 * hidden in the chip address.
474 chip |= ((addr >> (alen * 8)) &
475 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
477 i2c = get_base_i2c();
478 ret = i2c_transfer(i2c, I2C_READ, chip << 1, &xaddr[4 - alen], alen,
481 debug("I2c read: failed %d\n", ret);
487 int i2c_write(uchar chip, uint addr, int alen, uchar *buffer, int len)
489 struct s3c24x0_i2c *i2c;
493 debug("I2C write: addr len %d not supported\n", alen);
498 xaddr[0] = (addr >> 24) & 0xFF;
499 xaddr[1] = (addr >> 16) & 0xFF;
500 xaddr[2] = (addr >> 8) & 0xFF;
501 xaddr[3] = addr & 0xFF;
503 #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
505 * EEPROM chips that implement "address overflow" are ones
506 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
507 * address and the extra bits end up in the "chip address"
508 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
509 * four 256 byte chips.
511 * Note that we consider the length of the address field to
512 * still be one byte because the extra address bits are
513 * hidden in the chip address.
516 chip |= ((addr >> (alen * 8)) &
517 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
519 i2c = get_base_i2c();
521 (i2c, I2C_WRITE, chip << 1, &xaddr[4 - alen], alen, buffer,
525 #ifdef CONFIG_OF_CONTROL
526 void board_i2c_init(const void *blob)
528 int node_list[CONFIG_MAX_I2C_NUM];
531 count = fdtdec_find_aliases_for_id(blob, "i2c",
532 COMPAT_SAMSUNG_S3C2440_I2C, node_list,
535 for (i = 0; i < count; i++) {
536 struct s3c24x0_i2c_bus *bus;
537 int node = node_list[i];
542 bus->regs = (struct s3c24x0_i2c *)
543 fdtdec_get_addr(blob, node, "reg");
544 bus->id = pinmux_decode_periph_id(blob, node);
546 bus->bus_num = i2c_busses++;
547 exynos_pinmux_config(bus->id, 0);
551 static struct s3c24x0_i2c_bus *get_bus(unsigned int bus_idx)
553 if (bus_idx < i2c_busses)
554 return &i2c_bus[bus_idx];
556 debug("Undefined bus: %d\n", bus_idx);
560 int i2c_get_bus_num_fdt(int node)
564 for (i = 0; i < i2c_busses; i++) {
565 if (node == i2c_bus[i].node)
569 debug("%s: Can't find any matched I2C bus\n", __func__);
573 int i2c_reset_port_fdt(const void *blob, int node)
575 struct s3c24x0_i2c_bus *i2c;
578 bus = i2c_get_bus_num_fdt(node);
580 debug("could not get bus for node %d\n", node);
586 debug("get_bus() failed for node node %d\n", node);
590 i2c_ch_init(i2c->regs, CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
596 #endif /* CONFIG_HARD_I2C */