Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Vipin KUMAR | 2403f8f | 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 | 2403f8f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | afb8865 | 2020-01-23 11:48:06 -0700 | [diff] [blame] | 8 | #include <clk.h> |
Stefan Roese | 334b9b0 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 9 | #include <dm.h> |
Stefan Roese | 678398b | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 10 | #include <i2c.h> |
Stefan Roese | ba5da55 | 2016-04-21 08:19:42 +0200 | [diff] [blame] | 11 | #include <pci.h> |
Dinh Nguyen | 622597d | 2018-04-04 17:18:24 -0500 | [diff] [blame] | 12 | #include <reset.h> |
Vipin KUMAR | 2403f8f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 13 | #include <asm/io.h> |
Vipin KUMAR | 031ed2f | 2012-02-26 23:13:29 +0000 | [diff] [blame] | 14 | #include "designware_i2c.h" |
Vipin KUMAR | 2403f8f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 15 | |
Stefan Roese | b6a77b0 | 2016-04-27 09:02:12 +0200 | [diff] [blame] | 16 | #ifdef CONFIG_SYS_I2C_DW_ENABLE_STATUS_UNSUPPORTED |
Simon Glass | 2b5d029 | 2019-02-16 20:24:39 -0700 | [diff] [blame] | 17 | static int dw_i2c_enable(struct i2c_regs *i2c_base, bool enable) |
Stefan Roese | b6a77b0 | 2016-04-27 09:02:12 +0200 | [diff] [blame] | 18 | { |
| 19 | u32 ena = enable ? IC_ENABLE_0B : 0; |
| 20 | |
| 21 | writel(ena, &i2c_base->ic_enable); |
Simon Glass | 2b5d029 | 2019-02-16 20:24:39 -0700 | [diff] [blame] | 22 | |
| 23 | return 0; |
Stefan Roese | b6a77b0 | 2016-04-27 09:02:12 +0200 | [diff] [blame] | 24 | } |
| 25 | #else |
Simon Glass | 2b5d029 | 2019-02-16 20:24:39 -0700 | [diff] [blame] | 26 | static int dw_i2c_enable(struct i2c_regs *i2c_base, bool enable) |
Stefan Roese | 1c8b089 | 2016-04-21 08:19:38 +0200 | [diff] [blame] | 27 | { |
| 28 | u32 ena = enable ? IC_ENABLE_0B : 0; |
| 29 | int timeout = 100; |
| 30 | |
| 31 | do { |
| 32 | writel(ena, &i2c_base->ic_enable); |
| 33 | if ((readl(&i2c_base->ic_enable_status) & IC_ENABLE_0B) == ena) |
Simon Glass | 2b5d029 | 2019-02-16 20:24:39 -0700 | [diff] [blame] | 34 | return 0; |
Stefan Roese | 1c8b089 | 2016-04-21 08:19:38 +0200 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * Wait 10 times the signaling period of the highest I2C |
| 38 | * transfer supported by the driver (for 400KHz this is |
| 39 | * 25us) as described in the DesignWare I2C databook. |
| 40 | */ |
| 41 | udelay(25); |
| 42 | } while (timeout--); |
Stefan Roese | 1c8b089 | 2016-04-21 08:19:38 +0200 | [diff] [blame] | 43 | printf("timeout in %sabling I2C adapter\n", enable ? "en" : "dis"); |
Simon Glass | 2b5d029 | 2019-02-16 20:24:39 -0700 | [diff] [blame] | 44 | |
| 45 | return -ETIMEDOUT; |
Stefan Roese | 1c8b089 | 2016-04-21 08:19:38 +0200 | [diff] [blame] | 46 | } |
Stefan Roese | b6a77b0 | 2016-04-27 09:02:12 +0200 | [diff] [blame] | 47 | #endif |
Stefan Roese | 1c8b089 | 2016-04-21 08:19:38 +0200 | [diff] [blame] | 48 | |
Vipin KUMAR | 2403f8f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 49 | /* |
Stefan Roese | 11b544a | 2016-04-21 08:19:39 +0200 | [diff] [blame] | 50 | * i2c_set_bus_speed - Set the i2c speed |
| 51 | * @speed: required i2c speed |
Vipin KUMAR | 2403f8f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 52 | * |
Stefan Roese | 11b544a | 2016-04-21 08:19:39 +0200 | [diff] [blame] | 53 | * Set the i2c speed. |
Vipin KUMAR | 2403f8f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 54 | */ |
Simon Glass | d22409e | 2020-01-23 11:48:12 -0700 | [diff] [blame^] | 55 | static unsigned int __dw_i2c_set_bus_speed(struct dw_i2c *priv, |
| 56 | struct i2c_regs *i2c_base, |
Ley Foon Tan | 2d1e879 | 2019-06-12 09:48:04 +0800 | [diff] [blame] | 57 | unsigned int speed, |
Simon Glass | dd3c160 | 2020-01-23 11:48:09 -0700 | [diff] [blame] | 58 | unsigned int bus_clk) |
Vipin KUMAR | 2403f8f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 59 | { |
Simon Glass | d22409e | 2020-01-23 11:48:12 -0700 | [diff] [blame^] | 60 | const struct dw_scl_sda_cfg *scl_sda_cfg = NULL; |
Simon Glass | dd3c160 | 2020-01-23 11:48:09 -0700 | [diff] [blame] | 61 | ulong bus_khz = bus_clk / 1000; |
Simon Glass | 65190d1 | 2020-01-23 11:48:08 -0700 | [diff] [blame] | 62 | enum i2c_speed_mode i2c_spd; |
Vipin KUMAR | 2403f8f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 63 | unsigned int cntl; |
| 64 | unsigned int hcnt, lcnt; |
Jun Chen | e3b93dc | 2019-06-05 15:23:16 +0800 | [diff] [blame] | 65 | unsigned int ena; |
Stefan Roese | 11b544a | 2016-04-21 08:19:39 +0200 | [diff] [blame] | 66 | |
Simon Glass | d22409e | 2020-01-23 11:48:12 -0700 | [diff] [blame^] | 67 | if (priv) |
| 68 | scl_sda_cfg = priv->scl_sda_cfg; |
Simon Glass | 6db7943 | 2020-01-23 11:48:07 -0700 | [diff] [blame] | 69 | /* Allow high speed if there is no config, or the config allows it */ |
| 70 | if (speed >= I2C_HIGH_SPEED && |
| 71 | (!scl_sda_cfg || scl_sda_cfg->has_high_speed)) |
| 72 | i2c_spd = IC_SPEED_MODE_HIGH; |
Stefan Roese | 11b544a | 2016-04-21 08:19:39 +0200 | [diff] [blame] | 73 | else if (speed >= I2C_FAST_SPEED) |
| 74 | i2c_spd = IC_SPEED_MODE_FAST; |
| 75 | else |
| 76 | i2c_spd = IC_SPEED_MODE_STANDARD; |
Armando Visconti | 5e3e8dd | 2012-03-29 20:10:17 +0000 | [diff] [blame] | 77 | |
Jun Chen | e3b93dc | 2019-06-05 15:23:16 +0800 | [diff] [blame] | 78 | /* Get enable setting for restore later */ |
| 79 | ena = readl(&i2c_base->ic_enable) & IC_ENABLE_0B; |
| 80 | |
Armando Visconti | 5e3e8dd | 2012-03-29 20:10:17 +0000 | [diff] [blame] | 81 | /* to set speed cltr must be disabled */ |
Stefan Roese | 1c8b089 | 2016-04-21 08:19:38 +0200 | [diff] [blame] | 82 | dw_i2c_enable(i2c_base, false); |
Armando Visconti | 5e3e8dd | 2012-03-29 20:10:17 +0000 | [diff] [blame] | 83 | |
Stefan Roese | 678398b | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 84 | cntl = (readl(&i2c_base->ic_con) & (~IC_CON_SPD_MSK)); |
Vipin KUMAR | 2403f8f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 85 | |
| 86 | switch (i2c_spd) { |
Simon Glass | 6db7943 | 2020-01-23 11:48:07 -0700 | [diff] [blame] | 87 | case IC_SPEED_MODE_HIGH: |
Stefan Roese | ba5da55 | 2016-04-21 08:19:42 +0200 | [diff] [blame] | 88 | cntl |= IC_CON_SPD_SS; |
| 89 | if (scl_sda_cfg) { |
| 90 | hcnt = scl_sda_cfg->fs_hcnt; |
| 91 | lcnt = scl_sda_cfg->fs_lcnt; |
| 92 | } else { |
Simon Glass | dd3c160 | 2020-01-23 11:48:09 -0700 | [diff] [blame] | 93 | hcnt = (bus_khz * MIN_HS_SCL_HIGHTIME) / NANO_TO_KILO; |
| 94 | lcnt = (bus_khz * MIN_HS_SCL_LOWTIME) / NANO_TO_KILO; |
Stefan Roese | ba5da55 | 2016-04-21 08:19:42 +0200 | [diff] [blame] | 95 | } |
Stefan Roese | 678398b | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 96 | writel(hcnt, &i2c_base->ic_hs_scl_hcnt); |
Stefan Roese | 678398b | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 97 | writel(lcnt, &i2c_base->ic_hs_scl_lcnt); |
Vipin KUMAR | 2403f8f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 98 | break; |
| 99 | |
| 100 | case IC_SPEED_MODE_STANDARD: |
| 101 | cntl |= IC_CON_SPD_SS; |
Stefan Roese | ba5da55 | 2016-04-21 08:19:42 +0200 | [diff] [blame] | 102 | if (scl_sda_cfg) { |
| 103 | hcnt = scl_sda_cfg->ss_hcnt; |
| 104 | lcnt = scl_sda_cfg->ss_lcnt; |
| 105 | } else { |
Simon Glass | dd3c160 | 2020-01-23 11:48:09 -0700 | [diff] [blame] | 106 | hcnt = (bus_khz * MIN_SS_SCL_HIGHTIME) / NANO_TO_KILO; |
| 107 | lcnt = (bus_khz * MIN_SS_SCL_LOWTIME) / NANO_TO_KILO; |
Stefan Roese | ba5da55 | 2016-04-21 08:19:42 +0200 | [diff] [blame] | 108 | } |
Stefan Roese | 678398b | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 109 | writel(hcnt, &i2c_base->ic_ss_scl_hcnt); |
Stefan Roese | 678398b | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 110 | writel(lcnt, &i2c_base->ic_ss_scl_lcnt); |
Vipin KUMAR | 2403f8f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 111 | break; |
| 112 | |
| 113 | case IC_SPEED_MODE_FAST: |
| 114 | default: |
| 115 | cntl |= IC_CON_SPD_FS; |
Stefan Roese | ba5da55 | 2016-04-21 08:19:42 +0200 | [diff] [blame] | 116 | if (scl_sda_cfg) { |
| 117 | hcnt = scl_sda_cfg->fs_hcnt; |
| 118 | lcnt = scl_sda_cfg->fs_lcnt; |
| 119 | } else { |
Simon Glass | dd3c160 | 2020-01-23 11:48:09 -0700 | [diff] [blame] | 120 | hcnt = (bus_khz * MIN_FS_SCL_HIGHTIME) / NANO_TO_KILO; |
| 121 | lcnt = (bus_khz * MIN_FS_SCL_LOWTIME) / NANO_TO_KILO; |
Stefan Roese | ba5da55 | 2016-04-21 08:19:42 +0200 | [diff] [blame] | 122 | } |
Stefan Roese | 678398b | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 123 | writel(hcnt, &i2c_base->ic_fs_scl_hcnt); |
Stefan Roese | 678398b | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 124 | writel(lcnt, &i2c_base->ic_fs_scl_lcnt); |
Vipin KUMAR | 2403f8f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 125 | break; |
| 126 | } |
| 127 | |
Stefan Roese | 678398b | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 128 | writel(cntl, &i2c_base->ic_con); |
Vipin KUMAR | 2403f8f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 129 | |
Stefan Roese | ba5da55 | 2016-04-21 08:19:42 +0200 | [diff] [blame] | 130 | /* Configure SDA Hold Time if required */ |
| 131 | if (scl_sda_cfg) |
| 132 | writel(scl_sda_cfg->sda_hold, &i2c_base->ic_sda_hold); |
| 133 | |
Jun Chen | e3b93dc | 2019-06-05 15:23:16 +0800 | [diff] [blame] | 134 | /* Restore back i2c now speed set */ |
| 135 | if (ena == IC_ENABLE_0B) |
| 136 | dw_i2c_enable(i2c_base, true); |
Vipin KUMAR | 2403f8f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 137 | |
Stefan Roese | 3f4358d | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * i2c_setaddress - Sets the target slave address |
| 143 | * @i2c_addr: target i2c address |
| 144 | * |
| 145 | * Sets the target slave address. |
| 146 | */ |
| 147 | static void i2c_setaddress(struct i2c_regs *i2c_base, unsigned int i2c_addr) |
| 148 | { |
| 149 | /* Disable i2c */ |
| 150 | dw_i2c_enable(i2c_base, false); |
| 151 | |
| 152 | writel(i2c_addr, &i2c_base->ic_tar); |
| 153 | |
| 154 | /* Enable i2c */ |
| 155 | dw_i2c_enable(i2c_base, true); |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * i2c_flush_rxfifo - Flushes the i2c RX FIFO |
| 160 | * |
| 161 | * Flushes the i2c RX FIFO |
| 162 | */ |
| 163 | static void i2c_flush_rxfifo(struct i2c_regs *i2c_base) |
| 164 | { |
| 165 | while (readl(&i2c_base->ic_status) & IC_STATUS_RFNE) |
| 166 | readl(&i2c_base->ic_cmd_data); |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * i2c_wait_for_bb - Waits for bus busy |
| 171 | * |
| 172 | * Waits for bus busy |
| 173 | */ |
| 174 | static int i2c_wait_for_bb(struct i2c_regs *i2c_base) |
| 175 | { |
| 176 | unsigned long start_time_bb = get_timer(0); |
| 177 | |
| 178 | while ((readl(&i2c_base->ic_status) & IC_STATUS_MA) || |
| 179 | !(readl(&i2c_base->ic_status) & IC_STATUS_TFE)) { |
| 180 | |
| 181 | /* Evaluate timeout */ |
| 182 | if (get_timer(start_time_bb) > (unsigned long)(I2C_BYTE_TO_BB)) |
| 183 | return 1; |
| 184 | } |
Vipin KUMAR | 2403f8f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
Stefan Roese | 3f4358d | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 189 | static int i2c_xfer_init(struct i2c_regs *i2c_base, uchar chip, uint addr, |
| 190 | int alen) |
| 191 | { |
| 192 | if (i2c_wait_for_bb(i2c_base)) |
| 193 | return 1; |
| 194 | |
| 195 | i2c_setaddress(i2c_base, chip); |
| 196 | while (alen) { |
| 197 | alen--; |
| 198 | /* high byte address going out first */ |
| 199 | writel((addr >> (alen * 8)) & 0xff, |
| 200 | &i2c_base->ic_cmd_data); |
| 201 | } |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | static int i2c_xfer_finish(struct i2c_regs *i2c_base) |
| 206 | { |
| 207 | ulong start_stop_det = get_timer(0); |
| 208 | |
| 209 | while (1) { |
| 210 | if ((readl(&i2c_base->ic_raw_intr_stat) & IC_STOP_DET)) { |
| 211 | readl(&i2c_base->ic_clr_stop_det); |
| 212 | break; |
| 213 | } else if (get_timer(start_stop_det) > I2C_STOPDET_TO) { |
| 214 | break; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | if (i2c_wait_for_bb(i2c_base)) { |
| 219 | printf("Timed out waiting for bus\n"); |
| 220 | return 1; |
| 221 | } |
| 222 | |
| 223 | i2c_flush_rxfifo(i2c_base); |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | /* |
| 229 | * i2c_read - Read from i2c memory |
| 230 | * @chip: target i2c address |
| 231 | * @addr: address to read from |
| 232 | * @alen: |
| 233 | * @buffer: buffer for read data |
| 234 | * @len: no of bytes to be read |
| 235 | * |
| 236 | * Read from i2c memory. |
| 237 | */ |
| 238 | static int __dw_i2c_read(struct i2c_regs *i2c_base, u8 dev, uint addr, |
| 239 | int alen, u8 *buffer, int len) |
| 240 | { |
| 241 | unsigned long start_time_rx; |
Marek Vasut | b033808 | 2016-10-20 16:48:28 +0200 | [diff] [blame] | 242 | unsigned int active = 0; |
Stefan Roese | 3f4358d | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 243 | |
| 244 | #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW |
| 245 | /* |
| 246 | * EEPROM chips that implement "address overflow" are ones |
| 247 | * like Catalyst 24WC04/08/16 which has 9/10/11 bits of |
| 248 | * address and the extra bits end up in the "chip address" |
| 249 | * bit slots. This makes a 24WC08 (1Kbyte) chip look like |
| 250 | * four 256 byte chips. |
| 251 | * |
| 252 | * Note that we consider the length of the address field to |
| 253 | * still be one byte because the extra address bits are |
| 254 | * hidden in the chip address. |
| 255 | */ |
| 256 | dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW); |
| 257 | addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8)); |
| 258 | |
| 259 | debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev, |
| 260 | addr); |
| 261 | #endif |
| 262 | |
| 263 | if (i2c_xfer_init(i2c_base, dev, addr, alen)) |
| 264 | return 1; |
| 265 | |
| 266 | start_time_rx = get_timer(0); |
| 267 | while (len) { |
Marek Vasut | b033808 | 2016-10-20 16:48:28 +0200 | [diff] [blame] | 268 | if (!active) { |
| 269 | /* |
| 270 | * Avoid writing to ic_cmd_data multiple times |
| 271 | * in case this loop spins too quickly and the |
| 272 | * ic_status RFNE bit isn't set after the first |
| 273 | * write. Subsequent writes to ic_cmd_data can |
| 274 | * trigger spurious i2c transfer. |
| 275 | */ |
| 276 | if (len == 1) |
| 277 | writel(IC_CMD | IC_STOP, &i2c_base->ic_cmd_data); |
| 278 | else |
| 279 | writel(IC_CMD, &i2c_base->ic_cmd_data); |
| 280 | active = 1; |
| 281 | } |
Stefan Roese | 3f4358d | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 282 | |
| 283 | if (readl(&i2c_base->ic_status) & IC_STATUS_RFNE) { |
| 284 | *buffer++ = (uchar)readl(&i2c_base->ic_cmd_data); |
| 285 | len--; |
| 286 | start_time_rx = get_timer(0); |
Marek Vasut | b033808 | 2016-10-20 16:48:28 +0200 | [diff] [blame] | 287 | active = 0; |
Stefan Roese | 3f4358d | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 288 | } else if (get_timer(start_time_rx) > I2C_BYTE_TO) { |
Marek Vasut | b033808 | 2016-10-20 16:48:28 +0200 | [diff] [blame] | 289 | return 1; |
Stefan Roese | 3f4358d | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
| 293 | return i2c_xfer_finish(i2c_base); |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | * i2c_write - Write to i2c memory |
| 298 | * @chip: target i2c address |
| 299 | * @addr: address to read from |
| 300 | * @alen: |
| 301 | * @buffer: buffer for read data |
| 302 | * @len: no of bytes to be read |
| 303 | * |
| 304 | * Write to i2c memory. |
| 305 | */ |
| 306 | static int __dw_i2c_write(struct i2c_regs *i2c_base, u8 dev, uint addr, |
| 307 | int alen, u8 *buffer, int len) |
| 308 | { |
| 309 | int nb = len; |
| 310 | unsigned long start_time_tx; |
| 311 | |
| 312 | #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW |
| 313 | /* |
| 314 | * EEPROM chips that implement "address overflow" are ones |
| 315 | * like Catalyst 24WC04/08/16 which has 9/10/11 bits of |
| 316 | * address and the extra bits end up in the "chip address" |
| 317 | * bit slots. This makes a 24WC08 (1Kbyte) chip look like |
| 318 | * four 256 byte chips. |
| 319 | * |
| 320 | * Note that we consider the length of the address field to |
| 321 | * still be one byte because the extra address bits are |
| 322 | * hidden in the chip address. |
| 323 | */ |
| 324 | dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW); |
| 325 | addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8)); |
| 326 | |
| 327 | debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev, |
| 328 | addr); |
| 329 | #endif |
| 330 | |
| 331 | if (i2c_xfer_init(i2c_base, dev, addr, alen)) |
| 332 | return 1; |
| 333 | |
| 334 | start_time_tx = get_timer(0); |
| 335 | while (len) { |
| 336 | if (readl(&i2c_base->ic_status) & IC_STATUS_TFNF) { |
| 337 | if (--len == 0) { |
| 338 | writel(*buffer | IC_STOP, |
| 339 | &i2c_base->ic_cmd_data); |
| 340 | } else { |
| 341 | writel(*buffer, &i2c_base->ic_cmd_data); |
| 342 | } |
| 343 | buffer++; |
| 344 | start_time_tx = get_timer(0); |
| 345 | |
| 346 | } else if (get_timer(start_time_tx) > (nb * I2C_BYTE_TO)) { |
| 347 | printf("Timed out. i2c write Failed\n"); |
| 348 | return 1; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | return i2c_xfer_finish(i2c_base); |
| 353 | } |
| 354 | |
Stefan Roese | 334b9b0 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 355 | /* |
| 356 | * __dw_i2c_init - Init function |
| 357 | * @speed: required i2c speed |
| 358 | * @slaveaddr: slave address for the device |
| 359 | * |
| 360 | * Initialization function. |
| 361 | */ |
Simon Glass | 2b5d029 | 2019-02-16 20:24:39 -0700 | [diff] [blame] | 362 | static int __dw_i2c_init(struct i2c_regs *i2c_base, int speed, int slaveaddr) |
Stefan Roese | 334b9b0 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 363 | { |
Simon Glass | 2b5d029 | 2019-02-16 20:24:39 -0700 | [diff] [blame] | 364 | int ret; |
| 365 | |
Stefan Roese | 334b9b0 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 366 | /* Disable i2c */ |
Simon Glass | 2b5d029 | 2019-02-16 20:24:39 -0700 | [diff] [blame] | 367 | ret = dw_i2c_enable(i2c_base, false); |
| 368 | if (ret) |
| 369 | return ret; |
Stefan Roese | 334b9b0 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 370 | |
Marek Vasut | 014e47f | 2017-08-07 20:45:31 +0200 | [diff] [blame] | 371 | writel(IC_CON_SD | IC_CON_RE | IC_CON_SPD_FS | IC_CON_MM, |
| 372 | &i2c_base->ic_con); |
Stefan Roese | 334b9b0 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 373 | writel(IC_RX_TL, &i2c_base->ic_rx_tl); |
| 374 | writel(IC_TX_TL, &i2c_base->ic_tx_tl); |
| 375 | writel(IC_STOP_DET, &i2c_base->ic_intr_mask); |
| 376 | #ifndef CONFIG_DM_I2C |
Simon Glass | d22409e | 2020-01-23 11:48:12 -0700 | [diff] [blame^] | 377 | __dw_i2c_set_bus_speed(NULL, i2c_base, speed, IC_CLK); |
Stefan Roese | 334b9b0 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 378 | writel(slaveaddr, &i2c_base->ic_sar); |
| 379 | #endif |
| 380 | |
| 381 | /* Enable i2c */ |
Simon Glass | 2b5d029 | 2019-02-16 20:24:39 -0700 | [diff] [blame] | 382 | ret = dw_i2c_enable(i2c_base, true); |
| 383 | if (ret) |
| 384 | return ret; |
| 385 | |
| 386 | return 0; |
Stefan Roese | 334b9b0 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | #ifndef CONFIG_DM_I2C |
| 390 | /* |
| 391 | * The legacy I2C functions. These need to get removed once |
| 392 | * all users of this driver are converted to DM. |
| 393 | */ |
Stefan Roese | 3f4358d | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 394 | static struct i2c_regs *i2c_get_base(struct i2c_adapter *adap) |
| 395 | { |
| 396 | switch (adap->hwadapnr) { |
| 397 | #if CONFIG_SYS_I2C_BUS_MAX >= 4 |
| 398 | case 3: |
| 399 | return (struct i2c_regs *)CONFIG_SYS_I2C_BASE3; |
| 400 | #endif |
| 401 | #if CONFIG_SYS_I2C_BUS_MAX >= 3 |
| 402 | case 2: |
| 403 | return (struct i2c_regs *)CONFIG_SYS_I2C_BASE2; |
| 404 | #endif |
| 405 | #if CONFIG_SYS_I2C_BUS_MAX >= 2 |
| 406 | case 1: |
| 407 | return (struct i2c_regs *)CONFIG_SYS_I2C_BASE1; |
| 408 | #endif |
| 409 | case 0: |
| 410 | return (struct i2c_regs *)CONFIG_SYS_I2C_BASE; |
| 411 | default: |
| 412 | printf("Wrong I2C-adapter number %d\n", adap->hwadapnr); |
| 413 | } |
| 414 | |
| 415 | return NULL; |
| 416 | } |
| 417 | |
| 418 | static unsigned int dw_i2c_set_bus_speed(struct i2c_adapter *adap, |
| 419 | unsigned int speed) |
| 420 | { |
| 421 | adap->speed = speed; |
Simon Glass | d22409e | 2020-01-23 11:48:12 -0700 | [diff] [blame^] | 422 | return __dw_i2c_set_bus_speed(NULL, i2c_get_base(adap), speed, IC_CLK); |
Stefan Roese | 3f4358d | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 423 | } |
| 424 | |
Stefan Roese | 334b9b0 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 425 | static void dw_i2c_init(struct i2c_adapter *adap, int speed, int slaveaddr) |
Vipin KUMAR | 2403f8f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 426 | { |
Stefan Roese | 334b9b0 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 427 | __dw_i2c_init(i2c_get_base(adap), speed, slaveaddr); |
Vipin KUMAR | 2403f8f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 428 | } |
| 429 | |
Stefan Roese | 678398b | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 430 | static int dw_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr, |
| 431 | int alen, u8 *buffer, int len) |
Vipin KUMAR | 2403f8f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 432 | { |
Stefan Roese | 3f4358d | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 433 | return __dw_i2c_read(i2c_get_base(adap), dev, addr, alen, buffer, len); |
Vipin KUMAR | 2403f8f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 434 | } |
| 435 | |
Stefan Roese | 678398b | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 436 | static int dw_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr, |
| 437 | int alen, u8 *buffer, int len) |
Vipin KUMAR | 2403f8f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 438 | { |
Stefan Roese | 3f4358d | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 439 | return __dw_i2c_write(i2c_get_base(adap), dev, addr, alen, buffer, len); |
Vipin KUMAR | 2403f8f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 440 | } |
| 441 | |
Stefan Roese | 334b9b0 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 442 | /* dw_i2c_probe - Probe the i2c chip */ |
Stefan Roese | 678398b | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 443 | static int dw_i2c_probe(struct i2c_adapter *adap, u8 dev) |
Vipin KUMAR | 2403f8f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 444 | { |
Stefan Roese | 3f4358d | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 445 | struct i2c_regs *i2c_base = i2c_get_base(adap); |
Vipin KUMAR | 2403f8f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 446 | u32 tmp; |
Stefan Roese | 496ba48 | 2012-01-20 11:52:33 +0100 | [diff] [blame] | 447 | int ret; |
Vipin KUMAR | 2403f8f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 448 | |
| 449 | /* |
| 450 | * Try to read the first location of the chip. |
| 451 | */ |
Stefan Roese | 3f4358d | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 452 | ret = __dw_i2c_read(i2c_base, dev, 0, 1, (uchar *)&tmp, 1); |
Stefan Roese | 496ba48 | 2012-01-20 11:52:33 +0100 | [diff] [blame] | 453 | if (ret) |
Stefan Roese | 678398b | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 454 | dw_i2c_init(adap, adap->speed, adap->slaveaddr); |
Stefan Roese | 496ba48 | 2012-01-20 11:52:33 +0100 | [diff] [blame] | 455 | |
| 456 | return ret; |
Vipin KUMAR | 2403f8f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 457 | } |
Armando Visconti | ac6e2fe | 2012-12-06 00:04:15 +0000 | [diff] [blame] | 458 | |
Stefan Roese | 678398b | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 459 | U_BOOT_I2C_ADAP_COMPLETE(dw_0, dw_i2c_init, dw_i2c_probe, dw_i2c_read, |
| 460 | dw_i2c_write, dw_i2c_set_bus_speed, |
| 461 | CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0) |
Armando Visconti | ac6e2fe | 2012-12-06 00:04:15 +0000 | [diff] [blame] | 462 | |
Stefan Roese | 678398b | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 463 | #if CONFIG_SYS_I2C_BUS_MAX >= 2 |
| 464 | U_BOOT_I2C_ADAP_COMPLETE(dw_1, dw_i2c_init, dw_i2c_probe, dw_i2c_read, |
| 465 | dw_i2c_write, dw_i2c_set_bus_speed, |
| 466 | CONFIG_SYS_I2C_SPEED1, CONFIG_SYS_I2C_SLAVE1, 1) |
| 467 | #endif |
Armando Visconti | ac6e2fe | 2012-12-06 00:04:15 +0000 | [diff] [blame] | 468 | |
Stefan Roese | 678398b | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 469 | #if CONFIG_SYS_I2C_BUS_MAX >= 3 |
| 470 | U_BOOT_I2C_ADAP_COMPLETE(dw_2, dw_i2c_init, dw_i2c_probe, dw_i2c_read, |
| 471 | dw_i2c_write, dw_i2c_set_bus_speed, |
| 472 | CONFIG_SYS_I2C_SPEED2, CONFIG_SYS_I2C_SLAVE2, 2) |
| 473 | #endif |
Armando Visconti | ac6e2fe | 2012-12-06 00:04:15 +0000 | [diff] [blame] | 474 | |
Stefan Roese | 678398b | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 475 | #if CONFIG_SYS_I2C_BUS_MAX >= 4 |
| 476 | U_BOOT_I2C_ADAP_COMPLETE(dw_3, dw_i2c_init, dw_i2c_probe, dw_i2c_read, |
| 477 | dw_i2c_write, dw_i2c_set_bus_speed, |
| 478 | CONFIG_SYS_I2C_SPEED3, CONFIG_SYS_I2C_SLAVE3, 3) |
Armando Visconti | ac6e2fe | 2012-12-06 00:04:15 +0000 | [diff] [blame] | 479 | #endif |
Stefan Roese | 334b9b0 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 480 | |
| 481 | #else /* CONFIG_DM_I2C */ |
| 482 | /* The DM I2C functions */ |
| 483 | |
| 484 | static int designware_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, |
| 485 | int nmsgs) |
| 486 | { |
| 487 | struct dw_i2c *i2c = dev_get_priv(bus); |
| 488 | int ret; |
| 489 | |
| 490 | debug("i2c_xfer: %d messages\n", nmsgs); |
| 491 | for (; nmsgs > 0; nmsgs--, msg++) { |
| 492 | debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len); |
| 493 | if (msg->flags & I2C_M_RD) { |
| 494 | ret = __dw_i2c_read(i2c->regs, msg->addr, 0, 0, |
| 495 | msg->buf, msg->len); |
| 496 | } else { |
| 497 | ret = __dw_i2c_write(i2c->regs, msg->addr, 0, 0, |
| 498 | msg->buf, msg->len); |
| 499 | } |
| 500 | if (ret) { |
| 501 | debug("i2c_write: error sending\n"); |
| 502 | return -EREMOTEIO; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | static int designware_i2c_set_bus_speed(struct udevice *bus, unsigned int speed) |
| 510 | { |
| 511 | struct dw_i2c *i2c = dev_get_priv(bus); |
Ley Foon Tan | 2d1e879 | 2019-06-12 09:48:04 +0800 | [diff] [blame] | 512 | ulong rate; |
Stefan Roese | 334b9b0 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 513 | |
Ley Foon Tan | 2d1e879 | 2019-06-12 09:48:04 +0800 | [diff] [blame] | 514 | #if CONFIG_IS_ENABLED(CLK) |
| 515 | rate = clk_get_rate(&i2c->clk); |
| 516 | if (IS_ERR_VALUE(rate)) |
| 517 | return -EINVAL; |
Ley Foon Tan | 2d1e879 | 2019-06-12 09:48:04 +0800 | [diff] [blame] | 518 | #else |
| 519 | rate = IC_CLK; |
| 520 | #endif |
Simon Glass | d22409e | 2020-01-23 11:48:12 -0700 | [diff] [blame^] | 521 | return __dw_i2c_set_bus_speed(i2c, i2c->regs, speed, rate); |
Stefan Roese | 334b9b0 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | static int designware_i2c_probe_chip(struct udevice *bus, uint chip_addr, |
| 525 | uint chip_flags) |
| 526 | { |
| 527 | struct dw_i2c *i2c = dev_get_priv(bus); |
| 528 | struct i2c_regs *i2c_base = i2c->regs; |
| 529 | u32 tmp; |
| 530 | int ret; |
| 531 | |
| 532 | /* Try to read the first location of the chip */ |
| 533 | ret = __dw_i2c_read(i2c_base, chip_addr, 0, 1, (uchar *)&tmp, 1); |
| 534 | if (ret) |
| 535 | __dw_i2c_init(i2c_base, 0, 0); |
| 536 | |
| 537 | return ret; |
| 538 | } |
| 539 | |
Simon Glass | 80a03db | 2020-01-23 11:48:11 -0700 | [diff] [blame] | 540 | int designware_i2c_ofdata_to_platdata(struct udevice *bus) |
Simon Glass | 457df23 | 2019-12-06 21:41:40 -0700 | [diff] [blame] | 541 | { |
| 542 | struct dw_i2c *priv = dev_get_priv(bus); |
| 543 | |
Simon Glass | 80a03db | 2020-01-23 11:48:11 -0700 | [diff] [blame] | 544 | if (!priv->regs) |
| 545 | priv->regs = (struct i2c_regs *)devfdt_get_addr_ptr(bus); |
| 546 | dev_read_u32(bus, "i2c-scl-rising-time-ns", &priv->scl_rise_time_ns); |
| 547 | dev_read_u32(bus, "i2c-scl-falling-time-ns", &priv->scl_fall_time_ns); |
| 548 | dev_read_u32(bus, "i2c-sda-hold-time-ns", &priv->sda_hold_time_ns); |
Simon Glass | 457df23 | 2019-12-06 21:41:40 -0700 | [diff] [blame] | 549 | |
| 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | int designware_i2c_probe(struct udevice *bus) |
Stefan Roese | 334b9b0 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 554 | { |
| 555 | struct dw_i2c *priv = dev_get_priv(bus); |
Dinh Nguyen | 622597d | 2018-04-04 17:18:24 -0500 | [diff] [blame] | 556 | int ret; |
Stefan Roese | 334b9b0 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 557 | |
Simon Goldschmidt | 36821b3 | 2019-03-28 21:11:48 +0100 | [diff] [blame] | 558 | ret = reset_get_bulk(bus, &priv->resets); |
Dinh Nguyen | 622597d | 2018-04-04 17:18:24 -0500 | [diff] [blame] | 559 | if (ret) |
Simon Goldschmidt | 36821b3 | 2019-03-28 21:11:48 +0100 | [diff] [blame] | 560 | dev_warn(bus, "Can't get reset: %d\n", ret); |
| 561 | else |
| 562 | reset_deassert_bulk(&priv->resets); |
Dinh Nguyen | 622597d | 2018-04-04 17:18:24 -0500 | [diff] [blame] | 563 | |
Ley Foon Tan | 2d1e879 | 2019-06-12 09:48:04 +0800 | [diff] [blame] | 564 | #if CONFIG_IS_ENABLED(CLK) |
| 565 | ret = clk_get_by_index(bus, 0, &priv->clk); |
| 566 | if (ret) |
| 567 | return ret; |
| 568 | |
| 569 | ret = clk_enable(&priv->clk); |
| 570 | if (ret && ret != -ENOSYS && ret != -ENOTSUPP) { |
| 571 | clk_free(&priv->clk); |
| 572 | dev_err(bus, "failed to enable clock\n"); |
| 573 | return ret; |
| 574 | } |
| 575 | #endif |
| 576 | |
Simon Glass | 2b5d029 | 2019-02-16 20:24:39 -0700 | [diff] [blame] | 577 | return __dw_i2c_init(priv->regs, 0, 0); |
Stefan Roese | 334b9b0 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 578 | } |
| 579 | |
Simon Glass | 457df23 | 2019-12-06 21:41:40 -0700 | [diff] [blame] | 580 | int designware_i2c_remove(struct udevice *dev) |
Simon Goldschmidt | 36821b3 | 2019-03-28 21:11:48 +0100 | [diff] [blame] | 581 | { |
| 582 | struct dw_i2c *priv = dev_get_priv(dev); |
| 583 | |
Ley Foon Tan | 2d1e879 | 2019-06-12 09:48:04 +0800 | [diff] [blame] | 584 | #if CONFIG_IS_ENABLED(CLK) |
| 585 | clk_disable(&priv->clk); |
| 586 | clk_free(&priv->clk); |
| 587 | #endif |
| 588 | |
Simon Goldschmidt | 36821b3 | 2019-03-28 21:11:48 +0100 | [diff] [blame] | 589 | return reset_release_bulk(&priv->resets); |
| 590 | } |
| 591 | |
Simon Glass | 457df23 | 2019-12-06 21:41:40 -0700 | [diff] [blame] | 592 | const struct dm_i2c_ops designware_i2c_ops = { |
Stefan Roese | 334b9b0 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 593 | .xfer = designware_i2c_xfer, |
| 594 | .probe_chip = designware_i2c_probe_chip, |
| 595 | .set_bus_speed = designware_i2c_set_bus_speed, |
| 596 | }; |
| 597 | |
| 598 | static const struct udevice_id designware_i2c_ids[] = { |
| 599 | { .compatible = "snps,designware-i2c" }, |
| 600 | { } |
| 601 | }; |
| 602 | |
| 603 | U_BOOT_DRIVER(i2c_designware) = { |
| 604 | .name = "i2c_designware", |
| 605 | .id = UCLASS_I2C, |
| 606 | .of_match = designware_i2c_ids, |
Simon Glass | 457df23 | 2019-12-06 21:41:40 -0700 | [diff] [blame] | 607 | .ofdata_to_platdata = designware_i2c_ofdata_to_platdata, |
Stefan Roese | 334b9b0 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 608 | .probe = designware_i2c_probe, |
| 609 | .priv_auto_alloc_size = sizeof(struct dw_i2c), |
Simon Goldschmidt | 36821b3 | 2019-03-28 21:11:48 +0100 | [diff] [blame] | 610 | .remove = designware_i2c_remove, |
Simon Glass | 457df23 | 2019-12-06 21:41:40 -0700 | [diff] [blame] | 611 | .flags = DM_FLAG_OS_PREPARE, |
Stefan Roese | 334b9b0 | 2016-04-21 08:19:41 +0200 | [diff] [blame] | 612 | .ops = &designware_i2c_ops, |
| 613 | }; |
| 614 | |
| 615 | #endif /* CONFIG_DM_I2C */ |