1 // SPDX-License-Identifier: BSD-3-Clause 2 /* 3 * Copyright (c) 2017-2024, STMicroelectronics 4 * 5 * STM32 GPIO driver is used as pin controller for stm32mp SoCs. 6 */ 7 8 #include <assert.h> 9 #include <compiler.h> 10 #include <drivers/clk.h> 11 #include <drivers/clk_dt.h> 12 #include <drivers/gpio.h> 13 #include <drivers/pinctrl.h> 14 #include <drivers/stm32_gpio.h> 15 #include <drivers/stm32_rif.h> 16 #include <dt-bindings/gpio/gpio.h> 17 #include <io.h> 18 #include <kernel/dt.h> 19 #include <kernel/boot.h> 20 #include <kernel/panic.h> 21 #include <kernel/pm.h> 22 #include <kernel/spinlock.h> 23 #include <libfdt.h> 24 #include <mm/core_memprot.h> 25 #include <stdbool.h> 26 #include <stdint.h> 27 #include <stm32_util.h> 28 #include <sys/queue.h> 29 #include <trace.h> 30 #include <util.h> 31 32 #ifndef CFG_DRIVERS_GPIO 33 #error stm32_gpio driver expects CFG_DRIVERS_GPIO 34 #endif 35 36 #define GPIO_PIN_MAX 15 37 38 #define GPIO_MODER_OFFSET U(0x00) 39 #define GPIO_OTYPER_OFFSET U(0x04) 40 #define GPIO_OSPEEDR_OFFSET U(0x08) 41 #define GPIO_PUPDR_OFFSET U(0x0c) 42 #define GPIO_IDR_OFFSET U(0x10) 43 #define GPIO_ODR_OFFSET U(0x14) 44 #define GPIO_BSRR_OFFSET U(0x18) 45 #define GPIO_AFRL_OFFSET U(0x20) 46 #define GPIO_AFRH_OFFSET U(0x24) 47 #define GPIO_SECR_OFFSET U(0x30) 48 #define GPIO_PRIVCFGR_OFFSET U(0x34) 49 #define GPIO_RCFGLOCKR_OFFSET U(0x38) 50 #define GPIO_CIDCFGR(x) (U(0x50) + U(0x8) * (x)) 51 #define GPIO_SEMCR(x) (U(0x54) + U(0x8) * (x)) 52 53 #define GPIO_ALT_LOWER_LIMIT U(0x8) 54 55 /* 56 * CIDCFGR register bitfields 57 */ 58 #define GPIO_CIDCFGR_SEMWL_MASK GENMASK_32(23, 16) 59 #define GPIO_CIDCFGR_SCID_MASK GENMASK_32(6, 4) 60 #define GPIO_CIDCFGR_CONF_MASK (_CIDCFGR_CFEN | _CIDCFGR_SEMEN | \ 61 GPIO_CIDCFGR_SCID_MASK | \ 62 GPIO_CIDCFGR_SEMWL_MASK) 63 64 /* 65 * PRIVCFGR register bitfields 66 */ 67 #define GPIO_PRIVCFGR_MASK GENMASK_32(15, 0) 68 69 /* 70 * SECCFGR register bitfields 71 */ 72 #define GPIO_SECCFGR_MASK GENMASK_32(15, 0) 73 74 /* 75 * RCFGLOCKR register bitfields 76 */ 77 #define GPIO_RCFGLOCKR_MASK GENMASK_32(15, 0) 78 79 /* 80 * SEMCR register bitfields 81 */ 82 #define GPIO_SEMCR_SCID_M GENMASK_32(6, 4) 83 84 #define GPIO_MODE_MASK GENMASK_32(1, 0) 85 #define GPIO_OSPEED_MASK GENMASK_32(1, 0) 86 #define GPIO_PUPD_PULL_MASK GENMASK_32(1, 0) 87 #define GPIO_ALTERNATE_MASK GENMASK_32(3, 0) 88 89 #define DT_GPIO_BANK_SHIFT U(12) 90 #define DT_GPIO_BANK_MASK GENMASK_32(16, 12) 91 #define DT_GPIO_PIN_SHIFT U(8) 92 #define DT_GPIO_PIN_MASK GENMASK_32(11, 8) 93 #define DT_GPIO_MODE_MASK GENMASK_32(7, 0) 94 95 #define DT_GPIO_BANK_NAME0 "GPIOA" 96 97 #define GPIO_MODE_INPUT U(0x0) 98 #define GPIO_MODE_OUTPUT U(0x1) 99 #define GPIO_MODE_ALTERNATE U(0x2) 100 #define GPIO_MODE_ANALOG U(0x3) 101 102 #define GPIO_OTYPE_PUSH_PULL U(0x0) 103 #define GPIO_OTYPE_OPEN_DRAIN U(0x1) 104 105 #define GPIO_OSPEED_LOW U(0x0) 106 #define GPIO_OSPEED_MEDIUM U(0x1) 107 #define GPIO_OSPEED_HIGH U(0x2) 108 #define GPIO_OSPEED_VERY_HIGH U(0x3) 109 110 #define GPIO_PUPD_NO_PULL U(0x0) 111 #define GPIO_PUPD_PULL_UP U(0x1) 112 #define GPIO_PUPD_PULL_DOWN U(0x2) 113 114 #define GPIO_OD_LEVEL_LOW U(0x0) 115 #define GPIO_OD_LEVEL_HIGH U(0x1) 116 117 #define GPIO_MAX_CID_SUPPORTED U(3) 118 119 /* 120 * GPIO configuration description structured as single 16bit word 121 * for efficient save/restore when GPIO pin suspends or resumes. 122 * 123 * @mode: One of GPIO_MODE_* 124 * @otype: One of GPIO_OTYPE_* 125 * @ospeed: One of GPIO_OSPEED_* 126 * @pupd: One of GPIO_PUPD_* 127 * @od: One of GPIO_OD_* 128 * @af: Alternate function numerical ID between 0 and 15 129 */ 130 struct gpio_cfg { 131 uint16_t mode: 2; 132 uint16_t otype: 1; 133 uint16_t ospeed: 2; 134 uint16_t pupd: 2; 135 uint16_t od: 1; 136 uint16_t af: 4; 137 }; 138 139 /* 140 * Description of a pin and its muxing 141 * 142 * @bank: GPIO bank identifier as assigned by the platform 143 * @pin: Pin number in the GPIO bank 144 * @cfg: Pin configuration 145 */ 146 struct stm32_pinctrl { 147 uint8_t bank; 148 uint8_t pin; 149 struct gpio_cfg cfg; 150 }; 151 152 /* 153 * struct stm32_pinctrl_array - Array of pins in a pin control state 154 * @count: Number of cells in @pinctrl 155 * @pinctrl: Pin control configuration 156 */ 157 struct stm32_pinctrl_array { 158 size_t count; 159 struct stm32_pinctrl pinctrl[]; 160 }; 161 162 /** 163 * struct stm32_gpio_bank - GPIO bank instance 164 * 165 * @base: base address of the GPIO controller registers. 166 * @clock: clock identifier. 167 * @gpio_chip: GPIO chip reference for that GPIO bank 168 * @ngpios: number of GPIOs. 169 * @bank_id: Id of the bank. 170 * @lock: lock protecting the GPIO bank access. 171 * @rif_cfg: RIF configuration data 172 * @seccfgr: non-RIF bank secure configuration data 173 * @sec_support: True if bank supports pin security protection, else false 174 * @ready: True if configuration is applied, else false 175 * @is_tdcid: True if OP-TEE runs as Trusted Domain CID 176 * @link: Link in bank list 177 */ 178 struct stm32_gpio_bank { 179 vaddr_t base; 180 struct clk *clock; 181 struct gpio_chip gpio_chip; 182 unsigned int ngpios; 183 unsigned int bank_id; 184 unsigned int lock; 185 struct rif_conf_data *rif_cfg; 186 uint32_t seccfgr; 187 bool sec_support; 188 bool ready; 189 bool is_tdcid; 190 STAILQ_ENTRY(stm32_gpio_bank) link; 191 }; 192 193 /* 194 * struct stm32_gpio_pm_state - Consumed GPIO for PM purpose 195 * @gpio_pinctrl: Reference and configuration state for a consumed GPIO 196 * @level: GPIO level 197 * @link: Link in consumed GPIO list 198 */ 199 struct stm32_gpio_pm_state { 200 struct stm32_pinctrl gpio_pinctrl; 201 uint8_t level; 202 SLIST_ENTRY(stm32_gpio_pm_state) link; 203 }; 204 205 /** 206 * Compatibility information of supported banks 207 * 208 * @gpioz: True if bank is a GPIOZ bank 209 * @secure_control: Identify GPIO security bank capability. 210 * @secure_extended: Identify RIF presence. 211 */ 212 struct bank_compat { 213 bool gpioz; 214 bool secure_control; 215 bool secure_extended; 216 }; 217 218 static unsigned int gpio_lock; 219 220 static STAILQ_HEAD(, stm32_gpio_bank) bank_list = 221 STAILQ_HEAD_INITIALIZER(bank_list); 222 223 static SLIST_HEAD(, stm32_gpio_pm_state) consumed_gpios_head; 224 225 static bool is_stm32_gpio_chip(struct gpio_chip *chip); 226 227 static struct stm32_gpio_bank *gpio_chip_to_bank(struct gpio_chip *chip) 228 { 229 return container_of(chip, struct stm32_gpio_bank, gpio_chip); 230 } 231 232 static enum gpio_level stm32_gpio_get_level(struct gpio_chip *chip, 233 unsigned int gpio_pin) 234 { 235 struct stm32_gpio_bank *bank = gpio_chip_to_bank(chip); 236 enum gpio_level level = GPIO_LEVEL_HIGH; 237 unsigned int reg_offset = 0; 238 unsigned int mode = 0; 239 240 assert(gpio_pin < bank->ngpios); 241 242 if (clk_enable(bank->clock)) 243 panic(); 244 245 mode = (io_read32(bank->base + GPIO_MODER_OFFSET) >> (gpio_pin << 1)) & 246 GPIO_MODE_MASK; 247 248 switch (mode) { 249 case GPIO_MODE_INPUT: 250 reg_offset = GPIO_IDR_OFFSET; 251 break; 252 case GPIO_MODE_OUTPUT: 253 reg_offset = GPIO_ODR_OFFSET; 254 break; 255 default: 256 panic(); 257 } 258 259 if (io_read32(bank->base + reg_offset) & BIT(gpio_pin)) 260 level = GPIO_LEVEL_HIGH; 261 else 262 level = GPIO_LEVEL_LOW; 263 264 clk_disable(bank->clock); 265 266 return level; 267 } 268 269 static void stm32_gpio_set_level(struct gpio_chip *chip, unsigned int gpio_pin, 270 enum gpio_level level) 271 { 272 struct stm32_gpio_bank *bank = gpio_chip_to_bank(chip); 273 274 assert(gpio_pin < bank->ngpios); 275 276 if (clk_enable(bank->clock)) 277 panic(); 278 279 assert(((io_read32(bank->base + GPIO_MODER_OFFSET) >> 280 (gpio_pin << 1)) & GPIO_MODE_MASK) == GPIO_MODE_OUTPUT); 281 282 if (level == GPIO_LEVEL_HIGH) 283 io_write32(bank->base + GPIO_BSRR_OFFSET, BIT(gpio_pin)); 284 else 285 io_write32(bank->base + GPIO_BSRR_OFFSET, BIT(gpio_pin + 16)); 286 287 clk_disable(bank->clock); 288 } 289 290 static enum gpio_dir stm32_gpio_get_direction(struct gpio_chip *chip, 291 unsigned int gpio_pin) 292 { 293 struct stm32_gpio_bank *bank = gpio_chip_to_bank(chip); 294 uint32_t mode = 0; 295 296 assert(gpio_pin < bank->ngpios); 297 298 if (clk_enable(bank->clock)) 299 panic(); 300 301 mode = (io_read32(bank->base + GPIO_MODER_OFFSET) >> (gpio_pin << 1)) & 302 GPIO_MODE_MASK; 303 304 clk_disable(bank->clock); 305 306 switch (mode) { 307 case GPIO_MODE_INPUT: 308 return GPIO_DIR_IN; 309 case GPIO_MODE_OUTPUT: 310 return GPIO_DIR_OUT; 311 default: 312 panic(); 313 } 314 } 315 316 static void stm32_gpio_set_direction(struct gpio_chip *chip, 317 unsigned int gpio_pin, 318 enum gpio_dir direction) 319 { 320 struct stm32_gpio_bank *bank = gpio_chip_to_bank(chip); 321 uint32_t exceptions = 0; 322 uint32_t mode = 0; 323 324 assert(gpio_pin < bank->ngpios); 325 326 if (direction == GPIO_DIR_IN) 327 mode = GPIO_MODE_INPUT; 328 else 329 mode = GPIO_MODE_OUTPUT; 330 331 if (clk_enable(bank->clock)) 332 panic(); 333 exceptions = cpu_spin_lock_xsave(&gpio_lock); 334 io_clrsetbits32(bank->base + GPIO_MODER_OFFSET, 335 SHIFT_U32(GPIO_MODE_MASK, gpio_pin << 1), 336 SHIFT_U32(mode, gpio_pin << 1)); 337 cpu_spin_unlock_xrestore(&gpio_lock, exceptions); 338 clk_disable(bank->clock); 339 } 340 341 /* Forward reference to the PM callback handler for consumed GPIOs */ 342 static TEE_Result consumed_gpios_pm(enum pm_op op, unsigned int pm_hint, 343 const struct pm_callback_handle *pm_hdl); 344 345 static void stm32_gpio_put_gpio(struct gpio_chip *chip, struct gpio *gpio) 346 { 347 struct stm32_gpio_bank *bank = gpio_chip_to_bank(chip); 348 struct stm32_gpio_pm_state *tstate = NULL; 349 struct stm32_gpio_pm_state *state = NULL; 350 uint32_t exceptions = 0; 351 352 assert(is_stm32_gpio_chip(chip)); 353 354 exceptions = cpu_spin_lock_xsave(&gpio_lock); 355 356 SLIST_FOREACH_SAFE(state, &consumed_gpios_head, link, tstate) { 357 if (state->gpio_pinctrl.bank == bank->bank_id && 358 state->gpio_pinctrl.pin == gpio->pin) { 359 SLIST_REMOVE(&consumed_gpios_head, state, 360 stm32_gpio_pm_state, link); 361 unregister_pm_driver_cb(consumed_gpios_pm, state); 362 free(state); 363 free(gpio); 364 break; 365 } 366 } 367 assert(state); 368 369 cpu_spin_unlock_xrestore(&gpio_lock, exceptions); 370 } 371 372 static const struct gpio_ops stm32_gpio_ops = { 373 .get_direction = stm32_gpio_get_direction, 374 .set_direction = stm32_gpio_set_direction, 375 .get_value = stm32_gpio_get_level, 376 .set_value = stm32_gpio_set_level, 377 .put = stm32_gpio_put_gpio, 378 }; 379 380 static bool __maybe_unused is_stm32_gpio_chip(struct gpio_chip *chip) 381 { 382 return chip && chip->ops == &stm32_gpio_ops; 383 } 384 385 static struct stm32_gpio_bank *stm32_gpio_get_bank(unsigned int bank_id) 386 { 387 struct stm32_gpio_bank *bank = NULL; 388 389 STAILQ_FOREACH(bank, &bank_list, link) 390 if (bank_id == bank->bank_id) 391 return bank; 392 393 panic(); 394 } 395 396 /* Save to output @cfg the current GPIO (@bank_id/@pin) configuration */ 397 static void get_gpio_cfg(uint32_t bank_id, uint32_t pin, struct gpio_cfg *cfg) 398 { 399 struct stm32_gpio_bank *bank = stm32_gpio_get_bank(bank_id); 400 401 if (clk_enable(bank->clock)) 402 panic(); 403 404 /* 405 * Save GPIO configuration bits spread over the few bank registers. 406 * 1bit fields are accessed at bit position being the pin index. 407 * 2bit fields are accessed at bit position being twice the pin index. 408 * 4bit fields are accessed at bit position being fourth the pin index 409 * but accessed from 2 32bit registers at incremental addresses. 410 */ 411 cfg->mode = (io_read32(bank->base + GPIO_MODER_OFFSET) >> (pin << 1)) & 412 GPIO_MODE_MASK; 413 414 cfg->otype = (io_read32(bank->base + GPIO_OTYPER_OFFSET) >> pin) & 1; 415 416 cfg->ospeed = (io_read32(bank->base + GPIO_OSPEEDR_OFFSET) >> 417 (pin << 1)) & GPIO_OSPEED_MASK; 418 419 cfg->pupd = (io_read32(bank->base + GPIO_PUPDR_OFFSET) >> (pin << 1)) & 420 GPIO_PUPD_PULL_MASK; 421 422 cfg->od = (io_read32(bank->base + GPIO_ODR_OFFSET) >> (pin << 1)) & 1; 423 424 if (pin < GPIO_ALT_LOWER_LIMIT) 425 cfg->af = (io_read32(bank->base + GPIO_AFRL_OFFSET) >> 426 (pin << 2)) & GPIO_ALTERNATE_MASK; 427 else 428 cfg->af = (io_read32(bank->base + GPIO_AFRH_OFFSET) >> 429 ((pin - GPIO_ALT_LOWER_LIMIT) << 2)) & 430 GPIO_ALTERNATE_MASK; 431 432 clk_disable(bank->clock); 433 } 434 435 /* Apply GPIO (@bank/@pin) configuration described by @cfg */ 436 static void set_gpio_cfg(uint32_t bank_id, uint32_t pin, struct gpio_cfg *cfg) 437 { 438 struct stm32_gpio_bank *bank = stm32_gpio_get_bank(bank_id); 439 uint32_t exceptions = 0; 440 441 if (clk_enable(bank->clock)) 442 panic(); 443 exceptions = cpu_spin_lock_xsave(&gpio_lock); 444 445 /* Load GPIO MODE value, 2bit value shifted by twice the pin number */ 446 io_clrsetbits32(bank->base + GPIO_MODER_OFFSET, 447 SHIFT_U32(GPIO_MODE_MASK, pin << 1), 448 SHIFT_U32(cfg->mode, pin << 1)); 449 450 /* Load GPIO Output TYPE value, 1bit shifted by pin number value */ 451 io_clrsetbits32(bank->base + GPIO_OTYPER_OFFSET, BIT(pin), 452 SHIFT_U32(cfg->otype, pin)); 453 454 /* Load GPIO Output Speed confguration, 2bit value */ 455 io_clrsetbits32(bank->base + GPIO_OSPEEDR_OFFSET, 456 SHIFT_U32(GPIO_OSPEED_MASK, pin << 1), 457 SHIFT_U32(cfg->ospeed, pin << 1)); 458 459 /* Load GPIO pull configuration, 2bit value */ 460 io_clrsetbits32(bank->base + GPIO_PUPDR_OFFSET, BIT(pin), 461 SHIFT_U32(cfg->pupd, pin << 1)); 462 463 /* Load pin mux Alternate Function configuration, 4bit value */ 464 if (pin < GPIO_ALT_LOWER_LIMIT) { 465 io_clrsetbits32(bank->base + GPIO_AFRL_OFFSET, 466 SHIFT_U32(GPIO_ALTERNATE_MASK, pin << 2), 467 SHIFT_U32(cfg->af, pin << 2)); 468 } else { 469 size_t shift = (pin - GPIO_ALT_LOWER_LIMIT) << 2; 470 471 io_clrsetbits32(bank->base + GPIO_AFRH_OFFSET, 472 SHIFT_U32(GPIO_ALTERNATE_MASK, shift), 473 SHIFT_U32(cfg->af, shift)); 474 } 475 476 /* Load GPIO Output direction confuguration, 1bit */ 477 io_clrsetbits32(bank->base + GPIO_ODR_OFFSET, BIT(pin), cfg->od << pin); 478 479 cpu_spin_unlock_xrestore(&gpio_lock, exceptions); 480 clk_disable(bank->clock); 481 } 482 483 /* Count pins described in the DT node and get related data if possible */ 484 static int get_pinctrl_from_fdt(const void *fdt, int node, 485 struct stm32_pinctrl *pinctrl, size_t count) 486 { 487 const fdt32_t *cuint = NULL; 488 const fdt32_t *slewrate = NULL; 489 int len = 0; 490 uint32_t i = 0; 491 uint32_t speed = GPIO_OSPEED_LOW; 492 uint32_t pull = GPIO_PUPD_NO_PULL; 493 size_t found = 0; 494 495 cuint = fdt_getprop(fdt, node, "pinmux", &len); 496 if (!cuint) 497 return -FDT_ERR_NOTFOUND; 498 499 slewrate = fdt_getprop(fdt, node, "slew-rate", NULL); 500 if (slewrate) 501 speed = fdt32_to_cpu(*slewrate); 502 503 if (fdt_getprop(fdt, node, "bias-pull-up", NULL)) 504 pull = GPIO_PUPD_PULL_UP; 505 if (fdt_getprop(fdt, node, "bias-pull-down", NULL)) 506 pull = GPIO_PUPD_PULL_DOWN; 507 508 for (i = 0; i < ((uint32_t)len / sizeof(uint32_t)); i++) { 509 uint32_t pincfg = 0; 510 uint32_t bank = 0; 511 uint32_t pin = 0; 512 uint32_t mode = 0; 513 uint32_t alternate = 0; 514 uint32_t odata = 0; 515 bool opendrain = false; 516 517 pincfg = fdt32_to_cpu(*cuint); 518 cuint++; 519 520 bank = (pincfg & DT_GPIO_BANK_MASK) >> DT_GPIO_BANK_SHIFT; 521 522 pin = (pincfg & DT_GPIO_PIN_MASK) >> DT_GPIO_PIN_SHIFT; 523 524 mode = pincfg & DT_GPIO_MODE_MASK; 525 526 switch (mode) { 527 case 0: 528 mode = GPIO_MODE_INPUT; 529 break; 530 case 1: 531 case 2: 532 case 3: 533 case 4: 534 case 5: 535 case 6: 536 case 7: 537 case 8: 538 case 9: 539 case 10: 540 case 11: 541 case 12: 542 case 13: 543 case 14: 544 case 15: 545 case 16: 546 alternate = mode - 1U; 547 mode = GPIO_MODE_ALTERNATE; 548 break; 549 case 17: 550 mode = GPIO_MODE_ANALOG; 551 break; 552 default: 553 mode = GPIO_MODE_OUTPUT; 554 break; 555 } 556 557 if (fdt_getprop(fdt, node, "drive-open-drain", NULL)) 558 opendrain = true; 559 560 if (fdt_getprop(fdt, node, "output-high", NULL) && 561 mode == GPIO_MODE_INPUT) { 562 mode = GPIO_MODE_OUTPUT; 563 odata = 1; 564 } 565 566 if (fdt_getprop(fdt, node, "output-low", NULL) && 567 mode == GPIO_MODE_INPUT) { 568 mode = GPIO_MODE_OUTPUT; 569 odata = 0; 570 } 571 572 if (found < count) { 573 struct stm32_pinctrl *ref = &pinctrl[found]; 574 575 ref->bank = (uint8_t)bank; 576 ref->pin = (uint8_t)pin; 577 ref->cfg.mode = mode; 578 if (opendrain) 579 ref->cfg.otype = GPIO_OTYPE_OPEN_DRAIN; 580 else 581 ref->cfg.otype = GPIO_OTYPE_PUSH_PULL; 582 ref->cfg.ospeed = speed; 583 ref->cfg.pupd = pull; 584 ref->cfg.od = odata; 585 ref->cfg.af = alternate; 586 } 587 588 found++; 589 } 590 591 return (int)found; 592 } 593 594 static TEE_Result consumed_gpios_pm(enum pm_op op, 595 unsigned int pm_hint __unused, 596 const struct pm_callback_handle *pm_hdl) 597 { 598 struct stm32_gpio_pm_state *handle = pm_hdl->handle; 599 unsigned int bank_id = handle->gpio_pinctrl.bank; 600 unsigned int pin = handle->gpio_pinctrl.pin; 601 struct gpio_chip *chip = &stm32_gpio_get_bank(bank_id)->gpio_chip; 602 603 if (op == PM_OP_RESUME) { 604 set_gpio_cfg(bank_id, pin, &handle->gpio_pinctrl.cfg); 605 if (handle->gpio_pinctrl.cfg.mode == GPIO_MODE_OUTPUT) 606 stm32_gpio_set_level(chip, pin, handle->level); 607 } else { 608 get_gpio_cfg(bank_id, pin, &handle->gpio_pinctrl.cfg); 609 if (handle->gpio_pinctrl.cfg.mode == GPIO_MODE_OUTPUT) 610 handle->level = stm32_gpio_get_level(chip, pin); 611 } 612 613 return TEE_SUCCESS; 614 } 615 DECLARE_KEEP_PAGER(consumed_gpios_pm); 616 617 static TEE_Result stm32_gpio_get_dt(struct dt_pargs *pargs, void *data, 618 struct gpio **out_gpio) 619 { 620 TEE_Result res = TEE_ERROR_GENERIC; 621 struct stm32_gpio_pm_state *reg_state = NULL; 622 struct stm32_gpio_pm_state *state = NULL; 623 struct stm32_gpio_bank *bank = data; 624 struct gpio *gpio = NULL; 625 unsigned int shift_1b = 0; 626 unsigned int shift_2b = 0; 627 uint32_t exceptions = 0; 628 uint32_t otype = 0; 629 uint32_t pupd = 0; 630 uint32_t mode = 0; 631 632 res = gpio_dt_alloc_pin(pargs, &gpio); 633 if (res) 634 return res; 635 636 if (gpio->pin >= bank->ngpios) { 637 DMSG("Invalid GPIO reference"); 638 free(gpio); 639 return TEE_ERROR_GENERIC; 640 } 641 642 state = calloc(1, sizeof(*state)); 643 if (!state) { 644 free(gpio); 645 return TEE_ERROR_OUT_OF_MEMORY; 646 } 647 648 SLIST_FOREACH(reg_state, &consumed_gpios_head, link) { 649 if (reg_state->gpio_pinctrl.bank == bank->bank_id && 650 reg_state->gpio_pinctrl.pin == gpio->pin) { 651 EMSG("node %s: GPIO %c%u is used by another device", 652 fdt_get_name(pargs->fdt, pargs->consumer_node, 653 NULL), 654 bank->bank_id + 'A', gpio->pin); 655 free(state); 656 free(gpio); 657 return TEE_ERROR_GENERIC; 658 } 659 } 660 661 state->gpio_pinctrl.pin = gpio->pin; 662 state->gpio_pinctrl.bank = bank->bank_id; 663 SLIST_INSERT_HEAD(&consumed_gpios_head, state, link); 664 665 register_pm_driver_cb(consumed_gpios_pm, state, "stm32-gpio-state"); 666 667 shift_1b = gpio->pin; 668 shift_2b = SHIFT_U32(gpio->pin, 1); 669 670 if (gpio->dt_flags & GPIO_PULL_UP) 671 pupd = GPIO_PUPD_PULL_UP; 672 else if (gpio->dt_flags & GPIO_PULL_DOWN) 673 pupd = GPIO_PUPD_PULL_DOWN; 674 else 675 pupd = GPIO_PUPD_NO_PULL; 676 677 if (gpio->dt_flags & GPIO_LINE_OPEN_DRAIN) 678 otype = GPIO_OTYPE_OPEN_DRAIN; 679 else 680 otype = GPIO_OTYPE_PUSH_PULL; 681 682 if (clk_enable(bank->clock)) 683 panic(); 684 exceptions = cpu_spin_lock_xsave(&gpio_lock); 685 686 io_clrsetbits32(bank->base + GPIO_MODER_OFFSET, 687 SHIFT_U32(GPIO_MODE_MASK, shift_2b), 688 SHIFT_U32(mode, shift_2b)); 689 690 io_clrsetbits32(bank->base + GPIO_OTYPER_OFFSET, 691 SHIFT_U32(GPIO_OTYPE_OPEN_DRAIN, shift_1b), 692 SHIFT_U32(otype, shift_1b)); 693 694 io_clrsetbits32(bank->base + GPIO_PUPDR_OFFSET, 695 SHIFT_U32(GPIO_PUPD_PULL_MASK, shift_2b), 696 SHIFT_U32(pupd, shift_2b)); 697 698 cpu_spin_unlock_xrestore(&gpio_lock, exceptions); 699 clk_disable(bank->clock); 700 701 gpio->chip = &bank->gpio_chip; 702 703 *out_gpio = gpio; 704 705 return TEE_SUCCESS; 706 } 707 708 /* Get bank ID from bank node property st,bank-name or panic on failure */ 709 static unsigned int dt_get_bank_id(const void *fdt, int node) 710 { 711 const int dt_name_len = strlen(DT_GPIO_BANK_NAME0); 712 const fdt32_t *cuint = NULL; 713 int len = 0; 714 715 /* Parse "st,bank-name" to get its id (eg: GPIOA -> 0) */ 716 cuint = fdt_getprop(fdt, node, "st,bank-name", &len); 717 if (!cuint || (len != dt_name_len + 1)) 718 panic("Missing/wrong st,bank-name property"); 719 720 if (strncmp((const char *)cuint, DT_GPIO_BANK_NAME0, dt_name_len - 1) || 721 strcmp((const char *)cuint, DT_GPIO_BANK_NAME0) < 0) 722 panic("Wrong st,bank-name property"); 723 724 return (unsigned int)strcmp((const char *)cuint, DT_GPIO_BANK_NAME0); 725 } 726 727 /* 728 * Return whether or not the GPIO bank related to a DT node is already 729 * registered in the GPIO bank link. 730 */ 731 static bool bank_is_registered(const void *fdt, int node) 732 { 733 unsigned int bank_id = dt_get_bank_id(fdt, node); 734 struct stm32_gpio_bank *bank = NULL; 735 736 STAILQ_FOREACH(bank, &bank_list, link) 737 if (bank->bank_id == bank_id) 738 return true; 739 740 return false; 741 } 742 743 #ifdef CFG_STM32_RIF 744 static TEE_Result handle_available_semaphores(struct stm32_gpio_bank *bank, 745 uint32_t gpios_mask) 746 { 747 TEE_Result res = TEE_ERROR_GENERIC; 748 uint32_t cidcfgr = 0; 749 unsigned int i = 0; 750 751 for (i = 0 ; i < bank->ngpios; i++) { 752 if (!(BIT(i) & gpios_mask)) 753 continue; 754 755 cidcfgr = io_read32(bank->base + GPIO_CIDCFGR(i)); 756 757 if (!stm32_rif_semaphore_enabled_and_ok(cidcfgr, RIF_CID1)) 758 continue; 759 760 if (!(io_read32(bank->base + GPIO_SECR_OFFSET) & BIT(i))) { 761 res = stm32_rif_release_semaphore(bank->base + 762 GPIO_SEMCR(i), 763 MAX_CID_SUPPORTED); 764 if (res) { 765 EMSG("Cannot release semaphore for resource %u", 766 i); 767 return res; 768 } 769 } else { 770 res = stm32_rif_acquire_semaphore(bank->base + 771 GPIO_SEMCR(i), 772 MAX_CID_SUPPORTED); 773 if (res) { 774 EMSG("Cannot acquire semaphore for resource %u", 775 i); 776 return res; 777 } 778 } 779 } 780 781 return TEE_SUCCESS; 782 } 783 784 static TEE_Result apply_rif_config(struct stm32_gpio_bank *bank, 785 uint32_t gpios_mask) 786 { 787 TEE_Result res = TEE_ERROR_GENERIC; 788 unsigned int i = 0; 789 790 if (!bank->rif_cfg) 791 return TEE_SUCCESS; 792 793 if (clk_enable(bank->clock)) 794 panic(); 795 796 if (bank->is_tdcid) { 797 for (i = 0; i < bank->ngpios; i++) { 798 if (!(BIT(i) & gpios_mask)) 799 continue; 800 801 /* 802 * When TDCID, OP-TEE should be the one to set the CID 803 * filtering configuration. Clearing previous 804 * configuration prevents undesired events during the 805 * only legitimate configuration. 806 */ 807 io_clrbits32(bank->base + GPIO_CIDCFGR(i), 808 GPIO_CIDCFGR_CONF_MASK); 809 } 810 } else { 811 res = handle_available_semaphores(bank, gpios_mask); 812 if (res) 813 panic(); 814 } 815 816 /* Security and privilege RIF configuration */ 817 io_mask32(bank->base + GPIO_PRIVCFGR_OFFSET, 818 bank->rif_cfg->priv_conf[0], gpios_mask); 819 io_mask32(bank->base + GPIO_SECR_OFFSET, 820 bank->rif_cfg->sec_conf[0], gpios_mask); 821 822 if (!bank->is_tdcid) { 823 res = TEE_SUCCESS; 824 goto out; 825 } 826 827 for (i = 0; i < bank->ngpios; i++) { 828 if (!(BIT(i) & gpios_mask)) 829 continue; 830 831 io_clrsetbits32(bank->base + GPIO_CIDCFGR(i), 832 GPIO_CIDCFGR_CONF_MASK, 833 bank->rif_cfg->cid_confs[i]); 834 } 835 836 /* 837 * Lock RIF configuration if configured. This cannot be undone until 838 * next reset. 839 */ 840 io_setbits32(bank->base + GPIO_RCFGLOCKR_OFFSET, 841 bank->rif_cfg->lock_conf[0]); 842 843 res = handle_available_semaphores(bank, gpios_mask); 844 if (res) 845 panic(); 846 847 out: 848 if (IS_ENABLED(CFG_TEE_CORE_DEBUG)) { 849 /* Check that RIF config are applied, panic otherwise */ 850 if ((io_read32(bank->base + GPIO_PRIVCFGR_OFFSET) & 851 gpios_mask) != 852 (bank->rif_cfg->priv_conf[0] & gpios_mask)) { 853 EMSG("GPIO bank%c priv conf is incorrect", 854 'A' + bank->bank_id); 855 panic(); 856 } 857 858 if ((io_read32(bank->base + GPIO_SECR_OFFSET) & gpios_mask) != 859 (bank->rif_cfg->sec_conf[0] & gpios_mask)) { 860 EMSG("GPIO bank %c sec conf is incorrect", 861 'A' + bank->bank_id); 862 panic(); 863 } 864 } 865 866 clk_disable(bank->clock); 867 868 return res; 869 } 870 #else /* CFG_STM32_RIF */ 871 static TEE_Result apply_rif_config(struct stm32_gpio_bank *bank __unused, 872 uint32_t gpios_mask __unused) 873 { 874 return TEE_SUCCESS; 875 } 876 #endif /* CFG_STM32_RIF */ 877 878 static void stm32_gpio_save_rif_config(struct stm32_gpio_bank *bank) 879 { 880 size_t i = 0; 881 882 for (i = 0; i < bank->ngpios; i++) 883 bank->rif_cfg->cid_confs[i] = io_read32(bank->base + 884 GPIO_CIDCFGR(i)); 885 886 bank->rif_cfg->priv_conf[0] = io_read32(bank->base + 887 GPIO_PRIVCFGR_OFFSET); 888 bank->rif_cfg->sec_conf[0] = io_read32(bank->base + 889 GPIO_SECR_OFFSET); 890 bank->rif_cfg->lock_conf[0] = io_read32(bank->base + 891 GPIO_RCFGLOCKR_OFFSET); 892 } 893 894 static void stm32_parse_gpio_rif_conf(struct stm32_gpio_bank *bank, 895 const void *fdt, int node) 896 { 897 unsigned int i = 0; 898 unsigned int nb_rif_conf = 0; 899 int lenp = 0; 900 const fdt32_t *cuint = NULL; 901 902 cuint = fdt_getprop(fdt, node, "st,protreg", &lenp); 903 if (!cuint) { 904 DMSG("No RIF configuration available"); 905 return; 906 } 907 908 bank->rif_cfg = calloc(1, sizeof(*bank->rif_cfg)); 909 if (!bank->rif_cfg) 910 panic(); 911 912 bank->rif_cfg->sec_conf = calloc(1, sizeof(uint32_t)); 913 if (!bank->rif_cfg->sec_conf) 914 panic(); 915 916 nb_rif_conf = (unsigned int)(lenp / sizeof(uint32_t)); 917 assert(nb_rif_conf <= bank->ngpios); 918 919 bank->rif_cfg->cid_confs = calloc(bank->ngpios, sizeof(uint32_t)); 920 bank->rif_cfg->priv_conf = calloc(1, sizeof(uint32_t)); 921 bank->rif_cfg->lock_conf = calloc(1, sizeof(uint32_t)); 922 bank->rif_cfg->access_mask = calloc(1, sizeof(uint32_t)); 923 if (!bank->rif_cfg->cid_confs || !bank->rif_cfg->access_mask || 924 !bank->rif_cfg->priv_conf || !bank->rif_cfg->lock_conf) 925 panic("Missing memory capacity for GPIOS RIF configuration"); 926 927 for (i = 0; i < nb_rif_conf; i++) 928 stm32_rif_parse_cfg(fdt32_to_cpu(cuint[i]), bank->rif_cfg, 929 bank->ngpios); 930 } 931 932 /* Get GPIO bank information from the DT */ 933 static TEE_Result dt_stm32_gpio_bank(const void *fdt, int node, 934 const void *compat_data, 935 int range_offset, 936 struct stm32_gpio_bank **out_bank) 937 { 938 const struct bank_compat *compat = compat_data; 939 TEE_Result res = TEE_ERROR_GENERIC; 940 struct stm32_gpio_bank *bank = NULL; 941 const fdt32_t *cuint = NULL; 942 struct io_pa_va pa_va = { }; 943 struct clk *clk = NULL; 944 size_t blen = 0; 945 paddr_t pa = 0; 946 int len = 0; 947 int i = 0; 948 949 assert(out_bank); 950 951 /* Probe deferrable devices first */ 952 res = clk_dt_get_by_index(fdt, node, 0, &clk); 953 if (res) 954 return res; 955 956 bank = calloc(1, sizeof(*bank)); 957 if (!bank) 958 return TEE_ERROR_OUT_OF_MEMORY; 959 960 if (compat->secure_extended) { 961 res = stm32_rifsc_check_tdcid(&bank->is_tdcid); 962 if (res) { 963 free(bank); 964 return res; 965 } 966 } 967 968 /* 969 * Do not rely *only* on the "reg" property to get the address, 970 * but consider also the "ranges" translation property 971 */ 972 if (fdt_reg_info(fdt, node, &pa, &blen)) 973 panic("missing reg or reg size property"); 974 975 pa_va.pa = pa + range_offset; 976 977 DMSG("Bank name %s", fdt_get_name(fdt, node, NULL)); 978 bank->bank_id = dt_get_bank_id(fdt, node); 979 bank->clock = clk; 980 bank->gpio_chip.ops = &stm32_gpio_ops; 981 bank->sec_support = compat->secure_control; 982 983 /* Parse gpio-ranges with its 4 parameters */ 984 cuint = fdt_getprop(fdt, node, "gpio-ranges", &len); 985 len /= sizeof(*cuint); 986 if (len % 4) 987 panic("wrong gpio-ranges syntax"); 988 989 /* Get the last defined gpio line (offset + nb of pins) */ 990 for (i = 0; i < len / 4; i++) { 991 bank->ngpios = MAX(bank->ngpios, 992 (unsigned int)(fdt32_to_cpu(*(cuint + 1)) + 993 fdt32_to_cpu(*(cuint + 3)))); 994 cuint += 4; 995 } 996 997 if (compat->secure_extended) { 998 /* RIF configuration */ 999 bank->base = io_pa_or_va_secure(&pa_va, blen); 1000 1001 stm32_parse_gpio_rif_conf(bank, fdt, node); 1002 } else if (bank->sec_support) { 1003 /* Secure configuration */ 1004 bank->base = io_pa_or_va_secure(&pa_va, blen); 1005 cuint = fdt_getprop(fdt, node, "st,protreg", NULL); 1006 if (cuint) 1007 bank->seccfgr = fdt32_to_cpu(*cuint); 1008 else 1009 DMSG("GPIO bank %c assigned to non-secure", 1010 bank->bank_id + 'A'); 1011 } else { 1012 bank->base = io_pa_or_va_nsec(&pa_va, blen); 1013 } 1014 1015 if (compat->gpioz) 1016 stm32mp_register_gpioz_pin_count(bank->ngpios); 1017 1018 *out_bank = bank; 1019 1020 return TEE_SUCCESS; 1021 } 1022 1023 /* Parse a pinctrl node to register the GPIO banks it describes */ 1024 static TEE_Result dt_stm32_gpio_pinctrl(const void *fdt, int node, 1025 const void *compat_data) 1026 { 1027 TEE_Result res = TEE_SUCCESS; 1028 const fdt32_t *cuint = NULL; 1029 int range_offs = 0; 1030 int b_node = 0; 1031 int len = 0; 1032 1033 /* Read the ranges property (for regs memory translation) */ 1034 cuint = fdt_getprop(fdt, node, "ranges", &len); 1035 if (!cuint) 1036 panic("missing ranges property"); 1037 1038 len /= sizeof(*cuint); 1039 if (len == 3) 1040 range_offs = fdt32_to_cpu(*(cuint + 1)) - fdt32_to_cpu(*cuint); 1041 1042 fdt_for_each_subnode(b_node, fdt, node) { 1043 cuint = fdt_getprop(fdt, b_node, "gpio-controller", &len); 1044 if (cuint) { 1045 /* 1046 * We found a property "gpio-controller" in the node: 1047 * the node is a GPIO bank description, add it to the 1048 * bank list. 1049 */ 1050 struct stm32_gpio_bank *bank = NULL; 1051 1052 if (fdt_get_status(fdt, b_node) == DT_STATUS_DISABLED || 1053 bank_is_registered(fdt, b_node)) 1054 continue; 1055 1056 res = dt_stm32_gpio_bank(fdt, b_node, compat_data, 1057 range_offs, &bank); 1058 if (res) 1059 return res; 1060 1061 /* Registering a provider should not defer probe */ 1062 res = gpio_register_provider(fdt, b_node, 1063 stm32_gpio_get_dt, bank); 1064 if (res) 1065 panic(); 1066 1067 STAILQ_INSERT_TAIL(&bank_list, bank, link); 1068 } else { 1069 if (len != -FDT_ERR_NOTFOUND) 1070 panic(); 1071 } 1072 } 1073 1074 return TEE_SUCCESS; 1075 } 1076 1077 void stm32_gpio_set_secure_cfg(unsigned int bank_id, unsigned int pin, 1078 bool secure) 1079 { 1080 struct stm32_gpio_bank *bank = stm32_gpio_get_bank(bank_id); 1081 uint32_t exceptions = 0; 1082 1083 if (clk_enable(bank->clock)) 1084 panic(); 1085 exceptions = cpu_spin_lock_xsave(&gpio_lock); 1086 1087 if (secure) 1088 io_setbits32(bank->base + GPIO_SECR_OFFSET, BIT(pin)); 1089 else 1090 io_clrbits32(bank->base + GPIO_SECR_OFFSET, BIT(pin)); 1091 1092 cpu_spin_unlock_xrestore(&gpio_lock, exceptions); 1093 clk_disable(bank->clock); 1094 } 1095 1096 #ifdef CFG_DRIVERS_PINCTRL 1097 static TEE_Result stm32_pinctrl_conf_apply(struct pinconf *conf) 1098 { 1099 struct stm32_pinctrl_array *ref = conf->priv; 1100 struct stm32_pinctrl *p = ref->pinctrl; 1101 size_t pin_count = ref->count; 1102 size_t n = 0; 1103 1104 for (n = 0; n < pin_count; n++) 1105 set_gpio_cfg(p[n].bank, p[n].pin, &p[n].cfg); 1106 1107 return TEE_SUCCESS; 1108 } 1109 1110 static void stm32_pinctrl_conf_free(struct pinconf *conf) 1111 { 1112 free(conf); 1113 } 1114 1115 static const struct pinctrl_ops stm32_pinctrl_ops = { 1116 .conf_apply = stm32_pinctrl_conf_apply, 1117 .conf_free = stm32_pinctrl_conf_free, 1118 }; 1119 1120 DECLARE_KEEP_PAGER(stm32_pinctrl_ops); 1121 1122 void stm32_gpio_pinctrl_bank_pin(struct pinctrl_state *pinctrl, 1123 unsigned int *bank, unsigned int *pin, 1124 unsigned int *count) 1125 { 1126 size_t conf_index = 0; 1127 size_t pin_count = 0; 1128 size_t n = 0; 1129 1130 assert(count); 1131 if (!pinctrl) 1132 goto out; 1133 1134 for (conf_index = 0; conf_index < pinctrl->conf_count; conf_index++) { 1135 struct pinconf *pinconf = pinctrl->confs[conf_index]; 1136 struct stm32_pinctrl_array *ref = pinconf->priv; 1137 1138 /* Consider only the stm32_gpio pins */ 1139 if (pinconf->ops != &stm32_pinctrl_ops) 1140 continue; 1141 1142 if (bank || pin) { 1143 for (n = 0; n < ref->count; n++) { 1144 if (bank && pin_count < *count) 1145 bank[pin_count] = ref->pinctrl[n].bank; 1146 if (pin && pin_count < *count) 1147 pin[pin_count] = ref->pinctrl[n].pin; 1148 pin_count++; 1149 } 1150 } else { 1151 pin_count += ref->count; 1152 } 1153 } 1154 1155 out: 1156 *count = pin_count; 1157 } 1158 1159 void stm32_pinctrl_set_secure_cfg(struct pinctrl_state *pinctrl, bool secure) 1160 { 1161 size_t conf_index = 0; 1162 1163 if (!pinctrl) 1164 return; 1165 1166 for (conf_index = 0; conf_index < pinctrl->conf_count; conf_index++) { 1167 struct pinconf *pinconf = pinctrl->confs[conf_index]; 1168 struct stm32_pinctrl_array *ref = pinconf->priv; 1169 struct stm32_pinctrl *pc = NULL; 1170 size_t n = 0; 1171 1172 for (n = 0; n < ref->count; n++) { 1173 if (pinconf->ops != &stm32_pinctrl_ops) 1174 continue; 1175 1176 pc = ref->pinctrl + n; 1177 stm32_gpio_set_secure_cfg(pc->bank, pc->pin, secure); 1178 } 1179 } 1180 } 1181 1182 /* Allocate and return a pinctrl configuration from a DT reference */ 1183 static TEE_Result stm32_pinctrl_dt_get(struct dt_pargs *pargs, 1184 void *data __unused, 1185 struct pinconf **out_pinconf) 1186 { 1187 struct conf { 1188 struct pinconf pinconf; 1189 struct stm32_pinctrl_array array_ref; 1190 } *loc_conf = NULL; 1191 struct stm32_pinctrl *pinctrl = NULL; 1192 struct pinconf *pinconf = NULL; 1193 const void *fdt = NULL; 1194 size_t pin_count = 0; 1195 int pinctrl_node = 0; 1196 int pinmux_node = 0; 1197 int count = 0; 1198 1199 pinctrl_node = pargs->phandle_node; 1200 fdt = pargs->fdt; 1201 assert(fdt && pinctrl_node); 1202 1203 fdt_for_each_subnode(pinmux_node, fdt, pinctrl_node) { 1204 if (fdt_getprop(fdt, pinmux_node, "pinmux", &count)) 1205 pin_count += (size_t)count / sizeof(uint32_t); 1206 else if (count != -FDT_ERR_NOTFOUND) 1207 panic(); 1208 } 1209 1210 loc_conf = calloc(1, sizeof(*loc_conf) + sizeof(*pinctrl) * pin_count); 1211 if (!loc_conf) 1212 return TEE_ERROR_OUT_OF_MEMORY; 1213 1214 pinconf = &loc_conf->pinconf; 1215 pinconf->ops = &stm32_pinctrl_ops; 1216 pinconf->priv = &loc_conf->array_ref; 1217 1218 loc_conf->array_ref.count = pin_count; 1219 pinctrl = loc_conf->array_ref.pinctrl; 1220 1221 count = 0; 1222 fdt_for_each_subnode(pinmux_node, fdt, pinctrl_node) { 1223 int found = 0; 1224 1225 found = get_pinctrl_from_fdt(fdt, pinmux_node, pinctrl + count, 1226 pin_count - count); 1227 if (found <= 0 && found > ((int)pin_count - count)) { 1228 /* We can't recover from an error here so let's panic */ 1229 panic(); 1230 } 1231 1232 count += found; 1233 } 1234 1235 *out_pinconf = pinconf; 1236 1237 return TEE_SUCCESS; 1238 } 1239 #endif /*CFG_DRIVERS_PINCTRL*/ 1240 1241 static void stm32_gpio_get_conf_sec(struct stm32_gpio_bank *bank) 1242 { 1243 if (bank->sec_support) { 1244 clk_enable(bank->clock); 1245 bank->seccfgr = io_read32(bank->base + GPIO_SECR_OFFSET); 1246 clk_disable(bank->clock); 1247 } 1248 } 1249 1250 static void stm32_gpio_set_conf_sec(struct stm32_gpio_bank *bank) 1251 { 1252 if (bank->sec_support) { 1253 clk_enable(bank->clock); 1254 io_write32(bank->base + GPIO_SECR_OFFSET, bank->seccfgr); 1255 clk_disable(bank->clock); 1256 } 1257 } 1258 1259 static TEE_Result stm32_gpio_sec_config_resume(void) 1260 { 1261 TEE_Result res = TEE_ERROR_GENERIC; 1262 struct stm32_gpio_bank *bank = NULL; 1263 1264 STAILQ_FOREACH(bank, &bank_list, link) { 1265 if (bank->rif_cfg) { 1266 if (!bank->is_tdcid) 1267 continue; 1268 1269 bank->rif_cfg->access_mask[0] = GENMASK_32(bank->ngpios, 1270 0); 1271 1272 res = apply_rif_config(bank, 1273 bank->rif_cfg->access_mask[0]); 1274 if (res) { 1275 EMSG("Failed to set GPIO bank %c RIF config", 1276 'A' + bank->bank_id); 1277 return res; 1278 } 1279 } else { 1280 stm32_gpio_set_conf_sec(bank); 1281 } 1282 } 1283 1284 return TEE_SUCCESS; 1285 } 1286 1287 static TEE_Result stm32_gpio_sec_config_suspend(void) 1288 { 1289 struct stm32_gpio_bank *bank = NULL; 1290 1291 STAILQ_FOREACH(bank, &bank_list, link) { 1292 if (bank->rif_cfg) { 1293 if (bank->is_tdcid) 1294 stm32_gpio_save_rif_config(bank); 1295 } else { 1296 stm32_gpio_get_conf_sec(bank); 1297 } 1298 } 1299 1300 return TEE_SUCCESS; 1301 } 1302 1303 static TEE_Result 1304 stm32_gpio_sec_config_pm(enum pm_op op, unsigned int pm_hint, 1305 const struct pm_callback_handle *hdl __unused) 1306 { 1307 TEE_Result ret = TEE_ERROR_GENERIC; 1308 1309 if (!PM_HINT_IS_STATE(pm_hint, CONTEXT)) 1310 return TEE_SUCCESS; 1311 1312 if (op == PM_OP_RESUME) 1313 ret = stm32_gpio_sec_config_resume(); 1314 else 1315 ret = stm32_gpio_sec_config_suspend(); 1316 1317 return ret; 1318 } 1319 DECLARE_KEEP_PAGER(stm32_gpio_sec_config_pm); 1320 1321 /* 1322 * Several pinctrl nodes can be probed. Their bank will be put in the unique 1323 * bank_list. To avoid multiple configuration set for a bank when looping 1324 * over each bank in the bank list, ready is set to true when a bank is 1325 * configured. Therefore, during other bank probes, the configuration won't 1326 * be set again. 1327 */ 1328 static TEE_Result apply_sec_cfg(void) 1329 { 1330 TEE_Result res = TEE_ERROR_GENERIC; 1331 struct stm32_gpio_bank *bank = NULL; 1332 1333 STAILQ_FOREACH(bank, &bank_list, link) { 1334 if (bank->ready) 1335 continue; 1336 1337 if (bank->rif_cfg) { 1338 res = apply_rif_config(bank, 1339 bank->rif_cfg->access_mask[0]); 1340 if (res) { 1341 EMSG("Failed to set GPIO bank %c RIF config", 1342 'A' + bank->bank_id); 1343 STAILQ_REMOVE(&bank_list, bank, stm32_gpio_bank, 1344 link); 1345 free(bank); 1346 return res; 1347 } 1348 } else { 1349 stm32_gpio_set_conf_sec(bank); 1350 } 1351 1352 bank->ready = true; 1353 } 1354 1355 return TEE_SUCCESS; 1356 } 1357 1358 static TEE_Result stm32_pinctrl_probe(const void *fdt, int node, 1359 const void *compat_data) 1360 { 1361 static bool pm_register; 1362 TEE_Result res = TEE_ERROR_GENERIC; 1363 1364 /* Register GPIO banks described in this pin control node */ 1365 res = dt_stm32_gpio_pinctrl(fdt, node, compat_data); 1366 if (res) 1367 return res; 1368 1369 if (STAILQ_EMPTY(&bank_list)) 1370 DMSG("no gpio bank for that driver"); 1371 else if (apply_sec_cfg()) 1372 panic(); 1373 1374 if (!pm_register) { 1375 /* 1376 * Register to PM once for all probed banks to restore 1377 * their secure configuration. 1378 */ 1379 register_pm_driver_cb(stm32_gpio_sec_config_pm, NULL, 1380 "stm32-gpio-secure-config"); 1381 pm_register = true; 1382 } 1383 1384 #ifdef CFG_DRIVERS_PINCTRL 1385 res = pinctrl_register_provider(fdt, node, stm32_pinctrl_dt_get, 1386 (void *)compat_data); 1387 if (res) 1388 panic(); 1389 #endif 1390 1391 return TEE_SUCCESS; 1392 } 1393 1394 static const struct dt_device_match stm32_pinctrl_match_table[] = { 1395 { 1396 .compatible = "st,stm32mp135-pinctrl", 1397 .compat_data = &(struct bank_compat){ 1398 .secure_control = true, 1399 .secure_extended = false, 1400 }, 1401 }, 1402 { 1403 .compatible = "st,stm32mp157-pinctrl", 1404 .compat_data = &(struct bank_compat){ 1405 .secure_control = false, 1406 .secure_extended = false, 1407 }, 1408 }, 1409 { 1410 .compatible = "st,stm32mp157-z-pinctrl", 1411 .compat_data = &(struct bank_compat){ 1412 .gpioz = true, 1413 .secure_control = true, 1414 .secure_extended = false, 1415 }, 1416 }, 1417 { 1418 .compatible = "st,stm32mp257-pinctrl", 1419 .compat_data = &(struct bank_compat){ 1420 .secure_control = true, 1421 .secure_extended = true, 1422 }, 1423 }, 1424 { 1425 .compatible = "st,stm32mp257-z-pinctrl", 1426 .compat_data = &(struct bank_compat){ 1427 .gpioz = true, 1428 .secure_control = true, 1429 .secure_extended = true, 1430 }, 1431 }, 1432 { } 1433 }; 1434 1435 DEFINE_DT_DRIVER(stm32_pinctrl_dt_driver) = { 1436 .name = "stm32_gpio-pinctrl", 1437 .type = DT_DRIVER_PINCTRL, 1438 .match_table = stm32_pinctrl_match_table, 1439 .probe = stm32_pinctrl_probe, 1440 }; 1441