blob: 2adc36e3b7d2afbdfd2305ebb485a44c48486a94 [file] [log] [blame]
Vipin KUMAR2403f8f2010-01-15 19:15:44 +05301/*
2 * (C) Copyright 2009
3 * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Vipin KUMAR2403f8f2010-01-15 19:15:44 +05306 */
7
8#include <common.h>
Stefan Roese678398b2014-10-28 12:12:00 +01009#include <i2c.h>
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053010#include <asm/io.h>
Vipin KUMAR031ed2f2012-02-26 23:13:29 +000011#include "designware_i2c.h"
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053012
Stefan Roese1c8b0892016-04-21 08:19:38 +020013static void dw_i2c_enable(struct i2c_regs *i2c_base, bool enable)
14{
15 u32 ena = enable ? IC_ENABLE_0B : 0;
16 int timeout = 100;
17
18 do {
19 writel(ena, &i2c_base->ic_enable);
20 if ((readl(&i2c_base->ic_enable_status) & IC_ENABLE_0B) == ena)
21 return;
22
23 /*
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.
27 */
28 udelay(25);
29 } while (timeout--);
30
31 printf("timeout in %sabling I2C adapter\n", enable ? "en" : "dis");
32}
33
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053034/*
Stefan Roese11b544a2016-04-21 08:19:39 +020035 * i2c_set_bus_speed - Set the i2c speed
36 * @speed: required i2c speed
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053037 *
Stefan Roese11b544a2016-04-21 08:19:39 +020038 * Set the i2c speed.
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053039 */
Stefan Roese3f4358d2016-04-21 08:19:40 +020040static unsigned int __dw_i2c_set_bus_speed(struct i2c_regs *i2c_base,
41 unsigned int speed)
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053042{
43 unsigned int cntl;
44 unsigned int hcnt, lcnt;
Stefan Roese11b544a2016-04-21 08:19:39 +020045 int i2c_spd;
46
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;
51 else
52 i2c_spd = IC_SPEED_MODE_STANDARD;
Armando Visconti5e3e8dd2012-03-29 20:10:17 +000053
54 /* to set speed cltr must be disabled */
Stefan Roese1c8b0892016-04-21 08:19:38 +020055 dw_i2c_enable(i2c_base, false);
Armando Visconti5e3e8dd2012-03-29 20:10:17 +000056
Stefan Roese678398b2014-10-28 12:12:00 +010057 cntl = (readl(&i2c_base->ic_con) & (~IC_CON_SPD_MSK));
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053058
59 switch (i2c_spd) {
60 case IC_SPEED_MODE_MAX:
61 cntl |= IC_CON_SPD_HS;
Armando Visconti5b8439b2012-12-06 00:04:17 +000062 hcnt = (IC_CLK * MIN_HS_SCL_HIGHTIME) / NANO_TO_MICRO;
Stefan Roese678398b2014-10-28 12:12:00 +010063 writel(hcnt, &i2c_base->ic_hs_scl_hcnt);
Armando Visconti5b8439b2012-12-06 00:04:17 +000064 lcnt = (IC_CLK * MIN_HS_SCL_LOWTIME) / NANO_TO_MICRO;
Stefan Roese678398b2014-10-28 12:12:00 +010065 writel(lcnt, &i2c_base->ic_hs_scl_lcnt);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053066 break;
67
68 case IC_SPEED_MODE_STANDARD:
69 cntl |= IC_CON_SPD_SS;
Armando Visconti5b8439b2012-12-06 00:04:17 +000070 hcnt = (IC_CLK * MIN_SS_SCL_HIGHTIME) / NANO_TO_MICRO;
Stefan Roese678398b2014-10-28 12:12:00 +010071 writel(hcnt, &i2c_base->ic_ss_scl_hcnt);
Armando Visconti5b8439b2012-12-06 00:04:17 +000072 lcnt = (IC_CLK * MIN_SS_SCL_LOWTIME) / NANO_TO_MICRO;
Stefan Roese678398b2014-10-28 12:12:00 +010073 writel(lcnt, &i2c_base->ic_ss_scl_lcnt);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053074 break;
75
76 case IC_SPEED_MODE_FAST:
77 default:
78 cntl |= IC_CON_SPD_FS;
Armando Visconti5b8439b2012-12-06 00:04:17 +000079 hcnt = (IC_CLK * MIN_FS_SCL_HIGHTIME) / NANO_TO_MICRO;
Stefan Roese678398b2014-10-28 12:12:00 +010080 writel(hcnt, &i2c_base->ic_fs_scl_hcnt);
Armando Visconti5b8439b2012-12-06 00:04:17 +000081 lcnt = (IC_CLK * MIN_FS_SCL_LOWTIME) / NANO_TO_MICRO;
Stefan Roese678398b2014-10-28 12:12:00 +010082 writel(lcnt, &i2c_base->ic_fs_scl_lcnt);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053083 break;
84 }
85
Stefan Roese678398b2014-10-28 12:12:00 +010086 writel(cntl, &i2c_base->ic_con);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053087
Armando Visconti5b8439b2012-12-06 00:04:17 +000088 /* Enable back i2c now speed set */
Stefan Roese1c8b0892016-04-21 08:19:38 +020089 dw_i2c_enable(i2c_base, true);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053090
Stefan Roese3f4358d2016-04-21 08:19:40 +020091 return 0;
92}
93
94/*
95 * i2c_setaddress - Sets the target slave address
96 * @i2c_addr: target i2c address
97 *
98 * Sets the target slave address.
99 */
100static void i2c_setaddress(struct i2c_regs *i2c_base, unsigned int i2c_addr)
101{
102 /* Disable i2c */
103 dw_i2c_enable(i2c_base, false);
104
105 writel(i2c_addr, &i2c_base->ic_tar);
106
107 /* Enable i2c */
108 dw_i2c_enable(i2c_base, true);
109}
110
111/*
112 * i2c_flush_rxfifo - Flushes the i2c RX FIFO
113 *
114 * Flushes the i2c RX FIFO
115 */
116static void i2c_flush_rxfifo(struct i2c_regs *i2c_base)
117{
118 while (readl(&i2c_base->ic_status) & IC_STATUS_RFNE)
119 readl(&i2c_base->ic_cmd_data);
120}
121
122/*
123 * i2c_wait_for_bb - Waits for bus busy
124 *
125 * Waits for bus busy
126 */
127static int i2c_wait_for_bb(struct i2c_regs *i2c_base)
128{
129 unsigned long start_time_bb = get_timer(0);
130
131 while ((readl(&i2c_base->ic_status) & IC_STATUS_MA) ||
132 !(readl(&i2c_base->ic_status) & IC_STATUS_TFE)) {
133
134 /* Evaluate timeout */
135 if (get_timer(start_time_bb) > (unsigned long)(I2C_BYTE_TO_BB))
136 return 1;
137 }
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530138
139 return 0;
140}
141
Stefan Roese3f4358d2016-04-21 08:19:40 +0200142static int i2c_xfer_init(struct i2c_regs *i2c_base, uchar chip, uint addr,
143 int alen)
144{
145 if (i2c_wait_for_bb(i2c_base))
146 return 1;
147
148 i2c_setaddress(i2c_base, chip);
149 while (alen) {
150 alen--;
151 /* high byte address going out first */
152 writel((addr >> (alen * 8)) & 0xff,
153 &i2c_base->ic_cmd_data);
154 }
155 return 0;
156}
157
158static int i2c_xfer_finish(struct i2c_regs *i2c_base)
159{
160 ulong start_stop_det = get_timer(0);
161
162 while (1) {
163 if ((readl(&i2c_base->ic_raw_intr_stat) & IC_STOP_DET)) {
164 readl(&i2c_base->ic_clr_stop_det);
165 break;
166 } else if (get_timer(start_stop_det) > I2C_STOPDET_TO) {
167 break;
168 }
169 }
170
171 if (i2c_wait_for_bb(i2c_base)) {
172 printf("Timed out waiting for bus\n");
173 return 1;
174 }
175
176 i2c_flush_rxfifo(i2c_base);
177
178 return 0;
179}
180
181/*
182 * i2c_read - Read from i2c memory
183 * @chip: target i2c address
184 * @addr: address to read from
185 * @alen:
186 * @buffer: buffer for read data
187 * @len: no of bytes to be read
188 *
189 * Read from i2c memory.
190 */
191static int __dw_i2c_read(struct i2c_regs *i2c_base, u8 dev, uint addr,
192 int alen, u8 *buffer, int len)
193{
194 unsigned long start_time_rx;
195
196#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
197 /*
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.
203 *
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.
207 */
208 dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
209 addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
210
211 debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
212 addr);
213#endif
214
215 if (i2c_xfer_init(i2c_base, dev, addr, alen))
216 return 1;
217
218 start_time_rx = get_timer(0);
219 while (len) {
220 if (len == 1)
221 writel(IC_CMD | IC_STOP, &i2c_base->ic_cmd_data);
222 else
223 writel(IC_CMD, &i2c_base->ic_cmd_data);
224
225 if (readl(&i2c_base->ic_status) & IC_STATUS_RFNE) {
226 *buffer++ = (uchar)readl(&i2c_base->ic_cmd_data);
227 len--;
228 start_time_rx = get_timer(0);
229
230 } else if (get_timer(start_time_rx) > I2C_BYTE_TO) {
231 return 1;
232 }
233 }
234
235 return i2c_xfer_finish(i2c_base);
236}
237
238/*
239 * i2c_write - Write to i2c memory
240 * @chip: target i2c address
241 * @addr: address to read from
242 * @alen:
243 * @buffer: buffer for read data
244 * @len: no of bytes to be read
245 *
246 * Write to i2c memory.
247 */
248static int __dw_i2c_write(struct i2c_regs *i2c_base, u8 dev, uint addr,
249 int alen, u8 *buffer, int len)
250{
251 int nb = len;
252 unsigned long start_time_tx;
253
254#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
255 /*
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.
261 *
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.
265 */
266 dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
267 addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
268
269 debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
270 addr);
271#endif
272
273 if (i2c_xfer_init(i2c_base, dev, addr, alen))
274 return 1;
275
276 start_time_tx = get_timer(0);
277 while (len) {
278 if (readl(&i2c_base->ic_status) & IC_STATUS_TFNF) {
279 if (--len == 0) {
280 writel(*buffer | IC_STOP,
281 &i2c_base->ic_cmd_data);
282 } else {
283 writel(*buffer, &i2c_base->ic_cmd_data);
284 }
285 buffer++;
286 start_time_tx = get_timer(0);
287
288 } else if (get_timer(start_time_tx) > (nb * I2C_BYTE_TO)) {
289 printf("Timed out. i2c write Failed\n");
290 return 1;
291 }
292 }
293
294 return i2c_xfer_finish(i2c_base);
295}
296
297static struct i2c_regs *i2c_get_base(struct i2c_adapter *adap)
298{
299 switch (adap->hwadapnr) {
300#if CONFIG_SYS_I2C_BUS_MAX >= 4
301 case 3:
302 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE3;
303#endif
304#if CONFIG_SYS_I2C_BUS_MAX >= 3
305 case 2:
306 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE2;
307#endif
308#if CONFIG_SYS_I2C_BUS_MAX >= 2
309 case 1:
310 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE1;
311#endif
312 case 0:
313 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE;
314 default:
315 printf("Wrong I2C-adapter number %d\n", adap->hwadapnr);
316 }
317
318 return NULL;
319}
320
321static unsigned int dw_i2c_set_bus_speed(struct i2c_adapter *adap,
322 unsigned int speed)
323{
324 adap->speed = speed;
325 return __dw_i2c_set_bus_speed(i2c_get_base(adap), speed);
326}
327
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530328/*
329 * i2c_init - Init function
330 * @speed: required i2c speed
Stefan Roese678398b2014-10-28 12:12:00 +0100331 * @slaveaddr: slave address for the device
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530332 *
333 * Initialization function.
334 */
Stefan Roese678398b2014-10-28 12:12:00 +0100335static void dw_i2c_init(struct i2c_adapter *adap, int speed,
336 int slaveaddr)
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530337{
Stefan Roese678398b2014-10-28 12:12:00 +0100338 struct i2c_regs *i2c_base = i2c_get_base(adap);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530339
340 /* Disable i2c */
Stefan Roese1c8b0892016-04-21 08:19:38 +0200341 dw_i2c_enable(i2c_base, false);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530342
Stefan Roese678398b2014-10-28 12:12:00 +0100343 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);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530349
350 /* Enable i2c */
Stefan Roese1c8b0892016-04-21 08:19:38 +0200351 dw_i2c_enable(i2c_base, true);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530352}
353
Stefan Roese678398b2014-10-28 12:12:00 +0100354static int dw_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr,
355 int alen, u8 *buffer, int len)
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530356{
Stefan Roese3f4358d2016-04-21 08:19:40 +0200357 return __dw_i2c_read(i2c_get_base(adap), dev, addr, alen, buffer, len);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530358}
359
Stefan Roese678398b2014-10-28 12:12:00 +0100360static int dw_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr,
361 int alen, u8 *buffer, int len)
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530362{
Stefan Roese3f4358d2016-04-21 08:19:40 +0200363 return __dw_i2c_write(i2c_get_base(adap), dev, addr, alen, buffer, len);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530364}
365
366/*
367 * i2c_probe - Probe the i2c chip
368 */
Stefan Roese678398b2014-10-28 12:12:00 +0100369static int dw_i2c_probe(struct i2c_adapter *adap, u8 dev)
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530370{
Stefan Roese3f4358d2016-04-21 08:19:40 +0200371 struct i2c_regs *i2c_base = i2c_get_base(adap);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530372 u32 tmp;
Stefan Roese496ba482012-01-20 11:52:33 +0100373 int ret;
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530374
375 /*
376 * Try to read the first location of the chip.
377 */
Stefan Roese3f4358d2016-04-21 08:19:40 +0200378 ret = __dw_i2c_read(i2c_base, dev, 0, 1, (uchar *)&tmp, 1);
Stefan Roese496ba482012-01-20 11:52:33 +0100379 if (ret)
Stefan Roese678398b2014-10-28 12:12:00 +0100380 dw_i2c_init(adap, adap->speed, adap->slaveaddr);
Stefan Roese496ba482012-01-20 11:52:33 +0100381
382 return ret;
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530383}
Armando Viscontiac6e2fe2012-12-06 00:04:15 +0000384
Stefan Roese678398b2014-10-28 12:12:00 +0100385U_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)
Armando Viscontiac6e2fe2012-12-06 00:04:15 +0000388
Stefan Roese678398b2014-10-28 12:12:00 +0100389#if CONFIG_SYS_I2C_BUS_MAX >= 2
390U_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)
393#endif
Armando Viscontiac6e2fe2012-12-06 00:04:15 +0000394
Stefan Roese678398b2014-10-28 12:12:00 +0100395#if CONFIG_SYS_I2C_BUS_MAX >= 3
396U_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)
399#endif
Armando Viscontiac6e2fe2012-12-06 00:04:15 +0000400
Stefan Roese678398b2014-10-28 12:12:00 +0100401#if CONFIG_SYS_I2C_BUS_MAX >= 4
402U_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)
Armando Viscontiac6e2fe2012-12-06 00:04:15 +0000405#endif