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