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