blob: a4d59b67a2271bb2e06a8074f20803e4af0b3ec7 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Heiko Schocher5cd2a242009-07-20 09:59:37 +02002/*
Albert Aribaud04280c42010-08-27 18:26:05 +02003 * Driver for the TWSI (i2c) controller found on the Marvell
4 * orion5x and kirkwood SoC families.
Heiko Schocher5cd2a242009-07-20 09:59:37 +02005 *
Albert ARIBAUD340983d2011-04-22 19:41:02 +02006 * Author: Albert Aribaud <albert.u.boot@aribaud.net>
Albert Aribaud04280c42010-08-27 18:26:05 +02007 * Copyright (c) 2010 Albert Aribaud.
Heiko Schocher5cd2a242009-07-20 09:59:37 +02008 */
Albert Aribaud04280c42010-08-27 18:26:05 +02009
Heiko Schocher5cd2a242009-07-20 09:59:37 +020010#include <common.h>
11#include <i2c.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glassdbd79542020-05-10 11:40:11 -060013#include <linux/delay.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090014#include <linux/errno.h>
Heiko Schocher5cd2a242009-07-20 09:59:37 +020015#include <asm/io.h>
Baruch Siach91006c72018-06-07 12:38:10 +030016#include <linux/bitops.h>
mario.six@gdsys.cce841b582016-07-21 11:57:12 +020017#include <linux/compat.h>
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +020018#ifdef CONFIG_DM_I2C
19#include <dm.h>
20#endif
21
22DECLARE_GLOBAL_DATA_PTR;
Heiko Schocher5cd2a242009-07-20 09:59:37 +020023
Albert Aribaud04280c42010-08-27 18:26:05 +020024/*
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +020025 * Include a file that will provide CONFIG_I2C_MVTWSI_BASE*, and possibly other
26 * settings
Albert Aribaud04280c42010-08-27 18:26:05 +020027 */
Heiko Schocher5cd2a242009-07-20 09:59:37 +020028
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +020029#ifndef CONFIG_DM_I2C
Trevor Woernerf9953752020-05-06 08:02:38 -040030#if defined(CONFIG_ARCH_ORION5X)
Albert Aribaud04280c42010-08-27 18:26:05 +020031#include <asm/arch/orion5x.h>
Trevor Woernerbb7ab072020-05-06 08:02:40 -040032#elif (defined(CONFIG_ARCH_KIRKWOOD) || defined(CONFIG_ARCH_MVEBU))
Stefan Roesec2437842014-10-22 12:13:06 +020033#include <asm/arch/soc.h>
Jagan Teki68078f72016-10-13 14:19:35 +053034#elif defined(CONFIG_ARCH_SUNXI)
Hans de Goede3352b222014-06-13 22:55:49 +020035#include <asm/arch/i2c.h>
Albert Aribaud04280c42010-08-27 18:26:05 +020036#else
37#error Driver mvtwsi not supported by SoC or board
Heiko Schocher5cd2a242009-07-20 09:59:37 +020038#endif
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +020039#endif /* CONFIG_DM_I2C */
Heiko Schocher5cd2a242009-07-20 09:59:37 +020040
Albert Aribaud04280c42010-08-27 18:26:05 +020041/*
Jernej Skrabec9220d502017-04-27 00:03:36 +020042 * 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
49/*
Albert Aribaud04280c42010-08-27 18:26:05 +020050 * TWSI register structure
51 */
Heiko Schocher5cd2a242009-07-20 09:59:37 +020052
Jagan Teki68078f72016-10-13 14:19:35 +053053#ifdef CONFIG_ARCH_SUNXI
Hans de Goede3352b222014-06-13 22:55:49 +020054
Albert Aribaud04280c42010-08-27 18:26:05 +020055struct mvtwsi_registers {
56 u32 slave_address;
Hans de Goede3352b222014-06-13 22:55:49 +020057 u32 xtnd_slave_addr;
Albert Aribaud04280c42010-08-27 18:26:05 +020058 u32 data;
59 u32 control;
Hans de Goede3352b222014-06-13 22:55:49 +020060 u32 status;
61 u32 baudrate;
62 u32 soft_reset;
Baruch Siach91006c72018-06-07 12:38:10 +030063 u32 debug; /* Dummy field for build compatibility with mvebu */
Hans de Goede3352b222014-06-13 22:55:49 +020064};
65
66#else
67
68struct mvtwsi_registers {
69 u32 slave_address;
70 u32 data;
71 u32 control;
Albert Aribaud04280c42010-08-27 18:26:05 +020072 union {
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +020073 u32 status; /* When reading */
74 u32 baudrate; /* When writing */
Albert Aribaud04280c42010-08-27 18:26:05 +020075 };
76 u32 xtnd_slave_addr;
Baruch Siach91006c72018-06-07 12:38:10 +030077 u32 reserved0[2];
Albert Aribaud04280c42010-08-27 18:26:05 +020078 u32 soft_reset;
Baruch Siach91006c72018-06-07 12:38:10 +030079 u32 reserved1[27];
80 u32 debug;
Heiko Schocher5cd2a242009-07-20 09:59:37 +020081};
82
Hans de Goede3352b222014-06-13 22:55:49 +020083#endif
84
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +020085#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;
mario.six@gdsys.cce841b582016-07-21 11:57:12 +020095 /* The current length of a clock period (depending on speed) */
96 uint tick;
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +020097};
98#endif /* CONFIG_DM_I2C */
99
Albert Aribaud04280c42010-08-27 18:26:05 +0200100/*
mario.six@gdsys.ccf43d3e92016-07-21 11:57:02 +0200101 * enum mvtwsi_ctrl_register_fields - Bit masks for flags in the control
102 * register
Albert Aribaud04280c42010-08-27 18:26:05 +0200103 */
mario.six@gdsys.ccf43d3e92016-07-21 11:57:02 +0200104enum 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};
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200118
Albert Aribaud04280c42010-08-27 18:26:05 +0200119/*
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200120 * 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.
Hans de Goede6b703e02016-01-14 14:06:25 +0100122 */
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
130/*
mario.six@gdsys.ccf43d3e92016-07-21 11:57:02 +0200131 * 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
Albert Aribaud04280c42010-08-27 18:26:05 +0200139 * code.
140 */
mario.six@gdsys.ccf43d3e92016-07-21 11:57:02 +0200141enum 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};
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200161
Albert Aribaud04280c42010-08-27 18:26:05 +0200162/*
mario.six@gdsys.cc1cc2c282016-07-21 11:57:04 +0200163 * 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
mario.six@gdsys.ccefd1fb82016-07-21 11:57:13 +0200173/*
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 */
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200179inline 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
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +0200186#ifndef CONFIG_DM_I2C
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200187
mario.six@gdsys.cc1cc2c282016-07-21 11:57:04 +0200188/*
mario.six@gdsys.ccefd1fb82016-07-21 11:57:13 +0200189 * 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.
Albert Aribaud04280c42010-08-27 18:26:05 +0200193 */
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200194static struct mvtwsi_registers *twsi_get_base(struct i2c_adapter *adap)
195{
196 switch (adap->hwadapnr) {
197#ifdef CONFIG_I2C_MVTWSI_BASE0
198 case 0:
mario.six@gdsys.cc2b656eb2016-07-21 11:57:01 +0200199 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE0;
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200200#endif
201#ifdef CONFIG_I2C_MVTWSI_BASE1
202 case 1:
mario.six@gdsys.cc2b656eb2016-07-21 11:57:01 +0200203 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE1;
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200204#endif
205#ifdef CONFIG_I2C_MVTWSI_BASE2
206 case 2:
mario.six@gdsys.cc2b656eb2016-07-21 11:57:01 +0200207 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE2;
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200208#endif
209#ifdef CONFIG_I2C_MVTWSI_BASE3
210 case 3:
mario.six@gdsys.cc2b656eb2016-07-21 11:57:01 +0200211 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE3;
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200212#endif
213#ifdef CONFIG_I2C_MVTWSI_BASE4
214 case 4:
mario.six@gdsys.cc2b656eb2016-07-21 11:57:01 +0200215 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE4;
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200216#endif
Jelle van der Waa8d3d7c12016-01-14 14:06:26 +0100217#ifdef CONFIG_I2C_MVTWSI_BASE5
218 case 5:
mario.six@gdsys.cc2b656eb2016-07-21 11:57:01 +0200219 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE5;
Jelle van der Waa8d3d7c12016-01-14 14:06:26 +0100220#endif
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200221 default:
222 printf("Missing mvtwsi controller %d base\n", adap->hwadapnr);
223 break;
224 }
225
226 return NULL;
227}
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +0200228#endif
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200229
230/*
mario.six@gdsys.ccf43d3e92016-07-21 11:57:02 +0200231 * enum mvtwsi_error_class - types of I2C errors
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200232 */
mario.six@gdsys.ccf43d3e92016-07-21 11:57:02 +0200233enum 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};
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200239
mario.six@gdsys.ccf43d3e92016-07-21 11:57:02 +0200240/*
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}
Albert Aribaud04280c42010-08-27 18:26:05 +0200260
261/*
mario.six@gdsys.ccefd1fb82016-07-21 11:57:13 +0200262 * 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.
Albert Aribaud04280c42010-08-27 18:26:05 +0200266 */
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200267static int twsi_wait(struct mvtwsi_registers *twsi, int expected_status,
268 uint tick)
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200269{
Albert Aribaud04280c42010-08-27 18:26:05 +0200270 int control, status;
271 int timeout = 1000;
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200272
Albert Aribaud04280c42010-08-27 18:26:05 +0200273 do {
274 control = readl(&twsi->control);
275 if (control & MVTWSI_CONTROL_IFLG) {
Marek Behúnb9739da2019-05-02 16:53:38 +0200276 /*
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);
Albert Aribaud04280c42010-08-27 18:26:05 +0200287 status = readl(&twsi->status);
288 if (status == expected_status)
289 return 0;
290 else
mario.six@gdsys.ccf43d3e92016-07-21 11:57:02 +0200291 return mvtwsi_error(
Albert Aribaud04280c42010-08-27 18:26:05 +0200292 MVTWSI_ERROR_WRONG_STATUS,
293 control, status, expected_status);
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200294 }
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200295 ndelay(tick); /* One clock cycle */
Albert Aribaud04280c42010-08-27 18:26:05 +0200296 } while (timeout--);
297 status = readl(&twsi->status);
mario.six@gdsys.ccf43d3e92016-07-21 11:57:02 +0200298 return mvtwsi_error(MVTWSI_ERROR_TIMEOUT, control, status,
299 expected_status);
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200300}
301
Albert Aribaud04280c42010-08-27 18:26:05 +0200302/*
mario.six@gdsys.ccefd1fb82016-07-21 11:57:13 +0200303 * 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.
Albert Aribaud04280c42010-08-27 18:26:05 +0200314 */
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200315static int twsi_start(struct mvtwsi_registers *twsi, int expected_status,
316 uint tick)
Albert Aribaud04280c42010-08-27 18:26:05 +0200317{
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200318 /* Assert START */
mario.six@gdsys.cc1cc2c282016-07-21 11:57:04 +0200319 writel(MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_START |
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200320 MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
321 /* Wait for controller to process START */
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200322 return twsi_wait(twsi, expected_status, tick);
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200323}
324
Albert Aribaud04280c42010-08-27 18:26:05 +0200325/*
mario.six@gdsys.ccefd1fb82016-07-21 11:57:13 +0200326 * 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.
Albert Aribaud04280c42010-08-27 18:26:05 +0200337 */
mario.six@gdsys.cc9f7f6892016-07-21 11:57:08 +0200338static int twsi_send(struct mvtwsi_registers *twsi, u8 byte,
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200339 int expected_status, uint tick)
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200340{
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200341 /* Write byte to data register for sending */
Albert Aribaud04280c42010-08-27 18:26:05 +0200342 writel(byte, &twsi->data);
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200343 /* Clear any pending interrupt -- that will cause sending */
mario.six@gdsys.cc1cc2c282016-07-21 11:57:04 +0200344 writel(MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_CLEAR_IFLG,
345 &twsi->control);
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200346 /* Wait for controller to receive byte, and check ACK */
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200347 return twsi_wait(twsi, expected_status, tick);
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200348}
349
Albert Aribaud04280c42010-08-27 18:26:05 +0200350/*
mario.six@gdsys.ccefd1fb82016-07-21 11:57:13 +0200351 * 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.
Albert Aribaud04280c42010-08-27 18:26:05 +0200362 */
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200363static int twsi_recv(struct mvtwsi_registers *twsi, u8 *byte, int ack_flag,
364 uint tick)
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200365{
mario.six@gdsys.cc1cc2c282016-07-21 11:57:04 +0200366 int expected_status, status, control;
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200367
mario.six@gdsys.cc1cc2c282016-07-21 11:57:04 +0200368 /* Compute expected status based on passed ACK flag */
369 expected_status = ack_flag ? MVTWSI_STATUS_DATA_R_ACK :
370 MVTWSI_STATUS_DATA_R_NAK;
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200371 /* Acknowledge *previous state*, and launch receive */
mario.six@gdsys.cc1cc2c282016-07-21 11:57:04 +0200372 control = MVTWSI_CONTROL_TWSIEN;
373 control |= ack_flag == MVTWSI_READ_ACK ? MVTWSI_CONTROL_ACK : 0;
374 writel(control | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200375 /* Wait for controller to receive byte, and assert ACK or NAK */
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200376 status = twsi_wait(twsi, expected_status, tick);
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200377 /* If we did receive the expected byte, store it */
Albert Aribaud04280c42010-08-27 18:26:05 +0200378 if (status == 0)
379 *byte = readl(&twsi->data);
Albert Aribaud04280c42010-08-27 18:26:05 +0200380 return status;
381}
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200382
Albert Aribaud04280c42010-08-27 18:26:05 +0200383/*
mario.six@gdsys.ccefd1fb82016-07-21 11:57:13 +0200384 * 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.
Albert Aribaud04280c42010-08-27 18:26:05 +0200393 */
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200394static int twsi_stop(struct mvtwsi_registers *twsi, uint tick)
Albert Aribaud04280c42010-08-27 18:26:05 +0200395{
396 int control, stop_status;
mario.six@gdsys.ccbdf0f662016-07-21 11:57:05 +0200397 int status = 0;
Albert Aribaud04280c42010-08-27 18:26:05 +0200398 int timeout = 1000;
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200399
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200400 /* Assert STOP */
Albert Aribaud04280c42010-08-27 18:26:05 +0200401 control = MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_STOP;
Hans de Goede6b703e02016-01-14 14:06:25 +0100402 writel(control | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200403 /* Wait for IDLE; IFLG won't rise, so we can't use twsi_wait() */
Albert Aribaud04280c42010-08-27 18:26:05 +0200404 do {
405 stop_status = readl(&twsi->status);
406 if (stop_status == MVTWSI_STATUS_IDLE)
407 break;
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200408 ndelay(tick); /* One clock cycle */
Albert Aribaud04280c42010-08-27 18:26:05 +0200409 } while (timeout--);
410 control = readl(&twsi->control);
411 if (stop_status != MVTWSI_STATUS_IDLE)
mario.six@gdsys.ccbdf0f662016-07-21 11:57:05 +0200412 status = mvtwsi_error(MVTWSI_ERROR_TIMEOUT,
413 control, status, MVTWSI_STATUS_IDLE);
Albert Aribaud04280c42010-08-27 18:26:05 +0200414 return status;
415}
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200416
mario.six@gdsys.ccefd1fb82016-07-21 11:57:13 +0200417/*
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 */
mario.six@gdsys.cc9e118b32016-07-21 11:57:06 +0200424static uint twsi_calc_freq(const int n, const int m)
Stefan Roesecca56a72015-03-18 09:30:54 +0100425{
Jagan Teki68078f72016-10-13 14:19:35 +0530426#ifdef CONFIG_ARCH_SUNXI
Stefan Roesecca56a72015-03-18 09:30:54 +0100427 return CONFIG_SYS_TCLK / (10 * (m + 1) * (1 << n));
428#else
429 return CONFIG_SYS_TCLK / (10 * (m + 1) * (2 << n));
430#endif
431}
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200432
Albert Aribaud04280c42010-08-27 18:26:05 +0200433/*
mario.six@gdsys.ccefd1fb82016-07-21 11:57:13 +0200434 * 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.
Albert Aribaud04280c42010-08-27 18:26:05 +0200440 */
mario.six@gdsys.cc9f7f6892016-07-21 11:57:08 +0200441static void twsi_reset(struct mvtwsi_registers *twsi)
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200442{
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200443 /* Reset controller */
Albert Aribaud04280c42010-08-27 18:26:05 +0200444 writel(0, &twsi->soft_reset);
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200445 /* Wait 2 ms -- this is what the Marvell LSP does */
Albert Aribaud04280c42010-08-27 18:26:05 +0200446 udelay(20000);
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200447}
448
Albert Aribaud04280c42010-08-27 18:26:05 +0200449/*
mario.six@gdsys.ccefd1fb82016-07-21 11:57:13 +0200450 * __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.
Albert Aribaud04280c42010-08-27 18:26:05 +0200459 */
mario.six@gdsys.cc9f7f6892016-07-21 11:57:08 +0200460static uint __twsi_i2c_set_bus_speed(struct mvtwsi_registers *twsi,
mario.six@gdsys.cca4ac8b72016-07-21 11:57:07 +0200461 uint requested_speed)
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200462{
mario.six@gdsys.cc9e118b32016-07-21 11:57:06 +0200463 uint tmp_speed, highest_speed, n, m;
464 uint baud = 0x44; /* Baud rate after controller reset */
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200465
Albert Aribaud04280c42010-08-27 18:26:05 +0200466 highest_speed = 0;
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200467 /* Successively try m, n combinations, and use the combination
468 * resulting in the largest speed that's not above the requested
469 * speed */
Albert Aribaud04280c42010-08-27 18:26:05 +0200470 for (n = 0; n < 8; n++) {
471 for (m = 0; m < 16; m++) {
Stefan Roesecca56a72015-03-18 09:30:54 +0100472 tmp_speed = twsi_calc_freq(n, m);
mario.six@gdsys.cc2b656eb2016-07-21 11:57:01 +0200473 if ((tmp_speed <= requested_speed) &&
474 (tmp_speed > highest_speed)) {
Albert Aribaud04280c42010-08-27 18:26:05 +0200475 highest_speed = tmp_speed;
476 baud = (m << 3) | n;
477 }
478 }
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200479 }
Hans de Goede9830f1c2014-06-13 22:55:48 +0200480 writel(baud, &twsi->baudrate);
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200481
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;
Hans de Goede9830f1c2014-06-13 22:55:48 +0200489}
490
mario.six@gdsys.ccefd1fb82016-07-21 11:57:13 +0200491/*
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 */
mario.six@gdsys.cc9f7f6892016-07-21 11:57:08 +0200503static void __twsi_i2c_init(struct mvtwsi_registers *twsi, int speed,
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200504 int slaveadd, uint *actual_speed)
Hans de Goede9830f1c2014-06-13 22:55:48 +0200505{
Stefan Mavrodiev6425ebd2018-02-13 09:27:40 +0200506 uint tmp_speed;
507
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200508 /* Reset controller */
mario.six@gdsys.cc9f7f6892016-07-21 11:57:08 +0200509 twsi_reset(twsi);
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200510 /* Set speed */
Stefan Mavrodiev6425ebd2018-02-13 09:27:40 +0200511 tmp_speed = __twsi_i2c_set_bus_speed(twsi, speed);
Heinrich Schuchardt5e0fd542018-01-31 00:57:17 +0100512 if (actual_speed)
Stefan Mavrodiev6425ebd2018-02-13 09:27:40 +0200513 *actual_speed = tmp_speed;
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200514 /* Set slave address; even though we don't use it */
Hans de Goede9830f1c2014-06-13 22:55:48 +0200515 writel(slaveadd, &twsi->slave_address);
516 writel(0, &twsi->xtnd_slave_addr);
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200517 /* Assert STOP, but don't care for the result */
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200518#ifdef CONFIG_DM_I2C
519 (void) twsi_stop(twsi, calc_tick(*actual_speed));
520#else
521 (void) twsi_stop(twsi, 10000);
522#endif
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200523}
524
Albert Aribaud04280c42010-08-27 18:26:05 +0200525/*
mario.six@gdsys.ccefd1fb82016-07-21 11:57:13 +0200526 * 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.
Albert Aribaud04280c42010-08-27 18:26:05 +0200541 */
mario.six@gdsys.cc9f7f6892016-07-21 11:57:08 +0200542static int i2c_begin(struct mvtwsi_registers *twsi, int expected_start_status,
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200543 u8 addr, uint tick)
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200544{
Albert Aribaud04280c42010-08-27 18:26:05 +0200545 int status, expected_addr_status;
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200546
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200547 /* Compute the expected address status from the direction bit in
548 * the address byte */
549 if (addr & 1) /* Reading */
Albert Aribaud04280c42010-08-27 18:26:05 +0200550 expected_addr_status = MVTWSI_STATUS_ADDR_R_ACK;
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200551 else /* Writing */
Albert Aribaud04280c42010-08-27 18:26:05 +0200552 expected_addr_status = MVTWSI_STATUS_ADDR_W_ACK;
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200553 /* Assert START */
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200554 status = twsi_start(twsi, expected_start_status, tick);
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200555 /* Send out the address if the start went well */
Albert Aribaud04280c42010-08-27 18:26:05 +0200556 if (status == 0)
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200557 status = twsi_send(twsi, addr, expected_addr_status, tick);
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200558 /* Return 0, or the status of the first failure */
Albert Aribaud04280c42010-08-27 18:26:05 +0200559 return status;
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200560}
561
Albert Aribaud04280c42010-08-27 18:26:05 +0200562/*
mario.six@gdsys.ccefd1fb82016-07-21 11:57:13 +0200563 * __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.
Albert Aribaud04280c42010-08-27 18:26:05 +0200573 */
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200574static int __twsi_i2c_probe_chip(struct mvtwsi_registers *twsi, uchar chip,
575 uint tick)
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200576{
Albert Aribaud04280c42010-08-27 18:26:05 +0200577 u8 dummy_byte;
578 int status;
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200579
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200580 /* Begin i2c read */
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200581 status = i2c_begin(twsi, MVTWSI_STATUS_START, (chip << 1) | 1, tick);
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200582 /* Dummy read was accepted: receive byte, but NAK it. */
Albert Aribaud04280c42010-08-27 18:26:05 +0200583 if (status == 0)
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200584 status = twsi_recv(twsi, &dummy_byte, MVTWSI_READ_NAK, tick);
Albert Aribaud04280c42010-08-27 18:26:05 +0200585 /* Stop transaction */
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200586 twsi_stop(twsi, tick);
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200587 /* Return 0, or the status of the first failure */
Albert Aribaud04280c42010-08-27 18:26:05 +0200588 return status;
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200589}
590
Albert Aribaud04280c42010-08-27 18:26:05 +0200591/*
mario.six@gdsys.ccefd1fb82016-07-21 11:57:13 +0200592 * __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.
Albert Aribaud04280c42010-08-27 18:26:05 +0200596 *
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200597 * 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.
mario.six@gdsys.ccefd1fb82016-07-21 11:57:13 +0200601 *
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.
Albert Aribaud04280c42010-08-27 18:26:05 +0200612 */
mario.six@gdsys.cc9f7f6892016-07-21 11:57:08 +0200613static int __twsi_i2c_read(struct mvtwsi_registers *twsi, uchar chip,
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200614 u8 *addr, int alen, uchar *data, int length,
615 uint tick)
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200616{
mario.six@gdsys.ccbdf0f662016-07-21 11:57:05 +0200617 int status = 0;
618 int stop_status;
mario.six@gdsys.cc029a84b2016-07-21 11:57:11 +0200619 int expected_start = MVTWSI_STATUS_START;
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200620
mario.six@gdsys.cc029a84b2016-07-21 11:57:11 +0200621 if (alen > 0) {
622 /* Begin i2c write to send the address bytes */
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200623 status = i2c_begin(twsi, expected_start, (chip << 1), tick);
mario.six@gdsys.cc029a84b2016-07-21 11:57:11 +0200624 /* Send address bytes */
625 while ((status == 0) && alen--)
Stefan Roeseabd7d312016-08-25 15:20:01 +0200626 status = twsi_send(twsi, addr[alen],
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200627 MVTWSI_STATUS_DATA_W_ACK, tick);
mario.six@gdsys.cc029a84b2016-07-21 11:57:11 +0200628 /* Send repeated STARTs after the initial START */
629 expected_start = MVTWSI_STATUS_REPEATED_START;
630 }
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200631 /* Begin i2c read to receive data bytes */
Albert Aribaud04280c42010-08-27 18:26:05 +0200632 if (status == 0)
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200633 status = i2c_begin(twsi, expected_start, (chip << 1) | 1, tick);
mario.six@gdsys.cc1cc2c282016-07-21 11:57:04 +0200634 /* Receive actual data bytes; set NAK if we if we have nothing more to
635 * read */
636 while ((status == 0) && length--)
mario.six@gdsys.cc9f7f6892016-07-21 11:57:08 +0200637 status = twsi_recv(twsi, data++,
mario.six@gdsys.cc1cc2c282016-07-21 11:57:04 +0200638 length > 0 ?
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200639 MVTWSI_READ_ACK : MVTWSI_READ_NAK, tick);
Albert Aribaud04280c42010-08-27 18:26:05 +0200640 /* Stop transaction */
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200641 stop_status = twsi_stop(twsi, tick);
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200642 /* Return 0, or the status of the first failure */
mario.six@gdsys.ccbdf0f662016-07-21 11:57:05 +0200643 return status != 0 ? status : stop_status;
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200644}
645
Albert Aribaud04280c42010-08-27 18:26:05 +0200646/*
mario.six@gdsys.ccefd1fb82016-07-21 11:57:13 +0200647 * __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.
Albert Aribaud04280c42010-08-27 18:26:05 +0200661 */
mario.six@gdsys.cc9f7f6892016-07-21 11:57:08 +0200662static int __twsi_i2c_write(struct mvtwsi_registers *twsi, uchar chip,
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200663 u8 *addr, int alen, uchar *data, int length,
664 uint tick)
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200665{
mario.six@gdsys.ccbdf0f662016-07-21 11:57:05 +0200666 int status, stop_status;
Albert Aribaud04280c42010-08-27 18:26:05 +0200667
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200668 /* Begin i2c write to send first the address bytes, then the
669 * data bytes */
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200670 status = i2c_begin(twsi, MVTWSI_STATUS_START, (chip << 1), tick);
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200671 /* Send address bytes */
mario.six@gdsys.ccc4eceb52016-07-21 11:57:09 +0200672 while ((status == 0) && (alen-- > 0))
Stefan Roeseabd7d312016-08-25 15:20:01 +0200673 status = twsi_send(twsi, addr[alen], MVTWSI_STATUS_DATA_W_ACK,
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200674 tick);
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200675 /* Send data bytes */
Albert Aribaud04280c42010-08-27 18:26:05 +0200676 while ((status == 0) && (length-- > 0))
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200677 status = twsi_send(twsi, *(data++), MVTWSI_STATUS_DATA_W_ACK,
678 tick);
Albert Aribaud04280c42010-08-27 18:26:05 +0200679 /* Stop transaction */
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200680 stop_status = twsi_stop(twsi, tick);
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200681 /* Return 0, or the status of the first failure */
mario.six@gdsys.ccbdf0f662016-07-21 11:57:05 +0200682 return status != 0 ? status : stop_status;
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200683}
684
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +0200685#ifndef CONFIG_DM_I2C
mario.six@gdsys.cca4ac8b72016-07-21 11:57:07 +0200686static void twsi_i2c_init(struct i2c_adapter *adap, int speed,
687 int slaveadd)
688{
mario.six@gdsys.cc9f7f6892016-07-21 11:57:08 +0200689 struct mvtwsi_registers *twsi = twsi_get_base(adap);
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200690 __twsi_i2c_init(twsi, speed, slaveadd, NULL);
mario.six@gdsys.cca4ac8b72016-07-21 11:57:07 +0200691}
692
693static uint twsi_i2c_set_bus_speed(struct i2c_adapter *adap,
694 uint requested_speed)
695{
mario.six@gdsys.cc9f7f6892016-07-21 11:57:08 +0200696 struct mvtwsi_registers *twsi = twsi_get_base(adap);
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200697 __twsi_i2c_set_bus_speed(twsi, requested_speed);
698 return 0;
mario.six@gdsys.cca4ac8b72016-07-21 11:57:07 +0200699}
700
701static int twsi_i2c_probe(struct i2c_adapter *adap, uchar chip)
702{
mario.six@gdsys.cc9f7f6892016-07-21 11:57:08 +0200703 struct mvtwsi_registers *twsi = twsi_get_base(adap);
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200704 return __twsi_i2c_probe_chip(twsi, chip, 10000);
mario.six@gdsys.cca4ac8b72016-07-21 11:57:07 +0200705}
706
707static int twsi_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
708 int alen, uchar *data, int length)
709{
mario.six@gdsys.cc9f7f6892016-07-21 11:57:08 +0200710 struct mvtwsi_registers *twsi = twsi_get_base(adap);
mario.six@gdsys.ccc4eceb52016-07-21 11:57:09 +0200711 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
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200718 return __twsi_i2c_read(twsi, chip, addr_bytes, alen, data, length,
719 10000);
mario.six@gdsys.cca4ac8b72016-07-21 11:57:07 +0200720}
721
722static int twsi_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
723 int alen, uchar *data, int length)
724{
mario.six@gdsys.cc9f7f6892016-07-21 11:57:08 +0200725 struct mvtwsi_registers *twsi = twsi_get_base(adap);
mario.six@gdsys.ccc4eceb52016-07-21 11:57:09 +0200726 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
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200733 return __twsi_i2c_write(twsi, chip, addr_bytes, alen, data, length,
734 10000);
mario.six@gdsys.cca4ac8b72016-07-21 11:57:07 +0200735}
736
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200737#ifdef CONFIG_I2C_MVTWSI_BASE0
Hans de Goede9830f1c2014-06-13 22:55:48 +0200738U_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)
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200742#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
Jelle van der Waa8d3d7c12016-01-14 14:06:26 +0100771#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
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +0200778#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);
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200784 return __twsi_i2c_probe_chip(dev->base, chip_addr, dev->tick);
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +0200785}
786
787static int mvtwsi_i2c_set_bus_speed(struct udevice *bus, uint speed)
788{
789 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200790
791 dev->speed = __twsi_i2c_set_bus_speed(dev->base, speed);
792 dev->tick = calc_tick(dev->speed);
793
794 return 0;
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +0200795}
796
Simon Glassaad29ae2020-12-03 16:55:21 -0700797static int mvtwsi_i2c_of_to_plat(struct udevice *bus)
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +0200798{
799 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
800
Masahiro Yamada32822d02020-08-04 14:14:43 +0900801 dev->base = dev_read_addr_ptr(bus);
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +0200802
803 if (!dev->base)
804 return -ENOMEM;
805
Simon Glassdd79d6e2017-01-17 16:52:55 -0700806 dev->index = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus),
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +0200807 "cell-index", -1);
Simon Glassdd79d6e2017-01-17 16:52:55 -0700808 dev->slaveadd = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus),
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +0200809 "u-boot,i2c-slave-addr", 0x0);
Simon Glassf0c99c52020-01-23 11:48:22 -0700810 dev->speed = dev_read_u32_default(bus, "clock-frequency",
811 I2C_SPEED_STANDARD_RATE);
812
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +0200813 return 0;
814}
815
Baruch Siach91006c72018-06-07 12:38:10 +0300816static 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{
Masahiro Yamada32822d02020-08-04 14:14:43 +0900823 struct mvtwsi_registers *twsi = dev_read_addr_ptr(bus);
Baruch Siach91006c72018-06-07 12:38:10 +0300824
825 /* Disable the hidden slave in i2c0 of these platforms */
Simon Glass4123ba02020-12-16 21:20:15 -0700826 if ((IS_ENABLED(CONFIG_ARMADA_38X) ||
827 IS_ENABLED(CONFIG_ARCH_KIRKWOOD) ||
828 IS_ENABLED(CONFIG_ARMADA_8K)) && !dev_seq(bus))
Baruch Siach91006c72018-06-07 12:38:10 +0300829 twsi_disable_i2c_slave(twsi);
830
831 return 0;
832}
833
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +0200834static int mvtwsi_i2c_probe(struct udevice *bus)
835{
836 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200837 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);
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +0200842 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,
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200864 omsg->len, dmsg->buf, dmsg->len,
865 dev->tick);
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +0200866 else
867 return __twsi_i2c_write(dev->base, dmsg->addr, omsg->buf,
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200868 omsg->len, dmsg->buf, dmsg->len,
869 dev->tick);
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +0200870}
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", },
Stefan Roese58e58d82016-09-16 15:07:55 +0200880 { .compatible = "marvell,mv78230-i2c", },
Jernej Skrabec9220d502017-04-27 00:03:36 +0200881 { .compatible = "allwinner,sun6i-a31-i2c", },
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +0200882 { /* sentinel */ }
883};
884
885U_BOOT_DRIVER(i2c_mvtwsi) = {
886 .name = "i2c_mvtwsi",
887 .id = UCLASS_I2C,
888 .of_match = mvtwsi_i2c_ids,
Baruch Siach91006c72018-06-07 12:38:10 +0300889 .bind = mvtwsi_i2c_bind,
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +0200890 .probe = mvtwsi_i2c_probe,
Simon Glassaad29ae2020-12-03 16:55:21 -0700891 .of_to_plat = mvtwsi_i2c_of_to_plat,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700892 .priv_auto = sizeof(struct mvtwsi_i2c_dev),
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +0200893 .ops = &mvtwsi_i2c_ops,
894};
895#endif /* CONFIG_DM_I2C */