1 // SPDX-License-Identifier: GPL-2.0+
3 * RZ/G2L I2C (RIIC) driver
5 * Copyright (C) 2021-2023 Renesas Electronics Corp.
11 #include <dm/device_compat.h>
14 #include <linux/bitops.h>
15 #include <linux/delay.h>
19 #define RIIC_ICCR1 0x00
20 #define RIIC_ICCR2 0x04
21 #define RIIC_ICMR1 0x08
22 #define RIIC_ICMR2 0x0c
23 #define RIIC_ICMR3 0x10
24 #define RIIC_ICFER 0x14
25 #define RIIC_ICSER 0x18
26 #define RIIC_ICIER 0x1c
27 #define RIIC_ICSR1 0x20
28 #define RIIC_ICSR2 0x24
29 #define RIIC_ICSAR0 0x28
30 #define RIIC_ICBRL 0x34
31 #define RIIC_ICBRH 0x38
32 #define RIIC_ICDRT 0x3c
33 #define RIIC_ICDRR 0x40
36 #define ICCR1_ICE BIT(7)
37 #define ICCR1_IICRST BIT(6)
38 #define ICCR1_CLO BIT(5)
39 #define ICCR1_SOWP BIT(4)
40 #define ICCR1_SCLO BIT(3)
41 #define ICCR1_SDAO BIT(2)
42 #define ICCR1_SCLI BIT(1)
43 #define ICCR1_SDAI BIT(0)
46 #define ICCR2_BBSY BIT(7)
47 #define ICCR2_MST BIT(6)
48 #define ICCR2_TRS BIT(5)
49 #define ICCR2_SP BIT(3)
50 #define ICCR2_RS BIT(2)
51 #define ICCR2_ST BIT(1)
54 #define ICMR1_MTWP BIT(7)
55 #define ICMR1_CKS_MASK GENMASK(6, 4)
56 #define ICMR1_BCWP BIT(3)
57 #define ICMR1_BC_MASK GENMASK(2, 0)
59 #define ICMR1_CKS(x) (((x) << 4) & ICMR1_CKS_MASK)
60 #define ICMR1_BC(x) ((x) & ICMR1_BC_MASK)
63 #define ICMR2_DLCS BIT(7)
64 #define ICMR2_SDDL_MASK GENMASK(6, 4)
65 #define ICMR2_TMOH BIT(2)
66 #define ICMR2_TMOL BIT(1)
67 #define ICMR2_TMOS BIT(0)
70 #define ICMR3_SMBS BIT(7)
71 #define ICMR3_WAIT BIT(6)
72 #define ICMR3_RDRFS BIT(5)
73 #define ICMR3_ACKWP BIT(4)
74 #define ICMR3_ACKBT BIT(3)
75 #define ICMR3_ACKBR BIT(2)
76 #define ICMR3_NF_MASK GENMASK(1, 0)
79 #define ICFER_FMPE BIT(7)
80 #define ICFER_SCLE BIT(6)
81 #define ICFER_NFE BIT(5)
82 #define ICFER_NACKE BIT(4)
83 #define ICFER_SALE BIT(3)
84 #define ICFER_NALE BIT(2)
85 #define ICFER_MALE BIT(1)
86 #define ICFER_TMOE BIT(0)
89 #define ICSER_HOAE BIT(7)
90 #define ICSER_DIDE BIT(5)
91 #define ICSER_GCAE BIT(3)
92 #define ICSER_SAR2E BIT(2)
93 #define ICSER_SAR1E BIT(1)
94 #define ICSER_SAR0E BIT(0)
97 #define ICIER_TIE BIT(7)
98 #define ICIER_TEIE BIT(6)
99 #define ICIER_RIE BIT(5)
100 #define ICIER_NAKIE BIT(4)
101 #define ICIER_SPIE BIT(3)
102 #define ICIER_STIE BIT(2)
103 #define ICIER_ALIE BIT(1)
104 #define ICIER_TMOIE BIT(0)
107 #define ICSR1_HOA BIT(7)
108 #define ICSR1_DID BIT(5)
109 #define ICSR1_GCA BIT(3)
110 #define ICSR1_AAS2 BIT(2)
111 #define ICSR1_AAS1 BIT(1)
112 #define ICSR1_AAS0 BIT(0)
115 #define ICSR2_TDRE BIT(7)
116 #define ICSR2_TEND BIT(6)
117 #define ICSR2_RDRF BIT(5)
118 #define ICSR2_NACKF BIT(4)
119 #define ICSR2_STOP BIT(3)
120 #define ICSR2_START BIT(2)
121 #define ICSR2_AL BIT(1)
122 #define ICSR2_TMOF BIT(0)
125 #define ICBRH_RESERVED GENMASK(7, 5) /* The write value should always be 1 */
126 #define ICBRH_BRH_MASK GENMASK(4, 0)
129 #define ICBRL_RESERVED GENMASK(7, 5) /* The write value should always be 1 */
130 #define ICBRL_BRL_MASK GENMASK(4, 0)
132 #define RIIC_TIMEOUT_MSEC 100
134 #define RIIC_FLAG_DEFAULT_SCL_RISE_TIME BIT(0)
135 #define RIIC_FLAG_DEFAULT_SCL_FALL_TIME BIT(1)
138 * If SDA is stuck in a low state, the I2C spec says up to 9 clock cycles on SCL
139 * may be needed to unblock whichever other device on the bus is holding SDA low.
141 #define I2C_DEBLOCK_MAX_CYCLES 9
152 static int riic_check_busy(struct udevice *dev)
154 struct riic_priv *priv = dev_get_priv(dev);
157 ret = wait_for_bit_8(priv->base + RIIC_ICCR2, ICCR2_BBSY, 0,
158 RIIC_TIMEOUT_MSEC, 0);
159 if (ret == -ETIMEDOUT) {
160 dev_dbg(dev, "bus is busy!\n");
167 static int riic_wait_for_icsr2(struct udevice *dev, u8 bit)
169 struct riic_priv *priv = dev_get_priv(dev);
170 ulong start = get_timer(0);
173 /* We can't use wait_for_bit_8() here as we need to check for NACK. */
174 while (!((icsr2 = readb(priv->base + RIIC_ICSR2)) & bit)) {
175 if (icsr2 & ICSR2_NACKF)
177 if (get_timer(start) > RIIC_TIMEOUT_MSEC) {
178 dev_dbg(dev, "timeout! (bit=%x, icsr2=%x, iccr2=%x)\n",
179 bit, icsr2, readb(priv->base + RIIC_ICCR2));
189 static int riic_check_nack_receive(struct udevice *dev)
191 struct riic_priv *priv = dev_get_priv(dev);
193 if (readb(priv->base + RIIC_ICSR2) & ICSR2_NACKF) {
194 dev_dbg(dev, "received nack!\n");
196 clrbits_8(priv->base + RIIC_ICSR2, ICSR2_NACKF);
197 setbits_8(priv->base + RIIC_ICCR2, ICCR2_SP);
198 readb(priv->base + RIIC_ICDRR); /* dummy read */
204 static int riic_i2c_raw_write(struct udevice *dev, u8 *buf, size_t len)
206 struct riic_priv *priv = dev_get_priv(dev);
210 for (i = 0; i < len; i++) {
211 ret = riic_check_nack_receive(dev);
215 ret = riic_wait_for_icsr2(dev, ICSR2_TDRE);
219 writeb(buf[i], priv->base + RIIC_ICDRT);
222 return riic_check_nack_receive(dev);
225 static int riic_send_start_cond(struct udevice *dev, int restart)
227 struct riic_priv *priv = dev_get_priv(dev);
231 setbits_8(priv->base + RIIC_ICCR2, ICCR2_RS);
233 setbits_8(priv->base + RIIC_ICCR2, ICCR2_ST);
235 ret = riic_wait_for_icsr2(dev, ICSR2_START);
238 clrbits_8(priv->base + RIIC_ICSR2, ICSR2_START);
243 static int riic_receive_data(struct udevice *dev, struct i2c_msg *msg)
245 struct riic_priv *priv = dev_get_priv(dev);
246 int ret, stop_ret, i;
248 ret = riic_wait_for_icsr2(dev, ICSR2_RDRF);
252 ret = riic_check_nack_receive(dev);
256 setbits_8(priv->base + RIIC_ICMR3, ICMR3_WAIT | ICMR3_ACKWP | ICMR3_RDRFS);
258 /* A dummy read must be performed to trigger data reception */
259 readb(priv->base + RIIC_ICDRR);
261 for (i = 0; i < msg->len; i++) {
262 ret = riic_wait_for_icsr2(dev, ICSR2_RDRF);
266 if (i == (msg->len - 1)) {
267 clrbits_8(priv->base + RIIC_ICSR2, ICSR2_STOP);
268 setbits_8(priv->base + RIIC_ICCR2, ICCR2_SP);
269 setbits_8(priv->base + RIIC_ICMR3, ICMR3_ACKBT);
271 clrbits_8(priv->base + RIIC_ICMR3, ICMR3_ACKBT);
274 msg->buf[i] = readb(priv->base + RIIC_ICDRR);
280 * We got here due to an error condition, so we need to perform
281 * a dummy read to issue the stop bit.
283 clrbits_8(priv->base + RIIC_ICSR2, ICSR2_STOP);
284 setbits_8(priv->base + RIIC_ICCR2, ICCR2_SP);
285 readb(priv->base + RIIC_ICDRR);
287 stop_ret = riic_wait_for_icsr2(dev, ICSR2_STOP);
288 clrbits_8(priv->base + RIIC_ICSR2, ICSR2_STOP | ICSR2_NACKF);
289 clrbits_8(priv->base + RIIC_ICMR3, ICMR3_WAIT | ICMR3_ACKWP | ICMR3_RDRFS);
290 return ret ? ret : stop_ret;
293 static int riic_transmit_stop(struct udevice *dev)
295 struct riic_priv *priv = dev_get_priv(dev);
298 clrbits_8(priv->base + RIIC_ICSR2, ICSR2_STOP);
299 setbits_8(priv->base + RIIC_ICCR2, ICCR2_SP);
301 ret = riic_wait_for_icsr2(dev, ICSR2_STOP);
302 clrbits_8(priv->base + RIIC_ICSR2, ICSR2_STOP | ICSR2_NACKF);
306 static int riic_transmit_data(struct udevice *dev, struct i2c_msg *msg)
310 ret = riic_i2c_raw_write(dev, msg->buf, msg->len);
314 ret = riic_wait_for_icsr2(dev, ICSR2_TEND);
318 if (!ret && !(msg->flags & I2C_M_STOP))
322 stop_ret = riic_transmit_stop(dev);
323 return ret ? ret : stop_ret;
326 static int riic_xfer_one(struct udevice *dev, struct i2c_msg *msg, int first_msg)
328 u8 addr_byte = ((msg->addr << 1) | (msg->flags & I2C_M_RD));
331 if (!(msg->flags & I2C_M_NOSTART)) {
333 * Send a start for the first message and a restart for
334 * subsequent messages.
336 ret = riic_send_start_cond(dev, !first_msg);
341 ret = riic_i2c_raw_write(dev, &addr_byte, 1);
344 * We're aborting the transfer while still in master transmit
347 riic_transmit_stop(dev);
351 if (msg->flags & I2C_M_RD)
352 return riic_receive_data(dev, msg);
354 return riic_transmit_data(dev, msg);
357 static int riic_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
361 ret = riic_check_busy(dev);
365 /* Ensure that the last message is terminated with a stop bit. */
366 msg[nmsgs - 1].flags |= I2C_M_STOP;
368 for (i = 0; i < nmsgs; i++) {
369 ret = riic_xfer_one(dev, &msg[i], !i);
377 static int riic_deblock(struct udevice *dev)
379 struct riic_priv *priv = dev_get_priv(dev);
383 * Issue clock cycles on SCL to hopefully unblock whatever is holding
384 * SDA low. These clock cycles may trigger error conditions such as
385 * Arbitration Lost, so we clear the status bits in ICSR2 after each
388 while (!(readb(priv->base + RIIC_ICCR1) & ICCR1_SDAI)) {
389 if (i++ == I2C_DEBLOCK_MAX_CYCLES)
392 setbits_8(priv->base + RIIC_ICCR1, ICCR1_CLO);
393 if (wait_for_bit_8(priv->base + RIIC_ICCR1, ICCR1_CLO, 0,
394 RIIC_TIMEOUT_MSEC, false))
396 writeb(0, priv->base + RIIC_ICSR2);
400 * We have released SDA, but the I2C module is now out of sync
401 * with the bus state, so we need to reset its state machine.
403 setbits_8(priv->base + RIIC_ICCR1, ICCR1_IICRST);
404 clrbits_8(priv->base + RIIC_ICCR1, ICCR1_IICRST);
409 static int riic_set_bus_speed(struct udevice *dev, uint bus_speed)
411 struct riic_priv *priv = dev_get_priv(dev);
413 uint total_ticks, cks, brl, brh;
415 if (bus_speed > I2C_SPEED_FAST_PLUS_RATE) {
416 dev_err(dev, "unsupported bus speed (%dHz). %d max\n", bus_speed,
417 I2C_SPEED_FAST_PLUS_RATE);
422 * Assume the default register settings:
423 * FER.SCLE = 1 (SCL sync circuit enabled, adds 2 or 3 cycles)
424 * FER.NFE = 1 (noise circuit enabled)
425 * MR3.NF = 0 (1 cycle of noise filtered out)
427 * Freq (CKS=000) = (I2CCLK + tr + tf)/ (BRH + 3 + 1) + (BRL + 3 + 1)
428 * Freq (CKS!=000) = (I2CCLK + tr + tf)/ (BRH + 2 + 1) + (BRL + 2 + 1)
432 * Determine reference clock rate. We must be able to get the desired
433 * frequency with only 62 clock ticks max (31 high, 31 low).
434 * Aim for a duty of 60% LOW, 40% HIGH.
436 refclk = clk_get_rate(&priv->clk);
437 total_ticks = DIV_ROUND_UP(refclk, bus_speed ?: 1);
439 for (cks = 0; cks < 7; cks++) {
441 * 60% low time must be less than BRL + 2 + 1
442 * BRL max register value is 0x1F.
444 brl = ((total_ticks * 6) / 10);
445 if (brl <= (0x1f + 3))
452 if (brl > (0x1f + 3)) {
453 dev_err(dev, "invalid speed (%u). Too slow.\n", bus_speed);
457 brh = total_ticks - brl;
459 /* Remove automatic clock ticks for sync circuit and NF */
469 * If SCL rise and fall times weren't set in the device tree, set them
470 * based on the desired bus speed and the maximum timings given in the
473 if (priv->flags & RIIC_FLAG_DEFAULT_SCL_RISE_TIME)
474 priv->scl_rise_ns = bus_speed <= I2C_SPEED_STANDARD_RATE ? 1000 :
475 bus_speed <= I2C_SPEED_FAST_RATE ? 300 : 120;
476 if (priv->flags & RIIC_FLAG_DEFAULT_SCL_FALL_TIME)
477 priv->scl_fall_ns = bus_speed <= I2C_SPEED_FAST_RATE ? 300 : 120;
480 * Remove clock ticks for rise and fall times. Convert ns to clock
483 brl -= priv->scl_fall_ns / (1000000000 / refclk);
484 brh -= priv->scl_rise_ns / (1000000000 / refclk);
486 /* Adjust for min register values for when SCLE=1 and NFE=1 */
492 priv->bus_speed = refclk / total_ticks;
493 dev_dbg(dev, "freq=%u, duty=%d, fall=%lu, rise=%lu, cks=%d, brl=%d, brh=%d\n",
494 priv->bus_speed, ((brl + 3) * 100) / (brl + brh + 6),
495 priv->scl_fall_ns / (1000000000 / refclk),
496 priv->scl_rise_ns / (1000000000 / refclk), cks, brl, brh);
498 setbits_8(priv->base + RIIC_ICCR1, ICCR1_IICRST);
499 writeb(ICMR1_CKS(cks), priv->base + RIIC_ICMR1);
500 writeb(brh | ICBRH_RESERVED, priv->base + RIIC_ICBRH);
501 writeb(brl | ICBRL_RESERVED, priv->base + RIIC_ICBRL);
502 clrbits_8(priv->base + RIIC_ICCR1, ICCR1_IICRST);
507 static int riic_get_bus_speed(struct udevice *dev)
509 struct riic_priv *priv = dev_get_priv(dev);
511 return priv->bus_speed;
514 static const struct dm_i2c_ops riic_ops = {
516 .deblock = riic_deblock,
517 .set_bus_speed = riic_set_bus_speed,
518 .get_bus_speed = riic_get_bus_speed,
521 static int riic_init_setting(struct udevice *dev)
523 struct riic_priv *priv = dev_get_priv(dev);
526 clrbits_8(priv->base + RIIC_ICCR1, ICCR1_ICE);
527 setbits_8(priv->base + RIIC_ICCR1, ICCR1_IICRST);
528 setbits_8(priv->base + RIIC_ICCR1, ICCR1_ICE);
531 * Set a default bitrate. The rate may be overridden based on the device
532 * tree as part of i2c_post_probe().
534 ret = riic_set_bus_speed(dev, I2C_SPEED_STANDARD_RATE);
538 clrbits_8(priv->base + RIIC_ICCR1, ICCR1_IICRST);
540 /* Make sure the bus is not stuck. */
541 if (!(readb(priv->base + RIIC_ICCR1) & ICCR1_SDAI)) {
542 dev_dbg(dev, "clearing SDA low state\n");
543 ret = riic_deblock(dev);
545 dev_err(dev, "failed to clear SDA low state!\n");
552 clrbits_8(priv->base + RIIC_ICCR1, ICCR1_ICE | ICCR1_IICRST);
556 static int riic_probe(struct udevice *dev)
558 struct riic_priv *priv = dev_get_priv(dev);
559 struct reset_ctl rst;
562 priv->base = dev_read_addr_ptr(dev);
564 ret = dev_read_u32(dev, "i2c-scl-rising-time-ns", &priv->scl_rise_ns);
566 priv->flags |= RIIC_FLAG_DEFAULT_SCL_RISE_TIME;
567 ret = dev_read_u32(dev, "i2c-scl-falling-time-ns", &priv->scl_fall_ns);
569 priv->flags |= RIIC_FLAG_DEFAULT_SCL_FALL_TIME;
571 ret = clk_get_by_index(dev, 0, &priv->clk);
573 dev_err(dev, "failed to get clock\n");
577 ret = clk_enable(&priv->clk);
579 dev_err(dev, "failed to enable clock\n");
583 ret = reset_get_by_index(dev, 0, &rst);
585 dev_err(dev, "failed to get reset line\n");
589 ret = reset_deassert(&rst);
591 dev_err(dev, "failed to de-assert reset line\n");
595 ret = riic_init_setting(dev);
597 dev_err(dev, "failed to init i2c bus interface\n");
608 clk_disable(&priv->clk);
612 static const struct udevice_id riic_ids[] = {
613 { .compatible = "renesas,riic-rz", },
617 U_BOOT_DRIVER(riic_i2c) = {
620 .of_match = riic_ids,
622 .priv_auto = sizeof(struct riic_priv),