blob: d653eff133b6f81066043a1a8038bc66bb526326 [file] [log] [blame]
Vipin KUMARfc9589f2010-01-15 19:15:44 +05301/*
2 * (C) Copyright 2009
3 * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com.
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Vipin KUMARfc9589f2010-01-15 19:15:44 +05306 */
7
8#include <common.h>
Stefan Roese3cb27962016-04-21 08:19:41 +02009#include <dm.h>
Stefan Roeseef6073e2014-10-28 12:12:00 +010010#include <i2c.h>
Vipin KUMARfc9589f2010-01-15 19:15:44 +053011#include <asm/io.h>
Vipin KUMAR3f64acb2012-02-26 23:13:29 +000012#include "designware_i2c.h"
Vipin KUMARfc9589f2010-01-15 19:15:44 +053013
Stefan Roese3cb27962016-04-21 08:19:41 +020014struct dw_i2c {
15 struct i2c_regs *regs;
16};
17
Stefan Roese3bc33ba2016-04-21 08:19:38 +020018static void dw_i2c_enable(struct i2c_regs *i2c_base, bool enable)
19{
20 u32 ena = enable ? IC_ENABLE_0B : 0;
21 int timeout = 100;
22
23 do {
24 writel(ena, &i2c_base->ic_enable);
25 if ((readl(&i2c_base->ic_enable_status) & IC_ENABLE_0B) == ena)
26 return;
27
28 /*
29 * Wait 10 times the signaling period of the highest I2C
30 * transfer supported by the driver (for 400KHz this is
31 * 25us) as described in the DesignWare I2C databook.
32 */
33 udelay(25);
34 } while (timeout--);
35
36 printf("timeout in %sabling I2C adapter\n", enable ? "en" : "dis");
37}
38
Vipin KUMARfc9589f2010-01-15 19:15:44 +053039/*
Stefan Roese88893c92016-04-21 08:19:39 +020040 * i2c_set_bus_speed - Set the i2c speed
41 * @speed: required i2c speed
Vipin KUMARfc9589f2010-01-15 19:15:44 +053042 *
Stefan Roese88893c92016-04-21 08:19:39 +020043 * Set the i2c speed.
Vipin KUMARfc9589f2010-01-15 19:15:44 +053044 */
Stefan Roese41de7662016-04-21 08:19:40 +020045static unsigned int __dw_i2c_set_bus_speed(struct i2c_regs *i2c_base,
46 unsigned int speed)
Vipin KUMARfc9589f2010-01-15 19:15:44 +053047{
48 unsigned int cntl;
49 unsigned int hcnt, lcnt;
Stefan Roese88893c92016-04-21 08:19:39 +020050 int i2c_spd;
51
52 if (speed >= I2C_MAX_SPEED)
53 i2c_spd = IC_SPEED_MODE_MAX;
54 else if (speed >= I2C_FAST_SPEED)
55 i2c_spd = IC_SPEED_MODE_FAST;
56 else
57 i2c_spd = IC_SPEED_MODE_STANDARD;
Armando Visconti631e6932012-03-29 20:10:17 +000058
59 /* to set speed cltr must be disabled */
Stefan Roese3bc33ba2016-04-21 08:19:38 +020060 dw_i2c_enable(i2c_base, false);
Armando Visconti631e6932012-03-29 20:10:17 +000061
Stefan Roeseef6073e2014-10-28 12:12:00 +010062 cntl = (readl(&i2c_base->ic_con) & (~IC_CON_SPD_MSK));
Vipin KUMARfc9589f2010-01-15 19:15:44 +053063
64 switch (i2c_spd) {
65 case IC_SPEED_MODE_MAX:
66 cntl |= IC_CON_SPD_HS;
Armando Visconti28a724f2012-12-06 00:04:17 +000067 hcnt = (IC_CLK * MIN_HS_SCL_HIGHTIME) / NANO_TO_MICRO;
Stefan Roeseef6073e2014-10-28 12:12:00 +010068 writel(hcnt, &i2c_base->ic_hs_scl_hcnt);
Armando Visconti28a724f2012-12-06 00:04:17 +000069 lcnt = (IC_CLK * MIN_HS_SCL_LOWTIME) / NANO_TO_MICRO;
Stefan Roeseef6073e2014-10-28 12:12:00 +010070 writel(lcnt, &i2c_base->ic_hs_scl_lcnt);
Vipin KUMARfc9589f2010-01-15 19:15:44 +053071 break;
72
73 case IC_SPEED_MODE_STANDARD:
74 cntl |= IC_CON_SPD_SS;
Armando Visconti28a724f2012-12-06 00:04:17 +000075 hcnt = (IC_CLK * MIN_SS_SCL_HIGHTIME) / NANO_TO_MICRO;
Stefan Roeseef6073e2014-10-28 12:12:00 +010076 writel(hcnt, &i2c_base->ic_ss_scl_hcnt);
Armando Visconti28a724f2012-12-06 00:04:17 +000077 lcnt = (IC_CLK * MIN_SS_SCL_LOWTIME) / NANO_TO_MICRO;
Stefan Roeseef6073e2014-10-28 12:12:00 +010078 writel(lcnt, &i2c_base->ic_ss_scl_lcnt);
Vipin KUMARfc9589f2010-01-15 19:15:44 +053079 break;
80
81 case IC_SPEED_MODE_FAST:
82 default:
83 cntl |= IC_CON_SPD_FS;
Armando Visconti28a724f2012-12-06 00:04:17 +000084 hcnt = (IC_CLK * MIN_FS_SCL_HIGHTIME) / NANO_TO_MICRO;
Stefan Roeseef6073e2014-10-28 12:12:00 +010085 writel(hcnt, &i2c_base->ic_fs_scl_hcnt);
Armando Visconti28a724f2012-12-06 00:04:17 +000086 lcnt = (IC_CLK * MIN_FS_SCL_LOWTIME) / NANO_TO_MICRO;
Stefan Roeseef6073e2014-10-28 12:12:00 +010087 writel(lcnt, &i2c_base->ic_fs_scl_lcnt);
Vipin KUMARfc9589f2010-01-15 19:15:44 +053088 break;
89 }
90
Stefan Roeseef6073e2014-10-28 12:12:00 +010091 writel(cntl, &i2c_base->ic_con);
Vipin KUMARfc9589f2010-01-15 19:15:44 +053092
Armando Visconti28a724f2012-12-06 00:04:17 +000093 /* Enable back i2c now speed set */
Stefan Roese3bc33ba2016-04-21 08:19:38 +020094 dw_i2c_enable(i2c_base, true);
Stefan Roesef6322ebd2012-01-20 11:52:33 +010095
Vipin KUMARfc9589f2010-01-15 19:15:44 +053096 return 0;
97}
98
99/*
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530100 * i2c_setaddress - Sets the target slave address
101 * @i2c_addr: target i2c address
102 *
103 * Sets the target slave address.
104 */
Stefan Roese41de7662016-04-21 08:19:40 +0200105static void i2c_setaddress(struct i2c_regs *i2c_base, unsigned int i2c_addr)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530106{
Alexey Brodkin41c56552013-11-07 17:52:18 +0400107 /* Disable i2c */
Stefan Roese3bc33ba2016-04-21 08:19:38 +0200108 dw_i2c_enable(i2c_base, false);
Alexey Brodkin41c56552013-11-07 17:52:18 +0400109
Stefan Roeseef6073e2014-10-28 12:12:00 +0100110 writel(i2c_addr, &i2c_base->ic_tar);
Alexey Brodkin41c56552013-11-07 17:52:18 +0400111
112 /* Enable i2c */
Stefan Roese3bc33ba2016-04-21 08:19:38 +0200113 dw_i2c_enable(i2c_base, true);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530114}
115
116/*
117 * i2c_flush_rxfifo - Flushes the i2c RX FIFO
118 *
119 * Flushes the i2c RX FIFO
120 */
Stefan Roese41de7662016-04-21 08:19:40 +0200121static void i2c_flush_rxfifo(struct i2c_regs *i2c_base)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530122{
Stefan Roeseef6073e2014-10-28 12:12:00 +0100123 while (readl(&i2c_base->ic_status) & IC_STATUS_RFNE)
124 readl(&i2c_base->ic_cmd_data);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530125}
126
127/*
128 * i2c_wait_for_bb - Waits for bus busy
129 *
130 * Waits for bus busy
131 */
Stefan Roese41de7662016-04-21 08:19:40 +0200132static int i2c_wait_for_bb(struct i2c_regs *i2c_base)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530133{
134 unsigned long start_time_bb = get_timer(0);
135
Stefan Roeseef6073e2014-10-28 12:12:00 +0100136 while ((readl(&i2c_base->ic_status) & IC_STATUS_MA) ||
137 !(readl(&i2c_base->ic_status) & IC_STATUS_TFE)) {
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530138
139 /* Evaluate timeout */
140 if (get_timer(start_time_bb) > (unsigned long)(I2C_BYTE_TO_BB))
141 return 1;
142 }
143
144 return 0;
145}
146
Stefan Roese41de7662016-04-21 08:19:40 +0200147static int i2c_xfer_init(struct i2c_regs *i2c_base, uchar chip, uint addr,
Stefan Roeseef6073e2014-10-28 12:12:00 +0100148 int alen)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530149{
Stefan Roese41de7662016-04-21 08:19:40 +0200150 if (i2c_wait_for_bb(i2c_base))
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530151 return 1;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530152
Stefan Roese41de7662016-04-21 08:19:40 +0200153 i2c_setaddress(i2c_base, chip);
Chin Liang Seea0c26262014-02-04 11:56:23 -0600154 while (alen) {
155 alen--;
156 /* high byte address going out first */
157 writel((addr >> (alen * 8)) & 0xff,
Stefan Roeseef6073e2014-10-28 12:12:00 +0100158 &i2c_base->ic_cmd_data);
Chin Liang Seea0c26262014-02-04 11:56:23 -0600159 }
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530160 return 0;
161}
162
Stefan Roese41de7662016-04-21 08:19:40 +0200163static int i2c_xfer_finish(struct i2c_regs *i2c_base)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530164{
165 ulong start_stop_det = get_timer(0);
166
167 while (1) {
Stefan Roeseef6073e2014-10-28 12:12:00 +0100168 if ((readl(&i2c_base->ic_raw_intr_stat) & IC_STOP_DET)) {
169 readl(&i2c_base->ic_clr_stop_det);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530170 break;
171 } else if (get_timer(start_stop_det) > I2C_STOPDET_TO) {
172 break;
173 }
174 }
175
Stefan Roese41de7662016-04-21 08:19:40 +0200176 if (i2c_wait_for_bb(i2c_base)) {
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530177 printf("Timed out waiting for bus\n");
178 return 1;
179 }
180
Stefan Roese41de7662016-04-21 08:19:40 +0200181 i2c_flush_rxfifo(i2c_base);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530182
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530183 return 0;
184}
185
186/*
187 * i2c_read - Read from i2c memory
188 * @chip: target i2c address
189 * @addr: address to read from
190 * @alen:
191 * @buffer: buffer for read data
192 * @len: no of bytes to be read
193 *
194 * Read from i2c memory.
195 */
Stefan Roese41de7662016-04-21 08:19:40 +0200196static int __dw_i2c_read(struct i2c_regs *i2c_base, u8 dev, uint addr,
197 int alen, u8 *buffer, int len)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530198{
199 unsigned long start_time_rx;
200
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400201#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
202 /*
203 * EEPROM chips that implement "address overflow" are ones
204 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
205 * address and the extra bits end up in the "chip address"
206 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
207 * four 256 byte chips.
208 *
209 * Note that we consider the length of the address field to
210 * still be one byte because the extra address bits are
211 * hidden in the chip address.
212 */
Stefan Roeseef6073e2014-10-28 12:12:00 +0100213 dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400214 addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
215
Stefan Roeseef6073e2014-10-28 12:12:00 +0100216 debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400217 addr);
218#endif
219
Stefan Roese41de7662016-04-21 08:19:40 +0200220 if (i2c_xfer_init(i2c_base, dev, addr, alen))
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530221 return 1;
222
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530223 start_time_rx = get_timer(0);
224 while (len) {
Armando Visconti6bc6ef72012-12-06 00:04:16 +0000225 if (len == 1)
Stefan Roeseef6073e2014-10-28 12:12:00 +0100226 writel(IC_CMD | IC_STOP, &i2c_base->ic_cmd_data);
Armando Visconti6bc6ef72012-12-06 00:04:16 +0000227 else
Stefan Roeseef6073e2014-10-28 12:12:00 +0100228 writel(IC_CMD, &i2c_base->ic_cmd_data);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530229
Stefan Roeseef6073e2014-10-28 12:12:00 +0100230 if (readl(&i2c_base->ic_status) & IC_STATUS_RFNE) {
231 *buffer++ = (uchar)readl(&i2c_base->ic_cmd_data);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530232 len--;
233 start_time_rx = get_timer(0);
234
235 } else if (get_timer(start_time_rx) > I2C_BYTE_TO) {
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530236 return 1;
237 }
238 }
239
Stefan Roese41de7662016-04-21 08:19:40 +0200240 return i2c_xfer_finish(i2c_base);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530241}
242
243/*
244 * i2c_write - Write to i2c memory
245 * @chip: target i2c address
246 * @addr: address to read from
247 * @alen:
248 * @buffer: buffer for read data
249 * @len: no of bytes to be read
250 *
251 * Write to i2c memory.
252 */
Stefan Roese41de7662016-04-21 08:19:40 +0200253static int __dw_i2c_write(struct i2c_regs *i2c_base, u8 dev, uint addr,
254 int alen, u8 *buffer, int len)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530255{
256 int nb = len;
257 unsigned long start_time_tx;
258
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400259#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
260 /*
261 * EEPROM chips that implement "address overflow" are ones
262 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
263 * address and the extra bits end up in the "chip address"
264 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
265 * four 256 byte chips.
266 *
267 * Note that we consider the length of the address field to
268 * still be one byte because the extra address bits are
269 * hidden in the chip address.
270 */
Stefan Roeseef6073e2014-10-28 12:12:00 +0100271 dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400272 addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
273
Stefan Roeseef6073e2014-10-28 12:12:00 +0100274 debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400275 addr);
276#endif
277
Stefan Roese41de7662016-04-21 08:19:40 +0200278 if (i2c_xfer_init(i2c_base, dev, addr, alen))
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530279 return 1;
280
281 start_time_tx = get_timer(0);
282 while (len) {
Stefan Roeseef6073e2014-10-28 12:12:00 +0100283 if (readl(&i2c_base->ic_status) & IC_STATUS_TFNF) {
284 if (--len == 0) {
285 writel(*buffer | IC_STOP,
286 &i2c_base->ic_cmd_data);
287 } else {
288 writel(*buffer, &i2c_base->ic_cmd_data);
289 }
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530290 buffer++;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530291 start_time_tx = get_timer(0);
292
293 } else if (get_timer(start_time_tx) > (nb * I2C_BYTE_TO)) {
294 printf("Timed out. i2c write Failed\n");
295 return 1;
296 }
297 }
298
Stefan Roese41de7662016-04-21 08:19:40 +0200299 return i2c_xfer_finish(i2c_base);
300}
301
Stefan Roese3cb27962016-04-21 08:19:41 +0200302/*
303 * __dw_i2c_init - Init function
304 * @speed: required i2c speed
305 * @slaveaddr: slave address for the device
306 *
307 * Initialization function.
308 */
309static void __dw_i2c_init(struct i2c_regs *i2c_base, int speed, int slaveaddr)
310{
311 /* Disable i2c */
312 dw_i2c_enable(i2c_base, false);
313
314 writel((IC_CON_SD | IC_CON_SPD_FS | IC_CON_MM), &i2c_base->ic_con);
315 writel(IC_RX_TL, &i2c_base->ic_rx_tl);
316 writel(IC_TX_TL, &i2c_base->ic_tx_tl);
317 writel(IC_STOP_DET, &i2c_base->ic_intr_mask);
318#ifndef CONFIG_DM_I2C
319 __dw_i2c_set_bus_speed(i2c_base, speed);
320 writel(slaveaddr, &i2c_base->ic_sar);
321#endif
322
323 /* Enable i2c */
324 dw_i2c_enable(i2c_base, true);
325}
326
327#ifndef CONFIG_DM_I2C
328/*
329 * The legacy I2C functions. These need to get removed once
330 * all users of this driver are converted to DM.
331 */
Stefan Roese41de7662016-04-21 08:19:40 +0200332static struct i2c_regs *i2c_get_base(struct i2c_adapter *adap)
333{
334 switch (adap->hwadapnr) {
335#if CONFIG_SYS_I2C_BUS_MAX >= 4
336 case 3:
337 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE3;
338#endif
339#if CONFIG_SYS_I2C_BUS_MAX >= 3
340 case 2:
341 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE2;
342#endif
343#if CONFIG_SYS_I2C_BUS_MAX >= 2
344 case 1:
345 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE1;
346#endif
347 case 0:
348 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE;
349 default:
350 printf("Wrong I2C-adapter number %d\n", adap->hwadapnr);
351 }
352
353 return NULL;
354}
355
356static unsigned int dw_i2c_set_bus_speed(struct i2c_adapter *adap,
357 unsigned int speed)
358{
359 adap->speed = speed;
360 return __dw_i2c_set_bus_speed(i2c_get_base(adap), speed);
361}
362
Stefan Roese3cb27962016-04-21 08:19:41 +0200363static void dw_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
Stefan Roese41de7662016-04-21 08:19:40 +0200364{
Stefan Roese3cb27962016-04-21 08:19:41 +0200365 __dw_i2c_init(i2c_get_base(adap), speed, slaveaddr);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530366}
367
Stefan Roese41de7662016-04-21 08:19:40 +0200368static int dw_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr,
369 int alen, u8 *buffer, int len)
370{
371 return __dw_i2c_read(i2c_get_base(adap), dev, addr, alen, buffer, len);
372}
373
374static int dw_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr,
375 int alen, u8 *buffer, int len)
376{
377 return __dw_i2c_write(i2c_get_base(adap), dev, addr, alen, buffer, len);
378}
379
Stefan Roese3cb27962016-04-21 08:19:41 +0200380/* dw_i2c_probe - Probe the i2c chip */
Stefan Roeseef6073e2014-10-28 12:12:00 +0100381static int dw_i2c_probe(struct i2c_adapter *adap, u8 dev)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530382{
Stefan Roese41de7662016-04-21 08:19:40 +0200383 struct i2c_regs *i2c_base = i2c_get_base(adap);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530384 u32 tmp;
Stefan Roesef6322ebd2012-01-20 11:52:33 +0100385 int ret;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530386
387 /*
388 * Try to read the first location of the chip.
389 */
Stefan Roese41de7662016-04-21 08:19:40 +0200390 ret = __dw_i2c_read(i2c_base, dev, 0, 1, (uchar *)&tmp, 1);
Stefan Roesef6322ebd2012-01-20 11:52:33 +0100391 if (ret)
Stefan Roeseef6073e2014-10-28 12:12:00 +0100392 dw_i2c_init(adap, adap->speed, adap->slaveaddr);
Stefan Roesef6322ebd2012-01-20 11:52:33 +0100393
394 return ret;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530395}
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000396
Stefan Roeseef6073e2014-10-28 12:12:00 +0100397U_BOOT_I2C_ADAP_COMPLETE(dw_0, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
398 dw_i2c_write, dw_i2c_set_bus_speed,
399 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0)
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000400
Stefan Roeseef6073e2014-10-28 12:12:00 +0100401#if CONFIG_SYS_I2C_BUS_MAX >= 2
402U_BOOT_I2C_ADAP_COMPLETE(dw_1, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
403 dw_i2c_write, dw_i2c_set_bus_speed,
404 CONFIG_SYS_I2C_SPEED1, CONFIG_SYS_I2C_SLAVE1, 1)
405#endif
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000406
Stefan Roeseef6073e2014-10-28 12:12:00 +0100407#if CONFIG_SYS_I2C_BUS_MAX >= 3
408U_BOOT_I2C_ADAP_COMPLETE(dw_2, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
409 dw_i2c_write, dw_i2c_set_bus_speed,
410 CONFIG_SYS_I2C_SPEED2, CONFIG_SYS_I2C_SLAVE2, 2)
411#endif
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000412
Stefan Roeseef6073e2014-10-28 12:12:00 +0100413#if CONFIG_SYS_I2C_BUS_MAX >= 4
414U_BOOT_I2C_ADAP_COMPLETE(dw_3, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
415 dw_i2c_write, dw_i2c_set_bus_speed,
416 CONFIG_SYS_I2C_SPEED3, CONFIG_SYS_I2C_SLAVE3, 3)
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000417#endif
Stefan Roese3cb27962016-04-21 08:19:41 +0200418
419#else /* CONFIG_DM_I2C */
420/* The DM I2C functions */
421
422static int designware_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
423 int nmsgs)
424{
425 struct dw_i2c *i2c = dev_get_priv(bus);
426 int ret;
427
428 debug("i2c_xfer: %d messages\n", nmsgs);
429 for (; nmsgs > 0; nmsgs--, msg++) {
430 debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
431 if (msg->flags & I2C_M_RD) {
432 ret = __dw_i2c_read(i2c->regs, msg->addr, 0, 0,
433 msg->buf, msg->len);
434 } else {
435 ret = __dw_i2c_write(i2c->regs, msg->addr, 0, 0,
436 msg->buf, msg->len);
437 }
438 if (ret) {
439 debug("i2c_write: error sending\n");
440 return -EREMOTEIO;
441 }
442 }
443
444 return 0;
445}
446
447static int designware_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
448{
449 struct dw_i2c *i2c = dev_get_priv(bus);
450
451 return __dw_i2c_set_bus_speed(i2c->regs, speed);
452}
453
454static int designware_i2c_probe_chip(struct udevice *bus, uint chip_addr,
455 uint chip_flags)
456{
457 struct dw_i2c *i2c = dev_get_priv(bus);
458 struct i2c_regs *i2c_base = i2c->regs;
459 u32 tmp;
460 int ret;
461
462 /* Try to read the first location of the chip */
463 ret = __dw_i2c_read(i2c_base, chip_addr, 0, 1, (uchar *)&tmp, 1);
464 if (ret)
465 __dw_i2c_init(i2c_base, 0, 0);
466
467 return ret;
468}
469
470static int designware_i2c_probe(struct udevice *bus)
471{
472 struct dw_i2c *priv = dev_get_priv(bus);
473
474 /* Save base address from device-tree */
475 priv->regs = (struct i2c_regs *)dev_get_addr(bus);
476
477 __dw_i2c_init(priv->regs, 0, 0);
478
479 return 0;
480}
481
482static const struct dm_i2c_ops designware_i2c_ops = {
483 .xfer = designware_i2c_xfer,
484 .probe_chip = designware_i2c_probe_chip,
485 .set_bus_speed = designware_i2c_set_bus_speed,
486};
487
488static const struct udevice_id designware_i2c_ids[] = {
489 { .compatible = "snps,designware-i2c" },
490 { }
491};
492
493U_BOOT_DRIVER(i2c_designware) = {
494 .name = "i2c_designware",
495 .id = UCLASS_I2C,
496 .of_match = designware_i2c_ids,
497 .probe = designware_i2c_probe,
498 .priv_auto_alloc_size = sizeof(struct dw_i2c),
499 .ops = &designware_i2c_ops,
500};
501
502#endif /* CONFIG_DM_I2C */