blob: 6daa90e7442e9da916ca50691df250fd72187bcf [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 Roese38481202016-04-21 08:19:42 +020016struct dw_scl_sda_cfg {
17 u32 ss_hcnt;
18 u32 fs_hcnt;
19 u32 ss_lcnt;
20 u32 fs_lcnt;
21 u32 sda_hold;
22};
23
24#ifdef CONFIG_X86
25/* BayTrail HCNT/LCNT/SDA hold time */
26static struct dw_scl_sda_cfg byt_config = {
27 .ss_hcnt = 0x200,
28 .fs_hcnt = 0x55,
29 .ss_lcnt = 0x200,
30 .fs_lcnt = 0x99,
31 .sda_hold = 0x6,
32};
33#endif
34
Stefan Roese3cb27962016-04-21 08:19:41 +020035struct dw_i2c {
36 struct i2c_regs *regs;
Stefan Roese38481202016-04-21 08:19:42 +020037 struct dw_scl_sda_cfg *scl_sda_cfg;
Simon Goldschmidt28608a12019-03-28 21:11:48 +010038 struct reset_ctl_bulk resets;
Ley Foon Tan6e85c812019-06-12 09:48:04 +080039#if CONFIG_IS_ENABLED(CLK)
40 struct clk clk;
41#endif
Stefan Roese3cb27962016-04-21 08:19:41 +020042};
43
Stefan Roeseabb3e132016-04-27 09:02:12 +020044#ifdef CONFIG_SYS_I2C_DW_ENABLE_STATUS_UNSUPPORTED
Simon Glassbd9ca8d2019-02-16 20:24:39 -070045static int dw_i2c_enable(struct i2c_regs *i2c_base, bool enable)
Stefan Roese3bc33ba2016-04-21 08:19:38 +020046{
47 u32 ena = enable ? IC_ENABLE_0B : 0;
Stefan Roeseabb3e132016-04-27 09:02:12 +020048
49 writel(ena, &i2c_base->ic_enable);
Simon Glassbd9ca8d2019-02-16 20:24:39 -070050
51 return 0;
Stefan Roeseabb3e132016-04-27 09:02:12 +020052}
53#else
Simon Glassbd9ca8d2019-02-16 20:24:39 -070054static int dw_i2c_enable(struct i2c_regs *i2c_base, bool enable)
Stefan Roeseabb3e132016-04-27 09:02:12 +020055{
56 u32 ena = enable ? IC_ENABLE_0B : 0;
Stefan Roese3bc33ba2016-04-21 08:19:38 +020057 int timeout = 100;
58
59 do {
60 writel(ena, &i2c_base->ic_enable);
61 if ((readl(&i2c_base->ic_enable_status) & IC_ENABLE_0B) == ena)
Simon Glassbd9ca8d2019-02-16 20:24:39 -070062 return 0;
Stefan Roese3bc33ba2016-04-21 08:19:38 +020063
64 /*
65 * Wait 10 times the signaling period of the highest I2C
66 * transfer supported by the driver (for 400KHz this is
67 * 25us) as described in the DesignWare I2C databook.
68 */
69 udelay(25);
70 } while (timeout--);
Stefan Roese3bc33ba2016-04-21 08:19:38 +020071 printf("timeout in %sabling I2C adapter\n", enable ? "en" : "dis");
Simon Glassbd9ca8d2019-02-16 20:24:39 -070072
73 return -ETIMEDOUT;
Stefan Roese3bc33ba2016-04-21 08:19:38 +020074}
Stefan Roeseabb3e132016-04-27 09:02:12 +020075#endif
Stefan Roese3bc33ba2016-04-21 08:19:38 +020076
Vipin KUMARfc9589f2010-01-15 19:15:44 +053077/*
Stefan Roese88893c92016-04-21 08:19:39 +020078 * i2c_set_bus_speed - Set the i2c speed
79 * @speed: required i2c speed
Vipin KUMARfc9589f2010-01-15 19:15:44 +053080 *
Stefan Roese88893c92016-04-21 08:19:39 +020081 * Set the i2c speed.
Vipin KUMARfc9589f2010-01-15 19:15:44 +053082 */
Stefan Roese41de7662016-04-21 08:19:40 +020083static unsigned int __dw_i2c_set_bus_speed(struct i2c_regs *i2c_base,
Stefan Roese38481202016-04-21 08:19:42 +020084 struct dw_scl_sda_cfg *scl_sda_cfg,
Ley Foon Tan6e85c812019-06-12 09:48:04 +080085 unsigned int speed,
86 unsigned int bus_mhz)
Vipin KUMARfc9589f2010-01-15 19:15:44 +053087{
88 unsigned int cntl;
89 unsigned int hcnt, lcnt;
Jun Chend003a372019-06-05 15:23:16 +080090 unsigned int ena;
Stefan Roese88893c92016-04-21 08:19:39 +020091 int i2c_spd;
92
93 if (speed >= I2C_MAX_SPEED)
94 i2c_spd = IC_SPEED_MODE_MAX;
95 else if (speed >= I2C_FAST_SPEED)
96 i2c_spd = IC_SPEED_MODE_FAST;
97 else
98 i2c_spd = IC_SPEED_MODE_STANDARD;
Armando Visconti631e6932012-03-29 20:10:17 +000099
Jun Chend003a372019-06-05 15:23:16 +0800100 /* Get enable setting for restore later */
101 ena = readl(&i2c_base->ic_enable) & IC_ENABLE_0B;
102
Armando Visconti631e6932012-03-29 20:10:17 +0000103 /* to set speed cltr must be disabled */
Stefan Roese3bc33ba2016-04-21 08:19:38 +0200104 dw_i2c_enable(i2c_base, false);
Armando Visconti631e6932012-03-29 20:10:17 +0000105
Stefan Roeseef6073e2014-10-28 12:12:00 +0100106 cntl = (readl(&i2c_base->ic_con) & (~IC_CON_SPD_MSK));
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530107
108 switch (i2c_spd) {
Stefan Roese38481202016-04-21 08:19:42 +0200109#ifndef CONFIG_X86 /* No High-speed for BayTrail yet */
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530110 case IC_SPEED_MODE_MAX:
Stefan Roese38481202016-04-21 08:19:42 +0200111 cntl |= IC_CON_SPD_SS;
112 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_HS_SCL_HIGHTIME) / NANO_TO_MICRO;
117 lcnt = (bus_mhz * MIN_HS_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_hs_scl_hcnt);
Stefan Roeseef6073e2014-10-28 12:12:00 +0100120 writel(lcnt, &i2c_base->ic_hs_scl_lcnt);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530121 break;
Stefan Roese38481202016-04-21 08:19:42 +0200122#endif
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530123
124 case IC_SPEED_MODE_STANDARD:
125 cntl |= IC_CON_SPD_SS;
Stefan Roese38481202016-04-21 08:19:42 +0200126 if (scl_sda_cfg) {
127 hcnt = scl_sda_cfg->ss_hcnt;
128 lcnt = scl_sda_cfg->ss_lcnt;
129 } else {
Ley Foon Tan6e85c812019-06-12 09:48:04 +0800130 hcnt = (bus_mhz * MIN_SS_SCL_HIGHTIME) / NANO_TO_MICRO;
131 lcnt = (bus_mhz * MIN_SS_SCL_LOWTIME) / NANO_TO_MICRO;
Stefan Roese38481202016-04-21 08:19:42 +0200132 }
Stefan Roeseef6073e2014-10-28 12:12:00 +0100133 writel(hcnt, &i2c_base->ic_ss_scl_hcnt);
Stefan Roeseef6073e2014-10-28 12:12:00 +0100134 writel(lcnt, &i2c_base->ic_ss_scl_lcnt);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530135 break;
136
137 case IC_SPEED_MODE_FAST:
138 default:
139 cntl |= IC_CON_SPD_FS;
Stefan Roese38481202016-04-21 08:19:42 +0200140 if (scl_sda_cfg) {
141 hcnt = scl_sda_cfg->fs_hcnt;
142 lcnt = scl_sda_cfg->fs_lcnt;
143 } else {
Ley Foon Tan6e85c812019-06-12 09:48:04 +0800144 hcnt = (bus_mhz * MIN_FS_SCL_HIGHTIME) / NANO_TO_MICRO;
145 lcnt = (bus_mhz * MIN_FS_SCL_LOWTIME) / NANO_TO_MICRO;
Stefan Roese38481202016-04-21 08:19:42 +0200146 }
Stefan Roeseef6073e2014-10-28 12:12:00 +0100147 writel(hcnt, &i2c_base->ic_fs_scl_hcnt);
Stefan Roeseef6073e2014-10-28 12:12:00 +0100148 writel(lcnt, &i2c_base->ic_fs_scl_lcnt);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530149 break;
150 }
151
Stefan Roeseef6073e2014-10-28 12:12:00 +0100152 writel(cntl, &i2c_base->ic_con);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530153
Stefan Roese38481202016-04-21 08:19:42 +0200154 /* Configure SDA Hold Time if required */
155 if (scl_sda_cfg)
156 writel(scl_sda_cfg->sda_hold, &i2c_base->ic_sda_hold);
157
Jun Chend003a372019-06-05 15:23:16 +0800158 /* Restore back i2c now speed set */
159 if (ena == IC_ENABLE_0B)
160 dw_i2c_enable(i2c_base, true);
Stefan Roesef6322ebd2012-01-20 11:52:33 +0100161
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530162 return 0;
163}
164
165/*
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530166 * i2c_setaddress - Sets the target slave address
167 * @i2c_addr: target i2c address
168 *
169 * Sets the target slave address.
170 */
Stefan Roese41de7662016-04-21 08:19:40 +0200171static void i2c_setaddress(struct i2c_regs *i2c_base, unsigned int i2c_addr)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530172{
Alexey Brodkin41c56552013-11-07 17:52:18 +0400173 /* Disable i2c */
Stefan Roese3bc33ba2016-04-21 08:19:38 +0200174 dw_i2c_enable(i2c_base, false);
Alexey Brodkin41c56552013-11-07 17:52:18 +0400175
Stefan Roeseef6073e2014-10-28 12:12:00 +0100176 writel(i2c_addr, &i2c_base->ic_tar);
Alexey Brodkin41c56552013-11-07 17:52:18 +0400177
178 /* Enable i2c */
Stefan Roese3bc33ba2016-04-21 08:19:38 +0200179 dw_i2c_enable(i2c_base, true);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530180}
181
182/*
183 * i2c_flush_rxfifo - Flushes the i2c RX FIFO
184 *
185 * Flushes the i2c RX FIFO
186 */
Stefan Roese41de7662016-04-21 08:19:40 +0200187static void i2c_flush_rxfifo(struct i2c_regs *i2c_base)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530188{
Stefan Roeseef6073e2014-10-28 12:12:00 +0100189 while (readl(&i2c_base->ic_status) & IC_STATUS_RFNE)
190 readl(&i2c_base->ic_cmd_data);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530191}
192
193/*
194 * i2c_wait_for_bb - Waits for bus busy
195 *
196 * Waits for bus busy
197 */
Stefan Roese41de7662016-04-21 08:19:40 +0200198static int i2c_wait_for_bb(struct i2c_regs *i2c_base)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530199{
200 unsigned long start_time_bb = get_timer(0);
201
Stefan Roeseef6073e2014-10-28 12:12:00 +0100202 while ((readl(&i2c_base->ic_status) & IC_STATUS_MA) ||
203 !(readl(&i2c_base->ic_status) & IC_STATUS_TFE)) {
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530204
205 /* Evaluate timeout */
206 if (get_timer(start_time_bb) > (unsigned long)(I2C_BYTE_TO_BB))
207 return 1;
208 }
209
210 return 0;
211}
212
Stefan Roese41de7662016-04-21 08:19:40 +0200213static int i2c_xfer_init(struct i2c_regs *i2c_base, uchar chip, uint addr,
Stefan Roeseef6073e2014-10-28 12:12:00 +0100214 int alen)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530215{
Stefan Roese41de7662016-04-21 08:19:40 +0200216 if (i2c_wait_for_bb(i2c_base))
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530217 return 1;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530218
Stefan Roese41de7662016-04-21 08:19:40 +0200219 i2c_setaddress(i2c_base, chip);
Chin Liang Seea0c26262014-02-04 11:56:23 -0600220 while (alen) {
221 alen--;
222 /* high byte address going out first */
223 writel((addr >> (alen * 8)) & 0xff,
Stefan Roeseef6073e2014-10-28 12:12:00 +0100224 &i2c_base->ic_cmd_data);
Chin Liang Seea0c26262014-02-04 11:56:23 -0600225 }
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530226 return 0;
227}
228
Stefan Roese41de7662016-04-21 08:19:40 +0200229static int i2c_xfer_finish(struct i2c_regs *i2c_base)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530230{
231 ulong start_stop_det = get_timer(0);
232
233 while (1) {
Stefan Roeseef6073e2014-10-28 12:12:00 +0100234 if ((readl(&i2c_base->ic_raw_intr_stat) & IC_STOP_DET)) {
235 readl(&i2c_base->ic_clr_stop_det);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530236 break;
237 } else if (get_timer(start_stop_det) > I2C_STOPDET_TO) {
238 break;
239 }
240 }
241
Stefan Roese41de7662016-04-21 08:19:40 +0200242 if (i2c_wait_for_bb(i2c_base)) {
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530243 printf("Timed out waiting for bus\n");
244 return 1;
245 }
246
Stefan Roese41de7662016-04-21 08:19:40 +0200247 i2c_flush_rxfifo(i2c_base);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530248
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530249 return 0;
250}
251
252/*
253 * i2c_read - Read from i2c memory
254 * @chip: target i2c address
255 * @addr: address to read from
256 * @alen:
257 * @buffer: buffer for read data
258 * @len: no of bytes to be read
259 *
260 * Read from i2c memory.
261 */
Stefan Roese41de7662016-04-21 08:19:40 +0200262static int __dw_i2c_read(struct i2c_regs *i2c_base, u8 dev, uint addr,
263 int alen, u8 *buffer, int len)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530264{
265 unsigned long start_time_rx;
Marek Vasutc4bc9a82016-10-20 16:48:28 +0200266 unsigned int active = 0;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530267
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400268#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
269 /*
270 * EEPROM chips that implement "address overflow" are ones
271 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
272 * address and the extra bits end up in the "chip address"
273 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
274 * four 256 byte chips.
275 *
276 * Note that we consider the length of the address field to
277 * still be one byte because the extra address bits are
278 * hidden in the chip address.
279 */
Stefan Roeseef6073e2014-10-28 12:12:00 +0100280 dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400281 addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
282
Stefan Roeseef6073e2014-10-28 12:12:00 +0100283 debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400284 addr);
285#endif
286
Stefan Roese41de7662016-04-21 08:19:40 +0200287 if (i2c_xfer_init(i2c_base, dev, addr, alen))
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530288 return 1;
289
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530290 start_time_rx = get_timer(0);
291 while (len) {
Marek Vasutc4bc9a82016-10-20 16:48:28 +0200292 if (!active) {
293 /*
294 * Avoid writing to ic_cmd_data multiple times
295 * in case this loop spins too quickly and the
296 * ic_status RFNE bit isn't set after the first
297 * write. Subsequent writes to ic_cmd_data can
298 * trigger spurious i2c transfer.
299 */
300 if (len == 1)
301 writel(IC_CMD | IC_STOP, &i2c_base->ic_cmd_data);
302 else
303 writel(IC_CMD, &i2c_base->ic_cmd_data);
304 active = 1;
305 }
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530306
Stefan Roeseef6073e2014-10-28 12:12:00 +0100307 if (readl(&i2c_base->ic_status) & IC_STATUS_RFNE) {
308 *buffer++ = (uchar)readl(&i2c_base->ic_cmd_data);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530309 len--;
310 start_time_rx = get_timer(0);
Marek Vasutc4bc9a82016-10-20 16:48:28 +0200311 active = 0;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530312 } else if (get_timer(start_time_rx) > I2C_BYTE_TO) {
Marek Vasutc4bc9a82016-10-20 16:48:28 +0200313 return 1;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530314 }
315 }
316
Stefan Roese41de7662016-04-21 08:19:40 +0200317 return i2c_xfer_finish(i2c_base);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530318}
319
320/*
321 * i2c_write - Write to i2c memory
322 * @chip: target i2c address
323 * @addr: address to read from
324 * @alen:
325 * @buffer: buffer for read data
326 * @len: no of bytes to be read
327 *
328 * Write to i2c memory.
329 */
Stefan Roese41de7662016-04-21 08:19:40 +0200330static int __dw_i2c_write(struct i2c_regs *i2c_base, u8 dev, uint addr,
331 int alen, u8 *buffer, int len)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530332{
333 int nb = len;
334 unsigned long start_time_tx;
335
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400336#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
337 /*
338 * EEPROM chips that implement "address overflow" are ones
339 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
340 * address and the extra bits end up in the "chip address"
341 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
342 * four 256 byte chips.
343 *
344 * Note that we consider the length of the address field to
345 * still be one byte because the extra address bits are
346 * hidden in the chip address.
347 */
Stefan Roeseef6073e2014-10-28 12:12:00 +0100348 dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400349 addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
350
Stefan Roeseef6073e2014-10-28 12:12:00 +0100351 debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400352 addr);
353#endif
354
Stefan Roese41de7662016-04-21 08:19:40 +0200355 if (i2c_xfer_init(i2c_base, dev, addr, alen))
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530356 return 1;
357
358 start_time_tx = get_timer(0);
359 while (len) {
Stefan Roeseef6073e2014-10-28 12:12:00 +0100360 if (readl(&i2c_base->ic_status) & IC_STATUS_TFNF) {
361 if (--len == 0) {
362 writel(*buffer | IC_STOP,
363 &i2c_base->ic_cmd_data);
364 } else {
365 writel(*buffer, &i2c_base->ic_cmd_data);
366 }
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530367 buffer++;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530368 start_time_tx = get_timer(0);
369
370 } else if (get_timer(start_time_tx) > (nb * I2C_BYTE_TO)) {
371 printf("Timed out. i2c write Failed\n");
372 return 1;
373 }
374 }
375
Stefan Roese41de7662016-04-21 08:19:40 +0200376 return i2c_xfer_finish(i2c_base);
377}
378
Stefan Roese3cb27962016-04-21 08:19:41 +0200379/*
380 * __dw_i2c_init - Init function
381 * @speed: required i2c speed
382 * @slaveaddr: slave address for the device
383 *
384 * Initialization function.
385 */
Simon Glassbd9ca8d2019-02-16 20:24:39 -0700386static int __dw_i2c_init(struct i2c_regs *i2c_base, int speed, int slaveaddr)
Stefan Roese3cb27962016-04-21 08:19:41 +0200387{
Simon Glassbd9ca8d2019-02-16 20:24:39 -0700388 int ret;
389
Stefan Roese3cb27962016-04-21 08:19:41 +0200390 /* Disable i2c */
Simon Glassbd9ca8d2019-02-16 20:24:39 -0700391 ret = dw_i2c_enable(i2c_base, false);
392 if (ret)
393 return ret;
Stefan Roese3cb27962016-04-21 08:19:41 +0200394
Marek Vasut808aa132017-08-07 20:45:31 +0200395 writel(IC_CON_SD | IC_CON_RE | IC_CON_SPD_FS | IC_CON_MM,
396 &i2c_base->ic_con);
Stefan Roese3cb27962016-04-21 08:19:41 +0200397 writel(IC_RX_TL, &i2c_base->ic_rx_tl);
398 writel(IC_TX_TL, &i2c_base->ic_tx_tl);
399 writel(IC_STOP_DET, &i2c_base->ic_intr_mask);
400#ifndef CONFIG_DM_I2C
Ley Foon Tan6e85c812019-06-12 09:48:04 +0800401 __dw_i2c_set_bus_speed(i2c_base, NULL, speed, IC_CLK);
Stefan Roese3cb27962016-04-21 08:19:41 +0200402 writel(slaveaddr, &i2c_base->ic_sar);
403#endif
404
405 /* Enable i2c */
Simon Glassbd9ca8d2019-02-16 20:24:39 -0700406 ret = dw_i2c_enable(i2c_base, true);
407 if (ret)
408 return ret;
409
410 return 0;
Stefan Roese3cb27962016-04-21 08:19:41 +0200411}
412
413#ifndef CONFIG_DM_I2C
414/*
415 * The legacy I2C functions. These need to get removed once
416 * all users of this driver are converted to DM.
417 */
Stefan Roese41de7662016-04-21 08:19:40 +0200418static struct i2c_regs *i2c_get_base(struct i2c_adapter *adap)
419{
420 switch (adap->hwadapnr) {
421#if CONFIG_SYS_I2C_BUS_MAX >= 4
422 case 3:
423 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE3;
424#endif
425#if CONFIG_SYS_I2C_BUS_MAX >= 3
426 case 2:
427 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE2;
428#endif
429#if CONFIG_SYS_I2C_BUS_MAX >= 2
430 case 1:
431 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE1;
432#endif
433 case 0:
434 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE;
435 default:
436 printf("Wrong I2C-adapter number %d\n", adap->hwadapnr);
437 }
438
439 return NULL;
440}
441
442static unsigned int dw_i2c_set_bus_speed(struct i2c_adapter *adap,
443 unsigned int speed)
444{
445 adap->speed = speed;
Ley Foon Tan6e85c812019-06-12 09:48:04 +0800446 return __dw_i2c_set_bus_speed(i2c_get_base(adap), NULL, speed, IC_CLK);
Stefan Roese41de7662016-04-21 08:19:40 +0200447}
448
Stefan Roese3cb27962016-04-21 08:19:41 +0200449static void dw_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
Stefan Roese41de7662016-04-21 08:19:40 +0200450{
Stefan Roese3cb27962016-04-21 08:19:41 +0200451 __dw_i2c_init(i2c_get_base(adap), speed, slaveaddr);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530452}
453
Stefan Roese41de7662016-04-21 08:19:40 +0200454static int dw_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr,
455 int alen, u8 *buffer, int len)
456{
457 return __dw_i2c_read(i2c_get_base(adap), dev, addr, alen, buffer, len);
458}
459
460static int dw_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr,
461 int alen, u8 *buffer, int len)
462{
463 return __dw_i2c_write(i2c_get_base(adap), dev, addr, alen, buffer, len);
464}
465
Stefan Roese3cb27962016-04-21 08:19:41 +0200466/* dw_i2c_probe - Probe the i2c chip */
Stefan Roeseef6073e2014-10-28 12:12:00 +0100467static int dw_i2c_probe(struct i2c_adapter *adap, u8 dev)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530468{
Stefan Roese41de7662016-04-21 08:19:40 +0200469 struct i2c_regs *i2c_base = i2c_get_base(adap);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530470 u32 tmp;
Stefan Roesef6322ebd2012-01-20 11:52:33 +0100471 int ret;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530472
473 /*
474 * Try to read the first location of the chip.
475 */
Stefan Roese41de7662016-04-21 08:19:40 +0200476 ret = __dw_i2c_read(i2c_base, dev, 0, 1, (uchar *)&tmp, 1);
Stefan Roesef6322ebd2012-01-20 11:52:33 +0100477 if (ret)
Stefan Roeseef6073e2014-10-28 12:12:00 +0100478 dw_i2c_init(adap, adap->speed, adap->slaveaddr);
Stefan Roesef6322ebd2012-01-20 11:52:33 +0100479
480 return ret;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530481}
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000482
Stefan Roeseef6073e2014-10-28 12:12:00 +0100483U_BOOT_I2C_ADAP_COMPLETE(dw_0, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
484 dw_i2c_write, dw_i2c_set_bus_speed,
485 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0)
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000486
Stefan Roeseef6073e2014-10-28 12:12:00 +0100487#if CONFIG_SYS_I2C_BUS_MAX >= 2
488U_BOOT_I2C_ADAP_COMPLETE(dw_1, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
489 dw_i2c_write, dw_i2c_set_bus_speed,
490 CONFIG_SYS_I2C_SPEED1, CONFIG_SYS_I2C_SLAVE1, 1)
491#endif
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000492
Stefan Roeseef6073e2014-10-28 12:12:00 +0100493#if CONFIG_SYS_I2C_BUS_MAX >= 3
494U_BOOT_I2C_ADAP_COMPLETE(dw_2, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
495 dw_i2c_write, dw_i2c_set_bus_speed,
496 CONFIG_SYS_I2C_SPEED2, CONFIG_SYS_I2C_SLAVE2, 2)
497#endif
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000498
Stefan Roeseef6073e2014-10-28 12:12:00 +0100499#if CONFIG_SYS_I2C_BUS_MAX >= 4
500U_BOOT_I2C_ADAP_COMPLETE(dw_3, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
501 dw_i2c_write, dw_i2c_set_bus_speed,
502 CONFIG_SYS_I2C_SPEED3, CONFIG_SYS_I2C_SLAVE3, 3)
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000503#endif
Stefan Roese3cb27962016-04-21 08:19:41 +0200504
505#else /* CONFIG_DM_I2C */
506/* The DM I2C functions */
507
508static int designware_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
509 int nmsgs)
510{
511 struct dw_i2c *i2c = dev_get_priv(bus);
512 int ret;
513
514 debug("i2c_xfer: %d messages\n", nmsgs);
515 for (; nmsgs > 0; nmsgs--, msg++) {
516 debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
517 if (msg->flags & I2C_M_RD) {
518 ret = __dw_i2c_read(i2c->regs, msg->addr, 0, 0,
519 msg->buf, msg->len);
520 } else {
521 ret = __dw_i2c_write(i2c->regs, msg->addr, 0, 0,
522 msg->buf, msg->len);
523 }
524 if (ret) {
525 debug("i2c_write: error sending\n");
526 return -EREMOTEIO;
527 }
528 }
529
530 return 0;
531}
532
533static int designware_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
534{
535 struct dw_i2c *i2c = dev_get_priv(bus);
Ley Foon Tan6e85c812019-06-12 09:48:04 +0800536 ulong rate;
537
538#if CONFIG_IS_ENABLED(CLK)
539 rate = clk_get_rate(&i2c->clk);
540 if (IS_ERR_VALUE(rate))
541 return -EINVAL;
Stefan Roese3cb27962016-04-21 08:19:41 +0200542
Ley Foon Tan6e85c812019-06-12 09:48:04 +0800543 /* Convert to MHz */
544 rate /= 1000000;
545#else
546 rate = IC_CLK;
547#endif
548 return __dw_i2c_set_bus_speed(i2c->regs, i2c->scl_sda_cfg, speed,
549 rate);
Stefan Roese3cb27962016-04-21 08:19:41 +0200550}
551
552static int designware_i2c_probe_chip(struct udevice *bus, uint chip_addr,
553 uint chip_flags)
554{
555 struct dw_i2c *i2c = dev_get_priv(bus);
556 struct i2c_regs *i2c_base = i2c->regs;
557 u32 tmp;
558 int ret;
559
560 /* Try to read the first location of the chip */
561 ret = __dw_i2c_read(i2c_base, chip_addr, 0, 1, (uchar *)&tmp, 1);
562 if (ret)
563 __dw_i2c_init(i2c_base, 0, 0);
564
565 return ret;
566}
567
568static int designware_i2c_probe(struct udevice *bus)
569{
570 struct dw_i2c *priv = dev_get_priv(bus);
Dinh Nguyen08794aa2018-04-04 17:18:24 -0500571 int ret;
Stefan Roese3cb27962016-04-21 08:19:41 +0200572
Stefan Roese38481202016-04-21 08:19:42 +0200573 if (device_is_on_pci_bus(bus)) {
574#ifdef CONFIG_DM_PCI
575 /* Save base address from PCI BAR */
576 priv->regs = (struct i2c_regs *)
577 dm_pci_map_bar(bus, PCI_BASE_ADDRESS_0, PCI_REGION_MEM);
578#ifdef CONFIG_X86
579 /* Use BayTrail specific timing values */
580 priv->scl_sda_cfg = &byt_config;
581#endif
582#endif
583 } else {
Simon Glassba1dea42017-05-17 17:18:05 -0600584 priv->regs = (struct i2c_regs *)devfdt_get_addr_ptr(bus);
Stefan Roese38481202016-04-21 08:19:42 +0200585 }
Stefan Roese3cb27962016-04-21 08:19:41 +0200586
Simon Goldschmidt28608a12019-03-28 21:11:48 +0100587 ret = reset_get_bulk(bus, &priv->resets);
Dinh Nguyen08794aa2018-04-04 17:18:24 -0500588 if (ret)
Simon Goldschmidt28608a12019-03-28 21:11:48 +0100589 dev_warn(bus, "Can't get reset: %d\n", ret);
590 else
591 reset_deassert_bulk(&priv->resets);
Dinh Nguyen08794aa2018-04-04 17:18:24 -0500592
Ley Foon Tan6e85c812019-06-12 09:48:04 +0800593#if CONFIG_IS_ENABLED(CLK)
594 ret = clk_get_by_index(bus, 0, &priv->clk);
595 if (ret)
596 return ret;
597
598 ret = clk_enable(&priv->clk);
599 if (ret && ret != -ENOSYS && ret != -ENOTSUPP) {
600 clk_free(&priv->clk);
601 dev_err(bus, "failed to enable clock\n");
602 return ret;
603 }
604#endif
605
Simon Glassbd9ca8d2019-02-16 20:24:39 -0700606 return __dw_i2c_init(priv->regs, 0, 0);
Stefan Roese3cb27962016-04-21 08:19:41 +0200607}
608
Simon Goldschmidt28608a12019-03-28 21:11:48 +0100609static int designware_i2c_remove(struct udevice *dev)
610{
611 struct dw_i2c *priv = dev_get_priv(dev);
612
Ley Foon Tan6e85c812019-06-12 09:48:04 +0800613#if CONFIG_IS_ENABLED(CLK)
614 clk_disable(&priv->clk);
615 clk_free(&priv->clk);
616#endif
617
Simon Goldschmidt28608a12019-03-28 21:11:48 +0100618 return reset_release_bulk(&priv->resets);
619}
620
Stefan Roese38481202016-04-21 08:19:42 +0200621static int designware_i2c_bind(struct udevice *dev)
622{
623 static int num_cards;
624 char name[20];
625
626 /* Create a unique device name for PCI type devices */
627 if (device_is_on_pci_bus(dev)) {
628 /*
629 * ToDo:
630 * Setting req_seq in the driver is probably not recommended.
631 * But without a DT alias the number is not configured. And
632 * using this driver is impossible for PCIe I2C devices.
633 * This can be removed, once a better (correct) way for this
634 * is found and implemented.
635 */
636 dev->req_seq = num_cards;
637 sprintf(name, "i2c_designware#%u", num_cards++);
638 device_set_name(dev, name);
639 }
640
641 return 0;
642}
643
Stefan Roese3cb27962016-04-21 08:19:41 +0200644static const struct dm_i2c_ops designware_i2c_ops = {
645 .xfer = designware_i2c_xfer,
646 .probe_chip = designware_i2c_probe_chip,
647 .set_bus_speed = designware_i2c_set_bus_speed,
648};
649
650static const struct udevice_id designware_i2c_ids[] = {
651 { .compatible = "snps,designware-i2c" },
652 { }
653};
654
655U_BOOT_DRIVER(i2c_designware) = {
656 .name = "i2c_designware",
657 .id = UCLASS_I2C,
658 .of_match = designware_i2c_ids,
Stefan Roese38481202016-04-21 08:19:42 +0200659 .bind = designware_i2c_bind,
Stefan Roese3cb27962016-04-21 08:19:41 +0200660 .probe = designware_i2c_probe,
661 .priv_auto_alloc_size = sizeof(struct dw_i2c),
Simon Goldschmidt28608a12019-03-28 21:11:48 +0100662 .remove = designware_i2c_remove,
663 .flags = DM_FLAG_OS_PREPARE,
Stefan Roese3cb27962016-04-21 08:19:41 +0200664 .ops = &designware_i2c_ops,
665};
666
Stefan Roese38481202016-04-21 08:19:42 +0200667#ifdef CONFIG_X86
668static struct pci_device_id designware_pci_supported[] = {
669 /* Intel BayTrail has 7 I2C controller located on the PCI bus */
670 { PCI_VDEVICE(INTEL, 0x0f41) },
671 { PCI_VDEVICE(INTEL, 0x0f42) },
672 { PCI_VDEVICE(INTEL, 0x0f43) },
673 { PCI_VDEVICE(INTEL, 0x0f44) },
674 { PCI_VDEVICE(INTEL, 0x0f45) },
675 { PCI_VDEVICE(INTEL, 0x0f46) },
676 { PCI_VDEVICE(INTEL, 0x0f47) },
677 {},
678};
679
680U_BOOT_PCI_DEVICE(i2c_designware, designware_pci_supported);
681#endif
682
Stefan Roese3cb27962016-04-21 08:19:41 +0200683#endif /* CONFIG_DM_I2C */