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