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> |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 11 | #include <asm/io.h> |
Vipin KUMAR | 3f64acb | 2012-02-26 23:13:29 +0000 | [diff] [blame] | 12 | #include "designware_i2c.h" |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 13 | |
Stefan Roese | 3cb2796 | 2016-04-21 08:19:41 +0200 | [diff] [blame^] | 14 | struct dw_i2c { |
| 15 | struct i2c_regs *regs; |
| 16 | }; |
| 17 | |
Stefan Roese | 3bc33ba | 2016-04-21 08:19:38 +0200 | [diff] [blame] | 18 | static void dw_i2c_enable(struct i2c_regs *i2c_base, bool enable) |
| 19 | { |
| 20 | u32 ena = enable ? IC_ENABLE_0B : 0; |
| 21 | int timeout = 100; |
| 22 | |
| 23 | do { |
| 24 | writel(ena, &i2c_base->ic_enable); |
| 25 | if ((readl(&i2c_base->ic_enable_status) & IC_ENABLE_0B) == ena) |
| 26 | return; |
| 27 | |
| 28 | /* |
| 29 | * Wait 10 times the signaling period of the highest I2C |
| 30 | * transfer supported by the driver (for 400KHz this is |
| 31 | * 25us) as described in the DesignWare I2C databook. |
| 32 | */ |
| 33 | udelay(25); |
| 34 | } while (timeout--); |
| 35 | |
| 36 | printf("timeout in %sabling I2C adapter\n", enable ? "en" : "dis"); |
| 37 | } |
| 38 | |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 39 | /* |
Stefan Roese | 88893c9 | 2016-04-21 08:19:39 +0200 | [diff] [blame] | 40 | * i2c_set_bus_speed - Set the i2c speed |
| 41 | * @speed: required i2c speed |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 42 | * |
Stefan Roese | 88893c9 | 2016-04-21 08:19:39 +0200 | [diff] [blame] | 43 | * Set the i2c speed. |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 44 | */ |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 45 | static unsigned int __dw_i2c_set_bus_speed(struct i2c_regs *i2c_base, |
| 46 | unsigned int speed) |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 47 | { |
| 48 | unsigned int cntl; |
| 49 | unsigned int hcnt, lcnt; |
Stefan Roese | 88893c9 | 2016-04-21 08:19:39 +0200 | [diff] [blame] | 50 | int i2c_spd; |
| 51 | |
| 52 | if (speed >= I2C_MAX_SPEED) |
| 53 | i2c_spd = IC_SPEED_MODE_MAX; |
| 54 | else if (speed >= I2C_FAST_SPEED) |
| 55 | i2c_spd = IC_SPEED_MODE_FAST; |
| 56 | else |
| 57 | i2c_spd = IC_SPEED_MODE_STANDARD; |
Armando Visconti | 631e693 | 2012-03-29 20:10:17 +0000 | [diff] [blame] | 58 | |
| 59 | /* to set speed cltr must be disabled */ |
Stefan Roese | 3bc33ba | 2016-04-21 08:19:38 +0200 | [diff] [blame] | 60 | dw_i2c_enable(i2c_base, false); |
Armando Visconti | 631e693 | 2012-03-29 20:10:17 +0000 | [diff] [blame] | 61 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 62 | cntl = (readl(&i2c_base->ic_con) & (~IC_CON_SPD_MSK)); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 63 | |
| 64 | switch (i2c_spd) { |
| 65 | case IC_SPEED_MODE_MAX: |
| 66 | cntl |= IC_CON_SPD_HS; |
Armando Visconti | 28a724f | 2012-12-06 00:04:17 +0000 | [diff] [blame] | 67 | hcnt = (IC_CLK * MIN_HS_SCL_HIGHTIME) / NANO_TO_MICRO; |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 68 | writel(hcnt, &i2c_base->ic_hs_scl_hcnt); |
Armando Visconti | 28a724f | 2012-12-06 00:04:17 +0000 | [diff] [blame] | 69 | lcnt = (IC_CLK * MIN_HS_SCL_LOWTIME) / NANO_TO_MICRO; |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 70 | writel(lcnt, &i2c_base->ic_hs_scl_lcnt); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 71 | break; |
| 72 | |
| 73 | case IC_SPEED_MODE_STANDARD: |
| 74 | cntl |= IC_CON_SPD_SS; |
Armando Visconti | 28a724f | 2012-12-06 00:04:17 +0000 | [diff] [blame] | 75 | hcnt = (IC_CLK * MIN_SS_SCL_HIGHTIME) / NANO_TO_MICRO; |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 76 | writel(hcnt, &i2c_base->ic_ss_scl_hcnt); |
Armando Visconti | 28a724f | 2012-12-06 00:04:17 +0000 | [diff] [blame] | 77 | lcnt = (IC_CLK * MIN_SS_SCL_LOWTIME) / NANO_TO_MICRO; |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 78 | writel(lcnt, &i2c_base->ic_ss_scl_lcnt); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 79 | break; |
| 80 | |
| 81 | case IC_SPEED_MODE_FAST: |
| 82 | default: |
| 83 | cntl |= IC_CON_SPD_FS; |
Armando Visconti | 28a724f | 2012-12-06 00:04:17 +0000 | [diff] [blame] | 84 | hcnt = (IC_CLK * MIN_FS_SCL_HIGHTIME) / NANO_TO_MICRO; |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 85 | writel(hcnt, &i2c_base->ic_fs_scl_hcnt); |
Armando Visconti | 28a724f | 2012-12-06 00:04:17 +0000 | [diff] [blame] | 86 | lcnt = (IC_CLK * MIN_FS_SCL_LOWTIME) / NANO_TO_MICRO; |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 87 | writel(lcnt, &i2c_base->ic_fs_scl_lcnt); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 88 | break; |
| 89 | } |
| 90 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 91 | writel(cntl, &i2c_base->ic_con); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 92 | |
Armando Visconti | 28a724f | 2012-12-06 00:04:17 +0000 | [diff] [blame] | 93 | /* Enable back i2c now speed set */ |
Stefan Roese | 3bc33ba | 2016-04-21 08:19:38 +0200 | [diff] [blame] | 94 | dw_i2c_enable(i2c_base, true); |
Stefan Roese | f6322ebd | 2012-01-20 11:52:33 +0100 | [diff] [blame] | 95 | |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | /* |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 100 | * i2c_setaddress - Sets the target slave address |
| 101 | * @i2c_addr: target i2c address |
| 102 | * |
| 103 | * Sets the target slave address. |
| 104 | */ |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 105 | 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] | 106 | { |
Alexey Brodkin | 41c5655 | 2013-11-07 17:52:18 +0400 | [diff] [blame] | 107 | /* Disable i2c */ |
Stefan Roese | 3bc33ba | 2016-04-21 08:19:38 +0200 | [diff] [blame] | 108 | dw_i2c_enable(i2c_base, false); |
Alexey Brodkin | 41c5655 | 2013-11-07 17:52:18 +0400 | [diff] [blame] | 109 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 110 | writel(i2c_addr, &i2c_base->ic_tar); |
Alexey Brodkin | 41c5655 | 2013-11-07 17:52:18 +0400 | [diff] [blame] | 111 | |
| 112 | /* Enable i2c */ |
Stefan Roese | 3bc33ba | 2016-04-21 08:19:38 +0200 | [diff] [blame] | 113 | dw_i2c_enable(i2c_base, true); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /* |
| 117 | * i2c_flush_rxfifo - Flushes the i2c RX FIFO |
| 118 | * |
| 119 | * Flushes the i2c RX FIFO |
| 120 | */ |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 121 | static void i2c_flush_rxfifo(struct i2c_regs *i2c_base) |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 122 | { |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 123 | while (readl(&i2c_base->ic_status) & IC_STATUS_RFNE) |
| 124 | readl(&i2c_base->ic_cmd_data); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /* |
| 128 | * i2c_wait_for_bb - Waits for bus busy |
| 129 | * |
| 130 | * Waits for bus busy |
| 131 | */ |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 132 | static int i2c_wait_for_bb(struct i2c_regs *i2c_base) |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 133 | { |
| 134 | unsigned long start_time_bb = get_timer(0); |
| 135 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 136 | while ((readl(&i2c_base->ic_status) & IC_STATUS_MA) || |
| 137 | !(readl(&i2c_base->ic_status) & IC_STATUS_TFE)) { |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 138 | |
| 139 | /* Evaluate timeout */ |
| 140 | if (get_timer(start_time_bb) > (unsigned long)(I2C_BYTE_TO_BB)) |
| 141 | return 1; |
| 142 | } |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 147 | 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] | 148 | int alen) |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 149 | { |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 150 | if (i2c_wait_for_bb(i2c_base)) |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 151 | return 1; |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 152 | |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 153 | i2c_setaddress(i2c_base, chip); |
Chin Liang See | a0c2626 | 2014-02-04 11:56:23 -0600 | [diff] [blame] | 154 | while (alen) { |
| 155 | alen--; |
| 156 | /* high byte address going out first */ |
| 157 | writel((addr >> (alen * 8)) & 0xff, |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 158 | &i2c_base->ic_cmd_data); |
Chin Liang See | a0c2626 | 2014-02-04 11:56:23 -0600 | [diff] [blame] | 159 | } |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 160 | return 0; |
| 161 | } |
| 162 | |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 163 | static int i2c_xfer_finish(struct i2c_regs *i2c_base) |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 164 | { |
| 165 | ulong start_stop_det = get_timer(0); |
| 166 | |
| 167 | while (1) { |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 168 | if ((readl(&i2c_base->ic_raw_intr_stat) & IC_STOP_DET)) { |
| 169 | readl(&i2c_base->ic_clr_stop_det); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 170 | break; |
| 171 | } else if (get_timer(start_stop_det) > I2C_STOPDET_TO) { |
| 172 | break; |
| 173 | } |
| 174 | } |
| 175 | |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 176 | if (i2c_wait_for_bb(i2c_base)) { |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 177 | printf("Timed out waiting for bus\n"); |
| 178 | return 1; |
| 179 | } |
| 180 | |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 181 | i2c_flush_rxfifo(i2c_base); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 182 | |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * i2c_read - Read from i2c memory |
| 188 | * @chip: target i2c address |
| 189 | * @addr: address to read from |
| 190 | * @alen: |
| 191 | * @buffer: buffer for read data |
| 192 | * @len: no of bytes to be read |
| 193 | * |
| 194 | * Read from i2c memory. |
| 195 | */ |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 196 | static int __dw_i2c_read(struct i2c_regs *i2c_base, u8 dev, uint addr, |
| 197 | int alen, u8 *buffer, int len) |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 198 | { |
| 199 | unsigned long start_time_rx; |
| 200 | |
Alexey Brodkin | 7ef0036 | 2013-12-16 15:30:35 +0400 | [diff] [blame] | 201 | #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW |
| 202 | /* |
| 203 | * EEPROM chips that implement "address overflow" are ones |
| 204 | * like Catalyst 24WC04/08/16 which has 9/10/11 bits of |
| 205 | * address and the extra bits end up in the "chip address" |
| 206 | * bit slots. This makes a 24WC08 (1Kbyte) chip look like |
| 207 | * four 256 byte chips. |
| 208 | * |
| 209 | * Note that we consider the length of the address field to |
| 210 | * still be one byte because the extra address bits are |
| 211 | * hidden in the chip address. |
| 212 | */ |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 213 | dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW); |
Alexey Brodkin | 7ef0036 | 2013-12-16 15:30:35 +0400 | [diff] [blame] | 214 | addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8)); |
| 215 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 216 | debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev, |
Alexey Brodkin | 7ef0036 | 2013-12-16 15:30:35 +0400 | [diff] [blame] | 217 | addr); |
| 218 | #endif |
| 219 | |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 220 | if (i2c_xfer_init(i2c_base, dev, addr, alen)) |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 221 | return 1; |
| 222 | |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 223 | start_time_rx = get_timer(0); |
| 224 | while (len) { |
Armando Visconti | 6bc6ef7 | 2012-12-06 00:04:16 +0000 | [diff] [blame] | 225 | if (len == 1) |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 226 | writel(IC_CMD | IC_STOP, &i2c_base->ic_cmd_data); |
Armando Visconti | 6bc6ef7 | 2012-12-06 00:04:16 +0000 | [diff] [blame] | 227 | else |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 228 | writel(IC_CMD, &i2c_base->ic_cmd_data); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 229 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 230 | if (readl(&i2c_base->ic_status) & IC_STATUS_RFNE) { |
| 231 | *buffer++ = (uchar)readl(&i2c_base->ic_cmd_data); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 232 | len--; |
| 233 | start_time_rx = get_timer(0); |
| 234 | |
| 235 | } else if (get_timer(start_time_rx) > I2C_BYTE_TO) { |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 236 | return 1; |
| 237 | } |
| 238 | } |
| 239 | |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 240 | return i2c_xfer_finish(i2c_base); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /* |
| 244 | * i2c_write - Write to i2c memory |
| 245 | * @chip: target i2c address |
| 246 | * @addr: address to read from |
| 247 | * @alen: |
| 248 | * @buffer: buffer for read data |
| 249 | * @len: no of bytes to be read |
| 250 | * |
| 251 | * Write to i2c memory. |
| 252 | */ |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 253 | static int __dw_i2c_write(struct i2c_regs *i2c_base, u8 dev, uint addr, |
| 254 | int alen, u8 *buffer, int len) |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 255 | { |
| 256 | int nb = len; |
| 257 | unsigned long start_time_tx; |
| 258 | |
Alexey Brodkin | 7ef0036 | 2013-12-16 15:30:35 +0400 | [diff] [blame] | 259 | #ifdef CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW |
| 260 | /* |
| 261 | * EEPROM chips that implement "address overflow" are ones |
| 262 | * like Catalyst 24WC04/08/16 which has 9/10/11 bits of |
| 263 | * address and the extra bits end up in the "chip address" |
| 264 | * bit slots. This makes a 24WC08 (1Kbyte) chip look like |
| 265 | * four 256 byte chips. |
| 266 | * |
| 267 | * Note that we consider the length of the address field to |
| 268 | * still be one byte because the extra address bits are |
| 269 | * hidden in the chip address. |
| 270 | */ |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 271 | dev |= ((addr >> (alen * 8)) & CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW); |
Alexey Brodkin | 7ef0036 | 2013-12-16 15:30:35 +0400 | [diff] [blame] | 272 | addr &= ~(CONFIG_SYS_I2C_EEPROM_ADDR_OVERFLOW << (alen * 8)); |
| 273 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 274 | debug("%s: fix addr_overflow: dev %02x addr %02x\n", __func__, dev, |
Alexey Brodkin | 7ef0036 | 2013-12-16 15:30:35 +0400 | [diff] [blame] | 275 | addr); |
| 276 | #endif |
| 277 | |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 278 | if (i2c_xfer_init(i2c_base, dev, addr, alen)) |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 279 | return 1; |
| 280 | |
| 281 | start_time_tx = get_timer(0); |
| 282 | while (len) { |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 283 | if (readl(&i2c_base->ic_status) & IC_STATUS_TFNF) { |
| 284 | if (--len == 0) { |
| 285 | writel(*buffer | IC_STOP, |
| 286 | &i2c_base->ic_cmd_data); |
| 287 | } else { |
| 288 | writel(*buffer, &i2c_base->ic_cmd_data); |
| 289 | } |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 290 | buffer++; |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 291 | start_time_tx = get_timer(0); |
| 292 | |
| 293 | } else if (get_timer(start_time_tx) > (nb * I2C_BYTE_TO)) { |
| 294 | printf("Timed out. i2c write Failed\n"); |
| 295 | return 1; |
| 296 | } |
| 297 | } |
| 298 | |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 299 | return i2c_xfer_finish(i2c_base); |
| 300 | } |
| 301 | |
Stefan Roese | 3cb2796 | 2016-04-21 08:19:41 +0200 | [diff] [blame^] | 302 | /* |
| 303 | * __dw_i2c_init - Init function |
| 304 | * @speed: required i2c speed |
| 305 | * @slaveaddr: slave address for the device |
| 306 | * |
| 307 | * Initialization function. |
| 308 | */ |
| 309 | static void __dw_i2c_init(struct i2c_regs *i2c_base, int speed, int slaveaddr) |
| 310 | { |
| 311 | /* Disable i2c */ |
| 312 | dw_i2c_enable(i2c_base, false); |
| 313 | |
| 314 | writel((IC_CON_SD | IC_CON_SPD_FS | IC_CON_MM), &i2c_base->ic_con); |
| 315 | writel(IC_RX_TL, &i2c_base->ic_rx_tl); |
| 316 | writel(IC_TX_TL, &i2c_base->ic_tx_tl); |
| 317 | writel(IC_STOP_DET, &i2c_base->ic_intr_mask); |
| 318 | #ifndef CONFIG_DM_I2C |
| 319 | __dw_i2c_set_bus_speed(i2c_base, speed); |
| 320 | writel(slaveaddr, &i2c_base->ic_sar); |
| 321 | #endif |
| 322 | |
| 323 | /* Enable i2c */ |
| 324 | dw_i2c_enable(i2c_base, true); |
| 325 | } |
| 326 | |
| 327 | #ifndef CONFIG_DM_I2C |
| 328 | /* |
| 329 | * The legacy I2C functions. These need to get removed once |
| 330 | * all users of this driver are converted to DM. |
| 331 | */ |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 332 | static struct i2c_regs *i2c_get_base(struct i2c_adapter *adap) |
| 333 | { |
| 334 | switch (adap->hwadapnr) { |
| 335 | #if CONFIG_SYS_I2C_BUS_MAX >= 4 |
| 336 | case 3: |
| 337 | return (struct i2c_regs *)CONFIG_SYS_I2C_BASE3; |
| 338 | #endif |
| 339 | #if CONFIG_SYS_I2C_BUS_MAX >= 3 |
| 340 | case 2: |
| 341 | return (struct i2c_regs *)CONFIG_SYS_I2C_BASE2; |
| 342 | #endif |
| 343 | #if CONFIG_SYS_I2C_BUS_MAX >= 2 |
| 344 | case 1: |
| 345 | return (struct i2c_regs *)CONFIG_SYS_I2C_BASE1; |
| 346 | #endif |
| 347 | case 0: |
| 348 | return (struct i2c_regs *)CONFIG_SYS_I2C_BASE; |
| 349 | default: |
| 350 | printf("Wrong I2C-adapter number %d\n", adap->hwadapnr); |
| 351 | } |
| 352 | |
| 353 | return NULL; |
| 354 | } |
| 355 | |
| 356 | static unsigned int dw_i2c_set_bus_speed(struct i2c_adapter *adap, |
| 357 | unsigned int speed) |
| 358 | { |
| 359 | adap->speed = speed; |
| 360 | return __dw_i2c_set_bus_speed(i2c_get_base(adap), speed); |
| 361 | } |
| 362 | |
Stefan Roese | 3cb2796 | 2016-04-21 08:19:41 +0200 | [diff] [blame^] | 363 | 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] | 364 | { |
Stefan Roese | 3cb2796 | 2016-04-21 08:19:41 +0200 | [diff] [blame^] | 365 | __dw_i2c_init(i2c_get_base(adap), speed, slaveaddr); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 366 | } |
| 367 | |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 368 | static int dw_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr, |
| 369 | int alen, u8 *buffer, int len) |
| 370 | { |
| 371 | return __dw_i2c_read(i2c_get_base(adap), dev, addr, alen, buffer, len); |
| 372 | } |
| 373 | |
| 374 | static int dw_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr, |
| 375 | int alen, u8 *buffer, int len) |
| 376 | { |
| 377 | return __dw_i2c_write(i2c_get_base(adap), dev, addr, alen, buffer, len); |
| 378 | } |
| 379 | |
Stefan Roese | 3cb2796 | 2016-04-21 08:19:41 +0200 | [diff] [blame^] | 380 | /* dw_i2c_probe - Probe the i2c chip */ |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 381 | static int dw_i2c_probe(struct i2c_adapter *adap, u8 dev) |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 382 | { |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 383 | struct i2c_regs *i2c_base = i2c_get_base(adap); |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 384 | u32 tmp; |
Stefan Roese | f6322ebd | 2012-01-20 11:52:33 +0100 | [diff] [blame] | 385 | int ret; |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 386 | |
| 387 | /* |
| 388 | * Try to read the first location of the chip. |
| 389 | */ |
Stefan Roese | 41de766 | 2016-04-21 08:19:40 +0200 | [diff] [blame] | 390 | ret = __dw_i2c_read(i2c_base, dev, 0, 1, (uchar *)&tmp, 1); |
Stefan Roese | f6322ebd | 2012-01-20 11:52:33 +0100 | [diff] [blame] | 391 | if (ret) |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 392 | dw_i2c_init(adap, adap->speed, adap->slaveaddr); |
Stefan Roese | f6322ebd | 2012-01-20 11:52:33 +0100 | [diff] [blame] | 393 | |
| 394 | return ret; |
Vipin KUMAR | fc9589f | 2010-01-15 19:15:44 +0530 | [diff] [blame] | 395 | } |
Armando Visconti | 4a7b4ec | 2012-12-06 00:04:15 +0000 | [diff] [blame] | 396 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 397 | U_BOOT_I2C_ADAP_COMPLETE(dw_0, dw_i2c_init, dw_i2c_probe, dw_i2c_read, |
| 398 | dw_i2c_write, dw_i2c_set_bus_speed, |
| 399 | CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE, 0) |
Armando Visconti | 4a7b4ec | 2012-12-06 00:04:15 +0000 | [diff] [blame] | 400 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 401 | #if CONFIG_SYS_I2C_BUS_MAX >= 2 |
| 402 | U_BOOT_I2C_ADAP_COMPLETE(dw_1, dw_i2c_init, dw_i2c_probe, dw_i2c_read, |
| 403 | dw_i2c_write, dw_i2c_set_bus_speed, |
| 404 | CONFIG_SYS_I2C_SPEED1, CONFIG_SYS_I2C_SLAVE1, 1) |
| 405 | #endif |
Armando Visconti | 4a7b4ec | 2012-12-06 00:04:15 +0000 | [diff] [blame] | 406 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 407 | #if CONFIG_SYS_I2C_BUS_MAX >= 3 |
| 408 | U_BOOT_I2C_ADAP_COMPLETE(dw_2, dw_i2c_init, dw_i2c_probe, dw_i2c_read, |
| 409 | dw_i2c_write, dw_i2c_set_bus_speed, |
| 410 | CONFIG_SYS_I2C_SPEED2, CONFIG_SYS_I2C_SLAVE2, 2) |
| 411 | #endif |
Armando Visconti | 4a7b4ec | 2012-12-06 00:04:15 +0000 | [diff] [blame] | 412 | |
Stefan Roese | ef6073e | 2014-10-28 12:12:00 +0100 | [diff] [blame] | 413 | #if CONFIG_SYS_I2C_BUS_MAX >= 4 |
| 414 | U_BOOT_I2C_ADAP_COMPLETE(dw_3, dw_i2c_init, dw_i2c_probe, dw_i2c_read, |
| 415 | dw_i2c_write, dw_i2c_set_bus_speed, |
| 416 | CONFIG_SYS_I2C_SPEED3, CONFIG_SYS_I2C_SLAVE3, 3) |
Armando Visconti | 4a7b4ec | 2012-12-06 00:04:15 +0000 | [diff] [blame] | 417 | #endif |
Stefan Roese | 3cb2796 | 2016-04-21 08:19:41 +0200 | [diff] [blame^] | 418 | |
| 419 | #else /* CONFIG_DM_I2C */ |
| 420 | /* The DM I2C functions */ |
| 421 | |
| 422 | static int designware_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, |
| 423 | int nmsgs) |
| 424 | { |
| 425 | struct dw_i2c *i2c = dev_get_priv(bus); |
| 426 | int ret; |
| 427 | |
| 428 | debug("i2c_xfer: %d messages\n", nmsgs); |
| 429 | for (; nmsgs > 0; nmsgs--, msg++) { |
| 430 | debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len); |
| 431 | if (msg->flags & I2C_M_RD) { |
| 432 | ret = __dw_i2c_read(i2c->regs, msg->addr, 0, 0, |
| 433 | msg->buf, msg->len); |
| 434 | } else { |
| 435 | ret = __dw_i2c_write(i2c->regs, msg->addr, 0, 0, |
| 436 | msg->buf, msg->len); |
| 437 | } |
| 438 | if (ret) { |
| 439 | debug("i2c_write: error sending\n"); |
| 440 | return -EREMOTEIO; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | static int designware_i2c_set_bus_speed(struct udevice *bus, unsigned int speed) |
| 448 | { |
| 449 | struct dw_i2c *i2c = dev_get_priv(bus); |
| 450 | |
| 451 | return __dw_i2c_set_bus_speed(i2c->regs, speed); |
| 452 | } |
| 453 | |
| 454 | static int designware_i2c_probe_chip(struct udevice *bus, uint chip_addr, |
| 455 | uint chip_flags) |
| 456 | { |
| 457 | struct dw_i2c *i2c = dev_get_priv(bus); |
| 458 | struct i2c_regs *i2c_base = i2c->regs; |
| 459 | u32 tmp; |
| 460 | int ret; |
| 461 | |
| 462 | /* Try to read the first location of the chip */ |
| 463 | ret = __dw_i2c_read(i2c_base, chip_addr, 0, 1, (uchar *)&tmp, 1); |
| 464 | if (ret) |
| 465 | __dw_i2c_init(i2c_base, 0, 0); |
| 466 | |
| 467 | return ret; |
| 468 | } |
| 469 | |
| 470 | static int designware_i2c_probe(struct udevice *bus) |
| 471 | { |
| 472 | struct dw_i2c *priv = dev_get_priv(bus); |
| 473 | |
| 474 | /* Save base address from device-tree */ |
| 475 | priv->regs = (struct i2c_regs *)dev_get_addr(bus); |
| 476 | |
| 477 | __dw_i2c_init(priv->regs, 0, 0); |
| 478 | |
| 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | static const struct dm_i2c_ops designware_i2c_ops = { |
| 483 | .xfer = designware_i2c_xfer, |
| 484 | .probe_chip = designware_i2c_probe_chip, |
| 485 | .set_bus_speed = designware_i2c_set_bus_speed, |
| 486 | }; |
| 487 | |
| 488 | static const struct udevice_id designware_i2c_ids[] = { |
| 489 | { .compatible = "snps,designware-i2c" }, |
| 490 | { } |
| 491 | }; |
| 492 | |
| 493 | U_BOOT_DRIVER(i2c_designware) = { |
| 494 | .name = "i2c_designware", |
| 495 | .id = UCLASS_I2C, |
| 496 | .of_match = designware_i2c_ids, |
| 497 | .probe = designware_i2c_probe, |
| 498 | .priv_auto_alloc_size = sizeof(struct dw_i2c), |
| 499 | .ops = &designware_i2c_ops, |
| 500 | }; |
| 501 | |
| 502 | #endif /* CONFIG_DM_I2C */ |