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 #ifdef CONFIG_OF_CONTROL
71 static int i2c_busses
__attribute__((section(".data")));
72 static struct s3c24x0_i2c_bus i2c_bus
[CONFIG_MAX_I2C_NUM
]
73 __attribute__((section(".data")));
76 #if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
77 static int GetI2CSDA(void)
79 struct s3c24x0_gpio
*gpio
= s3c24x0_get_base_gpio();
82 return (readl(&gpio
->gpedat
) & 0x8000) >> 15;
85 return (readl(&gpio
->pgdat
) & 0x0020) >> 5;
89 static void SetI2CSCL(int x
)
91 struct s3c24x0_gpio
*gpio
= s3c24x0_get_base_gpio();
94 writel((readl(&gpio
->gpedat
) & ~0x4000) |
95 (x
& 1) << 14, &gpio
->gpedat
);
98 writel((readl(&gpio
->pgdat
) & ~0x0040) | (x
& 1) << 6, &gpio
->pgdat
);
103 static int WaitForXfer(struct s3c24x0_i2c
*i2c
)
107 i
= I2C_TIMEOUT
* 10000;
108 while (!(readl(&i2c
->iiccon
) & I2CCON_IRPND
) && (i
> 0)) {
113 return (readl(&i2c
->iiccon
) & I2CCON_IRPND
) ? I2C_OK
: I2C_NOK_TOUT
;
116 static int IsACK(struct s3c24x0_i2c
*i2c
)
118 return !(readl(&i2c
->iicstat
) & I2CSTAT_NACK
);
121 static void ReadWriteByte(struct s3c24x0_i2c
*i2c
)
123 writel(readl(&i2c
->iiccon
) & ~I2CCON_IRPND
, &i2c
->iiccon
);
126 static struct s3c24x0_i2c
*get_base_i2c(void)
128 #ifdef CONFIG_EXYNOS4
129 struct s3c24x0_i2c
*i2c
= (struct s3c24x0_i2c
*)(samsung_get_base_i2c()
130 + (EXYNOS4_I2C_SPACING
133 #elif defined CONFIG_EXYNOS5
134 struct s3c24x0_i2c
*i2c
= (struct s3c24x0_i2c
*)(samsung_get_base_i2c()
135 + (EXYNOS5_I2C_SPACING
139 return s3c24x0_get_base_i2c();
143 static void i2c_ch_init(struct s3c24x0_i2c
*i2c
, int speed
, int slaveadd
)
145 ulong freq
, pres
= 16, div
;
146 #if (defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
147 freq
= get_i2c_clk();
151 /* calculate prescaler and divisor values */
152 if ((freq
/ pres
/ (16 + 1)) > speed
)
153 /* set prescaler to 512 */
157 while ((freq
/ pres
/ (div
+ 1)) > speed
)
160 /* set prescaler, divisor according to freq, also set ACKGEN, IRQ */
161 writel((div
& 0x0F) | 0xA0 | ((pres
== 512) ? 0x40 : 0), &i2c
->iiccon
);
163 /* init to SLAVE REVEIVE and set slaveaddr */
164 writel(0, &i2c
->iicstat
);
165 writel(slaveadd
, &i2c
->iicadd
);
166 /* program Master Transmit (and implicit STOP) */
167 writel(I2C_MODE_MT
| I2C_TXRX_ENA
, &i2c
->iicstat
);
171 * MULTI BUS I2C support
174 #ifdef CONFIG_I2C_MULTI_BUS
175 int i2c_set_bus_num(unsigned int bus
)
177 struct s3c24x0_i2c
*i2c
;
179 if ((bus
< 0) || (bus
>= CONFIG_MAX_I2C_NUM
)) {
180 debug("Bad bus: %d\n", bus
);
185 i2c
= get_base_i2c();
186 i2c_ch_init(i2c
, CONFIG_SYS_I2C_SPEED
, CONFIG_SYS_I2C_SLAVE
);
191 unsigned int i2c_get_bus_num(void)
193 return g_current_bus
;
197 void i2c_init(int speed
, int slaveadd
)
199 struct s3c24x0_i2c
*i2c
;
200 #if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
201 struct s3c24x0_gpio
*gpio
= s3c24x0_get_base_gpio();
205 /* By default i2c channel 0 is the current bus */
207 i2c
= get_base_i2c();
209 /* wait for some time to give previous transfer a chance to finish */
210 i
= I2C_TIMEOUT
* 1000;
211 while ((readl(&i2c
->iicstat
) & I2CSTAT_BSY
) && (i
> 0)) {
216 #if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5)
217 if ((readl(&i2c
->iicstat
) & I2CSTAT_BSY
) || GetI2CSDA() == 0) {
218 #ifdef CONFIG_S3C2410
219 ulong old_gpecon
= readl(&gpio
->gpecon
);
221 #ifdef CONFIG_S3C2400
222 ulong old_gpecon
= readl(&gpio
->pgcon
);
224 /* bus still busy probably by (most) previously interrupted
227 #ifdef CONFIG_S3C2410
228 /* set I2CSDA and I2CSCL (GPE15, GPE14) to GPIO */
229 writel((readl(&gpio
->gpecon
) & ~0xF0000000) | 0x10000000,
232 #ifdef CONFIG_S3C2400
233 /* set I2CSDA and I2CSCL (PG5, PG6) to GPIO */
234 writel((readl(&gpio
->pgcon
) & ~0x00003c00) | 0x00001000,
238 /* toggle I2CSCL until bus idle */
242 while ((i
> 0) && (GetI2CSDA() != 1)) {
252 /* restore pin functions */
253 #ifdef CONFIG_S3C2410
254 writel(old_gpecon
, &gpio
->gpecon
);
256 #ifdef CONFIG_S3C2400
257 writel(old_gpecon
, &gpio
->pgcon
);
260 #endif /* #if !(defined CONFIG_EXYNOS4 || defined CONFIG_EXYNOS5) */
261 i2c_ch_init(i2c
, speed
, slaveadd
);
265 * cmd_type is 0 for write, 1 for read.
267 * addr_len can take any value from 0-255, it is only limited
268 * by the char, we could make it larger if needed. If it is
269 * 0 we skip the address write cycle.
271 static int i2c_transfer(struct s3c24x0_i2c
*i2c
,
272 unsigned char cmd_type
,
274 unsigned char addr
[],
275 unsigned char addr_len
,
276 unsigned char data
[],
277 unsigned short data_len
)
281 if (data
== 0 || data_len
== 0) {
282 /*Don't support data transfer of no length or to address 0 */
283 debug("i2c_transfer: bad call\n");
287 /* Check I2C bus idle */
288 i
= I2C_TIMEOUT
* 1000;
289 while ((readl(&i2c
->iicstat
) & I2CSTAT_BSY
) && (i
> 0)) {
294 if (readl(&i2c
->iicstat
) & I2CSTAT_BSY
)
297 writel(readl(&i2c
->iiccon
) | I2CCON_ACKGEN
, &i2c
->iiccon
);
302 if (addr
&& addr_len
) {
303 writel(chip
, &i2c
->iicds
);
305 writel(I2C_MODE_MT
| I2C_TXRX_ENA
| I2C_START_STOP
,
308 while ((i
< addr_len
) && (result
== I2C_OK
)) {
309 result
= WaitForXfer(i2c
);
310 writel(addr
[i
], &i2c
->iicds
);
315 while ((i
< data_len
) && (result
== I2C_OK
)) {
316 result
= WaitForXfer(i2c
);
317 writel(data
[i
], &i2c
->iicds
);
322 writel(chip
, &i2c
->iicds
);
324 writel(I2C_MODE_MT
| I2C_TXRX_ENA
| I2C_START_STOP
,
327 while ((i
< data_len
) && (result
== I2C_OK
)) {
328 result
= WaitForXfer(i2c
);
329 writel(data
[i
], &i2c
->iicds
);
335 if (result
== I2C_OK
)
336 result
= WaitForXfer(i2c
);
339 writel(I2C_MODE_MT
| I2C_TXRX_ENA
, &i2c
->iicstat
);
344 if (addr
&& addr_len
) {
345 writel(chip
, &i2c
->iicds
);
347 writel(I2C_MODE_MT
| I2C_TXRX_ENA
| I2C_START_STOP
,
349 result
= WaitForXfer(i2c
);
352 while ((i
< addr_len
) && (result
== I2C_OK
)) {
353 writel(addr
[i
], &i2c
->iicds
);
355 result
= WaitForXfer(i2c
);
359 writel(chip
, &i2c
->iicds
);
361 writel(I2C_MODE_MR
| I2C_TXRX_ENA
|
362 I2C_START_STOP
, &i2c
->iicstat
);
364 result
= WaitForXfer(i2c
);
366 while ((i
< data_len
) && (result
== I2C_OK
)) {
367 /* disable ACK for final READ */
368 if (i
== data_len
- 1)
369 writel(readl(&i2c
->iiccon
)
373 result
= WaitForXfer(i2c
);
374 data
[i
] = readl(&i2c
->iicds
);
382 writel(chip
, &i2c
->iicds
);
384 writel(I2C_MODE_MR
| I2C_TXRX_ENA
| I2C_START_STOP
,
386 result
= WaitForXfer(i2c
);
390 while ((i
< data_len
) && (result
== I2C_OK
)) {
391 /* disable ACK for final READ */
392 if (i
== data_len
- 1)
393 writel(readl(&i2c
->iiccon
) &
397 result
= WaitForXfer(i2c
);
398 data
[i
] = readl(&i2c
->iicds
);
407 writel(I2C_MODE_MR
| I2C_TXRX_ENA
, &i2c
->iicstat
);
412 debug("i2c_transfer: bad call\n");
420 int i2c_probe(uchar chip
)
422 struct s3c24x0_i2c
*i2c
;
425 i2c
= get_base_i2c();
429 * What is needed is to send the chip address and verify that the
430 * address was <ACK>ed (i.e. there was a chip at that address which
431 * drove the data line low).
433 return i2c_transfer(i2c
, I2C_READ
, chip
<< 1, 0, 0, buf
, 1) != I2C_OK
;
436 int i2c_read(uchar chip
, uint addr
, int alen
, uchar
*buffer
, int len
)
438 struct s3c24x0_i2c
*i2c
;
443 debug("I2C read: addr len %d not supported\n", alen
);
448 xaddr
[0] = (addr
>> 24) & 0xFF;
449 xaddr
[1] = (addr
>> 16) & 0xFF;
450 xaddr
[2] = (addr
>> 8) & 0xFF;
451 xaddr
[3] = addr
& 0xFF;
454 #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
456 * EEPROM chips that implement "address overflow" are ones
457 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
458 * address and the extra bits end up in the "chip address"
459 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
460 * four 256 byte chips.
462 * Note that we consider the length of the address field to
463 * still be one byte because the extra address bits are
464 * hidden in the chip address.
467 chip
|= ((addr
>> (alen
* 8)) &
468 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
);
470 i2c
= get_base_i2c();
471 ret
= i2c_transfer(i2c
, I2C_READ
, chip
<< 1, &xaddr
[4 - alen
], alen
,
474 debug("I2c read: failed %d\n", ret
);
480 int i2c_write(uchar chip
, uint addr
, int alen
, uchar
*buffer
, int len
)
482 struct s3c24x0_i2c
*i2c
;
486 debug("I2C write: addr len %d not supported\n", alen
);
491 xaddr
[0] = (addr
>> 24) & 0xFF;
492 xaddr
[1] = (addr
>> 16) & 0xFF;
493 xaddr
[2] = (addr
>> 8) & 0xFF;
494 xaddr
[3] = addr
& 0xFF;
496 #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
498 * EEPROM chips that implement "address overflow" are ones
499 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
500 * address and the extra bits end up in the "chip address"
501 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
502 * four 256 byte chips.
504 * Note that we consider the length of the address field to
505 * still be one byte because the extra address bits are
506 * hidden in the chip address.
509 chip
|= ((addr
>> (alen
* 8)) &
510 CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
);
512 i2c
= get_base_i2c();
514 (i2c
, I2C_WRITE
, chip
<< 1, &xaddr
[4 - alen
], alen
, buffer
,
518 #ifdef CONFIG_OF_CONTROL
519 void board_i2c_init(const void *blob
)
521 int node_list
[CONFIG_MAX_I2C_NUM
];
524 count
= fdtdec_find_aliases_for_id(blob
, "i2c",
525 COMPAT_SAMSUNG_S3C2440_I2C
, node_list
,
528 for (i
= 0; i
< count
; i
++) {
529 struct s3c24x0_i2c_bus
*bus
;
530 int node
= node_list
[i
];
535 bus
->regs
= (struct s3c24x0_i2c
*)
536 fdtdec_get_addr(blob
, node
, "reg");
537 bus
->id
= pinmux_decode_periph_id(blob
, node
);
539 bus
->bus_num
= i2c_busses
++;
540 exynos_pinmux_config(bus
->id
, 0);
544 static struct s3c24x0_i2c_bus
*get_bus(unsigned int bus_idx
)
546 if (bus_idx
< i2c_busses
)
547 return &i2c_bus
[bus_idx
];
549 debug("Undefined bus: %d\n", bus_idx
);
553 int i2c_get_bus_num_fdt(int node
)
557 for (i
= 0; i
< i2c_busses
; i
++) {
558 if (node
== i2c_bus
[i
].node
)
562 debug("%s: Can't find any matched I2C bus\n", __func__
);
566 int i2c_reset_port_fdt(const void *blob
, int node
)
568 struct s3c24x0_i2c_bus
*i2c
;
571 bus
= i2c_get_bus_num_fdt(node
);
573 debug("could not get bus for node %d\n", node
);
579 debug("get_bus() failed for node node %d\n", node
);
583 i2c_ch_init(i2c
->regs
, CONFIG_SYS_I2C_SPEED
, CONFIG_SYS_I2C_SLAVE
);
589 #endif /* CONFIG_HARD_I2C */