Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2017 STMicroelectronics |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
Patrick Delaunay | 6f600e3 | 2020-11-06 19:01:50 +0100 | [diff] [blame] | 6 | #define LOG_CATEGORY UCLASS_I2C |
| 7 | |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 8 | #include <common.h> |
| 9 | #include <clk.h> |
| 10 | #include <dm.h> |
| 11 | #include <i2c.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Patrick Delaunay | 4d15c18 | 2020-07-06 13:31:35 +0200 | [diff] [blame] | 13 | #include <regmap.h> |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 14 | #include <reset.h> |
Patrick Delaunay | 4d15c18 | 2020-07-06 13:31:35 +0200 | [diff] [blame] | 15 | #include <syscon.h> |
Patrick Delaunay | 6f600e3 | 2020-11-06 19:01:50 +0100 | [diff] [blame] | 16 | #include <dm/device.h> |
| 17 | #include <dm/device_compat.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 18 | #include <linux/bitops.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 19 | #include <linux/delay.h> |
Alain Volmat | e04600e | 2020-03-06 11:09:14 +0100 | [diff] [blame] | 20 | #include <linux/err.h> |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 21 | #include <linux/io.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 22 | #include <linux/printk.h> |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 23 | |
| 24 | /* STM32 I2C registers */ |
| 25 | struct stm32_i2c_regs { |
| 26 | u32 cr1; /* I2C control register 1 */ |
| 27 | u32 cr2; /* I2C control register 2 */ |
| 28 | u32 oar1; /* I2C own address 1 register */ |
| 29 | u32 oar2; /* I2C own address 2 register */ |
| 30 | u32 timingr; /* I2C timing register */ |
| 31 | u32 timeoutr; /* I2C timeout register */ |
| 32 | u32 isr; /* I2C interrupt and status register */ |
| 33 | u32 icr; /* I2C interrupt clear register */ |
| 34 | u32 pecr; /* I2C packet error checking register */ |
| 35 | u32 rxdr; /* I2C receive data register */ |
| 36 | u32 txdr; /* I2C transmit data register */ |
| 37 | }; |
| 38 | |
| 39 | #define STM32_I2C_CR1 0x00 |
| 40 | #define STM32_I2C_CR2 0x04 |
| 41 | #define STM32_I2C_TIMINGR 0x10 |
| 42 | #define STM32_I2C_ISR 0x18 |
| 43 | #define STM32_I2C_ICR 0x1C |
| 44 | #define STM32_I2C_RXDR 0x24 |
| 45 | #define STM32_I2C_TXDR 0x28 |
| 46 | |
| 47 | /* STM32 I2C control 1 */ |
| 48 | #define STM32_I2C_CR1_ANFOFF BIT(12) |
Patrick Delaunay | 05b8d60 | 2021-08-03 12:05:13 +0200 | [diff] [blame] | 49 | #define STM32_I2C_CR1_DNF_MASK GENMASK(11, 8) |
| 50 | #define STM32_I2C_CR1_DNF(n) (((n) & 0xf) << 8) |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 51 | #define STM32_I2C_CR1_ERRIE BIT(7) |
| 52 | #define STM32_I2C_CR1_TCIE BIT(6) |
| 53 | #define STM32_I2C_CR1_STOPIE BIT(5) |
| 54 | #define STM32_I2C_CR1_NACKIE BIT(4) |
| 55 | #define STM32_I2C_CR1_ADDRIE BIT(3) |
| 56 | #define STM32_I2C_CR1_RXIE BIT(2) |
| 57 | #define STM32_I2C_CR1_TXIE BIT(1) |
| 58 | #define STM32_I2C_CR1_PE BIT(0) |
| 59 | |
| 60 | /* STM32 I2C control 2 */ |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 61 | #define STM32_I2C_CR2_RELOAD BIT(24) |
| 62 | #define STM32_I2C_CR2_NBYTES_MASK GENMASK(23, 16) |
| 63 | #define STM32_I2C_CR2_NBYTES(n) ((n & 0xff) << 16) |
| 64 | #define STM32_I2C_CR2_NACK BIT(15) |
| 65 | #define STM32_I2C_CR2_STOP BIT(14) |
| 66 | #define STM32_I2C_CR2_START BIT(13) |
| 67 | #define STM32_I2C_CR2_HEAD10R BIT(12) |
| 68 | #define STM32_I2C_CR2_ADD10 BIT(11) |
| 69 | #define STM32_I2C_CR2_RD_WRN BIT(10) |
| 70 | #define STM32_I2C_CR2_SADD10_MASK GENMASK(9, 0) |
Patrick Delaunay | b13ab6f | 2018-10-29 15:31:55 +0100 | [diff] [blame] | 71 | #define STM32_I2C_CR2_SADD10(n) (n & STM32_I2C_CR2_SADD10_MASK) |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 72 | #define STM32_I2C_CR2_SADD7_MASK GENMASK(7, 1) |
| 73 | #define STM32_I2C_CR2_SADD7(n) ((n & 0x7f) << 1) |
| 74 | #define STM32_I2C_CR2_RESET_MASK (STM32_I2C_CR2_HEAD10R \ |
| 75 | | STM32_I2C_CR2_NBYTES_MASK \ |
| 76 | | STM32_I2C_CR2_SADD7_MASK \ |
| 77 | | STM32_I2C_CR2_RELOAD \ |
| 78 | | STM32_I2C_CR2_RD_WRN) |
| 79 | |
| 80 | /* STM32 I2C Interrupt Status */ |
| 81 | #define STM32_I2C_ISR_BUSY BIT(15) |
| 82 | #define STM32_I2C_ISR_ARLO BIT(9) |
| 83 | #define STM32_I2C_ISR_BERR BIT(8) |
| 84 | #define STM32_I2C_ISR_TCR BIT(7) |
| 85 | #define STM32_I2C_ISR_TC BIT(6) |
| 86 | #define STM32_I2C_ISR_STOPF BIT(5) |
| 87 | #define STM32_I2C_ISR_NACKF BIT(4) |
| 88 | #define STM32_I2C_ISR_ADDR BIT(3) |
| 89 | #define STM32_I2C_ISR_RXNE BIT(2) |
| 90 | #define STM32_I2C_ISR_TXIS BIT(1) |
| 91 | #define STM32_I2C_ISR_TXE BIT(0) |
| 92 | #define STM32_I2C_ISR_ERRORS (STM32_I2C_ISR_BERR \ |
| 93 | | STM32_I2C_ISR_ARLO) |
| 94 | |
| 95 | /* STM32 I2C Interrupt Clear */ |
| 96 | #define STM32_I2C_ICR_ARLOCF BIT(9) |
| 97 | #define STM32_I2C_ICR_BERRCF BIT(8) |
| 98 | #define STM32_I2C_ICR_STOPCF BIT(5) |
| 99 | #define STM32_I2C_ICR_NACKCF BIT(4) |
| 100 | |
| 101 | /* STM32 I2C Timing */ |
| 102 | #define STM32_I2C_TIMINGR_PRESC(n) ((n & 0xf) << 28) |
| 103 | #define STM32_I2C_TIMINGR_SCLDEL(n) ((n & 0xf) << 20) |
| 104 | #define STM32_I2C_TIMINGR_SDADEL(n) ((n & 0xf) << 16) |
| 105 | #define STM32_I2C_TIMINGR_SCLH(n) ((n & 0xff) << 8) |
| 106 | #define STM32_I2C_TIMINGR_SCLL(n) (n & 0xff) |
| 107 | |
| 108 | #define STM32_I2C_MAX_LEN 0xff |
| 109 | |
Patrick Delaunay | 05b8d60 | 2021-08-03 12:05:13 +0200 | [diff] [blame] | 110 | #define STM32_I2C_DNF_MAX 15 |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 111 | |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 112 | #define STM32_I2C_ANALOG_FILTER_DELAY_MIN 50 /* ns */ |
| 113 | #define STM32_I2C_ANALOG_FILTER_DELAY_MAX 260 /* ns */ |
| 114 | |
| 115 | #define STM32_I2C_RISE_TIME_DEFAULT 25 /* ns */ |
| 116 | #define STM32_I2C_FALL_TIME_DEFAULT 10 /* ns */ |
| 117 | |
| 118 | #define STM32_PRESC_MAX BIT(4) |
| 119 | #define STM32_SCLDEL_MAX BIT(4) |
| 120 | #define STM32_SDADEL_MAX BIT(4) |
| 121 | #define STM32_SCLH_MAX BIT(8) |
| 122 | #define STM32_SCLL_MAX BIT(8) |
| 123 | |
| 124 | #define STM32_NSEC_PER_SEC 1000000000L |
| 125 | |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 126 | /** |
| 127 | * struct stm32_i2c_spec - private i2c specification timing |
| 128 | * @rate: I2C bus speed (Hz) |
| 129 | * @rate_min: 80% of I2C bus speed (Hz) |
| 130 | * @rate_max: 120% of I2C bus speed (Hz) |
| 131 | * @fall_max: Max fall time of both SDA and SCL signals (ns) |
| 132 | * @rise_max: Max rise time of both SDA and SCL signals (ns) |
| 133 | * @hddat_min: Min data hold time (ns) |
| 134 | * @vddat_max: Max data valid time (ns) |
| 135 | * @sudat_min: Min data setup time (ns) |
| 136 | * @l_min: Min low period of the SCL clock (ns) |
| 137 | * @h_min: Min high period of the SCL clock (ns) |
| 138 | */ |
| 139 | |
| 140 | struct stm32_i2c_spec { |
| 141 | u32 rate; |
| 142 | u32 rate_min; |
| 143 | u32 rate_max; |
| 144 | u32 fall_max; |
| 145 | u32 rise_max; |
| 146 | u32 hddat_min; |
| 147 | u32 vddat_max; |
| 148 | u32 sudat_min; |
| 149 | u32 l_min; |
| 150 | u32 h_min; |
| 151 | }; |
| 152 | |
| 153 | /** |
| 154 | * struct stm32_i2c_setup - private I2C timing setup parameters |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 155 | * @speed_freq: I2C speed frequency (Hz) |
| 156 | * @clock_src: I2C clock source frequency (Hz) |
| 157 | * @rise_time: Rise time (ns) |
| 158 | * @fall_time: Fall time (ns) |
Patrick Delaunay | 05b8d60 | 2021-08-03 12:05:13 +0200 | [diff] [blame] | 159 | * @dnf: value of digital filter to apply |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 160 | * @analog_filter: Analog filter delay (On/Off) |
| 161 | */ |
| 162 | struct stm32_i2c_setup { |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 163 | u32 speed_freq; |
| 164 | u32 clock_src; |
| 165 | u32 rise_time; |
| 166 | u32 fall_time; |
| 167 | u8 dnf; |
| 168 | bool analog_filter; |
Patrick Delaunay | 5886278 | 2021-08-03 12:05:09 +0200 | [diff] [blame] | 169 | }; |
| 170 | |
| 171 | /** |
| 172 | * struct stm32_i2c_data - driver data for I2C configuration by compatible |
| 173 | * @fmp_clr_offset: Fast Mode Plus clear register offset from set register |
| 174 | */ |
| 175 | struct stm32_i2c_data { |
Patrick Delaunay | 4d15c18 | 2020-07-06 13:31:35 +0200 | [diff] [blame] | 176 | u32 fmp_clr_offset; |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 177 | }; |
| 178 | |
| 179 | /** |
| 180 | * struct stm32_i2c_timings - private I2C output parameters |
| 181 | * @prec: Prescaler value |
| 182 | * @scldel: Data setup time |
| 183 | * @sdadel: Data hold time |
| 184 | * @sclh: SCL high period (master mode) |
| 185 | * @sclh: SCL low period (master mode) |
| 186 | */ |
| 187 | struct stm32_i2c_timings { |
| 188 | struct list_head node; |
| 189 | u8 presc; |
| 190 | u8 scldel; |
| 191 | u8 sdadel; |
| 192 | u8 sclh; |
| 193 | u8 scll; |
| 194 | }; |
| 195 | |
Patrick Delaunay | 4d15c18 | 2020-07-06 13:31:35 +0200 | [diff] [blame] | 196 | /** |
| 197 | * struct stm32_i2c_priv - private data of the controller |
| 198 | * @regs: I2C registers address |
| 199 | * @clk: hw i2c clock |
| 200 | * @setup: I2C timing setup parameters |
| 201 | * @speed: I2C clock frequency of the controller. Standard, Fast or Fast+ |
| 202 | * @regmap: holds SYSCFG phandle for Fast Mode Plus bit |
| 203 | * @regmap_sreg: register address for setting Fast Mode Plus bits |
| 204 | * @regmap_creg: register address for clearing Fast Mode Plus bits |
| 205 | * @regmap_mask: mask for Fast Mode Plus bits |
Patrick Delaunay | a14855d | 2021-08-03 12:05:14 +0200 | [diff] [blame] | 206 | * @dnf_dt: value of digital filter requested via dt |
Patrick Delaunay | 4d15c18 | 2020-07-06 13:31:35 +0200 | [diff] [blame] | 207 | */ |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 208 | struct stm32_i2c_priv { |
| 209 | struct stm32_i2c_regs *regs; |
| 210 | struct clk clk; |
Patrick Delaunay | 5886278 | 2021-08-03 12:05:09 +0200 | [diff] [blame] | 211 | struct stm32_i2c_setup setup; |
Alain Volmat | e04600e | 2020-03-06 11:09:14 +0100 | [diff] [blame] | 212 | u32 speed; |
Patrick Delaunay | 4d15c18 | 2020-07-06 13:31:35 +0200 | [diff] [blame] | 213 | struct regmap *regmap; |
| 214 | u32 regmap_sreg; |
| 215 | u32 regmap_creg; |
| 216 | u32 regmap_mask; |
Patrick Delaunay | a14855d | 2021-08-03 12:05:14 +0200 | [diff] [blame] | 217 | u32 dnf_dt; |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 218 | }; |
| 219 | |
Patrick Delaunay | bf832e5 | 2018-10-29 15:31:56 +0100 | [diff] [blame] | 220 | static const struct stm32_i2c_spec i2c_specs[] = { |
Alain Volmat | e04600e | 2020-03-06 11:09:14 +0100 | [diff] [blame] | 221 | /* Standard speed - 100 KHz */ |
Simon Glass | e8e01cc | 2020-01-23 11:48:21 -0700 | [diff] [blame] | 222 | [IC_SPEED_MODE_STANDARD] = { |
| 223 | .rate = I2C_SPEED_STANDARD_RATE, |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 224 | .rate_min = 8000, |
| 225 | .rate_max = 120000, |
| 226 | .fall_max = 300, |
| 227 | .rise_max = 1000, |
| 228 | .hddat_min = 0, |
| 229 | .vddat_max = 3450, |
| 230 | .sudat_min = 250, |
| 231 | .l_min = 4700, |
| 232 | .h_min = 4000, |
| 233 | }, |
Alain Volmat | e04600e | 2020-03-06 11:09:14 +0100 | [diff] [blame] | 234 | /* Fast speed - 400 KHz */ |
Simon Glass | e8e01cc | 2020-01-23 11:48:21 -0700 | [diff] [blame] | 235 | [IC_SPEED_MODE_FAST] = { |
| 236 | .rate = I2C_SPEED_FAST_RATE, |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 237 | .rate_min = 320000, |
| 238 | .rate_max = 480000, |
| 239 | .fall_max = 300, |
| 240 | .rise_max = 300, |
| 241 | .hddat_min = 0, |
| 242 | .vddat_max = 900, |
| 243 | .sudat_min = 100, |
| 244 | .l_min = 1300, |
| 245 | .h_min = 600, |
| 246 | }, |
Alain Volmat | e04600e | 2020-03-06 11:09:14 +0100 | [diff] [blame] | 247 | /* Fast Plus Speed - 1 MHz */ |
Simon Glass | e8e01cc | 2020-01-23 11:48:21 -0700 | [diff] [blame] | 248 | [IC_SPEED_MODE_FAST_PLUS] = { |
| 249 | .rate = I2C_SPEED_FAST_PLUS_RATE, |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 250 | .rate_min = 800000, |
| 251 | .rate_max = 1200000, |
| 252 | .fall_max = 100, |
| 253 | .rise_max = 120, |
| 254 | .hddat_min = 0, |
| 255 | .vddat_max = 450, |
| 256 | .sudat_min = 50, |
| 257 | .l_min = 500, |
| 258 | .h_min = 260, |
| 259 | }, |
| 260 | }; |
| 261 | |
Patrick Delaunay | 5886278 | 2021-08-03 12:05:09 +0200 | [diff] [blame] | 262 | static const struct stm32_i2c_data stm32f7_data = { |
| 263 | .fmp_clr_offset = 0x00, |
Patrick Delaunay | 4d15c18 | 2020-07-06 13:31:35 +0200 | [diff] [blame] | 264 | }; |
| 265 | |
Patrick Delaunay | 5886278 | 2021-08-03 12:05:09 +0200 | [diff] [blame] | 266 | static const struct stm32_i2c_data stm32mp15_data = { |
Patrick Delaunay | 4d15c18 | 2020-07-06 13:31:35 +0200 | [diff] [blame] | 267 | .fmp_clr_offset = 0x40, |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 268 | }; |
| 269 | |
Patrick Delaunay | 11c064e | 2022-06-30 10:20:14 +0200 | [diff] [blame] | 270 | static const struct stm32_i2c_data stm32mp13_data = { |
| 271 | .fmp_clr_offset = 0x4, |
| 272 | }; |
| 273 | |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 274 | static int stm32_i2c_check_device_busy(struct stm32_i2c_priv *i2c_priv) |
| 275 | { |
| 276 | struct stm32_i2c_regs *regs = i2c_priv->regs; |
| 277 | u32 status = readl(®s->isr); |
| 278 | |
| 279 | if (status & STM32_I2C_ISR_BUSY) |
| 280 | return -EBUSY; |
| 281 | |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | static void stm32_i2c_message_start(struct stm32_i2c_priv *i2c_priv, |
Alain Volmat | 36fa3bb | 2022-09-12 10:41:59 +0200 | [diff] [blame] | 286 | struct i2c_msg *msg) |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 287 | { |
| 288 | struct stm32_i2c_regs *regs = i2c_priv->regs; |
| 289 | u32 cr2 = readl(®s->cr2); |
| 290 | |
| 291 | /* Set transfer direction */ |
| 292 | cr2 &= ~STM32_I2C_CR2_RD_WRN; |
| 293 | if (msg->flags & I2C_M_RD) |
| 294 | cr2 |= STM32_I2C_CR2_RD_WRN; |
| 295 | |
| 296 | /* Set slave address */ |
| 297 | cr2 &= ~(STM32_I2C_CR2_HEAD10R | STM32_I2C_CR2_ADD10); |
| 298 | if (msg->flags & I2C_M_TEN) { |
| 299 | cr2 &= ~STM32_I2C_CR2_SADD10_MASK; |
| 300 | cr2 |= STM32_I2C_CR2_SADD10(msg->addr); |
| 301 | cr2 |= STM32_I2C_CR2_ADD10; |
| 302 | } else { |
| 303 | cr2 &= ~STM32_I2C_CR2_SADD7_MASK; |
| 304 | cr2 |= STM32_I2C_CR2_SADD7(msg->addr); |
| 305 | } |
| 306 | |
Alain Volmat | 110efab | 2022-09-12 10:41:58 +0200 | [diff] [blame] | 307 | /* Set nb bytes to transfer and reload (if needed) */ |
| 308 | cr2 &= ~(STM32_I2C_CR2_NBYTES_MASK | STM32_I2C_CR2_RELOAD); |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 309 | if (msg->len > STM32_I2C_MAX_LEN) { |
| 310 | cr2 |= STM32_I2C_CR2_NBYTES(STM32_I2C_MAX_LEN); |
| 311 | cr2 |= STM32_I2C_CR2_RELOAD; |
| 312 | } else { |
| 313 | cr2 |= STM32_I2C_CR2_NBYTES(msg->len); |
| 314 | } |
| 315 | |
| 316 | /* Write configurations register */ |
| 317 | writel(cr2, ®s->cr2); |
| 318 | |
| 319 | /* START/ReSTART generation */ |
| 320 | setbits_le32(®s->cr2, STM32_I2C_CR2_START); |
| 321 | } |
| 322 | |
| 323 | /* |
| 324 | * RELOAD mode must be selected if total number of data bytes to be |
| 325 | * sent is greater than MAX_LEN |
| 326 | */ |
| 327 | |
| 328 | static void stm32_i2c_handle_reload(struct stm32_i2c_priv *i2c_priv, |
Alain Volmat | 36fa3bb | 2022-09-12 10:41:59 +0200 | [diff] [blame] | 329 | struct i2c_msg *msg) |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 330 | { |
| 331 | struct stm32_i2c_regs *regs = i2c_priv->regs; |
| 332 | u32 cr2 = readl(®s->cr2); |
| 333 | |
| 334 | cr2 &= ~STM32_I2C_CR2_NBYTES_MASK; |
| 335 | |
| 336 | if (msg->len > STM32_I2C_MAX_LEN) { |
| 337 | cr2 |= STM32_I2C_CR2_NBYTES(STM32_I2C_MAX_LEN); |
| 338 | } else { |
| 339 | cr2 &= ~STM32_I2C_CR2_RELOAD; |
| 340 | cr2 |= STM32_I2C_CR2_NBYTES(msg->len); |
| 341 | } |
| 342 | |
| 343 | writel(cr2, ®s->cr2); |
| 344 | } |
| 345 | |
| 346 | static int stm32_i2c_wait_flags(struct stm32_i2c_priv *i2c_priv, |
Patrick Delaunay | b13ab6f | 2018-10-29 15:31:55 +0100 | [diff] [blame] | 347 | u32 flags, u32 *status) |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 348 | { |
| 349 | struct stm32_i2c_regs *regs = i2c_priv->regs; |
| 350 | u32 time_start = get_timer(0); |
| 351 | |
| 352 | *status = readl(®s->isr); |
| 353 | while (!(*status & flags)) { |
| 354 | if (get_timer(time_start) > CONFIG_SYS_HZ) { |
Patrick Delaunay | 6f600e3 | 2020-11-06 19:01:50 +0100 | [diff] [blame] | 355 | log_debug("i2c timeout\n"); |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 356 | return -ETIMEDOUT; |
| 357 | } |
| 358 | |
| 359 | *status = readl(®s->isr); |
| 360 | } |
| 361 | |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | static int stm32_i2c_check_end_of_message(struct stm32_i2c_priv *i2c_priv) |
| 366 | { |
| 367 | struct stm32_i2c_regs *regs = i2c_priv->regs; |
| 368 | u32 mask = STM32_I2C_ISR_ERRORS | STM32_I2C_ISR_NACKF | |
| 369 | STM32_I2C_ISR_STOPF; |
| 370 | u32 status; |
| 371 | int ret; |
| 372 | |
| 373 | ret = stm32_i2c_wait_flags(i2c_priv, mask, &status); |
| 374 | if (ret) |
| 375 | return ret; |
| 376 | |
| 377 | if (status & STM32_I2C_ISR_BERR) { |
Patrick Delaunay | 6f600e3 | 2020-11-06 19:01:50 +0100 | [diff] [blame] | 378 | log_debug("Bus error\n"); |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 379 | |
| 380 | /* Clear BERR flag */ |
| 381 | setbits_le32(®s->icr, STM32_I2C_ICR_BERRCF); |
| 382 | |
| 383 | return -EIO; |
| 384 | } |
| 385 | |
| 386 | if (status & STM32_I2C_ISR_ARLO) { |
Patrick Delaunay | 6f600e3 | 2020-11-06 19:01:50 +0100 | [diff] [blame] | 387 | log_debug("Arbitration lost\n"); |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 388 | |
| 389 | /* Clear ARLO flag */ |
| 390 | setbits_le32(®s->icr, STM32_I2C_ICR_ARLOCF); |
| 391 | |
| 392 | return -EAGAIN; |
| 393 | } |
| 394 | |
| 395 | if (status & STM32_I2C_ISR_NACKF) { |
Patrick Delaunay | 6f600e3 | 2020-11-06 19:01:50 +0100 | [diff] [blame] | 396 | log_debug("Receive NACK\n"); |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 397 | |
| 398 | /* Clear NACK flag */ |
| 399 | setbits_le32(®s->icr, STM32_I2C_ICR_NACKCF); |
| 400 | |
| 401 | /* Wait until STOPF flag is set */ |
| 402 | mask = STM32_I2C_ISR_STOPF; |
| 403 | ret = stm32_i2c_wait_flags(i2c_priv, mask, &status); |
| 404 | if (ret) |
| 405 | return ret; |
| 406 | |
| 407 | ret = -EIO; |
| 408 | } |
| 409 | |
| 410 | if (status & STM32_I2C_ISR_STOPF) { |
| 411 | /* Clear STOP flag */ |
| 412 | setbits_le32(®s->icr, STM32_I2C_ICR_STOPCF); |
| 413 | |
| 414 | /* Clear control register 2 */ |
Jorge Ramirez-Ortiz | ddba62c | 2022-08-15 16:52:10 +0200 | [diff] [blame] | 415 | clrbits_le32(®s->cr2, STM32_I2C_CR2_RESET_MASK); |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | return ret; |
| 419 | } |
| 420 | |
| 421 | static int stm32_i2c_message_xfer(struct stm32_i2c_priv *i2c_priv, |
Patrick Delaunay | b13ab6f | 2018-10-29 15:31:55 +0100 | [diff] [blame] | 422 | struct i2c_msg *msg, bool stop) |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 423 | { |
| 424 | struct stm32_i2c_regs *regs = i2c_priv->regs; |
| 425 | u32 status; |
| 426 | u32 mask = msg->flags & I2C_M_RD ? STM32_I2C_ISR_RXNE : |
| 427 | STM32_I2C_ISR_TXIS | STM32_I2C_ISR_NACKF; |
| 428 | int bytes_to_rw = msg->len > STM32_I2C_MAX_LEN ? |
| 429 | STM32_I2C_MAX_LEN : msg->len; |
| 430 | int ret = 0; |
| 431 | |
| 432 | /* Add errors */ |
| 433 | mask |= STM32_I2C_ISR_ERRORS; |
| 434 | |
Alain Volmat | 36fa3bb | 2022-09-12 10:41:59 +0200 | [diff] [blame] | 435 | stm32_i2c_message_start(i2c_priv, msg); |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 436 | |
| 437 | while (msg->len) { |
| 438 | /* |
| 439 | * Wait until TXIS/NACKF/BERR/ARLO flags or |
| 440 | * RXNE/BERR/ARLO flags are set |
| 441 | */ |
| 442 | ret = stm32_i2c_wait_flags(i2c_priv, mask, &status); |
| 443 | if (ret) |
| 444 | break; |
| 445 | |
| 446 | if (status & (STM32_I2C_ISR_NACKF | STM32_I2C_ISR_ERRORS)) |
| 447 | break; |
| 448 | |
| 449 | if (status & STM32_I2C_ISR_RXNE) { |
| 450 | *msg->buf++ = readb(®s->rxdr); |
| 451 | msg->len--; |
| 452 | bytes_to_rw--; |
| 453 | } |
| 454 | |
| 455 | if (status & STM32_I2C_ISR_TXIS) { |
| 456 | writeb(*msg->buf++, ®s->txdr); |
| 457 | msg->len--; |
| 458 | bytes_to_rw--; |
| 459 | } |
| 460 | |
| 461 | if (!bytes_to_rw && msg->len) { |
| 462 | /* Wait until TCR flag is set */ |
| 463 | mask = STM32_I2C_ISR_TCR; |
| 464 | ret = stm32_i2c_wait_flags(i2c_priv, mask, &status); |
| 465 | if (ret) |
| 466 | break; |
| 467 | |
| 468 | bytes_to_rw = msg->len > STM32_I2C_MAX_LEN ? |
| 469 | STM32_I2C_MAX_LEN : msg->len; |
| 470 | mask = msg->flags & I2C_M_RD ? STM32_I2C_ISR_RXNE : |
| 471 | STM32_I2C_ISR_TXIS | STM32_I2C_ISR_NACKF; |
| 472 | |
Alain Volmat | 36fa3bb | 2022-09-12 10:41:59 +0200 | [diff] [blame] | 473 | stm32_i2c_handle_reload(i2c_priv, msg); |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 474 | } else if (!bytes_to_rw) { |
| 475 | /* Wait until TC flag is set */ |
| 476 | mask = STM32_I2C_ISR_TC; |
| 477 | ret = stm32_i2c_wait_flags(i2c_priv, mask, &status); |
| 478 | if (ret) |
| 479 | break; |
| 480 | |
| 481 | if (!stop) |
| 482 | /* Message sent, new message has to be sent */ |
| 483 | return 0; |
| 484 | } |
| 485 | } |
| 486 | |
Alain Volmat | ef8d2a2 | 2022-09-12 10:42:00 +0200 | [diff] [blame] | 487 | /* End of transfer, send stop condition if appropriate */ |
| 488 | if (!ret && !(status & (STM32_I2C_ISR_NACKF | STM32_I2C_ISR_ERRORS))) |
| 489 | setbits_le32(®s->cr2, STM32_I2C_CR2_STOP); |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 490 | |
| 491 | return stm32_i2c_check_end_of_message(i2c_priv); |
| 492 | } |
| 493 | |
| 494 | static int stm32_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, |
Patrick Delaunay | b13ab6f | 2018-10-29 15:31:55 +0100 | [diff] [blame] | 495 | int nmsgs) |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 496 | { |
| 497 | struct stm32_i2c_priv *i2c_priv = dev_get_priv(bus); |
| 498 | int ret; |
| 499 | |
| 500 | ret = stm32_i2c_check_device_busy(i2c_priv); |
| 501 | if (ret) |
| 502 | return ret; |
| 503 | |
| 504 | for (; nmsgs > 0; nmsgs--, msg++) { |
| 505 | ret = stm32_i2c_message_xfer(i2c_priv, msg, nmsgs == 1); |
| 506 | if (ret) |
| 507 | return ret; |
| 508 | } |
| 509 | |
| 510 | return 0; |
| 511 | } |
| 512 | |
Patrick Delaunay | 10605f7 | 2021-08-03 12:05:15 +0200 | [diff] [blame] | 513 | static int stm32_i2c_compute_solutions(u32 i2cclk, |
| 514 | struct stm32_i2c_setup *setup, |
Alain Volmat | e04600e | 2020-03-06 11:09:14 +0100 | [diff] [blame] | 515 | const struct stm32_i2c_spec *specs, |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 516 | struct list_head *solutions) |
| 517 | { |
| 518 | struct stm32_i2c_timings *v; |
| 519 | u32 p_prev = STM32_PRESC_MAX; |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 520 | u32 af_delay_min, af_delay_max; |
| 521 | u16 p, l, a; |
| 522 | int sdadel_min, sdadel_max, scldel_min; |
| 523 | int ret = 0; |
| 524 | |
| 525 | af_delay_min = setup->analog_filter ? |
| 526 | STM32_I2C_ANALOG_FILTER_DELAY_MIN : 0; |
| 527 | af_delay_max = setup->analog_filter ? |
| 528 | STM32_I2C_ANALOG_FILTER_DELAY_MAX : 0; |
| 529 | |
Alain Volmat | e04600e | 2020-03-06 11:09:14 +0100 | [diff] [blame] | 530 | sdadel_min = specs->hddat_min + setup->fall_time - |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 531 | af_delay_min - (setup->dnf + 3) * i2cclk; |
| 532 | |
Alain Volmat | e04600e | 2020-03-06 11:09:14 +0100 | [diff] [blame] | 533 | sdadel_max = specs->vddat_max - setup->rise_time - |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 534 | af_delay_max - (setup->dnf + 4) * i2cclk; |
| 535 | |
Alain Volmat | e04600e | 2020-03-06 11:09:14 +0100 | [diff] [blame] | 536 | scldel_min = setup->rise_time + specs->sudat_min; |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 537 | |
| 538 | if (sdadel_min < 0) |
| 539 | sdadel_min = 0; |
| 540 | if (sdadel_max < 0) |
| 541 | sdadel_max = 0; |
| 542 | |
Patrick Delaunay | 6f600e3 | 2020-11-06 19:01:50 +0100 | [diff] [blame] | 543 | log_debug("SDADEL(min/max): %i/%i, SCLDEL(Min): %i\n", |
| 544 | sdadel_min, sdadel_max, scldel_min); |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 545 | |
| 546 | /* Compute possible values for PRESC, SCLDEL and SDADEL */ |
| 547 | for (p = 0; p < STM32_PRESC_MAX; p++) { |
| 548 | for (l = 0; l < STM32_SCLDEL_MAX; l++) { |
Patrick Delaunay | 6f4f912 | 2019-06-21 15:26:47 +0200 | [diff] [blame] | 549 | int scldel = (l + 1) * (p + 1) * i2cclk; |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 550 | |
| 551 | if (scldel < scldel_min) |
| 552 | continue; |
| 553 | |
| 554 | for (a = 0; a < STM32_SDADEL_MAX; a++) { |
Patrick Delaunay | 6f4f912 | 2019-06-21 15:26:47 +0200 | [diff] [blame] | 555 | int sdadel = (a * (p + 1) + 1) * i2cclk; |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 556 | |
| 557 | if (((sdadel >= sdadel_min) && |
| 558 | (sdadel <= sdadel_max)) && |
| 559 | (p != p_prev)) { |
Patrick Delaunay | 28b10a7 | 2018-03-12 10:46:09 +0100 | [diff] [blame] | 560 | v = calloc(1, sizeof(*v)); |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 561 | if (!v) |
| 562 | return -ENOMEM; |
| 563 | |
| 564 | v->presc = p; |
| 565 | v->scldel = l; |
| 566 | v->sdadel = a; |
| 567 | p_prev = p; |
| 568 | |
| 569 | list_add_tail(&v->node, solutions); |
Nicolas Le Bayon | c41d307 | 2019-04-18 17:32:43 +0200 | [diff] [blame] | 570 | break; |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 571 | } |
| 572 | } |
Nicolas Le Bayon | c41d307 | 2019-04-18 17:32:43 +0200 | [diff] [blame] | 573 | |
| 574 | if (p_prev == p) |
| 575 | break; |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | |
| 579 | if (list_empty(solutions)) { |
Patrick Delaunay | 6f600e3 | 2020-11-06 19:01:50 +0100 | [diff] [blame] | 580 | log_err("no Prescaler solution\n"); |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 581 | ret = -EPERM; |
| 582 | } |
| 583 | |
| 584 | return ret; |
| 585 | } |
| 586 | |
Patrick Delaunay | 10605f7 | 2021-08-03 12:05:15 +0200 | [diff] [blame] | 587 | static int stm32_i2c_choose_solution(u32 i2cclk, |
| 588 | struct stm32_i2c_setup *setup, |
Alain Volmat | e04600e | 2020-03-06 11:09:14 +0100 | [diff] [blame] | 589 | const struct stm32_i2c_spec *specs, |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 590 | struct list_head *solutions, |
| 591 | struct stm32_i2c_timings *s) |
| 592 | { |
| 593 | struct stm32_i2c_timings *v; |
| 594 | u32 i2cbus = DIV_ROUND_CLOSEST(STM32_NSEC_PER_SEC, |
| 595 | setup->speed_freq); |
| 596 | u32 clk_error_prev = i2cbus; |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 597 | u32 clk_min, clk_max; |
| 598 | u32 af_delay_min; |
| 599 | u32 dnf_delay; |
| 600 | u32 tsync; |
| 601 | u16 l, h; |
Christophe Kerello | a71376a | 2017-10-17 11:21:32 +0200 | [diff] [blame] | 602 | bool sol_found = false; |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 603 | int ret = 0; |
| 604 | |
| 605 | af_delay_min = setup->analog_filter ? |
| 606 | STM32_I2C_ANALOG_FILTER_DELAY_MIN : 0; |
| 607 | dnf_delay = setup->dnf * i2cclk; |
| 608 | |
| 609 | tsync = af_delay_min + dnf_delay + (2 * i2cclk); |
Alain Volmat | e04600e | 2020-03-06 11:09:14 +0100 | [diff] [blame] | 610 | clk_max = STM32_NSEC_PER_SEC / specs->rate_min; |
| 611 | clk_min = STM32_NSEC_PER_SEC / specs->rate_max; |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 612 | |
| 613 | /* |
| 614 | * Among Prescaler possibilities discovered above figures out SCL Low |
| 615 | * and High Period. Provided: |
| 616 | * - SCL Low Period has to be higher than Low Period of the SCL Clock |
| 617 | * defined by I2C Specification. I2C Clock has to be lower than |
| 618 | * (SCL Low Period - Analog/Digital filters) / 4. |
| 619 | * - SCL High Period has to be lower than High Period of the SCL Clock |
| 620 | * defined by I2C Specification |
| 621 | * - I2C Clock has to be lower than SCL High Period |
| 622 | */ |
| 623 | list_for_each_entry(v, solutions, node) { |
| 624 | u32 prescaler = (v->presc + 1) * i2cclk; |
| 625 | |
| 626 | for (l = 0; l < STM32_SCLL_MAX; l++) { |
| 627 | u32 tscl_l = (l + 1) * prescaler + tsync; |
Patrick Delaunay | b13ab6f | 2018-10-29 15:31:55 +0100 | [diff] [blame] | 628 | |
Alain Volmat | e04600e | 2020-03-06 11:09:14 +0100 | [diff] [blame] | 629 | if (tscl_l < specs->l_min || |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 630 | (i2cclk >= |
| 631 | ((tscl_l - af_delay_min - dnf_delay) / 4))) { |
| 632 | continue; |
| 633 | } |
| 634 | |
| 635 | for (h = 0; h < STM32_SCLH_MAX; h++) { |
| 636 | u32 tscl_h = (h + 1) * prescaler + tsync; |
| 637 | u32 tscl = tscl_l + tscl_h + |
| 638 | setup->rise_time + setup->fall_time; |
| 639 | |
| 640 | if ((tscl >= clk_min) && (tscl <= clk_max) && |
Alain Volmat | e04600e | 2020-03-06 11:09:14 +0100 | [diff] [blame] | 641 | (tscl_h >= specs->h_min) && |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 642 | (i2cclk < tscl_h)) { |
Patrick Delaunay | 6f4f912 | 2019-06-21 15:26:47 +0200 | [diff] [blame] | 643 | u32 clk_error; |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 644 | |
Patrick Delaunay | 6f4f912 | 2019-06-21 15:26:47 +0200 | [diff] [blame] | 645 | if (tscl > i2cbus) |
| 646 | clk_error = tscl - i2cbus; |
| 647 | else |
| 648 | clk_error = i2cbus - tscl; |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 649 | |
| 650 | if (clk_error < clk_error_prev) { |
| 651 | clk_error_prev = clk_error; |
| 652 | v->scll = l; |
| 653 | v->sclh = h; |
Christophe Kerello | a71376a | 2017-10-17 11:21:32 +0200 | [diff] [blame] | 654 | sol_found = true; |
| 655 | memcpy(s, v, sizeof(*s)); |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 656 | } |
| 657 | } |
| 658 | } |
| 659 | } |
| 660 | } |
| 661 | |
Christophe Kerello | a71376a | 2017-10-17 11:21:32 +0200 | [diff] [blame] | 662 | if (!sol_found) { |
Patrick Delaunay | 6f600e3 | 2020-11-06 19:01:50 +0100 | [diff] [blame] | 663 | log_err("no solution at all\n"); |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 664 | ret = -EPERM; |
| 665 | } |
| 666 | |
| 667 | return ret; |
| 668 | } |
| 669 | |
Alain Volmat | e04600e | 2020-03-06 11:09:14 +0100 | [diff] [blame] | 670 | static const struct stm32_i2c_spec *get_specs(u32 rate) |
| 671 | { |
| 672 | unsigned int i; |
| 673 | |
| 674 | for (i = 0; i < ARRAY_SIZE(i2c_specs); i++) |
| 675 | if (rate <= i2c_specs[i].rate) |
| 676 | return &i2c_specs[i]; |
| 677 | |
| 678 | /* NOT REACHED */ |
| 679 | return ERR_PTR(-EINVAL); |
| 680 | } |
| 681 | |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 682 | static int stm32_i2c_compute_timing(struct stm32_i2c_priv *i2c_priv, |
Patrick Delaunay | b13ab6f | 2018-10-29 15:31:55 +0100 | [diff] [blame] | 683 | struct stm32_i2c_timings *output) |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 684 | { |
Patrick Delaunay | 10605f7 | 2021-08-03 12:05:15 +0200 | [diff] [blame] | 685 | struct stm32_i2c_setup *setup = &i2c_priv->setup; |
Alain Volmat | e04600e | 2020-03-06 11:09:14 +0100 | [diff] [blame] | 686 | const struct stm32_i2c_spec *specs; |
Patrice Chotard | c37885d | 2017-10-17 11:21:33 +0200 | [diff] [blame] | 687 | struct stm32_i2c_timings *v, *_v; |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 688 | struct list_head solutions; |
Patrick Delaunay | a14855d | 2021-08-03 12:05:14 +0200 | [diff] [blame] | 689 | u32 i2cclk = DIV_ROUND_CLOSEST(STM32_NSEC_PER_SEC, setup->clock_src); |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 690 | int ret; |
| 691 | |
Alain Volmat | e04600e | 2020-03-06 11:09:14 +0100 | [diff] [blame] | 692 | specs = get_specs(setup->speed_freq); |
| 693 | if (specs == ERR_PTR(-EINVAL)) { |
Patrick Delaunay | 6f600e3 | 2020-11-06 19:01:50 +0100 | [diff] [blame] | 694 | log_err("speed out of bound {%d}\n", |
| 695 | setup->speed_freq); |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 696 | return -EINVAL; |
| 697 | } |
| 698 | |
Alain Volmat | e04600e | 2020-03-06 11:09:14 +0100 | [diff] [blame] | 699 | if (setup->rise_time > specs->rise_max || |
| 700 | setup->fall_time > specs->fall_max) { |
Patrick Delaunay | 6f600e3 | 2020-11-06 19:01:50 +0100 | [diff] [blame] | 701 | log_err("timings out of bound Rise{%d>%d}/Fall{%d>%d}\n", |
| 702 | setup->rise_time, specs->rise_max, |
| 703 | setup->fall_time, specs->fall_max); |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 704 | return -EINVAL; |
| 705 | } |
| 706 | |
Patrick Delaunay | a14855d | 2021-08-03 12:05:14 +0200 | [diff] [blame] | 707 | /* Analog and Digital Filters */ |
| 708 | setup->dnf = DIV_ROUND_CLOSEST(i2c_priv->dnf_dt, i2cclk); |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 709 | if (setup->dnf > STM32_I2C_DNF_MAX) { |
Patrick Delaunay | 6f600e3 | 2020-11-06 19:01:50 +0100 | [diff] [blame] | 710 | log_err("DNF out of bound %d/%d\n", |
| 711 | setup->dnf, STM32_I2C_DNF_MAX); |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 712 | return -EINVAL; |
| 713 | } |
| 714 | |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 715 | INIT_LIST_HEAD(&solutions); |
Patrick Delaunay | 10605f7 | 2021-08-03 12:05:15 +0200 | [diff] [blame] | 716 | ret = stm32_i2c_compute_solutions(i2cclk, setup, specs, &solutions); |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 717 | if (ret) |
| 718 | goto exit; |
| 719 | |
Patrick Delaunay | 10605f7 | 2021-08-03 12:05:15 +0200 | [diff] [blame] | 720 | ret = stm32_i2c_choose_solution(i2cclk, setup, specs, &solutions, output); |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 721 | if (ret) |
| 722 | goto exit; |
| 723 | |
Patrick Delaunay | 6f600e3 | 2020-11-06 19:01:50 +0100 | [diff] [blame] | 724 | log_debug("Presc: %i, scldel: %i, sdadel: %i, scll: %i, sclh: %i\n", |
| 725 | output->presc, |
| 726 | output->scldel, output->sdadel, |
| 727 | output->scll, output->sclh); |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 728 | |
| 729 | exit: |
| 730 | /* Release list and memory */ |
| 731 | list_for_each_entry_safe(v, _v, &solutions, node) { |
| 732 | list_del(&v->node); |
Patrick Delaunay | 28b10a7 | 2018-03-12 10:46:09 +0100 | [diff] [blame] | 733 | free(v); |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | return ret; |
| 737 | } |
| 738 | |
Alain Volmat | e04600e | 2020-03-06 11:09:14 +0100 | [diff] [blame] | 739 | static u32 get_lower_rate(u32 rate) |
| 740 | { |
| 741 | int i; |
| 742 | |
| 743 | for (i = ARRAY_SIZE(i2c_specs) - 1; i >= 0; i--) |
| 744 | if (rate > i2c_specs[i].rate) |
| 745 | return i2c_specs[i].rate; |
| 746 | |
| 747 | return i2c_specs[0].rate; |
| 748 | } |
| 749 | |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 750 | static int stm32_i2c_setup_timing(struct stm32_i2c_priv *i2c_priv, |
Patrick Delaunay | b13ab6f | 2018-10-29 15:31:55 +0100 | [diff] [blame] | 751 | struct stm32_i2c_timings *timing) |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 752 | { |
Patrick Delaunay | 5886278 | 2021-08-03 12:05:09 +0200 | [diff] [blame] | 753 | struct stm32_i2c_setup *setup = &i2c_priv->setup; |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 754 | int ret = 0; |
| 755 | |
Alain Volmat | e04600e | 2020-03-06 11:09:14 +0100 | [diff] [blame] | 756 | setup->speed_freq = i2c_priv->speed; |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 757 | setup->clock_src = clk_get_rate(&i2c_priv->clk); |
| 758 | |
| 759 | if (!setup->clock_src) { |
Patrick Delaunay | 6f600e3 | 2020-11-06 19:01:50 +0100 | [diff] [blame] | 760 | log_err("clock rate is 0\n"); |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 761 | return -EINVAL; |
| 762 | } |
| 763 | |
| 764 | do { |
Patrick Delaunay | 10605f7 | 2021-08-03 12:05:15 +0200 | [diff] [blame] | 765 | ret = stm32_i2c_compute_timing(i2c_priv, timing); |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 766 | if (ret) { |
Patrick Delaunay | 6f600e3 | 2020-11-06 19:01:50 +0100 | [diff] [blame] | 767 | log_debug("failed to compute I2C timings.\n"); |
Alain Volmat | e04600e | 2020-03-06 11:09:14 +0100 | [diff] [blame] | 768 | if (setup->speed_freq > I2C_SPEED_STANDARD_RATE) { |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 769 | setup->speed_freq = |
Alain Volmat | e04600e | 2020-03-06 11:09:14 +0100 | [diff] [blame] | 770 | get_lower_rate(setup->speed_freq); |
Patrick Delaunay | 6f600e3 | 2020-11-06 19:01:50 +0100 | [diff] [blame] | 771 | log_debug("downgrade I2C Speed Freq to (%i)\n", |
| 772 | setup->speed_freq); |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 773 | } else { |
| 774 | break; |
| 775 | } |
| 776 | } |
| 777 | } while (ret); |
| 778 | |
| 779 | if (ret) { |
Patrick Delaunay | 6f600e3 | 2020-11-06 19:01:50 +0100 | [diff] [blame] | 780 | log_err("impossible to compute I2C timings.\n"); |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 781 | return ret; |
| 782 | } |
| 783 | |
Patrick Delaunay | 6f600e3 | 2020-11-06 19:01:50 +0100 | [diff] [blame] | 784 | log_debug("I2C Freq(%i), Clk Source(%i)\n", |
| 785 | setup->speed_freq, setup->clock_src); |
| 786 | log_debug("I2C Rise(%i) and Fall(%i) Time\n", |
| 787 | setup->rise_time, setup->fall_time); |
| 788 | log_debug("I2C Analog Filter(%s), DNF(%i)\n", |
| 789 | setup->analog_filter ? "On" : "Off", setup->dnf); |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 790 | |
Alain Volmat | e04600e | 2020-03-06 11:09:14 +0100 | [diff] [blame] | 791 | i2c_priv->speed = setup->speed_freq; |
| 792 | |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 793 | return 0; |
| 794 | } |
| 795 | |
Patrick Delaunay | 4d15c18 | 2020-07-06 13:31:35 +0200 | [diff] [blame] | 796 | static int stm32_i2c_write_fm_plus_bits(struct stm32_i2c_priv *i2c_priv) |
| 797 | { |
| 798 | int ret; |
| 799 | bool enable = i2c_priv->speed > I2C_SPEED_FAST_RATE; |
| 800 | |
| 801 | /* Optional */ |
| 802 | if (IS_ERR_OR_NULL(i2c_priv->regmap)) |
| 803 | return 0; |
| 804 | |
| 805 | if (i2c_priv->regmap_sreg == i2c_priv->regmap_creg) |
| 806 | ret = regmap_update_bits(i2c_priv->regmap, |
| 807 | i2c_priv->regmap_sreg, |
| 808 | i2c_priv->regmap_mask, |
| 809 | enable ? i2c_priv->regmap_mask : 0); |
| 810 | else |
| 811 | ret = regmap_write(i2c_priv->regmap, |
| 812 | enable ? i2c_priv->regmap_sreg : |
| 813 | i2c_priv->regmap_creg, |
| 814 | i2c_priv->regmap_mask); |
| 815 | |
| 816 | return ret; |
| 817 | } |
| 818 | |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 819 | static int stm32_i2c_hw_config(struct stm32_i2c_priv *i2c_priv) |
| 820 | { |
| 821 | struct stm32_i2c_regs *regs = i2c_priv->regs; |
| 822 | struct stm32_i2c_timings t; |
| 823 | int ret; |
| 824 | u32 timing = 0; |
| 825 | |
| 826 | ret = stm32_i2c_setup_timing(i2c_priv, &t); |
| 827 | if (ret) |
| 828 | return ret; |
| 829 | |
| 830 | /* Disable I2C */ |
| 831 | clrbits_le32(®s->cr1, STM32_I2C_CR1_PE); |
| 832 | |
Patrick Delaunay | 4d15c18 | 2020-07-06 13:31:35 +0200 | [diff] [blame] | 833 | /* Setup Fast mode plus if necessary */ |
| 834 | ret = stm32_i2c_write_fm_plus_bits(i2c_priv); |
| 835 | if (ret) |
| 836 | return ret; |
| 837 | |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 838 | /* Timing settings */ |
| 839 | timing |= STM32_I2C_TIMINGR_PRESC(t.presc); |
| 840 | timing |= STM32_I2C_TIMINGR_SCLDEL(t.scldel); |
| 841 | timing |= STM32_I2C_TIMINGR_SDADEL(t.sdadel); |
| 842 | timing |= STM32_I2C_TIMINGR_SCLH(t.sclh); |
| 843 | timing |= STM32_I2C_TIMINGR_SCLL(t.scll); |
| 844 | writel(timing, ®s->timingr); |
| 845 | |
| 846 | /* Enable I2C */ |
Patrick Delaunay | 5886278 | 2021-08-03 12:05:09 +0200 | [diff] [blame] | 847 | if (i2c_priv->setup.analog_filter) |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 848 | clrbits_le32(®s->cr1, STM32_I2C_CR1_ANFOFF); |
| 849 | else |
| 850 | setbits_le32(®s->cr1, STM32_I2C_CR1_ANFOFF); |
Patrick Delaunay | 5886278 | 2021-08-03 12:05:09 +0200 | [diff] [blame] | 851 | |
Patrick Delaunay | 05b8d60 | 2021-08-03 12:05:13 +0200 | [diff] [blame] | 852 | /* Program the Digital Filter */ |
| 853 | clrsetbits_le32(®s->cr1, STM32_I2C_CR1_DNF_MASK, |
| 854 | STM32_I2C_CR1_DNF(i2c_priv->setup.dnf)); |
| 855 | |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 856 | setbits_le32(®s->cr1, STM32_I2C_CR1_PE); |
| 857 | |
| 858 | return 0; |
| 859 | } |
| 860 | |
Patrick Delaunay | 6f600e3 | 2020-11-06 19:01:50 +0100 | [diff] [blame] | 861 | static int stm32_i2c_set_bus_speed(struct udevice *dev, unsigned int speed) |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 862 | { |
Patrick Delaunay | 6f600e3 | 2020-11-06 19:01:50 +0100 | [diff] [blame] | 863 | struct stm32_i2c_priv *i2c_priv = dev_get_priv(dev); |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 864 | |
Alain Volmat | e04600e | 2020-03-06 11:09:14 +0100 | [diff] [blame] | 865 | if (speed > I2C_SPEED_FAST_PLUS_RATE) { |
Patrick Delaunay | 6f600e3 | 2020-11-06 19:01:50 +0100 | [diff] [blame] | 866 | dev_dbg(dev, "Speed %d not supported\n", speed); |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 867 | return -EINVAL; |
| 868 | } |
| 869 | |
Alain Volmat | e04600e | 2020-03-06 11:09:14 +0100 | [diff] [blame] | 870 | i2c_priv->speed = speed; |
| 871 | |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 872 | return stm32_i2c_hw_config(i2c_priv); |
| 873 | } |
| 874 | |
| 875 | static int stm32_i2c_probe(struct udevice *dev) |
| 876 | { |
| 877 | struct stm32_i2c_priv *i2c_priv = dev_get_priv(dev); |
| 878 | struct reset_ctl reset_ctl; |
| 879 | fdt_addr_t addr; |
| 880 | int ret; |
| 881 | |
| 882 | addr = dev_read_addr(dev); |
| 883 | if (addr == FDT_ADDR_T_NONE) |
| 884 | return -EINVAL; |
| 885 | |
| 886 | i2c_priv->regs = (struct stm32_i2c_regs *)addr; |
| 887 | |
| 888 | ret = clk_get_by_index(dev, 0, &i2c_priv->clk); |
| 889 | if (ret) |
| 890 | return ret; |
| 891 | |
| 892 | ret = clk_enable(&i2c_priv->clk); |
| 893 | if (ret) |
| 894 | goto clk_free; |
| 895 | |
| 896 | ret = reset_get_by_index(dev, 0, &reset_ctl); |
| 897 | if (ret) |
| 898 | goto clk_disable; |
| 899 | |
| 900 | reset_assert(&reset_ctl); |
| 901 | udelay(2); |
| 902 | reset_deassert(&reset_ctl); |
| 903 | |
| 904 | return 0; |
| 905 | |
| 906 | clk_disable: |
| 907 | clk_disable(&i2c_priv->clk); |
| 908 | clk_free: |
| 909 | clk_free(&i2c_priv->clk); |
| 910 | |
| 911 | return ret; |
| 912 | } |
| 913 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 914 | static int stm32_of_to_plat(struct udevice *dev) |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 915 | { |
Patrick Delaunay | 5886278 | 2021-08-03 12:05:09 +0200 | [diff] [blame] | 916 | const struct stm32_i2c_data *data; |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 917 | struct stm32_i2c_priv *i2c_priv = dev_get_priv(dev); |
Patrick Delaunay | 4d15c18 | 2020-07-06 13:31:35 +0200 | [diff] [blame] | 918 | int ret; |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 919 | |
Patrick Delaunay | 5886278 | 2021-08-03 12:05:09 +0200 | [diff] [blame] | 920 | data = (const struct stm32_i2c_data *)dev_get_driver_data(dev); |
| 921 | if (!data) |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 922 | return -EINVAL; |
| 923 | |
Jorge Ramirez-Ortiz | feb3f9b | 2022-09-12 10:42:01 +0200 | [diff] [blame] | 924 | i2c_priv->setup.rise_time = dev_read_u32_default(dev, |
| 925 | "i2c-scl-rising-time-ns", |
| 926 | STM32_I2C_RISE_TIME_DEFAULT); |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 927 | |
Jorge Ramirez-Ortiz | feb3f9b | 2022-09-12 10:42:01 +0200 | [diff] [blame] | 928 | i2c_priv->setup.fall_time = dev_read_u32_default(dev, |
| 929 | "i2c-scl-falling-time-ns", |
| 930 | STM32_I2C_FALL_TIME_DEFAULT); |
Patrick Delaunay | 5886278 | 2021-08-03 12:05:09 +0200 | [diff] [blame] | 931 | |
Patrick Delaunay | a14855d | 2021-08-03 12:05:14 +0200 | [diff] [blame] | 932 | i2c_priv->dnf_dt = dev_read_u32_default(dev, "i2c-digital-filter-width-ns", 0); |
| 933 | if (!dev_read_bool(dev, "i2c-digital-filter")) |
| 934 | i2c_priv->dnf_dt = 0; |
| 935 | |
Patrick Delaunay | 20e3c41 | 2021-08-03 12:05:12 +0200 | [diff] [blame] | 936 | i2c_priv->setup.analog_filter = dev_read_bool(dev, "i2c-analog-filter"); |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 937 | |
Patrick Delaunay | 4d15c18 | 2020-07-06 13:31:35 +0200 | [diff] [blame] | 938 | /* Optional */ |
| 939 | i2c_priv->regmap = syscon_regmap_lookup_by_phandle(dev, |
| 940 | "st,syscfg-fmp"); |
| 941 | if (!IS_ERR(i2c_priv->regmap)) { |
| 942 | u32 fmp[3]; |
| 943 | |
| 944 | ret = dev_read_u32_array(dev, "st,syscfg-fmp", fmp, 3); |
| 945 | if (ret) |
| 946 | return ret; |
| 947 | |
| 948 | i2c_priv->regmap_sreg = fmp[1]; |
Patrick Delaunay | 5886278 | 2021-08-03 12:05:09 +0200 | [diff] [blame] | 949 | i2c_priv->regmap_creg = fmp[1] + data->fmp_clr_offset; |
Patrick Delaunay | 4d15c18 | 2020-07-06 13:31:35 +0200 | [diff] [blame] | 950 | i2c_priv->regmap_mask = fmp[2]; |
| 951 | } |
| 952 | |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 953 | return 0; |
| 954 | } |
| 955 | |
| 956 | static const struct dm_i2c_ops stm32_i2c_ops = { |
| 957 | .xfer = stm32_i2c_xfer, |
| 958 | .set_bus_speed = stm32_i2c_set_bus_speed, |
| 959 | }; |
| 960 | |
| 961 | static const struct udevice_id stm32_i2c_of_match[] = { |
Patrick Delaunay | 5886278 | 2021-08-03 12:05:09 +0200 | [diff] [blame] | 962 | { .compatible = "st,stm32f7-i2c", .data = (ulong)&stm32f7_data }, |
| 963 | { .compatible = "st,stm32mp15-i2c", .data = (ulong)&stm32mp15_data }, |
Patrick Delaunay | 11c064e | 2022-06-30 10:20:14 +0200 | [diff] [blame] | 964 | { .compatible = "st,stm32mp13-i2c", .data = (ulong)&stm32mp13_data }, |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 965 | {} |
| 966 | }; |
| 967 | |
| 968 | U_BOOT_DRIVER(stm32f7_i2c) = { |
| 969 | .name = "stm32f7-i2c", |
| 970 | .id = UCLASS_I2C, |
| 971 | .of_match = stm32_i2c_of_match, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 972 | .of_to_plat = stm32_of_to_plat, |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 973 | .probe = stm32_i2c_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 974 | .priv_auto = sizeof(struct stm32_i2c_priv), |
Patrice Chotard | ebf442d | 2017-08-09 14:45:27 +0200 | [diff] [blame] | 975 | .ops = &stm32_i2c_ops, |
| 976 | }; |