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