1 // SPDX-License-Identifier: GPL-2.0+
3 * Driver for the TWSI (i2c) controller found on the Marvell
4 * orion5x and kirkwood SoC families.
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)
25 DECLARE_GLOBAL_DATA_PTR;
28 * Include a file that will provide CONFIG_I2C_MVTWSI_BASE*, and possibly other
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>
40 #error Driver mvtwsi not supported by SoC or board
42 #endif /* CONFIG_DM_I2C */
45 * On SUNXI, we get CFG_SYS_TCLK from this include, so we want to
48 #if CONFIG_IS_ENABLED(DM_I2C) && defined(CONFIG_ARCH_SUNXI)
49 #include <asm/arch/i2c.h>
53 * TWSI register structure
56 #ifdef CONFIG_ARCH_SUNXI
58 struct mvtwsi_registers {
66 u32 debug; /* Dummy field for build compatibility with mvebu */
71 struct mvtwsi_registers {
76 u32 status; /* When reading */
77 u32 baudrate; /* When writing */
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) */
94 /* The I2C slave address for the device */
96 /* The configured I2C speed in Hz */
98 /* The current length of a clock period (depending on speed) */
101 #endif /* CONFIG_DM_I2C */
104 * enum mvtwsi_ctrl_register_fields - Bit masks for flags in the control
107 enum mvtwsi_ctrl_register_fields {
108 /* Acknowledge bit */
109 MVTWSI_CONTROL_ACK = 0x00000004,
111 MVTWSI_CONTROL_IFLG = 0x00000008,
113 MVTWSI_CONTROL_STOP = 0x00000010,
115 MVTWSI_CONTROL_START = 0x00000020,
117 MVTWSI_CONTROL_TWSIEN = 0x00000040,
118 /* Interrupt enable */
119 MVTWSI_CONTROL_INTEN = 0x00000080,
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.
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
131 #define MVTWSI_CONTROL_CLEAR_IFLG 0x00000000
135 * enum mvstwsi_status_values - Possible values of I2C controller's status
138 * Only those statuses expected in normal master operation on
139 * non-10-bit-address devices are specified.
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
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,
169 * enum mvstwsi_ack_flags - Determine whether a read byte should be
170 * acknowledged or not.
172 enum mvtwsi_ack_flags {
173 /* Send NAK after received byte */
175 /* Send ACK after received byte */
180 * calc_tick() - Calculate the duration of a clock cycle from the I2C speed
182 * @speed: The speed in Hz to calculate the clock cycle duration for.
183 * Return: The duration of a clock cycle in ns.
185 inline uint calc_tick(uint speed)
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;
192 #if !CONFIG_IS_ENABLED(DM_I2C)
195 * twsi_get_base() - Get controller register base for specified adapter
197 * @adap: Adapter to get the register base for.
198 * Return: Register base for the specified adapter.
200 static struct mvtwsi_registers *twsi_get_base(struct i2c_adapter *adap)
202 switch (adap->hwadapnr) {
203 #ifdef CFG_I2C_MVTWSI_BASE0
205 return (struct mvtwsi_registers *)CFG_I2C_MVTWSI_BASE0;
207 #ifdef CFG_I2C_MVTWSI_BASE1
209 return (struct mvtwsi_registers *)CFG_I2C_MVTWSI_BASE1;
211 #ifdef CFG_I2C_MVTWSI_BASE2
213 return (struct mvtwsi_registers *)CFG_I2C_MVTWSI_BASE2;
215 #ifdef CONFIG_I2C_MVTWSI_BASE3
217 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE3;
219 #ifdef CONFIG_I2C_MVTWSI_BASE4
221 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE4;
223 #ifdef CONFIG_I2C_MVTWSI_BASE5
225 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE5;
228 printf("Missing mvtwsi controller %d base\n", adap->hwadapnr);
237 * enum mvtwsi_error_class - types of I2C errors
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,
247 * mvtwsi_error() - Build I2C return code from error information
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.).
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.
259 inline uint mvtwsi_error(uint ec, uint lc, uint ls, uint es)
261 return ((ec << 24) & 0xFF000000)
262 | ((lc << 16) & 0x00FF0000)
263 | ((ls << 8) & 0x0000FF00)
268 * twsi_wait() - Wait for I2C bus interrupt flag and check status, or time out.
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.
273 static int twsi_wait(struct mvtwsi_registers *twsi, int expected_status,
280 control = readl(&twsi->control);
281 if (control & MVTWSI_CONTROL_IFLG) {
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
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.
293 status = readl(&twsi->status);
294 if (status == expected_status)
298 MVTWSI_ERROR_WRONG_STATUS,
299 control, status, expected_status);
301 ndelay(tick); /* One clock cycle */
303 status = readl(&twsi->status);
304 return mvtwsi_error(MVTWSI_ERROR_TIMEOUT, control, status,
309 * twsi_start() - Assert a START condition on the bus.
311 * This function is used in both single I2C transactions and inside
312 * back-to-back transactions (repeated starts).
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.
321 static int twsi_start(struct mvtwsi_registers *twsi, int expected_status,
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);
332 * twsi_send() - Send a byte on the I2C bus.
334 * The byte may be part of an address byte or data.
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.
344 static int twsi_send(struct mvtwsi_registers *twsi, u8 byte,
345 int expected_status, uint tick)
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,
352 /* Wait for controller to receive byte, and check ACK */
353 return twsi_wait(twsi, expected_status, tick);
357 * twsi_recv() - Receive a byte on the I2C bus.
359 * The static variable mvtwsi_control_flags controls whether we ack or nak.
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.
369 static int twsi_recv(struct mvtwsi_registers *twsi, u8 *byte, int ack_flag,
372 int expected_status, status, control;
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 */
385 *byte = readl(&twsi->data);
390 * twsi_stop() - Assert a STOP condition on the bus.
392 * This function is also used to force the bus back to idle state (SDA =
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
400 static int twsi_stop(struct mvtwsi_registers *twsi, uint tick)
402 int control, stop_status;
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() */
411 stop_status = readl(&twsi->status);
412 if (stop_status == MVTWSI_STATUS_IDLE)
414 ndelay(tick); /* One clock cycle */
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);
424 * twsi_calc_freq() - Compute I2C frequency depending on m and n parameters.
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.
430 static uint twsi_calc_freq(const int n, const int m)
432 #ifdef CONFIG_ARCH_SUNXI
433 return CFG_SYS_TCLK / (10 * (m + 1) * (1 << n));
435 return CFG_SYS_TCLK / (10 * (m + 1) * (2 << n));
440 * twsi_reset() - Reset the I2C controller.
442 * Resetting the controller also resets the baud rate and slave address, hence
443 * they must be re-established after the reset.
445 * @twsi: The MVTWSI register structure to use.
447 static void twsi_reset(struct mvtwsi_registers *twsi)
449 /* Reset controller */
450 writel(0, &twsi->soft_reset);
451 /* Wait 2 ms -- this is what the Marvell LSP does */
456 * __twsi_i2c_set_bus_speed() - Set the speed of the I2C controller.
458 * This function sets baud rate to the highest possible value that does not
459 * exceed the requested rate.
461 * @twsi: The MVTWSI register structure to use.
462 * @requested_speed: The desired frequency the controller should run at
464 * Return: The actual frequency the controller was configured to.
466 static uint __twsi_i2c_set_bus_speed(struct mvtwsi_registers *twsi,
467 uint requested_speed)
469 uint tmp_speed, highest_speed, n, m;
470 uint baud = 0x44; /* Baud rate after controller reset */
473 /* Successively try m, n combinations, and use the combination
474 * resulting in the largest speed that's not above the requested
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;
486 writel(baud, &twsi->baudrate);
488 /* Wait for controller for one tick */
489 #if CONFIG_IS_ENABLED(DM_I2C)
490 ndelay(calc_tick(highest_speed));
494 return highest_speed;
498 * __twsi_i2c_init() - Initialize the I2C controller.
500 * @twsi: The MVTWSI register structure to use.
501 * @speed: The initial frequency the controller should run at
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
509 static void __twsi_i2c_init(struct mvtwsi_registers *twsi, int speed,
510 int slaveadd, uint *actual_speed)
514 /* Reset controller */
517 tmp_speed = __twsi_i2c_set_bus_speed(twsi, 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));
527 (void) twsi_stop(twsi, 10000);
532 * __twsi_i2c_reinit() - Reset and reinitialize the I2C controller.
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.
537 * @twsi: The MVTWSI register structure to use.
538 * @tick: The duration of a clock cycle at the current I2C speed.
540 static void __twsi_i2c_reinit(struct mvtwsi_registers *twsi, uint tick)
545 /* Save baud, address registers */
546 baud = readl(&twsi->baudrate);
547 slaveadd = readl(&twsi->slave_address);
549 /* Reset controller */
552 /* Restore baud, address registers */
553 writel(baud, &twsi->baudrate);
554 writel(slaveadd, &twsi->slave_address);
555 writel(0, &twsi->xtnd_slave_addr);
557 /* Assert STOP, but don't care for the result */
558 (void) twsi_stop(twsi, tick);
562 * i2c_begin() - Start a I2C transaction.
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
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
575 * Return: Zero if the operation succeeded, or a non-zero code if a time out or
576 * unexpected I2C status occurred.
578 static int i2c_begin(struct mvtwsi_registers *twsi, int expected_start_status,
581 int status, expected_addr_status;
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;
588 expected_addr_status = MVTWSI_STATUS_ADDR_W_ACK;
590 status = twsi_start(twsi, expected_start_status, tick);
591 /* Send out the address if the start went well */
593 status = twsi_send(twsi, addr, expected_addr_status, tick);
594 /* Return 0, or the status of the first failure */
599 * __twsi_i2c_probe_chip() - Probe the given I2C chip address.
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.
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.
610 static int __twsi_i2c_probe_chip(struct mvtwsi_registers *twsi, uchar chip,
617 status = i2c_begin(twsi, MVTWSI_STATUS_START, (chip << 1) | 1, tick);
618 /* Dummy read was accepted: receive byte, but NAK it. */
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 */
628 * __twsi_i2c_read() - Read data from a I2C chip.
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.
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.
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.
649 static int __twsi_i2c_read(struct mvtwsi_registers *twsi, uchar chip,
650 u8 *addr, int alen, uchar *data, int length,
655 int expected_start = MVTWSI_STATUS_START;
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);
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;
672 /* Begin i2c read to receive data bytes */
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
677 while ((status == 0) && length--)
678 status = twsi_recv(twsi, data++,
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;
688 * __twsi_i2c_write() - Send data to a I2C chip.
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.
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.
703 static int __twsi_i2c_write(struct mvtwsi_registers *twsi, uchar chip,
704 u8 *addr, int alen, uchar *data, int length,
707 int status, stop_status;
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);
714 /* Begin i2c write to send first the address bytes, then the
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,
721 /* Send data bytes */
722 while ((status == 0) && (length-- > 0))
723 status = twsi_send(twsi, *(data++), MVTWSI_STATUS_DATA_W_ACK,
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;
731 #if !CONFIG_IS_ENABLED(DM_I2C)
732 static void twsi_i2c_init(struct i2c_adapter *adap, int speed,
735 struct mvtwsi_registers *twsi = twsi_get_base(adap);
736 __twsi_i2c_init(twsi, speed, slaveadd, NULL);
739 static uint twsi_i2c_set_bus_speed(struct i2c_adapter *adap,
740 uint requested_speed)
742 struct mvtwsi_registers *twsi = twsi_get_base(adap);
743 __twsi_i2c_set_bus_speed(twsi, requested_speed);
747 static int twsi_i2c_probe(struct i2c_adapter *adap, uchar chip)
749 struct mvtwsi_registers *twsi = twsi_get_base(adap);
750 return __twsi_i2c_probe_chip(twsi, chip, 10000);
753 static int twsi_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
754 int alen, uchar *data, int length)
756 struct mvtwsi_registers *twsi = twsi_get_base(adap);
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;
764 return __twsi_i2c_read(twsi, chip, addr_bytes, alen, data, length,
768 static int twsi_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
769 int alen, uchar *data, int length)
771 struct mvtwsi_registers *twsi = twsi_get_base(adap);
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;
779 return __twsi_i2c_write(twsi, chip, addr_bytes, alen, data, length,
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)
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)
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)
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)
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)
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)
824 #else /* CONFIG_DM_I2C */
826 static int mvtwsi_i2c_probe_chip(struct udevice *bus, u32 chip_addr,
829 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
830 return __twsi_i2c_probe_chip(dev->base, chip_addr, dev->tick);
833 static int mvtwsi_i2c_set_bus_speed(struct udevice *bus, uint speed)
835 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
837 dev->speed = __twsi_i2c_set_bus_speed(dev->base, speed);
838 dev->tick = calc_tick(dev->speed);
843 static int mvtwsi_i2c_of_to_plat(struct udevice *bus)
845 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
847 dev->base = dev_read_addr_ptr(bus);
852 dev->index = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus),
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);
862 static void twsi_disable_i2c_slave(struct mvtwsi_registers *twsi)
864 clrbits_le32(&twsi->debug, BIT(18));
867 static int mvtwsi_i2c_bind(struct udevice *bus)
869 struct mvtwsi_registers *twsi = dev_read_addr_ptr(bus);
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);
880 static int mvtwsi_i2c_probe(struct udevice *bus)
882 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
883 struct reset_ctl reset;
888 ret = reset_get_by_index(bus, 0, &reset);
890 reset_deassert(&reset);
892 ret = clk_get_by_index(bus, 0, &clk);
896 __twsi_i2c_init(dev->base, dev->speed, dev->slaveadd, &actual_speed);
897 dev->speed = actual_speed;
898 dev->tick = calc_tick(dev->speed);
902 static int mvtwsi_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
904 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
905 struct i2c_msg *dmsg, *omsg, dummy;
910 memset(&dummy, 0, sizeof(struct i2c_msg));
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__);
919 omsg = nmsgs == 1 ? &dummy : msg;
920 dmsg = nmsgs == 1 ? msg : msg + 1;
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];
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,
932 return __twsi_i2c_write(dev->base, dmsg->addr, addr_buf,
933 omsg->len, dmsg->buf, dmsg->len,
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,
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", },
951 U_BOOT_DRIVER(i2c_mvtwsi) = {
952 .name = "i2c_mvtwsi",
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,
961 #endif /* CONFIG_DM_I2C */