1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2016 IBM Corp. 4 */ 5 6 #include <linux/mfd/syscon.h> 7 #include <linux/platform_device.h> 8 #include <linux/slab.h> 9 #include <linux/string.h> 10 #include "../core.h" 11 #include "pinctrl-aspeed.h" 12 13 static const char *const aspeed_pinmux_ips[] = { 14 [ASPEED_IP_SCU] = "SCU", 15 [ASPEED_IP_GFX] = "GFX", 16 [ASPEED_IP_LPC] = "LPC", 17 }; 18 19 int aspeed_pinctrl_get_groups_count(struct pinctrl_dev *pctldev) 20 { 21 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev); 22 23 return pdata->ngroups; 24 } 25 26 const char *aspeed_pinctrl_get_group_name(struct pinctrl_dev *pctldev, 27 unsigned int group) 28 { 29 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev); 30 31 return pdata->groups[group].name; 32 } 33 34 int aspeed_pinctrl_get_group_pins(struct pinctrl_dev *pctldev, 35 unsigned int group, const unsigned int **pins, 36 unsigned int *npins) 37 { 38 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev); 39 40 *pins = &pdata->groups[group].pins[0]; 41 *npins = pdata->groups[group].npins; 42 43 return 0; 44 } 45 46 void aspeed_pinctrl_pin_dbg_show(struct pinctrl_dev *pctldev, 47 struct seq_file *s, unsigned int offset) 48 { 49 seq_printf(s, " %s", dev_name(pctldev->dev)); 50 } 51 52 int aspeed_pinmux_get_fn_count(struct pinctrl_dev *pctldev) 53 { 54 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev); 55 56 return pdata->nfunctions; 57 } 58 59 const char *aspeed_pinmux_get_fn_name(struct pinctrl_dev *pctldev, 60 unsigned int function) 61 { 62 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev); 63 64 return pdata->functions[function].name; 65 } 66 67 int aspeed_pinmux_get_fn_groups(struct pinctrl_dev *pctldev, 68 unsigned int function, 69 const char * const **groups, 70 unsigned int * const num_groups) 71 { 72 struct aspeed_pinctrl_data *pdata = pinctrl_dev_get_drvdata(pctldev); 73 74 *groups = pdata->functions[function].groups; 75 *num_groups = pdata->functions[function].ngroups; 76 77 return 0; 78 } 79 80 static inline void aspeed_sig_desc_print_val( 81 const struct aspeed_sig_desc *desc, bool enable, u32 rv) 82 { 83 pr_debug("Want %s%X[0x%08X]=0x%X, got 0x%X from 0x%08X\n", 84 aspeed_pinmux_ips[desc->ip], desc->reg, 85 desc->mask, enable ? desc->enable : desc->disable, 86 (rv & desc->mask) >> __ffs(desc->mask), rv); 87 } 88 89 /** 90 * Query the enabled or disabled state of a signal descriptor 91 * 92 * @desc: The signal descriptor of interest 93 * @enabled: True to query the enabled state, false to query disabled state 94 * @map: The IP block's regmap instance 95 * 96 * Return: 1 if the descriptor's bitfield is configured to the state 97 * selected by @enabled, 0 if not, and less than zero if an unrecoverable 98 * failure occurred 99 * 100 * Evaluation of descriptor state is non-trivial in that it is not a binary 101 * outcome: The bitfields can be greater than one bit in size and thus can take 102 * a value that is neither the enabled nor disabled state recorded in the 103 * descriptor (typically this means a different function to the one of interest 104 * is enabled). Thus we must explicitly test for either condition as required. 105 */ 106 static int aspeed_sig_desc_eval(const struct aspeed_sig_desc *desc, 107 bool enabled, struct regmap *map) 108 { 109 int ret; 110 unsigned int raw; 111 u32 want; 112 113 if (!map) 114 return -ENODEV; 115 116 ret = regmap_read(map, desc->reg, &raw); 117 if (ret) 118 return ret; 119 120 aspeed_sig_desc_print_val(desc, enabled, raw); 121 want = enabled ? desc->enable : desc->disable; 122 123 return ((raw & desc->mask) >> __ffs(desc->mask)) == want; 124 } 125 126 /** 127 * Query the enabled or disabled state for a mux function's signal on a pin 128 * 129 * @expr: An expression controlling the signal for a mux function on a pin 130 * @enabled: True to query the enabled state, false to query disabled state 131 * @maps: The list of regmap instances 132 * 133 * Return: 1 if the expression composed by @enabled evaluates true, 0 if not, 134 * and less than zero if an unrecoverable failure occurred. 135 * 136 * A mux function is enabled or disabled if the function's signal expression 137 * for each pin in the function's pin group evaluates true for the desired 138 * state. An signal expression evaluates true if all of its associated signal 139 * descriptors evaluate true for the desired state. 140 * 141 * If an expression's state is described by more than one bit, either through 142 * multi-bit bitfields in a single signal descriptor or through multiple signal 143 * descriptors of a single bit then it is possible for the expression to be in 144 * neither the enabled nor disabled state. Thus we must explicitly test for 145 * either condition as required. 146 */ 147 static int aspeed_sig_expr_eval(const struct aspeed_sig_expr *expr, 148 bool enabled, struct regmap * const *maps) 149 { 150 int i; 151 int ret; 152 153 for (i = 0; i < expr->ndescs; i++) { 154 const struct aspeed_sig_desc *desc = &expr->descs[i]; 155 156 ret = aspeed_sig_desc_eval(desc, enabled, maps[desc->ip]); 157 if (ret <= 0) 158 return ret; 159 } 160 161 return 1; 162 } 163 164 /** 165 * Configure a pin's signal by applying an expression's descriptor state for 166 * all descriptors in the expression. 167 * 168 * @expr: The expression associated with the function whose signal is to be 169 * configured 170 * @enable: true to enable an function's signal through a pin's signal 171 * expression, false to disable the function's signal 172 * @maps: The list of regmap instances for pinmux register access. 173 * 174 * Return: 0 if the expression is configured as requested and a negative error 175 * code otherwise 176 */ 177 static int aspeed_sig_expr_set(const struct aspeed_sig_expr *expr, 178 bool enable, struct regmap * const *maps) 179 { 180 int ret; 181 int i; 182 183 for (i = 0; i < expr->ndescs; i++) { 184 const struct aspeed_sig_desc *desc = &expr->descs[i]; 185 u32 pattern = enable ? desc->enable : desc->disable; 186 u32 val = (pattern << __ffs(desc->mask)); 187 188 if (!maps[desc->ip]) 189 return -ENODEV; 190 191 /* 192 * Strap registers are configured in hardware or by early-boot 193 * firmware. Treat them as read-only despite that we can write 194 * them. This may mean that certain functions cannot be 195 * deconfigured and is the reason we re-evaluate after writing 196 * all descriptor bits. 197 * 198 * Port D and port E GPIO loopback modes are the only exception 199 * as those are commonly used with front-panel buttons to allow 200 * normal operation of the host when the BMC is powered off or 201 * fails to boot. Once the BMC has booted, the loopback mode 202 * must be disabled for the BMC to control host power-on and 203 * reset. 204 */ 205 if (desc->ip == ASPEED_IP_SCU && desc->reg == HW_STRAP1 && 206 !(desc->mask & (BIT(21) | BIT(22)))) 207 continue; 208 209 if (desc->ip == ASPEED_IP_SCU && desc->reg == HW_STRAP2) 210 continue; 211 212 /* On AST2500, Set bits in SCU7C are cleared from SCU70 */ 213 if (desc->ip == ASPEED_IP_SCU && desc->reg == HW_STRAP1) { 214 unsigned int rev_id; 215 216 ret = regmap_read(maps[ASPEED_IP_SCU], 217 HW_REVISION_ID, &rev_id); 218 if (ret < 0) 219 return ret; 220 221 if (0x04 == (rev_id >> 24)) { 222 u32 value = ~val & desc->mask; 223 224 if (value) { 225 ret = regmap_write(maps[desc->ip], 226 HW_REVISION_ID, value); 227 if (ret < 0) 228 return ret; 229 } 230 } 231 } 232 233 ret = regmap_update_bits(maps[desc->ip], desc->reg, 234 desc->mask, val); 235 236 if (ret) 237 return ret; 238 } 239 240 ret = aspeed_sig_expr_eval(expr, enable, maps); 241 if (ret < 0) 242 return ret; 243 244 if (!ret) 245 return -EPERM; 246 247 return 0; 248 } 249 250 static int aspeed_sig_expr_enable(const struct aspeed_sig_expr *expr, 251 struct regmap * const *maps) 252 { 253 int ret; 254 255 ret = aspeed_sig_expr_eval(expr, true, maps); 256 if (ret < 0) 257 return ret; 258 259 if (!ret) 260 return aspeed_sig_expr_set(expr, true, maps); 261 262 return 0; 263 } 264 265 static int aspeed_sig_expr_disable(const struct aspeed_sig_expr *expr, 266 struct regmap * const *maps) 267 { 268 int ret; 269 270 ret = aspeed_sig_expr_eval(expr, true, maps); 271 if (ret < 0) 272 return ret; 273 274 if (ret) 275 return aspeed_sig_expr_set(expr, false, maps); 276 277 return 0; 278 } 279 280 /** 281 * Disable a signal on a pin by disabling all provided signal expressions. 282 * 283 * @exprs: The list of signal expressions (from a priority level on a pin) 284 * @maps: The list of regmap instances for pinmux register access. 285 * 286 * Return: 0 if all expressions are disabled, otherwise a negative error code 287 */ 288 static int aspeed_disable_sig(const struct aspeed_sig_expr **exprs, 289 struct regmap * const *maps) 290 { 291 int ret = 0; 292 293 if (!exprs) 294 return true; 295 296 while (*exprs && !ret) { 297 ret = aspeed_sig_expr_disable(*exprs, maps); 298 exprs++; 299 } 300 301 return ret; 302 } 303 304 /** 305 * Search for the signal expression needed to enable the pin's signal for the 306 * requested function. 307 * 308 * @exprs: List of signal expressions (haystack) 309 * @name: The name of the requested function (needle) 310 * 311 * Return: A pointer to the signal expression whose function tag matches the 312 * provided name, otherwise NULL. 313 * 314 */ 315 static const struct aspeed_sig_expr *aspeed_find_expr_by_name( 316 const struct aspeed_sig_expr **exprs, const char *name) 317 { 318 while (*exprs) { 319 if (strcmp((*exprs)->function, name) == 0) 320 return *exprs; 321 exprs++; 322 } 323 324 return NULL; 325 } 326 327 static char *get_defined_attribute(const struct aspeed_pin_desc *pdesc, 328 const char *(*get)( 329 const struct aspeed_sig_expr *)) 330 { 331 char *found = NULL; 332 size_t len = 0; 333 const struct aspeed_sig_expr ***prios, **funcs, *expr; 334 335 prios = pdesc->prios; 336 337 while ((funcs = *prios)) { 338 while ((expr = *funcs)) { 339 const char *str = get(expr); 340 size_t delta = strlen(str) + 2; 341 char *expanded; 342 343 expanded = krealloc(found, len + delta + 1, GFP_KERNEL); 344 if (!expanded) { 345 kfree(found); 346 return expanded; 347 } 348 349 found = expanded; 350 found[len] = '\0'; 351 len += delta; 352 353 strcat(found, str); 354 strcat(found, ", "); 355 356 funcs++; 357 } 358 prios++; 359 } 360 361 if (len < 2) { 362 kfree(found); 363 return NULL; 364 } 365 366 found[len - 2] = '\0'; 367 368 return found; 369 } 370 371 static const char *aspeed_sig_expr_function(const struct aspeed_sig_expr *expr) 372 { 373 return expr->function; 374 } 375 376 static char *get_defined_functions(const struct aspeed_pin_desc *pdesc) 377 { 378 return get_defined_attribute(pdesc, aspeed_sig_expr_function); 379 } 380 381 static const char *aspeed_sig_expr_signal(const struct aspeed_sig_expr *expr) 382 { 383 return expr->signal; 384 } 385 386 static char *get_defined_signals(const struct aspeed_pin_desc *pdesc) 387 { 388 return get_defined_attribute(pdesc, aspeed_sig_expr_signal); 389 } 390 391 int aspeed_pinmux_set_mux(struct pinctrl_dev *pctldev, unsigned int function, 392 unsigned int group) 393 { 394 int i; 395 int ret; 396 const struct aspeed_pinctrl_data *pdata = 397 pinctrl_dev_get_drvdata(pctldev); 398 const struct aspeed_pin_group *pgroup = &pdata->groups[group]; 399 const struct aspeed_pin_function *pfunc = 400 &pdata->functions[function]; 401 402 for (i = 0; i < pgroup->npins; i++) { 403 int pin = pgroup->pins[i]; 404 const struct aspeed_pin_desc *pdesc = pdata->pins[pin].drv_data; 405 const struct aspeed_sig_expr *expr = NULL; 406 const struct aspeed_sig_expr **funcs; 407 const struct aspeed_sig_expr ***prios; 408 409 pr_debug("Muxing pin %d for %s\n", pin, pfunc->name); 410 411 if (!pdesc) 412 return -EINVAL; 413 414 prios = pdesc->prios; 415 416 if (!prios) 417 continue; 418 419 /* Disable functions at a higher priority than that requested */ 420 while ((funcs = *prios)) { 421 expr = aspeed_find_expr_by_name(funcs, pfunc->name); 422 423 if (expr) 424 break; 425 426 ret = aspeed_disable_sig(funcs, pdata->maps); 427 if (ret) 428 return ret; 429 430 prios++; 431 } 432 433 if (!expr) { 434 char *functions = get_defined_functions(pdesc); 435 char *signals = get_defined_signals(pdesc); 436 437 pr_warn("No function %s found on pin %s (%d). Found signal(s) %s for function(s) %s\n", 438 pfunc->name, pdesc->name, pin, signals, 439 functions); 440 kfree(signals); 441 kfree(functions); 442 443 return -ENXIO; 444 } 445 446 ret = aspeed_sig_expr_enable(expr, pdata->maps); 447 if (ret) 448 return ret; 449 } 450 451 return 0; 452 } 453 454 static bool aspeed_expr_is_gpio(const struct aspeed_sig_expr *expr) 455 { 456 /* 457 * The signal type is GPIO if the signal name has "GPIO" as a prefix. 458 * strncmp (rather than strcmp) is used to implement the prefix 459 * requirement. 460 * 461 * expr->signal might look like "GPIOT3" in the GPIO case. 462 */ 463 return strncmp(expr->signal, "GPIO", 4) == 0; 464 } 465 466 static bool aspeed_gpio_in_exprs(const struct aspeed_sig_expr **exprs) 467 { 468 if (!exprs) 469 return false; 470 471 while (*exprs) { 472 if (aspeed_expr_is_gpio(*exprs)) 473 return true; 474 exprs++; 475 } 476 477 return false; 478 } 479 480 int aspeed_gpio_request_enable(struct pinctrl_dev *pctldev, 481 struct pinctrl_gpio_range *range, 482 unsigned int offset) 483 { 484 int ret; 485 const struct aspeed_pinctrl_data *pdata = 486 pinctrl_dev_get_drvdata(pctldev); 487 const struct aspeed_pin_desc *pdesc = pdata->pins[offset].drv_data; 488 const struct aspeed_sig_expr ***prios, **funcs, *expr; 489 490 if (!pdesc) 491 return -EINVAL; 492 493 prios = pdesc->prios; 494 495 if (!prios) 496 return -ENXIO; 497 498 /* Disable any functions of higher priority than GPIO */ 499 while ((funcs = *prios)) { 500 if (aspeed_gpio_in_exprs(funcs)) 501 break; 502 503 ret = aspeed_disable_sig(funcs, pdata->maps); 504 if (ret) 505 return ret; 506 507 prios++; 508 } 509 510 if (!funcs) { 511 char *signals = get_defined_signals(pdesc); 512 513 pr_warn("No GPIO signal type found on pin %s (%d). Found: %s\n", 514 pdesc->name, offset, signals); 515 kfree(signals); 516 517 return -ENXIO; 518 } 519 520 expr = *funcs; 521 522 /* 523 * Disabling all higher-priority expressions is enough to enable the 524 * lowest-priority signal type. As such it has no associated 525 * expression. 526 */ 527 if (!expr) 528 return 0; 529 530 /* 531 * If GPIO is not the lowest priority signal type, assume there is only 532 * one expression defined to enable the GPIO function 533 */ 534 return aspeed_sig_expr_enable(expr, pdata->maps); 535 } 536 537 int aspeed_pinctrl_probe(struct platform_device *pdev, 538 struct pinctrl_desc *pdesc, 539 struct aspeed_pinctrl_data *pdata) 540 { 541 struct device *parent; 542 struct pinctrl_dev *pctl; 543 544 parent = pdev->dev.parent; 545 if (!parent) { 546 dev_err(&pdev->dev, "No parent for syscon pincontroller\n"); 547 return -ENODEV; 548 } 549 550 pdata->maps[ASPEED_IP_SCU] = syscon_node_to_regmap(parent->of_node); 551 if (IS_ERR(pdata->maps[ASPEED_IP_SCU])) { 552 dev_err(&pdev->dev, "No regmap for syscon pincontroller parent\n"); 553 return PTR_ERR(pdata->maps[ASPEED_IP_SCU]); 554 } 555 556 pctl = pinctrl_register(pdesc, &pdev->dev, pdata); 557 558 if (IS_ERR(pctl)) { 559 dev_err(&pdev->dev, "Failed to register pinctrl\n"); 560 return PTR_ERR(pctl); 561 } 562 563 platform_set_drvdata(pdev, pdata); 564 565 return 0; 566 } 567 568 static inline bool pin_in_config_range(unsigned int offset, 569 const struct aspeed_pin_config *config) 570 { 571 return offset >= config->pins[0] && offset <= config->pins[1]; 572 } 573 574 static inline const struct aspeed_pin_config *find_pinconf_config( 575 const struct aspeed_pinctrl_data *pdata, 576 unsigned int offset, 577 enum pin_config_param param) 578 { 579 unsigned int i; 580 581 for (i = 0; i < pdata->nconfigs; i++) { 582 if (param == pdata->configs[i].param && 583 pin_in_config_range(offset, &pdata->configs[i])) 584 return &pdata->configs[i]; 585 } 586 587 return NULL; 588 } 589 590 /** 591 * @param: pinconf configuration parameter 592 * @arg: The supported argument for @param, or -1 if any value is supported 593 * @val: The register value to write to configure @arg for @param 594 * 595 * The map is to be used in conjunction with the configuration array supplied 596 * by the driver implementation. 597 */ 598 struct aspeed_pin_config_map { 599 enum pin_config_param param; 600 s32 arg; 601 u32 val; 602 }; 603 604 enum aspeed_pin_config_map_type { MAP_TYPE_ARG, MAP_TYPE_VAL }; 605 606 /* Aspeed consistently both: 607 * 608 * 1. Defines "disable bits" for internal pull-downs 609 * 2. Uses 8mA or 16mA drive strengths 610 */ 611 static const struct aspeed_pin_config_map pin_config_map[] = { 612 { PIN_CONFIG_BIAS_PULL_DOWN, 0, 1 }, 613 { PIN_CONFIG_BIAS_PULL_DOWN, -1, 0 }, 614 { PIN_CONFIG_BIAS_DISABLE, -1, 1 }, 615 { PIN_CONFIG_DRIVE_STRENGTH, 8, 0 }, 616 { PIN_CONFIG_DRIVE_STRENGTH, 16, 1 }, 617 }; 618 619 static const struct aspeed_pin_config_map *find_pinconf_map( 620 enum pin_config_param param, 621 enum aspeed_pin_config_map_type type, 622 s64 value) 623 { 624 int i; 625 626 for (i = 0; i < ARRAY_SIZE(pin_config_map); i++) { 627 const struct aspeed_pin_config_map *elem; 628 bool match; 629 630 elem = &pin_config_map[i]; 631 632 switch (type) { 633 case MAP_TYPE_ARG: 634 match = (elem->arg == -1 || elem->arg == value); 635 break; 636 case MAP_TYPE_VAL: 637 match = (elem->val == value); 638 break; 639 } 640 641 if (param == elem->param && match) 642 return elem; 643 } 644 645 return NULL; 646 } 647 648 int aspeed_pin_config_get(struct pinctrl_dev *pctldev, unsigned int offset, 649 unsigned long *config) 650 { 651 const enum pin_config_param param = pinconf_to_config_param(*config); 652 const struct aspeed_pin_config_map *pmap; 653 const struct aspeed_pinctrl_data *pdata; 654 const struct aspeed_pin_config *pconf; 655 unsigned int val; 656 int rc = 0; 657 u32 arg; 658 659 pdata = pinctrl_dev_get_drvdata(pctldev); 660 pconf = find_pinconf_config(pdata, offset, param); 661 if (!pconf) 662 return -ENOTSUPP; 663 664 rc = regmap_read(pdata->maps[ASPEED_IP_SCU], pconf->reg, &val); 665 if (rc < 0) 666 return rc; 667 668 pmap = find_pinconf_map(param, MAP_TYPE_VAL, 669 (val & BIT(pconf->bit)) >> pconf->bit); 670 671 if (!pmap) 672 return -EINVAL; 673 674 if (param == PIN_CONFIG_DRIVE_STRENGTH) 675 arg = (u32) pmap->arg; 676 else if (param == PIN_CONFIG_BIAS_PULL_DOWN) 677 arg = !!pmap->arg; 678 else 679 arg = 1; 680 681 if (!arg) 682 return -EINVAL; 683 684 *config = pinconf_to_config_packed(param, arg); 685 686 return 0; 687 } 688 689 int aspeed_pin_config_set(struct pinctrl_dev *pctldev, unsigned int offset, 690 unsigned long *configs, unsigned int num_configs) 691 { 692 const struct aspeed_pinctrl_data *pdata; 693 unsigned int i; 694 int rc = 0; 695 696 pdata = pinctrl_dev_get_drvdata(pctldev); 697 698 for (i = 0; i < num_configs; i++) { 699 const struct aspeed_pin_config_map *pmap; 700 const struct aspeed_pin_config *pconf; 701 enum pin_config_param param; 702 unsigned int val; 703 u32 arg; 704 705 param = pinconf_to_config_param(configs[i]); 706 arg = pinconf_to_config_argument(configs[i]); 707 708 pconf = find_pinconf_config(pdata, offset, param); 709 if (!pconf) 710 return -ENOTSUPP; 711 712 pmap = find_pinconf_map(param, MAP_TYPE_ARG, arg); 713 714 if (WARN_ON(!pmap)) 715 return -EINVAL; 716 717 val = pmap->val << pconf->bit; 718 719 rc = regmap_update_bits(pdata->maps[ASPEED_IP_SCU], pconf->reg, 720 BIT(pconf->bit), val); 721 722 if (rc < 0) 723 return rc; 724 725 pr_debug("%s: Set SCU%02X[%d]=%d for param %d(=%d) on pin %d\n", 726 __func__, pconf->reg, pconf->bit, pmap->val, 727 param, arg, offset); 728 } 729 730 return 0; 731 } 732 733 int aspeed_pin_config_group_get(struct pinctrl_dev *pctldev, 734 unsigned int selector, 735 unsigned long *config) 736 { 737 const unsigned int *pins; 738 unsigned int npins; 739 int rc; 740 741 rc = aspeed_pinctrl_get_group_pins(pctldev, selector, &pins, &npins); 742 if (rc < 0) 743 return rc; 744 745 if (!npins) 746 return -ENODEV; 747 748 rc = aspeed_pin_config_get(pctldev, pins[0], config); 749 750 return rc; 751 } 752 753 int aspeed_pin_config_group_set(struct pinctrl_dev *pctldev, 754 unsigned int selector, 755 unsigned long *configs, 756 unsigned int num_configs) 757 { 758 const unsigned int *pins; 759 unsigned int npins; 760 int rc; 761 int i; 762 763 pr_debug("%s: Fetching pins for group selector %d\n", 764 __func__, selector); 765 rc = aspeed_pinctrl_get_group_pins(pctldev, selector, &pins, &npins); 766 if (rc < 0) 767 return rc; 768 769 for (i = 0; i < npins; i++) { 770 rc = aspeed_pin_config_set(pctldev, pins[i], configs, 771 num_configs); 772 if (rc < 0) 773 return rc; 774 } 775 776 return 0; 777 } 778