3 * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com.
5 * SPDX-License-Identifier: GPL-2.0+
10 #include "designware_i2c.h"
12 #ifdef CONFIG_I2C_MULTI_BUS
13 static unsigned int bus_initialized
[CONFIG_SYS_I2C_BUS_MAX
];
14 static unsigned int current_bus
= 0;
17 static struct i2c_regs
*i2c_regs_p
=
18 (struct i2c_regs
*)CONFIG_SYS_I2C_BASE
;
21 * set_speed - Set the i2c speed mode (standard, high, fast)
22 * @i2c_spd: required i2c speed mode
24 * Set the i2c speed mode (standard, high, fast)
26 static void set_speed(int i2c_spd
)
29 unsigned int hcnt
, lcnt
;
32 /* to set speed cltr must be disabled */
33 enbl
= readl(&i2c_regs_p
->ic_enable
);
34 enbl
&= ~IC_ENABLE_0B
;
35 writel(enbl
, &i2c_regs_p
->ic_enable
);
37 cntl
= (readl(&i2c_regs_p
->ic_con
) & (~IC_CON_SPD_MSK
));
40 case IC_SPEED_MODE_MAX
:
41 cntl
|= IC_CON_SPD_HS
;
42 hcnt
= (IC_CLK
* MIN_HS_SCL_HIGHTIME
) / NANO_TO_MICRO
;
43 writel(hcnt
, &i2c_regs_p
->ic_hs_scl_hcnt
);
44 lcnt
= (IC_CLK
* MIN_HS_SCL_LOWTIME
) / NANO_TO_MICRO
;
45 writel(lcnt
, &i2c_regs_p
->ic_hs_scl_lcnt
);
48 case IC_SPEED_MODE_STANDARD
:
49 cntl
|= IC_CON_SPD_SS
;
50 hcnt
= (IC_CLK
* MIN_SS_SCL_HIGHTIME
) / NANO_TO_MICRO
;
51 writel(hcnt
, &i2c_regs_p
->ic_ss_scl_hcnt
);
52 lcnt
= (IC_CLK
* MIN_SS_SCL_LOWTIME
) / NANO_TO_MICRO
;
53 writel(lcnt
, &i2c_regs_p
->ic_ss_scl_lcnt
);
56 case IC_SPEED_MODE_FAST
:
58 cntl
|= IC_CON_SPD_FS
;
59 hcnt
= (IC_CLK
* MIN_FS_SCL_HIGHTIME
) / NANO_TO_MICRO
;
60 writel(hcnt
, &i2c_regs_p
->ic_fs_scl_hcnt
);
61 lcnt
= (IC_CLK
* MIN_FS_SCL_LOWTIME
) / NANO_TO_MICRO
;
62 writel(lcnt
, &i2c_regs_p
->ic_fs_scl_lcnt
);
66 writel(cntl
, &i2c_regs_p
->ic_con
);
68 /* Enable back i2c now speed set */
70 writel(enbl
, &i2c_regs_p
->ic_enable
);
74 * i2c_set_bus_speed - Set the i2c speed
75 * @speed: required i2c speed
79 int i2c_set_bus_speed(int speed
)
81 if (speed
>= I2C_MAX_SPEED
)
82 set_speed(IC_SPEED_MODE_MAX
);
83 else if (speed
>= I2C_FAST_SPEED
)
84 set_speed(IC_SPEED_MODE_FAST
);
86 set_speed(IC_SPEED_MODE_STANDARD
);
92 * i2c_get_bus_speed - Gets the i2c speed
96 int i2c_get_bus_speed(void)
100 cntl
= (readl(&i2c_regs_p
->ic_con
) & IC_CON_SPD_MSK
);
102 if (cntl
== IC_CON_SPD_HS
)
103 return I2C_MAX_SPEED
;
104 else if (cntl
== IC_CON_SPD_FS
)
105 return I2C_FAST_SPEED
;
106 else if (cntl
== IC_CON_SPD_SS
)
107 return I2C_STANDARD_SPEED
;
113 * i2c_init - Init function
114 * @speed: required i2c speed
115 * @slaveadd: slave address for the device
117 * Initialization function.
119 void i2c_init(int speed
, int slaveadd
)
124 enbl
= readl(&i2c_regs_p
->ic_enable
);
125 enbl
&= ~IC_ENABLE_0B
;
126 writel(enbl
, &i2c_regs_p
->ic_enable
);
128 writel((IC_CON_SD
| IC_CON_SPD_FS
| IC_CON_MM
), &i2c_regs_p
->ic_con
);
129 writel(IC_RX_TL
, &i2c_regs_p
->ic_rx_tl
);
130 writel(IC_TX_TL
, &i2c_regs_p
->ic_tx_tl
);
131 i2c_set_bus_speed(speed
);
132 writel(IC_STOP_DET
, &i2c_regs_p
->ic_intr_mask
);
133 writel(slaveadd
, &i2c_regs_p
->ic_sar
);
136 enbl
= readl(&i2c_regs_p
->ic_enable
);
137 enbl
|= IC_ENABLE_0B
;
138 writel(enbl
, &i2c_regs_p
->ic_enable
);
140 #ifdef CONFIG_I2C_MULTI_BUS
141 bus_initialized
[current_bus
] = 1;
146 * i2c_setaddress - Sets the target slave address
147 * @i2c_addr: target i2c address
149 * Sets the target slave address.
151 static void i2c_setaddress(unsigned int i2c_addr
)
156 enbl
= readl(&i2c_regs_p
->ic_enable
);
157 enbl
&= ~IC_ENABLE_0B
;
158 writel(enbl
, &i2c_regs_p
->ic_enable
);
160 writel(i2c_addr
, &i2c_regs_p
->ic_tar
);
163 enbl
= readl(&i2c_regs_p
->ic_enable
);
164 enbl
|= IC_ENABLE_0B
;
165 writel(enbl
, &i2c_regs_p
->ic_enable
);
169 * i2c_flush_rxfifo - Flushes the i2c RX FIFO
171 * Flushes the i2c RX FIFO
173 static void i2c_flush_rxfifo(void)
175 while (readl(&i2c_regs_p
->ic_status
) & IC_STATUS_RFNE
)
176 readl(&i2c_regs_p
->ic_cmd_data
);
180 * i2c_wait_for_bb - Waits for bus busy
184 static int i2c_wait_for_bb(void)
186 unsigned long start_time_bb
= get_timer(0);
188 while ((readl(&i2c_regs_p
->ic_status
) & IC_STATUS_MA
) ||
189 !(readl(&i2c_regs_p
->ic_status
) & IC_STATUS_TFE
)) {
191 /* Evaluate timeout */
192 if (get_timer(start_time_bb
) > (unsigned long)(I2C_BYTE_TO_BB
))
199 static int i2c_xfer_init(uchar chip
, uint addr
, int alen
)
201 if (i2c_wait_for_bb())
204 i2c_setaddress(chip
);
207 /* high byte address going out first */
208 writel((addr
>> (alen
* 8)) & 0xff,
209 &i2c_regs_p
->ic_cmd_data
);
214 static int i2c_xfer_finish(void)
216 ulong start_stop_det
= get_timer(0);
219 if ((readl(&i2c_regs_p
->ic_raw_intr_stat
) & IC_STOP_DET
)) {
220 readl(&i2c_regs_p
->ic_clr_stop_det
);
222 } else if (get_timer(start_stop_det
) > I2C_STOPDET_TO
) {
227 if (i2c_wait_for_bb()) {
228 printf("Timed out waiting for bus\n");
238 * i2c_read - Read from i2c memory
239 * @chip: target i2c address
240 * @addr: address to read from
242 * @buffer: buffer for read data
243 * @len: no of bytes to be read
245 * Read from i2c memory.
247 int i2c_read(uchar chip
, uint addr
, int alen
, uchar
*buffer
, int len
)
249 unsigned long start_time_rx
;
251 #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
253 * EEPROM chips that implement "address overflow" are ones
254 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
255 * address and the extra bits end up in the "chip address"
256 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
257 * four 256 byte chips.
259 * Note that we consider the length of the address field to
260 * still be one byte because the extra address bits are
261 * hidden in the chip address.
263 chip
|= ((addr
>> (alen
* 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
);
264 addr
&= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
<< (alen
* 8));
266 debug("%s: fix addr_overflow: chip %02x addr %02x\n", __func__
, chip
,
270 if (i2c_xfer_init(chip
, addr
, alen
))
273 start_time_rx
= get_timer(0);
276 writel(IC_CMD
| IC_STOP
, &i2c_regs_p
->ic_cmd_data
);
278 writel(IC_CMD
, &i2c_regs_p
->ic_cmd_data
);
280 if (readl(&i2c_regs_p
->ic_status
) & IC_STATUS_RFNE
) {
281 *buffer
++ = (uchar
)readl(&i2c_regs_p
->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();
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 int i2c_write(uchar chip
, uint addr
, int alen
, uchar
*buffer
, int len
)
306 unsigned long start_time_tx
;
308 #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
310 * EEPROM chips that implement "address overflow" are ones
311 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
312 * address and the extra bits end up in the "chip address"
313 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
314 * four 256 byte chips.
316 * Note that we consider the length of the address field to
317 * still be one byte because the extra address bits are
318 * hidden in the chip address.
320 chip
|= ((addr
>> (alen
* 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
);
321 addr
&= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
<< (alen
* 8));
323 debug("%s: fix addr_overflow: chip %02x addr %02x\n", __func__
, chip
,
327 if (i2c_xfer_init(chip
, addr
, alen
))
330 start_time_tx
= get_timer(0);
332 if (readl(&i2c_regs_p
->ic_status
) & IC_STATUS_TFNF
) {
334 writel(*buffer
| IC_STOP
, &i2c_regs_p
->ic_cmd_data
);
336 writel(*buffer
, &i2c_regs_p
->ic_cmd_data
);
338 start_time_tx
= get_timer(0);
340 } else if (get_timer(start_time_tx
) > (nb
* I2C_BYTE_TO
)) {
341 printf("Timed out. i2c write Failed\n");
346 return i2c_xfer_finish();
350 * i2c_probe - Probe the i2c chip
352 int i2c_probe(uchar chip
)
358 * Try to read the first location of the chip.
360 ret
= i2c_read(chip
, 0, 1, (uchar
*)&tmp
, 1);
362 i2c_init(CONFIG_SYS_I2C_SPEED
, CONFIG_SYS_I2C_SLAVE
);
367 #ifdef CONFIG_I2C_MULTI_BUS
368 int i2c_set_bus_num(unsigned int bus
)
372 i2c_regs_p
= (void *)CONFIG_SYS_I2C_BASE
;
374 #ifdef CONFIG_SYS_I2C_BASE1
376 i2c_regs_p
= (void *)CONFIG_SYS_I2C_BASE1
;
379 #ifdef CONFIG_SYS_I2C_BASE2
381 i2c_regs_p
= (void *)CONFIG_SYS_I2C_BASE2
;
384 #ifdef CONFIG_SYS_I2C_BASE3
386 i2c_regs_p
= (void *)CONFIG_SYS_I2C_BASE3
;
389 #ifdef CONFIG_SYS_I2C_BASE4
391 i2c_regs_p
= (void *)CONFIG_SYS_I2C_BASE4
;
394 #ifdef CONFIG_SYS_I2C_BASE5
396 i2c_regs_p
= (void *)CONFIG_SYS_I2C_BASE5
;
399 #ifdef CONFIG_SYS_I2C_BASE6
401 i2c_regs_p
= (void *)CONFIG_SYS_I2C_BASE6
;
404 #ifdef CONFIG_SYS_I2C_BASE7
406 i2c_regs_p
= (void *)CONFIG_SYS_I2C_BASE7
;
409 #ifdef CONFIG_SYS_I2C_BASE8
411 i2c_regs_p
= (void *)CONFIG_SYS_I2C_BASE8
;
414 #ifdef CONFIG_SYS_I2C_BASE9
416 i2c_regs_p
= (void *)CONFIG_SYS_I2C_BASE9
;
420 printf("Bad bus: %d\n", bus
);
426 if (!bus_initialized
[current_bus
])
427 i2c_init(CONFIG_SYS_I2C_SPEED
, CONFIG_SYS_I2C_SLAVE
);
432 int i2c_get_bus_num(void)