1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) 2 /* 3 * Copyright (c) 2017-2019, STMicroelectronics 4 * 5 * The driver API is defined in header file stm32_i2c.h. 6 * 7 * I2C bus driver does not register to the PM framework. It is the 8 * responsibility of the bus owner to call the related STM32 I2C driver 9 * API functions when bus suspends or resumes. 10 */ 11 12 #include <arm.h> 13 #include <drivers/stm32_i2c.h> 14 #include <io.h> 15 #include <kernel/delay.h> 16 #include <kernel/dt.h> 17 #include <kernel/boot.h> 18 #include <kernel/panic.h> 19 #include <libfdt.h> 20 #include <stdbool.h> 21 #include <stdlib.h> 22 #include <stm32_util.h> 23 #include <trace.h> 24 25 /* STM32 I2C registers offsets */ 26 #define I2C_CR1 0x00U 27 #define I2C_CR2 0x04U 28 #define I2C_OAR1 0x08U 29 #define I2C_OAR2 0x0CU 30 #define I2C_TIMINGR 0x10U 31 #define I2C_TIMEOUTR 0x14U 32 #define I2C_ISR 0x18U 33 #define I2C_ICR 0x1CU 34 #define I2C_PECR 0x20U 35 #define I2C_RXDR 0x24U 36 #define I2C_TXDR 0x28U 37 #define I2C_SIZE 0x2CU 38 39 /* Bit definition for I2C_CR1 register */ 40 #define I2C_CR1_PE BIT(0) 41 #define I2C_CR1_TXIE BIT(1) 42 #define I2C_CR1_RXIE BIT(2) 43 #define I2C_CR1_ADDRIE BIT(3) 44 #define I2C_CR1_NACKIE BIT(4) 45 #define I2C_CR1_STOPIE BIT(5) 46 #define I2C_CR1_TCIE BIT(6) 47 #define I2C_CR1_ERRIE BIT(7) 48 #define I2C_CR1_DNF GENMASK_32(11, 8) 49 #define I2C_CR1_ANFOFF BIT(12) 50 #define I2C_CR1_SWRST BIT(13) 51 #define I2C_CR1_TXDMAEN BIT(14) 52 #define I2C_CR1_RXDMAEN BIT(15) 53 #define I2C_CR1_SBC BIT(16) 54 #define I2C_CR1_NOSTRETCH BIT(17) 55 #define I2C_CR1_WUPEN BIT(18) 56 #define I2C_CR1_GCEN BIT(19) 57 #define I2C_CR1_SMBHEN BIT(22) 58 #define I2C_CR1_SMBDEN BIT(21) 59 #define I2C_CR1_ALERTEN BIT(22) 60 #define I2C_CR1_PECEN BIT(23) 61 62 /* Bit definition for I2C_CR2 register */ 63 #define I2C_CR2_SADD GENMASK_32(9, 0) 64 #define I2C_CR2_RD_WRN BIT(10) 65 #define I2C_CR2_RD_WRN_OFFSET 10U 66 #define I2C_CR2_ADD10 BIT(11) 67 #define I2C_CR2_HEAD10R BIT(12) 68 #define I2C_CR2_START BIT(13) 69 #define I2C_CR2_STOP BIT(14) 70 #define I2C_CR2_NACK BIT(15) 71 #define I2C_CR2_NBYTES GENMASK_32(23, 16) 72 #define I2C_CR2_NBYTES_OFFSET 16U 73 #define I2C_CR2_RELOAD BIT(24) 74 #define I2C_CR2_AUTOEND BIT(25) 75 #define I2C_CR2_PECBYTE BIT(26) 76 77 /* Bit definition for I2C_OAR1 register */ 78 #define I2C_OAR1_OA1 GENMASK_32(9, 0) 79 #define I2C_OAR1_OA1MODE BIT(10) 80 #define I2C_OAR1_OA1EN BIT(15) 81 82 /* Bit definition for I2C_OAR2 register */ 83 #define I2C_OAR2_OA2 GENMASK_32(7, 1) 84 #define I2C_OAR2_OA2MSK GENMASK_32(10, 8) 85 #define I2C_OAR2_OA2NOMASK 0 86 #define I2C_OAR2_OA2MASK01 BIT(8) 87 #define I2C_OAR2_OA2MASK02 BIT(9) 88 #define I2C_OAR2_OA2MASK03 GENMASK_32(9, 8) 89 #define I2C_OAR2_OA2MASK04 BIT(10) 90 #define I2C_OAR2_OA2MASK05 (BIT(8) | BIT(10)) 91 #define I2C_OAR2_OA2MASK06 (BIT(9) | BIT(10)) 92 #define I2C_OAR2_OA2MASK07 GENMASK_32(10, 8) 93 #define I2C_OAR2_OA2EN BIT(15) 94 95 /* Bit definition for I2C_TIMINGR register */ 96 #define I2C_TIMINGR_SCLL GENMASK_32(7, 0) 97 #define I2C_TIMINGR_SCLH GENMASK_32(15, 8) 98 #define I2C_TIMINGR_SDADEL GENMASK_32(19, 16) 99 #define I2C_TIMINGR_SCLDEL GENMASK_32(23, 20) 100 #define I2C_TIMINGR_PRESC GENMASK_32(31, 28) 101 #define I2C_TIMINGR_SCLL_MAX (I2C_TIMINGR_SCLL + 1) 102 #define I2C_TIMINGR_SCLH_MAX ((I2C_TIMINGR_SCLH >> 8) + 1) 103 #define I2C_TIMINGR_SDADEL_MAX ((I2C_TIMINGR_SDADEL >> 16) + 1) 104 #define I2C_TIMINGR_SCLDEL_MAX ((I2C_TIMINGR_SCLDEL >> 20) + 1) 105 #define I2C_TIMINGR_PRESC_MAX ((I2C_TIMINGR_PRESC >> 28) + 1) 106 #define I2C_SET_TIMINGR_SCLL(n) ((n) & \ 107 (I2C_TIMINGR_SCLL_MAX - 1)) 108 #define I2C_SET_TIMINGR_SCLH(n) (((n) & \ 109 (I2C_TIMINGR_SCLH_MAX - 1)) << 8) 110 #define I2C_SET_TIMINGR_SDADEL(n) (((n) & \ 111 (I2C_TIMINGR_SDADEL_MAX - 1)) << 16) 112 #define I2C_SET_TIMINGR_SCLDEL(n) (((n) & \ 113 (I2C_TIMINGR_SCLDEL_MAX - 1)) << 20) 114 #define I2C_SET_TIMINGR_PRESC(n) (((n) & \ 115 (I2C_TIMINGR_PRESC_MAX - 1)) << 28) 116 117 /* Bit definition for I2C_TIMEOUTR register */ 118 #define I2C_TIMEOUTR_TIMEOUTA GENMASK_32(11, 0) 119 #define I2C_TIMEOUTR_TIDLE BIT(12) 120 #define I2C_TIMEOUTR_TIMOUTEN BIT(15) 121 #define I2C_TIMEOUTR_TIMEOUTB GENMASK_32(27, 16) 122 #define I2C_TIMEOUTR_TEXTEN BIT(31) 123 124 /* Bit definition for I2C_ISR register */ 125 #define I2C_ISR_TXE BIT(0) 126 #define I2C_ISR_TXIS BIT(1) 127 #define I2C_ISR_RXNE BIT(2) 128 #define I2C_ISR_ADDR BIT(3) 129 #define I2C_ISR_NACKF BIT(4) 130 #define I2C_ISR_STOPF BIT(5) 131 #define I2C_ISR_TC BIT(6) 132 #define I2C_ISR_TCR BIT(7) 133 #define I2C_ISR_BERR BIT(8) 134 #define I2C_ISR_ARLO BIT(9) 135 #define I2C_ISR_OVR BIT(10) 136 #define I2C_ISR_PECERR BIT(11) 137 #define I2C_ISR_TIMEOUT BIT(12) 138 #define I2C_ISR_ALERT BIT(13) 139 #define I2C_ISR_BUSY BIT(15) 140 #define I2C_ISR_DIR BIT(16) 141 #define I2C_ISR_ADDCODE GENMASK_32(23, 17) 142 143 /* Bit definition for I2C_ICR register */ 144 #define I2C_ICR_ADDRCF BIT(3) 145 #define I2C_ICR_NACKCF BIT(4) 146 #define I2C_ICR_STOPCF BIT(5) 147 #define I2C_ICR_BERRCF BIT(8) 148 #define I2C_ICR_ARLOCF BIT(9) 149 #define I2C_ICR_OVRCF BIT(10) 150 #define I2C_ICR_PECCF BIT(11) 151 #define I2C_ICR_TIMOUTCF BIT(12) 152 #define I2C_ICR_ALERTCF BIT(13) 153 154 /* Max data size for a single I2C transfer */ 155 #define MAX_NBYTE_SIZE 255U 156 157 #define I2C_NSEC_PER_SEC 1000000000UL 158 #define I2C_TIMEOUT_BUSY_MS 25 159 #define I2C_TIMEOUT_BUSY_US (I2C_TIMEOUT_BUSY_MS * 1000) 160 161 #define CR2_RESET_MASK (I2C_CR2_SADD | I2C_CR2_HEAD10R | \ 162 I2C_CR2_NBYTES | I2C_CR2_RELOAD | \ 163 I2C_CR2_RD_WRN) 164 165 #define TIMINGR_CLEAR_MASK (I2C_TIMINGR_SCLL | I2C_TIMINGR_SCLH | \ 166 I2C_TIMINGR_SDADEL | \ 167 I2C_TIMINGR_SCLDEL | I2C_TIMINGR_PRESC) 168 169 /* 170 * I2C transfer modes 171 * I2C_RELOAD: Enable Reload mode 172 * I2C_AUTOEND_MODE: Enable automatic end mode 173 * I2C_SOFTEND_MODE: Enable software end mode 174 */ 175 #define I2C_RELOAD_MODE I2C_CR2_RELOAD 176 #define I2C_AUTOEND_MODE I2C_CR2_AUTOEND 177 #define I2C_SOFTEND_MODE 0x0 178 179 /* 180 * Start/restart/stop I2C transfer requests. 181 * 182 * I2C_NO_STARTSTOP: Don't Generate stop and start condition 183 * I2C_GENERATE_STOP: Generate stop condition (size should be set to 0) 184 * I2C_GENERATE_START_READ: Generate Restart for read request. 185 * I2C_GENERATE_START_WRITE: Generate Restart for write request 186 */ 187 #define I2C_NO_STARTSTOP 0x0 188 #define I2C_GENERATE_STOP (BIT(31) | I2C_CR2_STOP) 189 #define I2C_GENERATE_START_READ (BIT(31) | I2C_CR2_START | \ 190 I2C_CR2_RD_WRN) 191 #define I2C_GENERATE_START_WRITE (BIT(31) | I2C_CR2_START) 192 193 /* Memory address byte sizes */ 194 #define I2C_MEMADD_SIZE_8BIT 1 195 #define I2C_MEMADD_SIZE_16BIT 2 196 197 /* Effective rate cannot be lower than 80% target rate */ 198 #define RATE_MIN(rate) (((rate) * 80U) / 100U) 199 200 /* 201 * struct i2c_spec_s - Private I2C timing specifications. 202 * @rate: I2C bus speed (Hz) 203 * @fall_max: Max fall time of both SDA and SCL signals (ns) 204 * @rise_max: Max rise time of both SDA and SCL signals (ns) 205 * @hddat_min: Min data hold time (ns) 206 * @vddat_max: Max data valid time (ns) 207 * @sudat_min: Min data setup time (ns) 208 * @l_min: Min low period of the SCL clock (ns) 209 * @h_min: Min high period of the SCL clock (ns) 210 */ 211 struct i2c_spec_s { 212 uint32_t rate; 213 uint32_t fall_max; 214 uint32_t rise_max; 215 uint32_t hddat_min; 216 uint32_t vddat_max; 217 uint32_t sudat_min; 218 uint32_t l_min; 219 uint32_t h_min; 220 }; 221 222 /* 223 * struct i2c_timing_s - Private I2C output parameters. 224 * @scldel: Data setup time 225 * @sdadel: Data hold time 226 * @sclh: SCL high period (master mode) 227 * @sclh: SCL low period (master mode) 228 * @is_saved: True if relating to a configuration candidate 229 */ 230 struct i2c_timing_s { 231 uint8_t scldel; 232 uint8_t sdadel; 233 uint8_t sclh; 234 uint8_t scll; 235 bool is_saved; 236 }; 237 238 /* This table must be sorted in increasing value for field @rate */ 239 static const struct i2c_spec_s i2c_specs[] = { 240 /* Standard - 100KHz */ 241 { 242 .rate = I2C_STANDARD_RATE, 243 .fall_max = 300, 244 .rise_max = 1000, 245 .hddat_min = 0, 246 .vddat_max = 3450, 247 .sudat_min = 250, 248 .l_min = 4700, 249 .h_min = 4000, 250 }, 251 /* Fast - 400KHz */ 252 { 253 .rate = I2C_FAST_RATE, 254 .fall_max = 300, 255 .rise_max = 300, 256 .hddat_min = 0, 257 .vddat_max = 900, 258 .sudat_min = 100, 259 .l_min = 1300, 260 .h_min = 600, 261 }, 262 /* FastPlus - 1MHz */ 263 { 264 .rate = I2C_FAST_PLUS_RATE, 265 .fall_max = 100, 266 .rise_max = 120, 267 .hddat_min = 0, 268 .vddat_max = 450, 269 .sudat_min = 50, 270 .l_min = 500, 271 .h_min = 260, 272 }, 273 }; 274 275 /* 276 * I2C request parameters 277 * @dev_addr: I2C address of the target device 278 * @mode: Communication mode, one of I2C_MODE_(MASTER|MEM) 279 * @mem_addr: Target memory cell accessed in device (memory mode) 280 * @mem_addr_size: Byte size of the memory cell address (memory mode) 281 * @timeout_ms: Timeout in millisenconds for the request 282 */ 283 struct i2c_request { 284 uint32_t dev_addr; 285 enum i2c_mode_e mode; 286 uint32_t mem_addr; 287 uint32_t mem_addr_size; 288 unsigned int timeout_ms; 289 }; 290 291 static vaddr_t get_base(struct i2c_handle_s *hi2c) 292 { 293 return io_pa_or_va_secure(&hi2c->base, hi2c->reg_size); 294 } 295 296 static void notif_i2c_timeout(struct i2c_handle_s *hi2c) 297 { 298 hi2c->i2c_err |= I2C_ERROR_TIMEOUT; 299 hi2c->i2c_state = I2C_STATE_READY; 300 } 301 302 static const struct i2c_spec_s *get_specs(uint32_t rate) 303 { 304 size_t i = 0; 305 306 for (i = 0; i < ARRAY_SIZE(i2c_specs); i++) 307 if (rate <= i2c_specs[i].rate) 308 return i2c_specs + i; 309 310 return NULL; 311 } 312 313 static void save_cfg(struct i2c_handle_s *hi2c, struct i2c_cfg *cfg) 314 { 315 vaddr_t base = get_base(hi2c); 316 317 stm32_clock_enable(hi2c->clock); 318 319 cfg->cr1 = io_read32(base + I2C_CR1); 320 cfg->cr2 = io_read32(base + I2C_CR2); 321 cfg->oar1 = io_read32(base + I2C_OAR1); 322 cfg->oar2 = io_read32(base + I2C_OAR2); 323 cfg->timingr = io_read32(base + I2C_TIMINGR); 324 325 stm32_clock_disable(hi2c->clock); 326 } 327 328 static void restore_cfg(struct i2c_handle_s *hi2c, struct i2c_cfg *cfg) 329 { 330 vaddr_t base = get_base(hi2c); 331 332 stm32_clock_enable(hi2c->clock); 333 334 io_clrbits32(base + I2C_CR1, I2C_CR1_PE); 335 io_write32(base + I2C_TIMINGR, cfg->timingr & TIMINGR_CLEAR_MASK); 336 io_write32(base + I2C_OAR1, cfg->oar1); 337 io_write32(base + I2C_CR2, cfg->cr2); 338 io_write32(base + I2C_OAR2, cfg->oar2); 339 io_write32(base + I2C_CR1, cfg->cr1 & ~I2C_CR1_PE); 340 io_setbits32(base + I2C_CR1, cfg->cr1 & I2C_CR1_PE); 341 342 stm32_clock_disable(hi2c->clock); 343 } 344 345 static void __maybe_unused dump_cfg(struct i2c_cfg *cfg __maybe_unused) 346 { 347 DMSG("CR1: %#"PRIx32, cfg->cr1); 348 DMSG("CR2: %#"PRIx32, cfg->cr2); 349 DMSG("OAR1: %#"PRIx32, cfg->oar1); 350 DMSG("OAR2: %#"PRIx32, cfg->oar2); 351 DMSG("TIM: %#"PRIx32, cfg->timingr); 352 } 353 354 static void __maybe_unused dump_i2c(struct i2c_handle_s *hi2c) 355 { 356 vaddr_t __maybe_unused base = get_base(hi2c); 357 358 stm32_clock_enable(hi2c->clock); 359 360 DMSG("CR1: %#"PRIx32, io_read32(base + I2C_CR1)); 361 DMSG("CR2: %#"PRIx32, io_read32(base + I2C_CR2)); 362 DMSG("OAR1: %#"PRIx32, io_read32(base + I2C_OAR1)); 363 DMSG("OAR2: %#"PRIx32, io_read32(base + I2C_OAR2)); 364 DMSG("TIM: %#"PRIx32, io_read32(base + I2C_TIMINGR)); 365 366 stm32_clock_disable(hi2c->clock); 367 } 368 369 /* 370 * Compute the I2C device timings 371 * 372 * @init: Ref to the initialization configuration structure 373 * @clock_src: I2C clock source frequency (Hz) 374 * @timing: Pointer to the final computed timing result 375 * Return 0 on success or a negative value 376 */ 377 static int i2c_compute_timing(struct stm32_i2c_init_s *init, 378 unsigned long clock_src, uint32_t *timing) 379 { 380 const struct i2c_spec_s *specs = NULL; 381 uint32_t speed_freq = 0; 382 uint32_t i2cbus = UDIV_ROUND_NEAREST(I2C_NSEC_PER_SEC, speed_freq); 383 uint32_t i2cclk = UDIV_ROUND_NEAREST(I2C_NSEC_PER_SEC, clock_src); 384 uint32_t p_prev = I2C_TIMINGR_PRESC_MAX; 385 uint32_t af_delay_min = 0; 386 uint32_t af_delay_max = 0; 387 uint32_t dnf_delay = 0; 388 uint32_t tsync = 0; 389 uint32_t clk_min = 0; 390 uint32_t clk_max = 0; 391 int clk_error_prev = 0; 392 uint16_t p = 0; 393 uint16_t l = 0; 394 uint16_t a = 0; 395 uint16_t h = 0; 396 unsigned int sdadel_min = 0; 397 unsigned int sdadel_max = 0; 398 unsigned int scldel_min = 0; 399 unsigned int delay = 0; 400 int s = -1; 401 struct i2c_timing_s solutions[I2C_TIMINGR_PRESC_MAX] = { 0 }; 402 403 specs = get_specs(init->bus_rate); 404 if (!specs) { 405 DMSG("I2C speed out of bound: %"PRId32"Hz", init->bus_rate); 406 return -1; 407 } 408 409 speed_freq = specs->rate; 410 i2cbus = UDIV_ROUND_NEAREST(I2C_NSEC_PER_SEC, speed_freq); 411 clk_error_prev = INT_MAX; 412 413 if (init->rise_time > specs->rise_max || 414 init->fall_time > specs->fall_max) { 415 DMSG("I2C rise{%"PRId32">%"PRId32"}/fall{%"PRId32">%"PRId32"}", 416 init->rise_time, specs->rise_max, 417 init->fall_time, specs->fall_max); 418 return -1; 419 } 420 421 if (init->digital_filter_coef > STM32_I2C_DIGITAL_FILTER_MAX) { 422 DMSG("DNF out of bound %"PRId8"/%d", 423 init->digital_filter_coef, STM32_I2C_DIGITAL_FILTER_MAX); 424 return -1; 425 } 426 427 /* Analog and Digital Filters */ 428 if (init->analog_filter) { 429 af_delay_min = STM32_I2C_ANALOG_FILTER_DELAY_MIN; 430 af_delay_max = STM32_I2C_ANALOG_FILTER_DELAY_MAX; 431 } 432 dnf_delay = init->digital_filter_coef * i2cclk; 433 434 sdadel_min = specs->hddat_min + init->fall_time; 435 delay = af_delay_min - ((init->digital_filter_coef + 3) * i2cclk); 436 if (SUB_OVERFLOW(sdadel_min, delay, &sdadel_min)) 437 sdadel_min = 0; 438 439 sdadel_max = specs->vddat_max - init->rise_time; 440 delay = af_delay_max - ((init->digital_filter_coef + 4) * i2cclk); 441 if (SUB_OVERFLOW(sdadel_max, delay, &sdadel_max)) 442 sdadel_max = 0; 443 444 scldel_min = init->rise_time + specs->sudat_min; 445 446 DMSG("I2C SDADEL(min/max): %u/%u, SCLDEL(Min): %u", 447 sdadel_min, sdadel_max, scldel_min); 448 449 /* Compute possible values for PRESC, SCLDEL and SDADEL */ 450 for (p = 0; p < I2C_TIMINGR_PRESC_MAX; p++) { 451 for (l = 0; l < I2C_TIMINGR_SCLDEL_MAX; l++) { 452 uint32_t scldel = (l + 1) * (p + 1) * i2cclk; 453 454 if (scldel < scldel_min) 455 continue; 456 457 for (a = 0; a < I2C_TIMINGR_SDADEL_MAX; a++) { 458 uint32_t sdadel = (a * (p + 1) + 1) * i2cclk; 459 460 if ((sdadel >= sdadel_min) && 461 (sdadel <= sdadel_max) && 462 (p != p_prev)) { 463 solutions[p].scldel = l; 464 solutions[p].sdadel = a; 465 solutions[p].is_saved = true; 466 p_prev = p; 467 break; 468 } 469 } 470 471 if (p_prev == p) 472 break; 473 } 474 } 475 476 if (p_prev == I2C_TIMINGR_PRESC_MAX) { 477 DMSG("I2C no Prescaler solution"); 478 return -1; 479 } 480 481 tsync = af_delay_min + dnf_delay + (2 * i2cclk); 482 clk_max = I2C_NSEC_PER_SEC / RATE_MIN(specs->rate); 483 clk_min = I2C_NSEC_PER_SEC / specs->rate; 484 485 /* 486 * Among prescaler possibilities discovered above figures out SCL Low 487 * and High Period. Provided: 488 * - SCL Low Period has to be higher than Low Period of the SCL Clock 489 * defined by I2C Specification. I2C Clock has to be lower than 490 * (SCL Low Period - Analog/Digital filters) / 4. 491 * - SCL High Period has to be lower than High Period of the SCL Clock 492 * defined by I2C Specification. 493 * - I2C Clock has to be lower than SCL High Period. 494 */ 495 for (p = 0; p < I2C_TIMINGR_PRESC_MAX; p++) { 496 uint32_t prescaler = (p + 1) * i2cclk; 497 498 if (!solutions[p].is_saved) 499 continue; 500 501 for (l = 0; l < I2C_TIMINGR_SCLL_MAX; l++) { 502 uint32_t tscl_l = ((l + 1) * prescaler) + tsync; 503 504 if (tscl_l < specs->l_min || 505 i2cclk >= ((tscl_l - af_delay_min - dnf_delay) / 4)) 506 continue; 507 508 for (h = 0; h < I2C_TIMINGR_SCLH_MAX; h++) { 509 uint32_t tscl_h = ((h + 1) * prescaler) + tsync; 510 uint32_t tscl = tscl_l + tscl_h + 511 init->rise_time + 512 init->fall_time; 513 514 if (tscl >= clk_min && tscl <= clk_max && 515 tscl_h >= specs->h_min && i2cclk < tscl_h) { 516 int clk_error = tscl - i2cbus; 517 518 if (clk_error < 0) 519 clk_error = -clk_error; 520 521 if (clk_error < clk_error_prev) { 522 clk_error_prev = clk_error; 523 solutions[p].scll = l; 524 solutions[p].sclh = h; 525 s = p; 526 } 527 } 528 } 529 } 530 } 531 532 if (s < 0) { 533 DMSG("I2C no solution at all"); 534 return -1; 535 } 536 537 /* Finalize timing settings */ 538 *timing = I2C_SET_TIMINGR_PRESC(s) | 539 I2C_SET_TIMINGR_SCLDEL(solutions[s].scldel) | 540 I2C_SET_TIMINGR_SDADEL(solutions[s].sdadel) | 541 I2C_SET_TIMINGR_SCLH(solutions[s].sclh) | 542 I2C_SET_TIMINGR_SCLL(solutions[s].scll); 543 544 DMSG("I2C TIMINGR (PRESC/SCLDEL/SDADEL): %i/%"PRIu8"/%"PRIu8, 545 s, solutions[s].scldel, solutions[s].sdadel); 546 DMSG("I2C TIMINGR (SCLH/SCLL): %"PRIu8"/%"PRIu8, 547 solutions[s].sclh, solutions[s].scll); 548 DMSG("I2C TIMINGR: 0x%"PRIx32, *timing); 549 550 return 0; 551 } 552 553 /* i2c_specs[] must be sorted by increasing rate */ 554 static bool __maybe_unused i2c_specs_is_consistent(void) 555 { 556 size_t i = 0; 557 558 COMPILE_TIME_ASSERT(ARRAY_SIZE(i2c_specs)); 559 560 for (i = 1; i < ARRAY_SIZE(i2c_specs); i++) 561 if (i2c_specs[i - 1].rate >= i2c_specs[i].rate) 562 return false; 563 564 return true; 565 } 566 567 /* 568 * @brief From requested rate, get the closest I2C rate without exceeding it, 569 * within I2C specification values defined in @i2c_specs. 570 * @param rate: The requested rate. 571 * @retval Found rate, else the lowest value supported by platform. 572 */ 573 static uint32_t get_lower_rate(uint32_t rate) 574 { 575 size_t i = 0; 576 577 for (i = ARRAY_SIZE(i2c_specs); i > 0; i--) 578 if (rate > i2c_specs[i - 1].rate) 579 return i2c_specs[i - 1].rate; 580 581 return i2c_specs[0].rate; 582 } 583 584 /* 585 * Setup the I2C device timings 586 * 587 * @hi2c: I2C handle structure 588 * @init: Ref to the initialization configuration structure 589 * @timing: Output TIMINGR register configuration value 590 * @retval 0 if OK, negative value else 591 */ 592 static int i2c_setup_timing(struct i2c_handle_s *hi2c, 593 struct stm32_i2c_init_s *init, 594 uint32_t *timing) 595 { 596 int rc = 0; 597 unsigned long clock_src = 0; 598 599 assert(i2c_specs_is_consistent()); 600 601 clock_src = stm32_clock_get_rate(hi2c->clock); 602 if (!clock_src) { 603 DMSG("Null I2C clock rate"); 604 return -1; 605 } 606 607 /* 608 * If the timing has already been computed, and the frequency is the 609 * same as when it was computed, then use the saved timing. 610 */ 611 if (clock_src == hi2c->saved_frequency) { 612 *timing = hi2c->saved_timing; 613 return 0; 614 } 615 616 do { 617 rc = i2c_compute_timing(init, clock_src, timing); 618 if (rc) { 619 DMSG("Failed to compute I2C timings"); 620 if (init->bus_rate > I2C_STANDARD_RATE) { 621 init->bus_rate = get_lower_rate(init->bus_rate); 622 IMSG("Downgrade I2C speed to %"PRIu32"Hz)", 623 init->bus_rate); 624 } else { 625 break; 626 } 627 } 628 } while (rc); 629 630 if (rc) { 631 DMSG("Impossible to compute I2C timings"); 632 return rc; 633 } 634 635 DMSG("I2C Freq(%"PRIu32"Hz), Clk Source(%lu)", 636 init->bus_rate, clock_src); 637 DMSG("I2C Rise(%"PRId32") and Fall(%"PRId32") Time", 638 init->rise_time, init->fall_time); 639 DMSG("I2C Analog Filter(%s), DNF(%"PRIu8")", 640 init->analog_filter ? "On" : "Off", init->digital_filter_coef); 641 642 hi2c->saved_timing = *timing; 643 hi2c->saved_frequency = clock_src; 644 645 return 0; 646 } 647 648 /* 649 * Configure I2C Analog noise filter. 650 * @hi2c: I2C handle structure 651 * @analog_filter_on: True if enabling analog filter, false otherwise 652 * Return 0 on success or a negative value 653 */ 654 static int i2c_config_analog_filter(struct i2c_handle_s *hi2c, 655 bool analog_filter_on) 656 { 657 vaddr_t base = get_base(hi2c); 658 659 if (hi2c->i2c_state != I2C_STATE_READY) 660 return -1; 661 662 hi2c->i2c_state = I2C_STATE_BUSY; 663 664 /* Disable the selected I2C peripheral */ 665 io_clrbits32(base + I2C_CR1, I2C_CR1_PE); 666 667 /* Reset I2Cx ANOFF bit */ 668 io_clrbits32(base + I2C_CR1, I2C_CR1_ANFOFF); 669 670 /* Set analog filter bit if filter is disabled */ 671 if (!analog_filter_on) 672 io_setbits32(base + I2C_CR1, I2C_CR1_ANFOFF); 673 674 /* Enable the selected I2C peripheral */ 675 io_setbits32(base + I2C_CR1, I2C_CR1_PE); 676 677 hi2c->i2c_state = I2C_STATE_READY; 678 679 return 0; 680 } 681 682 TEE_Result stm32_i2c_get_setup_from_fdt(void *fdt, int node, 683 struct stm32_i2c_init_s *init, 684 struct stm32_pinctrl **pinctrl, 685 size_t *pinctrl_count) 686 { 687 const fdt32_t *cuint = NULL; 688 struct dt_node_info info = { .status = 0 }; 689 int count = 0; 690 691 /* Default STM32 specific configs caller may need to overwrite */ 692 memset(init, 0, sizeof(*init)); 693 694 _fdt_fill_device_info(fdt, &info, node); 695 assert(info.reg != DT_INFO_INVALID_REG && 696 info.reg_size != DT_INFO_INVALID_REG_SIZE && 697 info.clock != DT_INFO_INVALID_CLOCK); 698 699 init->dt_status = info.status; 700 init->pbase = info.reg; 701 init->reg_size = info.reg_size; 702 init->clock = info.clock; 703 704 cuint = fdt_getprop(fdt, node, "i2c-scl-rising-time-ns", NULL); 705 if (cuint) 706 init->rise_time = fdt32_to_cpu(*cuint); 707 else 708 init->rise_time = STM32_I2C_RISE_TIME_DEFAULT; 709 710 cuint = fdt_getprop(fdt, node, "i2c-scl-falling-time-ns", NULL); 711 if (cuint) 712 init->fall_time = fdt32_to_cpu(*cuint); 713 else 714 init->fall_time = STM32_I2C_FALL_TIME_DEFAULT; 715 716 cuint = fdt_getprop(fdt, node, "clock-frequency", NULL); 717 if (cuint) { 718 init->bus_rate = fdt32_to_cpu(*cuint); 719 720 if (init->bus_rate > I2C_FAST_PLUS_RATE) { 721 DMSG("Invalid bus speed (%"PRIu32" > %i)", 722 init->bus_rate, I2C_FAST_PLUS_RATE); 723 return TEE_ERROR_GENERIC; 724 } 725 } else { 726 init->bus_rate = I2C_STANDARD_RATE; 727 } 728 729 count = stm32_pinctrl_fdt_get_pinctrl(fdt, node, NULL, 0); 730 if (count <= 0) { 731 *pinctrl = NULL; 732 *pinctrl_count = count; 733 DMSG("Failed to get pinctrl: FDT errno %d", count); 734 return TEE_ERROR_GENERIC; 735 } 736 737 if (count > 2) { 738 DMSG("Too many PINCTRLs found: %zd", count); 739 return TEE_ERROR_GENERIC; 740 } 741 742 *pinctrl = calloc(count, sizeof(**pinctrl)); 743 if (!*pinctrl) 744 return TEE_ERROR_OUT_OF_MEMORY; 745 746 *pinctrl_count = stm32_pinctrl_fdt_get_pinctrl(fdt, node, 747 *pinctrl, count); 748 assert(*pinctrl_count == (unsigned int)count); 749 750 return TEE_SUCCESS; 751 } 752 753 int stm32_i2c_init(struct i2c_handle_s *hi2c, 754 struct stm32_i2c_init_s *init_data) 755 { 756 int rc = 0; 757 uint32_t timing = 0; 758 vaddr_t base = 0; 759 uint32_t val = 0; 760 761 hi2c->dt_status = init_data->dt_status; 762 hi2c->base.pa = init_data->pbase; 763 hi2c->reg_size = init_data->reg_size; 764 hi2c->clock = init_data->clock; 765 766 rc = i2c_setup_timing(hi2c, init_data, &timing); 767 if (rc) 768 return rc; 769 770 stm32_clock_enable(hi2c->clock); 771 base = get_base(hi2c); 772 hi2c->i2c_state = I2C_STATE_BUSY; 773 774 /* Disable the selected I2C peripheral */ 775 io_clrbits32(base + I2C_CR1, I2C_CR1_PE); 776 777 /* Configure I2Cx: Frequency range */ 778 io_write32(base + I2C_TIMINGR, timing & TIMINGR_CLEAR_MASK); 779 780 /* Disable Own Address1 before set the Own Address1 configuration */ 781 io_write32(base + I2C_OAR1, 0); 782 783 /* Configure I2Cx: Own Address1 and ack own address1 mode */ 784 if (init_data->addr_mode_10b_not_7b) 785 io_write32(base + I2C_OAR1, 786 I2C_OAR1_OA1EN | I2C_OAR1_OA1MODE | 787 init_data->own_address1); 788 else 789 io_write32(base + I2C_OAR1, 790 I2C_OAR1_OA1EN | init_data->own_address1); 791 792 /* Configure I2Cx: Addressing Master mode */ 793 io_write32(base + I2C_CR2, 0); 794 if (init_data->addr_mode_10b_not_7b) 795 io_setbits32(base + I2C_CR2, I2C_CR2_ADD10); 796 797 /* 798 * Enable the AUTOEND by default, and enable NACK 799 * (should be disabled only during Slave process). 800 */ 801 io_setbits32(base + I2C_CR2, I2C_CR2_AUTOEND | I2C_CR2_NACK); 802 803 /* Disable Own Address2 before set the Own Address2 configuration */ 804 io_write32(base + I2C_OAR2, 0); 805 806 /* Configure I2Cx: Dual mode and Own Address2 */ 807 if (init_data->dual_address_mode) 808 io_write32(base + I2C_OAR2, 809 I2C_OAR2_OA2EN | init_data->own_address2 | 810 (init_data->own_address2_masks << 8)); 811 812 /* Configure I2Cx: Generalcall and NoStretch mode */ 813 val = 0; 814 if (init_data->general_call_mode) 815 val |= I2C_CR1_GCEN; 816 if (init_data->no_stretch_mode) 817 val |= I2C_CR1_NOSTRETCH; 818 io_write32(base + I2C_CR1, val); 819 820 /* Enable the selected I2C peripheral */ 821 io_setbits32(base + I2C_CR1, I2C_CR1_PE); 822 823 hi2c->i2c_err = I2C_ERROR_NONE; 824 hi2c->i2c_state = I2C_STATE_READY; 825 826 rc = i2c_config_analog_filter(hi2c, init_data->analog_filter); 827 if (rc) 828 DMSG("I2C analog filter error %d", rc); 829 830 stm32_clock_disable(hi2c->clock); 831 832 return rc; 833 } 834 835 /* I2C transmit (TX) data register flush sequence */ 836 static void i2c_flush_txdr(struct i2c_handle_s *hi2c) 837 { 838 vaddr_t base = get_base(hi2c); 839 840 /* 841 * If a pending TXIS flag is set, 842 * write a dummy data in TXDR to clear it. 843 */ 844 if (io_read32(base + I2C_ISR) & I2C_ISR_TXIS) 845 io_write32(base + I2C_TXDR, 0); 846 847 /* Flush TX register if not empty */ 848 if ((io_read32(base + I2C_ISR) & I2C_ISR_TXE) == 0) 849 io_setbits32(base + I2C_ISR, I2C_ISR_TXE); 850 } 851 852 /* 853 * Wait for a single target I2C_ISR bit to reach an awaited value (0 or 1) 854 * 855 * @hi2c: I2C handle structure 856 * @bit_mask: Bit mask for the target single bit position to consider 857 * @awaited_value: Awaited value of the target bit in I2C_ISR, 0 or 1 858 * @timeout_ref: Expriation timeout reference 859 * Return 0 on success and a non-zero value on timeout 860 */ 861 static int wait_isr_event(struct i2c_handle_s *hi2c, uint32_t bit_mask, 862 unsigned int awaited_value, uint64_t timeout_ref) 863 { 864 vaddr_t isr = get_base(hi2c) + I2C_ISR; 865 866 assert(IS_POWER_OF_TWO(bit_mask) && !(awaited_value & ~1U)); 867 868 /* May timeout while TEE thread is suspended */ 869 while (!timeout_elapsed(timeout_ref)) 870 if (!!(io_read32(isr) & bit_mask) == awaited_value) 871 break; 872 873 if (!!(io_read32(isr) & bit_mask) == awaited_value) 874 return 0; 875 876 notif_i2c_timeout(hi2c); 877 return -1; 878 } 879 880 /* Handle Acknowledge-Failed sequence detection during an I2C Communication */ 881 static int i2c_ack_failed(struct i2c_handle_s *hi2c, uint64_t timeout_ref) 882 { 883 vaddr_t base = get_base(hi2c); 884 885 if ((io_read32(base + I2C_ISR) & I2C_ISR_NACKF) == 0U) 886 return 0; 887 888 /* 889 * Wait until STOP Flag is reset. Use polling method. 890 * AutoEnd should be initiate after AF. 891 * Timeout may elpased while TEE thread is suspended. 892 */ 893 while (!timeout_elapsed(timeout_ref)) 894 if (io_read32(base + I2C_ISR) & I2C_ISR_STOPF) 895 break; 896 897 if ((io_read32(base + I2C_ISR) & I2C_ISR_STOPF) == 0) { 898 notif_i2c_timeout(hi2c); 899 return -1; 900 } 901 902 io_write32(base + I2C_ICR, I2C_ISR_NACKF); 903 904 io_write32(base + I2C_ICR, I2C_ISR_STOPF); 905 906 i2c_flush_txdr(hi2c); 907 908 io_clrbits32(base + I2C_CR2, CR2_RESET_MASK); 909 910 hi2c->i2c_err |= I2C_ERROR_ACKF; 911 hi2c->i2c_state = I2C_STATE_READY; 912 913 return -1; 914 } 915 916 /* Wait TXIS bit is 1 in I2C_ISR register */ 917 static int i2c_wait_txis(struct i2c_handle_s *hi2c, uint64_t timeout_ref) 918 { 919 while (!timeout_elapsed(timeout_ref)) { 920 if (io_read32(get_base(hi2c) + I2C_ISR) & I2C_ISR_TXIS) 921 break; 922 if (i2c_ack_failed(hi2c, timeout_ref)) 923 return -1; 924 } 925 926 if (io_read32(get_base(hi2c) + I2C_ISR) & I2C_ISR_TXIS) 927 return 0; 928 929 if (i2c_ack_failed(hi2c, timeout_ref)) 930 return -1; 931 932 notif_i2c_timeout(hi2c); 933 return -1; 934 } 935 936 /* Wait STOPF bit is 1 in I2C_ISR register */ 937 static int i2c_wait_stop(struct i2c_handle_s *hi2c, uint64_t timeout_ref) 938 { 939 while (!timeout_elapsed(timeout_ref)) { 940 if (io_read32(get_base(hi2c) + I2C_ISR) & I2C_ISR_STOPF) 941 break; 942 943 if (i2c_ack_failed(hi2c, timeout_ref)) 944 return -1; 945 } 946 947 if (io_read32(get_base(hi2c) + I2C_ISR) & I2C_ISR_STOPF) 948 return 0; 949 950 if (i2c_ack_failed(hi2c, timeout_ref)) 951 return -1; 952 953 notif_i2c_timeout(hi2c); 954 return -1; 955 } 956 957 /* 958 * Load I2C_CR2 register for a I2C transfer 959 * 960 * @hi2c: I2C handle structure 961 * @dev_addr: Slave address to be transferred 962 * @size: Number of bytes to be transferred 963 * @i2c_mode: One of I2C_{RELOAD|AUTOEND|SOFTEND}_MODE: Enable Reload mode. 964 * @startstop: One of I2C_NO_STARTSTOP, I2C_GENERATE_STOP, 965 * I2C_GENERATE_START_{READ|WRITE} 966 */ 967 static void i2c_transfer_config(struct i2c_handle_s *hi2c, uint32_t dev_addr, 968 uint32_t size, uint32_t i2c_mode, 969 uint32_t startstop) 970 { 971 uint32_t clr_value = I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | 972 I2C_CR2_AUTOEND | I2C_CR2_START | I2C_CR2_STOP | 973 (I2C_CR2_RD_WRN & 974 (startstop >> (31U - I2C_CR2_RD_WRN_OFFSET))); 975 uint32_t set_value = (dev_addr & I2C_CR2_SADD) | 976 ((size << I2C_CR2_NBYTES_OFFSET) & 977 I2C_CR2_NBYTES) | 978 i2c_mode | startstop; 979 980 io_clrsetbits32(get_base(hi2c) + I2C_CR2, clr_value, set_value); 981 } 982 983 /* 984 * Master sends target device address followed by internal memory 985 * address for a memory write request. 986 * Function returns 0 on success or a negative value. 987 */ 988 static int i2c_request_mem_write(struct i2c_handle_s *hi2c, 989 struct i2c_request *request, 990 uint64_t timeout_ref) 991 { 992 vaddr_t base = get_base(hi2c); 993 994 i2c_transfer_config(hi2c, request->dev_addr, request->mem_addr_size, 995 I2C_RELOAD_MODE, I2C_GENERATE_START_WRITE); 996 997 if (i2c_wait_txis(hi2c, timeout_ref)) 998 return -1; 999 1000 if (request->mem_addr_size == I2C_MEMADD_SIZE_8BIT) { 1001 /* Send memory address */ 1002 io_write8(base + I2C_TXDR, request->mem_addr & 0x00FFU); 1003 } else { 1004 /* Send MSB of memory address */ 1005 io_write8(base + I2C_TXDR, (request->mem_addr & 0xFF00U) >> 8); 1006 1007 if (i2c_wait_txis(hi2c, timeout_ref)) 1008 return -1; 1009 1010 /* Send LSB of memory address */ 1011 io_write8(base + I2C_TXDR, request->mem_addr & 0x00FFU); 1012 } 1013 1014 if (wait_isr_event(hi2c, I2C_ISR_TCR, 1, timeout_ref)) 1015 return -1; 1016 1017 return 0; 1018 } 1019 1020 /* 1021 * Master sends target device address followed by internal memory 1022 * address to prepare a memory read request. 1023 * Function returns 0 on success or a negative value. 1024 */ 1025 static int i2c_request_mem_read(struct i2c_handle_s *hi2c, 1026 struct i2c_request *request, 1027 uint64_t timeout_ref) 1028 { 1029 vaddr_t base = get_base(hi2c); 1030 1031 i2c_transfer_config(hi2c, request->dev_addr, request->mem_addr_size, 1032 I2C_SOFTEND_MODE, I2C_GENERATE_START_WRITE); 1033 1034 if (i2c_wait_txis(hi2c, timeout_ref)) 1035 return -1; 1036 1037 if (request->mem_addr_size == I2C_MEMADD_SIZE_8BIT) { 1038 /* Send memory address */ 1039 io_write8(base + I2C_TXDR, request->mem_addr & 0x00FFU); 1040 } else { 1041 /* Send MSB of memory address */ 1042 io_write8(base + I2C_TXDR, (request->mem_addr & 0xFF00U) >> 8); 1043 1044 if (i2c_wait_txis(hi2c, timeout_ref)) 1045 return -1; 1046 1047 /* Send LSB of memory address */ 1048 io_write8(base + I2C_TXDR, request->mem_addr & 0x00FFU); 1049 } 1050 1051 if (wait_isr_event(hi2c, I2C_ISR_TC, 1, timeout_ref)) 1052 return -1; 1053 1054 return 0; 1055 } 1056 1057 /* 1058 * Write an amount of data in blocking mode 1059 * 1060 * @hi2c: Reference to struct i2c_handle_s 1061 * @request: I2C request parameters 1062 * @p_data: Pointer to data buffer 1063 * @size: Amount of data to be sent 1064 * Return 0 on success or a negative value 1065 */ 1066 static int i2c_write(struct i2c_handle_s *hi2c, struct i2c_request *request, 1067 uint8_t *p_data, uint16_t size) 1068 { 1069 uint64_t timeout_ref = 0; 1070 vaddr_t base = get_base(hi2c); 1071 int rc = -1; 1072 uint8_t *p_buff = p_data; 1073 size_t xfer_size = 0; 1074 size_t xfer_count = size; 1075 1076 if (request->mode != I2C_MODE_MASTER && request->mode != I2C_MODE_MEM) 1077 return -1; 1078 1079 if (hi2c->i2c_state != I2C_STATE_READY) 1080 return -1; 1081 1082 if (!p_data || !size) 1083 return -1; 1084 1085 stm32_clock_enable(hi2c->clock); 1086 1087 timeout_ref = timeout_init_us(I2C_TIMEOUT_BUSY_MS * 1000); 1088 if (wait_isr_event(hi2c, I2C_ISR_BUSY, 0, timeout_ref)) 1089 goto bail; 1090 1091 hi2c->i2c_state = I2C_STATE_BUSY_TX; 1092 hi2c->i2c_err = I2C_ERROR_NONE; 1093 timeout_ref = timeout_init_us(request->timeout_ms * 1000); 1094 1095 if (request->mode == I2C_MODE_MEM) { 1096 /* In memory mode, send slave address and memory address */ 1097 if (i2c_request_mem_write(hi2c, request, timeout_ref)) 1098 goto bail; 1099 1100 if (xfer_count > MAX_NBYTE_SIZE) { 1101 xfer_size = MAX_NBYTE_SIZE; 1102 i2c_transfer_config(hi2c, request->dev_addr, xfer_size, 1103 I2C_RELOAD_MODE, I2C_NO_STARTSTOP); 1104 } else { 1105 xfer_size = xfer_count; 1106 i2c_transfer_config(hi2c, request->dev_addr, xfer_size, 1107 I2C_AUTOEND_MODE, I2C_NO_STARTSTOP); 1108 } 1109 } else { 1110 /* In master mode, send slave address */ 1111 if (xfer_count > MAX_NBYTE_SIZE) { 1112 xfer_size = MAX_NBYTE_SIZE; 1113 i2c_transfer_config(hi2c, request->dev_addr, xfer_size, 1114 I2C_RELOAD_MODE, 1115 I2C_GENERATE_START_WRITE); 1116 } else { 1117 xfer_size = xfer_count; 1118 i2c_transfer_config(hi2c, request->dev_addr, xfer_size, 1119 I2C_AUTOEND_MODE, 1120 I2C_GENERATE_START_WRITE); 1121 } 1122 } 1123 1124 do { 1125 if (i2c_wait_txis(hi2c, timeout_ref)) 1126 goto bail; 1127 1128 io_write8(base + I2C_TXDR, *p_buff); 1129 p_buff++; 1130 xfer_count--; 1131 xfer_size--; 1132 1133 if (xfer_count && !xfer_size) { 1134 /* Wait until TCR flag is set */ 1135 if (wait_isr_event(hi2c, I2C_ISR_TCR, 1, timeout_ref)) 1136 goto bail; 1137 1138 if (xfer_count > MAX_NBYTE_SIZE) { 1139 xfer_size = MAX_NBYTE_SIZE; 1140 i2c_transfer_config(hi2c, request->dev_addr, 1141 xfer_size, 1142 I2C_RELOAD_MODE, 1143 I2C_NO_STARTSTOP); 1144 } else { 1145 xfer_size = xfer_count; 1146 i2c_transfer_config(hi2c, request->dev_addr, 1147 xfer_size, 1148 I2C_AUTOEND_MODE, 1149 I2C_NO_STARTSTOP); 1150 } 1151 } 1152 1153 } while (xfer_count > 0U); 1154 1155 /* 1156 * No need to Check TC flag, with AUTOEND mode the stop 1157 * is automatically generated. 1158 * Wait until STOPF flag is reset. 1159 */ 1160 if (i2c_wait_stop(hi2c, timeout_ref)) 1161 goto bail; 1162 1163 io_write32(base + I2C_ICR, I2C_ISR_STOPF); 1164 1165 io_clrbits32(base + I2C_CR2, CR2_RESET_MASK); 1166 1167 hi2c->i2c_state = I2C_STATE_READY; 1168 1169 rc = 0; 1170 1171 bail: 1172 stm32_clock_disable(hi2c->clock); 1173 1174 return rc; 1175 } 1176 1177 int stm32_i2c_mem_write(struct i2c_handle_s *hi2c, uint32_t dev_addr, 1178 uint32_t mem_addr, uint32_t mem_addr_size, 1179 uint8_t *p_data, size_t size, unsigned int timeout_ms) 1180 { 1181 struct i2c_request request = { 1182 .dev_addr = dev_addr, 1183 .mode = I2C_MODE_MEM, 1184 .mem_addr = mem_addr, 1185 .mem_addr_size = mem_addr_size, 1186 .timeout_ms = timeout_ms, 1187 }; 1188 1189 return i2c_write(hi2c, &request, p_data, size); 1190 } 1191 1192 int stm32_i2c_master_transmit(struct i2c_handle_s *hi2c, uint32_t dev_addr, 1193 uint8_t *p_data, size_t size, 1194 unsigned int timeout_ms) 1195 { 1196 struct i2c_request request = { 1197 .dev_addr = dev_addr, 1198 .mode = I2C_MODE_MASTER, 1199 .timeout_ms = timeout_ms, 1200 }; 1201 1202 return i2c_write(hi2c, &request, p_data, size); 1203 } 1204 1205 int stm32_i2c_read_write_membyte(struct i2c_handle_s *hi2c, uint16_t dev_addr, 1206 unsigned int mem_addr, uint8_t *p_data, 1207 bool write) 1208 { 1209 uint64_t timeout_ref = 0; 1210 uintptr_t base = get_base(hi2c); 1211 int rc = -1; 1212 uint8_t *p_buff = p_data; 1213 uint32_t event_mask = 0; 1214 1215 if (hi2c->i2c_state != I2C_STATE_READY || !p_data) 1216 return -1; 1217 1218 stm32_clock_enable(hi2c->clock); 1219 1220 timeout_ref = timeout_init_us(I2C_TIMEOUT_BUSY_US); 1221 if (wait_isr_event(hi2c, I2C_ISR_BUSY, 0, timeout_ref)) 1222 goto bail; 1223 1224 hi2c->i2c_state = write ? I2C_STATE_BUSY_TX : I2C_STATE_BUSY_RX; 1225 hi2c->i2c_err = I2C_ERROR_NONE; 1226 1227 i2c_transfer_config(hi2c, dev_addr, I2C_MEMADD_SIZE_8BIT, 1228 write ? I2C_RELOAD_MODE : I2C_SOFTEND_MODE, 1229 I2C_GENERATE_START_WRITE); 1230 1231 timeout_ref = timeout_init_us(I2C_TIMEOUT_BUSY_US); 1232 if (i2c_wait_txis(hi2c, timeout_ref)) 1233 goto bail; 1234 1235 io_write8(base + I2C_TXDR, mem_addr); 1236 1237 if (write) 1238 event_mask = I2C_ISR_TCR; 1239 else 1240 event_mask = I2C_ISR_TC; 1241 1242 timeout_ref = timeout_init_us(I2C_TIMEOUT_BUSY_US); 1243 if (wait_isr_event(hi2c, event_mask, 1, timeout_ref)) 1244 goto bail; 1245 1246 i2c_transfer_config(hi2c, dev_addr, I2C_MEMADD_SIZE_8BIT, 1247 I2C_AUTOEND_MODE, 1248 write ? I2C_NO_STARTSTOP : I2C_GENERATE_START_READ); 1249 1250 timeout_ref = timeout_init_us(I2C_TIMEOUT_BUSY_US); 1251 if (write) { 1252 if (i2c_wait_txis(hi2c, timeout_ref)) 1253 goto bail; 1254 1255 io_write8(base + I2C_TXDR, *p_buff); 1256 } else { 1257 if (wait_isr_event(hi2c, I2C_ISR_RXNE, 1, timeout_ref)) 1258 goto bail; 1259 1260 *p_buff = io_read8(base + I2C_RXDR); 1261 } 1262 1263 timeout_ref = timeout_init_us(I2C_TIMEOUT_BUSY_US); 1264 if (i2c_wait_stop(hi2c, timeout_ref)) 1265 goto bail; 1266 1267 io_write32(base + I2C_ICR, I2C_ISR_STOPF); 1268 io_clrbits32(base + I2C_CR2, CR2_RESET_MASK); 1269 1270 hi2c->i2c_state = I2C_STATE_READY; 1271 1272 rc = 0; 1273 1274 bail: 1275 stm32_clock_disable(hi2c->clock); 1276 1277 return rc; 1278 } 1279 1280 /* 1281 * Read an amount of data in blocking mode 1282 * 1283 * @hi2c: Reference to struct i2c_handle_s 1284 * @request: I2C request parameters 1285 * @p_data: Pointer to data buffer 1286 * @size: Amount of data to be sent 1287 * Return 0 on success or a negative value 1288 */ 1289 static int i2c_read(struct i2c_handle_s *hi2c, struct i2c_request *request, 1290 uint8_t *p_data, uint32_t size) 1291 { 1292 vaddr_t base = get_base(hi2c); 1293 uint64_t timeout_ref = 0; 1294 int rc = -1; 1295 uint8_t *p_buff = p_data; 1296 size_t xfer_count = size; 1297 size_t xfer_size = 0; 1298 1299 if (request->mode != I2C_MODE_MASTER && request->mode != I2C_MODE_MEM) 1300 return -1; 1301 1302 if (hi2c->i2c_state != I2C_STATE_READY) 1303 return -1; 1304 1305 if (!p_data || !size) 1306 return -1; 1307 1308 stm32_clock_enable(hi2c->clock); 1309 1310 timeout_ref = timeout_init_us(I2C_TIMEOUT_BUSY_MS * 1000); 1311 if (wait_isr_event(hi2c, I2C_ISR_BUSY, 0, timeout_ref)) 1312 goto bail; 1313 1314 hi2c->i2c_state = I2C_STATE_BUSY_RX; 1315 hi2c->i2c_err = I2C_ERROR_NONE; 1316 timeout_ref = timeout_init_us(request->timeout_ms * 1000); 1317 1318 if (request->mode == I2C_MODE_MEM) { 1319 /* Send memory address */ 1320 if (i2c_request_mem_read(hi2c, request, timeout_ref)) 1321 goto bail; 1322 } 1323 1324 /* 1325 * Send slave address. 1326 * Set NBYTES to write and reload if xfer_count > MAX_NBYTE_SIZE 1327 * and generate RESTART. 1328 */ 1329 if (xfer_count > MAX_NBYTE_SIZE) { 1330 xfer_size = MAX_NBYTE_SIZE; 1331 i2c_transfer_config(hi2c, request->dev_addr, xfer_size, 1332 I2C_RELOAD_MODE, I2C_GENERATE_START_READ); 1333 } else { 1334 xfer_size = xfer_count; 1335 i2c_transfer_config(hi2c, request->dev_addr, xfer_size, 1336 I2C_AUTOEND_MODE, I2C_GENERATE_START_READ); 1337 } 1338 1339 do { 1340 if (wait_isr_event(hi2c, I2C_ISR_RXNE, 1, timeout_ref)) 1341 goto bail; 1342 1343 *p_buff = io_read8(base + I2C_RXDR); 1344 p_buff++; 1345 xfer_size--; 1346 xfer_count--; 1347 1348 if (xfer_count && !xfer_size) { 1349 if (wait_isr_event(hi2c, I2C_ISR_TCR, 1, timeout_ref)) 1350 goto bail; 1351 1352 if (xfer_count > MAX_NBYTE_SIZE) { 1353 xfer_size = MAX_NBYTE_SIZE; 1354 i2c_transfer_config(hi2c, request->dev_addr, 1355 xfer_size, 1356 I2C_RELOAD_MODE, 1357 I2C_NO_STARTSTOP); 1358 } else { 1359 xfer_size = xfer_count; 1360 i2c_transfer_config(hi2c, request->dev_addr, 1361 xfer_size, 1362 I2C_AUTOEND_MODE, 1363 I2C_NO_STARTSTOP); 1364 } 1365 } 1366 } while (xfer_count > 0U); 1367 1368 /* 1369 * No need to Check TC flag, with AUTOEND mode the stop 1370 * is automatically generated. 1371 * Wait until STOPF flag is reset. 1372 */ 1373 if (i2c_wait_stop(hi2c, timeout_ref)) 1374 goto bail; 1375 1376 io_write32(base + I2C_ICR, I2C_ISR_STOPF); 1377 1378 io_clrbits32(base + I2C_CR2, CR2_RESET_MASK); 1379 1380 hi2c->i2c_state = I2C_STATE_READY; 1381 1382 rc = 0; 1383 1384 bail: 1385 stm32_clock_disable(hi2c->clock); 1386 1387 return rc; 1388 } 1389 1390 int stm32_i2c_mem_read(struct i2c_handle_s *hi2c, uint32_t dev_addr, 1391 uint32_t mem_addr, uint32_t mem_addr_size, 1392 uint8_t *p_data, size_t size, unsigned int timeout_ms) 1393 { 1394 struct i2c_request request = { 1395 .dev_addr = dev_addr, 1396 .mode = I2C_MODE_MEM, 1397 .mem_addr = mem_addr, 1398 .mem_addr_size = mem_addr_size, 1399 .timeout_ms = timeout_ms, 1400 }; 1401 1402 return i2c_read(hi2c, &request, p_data, size); 1403 } 1404 1405 int stm32_i2c_master_receive(struct i2c_handle_s *hi2c, uint32_t dev_addr, 1406 uint8_t *p_data, size_t size, 1407 unsigned int timeout_ms) 1408 { 1409 struct i2c_request request = { 1410 .dev_addr = dev_addr, 1411 .mode = I2C_MODE_MASTER, 1412 .timeout_ms = timeout_ms, 1413 }; 1414 1415 return i2c_read(hi2c, &request, p_data, size); 1416 } 1417 1418 bool stm32_i2c_is_device_ready(struct i2c_handle_s *hi2c, uint32_t dev_addr, 1419 unsigned int trials, unsigned int timeout_ms) 1420 { 1421 vaddr_t base = get_base(hi2c); 1422 unsigned int i2c_trials = 0U; 1423 bool rc = false; 1424 1425 if (hi2c->i2c_state != I2C_STATE_READY) 1426 return rc; 1427 1428 stm32_clock_enable(hi2c->clock); 1429 1430 if (io_read32(base + I2C_ISR) & I2C_ISR_BUSY) 1431 goto bail; 1432 1433 hi2c->i2c_state = I2C_STATE_BUSY; 1434 hi2c->i2c_err = I2C_ERROR_NONE; 1435 1436 do { 1437 uint64_t timeout_ref = 0; 1438 vaddr_t isr = base + I2C_ISR; 1439 1440 /* Generate Start */ 1441 if ((io_read32(base + I2C_OAR1) & I2C_OAR1_OA1MODE) == 0) 1442 io_write32(base + I2C_CR2, 1443 ((dev_addr & I2C_CR2_SADD) | 1444 I2C_CR2_START | I2C_CR2_AUTOEND) & 1445 ~I2C_CR2_RD_WRN); 1446 else 1447 io_write32(base + I2C_CR2, 1448 ((dev_addr & I2C_CR2_SADD) | 1449 I2C_CR2_START | I2C_CR2_ADD10) & 1450 ~I2C_CR2_RD_WRN); 1451 1452 /* 1453 * No need to Check TC flag, with AUTOEND mode the stop 1454 * is automatically generated. 1455 * Wait until STOPF flag is set or a NACK flag is set. 1456 */ 1457 timeout_ref = timeout_init_us(timeout_ms * 1000); 1458 while (!timeout_elapsed(timeout_ref)) 1459 if (io_read32(isr) & (I2C_ISR_STOPF | I2C_ISR_NACKF)) 1460 break; 1461 1462 if ((io_read32(isr) & (I2C_ISR_STOPF | I2C_ISR_NACKF)) == 0) { 1463 notif_i2c_timeout(hi2c); 1464 goto bail; 1465 } 1466 1467 if ((io_read32(base + I2C_ISR) & I2C_ISR_NACKF) == 0U) { 1468 if (wait_isr_event(hi2c, I2C_ISR_STOPF, 1, timeout_ref)) 1469 goto bail; 1470 1471 io_write32(base + I2C_ICR, I2C_ISR_STOPF); 1472 1473 hi2c->i2c_state = I2C_STATE_READY; 1474 1475 rc = true; 1476 goto bail; 1477 } 1478 1479 if (wait_isr_event(hi2c, I2C_ISR_STOPF, 1, timeout_ref)) 1480 goto bail; 1481 1482 io_write32(base + I2C_ICR, I2C_ISR_NACKF); 1483 io_write32(base + I2C_ICR, I2C_ISR_STOPF); 1484 1485 if (i2c_trials == trials) { 1486 io_setbits32(base + I2C_CR2, I2C_CR2_STOP); 1487 1488 if (wait_isr_event(hi2c, I2C_ISR_STOPF, 1, timeout_ref)) 1489 goto bail; 1490 1491 io_write32(base + I2C_ICR, I2C_ISR_STOPF); 1492 } 1493 1494 i2c_trials++; 1495 } while (i2c_trials < trials); 1496 1497 notif_i2c_timeout(hi2c); 1498 1499 bail: 1500 stm32_clock_disable(hi2c->clock); 1501 1502 return rc; 1503 } 1504 1505 void stm32_i2c_resume(struct i2c_handle_s *hi2c) 1506 { 1507 if (hi2c->i2c_state == I2C_STATE_READY) 1508 return; 1509 1510 if ((hi2c->i2c_state != I2C_STATE_RESET) && 1511 (hi2c->i2c_state != I2C_STATE_SUSPENDED)) 1512 panic(); 1513 1514 stm32_pinctrl_load_active_cfg(hi2c->pinctrl, hi2c->pinctrl_count); 1515 1516 if (hi2c->i2c_state == I2C_STATE_RESET) { 1517 /* There is no valid I2C configuration to be loaded yet */ 1518 return; 1519 } 1520 1521 restore_cfg(hi2c, &hi2c->sec_cfg); 1522 1523 hi2c->i2c_state = I2C_STATE_READY; 1524 } 1525 1526 void stm32_i2c_suspend(struct i2c_handle_s *hi2c) 1527 { 1528 if (hi2c->i2c_state == I2C_STATE_SUSPENDED) 1529 return; 1530 1531 if (hi2c->i2c_state != I2C_STATE_READY) 1532 panic(); 1533 1534 save_cfg(hi2c, &hi2c->sec_cfg); 1535 stm32_pinctrl_load_standby_cfg(hi2c->pinctrl, hi2c->pinctrl_count); 1536 1537 hi2c->i2c_state = I2C_STATE_SUSPENDED; 1538 } 1539