1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2015, 2017-2018, 2022, The Linux Foundation. All rights reserved. 4 */ 5 6 #include <linux/bitops.h> 7 #include <linux/delay.h> 8 #include <linux/err.h> 9 #include <linux/export.h> 10 #include <linux/interconnect.h> 11 #include <linux/jiffies.h> 12 #include <linux/kernel.h> 13 #include <linux/ktime.h> 14 #include <linux/pm_domain.h> 15 #include <linux/regmap.h> 16 #include <linux/regulator/consumer.h> 17 #include <linux/reset-controller.h> 18 #include <linux/slab.h> 19 #include "gdsc.h" 20 21 #define PWR_ON_MASK BIT(31) 22 #define EN_REST_WAIT_MASK GENMASK_ULL(23, 20) 23 #define EN_FEW_WAIT_MASK GENMASK_ULL(19, 16) 24 #define CLK_DIS_WAIT_MASK GENMASK_ULL(15, 12) 25 #define SW_OVERRIDE_MASK BIT(2) 26 #define HW_CONTROL_MASK BIT(1) 27 #define SW_COLLAPSE_MASK BIT(0) 28 #define GMEM_CLAMP_IO_MASK BIT(0) 29 #define GMEM_RESET_MASK BIT(4) 30 31 /* CFG_GDSCR */ 32 #define GDSC_POWER_UP_COMPLETE BIT(16) 33 #define GDSC_POWER_DOWN_COMPLETE BIT(15) 34 #define GDSC_RETAIN_FF_ENABLE BIT(11) 35 #define CFG_GDSCR_OFFSET 0x4 36 37 /* Wait 2^n CXO cycles between all states. Here, n=2 (4 cycles). */ 38 #define EN_REST_WAIT_VAL 0x2 39 #define EN_FEW_WAIT_VAL 0x8 40 #define CLK_DIS_WAIT_VAL 0x2 41 42 /* Transition delay shifts */ 43 #define EN_REST_WAIT_SHIFT 20 44 #define EN_FEW_WAIT_SHIFT 16 45 #define CLK_DIS_WAIT_SHIFT 12 46 47 #define RETAIN_MEM BIT(14) 48 #define RETAIN_PERIPH BIT(13) 49 50 #define STATUS_POLL_TIMEOUT_US 2000 51 #define TIMEOUT_US 500 52 53 #define domain_to_gdsc(domain) container_of(domain, struct gdsc, pd) 54 55 enum gdsc_status { 56 GDSC_OFF, 57 GDSC_ON 58 }; 59 60 /* Returns 1 if GDSC status is status, 0 if not, and < 0 on error */ 61 static int gdsc_check_status(struct gdsc *sc, enum gdsc_status status) 62 { 63 unsigned int reg; 64 u32 val; 65 int ret; 66 67 if (sc->flags & POLL_CFG_GDSCR) 68 reg = sc->gdscr + CFG_GDSCR_OFFSET; 69 else if (sc->gds_hw_ctrl) 70 reg = sc->gds_hw_ctrl; 71 else 72 reg = sc->gdscr; 73 74 ret = regmap_read(sc->regmap, reg, &val); 75 if (ret) 76 return ret; 77 78 if (sc->flags & POLL_CFG_GDSCR) { 79 switch (status) { 80 case GDSC_ON: 81 return !!(val & GDSC_POWER_UP_COMPLETE); 82 case GDSC_OFF: 83 return !!(val & GDSC_POWER_DOWN_COMPLETE); 84 } 85 } 86 87 switch (status) { 88 case GDSC_ON: 89 return !!(val & PWR_ON_MASK); 90 case GDSC_OFF: 91 return !(val & PWR_ON_MASK); 92 } 93 94 return -EINVAL; 95 } 96 97 static int gdsc_hwctrl(struct gdsc *sc, bool en) 98 { 99 u32 val = en ? HW_CONTROL_MASK : 0; 100 101 return regmap_update_bits(sc->regmap, sc->gdscr, HW_CONTROL_MASK, val); 102 } 103 104 static int gdsc_poll_status(struct gdsc *sc, enum gdsc_status status) 105 { 106 ktime_t start; 107 108 start = ktime_get(); 109 do { 110 if (gdsc_check_status(sc, status)) 111 return 0; 112 } while (ktime_us_delta(ktime_get(), start) < STATUS_POLL_TIMEOUT_US); 113 114 if (gdsc_check_status(sc, status)) 115 return 0; 116 117 return -ETIMEDOUT; 118 } 119 120 static int gdsc_update_collapse_bit(struct gdsc *sc, bool val) 121 { 122 u32 reg, mask; 123 int ret; 124 125 if (sc->collapse_mask) { 126 reg = sc->collapse_ctrl; 127 mask = sc->collapse_mask; 128 } else { 129 reg = sc->gdscr; 130 mask = SW_COLLAPSE_MASK; 131 } 132 133 ret = regmap_update_bits(sc->regmap, reg, mask, val ? mask : 0); 134 if (ret) 135 return ret; 136 137 return 0; 138 } 139 140 static int gdsc_toggle_logic(struct gdsc *sc, enum gdsc_status status, 141 bool wait) 142 { 143 int ret; 144 145 if (status == GDSC_ON && sc->rsupply) { 146 ret = regulator_enable(sc->rsupply); 147 if (ret < 0) 148 return ret; 149 } 150 151 if (status == GDSC_ON) { 152 ret = icc_set_bw(sc->icc_path, 1, 1); 153 if (ret) 154 goto err_disable_supply; 155 } 156 157 ret = gdsc_update_collapse_bit(sc, status == GDSC_OFF); 158 159 /* If disabling votable gdscs, don't poll on status */ 160 if ((sc->flags & VOTABLE) && status == GDSC_OFF && !wait) { 161 /* 162 * Add a short delay here to ensure that an enable 163 * right after it was disabled does not put it in an 164 * unknown state 165 */ 166 udelay(TIMEOUT_US); 167 return 0; 168 } 169 170 if (sc->gds_hw_ctrl) { 171 /* 172 * The gds hw controller asserts/de-asserts the status bit soon 173 * after it receives a power on/off request from a master. 174 * The controller then takes around 8 xo cycles to start its 175 * internal state machine and update the status bit. During 176 * this time, the status bit does not reflect the true status 177 * of the core. 178 * Add a delay of 1 us between writing to the SW_COLLAPSE bit 179 * and polling the status bit. 180 */ 181 udelay(1); 182 } 183 184 ret = gdsc_poll_status(sc, status); 185 WARN(ret, "%s status stuck at 'o%s'", sc->pd.name, status ? "ff" : "n"); 186 187 if (!ret && status == GDSC_OFF) { 188 ret = icc_set_bw(sc->icc_path, 0, 0); 189 if (ret) 190 return ret; 191 } 192 193 if (!ret && status == GDSC_OFF && sc->rsupply) { 194 ret = regulator_disable(sc->rsupply); 195 if (ret < 0) 196 return ret; 197 } 198 199 return ret; 200 201 err_disable_supply: 202 if (status == GDSC_ON && sc->rsupply) 203 regulator_disable(sc->rsupply); 204 205 return ret; 206 } 207 208 static inline int gdsc_deassert_reset(struct gdsc *sc) 209 { 210 int i; 211 212 for (i = 0; i < sc->reset_count; i++) 213 sc->rcdev->ops->deassert(sc->rcdev, sc->resets[i]); 214 return 0; 215 } 216 217 static inline int gdsc_assert_reset(struct gdsc *sc) 218 { 219 int i; 220 221 for (i = 0; i < sc->reset_count; i++) 222 sc->rcdev->ops->assert(sc->rcdev, sc->resets[i]); 223 return 0; 224 } 225 226 static inline void gdsc_force_mem_on(struct gdsc *sc) 227 { 228 int i; 229 u32 mask = RETAIN_MEM; 230 231 if (!(sc->flags & NO_RET_PERIPH)) 232 mask |= RETAIN_PERIPH; 233 234 for (i = 0; i < sc->cxc_count; i++) 235 regmap_update_bits(sc->regmap, sc->cxcs[i], mask, mask); 236 } 237 238 static inline void gdsc_clear_mem_on(struct gdsc *sc) 239 { 240 int i; 241 u32 mask = RETAIN_MEM; 242 243 if (!(sc->flags & NO_RET_PERIPH)) 244 mask |= RETAIN_PERIPH; 245 246 for (i = 0; i < sc->cxc_count; i++) 247 regmap_update_bits(sc->regmap, sc->cxcs[i], mask, 0); 248 } 249 250 static inline void gdsc_deassert_clamp_io(struct gdsc *sc) 251 { 252 regmap_update_bits(sc->regmap, sc->clamp_io_ctrl, 253 GMEM_CLAMP_IO_MASK, 0); 254 } 255 256 static inline void gdsc_assert_clamp_io(struct gdsc *sc) 257 { 258 regmap_update_bits(sc->regmap, sc->clamp_io_ctrl, 259 GMEM_CLAMP_IO_MASK, 1); 260 } 261 262 static inline void gdsc_assert_reset_aon(struct gdsc *sc) 263 { 264 regmap_update_bits(sc->regmap, sc->clamp_io_ctrl, 265 GMEM_RESET_MASK, 1); 266 udelay(1); 267 regmap_update_bits(sc->regmap, sc->clamp_io_ctrl, 268 GMEM_RESET_MASK, 0); 269 } 270 271 static void gdsc_retain_ff_on(struct gdsc *sc) 272 { 273 u32 mask = GDSC_RETAIN_FF_ENABLE; 274 275 regmap_update_bits(sc->regmap, sc->gdscr, mask, mask); 276 } 277 278 static int gdsc_enable(struct generic_pm_domain *domain) 279 { 280 struct gdsc *sc = domain_to_gdsc(domain); 281 int ret; 282 283 if (sc->pwrsts == PWRSTS_ON) 284 return gdsc_deassert_reset(sc); 285 286 if (sc->flags & SW_RESET) { 287 gdsc_assert_reset(sc); 288 udelay(1); 289 gdsc_deassert_reset(sc); 290 } 291 292 if (sc->flags & CLAMP_IO) { 293 if (sc->flags & AON_RESET) 294 gdsc_assert_reset_aon(sc); 295 gdsc_deassert_clamp_io(sc); 296 } 297 298 ret = gdsc_toggle_logic(sc, GDSC_ON, false); 299 if (ret) 300 return ret; 301 302 if (sc->pwrsts & PWRSTS_OFF) 303 gdsc_force_mem_on(sc); 304 305 /* 306 * If clocks to this power domain were already on, they will take an 307 * additional 4 clock cycles to re-enable after the power domain is 308 * enabled. Delay to account for this. A delay is also needed to ensure 309 * clocks are not enabled within 400ns of enabling power to the 310 * memories. 311 */ 312 udelay(1); 313 314 if (sc->flags & RETAIN_FF_ENABLE) 315 gdsc_retain_ff_on(sc); 316 317 /* Turn on HW trigger mode if supported */ 318 if (sc->flags & HW_CTRL) { 319 ret = gdsc_hwctrl(sc, true); 320 if (ret) 321 return ret; 322 /* 323 * Wait for the GDSC to go through a power down and 324 * up cycle. In case a firmware ends up polling status 325 * bits for the gdsc, it might read an 'on' status before 326 * the GDSC can finish the power cycle. 327 * We wait 1us before returning to ensure the firmware 328 * can't immediately poll the status bits. 329 */ 330 udelay(1); 331 } 332 333 return 0; 334 } 335 336 static int gdsc_disable(struct generic_pm_domain *domain) 337 { 338 struct gdsc *sc = domain_to_gdsc(domain); 339 int ret; 340 341 if (sc->pwrsts == PWRSTS_ON) 342 return gdsc_assert_reset(sc); 343 344 /* Turn off HW trigger mode if supported */ 345 if (sc->flags & HW_CTRL) { 346 ret = gdsc_hwctrl(sc, false); 347 if (ret < 0) 348 return ret; 349 /* 350 * Wait for the GDSC to go through a power down and 351 * up cycle. In case we end up polling status 352 * bits for the gdsc before the power cycle is completed 353 * it might read an 'on' status wrongly. 354 */ 355 udelay(1); 356 357 ret = gdsc_poll_status(sc, GDSC_ON); 358 if (ret) 359 return ret; 360 } 361 362 if (sc->pwrsts & PWRSTS_OFF) 363 gdsc_clear_mem_on(sc); 364 365 /* 366 * If the GDSC supports only a Retention state, apart from ON, 367 * leave it in ON state. 368 * There is no SW control to transition the GDSC into 369 * Retention state. This happens in HW when the parent 370 * domain goes down to a Low power state 371 */ 372 if (sc->pwrsts == PWRSTS_RET_ON) 373 return 0; 374 375 ret = gdsc_toggle_logic(sc, GDSC_OFF, domain->synced_poweroff); 376 if (ret) 377 return ret; 378 379 if (sc->flags & CLAMP_IO) 380 gdsc_assert_clamp_io(sc); 381 382 return 0; 383 } 384 385 static int gdsc_set_hwmode(struct generic_pm_domain *domain, struct device *dev, bool mode) 386 { 387 struct gdsc *sc = domain_to_gdsc(domain); 388 int ret; 389 390 ret = gdsc_hwctrl(sc, mode); 391 if (ret) 392 return ret; 393 394 /* 395 * Wait for the GDSC to go through a power down and 396 * up cycle. If we poll the status register before the 397 * power cycle is finished we might read incorrect values. 398 */ 399 udelay(1); 400 401 /* 402 * When the GDSC is switched to HW mode, HW can disable the GDSC. 403 * When the GDSC is switched back to SW mode, the GDSC will be enabled 404 * again, hence we need to poll for GDSC to complete the power up. 405 */ 406 if (!mode) 407 return gdsc_poll_status(sc, GDSC_ON); 408 409 return 0; 410 } 411 412 static bool gdsc_get_hwmode(struct generic_pm_domain *domain, struct device *dev) 413 { 414 struct gdsc *sc = domain_to_gdsc(domain); 415 u32 val; 416 417 regmap_read(sc->regmap, sc->gdscr, &val); 418 419 return !!(val & HW_CONTROL_MASK); 420 } 421 422 static int gdsc_init(struct gdsc *sc) 423 { 424 u32 mask, val; 425 int on, ret; 426 427 /* 428 * Disable HW trigger: collapse/restore occur based on registers writes. 429 * Disable SW override: Use hardware state-machine for sequencing. 430 * Configure wait time between states. 431 */ 432 mask = HW_CONTROL_MASK | SW_OVERRIDE_MASK | 433 EN_REST_WAIT_MASK | EN_FEW_WAIT_MASK | CLK_DIS_WAIT_MASK; 434 435 if (!sc->en_rest_wait_val) 436 sc->en_rest_wait_val = EN_REST_WAIT_VAL; 437 if (!sc->en_few_wait_val) 438 sc->en_few_wait_val = EN_FEW_WAIT_VAL; 439 if (!sc->clk_dis_wait_val) 440 sc->clk_dis_wait_val = CLK_DIS_WAIT_VAL; 441 442 val = sc->en_rest_wait_val << EN_REST_WAIT_SHIFT | 443 sc->en_few_wait_val << EN_FEW_WAIT_SHIFT | 444 sc->clk_dis_wait_val << CLK_DIS_WAIT_SHIFT; 445 446 ret = regmap_update_bits(sc->regmap, sc->gdscr, mask, val); 447 if (ret) 448 return ret; 449 450 /* Force gdsc ON if only ON state is supported */ 451 if (sc->pwrsts == PWRSTS_ON) { 452 ret = gdsc_toggle_logic(sc, GDSC_ON, false); 453 if (ret) 454 return ret; 455 } 456 457 on = gdsc_check_status(sc, GDSC_ON); 458 if (on < 0) 459 return on; 460 461 if (on) { 462 /* The regulator must be on, sync the kernel state */ 463 if (sc->rsupply) { 464 ret = regulator_enable(sc->rsupply); 465 if (ret < 0) 466 return ret; 467 } 468 469 /* 470 * Votable GDSCs can be ON due to Vote from other masters. 471 * If a Votable GDSC is ON, make sure we have a Vote. 472 */ 473 if (sc->flags & VOTABLE) { 474 ret = gdsc_update_collapse_bit(sc, false); 475 if (ret) 476 goto err_disable_supply; 477 } 478 479 /* 480 * Make sure the retain bit is set if the GDSC is already on, 481 * otherwise we end up turning off the GDSC and destroying all 482 * the register contents that we thought we were saving. 483 */ 484 if (sc->flags & RETAIN_FF_ENABLE) 485 gdsc_retain_ff_on(sc); 486 487 /* Turn on HW trigger mode if supported */ 488 if (sc->flags & HW_CTRL) { 489 ret = gdsc_hwctrl(sc, true); 490 if (ret < 0) 491 goto err_disable_supply; 492 } 493 494 } else if (sc->flags & ALWAYS_ON) { 495 /* If ALWAYS_ON GDSCs are not ON, turn them ON */ 496 gdsc_enable(&sc->pd); 497 on = true; 498 } 499 500 if (on || (sc->pwrsts & PWRSTS_RET)) 501 gdsc_force_mem_on(sc); 502 else 503 gdsc_clear_mem_on(sc); 504 505 if (sc->flags & ALWAYS_ON) 506 sc->pd.flags |= GENPD_FLAG_ALWAYS_ON; 507 if (!sc->pd.power_off) 508 sc->pd.power_off = gdsc_disable; 509 if (!sc->pd.power_on) 510 sc->pd.power_on = gdsc_enable; 511 if (sc->flags & HW_CTRL_TRIGGER) { 512 sc->pd.set_hwmode_dev = gdsc_set_hwmode; 513 sc->pd.get_hwmode_dev = gdsc_get_hwmode; 514 } 515 516 ret = pm_genpd_init(&sc->pd, NULL, !on); 517 if (ret) 518 goto err_disable_supply; 519 520 return 0; 521 522 err_disable_supply: 523 if (on && sc->rsupply) 524 regulator_disable(sc->rsupply); 525 526 return ret; 527 } 528 529 static int gdsc_add_subdomain_list(struct dev_pm_domain_list *pd_list, 530 struct generic_pm_domain *subdomain) 531 { 532 int i, ret; 533 534 for (i = 0; i < pd_list->num_pds; i++) { 535 struct device *dev = pd_list->pd_devs[i]; 536 struct generic_pm_domain *genpd = pd_to_genpd(dev->pm_domain); 537 538 ret = pm_genpd_add_subdomain(genpd, subdomain); 539 if (ret) 540 goto remove_added_subdomains; 541 } 542 543 return 0; 544 545 remove_added_subdomains: 546 for (i--; i >= 0; i--) { 547 struct device *dev = pd_list->pd_devs[i]; 548 struct generic_pm_domain *genpd = pd_to_genpd(dev->pm_domain); 549 550 pm_genpd_remove_subdomain(genpd, subdomain); 551 } 552 553 return ret; 554 } 555 556 static void gdsc_remove_subdomain_list(struct dev_pm_domain_list *pd_list, 557 struct generic_pm_domain *subdomain) 558 { 559 int i; 560 561 for (i = 0; i < pd_list->num_pds; i++) { 562 struct device *dev = pd_list->pd_devs[i]; 563 struct generic_pm_domain *genpd = pd_to_genpd(dev->pm_domain); 564 565 pm_genpd_remove_subdomain(genpd, subdomain); 566 } 567 } 568 569 static void gdsc_pm_subdomain_remove(struct gdsc_desc *desc, size_t num) 570 { 571 struct device *dev = desc->dev; 572 struct gdsc **scs = desc->scs; 573 int i; 574 575 /* Remove subdomains */ 576 for (i = num - 1; i >= 0; i--) { 577 if (!scs[i]) 578 continue; 579 if (scs[i]->parent) 580 pm_genpd_remove_subdomain(scs[i]->parent, &scs[i]->pd); 581 else if (!IS_ERR_OR_NULL(dev->pm_domain)) 582 pm_genpd_remove_subdomain(pd_to_genpd(dev->pm_domain), &scs[i]->pd); 583 else if (desc->pd_list) 584 gdsc_remove_subdomain_list(desc->pd_list, &scs[i]->pd); 585 } 586 } 587 588 int gdsc_register(struct gdsc_desc *desc, 589 struct reset_controller_dev *rcdev, struct regmap *regmap) 590 { 591 int i, ret; 592 struct genpd_onecell_data *data; 593 struct device *dev = desc->dev; 594 struct gdsc **scs = desc->scs; 595 size_t num = desc->num; 596 597 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 598 if (!data) 599 return -ENOMEM; 600 601 data->domains = devm_kcalloc(dev, num, sizeof(*data->domains), 602 GFP_KERNEL); 603 if (!data->domains) 604 return -ENOMEM; 605 606 for (i = 0; i < num; i++) { 607 if (!scs[i] || !scs[i]->needs_icc) 608 continue; 609 610 scs[i]->icc_path = devm_of_icc_get_by_index(dev, scs[i]->icc_path_index); 611 if (IS_ERR(scs[i]->icc_path)) { 612 ret = PTR_ERR(scs[i]->icc_path); 613 if (ret != -ENODEV) 614 return ret; 615 616 scs[i]->icc_path = NULL; 617 } 618 } 619 620 for (i = 0; i < num; i++) { 621 if (!scs[i] || !scs[i]->supply) 622 continue; 623 624 scs[i]->rsupply = devm_regulator_get_optional(dev, scs[i]->supply); 625 if (IS_ERR(scs[i]->rsupply)) { 626 ret = PTR_ERR(scs[i]->rsupply); 627 if (ret != -ENODEV) 628 return ret; 629 630 scs[i]->rsupply = NULL; 631 } 632 } 633 634 data->num_domains = num; 635 for (i = 0; i < num; i++) { 636 if (!scs[i]) 637 continue; 638 scs[i]->regmap = regmap; 639 scs[i]->rcdev = rcdev; 640 ret = gdsc_init(scs[i]); 641 if (ret) 642 return ret; 643 data->domains[i] = &scs[i]->pd; 644 } 645 646 /* Add subdomains */ 647 for (i = 0; i < num; i++) { 648 if (!scs[i]) 649 continue; 650 if (scs[i]->parent) 651 ret = pm_genpd_add_subdomain(scs[i]->parent, &scs[i]->pd); 652 else if (!IS_ERR_OR_NULL(dev->pm_domain)) 653 ret = pm_genpd_add_subdomain(pd_to_genpd(dev->pm_domain), &scs[i]->pd); 654 else if (desc->pd_list) 655 ret = gdsc_add_subdomain_list(desc->pd_list, &scs[i]->pd); 656 657 if (ret) 658 goto err_pm_subdomain_remove; 659 } 660 661 return of_genpd_add_provider_onecell(dev->of_node, data); 662 663 err_pm_subdomain_remove: 664 gdsc_pm_subdomain_remove(desc, i); 665 666 return ret; 667 } 668 669 void gdsc_unregister(struct gdsc_desc *desc) 670 { 671 struct device *dev = desc->dev; 672 size_t num = desc->num; 673 674 gdsc_pm_subdomain_remove(desc, num); 675 of_genpd_del_provider(dev->of_node); 676 } 677 678 /* 679 * On SDM845+ the GPU GX domain is *almost* entirely controlled by the GMU 680 * running in the CX domain so the CPU doesn't need to know anything about the 681 * GX domain EXCEPT.... 682 * 683 * Hardware constraints dictate that the GX be powered down before the CX. If 684 * the GMU crashes it could leave the GX on. In order to successfully bring back 685 * the device the CPU needs to disable the GX headswitch. There being no sane 686 * way to reach in and touch that register from deep inside the GPU driver we 687 * need to set up the infrastructure to be able to ensure that the GPU can 688 * ensure that the GX is off during this super special case. We do this by 689 * defining a GX gdsc with a dummy enable function and a "default" disable 690 * function. 691 * 692 * This allows us to attach with genpd_dev_pm_attach_by_name() in the GPU 693 * driver. During power up, nothing will happen from the CPU (and the GMU will 694 * power up normally but during power down this will ensure that the GX domain 695 * is *really* off - this gives us a semi standard way of doing what we need. 696 */ 697 int gdsc_gx_do_nothing_enable(struct generic_pm_domain *domain) 698 { 699 struct gdsc *sc = domain_to_gdsc(domain); 700 int ret = 0; 701 702 /* Enable the parent supply, when controlled through the regulator framework. */ 703 if (sc->rsupply) 704 ret = regulator_enable(sc->rsupply); 705 706 /* Do nothing with the GDSC itself */ 707 708 return ret; 709 } 710 EXPORT_SYMBOL_GPL(gdsc_gx_do_nothing_enable); 711