1 // SPDX-License-Identifier: BSD-3-Clause 2 /* 3 * Copyright (c) 2017-2023, STMicroelectronics 4 * 5 * STM32 GPIO driver is used as pin controller for stm32mp SoCs. 6 * The driver API is defined in header file stm32_gpio.h. 7 */ 8 9 #include <assert.h> 10 #include <drivers/clk.h> 11 #include <drivers/clk_dt.h> 12 #include <drivers/stm32_gpio.h> 13 #include <io.h> 14 #include <kernel/dt.h> 15 #include <kernel/boot.h> 16 #include <kernel/panic.h> 17 #include <kernel/spinlock.h> 18 #include <libfdt.h> 19 #include <mm/core_memprot.h> 20 #include <stdbool.h> 21 #include <stm32_util.h> 22 #include <sys/queue.h> 23 #include <trace.h> 24 #include <util.h> 25 26 #define GPIO_PIN_MAX 15 27 28 #define GPIO_MODER_OFFSET 0x00 29 #define GPIO_OTYPER_OFFSET 0x04 30 #define GPIO_OSPEEDR_OFFSET 0x08 31 #define GPIO_PUPDR_OFFSET 0x0c 32 #define GPIO_IDR_OFFSET 0x10 33 #define GPIO_ODR_OFFSET 0x14 34 #define GPIO_BSRR_OFFSET 0x18 35 #define GPIO_AFRL_OFFSET 0x20 36 #define GPIO_AFRH_OFFSET 0x24 37 #define GPIO_SECR_OFFSET 0x30 38 39 #define GPIO_ALT_LOWER_LIMIT 0x8 40 41 #define GPIO_MODE_MASK GENMASK_32(1, 0) 42 #define GPIO_OSPEED_MASK GENMASK_32(1, 0) 43 #define GPIO_PUPD_PULL_MASK GENMASK_32(1, 0) 44 #define GPIO_ALTERNATE_MASK GENMASK_32(3, 0) 45 46 #define DT_GPIO_BANK_SHIFT 12 47 #define DT_GPIO_BANK_MASK GENMASK_32(16, 12) 48 #define DT_GPIO_PIN_SHIFT 8 49 #define DT_GPIO_PIN_MASK GENMASK_32(11, 8) 50 #define DT_GPIO_MODE_MASK GENMASK_32(7, 0) 51 52 #define DT_GPIO_BANK_NAME0 "GPIOA" 53 54 /** 55 * struct stm32_gpio_bank - GPIO bank instance 56 * 57 * @base: base address of the GPIO controller registers. 58 * @clock: clock identifier. 59 * @ngpios: number of GPIOs. 60 * @bank_id: Id of the bank. 61 * @lock: lock protecting the GPIO bank access. 62 * @sec_support: True if bank supports pin security protection, otherwise false 63 * @seccfgr: Secure configuration register value. 64 * @link: Link in bank list 65 */ 66 struct stm32_gpio_bank { 67 vaddr_t base; 68 struct clk *clock; 69 unsigned int ngpios; 70 unsigned int bank_id; 71 unsigned int lock; 72 STAILQ_ENTRY(stm32_gpio_bank) link; 73 }; 74 75 static unsigned int gpio_lock; 76 77 static STAILQ_HEAD(, stm32_gpio_bank) bank_list = 78 STAILQ_HEAD_INITIALIZER(bank_list); 79 80 /* Save to output @cfg the current GPIO (@bank/@pin) configuration */ 81 static void get_gpio_cfg(uint32_t bank, uint32_t pin, struct gpio_cfg *cfg) 82 { 83 vaddr_t base = stm32_get_gpio_bank_base(bank); 84 struct clk *clk = stm32_get_gpio_bank_clk(bank); 85 86 clk_enable(clk); 87 88 /* 89 * Save GPIO configuration bits spread over the few bank registers. 90 * 1bit fields are accessed at bit position being the pin index. 91 * 2bit fields are accessed at bit position being twice the pin index. 92 * 4bit fields are accessed at bit position being fourth the pin index 93 * but accessed from 2 32bit registers at incremental addresses. 94 */ 95 cfg->mode = (io_read32(base + GPIO_MODER_OFFSET) >> (pin << 1)) & 96 GPIO_MODE_MASK; 97 98 cfg->otype = (io_read32(base + GPIO_OTYPER_OFFSET) >> pin) & 1; 99 100 cfg->ospeed = (io_read32(base + GPIO_OSPEEDR_OFFSET) >> (pin << 1)) & 101 GPIO_OSPEED_MASK; 102 103 cfg->pupd = (io_read32(base + GPIO_PUPDR_OFFSET) >> (pin << 1)) & 104 GPIO_PUPD_PULL_MASK; 105 106 cfg->od = (io_read32(base + GPIO_ODR_OFFSET) >> (pin << 1)) & 1; 107 108 if (pin < GPIO_ALT_LOWER_LIMIT) 109 cfg->af = (io_read32(base + GPIO_AFRL_OFFSET) >> (pin << 2)) & 110 GPIO_ALTERNATE_MASK; 111 else 112 cfg->af = (io_read32(base + GPIO_AFRH_OFFSET) >> 113 ((pin - GPIO_ALT_LOWER_LIMIT) << 2)) & 114 GPIO_ALTERNATE_MASK; 115 116 clk_disable(clk); 117 } 118 119 /* Apply GPIO (@bank/@pin) configuration described by @cfg */ 120 static void set_gpio_cfg(uint32_t bank, uint32_t pin, struct gpio_cfg *cfg) 121 { 122 vaddr_t base = stm32_get_gpio_bank_base(bank); 123 struct clk *clk = stm32_get_gpio_bank_clk(bank); 124 uint32_t exceptions = cpu_spin_lock_xsave(&gpio_lock); 125 126 clk_enable(clk); 127 128 /* Load GPIO MODE value, 2bit value shifted by twice the pin number */ 129 io_clrsetbits32(base + GPIO_MODER_OFFSET, 130 GPIO_MODE_MASK << (pin << 1), 131 cfg->mode << (pin << 1)); 132 133 /* Load GPIO Output TYPE value, 1bit shifted by pin number value */ 134 io_clrsetbits32(base + GPIO_OTYPER_OFFSET, BIT(pin), cfg->otype << pin); 135 136 /* Load GPIO Output Speed confguration, 2bit value */ 137 io_clrsetbits32(base + GPIO_OSPEEDR_OFFSET, 138 GPIO_OSPEED_MASK << (pin << 1), 139 cfg->ospeed << (pin << 1)); 140 141 /* Load GPIO pull configuration, 2bit value */ 142 io_clrsetbits32(base + GPIO_PUPDR_OFFSET, BIT(pin), 143 cfg->pupd << (pin << 1)); 144 145 /* Load pin mux Alternate Function configuration, 4bit value */ 146 if (pin < GPIO_ALT_LOWER_LIMIT) { 147 io_clrsetbits32(base + GPIO_AFRL_OFFSET, 148 GPIO_ALTERNATE_MASK << (pin << 2), 149 cfg->af << (pin << 2)); 150 } else { 151 size_t shift = (pin - GPIO_ALT_LOWER_LIMIT) << 2; 152 153 io_clrsetbits32(base + GPIO_AFRH_OFFSET, 154 GPIO_ALTERNATE_MASK << shift, 155 cfg->af << shift); 156 } 157 158 /* Load GPIO Output direction confuguration, 1bit */ 159 io_clrsetbits32(base + GPIO_ODR_OFFSET, BIT(pin), cfg->od << pin); 160 161 clk_disable(clk); 162 cpu_spin_unlock_xrestore(&gpio_lock, exceptions); 163 } 164 165 void stm32_pinctrl_load_active_cfg(struct stm32_pinctrl *pinctrl, size_t cnt) 166 { 167 size_t n = 0; 168 169 for (n = 0; n < cnt; n++) 170 set_gpio_cfg(pinctrl[n].bank, pinctrl[n].pin, 171 &pinctrl[n].active_cfg); 172 } 173 174 void stm32_pinctrl_load_standby_cfg(struct stm32_pinctrl *pinctrl, size_t cnt) 175 { 176 size_t n = 0; 177 178 for (n = 0; n < cnt; n++) 179 set_gpio_cfg(pinctrl[n].bank, pinctrl[n].pin, 180 &pinctrl[n].standby_cfg); 181 } 182 183 void stm32_pinctrl_store_standby_cfg(struct stm32_pinctrl *pinctrl, size_t cnt) 184 { 185 size_t n = 0; 186 187 for (n = 0; n < cnt; n++) 188 get_gpio_cfg(pinctrl[n].bank, pinctrl[n].pin, 189 &pinctrl[n].standby_cfg); 190 } 191 192 /* Panic if GPIO bank information from platform do not match DTB description */ 193 static void ckeck_gpio_bank(void *fdt, uint32_t bank, int pinctrl_node) 194 { 195 int pinctrl_subnode = 0; 196 197 fdt_for_each_subnode(pinctrl_subnode, fdt, pinctrl_node) { 198 TEE_Result res = TEE_ERROR_GENERIC; 199 const fdt32_t *cuint = NULL; 200 struct clk *clk = NULL; 201 202 if (fdt_getprop(fdt, pinctrl_subnode, 203 "gpio-controller", NULL) == NULL) 204 continue; 205 206 /* Check bank register offset matches platform assumptions */ 207 cuint = fdt_getprop(fdt, pinctrl_subnode, "reg", NULL); 208 if (fdt32_to_cpu(*cuint) != stm32_get_gpio_bank_offset(bank)) 209 continue; 210 211 /* Check bank clock matches platform assumptions */ 212 res = clk_dt_get_by_index(fdt, pinctrl_subnode, 0, &clk); 213 if (res || clk != stm32_get_gpio_bank_clk(bank)) 214 panic(); 215 216 /* Check controller is enabled */ 217 if (fdt_get_status(fdt, pinctrl_subnode) == DT_STATUS_DISABLED) 218 panic(); 219 220 return; 221 } 222 223 panic(); 224 } 225 226 /* Count pins described in the DT node and get related data if possible */ 227 static int get_pinctrl_from_fdt(void *fdt, int node, 228 struct stm32_pinctrl *pinctrl, size_t count) 229 { 230 const fdt32_t *cuint, *slewrate; 231 int len = 0; 232 int pinctrl_node = 0; 233 uint32_t i = 0; 234 uint32_t speed = GPIO_OSPEED_LOW; 235 uint32_t pull = GPIO_PUPD_NO_PULL; 236 size_t found = 0; 237 238 cuint = fdt_getprop(fdt, node, "pinmux", &len); 239 if (!cuint) 240 return -FDT_ERR_NOTFOUND; 241 242 pinctrl_node = fdt_parent_offset(fdt, fdt_parent_offset(fdt, node)); 243 if (pinctrl_node < 0) 244 return -FDT_ERR_NOTFOUND; 245 246 slewrate = fdt_getprop(fdt, node, "slew-rate", NULL); 247 if (slewrate) 248 speed = fdt32_to_cpu(*slewrate); 249 250 if (fdt_getprop(fdt, node, "bias-pull-up", NULL)) 251 pull = GPIO_PUPD_PULL_UP; 252 if (fdt_getprop(fdt, node, "bias-pull-down", NULL)) 253 pull = GPIO_PUPD_PULL_DOWN; 254 255 for (i = 0; i < ((uint32_t)len / sizeof(uint32_t)); i++) { 256 uint32_t pincfg = 0; 257 uint32_t bank = 0; 258 uint32_t pin = 0; 259 uint32_t mode = 0; 260 uint32_t alternate = 0; 261 bool opendrain = false; 262 263 pincfg = fdt32_to_cpu(*cuint); 264 cuint++; 265 266 bank = (pincfg & DT_GPIO_BANK_MASK) >> DT_GPIO_BANK_SHIFT; 267 268 pin = (pincfg & DT_GPIO_PIN_MASK) >> DT_GPIO_PIN_SHIFT; 269 270 mode = pincfg & DT_GPIO_MODE_MASK; 271 272 switch (mode) { 273 case 0: 274 mode = GPIO_MODE_INPUT; 275 break; 276 case 1: 277 case 2: 278 case 3: 279 case 4: 280 case 5: 281 case 6: 282 case 7: 283 case 8: 284 case 9: 285 case 10: 286 case 11: 287 case 12: 288 case 13: 289 case 14: 290 case 15: 291 case 16: 292 alternate = mode - 1U; 293 mode = GPIO_MODE_ALTERNATE; 294 break; 295 case 17: 296 mode = GPIO_MODE_ANALOG; 297 break; 298 default: 299 mode = GPIO_MODE_OUTPUT; 300 break; 301 } 302 303 if (fdt_getprop(fdt, node, "drive-open-drain", NULL)) 304 opendrain = true; 305 306 /* Check GPIO bank clock/base address against platform */ 307 ckeck_gpio_bank(fdt, bank, pinctrl_node); 308 309 if (found < count) { 310 struct stm32_pinctrl *ref = &pinctrl[found]; 311 312 ref->bank = (uint8_t)bank; 313 ref->pin = (uint8_t)pin; 314 ref->active_cfg.mode = mode; 315 ref->active_cfg.otype = opendrain ? 1 : 0; 316 ref->active_cfg.ospeed = speed; 317 ref->active_cfg.pupd = pull; 318 ref->active_cfg.od = 0; 319 ref->active_cfg.af = alternate; 320 /* Default to analog mode for standby state */ 321 ref->standby_cfg.mode = GPIO_MODE_ANALOG; 322 ref->standby_cfg.pupd = GPIO_PUPD_NO_PULL; 323 } 324 325 found++; 326 } 327 328 return (int)found; 329 } 330 331 /* Get bank ID from bank node property st,bank-name or panic on failure */ 332 static unsigned int dt_get_bank_id(const void *fdt, int node) 333 { 334 const int dt_name_len = strlen(DT_GPIO_BANK_NAME0); 335 const fdt32_t *cuint = NULL; 336 int len = 0; 337 338 /* Parse "st,bank-name" to get its id (eg: GPIOA -> 0) */ 339 cuint = fdt_getprop(fdt, node, "st,bank-name", &len); 340 if (!cuint || (len != dt_name_len + 1)) 341 panic("Missing/wrong st,bank-name property"); 342 343 if (strncmp((const char *)cuint, DT_GPIO_BANK_NAME0, dt_name_len - 1) || 344 strcmp((const char *)cuint, DT_GPIO_BANK_NAME0) < 0) 345 panic("Wrong st,bank-name property"); 346 347 return (unsigned int)strcmp((const char *)cuint, DT_GPIO_BANK_NAME0); 348 } 349 350 /* 351 * Return whether or not the GPIO bank related to a DT node is already 352 * registered in the GPIO bank link. 353 */ 354 static bool bank_is_registered(const void *fdt, int node) 355 { 356 unsigned int bank_id = dt_get_bank_id(fdt, node); 357 struct stm32_gpio_bank *bank = NULL; 358 359 STAILQ_FOREACH(bank, &bank_list, link) 360 if (bank->bank_id == bank_id) 361 return true; 362 363 return false; 364 } 365 366 /* Get GPIO bank information from the DT */ 367 static TEE_Result dt_stm32_gpio_bank(const void *fdt, int node, 368 const void *compat_data __unused, 369 int range_offset, 370 struct stm32_gpio_bank **out_bank) 371 { 372 TEE_Result res = TEE_ERROR_GENERIC; 373 struct stm32_gpio_bank *bank = NULL; 374 const fdt32_t *cuint = NULL; 375 struct io_pa_va pa_va = { }; 376 struct clk *clk = NULL; 377 size_t blen = 0; 378 paddr_t pa = 0; 379 int len = 0; 380 int i = 0; 381 382 assert(out_bank); 383 384 /* Probe deferrable devices first */ 385 res = clk_dt_get_by_index(fdt, node, 0, &clk); 386 if (res) 387 return res; 388 389 bank = calloc(1, sizeof(*bank)); 390 if (!bank) 391 return TEE_ERROR_OUT_OF_MEMORY; 392 393 /* 394 * Do not rely *only* on the "reg" property to get the address, 395 * but consider also the "ranges" translation property 396 */ 397 pa = fdt_reg_base_address(fdt, node); 398 if (pa == DT_INFO_INVALID_REG) 399 panic("missing reg property"); 400 401 pa_va.pa = pa + range_offset; 402 403 blen = fdt_reg_size(fdt, node); 404 if (blen == DT_INFO_INVALID_REG_SIZE) 405 panic("missing reg size property"); 406 407 DMSG("Bank name %s", fdt_get_name(fdt, node, NULL)); 408 bank->base = io_pa_or_va_secure(&pa_va, blen); 409 bank->bank_id = dt_get_bank_id(fdt, node); 410 bank->clock = clk; 411 412 /* Parse gpio-ranges with its 4 parameters */ 413 cuint = fdt_getprop(fdt, node, "gpio-ranges", &len); 414 len /= sizeof(*cuint); 415 if (len % 4) 416 panic("wrong gpio-ranges syntax"); 417 418 /* Get the last defined gpio line (offset + nb of pins) */ 419 for (i = 0; i < len / 4; i++) { 420 bank->ngpios = MAX(bank->ngpios, 421 (unsigned int)(fdt32_to_cpu(*(cuint + 1)) + 422 fdt32_to_cpu(*(cuint + 3)))); 423 cuint += 4; 424 } 425 426 *out_bank = bank; 427 return TEE_SUCCESS; 428 } 429 430 /* Parse a pinctrl node to register the GPIO banks it describes */ 431 static TEE_Result dt_stm32_gpio_pinctrl(const void *fdt, int node, 432 const void *compat_data) 433 { 434 TEE_Result res = TEE_SUCCESS; 435 const fdt32_t *cuint = NULL; 436 int range_offs = 0; 437 int b_node = 0; 438 int len = 0; 439 440 /* Read the ranges property (for regs memory translation) */ 441 cuint = fdt_getprop(fdt, node, "ranges", &len); 442 if (!cuint) 443 panic("missing ranges property"); 444 445 len /= sizeof(*cuint); 446 if (len == 3) 447 range_offs = fdt32_to_cpu(*(cuint + 1)) - fdt32_to_cpu(*cuint); 448 449 fdt_for_each_subnode(b_node, fdt, node) { 450 cuint = fdt_getprop(fdt, b_node, "gpio-controller", &len); 451 if (cuint) { 452 /* 453 * We found a property "gpio-controller" in the node: 454 * the node is a GPIO bank description, add it to the 455 * bank list. 456 */ 457 struct stm32_gpio_bank *bank = NULL; 458 459 if (fdt_get_status(fdt, b_node) == DT_STATUS_DISABLED || 460 bank_is_registered(fdt, b_node)) 461 continue; 462 463 res = dt_stm32_gpio_bank(fdt, b_node, compat_data, 464 range_offs, &bank); 465 if (res) 466 return res; 467 468 STAILQ_INSERT_TAIL(&bank_list, bank, link); 469 } else { 470 if (len != -FDT_ERR_NOTFOUND) 471 panic(); 472 } 473 } 474 475 return TEE_SUCCESS; 476 } 477 478 int stm32_pinctrl_fdt_get_pinctrl(void *fdt, int device_node, 479 struct stm32_pinctrl *pinctrl, size_t count) 480 { 481 const fdt32_t *cuint = NULL; 482 int lenp = 0; 483 int i = 0; 484 size_t found = 0; 485 486 cuint = fdt_getprop(fdt, device_node, "pinctrl-0", &lenp); 487 if (!cuint) 488 return -FDT_ERR_NOTFOUND; 489 490 for (i = 0; i < (lenp / 4); i++) { 491 int node = 0; 492 int subnode = 0; 493 494 node = fdt_node_offset_by_phandle(fdt, fdt32_to_cpu(*cuint)); 495 if (node < 0) 496 return -FDT_ERR_NOTFOUND; 497 498 fdt_for_each_subnode(subnode, fdt, node) { 499 size_t n = 0; 500 int rc = 0; 501 502 if (count > found) 503 n = count - found; 504 else 505 n = 0; 506 507 rc = get_pinctrl_from_fdt(fdt, subnode, 508 &pinctrl[found], n); 509 if (rc < 0) 510 return rc; 511 512 found += (size_t)rc; 513 } 514 515 cuint++; 516 } 517 518 return (int)found; 519 } 520 521 int stm32_get_gpio_count(void *fdt, int pinctrl_node, unsigned int bank) 522 { 523 int node = 0; 524 const fdt32_t *cuint = NULL; 525 526 fdt_for_each_subnode(node, fdt, pinctrl_node) { 527 if (!fdt_getprop(fdt, node, "gpio-controller", NULL)) 528 continue; 529 530 cuint = fdt_getprop(fdt, node, "reg", NULL); 531 if (!cuint) 532 continue; 533 534 if (fdt32_to_cpu(*cuint) != stm32_get_gpio_bank_offset(bank)) 535 continue; 536 537 cuint = fdt_getprop(fdt, node, "ngpios", NULL); 538 if (!cuint) 539 panic(); 540 541 return (int)fdt32_to_cpu(*cuint); 542 } 543 544 return -1; 545 } 546 547 static __maybe_unused bool valid_gpio_config(unsigned int bank, 548 unsigned int pin, bool input) 549 { 550 vaddr_t base = stm32_get_gpio_bank_base(bank); 551 uint32_t mode = (io_read32(base + GPIO_MODER_OFFSET) >> (pin << 1)) & 552 GPIO_MODE_MASK; 553 554 if (pin > GPIO_PIN_MAX) 555 return false; 556 557 if (input) 558 return mode == GPIO_MODE_INPUT; 559 else 560 return mode == GPIO_MODE_OUTPUT; 561 } 562 563 int stm32_gpio_get_input_level(unsigned int bank, unsigned int pin) 564 { 565 vaddr_t base = stm32_get_gpio_bank_base(bank); 566 struct clk *clk = stm32_get_gpio_bank_clk(bank); 567 int rc = 0; 568 569 clk_enable(clk); 570 571 assert(valid_gpio_config(bank, pin, true)); 572 573 if (io_read32(base + GPIO_IDR_OFFSET) == BIT(pin)) 574 rc = 1; 575 576 clk_disable(clk); 577 578 return rc; 579 } 580 581 void stm32_gpio_set_output_level(unsigned int bank, unsigned int pin, int level) 582 { 583 vaddr_t base = stm32_get_gpio_bank_base(bank); 584 struct clk *clk = stm32_get_gpio_bank_clk(bank); 585 586 clk_enable(clk); 587 588 assert(valid_gpio_config(bank, pin, false)); 589 590 if (level) 591 io_write32(base + GPIO_BSRR_OFFSET, BIT(pin)); 592 else 593 io_write32(base + GPIO_BSRR_OFFSET, BIT(pin + 16)); 594 595 clk_disable(clk); 596 } 597 598 void stm32_gpio_set_secure_cfg(unsigned int bank, unsigned int pin, bool secure) 599 { 600 vaddr_t base = stm32_get_gpio_bank_base(bank); 601 struct clk *clk = stm32_get_gpio_bank_clk(bank); 602 uint32_t exceptions = cpu_spin_lock_xsave(&gpio_lock); 603 604 clk_enable(clk); 605 606 if (secure) 607 io_setbits32(base + GPIO_SECR_OFFSET, BIT(pin)); 608 else 609 io_clrbits32(base + GPIO_SECR_OFFSET, BIT(pin)); 610 611 clk_disable(clk); 612 cpu_spin_unlock_xrestore(&gpio_lock, exceptions); 613 } 614 615 static TEE_Result stm32_pinctrl_probe(const void *fdt, int node, 616 const void *compat_data) 617 { 618 /* Register GPIO banks described in this pin control node */ 619 return dt_stm32_gpio_pinctrl(fdt, node, compat_data); 620 } 621 622 static const struct dt_device_match stm32_pinctrl_match_table[] = { 623 { .compatible = "st,stm32mp135-pinctrl" }, 624 { .compatible = "st,stm32mp157-pinctrl" }, 625 { .compatible = "st,stm32mp157-z-pinctrl" }, 626 { } 627 }; 628 629 DEFINE_DT_DRIVER(stm32_pinctrl_dt_driver) = { 630 .name = "stm32_gpio-pinctrl", 631 .type = DT_DRIVER_PINCTRL, 632 .match_table = stm32_pinctrl_match_table, 633 .probe = stm32_pinctrl_probe, 634 }; 635