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