Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Moritz Fischer <moritz.fischer@ettus.com> |
| 4 | * IP from Cadence (ID T-CS-PE-0007-100, Version R1p10f2) |
| 5 | * |
| 6 | * This file is based on: drivers/i2c/zynq_i2c.c, |
| 7 | * with added driver-model support and code cleanup. |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
Simon Glass | 11c89f3 | 2017-05-17 17:18:03 -0600 | [diff] [blame] | 11 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 13 | #include <linux/bitops.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 14 | #include <linux/delay.h> |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 15 | #include <linux/types.h> |
| 16 | #include <linux/io.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 17 | #include <linux/errno.h> |
T Karthik Reddy | f814987 | 2021-02-03 03:10:46 -0700 | [diff] [blame] | 18 | #include <dm/device_compat.h> |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 19 | #include <dm/root.h> |
| 20 | #include <i2c.h> |
| 21 | #include <fdtdec.h> |
| 22 | #include <mapmem.h> |
Moritz Fischer | 9393f58 | 2017-01-16 09:50:46 -0800 | [diff] [blame] | 23 | #include <wait_bit.h> |
Tomasz Gorochowik | 59e2585 | 2019-01-03 13:36:33 +0100 | [diff] [blame] | 24 | #include <clk.h> |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 25 | |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 26 | /* i2c register set */ |
| 27 | struct cdns_i2c_regs { |
| 28 | u32 control; |
| 29 | u32 status; |
| 30 | u32 address; |
| 31 | u32 data; |
| 32 | u32 interrupt_status; |
| 33 | u32 transfer_size; |
| 34 | u32 slave_mon_pause; |
| 35 | u32 time_out; |
| 36 | u32 interrupt_mask; |
| 37 | u32 interrupt_enable; |
| 38 | u32 interrupt_disable; |
| 39 | }; |
| 40 | |
| 41 | /* Control register fields */ |
| 42 | #define CDNS_I2C_CONTROL_RW 0x00000001 |
| 43 | #define CDNS_I2C_CONTROL_MS 0x00000002 |
| 44 | #define CDNS_I2C_CONTROL_NEA 0x00000004 |
| 45 | #define CDNS_I2C_CONTROL_ACKEN 0x00000008 |
| 46 | #define CDNS_I2C_CONTROL_HOLD 0x00000010 |
| 47 | #define CDNS_I2C_CONTROL_SLVMON 0x00000020 |
| 48 | #define CDNS_I2C_CONTROL_CLR_FIFO 0x00000040 |
| 49 | #define CDNS_I2C_CONTROL_DIV_B_SHIFT 8 |
| 50 | #define CDNS_I2C_CONTROL_DIV_B_MASK 0x00003F00 |
| 51 | #define CDNS_I2C_CONTROL_DIV_A_SHIFT 14 |
| 52 | #define CDNS_I2C_CONTROL_DIV_A_MASK 0x0000C000 |
| 53 | |
| 54 | /* Status register values */ |
| 55 | #define CDNS_I2C_STATUS_RXDV 0x00000020 |
| 56 | #define CDNS_I2C_STATUS_TXDV 0x00000040 |
| 57 | #define CDNS_I2C_STATUS_RXOVF 0x00000080 |
| 58 | #define CDNS_I2C_STATUS_BA 0x00000100 |
| 59 | |
| 60 | /* Interrupt register fields */ |
| 61 | #define CDNS_I2C_INTERRUPT_COMP 0x00000001 |
| 62 | #define CDNS_I2C_INTERRUPT_DATA 0x00000002 |
| 63 | #define CDNS_I2C_INTERRUPT_NACK 0x00000004 |
| 64 | #define CDNS_I2C_INTERRUPT_TO 0x00000008 |
| 65 | #define CDNS_I2C_INTERRUPT_SLVRDY 0x00000010 |
| 66 | #define CDNS_I2C_INTERRUPT_RXOVF 0x00000020 |
| 67 | #define CDNS_I2C_INTERRUPT_TXOVF 0x00000040 |
| 68 | #define CDNS_I2C_INTERRUPT_RXUNF 0x00000080 |
| 69 | #define CDNS_I2C_INTERRUPT_ARBLOST 0x00000200 |
| 70 | |
Siva Durga Prasad Paladugu | 4f0205b | 2019-03-07 11:52:48 +0100 | [diff] [blame] | 71 | #define CDNS_I2C_INTERRUPTS_MASK (CDNS_I2C_INTERRUPT_COMP | \ |
| 72 | CDNS_I2C_INTERRUPT_DATA | \ |
| 73 | CDNS_I2C_INTERRUPT_NACK | \ |
| 74 | CDNS_I2C_INTERRUPT_TO | \ |
| 75 | CDNS_I2C_INTERRUPT_SLVRDY | \ |
| 76 | CDNS_I2C_INTERRUPT_RXOVF | \ |
| 77 | CDNS_I2C_INTERRUPT_TXOVF | \ |
| 78 | CDNS_I2C_INTERRUPT_RXUNF | \ |
| 79 | CDNS_I2C_INTERRUPT_ARBLOST) |
| 80 | |
Pei Yue Ho | ca39472 | 2023-02-13 00:02:41 -0800 | [diff] [blame] | 81 | #define CDNS_I2C_FIFO_DEPTH_DEFAULT 16 |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 82 | #define CDNS_I2C_TRANSFER_SIZE_MAX 255 /* Controller transfer limit */ |
Moritz Fischer | 9393f58 | 2017-01-16 09:50:46 -0800 | [diff] [blame] | 83 | #define CDNS_I2C_TRANSFER_SIZE (CDNS_I2C_TRANSFER_SIZE_MAX - 3) |
| 84 | |
Moritz Fischer | 61f0651 | 2017-01-16 09:50:44 -0800 | [diff] [blame] | 85 | #define CDNS_I2C_BROKEN_HOLD_BIT BIT(0) |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 86 | |
Siva Durga Prasad Paladugu | 07c5a1b | 2019-03-07 11:52:49 +0100 | [diff] [blame] | 87 | #define CDNS_I2C_ARB_LOST_MAX_RETRIES 10 |
| 88 | |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 89 | #ifdef DEBUG |
| 90 | static void cdns_i2c_debug_status(struct cdns_i2c_regs *cdns_i2c) |
| 91 | { |
| 92 | int int_status; |
| 93 | int status; |
| 94 | int_status = readl(&cdns_i2c->interrupt_status); |
| 95 | |
| 96 | status = readl(&cdns_i2c->status); |
| 97 | if (int_status || status) { |
| 98 | debug("Status: "); |
| 99 | if (int_status & CDNS_I2C_INTERRUPT_COMP) |
| 100 | debug("COMP "); |
| 101 | if (int_status & CDNS_I2C_INTERRUPT_DATA) |
| 102 | debug("DATA "); |
| 103 | if (int_status & CDNS_I2C_INTERRUPT_NACK) |
| 104 | debug("NACK "); |
| 105 | if (int_status & CDNS_I2C_INTERRUPT_TO) |
| 106 | debug("TO "); |
| 107 | if (int_status & CDNS_I2C_INTERRUPT_SLVRDY) |
| 108 | debug("SLVRDY "); |
| 109 | if (int_status & CDNS_I2C_INTERRUPT_RXOVF) |
| 110 | debug("RXOVF "); |
| 111 | if (int_status & CDNS_I2C_INTERRUPT_TXOVF) |
| 112 | debug("TXOVF "); |
| 113 | if (int_status & CDNS_I2C_INTERRUPT_RXUNF) |
| 114 | debug("RXUNF "); |
| 115 | if (int_status & CDNS_I2C_INTERRUPT_ARBLOST) |
| 116 | debug("ARBLOST "); |
| 117 | if (status & CDNS_I2C_STATUS_RXDV) |
| 118 | debug("RXDV "); |
| 119 | if (status & CDNS_I2C_STATUS_TXDV) |
| 120 | debug("TXDV "); |
| 121 | if (status & CDNS_I2C_STATUS_RXOVF) |
| 122 | debug("RXOVF "); |
| 123 | if (status & CDNS_I2C_STATUS_BA) |
| 124 | debug("BA "); |
| 125 | debug("TS%d ", readl(&cdns_i2c->transfer_size)); |
| 126 | debug("\n"); |
| 127 | } |
| 128 | } |
| 129 | #endif |
| 130 | |
| 131 | struct i2c_cdns_bus { |
| 132 | int id; |
Michal Simek | 91429f4 | 2016-04-14 14:15:49 +0200 | [diff] [blame] | 133 | unsigned int input_freq; |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 134 | struct cdns_i2c_regs __iomem *regs; /* register base */ |
Moritz Fischer | 61f0651 | 2017-01-16 09:50:44 -0800 | [diff] [blame] | 135 | |
| 136 | int hold_flag; |
| 137 | u32 quirks; |
Pei Yue Ho | ca39472 | 2023-02-13 00:02:41 -0800 | [diff] [blame] | 138 | u32 fifo_depth; |
Moritz Fischer | 61f0651 | 2017-01-16 09:50:44 -0800 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | struct cdns_i2c_platform_data { |
| 142 | u32 quirks; |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 143 | }; |
| 144 | |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 145 | /* Wait for an interrupt */ |
| 146 | static u32 cdns_i2c_wait(struct cdns_i2c_regs *cdns_i2c, u32 mask) |
| 147 | { |
| 148 | int timeout, int_status; |
| 149 | |
| 150 | for (timeout = 0; timeout < 100; timeout++) { |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 151 | int_status = readl(&cdns_i2c->interrupt_status); |
| 152 | if (int_status & mask) |
| 153 | break; |
Moritz Fischer | 0b24579 | 2017-01-16 09:50:45 -0800 | [diff] [blame] | 154 | udelay(100); |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /* Clear interrupt status flags */ |
| 158 | writel(int_status & mask, &cdns_i2c->interrupt_status); |
| 159 | |
| 160 | return int_status & mask; |
| 161 | } |
| 162 | |
Michal Simek | 91429f4 | 2016-04-14 14:15:49 +0200 | [diff] [blame] | 163 | #define CDNS_I2C_DIVA_MAX 4 |
| 164 | #define CDNS_I2C_DIVB_MAX 64 |
| 165 | |
| 166 | static int cdns_i2c_calc_divs(unsigned long *f, unsigned long input_clk, |
| 167 | unsigned int *a, unsigned int *b) |
| 168 | { |
| 169 | unsigned long fscl = *f, best_fscl = *f, actual_fscl, temp; |
| 170 | unsigned int div_a, div_b, calc_div_a = 0, calc_div_b = 0; |
| 171 | unsigned int last_error, current_error; |
| 172 | |
| 173 | /* calculate (divisor_a+1) x (divisor_b+1) */ |
| 174 | temp = input_clk / (22 * fscl); |
| 175 | |
| 176 | /* |
| 177 | * If the calculated value is negative or 0CDNS_I2C_DIVA_MAX, |
| 178 | * the fscl input is out of range. Return error. |
| 179 | */ |
| 180 | if (!temp || (temp > (CDNS_I2C_DIVA_MAX * CDNS_I2C_DIVB_MAX))) |
| 181 | return -EINVAL; |
| 182 | |
| 183 | last_error = -1; |
| 184 | for (div_a = 0; div_a < CDNS_I2C_DIVA_MAX; div_a++) { |
| 185 | div_b = DIV_ROUND_UP(input_clk, 22 * fscl * (div_a + 1)); |
| 186 | |
| 187 | if ((div_b < 1) || (div_b > CDNS_I2C_DIVB_MAX)) |
| 188 | continue; |
| 189 | div_b--; |
| 190 | |
| 191 | actual_fscl = input_clk / (22 * (div_a + 1) * (div_b + 1)); |
| 192 | |
| 193 | if (actual_fscl > fscl) |
| 194 | continue; |
| 195 | |
| 196 | current_error = ((actual_fscl > fscl) ? (actual_fscl - fscl) : |
| 197 | (fscl - actual_fscl)); |
| 198 | |
| 199 | if (last_error > current_error) { |
| 200 | calc_div_a = div_a; |
| 201 | calc_div_b = div_b; |
| 202 | best_fscl = actual_fscl; |
| 203 | last_error = current_error; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | *a = calc_div_a; |
| 208 | *b = calc_div_b; |
| 209 | *f = best_fscl; |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 214 | static int cdns_i2c_set_bus_speed(struct udevice *dev, unsigned int speed) |
| 215 | { |
Michal Simek | 48cd206 | 2016-04-14 14:15:48 +0200 | [diff] [blame] | 216 | struct i2c_cdns_bus *bus = dev_get_priv(dev); |
Michal Simek | 91429f4 | 2016-04-14 14:15:49 +0200 | [diff] [blame] | 217 | u32 div_a = 0, div_b = 0; |
| 218 | unsigned long speed_p = speed; |
| 219 | int ret = 0; |
Michal Simek | 48cd206 | 2016-04-14 14:15:48 +0200 | [diff] [blame] | 220 | |
Simon Glass | f0c99c5 | 2020-01-23 11:48:22 -0700 | [diff] [blame] | 221 | if (speed > I2C_SPEED_FAST_RATE) { |
Michal Simek | 91429f4 | 2016-04-14 14:15:49 +0200 | [diff] [blame] | 222 | debug("%s, failed to set clock speed to %u\n", __func__, |
| 223 | speed); |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 224 | return -EINVAL; |
| 225 | } |
| 226 | |
Michal Simek | 91429f4 | 2016-04-14 14:15:49 +0200 | [diff] [blame] | 227 | ret = cdns_i2c_calc_divs(&speed_p, bus->input_freq, &div_a, &div_b); |
| 228 | if (ret) |
| 229 | return ret; |
| 230 | |
| 231 | debug("%s: div_a: %d, div_b: %d, input freq: %d, speed: %d/%ld\n", |
| 232 | __func__, div_a, div_b, bus->input_freq, speed, speed_p); |
| 233 | |
| 234 | writel((div_b << CDNS_I2C_CONTROL_DIV_B_SHIFT) | |
| 235 | (div_a << CDNS_I2C_CONTROL_DIV_A_SHIFT), &bus->regs->control); |
Michal Simek | 48cd206 | 2016-04-14 14:15:48 +0200 | [diff] [blame] | 236 | |
| 237 | /* Enable master mode, ack, and 7-bit addressing */ |
| 238 | setbits_le32(&bus->regs->control, CDNS_I2C_CONTROL_MS | |
| 239 | CDNS_I2C_CONTROL_ACKEN | CDNS_I2C_CONTROL_NEA); |
| 240 | |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 241 | return 0; |
| 242 | } |
| 243 | |
Siva Durga Prasad Paladugu | 07c5a1b | 2019-03-07 11:52:49 +0100 | [diff] [blame] | 244 | static inline u32 is_arbitration_lost(struct cdns_i2c_regs *regs) |
| 245 | { |
| 246 | return (readl(®s->interrupt_status) & CDNS_I2C_INTERRUPT_ARBLOST); |
| 247 | } |
| 248 | |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 249 | static int cdns_i2c_write_data(struct i2c_cdns_bus *i2c_bus, u32 addr, u8 *data, |
Moritz Fischer | 61f0651 | 2017-01-16 09:50:44 -0800 | [diff] [blame] | 250 | u32 len) |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 251 | { |
| 252 | u8 *cur_data = data; |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 253 | struct cdns_i2c_regs *regs = i2c_bus->regs; |
Siva Durga Prasad Paladugu | 07c5a1b | 2019-03-07 11:52:49 +0100 | [diff] [blame] | 254 | u32 ret; |
Sai Pavan Boddu | 252b4a3 | 2022-03-01 09:16:51 +0100 | [diff] [blame] | 255 | bool start = 1; |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 256 | |
Moritz Fischer | 9393f58 | 2017-01-16 09:50:46 -0800 | [diff] [blame] | 257 | /* Set the controller in Master transmit mode and clear FIFO */ |
Moritz Fischer | 61f0651 | 2017-01-16 09:50:44 -0800 | [diff] [blame] | 258 | setbits_le32(®s->control, CDNS_I2C_CONTROL_CLR_FIFO); |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 259 | clrbits_le32(®s->control, CDNS_I2C_CONTROL_RW); |
| 260 | |
Sai Pavan Boddu | 19497b4 | 2022-03-01 09:16:52 +0100 | [diff] [blame] | 261 | /* |
| 262 | * For sequential data load hold the bus. |
Moritz Fischer | 9393f58 | 2017-01-16 09:50:46 -0800 | [diff] [blame] | 263 | */ |
Sai Pavan Boddu | 19497b4 | 2022-03-01 09:16:52 +0100 | [diff] [blame] | 264 | if (len > 1) |
Moritz Fischer | 9393f58 | 2017-01-16 09:50:46 -0800 | [diff] [blame] | 265 | setbits_le32(®s->control, CDNS_I2C_CONTROL_HOLD); |
| 266 | |
| 267 | /* Clear the interrupts in status register */ |
Siva Durga Prasad Paladugu | 4f0205b | 2019-03-07 11:52:48 +0100 | [diff] [blame] | 268 | writel(CDNS_I2C_INTERRUPTS_MASK, ®s->interrupt_status); |
Moritz Fischer | 9393f58 | 2017-01-16 09:50:46 -0800 | [diff] [blame] | 269 | |
Sai Pavan Boddu | 19497b4 | 2022-03-01 09:16:52 +0100 | [diff] [blame] | 270 | /* In case of Probe (i.e no data), start the transfer */ |
| 271 | if (!len) |
| 272 | writel(addr, ®s->address); |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 273 | |
Siva Durga Prasad Paladugu | 07c5a1b | 2019-03-07 11:52:49 +0100 | [diff] [blame] | 274 | while (len-- && !is_arbitration_lost(regs)) { |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 275 | writel(*(cur_data++), ®s->data); |
Sai Pavan Boddu | 252b4a3 | 2022-03-01 09:16:51 +0100 | [diff] [blame] | 276 | /* Trigger write only after loading data */ |
| 277 | if (start) { |
| 278 | writel(addr, ®s->address); |
| 279 | start = 0; |
| 280 | } |
Pei Yue Ho | ca39472 | 2023-02-13 00:02:41 -0800 | [diff] [blame] | 281 | if (len && readl(®s->transfer_size) == i2c_bus->fifo_depth) { |
Siva Durga Prasad Paladugu | 07c5a1b | 2019-03-07 11:52:49 +0100 | [diff] [blame] | 282 | ret = cdns_i2c_wait(regs, CDNS_I2C_INTERRUPT_COMP | |
| 283 | CDNS_I2C_INTERRUPT_ARBLOST); |
| 284 | if (ret & CDNS_I2C_INTERRUPT_ARBLOST) |
| 285 | return -EAGAIN; |
| 286 | if (ret & CDNS_I2C_INTERRUPT_COMP) |
| 287 | continue; |
| 288 | /* Release the bus */ |
| 289 | clrbits_le32(®s->control, |
| 290 | CDNS_I2C_CONTROL_HOLD); |
| 291 | return -ETIMEDOUT; |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
Siva Durga Prasad Paladugu | 07c5a1b | 2019-03-07 11:52:49 +0100 | [diff] [blame] | 295 | if (len && is_arbitration_lost(regs)) |
| 296 | return -EAGAIN; |
| 297 | |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 298 | /* All done... release the bus */ |
Moritz Fischer | 61f0651 | 2017-01-16 09:50:44 -0800 | [diff] [blame] | 299 | if (!i2c_bus->hold_flag) |
| 300 | clrbits_le32(®s->control, CDNS_I2C_CONTROL_HOLD); |
| 301 | |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 302 | /* Wait for the address and data to be sent */ |
Siva Durga Prasad Paladugu | 07c5a1b | 2019-03-07 11:52:49 +0100 | [diff] [blame] | 303 | ret = cdns_i2c_wait(regs, CDNS_I2C_INTERRUPT_COMP | |
| 304 | CDNS_I2C_INTERRUPT_ARBLOST); |
| 305 | if (!(ret & (CDNS_I2C_INTERRUPT_ARBLOST | |
| 306 | CDNS_I2C_INTERRUPT_COMP))) |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 307 | return -ETIMEDOUT; |
Siva Durga Prasad Paladugu | 07c5a1b | 2019-03-07 11:52:49 +0100 | [diff] [blame] | 308 | if (ret & CDNS_I2C_INTERRUPT_ARBLOST) |
| 309 | return -EAGAIN; |
| 310 | |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 311 | return 0; |
| 312 | } |
| 313 | |
Pei Yue Ho | ca39472 | 2023-02-13 00:02:41 -0800 | [diff] [blame] | 314 | static inline bool cdns_is_hold_quirk(struct i2c_cdns_bus *i2c_bus, int hold_quirk, |
| 315 | int curr_recv_count) |
Moritz Fischer | 9393f58 | 2017-01-16 09:50:46 -0800 | [diff] [blame] | 316 | { |
Pei Yue Ho | ca39472 | 2023-02-13 00:02:41 -0800 | [diff] [blame] | 317 | return hold_quirk && (curr_recv_count == i2c_bus->fifo_depth + 1); |
Moritz Fischer | 9393f58 | 2017-01-16 09:50:46 -0800 | [diff] [blame] | 318 | } |
| 319 | |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 320 | static int cdns_i2c_read_data(struct i2c_cdns_bus *i2c_bus, u32 addr, u8 *data, |
Moritz Fischer | 9393f58 | 2017-01-16 09:50:46 -0800 | [diff] [blame] | 321 | u32 recv_count) |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 322 | { |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 323 | u8 *cur_data = data; |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 324 | struct cdns_i2c_regs *regs = i2c_bus->regs; |
Siva Durga Prasad Paladugu | 142cf18 | 2019-03-14 09:18:37 +0100 | [diff] [blame] | 325 | u32 curr_recv_count; |
Moritz Fischer | 9393f58 | 2017-01-16 09:50:46 -0800 | [diff] [blame] | 326 | int updatetx, hold_quirk; |
Siva Durga Prasad Paladugu | 07c5a1b | 2019-03-07 11:52:49 +0100 | [diff] [blame] | 327 | u32 ret; |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 328 | |
Moritz Fischer | 9393f58 | 2017-01-16 09:50:46 -0800 | [diff] [blame] | 329 | curr_recv_count = recv_count; |
| 330 | |
| 331 | /* Check for the message size against the FIFO depth */ |
Pei Yue Ho | ca39472 | 2023-02-13 00:02:41 -0800 | [diff] [blame] | 332 | if (recv_count > i2c_bus->fifo_depth) |
Moritz Fischer | 9393f58 | 2017-01-16 09:50:46 -0800 | [diff] [blame] | 333 | setbits_le32(®s->control, CDNS_I2C_CONTROL_HOLD); |
| 334 | |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 335 | setbits_le32(®s->control, CDNS_I2C_CONTROL_CLR_FIFO | |
| 336 | CDNS_I2C_CONTROL_RW); |
| 337 | |
Moritz Fischer | 9393f58 | 2017-01-16 09:50:46 -0800 | [diff] [blame] | 338 | if (recv_count > CDNS_I2C_TRANSFER_SIZE) { |
| 339 | curr_recv_count = CDNS_I2C_TRANSFER_SIZE; |
| 340 | writel(curr_recv_count, ®s->transfer_size); |
| 341 | } else { |
| 342 | writel(recv_count, ®s->transfer_size); |
| 343 | } |
| 344 | |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 345 | /* Start reading data */ |
| 346 | writel(addr, ®s->address); |
Moritz Fischer | 9393f58 | 2017-01-16 09:50:46 -0800 | [diff] [blame] | 347 | |
| 348 | updatetx = recv_count > curr_recv_count; |
| 349 | |
| 350 | hold_quirk = (i2c_bus->quirks & CDNS_I2C_BROKEN_HOLD_BIT) && updatetx; |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 351 | |
Siva Durga Prasad Paladugu | 07c5a1b | 2019-03-07 11:52:49 +0100 | [diff] [blame] | 352 | while (recv_count && !is_arbitration_lost(regs)) { |
Moritz Fischer | 9393f58 | 2017-01-16 09:50:46 -0800 | [diff] [blame] | 353 | while (readl(®s->status) & CDNS_I2C_STATUS_RXDV) { |
Pei Yue Ho | ca39472 | 2023-02-13 00:02:41 -0800 | [diff] [blame] | 354 | if (recv_count < i2c_bus->fifo_depth && |
Moritz Fischer | 9393f58 | 2017-01-16 09:50:46 -0800 | [diff] [blame] | 355 | !i2c_bus->hold_flag) { |
| 356 | clrbits_le32(®s->control, |
| 357 | CDNS_I2C_CONTROL_HOLD); |
| 358 | } |
| 359 | *(cur_data)++ = readl(®s->data); |
| 360 | recv_count--; |
| 361 | curr_recv_count--; |
| 362 | |
Pei Yue Ho | ca39472 | 2023-02-13 00:02:41 -0800 | [diff] [blame] | 363 | if (cdns_is_hold_quirk(i2c_bus, hold_quirk, curr_recv_count)) |
Moritz Fischer | 9393f58 | 2017-01-16 09:50:46 -0800 | [diff] [blame] | 364 | break; |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 365 | } |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 366 | |
Pei Yue Ho | ca39472 | 2023-02-13 00:02:41 -0800 | [diff] [blame] | 367 | if (cdns_is_hold_quirk(i2c_bus, hold_quirk, curr_recv_count)) { |
Moritz Fischer | 9393f58 | 2017-01-16 09:50:46 -0800 | [diff] [blame] | 368 | /* wait while fifo is full */ |
| 369 | while (readl(®s->transfer_size) != |
Pei Yue Ho | ca39472 | 2023-02-13 00:02:41 -0800 | [diff] [blame] | 370 | (curr_recv_count - i2c_bus->fifo_depth)) |
Moritz Fischer | 9393f58 | 2017-01-16 09:50:46 -0800 | [diff] [blame] | 371 | ; |
| 372 | /* |
| 373 | * Check number of bytes to be received against maximum |
| 374 | * transfer size and update register accordingly. |
| 375 | */ |
Pei Yue Ho | ca39472 | 2023-02-13 00:02:41 -0800 | [diff] [blame] | 376 | if ((recv_count - i2c_bus->fifo_depth) > |
Moritz Fischer | 9393f58 | 2017-01-16 09:50:46 -0800 | [diff] [blame] | 377 | CDNS_I2C_TRANSFER_SIZE) { |
| 378 | writel(CDNS_I2C_TRANSFER_SIZE, |
| 379 | ®s->transfer_size); |
| 380 | curr_recv_count = CDNS_I2C_TRANSFER_SIZE + |
Pei Yue Ho | ca39472 | 2023-02-13 00:02:41 -0800 | [diff] [blame] | 381 | i2c_bus->fifo_depth; |
Moritz Fischer | 9393f58 | 2017-01-16 09:50:46 -0800 | [diff] [blame] | 382 | } else { |
Pei Yue Ho | ca39472 | 2023-02-13 00:02:41 -0800 | [diff] [blame] | 383 | writel(recv_count - i2c_bus->fifo_depth, |
Moritz Fischer | 9393f58 | 2017-01-16 09:50:46 -0800 | [diff] [blame] | 384 | ®s->transfer_size); |
| 385 | curr_recv_count = recv_count; |
| 386 | } |
| 387 | } else if (recv_count && !hold_quirk && !curr_recv_count) { |
Moritz Fischer | 9393f58 | 2017-01-16 09:50:46 -0800 | [diff] [blame] | 388 | if (recv_count > CDNS_I2C_TRANSFER_SIZE) { |
| 389 | writel(CDNS_I2C_TRANSFER_SIZE, |
| 390 | ®s->transfer_size); |
| 391 | curr_recv_count = CDNS_I2C_TRANSFER_SIZE; |
| 392 | } else { |
| 393 | writel(recv_count, ®s->transfer_size); |
| 394 | curr_recv_count = recv_count; |
| 395 | } |
Sai Pavan Boddu | bdeeb2b | 2022-03-01 09:16:50 +0100 | [diff] [blame] | 396 | writel(addr, ®s->address); |
Moritz Fischer | 9393f58 | 2017-01-16 09:50:46 -0800 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | |
| 400 | /* Wait for the address and data to be sent */ |
Siva Durga Prasad Paladugu | 07c5a1b | 2019-03-07 11:52:49 +0100 | [diff] [blame] | 401 | ret = cdns_i2c_wait(regs, CDNS_I2C_INTERRUPT_COMP | |
| 402 | CDNS_I2C_INTERRUPT_ARBLOST); |
| 403 | if (!(ret & (CDNS_I2C_INTERRUPT_ARBLOST | |
| 404 | CDNS_I2C_INTERRUPT_COMP))) |
Moritz Fischer | 9393f58 | 2017-01-16 09:50:46 -0800 | [diff] [blame] | 405 | return -ETIMEDOUT; |
Siva Durga Prasad Paladugu | 07c5a1b | 2019-03-07 11:52:49 +0100 | [diff] [blame] | 406 | if (ret & CDNS_I2C_INTERRUPT_ARBLOST) |
| 407 | return -EAGAIN; |
Moritz Fischer | 9393f58 | 2017-01-16 09:50:46 -0800 | [diff] [blame] | 408 | |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 409 | return 0; |
| 410 | } |
| 411 | |
| 412 | static int cdns_i2c_xfer(struct udevice *dev, struct i2c_msg *msg, |
| 413 | int nmsgs) |
| 414 | { |
| 415 | struct i2c_cdns_bus *i2c_bus = dev_get_priv(dev); |
Siva Durga Prasad Paladugu | 07c5a1b | 2019-03-07 11:52:49 +0100 | [diff] [blame] | 416 | int ret = 0; |
| 417 | int count; |
Moritz Fischer | 61f0651 | 2017-01-16 09:50:44 -0800 | [diff] [blame] | 418 | bool hold_quirk; |
Siva Durga Prasad Paladugu | 07c5a1b | 2019-03-07 11:52:49 +0100 | [diff] [blame] | 419 | struct i2c_msg *message = msg; |
| 420 | int num_msgs = nmsgs; |
Moritz Fischer | 61f0651 | 2017-01-16 09:50:44 -0800 | [diff] [blame] | 421 | |
| 422 | hold_quirk = !!(i2c_bus->quirks & CDNS_I2C_BROKEN_HOLD_BIT); |
| 423 | |
| 424 | if (nmsgs > 1) { |
| 425 | /* |
| 426 | * This controller does not give completion interrupt after a |
| 427 | * master receive message if HOLD bit is set (repeated start), |
| 428 | * resulting in SW timeout. Hence, if a receive message is |
| 429 | * followed by any other message, an error is returned |
| 430 | * indicating that this sequence is not supported. |
| 431 | */ |
| 432 | for (count = 0; (count < nmsgs - 1) && hold_quirk; count++) { |
| 433 | if (msg[count].flags & I2C_M_RD) { |
| 434 | printf("Can't do repeated start after a receive message\n"); |
| 435 | return -EOPNOTSUPP; |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | i2c_bus->hold_flag = 1; |
| 440 | setbits_le32(&i2c_bus->regs->control, CDNS_I2C_CONTROL_HOLD); |
| 441 | } else { |
| 442 | i2c_bus->hold_flag = 0; |
| 443 | } |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 444 | |
| 445 | debug("i2c_xfer: %d messages\n", nmsgs); |
Siva Durga Prasad Paladugu | 07c5a1b | 2019-03-07 11:52:49 +0100 | [diff] [blame] | 446 | for (u8 retry = 0; retry < CDNS_I2C_ARB_LOST_MAX_RETRIES && |
| 447 | nmsgs > 0; nmsgs--, msg++) { |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 448 | debug("i2c_xfer: chip=0x%x, len=0x%x\n", msg->addr, msg->len); |
| 449 | if (msg->flags & I2C_M_RD) { |
| 450 | ret = cdns_i2c_read_data(i2c_bus, msg->addr, msg->buf, |
| 451 | msg->len); |
| 452 | } else { |
| 453 | ret = cdns_i2c_write_data(i2c_bus, msg->addr, msg->buf, |
Moritz Fischer | 61f0651 | 2017-01-16 09:50:44 -0800 | [diff] [blame] | 454 | msg->len); |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 455 | } |
Siva Durga Prasad Paladugu | 07c5a1b | 2019-03-07 11:52:49 +0100 | [diff] [blame] | 456 | if (ret == -EAGAIN) { |
| 457 | msg = message; |
| 458 | nmsgs = num_msgs; |
| 459 | retry++; |
| 460 | printf("%s,arbitration lost, retrying:%d\n", __func__, |
| 461 | retry); |
| 462 | continue; |
| 463 | } |
| 464 | |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 465 | if (ret) { |
| 466 | debug("i2c_write: error sending\n"); |
| 467 | return -EREMOTEIO; |
| 468 | } |
| 469 | } |
| 470 | |
Siva Durga Prasad Paladugu | 07c5a1b | 2019-03-07 11:52:49 +0100 | [diff] [blame] | 471 | return ret; |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 472 | } |
| 473 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 474 | static int cdns_i2c_of_to_plat(struct udevice *dev) |
Michal Simek | 97ba81f | 2016-04-14 14:15:47 +0200 | [diff] [blame] | 475 | { |
| 476 | struct i2c_cdns_bus *i2c_bus = dev_get_priv(dev); |
Moritz Fischer | 61f0651 | 2017-01-16 09:50:44 -0800 | [diff] [blame] | 477 | struct cdns_i2c_platform_data *pdata = |
| 478 | (struct cdns_i2c_platform_data *)dev_get_driver_data(dev); |
Tomasz Gorochowik | 59e2585 | 2019-01-03 13:36:33 +0100 | [diff] [blame] | 479 | struct clk clk; |
| 480 | int ret; |
Michal Simek | 97ba81f | 2016-04-14 14:15:47 +0200 | [diff] [blame] | 481 | |
Michal Simek | 4747d2c | 2019-01-18 10:43:39 +0100 | [diff] [blame] | 482 | i2c_bus->regs = (struct cdns_i2c_regs *)dev_read_addr(dev); |
Michal Simek | 97ba81f | 2016-04-14 14:15:47 +0200 | [diff] [blame] | 483 | if (!i2c_bus->regs) |
| 484 | return -ENOMEM; |
| 485 | |
Moritz Fischer | 61f0651 | 2017-01-16 09:50:44 -0800 | [diff] [blame] | 486 | if (pdata) |
| 487 | i2c_bus->quirks = pdata->quirks; |
| 488 | |
Tomasz Gorochowik | 59e2585 | 2019-01-03 13:36:33 +0100 | [diff] [blame] | 489 | ret = clk_get_by_index(dev, 0, &clk); |
| 490 | if (ret) |
| 491 | return ret; |
| 492 | |
| 493 | i2c_bus->input_freq = clk_get_rate(&clk); |
Michal Simek | 91429f4 | 2016-04-14 14:15:49 +0200 | [diff] [blame] | 494 | |
T Karthik Reddy | f814987 | 2021-02-03 03:10:46 -0700 | [diff] [blame] | 495 | ret = clk_enable(&clk); |
| 496 | if (ret) { |
| 497 | dev_err(dev, "failed to enable clock\n"); |
| 498 | return ret; |
| 499 | } |
| 500 | |
Pei Yue Ho | ca39472 | 2023-02-13 00:02:41 -0800 | [diff] [blame] | 501 | /* Update FIFO depth based on device tree entry */ |
| 502 | i2c_bus->fifo_depth = dev_read_u32_default(dev, "fifo-depth", |
| 503 | CDNS_I2C_FIFO_DEPTH_DEFAULT); |
| 504 | |
Michal Simek | 97ba81f | 2016-04-14 14:15:47 +0200 | [diff] [blame] | 505 | return 0; |
| 506 | } |
| 507 | |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 508 | static const struct dm_i2c_ops cdns_i2c_ops = { |
| 509 | .xfer = cdns_i2c_xfer, |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 510 | .set_bus_speed = cdns_i2c_set_bus_speed, |
| 511 | }; |
| 512 | |
Moritz Fischer | 61f0651 | 2017-01-16 09:50:44 -0800 | [diff] [blame] | 513 | static const struct cdns_i2c_platform_data r1p10_i2c_def = { |
| 514 | .quirks = CDNS_I2C_BROKEN_HOLD_BIT, |
| 515 | }; |
| 516 | |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 517 | static const struct udevice_id cdns_i2c_of_match[] = { |
Moritz Fischer | 61f0651 | 2017-01-16 09:50:44 -0800 | [diff] [blame] | 518 | { .compatible = "cdns,i2c-r1p10", .data = (ulong)&r1p10_i2c_def }, |
Moritz Fischer | f807776 | 2016-12-22 09:36:10 -0800 | [diff] [blame] | 519 | { .compatible = "cdns,i2c-r1p14" }, |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 520 | { /* end of table */ } |
| 521 | }; |
| 522 | |
| 523 | U_BOOT_DRIVER(cdns_i2c) = { |
Michal Simek | 3309308 | 2020-01-07 08:50:34 +0100 | [diff] [blame] | 524 | .name = "i2c_cdns", |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 525 | .id = UCLASS_I2C, |
| 526 | .of_match = cdns_i2c_of_match, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 527 | .of_to_plat = cdns_i2c_of_to_plat, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 528 | .priv_auto = sizeof(struct i2c_cdns_bus), |
Moritz Fischer | 0075dac | 2015-12-28 09:47:11 -0800 | [diff] [blame] | 529 | .ops = &cdns_i2c_ops, |
| 530 | }; |