blob: 63e40823f121f680b2d0b214c0e45b16f8f18db9 [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>
Stefan Roese334b9b02016-04-21 08:19:41 +02008#include <dm.h>
Stefan Roese678398b2014-10-28 12:12:00 +01009#include <i2c.h>
Stefan Roeseba5da552016-04-21 08:19:42 +020010#include <pci.h>
Dinh Nguyen622597d2018-04-04 17:18:24 -050011#include <reset.h>
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053012#include <asm/io.h>
Vipin KUMAR031ed2f2012-02-26 23:13:29 +000013#include "designware_i2c.h"
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053014
Stefan Roeseba5da552016-04-21 08:19:42 +020015struct dw_scl_sda_cfg {
16 u32 ss_hcnt;
17 u32 fs_hcnt;
18 u32 ss_lcnt;
19 u32 fs_lcnt;
20 u32 sda_hold;
21};
22
23#ifdef CONFIG_X86
24/* BayTrail HCNT/LCNT/SDA hold time */
25static struct dw_scl_sda_cfg byt_config = {
26 .ss_hcnt = 0x200,
27 .fs_hcnt = 0x55,
28 .ss_lcnt = 0x200,
29 .fs_lcnt = 0x99,
30 .sda_hold = 0x6,
31};
32#endif
33
Stefan Roese334b9b02016-04-21 08:19:41 +020034struct dw_i2c {
35 struct i2c_regs *regs;
Stefan Roeseba5da552016-04-21 08:19:42 +020036 struct dw_scl_sda_cfg *scl_sda_cfg;
Dinh Nguyen622597d2018-04-04 17:18:24 -050037 struct reset_ctl reset_ctl;
Stefan Roese334b9b02016-04-21 08:19:41 +020038};
39
Stefan Roeseb6a77b02016-04-27 09:02:12 +020040#ifdef CONFIG_SYS_I2C_DW_ENABLE_STATUS_UNSUPPORTED
Simon Glass2b5d0292019-02-16 20:24:39 -070041static int dw_i2c_enable(struct i2c_regs *i2c_base, bool enable)
Stefan Roeseb6a77b02016-04-27 09:02:12 +020042{
43 u32 ena = enable ? IC_ENABLE_0B : 0;
44
45 writel(ena, &i2c_base->ic_enable);
Simon Glass2b5d0292019-02-16 20:24:39 -070046
47 return 0;
Stefan Roeseb6a77b02016-04-27 09:02:12 +020048}
49#else
Simon Glass2b5d0292019-02-16 20:24:39 -070050static int dw_i2c_enable(struct i2c_regs *i2c_base, bool enable)
Stefan Roese1c8b0892016-04-21 08:19:38 +020051{
52 u32 ena = enable ? IC_ENABLE_0B : 0;
53 int timeout = 100;
54
55 do {
56 writel(ena, &i2c_base->ic_enable);
57 if ((readl(&i2c_base->ic_enable_status) & IC_ENABLE_0B) == ena)
Simon Glass2b5d0292019-02-16 20:24:39 -070058 return 0;
Stefan Roese1c8b0892016-04-21 08:19:38 +020059
60 /*
61 * Wait 10 times the signaling period of the highest I2C
62 * transfer supported by the driver (for 400KHz this is
63 * 25us) as described in the DesignWare I2C databook.
64 */
65 udelay(25);
66 } while (timeout--);
Stefan Roese1c8b0892016-04-21 08:19:38 +020067 printf("timeout in %sabling I2C adapter\n", enable ? "en" : "dis");
Simon Glass2b5d0292019-02-16 20:24:39 -070068
69 return -ETIMEDOUT;
Stefan Roese1c8b0892016-04-21 08:19:38 +020070}
Stefan Roeseb6a77b02016-04-27 09:02:12 +020071#endif
Stefan Roese1c8b0892016-04-21 08:19:38 +020072
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053073/*
Stefan Roese11b544a2016-04-21 08:19:39 +020074 * i2c_set_bus_speed - Set the i2c speed
75 * @speed: required i2c speed
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053076 *
Stefan Roese11b544a2016-04-21 08:19:39 +020077 * Set the i2c speed.
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053078 */
Stefan Roese3f4358d2016-04-21 08:19:40 +020079static unsigned int __dw_i2c_set_bus_speed(struct i2c_regs *i2c_base,
Stefan Roeseba5da552016-04-21 08:19:42 +020080 struct dw_scl_sda_cfg *scl_sda_cfg,
Stefan Roese3f4358d2016-04-21 08:19:40 +020081 unsigned int speed)
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053082{
83 unsigned int cntl;
84 unsigned int hcnt, lcnt;
Stefan Roese11b544a2016-04-21 08:19:39 +020085 int i2c_spd;
86
87 if (speed >= I2C_MAX_SPEED)
88 i2c_spd = IC_SPEED_MODE_MAX;
89 else if (speed >= I2C_FAST_SPEED)
90 i2c_spd = IC_SPEED_MODE_FAST;
91 else
92 i2c_spd = IC_SPEED_MODE_STANDARD;
Armando Visconti5e3e8dd2012-03-29 20:10:17 +000093
94 /* to set speed cltr must be disabled */
Stefan Roese1c8b0892016-04-21 08:19:38 +020095 dw_i2c_enable(i2c_base, false);
Armando Visconti5e3e8dd2012-03-29 20:10:17 +000096
Stefan Roese678398b2014-10-28 12:12:00 +010097 cntl = (readl(&i2c_base->ic_con) & (~IC_CON_SPD_MSK));
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053098
99 switch (i2c_spd) {
Stefan Roeseba5da552016-04-21 08:19:42 +0200100#ifndef CONFIG_X86 /* No High-speed for BayTrail yet */
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530101 case IC_SPEED_MODE_MAX:
Stefan Roeseba5da552016-04-21 08:19:42 +0200102 cntl |= IC_CON_SPD_SS;
103 if (scl_sda_cfg) {
104 hcnt = scl_sda_cfg->fs_hcnt;
105 lcnt = scl_sda_cfg->fs_lcnt;
106 } else {
107 hcnt = (IC_CLK * MIN_HS_SCL_HIGHTIME) / NANO_TO_MICRO;
108 lcnt = (IC_CLK * MIN_HS_SCL_LOWTIME) / NANO_TO_MICRO;
109 }
Stefan Roese678398b2014-10-28 12:12:00 +0100110 writel(hcnt, &i2c_base->ic_hs_scl_hcnt);
Stefan Roese678398b2014-10-28 12:12:00 +0100111 writel(lcnt, &i2c_base->ic_hs_scl_lcnt);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530112 break;
Stefan Roeseba5da552016-04-21 08:19:42 +0200113#endif
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530114
115 case IC_SPEED_MODE_STANDARD:
116 cntl |= IC_CON_SPD_SS;
Stefan Roeseba5da552016-04-21 08:19:42 +0200117 if (scl_sda_cfg) {
118 hcnt = scl_sda_cfg->ss_hcnt;
119 lcnt = scl_sda_cfg->ss_lcnt;
120 } else {
121 hcnt = (IC_CLK * MIN_SS_SCL_HIGHTIME) / NANO_TO_MICRO;
122 lcnt = (IC_CLK * MIN_SS_SCL_LOWTIME) / NANO_TO_MICRO;
123 }
Stefan Roese678398b2014-10-28 12:12:00 +0100124 writel(hcnt, &i2c_base->ic_ss_scl_hcnt);
Stefan Roese678398b2014-10-28 12:12:00 +0100125 writel(lcnt, &i2c_base->ic_ss_scl_lcnt);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530126 break;
127
128 case IC_SPEED_MODE_FAST:
129 default:
130 cntl |= IC_CON_SPD_FS;
Stefan Roeseba5da552016-04-21 08:19:42 +0200131 if (scl_sda_cfg) {
132 hcnt = scl_sda_cfg->fs_hcnt;
133 lcnt = scl_sda_cfg->fs_lcnt;
134 } else {
135 hcnt = (IC_CLK * MIN_FS_SCL_HIGHTIME) / NANO_TO_MICRO;
136 lcnt = (IC_CLK * MIN_FS_SCL_LOWTIME) / NANO_TO_MICRO;
137 }
Stefan Roese678398b2014-10-28 12:12:00 +0100138 writel(hcnt, &i2c_base->ic_fs_scl_hcnt);
Stefan Roese678398b2014-10-28 12:12:00 +0100139 writel(lcnt, &i2c_base->ic_fs_scl_lcnt);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530140 break;
141 }
142
Stefan Roese678398b2014-10-28 12:12:00 +0100143 writel(cntl, &i2c_base->ic_con);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530144
Stefan Roeseba5da552016-04-21 08:19:42 +0200145 /* Configure SDA Hold Time if required */
146 if (scl_sda_cfg)
147 writel(scl_sda_cfg->sda_hold, &i2c_base->ic_sda_hold);
148
Armando Visconti5b8439b2012-12-06 00:04:17 +0000149 /* Enable back i2c now speed set */
Stefan Roese1c8b0892016-04-21 08:19:38 +0200150 dw_i2c_enable(i2c_base, true);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530151
Stefan Roese3f4358d2016-04-21 08:19:40 +0200152 return 0;
153}
154
155/*
156 * i2c_setaddress - Sets the target slave address
157 * @i2c_addr: target i2c address
158 *
159 * Sets the target slave address.
160 */
161static void i2c_setaddress(struct i2c_regs *i2c_base, unsigned int i2c_addr)
162{
163 /* Disable i2c */
164 dw_i2c_enable(i2c_base, false);
165
166 writel(i2c_addr, &i2c_base->ic_tar);
167
168 /* Enable i2c */
169 dw_i2c_enable(i2c_base, true);
170}
171
172/*
173 * i2c_flush_rxfifo - Flushes the i2c RX FIFO
174 *
175 * Flushes the i2c RX FIFO
176 */
177static void i2c_flush_rxfifo(struct i2c_regs *i2c_base)
178{
179 while (readl(&i2c_base->ic_status) & IC_STATUS_RFNE)
180 readl(&i2c_base->ic_cmd_data);
181}
182
183/*
184 * i2c_wait_for_bb - Waits for bus busy
185 *
186 * Waits for bus busy
187 */
188static int i2c_wait_for_bb(struct i2c_regs *i2c_base)
189{
190 unsigned long start_time_bb = get_timer(0);
191
192 while ((readl(&i2c_base->ic_status) & IC_STATUS_MA) ||
193 !(readl(&i2c_base->ic_status) & IC_STATUS_TFE)) {
194
195 /* Evaluate timeout */
196 if (get_timer(start_time_bb) > (unsigned long)(I2C_BYTE_TO_BB))
197 return 1;
198 }
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530199
200 return 0;
201}
202
Stefan Roese3f4358d2016-04-21 08:19:40 +0200203static int i2c_xfer_init(struct i2c_regs *i2c_base, uchar chip, uint addr,
204 int alen)
205{
206 if (i2c_wait_for_bb(i2c_base))
207 return 1;
208
209 i2c_setaddress(i2c_base, chip);
210 while (alen) {
211 alen--;
212 /* high byte address going out first */
213 writel((addr >> (alen * 8)) & 0xff,
214 &i2c_base->ic_cmd_data);
215 }
216 return 0;
217}
218
219static int i2c_xfer_finish(struct i2c_regs *i2c_base)
220{
221 ulong start_stop_det = get_timer(0);
222
223 while (1) {
224 if ((readl(&i2c_base->ic_raw_intr_stat) & IC_STOP_DET)) {
225 readl(&i2c_base->ic_clr_stop_det);
226 break;
227 } else if (get_timer(start_stop_det) > I2C_STOPDET_TO) {
228 break;
229 }
230 }
231
232 if (i2c_wait_for_bb(i2c_base)) {
233 printf("Timed out waiting for bus\n");
234 return 1;
235 }
236
237 i2c_flush_rxfifo(i2c_base);
238
239 return 0;
240}
241
242/*
243 * i2c_read - Read from i2c memory
244 * @chip: target i2c address
245 * @addr: address to read from
246 * @alen:
247 * @buffer: buffer for read data
248 * @len: no of bytes to be read
249 *
250 * Read from i2c memory.
251 */
252static int __dw_i2c_read(struct i2c_regs *i2c_base, u8 dev, uint addr,
253 int alen, u8 *buffer, int len)
254{
255 unsigned long start_time_rx;
Marek Vasutb0338082016-10-20 16:48:28 +0200256 unsigned int active = 0;
Stefan Roese3f4358d2016-04-21 08:19:40 +0200257
258#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
259 /*
260 * EEPROM chips that implement "address overflow" are ones
261 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
262 * address and the extra bits end up in the "chip address"
263 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
264 * four 256 byte chips.
265 *
266 * Note that we consider the length of the address field to
267 * still be one byte because the extra address bits are
268 * hidden in the chip address.
269 */
270 dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
271 addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
272
273 debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
274 addr);
275#endif
276
277 if (i2c_xfer_init(i2c_base, dev, addr, alen))
278 return 1;
279
280 start_time_rx = get_timer(0);
281 while (len) {
Marek Vasutb0338082016-10-20 16:48:28 +0200282 if (!active) {
283 /*
284 * Avoid writing to ic_cmd_data multiple times
285 * in case this loop spins too quickly and the
286 * ic_status RFNE bit isn't set after the first
287 * write. Subsequent writes to ic_cmd_data can
288 * trigger spurious i2c transfer.
289 */
290 if (len == 1)
291 writel(IC_CMD | IC_STOP, &i2c_base->ic_cmd_data);
292 else
293 writel(IC_CMD, &i2c_base->ic_cmd_data);
294 active = 1;
295 }
Stefan Roese3f4358d2016-04-21 08:19:40 +0200296
297 if (readl(&i2c_base->ic_status) & IC_STATUS_RFNE) {
298 *buffer++ = (uchar)readl(&i2c_base->ic_cmd_data);
299 len--;
300 start_time_rx = get_timer(0);
Marek Vasutb0338082016-10-20 16:48:28 +0200301 active = 0;
Stefan Roese3f4358d2016-04-21 08:19:40 +0200302 } else if (get_timer(start_time_rx) > I2C_BYTE_TO) {
Marek Vasutb0338082016-10-20 16:48:28 +0200303 return 1;
Stefan Roese3f4358d2016-04-21 08:19:40 +0200304 }
305 }
306
307 return i2c_xfer_finish(i2c_base);
308}
309
310/*
311 * i2c_write - Write to i2c memory
312 * @chip: target i2c address
313 * @addr: address to read from
314 * @alen:
315 * @buffer: buffer for read data
316 * @len: no of bytes to be read
317 *
318 * Write to i2c memory.
319 */
320static int __dw_i2c_write(struct i2c_regs *i2c_base, u8 dev, uint addr,
321 int alen, u8 *buffer, int len)
322{
323 int nb = len;
324 unsigned long start_time_tx;
325
326#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
327 /*
328 * EEPROM chips that implement "address overflow" are ones
329 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
330 * address and the extra bits end up in the "chip address"
331 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
332 * four 256 byte chips.
333 *
334 * Note that we consider the length of the address field to
335 * still be one byte because the extra address bits are
336 * hidden in the chip address.
337 */
338 dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
339 addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
340
341 debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
342 addr);
343#endif
344
345 if (i2c_xfer_init(i2c_base, dev, addr, alen))
346 return 1;
347
348 start_time_tx = get_timer(0);
349 while (len) {
350 if (readl(&i2c_base->ic_status) & IC_STATUS_TFNF) {
351 if (--len == 0) {
352 writel(*buffer | IC_STOP,
353 &i2c_base->ic_cmd_data);
354 } else {
355 writel(*buffer, &i2c_base->ic_cmd_data);
356 }
357 buffer++;
358 start_time_tx = get_timer(0);
359
360 } else if (get_timer(start_time_tx) > (nb * I2C_BYTE_TO)) {
361 printf("Timed out. i2c write Failed\n");
362 return 1;
363 }
364 }
365
366 return i2c_xfer_finish(i2c_base);
367}
368
Stefan Roese334b9b02016-04-21 08:19:41 +0200369/*
370 * __dw_i2c_init - Init function
371 * @speed: required i2c speed
372 * @slaveaddr: slave address for the device
373 *
374 * Initialization function.
375 */
Simon Glass2b5d0292019-02-16 20:24:39 -0700376static int __dw_i2c_init(struct i2c_regs *i2c_base, int speed, int slaveaddr)
Stefan Roese334b9b02016-04-21 08:19:41 +0200377{
Simon Glass2b5d0292019-02-16 20:24:39 -0700378 int ret;
379
Stefan Roese334b9b02016-04-21 08:19:41 +0200380 /* Disable i2c */
Simon Glass2b5d0292019-02-16 20:24:39 -0700381 ret = dw_i2c_enable(i2c_base, false);
382 if (ret)
383 return ret;
Stefan Roese334b9b02016-04-21 08:19:41 +0200384
Marek Vasut014e47f2017-08-07 20:45:31 +0200385 writel(IC_CON_SD | IC_CON_RE | IC_CON_SPD_FS | IC_CON_MM,
386 &i2c_base->ic_con);
Stefan Roese334b9b02016-04-21 08:19:41 +0200387 writel(IC_RX_TL, &i2c_base->ic_rx_tl);
388 writel(IC_TX_TL, &i2c_base->ic_tx_tl);
389 writel(IC_STOP_DET, &i2c_base->ic_intr_mask);
390#ifndef CONFIG_DM_I2C
Stefan Roeseba5da552016-04-21 08:19:42 +0200391 __dw_i2c_set_bus_speed(i2c_base, NULL, speed);
Stefan Roese334b9b02016-04-21 08:19:41 +0200392 writel(slaveaddr, &i2c_base->ic_sar);
393#endif
394
395 /* Enable i2c */
Simon Glass2b5d0292019-02-16 20:24:39 -0700396 ret = dw_i2c_enable(i2c_base, true);
397 if (ret)
398 return ret;
399
400 return 0;
Stefan Roese334b9b02016-04-21 08:19:41 +0200401}
402
403#ifndef CONFIG_DM_I2C
404/*
405 * The legacy I2C functions. These need to get removed once
406 * all users of this driver are converted to DM.
407 */
Stefan Roese3f4358d2016-04-21 08:19:40 +0200408static struct i2c_regs *i2c_get_base(struct i2c_adapter *adap)
409{
410 switch (adap->hwadapnr) {
411#if CONFIG_SYS_I2C_BUS_MAX >= 4
412 case 3:
413 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE3;
414#endif
415#if CONFIG_SYS_I2C_BUS_MAX >= 3
416 case 2:
417 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE2;
418#endif
419#if CONFIG_SYS_I2C_BUS_MAX >= 2
420 case 1:
421 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE1;
422#endif
423 case 0:
424 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE;
425 default:
426 printf("Wrong I2C-adapter number %d\n", adap->hwadapnr);
427 }
428
429 return NULL;
430}
431
432static unsigned int dw_i2c_set_bus_speed(struct i2c_adapter *adap,
433 unsigned int speed)
434{
435 adap->speed = speed;
Stefan Roeseba5da552016-04-21 08:19:42 +0200436 return __dw_i2c_set_bus_speed(i2c_get_base(adap), NULL, speed);
Stefan Roese3f4358d2016-04-21 08:19:40 +0200437}
438
Stefan Roese334b9b02016-04-21 08:19:41 +0200439static void dw_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530440{
Stefan Roese334b9b02016-04-21 08:19:41 +0200441 __dw_i2c_init(i2c_get_base(adap), speed, slaveaddr);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530442}
443
Stefan Roese678398b2014-10-28 12:12:00 +0100444static int dw_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr,
445 int alen, u8 *buffer, int len)
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530446{
Stefan Roese3f4358d2016-04-21 08:19:40 +0200447 return __dw_i2c_read(i2c_get_base(adap), dev, addr, alen, buffer, len);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530448}
449
Stefan Roese678398b2014-10-28 12:12:00 +0100450static int dw_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr,
451 int alen, u8 *buffer, int len)
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530452{
Stefan Roese3f4358d2016-04-21 08:19:40 +0200453 return __dw_i2c_write(i2c_get_base(adap), dev, addr, alen, buffer, len);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530454}
455
Stefan Roese334b9b02016-04-21 08:19:41 +0200456/* dw_i2c_probe - Probe the i2c chip */
Stefan Roese678398b2014-10-28 12:12:00 +0100457static int dw_i2c_probe(struct i2c_adapter *adap, u8 dev)
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530458{
Stefan Roese3f4358d2016-04-21 08:19:40 +0200459 struct i2c_regs *i2c_base = i2c_get_base(adap);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530460 u32 tmp;
Stefan Roese496ba482012-01-20 11:52:33 +0100461 int ret;
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530462
463 /*
464 * Try to read the first location of the chip.
465 */
Stefan Roese3f4358d2016-04-21 08:19:40 +0200466 ret = __dw_i2c_read(i2c_base, dev, 0, 1, (uchar *)&tmp, 1);
Stefan Roese496ba482012-01-20 11:52:33 +0100467 if (ret)
Stefan Roese678398b2014-10-28 12:12:00 +0100468 dw_i2c_init(adap, adap->speed, adap->slaveaddr);
Stefan Roese496ba482012-01-20 11:52:33 +0100469
470 return ret;
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530471}
Armando Viscontiac6e2fe2012-12-06 00:04:15 +0000472
Stefan Roese678398b2014-10-28 12:12:00 +0100473U_BOOT_I2C_ADAP_COMPLETE(dw_0, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
474 dw_i2c_write, dw_i2c_set_bus_speed,
475 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0)
Armando Viscontiac6e2fe2012-12-06 00:04:15 +0000476
Stefan Roese678398b2014-10-28 12:12:00 +0100477#if CONFIG_SYS_I2C_BUS_MAX >= 2
478U_BOOT_I2C_ADAP_COMPLETE(dw_1, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
479 dw_i2c_write, dw_i2c_set_bus_speed,
480 CONFIG_SYS_I2C_SPEED1, CONFIG_SYS_I2C_SLAVE1, 1)
481#endif
Armando Viscontiac6e2fe2012-12-06 00:04:15 +0000482
Stefan Roese678398b2014-10-28 12:12:00 +0100483#if CONFIG_SYS_I2C_BUS_MAX >= 3
484U_BOOT_I2C_ADAP_COMPLETE(dw_2, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
485 dw_i2c_write, dw_i2c_set_bus_speed,
486 CONFIG_SYS_I2C_SPEED2, CONFIG_SYS_I2C_SLAVE2, 2)
487#endif
Armando Viscontiac6e2fe2012-12-06 00:04:15 +0000488
Stefan Roese678398b2014-10-28 12:12:00 +0100489#if CONFIG_SYS_I2C_BUS_MAX >= 4
490U_BOOT_I2C_ADAP_COMPLETE(dw_3, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
491 dw_i2c_write, dw_i2c_set_bus_speed,
492 CONFIG_SYS_I2C_SPEED3, CONFIG_SYS_I2C_SLAVE3, 3)
Armando Viscontiac6e2fe2012-12-06 00:04:15 +0000493#endif
Stefan Roese334b9b02016-04-21 08:19:41 +0200494
495#else /* CONFIG_DM_I2C */
496/* The DM I2C functions */
497
498static int designware_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
499 int nmsgs)
500{
501 struct dw_i2c *i2c = dev_get_priv(bus);
502 int ret;
503
504 debug("i2c_xfer: %d messages\n", nmsgs);
505 for (; nmsgs > 0; nmsgs--, msg++) {
506 debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
507 if (msg->flags & I2C_M_RD) {
508 ret = __dw_i2c_read(i2c->regs, msg->addr, 0, 0,
509 msg->buf, msg->len);
510 } else {
511 ret = __dw_i2c_write(i2c->regs, msg->addr, 0, 0,
512 msg->buf, msg->len);
513 }
514 if (ret) {
515 debug("i2c_write: error sending\n");
516 return -EREMOTEIO;
517 }
518 }
519
520 return 0;
521}
522
523static int designware_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
524{
525 struct dw_i2c *i2c = dev_get_priv(bus);
526
Stefan Roeseba5da552016-04-21 08:19:42 +0200527 return __dw_i2c_set_bus_speed(i2c->regs, i2c->scl_sda_cfg, speed);
Stefan Roese334b9b02016-04-21 08:19:41 +0200528}
529
530static int designware_i2c_probe_chip(struct udevice *bus, uint chip_addr,
531 uint chip_flags)
532{
533 struct dw_i2c *i2c = dev_get_priv(bus);
534 struct i2c_regs *i2c_base = i2c->regs;
535 u32 tmp;
536 int ret;
537
538 /* Try to read the first location of the chip */
539 ret = __dw_i2c_read(i2c_base, chip_addr, 0, 1, (uchar *)&tmp, 1);
540 if (ret)
541 __dw_i2c_init(i2c_base, 0, 0);
542
543 return ret;
544}
545
546static int designware_i2c_probe(struct udevice *bus)
547{
548 struct dw_i2c *priv = dev_get_priv(bus);
Dinh Nguyen622597d2018-04-04 17:18:24 -0500549 int ret;
Stefan Roese334b9b02016-04-21 08:19:41 +0200550
Stefan Roeseba5da552016-04-21 08:19:42 +0200551 if (device_is_on_pci_bus(bus)) {
552#ifdef CONFIG_DM_PCI
553 /* Save base address from PCI BAR */
554 priv->regs = (struct i2c_regs *)
555 dm_pci_map_bar(bus, PCI_BASE_ADDRESS_0, PCI_REGION_MEM);
556#ifdef CONFIG_X86
557 /* Use BayTrail specific timing values */
558 priv->scl_sda_cfg = &byt_config;
559#endif
560#endif
561 } else {
Simon Glassa821c4a2017-05-17 17:18:05 -0600562 priv->regs = (struct i2c_regs *)devfdt_get_addr_ptr(bus);
Stefan Roeseba5da552016-04-21 08:19:42 +0200563 }
Stefan Roese334b9b02016-04-21 08:19:41 +0200564
Dinh Nguyen622597d2018-04-04 17:18:24 -0500565 ret = reset_get_by_name(bus, "i2c", &priv->reset_ctl);
566 if (ret)
567 pr_info("reset_get_by_name() failed: %d\n", ret);
568
569 if (&priv->reset_ctl)
570 reset_deassert(&priv->reset_ctl);
571
Simon Glass2b5d0292019-02-16 20:24:39 -0700572 return __dw_i2c_init(priv->regs, 0, 0);
Stefan Roese334b9b02016-04-21 08:19:41 +0200573}
574
Stefan Roeseba5da552016-04-21 08:19:42 +0200575static int designware_i2c_bind(struct udevice *dev)
576{
577 static int num_cards;
578 char name[20];
579
580 /* Create a unique device name for PCI type devices */
581 if (device_is_on_pci_bus(dev)) {
582 /*
583 * ToDo:
584 * Setting req_seq in the driver is probably not recommended.
585 * But without a DT alias the number is not configured. And
586 * using this driver is impossible for PCIe I2C devices.
587 * This can be removed, once a better (correct) way for this
588 * is found and implemented.
589 */
590 dev->req_seq = num_cards;
591 sprintf(name, "i2c_designware#%u", num_cards++);
592 device_set_name(dev, name);
593 }
594
595 return 0;
596}
597
Stefan Roese334b9b02016-04-21 08:19:41 +0200598static const struct dm_i2c_ops designware_i2c_ops = {
599 .xfer = designware_i2c_xfer,
600 .probe_chip = designware_i2c_probe_chip,
601 .set_bus_speed = designware_i2c_set_bus_speed,
602};
603
604static const struct udevice_id designware_i2c_ids[] = {
605 { .compatible = "snps,designware-i2c" },
606 { }
607};
608
609U_BOOT_DRIVER(i2c_designware) = {
610 .name = "i2c_designware",
611 .id = UCLASS_I2C,
612 .of_match = designware_i2c_ids,
Stefan Roeseba5da552016-04-21 08:19:42 +0200613 .bind = designware_i2c_bind,
Stefan Roese334b9b02016-04-21 08:19:41 +0200614 .probe = designware_i2c_probe,
615 .priv_auto_alloc_size = sizeof(struct dw_i2c),
616 .ops = &designware_i2c_ops,
617};
618
Stefan Roeseba5da552016-04-21 08:19:42 +0200619#ifdef CONFIG_X86
620static struct pci_device_id designware_pci_supported[] = {
621 /* Intel BayTrail has 7 I2C controller located on the PCI bus */
622 { PCI_VDEVICE(INTEL, 0x0f41) },
623 { PCI_VDEVICE(INTEL, 0x0f42) },
624 { PCI_VDEVICE(INTEL, 0x0f43) },
625 { PCI_VDEVICE(INTEL, 0x0f44) },
626 { PCI_VDEVICE(INTEL, 0x0f45) },
627 { PCI_VDEVICE(INTEL, 0x0f46) },
628 { PCI_VDEVICE(INTEL, 0x0f47) },
629 {},
630};
631
632U_BOOT_PCI_DEVICE(i2c_designware, designware_pci_supported);
633#endif
634
Stefan Roese334b9b02016-04-21 08:19:41 +0200635#endif /* CONFIG_DM_I2C */