6 #include <asm/arch/nexell.h>
7 #include <asm/arch/reset.h>
8 #include <asm/arch/clk.h>
9 #include <asm/arch/nx_gpio.h>
10 #include <linux/delay.h>
15 #define I2CSTAT_MTM 0xC0 /* Master Transmit Mode */
16 #define I2CSTAT_MRM 0x80 /* Master Receive Mode */
17 #define I2CSTAT_BSY 0x20 /* Read: Bus Busy */
18 #define I2CSTAT_SS 0x20 /* Write: START (1) / STOP (0) */
19 #define I2CSTAT_RXTXEN 0x10 /* Rx/Tx enable */
20 #define I2CSTAT_ABT 0x08 /* Arbitration bit */
21 #define I2CSTAT_NACK 0x01 /* Nack bit */
22 #define I2CCON_IRCLR 0x100 /* Interrupt Clear bit */
23 #define I2CCON_ACKGEN 0x80 /* Acknowledge generation */
24 #define I2CCON_TCP256 0x40 /* Tx-clock prescaler: 16 (0) / 256 (1) */
25 #define I2CCON_IRENB 0x20 /* Interrupt Enable bit */
26 #define I2CCON_IRPND 0x10 /* Interrupt pending bit */
27 #define I2CCON_TCDMSK 0x0F /* I2C-bus transmit clock divider bit mask */
29 #ifdef CONFIG_ARCH_S5P6818
30 #define SDADLY_CLKSTEP 5 /* SDA delay: Reg. val. is multiple of 5 clks */
31 #define SDADLY_MAX 3 /* SDA delay: Max. reg. value is 3 */
32 #define I2CLC_FILTER 0x04 /* SDA filter on */
34 #define STOPCON_CLR 0x01 /* Clock Line Release */
35 #define STOPCON_DLR 0x02 /* Data Line Release */
36 #define STOPCON_NAG 0x04 /* not-ackn. generation and data shift cont. */
39 #define I2C_TIMEOUT_MS 10 /* 10 ms */
41 #define I2C_M_NOSTOP 0x100
45 #define DEFAULT_SPEED 100000 /* default I2C speed [Hz] */
47 DECLARE_GLOBAL_DATA_PTR;
54 #ifdef CONFIG_ARCH_S5P6818
55 /* S5P6818: Offset 0x10 is Line Control Register (SDA-delay, Filter) */
58 /* S5P4418: Offset 0x10 is Stop Control Register */
65 struct nx_i2c_regs *regs;
68 #ifdef CONFIG_ARCH_S5P6818
71 /* setup time for Stop condition [us] */
76 /* s5pxx18 i2c must be reset before enabled */
77 static void i2c_reset(int ch)
79 int rst_id = RESET_ID_I2C0 + ch;
81 nx_rstcon_setrst(rst_id, 0);
82 nx_rstcon_setrst(rst_id, 1);
85 static uint i2c_get_clkrate(struct nx_i2c_bus *bus)
88 int index = bus->bus_num;
89 char name[50] = {0, };
91 sprintf(name, "%s.%d", DEV_NAME_I2C, index);
92 clk = clk_get((const char *)name);
96 return clk_get_rate(clk);
99 static uint i2c_set_clk(struct nx_i2c_bus *bus, uint enb)
104 sprintf(name, "%s.%d", DEV_NAME_I2C, bus->bus_num);
105 clk = clk_get((const char *)name);
107 debug("%s(): clk_get(%s) error!\n",
108 __func__, (const char *)name);
119 #ifdef CONFIG_ARCH_S5P6818
120 /* Set SDA line delay, not available at S5P4418 */
121 static int nx_i2c_set_sda_delay(struct nx_i2c_bus *bus)
123 struct nx_i2c_regs *i2c = bus->regs;
128 /* get input clock of the I2C-controller */
129 pclk = i2c_get_clkrate(bus);
131 if (bus->sda_delay) {
132 /* t_pclk = period time of one pclk [ns] */
133 t_pclk = DIV_ROUND_UP(1000, pclk / 1000000);
134 /* delay = number of pclks required for sda_delay [ns] */
135 delay = DIV_ROUND_UP(bus->sda_delay, t_pclk);
136 /* delay = register value (step of 5 clocks) */
137 delay = DIV_ROUND_UP(delay, SDADLY_CLKSTEP);
138 /* max. possible register value = 3 */
139 if (delay > SDADLY_MAX) {
141 debug("%s(): sda-delay des.: %dns, sat. to max.: %dns (granularity: %dns)\n",
142 __func__, bus->sda_delay, t_pclk * delay * SDADLY_CLKSTEP,
143 t_pclk * SDADLY_CLKSTEP);
145 debug("%s(): sda-delay des.: %dns, act.: %dns (granularity: %dns)\n",
146 __func__, bus->sda_delay, t_pclk * delay * SDADLY_CLKSTEP,
147 t_pclk * SDADLY_CLKSTEP);
150 delay |= I2CLC_FILTER;
153 debug("%s(): sda-delay = 0\n", __func__);
157 writel(delay, &i2c->iiclc);
163 static int nx_i2c_set_bus_speed(struct udevice *dev, uint speed)
165 struct nx_i2c_bus *bus = dev_get_priv(dev);
166 struct nx_i2c_regs *i2c = bus->regs;
167 unsigned long pclk, pres = 16, div;
169 if (i2c_set_clk(bus, 1))
172 /* get input clock of the I2C-controller */
173 pclk = i2c_get_clkrate(bus);
175 /* calculate prescaler and divisor values */
176 if ((pclk / pres / (16 + 1)) > speed)
177 /* prescaler value 16 is too less --> set to 256 */
181 /* actual divider = div + 1 */
182 while ((pclk / pres / (div + 1)) > speed)
186 debug("%s(): pres==%ld, div==0x%lx is saturated to 0xF !)\n",
187 __func__, pres, div);
190 debug("%s(): pres==%ld, div==0x%lx)\n", __func__, pres, div);
193 /* set Tx-clock divisor and prescaler values */
194 writel((div & I2CCON_TCDMSK) | ((pres == 256) ? I2CCON_TCP256 : 0),
197 /* init to SLAVE REVEIVE and set slaveaddr */
198 writel(0, &i2c->iicstat);
199 writel(0x00, &i2c->iicadd);
201 /* program Master Transmit (and implicit STOP) */
202 writel(I2CSTAT_MTM | I2CSTAT_RXTXEN, &i2c->iicstat);
204 /* calculate actual I2C speed [Hz] */
205 bus->speed = pclk / ((div + 1) * pres);
206 debug("%s(): speed des.: %dHz, act.: %dHz\n",
207 __func__, speed, bus->speed);
209 #ifdef CONFIG_ARCH_S5P6818
210 nx_i2c_set_sda_delay(bus);
212 /* setup time for Stop condition [us], min. 4us @ 100kHz I2C-clock */
213 bus->tsu_stop = DIV_ROUND_UP(400, bus->speed / 1000);
216 if (i2c_set_clk(bus, 0))
221 static void i2c_process_node(struct udevice *dev)
223 struct nx_i2c_bus *bus = dev_get_priv(dev);
225 bus->target_speed = dev_read_s32_default(dev, "clock-frequency",
227 #ifdef CONFIG_ARCH_S5P6818
228 bus->sda_delay = dev_read_s32_default(dev, "i2c-sda-delay-ns", 0);
232 static int nx_i2c_probe(struct udevice *dev)
234 struct nx_i2c_bus *bus = dev_get_priv(dev);
237 /* get regs = i2c base address */
238 addr = devfdt_get_addr(dev);
239 if (addr == FDT_ADDR_T_NONE)
241 bus->regs = (struct nx_i2c_regs *)addr;
243 bus->bus_num = dev_seq(dev);
245 /* i2c node parsing */
246 i2c_process_node(dev);
247 if (!bus->target_speed)
251 i2c_reset(bus->bus_num);
256 /* i2c bus busy check */
257 static int i2c_is_busy(struct nx_i2c_regs *i2c)
261 start_time = get_timer(0);
262 while (readl(&i2c->iicstat) & I2CSTAT_BSY) {
263 if (get_timer(start_time) > I2C_TIMEOUT_MS) {
271 /* irq enable/disable functions */
272 static void i2c_enable_irq(struct nx_i2c_regs *i2c)
276 reg = readl(&i2c->iiccon);
278 writel(reg, &i2c->iiccon);
281 /* irq clear function */
282 static void i2c_clear_irq(struct nx_i2c_regs *i2c)
286 reg = readl(&i2c->iiccon);
287 /* reset interrupt pending flag */
288 reg &= ~(I2CCON_IRPND);
290 * Interrupt must also be cleared!
291 * Otherwise linux boot may hang after:
292 * [ 0.436000] NetLabel: unlabeled traffic allowed by default
294 * [ 0.442000] clocksource: Switched to clocksource source timer
297 writel(reg, &i2c->iiccon);
300 /* ack enable functions */
301 static void i2c_enable_ack(struct nx_i2c_regs *i2c)
305 reg = readl(&i2c->iiccon);
306 reg |= I2CCON_ACKGEN;
307 writel(reg, &i2c->iiccon);
310 static void i2c_send_stop(struct nx_i2c_bus *bus)
312 struct nx_i2c_regs *i2c = bus->regs;
314 if (IS_ENABLED(CONFIG_ARCH_S5P6818)) {
317 reg = readl(&i2c->iicstat);
318 reg |= I2CSTAT_MRM | I2CSTAT_RXTXEN;
319 reg &= (~I2CSTAT_SS);
321 writel(reg, &i2c->iicstat);
323 } else { /* S5P4418 */
324 writel(STOPCON_NAG, &i2c->iicstopcon);
329 * Clock Line Release --> SDC changes from Low to High and
330 * SDA from High to Low
332 writel(STOPCON_CLR, &i2c->iicstopcon);
334 /* Hold SDA Low (Setup Time for Stop condition) */
335 udelay(bus->tsu_stop);
339 /* Master Receive Mode Stop --> SDA becomes High */
340 writel(I2CSTAT_MRM, &i2c->iicstat);
344 static int wait_for_xfer(struct nx_i2c_regs *i2c)
346 unsigned long start_time = get_timer(0);
349 if (readl(&i2c->iiccon) & I2CCON_IRPND)
350 /* return -EREMOTEIO if not Acknowledged, otherwise 0 */
351 return (readl(&i2c->iicstat) & I2CSTAT_NACK) ?
353 } while (get_timer(start_time) < I2C_TIMEOUT_MS);
358 static int i2c_transfer(struct nx_i2c_regs *i2c,
364 unsigned short data_len,
370 /* Note: data_len = 0 is supported for "probe_chip" */
375 /* Get the slave chip address going */
377 writel(I2CSTAT_RXTXEN, &i2c->iicstat);
379 writel(chip_addr, &i2c->iicds);
380 status = I2CSTAT_RXTXEN | I2CSTAT_SS;
381 if (cmd_type == I2C_WRITE || (addr && addr_len))
382 status |= I2CSTAT_MTM;
384 status |= I2CSTAT_MRM;
386 writel(status, &i2c->iicstat);
390 /* Wait for chip address to transmit. */
391 result = wait_for_xfer(i2c);
393 debug("%s: transmitting chip address failed\n", __func__);
397 /* If register address needs to be transmitted - do it now. */
398 if (addr && addr_len) { /* register addr */
399 while ((i < addr_len) && !result) {
400 writel(addr[i++], &i2c->iicds);
402 result = wait_for_xfer(i2c);
407 debug("%s: transmitting register address failed\n",
415 while ((i < data_len) && !result) {
416 writel(data[i++], &i2c->iicds);
418 result = wait_for_xfer(i2c);
422 if (addr && addr_len) {
424 * Register address has been sent, now send slave chip
425 * address again to start the actual read transaction.
427 writel(chip_addr, &i2c->iicds);
429 /* Generate a re-START. */
430 writel(I2CSTAT_MRM | I2CSTAT_RXTXEN |
431 I2CSTAT_SS, &i2c->iicstat);
433 result = wait_for_xfer(i2c);
435 debug("%s: I2C_READ: sending chip addr. failed\n",
441 while ((i < data_len) && !result) {
442 /* disable ACK for final READ */
443 if (i == data_len - 1)
444 clrbits_le32(&i2c->iiccon, I2CCON_ACKGEN);
447 result = wait_for_xfer(i2c);
448 data[i++] = readb(&i2c->iicds);
451 if (result == -EREMOTEIO)
452 /* Not Acknowledged --> normal terminated read. */
454 else if (result == -ETIMEDOUT)
455 debug("%s: I2C_READ: time out\n", __func__);
457 debug("%s: I2C_READ: read not terminated with NACK\n",
462 debug("%s: bad call\n", __func__);
471 static int nx_i2c_read(struct udevice *dev, uchar chip_addr, uint addr,
472 uint alen, uchar *buffer, uint len, uint seq)
474 struct nx_i2c_bus *i2c;
478 i2c = dev_get_priv(dev);
483 debug("I2C read: addr len %d not supported\n", alen);
484 return -EADDRNOTAVAIL;
488 xaddr[0] = (addr >> 24) & 0xFF;
491 xaddr[0] = (addr >> 24) & 0xFF;
492 xaddr[1] = (addr >> 16) & 0xFF;
493 xaddr[2] = (addr >> 8) & 0xFF;
494 xaddr[3] = addr & 0xFF;
497 ret = i2c_transfer(i2c->regs, I2C_READ, chip_addr << 1,
498 &xaddr[4 - alen], alen, buffer, len, seq);
501 debug("I2C read failed %d\n", ret);
508 static int nx_i2c_write(struct udevice *dev, uchar chip_addr, uint addr,
509 uint alen, uchar *buffer, uint len, uint seq)
511 struct nx_i2c_bus *i2c;
515 i2c = dev_get_priv(dev);
520 debug("I2C write: addr len %d not supported\n", alen);
525 xaddr[0] = (addr >> 24) & 0xFF;
526 xaddr[1] = (addr >> 16) & 0xFF;
527 xaddr[2] = (addr >> 8) & 0xFF;
528 xaddr[3] = addr & 0xFF;
531 ret = i2c_transfer(i2c->regs, I2C_WRITE, chip_addr << 1,
532 &xaddr[4 - alen], alen, buffer, len, seq);
534 debug("I2C write failed %d\n", ret);
541 static int nx_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
543 struct nx_i2c_bus *bus = dev_get_priv(dev);
544 struct nx_i2c_regs *i2c = bus->regs;
548 /* The power loss by the clock, only during on/off. */
549 ret = i2c_set_clk(bus, 1);
552 /* Bus State(Busy) check */
553 ret = i2c_is_busy(i2c);
555 for (i = 0; i < nmsgs; msg++, i++) {
556 if (msg->flags & I2C_M_RD) {
557 ret = nx_i2c_read(dev, msg->addr, 0, 0,
558 msg->buf, msg->len, i);
560 ret = nx_i2c_write(dev, msg->addr, 0, 0,
561 msg->buf, msg->len, i);
565 debug("i2c_xfer: error sending\n");
571 if (i2c_set_clk(bus, 0))
578 static int nx_i2c_probe_chip(struct udevice *dev, u32 chip_addr,
582 struct nx_i2c_bus *bus = dev_get_priv(dev);
584 ret = i2c_set_clk(bus, 1);
588 * Send Chip Address only
589 * --> I2C transfer with data length and address length = 0.
590 * If there is a Slave, i2c_transfer() returns 0 (acknowledge
592 * I2C_WRITE must be used in order Master Transmit Mode is
593 * selected. Otherwise (in Master Receive Mode, I2C_READ)
594 * sending the stop condition below is not working (SDA does
595 * not transit to High).
597 ret = i2c_transfer(bus->regs, I2C_WRITE, (uchar)chip_addr << 1,
598 NULL, 0, NULL, 0, 0);
601 if (i2c_set_clk(bus, 0))
608 static const struct dm_i2c_ops nx_i2c_ops = {
610 .probe_chip = nx_i2c_probe_chip,
611 .set_bus_speed = nx_i2c_set_bus_speed,
614 static const struct udevice_id nx_i2c_ids[] = {
615 { .compatible = "nexell,s5pxx18-i2c" },
619 U_BOOT_DRIVER(i2c_nexell) = {
620 .name = "i2c_nexell",
622 .of_match = nx_i2c_ids,
623 .probe = nx_i2c_probe,
624 .priv_auto = sizeof(struct nx_i2c_bus),