2 * Driver for the TWSI (i2c) controller found on the Marvell
3 * orion5x and kirkwood SoC families.
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*, and possibly other
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 the status is as
198 * expected, return 0 (ok) or 'wrong status' otherwise.
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);
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 /* Write byte to data register for sending */
250 writel(byte, &twsi->data);
251 /* Clear any pending interrupt -- that will 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);
260 static int twsi_recv(struct i2c_adapter *adap, u8 *byte, u8 *flags)
262 struct mvtwsi_registers *twsi = twsi_get_base(adap);
263 int expected_status, status;
265 /* Compute expected status based on ACK bit in passed control flags */
266 if (*flags & MVTWSI_CONTROL_ACK)
267 expected_status = MVTWSI_STATUS_DATA_R_ACK;
269 expected_status = MVTWSI_STATUS_DATA_R_NAK;
270 /* Acknowledge *previous state*, and launch receive */
271 writel(*flags | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
272 /* Wait for controller to receive byte, and assert ACK or NAK */
273 status = twsi_wait(adap, expected_status);
274 /* If we did receive the expected byte, store it */
276 *byte = readl(&twsi->data);
281 * Assert the STOP condition.
282 * This is also used to force the bus back to idle (SDA = SCL = 1).
284 static int twsi_stop(struct i2c_adapter *adap, int status)
286 struct mvtwsi_registers *twsi = twsi_get_base(adap);
287 int control, stop_status;
291 control = MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_STOP;
292 writel(control | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
293 /* Wait for IDLE; IFLG won't rise, so we can't use twsi_wait() */
295 stop_status = readl(&twsi->status);
296 if (stop_status == MVTWSI_STATUS_IDLE)
298 udelay(10); /* One clock cycle at 100 kHz */
300 control = readl(&twsi->control);
301 if (stop_status != MVTWSI_STATUS_IDLE)
303 status = mvtwsi_error(
304 MVTWSI_ERROR_TIMEOUT,
305 control, status, MVTWSI_STATUS_IDLE);
309 static unsigned int twsi_calc_freq(const int n, const int m)
312 return CONFIG_SYS_TCLK / (10 * (m + 1) * (1 << n));
314 return CONFIG_SYS_TCLK / (10 * (m + 1) * (2 << n));
320 * Controller reset also resets the baud rate and slave address, so
321 * they must be re-established afterwards.
323 static void twsi_reset(struct i2c_adapter *adap)
325 struct mvtwsi_registers *twsi = twsi_get_base(adap);
327 /* Reset controller */
328 writel(0, &twsi->soft_reset);
329 /* Wait 2 ms -- this is what the Marvell LSP does */
334 * Sets baud to the highest possible value not exceeding the requested one.
336 static unsigned int twsi_i2c_set_bus_speed(struct i2c_adapter *adap,
337 unsigned int requested_speed)
339 struct mvtwsi_registers *twsi = twsi_get_base(adap);
340 unsigned int tmp_speed, highest_speed, n, m;
341 unsigned int baud = 0x44; /* Baud rate after controller reset */
344 /* Successively try m, n combinations, and use the combination
345 * resulting in the largest speed that's not above the requested
347 for (n = 0; n < 8; n++) {
348 for (m = 0; m < 16; m++) {
349 tmp_speed = twsi_calc_freq(n, m);
350 if ((tmp_speed <= requested_speed) &&
351 (tmp_speed > highest_speed)) {
352 highest_speed = tmp_speed;
357 writel(baud, &twsi->baudrate);
361 static void twsi_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
363 struct mvtwsi_registers *twsi = twsi_get_base(adap);
365 /* Reset controller */
368 twsi_i2c_set_bus_speed(adap, speed);
369 /* Set slave address; even though we don't use it */
370 writel(slaveadd, &twsi->slave_address);
371 writel(0, &twsi->xtnd_slave_addr);
372 /* Assert STOP, but don't care for the result */
373 (void) twsi_stop(adap, 0);
377 * Begin I2C transaction with expected start status, at given address.
378 * Expected address status will derive from direction bit (bit 0) in addr.
380 static int i2c_begin(struct i2c_adapter *adap, int expected_start_status,
383 int status, expected_addr_status;
385 /* Compute the expected address status from the direction bit in
386 * the address byte */
387 if (addr & 1) /* Reading */
388 expected_addr_status = MVTWSI_STATUS_ADDR_R_ACK;
390 expected_addr_status = MVTWSI_STATUS_ADDR_W_ACK;
392 status = twsi_start(adap, expected_start_status, flags);
393 /* Send out the address if the start went well */
395 status = twsi_send(adap, addr, expected_addr_status,
397 /* Return 0, or the status of the first failure */
402 * Begin read, nak data byte, end.
404 static int twsi_i2c_probe(struct i2c_adapter *adap, uchar chip)
411 status = i2c_begin(adap, MVTWSI_STATUS_START, (chip << 1) | 1, &flags);
412 /* Dummy read was accepted: receive byte, but NAK it. */
414 status = twsi_recv(adap, &dummy_byte, &flags);
415 /* Stop transaction */
417 /* Return 0, or the status of the first failure */
422 * Begin write, send address byte(s), begin read, receive data bytes, end.
424 * NOTE: Some devices want a stop right before the second start, while some
425 * will choke if it is there. Since deciding this is not yet supported in
426 * higher level APIs, we need to make a decision here, and for the moment that
427 * will be a repeated start without a preceding stop.
429 static int twsi_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
430 int alen, uchar *data, int length)
435 /* Begin i2c write to send the address bytes */
436 status = i2c_begin(adap, MVTWSI_STATUS_START, (chip << 1), &flags);
437 /* Send address bytes */
438 while ((status == 0) && alen--)
439 status = twsi_send(adap, addr >> (8*alen),
440 MVTWSI_STATUS_DATA_W_ACK, &flags);
441 /* Begin i2c read to receive data bytes */
443 status = i2c_begin(adap, MVTWSI_STATUS_REPEATED_START,
444 (chip << 1) | 1, &flags);
445 /* Prepare ACK if at least one byte must be received */
447 flags |= MVTWSI_CONTROL_ACK;
448 /* Receive actual data bytes */
449 while ((status == 0) && length--) {
450 /* Set NAK if we if we have nothing more to read */
452 flags &= ~MVTWSI_CONTROL_ACK;
453 /* Read current byte */
454 status = twsi_recv(adap, data++, &flags);
456 /* Stop transaction */
457 status = twsi_stop(adap, status);
458 /* Return 0, or the status of the first failure */
463 * Begin write, send address byte(s), send data bytes, end.
465 static int twsi_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
466 int alen, uchar *data, int length)
471 /* Begin i2c write to send first the address bytes, then the
473 status = i2c_begin(adap, MVTWSI_STATUS_START, (chip << 1), &flags);
474 /* Send address bytes */
475 while ((status == 0) && alen--)
476 status = twsi_send(adap, addr >> (8*alen),
477 MVTWSI_STATUS_DATA_W_ACK, &flags);
478 /* Send data bytes */
479 while ((status == 0) && (length-- > 0))
480 status = twsi_send(adap, *(data++), MVTWSI_STATUS_DATA_W_ACK,
482 /* Stop transaction */
483 status = twsi_stop(adap, status);
484 /* Return 0, or the status of the first failure */
488 #ifdef CONFIG_I2C_MVTWSI_BASE0
489 U_BOOT_I2C_ADAP_COMPLETE(twsi0, twsi_i2c_init, twsi_i2c_probe,
490 twsi_i2c_read, twsi_i2c_write,
491 twsi_i2c_set_bus_speed,
492 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0)
494 #ifdef CONFIG_I2C_MVTWSI_BASE1
495 U_BOOT_I2C_ADAP_COMPLETE(twsi1, twsi_i2c_init, twsi_i2c_probe,
496 twsi_i2c_read, twsi_i2c_write,
497 twsi_i2c_set_bus_speed,
498 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 1)
501 #ifdef CONFIG_I2C_MVTWSI_BASE2
502 U_BOOT_I2C_ADAP_COMPLETE(twsi2, twsi_i2c_init, twsi_i2c_probe,
503 twsi_i2c_read, twsi_i2c_write,
504 twsi_i2c_set_bus_speed,
505 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 2)
508 #ifdef CONFIG_I2C_MVTWSI_BASE3
509 U_BOOT_I2C_ADAP_COMPLETE(twsi3, twsi_i2c_init, twsi_i2c_probe,
510 twsi_i2c_read, twsi_i2c_write,
511 twsi_i2c_set_bus_speed,
512 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 3)
515 #ifdef CONFIG_I2C_MVTWSI_BASE4
516 U_BOOT_I2C_ADAP_COMPLETE(twsi4, twsi_i2c_init, twsi_i2c_probe,
517 twsi_i2c_read, twsi_i2c_write,
518 twsi_i2c_set_bus_speed,
519 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 4)
522 #ifdef CONFIG_I2C_MVTWSI_BASE5
523 U_BOOT_I2C_ADAP_COMPLETE(twsi5, twsi_i2c_init, twsi_i2c_probe,
524 twsi_i2c_read, twsi_i2c_write,
525 twsi_i2c_set_bus_speed,
526 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 5)