blob: d33e2c7c9d836086bf64e159c77392cf34576b22 [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 Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Simon Glassdbd79542020-05-10 11:40:11 -060014#include <linux/delay.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090015#include <linux/errno.h>
Heiko Schocher5cd2a242009-07-20 09:59:37 +020016#include <asm/io.h>
Baruch Siach91006c72018-06-07 12:38:10 +030017#include <linux/bitops.h>
mario.six@gdsys.cce841b582016-07-21 11:57:12 +020018#include <linux/compat.h>
Igor Opaniukf7c91762021-02-09 13:52:45 +020019#if CONFIG_IS_ENABLED(DM_I2C)
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +020020#include <dm.h>
21#endif
22
23DECLARE_GLOBAL_DATA_PTR;
Heiko Schocher5cd2a242009-07-20 09:59:37 +020024
Albert Aribaud04280c42010-08-27 18:26:05 +020025/*
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +020026 * Include a file that will provide CONFIG_I2C_MVTWSI_BASE*, and possibly other
27 * settings
Albert Aribaud04280c42010-08-27 18:26:05 +020028 */
Heiko Schocher5cd2a242009-07-20 09:59:37 +020029
Igor Opaniukf7c91762021-02-09 13:52:45 +020030#if !CONFIG_IS_ENABLED(DM_I2C)
Trevor Woernerf9953752020-05-06 08:02:38 -040031#if defined(CONFIG_ARCH_ORION5X)
Albert Aribaud04280c42010-08-27 18:26:05 +020032#include <asm/arch/orion5x.h>
Trevor Woernerbb7ab072020-05-06 08:02:40 -040033#elif (defined(CONFIG_ARCH_KIRKWOOD) || defined(CONFIG_ARCH_MVEBU))
Stefan Roesec2437842014-10-22 12:13:06 +020034#include <asm/arch/soc.h>
Jagan Teki68078f72016-10-13 14:19:35 +053035#elif defined(CONFIG_ARCH_SUNXI)
Hans de Goede3352b222014-06-13 22:55:49 +020036#include <asm/arch/i2c.h>
Albert Aribaud04280c42010-08-27 18:26:05 +020037#else
38#error Driver mvtwsi not supported by SoC or board
Heiko Schocher5cd2a242009-07-20 09:59:37 +020039#endif
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +020040#endif /* CONFIG_DM_I2C */
Heiko Schocher5cd2a242009-07-20 09:59:37 +020041
Albert Aribaud04280c42010-08-27 18:26:05 +020042/*
Jernej Skrabec9220d502017-04-27 00:03:36 +020043 * On SUNXI, we get CONFIG_SYS_TCLK from this include, so we want to
44 * always have it.
45 */
Igor Opaniukf7c91762021-02-09 13:52:45 +020046#if CONFIG_IS_ENABLED(DM_I2C) && defined(CONFIG_ARCH_SUNXI)
Jernej Skrabec9220d502017-04-27 00:03:36 +020047#include <asm/arch/i2c.h>
48#endif
49
50/*
Albert Aribaud04280c42010-08-27 18:26:05 +020051 * TWSI register structure
52 */
Heiko Schocher5cd2a242009-07-20 09:59:37 +020053
Jagan Teki68078f72016-10-13 14:19:35 +053054#ifdef CONFIG_ARCH_SUNXI
Hans de Goede3352b222014-06-13 22:55:49 +020055
Albert Aribaud04280c42010-08-27 18:26:05 +020056struct mvtwsi_registers {
57 u32 slave_address;
Hans de Goede3352b222014-06-13 22:55:49 +020058 u32 xtnd_slave_addr;
Albert Aribaud04280c42010-08-27 18:26:05 +020059 u32 data;
60 u32 control;
Hans de Goede3352b222014-06-13 22:55:49 +020061 u32 status;
62 u32 baudrate;
63 u32 soft_reset;
Baruch Siach91006c72018-06-07 12:38:10 +030064 u32 debug; /* Dummy field for build compatibility with mvebu */
Hans de Goede3352b222014-06-13 22:55:49 +020065};
66
67#else
68
69struct mvtwsi_registers {
70 u32 slave_address;
71 u32 data;
72 u32 control;
Albert Aribaud04280c42010-08-27 18:26:05 +020073 union {
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +020074 u32 status; /* When reading */
75 u32 baudrate; /* When writing */
Albert Aribaud04280c42010-08-27 18:26:05 +020076 };
77 u32 xtnd_slave_addr;
Baruch Siach91006c72018-06-07 12:38:10 +030078 u32 reserved0[2];
Albert Aribaud04280c42010-08-27 18:26:05 +020079 u32 soft_reset;
Baruch Siach91006c72018-06-07 12:38:10 +030080 u32 reserved1[27];
81 u32 debug;
Heiko Schocher5cd2a242009-07-20 09:59:37 +020082};
83
Hans de Goede3352b222014-06-13 22:55:49 +020084#endif
85
Igor Opaniukf7c91762021-02-09 13:52:45 +020086#if CONFIG_IS_ENABLED(DM_I2C)
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +020087struct mvtwsi_i2c_dev {
88 /* TWSI Register base for the device */
89 struct mvtwsi_registers *base;
90 /* Number of the device (determined from cell-index property) */
91 int index;
92 /* The I2C slave address for the device */
93 u8 slaveadd;
94 /* The configured I2C speed in Hz */
95 uint speed;
mario.six@gdsys.cce841b582016-07-21 11:57:12 +020096 /* The current length of a clock period (depending on speed) */
97 uint tick;
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +020098};
99#endif /* CONFIG_DM_I2C */
100
Albert Aribaud04280c42010-08-27 18:26:05 +0200101/*
mario.six@gdsys.ccf43d3e92016-07-21 11:57:02 +0200102 * enum mvtwsi_ctrl_register_fields - Bit masks for flags in the control
103 * register
Albert Aribaud04280c42010-08-27 18:26:05 +0200104 */
mario.six@gdsys.ccf43d3e92016-07-21 11:57:02 +0200105enum mvtwsi_ctrl_register_fields {
106 /* Acknowledge bit */
107 MVTWSI_CONTROL_ACK = 0x00000004,
108 /* Interrupt flag */
109 MVTWSI_CONTROL_IFLG = 0x00000008,
110 /* Stop bit */
111 MVTWSI_CONTROL_STOP = 0x00000010,
112 /* Start bit */
113 MVTWSI_CONTROL_START = 0x00000020,
114 /* I2C enable */
115 MVTWSI_CONTROL_TWSIEN = 0x00000040,
116 /* Interrupt enable */
117 MVTWSI_CONTROL_INTEN = 0x00000080,
118};
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200119
Albert Aribaud04280c42010-08-27 18:26:05 +0200120/*
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200121 * On sun6i and newer, IFLG is a write-clear bit, which is cleared by writing 1;
122 * on other platforms, it is a normal r/w bit, which is cleared by writing 0.
Hans de Goede6b703e02016-01-14 14:06:25 +0100123 */
124
Jernej Skrabecd4330c62021-01-11 21:11:36 +0100125#if defined(CONFIG_SUNXI_GEN_SUN6I) || defined(CONFIG_SUN50I_GEN_H6)
Hans de Goede6b703e02016-01-14 14:06:25 +0100126#define MVTWSI_CONTROL_CLEAR_IFLG 0x00000008
127#else
128#define MVTWSI_CONTROL_CLEAR_IFLG 0x00000000
129#endif
130
131/*
mario.six@gdsys.ccf43d3e92016-07-21 11:57:02 +0200132 * enum mvstwsi_status_values - Possible values of I2C controller's status
133 * register
134 *
135 * Only those statuses expected in normal master operation on
136 * non-10-bit-address devices are specified.
137 *
138 * Every status that's unexpected during normal operation (bus errors,
139 * arbitration losses, missing ACKs...) is passed back to the caller as an error
Albert Aribaud04280c42010-08-27 18:26:05 +0200140 * code.
141 */
mario.six@gdsys.ccf43d3e92016-07-21 11:57:02 +0200142enum mvstwsi_status_values {
143 /* START condition transmitted */
144 MVTWSI_STATUS_START = 0x08,
145 /* Repeated START condition transmitted */
146 MVTWSI_STATUS_REPEATED_START = 0x10,
147 /* Address + write bit transmitted, ACK received */
148 MVTWSI_STATUS_ADDR_W_ACK = 0x18,
149 /* Data transmitted, ACK received */
150 MVTWSI_STATUS_DATA_W_ACK = 0x28,
151 /* Address + read bit transmitted, ACK received */
152 MVTWSI_STATUS_ADDR_R_ACK = 0x40,
153 /* Address + read bit transmitted, ACK not received */
154 MVTWSI_STATUS_ADDR_R_NAK = 0x48,
155 /* Data received, ACK transmitted */
156 MVTWSI_STATUS_DATA_R_ACK = 0x50,
157 /* Data received, ACK not transmitted */
158 MVTWSI_STATUS_DATA_R_NAK = 0x58,
159 /* No relevant status */
160 MVTWSI_STATUS_IDLE = 0xF8,
161};
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200162
Albert Aribaud04280c42010-08-27 18:26:05 +0200163/*
mario.six@gdsys.cc1cc2c282016-07-21 11:57:04 +0200164 * enum mvstwsi_ack_flags - Determine whether a read byte should be
165 * acknowledged or not.
166 */
167enum mvtwsi_ack_flags {
168 /* Send NAK after received byte */
169 MVTWSI_READ_NAK = 0,
170 /* Send ACK after received byte */
171 MVTWSI_READ_ACK = 1,
172};
173
mario.six@gdsys.ccefd1fb82016-07-21 11:57:13 +0200174/*
175 * calc_tick() - Calculate the duration of a clock cycle from the I2C speed
176 *
177 * @speed: The speed in Hz to calculate the clock cycle duration for.
178 * @return The duration of a clock cycle in ns.
179 */
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200180inline uint calc_tick(uint speed)
181{
182 /* One tick = the duration of a period at the specified speed in ns (we
183 * add 100 ns to be on the safe side) */
184 return (1000000000u / speed) + 100;
185}
186
Igor Opaniukf7c91762021-02-09 13:52:45 +0200187#if !CONFIG_IS_ENABLED(DM_I2C)
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200188
mario.six@gdsys.cc1cc2c282016-07-21 11:57:04 +0200189/*
mario.six@gdsys.ccefd1fb82016-07-21 11:57:13 +0200190 * twsi_get_base() - Get controller register base for specified adapter
191 *
192 * @adap: Adapter to get the register base for.
193 * @return Register base for the specified adapter.
Albert Aribaud04280c42010-08-27 18:26:05 +0200194 */
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200195static struct mvtwsi_registers *twsi_get_base(struct i2c_adapter *adap)
196{
197 switch (adap->hwadapnr) {
198#ifdef CONFIG_I2C_MVTWSI_BASE0
199 case 0:
mario.six@gdsys.cc2b656eb2016-07-21 11:57:01 +0200200 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE0;
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200201#endif
202#ifdef CONFIG_I2C_MVTWSI_BASE1
203 case 1:
mario.six@gdsys.cc2b656eb2016-07-21 11:57:01 +0200204 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE1;
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200205#endif
206#ifdef CONFIG_I2C_MVTWSI_BASE2
207 case 2:
mario.six@gdsys.cc2b656eb2016-07-21 11:57:01 +0200208 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE2;
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200209#endif
210#ifdef CONFIG_I2C_MVTWSI_BASE3
211 case 3:
mario.six@gdsys.cc2b656eb2016-07-21 11:57:01 +0200212 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE3;
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200213#endif
214#ifdef CONFIG_I2C_MVTWSI_BASE4
215 case 4:
mario.six@gdsys.cc2b656eb2016-07-21 11:57:01 +0200216 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE4;
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200217#endif
Jelle van der Waa8d3d7c12016-01-14 14:06:26 +0100218#ifdef CONFIG_I2C_MVTWSI_BASE5
219 case 5:
mario.six@gdsys.cc2b656eb2016-07-21 11:57:01 +0200220 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE5;
Jelle van der Waa8d3d7c12016-01-14 14:06:26 +0100221#endif
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200222 default:
223 printf("Missing mvtwsi controller %d base\n", adap->hwadapnr);
224 break;
225 }
226
227 return NULL;
228}
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +0200229#endif
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200230
231/*
mario.six@gdsys.ccf43d3e92016-07-21 11:57:02 +0200232 * enum mvtwsi_error_class - types of I2C errors
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200233 */
mario.six@gdsys.ccf43d3e92016-07-21 11:57:02 +0200234enum mvtwsi_error_class {
235 /* The controller returned a different status than expected */
236 MVTWSI_ERROR_WRONG_STATUS = 0x01,
237 /* The controller timed out */
238 MVTWSI_ERROR_TIMEOUT = 0x02,
239};
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200240
mario.six@gdsys.ccf43d3e92016-07-21 11:57:02 +0200241/*
242 * mvtwsi_error() - Build I2C return code from error information
243 *
244 * For debugging purposes, this function packs some information of an occurred
245 * error into a return code. These error codes are returned from I2C API
246 * functions (i2c_{read,write}, dm_i2c_{read,write}, etc.).
247 *
248 * @ec: The error class of the error (enum mvtwsi_error_class).
249 * @lc: The last value of the control register.
250 * @ls: The last value of the status register.
251 * @es: The expected value of the status register.
252 * @return The generated error code.
253 */
254inline uint mvtwsi_error(uint ec, uint lc, uint ls, uint es)
255{
256 return ((ec << 24) & 0xFF000000)
257 | ((lc << 16) & 0x00FF0000)
258 | ((ls << 8) & 0x0000FF00)
259 | (es & 0xFF);
260}
Albert Aribaud04280c42010-08-27 18:26:05 +0200261
262/*
mario.six@gdsys.ccefd1fb82016-07-21 11:57:13 +0200263 * twsi_wait() - Wait for I2C bus interrupt flag and check status, or time out.
264 *
265 * @return Zero if status is as expected, or a non-zero code if either a time
266 * out occurred, or the status was not the expected one.
Albert Aribaud04280c42010-08-27 18:26:05 +0200267 */
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200268static int twsi_wait(struct mvtwsi_registers *twsi, int expected_status,
269 uint tick)
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200270{
Albert Aribaud04280c42010-08-27 18:26:05 +0200271 int control, status;
272 int timeout = 1000;
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200273
Albert Aribaud04280c42010-08-27 18:26:05 +0200274 do {
275 control = readl(&twsi->control);
276 if (control & MVTWSI_CONTROL_IFLG) {
Marek Behúnb9739da2019-05-02 16:53:38 +0200277 /*
278 * On Armada 38x it seems that the controller works as
279 * if it first set the MVTWSI_CONTROL_IFLAG in the
280 * control register and only after that it changed the
281 * status register.
282 * This sometimes caused weird bugs which only appeared
283 * on selected I2C speeds and even then only sometimes.
284 * We therefore add here a simple ndealy(100), which
285 * seems to fix this weird bug.
286 */
287 ndelay(100);
Albert Aribaud04280c42010-08-27 18:26:05 +0200288 status = readl(&twsi->status);
289 if (status == expected_status)
290 return 0;
291 else
mario.six@gdsys.ccf43d3e92016-07-21 11:57:02 +0200292 return mvtwsi_error(
Albert Aribaud04280c42010-08-27 18:26:05 +0200293 MVTWSI_ERROR_WRONG_STATUS,
294 control, status, expected_status);
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200295 }
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200296 ndelay(tick); /* One clock cycle */
Albert Aribaud04280c42010-08-27 18:26:05 +0200297 } while (timeout--);
298 status = readl(&twsi->status);
mario.six@gdsys.ccf43d3e92016-07-21 11:57:02 +0200299 return mvtwsi_error(MVTWSI_ERROR_TIMEOUT, control, status,
300 expected_status);
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200301}
302
Albert Aribaud04280c42010-08-27 18:26:05 +0200303/*
mario.six@gdsys.ccefd1fb82016-07-21 11:57:13 +0200304 * twsi_start() - Assert a START condition on the bus.
305 *
306 * This function is used in both single I2C transactions and inside
307 * back-to-back transactions (repeated starts).
308 *
309 * @twsi: The MVTWSI register structure to use.
310 * @expected_status: The I2C bus status expected to be asserted after the
311 * operation completion.
312 * @tick: The duration of a clock cycle at the current I2C speed.
313 * @return Zero if status is as expected, or a non-zero code if either a time
314 * out occurred or the status was not the expected one.
Albert Aribaud04280c42010-08-27 18:26:05 +0200315 */
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200316static int twsi_start(struct mvtwsi_registers *twsi, int expected_status,
317 uint tick)
Albert Aribaud04280c42010-08-27 18:26:05 +0200318{
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200319 /* Assert START */
mario.six@gdsys.cc1cc2c282016-07-21 11:57:04 +0200320 writel(MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_START |
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200321 MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
322 /* Wait for controller to process START */
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200323 return twsi_wait(twsi, expected_status, tick);
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200324}
325
Albert Aribaud04280c42010-08-27 18:26:05 +0200326/*
mario.six@gdsys.ccefd1fb82016-07-21 11:57:13 +0200327 * twsi_send() - Send a byte on the I2C bus.
328 *
329 * The byte may be part of an address byte or data.
330 *
331 * @twsi: The MVTWSI register structure to use.
332 * @byte: The byte to send.
333 * @expected_status: The I2C bus status expected to be asserted after the
334 * operation completion.
335 * @tick: The duration of a clock cycle at the current I2C speed.
336 * @return Zero if status is as expected, or a non-zero code if either a time
337 * out occurred or the status was not the expected one.
Albert Aribaud04280c42010-08-27 18:26:05 +0200338 */
mario.six@gdsys.cc9f7f6892016-07-21 11:57:08 +0200339static int twsi_send(struct mvtwsi_registers *twsi, u8 byte,
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200340 int expected_status, uint tick)
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200341{
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200342 /* Write byte to data register for sending */
Albert Aribaud04280c42010-08-27 18:26:05 +0200343 writel(byte, &twsi->data);
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200344 /* Clear any pending interrupt -- that will cause sending */
mario.six@gdsys.cc1cc2c282016-07-21 11:57:04 +0200345 writel(MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_CLEAR_IFLG,
346 &twsi->control);
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200347 /* Wait for controller to receive byte, and check ACK */
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200348 return twsi_wait(twsi, expected_status, tick);
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200349}
350
Albert Aribaud04280c42010-08-27 18:26:05 +0200351/*
mario.six@gdsys.ccefd1fb82016-07-21 11:57:13 +0200352 * twsi_recv() - Receive a byte on the I2C bus.
353 *
354 * The static variable mvtwsi_control_flags controls whether we ack or nak.
355 *
356 * @twsi: The MVTWSI register structure to use.
357 * @byte: The byte to send.
358 * @ack_flag: Flag that determines whether the received byte should
359 * be acknowledged by the controller or not (sent ACK/NAK).
360 * @tick: The duration of a clock cycle at the current I2C speed.
361 * @return Zero if status is as expected, or a non-zero code if either a time
362 * out occurred or the status was not the expected one.
Albert Aribaud04280c42010-08-27 18:26:05 +0200363 */
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200364static int twsi_recv(struct mvtwsi_registers *twsi, u8 *byte, int ack_flag,
365 uint tick)
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200366{
mario.six@gdsys.cc1cc2c282016-07-21 11:57:04 +0200367 int expected_status, status, control;
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200368
mario.six@gdsys.cc1cc2c282016-07-21 11:57:04 +0200369 /* Compute expected status based on passed ACK flag */
370 expected_status = ack_flag ? MVTWSI_STATUS_DATA_R_ACK :
371 MVTWSI_STATUS_DATA_R_NAK;
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200372 /* Acknowledge *previous state*, and launch receive */
mario.six@gdsys.cc1cc2c282016-07-21 11:57:04 +0200373 control = MVTWSI_CONTROL_TWSIEN;
374 control |= ack_flag == MVTWSI_READ_ACK ? MVTWSI_CONTROL_ACK : 0;
375 writel(control | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200376 /* Wait for controller to receive byte, and assert ACK or NAK */
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200377 status = twsi_wait(twsi, expected_status, tick);
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200378 /* If we did receive the expected byte, store it */
Albert Aribaud04280c42010-08-27 18:26:05 +0200379 if (status == 0)
380 *byte = readl(&twsi->data);
Albert Aribaud04280c42010-08-27 18:26:05 +0200381 return status;
382}
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200383
Albert Aribaud04280c42010-08-27 18:26:05 +0200384/*
mario.six@gdsys.ccefd1fb82016-07-21 11:57:13 +0200385 * twsi_stop() - Assert a STOP condition on the bus.
386 *
387 * This function is also used to force the bus back to idle state (SDA =
388 * SCL = 1).
389 *
390 * @twsi: The MVTWSI register structure to use.
391 * @tick: The duration of a clock cycle at the current I2C speed.
392 * @return Zero if the operation succeeded, or a non-zero code if a time out
393 * occurred.
Albert Aribaud04280c42010-08-27 18:26:05 +0200394 */
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200395static int twsi_stop(struct mvtwsi_registers *twsi, uint tick)
Albert Aribaud04280c42010-08-27 18:26:05 +0200396{
397 int control, stop_status;
mario.six@gdsys.ccbdf0f662016-07-21 11:57:05 +0200398 int status = 0;
Albert Aribaud04280c42010-08-27 18:26:05 +0200399 int timeout = 1000;
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200400
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200401 /* Assert STOP */
Albert Aribaud04280c42010-08-27 18:26:05 +0200402 control = MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_STOP;
Hans de Goede6b703e02016-01-14 14:06:25 +0100403 writel(control | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200404 /* Wait for IDLE; IFLG won't rise, so we can't use twsi_wait() */
Albert Aribaud04280c42010-08-27 18:26:05 +0200405 do {
406 stop_status = readl(&twsi->status);
407 if (stop_status == MVTWSI_STATUS_IDLE)
408 break;
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200409 ndelay(tick); /* One clock cycle */
Albert Aribaud04280c42010-08-27 18:26:05 +0200410 } while (timeout--);
411 control = readl(&twsi->control);
412 if (stop_status != MVTWSI_STATUS_IDLE)
mario.six@gdsys.ccbdf0f662016-07-21 11:57:05 +0200413 status = mvtwsi_error(MVTWSI_ERROR_TIMEOUT,
414 control, status, MVTWSI_STATUS_IDLE);
Albert Aribaud04280c42010-08-27 18:26:05 +0200415 return status;
416}
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200417
mario.six@gdsys.ccefd1fb82016-07-21 11:57:13 +0200418/*
419 * twsi_calc_freq() - Compute I2C frequency depending on m and n parameters.
420 *
421 * @n: Parameter 'n' for the frequency calculation algorithm.
422 * @m: Parameter 'm' for the frequency calculation algorithm.
423 * @return The I2C frequency corresponding to the passed m and n parameters.
424 */
mario.six@gdsys.cc9e118b32016-07-21 11:57:06 +0200425static uint twsi_calc_freq(const int n, const int m)
Stefan Roesecca56a72015-03-18 09:30:54 +0100426{
Jagan Teki68078f72016-10-13 14:19:35 +0530427#ifdef CONFIG_ARCH_SUNXI
Stefan Roesecca56a72015-03-18 09:30:54 +0100428 return CONFIG_SYS_TCLK / (10 * (m + 1) * (1 << n));
429#else
430 return CONFIG_SYS_TCLK / (10 * (m + 1) * (2 << n));
431#endif
432}
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200433
Albert Aribaud04280c42010-08-27 18:26:05 +0200434/*
mario.six@gdsys.ccefd1fb82016-07-21 11:57:13 +0200435 * twsi_reset() - Reset the I2C controller.
436 *
437 * Resetting the controller also resets the baud rate and slave address, hence
438 * they must be re-established after the reset.
439 *
440 * @twsi: The MVTWSI register structure to use.
Albert Aribaud04280c42010-08-27 18:26:05 +0200441 */
mario.six@gdsys.cc9f7f6892016-07-21 11:57:08 +0200442static void twsi_reset(struct mvtwsi_registers *twsi)
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200443{
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200444 /* Reset controller */
Albert Aribaud04280c42010-08-27 18:26:05 +0200445 writel(0, &twsi->soft_reset);
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200446 /* Wait 2 ms -- this is what the Marvell LSP does */
Albert Aribaud04280c42010-08-27 18:26:05 +0200447 udelay(20000);
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200448}
449
Albert Aribaud04280c42010-08-27 18:26:05 +0200450/*
mario.six@gdsys.ccefd1fb82016-07-21 11:57:13 +0200451 * __twsi_i2c_set_bus_speed() - Set the speed of the I2C controller.
452 *
453 * This function sets baud rate to the highest possible value that does not
454 * exceed the requested rate.
455 *
456 * @twsi: The MVTWSI register structure to use.
457 * @requested_speed: The desired frequency the controller should run at
458 * in Hz.
459 * @return The actual frequency the controller was configured to.
Albert Aribaud04280c42010-08-27 18:26:05 +0200460 */
mario.six@gdsys.cc9f7f6892016-07-21 11:57:08 +0200461static uint __twsi_i2c_set_bus_speed(struct mvtwsi_registers *twsi,
mario.six@gdsys.cca4ac8b72016-07-21 11:57:07 +0200462 uint requested_speed)
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200463{
mario.six@gdsys.cc9e118b32016-07-21 11:57:06 +0200464 uint tmp_speed, highest_speed, n, m;
465 uint baud = 0x44; /* Baud rate after controller reset */
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200466
Albert Aribaud04280c42010-08-27 18:26:05 +0200467 highest_speed = 0;
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200468 /* Successively try m, n combinations, and use the combination
469 * resulting in the largest speed that's not above the requested
470 * speed */
Albert Aribaud04280c42010-08-27 18:26:05 +0200471 for (n = 0; n < 8; n++) {
472 for (m = 0; m < 16; m++) {
Stefan Roesecca56a72015-03-18 09:30:54 +0100473 tmp_speed = twsi_calc_freq(n, m);
mario.six@gdsys.cc2b656eb2016-07-21 11:57:01 +0200474 if ((tmp_speed <= requested_speed) &&
475 (tmp_speed > highest_speed)) {
Albert Aribaud04280c42010-08-27 18:26:05 +0200476 highest_speed = tmp_speed;
477 baud = (m << 3) | n;
478 }
479 }
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200480 }
Hans de Goede9830f1c2014-06-13 22:55:48 +0200481 writel(baud, &twsi->baudrate);
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200482
483 /* Wait for controller for one tick */
Igor Opaniukf7c91762021-02-09 13:52:45 +0200484#if CONFIG_IS_ENABLED(DM_I2C)
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200485 ndelay(calc_tick(highest_speed));
486#else
487 ndelay(10000);
488#endif
489 return highest_speed;
Hans de Goede9830f1c2014-06-13 22:55:48 +0200490}
491
mario.six@gdsys.ccefd1fb82016-07-21 11:57:13 +0200492/*
493 * __twsi_i2c_init() - Initialize the I2C controller.
494 *
495 * @twsi: The MVTWSI register structure to use.
496 * @speed: The initial frequency the controller should run at
497 * in Hz.
498 * @slaveadd: The I2C address to be set for the I2C master.
499 * @actual_speed: A output parameter that receives the actual frequency
500 * in Hz the controller was set to by the function.
501 * @return Zero if the operation succeeded, or a non-zero code if a time out
502 * occurred.
503 */
mario.six@gdsys.cc9f7f6892016-07-21 11:57:08 +0200504static void __twsi_i2c_init(struct mvtwsi_registers *twsi, int speed,
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200505 int slaveadd, uint *actual_speed)
Hans de Goede9830f1c2014-06-13 22:55:48 +0200506{
Stefan Mavrodiev6425ebd2018-02-13 09:27:40 +0200507 uint tmp_speed;
508
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200509 /* Reset controller */
mario.six@gdsys.cc9f7f6892016-07-21 11:57:08 +0200510 twsi_reset(twsi);
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200511 /* Set speed */
Stefan Mavrodiev6425ebd2018-02-13 09:27:40 +0200512 tmp_speed = __twsi_i2c_set_bus_speed(twsi, speed);
Heinrich Schuchardt5e0fd542018-01-31 00:57:17 +0100513 if (actual_speed)
Stefan Mavrodiev6425ebd2018-02-13 09:27:40 +0200514 *actual_speed = tmp_speed;
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200515 /* Set slave address; even though we don't use it */
Hans de Goede9830f1c2014-06-13 22:55:48 +0200516 writel(slaveadd, &twsi->slave_address);
517 writel(0, &twsi->xtnd_slave_addr);
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200518 /* Assert STOP, but don't care for the result */
Igor Opaniukf7c91762021-02-09 13:52:45 +0200519#if CONFIG_IS_ENABLED(DM_I2C)
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200520 (void) twsi_stop(twsi, calc_tick(*actual_speed));
521#else
522 (void) twsi_stop(twsi, 10000);
523#endif
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200524}
525
Albert Aribaud04280c42010-08-27 18:26:05 +0200526/*
mario.six@gdsys.ccefd1fb82016-07-21 11:57:13 +0200527 * i2c_begin() - Start a I2C transaction.
528 *
529 * Begin a I2C transaction with a given expected start status and chip address.
530 * A START is asserted, and the address byte is sent to the I2C controller. The
531 * expected address status will be derived from the direction bit (bit 0) of
532 * the address byte.
533 *
534 * @twsi: The MVTWSI register structure to use.
535 * @expected_start_status: The I2C status the controller is expected to
536 * assert after the address byte was sent.
537 * @addr: The address byte to be sent.
538 * @tick: The duration of a clock cycle at the current
539 * I2C speed.
540 * @return Zero if the operation succeeded, or a non-zero code if a time out or
541 * unexpected I2C status occurred.
Albert Aribaud04280c42010-08-27 18:26:05 +0200542 */
mario.six@gdsys.cc9f7f6892016-07-21 11:57:08 +0200543static int i2c_begin(struct mvtwsi_registers *twsi, int expected_start_status,
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200544 u8 addr, uint tick)
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200545{
Albert Aribaud04280c42010-08-27 18:26:05 +0200546 int status, expected_addr_status;
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200547
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200548 /* Compute the expected address status from the direction bit in
549 * the address byte */
550 if (addr & 1) /* Reading */
Albert Aribaud04280c42010-08-27 18:26:05 +0200551 expected_addr_status = MVTWSI_STATUS_ADDR_R_ACK;
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200552 else /* Writing */
Albert Aribaud04280c42010-08-27 18:26:05 +0200553 expected_addr_status = MVTWSI_STATUS_ADDR_W_ACK;
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200554 /* Assert START */
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200555 status = twsi_start(twsi, expected_start_status, tick);
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200556 /* Send out the address if the start went well */
Albert Aribaud04280c42010-08-27 18:26:05 +0200557 if (status == 0)
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200558 status = twsi_send(twsi, addr, expected_addr_status, tick);
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200559 /* Return 0, or the status of the first failure */
Albert Aribaud04280c42010-08-27 18:26:05 +0200560 return status;
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200561}
562
Albert Aribaud04280c42010-08-27 18:26:05 +0200563/*
mario.six@gdsys.ccefd1fb82016-07-21 11:57:13 +0200564 * __twsi_i2c_probe_chip() - Probe the given I2C chip address.
565 *
566 * This function begins a I2C read transaction, does a dummy read and NAKs; if
567 * the procedure succeeds, the chip is considered to be present.
568 *
569 * @twsi: The MVTWSI register structure to use.
570 * @chip: The chip address to probe.
571 * @tick: The duration of a clock cycle at the current I2C speed.
572 * @return Zero if the operation succeeded, or a non-zero code if a time out or
573 * unexpected I2C status occurred.
Albert Aribaud04280c42010-08-27 18:26:05 +0200574 */
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200575static int __twsi_i2c_probe_chip(struct mvtwsi_registers *twsi, uchar chip,
576 uint tick)
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200577{
Albert Aribaud04280c42010-08-27 18:26:05 +0200578 u8 dummy_byte;
579 int status;
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200580
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200581 /* Begin i2c read */
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200582 status = i2c_begin(twsi, MVTWSI_STATUS_START, (chip << 1) | 1, tick);
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200583 /* Dummy read was accepted: receive byte, but NAK it. */
Albert Aribaud04280c42010-08-27 18:26:05 +0200584 if (status == 0)
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200585 status = twsi_recv(twsi, &dummy_byte, MVTWSI_READ_NAK, tick);
Albert Aribaud04280c42010-08-27 18:26:05 +0200586 /* Stop transaction */
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200587 twsi_stop(twsi, tick);
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200588 /* Return 0, or the status of the first failure */
Albert Aribaud04280c42010-08-27 18:26:05 +0200589 return status;
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200590}
591
Albert Aribaud04280c42010-08-27 18:26:05 +0200592/*
mario.six@gdsys.ccefd1fb82016-07-21 11:57:13 +0200593 * __twsi_i2c_read() - Read data from a I2C chip.
594 *
595 * This function begins a I2C write transaction, and transmits the address
596 * bytes; then begins a I2C read transaction, and receives the data bytes.
Albert Aribaud04280c42010-08-27 18:26:05 +0200597 *
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200598 * NOTE: Some devices want a stop right before the second start, while some
599 * will choke if it is there. Since deciding this is not yet supported in
600 * higher level APIs, we need to make a decision here, and for the moment that
601 * will be a repeated start without a preceding stop.
mario.six@gdsys.ccefd1fb82016-07-21 11:57:13 +0200602 *
603 * @twsi: The MVTWSI register structure to use.
604 * @chip: The chip address to read from.
605 * @addr: The address bytes to send.
606 * @alen: The length of the address bytes in bytes.
607 * @data: The buffer to receive the data read from the chip (has to have
608 * a size of at least 'length' bytes).
609 * @length: The amount of data to be read from the chip in bytes.
610 * @tick: The duration of a clock cycle at the current I2C speed.
611 * @return Zero if the operation succeeded, or a non-zero code if a time out or
612 * unexpected I2C status occurred.
Albert Aribaud04280c42010-08-27 18:26:05 +0200613 */
mario.six@gdsys.cc9f7f6892016-07-21 11:57:08 +0200614static int __twsi_i2c_read(struct mvtwsi_registers *twsi, uchar chip,
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200615 u8 *addr, int alen, uchar *data, int length,
616 uint tick)
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200617{
mario.six@gdsys.ccbdf0f662016-07-21 11:57:05 +0200618 int status = 0;
619 int stop_status;
mario.six@gdsys.cc029a84b2016-07-21 11:57:11 +0200620 int expected_start = MVTWSI_STATUS_START;
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200621
mario.six@gdsys.cc029a84b2016-07-21 11:57:11 +0200622 if (alen > 0) {
623 /* Begin i2c write to send the address bytes */
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200624 status = i2c_begin(twsi, expected_start, (chip << 1), tick);
mario.six@gdsys.cc029a84b2016-07-21 11:57:11 +0200625 /* Send address bytes */
626 while ((status == 0) && alen--)
Stefan Roeseabd7d312016-08-25 15:20:01 +0200627 status = twsi_send(twsi, addr[alen],
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200628 MVTWSI_STATUS_DATA_W_ACK, tick);
mario.six@gdsys.cc029a84b2016-07-21 11:57:11 +0200629 /* Send repeated STARTs after the initial START */
630 expected_start = MVTWSI_STATUS_REPEATED_START;
631 }
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200632 /* Begin i2c read to receive data bytes */
Albert Aribaud04280c42010-08-27 18:26:05 +0200633 if (status == 0)
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200634 status = i2c_begin(twsi, expected_start, (chip << 1) | 1, tick);
mario.six@gdsys.cc1cc2c282016-07-21 11:57:04 +0200635 /* Receive actual data bytes; set NAK if we if we have nothing more to
636 * read */
637 while ((status == 0) && length--)
mario.six@gdsys.cc9f7f6892016-07-21 11:57:08 +0200638 status = twsi_recv(twsi, data++,
mario.six@gdsys.cc1cc2c282016-07-21 11:57:04 +0200639 length > 0 ?
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200640 MVTWSI_READ_ACK : MVTWSI_READ_NAK, tick);
Albert Aribaud04280c42010-08-27 18:26:05 +0200641 /* Stop transaction */
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200642 stop_status = twsi_stop(twsi, tick);
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200643 /* Return 0, or the status of the first failure */
mario.six@gdsys.ccbdf0f662016-07-21 11:57:05 +0200644 return status != 0 ? status : stop_status;
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200645}
646
Albert Aribaud04280c42010-08-27 18:26:05 +0200647/*
mario.six@gdsys.ccefd1fb82016-07-21 11:57:13 +0200648 * __twsi_i2c_write() - Send data to a I2C chip.
649 *
650 * This function begins a I2C write transaction, and transmits the address
651 * bytes; then begins a new I2C write transaction, and sends the data bytes.
652 *
653 * @twsi: The MVTWSI register structure to use.
654 * @chip: The chip address to read from.
655 * @addr: The address bytes to send.
656 * @alen: The length of the address bytes in bytes.
657 * @data: The buffer containing the data to be sent to the chip.
658 * @length: The length of data to be sent to the chip in bytes.
659 * @tick: The duration of a clock cycle at the current I2C speed.
660 * @return Zero if the operation succeeded, or a non-zero code if a time out or
661 * unexpected I2C status occurred.
Albert Aribaud04280c42010-08-27 18:26:05 +0200662 */
mario.six@gdsys.cc9f7f6892016-07-21 11:57:08 +0200663static int __twsi_i2c_write(struct mvtwsi_registers *twsi, uchar chip,
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200664 u8 *addr, int alen, uchar *data, int length,
665 uint tick)
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200666{
mario.six@gdsys.ccbdf0f662016-07-21 11:57:05 +0200667 int status, stop_status;
Albert Aribaud04280c42010-08-27 18:26:05 +0200668
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200669 /* Begin i2c write to send first the address bytes, then the
670 * data bytes */
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200671 status = i2c_begin(twsi, MVTWSI_STATUS_START, (chip << 1), tick);
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200672 /* Send address bytes */
mario.six@gdsys.ccc4eceb52016-07-21 11:57:09 +0200673 while ((status == 0) && (alen-- > 0))
Stefan Roeseabd7d312016-08-25 15:20:01 +0200674 status = twsi_send(twsi, addr[alen], MVTWSI_STATUS_DATA_W_ACK,
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200675 tick);
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200676 /* Send data bytes */
Albert Aribaud04280c42010-08-27 18:26:05 +0200677 while ((status == 0) && (length-- > 0))
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200678 status = twsi_send(twsi, *(data++), MVTWSI_STATUS_DATA_W_ACK,
679 tick);
Albert Aribaud04280c42010-08-27 18:26:05 +0200680 /* Stop transaction */
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200681 stop_status = twsi_stop(twsi, tick);
mario.six@gdsys.cc7b0c4312016-07-21 11:57:03 +0200682 /* Return 0, or the status of the first failure */
mario.six@gdsys.ccbdf0f662016-07-21 11:57:05 +0200683 return status != 0 ? status : stop_status;
Heiko Schocher5cd2a242009-07-20 09:59:37 +0200684}
685
Igor Opaniukf7c91762021-02-09 13:52:45 +0200686#if !CONFIG_IS_ENABLED(DM_I2C)
mario.six@gdsys.cca4ac8b72016-07-21 11:57:07 +0200687static void twsi_i2c_init(struct i2c_adapter *adap, int speed,
688 int slaveadd)
689{
mario.six@gdsys.cc9f7f6892016-07-21 11:57:08 +0200690 struct mvtwsi_registers *twsi = twsi_get_base(adap);
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200691 __twsi_i2c_init(twsi, speed, slaveadd, NULL);
mario.six@gdsys.cca4ac8b72016-07-21 11:57:07 +0200692}
693
694static uint twsi_i2c_set_bus_speed(struct i2c_adapter *adap,
695 uint requested_speed)
696{
mario.six@gdsys.cc9f7f6892016-07-21 11:57:08 +0200697 struct mvtwsi_registers *twsi = twsi_get_base(adap);
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200698 __twsi_i2c_set_bus_speed(twsi, requested_speed);
699 return 0;
mario.six@gdsys.cca4ac8b72016-07-21 11:57:07 +0200700}
701
702static int twsi_i2c_probe(struct i2c_adapter *adap, uchar chip)
703{
mario.six@gdsys.cc9f7f6892016-07-21 11:57:08 +0200704 struct mvtwsi_registers *twsi = twsi_get_base(adap);
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200705 return __twsi_i2c_probe_chip(twsi, chip, 10000);
mario.six@gdsys.cca4ac8b72016-07-21 11:57:07 +0200706}
707
708static int twsi_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
709 int alen, uchar *data, int length)
710{
mario.six@gdsys.cc9f7f6892016-07-21 11:57:08 +0200711 struct mvtwsi_registers *twsi = twsi_get_base(adap);
mario.six@gdsys.ccc4eceb52016-07-21 11:57:09 +0200712 u8 addr_bytes[4];
713
714 addr_bytes[0] = (addr >> 0) & 0xFF;
715 addr_bytes[1] = (addr >> 8) & 0xFF;
716 addr_bytes[2] = (addr >> 16) & 0xFF;
717 addr_bytes[3] = (addr >> 24) & 0xFF;
718
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200719 return __twsi_i2c_read(twsi, chip, addr_bytes, alen, data, length,
720 10000);
mario.six@gdsys.cca4ac8b72016-07-21 11:57:07 +0200721}
722
723static int twsi_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
724 int alen, uchar *data, int length)
725{
mario.six@gdsys.cc9f7f6892016-07-21 11:57:08 +0200726 struct mvtwsi_registers *twsi = twsi_get_base(adap);
mario.six@gdsys.ccc4eceb52016-07-21 11:57:09 +0200727 u8 addr_bytes[4];
728
729 addr_bytes[0] = (addr >> 0) & 0xFF;
730 addr_bytes[1] = (addr >> 8) & 0xFF;
731 addr_bytes[2] = (addr >> 16) & 0xFF;
732 addr_bytes[3] = (addr >> 24) & 0xFF;
733
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200734 return __twsi_i2c_write(twsi, chip, addr_bytes, alen, data, length,
735 10000);
mario.six@gdsys.cca4ac8b72016-07-21 11:57:07 +0200736}
737
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200738#ifdef CONFIG_I2C_MVTWSI_BASE0
Hans de Goede9830f1c2014-06-13 22:55:48 +0200739U_BOOT_I2C_ADAP_COMPLETE(twsi0, twsi_i2c_init, twsi_i2c_probe,
740 twsi_i2c_read, twsi_i2c_write,
741 twsi_i2c_set_bus_speed,
742 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0)
Paul Kocialkowski2fae3e72015-04-10 23:09:51 +0200743#endif
744#ifdef CONFIG_I2C_MVTWSI_BASE1
745U_BOOT_I2C_ADAP_COMPLETE(twsi1, 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, 1)
749
750#endif
751#ifdef CONFIG_I2C_MVTWSI_BASE2
752U_BOOT_I2C_ADAP_COMPLETE(twsi2, 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, 2)
756
757#endif
758#ifdef CONFIG_I2C_MVTWSI_BASE3
759U_BOOT_I2C_ADAP_COMPLETE(twsi3, 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, 3)
763
764#endif
765#ifdef CONFIG_I2C_MVTWSI_BASE4
766U_BOOT_I2C_ADAP_COMPLETE(twsi4, twsi_i2c_init, twsi_i2c_probe,
767 twsi_i2c_read, twsi_i2c_write,
768 twsi_i2c_set_bus_speed,
769 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 4)
770
771#endif
Jelle van der Waa8d3d7c12016-01-14 14:06:26 +0100772#ifdef CONFIG_I2C_MVTWSI_BASE5
773U_BOOT_I2C_ADAP_COMPLETE(twsi5, twsi_i2c_init, twsi_i2c_probe,
774 twsi_i2c_read, twsi_i2c_write,
775 twsi_i2c_set_bus_speed,
776 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 5)
777
778#endif
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +0200779#else /* CONFIG_DM_I2C */
780
781static int mvtwsi_i2c_probe_chip(struct udevice *bus, u32 chip_addr,
782 u32 chip_flags)
783{
784 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200785 return __twsi_i2c_probe_chip(dev->base, chip_addr, dev->tick);
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +0200786}
787
788static int mvtwsi_i2c_set_bus_speed(struct udevice *bus, uint speed)
789{
790 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200791
792 dev->speed = __twsi_i2c_set_bus_speed(dev->base, speed);
793 dev->tick = calc_tick(dev->speed);
794
795 return 0;
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +0200796}
797
Simon Glassaad29ae2020-12-03 16:55:21 -0700798static int mvtwsi_i2c_of_to_plat(struct udevice *bus)
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +0200799{
800 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
801
Masahiro Yamada32822d02020-08-04 14:14:43 +0900802 dev->base = dev_read_addr_ptr(bus);
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +0200803
804 if (!dev->base)
805 return -ENOMEM;
806
Simon Glassdd79d6e2017-01-17 16:52:55 -0700807 dev->index = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus),
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +0200808 "cell-index", -1);
Simon Glassdd79d6e2017-01-17 16:52:55 -0700809 dev->slaveadd = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus),
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +0200810 "u-boot,i2c-slave-addr", 0x0);
Simon Glassf0c99c52020-01-23 11:48:22 -0700811 dev->speed = dev_read_u32_default(bus, "clock-frequency",
812 I2C_SPEED_STANDARD_RATE);
813
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +0200814 return 0;
815}
816
Baruch Siach91006c72018-06-07 12:38:10 +0300817static void twsi_disable_i2c_slave(struct mvtwsi_registers *twsi)
818{
819 clrbits_le32(&twsi->debug, BIT(18));
820}
821
822static int mvtwsi_i2c_bind(struct udevice *bus)
823{
Masahiro Yamada32822d02020-08-04 14:14:43 +0900824 struct mvtwsi_registers *twsi = dev_read_addr_ptr(bus);
Baruch Siach91006c72018-06-07 12:38:10 +0300825
826 /* Disable the hidden slave in i2c0 of these platforms */
Simon Glass4123ba02020-12-16 21:20:15 -0700827 if ((IS_ENABLED(CONFIG_ARMADA_38X) ||
828 IS_ENABLED(CONFIG_ARCH_KIRKWOOD) ||
829 IS_ENABLED(CONFIG_ARMADA_8K)) && !dev_seq(bus))
Baruch Siach91006c72018-06-07 12:38:10 +0300830 twsi_disable_i2c_slave(twsi);
831
832 return 0;
833}
834
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +0200835static int mvtwsi_i2c_probe(struct udevice *bus)
836{
837 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200838 uint actual_speed;
839
840 __twsi_i2c_init(dev->base, dev->speed, dev->slaveadd, &actual_speed);
841 dev->speed = actual_speed;
842 dev->tick = calc_tick(dev->speed);
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +0200843 return 0;
844}
845
846static int mvtwsi_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
847{
848 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
849 struct i2c_msg *dmsg, *omsg, dummy;
850
851 memset(&dummy, 0, sizeof(struct i2c_msg));
852
853 /* We expect either two messages (one with an offset and one with the
854 * actual data) or one message (just data or offset/data combined) */
855 if (nmsgs > 2 || nmsgs == 0) {
856 debug("%s: Only one or two messages are supported.", __func__);
857 return -1;
858 }
859
860 omsg = nmsgs == 1 ? &dummy : msg;
861 dmsg = nmsgs == 1 ? msg : msg + 1;
862
863 if (dmsg->flags & I2C_M_RD)
864 return __twsi_i2c_read(dev->base, dmsg->addr, omsg->buf,
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200865 omsg->len, dmsg->buf, dmsg->len,
866 dev->tick);
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +0200867 else
868 return __twsi_i2c_write(dev->base, dmsg->addr, omsg->buf,
mario.six@gdsys.cce841b582016-07-21 11:57:12 +0200869 omsg->len, dmsg->buf, dmsg->len,
870 dev->tick);
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +0200871}
872
873static const struct dm_i2c_ops mvtwsi_i2c_ops = {
874 .xfer = mvtwsi_i2c_xfer,
875 .probe_chip = mvtwsi_i2c_probe_chip,
876 .set_bus_speed = mvtwsi_i2c_set_bus_speed,
877};
878
879static const struct udevice_id mvtwsi_i2c_ids[] = {
880 { .compatible = "marvell,mv64xxx-i2c", },
Stefan Roese58e58d82016-09-16 15:07:55 +0200881 { .compatible = "marvell,mv78230-i2c", },
Jernej Skrabec9220d502017-04-27 00:03:36 +0200882 { .compatible = "allwinner,sun6i-a31-i2c", },
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +0200883 { /* sentinel */ }
884};
885
886U_BOOT_DRIVER(i2c_mvtwsi) = {
887 .name = "i2c_mvtwsi",
888 .id = UCLASS_I2C,
889 .of_match = mvtwsi_i2c_ids,
Baruch Siach91006c72018-06-07 12:38:10 +0300890 .bind = mvtwsi_i2c_bind,
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +0200891 .probe = mvtwsi_i2c_probe,
Simon Glassaad29ae2020-12-03 16:55:21 -0700892 .of_to_plat = mvtwsi_i2c_of_to_plat,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700893 .priv_auto = sizeof(struct mvtwsi_i2c_dev),
mario.six@gdsys.cc355a1272016-07-21 11:57:10 +0200894 .ops = &mvtwsi_i2c_ops,
895};
896#endif /* CONFIG_DM_I2C */