blob: 29cf63375c7f173e71c8265f69ddd041c8fbc4f2 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Vipin KUMAR2403f8f2010-01-15 19:15:44 +05302/*
3 * (C) Copyright 2009
Patrick Delaunayeae488b2022-05-20 18:38:10 +02004 * Vipin Kumar, STMicroelectronics, vipin.kumar@st.com.
Vipin KUMAR2403f8f2010-01-15 19:15:44 +05305 */
6
7#include <common.h>
Simon Glassafb88652020-01-23 11:48:06 -07008#include <clk.h>
Stefan Roese334b9b02016-04-21 08:19:41 +02009#include <dm.h>
Stefan Roese678398b2014-10-28 12:12:00 +010010#include <i2c.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070012#include <malloc.h>
Stefan Roeseba5da552016-04-21 08:19:42 +020013#include <pci.h>
Dinh Nguyen622597d2018-04-04 17:18:24 -050014#include <reset.h>
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053015#include <asm/io.h>
Simon Glassc05ed002020-05-10 11:40:11 -060016#include <linux/delay.h>
Vipin KUMAR031ed2f2012-02-26 23:13:29 +000017#include "designware_i2c.h"
Simon Glass336d4612020-02-03 07:36:16 -070018#include <dm/device_compat.h>
Simon Glass61b29b82020-02-03 07:36:15 -070019#include <linux/err.h>
Vipin KUMAR2403f8f2010-01-15 19:15:44 +053020
Raul E Rangelf6f9a012020-04-22 10:13:54 -060021/*
22 * This assigned unique hex value is constant and is derived from the two ASCII
23 * letters 'DW' followed by a 16-bit unsigned number
24 */
25#define DW_I2C_COMP_TYPE 0x44570140
26
Heinrich Schuchardt35e80072023-10-13 15:09:39 +020027/*
28 * This constant is used to calculate when during the clock high phase the data
29 * bit shall be read. The value was copied from the Linux v6.5 function
30 * i2c_dw_scl_hcnt() which provides the following explanation:
31 *
32 * "This is just an experimental rule: the tHD;STA period turned out to be
33 * proportinal to (_HCNT + 3). With this setting, we could meet both tHIGH and
34 * tHD;STA timing specs."
35 */
36#define T_HD_STA_OFFSET 3
37
Simon Glass2b5d0292019-02-16 20:24:39 -070038static int dw_i2c_enable(struct i2c_regs *i2c_base, bool enable)
Stefan Roese1c8b0892016-04-21 08:19:38 +020039{
40 u32 ena = enable ? IC_ENABLE_0B : 0;
41 int timeout = 100;
42
43 do {
44 writel(ena, &i2c_base->ic_enable);
45 if ((readl(&i2c_base->ic_enable_status) & IC_ENABLE_0B) == ena)
Simon Glass2b5d0292019-02-16 20:24:39 -070046 return 0;
Stefan Roese1c8b0892016-04-21 08:19:38 +020047
48 /*
49 * Wait 10 times the signaling period of the highest I2C
50 * transfer supported by the driver (for 400KHz this is
51 * 25us) as described in the DesignWare I2C databook.
52 */
53 udelay(25);
54 } while (timeout--);
Stefan Roese1c8b0892016-04-21 08:19:38 +020055 printf("timeout in %sabling I2C adapter\n", enable ? "en" : "dis");
Simon Glass2b5d0292019-02-16 20:24:39 -070056
57 return -ETIMEDOUT;
Stefan Roese1c8b0892016-04-21 08:19:38 +020058}
59
Simon Glasse71b6f62020-01-23 11:48:14 -070060/* High and low times in different speed modes (in ns) */
61enum {
62 /* SDA Hold Time */
63 DEFAULT_SDA_HOLD_TIME = 300,
64};
65
66/**
67 * calc_counts() - Convert a period to a number of IC clk cycles
68 *
69 * @ic_clk: Input clock in Hz
70 * @period_ns: Period to represent, in ns
Heinrich Schuchardt185f8122022-01-19 18:05:50 +010071 * Return: calculated count
Simon Glasse71b6f62020-01-23 11:48:14 -070072 */
73static uint calc_counts(uint ic_clk, uint period_ns)
74{
75 return DIV_ROUND_UP(ic_clk / 1000 * period_ns, NANO_TO_KILO);
76}
77
78/**
79 * struct i2c_mode_info - Information about an I2C speed mode
80 *
81 * Each speed mode has its own characteristics. This struct holds these to aid
82 * calculations in dw_i2c_calc_timing().
83 *
84 * @speed: Speed in Hz
85 * @min_scl_lowtime_ns: Minimum value for SCL low period in ns
86 * @min_scl_hightime_ns: Minimum value for SCL high period in ns
87 * @def_rise_time_ns: Default rise time in ns
88 * @def_fall_time_ns: Default fall time in ns
89 */
90struct i2c_mode_info {
91 int speed;
92 int min_scl_hightime_ns;
93 int min_scl_lowtime_ns;
94 int def_rise_time_ns;
95 int def_fall_time_ns;
96};
97
98static const struct i2c_mode_info info_for_mode[] = {
99 [IC_SPEED_MODE_STANDARD] = {
Simon Glass54290c62020-01-23 11:48:18 -0700100 I2C_SPEED_STANDARD_RATE,
Simon Glasse71b6f62020-01-23 11:48:14 -0700101 MIN_SS_SCL_HIGHTIME,
102 MIN_SS_SCL_LOWTIME,
103 1000,
104 300,
105 },
106 [IC_SPEED_MODE_FAST] = {
Simon Glass54290c62020-01-23 11:48:18 -0700107 I2C_SPEED_FAST_RATE,
Simon Glasse71b6f62020-01-23 11:48:14 -0700108 MIN_FS_SCL_HIGHTIME,
109 MIN_FS_SCL_LOWTIME,
110 300,
111 300,
112 },
Simon Glassd96440d2020-01-23 11:48:23 -0700113 [IC_SPEED_MODE_FAST_PLUS] = {
114 I2C_SPEED_FAST_PLUS_RATE,
115 MIN_FP_SCL_HIGHTIME,
116 MIN_FP_SCL_LOWTIME,
117 260,
118 500,
119 },
Simon Glasse71b6f62020-01-23 11:48:14 -0700120 [IC_SPEED_MODE_HIGH] = {
Simon Glass54290c62020-01-23 11:48:18 -0700121 I2C_SPEED_HIGH_RATE,
Simon Glasse71b6f62020-01-23 11:48:14 -0700122 MIN_HS_SCL_HIGHTIME,
123 MIN_HS_SCL_LOWTIME,
124 120,
125 120,
126 },
127};
128
129/**
130 * dw_i2c_calc_timing() - Calculate the timings to use for a bus
131 *
132 * @priv: Bus private information (NULL if not using driver model)
133 * @mode: Speed mode to use
134 * @ic_clk: IC clock speed in Hz
135 * @spk_cnt: Spike-suppression count
136 * @config: Returns value to use
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100137 * Return: 0 if OK, -EINVAL if the calculation failed due to invalid data
Simon Glasse71b6f62020-01-23 11:48:14 -0700138 */
139static int dw_i2c_calc_timing(struct dw_i2c *priv, enum i2c_speed_mode mode,
140 int ic_clk, int spk_cnt,
141 struct dw_i2c_speed_config *config)
142{
143 int fall_cnt, rise_cnt, min_tlow_cnt, min_thigh_cnt;
144 int hcnt, lcnt, period_cnt, diff, tot;
145 int sda_hold_time_ns, scl_rise_time_ns, scl_fall_time_ns;
146 const struct i2c_mode_info *info;
147
148 /*
149 * Find the period, rise, fall, min tlow, and min thigh in terms of
150 * counts of the IC clock
151 */
152 info = &info_for_mode[mode];
153 period_cnt = ic_clk / info->speed;
154 scl_rise_time_ns = priv && priv->scl_rise_time_ns ?
155 priv->scl_rise_time_ns : info->def_rise_time_ns;
156 scl_fall_time_ns = priv && priv->scl_fall_time_ns ?
157 priv->scl_fall_time_ns : info->def_fall_time_ns;
158 rise_cnt = calc_counts(ic_clk, scl_rise_time_ns);
159 fall_cnt = calc_counts(ic_clk, scl_fall_time_ns);
160 min_tlow_cnt = calc_counts(ic_clk, info->min_scl_lowtime_ns);
161 min_thigh_cnt = calc_counts(ic_clk, info->min_scl_hightime_ns);
162
Simon Glass767abfc2020-07-07 21:32:27 -0600163 debug("dw_i2c: mode %d, ic_clk %d, speed %d, period %d rise %d fall %d tlow %d thigh %d spk %d\n",
164 mode, ic_clk, info->speed, period_cnt, rise_cnt, fall_cnt,
165 min_tlow_cnt, min_thigh_cnt, spk_cnt);
Simon Glasse71b6f62020-01-23 11:48:14 -0700166
167 /*
168 * Back-solve for hcnt and lcnt according to the following equations:
Heinrich Schuchardt35e80072023-10-13 15:09:39 +0200169 * SCL_High_time = [(HCNT + IC_*_SPKLEN + T_HD_STA_OFFSET) * ic_clk] + SCL_Fall_time
Simon Glasse71b6f62020-01-23 11:48:14 -0700170 * SCL_Low_time = [(LCNT + 1) * ic_clk] - SCL_Fall_time + SCL_Rise_time
171 */
Heinrich Schuchardt35e80072023-10-13 15:09:39 +0200172 hcnt = min_thigh_cnt - fall_cnt - T_HD_STA_OFFSET - spk_cnt;
Simon Glasse71b6f62020-01-23 11:48:14 -0700173 lcnt = min_tlow_cnt - rise_cnt + fall_cnt - 1;
174
175 if (hcnt < 0 || lcnt < 0) {
176 debug("dw_i2c: bad counts. hcnt = %d lcnt = %d\n", hcnt, lcnt);
Simon Glass767abfc2020-07-07 21:32:27 -0600177 return log_msg_ret("counts", -EINVAL);
Simon Glasse71b6f62020-01-23 11:48:14 -0700178 }
179
180 /*
181 * Now add things back up to ensure the period is hit. If it is off,
182 * split the difference and bias to lcnt for remainder
183 */
Heinrich Schuchardt35e80072023-10-13 15:09:39 +0200184 tot = hcnt + lcnt + T_HD_STA_OFFSET + spk_cnt + rise_cnt + 1;
Simon Glasse71b6f62020-01-23 11:48:14 -0700185
186 if (tot < period_cnt) {
187 diff = (period_cnt - tot) / 2;
188 hcnt += diff;
189 lcnt += diff;
Heinrich Schuchardt35e80072023-10-13 15:09:39 +0200190 tot = hcnt + lcnt + T_HD_STA_OFFSET + spk_cnt + rise_cnt + 1;
Simon Glasse71b6f62020-01-23 11:48:14 -0700191 lcnt += period_cnt - tot;
192 }
193
194 config->scl_lcnt = lcnt;
195 config->scl_hcnt = hcnt;
196
197 /* Use internal default unless other value is specified */
198 sda_hold_time_ns = priv && priv->sda_hold_time_ns ?
199 priv->sda_hold_time_ns : DEFAULT_SDA_HOLD_TIME;
200 config->sda_hold = calc_counts(ic_clk, sda_hold_time_ns);
201
202 debug("dw_i2c: hcnt = %d lcnt = %d sda hold = %d\n", hcnt, lcnt,
203 config->sda_hold);
204
205 return 0;
206}
207
Simon Glassbcf08502020-04-22 10:13:53 -0600208/**
209 * calc_bus_speed() - Calculate the config to use for a particular i2c speed
210 *
211 * @priv: Private information for the driver (NULL if not using driver model)
212 * @i2c_base: Registers for the I2C controller
213 * @speed: Required i2c speed in Hz
214 * @bus_clk: Input clock to the I2C controller in Hz (e.g. IC_CLK)
215 * @config: Returns the config to use for this speed
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100216 * Return: 0 if OK, -ve on error
Simon Glassbcf08502020-04-22 10:13:53 -0600217 */
218static int calc_bus_speed(struct dw_i2c *priv, struct i2c_regs *regs, int speed,
219 ulong bus_clk, struct dw_i2c_speed_config *config)
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530220{
Simon Glassd22409e2020-01-23 11:48:12 -0700221 const struct dw_scl_sda_cfg *scl_sda_cfg = NULL;
Simon Glass65190d12020-01-23 11:48:08 -0700222 enum i2c_speed_mode i2c_spd;
Simon Glass96fe11c2020-01-23 11:48:15 -0700223 int spk_cnt;
Simon Glasse71b6f62020-01-23 11:48:14 -0700224 int ret;
Stefan Roese11b544a2016-04-21 08:19:39 +0200225
Simon Glassd22409e2020-01-23 11:48:12 -0700226 if (priv)
227 scl_sda_cfg = priv->scl_sda_cfg;
Simon Glass6db79432020-01-23 11:48:07 -0700228 /* Allow high speed if there is no config, or the config allows it */
Jun Chenbe263422020-03-02 16:58:56 +0800229 if (speed >= I2C_SPEED_HIGH_RATE)
Simon Glass6db79432020-01-23 11:48:07 -0700230 i2c_spd = IC_SPEED_MODE_HIGH;
Simon Glassd96440d2020-01-23 11:48:23 -0700231 else if (speed >= I2C_SPEED_FAST_PLUS_RATE)
Simon Glass64d44c42020-02-13 13:24:55 -0700232 i2c_spd = IC_SPEED_MODE_FAST_PLUS;
233 else if (speed >= I2C_SPEED_FAST_RATE)
Stefan Roese11b544a2016-04-21 08:19:39 +0200234 i2c_spd = IC_SPEED_MODE_FAST;
235 else
236 i2c_spd = IC_SPEED_MODE_STANDARD;
Armando Visconti5e3e8dd2012-03-29 20:10:17 +0000237
Jun Chen565e3282020-03-02 16:58:55 +0800238 /* Check is high speed possible and fall back to fast mode if not */
239 if (i2c_spd == IC_SPEED_MODE_HIGH) {
Simon Glassbcf08502020-04-22 10:13:53 -0600240 u32 comp_param1;
241
242 comp_param1 = readl(&regs->comp_param1);
Jun Chen565e3282020-03-02 16:58:55 +0800243 if ((comp_param1 & DW_IC_COMP_PARAM_1_SPEED_MODE_MASK)
244 != DW_IC_COMP_PARAM_1_SPEED_MODE_HIGH)
245 i2c_spd = IC_SPEED_MODE_FAST;
246 }
247
Simon Glass23ad52e2020-01-23 11:48:25 -0700248 /* Get the proper spike-suppression count based on target speed */
249 if (!priv || !priv->has_spk_cnt)
250 spk_cnt = 0;
251 else if (i2c_spd >= IC_SPEED_MODE_HIGH)
252 spk_cnt = readl(&regs->hs_spklen);
253 else
254 spk_cnt = readl(&regs->fs_spklen);
255 if (scl_sda_cfg) {
256 config->sda_hold = scl_sda_cfg->sda_hold;
257 if (i2c_spd == IC_SPEED_MODE_STANDARD) {
258 config->scl_hcnt = scl_sda_cfg->ss_hcnt;
259 config->scl_lcnt = scl_sda_cfg->ss_lcnt;
Jun Chen27d483b2020-03-02 16:58:57 +0800260 } else if (i2c_spd == IC_SPEED_MODE_HIGH) {
261 config->scl_hcnt = scl_sda_cfg->hs_hcnt;
262 config->scl_lcnt = scl_sda_cfg->hs_lcnt;
Simon Glass23ad52e2020-01-23 11:48:25 -0700263 } else {
264 config->scl_hcnt = scl_sda_cfg->fs_hcnt;
265 config->scl_lcnt = scl_sda_cfg->fs_lcnt;
266 }
267 } else {
268 ret = dw_i2c_calc_timing(priv, i2c_spd, bus_clk, spk_cnt,
269 config);
270 if (ret)
271 return log_msg_ret("gen_confg", ret);
272 }
273 config->speed_mode = i2c_spd;
274
275 return 0;
276}
277
Simon Glassbcf08502020-04-22 10:13:53 -0600278/**
279 * _dw_i2c_set_bus_speed() - Set the i2c speed
Simon Glass23ad52e2020-01-23 11:48:25 -0700280 *
Simon Glassbcf08502020-04-22 10:13:53 -0600281 * @priv: Private information for the driver (NULL if not using driver model)
282 * @i2c_base: Registers for the I2C controller
283 * @speed: Required i2c speed in Hz
284 * @bus_clk: Input clock to the I2C controller in Hz (e.g. IC_CLK)
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100285 * Return: 0 if OK, -ve on error
Simon Glass23ad52e2020-01-23 11:48:25 -0700286 */
287static int _dw_i2c_set_bus_speed(struct dw_i2c *priv, struct i2c_regs *i2c_base,
288 unsigned int speed, unsigned int bus_clk)
289{
290 struct dw_i2c_speed_config config;
291 unsigned int cntl;
292 unsigned int ena;
293 int ret;
294
Simon Glassbcf08502020-04-22 10:13:53 -0600295 ret = calc_bus_speed(priv, i2c_base, speed, bus_clk, &config);
Simon Glass23ad52e2020-01-23 11:48:25 -0700296 if (ret)
297 return ret;
298
Jun Chene3b93dc2019-06-05 15:23:16 +0800299 /* Get enable setting for restore later */
300 ena = readl(&i2c_base->ic_enable) & IC_ENABLE_0B;
301
Armando Visconti5e3e8dd2012-03-29 20:10:17 +0000302 /* to set speed cltr must be disabled */
Stefan Roese1c8b0892016-04-21 08:19:38 +0200303 dw_i2c_enable(i2c_base, false);
Armando Visconti5e3e8dd2012-03-29 20:10:17 +0000304
Stefan Roese678398b2014-10-28 12:12:00 +0100305 cntl = (readl(&i2c_base->ic_con) & (~IC_CON_SPD_MSK));
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530306
Simon Glass23ad52e2020-01-23 11:48:25 -0700307 switch (config.speed_mode) {
Simon Glass6db79432020-01-23 11:48:07 -0700308 case IC_SPEED_MODE_HIGH:
Jun Chen70c894f2020-03-02 16:58:54 +0800309 cntl |= IC_CON_SPD_HS;
Simon Glass31adb872020-01-23 11:48:13 -0700310 writel(config.scl_hcnt, &i2c_base->ic_hs_scl_hcnt);
311 writel(config.scl_lcnt, &i2c_base->ic_hs_scl_lcnt);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530312 break;
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530313 case IC_SPEED_MODE_STANDARD:
314 cntl |= IC_CON_SPD_SS;
Simon Glass31adb872020-01-23 11:48:13 -0700315 writel(config.scl_hcnt, &i2c_base->ic_ss_scl_hcnt);
316 writel(config.scl_lcnt, &i2c_base->ic_ss_scl_lcnt);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530317 break;
Simon Glassd96440d2020-01-23 11:48:23 -0700318 case IC_SPEED_MODE_FAST_PLUS:
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530319 case IC_SPEED_MODE_FAST:
320 default:
321 cntl |= IC_CON_SPD_FS;
Simon Glass31adb872020-01-23 11:48:13 -0700322 writel(config.scl_hcnt, &i2c_base->ic_fs_scl_hcnt);
323 writel(config.scl_lcnt, &i2c_base->ic_fs_scl_lcnt);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530324 break;
325 }
326
Stefan Roese678398b2014-10-28 12:12:00 +0100327 writel(cntl, &i2c_base->ic_con);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530328
Stefan Roeseba5da552016-04-21 08:19:42 +0200329 /* Configure SDA Hold Time if required */
Simon Glass31adb872020-01-23 11:48:13 -0700330 if (config.sda_hold)
331 writel(config.sda_hold, &i2c_base->ic_sda_hold);
Stefan Roeseba5da552016-04-21 08:19:42 +0200332
Jun Chene3b93dc2019-06-05 15:23:16 +0800333 /* Restore back i2c now speed set */
334 if (ena == IC_ENABLE_0B)
335 dw_i2c_enable(i2c_base, true);
Simon Glass4b0ec522020-07-07 21:32:29 -0600336 if (priv)
337 priv->config = config;
338
339 return 0;
340}
341
342int dw_i2c_gen_speed_config(const struct udevice *dev, int speed_hz,
343 struct dw_i2c_speed_config *config)
344{
345 struct dw_i2c *priv = dev_get_priv(dev);
346 ulong rate;
347 int ret;
348
349#if CONFIG_IS_ENABLED(CLK)
350 rate = clk_get_rate(&priv->clk);
351 if (IS_ERR_VALUE(rate))
352 return log_msg_ret("clk", -EINVAL);
353#else
354 rate = IC_CLK;
355#endif
356
357 ret = calc_bus_speed(priv, priv->regs, speed_hz, rate, config);
358 if (ret)
359 printf("%s: ret=%d\n", __func__, ret);
360 if (ret)
361 return log_msg_ret("calc_bus_speed", ret);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530362
Stefan Roese3f4358d2016-04-21 08:19:40 +0200363 return 0;
364}
365
366/*
367 * i2c_setaddress - Sets the target slave address
368 * @i2c_addr: target i2c address
369 *
370 * Sets the target slave address.
371 */
372static void i2c_setaddress(struct i2c_regs *i2c_base, unsigned int i2c_addr)
373{
374 /* Disable i2c */
375 dw_i2c_enable(i2c_base, false);
376
377 writel(i2c_addr, &i2c_base->ic_tar);
378
379 /* Enable i2c */
380 dw_i2c_enable(i2c_base, true);
381}
382
383/*
384 * i2c_flush_rxfifo - Flushes the i2c RX FIFO
385 *
386 * Flushes the i2c RX FIFO
387 */
388static void i2c_flush_rxfifo(struct i2c_regs *i2c_base)
389{
390 while (readl(&i2c_base->ic_status) & IC_STATUS_RFNE)
391 readl(&i2c_base->ic_cmd_data);
392}
393
394/*
395 * i2c_wait_for_bb - Waits for bus busy
396 *
397 * Waits for bus busy
398 */
399static int i2c_wait_for_bb(struct i2c_regs *i2c_base)
400{
401 unsigned long start_time_bb = get_timer(0);
402
403 while ((readl(&i2c_base->ic_status) & IC_STATUS_MA) ||
404 !(readl(&i2c_base->ic_status) & IC_STATUS_TFE)) {
405
406 /* Evaluate timeout */
407 if (get_timer(start_time_bb) > (unsigned long)(I2C_BYTE_TO_BB))
408 return 1;
409 }
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530410
411 return 0;
412}
413
Stefan Roese3f4358d2016-04-21 08:19:40 +0200414static int i2c_xfer_init(struct i2c_regs *i2c_base, uchar chip, uint addr,
415 int alen)
416{
417 if (i2c_wait_for_bb(i2c_base))
418 return 1;
419
420 i2c_setaddress(i2c_base, chip);
421 while (alen) {
422 alen--;
423 /* high byte address going out first */
424 writel((addr >> (alen * 8)) & 0xff,
425 &i2c_base->ic_cmd_data);
426 }
427 return 0;
428}
429
430static int i2c_xfer_finish(struct i2c_regs *i2c_base)
431{
432 ulong start_stop_det = get_timer(0);
433
434 while (1) {
435 if ((readl(&i2c_base->ic_raw_intr_stat) & IC_STOP_DET)) {
436 readl(&i2c_base->ic_clr_stop_det);
437 break;
438 } else if (get_timer(start_stop_det) > I2C_STOPDET_TO) {
439 break;
440 }
441 }
442
443 if (i2c_wait_for_bb(i2c_base)) {
444 printf("Timed out waiting for bus\n");
445 return 1;
446 }
447
448 i2c_flush_rxfifo(i2c_base);
449
450 return 0;
451}
452
453/*
454 * i2c_read - Read from i2c memory
455 * @chip: target i2c address
456 * @addr: address to read from
457 * @alen:
458 * @buffer: buffer for read data
459 * @len: no of bytes to be read
460 *
461 * Read from i2c memory.
462 */
463static int __dw_i2c_read(struct i2c_regs *i2c_base, u8 dev, uint addr,
464 int alen, u8 *buffer, int len)
465{
466 unsigned long start_time_rx;
Marek Vasutb0338082016-10-20 16:48:28 +0200467 unsigned int active = 0;
Stefan Roese3f4358d2016-04-21 08:19:40 +0200468
469#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
470 /*
471 * EEPROM chips that implement "address overflow" are ones
472 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
473 * address and the extra bits end up in the "chip address"
474 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
475 * four 256 byte chips.
476 *
477 * Note that we consider the length of the address field to
478 * still be one byte because the extra address bits are
479 * hidden in the chip address.
480 */
481 dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
482 addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
483
484 debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
485 addr);
486#endif
487
488 if (i2c_xfer_init(i2c_base, dev, addr, alen))
489 return 1;
490
491 start_time_rx = get_timer(0);
492 while (len) {
Marek Vasutb0338082016-10-20 16:48:28 +0200493 if (!active) {
494 /*
495 * Avoid writing to ic_cmd_data multiple times
496 * in case this loop spins too quickly and the
497 * ic_status RFNE bit isn't set after the first
498 * write. Subsequent writes to ic_cmd_data can
499 * trigger spurious i2c transfer.
500 */
501 if (len == 1)
502 writel(IC_CMD | IC_STOP, &i2c_base->ic_cmd_data);
503 else
504 writel(IC_CMD, &i2c_base->ic_cmd_data);
505 active = 1;
506 }
Stefan Roese3f4358d2016-04-21 08:19:40 +0200507
508 if (readl(&i2c_base->ic_status) & IC_STATUS_RFNE) {
509 *buffer++ = (uchar)readl(&i2c_base->ic_cmd_data);
510 len--;
511 start_time_rx = get_timer(0);
Marek Vasutb0338082016-10-20 16:48:28 +0200512 active = 0;
Stefan Roese3f4358d2016-04-21 08:19:40 +0200513 } else if (get_timer(start_time_rx) > I2C_BYTE_TO) {
Marek Vasutb0338082016-10-20 16:48:28 +0200514 return 1;
Stefan Roese3f4358d2016-04-21 08:19:40 +0200515 }
516 }
517
518 return i2c_xfer_finish(i2c_base);
519}
520
521/*
522 * i2c_write - Write to i2c memory
523 * @chip: target i2c address
524 * @addr: address to read from
525 * @alen:
526 * @buffer: buffer for read data
527 * @len: no of bytes to be read
528 *
529 * Write to i2c memory.
530 */
531static int __dw_i2c_write(struct i2c_regs *i2c_base, u8 dev, uint addr,
532 int alen, u8 *buffer, int len)
533{
534 int nb = len;
535 unsigned long start_time_tx;
536
537#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
538 /*
539 * EEPROM chips that implement "address overflow" are ones
540 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
541 * address and the extra bits end up in the "chip address"
542 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
543 * four 256 byte chips.
544 *
545 * Note that we consider the length of the address field to
546 * still be one byte because the extra address bits are
547 * hidden in the chip address.
548 */
549 dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
550 addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
551
552 debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
553 addr);
554#endif
555
556 if (i2c_xfer_init(i2c_base, dev, addr, alen))
557 return 1;
558
559 start_time_tx = get_timer(0);
560 while (len) {
561 if (readl(&i2c_base->ic_status) & IC_STATUS_TFNF) {
562 if (--len == 0) {
563 writel(*buffer | IC_STOP,
564 &i2c_base->ic_cmd_data);
565 } else {
566 writel(*buffer, &i2c_base->ic_cmd_data);
567 }
568 buffer++;
569 start_time_tx = get_timer(0);
570
571 } else if (get_timer(start_time_tx) > (nb * I2C_BYTE_TO)) {
572 printf("Timed out. i2c write Failed\n");
573 return 1;
574 }
575 }
576
577 return i2c_xfer_finish(i2c_base);
578}
579
Stefan Roese334b9b02016-04-21 08:19:41 +0200580/*
581 * __dw_i2c_init - Init function
582 * @speed: required i2c speed
583 * @slaveaddr: slave address for the device
584 *
585 * Initialization function.
586 */
Simon Glass2b5d0292019-02-16 20:24:39 -0700587static int __dw_i2c_init(struct i2c_regs *i2c_base, int speed, int slaveaddr)
Stefan Roese334b9b02016-04-21 08:19:41 +0200588{
Simon Glass2b5d0292019-02-16 20:24:39 -0700589 int ret;
590
Stefan Roese334b9b02016-04-21 08:19:41 +0200591 /* Disable i2c */
Simon Glass2b5d0292019-02-16 20:24:39 -0700592 ret = dw_i2c_enable(i2c_base, false);
593 if (ret)
594 return ret;
Stefan Roese334b9b02016-04-21 08:19:41 +0200595
Marek Vasut014e47f2017-08-07 20:45:31 +0200596 writel(IC_CON_SD | IC_CON_RE | IC_CON_SPD_FS | IC_CON_MM,
597 &i2c_base->ic_con);
Stefan Roese334b9b02016-04-21 08:19:41 +0200598 writel(IC_RX_TL, &i2c_base->ic_rx_tl);
599 writel(IC_TX_TL, &i2c_base->ic_tx_tl);
600 writel(IC_STOP_DET, &i2c_base->ic_intr_mask);
Igor Opaniuk2147a162021-02-09 13:52:45 +0200601#if !CONFIG_IS_ENABLED(DM_I2C)
Simon Glass23ad52e2020-01-23 11:48:25 -0700602 _dw_i2c_set_bus_speed(NULL, i2c_base, speed, IC_CLK);
Stefan Roese334b9b02016-04-21 08:19:41 +0200603 writel(slaveaddr, &i2c_base->ic_sar);
604#endif
605
606 /* Enable i2c */
Simon Glass2b5d0292019-02-16 20:24:39 -0700607 ret = dw_i2c_enable(i2c_base, true);
608 if (ret)
609 return ret;
610
611 return 0;
Stefan Roese334b9b02016-04-21 08:19:41 +0200612}
613
Igor Opaniuk2147a162021-02-09 13:52:45 +0200614#if !CONFIG_IS_ENABLED(DM_I2C)
Stefan Roese334b9b02016-04-21 08:19:41 +0200615/*
616 * The legacy I2C functions. These need to get removed once
617 * all users of this driver are converted to DM.
618 */
Stefan Roese3f4358d2016-04-21 08:19:40 +0200619static struct i2c_regs *i2c_get_base(struct i2c_adapter *adap)
620{
621 switch (adap->hwadapnr) {
622#if CONFIG_SYS_I2C_BUS_MAX >= 4
623 case 3:
624 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE3;
625#endif
626#if CONFIG_SYS_I2C_BUS_MAX >= 3
627 case 2:
628 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE2;
629#endif
630#if CONFIG_SYS_I2C_BUS_MAX >= 2
631 case 1:
632 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE1;
633#endif
634 case 0:
635 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE;
636 default:
637 printf("Wrong I2C-adapter number %d\n", adap->hwadapnr);
638 }
639
640 return NULL;
641}
642
643static unsigned int dw_i2c_set_bus_speed(struct i2c_adapter *adap,
644 unsigned int speed)
645{
646 adap->speed = speed;
Simon Glass23ad52e2020-01-23 11:48:25 -0700647 return _dw_i2c_set_bus_speed(NULL, i2c_get_base(adap), speed, IC_CLK);
Stefan Roese3f4358d2016-04-21 08:19:40 +0200648}
649
Stefan Roese334b9b02016-04-21 08:19:41 +0200650static void dw_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530651{
Stefan Roese334b9b02016-04-21 08:19:41 +0200652 __dw_i2c_init(i2c_get_base(adap), speed, slaveaddr);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530653}
654
Stefan Roese678398b2014-10-28 12:12:00 +0100655static int dw_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr,
656 int alen, u8 *buffer, int len)
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530657{
Stefan Roese3f4358d2016-04-21 08:19:40 +0200658 return __dw_i2c_read(i2c_get_base(adap), dev, addr, alen, buffer, len);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530659}
660
Stefan Roese678398b2014-10-28 12:12:00 +0100661static int dw_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr,
662 int alen, u8 *buffer, int len)
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530663{
Stefan Roese3f4358d2016-04-21 08:19:40 +0200664 return __dw_i2c_write(i2c_get_base(adap), dev, addr, alen, buffer, len);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530665}
666
Stefan Roese334b9b02016-04-21 08:19:41 +0200667/* dw_i2c_probe - Probe the i2c chip */
Stefan Roese678398b2014-10-28 12:12:00 +0100668static int dw_i2c_probe(struct i2c_adapter *adap, u8 dev)
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530669{
Stefan Roese3f4358d2016-04-21 08:19:40 +0200670 struct i2c_regs *i2c_base = i2c_get_base(adap);
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530671 u32 tmp;
Stefan Roese496ba482012-01-20 11:52:33 +0100672 int ret;
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530673
674 /*
675 * Try to read the first location of the chip.
676 */
Stefan Roese3f4358d2016-04-21 08:19:40 +0200677 ret = __dw_i2c_read(i2c_base, dev, 0, 1, (uchar *)&tmp, 1);
Stefan Roese496ba482012-01-20 11:52:33 +0100678 if (ret)
Stefan Roese678398b2014-10-28 12:12:00 +0100679 dw_i2c_init(adap, adap->speed, adap->slaveaddr);
Stefan Roese496ba482012-01-20 11:52:33 +0100680
681 return ret;
Vipin KUMAR2403f8f2010-01-15 19:15:44 +0530682}
Armando Viscontiac6e2fe2012-12-06 00:04:15 +0000683
Stefan Roese678398b2014-10-28 12:12:00 +0100684U_BOOT_I2C_ADAP_COMPLETE(dw_0, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
685 dw_i2c_write, dw_i2c_set_bus_speed,
686 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0)
Armando Viscontiac6e2fe2012-12-06 00:04:15 +0000687
Stefan Roese334b9b02016-04-21 08:19:41 +0200688#else /* CONFIG_DM_I2C */
689/* The DM I2C functions */
690
691static int designware_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
692 int nmsgs)
693{
694 struct dw_i2c *i2c = dev_get_priv(bus);
695 int ret;
696
697 debug("i2c_xfer: %d messages\n", nmsgs);
698 for (; nmsgs > 0; nmsgs--, msg++) {
699 debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
700 if (msg->flags & I2C_M_RD) {
701 ret = __dw_i2c_read(i2c->regs, msg->addr, 0, 0,
702 msg->buf, msg->len);
703 } else {
704 ret = __dw_i2c_write(i2c->regs, msg->addr, 0, 0,
705 msg->buf, msg->len);
706 }
707 if (ret) {
708 debug("i2c_write: error sending\n");
709 return -EREMOTEIO;
710 }
711 }
712
713 return 0;
714}
715
716static int designware_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
717{
718 struct dw_i2c *i2c = dev_get_priv(bus);
Ley Foon Tan2d1e8792019-06-12 09:48:04 +0800719 ulong rate;
Stefan Roese334b9b02016-04-21 08:19:41 +0200720
Ley Foon Tan2d1e8792019-06-12 09:48:04 +0800721#if CONFIG_IS_ENABLED(CLK)
722 rate = clk_get_rate(&i2c->clk);
723 if (IS_ERR_VALUE(rate))
Simon Glass767abfc2020-07-07 21:32:27 -0600724 return log_ret(-EINVAL);
Ley Foon Tan2d1e8792019-06-12 09:48:04 +0800725#else
726 rate = IC_CLK;
727#endif
Simon Glass23ad52e2020-01-23 11:48:25 -0700728 return _dw_i2c_set_bus_speed(i2c, i2c->regs, speed, rate);
Stefan Roese334b9b02016-04-21 08:19:41 +0200729}
730
731static int designware_i2c_probe_chip(struct udevice *bus, uint chip_addr,
732 uint chip_flags)
733{
734 struct dw_i2c *i2c = dev_get_priv(bus);
735 struct i2c_regs *i2c_base = i2c->regs;
736 u32 tmp;
737 int ret;
738
739 /* Try to read the first location of the chip */
740 ret = __dw_i2c_read(i2c_base, chip_addr, 0, 1, (uchar *)&tmp, 1);
741 if (ret)
742 __dw_i2c_init(i2c_base, 0, 0);
743
744 return ret;
745}
746
Simon Glassd1998a92020-12-03 16:55:21 -0700747int designware_i2c_of_to_plat(struct udevice *bus)
Simon Glass457df232019-12-06 21:41:40 -0700748{
749 struct dw_i2c *priv = dev_get_priv(bus);
Simon Glass2034f6c2020-01-23 11:48:26 -0700750 int ret;
Simon Glass457df232019-12-06 21:41:40 -0700751
Simon Glass80a03db2020-01-23 11:48:11 -0700752 if (!priv->regs)
Masahiro Yamada702e57e2020-08-04 14:14:43 +0900753 priv->regs = dev_read_addr_ptr(bus);
Simon Glass80a03db2020-01-23 11:48:11 -0700754 dev_read_u32(bus, "i2c-scl-rising-time-ns", &priv->scl_rise_time_ns);
755 dev_read_u32(bus, "i2c-scl-falling-time-ns", &priv->scl_fall_time_ns);
756 dev_read_u32(bus, "i2c-sda-hold-time-ns", &priv->sda_hold_time_ns);
Simon Glass457df232019-12-06 21:41:40 -0700757
Simon Goldschmidt36821b32019-03-28 21:11:48 +0100758 ret = reset_get_bulk(bus, &priv->resets);
Simon Glass94201222020-11-09 07:12:49 -0700759 if (ret) {
760 if (ret != -ENOTSUPP)
761 dev_warn(bus, "Can't get reset: %d\n", ret);
762 } else {
Simon Goldschmidt36821b32019-03-28 21:11:48 +0100763 reset_deassert_bulk(&priv->resets);
Simon Glass94201222020-11-09 07:12:49 -0700764 }
Dinh Nguyen622597d2018-04-04 17:18:24 -0500765
Ley Foon Tan2d1e8792019-06-12 09:48:04 +0800766#if CONFIG_IS_ENABLED(CLK)
767 ret = clk_get_by_index(bus, 0, &priv->clk);
768 if (ret)
769 return ret;
770
771 ret = clk_enable(&priv->clk);
772 if (ret && ret != -ENOSYS && ret != -ENOTSUPP) {
Ley Foon Tan2d1e8792019-06-12 09:48:04 +0800773 dev_err(bus, "failed to enable clock\n");
774 return ret;
775 }
776#endif
777
Simon Glass2034f6c2020-01-23 11:48:26 -0700778 return 0;
779}
780
781int designware_i2c_probe(struct udevice *bus)
782{
783 struct dw_i2c *priv = dev_get_priv(bus);
Raul E Rangelf6f9a012020-04-22 10:13:54 -0600784 uint comp_type;
785
786 comp_type = readl(&priv->regs->comp_type);
787 if (comp_type != DW_I2C_COMP_TYPE) {
788 log_err("I2C bus %s has unknown type %#x\n", bus->name,
789 comp_type);
790 return -ENXIO;
791 }
792
Simon Glassacdd2482020-09-27 18:46:23 -0600793 log_debug("I2C bus %s version %#x\n", bus->name,
794 readl(&priv->regs->comp_version));
Simon Glass2034f6c2020-01-23 11:48:26 -0700795
Simon Glass2b5d0292019-02-16 20:24:39 -0700796 return __dw_i2c_init(priv->regs, 0, 0);
Stefan Roese334b9b02016-04-21 08:19:41 +0200797}
798
Simon Glass457df232019-12-06 21:41:40 -0700799int designware_i2c_remove(struct udevice *dev)
Simon Goldschmidt36821b32019-03-28 21:11:48 +0100800{
801 struct dw_i2c *priv = dev_get_priv(dev);
802
Ley Foon Tan2d1e8792019-06-12 09:48:04 +0800803#if CONFIG_IS_ENABLED(CLK)
804 clk_disable(&priv->clk);
Ley Foon Tan2d1e8792019-06-12 09:48:04 +0800805#endif
806
Simon Goldschmidt36821b32019-03-28 21:11:48 +0100807 return reset_release_bulk(&priv->resets);
808}
809
Simon Glass457df232019-12-06 21:41:40 -0700810const struct dm_i2c_ops designware_i2c_ops = {
Stefan Roese334b9b02016-04-21 08:19:41 +0200811 .xfer = designware_i2c_xfer,
812 .probe_chip = designware_i2c_probe_chip,
813 .set_bus_speed = designware_i2c_set_bus_speed,
814};
815
816static const struct udevice_id designware_i2c_ids[] = {
817 { .compatible = "snps,designware-i2c" },
818 { }
819};
820
821U_BOOT_DRIVER(i2c_designware) = {
822 .name = "i2c_designware",
823 .id = UCLASS_I2C,
824 .of_match = designware_i2c_ids,
Simon Glassd1998a92020-12-03 16:55:21 -0700825 .of_to_plat = designware_i2c_of_to_plat,
Stefan Roese334b9b02016-04-21 08:19:41 +0200826 .probe = designware_i2c_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700827 .priv_auto = sizeof(struct dw_i2c),
Simon Goldschmidt36821b32019-03-28 21:11:48 +0100828 .remove = designware_i2c_remove,
Simon Glass457df232019-12-06 21:41:40 -0700829 .flags = DM_FLAG_OS_PREPARE,
Stefan Roese334b9b02016-04-21 08:19:41 +0200830 .ops = &designware_i2c_ops,
831};
832
833#endif /* CONFIG_DM_I2C */