blob: f292c8243621e2b997869129d42dfa3ef7f9332a [file] [log] [blame]
Paul Barker7fe0e0a2024-02-27 20:40:30 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * RZ/G2L I2C (RIIC) driver
4 *
5 * Copyright (C) 2021-2023 Renesas Electronics Corp.
6 */
7
8#include <asm/io.h>
9#include <clk.h>
10#include <dm.h>
11#include <dm/device_compat.h>
12#include <errno.h>
13#include <i2c.h>
14#include <linux/bitops.h>
15#include <linux/delay.h>
16#include <reset.h>
Rasmus Villemoes6ec38902024-10-03 23:27:59 +020017#include <u-boot/schedule.h>
Paul Barker7fe0e0a2024-02-27 20:40:30 +000018#include <wait_bit.h>
19
20#define RIIC_ICCR1 0x00
21#define RIIC_ICCR2 0x04
22#define RIIC_ICMR1 0x08
23#define RIIC_ICMR2 0x0c
24#define RIIC_ICMR3 0x10
25#define RIIC_ICFER 0x14
26#define RIIC_ICSER 0x18
27#define RIIC_ICIER 0x1c
28#define RIIC_ICSR1 0x20
29#define RIIC_ICSR2 0x24
30#define RIIC_ICSAR0 0x28
31#define RIIC_ICBRL 0x34
32#define RIIC_ICBRH 0x38
33#define RIIC_ICDRT 0x3c
34#define RIIC_ICDRR 0x40
35
36/* ICCR1 */
37#define ICCR1_ICE BIT(7)
38#define ICCR1_IICRST BIT(6)
39#define ICCR1_CLO BIT(5)
40#define ICCR1_SOWP BIT(4)
41#define ICCR1_SCLO BIT(3)
42#define ICCR1_SDAO BIT(2)
43#define ICCR1_SCLI BIT(1)
44#define ICCR1_SDAI BIT(0)
45
46/* ICCR2 */
47#define ICCR2_BBSY BIT(7)
48#define ICCR2_MST BIT(6)
49#define ICCR2_TRS BIT(5)
50#define ICCR2_SP BIT(3)
51#define ICCR2_RS BIT(2)
52#define ICCR2_ST BIT(1)
53
54/* ICMR1 */
55#define ICMR1_MTWP BIT(7)
56#define ICMR1_CKS_MASK GENMASK(6, 4)
57#define ICMR1_BCWP BIT(3)
58#define ICMR1_BC_MASK GENMASK(2, 0)
59
60#define ICMR1_CKS(x) (((x) << 4) & ICMR1_CKS_MASK)
61#define ICMR1_BC(x) ((x) & ICMR1_BC_MASK)
62
63/* ICMR2 */
64#define ICMR2_DLCS BIT(7)
65#define ICMR2_SDDL_MASK GENMASK(6, 4)
66#define ICMR2_TMOH BIT(2)
67#define ICMR2_TMOL BIT(1)
68#define ICMR2_TMOS BIT(0)
69
70/* ICMR3 */
71#define ICMR3_SMBS BIT(7)
72#define ICMR3_WAIT BIT(6)
73#define ICMR3_RDRFS BIT(5)
74#define ICMR3_ACKWP BIT(4)
75#define ICMR3_ACKBT BIT(3)
76#define ICMR3_ACKBR BIT(2)
77#define ICMR3_NF_MASK GENMASK(1, 0)
78
79/* ICFER */
80#define ICFER_FMPE BIT(7)
81#define ICFER_SCLE BIT(6)
82#define ICFER_NFE BIT(5)
83#define ICFER_NACKE BIT(4)
84#define ICFER_SALE BIT(3)
85#define ICFER_NALE BIT(2)
86#define ICFER_MALE BIT(1)
87#define ICFER_TMOE BIT(0)
88
89/* ICSER */
90#define ICSER_HOAE BIT(7)
91#define ICSER_DIDE BIT(5)
92#define ICSER_GCAE BIT(3)
93#define ICSER_SAR2E BIT(2)
94#define ICSER_SAR1E BIT(1)
95#define ICSER_SAR0E BIT(0)
96
97/* ICIER */
98#define ICIER_TIE BIT(7)
99#define ICIER_TEIE BIT(6)
100#define ICIER_RIE BIT(5)
101#define ICIER_NAKIE BIT(4)
102#define ICIER_SPIE BIT(3)
103#define ICIER_STIE BIT(2)
104#define ICIER_ALIE BIT(1)
105#define ICIER_TMOIE BIT(0)
106
107/* ICSR1 */
108#define ICSR1_HOA BIT(7)
109#define ICSR1_DID BIT(5)
110#define ICSR1_GCA BIT(3)
111#define ICSR1_AAS2 BIT(2)
112#define ICSR1_AAS1 BIT(1)
113#define ICSR1_AAS0 BIT(0)
114
115/* ICSR2 */
116#define ICSR2_TDRE BIT(7)
117#define ICSR2_TEND BIT(6)
118#define ICSR2_RDRF BIT(5)
119#define ICSR2_NACKF BIT(4)
120#define ICSR2_STOP BIT(3)
121#define ICSR2_START BIT(2)
122#define ICSR2_AL BIT(1)
123#define ICSR2_TMOF BIT(0)
124
125/* ICBRH */
126#define ICBRH_RESERVED GENMASK(7, 5) /* The write value should always be 1 */
127#define ICBRH_BRH_MASK GENMASK(4, 0)
128
129/* ICBRL */
130#define ICBRL_RESERVED GENMASK(7, 5) /* The write value should always be 1 */
131#define ICBRL_BRL_MASK GENMASK(4, 0)
132
133#define RIIC_TIMEOUT_MSEC 100
134
135#define RIIC_FLAG_DEFAULT_SCL_RISE_TIME BIT(0)
136#define RIIC_FLAG_DEFAULT_SCL_FALL_TIME BIT(1)
137
138/*
139 * If SDA is stuck in a low state, the I2C spec says up to 9 clock cycles on SCL
140 * may be needed to unblock whichever other device on the bus is holding SDA low.
141 */
142#define I2C_DEBLOCK_MAX_CYCLES 9
143
144struct riic_priv {
145 void __iomem *base;
146 struct clk clk;
147 uint bus_speed;
148 u32 scl_rise_ns;
149 u32 scl_fall_ns;
150 u32 flags;
151};
152
153static int riic_check_busy(struct udevice *dev)
154{
155 struct riic_priv *priv = dev_get_priv(dev);
156 int ret;
157
158 ret = wait_for_bit_8(priv->base + RIIC_ICCR2, ICCR2_BBSY, 0,
159 RIIC_TIMEOUT_MSEC, 0);
160 if (ret == -ETIMEDOUT) {
161 dev_dbg(dev, "bus is busy!\n");
162 return -EBUSY;
163 }
164
165 return ret;
166}
167
168static int riic_wait_for_icsr2(struct udevice *dev, u8 bit)
169{
170 struct riic_priv *priv = dev_get_priv(dev);
171 ulong start = get_timer(0);
172 u8 icsr2;
173
174 /* We can't use wait_for_bit_8() here as we need to check for NACK. */
175 while (!((icsr2 = readb(priv->base + RIIC_ICSR2)) & bit)) {
176 if (icsr2 & ICSR2_NACKF)
177 return -EIO;
178 if (get_timer(start) > RIIC_TIMEOUT_MSEC) {
179 dev_dbg(dev, "timeout! (bit=%x, icsr2=%x, iccr2=%x)\n",
180 bit, icsr2, readb(priv->base + RIIC_ICCR2));
181 return -ETIMEDOUT;
182 }
183 udelay(1);
184 schedule();
185 }
186
187 return 0;
188}
189
190static int riic_check_nack_receive(struct udevice *dev)
191{
192 struct riic_priv *priv = dev_get_priv(dev);
193
194 if (readb(priv->base + RIIC_ICSR2) & ICSR2_NACKF) {
195 dev_dbg(dev, "received nack!\n");
196 /* received NACK */
197 clrbits_8(priv->base + RIIC_ICSR2, ICSR2_NACKF);
198 setbits_8(priv->base + RIIC_ICCR2, ICCR2_SP);
199 readb(priv->base + RIIC_ICDRR); /* dummy read */
200 return -EIO;
201 }
202 return 0;
203}
204
205static int riic_i2c_raw_write(struct udevice *dev, u8 *buf, size_t len)
206{
207 struct riic_priv *priv = dev_get_priv(dev);
208 size_t i;
209 int ret;
210
211 for (i = 0; i < len; i++) {
212 ret = riic_check_nack_receive(dev);
213 if (ret < 0)
214 return ret;
215
216 ret = riic_wait_for_icsr2(dev, ICSR2_TDRE);
217 if (ret < 0)
218 return ret;
219
220 writeb(buf[i], priv->base + RIIC_ICDRT);
221 }
222
223 return riic_check_nack_receive(dev);
224}
225
226static int riic_send_start_cond(struct udevice *dev, int restart)
227{
228 struct riic_priv *priv = dev_get_priv(dev);
229 int ret;
230
231 if (restart)
232 setbits_8(priv->base + RIIC_ICCR2, ICCR2_RS);
233 else
234 setbits_8(priv->base + RIIC_ICCR2, ICCR2_ST);
235
236 ret = riic_wait_for_icsr2(dev, ICSR2_START);
237 if (ret < 0)
238 return ret;
239 clrbits_8(priv->base + RIIC_ICSR2, ICSR2_START);
240
241 return ret;
242}
243
244static int riic_receive_data(struct udevice *dev, struct i2c_msg *msg)
245{
246 struct riic_priv *priv = dev_get_priv(dev);
247 int ret, stop_ret, i;
248
249 ret = riic_wait_for_icsr2(dev, ICSR2_RDRF);
250 if (ret < 0)
251 goto send_stop;
252
253 ret = riic_check_nack_receive(dev);
254 if (ret < 0)
255 goto send_stop;
256
257 setbits_8(priv->base + RIIC_ICMR3, ICMR3_WAIT | ICMR3_ACKWP | ICMR3_RDRFS);
258
259 /* A dummy read must be performed to trigger data reception */
260 readb(priv->base + RIIC_ICDRR);
261
262 for (i = 0; i < msg->len; i++) {
263 ret = riic_wait_for_icsr2(dev, ICSR2_RDRF);
264 if (ret < 0)
265 goto send_stop;
266
267 if (i == (msg->len - 1)) {
268 clrbits_8(priv->base + RIIC_ICSR2, ICSR2_STOP);
269 setbits_8(priv->base + RIIC_ICCR2, ICCR2_SP);
270 setbits_8(priv->base + RIIC_ICMR3, ICMR3_ACKBT);
271 } else {
272 clrbits_8(priv->base + RIIC_ICMR3, ICMR3_ACKBT);
273 }
274
275 msg->buf[i] = readb(priv->base + RIIC_ICDRR);
276 };
277
278send_stop:
279 if (ret) {
280 /*
281 * We got here due to an error condition, so we need to perform
282 * a dummy read to issue the stop bit.
283 */
284 clrbits_8(priv->base + RIIC_ICSR2, ICSR2_STOP);
285 setbits_8(priv->base + RIIC_ICCR2, ICCR2_SP);
286 readb(priv->base + RIIC_ICDRR);
287 }
288 stop_ret = riic_wait_for_icsr2(dev, ICSR2_STOP);
289 clrbits_8(priv->base + RIIC_ICSR2, ICSR2_STOP | ICSR2_NACKF);
290 clrbits_8(priv->base + RIIC_ICMR3, ICMR3_WAIT | ICMR3_ACKWP | ICMR3_RDRFS);
291 return ret ? ret : stop_ret;
292}
293
294static int riic_transmit_stop(struct udevice *dev)
295{
296 struct riic_priv *priv = dev_get_priv(dev);
297 int ret;
298
299 clrbits_8(priv->base + RIIC_ICSR2, ICSR2_STOP);
300 setbits_8(priv->base + RIIC_ICCR2, ICCR2_SP);
301
302 ret = riic_wait_for_icsr2(dev, ICSR2_STOP);
303 clrbits_8(priv->base + RIIC_ICSR2, ICSR2_STOP | ICSR2_NACKF);
304 return ret;
305}
306
307static int riic_transmit_data(struct udevice *dev, struct i2c_msg *msg)
308{
309 int ret, stop_ret;
310
311 ret = riic_i2c_raw_write(dev, msg->buf, msg->len);
312 if (ret < 0)
313 goto send_stop;
314
315 ret = riic_wait_for_icsr2(dev, ICSR2_TEND);
316 if (ret < 0)
317 goto send_stop;
318
319 if (!ret && !(msg->flags & I2C_M_STOP))
320 return 0;
321
322send_stop:
323 stop_ret = riic_transmit_stop(dev);
324 return ret ? ret : stop_ret;
325}
326
327static int riic_xfer_one(struct udevice *dev, struct i2c_msg *msg, int first_msg)
328{
329 u8 addr_byte = ((msg->addr << 1) | (msg->flags & I2C_M_RD));
330 int ret;
331
332 if (!(msg->flags & I2C_M_NOSTART)) {
333 /*
334 * Send a start for the first message and a restart for
335 * subsequent messages.
336 */
337 ret = riic_send_start_cond(dev, !first_msg);
338 if (ret < 0)
339 return ret;
340 }
341
342 ret = riic_i2c_raw_write(dev, &addr_byte, 1);
343 if (ret < 0) {
344 /*
345 * We're aborting the transfer while still in master transmit
346 * mode.
347 */
348 riic_transmit_stop(dev);
349 return ret;
350 }
351
352 if (msg->flags & I2C_M_RD)
353 return riic_receive_data(dev, msg);
354
355 return riic_transmit_data(dev, msg);
356}
357
358static int riic_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
359{
360 int ret, i;
361
362 ret = riic_check_busy(dev);
363 if (ret < 0)
364 return ret;
365
366 /* Ensure that the last message is terminated with a stop bit. */
367 msg[nmsgs - 1].flags |= I2C_M_STOP;
368
369 for (i = 0; i < nmsgs; i++) {
370 ret = riic_xfer_one(dev, &msg[i], !i);
371 if (ret)
372 return ret;
373 }
374
375 return 0;
376}
377
378static int riic_deblock(struct udevice *dev)
379{
380 struct riic_priv *priv = dev_get_priv(dev);
381 int i = 0;
382
383 /*
384 * Issue clock cycles on SCL to hopefully unblock whatever is holding
385 * SDA low. These clock cycles may trigger error conditions such as
386 * Arbitration Lost, so we clear the status bits in ICSR2 after each
387 * cycle.
388 */
389 while (!(readb(priv->base + RIIC_ICCR1) & ICCR1_SDAI)) {
390 if (i++ == I2C_DEBLOCK_MAX_CYCLES)
391 return -EIO;
392
393 setbits_8(priv->base + RIIC_ICCR1, ICCR1_CLO);
394 if (wait_for_bit_8(priv->base + RIIC_ICCR1, ICCR1_CLO, 0,
395 RIIC_TIMEOUT_MSEC, false))
396 return -ETIMEDOUT;
397 writeb(0, priv->base + RIIC_ICSR2);
398 }
399
400 /*
401 * We have released SDA, but the I2C module is now out of sync
402 * with the bus state, so we need to reset its state machine.
403 */
404 setbits_8(priv->base + RIIC_ICCR1, ICCR1_IICRST);
405 clrbits_8(priv->base + RIIC_ICCR1, ICCR1_IICRST);
406
407 return 0;
408}
409
410static int riic_set_bus_speed(struct udevice *dev, uint bus_speed)
411{
412 struct riic_priv *priv = dev_get_priv(dev);
413 ulong refclk;
414 uint total_ticks, cks, brl, brh;
415
416 if (bus_speed > I2C_SPEED_FAST_PLUS_RATE) {
417 dev_err(dev, "unsupported bus speed (%dHz). %d max\n", bus_speed,
418 I2C_SPEED_FAST_PLUS_RATE);
419 return -EINVAL;
420 }
421
422 /*
423 * Assume the default register settings:
424 * FER.SCLE = 1 (SCL sync circuit enabled, adds 2 or 3 cycles)
425 * FER.NFE = 1 (noise circuit enabled)
426 * MR3.NF = 0 (1 cycle of noise filtered out)
427 *
428 * Freq (CKS=000) = (I2CCLK + tr + tf)/ (BRH + 3 + 1) + (BRL + 3 + 1)
429 * Freq (CKS!=000) = (I2CCLK + tr + tf)/ (BRH + 2 + 1) + (BRL + 2 + 1)
430 */
431
432 /*
433 * Determine reference clock rate. We must be able to get the desired
434 * frequency with only 62 clock ticks max (31 high, 31 low).
435 * Aim for a duty of 60% LOW, 40% HIGH.
436 */
437 refclk = clk_get_rate(&priv->clk);
438 total_ticks = DIV_ROUND_UP(refclk, bus_speed ?: 1);
439
440 for (cks = 0; cks < 7; cks++) {
441 /*
442 * 60% low time must be less than BRL + 2 + 1
443 * BRL max register value is 0x1F.
444 */
445 brl = ((total_ticks * 6) / 10);
446 if (brl <= (0x1f + 3))
447 break;
448
449 total_ticks /= 2;
450 refclk /= 2;
451 }
452
453 if (brl > (0x1f + 3)) {
454 dev_err(dev, "invalid speed (%u). Too slow.\n", bus_speed);
455 return -EINVAL;
456 }
457
458 brh = total_ticks - brl;
459
460 /* Remove automatic clock ticks for sync circuit and NF */
461 if (cks == 0) {
462 brl -= 4;
463 brh -= 4;
464 } else {
465 brl -= 3;
466 brh -= 3;
467 }
468
469 /*
470 * If SCL rise and fall times weren't set in the device tree, set them
471 * based on the desired bus speed and the maximum timings given in the
472 * I2C specification.
473 */
474 if (priv->flags & RIIC_FLAG_DEFAULT_SCL_RISE_TIME)
475 priv->scl_rise_ns = bus_speed <= I2C_SPEED_STANDARD_RATE ? 1000 :
476 bus_speed <= I2C_SPEED_FAST_RATE ? 300 : 120;
477 if (priv->flags & RIIC_FLAG_DEFAULT_SCL_FALL_TIME)
478 priv->scl_fall_ns = bus_speed <= I2C_SPEED_FAST_RATE ? 300 : 120;
479
480 /*
481 * Remove clock ticks for rise and fall times. Convert ns to clock
482 * ticks.
483 */
484 brl -= priv->scl_fall_ns / (1000000000 / refclk);
485 brh -= priv->scl_rise_ns / (1000000000 / refclk);
486
487 /* Adjust for min register values for when SCLE=1 and NFE=1 */
488 if (brl < 1)
489 brl = 1;
490 if (brh < 1)
491 brh = 1;
492
493 priv->bus_speed = refclk / total_ticks;
494 dev_dbg(dev, "freq=%u, duty=%d, fall=%lu, rise=%lu, cks=%d, brl=%d, brh=%d\n",
495 priv->bus_speed, ((brl + 3) * 100) / (brl + brh + 6),
496 priv->scl_fall_ns / (1000000000 / refclk),
497 priv->scl_rise_ns / (1000000000 / refclk), cks, brl, brh);
498
499 setbits_8(priv->base + RIIC_ICCR1, ICCR1_IICRST);
500 writeb(ICMR1_CKS(cks), priv->base + RIIC_ICMR1);
501 writeb(brh | ICBRH_RESERVED, priv->base + RIIC_ICBRH);
502 writeb(brl | ICBRL_RESERVED, priv->base + RIIC_ICBRL);
503 clrbits_8(priv->base + RIIC_ICCR1, ICCR1_IICRST);
504
505 return 0;
506}
507
508static int riic_get_bus_speed(struct udevice *dev)
509{
510 struct riic_priv *priv = dev_get_priv(dev);
511
512 return priv->bus_speed;
513}
514
515static const struct dm_i2c_ops riic_ops = {
516 .xfer = riic_xfer,
517 .deblock = riic_deblock,
518 .set_bus_speed = riic_set_bus_speed,
519 .get_bus_speed = riic_get_bus_speed,
520};
521
522static int riic_init_setting(struct udevice *dev)
523{
524 struct riic_priv *priv = dev_get_priv(dev);
525 int ret;
526
527 clrbits_8(priv->base + RIIC_ICCR1, ICCR1_ICE);
528 setbits_8(priv->base + RIIC_ICCR1, ICCR1_IICRST);
529 setbits_8(priv->base + RIIC_ICCR1, ICCR1_ICE);
530
531 /*
532 * Set a default bitrate. The rate may be overridden based on the device
533 * tree as part of i2c_post_probe().
534 */
535 ret = riic_set_bus_speed(dev, I2C_SPEED_STANDARD_RATE);
536 if (ret < 0)
537 goto err;
538
539 clrbits_8(priv->base + RIIC_ICCR1, ICCR1_IICRST);
540
541 /* Make sure the bus is not stuck. */
542 if (!(readb(priv->base + RIIC_ICCR1) & ICCR1_SDAI)) {
543 dev_dbg(dev, "clearing SDA low state\n");
544 ret = riic_deblock(dev);
545 if (ret) {
546 dev_err(dev, "failed to clear SDA low state!\n");
547 goto err;
548 }
549 }
550 return 0;
551
552err:
553 clrbits_8(priv->base + RIIC_ICCR1, ICCR1_ICE | ICCR1_IICRST);
554 return ret;
555}
556
557static int riic_probe(struct udevice *dev)
558{
559 struct riic_priv *priv = dev_get_priv(dev);
560 struct reset_ctl rst;
561 int ret;
562
563 priv->base = dev_read_addr_ptr(dev);
564
565 ret = dev_read_u32(dev, "i2c-scl-rising-time-ns", &priv->scl_rise_ns);
566 if (ret)
567 priv->flags |= RIIC_FLAG_DEFAULT_SCL_RISE_TIME;
568 ret = dev_read_u32(dev, "i2c-scl-falling-time-ns", &priv->scl_fall_ns);
569 if (ret)
570 priv->flags |= RIIC_FLAG_DEFAULT_SCL_FALL_TIME;
571
572 ret = clk_get_by_index(dev, 0, &priv->clk);
573 if (ret) {
574 dev_err(dev, "failed to get clock\n");
575 return ret;
576 }
577
578 ret = clk_enable(&priv->clk);
579 if (ret) {
580 dev_err(dev, "failed to enable clock\n");
581 return ret;
582 }
583
584 ret = reset_get_by_index(dev, 0, &rst);
585 if (ret < 0) {
586 dev_err(dev, "failed to get reset line\n");
587 goto err_get_reset;
588 }
589
590 ret = reset_deassert(&rst);
591 if (ret < 0) {
592 dev_err(dev, "failed to de-assert reset line\n");
593 goto err_reset;
594 }
595
596 ret = riic_init_setting(dev);
597 if (ret < 0) {
598 dev_err(dev, "failed to init i2c bus interface\n");
599 goto err_init;
600 }
601
602 return 0;
603
604err_init:
605 reset_assert(&rst);
606err_reset:
607 reset_free(&rst);
608err_get_reset:
609 clk_disable(&priv->clk);
610 return ret;
611}
612
613static const struct udevice_id riic_ids[] = {
614 { .compatible = "renesas,riic-rz", },
615 { /* sentinel */ }
616};
617
618U_BOOT_DRIVER(riic_i2c) = {
619 .name = "riic-i2c",
620 .id = UCLASS_I2C,
621 .of_match = riic_ids,
622 .probe = riic_probe,
623 .priv_auto = sizeof(struct riic_priv),
624 .ops = &riic_ops,
625};