blob: d9e0d81ff0559cd579400e38105fddcfe538b59d [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
Simon Glass245ec0b2020-01-23 11:48:13 -070016/**
17 * struct dw_i2c_speed_config - timings to use for a particular speed
18 *
19 * This holds calculated values to be written to the I2C controller. Each value
20 * is represented as a number of IC clock cycles.
21 *
22 * @scl_lcnt: Low count value for SCL
23 * @scl_hcnt: High count value for SCL
24 * @sda_hold: Data hold count
25 */
26struct dw_i2c_speed_config {
27 /* SCL high and low period count */
28 uint scl_lcnt;
29 uint scl_hcnt;
30 uint sda_hold;
31};
32
Stefan Roeseabb3e132016-04-27 09:02:12 +020033#ifdef CONFIG_SYS_I2C_DW_ENABLE_STATUS_UNSUPPORTED
Simon Glassbd9ca8d2019-02-16 20:24:39 -070034static int dw_i2c_enable(struct i2c_regs *i2c_base, bool enable)
Stefan Roese3bc33ba2016-04-21 08:19:38 +020035{
36 u32 ena = enable ? IC_ENABLE_0B : 0;
Stefan Roeseabb3e132016-04-27 09:02:12 +020037
38 writel(ena, &i2c_base->ic_enable);
Simon Glassbd9ca8d2019-02-16 20:24:39 -070039
40 return 0;
Stefan Roeseabb3e132016-04-27 09:02:12 +020041}
42#else
Simon Glassbd9ca8d2019-02-16 20:24:39 -070043static int dw_i2c_enable(struct i2c_regs *i2c_base, bool enable)
Stefan Roeseabb3e132016-04-27 09:02:12 +020044{
45 u32 ena = enable ? IC_ENABLE_0B : 0;
Stefan Roese3bc33ba2016-04-21 08:19:38 +020046 int timeout = 100;
47
48 do {
49 writel(ena, &i2c_base->ic_enable);
50 if ((readl(&i2c_base->ic_enable_status) & IC_ENABLE_0B) == ena)
Simon Glassbd9ca8d2019-02-16 20:24:39 -070051 return 0;
Stefan Roese3bc33ba2016-04-21 08:19:38 +020052
53 /*
54 * Wait 10 times the signaling period of the highest I2C
55 * transfer supported by the driver (for 400KHz this is
56 * 25us) as described in the DesignWare I2C databook.
57 */
58 udelay(25);
59 } while (timeout--);
Stefan Roese3bc33ba2016-04-21 08:19:38 +020060 printf("timeout in %sabling I2C adapter\n", enable ? "en" : "dis");
Simon Glassbd9ca8d2019-02-16 20:24:39 -070061
62 return -ETIMEDOUT;
Stefan Roese3bc33ba2016-04-21 08:19:38 +020063}
Stefan Roeseabb3e132016-04-27 09:02:12 +020064#endif
Stefan Roese3bc33ba2016-04-21 08:19:38 +020065
Simon Glassc7181102020-01-23 11:48:14 -070066/* High and low times in different speed modes (in ns) */
67enum {
68 /* SDA Hold Time */
69 DEFAULT_SDA_HOLD_TIME = 300,
70};
71
72/**
73 * calc_counts() - Convert a period to a number of IC clk cycles
74 *
75 * @ic_clk: Input clock in Hz
76 * @period_ns: Period to represent, in ns
77 * @return calculated count
78 */
79static uint calc_counts(uint ic_clk, uint period_ns)
80{
81 return DIV_ROUND_UP(ic_clk / 1000 * period_ns, NANO_TO_KILO);
82}
83
84/**
85 * struct i2c_mode_info - Information about an I2C speed mode
86 *
87 * Each speed mode has its own characteristics. This struct holds these to aid
88 * calculations in dw_i2c_calc_timing().
89 *
90 * @speed: Speed in Hz
91 * @min_scl_lowtime_ns: Minimum value for SCL low period in ns
92 * @min_scl_hightime_ns: Minimum value for SCL high period in ns
93 * @def_rise_time_ns: Default rise time in ns
94 * @def_fall_time_ns: Default fall time in ns
95 */
96struct i2c_mode_info {
97 int speed;
98 int min_scl_hightime_ns;
99 int min_scl_lowtime_ns;
100 int def_rise_time_ns;
101 int def_fall_time_ns;
102};
103
104static const struct i2c_mode_info info_for_mode[] = {
105 [IC_SPEED_MODE_STANDARD] = {
Simon Glassac77bae2020-01-23 11:48:18 -0700106 I2C_SPEED_STANDARD_RATE,
Simon Glassc7181102020-01-23 11:48:14 -0700107 MIN_SS_SCL_HIGHTIME,
108 MIN_SS_SCL_LOWTIME,
109 1000,
110 300,
111 },
112 [IC_SPEED_MODE_FAST] = {
Simon Glassac77bae2020-01-23 11:48:18 -0700113 I2C_SPEED_FAST_RATE,
Simon Glassc7181102020-01-23 11:48:14 -0700114 MIN_FS_SCL_HIGHTIME,
115 MIN_FS_SCL_LOWTIME,
116 300,
117 300,
118 },
Simon Glass45649222020-01-23 11:48:23 -0700119 [IC_SPEED_MODE_FAST_PLUS] = {
120 I2C_SPEED_FAST_PLUS_RATE,
121 MIN_FP_SCL_HIGHTIME,
122 MIN_FP_SCL_LOWTIME,
123 260,
124 500,
125 },
Simon Glassc7181102020-01-23 11:48:14 -0700126 [IC_SPEED_MODE_HIGH] = {
Simon Glassac77bae2020-01-23 11:48:18 -0700127 I2C_SPEED_HIGH_RATE,
Simon Glassc7181102020-01-23 11:48:14 -0700128 MIN_HS_SCL_HIGHTIME,
129 MIN_HS_SCL_LOWTIME,
130 120,
131 120,
132 },
133};
134
135/**
136 * dw_i2c_calc_timing() - Calculate the timings to use for a bus
137 *
138 * @priv: Bus private information (NULL if not using driver model)
139 * @mode: Speed mode to use
140 * @ic_clk: IC clock speed in Hz
141 * @spk_cnt: Spike-suppression count
142 * @config: Returns value to use
143 * @return 0 if OK, -EINVAL if the calculation failed due to invalid data
144 */
145static int dw_i2c_calc_timing(struct dw_i2c *priv, enum i2c_speed_mode mode,
146 int ic_clk, int spk_cnt,
147 struct dw_i2c_speed_config *config)
148{
149 int fall_cnt, rise_cnt, min_tlow_cnt, min_thigh_cnt;
150 int hcnt, lcnt, period_cnt, diff, tot;
151 int sda_hold_time_ns, scl_rise_time_ns, scl_fall_time_ns;
152 const struct i2c_mode_info *info;
153
154 /*
155 * Find the period, rise, fall, min tlow, and min thigh in terms of
156 * counts of the IC clock
157 */
158 info = &info_for_mode[mode];
159 period_cnt = ic_clk / info->speed;
160 scl_rise_time_ns = priv && priv->scl_rise_time_ns ?
161 priv->scl_rise_time_ns : info->def_rise_time_ns;
162 scl_fall_time_ns = priv && priv->scl_fall_time_ns ?
163 priv->scl_fall_time_ns : info->def_fall_time_ns;
164 rise_cnt = calc_counts(ic_clk, scl_rise_time_ns);
165 fall_cnt = calc_counts(ic_clk, scl_fall_time_ns);
166 min_tlow_cnt = calc_counts(ic_clk, info->min_scl_lowtime_ns);
167 min_thigh_cnt = calc_counts(ic_clk, info->min_scl_hightime_ns);
168
169 debug("dw_i2c: period %d rise %d fall %d tlow %d thigh %d spk %d\n",
170 period_cnt, rise_cnt, fall_cnt, min_tlow_cnt, min_thigh_cnt,
171 spk_cnt);
172
173 /*
174 * Back-solve for hcnt and lcnt according to the following equations:
175 * SCL_High_time = [(HCNT + IC_*_SPKLEN + 7) * ic_clk] + SCL_Fall_time
176 * SCL_Low_time = [(LCNT + 1) * ic_clk] - SCL_Fall_time + SCL_Rise_time
177 */
178 hcnt = min_thigh_cnt - fall_cnt - 7 - spk_cnt;
179 lcnt = min_tlow_cnt - rise_cnt + fall_cnt - 1;
180
181 if (hcnt < 0 || lcnt < 0) {
182 debug("dw_i2c: bad counts. hcnt = %d lcnt = %d\n", hcnt, lcnt);
183 return -EINVAL;
184 }
185
186 /*
187 * Now add things back up to ensure the period is hit. If it is off,
188 * split the difference and bias to lcnt for remainder
189 */
190 tot = hcnt + lcnt + 7 + spk_cnt + rise_cnt + 1;
191
192 if (tot < period_cnt) {
193 diff = (period_cnt - tot) / 2;
194 hcnt += diff;
195 lcnt += diff;
196 tot = hcnt + lcnt + 7 + spk_cnt + rise_cnt + 1;
197 lcnt += period_cnt - tot;
198 }
199
200 config->scl_lcnt = lcnt;
201 config->scl_hcnt = hcnt;
202
203 /* Use internal default unless other value is specified */
204 sda_hold_time_ns = priv && priv->sda_hold_time_ns ?
205 priv->sda_hold_time_ns : DEFAULT_SDA_HOLD_TIME;
206 config->sda_hold = calc_counts(ic_clk, sda_hold_time_ns);
207
208 debug("dw_i2c: hcnt = %d lcnt = %d sda hold = %d\n", hcnt, lcnt,
209 config->sda_hold);
210
211 return 0;
212}
213
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530214/*
Stefan Roese88893c92016-04-21 08:19:39 +0200215 * i2c_set_bus_speed - Set the i2c speed
216 * @speed: required i2c speed
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530217 *
Stefan Roese88893c92016-04-21 08:19:39 +0200218 * Set the i2c speed.
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530219 */
Simon Glass60e0c3a2020-01-23 11:48:12 -0700220static unsigned int __dw_i2c_set_bus_speed(struct dw_i2c *priv,
221 struct i2c_regs *i2c_base,
Ley Foon Tan6e85c812019-06-12 09:48:04 +0800222 unsigned int speed,
Simon Glass333dadd2020-01-23 11:48:09 -0700223 unsigned int bus_clk)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530224{
Simon Glass60e0c3a2020-01-23 11:48:12 -0700225 const struct dw_scl_sda_cfg *scl_sda_cfg = NULL;
Simon Glass245ec0b2020-01-23 11:48:13 -0700226 struct dw_i2c_speed_config config;
Simon Glass6ed44ae2020-01-23 11:48:08 -0700227 enum i2c_speed_mode i2c_spd;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530228 unsigned int cntl;
Jun Chend003a372019-06-05 15:23:16 +0800229 unsigned int ena;
Simon Glassc38e2b32020-01-23 11:48:15 -0700230 int spk_cnt;
Simon Glassc7181102020-01-23 11:48:14 -0700231 int ret;
Stefan Roese88893c92016-04-21 08:19:39 +0200232
Simon Glass60e0c3a2020-01-23 11:48:12 -0700233 if (priv)
234 scl_sda_cfg = priv->scl_sda_cfg;
Simon Glassf5ef1012020-01-23 11:48:07 -0700235 /* Allow high speed if there is no config, or the config allows it */
Simon Glassac77bae2020-01-23 11:48:18 -0700236 if (speed >= I2C_SPEED_HIGH_RATE &&
Simon Glassf5ef1012020-01-23 11:48:07 -0700237 (!scl_sda_cfg || scl_sda_cfg->has_high_speed))
238 i2c_spd = IC_SPEED_MODE_HIGH;
Simon Glassac77bae2020-01-23 11:48:18 -0700239 else if (speed >= I2C_SPEED_FAST_RATE)
Simon Glass45649222020-01-23 11:48:23 -0700240 i2c_spd = IC_SPEED_MODE_FAST_PLUS;
241 else if (speed >= I2C_SPEED_FAST_PLUS_RATE)
Stefan Roese88893c92016-04-21 08:19:39 +0200242 i2c_spd = IC_SPEED_MODE_FAST;
243 else
244 i2c_spd = IC_SPEED_MODE_STANDARD;
Armando Visconti631e6932012-03-29 20:10:17 +0000245
Jun Chend003a372019-06-05 15:23:16 +0800246 /* Get enable setting for restore later */
247 ena = readl(&i2c_base->ic_enable) & IC_ENABLE_0B;
248
Armando Visconti631e6932012-03-29 20:10:17 +0000249 /* to set speed cltr must be disabled */
Stefan Roese3bc33ba2016-04-21 08:19:38 +0200250 dw_i2c_enable(i2c_base, false);
Armando Visconti631e6932012-03-29 20:10:17 +0000251
Stefan Roeseef6073e2014-10-28 12:12:00 +0100252 cntl = (readl(&i2c_base->ic_con) & (~IC_CON_SPD_MSK));
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530253
Simon Glassc38e2b32020-01-23 11:48:15 -0700254 /* Get the proper spike-suppression count based on target speed */
255 if (!priv || !priv->has_spk_cnt)
256 spk_cnt = 0;
257 else if (i2c_spd >= IC_SPEED_MODE_HIGH)
258 spk_cnt = readl(&i2c_base->hs_spklen);
259 else
260 spk_cnt = readl(&i2c_base->fs_spklen);
Simon Glass245ec0b2020-01-23 11:48:13 -0700261 if (scl_sda_cfg) {
262 config.sda_hold = scl_sda_cfg->sda_hold;
263 if (i2c_spd == IC_SPEED_MODE_STANDARD) {
264 config.scl_hcnt = scl_sda_cfg->ss_hcnt;
265 config.scl_lcnt = scl_sda_cfg->ss_lcnt;
266 } else {
267 config.scl_hcnt = scl_sda_cfg->fs_hcnt;
268 config.scl_lcnt = scl_sda_cfg->fs_lcnt;
269 }
Simon Glassc7181102020-01-23 11:48:14 -0700270 } else {
Simon Glassc38e2b32020-01-23 11:48:15 -0700271 ret = dw_i2c_calc_timing(priv, i2c_spd, bus_clk, spk_cnt,
Simon Glassc7181102020-01-23 11:48:14 -0700272 &config);
273 if (ret)
274 return log_msg_ret("gen_confg", ret);
Simon Glass245ec0b2020-01-23 11:48:13 -0700275 }
276
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530277 switch (i2c_spd) {
Simon Glassf5ef1012020-01-23 11:48:07 -0700278 case IC_SPEED_MODE_HIGH:
Stefan Roese38481202016-04-21 08:19:42 +0200279 cntl |= IC_CON_SPD_SS;
Simon Glass245ec0b2020-01-23 11:48:13 -0700280 writel(config.scl_hcnt, &i2c_base->ic_hs_scl_hcnt);
281 writel(config.scl_lcnt, &i2c_base->ic_hs_scl_lcnt);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530282 break;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530283 case IC_SPEED_MODE_STANDARD:
284 cntl |= IC_CON_SPD_SS;
Simon Glass245ec0b2020-01-23 11:48:13 -0700285 writel(config.scl_hcnt, &i2c_base->ic_ss_scl_hcnt);
286 writel(config.scl_lcnt, &i2c_base->ic_ss_scl_lcnt);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530287 break;
Simon Glass45649222020-01-23 11:48:23 -0700288 case IC_SPEED_MODE_FAST_PLUS:
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530289 case IC_SPEED_MODE_FAST:
290 default:
291 cntl |= IC_CON_SPD_FS;
Simon Glass245ec0b2020-01-23 11:48:13 -0700292 writel(config.scl_hcnt, &i2c_base->ic_fs_scl_hcnt);
293 writel(config.scl_lcnt, &i2c_base->ic_fs_scl_lcnt);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530294 break;
295 }
296
Stefan Roeseef6073e2014-10-28 12:12:00 +0100297 writel(cntl, &i2c_base->ic_con);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530298
Stefan Roese38481202016-04-21 08:19:42 +0200299 /* Configure SDA Hold Time if required */
Simon Glass245ec0b2020-01-23 11:48:13 -0700300 if (config.sda_hold)
301 writel(config.sda_hold, &i2c_base->ic_sda_hold);
Stefan Roese38481202016-04-21 08:19:42 +0200302
Jun Chend003a372019-06-05 15:23:16 +0800303 /* Restore back i2c now speed set */
304 if (ena == IC_ENABLE_0B)
305 dw_i2c_enable(i2c_base, true);
Stefan Roesef6322ebd2012-01-20 11:52:33 +0100306
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530307 return 0;
308}
309
310/*
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530311 * i2c_setaddress - Sets the target slave address
312 * @i2c_addr: target i2c address
313 *
314 * Sets the target slave address.
315 */
Stefan Roese41de7662016-04-21 08:19:40 +0200316static void i2c_setaddress(struct i2c_regs *i2c_base, unsigned int i2c_addr)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530317{
Alexey Brodkin41c56552013-11-07 17:52:18 +0400318 /* Disable i2c */
Stefan Roese3bc33ba2016-04-21 08:19:38 +0200319 dw_i2c_enable(i2c_base, false);
Alexey Brodkin41c56552013-11-07 17:52:18 +0400320
Stefan Roeseef6073e2014-10-28 12:12:00 +0100321 writel(i2c_addr, &i2c_base->ic_tar);
Alexey Brodkin41c56552013-11-07 17:52:18 +0400322
323 /* Enable i2c */
Stefan Roese3bc33ba2016-04-21 08:19:38 +0200324 dw_i2c_enable(i2c_base, true);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530325}
326
327/*
328 * i2c_flush_rxfifo - Flushes the i2c RX FIFO
329 *
330 * Flushes the i2c RX FIFO
331 */
Stefan Roese41de7662016-04-21 08:19:40 +0200332static void i2c_flush_rxfifo(struct i2c_regs *i2c_base)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530333{
Stefan Roeseef6073e2014-10-28 12:12:00 +0100334 while (readl(&i2c_base->ic_status) & IC_STATUS_RFNE)
335 readl(&i2c_base->ic_cmd_data);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530336}
337
338/*
339 * i2c_wait_for_bb - Waits for bus busy
340 *
341 * Waits for bus busy
342 */
Stefan Roese41de7662016-04-21 08:19:40 +0200343static int i2c_wait_for_bb(struct i2c_regs *i2c_base)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530344{
345 unsigned long start_time_bb = get_timer(0);
346
Stefan Roeseef6073e2014-10-28 12:12:00 +0100347 while ((readl(&i2c_base->ic_status) & IC_STATUS_MA) ||
348 !(readl(&i2c_base->ic_status) & IC_STATUS_TFE)) {
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530349
350 /* Evaluate timeout */
351 if (get_timer(start_time_bb) > (unsigned long)(I2C_BYTE_TO_BB))
352 return 1;
353 }
354
355 return 0;
356}
357
Stefan Roese41de7662016-04-21 08:19:40 +0200358static int i2c_xfer_init(struct i2c_regs *i2c_base, uchar chip, uint addr,
Stefan Roeseef6073e2014-10-28 12:12:00 +0100359 int alen)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530360{
Stefan Roese41de7662016-04-21 08:19:40 +0200361 if (i2c_wait_for_bb(i2c_base))
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530362 return 1;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530363
Stefan Roese41de7662016-04-21 08:19:40 +0200364 i2c_setaddress(i2c_base, chip);
Chin Liang Seea0c26262014-02-04 11:56:23 -0600365 while (alen) {
366 alen--;
367 /* high byte address going out first */
368 writel((addr >> (alen * 8)) & 0xff,
Stefan Roeseef6073e2014-10-28 12:12:00 +0100369 &i2c_base->ic_cmd_data);
Chin Liang Seea0c26262014-02-04 11:56:23 -0600370 }
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530371 return 0;
372}
373
Stefan Roese41de7662016-04-21 08:19:40 +0200374static int i2c_xfer_finish(struct i2c_regs *i2c_base)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530375{
376 ulong start_stop_det = get_timer(0);
377
378 while (1) {
Stefan Roeseef6073e2014-10-28 12:12:00 +0100379 if ((readl(&i2c_base->ic_raw_intr_stat) & IC_STOP_DET)) {
380 readl(&i2c_base->ic_clr_stop_det);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530381 break;
382 } else if (get_timer(start_stop_det) > I2C_STOPDET_TO) {
383 break;
384 }
385 }
386
Stefan Roese41de7662016-04-21 08:19:40 +0200387 if (i2c_wait_for_bb(i2c_base)) {
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530388 printf("Timed out waiting for bus\n");
389 return 1;
390 }
391
Stefan Roese41de7662016-04-21 08:19:40 +0200392 i2c_flush_rxfifo(i2c_base);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530393
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530394 return 0;
395}
396
397/*
398 * i2c_read - Read from i2c memory
399 * @chip: target i2c address
400 * @addr: address to read from
401 * @alen:
402 * @buffer: buffer for read data
403 * @len: no of bytes to be read
404 *
405 * Read from i2c memory.
406 */
Stefan Roese41de7662016-04-21 08:19:40 +0200407static int __dw_i2c_read(struct i2c_regs *i2c_base, u8 dev, uint addr,
408 int alen, u8 *buffer, int len)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530409{
410 unsigned long start_time_rx;
Marek Vasutc4bc9a82016-10-20 16:48:28 +0200411 unsigned int active = 0;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530412
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400413#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
414 /*
415 * EEPROM chips that implement "address overflow" are ones
416 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
417 * address and the extra bits end up in the "chip address"
418 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
419 * four 256 byte chips.
420 *
421 * Note that we consider the length of the address field to
422 * still be one byte because the extra address bits are
423 * hidden in the chip address.
424 */
Stefan Roeseef6073e2014-10-28 12:12:00 +0100425 dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400426 addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
427
Stefan Roeseef6073e2014-10-28 12:12:00 +0100428 debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400429 addr);
430#endif
431
Stefan Roese41de7662016-04-21 08:19:40 +0200432 if (i2c_xfer_init(i2c_base, dev, addr, alen))
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530433 return 1;
434
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530435 start_time_rx = get_timer(0);
436 while (len) {
Marek Vasutc4bc9a82016-10-20 16:48:28 +0200437 if (!active) {
438 /*
439 * Avoid writing to ic_cmd_data multiple times
440 * in case this loop spins too quickly and the
441 * ic_status RFNE bit isn't set after the first
442 * write. Subsequent writes to ic_cmd_data can
443 * trigger spurious i2c transfer.
444 */
445 if (len == 1)
446 writel(IC_CMD | IC_STOP, &i2c_base->ic_cmd_data);
447 else
448 writel(IC_CMD, &i2c_base->ic_cmd_data);
449 active = 1;
450 }
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530451
Stefan Roeseef6073e2014-10-28 12:12:00 +0100452 if (readl(&i2c_base->ic_status) & IC_STATUS_RFNE) {
453 *buffer++ = (uchar)readl(&i2c_base->ic_cmd_data);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530454 len--;
455 start_time_rx = get_timer(0);
Marek Vasutc4bc9a82016-10-20 16:48:28 +0200456 active = 0;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530457 } else if (get_timer(start_time_rx) > I2C_BYTE_TO) {
Marek Vasutc4bc9a82016-10-20 16:48:28 +0200458 return 1;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530459 }
460 }
461
Stefan Roese41de7662016-04-21 08:19:40 +0200462 return i2c_xfer_finish(i2c_base);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530463}
464
465/*
466 * i2c_write - Write to i2c memory
467 * @chip: target i2c address
468 * @addr: address to read from
469 * @alen:
470 * @buffer: buffer for read data
471 * @len: no of bytes to be read
472 *
473 * Write to i2c memory.
474 */
Stefan Roese41de7662016-04-21 08:19:40 +0200475static int __dw_i2c_write(struct i2c_regs *i2c_base, u8 dev, uint addr,
476 int alen, u8 *buffer, int len)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530477{
478 int nb = len;
479 unsigned long start_time_tx;
480
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400481#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
482 /*
483 * EEPROM chips that implement "address overflow" are ones
484 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
485 * address and the extra bits end up in the "chip address"
486 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
487 * four 256 byte chips.
488 *
489 * Note that we consider the length of the address field to
490 * still be one byte because the extra address bits are
491 * hidden in the chip address.
492 */
Stefan Roeseef6073e2014-10-28 12:12:00 +0100493 dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400494 addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
495
Stefan Roeseef6073e2014-10-28 12:12:00 +0100496 debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400497 addr);
498#endif
499
Stefan Roese41de7662016-04-21 08:19:40 +0200500 if (i2c_xfer_init(i2c_base, dev, addr, alen))
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530501 return 1;
502
503 start_time_tx = get_timer(0);
504 while (len) {
Stefan Roeseef6073e2014-10-28 12:12:00 +0100505 if (readl(&i2c_base->ic_status) & IC_STATUS_TFNF) {
506 if (--len == 0) {
507 writel(*buffer | IC_STOP,
508 &i2c_base->ic_cmd_data);
509 } else {
510 writel(*buffer, &i2c_base->ic_cmd_data);
511 }
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530512 buffer++;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530513 start_time_tx = get_timer(0);
514
515 } else if (get_timer(start_time_tx) > (nb * I2C_BYTE_TO)) {
516 printf("Timed out. i2c write Failed\n");
517 return 1;
518 }
519 }
520
Stefan Roese41de7662016-04-21 08:19:40 +0200521 return i2c_xfer_finish(i2c_base);
522}
523
Stefan Roese3cb27962016-04-21 08:19:41 +0200524/*
525 * __dw_i2c_init - Init function
526 * @speed: required i2c speed
527 * @slaveaddr: slave address for the device
528 *
529 * Initialization function.
530 */
Simon Glassbd9ca8d2019-02-16 20:24:39 -0700531static int __dw_i2c_init(struct i2c_regs *i2c_base, int speed, int slaveaddr)
Stefan Roese3cb27962016-04-21 08:19:41 +0200532{
Simon Glassbd9ca8d2019-02-16 20:24:39 -0700533 int ret;
534
Stefan Roese3cb27962016-04-21 08:19:41 +0200535 /* Disable i2c */
Simon Glassbd9ca8d2019-02-16 20:24:39 -0700536 ret = dw_i2c_enable(i2c_base, false);
537 if (ret)
538 return ret;
Stefan Roese3cb27962016-04-21 08:19:41 +0200539
Marek Vasut808aa132017-08-07 20:45:31 +0200540 writel(IC_CON_SD | IC_CON_RE | IC_CON_SPD_FS | IC_CON_MM,
541 &i2c_base->ic_con);
Stefan Roese3cb27962016-04-21 08:19:41 +0200542 writel(IC_RX_TL, &i2c_base->ic_rx_tl);
543 writel(IC_TX_TL, &i2c_base->ic_tx_tl);
544 writel(IC_STOP_DET, &i2c_base->ic_intr_mask);
545#ifndef CONFIG_DM_I2C
Simon Glass60e0c3a2020-01-23 11:48:12 -0700546 __dw_i2c_set_bus_speed(NULL, i2c_base, speed, IC_CLK);
Stefan Roese3cb27962016-04-21 08:19:41 +0200547 writel(slaveaddr, &i2c_base->ic_sar);
548#endif
549
550 /* Enable i2c */
Simon Glassbd9ca8d2019-02-16 20:24:39 -0700551 ret = dw_i2c_enable(i2c_base, true);
552 if (ret)
553 return ret;
554
555 return 0;
Stefan Roese3cb27962016-04-21 08:19:41 +0200556}
557
558#ifndef CONFIG_DM_I2C
559/*
560 * The legacy I2C functions. These need to get removed once
561 * all users of this driver are converted to DM.
562 */
Stefan Roese41de7662016-04-21 08:19:40 +0200563static struct i2c_regs *i2c_get_base(struct i2c_adapter *adap)
564{
565 switch (adap->hwadapnr) {
566#if CONFIG_SYS_I2C_BUS_MAX >= 4
567 case 3:
568 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE3;
569#endif
570#if CONFIG_SYS_I2C_BUS_MAX >= 3
571 case 2:
572 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE2;
573#endif
574#if CONFIG_SYS_I2C_BUS_MAX >= 2
575 case 1:
576 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE1;
577#endif
578 case 0:
579 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE;
580 default:
581 printf("Wrong I2C-adapter number %d\n", adap->hwadapnr);
582 }
583
584 return NULL;
585}
586
587static unsigned int dw_i2c_set_bus_speed(struct i2c_adapter *adap,
588 unsigned int speed)
589{
590 adap->speed = speed;
Simon Glass60e0c3a2020-01-23 11:48:12 -0700591 return __dw_i2c_set_bus_speed(NULL, i2c_get_base(adap), speed, IC_CLK);
Stefan Roese41de7662016-04-21 08:19:40 +0200592}
593
Stefan Roese3cb27962016-04-21 08:19:41 +0200594static void dw_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
Stefan Roese41de7662016-04-21 08:19:40 +0200595{
Stefan Roese3cb27962016-04-21 08:19:41 +0200596 __dw_i2c_init(i2c_get_base(adap), speed, slaveaddr);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530597}
598
Stefan Roese41de7662016-04-21 08:19:40 +0200599static int dw_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr,
600 int alen, u8 *buffer, int len)
601{
602 return __dw_i2c_read(i2c_get_base(adap), dev, addr, alen, buffer, len);
603}
604
605static int dw_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr,
606 int alen, u8 *buffer, int len)
607{
608 return __dw_i2c_write(i2c_get_base(adap), dev, addr, alen, buffer, len);
609}
610
Stefan Roese3cb27962016-04-21 08:19:41 +0200611/* dw_i2c_probe - Probe the i2c chip */
Stefan Roeseef6073e2014-10-28 12:12:00 +0100612static int dw_i2c_probe(struct i2c_adapter *adap, u8 dev)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530613{
Stefan Roese41de7662016-04-21 08:19:40 +0200614 struct i2c_regs *i2c_base = i2c_get_base(adap);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530615 u32 tmp;
Stefan Roesef6322ebd2012-01-20 11:52:33 +0100616 int ret;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530617
618 /*
619 * Try to read the first location of the chip.
620 */
Stefan Roese41de7662016-04-21 08:19:40 +0200621 ret = __dw_i2c_read(i2c_base, dev, 0, 1, (uchar *)&tmp, 1);
Stefan Roesef6322ebd2012-01-20 11:52:33 +0100622 if (ret)
Stefan Roeseef6073e2014-10-28 12:12:00 +0100623 dw_i2c_init(adap, adap->speed, adap->slaveaddr);
Stefan Roesef6322ebd2012-01-20 11:52:33 +0100624
625 return ret;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530626}
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000627
Stefan Roeseef6073e2014-10-28 12:12:00 +0100628U_BOOT_I2C_ADAP_COMPLETE(dw_0, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
629 dw_i2c_write, dw_i2c_set_bus_speed,
630 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0)
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000631
Stefan Roeseef6073e2014-10-28 12:12:00 +0100632#if CONFIG_SYS_I2C_BUS_MAX >= 2
633U_BOOT_I2C_ADAP_COMPLETE(dw_1, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
634 dw_i2c_write, dw_i2c_set_bus_speed,
635 CONFIG_SYS_I2C_SPEED1, CONFIG_SYS_I2C_SLAVE1, 1)
636#endif
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000637
Stefan Roeseef6073e2014-10-28 12:12:00 +0100638#if CONFIG_SYS_I2C_BUS_MAX >= 3
639U_BOOT_I2C_ADAP_COMPLETE(dw_2, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
640 dw_i2c_write, dw_i2c_set_bus_speed,
641 CONFIG_SYS_I2C_SPEED2, CONFIG_SYS_I2C_SLAVE2, 2)
642#endif
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000643
Stefan Roeseef6073e2014-10-28 12:12:00 +0100644#if CONFIG_SYS_I2C_BUS_MAX >= 4
645U_BOOT_I2C_ADAP_COMPLETE(dw_3, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
646 dw_i2c_write, dw_i2c_set_bus_speed,
647 CONFIG_SYS_I2C_SPEED3, CONFIG_SYS_I2C_SLAVE3, 3)
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000648#endif
Stefan Roese3cb27962016-04-21 08:19:41 +0200649
650#else /* CONFIG_DM_I2C */
651/* The DM I2C functions */
652
653static int designware_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
654 int nmsgs)
655{
656 struct dw_i2c *i2c = dev_get_priv(bus);
657 int ret;
658
659 debug("i2c_xfer: %d messages\n", nmsgs);
660 for (; nmsgs > 0; nmsgs--, msg++) {
661 debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
662 if (msg->flags & I2C_M_RD) {
663 ret = __dw_i2c_read(i2c->regs, msg->addr, 0, 0,
664 msg->buf, msg->len);
665 } else {
666 ret = __dw_i2c_write(i2c->regs, msg->addr, 0, 0,
667 msg->buf, msg->len);
668 }
669 if (ret) {
670 debug("i2c_write: error sending\n");
671 return -EREMOTEIO;
672 }
673 }
674
675 return 0;
676}
677
678static int designware_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
679{
680 struct dw_i2c *i2c = dev_get_priv(bus);
Ley Foon Tan6e85c812019-06-12 09:48:04 +0800681 ulong rate;
682
683#if CONFIG_IS_ENABLED(CLK)
684 rate = clk_get_rate(&i2c->clk);
685 if (IS_ERR_VALUE(rate))
686 return -EINVAL;
Ley Foon Tan6e85c812019-06-12 09:48:04 +0800687#else
688 rate = IC_CLK;
689#endif
Simon Glass60e0c3a2020-01-23 11:48:12 -0700690 return __dw_i2c_set_bus_speed(i2c, i2c->regs, speed, rate);
Stefan Roese3cb27962016-04-21 08:19:41 +0200691}
692
693static int designware_i2c_probe_chip(struct udevice *bus, uint chip_addr,
694 uint chip_flags)
695{
696 struct dw_i2c *i2c = dev_get_priv(bus);
697 struct i2c_regs *i2c_base = i2c->regs;
698 u32 tmp;
699 int ret;
700
701 /* Try to read the first location of the chip */
702 ret = __dw_i2c_read(i2c_base, chip_addr, 0, 1, (uchar *)&tmp, 1);
703 if (ret)
704 __dw_i2c_init(i2c_base, 0, 0);
705
706 return ret;
707}
708
Simon Glass9e5d1742020-01-23 11:48:11 -0700709int designware_i2c_ofdata_to_platdata(struct udevice *bus)
Stefan Roese3cb27962016-04-21 08:19:41 +0200710{
711 struct dw_i2c *priv = dev_get_priv(bus);
712
Simon Glass9e5d1742020-01-23 11:48:11 -0700713 if (!priv->regs)
714 priv->regs = (struct i2c_regs *)devfdt_get_addr_ptr(bus);
715 dev_read_u32(bus, "i2c-scl-rising-time-ns", &priv->scl_rise_time_ns);
716 dev_read_u32(bus, "i2c-scl-falling-time-ns", &priv->scl_fall_time_ns);
717 dev_read_u32(bus, "i2c-sda-hold-time-ns", &priv->sda_hold_time_ns);
Simon Glasse2be5532019-12-06 21:41:40 -0700718
719 return 0;
720}
721
722int designware_i2c_probe(struct udevice *bus)
723{
724 struct dw_i2c *priv = dev_get_priv(bus);
725 int ret;
Stefan Roese3cb27962016-04-21 08:19:41 +0200726
Simon Goldschmidt28608a12019-03-28 21:11:48 +0100727 ret = reset_get_bulk(bus, &priv->resets);
Dinh Nguyen08794aa2018-04-04 17:18:24 -0500728 if (ret)
Simon Goldschmidt28608a12019-03-28 21:11:48 +0100729 dev_warn(bus, "Can't get reset: %d\n", ret);
730 else
731 reset_deassert_bulk(&priv->resets);
Dinh Nguyen08794aa2018-04-04 17:18:24 -0500732
Ley Foon Tan6e85c812019-06-12 09:48:04 +0800733#if CONFIG_IS_ENABLED(CLK)
734 ret = clk_get_by_index(bus, 0, &priv->clk);
735 if (ret)
736 return ret;
737
738 ret = clk_enable(&priv->clk);
739 if (ret && ret != -ENOSYS && ret != -ENOTSUPP) {
740 clk_free(&priv->clk);
741 dev_err(bus, "failed to enable clock\n");
742 return ret;
743 }
744#endif
745
Simon Glassbd9ca8d2019-02-16 20:24:39 -0700746 return __dw_i2c_init(priv->regs, 0, 0);
Stefan Roese3cb27962016-04-21 08:19:41 +0200747}
748
Simon Glasse2be5532019-12-06 21:41:40 -0700749int designware_i2c_remove(struct udevice *dev)
Simon Goldschmidt28608a12019-03-28 21:11:48 +0100750{
751 struct dw_i2c *priv = dev_get_priv(dev);
752
Ley Foon Tan6e85c812019-06-12 09:48:04 +0800753#if CONFIG_IS_ENABLED(CLK)
754 clk_disable(&priv->clk);
755 clk_free(&priv->clk);
756#endif
757
Simon Goldschmidt28608a12019-03-28 21:11:48 +0100758 return reset_release_bulk(&priv->resets);
759}
760
Simon Glasse2be5532019-12-06 21:41:40 -0700761const struct dm_i2c_ops designware_i2c_ops = {
Stefan Roese3cb27962016-04-21 08:19:41 +0200762 .xfer = designware_i2c_xfer,
763 .probe_chip = designware_i2c_probe_chip,
764 .set_bus_speed = designware_i2c_set_bus_speed,
765};
766
767static const struct udevice_id designware_i2c_ids[] = {
768 { .compatible = "snps,designware-i2c" },
769 { }
770};
771
772U_BOOT_DRIVER(i2c_designware) = {
773 .name = "i2c_designware",
774 .id = UCLASS_I2C,
775 .of_match = designware_i2c_ids,
Simon Glasse2be5532019-12-06 21:41:40 -0700776 .ofdata_to_platdata = designware_i2c_ofdata_to_platdata,
Stefan Roese3cb27962016-04-21 08:19:41 +0200777 .probe = designware_i2c_probe,
778 .priv_auto_alloc_size = sizeof(struct dw_i2c),
Simon Goldschmidt28608a12019-03-28 21:11:48 +0100779 .remove = designware_i2c_remove,
Simon Glasse2be5532019-12-06 21:41:40 -0700780 .flags = DM_FLAG_OS_PREPARE,
Stefan Roese3cb27962016-04-21 08:19:41 +0200781 .ops = &designware_i2c_ops,
782};
783
784#endif /* CONFIG_DM_I2C */