blob: b8cdd1c661334fedd866ec100303f533c2e54042 [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
Ley Foon Tan6e85c812019-06-12 09:48:04 +08007#include <clk.h>
Vipin KUMARfc9589f2010-01-15 19:15:44 +05308#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>
Stefan Roese38481202016-04-21 08:19:42 +020011#include <pci.h>
Dinh Nguyen08794aa2018-04-04 17:18:24 -050012#include <reset.h>
Vipin KUMARfc9589f2010-01-15 19:15:44 +053013#include <asm/io.h>
Vipin KUMAR3f64acb2012-02-26 23:13:29 +000014#include "designware_i2c.h"
Vipin KUMARfc9589f2010-01-15 19:15:44 +053015
Stefan Roeseabb3e132016-04-27 09:02:12 +020016#ifdef CONFIG_SYS_I2C_DW_ENABLE_STATUS_UNSUPPORTED
Simon Glassbd9ca8d2019-02-16 20:24:39 -070017static int dw_i2c_enable(struct i2c_regs *i2c_base, bool enable)
Stefan Roese3bc33ba2016-04-21 08:19:38 +020018{
19 u32 ena = enable ? IC_ENABLE_0B : 0;
Stefan Roeseabb3e132016-04-27 09:02:12 +020020
21 writel(ena, &i2c_base->ic_enable);
Simon Glassbd9ca8d2019-02-16 20:24:39 -070022
23 return 0;
Stefan Roeseabb3e132016-04-27 09:02:12 +020024}
25#else
Simon Glassbd9ca8d2019-02-16 20:24:39 -070026static int dw_i2c_enable(struct i2c_regs *i2c_base, bool enable)
Stefan Roeseabb3e132016-04-27 09:02:12 +020027{
28 u32 ena = enable ? IC_ENABLE_0B : 0;
Stefan Roese3bc33ba2016-04-21 08:19:38 +020029 int timeout = 100;
30
31 do {
32 writel(ena, &i2c_base->ic_enable);
33 if ((readl(&i2c_base->ic_enable_status) & IC_ENABLE_0B) == ena)
Simon Glassbd9ca8d2019-02-16 20:24:39 -070034 return 0;
Stefan Roese3bc33ba2016-04-21 08:19:38 +020035
36 /*
37 * Wait 10 times the signaling period of the highest I2C
38 * transfer supported by the driver (for 400KHz this is
39 * 25us) as described in the DesignWare I2C databook.
40 */
41 udelay(25);
42 } while (timeout--);
Stefan Roese3bc33ba2016-04-21 08:19:38 +020043 printf("timeout in %sabling I2C adapter\n", enable ? "en" : "dis");
Simon Glassbd9ca8d2019-02-16 20:24:39 -070044
45 return -ETIMEDOUT;
Stefan Roese3bc33ba2016-04-21 08:19:38 +020046}
Stefan Roeseabb3e132016-04-27 09:02:12 +020047#endif
Stefan Roese3bc33ba2016-04-21 08:19:38 +020048
Vipin KUMARfc9589f2010-01-15 19:15:44 +053049/*
Stefan Roese88893c92016-04-21 08:19:39 +020050 * i2c_set_bus_speed - Set the i2c speed
51 * @speed: required i2c speed
Vipin KUMARfc9589f2010-01-15 19:15:44 +053052 *
Stefan Roese88893c92016-04-21 08:19:39 +020053 * Set the i2c speed.
Vipin KUMARfc9589f2010-01-15 19:15:44 +053054 */
Stefan Roese41de7662016-04-21 08:19:40 +020055static unsigned int __dw_i2c_set_bus_speed(struct i2c_regs *i2c_base,
Stefan Roese38481202016-04-21 08:19:42 +020056 struct dw_scl_sda_cfg *scl_sda_cfg,
Ley Foon Tan6e85c812019-06-12 09:48:04 +080057 unsigned int speed,
58 unsigned int bus_mhz)
Vipin KUMARfc9589f2010-01-15 19:15:44 +053059{
60 unsigned int cntl;
61 unsigned int hcnt, lcnt;
Jun Chend003a372019-06-05 15:23:16 +080062 unsigned int ena;
Stefan Roese88893c92016-04-21 08:19:39 +020063 int i2c_spd;
64
Simon Glasse2be5532019-12-06 21:41:40 -070065 /* Allow max speed if there is no config, or the config allows it */
66 if (speed >= I2C_MAX_SPEED &&
67 (!scl_sda_cfg || scl_sda_cfg->has_max_speed))
Stefan Roese88893c92016-04-21 08:19:39 +020068 i2c_spd = IC_SPEED_MODE_MAX;
69 else if (speed >= I2C_FAST_SPEED)
70 i2c_spd = IC_SPEED_MODE_FAST;
71 else
72 i2c_spd = IC_SPEED_MODE_STANDARD;
Armando Visconti631e6932012-03-29 20:10:17 +000073
Jun Chend003a372019-06-05 15:23:16 +080074 /* Get enable setting for restore later */
75 ena = readl(&i2c_base->ic_enable) & IC_ENABLE_0B;
76
Armando Visconti631e6932012-03-29 20:10:17 +000077 /* to set speed cltr must be disabled */
Stefan Roese3bc33ba2016-04-21 08:19:38 +020078 dw_i2c_enable(i2c_base, false);
Armando Visconti631e6932012-03-29 20:10:17 +000079
Stefan Roeseef6073e2014-10-28 12:12:00 +010080 cntl = (readl(&i2c_base->ic_con) & (~IC_CON_SPD_MSK));
Vipin KUMARfc9589f2010-01-15 19:15:44 +053081
82 switch (i2c_spd) {
83 case IC_SPEED_MODE_MAX:
Stefan Roese38481202016-04-21 08:19:42 +020084 cntl |= IC_CON_SPD_SS;
85 if (scl_sda_cfg) {
86 hcnt = scl_sda_cfg->fs_hcnt;
87 lcnt = scl_sda_cfg->fs_lcnt;
88 } else {
Ley Foon Tan6e85c812019-06-12 09:48:04 +080089 hcnt = (bus_mhz * MIN_HS_SCL_HIGHTIME) / NANO_TO_MICRO;
90 lcnt = (bus_mhz * MIN_HS_SCL_LOWTIME) / NANO_TO_MICRO;
Stefan Roese38481202016-04-21 08:19:42 +020091 }
Stefan Roeseef6073e2014-10-28 12:12:00 +010092 writel(hcnt, &i2c_base->ic_hs_scl_hcnt);
Stefan Roeseef6073e2014-10-28 12:12:00 +010093 writel(lcnt, &i2c_base->ic_hs_scl_lcnt);
Vipin KUMARfc9589f2010-01-15 19:15:44 +053094 break;
95
96 case IC_SPEED_MODE_STANDARD:
97 cntl |= IC_CON_SPD_SS;
Stefan Roese38481202016-04-21 08:19:42 +020098 if (scl_sda_cfg) {
99 hcnt = scl_sda_cfg->ss_hcnt;
100 lcnt = scl_sda_cfg->ss_lcnt;
101 } else {
Ley Foon Tan6e85c812019-06-12 09:48:04 +0800102 hcnt = (bus_mhz * MIN_SS_SCL_HIGHTIME) / NANO_TO_MICRO;
103 lcnt = (bus_mhz * MIN_SS_SCL_LOWTIME) / NANO_TO_MICRO;
Stefan Roese38481202016-04-21 08:19:42 +0200104 }
Stefan Roeseef6073e2014-10-28 12:12:00 +0100105 writel(hcnt, &i2c_base->ic_ss_scl_hcnt);
Stefan Roeseef6073e2014-10-28 12:12:00 +0100106 writel(lcnt, &i2c_base->ic_ss_scl_lcnt);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530107 break;
108
109 case IC_SPEED_MODE_FAST:
110 default:
111 cntl |= IC_CON_SPD_FS;
Stefan Roese38481202016-04-21 08:19:42 +0200112 if (scl_sda_cfg) {
113 hcnt = scl_sda_cfg->fs_hcnt;
114 lcnt = scl_sda_cfg->fs_lcnt;
115 } else {
Ley Foon Tan6e85c812019-06-12 09:48:04 +0800116 hcnt = (bus_mhz * MIN_FS_SCL_HIGHTIME) / NANO_TO_MICRO;
117 lcnt = (bus_mhz * MIN_FS_SCL_LOWTIME) / NANO_TO_MICRO;
Stefan Roese38481202016-04-21 08:19:42 +0200118 }
Stefan Roeseef6073e2014-10-28 12:12:00 +0100119 writel(hcnt, &i2c_base->ic_fs_scl_hcnt);
Stefan Roeseef6073e2014-10-28 12:12:00 +0100120 writel(lcnt, &i2c_base->ic_fs_scl_lcnt);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530121 break;
122 }
123
Stefan Roeseef6073e2014-10-28 12:12:00 +0100124 writel(cntl, &i2c_base->ic_con);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530125
Stefan Roese38481202016-04-21 08:19:42 +0200126 /* Configure SDA Hold Time if required */
127 if (scl_sda_cfg)
128 writel(scl_sda_cfg->sda_hold, &i2c_base->ic_sda_hold);
129
Jun Chend003a372019-06-05 15:23:16 +0800130 /* Restore back i2c now speed set */
131 if (ena == IC_ENABLE_0B)
132 dw_i2c_enable(i2c_base, true);
Stefan Roesef6322ebd2012-01-20 11:52:33 +0100133
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530134 return 0;
135}
136
137/*
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530138 * i2c_setaddress - Sets the target slave address
139 * @i2c_addr: target i2c address
140 *
141 * Sets the target slave address.
142 */
Stefan Roese41de7662016-04-21 08:19:40 +0200143static void i2c_setaddress(struct i2c_regs *i2c_base, unsigned int i2c_addr)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530144{
Alexey Brodkin41c56552013-11-07 17:52:18 +0400145 /* Disable i2c */
Stefan Roese3bc33ba2016-04-21 08:19:38 +0200146 dw_i2c_enable(i2c_base, false);
Alexey Brodkin41c56552013-11-07 17:52:18 +0400147
Stefan Roeseef6073e2014-10-28 12:12:00 +0100148 writel(i2c_addr, &i2c_base->ic_tar);
Alexey Brodkin41c56552013-11-07 17:52:18 +0400149
150 /* Enable i2c */
Stefan Roese3bc33ba2016-04-21 08:19:38 +0200151 dw_i2c_enable(i2c_base, true);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530152}
153
154/*
155 * i2c_flush_rxfifo - Flushes the i2c RX FIFO
156 *
157 * Flushes the i2c RX FIFO
158 */
Stefan Roese41de7662016-04-21 08:19:40 +0200159static void i2c_flush_rxfifo(struct i2c_regs *i2c_base)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530160{
Stefan Roeseef6073e2014-10-28 12:12:00 +0100161 while (readl(&i2c_base->ic_status) & IC_STATUS_RFNE)
162 readl(&i2c_base->ic_cmd_data);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530163}
164
165/*
166 * i2c_wait_for_bb - Waits for bus busy
167 *
168 * Waits for bus busy
169 */
Stefan Roese41de7662016-04-21 08:19:40 +0200170static int i2c_wait_for_bb(struct i2c_regs *i2c_base)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530171{
172 unsigned long start_time_bb = get_timer(0);
173
Stefan Roeseef6073e2014-10-28 12:12:00 +0100174 while ((readl(&i2c_base->ic_status) & IC_STATUS_MA) ||
175 !(readl(&i2c_base->ic_status) & IC_STATUS_TFE)) {
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530176
177 /* Evaluate timeout */
178 if (get_timer(start_time_bb) > (unsigned long)(I2C_BYTE_TO_BB))
179 return 1;
180 }
181
182 return 0;
183}
184
Stefan Roese41de7662016-04-21 08:19:40 +0200185static int i2c_xfer_init(struct i2c_regs *i2c_base, uchar chip, uint addr,
Stefan Roeseef6073e2014-10-28 12:12:00 +0100186 int alen)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530187{
Stefan Roese41de7662016-04-21 08:19:40 +0200188 if (i2c_wait_for_bb(i2c_base))
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530189 return 1;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530190
Stefan Roese41de7662016-04-21 08:19:40 +0200191 i2c_setaddress(i2c_base, chip);
Chin Liang Seea0c26262014-02-04 11:56:23 -0600192 while (alen) {
193 alen--;
194 /* high byte address going out first */
195 writel((addr >> (alen * 8)) & 0xff,
Stefan Roeseef6073e2014-10-28 12:12:00 +0100196 &i2c_base->ic_cmd_data);
Chin Liang Seea0c26262014-02-04 11:56:23 -0600197 }
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530198 return 0;
199}
200
Stefan Roese41de7662016-04-21 08:19:40 +0200201static int i2c_xfer_finish(struct i2c_regs *i2c_base)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530202{
203 ulong start_stop_det = get_timer(0);
204
205 while (1) {
Stefan Roeseef6073e2014-10-28 12:12:00 +0100206 if ((readl(&i2c_base->ic_raw_intr_stat) & IC_STOP_DET)) {
207 readl(&i2c_base->ic_clr_stop_det);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530208 break;
209 } else if (get_timer(start_stop_det) > I2C_STOPDET_TO) {
210 break;
211 }
212 }
213
Stefan Roese41de7662016-04-21 08:19:40 +0200214 if (i2c_wait_for_bb(i2c_base)) {
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530215 printf("Timed out waiting for bus\n");
216 return 1;
217 }
218
Stefan Roese41de7662016-04-21 08:19:40 +0200219 i2c_flush_rxfifo(i2c_base);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530220
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530221 return 0;
222}
223
224/*
225 * i2c_read - Read from i2c memory
226 * @chip: target i2c address
227 * @addr: address to read from
228 * @alen:
229 * @buffer: buffer for read data
230 * @len: no of bytes to be read
231 *
232 * Read from i2c memory.
233 */
Stefan Roese41de7662016-04-21 08:19:40 +0200234static int __dw_i2c_read(struct i2c_regs *i2c_base, u8 dev, uint addr,
235 int alen, u8 *buffer, int len)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530236{
237 unsigned long start_time_rx;
Marek Vasutc4bc9a82016-10-20 16:48:28 +0200238 unsigned int active = 0;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530239
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400240#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
241 /*
242 * EEPROM chips that implement "address overflow" are ones
243 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
244 * address and the extra bits end up in the "chip address"
245 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
246 * four 256 byte chips.
247 *
248 * Note that we consider the length of the address field to
249 * still be one byte because the extra address bits are
250 * hidden in the chip address.
251 */
Stefan Roeseef6073e2014-10-28 12:12:00 +0100252 dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400253 addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
254
Stefan Roeseef6073e2014-10-28 12:12:00 +0100255 debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400256 addr);
257#endif
258
Stefan Roese41de7662016-04-21 08:19:40 +0200259 if (i2c_xfer_init(i2c_base, dev, addr, alen))
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530260 return 1;
261
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530262 start_time_rx = get_timer(0);
263 while (len) {
Marek Vasutc4bc9a82016-10-20 16:48:28 +0200264 if (!active) {
265 /*
266 * Avoid writing to ic_cmd_data multiple times
267 * in case this loop spins too quickly and the
268 * ic_status RFNE bit isn't set after the first
269 * write. Subsequent writes to ic_cmd_data can
270 * trigger spurious i2c transfer.
271 */
272 if (len == 1)
273 writel(IC_CMD | IC_STOP, &i2c_base->ic_cmd_data);
274 else
275 writel(IC_CMD, &i2c_base->ic_cmd_data);
276 active = 1;
277 }
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530278
Stefan Roeseef6073e2014-10-28 12:12:00 +0100279 if (readl(&i2c_base->ic_status) & IC_STATUS_RFNE) {
280 *buffer++ = (uchar)readl(&i2c_base->ic_cmd_data);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530281 len--;
282 start_time_rx = get_timer(0);
Marek Vasutc4bc9a82016-10-20 16:48:28 +0200283 active = 0;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530284 } else if (get_timer(start_time_rx) > I2C_BYTE_TO) {
Marek Vasutc4bc9a82016-10-20 16:48:28 +0200285 return 1;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530286 }
287 }
288
Stefan Roese41de7662016-04-21 08:19:40 +0200289 return i2c_xfer_finish(i2c_base);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530290}
291
292/*
293 * i2c_write - Write to i2c memory
294 * @chip: target i2c address
295 * @addr: address to read from
296 * @alen:
297 * @buffer: buffer for read data
298 * @len: no of bytes to be read
299 *
300 * Write to i2c memory.
301 */
Stefan Roese41de7662016-04-21 08:19:40 +0200302static int __dw_i2c_write(struct i2c_regs *i2c_base, u8 dev, uint addr,
303 int alen, u8 *buffer, int len)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530304{
305 int nb = len;
306 unsigned long start_time_tx;
307
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400308#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
309 /*
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.
315 *
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.
319 */
Stefan Roeseef6073e2014-10-28 12:12:00 +0100320 dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400321 addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
322
Stefan Roeseef6073e2014-10-28 12:12:00 +0100323 debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400324 addr);
325#endif
326
Stefan Roese41de7662016-04-21 08:19:40 +0200327 if (i2c_xfer_init(i2c_base, dev, addr, alen))
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530328 return 1;
329
330 start_time_tx = get_timer(0);
331 while (len) {
Stefan Roeseef6073e2014-10-28 12:12:00 +0100332 if (readl(&i2c_base->ic_status) & IC_STATUS_TFNF) {
333 if (--len == 0) {
334 writel(*buffer | IC_STOP,
335 &i2c_base->ic_cmd_data);
336 } else {
337 writel(*buffer, &i2c_base->ic_cmd_data);
338 }
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530339 buffer++;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530340 start_time_tx = get_timer(0);
341
342 } else if (get_timer(start_time_tx) > (nb * I2C_BYTE_TO)) {
343 printf("Timed out. i2c write Failed\n");
344 return 1;
345 }
346 }
347
Stefan Roese41de7662016-04-21 08:19:40 +0200348 return i2c_xfer_finish(i2c_base);
349}
350
Stefan Roese3cb27962016-04-21 08:19:41 +0200351/*
352 * __dw_i2c_init - Init function
353 * @speed: required i2c speed
354 * @slaveaddr: slave address for the device
355 *
356 * Initialization function.
357 */
Simon Glassbd9ca8d2019-02-16 20:24:39 -0700358static int __dw_i2c_init(struct i2c_regs *i2c_base, int speed, int slaveaddr)
Stefan Roese3cb27962016-04-21 08:19:41 +0200359{
Simon Glassbd9ca8d2019-02-16 20:24:39 -0700360 int ret;
361
Stefan Roese3cb27962016-04-21 08:19:41 +0200362 /* Disable i2c */
Simon Glassbd9ca8d2019-02-16 20:24:39 -0700363 ret = dw_i2c_enable(i2c_base, false);
364 if (ret)
365 return ret;
Stefan Roese3cb27962016-04-21 08:19:41 +0200366
Marek Vasut808aa132017-08-07 20:45:31 +0200367 writel(IC_CON_SD | IC_CON_RE | IC_CON_SPD_FS | IC_CON_MM,
368 &i2c_base->ic_con);
Stefan Roese3cb27962016-04-21 08:19:41 +0200369 writel(IC_RX_TL, &i2c_base->ic_rx_tl);
370 writel(IC_TX_TL, &i2c_base->ic_tx_tl);
371 writel(IC_STOP_DET, &i2c_base->ic_intr_mask);
372#ifndef CONFIG_DM_I2C
Ley Foon Tan6e85c812019-06-12 09:48:04 +0800373 __dw_i2c_set_bus_speed(i2c_base, NULL, speed, IC_CLK);
Stefan Roese3cb27962016-04-21 08:19:41 +0200374 writel(slaveaddr, &i2c_base->ic_sar);
375#endif
376
377 /* Enable i2c */
Simon Glassbd9ca8d2019-02-16 20:24:39 -0700378 ret = dw_i2c_enable(i2c_base, true);
379 if (ret)
380 return ret;
381
382 return 0;
Stefan Roese3cb27962016-04-21 08:19:41 +0200383}
384
385#ifndef CONFIG_DM_I2C
386/*
387 * The legacy I2C functions. These need to get removed once
388 * all users of this driver are converted to DM.
389 */
Stefan Roese41de7662016-04-21 08:19:40 +0200390static struct i2c_regs *i2c_get_base(struct i2c_adapter *adap)
391{
392 switch (adap->hwadapnr) {
393#if CONFIG_SYS_I2C_BUS_MAX >= 4
394 case 3:
395 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE3;
396#endif
397#if CONFIG_SYS_I2C_BUS_MAX >= 3
398 case 2:
399 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE2;
400#endif
401#if CONFIG_SYS_I2C_BUS_MAX >= 2
402 case 1:
403 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE1;
404#endif
405 case 0:
406 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE;
407 default:
408 printf("Wrong I2C-adapter number %d\n", adap->hwadapnr);
409 }
410
411 return NULL;
412}
413
414static unsigned int dw_i2c_set_bus_speed(struct i2c_adapter *adap,
415 unsigned int speed)
416{
417 adap->speed = speed;
Ley Foon Tan6e85c812019-06-12 09:48:04 +0800418 return __dw_i2c_set_bus_speed(i2c_get_base(adap), NULL, speed, IC_CLK);
Stefan Roese41de7662016-04-21 08:19:40 +0200419}
420
Stefan Roese3cb27962016-04-21 08:19:41 +0200421static void dw_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
Stefan Roese41de7662016-04-21 08:19:40 +0200422{
Stefan Roese3cb27962016-04-21 08:19:41 +0200423 __dw_i2c_init(i2c_get_base(adap), speed, slaveaddr);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530424}
425
Stefan Roese41de7662016-04-21 08:19:40 +0200426static int dw_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr,
427 int alen, u8 *buffer, int len)
428{
429 return __dw_i2c_read(i2c_get_base(adap), dev, addr, alen, buffer, len);
430}
431
432static int dw_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr,
433 int alen, u8 *buffer, int len)
434{
435 return __dw_i2c_write(i2c_get_base(adap), dev, addr, alen, buffer, len);
436}
437
Stefan Roese3cb27962016-04-21 08:19:41 +0200438/* dw_i2c_probe - Probe the i2c chip */
Stefan Roeseef6073e2014-10-28 12:12:00 +0100439static int dw_i2c_probe(struct i2c_adapter *adap, u8 dev)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530440{
Stefan Roese41de7662016-04-21 08:19:40 +0200441 struct i2c_regs *i2c_base = i2c_get_base(adap);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530442 u32 tmp;
Stefan Roesef6322ebd2012-01-20 11:52:33 +0100443 int ret;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530444
445 /*
446 * Try to read the first location of the chip.
447 */
Stefan Roese41de7662016-04-21 08:19:40 +0200448 ret = __dw_i2c_read(i2c_base, dev, 0, 1, (uchar *)&tmp, 1);
Stefan Roesef6322ebd2012-01-20 11:52:33 +0100449 if (ret)
Stefan Roeseef6073e2014-10-28 12:12:00 +0100450 dw_i2c_init(adap, adap->speed, adap->slaveaddr);
Stefan Roesef6322ebd2012-01-20 11:52:33 +0100451
452 return ret;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530453}
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000454
Stefan Roeseef6073e2014-10-28 12:12:00 +0100455U_BOOT_I2C_ADAP_COMPLETE(dw_0, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
456 dw_i2c_write, dw_i2c_set_bus_speed,
457 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0)
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000458
Stefan Roeseef6073e2014-10-28 12:12:00 +0100459#if CONFIG_SYS_I2C_BUS_MAX >= 2
460U_BOOT_I2C_ADAP_COMPLETE(dw_1, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
461 dw_i2c_write, dw_i2c_set_bus_speed,
462 CONFIG_SYS_I2C_SPEED1, CONFIG_SYS_I2C_SLAVE1, 1)
463#endif
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000464
Stefan Roeseef6073e2014-10-28 12:12:00 +0100465#if CONFIG_SYS_I2C_BUS_MAX >= 3
466U_BOOT_I2C_ADAP_COMPLETE(dw_2, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
467 dw_i2c_write, dw_i2c_set_bus_speed,
468 CONFIG_SYS_I2C_SPEED2, CONFIG_SYS_I2C_SLAVE2, 2)
469#endif
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000470
Stefan Roeseef6073e2014-10-28 12:12:00 +0100471#if CONFIG_SYS_I2C_BUS_MAX >= 4
472U_BOOT_I2C_ADAP_COMPLETE(dw_3, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
473 dw_i2c_write, dw_i2c_set_bus_speed,
474 CONFIG_SYS_I2C_SPEED3, CONFIG_SYS_I2C_SLAVE3, 3)
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000475#endif
Stefan Roese3cb27962016-04-21 08:19:41 +0200476
477#else /* CONFIG_DM_I2C */
478/* The DM I2C functions */
479
480static int designware_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
481 int nmsgs)
482{
483 struct dw_i2c *i2c = dev_get_priv(bus);
484 int ret;
485
486 debug("i2c_xfer: %d messages\n", nmsgs);
487 for (; nmsgs > 0; nmsgs--, msg++) {
488 debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
489 if (msg->flags & I2C_M_RD) {
490 ret = __dw_i2c_read(i2c->regs, msg->addr, 0, 0,
491 msg->buf, msg->len);
492 } else {
493 ret = __dw_i2c_write(i2c->regs, msg->addr, 0, 0,
494 msg->buf, msg->len);
495 }
496 if (ret) {
497 debug("i2c_write: error sending\n");
498 return -EREMOTEIO;
499 }
500 }
501
502 return 0;
503}
504
505static int designware_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
506{
507 struct dw_i2c *i2c = dev_get_priv(bus);
Ley Foon Tan6e85c812019-06-12 09:48:04 +0800508 ulong rate;
509
510#if CONFIG_IS_ENABLED(CLK)
511 rate = clk_get_rate(&i2c->clk);
512 if (IS_ERR_VALUE(rate))
513 return -EINVAL;
Stefan Roese3cb27962016-04-21 08:19:41 +0200514
Ley Foon Tan6e85c812019-06-12 09:48:04 +0800515 /* Convert to MHz */
516 rate /= 1000000;
517#else
518 rate = IC_CLK;
519#endif
520 return __dw_i2c_set_bus_speed(i2c->regs, i2c->scl_sda_cfg, speed,
521 rate);
Stefan Roese3cb27962016-04-21 08:19:41 +0200522}
523
524static int designware_i2c_probe_chip(struct udevice *bus, uint chip_addr,
525 uint chip_flags)
526{
527 struct dw_i2c *i2c = dev_get_priv(bus);
528 struct i2c_regs *i2c_base = i2c->regs;
529 u32 tmp;
530 int ret;
531
532 /* Try to read the first location of the chip */
533 ret = __dw_i2c_read(i2c_base, chip_addr, 0, 1, (uchar *)&tmp, 1);
534 if (ret)
535 __dw_i2c_init(i2c_base, 0, 0);
536
537 return ret;
538}
539
Simon Glasse2be5532019-12-06 21:41:40 -0700540static int designware_i2c_ofdata_to_platdata(struct udevice *bus)
Stefan Roese3cb27962016-04-21 08:19:41 +0200541{
542 struct dw_i2c *priv = dev_get_priv(bus);
543
Simon Glasse2be5532019-12-06 21:41:40 -0700544 priv->regs = (struct i2c_regs *)devfdt_get_addr_ptr(bus);
545
546 return 0;
547}
548
549int designware_i2c_probe(struct udevice *bus)
550{
551 struct dw_i2c *priv = dev_get_priv(bus);
552 int ret;
Stefan Roese3cb27962016-04-21 08:19:41 +0200553
Simon Goldschmidt28608a12019-03-28 21:11:48 +0100554 ret = reset_get_bulk(bus, &priv->resets);
Dinh Nguyen08794aa2018-04-04 17:18:24 -0500555 if (ret)
Simon Goldschmidt28608a12019-03-28 21:11:48 +0100556 dev_warn(bus, "Can't get reset: %d\n", ret);
557 else
558 reset_deassert_bulk(&priv->resets);
Dinh Nguyen08794aa2018-04-04 17:18:24 -0500559
Ley Foon Tan6e85c812019-06-12 09:48:04 +0800560#if CONFIG_IS_ENABLED(CLK)
561 ret = clk_get_by_index(bus, 0, &priv->clk);
562 if (ret)
563 return ret;
564
565 ret = clk_enable(&priv->clk);
566 if (ret && ret != -ENOSYS && ret != -ENOTSUPP) {
567 clk_free(&priv->clk);
568 dev_err(bus, "failed to enable clock\n");
569 return ret;
570 }
571#endif
572
Simon Glassbd9ca8d2019-02-16 20:24:39 -0700573 return __dw_i2c_init(priv->regs, 0, 0);
Stefan Roese3cb27962016-04-21 08:19:41 +0200574}
575
Simon Glasse2be5532019-12-06 21:41:40 -0700576int designware_i2c_remove(struct udevice *dev)
Simon Goldschmidt28608a12019-03-28 21:11:48 +0100577{
578 struct dw_i2c *priv = dev_get_priv(dev);
579
Ley Foon Tan6e85c812019-06-12 09:48:04 +0800580#if CONFIG_IS_ENABLED(CLK)
581 clk_disable(&priv->clk);
582 clk_free(&priv->clk);
583#endif
584
Simon Goldschmidt28608a12019-03-28 21:11:48 +0100585 return reset_release_bulk(&priv->resets);
586}
587
Simon Glasse2be5532019-12-06 21:41:40 -0700588const struct dm_i2c_ops designware_i2c_ops = {
Stefan Roese3cb27962016-04-21 08:19:41 +0200589 .xfer = designware_i2c_xfer,
590 .probe_chip = designware_i2c_probe_chip,
591 .set_bus_speed = designware_i2c_set_bus_speed,
592};
593
594static const struct udevice_id designware_i2c_ids[] = {
595 { .compatible = "snps,designware-i2c" },
596 { }
597};
598
599U_BOOT_DRIVER(i2c_designware) = {
600 .name = "i2c_designware",
601 .id = UCLASS_I2C,
602 .of_match = designware_i2c_ids,
Simon Glasse2be5532019-12-06 21:41:40 -0700603 .ofdata_to_platdata = designware_i2c_ofdata_to_platdata,
Stefan Roese3cb27962016-04-21 08:19:41 +0200604 .probe = designware_i2c_probe,
605 .priv_auto_alloc_size = sizeof(struct dw_i2c),
Simon Goldschmidt28608a12019-03-28 21:11:48 +0100606 .remove = designware_i2c_remove,
Simon Glasse2be5532019-12-06 21:41:40 -0700607 .flags = DM_FLAG_OS_PREPARE,
Stefan Roese3cb27962016-04-21 08:19:41 +0200608 .ops = &designware_i2c_ops,
609};
610
611#endif /* CONFIG_DM_I2C */