blob: dbc3326b5a9923c78550465ec31a55e7511161a4 [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;
Dinh Nguyen08794aa2018-04-04 17:18:24 -050037 struct reset_ctl reset_ctl;
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
Stefan Roese3bc33ba2016-04-21 08:19:38 +020041static void dw_i2c_enable(struct i2c_regs *i2c_base, bool enable)
42{
43 u32 ena = enable ? IC_ENABLE_0B : 0;
Stefan Roeseabb3e132016-04-27 09:02:12 +020044
45 writel(ena, &i2c_base->ic_enable);
46}
47#else
48static void dw_i2c_enable(struct i2c_regs *i2c_base, bool enable)
49{
50 u32 ena = enable ? IC_ENABLE_0B : 0;
Stefan Roese3bc33ba2016-04-21 08:19:38 +020051 int timeout = 100;
52
53 do {
54 writel(ena, &i2c_base->ic_enable);
55 if ((readl(&i2c_base->ic_enable_status) & IC_ENABLE_0B) == ena)
56 return;
57
58 /*
59 * Wait 10 times the signaling period of the highest I2C
60 * transfer supported by the driver (for 400KHz this is
61 * 25us) as described in the DesignWare I2C databook.
62 */
63 udelay(25);
64 } while (timeout--);
65
66 printf("timeout in %sabling I2C adapter\n", enable ? "en" : "dis");
67}
Stefan Roeseabb3e132016-04-27 09:02:12 +020068#endif
Stefan Roese3bc33ba2016-04-21 08:19:38 +020069
Vipin KUMARfc9589f2010-01-15 19:15:44 +053070/*
Stefan Roese88893c92016-04-21 08:19:39 +020071 * i2c_set_bus_speed - Set the i2c speed
72 * @speed: required i2c speed
Vipin KUMARfc9589f2010-01-15 19:15:44 +053073 *
Stefan Roese88893c92016-04-21 08:19:39 +020074 * Set the i2c speed.
Vipin KUMARfc9589f2010-01-15 19:15:44 +053075 */
Stefan Roese41de7662016-04-21 08:19:40 +020076static unsigned int __dw_i2c_set_bus_speed(struct i2c_regs *i2c_base,
Stefan Roese38481202016-04-21 08:19:42 +020077 struct dw_scl_sda_cfg *scl_sda_cfg,
Stefan Roese41de7662016-04-21 08:19:40 +020078 unsigned int speed)
Vipin KUMARfc9589f2010-01-15 19:15:44 +053079{
80 unsigned int cntl;
81 unsigned int hcnt, lcnt;
Stefan Roese88893c92016-04-21 08:19:39 +020082 int i2c_spd;
83
84 if (speed >= I2C_MAX_SPEED)
85 i2c_spd = IC_SPEED_MODE_MAX;
86 else if (speed >= I2C_FAST_SPEED)
87 i2c_spd = IC_SPEED_MODE_FAST;
88 else
89 i2c_spd = IC_SPEED_MODE_STANDARD;
Armando Visconti631e6932012-03-29 20:10:17 +000090
91 /* to set speed cltr must be disabled */
Stefan Roese3bc33ba2016-04-21 08:19:38 +020092 dw_i2c_enable(i2c_base, false);
Armando Visconti631e6932012-03-29 20:10:17 +000093
Stefan Roeseef6073e2014-10-28 12:12:00 +010094 cntl = (readl(&i2c_base->ic_con) & (~IC_CON_SPD_MSK));
Vipin KUMARfc9589f2010-01-15 19:15:44 +053095
96 switch (i2c_spd) {
Stefan Roese38481202016-04-21 08:19:42 +020097#ifndef CONFIG_X86 /* No High-speed for BayTrail yet */
Vipin KUMARfc9589f2010-01-15 19:15:44 +053098 case IC_SPEED_MODE_MAX:
Stefan Roese38481202016-04-21 08:19:42 +020099 cntl |= IC_CON_SPD_SS;
100 if (scl_sda_cfg) {
101 hcnt = scl_sda_cfg->fs_hcnt;
102 lcnt = scl_sda_cfg->fs_lcnt;
103 } else {
104 hcnt = (IC_CLK * MIN_HS_SCL_HIGHTIME) / NANO_TO_MICRO;
105 lcnt = (IC_CLK * MIN_HS_SCL_LOWTIME) / NANO_TO_MICRO;
106 }
Stefan Roeseef6073e2014-10-28 12:12:00 +0100107 writel(hcnt, &i2c_base->ic_hs_scl_hcnt);
Stefan Roeseef6073e2014-10-28 12:12:00 +0100108 writel(lcnt, &i2c_base->ic_hs_scl_lcnt);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530109 break;
Stefan Roese38481202016-04-21 08:19:42 +0200110#endif
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530111
112 case IC_SPEED_MODE_STANDARD:
113 cntl |= IC_CON_SPD_SS;
Stefan Roese38481202016-04-21 08:19:42 +0200114 if (scl_sda_cfg) {
115 hcnt = scl_sda_cfg->ss_hcnt;
116 lcnt = scl_sda_cfg->ss_lcnt;
117 } else {
118 hcnt = (IC_CLK * MIN_SS_SCL_HIGHTIME) / NANO_TO_MICRO;
119 lcnt = (IC_CLK * MIN_SS_SCL_LOWTIME) / NANO_TO_MICRO;
120 }
Stefan Roeseef6073e2014-10-28 12:12:00 +0100121 writel(hcnt, &i2c_base->ic_ss_scl_hcnt);
Stefan Roeseef6073e2014-10-28 12:12:00 +0100122 writel(lcnt, &i2c_base->ic_ss_scl_lcnt);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530123 break;
124
125 case IC_SPEED_MODE_FAST:
126 default:
127 cntl |= IC_CON_SPD_FS;
Stefan Roese38481202016-04-21 08:19:42 +0200128 if (scl_sda_cfg) {
129 hcnt = scl_sda_cfg->fs_hcnt;
130 lcnt = scl_sda_cfg->fs_lcnt;
131 } else {
132 hcnt = (IC_CLK * MIN_FS_SCL_HIGHTIME) / NANO_TO_MICRO;
133 lcnt = (IC_CLK * MIN_FS_SCL_LOWTIME) / NANO_TO_MICRO;
134 }
Stefan Roeseef6073e2014-10-28 12:12:00 +0100135 writel(hcnt, &i2c_base->ic_fs_scl_hcnt);
Stefan Roeseef6073e2014-10-28 12:12:00 +0100136 writel(lcnt, &i2c_base->ic_fs_scl_lcnt);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530137 break;
138 }
139
Stefan Roeseef6073e2014-10-28 12:12:00 +0100140 writel(cntl, &i2c_base->ic_con);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530141
Stefan Roese38481202016-04-21 08:19:42 +0200142 /* Configure SDA Hold Time if required */
143 if (scl_sda_cfg)
144 writel(scl_sda_cfg->sda_hold, &i2c_base->ic_sda_hold);
145
Armando Visconti28a724f2012-12-06 00:04:17 +0000146 /* Enable back i2c now speed set */
Stefan Roese3bc33ba2016-04-21 08:19:38 +0200147 dw_i2c_enable(i2c_base, true);
Stefan Roesef6322ebd2012-01-20 11:52:33 +0100148
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530149 return 0;
150}
151
152/*
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530153 * i2c_setaddress - Sets the target slave address
154 * @i2c_addr: target i2c address
155 *
156 * Sets the target slave address.
157 */
Stefan Roese41de7662016-04-21 08:19:40 +0200158static void i2c_setaddress(struct i2c_regs *i2c_base, unsigned int i2c_addr)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530159{
Alexey Brodkin41c56552013-11-07 17:52:18 +0400160 /* Disable i2c */
Stefan Roese3bc33ba2016-04-21 08:19:38 +0200161 dw_i2c_enable(i2c_base, false);
Alexey Brodkin41c56552013-11-07 17:52:18 +0400162
Stefan Roeseef6073e2014-10-28 12:12:00 +0100163 writel(i2c_addr, &i2c_base->ic_tar);
Alexey Brodkin41c56552013-11-07 17:52:18 +0400164
165 /* Enable i2c */
Stefan Roese3bc33ba2016-04-21 08:19:38 +0200166 dw_i2c_enable(i2c_base, true);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530167}
168
169/*
170 * i2c_flush_rxfifo - Flushes the i2c RX FIFO
171 *
172 * Flushes the i2c RX FIFO
173 */
Stefan Roese41de7662016-04-21 08:19:40 +0200174static void i2c_flush_rxfifo(struct i2c_regs *i2c_base)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530175{
Stefan Roeseef6073e2014-10-28 12:12:00 +0100176 while (readl(&i2c_base->ic_status) & IC_STATUS_RFNE)
177 readl(&i2c_base->ic_cmd_data);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530178}
179
180/*
181 * i2c_wait_for_bb - Waits for bus busy
182 *
183 * Waits for bus busy
184 */
Stefan Roese41de7662016-04-21 08:19:40 +0200185static int i2c_wait_for_bb(struct i2c_regs *i2c_base)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530186{
187 unsigned long start_time_bb = get_timer(0);
188
Stefan Roeseef6073e2014-10-28 12:12:00 +0100189 while ((readl(&i2c_base->ic_status) & IC_STATUS_MA) ||
190 !(readl(&i2c_base->ic_status) & IC_STATUS_TFE)) {
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530191
192 /* Evaluate timeout */
193 if (get_timer(start_time_bb) > (unsigned long)(I2C_BYTE_TO_BB))
194 return 1;
195 }
196
197 return 0;
198}
199
Stefan Roese41de7662016-04-21 08:19:40 +0200200static int i2c_xfer_init(struct i2c_regs *i2c_base, uchar chip, uint addr,
Stefan Roeseef6073e2014-10-28 12:12:00 +0100201 int alen)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530202{
Stefan Roese41de7662016-04-21 08:19:40 +0200203 if (i2c_wait_for_bb(i2c_base))
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530204 return 1;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530205
Stefan Roese41de7662016-04-21 08:19:40 +0200206 i2c_setaddress(i2c_base, chip);
Chin Liang Seea0c26262014-02-04 11:56:23 -0600207 while (alen) {
208 alen--;
209 /* high byte address going out first */
210 writel((addr >> (alen * 8)) & 0xff,
Stefan Roeseef6073e2014-10-28 12:12:00 +0100211 &i2c_base->ic_cmd_data);
Chin Liang Seea0c26262014-02-04 11:56:23 -0600212 }
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530213 return 0;
214}
215
Stefan Roese41de7662016-04-21 08:19:40 +0200216static int i2c_xfer_finish(struct i2c_regs *i2c_base)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530217{
218 ulong start_stop_det = get_timer(0);
219
220 while (1) {
Stefan Roeseef6073e2014-10-28 12:12:00 +0100221 if ((readl(&i2c_base->ic_raw_intr_stat) & IC_STOP_DET)) {
222 readl(&i2c_base->ic_clr_stop_det);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530223 break;
224 } else if (get_timer(start_stop_det) > I2C_STOPDET_TO) {
225 break;
226 }
227 }
228
Stefan Roese41de7662016-04-21 08:19:40 +0200229 if (i2c_wait_for_bb(i2c_base)) {
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530230 printf("Timed out waiting for bus\n");
231 return 1;
232 }
233
Stefan Roese41de7662016-04-21 08:19:40 +0200234 i2c_flush_rxfifo(i2c_base);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530235
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530236 return 0;
237}
238
239/*
240 * i2c_read - Read from i2c memory
241 * @chip: target i2c address
242 * @addr: address to read from
243 * @alen:
244 * @buffer: buffer for read data
245 * @len: no of bytes to be read
246 *
247 * Read from i2c memory.
248 */
Stefan Roese41de7662016-04-21 08:19:40 +0200249static int __dw_i2c_read(struct i2c_regs *i2c_base, u8 dev, uint addr,
250 int alen, u8 *buffer, int len)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530251{
252 unsigned long start_time_rx;
Marek Vasutc4bc9a82016-10-20 16:48:28 +0200253 unsigned int active = 0;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530254
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400255#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
256 /*
257 * EEPROM chips that implement "address overflow" are ones
258 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
259 * address and the extra bits end up in the "chip address"
260 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
261 * four 256 byte chips.
262 *
263 * Note that we consider the length of the address field to
264 * still be one byte because the extra address bits are
265 * hidden in the chip address.
266 */
Stefan Roeseef6073e2014-10-28 12:12:00 +0100267 dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400268 addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
269
Stefan Roeseef6073e2014-10-28 12:12:00 +0100270 debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400271 addr);
272#endif
273
Stefan Roese41de7662016-04-21 08:19:40 +0200274 if (i2c_xfer_init(i2c_base, dev, addr, alen))
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530275 return 1;
276
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530277 start_time_rx = get_timer(0);
278 while (len) {
Marek Vasutc4bc9a82016-10-20 16:48:28 +0200279 if (!active) {
280 /*
281 * Avoid writing to ic_cmd_data multiple times
282 * in case this loop spins too quickly and the
283 * ic_status RFNE bit isn't set after the first
284 * write. Subsequent writes to ic_cmd_data can
285 * trigger spurious i2c transfer.
286 */
287 if (len == 1)
288 writel(IC_CMD | IC_STOP, &i2c_base->ic_cmd_data);
289 else
290 writel(IC_CMD, &i2c_base->ic_cmd_data);
291 active = 1;
292 }
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530293
Stefan Roeseef6073e2014-10-28 12:12:00 +0100294 if (readl(&i2c_base->ic_status) & IC_STATUS_RFNE) {
295 *buffer++ = (uchar)readl(&i2c_base->ic_cmd_data);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530296 len--;
297 start_time_rx = get_timer(0);
Marek Vasutc4bc9a82016-10-20 16:48:28 +0200298 active = 0;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530299 } else if (get_timer(start_time_rx) > I2C_BYTE_TO) {
Marek Vasutc4bc9a82016-10-20 16:48:28 +0200300 return 1;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530301 }
302 }
303
Stefan Roese41de7662016-04-21 08:19:40 +0200304 return i2c_xfer_finish(i2c_base);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530305}
306
307/*
308 * i2c_write - Write to i2c memory
309 * @chip: target i2c address
310 * @addr: address to read from
311 * @alen:
312 * @buffer: buffer for read data
313 * @len: no of bytes to be read
314 *
315 * Write to i2c memory.
316 */
Stefan Roese41de7662016-04-21 08:19:40 +0200317static int __dw_i2c_write(struct i2c_regs *i2c_base, u8 dev, uint addr,
318 int alen, u8 *buffer, int len)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530319{
320 int nb = len;
321 unsigned long start_time_tx;
322
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400323#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
324 /*
325 * EEPROM chips that implement "address overflow" are ones
326 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
327 * address and the extra bits end up in the "chip address"
328 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
329 * four 256 byte chips.
330 *
331 * Note that we consider the length of the address field to
332 * still be one byte because the extra address bits are
333 * hidden in the chip address.
334 */
Stefan Roeseef6073e2014-10-28 12:12:00 +0100335 dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400336 addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
337
Stefan Roeseef6073e2014-10-28 12:12:00 +0100338 debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400339 addr);
340#endif
341
Stefan Roese41de7662016-04-21 08:19:40 +0200342 if (i2c_xfer_init(i2c_base, dev, addr, alen))
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530343 return 1;
344
345 start_time_tx = get_timer(0);
346 while (len) {
Stefan Roeseef6073e2014-10-28 12:12:00 +0100347 if (readl(&i2c_base->ic_status) & IC_STATUS_TFNF) {
348 if (--len == 0) {
349 writel(*buffer | IC_STOP,
350 &i2c_base->ic_cmd_data);
351 } else {
352 writel(*buffer, &i2c_base->ic_cmd_data);
353 }
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530354 buffer++;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530355 start_time_tx = get_timer(0);
356
357 } else if (get_timer(start_time_tx) > (nb * I2C_BYTE_TO)) {
358 printf("Timed out. i2c write Failed\n");
359 return 1;
360 }
361 }
362
Stefan Roese41de7662016-04-21 08:19:40 +0200363 return i2c_xfer_finish(i2c_base);
364}
365
Stefan Roese3cb27962016-04-21 08:19:41 +0200366/*
367 * __dw_i2c_init - Init function
368 * @speed: required i2c speed
369 * @slaveaddr: slave address for the device
370 *
371 * Initialization function.
372 */
373static void __dw_i2c_init(struct i2c_regs *i2c_base, int speed, int slaveaddr)
374{
375 /* Disable i2c */
376 dw_i2c_enable(i2c_base, false);
377
Marek Vasut808aa132017-08-07 20:45:31 +0200378 writel(IC_CON_SD | IC_CON_RE | IC_CON_SPD_FS | IC_CON_MM,
379 &i2c_base->ic_con);
Stefan Roese3cb27962016-04-21 08:19:41 +0200380 writel(IC_RX_TL, &i2c_base->ic_rx_tl);
381 writel(IC_TX_TL, &i2c_base->ic_tx_tl);
382 writel(IC_STOP_DET, &i2c_base->ic_intr_mask);
383#ifndef CONFIG_DM_I2C
Stefan Roese38481202016-04-21 08:19:42 +0200384 __dw_i2c_set_bus_speed(i2c_base, NULL, speed);
Stefan Roese3cb27962016-04-21 08:19:41 +0200385 writel(slaveaddr, &i2c_base->ic_sar);
386#endif
387
388 /* Enable i2c */
389 dw_i2c_enable(i2c_base, true);
390}
391
392#ifndef CONFIG_DM_I2C
393/*
394 * The legacy I2C functions. These need to get removed once
395 * all users of this driver are converted to DM.
396 */
Stefan Roese41de7662016-04-21 08:19:40 +0200397static struct i2c_regs *i2c_get_base(struct i2c_adapter *adap)
398{
399 switch (adap->hwadapnr) {
400#if CONFIG_SYS_I2C_BUS_MAX >= 4
401 case 3:
402 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE3;
403#endif
404#if CONFIG_SYS_I2C_BUS_MAX >= 3
405 case 2:
406 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE2;
407#endif
408#if CONFIG_SYS_I2C_BUS_MAX >= 2
409 case 1:
410 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE1;
411#endif
412 case 0:
413 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE;
414 default:
415 printf("Wrong I2C-adapter number %d\n", adap->hwadapnr);
416 }
417
418 return NULL;
419}
420
421static unsigned int dw_i2c_set_bus_speed(struct i2c_adapter *adap,
422 unsigned int speed)
423{
424 adap->speed = speed;
Stefan Roese38481202016-04-21 08:19:42 +0200425 return __dw_i2c_set_bus_speed(i2c_get_base(adap), NULL, speed);
Stefan Roese41de7662016-04-21 08:19:40 +0200426}
427
Stefan Roese3cb27962016-04-21 08:19:41 +0200428static void dw_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
Stefan Roese41de7662016-04-21 08:19:40 +0200429{
Stefan Roese3cb27962016-04-21 08:19:41 +0200430 __dw_i2c_init(i2c_get_base(adap), speed, slaveaddr);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530431}
432
Stefan Roese41de7662016-04-21 08:19:40 +0200433static int dw_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr,
434 int alen, u8 *buffer, int len)
435{
436 return __dw_i2c_read(i2c_get_base(adap), dev, addr, alen, buffer, len);
437}
438
439static int dw_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr,
440 int alen, u8 *buffer, int len)
441{
442 return __dw_i2c_write(i2c_get_base(adap), dev, addr, alen, buffer, len);
443}
444
Stefan Roese3cb27962016-04-21 08:19:41 +0200445/* dw_i2c_probe - Probe the i2c chip */
Stefan Roeseef6073e2014-10-28 12:12:00 +0100446static int dw_i2c_probe(struct i2c_adapter *adap, u8 dev)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530447{
Stefan Roese41de7662016-04-21 08:19:40 +0200448 struct i2c_regs *i2c_base = i2c_get_base(adap);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530449 u32 tmp;
Stefan Roesef6322ebd2012-01-20 11:52:33 +0100450 int ret;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530451
452 /*
453 * Try to read the first location of the chip.
454 */
Stefan Roese41de7662016-04-21 08:19:40 +0200455 ret = __dw_i2c_read(i2c_base, dev, 0, 1, (uchar *)&tmp, 1);
Stefan Roesef6322ebd2012-01-20 11:52:33 +0100456 if (ret)
Stefan Roeseef6073e2014-10-28 12:12:00 +0100457 dw_i2c_init(adap, adap->speed, adap->slaveaddr);
Stefan Roesef6322ebd2012-01-20 11:52:33 +0100458
459 return ret;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530460}
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000461
Stefan Roeseef6073e2014-10-28 12:12:00 +0100462U_BOOT_I2C_ADAP_COMPLETE(dw_0, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
463 dw_i2c_write, dw_i2c_set_bus_speed,
464 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0)
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000465
Stefan Roeseef6073e2014-10-28 12:12:00 +0100466#if CONFIG_SYS_I2C_BUS_MAX >= 2
467U_BOOT_I2C_ADAP_COMPLETE(dw_1, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
468 dw_i2c_write, dw_i2c_set_bus_speed,
469 CONFIG_SYS_I2C_SPEED1, CONFIG_SYS_I2C_SLAVE1, 1)
470#endif
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000471
Stefan Roeseef6073e2014-10-28 12:12:00 +0100472#if CONFIG_SYS_I2C_BUS_MAX >= 3
473U_BOOT_I2C_ADAP_COMPLETE(dw_2, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
474 dw_i2c_write, dw_i2c_set_bus_speed,
475 CONFIG_SYS_I2C_SPEED2, CONFIG_SYS_I2C_SLAVE2, 2)
476#endif
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000477
Stefan Roeseef6073e2014-10-28 12:12:00 +0100478#if CONFIG_SYS_I2C_BUS_MAX >= 4
479U_BOOT_I2C_ADAP_COMPLETE(dw_3, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
480 dw_i2c_write, dw_i2c_set_bus_speed,
481 CONFIG_SYS_I2C_SPEED3, CONFIG_SYS_I2C_SLAVE3, 3)
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000482#endif
Stefan Roese3cb27962016-04-21 08:19:41 +0200483
484#else /* CONFIG_DM_I2C */
485/* The DM I2C functions */
486
487static int designware_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
488 int nmsgs)
489{
490 struct dw_i2c *i2c = dev_get_priv(bus);
491 int ret;
492
493 debug("i2c_xfer: %d messages\n", nmsgs);
494 for (; nmsgs > 0; nmsgs--, msg++) {
495 debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
496 if (msg->flags & I2C_M_RD) {
497 ret = __dw_i2c_read(i2c->regs, msg->addr, 0, 0,
498 msg->buf, msg->len);
499 } else {
500 ret = __dw_i2c_write(i2c->regs, msg->addr, 0, 0,
501 msg->buf, msg->len);
502 }
503 if (ret) {
504 debug("i2c_write: error sending\n");
505 return -EREMOTEIO;
506 }
507 }
508
509 return 0;
510}
511
512static int designware_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
513{
514 struct dw_i2c *i2c = dev_get_priv(bus);
515
Stefan Roese38481202016-04-21 08:19:42 +0200516 return __dw_i2c_set_bus_speed(i2c->regs, i2c->scl_sda_cfg, speed);
Stefan Roese3cb27962016-04-21 08:19:41 +0200517}
518
519static int designware_i2c_probe_chip(struct udevice *bus, uint chip_addr,
520 uint chip_flags)
521{
522 struct dw_i2c *i2c = dev_get_priv(bus);
523 struct i2c_regs *i2c_base = i2c->regs;
524 u32 tmp;
525 int ret;
526
527 /* Try to read the first location of the chip */
528 ret = __dw_i2c_read(i2c_base, chip_addr, 0, 1, (uchar *)&tmp, 1);
529 if (ret)
530 __dw_i2c_init(i2c_base, 0, 0);
531
532 return ret;
533}
534
535static int designware_i2c_probe(struct udevice *bus)
536{
537 struct dw_i2c *priv = dev_get_priv(bus);
Dinh Nguyen08794aa2018-04-04 17:18:24 -0500538 int ret;
Stefan Roese3cb27962016-04-21 08:19:41 +0200539
Stefan Roese38481202016-04-21 08:19:42 +0200540 if (device_is_on_pci_bus(bus)) {
541#ifdef CONFIG_DM_PCI
542 /* Save base address from PCI BAR */
543 priv->regs = (struct i2c_regs *)
544 dm_pci_map_bar(bus, PCI_BASE_ADDRESS_0, PCI_REGION_MEM);
545#ifdef CONFIG_X86
546 /* Use BayTrail specific timing values */
547 priv->scl_sda_cfg = &byt_config;
548#endif
549#endif
550 } else {
Simon Glassba1dea42017-05-17 17:18:05 -0600551 priv->regs = (struct i2c_regs *)devfdt_get_addr_ptr(bus);
Stefan Roese38481202016-04-21 08:19:42 +0200552 }
Stefan Roese3cb27962016-04-21 08:19:41 +0200553
Dinh Nguyen08794aa2018-04-04 17:18:24 -0500554 ret = reset_get_by_name(bus, "i2c", &priv->reset_ctl);
555 if (ret)
556 pr_info("reset_get_by_name() failed: %d\n", ret);
557
558 if (&priv->reset_ctl)
559 reset_deassert(&priv->reset_ctl);
560
Stefan Roese3cb27962016-04-21 08:19:41 +0200561 __dw_i2c_init(priv->regs, 0, 0);
562
563 return 0;
564}
565
Stefan Roese38481202016-04-21 08:19:42 +0200566static int designware_i2c_bind(struct udevice *dev)
567{
568 static int num_cards;
569 char name[20];
570
571 /* Create a unique device name for PCI type devices */
572 if (device_is_on_pci_bus(dev)) {
573 /*
574 * ToDo:
575 * Setting req_seq in the driver is probably not recommended.
576 * But without a DT alias the number is not configured. And
577 * using this driver is impossible for PCIe I2C devices.
578 * This can be removed, once a better (correct) way for this
579 * is found and implemented.
580 */
581 dev->req_seq = num_cards;
582 sprintf(name, "i2c_designware#%u", num_cards++);
583 device_set_name(dev, name);
584 }
585
586 return 0;
587}
588
Stefan Roese3cb27962016-04-21 08:19:41 +0200589static const struct dm_i2c_ops designware_i2c_ops = {
590 .xfer = designware_i2c_xfer,
591 .probe_chip = designware_i2c_probe_chip,
592 .set_bus_speed = designware_i2c_set_bus_speed,
593};
594
595static const struct udevice_id designware_i2c_ids[] = {
596 { .compatible = "snps,designware-i2c" },
597 { }
598};
599
600U_BOOT_DRIVER(i2c_designware) = {
601 .name = "i2c_designware",
602 .id = UCLASS_I2C,
603 .of_match = designware_i2c_ids,
Stefan Roese38481202016-04-21 08:19:42 +0200604 .bind = designware_i2c_bind,
Stefan Roese3cb27962016-04-21 08:19:41 +0200605 .probe = designware_i2c_probe,
606 .priv_auto_alloc_size = sizeof(struct dw_i2c),
607 .ops = &designware_i2c_ops,
608};
609
Stefan Roese38481202016-04-21 08:19:42 +0200610#ifdef CONFIG_X86
611static struct pci_device_id designware_pci_supported[] = {
612 /* Intel BayTrail has 7 I2C controller located on the PCI bus */
613 { PCI_VDEVICE(INTEL, 0x0f41) },
614 { PCI_VDEVICE(INTEL, 0x0f42) },
615 { PCI_VDEVICE(INTEL, 0x0f43) },
616 { PCI_VDEVICE(INTEL, 0x0f44) },
617 { PCI_VDEVICE(INTEL, 0x0f45) },
618 { PCI_VDEVICE(INTEL, 0x0f46) },
619 { PCI_VDEVICE(INTEL, 0x0f47) },
620 {},
621};
622
623U_BOOT_PCI_DEVICE(i2c_designware, designware_pci_supported);
624#endif
625
Stefan Roese3cb27962016-04-21 08:19:41 +0200626#endif /* CONFIG_DM_I2C */