blob: 0c7cd0ba727921e2dc49acaf761036bc7e511a86 [file] [log] [blame]
Vipin KUMAR2403f8f2010-01-15 19:15:44 +05301/*
2 * (C) Copyright 2009
3 * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Vipin KUMAR2403f8f2010-01-15 19:15:44 +05306 */
7
8#include <common.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>
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;
Stefan Roese334b9b02016-04-21 08:19:41 +020037};
38
Stefan Roese1c8b0892016-04-21 08:19:38 +020039static void dw_i2c_enable(struct i2c_regs *i2c_base, bool enable)
40{
41 u32 ena = enable ? IC_ENABLE_0B : 0;
42 int timeout = 100;
43
44 do {
45 writel(ena, &i2c_base->ic_enable);
46 if ((readl(&i2c_base->ic_enable_status) & IC_ENABLE_0B) == ena)
47 return;
48
49 /*
50 * Wait 10 times the signaling period of the highest I2C
51 * transfer supported by the driver (for 400KHz this is
52 * 25us) as described in the DesignWare I2C databook.
53 */
54 udelay(25);
55 } while (timeout--);
56
57 printf("timeout in %sabling I2C adapter\n", enable ? "en" : "dis");
58}
59
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053060/*
Stefan Roese11b544a2016-04-21 08:19:39 +020061 * i2c_set_bus_speed - Set the i2c speed
62 * @speed: required i2c speed
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053063 *
Stefan Roese11b544a2016-04-21 08:19:39 +020064 * Set the i2c speed.
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053065 */
Stefan Roese3f4358d2016-04-21 08:19:40 +020066static unsigned int __dw_i2c_set_bus_speed(struct i2c_regs *i2c_base,
Stefan Roeseba5da552016-04-21 08:19:42 +020067 struct dw_scl_sda_cfg *scl_sda_cfg,
Stefan Roese3f4358d2016-04-21 08:19:40 +020068 unsigned int speed)
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053069{
70 unsigned int cntl;
71 unsigned int hcnt, lcnt;
Stefan Roese11b544a2016-04-21 08:19:39 +020072 int i2c_spd;
73
74 if (speed >= I2C_MAX_SPEED)
75 i2c_spd = IC_SPEED_MODE_MAX;
76 else if (speed >= I2C_FAST_SPEED)
77 i2c_spd = IC_SPEED_MODE_FAST;
78 else
79 i2c_spd = IC_SPEED_MODE_STANDARD;
Armando Visconti5e3e8dd2012-03-29 20:10:17 +000080
81 /* 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) {
Stefan Roeseba5da552016-04-21 08:19:42 +020087#ifndef CONFIG_X86 /* No High-speed for BayTrail yet */
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053088 case IC_SPEED_MODE_MAX:
Stefan Roeseba5da552016-04-21 08:19:42 +020089 cntl |= IC_CON_SPD_SS;
90 if (scl_sda_cfg) {
91 hcnt = scl_sda_cfg->fs_hcnt;
92 lcnt = scl_sda_cfg->fs_lcnt;
93 } else {
94 hcnt = (IC_CLK * MIN_HS_SCL_HIGHTIME) / NANO_TO_MICRO;
95 lcnt = (IC_CLK * MIN_HS_SCL_LOWTIME) / NANO_TO_MICRO;
96 }
Stefan Roese678398b2014-10-28 12:12:00 +010097 writel(hcnt, &i2c_base->ic_hs_scl_hcnt);
Stefan Roese678398b2014-10-28 12:12:00 +010098 writel(lcnt, &i2c_base->ic_hs_scl_lcnt);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053099 break;
Stefan Roeseba5da552016-04-21 08:19:42 +0200100#endif
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530101
102 case IC_SPEED_MODE_STANDARD:
103 cntl |= IC_CON_SPD_SS;
Stefan Roeseba5da552016-04-21 08:19:42 +0200104 if (scl_sda_cfg) {
105 hcnt = scl_sda_cfg->ss_hcnt;
106 lcnt = scl_sda_cfg->ss_lcnt;
107 } else {
108 hcnt = (IC_CLK * MIN_SS_SCL_HIGHTIME) / NANO_TO_MICRO;
109 lcnt = (IC_CLK * MIN_SS_SCL_LOWTIME) / NANO_TO_MICRO;
110 }
Stefan Roese678398b2014-10-28 12:12:00 +0100111 writel(hcnt, &i2c_base->ic_ss_scl_hcnt);
Stefan Roese678398b2014-10-28 12:12:00 +0100112 writel(lcnt, &i2c_base->ic_ss_scl_lcnt);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530113 break;
114
115 case IC_SPEED_MODE_FAST:
116 default:
117 cntl |= IC_CON_SPD_FS;
Stefan Roeseba5da552016-04-21 08:19:42 +0200118 if (scl_sda_cfg) {
119 hcnt = scl_sda_cfg->fs_hcnt;
120 lcnt = scl_sda_cfg->fs_lcnt;
121 } else {
122 hcnt = (IC_CLK * MIN_FS_SCL_HIGHTIME) / NANO_TO_MICRO;
123 lcnt = (IC_CLK * MIN_FS_SCL_LOWTIME) / NANO_TO_MICRO;
124 }
Stefan Roese678398b2014-10-28 12:12:00 +0100125 writel(hcnt, &i2c_base->ic_fs_scl_hcnt);
Stefan Roese678398b2014-10-28 12:12:00 +0100126 writel(lcnt, &i2c_base->ic_fs_scl_lcnt);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530127 break;
128 }
129
Stefan Roese678398b2014-10-28 12:12:00 +0100130 writel(cntl, &i2c_base->ic_con);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530131
Stefan Roeseba5da552016-04-21 08:19:42 +0200132 /* Configure SDA Hold Time if required */
133 if (scl_sda_cfg)
134 writel(scl_sda_cfg->sda_hold, &i2c_base->ic_sda_hold);
135
Armando Visconti5b8439b2012-12-06 00:04:17 +0000136 /* Enable back i2c now speed set */
Stefan Roese1c8b0892016-04-21 08:19:38 +0200137 dw_i2c_enable(i2c_base, true);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530138
Stefan Roese3f4358d2016-04-21 08:19:40 +0200139 return 0;
140}
141
142/*
143 * i2c_setaddress - Sets the target slave address
144 * @i2c_addr: target i2c address
145 *
146 * Sets the target slave address.
147 */
148static void i2c_setaddress(struct i2c_regs *i2c_base, unsigned int i2c_addr)
149{
150 /* Disable i2c */
151 dw_i2c_enable(i2c_base, false);
152
153 writel(i2c_addr, &i2c_base->ic_tar);
154
155 /* Enable i2c */
156 dw_i2c_enable(i2c_base, true);
157}
158
159/*
160 * i2c_flush_rxfifo - Flushes the i2c RX FIFO
161 *
162 * Flushes the i2c RX FIFO
163 */
164static void i2c_flush_rxfifo(struct i2c_regs *i2c_base)
165{
166 while (readl(&i2c_base->ic_status) & IC_STATUS_RFNE)
167 readl(&i2c_base->ic_cmd_data);
168}
169
170/*
171 * i2c_wait_for_bb - Waits for bus busy
172 *
173 * Waits for bus busy
174 */
175static int i2c_wait_for_bb(struct i2c_regs *i2c_base)
176{
177 unsigned long start_time_bb = get_timer(0);
178
179 while ((readl(&i2c_base->ic_status) & IC_STATUS_MA) ||
180 !(readl(&i2c_base->ic_status) & IC_STATUS_TFE)) {
181
182 /* Evaluate timeout */
183 if (get_timer(start_time_bb) > (unsigned long)(I2C_BYTE_TO_BB))
184 return 1;
185 }
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530186
187 return 0;
188}
189
Stefan Roese3f4358d2016-04-21 08:19:40 +0200190static int i2c_xfer_init(struct i2c_regs *i2c_base, uchar chip, uint addr,
191 int alen)
192{
193 if (i2c_wait_for_bb(i2c_base))
194 return 1;
195
196 i2c_setaddress(i2c_base, chip);
197 while (alen) {
198 alen--;
199 /* high byte address going out first */
200 writel((addr >> (alen * 8)) & 0xff,
201 &i2c_base->ic_cmd_data);
202 }
203 return 0;
204}
205
206static int i2c_xfer_finish(struct i2c_regs *i2c_base)
207{
208 ulong start_stop_det = get_timer(0);
209
210 while (1) {
211 if ((readl(&i2c_base->ic_raw_intr_stat) & IC_STOP_DET)) {
212 readl(&i2c_base->ic_clr_stop_det);
213 break;
214 } else if (get_timer(start_stop_det) > I2C_STOPDET_TO) {
215 break;
216 }
217 }
218
219 if (i2c_wait_for_bb(i2c_base)) {
220 printf("Timed out waiting for bus\n");
221 return 1;
222 }
223
224 i2c_flush_rxfifo(i2c_base);
225
226 return 0;
227}
228
229/*
230 * i2c_read - Read from i2c memory
231 * @chip: target i2c address
232 * @addr: address to read from
233 * @alen:
234 * @buffer: buffer for read data
235 * @len: no of bytes to be read
236 *
237 * Read from i2c memory.
238 */
239static int __dw_i2c_read(struct i2c_regs *i2c_base, u8 dev, uint addr,
240 int alen, u8 *buffer, int len)
241{
242 unsigned long start_time_rx;
243
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) {
268 if (len == 1)
269 writel(IC_CMD | IC_STOP, &i2c_base->ic_cmd_data);
270 else
271 writel(IC_CMD, &i2c_base->ic_cmd_data);
272
273 if (readl(&i2c_base->ic_status) & IC_STATUS_RFNE) {
274 *buffer++ = (uchar)readl(&i2c_base->ic_cmd_data);
275 len--;
276 start_time_rx = get_timer(0);
277
278 } else if (get_timer(start_time_rx) > I2C_BYTE_TO) {
279 return 1;
280 }
281 }
282
283 return i2c_xfer_finish(i2c_base);
284}
285
286/*
287 * i2c_write - Write to i2c memory
288 * @chip: target i2c address
289 * @addr: address to read from
290 * @alen:
291 * @buffer: buffer for read data
292 * @len: no of bytes to be read
293 *
294 * Write to i2c memory.
295 */
296static int __dw_i2c_write(struct i2c_regs *i2c_base, u8 dev, uint addr,
297 int alen, u8 *buffer, int len)
298{
299 int nb = len;
300 unsigned long start_time_tx;
301
302#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
303 /*
304 * EEPROM chips that implement "address overflow" are ones
305 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
306 * address and the extra bits end up in the "chip address"
307 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
308 * four 256 byte chips.
309 *
310 * Note that we consider the length of the address field to
311 * still be one byte because the extra address bits are
312 * hidden in the chip address.
313 */
314 dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
315 addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
316
317 debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
318 addr);
319#endif
320
321 if (i2c_xfer_init(i2c_base, dev, addr, alen))
322 return 1;
323
324 start_time_tx = get_timer(0);
325 while (len) {
326 if (readl(&i2c_base->ic_status) & IC_STATUS_TFNF) {
327 if (--len == 0) {
328 writel(*buffer | IC_STOP,
329 &i2c_base->ic_cmd_data);
330 } else {
331 writel(*buffer, &i2c_base->ic_cmd_data);
332 }
333 buffer++;
334 start_time_tx = get_timer(0);
335
336 } else if (get_timer(start_time_tx) > (nb * I2C_BYTE_TO)) {
337 printf("Timed out. i2c write Failed\n");
338 return 1;
339 }
340 }
341
342 return i2c_xfer_finish(i2c_base);
343}
344
Stefan Roese334b9b02016-04-21 08:19:41 +0200345/*
346 * __dw_i2c_init - Init function
347 * @speed: required i2c speed
348 * @slaveaddr: slave address for the device
349 *
350 * Initialization function.
351 */
352static void __dw_i2c_init(struct i2c_regs *i2c_base, int speed, int slaveaddr)
353{
354 /* Disable i2c */
355 dw_i2c_enable(i2c_base, false);
356
357 writel((IC_CON_SD | IC_CON_SPD_FS | IC_CON_MM), &i2c_base->ic_con);
358 writel(IC_RX_TL, &i2c_base->ic_rx_tl);
359 writel(IC_TX_TL, &i2c_base->ic_tx_tl);
360 writel(IC_STOP_DET, &i2c_base->ic_intr_mask);
361#ifndef CONFIG_DM_I2C
Stefan Roeseba5da552016-04-21 08:19:42 +0200362 __dw_i2c_set_bus_speed(i2c_base, NULL, speed);
Stefan Roese334b9b02016-04-21 08:19:41 +0200363 writel(slaveaddr, &i2c_base->ic_sar);
364#endif
365
366 /* Enable i2c */
367 dw_i2c_enable(i2c_base, true);
368}
369
370#ifndef CONFIG_DM_I2C
371/*
372 * The legacy I2C functions. These need to get removed once
373 * all users of this driver are converted to DM.
374 */
Stefan Roese3f4358d2016-04-21 08:19:40 +0200375static struct i2c_regs *i2c_get_base(struct i2c_adapter *adap)
376{
377 switch (adap->hwadapnr) {
378#if CONFIG_SYS_I2C_BUS_MAX >= 4
379 case 3:
380 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE3;
381#endif
382#if CONFIG_SYS_I2C_BUS_MAX >= 3
383 case 2:
384 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE2;
385#endif
386#if CONFIG_SYS_I2C_BUS_MAX >= 2
387 case 1:
388 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE1;
389#endif
390 case 0:
391 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE;
392 default:
393 printf("Wrong I2C-adapter number %d\n", adap->hwadapnr);
394 }
395
396 return NULL;
397}
398
399static unsigned int dw_i2c_set_bus_speed(struct i2c_adapter *adap,
400 unsigned int speed)
401{
402 adap->speed = speed;
Stefan Roeseba5da552016-04-21 08:19:42 +0200403 return __dw_i2c_set_bus_speed(i2c_get_base(adap), NULL, speed);
Stefan Roese3f4358d2016-04-21 08:19:40 +0200404}
405
Stefan Roese334b9b02016-04-21 08:19:41 +0200406static void dw_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530407{
Stefan Roese334b9b02016-04-21 08:19:41 +0200408 __dw_i2c_init(i2c_get_base(adap), speed, slaveaddr);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530409}
410
Stefan Roese678398b2014-10-28 12:12:00 +0100411static int dw_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr,
412 int alen, u8 *buffer, int len)
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530413{
Stefan Roese3f4358d2016-04-21 08:19:40 +0200414 return __dw_i2c_read(i2c_get_base(adap), dev, addr, alen, buffer, len);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530415}
416
Stefan Roese678398b2014-10-28 12:12:00 +0100417static int dw_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr,
418 int alen, u8 *buffer, int len)
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530419{
Stefan Roese3f4358d2016-04-21 08:19:40 +0200420 return __dw_i2c_write(i2c_get_base(adap), dev, addr, alen, buffer, len);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530421}
422
Stefan Roese334b9b02016-04-21 08:19:41 +0200423/* dw_i2c_probe - Probe the i2c chip */
Stefan Roese678398b2014-10-28 12:12:00 +0100424static int dw_i2c_probe(struct i2c_adapter *adap, u8 dev)
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530425{
Stefan Roese3f4358d2016-04-21 08:19:40 +0200426 struct i2c_regs *i2c_base = i2c_get_base(adap);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530427 u32 tmp;
Stefan Roese496ba482012-01-20 11:52:33 +0100428 int ret;
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530429
430 /*
431 * Try to read the first location of the chip.
432 */
Stefan Roese3f4358d2016-04-21 08:19:40 +0200433 ret = __dw_i2c_read(i2c_base, dev, 0, 1, (uchar *)&tmp, 1);
Stefan Roese496ba482012-01-20 11:52:33 +0100434 if (ret)
Stefan Roese678398b2014-10-28 12:12:00 +0100435 dw_i2c_init(adap, adap->speed, adap->slaveaddr);
Stefan Roese496ba482012-01-20 11:52:33 +0100436
437 return ret;
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530438}
Armando Viscontiac6e2fe2012-12-06 00:04:15 +0000439
Stefan Roese678398b2014-10-28 12:12:00 +0100440U_BOOT_I2C_ADAP_COMPLETE(dw_0, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
441 dw_i2c_write, dw_i2c_set_bus_speed,
442 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0)
Armando Viscontiac6e2fe2012-12-06 00:04:15 +0000443
Stefan Roese678398b2014-10-28 12:12:00 +0100444#if CONFIG_SYS_I2C_BUS_MAX >= 2
445U_BOOT_I2C_ADAP_COMPLETE(dw_1, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
446 dw_i2c_write, dw_i2c_set_bus_speed,
447 CONFIG_SYS_I2C_SPEED1, CONFIG_SYS_I2C_SLAVE1, 1)
448#endif
Armando Viscontiac6e2fe2012-12-06 00:04:15 +0000449
Stefan Roese678398b2014-10-28 12:12:00 +0100450#if CONFIG_SYS_I2C_BUS_MAX >= 3
451U_BOOT_I2C_ADAP_COMPLETE(dw_2, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
452 dw_i2c_write, dw_i2c_set_bus_speed,
453 CONFIG_SYS_I2C_SPEED2, CONFIG_SYS_I2C_SLAVE2, 2)
454#endif
Armando Viscontiac6e2fe2012-12-06 00:04:15 +0000455
Stefan Roese678398b2014-10-28 12:12:00 +0100456#if CONFIG_SYS_I2C_BUS_MAX >= 4
457U_BOOT_I2C_ADAP_COMPLETE(dw_3, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
458 dw_i2c_write, dw_i2c_set_bus_speed,
459 CONFIG_SYS_I2C_SPEED3, CONFIG_SYS_I2C_SLAVE3, 3)
Armando Viscontiac6e2fe2012-12-06 00:04:15 +0000460#endif
Stefan Roese334b9b02016-04-21 08:19:41 +0200461
462#else /* CONFIG_DM_I2C */
463/* The DM I2C functions */
464
465static int designware_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
466 int nmsgs)
467{
468 struct dw_i2c *i2c = dev_get_priv(bus);
469 int ret;
470
471 debug("i2c_xfer: %d messages\n", nmsgs);
472 for (; nmsgs > 0; nmsgs--, msg++) {
473 debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
474 if (msg->flags & I2C_M_RD) {
475 ret = __dw_i2c_read(i2c->regs, msg->addr, 0, 0,
476 msg->buf, msg->len);
477 } else {
478 ret = __dw_i2c_write(i2c->regs, msg->addr, 0, 0,
479 msg->buf, msg->len);
480 }
481 if (ret) {
482 debug("i2c_write: error sending\n");
483 return -EREMOTEIO;
484 }
485 }
486
487 return 0;
488}
489
490static int designware_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
491{
492 struct dw_i2c *i2c = dev_get_priv(bus);
493
Stefan Roeseba5da552016-04-21 08:19:42 +0200494 return __dw_i2c_set_bus_speed(i2c->regs, i2c->scl_sda_cfg, speed);
Stefan Roese334b9b02016-04-21 08:19:41 +0200495}
496
497static int designware_i2c_probe_chip(struct udevice *bus, uint chip_addr,
498 uint chip_flags)
499{
500 struct dw_i2c *i2c = dev_get_priv(bus);
501 struct i2c_regs *i2c_base = i2c->regs;
502 u32 tmp;
503 int ret;
504
505 /* Try to read the first location of the chip */
506 ret = __dw_i2c_read(i2c_base, chip_addr, 0, 1, (uchar *)&tmp, 1);
507 if (ret)
508 __dw_i2c_init(i2c_base, 0, 0);
509
510 return ret;
511}
512
513static int designware_i2c_probe(struct udevice *bus)
514{
515 struct dw_i2c *priv = dev_get_priv(bus);
516
Stefan Roeseba5da552016-04-21 08:19:42 +0200517 if (device_is_on_pci_bus(bus)) {
518#ifdef CONFIG_DM_PCI
519 /* Save base address from PCI BAR */
520 priv->regs = (struct i2c_regs *)
521 dm_pci_map_bar(bus, PCI_BASE_ADDRESS_0, PCI_REGION_MEM);
522#ifdef CONFIG_X86
523 /* Use BayTrail specific timing values */
524 priv->scl_sda_cfg = &byt_config;
525#endif
526#endif
527 } else {
528 priv->regs = (struct i2c_regs *)dev_get_addr_ptr(bus);
529 }
Stefan Roese334b9b02016-04-21 08:19:41 +0200530
531 __dw_i2c_init(priv->regs, 0, 0);
532
533 return 0;
534}
535
Stefan Roeseba5da552016-04-21 08:19:42 +0200536static int designware_i2c_bind(struct udevice *dev)
537{
538 static int num_cards;
539 char name[20];
540
541 /* Create a unique device name for PCI type devices */
542 if (device_is_on_pci_bus(dev)) {
543 /*
544 * ToDo:
545 * Setting req_seq in the driver is probably not recommended.
546 * But without a DT alias the number is not configured. And
547 * using this driver is impossible for PCIe I2C devices.
548 * This can be removed, once a better (correct) way for this
549 * is found and implemented.
550 */
551 dev->req_seq = num_cards;
552 sprintf(name, "i2c_designware#%u", num_cards++);
553 device_set_name(dev, name);
554 }
555
556 return 0;
557}
558
Stefan Roese334b9b02016-04-21 08:19:41 +0200559static const struct dm_i2c_ops designware_i2c_ops = {
560 .xfer = designware_i2c_xfer,
561 .probe_chip = designware_i2c_probe_chip,
562 .set_bus_speed = designware_i2c_set_bus_speed,
563};
564
565static const struct udevice_id designware_i2c_ids[] = {
566 { .compatible = "snps,designware-i2c" },
567 { }
568};
569
570U_BOOT_DRIVER(i2c_designware) = {
571 .name = "i2c_designware",
572 .id = UCLASS_I2C,
573 .of_match = designware_i2c_ids,
Stefan Roeseba5da552016-04-21 08:19:42 +0200574 .bind = designware_i2c_bind,
Stefan Roese334b9b02016-04-21 08:19:41 +0200575 .probe = designware_i2c_probe,
576 .priv_auto_alloc_size = sizeof(struct dw_i2c),
577 .ops = &designware_i2c_ops,
578};
579
Stefan Roeseba5da552016-04-21 08:19:42 +0200580#ifdef CONFIG_X86
581static struct pci_device_id designware_pci_supported[] = {
582 /* Intel BayTrail has 7 I2C controller located on the PCI bus */
583 { PCI_VDEVICE(INTEL, 0x0f41) },
584 { PCI_VDEVICE(INTEL, 0x0f42) },
585 { PCI_VDEVICE(INTEL, 0x0f43) },
586 { PCI_VDEVICE(INTEL, 0x0f44) },
587 { PCI_VDEVICE(INTEL, 0x0f45) },
588 { PCI_VDEVICE(INTEL, 0x0f46) },
589 { PCI_VDEVICE(INTEL, 0x0f47) },
590 {},
591};
592
593U_BOOT_PCI_DEVICE(i2c_designware, designware_pci_supported);
594#endif
595
Stefan Roese334b9b02016-04-21 08:19:41 +0200596#endif /* CONFIG_DM_I2C */