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