1 // SPDX-License-Identifier: GPL-2.0+
3 * Driver for the TWSI (i2c) controller found on the Marvell
4 * orion5x and kirkwood SoC families.
6 * Author: Albert Aribaud <albert.u.boot@aribaud.net>
7 * Copyright (c) 2010 Albert Aribaud.
13 #include <asm/global_data.h>
14 #include <linux/delay.h>
15 #include <linux/errno.h>
17 #include <linux/bitops.h>
18 #include <linux/compat.h>
19 #if CONFIG_IS_ENABLED(DM_I2C)
23 DECLARE_GLOBAL_DATA_PTR;
26 * Include a file that will provide CONFIG_I2C_MVTWSI_BASE*, and possibly other
30 #if !CONFIG_IS_ENABLED(DM_I2C)
31 #if defined(CONFIG_ARCH_ORION5X)
32 #include <asm/arch/orion5x.h>
33 #elif (defined(CONFIG_ARCH_KIRKWOOD) || defined(CONFIG_ARCH_MVEBU))
34 #include <asm/arch/soc.h>
35 #elif defined(CONFIG_ARCH_SUNXI)
36 #include <asm/arch/i2c.h>
38 #error Driver mvtwsi not supported by SoC or board
40 #endif /* CONFIG_DM_I2C */
43 * On SUNXI, we get CONFIG_SYS_TCLK from this include, so we want to
46 #if CONFIG_IS_ENABLED(DM_I2C) && defined(CONFIG_ARCH_SUNXI)
47 #include <asm/arch/i2c.h>
51 * TWSI register structure
54 #ifdef CONFIG_ARCH_SUNXI
56 struct mvtwsi_registers {
64 u32 debug; /* Dummy field for build compatibility with mvebu */
69 struct mvtwsi_registers {
74 u32 status; /* When reading */
75 u32 baudrate; /* When writing */
86 #if CONFIG_IS_ENABLED(DM_I2C)
87 struct mvtwsi_i2c_dev {
88 /* TWSI Register base for the device */
89 struct mvtwsi_registers *base;
90 /* Number of the device (determined from cell-index property) */
92 /* The I2C slave address for the device */
94 /* The configured I2C speed in Hz */
96 /* The current length of a clock period (depending on speed) */
99 #endif /* CONFIG_DM_I2C */
102 * enum mvtwsi_ctrl_register_fields - Bit masks for flags in the control
105 enum mvtwsi_ctrl_register_fields {
106 /* Acknowledge bit */
107 MVTWSI_CONTROL_ACK = 0x00000004,
109 MVTWSI_CONTROL_IFLG = 0x00000008,
111 MVTWSI_CONTROL_STOP = 0x00000010,
113 MVTWSI_CONTROL_START = 0x00000020,
115 MVTWSI_CONTROL_TWSIEN = 0x00000040,
116 /* Interrupt enable */
117 MVTWSI_CONTROL_INTEN = 0x00000080,
121 * On sun6i and newer, IFLG is a write-clear bit, which is cleared by writing 1;
122 * on other platforms, it is a normal r/w bit, which is cleared by writing 0.
125 #if defined(CONFIG_SUNXI_GEN_SUN6I) || defined(CONFIG_SUN50I_GEN_H6)
126 #define MVTWSI_CONTROL_CLEAR_IFLG 0x00000008
128 #define MVTWSI_CONTROL_CLEAR_IFLG 0x00000000
132 * enum mvstwsi_status_values - Possible values of I2C controller's status
135 * Only those statuses expected in normal master operation on
136 * non-10-bit-address devices are specified.
138 * Every status that's unexpected during normal operation (bus errors,
139 * arbitration losses, missing ACKs...) is passed back to the caller as an error
142 enum mvstwsi_status_values {
143 /* START condition transmitted */
144 MVTWSI_STATUS_START = 0x08,
145 /* Repeated START condition transmitted */
146 MVTWSI_STATUS_REPEATED_START = 0x10,
147 /* Address + write bit transmitted, ACK received */
148 MVTWSI_STATUS_ADDR_W_ACK = 0x18,
149 /* Data transmitted, ACK received */
150 MVTWSI_STATUS_DATA_W_ACK = 0x28,
151 /* Address + read bit transmitted, ACK received */
152 MVTWSI_STATUS_ADDR_R_ACK = 0x40,
153 /* Address + read bit transmitted, ACK not received */
154 MVTWSI_STATUS_ADDR_R_NAK = 0x48,
155 /* Data received, ACK transmitted */
156 MVTWSI_STATUS_DATA_R_ACK = 0x50,
157 /* Data received, ACK not transmitted */
158 MVTWSI_STATUS_DATA_R_NAK = 0x58,
159 /* No relevant status */
160 MVTWSI_STATUS_IDLE = 0xF8,
164 * enum mvstwsi_ack_flags - Determine whether a read byte should be
165 * acknowledged or not.
167 enum mvtwsi_ack_flags {
168 /* Send NAK after received byte */
170 /* Send ACK after received byte */
175 * calc_tick() - Calculate the duration of a clock cycle from the I2C speed
177 * @speed: The speed in Hz to calculate the clock cycle duration for.
178 * @return The duration of a clock cycle in ns.
180 inline uint calc_tick(uint speed)
182 /* One tick = the duration of a period at the specified speed in ns (we
183 * add 100 ns to be on the safe side) */
184 return (1000000000u / speed) + 100;
187 #if !CONFIG_IS_ENABLED(DM_I2C)
190 * twsi_get_base() - Get controller register base for specified adapter
192 * @adap: Adapter to get the register base for.
193 * @return Register base for the specified adapter.
195 static struct mvtwsi_registers *twsi_get_base(struct i2c_adapter *adap)
197 switch (adap->hwadapnr) {
198 #ifdef CONFIG_I2C_MVTWSI_BASE0
200 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE0;
202 #ifdef CONFIG_I2C_MVTWSI_BASE1
204 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE1;
206 #ifdef CONFIG_I2C_MVTWSI_BASE2
208 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE2;
210 #ifdef CONFIG_I2C_MVTWSI_BASE3
212 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE3;
214 #ifdef CONFIG_I2C_MVTWSI_BASE4
216 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE4;
218 #ifdef CONFIG_I2C_MVTWSI_BASE5
220 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE5;
223 printf("Missing mvtwsi controller %d base\n", adap->hwadapnr);
232 * enum mvtwsi_error_class - types of I2C errors
234 enum mvtwsi_error_class {
235 /* The controller returned a different status than expected */
236 MVTWSI_ERROR_WRONG_STATUS = 0x01,
237 /* The controller timed out */
238 MVTWSI_ERROR_TIMEOUT = 0x02,
242 * mvtwsi_error() - Build I2C return code from error information
244 * For debugging purposes, this function packs some information of an occurred
245 * error into a return code. These error codes are returned from I2C API
246 * functions (i2c_{read,write}, dm_i2c_{read,write}, etc.).
248 * @ec: The error class of the error (enum mvtwsi_error_class).
249 * @lc: The last value of the control register.
250 * @ls: The last value of the status register.
251 * @es: The expected value of the status register.
252 * @return The generated error code.
254 inline uint mvtwsi_error(uint ec, uint lc, uint ls, uint es)
256 return ((ec << 24) & 0xFF000000)
257 | ((lc << 16) & 0x00FF0000)
258 | ((ls << 8) & 0x0000FF00)
263 * twsi_wait() - Wait for I2C bus interrupt flag and check status, or time out.
265 * @return Zero if status is as expected, or a non-zero code if either a time
266 * out occurred, or the status was not the expected one.
268 static int twsi_wait(struct mvtwsi_registers *twsi, int expected_status,
275 control = readl(&twsi->control);
276 if (control & MVTWSI_CONTROL_IFLG) {
278 * On Armada 38x it seems that the controller works as
279 * if it first set the MVTWSI_CONTROL_IFLAG in the
280 * control register and only after that it changed the
282 * This sometimes caused weird bugs which only appeared
283 * on selected I2C speeds and even then only sometimes.
284 * We therefore add here a simple ndealy(100), which
285 * seems to fix this weird bug.
288 status = readl(&twsi->status);
289 if (status == expected_status)
293 MVTWSI_ERROR_WRONG_STATUS,
294 control, status, expected_status);
296 ndelay(tick); /* One clock cycle */
298 status = readl(&twsi->status);
299 return mvtwsi_error(MVTWSI_ERROR_TIMEOUT, control, status,
304 * twsi_start() - Assert a START condition on the bus.
306 * This function is used in both single I2C transactions and inside
307 * back-to-back transactions (repeated starts).
309 * @twsi: The MVTWSI register structure to use.
310 * @expected_status: The I2C bus status expected to be asserted after the
311 * operation completion.
312 * @tick: The duration of a clock cycle at the current I2C speed.
313 * @return Zero if status is as expected, or a non-zero code if either a time
314 * out occurred or the status was not the expected one.
316 static int twsi_start(struct mvtwsi_registers *twsi, int expected_status,
320 writel(MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_START |
321 MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
322 /* Wait for controller to process START */
323 return twsi_wait(twsi, expected_status, tick);
327 * twsi_send() - Send a byte on the I2C bus.
329 * The byte may be part of an address byte or data.
331 * @twsi: The MVTWSI register structure to use.
332 * @byte: The byte to send.
333 * @expected_status: The I2C bus status expected to be asserted after the
334 * operation completion.
335 * @tick: The duration of a clock cycle at the current I2C speed.
336 * @return Zero if status is as expected, or a non-zero code if either a time
337 * out occurred or the status was not the expected one.
339 static int twsi_send(struct mvtwsi_registers *twsi, u8 byte,
340 int expected_status, uint tick)
342 /* Write byte to data register for sending */
343 writel(byte, &twsi->data);
344 /* Clear any pending interrupt -- that will cause sending */
345 writel(MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_CLEAR_IFLG,
347 /* Wait for controller to receive byte, and check ACK */
348 return twsi_wait(twsi, expected_status, tick);
352 * twsi_recv() - Receive a byte on the I2C bus.
354 * The static variable mvtwsi_control_flags controls whether we ack or nak.
356 * @twsi: The MVTWSI register structure to use.
357 * @byte: The byte to send.
358 * @ack_flag: Flag that determines whether the received byte should
359 * be acknowledged by the controller or not (sent ACK/NAK).
360 * @tick: The duration of a clock cycle at the current I2C speed.
361 * @return Zero if status is as expected, or a non-zero code if either a time
362 * out occurred or the status was not the expected one.
364 static int twsi_recv(struct mvtwsi_registers *twsi, u8 *byte, int ack_flag,
367 int expected_status, status, control;
369 /* Compute expected status based on passed ACK flag */
370 expected_status = ack_flag ? MVTWSI_STATUS_DATA_R_ACK :
371 MVTWSI_STATUS_DATA_R_NAK;
372 /* Acknowledge *previous state*, and launch receive */
373 control = MVTWSI_CONTROL_TWSIEN;
374 control |= ack_flag == MVTWSI_READ_ACK ? MVTWSI_CONTROL_ACK : 0;
375 writel(control | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
376 /* Wait for controller to receive byte, and assert ACK or NAK */
377 status = twsi_wait(twsi, expected_status, tick);
378 /* If we did receive the expected byte, store it */
380 *byte = readl(&twsi->data);
385 * twsi_stop() - Assert a STOP condition on the bus.
387 * This function is also used to force the bus back to idle state (SDA =
390 * @twsi: The MVTWSI register structure to use.
391 * @tick: The duration of a clock cycle at the current I2C speed.
392 * @return Zero if the operation succeeded, or a non-zero code if a time out
395 static int twsi_stop(struct mvtwsi_registers *twsi, uint tick)
397 int control, stop_status;
402 control = MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_STOP;
403 writel(control | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
404 /* Wait for IDLE; IFLG won't rise, so we can't use twsi_wait() */
406 stop_status = readl(&twsi->status);
407 if (stop_status == MVTWSI_STATUS_IDLE)
409 ndelay(tick); /* One clock cycle */
411 control = readl(&twsi->control);
412 if (stop_status != MVTWSI_STATUS_IDLE)
413 status = mvtwsi_error(MVTWSI_ERROR_TIMEOUT,
414 control, status, MVTWSI_STATUS_IDLE);
419 * twsi_calc_freq() - Compute I2C frequency depending on m and n parameters.
421 * @n: Parameter 'n' for the frequency calculation algorithm.
422 * @m: Parameter 'm' for the frequency calculation algorithm.
423 * @return The I2C frequency corresponding to the passed m and n parameters.
425 static uint twsi_calc_freq(const int n, const int m)
427 #ifdef CONFIG_ARCH_SUNXI
428 return CONFIG_SYS_TCLK / (10 * (m + 1) * (1 << n));
430 return CONFIG_SYS_TCLK / (10 * (m + 1) * (2 << n));
435 * twsi_reset() - Reset the I2C controller.
437 * Resetting the controller also resets the baud rate and slave address, hence
438 * they must be re-established after the reset.
440 * @twsi: The MVTWSI register structure to use.
442 static void twsi_reset(struct mvtwsi_registers *twsi)
444 /* Reset controller */
445 writel(0, &twsi->soft_reset);
446 /* Wait 2 ms -- this is what the Marvell LSP does */
451 * __twsi_i2c_set_bus_speed() - Set the speed of the I2C controller.
453 * This function sets baud rate to the highest possible value that does not
454 * exceed the requested rate.
456 * @twsi: The MVTWSI register structure to use.
457 * @requested_speed: The desired frequency the controller should run at
459 * @return The actual frequency the controller was configured to.
461 static uint __twsi_i2c_set_bus_speed(struct mvtwsi_registers *twsi,
462 uint requested_speed)
464 uint tmp_speed, highest_speed, n, m;
465 uint baud = 0x44; /* Baud rate after controller reset */
468 /* Successively try m, n combinations, and use the combination
469 * resulting in the largest speed that's not above the requested
471 for (n = 0; n < 8; n++) {
472 for (m = 0; m < 16; m++) {
473 tmp_speed = twsi_calc_freq(n, m);
474 if ((tmp_speed <= requested_speed) &&
475 (tmp_speed > highest_speed)) {
476 highest_speed = tmp_speed;
481 writel(baud, &twsi->baudrate);
483 /* Wait for controller for one tick */
484 #if CONFIG_IS_ENABLED(DM_I2C)
485 ndelay(calc_tick(highest_speed));
489 return highest_speed;
493 * __twsi_i2c_init() - Initialize the I2C controller.
495 * @twsi: The MVTWSI register structure to use.
496 * @speed: The initial frequency the controller should run at
498 * @slaveadd: The I2C address to be set for the I2C master.
499 * @actual_speed: A output parameter that receives the actual frequency
500 * in Hz the controller was set to by the function.
501 * @return Zero if the operation succeeded, or a non-zero code if a time out
504 static void __twsi_i2c_init(struct mvtwsi_registers *twsi, int speed,
505 int slaveadd, uint *actual_speed)
509 /* Reset controller */
512 tmp_speed = __twsi_i2c_set_bus_speed(twsi, speed);
514 *actual_speed = tmp_speed;
515 /* Set slave address; even though we don't use it */
516 writel(slaveadd, &twsi->slave_address);
517 writel(0, &twsi->xtnd_slave_addr);
518 /* Assert STOP, but don't care for the result */
519 #if CONFIG_IS_ENABLED(DM_I2C)
520 (void) twsi_stop(twsi, calc_tick(*actual_speed));
522 (void) twsi_stop(twsi, 10000);
527 * i2c_begin() - Start a I2C transaction.
529 * Begin a I2C transaction with a given expected start status and chip address.
530 * A START is asserted, and the address byte is sent to the I2C controller. The
531 * expected address status will be derived from the direction bit (bit 0) of
534 * @twsi: The MVTWSI register structure to use.
535 * @expected_start_status: The I2C status the controller is expected to
536 * assert after the address byte was sent.
537 * @addr: The address byte to be sent.
538 * @tick: The duration of a clock cycle at the current
540 * @return Zero if the operation succeeded, or a non-zero code if a time out or
541 * unexpected I2C status occurred.
543 static int i2c_begin(struct mvtwsi_registers *twsi, int expected_start_status,
546 int status, expected_addr_status;
548 /* Compute the expected address status from the direction bit in
549 * the address byte */
550 if (addr & 1) /* Reading */
551 expected_addr_status = MVTWSI_STATUS_ADDR_R_ACK;
553 expected_addr_status = MVTWSI_STATUS_ADDR_W_ACK;
555 status = twsi_start(twsi, expected_start_status, tick);
556 /* Send out the address if the start went well */
558 status = twsi_send(twsi, addr, expected_addr_status, tick);
559 /* Return 0, or the status of the first failure */
564 * __twsi_i2c_probe_chip() - Probe the given I2C chip address.
566 * This function begins a I2C read transaction, does a dummy read and NAKs; if
567 * the procedure succeeds, the chip is considered to be present.
569 * @twsi: The MVTWSI register structure to use.
570 * @chip: The chip address to probe.
571 * @tick: The duration of a clock cycle at the current I2C speed.
572 * @return Zero if the operation succeeded, or a non-zero code if a time out or
573 * unexpected I2C status occurred.
575 static int __twsi_i2c_probe_chip(struct mvtwsi_registers *twsi, uchar chip,
582 status = i2c_begin(twsi, MVTWSI_STATUS_START, (chip << 1) | 1, tick);
583 /* Dummy read was accepted: receive byte, but NAK it. */
585 status = twsi_recv(twsi, &dummy_byte, MVTWSI_READ_NAK, tick);
586 /* Stop transaction */
587 twsi_stop(twsi, tick);
588 /* Return 0, or the status of the first failure */
593 * __twsi_i2c_read() - Read data from a I2C chip.
595 * This function begins a I2C write transaction, and transmits the address
596 * bytes; then begins a I2C read transaction, and receives the data bytes.
598 * NOTE: Some devices want a stop right before the second start, while some
599 * will choke if it is there. Since deciding this is not yet supported in
600 * higher level APIs, we need to make a decision here, and for the moment that
601 * will be a repeated start without a preceding stop.
603 * @twsi: The MVTWSI register structure to use.
604 * @chip: The chip address to read from.
605 * @addr: The address bytes to send.
606 * @alen: The length of the address bytes in bytes.
607 * @data: The buffer to receive the data read from the chip (has to have
608 * a size of at least 'length' bytes).
609 * @length: The amount of data to be read from the chip in bytes.
610 * @tick: The duration of a clock cycle at the current I2C speed.
611 * @return Zero if the operation succeeded, or a non-zero code if a time out or
612 * unexpected I2C status occurred.
614 static int __twsi_i2c_read(struct mvtwsi_registers *twsi, uchar chip,
615 u8 *addr, int alen, uchar *data, int length,
620 int expected_start = MVTWSI_STATUS_START;
623 /* Begin i2c write to send the address bytes */
624 status = i2c_begin(twsi, expected_start, (chip << 1), tick);
625 /* Send address bytes */
626 while ((status == 0) && alen--)
627 status = twsi_send(twsi, addr[alen],
628 MVTWSI_STATUS_DATA_W_ACK, tick);
629 /* Send repeated STARTs after the initial START */
630 expected_start = MVTWSI_STATUS_REPEATED_START;
632 /* Begin i2c read to receive data bytes */
634 status = i2c_begin(twsi, expected_start, (chip << 1) | 1, tick);
635 /* Receive actual data bytes; set NAK if we if we have nothing more to
637 while ((status == 0) && length--)
638 status = twsi_recv(twsi, data++,
640 MVTWSI_READ_ACK : MVTWSI_READ_NAK, tick);
641 /* Stop transaction */
642 stop_status = twsi_stop(twsi, tick);
643 /* Return 0, or the status of the first failure */
644 return status != 0 ? status : stop_status;
648 * __twsi_i2c_write() - Send data to a I2C chip.
650 * This function begins a I2C write transaction, and transmits the address
651 * bytes; then begins a new I2C write transaction, and sends the data bytes.
653 * @twsi: The MVTWSI register structure to use.
654 * @chip: The chip address to read from.
655 * @addr: The address bytes to send.
656 * @alen: The length of the address bytes in bytes.
657 * @data: The buffer containing the data to be sent to the chip.
658 * @length: The length of data to be sent to the chip in bytes.
659 * @tick: The duration of a clock cycle at the current I2C speed.
660 * @return Zero if the operation succeeded, or a non-zero code if a time out or
661 * unexpected I2C status occurred.
663 static int __twsi_i2c_write(struct mvtwsi_registers *twsi, uchar chip,
664 u8 *addr, int alen, uchar *data, int length,
667 int status, stop_status;
669 /* Begin i2c write to send first the address bytes, then the
671 status = i2c_begin(twsi, MVTWSI_STATUS_START, (chip << 1), tick);
672 /* Send address bytes */
673 while ((status == 0) && (alen-- > 0))
674 status = twsi_send(twsi, addr[alen], MVTWSI_STATUS_DATA_W_ACK,
676 /* Send data bytes */
677 while ((status == 0) && (length-- > 0))
678 status = twsi_send(twsi, *(data++), MVTWSI_STATUS_DATA_W_ACK,
680 /* Stop transaction */
681 stop_status = twsi_stop(twsi, tick);
682 /* Return 0, or the status of the first failure */
683 return status != 0 ? status : stop_status;
686 #if !CONFIG_IS_ENABLED(DM_I2C)
687 static void twsi_i2c_init(struct i2c_adapter *adap, int speed,
690 struct mvtwsi_registers *twsi = twsi_get_base(adap);
691 __twsi_i2c_init(twsi, speed, slaveadd, NULL);
694 static uint twsi_i2c_set_bus_speed(struct i2c_adapter *adap,
695 uint requested_speed)
697 struct mvtwsi_registers *twsi = twsi_get_base(adap);
698 __twsi_i2c_set_bus_speed(twsi, requested_speed);
702 static int twsi_i2c_probe(struct i2c_adapter *adap, uchar chip)
704 struct mvtwsi_registers *twsi = twsi_get_base(adap);
705 return __twsi_i2c_probe_chip(twsi, chip, 10000);
708 static int twsi_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
709 int alen, uchar *data, int length)
711 struct mvtwsi_registers *twsi = twsi_get_base(adap);
714 addr_bytes[0] = (addr >> 0) & 0xFF;
715 addr_bytes[1] = (addr >> 8) & 0xFF;
716 addr_bytes[2] = (addr >> 16) & 0xFF;
717 addr_bytes[3] = (addr >> 24) & 0xFF;
719 return __twsi_i2c_read(twsi, chip, addr_bytes, alen, data, length,
723 static int twsi_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
724 int alen, uchar *data, int length)
726 struct mvtwsi_registers *twsi = twsi_get_base(adap);
729 addr_bytes[0] = (addr >> 0) & 0xFF;
730 addr_bytes[1] = (addr >> 8) & 0xFF;
731 addr_bytes[2] = (addr >> 16) & 0xFF;
732 addr_bytes[3] = (addr >> 24) & 0xFF;
734 return __twsi_i2c_write(twsi, chip, addr_bytes, alen, data, length,
738 #ifdef CONFIG_I2C_MVTWSI_BASE0
739 U_BOOT_I2C_ADAP_COMPLETE(twsi0, twsi_i2c_init, twsi_i2c_probe,
740 twsi_i2c_read, twsi_i2c_write,
741 twsi_i2c_set_bus_speed,
742 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0)
744 #ifdef CONFIG_I2C_MVTWSI_BASE1
745 U_BOOT_I2C_ADAP_COMPLETE(twsi1, twsi_i2c_init, twsi_i2c_probe,
746 twsi_i2c_read, twsi_i2c_write,
747 twsi_i2c_set_bus_speed,
748 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 1)
751 #ifdef CONFIG_I2C_MVTWSI_BASE2
752 U_BOOT_I2C_ADAP_COMPLETE(twsi2, twsi_i2c_init, twsi_i2c_probe,
753 twsi_i2c_read, twsi_i2c_write,
754 twsi_i2c_set_bus_speed,
755 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 2)
758 #ifdef CONFIG_I2C_MVTWSI_BASE3
759 U_BOOT_I2C_ADAP_COMPLETE(twsi3, twsi_i2c_init, twsi_i2c_probe,
760 twsi_i2c_read, twsi_i2c_write,
761 twsi_i2c_set_bus_speed,
762 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 3)
765 #ifdef CONFIG_I2C_MVTWSI_BASE4
766 U_BOOT_I2C_ADAP_COMPLETE(twsi4, twsi_i2c_init, twsi_i2c_probe,
767 twsi_i2c_read, twsi_i2c_write,
768 twsi_i2c_set_bus_speed,
769 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 4)
772 #ifdef CONFIG_I2C_MVTWSI_BASE5
773 U_BOOT_I2C_ADAP_COMPLETE(twsi5, twsi_i2c_init, twsi_i2c_probe,
774 twsi_i2c_read, twsi_i2c_write,
775 twsi_i2c_set_bus_speed,
776 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 5)
779 #else /* CONFIG_DM_I2C */
781 static int mvtwsi_i2c_probe_chip(struct udevice *bus, u32 chip_addr,
784 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
785 return __twsi_i2c_probe_chip(dev->base, chip_addr, dev->tick);
788 static int mvtwsi_i2c_set_bus_speed(struct udevice *bus, uint speed)
790 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
792 dev->speed = __twsi_i2c_set_bus_speed(dev->base, speed);
793 dev->tick = calc_tick(dev->speed);
798 static int mvtwsi_i2c_of_to_plat(struct udevice *bus)
800 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
802 dev->base = dev_read_addr_ptr(bus);
807 dev->index = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus),
809 dev->slaveadd = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus),
810 "u-boot,i2c-slave-addr", 0x0);
811 dev->speed = dev_read_u32_default(bus, "clock-frequency",
812 I2C_SPEED_STANDARD_RATE);
817 static void twsi_disable_i2c_slave(struct mvtwsi_registers *twsi)
819 clrbits_le32(&twsi->debug, BIT(18));
822 static int mvtwsi_i2c_bind(struct udevice *bus)
824 struct mvtwsi_registers *twsi = dev_read_addr_ptr(bus);
826 /* Disable the hidden slave in i2c0 of these platforms */
827 if ((IS_ENABLED(CONFIG_ARMADA_38X) ||
828 IS_ENABLED(CONFIG_ARCH_KIRKWOOD) ||
829 IS_ENABLED(CONFIG_ARMADA_8K)) && !dev_seq(bus))
830 twsi_disable_i2c_slave(twsi);
835 static int mvtwsi_i2c_probe(struct udevice *bus)
837 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
840 __twsi_i2c_init(dev->base, dev->speed, dev->slaveadd, &actual_speed);
841 dev->speed = actual_speed;
842 dev->tick = calc_tick(dev->speed);
846 static int mvtwsi_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
848 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
849 struct i2c_msg *dmsg, *omsg, dummy;
851 memset(&dummy, 0, sizeof(struct i2c_msg));
853 /* We expect either two messages (one with an offset and one with the
854 * actual data) or one message (just data or offset/data combined) */
855 if (nmsgs > 2 || nmsgs == 0) {
856 debug("%s: Only one or two messages are supported.", __func__);
860 omsg = nmsgs == 1 ? &dummy : msg;
861 dmsg = nmsgs == 1 ? msg : msg + 1;
863 if (dmsg->flags & I2C_M_RD)
864 return __twsi_i2c_read(dev->base, dmsg->addr, omsg->buf,
865 omsg->len, dmsg->buf, dmsg->len,
868 return __twsi_i2c_write(dev->base, dmsg->addr, omsg->buf,
869 omsg->len, dmsg->buf, dmsg->len,
873 static const struct dm_i2c_ops mvtwsi_i2c_ops = {
874 .xfer = mvtwsi_i2c_xfer,
875 .probe_chip = mvtwsi_i2c_probe_chip,
876 .set_bus_speed = mvtwsi_i2c_set_bus_speed,
879 static const struct udevice_id mvtwsi_i2c_ids[] = {
880 { .compatible = "marvell,mv64xxx-i2c", },
881 { .compatible = "marvell,mv78230-i2c", },
882 { .compatible = "allwinner,sun6i-a31-i2c", },
886 U_BOOT_DRIVER(i2c_mvtwsi) = {
887 .name = "i2c_mvtwsi",
889 .of_match = mvtwsi_i2c_ids,
890 .bind = mvtwsi_i2c_bind,
891 .probe = mvtwsi_i2c_probe,
892 .of_to_plat = mvtwsi_i2c_of_to_plat,
893 .priv_auto = sizeof(struct mvtwsi_i2c_dev),
894 .ops = &mvtwsi_i2c_ops,
896 #endif /* CONFIG_DM_I2C */