blob: 9ccc2411a62a521bbaad9cadcdf46273136d7801 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Vipin KUMARfc9589f2010-01-15 19:15:44 +05302/*
3 * (C) Copyright 2009
4 * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com.
Vipin KUMARfc9589f2010-01-15 19:15:44 +05305 */
6
7#include <common.h>
Stefan Roese3cb27962016-04-21 08:19:41 +02008#include <dm.h>
Stefan Roeseef6073e2014-10-28 12:12:00 +01009#include <i2c.h>
Stefan Roese38481202016-04-21 08:19:42 +020010#include <pci.h>
Dinh Nguyen08794aa2018-04-04 17:18:24 -050011#include <reset.h>
Vipin KUMARfc9589f2010-01-15 19:15:44 +053012#include <asm/io.h>
Vipin KUMAR3f64acb2012-02-26 23:13:29 +000013#include "designware_i2c.h"
Vipin KUMARfc9589f2010-01-15 19:15:44 +053014
Stefan Roese38481202016-04-21 08:19:42 +020015struct dw_scl_sda_cfg {
16 u32 ss_hcnt;
17 u32 fs_hcnt;
18 u32 ss_lcnt;
19 u32 fs_lcnt;
20 u32 sda_hold;
21};
22
23#ifdef CONFIG_X86
24/* BayTrail HCNT/LCNT/SDA hold time */
25static struct dw_scl_sda_cfg byt_config = {
26 .ss_hcnt = 0x200,
27 .fs_hcnt = 0x55,
28 .ss_lcnt = 0x200,
29 .fs_lcnt = 0x99,
30 .sda_hold = 0x6,
31};
32#endif
33
Stefan Roese3cb27962016-04-21 08:19:41 +020034struct dw_i2c {
35 struct i2c_regs *regs;
Stefan Roese38481202016-04-21 08:19:42 +020036 struct dw_scl_sda_cfg *scl_sda_cfg;
Simon Goldschmidt28608a12019-03-28 21:11:48 +010037 struct reset_ctl_bulk resets;
Stefan Roese3cb27962016-04-21 08:19:41 +020038};
39
Stefan Roeseabb3e132016-04-27 09:02:12 +020040#ifdef CONFIG_SYS_I2C_DW_ENABLE_STATUS_UNSUPPORTED
Simon Glassbd9ca8d2019-02-16 20:24:39 -070041static int dw_i2c_enable(struct i2c_regs *i2c_base, bool enable)
Stefan Roese3bc33ba2016-04-21 08:19:38 +020042{
43 u32 ena = enable ? IC_ENABLE_0B : 0;
Stefan Roeseabb3e132016-04-27 09:02:12 +020044
45 writel(ena, &i2c_base->ic_enable);
Simon Glassbd9ca8d2019-02-16 20:24:39 -070046
47 return 0;
Stefan Roeseabb3e132016-04-27 09:02:12 +020048}
49#else
Simon Glassbd9ca8d2019-02-16 20:24:39 -070050static int dw_i2c_enable(struct i2c_regs *i2c_base, bool enable)
Stefan Roeseabb3e132016-04-27 09:02:12 +020051{
52 u32 ena = enable ? IC_ENABLE_0B : 0;
Stefan Roese3bc33ba2016-04-21 08:19:38 +020053 int timeout = 100;
54
55 do {
56 writel(ena, &i2c_base->ic_enable);
57 if ((readl(&i2c_base->ic_enable_status) & IC_ENABLE_0B) == ena)
Simon Glassbd9ca8d2019-02-16 20:24:39 -070058 return 0;
Stefan Roese3bc33ba2016-04-21 08:19:38 +020059
60 /*
61 * Wait 10 times the signaling period of the highest I2C
62 * transfer supported by the driver (for 400KHz this is
63 * 25us) as described in the DesignWare I2C databook.
64 */
65 udelay(25);
66 } while (timeout--);
Stefan Roese3bc33ba2016-04-21 08:19:38 +020067 printf("timeout in %sabling I2C adapter\n", enable ? "en" : "dis");
Simon Glassbd9ca8d2019-02-16 20:24:39 -070068
69 return -ETIMEDOUT;
Stefan Roese3bc33ba2016-04-21 08:19:38 +020070}
Stefan Roeseabb3e132016-04-27 09:02:12 +020071#endif
Stefan Roese3bc33ba2016-04-21 08:19:38 +020072
Vipin KUMARfc9589f2010-01-15 19:15:44 +053073/*
Stefan Roese88893c92016-04-21 08:19:39 +020074 * i2c_set_bus_speed - Set the i2c speed
75 * @speed: required i2c speed
Vipin KUMARfc9589f2010-01-15 19:15:44 +053076 *
Stefan Roese88893c92016-04-21 08:19:39 +020077 * Set the i2c speed.
Vipin KUMARfc9589f2010-01-15 19:15:44 +053078 */
Stefan Roese41de7662016-04-21 08:19:40 +020079static unsigned int __dw_i2c_set_bus_speed(struct i2c_regs *i2c_base,
Stefan Roese38481202016-04-21 08:19:42 +020080 struct dw_scl_sda_cfg *scl_sda_cfg,
Stefan Roese41de7662016-04-21 08:19:40 +020081 unsigned int speed)
Vipin KUMARfc9589f2010-01-15 19:15:44 +053082{
83 unsigned int cntl;
84 unsigned int hcnt, lcnt;
Stefan Roese88893c92016-04-21 08:19:39 +020085 int i2c_spd;
86
87 if (speed >= I2C_MAX_SPEED)
88 i2c_spd = IC_SPEED_MODE_MAX;
89 else if (speed >= I2C_FAST_SPEED)
90 i2c_spd = IC_SPEED_MODE_FAST;
91 else
92 i2c_spd = IC_SPEED_MODE_STANDARD;
Armando Visconti631e6932012-03-29 20:10:17 +000093
94 /* to set speed cltr must be disabled */
Stefan Roese3bc33ba2016-04-21 08:19:38 +020095 dw_i2c_enable(i2c_base, false);
Armando Visconti631e6932012-03-29 20:10:17 +000096
Stefan Roeseef6073e2014-10-28 12:12:00 +010097 cntl = (readl(&i2c_base->ic_con) & (~IC_CON_SPD_MSK));
Vipin KUMARfc9589f2010-01-15 19:15:44 +053098
99 switch (i2c_spd) {
Stefan Roese38481202016-04-21 08:19:42 +0200100#ifndef CONFIG_X86 /* No High-speed for BayTrail yet */
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530101 case IC_SPEED_MODE_MAX:
Stefan Roese38481202016-04-21 08:19:42 +0200102 cntl |= IC_CON_SPD_SS;
103 if (scl_sda_cfg) {
104 hcnt = scl_sda_cfg->fs_hcnt;
105 lcnt = scl_sda_cfg->fs_lcnt;
106 } else {
107 hcnt = (IC_CLK * MIN_HS_SCL_HIGHTIME) / NANO_TO_MICRO;
108 lcnt = (IC_CLK * MIN_HS_SCL_LOWTIME) / NANO_TO_MICRO;
109 }
Stefan Roeseef6073e2014-10-28 12:12:00 +0100110 writel(hcnt, &i2c_base->ic_hs_scl_hcnt);
Stefan Roeseef6073e2014-10-28 12:12:00 +0100111 writel(lcnt, &i2c_base->ic_hs_scl_lcnt);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530112 break;
Stefan Roese38481202016-04-21 08:19:42 +0200113#endif
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530114
115 case IC_SPEED_MODE_STANDARD:
116 cntl |= IC_CON_SPD_SS;
Stefan Roese38481202016-04-21 08:19:42 +0200117 if (scl_sda_cfg) {
118 hcnt = scl_sda_cfg->ss_hcnt;
119 lcnt = scl_sda_cfg->ss_lcnt;
120 } else {
121 hcnt = (IC_CLK * MIN_SS_SCL_HIGHTIME) / NANO_TO_MICRO;
122 lcnt = (IC_CLK * MIN_SS_SCL_LOWTIME) / NANO_TO_MICRO;
123 }
Stefan Roeseef6073e2014-10-28 12:12:00 +0100124 writel(hcnt, &i2c_base->ic_ss_scl_hcnt);
Stefan Roeseef6073e2014-10-28 12:12:00 +0100125 writel(lcnt, &i2c_base->ic_ss_scl_lcnt);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530126 break;
127
128 case IC_SPEED_MODE_FAST:
129 default:
130 cntl |= IC_CON_SPD_FS;
Stefan Roese38481202016-04-21 08:19:42 +0200131 if (scl_sda_cfg) {
132 hcnt = scl_sda_cfg->fs_hcnt;
133 lcnt = scl_sda_cfg->fs_lcnt;
134 } else {
135 hcnt = (IC_CLK * MIN_FS_SCL_HIGHTIME) / NANO_TO_MICRO;
136 lcnt = (IC_CLK * MIN_FS_SCL_LOWTIME) / NANO_TO_MICRO;
137 }
Stefan Roeseef6073e2014-10-28 12:12:00 +0100138 writel(hcnt, &i2c_base->ic_fs_scl_hcnt);
Stefan Roeseef6073e2014-10-28 12:12:00 +0100139 writel(lcnt, &i2c_base->ic_fs_scl_lcnt);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530140 break;
141 }
142
Stefan Roeseef6073e2014-10-28 12:12:00 +0100143 writel(cntl, &i2c_base->ic_con);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530144
Stefan Roese38481202016-04-21 08:19:42 +0200145 /* Configure SDA Hold Time if required */
146 if (scl_sda_cfg)
147 writel(scl_sda_cfg->sda_hold, &i2c_base->ic_sda_hold);
148
Armando Visconti28a724f2012-12-06 00:04:17 +0000149 /* Enable back i2c now speed set */
Stefan Roese3bc33ba2016-04-21 08:19:38 +0200150 dw_i2c_enable(i2c_base, true);
Stefan Roesef6322ebd2012-01-20 11:52:33 +0100151
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530152 return 0;
153}
154
155/*
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530156 * i2c_setaddress - Sets the target slave address
157 * @i2c_addr: target i2c address
158 *
159 * Sets the target slave address.
160 */
Stefan Roese41de7662016-04-21 08:19:40 +0200161static void i2c_setaddress(struct i2c_regs *i2c_base, unsigned int i2c_addr)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530162{
Alexey Brodkin41c56552013-11-07 17:52:18 +0400163 /* Disable i2c */
Stefan Roese3bc33ba2016-04-21 08:19:38 +0200164 dw_i2c_enable(i2c_base, false);
Alexey Brodkin41c56552013-11-07 17:52:18 +0400165
Stefan Roeseef6073e2014-10-28 12:12:00 +0100166 writel(i2c_addr, &i2c_base->ic_tar);
Alexey Brodkin41c56552013-11-07 17:52:18 +0400167
168 /* Enable i2c */
Stefan Roese3bc33ba2016-04-21 08:19:38 +0200169 dw_i2c_enable(i2c_base, true);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530170}
171
172/*
173 * i2c_flush_rxfifo - Flushes the i2c RX FIFO
174 *
175 * Flushes the i2c RX FIFO
176 */
Stefan Roese41de7662016-04-21 08:19:40 +0200177static void i2c_flush_rxfifo(struct i2c_regs *i2c_base)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530178{
Stefan Roeseef6073e2014-10-28 12:12:00 +0100179 while (readl(&i2c_base->ic_status) & IC_STATUS_RFNE)
180 readl(&i2c_base->ic_cmd_data);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530181}
182
183/*
184 * i2c_wait_for_bb - Waits for bus busy
185 *
186 * Waits for bus busy
187 */
Stefan Roese41de7662016-04-21 08:19:40 +0200188static int i2c_wait_for_bb(struct i2c_regs *i2c_base)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530189{
190 unsigned long start_time_bb = get_timer(0);
191
Stefan Roeseef6073e2014-10-28 12:12:00 +0100192 while ((readl(&i2c_base->ic_status) & IC_STATUS_MA) ||
193 !(readl(&i2c_base->ic_status) & IC_STATUS_TFE)) {
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530194
195 /* Evaluate timeout */
196 if (get_timer(start_time_bb) > (unsigned long)(I2C_BYTE_TO_BB))
197 return 1;
198 }
199
200 return 0;
201}
202
Stefan Roese41de7662016-04-21 08:19:40 +0200203static int i2c_xfer_init(struct i2c_regs *i2c_base, uchar chip, uint addr,
Stefan Roeseef6073e2014-10-28 12:12:00 +0100204 int alen)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530205{
Stefan Roese41de7662016-04-21 08:19:40 +0200206 if (i2c_wait_for_bb(i2c_base))
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530207 return 1;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530208
Stefan Roese41de7662016-04-21 08:19:40 +0200209 i2c_setaddress(i2c_base, chip);
Chin Liang Seea0c26262014-02-04 11:56:23 -0600210 while (alen) {
211 alen--;
212 /* high byte address going out first */
213 writel((addr >> (alen * 8)) & 0xff,
Stefan Roeseef6073e2014-10-28 12:12:00 +0100214 &i2c_base->ic_cmd_data);
Chin Liang Seea0c26262014-02-04 11:56:23 -0600215 }
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530216 return 0;
217}
218
Stefan Roese41de7662016-04-21 08:19:40 +0200219static int i2c_xfer_finish(struct i2c_regs *i2c_base)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530220{
221 ulong start_stop_det = get_timer(0);
222
223 while (1) {
Stefan Roeseef6073e2014-10-28 12:12:00 +0100224 if ((readl(&i2c_base->ic_raw_intr_stat) & IC_STOP_DET)) {
225 readl(&i2c_base->ic_clr_stop_det);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530226 break;
227 } else if (get_timer(start_stop_det) > I2C_STOPDET_TO) {
228 break;
229 }
230 }
231
Stefan Roese41de7662016-04-21 08:19:40 +0200232 if (i2c_wait_for_bb(i2c_base)) {
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530233 printf("Timed out waiting for bus\n");
234 return 1;
235 }
236
Stefan Roese41de7662016-04-21 08:19:40 +0200237 i2c_flush_rxfifo(i2c_base);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530238
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530239 return 0;
240}
241
242/*
243 * i2c_read - Read from i2c memory
244 * @chip: target i2c address
245 * @addr: address to read from
246 * @alen:
247 * @buffer: buffer for read data
248 * @len: no of bytes to be read
249 *
250 * Read from i2c memory.
251 */
Stefan Roese41de7662016-04-21 08:19:40 +0200252static int __dw_i2c_read(struct i2c_regs *i2c_base, u8 dev, uint addr,
253 int alen, u8 *buffer, int len)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530254{
255 unsigned long start_time_rx;
Marek Vasutc4bc9a82016-10-20 16:48:28 +0200256 unsigned int active = 0;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530257
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400258#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
259 /*
260 * EEPROM chips that implement "address overflow" are ones
261 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
262 * address and the extra bits end up in the "chip address"
263 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
264 * four 256 byte chips.
265 *
266 * Note that we consider the length of the address field to
267 * still be one byte because the extra address bits are
268 * hidden in the chip address.
269 */
Stefan Roeseef6073e2014-10-28 12:12:00 +0100270 dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400271 addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
272
Stefan Roeseef6073e2014-10-28 12:12:00 +0100273 debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400274 addr);
275#endif
276
Stefan Roese41de7662016-04-21 08:19:40 +0200277 if (i2c_xfer_init(i2c_base, dev, addr, alen))
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530278 return 1;
279
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530280 start_time_rx = get_timer(0);
281 while (len) {
Marek Vasutc4bc9a82016-10-20 16:48:28 +0200282 if (!active) {
283 /*
284 * Avoid writing to ic_cmd_data multiple times
285 * in case this loop spins too quickly and the
286 * ic_status RFNE bit isn't set after the first
287 * write. Subsequent writes to ic_cmd_data can
288 * trigger spurious i2c transfer.
289 */
290 if (len == 1)
291 writel(IC_CMD | IC_STOP, &i2c_base->ic_cmd_data);
292 else
293 writel(IC_CMD, &i2c_base->ic_cmd_data);
294 active = 1;
295 }
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530296
Stefan Roeseef6073e2014-10-28 12:12:00 +0100297 if (readl(&i2c_base->ic_status) & IC_STATUS_RFNE) {
298 *buffer++ = (uchar)readl(&i2c_base->ic_cmd_data);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530299 len--;
300 start_time_rx = get_timer(0);
Marek Vasutc4bc9a82016-10-20 16:48:28 +0200301 active = 0;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530302 } else if (get_timer(start_time_rx) > I2C_BYTE_TO) {
Marek Vasutc4bc9a82016-10-20 16:48:28 +0200303 return 1;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530304 }
305 }
306
Stefan Roese41de7662016-04-21 08:19:40 +0200307 return i2c_xfer_finish(i2c_base);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530308}
309
310/*
311 * i2c_write - Write to i2c memory
312 * @chip: target i2c address
313 * @addr: address to read from
314 * @alen:
315 * @buffer: buffer for read data
316 * @len: no of bytes to be read
317 *
318 * Write to i2c memory.
319 */
Stefan Roese41de7662016-04-21 08:19:40 +0200320static int __dw_i2c_write(struct i2c_regs *i2c_base, u8 dev, uint addr,
321 int alen, u8 *buffer, int len)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530322{
323 int nb = len;
324 unsigned long start_time_tx;
325
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400326#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
327 /*
328 * EEPROM chips that implement "address overflow" are ones
329 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
330 * address and the extra bits end up in the "chip address"
331 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
332 * four 256 byte chips.
333 *
334 * Note that we consider the length of the address field to
335 * still be one byte because the extra address bits are
336 * hidden in the chip address.
337 */
Stefan Roeseef6073e2014-10-28 12:12:00 +0100338 dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400339 addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
340
Stefan Roeseef6073e2014-10-28 12:12:00 +0100341 debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400342 addr);
343#endif
344
Stefan Roese41de7662016-04-21 08:19:40 +0200345 if (i2c_xfer_init(i2c_base, dev, addr, alen))
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530346 return 1;
347
348 start_time_tx = get_timer(0);
349 while (len) {
Stefan Roeseef6073e2014-10-28 12:12:00 +0100350 if (readl(&i2c_base->ic_status) & IC_STATUS_TFNF) {
351 if (--len == 0) {
352 writel(*buffer | IC_STOP,
353 &i2c_base->ic_cmd_data);
354 } else {
355 writel(*buffer, &i2c_base->ic_cmd_data);
356 }
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530357 buffer++;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530358 start_time_tx = get_timer(0);
359
360 } else if (get_timer(start_time_tx) > (nb * I2C_BYTE_TO)) {
361 printf("Timed out. i2c write Failed\n");
362 return 1;
363 }
364 }
365
Stefan Roese41de7662016-04-21 08:19:40 +0200366 return i2c_xfer_finish(i2c_base);
367}
368
Stefan Roese3cb27962016-04-21 08:19:41 +0200369/*
370 * __dw_i2c_init - Init function
371 * @speed: required i2c speed
372 * @slaveaddr: slave address for the device
373 *
374 * Initialization function.
375 */
Simon Glassbd9ca8d2019-02-16 20:24:39 -0700376static int __dw_i2c_init(struct i2c_regs *i2c_base, int speed, int slaveaddr)
Stefan Roese3cb27962016-04-21 08:19:41 +0200377{
Simon Glassbd9ca8d2019-02-16 20:24:39 -0700378 int ret;
379
Stefan Roese3cb27962016-04-21 08:19:41 +0200380 /* Disable i2c */
Simon Glassbd9ca8d2019-02-16 20:24:39 -0700381 ret = dw_i2c_enable(i2c_base, false);
382 if (ret)
383 return ret;
Stefan Roese3cb27962016-04-21 08:19:41 +0200384
Marek Vasut808aa132017-08-07 20:45:31 +0200385 writel(IC_CON_SD | IC_CON_RE | IC_CON_SPD_FS | IC_CON_MM,
386 &i2c_base->ic_con);
Stefan Roese3cb27962016-04-21 08:19:41 +0200387 writel(IC_RX_TL, &i2c_base->ic_rx_tl);
388 writel(IC_TX_TL, &i2c_base->ic_tx_tl);
389 writel(IC_STOP_DET, &i2c_base->ic_intr_mask);
390#ifndef CONFIG_DM_I2C
Stefan Roese38481202016-04-21 08:19:42 +0200391 __dw_i2c_set_bus_speed(i2c_base, NULL, speed);
Stefan Roese3cb27962016-04-21 08:19:41 +0200392 writel(slaveaddr, &i2c_base->ic_sar);
393#endif
394
395 /* Enable i2c */
Simon Glassbd9ca8d2019-02-16 20:24:39 -0700396 ret = dw_i2c_enable(i2c_base, true);
397 if (ret)
398 return ret;
399
400 return 0;
Stefan Roese3cb27962016-04-21 08:19:41 +0200401}
402
403#ifndef CONFIG_DM_I2C
404/*
405 * The legacy I2C functions. These need to get removed once
406 * all users of this driver are converted to DM.
407 */
Stefan Roese41de7662016-04-21 08:19:40 +0200408static struct i2c_regs *i2c_get_base(struct i2c_adapter *adap)
409{
410 switch (adap->hwadapnr) {
411#if CONFIG_SYS_I2C_BUS_MAX >= 4
412 case 3:
413 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE3;
414#endif
415#if CONFIG_SYS_I2C_BUS_MAX >= 3
416 case 2:
417 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE2;
418#endif
419#if CONFIG_SYS_I2C_BUS_MAX >= 2
420 case 1:
421 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE1;
422#endif
423 case 0:
424 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE;
425 default:
426 printf("Wrong I2C-adapter number %d\n", adap->hwadapnr);
427 }
428
429 return NULL;
430}
431
432static unsigned int dw_i2c_set_bus_speed(struct i2c_adapter *adap,
433 unsigned int speed)
434{
435 adap->speed = speed;
Stefan Roese38481202016-04-21 08:19:42 +0200436 return __dw_i2c_set_bus_speed(i2c_get_base(adap), NULL, speed);
Stefan Roese41de7662016-04-21 08:19:40 +0200437}
438
Stefan Roese3cb27962016-04-21 08:19:41 +0200439static void dw_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
Stefan Roese41de7662016-04-21 08:19:40 +0200440{
Stefan Roese3cb27962016-04-21 08:19:41 +0200441 __dw_i2c_init(i2c_get_base(adap), speed, slaveaddr);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530442}
443
Stefan Roese41de7662016-04-21 08:19:40 +0200444static int dw_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr,
445 int alen, u8 *buffer, int len)
446{
447 return __dw_i2c_read(i2c_get_base(adap), dev, addr, alen, buffer, len);
448}
449
450static int dw_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr,
451 int alen, u8 *buffer, int len)
452{
453 return __dw_i2c_write(i2c_get_base(adap), dev, addr, alen, buffer, len);
454}
455
Stefan Roese3cb27962016-04-21 08:19:41 +0200456/* dw_i2c_probe - Probe the i2c chip */
Stefan Roeseef6073e2014-10-28 12:12:00 +0100457static int dw_i2c_probe(struct i2c_adapter *adap, u8 dev)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530458{
Stefan Roese41de7662016-04-21 08:19:40 +0200459 struct i2c_regs *i2c_base = i2c_get_base(adap);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530460 u32 tmp;
Stefan Roesef6322ebd2012-01-20 11:52:33 +0100461 int ret;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530462
463 /*
464 * Try to read the first location of the chip.
465 */
Stefan Roese41de7662016-04-21 08:19:40 +0200466 ret = __dw_i2c_read(i2c_base, dev, 0, 1, (uchar *)&tmp, 1);
Stefan Roesef6322ebd2012-01-20 11:52:33 +0100467 if (ret)
Stefan Roeseef6073e2014-10-28 12:12:00 +0100468 dw_i2c_init(adap, adap->speed, adap->slaveaddr);
Stefan Roesef6322ebd2012-01-20 11:52:33 +0100469
470 return ret;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530471}
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000472
Stefan Roeseef6073e2014-10-28 12:12:00 +0100473U_BOOT_I2C_ADAP_COMPLETE(dw_0, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
474 dw_i2c_write, dw_i2c_set_bus_speed,
475 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0)
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000476
Stefan Roeseef6073e2014-10-28 12:12:00 +0100477#if CONFIG_SYS_I2C_BUS_MAX >= 2
478U_BOOT_I2C_ADAP_COMPLETE(dw_1, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
479 dw_i2c_write, dw_i2c_set_bus_speed,
480 CONFIG_SYS_I2C_SPEED1, CONFIG_SYS_I2C_SLAVE1, 1)
481#endif
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000482
Stefan Roeseef6073e2014-10-28 12:12:00 +0100483#if CONFIG_SYS_I2C_BUS_MAX >= 3
484U_BOOT_I2C_ADAP_COMPLETE(dw_2, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
485 dw_i2c_write, dw_i2c_set_bus_speed,
486 CONFIG_SYS_I2C_SPEED2, CONFIG_SYS_I2C_SLAVE2, 2)
487#endif
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000488
Stefan Roeseef6073e2014-10-28 12:12:00 +0100489#if CONFIG_SYS_I2C_BUS_MAX >= 4
490U_BOOT_I2C_ADAP_COMPLETE(dw_3, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
491 dw_i2c_write, dw_i2c_set_bus_speed,
492 CONFIG_SYS_I2C_SPEED3, CONFIG_SYS_I2C_SLAVE3, 3)
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000493#endif
Stefan Roese3cb27962016-04-21 08:19:41 +0200494
495#else /* CONFIG_DM_I2C */
496/* The DM I2C functions */
497
498static int designware_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
499 int nmsgs)
500{
501 struct dw_i2c *i2c = dev_get_priv(bus);
502 int ret;
503
504 debug("i2c_xfer: %d messages\n", nmsgs);
505 for (; nmsgs > 0; nmsgs--, msg++) {
506 debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
507 if (msg->flags & I2C_M_RD) {
508 ret = __dw_i2c_read(i2c->regs, msg->addr, 0, 0,
509 msg->buf, msg->len);
510 } else {
511 ret = __dw_i2c_write(i2c->regs, msg->addr, 0, 0,
512 msg->buf, msg->len);
513 }
514 if (ret) {
515 debug("i2c_write: error sending\n");
516 return -EREMOTEIO;
517 }
518 }
519
520 return 0;
521}
522
523static int designware_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
524{
525 struct dw_i2c *i2c = dev_get_priv(bus);
526
Stefan Roese38481202016-04-21 08:19:42 +0200527 return __dw_i2c_set_bus_speed(i2c->regs, i2c->scl_sda_cfg, speed);
Stefan Roese3cb27962016-04-21 08:19:41 +0200528}
529
530static int designware_i2c_probe_chip(struct udevice *bus, uint chip_addr,
531 uint chip_flags)
532{
533 struct dw_i2c *i2c = dev_get_priv(bus);
534 struct i2c_regs *i2c_base = i2c->regs;
535 u32 tmp;
536 int ret;
537
538 /* Try to read the first location of the chip */
539 ret = __dw_i2c_read(i2c_base, chip_addr, 0, 1, (uchar *)&tmp, 1);
540 if (ret)
541 __dw_i2c_init(i2c_base, 0, 0);
542
543 return ret;
544}
545
546static int designware_i2c_probe(struct udevice *bus)
547{
548 struct dw_i2c *priv = dev_get_priv(bus);
Dinh Nguyen08794aa2018-04-04 17:18:24 -0500549 int ret;
Stefan Roese3cb27962016-04-21 08:19:41 +0200550
Stefan Roese38481202016-04-21 08:19:42 +0200551 if (device_is_on_pci_bus(bus)) {
552#ifdef CONFIG_DM_PCI
553 /* Save base address from PCI BAR */
554 priv->regs = (struct i2c_regs *)
555 dm_pci_map_bar(bus, PCI_BASE_ADDRESS_0, PCI_REGION_MEM);
556#ifdef CONFIG_X86
557 /* Use BayTrail specific timing values */
558 priv->scl_sda_cfg = &byt_config;
559#endif
560#endif
561 } else {
Simon Glassba1dea42017-05-17 17:18:05 -0600562 priv->regs = (struct i2c_regs *)devfdt_get_addr_ptr(bus);
Stefan Roese38481202016-04-21 08:19:42 +0200563 }
Stefan Roese3cb27962016-04-21 08:19:41 +0200564
Simon Goldschmidt28608a12019-03-28 21:11:48 +0100565 ret = reset_get_bulk(bus, &priv->resets);
Dinh Nguyen08794aa2018-04-04 17:18:24 -0500566 if (ret)
Simon Goldschmidt28608a12019-03-28 21:11:48 +0100567 dev_warn(bus, "Can't get reset: %d\n", ret);
568 else
569 reset_deassert_bulk(&priv->resets);
Dinh Nguyen08794aa2018-04-04 17:18:24 -0500570
Simon Glassbd9ca8d2019-02-16 20:24:39 -0700571 return __dw_i2c_init(priv->regs, 0, 0);
Stefan Roese3cb27962016-04-21 08:19:41 +0200572}
573
Simon Goldschmidt28608a12019-03-28 21:11:48 +0100574static int designware_i2c_remove(struct udevice *dev)
575{
576 struct dw_i2c *priv = dev_get_priv(dev);
577
578 return reset_release_bulk(&priv->resets);
579}
580
Stefan Roese38481202016-04-21 08:19:42 +0200581static int designware_i2c_bind(struct udevice *dev)
582{
583 static int num_cards;
584 char name[20];
585
586 /* Create a unique device name for PCI type devices */
587 if (device_is_on_pci_bus(dev)) {
588 /*
589 * ToDo:
590 * Setting req_seq in the driver is probably not recommended.
591 * But without a DT alias the number is not configured. And
592 * using this driver is impossible for PCIe I2C devices.
593 * This can be removed, once a better (correct) way for this
594 * is found and implemented.
595 */
596 dev->req_seq = num_cards;
597 sprintf(name, "i2c_designware#%u", num_cards++);
598 device_set_name(dev, name);
599 }
600
601 return 0;
602}
603
Stefan Roese3cb27962016-04-21 08:19:41 +0200604static const struct dm_i2c_ops designware_i2c_ops = {
605 .xfer = designware_i2c_xfer,
606 .probe_chip = designware_i2c_probe_chip,
607 .set_bus_speed = designware_i2c_set_bus_speed,
608};
609
610static const struct udevice_id designware_i2c_ids[] = {
611 { .compatible = "snps,designware-i2c" },
612 { }
613};
614
615U_BOOT_DRIVER(i2c_designware) = {
616 .name = "i2c_designware",
617 .id = UCLASS_I2C,
618 .of_match = designware_i2c_ids,
Stefan Roese38481202016-04-21 08:19:42 +0200619 .bind = designware_i2c_bind,
Stefan Roese3cb27962016-04-21 08:19:41 +0200620 .probe = designware_i2c_probe,
621 .priv_auto_alloc_size = sizeof(struct dw_i2c),
Simon Goldschmidt28608a12019-03-28 21:11:48 +0100622 .remove = designware_i2c_remove,
623 .flags = DM_FLAG_OS_PREPARE,
Stefan Roese3cb27962016-04-21 08:19:41 +0200624 .ops = &designware_i2c_ops,
625};
626
Stefan Roese38481202016-04-21 08:19:42 +0200627#ifdef CONFIG_X86
628static struct pci_device_id designware_pci_supported[] = {
629 /* Intel BayTrail has 7 I2C controller located on the PCI bus */
630 { PCI_VDEVICE(INTEL, 0x0f41) },
631 { PCI_VDEVICE(INTEL, 0x0f42) },
632 { PCI_VDEVICE(INTEL, 0x0f43) },
633 { PCI_VDEVICE(INTEL, 0x0f44) },
634 { PCI_VDEVICE(INTEL, 0x0f45) },
635 { PCI_VDEVICE(INTEL, 0x0f46) },
636 { PCI_VDEVICE(INTEL, 0x0f47) },
637 {},
638};
639
640U_BOOT_PCI_DEVICE(i2c_designware, designware_pci_supported);
641#endif
642
Stefan Roese3cb27962016-04-21 08:19:41 +0200643#endif /* CONFIG_DM_I2C */