blob: 74aef7744caa45397b41ab6da139f8802a4808b8 [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>
Simon Glass9bc15642020-02-03 07:36:16 -070011#include <malloc.h>
Stefan Roese38481202016-04-21 08:19:42 +020012#include <pci.h>
Dinh Nguyen08794aa2018-04-04 17:18:24 -050013#include <reset.h>
Vipin KUMARfc9589f2010-01-15 19:15:44 +053014#include <asm/io.h>
Vipin KUMAR3f64acb2012-02-26 23:13:29 +000015#include "designware_i2c.h"
Simon Glass9bc15642020-02-03 07:36:16 -070016#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070017#include <linux/err.h>
Vipin KUMARfc9589f2010-01-15 19:15:44 +053018
Stefan Roeseabb3e132016-04-27 09:02:12 +020019#ifdef CONFIG_SYS_I2C_DW_ENABLE_STATUS_UNSUPPORTED
Simon Glassbd9ca8d2019-02-16 20:24:39 -070020static int dw_i2c_enable(struct i2c_regs *i2c_base, bool enable)
Stefan Roese3bc33ba2016-04-21 08:19:38 +020021{
22 u32 ena = enable ? IC_ENABLE_0B : 0;
Stefan Roeseabb3e132016-04-27 09:02:12 +020023
24 writel(ena, &i2c_base->ic_enable);
Simon Glassbd9ca8d2019-02-16 20:24:39 -070025
26 return 0;
Stefan Roeseabb3e132016-04-27 09:02:12 +020027}
28#else
Simon Glassbd9ca8d2019-02-16 20:24:39 -070029static int dw_i2c_enable(struct i2c_regs *i2c_base, bool enable)
Stefan Roeseabb3e132016-04-27 09:02:12 +020030{
31 u32 ena = enable ? IC_ENABLE_0B : 0;
Stefan Roese3bc33ba2016-04-21 08:19:38 +020032 int timeout = 100;
33
34 do {
35 writel(ena, &i2c_base->ic_enable);
36 if ((readl(&i2c_base->ic_enable_status) & IC_ENABLE_0B) == ena)
Simon Glassbd9ca8d2019-02-16 20:24:39 -070037 return 0;
Stefan Roese3bc33ba2016-04-21 08:19:38 +020038
39 /*
40 * Wait 10 times the signaling period of the highest I2C
41 * transfer supported by the driver (for 400KHz this is
42 * 25us) as described in the DesignWare I2C databook.
43 */
44 udelay(25);
45 } while (timeout--);
Stefan Roese3bc33ba2016-04-21 08:19:38 +020046 printf("timeout in %sabling I2C adapter\n", enable ? "en" : "dis");
Simon Glassbd9ca8d2019-02-16 20:24:39 -070047
48 return -ETIMEDOUT;
Stefan Roese3bc33ba2016-04-21 08:19:38 +020049}
Stefan Roeseabb3e132016-04-27 09:02:12 +020050#endif
Stefan Roese3bc33ba2016-04-21 08:19:38 +020051
Simon Glassc7181102020-01-23 11:48:14 -070052/* High and low times in different speed modes (in ns) */
53enum {
54 /* SDA Hold Time */
55 DEFAULT_SDA_HOLD_TIME = 300,
56};
57
58/**
59 * calc_counts() - Convert a period to a number of IC clk cycles
60 *
61 * @ic_clk: Input clock in Hz
62 * @period_ns: Period to represent, in ns
63 * @return calculated count
64 */
65static uint calc_counts(uint ic_clk, uint period_ns)
66{
67 return DIV_ROUND_UP(ic_clk / 1000 * period_ns, NANO_TO_KILO);
68}
69
70/**
71 * struct i2c_mode_info - Information about an I2C speed mode
72 *
73 * Each speed mode has its own characteristics. This struct holds these to aid
74 * calculations in dw_i2c_calc_timing().
75 *
76 * @speed: Speed in Hz
77 * @min_scl_lowtime_ns: Minimum value for SCL low period in ns
78 * @min_scl_hightime_ns: Minimum value for SCL high period in ns
79 * @def_rise_time_ns: Default rise time in ns
80 * @def_fall_time_ns: Default fall time in ns
81 */
82struct i2c_mode_info {
83 int speed;
84 int min_scl_hightime_ns;
85 int min_scl_lowtime_ns;
86 int def_rise_time_ns;
87 int def_fall_time_ns;
88};
89
90static const struct i2c_mode_info info_for_mode[] = {
91 [IC_SPEED_MODE_STANDARD] = {
Simon Glassac77bae2020-01-23 11:48:18 -070092 I2C_SPEED_STANDARD_RATE,
Simon Glassc7181102020-01-23 11:48:14 -070093 MIN_SS_SCL_HIGHTIME,
94 MIN_SS_SCL_LOWTIME,
95 1000,
96 300,
97 },
98 [IC_SPEED_MODE_FAST] = {
Simon Glassac77bae2020-01-23 11:48:18 -070099 I2C_SPEED_FAST_RATE,
Simon Glassc7181102020-01-23 11:48:14 -0700100 MIN_FS_SCL_HIGHTIME,
101 MIN_FS_SCL_LOWTIME,
102 300,
103 300,
104 },
Simon Glass45649222020-01-23 11:48:23 -0700105 [IC_SPEED_MODE_FAST_PLUS] = {
106 I2C_SPEED_FAST_PLUS_RATE,
107 MIN_FP_SCL_HIGHTIME,
108 MIN_FP_SCL_LOWTIME,
109 260,
110 500,
111 },
Simon Glassc7181102020-01-23 11:48:14 -0700112 [IC_SPEED_MODE_HIGH] = {
Simon Glassac77bae2020-01-23 11:48:18 -0700113 I2C_SPEED_HIGH_RATE,
Simon Glassc7181102020-01-23 11:48:14 -0700114 MIN_HS_SCL_HIGHTIME,
115 MIN_HS_SCL_LOWTIME,
116 120,
117 120,
118 },
119};
120
121/**
122 * dw_i2c_calc_timing() - Calculate the timings to use for a bus
123 *
124 * @priv: Bus private information (NULL if not using driver model)
125 * @mode: Speed mode to use
126 * @ic_clk: IC clock speed in Hz
127 * @spk_cnt: Spike-suppression count
128 * @config: Returns value to use
129 * @return 0 if OK, -EINVAL if the calculation failed due to invalid data
130 */
131static int dw_i2c_calc_timing(struct dw_i2c *priv, enum i2c_speed_mode mode,
132 int ic_clk, int spk_cnt,
133 struct dw_i2c_speed_config *config)
134{
135 int fall_cnt, rise_cnt, min_tlow_cnt, min_thigh_cnt;
136 int hcnt, lcnt, period_cnt, diff, tot;
137 int sda_hold_time_ns, scl_rise_time_ns, scl_fall_time_ns;
138 const struct i2c_mode_info *info;
139
140 /*
141 * Find the period, rise, fall, min tlow, and min thigh in terms of
142 * counts of the IC clock
143 */
144 info = &info_for_mode[mode];
145 period_cnt = ic_clk / info->speed;
146 scl_rise_time_ns = priv && priv->scl_rise_time_ns ?
147 priv->scl_rise_time_ns : info->def_rise_time_ns;
148 scl_fall_time_ns = priv && priv->scl_fall_time_ns ?
149 priv->scl_fall_time_ns : info->def_fall_time_ns;
150 rise_cnt = calc_counts(ic_clk, scl_rise_time_ns);
151 fall_cnt = calc_counts(ic_clk, scl_fall_time_ns);
152 min_tlow_cnt = calc_counts(ic_clk, info->min_scl_lowtime_ns);
153 min_thigh_cnt = calc_counts(ic_clk, info->min_scl_hightime_ns);
154
155 debug("dw_i2c: period %d rise %d fall %d tlow %d thigh %d spk %d\n",
156 period_cnt, rise_cnt, fall_cnt, min_tlow_cnt, min_thigh_cnt,
157 spk_cnt);
158
159 /*
160 * Back-solve for hcnt and lcnt according to the following equations:
161 * SCL_High_time = [(HCNT + IC_*_SPKLEN + 7) * ic_clk] + SCL_Fall_time
162 * SCL_Low_time = [(LCNT + 1) * ic_clk] - SCL_Fall_time + SCL_Rise_time
163 */
164 hcnt = min_thigh_cnt - fall_cnt - 7 - spk_cnt;
165 lcnt = min_tlow_cnt - rise_cnt + fall_cnt - 1;
166
167 if (hcnt < 0 || lcnt < 0) {
168 debug("dw_i2c: bad counts. hcnt = %d lcnt = %d\n", hcnt, lcnt);
169 return -EINVAL;
170 }
171
172 /*
173 * Now add things back up to ensure the period is hit. If it is off,
174 * split the difference and bias to lcnt for remainder
175 */
176 tot = hcnt + lcnt + 7 + spk_cnt + rise_cnt + 1;
177
178 if (tot < period_cnt) {
179 diff = (period_cnt - tot) / 2;
180 hcnt += diff;
181 lcnt += diff;
182 tot = hcnt + lcnt + 7 + spk_cnt + rise_cnt + 1;
183 lcnt += period_cnt - tot;
184 }
185
186 config->scl_lcnt = lcnt;
187 config->scl_hcnt = hcnt;
188
189 /* Use internal default unless other value is specified */
190 sda_hold_time_ns = priv && priv->sda_hold_time_ns ?
191 priv->sda_hold_time_ns : DEFAULT_SDA_HOLD_TIME;
192 config->sda_hold = calc_counts(ic_clk, sda_hold_time_ns);
193
194 debug("dw_i2c: hcnt = %d lcnt = %d sda hold = %d\n", hcnt, lcnt,
195 config->sda_hold);
196
197 return 0;
198}
199
Simon Glassc5294192020-01-23 11:48:25 -0700200static int calc_bus_speed(struct dw_i2c *priv, int speed, ulong bus_clk,
201 struct dw_i2c_speed_config *config)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530202{
Simon Glass60e0c3a2020-01-23 11:48:12 -0700203 const struct dw_scl_sda_cfg *scl_sda_cfg = NULL;
Simon Glassc5294192020-01-23 11:48:25 -0700204 struct i2c_regs *regs = priv->regs;
Simon Glass6ed44ae2020-01-23 11:48:08 -0700205 enum i2c_speed_mode i2c_spd;
Jun Chenef6677e2020-03-02 16:58:55 +0800206 u32 comp_param1;
Simon Glassc38e2b32020-01-23 11:48:15 -0700207 int spk_cnt;
Simon Glassc7181102020-01-23 11:48:14 -0700208 int ret;
Stefan Roese88893c92016-04-21 08:19:39 +0200209
Jun Chenef6677e2020-03-02 16:58:55 +0800210 comp_param1 = readl(&regs->comp_param1);
211
Simon Glass60e0c3a2020-01-23 11:48:12 -0700212 if (priv)
213 scl_sda_cfg = priv->scl_sda_cfg;
Simon Glassf5ef1012020-01-23 11:48:07 -0700214 /* Allow high speed if there is no config, or the config allows it */
Jun Chen3ce27d42020-03-02 16:58:56 +0800215 if (speed >= I2C_SPEED_HIGH_RATE)
Simon Glassf5ef1012020-01-23 11:48:07 -0700216 i2c_spd = IC_SPEED_MODE_HIGH;
Simon Glass45649222020-01-23 11:48:23 -0700217 else if (speed >= I2C_SPEED_FAST_PLUS_RATE)
Simon Glass55397682020-02-13 13:24:55 -0700218 i2c_spd = IC_SPEED_MODE_FAST_PLUS;
219 else if (speed >= I2C_SPEED_FAST_RATE)
Stefan Roese88893c92016-04-21 08:19:39 +0200220 i2c_spd = IC_SPEED_MODE_FAST;
221 else
222 i2c_spd = IC_SPEED_MODE_STANDARD;
Armando Visconti631e6932012-03-29 20:10:17 +0000223
Jun Chenef6677e2020-03-02 16:58:55 +0800224 /* Check is high speed possible and fall back to fast mode if not */
225 if (i2c_spd == IC_SPEED_MODE_HIGH) {
226 if ((comp_param1 & DW_IC_COMP_PARAM_1_SPEED_MODE_MASK)
227 != DW_IC_COMP_PARAM_1_SPEED_MODE_HIGH)
228 i2c_spd = IC_SPEED_MODE_FAST;
229 }
230
Simon Glassc38e2b32020-01-23 11:48:15 -0700231 /* Get the proper spike-suppression count based on target speed */
232 if (!priv || !priv->has_spk_cnt)
233 spk_cnt = 0;
234 else if (i2c_spd >= IC_SPEED_MODE_HIGH)
Simon Glassc5294192020-01-23 11:48:25 -0700235 spk_cnt = readl(&regs->hs_spklen);
Simon Glassc38e2b32020-01-23 11:48:15 -0700236 else
Simon Glassc5294192020-01-23 11:48:25 -0700237 spk_cnt = readl(&regs->fs_spklen);
Simon Glass245ec0b2020-01-23 11:48:13 -0700238 if (scl_sda_cfg) {
Simon Glassc5294192020-01-23 11:48:25 -0700239 config->sda_hold = scl_sda_cfg->sda_hold;
Simon Glass245ec0b2020-01-23 11:48:13 -0700240 if (i2c_spd == IC_SPEED_MODE_STANDARD) {
Simon Glassc5294192020-01-23 11:48:25 -0700241 config->scl_hcnt = scl_sda_cfg->ss_hcnt;
242 config->scl_lcnt = scl_sda_cfg->ss_lcnt;
Simon Glass245ec0b2020-01-23 11:48:13 -0700243 } else {
Simon Glassc5294192020-01-23 11:48:25 -0700244 config->scl_hcnt = scl_sda_cfg->fs_hcnt;
245 config->scl_lcnt = scl_sda_cfg->fs_lcnt;
Simon Glass245ec0b2020-01-23 11:48:13 -0700246 }
Simon Glassc7181102020-01-23 11:48:14 -0700247 } else {
Simon Glassc38e2b32020-01-23 11:48:15 -0700248 ret = dw_i2c_calc_timing(priv, i2c_spd, bus_clk, spk_cnt,
Simon Glassc5294192020-01-23 11:48:25 -0700249 config);
Simon Glassc7181102020-01-23 11:48:14 -0700250 if (ret)
251 return log_msg_ret("gen_confg", ret);
Simon Glass245ec0b2020-01-23 11:48:13 -0700252 }
Simon Glassc5294192020-01-23 11:48:25 -0700253 config->speed_mode = i2c_spd;
254
255 return 0;
256}
257
258/*
259 * _dw_i2c_set_bus_speed - Set the i2c speed
260 * @speed: required i2c speed
261 *
262 * Set the i2c speed.
263 */
264static int _dw_i2c_set_bus_speed(struct dw_i2c *priv, struct i2c_regs *i2c_base,
265 unsigned int speed, unsigned int bus_clk)
266{
267 struct dw_i2c_speed_config config;
268 unsigned int cntl;
269 unsigned int ena;
270 int ret;
271
272 ret = calc_bus_speed(priv, speed, bus_clk, &config);
273 if (ret)
274 return ret;
275
276 /* Get enable setting for restore later */
277 ena = readl(&i2c_base->ic_enable) & IC_ENABLE_0B;
278
279 /* to set speed cltr must be disabled */
280 dw_i2c_enable(i2c_base, false);
281
282 cntl = (readl(&i2c_base->ic_con) & (~IC_CON_SPD_MSK));
Simon Glass245ec0b2020-01-23 11:48:13 -0700283
Simon Glassc5294192020-01-23 11:48:25 -0700284 switch (config.speed_mode) {
Simon Glassf5ef1012020-01-23 11:48:07 -0700285 case IC_SPEED_MODE_HIGH:
Jun Chen635cf512020-03-02 16:58:54 +0800286 cntl |= IC_CON_SPD_HS;
Simon Glass245ec0b2020-01-23 11:48:13 -0700287 writel(config.scl_hcnt, &i2c_base->ic_hs_scl_hcnt);
288 writel(config.scl_lcnt, &i2c_base->ic_hs_scl_lcnt);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530289 break;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530290 case IC_SPEED_MODE_STANDARD:
291 cntl |= IC_CON_SPD_SS;
Simon Glass245ec0b2020-01-23 11:48:13 -0700292 writel(config.scl_hcnt, &i2c_base->ic_ss_scl_hcnt);
293 writel(config.scl_lcnt, &i2c_base->ic_ss_scl_lcnt);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530294 break;
Simon Glass45649222020-01-23 11:48:23 -0700295 case IC_SPEED_MODE_FAST_PLUS:
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530296 case IC_SPEED_MODE_FAST:
297 default:
298 cntl |= IC_CON_SPD_FS;
Simon Glass245ec0b2020-01-23 11:48:13 -0700299 writel(config.scl_hcnt, &i2c_base->ic_fs_scl_hcnt);
300 writel(config.scl_lcnt, &i2c_base->ic_fs_scl_lcnt);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530301 break;
302 }
303
Stefan Roeseef6073e2014-10-28 12:12:00 +0100304 writel(cntl, &i2c_base->ic_con);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530305
Stefan Roese38481202016-04-21 08:19:42 +0200306 /* Configure SDA Hold Time if required */
Simon Glass245ec0b2020-01-23 11:48:13 -0700307 if (config.sda_hold)
308 writel(config.sda_hold, &i2c_base->ic_sda_hold);
Stefan Roese38481202016-04-21 08:19:42 +0200309
Jun Chend003a372019-06-05 15:23:16 +0800310 /* Restore back i2c now speed set */
311 if (ena == IC_ENABLE_0B)
312 dw_i2c_enable(i2c_base, true);
Stefan Roesef6322ebd2012-01-20 11:52:33 +0100313
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530314 return 0;
315}
316
317/*
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530318 * i2c_setaddress - Sets the target slave address
319 * @i2c_addr: target i2c address
320 *
321 * Sets the target slave address.
322 */
Stefan Roese41de7662016-04-21 08:19:40 +0200323static void i2c_setaddress(struct i2c_regs *i2c_base, unsigned int i2c_addr)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530324{
Alexey Brodkin41c56552013-11-07 17:52:18 +0400325 /* Disable i2c */
Stefan Roese3bc33ba2016-04-21 08:19:38 +0200326 dw_i2c_enable(i2c_base, false);
Alexey Brodkin41c56552013-11-07 17:52:18 +0400327
Stefan Roeseef6073e2014-10-28 12:12:00 +0100328 writel(i2c_addr, &i2c_base->ic_tar);
Alexey Brodkin41c56552013-11-07 17:52:18 +0400329
330 /* Enable i2c */
Stefan Roese3bc33ba2016-04-21 08:19:38 +0200331 dw_i2c_enable(i2c_base, true);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530332}
333
334/*
335 * i2c_flush_rxfifo - Flushes the i2c RX FIFO
336 *
337 * Flushes the i2c RX FIFO
338 */
Stefan Roese41de7662016-04-21 08:19:40 +0200339static void i2c_flush_rxfifo(struct i2c_regs *i2c_base)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530340{
Stefan Roeseef6073e2014-10-28 12:12:00 +0100341 while (readl(&i2c_base->ic_status) & IC_STATUS_RFNE)
342 readl(&i2c_base->ic_cmd_data);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530343}
344
345/*
346 * i2c_wait_for_bb - Waits for bus busy
347 *
348 * Waits for bus busy
349 */
Stefan Roese41de7662016-04-21 08:19:40 +0200350static int i2c_wait_for_bb(struct i2c_regs *i2c_base)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530351{
352 unsigned long start_time_bb = get_timer(0);
353
Stefan Roeseef6073e2014-10-28 12:12:00 +0100354 while ((readl(&i2c_base->ic_status) & IC_STATUS_MA) ||
355 !(readl(&i2c_base->ic_status) & IC_STATUS_TFE)) {
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530356
357 /* Evaluate timeout */
358 if (get_timer(start_time_bb) > (unsigned long)(I2C_BYTE_TO_BB))
359 return 1;
360 }
361
362 return 0;
363}
364
Stefan Roese41de7662016-04-21 08:19:40 +0200365static int i2c_xfer_init(struct i2c_regs *i2c_base, uchar chip, uint addr,
Stefan Roeseef6073e2014-10-28 12:12:00 +0100366 int alen)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530367{
Stefan Roese41de7662016-04-21 08:19:40 +0200368 if (i2c_wait_for_bb(i2c_base))
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530369 return 1;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530370
Stefan Roese41de7662016-04-21 08:19:40 +0200371 i2c_setaddress(i2c_base, chip);
Chin Liang Seea0c26262014-02-04 11:56:23 -0600372 while (alen) {
373 alen--;
374 /* high byte address going out first */
375 writel((addr >> (alen * 8)) & 0xff,
Stefan Roeseef6073e2014-10-28 12:12:00 +0100376 &i2c_base->ic_cmd_data);
Chin Liang Seea0c26262014-02-04 11:56:23 -0600377 }
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530378 return 0;
379}
380
Stefan Roese41de7662016-04-21 08:19:40 +0200381static int i2c_xfer_finish(struct i2c_regs *i2c_base)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530382{
383 ulong start_stop_det = get_timer(0);
384
385 while (1) {
Stefan Roeseef6073e2014-10-28 12:12:00 +0100386 if ((readl(&i2c_base->ic_raw_intr_stat) & IC_STOP_DET)) {
387 readl(&i2c_base->ic_clr_stop_det);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530388 break;
389 } else if (get_timer(start_stop_det) > I2C_STOPDET_TO) {
390 break;
391 }
392 }
393
Stefan Roese41de7662016-04-21 08:19:40 +0200394 if (i2c_wait_for_bb(i2c_base)) {
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530395 printf("Timed out waiting for bus\n");
396 return 1;
397 }
398
Stefan Roese41de7662016-04-21 08:19:40 +0200399 i2c_flush_rxfifo(i2c_base);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530400
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530401 return 0;
402}
403
404/*
405 * i2c_read - Read from i2c memory
406 * @chip: target i2c address
407 * @addr: address to read from
408 * @alen:
409 * @buffer: buffer for read data
410 * @len: no of bytes to be read
411 *
412 * Read from i2c memory.
413 */
Stefan Roese41de7662016-04-21 08:19:40 +0200414static int __dw_i2c_read(struct i2c_regs *i2c_base, u8 dev, uint addr,
415 int alen, u8 *buffer, int len)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530416{
417 unsigned long start_time_rx;
Marek Vasutc4bc9a82016-10-20 16:48:28 +0200418 unsigned int active = 0;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530419
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400420#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
421 /*
422 * EEPROM chips that implement "address overflow" are ones
423 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
424 * address and the extra bits end up in the "chip address"
425 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
426 * four 256 byte chips.
427 *
428 * Note that we consider the length of the address field to
429 * still be one byte because the extra address bits are
430 * hidden in the chip address.
431 */
Stefan Roeseef6073e2014-10-28 12:12:00 +0100432 dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400433 addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
434
Stefan Roeseef6073e2014-10-28 12:12:00 +0100435 debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400436 addr);
437#endif
438
Stefan Roese41de7662016-04-21 08:19:40 +0200439 if (i2c_xfer_init(i2c_base, dev, addr, alen))
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530440 return 1;
441
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530442 start_time_rx = get_timer(0);
443 while (len) {
Marek Vasutc4bc9a82016-10-20 16:48:28 +0200444 if (!active) {
445 /*
446 * Avoid writing to ic_cmd_data multiple times
447 * in case this loop spins too quickly and the
448 * ic_status RFNE bit isn't set after the first
449 * write. Subsequent writes to ic_cmd_data can
450 * trigger spurious i2c transfer.
451 */
452 if (len == 1)
453 writel(IC_CMD | IC_STOP, &i2c_base->ic_cmd_data);
454 else
455 writel(IC_CMD, &i2c_base->ic_cmd_data);
456 active = 1;
457 }
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530458
Stefan Roeseef6073e2014-10-28 12:12:00 +0100459 if (readl(&i2c_base->ic_status) & IC_STATUS_RFNE) {
460 *buffer++ = (uchar)readl(&i2c_base->ic_cmd_data);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530461 len--;
462 start_time_rx = get_timer(0);
Marek Vasutc4bc9a82016-10-20 16:48:28 +0200463 active = 0;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530464 } else if (get_timer(start_time_rx) > I2C_BYTE_TO) {
Marek Vasutc4bc9a82016-10-20 16:48:28 +0200465 return 1;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530466 }
467 }
468
Stefan Roese41de7662016-04-21 08:19:40 +0200469 return i2c_xfer_finish(i2c_base);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530470}
471
472/*
473 * i2c_write - Write to i2c memory
474 * @chip: target i2c address
475 * @addr: address to read from
476 * @alen:
477 * @buffer: buffer for read data
478 * @len: no of bytes to be read
479 *
480 * Write to i2c memory.
481 */
Stefan Roese41de7662016-04-21 08:19:40 +0200482static int __dw_i2c_write(struct i2c_regs *i2c_base, u8 dev, uint addr,
483 int alen, u8 *buffer, int len)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530484{
485 int nb = len;
486 unsigned long start_time_tx;
487
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400488#ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW
489 /*
490 * EEPROM chips that implement "address overflow" are ones
491 * like Catalyst 24WC04/08/16 which has 9/10/11 bits of
492 * address and the extra bits end up in the "chip address"
493 * bit slots. This makes a 24WC08 (1Kbyte) chip look like
494 * four 256 byte chips.
495 *
496 * Note that we consider the length of the address field to
497 * still be one byte because the extra address bits are
498 * hidden in the chip address.
499 */
Stefan Roeseef6073e2014-10-28 12:12:00 +0100500 dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW);
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400501 addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8));
502
Stefan Roeseef6073e2014-10-28 12:12:00 +0100503 debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev,
Alexey Brodkin7ef00362013-12-16 15:30:35 +0400504 addr);
505#endif
506
Stefan Roese41de7662016-04-21 08:19:40 +0200507 if (i2c_xfer_init(i2c_base, dev, addr, alen))
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530508 return 1;
509
510 start_time_tx = get_timer(0);
511 while (len) {
Stefan Roeseef6073e2014-10-28 12:12:00 +0100512 if (readl(&i2c_base->ic_status) & IC_STATUS_TFNF) {
513 if (--len == 0) {
514 writel(*buffer | IC_STOP,
515 &i2c_base->ic_cmd_data);
516 } else {
517 writel(*buffer, &i2c_base->ic_cmd_data);
518 }
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530519 buffer++;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530520 start_time_tx = get_timer(0);
521
522 } else if (get_timer(start_time_tx) > (nb * I2C_BYTE_TO)) {
523 printf("Timed out. i2c write Failed\n");
524 return 1;
525 }
526 }
527
Stefan Roese41de7662016-04-21 08:19:40 +0200528 return i2c_xfer_finish(i2c_base);
529}
530
Stefan Roese3cb27962016-04-21 08:19:41 +0200531/*
532 * __dw_i2c_init - Init function
533 * @speed: required i2c speed
534 * @slaveaddr: slave address for the device
535 *
536 * Initialization function.
537 */
Simon Glassbd9ca8d2019-02-16 20:24:39 -0700538static int __dw_i2c_init(struct i2c_regs *i2c_base, int speed, int slaveaddr)
Stefan Roese3cb27962016-04-21 08:19:41 +0200539{
Simon Glassbd9ca8d2019-02-16 20:24:39 -0700540 int ret;
541
Stefan Roese3cb27962016-04-21 08:19:41 +0200542 /* Disable i2c */
Simon Glassbd9ca8d2019-02-16 20:24:39 -0700543 ret = dw_i2c_enable(i2c_base, false);
544 if (ret)
545 return ret;
Stefan Roese3cb27962016-04-21 08:19:41 +0200546
Marek Vasut808aa132017-08-07 20:45:31 +0200547 writel(IC_CON_SD | IC_CON_RE | IC_CON_SPD_FS | IC_CON_MM,
548 &i2c_base->ic_con);
Stefan Roese3cb27962016-04-21 08:19:41 +0200549 writel(IC_RX_TL, &i2c_base->ic_rx_tl);
550 writel(IC_TX_TL, &i2c_base->ic_tx_tl);
551 writel(IC_STOP_DET, &i2c_base->ic_intr_mask);
552#ifndef CONFIG_DM_I2C
Simon Glassc5294192020-01-23 11:48:25 -0700553 _dw_i2c_set_bus_speed(NULL, i2c_base, speed, IC_CLK);
Stefan Roese3cb27962016-04-21 08:19:41 +0200554 writel(slaveaddr, &i2c_base->ic_sar);
555#endif
556
557 /* Enable i2c */
Simon Glassbd9ca8d2019-02-16 20:24:39 -0700558 ret = dw_i2c_enable(i2c_base, true);
559 if (ret)
560 return ret;
561
562 return 0;
Stefan Roese3cb27962016-04-21 08:19:41 +0200563}
564
565#ifndef CONFIG_DM_I2C
566/*
567 * The legacy I2C functions. These need to get removed once
568 * all users of this driver are converted to DM.
569 */
Stefan Roese41de7662016-04-21 08:19:40 +0200570static struct i2c_regs *i2c_get_base(struct i2c_adapter *adap)
571{
572 switch (adap->hwadapnr) {
573#if CONFIG_SYS_I2C_BUS_MAX >= 4
574 case 3:
575 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE3;
576#endif
577#if CONFIG_SYS_I2C_BUS_MAX >= 3
578 case 2:
579 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE2;
580#endif
581#if CONFIG_SYS_I2C_BUS_MAX >= 2
582 case 1:
583 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE1;
584#endif
585 case 0:
586 return (struct i2c_regs *)CONFIG_SYS_I2C_BASE;
587 default:
588 printf("Wrong I2C-adapter number %d\n", adap->hwadapnr);
589 }
590
591 return NULL;
592}
593
594static unsigned int dw_i2c_set_bus_speed(struct i2c_adapter *adap,
595 unsigned int speed)
596{
597 adap->speed = speed;
Simon Glassc5294192020-01-23 11:48:25 -0700598 return _dw_i2c_set_bus_speed(NULL, i2c_get_base(adap), speed, IC_CLK);
Stefan Roese41de7662016-04-21 08:19:40 +0200599}
600
Stefan Roese3cb27962016-04-21 08:19:41 +0200601static void dw_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr)
Stefan Roese41de7662016-04-21 08:19:40 +0200602{
Stefan Roese3cb27962016-04-21 08:19:41 +0200603 __dw_i2c_init(i2c_get_base(adap), speed, slaveaddr);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530604}
605
Stefan Roese41de7662016-04-21 08:19:40 +0200606static int dw_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr,
607 int alen, u8 *buffer, int len)
608{
609 return __dw_i2c_read(i2c_get_base(adap), dev, addr, alen, buffer, len);
610}
611
612static int dw_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr,
613 int alen, u8 *buffer, int len)
614{
615 return __dw_i2c_write(i2c_get_base(adap), dev, addr, alen, buffer, len);
616}
617
Stefan Roese3cb27962016-04-21 08:19:41 +0200618/* dw_i2c_probe - Probe the i2c chip */
Stefan Roeseef6073e2014-10-28 12:12:00 +0100619static int dw_i2c_probe(struct i2c_adapter *adap, u8 dev)
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530620{
Stefan Roese41de7662016-04-21 08:19:40 +0200621 struct i2c_regs *i2c_base = i2c_get_base(adap);
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530622 u32 tmp;
Stefan Roesef6322ebd2012-01-20 11:52:33 +0100623 int ret;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530624
625 /*
626 * Try to read the first location of the chip.
627 */
Stefan Roese41de7662016-04-21 08:19:40 +0200628 ret = __dw_i2c_read(i2c_base, dev, 0, 1, (uchar *)&tmp, 1);
Stefan Roesef6322ebd2012-01-20 11:52:33 +0100629 if (ret)
Stefan Roeseef6073e2014-10-28 12:12:00 +0100630 dw_i2c_init(adap, adap->speed, adap->slaveaddr);
Stefan Roesef6322ebd2012-01-20 11:52:33 +0100631
632 return ret;
Vipin KUMARfc9589f2010-01-15 19:15:44 +0530633}
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000634
Stefan Roeseef6073e2014-10-28 12:12:00 +0100635U_BOOT_I2C_ADAP_COMPLETE(dw_0, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
636 dw_i2c_write, dw_i2c_set_bus_speed,
637 CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0)
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000638
Stefan Roeseef6073e2014-10-28 12:12:00 +0100639#if CONFIG_SYS_I2C_BUS_MAX >= 2
640U_BOOT_I2C_ADAP_COMPLETE(dw_1, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
641 dw_i2c_write, dw_i2c_set_bus_speed,
642 CONFIG_SYS_I2C_SPEED1, CONFIG_SYS_I2C_SLAVE1, 1)
643#endif
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000644
Stefan Roeseef6073e2014-10-28 12:12:00 +0100645#if CONFIG_SYS_I2C_BUS_MAX >= 3
646U_BOOT_I2C_ADAP_COMPLETE(dw_2, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
647 dw_i2c_write, dw_i2c_set_bus_speed,
648 CONFIG_SYS_I2C_SPEED2, CONFIG_SYS_I2C_SLAVE2, 2)
649#endif
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000650
Stefan Roeseef6073e2014-10-28 12:12:00 +0100651#if CONFIG_SYS_I2C_BUS_MAX >= 4
652U_BOOT_I2C_ADAP_COMPLETE(dw_3, dw_i2c_init, dw_i2c_probe, dw_i2c_read,
653 dw_i2c_write, dw_i2c_set_bus_speed,
654 CONFIG_SYS_I2C_SPEED3, CONFIG_SYS_I2C_SLAVE3, 3)
Armando Visconti4a7b4ec2012-12-06 00:04:15 +0000655#endif
Stefan Roese3cb27962016-04-21 08:19:41 +0200656
657#else /* CONFIG_DM_I2C */
658/* The DM I2C functions */
659
660static int designware_i2c_xfer(struct udevice *bus, struct i2c_msg *msg,
661 int nmsgs)
662{
663 struct dw_i2c *i2c = dev_get_priv(bus);
664 int ret;
665
666 debug("i2c_xfer: %d messages\n", nmsgs);
667 for (; nmsgs > 0; nmsgs--, msg++) {
668 debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len);
669 if (msg->flags & I2C_M_RD) {
670 ret = __dw_i2c_read(i2c->regs, msg->addr, 0, 0,
671 msg->buf, msg->len);
672 } else {
673 ret = __dw_i2c_write(i2c->regs, msg->addr, 0, 0,
674 msg->buf, msg->len);
675 }
676 if (ret) {
677 debug("i2c_write: error sending\n");
678 return -EREMOTEIO;
679 }
680 }
681
682 return 0;
683}
684
685static int designware_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
686{
687 struct dw_i2c *i2c = dev_get_priv(bus);
Ley Foon Tan6e85c812019-06-12 09:48:04 +0800688 ulong rate;
689
690#if CONFIG_IS_ENABLED(CLK)
691 rate = clk_get_rate(&i2c->clk);
692 if (IS_ERR_VALUE(rate))
693 return -EINVAL;
Ley Foon Tan6e85c812019-06-12 09:48:04 +0800694#else
695 rate = IC_CLK;
696#endif
Simon Glassc5294192020-01-23 11:48:25 -0700697 return _dw_i2c_set_bus_speed(i2c, i2c->regs, speed, rate);
Stefan Roese3cb27962016-04-21 08:19:41 +0200698}
699
700static int designware_i2c_probe_chip(struct udevice *bus, uint chip_addr,
701 uint chip_flags)
702{
703 struct dw_i2c *i2c = dev_get_priv(bus);
704 struct i2c_regs *i2c_base = i2c->regs;
705 u32 tmp;
706 int ret;
707
708 /* Try to read the first location of the chip */
709 ret = __dw_i2c_read(i2c_base, chip_addr, 0, 1, (uchar *)&tmp, 1);
710 if (ret)
711 __dw_i2c_init(i2c_base, 0, 0);
712
713 return ret;
714}
715
Simon Glass9e5d1742020-01-23 11:48:11 -0700716int designware_i2c_ofdata_to_platdata(struct udevice *bus)
Stefan Roese3cb27962016-04-21 08:19:41 +0200717{
718 struct dw_i2c *priv = dev_get_priv(bus);
Simon Glass8de5ae82020-01-23 11:48:26 -0700719 int ret;
Stefan Roese3cb27962016-04-21 08:19:41 +0200720
Simon Glass9e5d1742020-01-23 11:48:11 -0700721 if (!priv->regs)
722 priv->regs = (struct i2c_regs *)devfdt_get_addr_ptr(bus);
723 dev_read_u32(bus, "i2c-scl-rising-time-ns", &priv->scl_rise_time_ns);
724 dev_read_u32(bus, "i2c-scl-falling-time-ns", &priv->scl_fall_time_ns);
725 dev_read_u32(bus, "i2c-sda-hold-time-ns", &priv->sda_hold_time_ns);
Simon Glasse2be5532019-12-06 21:41:40 -0700726
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 Glass8de5ae82020-01-23 11:48:26 -0700746 return 0;
747}
748
749int designware_i2c_probe(struct udevice *bus)
750{
751 struct dw_i2c *priv = dev_get_priv(bus);
752
Simon Glassbd9ca8d2019-02-16 20:24:39 -0700753 return __dw_i2c_init(priv->regs, 0, 0);
Stefan Roese3cb27962016-04-21 08:19:41 +0200754}
755
Simon Glasse2be5532019-12-06 21:41:40 -0700756int designware_i2c_remove(struct udevice *dev)
Simon Goldschmidt28608a12019-03-28 21:11:48 +0100757{
758 struct dw_i2c *priv = dev_get_priv(dev);
759
Ley Foon Tan6e85c812019-06-12 09:48:04 +0800760#if CONFIG_IS_ENABLED(CLK)
761 clk_disable(&priv->clk);
762 clk_free(&priv->clk);
763#endif
764
Simon Goldschmidt28608a12019-03-28 21:11:48 +0100765 return reset_release_bulk(&priv->resets);
766}
767
Simon Glasse2be5532019-12-06 21:41:40 -0700768const struct dm_i2c_ops designware_i2c_ops = {
Stefan Roese3cb27962016-04-21 08:19:41 +0200769 .xfer = designware_i2c_xfer,
770 .probe_chip = designware_i2c_probe_chip,
771 .set_bus_speed = designware_i2c_set_bus_speed,
772};
773
774static const struct udevice_id designware_i2c_ids[] = {
775 { .compatible = "snps,designware-i2c" },
776 { }
777};
778
779U_BOOT_DRIVER(i2c_designware) = {
780 .name = "i2c_designware",
781 .id = UCLASS_I2C,
782 .of_match = designware_i2c_ids,
Simon Glasse2be5532019-12-06 21:41:40 -0700783 .ofdata_to_platdata = designware_i2c_ofdata_to_platdata,
Stefan Roese3cb27962016-04-21 08:19:41 +0200784 .probe = designware_i2c_probe,
785 .priv_auto_alloc_size = sizeof(struct dw_i2c),
Simon Goldschmidt28608a12019-03-28 21:11:48 +0100786 .remove = designware_i2c_remove,
Simon Glasse2be5532019-12-06 21:41:40 -0700787 .flags = DM_FLAG_OS_PREPARE,
Stefan Roese3cb27962016-04-21 08:19:41 +0200788 .ops = &designware_i2c_ops,
789};
790
791#endif /* CONFIG_DM_I2C */