blob: 14cdb0f663512e34aebe921160626384eeebca54 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Heiko Schocher4ce5a722009-07-20 09:59:37 +02002/*
Albert Aribaud306563a2010-08-27 18:26:05 +02003 * Driver for the TWSI (i2c) controller found on the Marvell
4 * orion5x and kirkwood SoC families.
Heiko Schocher4ce5a722009-07-20 09:59:37 +02005 *
Albert ARIBAUD57b4bce2011-04-22 19:41:02 +02006 * Author: Albert Aribaud <albert.u.boot@aribaud.net>
Albert Aribaud306563a2010-08-27 18:26:05 +02007 * Copyright (c) 2010 Albert Aribaud.
Heiko Schocher4ce5a722009-07-20 09:59:37 +02008 */
Albert Aribaud306563a2010-08-27 18:26:05 +02009
Heiko Schocher4ce5a722009-07-20 09:59:37 +020010#include <common.h>
11#include <i2c.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Simon Glass401d1c42020-10-30 21:38:53 -060013#include <asm/global_data.h>
Simon Glassc05ed002020-05-10 11:40:11 -060014#include <linux/delay.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090015#include <linux/errno.h>
Heiko Schocher4ce5a722009-07-20 09:59:37 +020016#include <asm/io.h>
Baruch Siach173ec352018-06-07 12:38:10 +030017#include <linux/bitops.h>
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +020018#include <linux/compat.h>
Igor Opaniuk2147a162021-02-09 13:52:45 +020019#if CONFIG_IS_ENABLED(DM_I2C)
Samuel Holland06cec892021-09-12 10:21:39 -050020#include <clk.h>
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +020021#include <dm.h>
Samuel Holland06cec892021-09-12 10:21:39 -050022#include <reset.h>
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +020023#endif
24
25DECLARE_GLOBAL_DATA_PTR;
Heiko Schocher4ce5a722009-07-20 09:59:37 +020026
Heiko Schocher4ce5a722009-07-20 09:59:37 +020027/*
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +020028 * Include a file that will provide CONFIG_I2C_MVTWSI_BASE*, and possibly other
29 * settings
Heiko Schocher4ce5a722009-07-20 09:59:37 +020030 */
31
Igor Opaniuk2147a162021-02-09 13:52:45 +020032#if !CONFIG_IS_ENABLED(DM_I2C)
Trevor Woernerb16a3312020-05-06 08:02:38 -040033#if defined(CONFIG_ARCH_ORION5X)
Albert Aribaud306563a2010-08-27 18:26:05 +020034#include <asm/arch/orion5x.h>
Trevor Woernerbb0fb4c2020-05-06 08:02:40 -040035#elif (defined(CONFIG_ARCH_KIRKWOOD) || defined(CONFIG_ARCH_MVEBU))
Stefan Roese3dc23f72014-10-22 12:13:06 +020036#include <asm/arch/soc.h>
Jagan Tekiaec9a0f2016-10-13 14:19:35 +053037#elif defined(CONFIG_ARCH_SUNXI)
Hans de Goede66203772014-06-13 22:55:49 +020038#include <asm/arch/i2c.h>
Albert Aribaud306563a2010-08-27 18:26:05 +020039#else
40#error Driver mvtwsi not supported by SoC or board
41#endif
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +020042#endif /* CONFIG_DM_I2C */
Albert Aribaud306563a2010-08-27 18:26:05 +020043
44/*
Tom Rini65cc0e22022-11-16 13:10:41 -050045 * On SUNXI, we get CFG_SYS_TCLK from this include, so we want to
Jernej Skrabeca8f01cc2017-04-27 00:03:36 +020046 * always have it.
47 */
Igor Opaniuk2147a162021-02-09 13:52:45 +020048#if CONFIG_IS_ENABLED(DM_I2C) && defined(CONFIG_ARCH_SUNXI)
Jernej Skrabeca8f01cc2017-04-27 00:03:36 +020049#include <asm/arch/i2c.h>
50#endif
51
52/*
Albert Aribaud306563a2010-08-27 18:26:05 +020053 * TWSI register structure
54 */
55
Jagan Tekiaec9a0f2016-10-13 14:19:35 +053056#ifdef CONFIG_ARCH_SUNXI
Hans de Goede66203772014-06-13 22:55:49 +020057
58struct mvtwsi_registers {
59 u32 slave_address;
60 u32 xtnd_slave_addr;
61 u32 data;
62 u32 control;
63 u32 status;
64 u32 baudrate;
65 u32 soft_reset;
Baruch Siach173ec352018-06-07 12:38:10 +030066 u32 debug; /* Dummy field for build compatibility with mvebu */
Hans de Goede66203772014-06-13 22:55:49 +020067};
68
69#else
70
Albert Aribaud306563a2010-08-27 18:26:05 +020071struct mvtwsi_registers {
72 u32 slave_address;
73 u32 data;
74 u32 control;
75 union {
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +020076 u32 status; /* When reading */
77 u32 baudrate; /* When writing */
Albert Aribaud306563a2010-08-27 18:26:05 +020078 };
79 u32 xtnd_slave_addr;
Baruch Siach173ec352018-06-07 12:38:10 +030080 u32 reserved0[2];
Albert Aribaud306563a2010-08-27 18:26:05 +020081 u32 soft_reset;
Baruch Siach173ec352018-06-07 12:38:10 +030082 u32 reserved1[27];
83 u32 debug;
Albert Aribaud306563a2010-08-27 18:26:05 +020084};
85
Hans de Goede66203772014-06-13 22:55:49 +020086#endif
87
Igor Opaniuk2147a162021-02-09 13:52:45 +020088#if CONFIG_IS_ENABLED(DM_I2C)
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +020089struct mvtwsi_i2c_dev {
90 /* TWSI Register base for the device */
91 struct mvtwsi_registers *base;
92 /* Number of the device (determined from cell-index property) */
93 int index;
94 /* The I2C slave address for the device */
95 u8 slaveadd;
96 /* The configured I2C speed in Hz */
97 uint speed;
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +020098 /* The current length of a clock period (depending on speed) */
99 uint tick;
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +0200100};
101#endif /* CONFIG_DM_I2C */
102
Albert Aribaud306563a2010-08-27 18:26:05 +0200103/*
mario.six@gdsys.ccdfc39582016-07-21 11:57:02 +0200104 * enum mvtwsi_ctrl_register_fields - Bit masks for flags in the control
105 * register
Albert Aribaud306563a2010-08-27 18:26:05 +0200106 */
mario.six@gdsys.ccdfc39582016-07-21 11:57:02 +0200107enum mvtwsi_ctrl_register_fields {
108 /* Acknowledge bit */
109 MVTWSI_CONTROL_ACK = 0x00000004,
110 /* Interrupt flag */
111 MVTWSI_CONTROL_IFLG = 0x00000008,
112 /* Stop bit */
113 MVTWSI_CONTROL_STOP = 0x00000010,
114 /* Start bit */
115 MVTWSI_CONTROL_START = 0x00000020,
116 /* I2C enable */
117 MVTWSI_CONTROL_TWSIEN = 0x00000040,
118 /* Interrupt enable */
119 MVTWSI_CONTROL_INTEN = 0x00000080,
120};
Albert Aribaud306563a2010-08-27 18:26:05 +0200121
122/*
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200123 * On sun6i and newer, IFLG is a write-clear bit, which is cleared by writing 1;
124 * on other platforms, it is a normal r/w bit, which is cleared by writing 0.
Hans de Goede904dfbf2016-01-14 14:06:25 +0100125 */
126
Jernej Skrabecb2968002021-01-11 21:11:36 +0100127#if defined(CONFIG_SUNXI_GEN_SUN6I) || defined(CONFIG_SUN50I_GEN_H6)
Hans de Goede904dfbf2016-01-14 14:06:25 +0100128#define MVTWSI_CONTROL_CLEAR_IFLG 0x00000008
129#else
130#define MVTWSI_CONTROL_CLEAR_IFLG 0x00000000
131#endif
132
133/*
mario.six@gdsys.ccdfc39582016-07-21 11:57:02 +0200134 * enum mvstwsi_status_values - Possible values of I2C controller's status
135 * register
136 *
137 * Only those statuses expected in normal master operation on
138 * non-10-bit-address devices are specified.
139 *
140 * Every status that's unexpected during normal operation (bus errors,
141 * arbitration losses, missing ACKs...) is passed back to the caller as an error
Albert Aribaud306563a2010-08-27 18:26:05 +0200142 * code.
143 */
mario.six@gdsys.ccdfc39582016-07-21 11:57:02 +0200144enum mvstwsi_status_values {
Sam Edwards250454c2023-07-25 16:13:05 -0600145 /* Protocol violation on bus; this is a terminal state */
146 MVTWSI_BUS_ERROR = 0x00,
mario.six@gdsys.ccdfc39582016-07-21 11:57:02 +0200147 /* START condition transmitted */
148 MVTWSI_STATUS_START = 0x08,
149 /* Repeated START condition transmitted */
150 MVTWSI_STATUS_REPEATED_START = 0x10,
151 /* Address + write bit transmitted, ACK received */
152 MVTWSI_STATUS_ADDR_W_ACK = 0x18,
153 /* Data transmitted, ACK received */
154 MVTWSI_STATUS_DATA_W_ACK = 0x28,
155 /* Address + read bit transmitted, ACK received */
156 MVTWSI_STATUS_ADDR_R_ACK = 0x40,
157 /* Address + read bit transmitted, ACK not received */
158 MVTWSI_STATUS_ADDR_R_NAK = 0x48,
159 /* Data received, ACK transmitted */
160 MVTWSI_STATUS_DATA_R_ACK = 0x50,
161 /* Data received, ACK not transmitted */
162 MVTWSI_STATUS_DATA_R_NAK = 0x58,
163 /* No relevant status */
164 MVTWSI_STATUS_IDLE = 0xF8,
165};
Albert Aribaud306563a2010-08-27 18:26:05 +0200166
167/*
mario.six@gdsys.cc670514f2016-07-21 11:57:04 +0200168 * enum mvstwsi_ack_flags - Determine whether a read byte should be
169 * acknowledged or not.
170 */
171enum mvtwsi_ack_flags {
172 /* Send NAK after received byte */
173 MVTWSI_READ_NAK = 0,
174 /* Send ACK after received byte */
175 MVTWSI_READ_ACK = 1,
176};
177
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200178/*
179 * calc_tick() - Calculate the duration of a clock cycle from the I2C speed
180 *
181 * @speed: The speed in Hz to calculate the clock cycle duration for.
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100182 * Return: The duration of a clock cycle in ns.
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200183 */
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200184inline uint calc_tick(uint speed)
185{
186 /* One tick = the duration of a period at the specified speed in ns (we
187 * add 100 ns to be on the safe side) */
188 return (1000000000u / speed) + 100;
189}
190
Igor Opaniuk2147a162021-02-09 13:52:45 +0200191#if !CONFIG_IS_ENABLED(DM_I2C)
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200192
mario.six@gdsys.cc670514f2016-07-21 11:57:04 +0200193/*
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200194 * twsi_get_base() - Get controller register base for specified adapter
195 *
196 * @adap: Adapter to get the register base for.
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100197 * Return: Register base for the specified adapter.
Albert Aribaud306563a2010-08-27 18:26:05 +0200198 */
Paul Kocialkowskidd822422015-04-10 23:09:51 +0200199static struct mvtwsi_registers *twsi_get_base(struct i2c_adapter *adap)
200{
201 switch (adap->hwadapnr) {
Tom Rini45ede972022-12-04 10:04:09 -0500202#ifdef CFG_I2C_MVTWSI_BASE0
Paul Kocialkowskidd822422015-04-10 23:09:51 +0200203 case 0:
Tom Rini45ede972022-12-04 10:04:09 -0500204 return (struct mvtwsi_registers *)CFG_I2C_MVTWSI_BASE0;
Paul Kocialkowskidd822422015-04-10 23:09:51 +0200205#endif
Tom Rini35661f82022-12-04 10:04:10 -0500206#ifdef CFG_I2C_MVTWSI_BASE1
Paul Kocialkowskidd822422015-04-10 23:09:51 +0200207 case 1:
Tom Rini35661f82022-12-04 10:04:10 -0500208 return (struct mvtwsi_registers *)CFG_I2C_MVTWSI_BASE1;
Paul Kocialkowskidd822422015-04-10 23:09:51 +0200209#endif
Tom Rini6e7df1d2023-01-10 11:19:45 -0500210#ifdef CFG_I2C_MVTWSI_BASE2
Paul Kocialkowskidd822422015-04-10 23:09:51 +0200211 case 2:
Tom Rini6e7df1d2023-01-10 11:19:45 -0500212 return (struct mvtwsi_registers *)CFG_I2C_MVTWSI_BASE2;
Paul Kocialkowskidd822422015-04-10 23:09:51 +0200213#endif
214#ifdef CONFIG_I2C_MVTWSI_BASE3
215 case 3:
mario.six@gdsys.cc9ec43b02016-07-21 11:57:01 +0200216 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE3;
Paul Kocialkowskidd822422015-04-10 23:09:51 +0200217#endif
218#ifdef CONFIG_I2C_MVTWSI_BASE4
219 case 4:
mario.six@gdsys.cc9ec43b02016-07-21 11:57:01 +0200220 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE4;
Paul Kocialkowskidd822422015-04-10 23:09:51 +0200221#endif
Jelle van der Waa9d082682016-01-14 14:06:26 +0100222#ifdef CONFIG_I2C_MVTWSI_BASE5
223 case 5:
mario.six@gdsys.cc9ec43b02016-07-21 11:57:01 +0200224 return (struct mvtwsi_registers *)CONFIG_I2C_MVTWSI_BASE5;
Jelle van der Waa9d082682016-01-14 14:06:26 +0100225#endif
Paul Kocialkowskidd822422015-04-10 23:09:51 +0200226 default:
227 printf("Missing mvtwsi controller %d base\n", adap->hwadapnr);
228 break;
229 }
230
231 return NULL;
232}
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +0200233#endif
Albert Aribaud306563a2010-08-27 18:26:05 +0200234
235/*
mario.six@gdsys.ccdfc39582016-07-21 11:57:02 +0200236 * enum mvtwsi_error_class - types of I2C errors
Albert Aribaud306563a2010-08-27 18:26:05 +0200237 */
mario.six@gdsys.ccdfc39582016-07-21 11:57:02 +0200238enum mvtwsi_error_class {
239 /* The controller returned a different status than expected */
240 MVTWSI_ERROR_WRONG_STATUS = 0x01,
241 /* The controller timed out */
242 MVTWSI_ERROR_TIMEOUT = 0x02,
243};
Albert Aribaud306563a2010-08-27 18:26:05 +0200244
mario.six@gdsys.ccdfc39582016-07-21 11:57:02 +0200245/*
246 * mvtwsi_error() - Build I2C return code from error information
247 *
248 * For debugging purposes, this function packs some information of an occurred
249 * error into a return code. These error codes are returned from I2C API
250 * functions (i2c_{read,write}, dm_i2c_{read,write}, etc.).
251 *
252 * @ec: The error class of the error (enum mvtwsi_error_class).
253 * @lc: The last value of the control register.
254 * @ls: The last value of the status register.
255 * @es: The expected value of the status register.
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100256 * Return: The generated error code.
mario.six@gdsys.ccdfc39582016-07-21 11:57:02 +0200257 */
258inline uint mvtwsi_error(uint ec, uint lc, uint ls, uint es)
259{
260 return ((ec << 24) & 0xFF000000)
261 | ((lc << 16) & 0x00FF0000)
262 | ((ls << 8) & 0x0000FF00)
263 | (es & 0xFF);
264}
Albert Aribaud306563a2010-08-27 18:26:05 +0200265
266/*
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200267 * twsi_wait() - Wait for I2C bus interrupt flag and check status, or time out.
268 *
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100269 * Return: Zero if status is as expected, or a non-zero code if either a time
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200270 * out occurred, or the status was not the expected one.
Albert Aribaud306563a2010-08-27 18:26:05 +0200271 */
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200272static int twsi_wait(struct mvtwsi_registers *twsi, int expected_status,
273 uint tick)
Heiko Schocher4ce5a722009-07-20 09:59:37 +0200274{
Albert Aribaud306563a2010-08-27 18:26:05 +0200275 int control, status;
276 int timeout = 1000;
277
278 do {
279 control = readl(&twsi->control);
280 if (control & MVTWSI_CONTROL_IFLG) {
Marek Behúnd50e2962019-05-02 16:53:38 +0200281 /*
282 * On Armada 38x it seems that the controller works as
283 * if it first set the MVTWSI_CONTROL_IFLAG in the
284 * control register and only after that it changed the
285 * status register.
286 * This sometimes caused weird bugs which only appeared
287 * on selected I2C speeds and even then only sometimes.
288 * We therefore add here a simple ndealy(100), which
289 * seems to fix this weird bug.
290 */
291 ndelay(100);
Albert Aribaud306563a2010-08-27 18:26:05 +0200292 status = readl(&twsi->status);
293 if (status == expected_status)
294 return 0;
295 else
mario.six@gdsys.ccdfc39582016-07-21 11:57:02 +0200296 return mvtwsi_error(
Albert Aribaud306563a2010-08-27 18:26:05 +0200297 MVTWSI_ERROR_WRONG_STATUS,
298 control, status, expected_status);
299 }
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200300 ndelay(tick); /* One clock cycle */
Albert Aribaud306563a2010-08-27 18:26:05 +0200301 } while (timeout--);
302 status = readl(&twsi->status);
mario.six@gdsys.ccdfc39582016-07-21 11:57:02 +0200303 return mvtwsi_error(MVTWSI_ERROR_TIMEOUT, control, status,
304 expected_status);
Heiko Schocher4ce5a722009-07-20 09:59:37 +0200305}
306
Albert Aribaud306563a2010-08-27 18:26:05 +0200307/*
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200308 * twsi_start() - Assert a START condition on the bus.
309 *
310 * This function is used in both single I2C transactions and inside
311 * back-to-back transactions (repeated starts).
312 *
313 * @twsi: The MVTWSI register structure to use.
314 * @expected_status: The I2C bus status expected to be asserted after the
315 * operation completion.
316 * @tick: The duration of a clock cycle at the current I2C speed.
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100317 * Return: Zero if status is as expected, or a non-zero code if either a time
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200318 * out occurred or the status was not the expected one.
Albert Aribaud306563a2010-08-27 18:26:05 +0200319 */
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200320static int twsi_start(struct mvtwsi_registers *twsi, int expected_status,
321 uint tick)
Albert Aribaud306563a2010-08-27 18:26:05 +0200322{
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200323 /* Assert START */
mario.six@gdsys.cc670514f2016-07-21 11:57:04 +0200324 writel(MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_START |
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200325 MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
326 /* Wait for controller to process START */
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200327 return twsi_wait(twsi, expected_status, tick);
Albert Aribaud306563a2010-08-27 18:26:05 +0200328}
329
330/*
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200331 * twsi_send() - Send a byte on the I2C bus.
332 *
333 * The byte may be part of an address byte or data.
334 *
335 * @twsi: The MVTWSI register structure to use.
336 * @byte: The byte to send.
337 * @expected_status: The I2C bus status expected to be asserted after the
338 * operation completion.
339 * @tick: The duration of a clock cycle at the current I2C speed.
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100340 * Return: Zero if status is as expected, or a non-zero code if either a time
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200341 * out occurred or the status was not the expected one.
Albert Aribaud306563a2010-08-27 18:26:05 +0200342 */
mario.six@gdsys.cc3c4db632016-07-21 11:57:08 +0200343static int twsi_send(struct mvtwsi_registers *twsi, u8 byte,
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200344 int expected_status, uint tick)
Albert Aribaud306563a2010-08-27 18:26:05 +0200345{
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200346 /* Write byte to data register for sending */
Albert Aribaud306563a2010-08-27 18:26:05 +0200347 writel(byte, &twsi->data);
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200348 /* Clear any pending interrupt -- that will cause sending */
mario.six@gdsys.cc670514f2016-07-21 11:57:04 +0200349 writel(MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_CLEAR_IFLG,
350 &twsi->control);
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200351 /* Wait for controller to receive byte, and check ACK */
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200352 return twsi_wait(twsi, expected_status, tick);
Albert Aribaud306563a2010-08-27 18:26:05 +0200353}
354
355/*
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200356 * twsi_recv() - Receive a byte on the I2C bus.
357 *
358 * The static variable mvtwsi_control_flags controls whether we ack or nak.
359 *
360 * @twsi: The MVTWSI register structure to use.
361 * @byte: The byte to send.
362 * @ack_flag: Flag that determines whether the received byte should
363 * be acknowledged by the controller or not (sent ACK/NAK).
364 * @tick: The duration of a clock cycle at the current I2C speed.
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100365 * Return: Zero if status is as expected, or a non-zero code if either a time
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200366 * out occurred or the status was not the expected one.
Albert Aribaud306563a2010-08-27 18:26:05 +0200367 */
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200368static int twsi_recv(struct mvtwsi_registers *twsi, u8 *byte, int ack_flag,
369 uint tick)
Albert Aribaud306563a2010-08-27 18:26:05 +0200370{
mario.six@gdsys.cc670514f2016-07-21 11:57:04 +0200371 int expected_status, status, control;
Albert Aribaud306563a2010-08-27 18:26:05 +0200372
mario.six@gdsys.cc670514f2016-07-21 11:57:04 +0200373 /* Compute expected status based on passed ACK flag */
374 expected_status = ack_flag ? MVTWSI_STATUS_DATA_R_ACK :
375 MVTWSI_STATUS_DATA_R_NAK;
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200376 /* Acknowledge *previous state*, and launch receive */
mario.six@gdsys.cc670514f2016-07-21 11:57:04 +0200377 control = MVTWSI_CONTROL_TWSIEN;
378 control |= ack_flag == MVTWSI_READ_ACK ? MVTWSI_CONTROL_ACK : 0;
379 writel(control | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200380 /* Wait for controller to receive byte, and assert ACK or NAK */
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200381 status = twsi_wait(twsi, expected_status, tick);
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200382 /* If we did receive the expected byte, store it */
Albert Aribaud306563a2010-08-27 18:26:05 +0200383 if (status == 0)
384 *byte = readl(&twsi->data);
Albert Aribaud306563a2010-08-27 18:26:05 +0200385 return status;
386}
387
388/*
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200389 * twsi_stop() - Assert a STOP condition on the bus.
390 *
391 * This function is also used to force the bus back to idle state (SDA =
392 * SCL = 1).
393 *
394 * @twsi: The MVTWSI register structure to use.
395 * @tick: The duration of a clock cycle at the current I2C speed.
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100396 * Return: Zero if the operation succeeded, or a non-zero code if a time out
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200397 * occurred.
Albert Aribaud306563a2010-08-27 18:26:05 +0200398 */
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200399static int twsi_stop(struct mvtwsi_registers *twsi, uint tick)
Albert Aribaud306563a2010-08-27 18:26:05 +0200400{
401 int control, stop_status;
mario.six@gdsys.cc059fce92016-07-21 11:57:05 +0200402 int status = 0;
Albert Aribaud306563a2010-08-27 18:26:05 +0200403 int timeout = 1000;
404
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200405 /* Assert STOP */
Albert Aribaud306563a2010-08-27 18:26:05 +0200406 control = MVTWSI_CONTROL_TWSIEN | MVTWSI_CONTROL_STOP;
Hans de Goede904dfbf2016-01-14 14:06:25 +0100407 writel(control | MVTWSI_CONTROL_CLEAR_IFLG, &twsi->control);
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200408 /* Wait for IDLE; IFLG won't rise, so we can't use twsi_wait() */
Albert Aribaud306563a2010-08-27 18:26:05 +0200409 do {
410 stop_status = readl(&twsi->status);
411 if (stop_status == MVTWSI_STATUS_IDLE)
412 break;
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200413 ndelay(tick); /* One clock cycle */
Albert Aribaud306563a2010-08-27 18:26:05 +0200414 } while (timeout--);
415 control = readl(&twsi->control);
416 if (stop_status != MVTWSI_STATUS_IDLE)
mario.six@gdsys.cc059fce92016-07-21 11:57:05 +0200417 status = mvtwsi_error(MVTWSI_ERROR_TIMEOUT,
418 control, status, MVTWSI_STATUS_IDLE);
Albert Aribaud306563a2010-08-27 18:26:05 +0200419 return status;
420}
421
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200422/*
423 * twsi_calc_freq() - Compute I2C frequency depending on m and n parameters.
424 *
425 * @n: Parameter 'n' for the frequency calculation algorithm.
426 * @m: Parameter 'm' for the frequency calculation algorithm.
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100427 * Return: The I2C frequency corresponding to the passed m and n parameters.
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200428 */
mario.six@gdsys.cce0758282016-07-21 11:57:06 +0200429static uint twsi_calc_freq(const int n, const int m)
Stefan Roesef582a152015-03-18 09:30:54 +0100430{
Jagan Tekiaec9a0f2016-10-13 14:19:35 +0530431#ifdef CONFIG_ARCH_SUNXI
Tom Rini65cc0e22022-11-16 13:10:41 -0500432 return CFG_SYS_TCLK / (10 * (m + 1) * (1 << n));
Stefan Roesef582a152015-03-18 09:30:54 +0100433#else
Tom Rini65cc0e22022-11-16 13:10:41 -0500434 return CFG_SYS_TCLK / (10 * (m + 1) * (2 << n));
Stefan Roesef582a152015-03-18 09:30:54 +0100435#endif
436}
Albert Aribaud306563a2010-08-27 18:26:05 +0200437
438/*
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200439 * twsi_reset() - Reset the I2C controller.
440 *
441 * Resetting the controller also resets the baud rate and slave address, hence
442 * they must be re-established after the reset.
443 *
444 * @twsi: The MVTWSI register structure to use.
Albert Aribaud306563a2010-08-27 18:26:05 +0200445 */
mario.six@gdsys.cc3c4db632016-07-21 11:57:08 +0200446static void twsi_reset(struct mvtwsi_registers *twsi)
Albert Aribaud306563a2010-08-27 18:26:05 +0200447{
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200448 /* Reset controller */
Albert Aribaud306563a2010-08-27 18:26:05 +0200449 writel(0, &twsi->soft_reset);
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200450 /* Wait 2 ms -- this is what the Marvell LSP does */
Albert Aribaud306563a2010-08-27 18:26:05 +0200451 udelay(20000);
Albert Aribaud306563a2010-08-27 18:26:05 +0200452}
453
454/*
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200455 * __twsi_i2c_set_bus_speed() - Set the speed of the I2C controller.
456 *
457 * This function sets baud rate to the highest possible value that does not
458 * exceed the requested rate.
459 *
460 * @twsi: The MVTWSI register structure to use.
461 * @requested_speed: The desired frequency the controller should run at
462 * in Hz.
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100463 * Return: The actual frequency the controller was configured to.
Albert Aribaud306563a2010-08-27 18:26:05 +0200464 */
mario.six@gdsys.cc3c4db632016-07-21 11:57:08 +0200465static uint __twsi_i2c_set_bus_speed(struct mvtwsi_registers *twsi,
mario.six@gdsys.cc61bc02b2016-07-21 11:57:07 +0200466 uint requested_speed)
Albert Aribaud306563a2010-08-27 18:26:05 +0200467{
mario.six@gdsys.cce0758282016-07-21 11:57:06 +0200468 uint tmp_speed, highest_speed, n, m;
469 uint baud = 0x44; /* Baud rate after controller reset */
Albert Aribaud306563a2010-08-27 18:26:05 +0200470
Albert Aribaud306563a2010-08-27 18:26:05 +0200471 highest_speed = 0;
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200472 /* Successively try m, n combinations, and use the combination
473 * resulting in the largest speed that's not above the requested
474 * speed */
Heiko Schocher4ce5a722009-07-20 09:59:37 +0200475 for (n = 0; n < 8; n++) {
476 for (m = 0; m < 16; m++) {
Stefan Roesef582a152015-03-18 09:30:54 +0100477 tmp_speed = twsi_calc_freq(n, m);
mario.six@gdsys.cc9ec43b02016-07-21 11:57:01 +0200478 if ((tmp_speed <= requested_speed) &&
479 (tmp_speed > highest_speed)) {
Albert Aribaud306563a2010-08-27 18:26:05 +0200480 highest_speed = tmp_speed;
481 baud = (m << 3) | n;
Heiko Schocher4ce5a722009-07-20 09:59:37 +0200482 }
483 }
484 }
Hans de Goede0db2bbd2014-06-13 22:55:48 +0200485 writel(baud, &twsi->baudrate);
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200486
487 /* Wait for controller for one tick */
Igor Opaniuk2147a162021-02-09 13:52:45 +0200488#if CONFIG_IS_ENABLED(DM_I2C)
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200489 ndelay(calc_tick(highest_speed));
490#else
491 ndelay(10000);
492#endif
493 return highest_speed;
Hans de Goede0db2bbd2014-06-13 22:55:48 +0200494}
495
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200496/*
497 * __twsi_i2c_init() - Initialize the I2C controller.
498 *
499 * @twsi: The MVTWSI register structure to use.
500 * @speed: The initial frequency the controller should run at
501 * in Hz.
502 * @slaveadd: The I2C address to be set for the I2C master.
503 * @actual_speed: A output parameter that receives the actual frequency
504 * in Hz the controller was set to by the function.
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100505 * Return: Zero if the operation succeeded, or a non-zero code if a time out
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200506 * occurred.
507 */
mario.six@gdsys.cc3c4db632016-07-21 11:57:08 +0200508static void __twsi_i2c_init(struct mvtwsi_registers *twsi, int speed,
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200509 int slaveadd, uint *actual_speed)
Hans de Goede0db2bbd2014-06-13 22:55:48 +0200510{
Stefan Mavrodiev004b4cd2018-02-13 09:27:40 +0200511 uint tmp_speed;
512
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200513 /* Reset controller */
mario.six@gdsys.cc3c4db632016-07-21 11:57:08 +0200514 twsi_reset(twsi);
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200515 /* Set speed */
Stefan Mavrodiev004b4cd2018-02-13 09:27:40 +0200516 tmp_speed = __twsi_i2c_set_bus_speed(twsi, speed);
Heinrich Schuchardt8bcf12c2018-01-31 00:57:17 +0100517 if (actual_speed)
Stefan Mavrodiev004b4cd2018-02-13 09:27:40 +0200518 *actual_speed = tmp_speed;
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200519 /* Set slave address; even though we don't use it */
Hans de Goede0db2bbd2014-06-13 22:55:48 +0200520 writel(slaveadd, &twsi->slave_address);
521 writel(0, &twsi->xtnd_slave_addr);
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200522 /* Assert STOP, but don't care for the result */
Igor Opaniuk2147a162021-02-09 13:52:45 +0200523#if CONFIG_IS_ENABLED(DM_I2C)
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200524 (void) twsi_stop(twsi, calc_tick(*actual_speed));
525#else
526 (void) twsi_stop(twsi, 10000);
527#endif
Heiko Schocher4ce5a722009-07-20 09:59:37 +0200528}
529
Albert Aribaud306563a2010-08-27 18:26:05 +0200530/*
Sam Edwards250454c2023-07-25 16:13:05 -0600531 * __twsi_i2c_reinit() - Reset and reinitialize the I2C controller.
532 *
533 * This function should be called to get the MVTWSI controller out of the
534 * "bus error" state. It saves and restores the baud and address registers.
535 *
536 * @twsi: The MVTWSI register structure to use.
537 * @tick: The duration of a clock cycle at the current I2C speed.
538 */
539static void __twsi_i2c_reinit(struct mvtwsi_registers *twsi, uint tick)
540{
541 uint baud;
542 uint slaveadd;
543
544 /* Save baud, address registers */
545 baud = readl(&twsi->baudrate);
546 slaveadd = readl(&twsi->slave_address);
547
548 /* Reset controller */
549 twsi_reset(twsi);
550
551 /* Restore baud, address registers */
552 writel(baud, &twsi->baudrate);
553 writel(slaveadd, &twsi->slave_address);
554 writel(0, &twsi->xtnd_slave_addr);
555
556 /* Assert STOP, but don't care for the result */
557 (void) twsi_stop(twsi, tick);
558}
559
560/*
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200561 * i2c_begin() - Start a I2C transaction.
562 *
563 * Begin a I2C transaction with a given expected start status and chip address.
564 * A START is asserted, and the address byte is sent to the I2C controller. The
565 * expected address status will be derived from the direction bit (bit 0) of
566 * the address byte.
567 *
568 * @twsi: The MVTWSI register structure to use.
569 * @expected_start_status: The I2C status the controller is expected to
570 * assert after the address byte was sent.
571 * @addr: The address byte to be sent.
572 * @tick: The duration of a clock cycle at the current
573 * I2C speed.
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100574 * Return: Zero if the operation succeeded, or a non-zero code if a time out or
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200575 * unexpected I2C status occurred.
Albert Aribaud306563a2010-08-27 18:26:05 +0200576 */
mario.six@gdsys.cc3c4db632016-07-21 11:57:08 +0200577static int i2c_begin(struct mvtwsi_registers *twsi, int expected_start_status,
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200578 u8 addr, uint tick)
Heiko Schocher4ce5a722009-07-20 09:59:37 +0200579{
Albert Aribaud306563a2010-08-27 18:26:05 +0200580 int status, expected_addr_status;
Heiko Schocher4ce5a722009-07-20 09:59:37 +0200581
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200582 /* Compute the expected address status from the direction bit in
583 * the address byte */
584 if (addr & 1) /* Reading */
Albert Aribaud306563a2010-08-27 18:26:05 +0200585 expected_addr_status = MVTWSI_STATUS_ADDR_R_ACK;
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200586 else /* Writing */
Albert Aribaud306563a2010-08-27 18:26:05 +0200587 expected_addr_status = MVTWSI_STATUS_ADDR_W_ACK;
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200588 /* Assert START */
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200589 status = twsi_start(twsi, expected_start_status, tick);
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200590 /* Send out the address if the start went well */
Albert Aribaud306563a2010-08-27 18:26:05 +0200591 if (status == 0)
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200592 status = twsi_send(twsi, addr, expected_addr_status, tick);
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200593 /* Return 0, or the status of the first failure */
Albert Aribaud306563a2010-08-27 18:26:05 +0200594 return status;
Heiko Schocher4ce5a722009-07-20 09:59:37 +0200595}
596
Albert Aribaud306563a2010-08-27 18:26:05 +0200597/*
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200598 * __twsi_i2c_probe_chip() - Probe the given I2C chip address.
599 *
600 * This function begins a I2C read transaction, does a dummy read and NAKs; if
601 * the procedure succeeds, the chip is considered to be present.
602 *
603 * @twsi: The MVTWSI register structure to use.
604 * @chip: The chip address to probe.
605 * @tick: The duration of a clock cycle at the current I2C speed.
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100606 * Return: Zero if the operation succeeded, or a non-zero code if a time out or
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200607 * unexpected I2C status occurred.
Albert Aribaud306563a2010-08-27 18:26:05 +0200608 */
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200609static int __twsi_i2c_probe_chip(struct mvtwsi_registers *twsi, uchar chip,
610 uint tick)
Heiko Schocher4ce5a722009-07-20 09:59:37 +0200611{
Albert Aribaud306563a2010-08-27 18:26:05 +0200612 u8 dummy_byte;
613 int status;
614
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200615 /* Begin i2c read */
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200616 status = i2c_begin(twsi, MVTWSI_STATUS_START, (chip << 1) | 1, tick);
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200617 /* Dummy read was accepted: receive byte, but NAK it. */
Albert Aribaud306563a2010-08-27 18:26:05 +0200618 if (status == 0)
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200619 status = twsi_recv(twsi, &dummy_byte, MVTWSI_READ_NAK, tick);
Albert Aribaud306563a2010-08-27 18:26:05 +0200620 /* Stop transaction */
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200621 twsi_stop(twsi, tick);
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200622 /* Return 0, or the status of the first failure */
Albert Aribaud306563a2010-08-27 18:26:05 +0200623 return status;
624}
625
626/*
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200627 * __twsi_i2c_read() - Read data from a I2C chip.
628 *
629 * This function begins a I2C write transaction, and transmits the address
630 * bytes; then begins a I2C read transaction, and receives the data bytes.
Albert Aribaud306563a2010-08-27 18:26:05 +0200631 *
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200632 * NOTE: Some devices want a stop right before the second start, while some
633 * will choke if it is there. Since deciding this is not yet supported in
634 * higher level APIs, we need to make a decision here, and for the moment that
635 * will be a repeated start without a preceding stop.
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200636 *
637 * @twsi: The MVTWSI register structure to use.
638 * @chip: The chip address to read from.
639 * @addr: The address bytes to send.
640 * @alen: The length of the address bytes in bytes.
641 * @data: The buffer to receive the data read from the chip (has to have
642 * a size of at least 'length' bytes).
643 * @length: The amount of data to be read from the chip in bytes.
644 * @tick: The duration of a clock cycle at the current I2C speed.
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100645 * Return: Zero if the operation succeeded, or a non-zero code if a time out or
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200646 * unexpected I2C status occurred.
Albert Aribaud306563a2010-08-27 18:26:05 +0200647 */
mario.six@gdsys.cc3c4db632016-07-21 11:57:08 +0200648static int __twsi_i2c_read(struct mvtwsi_registers *twsi, uchar chip,
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200649 u8 *addr, int alen, uchar *data, int length,
650 uint tick)
Albert Aribaud306563a2010-08-27 18:26:05 +0200651{
mario.six@gdsys.cc059fce92016-07-21 11:57:05 +0200652 int status = 0;
653 int stop_status;
mario.six@gdsys.cc24f9c6b2016-07-21 11:57:11 +0200654 int expected_start = MVTWSI_STATUS_START;
Albert Aribaud306563a2010-08-27 18:26:05 +0200655
Sam Edwards250454c2023-07-25 16:13:05 -0600656 /* Check for (and clear) a bus error from a previous failed transaction
657 * or another master on the same bus */
658 if (readl(&twsi->status) == MVTWSI_BUS_ERROR)
659 __twsi_i2c_reinit(twsi, tick);
660
mario.six@gdsys.cc24f9c6b2016-07-21 11:57:11 +0200661 if (alen > 0) {
662 /* Begin i2c write to send the address bytes */
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200663 status = i2c_begin(twsi, expected_start, (chip << 1), tick);
mario.six@gdsys.cc24f9c6b2016-07-21 11:57:11 +0200664 /* Send address bytes */
665 while ((status == 0) && alen--)
Stefan Roese03d6cd92016-08-25 15:20:01 +0200666 status = twsi_send(twsi, addr[alen],
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200667 MVTWSI_STATUS_DATA_W_ACK, tick);
mario.six@gdsys.cc24f9c6b2016-07-21 11:57:11 +0200668 /* Send repeated STARTs after the initial START */
669 expected_start = MVTWSI_STATUS_REPEATED_START;
670 }
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200671 /* Begin i2c read to receive data bytes */
Albert Aribaud306563a2010-08-27 18:26:05 +0200672 if (status == 0)
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200673 status = i2c_begin(twsi, expected_start, (chip << 1) | 1, tick);
mario.six@gdsys.cc670514f2016-07-21 11:57:04 +0200674 /* Receive actual data bytes; set NAK if we if we have nothing more to
675 * read */
676 while ((status == 0) && length--)
mario.six@gdsys.cc3c4db632016-07-21 11:57:08 +0200677 status = twsi_recv(twsi, data++,
mario.six@gdsys.cc670514f2016-07-21 11:57:04 +0200678 length > 0 ?
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200679 MVTWSI_READ_ACK : MVTWSI_READ_NAK, tick);
Albert Aribaud306563a2010-08-27 18:26:05 +0200680 /* Stop transaction */
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200681 stop_status = twsi_stop(twsi, tick);
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200682 /* Return 0, or the status of the first failure */
mario.six@gdsys.cc059fce92016-07-21 11:57:05 +0200683 return status != 0 ? status : stop_status;
Heiko Schocher4ce5a722009-07-20 09:59:37 +0200684}
685
Albert Aribaud306563a2010-08-27 18:26:05 +0200686/*
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200687 * __twsi_i2c_write() - Send data to a I2C chip.
688 *
689 * This function begins a I2C write transaction, and transmits the address
690 * bytes; then begins a new I2C write transaction, and sends the data bytes.
691 *
692 * @twsi: The MVTWSI register structure to use.
693 * @chip: The chip address to read from.
694 * @addr: The address bytes to send.
695 * @alen: The length of the address bytes in bytes.
696 * @data: The buffer containing the data to be sent to the chip.
697 * @length: The length of data to be sent to the chip in bytes.
698 * @tick: The duration of a clock cycle at the current I2C speed.
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100699 * Return: Zero if the operation succeeded, or a non-zero code if a time out or
mario.six@gdsys.cc6e677ca2016-07-21 11:57:13 +0200700 * unexpected I2C status occurred.
Albert Aribaud306563a2010-08-27 18:26:05 +0200701 */
mario.six@gdsys.cc3c4db632016-07-21 11:57:08 +0200702static int __twsi_i2c_write(struct mvtwsi_registers *twsi, uchar chip,
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200703 u8 *addr, int alen, uchar *data, int length,
704 uint tick)
Heiko Schocher4ce5a722009-07-20 09:59:37 +0200705{
mario.six@gdsys.cc059fce92016-07-21 11:57:05 +0200706 int status, stop_status;
Heiko Schocher4ce5a722009-07-20 09:59:37 +0200707
Sam Edwards250454c2023-07-25 16:13:05 -0600708 /* Check for (and clear) a bus error from a previous failed transaction
709 * or another master on the same bus */
710 if (readl(&twsi->status) == MVTWSI_BUS_ERROR)
711 __twsi_i2c_reinit(twsi, tick);
712
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200713 /* Begin i2c write to send first the address bytes, then the
714 * data bytes */
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200715 status = i2c_begin(twsi, MVTWSI_STATUS_START, (chip << 1), tick);
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200716 /* Send address bytes */
mario.six@gdsys.ccf8a10ed2016-07-21 11:57:09 +0200717 while ((status == 0) && (alen-- > 0))
Stefan Roese03d6cd92016-08-25 15:20:01 +0200718 status = twsi_send(twsi, addr[alen], MVTWSI_STATUS_DATA_W_ACK,
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200719 tick);
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200720 /* Send data bytes */
Albert Aribaud306563a2010-08-27 18:26:05 +0200721 while ((status == 0) && (length-- > 0))
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200722 status = twsi_send(twsi, *(data++), MVTWSI_STATUS_DATA_W_ACK,
723 tick);
Albert Aribaud306563a2010-08-27 18:26:05 +0200724 /* Stop transaction */
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200725 stop_status = twsi_stop(twsi, tick);
mario.six@gdsys.cc49c801b2016-07-21 11:57:03 +0200726 /* Return 0, or the status of the first failure */
mario.six@gdsys.cc059fce92016-07-21 11:57:05 +0200727 return status != 0 ? status : stop_status;
Heiko Schocher4ce5a722009-07-20 09:59:37 +0200728}
729
Igor Opaniuk2147a162021-02-09 13:52:45 +0200730#if !CONFIG_IS_ENABLED(DM_I2C)
mario.six@gdsys.cc61bc02b2016-07-21 11:57:07 +0200731static void twsi_i2c_init(struct i2c_adapter *adap, int speed,
732 int slaveadd)
733{
mario.six@gdsys.cc3c4db632016-07-21 11:57:08 +0200734 struct mvtwsi_registers *twsi = twsi_get_base(adap);
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200735 __twsi_i2c_init(twsi, speed, slaveadd, NULL);
mario.six@gdsys.cc61bc02b2016-07-21 11:57:07 +0200736}
737
738static uint twsi_i2c_set_bus_speed(struct i2c_adapter *adap,
739 uint requested_speed)
740{
mario.six@gdsys.cc3c4db632016-07-21 11:57:08 +0200741 struct mvtwsi_registers *twsi = twsi_get_base(adap);
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200742 __twsi_i2c_set_bus_speed(twsi, requested_speed);
743 return 0;
mario.six@gdsys.cc61bc02b2016-07-21 11:57:07 +0200744}
745
746static int twsi_i2c_probe(struct i2c_adapter *adap, uchar chip)
747{
mario.six@gdsys.cc3c4db632016-07-21 11:57:08 +0200748 struct mvtwsi_registers *twsi = twsi_get_base(adap);
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200749 return __twsi_i2c_probe_chip(twsi, chip, 10000);
mario.six@gdsys.cc61bc02b2016-07-21 11:57:07 +0200750}
751
752static int twsi_i2c_read(struct i2c_adapter *adap, uchar chip, uint addr,
753 int alen, uchar *data, int length)
754{
mario.six@gdsys.cc3c4db632016-07-21 11:57:08 +0200755 struct mvtwsi_registers *twsi = twsi_get_base(adap);
mario.six@gdsys.ccf8a10ed2016-07-21 11:57:09 +0200756 u8 addr_bytes[4];
757
758 addr_bytes[0] = (addr >> 0) & 0xFF;
759 addr_bytes[1] = (addr >> 8) & 0xFF;
760 addr_bytes[2] = (addr >> 16) & 0xFF;
761 addr_bytes[3] = (addr >> 24) & 0xFF;
762
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200763 return __twsi_i2c_read(twsi, chip, addr_bytes, alen, data, length,
764 10000);
mario.six@gdsys.cc61bc02b2016-07-21 11:57:07 +0200765}
766
767static int twsi_i2c_write(struct i2c_adapter *adap, uchar chip, uint addr,
768 int alen, uchar *data, int length)
769{
mario.six@gdsys.cc3c4db632016-07-21 11:57:08 +0200770 struct mvtwsi_registers *twsi = twsi_get_base(adap);
mario.six@gdsys.ccf8a10ed2016-07-21 11:57:09 +0200771 u8 addr_bytes[4];
772
773 addr_bytes[0] = (addr >> 0) & 0xFF;
774 addr_bytes[1] = (addr >> 8) & 0xFF;
775 addr_bytes[2] = (addr >> 16) & 0xFF;
776 addr_bytes[3] = (addr >> 24) & 0xFF;
777
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200778 return __twsi_i2c_write(twsi, chip, addr_bytes, alen, data, length,
779 10000);
mario.six@gdsys.cc61bc02b2016-07-21 11:57:07 +0200780}
781
Tom Rini45ede972022-12-04 10:04:09 -0500782#ifdef CFG_I2C_MVTWSI_BASE0
Hans de Goede0db2bbd2014-06-13 22:55:48 +0200783U_BOOT_I2C_ADAP_COMPLETE(twsi0, twsi_i2c_init, twsi_i2c_probe,
784 twsi_i2c_read, twsi_i2c_write,
785 twsi_i2c_set_bus_speed,
786 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0)
Paul Kocialkowskidd822422015-04-10 23:09:51 +0200787#endif
Tom Rini35661f82022-12-04 10:04:10 -0500788#ifdef CFG_I2C_MVTWSI_BASE1
Paul Kocialkowskidd822422015-04-10 23:09:51 +0200789U_BOOT_I2C_ADAP_COMPLETE(twsi1, twsi_i2c_init, twsi_i2c_probe,
790 twsi_i2c_read, twsi_i2c_write,
791 twsi_i2c_set_bus_speed,
792 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 1)
793
794#endif
Tom Rini6e7df1d2023-01-10 11:19:45 -0500795#ifdef CFG_I2C_MVTWSI_BASE2
Paul Kocialkowskidd822422015-04-10 23:09:51 +0200796U_BOOT_I2C_ADAP_COMPLETE(twsi2, twsi_i2c_init, twsi_i2c_probe,
797 twsi_i2c_read, twsi_i2c_write,
798 twsi_i2c_set_bus_speed,
799 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 2)
800
801#endif
802#ifdef CONFIG_I2C_MVTWSI_BASE3
803U_BOOT_I2C_ADAP_COMPLETE(twsi3, twsi_i2c_init, twsi_i2c_probe,
804 twsi_i2c_read, twsi_i2c_write,
805 twsi_i2c_set_bus_speed,
806 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 3)
807
808#endif
809#ifdef CONFIG_I2C_MVTWSI_BASE4
810U_BOOT_I2C_ADAP_COMPLETE(twsi4, twsi_i2c_init, twsi_i2c_probe,
811 twsi_i2c_read, twsi_i2c_write,
812 twsi_i2c_set_bus_speed,
813 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 4)
814
815#endif
Jelle van der Waa9d082682016-01-14 14:06:26 +0100816#ifdef CONFIG_I2C_MVTWSI_BASE5
817U_BOOT_I2C_ADAP_COMPLETE(twsi5, twsi_i2c_init, twsi_i2c_probe,
818 twsi_i2c_read, twsi_i2c_write,
819 twsi_i2c_set_bus_speed,
820 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 5)
821
822#endif
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +0200823#else /* CONFIG_DM_I2C */
824
825static int mvtwsi_i2c_probe_chip(struct udevice *bus, u32 chip_addr,
826 u32 chip_flags)
827{
828 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200829 return __twsi_i2c_probe_chip(dev->base, chip_addr, dev->tick);
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +0200830}
831
832static int mvtwsi_i2c_set_bus_speed(struct udevice *bus, uint speed)
833{
834 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200835
836 dev->speed = __twsi_i2c_set_bus_speed(dev->base, speed);
837 dev->tick = calc_tick(dev->speed);
838
839 return 0;
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +0200840}
841
Simon Glassd1998a92020-12-03 16:55:21 -0700842static int mvtwsi_i2c_of_to_plat(struct udevice *bus)
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +0200843{
844 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
845
Masahiro Yamada702e57e2020-08-04 14:14:43 +0900846 dev->base = dev_read_addr_ptr(bus);
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +0200847
848 if (!dev->base)
849 return -ENOMEM;
850
Simon Glasse160f7d2017-01-17 16:52:55 -0700851 dev->index = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus),
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +0200852 "cell-index", -1);
Simon Glasse160f7d2017-01-17 16:52:55 -0700853 dev->slaveadd = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus),
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +0200854 "u-boot,i2c-slave-addr", 0x0);
Simon Glassf3d46152020-01-23 11:48:22 -0700855 dev->speed = dev_read_u32_default(bus, "clock-frequency",
856 I2C_SPEED_STANDARD_RATE);
857
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +0200858 return 0;
859}
860
Baruch Siach173ec352018-06-07 12:38:10 +0300861static void twsi_disable_i2c_slave(struct mvtwsi_registers *twsi)
862{
863 clrbits_le32(&twsi->debug, BIT(18));
864}
865
866static int mvtwsi_i2c_bind(struct udevice *bus)
867{
Masahiro Yamada702e57e2020-08-04 14:14:43 +0900868 struct mvtwsi_registers *twsi = dev_read_addr_ptr(bus);
Baruch Siach173ec352018-06-07 12:38:10 +0300869
870 /* Disable the hidden slave in i2c0 of these platforms */
Simon Glass16df9932020-12-16 21:20:15 -0700871 if ((IS_ENABLED(CONFIG_ARMADA_38X) ||
872 IS_ENABLED(CONFIG_ARCH_KIRKWOOD) ||
873 IS_ENABLED(CONFIG_ARMADA_8K)) && !dev_seq(bus))
Baruch Siach173ec352018-06-07 12:38:10 +0300874 twsi_disable_i2c_slave(twsi);
875
876 return 0;
877}
878
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +0200879static int mvtwsi_i2c_probe(struct udevice *bus)
880{
881 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
Samuel Holland06cec892021-09-12 10:21:39 -0500882 struct reset_ctl reset;
883 struct clk clk;
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200884 uint actual_speed;
Samuel Holland06cec892021-09-12 10:21:39 -0500885 int ret;
886
887 ret = reset_get_by_index(bus, 0, &reset);
888 if (!ret)
889 reset_deassert(&reset);
890
891 ret = clk_get_by_index(bus, 0, &clk);
892 if (!ret)
893 clk_enable(&clk);
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200894
895 __twsi_i2c_init(dev->base, dev->speed, dev->slaveadd, &actual_speed);
896 dev->speed = actual_speed;
897 dev->tick = calc_tick(dev->speed);
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +0200898 return 0;
899}
900
901static int mvtwsi_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
902{
903 struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
904 struct i2c_msg *dmsg, *omsg, dummy;
Stefan Roeseccea46c2021-11-18 09:18:41 +0100905 u8 *addr_buf_ptr;
906 u8 addr_buf[4];
907 int i;
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +0200908
909 memset(&dummy, 0, sizeof(struct i2c_msg));
910
911 /* We expect either two messages (one with an offset and one with the
912 * actual data) or one message (just data or offset/data combined) */
913 if (nmsgs > 2 || nmsgs == 0) {
914 debug("%s: Only one or two messages are supported.", __func__);
915 return -1;
916 }
917
918 omsg = nmsgs == 1 ? &dummy : msg;
919 dmsg = nmsgs == 1 ? msg : msg + 1;
920
Stefan Roeseccea46c2021-11-18 09:18:41 +0100921 /* We need to swap the register address if its size is > 1 */
922 addr_buf_ptr = &addr_buf[0];
923 for (i = omsg->len; i > 0; i--)
924 *addr_buf_ptr++ = omsg->buf[i - 1];
925
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +0200926 if (dmsg->flags & I2C_M_RD)
Stefan Roeseccea46c2021-11-18 09:18:41 +0100927 return __twsi_i2c_read(dev->base, dmsg->addr, addr_buf,
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200928 omsg->len, dmsg->buf, dmsg->len,
929 dev->tick);
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +0200930 else
Stefan Roeseccea46c2021-11-18 09:18:41 +0100931 return __twsi_i2c_write(dev->base, dmsg->addr, addr_buf,
mario.six@gdsys.ccc68c6242016-07-21 11:57:12 +0200932 omsg->len, dmsg->buf, dmsg->len,
933 dev->tick);
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +0200934}
935
936static const struct dm_i2c_ops mvtwsi_i2c_ops = {
937 .xfer = mvtwsi_i2c_xfer,
938 .probe_chip = mvtwsi_i2c_probe_chip,
939 .set_bus_speed = mvtwsi_i2c_set_bus_speed,
940};
941
942static const struct udevice_id mvtwsi_i2c_ids[] = {
943 { .compatible = "marvell,mv64xxx-i2c", },
Stefan Roese87de0eb2016-09-16 15:07:55 +0200944 { .compatible = "marvell,mv78230-i2c", },
Chris Morganeb31a4a2022-01-07 11:52:54 -0600945 { .compatible = "allwinner,sun4i-a10-i2c", },
Jernej Skrabeca8f01cc2017-04-27 00:03:36 +0200946 { .compatible = "allwinner,sun6i-a31-i2c", },
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +0200947 { /* sentinel */ }
948};
949
950U_BOOT_DRIVER(i2c_mvtwsi) = {
951 .name = "i2c_mvtwsi",
952 .id = UCLASS_I2C,
953 .of_match = mvtwsi_i2c_ids,
Baruch Siach173ec352018-06-07 12:38:10 +0300954 .bind = mvtwsi_i2c_bind,
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +0200955 .probe = mvtwsi_i2c_probe,
Simon Glassd1998a92020-12-03 16:55:21 -0700956 .of_to_plat = mvtwsi_i2c_of_to_plat,
Simon Glass41575d82020-12-03 16:55:17 -0700957 .priv_auto = sizeof(struct mvtwsi_i2c_dev),
mario.six@gdsys.cc14a6ff22016-07-21 11:57:10 +0200958 .ops = &mvtwsi_i2c_ops,
959};
960#endif /* CONFIG_DM_I2C */