Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2016 Freescale Semiconductors, Inc. |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 4 | */ |
| 5 | |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 6 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 7 | #include <log.h> |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 8 | #include <asm/io.h> |
| 9 | #include <asm/arch/clock.h> |
| 10 | #include <asm/arch/imx-regs.h> |
Ye Li | d9103f8 | 2018-07-08 11:46:40 +0800 | [diff] [blame] | 11 | #include <imx_lpi2c.h> |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 12 | #include <asm/arch/sys_proto.h> |
| 13 | #include <dm.h> |
| 14 | #include <fdtdec.h> |
| 15 | #include <i2c.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 16 | #include <dm/device_compat.h> |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 17 | |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 18 | #define LPI2C_FIFO_SIZE 4 |
Gao Pan | 52d457b | 2018-07-08 11:46:41 +0800 | [diff] [blame] | 19 | #define LPI2C_NACK_TOUT_MS 1 |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 20 | #define LPI2C_TIMEOUT_MS 100 |
| 21 | |
Ye Li | 2c69546 | 2018-07-08 11:46:43 +0800 | [diff] [blame] | 22 | static int bus_i2c_init(struct udevice *bus, int speed); |
| 23 | |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 24 | /* Weak linked function for overridden by some SoC power function */ |
| 25 | int __weak init_i2c_power(unsigned i2c_num) |
| 26 | { |
| 27 | return 0; |
| 28 | } |
| 29 | |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 30 | static int imx_lpci2c_check_busy_bus(const struct imx_lpi2c_reg *regs) |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 31 | { |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 32 | lpi2c_status_t result = LPI2C_SUCESS; |
| 33 | u32 status; |
| 34 | |
| 35 | status = readl(®s->msr); |
| 36 | |
| 37 | if ((status & LPI2C_MSR_BBF_MASK) && !(status & LPI2C_MSR_MBF_MASK)) |
| 38 | result = LPI2C_BUSY; |
| 39 | |
| 40 | return result; |
| 41 | } |
| 42 | |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 43 | static int imx_lpci2c_check_clear_error(struct imx_lpi2c_reg *regs) |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 44 | { |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 45 | lpi2c_status_t result = LPI2C_SUCESS; |
| 46 | u32 val, status; |
| 47 | |
| 48 | status = readl(®s->msr); |
| 49 | /* errors to check for */ |
| 50 | status &= LPI2C_MSR_NDF_MASK | LPI2C_MSR_ALF_MASK | |
| 51 | LPI2C_MSR_FEF_MASK | LPI2C_MSR_PLTF_MASK; |
| 52 | |
| 53 | if (status) { |
| 54 | if (status & LPI2C_MSR_PLTF_MASK) |
| 55 | result = LPI2C_PIN_LOW_TIMEOUT_ERR; |
| 56 | else if (status & LPI2C_MSR_ALF_MASK) |
| 57 | result = LPI2C_ARB_LOST_ERR; |
| 58 | else if (status & LPI2C_MSR_NDF_MASK) |
| 59 | result = LPI2C_NAK_ERR; |
| 60 | else if (status & LPI2C_MSR_FEF_MASK) |
| 61 | result = LPI2C_FIFO_ERR; |
| 62 | |
| 63 | /* clear status flags */ |
| 64 | writel(0x7f00, ®s->msr); |
| 65 | /* reset fifos */ |
| 66 | val = readl(®s->mcr); |
| 67 | val |= LPI2C_MCR_RRF_MASK | LPI2C_MCR_RTF_MASK; |
| 68 | writel(val, ®s->mcr); |
| 69 | } |
| 70 | |
| 71 | return result; |
| 72 | } |
| 73 | |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 74 | static int bus_i2c_wait_for_tx_ready(struct imx_lpi2c_reg *regs) |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 75 | { |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 76 | lpi2c_status_t result = LPI2C_SUCESS; |
| 77 | u32 txcount = 0; |
| 78 | ulong start_time = get_timer(0); |
| 79 | |
| 80 | do { |
| 81 | txcount = LPI2C_MFSR_TXCOUNT(readl(®s->mfsr)); |
| 82 | txcount = LPI2C_FIFO_SIZE - txcount; |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 83 | result = imx_lpci2c_check_clear_error(regs); |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 84 | if (result) { |
| 85 | debug("i2c: wait for tx ready: result 0x%x\n", result); |
| 86 | return result; |
| 87 | } |
| 88 | if (get_timer(start_time) > LPI2C_TIMEOUT_MS) { |
| 89 | debug("i2c: wait for tx ready: timeout\n"); |
| 90 | return -1; |
| 91 | } |
| 92 | } while (!txcount); |
| 93 | |
| 94 | return result; |
| 95 | } |
| 96 | |
Ye Li | 2c69546 | 2018-07-08 11:46:43 +0800 | [diff] [blame] | 97 | static int bus_i2c_send(struct udevice *bus, u8 *txbuf, int len) |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 98 | { |
Ye Li | 890813f | 2020-06-09 20:29:50 -0700 | [diff] [blame] | 99 | struct imx_lpi2c_bus *i2c_bus = dev_get_priv(bus); |
| 100 | struct imx_lpi2c_reg *regs = (struct imx_lpi2c_reg *)(i2c_bus->base); |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 101 | lpi2c_status_t result = LPI2C_SUCESS; |
| 102 | |
| 103 | /* empty tx */ |
| 104 | if (!len) |
| 105 | return result; |
| 106 | |
| 107 | while (len--) { |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 108 | result = bus_i2c_wait_for_tx_ready(regs); |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 109 | if (result) { |
Anatolij Gustschin | 422f8a8 | 2018-10-18 16:36:01 +0200 | [diff] [blame] | 110 | debug("i2c: send wait for tx ready: %d\n", result); |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 111 | return result; |
| 112 | } |
| 113 | writel(*txbuf++, ®s->mtdr); |
| 114 | } |
| 115 | |
| 116 | return result; |
| 117 | } |
| 118 | |
Ye Li | 2c69546 | 2018-07-08 11:46:43 +0800 | [diff] [blame] | 119 | static int bus_i2c_receive(struct udevice *bus, u8 *rxbuf, int len) |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 120 | { |
Ye Li | 890813f | 2020-06-09 20:29:50 -0700 | [diff] [blame] | 121 | struct imx_lpi2c_bus *i2c_bus = dev_get_priv(bus); |
| 122 | struct imx_lpi2c_reg *regs = (struct imx_lpi2c_reg *)(i2c_bus->base); |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 123 | lpi2c_status_t result = LPI2C_SUCESS; |
| 124 | u32 val; |
| 125 | ulong start_time = get_timer(0); |
| 126 | |
| 127 | /* empty read */ |
| 128 | if (!len) |
| 129 | return result; |
| 130 | |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 131 | result = bus_i2c_wait_for_tx_ready(regs); |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 132 | if (result) { |
| 133 | debug("i2c: receive wait fot tx ready: %d\n", result); |
| 134 | return result; |
| 135 | } |
| 136 | |
| 137 | /* clear all status flags */ |
| 138 | writel(0x7f00, ®s->msr); |
| 139 | /* send receive command */ |
| 140 | val = LPI2C_MTDR_CMD(0x1) | LPI2C_MTDR_DATA(len - 1); |
| 141 | writel(val, ®s->mtdr); |
| 142 | |
| 143 | while (len--) { |
| 144 | do { |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 145 | result = imx_lpci2c_check_clear_error(regs); |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 146 | if (result) { |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 147 | debug("i2c: receive check clear error: %d\n", |
| 148 | result); |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 149 | return result; |
| 150 | } |
| 151 | if (get_timer(start_time) > LPI2C_TIMEOUT_MS) { |
| 152 | debug("i2c: receive mrdr: timeout\n"); |
| 153 | return -1; |
| 154 | } |
| 155 | val = readl(®s->mrdr); |
| 156 | } while (val & LPI2C_MRDR_RXEMPTY_MASK); |
| 157 | *rxbuf++ = LPI2C_MRDR_DATA(val); |
| 158 | } |
| 159 | |
| 160 | return result; |
| 161 | } |
| 162 | |
Ye Li | 2c69546 | 2018-07-08 11:46:43 +0800 | [diff] [blame] | 163 | static int bus_i2c_start(struct udevice *bus, u8 addr, u8 dir) |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 164 | { |
Heinrich Schuchardt | 1a5d485 | 2018-03-18 11:14:56 +0100 | [diff] [blame] | 165 | lpi2c_status_t result; |
Ye Li | 890813f | 2020-06-09 20:29:50 -0700 | [diff] [blame] | 166 | struct imx_lpi2c_bus *i2c_bus = dev_get_priv(bus); |
| 167 | struct imx_lpi2c_reg *regs = (struct imx_lpi2c_reg *)(i2c_bus->base); |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 168 | u32 val; |
| 169 | |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 170 | result = imx_lpci2c_check_busy_bus(regs); |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 171 | if (result) { |
| 172 | debug("i2c: start check busy bus: 0x%x\n", result); |
Ye Li | 2c69546 | 2018-07-08 11:46:43 +0800 | [diff] [blame] | 173 | |
| 174 | /* Try to init the lpi2c then check the bus busy again */ |
Simon Glass | f0c99c5 | 2020-01-23 11:48:22 -0700 | [diff] [blame] | 175 | bus_i2c_init(bus, I2C_SPEED_STANDARD_RATE); |
Ye Li | 2c69546 | 2018-07-08 11:46:43 +0800 | [diff] [blame] | 176 | result = imx_lpci2c_check_busy_bus(regs); |
| 177 | if (result) { |
| 178 | printf("i2c: Error check busy bus: 0x%x\n", result); |
| 179 | return result; |
| 180 | } |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 181 | } |
| 182 | /* clear all status flags */ |
| 183 | writel(0x7f00, ®s->msr); |
| 184 | /* turn off auto-stop condition */ |
| 185 | val = readl(®s->mcfgr1) & ~LPI2C_MCFGR1_AUTOSTOP_MASK; |
| 186 | writel(val, ®s->mcfgr1); |
| 187 | /* wait tx fifo ready */ |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 188 | result = bus_i2c_wait_for_tx_ready(regs); |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 189 | if (result) { |
| 190 | debug("i2c: start wait for tx ready: 0x%x\n", result); |
| 191 | return result; |
| 192 | } |
| 193 | /* issue start command */ |
| 194 | val = LPI2C_MTDR_CMD(0x4) | (addr << 0x1) | dir; |
| 195 | writel(val, ®s->mtdr); |
| 196 | |
| 197 | return result; |
| 198 | } |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 199 | |
Ye Li | 2c69546 | 2018-07-08 11:46:43 +0800 | [diff] [blame] | 200 | static int bus_i2c_stop(struct udevice *bus) |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 201 | { |
Heinrich Schuchardt | 1a5d485 | 2018-03-18 11:14:56 +0100 | [diff] [blame] | 202 | lpi2c_status_t result; |
Ye Li | 890813f | 2020-06-09 20:29:50 -0700 | [diff] [blame] | 203 | struct imx_lpi2c_bus *i2c_bus = dev_get_priv(bus); |
| 204 | struct imx_lpi2c_reg *regs = (struct imx_lpi2c_reg *)(i2c_bus->base); |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 205 | u32 status; |
Gao Pan | 52d457b | 2018-07-08 11:46:41 +0800 | [diff] [blame] | 206 | ulong start_time; |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 207 | |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 208 | result = bus_i2c_wait_for_tx_ready(regs); |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 209 | if (result) { |
| 210 | debug("i2c: stop wait for tx ready: 0x%x\n", result); |
| 211 | return result; |
| 212 | } |
| 213 | |
| 214 | /* send stop command */ |
| 215 | writel(LPI2C_MTDR_CMD(0x2), ®s->mtdr); |
| 216 | |
Gao Pan | 52d457b | 2018-07-08 11:46:41 +0800 | [diff] [blame] | 217 | start_time = get_timer(0); |
| 218 | while (1) { |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 219 | status = readl(®s->msr); |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 220 | result = imx_lpci2c_check_clear_error(regs); |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 221 | /* stop detect flag */ |
| 222 | if (status & LPI2C_MSR_SDF_MASK) { |
| 223 | /* clear stop flag */ |
| 224 | status &= LPI2C_MSR_SDF_MASK; |
| 225 | writel(status, ®s->msr); |
| 226 | break; |
| 227 | } |
Gao Pan | 52d457b | 2018-07-08 11:46:41 +0800 | [diff] [blame] | 228 | |
| 229 | if (get_timer(start_time) > LPI2C_NACK_TOUT_MS) { |
| 230 | debug("stop timeout\n"); |
| 231 | return -ETIMEDOUT; |
| 232 | } |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | return result; |
| 236 | } |
| 237 | |
Ye Li | 2c69546 | 2018-07-08 11:46:43 +0800 | [diff] [blame] | 238 | static int bus_i2c_read(struct udevice *bus, u32 chip, u8 *buf, int len) |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 239 | { |
Heinrich Schuchardt | 1a5d485 | 2018-03-18 11:14:56 +0100 | [diff] [blame] | 240 | lpi2c_status_t result; |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 241 | |
Ye Li | 2c69546 | 2018-07-08 11:46:43 +0800 | [diff] [blame] | 242 | result = bus_i2c_start(bus, chip, 1); |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 243 | if (result) |
| 244 | return result; |
Ye Li | 2c69546 | 2018-07-08 11:46:43 +0800 | [diff] [blame] | 245 | result = bus_i2c_receive(bus, buf, len); |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 246 | if (result) |
| 247 | return result; |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 248 | |
| 249 | return result; |
| 250 | } |
| 251 | |
Ye Li | 2c69546 | 2018-07-08 11:46:43 +0800 | [diff] [blame] | 252 | static int bus_i2c_write(struct udevice *bus, u32 chip, u8 *buf, int len) |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 253 | { |
Heinrich Schuchardt | 1a5d485 | 2018-03-18 11:14:56 +0100 | [diff] [blame] | 254 | lpi2c_status_t result; |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 255 | |
Ye Li | 2c69546 | 2018-07-08 11:46:43 +0800 | [diff] [blame] | 256 | result = bus_i2c_start(bus, chip, 0); |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 257 | if (result) |
| 258 | return result; |
Ye Li | 2c69546 | 2018-07-08 11:46:43 +0800 | [diff] [blame] | 259 | result = bus_i2c_send(bus, buf, len); |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 260 | if (result) |
| 261 | return result; |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 262 | |
| 263 | return result; |
| 264 | } |
| 265 | |
Peng Fan | 5b269c4 | 2018-07-17 20:38:33 +0800 | [diff] [blame] | 266 | u32 __weak imx_get_i2cclk(u32 i2c_num) |
| 267 | { |
| 268 | return 0; |
| 269 | } |
| 270 | |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 271 | static int bus_i2c_set_bus_speed(struct udevice *bus, int speed) |
| 272 | { |
Peng Fan | 5b269c4 | 2018-07-17 20:38:33 +0800 | [diff] [blame] | 273 | struct imx_lpi2c_bus *i2c_bus = dev_get_priv(bus); |
Ye Li | 890813f | 2020-06-09 20:29:50 -0700 | [diff] [blame] | 274 | struct imx_lpi2c_reg *regs = (struct imx_lpi2c_reg *)(i2c_bus->base); |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 275 | u32 val; |
| 276 | u32 preescale = 0, best_pre = 0, clkhi = 0; |
| 277 | u32 best_clkhi = 0, abs_error = 0, rate; |
| 278 | u32 error = 0xffffffff; |
| 279 | u32 clock_rate; |
| 280 | bool mode; |
| 281 | int i; |
| 282 | |
Ye Li | 725a7ac | 2023-04-06 18:26:35 +0800 | [diff] [blame] | 283 | if (CONFIG_IS_ENABLED(CLK)) { |
Peng Fan | 5b269c4 | 2018-07-17 20:38:33 +0800 | [diff] [blame] | 284 | clock_rate = clk_get_rate(&i2c_bus->per_clk); |
| 285 | if (clock_rate <= 0) { |
| 286 | dev_err(bus, "Failed to get i2c clk: %d\n", clock_rate); |
| 287 | return clock_rate; |
| 288 | } |
| 289 | } else { |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 290 | clock_rate = imx_get_i2cclk(dev_seq(bus)); |
Peng Fan | 5b269c4 | 2018-07-17 20:38:33 +0800 | [diff] [blame] | 291 | if (!clock_rate) |
| 292 | return -EPERM; |
| 293 | } |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 294 | |
| 295 | mode = (readl(®s->mcr) & LPI2C_MCR_MEN_MASK) >> LPI2C_MCR_MEN_SHIFT; |
| 296 | /* disable master mode */ |
| 297 | val = readl(®s->mcr) & ~LPI2C_MCR_MEN_MASK; |
| 298 | writel(val | LPI2C_MCR_MEN(0), ®s->mcr); |
| 299 | |
| 300 | for (preescale = 1; (preescale <= 128) && |
| 301 | (error != 0); preescale = 2 * preescale) { |
| 302 | for (clkhi = 1; clkhi < 32; clkhi++) { |
| 303 | if (clkhi == 1) |
| 304 | rate = (clock_rate / preescale) / (1 + 3 + 2 + 2 / preescale); |
| 305 | else |
| 306 | rate = (clock_rate / preescale / (3 * clkhi + 2 + 2 / preescale)); |
| 307 | |
| 308 | abs_error = speed > rate ? speed - rate : rate - speed; |
| 309 | |
| 310 | if (abs_error < error) { |
| 311 | best_pre = preescale; |
| 312 | best_clkhi = clkhi; |
| 313 | error = abs_error; |
| 314 | if (abs_error == 0) |
| 315 | break; |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | /* Standard, fast, fast mode plus and ultra-fast transfers. */ |
| 321 | val = LPI2C_MCCR0_CLKHI(best_clkhi); |
| 322 | if (best_clkhi < 2) |
| 323 | val |= LPI2C_MCCR0_CLKLO(3) | LPI2C_MCCR0_SETHOLD(2) | LPI2C_MCCR0_DATAVD(1); |
| 324 | else |
| 325 | val |= LPI2C_MCCR0_CLKLO(2 * best_clkhi) | LPI2C_MCCR0_SETHOLD(best_clkhi) | |
| 326 | LPI2C_MCCR0_DATAVD(best_clkhi / 2); |
| 327 | writel(val, ®s->mccr0); |
| 328 | |
| 329 | for (i = 0; i < 8; i++) { |
| 330 | if (best_pre == (1 << i)) { |
| 331 | best_pre = i; |
| 332 | break; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | val = readl(®s->mcfgr1) & ~LPI2C_MCFGR1_PRESCALE_MASK; |
| 337 | writel(val | LPI2C_MCFGR1_PRESCALE(best_pre), ®s->mcfgr1); |
| 338 | |
| 339 | if (mode) { |
| 340 | val = readl(®s->mcr) & ~LPI2C_MCR_MEN_MASK; |
| 341 | writel(val | LPI2C_MCR_MEN(1), ®s->mcr); |
| 342 | } |
| 343 | |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | static int bus_i2c_init(struct udevice *bus, int speed) |
| 348 | { |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 349 | u32 val; |
| 350 | int ret; |
| 351 | |
Ye Li | 890813f | 2020-06-09 20:29:50 -0700 | [diff] [blame] | 352 | struct imx_lpi2c_bus *i2c_bus = dev_get_priv(bus); |
| 353 | struct imx_lpi2c_reg *regs = (struct imx_lpi2c_reg *)(i2c_bus->base); |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 354 | /* reset peripheral */ |
| 355 | writel(LPI2C_MCR_RST_MASK, ®s->mcr); |
| 356 | writel(0x0, ®s->mcr); |
| 357 | /* Disable Dozen mode */ |
| 358 | writel(LPI2C_MCR_DBGEN(0) | LPI2C_MCR_DOZEN(1), ®s->mcr); |
| 359 | /* host request disable, active high, external pin */ |
| 360 | val = readl(®s->mcfgr0); |
| 361 | val &= (~(LPI2C_MCFGR0_HREN_MASK | LPI2C_MCFGR0_HRPOL_MASK | |
| 362 | LPI2C_MCFGR0_HRSEL_MASK)); |
| 363 | val |= LPI2C_MCFGR0_HRPOL(0x1); |
| 364 | writel(val, ®s->mcfgr0); |
| 365 | /* pincfg and ignore ack */ |
| 366 | val = readl(®s->mcfgr1); |
| 367 | val &= ~(LPI2C_MCFGR1_PINCFG_MASK | LPI2C_MCFGR1_IGNACK_MASK); |
| 368 | val |= LPI2C_MCFGR1_PINCFG(0x0); /* 2 pin open drain */ |
| 369 | val |= LPI2C_MCFGR1_IGNACK(0x0); /* ignore nack */ |
| 370 | writel(val, ®s->mcfgr1); |
| 371 | |
| 372 | ret = bus_i2c_set_bus_speed(bus, speed); |
| 373 | |
| 374 | /* enable lpi2c in master mode */ |
| 375 | val = readl(®s->mcr) & ~LPI2C_MCR_MEN_MASK; |
| 376 | writel(val | LPI2C_MCR_MEN(1), ®s->mcr); |
| 377 | |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 378 | debug("i2c : controller bus %d, speed %d:\n", dev_seq(bus), speed); |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 379 | |
| 380 | return ret; |
| 381 | } |
| 382 | |
| 383 | static int imx_lpi2c_probe_chip(struct udevice *bus, u32 chip, |
| 384 | u32 chip_flags) |
| 385 | { |
Heinrich Schuchardt | 1a5d485 | 2018-03-18 11:14:56 +0100 | [diff] [blame] | 386 | lpi2c_status_t result; |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 387 | |
Ye Li | 2c69546 | 2018-07-08 11:46:43 +0800 | [diff] [blame] | 388 | result = bus_i2c_start(bus, chip, 0); |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 389 | if (result) { |
Ye Li | 2c69546 | 2018-07-08 11:46:43 +0800 | [diff] [blame] | 390 | bus_i2c_stop(bus); |
Simon Glass | f0c99c5 | 2020-01-23 11:48:22 -0700 | [diff] [blame] | 391 | bus_i2c_init(bus, I2C_SPEED_STANDARD_RATE); |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 392 | return result; |
| 393 | } |
| 394 | |
Ye Li | 2c69546 | 2018-07-08 11:46:43 +0800 | [diff] [blame] | 395 | result = bus_i2c_stop(bus); |
Gao Pan | 52d457b | 2018-07-08 11:46:41 +0800 | [diff] [blame] | 396 | if (result) |
Simon Glass | f0c99c5 | 2020-01-23 11:48:22 -0700 | [diff] [blame] | 397 | bus_i2c_init(bus, I2C_SPEED_STANDARD_RATE); |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 398 | |
| 399 | return result; |
| 400 | } |
| 401 | |
| 402 | static int imx_lpi2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs) |
| 403 | { |
Ye Li | a917ed9 | 2018-07-08 11:46:42 +0800 | [diff] [blame] | 404 | int ret = 0, ret_stop; |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 405 | |
| 406 | for (; nmsgs > 0; nmsgs--, msg++) { |
| 407 | debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len); |
| 408 | if (msg->flags & I2C_M_RD) |
Ye Li | 2c69546 | 2018-07-08 11:46:43 +0800 | [diff] [blame] | 409 | ret = bus_i2c_read(bus, msg->addr, msg->buf, msg->len); |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 410 | else { |
Ye Li | 2c69546 | 2018-07-08 11:46:43 +0800 | [diff] [blame] | 411 | ret = bus_i2c_write(bus, msg->addr, msg->buf, |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 412 | msg->len); |
| 413 | if (ret) |
| 414 | break; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | if (ret) |
| 419 | debug("i2c_write: error sending\n"); |
| 420 | |
Ye Li | 2c69546 | 2018-07-08 11:46:43 +0800 | [diff] [blame] | 421 | ret_stop = bus_i2c_stop(bus); |
Ye Li | a917ed9 | 2018-07-08 11:46:42 +0800 | [diff] [blame] | 422 | if (ret_stop) |
| 423 | debug("i2c_xfer: stop bus error\n"); |
| 424 | |
| 425 | ret |= ret_stop; |
| 426 | |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 427 | return ret; |
| 428 | } |
| 429 | |
| 430 | static int imx_lpi2c_set_bus_speed(struct udevice *bus, unsigned int speed) |
| 431 | { |
| 432 | return bus_i2c_set_bus_speed(bus, speed); |
| 433 | } |
| 434 | |
Peng Fan | 5b269c4 | 2018-07-17 20:38:33 +0800 | [diff] [blame] | 435 | __weak int enable_i2c_clk(unsigned char enable, unsigned int i2c_num) |
| 436 | { |
| 437 | return 0; |
| 438 | } |
| 439 | |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 440 | static int imx_lpi2c_probe(struct udevice *bus) |
| 441 | { |
| 442 | struct imx_lpi2c_bus *i2c_bus = dev_get_priv(bus); |
| 443 | fdt_addr_t addr; |
| 444 | int ret; |
| 445 | |
| 446 | i2c_bus->driver_data = dev_get_driver_data(bus); |
| 447 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 448 | addr = dev_read_addr(bus); |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 449 | if (addr == FDT_ADDR_T_NONE) |
Simon Glass | f44b4bf | 2017-09-17 16:54:53 -0600 | [diff] [blame] | 450 | return -EINVAL; |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 451 | |
| 452 | i2c_bus->base = addr; |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 453 | i2c_bus->index = dev_seq(bus); |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 454 | i2c_bus->bus = bus; |
| 455 | |
| 456 | /* power up i2c resource */ |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 457 | ret = init_i2c_power(dev_seq(bus)); |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 458 | if (ret) { |
| 459 | debug("init_i2c_power err = %d\n", ret); |
| 460 | return ret; |
| 461 | } |
| 462 | |
Ye Li | 725a7ac | 2023-04-06 18:26:35 +0800 | [diff] [blame] | 463 | if (CONFIG_IS_ENABLED(CLK)) { |
Peng Fan | 5b269c4 | 2018-07-17 20:38:33 +0800 | [diff] [blame] | 464 | ret = clk_get_by_name(bus, "per", &i2c_bus->per_clk); |
| 465 | if (ret) { |
| 466 | dev_err(bus, "Failed to get per clk\n"); |
| 467 | return ret; |
| 468 | } |
| 469 | ret = clk_enable(&i2c_bus->per_clk); |
| 470 | if (ret) { |
| 471 | dev_err(bus, "Failed to enable per clk\n"); |
| 472 | return ret; |
| 473 | } |
Peng Fan | f0fb386 | 2019-07-24 08:54:16 +0000 | [diff] [blame] | 474 | |
| 475 | ret = clk_get_by_name(bus, "ipg", &i2c_bus->ipg_clk); |
| 476 | if (ret) { |
| 477 | dev_err(bus, "Failed to get ipg clk\n"); |
| 478 | return ret; |
| 479 | } |
| 480 | ret = clk_enable(&i2c_bus->ipg_clk); |
| 481 | if (ret) { |
| 482 | dev_err(bus, "Failed to enable ipg clk\n"); |
| 483 | return ret; |
| 484 | } |
Peng Fan | 5b269c4 | 2018-07-17 20:38:33 +0800 | [diff] [blame] | 485 | } else { |
| 486 | /* To i.MX7ULP, only i2c4-7 can be handled by A7 core */ |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 487 | ret = enable_i2c_clk(1, dev_seq(bus)); |
Peng Fan | 5b269c4 | 2018-07-17 20:38:33 +0800 | [diff] [blame] | 488 | if (ret < 0) |
| 489 | return ret; |
| 490 | } |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 491 | |
Simon Glass | f0c99c5 | 2020-01-23 11:48:22 -0700 | [diff] [blame] | 492 | ret = bus_i2c_init(bus, I2C_SPEED_STANDARD_RATE); |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 493 | if (ret < 0) |
| 494 | return ret; |
| 495 | |
Anatolij Gustschin | 422f8a8 | 2018-10-18 16:36:01 +0200 | [diff] [blame] | 496 | debug("i2c : controller bus %d at 0x%lx , speed %d: ", |
Simon Glass | 75e534b | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 497 | dev_seq(bus), i2c_bus->base, |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 498 | i2c_bus->speed); |
| 499 | |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | static const struct dm_i2c_ops imx_lpi2c_ops = { |
| 504 | .xfer = imx_lpi2c_xfer, |
| 505 | .probe_chip = imx_lpi2c_probe_chip, |
| 506 | .set_bus_speed = imx_lpi2c_set_bus_speed, |
| 507 | }; |
| 508 | |
| 509 | static const struct udevice_id imx_lpi2c_ids[] = { |
| 510 | { .compatible = "fsl,imx7ulp-lpi2c", }, |
Ye Li | d9103f8 | 2018-07-08 11:46:40 +0800 | [diff] [blame] | 511 | { .compatible = "fsl,imx8qm-lpi2c", }, |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 512 | {} |
| 513 | }; |
| 514 | |
| 515 | U_BOOT_DRIVER(imx_lpi2c) = { |
| 516 | .name = "imx_lpi2c", |
| 517 | .id = UCLASS_I2C, |
| 518 | .of_match = imx_lpi2c_ids, |
| 519 | .probe = imx_lpi2c_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 520 | .priv_auto = sizeof(struct imx_lpi2c_bus), |
Peng Fan | d684adb | 2017-02-24 09:54:18 +0800 | [diff] [blame] | 521 | .ops = &imx_lpi2c_ops, |
| 522 | }; |