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 <linux/delay.h>
14 #include <linux/errno.h>
16 #include <linux/bitops.h>
17 #include <linux/compat.h>
22 DECLARE_GLOBAL_DATA_PTR;
25 * Include a file that will provide CONFIG_I2C_MVTWSI_BASE*, and possibly other
30 #if defined(CONFIG_ARCH_ORION5X)
31 #include <asm/arch/orion5x.h>
32 #elif (defined(CONFIG_ARCH_KIRKWOOD) || defined(CONFIG_ARCH_MVEBU))
33 #include <asm/arch/soc.h>
34 #elif defined(CONFIG_ARCH_SUNXI)
35 #include <asm/arch/i2c.h>
37 #error Driver mvtwsi not supported by SoC or board
39 #endif /* CONFIG_DM_I2C */
42 * On SUNXI, we get CONFIG_SYS_TCLK from this include, so we want to
45 #if defined(CONFIG_DM_I2C) && defined(CONFIG_ARCH_SUNXI)
46 #include <asm/arch/i2c.h>
50 * TWSI register structure
53 #ifdef CONFIG_ARCH_SUNXI
55 struct mvtwsi_registers {
63 u32 debug; /* Dummy field for build compatibility with mvebu */
68 struct mvtwsi_registers {
73 u32 status; /* When reading */
74 u32 baudrate; /* When writing */
86 struct mvtwsi_i2c_dev {
87 /* TWSI Register base for the device */
88 struct mvtwsi_registers *base;
89 /* Number of the device (determined from cell-index property) */
91 /* The I2C slave address for the device */
93 /* The configured I2C speed in Hz */
95 /* The current length of a clock period (depending on speed) */
98 #endif /* CONFIG_DM_I2C */
101 * enum mvtwsi_ctrl_register_fields - Bit masks for flags in the control
104 enum mvtwsi_ctrl_register_fields {
105 /* Acknowledge bit */
106 MVTWSI_CONTROL_ACK = 0x00000004,
108 MVTWSI_CONTROL_IFLG = 0x00000008,
110 MVTWSI_CONTROL_STOP = 0x00000010,
112 MVTWSI_CONTROL_START = 0x00000020,
114 MVTWSI_CONTROL_TWSIEN = 0x00000040,
115 /* Interrupt enable */
116 MVTWSI_CONTROL_INTEN = 0x00000080,
120 * On sun6i and newer, IFLG is a write-clear bit, which is cleared by writing 1;
121 * on other platforms, it is a normal r/w bit, which is cleared by writing 0.
124 #ifdef CONFIG_SUNXI_GEN_SUN6I
125 #define MVTWSI_CONTROL_CLEAR_IFLG 0x00000008
127 #define MVTWSI_CONTROL_CLEAR_IFLG 0x00000000
131 * enum mvstwsi_status_values - Possible values of I2C controller's status
134 * Only those statuses expected in normal master operation on
135 * non-10-bit-address devices are specified.
137 * Every status that's unexpected during normal operation (bus errors,
138 * arbitration losses, missing ACKs...) is passed back to the caller as an error
141 enum mvstwsi_status_values {
142 /* START condition transmitted */
143 MVTWSI_STATUS_START = 0x08,
144 /* Repeated START condition transmitted */
145 MVTWSI_STATUS_REPEATED_START = 0x10,
146 /* Address + write bit transmitted, ACK received */
147 MVTWSI_STATUS_ADDR_W_ACK = 0x18,
148 /* Data transmitted, ACK received */
149 MVTWSI_STATUS_DATA_W_ACK = 0x28,
150 /* Address + read bit transmitted, ACK received */
151 MVTWSI_STATUS_ADDR_R_ACK = 0x40,
152 /* Address + read bit transmitted, ACK not received */
153 MVTWSI_STATUS_ADDR_R_NAK = 0x48,
154 /* Data received, ACK transmitted */
155 MVTWSI_STATUS_DATA_R_ACK = 0x50,
156 /* Data received, ACK not transmitted */
157 MVTWSI_STATUS_DATA_R_NAK = 0x58,
158 /* No relevant status */
159 MVTWSI_STATUS_IDLE = 0xF8,
163 * enum mvstwsi_ack_flags - Determine whether a read byte should be
164 * acknowledged or not.
166 enum mvtwsi_ack_flags {
167 /* Send NAK after received byte */
169 /* Send ACK after received byte */
174 * calc_tick() - Calculate the duration of a clock cycle from the I2C speed
176 * @speed: The speed in Hz to calculate the clock cycle duration for.
177 * @return The duration of a clock cycle in ns.
179 inline uint calc_tick(uint speed)
181 /* One tick = the duration of a period at the specified speed in ns (we
182 * add 100 ns to be on the safe side) */
183 return (1000000000u / speed) + 100;
186 #ifndef CONFIG_DM_I2C
189 * twsi_get_base() - Get controller register base for specified adapter
191 * @adap: Adapter to get the register base for.
192 * @return Register base for the specified adapter.
194 static struct mvtwsi_registers *twsi_get_base(struct i2c_adapter *adap)
196 switch (adap->hwadapnr) {
197 #ifdef CONFIG_I2C_MVTWSI_BASE0
199 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE0;
201 #ifdef CONFIG_I2C_MVTWSI_BASE1
203 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE1;
205 #ifdef CONFIG_I2C_MVTWSI_BASE2
207 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE2;
209 #ifdef CONFIG_I2C_MVTWSI_BASE3
211 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE3;
213 #ifdef CONFIG_I2C_MVTWSI_BASE4
215 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE4;
217 #ifdef CONFIG_I2C_MVTWSI_BASE5
219 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE5;
222 printf("Missing mvtwsi controller %d base\n", adap->hwadapnr);
231 * enum mvtwsi_error_class - types of I2C errors
233 enum mvtwsi_error_class {
234 /* The controller returned a different status than expected */
235 MVTWSI_ERROR_WRONG_STATUS = 0x01,
236 /* The controller timed out */
237 MVTWSI_ERROR_TIMEOUT = 0x02,
241 * mvtwsi_error() - Build I2C return code from error information
243 * For debugging purposes, this function packs some information of an occurred
244 * error into a return code. These error codes are returned from I2C API
245 * functions (i2c_{read,write}, dm_i2c_{read,write}, etc.).
247 * @ec: The error class of the error (enum mvtwsi_error_class).
248 * @lc: The last value of the control register.
249 * @ls: The last value of the status register.
250 * @es: The expected value of the status register.
251 * @return The generated error code.
253 inline uint mvtwsi_error(uint ec, uint lc, uint ls, uint es)
255 return ((ec << 24) & 0xFF000000)
256 | ((lc << 16) & 0x00FF0000)
257 | ((ls << 8) & 0x0000FF00)
262 * twsi_wait() - Wait for I2C bus interrupt flag and check status, or time out.
264 * @return Zero if status is as expected, or a non-zero code if either a time
265 * out occurred, or the status was not the expected one.
267 static int twsi_wait(struct mvtwsi_registers *twsi, int expected_status,
274 control = readl(&twsi->control);
275 if (control & MVTWSI_CONTROL_IFLG) {
277 * On Armada 38x it seems that the controller works as
278 * if it first set the MVTWSI_CONTROL_IFLAG in the
279 * control register and only after that it changed the
281 * This sometimes caused weird bugs which only appeared
282 * on selected I2C speeds and even then only sometimes.
283 * We therefore add here a simple ndealy(100), which
284 * seems to fix this weird bug.
287 status = readl(&twsi->status);
288 if (status == expected_status)
292 MVTWSI_ERROR_WRONG_STATUS,
293 control, status, expected_status);
295 ndelay(tick); /* One clock cycle */
297 status = readl(&twsi->status);
298 return mvtwsi_error(MVTWSI_ERROR_TIMEOUT, control, status,
303 * twsi_start() - Assert a START condition on the bus.
305 * This function is used in both single I2C transactions and inside
306 * back-to-back transactions (repeated starts).
308 * @twsi: The MVTWSI register structure to use.
309 * @expected_status: The I2C bus status expected to be asserted after the
310 * operation completion.
311 * @tick: The duration of a clock cycle at the current I2C speed.
312 * @return Zero if status is as expected, or a non-zero code if either a time
313 * out occurred or the status was not the expected one.
315 static int twsi_start(struct mvtwsi_registers *twsi, int expected_status,
319 writel(MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_START |
320 MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
321 /* Wait for controller to process START */
322 return twsi_wait(twsi, expected_status, tick);
326 * twsi_send() - Send a byte on the I2C bus.
328 * The byte may be part of an address byte or data.
330 * @twsi: The MVTWSI register structure to use.
331 * @byte: The byte to send.
332 * @expected_status: The I2C bus status expected to be asserted after the
333 * operation completion.
334 * @tick: The duration of a clock cycle at the current I2C speed.
335 * @return Zero if status is as expected, or a non-zero code if either a time
336 * out occurred or the status was not the expected one.
338 static int twsi_send(struct mvtwsi_registers *twsi, u8 byte,
339 int expected_status, uint tick)
341 /* Write byte to data register for sending */
342 writel(byte, &twsi->data);
343 /* Clear any pending interrupt -- that will cause sending */
344 writel(MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_CLEAR_IFLG,
346 /* Wait for controller to receive byte, and check ACK */
347 return twsi_wait(twsi, expected_status, tick);
351 * twsi_recv() - Receive a byte on the I2C bus.
353 * The static variable mvtwsi_control_flags controls whether we ack or nak.
355 * @twsi: The MVTWSI register structure to use.
356 * @byte: The byte to send.
357 * @ack_flag: Flag that determines whether the received byte should
358 * be acknowledged by the controller or not (sent ACK/NAK).
359 * @tick: The duration of a clock cycle at the current I2C speed.
360 * @return Zero if status is as expected, or a non-zero code if either a time
361 * out occurred or the status was not the expected one.
363 static int twsi_recv(struct mvtwsi_registers *twsi, u8 *byte, int ack_flag,
366 int expected_status, status, control;
368 /* Compute expected status based on passed ACK flag */
369 expected_status = ack_flag ? MVTWSI_STATUS_DATA_R_ACK :
370 MVTWSI_STATUS_DATA_R_NAK;
371 /* Acknowledge *previous state*, and launch receive */
372 control = MVTWSI_CONTROL_TWSIEN;
373 control |= ack_flag == MVTWSI_READ_ACK ? MVTWSI_CONTROL_ACK : 0;
374 writel(control | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
375 /* Wait for controller to receive byte, and assert ACK or NAK */
376 status = twsi_wait(twsi, expected_status, tick);
377 /* If we did receive the expected byte, store it */
379 *byte = readl(&twsi->data);
384 * twsi_stop() - Assert a STOP condition on the bus.
386 * This function is also used to force the bus back to idle state (SDA =
389 * @twsi: The MVTWSI register structure to use.
390 * @tick: The duration of a clock cycle at the current I2C speed.
391 * @return Zero if the operation succeeded, or a non-zero code if a time out
394 static int twsi_stop(struct mvtwsi_registers *twsi, uint tick)
396 int control, stop_status;
401 control = MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_STOP;
402 writel(control | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
403 /* Wait for IDLE; IFLG won't rise, so we can't use twsi_wait() */
405 stop_status = readl(&twsi->status);
406 if (stop_status == MVTWSI_STATUS_IDLE)
408 ndelay(tick); /* One clock cycle */
410 control = readl(&twsi->control);
411 if (stop_status != MVTWSI_STATUS_IDLE)
412 status = mvtwsi_error(MVTWSI_ERROR_TIMEOUT,
413 control, status, MVTWSI_STATUS_IDLE);
418 * twsi_calc_freq() - Compute I2C frequency depending on m and n parameters.
420 * @n: Parameter 'n' for the frequency calculation algorithm.
421 * @m: Parameter 'm' for the frequency calculation algorithm.
422 * @return The I2C frequency corresponding to the passed m and n parameters.
424 static uint twsi_calc_freq(const int n, const int m)
426 #ifdef CONFIG_ARCH_SUNXI
427 return CONFIG_SYS_TCLK / (10 * (m + 1) * (1 << n));
429 return CONFIG_SYS_TCLK / (10 * (m + 1) * (2 << n));
434 * twsi_reset() - Reset the I2C controller.
436 * Resetting the controller also resets the baud rate and slave address, hence
437 * they must be re-established after the reset.
439 * @twsi: The MVTWSI register structure to use.
441 static void twsi_reset(struct mvtwsi_registers *twsi)
443 /* Reset controller */
444 writel(0, &twsi->soft_reset);
445 /* Wait 2 ms -- this is what the Marvell LSP does */
450 * __twsi_i2c_set_bus_speed() - Set the speed of the I2C controller.
452 * This function sets baud rate to the highest possible value that does not
453 * exceed the requested rate.
455 * @twsi: The MVTWSI register structure to use.
456 * @requested_speed: The desired frequency the controller should run at
458 * @return The actual frequency the controller was configured to.
460 static uint __twsi_i2c_set_bus_speed(struct mvtwsi_registers *twsi,
461 uint requested_speed)
463 uint tmp_speed, highest_speed, n, m;
464 uint baud = 0x44; /* Baud rate after controller reset */
467 /* Successively try m, n combinations, and use the combination
468 * resulting in the largest speed that's not above the requested
470 for (n = 0; n < 8; n++) {
471 for (m = 0; m < 16; m++) {
472 tmp_speed = twsi_calc_freq(n, m);
473 if ((tmp_speed <= requested_speed) &&
474 (tmp_speed > highest_speed)) {
475 highest_speed = tmp_speed;
480 writel(baud, &twsi->baudrate);
482 /* Wait for controller for one tick */
484 ndelay(calc_tick(highest_speed));
488 return highest_speed;
492 * __twsi_i2c_init() - Initialize the I2C controller.
494 * @twsi: The MVTWSI register structure to use.
495 * @speed: The initial frequency the controller should run at
497 * @slaveadd: The I2C address to be set for the I2C master.
498 * @actual_speed: A output parameter that receives the actual frequency
499 * in Hz the controller was set to by the function.
500 * @return Zero if the operation succeeded, or a non-zero code if a time out
503 static void __twsi_i2c_init(struct mvtwsi_registers *twsi, int speed,
504 int slaveadd, uint *actual_speed)
508 /* Reset controller */
511 tmp_speed = __twsi_i2c_set_bus_speed(twsi, speed);
513 *actual_speed = tmp_speed;
514 /* Set slave address; even though we don't use it */
515 writel(slaveadd, &twsi->slave_address);
516 writel(0, &twsi->xtnd_slave_addr);
517 /* Assert STOP, but don't care for the result */
519 (void) twsi_stop(twsi, calc_tick(*actual_speed));
521 (void) twsi_stop(twsi, 10000);
526 * i2c_begin() - Start a I2C transaction.
528 * Begin a I2C transaction with a given expected start status and chip address.
529 * A START is asserted, and the address byte is sent to the I2C controller. The
530 * expected address status will be derived from the direction bit (bit 0) of
533 * @twsi: The MVTWSI register structure to use.
534 * @expected_start_status: The I2C status the controller is expected to
535 * assert after the address byte was sent.
536 * @addr: The address byte to be sent.
537 * @tick: The duration of a clock cycle at the current
539 * @return Zero if the operation succeeded, or a non-zero code if a time out or
540 * unexpected I2C status occurred.
542 static int i2c_begin(struct mvtwsi_registers *twsi, int expected_start_status,
545 int status, expected_addr_status;
547 /* Compute the expected address status from the direction bit in
548 * the address byte */
549 if (addr & 1) /* Reading */
550 expected_addr_status = MVTWSI_STATUS_ADDR_R_ACK;
552 expected_addr_status = MVTWSI_STATUS_ADDR_W_ACK;
554 status = twsi_start(twsi, expected_start_status, tick);
555 /* Send out the address if the start went well */
557 status = twsi_send(twsi, addr, expected_addr_status, tick);
558 /* Return 0, or the status of the first failure */
563 * __twsi_i2c_probe_chip() - Probe the given I2C chip address.
565 * This function begins a I2C read transaction, does a dummy read and NAKs; if
566 * the procedure succeeds, the chip is considered to be present.
568 * @twsi: The MVTWSI register structure to use.
569 * @chip: The chip address to probe.
570 * @tick: The duration of a clock cycle at the current I2C speed.
571 * @return Zero if the operation succeeded, or a non-zero code if a time out or
572 * unexpected I2C status occurred.
574 static int __twsi_i2c_probe_chip(struct mvtwsi_registers *twsi, uchar chip,
581 status = i2c_begin(twsi, MVTWSI_STATUS_START, (chip << 1) | 1, tick);
582 /* Dummy read was accepted: receive byte, but NAK it. */
584 status = twsi_recv(twsi, &dummy_byte, MVTWSI_READ_NAK, tick);
585 /* Stop transaction */
586 twsi_stop(twsi, tick);
587 /* Return 0, or the status of the first failure */
592 * __twsi_i2c_read() - Read data from a I2C chip.
594 * This function begins a I2C write transaction, and transmits the address
595 * bytes; then begins a I2C read transaction, and receives the data bytes.
597 * NOTE: Some devices want a stop right before the second start, while some
598 * will choke if it is there. Since deciding this is not yet supported in
599 * higher level APIs, we need to make a decision here, and for the moment that
600 * will be a repeated start without a preceding stop.
602 * @twsi: The MVTWSI register structure to use.
603 * @chip: The chip address to read from.
604 * @addr: The address bytes to send.
605 * @alen: The length of the address bytes in bytes.
606 * @data: The buffer to receive the data read from the chip (has to have
607 * a size of at least 'length' bytes).
608 * @length: The amount of data to be read from the chip in bytes.
609 * @tick: The duration of a clock cycle at the current I2C speed.
610 * @return Zero if the operation succeeded, or a non-zero code if a time out or
611 * unexpected I2C status occurred.
613 static int __twsi_i2c_read(struct mvtwsi_registers *twsi, uchar chip,
614 u8 *addr, int alen, uchar *data, int length,
619 int expected_start = MVTWSI_STATUS_START;
622 /* Begin i2c write to send the address bytes */
623 status = i2c_begin(twsi, expected_start, (chip << 1), tick);
624 /* Send address bytes */
625 while ((status == 0) && alen--)
626 status = twsi_send(twsi, addr[alen],
627 MVTWSI_STATUS_DATA_W_ACK, tick);
628 /* Send repeated STARTs after the initial START */
629 expected_start = MVTWSI_STATUS_REPEATED_START;
631 /* Begin i2c read to receive data bytes */
633 status = i2c_begin(twsi, expected_start, (chip << 1) | 1, tick);
634 /* Receive actual data bytes; set NAK if we if we have nothing more to
636 while ((status == 0) && length--)
637 status = twsi_recv(twsi, data++,
639 MVTWSI_READ_ACK : MVTWSI_READ_NAK, tick);
640 /* Stop transaction */
641 stop_status = twsi_stop(twsi, tick);
642 /* Return 0, or the status of the first failure */
643 return status != 0 ? status : stop_status;
647 * __twsi_i2c_write() - Send data to a I2C chip.
649 * This function begins a I2C write transaction, and transmits the address
650 * bytes; then begins a new I2C write transaction, and sends the data bytes.
652 * @twsi: The MVTWSI register structure to use.
653 * @chip: The chip address to read from.
654 * @addr: The address bytes to send.
655 * @alen: The length of the address bytes in bytes.
656 * @data: The buffer containing the data to be sent to the chip.
657 * @length: The length of data to be sent to the chip in bytes.
658 * @tick: The duration of a clock cycle at the current I2C speed.
659 * @return Zero if the operation succeeded, or a non-zero code if a time out or
660 * unexpected I2C status occurred.
662 static int __twsi_i2c_write(struct mvtwsi_registers *twsi, uchar chip,
663 u8 *addr, int alen, uchar *data, int length,
666 int status, stop_status;
668 /* Begin i2c write to send first the address bytes, then the
670 status = i2c_begin(twsi, MVTWSI_STATUS_START, (chip << 1), tick);
671 /* Send address bytes */
672 while ((status == 0) && (alen-- > 0))
673 status = twsi_send(twsi, addr[alen], MVTWSI_STATUS_DATA_W_ACK,
675 /* Send data bytes */
676 while ((status == 0) && (length-- > 0))
677 status = twsi_send(twsi, *(data++), MVTWSI_STATUS_DATA_W_ACK,
679 /* Stop transaction */
680 stop_status = twsi_stop(twsi, tick);
681 /* Return 0, or the status of the first failure */
682 return status != 0 ? status : stop_status;
685 #ifndef CONFIG_DM_I2C
686 static void twsi_i2c_init(struct i2c_adapter *adap, int speed,
689 struct mvtwsi_registers *twsi = twsi_get_base(adap);
690 __twsi_i2c_init(twsi, speed, slaveadd, NULL);
693 static uint twsi_i2c_set_bus_speed(struct i2c_adapter *adap,
694 uint requested_speed)
696 struct mvtwsi_registers *twsi = twsi_get_base(adap);
697 __twsi_i2c_set_bus_speed(twsi, requested_speed);
701 static int twsi_i2c_probe(struct i2c_adapter *adap, uchar chip)
703 struct mvtwsi_registers *twsi = twsi_get_base(adap);
704 return __twsi_i2c_probe_chip(twsi, chip, 10000);
707 static int twsi_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
708 int alen, uchar *data, int length)
710 struct mvtwsi_registers *twsi = twsi_get_base(adap);
713 addr_bytes[0] = (addr >> 0) & 0xFF;
714 addr_bytes[1] = (addr >> 8) & 0xFF;
715 addr_bytes[2] = (addr >> 16) & 0xFF;
716 addr_bytes[3] = (addr >> 24) & 0xFF;
718 return __twsi_i2c_read(twsi, chip, addr_bytes, alen, data, length,
722 static int twsi_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
723 int alen, uchar *data, int length)
725 struct mvtwsi_registers *twsi = twsi_get_base(adap);
728 addr_bytes[0] = (addr >> 0) & 0xFF;
729 addr_bytes[1] = (addr >> 8) & 0xFF;
730 addr_bytes[2] = (addr >> 16) & 0xFF;
731 addr_bytes[3] = (addr >> 24) & 0xFF;
733 return __twsi_i2c_write(twsi, chip, addr_bytes, alen, data, length,
737 #ifdef CONFIG_I2C_MVTWSI_BASE0
738 U_BOOT_I2C_ADAP_COMPLETE(twsi0, twsi_i2c_init, twsi_i2c_probe,
739 twsi_i2c_read, twsi_i2c_write,
740 twsi_i2c_set_bus_speed,
741 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0)
743 #ifdef CONFIG_I2C_MVTWSI_BASE1
744 U_BOOT_I2C_ADAP_COMPLETE(twsi1, twsi_i2c_init, twsi_i2c_probe,
745 twsi_i2c_read, twsi_i2c_write,
746 twsi_i2c_set_bus_speed,
747 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 1)
750 #ifdef CONFIG_I2C_MVTWSI_BASE2
751 U_BOOT_I2C_ADAP_COMPLETE(twsi2, twsi_i2c_init, twsi_i2c_probe,
752 twsi_i2c_read, twsi_i2c_write,
753 twsi_i2c_set_bus_speed,
754 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 2)
757 #ifdef CONFIG_I2C_MVTWSI_BASE3
758 U_BOOT_I2C_ADAP_COMPLETE(twsi3, twsi_i2c_init, twsi_i2c_probe,
759 twsi_i2c_read, twsi_i2c_write,
760 twsi_i2c_set_bus_speed,
761 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 3)
764 #ifdef CONFIG_I2C_MVTWSI_BASE4
765 U_BOOT_I2C_ADAP_COMPLETE(twsi4, twsi_i2c_init, twsi_i2c_probe,
766 twsi_i2c_read, twsi_i2c_write,
767 twsi_i2c_set_bus_speed,
768 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 4)
771 #ifdef CONFIG_I2C_MVTWSI_BASE5
772 U_BOOT_I2C_ADAP_COMPLETE(twsi5, twsi_i2c_init, twsi_i2c_probe,
773 twsi_i2c_read, twsi_i2c_write,
774 twsi_i2c_set_bus_speed,
775 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 5)
778 #else /* CONFIG_DM_I2C */
780 static int mvtwsi_i2c_probe_chip(struct udevice *bus, u32 chip_addr,
783 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
784 return __twsi_i2c_probe_chip(dev->base, chip_addr, dev->tick);
787 static int mvtwsi_i2c_set_bus_speed(struct udevice *bus, uint speed)
789 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
791 dev->speed = __twsi_i2c_set_bus_speed(dev->base, speed);
792 dev->tick = calc_tick(dev->speed);
797 static int mvtwsi_i2c_of_to_plat(struct udevice *bus)
799 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
801 dev->base = dev_read_addr_ptr(bus);
806 dev->index = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus),
808 dev->slaveadd = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus),
809 "u-boot,i2c-slave-addr", 0x0);
810 dev->speed = dev_read_u32_default(bus, "clock-frequency",
811 I2C_SPEED_STANDARD_RATE);
816 static void twsi_disable_i2c_slave(struct mvtwsi_registers *twsi)
818 clrbits_le32(&twsi->debug, BIT(18));
821 static int mvtwsi_i2c_bind(struct udevice *bus)
823 struct mvtwsi_registers *twsi = dev_read_addr_ptr(bus);
825 /* Disable the hidden slave in i2c0 of these platforms */
826 if ((IS_ENABLED(CONFIG_ARMADA_38X) ||
827 IS_ENABLED(CONFIG_ARCH_KIRKWOOD) ||
828 IS_ENABLED(CONFIG_ARMADA_8K)) && !dev_seq(bus))
829 twsi_disable_i2c_slave(twsi);
834 static int mvtwsi_i2c_probe(struct udevice *bus)
836 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
839 __twsi_i2c_init(dev->base, dev->speed, dev->slaveadd, &actual_speed);
840 dev->speed = actual_speed;
841 dev->tick = calc_tick(dev->speed);
845 static int mvtwsi_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
847 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
848 struct i2c_msg *dmsg, *omsg, dummy;
850 memset(&dummy, 0, sizeof(struct i2c_msg));
852 /* We expect either two messages (one with an offset and one with the
853 * actual data) or one message (just data or offset/data combined) */
854 if (nmsgs > 2 || nmsgs == 0) {
855 debug("%s: Only one or two messages are supported.", __func__);
859 omsg = nmsgs == 1 ? &dummy : msg;
860 dmsg = nmsgs == 1 ? msg : msg + 1;
862 if (dmsg->flags & I2C_M_RD)
863 return __twsi_i2c_read(dev->base, dmsg->addr, omsg->buf,
864 omsg->len, dmsg->buf, dmsg->len,
867 return __twsi_i2c_write(dev->base, dmsg->addr, omsg->buf,
868 omsg->len, dmsg->buf, dmsg->len,
872 static const struct dm_i2c_ops mvtwsi_i2c_ops = {
873 .xfer = mvtwsi_i2c_xfer,
874 .probe_chip = mvtwsi_i2c_probe_chip,
875 .set_bus_speed = mvtwsi_i2c_set_bus_speed,
878 static const struct udevice_id mvtwsi_i2c_ids[] = {
879 { .compatible = "marvell,mv64xxx-i2c", },
880 { .compatible = "marvell,mv78230-i2c", },
881 { .compatible = "allwinner,sun6i-a31-i2c", },
885 U_BOOT_DRIVER(i2c_mvtwsi) = {
886 .name = "i2c_mvtwsi",
888 .of_match = mvtwsi_i2c_ids,
889 .bind = mvtwsi_i2c_bind,
890 .probe = mvtwsi_i2c_probe,
891 .of_to_plat = mvtwsi_i2c_of_to_plat,
892 .priv_auto = sizeof(struct mvtwsi_i2c_dev),
893 .ops = &mvtwsi_i2c_ops,
895 #endif /* CONFIG_DM_I2C */