]> git.ipfire.org Git - people/ms/u-boot.git/blame - drivers/i2c/mvtwsi.c
ARM: dts: i.MX6QDL: icore-rqs: Fix eMMC detection during SPL
[people/ms/u-boot.git] / drivers / i2c / mvtwsi.c
CommitLineData
4ce5a728 1/*
306563a7
AA
2 * Driver for the TWSI (i2c) controller found on the Marvell
3 * orion5x and kirkwood SoC families.
4ce5a728 4 *
57b4bce9 5 * Author: Albert Aribaud <albert.u.boot@aribaud.net>
306563a7 6 * Copyright (c) 2010 Albert Aribaud.
4ce5a728 7 *
1a459660 8 * SPDX-License-Identifier: GPL-2.0+
4ce5a728 9 */
306563a7 10
4ce5a728
HS
11#include <common.h>
12#include <i2c.h>
1221ce45 13#include <linux/errno.h>
4ce5a728 14#include <asm/io.h>
c68c6243 15#include <linux/compat.h>
14a6ff2c 16#ifdef CONFIG_DM_I2C
17#include <dm.h>
18#endif
19
20DECLARE_GLOBAL_DATA_PTR;
4ce5a728 21
306563a7 22/*
49c801bf 23 * Include a file that will provide CONFIG_I2C_MVTWSI_BASE*, and possibly other
24 * settings
306563a7 25 */
4ce5a728 26
14a6ff2c 27#ifndef CONFIG_DM_I2C
306563a7
AA
28#if defined(CONFIG_ORION5X)
29#include <asm/arch/orion5x.h>
81e33f4b 30#elif (defined(CONFIG_KIRKWOOD) || defined(CONFIG_ARCH_MVEBU))
3dc23f78 31#include <asm/arch/soc.h>
aec9a0f1 32#elif defined(CONFIG_ARCH_SUNXI)
6620377e 33#include <asm/arch/i2c.h>
306563a7
AA
34#else
35#error Driver mvtwsi not supported by SoC or board
4ce5a728 36#endif
14a6ff2c 37#endif /* CONFIG_DM_I2C */
4ce5a728 38
a8f01ccf
JS
39/*
40 * On SUNXI, we get CONFIG_SYS_TCLK from this include, so we want to
41 * always have it.
42 */
43#if defined(CONFIG_DM_I2C) && defined(CONFIG_ARCH_SUNXI)
44#include <asm/arch/i2c.h>
45#endif
46
306563a7
AA
47/*
48 * TWSI register structure
49 */
4ce5a728 50
aec9a0f1 51#ifdef CONFIG_ARCH_SUNXI
6620377e
HG
52
53struct mvtwsi_registers {
54 u32 slave_address;
55 u32 xtnd_slave_addr;
56 u32 data;
57 u32 control;
58 u32 status;
59 u32 baudrate;
60 u32 soft_reset;
61};
62
63#else
64
306563a7
AA
65struct mvtwsi_registers {
66 u32 slave_address;
67 u32 data;
68 u32 control;
69 union {
49c801bf 70 u32 status; /* When reading */
71 u32 baudrate; /* When writing */
306563a7
AA
72 };
73 u32 xtnd_slave_addr;
74 u32 reserved[2];
75 u32 soft_reset;
4ce5a728
HS
76};
77
6620377e
HG
78#endif
79
14a6ff2c 80#ifdef CONFIG_DM_I2C
81struct mvtwsi_i2c_dev {
82 /* TWSI Register base for the device */
83 struct mvtwsi_registers *base;
84 /* Number of the device (determined from cell-index property) */
85 int index;
86 /* The I2C slave address for the device */
87 u8 slaveadd;
88 /* The configured I2C speed in Hz */
89 uint speed;
c68c6243 90 /* The current length of a clock period (depending on speed) */
91 uint tick;
14a6ff2c 92};
93#endif /* CONFIG_DM_I2C */
94
306563a7 95/*
dfc3958c 96 * enum mvtwsi_ctrl_register_fields - Bit masks for flags in the control
97 * register
306563a7 98 */
dfc3958c 99enum mvtwsi_ctrl_register_fields {
100 /* Acknowledge bit */
101 MVTWSI_CONTROL_ACK = 0x00000004,
102 /* Interrupt flag */
103 MVTWSI_CONTROL_IFLG = 0x00000008,
104 /* Stop bit */
105 MVTWSI_CONTROL_STOP = 0x00000010,
106 /* Start bit */
107 MVTWSI_CONTROL_START = 0x00000020,
108 /* I2C enable */
109 MVTWSI_CONTROL_TWSIEN = 0x00000040,
110 /* Interrupt enable */
111 MVTWSI_CONTROL_INTEN = 0x00000080,
112};
4ce5a728 113
904dfbfd 114/*
49c801bf 115 * On sun6i and newer, IFLG is a write-clear bit, which is cleared by writing 1;
116 * on other platforms, it is a normal r/w bit, which is cleared by writing 0.
904dfbfd
HG
117 */
118
119#ifdef CONFIG_SUNXI_GEN_SUN6I
120#define MVTWSI_CONTROL_CLEAR_IFLG 0x00000008
121#else
122#define MVTWSI_CONTROL_CLEAR_IFLG 0x00000000
123#endif
124
306563a7 125/*
dfc3958c 126 * enum mvstwsi_status_values - Possible values of I2C controller's status
127 * register
128 *
129 * Only those statuses expected in normal master operation on
130 * non-10-bit-address devices are specified.
131 *
132 * Every status that's unexpected during normal operation (bus errors,
133 * arbitration losses, missing ACKs...) is passed back to the caller as an error
306563a7
AA
134 * code.
135 */
dfc3958c 136enum mvstwsi_status_values {
137 /* START condition transmitted */
138 MVTWSI_STATUS_START = 0x08,
139 /* Repeated START condition transmitted */
140 MVTWSI_STATUS_REPEATED_START = 0x10,
141 /* Address + write bit transmitted, ACK received */
142 MVTWSI_STATUS_ADDR_W_ACK = 0x18,
143 /* Data transmitted, ACK received */
144 MVTWSI_STATUS_DATA_W_ACK = 0x28,
145 /* Address + read bit transmitted, ACK received */
146 MVTWSI_STATUS_ADDR_R_ACK = 0x40,
147 /* Address + read bit transmitted, ACK not received */
148 MVTWSI_STATUS_ADDR_R_NAK = 0x48,
149 /* Data received, ACK transmitted */
150 MVTWSI_STATUS_DATA_R_ACK = 0x50,
151 /* Data received, ACK not transmitted */
152 MVTWSI_STATUS_DATA_R_NAK = 0x58,
153 /* No relevant status */
154 MVTWSI_STATUS_IDLE = 0xF8,
155};
306563a7 156
670514f5 157/*
158 * enum mvstwsi_ack_flags - Determine whether a read byte should be
159 * acknowledged or not.
160 */
161enum mvtwsi_ack_flags {
162 /* Send NAK after received byte */
163 MVTWSI_READ_NAK = 0,
164 /* Send ACK after received byte */
165 MVTWSI_READ_ACK = 1,
166};
167
6e677caf 168/*
169 * calc_tick() - Calculate the duration of a clock cycle from the I2C speed
170 *
171 * @speed: The speed in Hz to calculate the clock cycle duration for.
172 * @return The duration of a clock cycle in ns.
173 */
c68c6243 174inline uint calc_tick(uint speed)
175{
176 /* One tick = the duration of a period at the specified speed in ns (we
177 * add 100 ns to be on the safe side) */
178 return (1000000000u / speed) + 100;
179}
180
14a6ff2c 181#ifndef CONFIG_DM_I2C
c68c6243 182
306563a7 183/*
6e677caf 184 * twsi_get_base() - Get controller register base for specified adapter
185 *
186 * @adap: Adapter to get the register base for.
187 * @return Register base for the specified adapter.
306563a7 188 */
dd82242b
PK
189static struct mvtwsi_registers *twsi_get_base(struct i2c_adapter *adap)
190{
191 switch (adap->hwadapnr) {
192#ifdef CONFIG_I2C_MVTWSI_BASE0
193 case 0:
9ec43b0c 194 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE0;
dd82242b
PK
195#endif
196#ifdef CONFIG_I2C_MVTWSI_BASE1
197 case 1:
9ec43b0c 198 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE1;
dd82242b
PK
199#endif
200#ifdef CONFIG_I2C_MVTWSI_BASE2
201 case 2:
9ec43b0c 202 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE2;
dd82242b
PK
203#endif
204#ifdef CONFIG_I2C_MVTWSI_BASE3
205 case 3:
9ec43b0c 206 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE3;
dd82242b
PK
207#endif
208#ifdef CONFIG_I2C_MVTWSI_BASE4
209 case 4:
9ec43b0c 210 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE4;
9d082687
JW
211#endif
212#ifdef CONFIG_I2C_MVTWSI_BASE5
213 case 5:
9ec43b0c 214 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE5;
dd82242b
PK
215#endif
216 default:
217 printf("Missing mvtwsi controller %d base\n", adap->hwadapnr);
218 break;
219 }
220
221 return NULL;
222}
14a6ff2c 223#endif
4ce5a728
HS
224
225/*
dfc3958c 226 * enum mvtwsi_error_class - types of I2C errors
4ce5a728 227 */
dfc3958c 228enum mvtwsi_error_class {
229 /* The controller returned a different status than expected */
230 MVTWSI_ERROR_WRONG_STATUS = 0x01,
231 /* The controller timed out */
232 MVTWSI_ERROR_TIMEOUT = 0x02,
233};
4ce5a728 234
dfc3958c 235/*
236 * mvtwsi_error() - Build I2C return code from error information
237 *
238 * For debugging purposes, this function packs some information of an occurred
239 * error into a return code. These error codes are returned from I2C API
240 * functions (i2c_{read,write}, dm_i2c_{read,write}, etc.).
241 *
242 * @ec: The error class of the error (enum mvtwsi_error_class).
243 * @lc: The last value of the control register.
244 * @ls: The last value of the status register.
245 * @es: The expected value of the status register.
246 * @return The generated error code.
247 */
248inline uint mvtwsi_error(uint ec, uint lc, uint ls, uint es)
249{
250 return ((ec << 24) & 0xFF000000)
251 | ((lc << 16) & 0x00FF0000)
252 | ((ls << 8) & 0x0000FF00)
253 | (es & 0xFF);
254}
4ce5a728 255
306563a7 256/*
6e677caf 257 * twsi_wait() - Wait for I2C bus interrupt flag and check status, or time out.
258 *
259 * @return Zero if status is as expected, or a non-zero code if either a time
260 * out occurred, or the status was not the expected one.
306563a7 261 */
c68c6243 262static int twsi_wait(struct mvtwsi_registers *twsi, int expected_status,
263 uint tick)
306563a7
AA
264{
265 int control, status;
266 int timeout = 1000;
267
268 do {
269 control = readl(&twsi->control);
270 if (control & MVTWSI_CONTROL_IFLG) {
271 status = readl(&twsi->status);
272 if (status == expected_status)
273 return 0;
274 else
dfc3958c 275 return mvtwsi_error(
306563a7
AA
276 MVTWSI_ERROR_WRONG_STATUS,
277 control, status, expected_status);
4ce5a728 278 }
c68c6243 279 ndelay(tick); /* One clock cycle */
306563a7
AA
280 } while (timeout--);
281 status = readl(&twsi->status);
dfc3958c 282 return mvtwsi_error(MVTWSI_ERROR_TIMEOUT, control, status,
283 expected_status);
4ce5a728
HS
284}
285
306563a7 286/*
6e677caf 287 * twsi_start() - Assert a START condition on the bus.
288 *
289 * This function is used in both single I2C transactions and inside
290 * back-to-back transactions (repeated starts).
291 *
292 * @twsi: The MVTWSI register structure to use.
293 * @expected_status: The I2C bus status expected to be asserted after the
294 * operation completion.
295 * @tick: The duration of a clock cycle at the current I2C speed.
296 * @return Zero if status is as expected, or a non-zero code if either a time
297 * out occurred or the status was not the expected one.
306563a7 298 */
c68c6243 299static int twsi_start(struct mvtwsi_registers *twsi, int expected_status,
300 uint tick)
4ce5a728 301{
49c801bf 302 /* Assert START */
670514f5 303 writel(MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_START |
49c801bf 304 MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
305 /* Wait for controller to process START */
c68c6243 306 return twsi_wait(twsi, expected_status, tick);
4ce5a728
HS
307}
308
306563a7 309/*
6e677caf 310 * twsi_send() - Send a byte on the I2C bus.
311 *
312 * The byte may be part of an address byte or data.
313 *
314 * @twsi: The MVTWSI register structure to use.
315 * @byte: The byte to send.
316 * @expected_status: The I2C bus status expected to be asserted after the
317 * operation completion.
318 * @tick: The duration of a clock cycle at the current I2C speed.
319 * @return Zero if status is as expected, or a non-zero code if either a time
320 * out occurred or the status was not the expected one.
306563a7 321 */
3c4db636 322static int twsi_send(struct mvtwsi_registers *twsi, u8 byte,
c68c6243 323 int expected_status, uint tick)
4ce5a728 324{
49c801bf 325 /* Write byte to data register for sending */
306563a7 326 writel(byte, &twsi->data);
49c801bf 327 /* Clear any pending interrupt -- that will cause sending */
670514f5 328 writel(MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_CLEAR_IFLG,
329 &twsi->control);
49c801bf 330 /* Wait for controller to receive byte, and check ACK */
c68c6243 331 return twsi_wait(twsi, expected_status, tick);
4ce5a728
HS
332}
333
306563a7 334/*
6e677caf 335 * twsi_recv() - Receive a byte on the I2C bus.
336 *
337 * The static variable mvtwsi_control_flags controls whether we ack or nak.
338 *
339 * @twsi: The MVTWSI register structure to use.
340 * @byte: The byte to send.
341 * @ack_flag: Flag that determines whether the received byte should
342 * be acknowledged by the controller or not (sent ACK/NAK).
343 * @tick: The duration of a clock cycle at the current I2C speed.
344 * @return Zero if status is as expected, or a non-zero code if either a time
345 * out occurred or the status was not the expected one.
306563a7 346 */
c68c6243 347static int twsi_recv(struct mvtwsi_registers *twsi, u8 *byte, int ack_flag,
348 uint tick)
4ce5a728 349{
670514f5 350 int expected_status, status, control;
306563a7 351
670514f5 352 /* Compute expected status based on passed ACK flag */
353 expected_status = ack_flag ? MVTWSI_STATUS_DATA_R_ACK :
354 MVTWSI_STATUS_DATA_R_NAK;
49c801bf 355 /* Acknowledge *previous state*, and launch receive */
670514f5 356 control = MVTWSI_CONTROL_TWSIEN;
357 control |= ack_flag == MVTWSI_READ_ACK ? MVTWSI_CONTROL_ACK : 0;
358 writel(control | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
49c801bf 359 /* Wait for controller to receive byte, and assert ACK or NAK */
c68c6243 360 status = twsi_wait(twsi, expected_status, tick);
49c801bf 361 /* If we did receive the expected byte, store it */
306563a7
AA
362 if (status == 0)
363 *byte = readl(&twsi->data);
306563a7 364 return status;
4ce5a728
HS
365}
366
306563a7 367/*
6e677caf 368 * twsi_stop() - Assert a STOP condition on the bus.
369 *
370 * This function is also used to force the bus back to idle state (SDA =
371 * SCL = 1).
372 *
373 * @twsi: The MVTWSI register structure to use.
374 * @tick: The duration of a clock cycle at the current I2C speed.
375 * @return Zero if the operation succeeded, or a non-zero code if a time out
376 * occurred.
306563a7 377 */
c68c6243 378static int twsi_stop(struct mvtwsi_registers *twsi, uint tick)
4ce5a728 379{
306563a7 380 int control, stop_status;
059fce9f 381 int status = 0;
306563a7
AA
382 int timeout = 1000;
383
49c801bf 384 /* Assert STOP */
306563a7 385 control = MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_STOP;
904dfbfd 386 writel(control | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
49c801bf 387 /* Wait for IDLE; IFLG won't rise, so we can't use twsi_wait() */
306563a7
AA
388 do {
389 stop_status = readl(&twsi->status);
390 if (stop_status == MVTWSI_STATUS_IDLE)
391 break;
c68c6243 392 ndelay(tick); /* One clock cycle */
306563a7
AA
393 } while (timeout--);
394 control = readl(&twsi->control);
395 if (stop_status != MVTWSI_STATUS_IDLE)
059fce9f 396 status = mvtwsi_error(MVTWSI_ERROR_TIMEOUT,
397 control, status, MVTWSI_STATUS_IDLE);
306563a7 398 return status;
4ce5a728
HS
399}
400
6e677caf 401/*
402 * twsi_calc_freq() - Compute I2C frequency depending on m and n parameters.
403 *
404 * @n: Parameter 'n' for the frequency calculation algorithm.
405 * @m: Parameter 'm' for the frequency calculation algorithm.
406 * @return The I2C frequency corresponding to the passed m and n parameters.
407 */
e0758281 408static uint twsi_calc_freq(const int n, const int m)
f582a158 409{
aec9a0f1 410#ifdef CONFIG_ARCH_SUNXI
f582a158
SR
411 return CONFIG_SYS_TCLK / (10 * (m + 1) * (1 << n));
412#else
413 return CONFIG_SYS_TCLK / (10 * (m + 1) * (2 << n));
414#endif
415}
306563a7 416
306563a7 417/*
6e677caf 418 * twsi_reset() - Reset the I2C controller.
419 *
420 * Resetting the controller also resets the baud rate and slave address, hence
421 * they must be re-established after the reset.
422 *
423 * @twsi: The MVTWSI register structure to use.
306563a7 424 */
3c4db636 425static void twsi_reset(struct mvtwsi_registers *twsi)
306563a7 426{
49c801bf 427 /* Reset controller */
306563a7 428 writel(0, &twsi->soft_reset);
49c801bf 429 /* Wait 2 ms -- this is what the Marvell LSP does */
306563a7 430 udelay(20000);
4ce5a728
HS
431}
432
306563a7 433/*
6e677caf 434 * __twsi_i2c_set_bus_speed() - Set the speed of the I2C controller.
435 *
436 * This function sets baud rate to the highest possible value that does not
437 * exceed the requested rate.
438 *
439 * @twsi: The MVTWSI register structure to use.
440 * @requested_speed: The desired frequency the controller should run at
441 * in Hz.
442 * @return The actual frequency the controller was configured to.
306563a7 443 */
3c4db636 444static uint __twsi_i2c_set_bus_speed(struct mvtwsi_registers *twsi,
61bc02b2 445 uint requested_speed)
4ce5a728 446{
e0758281 447 uint tmp_speed, highest_speed, n, m;
448 uint baud = 0x44; /* Baud rate after controller reset */
306563a7 449
306563a7 450 highest_speed = 0;
49c801bf 451 /* Successively try m, n combinations, and use the combination
452 * resulting in the largest speed that's not above the requested
453 * speed */
306563a7
AA
454 for (n = 0; n < 8; n++) {
455 for (m = 0; m < 16; m++) {
f582a158 456 tmp_speed = twsi_calc_freq(n, m);
9ec43b0c 457 if ((tmp_speed <= requested_speed) &&
458 (tmp_speed > highest_speed)) {
306563a7
AA
459 highest_speed = tmp_speed;
460 baud = (m << 3) | n;
461 }
462 }
4ce5a728 463 }
0db2bbdc 464 writel(baud, &twsi->baudrate);
c68c6243 465
466 /* Wait for controller for one tick */
467#ifdef CONFIG_DM_I2C
468 ndelay(calc_tick(highest_speed));
469#else
470 ndelay(10000);
471#endif
472 return highest_speed;
0db2bbdc
HG
473}
474
6e677caf 475/*
476 * __twsi_i2c_init() - Initialize the I2C controller.
477 *
478 * @twsi: The MVTWSI register structure to use.
479 * @speed: The initial frequency the controller should run at
480 * in Hz.
481 * @slaveadd: The I2C address to be set for the I2C master.
482 * @actual_speed: A output parameter that receives the actual frequency
483 * in Hz the controller was set to by the function.
484 * @return Zero if the operation succeeded, or a non-zero code if a time out
485 * occurred.
486 */
3c4db636 487static void __twsi_i2c_init(struct mvtwsi_registers *twsi, int speed,
c68c6243 488 int slaveadd, uint *actual_speed)
0db2bbdc 489{
49c801bf 490 /* Reset controller */
3c4db636 491 twsi_reset(twsi);
49c801bf 492 /* Set speed */
8bcf12cc
HS
493 if (actual_speed)
494 *actual_speed = __twsi_i2c_set_bus_speed(twsi, speed);
49c801bf 495 /* Set slave address; even though we don't use it */
0db2bbdc
HG
496 writel(slaveadd, &twsi->slave_address);
497 writel(0, &twsi->xtnd_slave_addr);
49c801bf 498 /* Assert STOP, but don't care for the result */
c68c6243 499#ifdef CONFIG_DM_I2C
500 (void) twsi_stop(twsi, calc_tick(*actual_speed));
501#else
502 (void) twsi_stop(twsi, 10000);
503#endif
4ce5a728
HS
504}
505
306563a7 506/*
6e677caf 507 * i2c_begin() - Start a I2C transaction.
508 *
509 * Begin a I2C transaction with a given expected start status and chip address.
510 * A START is asserted, and the address byte is sent to the I2C controller. The
511 * expected address status will be derived from the direction bit (bit 0) of
512 * the address byte.
513 *
514 * @twsi: The MVTWSI register structure to use.
515 * @expected_start_status: The I2C status the controller is expected to
516 * assert after the address byte was sent.
517 * @addr: The address byte to be sent.
518 * @tick: The duration of a clock cycle at the current
519 * I2C speed.
520 * @return Zero if the operation succeeded, or a non-zero code if a time out or
521 * unexpected I2C status occurred.
306563a7 522 */
3c4db636 523static int i2c_begin(struct mvtwsi_registers *twsi, int expected_start_status,
c68c6243 524 u8 addr, uint tick)
4ce5a728 525{
306563a7
AA
526 int status, expected_addr_status;
527
49c801bf 528 /* Compute the expected address status from the direction bit in
529 * the address byte */
530 if (addr & 1) /* Reading */
306563a7 531 expected_addr_status = MVTWSI_STATUS_ADDR_R_ACK;
49c801bf 532 else /* Writing */
306563a7 533 expected_addr_status = MVTWSI_STATUS_ADDR_W_ACK;
49c801bf 534 /* Assert START */
c68c6243 535 status = twsi_start(twsi, expected_start_status, tick);
49c801bf 536 /* Send out the address if the start went well */
306563a7 537 if (status == 0)
c68c6243 538 status = twsi_send(twsi, addr, expected_addr_status, tick);
49c801bf 539 /* Return 0, or the status of the first failure */
306563a7 540 return status;
4ce5a728
HS
541}
542
306563a7 543/*
6e677caf 544 * __twsi_i2c_probe_chip() - Probe the given I2C chip address.
545 *
546 * This function begins a I2C read transaction, does a dummy read and NAKs; if
547 * the procedure succeeds, the chip is considered to be present.
548 *
549 * @twsi: The MVTWSI register structure to use.
550 * @chip: The chip address to probe.
551 * @tick: The duration of a clock cycle at the current I2C speed.
552 * @return Zero if the operation succeeded, or a non-zero code if a time out or
553 * unexpected I2C status occurred.
306563a7 554 */
c68c6243 555static int __twsi_i2c_probe_chip(struct mvtwsi_registers *twsi, uchar chip,
556 uint tick)
4ce5a728 557{
306563a7
AA
558 u8 dummy_byte;
559 int status;
560
49c801bf 561 /* Begin i2c read */
c68c6243 562 status = i2c_begin(twsi, MVTWSI_STATUS_START, (chip << 1) | 1, tick);
49c801bf 563 /* Dummy read was accepted: receive byte, but NAK it. */
306563a7 564 if (status == 0)
c68c6243 565 status = twsi_recv(twsi, &dummy_byte, MVTWSI_READ_NAK, tick);
306563a7 566 /* Stop transaction */
c68c6243 567 twsi_stop(twsi, tick);
49c801bf 568 /* Return 0, or the status of the first failure */
306563a7 569 return status;
4ce5a728
HS
570}
571
306563a7 572/*
6e677caf 573 * __twsi_i2c_read() - Read data from a I2C chip.
574 *
575 * This function begins a I2C write transaction, and transmits the address
576 * bytes; then begins a I2C read transaction, and receives the data bytes.
306563a7 577 *
49c801bf 578 * NOTE: Some devices want a stop right before the second start, while some
579 * will choke if it is there. Since deciding this is not yet supported in
580 * higher level APIs, we need to make a decision here, and for the moment that
581 * will be a repeated start without a preceding stop.
6e677caf 582 *
583 * @twsi: The MVTWSI register structure to use.
584 * @chip: The chip address to read from.
585 * @addr: The address bytes to send.
586 * @alen: The length of the address bytes in bytes.
587 * @data: The buffer to receive the data read from the chip (has to have
588 * a size of at least 'length' bytes).
589 * @length: The amount of data to be read from the chip in bytes.
590 * @tick: The duration of a clock cycle at the current I2C speed.
591 * @return Zero if the operation succeeded, or a non-zero code if a time out or
592 * unexpected I2C status occurred.
306563a7 593 */
3c4db636 594static int __twsi_i2c_read(struct mvtwsi_registers *twsi, uchar chip,
c68c6243 595 u8 *addr, int alen, uchar *data, int length,
596 uint tick)
4ce5a728 597{
059fce9f 598 int status = 0;
599 int stop_status;
24f9c6bb 600 int expected_start = MVTWSI_STATUS_START;
601
602 if (alen > 0) {
603 /* Begin i2c write to send the address bytes */
c68c6243 604 status = i2c_begin(twsi, expected_start, (chip << 1), tick);
24f9c6bb 605 /* Send address bytes */
606 while ((status == 0) && alen--)
03d6cd97 607 status = twsi_send(twsi, addr[alen],
c68c6243 608 MVTWSI_STATUS_DATA_W_ACK, tick);
24f9c6bb 609 /* Send repeated STARTs after the initial START */
610 expected_start = MVTWSI_STATUS_REPEATED_START;
611 }
49c801bf 612 /* Begin i2c read to receive data bytes */
306563a7 613 if (status == 0)
c68c6243 614 status = i2c_begin(twsi, expected_start, (chip << 1) | 1, tick);
670514f5 615 /* Receive actual data bytes; set NAK if we if we have nothing more to
616 * read */
617 while ((status == 0) && length--)
3c4db636 618 status = twsi_recv(twsi, data++,
670514f5 619 length > 0 ?
c68c6243 620 MVTWSI_READ_ACK : MVTWSI_READ_NAK, tick);
306563a7 621 /* Stop transaction */
c68c6243 622 stop_status = twsi_stop(twsi, tick);
49c801bf 623 /* Return 0, or the status of the first failure */
059fce9f 624 return status != 0 ? status : stop_status;
4ce5a728
HS
625}
626
306563a7 627/*
6e677caf 628 * __twsi_i2c_write() - Send data to a I2C chip.
629 *
630 * This function begins a I2C write transaction, and transmits the address
631 * bytes; then begins a new I2C write transaction, and sends the data bytes.
632 *
633 * @twsi: The MVTWSI register structure to use.
634 * @chip: The chip address to read from.
635 * @addr: The address bytes to send.
636 * @alen: The length of the address bytes in bytes.
637 * @data: The buffer containing the data to be sent to the chip.
638 * @length: The length of data to be sent to the chip in bytes.
639 * @tick: The duration of a clock cycle at the current I2C speed.
640 * @return Zero if the operation succeeded, or a non-zero code if a time out or
641 * unexpected I2C status occurred.
306563a7 642 */
3c4db636 643static int __twsi_i2c_write(struct mvtwsi_registers *twsi, uchar chip,
c68c6243 644 u8 *addr, int alen, uchar *data, int length,
645 uint tick)
4ce5a728 646{
059fce9f 647 int status, stop_status;
306563a7 648
49c801bf 649 /* Begin i2c write to send first the address bytes, then the
650 * data bytes */
c68c6243 651 status = i2c_begin(twsi, MVTWSI_STATUS_START, (chip << 1), tick);
49c801bf 652 /* Send address bytes */
f8a10ed1 653 while ((status == 0) && (alen-- > 0))
03d6cd97 654 status = twsi_send(twsi, addr[alen], MVTWSI_STATUS_DATA_W_ACK,
c68c6243 655 tick);
49c801bf 656 /* Send data bytes */
306563a7 657 while ((status == 0) && (length-- > 0))
c68c6243 658 status = twsi_send(twsi, *(data++), MVTWSI_STATUS_DATA_W_ACK,
659 tick);
306563a7 660 /* Stop transaction */
c68c6243 661 stop_status = twsi_stop(twsi, tick);
49c801bf 662 /* Return 0, or the status of the first failure */
059fce9f 663 return status != 0 ? status : stop_status;
4ce5a728
HS
664}
665
14a6ff2c 666#ifndef CONFIG_DM_I2C
61bc02b2 667static void twsi_i2c_init(struct i2c_adapter *adap, int speed,
668 int slaveadd)
669{
3c4db636 670 struct mvtwsi_registers *twsi = twsi_get_base(adap);
c68c6243 671 __twsi_i2c_init(twsi, speed, slaveadd, NULL);
61bc02b2 672}
673
674static uint twsi_i2c_set_bus_speed(struct i2c_adapter *adap,
675 uint requested_speed)
676{
3c4db636 677 struct mvtwsi_registers *twsi = twsi_get_base(adap);
c68c6243 678 __twsi_i2c_set_bus_speed(twsi, requested_speed);
679 return 0;
61bc02b2 680}
681
682static int twsi_i2c_probe(struct i2c_adapter *adap, uchar chip)
683{
3c4db636 684 struct mvtwsi_registers *twsi = twsi_get_base(adap);
c68c6243 685 return __twsi_i2c_probe_chip(twsi, chip, 10000);
61bc02b2 686}
687
688static int twsi_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
689 int alen, uchar *data, int length)
690{
3c4db636 691 struct mvtwsi_registers *twsi = twsi_get_base(adap);
f8a10ed1 692 u8 addr_bytes[4];
693
694 addr_bytes[0] = (addr >> 0) & 0xFF;
695 addr_bytes[1] = (addr >> 8) & 0xFF;
696 addr_bytes[2] = (addr >> 16) & 0xFF;
697 addr_bytes[3] = (addr >> 24) & 0xFF;
698
c68c6243 699 return __twsi_i2c_read(twsi, chip, addr_bytes, alen, data, length,
700 10000);
61bc02b2 701}
702
703static int twsi_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
704 int alen, uchar *data, int length)
705{
3c4db636 706 struct mvtwsi_registers *twsi = twsi_get_base(adap);
f8a10ed1 707 u8 addr_bytes[4];
708
709 addr_bytes[0] = (addr >> 0) & 0xFF;
710 addr_bytes[1] = (addr >> 8) & 0xFF;
711 addr_bytes[2] = (addr >> 16) & 0xFF;
712 addr_bytes[3] = (addr >> 24) & 0xFF;
713
c68c6243 714 return __twsi_i2c_write(twsi, chip, addr_bytes, alen, data, length,
715 10000);
61bc02b2 716}
717
dd82242b 718#ifdef CONFIG_I2C_MVTWSI_BASE0
0db2bbdc
HG
719U_BOOT_I2C_ADAP_COMPLETE(twsi0, twsi_i2c_init, twsi_i2c_probe,
720 twsi_i2c_read, twsi_i2c_write,
721 twsi_i2c_set_bus_speed,
722 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0)
dd82242b
PK
723#endif
724#ifdef CONFIG_I2C_MVTWSI_BASE1
725U_BOOT_I2C_ADAP_COMPLETE(twsi1, twsi_i2c_init, twsi_i2c_probe,
726 twsi_i2c_read, twsi_i2c_write,
727 twsi_i2c_set_bus_speed,
728 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 1)
729
730#endif
731#ifdef CONFIG_I2C_MVTWSI_BASE2
732U_BOOT_I2C_ADAP_COMPLETE(twsi2, twsi_i2c_init, twsi_i2c_probe,
733 twsi_i2c_read, twsi_i2c_write,
734 twsi_i2c_set_bus_speed,
735 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 2)
736
737#endif
738#ifdef CONFIG_I2C_MVTWSI_BASE3
739U_BOOT_I2C_ADAP_COMPLETE(twsi3, twsi_i2c_init, twsi_i2c_probe,
740 twsi_i2c_read, twsi_i2c_write,
741 twsi_i2c_set_bus_speed,
742 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 3)
743
744#endif
745#ifdef CONFIG_I2C_MVTWSI_BASE4
746U_BOOT_I2C_ADAP_COMPLETE(twsi4, twsi_i2c_init, twsi_i2c_probe,
747 twsi_i2c_read, twsi_i2c_write,
748 twsi_i2c_set_bus_speed,
749 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 4)
750
751#endif
9d082687
JW
752#ifdef CONFIG_I2C_MVTWSI_BASE5
753U_BOOT_I2C_ADAP_COMPLETE(twsi5, twsi_i2c_init, twsi_i2c_probe,
754 twsi_i2c_read, twsi_i2c_write,
755 twsi_i2c_set_bus_speed,
756 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 5)
757
758#endif
14a6ff2c 759#else /* CONFIG_DM_I2C */
760
761static int mvtwsi_i2c_probe_chip(struct udevice *bus, u32 chip_addr,
762 u32 chip_flags)
763{
764 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
c68c6243 765 return __twsi_i2c_probe_chip(dev->base, chip_addr, dev->tick);
14a6ff2c 766}
767
768static int mvtwsi_i2c_set_bus_speed(struct udevice *bus, uint speed)
769{
770 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
c68c6243 771
772 dev->speed = __twsi_i2c_set_bus_speed(dev->base, speed);
773 dev->tick = calc_tick(dev->speed);
774
775 return 0;
14a6ff2c 776}
777
778static int mvtwsi_i2c_ofdata_to_platdata(struct udevice *bus)
779{
780 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
781
a821c4af 782 dev->base = devfdt_get_addr_ptr(bus);
14a6ff2c 783
784 if (!dev->base)
785 return -ENOMEM;
786
e160f7d4 787 dev->index = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus),
14a6ff2c 788 "cell-index", -1);
e160f7d4 789 dev->slaveadd = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus),
14a6ff2c 790 "u-boot,i2c-slave-addr", 0x0);
e160f7d4 791 dev->speed = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus),
14a6ff2c 792 "clock-frequency", 100000);
793 return 0;
794}
795
796static int mvtwsi_i2c_probe(struct udevice *bus)
797{
798 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
c68c6243 799 uint actual_speed;
800
801 __twsi_i2c_init(dev->base, dev->speed, dev->slaveadd, &actual_speed);
802 dev->speed = actual_speed;
803 dev->tick = calc_tick(dev->speed);
14a6ff2c 804 return 0;
805}
806
807static int mvtwsi_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
808{
809 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
810 struct i2c_msg *dmsg, *omsg, dummy;
811
812 memset(&dummy, 0, sizeof(struct i2c_msg));
813
814 /* We expect either two messages (one with an offset and one with the
815 * actual data) or one message (just data or offset/data combined) */
816 if (nmsgs > 2 || nmsgs == 0) {
817 debug("%s: Only one or two messages are supported.", __func__);
818 return -1;
819 }
820
821 omsg = nmsgs == 1 ? &dummy : msg;
822 dmsg = nmsgs == 1 ? msg : msg + 1;
823
824 if (dmsg->flags & I2C_M_RD)
825 return __twsi_i2c_read(dev->base, dmsg->addr, omsg->buf,
c68c6243 826 omsg->len, dmsg->buf, dmsg->len,
827 dev->tick);
14a6ff2c 828 else
829 return __twsi_i2c_write(dev->base, dmsg->addr, omsg->buf,
c68c6243 830 omsg->len, dmsg->buf, dmsg->len,
831 dev->tick);
14a6ff2c 832}
833
834static const struct dm_i2c_ops mvtwsi_i2c_ops = {
835 .xfer = mvtwsi_i2c_xfer,
836 .probe_chip = mvtwsi_i2c_probe_chip,
837 .set_bus_speed = mvtwsi_i2c_set_bus_speed,
838};
839
840static const struct udevice_id mvtwsi_i2c_ids[] = {
841 { .compatible = "marvell,mv64xxx-i2c", },
87de0eb3 842 { .compatible = "marvell,mv78230-i2c", },
a8f01ccf 843 { .compatible = "allwinner,sun6i-a31-i2c", },
14a6ff2c 844 { /* sentinel */ }
845};
846
847U_BOOT_DRIVER(i2c_mvtwsi) = {
848 .name = "i2c_mvtwsi",
849 .id = UCLASS_I2C,
850 .of_match = mvtwsi_i2c_ids,
851 .probe = mvtwsi_i2c_probe,
852 .ofdata_to_platdata = mvtwsi_i2c_ofdata_to_platdata,
853 .priv_auto_alloc_size = sizeof(struct mvtwsi_i2c_dev),
854 .ops = &mvtwsi_i2c_ops,
855};
856#endif /* CONFIG_DM_I2C */