2 * Driver for the TWSI (i2c) controller found on the Marvell
3 * orion5x and kirkwood SoC families.
5 * Author: Albert Aribaud <albert.u.boot@aribaud.net>
6 * Copyright (c) 2010 Albert Aribaud.
8 * SPDX-License-Identifier: GPL-2.0+
13 #include <asm/errno.h>
17 * include a file that will provide CONFIG_I2C_MVTWSI_BASE*
18 * and possibly other settings
21 #if defined(CONFIG_ORION5X)
22 #include <asm/arch/orion5x.h>
23 #elif (defined(CONFIG_KIRKWOOD) || defined(CONFIG_ARCH_MVEBU))
24 #include <asm/arch/soc.h>
25 #elif defined(CONFIG_SUNXI)
26 #include <asm/arch/i2c.h>
28 #error Driver mvtwsi not supported by SoC or board
32 * TWSI register structure
37 struct mvtwsi_registers
{
49 struct mvtwsi_registers
{
54 u32 status
; /* when reading */
55 u32 baudrate
; /* when writing */
65 * enum mvtwsi_ctrl_register_fields - Bit masks for flags in the control
68 enum mvtwsi_ctrl_register_fields
{
70 MVTWSI_CONTROL_ACK
= 0x00000004,
72 MVTWSI_CONTROL_IFLG
= 0x00000008,
74 MVTWSI_CONTROL_STOP
= 0x00000010,
76 MVTWSI_CONTROL_START
= 0x00000020,
78 MVTWSI_CONTROL_TWSIEN
= 0x00000040,
79 /* Interrupt enable */
80 MVTWSI_CONTROL_INTEN
= 0x00000080,
84 * On sun6i and newer IFLG is a write-clear bit which is cleared by writing 1,
85 * on other platforms it is a normal r/w bit which is cleared by writing 0.
88 #ifdef CONFIG_SUNXI_GEN_SUN6I
89 #define MVTWSI_CONTROL_CLEAR_IFLG 0x00000008
91 #define MVTWSI_CONTROL_CLEAR_IFLG 0x00000000
95 * enum mvstwsi_status_values - Possible values of I2C controller's status
98 * Only those statuses expected in normal master operation on
99 * non-10-bit-address devices are specified.
101 * Every status that's unexpected during normal operation (bus errors,
102 * arbitration losses, missing ACKs...) is passed back to the caller as an error
105 enum mvstwsi_status_values
{
106 /* START condition transmitted */
107 MVTWSI_STATUS_START
= 0x08,
108 /* Repeated START condition transmitted */
109 MVTWSI_STATUS_REPEATED_START
= 0x10,
110 /* Address + write bit transmitted, ACK received */
111 MVTWSI_STATUS_ADDR_W_ACK
= 0x18,
112 /* Data transmitted, ACK received */
113 MVTWSI_STATUS_DATA_W_ACK
= 0x28,
114 /* Address + read bit transmitted, ACK received */
115 MVTWSI_STATUS_ADDR_R_ACK
= 0x40,
116 /* Address + read bit transmitted, ACK not received */
117 MVTWSI_STATUS_ADDR_R_NAK
= 0x48,
118 /* Data received, ACK transmitted */
119 MVTWSI_STATUS_DATA_R_ACK
= 0x50,
120 /* Data received, ACK not transmitted */
121 MVTWSI_STATUS_DATA_R_NAK
= 0x58,
122 /* No relevant status */
123 MVTWSI_STATUS_IDLE
= 0xF8,
127 * MVTWSI controller base
130 static struct mvtwsi_registers
*twsi_get_base(struct i2c_adapter
*adap
)
132 switch (adap
->hwadapnr
) {
133 #ifdef CONFIG_I2C_MVTWSI_BASE0
135 return (struct mvtwsi_registers
*)CONFIG_I2C_MVTWSI_BASE0
;
137 #ifdef CONFIG_I2C_MVTWSI_BASE1
139 return (struct mvtwsi_registers
*)CONFIG_I2C_MVTWSI_BASE1
;
141 #ifdef CONFIG_I2C_MVTWSI_BASE2
143 return (struct mvtwsi_registers
*)CONFIG_I2C_MVTWSI_BASE2
;
145 #ifdef CONFIG_I2C_MVTWSI_BASE3
147 return (struct mvtwsi_registers
*)CONFIG_I2C_MVTWSI_BASE3
;
149 #ifdef CONFIG_I2C_MVTWSI_BASE4
151 return (struct mvtwsi_registers
*)CONFIG_I2C_MVTWSI_BASE4
;
153 #ifdef CONFIG_I2C_MVTWSI_BASE5
155 return (struct mvtwsi_registers
*)CONFIG_I2C_MVTWSI_BASE5
;
158 printf("Missing mvtwsi controller %d base\n", adap
->hwadapnr
);
166 * enum mvtwsi_error_class - types of I2C errors
168 enum mvtwsi_error_class
{
169 /* The controller returned a different status than expected */
170 MVTWSI_ERROR_WRONG_STATUS
= 0x01,
171 /* The controller timed out */
172 MVTWSI_ERROR_TIMEOUT
= 0x02,
176 * mvtwsi_error() - Build I2C return code from error information
178 * For debugging purposes, this function packs some information of an occurred
179 * error into a return code. These error codes are returned from I2C API
180 * functions (i2c_{read,write}, dm_i2c_{read,write}, etc.).
182 * @ec: The error class of the error (enum mvtwsi_error_class).
183 * @lc: The last value of the control register.
184 * @ls: The last value of the status register.
185 * @es: The expected value of the status register.
186 * @return The generated error code.
188 inline uint
mvtwsi_error(uint ec
, uint lc
, uint ls
, uint es
)
190 return ((ec
<< 24) & 0xFF000000)
191 | ((lc
<< 16) & 0x00FF0000)
192 | ((ls
<< 8) & 0x0000FF00)
197 * Wait for IFLG to raise, or return 'timeout'; then if status is as expected,
198 * return 0 (ok) or return 'wrong status'.
200 static int twsi_wait(struct i2c_adapter
*adap
, int expected_status
)
202 struct mvtwsi_registers
*twsi
= twsi_get_base(adap
);
207 control
= readl(&twsi
->control
);
208 if (control
& MVTWSI_CONTROL_IFLG
) {
209 status
= readl(&twsi
->status
);
210 if (status
== expected_status
)
214 MVTWSI_ERROR_WRONG_STATUS
,
215 control
, status
, expected_status
);
217 udelay(10); /* one clock cycle at 100 kHz */
219 status
= readl(&twsi
->status
);
220 return mvtwsi_error(MVTWSI_ERROR_TIMEOUT
, control
, status
,
225 * Assert the START condition, either in a single I2C transaction
226 * or inside back-to-back ones (repeated starts).
228 static int twsi_start(struct i2c_adapter
*adap
, int expected_status
, u8
*flags
)
230 struct mvtwsi_registers
*twsi
= twsi_get_base(adap
);
232 /* globally set TWSIEN in case it was not */
233 *flags
|= MVTWSI_CONTROL_TWSIEN
;
235 writel(*flags
| MVTWSI_CONTROL_START
|
236 MVTWSI_CONTROL_CLEAR_IFLG
, &twsi
->control
);
237 /* wait for controller to process START */
238 return twsi_wait(adap
, expected_status
);
242 * Send a byte (i2c address or data).
244 static int twsi_send(struct i2c_adapter
*adap
, u8 byte
, int expected_status
,
247 struct mvtwsi_registers
*twsi
= twsi_get_base(adap
);
249 /* put byte in data register for sending */
250 writel(byte
, &twsi
->data
);
251 /* clear any pending interrupt -- that'll cause sending */
252 writel(*flags
| MVTWSI_CONTROL_CLEAR_IFLG
, &twsi
->control
);
253 /* wait for controller to receive byte and check ACK */
254 return twsi_wait(adap
, expected_status
);
259 * Global mvtwsi_control_flags variable says if we should ack or nak.
261 static int twsi_recv(struct i2c_adapter
*adap
, u8
*byte
, u8
*flags
)
263 struct mvtwsi_registers
*twsi
= twsi_get_base(adap
);
264 int expected_status
, status
;
266 /* compute expected status based on ACK bit in global control flags */
267 if (*flags
& MVTWSI_CONTROL_ACK
)
268 expected_status
= MVTWSI_STATUS_DATA_R_ACK
;
270 expected_status
= MVTWSI_STATUS_DATA_R_NAK
;
271 /* acknowledge *previous state* and launch receive */
272 writel(*flags
| MVTWSI_CONTROL_CLEAR_IFLG
, &twsi
->control
);
273 /* wait for controller to receive byte and assert ACK or NAK */
274 status
= twsi_wait(adap
, expected_status
);
275 /* if we did receive expected byte then store it */
277 *byte
= readl(&twsi
->data
);
283 * Assert the STOP condition.
284 * This is also used to force the bus back in idle (SDA=SCL=1).
286 static int twsi_stop(struct i2c_adapter
*adap
, int status
)
288 struct mvtwsi_registers
*twsi
= twsi_get_base(adap
);
289 int control
, stop_status
;
293 control
= MVTWSI_CONTROL_TWSIEN
| MVTWSI_CONTROL_STOP
;
294 writel(control
| MVTWSI_CONTROL_CLEAR_IFLG
, &twsi
->control
);
295 /* wait for IDLE; IFLG won't rise so twsi_wait() is no use. */
297 stop_status
= readl(&twsi
->status
);
298 if (stop_status
== MVTWSI_STATUS_IDLE
)
300 udelay(10); /* one clock cycle at 100 kHz */
302 control
= readl(&twsi
->control
);
303 if (stop_status
!= MVTWSI_STATUS_IDLE
)
305 status
= mvtwsi_error(
306 MVTWSI_ERROR_TIMEOUT
,
307 control
, status
, MVTWSI_STATUS_IDLE
);
311 static unsigned int twsi_calc_freq(const int n
, const int m
)
314 return CONFIG_SYS_TCLK
/ (10 * (m
+ 1) * (1 << n
));
316 return CONFIG_SYS_TCLK
/ (10 * (m
+ 1) * (2 << n
));
322 * Controller reset also resets the baud rate and slave address, so
323 * they must be re-established afterwards.
325 static void twsi_reset(struct i2c_adapter
*adap
)
327 struct mvtwsi_registers
*twsi
= twsi_get_base(adap
);
329 /* reset controller */
330 writel(0, &twsi
->soft_reset
);
331 /* wait 2 ms -- this is what the Marvell LSP does */
336 * I2C init called by cmd_i2c when doing 'i2c reset'.
337 * Sets baud to the highest possible value not exceeding requested one.
339 static unsigned int twsi_i2c_set_bus_speed(struct i2c_adapter
*adap
,
340 unsigned int requested_speed
)
342 struct mvtwsi_registers
*twsi
= twsi_get_base(adap
);
343 unsigned int tmp_speed
, highest_speed
, n
, m
;
344 unsigned int baud
= 0x44; /* baudrate at controller reset */
346 /* use actual speed to collect progressively higher values */
348 /* compute m, n setting for highest speed not above requested speed */
349 for (n
= 0; n
< 8; n
++) {
350 for (m
= 0; m
< 16; m
++) {
351 tmp_speed
= twsi_calc_freq(n
, m
);
352 if ((tmp_speed
<= requested_speed
) &&
353 (tmp_speed
> highest_speed
)) {
354 highest_speed
= tmp_speed
;
359 writel(baud
, &twsi
->baudrate
);
363 static void twsi_i2c_init(struct i2c_adapter
*adap
, int speed
, int slaveadd
)
365 struct mvtwsi_registers
*twsi
= twsi_get_base(adap
);
367 /* reset controller */
370 twsi_i2c_set_bus_speed(adap
, speed
);
371 /* set slave address even though we don't use it */
372 writel(slaveadd
, &twsi
->slave_address
);
373 writel(0, &twsi
->xtnd_slave_addr
);
374 /* assert STOP but don't care for the result */
375 (void) twsi_stop(adap
, 0);
379 * Begin I2C transaction with expected start status, at given address.
380 * Common to i2c_probe, i2c_read and i2c_write.
381 * Expected address status will derive from direction bit (bit 0) in addr.
383 static int i2c_begin(struct i2c_adapter
*adap
, int expected_start_status
,
386 int status
, expected_addr_status
;
388 /* compute expected address status from direction bit in addr */
389 if (addr
& 1) /* reading */
390 expected_addr_status
= MVTWSI_STATUS_ADDR_R_ACK
;
392 expected_addr_status
= MVTWSI_STATUS_ADDR_W_ACK
;
394 status
= twsi_start(adap
, expected_start_status
, flags
);
395 /* send out the address if the start went well */
397 status
= twsi_send(adap
, addr
, expected_addr_status
,
399 /* return ok or status of first failure to caller */
404 * I2C probe called by cmd_i2c when doing 'i2c probe'.
405 * Begin read, nak data byte, end.
407 static int twsi_i2c_probe(struct i2c_adapter
*adap
, uchar chip
)
414 status
= i2c_begin(adap
, MVTWSI_STATUS_START
, (chip
<< 1) | 1, &flags
);
415 /* dummy read was accepted: receive byte but NAK it. */
417 status
= twsi_recv(adap
, &dummy_byte
, &flags
);
418 /* Stop transaction */
420 /* return 0 or status of first failure */
425 * I2C read called by cmd_i2c when doing 'i2c read' and by cmd_eeprom.c
426 * Begin write, send address byte(s), begin read, receive data bytes, end.
428 * NOTE: some EEPROMS want a stop right before the second start, while
429 * some will choke if it is there. Deciding which we should do is eeprom
430 * stuff, not i2c, but at the moment the APIs won't let us put it in
431 * cmd_eeprom, so we have to choose here, and for the moment that'll be
432 * a repeated start without a preceding stop.
434 static int twsi_i2c_read(struct i2c_adapter
*adap
, uchar chip
, uint addr
,
435 int alen
, uchar
*data
, int length
)
440 /* begin i2c write to send the address bytes */
441 status
= i2c_begin(adap
, MVTWSI_STATUS_START
, (chip
<< 1), &flags
);
442 /* send addr bytes */
443 while ((status
== 0) && alen
--)
444 status
= twsi_send(adap
, addr
>> (8*alen
),
445 MVTWSI_STATUS_DATA_W_ACK
, &flags
);
446 /* begin i2c read to receive eeprom data bytes */
448 status
= i2c_begin(adap
, MVTWSI_STATUS_REPEATED_START
,
449 (chip
<< 1) | 1, &flags
);
450 /* prepare ACK if at least one byte must be received */
452 flags
|= MVTWSI_CONTROL_ACK
;
453 /* now receive actual bytes */
454 while ((status
== 0) && length
--) {
455 /* reset NAK if we if no more to read now */
457 flags
&= ~MVTWSI_CONTROL_ACK
;
458 /* read current byte */
459 status
= twsi_recv(adap
, data
++, &flags
);
461 /* Stop transaction */
462 status
= twsi_stop(adap
, status
);
463 /* return 0 or status of first failure */
468 * I2C write called by cmd_i2c when doing 'i2c write' and by cmd_eeprom.c
469 * Begin write, send address byte(s), send data bytes, end.
471 static int twsi_i2c_write(struct i2c_adapter
*adap
, uchar chip
, uint addr
,
472 int alen
, uchar
*data
, int length
)
477 /* begin i2c write to send the eeprom adress bytes then data bytes */
478 status
= i2c_begin(adap
, MVTWSI_STATUS_START
, (chip
<< 1), &flags
);
479 /* send addr bytes */
480 while ((status
== 0) && alen
--)
481 status
= twsi_send(adap
, addr
>> (8*alen
),
482 MVTWSI_STATUS_DATA_W_ACK
, &flags
);
483 /* send data bytes */
484 while ((status
== 0) && (length
-- > 0))
485 status
= twsi_send(adap
, *(data
++), MVTWSI_STATUS_DATA_W_ACK
,
487 /* Stop transaction */
488 status
= twsi_stop(adap
, status
);
489 /* return 0 or status of first failure */
493 #ifdef CONFIG_I2C_MVTWSI_BASE0
494 U_BOOT_I2C_ADAP_COMPLETE(twsi0
, twsi_i2c_init
, twsi_i2c_probe
,
495 twsi_i2c_read
, twsi_i2c_write
,
496 twsi_i2c_set_bus_speed
,
497 CONFIG_SYS_I2C_SPEED
, CONFIG_SYS_I2C_SLAVE
, 0)
499 #ifdef CONFIG_I2C_MVTWSI_BASE1
500 U_BOOT_I2C_ADAP_COMPLETE(twsi1
, twsi_i2c_init
, twsi_i2c_probe
,
501 twsi_i2c_read
, twsi_i2c_write
,
502 twsi_i2c_set_bus_speed
,
503 CONFIG_SYS_I2C_SPEED
, CONFIG_SYS_I2C_SLAVE
, 1)
506 #ifdef CONFIG_I2C_MVTWSI_BASE2
507 U_BOOT_I2C_ADAP_COMPLETE(twsi2
, twsi_i2c_init
, twsi_i2c_probe
,
508 twsi_i2c_read
, twsi_i2c_write
,
509 twsi_i2c_set_bus_speed
,
510 CONFIG_SYS_I2C_SPEED
, CONFIG_SYS_I2C_SLAVE
, 2)
513 #ifdef CONFIG_I2C_MVTWSI_BASE3
514 U_BOOT_I2C_ADAP_COMPLETE(twsi3
, twsi_i2c_init
, twsi_i2c_probe
,
515 twsi_i2c_read
, twsi_i2c_write
,
516 twsi_i2c_set_bus_speed
,
517 CONFIG_SYS_I2C_SPEED
, CONFIG_SYS_I2C_SLAVE
, 3)
520 #ifdef CONFIG_I2C_MVTWSI_BASE4
521 U_BOOT_I2C_ADAP_COMPLETE(twsi4
, twsi_i2c_init
, twsi_i2c_probe
,
522 twsi_i2c_read
, twsi_i2c_write
,
523 twsi_i2c_set_bus_speed
,
524 CONFIG_SYS_I2C_SPEED
, CONFIG_SYS_I2C_SLAVE
, 4)
527 #ifdef CONFIG_I2C_MVTWSI_BASE5
528 U_BOOT_I2C_ADAP_COMPLETE(twsi5
, twsi_i2c_init
, twsi_i2c_probe
,
529 twsi_i2c_read
, twsi_i2c_write
,
530 twsi_i2c_set_bus_speed
,
531 CONFIG_SYS_I2C_SPEED
, CONFIG_SYS_I2C_SLAVE
, 5)