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 <asm/global_data.h>
11 #include <linux/delay.h>
16 #define I2CSTAT_MTM 0xC0 /* Master Transmit Mode */
17 #define I2CSTAT_MRM 0x80 /* Master Receive Mode */
18 #define I2CSTAT_BSY 0x20 /* Read: Bus Busy */
19 #define I2CSTAT_SS 0x20 /* Write: START (1) / STOP (0) */
20 #define I2CSTAT_RXTXEN 0x10 /* Rx/Tx enable */
21 #define I2CSTAT_ABT 0x08 /* Arbitration bit */
22 #define I2CSTAT_NACK 0x01 /* Nack bit */
23 #define I2CCON_IRCLR 0x100 /* Interrupt Clear bit */
24 #define I2CCON_ACKGEN 0x80 /* Acknowledge generation */
25 #define I2CCON_TCP256 0x40 /* Tx-clock prescaler: 16 (0) / 256 (1) */
26 #define I2CCON_IRENB 0x20 /* Interrupt Enable bit */
27 #define I2CCON_IRPND 0x10 /* Interrupt pending bit */
28 #define I2CCON_TCDMSK 0x0F /* I2C-bus transmit clock divider bit mask */
30 #ifdef CONFIG_ARCH_S5P6818
31 #define SDADLY_CLKSTEP 5 /* SDA delay: Reg. val. is multiple of 5 clks */
32 #define SDADLY_MAX 3 /* SDA delay: Max. reg. value is 3 */
33 #define I2CLC_FILTER 0x04 /* SDA filter on */
35 #define STOPCON_CLR 0x01 /* Clock Line Release */
36 #define STOPCON_DLR 0x02 /* Data Line Release */
37 #define STOPCON_NAG 0x04 /* not-ackn. generation and data shift cont. */
40 #define I2C_TIMEOUT_MS 10 /* 10 ms */
42 #define I2C_M_NOSTOP 0x100
46 #define DEFAULT_SPEED 100000 /* default I2C speed [Hz] */
48 DECLARE_GLOBAL_DATA_PTR;
55 #ifdef CONFIG_ARCH_S5P6818
56 /* S5P6818: Offset 0x10 is Line Control Register (SDA-delay, Filter) */
59 /* S5P4418: Offset 0x10 is Stop Control Register */
66 struct nx_i2c_regs *regs;
69 #ifdef CONFIG_ARCH_S5P6818
72 /* setup time for Stop condition [us] */
77 /* s5pxx18 i2c must be reset before enabled */
78 static void i2c_reset(int ch)
80 int rst_id = RESET_ID_I2C0 + ch;
82 nx_rstcon_setrst(rst_id, 0);
83 nx_rstcon_setrst(rst_id, 1);
86 static uint i2c_get_clkrate(struct nx_i2c_bus *bus)
89 int index = bus->bus_num;
90 char name[50] = {0, };
92 sprintf(name, "%s.%d", DEV_NAME_I2C, index);
93 clk = clk_get((const char *)name);
97 return clk_get_rate(clk);
100 static uint i2c_set_clk(struct nx_i2c_bus *bus, uint enb)
105 sprintf(name, "%s.%d", DEV_NAME_I2C, bus->bus_num);
106 clk = clk_get((const char *)name);
108 debug("%s(): clk_get(%s) error!\n",
109 __func__, (const char *)name);
120 #ifdef CONFIG_ARCH_S5P6818
121 /* Set SDA line delay, not available at S5P4418 */
122 static int nx_i2c_set_sda_delay(struct nx_i2c_bus *bus)
124 struct nx_i2c_regs *i2c = bus->regs;
129 /* get input clock of the I2C-controller */
130 pclk = i2c_get_clkrate(bus);
132 if (bus->sda_delay) {
133 /* t_pclk = period time of one pclk [ns] */
134 t_pclk = DIV_ROUND_UP(1000, pclk / 1000000);
135 /* delay = number of pclks required for sda_delay [ns] */
136 delay = DIV_ROUND_UP(bus->sda_delay, t_pclk);
137 /* delay = register value (step of 5 clocks) */
138 delay = DIV_ROUND_UP(delay, SDADLY_CLKSTEP);
139 /* max. possible register value = 3 */
140 if (delay > SDADLY_MAX) {
142 debug("%s(): sda-delay des.: %dns, sat. to max.: %dns (granularity: %dns)\n",
143 __func__, bus->sda_delay, t_pclk * delay * SDADLY_CLKSTEP,
144 t_pclk * SDADLY_CLKSTEP);
146 debug("%s(): sda-delay des.: %dns, act.: %dns (granularity: %dns)\n",
147 __func__, bus->sda_delay, t_pclk * delay * SDADLY_CLKSTEP,
148 t_pclk * SDADLY_CLKSTEP);
151 delay |= I2CLC_FILTER;
154 debug("%s(): sda-delay = 0\n", __func__);
158 writel(delay, &i2c->iiclc);
164 static int nx_i2c_set_bus_speed(struct udevice *dev, uint speed)
166 struct nx_i2c_bus *bus = dev_get_priv(dev);
167 struct nx_i2c_regs *i2c = bus->regs;
168 unsigned long pclk, pres = 16, div;
170 if (i2c_set_clk(bus, 1))
173 /* get input clock of the I2C-controller */
174 pclk = i2c_get_clkrate(bus);
176 /* calculate prescaler and divisor values */
177 if ((pclk / pres / (16 + 1)) > speed)
178 /* prescaler value 16 is too less --> set to 256 */
182 /* actual divider = div + 1 */
183 while ((pclk / pres / (div + 1)) > speed)
187 debug("%s(): pres==%ld, div==0x%lx is saturated to 0xF !)\n",
188 __func__, pres, div);
191 debug("%s(): pres==%ld, div==0x%lx)\n", __func__, pres, div);
194 /* set Tx-clock divisor and prescaler values */
195 writel((div & I2CCON_TCDMSK) | ((pres == 256) ? I2CCON_TCP256 : 0),
198 /* init to SLAVE REVEIVE and set slaveaddr */
199 writel(0, &i2c->iicstat);
200 writel(0x00, &i2c->iicadd);
202 /* program Master Transmit (and implicit STOP) */
203 writel(I2CSTAT_MTM | I2CSTAT_RXTXEN, &i2c->iicstat);
205 /* calculate actual I2C speed [Hz] */
206 bus->speed = pclk / ((div + 1) * pres);
207 debug("%s(): speed des.: %dHz, act.: %dHz\n",
208 __func__, speed, bus->speed);
210 #ifdef CONFIG_ARCH_S5P6818
211 nx_i2c_set_sda_delay(bus);
213 /* setup time for Stop condition [us], min. 4us @ 100kHz I2C-clock */
214 bus->tsu_stop = DIV_ROUND_UP(400, bus->speed / 1000);
217 if (i2c_set_clk(bus, 0))
222 static void i2c_process_node(struct udevice *dev)
224 struct nx_i2c_bus *bus = dev_get_priv(dev);
226 bus->target_speed = dev_read_s32_default(dev, "clock-frequency",
228 #ifdef CONFIG_ARCH_S5P6818
229 bus->sda_delay = dev_read_s32_default(dev, "i2c-sda-delay-ns", 0);
233 static int nx_i2c_probe(struct udevice *dev)
235 struct nx_i2c_bus *bus = dev_get_priv(dev);
238 /* get regs = i2c base address */
239 addr = devfdt_get_addr(dev);
240 if (addr == FDT_ADDR_T_NONE)
242 bus->regs = (struct nx_i2c_regs *)addr;
244 bus->bus_num = dev_seq(dev);
246 /* i2c node parsing */
247 i2c_process_node(dev);
248 if (!bus->target_speed)
252 i2c_reset(bus->bus_num);
257 /* i2c bus busy check */
258 static int i2c_is_busy(struct nx_i2c_regs *i2c)
262 start_time = get_timer(0);
263 while (readl(&i2c->iicstat) & I2CSTAT_BSY) {
264 if (get_timer(start_time) > I2C_TIMEOUT_MS) {
272 /* irq enable/disable functions */
273 static void i2c_enable_irq(struct nx_i2c_regs *i2c)
277 reg = readl(&i2c->iiccon);
279 writel(reg, &i2c->iiccon);
282 /* irq clear function */
283 static void i2c_clear_irq(struct nx_i2c_regs *i2c)
287 reg = readl(&i2c->iiccon);
288 /* reset interrupt pending flag */
289 reg &= ~(I2CCON_IRPND);
291 * Interrupt must also be cleared!
292 * Otherwise linux boot may hang after:
293 * [ 0.436000] NetLabel: unlabeled traffic allowed by default
295 * [ 0.442000] clocksource: Switched to clocksource source timer
298 writel(reg, &i2c->iiccon);
301 /* ack enable functions */
302 static void i2c_enable_ack(struct nx_i2c_regs *i2c)
306 reg = readl(&i2c->iiccon);
307 reg |= I2CCON_ACKGEN;
308 writel(reg, &i2c->iiccon);
311 static void i2c_send_stop(struct nx_i2c_bus *bus)
313 struct nx_i2c_regs *i2c = bus->regs;
315 if (IS_ENABLED(CONFIG_ARCH_S5P6818)) {
318 reg = readl(&i2c->iicstat);
319 reg |= I2CSTAT_MRM | I2CSTAT_RXTXEN;
320 reg &= (~I2CSTAT_SS);
322 writel(reg, &i2c->iicstat);
324 } else { /* S5P4418 */
325 writel(STOPCON_NAG, &i2c->iicstopcon);
330 * Clock Line Release --> SDC changes from Low to High and
331 * SDA from High to Low
333 writel(STOPCON_CLR, &i2c->iicstopcon);
335 /* Hold SDA Low (Setup Time for Stop condition) */
336 udelay(bus->tsu_stop);
340 /* Master Receive Mode Stop --> SDA becomes High */
341 writel(I2CSTAT_MRM, &i2c->iicstat);
345 static int wait_for_xfer(struct nx_i2c_regs *i2c)
347 unsigned long start_time = get_timer(0);
350 if (readl(&i2c->iiccon) & I2CCON_IRPND)
351 /* return -EREMOTEIO if not Acknowledged, otherwise 0 */
352 return (readl(&i2c->iicstat) & I2CSTAT_NACK) ?
354 } while (get_timer(start_time) < I2C_TIMEOUT_MS);
359 static int i2c_transfer(struct nx_i2c_regs *i2c,
365 unsigned short data_len,
371 /* Note: data_len = 0 is supported for "probe_chip" */
376 /* Get the slave chip address going */
378 writel(I2CSTAT_RXTXEN, &i2c->iicstat);
380 writel(chip_addr, &i2c->iicds);
381 status = I2CSTAT_RXTXEN | I2CSTAT_SS;
382 if (cmd_type == I2C_WRITE || (addr && addr_len))
383 status |= I2CSTAT_MTM;
385 status |= I2CSTAT_MRM;
387 writel(status, &i2c->iicstat);
391 /* Wait for chip address to transmit. */
392 result = wait_for_xfer(i2c);
394 debug("%s: transmitting chip address failed\n", __func__);
398 /* If register address needs to be transmitted - do it now. */
399 if (addr && addr_len) { /* register addr */
400 while ((i < addr_len) && !result) {
401 writel(addr[i++], &i2c->iicds);
403 result = wait_for_xfer(i2c);
408 debug("%s: transmitting register address failed\n",
416 while ((i < data_len) && !result) {
417 writel(data[i++], &i2c->iicds);
419 result = wait_for_xfer(i2c);
423 if (addr && addr_len) {
425 * Register address has been sent, now send slave chip
426 * address again to start the actual read transaction.
428 writel(chip_addr, &i2c->iicds);
430 /* Generate a re-START. */
431 writel(I2CSTAT_MRM | I2CSTAT_RXTXEN |
432 I2CSTAT_SS, &i2c->iicstat);
434 result = wait_for_xfer(i2c);
436 debug("%s: I2C_READ: sending chip addr. failed\n",
442 while ((i < data_len) && !result) {
443 /* disable ACK for final READ */
444 if (i == data_len - 1)
445 clrbits_le32(&i2c->iiccon, I2CCON_ACKGEN);
448 result = wait_for_xfer(i2c);
449 data[i++] = readb(&i2c->iicds);
452 if (result == -EREMOTEIO)
453 /* Not Acknowledged --> normal terminated read. */
455 else if (result == -ETIMEDOUT)
456 debug("%s: I2C_READ: time out\n", __func__);
458 debug("%s: I2C_READ: read not terminated with NACK\n",
463 debug("%s: bad call\n", __func__);
472 static int nx_i2c_read(struct udevice *dev, uchar chip_addr, uint addr,
473 uint alen, uchar *buffer, uint len, uint seq)
475 struct nx_i2c_bus *i2c;
479 i2c = dev_get_priv(dev);
484 debug("I2C read: addr len %d not supported\n", alen);
485 return -EADDRNOTAVAIL;
489 xaddr[0] = (addr >> 24) & 0xFF;
492 xaddr[0] = (addr >> 24) & 0xFF;
493 xaddr[1] = (addr >> 16) & 0xFF;
494 xaddr[2] = (addr >> 8) & 0xFF;
495 xaddr[3] = addr & 0xFF;
498 ret = i2c_transfer(i2c->regs, I2C_READ, chip_addr << 1,
499 &xaddr[4 - alen], alen, buffer, len, seq);
502 debug("I2C read failed %d\n", ret);
509 static int nx_i2c_write(struct udevice *dev, uchar chip_addr, uint addr,
510 uint alen, uchar *buffer, uint len, uint seq)
512 struct nx_i2c_bus *i2c;
516 i2c = dev_get_priv(dev);
521 debug("I2C write: addr len %d not supported\n", alen);
526 xaddr[0] = (addr >> 24) & 0xFF;
527 xaddr[1] = (addr >> 16) & 0xFF;
528 xaddr[2] = (addr >> 8) & 0xFF;
529 xaddr[3] = addr & 0xFF;
532 ret = i2c_transfer(i2c->regs, I2C_WRITE, chip_addr << 1,
533 &xaddr[4 - alen], alen, buffer, len, seq);
535 debug("I2C write failed %d\n", ret);
542 static int nx_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
544 struct nx_i2c_bus *bus = dev_get_priv(dev);
545 struct nx_i2c_regs *i2c = bus->regs;
549 /* The power loss by the clock, only during on/off. */
550 ret = i2c_set_clk(bus, 1);
553 /* Bus State(Busy) check */
554 ret = i2c_is_busy(i2c);
556 for (i = 0; i < nmsgs; msg++, i++) {
557 if (msg->flags & I2C_M_RD) {
558 ret = nx_i2c_read(dev, msg->addr, 0, 0,
559 msg->buf, msg->len, i);
561 ret = nx_i2c_write(dev, msg->addr, 0, 0,
562 msg->buf, msg->len, i);
566 debug("i2c_xfer: error sending\n");
572 if (i2c_set_clk(bus, 0))
579 static int nx_i2c_probe_chip(struct udevice *dev, u32 chip_addr,
583 struct nx_i2c_bus *bus = dev_get_priv(dev);
585 ret = i2c_set_clk(bus, 1);
589 * Send Chip Address only
590 * --> I2C transfer with data length and address length = 0.
591 * If there is a Slave, i2c_transfer() returns 0 (acknowledge
593 * I2C_WRITE must be used in order Master Transmit Mode is
594 * selected. Otherwise (in Master Receive Mode, I2C_READ)
595 * sending the stop condition below is not working (SDA does
596 * not transit to High).
598 ret = i2c_transfer(bus->regs, I2C_WRITE, (uchar)chip_addr << 1,
599 NULL, 0, NULL, 0, 0);
602 if (i2c_set_clk(bus, 0))
609 static const struct dm_i2c_ops nx_i2c_ops = {
611 .probe_chip = nx_i2c_probe_chip,
612 .set_bus_speed = nx_i2c_set_bus_speed,
615 static const struct udevice_id nx_i2c_ids[] = {
616 { .compatible = "nexell,s5pxx18-i2c" },
620 U_BOOT_DRIVER(i2c_nexell) = {
621 .name = "i2c_nexell",
623 .of_match = nx_i2c_ids,
624 .probe = nx_i2c_probe,
625 .priv_auto = sizeof(struct nx_i2c_bus),