1 // SPDX-License-Identifier: GPL-2.0+
4 * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com.
14 #include "designware_i2c.h"
16 #ifdef CONFIG_SYS_I2C_DW_ENABLE_STATUS_UNSUPPORTED
17 static int dw_i2c_enable(struct i2c_regs *i2c_base, bool enable)
19 u32 ena = enable ? IC_ENABLE_0B : 0;
21 writel(ena, &i2c_base->ic_enable);
26 static int dw_i2c_enable(struct i2c_regs *i2c_base, bool enable)
28 u32 ena = enable ? IC_ENABLE_0B : 0;
32 writel(ena, &i2c_base->ic_enable);
33 if ((readl(&i2c_base->ic_enable_status) & IC_ENABLE_0B) == ena)
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.
43 printf("timeout in %sabling I2C adapter\n", enable ? "en" : "dis");
50 * i2c_set_bus_speed - Set the i2c speed
51 * @speed: required i2c speed
55 static unsigned int __dw_i2c_set_bus_speed(struct i2c_regs *i2c_base,
56 struct dw_scl_sda_cfg *scl_sda_cfg,
60 ulong bus_khz = bus_clk / 1000;
61 enum i2c_speed_mode i2c_spd;
63 unsigned int hcnt, lcnt;
66 /* Allow high speed if there is no config, or the config allows it */
67 if (speed >= I2C_HIGH_SPEED &&
68 (!scl_sda_cfg || scl_sda_cfg->has_high_speed))
69 i2c_spd = IC_SPEED_MODE_HIGH;
70 else if (speed >= I2C_FAST_SPEED)
71 i2c_spd = IC_SPEED_MODE_FAST;
73 i2c_spd = IC_SPEED_MODE_STANDARD;
75 /* Get enable setting for restore later */
76 ena = readl(&i2c_base->ic_enable) & IC_ENABLE_0B;
78 /* to set speed cltr must be disabled */
79 dw_i2c_enable(i2c_base, false);
81 cntl = (readl(&i2c_base->ic_con) & (~IC_CON_SPD_MSK));
84 case IC_SPEED_MODE_HIGH:
85 cntl |= IC_CON_SPD_SS;
87 hcnt = scl_sda_cfg->fs_hcnt;
88 lcnt = scl_sda_cfg->fs_lcnt;
90 hcnt = (bus_khz * MIN_HS_SCL_HIGHTIME) / NANO_TO_KILO;
91 lcnt = (bus_khz * MIN_HS_SCL_LOWTIME) / NANO_TO_KILO;
93 writel(hcnt, &i2c_base->ic_hs_scl_hcnt);
94 writel(lcnt, &i2c_base->ic_hs_scl_lcnt);
97 case IC_SPEED_MODE_STANDARD:
98 cntl |= IC_CON_SPD_SS;
100 hcnt = scl_sda_cfg->ss_hcnt;
101 lcnt = scl_sda_cfg->ss_lcnt;
103 hcnt = (bus_khz * MIN_SS_SCL_HIGHTIME) / NANO_TO_KILO;
104 lcnt = (bus_khz * MIN_SS_SCL_LOWTIME) / NANO_TO_KILO;
106 writel(hcnt, &i2c_base->ic_ss_scl_hcnt);
107 writel(lcnt, &i2c_base->ic_ss_scl_lcnt);
110 case IC_SPEED_MODE_FAST:
112 cntl |= IC_CON_SPD_FS;
114 hcnt = scl_sda_cfg->fs_hcnt;
115 lcnt = scl_sda_cfg->fs_lcnt;
117 hcnt = (bus_khz * MIN_FS_SCL_HIGHTIME) / NANO_TO_KILO;
118 lcnt = (bus_khz * MIN_FS_SCL_LOWTIME) / NANO_TO_KILO;
120 writel(hcnt, &i2c_base->ic_fs_scl_hcnt);
121 writel(lcnt, &i2c_base->ic_fs_scl_lcnt);
125 writel(cntl, &i2c_base->ic_con);
127 /* Configure SDA Hold Time if required */
129 writel(scl_sda_cfg->sda_hold, &i2c_base->ic_sda_hold);
131 /* Restore back i2c now speed set */
132 if (ena == IC_ENABLE_0B)
133 dw_i2c_enable(i2c_base, true);
139 * i2c_setaddress - Sets the target slave address
140 * @i2c_addr: target i2c address
142 * Sets the target slave address.
144 static void i2c_setaddress(struct i2c_regs *i2c_base, unsigned int i2c_addr)
147 dw_i2c_enable(i2c_base, false);
149 writel(i2c_addr, &i2c_base->ic_tar);
152 dw_i2c_enable(i2c_base, true);
156 * i2c_flush_rxfifo - Flushes the i2c RX FIFO
158 * Flushes the i2c RX FIFO
160 static void i2c_flush_rxfifo(struct i2c_regs *i2c_base)
162 while (readl(&i2c_base->ic_status) & IC_STATUS_RFNE)
163 readl(&i2c_base->ic_cmd_data);
167 * i2c_wait_for_bb - Waits for bus busy
171 static int i2c_wait_for_bb(struct i2c_regs *i2c_base)
173 unsigned long start_time_bb = get_timer(0);
175 while ((readl(&i2c_base->ic_status) & IC_STATUS_MA) ||
176 !(readl(&i2c_base->ic_status) & IC_STATUS_TFE)) {
178 /* Evaluate timeout */
179 if (get_timer(start_time_bb) > (unsigned long)(I2C_BYTE_TO_BB))
186 static int i2c_xfer_init(struct i2c_regs *i2c_base, uchar chip, uint addr,
189 if (i2c_wait_for_bb(i2c_base))
192 i2c_setaddress(i2c_base, chip);
195 /* high byte address going out first */
196 writel((addr >> (alen * 8)) & 0xff,
197 &i2c_base->ic_cmd_data);
202 static int i2c_xfer_finish(struct i2c_regs *i2c_base)
204 ulong start_stop_det = get_timer(0);
207 if ((readl(&i2c_base->ic_raw_intr_stat) & IC_STOP_DET)) {
208 readl(&i2c_base->ic_clr_stop_det);
210 } else if (get_timer(start_stop_det) > I2C_STOPDET_TO) {
215 if (i2c_wait_for_bb(i2c_base)) {
216 printf("Timed out waiting for bus\n");
220 i2c_flush_rxfifo(i2c_base);
226 * i2c_read - Read from i2c memory
227 * @chip: target i2c address
228 * @addr: address to read from
230 * @buffer: buffer for read data
231 * @len: no of bytes to be read
233 * Read from i2c memory.
235 static int __dw_i2c_read(struct i2c_regs *i2c_base, u8 dev, uint addr,
236 int alen, u8 *buffer, int len)
238 unsigned long start_time_rx;
239 unsigned int active = 0;
241 #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
243 * EEPROM chips that implement "address overflow" are ones
244 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
245 * address and the extra bits end up in the "chip address"
246 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
247 * four 256 byte chips.
249 * Note that we consider the length of the address field to
250 * still be one byte because the extra address bits are
251 * hidden in the chip address.
253 dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
254 addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
256 debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
260 if (i2c_xfer_init(i2c_base, dev, addr, alen))
263 start_time_rx = get_timer(0);
267 * Avoid writing to ic_cmd_data multiple times
268 * in case this loop spins too quickly and the
269 * ic_status RFNE bit isn't set after the first
270 * write. Subsequent writes to ic_cmd_data can
271 * trigger spurious i2c transfer.
274 writel(IC_CMD | IC_STOP, &i2c_base->ic_cmd_data);
276 writel(IC_CMD, &i2c_base->ic_cmd_data);
280 if (readl(&i2c_base->ic_status) & IC_STATUS_RFNE) {
281 *buffer++ = (uchar)readl(&i2c_base->ic_cmd_data);
283 start_time_rx = get_timer(0);
285 } else if (get_timer(start_time_rx) > I2C_BYTE_TO) {
290 return i2c_xfer_finish(i2c_base);
294 * i2c_write - Write to i2c memory
295 * @chip: target i2c address
296 * @addr: address to read from
298 * @buffer: buffer for read data
299 * @len: no of bytes to be read
301 * Write to i2c memory.
303 static int __dw_i2c_write(struct i2c_regs *i2c_base, u8 dev, uint addr,
304 int alen, u8 *buffer, int len)
307 unsigned long start_time_tx;
309 #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
311 * EEPROM chips that implement "address overflow" are ones
312 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
313 * address and the extra bits end up in the "chip address"
314 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
315 * four 256 byte chips.
317 * Note that we consider the length of the address field to
318 * still be one byte because the extra address bits are
319 * hidden in the chip address.
321 dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
322 addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
324 debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
328 if (i2c_xfer_init(i2c_base, dev, addr, alen))
331 start_time_tx = get_timer(0);
333 if (readl(&i2c_base->ic_status) & IC_STATUS_TFNF) {
335 writel(*buffer | IC_STOP,
336 &i2c_base->ic_cmd_data);
338 writel(*buffer, &i2c_base->ic_cmd_data);
341 start_time_tx = get_timer(0);
343 } else if (get_timer(start_time_tx) > (nb * I2C_BYTE_TO)) {
344 printf("Timed out. i2c write Failed\n");
349 return i2c_xfer_finish(i2c_base);
353 * __dw_i2c_init - Init function
354 * @speed: required i2c speed
355 * @slaveaddr: slave address for the device
357 * Initialization function.
359 static int __dw_i2c_init(struct i2c_regs *i2c_base, int speed, int slaveaddr)
364 ret = dw_i2c_enable(i2c_base, false);
368 writel(IC_CON_SD | IC_CON_RE | IC_CON_SPD_FS | IC_CON_MM,
370 writel(IC_RX_TL, &i2c_base->ic_rx_tl);
371 writel(IC_TX_TL, &i2c_base->ic_tx_tl);
372 writel(IC_STOP_DET, &i2c_base->ic_intr_mask);
373 #ifndef CONFIG_DM_I2C
374 __dw_i2c_set_bus_speed(i2c_base, NULL, speed, IC_CLK);
375 writel(slaveaddr, &i2c_base->ic_sar);
379 ret = dw_i2c_enable(i2c_base, true);
386 #ifndef CONFIG_DM_I2C
388 * The legacy I2C functions. These need to get removed once
389 * all users of this driver are converted to DM.
391 static struct i2c_regs *i2c_get_base(struct i2c_adapter *adap)
393 switch (adap->hwadapnr) {
394 #if CONFIG_SYS_I2C_BUS_MAX >= 4
396 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE3;
398 #if CONFIG_SYS_I2C_BUS_MAX >= 3
400 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE2;
402 #if CONFIG_SYS_I2C_BUS_MAX >= 2
404 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE1;
407 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE;
409 printf("Wrong I2C-adapter number %d\n", adap->hwadapnr);
415 static unsigned int dw_i2c_set_bus_speed(struct i2c_adapter *adap,
419 return __dw_i2c_set_bus_speed(i2c_get_base(adap), NULL, speed, IC_CLK);
422 static void dw_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
424 __dw_i2c_init(i2c_get_base(adap), speed, slaveaddr);
427 static int dw_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr,
428 int alen, u8 *buffer, int len)
430 return __dw_i2c_read(i2c_get_base(adap), dev, addr, alen, buffer, len);
433 static int dw_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr,
434 int alen, u8 *buffer, int len)
436 return __dw_i2c_write(i2c_get_base(adap), dev, addr, alen, buffer, len);
439 /* dw_i2c_probe - Probe the i2c chip */
440 static int dw_i2c_probe(struct i2c_adapter *adap, u8 dev)
442 struct i2c_regs *i2c_base = i2c_get_base(adap);
447 * Try to read the first location of the chip.
449 ret = __dw_i2c_read(i2c_base, dev, 0, 1, (uchar *)&tmp, 1);
451 dw_i2c_init(adap, adap->speed, adap->slaveaddr);
456 U_BOOT_I2C_ADAP_COMPLETE(dw_0, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
457 dw_i2c_write, dw_i2c_set_bus_speed,
458 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0)
460 #if CONFIG_SYS_I2C_BUS_MAX >= 2
461 U_BOOT_I2C_ADAP_COMPLETE(dw_1, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
462 dw_i2c_write, dw_i2c_set_bus_speed,
463 CONFIG_SYS_I2C_SPEED1, CONFIG_SYS_I2C_SLAVE1, 1)
466 #if CONFIG_SYS_I2C_BUS_MAX >= 3
467 U_BOOT_I2C_ADAP_COMPLETE(dw_2, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
468 dw_i2c_write, dw_i2c_set_bus_speed,
469 CONFIG_SYS_I2C_SPEED2, CONFIG_SYS_I2C_SLAVE2, 2)
472 #if CONFIG_SYS_I2C_BUS_MAX >= 4
473 U_BOOT_I2C_ADAP_COMPLETE(dw_3, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
474 dw_i2c_write, dw_i2c_set_bus_speed,
475 CONFIG_SYS_I2C_SPEED3, CONFIG_SYS_I2C_SLAVE3, 3)
478 #else /* CONFIG_DM_I2C */
479 /* The DM I2C functions */
481 static int designware_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
484 struct dw_i2c *i2c = dev_get_priv(bus);
487 debug("i2c_xfer: %d messages\n", nmsgs);
488 for (; nmsgs > 0; nmsgs--, msg++) {
489 debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
490 if (msg->flags & I2C_M_RD) {
491 ret = __dw_i2c_read(i2c->regs, msg->addr, 0, 0,
494 ret = __dw_i2c_write(i2c->regs, msg->addr, 0, 0,
498 debug("i2c_write: error sending\n");
506 static int designware_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
508 struct dw_i2c *i2c = dev_get_priv(bus);
511 #if CONFIG_IS_ENABLED(CLK)
512 rate = clk_get_rate(&i2c->clk);
513 if (IS_ERR_VALUE(rate))
518 return __dw_i2c_set_bus_speed(i2c->regs, i2c->scl_sda_cfg, speed,
522 static int designware_i2c_probe_chip(struct udevice *bus, uint chip_addr,
525 struct dw_i2c *i2c = dev_get_priv(bus);
526 struct i2c_regs *i2c_base = i2c->regs;
530 /* Try to read the first location of the chip */
531 ret = __dw_i2c_read(i2c_base, chip_addr, 0, 1, (uchar *)&tmp, 1);
533 __dw_i2c_init(i2c_base, 0, 0);
538 int designware_i2c_ofdata_to_platdata(struct udevice *bus)
540 struct dw_i2c *priv = dev_get_priv(bus);
543 priv->regs = (struct i2c_regs *)devfdt_get_addr_ptr(bus);
544 dev_read_u32(bus, "i2c-scl-rising-time-ns", &priv->scl_rise_time_ns);
545 dev_read_u32(bus, "i2c-scl-falling-time-ns", &priv->scl_fall_time_ns);
546 dev_read_u32(bus, "i2c-sda-hold-time-ns", &priv->sda_hold_time_ns);
551 int designware_i2c_probe(struct udevice *bus)
553 struct dw_i2c *priv = dev_get_priv(bus);
556 ret = reset_get_bulk(bus, &priv->resets);
558 dev_warn(bus, "Can't get reset: %d\n", ret);
560 reset_deassert_bulk(&priv->resets);
562 #if CONFIG_IS_ENABLED(CLK)
563 ret = clk_get_by_index(bus, 0, &priv->clk);
567 ret = clk_enable(&priv->clk);
568 if (ret && ret != -ENOSYS && ret != -ENOTSUPP) {
569 clk_free(&priv->clk);
570 dev_err(bus, "failed to enable clock\n");
575 return __dw_i2c_init(priv->regs, 0, 0);
578 int designware_i2c_remove(struct udevice *dev)
580 struct dw_i2c *priv = dev_get_priv(dev);
582 #if CONFIG_IS_ENABLED(CLK)
583 clk_disable(&priv->clk);
584 clk_free(&priv->clk);
587 return reset_release_bulk(&priv->resets);
590 const struct dm_i2c_ops designware_i2c_ops = {
591 .xfer = designware_i2c_xfer,
592 .probe_chip = designware_i2c_probe_chip,
593 .set_bus_speed = designware_i2c_set_bus_speed,
596 static const struct udevice_id designware_i2c_ids[] = {
597 { .compatible = "snps,designware-i2c" },
601 U_BOOT_DRIVER(i2c_designware) = {
602 .name = "i2c_designware",
604 .of_match = designware_i2c_ids,
605 .ofdata_to_platdata = designware_i2c_ofdata_to_platdata,
606 .probe = designware_i2c_probe,
607 .priv_auto_alloc_size = sizeof(struct dw_i2c),
608 .remove = designware_i2c_remove,
609 .flags = DM_FLAG_OS_PREPARE,
610 .ops = &designware_i2c_ops,
613 #endif /* CONFIG_DM_I2C */