blob: bf4443287fd2977fe3a61f3f653327ab3e013323 [file] [log] [blame]
Heiko Schocher5cd2a242009-07-20 09:59:37 +02001/*
Albert Aribaud04280c42010-08-27 18:26:05 +02002 * Driver for the TWSI (i2c) controller found on the Marvell
3 * orion5x and kirkwood SoC families.
Heiko Schocher5cd2a242009-07-20 09:59:37 +02004 *
Albert ARIBAUD340983d2011-04-22 19:41:02 +02005 * Author: Albert Aribaud <albert.u.boot@aribaud.net>
Albert Aribaud04280c42010-08-27 18:26:05 +02006 * Copyright (c) 2010 Albert Aribaud.
Heiko Schocher5cd2a242009-07-20 09:59:37 +02007 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
Heiko Schocher5cd2a242009-07-20 09:59:37 +02009 */
Albert Aribaud04280c42010-08-27 18:26:05 +020010
Heiko Schocher5cd2a242009-07-20 09:59:37 +020011#include <common.h>
12#include <i2c.h>
Heiko Schocher5cd2a242009-07-20 09:59:37 +020013#include <asm/errno.h>
14#include <asm/io.h>
15
Albert Aribaud04280c42010-08-27 18:26:05 +020016/*
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +020017 * include a file that will provide CONFIG_I2C_MVTWSI_BASE*
Albert Aribaud04280c42010-08-27 18:26:05 +020018 * and possibly other settings
19 */
Heiko Schocher5cd2a242009-07-20 09:59:37 +020020
Albert Aribaud04280c42010-08-27 18:26:05 +020021#if defined(CONFIG_ORION5X)
22#include <asm/arch/orion5x.h>
Stefan Roeseeb083e52015-12-21 13:56:33 +010023#elif (defined(CONFIG_KIRKWOOD) || defined(CONFIG_ARCH_MVEBU))
Stefan Roesec2437842014-10-22 12:13:06 +020024#include <asm/arch/soc.h>
Hans de Goede3352b222014-06-13 22:55:49 +020025#elif defined(CONFIG_SUNXI)
26#include <asm/arch/i2c.h>
Albert Aribaud04280c42010-08-27 18:26:05 +020027#else
28#error Driver mvtwsi not supported by SoC or board
Heiko Schocher5cd2a242009-07-20 09:59:37 +020029#endif
30
Albert Aribaud04280c42010-08-27 18:26:05 +020031/*
32 * TWSI register structure
33 */
Heiko Schocher5cd2a242009-07-20 09:59:37 +020034
Hans de Goede3352b222014-06-13 22:55:49 +020035#ifdef CONFIG_SUNXI
36
Albert Aribaud04280c42010-08-27 18:26:05 +020037struct mvtwsi_registers {
38 u32 slave_address;
Hans de Goede3352b222014-06-13 22:55:49 +020039 u32 xtnd_slave_addr;
Albert Aribaud04280c42010-08-27 18:26:05 +020040 u32 data;
41 u32 control;
Hans de Goede3352b222014-06-13 22:55:49 +020042 u32 status;
43 u32 baudrate;
44 u32 soft_reset;
45};
46
47#else
48
49struct mvtwsi_registers {
50 u32 slave_address;
51 u32 data;
52 u32 control;
Albert Aribaud04280c42010-08-27 18:26:05 +020053 union {
54 u32 status; /* when reading */
55 u32 baudrate; /* when writing */
56 };
57 u32 xtnd_slave_addr;
58 u32 reserved[2];
59 u32 soft_reset;
Heiko Schocher5cd2a242009-07-20 09:59:37 +020060};
61
Hans de Goede3352b222014-06-13 22:55:49 +020062#endif
63
Albert Aribaud04280c42010-08-27 18:26:05 +020064/*
65 * Control register fields
66 */
Heiko Schocher5cd2a242009-07-20 09:59:37 +020067
Albert Aribaud04280c42010-08-27 18:26:05 +020068#define MVTWSI_CONTROL_ACK 0x00000004
69#define MVTWSI_CONTROL_IFLG 0x00000008
70#define MVTWSI_CONTROL_STOP 0x00000010
71#define MVTWSI_CONTROL_START 0x00000020
72#define MVTWSI_CONTROL_TWSIEN 0x00000040
73#define MVTWSI_CONTROL_INTEN 0x00000080
Heiko Schocher5cd2a242009-07-20 09:59:37 +020074
Albert Aribaud04280c42010-08-27 18:26:05 +020075/*
Hans de Goede6b703e02016-01-14 14:06:25 +010076 * On sun6i and newer IFLG is a write-clear bit which is cleared by writing 1,
77 * on other platforms it is a normal r/w bit which is cleared by writing 0.
78 */
79
80#ifdef CONFIG_SUNXI_GEN_SUN6I
81#define MVTWSI_CONTROL_CLEAR_IFLG 0x00000008
82#else
83#define MVTWSI_CONTROL_CLEAR_IFLG 0x00000000
84#endif
85
86/*
Albert Aribaud04280c42010-08-27 18:26:05 +020087 * Status register values -- only those expected in normal master
88 * operation on non-10-bit-address devices; whatever status we don't
89 * expect in nominal conditions (bus errors, arbitration losses,
90 * missing ACKs...) we just pass back to the caller as an error
91 * code.
92 */
Heiko Schocher5cd2a242009-07-20 09:59:37 +020093
Albert Aribaud04280c42010-08-27 18:26:05 +020094#define MVTWSI_STATUS_START 0x08
95#define MVTWSI_STATUS_REPEATED_START 0x10
96#define MVTWSI_STATUS_ADDR_W_ACK 0x18
97#define MVTWSI_STATUS_DATA_W_ACK 0x28
98#define MVTWSI_STATUS_ADDR_R_ACK 0x40
99#define MVTWSI_STATUS_ADDR_R_NAK 0x48
100#define MVTWSI_STATUS_DATA_R_ACK 0x50
101#define MVTWSI_STATUS_DATA_R_NAK 0x58
102#define MVTWSI_STATUS_IDLE 0xF8
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200103
Albert Aribaud04280c42010-08-27 18:26:05 +0200104/*
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200105 * MVTWSI controller base
Albert Aribaud04280c42010-08-27 18:26:05 +0200106 */
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200107
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200108static struct mvtwsi_registers *twsi_get_base(struct i2c_adapter *adap)
109{
110 switch (adap->hwadapnr) {
111#ifdef CONFIG_I2C_MVTWSI_BASE0
112 case 0:
113 return (struct mvtwsi_registers *) CONFIG_I2C_MVTWSI_BASE0;
114#endif
115#ifdef CONFIG_I2C_MVTWSI_BASE1
116 case 1:
117 return (struct mvtwsi_registers *) CONFIG_I2C_MVTWSI_BASE1;
118#endif
119#ifdef CONFIG_I2C_MVTWSI_BASE2
120 case 2:
121 return (struct mvtwsi_registers *) CONFIG_I2C_MVTWSI_BASE2;
122#endif
123#ifdef CONFIG_I2C_MVTWSI_BASE3
124 case 3:
125 return (struct mvtwsi_registers *) CONFIG_I2C_MVTWSI_BASE3;
126#endif
127#ifdef CONFIG_I2C_MVTWSI_BASE4
128 case 4:
129 return (struct mvtwsi_registers *) CONFIG_I2C_MVTWSI_BASE4;
130#endif
Jelle van der Waa8d3d7c12016-01-14 14:06:26 +0100131#ifdef CONFIG_I2C_MVTWSI_BASE5
132 case 5:
133 return (struct mvtwsi_registers *) CONFIG_I2C_MVTWSI_BASE5;
134#endif
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200135 default:
136 printf("Missing mvtwsi controller %d base\n", adap->hwadapnr);
137 break;
138 }
139
140 return NULL;
141}
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200142
143/*
Albert Aribaud04280c42010-08-27 18:26:05 +0200144 * Returned statuses are 0 for success and nonzero otherwise.
145 * Currently, cmd_i2c and cmd_eeprom do not interpret an error status.
146 * Thus to ease debugging, the return status contains some debug info:
147 * - bits 31..24 are error class: 1 is timeout, 2 is 'status mismatch'.
148 * - bits 23..16 are the last value of the control register.
149 * - bits 15..8 are the last value of the status register.
150 * - bits 7..0 are the expected value of the status register.
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200151 */
152
Albert Aribaud04280c42010-08-27 18:26:05 +0200153#define MVTWSI_ERROR_WRONG_STATUS 0x01
154#define MVTWSI_ERROR_TIMEOUT 0x02
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200155
Albert Aribaud04280c42010-08-27 18:26:05 +0200156#define MVTWSI_ERROR(ec, lc, ls, es) (((ec << 24) & 0xFF000000) | \
157 ((lc << 16) & 0x00FF0000) | ((ls<<8) & 0x0000FF00) | (es & 0xFF))
158
159/*
160 * Wait for IFLG to raise, or return 'timeout'; then if status is as expected,
161 * return 0 (ok) or return 'wrong status'.
162 */
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200163static int twsi_wait(struct i2c_adapter *adap, int expected_status)
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200164{
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200165 struct mvtwsi_registers *twsi = twsi_get_base(adap);
Albert Aribaud04280c42010-08-27 18:26:05 +0200166 int control, status;
167 int timeout = 1000;
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200168
Albert Aribaud04280c42010-08-27 18:26:05 +0200169 do {
170 control = readl(&twsi->control);
171 if (control & MVTWSI_CONTROL_IFLG) {
172 status = readl(&twsi->status);
173 if (status == expected_status)
174 return 0;
175 else
176 return MVTWSI_ERROR(
177 MVTWSI_ERROR_WRONG_STATUS,
178 control, status, expected_status);
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200179 }
Albert Aribaud04280c42010-08-27 18:26:05 +0200180 udelay(10); /* one clock cycle at 100 kHz */
181 } while (timeout--);
182 status = readl(&twsi->status);
183 return MVTWSI_ERROR(
184 MVTWSI_ERROR_TIMEOUT, control, status, expected_status);
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200185}
186
Albert Aribaud04280c42010-08-27 18:26:05 +0200187/*
Albert Aribaud04280c42010-08-27 18:26:05 +0200188 * Assert the START condition, either in a single I2C transaction
189 * or inside back-to-back ones (repeated starts).
190 */
Chris Packhamc42be012016-05-13 15:19:31 +1200191static int twsi_start(struct i2c_adapter *adap, int expected_status, u8 *flags)
Albert Aribaud04280c42010-08-27 18:26:05 +0200192{
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200193 struct mvtwsi_registers *twsi = twsi_get_base(adap);
194
Albert Aribaud04280c42010-08-27 18:26:05 +0200195 /* globally set TWSIEN in case it was not */
Chris Packhamc42be012016-05-13 15:19:31 +1200196 *flags |= MVTWSI_CONTROL_TWSIEN;
Albert Aribaud04280c42010-08-27 18:26:05 +0200197 /* assert START */
Chris Packhamc42be012016-05-13 15:19:31 +1200198 writel(*flags | MVTWSI_CONTROL_START |
Hans de Goede5a092092016-01-26 16:51:14 +0100199 MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
Albert Aribaud04280c42010-08-27 18:26:05 +0200200 /* wait for controller to process START */
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200201 return twsi_wait(adap, expected_status);
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200202}
203
Albert Aribaud04280c42010-08-27 18:26:05 +0200204/*
205 * Send a byte (i2c address or data).
206 */
Chris Packhamc42be012016-05-13 15:19:31 +1200207static int twsi_send(struct i2c_adapter *adap, u8 byte, int expected_status,
208 u8 *flags)
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200209{
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200210 struct mvtwsi_registers *twsi = twsi_get_base(adap);
211
Albert Aribaud04280c42010-08-27 18:26:05 +0200212 /* put byte in data register for sending */
213 writel(byte, &twsi->data);
214 /* clear any pending interrupt -- that'll cause sending */
Chris Packhamc42be012016-05-13 15:19:31 +1200215 writel(*flags | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
Albert Aribaud04280c42010-08-27 18:26:05 +0200216 /* wait for controller to receive byte and check ACK */
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200217 return twsi_wait(adap, expected_status);
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200218}
219
Albert Aribaud04280c42010-08-27 18:26:05 +0200220/*
221 * Receive a byte.
222 * Global mvtwsi_control_flags variable says if we should ack or nak.
223 */
Chris Packhamc42be012016-05-13 15:19:31 +1200224static int twsi_recv(struct i2c_adapter *adap, u8 *byte, u8 *flags)
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200225{
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200226 struct mvtwsi_registers *twsi = twsi_get_base(adap);
Albert Aribaud04280c42010-08-27 18:26:05 +0200227 int expected_status, status;
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200228
Albert Aribaud04280c42010-08-27 18:26:05 +0200229 /* compute expected status based on ACK bit in global control flags */
Chris Packhamc42be012016-05-13 15:19:31 +1200230 if (*flags & MVTWSI_CONTROL_ACK)
Albert Aribaud04280c42010-08-27 18:26:05 +0200231 expected_status = MVTWSI_STATUS_DATA_R_ACK;
232 else
233 expected_status = MVTWSI_STATUS_DATA_R_NAK;
234 /* acknowledge *previous state* and launch receive */
Chris Packhamc42be012016-05-13 15:19:31 +1200235 writel(*flags | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
Albert Aribaud04280c42010-08-27 18:26:05 +0200236 /* wait for controller to receive byte and assert ACK or NAK */
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200237 status = twsi_wait(adap, expected_status);
Albert Aribaud04280c42010-08-27 18:26:05 +0200238 /* if we did receive expected byte then store it */
239 if (status == 0)
240 *byte = readl(&twsi->data);
241 /* return status */
242 return status;
243}
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200244
Albert Aribaud04280c42010-08-27 18:26:05 +0200245/*
246 * Assert the STOP condition.
247 * This is also used to force the bus back in idle (SDA=SCL=1).
248 */
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200249static int twsi_stop(struct i2c_adapter *adap, int status)
Albert Aribaud04280c42010-08-27 18:26:05 +0200250{
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200251 struct mvtwsi_registers *twsi = twsi_get_base(adap);
Albert Aribaud04280c42010-08-27 18:26:05 +0200252 int control, stop_status;
253 int timeout = 1000;
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200254
Albert Aribaud04280c42010-08-27 18:26:05 +0200255 /* assert STOP */
256 control = MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_STOP;
Hans de Goede6b703e02016-01-14 14:06:25 +0100257 writel(control | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
Albert Aribaud04280c42010-08-27 18:26:05 +0200258 /* wait for IDLE; IFLG won't rise so twsi_wait() is no use. */
259 do {
260 stop_status = readl(&twsi->status);
261 if (stop_status == MVTWSI_STATUS_IDLE)
262 break;
263 udelay(10); /* one clock cycle at 100 kHz */
264 } while (timeout--);
265 control = readl(&twsi->control);
266 if (stop_status != MVTWSI_STATUS_IDLE)
267 if (status == 0)
268 status = MVTWSI_ERROR(
269 MVTWSI_ERROR_TIMEOUT,
270 control, status, MVTWSI_STATUS_IDLE);
271 return status;
272}
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200273
Stefan Roesecca56a72015-03-18 09:30:54 +0100274static unsigned int twsi_calc_freq(const int n, const int m)
275{
276#ifdef CONFIG_SUNXI
277 return CONFIG_SYS_TCLK / (10 * (m + 1) * (1 << n));
278#else
279 return CONFIG_SYS_TCLK / (10 * (m + 1) * (2 << n));
280#endif
281}
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200282
Albert Aribaud04280c42010-08-27 18:26:05 +0200283/*
Albert Aribaud04280c42010-08-27 18:26:05 +0200284 * Reset controller.
Albert Aribaud04280c42010-08-27 18:26:05 +0200285 * Controller reset also resets the baud rate and slave address, so
Hans de Goede9830f1c2014-06-13 22:55:48 +0200286 * they must be re-established afterwards.
Albert Aribaud04280c42010-08-27 18:26:05 +0200287 */
Hans de Goede9830f1c2014-06-13 22:55:48 +0200288static void twsi_reset(struct i2c_adapter *adap)
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200289{
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200290 struct mvtwsi_registers *twsi = twsi_get_base(adap);
Chris Packhamc42be012016-05-13 15:19:31 +1200291
Albert Aribaud04280c42010-08-27 18:26:05 +0200292 /* reset controller */
293 writel(0, &twsi->soft_reset);
294 /* wait 2 ms -- this is what the Marvell LSP does */
295 udelay(20000);
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200296}
297
Albert Aribaud04280c42010-08-27 18:26:05 +0200298/*
299 * I2C init called by cmd_i2c when doing 'i2c reset'.
300 * Sets baud to the highest possible value not exceeding requested one.
301 */
Hans de Goede9830f1c2014-06-13 22:55:48 +0200302static unsigned int twsi_i2c_set_bus_speed(struct i2c_adapter *adap,
303 unsigned int requested_speed)
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200304{
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200305 struct mvtwsi_registers *twsi = twsi_get_base(adap);
Hans de Goede9830f1c2014-06-13 22:55:48 +0200306 unsigned int tmp_speed, highest_speed, n, m;
307 unsigned int baud = 0x44; /* baudrate at controller reset */
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200308
Albert Aribaud04280c42010-08-27 18:26:05 +0200309 /* use actual speed to collect progressively higher values */
310 highest_speed = 0;
311 /* compute m, n setting for highest speed not above requested speed */
312 for (n = 0; n < 8; n++) {
313 for (m = 0; m < 16; m++) {
Stefan Roesecca56a72015-03-18 09:30:54 +0100314 tmp_speed = twsi_calc_freq(n, m);
Albert Aribaud04280c42010-08-27 18:26:05 +0200315 if ((tmp_speed <= requested_speed)
316 && (tmp_speed > highest_speed)) {
317 highest_speed = tmp_speed;
318 baud = (m << 3) | n;
319 }
320 }
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200321 }
Hans de Goede9830f1c2014-06-13 22:55:48 +0200322 writel(baud, &twsi->baudrate);
323 return 0;
324}
325
326static void twsi_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
327{
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200328 struct mvtwsi_registers *twsi = twsi_get_base(adap);
329
Albert Aribaud04280c42010-08-27 18:26:05 +0200330 /* reset controller */
Hans de Goede9830f1c2014-06-13 22:55:48 +0200331 twsi_reset(adap);
332 /* set speed */
333 twsi_i2c_set_bus_speed(adap, speed);
334 /* set slave address even though we don't use it */
335 writel(slaveadd, &twsi->slave_address);
336 writel(0, &twsi->xtnd_slave_addr);
337 /* assert STOP but don't care for the result */
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200338 (void) twsi_stop(adap, 0);
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200339}
340
Albert Aribaud04280c42010-08-27 18:26:05 +0200341/*
342 * Begin I2C transaction with expected start status, at given address.
343 * Common to i2c_probe, i2c_read and i2c_write.
344 * Expected address status will derive from direction bit (bit 0) in addr.
345 */
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200346static int i2c_begin(struct i2c_adapter *adap, int expected_start_status,
Chris Packhamc42be012016-05-13 15:19:31 +1200347 u8 addr, u8 *flags)
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200348{
Albert Aribaud04280c42010-08-27 18:26:05 +0200349 int status, expected_addr_status;
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200350
Albert Aribaud04280c42010-08-27 18:26:05 +0200351 /* compute expected address status from direction bit in addr */
352 if (addr & 1) /* reading */
353 expected_addr_status = MVTWSI_STATUS_ADDR_R_ACK;
354 else /* writing */
355 expected_addr_status = MVTWSI_STATUS_ADDR_W_ACK;
356 /* assert START */
Chris Packhamc42be012016-05-13 15:19:31 +1200357 status = twsi_start(adap, expected_start_status, flags);
Albert Aribaud04280c42010-08-27 18:26:05 +0200358 /* send out the address if the start went well */
359 if (status == 0)
Chris Packhamc42be012016-05-13 15:19:31 +1200360 status = twsi_send(adap, addr, expected_addr_status,
361 flags);
Albert Aribaud04280c42010-08-27 18:26:05 +0200362 /* return ok or status of first failure to caller */
363 return status;
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200364}
365
Albert Aribaud04280c42010-08-27 18:26:05 +0200366/*
367 * I2C probe called by cmd_i2c when doing 'i2c probe'.
368 * Begin read, nak data byte, end.
369 */
Hans de Goede9830f1c2014-06-13 22:55:48 +0200370static int twsi_i2c_probe(struct i2c_adapter *adap, uchar chip)
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200371{
Albert Aribaud04280c42010-08-27 18:26:05 +0200372 u8 dummy_byte;
Chris Packhamc42be012016-05-13 15:19:31 +1200373 u8 flags = 0;
Albert Aribaud04280c42010-08-27 18:26:05 +0200374 int status;
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200375
Albert Aribaud04280c42010-08-27 18:26:05 +0200376 /* begin i2c read */
Chris Packhamc42be012016-05-13 15:19:31 +1200377 status = i2c_begin(adap, MVTWSI_STATUS_START, (chip << 1) | 1, &flags);
Albert Aribaud04280c42010-08-27 18:26:05 +0200378 /* dummy read was accepted: receive byte but NAK it. */
379 if (status == 0)
Chris Packhamc42be012016-05-13 15:19:31 +1200380 status = twsi_recv(adap, &dummy_byte, &flags);
Albert Aribaud04280c42010-08-27 18:26:05 +0200381 /* Stop transaction */
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200382 twsi_stop(adap, 0);
Albert Aribaud04280c42010-08-27 18:26:05 +0200383 /* return 0 or status of first failure */
384 return status;
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200385}
386
Albert Aribaud04280c42010-08-27 18:26:05 +0200387/*
388 * I2C read called by cmd_i2c when doing 'i2c read' and by cmd_eeprom.c
389 * Begin write, send address byte(s), begin read, receive data bytes, end.
390 *
391 * NOTE: some EEPROMS want a stop right before the second start, while
392 * some will choke if it is there. Deciding which we should do is eeprom
393 * stuff, not i2c, but at the moment the APIs won't let us put it in
394 * cmd_eeprom, so we have to choose here, and for the moment that'll be
395 * a repeated start without a preceding stop.
396 */
Hans de Goede9830f1c2014-06-13 22:55:48 +0200397static int twsi_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
398 int alen, uchar *data, int length)
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200399{
Albert Aribaud04280c42010-08-27 18:26:05 +0200400 int status;
Chris Packhamc42be012016-05-13 15:19:31 +1200401 u8 flags = 0;
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200402
Albert Aribaud04280c42010-08-27 18:26:05 +0200403 /* begin i2c write to send the address bytes */
Chris Packhamc42be012016-05-13 15:19:31 +1200404 status = i2c_begin(adap, MVTWSI_STATUS_START, (chip << 1), &flags);
Albert Aribaud04280c42010-08-27 18:26:05 +0200405 /* send addr bytes */
406 while ((status == 0) && alen--)
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200407 status = twsi_send(adap, addr >> (8*alen),
Chris Packhamc42be012016-05-13 15:19:31 +1200408 MVTWSI_STATUS_DATA_W_ACK, &flags);
Albert Aribaud04280c42010-08-27 18:26:05 +0200409 /* begin i2c read to receive eeprom data bytes */
410 if (status == 0)
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200411 status = i2c_begin(adap, MVTWSI_STATUS_REPEATED_START,
Chris Packhamc42be012016-05-13 15:19:31 +1200412 (chip << 1) | 1, &flags);
Albert Aribaud04280c42010-08-27 18:26:05 +0200413 /* prepare ACK if at least one byte must be received */
414 if (length > 0)
Chris Packhamc42be012016-05-13 15:19:31 +1200415 flags |= MVTWSI_CONTROL_ACK;
Albert Aribaud04280c42010-08-27 18:26:05 +0200416 /* now receive actual bytes */
417 while ((status == 0) && length--) {
418 /* reset NAK if we if no more to read now */
419 if (length == 0)
Chris Packhamc42be012016-05-13 15:19:31 +1200420 flags &= ~MVTWSI_CONTROL_ACK;
Albert Aribaud04280c42010-08-27 18:26:05 +0200421 /* read current byte */
Chris Packhamc42be012016-05-13 15:19:31 +1200422 status = twsi_recv(adap, data++, &flags);
Albert Aribaud04280c42010-08-27 18:26:05 +0200423 }
424 /* Stop transaction */
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200425 status = twsi_stop(adap, status);
Albert Aribaud04280c42010-08-27 18:26:05 +0200426 /* return 0 or status of first failure */
427 return status;
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200428}
429
Albert Aribaud04280c42010-08-27 18:26:05 +0200430/*
431 * I2C write called by cmd_i2c when doing 'i2c write' and by cmd_eeprom.c
432 * Begin write, send address byte(s), send data bytes, end.
433 */
Hans de Goede9830f1c2014-06-13 22:55:48 +0200434static int twsi_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
435 int alen, uchar *data, int length)
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200436{
Albert Aribaud04280c42010-08-27 18:26:05 +0200437 int status;
Chris Packhamc42be012016-05-13 15:19:31 +1200438 u8 flags = 0;
Albert Aribaud04280c42010-08-27 18:26:05 +0200439
440 /* begin i2c write to send the eeprom adress bytes then data bytes */
Chris Packhamc42be012016-05-13 15:19:31 +1200441 status = i2c_begin(adap, MVTWSI_STATUS_START, (chip << 1), &flags);
Albert Aribaud04280c42010-08-27 18:26:05 +0200442 /* send addr bytes */
443 while ((status == 0) && alen--)
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200444 status = twsi_send(adap, addr >> (8*alen),
Chris Packhamc42be012016-05-13 15:19:31 +1200445 MVTWSI_STATUS_DATA_W_ACK, &flags);
Albert Aribaud04280c42010-08-27 18:26:05 +0200446 /* send data bytes */
447 while ((status == 0) && (length-- > 0))
Chris Packhamc42be012016-05-13 15:19:31 +1200448 status = twsi_send(adap, *(data++), MVTWSI_STATUS_DATA_W_ACK,
449 &flags);
Albert Aribaud04280c42010-08-27 18:26:05 +0200450 /* Stop transaction */
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200451 status = twsi_stop(adap, status);
Albert Aribaud04280c42010-08-27 18:26:05 +0200452 /* return 0 or status of first failure */
453 return status;
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200454}
455
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200456#ifdef CONFIG_I2C_MVTWSI_BASE0
Hans de Goede9830f1c2014-06-13 22:55:48 +0200457U_BOOT_I2C_ADAP_COMPLETE(twsi0, twsi_i2c_init, twsi_i2c_probe,
458 twsi_i2c_read, twsi_i2c_write,
459 twsi_i2c_set_bus_speed,
460 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0)
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200461#endif
462#ifdef CONFIG_I2C_MVTWSI_BASE1
463U_BOOT_I2C_ADAP_COMPLETE(twsi1, twsi_i2c_init, twsi_i2c_probe,
464 twsi_i2c_read, twsi_i2c_write,
465 twsi_i2c_set_bus_speed,
466 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 1)
467
468#endif
469#ifdef CONFIG_I2C_MVTWSI_BASE2
470U_BOOT_I2C_ADAP_COMPLETE(twsi2, twsi_i2c_init, twsi_i2c_probe,
471 twsi_i2c_read, twsi_i2c_write,
472 twsi_i2c_set_bus_speed,
473 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 2)
474
475#endif
476#ifdef CONFIG_I2C_MVTWSI_BASE3
477U_BOOT_I2C_ADAP_COMPLETE(twsi3, twsi_i2c_init, twsi_i2c_probe,
478 twsi_i2c_read, twsi_i2c_write,
479 twsi_i2c_set_bus_speed,
480 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 3)
481
482#endif
483#ifdef CONFIG_I2C_MVTWSI_BASE4
484U_BOOT_I2C_ADAP_COMPLETE(twsi4, twsi_i2c_init, twsi_i2c_probe,
485 twsi_i2c_read, twsi_i2c_write,
486 twsi_i2c_set_bus_speed,
487 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 4)
488
489#endif
Jelle van der Waa8d3d7c12016-01-14 14:06:26 +0100490#ifdef CONFIG_I2C_MVTWSI_BASE5
491U_BOOT_I2C_ADAP_COMPLETE(twsi5, twsi_i2c_init, twsi_i2c_probe,
492 twsi_i2c_read, twsi_i2c_write,
493 twsi_i2c_set_bus_speed,
494 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 5)
495
496#endif