i2c: mvtwsi: Improve and fix comments
[platform/kernel/u-boot.git] / drivers / i2c / mvtwsi.c
1 /*
2  * Driver for the TWSI (i2c) controller found on the Marvell
3  * orion5x and kirkwood SoC families.
4  *
5  * Author: Albert Aribaud <albert.u.boot@aribaud.net>
6  * Copyright (c) 2010 Albert Aribaud.
7  *
8  * SPDX-License-Identifier:     GPL-2.0+
9  */
10
11 #include <common.h>
12 #include <i2c.h>
13 #include <asm/errno.h>
14 #include <asm/io.h>
15
16 /*
17  * Include a file that will provide CONFIG_I2C_MVTWSI_BASE*, and possibly other
18  * settings
19  */
20
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>
27 #else
28 #error Driver mvtwsi not supported by SoC or board
29 #endif
30
31 /*
32  * TWSI register structure
33  */
34
35 #ifdef CONFIG_SUNXI
36
37 struct  mvtwsi_registers {
38         u32 slave_address;
39         u32 xtnd_slave_addr;
40         u32 data;
41         u32 control;
42         u32 status;
43         u32 baudrate;
44         u32 soft_reset;
45 };
46
47 #else
48
49 struct  mvtwsi_registers {
50         u32 slave_address;
51         u32 data;
52         u32 control;
53         union {
54                 u32 status;     /* When reading */
55                 u32 baudrate;   /* When writing */
56         };
57         u32 xtnd_slave_addr;
58         u32 reserved[2];
59         u32 soft_reset;
60 };
61
62 #endif
63
64 /*
65  * enum mvtwsi_ctrl_register_fields - Bit masks for flags in the control
66  * register
67  */
68 enum mvtwsi_ctrl_register_fields {
69         /* Acknowledge bit */
70         MVTWSI_CONTROL_ACK      = 0x00000004,
71         /* Interrupt flag */
72         MVTWSI_CONTROL_IFLG     = 0x00000008,
73         /* Stop bit */
74         MVTWSI_CONTROL_STOP     = 0x00000010,
75         /* Start bit */
76         MVTWSI_CONTROL_START    = 0x00000020,
77         /* I2C enable */
78         MVTWSI_CONTROL_TWSIEN   = 0x00000040,
79         /* Interrupt enable */
80         MVTWSI_CONTROL_INTEN    = 0x00000080,
81 };
82
83 /*
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.
86  */
87
88 #ifdef CONFIG_SUNXI_GEN_SUN6I
89 #define MVTWSI_CONTROL_CLEAR_IFLG       0x00000008
90 #else
91 #define MVTWSI_CONTROL_CLEAR_IFLG       0x00000000
92 #endif
93
94 /*
95  * enum mvstwsi_status_values - Possible values of I2C controller's status
96  * register
97  *
98  * Only those statuses expected in normal master operation on
99  * non-10-bit-address devices are specified.
100  *
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
103  * code.
104  */
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,
124 };
125
126 /*
127  * MVTWSI controller base
128  */
129
130 static struct mvtwsi_registers *twsi_get_base(struct i2c_adapter *adap)
131 {
132         switch (adap->hwadapnr) {
133 #ifdef CONFIG_I2C_MVTWSI_BASE0
134         case 0:
135                 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE0;
136 #endif
137 #ifdef CONFIG_I2C_MVTWSI_BASE1
138         case 1:
139                 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE1;
140 #endif
141 #ifdef CONFIG_I2C_MVTWSI_BASE2
142         case 2:
143                 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE2;
144 #endif
145 #ifdef CONFIG_I2C_MVTWSI_BASE3
146         case 3:
147                 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE3;
148 #endif
149 #ifdef CONFIG_I2C_MVTWSI_BASE4
150         case 4:
151                 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE4;
152 #endif
153 #ifdef CONFIG_I2C_MVTWSI_BASE5
154         case 5:
155                 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE5;
156 #endif
157         default:
158                 printf("Missing mvtwsi controller %d base\n", adap->hwadapnr);
159                 break;
160         }
161
162         return NULL;
163 }
164
165 /*
166  * enum mvtwsi_error_class - types of I2C errors
167  */
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,
173 };
174
175 /*
176  * mvtwsi_error() - Build I2C return code from error information
177  *
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.).
181  *
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.
187  */
188 inline uint mvtwsi_error(uint ec, uint lc, uint ls, uint es)
189 {
190         return ((ec << 24) & 0xFF000000)
191                | ((lc << 16) & 0x00FF0000)
192                | ((ls << 8) & 0x0000FF00)
193                | (es & 0xFF);
194 }
195
196 /*
197  * Wait for IFLG to raise, or return 'timeout.' Then, if the status is as
198  * expected, return 0 (ok) or 'wrong status' otherwise.
199  */
200 static int twsi_wait(struct i2c_adapter *adap, int expected_status)
201 {
202         struct mvtwsi_registers *twsi = twsi_get_base(adap);
203         int control, status;
204         int timeout = 1000;
205
206         do {
207                 control = readl(&twsi->control);
208                 if (control & MVTWSI_CONTROL_IFLG) {
209                         status = readl(&twsi->status);
210                         if (status == expected_status)
211                                 return 0;
212                         else
213                                 return mvtwsi_error(
214                                         MVTWSI_ERROR_WRONG_STATUS,
215                                         control, status, expected_status);
216                 }
217                 udelay(10); /* One clock cycle at 100 kHz */
218         } while (timeout--);
219         status = readl(&twsi->status);
220         return mvtwsi_error(MVTWSI_ERROR_TIMEOUT, control, status,
221                             expected_status);
222 }
223
224 /*
225  * Assert the START condition, either in a single I2C transaction
226  * or inside back-to-back ones (repeated starts).
227  */
228 static int twsi_start(struct i2c_adapter *adap, int expected_status, u8 *flags)
229 {
230         struct mvtwsi_registers *twsi = twsi_get_base(adap);
231
232         /* Set TWSIEN */
233         *flags |= MVTWSI_CONTROL_TWSIEN;
234         /* Assert START */
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);
239 }
240
241 /*
242  * Send a byte (i2c address or data).
243  */
244 static int twsi_send(struct i2c_adapter *adap, u8 byte, int expected_status,
245                      u8 *flags)
246 {
247         struct mvtwsi_registers *twsi = twsi_get_base(adap);
248
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);
255 }
256
257 /*
258  * Receive a byte.
259  */
260 static int twsi_recv(struct i2c_adapter *adap, u8 *byte, u8 *flags)
261 {
262         struct mvtwsi_registers *twsi = twsi_get_base(adap);
263         int expected_status, status;
264
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;
268         else
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 */
275         if (status == 0)
276                 *byte = readl(&twsi->data);
277         return status;
278 }
279
280 /*
281  * Assert the STOP condition.
282  * This is also used to force the bus back to idle (SDA = SCL = 1).
283  */
284 static int twsi_stop(struct i2c_adapter *adap, int status)
285 {
286         struct mvtwsi_registers *twsi = twsi_get_base(adap);
287         int control, stop_status;
288         int timeout = 1000;
289
290         /* Assert STOP */
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() */
294         do {
295                 stop_status = readl(&twsi->status);
296                 if (stop_status == MVTWSI_STATUS_IDLE)
297                         break;
298                 udelay(10); /* One clock cycle at 100 kHz */
299         } while (timeout--);
300         control = readl(&twsi->control);
301         if (stop_status != MVTWSI_STATUS_IDLE)
302                 if (status == 0)
303                         status = mvtwsi_error(
304                                 MVTWSI_ERROR_TIMEOUT,
305                                 control, status, MVTWSI_STATUS_IDLE);
306         return status;
307 }
308
309 static unsigned int twsi_calc_freq(const int n, const int m)
310 {
311 #ifdef CONFIG_SUNXI
312         return CONFIG_SYS_TCLK / (10 * (m + 1) * (1 << n));
313 #else
314         return CONFIG_SYS_TCLK / (10 * (m + 1) * (2 << n));
315 #endif
316 }
317
318 /*
319  * Reset controller.
320  * Controller reset also resets the baud rate and slave address, so
321  * they must be re-established afterwards.
322  */
323 static void twsi_reset(struct i2c_adapter *adap)
324 {
325         struct mvtwsi_registers *twsi = twsi_get_base(adap);
326
327         /* Reset controller */
328         writel(0, &twsi->soft_reset);
329         /* Wait 2 ms -- this is what the Marvell LSP does */
330         udelay(20000);
331 }
332
333 /*
334  * Sets baud to the highest possible value not exceeding the requested one.
335  */
336 static unsigned int twsi_i2c_set_bus_speed(struct i2c_adapter *adap,
337                                            unsigned int requested_speed)
338 {
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 */
342
343         highest_speed = 0;
344         /* Successively try m, n combinations, and use the combination
345          * resulting in the largest speed that's not above the requested
346          * speed */
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;
353                                 baud = (m << 3) | n;
354                         }
355                 }
356         }
357         writel(baud, &twsi->baudrate);
358         return 0;
359 }
360
361 static void twsi_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
362 {
363         struct mvtwsi_registers *twsi = twsi_get_base(adap);
364
365         /* Reset controller */
366         twsi_reset(adap);
367         /* Set speed */
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);
374 }
375
376 /*
377  * Begin I2C transaction with expected start status, at given address.
378  * Expected address status will derive from direction bit (bit 0) in addr.
379  */
380 static int i2c_begin(struct i2c_adapter *adap, int expected_start_status,
381                      u8 addr, u8 *flags)
382 {
383         int status, expected_addr_status;
384
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;
389         else /* Writing */
390                 expected_addr_status = MVTWSI_STATUS_ADDR_W_ACK;
391         /* Assert START */
392         status = twsi_start(adap, expected_start_status, flags);
393         /* Send out the address if the start went well */
394         if (status == 0)
395                 status = twsi_send(adap, addr, expected_addr_status,
396                                    flags);
397         /* Return 0, or the status of the first failure */
398         return status;
399 }
400
401 /*
402  * Begin read, nak data byte, end.
403  */
404 static int twsi_i2c_probe(struct i2c_adapter *adap, uchar chip)
405 {
406         u8 dummy_byte;
407         u8 flags = 0;
408         int status;
409
410         /* Begin i2c read */
411         status = i2c_begin(adap, MVTWSI_STATUS_START, (chip << 1) | 1, &flags);
412         /* Dummy read was accepted: receive byte, but NAK it. */
413         if (status == 0)
414                 status = twsi_recv(adap, &dummy_byte, &flags);
415         /* Stop transaction */
416         twsi_stop(adap, 0);
417         /* Return 0, or the status of the first failure */
418         return status;
419 }
420
421 /*
422  * Begin write, send address byte(s), begin read, receive data bytes, end.
423  *
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.
428  */
429 static int twsi_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
430                         int alen, uchar *data, int length)
431 {
432         int status;
433         u8 flags = 0;
434
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 */
442         if (status == 0)
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 */
446         if (length > 0)
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 */
451                 if (length == 0)
452                         flags &= ~MVTWSI_CONTROL_ACK;
453                 /* Read current byte */
454                 status = twsi_recv(adap, data++, &flags);
455         }
456         /* Stop transaction */
457         status = twsi_stop(adap, status);
458         /* Return 0, or the status of the first failure */
459         return status;
460 }
461
462 /*
463  * Begin write, send address byte(s), send data bytes, end.
464  */
465 static int twsi_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
466                         int alen, uchar *data, int length)
467 {
468         int status;
469         u8 flags = 0;
470
471         /* Begin i2c write to send first the address bytes, then the
472          * data bytes */
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,
481                                    &flags);
482         /* Stop transaction */
483         status = twsi_stop(adap, status);
484         /* Return 0, or the status of the first failure */
485         return status;
486 }
487
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)
493 #endif
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)
499
500 #endif
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)
506
507 #endif
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)
513
514 #endif
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)
520
521 #endif
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)
527
528 #endif