1 // SPDX-License-Identifier: GPL-2.0+
4 * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com.
13 #include "designware_i2c.h"
15 struct dw_scl_sda_cfg {
24 /* BayTrail HCNT/LCNT/SDA hold time */
25 static struct dw_scl_sda_cfg byt_config = {
35 struct i2c_regs *regs;
36 struct dw_scl_sda_cfg *scl_sda_cfg;
37 struct reset_ctl_bulk resets;
40 #ifdef CONFIG_SYS_I2C_DW_ENABLE_STATUS_UNSUPPORTED
41 static int dw_i2c_enable(struct i2c_regs *i2c_base, bool enable)
43 u32 ena = enable ? IC_ENABLE_0B : 0;
45 writel(ena, &i2c_base->ic_enable);
50 static int dw_i2c_enable(struct i2c_regs *i2c_base, bool enable)
52 u32 ena = enable ? IC_ENABLE_0B : 0;
56 writel(ena, &i2c_base->ic_enable);
57 if ((readl(&i2c_base->ic_enable_status) & IC_ENABLE_0B) == ena)
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.
67 printf("timeout in %sabling I2C adapter\n", enable ? "en" : "dis");
74 * i2c_set_bus_speed - Set the i2c speed
75 * @speed: required i2c speed
79 static unsigned int __dw_i2c_set_bus_speed(struct i2c_regs *i2c_base,
80 struct dw_scl_sda_cfg *scl_sda_cfg,
84 unsigned int hcnt, lcnt;
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;
92 i2c_spd = IC_SPEED_MODE_STANDARD;
94 /* to set speed cltr must be disabled */
95 dw_i2c_enable(i2c_base, false);
97 cntl = (readl(&i2c_base->ic_con) & (~IC_CON_SPD_MSK));
100 #ifndef CONFIG_X86 /* No High-speed for BayTrail yet */
101 case IC_SPEED_MODE_MAX:
102 cntl |= IC_CON_SPD_SS;
104 hcnt = scl_sda_cfg->fs_hcnt;
105 lcnt = scl_sda_cfg->fs_lcnt;
107 hcnt = (IC_CLK * MIN_HS_SCL_HIGHTIME) / NANO_TO_MICRO;
108 lcnt = (IC_CLK * MIN_HS_SCL_LOWTIME) / NANO_TO_MICRO;
110 writel(hcnt, &i2c_base->ic_hs_scl_hcnt);
111 writel(lcnt, &i2c_base->ic_hs_scl_lcnt);
115 case IC_SPEED_MODE_STANDARD:
116 cntl |= IC_CON_SPD_SS;
118 hcnt = scl_sda_cfg->ss_hcnt;
119 lcnt = scl_sda_cfg->ss_lcnt;
121 hcnt = (IC_CLK * MIN_SS_SCL_HIGHTIME) / NANO_TO_MICRO;
122 lcnt = (IC_CLK * MIN_SS_SCL_LOWTIME) / NANO_TO_MICRO;
124 writel(hcnt, &i2c_base->ic_ss_scl_hcnt);
125 writel(lcnt, &i2c_base->ic_ss_scl_lcnt);
128 case IC_SPEED_MODE_FAST:
130 cntl |= IC_CON_SPD_FS;
132 hcnt = scl_sda_cfg->fs_hcnt;
133 lcnt = scl_sda_cfg->fs_lcnt;
135 hcnt = (IC_CLK * MIN_FS_SCL_HIGHTIME) / NANO_TO_MICRO;
136 lcnt = (IC_CLK * MIN_FS_SCL_LOWTIME) / NANO_TO_MICRO;
138 writel(hcnt, &i2c_base->ic_fs_scl_hcnt);
139 writel(lcnt, &i2c_base->ic_fs_scl_lcnt);
143 writel(cntl, &i2c_base->ic_con);
145 /* Configure SDA Hold Time if required */
147 writel(scl_sda_cfg->sda_hold, &i2c_base->ic_sda_hold);
149 /* Enable back i2c now speed set */
150 dw_i2c_enable(i2c_base, true);
156 * i2c_setaddress - Sets the target slave address
157 * @i2c_addr: target i2c address
159 * Sets the target slave address.
161 static void i2c_setaddress(struct i2c_regs *i2c_base, unsigned int i2c_addr)
164 dw_i2c_enable(i2c_base, false);
166 writel(i2c_addr, &i2c_base->ic_tar);
169 dw_i2c_enable(i2c_base, true);
173 * i2c_flush_rxfifo - Flushes the i2c RX FIFO
175 * Flushes the i2c RX FIFO
177 static void i2c_flush_rxfifo(struct i2c_regs *i2c_base)
179 while (readl(&i2c_base->ic_status) & IC_STATUS_RFNE)
180 readl(&i2c_base->ic_cmd_data);
184 * i2c_wait_for_bb - Waits for bus busy
188 static int i2c_wait_for_bb(struct i2c_regs *i2c_base)
190 unsigned long start_time_bb = get_timer(0);
192 while ((readl(&i2c_base->ic_status) & IC_STATUS_MA) ||
193 !(readl(&i2c_base->ic_status) & IC_STATUS_TFE)) {
195 /* Evaluate timeout */
196 if (get_timer(start_time_bb) > (unsigned long)(I2C_BYTE_TO_BB))
203 static int i2c_xfer_init(struct i2c_regs *i2c_base, uchar chip, uint addr,
206 if (i2c_wait_for_bb(i2c_base))
209 i2c_setaddress(i2c_base, chip);
212 /* high byte address going out first */
213 writel((addr >> (alen * 8)) & 0xff,
214 &i2c_base->ic_cmd_data);
219 static int i2c_xfer_finish(struct i2c_regs *i2c_base)
221 ulong start_stop_det = get_timer(0);
224 if ((readl(&i2c_base->ic_raw_intr_stat) & IC_STOP_DET)) {
225 readl(&i2c_base->ic_clr_stop_det);
227 } else if (get_timer(start_stop_det) > I2C_STOPDET_TO) {
232 if (i2c_wait_for_bb(i2c_base)) {
233 printf("Timed out waiting for bus\n");
237 i2c_flush_rxfifo(i2c_base);
243 * i2c_read - Read from i2c memory
244 * @chip: target i2c address
245 * @addr: address to read from
247 * @buffer: buffer for read data
248 * @len: no of bytes to be read
250 * Read from i2c memory.
252 static int __dw_i2c_read(struct i2c_regs *i2c_base, u8 dev, uint addr,
253 int alen, u8 *buffer, int len)
255 unsigned long start_time_rx;
256 unsigned int active = 0;
258 #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
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.
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.
270 dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
271 addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
273 debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
277 if (i2c_xfer_init(i2c_base, dev, addr, alen))
280 start_time_rx = get_timer(0);
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.
291 writel(IC_CMD | IC_STOP, &i2c_base->ic_cmd_data);
293 writel(IC_CMD, &i2c_base->ic_cmd_data);
297 if (readl(&i2c_base->ic_status) & IC_STATUS_RFNE) {
298 *buffer++ = (uchar)readl(&i2c_base->ic_cmd_data);
300 start_time_rx = get_timer(0);
302 } else if (get_timer(start_time_rx) > I2C_BYTE_TO) {
307 return i2c_xfer_finish(i2c_base);
311 * i2c_write - Write to i2c memory
312 * @chip: target i2c address
313 * @addr: address to read from
315 * @buffer: buffer for read data
316 * @len: no of bytes to be read
318 * Write to i2c memory.
320 static int __dw_i2c_write(struct i2c_regs *i2c_base, u8 dev, uint addr,
321 int alen, u8 *buffer, int len)
324 unsigned long start_time_tx;
326 #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
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.
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.
338 dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
339 addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
341 debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
345 if (i2c_xfer_init(i2c_base, dev, addr, alen))
348 start_time_tx = get_timer(0);
350 if (readl(&i2c_base->ic_status) & IC_STATUS_TFNF) {
352 writel(*buffer | IC_STOP,
353 &i2c_base->ic_cmd_data);
355 writel(*buffer, &i2c_base->ic_cmd_data);
358 start_time_tx = get_timer(0);
360 } else if (get_timer(start_time_tx) > (nb * I2C_BYTE_TO)) {
361 printf("Timed out. i2c write Failed\n");
366 return i2c_xfer_finish(i2c_base);
370 * __dw_i2c_init - Init function
371 * @speed: required i2c speed
372 * @slaveaddr: slave address for the device
374 * Initialization function.
376 static int __dw_i2c_init(struct i2c_regs *i2c_base, int speed, int slaveaddr)
381 ret = dw_i2c_enable(i2c_base, false);
385 writel(IC_CON_SD | IC_CON_RE | IC_CON_SPD_FS | IC_CON_MM,
387 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
391 __dw_i2c_set_bus_speed(i2c_base, NULL, speed);
392 writel(slaveaddr, &i2c_base->ic_sar);
396 ret = dw_i2c_enable(i2c_base, true);
403 #ifndef CONFIG_DM_I2C
405 * The legacy I2C functions. These need to get removed once
406 * all users of this driver are converted to DM.
408 static struct i2c_regs *i2c_get_base(struct i2c_adapter *adap)
410 switch (adap->hwadapnr) {
411 #if CONFIG_SYS_I2C_BUS_MAX >= 4
413 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE3;
415 #if CONFIG_SYS_I2C_BUS_MAX >= 3
417 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE2;
419 #if CONFIG_SYS_I2C_BUS_MAX >= 2
421 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE1;
424 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE;
426 printf("Wrong I2C-adapter number %d\n", adap->hwadapnr);
432 static unsigned int dw_i2c_set_bus_speed(struct i2c_adapter *adap,
436 return __dw_i2c_set_bus_speed(i2c_get_base(adap), NULL, speed);
439 static void dw_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
441 __dw_i2c_init(i2c_get_base(adap), speed, slaveaddr);
444 static int dw_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr,
445 int alen, u8 *buffer, int len)
447 return __dw_i2c_read(i2c_get_base(adap), dev, addr, alen, buffer, len);
450 static int dw_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr,
451 int alen, u8 *buffer, int len)
453 return __dw_i2c_write(i2c_get_base(adap), dev, addr, alen, buffer, len);
456 /* dw_i2c_probe - Probe the i2c chip */
457 static int dw_i2c_probe(struct i2c_adapter *adap, u8 dev)
459 struct i2c_regs *i2c_base = i2c_get_base(adap);
464 * Try to read the first location of the chip.
466 ret = __dw_i2c_read(i2c_base, dev, 0, 1, (uchar *)&tmp, 1);
468 dw_i2c_init(adap, adap->speed, adap->slaveaddr);
473 U_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)
477 #if CONFIG_SYS_I2C_BUS_MAX >= 2
478 U_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)
483 #if CONFIG_SYS_I2C_BUS_MAX >= 3
484 U_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)
489 #if CONFIG_SYS_I2C_BUS_MAX >= 4
490 U_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)
495 #else /* CONFIG_DM_I2C */
496 /* The DM I2C functions */
498 static int designware_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
501 struct dw_i2c *i2c = dev_get_priv(bus);
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,
511 ret = __dw_i2c_write(i2c->regs, msg->addr, 0, 0,
515 debug("i2c_write: error sending\n");
523 static int designware_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
525 struct dw_i2c *i2c = dev_get_priv(bus);
527 return __dw_i2c_set_bus_speed(i2c->regs, i2c->scl_sda_cfg, speed);
530 static int designware_i2c_probe_chip(struct udevice *bus, uint chip_addr,
533 struct dw_i2c *i2c = dev_get_priv(bus);
534 struct i2c_regs *i2c_base = i2c->regs;
538 /* Try to read the first location of the chip */
539 ret = __dw_i2c_read(i2c_base, chip_addr, 0, 1, (uchar *)&tmp, 1);
541 __dw_i2c_init(i2c_base, 0, 0);
546 static int designware_i2c_probe(struct udevice *bus)
548 struct dw_i2c *priv = dev_get_priv(bus);
551 if (device_is_on_pci_bus(bus)) {
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);
557 /* Use BayTrail specific timing values */
558 priv->scl_sda_cfg = &byt_config;
562 priv->regs = (struct i2c_regs *)devfdt_get_addr_ptr(bus);
565 ret = reset_get_bulk(bus, &priv->resets);
567 dev_warn(bus, "Can't get reset: %d\n", ret);
569 reset_deassert_bulk(&priv->resets);
571 return __dw_i2c_init(priv->regs, 0, 0);
574 static int designware_i2c_remove(struct udevice *dev)
576 struct dw_i2c *priv = dev_get_priv(dev);
578 return reset_release_bulk(&priv->resets);
581 static int designware_i2c_bind(struct udevice *dev)
583 static int num_cards;
586 /* Create a unique device name for PCI type devices */
587 if (device_is_on_pci_bus(dev)) {
590 * Setting req_seq in the driver is probably not recommended.
591 * But without a DT alias the number is not configured. And
592 * using this driver is impossible for PCIe I2C devices.
593 * This can be removed, once a better (correct) way for this
594 * is found and implemented.
596 dev->req_seq = num_cards;
597 sprintf(name, "i2c_designware#%u", num_cards++);
598 device_set_name(dev, name);
604 static const struct dm_i2c_ops designware_i2c_ops = {
605 .xfer = designware_i2c_xfer,
606 .probe_chip = designware_i2c_probe_chip,
607 .set_bus_speed = designware_i2c_set_bus_speed,
610 static const struct udevice_id designware_i2c_ids[] = {
611 { .compatible = "snps,designware-i2c" },
615 U_BOOT_DRIVER(i2c_designware) = {
616 .name = "i2c_designware",
618 .of_match = designware_i2c_ids,
619 .bind = designware_i2c_bind,
620 .probe = designware_i2c_probe,
621 .priv_auto_alloc_size = sizeof(struct dw_i2c),
622 .remove = designware_i2c_remove,
623 .flags = DM_FLAG_OS_PREPARE,
624 .ops = &designware_i2c_ops,
628 static struct pci_device_id designware_pci_supported[] = {
629 /* Intel BayTrail has 7 I2C controller located on the PCI bus */
630 { PCI_VDEVICE(INTEL, 0x0f41) },
631 { PCI_VDEVICE(INTEL, 0x0f42) },
632 { PCI_VDEVICE(INTEL, 0x0f43) },
633 { PCI_VDEVICE(INTEL, 0x0f44) },
634 { PCI_VDEVICE(INTEL, 0x0f45) },
635 { PCI_VDEVICE(INTEL, 0x0f46) },
636 { PCI_VDEVICE(INTEL, 0x0f47) },
640 U_BOOT_PCI_DEVICE(i2c_designware, designware_pci_supported);
643 #endif /* CONFIG_DM_I2C */