arm: vf610: fix double iomux configuration for vf610twr board
[u-boot/qq2440-u-boot.git] / drivers / i2c / designware_i2c.c
blobc891ebd39e243ebc5540e7db6291ed88f7e84678
1 /*
2 * (C) Copyright 2009
3 * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com.
5 * SPDX-License-Identifier: GPL-2.0+
6 */
8 #include <common.h>
9 #include <asm/io.h>
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;
15 #endif
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)
28 unsigned int cntl;
29 unsigned int hcnt, lcnt;
30 unsigned int enbl;
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));
39 switch (i2c_spd) {
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);
46 break;
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);
54 break;
56 case IC_SPEED_MODE_FAST:
57 default:
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);
63 break;
66 writel(cntl, &i2c_regs_p->ic_con);
68 /* Enable back i2c now speed set */
69 enbl |= IC_ENABLE_0B;
70 writel(enbl, &i2c_regs_p->ic_enable);
74 * i2c_set_bus_speed - Set the i2c speed
75 * @speed: required i2c speed
77 * Set the 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);
85 else
86 set_speed(IC_SPEED_MODE_STANDARD);
88 return 0;
92 * i2c_get_bus_speed - Gets the i2c speed
94 * Gets the i2c speed.
96 int i2c_get_bus_speed(void)
98 u32 cntl;
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;
109 return 0;
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)
121 unsigned int enbl;
123 /* Disable i2c */
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);
135 /* Enable i2c */
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;
142 #endif
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)
153 unsigned int enbl;
155 /* Disable i2c */
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);
162 /* Enable i2c */
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
182 * 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))
193 return 1;
196 return 0;
199 static int i2c_xfer_init(uchar chip, uint addr, int alen)
201 if (i2c_wait_for_bb())
202 return 1;
204 i2c_setaddress(chip);
205 while (alen) {
206 alen--;
207 /* high byte address going out first */
208 writel((addr >> (alen * 8)) & 0xff,
209 &i2c_regs_p->ic_cmd_data);
211 return 0;
214 static int i2c_xfer_finish(void)
216 ulong start_stop_det = get_timer(0);
218 while (1) {
219 if ((readl(&i2c_regs_p->ic_raw_intr_stat) & IC_STOP_DET)) {
220 readl(&i2c_regs_p->ic_clr_stop_det);
221 break;
222 } else if (get_timer(start_stop_det) > I2C_STOPDET_TO) {
223 break;
227 if (i2c_wait_for_bb()) {
228 printf("Timed out waiting for bus\n");
229 return 1;
232 i2c_flush_rxfifo();
234 return 0;
238 * i2c_read - Read from i2c memory
239 * @chip: target i2c address
240 * @addr: address to read from
241 * @alen:
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,
267 addr);
268 #endif
270 if (i2c_xfer_init(chip, addr, alen))
271 return 1;
273 start_time_rx = get_timer(0);
274 while (len) {
275 if (len == 1)
276 writel(IC_CMD | IC_STOP, &i2c_regs_p->ic_cmd_data);
277 else
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);
282 len--;
283 start_time_rx = get_timer(0);
285 } else if (get_timer(start_time_rx) > I2C_BYTE_TO) {
286 return 1;
290 return i2c_xfer_finish();
294 * i2c_write - Write to i2c memory
295 * @chip: target i2c address
296 * @addr: address to read from
297 * @alen:
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)
305 int nb = 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,
324 addr);
325 #endif
327 if (i2c_xfer_init(chip, addr, alen))
328 return 1;
330 start_time_tx = get_timer(0);
331 while (len) {
332 if (readl(&i2c_regs_p->ic_status) & IC_STATUS_TFNF) {
333 if (--len == 0)
334 writel(*buffer | IC_STOP, &i2c_regs_p->ic_cmd_data);
335 else
336 writel(*buffer, &i2c_regs_p->ic_cmd_data);
337 buffer++;
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");
342 return 1;
346 return i2c_xfer_finish();
350 * i2c_probe - Probe the i2c chip
352 int i2c_probe(uchar chip)
354 u32 tmp;
355 int ret;
358 * Try to read the first location of the chip.
360 ret = i2c_read(chip, 0, 1, (uchar *)&tmp, 1);
361 if (ret)
362 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
364 return ret;
367 #ifdef CONFIG_I2C_MULTI_BUS
368 int i2c_set_bus_num(unsigned int bus)
370 switch (bus) {
371 case 0:
372 i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE;
373 break;
374 #ifdef CONFIG_SYS_I2C_BASE1
375 case 1:
376 i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE1;
377 break;
378 #endif
379 #ifdef CONFIG_SYS_I2C_BASE2
380 case 2:
381 i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE2;
382 break;
383 #endif
384 #ifdef CONFIG_SYS_I2C_BASE3
385 case 3:
386 i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE3;
387 break;
388 #endif
389 #ifdef CONFIG_SYS_I2C_BASE4
390 case 4:
391 i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE4;
392 break;
393 #endif
394 #ifdef CONFIG_SYS_I2C_BASE5
395 case 5:
396 i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE5;
397 break;
398 #endif
399 #ifdef CONFIG_SYS_I2C_BASE6
400 case 6:
401 i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE6;
402 break;
403 #endif
404 #ifdef CONFIG_SYS_I2C_BASE7
405 case 7:
406 i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE7;
407 break;
408 #endif
409 #ifdef CONFIG_SYS_I2C_BASE8
410 case 8:
411 i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE8;
412 break;
413 #endif
414 #ifdef CONFIG_SYS_I2C_BASE9
415 case 9:
416 i2c_regs_p = (void *)CONFIG_SYS_I2C_BASE9;
417 break;
418 #endif
419 default:
420 printf("Bad bus: %d\n", bus);
421 return -1;
424 current_bus = bus;
426 if (!bus_initialized[current_bus])
427 i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
429 return 0;
432 int i2c_get_bus_num(void)
434 return current_bus;
436 #endif