1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Driver for the TWSI (i2c) controller found on the Marvell
4 * orion5x and kirkwood SoC families.
5 *
6 * Author: Albert Aribaud <albert.u.boot@aribaud.net>
7 * Copyright (c) 2010 Albert Aribaud.
8 */
9
10 #include <config.h>
11 #include <i2c.h>
12 #include <log.h>
13 #include <asm/global_data.h>
14 #include <linux/delay.h>
15 #include <linux/errno.h>
16 #include <asm/io.h>
17 #include <linux/bitops.h>
18 #include <linux/compat.h>
19 #if CONFIG_IS_ENABLED(DM_I2C)
20 #include <clk.h>
21 #include <dm.h>
22 #include <reset.h>
23 #endif
24
25 DECLARE_GLOBAL_DATA_PTR;
26
27 /*
28 * Include a file that will provide CONFIG_I2C_MVTWSI_BASE*, and possibly other
29 * settings
30 */
31
32 #if !CONFIG_IS_ENABLED(DM_I2C)
33 #if defined(CONFIG_ARCH_ORION5X)
34 #include <asm/arch/orion5x.h>
35 #elif (defined(CONFIG_ARCH_KIRKWOOD) || defined(CONFIG_ARCH_MVEBU))
36 #include <asm/arch/soc.h>
37 #elif defined(CONFIG_ARCH_SUNXI)
38 #include <asm/arch/i2c.h>
39 #else
40 #error Driver mvtwsi not supported by SoC or board
41 #endif
42 #endif /* CONFIG_DM_I2C */
43
44 /*
45 * On SUNXI, we get CFG_SYS_TCLK from this include, so we want to
46 * always have it.
47 */
48 #if CONFIG_IS_ENABLED(DM_I2C) && defined(CONFIG_ARCH_SUNXI)
49 #include <asm/arch/i2c.h>
50 #endif
51
52 /*
53 * TWSI register structure
54 */
55
56 #ifdef CONFIG_ARCH_SUNXI
57
58 struct mvtwsi_registers {
59 u32 slave_address;
60 u32 xtnd_slave_addr;
61 u32 data;
62 u32 control;
63 u32 status;
64 u32 baudrate;
65 u32 soft_reset;
66 u32 debug; /* Dummy field for build compatibility with mvebu */
67 };
68
69 #else
70
71 struct mvtwsi_registers {
72 u32 slave_address;
73 u32 data;
74 u32 control;
75 union {
76 u32 status; /* When reading */
77 u32 baudrate; /* When writing */
78 };
79 u32 xtnd_slave_addr;
80 u32 reserved0[2];
81 u32 soft_reset;
82 u32 reserved1[27];
83 u32 debug;
84 };
85
86 #endif
87
88 #if CONFIG_IS_ENABLED(DM_I2C)
89 struct mvtwsi_i2c_dev {
90 /* TWSI Register base for the device */
91 struct mvtwsi_registers *base;
92 /* Number of the device (determined from cell-index property) */
93 int index;
94 /* The I2C slave address for the device */
95 u8 slaveadd;
96 /* The configured I2C speed in Hz */
97 uint speed;
98 /* The current length of a clock period (depending on speed) */
99 uint tick;
100 };
101 #endif /* CONFIG_DM_I2C */
102
103 /*
104 * enum mvtwsi_ctrl_register_fields - Bit masks for flags in the control
105 * register
106 */
107 enum mvtwsi_ctrl_register_fields {
108 /* Acknowledge bit */
109 MVTWSI_CONTROL_ACK = 0x00000004,
110 /* Interrupt flag */
111 MVTWSI_CONTROL_IFLG = 0x00000008,
112 /* Stop bit */
113 MVTWSI_CONTROL_STOP = 0x00000010,
114 /* Start bit */
115 MVTWSI_CONTROL_START = 0x00000020,
116 /* I2C enable */
117 MVTWSI_CONTROL_TWSIEN = 0x00000040,
118 /* Interrupt enable */
119 MVTWSI_CONTROL_INTEN = 0x00000080,
120 };
121
122 /*
123 * On sun6i and newer, IFLG is a write-clear bit, which is cleared by writing 1;
124 * on other platforms, it is a normal r/w bit, which is cleared by writing 0.
125 */
126
127 #if defined(CONFIG_SUNXI_GEN_SUN6I) || defined(CONFIG_SUN50I_GEN_H6) || \
128 defined(CONFIG_SUNXI_GEN_NCAT2)
129 #define MVTWSI_CONTROL_CLEAR_IFLG 0x00000008
130 #else
131 #define MVTWSI_CONTROL_CLEAR_IFLG 0x00000000
132 #endif
133
134 /*
135 * enum mvstwsi_status_values - Possible values of I2C controller's status
136 * register
137 *
138 * Only those statuses expected in normal master operation on
139 * non-10-bit-address devices are specified.
140 *
141 * Every status that's unexpected during normal operation (bus errors,
142 * arbitration losses, missing ACKs...) is passed back to the caller as an error
143 * code.
144 */
145 enum mvstwsi_status_values {
146 /* Protocol violation on bus; this is a terminal state */
147 MVTWSI_BUS_ERROR = 0x00,
148 /* START condition transmitted */
149 MVTWSI_STATUS_START = 0x08,
150 /* Repeated START condition transmitted */
151 MVTWSI_STATUS_REPEATED_START = 0x10,
152 /* Address + write bit transmitted, ACK received */
153 MVTWSI_STATUS_ADDR_W_ACK = 0x18,
154 /* Data transmitted, ACK received */
155 MVTWSI_STATUS_DATA_W_ACK = 0x28,
156 /* Address + read bit transmitted, ACK received */
157 MVTWSI_STATUS_ADDR_R_ACK = 0x40,
158 /* Address + read bit transmitted, ACK not received */
159 MVTWSI_STATUS_ADDR_R_NAK = 0x48,
160 /* Data received, ACK transmitted */
161 MVTWSI_STATUS_DATA_R_ACK = 0x50,
162 /* Data received, ACK not transmitted */
163 MVTWSI_STATUS_DATA_R_NAK = 0x58,
164 /* No relevant status */
165 MVTWSI_STATUS_IDLE = 0xF8,
166 };
167
168 /*
169 * enum mvstwsi_ack_flags - Determine whether a read byte should be
170 * acknowledged or not.
171 */
172 enum mvtwsi_ack_flags {
173 /* Send NAK after received byte */
174 MVTWSI_READ_NAK = 0,
175 /* Send ACK after received byte */
176 MVTWSI_READ_ACK = 1,
177 };
178
179 /*
180 * calc_tick() - Calculate the duration of a clock cycle from the I2C speed
181 *
182 * @speed: The speed in Hz to calculate the clock cycle duration for.
183 * Return: The duration of a clock cycle in ns.
184 */
calc_tick(uint speed)185 inline uint calc_tick(uint speed)
186 {
187 /* One tick = the duration of a period at the specified speed in ns (we
188 * add 100 ns to be on the safe side) */
189 return (1000000000u / speed) + 100;
190 }
191
192 #if !CONFIG_IS_ENABLED(DM_I2C)
193
194 /*
195 * twsi_get_base() - Get controller register base for specified adapter
196 *
197 * @adap: Adapter to get the register base for.
198 * Return: Register base for the specified adapter.
199 */
twsi_get_base(struct i2c_adapter * adap)200 static struct mvtwsi_registers *twsi_get_base(struct i2c_adapter *adap)
201 {
202 switch (adap->hwadapnr) {
203 #ifdef CFG_I2C_MVTWSI_BASE0
204 case 0:
205 return (struct mvtwsi_registers *)CFG_I2C_MVTWSI_BASE0;
206 #endif
207 #ifdef CFG_I2C_MVTWSI_BASE1
208 case 1:
209 return (struct mvtwsi_registers *)CFG_I2C_MVTWSI_BASE1;
210 #endif
211 #ifdef CFG_I2C_MVTWSI_BASE2
212 case 2:
213 return (struct mvtwsi_registers *)CFG_I2C_MVTWSI_BASE2;
214 #endif
215 #ifdef CONFIG_I2C_MVTWSI_BASE3
216 case 3:
217 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE3;
218 #endif
219 #ifdef CONFIG_I2C_MVTWSI_BASE4
220 case 4:
221 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE4;
222 #endif
223 #ifdef CONFIG_I2C_MVTWSI_BASE5
224 case 5:
225 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE5;
226 #endif
227 default:
228 printf("Missing mvtwsi controller %d base\n", adap->hwadapnr);
229 break;
230 }
231
232 return NULL;
233 }
234 #endif
235
236 /*
237 * enum mvtwsi_error_class - types of I2C errors
238 */
239 enum mvtwsi_error_class {
240 /* The controller returned a different status than expected */
241 MVTWSI_ERROR_WRONG_STATUS = 0x01,
242 /* The controller timed out */
243 MVTWSI_ERROR_TIMEOUT = 0x02,
244 };
245
246 /*
247 * mvtwsi_error() - Build I2C return code from error information
248 *
249 * For debugging purposes, this function packs some information of an occurred
250 * error into a return code. These error codes are returned from I2C API
251 * functions (i2c_{read,write}, dm_i2c_{read,write}, etc.).
252 *
253 * @ec: The error class of the error (enum mvtwsi_error_class).
254 * @lc: The last value of the control register.
255 * @ls: The last value of the status register.
256 * @es: The expected value of the status register.
257 * Return: The generated error code.
258 */
mvtwsi_error(uint ec,uint lc,uint ls,uint es)259 inline uint mvtwsi_error(uint ec, uint lc, uint ls, uint es)
260 {
261 return ((ec << 24) & 0xFF000000)
262 | ((lc << 16) & 0x00FF0000)
263 | ((ls << 8) & 0x0000FF00)
264 | (es & 0xFF);
265 }
266
267 /*
268 * twsi_wait() - Wait for I2C bus interrupt flag and check status, or time out.
269 *
270 * Return: Zero if status is as expected, or a non-zero code if either a time
271 * out occurred, or the status was not the expected one.
272 */
twsi_wait(struct mvtwsi_registers * twsi,int expected_status,uint tick)273 static int twsi_wait(struct mvtwsi_registers *twsi, int expected_status,
274 uint tick)
275 {
276 int control, status;
277 int timeout = 1000;
278
279 do {
280 control = readl(&twsi->control);
281 if (control & MVTWSI_CONTROL_IFLG) {
282 /*
283 * On Armada 38x it seems that the controller works as
284 * if it first set the MVTWSI_CONTROL_IFLAG in the
285 * control register and only after that it changed the
286 * status register.
287 * This sometimes caused weird bugs which only appeared
288 * on selected I2C speeds and even then only sometimes.
289 * We therefore add here a simple ndealy(100), which
290 * seems to fix this weird bug.
291 */
292 ndelay(100);
293 status = readl(&twsi->status);
294 if (status == expected_status)
295 return 0;
296 else
297 return mvtwsi_error(
298 MVTWSI_ERROR_WRONG_STATUS,
299 control, status, expected_status);
300 }
301 ndelay(tick); /* One clock cycle */
302 } while (timeout--);
303 status = readl(&twsi->status);
304 return mvtwsi_error(MVTWSI_ERROR_TIMEOUT, control, status,
305 expected_status);
306 }
307
308 /*
309 * twsi_start() - Assert a START condition on the bus.
310 *
311 * This function is used in both single I2C transactions and inside
312 * back-to-back transactions (repeated starts).
313 *
314 * @twsi: The MVTWSI register structure to use.
315 * @expected_status: The I2C bus status expected to be asserted after the
316 * operation completion.
317 * @tick: The duration of a clock cycle at the current I2C speed.
318 * Return: Zero if status is as expected, or a non-zero code if either a time
319 * out occurred or the status was not the expected one.
320 */
twsi_start(struct mvtwsi_registers * twsi,int expected_status,uint tick)321 static int twsi_start(struct mvtwsi_registers *twsi, int expected_status,
322 uint tick)
323 {
324 /* Assert START */
325 writel(MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_START |
326 MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
327 /* Wait for controller to process START */
328 return twsi_wait(twsi, expected_status, tick);
329 }
330
331 /*
332 * twsi_send() - Send a byte on the I2C bus.
333 *
334 * The byte may be part of an address byte or data.
335 *
336 * @twsi: The MVTWSI register structure to use.
337 * @byte: The byte to send.
338 * @expected_status: The I2C bus status expected to be asserted after the
339 * operation completion.
340 * @tick: The duration of a clock cycle at the current I2C speed.
341 * Return: Zero if status is as expected, or a non-zero code if either a time
342 * out occurred or the status was not the expected one.
343 */
twsi_send(struct mvtwsi_registers * twsi,u8 byte,int expected_status,uint tick)344 static int twsi_send(struct mvtwsi_registers *twsi, u8 byte,
345 int expected_status, uint tick)
346 {
347 /* Write byte to data register for sending */
348 writel(byte, &twsi->data);
349 /* Clear any pending interrupt -- that will cause sending */
350 writel(MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_CLEAR_IFLG,
351 &twsi->control);
352 /* Wait for controller to receive byte, and check ACK */
353 return twsi_wait(twsi, expected_status, tick);
354 }
355
356 /*
357 * twsi_recv() - Receive a byte on the I2C bus.
358 *
359 * The static variable mvtwsi_control_flags controls whether we ack or nak.
360 *
361 * @twsi: The MVTWSI register structure to use.
362 * @byte: The byte to send.
363 * @ack_flag: Flag that determines whether the received byte should
364 * be acknowledged by the controller or not (sent ACK/NAK).
365 * @tick: The duration of a clock cycle at the current I2C speed.
366 * Return: Zero if status is as expected, or a non-zero code if either a time
367 * out occurred or the status was not the expected one.
368 */
twsi_recv(struct mvtwsi_registers * twsi,u8 * byte,int ack_flag,uint tick)369 static int twsi_recv(struct mvtwsi_registers *twsi, u8 *byte, int ack_flag,
370 uint tick)
371 {
372 int expected_status, status, control;
373
374 /* Compute expected status based on passed ACK flag */
375 expected_status = ack_flag ? MVTWSI_STATUS_DATA_R_ACK :
376 MVTWSI_STATUS_DATA_R_NAK;
377 /* Acknowledge *previous state*, and launch receive */
378 control = MVTWSI_CONTROL_TWSIEN;
379 control |= ack_flag == MVTWSI_READ_ACK ? MVTWSI_CONTROL_ACK : 0;
380 writel(control | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
381 /* Wait for controller to receive byte, and assert ACK or NAK */
382 status = twsi_wait(twsi, expected_status, tick);
383 /* If we did receive the expected byte, store it */
384 if (status == 0)
385 *byte = readl(&twsi->data);
386 return status;
387 }
388
389 /*
390 * twsi_stop() - Assert a STOP condition on the bus.
391 *
392 * This function is also used to force the bus back to idle state (SDA =
393 * SCL = 1).
394 *
395 * @twsi: The MVTWSI register structure to use.
396 * @tick: The duration of a clock cycle at the current I2C speed.
397 * Return: Zero if the operation succeeded, or a non-zero code if a time out
398 * occurred.
399 */
twsi_stop(struct mvtwsi_registers * twsi,uint tick)400 static int twsi_stop(struct mvtwsi_registers *twsi, uint tick)
401 {
402 int control, stop_status;
403 int status = 0;
404 int timeout = 1000;
405
406 /* Assert STOP */
407 control = MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_STOP;
408 writel(control | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
409 /* Wait for IDLE; IFLG won't rise, so we can't use twsi_wait() */
410 do {
411 stop_status = readl(&twsi->status);
412 if (stop_status == MVTWSI_STATUS_IDLE)
413 break;
414 ndelay(tick); /* One clock cycle */
415 } while (timeout--);
416 control = readl(&twsi->control);
417 if (stop_status != MVTWSI_STATUS_IDLE)
418 status = mvtwsi_error(MVTWSI_ERROR_TIMEOUT,
419 control, status, MVTWSI_STATUS_IDLE);
420 return status;
421 }
422
423 /*
424 * twsi_calc_freq() - Compute I2C frequency depending on m and n parameters.
425 *
426 * @n: Parameter 'n' for the frequency calculation algorithm.
427 * @m: Parameter 'm' for the frequency calculation algorithm.
428 * Return: The I2C frequency corresponding to the passed m and n parameters.
429 */
twsi_calc_freq(const int n,const int m)430 static uint twsi_calc_freq(const int n, const int m)
431 {
432 #ifdef CONFIG_ARCH_SUNXI
433 return CFG_SYS_TCLK / (10 * (m + 1) * (1 << n));
434 #else
435 return CFG_SYS_TCLK / (10 * (m + 1) * (2 << n));
436 #endif
437 }
438
439 /*
440 * twsi_reset() - Reset the I2C controller.
441 *
442 * Resetting the controller also resets the baud rate and slave address, hence
443 * they must be re-established after the reset.
444 *
445 * @twsi: The MVTWSI register structure to use.
446 */
twsi_reset(struct mvtwsi_registers * twsi)447 static void twsi_reset(struct mvtwsi_registers *twsi)
448 {
449 /* Reset controller */
450 writel(0, &twsi->soft_reset);
451 /* Wait 2 ms -- this is what the Marvell LSP does */
452 udelay(20000);
453 }
454
455 /*
456 * __twsi_i2c_set_bus_speed() - Set the speed of the I2C controller.
457 *
458 * This function sets baud rate to the highest possible value that does not
459 * exceed the requested rate.
460 *
461 * @twsi: The MVTWSI register structure to use.
462 * @requested_speed: The desired frequency the controller should run at
463 * in Hz.
464 * Return: The actual frequency the controller was configured to.
465 */
__twsi_i2c_set_bus_speed(struct mvtwsi_registers * twsi,uint requested_speed)466 static uint __twsi_i2c_set_bus_speed(struct mvtwsi_registers *twsi,
467 uint requested_speed)
468 {
469 uint tmp_speed, highest_speed, n, m;
470 uint baud = 0x44; /* Baud rate after controller reset */
471
472 highest_speed = 0;
473 /* Successively try m, n combinations, and use the combination
474 * resulting in the largest speed that's not above the requested
475 * speed */
476 for (n = 0; n < 8; n++) {
477 for (m = 0; m < 16; m++) {
478 tmp_speed = twsi_calc_freq(n, m);
479 if ((tmp_speed <= requested_speed) &&
480 (tmp_speed > highest_speed)) {
481 highest_speed = tmp_speed;
482 baud = (m << 3) | n;
483 }
484 }
485 }
486 writel(baud, &twsi->baudrate);
487
488 /* Wait for controller for one tick */
489 #if CONFIG_IS_ENABLED(DM_I2C)
490 ndelay(calc_tick(highest_speed));
491 #else
492 ndelay(10000);
493 #endif
494 return highest_speed;
495 }
496
497 /*
498 * __twsi_i2c_init() - Initialize the I2C controller.
499 *
500 * @twsi: The MVTWSI register structure to use.
501 * @speed: The initial frequency the controller should run at
502 * in Hz.
503 * @slaveadd: The I2C address to be set for the I2C master.
504 * @actual_speed: A output parameter that receives the actual frequency
505 * in Hz the controller was set to by the function.
506 * Return: Zero if the operation succeeded, or a non-zero code if a time out
507 * occurred.
508 */
__twsi_i2c_init(struct mvtwsi_registers * twsi,int speed,int slaveadd,uint * actual_speed)509 static void __twsi_i2c_init(struct mvtwsi_registers *twsi, int speed,
510 int slaveadd, uint *actual_speed)
511 {
512 uint tmp_speed;
513
514 /* Reset controller */
515 twsi_reset(twsi);
516 /* Set speed */
517 tmp_speed = __twsi_i2c_set_bus_speed(twsi, speed);
518 if (actual_speed)
519 *actual_speed = tmp_speed;
520 /* Set slave address; even though we don't use it */
521 writel(slaveadd, &twsi->slave_address);
522 writel(0, &twsi->xtnd_slave_addr);
523 /* Assert STOP, but don't care for the result */
524 #if CONFIG_IS_ENABLED(DM_I2C)
525 (void) twsi_stop(twsi, calc_tick(*actual_speed));
526 #else
527 (void) twsi_stop(twsi, 10000);
528 #endif
529 }
530
531 /*
532 * __twsi_i2c_reinit() - Reset and reinitialize the I2C controller.
533 *
534 * This function should be called to get the MVTWSI controller out of the
535 * "bus error" state. It saves and restores the baud and address registers.
536 *
537 * @twsi: The MVTWSI register structure to use.
538 * @tick: The duration of a clock cycle at the current I2C speed.
539 */
__twsi_i2c_reinit(struct mvtwsi_registers * twsi,uint tick)540 static void __twsi_i2c_reinit(struct mvtwsi_registers *twsi, uint tick)
541 {
542 uint baud;
543 uint slaveadd;
544
545 /* Save baud, address registers */
546 baud = readl(&twsi->baudrate);
547 slaveadd = readl(&twsi->slave_address);
548
549 /* Reset controller */
550 twsi_reset(twsi);
551
552 /* Restore baud, address registers */
553 writel(baud, &twsi->baudrate);
554 writel(slaveadd, &twsi->slave_address);
555 writel(0, &twsi->xtnd_slave_addr);
556
557 /* Assert STOP, but don't care for the result */
558 (void) twsi_stop(twsi, tick);
559 }
560
561 /*
562 * i2c_begin() - Start a I2C transaction.
563 *
564 * Begin a I2C transaction with a given expected start status and chip address.
565 * A START is asserted, and the address byte is sent to the I2C controller. The
566 * expected address status will be derived from the direction bit (bit 0) of
567 * the address byte.
568 *
569 * @twsi: The MVTWSI register structure to use.
570 * @expected_start_status: The I2C status the controller is expected to
571 * assert after the address byte was sent.
572 * @addr: The address byte to be sent.
573 * @tick: The duration of a clock cycle at the current
574 * I2C speed.
575 * Return: Zero if the operation succeeded, or a non-zero code if a time out or
576 * unexpected I2C status occurred.
577 */
i2c_begin(struct mvtwsi_registers * twsi,int expected_start_status,u8 addr,uint tick)578 static int i2c_begin(struct mvtwsi_registers *twsi, int expected_start_status,
579 u8 addr, uint tick)
580 {
581 int status, expected_addr_status;
582
583 /* Compute the expected address status from the direction bit in
584 * the address byte */
585 if (addr & 1) /* Reading */
586 expected_addr_status = MVTWSI_STATUS_ADDR_R_ACK;
587 else /* Writing */
588 expected_addr_status = MVTWSI_STATUS_ADDR_W_ACK;
589 /* Assert START */
590 status = twsi_start(twsi, expected_start_status, tick);
591 /* Send out the address if the start went well */
592 if (status == 0)
593 status = twsi_send(twsi, addr, expected_addr_status, tick);
594 /* Return 0, or the status of the first failure */
595 return status;
596 }
597
598 /*
599 * __twsi_i2c_probe_chip() - Probe the given I2C chip address.
600 *
601 * This function begins a I2C read transaction, does a dummy read and NAKs; if
602 * the procedure succeeds, the chip is considered to be present.
603 *
604 * @twsi: The MVTWSI register structure to use.
605 * @chip: The chip address to probe.
606 * @tick: The duration of a clock cycle at the current I2C speed.
607 * Return: Zero if the operation succeeded, or a non-zero code if a time out or
608 * unexpected I2C status occurred.
609 */
__twsi_i2c_probe_chip(struct mvtwsi_registers * twsi,uchar chip,uint tick)610 static int __twsi_i2c_probe_chip(struct mvtwsi_registers *twsi, uchar chip,
611 uint tick)
612 {
613 u8 dummy_byte;
614 int status;
615
616 /* Begin i2c read */
617 status = i2c_begin(twsi, MVTWSI_STATUS_START, (chip << 1) | 1, tick);
618 /* Dummy read was accepted: receive byte, but NAK it. */
619 if (status == 0)
620 status = twsi_recv(twsi, &dummy_byte, MVTWSI_READ_NAK, tick);
621 /* Stop transaction */
622 twsi_stop(twsi, tick);
623 /* Return 0, or the status of the first failure */
624 return status;
625 }
626
627 /*
628 * __twsi_i2c_read() - Read data from a I2C chip.
629 *
630 * This function begins a I2C write transaction, and transmits the address
631 * bytes; then begins a I2C read transaction, and receives the data bytes.
632 *
633 * NOTE: Some devices want a stop right before the second start, while some
634 * will choke if it is there. Since deciding this is not yet supported in
635 * higher level APIs, we need to make a decision here, and for the moment that
636 * will be a repeated start without a preceding stop.
637 *
638 * @twsi: The MVTWSI register structure to use.
639 * @chip: The chip address to read from.
640 * @addr: The address bytes to send.
641 * @alen: The length of the address bytes in bytes.
642 * @data: The buffer to receive the data read from the chip (has to have
643 * a size of at least 'length' bytes).
644 * @length: The amount of data to be read from the chip in bytes.
645 * @tick: The duration of a clock cycle at the current I2C speed.
646 * Return: Zero if the operation succeeded, or a non-zero code if a time out or
647 * unexpected I2C status occurred.
648 */
__twsi_i2c_read(struct mvtwsi_registers * twsi,uchar chip,u8 * addr,int alen,uchar * data,int length,uint tick)649 static int __twsi_i2c_read(struct mvtwsi_registers *twsi, uchar chip,
650 u8 *addr, int alen, uchar *data, int length,
651 uint tick)
652 {
653 int status = 0;
654 int stop_status;
655 int expected_start = MVTWSI_STATUS_START;
656
657 /* Check for (and clear) a bus error from a previous failed transaction
658 * or another master on the same bus */
659 if (readl(&twsi->status) == MVTWSI_BUS_ERROR)
660 __twsi_i2c_reinit(twsi, tick);
661
662 if (alen > 0) {
663 /* Begin i2c write to send the address bytes */
664 status = i2c_begin(twsi, expected_start, (chip << 1), tick);
665 /* Send address bytes */
666 while ((status == 0) && alen--)
667 status = twsi_send(twsi, addr[alen],
668 MVTWSI_STATUS_DATA_W_ACK, tick);
669 /* Send repeated STARTs after the initial START */
670 expected_start = MVTWSI_STATUS_REPEATED_START;
671 }
672 /* Begin i2c read to receive data bytes */
673 if (status == 0)
674 status = i2c_begin(twsi, expected_start, (chip << 1) | 1, tick);
675 /* Receive actual data bytes; set NAK if we if we have nothing more to
676 * read */
677 while ((status == 0) && length--)
678 status = twsi_recv(twsi, data++,
679 length > 0 ?
680 MVTWSI_READ_ACK : MVTWSI_READ_NAK, tick);
681 /* Stop transaction */
682 stop_status = twsi_stop(twsi, tick);
683 /* Return 0, or the status of the first failure */
684 return status != 0 ? status : stop_status;
685 }
686
687 /*
688 * __twsi_i2c_write() - Send data to a I2C chip.
689 *
690 * This function begins a I2C write transaction, and transmits the address
691 * bytes; then begins a new I2C write transaction, and sends the data bytes.
692 *
693 * @twsi: The MVTWSI register structure to use.
694 * @chip: The chip address to read from.
695 * @addr: The address bytes to send.
696 * @alen: The length of the address bytes in bytes.
697 * @data: The buffer containing the data to be sent to the chip.
698 * @length: The length of data to be sent to the chip in bytes.
699 * @tick: The duration of a clock cycle at the current I2C speed.
700 * Return: Zero if the operation succeeded, or a non-zero code if a time out or
701 * unexpected I2C status occurred.
702 */
__twsi_i2c_write(struct mvtwsi_registers * twsi,uchar chip,u8 * addr,int alen,uchar * data,int length,uint tick)703 static int __twsi_i2c_write(struct mvtwsi_registers *twsi, uchar chip,
704 u8 *addr, int alen, uchar *data, int length,
705 uint tick)
706 {
707 int status, stop_status;
708
709 /* Check for (and clear) a bus error from a previous failed transaction
710 * or another master on the same bus */
711 if (readl(&twsi->status) == MVTWSI_BUS_ERROR)
712 __twsi_i2c_reinit(twsi, tick);
713
714 /* Begin i2c write to send first the address bytes, then the
715 * data bytes */
716 status = i2c_begin(twsi, MVTWSI_STATUS_START, (chip << 1), tick);
717 /* Send address bytes */
718 while ((status == 0) && (alen-- > 0))
719 status = twsi_send(twsi, addr[alen], MVTWSI_STATUS_DATA_W_ACK,
720 tick);
721 /* Send data bytes */
722 while ((status == 0) && (length-- > 0))
723 status = twsi_send(twsi, *(data++), MVTWSI_STATUS_DATA_W_ACK,
724 tick);
725 /* Stop transaction */
726 stop_status = twsi_stop(twsi, tick);
727 /* Return 0, or the status of the first failure */
728 return status != 0 ? status : stop_status;
729 }
730
731 #if !CONFIG_IS_ENABLED(DM_I2C)
twsi_i2c_init(struct i2c_adapter * adap,int speed,int slaveadd)732 static void twsi_i2c_init(struct i2c_adapter *adap, int speed,
733 int slaveadd)
734 {
735 struct mvtwsi_registers *twsi = twsi_get_base(adap);
736 __twsi_i2c_init(twsi, speed, slaveadd, NULL);
737 }
738
twsi_i2c_set_bus_speed(struct i2c_adapter * adap,uint requested_speed)739 static uint twsi_i2c_set_bus_speed(struct i2c_adapter *adap,
740 uint requested_speed)
741 {
742 struct mvtwsi_registers *twsi = twsi_get_base(adap);
743 __twsi_i2c_set_bus_speed(twsi, requested_speed);
744 return 0;
745 }
746
twsi_i2c_probe(struct i2c_adapter * adap,uchar chip)747 static int twsi_i2c_probe(struct i2c_adapter *adap, uchar chip)
748 {
749 struct mvtwsi_registers *twsi = twsi_get_base(adap);
750 return __twsi_i2c_probe_chip(twsi, chip, 10000);
751 }
752
twsi_i2c_read(struct i2c_adapter * adap,uchar chip,uint addr,int alen,uchar * data,int length)753 static int twsi_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
754 int alen, uchar *data, int length)
755 {
756 struct mvtwsi_registers *twsi = twsi_get_base(adap);
757 u8 addr_bytes[4];
758
759 addr_bytes[0] = (addr >> 0) & 0xFF;
760 addr_bytes[1] = (addr >> 8) & 0xFF;
761 addr_bytes[2] = (addr >> 16) & 0xFF;
762 addr_bytes[3] = (addr >> 24) & 0xFF;
763
764 return __twsi_i2c_read(twsi, chip, addr_bytes, alen, data, length,
765 10000);
766 }
767
twsi_i2c_write(struct i2c_adapter * adap,uchar chip,uint addr,int alen,uchar * data,int length)768 static int twsi_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
769 int alen, uchar *data, int length)
770 {
771 struct mvtwsi_registers *twsi = twsi_get_base(adap);
772 u8 addr_bytes[4];
773
774 addr_bytes[0] = (addr >> 0) & 0xFF;
775 addr_bytes[1] = (addr >> 8) & 0xFF;
776 addr_bytes[2] = (addr >> 16) & 0xFF;
777 addr_bytes[3] = (addr >> 24) & 0xFF;
778
779 return __twsi_i2c_write(twsi, chip, addr_bytes, alen, data, length,
780 10000);
781 }
782
783 #ifdef CFG_I2C_MVTWSI_BASE0
784 U_BOOT_I2C_ADAP_COMPLETE(twsi0, twsi_i2c_init, twsi_i2c_probe,
785 twsi_i2c_read, twsi_i2c_write,
786 twsi_i2c_set_bus_speed,
787 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0)
788 #endif
789 #ifdef CFG_I2C_MVTWSI_BASE1
790 U_BOOT_I2C_ADAP_COMPLETE(twsi1, twsi_i2c_init, twsi_i2c_probe,
791 twsi_i2c_read, twsi_i2c_write,
792 twsi_i2c_set_bus_speed,
793 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 1)
794
795 #endif
796 #ifdef CFG_I2C_MVTWSI_BASE2
797 U_BOOT_I2C_ADAP_COMPLETE(twsi2, twsi_i2c_init, twsi_i2c_probe,
798 twsi_i2c_read, twsi_i2c_write,
799 twsi_i2c_set_bus_speed,
800 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 2)
801
802 #endif
803 #ifdef CONFIG_I2C_MVTWSI_BASE3
804 U_BOOT_I2C_ADAP_COMPLETE(twsi3, twsi_i2c_init, twsi_i2c_probe,
805 twsi_i2c_read, twsi_i2c_write,
806 twsi_i2c_set_bus_speed,
807 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 3)
808
809 #endif
810 #ifdef CONFIG_I2C_MVTWSI_BASE4
811 U_BOOT_I2C_ADAP_COMPLETE(twsi4, twsi_i2c_init, twsi_i2c_probe,
812 twsi_i2c_read, twsi_i2c_write,
813 twsi_i2c_set_bus_speed,
814 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 4)
815
816 #endif
817 #ifdef CONFIG_I2C_MVTWSI_BASE5
818 U_BOOT_I2C_ADAP_COMPLETE(twsi5, twsi_i2c_init, twsi_i2c_probe,
819 twsi_i2c_read, twsi_i2c_write,
820 twsi_i2c_set_bus_speed,
821 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 5)
822
823 #endif
824 #else /* CONFIG_DM_I2C */
825
826 static int mvtwsi_i2c_probe_chip(struct udevice *bus, u32 chip_addr,
827 u32 chip_flags)
828 {
829 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
830 return __twsi_i2c_probe_chip(dev->base, chip_addr, dev->tick);
831 }
832
833 static int mvtwsi_i2c_set_bus_speed(struct udevice *bus, uint speed)
834 {
835 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
836
837 dev->speed = __twsi_i2c_set_bus_speed(dev->base, speed);
838 dev->tick = calc_tick(dev->speed);
839
840 return 0;
841 }
842
843 static int mvtwsi_i2c_of_to_plat(struct udevice *bus)
844 {
845 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
846
847 dev->base = dev_read_addr_ptr(bus);
848
849 if (!dev->base)
850 return -ENOMEM;
851
852 dev->index = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus),
853 "cell-index", -1);
854 dev->slaveadd = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus),
855 "u-boot,i2c-slave-addr", 0x0);
856 dev->speed = dev_read_u32_default(bus, "clock-frequency",
857 I2C_SPEED_STANDARD_RATE);
858
859 return 0;
860 }
861
862 static void twsi_disable_i2c_slave(struct mvtwsi_registers *twsi)
863 {
864 clrbits_le32(&twsi->debug, BIT(18));
865 }
866
867 static int mvtwsi_i2c_bind(struct udevice *bus)
868 {
869 struct mvtwsi_registers *twsi = dev_read_addr_ptr(bus);
870
871 /* Disable the hidden slave in i2c0 of these platforms */
872 if ((IS_ENABLED(CONFIG_ARMADA_38X) ||
873 IS_ENABLED(CONFIG_ARCH_KIRKWOOD) ||
874 IS_ENABLED(CONFIG_ARMADA_8K)) && !dev_seq(bus))
875 twsi_disable_i2c_slave(twsi);
876
877 return 0;
878 }
879
880 static int mvtwsi_i2c_probe(struct udevice *bus)
881 {
882 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
883 struct reset_ctl reset;
884 struct clk clk;
885 uint actual_speed;
886 int ret;
887
888 ret = reset_get_by_index(bus, 0, &reset);
889 if (!ret)
890 reset_deassert(&reset);
891
892 ret = clk_get_by_index(bus, 0, &clk);
893 if (!ret)
894 clk_enable(&clk);
895
896 __twsi_i2c_init(dev->base, dev->speed, dev->slaveadd, &actual_speed);
897 dev->speed = actual_speed;
898 dev->tick = calc_tick(dev->speed);
899 return 0;
900 }
901
902 static int mvtwsi_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
903 {
904 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
905 struct i2c_msg *dmsg, *omsg, dummy;
906 u8 *addr_buf_ptr;
907 u8 addr_buf[4];
908 int i;
909
910 memset(&dummy, 0, sizeof(struct i2c_msg));
911
912 /* We expect either two messages (one with an offset and one with the
913 * actual data) or one message (just data or offset/data combined) */
914 if (nmsgs > 2 || nmsgs == 0) {
915 debug("%s: Only one or two messages are supported.", __func__);
916 return -1;
917 }
918
919 omsg = nmsgs == 1 ? &dummy : msg;
920 dmsg = nmsgs == 1 ? msg : msg + 1;
921
922 /* We need to swap the register address if its size is > 1 */
923 addr_buf_ptr = &addr_buf[0];
924 for (i = omsg->len; i > 0; i--)
925 *addr_buf_ptr++ = omsg->buf[i - 1];
926
927 if (dmsg->flags & I2C_M_RD)
928 return __twsi_i2c_read(dev->base, dmsg->addr, addr_buf,
929 omsg->len, dmsg->buf, dmsg->len,
930 dev->tick);
931 else
932 return __twsi_i2c_write(dev->base, dmsg->addr, addr_buf,
933 omsg->len, dmsg->buf, dmsg->len,
934 dev->tick);
935 }
936
937 static const struct dm_i2c_ops mvtwsi_i2c_ops = {
938 .xfer = mvtwsi_i2c_xfer,
939 .probe_chip = mvtwsi_i2c_probe_chip,
940 .set_bus_speed = mvtwsi_i2c_set_bus_speed,
941 };
942
943 static const struct udevice_id mvtwsi_i2c_ids[] = {
944 { .compatible = "marvell,mv64xxx-i2c", },
945 { .compatible = "marvell,mv78230-i2c", },
946 { .compatible = "allwinner,sun4i-a10-i2c", },
947 { .compatible = "allwinner,sun6i-a31-i2c", },
948 { /* sentinel */ }
949 };
950
951 U_BOOT_DRIVER(i2c_mvtwsi) = {
952 .name = "i2c_mvtwsi",
953 .id = UCLASS_I2C,
954 .of_match = mvtwsi_i2c_ids,
955 .bind = mvtwsi_i2c_bind,
956 .probe = mvtwsi_i2c_probe,
957 .of_to_plat = mvtwsi_i2c_of_to_plat,
958 .priv_auto = sizeof(struct mvtwsi_i2c_dev),
959 .ops = &mvtwsi_i2c_ops,
960 };
961 #endif /* CONFIG_DM_I2C */
962