blob: 0a1df8015fee86c4df2282e2e9dd065d4a59283a [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>
Simon Glass9b7af642020-01-23 11:48:06 -07008#include <clk.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,
Simon Glass333dadd2020-01-23 11:48:09 -070058 unsigned int bus_clk)
Vipin KUMARfc9589f2010-01-15 19:15:44 +053059{
Simon Glass333dadd2020-01-23 11:48:09 -070060 ulong bus_khz = bus_clk / 1000;
Simon Glass6ed44ae2020-01-23 11:48:08 -070061 enum i2c_speed_mode i2c_spd;
Vipin KUMARfc9589f2010-01-15 19:15:44 +053062 unsigned int cntl;
63 unsigned int hcnt, lcnt;
Jun Chend003a372019-06-05 15:23:16 +080064 unsigned int ena;
Stefan Roese88893c92016-04-21 08:19:39 +020065
Simon Glassf5ef1012020-01-23 11:48:07 -070066 /* Allow high speed if there is no config, or the config allows it */
67 if (speed >= I2C_HIGH_SPEED &&
68 (!scl_sda_cfg || scl_sda_cfg->has_high_speed))
69 i2c_spd = IC_SPEED_MODE_HIGH;
Stefan Roese88893c92016-04-21 08:19:39 +020070 else if (speed >= I2C_FAST_SPEED)
71 i2c_spd = IC_SPEED_MODE_FAST;
72 else
73 i2c_spd = IC_SPEED_MODE_STANDARD;
Armando Visconti631e6932012-03-29 20:10:17 +000074
Jun Chend003a372019-06-05 15:23:16 +080075 /* Get enable setting for restore later */
76 ena = readl(&i2c_base->ic_enable) & IC_ENABLE_0B;
77
Armando Visconti631e6932012-03-29 20:10:17 +000078 /* to set speed cltr must be disabled */
Stefan Roese3bc33ba2016-04-21 08:19:38 +020079 dw_i2c_enable(i2c_base, false);
Armando Visconti631e6932012-03-29 20:10:17 +000080
Stefan Roeseef6073e2014-10-28 12:12:00 +010081 cntl = (readl(&i2c_base->ic_con) & (~IC_CON_SPD_MSK));
Vipin KUMARfc9589f2010-01-15 19:15:44 +053082
83 switch (i2c_spd) {
Simon Glassf5ef1012020-01-23 11:48:07 -070084 case IC_SPEED_MODE_HIGH:
Stefan Roese38481202016-04-21 08:19:42 +020085 cntl |= IC_CON_SPD_SS;
86 if (scl_sda_cfg) {
87 hcnt = scl_sda_cfg->fs_hcnt;
88 lcnt = scl_sda_cfg->fs_lcnt;
89 } else {
Simon Glass333dadd2020-01-23 11:48:09 -070090 hcnt = (bus_khz * MIN_HS_SCL_HIGHTIME) / NANO_TO_KILO;
91 lcnt = (bus_khz * MIN_HS_SCL_LOWTIME) / NANO_TO_KILO;
Stefan Roese38481202016-04-21 08:19:42 +020092 }
Stefan Roeseef6073e2014-10-28 12:12:00 +010093 writel(hcnt, &i2c_base->ic_hs_scl_hcnt);
Stefan Roeseef6073e2014-10-28 12:12:00 +010094 writel(lcnt, &i2c_base->ic_hs_scl_lcnt);
Vipin KUMARfc9589f2010-01-15 19:15:44 +053095 break;
96
97 case IC_SPEED_MODE_STANDARD:
98 cntl |= IC_CON_SPD_SS;
Stefan Roese38481202016-04-21 08:19:42 +020099 if (scl_sda_cfg) {
100 hcnt = scl_sda_cfg->ss_hcnt;
101 lcnt = scl_sda_cfg->ss_lcnt;
102 } else {
Simon Glass333dadd2020-01-23 11:48:09 -0700103 hcnt = (bus_khz * MIN_SS_SCL_HIGHTIME) / NANO_TO_KILO;
104 lcnt = (bus_khz * MIN_SS_SCL_LOWTIME) / NANO_TO_KILO;
Stefan Roese38481202016-04-21 08:19:42 +0200105 }
Stefan Roeseef6073e2014-10-28 12:12:00 +0100106 writel(hcnt, &i2c_base->ic_ss_scl_hcnt);
Stefan Roeseef6073e2014-10-28 12:12:00 +0100107 writel(lcnt, &i2c_base->ic_ss_scl_lcnt);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530108 break;
109
110 case IC_SPEED_MODE_FAST:
111 default:
112 cntl |= IC_CON_SPD_FS;
Stefan Roese38481202016-04-21 08:19:42 +0200113 if (scl_sda_cfg) {
114 hcnt = scl_sda_cfg->fs_hcnt;
115 lcnt = scl_sda_cfg->fs_lcnt;
116 } else {
Simon Glass333dadd2020-01-23 11:48:09 -0700117 hcnt = (bus_khz * MIN_FS_SCL_HIGHTIME) / NANO_TO_KILO;
118 lcnt = (bus_khz * MIN_FS_SCL_LOWTIME) / NANO_TO_KILO;
Stefan Roese38481202016-04-21 08:19:42 +0200119 }
Stefan Roeseef6073e2014-10-28 12:12:00 +0100120 writel(hcnt, &i2c_base->ic_fs_scl_hcnt);
Stefan Roeseef6073e2014-10-28 12:12:00 +0100121 writel(lcnt, &i2c_base->ic_fs_scl_lcnt);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530122 break;
123 }
124
Stefan Roeseef6073e2014-10-28 12:12:00 +0100125 writel(cntl, &i2c_base->ic_con);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530126
Stefan Roese38481202016-04-21 08:19:42 +0200127 /* Configure SDA Hold Time if required */
128 if (scl_sda_cfg)
129 writel(scl_sda_cfg->sda_hold, &i2c_base->ic_sda_hold);
130
Jun Chend003a372019-06-05 15:23:16 +0800131 /* Restore back i2c now speed set */
132 if (ena == IC_ENABLE_0B)
133 dw_i2c_enable(i2c_base, true);
Stefan Roesef6322ebd2012-01-20 11:52:33 +0100134
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530135 return 0;
136}
137
138/*
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530139 * i2c_setaddress - Sets the target slave address
140 * @i2c_addr: target i2c address
141 *
142 * Sets the target slave address.
143 */
Stefan Roese41de7662016-04-21 08:19:40 +0200144static void i2c_setaddress(struct i2c_regs *i2c_base, unsigned int i2c_addr)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530145{
Alexey Brodkin41c56552013-11-07 17:52:18 +0400146 /* Disable i2c */
Stefan Roese3bc33ba2016-04-21 08:19:38 +0200147 dw_i2c_enable(i2c_base, false);
Alexey Brodkin41c56552013-11-07 17:52:18 +0400148
Stefan Roeseef6073e2014-10-28 12:12:00 +0100149 writel(i2c_addr, &i2c_base->ic_tar);
Alexey Brodkin41c56552013-11-07 17:52:18 +0400150
151 /* Enable i2c */
Stefan Roese3bc33ba2016-04-21 08:19:38 +0200152 dw_i2c_enable(i2c_base, true);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530153}
154
155/*
156 * i2c_flush_rxfifo - Flushes the i2c RX FIFO
157 *
158 * Flushes the i2c RX FIFO
159 */
Stefan Roese41de7662016-04-21 08:19:40 +0200160static void i2c_flush_rxfifo(struct i2c_regs *i2c_base)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530161{
Stefan Roeseef6073e2014-10-28 12:12:00 +0100162 while (readl(&i2c_base->ic_status) & IC_STATUS_RFNE)
163 readl(&i2c_base->ic_cmd_data);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530164}
165
166/*
167 * i2c_wait_for_bb - Waits for bus busy
168 *
169 * Waits for bus busy
170 */
Stefan Roese41de7662016-04-21 08:19:40 +0200171static int i2c_wait_for_bb(struct i2c_regs *i2c_base)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530172{
173 unsigned long start_time_bb = get_timer(0);
174
Stefan Roeseef6073e2014-10-28 12:12:00 +0100175 while ((readl(&i2c_base->ic_status) & IC_STATUS_MA) ||
176 !(readl(&i2c_base->ic_status) & IC_STATUS_TFE)) {
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530177
178 /* Evaluate timeout */
179 if (get_timer(start_time_bb) > (unsigned long)(I2C_BYTE_TO_BB))
180 return 1;
181 }
182
183 return 0;
184}
185
Stefan Roese41de7662016-04-21 08:19:40 +0200186static int i2c_xfer_init(struct i2c_regs *i2c_base, uchar chip, uint addr,
Stefan Roeseef6073e2014-10-28 12:12:00 +0100187 int alen)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530188{
Stefan Roese41de7662016-04-21 08:19:40 +0200189 if (i2c_wait_for_bb(i2c_base))
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530190 return 1;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530191
Stefan Roese41de7662016-04-21 08:19:40 +0200192 i2c_setaddress(i2c_base, chip);
Chin Liang Seea0c26262014-02-04 11:56:23 -0600193 while (alen) {
194 alen--;
195 /* high byte address going out first */
196 writel((addr >> (alen * 8)) & 0xff,
Stefan Roeseef6073e2014-10-28 12:12:00 +0100197 &i2c_base->ic_cmd_data);
Chin Liang Seea0c26262014-02-04 11:56:23 -0600198 }
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530199 return 0;
200}
201
Stefan Roese41de7662016-04-21 08:19:40 +0200202static int i2c_xfer_finish(struct i2c_regs *i2c_base)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530203{
204 ulong start_stop_det = get_timer(0);
205
206 while (1) {
Stefan Roeseef6073e2014-10-28 12:12:00 +0100207 if ((readl(&i2c_base->ic_raw_intr_stat) & IC_STOP_DET)) {
208 readl(&i2c_base->ic_clr_stop_det);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530209 break;
210 } else if (get_timer(start_stop_det) > I2C_STOPDET_TO) {
211 break;
212 }
213 }
214
Stefan Roese41de7662016-04-21 08:19:40 +0200215 if (i2c_wait_for_bb(i2c_base)) {
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530216 printf("Timed out waiting for bus\n");
217 return 1;
218 }
219
Stefan Roese41de7662016-04-21 08:19:40 +0200220 i2c_flush_rxfifo(i2c_base);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530221
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530222 return 0;
223}
224
225/*
226 * i2c_read - Read from i2c memory
227 * @chip: target i2c address
228 * @addr: address to read from
229 * @alen:
230 * @buffer: buffer for read data
231 * @len: no of bytes to be read
232 *
233 * Read from i2c memory.
234 */
Stefan Roese41de7662016-04-21 08:19:40 +0200235static int __dw_i2c_read(struct i2c_regs *i2c_base, u8 dev, uint addr,
236 int alen, u8 *buffer, int len)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530237{
238 unsigned long start_time_rx;
Marek Vasutc4bc9a82016-10-20 16:48:28 +0200239 unsigned int active = 0;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530240
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400241#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
242 /*
243 * EEPROM chips that implement "address overflow" are ones
244 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
245 * address and the extra bits end up in the "chip address"
246 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
247 * four 256 byte chips.
248 *
249 * Note that we consider the length of the address field to
250 * still be one byte because the extra address bits are
251 * hidden in the chip address.
252 */
Stefan Roeseef6073e2014-10-28 12:12:00 +0100253 dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400254 addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
255
Stefan Roeseef6073e2014-10-28 12:12:00 +0100256 debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400257 addr);
258#endif
259
Stefan Roese41de7662016-04-21 08:19:40 +0200260 if (i2c_xfer_init(i2c_base, dev, addr, alen))
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530261 return 1;
262
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530263 start_time_rx = get_timer(0);
264 while (len) {
Marek Vasutc4bc9a82016-10-20 16:48:28 +0200265 if (!active) {
266 /*
267 * Avoid writing to ic_cmd_data multiple times
268 * in case this loop spins too quickly and the
269 * ic_status RFNE bit isn't set after the first
270 * write. Subsequent writes to ic_cmd_data can
271 * trigger spurious i2c transfer.
272 */
273 if (len == 1)
274 writel(IC_CMD | IC_STOP, &i2c_base->ic_cmd_data);
275 else
276 writel(IC_CMD, &i2c_base->ic_cmd_data);
277 active = 1;
278 }
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530279
Stefan Roeseef6073e2014-10-28 12:12:00 +0100280 if (readl(&i2c_base->ic_status) & IC_STATUS_RFNE) {
281 *buffer++ = (uchar)readl(&i2c_base->ic_cmd_data);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530282 len--;
283 start_time_rx = get_timer(0);
Marek Vasutc4bc9a82016-10-20 16:48:28 +0200284 active = 0;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530285 } else if (get_timer(start_time_rx) > I2C_BYTE_TO) {
Marek Vasutc4bc9a82016-10-20 16:48:28 +0200286 return 1;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530287 }
288 }
289
Stefan Roese41de7662016-04-21 08:19:40 +0200290 return i2c_xfer_finish(i2c_base);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530291}
292
293/*
294 * i2c_write - Write to i2c memory
295 * @chip: target i2c address
296 * @addr: address to read from
297 * @alen:
298 * @buffer: buffer for read data
299 * @len: no of bytes to be read
300 *
301 * Write to i2c memory.
302 */
Stefan Roese41de7662016-04-21 08:19:40 +0200303static int __dw_i2c_write(struct i2c_regs *i2c_base, u8 dev, uint addr,
304 int alen, u8 *buffer, int len)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530305{
306 int nb = len;
307 unsigned long start_time_tx;
308
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400309#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
310 /*
311 * EEPROM chips that implement "address overflow" are ones
312 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
313 * address and the extra bits end up in the "chip address"
314 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
315 * four 256 byte chips.
316 *
317 * Note that we consider the length of the address field to
318 * still be one byte because the extra address bits are
319 * hidden in the chip address.
320 */
Stefan Roeseef6073e2014-10-28 12:12:00 +0100321 dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400322 addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
323
Stefan Roeseef6073e2014-10-28 12:12:00 +0100324 debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400325 addr);
326#endif
327
Stefan Roese41de7662016-04-21 08:19:40 +0200328 if (i2c_xfer_init(i2c_base, dev, addr, alen))
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530329 return 1;
330
331 start_time_tx = get_timer(0);
332 while (len) {
Stefan Roeseef6073e2014-10-28 12:12:00 +0100333 if (readl(&i2c_base->ic_status) & IC_STATUS_TFNF) {
334 if (--len == 0) {
335 writel(*buffer | IC_STOP,
336 &i2c_base->ic_cmd_data);
337 } else {
338 writel(*buffer, &i2c_base->ic_cmd_data);
339 }
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530340 buffer++;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530341 start_time_tx = get_timer(0);
342
343 } else if (get_timer(start_time_tx) > (nb * I2C_BYTE_TO)) {
344 printf("Timed out. i2c write Failed\n");
345 return 1;
346 }
347 }
348
Stefan Roese41de7662016-04-21 08:19:40 +0200349 return i2c_xfer_finish(i2c_base);
350}
351
Stefan Roese3cb27962016-04-21 08:19:41 +0200352/*
353 * __dw_i2c_init - Init function
354 * @speed: required i2c speed
355 * @slaveaddr: slave address for the device
356 *
357 * Initialization function.
358 */
Simon Glassbd9ca8d2019-02-16 20:24:39 -0700359static int __dw_i2c_init(struct i2c_regs *i2c_base, int speed, int slaveaddr)
Stefan Roese3cb27962016-04-21 08:19:41 +0200360{
Simon Glassbd9ca8d2019-02-16 20:24:39 -0700361 int ret;
362
Stefan Roese3cb27962016-04-21 08:19:41 +0200363 /* Disable i2c */
Simon Glassbd9ca8d2019-02-16 20:24:39 -0700364 ret = dw_i2c_enable(i2c_base, false);
365 if (ret)
366 return ret;
Stefan Roese3cb27962016-04-21 08:19:41 +0200367
Marek Vasut808aa132017-08-07 20:45:31 +0200368 writel(IC_CON_SD | IC_CON_RE | IC_CON_SPD_FS | IC_CON_MM,
369 &i2c_base->ic_con);
Stefan Roese3cb27962016-04-21 08:19:41 +0200370 writel(IC_RX_TL, &i2c_base->ic_rx_tl);
371 writel(IC_TX_TL, &i2c_base->ic_tx_tl);
372 writel(IC_STOP_DET, &i2c_base->ic_intr_mask);
373#ifndef CONFIG_DM_I2C
Ley Foon Tan6e85c812019-06-12 09:48:04 +0800374 __dw_i2c_set_bus_speed(i2c_base, NULL, speed, IC_CLK);
Stefan Roese3cb27962016-04-21 08:19:41 +0200375 writel(slaveaddr, &i2c_base->ic_sar);
376#endif
377
378 /* Enable i2c */
Simon Glassbd9ca8d2019-02-16 20:24:39 -0700379 ret = dw_i2c_enable(i2c_base, true);
380 if (ret)
381 return ret;
382
383 return 0;
Stefan Roese3cb27962016-04-21 08:19:41 +0200384}
385
386#ifndef CONFIG_DM_I2C
387/*
388 * The legacy I2C functions. These need to get removed once
389 * all users of this driver are converted to DM.
390 */
Stefan Roese41de7662016-04-21 08:19:40 +0200391static struct i2c_regs *i2c_get_base(struct i2c_adapter *adap)
392{
393 switch (adap->hwadapnr) {
394#if CONFIG_SYS_I2C_BUS_MAX >= 4
395 case 3:
396 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE3;
397#endif
398#if CONFIG_SYS_I2C_BUS_MAX >= 3
399 case 2:
400 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE2;
401#endif
402#if CONFIG_SYS_I2C_BUS_MAX >= 2
403 case 1:
404 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE1;
405#endif
406 case 0:
407 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE;
408 default:
409 printf("Wrong I2C-adapter number %d\n", adap->hwadapnr);
410 }
411
412 return NULL;
413}
414
415static unsigned int dw_i2c_set_bus_speed(struct i2c_adapter *adap,
416 unsigned int speed)
417{
418 adap->speed = speed;
Ley Foon Tan6e85c812019-06-12 09:48:04 +0800419 return __dw_i2c_set_bus_speed(i2c_get_base(adap), NULL, speed, IC_CLK);
Stefan Roese41de7662016-04-21 08:19:40 +0200420}
421
Stefan Roese3cb27962016-04-21 08:19:41 +0200422static void dw_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
Stefan Roese41de7662016-04-21 08:19:40 +0200423{
Stefan Roese3cb27962016-04-21 08:19:41 +0200424 __dw_i2c_init(i2c_get_base(adap), speed, slaveaddr);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530425}
426
Stefan Roese41de7662016-04-21 08:19:40 +0200427static int dw_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr,
428 int alen, u8 *buffer, int len)
429{
430 return __dw_i2c_read(i2c_get_base(adap), dev, addr, alen, buffer, len);
431}
432
433static int dw_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr,
434 int alen, u8 *buffer, int len)
435{
436 return __dw_i2c_write(i2c_get_base(adap), dev, addr, alen, buffer, len);
437}
438
Stefan Roese3cb27962016-04-21 08:19:41 +0200439/* dw_i2c_probe - Probe the i2c chip */
Stefan Roeseef6073e2014-10-28 12:12:00 +0100440static int dw_i2c_probe(struct i2c_adapter *adap, u8 dev)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530441{
Stefan Roese41de7662016-04-21 08:19:40 +0200442 struct i2c_regs *i2c_base = i2c_get_base(adap);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530443 u32 tmp;
Stefan Roesef6322ebd2012-01-20 11:52:33 +0100444 int ret;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530445
446 /*
447 * Try to read the first location of the chip.
448 */
Stefan Roese41de7662016-04-21 08:19:40 +0200449 ret = __dw_i2c_read(i2c_base, dev, 0, 1, (uchar *)&tmp, 1);
Stefan Roesef6322ebd2012-01-20 11:52:33 +0100450 if (ret)
Stefan Roeseef6073e2014-10-28 12:12:00 +0100451 dw_i2c_init(adap, adap->speed, adap->slaveaddr);
Stefan Roesef6322ebd2012-01-20 11:52:33 +0100452
453 return ret;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530454}
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000455
Stefan Roeseef6073e2014-10-28 12:12:00 +0100456U_BOOT_I2C_ADAP_COMPLETE(dw_0, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
457 dw_i2c_write, dw_i2c_set_bus_speed,
458 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0)
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000459
Stefan Roeseef6073e2014-10-28 12:12:00 +0100460#if CONFIG_SYS_I2C_BUS_MAX >= 2
461U_BOOT_I2C_ADAP_COMPLETE(dw_1, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
462 dw_i2c_write, dw_i2c_set_bus_speed,
463 CONFIG_SYS_I2C_SPEED1, CONFIG_SYS_I2C_SLAVE1, 1)
464#endif
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000465
Stefan Roeseef6073e2014-10-28 12:12:00 +0100466#if CONFIG_SYS_I2C_BUS_MAX >= 3
467U_BOOT_I2C_ADAP_COMPLETE(dw_2, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
468 dw_i2c_write, dw_i2c_set_bus_speed,
469 CONFIG_SYS_I2C_SPEED2, CONFIG_SYS_I2C_SLAVE2, 2)
470#endif
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000471
Stefan Roeseef6073e2014-10-28 12:12:00 +0100472#if CONFIG_SYS_I2C_BUS_MAX >= 4
473U_BOOT_I2C_ADAP_COMPLETE(dw_3, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
474 dw_i2c_write, dw_i2c_set_bus_speed,
475 CONFIG_SYS_I2C_SPEED3, CONFIG_SYS_I2C_SLAVE3, 3)
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000476#endif
Stefan Roese3cb27962016-04-21 08:19:41 +0200477
478#else /* CONFIG_DM_I2C */
479/* The DM I2C functions */
480
481static int designware_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
482 int nmsgs)
483{
484 struct dw_i2c *i2c = dev_get_priv(bus);
485 int ret;
486
487 debug("i2c_xfer: %d messages\n", nmsgs);
488 for (; nmsgs > 0; nmsgs--, msg++) {
489 debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
490 if (msg->flags & I2C_M_RD) {
491 ret = __dw_i2c_read(i2c->regs, msg->addr, 0, 0,
492 msg->buf, msg->len);
493 } else {
494 ret = __dw_i2c_write(i2c->regs, msg->addr, 0, 0,
495 msg->buf, msg->len);
496 }
497 if (ret) {
498 debug("i2c_write: error sending\n");
499 return -EREMOTEIO;
500 }
501 }
502
503 return 0;
504}
505
506static int designware_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
507{
508 struct dw_i2c *i2c = dev_get_priv(bus);
Ley Foon Tan6e85c812019-06-12 09:48:04 +0800509 ulong rate;
510
511#if CONFIG_IS_ENABLED(CLK)
512 rate = clk_get_rate(&i2c->clk);
513 if (IS_ERR_VALUE(rate))
514 return -EINVAL;
Ley Foon Tan6e85c812019-06-12 09:48:04 +0800515#else
516 rate = IC_CLK;
517#endif
518 return __dw_i2c_set_bus_speed(i2c->regs, i2c->scl_sda_cfg, speed,
519 rate);
Stefan Roese3cb27962016-04-21 08:19:41 +0200520}
521
522static int designware_i2c_probe_chip(struct udevice *bus, uint chip_addr,
523 uint chip_flags)
524{
525 struct dw_i2c *i2c = dev_get_priv(bus);
526 struct i2c_regs *i2c_base = i2c->regs;
527 u32 tmp;
528 int ret;
529
530 /* Try to read the first location of the chip */
531 ret = __dw_i2c_read(i2c_base, chip_addr, 0, 1, (uchar *)&tmp, 1);
532 if (ret)
533 __dw_i2c_init(i2c_base, 0, 0);
534
535 return ret;
536}
537
Simon Glasse2be5532019-12-06 21:41:40 -0700538static int designware_i2c_ofdata_to_platdata(struct udevice *bus)
Stefan Roese3cb27962016-04-21 08:19:41 +0200539{
540 struct dw_i2c *priv = dev_get_priv(bus);
541
Simon Glasse2be5532019-12-06 21:41:40 -0700542 priv->regs = (struct i2c_regs *)devfdt_get_addr_ptr(bus);
543
544 return 0;
545}
546
547int designware_i2c_probe(struct udevice *bus)
548{
549 struct dw_i2c *priv = dev_get_priv(bus);
550 int ret;
Stefan Roese3cb27962016-04-21 08:19:41 +0200551
Simon Goldschmidt28608a12019-03-28 21:11:48 +0100552 ret = reset_get_bulk(bus, &priv->resets);
Dinh Nguyen08794aa2018-04-04 17:18:24 -0500553 if (ret)
Simon Goldschmidt28608a12019-03-28 21:11:48 +0100554 dev_warn(bus, "Can't get reset: %d\n", ret);
555 else
556 reset_deassert_bulk(&priv->resets);
Dinh Nguyen08794aa2018-04-04 17:18:24 -0500557
Ley Foon Tan6e85c812019-06-12 09:48:04 +0800558#if CONFIG_IS_ENABLED(CLK)
559 ret = clk_get_by_index(bus, 0, &priv->clk);
560 if (ret)
561 return ret;
562
563 ret = clk_enable(&priv->clk);
564 if (ret && ret != -ENOSYS && ret != -ENOTSUPP) {
565 clk_free(&priv->clk);
566 dev_err(bus, "failed to enable clock\n");
567 return ret;
568 }
569#endif
570
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 Glasse2be5532019-12-06 21:41:40 -0700574int designware_i2c_remove(struct udevice *dev)
Simon Goldschmidt28608a12019-03-28 21:11:48 +0100575{
576 struct dw_i2c *priv = dev_get_priv(dev);
577
Ley Foon Tan6e85c812019-06-12 09:48:04 +0800578#if CONFIG_IS_ENABLED(CLK)
579 clk_disable(&priv->clk);
580 clk_free(&priv->clk);
581#endif
582
Simon Goldschmidt28608a12019-03-28 21:11:48 +0100583 return reset_release_bulk(&priv->resets);
584}
585
Simon Glasse2be5532019-12-06 21:41:40 -0700586const struct dm_i2c_ops designware_i2c_ops = {
Stefan Roese3cb27962016-04-21 08:19:41 +0200587 .xfer = designware_i2c_xfer,
588 .probe_chip = designware_i2c_probe_chip,
589 .set_bus_speed = designware_i2c_set_bus_speed,
590};
591
592static const struct udevice_id designware_i2c_ids[] = {
593 { .compatible = "snps,designware-i2c" },
594 { }
595};
596
597U_BOOT_DRIVER(i2c_designware) = {
598 .name = "i2c_designware",
599 .id = UCLASS_I2C,
600 .of_match = designware_i2c_ids,
Simon Glasse2be5532019-12-06 21:41:40 -0700601 .ofdata_to_platdata = designware_i2c_ofdata_to_platdata,
Stefan Roese3cb27962016-04-21 08:19:41 +0200602 .probe = designware_i2c_probe,
603 .priv_auto_alloc_size = sizeof(struct dw_i2c),
Simon Goldschmidt28608a12019-03-28 21:11:48 +0100604 .remove = designware_i2c_remove,
Simon Glasse2be5532019-12-06 21:41:40 -0700605 .flags = DM_FLAG_OS_PREPARE,
Stefan Roese3cb27962016-04-21 08:19:41 +0200606 .ops = &designware_i2c_ops,
607};
608
609#endif /* CONFIG_DM_I2C */