3 * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com.
5 * SPDX-License-Identifier: GPL-2.0+
11 #include "designware_i2c.h"
13 static void dw_i2c_enable(struct i2c_regs *i2c_base, bool enable)
15 u32 ena = enable ? IC_ENABLE_0B : 0;
19 writel(ena, &i2c_base->ic_enable);
20 if ((readl(&i2c_base->ic_enable_status) & IC_ENABLE_0B) == ena)
24 * Wait 10 times the signaling period of the highest I2C
25 * transfer supported by the driver (for 400KHz this is
26 * 25us) as described in the DesignWare I2C databook.
31 printf("timeout in %sabling I2C adapter\n", enable ? "en" : "dis");
35 * i2c_set_bus_speed - Set the i2c speed
36 * @speed: required i2c speed
40 static unsigned int __dw_i2c_set_bus_speed(struct i2c_regs *i2c_base,
44 unsigned int hcnt, lcnt;
47 if (speed >= I2C_MAX_SPEED)
48 i2c_spd = IC_SPEED_MODE_MAX;
49 else if (speed >= I2C_FAST_SPEED)
50 i2c_spd = IC_SPEED_MODE_FAST;
52 i2c_spd = IC_SPEED_MODE_STANDARD;
54 /* to set speed cltr must be disabled */
55 dw_i2c_enable(i2c_base, false);
57 cntl = (readl(&i2c_base->ic_con) & (~IC_CON_SPD_MSK));
60 case IC_SPEED_MODE_MAX:
61 cntl |= IC_CON_SPD_HS;
62 hcnt = (IC_CLK * MIN_HS_SCL_HIGHTIME) / NANO_TO_MICRO;
63 writel(hcnt, &i2c_base->ic_hs_scl_hcnt);
64 lcnt = (IC_CLK * MIN_HS_SCL_LOWTIME) / NANO_TO_MICRO;
65 writel(lcnt, &i2c_base->ic_hs_scl_lcnt);
68 case IC_SPEED_MODE_STANDARD:
69 cntl |= IC_CON_SPD_SS;
70 hcnt = (IC_CLK * MIN_SS_SCL_HIGHTIME) / NANO_TO_MICRO;
71 writel(hcnt, &i2c_base->ic_ss_scl_hcnt);
72 lcnt = (IC_CLK * MIN_SS_SCL_LOWTIME) / NANO_TO_MICRO;
73 writel(lcnt, &i2c_base->ic_ss_scl_lcnt);
76 case IC_SPEED_MODE_FAST:
78 cntl |= IC_CON_SPD_FS;
79 hcnt = (IC_CLK * MIN_FS_SCL_HIGHTIME) / NANO_TO_MICRO;
80 writel(hcnt, &i2c_base->ic_fs_scl_hcnt);
81 lcnt = (IC_CLK * MIN_FS_SCL_LOWTIME) / NANO_TO_MICRO;
82 writel(lcnt, &i2c_base->ic_fs_scl_lcnt);
86 writel(cntl, &i2c_base->ic_con);
88 /* Enable back i2c now speed set */
89 dw_i2c_enable(i2c_base, true);
95 * i2c_setaddress - Sets the target slave address
96 * @i2c_addr: target i2c address
98 * Sets the target slave address.
100 static void i2c_setaddress(struct i2c_regs *i2c_base, unsigned int i2c_addr)
103 dw_i2c_enable(i2c_base, false);
105 writel(i2c_addr, &i2c_base->ic_tar);
108 dw_i2c_enable(i2c_base, true);
112 * i2c_flush_rxfifo - Flushes the i2c RX FIFO
114 * Flushes the i2c RX FIFO
116 static void i2c_flush_rxfifo(struct i2c_regs *i2c_base)
118 while (readl(&i2c_base->ic_status) & IC_STATUS_RFNE)
119 readl(&i2c_base->ic_cmd_data);
123 * i2c_wait_for_bb - Waits for bus busy
127 static int i2c_wait_for_bb(struct i2c_regs *i2c_base)
129 unsigned long start_time_bb = get_timer(0);
131 while ((readl(&i2c_base->ic_status) & IC_STATUS_MA) ||
132 !(readl(&i2c_base->ic_status) & IC_STATUS_TFE)) {
134 /* Evaluate timeout */
135 if (get_timer(start_time_bb) > (unsigned long)(I2C_BYTE_TO_BB))
142 static int i2c_xfer_init(struct i2c_regs *i2c_base, uchar chip, uint addr,
145 if (i2c_wait_for_bb(i2c_base))
148 i2c_setaddress(i2c_base, chip);
151 /* high byte address going out first */
152 writel((addr >> (alen * 8)) & 0xff,
153 &i2c_base->ic_cmd_data);
158 static int i2c_xfer_finish(struct i2c_regs *i2c_base)
160 ulong start_stop_det = get_timer(0);
163 if ((readl(&i2c_base->ic_raw_intr_stat) & IC_STOP_DET)) {
164 readl(&i2c_base->ic_clr_stop_det);
166 } else if (get_timer(start_stop_det) > I2C_STOPDET_TO) {
171 if (i2c_wait_for_bb(i2c_base)) {
172 printf("Timed out waiting for bus\n");
176 i2c_flush_rxfifo(i2c_base);
182 * i2c_read - Read from i2c memory
183 * @chip: target i2c address
184 * @addr: address to read from
186 * @buffer: buffer for read data
187 * @len: no of bytes to be read
189 * Read from i2c memory.
191 static int __dw_i2c_read(struct i2c_regs *i2c_base, u8 dev, uint addr,
192 int alen, u8 *buffer, int len)
194 unsigned long start_time_rx;
196 #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
198 * EEPROM chips that implement "address overflow" are ones
199 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
200 * address and the extra bits end up in the "chip address"
201 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
202 * four 256 byte chips.
204 * Note that we consider the length of the address field to
205 * still be one byte because the extra address bits are
206 * hidden in the chip address.
208 dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
209 addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
211 debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
215 if (i2c_xfer_init(i2c_base, dev, addr, alen))
218 start_time_rx = get_timer(0);
221 writel(IC_CMD | IC_STOP, &i2c_base->ic_cmd_data);
223 writel(IC_CMD, &i2c_base->ic_cmd_data);
225 if (readl(&i2c_base->ic_status) & IC_STATUS_RFNE) {
226 *buffer++ = (uchar)readl(&i2c_base->ic_cmd_data);
228 start_time_rx = get_timer(0);
230 } else if (get_timer(start_time_rx) > I2C_BYTE_TO) {
235 return i2c_xfer_finish(i2c_base);
239 * i2c_write - Write to i2c memory
240 * @chip: target i2c address
241 * @addr: address to read from
243 * @buffer: buffer for read data
244 * @len: no of bytes to be read
246 * Write to i2c memory.
248 static int __dw_i2c_write(struct i2c_regs *i2c_base, u8 dev, uint addr,
249 int alen, u8 *buffer, int len)
252 unsigned long start_time_tx;
254 #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
256 * EEPROM chips that implement "address overflow" are ones
257 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
258 * address and the extra bits end up in the "chip address"
259 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
260 * four 256 byte chips.
262 * Note that we consider the length of the address field to
263 * still be one byte because the extra address bits are
264 * hidden in the chip address.
266 dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
267 addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
269 debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
273 if (i2c_xfer_init(i2c_base, dev, addr, alen))
276 start_time_tx = get_timer(0);
278 if (readl(&i2c_base->ic_status) & IC_STATUS_TFNF) {
280 writel(*buffer | IC_STOP,
281 &i2c_base->ic_cmd_data);
283 writel(*buffer, &i2c_base->ic_cmd_data);
286 start_time_tx = get_timer(0);
288 } else if (get_timer(start_time_tx) > (nb * I2C_BYTE_TO)) {
289 printf("Timed out. i2c write Failed\n");
294 return i2c_xfer_finish(i2c_base);
297 static struct i2c_regs *i2c_get_base(struct i2c_adapter *adap)
299 switch (adap->hwadapnr) {
300 #if CONFIG_SYS_I2C_BUS_MAX >= 4
302 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE3;
304 #if CONFIG_SYS_I2C_BUS_MAX >= 3
306 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE2;
308 #if CONFIG_SYS_I2C_BUS_MAX >= 2
310 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE1;
313 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE;
315 printf("Wrong I2C-adapter number %d\n", adap->hwadapnr);
321 static unsigned int dw_i2c_set_bus_speed(struct i2c_adapter *adap,
325 return __dw_i2c_set_bus_speed(i2c_get_base(adap), speed);
329 * i2c_init - Init function
330 * @speed: required i2c speed
331 * @slaveaddr: slave address for the device
333 * Initialization function.
335 static void dw_i2c_init(struct i2c_adapter *adap, int speed,
338 struct i2c_regs *i2c_base = i2c_get_base(adap);
341 dw_i2c_enable(i2c_base, false);
343 writel((IC_CON_SD | IC_CON_SPD_FS | IC_CON_MM), &i2c_base->ic_con);
344 writel(IC_RX_TL, &i2c_base->ic_rx_tl);
345 writel(IC_TX_TL, &i2c_base->ic_tx_tl);
346 dw_i2c_set_bus_speed(adap, speed);
347 writel(IC_STOP_DET, &i2c_base->ic_intr_mask);
348 writel(slaveaddr, &i2c_base->ic_sar);
351 dw_i2c_enable(i2c_base, true);
354 static int dw_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr,
355 int alen, u8 *buffer, int len)
357 return __dw_i2c_read(i2c_get_base(adap), dev, addr, alen, buffer, len);
360 static int dw_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr,
361 int alen, u8 *buffer, int len)
363 return __dw_i2c_write(i2c_get_base(adap), dev, addr, alen, buffer, len);
367 * i2c_probe - Probe the i2c chip
369 static int dw_i2c_probe(struct i2c_adapter *adap, u8 dev)
371 struct i2c_regs *i2c_base = i2c_get_base(adap);
376 * Try to read the first location of the chip.
378 ret = __dw_i2c_read(i2c_base, dev, 0, 1, (uchar *)&tmp, 1);
380 dw_i2c_init(adap, adap->speed, adap->slaveaddr);
385 U_BOOT_I2C_ADAP_COMPLETE(dw_0, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
386 dw_i2c_write, dw_i2c_set_bus_speed,
387 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0)
389 #if CONFIG_SYS_I2C_BUS_MAX >= 2
390 U_BOOT_I2C_ADAP_COMPLETE(dw_1, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
391 dw_i2c_write, dw_i2c_set_bus_speed,
392 CONFIG_SYS_I2C_SPEED1, CONFIG_SYS_I2C_SLAVE1, 1)
395 #if CONFIG_SYS_I2C_BUS_MAX >= 3
396 U_BOOT_I2C_ADAP_COMPLETE(dw_2, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
397 dw_i2c_write, dw_i2c_set_bus_speed,
398 CONFIG_SYS_I2C_SPEED2, CONFIG_SYS_I2C_SLAVE2, 2)
401 #if CONFIG_SYS_I2C_BUS_MAX >= 4
402 U_BOOT_I2C_ADAP_COMPLETE(dw_3, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
403 dw_i2c_write, dw_i2c_set_bus_speed,
404 CONFIG_SYS_I2C_SPEED3, CONFIG_SYS_I2C_SLAVE3, 3)