Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2009 |
| 4 | * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com. |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | 9b7af64 | 2020-01-23 11:48:06 -0700 | [diff] [blame] | 8 | #include <clk.h> |
Stefan Roese | 3cb2796 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 9 | #include <dm.h> |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 10 | #include <i2c.h> |
Stefan Roese | 3848120 | 2016-04-21 08:19:42 +0200 | [diff] [blame] | 11 | #include <pci.h> |
Dinh Nguyen | 08794aa | 2018-04-04 17:18:24 -0500 | [diff] [blame] | 12 | #include <reset.h> |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 13 | #include <asm/io.h> |
Vipin KUMAR | 3f64acb | 2012-02-26 23:13:29 +0000 | [diff] [blame] | 14 | #include "designware_i2c.h" |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame^] | 15 | #include <linux/err.h> |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 16 | |
Stefan Roese | abb3e13 | 2016-04-27 09:02:12 +0200 | [diff] [blame] | 17 | #ifdef CONFIG_SYS_I2C_DW_ENABLE_STATUS_UNSUPPORTED |
Simon Glass | bd9ca8d | 2019-02-16 20:24:39 -0700 | [diff] [blame] | 18 | static int dw_i2c_enable(struct i2c_regs *i2c_base, bool enable) |
Stefan Roese | 3bc33ba | 2016-04-21 08:19:38 +0200 | [diff] [blame] | 19 | { |
| 20 | u32 ena = enable ? IC_ENABLE_0B : 0; |
Stefan Roese | abb3e13 | 2016-04-27 09:02:12 +0200 | [diff] [blame] | 21 | |
| 22 | writel(ena, &i2c_base->ic_enable); |
Simon Glass | bd9ca8d | 2019-02-16 20:24:39 -0700 | [diff] [blame] | 23 | |
| 24 | return 0; |
Stefan Roese | abb3e13 | 2016-04-27 09:02:12 +0200 | [diff] [blame] | 25 | } |
| 26 | #else |
Simon Glass | bd9ca8d | 2019-02-16 20:24:39 -0700 | [diff] [blame] | 27 | static int dw_i2c_enable(struct i2c_regs *i2c_base, bool enable) |
Stefan Roese | abb3e13 | 2016-04-27 09:02:12 +0200 | [diff] [blame] | 28 | { |
| 29 | u32 ena = enable ? IC_ENABLE_0B : 0; |
Stefan Roese | 3bc33ba | 2016-04-21 08:19:38 +0200 | [diff] [blame] | 30 | int timeout = 100; |
| 31 | |
| 32 | do { |
| 33 | writel(ena, &i2c_base->ic_enable); |
| 34 | if ((readl(&i2c_base->ic_enable_status) & IC_ENABLE_0B) == ena) |
Simon Glass | bd9ca8d | 2019-02-16 20:24:39 -0700 | [diff] [blame] | 35 | return 0; |
Stefan Roese | 3bc33ba | 2016-04-21 08:19:38 +0200 | [diff] [blame] | 36 | |
| 37 | /* |
| 38 | * Wait 10 times the signaling period of the highest I2C |
| 39 | * transfer supported by the driver (for 400KHz this is |
| 40 | * 25us) as described in the DesignWare I2C databook. |
| 41 | */ |
| 42 | udelay(25); |
| 43 | } while (timeout--); |
Stefan Roese | 3bc33ba | 2016-04-21 08:19:38 +0200 | [diff] [blame] | 44 | printf("timeout in %sabling I2C adapter\n", enable ? "en" : "dis"); |
Simon Glass | bd9ca8d | 2019-02-16 20:24:39 -0700 | [diff] [blame] | 45 | |
| 46 | return -ETIMEDOUT; |
Stefan Roese | 3bc33ba | 2016-04-21 08:19:38 +0200 | [diff] [blame] | 47 | } |
Stefan Roese | abb3e13 | 2016-04-27 09:02:12 +0200 | [diff] [blame] | 48 | #endif |
Stefan Roese | 3bc33ba | 2016-04-21 08:19:38 +0200 | [diff] [blame] | 49 | |
Simon Glass | c718110 | 2020-01-23 11:48:14 -0700 | [diff] [blame] | 50 | /* High and low times in different speed modes (in ns) */ |
| 51 | enum { |
| 52 | /* SDA Hold Time */ |
| 53 | DEFAULT_SDA_HOLD_TIME = 300, |
| 54 | }; |
| 55 | |
| 56 | /** |
| 57 | * calc_counts() - Convert a period to a number of IC clk cycles |
| 58 | * |
| 59 | * @ic_clk: Input clock in Hz |
| 60 | * @period_ns: Period to represent, in ns |
| 61 | * @return calculated count |
| 62 | */ |
| 63 | static uint calc_counts(uint ic_clk, uint period_ns) |
| 64 | { |
| 65 | return DIV_ROUND_UP(ic_clk / 1000 * period_ns, NANO_TO_KILO); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * struct i2c_mode_info - Information about an I2C speed mode |
| 70 | * |
| 71 | * Each speed mode has its own characteristics. This struct holds these to aid |
| 72 | * calculations in dw_i2c_calc_timing(). |
| 73 | * |
| 74 | * @speed: Speed in Hz |
| 75 | * @min_scl_lowtime_ns: Minimum value for SCL low period in ns |
| 76 | * @min_scl_hightime_ns: Minimum value for SCL high period in ns |
| 77 | * @def_rise_time_ns: Default rise time in ns |
| 78 | * @def_fall_time_ns: Default fall time in ns |
| 79 | */ |
| 80 | struct i2c_mode_info { |
| 81 | int speed; |
| 82 | int min_scl_hightime_ns; |
| 83 | int min_scl_lowtime_ns; |
| 84 | int def_rise_time_ns; |
| 85 | int def_fall_time_ns; |
| 86 | }; |
| 87 | |
| 88 | static const struct i2c_mode_info info_for_mode[] = { |
| 89 | [IC_SPEED_MODE_STANDARD] = { |
Simon Glass | ac77bae | 2020-01-23 11:48:18 -0700 | [diff] [blame] | 90 | I2C_SPEED_STANDARD_RATE, |
Simon Glass | c718110 | 2020-01-23 11:48:14 -0700 | [diff] [blame] | 91 | MIN_SS_SCL_HIGHTIME, |
| 92 | MIN_SS_SCL_LOWTIME, |
| 93 | 1000, |
| 94 | 300, |
| 95 | }, |
| 96 | [IC_SPEED_MODE_FAST] = { |
Simon Glass | ac77bae | 2020-01-23 11:48:18 -0700 | [diff] [blame] | 97 | I2C_SPEED_FAST_RATE, |
Simon Glass | c718110 | 2020-01-23 11:48:14 -0700 | [diff] [blame] | 98 | MIN_FS_SCL_HIGHTIME, |
| 99 | MIN_FS_SCL_LOWTIME, |
| 100 | 300, |
| 101 | 300, |
| 102 | }, |
Simon Glass | 4564922 | 2020-01-23 11:48:23 -0700 | [diff] [blame] | 103 | [IC_SPEED_MODE_FAST_PLUS] = { |
| 104 | I2C_SPEED_FAST_PLUS_RATE, |
| 105 | MIN_FP_SCL_HIGHTIME, |
| 106 | MIN_FP_SCL_LOWTIME, |
| 107 | 260, |
| 108 | 500, |
| 109 | }, |
Simon Glass | c718110 | 2020-01-23 11:48:14 -0700 | [diff] [blame] | 110 | [IC_SPEED_MODE_HIGH] = { |
Simon Glass | ac77bae | 2020-01-23 11:48:18 -0700 | [diff] [blame] | 111 | I2C_SPEED_HIGH_RATE, |
Simon Glass | c718110 | 2020-01-23 11:48:14 -0700 | [diff] [blame] | 112 | MIN_HS_SCL_HIGHTIME, |
| 113 | MIN_HS_SCL_LOWTIME, |
| 114 | 120, |
| 115 | 120, |
| 116 | }, |
| 117 | }; |
| 118 | |
| 119 | /** |
| 120 | * dw_i2c_calc_timing() - Calculate the timings to use for a bus |
| 121 | * |
| 122 | * @priv: Bus private information (NULL if not using driver model) |
| 123 | * @mode: Speed mode to use |
| 124 | * @ic_clk: IC clock speed in Hz |
| 125 | * @spk_cnt: Spike-suppression count |
| 126 | * @config: Returns value to use |
| 127 | * @return 0 if OK, -EINVAL if the calculation failed due to invalid data |
| 128 | */ |
| 129 | static int dw_i2c_calc_timing(struct dw_i2c *priv, enum i2c_speed_mode mode, |
| 130 | int ic_clk, int spk_cnt, |
| 131 | struct dw_i2c_speed_config *config) |
| 132 | { |
| 133 | int fall_cnt, rise_cnt, min_tlow_cnt, min_thigh_cnt; |
| 134 | int hcnt, lcnt, period_cnt, diff, tot; |
| 135 | int sda_hold_time_ns, scl_rise_time_ns, scl_fall_time_ns; |
| 136 | const struct i2c_mode_info *info; |
| 137 | |
| 138 | /* |
| 139 | * Find the period, rise, fall, min tlow, and min thigh in terms of |
| 140 | * counts of the IC clock |
| 141 | */ |
| 142 | info = &info_for_mode[mode]; |
| 143 | period_cnt = ic_clk / info->speed; |
| 144 | scl_rise_time_ns = priv && priv->scl_rise_time_ns ? |
| 145 | priv->scl_rise_time_ns : info->def_rise_time_ns; |
| 146 | scl_fall_time_ns = priv && priv->scl_fall_time_ns ? |
| 147 | priv->scl_fall_time_ns : info->def_fall_time_ns; |
| 148 | rise_cnt = calc_counts(ic_clk, scl_rise_time_ns); |
| 149 | fall_cnt = calc_counts(ic_clk, scl_fall_time_ns); |
| 150 | min_tlow_cnt = calc_counts(ic_clk, info->min_scl_lowtime_ns); |
| 151 | min_thigh_cnt = calc_counts(ic_clk, info->min_scl_hightime_ns); |
| 152 | |
| 153 | debug("dw_i2c: period %d rise %d fall %d tlow %d thigh %d spk %d\n", |
| 154 | period_cnt, rise_cnt, fall_cnt, min_tlow_cnt, min_thigh_cnt, |
| 155 | spk_cnt); |
| 156 | |
| 157 | /* |
| 158 | * Back-solve for hcnt and lcnt according to the following equations: |
| 159 | * SCL_High_time = [(HCNT + IC_*_SPKLEN + 7) * ic_clk] + SCL_Fall_time |
| 160 | * SCL_Low_time = [(LCNT + 1) * ic_clk] - SCL_Fall_time + SCL_Rise_time |
| 161 | */ |
| 162 | hcnt = min_thigh_cnt - fall_cnt - 7 - spk_cnt; |
| 163 | lcnt = min_tlow_cnt - rise_cnt + fall_cnt - 1; |
| 164 | |
| 165 | if (hcnt < 0 || lcnt < 0) { |
| 166 | debug("dw_i2c: bad counts. hcnt = %d lcnt = %d\n", hcnt, lcnt); |
| 167 | return -EINVAL; |
| 168 | } |
| 169 | |
| 170 | /* |
| 171 | * Now add things back up to ensure the period is hit. If it is off, |
| 172 | * split the difference and bias to lcnt for remainder |
| 173 | */ |
| 174 | tot = hcnt + lcnt + 7 + spk_cnt + rise_cnt + 1; |
| 175 | |
| 176 | if (tot < period_cnt) { |
| 177 | diff = (period_cnt - tot) / 2; |
| 178 | hcnt += diff; |
| 179 | lcnt += diff; |
| 180 | tot = hcnt + lcnt + 7 + spk_cnt + rise_cnt + 1; |
| 181 | lcnt += period_cnt - tot; |
| 182 | } |
| 183 | |
| 184 | config->scl_lcnt = lcnt; |
| 185 | config->scl_hcnt = hcnt; |
| 186 | |
| 187 | /* Use internal default unless other value is specified */ |
| 188 | sda_hold_time_ns = priv && priv->sda_hold_time_ns ? |
| 189 | priv->sda_hold_time_ns : DEFAULT_SDA_HOLD_TIME; |
| 190 | config->sda_hold = calc_counts(ic_clk, sda_hold_time_ns); |
| 191 | |
| 192 | debug("dw_i2c: hcnt = %d lcnt = %d sda hold = %d\n", hcnt, lcnt, |
| 193 | config->sda_hold); |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
Simon Glass | c529419 | 2020-01-23 11:48:25 -0700 | [diff] [blame] | 198 | static int calc_bus_speed(struct dw_i2c *priv, int speed, ulong bus_clk, |
| 199 | struct dw_i2c_speed_config *config) |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 200 | { |
Simon Glass | 60e0c3a | 2020-01-23 11:48:12 -0700 | [diff] [blame] | 201 | const struct dw_scl_sda_cfg *scl_sda_cfg = NULL; |
Simon Glass | c529419 | 2020-01-23 11:48:25 -0700 | [diff] [blame] | 202 | struct i2c_regs *regs = priv->regs; |
Simon Glass | 6ed44ae | 2020-01-23 11:48:08 -0700 | [diff] [blame] | 203 | enum i2c_speed_mode i2c_spd; |
Simon Glass | c38e2b3 | 2020-01-23 11:48:15 -0700 | [diff] [blame] | 204 | int spk_cnt; |
Simon Glass | c718110 | 2020-01-23 11:48:14 -0700 | [diff] [blame] | 205 | int ret; |
Stefan Roese | 88893c9 | 2016-04-21 08:19:39 +0200 | [diff] [blame] | 206 | |
Simon Glass | 60e0c3a | 2020-01-23 11:48:12 -0700 | [diff] [blame] | 207 | if (priv) |
| 208 | scl_sda_cfg = priv->scl_sda_cfg; |
Simon Glass | f5ef101 | 2020-01-23 11:48:07 -0700 | [diff] [blame] | 209 | /* Allow high speed if there is no config, or the config allows it */ |
Simon Glass | ac77bae | 2020-01-23 11:48:18 -0700 | [diff] [blame] | 210 | if (speed >= I2C_SPEED_HIGH_RATE && |
Simon Glass | f5ef101 | 2020-01-23 11:48:07 -0700 | [diff] [blame] | 211 | (!scl_sda_cfg || scl_sda_cfg->has_high_speed)) |
| 212 | i2c_spd = IC_SPEED_MODE_HIGH; |
Simon Glass | ac77bae | 2020-01-23 11:48:18 -0700 | [diff] [blame] | 213 | else if (speed >= I2C_SPEED_FAST_RATE) |
Simon Glass | 4564922 | 2020-01-23 11:48:23 -0700 | [diff] [blame] | 214 | i2c_spd = IC_SPEED_MODE_FAST_PLUS; |
| 215 | else if (speed >= I2C_SPEED_FAST_PLUS_RATE) |
Stefan Roese | 88893c9 | 2016-04-21 08:19:39 +0200 | [diff] [blame] | 216 | i2c_spd = IC_SPEED_MODE_FAST; |
| 217 | else |
| 218 | i2c_spd = IC_SPEED_MODE_STANDARD; |
Armando Visconti | 631e693 | 2012-03-29 20:10:17 +0000 | [diff] [blame] | 219 | |
Simon Glass | c38e2b3 | 2020-01-23 11:48:15 -0700 | [diff] [blame] | 220 | /* Get the proper spike-suppression count based on target speed */ |
| 221 | if (!priv || !priv->has_spk_cnt) |
| 222 | spk_cnt = 0; |
| 223 | else if (i2c_spd >= IC_SPEED_MODE_HIGH) |
Simon Glass | c529419 | 2020-01-23 11:48:25 -0700 | [diff] [blame] | 224 | spk_cnt = readl(®s->hs_spklen); |
Simon Glass | c38e2b3 | 2020-01-23 11:48:15 -0700 | [diff] [blame] | 225 | else |
Simon Glass | c529419 | 2020-01-23 11:48:25 -0700 | [diff] [blame] | 226 | spk_cnt = readl(®s->fs_spklen); |
Simon Glass | 245ec0b | 2020-01-23 11:48:13 -0700 | [diff] [blame] | 227 | if (scl_sda_cfg) { |
Simon Glass | c529419 | 2020-01-23 11:48:25 -0700 | [diff] [blame] | 228 | config->sda_hold = scl_sda_cfg->sda_hold; |
Simon Glass | 245ec0b | 2020-01-23 11:48:13 -0700 | [diff] [blame] | 229 | if (i2c_spd == IC_SPEED_MODE_STANDARD) { |
Simon Glass | c529419 | 2020-01-23 11:48:25 -0700 | [diff] [blame] | 230 | config->scl_hcnt = scl_sda_cfg->ss_hcnt; |
| 231 | config->scl_lcnt = scl_sda_cfg->ss_lcnt; |
Simon Glass | 245ec0b | 2020-01-23 11:48:13 -0700 | [diff] [blame] | 232 | } else { |
Simon Glass | c529419 | 2020-01-23 11:48:25 -0700 | [diff] [blame] | 233 | config->scl_hcnt = scl_sda_cfg->fs_hcnt; |
| 234 | config->scl_lcnt = scl_sda_cfg->fs_lcnt; |
Simon Glass | 245ec0b | 2020-01-23 11:48:13 -0700 | [diff] [blame] | 235 | } |
Simon Glass | c718110 | 2020-01-23 11:48:14 -0700 | [diff] [blame] | 236 | } else { |
Simon Glass | c38e2b3 | 2020-01-23 11:48:15 -0700 | [diff] [blame] | 237 | ret = dw_i2c_calc_timing(priv, i2c_spd, bus_clk, spk_cnt, |
Simon Glass | c529419 | 2020-01-23 11:48:25 -0700 | [diff] [blame] | 238 | config); |
Simon Glass | c718110 | 2020-01-23 11:48:14 -0700 | [diff] [blame] | 239 | if (ret) |
| 240 | return log_msg_ret("gen_confg", ret); |
Simon Glass | 245ec0b | 2020-01-23 11:48:13 -0700 | [diff] [blame] | 241 | } |
Simon Glass | c529419 | 2020-01-23 11:48:25 -0700 | [diff] [blame] | 242 | config->speed_mode = i2c_spd; |
| 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * _dw_i2c_set_bus_speed - Set the i2c speed |
| 249 | * @speed: required i2c speed |
| 250 | * |
| 251 | * Set the i2c speed. |
| 252 | */ |
| 253 | static int _dw_i2c_set_bus_speed(struct dw_i2c *priv, struct i2c_regs *i2c_base, |
| 254 | unsigned int speed, unsigned int bus_clk) |
| 255 | { |
| 256 | struct dw_i2c_speed_config config; |
| 257 | unsigned int cntl; |
| 258 | unsigned int ena; |
| 259 | int ret; |
| 260 | |
| 261 | ret = calc_bus_speed(priv, speed, bus_clk, &config); |
| 262 | if (ret) |
| 263 | return ret; |
| 264 | |
| 265 | /* Get enable setting for restore later */ |
| 266 | ena = readl(&i2c_base->ic_enable) & IC_ENABLE_0B; |
| 267 | |
| 268 | /* to set speed cltr must be disabled */ |
| 269 | dw_i2c_enable(i2c_base, false); |
| 270 | |
| 271 | cntl = (readl(&i2c_base->ic_con) & (~IC_CON_SPD_MSK)); |
Simon Glass | 245ec0b | 2020-01-23 11:48:13 -0700 | [diff] [blame] | 272 | |
Simon Glass | c529419 | 2020-01-23 11:48:25 -0700 | [diff] [blame] | 273 | switch (config.speed_mode) { |
Simon Glass | f5ef101 | 2020-01-23 11:48:07 -0700 | [diff] [blame] | 274 | case IC_SPEED_MODE_HIGH: |
Stefan Roese | 3848120 | 2016-04-21 08:19:42 +0200 | [diff] [blame] | 275 | cntl |= IC_CON_SPD_SS; |
Simon Glass | 245ec0b | 2020-01-23 11:48:13 -0700 | [diff] [blame] | 276 | writel(config.scl_hcnt, &i2c_base->ic_hs_scl_hcnt); |
| 277 | writel(config.scl_lcnt, &i2c_base->ic_hs_scl_lcnt); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 278 | break; |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 279 | case IC_SPEED_MODE_STANDARD: |
| 280 | cntl |= IC_CON_SPD_SS; |
Simon Glass | 245ec0b | 2020-01-23 11:48:13 -0700 | [diff] [blame] | 281 | writel(config.scl_hcnt, &i2c_base->ic_ss_scl_hcnt); |
| 282 | writel(config.scl_lcnt, &i2c_base->ic_ss_scl_lcnt); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 283 | break; |
Simon Glass | 4564922 | 2020-01-23 11:48:23 -0700 | [diff] [blame] | 284 | case IC_SPEED_MODE_FAST_PLUS: |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 285 | case IC_SPEED_MODE_FAST: |
| 286 | default: |
| 287 | cntl |= IC_CON_SPD_FS; |
Simon Glass | 245ec0b | 2020-01-23 11:48:13 -0700 | [diff] [blame] | 288 | writel(config.scl_hcnt, &i2c_base->ic_fs_scl_hcnt); |
| 289 | writel(config.scl_lcnt, &i2c_base->ic_fs_scl_lcnt); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 290 | break; |
| 291 | } |
| 292 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 293 | writel(cntl, &i2c_base->ic_con); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 294 | |
Stefan Roese | 3848120 | 2016-04-21 08:19:42 +0200 | [diff] [blame] | 295 | /* Configure SDA Hold Time if required */ |
Simon Glass | 245ec0b | 2020-01-23 11:48:13 -0700 | [diff] [blame] | 296 | if (config.sda_hold) |
| 297 | writel(config.sda_hold, &i2c_base->ic_sda_hold); |
Stefan Roese | 3848120 | 2016-04-21 08:19:42 +0200 | [diff] [blame] | 298 | |
Jun Chen | d003a37 | 2019-06-05 15:23:16 +0800 | [diff] [blame] | 299 | /* Restore back i2c now speed set */ |
| 300 | if (ena == IC_ENABLE_0B) |
| 301 | dw_i2c_enable(i2c_base, true); |
Stefan Roese | f6322ebd | 2012-01-20 11:52:33 +0100 | [diff] [blame] | 302 | |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | /* |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 307 | * i2c_setaddress - Sets the target slave address |
| 308 | * @i2c_addr: target i2c address |
| 309 | * |
| 310 | * Sets the target slave address. |
| 311 | */ |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 312 | static void i2c_setaddress(struct i2c_regs *i2c_base, unsigned int i2c_addr) |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 313 | { |
Alexey Brodkin | 41c5655 | 2013-11-07 17:52:18 +0400 | [diff] [blame] | 314 | /* Disable i2c */ |
Stefan Roese | 3bc33ba | 2016-04-21 08:19:38 +0200 | [diff] [blame] | 315 | dw_i2c_enable(i2c_base, false); |
Alexey Brodkin | 41c5655 | 2013-11-07 17:52:18 +0400 | [diff] [blame] | 316 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 317 | writel(i2c_addr, &i2c_base->ic_tar); |
Alexey Brodkin | 41c5655 | 2013-11-07 17:52:18 +0400 | [diff] [blame] | 318 | |
| 319 | /* Enable i2c */ |
Stefan Roese | 3bc33ba | 2016-04-21 08:19:38 +0200 | [diff] [blame] | 320 | dw_i2c_enable(i2c_base, true); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | /* |
| 324 | * i2c_flush_rxfifo - Flushes the i2c RX FIFO |
| 325 | * |
| 326 | * Flushes the i2c RX FIFO |
| 327 | */ |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 328 | static void i2c_flush_rxfifo(struct i2c_regs *i2c_base) |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 329 | { |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 330 | while (readl(&i2c_base->ic_status) & IC_STATUS_RFNE) |
| 331 | readl(&i2c_base->ic_cmd_data); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | /* |
| 335 | * i2c_wait_for_bb - Waits for bus busy |
| 336 | * |
| 337 | * Waits for bus busy |
| 338 | */ |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 339 | static int i2c_wait_for_bb(struct i2c_regs *i2c_base) |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 340 | { |
| 341 | unsigned long start_time_bb = get_timer(0); |
| 342 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 343 | while ((readl(&i2c_base->ic_status) & IC_STATUS_MA) || |
| 344 | !(readl(&i2c_base->ic_status) & IC_STATUS_TFE)) { |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 345 | |
| 346 | /* Evaluate timeout */ |
| 347 | if (get_timer(start_time_bb) > (unsigned long)(I2C_BYTE_TO_BB)) |
| 348 | return 1; |
| 349 | } |
| 350 | |
| 351 | return 0; |
| 352 | } |
| 353 | |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 354 | static int i2c_xfer_init(struct i2c_regs *i2c_base, uchar chip, uint addr, |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 355 | int alen) |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 356 | { |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 357 | if (i2c_wait_for_bb(i2c_base)) |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 358 | return 1; |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 359 | |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 360 | i2c_setaddress(i2c_base, chip); |
Chin Liang See | a0c2626 | 2014-02-04 11:56:23 -0600 | [diff] [blame] | 361 | while (alen) { |
| 362 | alen--; |
| 363 | /* high byte address going out first */ |
| 364 | writel((addr >> (alen * 8)) & 0xff, |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 365 | &i2c_base->ic_cmd_data); |
Chin Liang See | a0c2626 | 2014-02-04 11:56:23 -0600 | [diff] [blame] | 366 | } |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 367 | return 0; |
| 368 | } |
| 369 | |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 370 | static int i2c_xfer_finish(struct i2c_regs *i2c_base) |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 371 | { |
| 372 | ulong start_stop_det = get_timer(0); |
| 373 | |
| 374 | while (1) { |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 375 | if ((readl(&i2c_base->ic_raw_intr_stat) & IC_STOP_DET)) { |
| 376 | readl(&i2c_base->ic_clr_stop_det); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 377 | break; |
| 378 | } else if (get_timer(start_stop_det) > I2C_STOPDET_TO) { |
| 379 | break; |
| 380 | } |
| 381 | } |
| 382 | |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 383 | if (i2c_wait_for_bb(i2c_base)) { |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 384 | printf("Timed out waiting for bus\n"); |
| 385 | return 1; |
| 386 | } |
| 387 | |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 388 | i2c_flush_rxfifo(i2c_base); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 389 | |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | /* |
| 394 | * i2c_read - Read from i2c memory |
| 395 | * @chip: target i2c address |
| 396 | * @addr: address to read from |
| 397 | * @alen: |
| 398 | * @buffer: buffer for read data |
| 399 | * @len: no of bytes to be read |
| 400 | * |
| 401 | * Read from i2c memory. |
| 402 | */ |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 403 | static int __dw_i2c_read(struct i2c_regs *i2c_base, u8 dev, uint addr, |
| 404 | int alen, u8 *buffer, int len) |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 405 | { |
| 406 | unsigned long start_time_rx; |
Marek Vasut | c4bc9a8 | 2016-10-20 16:48:28 +0200 | [diff] [blame] | 407 | unsigned int active = 0; |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 408 | |
Alexey Brodkin | 7ef0036 | 2013-12-16 15:30:35 +0400 | [diff] [blame] | 409 | #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW |
| 410 | /* |
| 411 | * EEPROM chips that implement "address overflow" are ones |
| 412 | * like Catalyst 24WC04/08/16 which has 9/10/11 bits of |
| 413 | * address and the extra bits end up in the "chip address" |
| 414 | * bit slots. This makes a 24WC08 (1Kbyte) chip look like |
| 415 | * four 256 byte chips. |
| 416 | * |
| 417 | * Note that we consider the length of the address field to |
| 418 | * still be one byte because the extra address bits are |
| 419 | * hidden in the chip address. |
| 420 | */ |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 421 | dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW); |
Alexey Brodkin | 7ef0036 | 2013-12-16 15:30:35 +0400 | [diff] [blame] | 422 | addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8)); |
| 423 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 424 | debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev, |
Alexey Brodkin | 7ef0036 | 2013-12-16 15:30:35 +0400 | [diff] [blame] | 425 | addr); |
| 426 | #endif |
| 427 | |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 428 | if (i2c_xfer_init(i2c_base, dev, addr, alen)) |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 429 | return 1; |
| 430 | |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 431 | start_time_rx = get_timer(0); |
| 432 | while (len) { |
Marek Vasut | c4bc9a8 | 2016-10-20 16:48:28 +0200 | [diff] [blame] | 433 | if (!active) { |
| 434 | /* |
| 435 | * Avoid writing to ic_cmd_data multiple times |
| 436 | * in case this loop spins too quickly and the |
| 437 | * ic_status RFNE bit isn't set after the first |
| 438 | * write. Subsequent writes to ic_cmd_data can |
| 439 | * trigger spurious i2c transfer. |
| 440 | */ |
| 441 | if (len == 1) |
| 442 | writel(IC_CMD | IC_STOP, &i2c_base->ic_cmd_data); |
| 443 | else |
| 444 | writel(IC_CMD, &i2c_base->ic_cmd_data); |
| 445 | active = 1; |
| 446 | } |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 447 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 448 | if (readl(&i2c_base->ic_status) & IC_STATUS_RFNE) { |
| 449 | *buffer++ = (uchar)readl(&i2c_base->ic_cmd_data); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 450 | len--; |
| 451 | start_time_rx = get_timer(0); |
Marek Vasut | c4bc9a8 | 2016-10-20 16:48:28 +0200 | [diff] [blame] | 452 | active = 0; |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 453 | } else if (get_timer(start_time_rx) > I2C_BYTE_TO) { |
Marek Vasut | c4bc9a8 | 2016-10-20 16:48:28 +0200 | [diff] [blame] | 454 | return 1; |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 458 | return i2c_xfer_finish(i2c_base); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | /* |
| 462 | * i2c_write - Write to i2c memory |
| 463 | * @chip: target i2c address |
| 464 | * @addr: address to read from |
| 465 | * @alen: |
| 466 | * @buffer: buffer for read data |
| 467 | * @len: no of bytes to be read |
| 468 | * |
| 469 | * Write to i2c memory. |
| 470 | */ |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 471 | static int __dw_i2c_write(struct i2c_regs *i2c_base, u8 dev, uint addr, |
| 472 | int alen, u8 *buffer, int len) |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 473 | { |
| 474 | int nb = len; |
| 475 | unsigned long start_time_tx; |
| 476 | |
Alexey Brodkin | 7ef0036 | 2013-12-16 15:30:35 +0400 | [diff] [blame] | 477 | #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW |
| 478 | /* |
| 479 | * EEPROM chips that implement "address overflow" are ones |
| 480 | * like Catalyst 24WC04/08/16 which has 9/10/11 bits of |
| 481 | * address and the extra bits end up in the "chip address" |
| 482 | * bit slots. This makes a 24WC08 (1Kbyte) chip look like |
| 483 | * four 256 byte chips. |
| 484 | * |
| 485 | * Note that we consider the length of the address field to |
| 486 | * still be one byte because the extra address bits are |
| 487 | * hidden in the chip address. |
| 488 | */ |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 489 | dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW); |
Alexey Brodkin | 7ef0036 | 2013-12-16 15:30:35 +0400 | [diff] [blame] | 490 | addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8)); |
| 491 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 492 | debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev, |
Alexey Brodkin | 7ef0036 | 2013-12-16 15:30:35 +0400 | [diff] [blame] | 493 | addr); |
| 494 | #endif |
| 495 | |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 496 | if (i2c_xfer_init(i2c_base, dev, addr, alen)) |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 497 | return 1; |
| 498 | |
| 499 | start_time_tx = get_timer(0); |
| 500 | while (len) { |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 501 | if (readl(&i2c_base->ic_status) & IC_STATUS_TFNF) { |
| 502 | if (--len == 0) { |
| 503 | writel(*buffer | IC_STOP, |
| 504 | &i2c_base->ic_cmd_data); |
| 505 | } else { |
| 506 | writel(*buffer, &i2c_base->ic_cmd_data); |
| 507 | } |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 508 | buffer++; |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 509 | start_time_tx = get_timer(0); |
| 510 | |
| 511 | } else if (get_timer(start_time_tx) > (nb * I2C_BYTE_TO)) { |
| 512 | printf("Timed out. i2c write Failed\n"); |
| 513 | return 1; |
| 514 | } |
| 515 | } |
| 516 | |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 517 | return i2c_xfer_finish(i2c_base); |
| 518 | } |
| 519 | |
Stefan Roese | 3cb2796 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 520 | /* |
| 521 | * __dw_i2c_init - Init function |
| 522 | * @speed: required i2c speed |
| 523 | * @slaveaddr: slave address for the device |
| 524 | * |
| 525 | * Initialization function. |
| 526 | */ |
Simon Glass | bd9ca8d | 2019-02-16 20:24:39 -0700 | [diff] [blame] | 527 | static int __dw_i2c_init(struct i2c_regs *i2c_base, int speed, int slaveaddr) |
Stefan Roese | 3cb2796 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 528 | { |
Simon Glass | bd9ca8d | 2019-02-16 20:24:39 -0700 | [diff] [blame] | 529 | int ret; |
| 530 | |
Stefan Roese | 3cb2796 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 531 | /* Disable i2c */ |
Simon Glass | bd9ca8d | 2019-02-16 20:24:39 -0700 | [diff] [blame] | 532 | ret = dw_i2c_enable(i2c_base, false); |
| 533 | if (ret) |
| 534 | return ret; |
Stefan Roese | 3cb2796 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 535 | |
Marek Vasut | 808aa13 | 2017-08-07 20:45:31 +0200 | [diff] [blame] | 536 | writel(IC_CON_SD | IC_CON_RE | IC_CON_SPD_FS | IC_CON_MM, |
| 537 | &i2c_base->ic_con); |
Stefan Roese | 3cb2796 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 538 | writel(IC_RX_TL, &i2c_base->ic_rx_tl); |
| 539 | writel(IC_TX_TL, &i2c_base->ic_tx_tl); |
| 540 | writel(IC_STOP_DET, &i2c_base->ic_intr_mask); |
| 541 | #ifndef CONFIG_DM_I2C |
Simon Glass | c529419 | 2020-01-23 11:48:25 -0700 | [diff] [blame] | 542 | _dw_i2c_set_bus_speed(NULL, i2c_base, speed, IC_CLK); |
Stefan Roese | 3cb2796 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 543 | writel(slaveaddr, &i2c_base->ic_sar); |
| 544 | #endif |
| 545 | |
| 546 | /* Enable i2c */ |
Simon Glass | bd9ca8d | 2019-02-16 20:24:39 -0700 | [diff] [blame] | 547 | ret = dw_i2c_enable(i2c_base, true); |
| 548 | if (ret) |
| 549 | return ret; |
| 550 | |
| 551 | return 0; |
Stefan Roese | 3cb2796 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | #ifndef CONFIG_DM_I2C |
| 555 | /* |
| 556 | * The legacy I2C functions. These need to get removed once |
| 557 | * all users of this driver are converted to DM. |
| 558 | */ |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 559 | static struct i2c_regs *i2c_get_base(struct i2c_adapter *adap) |
| 560 | { |
| 561 | switch (adap->hwadapnr) { |
| 562 | #if CONFIG_SYS_I2C_BUS_MAX >= 4 |
| 563 | case 3: |
| 564 | return (struct i2c_regs *)CONFIG_SYS_I2C_BASE3; |
| 565 | #endif |
| 566 | #if CONFIG_SYS_I2C_BUS_MAX >= 3 |
| 567 | case 2: |
| 568 | return (struct i2c_regs *)CONFIG_SYS_I2C_BASE2; |
| 569 | #endif |
| 570 | #if CONFIG_SYS_I2C_BUS_MAX >= 2 |
| 571 | case 1: |
| 572 | return (struct i2c_regs *)CONFIG_SYS_I2C_BASE1; |
| 573 | #endif |
| 574 | case 0: |
| 575 | return (struct i2c_regs *)CONFIG_SYS_I2C_BASE; |
| 576 | default: |
| 577 | printf("Wrong I2C-adapter number %d\n", adap->hwadapnr); |
| 578 | } |
| 579 | |
| 580 | return NULL; |
| 581 | } |
| 582 | |
| 583 | static unsigned int dw_i2c_set_bus_speed(struct i2c_adapter *adap, |
| 584 | unsigned int speed) |
| 585 | { |
| 586 | adap->speed = speed; |
Simon Glass | c529419 | 2020-01-23 11:48:25 -0700 | [diff] [blame] | 587 | return _dw_i2c_set_bus_speed(NULL, i2c_get_base(adap), speed, IC_CLK); |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 588 | } |
| 589 | |
Stefan Roese | 3cb2796 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 590 | static void dw_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr) |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 591 | { |
Stefan Roese | 3cb2796 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 592 | __dw_i2c_init(i2c_get_base(adap), speed, slaveaddr); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 593 | } |
| 594 | |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 595 | static int dw_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr, |
| 596 | int alen, u8 *buffer, int len) |
| 597 | { |
| 598 | return __dw_i2c_read(i2c_get_base(adap), dev, addr, alen, buffer, len); |
| 599 | } |
| 600 | |
| 601 | static int dw_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr, |
| 602 | int alen, u8 *buffer, int len) |
| 603 | { |
| 604 | return __dw_i2c_write(i2c_get_base(adap), dev, addr, alen, buffer, len); |
| 605 | } |
| 606 | |
Stefan Roese | 3cb2796 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 607 | /* dw_i2c_probe - Probe the i2c chip */ |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 608 | static int dw_i2c_probe(struct i2c_adapter *adap, u8 dev) |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 609 | { |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 610 | struct i2c_regs *i2c_base = i2c_get_base(adap); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 611 | u32 tmp; |
Stefan Roese | f6322ebd | 2012-01-20 11:52:33 +0100 | [diff] [blame] | 612 | int ret; |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 613 | |
| 614 | /* |
| 615 | * Try to read the first location of the chip. |
| 616 | */ |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 617 | ret = __dw_i2c_read(i2c_base, dev, 0, 1, (uchar *)&tmp, 1); |
Stefan Roese | f6322ebd | 2012-01-20 11:52:33 +0100 | [diff] [blame] | 618 | if (ret) |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 619 | dw_i2c_init(adap, adap->speed, adap->slaveaddr); |
Stefan Roese | f6322ebd | 2012-01-20 11:52:33 +0100 | [diff] [blame] | 620 | |
| 621 | return ret; |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 622 | } |
Armando Visconti | 4a7b4ec | 2012-12-06 00:04:15 +0000 | [diff] [blame] | 623 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 624 | U_BOOT_I2C_ADAP_COMPLETE(dw_0, dw_i2c_init, dw_i2c_probe, dw_i2c_read, |
| 625 | dw_i2c_write, dw_i2c_set_bus_speed, |
| 626 | CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0) |
Armando Visconti | 4a7b4ec | 2012-12-06 00:04:15 +0000 | [diff] [blame] | 627 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 628 | #if CONFIG_SYS_I2C_BUS_MAX >= 2 |
| 629 | U_BOOT_I2C_ADAP_COMPLETE(dw_1, dw_i2c_init, dw_i2c_probe, dw_i2c_read, |
| 630 | dw_i2c_write, dw_i2c_set_bus_speed, |
| 631 | CONFIG_SYS_I2C_SPEED1, CONFIG_SYS_I2C_SLAVE1, 1) |
| 632 | #endif |
Armando Visconti | 4a7b4ec | 2012-12-06 00:04:15 +0000 | [diff] [blame] | 633 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 634 | #if CONFIG_SYS_I2C_BUS_MAX >= 3 |
| 635 | U_BOOT_I2C_ADAP_COMPLETE(dw_2, dw_i2c_init, dw_i2c_probe, dw_i2c_read, |
| 636 | dw_i2c_write, dw_i2c_set_bus_speed, |
| 637 | CONFIG_SYS_I2C_SPEED2, CONFIG_SYS_I2C_SLAVE2, 2) |
| 638 | #endif |
Armando Visconti | 4a7b4ec | 2012-12-06 00:04:15 +0000 | [diff] [blame] | 639 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 640 | #if CONFIG_SYS_I2C_BUS_MAX >= 4 |
| 641 | U_BOOT_I2C_ADAP_COMPLETE(dw_3, dw_i2c_init, dw_i2c_probe, dw_i2c_read, |
| 642 | dw_i2c_write, dw_i2c_set_bus_speed, |
| 643 | CONFIG_SYS_I2C_SPEED3, CONFIG_SYS_I2C_SLAVE3, 3) |
Armando Visconti | 4a7b4ec | 2012-12-06 00:04:15 +0000 | [diff] [blame] | 644 | #endif |
Stefan Roese | 3cb2796 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 645 | |
| 646 | #else /* CONFIG_DM_I2C */ |
| 647 | /* The DM I2C functions */ |
| 648 | |
| 649 | static int designware_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, |
| 650 | int nmsgs) |
| 651 | { |
| 652 | struct dw_i2c *i2c = dev_get_priv(bus); |
| 653 | int ret; |
| 654 | |
| 655 | debug("i2c_xfer: %d messages\n", nmsgs); |
| 656 | for (; nmsgs > 0; nmsgs--, msg++) { |
| 657 | debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len); |
| 658 | if (msg->flags & I2C_M_RD) { |
| 659 | ret = __dw_i2c_read(i2c->regs, msg->addr, 0, 0, |
| 660 | msg->buf, msg->len); |
| 661 | } else { |
| 662 | ret = __dw_i2c_write(i2c->regs, msg->addr, 0, 0, |
| 663 | msg->buf, msg->len); |
| 664 | } |
| 665 | if (ret) { |
| 666 | debug("i2c_write: error sending\n"); |
| 667 | return -EREMOTEIO; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | return 0; |
| 672 | } |
| 673 | |
| 674 | static int designware_i2c_set_bus_speed(struct udevice *bus, unsigned int speed) |
| 675 | { |
| 676 | struct dw_i2c *i2c = dev_get_priv(bus); |
Ley Foon Tan | 6e85c81 | 2019-06-12 09:48:04 +0800 | [diff] [blame] | 677 | ulong rate; |
| 678 | |
| 679 | #if CONFIG_IS_ENABLED(CLK) |
| 680 | rate = clk_get_rate(&i2c->clk); |
| 681 | if (IS_ERR_VALUE(rate)) |
| 682 | return -EINVAL; |
Ley Foon Tan | 6e85c81 | 2019-06-12 09:48:04 +0800 | [diff] [blame] | 683 | #else |
| 684 | rate = IC_CLK; |
| 685 | #endif |
Simon Glass | c529419 | 2020-01-23 11:48:25 -0700 | [diff] [blame] | 686 | return _dw_i2c_set_bus_speed(i2c, i2c->regs, speed, rate); |
Stefan Roese | 3cb2796 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | static int designware_i2c_probe_chip(struct udevice *bus, uint chip_addr, |
| 690 | uint chip_flags) |
| 691 | { |
| 692 | struct dw_i2c *i2c = dev_get_priv(bus); |
| 693 | struct i2c_regs *i2c_base = i2c->regs; |
| 694 | u32 tmp; |
| 695 | int ret; |
| 696 | |
| 697 | /* Try to read the first location of the chip */ |
| 698 | ret = __dw_i2c_read(i2c_base, chip_addr, 0, 1, (uchar *)&tmp, 1); |
| 699 | if (ret) |
| 700 | __dw_i2c_init(i2c_base, 0, 0); |
| 701 | |
| 702 | return ret; |
| 703 | } |
| 704 | |
Simon Glass | 9e5d174 | 2020-01-23 11:48:11 -0700 | [diff] [blame] | 705 | int designware_i2c_ofdata_to_platdata(struct udevice *bus) |
Stefan Roese | 3cb2796 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 706 | { |
| 707 | struct dw_i2c *priv = dev_get_priv(bus); |
Simon Glass | 8de5ae8 | 2020-01-23 11:48:26 -0700 | [diff] [blame] | 708 | int ret; |
Stefan Roese | 3cb2796 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 709 | |
Simon Glass | 9e5d174 | 2020-01-23 11:48:11 -0700 | [diff] [blame] | 710 | if (!priv->regs) |
| 711 | priv->regs = (struct i2c_regs *)devfdt_get_addr_ptr(bus); |
| 712 | dev_read_u32(bus, "i2c-scl-rising-time-ns", &priv->scl_rise_time_ns); |
| 713 | dev_read_u32(bus, "i2c-scl-falling-time-ns", &priv->scl_fall_time_ns); |
| 714 | dev_read_u32(bus, "i2c-sda-hold-time-ns", &priv->sda_hold_time_ns); |
Simon Glass | e2be553 | 2019-12-06 21:41:40 -0700 | [diff] [blame] | 715 | |
Simon Goldschmidt | 28608a1 | 2019-03-28 21:11:48 +0100 | [diff] [blame] | 716 | ret = reset_get_bulk(bus, &priv->resets); |
Dinh Nguyen | 08794aa | 2018-04-04 17:18:24 -0500 | [diff] [blame] | 717 | if (ret) |
Simon Goldschmidt | 28608a1 | 2019-03-28 21:11:48 +0100 | [diff] [blame] | 718 | dev_warn(bus, "Can't get reset: %d\n", ret); |
| 719 | else |
| 720 | reset_deassert_bulk(&priv->resets); |
Dinh Nguyen | 08794aa | 2018-04-04 17:18:24 -0500 | [diff] [blame] | 721 | |
Ley Foon Tan | 6e85c81 | 2019-06-12 09:48:04 +0800 | [diff] [blame] | 722 | #if CONFIG_IS_ENABLED(CLK) |
| 723 | ret = clk_get_by_index(bus, 0, &priv->clk); |
| 724 | if (ret) |
| 725 | return ret; |
| 726 | |
| 727 | ret = clk_enable(&priv->clk); |
| 728 | if (ret && ret != -ENOSYS && ret != -ENOTSUPP) { |
| 729 | clk_free(&priv->clk); |
| 730 | dev_err(bus, "failed to enable clock\n"); |
| 731 | return ret; |
| 732 | } |
| 733 | #endif |
| 734 | |
Simon Glass | 8de5ae8 | 2020-01-23 11:48:26 -0700 | [diff] [blame] | 735 | return 0; |
| 736 | } |
| 737 | |
| 738 | int designware_i2c_probe(struct udevice *bus) |
| 739 | { |
| 740 | struct dw_i2c *priv = dev_get_priv(bus); |
| 741 | |
Simon Glass | bd9ca8d | 2019-02-16 20:24:39 -0700 | [diff] [blame] | 742 | return __dw_i2c_init(priv->regs, 0, 0); |
Stefan Roese | 3cb2796 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 743 | } |
| 744 | |
Simon Glass | e2be553 | 2019-12-06 21:41:40 -0700 | [diff] [blame] | 745 | int designware_i2c_remove(struct udevice *dev) |
Simon Goldschmidt | 28608a1 | 2019-03-28 21:11:48 +0100 | [diff] [blame] | 746 | { |
| 747 | struct dw_i2c *priv = dev_get_priv(dev); |
| 748 | |
Ley Foon Tan | 6e85c81 | 2019-06-12 09:48:04 +0800 | [diff] [blame] | 749 | #if CONFIG_IS_ENABLED(CLK) |
| 750 | clk_disable(&priv->clk); |
| 751 | clk_free(&priv->clk); |
| 752 | #endif |
| 753 | |
Simon Goldschmidt | 28608a1 | 2019-03-28 21:11:48 +0100 | [diff] [blame] | 754 | return reset_release_bulk(&priv->resets); |
| 755 | } |
| 756 | |
Simon Glass | e2be553 | 2019-12-06 21:41:40 -0700 | [diff] [blame] | 757 | const struct dm_i2c_ops designware_i2c_ops = { |
Stefan Roese | 3cb2796 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 758 | .xfer = designware_i2c_xfer, |
| 759 | .probe_chip = designware_i2c_probe_chip, |
| 760 | .set_bus_speed = designware_i2c_set_bus_speed, |
| 761 | }; |
| 762 | |
| 763 | static const struct udevice_id designware_i2c_ids[] = { |
| 764 | { .compatible = "snps,designware-i2c" }, |
| 765 | { } |
| 766 | }; |
| 767 | |
| 768 | U_BOOT_DRIVER(i2c_designware) = { |
| 769 | .name = "i2c_designware", |
| 770 | .id = UCLASS_I2C, |
| 771 | .of_match = designware_i2c_ids, |
Simon Glass | e2be553 | 2019-12-06 21:41:40 -0700 | [diff] [blame] | 772 | .ofdata_to_platdata = designware_i2c_ofdata_to_platdata, |
Stefan Roese | 3cb2796 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 773 | .probe = designware_i2c_probe, |
| 774 | .priv_auto_alloc_size = sizeof(struct dw_i2c), |
Simon Goldschmidt | 28608a1 | 2019-03-28 21:11:48 +0100 | [diff] [blame] | 775 | .remove = designware_i2c_remove, |
Simon Glass | e2be553 | 2019-12-06 21:41:40 -0700 | [diff] [blame] | 776 | .flags = DM_FLAG_OS_PREPARE, |
Stefan Roese | 3cb2796 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 777 | .ops = &designware_i2c_ops, |
| 778 | }; |
| 779 | |
| 780 | #endif /* CONFIG_DM_I2C */ |