1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2021 Nuvoton Technology Corp.
10 #include <linux/iopoll.h>
11 #include <asm/arch/gcr.h>
13 #define I2C_FREQ_100K 100000
14 #define NPCM_I2C_TIMEOUT_MS 10
15 #define NPCM7XX_I2CSEGCTL_INIT_VAL 0x0333F000
16 #define NPCM8XX_I2CSEGCTL_INIT_VAL 0x9333F000
18 /* SCLFRQ min/max field values */
20 #define SCLFRQ_MAX 511
23 #define SMBCTL1_START BIT(0)
24 #define SMBCTL1_STOP BIT(1)
25 #define SMBCTL1_INTEN BIT(2)
26 #define SMBCTL1_ACK BIT(4)
27 #define SMBCTL1_STASTRE BIT(7)
30 #define SMBCTL2_ENABLE BIT(0)
33 #define SMBCTL3_SCL_LVL BIT(7)
34 #define SMBCTL3_SDA_LVL BIT(6)
37 #define SMBCST_BB BIT(1)
38 #define SMBCST_TGSCL BIT(5)
41 #define SMBST_XMIT BIT(0)
42 #define SMBST_MASTER BIT(1)
43 #define SMBST_STASTR BIT(3)
44 #define SMBST_NEGACK BIT(4)
45 #define SMBST_BER BIT(5)
46 #define SMBST_SDAST BIT(6)
48 /* SMBCST3 in bank0 */
49 #define SMBCST3_EO_BUSY BIT(7)
51 /* SMBFIF_CTS in bank1 */
52 #define SMBFIF_CTS_CLR_FIFO BIT(6)
54 #define SMBFIF_CTL_FIFO_EN BIT(4)
55 #define SMBCTL3_BNK_SEL BIT(5)
63 struct smb_bank0_regs {
79 struct smb_bank1_regs {
91 struct npcm_i2c_regs {
101 struct smb_bank0_regs bank0;
102 struct smb_bank1_regs bank1;
107 struct npcm_i2c_bus {
108 struct npcm_i2c_regs *reg;
115 static void npcm_dump_regs(struct npcm_i2c_bus *bus)
117 struct npcm_i2c_regs *reg = bus->reg;
120 printf("SMBST=0x%x\n", readb(®->st));
121 printf("SMBCST=0x%x\n", readb(®->cst));
122 printf("SMBCTL1=0x%x\n", readb(®->ctl1));
126 static int npcm_i2c_check_sda(struct npcm_i2c_bus *bus)
128 struct npcm_i2c_regs *reg = bus->reg;
130 int err = I2C_ERR_TIMEOUT;
133 start_time = get_timer(0);
134 /* wait SDAST to be 1 */
135 while (get_timer(start_time) < NPCM_I2C_TIMEOUT_MS) {
136 val = readb(®->st);
137 if (val & SMBST_NEGACK) {
141 if (val & SMBST_BER) {
145 if (val & SMBST_SDAST) {
152 printf("%s: err %d\n", __func__, err);
157 static int npcm_i2c_send_start(struct npcm_i2c_bus *bus)
159 struct npcm_i2c_regs *reg = bus->reg;
161 int err = I2C_ERR_TIMEOUT;
163 /* Generate START condition */
164 setbits_8(®->ctl1, SMBCTL1_START);
166 start_time = get_timer(0);
167 while (get_timer(start_time) < NPCM_I2C_TIMEOUT_MS) {
168 if (readb(®->st) & SMBST_BER)
170 if (readb(®->st) & SMBST_MASTER) {
180 static int npcm_i2c_send_stop(struct npcm_i2c_bus *bus, bool wait)
182 struct npcm_i2c_regs *reg = bus->reg;
184 int err = I2C_ERR_TIMEOUT;
186 setbits_8(®->ctl1, SMBCTL1_STOP);
188 /* Clear NEGACK, STASTR and BER bits */
189 writeb(SMBST_STASTR | SMBST_NEGACK | SMBST_BER, ®->st);
191 bus->started = false;
196 start_time = get_timer(0);
197 while (get_timer(start_time) < NPCM_I2C_TIMEOUT_MS) {
198 if ((readb(®->ctl1) & SMBCTL1_STOP) == 0) {
204 printf("%s: err %d\n", __func__, err);
211 static void npcm_i2c_reset(struct npcm_i2c_bus *bus)
213 struct npcm_i2c_regs *reg = bus->reg;
215 debug("%s: module %d\n", __func__, bus->num);
216 /* disable & enable SMB moudle */
217 clrbits_8(®->ctl2, SMBCTL2_ENABLE);
218 setbits_8(®->ctl2, SMBCTL2_ENABLE);
220 /* clear BB and status */
221 writeb(SMBCST_BB, ®->cst);
222 writeb(0xff, ®->st);
225 setbits_8(®->ctl3, SMBCTL3_BNK_SEL);
226 /* Clear all fifo bits */
227 writeb(SMBFIF_CTS_CLR_FIFO, ®->bank1.fif_cts);
230 clrbits_8(®->ctl3, SMBCTL3_BNK_SEL);
232 writeb(SMBCST3_EO_BUSY, ®->bank0.cst3);
233 /* single byte mode */
234 clrbits_8(®->bank0.fif_ctl, SMBFIF_CTL_FIFO_EN);
237 writeb(0, ®->ctl1);
240 static void npcm_i2c_recovery(struct npcm_i2c_bus *bus, u32 addr)
244 struct npcm_i2c_regs *reg = bus->reg;
247 val = readb(®->ctl3);
248 /* Skip recovery, bus not stucked */
249 if ((val & SMBCTL3_SCL_LVL) && (val & SMBCTL3_SDA_LVL))
252 printf("Performing I2C bus %d recovery...\n", bus->num);
253 /* SCL/SDA are not releaed, perform recovery */
255 /* toggle SCL line */
256 writeb(SMBCST_TGSCL, ®->cst);
259 val = readb(®->ctl3);
260 if (val & SMBCTL3_SDA_LVL)
266 if (val & SMBCTL3_SDA_LVL) {
267 writeb((u8)((addr << 1) & 0xff), ®->sda);
268 err = npcm_i2c_send_start(bus);
271 npcm_i2c_send_stop(bus, false);
273 printf("I2C bus %d recovery completed\n",
276 printf("%s: send START err %d\n", __func__, err);
279 printf("Fail to recover I2C bus %d\n", bus->num);
284 static int npcm_i2c_send_address(struct npcm_i2c_bus *bus, u8 addr,
287 struct npcm_i2c_regs *reg = bus->reg;
291 /* Stall After Start Enable */
293 setbits_8(®->ctl1, SMBCTL1_STASTRE);
295 writeb(addr, ®->sda);
297 start_time = get_timer(0);
298 while (get_timer(start_time) < NPCM_I2C_TIMEOUT_MS) {
299 if (readb(®->st) & SMBST_STASTR)
302 if (readb(®->st) & SMBST_BER) {
303 clrbits_8(®->ctl1, SMBCTL1_STASTRE);
310 val = readb(®->st);
311 if (val & SMBST_NEGACK) {
312 debug("NACK on addr 0x%x\n", addr >> 1);
313 /* After a Stop condition, writing 1 to NEGACK clears it */
322 static int npcm_i2c_read_bytes(struct npcm_i2c_bus *bus, u8 *data, int len)
324 struct npcm_i2c_regs *reg = bus->reg;
330 /* bus should be stalled before receiving last byte */
331 setbits_8(®->ctl1, SMBCTL1_ACK);
333 /* clear STASTRE if it is set */
334 if (readb(®->ctl1) & SMBCTL1_STASTRE) {
335 writeb(SMBST_STASTR, ®->st);
336 clrbits_8(®->ctl1, SMBCTL1_STASTRE);
338 npcm_i2c_check_sda(bus);
339 npcm_i2c_send_stop(bus, false);
340 *data = readb(®->sda);
341 /* this must be done to generate STOP condition */
342 writeb(SMBST_NEGACK, ®->st);
344 for (i = 0; i < len; i++) {
346 * When NEGACK bit is set to 1 after the transmission of a byte,
347 * SDAST is not set to 1.
349 if (i != (len - 1)) {
350 err = npcm_i2c_check_sda(bus);
352 err = readb_poll_timeout(®->ctl1, val,
353 !(val & SMBCTL1_ACK), 100000);
355 printf("wait nack timeout\n");
356 err = I2C_ERR_TIMEOUT;
360 if (err && err != I2C_ERR_TIMEOUT)
362 if (i == (len - 2)) {
363 /* set NACK before last byte */
364 setbits_8(®->ctl1, SMBCTL1_ACK);
366 if (i == (len - 1)) {
367 /* last byte, send STOP condition */
368 npcm_i2c_send_stop(bus, false);
369 *data = readb(®->sda);
370 writeb(SMBST_NEGACK, ®->st);
373 *data = readb(®->sda);
381 static int npcm_i2c_send_bytes(struct npcm_i2c_bus *bus, u8 *data, int len)
383 struct npcm_i2c_regs *reg = bus->reg;
388 val = readb(®->st);
389 if (val & SMBST_NEGACK)
391 else if (val & SMBST_BER)
394 /* clear STASTRE if it is set */
395 if (readb(®->ctl1) & SMBCTL1_STASTRE)
396 clrbits_8(®->ctl1, SMBCTL1_STASTRE);
398 for (i = 0; i < len; i++) {
399 err = npcm_i2c_check_sda(bus);
402 writeb(*data, ®->sda);
405 npcm_i2c_check_sda(bus);
410 static int npcm_i2c_read(struct npcm_i2c_bus *bus, u32 addr, u8 *data,
413 struct npcm_i2c_regs *reg = bus->reg;
420 /* send START condition */
421 err = npcm_i2c_send_start(bus);
423 debug("%s: send START err %d\n", __func__, err);
427 stall = (len == 1) ? true : false;
428 /* send address byte */
429 err = npcm_i2c_send_address(bus, (u8)(addr << 1) | 0x1, stall);
432 npcm_i2c_read_bytes(bus, data, len);
434 if (err == I2C_ERR_NACK) {
436 writeb(SMBST_NEGACK, ®->st);
440 debug("%s: err %d\n", __func__, err);
445 static int npcm_i2c_write(struct npcm_i2c_bus *bus, u32 addr, u8 *data,
448 struct npcm_i2c_regs *reg = bus->reg;
452 /* send START condition */
453 err = npcm_i2c_send_start(bus);
455 debug("%s: send START err %d\n", __func__, err);
459 stall = (len == 0) ? true : false;
460 /* send address byte */
461 err = npcm_i2c_send_address(bus, (u8)(addr << 1), stall);
464 err = npcm_i2c_send_bytes(bus, data, len);
466 /* clear STASTRE if it is set */
468 clrbits_8(®->ctl1, SMBCTL1_STASTRE);
471 debug("%s: err %d\n", __func__, err);
476 static int npcm_i2c_xfer(struct udevice *dev,
477 struct i2c_msg *msg, int nmsgs)
479 struct npcm_i2c_bus *bus = dev_get_priv(dev);
480 struct npcm_i2c_regs *reg = bus->reg;
481 int ret = 0, err = 0;
483 if (nmsgs < 1 || nmsgs > 2) {
484 printf("%s: commands not support\n", __func__);
487 /* clear ST register */
488 writeb(0xFF, ®->st);
490 for ( ; nmsgs > 0; nmsgs--, msg++) {
491 if (msg->flags & I2C_M_RD)
492 err = npcm_i2c_read(bus, msg->addr, msg->buf,
495 err = npcm_i2c_write(bus, msg->addr, msg->buf,
498 debug("i2c_xfer: error %d\n", err);
505 npcm_i2c_send_stop(bus, true);
508 npcm_i2c_recovery(bus, msg->addr);
513 static int npcm_i2c_init_clk(struct npcm_i2c_bus *bus, u32 bus_freq)
515 struct npcm_i2c_regs *reg = bus->reg;
516 u32 freq = bus->apb_clk;
520 /* SCLFRQ = T(SCL)/4/T(CLK) = FREQ(CLK)/4/FREQ(SCL) */
521 sclfrq = freq / (bus_freq * 4);
522 if (sclfrq < SCLFRQ_MIN || sclfrq > SCLFRQ_MAX)
525 if (freq >= 40000000)
527 else if (freq >= 12500000)
532 val = readb(®->ctl2) & 0x1;
533 val |= (sclfrq & 0x7F) << 1;
534 writeb(val, ®->ctl2);
536 /* clear 400K_MODE bit */
537 val = readb(®->ctl3) & 0xc;
538 val |= (sclfrq >> 7) & 0x3;
539 writeb(val, ®->ctl3);
541 writeb(hldt, ®->bank0.ctl4);
546 static int npcm_i2c_set_bus_speed(struct udevice *dev,
549 struct npcm_i2c_bus *bus = dev_get_priv(dev);
551 return npcm_i2c_init_clk(bus, speed);
554 static int npcm_i2c_probe(struct udevice *dev)
556 struct npcm_i2c_bus *bus = dev_get_priv(dev);
557 struct npcm_gcr *gcr = (struct npcm_gcr *)NPCM_GCR_BA;
558 struct npcm_i2c_regs *reg;
559 u32 i2csegctl_val = dev_get_driver_data(dev);
563 ret = clk_get_by_index(dev, 0, &clk);
565 printf("%s: ret %d\n", __func__, ret);
568 bus->apb_clk = clk_get_rate(&clk);
569 if (bus->apb_clk <= 0) {
570 printf("%s: fail to get rate\n", __func__);
574 bus->num = dev->seq_;
575 bus->reg = dev_read_addr_ptr(dev);
576 bus->freq = dev_read_u32_default(dev, "clock-frequency", 100000);
577 bus->started = false;
580 if (npcm_i2c_init_clk(bus, bus->freq)) {
581 printf("%s: init_clk failed\n", __func__);
585 /* set initial i2csegctl value */
586 writel(i2csegctl_val, &gcr->i2csegctl);
588 /* enable SMB module */
589 setbits_8(®->ctl2, SMBCTL2_ENABLE);
591 /* select register bank 0 */
592 clrbits_8(®->ctl3, SMBCTL3_BNK_SEL);
594 /* single byte mode */
595 clrbits_8(®->bank0.fif_ctl, SMBFIF_CTL_FIFO_EN);
598 writeb(0, ®->ctl1);
600 printf("I2C bus %d ready. speed=%d, base=0x%x, apb=%u\n",
601 bus->num, bus->freq, (u32)(uintptr_t)bus->reg, bus->apb_clk);
606 static const struct dm_i2c_ops nuvoton_i2c_ops = {
607 .xfer = npcm_i2c_xfer,
608 .set_bus_speed = npcm_i2c_set_bus_speed,
611 static const struct udevice_id nuvoton_i2c_of_match[] = {
612 { .compatible = "nuvoton,npcm845-i2c", .data = NPCM8XX_I2CSEGCTL_INIT_VAL},
613 { .compatible = "nuvoton,npcm750-i2c", .data = NPCM7XX_I2CSEGCTL_INIT_VAL},
617 U_BOOT_DRIVER(npcm_i2c_bus) = {
620 .of_match = nuvoton_i2c_of_match,
621 .probe = npcm_i2c_probe,
622 .priv_auto = sizeof(struct npcm_i2c_bus),
623 .ops = &nuvoton_i2c_ops,