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