blob: 74ac0a4aa78976487f8314ec7c531a7f781d9299 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Heiko Schocher4ce5a722009-07-20 09:59:37 +02002/*
Albert Aribaud306563a2010-08-27 18:26:05 +02003 * Driver for the TWSI (i2c) controller found on the Marvell
4 * orion5x and kirkwood SoC families.
Heiko Schocher4ce5a722009-07-20 09:59:37 +02005 *
Albert ARIBAUD57b4bce2011-04-22 19:41:02 +02006 * Author: Albert Aribaud <albert.u.boot@aribaud.net>
Albert Aribaud306563a2010-08-27 18:26:05 +02007 * Copyright (c) 2010 Albert Aribaud.
Heiko Schocher4ce5a722009-07-20 09:59:37 +02008 */
Albert Aribaud306563a2010-08-27 18:26:05 +02009
Heiko Schocher4ce5a722009-07-20 09:59:37 +020010#include <common.h>
11#include <i2c.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090012#include <linux/errno.h>
Heiko Schocher4ce5a722009-07-20 09:59:37 +020013#include <asm/io.h>
Baruch Siach173ec352018-06-07 12:38:10 +030014#include <linux/bitops.h>
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +020015#include <linux/compat.h>
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +020016#ifdef CONFIG_DM_I2C
17#include <dm.h>
18#endif
19
20DECLARE_GLOBAL_DATA_PTR;
Heiko Schocher4ce5a722009-07-20 09:59:37 +020021
Heiko Schocher4ce5a722009-07-20 09:59:37 +020022/*
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +020023 * Include a file that will provide CONFIG_I2C_MVTWSI_BASE*, and possibly other
24 * settings
Heiko Schocher4ce5a722009-07-20 09:59:37 +020025 */
26
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +020027#ifndef CONFIG_DM_I2C
Albert Aribaud306563a2010-08-27 18:26:05 +020028#if defined(CONFIG_ORION5X)
29#include <asm/arch/orion5x.h>
Stefan Roese81e33f42015-12-21 13:56:33 +010030#elif (defined(CONFIG_KIRKWOOD) || defined(CONFIG_ARCH_MVEBU))
Stefan Roese3dc23f72014-10-22 12:13:06 +020031#include <asm/arch/soc.h>
Jagan Tekiaec9a0f2016-10-13 14:19:35 +053032#elif defined(CONFIG_ARCH_SUNXI)
Hans de Goede66203772014-06-13 22:55:49 +020033#include <asm/arch/i2c.h>
Albert Aribaud306563a2010-08-27 18:26:05 +020034#else
35#error Driver mvtwsi not supported by SoC or board
36#endif
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +020037#endif /* CONFIG_DM_I2C */
Albert Aribaud306563a2010-08-27 18:26:05 +020038
39/*
Jernej Skrabeca8f01cc2017-04-27 00:03:36 +020040 * On SUNXI, we get CONFIG_SYS_TCLK from this include, so we want to
41 * always have it.
42 */
43#if defined(CONFIG_DM_I2C) && defined(CONFIG_ARCH_SUNXI)
44#include <asm/arch/i2c.h>
45#endif
46
47/*
Albert Aribaud306563a2010-08-27 18:26:05 +020048 * TWSI register structure
49 */
50
Jagan Tekiaec9a0f2016-10-13 14:19:35 +053051#ifdef CONFIG_ARCH_SUNXI
Hans de Goede66203772014-06-13 22:55:49 +020052
53struct mvtwsi_registers {
54 u32 slave_address;
55 u32 xtnd_slave_addr;
56 u32 data;
57 u32 control;
58 u32 status;
59 u32 baudrate;
60 u32 soft_reset;
Baruch Siach173ec352018-06-07 12:38:10 +030061 u32 debug; /* Dummy field for build compatibility with mvebu */
Hans de Goede66203772014-06-13 22:55:49 +020062};
63
64#else
65
Albert Aribaud306563a2010-08-27 18:26:05 +020066struct mvtwsi_registers {
67 u32 slave_address;
68 u32 data;
69 u32 control;
70 union {
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +020071 u32 status; /* When reading */
72 u32 baudrate; /* When writing */
Albert Aribaud306563a2010-08-27 18:26:05 +020073 };
74 u32 xtnd_slave_addr;
Baruch Siach173ec352018-06-07 12:38:10 +030075 u32 reserved0[2];
Albert Aribaud306563a2010-08-27 18:26:05 +020076 u32 soft_reset;
Baruch Siach173ec352018-06-07 12:38:10 +030077 u32 reserved1[27];
78 u32 debug;
Albert Aribaud306563a2010-08-27 18:26:05 +020079};
80
Hans de Goede66203772014-06-13 22:55:49 +020081#endif
82
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +020083#ifdef CONFIG_DM_I2C
84struct mvtwsi_i2c_dev {
85 /* TWSI Register base for the device */
86 struct mvtwsi_registers *base;
87 /* Number of the device (determined from cell-index property) */
88 int index;
89 /* The I2C slave address for the device */
90 u8 slaveadd;
91 /* The configured I2C speed in Hz */
92 uint speed;
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +020093 /* The current length of a clock period (depending on speed) */
94 uint tick;
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +020095};
96#endif /* CONFIG_DM_I2C */
97
Albert Aribaud306563a2010-08-27 18:26:05 +020098/*
mario.six@gdsys.ccdfc39582016-07-21 11:57:02 +020099 * enum mvtwsi_ctrl_register_fields - Bit masks for flags in the control
100 * register
Albert Aribaud306563a2010-08-27 18:26:05 +0200101 */
mario.six@gdsys.ccdfc39582016-07-21 11:57:02 +0200102enum mvtwsi_ctrl_register_fields {
103 /* Acknowledge bit */
104 MVTWSI_CONTROL_ACK = 0x00000004,
105 /* Interrupt flag */
106 MVTWSI_CONTROL_IFLG = 0x00000008,
107 /* Stop bit */
108 MVTWSI_CONTROL_STOP = 0x00000010,
109 /* Start bit */
110 MVTWSI_CONTROL_START = 0x00000020,
111 /* I2C enable */
112 MVTWSI_CONTROL_TWSIEN = 0x00000040,
113 /* Interrupt enable */
114 MVTWSI_CONTROL_INTEN = 0x00000080,
115};
Albert Aribaud306563a2010-08-27 18:26:05 +0200116
117/*
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200118 * On sun6i and newer, IFLG is a write-clear bit, which is cleared by writing 1;
119 * on other platforms, it is a normal r/w bit, which is cleared by writing 0.
Hans de Goede904dfbf2016-01-14 14:06:25 +0100120 */
121
122#ifdef CONFIG_SUNXI_GEN_SUN6I
123#define MVTWSI_CONTROL_CLEAR_IFLG 0x00000008
124#else
125#define MVTWSI_CONTROL_CLEAR_IFLG 0x00000000
126#endif
127
128/*
mario.six@gdsys.ccdfc39582016-07-21 11:57:02 +0200129 * enum mvstwsi_status_values - Possible values of I2C controller's status
130 * register
131 *
132 * Only those statuses expected in normal master operation on
133 * non-10-bit-address devices are specified.
134 *
135 * Every status that's unexpected during normal operation (bus errors,
136 * arbitration losses, missing ACKs...) is passed back to the caller as an error
Albert Aribaud306563a2010-08-27 18:26:05 +0200137 * code.
138 */
mario.six@gdsys.ccdfc39582016-07-21 11:57:02 +0200139enum mvstwsi_status_values {
140 /* START condition transmitted */
141 MVTWSI_STATUS_START = 0x08,
142 /* Repeated START condition transmitted */
143 MVTWSI_STATUS_REPEATED_START = 0x10,
144 /* Address + write bit transmitted, ACK received */
145 MVTWSI_STATUS_ADDR_W_ACK = 0x18,
146 /* Data transmitted, ACK received */
147 MVTWSI_STATUS_DATA_W_ACK = 0x28,
148 /* Address + read bit transmitted, ACK received */
149 MVTWSI_STATUS_ADDR_R_ACK = 0x40,
150 /* Address + read bit transmitted, ACK not received */
151 MVTWSI_STATUS_ADDR_R_NAK = 0x48,
152 /* Data received, ACK transmitted */
153 MVTWSI_STATUS_DATA_R_ACK = 0x50,
154 /* Data received, ACK not transmitted */
155 MVTWSI_STATUS_DATA_R_NAK = 0x58,
156 /* No relevant status */
157 MVTWSI_STATUS_IDLE = 0xF8,
158};
Albert Aribaud306563a2010-08-27 18:26:05 +0200159
160/*
mario.six@gdsys.cc670514f2016-07-21 11:57:04 +0200161 * enum mvstwsi_ack_flags - Determine whether a read byte should be
162 * acknowledged or not.
163 */
164enum mvtwsi_ack_flags {
165 /* Send NAK after received byte */
166 MVTWSI_READ_NAK = 0,
167 /* Send ACK after received byte */
168 MVTWSI_READ_ACK = 1,
169};
170
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200171/*
172 * calc_tick() - Calculate the duration of a clock cycle from the I2C speed
173 *
174 * @speed: The speed in Hz to calculate the clock cycle duration for.
175 * @return The duration of a clock cycle in ns.
176 */
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200177inline uint calc_tick(uint speed)
178{
179 /* One tick = the duration of a period at the specified speed in ns (we
180 * add 100 ns to be on the safe side) */
181 return (1000000000u / speed) + 100;
182}
183
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +0200184#ifndef CONFIG_DM_I2C
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200185
mario.six@gdsys.cc670514f2016-07-21 11:57:04 +0200186/*
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200187 * twsi_get_base() - Get controller register base for specified adapter
188 *
189 * @adap: Adapter to get the register base for.
190 * @return Register base for the specified adapter.
Albert Aribaud306563a2010-08-27 18:26:05 +0200191 */
Paul Kocialkowskidd822422015-04-10 23:09:51 +0200192static struct mvtwsi_registers *twsi_get_base(struct i2c_adapter *adap)
193{
194 switch (adap->hwadapnr) {
195#ifdef CONFIG_I2C_MVTWSI_BASE0
196 case 0:
mario.six@gdsys.cc9ec43b02016-07-21 11:57:01 +0200197 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE0;
Paul Kocialkowskidd822422015-04-10 23:09:51 +0200198#endif
199#ifdef CONFIG_I2C_MVTWSI_BASE1
200 case 1:
mario.six@gdsys.cc9ec43b02016-07-21 11:57:01 +0200201 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE1;
Paul Kocialkowskidd822422015-04-10 23:09:51 +0200202#endif
203#ifdef CONFIG_I2C_MVTWSI_BASE2
204 case 2:
mario.six@gdsys.cc9ec43b02016-07-21 11:57:01 +0200205 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE2;
Paul Kocialkowskidd822422015-04-10 23:09:51 +0200206#endif
207#ifdef CONFIG_I2C_MVTWSI_BASE3
208 case 3:
mario.six@gdsys.cc9ec43b02016-07-21 11:57:01 +0200209 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE3;
Paul Kocialkowskidd822422015-04-10 23:09:51 +0200210#endif
211#ifdef CONFIG_I2C_MVTWSI_BASE4
212 case 4:
mario.six@gdsys.cc9ec43b02016-07-21 11:57:01 +0200213 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE4;
Paul Kocialkowskidd822422015-04-10 23:09:51 +0200214#endif
Jelle van der Waa9d082682016-01-14 14:06:26 +0100215#ifdef CONFIG_I2C_MVTWSI_BASE5
216 case 5:
mario.six@gdsys.cc9ec43b02016-07-21 11:57:01 +0200217 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE5;
Jelle van der Waa9d082682016-01-14 14:06:26 +0100218#endif
Paul Kocialkowskidd822422015-04-10 23:09:51 +0200219 default:
220 printf("Missing mvtwsi controller %d base\n", adap->hwadapnr);
221 break;
222 }
223
224 return NULL;
225}
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +0200226#endif
Albert Aribaud306563a2010-08-27 18:26:05 +0200227
228/*
mario.six@gdsys.ccdfc39582016-07-21 11:57:02 +0200229 * enum mvtwsi_error_class - types of I2C errors
Albert Aribaud306563a2010-08-27 18:26:05 +0200230 */
mario.six@gdsys.ccdfc39582016-07-21 11:57:02 +0200231enum mvtwsi_error_class {
232 /* The controller returned a different status than expected */
233 MVTWSI_ERROR_WRONG_STATUS = 0x01,
234 /* The controller timed out */
235 MVTWSI_ERROR_TIMEOUT = 0x02,
236};
Albert Aribaud306563a2010-08-27 18:26:05 +0200237
mario.six@gdsys.ccdfc39582016-07-21 11:57:02 +0200238/*
239 * mvtwsi_error() - Build I2C return code from error information
240 *
241 * For debugging purposes, this function packs some information of an occurred
242 * error into a return code. These error codes are returned from I2C API
243 * functions (i2c_{read,write}, dm_i2c_{read,write}, etc.).
244 *
245 * @ec: The error class of the error (enum mvtwsi_error_class).
246 * @lc: The last value of the control register.
247 * @ls: The last value of the status register.
248 * @es: The expected value of the status register.
249 * @return The generated error code.
250 */
251inline uint mvtwsi_error(uint ec, uint lc, uint ls, uint es)
252{
253 return ((ec << 24) & 0xFF000000)
254 | ((lc << 16) & 0x00FF0000)
255 | ((ls << 8) & 0x0000FF00)
256 | (es & 0xFF);
257}
Albert Aribaud306563a2010-08-27 18:26:05 +0200258
259/*
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200260 * twsi_wait() - Wait for I2C bus interrupt flag and check status, or time out.
261 *
262 * @return Zero if status is as expected, or a non-zero code if either a time
263 * out occurred, or the status was not the expected one.
Albert Aribaud306563a2010-08-27 18:26:05 +0200264 */
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200265static int twsi_wait(struct mvtwsi_registers *twsi, int expected_status,
266 uint tick)
Heiko Schocher4ce5a722009-07-20 09:59:37 +0200267{
Albert Aribaud306563a2010-08-27 18:26:05 +0200268 int control, status;
269 int timeout = 1000;
270
271 do {
272 control = readl(&twsi->control);
273 if (control & MVTWSI_CONTROL_IFLG) {
274 status = readl(&twsi->status);
275 if (status == expected_status)
276 return 0;
277 else
mario.six@gdsys.ccdfc39582016-07-21 11:57:02 +0200278 return mvtwsi_error(
Albert Aribaud306563a2010-08-27 18:26:05 +0200279 MVTWSI_ERROR_WRONG_STATUS,
280 control, status, expected_status);
281 }
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200282 ndelay(tick); /* One clock cycle */
Albert Aribaud306563a2010-08-27 18:26:05 +0200283 } while (timeout--);
284 status = readl(&twsi->status);
mario.six@gdsys.ccdfc39582016-07-21 11:57:02 +0200285 return mvtwsi_error(MVTWSI_ERROR_TIMEOUT, control, status,
286 expected_status);
Heiko Schocher4ce5a722009-07-20 09:59:37 +0200287}
288
Albert Aribaud306563a2010-08-27 18:26:05 +0200289/*
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200290 * twsi_start() - Assert a START condition on the bus.
291 *
292 * This function is used in both single I2C transactions and inside
293 * back-to-back transactions (repeated starts).
294 *
295 * @twsi: The MVTWSI register structure to use.
296 * @expected_status: The I2C bus status expected to be asserted after the
297 * operation completion.
298 * @tick: The duration of a clock cycle at the current I2C speed.
299 * @return Zero if status is as expected, or a non-zero code if either a time
300 * out occurred or the status was not the expected one.
Albert Aribaud306563a2010-08-27 18:26:05 +0200301 */
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200302static int twsi_start(struct mvtwsi_registers *twsi, int expected_status,
303 uint tick)
Albert Aribaud306563a2010-08-27 18:26:05 +0200304{
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200305 /* Assert START */
mario.six@gdsys.cc670514f2016-07-21 11:57:04 +0200306 writel(MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_START |
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200307 MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
308 /* Wait for controller to process START */
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200309 return twsi_wait(twsi, expected_status, tick);
Albert Aribaud306563a2010-08-27 18:26:05 +0200310}
311
312/*
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200313 * twsi_send() - Send a byte on the I2C bus.
314 *
315 * The byte may be part of an address byte or data.
316 *
317 * @twsi: The MVTWSI register structure to use.
318 * @byte: The byte to send.
319 * @expected_status: The I2C bus status expected to be asserted after the
320 * operation completion.
321 * @tick: The duration of a clock cycle at the current I2C speed.
322 * @return Zero if status is as expected, or a non-zero code if either a time
323 * out occurred or the status was not the expected one.
Albert Aribaud306563a2010-08-27 18:26:05 +0200324 */
mario.six@gdsys.cc3c4db632016-07-21 11:57:08 +0200325static int twsi_send(struct mvtwsi_registers *twsi, u8 byte,
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200326 int expected_status, uint tick)
Albert Aribaud306563a2010-08-27 18:26:05 +0200327{
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200328 /* Write byte to data register for sending */
Albert Aribaud306563a2010-08-27 18:26:05 +0200329 writel(byte, &twsi->data);
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200330 /* Clear any pending interrupt -- that will cause sending */
mario.six@gdsys.cc670514f2016-07-21 11:57:04 +0200331 writel(MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_CLEAR_IFLG,
332 &twsi->control);
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200333 /* Wait for controller to receive byte, and check ACK */
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200334 return twsi_wait(twsi, expected_status, tick);
Albert Aribaud306563a2010-08-27 18:26:05 +0200335}
336
337/*
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200338 * twsi_recv() - Receive a byte on the I2C bus.
339 *
340 * The static variable mvtwsi_control_flags controls whether we ack or nak.
341 *
342 * @twsi: The MVTWSI register structure to use.
343 * @byte: The byte to send.
344 * @ack_flag: Flag that determines whether the received byte should
345 * be acknowledged by the controller or not (sent ACK/NAK).
346 * @tick: The duration of a clock cycle at the current I2C speed.
347 * @return Zero if status is as expected, or a non-zero code if either a time
348 * out occurred or the status was not the expected one.
Albert Aribaud306563a2010-08-27 18:26:05 +0200349 */
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200350static int twsi_recv(struct mvtwsi_registers *twsi, u8 *byte, int ack_flag,
351 uint tick)
Albert Aribaud306563a2010-08-27 18:26:05 +0200352{
mario.six@gdsys.cc670514f2016-07-21 11:57:04 +0200353 int expected_status, status, control;
Albert Aribaud306563a2010-08-27 18:26:05 +0200354
mario.six@gdsys.cc670514f2016-07-21 11:57:04 +0200355 /* Compute expected status based on passed ACK flag */
356 expected_status = ack_flag ? MVTWSI_STATUS_DATA_R_ACK :
357 MVTWSI_STATUS_DATA_R_NAK;
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200358 /* Acknowledge *previous state*, and launch receive */
mario.six@gdsys.cc670514f2016-07-21 11:57:04 +0200359 control = MVTWSI_CONTROL_TWSIEN;
360 control |= ack_flag == MVTWSI_READ_ACK ? MVTWSI_CONTROL_ACK : 0;
361 writel(control | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200362 /* Wait for controller to receive byte, and assert ACK or NAK */
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200363 status = twsi_wait(twsi, expected_status, tick);
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200364 /* If we did receive the expected byte, store it */
Albert Aribaud306563a2010-08-27 18:26:05 +0200365 if (status == 0)
366 *byte = readl(&twsi->data);
Albert Aribaud306563a2010-08-27 18:26:05 +0200367 return status;
368}
369
370/*
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200371 * twsi_stop() - Assert a STOP condition on the bus.
372 *
373 * This function is also used to force the bus back to idle state (SDA =
374 * SCL = 1).
375 *
376 * @twsi: The MVTWSI register structure to use.
377 * @tick: The duration of a clock cycle at the current I2C speed.
378 * @return Zero if the operation succeeded, or a non-zero code if a time out
379 * occurred.
Albert Aribaud306563a2010-08-27 18:26:05 +0200380 */
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200381static int twsi_stop(struct mvtwsi_registers *twsi, uint tick)
Albert Aribaud306563a2010-08-27 18:26:05 +0200382{
383 int control, stop_status;
mario.six@gdsys.cc059fce92016-07-21 11:57:05 +0200384 int status = 0;
Albert Aribaud306563a2010-08-27 18:26:05 +0200385 int timeout = 1000;
386
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200387 /* Assert STOP */
Albert Aribaud306563a2010-08-27 18:26:05 +0200388 control = MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_STOP;
Hans de Goede904dfbf2016-01-14 14:06:25 +0100389 writel(control | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200390 /* Wait for IDLE; IFLG won't rise, so we can't use twsi_wait() */
Albert Aribaud306563a2010-08-27 18:26:05 +0200391 do {
392 stop_status = readl(&twsi->status);
393 if (stop_status == MVTWSI_STATUS_IDLE)
394 break;
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200395 ndelay(tick); /* One clock cycle */
Albert Aribaud306563a2010-08-27 18:26:05 +0200396 } while (timeout--);
397 control = readl(&twsi->control);
398 if (stop_status != MVTWSI_STATUS_IDLE)
mario.six@gdsys.cc059fce92016-07-21 11:57:05 +0200399 status = mvtwsi_error(MVTWSI_ERROR_TIMEOUT,
400 control, status, MVTWSI_STATUS_IDLE);
Albert Aribaud306563a2010-08-27 18:26:05 +0200401 return status;
402}
403
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200404/*
405 * twsi_calc_freq() - Compute I2C frequency depending on m and n parameters.
406 *
407 * @n: Parameter 'n' for the frequency calculation algorithm.
408 * @m: Parameter 'm' for the frequency calculation algorithm.
409 * @return The I2C frequency corresponding to the passed m and n parameters.
410 */
mario.six@gdsys.cce0758282016-07-21 11:57:06 +0200411static uint twsi_calc_freq(const int n, const int m)
Stefan Roesef582a152015-03-18 09:30:54 +0100412{
Jagan Tekiaec9a0f2016-10-13 14:19:35 +0530413#ifdef CONFIG_ARCH_SUNXI
Stefan Roesef582a152015-03-18 09:30:54 +0100414 return CONFIG_SYS_TCLK / (10 * (m + 1) * (1 << n));
415#else
416 return CONFIG_SYS_TCLK / (10 * (m + 1) * (2 << n));
417#endif
418}
Albert Aribaud306563a2010-08-27 18:26:05 +0200419
420/*
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200421 * twsi_reset() - Reset the I2C controller.
422 *
423 * Resetting the controller also resets the baud rate and slave address, hence
424 * they must be re-established after the reset.
425 *
426 * @twsi: The MVTWSI register structure to use.
Albert Aribaud306563a2010-08-27 18:26:05 +0200427 */
mario.six@gdsys.cc3c4db632016-07-21 11:57:08 +0200428static void twsi_reset(struct mvtwsi_registers *twsi)
Albert Aribaud306563a2010-08-27 18:26:05 +0200429{
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200430 /* Reset controller */
Albert Aribaud306563a2010-08-27 18:26:05 +0200431 writel(0, &twsi->soft_reset);
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200432 /* Wait 2 ms -- this is what the Marvell LSP does */
Albert Aribaud306563a2010-08-27 18:26:05 +0200433 udelay(20000);
Albert Aribaud306563a2010-08-27 18:26:05 +0200434}
435
436/*
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200437 * __twsi_i2c_set_bus_speed() - Set the speed of the I2C controller.
438 *
439 * This function sets baud rate to the highest possible value that does not
440 * exceed the requested rate.
441 *
442 * @twsi: The MVTWSI register structure to use.
443 * @requested_speed: The desired frequency the controller should run at
444 * in Hz.
445 * @return The actual frequency the controller was configured to.
Albert Aribaud306563a2010-08-27 18:26:05 +0200446 */
mario.six@gdsys.cc3c4db632016-07-21 11:57:08 +0200447static uint __twsi_i2c_set_bus_speed(struct mvtwsi_registers *twsi,
mario.six@gdsys.cc61bc02b2016-07-21 11:57:07 +0200448 uint requested_speed)
Albert Aribaud306563a2010-08-27 18:26:05 +0200449{
mario.six@gdsys.cce0758282016-07-21 11:57:06 +0200450 uint tmp_speed, highest_speed, n, m;
451 uint baud = 0x44; /* Baud rate after controller reset */
Albert Aribaud306563a2010-08-27 18:26:05 +0200452
Albert Aribaud306563a2010-08-27 18:26:05 +0200453 highest_speed = 0;
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200454 /* Successively try m, n combinations, and use the combination
455 * resulting in the largest speed that's not above the requested
456 * speed */
Heiko Schocher4ce5a722009-07-20 09:59:37 +0200457 for (n = 0; n < 8; n++) {
458 for (m = 0; m < 16; m++) {
Stefan Roesef582a152015-03-18 09:30:54 +0100459 tmp_speed = twsi_calc_freq(n, m);
mario.six@gdsys.cc9ec43b02016-07-21 11:57:01 +0200460 if ((tmp_speed <= requested_speed) &&
461 (tmp_speed > highest_speed)) {
Albert Aribaud306563a2010-08-27 18:26:05 +0200462 highest_speed = tmp_speed;
463 baud = (m << 3) | n;
Heiko Schocher4ce5a722009-07-20 09:59:37 +0200464 }
465 }
466 }
Hans de Goede0db2bbd2014-06-13 22:55:48 +0200467 writel(baud, &twsi->baudrate);
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200468
469 /* Wait for controller for one tick */
470#ifdef CONFIG_DM_I2C
471 ndelay(calc_tick(highest_speed));
472#else
473 ndelay(10000);
474#endif
475 return highest_speed;
Hans de Goede0db2bbd2014-06-13 22:55:48 +0200476}
477
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200478/*
479 * __twsi_i2c_init() - Initialize the I2C controller.
480 *
481 * @twsi: The MVTWSI register structure to use.
482 * @speed: The initial frequency the controller should run at
483 * in Hz.
484 * @slaveadd: The I2C address to be set for the I2C master.
485 * @actual_speed: A output parameter that receives the actual frequency
486 * in Hz the controller was set to by the function.
487 * @return Zero if the operation succeeded, or a non-zero code if a time out
488 * occurred.
489 */
mario.six@gdsys.cc3c4db632016-07-21 11:57:08 +0200490static void __twsi_i2c_init(struct mvtwsi_registers *twsi, int speed,
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200491 int slaveadd, uint *actual_speed)
Hans de Goede0db2bbd2014-06-13 22:55:48 +0200492{
Stefan Mavrodiev004b4cd2018-02-13 09:27:40 +0200493 uint tmp_speed;
494
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200495 /* Reset controller */
mario.six@gdsys.cc3c4db632016-07-21 11:57:08 +0200496 twsi_reset(twsi);
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200497 /* Set speed */
Stefan Mavrodiev004b4cd2018-02-13 09:27:40 +0200498 tmp_speed = __twsi_i2c_set_bus_speed(twsi, speed);
Heinrich Schuchardt8bcf12c2018-01-31 00:57:17 +0100499 if (actual_speed)
Stefan Mavrodiev004b4cd2018-02-13 09:27:40 +0200500 *actual_speed = tmp_speed;
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200501 /* Set slave address; even though we don't use it */
Hans de Goede0db2bbd2014-06-13 22:55:48 +0200502 writel(slaveadd, &twsi->slave_address);
503 writel(0, &twsi->xtnd_slave_addr);
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200504 /* Assert STOP, but don't care for the result */
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200505#ifdef CONFIG_DM_I2C
506 (void) twsi_stop(twsi, calc_tick(*actual_speed));
507#else
508 (void) twsi_stop(twsi, 10000);
509#endif
Heiko Schocher4ce5a722009-07-20 09:59:37 +0200510}
511
Albert Aribaud306563a2010-08-27 18:26:05 +0200512/*
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200513 * i2c_begin() - Start a I2C transaction.
514 *
515 * Begin a I2C transaction with a given expected start status and chip address.
516 * A START is asserted, and the address byte is sent to the I2C controller. The
517 * expected address status will be derived from the direction bit (bit 0) of
518 * the address byte.
519 *
520 * @twsi: The MVTWSI register structure to use.
521 * @expected_start_status: The I2C status the controller is expected to
522 * assert after the address byte was sent.
523 * @addr: The address byte to be sent.
524 * @tick: The duration of a clock cycle at the current
525 * I2C speed.
526 * @return Zero if the operation succeeded, or a non-zero code if a time out or
527 * unexpected I2C status occurred.
Albert Aribaud306563a2010-08-27 18:26:05 +0200528 */
mario.six@gdsys.cc3c4db632016-07-21 11:57:08 +0200529static int i2c_begin(struct mvtwsi_registers *twsi, int expected_start_status,
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200530 u8 addr, uint tick)
Heiko Schocher4ce5a722009-07-20 09:59:37 +0200531{
Albert Aribaud306563a2010-08-27 18:26:05 +0200532 int status, expected_addr_status;
Heiko Schocher4ce5a722009-07-20 09:59:37 +0200533
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200534 /* Compute the expected address status from the direction bit in
535 * the address byte */
536 if (addr & 1) /* Reading */
Albert Aribaud306563a2010-08-27 18:26:05 +0200537 expected_addr_status = MVTWSI_STATUS_ADDR_R_ACK;
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200538 else /* Writing */
Albert Aribaud306563a2010-08-27 18:26:05 +0200539 expected_addr_status = MVTWSI_STATUS_ADDR_W_ACK;
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200540 /* Assert START */
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200541 status = twsi_start(twsi, expected_start_status, tick);
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200542 /* Send out the address if the start went well */
Albert Aribaud306563a2010-08-27 18:26:05 +0200543 if (status == 0)
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200544 status = twsi_send(twsi, addr, expected_addr_status, tick);
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200545 /* Return 0, or the status of the first failure */
Albert Aribaud306563a2010-08-27 18:26:05 +0200546 return status;
Heiko Schocher4ce5a722009-07-20 09:59:37 +0200547}
548
Albert Aribaud306563a2010-08-27 18:26:05 +0200549/*
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200550 * __twsi_i2c_probe_chip() - Probe the given I2C chip address.
551 *
552 * This function begins a I2C read transaction, does a dummy read and NAKs; if
553 * the procedure succeeds, the chip is considered to be present.
554 *
555 * @twsi: The MVTWSI register structure to use.
556 * @chip: The chip address to probe.
557 * @tick: The duration of a clock cycle at the current I2C speed.
558 * @return Zero if the operation succeeded, or a non-zero code if a time out or
559 * unexpected I2C status occurred.
Albert Aribaud306563a2010-08-27 18:26:05 +0200560 */
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200561static int __twsi_i2c_probe_chip(struct mvtwsi_registers *twsi, uchar chip,
562 uint tick)
Heiko Schocher4ce5a722009-07-20 09:59:37 +0200563{
Albert Aribaud306563a2010-08-27 18:26:05 +0200564 u8 dummy_byte;
565 int status;
566
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200567 /* Begin i2c read */
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200568 status = i2c_begin(twsi, MVTWSI_STATUS_START, (chip << 1) | 1, tick);
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200569 /* Dummy read was accepted: receive byte, but NAK it. */
Albert Aribaud306563a2010-08-27 18:26:05 +0200570 if (status == 0)
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200571 status = twsi_recv(twsi, &dummy_byte, MVTWSI_READ_NAK, tick);
Albert Aribaud306563a2010-08-27 18:26:05 +0200572 /* Stop transaction */
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200573 twsi_stop(twsi, tick);
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200574 /* Return 0, or the status of the first failure */
Albert Aribaud306563a2010-08-27 18:26:05 +0200575 return status;
576}
577
578/*
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200579 * __twsi_i2c_read() - Read data from a I2C chip.
580 *
581 * This function begins a I2C write transaction, and transmits the address
582 * bytes; then begins a I2C read transaction, and receives the data bytes.
Albert Aribaud306563a2010-08-27 18:26:05 +0200583 *
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200584 * NOTE: Some devices want a stop right before the second start, while some
585 * will choke if it is there. Since deciding this is not yet supported in
586 * higher level APIs, we need to make a decision here, and for the moment that
587 * will be a repeated start without a preceding stop.
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200588 *
589 * @twsi: The MVTWSI register structure to use.
590 * @chip: The chip address to read from.
591 * @addr: The address bytes to send.
592 * @alen: The length of the address bytes in bytes.
593 * @data: The buffer to receive the data read from the chip (has to have
594 * a size of at least 'length' bytes).
595 * @length: The amount of data to be read from the chip in bytes.
596 * @tick: The duration of a clock cycle at the current I2C speed.
597 * @return Zero if the operation succeeded, or a non-zero code if a time out or
598 * unexpected I2C status occurred.
Albert Aribaud306563a2010-08-27 18:26:05 +0200599 */
mario.six@gdsys.cc3c4db632016-07-21 11:57:08 +0200600static int __twsi_i2c_read(struct mvtwsi_registers *twsi, uchar chip,
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200601 u8 *addr, int alen, uchar *data, int length,
602 uint tick)
Albert Aribaud306563a2010-08-27 18:26:05 +0200603{
mario.six@gdsys.cc059fce92016-07-21 11:57:05 +0200604 int status = 0;
605 int stop_status;
mario.six@gdsys.cc24f9c6b2016-07-21 11:57:11 +0200606 int expected_start = MVTWSI_STATUS_START;
Albert Aribaud306563a2010-08-27 18:26:05 +0200607
mario.six@gdsys.cc24f9c6b2016-07-21 11:57:11 +0200608 if (alen > 0) {
609 /* Begin i2c write to send the address bytes */
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200610 status = i2c_begin(twsi, expected_start, (chip << 1), tick);
mario.six@gdsys.cc24f9c6b2016-07-21 11:57:11 +0200611 /* Send address bytes */
612 while ((status == 0) && alen--)
Stefan Roese03d6cd92016-08-25 15:20:01 +0200613 status = twsi_send(twsi, addr[alen],
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200614 MVTWSI_STATUS_DATA_W_ACK, tick);
mario.six@gdsys.cc24f9c6b2016-07-21 11:57:11 +0200615 /* Send repeated STARTs after the initial START */
616 expected_start = MVTWSI_STATUS_REPEATED_START;
617 }
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200618 /* Begin i2c read to receive data bytes */
Albert Aribaud306563a2010-08-27 18:26:05 +0200619 if (status == 0)
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200620 status = i2c_begin(twsi, expected_start, (chip << 1) | 1, tick);
mario.six@gdsys.cc670514f2016-07-21 11:57:04 +0200621 /* Receive actual data bytes; set NAK if we if we have nothing more to
622 * read */
623 while ((status == 0) && length--)
mario.six@gdsys.cc3c4db632016-07-21 11:57:08 +0200624 status = twsi_recv(twsi, data++,
mario.six@gdsys.cc670514f2016-07-21 11:57:04 +0200625 length > 0 ?
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200626 MVTWSI_READ_ACK : MVTWSI_READ_NAK, tick);
Albert Aribaud306563a2010-08-27 18:26:05 +0200627 /* Stop transaction */
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200628 stop_status = twsi_stop(twsi, tick);
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200629 /* Return 0, or the status of the first failure */
mario.six@gdsys.cc059fce92016-07-21 11:57:05 +0200630 return status != 0 ? status : stop_status;
Heiko Schocher4ce5a722009-07-20 09:59:37 +0200631}
632
Albert Aribaud306563a2010-08-27 18:26:05 +0200633/*
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200634 * __twsi_i2c_write() - Send data to a I2C chip.
635 *
636 * This function begins a I2C write transaction, and transmits the address
637 * bytes; then begins a new I2C write transaction, and sends the data bytes.
638 *
639 * @twsi: The MVTWSI register structure to use.
640 * @chip: The chip address to read from.
641 * @addr: The address bytes to send.
642 * @alen: The length of the address bytes in bytes.
643 * @data: The buffer containing the data to be sent to the chip.
644 * @length: The length of data to be sent to the chip in bytes.
645 * @tick: The duration of a clock cycle at the current I2C speed.
646 * @return Zero if the operation succeeded, or a non-zero code if a time out or
647 * unexpected I2C status occurred.
Albert Aribaud306563a2010-08-27 18:26:05 +0200648 */
mario.six@gdsys.cc3c4db632016-07-21 11:57:08 +0200649static int __twsi_i2c_write(struct mvtwsi_registers *twsi, uchar chip,
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200650 u8 *addr, int alen, uchar *data, int length,
651 uint tick)
Heiko Schocher4ce5a722009-07-20 09:59:37 +0200652{
mario.six@gdsys.cc059fce92016-07-21 11:57:05 +0200653 int status, stop_status;
Heiko Schocher4ce5a722009-07-20 09:59:37 +0200654
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200655 /* Begin i2c write to send first the address bytes, then the
656 * data bytes */
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200657 status = i2c_begin(twsi, MVTWSI_STATUS_START, (chip << 1), tick);
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200658 /* Send address bytes */
mario.six@gdsys.ccf8a10ed2016-07-21 11:57:09 +0200659 while ((status == 0) && (alen-- > 0))
Stefan Roese03d6cd92016-08-25 15:20:01 +0200660 status = twsi_send(twsi, addr[alen], MVTWSI_STATUS_DATA_W_ACK,
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200661 tick);
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200662 /* Send data bytes */
Albert Aribaud306563a2010-08-27 18:26:05 +0200663 while ((status == 0) && (length-- > 0))
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200664 status = twsi_send(twsi, *(data++), MVTWSI_STATUS_DATA_W_ACK,
665 tick);
Albert Aribaud306563a2010-08-27 18:26:05 +0200666 /* Stop transaction */
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200667 stop_status = twsi_stop(twsi, tick);
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200668 /* Return 0, or the status of the first failure */
mario.six@gdsys.cc059fce92016-07-21 11:57:05 +0200669 return status != 0 ? status : stop_status;
Heiko Schocher4ce5a722009-07-20 09:59:37 +0200670}
671
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +0200672#ifndef CONFIG_DM_I2C
mario.six@gdsys.cc61bc02b2016-07-21 11:57:07 +0200673static void twsi_i2c_init(struct i2c_adapter *adap, int speed,
674 int slaveadd)
675{
mario.six@gdsys.cc3c4db632016-07-21 11:57:08 +0200676 struct mvtwsi_registers *twsi = twsi_get_base(adap);
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200677 __twsi_i2c_init(twsi, speed, slaveadd, NULL);
mario.six@gdsys.cc61bc02b2016-07-21 11:57:07 +0200678}
679
680static uint twsi_i2c_set_bus_speed(struct i2c_adapter *adap,
681 uint requested_speed)
682{
mario.six@gdsys.cc3c4db632016-07-21 11:57:08 +0200683 struct mvtwsi_registers *twsi = twsi_get_base(adap);
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200684 __twsi_i2c_set_bus_speed(twsi, requested_speed);
685 return 0;
mario.six@gdsys.cc61bc02b2016-07-21 11:57:07 +0200686}
687
688static int twsi_i2c_probe(struct i2c_adapter *adap, uchar chip)
689{
mario.six@gdsys.cc3c4db632016-07-21 11:57:08 +0200690 struct mvtwsi_registers *twsi = twsi_get_base(adap);
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200691 return __twsi_i2c_probe_chip(twsi, chip, 10000);
mario.six@gdsys.cc61bc02b2016-07-21 11:57:07 +0200692}
693
694static int twsi_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
695 int alen, uchar *data, int length)
696{
mario.six@gdsys.cc3c4db632016-07-21 11:57:08 +0200697 struct mvtwsi_registers *twsi = twsi_get_base(adap);
mario.six@gdsys.ccf8a10ed2016-07-21 11:57:09 +0200698 u8 addr_bytes[4];
699
700 addr_bytes[0] = (addr >> 0) & 0xFF;
701 addr_bytes[1] = (addr >> 8) & 0xFF;
702 addr_bytes[2] = (addr >> 16) & 0xFF;
703 addr_bytes[3] = (addr >> 24) & 0xFF;
704
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200705 return __twsi_i2c_read(twsi, chip, addr_bytes, alen, data, length,
706 10000);
mario.six@gdsys.cc61bc02b2016-07-21 11:57:07 +0200707}
708
709static int twsi_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
710 int alen, uchar *data, int length)
711{
mario.six@gdsys.cc3c4db632016-07-21 11:57:08 +0200712 struct mvtwsi_registers *twsi = twsi_get_base(adap);
mario.six@gdsys.ccf8a10ed2016-07-21 11:57:09 +0200713 u8 addr_bytes[4];
714
715 addr_bytes[0] = (addr >> 0) & 0xFF;
716 addr_bytes[1] = (addr >> 8) & 0xFF;
717 addr_bytes[2] = (addr >> 16) & 0xFF;
718 addr_bytes[3] = (addr >> 24) & 0xFF;
719
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200720 return __twsi_i2c_write(twsi, chip, addr_bytes, alen, data, length,
721 10000);
mario.six@gdsys.cc61bc02b2016-07-21 11:57:07 +0200722}
723
Paul Kocialkowskidd822422015-04-10 23:09:51 +0200724#ifdef CONFIG_I2C_MVTWSI_BASE0
Hans de Goede0db2bbd2014-06-13 22:55:48 +0200725U_BOOT_I2C_ADAP_COMPLETE(twsi0, twsi_i2c_init, twsi_i2c_probe,
726 twsi_i2c_read, twsi_i2c_write,
727 twsi_i2c_set_bus_speed,
728 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0)
Paul Kocialkowskidd822422015-04-10 23:09:51 +0200729#endif
730#ifdef CONFIG_I2C_MVTWSI_BASE1
731U_BOOT_I2C_ADAP_COMPLETE(twsi1, twsi_i2c_init, twsi_i2c_probe,
732 twsi_i2c_read, twsi_i2c_write,
733 twsi_i2c_set_bus_speed,
734 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 1)
735
736#endif
737#ifdef CONFIG_I2C_MVTWSI_BASE2
738U_BOOT_I2C_ADAP_COMPLETE(twsi2, 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, 2)
742
743#endif
744#ifdef CONFIG_I2C_MVTWSI_BASE3
745U_BOOT_I2C_ADAP_COMPLETE(twsi3, twsi_i2c_init, twsi_i2c_probe,
746 twsi_i2c_read, twsi_i2c_write,
747 twsi_i2c_set_bus_speed,
748 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 3)
749
750#endif
751#ifdef CONFIG_I2C_MVTWSI_BASE4
752U_BOOT_I2C_ADAP_COMPLETE(twsi4, twsi_i2c_init, twsi_i2c_probe,
753 twsi_i2c_read, twsi_i2c_write,
754 twsi_i2c_set_bus_speed,
755 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 4)
756
757#endif
Jelle van der Waa9d082682016-01-14 14:06:26 +0100758#ifdef CONFIG_I2C_MVTWSI_BASE5
759U_BOOT_I2C_ADAP_COMPLETE(twsi5, twsi_i2c_init, twsi_i2c_probe,
760 twsi_i2c_read, twsi_i2c_write,
761 twsi_i2c_set_bus_speed,
762 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 5)
763
764#endif
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +0200765#else /* CONFIG_DM_I2C */
766
767static int mvtwsi_i2c_probe_chip(struct udevice *bus, u32 chip_addr,
768 u32 chip_flags)
769{
770 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200771 return __twsi_i2c_probe_chip(dev->base, chip_addr, dev->tick);
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +0200772}
773
774static int mvtwsi_i2c_set_bus_speed(struct udevice *bus, uint speed)
775{
776 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200777
778 dev->speed = __twsi_i2c_set_bus_speed(dev->base, speed);
779 dev->tick = calc_tick(dev->speed);
780
781 return 0;
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +0200782}
783
784static int mvtwsi_i2c_ofdata_to_platdata(struct udevice *bus)
785{
786 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
787
Simon Glassa821c4a2017-05-17 17:18:05 -0600788 dev->base = devfdt_get_addr_ptr(bus);
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +0200789
790 if (!dev->base)
791 return -ENOMEM;
792
Simon Glasse160f7d2017-01-17 16:52:55 -0700793 dev->index = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus),
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +0200794 "cell-index", -1);
Simon Glasse160f7d2017-01-17 16:52:55 -0700795 dev->slaveadd = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus),
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +0200796 "u-boot,i2c-slave-addr", 0x0);
Simon Glasse160f7d2017-01-17 16:52:55 -0700797 dev->speed = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus),
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +0200798 "clock-frequency", 100000);
799 return 0;
800}
801
Baruch Siach173ec352018-06-07 12:38:10 +0300802static void twsi_disable_i2c_slave(struct mvtwsi_registers *twsi)
803{
804 clrbits_le32(&twsi->debug, BIT(18));
805}
806
807static int mvtwsi_i2c_bind(struct udevice *bus)
808{
809 struct mvtwsi_registers *twsi = devfdt_get_addr_ptr(bus);
810
811 /* Disable the hidden slave in i2c0 of these platforms */
812 if ((IS_ENABLED(CONFIG_ARMADA_38X) || IS_ENABLED(CONFIG_KIRKWOOD))
813 && bus->req_seq == 0)
814 twsi_disable_i2c_slave(twsi);
815
816 return 0;
817}
818
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +0200819static int mvtwsi_i2c_probe(struct udevice *bus)
820{
821 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200822 uint actual_speed;
823
824 __twsi_i2c_init(dev->base, dev->speed, dev->slaveadd, &actual_speed);
825 dev->speed = actual_speed;
826 dev->tick = calc_tick(dev->speed);
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +0200827 return 0;
828}
829
830static int mvtwsi_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
831{
832 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
833 struct i2c_msg *dmsg, *omsg, dummy;
834
835 memset(&dummy, 0, sizeof(struct i2c_msg));
836
837 /* We expect either two messages (one with an offset and one with the
838 * actual data) or one message (just data or offset/data combined) */
839 if (nmsgs > 2 || nmsgs == 0) {
840 debug("%s: Only one or two messages are supported.", __func__);
841 return -1;
842 }
843
844 omsg = nmsgs == 1 ? &dummy : msg;
845 dmsg = nmsgs == 1 ? msg : msg + 1;
846
847 if (dmsg->flags & I2C_M_RD)
848 return __twsi_i2c_read(dev->base, dmsg->addr, omsg->buf,
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200849 omsg->len, dmsg->buf, dmsg->len,
850 dev->tick);
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +0200851 else
852 return __twsi_i2c_write(dev->base, dmsg->addr, omsg->buf,
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200853 omsg->len, dmsg->buf, dmsg->len,
854 dev->tick);
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +0200855}
856
857static const struct dm_i2c_ops mvtwsi_i2c_ops = {
858 .xfer = mvtwsi_i2c_xfer,
859 .probe_chip = mvtwsi_i2c_probe_chip,
860 .set_bus_speed = mvtwsi_i2c_set_bus_speed,
861};
862
863static const struct udevice_id mvtwsi_i2c_ids[] = {
864 { .compatible = "marvell,mv64xxx-i2c", },
Stefan Roese87de0eb2016-09-16 15:07:55 +0200865 { .compatible = "marvell,mv78230-i2c", },
Jernej Skrabeca8f01cc2017-04-27 00:03:36 +0200866 { .compatible = "allwinner,sun6i-a31-i2c", },
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +0200867 { /* sentinel */ }
868};
869
870U_BOOT_DRIVER(i2c_mvtwsi) = {
871 .name = "i2c_mvtwsi",
872 .id = UCLASS_I2C,
873 .of_match = mvtwsi_i2c_ids,
Baruch Siach173ec352018-06-07 12:38:10 +0300874 .bind = mvtwsi_i2c_bind,
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +0200875 .probe = mvtwsi_i2c_probe,
876 .ofdata_to_platdata = mvtwsi_i2c_ofdata_to_platdata,
877 .priv_auto_alloc_size = sizeof(struct mvtwsi_i2c_dev),
878 .ops = &mvtwsi_i2c_ops,
879};
880#endif /* CONFIG_DM_I2C */