1
2
3
4
5
6
7#include <common.h>
8#include <dm.h>
9#include <i2c.h>
10#include <pci.h>
11#include <reset.h>
12#include <asm/io.h>
13#include "designware_i2c.h"
14
15struct 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
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
34struct dw_i2c {
35 struct i2c_regs *regs;
36 struct dw_scl_sda_cfg *scl_sda_cfg;
37 struct reset_ctl reset_ctl;
38};
39
40#ifdef CONFIG_SYS_I2C_DW_ENABLE_STATUS_UNSUPPORTED
41static void dw_i2c_enable(struct i2c_regs *i2c_base, bool enable)
42{
43 u32 ena = enable ? IC_ENABLE_0B : 0;
44
45 writel(ena, &i2c_base->ic_enable);
46}
47#else
48static void dw_i2c_enable(struct i2c_regs *i2c_base, bool enable)
49{
50 u32 ena = enable ? IC_ENABLE_0B : 0;
51 int timeout = 100;
52
53 do {
54 writel(ena, &i2c_base->ic_enable);
55 if ((readl(&i2c_base->ic_enable_status) & IC_ENABLE_0B) == ena)
56 return;
57
58
59
60
61
62
63 udelay(25);
64 } while (timeout--);
65
66 printf("timeout in %sabling I2C adapter\n", enable ? "en" : "dis");
67}
68#endif
69
70
71
72
73
74
75
76static unsigned int __dw_i2c_set_bus_speed(struct i2c_regs *i2c_base,
77 struct dw_scl_sda_cfg *scl_sda_cfg,
78 unsigned int speed)
79{
80 unsigned int cntl;
81 unsigned int hcnt, lcnt;
82 int i2c_spd;
83
84 if (speed >= I2C_MAX_SPEED)
85 i2c_spd = IC_SPEED_MODE_MAX;
86 else if (speed >= I2C_FAST_SPEED)
87 i2c_spd = IC_SPEED_MODE_FAST;
88 else
89 i2c_spd = IC_SPEED_MODE_STANDARD;
90
91
92 dw_i2c_enable(i2c_base, false);
93
94 cntl = (readl(&i2c_base->ic_con) & (~IC_CON_SPD_MSK));
95
96 switch (i2c_spd) {
97#ifndef CONFIG_X86
98 case IC_SPEED_MODE_MAX:
99 cntl |= IC_CON_SPD_SS;
100 if (scl_sda_cfg) {
101 hcnt = scl_sda_cfg->fs_hcnt;
102 lcnt = scl_sda_cfg->fs_lcnt;
103 } else {
104 hcnt = (IC_CLK * MIN_HS_SCL_HIGHTIME) / NANO_TO_MICRO;
105 lcnt = (IC_CLK * MIN_HS_SCL_LOWTIME) / NANO_TO_MICRO;
106 }
107 writel(hcnt, &i2c_base->ic_hs_scl_hcnt);
108 writel(lcnt, &i2c_base->ic_hs_scl_lcnt);
109 break;
110#endif
111
112 case IC_SPEED_MODE_STANDARD:
113 cntl |= IC_CON_SPD_SS;
114 if (scl_sda_cfg) {
115 hcnt = scl_sda_cfg->ss_hcnt;
116 lcnt = scl_sda_cfg->ss_lcnt;
117 } else {
118 hcnt = (IC_CLK * MIN_SS_SCL_HIGHTIME) / NANO_TO_MICRO;
119 lcnt = (IC_CLK * MIN_SS_SCL_LOWTIME) / NANO_TO_MICRO;
120 }
121 writel(hcnt, &i2c_base->ic_ss_scl_hcnt);
122 writel(lcnt, &i2c_base->ic_ss_scl_lcnt);
123 break;
124
125 case IC_SPEED_MODE_FAST:
126 default:
127 cntl |= IC_CON_SPD_FS;
128 if (scl_sda_cfg) {
129 hcnt = scl_sda_cfg->fs_hcnt;
130 lcnt = scl_sda_cfg->fs_lcnt;
131 } else {
132 hcnt = (IC_CLK * MIN_FS_SCL_HIGHTIME) / NANO_TO_MICRO;
133 lcnt = (IC_CLK * MIN_FS_SCL_LOWTIME) / NANO_TO_MICRO;
134 }
135 writel(hcnt, &i2c_base->ic_fs_scl_hcnt);
136 writel(lcnt, &i2c_base->ic_fs_scl_lcnt);
137 break;
138 }
139
140 writel(cntl, &i2c_base->ic_con);
141
142
143 if (scl_sda_cfg)
144 writel(scl_sda_cfg->sda_hold, &i2c_base->ic_sda_hold);
145
146
147 dw_i2c_enable(i2c_base, true);
148
149 return 0;
150}
151
152
153
154
155
156
157
158static void i2c_setaddress(struct i2c_regs *i2c_base, unsigned int i2c_addr)
159{
160
161 dw_i2c_enable(i2c_base, false);
162
163 writel(i2c_addr, &i2c_base->ic_tar);
164
165
166 dw_i2c_enable(i2c_base, true);
167}
168
169
170
171
172
173
174static void i2c_flush_rxfifo(struct i2c_regs *i2c_base)
175{
176 while (readl(&i2c_base->ic_status) & IC_STATUS_RFNE)
177 readl(&i2c_base->ic_cmd_data);
178}
179
180
181
182
183
184
185static int i2c_wait_for_bb(struct i2c_regs *i2c_base)
186{
187 unsigned long start_time_bb = get_timer(0);
188
189 while ((readl(&i2c_base->ic_status) & IC_STATUS_MA) ||
190 !(readl(&i2c_base->ic_status) & IC_STATUS_TFE)) {
191
192
193 if (get_timer(start_time_bb) > (unsigned long)(I2C_BYTE_TO_BB))
194 return 1;
195 }
196
197 return 0;
198}
199
200static int i2c_xfer_init(struct i2c_regs *i2c_base, uchar chip, uint addr,
201 int alen)
202{
203 if (i2c_wait_for_bb(i2c_base))
204 return 1;
205
206 i2c_setaddress(i2c_base, chip);
207 while (alen) {
208 alen--;
209
210 writel((addr >> (alen * 8)) & 0xff,
211 &i2c_base->ic_cmd_data);
212 }
213 return 0;
214}
215
216static int i2c_xfer_finish(struct i2c_regs *i2c_base)
217{
218 ulong start_stop_det = get_timer(0);
219
220 while (1) {
221 if ((readl(&i2c_base->ic_raw_intr_stat) & IC_STOP_DET)) {
222 readl(&i2c_base->ic_clr_stop_det);
223 break;
224 } else if (get_timer(start_stop_det) > I2C_STOPDET_TO) {
225 break;
226 }
227 }
228
229 if (i2c_wait_for_bb(i2c_base)) {
230 printf("Timed out waiting for bus\n");
231 return 1;
232 }
233
234 i2c_flush_rxfifo(i2c_base);
235
236 return 0;
237}
238
239
240
241
242
243
244
245
246
247
248
249static int __dw_i2c_read(struct i2c_regs *i2c_base, u8 dev, uint addr,
250 int alen, u8 *buffer, int len)
251{
252 unsigned long start_time_rx;
253 unsigned int active = 0;
254
255#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
256
257
258
259
260
261
262
263
264
265
266
267 dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
268 addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
269
270 debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
271 addr);
272#endif
273
274 if (i2c_xfer_init(i2c_base, dev, addr, alen))
275 return 1;
276
277 start_time_rx = get_timer(0);
278 while (len) {
279 if (!active) {
280
281
282
283
284
285
286
287 if (len == 1)
288 writel(IC_CMD | IC_STOP, &i2c_base->ic_cmd_data);
289 else
290 writel(IC_CMD, &i2c_base->ic_cmd_data);
291 active = 1;
292 }
293
294 if (readl(&i2c_base->ic_status) & IC_STATUS_RFNE) {
295 *buffer++ = (uchar)readl(&i2c_base->ic_cmd_data);
296 len--;
297 start_time_rx = get_timer(0);
298 active = 0;
299 } else if (get_timer(start_time_rx) > I2C_BYTE_TO) {
300 return 1;
301 }
302 }
303
304 return i2c_xfer_finish(i2c_base);
305}
306
307
308
309
310
311
312
313
314
315
316
317static int __dw_i2c_write(struct i2c_regs *i2c_base, u8 dev, uint addr,
318 int alen, u8 *buffer, int len)
319{
320 int nb = len;
321 unsigned long start_time_tx;
322
323#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
324
325
326
327
328
329
330
331
332
333
334
335 dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
336 addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
337
338 debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
339 addr);
340#endif
341
342 if (i2c_xfer_init(i2c_base, dev, addr, alen))
343 return 1;
344
345 start_time_tx = get_timer(0);
346 while (len) {
347 if (readl(&i2c_base->ic_status) & IC_STATUS_TFNF) {
348 if (--len == 0) {
349 writel(*buffer | IC_STOP,
350 &i2c_base->ic_cmd_data);
351 } else {
352 writel(*buffer, &i2c_base->ic_cmd_data);
353 }
354 buffer++;
355 start_time_tx = get_timer(0);
356
357 } else if (get_timer(start_time_tx) > (nb * I2C_BYTE_TO)) {
358 printf("Timed out. i2c write Failed\n");
359 return 1;
360 }
361 }
362
363 return i2c_xfer_finish(i2c_base);
364}
365
366
367
368
369
370
371
372
373static void __dw_i2c_init(struct i2c_regs *i2c_base, int speed, int slaveaddr)
374{
375
376 dw_i2c_enable(i2c_base, false);
377
378 writel(IC_CON_SD | IC_CON_RE | IC_CON_SPD_FS | IC_CON_MM,
379 &i2c_base->ic_con);
380 writel(IC_RX_TL, &i2c_base->ic_rx_tl);
381 writel(IC_TX_TL, &i2c_base->ic_tx_tl);
382 writel(IC_STOP_DET, &i2c_base->ic_intr_mask);
383#ifndef CONFIG_DM_I2C
384 __dw_i2c_set_bus_speed(i2c_base, NULL, speed);
385 writel(slaveaddr, &i2c_base->ic_sar);
386#endif
387
388
389 dw_i2c_enable(i2c_base, true);
390}
391
392#ifndef CONFIG_DM_I2C
393
394
395
396
397static struct i2c_regs *i2c_get_base(struct i2c_adapter *adap)
398{
399 switch (adap->hwadapnr) {
400#if CONFIG_SYS_I2C_BUS_MAX >= 4
401 case 3:
402 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE3;
403#endif
404#if CONFIG_SYS_I2C_BUS_MAX >= 3
405 case 2:
406 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE2;
407#endif
408#if CONFIG_SYS_I2C_BUS_MAX >= 2
409 case 1:
410 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE1;
411#endif
412 case 0:
413 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE;
414 default:
415 printf("Wrong I2C-adapter number %d\n", adap->hwadapnr);
416 }
417
418 return NULL;
419}
420
421static unsigned int dw_i2c_set_bus_speed(struct i2c_adapter *adap,
422 unsigned int speed)
423{
424 adap->speed = speed;
425 return __dw_i2c_set_bus_speed(i2c_get_base(adap), NULL, speed);
426}
427
428static void dw_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
429{
430 __dw_i2c_init(i2c_get_base(adap), speed, slaveaddr);
431}
432
433static int dw_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr,
434 int alen, u8 *buffer, int len)
435{
436 return __dw_i2c_read(i2c_get_base(adap), dev, addr, alen, buffer, len);
437}
438
439static int dw_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr,
440 int alen, u8 *buffer, int len)
441{
442 return __dw_i2c_write(i2c_get_base(adap), dev, addr, alen, buffer, len);
443}
444
445
446static int dw_i2c_probe(struct i2c_adapter *adap, u8 dev)
447{
448 struct i2c_regs *i2c_base = i2c_get_base(adap);
449 u32 tmp;
450 int ret;
451
452
453
454
455 ret = __dw_i2c_read(i2c_base, dev, 0, 1, (uchar *)&tmp, 1);
456 if (ret)
457 dw_i2c_init(adap, adap->speed, adap->slaveaddr);
458
459 return ret;
460}
461
462U_BOOT_I2C_ADAP_COMPLETE(dw_0, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
463 dw_i2c_write, dw_i2c_set_bus_speed,
464 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0)
465
466#if CONFIG_SYS_I2C_BUS_MAX >= 2
467U_BOOT_I2C_ADAP_COMPLETE(dw_1, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
468 dw_i2c_write, dw_i2c_set_bus_speed,
469 CONFIG_SYS_I2C_SPEED1, CONFIG_SYS_I2C_SLAVE1, 1)
470#endif
471
472#if CONFIG_SYS_I2C_BUS_MAX >= 3
473U_BOOT_I2C_ADAP_COMPLETE(dw_2, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
474 dw_i2c_write, dw_i2c_set_bus_speed,
475 CONFIG_SYS_I2C_SPEED2, CONFIG_SYS_I2C_SLAVE2, 2)
476#endif
477
478#if CONFIG_SYS_I2C_BUS_MAX >= 4
479U_BOOT_I2C_ADAP_COMPLETE(dw_3, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
480 dw_i2c_write, dw_i2c_set_bus_speed,
481 CONFIG_SYS_I2C_SPEED3, CONFIG_SYS_I2C_SLAVE3, 3)
482#endif
483
484#else
485
486
487static int designware_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
488 int nmsgs)
489{
490 struct dw_i2c *i2c = dev_get_priv(bus);
491 int ret;
492
493 debug("i2c_xfer: %d messages\n", nmsgs);
494 for (; nmsgs > 0; nmsgs--, msg++) {
495 debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
496 if (msg->flags & I2C_M_RD) {
497 ret = __dw_i2c_read(i2c->regs, msg->addr, 0, 0,
498 msg->buf, msg->len);
499 } else {
500 ret = __dw_i2c_write(i2c->regs, msg->addr, 0, 0,
501 msg->buf, msg->len);
502 }
503 if (ret) {
504 debug("i2c_write: error sending\n");
505 return -EREMOTEIO;
506 }
507 }
508
509 return 0;
510}
511
512static int designware_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
513{
514 struct dw_i2c *i2c = dev_get_priv(bus);
515
516 return __dw_i2c_set_bus_speed(i2c->regs, i2c->scl_sda_cfg, speed);
517}
518
519static int designware_i2c_probe_chip(struct udevice *bus, uint chip_addr,
520 uint chip_flags)
521{
522 struct dw_i2c *i2c = dev_get_priv(bus);
523 struct i2c_regs *i2c_base = i2c->regs;
524 u32 tmp;
525 int ret;
526
527
528 ret = __dw_i2c_read(i2c_base, chip_addr, 0, 1, (uchar *)&tmp, 1);
529 if (ret)
530 __dw_i2c_init(i2c_base, 0, 0);
531
532 return ret;
533}
534
535static int designware_i2c_probe(struct udevice *bus)
536{
537 struct dw_i2c *priv = dev_get_priv(bus);
538 int ret;
539
540 if (device_is_on_pci_bus(bus)) {
541#ifdef CONFIG_DM_PCI
542
543 priv->regs = (struct i2c_regs *)
544 dm_pci_map_bar(bus, PCI_BASE_ADDRESS_0, PCI_REGION_MEM);
545#ifdef CONFIG_X86
546
547 priv->scl_sda_cfg = &byt_config;
548#endif
549#endif
550 } else {
551 priv->regs = (struct i2c_regs *)devfdt_get_addr_ptr(bus);
552 }
553
554 ret = reset_get_by_name(bus, "i2c", &priv->reset_ctl);
555 if (ret)
556 pr_info("reset_get_by_name() failed: %d\n", ret);
557
558 if (&priv->reset_ctl)
559 reset_deassert(&priv->reset_ctl);
560
561 __dw_i2c_init(priv->regs, 0, 0);
562
563 return 0;
564}
565
566static int designware_i2c_bind(struct udevice *dev)
567{
568 static int num_cards;
569 char name[20];
570
571
572 if (device_is_on_pci_bus(dev)) {
573
574
575
576
577
578
579
580
581 dev->req_seq = num_cards;
582 sprintf(name, "i2c_designware#%u", num_cards++);
583 device_set_name(dev, name);
584 }
585
586 return 0;
587}
588
589static const struct dm_i2c_ops designware_i2c_ops = {
590 .xfer = designware_i2c_xfer,
591 .probe_chip = designware_i2c_probe_chip,
592 .set_bus_speed = designware_i2c_set_bus_speed,
593};
594
595static const struct udevice_id designware_i2c_ids[] = {
596 { .compatible = "snps,designware-i2c" },
597 { }
598};
599
600U_BOOT_DRIVER(i2c_designware) = {
601 .name = "i2c_designware",
602 .id = UCLASS_I2C,
603 .of_match = designware_i2c_ids,
604 .bind = designware_i2c_bind,
605 .probe = designware_i2c_probe,
606 .priv_auto_alloc_size = sizeof(struct dw_i2c),
607 .ops = &designware_i2c_ops,
608};
609
610#ifdef CONFIG_X86
611static struct pci_device_id designware_pci_supported[] = {
612
613 { PCI_VDEVICE(INTEL, 0x0f41) },
614 { PCI_VDEVICE(INTEL, 0x0f42) },
615 { PCI_VDEVICE(INTEL, 0x0f43) },
616 { PCI_VDEVICE(INTEL, 0x0f44) },
617 { PCI_VDEVICE(INTEL, 0x0f45) },
618 { PCI_VDEVICE(INTEL, 0x0f46) },
619 { PCI_VDEVICE(INTEL, 0x0f47) },
620 {},
621};
622
623U_BOOT_PCI_DEVICE(i2c_designware, designware_pci_supported);
624#endif
625
626#endif
627