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