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