blob: 3f7cf8533ec763e44e12f10786f93342c0f229ce [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Moritz Fischer0075dac2015-12-28 09:47:11 -08002/*
3 * Copyright (C) 2015 Moritz Fischer <moritz.fischer@ettus.com>
4 * IP from Cadence (ID T-CS-PE-0007-100, Version R1p10f2)
5 *
6 * This file is based on: drivers/i2c/zynq_i2c.c,
7 * with added driver-model support and code cleanup.
Moritz Fischer0075dac2015-12-28 09:47:11 -08008 */
9
Simon Glass11c89f32017-05-17 17:18:03 -060010#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060012#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060013#include <linux/delay.h>
Moritz Fischer0075dac2015-12-28 09:47:11 -080014#include <linux/types.h>
15#include <linux/io.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090016#include <linux/errno.h>
T Karthik Reddyf8149872021-02-03 03:10:46 -070017#include <dm/device_compat.h>
Moritz Fischer0075dac2015-12-28 09:47:11 -080018#include <dm/root.h>
19#include <i2c.h>
20#include <fdtdec.h>
21#include <mapmem.h>
Moritz Fischer9393f582017-01-16 09:50:46 -080022#include <wait_bit.h>
Tomasz Gorochowik59e25852019-01-03 13:36:33 +010023#include <clk.h>
Moritz Fischer0075dac2015-12-28 09:47:11 -080024
Moritz Fischer0075dac2015-12-28 09:47:11 -080025/* i2c register set */
26struct cdns_i2c_regs {
27 u32 control;
28 u32 status;
29 u32 address;
30 u32 data;
31 u32 interrupt_status;
32 u32 transfer_size;
33 u32 slave_mon_pause;
34 u32 time_out;
35 u32 interrupt_mask;
36 u32 interrupt_enable;
37 u32 interrupt_disable;
38};
39
40/* Control register fields */
41#define CDNS_I2C_CONTROL_RW 0x00000001
42#define CDNS_I2C_CONTROL_MS 0x00000002
43#define CDNS_I2C_CONTROL_NEA 0x00000004
44#define CDNS_I2C_CONTROL_ACKEN 0x00000008
45#define CDNS_I2C_CONTROL_HOLD 0x00000010
46#define CDNS_I2C_CONTROL_SLVMON 0x00000020
47#define CDNS_I2C_CONTROL_CLR_FIFO 0x00000040
48#define CDNS_I2C_CONTROL_DIV_B_SHIFT 8
49#define CDNS_I2C_CONTROL_DIV_B_MASK 0x00003F00
50#define CDNS_I2C_CONTROL_DIV_A_SHIFT 14
51#define CDNS_I2C_CONTROL_DIV_A_MASK 0x0000C000
52
53/* Status register values */
54#define CDNS_I2C_STATUS_RXDV 0x00000020
55#define CDNS_I2C_STATUS_TXDV 0x00000040
56#define CDNS_I2C_STATUS_RXOVF 0x00000080
57#define CDNS_I2C_STATUS_BA 0x00000100
58
59/* Interrupt register fields */
60#define CDNS_I2C_INTERRUPT_COMP 0x00000001
61#define CDNS_I2C_INTERRUPT_DATA 0x00000002
62#define CDNS_I2C_INTERRUPT_NACK 0x00000004
63#define CDNS_I2C_INTERRUPT_TO 0x00000008
64#define CDNS_I2C_INTERRUPT_SLVRDY 0x00000010
65#define CDNS_I2C_INTERRUPT_RXOVF 0x00000020
66#define CDNS_I2C_INTERRUPT_TXOVF 0x00000040
67#define CDNS_I2C_INTERRUPT_RXUNF 0x00000080
68#define CDNS_I2C_INTERRUPT_ARBLOST 0x00000200
69
Siva Durga Prasad Paladugu4f0205b2019-03-07 11:52:48 +010070#define CDNS_I2C_INTERRUPTS_MASK (CDNS_I2C_INTERRUPT_COMP | \
71 CDNS_I2C_INTERRUPT_DATA | \
72 CDNS_I2C_INTERRUPT_NACK | \
73 CDNS_I2C_INTERRUPT_TO | \
74 CDNS_I2C_INTERRUPT_SLVRDY | \
75 CDNS_I2C_INTERRUPT_RXOVF | \
76 CDNS_I2C_INTERRUPT_TXOVF | \
77 CDNS_I2C_INTERRUPT_RXUNF | \
78 CDNS_I2C_INTERRUPT_ARBLOST)
79
Pei Yue Hoca394722023-02-13 00:02:41 -080080#define CDNS_I2C_FIFO_DEPTH_DEFAULT 16
Moritz Fischer0075dac2015-12-28 09:47:11 -080081#define CDNS_I2C_TRANSFER_SIZE_MAX 255 /* Controller transfer limit */
Moritz Fischer9393f582017-01-16 09:50:46 -080082#define CDNS_I2C_TRANSFER_SIZE (CDNS_I2C_TRANSFER_SIZE_MAX - 3)
83
Moritz Fischer61f06512017-01-16 09:50:44 -080084#define CDNS_I2C_BROKEN_HOLD_BIT BIT(0)
Moritz Fischer0075dac2015-12-28 09:47:11 -080085
Siva Durga Prasad Paladugu07c5a1b2019-03-07 11:52:49 +010086#define CDNS_I2C_ARB_LOST_MAX_RETRIES 10
87
Moritz Fischer0075dac2015-12-28 09:47:11 -080088#ifdef DEBUG
89static void cdns_i2c_debug_status(struct cdns_i2c_regs *cdns_i2c)
90{
91 int int_status;
92 int status;
93 int_status = readl(&cdns_i2c->interrupt_status);
94
95 status = readl(&cdns_i2c->status);
96 if (int_status || status) {
97 debug("Status: ");
98 if (int_status & CDNS_I2C_INTERRUPT_COMP)
99 debug("COMP ");
100 if (int_status & CDNS_I2C_INTERRUPT_DATA)
101 debug("DATA ");
102 if (int_status & CDNS_I2C_INTERRUPT_NACK)
103 debug("NACK ");
104 if (int_status & CDNS_I2C_INTERRUPT_TO)
105 debug("TO ");
106 if (int_status & CDNS_I2C_INTERRUPT_SLVRDY)
107 debug("SLVRDY ");
108 if (int_status & CDNS_I2C_INTERRUPT_RXOVF)
109 debug("RXOVF ");
110 if (int_status & CDNS_I2C_INTERRUPT_TXOVF)
111 debug("TXOVF ");
112 if (int_status & CDNS_I2C_INTERRUPT_RXUNF)
113 debug("RXUNF ");
114 if (int_status & CDNS_I2C_INTERRUPT_ARBLOST)
115 debug("ARBLOST ");
116 if (status & CDNS_I2C_STATUS_RXDV)
117 debug("RXDV ");
118 if (status & CDNS_I2C_STATUS_TXDV)
119 debug("TXDV ");
120 if (status & CDNS_I2C_STATUS_RXOVF)
121 debug("RXOVF ");
122 if (status & CDNS_I2C_STATUS_BA)
123 debug("BA ");
124 debug("TS%d ", readl(&cdns_i2c->transfer_size));
125 debug("\n");
126 }
127}
128#endif
129
130struct i2c_cdns_bus {
131 int id;
Michal Simek91429f42016-04-14 14:15:49 +0200132 unsigned int input_freq;
Moritz Fischer0075dac2015-12-28 09:47:11 -0800133 struct cdns_i2c_regs __iomem *regs; /* register base */
Moritz Fischer61f06512017-01-16 09:50:44 -0800134
135 int hold_flag;
136 u32 quirks;
Pei Yue Hoca394722023-02-13 00:02:41 -0800137 u32 fifo_depth;
Moritz Fischer61f06512017-01-16 09:50:44 -0800138};
139
140struct cdns_i2c_platform_data {
141 u32 quirks;
Moritz Fischer0075dac2015-12-28 09:47:11 -0800142};
143
Moritz Fischer0075dac2015-12-28 09:47:11 -0800144/* Wait for an interrupt */
145static u32 cdns_i2c_wait(struct cdns_i2c_regs *cdns_i2c, u32 mask)
146{
147 int timeout, int_status;
148
149 for (timeout = 0; timeout < 100; timeout++) {
Moritz Fischer0075dac2015-12-28 09:47:11 -0800150 int_status = readl(&cdns_i2c->interrupt_status);
151 if (int_status & mask)
152 break;
Moritz Fischer0b245792017-01-16 09:50:45 -0800153 udelay(100);
Moritz Fischer0075dac2015-12-28 09:47:11 -0800154 }
155
156 /* Clear interrupt status flags */
157 writel(int_status & mask, &cdns_i2c->interrupt_status);
158
159 return int_status & mask;
160}
161
Michal Simek91429f42016-04-14 14:15:49 +0200162#define CDNS_I2C_DIVA_MAX 4
163#define CDNS_I2C_DIVB_MAX 64
164
165static int cdns_i2c_calc_divs(unsigned long *f, unsigned long input_clk,
166 unsigned int *a, unsigned int *b)
167{
168 unsigned long fscl = *f, best_fscl = *f, actual_fscl, temp;
169 unsigned int div_a, div_b, calc_div_a = 0, calc_div_b = 0;
170 unsigned int last_error, current_error;
171
172 /* calculate (divisor_a+1) x (divisor_b+1) */
173 temp = input_clk / (22 * fscl);
174
175 /*
176 * If the calculated value is negative or 0CDNS_I2C_DIVA_MAX,
177 * the fscl input is out of range. Return error.
178 */
179 if (!temp || (temp > (CDNS_I2C_DIVA_MAX * CDNS_I2C_DIVB_MAX)))
180 return -EINVAL;
181
182 last_error = -1;
183 for (div_a = 0; div_a < CDNS_I2C_DIVA_MAX; div_a++) {
184 div_b = DIV_ROUND_UP(input_clk, 22 * fscl * (div_a + 1));
185
186 if ((div_b < 1) || (div_b > CDNS_I2C_DIVB_MAX))
187 continue;
188 div_b--;
189
190 actual_fscl = input_clk / (22 * (div_a + 1) * (div_b + 1));
191
192 if (actual_fscl > fscl)
193 continue;
194
195 current_error = ((actual_fscl > fscl) ? (actual_fscl - fscl) :
196 (fscl - actual_fscl));
197
198 if (last_error > current_error) {
199 calc_div_a = div_a;
200 calc_div_b = div_b;
201 best_fscl = actual_fscl;
202 last_error = current_error;
203 }
204 }
205
206 *a = calc_div_a;
207 *b = calc_div_b;
208 *f = best_fscl;
209
210 return 0;
211}
212
Moritz Fischer0075dac2015-12-28 09:47:11 -0800213static int cdns_i2c_set_bus_speed(struct udevice *dev, unsigned int speed)
214{
Michal Simek48cd2062016-04-14 14:15:48 +0200215 struct i2c_cdns_bus *bus = dev_get_priv(dev);
Michal Simek91429f42016-04-14 14:15:49 +0200216 u32 div_a = 0, div_b = 0;
217 unsigned long speed_p = speed;
218 int ret = 0;
Michal Simek48cd2062016-04-14 14:15:48 +0200219
Simon Glassf0c99c52020-01-23 11:48:22 -0700220 if (speed > I2C_SPEED_FAST_RATE) {
Michal Simek91429f42016-04-14 14:15:49 +0200221 debug("%s, failed to set clock speed to %u\n", __func__,
222 speed);
Moritz Fischer0075dac2015-12-28 09:47:11 -0800223 return -EINVAL;
224 }
225
Michal Simek91429f42016-04-14 14:15:49 +0200226 ret = cdns_i2c_calc_divs(&speed_p, bus->input_freq, &div_a, &div_b);
227 if (ret)
228 return ret;
229
230 debug("%s: div_a: %d, div_b: %d, input freq: %d, speed: %d/%ld\n",
231 __func__, div_a, div_b, bus->input_freq, speed, speed_p);
232
233 writel((div_b << CDNS_I2C_CONTROL_DIV_B_SHIFT) |
234 (div_a << CDNS_I2C_CONTROL_DIV_A_SHIFT), &bus->regs->control);
Michal Simek48cd2062016-04-14 14:15:48 +0200235
236 /* Enable master mode, ack, and 7-bit addressing */
237 setbits_le32(&bus->regs->control, CDNS_I2C_CONTROL_MS |
238 CDNS_I2C_CONTROL_ACKEN | CDNS_I2C_CONTROL_NEA);
239
Moritz Fischer0075dac2015-12-28 09:47:11 -0800240 return 0;
241}
242
Siva Durga Prasad Paladugu07c5a1b2019-03-07 11:52:49 +0100243static inline u32 is_arbitration_lost(struct cdns_i2c_regs *regs)
244{
245 return (readl(&regs->interrupt_status) & CDNS_I2C_INTERRUPT_ARBLOST);
246}
247
Moritz Fischer0075dac2015-12-28 09:47:11 -0800248static int cdns_i2c_write_data(struct i2c_cdns_bus *i2c_bus, u32 addr, u8 *data,
Moritz Fischer61f06512017-01-16 09:50:44 -0800249 u32 len)
Moritz Fischer0075dac2015-12-28 09:47:11 -0800250{
251 u8 *cur_data = data;
Moritz Fischer0075dac2015-12-28 09:47:11 -0800252 struct cdns_i2c_regs *regs = i2c_bus->regs;
Siva Durga Prasad Paladugu07c5a1b2019-03-07 11:52:49 +0100253 u32 ret;
Sai Pavan Boddu252b4a32022-03-01 09:16:51 +0100254 bool start = 1;
Moritz Fischer0075dac2015-12-28 09:47:11 -0800255
Moritz Fischer9393f582017-01-16 09:50:46 -0800256 /* Set the controller in Master transmit mode and clear FIFO */
Moritz Fischer61f06512017-01-16 09:50:44 -0800257 setbits_le32(&regs->control, CDNS_I2C_CONTROL_CLR_FIFO);
Moritz Fischer0075dac2015-12-28 09:47:11 -0800258 clrbits_le32(&regs->control, CDNS_I2C_CONTROL_RW);
259
Sai Pavan Boddu19497b42022-03-01 09:16:52 +0100260 /*
261 * For sequential data load hold the bus.
Moritz Fischer9393f582017-01-16 09:50:46 -0800262 */
Sai Pavan Boddu19497b42022-03-01 09:16:52 +0100263 if (len > 1)
Moritz Fischer9393f582017-01-16 09:50:46 -0800264 setbits_le32(&regs->control, CDNS_I2C_CONTROL_HOLD);
265
266 /* Clear the interrupts in status register */
Siva Durga Prasad Paladugu4f0205b2019-03-07 11:52:48 +0100267 writel(CDNS_I2C_INTERRUPTS_MASK, &regs->interrupt_status);
Moritz Fischer9393f582017-01-16 09:50:46 -0800268
Sai Pavan Boddu19497b42022-03-01 09:16:52 +0100269 /* In case of Probe (i.e no data), start the transfer */
270 if (!len)
271 writel(addr, &regs->address);
Moritz Fischer0075dac2015-12-28 09:47:11 -0800272
Siva Durga Prasad Paladugu07c5a1b2019-03-07 11:52:49 +0100273 while (len-- && !is_arbitration_lost(regs)) {
Moritz Fischer0075dac2015-12-28 09:47:11 -0800274 writel(*(cur_data++), &regs->data);
Sai Pavan Boddu252b4a32022-03-01 09:16:51 +0100275 /* Trigger write only after loading data */
276 if (start) {
277 writel(addr, &regs->address);
278 start = 0;
279 }
Pei Yue Hoca394722023-02-13 00:02:41 -0800280 if (len && readl(&regs->transfer_size) == i2c_bus->fifo_depth) {
Siva Durga Prasad Paladugu07c5a1b2019-03-07 11:52:49 +0100281 ret = cdns_i2c_wait(regs, CDNS_I2C_INTERRUPT_COMP |
282 CDNS_I2C_INTERRUPT_ARBLOST);
283 if (ret & CDNS_I2C_INTERRUPT_ARBLOST)
284 return -EAGAIN;
285 if (ret & CDNS_I2C_INTERRUPT_COMP)
286 continue;
287 /* Release the bus */
288 clrbits_le32(&regs->control,
289 CDNS_I2C_CONTROL_HOLD);
290 return -ETIMEDOUT;
Moritz Fischer0075dac2015-12-28 09:47:11 -0800291 }
292 }
293
Siva Durga Prasad Paladugu07c5a1b2019-03-07 11:52:49 +0100294 if (len && is_arbitration_lost(regs))
295 return -EAGAIN;
296
Moritz Fischer0075dac2015-12-28 09:47:11 -0800297 /* All done... release the bus */
Moritz Fischer61f06512017-01-16 09:50:44 -0800298 if (!i2c_bus->hold_flag)
299 clrbits_le32(&regs->control, CDNS_I2C_CONTROL_HOLD);
300
Moritz Fischer0075dac2015-12-28 09:47:11 -0800301 /* Wait for the address and data to be sent */
Siva Durga Prasad Paladugu07c5a1b2019-03-07 11:52:49 +0100302 ret = cdns_i2c_wait(regs, CDNS_I2C_INTERRUPT_COMP |
303 CDNS_I2C_INTERRUPT_ARBLOST);
304 if (!(ret & (CDNS_I2C_INTERRUPT_ARBLOST |
305 CDNS_I2C_INTERRUPT_COMP)))
Moritz Fischer0075dac2015-12-28 09:47:11 -0800306 return -ETIMEDOUT;
Siva Durga Prasad Paladugu07c5a1b2019-03-07 11:52:49 +0100307 if (ret & CDNS_I2C_INTERRUPT_ARBLOST)
308 return -EAGAIN;
309
Moritz Fischer0075dac2015-12-28 09:47:11 -0800310 return 0;
311}
312
Pei Yue Hoca394722023-02-13 00:02:41 -0800313static inline bool cdns_is_hold_quirk(struct i2c_cdns_bus *i2c_bus, int hold_quirk,
314 int curr_recv_count)
Moritz Fischer9393f582017-01-16 09:50:46 -0800315{
Pei Yue Hoca394722023-02-13 00:02:41 -0800316 return hold_quirk && (curr_recv_count == i2c_bus->fifo_depth + 1);
Moritz Fischer9393f582017-01-16 09:50:46 -0800317}
318
Moritz Fischer0075dac2015-12-28 09:47:11 -0800319static int cdns_i2c_read_data(struct i2c_cdns_bus *i2c_bus, u32 addr, u8 *data,
Moritz Fischer9393f582017-01-16 09:50:46 -0800320 u32 recv_count)
Moritz Fischer0075dac2015-12-28 09:47:11 -0800321{
Moritz Fischer0075dac2015-12-28 09:47:11 -0800322 u8 *cur_data = data;
Moritz Fischer0075dac2015-12-28 09:47:11 -0800323 struct cdns_i2c_regs *regs = i2c_bus->regs;
Siva Durga Prasad Paladugu142cf182019-03-14 09:18:37 +0100324 u32 curr_recv_count;
Moritz Fischer9393f582017-01-16 09:50:46 -0800325 int updatetx, hold_quirk;
Siva Durga Prasad Paladugu07c5a1b2019-03-07 11:52:49 +0100326 u32 ret;
Moritz Fischer0075dac2015-12-28 09:47:11 -0800327
Moritz Fischer9393f582017-01-16 09:50:46 -0800328 curr_recv_count = recv_count;
329
330 /* Check for the message size against the FIFO depth */
Pei Yue Hoca394722023-02-13 00:02:41 -0800331 if (recv_count > i2c_bus->fifo_depth)
Moritz Fischer9393f582017-01-16 09:50:46 -0800332 setbits_le32(&regs->control, CDNS_I2C_CONTROL_HOLD);
333
Moritz Fischer0075dac2015-12-28 09:47:11 -0800334 setbits_le32(&regs->control, CDNS_I2C_CONTROL_CLR_FIFO |
335 CDNS_I2C_CONTROL_RW);
336
Moritz Fischer9393f582017-01-16 09:50:46 -0800337 if (recv_count > CDNS_I2C_TRANSFER_SIZE) {
338 curr_recv_count = CDNS_I2C_TRANSFER_SIZE;
339 writel(curr_recv_count, &regs->transfer_size);
340 } else {
341 writel(recv_count, &regs->transfer_size);
342 }
343
Moritz Fischer0075dac2015-12-28 09:47:11 -0800344 /* Start reading data */
345 writel(addr, &regs->address);
Moritz Fischer9393f582017-01-16 09:50:46 -0800346
347 updatetx = recv_count > curr_recv_count;
348
349 hold_quirk = (i2c_bus->quirks & CDNS_I2C_BROKEN_HOLD_BIT) && updatetx;
Moritz Fischer0075dac2015-12-28 09:47:11 -0800350
Siva Durga Prasad Paladugu07c5a1b2019-03-07 11:52:49 +0100351 while (recv_count && !is_arbitration_lost(regs)) {
Moritz Fischer9393f582017-01-16 09:50:46 -0800352 while (readl(&regs->status) & CDNS_I2C_STATUS_RXDV) {
Pei Yue Hoca394722023-02-13 00:02:41 -0800353 if (recv_count < i2c_bus->fifo_depth &&
Moritz Fischer9393f582017-01-16 09:50:46 -0800354 !i2c_bus->hold_flag) {
355 clrbits_le32(&regs->control,
356 CDNS_I2C_CONTROL_HOLD);
357 }
358 *(cur_data)++ = readl(&regs->data);
359 recv_count--;
360 curr_recv_count--;
361
Pei Yue Hoca394722023-02-13 00:02:41 -0800362 if (cdns_is_hold_quirk(i2c_bus, hold_quirk, curr_recv_count))
Moritz Fischer9393f582017-01-16 09:50:46 -0800363 break;
Moritz Fischer0075dac2015-12-28 09:47:11 -0800364 }
Moritz Fischer0075dac2015-12-28 09:47:11 -0800365
Pei Yue Hoca394722023-02-13 00:02:41 -0800366 if (cdns_is_hold_quirk(i2c_bus, hold_quirk, curr_recv_count)) {
Moritz Fischer9393f582017-01-16 09:50:46 -0800367 /* wait while fifo is full */
368 while (readl(&regs->transfer_size) !=
Pei Yue Hoca394722023-02-13 00:02:41 -0800369 (curr_recv_count - i2c_bus->fifo_depth))
Moritz Fischer9393f582017-01-16 09:50:46 -0800370 ;
371 /*
372 * Check number of bytes to be received against maximum
373 * transfer size and update register accordingly.
374 */
Pei Yue Hoca394722023-02-13 00:02:41 -0800375 if ((recv_count - i2c_bus->fifo_depth) >
Moritz Fischer9393f582017-01-16 09:50:46 -0800376 CDNS_I2C_TRANSFER_SIZE) {
377 writel(CDNS_I2C_TRANSFER_SIZE,
378 &regs->transfer_size);
379 curr_recv_count = CDNS_I2C_TRANSFER_SIZE +
Pei Yue Hoca394722023-02-13 00:02:41 -0800380 i2c_bus->fifo_depth;
Moritz Fischer9393f582017-01-16 09:50:46 -0800381 } else {
Pei Yue Hoca394722023-02-13 00:02:41 -0800382 writel(recv_count - i2c_bus->fifo_depth,
Moritz Fischer9393f582017-01-16 09:50:46 -0800383 &regs->transfer_size);
384 curr_recv_count = recv_count;
385 }
386 } else if (recv_count && !hold_quirk && !curr_recv_count) {
Moritz Fischer9393f582017-01-16 09:50:46 -0800387 if (recv_count > CDNS_I2C_TRANSFER_SIZE) {
388 writel(CDNS_I2C_TRANSFER_SIZE,
389 &regs->transfer_size);
390 curr_recv_count = CDNS_I2C_TRANSFER_SIZE;
391 } else {
392 writel(recv_count, &regs->transfer_size);
393 curr_recv_count = recv_count;
394 }
Sai Pavan Boddubdeeb2b2022-03-01 09:16:50 +0100395 writel(addr, &regs->address);
Moritz Fischer9393f582017-01-16 09:50:46 -0800396 }
397 }
398
399 /* Wait for the address and data to be sent */
Siva Durga Prasad Paladugu07c5a1b2019-03-07 11:52:49 +0100400 ret = cdns_i2c_wait(regs, CDNS_I2C_INTERRUPT_COMP |
401 CDNS_I2C_INTERRUPT_ARBLOST);
402 if (!(ret & (CDNS_I2C_INTERRUPT_ARBLOST |
403 CDNS_I2C_INTERRUPT_COMP)))
Moritz Fischer9393f582017-01-16 09:50:46 -0800404 return -ETIMEDOUT;
Siva Durga Prasad Paladugu07c5a1b2019-03-07 11:52:49 +0100405 if (ret & CDNS_I2C_INTERRUPT_ARBLOST)
406 return -EAGAIN;
Moritz Fischer9393f582017-01-16 09:50:46 -0800407
Moritz Fischer0075dac2015-12-28 09:47:11 -0800408 return 0;
409}
410
411static int cdns_i2c_xfer(struct udevice *dev, struct i2c_msg *msg,
412 int nmsgs)
413{
414 struct i2c_cdns_bus *i2c_bus = dev_get_priv(dev);
Siva Durga Prasad Paladugu07c5a1b2019-03-07 11:52:49 +0100415 int ret = 0;
416 int count;
Moritz Fischer61f06512017-01-16 09:50:44 -0800417 bool hold_quirk;
Siva Durga Prasad Paladugu07c5a1b2019-03-07 11:52:49 +0100418 struct i2c_msg *message = msg;
419 int num_msgs = nmsgs;
Moritz Fischer61f06512017-01-16 09:50:44 -0800420
421 hold_quirk = !!(i2c_bus->quirks & CDNS_I2C_BROKEN_HOLD_BIT);
422
423 if (nmsgs > 1) {
424 /*
425 * This controller does not give completion interrupt after a
426 * master receive message if HOLD bit is set (repeated start),
427 * resulting in SW timeout. Hence, if a receive message is
428 * followed by any other message, an error is returned
429 * indicating that this sequence is not supported.
430 */
431 for (count = 0; (count < nmsgs - 1) && hold_quirk; count++) {
432 if (msg[count].flags & I2C_M_RD) {
433 printf("Can't do repeated start after a receive message\n");
434 return -EOPNOTSUPP;
435 }
436 }
437
438 i2c_bus->hold_flag = 1;
439 setbits_le32(&i2c_bus->regs->control, CDNS_I2C_CONTROL_HOLD);
440 } else {
441 i2c_bus->hold_flag = 0;
442 }
Moritz Fischer0075dac2015-12-28 09:47:11 -0800443
444 debug("i2c_xfer: %d messages\n", nmsgs);
Siva Durga Prasad Paladugu07c5a1b2019-03-07 11:52:49 +0100445 for (u8 retry = 0; retry < CDNS_I2C_ARB_LOST_MAX_RETRIES &&
Andrea Merello81b85122023-05-26 16:56:16 +0200446 nmsgs > 0;) {
Moritz Fischer0075dac2015-12-28 09:47:11 -0800447 debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
448 if (msg->flags & I2C_M_RD) {
449 ret = cdns_i2c_read_data(i2c_bus, msg->addr, msg->buf,
450 msg->len);
451 } else {
452 ret = cdns_i2c_write_data(i2c_bus, msg->addr, msg->buf,
Moritz Fischer61f06512017-01-16 09:50:44 -0800453 msg->len);
Moritz Fischer0075dac2015-12-28 09:47:11 -0800454 }
Siva Durga Prasad Paladugu07c5a1b2019-03-07 11:52:49 +0100455 if (ret == -EAGAIN) {
456 msg = message;
457 nmsgs = num_msgs;
458 retry++;
459 printf("%s,arbitration lost, retrying:%d\n", __func__,
460 retry);
461 continue;
462 }
Andrea Merello81b85122023-05-26 16:56:16 +0200463 nmsgs--;
464 msg++;
Moritz Fischer0075dac2015-12-28 09:47:11 -0800465 if (ret) {
466 debug("i2c_write: error sending\n");
467 return -EREMOTEIO;
468 }
469 }
470
Siva Durga Prasad Paladugu07c5a1b2019-03-07 11:52:49 +0100471 return ret;
Moritz Fischer0075dac2015-12-28 09:47:11 -0800472}
473
Simon Glassaad29ae2020-12-03 16:55:21 -0700474static int cdns_i2c_of_to_plat(struct udevice *dev)
Michal Simek97ba81f2016-04-14 14:15:47 +0200475{
476 struct i2c_cdns_bus *i2c_bus = dev_get_priv(dev);
Moritz Fischer61f06512017-01-16 09:50:44 -0800477 struct cdns_i2c_platform_data *pdata =
478 (struct cdns_i2c_platform_data *)dev_get_driver_data(dev);
Tomasz Gorochowik59e25852019-01-03 13:36:33 +0100479 struct clk clk;
480 int ret;
Michal Simek97ba81f2016-04-14 14:15:47 +0200481
Johan Jonker8d5d8e02023-03-13 01:32:04 +0100482 i2c_bus->regs = dev_read_addr_ptr(dev);
Michal Simek97ba81f2016-04-14 14:15:47 +0200483 if (!i2c_bus->regs)
Johan Jonker8d5d8e02023-03-13 01:32:04 +0100484 return -EINVAL;
Michal Simek97ba81f2016-04-14 14:15:47 +0200485
Moritz Fischer61f06512017-01-16 09:50:44 -0800486 if (pdata)
487 i2c_bus->quirks = pdata->quirks;
488
Tomasz Gorochowik59e25852019-01-03 13:36:33 +0100489 ret = clk_get_by_index(dev, 0, &clk);
490 if (ret)
491 return ret;
492
493 i2c_bus->input_freq = clk_get_rate(&clk);
Michal Simek91429f42016-04-14 14:15:49 +0200494
T Karthik Reddyf8149872021-02-03 03:10:46 -0700495 ret = clk_enable(&clk);
496 if (ret) {
497 dev_err(dev, "failed to enable clock\n");
498 return ret;
499 }
500
Pei Yue Hoca394722023-02-13 00:02:41 -0800501 /* Update FIFO depth based on device tree entry */
502 i2c_bus->fifo_depth = dev_read_u32_default(dev, "fifo-depth",
503 CDNS_I2C_FIFO_DEPTH_DEFAULT);
504
Michal Simek97ba81f2016-04-14 14:15:47 +0200505 return 0;
506}
507
Moritz Fischer0075dac2015-12-28 09:47:11 -0800508static const struct dm_i2c_ops cdns_i2c_ops = {
509 .xfer = cdns_i2c_xfer,
Moritz Fischer0075dac2015-12-28 09:47:11 -0800510 .set_bus_speed = cdns_i2c_set_bus_speed,
511};
512
Moritz Fischer61f06512017-01-16 09:50:44 -0800513static const struct cdns_i2c_platform_data r1p10_i2c_def = {
514 .quirks = CDNS_I2C_BROKEN_HOLD_BIT,
515};
516
Moritz Fischer0075dac2015-12-28 09:47:11 -0800517static const struct udevice_id cdns_i2c_of_match[] = {
Moritz Fischer61f06512017-01-16 09:50:44 -0800518 { .compatible = "cdns,i2c-r1p10", .data = (ulong)&r1p10_i2c_def },
Moritz Fischerf8077762016-12-22 09:36:10 -0800519 { .compatible = "cdns,i2c-r1p14" },
Moritz Fischer0075dac2015-12-28 09:47:11 -0800520 { /* end of table */ }
521};
522
523U_BOOT_DRIVER(cdns_i2c) = {
Michal Simek33093082020-01-07 08:50:34 +0100524 .name = "i2c_cdns",
Moritz Fischer0075dac2015-12-28 09:47:11 -0800525 .id = UCLASS_I2C,
526 .of_match = cdns_i2c_of_match,
Simon Glassaad29ae2020-12-03 16:55:21 -0700527 .of_to_plat = cdns_i2c_of_to_plat,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700528 .priv_auto = sizeof(struct i2c_cdns_bus),
Moritz Fischer0075dac2015-12-28 09:47:11 -0800529 .ops = &cdns_i2c_ops,
530};