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