blob: 2416ef32f9fac8a69f9d91c5a08dc862d82134a3 [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 */
Stefan Roese3f4358d2016-04-21 08:19:40 +020055static unsigned int __dw_i2c_set_bus_speed(struct i2c_regs *i2c_base,
Stefan Roeseba5da552016-04-21 08:19:42 +020056 struct dw_scl_sda_cfg *scl_sda_cfg,
Ley Foon Tan2d1e8792019-06-12 09:48:04 +080057 unsigned int speed,
58 unsigned int bus_mhz)
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053059{
Simon Glass65190d12020-01-23 11:48:08 -070060 enum i2c_speed_mode i2c_spd;
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053061 unsigned int cntl;
62 unsigned int hcnt, lcnt;
Jun Chene3b93dc2019-06-05 15:23:16 +080063 unsigned int ena;
Stefan Roese11b544a2016-04-21 08:19:39 +020064
Simon Glass6db79432020-01-23 11:48:07 -070065 /* Allow high speed if there is no config, or the config allows it */
66 if (speed >= I2C_HIGH_SPEED &&
67 (!scl_sda_cfg || scl_sda_cfg->has_high_speed))
68 i2c_spd = IC_SPEED_MODE_HIGH;
Stefan Roese11b544a2016-04-21 08:19:39 +020069 else if (speed >= I2C_FAST_SPEED)
70 i2c_spd = IC_SPEED_MODE_FAST;
71 else
72 i2c_spd = IC_SPEED_MODE_STANDARD;
Armando Visconti5e3e8dd2012-03-29 20:10:17 +000073
Jun Chene3b93dc2019-06-05 15:23:16 +080074 /* Get enable setting for restore later */
75 ena = readl(&i2c_base->ic_enable) & IC_ENABLE_0B;
76
Armando Visconti5e3e8dd2012-03-29 20:10:17 +000077 /* to set speed cltr must be disabled */
Stefan Roese1c8b0892016-04-21 08:19:38 +020078 dw_i2c_enable(i2c_base, false);
Armando Visconti5e3e8dd2012-03-29 20:10:17 +000079
Stefan Roese678398b2014-10-28 12:12:00 +010080 cntl = (readl(&i2c_base->ic_con) & (~IC_CON_SPD_MSK));
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053081
82 switch (i2c_spd) {
Simon Glass6db79432020-01-23 11:48:07 -070083 case IC_SPEED_MODE_HIGH:
Stefan Roeseba5da552016-04-21 08:19:42 +020084 cntl |= IC_CON_SPD_SS;
85 if (scl_sda_cfg) {
86 hcnt = scl_sda_cfg->fs_hcnt;
87 lcnt = scl_sda_cfg->fs_lcnt;
88 } else {
Ley Foon Tan2d1e8792019-06-12 09:48:04 +080089 hcnt = (bus_mhz * MIN_HS_SCL_HIGHTIME) / NANO_TO_MICRO;
90 lcnt = (bus_mhz * MIN_HS_SCL_LOWTIME) / NANO_TO_MICRO;
Stefan Roeseba5da552016-04-21 08:19:42 +020091 }
Stefan Roese678398b2014-10-28 12:12:00 +010092 writel(hcnt, &i2c_base->ic_hs_scl_hcnt);
Stefan Roese678398b2014-10-28 12:12:00 +010093 writel(lcnt, &i2c_base->ic_hs_scl_lcnt);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053094 break;
95
96 case IC_SPEED_MODE_STANDARD:
97 cntl |= IC_CON_SPD_SS;
Stefan Roeseba5da552016-04-21 08:19:42 +020098 if (scl_sda_cfg) {
99 hcnt = scl_sda_cfg->ss_hcnt;
100 lcnt = scl_sda_cfg->ss_lcnt;
101 } else {
Ley Foon Tan2d1e8792019-06-12 09:48:04 +0800102 hcnt = (bus_mhz * MIN_SS_SCL_HIGHTIME) / NANO_TO_MICRO;
103 lcnt = (bus_mhz * MIN_SS_SCL_LOWTIME) / NANO_TO_MICRO;
Stefan Roeseba5da552016-04-21 08:19:42 +0200104 }
Stefan Roese678398b2014-10-28 12:12:00 +0100105 writel(hcnt, &i2c_base->ic_ss_scl_hcnt);
Stefan Roese678398b2014-10-28 12:12:00 +0100106 writel(lcnt, &i2c_base->ic_ss_scl_lcnt);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530107 break;
108
109 case IC_SPEED_MODE_FAST:
110 default:
111 cntl |= IC_CON_SPD_FS;
Stefan Roeseba5da552016-04-21 08:19:42 +0200112 if (scl_sda_cfg) {
113 hcnt = scl_sda_cfg->fs_hcnt;
114 lcnt = scl_sda_cfg->fs_lcnt;
115 } else {
Ley Foon Tan2d1e8792019-06-12 09:48:04 +0800116 hcnt = (bus_mhz * MIN_FS_SCL_HIGHTIME) / NANO_TO_MICRO;
117 lcnt = (bus_mhz * MIN_FS_SCL_LOWTIME) / NANO_TO_MICRO;
Stefan Roeseba5da552016-04-21 08:19:42 +0200118 }
Stefan Roese678398b2014-10-28 12:12:00 +0100119 writel(hcnt, &i2c_base->ic_fs_scl_hcnt);
Stefan Roese678398b2014-10-28 12:12:00 +0100120 writel(lcnt, &i2c_base->ic_fs_scl_lcnt);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530121 break;
122 }
123
Stefan Roese678398b2014-10-28 12:12:00 +0100124 writel(cntl, &i2c_base->ic_con);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530125
Stefan Roeseba5da552016-04-21 08:19:42 +0200126 /* Configure SDA Hold Time if required */
127 if (scl_sda_cfg)
128 writel(scl_sda_cfg->sda_hold, &i2c_base->ic_sda_hold);
129
Jun Chene3b93dc2019-06-05 15:23:16 +0800130 /* Restore back i2c now speed set */
131 if (ena == IC_ENABLE_0B)
132 dw_i2c_enable(i2c_base, true);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530133
Stefan Roese3f4358d2016-04-21 08:19:40 +0200134 return 0;
135}
136
137/*
138 * i2c_setaddress - Sets the target slave address
139 * @i2c_addr: target i2c address
140 *
141 * Sets the target slave address.
142 */
143static void i2c_setaddress(struct i2c_regs *i2c_base, unsigned int i2c_addr)
144{
145 /* Disable i2c */
146 dw_i2c_enable(i2c_base, false);
147
148 writel(i2c_addr, &i2c_base->ic_tar);
149
150 /* Enable i2c */
151 dw_i2c_enable(i2c_base, true);
152}
153
154/*
155 * i2c_flush_rxfifo - Flushes the i2c RX FIFO
156 *
157 * Flushes the i2c RX FIFO
158 */
159static void i2c_flush_rxfifo(struct i2c_regs *i2c_base)
160{
161 while (readl(&i2c_base->ic_status) & IC_STATUS_RFNE)
162 readl(&i2c_base->ic_cmd_data);
163}
164
165/*
166 * i2c_wait_for_bb - Waits for bus busy
167 *
168 * Waits for bus busy
169 */
170static int i2c_wait_for_bb(struct i2c_regs *i2c_base)
171{
172 unsigned long start_time_bb = get_timer(0);
173
174 while ((readl(&i2c_base->ic_status) & IC_STATUS_MA) ||
175 !(readl(&i2c_base->ic_status) & IC_STATUS_TFE)) {
176
177 /* Evaluate timeout */
178 if (get_timer(start_time_bb) > (unsigned long)(I2C_BYTE_TO_BB))
179 return 1;
180 }
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530181
182 return 0;
183}
184
Stefan Roese3f4358d2016-04-21 08:19:40 +0200185static int i2c_xfer_init(struct i2c_regs *i2c_base, uchar chip, uint addr,
186 int alen)
187{
188 if (i2c_wait_for_bb(i2c_base))
189 return 1;
190
191 i2c_setaddress(i2c_base, chip);
192 while (alen) {
193 alen--;
194 /* high byte address going out first */
195 writel((addr >> (alen * 8)) & 0xff,
196 &i2c_base->ic_cmd_data);
197 }
198 return 0;
199}
200
201static int i2c_xfer_finish(struct i2c_regs *i2c_base)
202{
203 ulong start_stop_det = get_timer(0);
204
205 while (1) {
206 if ((readl(&i2c_base->ic_raw_intr_stat) & IC_STOP_DET)) {
207 readl(&i2c_base->ic_clr_stop_det);
208 break;
209 } else if (get_timer(start_stop_det) > I2C_STOPDET_TO) {
210 break;
211 }
212 }
213
214 if (i2c_wait_for_bb(i2c_base)) {
215 printf("Timed out waiting for bus\n");
216 return 1;
217 }
218
219 i2c_flush_rxfifo(i2c_base);
220
221 return 0;
222}
223
224/*
225 * i2c_read - Read from i2c memory
226 * @chip: target i2c address
227 * @addr: address to read from
228 * @alen:
229 * @buffer: buffer for read data
230 * @len: no of bytes to be read
231 *
232 * Read from i2c memory.
233 */
234static int __dw_i2c_read(struct i2c_regs *i2c_base, u8 dev, uint addr,
235 int alen, u8 *buffer, int len)
236{
237 unsigned long start_time_rx;
Marek Vasutb0338082016-10-20 16:48:28 +0200238 unsigned int active = 0;
Stefan Roese3f4358d2016-04-21 08:19:40 +0200239
240#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
241 /*
242 * EEPROM chips that implement "address overflow" are ones
243 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
244 * address and the extra bits end up in the "chip address"
245 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
246 * four 256 byte chips.
247 *
248 * Note that we consider the length of the address field to
249 * still be one byte because the extra address bits are
250 * hidden in the chip address.
251 */
252 dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
253 addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
254
255 debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
256 addr);
257#endif
258
259 if (i2c_xfer_init(i2c_base, dev, addr, alen))
260 return 1;
261
262 start_time_rx = get_timer(0);
263 while (len) {
Marek Vasutb0338082016-10-20 16:48:28 +0200264 if (!active) {
265 /*
266 * Avoid writing to ic_cmd_data multiple times
267 * in case this loop spins too quickly and the
268 * ic_status RFNE bit isn't set after the first
269 * write. Subsequent writes to ic_cmd_data can
270 * trigger spurious i2c transfer.
271 */
272 if (len == 1)
273 writel(IC_CMD | IC_STOP, &i2c_base->ic_cmd_data);
274 else
275 writel(IC_CMD, &i2c_base->ic_cmd_data);
276 active = 1;
277 }
Stefan Roese3f4358d2016-04-21 08:19:40 +0200278
279 if (readl(&i2c_base->ic_status) & IC_STATUS_RFNE) {
280 *buffer++ = (uchar)readl(&i2c_base->ic_cmd_data);
281 len--;
282 start_time_rx = get_timer(0);
Marek Vasutb0338082016-10-20 16:48:28 +0200283 active = 0;
Stefan Roese3f4358d2016-04-21 08:19:40 +0200284 } else if (get_timer(start_time_rx) > I2C_BYTE_TO) {
Marek Vasutb0338082016-10-20 16:48:28 +0200285 return 1;
Stefan Roese3f4358d2016-04-21 08:19:40 +0200286 }
287 }
288
289 return i2c_xfer_finish(i2c_base);
290}
291
292/*
293 * i2c_write - Write to i2c memory
294 * @chip: target i2c address
295 * @addr: address to read from
296 * @alen:
297 * @buffer: buffer for read data
298 * @len: no of bytes to be read
299 *
300 * Write to i2c memory.
301 */
302static int __dw_i2c_write(struct i2c_regs *i2c_base, u8 dev, uint addr,
303 int alen, u8 *buffer, int len)
304{
305 int nb = len;
306 unsigned long start_time_tx;
307
308#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
309 /*
310 * EEPROM chips that implement "address overflow" are ones
311 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
312 * address and the extra bits end up in the "chip address"
313 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
314 * four 256 byte chips.
315 *
316 * Note that we consider the length of the address field to
317 * still be one byte because the extra address bits are
318 * hidden in the chip address.
319 */
320 dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
321 addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
322
323 debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
324 addr);
325#endif
326
327 if (i2c_xfer_init(i2c_base, dev, addr, alen))
328 return 1;
329
330 start_time_tx = get_timer(0);
331 while (len) {
332 if (readl(&i2c_base->ic_status) & IC_STATUS_TFNF) {
333 if (--len == 0) {
334 writel(*buffer | IC_STOP,
335 &i2c_base->ic_cmd_data);
336 } else {
337 writel(*buffer, &i2c_base->ic_cmd_data);
338 }
339 buffer++;
340 start_time_tx = get_timer(0);
341
342 } else if (get_timer(start_time_tx) > (nb * I2C_BYTE_TO)) {
343 printf("Timed out. i2c write Failed\n");
344 return 1;
345 }
346 }
347
348 return i2c_xfer_finish(i2c_base);
349}
350
Stefan Roese334b9b02016-04-21 08:19:41 +0200351/*
352 * __dw_i2c_init - Init function
353 * @speed: required i2c speed
354 * @slaveaddr: slave address for the device
355 *
356 * Initialization function.
357 */
Simon Glass2b5d0292019-02-16 20:24:39 -0700358static int __dw_i2c_init(struct i2c_regs *i2c_base, int speed, int slaveaddr)
Stefan Roese334b9b02016-04-21 08:19:41 +0200359{
Simon Glass2b5d0292019-02-16 20:24:39 -0700360 int ret;
361
Stefan Roese334b9b02016-04-21 08:19:41 +0200362 /* Disable i2c */
Simon Glass2b5d0292019-02-16 20:24:39 -0700363 ret = dw_i2c_enable(i2c_base, false);
364 if (ret)
365 return ret;
Stefan Roese334b9b02016-04-21 08:19:41 +0200366
Marek Vasut014e47f2017-08-07 20:45:31 +0200367 writel(IC_CON_SD | IC_CON_RE | IC_CON_SPD_FS | IC_CON_MM,
368 &i2c_base->ic_con);
Stefan Roese334b9b02016-04-21 08:19:41 +0200369 writel(IC_RX_TL, &i2c_base->ic_rx_tl);
370 writel(IC_TX_TL, &i2c_base->ic_tx_tl);
371 writel(IC_STOP_DET, &i2c_base->ic_intr_mask);
372#ifndef CONFIG_DM_I2C
Ley Foon Tan2d1e8792019-06-12 09:48:04 +0800373 __dw_i2c_set_bus_speed(i2c_base, NULL, speed, IC_CLK);
Stefan Roese334b9b02016-04-21 08:19:41 +0200374 writel(slaveaddr, &i2c_base->ic_sar);
375#endif
376
377 /* Enable i2c */
Simon Glass2b5d0292019-02-16 20:24:39 -0700378 ret = dw_i2c_enable(i2c_base, true);
379 if (ret)
380 return ret;
381
382 return 0;
Stefan Roese334b9b02016-04-21 08:19:41 +0200383}
384
385#ifndef CONFIG_DM_I2C
386/*
387 * The legacy I2C functions. These need to get removed once
388 * all users of this driver are converted to DM.
389 */
Stefan Roese3f4358d2016-04-21 08:19:40 +0200390static struct i2c_regs *i2c_get_base(struct i2c_adapter *adap)
391{
392 switch (adap->hwadapnr) {
393#if CONFIG_SYS_I2C_BUS_MAX >= 4
394 case 3:
395 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE3;
396#endif
397#if CONFIG_SYS_I2C_BUS_MAX >= 3
398 case 2:
399 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE2;
400#endif
401#if CONFIG_SYS_I2C_BUS_MAX >= 2
402 case 1:
403 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE1;
404#endif
405 case 0:
406 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE;
407 default:
408 printf("Wrong I2C-adapter number %d\n", adap->hwadapnr);
409 }
410
411 return NULL;
412}
413
414static unsigned int dw_i2c_set_bus_speed(struct i2c_adapter *adap,
415 unsigned int speed)
416{
417 adap->speed = speed;
Ley Foon Tan2d1e8792019-06-12 09:48:04 +0800418 return __dw_i2c_set_bus_speed(i2c_get_base(adap), NULL, speed, IC_CLK);
Stefan Roese3f4358d2016-04-21 08:19:40 +0200419}
420
Stefan Roese334b9b02016-04-21 08:19:41 +0200421static void dw_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530422{
Stefan Roese334b9b02016-04-21 08:19:41 +0200423 __dw_i2c_init(i2c_get_base(adap), speed, slaveaddr);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530424}
425
Stefan Roese678398b2014-10-28 12:12:00 +0100426static int dw_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr,
427 int alen, u8 *buffer, int len)
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530428{
Stefan Roese3f4358d2016-04-21 08:19:40 +0200429 return __dw_i2c_read(i2c_get_base(adap), dev, addr, alen, buffer, len);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530430}
431
Stefan Roese678398b2014-10-28 12:12:00 +0100432static int dw_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr,
433 int alen, u8 *buffer, int len)
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530434{
Stefan Roese3f4358d2016-04-21 08:19:40 +0200435 return __dw_i2c_write(i2c_get_base(adap), dev, addr, alen, buffer, len);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530436}
437
Stefan Roese334b9b02016-04-21 08:19:41 +0200438/* dw_i2c_probe - Probe the i2c chip */
Stefan Roese678398b2014-10-28 12:12:00 +0100439static int dw_i2c_probe(struct i2c_adapter *adap, u8 dev)
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530440{
Stefan Roese3f4358d2016-04-21 08:19:40 +0200441 struct i2c_regs *i2c_base = i2c_get_base(adap);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530442 u32 tmp;
Stefan Roese496ba482012-01-20 11:52:33 +0100443 int ret;
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530444
445 /*
446 * Try to read the first location of the chip.
447 */
Stefan Roese3f4358d2016-04-21 08:19:40 +0200448 ret = __dw_i2c_read(i2c_base, dev, 0, 1, (uchar *)&tmp, 1);
Stefan Roese496ba482012-01-20 11:52:33 +0100449 if (ret)
Stefan Roese678398b2014-10-28 12:12:00 +0100450 dw_i2c_init(adap, adap->speed, adap->slaveaddr);
Stefan Roese496ba482012-01-20 11:52:33 +0100451
452 return ret;
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530453}
Armando Viscontiac6e2fe2012-12-06 00:04:15 +0000454
Stefan Roese678398b2014-10-28 12:12:00 +0100455U_BOOT_I2C_ADAP_COMPLETE(dw_0, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
456 dw_i2c_write, dw_i2c_set_bus_speed,
457 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0)
Armando Viscontiac6e2fe2012-12-06 00:04:15 +0000458
Stefan Roese678398b2014-10-28 12:12:00 +0100459#if CONFIG_SYS_I2C_BUS_MAX >= 2
460U_BOOT_I2C_ADAP_COMPLETE(dw_1, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
461 dw_i2c_write, dw_i2c_set_bus_speed,
462 CONFIG_SYS_I2C_SPEED1, CONFIG_SYS_I2C_SLAVE1, 1)
463#endif
Armando Viscontiac6e2fe2012-12-06 00:04:15 +0000464
Stefan Roese678398b2014-10-28 12:12:00 +0100465#if CONFIG_SYS_I2C_BUS_MAX >= 3
466U_BOOT_I2C_ADAP_COMPLETE(dw_2, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
467 dw_i2c_write, dw_i2c_set_bus_speed,
468 CONFIG_SYS_I2C_SPEED2, CONFIG_SYS_I2C_SLAVE2, 2)
469#endif
Armando Viscontiac6e2fe2012-12-06 00:04:15 +0000470
Stefan Roese678398b2014-10-28 12:12:00 +0100471#if CONFIG_SYS_I2C_BUS_MAX >= 4
472U_BOOT_I2C_ADAP_COMPLETE(dw_3, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
473 dw_i2c_write, dw_i2c_set_bus_speed,
474 CONFIG_SYS_I2C_SPEED3, CONFIG_SYS_I2C_SLAVE3, 3)
Armando Viscontiac6e2fe2012-12-06 00:04:15 +0000475#endif
Stefan Roese334b9b02016-04-21 08:19:41 +0200476
477#else /* CONFIG_DM_I2C */
478/* The DM I2C functions */
479
480static int designware_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
481 int nmsgs)
482{
483 struct dw_i2c *i2c = dev_get_priv(bus);
484 int ret;
485
486 debug("i2c_xfer: %d messages\n", nmsgs);
487 for (; nmsgs > 0; nmsgs--, msg++) {
488 debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
489 if (msg->flags & I2C_M_RD) {
490 ret = __dw_i2c_read(i2c->regs, msg->addr, 0, 0,
491 msg->buf, msg->len);
492 } else {
493 ret = __dw_i2c_write(i2c->regs, msg->addr, 0, 0,
494 msg->buf, msg->len);
495 }
496 if (ret) {
497 debug("i2c_write: error sending\n");
498 return -EREMOTEIO;
499 }
500 }
501
502 return 0;
503}
504
505static int designware_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
506{
507 struct dw_i2c *i2c = dev_get_priv(bus);
Ley Foon Tan2d1e8792019-06-12 09:48:04 +0800508 ulong rate;
Stefan Roese334b9b02016-04-21 08:19:41 +0200509
Ley Foon Tan2d1e8792019-06-12 09:48:04 +0800510#if CONFIG_IS_ENABLED(CLK)
511 rate = clk_get_rate(&i2c->clk);
512 if (IS_ERR_VALUE(rate))
513 return -EINVAL;
514
515 /* Convert to MHz */
516 rate /= 1000000;
517#else
518 rate = IC_CLK;
519#endif
520 return __dw_i2c_set_bus_speed(i2c->regs, i2c->scl_sda_cfg, speed,
521 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 Glass457df232019-12-06 21:41:40 -0700540static int designware_i2c_ofdata_to_platdata(struct udevice *bus)
541{
542 struct dw_i2c *priv = dev_get_priv(bus);
543
544 priv->regs = (struct i2c_regs *)devfdt_get_addr_ptr(bus);
545
546 return 0;
547}
548
549int designware_i2c_probe(struct udevice *bus)
Stefan Roese334b9b02016-04-21 08:19:41 +0200550{
551 struct dw_i2c *priv = dev_get_priv(bus);
Dinh Nguyen622597d2018-04-04 17:18:24 -0500552 int ret;
Stefan Roese334b9b02016-04-21 08:19:41 +0200553
Simon Goldschmidt36821b32019-03-28 21:11:48 +0100554 ret = reset_get_bulk(bus, &priv->resets);
Dinh Nguyen622597d2018-04-04 17:18:24 -0500555 if (ret)
Simon Goldschmidt36821b32019-03-28 21:11:48 +0100556 dev_warn(bus, "Can't get reset: %d\n", ret);
557 else
558 reset_deassert_bulk(&priv->resets);
Dinh Nguyen622597d2018-04-04 17:18:24 -0500559
Ley Foon Tan2d1e8792019-06-12 09:48:04 +0800560#if CONFIG_IS_ENABLED(CLK)
561 ret = clk_get_by_index(bus, 0, &priv->clk);
562 if (ret)
563 return ret;
564
565 ret = clk_enable(&priv->clk);
566 if (ret && ret != -ENOSYS && ret != -ENOTSUPP) {
567 clk_free(&priv->clk);
568 dev_err(bus, "failed to enable clock\n");
569 return ret;
570 }
571#endif
572
Simon Glass2b5d0292019-02-16 20:24:39 -0700573 return __dw_i2c_init(priv->regs, 0, 0);
Stefan Roese334b9b02016-04-21 08:19:41 +0200574}
575
Simon Glass457df232019-12-06 21:41:40 -0700576int designware_i2c_remove(struct udevice *dev)
Simon Goldschmidt36821b32019-03-28 21:11:48 +0100577{
578 struct dw_i2c *priv = dev_get_priv(dev);
579
Ley Foon Tan2d1e8792019-06-12 09:48:04 +0800580#if CONFIG_IS_ENABLED(CLK)
581 clk_disable(&priv->clk);
582 clk_free(&priv->clk);
583#endif
584
Simon Goldschmidt36821b32019-03-28 21:11:48 +0100585 return reset_release_bulk(&priv->resets);
586}
587
Simon Glass457df232019-12-06 21:41:40 -0700588const struct dm_i2c_ops designware_i2c_ops = {
Stefan Roese334b9b02016-04-21 08:19:41 +0200589 .xfer = designware_i2c_xfer,
590 .probe_chip = designware_i2c_probe_chip,
591 .set_bus_speed = designware_i2c_set_bus_speed,
592};
593
594static const struct udevice_id designware_i2c_ids[] = {
595 { .compatible = "snps,designware-i2c" },
596 { }
597};
598
599U_BOOT_DRIVER(i2c_designware) = {
600 .name = "i2c_designware",
601 .id = UCLASS_I2C,
602 .of_match = designware_i2c_ids,
Simon Glass457df232019-12-06 21:41:40 -0700603 .ofdata_to_platdata = designware_i2c_ofdata_to_platdata,
Stefan Roese334b9b02016-04-21 08:19:41 +0200604 .probe = designware_i2c_probe,
605 .priv_auto_alloc_size = sizeof(struct dw_i2c),
Simon Goldschmidt36821b32019-03-28 21:11:48 +0100606 .remove = designware_i2c_remove,
Simon Glass457df232019-12-06 21:41:40 -0700607 .flags = DM_FLAG_OS_PREPARE,
Stefan Roese334b9b02016-04-21 08:19:41 +0200608 .ops = &designware_i2c_ops,
609};
610
611#endif /* CONFIG_DM_I2C */