common: Drop linux/delay.h from common header
[oweals/u-boot.git] / drivers / i2c / mvtwsi.c
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 <common.h>
11 #include <i2c.h>
12 #include <log.h>
13 #include <linux/delay.h>
14 #include <linux/errno.h>
15 #include <asm/io.h>
16 #include <linux/bitops.h>
17 #include <linux/compat.h>
18 #ifdef CONFIG_DM_I2C
19 #include <dm.h>
20 #endif
21
22 DECLARE_GLOBAL_DATA_PTR;
23
24 /*
25  * Include a file that will provide CONFIG_I2C_MVTWSI_BASE*, and possibly other
26  * settings
27  */
28
29 #ifndef CONFIG_DM_I2C
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>
36 #else
37 #error Driver mvtwsi not supported by SoC or board
38 #endif
39 #endif /* CONFIG_DM_I2C */
40
41 /*
42  * On SUNXI, we get CONFIG_SYS_TCLK from this include, so we want to
43  * always have it.
44  */
45 #if defined(CONFIG_DM_I2C) && defined(CONFIG_ARCH_SUNXI)
46 #include <asm/arch/i2c.h>
47 #endif
48
49 /*
50  * TWSI register structure
51  */
52
53 #ifdef CONFIG_ARCH_SUNXI
54
55 struct  mvtwsi_registers {
56         u32 slave_address;
57         u32 xtnd_slave_addr;
58         u32 data;
59         u32 control;
60         u32 status;
61         u32 baudrate;
62         u32 soft_reset;
63         u32 debug; /* Dummy field for build compatibility with mvebu */
64 };
65
66 #else
67
68 struct  mvtwsi_registers {
69         u32 slave_address;
70         u32 data;
71         u32 control;
72         union {
73                 u32 status;     /* When reading */
74                 u32 baudrate;   /* When writing */
75         };
76         u32 xtnd_slave_addr;
77         u32 reserved0[2];
78         u32 soft_reset;
79         u32 reserved1[27];
80         u32 debug;
81 };
82
83 #endif
84
85 #ifdef CONFIG_DM_I2C
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) */
90         int index;
91         /* The I2C slave address for the device */
92         u8 slaveadd;
93         /* The configured I2C speed in Hz */
94         uint speed;
95         /* The current length of a clock period (depending on speed) */
96         uint tick;
97 };
98 #endif /* CONFIG_DM_I2C */
99
100 /*
101  * enum mvtwsi_ctrl_register_fields - Bit masks for flags in the control
102  * register
103  */
104 enum mvtwsi_ctrl_register_fields {
105         /* Acknowledge bit */
106         MVTWSI_CONTROL_ACK      = 0x00000004,
107         /* Interrupt flag */
108         MVTWSI_CONTROL_IFLG     = 0x00000008,
109         /* Stop bit */
110         MVTWSI_CONTROL_STOP     = 0x00000010,
111         /* Start bit */
112         MVTWSI_CONTROL_START    = 0x00000020,
113         /* I2C enable */
114         MVTWSI_CONTROL_TWSIEN   = 0x00000040,
115         /* Interrupt enable */
116         MVTWSI_CONTROL_INTEN    = 0x00000080,
117 };
118
119 /*
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.
122  */
123
124 #ifdef CONFIG_SUNXI_GEN_SUN6I
125 #define MVTWSI_CONTROL_CLEAR_IFLG       0x00000008
126 #else
127 #define MVTWSI_CONTROL_CLEAR_IFLG       0x00000000
128 #endif
129
130 /*
131  * enum mvstwsi_status_values - Possible values of I2C controller's status
132  * register
133  *
134  * Only those statuses expected in normal master operation on
135  * non-10-bit-address devices are specified.
136  *
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
139  * code.
140  */
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,
160 };
161
162 /*
163  * enum mvstwsi_ack_flags - Determine whether a read byte should be
164  * acknowledged or not.
165  */
166 enum mvtwsi_ack_flags {
167         /* Send NAK after received byte */
168         MVTWSI_READ_NAK = 0,
169         /* Send ACK after received byte */
170         MVTWSI_READ_ACK = 1,
171 };
172
173 /*
174  * calc_tick() - Calculate the duration of a clock cycle from the I2C speed
175  *
176  * @speed:      The speed in Hz to calculate the clock cycle duration for.
177  * @return The duration of a clock cycle in ns.
178  */
179 inline uint calc_tick(uint speed)
180 {
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;
184 }
185
186 #ifndef CONFIG_DM_I2C
187
188 /*
189  * twsi_get_base() - Get controller register base for specified adapter
190  *
191  * @adap:       Adapter to get the register base for.
192  * @return Register base for the specified adapter.
193  */
194 static struct mvtwsi_registers *twsi_get_base(struct i2c_adapter *adap)
195 {
196         switch (adap->hwadapnr) {
197 #ifdef CONFIG_I2C_MVTWSI_BASE0
198         case 0:
199                 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE0;
200 #endif
201 #ifdef CONFIG_I2C_MVTWSI_BASE1
202         case 1:
203                 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE1;
204 #endif
205 #ifdef CONFIG_I2C_MVTWSI_BASE2
206         case 2:
207                 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE2;
208 #endif
209 #ifdef CONFIG_I2C_MVTWSI_BASE3
210         case 3:
211                 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE3;
212 #endif
213 #ifdef CONFIG_I2C_MVTWSI_BASE4
214         case 4:
215                 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE4;
216 #endif
217 #ifdef CONFIG_I2C_MVTWSI_BASE5
218         case 5:
219                 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE5;
220 #endif
221         default:
222                 printf("Missing mvtwsi controller %d base\n", adap->hwadapnr);
223                 break;
224         }
225
226         return NULL;
227 }
228 #endif
229
230 /*
231  * enum mvtwsi_error_class - types of I2C errors
232  */
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,
238 };
239
240 /*
241  * mvtwsi_error() - Build I2C return code from error information
242  *
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.).
246  *
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.
252  */
253 inline uint mvtwsi_error(uint ec, uint lc, uint ls, uint es)
254 {
255         return ((ec << 24) & 0xFF000000)
256                | ((lc << 16) & 0x00FF0000)
257                | ((ls << 8) & 0x0000FF00)
258                | (es & 0xFF);
259 }
260
261 /*
262  * twsi_wait() - Wait for I2C bus interrupt flag and check status, or time out.
263  *
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.
266  */
267 static int twsi_wait(struct mvtwsi_registers *twsi, int expected_status,
268                      uint tick)
269 {
270         int control, status;
271         int timeout = 1000;
272
273         do {
274                 control = readl(&twsi->control);
275                 if (control & MVTWSI_CONTROL_IFLG) {
276                         /*
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
280                          * status register.
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.
285                          */
286                         ndelay(100);
287                         status = readl(&twsi->status);
288                         if (status == expected_status)
289                                 return 0;
290                         else
291                                 return mvtwsi_error(
292                                         MVTWSI_ERROR_WRONG_STATUS,
293                                         control, status, expected_status);
294                 }
295                 ndelay(tick); /* One clock cycle */
296         } while (timeout--);
297         status = readl(&twsi->status);
298         return mvtwsi_error(MVTWSI_ERROR_TIMEOUT, control, status,
299                             expected_status);
300 }
301
302 /*
303  * twsi_start() - Assert a START condition on the bus.
304  *
305  * This function is used in both single I2C transactions and inside
306  * back-to-back transactions (repeated starts).
307  *
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.
314  */
315 static int twsi_start(struct mvtwsi_registers *twsi, int expected_status,
316                       uint tick)
317 {
318         /* Assert START */
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);
323 }
324
325 /*
326  * twsi_send() - Send a byte on the I2C bus.
327  *
328  * The byte may be part of an address byte or data.
329  *
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.
337  */
338 static int twsi_send(struct mvtwsi_registers *twsi, u8 byte,
339                      int expected_status, uint tick)
340 {
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,
345                &twsi->control);
346         /* Wait for controller to receive byte, and check ACK */
347         return twsi_wait(twsi, expected_status, tick);
348 }
349
350 /*
351  * twsi_recv() - Receive a byte on the I2C bus.
352  *
353  * The static variable mvtwsi_control_flags controls whether we ack or nak.
354  *
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.
362  */
363 static int twsi_recv(struct mvtwsi_registers *twsi, u8 *byte, int ack_flag,
364                      uint tick)
365 {
366         int expected_status, status, control;
367
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 */
378         if (status == 0)
379                 *byte = readl(&twsi->data);
380         return status;
381 }
382
383 /*
384  * twsi_stop() - Assert a STOP condition on the bus.
385  *
386  * This function is also used to force the bus back to idle state (SDA =
387  * SCL = 1).
388  *
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
392  *         occurred.
393  */
394 static int twsi_stop(struct mvtwsi_registers *twsi, uint tick)
395 {
396         int control, stop_status;
397         int status = 0;
398         int timeout = 1000;
399
400         /* Assert STOP */
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() */
404         do {
405                 stop_status = readl(&twsi->status);
406                 if (stop_status == MVTWSI_STATUS_IDLE)
407                         break;
408                 ndelay(tick); /* One clock cycle */
409         } while (timeout--);
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);
414         return status;
415 }
416
417 /*
418  * twsi_calc_freq() - Compute I2C frequency depending on m and n parameters.
419  *
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.
423  */
424 static uint twsi_calc_freq(const int n, const int m)
425 {
426 #ifdef CONFIG_ARCH_SUNXI
427         return CONFIG_SYS_TCLK / (10 * (m + 1) * (1 << n));
428 #else
429         return CONFIG_SYS_TCLK / (10 * (m + 1) * (2 << n));
430 #endif
431 }
432
433 /*
434  * twsi_reset() - Reset the I2C controller.
435  *
436  * Resetting the controller also resets the baud rate and slave address, hence
437  * they must be re-established after the reset.
438  *
439  * @twsi:       The MVTWSI register structure to use.
440  */
441 static void twsi_reset(struct mvtwsi_registers *twsi)
442 {
443         /* Reset controller */
444         writel(0, &twsi->soft_reset);
445         /* Wait 2 ms -- this is what the Marvell LSP does */
446         udelay(20000);
447 }
448
449 /*
450  * __twsi_i2c_set_bus_speed() - Set the speed of the I2C controller.
451  *
452  * This function sets baud rate to the highest possible value that does not
453  * exceed the requested rate.
454  *
455  * @twsi:               The MVTWSI register structure to use.
456  * @requested_speed:    The desired frequency the controller should run at
457  *                      in Hz.
458  * @return The actual frequency the controller was configured to.
459  */
460 static uint __twsi_i2c_set_bus_speed(struct mvtwsi_registers *twsi,
461                                      uint requested_speed)
462 {
463         uint tmp_speed, highest_speed, n, m;
464         uint baud = 0x44; /* Baud rate after controller reset */
465
466         highest_speed = 0;
467         /* Successively try m, n combinations, and use the combination
468          * resulting in the largest speed that's not above the requested
469          * speed */
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;
476                                 baud = (m << 3) | n;
477                         }
478                 }
479         }
480         writel(baud, &twsi->baudrate);
481
482         /* Wait for controller for one tick */
483 #ifdef CONFIG_DM_I2C
484         ndelay(calc_tick(highest_speed));
485 #else
486         ndelay(10000);
487 #endif
488         return highest_speed;
489 }
490
491 /*
492  * __twsi_i2c_init() - Initialize the I2C controller.
493  *
494  * @twsi:               The MVTWSI register structure to use.
495  * @speed:              The initial frequency the controller should run at
496  *                      in Hz.
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
501  *         occurred.
502  */
503 static void __twsi_i2c_init(struct mvtwsi_registers *twsi, int speed,
504                             int slaveadd, uint *actual_speed)
505 {
506         uint tmp_speed;
507
508         /* Reset controller */
509         twsi_reset(twsi);
510         /* Set speed */
511         tmp_speed = __twsi_i2c_set_bus_speed(twsi, speed);
512         if (actual_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 */
518 #ifdef CONFIG_DM_I2C
519         (void) twsi_stop(twsi, calc_tick(*actual_speed));
520 #else
521         (void) twsi_stop(twsi, 10000);
522 #endif
523 }
524
525 /*
526  * i2c_begin() - Start a I2C transaction.
527  *
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
531  * the address byte.
532  *
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
538  *                              I2C speed.
539  * @return Zero if the operation succeeded, or a non-zero code if a time out or
540  *         unexpected I2C status occurred.
541  */
542 static int i2c_begin(struct mvtwsi_registers *twsi, int expected_start_status,
543                      u8 addr, uint tick)
544 {
545         int status, expected_addr_status;
546
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;
551         else /* Writing */
552                 expected_addr_status = MVTWSI_STATUS_ADDR_W_ACK;
553         /* Assert START */
554         status = twsi_start(twsi, expected_start_status, tick);
555         /* Send out the address if the start went well */
556         if (status == 0)
557                 status = twsi_send(twsi, addr, expected_addr_status, tick);
558         /* Return 0, or the status of the first failure */
559         return status;
560 }
561
562 /*
563  * __twsi_i2c_probe_chip() - Probe the given I2C chip address.
564  *
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.
567  *
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.
573  */
574 static int __twsi_i2c_probe_chip(struct mvtwsi_registers *twsi, uchar chip,
575                                  uint tick)
576 {
577         u8 dummy_byte;
578         int status;
579
580         /* Begin i2c read */
581         status = i2c_begin(twsi, MVTWSI_STATUS_START, (chip << 1) | 1, tick);
582         /* Dummy read was accepted: receive byte, but NAK it. */
583         if (status == 0)
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 */
588         return status;
589 }
590
591 /*
592  * __twsi_i2c_read() - Read data from a I2C chip.
593  *
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.
596  *
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.
601  *
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.
612  */
613 static int __twsi_i2c_read(struct mvtwsi_registers *twsi, uchar chip,
614                            u8 *addr, int alen, uchar *data, int length,
615                            uint tick)
616 {
617         int status = 0;
618         int stop_status;
619         int expected_start = MVTWSI_STATUS_START;
620
621         if (alen > 0) {
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;
630         }
631         /* Begin i2c read to receive data bytes */
632         if (status == 0)
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
635          * read */
636         while ((status == 0) && length--)
637                 status = twsi_recv(twsi, data++,
638                                    length > 0 ?
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;
644 }
645
646 /*
647  * __twsi_i2c_write() - Send data to a I2C chip.
648  *
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.
651  *
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.
661  */
662 static int __twsi_i2c_write(struct mvtwsi_registers *twsi, uchar chip,
663                             u8 *addr, int alen, uchar *data, int length,
664                             uint tick)
665 {
666         int status, stop_status;
667
668         /* Begin i2c write to send first the address bytes, then the
669          * data bytes */
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,
674                                    tick);
675         /* Send data bytes */
676         while ((status == 0) && (length-- > 0))
677                 status = twsi_send(twsi, *(data++), MVTWSI_STATUS_DATA_W_ACK,
678                                    tick);
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;
683 }
684
685 #ifndef CONFIG_DM_I2C
686 static void twsi_i2c_init(struct i2c_adapter *adap, int speed,
687                           int slaveadd)
688 {
689         struct mvtwsi_registers *twsi = twsi_get_base(adap);
690         __twsi_i2c_init(twsi, speed, slaveadd, NULL);
691 }
692
693 static uint twsi_i2c_set_bus_speed(struct i2c_adapter *adap,
694                                    uint requested_speed)
695 {
696         struct mvtwsi_registers *twsi = twsi_get_base(adap);
697         __twsi_i2c_set_bus_speed(twsi, requested_speed);
698         return 0;
699 }
700
701 static int twsi_i2c_probe(struct i2c_adapter *adap, uchar chip)
702 {
703         struct mvtwsi_registers *twsi = twsi_get_base(adap);
704         return __twsi_i2c_probe_chip(twsi, chip, 10000);
705 }
706
707 static int twsi_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
708                          int alen, uchar *data, int length)
709 {
710         struct mvtwsi_registers *twsi = twsi_get_base(adap);
711         u8 addr_bytes[4];
712
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;
717
718         return __twsi_i2c_read(twsi, chip, addr_bytes, alen, data, length,
719                                10000);
720 }
721
722 static int twsi_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
723                           int alen, uchar *data, int length)
724 {
725         struct mvtwsi_registers *twsi = twsi_get_base(adap);
726         u8 addr_bytes[4];
727
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;
732
733         return __twsi_i2c_write(twsi, chip, addr_bytes, alen, data, length,
734                                 10000);
735 }
736
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)
742 #endif
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)
748
749 #endif
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)
755
756 #endif
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)
762
763 #endif
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)
769
770 #endif
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)
776
777 #endif
778 #else /* CONFIG_DM_I2C */
779
780 static int mvtwsi_i2c_probe_chip(struct udevice *bus, u32 chip_addr,
781                                  u32 chip_flags)
782 {
783         struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
784         return __twsi_i2c_probe_chip(dev->base, chip_addr, dev->tick);
785 }
786
787 static int mvtwsi_i2c_set_bus_speed(struct udevice *bus, uint speed)
788 {
789         struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
790
791         dev->speed = __twsi_i2c_set_bus_speed(dev->base, speed);
792         dev->tick = calc_tick(dev->speed);
793
794         return 0;
795 }
796
797 static int mvtwsi_i2c_ofdata_to_platdata(struct udevice *bus)
798 {
799         struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
800
801         dev->base = devfdt_get_addr_ptr(bus);
802
803         if (!dev->base)
804                 return -ENOMEM;
805
806         dev->index = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus),
807                                     "cell-index", -1);
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);
812
813         return 0;
814 }
815
816 static void twsi_disable_i2c_slave(struct mvtwsi_registers *twsi)
817 {
818         clrbits_le32(&twsi->debug, BIT(18));
819 }
820
821 static int mvtwsi_i2c_bind(struct udevice *bus)
822 {
823         struct mvtwsi_registers *twsi = devfdt_get_addr_ptr(bus);
824
825         /* Disable the hidden slave in i2c0 of these platforms */
826         if ((IS_ENABLED(CONFIG_ARMADA_38X) || IS_ENABLED(CONFIG_ARCH_KIRKWOOD))
827                         && bus->req_seq == 0)
828                 twsi_disable_i2c_slave(twsi);
829
830         return 0;
831 }
832
833 static int mvtwsi_i2c_probe(struct udevice *bus)
834 {
835         struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
836         uint actual_speed;
837
838         __twsi_i2c_init(dev->base, dev->speed, dev->slaveadd, &actual_speed);
839         dev->speed = actual_speed;
840         dev->tick = calc_tick(dev->speed);
841         return 0;
842 }
843
844 static int mvtwsi_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
845 {
846         struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
847         struct i2c_msg *dmsg, *omsg, dummy;
848
849         memset(&dummy, 0, sizeof(struct i2c_msg));
850
851         /* We expect either two messages (one with an offset and one with the
852          * actual data) or one message (just data or offset/data combined) */
853         if (nmsgs > 2 || nmsgs == 0) {
854                 debug("%s: Only one or two messages are supported.", __func__);
855                 return -1;
856         }
857
858         omsg = nmsgs == 1 ? &dummy : msg;
859         dmsg = nmsgs == 1 ? msg : msg + 1;
860
861         if (dmsg->flags & I2C_M_RD)
862                 return __twsi_i2c_read(dev->base, dmsg->addr, omsg->buf,
863                                        omsg->len, dmsg->buf, dmsg->len,
864                                        dev->tick);
865         else
866                 return __twsi_i2c_write(dev->base, dmsg->addr, omsg->buf,
867                                         omsg->len, dmsg->buf, dmsg->len,
868                                         dev->tick);
869 }
870
871 static const struct dm_i2c_ops mvtwsi_i2c_ops = {
872         .xfer           = mvtwsi_i2c_xfer,
873         .probe_chip     = mvtwsi_i2c_probe_chip,
874         .set_bus_speed  = mvtwsi_i2c_set_bus_speed,
875 };
876
877 static const struct udevice_id mvtwsi_i2c_ids[] = {
878         { .compatible = "marvell,mv64xxx-i2c", },
879         { .compatible = "marvell,mv78230-i2c", },
880         { .compatible = "allwinner,sun6i-a31-i2c", },
881         { /* sentinel */ }
882 };
883
884 U_BOOT_DRIVER(i2c_mvtwsi) = {
885         .name = "i2c_mvtwsi",
886         .id = UCLASS_I2C,
887         .of_match = mvtwsi_i2c_ids,
888         .bind = mvtwsi_i2c_bind,
889         .probe = mvtwsi_i2c_probe,
890         .ofdata_to_platdata = mvtwsi_i2c_ofdata_to_platdata,
891         .priv_auto_alloc_size = sizeof(struct mvtwsi_i2c_dev),
892         .ops = &mvtwsi_i2c_ops,
893 };
894 #endif /* CONFIG_DM_I2C */