blob: 6e5545cd0cd26f7b6f7bf3fa96fa5bb017dc8282 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Vipin KUMAR2403f8f2010-01-15 19:15:44 +05302/*
3 * (C) Copyright 2009
4 * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com.
Vipin KUMAR2403f8f2010-01-15 19:15:44 +05305 */
6
7#include <common.h>
Simon Glassafb88652020-01-23 11:48:06 -07008#include <clk.h>
Stefan Roese334b9b02016-04-21 08:19:41 +02009#include <dm.h>
Stefan Roese678398b2014-10-28 12:12:00 +010010#include <i2c.h>
Stefan Roeseba5da552016-04-21 08:19:42 +020011#include <pci.h>
Dinh Nguyen622597d2018-04-04 17:18:24 -050012#include <reset.h>
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053013#include <asm/io.h>
Vipin KUMAR031ed2f2012-02-26 23:13:29 +000014#include "designware_i2c.h"
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053015
Stefan Roeseb6a77b02016-04-27 09:02:12 +020016#ifdef CONFIG_SYS_I2C_DW_ENABLE_STATUS_UNSUPPORTED
Simon Glass2b5d0292019-02-16 20:24:39 -070017static int dw_i2c_enable(struct i2c_regs *i2c_base, bool enable)
Stefan Roeseb6a77b02016-04-27 09:02:12 +020018{
19 u32 ena = enable ? IC_ENABLE_0B : 0;
20
21 writel(ena, &i2c_base->ic_enable);
Simon Glass2b5d0292019-02-16 20:24:39 -070022
23 return 0;
Stefan Roeseb6a77b02016-04-27 09:02:12 +020024}
25#else
Simon Glass2b5d0292019-02-16 20:24:39 -070026static int dw_i2c_enable(struct i2c_regs *i2c_base, bool enable)
Stefan Roese1c8b0892016-04-21 08:19:38 +020027{
28 u32 ena = enable ? IC_ENABLE_0B : 0;
29 int timeout = 100;
30
31 do {
32 writel(ena, &i2c_base->ic_enable);
33 if ((readl(&i2c_base->ic_enable_status) & IC_ENABLE_0B) == ena)
Simon Glass2b5d0292019-02-16 20:24:39 -070034 return 0;
Stefan Roese1c8b0892016-04-21 08:19:38 +020035
36 /*
37 * Wait 10 times the signaling period of the highest I2C
38 * transfer supported by the driver (for 400KHz this is
39 * 25us) as described in the DesignWare I2C databook.
40 */
41 udelay(25);
42 } while (timeout--);
Stefan Roese1c8b0892016-04-21 08:19:38 +020043 printf("timeout in %sabling I2C adapter\n", enable ? "en" : "dis");
Simon Glass2b5d0292019-02-16 20:24:39 -070044
45 return -ETIMEDOUT;
Stefan Roese1c8b0892016-04-21 08:19:38 +020046}
Stefan Roeseb6a77b02016-04-27 09:02:12 +020047#endif
Stefan Roese1c8b0892016-04-21 08:19:38 +020048
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053049/*
Stefan Roese11b544a2016-04-21 08:19:39 +020050 * i2c_set_bus_speed - Set the i2c speed
51 * @speed: required i2c speed
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053052 *
Stefan Roese11b544a2016-04-21 08:19:39 +020053 * Set the i2c speed.
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053054 */
Simon Glassd22409e2020-01-23 11:48:12 -070055static unsigned int __dw_i2c_set_bus_speed(struct dw_i2c *priv,
56 struct i2c_regs *i2c_base,
Ley Foon Tan2d1e8792019-06-12 09:48:04 +080057 unsigned int speed,
Simon Glassdd3c1602020-01-23 11:48:09 -070058 unsigned int bus_clk)
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053059{
Simon Glassd22409e2020-01-23 11:48:12 -070060 const struct dw_scl_sda_cfg *scl_sda_cfg = NULL;
Simon Glassdd3c1602020-01-23 11:48:09 -070061 ulong bus_khz = bus_clk / 1000;
Simon Glass65190d12020-01-23 11:48:08 -070062 enum i2c_speed_mode i2c_spd;
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053063 unsigned int cntl;
64 unsigned int hcnt, lcnt;
Jun Chene3b93dc2019-06-05 15:23:16 +080065 unsigned int ena;
Stefan Roese11b544a2016-04-21 08:19:39 +020066
Simon Glassd22409e2020-01-23 11:48:12 -070067 if (priv)
68 scl_sda_cfg = priv->scl_sda_cfg;
Simon Glass6db79432020-01-23 11:48:07 -070069 /* Allow high speed if there is no config, or the config allows it */
70 if (speed >= I2C_HIGH_SPEED &&
71 (!scl_sda_cfg || scl_sda_cfg->has_high_speed))
72 i2c_spd = IC_SPEED_MODE_HIGH;
Stefan Roese11b544a2016-04-21 08:19:39 +020073 else if (speed >= I2C_FAST_SPEED)
74 i2c_spd = IC_SPEED_MODE_FAST;
75 else
76 i2c_spd = IC_SPEED_MODE_STANDARD;
Armando Visconti5e3e8dd2012-03-29 20:10:17 +000077
Jun Chene3b93dc2019-06-05 15:23:16 +080078 /* Get enable setting for restore later */
79 ena = readl(&i2c_base->ic_enable) & IC_ENABLE_0B;
80
Armando Visconti5e3e8dd2012-03-29 20:10:17 +000081 /* to set speed cltr must be disabled */
Stefan Roese1c8b0892016-04-21 08:19:38 +020082 dw_i2c_enable(i2c_base, false);
Armando Visconti5e3e8dd2012-03-29 20:10:17 +000083
Stefan Roese678398b2014-10-28 12:12:00 +010084 cntl = (readl(&i2c_base->ic_con) & (~IC_CON_SPD_MSK));
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053085
86 switch (i2c_spd) {
Simon Glass6db79432020-01-23 11:48:07 -070087 case IC_SPEED_MODE_HIGH:
Stefan Roeseba5da552016-04-21 08:19:42 +020088 cntl |= IC_CON_SPD_SS;
89 if (scl_sda_cfg) {
90 hcnt = scl_sda_cfg->fs_hcnt;
91 lcnt = scl_sda_cfg->fs_lcnt;
92 } else {
Simon Glassdd3c1602020-01-23 11:48:09 -070093 hcnt = (bus_khz * MIN_HS_SCL_HIGHTIME) / NANO_TO_KILO;
94 lcnt = (bus_khz * MIN_HS_SCL_LOWTIME) / NANO_TO_KILO;
Stefan Roeseba5da552016-04-21 08:19:42 +020095 }
Stefan Roese678398b2014-10-28 12:12:00 +010096 writel(hcnt, &i2c_base->ic_hs_scl_hcnt);
Stefan Roese678398b2014-10-28 12:12:00 +010097 writel(lcnt, &i2c_base->ic_hs_scl_lcnt);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053098 break;
99
100 case IC_SPEED_MODE_STANDARD:
101 cntl |= IC_CON_SPD_SS;
Stefan Roeseba5da552016-04-21 08:19:42 +0200102 if (scl_sda_cfg) {
103 hcnt = scl_sda_cfg->ss_hcnt;
104 lcnt = scl_sda_cfg->ss_lcnt;
105 } else {
Simon Glassdd3c1602020-01-23 11:48:09 -0700106 hcnt = (bus_khz * MIN_SS_SCL_HIGHTIME) / NANO_TO_KILO;
107 lcnt = (bus_khz * MIN_SS_SCL_LOWTIME) / NANO_TO_KILO;
Stefan Roeseba5da552016-04-21 08:19:42 +0200108 }
Stefan Roese678398b2014-10-28 12:12:00 +0100109 writel(hcnt, &i2c_base->ic_ss_scl_hcnt);
Stefan Roese678398b2014-10-28 12:12:00 +0100110 writel(lcnt, &i2c_base->ic_ss_scl_lcnt);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530111 break;
112
113 case IC_SPEED_MODE_FAST:
114 default:
115 cntl |= IC_CON_SPD_FS;
Stefan Roeseba5da552016-04-21 08:19:42 +0200116 if (scl_sda_cfg) {
117 hcnt = scl_sda_cfg->fs_hcnt;
118 lcnt = scl_sda_cfg->fs_lcnt;
119 } else {
Simon Glassdd3c1602020-01-23 11:48:09 -0700120 hcnt = (bus_khz * MIN_FS_SCL_HIGHTIME) / NANO_TO_KILO;
121 lcnt = (bus_khz * MIN_FS_SCL_LOWTIME) / NANO_TO_KILO;
Stefan Roeseba5da552016-04-21 08:19:42 +0200122 }
Stefan Roese678398b2014-10-28 12:12:00 +0100123 writel(hcnt, &i2c_base->ic_fs_scl_hcnt);
Stefan Roese678398b2014-10-28 12:12:00 +0100124 writel(lcnt, &i2c_base->ic_fs_scl_lcnt);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530125 break;
126 }
127
Stefan Roese678398b2014-10-28 12:12:00 +0100128 writel(cntl, &i2c_base->ic_con);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530129
Stefan Roeseba5da552016-04-21 08:19:42 +0200130 /* Configure SDA Hold Time if required */
131 if (scl_sda_cfg)
132 writel(scl_sda_cfg->sda_hold, &i2c_base->ic_sda_hold);
133
Jun Chene3b93dc2019-06-05 15:23:16 +0800134 /* Restore back i2c now speed set */
135 if (ena == IC_ENABLE_0B)
136 dw_i2c_enable(i2c_base, true);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530137
Stefan Roese3f4358d2016-04-21 08:19:40 +0200138 return 0;
139}
140
141/*
142 * i2c_setaddress - Sets the target slave address
143 * @i2c_addr: target i2c address
144 *
145 * Sets the target slave address.
146 */
147static void i2c_setaddress(struct i2c_regs *i2c_base, unsigned int i2c_addr)
148{
149 /* Disable i2c */
150 dw_i2c_enable(i2c_base, false);
151
152 writel(i2c_addr, &i2c_base->ic_tar);
153
154 /* Enable i2c */
155 dw_i2c_enable(i2c_base, true);
156}
157
158/*
159 * i2c_flush_rxfifo - Flushes the i2c RX FIFO
160 *
161 * Flushes the i2c RX FIFO
162 */
163static void i2c_flush_rxfifo(struct i2c_regs *i2c_base)
164{
165 while (readl(&i2c_base->ic_status) & IC_STATUS_RFNE)
166 readl(&i2c_base->ic_cmd_data);
167}
168
169/*
170 * i2c_wait_for_bb - Waits for bus busy
171 *
172 * Waits for bus busy
173 */
174static int i2c_wait_for_bb(struct i2c_regs *i2c_base)
175{
176 unsigned long start_time_bb = get_timer(0);
177
178 while ((readl(&i2c_base->ic_status) & IC_STATUS_MA) ||
179 !(readl(&i2c_base->ic_status) & IC_STATUS_TFE)) {
180
181 /* Evaluate timeout */
182 if (get_timer(start_time_bb) > (unsigned long)(I2C_BYTE_TO_BB))
183 return 1;
184 }
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530185
186 return 0;
187}
188
Stefan Roese3f4358d2016-04-21 08:19:40 +0200189static int i2c_xfer_init(struct i2c_regs *i2c_base, uchar chip, uint addr,
190 int alen)
191{
192 if (i2c_wait_for_bb(i2c_base))
193 return 1;
194
195 i2c_setaddress(i2c_base, chip);
196 while (alen) {
197 alen--;
198 /* high byte address going out first */
199 writel((addr >> (alen * 8)) & 0xff,
200 &i2c_base->ic_cmd_data);
201 }
202 return 0;
203}
204
205static int i2c_xfer_finish(struct i2c_regs *i2c_base)
206{
207 ulong start_stop_det = get_timer(0);
208
209 while (1) {
210 if ((readl(&i2c_base->ic_raw_intr_stat) & IC_STOP_DET)) {
211 readl(&i2c_base->ic_clr_stop_det);
212 break;
213 } else if (get_timer(start_stop_det) > I2C_STOPDET_TO) {
214 break;
215 }
216 }
217
218 if (i2c_wait_for_bb(i2c_base)) {
219 printf("Timed out waiting for bus\n");
220 return 1;
221 }
222
223 i2c_flush_rxfifo(i2c_base);
224
225 return 0;
226}
227
228/*
229 * i2c_read - Read from i2c memory
230 * @chip: target i2c address
231 * @addr: address to read from
232 * @alen:
233 * @buffer: buffer for read data
234 * @len: no of bytes to be read
235 *
236 * Read from i2c memory.
237 */
238static int __dw_i2c_read(struct i2c_regs *i2c_base, u8 dev, uint addr,
239 int alen, u8 *buffer, int len)
240{
241 unsigned long start_time_rx;
Marek Vasutb0338082016-10-20 16:48:28 +0200242 unsigned int active = 0;
Stefan Roese3f4358d2016-04-21 08:19:40 +0200243
244#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
245 /*
246 * EEPROM chips that implement "address overflow" are ones
247 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
248 * address and the extra bits end up in the "chip address"
249 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
250 * four 256 byte chips.
251 *
252 * Note that we consider the length of the address field to
253 * still be one byte because the extra address bits are
254 * hidden in the chip address.
255 */
256 dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
257 addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
258
259 debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
260 addr);
261#endif
262
263 if (i2c_xfer_init(i2c_base, dev, addr, alen))
264 return 1;
265
266 start_time_rx = get_timer(0);
267 while (len) {
Marek Vasutb0338082016-10-20 16:48:28 +0200268 if (!active) {
269 /*
270 * Avoid writing to ic_cmd_data multiple times
271 * in case this loop spins too quickly and the
272 * ic_status RFNE bit isn't set after the first
273 * write. Subsequent writes to ic_cmd_data can
274 * trigger spurious i2c transfer.
275 */
276 if (len == 1)
277 writel(IC_CMD | IC_STOP, &i2c_base->ic_cmd_data);
278 else
279 writel(IC_CMD, &i2c_base->ic_cmd_data);
280 active = 1;
281 }
Stefan Roese3f4358d2016-04-21 08:19:40 +0200282
283 if (readl(&i2c_base->ic_status) & IC_STATUS_RFNE) {
284 *buffer++ = (uchar)readl(&i2c_base->ic_cmd_data);
285 len--;
286 start_time_rx = get_timer(0);
Marek Vasutb0338082016-10-20 16:48:28 +0200287 active = 0;
Stefan Roese3f4358d2016-04-21 08:19:40 +0200288 } else if (get_timer(start_time_rx) > I2C_BYTE_TO) {
Marek Vasutb0338082016-10-20 16:48:28 +0200289 return 1;
Stefan Roese3f4358d2016-04-21 08:19:40 +0200290 }
291 }
292
293 return i2c_xfer_finish(i2c_base);
294}
295
296/*
297 * i2c_write - Write to i2c memory
298 * @chip: target i2c address
299 * @addr: address to read from
300 * @alen:
301 * @buffer: buffer for read data
302 * @len: no of bytes to be read
303 *
304 * Write to i2c memory.
305 */
306static int __dw_i2c_write(struct i2c_regs *i2c_base, u8 dev, uint addr,
307 int alen, u8 *buffer, int len)
308{
309 int nb = len;
310 unsigned long start_time_tx;
311
312#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
313 /*
314 * EEPROM chips that implement "address overflow" are ones
315 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
316 * address and the extra bits end up in the "chip address"
317 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
318 * four 256 byte chips.
319 *
320 * Note that we consider the length of the address field to
321 * still be one byte because the extra address bits are
322 * hidden in the chip address.
323 */
324 dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
325 addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
326
327 debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
328 addr);
329#endif
330
331 if (i2c_xfer_init(i2c_base, dev, addr, alen))
332 return 1;
333
334 start_time_tx = get_timer(0);
335 while (len) {
336 if (readl(&i2c_base->ic_status) & IC_STATUS_TFNF) {
337 if (--len == 0) {
338 writel(*buffer | IC_STOP,
339 &i2c_base->ic_cmd_data);
340 } else {
341 writel(*buffer, &i2c_base->ic_cmd_data);
342 }
343 buffer++;
344 start_time_tx = get_timer(0);
345
346 } else if (get_timer(start_time_tx) > (nb * I2C_BYTE_TO)) {
347 printf("Timed out. i2c write Failed\n");
348 return 1;
349 }
350 }
351
352 return i2c_xfer_finish(i2c_base);
353}
354
Stefan Roese334b9b02016-04-21 08:19:41 +0200355/*
356 * __dw_i2c_init - Init function
357 * @speed: required i2c speed
358 * @slaveaddr: slave address for the device
359 *
360 * Initialization function.
361 */
Simon Glass2b5d0292019-02-16 20:24:39 -0700362static int __dw_i2c_init(struct i2c_regs *i2c_base, int speed, int slaveaddr)
Stefan Roese334b9b02016-04-21 08:19:41 +0200363{
Simon Glass2b5d0292019-02-16 20:24:39 -0700364 int ret;
365
Stefan Roese334b9b02016-04-21 08:19:41 +0200366 /* Disable i2c */
Simon Glass2b5d0292019-02-16 20:24:39 -0700367 ret = dw_i2c_enable(i2c_base, false);
368 if (ret)
369 return ret;
Stefan Roese334b9b02016-04-21 08:19:41 +0200370
Marek Vasut014e47f2017-08-07 20:45:31 +0200371 writel(IC_CON_SD | IC_CON_RE | IC_CON_SPD_FS | IC_CON_MM,
372 &i2c_base->ic_con);
Stefan Roese334b9b02016-04-21 08:19:41 +0200373 writel(IC_RX_TL, &i2c_base->ic_rx_tl);
374 writel(IC_TX_TL, &i2c_base->ic_tx_tl);
375 writel(IC_STOP_DET, &i2c_base->ic_intr_mask);
376#ifndef CONFIG_DM_I2C
Simon Glassd22409e2020-01-23 11:48:12 -0700377 __dw_i2c_set_bus_speed(NULL, i2c_base, speed, IC_CLK);
Stefan Roese334b9b02016-04-21 08:19:41 +0200378 writel(slaveaddr, &i2c_base->ic_sar);
379#endif
380
381 /* Enable i2c */
Simon Glass2b5d0292019-02-16 20:24:39 -0700382 ret = dw_i2c_enable(i2c_base, true);
383 if (ret)
384 return ret;
385
386 return 0;
Stefan Roese334b9b02016-04-21 08:19:41 +0200387}
388
389#ifndef CONFIG_DM_I2C
390/*
391 * The legacy I2C functions. These need to get removed once
392 * all users of this driver are converted to DM.
393 */
Stefan Roese3f4358d2016-04-21 08:19:40 +0200394static struct i2c_regs *i2c_get_base(struct i2c_adapter *adap)
395{
396 switch (adap->hwadapnr) {
397#if CONFIG_SYS_I2C_BUS_MAX >= 4
398 case 3:
399 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE3;
400#endif
401#if CONFIG_SYS_I2C_BUS_MAX >= 3
402 case 2:
403 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE2;
404#endif
405#if CONFIG_SYS_I2C_BUS_MAX >= 2
406 case 1:
407 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE1;
408#endif
409 case 0:
410 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE;
411 default:
412 printf("Wrong I2C-adapter number %d\n", adap->hwadapnr);
413 }
414
415 return NULL;
416}
417
418static unsigned int dw_i2c_set_bus_speed(struct i2c_adapter *adap,
419 unsigned int speed)
420{
421 adap->speed = speed;
Simon Glassd22409e2020-01-23 11:48:12 -0700422 return __dw_i2c_set_bus_speed(NULL, i2c_get_base(adap), speed, IC_CLK);
Stefan Roese3f4358d2016-04-21 08:19:40 +0200423}
424
Stefan Roese334b9b02016-04-21 08:19:41 +0200425static void dw_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530426{
Stefan Roese334b9b02016-04-21 08:19:41 +0200427 __dw_i2c_init(i2c_get_base(adap), speed, slaveaddr);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530428}
429
Stefan Roese678398b2014-10-28 12:12:00 +0100430static int dw_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr,
431 int alen, u8 *buffer, int len)
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530432{
Stefan Roese3f4358d2016-04-21 08:19:40 +0200433 return __dw_i2c_read(i2c_get_base(adap), dev, addr, alen, buffer, len);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530434}
435
Stefan Roese678398b2014-10-28 12:12:00 +0100436static int dw_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr,
437 int alen, u8 *buffer, int len)
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530438{
Stefan Roese3f4358d2016-04-21 08:19:40 +0200439 return __dw_i2c_write(i2c_get_base(adap), dev, addr, alen, buffer, len);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530440}
441
Stefan Roese334b9b02016-04-21 08:19:41 +0200442/* dw_i2c_probe - Probe the i2c chip */
Stefan Roese678398b2014-10-28 12:12:00 +0100443static int dw_i2c_probe(struct i2c_adapter *adap, u8 dev)
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530444{
Stefan Roese3f4358d2016-04-21 08:19:40 +0200445 struct i2c_regs *i2c_base = i2c_get_base(adap);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530446 u32 tmp;
Stefan Roese496ba482012-01-20 11:52:33 +0100447 int ret;
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530448
449 /*
450 * Try to read the first location of the chip.
451 */
Stefan Roese3f4358d2016-04-21 08:19:40 +0200452 ret = __dw_i2c_read(i2c_base, dev, 0, 1, (uchar *)&tmp, 1);
Stefan Roese496ba482012-01-20 11:52:33 +0100453 if (ret)
Stefan Roese678398b2014-10-28 12:12:00 +0100454 dw_i2c_init(adap, adap->speed, adap->slaveaddr);
Stefan Roese496ba482012-01-20 11:52:33 +0100455
456 return ret;
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530457}
Armando Viscontiac6e2fe2012-12-06 00:04:15 +0000458
Stefan Roese678398b2014-10-28 12:12:00 +0100459U_BOOT_I2C_ADAP_COMPLETE(dw_0, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
460 dw_i2c_write, dw_i2c_set_bus_speed,
461 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0)
Armando Viscontiac6e2fe2012-12-06 00:04:15 +0000462
Stefan Roese678398b2014-10-28 12:12:00 +0100463#if CONFIG_SYS_I2C_BUS_MAX >= 2
464U_BOOT_I2C_ADAP_COMPLETE(dw_1, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
465 dw_i2c_write, dw_i2c_set_bus_speed,
466 CONFIG_SYS_I2C_SPEED1, CONFIG_SYS_I2C_SLAVE1, 1)
467#endif
Armando Viscontiac6e2fe2012-12-06 00:04:15 +0000468
Stefan Roese678398b2014-10-28 12:12:00 +0100469#if CONFIG_SYS_I2C_BUS_MAX >= 3
470U_BOOT_I2C_ADAP_COMPLETE(dw_2, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
471 dw_i2c_write, dw_i2c_set_bus_speed,
472 CONFIG_SYS_I2C_SPEED2, CONFIG_SYS_I2C_SLAVE2, 2)
473#endif
Armando Viscontiac6e2fe2012-12-06 00:04:15 +0000474
Stefan Roese678398b2014-10-28 12:12:00 +0100475#if CONFIG_SYS_I2C_BUS_MAX >= 4
476U_BOOT_I2C_ADAP_COMPLETE(dw_3, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
477 dw_i2c_write, dw_i2c_set_bus_speed,
478 CONFIG_SYS_I2C_SPEED3, CONFIG_SYS_I2C_SLAVE3, 3)
Armando Viscontiac6e2fe2012-12-06 00:04:15 +0000479#endif
Stefan Roese334b9b02016-04-21 08:19:41 +0200480
481#else /* CONFIG_DM_I2C */
482/* The DM I2C functions */
483
484static int designware_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
485 int nmsgs)
486{
487 struct dw_i2c *i2c = dev_get_priv(bus);
488 int ret;
489
490 debug("i2c_xfer: %d messages\n", nmsgs);
491 for (; nmsgs > 0; nmsgs--, msg++) {
492 debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
493 if (msg->flags & I2C_M_RD) {
494 ret = __dw_i2c_read(i2c->regs, msg->addr, 0, 0,
495 msg->buf, msg->len);
496 } else {
497 ret = __dw_i2c_write(i2c->regs, msg->addr, 0, 0,
498 msg->buf, msg->len);
499 }
500 if (ret) {
501 debug("i2c_write: error sending\n");
502 return -EREMOTEIO;
503 }
504 }
505
506 return 0;
507}
508
509static int designware_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
510{
511 struct dw_i2c *i2c = dev_get_priv(bus);
Ley Foon Tan2d1e8792019-06-12 09:48:04 +0800512 ulong rate;
Stefan Roese334b9b02016-04-21 08:19:41 +0200513
Ley Foon Tan2d1e8792019-06-12 09:48:04 +0800514#if CONFIG_IS_ENABLED(CLK)
515 rate = clk_get_rate(&i2c->clk);
516 if (IS_ERR_VALUE(rate))
517 return -EINVAL;
Ley Foon Tan2d1e8792019-06-12 09:48:04 +0800518#else
519 rate = IC_CLK;
520#endif
Simon Glassd22409e2020-01-23 11:48:12 -0700521 return __dw_i2c_set_bus_speed(i2c, i2c->regs, speed, rate);
Stefan Roese334b9b02016-04-21 08:19:41 +0200522}
523
524static int designware_i2c_probe_chip(struct udevice *bus, uint chip_addr,
525 uint chip_flags)
526{
527 struct dw_i2c *i2c = dev_get_priv(bus);
528 struct i2c_regs *i2c_base = i2c->regs;
529 u32 tmp;
530 int ret;
531
532 /* Try to read the first location of the chip */
533 ret = __dw_i2c_read(i2c_base, chip_addr, 0, 1, (uchar *)&tmp, 1);
534 if (ret)
535 __dw_i2c_init(i2c_base, 0, 0);
536
537 return ret;
538}
539
Simon Glass80a03db2020-01-23 11:48:11 -0700540int designware_i2c_ofdata_to_platdata(struct udevice *bus)
Simon Glass457df232019-12-06 21:41:40 -0700541{
542 struct dw_i2c *priv = dev_get_priv(bus);
543
Simon Glass80a03db2020-01-23 11:48:11 -0700544 if (!priv->regs)
545 priv->regs = (struct i2c_regs *)devfdt_get_addr_ptr(bus);
546 dev_read_u32(bus, "i2c-scl-rising-time-ns", &priv->scl_rise_time_ns);
547 dev_read_u32(bus, "i2c-scl-falling-time-ns", &priv->scl_fall_time_ns);
548 dev_read_u32(bus, "i2c-sda-hold-time-ns", &priv->sda_hold_time_ns);
Simon Glass457df232019-12-06 21:41:40 -0700549
550 return 0;
551}
552
553int designware_i2c_probe(struct udevice *bus)
Stefan Roese334b9b02016-04-21 08:19:41 +0200554{
555 struct dw_i2c *priv = dev_get_priv(bus);
Dinh Nguyen622597d2018-04-04 17:18:24 -0500556 int ret;
Stefan Roese334b9b02016-04-21 08:19:41 +0200557
Simon Goldschmidt36821b32019-03-28 21:11:48 +0100558 ret = reset_get_bulk(bus, &priv->resets);
Dinh Nguyen622597d2018-04-04 17:18:24 -0500559 if (ret)
Simon Goldschmidt36821b32019-03-28 21:11:48 +0100560 dev_warn(bus, "Can't get reset: %d\n", ret);
561 else
562 reset_deassert_bulk(&priv->resets);
Dinh Nguyen622597d2018-04-04 17:18:24 -0500563
Ley Foon Tan2d1e8792019-06-12 09:48:04 +0800564#if CONFIG_IS_ENABLED(CLK)
565 ret = clk_get_by_index(bus, 0, &priv->clk);
566 if (ret)
567 return ret;
568
569 ret = clk_enable(&priv->clk);
570 if (ret && ret != -ENOSYS && ret != -ENOTSUPP) {
571 clk_free(&priv->clk);
572 dev_err(bus, "failed to enable clock\n");
573 return ret;
574 }
575#endif
576
Simon Glass2b5d0292019-02-16 20:24:39 -0700577 return __dw_i2c_init(priv->regs, 0, 0);
Stefan Roese334b9b02016-04-21 08:19:41 +0200578}
579
Simon Glass457df232019-12-06 21:41:40 -0700580int designware_i2c_remove(struct udevice *dev)
Simon Goldschmidt36821b32019-03-28 21:11:48 +0100581{
582 struct dw_i2c *priv = dev_get_priv(dev);
583
Ley Foon Tan2d1e8792019-06-12 09:48:04 +0800584#if CONFIG_IS_ENABLED(CLK)
585 clk_disable(&priv->clk);
586 clk_free(&priv->clk);
587#endif
588
Simon Goldschmidt36821b32019-03-28 21:11:48 +0100589 return reset_release_bulk(&priv->resets);
590}
591
Simon Glass457df232019-12-06 21:41:40 -0700592const struct dm_i2c_ops designware_i2c_ops = {
Stefan Roese334b9b02016-04-21 08:19:41 +0200593 .xfer = designware_i2c_xfer,
594 .probe_chip = designware_i2c_probe_chip,
595 .set_bus_speed = designware_i2c_set_bus_speed,
596};
597
598static const struct udevice_id designware_i2c_ids[] = {
599 { .compatible = "snps,designware-i2c" },
600 { }
601};
602
603U_BOOT_DRIVER(i2c_designware) = {
604 .name = "i2c_designware",
605 .id = UCLASS_I2C,
606 .of_match = designware_i2c_ids,
Simon Glass457df232019-12-06 21:41:40 -0700607 .ofdata_to_platdata = designware_i2c_ofdata_to_platdata,
Stefan Roese334b9b02016-04-21 08:19:41 +0200608 .probe = designware_i2c_probe,
609 .priv_auto_alloc_size = sizeof(struct dw_i2c),
Simon Goldschmidt36821b32019-03-28 21:11:48 +0100610 .remove = designware_i2c_remove,
Simon Glass457df232019-12-06 21:41:40 -0700611 .flags = DM_FLAG_OS_PREPARE,
Stefan Roese334b9b02016-04-21 08:19:41 +0200612 .ops = &designware_i2c_ops,
613};
614
615#endif /* CONFIG_DM_I2C */