1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * drivers/soc/tegra/pmc.c 4 * 5 * Copyright (c) 2010 Google, Inc 6 * Copyright (c) 2018-2020, NVIDIA CORPORATION. All rights reserved. 7 * 8 * Author: 9 * Colin Cross <ccross@google.com> 10 */ 11 12 #define pr_fmt(fmt) "tegra-pmc: " fmt 13 14 #include <linux/arm-smccc.h> 15 #include <linux/clk.h> 16 #include <linux/clk-provider.h> 17 #include <linux/clkdev.h> 18 #include <linux/clk/clk-conf.h> 19 #include <linux/clk/tegra.h> 20 #include <linux/debugfs.h> 21 #include <linux/delay.h> 22 #include <linux/device.h> 23 #include <linux/err.h> 24 #include <linux/export.h> 25 #include <linux/init.h> 26 #include <linux/io.h> 27 #include <linux/iopoll.h> 28 #include <linux/irqdomain.h> 29 #include <linux/irq.h> 30 #include <linux/kernel.h> 31 #include <linux/of_address.h> 32 #include <linux/of_clk.h> 33 #include <linux/of.h> 34 #include <linux/of_irq.h> 35 #include <linux/of_platform.h> 36 #include <linux/pinctrl/pinconf-generic.h> 37 #include <linux/pinctrl/pinconf.h> 38 #include <linux/pinctrl/pinctrl.h> 39 #include <linux/platform_device.h> 40 #include <linux/pm_domain.h> 41 #include <linux/reboot.h> 42 #include <linux/reset.h> 43 #include <linux/seq_file.h> 44 #include <linux/slab.h> 45 #include <linux/spinlock.h> 46 47 #include <soc/tegra/common.h> 48 #include <soc/tegra/fuse.h> 49 #include <soc/tegra/pmc.h> 50 51 #include <dt-bindings/interrupt-controller/arm-gic.h> 52 #include <dt-bindings/pinctrl/pinctrl-tegra-io-pad.h> 53 #include <dt-bindings/gpio/tegra186-gpio.h> 54 #include <dt-bindings/gpio/tegra194-gpio.h> 55 #include <dt-bindings/soc/tegra-pmc.h> 56 57 #define PMC_CNTRL 0x0 58 #define PMC_CNTRL_INTR_POLARITY BIT(17) /* inverts INTR polarity */ 59 #define PMC_CNTRL_CPU_PWRREQ_OE BIT(16) /* CPU pwr req enable */ 60 #define PMC_CNTRL_CPU_PWRREQ_POLARITY BIT(15) /* CPU pwr req polarity */ 61 #define PMC_CNTRL_SIDE_EFFECT_LP0 BIT(14) /* LP0 when CPU pwr gated */ 62 #define PMC_CNTRL_SYSCLK_OE BIT(11) /* system clock enable */ 63 #define PMC_CNTRL_SYSCLK_POLARITY BIT(10) /* sys clk polarity */ 64 #define PMC_CNTRL_PWRREQ_POLARITY BIT(8) 65 #define PMC_CNTRL_BLINK_EN 7 66 #define PMC_CNTRL_MAIN_RST BIT(4) 67 68 #define PMC_WAKE_MASK 0x0c 69 #define PMC_WAKE_LEVEL 0x10 70 #define PMC_WAKE_STATUS 0x14 71 #define PMC_SW_WAKE_STATUS 0x18 72 #define PMC_DPD_PADS_ORIDE 0x1c 73 #define PMC_DPD_PADS_ORIDE_BLINK 20 74 75 #define DPD_SAMPLE 0x020 76 #define DPD_SAMPLE_ENABLE BIT(0) 77 #define DPD_SAMPLE_DISABLE (0 << 0) 78 79 #define PWRGATE_TOGGLE 0x30 80 #define PWRGATE_TOGGLE_START BIT(8) 81 82 #define REMOVE_CLAMPING 0x34 83 84 #define PWRGATE_STATUS 0x38 85 86 #define PMC_BLINK_TIMER 0x40 87 #define PMC_IMPL_E_33V_PWR 0x40 88 89 #define PMC_PWR_DET 0x48 90 91 #define PMC_SCRATCH0_MODE_RECOVERY BIT(31) 92 #define PMC_SCRATCH0_MODE_BOOTLOADER BIT(30) 93 #define PMC_SCRATCH0_MODE_RCM BIT(1) 94 #define PMC_SCRATCH0_MODE_MASK (PMC_SCRATCH0_MODE_RECOVERY | \ 95 PMC_SCRATCH0_MODE_BOOTLOADER | \ 96 PMC_SCRATCH0_MODE_RCM) 97 98 #define PMC_CPUPWRGOOD_TIMER 0xc8 99 #define PMC_CPUPWROFF_TIMER 0xcc 100 #define PMC_COREPWRGOOD_TIMER 0x3c 101 #define PMC_COREPWROFF_TIMER 0xe0 102 103 #define PMC_PWR_DET_VALUE 0xe4 104 105 #define PMC_SCRATCH41 0x140 106 107 #define PMC_WAKE2_MASK 0x160 108 #define PMC_WAKE2_LEVEL 0x164 109 #define PMC_WAKE2_STATUS 0x168 110 #define PMC_SW_WAKE2_STATUS 0x16c 111 112 #define PMC_CLK_OUT_CNTRL 0x1a8 113 #define PMC_CLK_OUT_MUX_MASK GENMASK(1, 0) 114 #define PMC_SENSOR_CTRL 0x1b0 115 #define PMC_SENSOR_CTRL_SCRATCH_WRITE BIT(2) 116 #define PMC_SENSOR_CTRL_ENABLE_RST BIT(1) 117 118 #define PMC_RST_STATUS_POR 0 119 #define PMC_RST_STATUS_WATCHDOG 1 120 #define PMC_RST_STATUS_SENSOR 2 121 #define PMC_RST_STATUS_SW_MAIN 3 122 #define PMC_RST_STATUS_LP0 4 123 #define PMC_RST_STATUS_AOTAG 5 124 125 #define IO_DPD_REQ 0x1b8 126 #define IO_DPD_REQ_CODE_IDLE (0U << 30) 127 #define IO_DPD_REQ_CODE_OFF (1U << 30) 128 #define IO_DPD_REQ_CODE_ON (2U << 30) 129 #define IO_DPD_REQ_CODE_MASK (3U << 30) 130 131 #define IO_DPD_STATUS 0x1bc 132 #define IO_DPD2_REQ 0x1c0 133 #define IO_DPD2_STATUS 0x1c4 134 #define SEL_DPD_TIM 0x1c8 135 136 #define PMC_SCRATCH54 0x258 137 #define PMC_SCRATCH54_DATA_SHIFT 8 138 #define PMC_SCRATCH54_ADDR_SHIFT 0 139 140 #define PMC_SCRATCH55 0x25c 141 #define PMC_SCRATCH55_RESET_TEGRA BIT(31) 142 #define PMC_SCRATCH55_CNTRL_ID_SHIFT 27 143 #define PMC_SCRATCH55_PINMUX_SHIFT 24 144 #define PMC_SCRATCH55_16BITOP BIT(15) 145 #define PMC_SCRATCH55_CHECKSUM_SHIFT 16 146 #define PMC_SCRATCH55_I2CSLV1_SHIFT 0 147 148 #define GPU_RG_CNTRL 0x2d4 149 150 /* Tegra186 and later */ 151 #define WAKE_AOWAKE_CNTRL(x) (0x000 + ((x) << 2)) 152 #define WAKE_AOWAKE_CNTRL_LEVEL (1 << 3) 153 #define WAKE_AOWAKE_MASK_W(x) (0x180 + ((x) << 2)) 154 #define WAKE_AOWAKE_MASK_R(x) (0x300 + ((x) << 2)) 155 #define WAKE_AOWAKE_STATUS_W(x) (0x30c + ((x) << 2)) 156 #define WAKE_AOWAKE_STATUS_R(x) (0x48c + ((x) << 2)) 157 #define WAKE_AOWAKE_TIER0_ROUTING(x) (0x4b4 + ((x) << 2)) 158 #define WAKE_AOWAKE_TIER1_ROUTING(x) (0x4c0 + ((x) << 2)) 159 #define WAKE_AOWAKE_TIER2_ROUTING(x) (0x4cc + ((x) << 2)) 160 161 #define WAKE_AOWAKE_CTRL 0x4f4 162 #define WAKE_AOWAKE_CTRL_INTR_POLARITY BIT(0) 163 164 /* for secure PMC */ 165 #define TEGRA_SMC_PMC 0xc2fffe00 166 #define TEGRA_SMC_PMC_READ 0xaa 167 #define TEGRA_SMC_PMC_WRITE 0xbb 168 169 struct pmc_clk { 170 struct clk_hw hw; 171 unsigned long offs; 172 u32 mux_shift; 173 u32 force_en_shift; 174 }; 175 176 #define to_pmc_clk(_hw) container_of(_hw, struct pmc_clk, hw) 177 178 struct pmc_clk_gate { 179 struct clk_hw hw; 180 unsigned long offs; 181 u32 shift; 182 }; 183 184 #define to_pmc_clk_gate(_hw) container_of(_hw, struct pmc_clk_gate, hw) 185 186 struct pmc_clk_init_data { 187 char *name; 188 const char *const *parents; 189 int num_parents; 190 int clk_id; 191 u8 mux_shift; 192 u8 force_en_shift; 193 }; 194 195 static const char * const clk_out1_parents[] = { "osc", "osc_div2", 196 "osc_div4", "extern1", 197 }; 198 199 static const char * const clk_out2_parents[] = { "osc", "osc_div2", 200 "osc_div4", "extern2", 201 }; 202 203 static const char * const clk_out3_parents[] = { "osc", "osc_div2", 204 "osc_div4", "extern3", 205 }; 206 207 static const struct pmc_clk_init_data tegra_pmc_clks_data[] = { 208 { 209 .name = "pmc_clk_out_1", 210 .parents = clk_out1_parents, 211 .num_parents = ARRAY_SIZE(clk_out1_parents), 212 .clk_id = TEGRA_PMC_CLK_OUT_1, 213 .mux_shift = 6, 214 .force_en_shift = 2, 215 }, 216 { 217 .name = "pmc_clk_out_2", 218 .parents = clk_out2_parents, 219 .num_parents = ARRAY_SIZE(clk_out2_parents), 220 .clk_id = TEGRA_PMC_CLK_OUT_2, 221 .mux_shift = 14, 222 .force_en_shift = 10, 223 }, 224 { 225 .name = "pmc_clk_out_3", 226 .parents = clk_out3_parents, 227 .num_parents = ARRAY_SIZE(clk_out3_parents), 228 .clk_id = TEGRA_PMC_CLK_OUT_3, 229 .mux_shift = 22, 230 .force_en_shift = 18, 231 }, 232 }; 233 234 struct tegra_powergate { 235 struct generic_pm_domain genpd; 236 struct tegra_pmc *pmc; 237 unsigned int id; 238 struct clk **clks; 239 unsigned int num_clks; 240 struct reset_control *reset; 241 }; 242 243 struct tegra_io_pad_soc { 244 enum tegra_io_pad id; 245 unsigned int dpd; 246 unsigned int voltage; 247 const char *name; 248 }; 249 250 struct tegra_pmc_regs { 251 unsigned int scratch0; 252 unsigned int dpd_req; 253 unsigned int dpd_status; 254 unsigned int dpd2_req; 255 unsigned int dpd2_status; 256 unsigned int rst_status; 257 unsigned int rst_source_shift; 258 unsigned int rst_source_mask; 259 unsigned int rst_level_shift; 260 unsigned int rst_level_mask; 261 }; 262 263 struct tegra_wake_event { 264 const char *name; 265 unsigned int id; 266 unsigned int irq; 267 struct { 268 unsigned int instance; 269 unsigned int pin; 270 } gpio; 271 }; 272 273 #define TEGRA_WAKE_IRQ(_name, _id, _irq) \ 274 { \ 275 .name = _name, \ 276 .id = _id, \ 277 .irq = _irq, \ 278 .gpio = { \ 279 .instance = UINT_MAX, \ 280 .pin = UINT_MAX, \ 281 }, \ 282 } 283 284 #define TEGRA_WAKE_GPIO(_name, _id, _instance, _pin) \ 285 { \ 286 .name = _name, \ 287 .id = _id, \ 288 .irq = 0, \ 289 .gpio = { \ 290 .instance = _instance, \ 291 .pin = _pin, \ 292 }, \ 293 } 294 295 struct tegra_pmc_soc { 296 unsigned int num_powergates; 297 const char *const *powergates; 298 unsigned int num_cpu_powergates; 299 const u8 *cpu_powergates; 300 301 bool has_tsense_reset; 302 bool has_gpu_clamps; 303 bool needs_mbist_war; 304 bool has_impl_33v_pwr; 305 bool maybe_tz_only; 306 307 const struct tegra_io_pad_soc *io_pads; 308 unsigned int num_io_pads; 309 310 const struct pinctrl_pin_desc *pin_descs; 311 unsigned int num_pin_descs; 312 313 const struct tegra_pmc_regs *regs; 314 void (*init)(struct tegra_pmc *pmc); 315 void (*setup_irq_polarity)(struct tegra_pmc *pmc, 316 struct device_node *np, 317 bool invert); 318 int (*irq_set_wake)(struct irq_data *data, unsigned int on); 319 int (*irq_set_type)(struct irq_data *data, unsigned int type); 320 321 const char * const *reset_sources; 322 unsigned int num_reset_sources; 323 const char * const *reset_levels; 324 unsigned int num_reset_levels; 325 326 /* 327 * These describe events that can wake the system from sleep (i.e. 328 * LP0 or SC7). Wakeup from other sleep states (such as LP1 or LP2) 329 * are dealt with in the LIC. 330 */ 331 const struct tegra_wake_event *wake_events; 332 unsigned int num_wake_events; 333 334 const struct pmc_clk_init_data *pmc_clks_data; 335 unsigned int num_pmc_clks; 336 bool has_blink_output; 337 }; 338 339 static const char * const tegra186_reset_sources[] = { 340 "SYS_RESET", 341 "AOWDT", 342 "MCCPLEXWDT", 343 "BPMPWDT", 344 "SCEWDT", 345 "SPEWDT", 346 "APEWDT", 347 "BCCPLEXWDT", 348 "SENSOR", 349 "AOTAG", 350 "VFSENSOR", 351 "SWREST", 352 "SC7", 353 "HSM", 354 "CORESIGHT" 355 }; 356 357 static const char * const tegra186_reset_levels[] = { 358 "L0", "L1", "L2", "WARM" 359 }; 360 361 static const char * const tegra30_reset_sources[] = { 362 "POWER_ON_RESET", 363 "WATCHDOG", 364 "SENSOR", 365 "SW_MAIN", 366 "LP0" 367 }; 368 369 static const char * const tegra210_reset_sources[] = { 370 "POWER_ON_RESET", 371 "WATCHDOG", 372 "SENSOR", 373 "SW_MAIN", 374 "LP0", 375 "AOTAG" 376 }; 377 378 /** 379 * struct tegra_pmc - NVIDIA Tegra PMC 380 * @dev: pointer to PMC device structure 381 * @base: pointer to I/O remapped register region 382 * @wake: pointer to I/O remapped region for WAKE registers 383 * @aotag: pointer to I/O remapped region for AOTAG registers 384 * @scratch: pointer to I/O remapped region for scratch registers 385 * @clk: pointer to pclk clock 386 * @soc: pointer to SoC data structure 387 * @tz_only: flag specifying if the PMC can only be accessed via TrustZone 388 * @debugfs: pointer to debugfs entry 389 * @rate: currently configured rate of pclk 390 * @suspend_mode: lowest suspend mode available 391 * @cpu_good_time: CPU power good time (in microseconds) 392 * @cpu_off_time: CPU power off time (in microsecends) 393 * @core_osc_time: core power good OSC time (in microseconds) 394 * @core_pmu_time: core power good PMU time (in microseconds) 395 * @core_off_time: core power off time (in microseconds) 396 * @corereq_high: core power request is active-high 397 * @sysclkreq_high: system clock request is active-high 398 * @combined_req: combined power request for CPU & core 399 * @cpu_pwr_good_en: CPU power good signal is enabled 400 * @lp0_vec_phys: physical base address of the LP0 warm boot code 401 * @lp0_vec_size: size of the LP0 warm boot code 402 * @powergates_available: Bitmap of available power gates 403 * @powergates_lock: mutex for power gate register access 404 * @pctl_dev: pin controller exposed by the PMC 405 * @domain: IRQ domain provided by the PMC 406 * @irq: chip implementation for the IRQ domain 407 * @clk_nb: pclk clock changes handler 408 */ 409 struct tegra_pmc { 410 struct device *dev; 411 void __iomem *base; 412 void __iomem *wake; 413 void __iomem *aotag; 414 void __iomem *scratch; 415 struct clk *clk; 416 struct dentry *debugfs; 417 418 const struct tegra_pmc_soc *soc; 419 bool tz_only; 420 421 unsigned long rate; 422 423 enum tegra_suspend_mode suspend_mode; 424 u32 cpu_good_time; 425 u32 cpu_off_time; 426 u32 core_osc_time; 427 u32 core_pmu_time; 428 u32 core_off_time; 429 bool corereq_high; 430 bool sysclkreq_high; 431 bool combined_req; 432 bool cpu_pwr_good_en; 433 u32 lp0_vec_phys; 434 u32 lp0_vec_size; 435 DECLARE_BITMAP(powergates_available, TEGRA_POWERGATE_MAX); 436 437 struct mutex powergates_lock; 438 439 struct pinctrl_dev *pctl_dev; 440 441 struct irq_domain *domain; 442 struct irq_chip irq; 443 444 struct notifier_block clk_nb; 445 }; 446 447 static struct tegra_pmc *pmc = &(struct tegra_pmc) { 448 .base = NULL, 449 .suspend_mode = TEGRA_SUSPEND_NONE, 450 }; 451 452 static inline struct tegra_powergate * 453 to_powergate(struct generic_pm_domain *domain) 454 { 455 return container_of(domain, struct tegra_powergate, genpd); 456 } 457 458 static u32 tegra_pmc_readl(struct tegra_pmc *pmc, unsigned long offset) 459 { 460 struct arm_smccc_res res; 461 462 if (pmc->tz_only) { 463 arm_smccc_smc(TEGRA_SMC_PMC, TEGRA_SMC_PMC_READ, offset, 0, 0, 464 0, 0, 0, &res); 465 if (res.a0) { 466 if (pmc->dev) 467 dev_warn(pmc->dev, "%s(): SMC failed: %lu\n", 468 __func__, res.a0); 469 else 470 pr_warn("%s(): SMC failed: %lu\n", __func__, 471 res.a0); 472 } 473 474 return res.a1; 475 } 476 477 return readl(pmc->base + offset); 478 } 479 480 static void tegra_pmc_writel(struct tegra_pmc *pmc, u32 value, 481 unsigned long offset) 482 { 483 struct arm_smccc_res res; 484 485 if (pmc->tz_only) { 486 arm_smccc_smc(TEGRA_SMC_PMC, TEGRA_SMC_PMC_WRITE, offset, 487 value, 0, 0, 0, 0, &res); 488 if (res.a0) { 489 if (pmc->dev) 490 dev_warn(pmc->dev, "%s(): SMC failed: %lu\n", 491 __func__, res.a0); 492 else 493 pr_warn("%s(): SMC failed: %lu\n", __func__, 494 res.a0); 495 } 496 } else { 497 writel(value, pmc->base + offset); 498 } 499 } 500 501 static u32 tegra_pmc_scratch_readl(struct tegra_pmc *pmc, unsigned long offset) 502 { 503 if (pmc->tz_only) 504 return tegra_pmc_readl(pmc, offset); 505 506 return readl(pmc->scratch + offset); 507 } 508 509 static void tegra_pmc_scratch_writel(struct tegra_pmc *pmc, u32 value, 510 unsigned long offset) 511 { 512 if (pmc->tz_only) 513 tegra_pmc_writel(pmc, value, offset); 514 else 515 writel(value, pmc->scratch + offset); 516 } 517 518 /* 519 * TODO Figure out a way to call this with the struct tegra_pmc * passed in. 520 * This currently doesn't work because readx_poll_timeout() can only operate 521 * on functions that take a single argument. 522 */ 523 static inline bool tegra_powergate_state(int id) 524 { 525 if (id == TEGRA_POWERGATE_3D && pmc->soc->has_gpu_clamps) 526 return (tegra_pmc_readl(pmc, GPU_RG_CNTRL) & 0x1) == 0; 527 else 528 return (tegra_pmc_readl(pmc, PWRGATE_STATUS) & BIT(id)) != 0; 529 } 530 531 static inline bool tegra_powergate_is_valid(struct tegra_pmc *pmc, int id) 532 { 533 return (pmc->soc && pmc->soc->powergates[id]); 534 } 535 536 static inline bool tegra_powergate_is_available(struct tegra_pmc *pmc, int id) 537 { 538 return test_bit(id, pmc->powergates_available); 539 } 540 541 static int tegra_powergate_lookup(struct tegra_pmc *pmc, const char *name) 542 { 543 unsigned int i; 544 545 if (!pmc || !pmc->soc || !name) 546 return -EINVAL; 547 548 for (i = 0; i < pmc->soc->num_powergates; i++) { 549 if (!tegra_powergate_is_valid(pmc, i)) 550 continue; 551 552 if (!strcmp(name, pmc->soc->powergates[i])) 553 return i; 554 } 555 556 return -ENODEV; 557 } 558 559 /** 560 * tegra_powergate_set() - set the state of a partition 561 * @pmc: power management controller 562 * @id: partition ID 563 * @new_state: new state of the partition 564 */ 565 static int tegra_powergate_set(struct tegra_pmc *pmc, unsigned int id, 566 bool new_state) 567 { 568 bool status; 569 int err; 570 571 if (id == TEGRA_POWERGATE_3D && pmc->soc->has_gpu_clamps) 572 return -EINVAL; 573 574 mutex_lock(&pmc->powergates_lock); 575 576 if (tegra_powergate_state(id) == new_state) { 577 mutex_unlock(&pmc->powergates_lock); 578 return 0; 579 } 580 581 tegra_pmc_writel(pmc, PWRGATE_TOGGLE_START | id, PWRGATE_TOGGLE); 582 583 err = readx_poll_timeout(tegra_powergate_state, id, status, 584 status == new_state, 10, 100000); 585 586 mutex_unlock(&pmc->powergates_lock); 587 588 return err; 589 } 590 591 static int __tegra_powergate_remove_clamping(struct tegra_pmc *pmc, 592 unsigned int id) 593 { 594 u32 mask; 595 596 mutex_lock(&pmc->powergates_lock); 597 598 /* 599 * On Tegra124 and later, the clamps for the GPU are controlled by a 600 * separate register (with different semantics). 601 */ 602 if (id == TEGRA_POWERGATE_3D) { 603 if (pmc->soc->has_gpu_clamps) { 604 tegra_pmc_writel(pmc, 0, GPU_RG_CNTRL); 605 goto out; 606 } 607 } 608 609 /* 610 * Tegra 2 has a bug where PCIE and VDE clamping masks are 611 * swapped relatively to the partition ids 612 */ 613 if (id == TEGRA_POWERGATE_VDEC) 614 mask = (1 << TEGRA_POWERGATE_PCIE); 615 else if (id == TEGRA_POWERGATE_PCIE) 616 mask = (1 << TEGRA_POWERGATE_VDEC); 617 else 618 mask = (1 << id); 619 620 tegra_pmc_writel(pmc, mask, REMOVE_CLAMPING); 621 622 out: 623 mutex_unlock(&pmc->powergates_lock); 624 625 return 0; 626 } 627 628 static void tegra_powergate_disable_clocks(struct tegra_powergate *pg) 629 { 630 unsigned int i; 631 632 for (i = 0; i < pg->num_clks; i++) 633 clk_disable_unprepare(pg->clks[i]); 634 } 635 636 static int tegra_powergate_enable_clocks(struct tegra_powergate *pg) 637 { 638 unsigned int i; 639 int err; 640 641 for (i = 0; i < pg->num_clks; i++) { 642 err = clk_prepare_enable(pg->clks[i]); 643 if (err) 644 goto out; 645 } 646 647 return 0; 648 649 out: 650 while (i--) 651 clk_disable_unprepare(pg->clks[i]); 652 653 return err; 654 } 655 656 int __weak tegra210_clk_handle_mbist_war(unsigned int id) 657 { 658 return 0; 659 } 660 661 static int tegra_powergate_power_up(struct tegra_powergate *pg, 662 bool disable_clocks) 663 { 664 int err; 665 666 err = reset_control_assert(pg->reset); 667 if (err) 668 return err; 669 670 usleep_range(10, 20); 671 672 err = tegra_powergate_set(pg->pmc, pg->id, true); 673 if (err < 0) 674 return err; 675 676 usleep_range(10, 20); 677 678 err = tegra_powergate_enable_clocks(pg); 679 if (err) 680 goto disable_clks; 681 682 usleep_range(10, 20); 683 684 err = __tegra_powergate_remove_clamping(pg->pmc, pg->id); 685 if (err) 686 goto disable_clks; 687 688 usleep_range(10, 20); 689 690 err = reset_control_deassert(pg->reset); 691 if (err) 692 goto powergate_off; 693 694 usleep_range(10, 20); 695 696 if (pg->pmc->soc->needs_mbist_war) 697 err = tegra210_clk_handle_mbist_war(pg->id); 698 if (err) 699 goto disable_clks; 700 701 if (disable_clocks) 702 tegra_powergate_disable_clocks(pg); 703 704 return 0; 705 706 disable_clks: 707 tegra_powergate_disable_clocks(pg); 708 usleep_range(10, 20); 709 710 powergate_off: 711 tegra_powergate_set(pg->pmc, pg->id, false); 712 713 return err; 714 } 715 716 static int tegra_powergate_power_down(struct tegra_powergate *pg) 717 { 718 int err; 719 720 err = tegra_powergate_enable_clocks(pg); 721 if (err) 722 return err; 723 724 usleep_range(10, 20); 725 726 err = reset_control_assert(pg->reset); 727 if (err) 728 goto disable_clks; 729 730 usleep_range(10, 20); 731 732 tegra_powergate_disable_clocks(pg); 733 734 usleep_range(10, 20); 735 736 err = tegra_powergate_set(pg->pmc, pg->id, false); 737 if (err) 738 goto assert_resets; 739 740 return 0; 741 742 assert_resets: 743 tegra_powergate_enable_clocks(pg); 744 usleep_range(10, 20); 745 reset_control_deassert(pg->reset); 746 usleep_range(10, 20); 747 748 disable_clks: 749 tegra_powergate_disable_clocks(pg); 750 751 return err; 752 } 753 754 static int tegra_genpd_power_on(struct generic_pm_domain *domain) 755 { 756 struct tegra_powergate *pg = to_powergate(domain); 757 struct device *dev = pg->pmc->dev; 758 int err; 759 760 err = tegra_powergate_power_up(pg, true); 761 if (err) { 762 dev_err(dev, "failed to turn on PM domain %s: %d\n", 763 pg->genpd.name, err); 764 goto out; 765 } 766 767 reset_control_release(pg->reset); 768 769 out: 770 return err; 771 } 772 773 static int tegra_genpd_power_off(struct generic_pm_domain *domain) 774 { 775 struct tegra_powergate *pg = to_powergate(domain); 776 struct device *dev = pg->pmc->dev; 777 int err; 778 779 err = reset_control_acquire(pg->reset); 780 if (err < 0) { 781 pr_err("failed to acquire resets: %d\n", err); 782 return err; 783 } 784 785 err = tegra_powergate_power_down(pg); 786 if (err) { 787 dev_err(dev, "failed to turn off PM domain %s: %d\n", 788 pg->genpd.name, err); 789 reset_control_release(pg->reset); 790 } 791 792 return err; 793 } 794 795 /** 796 * tegra_powergate_power_on() - power on partition 797 * @id: partition ID 798 */ 799 int tegra_powergate_power_on(unsigned int id) 800 { 801 if (!tegra_powergate_is_available(pmc, id)) 802 return -EINVAL; 803 804 return tegra_powergate_set(pmc, id, true); 805 } 806 EXPORT_SYMBOL(tegra_powergate_power_on); 807 808 /** 809 * tegra_powergate_power_off() - power off partition 810 * @id: partition ID 811 */ 812 int tegra_powergate_power_off(unsigned int id) 813 { 814 if (!tegra_powergate_is_available(pmc, id)) 815 return -EINVAL; 816 817 return tegra_powergate_set(pmc, id, false); 818 } 819 EXPORT_SYMBOL(tegra_powergate_power_off); 820 821 /** 822 * tegra_powergate_is_powered() - check if partition is powered 823 * @pmc: power management controller 824 * @id: partition ID 825 */ 826 static int tegra_powergate_is_powered(struct tegra_pmc *pmc, unsigned int id) 827 { 828 if (!tegra_powergate_is_valid(pmc, id)) 829 return -EINVAL; 830 831 return tegra_powergate_state(id); 832 } 833 834 /** 835 * tegra_powergate_remove_clamping() - remove power clamps for partition 836 * @id: partition ID 837 */ 838 int tegra_powergate_remove_clamping(unsigned int id) 839 { 840 if (!tegra_powergate_is_available(pmc, id)) 841 return -EINVAL; 842 843 return __tegra_powergate_remove_clamping(pmc, id); 844 } 845 EXPORT_SYMBOL(tegra_powergate_remove_clamping); 846 847 /** 848 * tegra_powergate_sequence_power_up() - power up partition 849 * @id: partition ID 850 * @clk: clock for partition 851 * @rst: reset for partition 852 * 853 * Must be called with clk disabled, and returns with clk enabled. 854 */ 855 int tegra_powergate_sequence_power_up(unsigned int id, struct clk *clk, 856 struct reset_control *rst) 857 { 858 struct tegra_powergate *pg; 859 int err; 860 861 if (!tegra_powergate_is_available(pmc, id)) 862 return -EINVAL; 863 864 pg = kzalloc(sizeof(*pg), GFP_KERNEL); 865 if (!pg) 866 return -ENOMEM; 867 868 pg->id = id; 869 pg->clks = &clk; 870 pg->num_clks = 1; 871 pg->reset = rst; 872 pg->pmc = pmc; 873 874 err = tegra_powergate_power_up(pg, false); 875 if (err) 876 dev_err(pmc->dev, "failed to turn on partition %d: %d\n", id, 877 err); 878 879 kfree(pg); 880 881 return err; 882 } 883 EXPORT_SYMBOL(tegra_powergate_sequence_power_up); 884 885 /** 886 * tegra_get_cpu_powergate_id() - convert from CPU ID to partition ID 887 * @pmc: power management controller 888 * @cpuid: CPU partition ID 889 * 890 * Returns the partition ID corresponding to the CPU partition ID or a 891 * negative error code on failure. 892 */ 893 static int tegra_get_cpu_powergate_id(struct tegra_pmc *pmc, 894 unsigned int cpuid) 895 { 896 if (pmc->soc && cpuid < pmc->soc->num_cpu_powergates) 897 return pmc->soc->cpu_powergates[cpuid]; 898 899 return -EINVAL; 900 } 901 902 /** 903 * tegra_pmc_cpu_is_powered() - check if CPU partition is powered 904 * @cpuid: CPU partition ID 905 */ 906 bool tegra_pmc_cpu_is_powered(unsigned int cpuid) 907 { 908 int id; 909 910 id = tegra_get_cpu_powergate_id(pmc, cpuid); 911 if (id < 0) 912 return false; 913 914 return tegra_powergate_is_powered(pmc, id); 915 } 916 917 /** 918 * tegra_pmc_cpu_power_on() - power on CPU partition 919 * @cpuid: CPU partition ID 920 */ 921 int tegra_pmc_cpu_power_on(unsigned int cpuid) 922 { 923 int id; 924 925 id = tegra_get_cpu_powergate_id(pmc, cpuid); 926 if (id < 0) 927 return id; 928 929 return tegra_powergate_set(pmc, id, true); 930 } 931 932 /** 933 * tegra_pmc_cpu_remove_clamping() - remove power clamps for CPU partition 934 * @cpuid: CPU partition ID 935 */ 936 int tegra_pmc_cpu_remove_clamping(unsigned int cpuid) 937 { 938 int id; 939 940 id = tegra_get_cpu_powergate_id(pmc, cpuid); 941 if (id < 0) 942 return id; 943 944 return tegra_powergate_remove_clamping(id); 945 } 946 947 static int tegra_pmc_restart_notify(struct notifier_block *this, 948 unsigned long action, void *data) 949 { 950 const char *cmd = data; 951 u32 value; 952 953 value = tegra_pmc_scratch_readl(pmc, pmc->soc->regs->scratch0); 954 value &= ~PMC_SCRATCH0_MODE_MASK; 955 956 if (cmd) { 957 if (strcmp(cmd, "recovery") == 0) 958 value |= PMC_SCRATCH0_MODE_RECOVERY; 959 960 if (strcmp(cmd, "bootloader") == 0) 961 value |= PMC_SCRATCH0_MODE_BOOTLOADER; 962 963 if (strcmp(cmd, "forced-recovery") == 0) 964 value |= PMC_SCRATCH0_MODE_RCM; 965 } 966 967 tegra_pmc_scratch_writel(pmc, value, pmc->soc->regs->scratch0); 968 969 /* reset everything but PMC_SCRATCH0 and PMC_RST_STATUS */ 970 value = tegra_pmc_readl(pmc, PMC_CNTRL); 971 value |= PMC_CNTRL_MAIN_RST; 972 tegra_pmc_writel(pmc, value, PMC_CNTRL); 973 974 return NOTIFY_DONE; 975 } 976 977 static struct notifier_block tegra_pmc_restart_handler = { 978 .notifier_call = tegra_pmc_restart_notify, 979 .priority = 128, 980 }; 981 982 static int powergate_show(struct seq_file *s, void *data) 983 { 984 unsigned int i; 985 int status; 986 987 seq_printf(s, " powergate powered\n"); 988 seq_printf(s, "------------------\n"); 989 990 for (i = 0; i < pmc->soc->num_powergates; i++) { 991 status = tegra_powergate_is_powered(pmc, i); 992 if (status < 0) 993 continue; 994 995 seq_printf(s, " %9s %7s\n", pmc->soc->powergates[i], 996 status ? "yes" : "no"); 997 } 998 999 return 0; 1000 } 1001 1002 DEFINE_SHOW_ATTRIBUTE(powergate); 1003 1004 static int tegra_powergate_debugfs_init(void) 1005 { 1006 pmc->debugfs = debugfs_create_file("powergate", S_IRUGO, NULL, NULL, 1007 &powergate_fops); 1008 if (!pmc->debugfs) 1009 return -ENOMEM; 1010 1011 return 0; 1012 } 1013 1014 static int tegra_powergate_of_get_clks(struct tegra_powergate *pg, 1015 struct device_node *np) 1016 { 1017 struct clk *clk; 1018 unsigned int i, count; 1019 int err; 1020 1021 count = of_clk_get_parent_count(np); 1022 if (count == 0) 1023 return -ENODEV; 1024 1025 pg->clks = kcalloc(count, sizeof(clk), GFP_KERNEL); 1026 if (!pg->clks) 1027 return -ENOMEM; 1028 1029 for (i = 0; i < count; i++) { 1030 pg->clks[i] = of_clk_get(np, i); 1031 if (IS_ERR(pg->clks[i])) { 1032 err = PTR_ERR(pg->clks[i]); 1033 goto err; 1034 } 1035 } 1036 1037 pg->num_clks = count; 1038 1039 return 0; 1040 1041 err: 1042 while (i--) 1043 clk_put(pg->clks[i]); 1044 1045 kfree(pg->clks); 1046 1047 return err; 1048 } 1049 1050 static int tegra_powergate_of_get_resets(struct tegra_powergate *pg, 1051 struct device_node *np, bool off) 1052 { 1053 struct device *dev = pg->pmc->dev; 1054 int err; 1055 1056 pg->reset = of_reset_control_array_get_exclusive_released(np); 1057 if (IS_ERR(pg->reset)) { 1058 err = PTR_ERR(pg->reset); 1059 dev_err(dev, "failed to get device resets: %d\n", err); 1060 return err; 1061 } 1062 1063 err = reset_control_acquire(pg->reset); 1064 if (err < 0) { 1065 pr_err("failed to acquire resets: %d\n", err); 1066 goto out; 1067 } 1068 1069 if (off) { 1070 err = reset_control_assert(pg->reset); 1071 } else { 1072 err = reset_control_deassert(pg->reset); 1073 if (err < 0) 1074 goto out; 1075 1076 reset_control_release(pg->reset); 1077 } 1078 1079 out: 1080 if (err) { 1081 reset_control_release(pg->reset); 1082 reset_control_put(pg->reset); 1083 } 1084 1085 return err; 1086 } 1087 1088 static int tegra_powergate_add(struct tegra_pmc *pmc, struct device_node *np) 1089 { 1090 struct device *dev = pmc->dev; 1091 struct tegra_powergate *pg; 1092 int id, err = 0; 1093 bool off; 1094 1095 pg = kzalloc(sizeof(*pg), GFP_KERNEL); 1096 if (!pg) 1097 return -ENOMEM; 1098 1099 id = tegra_powergate_lookup(pmc, np->name); 1100 if (id < 0) { 1101 dev_err(dev, "powergate lookup failed for %pOFn: %d\n", np, id); 1102 err = -ENODEV; 1103 goto free_mem; 1104 } 1105 1106 /* 1107 * Clear the bit for this powergate so it cannot be managed 1108 * directly via the legacy APIs for controlling powergates. 1109 */ 1110 clear_bit(id, pmc->powergates_available); 1111 1112 pg->id = id; 1113 pg->genpd.name = np->name; 1114 pg->genpd.power_off = tegra_genpd_power_off; 1115 pg->genpd.power_on = tegra_genpd_power_on; 1116 pg->pmc = pmc; 1117 1118 off = !tegra_powergate_is_powered(pmc, pg->id); 1119 1120 err = tegra_powergate_of_get_clks(pg, np); 1121 if (err < 0) { 1122 dev_err(dev, "failed to get clocks for %pOFn: %d\n", np, err); 1123 goto set_available; 1124 } 1125 1126 err = tegra_powergate_of_get_resets(pg, np, off); 1127 if (err < 0) { 1128 dev_err(dev, "failed to get resets for %pOFn: %d\n", np, err); 1129 goto remove_clks; 1130 } 1131 1132 if (!IS_ENABLED(CONFIG_PM_GENERIC_DOMAINS)) { 1133 if (off) 1134 WARN_ON(tegra_powergate_power_up(pg, true)); 1135 1136 goto remove_resets; 1137 } 1138 1139 err = pm_genpd_init(&pg->genpd, NULL, off); 1140 if (err < 0) { 1141 dev_err(dev, "failed to initialise PM domain %pOFn: %d\n", np, 1142 err); 1143 goto remove_resets; 1144 } 1145 1146 err = of_genpd_add_provider_simple(np, &pg->genpd); 1147 if (err < 0) { 1148 dev_err(dev, "failed to add PM domain provider for %pOFn: %d\n", 1149 np, err); 1150 goto remove_genpd; 1151 } 1152 1153 dev_dbg(dev, "added PM domain %s\n", pg->genpd.name); 1154 1155 return 0; 1156 1157 remove_genpd: 1158 pm_genpd_remove(&pg->genpd); 1159 1160 remove_resets: 1161 reset_control_put(pg->reset); 1162 1163 remove_clks: 1164 while (pg->num_clks--) 1165 clk_put(pg->clks[pg->num_clks]); 1166 1167 kfree(pg->clks); 1168 1169 set_available: 1170 set_bit(id, pmc->powergates_available); 1171 1172 free_mem: 1173 kfree(pg); 1174 1175 return err; 1176 } 1177 1178 static int tegra_powergate_init(struct tegra_pmc *pmc, 1179 struct device_node *parent) 1180 { 1181 struct device_node *np, *child; 1182 int err = 0; 1183 1184 np = of_get_child_by_name(parent, "powergates"); 1185 if (!np) 1186 return 0; 1187 1188 for_each_child_of_node(np, child) { 1189 err = tegra_powergate_add(pmc, child); 1190 if (err < 0) { 1191 of_node_put(child); 1192 break; 1193 } 1194 } 1195 1196 of_node_put(np); 1197 1198 return err; 1199 } 1200 1201 static void tegra_powergate_remove(struct generic_pm_domain *genpd) 1202 { 1203 struct tegra_powergate *pg = to_powergate(genpd); 1204 1205 reset_control_put(pg->reset); 1206 1207 while (pg->num_clks--) 1208 clk_put(pg->clks[pg->num_clks]); 1209 1210 kfree(pg->clks); 1211 1212 set_bit(pg->id, pmc->powergates_available); 1213 1214 kfree(pg); 1215 } 1216 1217 static void tegra_powergate_remove_all(struct device_node *parent) 1218 { 1219 struct generic_pm_domain *genpd; 1220 struct device_node *np, *child; 1221 1222 np = of_get_child_by_name(parent, "powergates"); 1223 if (!np) 1224 return; 1225 1226 for_each_child_of_node(np, child) { 1227 of_genpd_del_provider(child); 1228 1229 genpd = of_genpd_remove_last(child); 1230 if (IS_ERR(genpd)) 1231 continue; 1232 1233 tegra_powergate_remove(genpd); 1234 } 1235 1236 of_node_put(np); 1237 } 1238 1239 static const struct tegra_io_pad_soc * 1240 tegra_io_pad_find(struct tegra_pmc *pmc, enum tegra_io_pad id) 1241 { 1242 unsigned int i; 1243 1244 for (i = 0; i < pmc->soc->num_io_pads; i++) 1245 if (pmc->soc->io_pads[i].id == id) 1246 return &pmc->soc->io_pads[i]; 1247 1248 return NULL; 1249 } 1250 1251 static int tegra_io_pad_get_dpd_register_bit(struct tegra_pmc *pmc, 1252 enum tegra_io_pad id, 1253 unsigned long *request, 1254 unsigned long *status, 1255 u32 *mask) 1256 { 1257 const struct tegra_io_pad_soc *pad; 1258 1259 pad = tegra_io_pad_find(pmc, id); 1260 if (!pad) { 1261 dev_err(pmc->dev, "invalid I/O pad ID %u\n", id); 1262 return -ENOENT; 1263 } 1264 1265 if (pad->dpd == UINT_MAX) 1266 return -ENOTSUPP; 1267 1268 *mask = BIT(pad->dpd % 32); 1269 1270 if (pad->dpd < 32) { 1271 *status = pmc->soc->regs->dpd_status; 1272 *request = pmc->soc->regs->dpd_req; 1273 } else { 1274 *status = pmc->soc->regs->dpd2_status; 1275 *request = pmc->soc->regs->dpd2_req; 1276 } 1277 1278 return 0; 1279 } 1280 1281 static int tegra_io_pad_prepare(struct tegra_pmc *pmc, enum tegra_io_pad id, 1282 unsigned long *request, unsigned long *status, 1283 u32 *mask) 1284 { 1285 unsigned long rate, value; 1286 int err; 1287 1288 err = tegra_io_pad_get_dpd_register_bit(pmc, id, request, status, mask); 1289 if (err) 1290 return err; 1291 1292 if (pmc->clk) { 1293 rate = pmc->rate; 1294 if (!rate) { 1295 dev_err(pmc->dev, "failed to get clock rate\n"); 1296 return -ENODEV; 1297 } 1298 1299 tegra_pmc_writel(pmc, DPD_SAMPLE_ENABLE, DPD_SAMPLE); 1300 1301 /* must be at least 200 ns, in APB (PCLK) clock cycles */ 1302 value = DIV_ROUND_UP(1000000000, rate); 1303 value = DIV_ROUND_UP(200, value); 1304 tegra_pmc_writel(pmc, value, SEL_DPD_TIM); 1305 } 1306 1307 return 0; 1308 } 1309 1310 static int tegra_io_pad_poll(struct tegra_pmc *pmc, unsigned long offset, 1311 u32 mask, u32 val, unsigned long timeout) 1312 { 1313 u32 value; 1314 1315 timeout = jiffies + msecs_to_jiffies(timeout); 1316 1317 while (time_after(timeout, jiffies)) { 1318 value = tegra_pmc_readl(pmc, offset); 1319 if ((value & mask) == val) 1320 return 0; 1321 1322 usleep_range(250, 1000); 1323 } 1324 1325 return -ETIMEDOUT; 1326 } 1327 1328 static void tegra_io_pad_unprepare(struct tegra_pmc *pmc) 1329 { 1330 if (pmc->clk) 1331 tegra_pmc_writel(pmc, DPD_SAMPLE_DISABLE, DPD_SAMPLE); 1332 } 1333 1334 /** 1335 * tegra_io_pad_power_enable() - enable power to I/O pad 1336 * @id: Tegra I/O pad ID for which to enable power 1337 * 1338 * Returns: 0 on success or a negative error code on failure. 1339 */ 1340 int tegra_io_pad_power_enable(enum tegra_io_pad id) 1341 { 1342 unsigned long request, status; 1343 u32 mask; 1344 int err; 1345 1346 mutex_lock(&pmc->powergates_lock); 1347 1348 err = tegra_io_pad_prepare(pmc, id, &request, &status, &mask); 1349 if (err < 0) { 1350 dev_err(pmc->dev, "failed to prepare I/O pad: %d\n", err); 1351 goto unlock; 1352 } 1353 1354 tegra_pmc_writel(pmc, IO_DPD_REQ_CODE_OFF | mask, request); 1355 1356 err = tegra_io_pad_poll(pmc, status, mask, 0, 250); 1357 if (err < 0) { 1358 dev_err(pmc->dev, "failed to enable I/O pad: %d\n", err); 1359 goto unlock; 1360 } 1361 1362 tegra_io_pad_unprepare(pmc); 1363 1364 unlock: 1365 mutex_unlock(&pmc->powergates_lock); 1366 return err; 1367 } 1368 EXPORT_SYMBOL(tegra_io_pad_power_enable); 1369 1370 /** 1371 * tegra_io_pad_power_disable() - disable power to I/O pad 1372 * @id: Tegra I/O pad ID for which to disable power 1373 * 1374 * Returns: 0 on success or a negative error code on failure. 1375 */ 1376 int tegra_io_pad_power_disable(enum tegra_io_pad id) 1377 { 1378 unsigned long request, status; 1379 u32 mask; 1380 int err; 1381 1382 mutex_lock(&pmc->powergates_lock); 1383 1384 err = tegra_io_pad_prepare(pmc, id, &request, &status, &mask); 1385 if (err < 0) { 1386 dev_err(pmc->dev, "failed to prepare I/O pad: %d\n", err); 1387 goto unlock; 1388 } 1389 1390 tegra_pmc_writel(pmc, IO_DPD_REQ_CODE_ON | mask, request); 1391 1392 err = tegra_io_pad_poll(pmc, status, mask, mask, 250); 1393 if (err < 0) { 1394 dev_err(pmc->dev, "failed to disable I/O pad: %d\n", err); 1395 goto unlock; 1396 } 1397 1398 tegra_io_pad_unprepare(pmc); 1399 1400 unlock: 1401 mutex_unlock(&pmc->powergates_lock); 1402 return err; 1403 } 1404 EXPORT_SYMBOL(tegra_io_pad_power_disable); 1405 1406 static int tegra_io_pad_is_powered(struct tegra_pmc *pmc, enum tegra_io_pad id) 1407 { 1408 unsigned long request, status; 1409 u32 mask, value; 1410 int err; 1411 1412 err = tegra_io_pad_get_dpd_register_bit(pmc, id, &request, &status, 1413 &mask); 1414 if (err) 1415 return err; 1416 1417 value = tegra_pmc_readl(pmc, status); 1418 1419 return !(value & mask); 1420 } 1421 1422 static int tegra_io_pad_set_voltage(struct tegra_pmc *pmc, enum tegra_io_pad id, 1423 int voltage) 1424 { 1425 const struct tegra_io_pad_soc *pad; 1426 u32 value; 1427 1428 pad = tegra_io_pad_find(pmc, id); 1429 if (!pad) 1430 return -ENOENT; 1431 1432 if (pad->voltage == UINT_MAX) 1433 return -ENOTSUPP; 1434 1435 mutex_lock(&pmc->powergates_lock); 1436 1437 if (pmc->soc->has_impl_33v_pwr) { 1438 value = tegra_pmc_readl(pmc, PMC_IMPL_E_33V_PWR); 1439 1440 if (voltage == TEGRA_IO_PAD_VOLTAGE_1V8) 1441 value &= ~BIT(pad->voltage); 1442 else 1443 value |= BIT(pad->voltage); 1444 1445 tegra_pmc_writel(pmc, value, PMC_IMPL_E_33V_PWR); 1446 } else { 1447 /* write-enable PMC_PWR_DET_VALUE[pad->voltage] */ 1448 value = tegra_pmc_readl(pmc, PMC_PWR_DET); 1449 value |= BIT(pad->voltage); 1450 tegra_pmc_writel(pmc, value, PMC_PWR_DET); 1451 1452 /* update I/O voltage */ 1453 value = tegra_pmc_readl(pmc, PMC_PWR_DET_VALUE); 1454 1455 if (voltage == TEGRA_IO_PAD_VOLTAGE_1V8) 1456 value &= ~BIT(pad->voltage); 1457 else 1458 value |= BIT(pad->voltage); 1459 1460 tegra_pmc_writel(pmc, value, PMC_PWR_DET_VALUE); 1461 } 1462 1463 mutex_unlock(&pmc->powergates_lock); 1464 1465 usleep_range(100, 250); 1466 1467 return 0; 1468 } 1469 1470 static int tegra_io_pad_get_voltage(struct tegra_pmc *pmc, enum tegra_io_pad id) 1471 { 1472 const struct tegra_io_pad_soc *pad; 1473 u32 value; 1474 1475 pad = tegra_io_pad_find(pmc, id); 1476 if (!pad) 1477 return -ENOENT; 1478 1479 if (pad->voltage == UINT_MAX) 1480 return -ENOTSUPP; 1481 1482 if (pmc->soc->has_impl_33v_pwr) 1483 value = tegra_pmc_readl(pmc, PMC_IMPL_E_33V_PWR); 1484 else 1485 value = tegra_pmc_readl(pmc, PMC_PWR_DET_VALUE); 1486 1487 if ((value & BIT(pad->voltage)) == 0) 1488 return TEGRA_IO_PAD_VOLTAGE_1V8; 1489 1490 return TEGRA_IO_PAD_VOLTAGE_3V3; 1491 } 1492 1493 /** 1494 * tegra_io_rail_power_on() - enable power to I/O rail 1495 * @id: Tegra I/O pad ID for which to enable power 1496 * 1497 * See also: tegra_io_pad_power_enable() 1498 */ 1499 int tegra_io_rail_power_on(unsigned int id) 1500 { 1501 return tegra_io_pad_power_enable(id); 1502 } 1503 EXPORT_SYMBOL(tegra_io_rail_power_on); 1504 1505 /** 1506 * tegra_io_rail_power_off() - disable power to I/O rail 1507 * @id: Tegra I/O pad ID for which to disable power 1508 * 1509 * See also: tegra_io_pad_power_disable() 1510 */ 1511 int tegra_io_rail_power_off(unsigned int id) 1512 { 1513 return tegra_io_pad_power_disable(id); 1514 } 1515 EXPORT_SYMBOL(tegra_io_rail_power_off); 1516 1517 #ifdef CONFIG_PM_SLEEP 1518 enum tegra_suspend_mode tegra_pmc_get_suspend_mode(void) 1519 { 1520 return pmc->suspend_mode; 1521 } 1522 1523 void tegra_pmc_set_suspend_mode(enum tegra_suspend_mode mode) 1524 { 1525 if (mode < TEGRA_SUSPEND_NONE || mode >= TEGRA_MAX_SUSPEND_MODE) 1526 return; 1527 1528 pmc->suspend_mode = mode; 1529 } 1530 1531 void tegra_pmc_enter_suspend_mode(enum tegra_suspend_mode mode) 1532 { 1533 unsigned long long rate = 0; 1534 u64 ticks; 1535 u32 value; 1536 1537 switch (mode) { 1538 case TEGRA_SUSPEND_LP1: 1539 rate = 32768; 1540 break; 1541 1542 case TEGRA_SUSPEND_LP2: 1543 rate = pmc->rate; 1544 break; 1545 1546 default: 1547 break; 1548 } 1549 1550 if (WARN_ON_ONCE(rate == 0)) 1551 rate = 100000000; 1552 1553 ticks = pmc->cpu_good_time * rate + USEC_PER_SEC - 1; 1554 do_div(ticks, USEC_PER_SEC); 1555 tegra_pmc_writel(pmc, ticks, PMC_CPUPWRGOOD_TIMER); 1556 1557 ticks = pmc->cpu_off_time * rate + USEC_PER_SEC - 1; 1558 do_div(ticks, USEC_PER_SEC); 1559 tegra_pmc_writel(pmc, ticks, PMC_CPUPWROFF_TIMER); 1560 1561 value = tegra_pmc_readl(pmc, PMC_CNTRL); 1562 value &= ~PMC_CNTRL_SIDE_EFFECT_LP0; 1563 value |= PMC_CNTRL_CPU_PWRREQ_OE; 1564 tegra_pmc_writel(pmc, value, PMC_CNTRL); 1565 } 1566 #endif 1567 1568 static int tegra_pmc_parse_dt(struct tegra_pmc *pmc, struct device_node *np) 1569 { 1570 u32 value, values[2]; 1571 1572 if (of_property_read_u32(np, "nvidia,suspend-mode", &value)) { 1573 } else { 1574 switch (value) { 1575 case 0: 1576 pmc->suspend_mode = TEGRA_SUSPEND_LP0; 1577 break; 1578 1579 case 1: 1580 pmc->suspend_mode = TEGRA_SUSPEND_LP1; 1581 break; 1582 1583 case 2: 1584 pmc->suspend_mode = TEGRA_SUSPEND_LP2; 1585 break; 1586 1587 default: 1588 pmc->suspend_mode = TEGRA_SUSPEND_NONE; 1589 break; 1590 } 1591 } 1592 1593 pmc->suspend_mode = tegra_pm_validate_suspend_mode(pmc->suspend_mode); 1594 1595 if (of_property_read_u32(np, "nvidia,cpu-pwr-good-time", &value)) 1596 pmc->suspend_mode = TEGRA_SUSPEND_NONE; 1597 1598 pmc->cpu_good_time = value; 1599 1600 if (of_property_read_u32(np, "nvidia,cpu-pwr-off-time", &value)) 1601 pmc->suspend_mode = TEGRA_SUSPEND_NONE; 1602 1603 pmc->cpu_off_time = value; 1604 1605 if (of_property_read_u32_array(np, "nvidia,core-pwr-good-time", 1606 values, ARRAY_SIZE(values))) 1607 pmc->suspend_mode = TEGRA_SUSPEND_NONE; 1608 1609 pmc->core_osc_time = values[0]; 1610 pmc->core_pmu_time = values[1]; 1611 1612 if (of_property_read_u32(np, "nvidia,core-pwr-off-time", &value)) 1613 pmc->suspend_mode = TEGRA_SUSPEND_NONE; 1614 1615 pmc->core_off_time = value; 1616 1617 pmc->corereq_high = of_property_read_bool(np, 1618 "nvidia,core-power-req-active-high"); 1619 1620 pmc->sysclkreq_high = of_property_read_bool(np, 1621 "nvidia,sys-clock-req-active-high"); 1622 1623 pmc->combined_req = of_property_read_bool(np, 1624 "nvidia,combined-power-req"); 1625 1626 pmc->cpu_pwr_good_en = of_property_read_bool(np, 1627 "nvidia,cpu-pwr-good-en"); 1628 1629 if (of_property_read_u32_array(np, "nvidia,lp0-vec", values, 1630 ARRAY_SIZE(values))) 1631 if (pmc->suspend_mode == TEGRA_SUSPEND_LP0) 1632 pmc->suspend_mode = TEGRA_SUSPEND_LP1; 1633 1634 pmc->lp0_vec_phys = values[0]; 1635 pmc->lp0_vec_size = values[1]; 1636 1637 return 0; 1638 } 1639 1640 static void tegra_pmc_init(struct tegra_pmc *pmc) 1641 { 1642 if (pmc->soc->init) 1643 pmc->soc->init(pmc); 1644 } 1645 1646 static void tegra_pmc_init_tsense_reset(struct tegra_pmc *pmc) 1647 { 1648 static const char disabled[] = "emergency thermal reset disabled"; 1649 u32 pmu_addr, ctrl_id, reg_addr, reg_data, pinmux; 1650 struct device *dev = pmc->dev; 1651 struct device_node *np; 1652 u32 value, checksum; 1653 1654 if (!pmc->soc->has_tsense_reset) 1655 return; 1656 1657 np = of_get_child_by_name(pmc->dev->of_node, "i2c-thermtrip"); 1658 if (!np) { 1659 dev_warn(dev, "i2c-thermtrip node not found, %s.\n", disabled); 1660 return; 1661 } 1662 1663 if (of_property_read_u32(np, "nvidia,i2c-controller-id", &ctrl_id)) { 1664 dev_err(dev, "I2C controller ID missing, %s.\n", disabled); 1665 goto out; 1666 } 1667 1668 if (of_property_read_u32(np, "nvidia,bus-addr", &pmu_addr)) { 1669 dev_err(dev, "nvidia,bus-addr missing, %s.\n", disabled); 1670 goto out; 1671 } 1672 1673 if (of_property_read_u32(np, "nvidia,reg-addr", ®_addr)) { 1674 dev_err(dev, "nvidia,reg-addr missing, %s.\n", disabled); 1675 goto out; 1676 } 1677 1678 if (of_property_read_u32(np, "nvidia,reg-data", ®_data)) { 1679 dev_err(dev, "nvidia,reg-data missing, %s.\n", disabled); 1680 goto out; 1681 } 1682 1683 if (of_property_read_u32(np, "nvidia,pinmux-id", &pinmux)) 1684 pinmux = 0; 1685 1686 value = tegra_pmc_readl(pmc, PMC_SENSOR_CTRL); 1687 value |= PMC_SENSOR_CTRL_SCRATCH_WRITE; 1688 tegra_pmc_writel(pmc, value, PMC_SENSOR_CTRL); 1689 1690 value = (reg_data << PMC_SCRATCH54_DATA_SHIFT) | 1691 (reg_addr << PMC_SCRATCH54_ADDR_SHIFT); 1692 tegra_pmc_writel(pmc, value, PMC_SCRATCH54); 1693 1694 value = PMC_SCRATCH55_RESET_TEGRA; 1695 value |= ctrl_id << PMC_SCRATCH55_CNTRL_ID_SHIFT; 1696 value |= pinmux << PMC_SCRATCH55_PINMUX_SHIFT; 1697 value |= pmu_addr << PMC_SCRATCH55_I2CSLV1_SHIFT; 1698 1699 /* 1700 * Calculate checksum of SCRATCH54, SCRATCH55 fields. Bits 23:16 will 1701 * contain the checksum and are currently zero, so they are not added. 1702 */ 1703 checksum = reg_addr + reg_data + (value & 0xff) + ((value >> 8) & 0xff) 1704 + ((value >> 24) & 0xff); 1705 checksum &= 0xff; 1706 checksum = 0x100 - checksum; 1707 1708 value |= checksum << PMC_SCRATCH55_CHECKSUM_SHIFT; 1709 1710 tegra_pmc_writel(pmc, value, PMC_SCRATCH55); 1711 1712 value = tegra_pmc_readl(pmc, PMC_SENSOR_CTRL); 1713 value |= PMC_SENSOR_CTRL_ENABLE_RST; 1714 tegra_pmc_writel(pmc, value, PMC_SENSOR_CTRL); 1715 1716 dev_info(pmc->dev, "emergency thermal reset enabled\n"); 1717 1718 out: 1719 of_node_put(np); 1720 } 1721 1722 static int tegra_io_pad_pinctrl_get_groups_count(struct pinctrl_dev *pctl_dev) 1723 { 1724 struct tegra_pmc *pmc = pinctrl_dev_get_drvdata(pctl_dev); 1725 1726 return pmc->soc->num_io_pads; 1727 } 1728 1729 static const char *tegra_io_pad_pinctrl_get_group_name(struct pinctrl_dev *pctl, 1730 unsigned int group) 1731 { 1732 struct tegra_pmc *pmc = pinctrl_dev_get_drvdata(pctl); 1733 1734 return pmc->soc->io_pads[group].name; 1735 } 1736 1737 static int tegra_io_pad_pinctrl_get_group_pins(struct pinctrl_dev *pctl_dev, 1738 unsigned int group, 1739 const unsigned int **pins, 1740 unsigned int *num_pins) 1741 { 1742 struct tegra_pmc *pmc = pinctrl_dev_get_drvdata(pctl_dev); 1743 1744 *pins = &pmc->soc->io_pads[group].id; 1745 *num_pins = 1; 1746 1747 return 0; 1748 } 1749 1750 static const struct pinctrl_ops tegra_io_pad_pinctrl_ops = { 1751 .get_groups_count = tegra_io_pad_pinctrl_get_groups_count, 1752 .get_group_name = tegra_io_pad_pinctrl_get_group_name, 1753 .get_group_pins = tegra_io_pad_pinctrl_get_group_pins, 1754 .dt_node_to_map = pinconf_generic_dt_node_to_map_pin, 1755 .dt_free_map = pinconf_generic_dt_free_map, 1756 }; 1757 1758 static int tegra_io_pad_pinconf_get(struct pinctrl_dev *pctl_dev, 1759 unsigned int pin, unsigned long *config) 1760 { 1761 enum pin_config_param param = pinconf_to_config_param(*config); 1762 struct tegra_pmc *pmc = pinctrl_dev_get_drvdata(pctl_dev); 1763 const struct tegra_io_pad_soc *pad; 1764 int ret; 1765 u32 arg; 1766 1767 pad = tegra_io_pad_find(pmc, pin); 1768 if (!pad) 1769 return -EINVAL; 1770 1771 switch (param) { 1772 case PIN_CONFIG_POWER_SOURCE: 1773 ret = tegra_io_pad_get_voltage(pmc, pad->id); 1774 if (ret < 0) 1775 return ret; 1776 1777 arg = ret; 1778 break; 1779 1780 case PIN_CONFIG_LOW_POWER_MODE: 1781 ret = tegra_io_pad_is_powered(pmc, pad->id); 1782 if (ret < 0) 1783 return ret; 1784 1785 arg = !ret; 1786 break; 1787 1788 default: 1789 return -EINVAL; 1790 } 1791 1792 *config = pinconf_to_config_packed(param, arg); 1793 1794 return 0; 1795 } 1796 1797 static int tegra_io_pad_pinconf_set(struct pinctrl_dev *pctl_dev, 1798 unsigned int pin, unsigned long *configs, 1799 unsigned int num_configs) 1800 { 1801 struct tegra_pmc *pmc = pinctrl_dev_get_drvdata(pctl_dev); 1802 const struct tegra_io_pad_soc *pad; 1803 enum pin_config_param param; 1804 unsigned int i; 1805 int err; 1806 u32 arg; 1807 1808 pad = tegra_io_pad_find(pmc, pin); 1809 if (!pad) 1810 return -EINVAL; 1811 1812 for (i = 0; i < num_configs; ++i) { 1813 param = pinconf_to_config_param(configs[i]); 1814 arg = pinconf_to_config_argument(configs[i]); 1815 1816 switch (param) { 1817 case PIN_CONFIG_LOW_POWER_MODE: 1818 if (arg) 1819 err = tegra_io_pad_power_disable(pad->id); 1820 else 1821 err = tegra_io_pad_power_enable(pad->id); 1822 if (err) 1823 return err; 1824 break; 1825 case PIN_CONFIG_POWER_SOURCE: 1826 if (arg != TEGRA_IO_PAD_VOLTAGE_1V8 && 1827 arg != TEGRA_IO_PAD_VOLTAGE_3V3) 1828 return -EINVAL; 1829 err = tegra_io_pad_set_voltage(pmc, pad->id, arg); 1830 if (err) 1831 return err; 1832 break; 1833 default: 1834 return -EINVAL; 1835 } 1836 } 1837 1838 return 0; 1839 } 1840 1841 static const struct pinconf_ops tegra_io_pad_pinconf_ops = { 1842 .pin_config_get = tegra_io_pad_pinconf_get, 1843 .pin_config_set = tegra_io_pad_pinconf_set, 1844 .is_generic = true, 1845 }; 1846 1847 static struct pinctrl_desc tegra_pmc_pctl_desc = { 1848 .pctlops = &tegra_io_pad_pinctrl_ops, 1849 .confops = &tegra_io_pad_pinconf_ops, 1850 }; 1851 1852 static int tegra_pmc_pinctrl_init(struct tegra_pmc *pmc) 1853 { 1854 int err; 1855 1856 if (!pmc->soc->num_pin_descs) 1857 return 0; 1858 1859 tegra_pmc_pctl_desc.name = dev_name(pmc->dev); 1860 tegra_pmc_pctl_desc.pins = pmc->soc->pin_descs; 1861 tegra_pmc_pctl_desc.npins = pmc->soc->num_pin_descs; 1862 1863 pmc->pctl_dev = devm_pinctrl_register(pmc->dev, &tegra_pmc_pctl_desc, 1864 pmc); 1865 if (IS_ERR(pmc->pctl_dev)) { 1866 err = PTR_ERR(pmc->pctl_dev); 1867 dev_err(pmc->dev, "failed to register pin controller: %d\n", 1868 err); 1869 return err; 1870 } 1871 1872 return 0; 1873 } 1874 1875 static ssize_t reset_reason_show(struct device *dev, 1876 struct device_attribute *attr, char *buf) 1877 { 1878 u32 value; 1879 1880 value = tegra_pmc_readl(pmc, pmc->soc->regs->rst_status); 1881 value &= pmc->soc->regs->rst_source_mask; 1882 value >>= pmc->soc->regs->rst_source_shift; 1883 1884 if (WARN_ON(value >= pmc->soc->num_reset_sources)) 1885 return sprintf(buf, "%s\n", "UNKNOWN"); 1886 1887 return sprintf(buf, "%s\n", pmc->soc->reset_sources[value]); 1888 } 1889 1890 static DEVICE_ATTR_RO(reset_reason); 1891 1892 static ssize_t reset_level_show(struct device *dev, 1893 struct device_attribute *attr, char *buf) 1894 { 1895 u32 value; 1896 1897 value = tegra_pmc_readl(pmc, pmc->soc->regs->rst_status); 1898 value &= pmc->soc->regs->rst_level_mask; 1899 value >>= pmc->soc->regs->rst_level_shift; 1900 1901 if (WARN_ON(value >= pmc->soc->num_reset_levels)) 1902 return sprintf(buf, "%s\n", "UNKNOWN"); 1903 1904 return sprintf(buf, "%s\n", pmc->soc->reset_levels[value]); 1905 } 1906 1907 static DEVICE_ATTR_RO(reset_level); 1908 1909 static void tegra_pmc_reset_sysfs_init(struct tegra_pmc *pmc) 1910 { 1911 struct device *dev = pmc->dev; 1912 int err = 0; 1913 1914 if (pmc->soc->reset_sources) { 1915 err = device_create_file(dev, &dev_attr_reset_reason); 1916 if (err < 0) 1917 dev_warn(dev, 1918 "failed to create attr \"reset_reason\": %d\n", 1919 err); 1920 } 1921 1922 if (pmc->soc->reset_levels) { 1923 err = device_create_file(dev, &dev_attr_reset_level); 1924 if (err < 0) 1925 dev_warn(dev, 1926 "failed to create attr \"reset_level\": %d\n", 1927 err); 1928 } 1929 } 1930 1931 static int tegra_pmc_irq_translate(struct irq_domain *domain, 1932 struct irq_fwspec *fwspec, 1933 unsigned long *hwirq, 1934 unsigned int *type) 1935 { 1936 if (WARN_ON(fwspec->param_count < 2)) 1937 return -EINVAL; 1938 1939 *hwirq = fwspec->param[0]; 1940 *type = fwspec->param[1]; 1941 1942 return 0; 1943 } 1944 1945 static int tegra_pmc_irq_alloc(struct irq_domain *domain, unsigned int virq, 1946 unsigned int num_irqs, void *data) 1947 { 1948 struct tegra_pmc *pmc = domain->host_data; 1949 const struct tegra_pmc_soc *soc = pmc->soc; 1950 struct irq_fwspec *fwspec = data; 1951 unsigned int i; 1952 int err = 0; 1953 1954 if (WARN_ON(num_irqs > 1)) 1955 return -EINVAL; 1956 1957 for (i = 0; i < soc->num_wake_events; i++) { 1958 const struct tegra_wake_event *event = &soc->wake_events[i]; 1959 1960 if (fwspec->param_count == 2) { 1961 struct irq_fwspec spec; 1962 1963 if (event->id != fwspec->param[0]) 1964 continue; 1965 1966 err = irq_domain_set_hwirq_and_chip(domain, virq, 1967 event->id, 1968 &pmc->irq, pmc); 1969 if (err < 0) 1970 break; 1971 1972 spec.fwnode = &pmc->dev->of_node->fwnode; 1973 spec.param_count = 3; 1974 spec.param[0] = GIC_SPI; 1975 spec.param[1] = event->irq; 1976 spec.param[2] = fwspec->param[1]; 1977 1978 err = irq_domain_alloc_irqs_parent(domain, virq, 1979 num_irqs, &spec); 1980 1981 break; 1982 } 1983 1984 if (fwspec->param_count == 3) { 1985 if (event->gpio.instance != fwspec->param[0] || 1986 event->gpio.pin != fwspec->param[1]) 1987 continue; 1988 1989 err = irq_domain_set_hwirq_and_chip(domain, virq, 1990 event->id, 1991 &pmc->irq, pmc); 1992 1993 /* GPIO hierarchies stop at the PMC level */ 1994 if (!err && domain->parent) 1995 err = irq_domain_disconnect_hierarchy(domain->parent, 1996 virq); 1997 break; 1998 } 1999 } 2000 2001 /* If there is no wake-up event, there is no PMC mapping */ 2002 if (i == soc->num_wake_events) 2003 err = irq_domain_disconnect_hierarchy(domain, virq); 2004 2005 return err; 2006 } 2007 2008 static const struct irq_domain_ops tegra_pmc_irq_domain_ops = { 2009 .translate = tegra_pmc_irq_translate, 2010 .alloc = tegra_pmc_irq_alloc, 2011 }; 2012 2013 static int tegra210_pmc_irq_set_wake(struct irq_data *data, unsigned int on) 2014 { 2015 struct tegra_pmc *pmc = irq_data_get_irq_chip_data(data); 2016 unsigned int offset, bit; 2017 u32 value; 2018 2019 offset = data->hwirq / 32; 2020 bit = data->hwirq % 32; 2021 2022 /* clear wake status */ 2023 tegra_pmc_writel(pmc, 0, PMC_SW_WAKE_STATUS); 2024 tegra_pmc_writel(pmc, 0, PMC_SW_WAKE2_STATUS); 2025 2026 tegra_pmc_writel(pmc, 0, PMC_WAKE_STATUS); 2027 tegra_pmc_writel(pmc, 0, PMC_WAKE2_STATUS); 2028 2029 /* enable PMC wake */ 2030 if (data->hwirq >= 32) 2031 offset = PMC_WAKE2_MASK; 2032 else 2033 offset = PMC_WAKE_MASK; 2034 2035 value = tegra_pmc_readl(pmc, offset); 2036 2037 if (on) 2038 value |= BIT(bit); 2039 else 2040 value &= ~BIT(bit); 2041 2042 tegra_pmc_writel(pmc, value, offset); 2043 2044 return 0; 2045 } 2046 2047 static int tegra210_pmc_irq_set_type(struct irq_data *data, unsigned int type) 2048 { 2049 struct tegra_pmc *pmc = irq_data_get_irq_chip_data(data); 2050 unsigned int offset, bit; 2051 u32 value; 2052 2053 offset = data->hwirq / 32; 2054 bit = data->hwirq % 32; 2055 2056 if (data->hwirq >= 32) 2057 offset = PMC_WAKE2_LEVEL; 2058 else 2059 offset = PMC_WAKE_LEVEL; 2060 2061 value = tegra_pmc_readl(pmc, offset); 2062 2063 switch (type) { 2064 case IRQ_TYPE_EDGE_RISING: 2065 case IRQ_TYPE_LEVEL_HIGH: 2066 value |= BIT(bit); 2067 break; 2068 2069 case IRQ_TYPE_EDGE_FALLING: 2070 case IRQ_TYPE_LEVEL_LOW: 2071 value &= ~BIT(bit); 2072 break; 2073 2074 case IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING: 2075 value ^= BIT(bit); 2076 break; 2077 2078 default: 2079 return -EINVAL; 2080 } 2081 2082 tegra_pmc_writel(pmc, value, offset); 2083 2084 return 0; 2085 } 2086 2087 static int tegra186_pmc_irq_set_wake(struct irq_data *data, unsigned int on) 2088 { 2089 struct tegra_pmc *pmc = irq_data_get_irq_chip_data(data); 2090 unsigned int offset, bit; 2091 u32 value; 2092 2093 offset = data->hwirq / 32; 2094 bit = data->hwirq % 32; 2095 2096 /* clear wake status */ 2097 writel(0x1, pmc->wake + WAKE_AOWAKE_STATUS_W(data->hwirq)); 2098 2099 /* route wake to tier 2 */ 2100 value = readl(pmc->wake + WAKE_AOWAKE_TIER2_ROUTING(offset)); 2101 2102 if (!on) 2103 value &= ~(1 << bit); 2104 else 2105 value |= 1 << bit; 2106 2107 writel(value, pmc->wake + WAKE_AOWAKE_TIER2_ROUTING(offset)); 2108 2109 /* enable wakeup event */ 2110 writel(!!on, pmc->wake + WAKE_AOWAKE_MASK_W(data->hwirq)); 2111 2112 return 0; 2113 } 2114 2115 static int tegra186_pmc_irq_set_type(struct irq_data *data, unsigned int type) 2116 { 2117 struct tegra_pmc *pmc = irq_data_get_irq_chip_data(data); 2118 u32 value; 2119 2120 value = readl(pmc->wake + WAKE_AOWAKE_CNTRL(data->hwirq)); 2121 2122 switch (type) { 2123 case IRQ_TYPE_EDGE_RISING: 2124 case IRQ_TYPE_LEVEL_HIGH: 2125 value |= WAKE_AOWAKE_CNTRL_LEVEL; 2126 break; 2127 2128 case IRQ_TYPE_EDGE_FALLING: 2129 case IRQ_TYPE_LEVEL_LOW: 2130 value &= ~WAKE_AOWAKE_CNTRL_LEVEL; 2131 break; 2132 2133 case IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING: 2134 value ^= WAKE_AOWAKE_CNTRL_LEVEL; 2135 break; 2136 2137 default: 2138 return -EINVAL; 2139 } 2140 2141 writel(value, pmc->wake + WAKE_AOWAKE_CNTRL(data->hwirq)); 2142 2143 return 0; 2144 } 2145 2146 static void tegra_irq_mask_parent(struct irq_data *data) 2147 { 2148 if (data->parent_data) 2149 irq_chip_mask_parent(data); 2150 } 2151 2152 static void tegra_irq_unmask_parent(struct irq_data *data) 2153 { 2154 if (data->parent_data) 2155 irq_chip_unmask_parent(data); 2156 } 2157 2158 static void tegra_irq_eoi_parent(struct irq_data *data) 2159 { 2160 if (data->parent_data) 2161 irq_chip_eoi_parent(data); 2162 } 2163 2164 static int tegra_irq_set_affinity_parent(struct irq_data *data, 2165 const struct cpumask *dest, 2166 bool force) 2167 { 2168 if (data->parent_data) 2169 return irq_chip_set_affinity_parent(data, dest, force); 2170 2171 return -EINVAL; 2172 } 2173 2174 static int tegra_pmc_irq_init(struct tegra_pmc *pmc) 2175 { 2176 struct irq_domain *parent = NULL; 2177 struct device_node *np; 2178 2179 np = of_irq_find_parent(pmc->dev->of_node); 2180 if (np) { 2181 parent = irq_find_host(np); 2182 of_node_put(np); 2183 } 2184 2185 if (!parent) 2186 return 0; 2187 2188 pmc->irq.name = dev_name(pmc->dev); 2189 pmc->irq.irq_mask = tegra_irq_mask_parent; 2190 pmc->irq.irq_unmask = tegra_irq_unmask_parent; 2191 pmc->irq.irq_eoi = tegra_irq_eoi_parent; 2192 pmc->irq.irq_set_affinity = tegra_irq_set_affinity_parent; 2193 pmc->irq.irq_set_type = pmc->soc->irq_set_type; 2194 pmc->irq.irq_set_wake = pmc->soc->irq_set_wake; 2195 2196 pmc->domain = irq_domain_add_hierarchy(parent, 0, 96, pmc->dev->of_node, 2197 &tegra_pmc_irq_domain_ops, pmc); 2198 if (!pmc->domain) { 2199 dev_err(pmc->dev, "failed to allocate domain\n"); 2200 return -ENOMEM; 2201 } 2202 2203 return 0; 2204 } 2205 2206 static int tegra_pmc_clk_notify_cb(struct notifier_block *nb, 2207 unsigned long action, void *ptr) 2208 { 2209 struct tegra_pmc *pmc = container_of(nb, struct tegra_pmc, clk_nb); 2210 struct clk_notifier_data *data = ptr; 2211 2212 switch (action) { 2213 case PRE_RATE_CHANGE: 2214 mutex_lock(&pmc->powergates_lock); 2215 break; 2216 2217 case POST_RATE_CHANGE: 2218 pmc->rate = data->new_rate; 2219 fallthrough; 2220 2221 case ABORT_RATE_CHANGE: 2222 mutex_unlock(&pmc->powergates_lock); 2223 break; 2224 2225 default: 2226 WARN_ON_ONCE(1); 2227 return notifier_from_errno(-EINVAL); 2228 } 2229 2230 return NOTIFY_OK; 2231 } 2232 2233 static void pmc_clk_fence_udelay(u32 offset) 2234 { 2235 tegra_pmc_readl(pmc, offset); 2236 /* pmc clk propagation delay 2 us */ 2237 udelay(2); 2238 } 2239 2240 static u8 pmc_clk_mux_get_parent(struct clk_hw *hw) 2241 { 2242 struct pmc_clk *clk = to_pmc_clk(hw); 2243 u32 val; 2244 2245 val = tegra_pmc_readl(pmc, clk->offs) >> clk->mux_shift; 2246 val &= PMC_CLK_OUT_MUX_MASK; 2247 2248 return val; 2249 } 2250 2251 static int pmc_clk_mux_set_parent(struct clk_hw *hw, u8 index) 2252 { 2253 struct pmc_clk *clk = to_pmc_clk(hw); 2254 u32 val; 2255 2256 val = tegra_pmc_readl(pmc, clk->offs); 2257 val &= ~(PMC_CLK_OUT_MUX_MASK << clk->mux_shift); 2258 val |= index << clk->mux_shift; 2259 tegra_pmc_writel(pmc, val, clk->offs); 2260 pmc_clk_fence_udelay(clk->offs); 2261 2262 return 0; 2263 } 2264 2265 static int pmc_clk_is_enabled(struct clk_hw *hw) 2266 { 2267 struct pmc_clk *clk = to_pmc_clk(hw); 2268 u32 val; 2269 2270 val = tegra_pmc_readl(pmc, clk->offs) & BIT(clk->force_en_shift); 2271 2272 return val ? 1 : 0; 2273 } 2274 2275 static void pmc_clk_set_state(unsigned long offs, u32 shift, int state) 2276 { 2277 u32 val; 2278 2279 val = tegra_pmc_readl(pmc, offs); 2280 val = state ? (val | BIT(shift)) : (val & ~BIT(shift)); 2281 tegra_pmc_writel(pmc, val, offs); 2282 pmc_clk_fence_udelay(offs); 2283 } 2284 2285 static int pmc_clk_enable(struct clk_hw *hw) 2286 { 2287 struct pmc_clk *clk = to_pmc_clk(hw); 2288 2289 pmc_clk_set_state(clk->offs, clk->force_en_shift, 1); 2290 2291 return 0; 2292 } 2293 2294 static void pmc_clk_disable(struct clk_hw *hw) 2295 { 2296 struct pmc_clk *clk = to_pmc_clk(hw); 2297 2298 pmc_clk_set_state(clk->offs, clk->force_en_shift, 0); 2299 } 2300 2301 static const struct clk_ops pmc_clk_ops = { 2302 .get_parent = pmc_clk_mux_get_parent, 2303 .set_parent = pmc_clk_mux_set_parent, 2304 .determine_rate = __clk_mux_determine_rate, 2305 .is_enabled = pmc_clk_is_enabled, 2306 .enable = pmc_clk_enable, 2307 .disable = pmc_clk_disable, 2308 }; 2309 2310 static struct clk * 2311 tegra_pmc_clk_out_register(struct tegra_pmc *pmc, 2312 const struct pmc_clk_init_data *data, 2313 unsigned long offset) 2314 { 2315 struct clk_init_data init; 2316 struct pmc_clk *pmc_clk; 2317 2318 pmc_clk = devm_kzalloc(pmc->dev, sizeof(*pmc_clk), GFP_KERNEL); 2319 if (!pmc_clk) 2320 return ERR_PTR(-ENOMEM); 2321 2322 init.name = data->name; 2323 init.ops = &pmc_clk_ops; 2324 init.parent_names = data->parents; 2325 init.num_parents = data->num_parents; 2326 init.flags = CLK_SET_RATE_NO_REPARENT | CLK_SET_RATE_PARENT | 2327 CLK_SET_PARENT_GATE; 2328 2329 pmc_clk->hw.init = &init; 2330 pmc_clk->offs = offset; 2331 pmc_clk->mux_shift = data->mux_shift; 2332 pmc_clk->force_en_shift = data->force_en_shift; 2333 2334 return clk_register(NULL, &pmc_clk->hw); 2335 } 2336 2337 static int pmc_clk_gate_is_enabled(struct clk_hw *hw) 2338 { 2339 struct pmc_clk_gate *gate = to_pmc_clk_gate(hw); 2340 2341 return tegra_pmc_readl(pmc, gate->offs) & BIT(gate->shift) ? 1 : 0; 2342 } 2343 2344 static int pmc_clk_gate_enable(struct clk_hw *hw) 2345 { 2346 struct pmc_clk_gate *gate = to_pmc_clk_gate(hw); 2347 2348 pmc_clk_set_state(gate->offs, gate->shift, 1); 2349 2350 return 0; 2351 } 2352 2353 static void pmc_clk_gate_disable(struct clk_hw *hw) 2354 { 2355 struct pmc_clk_gate *gate = to_pmc_clk_gate(hw); 2356 2357 pmc_clk_set_state(gate->offs, gate->shift, 0); 2358 } 2359 2360 static const struct clk_ops pmc_clk_gate_ops = { 2361 .is_enabled = pmc_clk_gate_is_enabled, 2362 .enable = pmc_clk_gate_enable, 2363 .disable = pmc_clk_gate_disable, 2364 }; 2365 2366 static struct clk * 2367 tegra_pmc_clk_gate_register(struct tegra_pmc *pmc, const char *name, 2368 const char *parent_name, unsigned long offset, 2369 u32 shift) 2370 { 2371 struct clk_init_data init; 2372 struct pmc_clk_gate *gate; 2373 2374 gate = devm_kzalloc(pmc->dev, sizeof(*gate), GFP_KERNEL); 2375 if (!gate) 2376 return ERR_PTR(-ENOMEM); 2377 2378 init.name = name; 2379 init.ops = &pmc_clk_gate_ops; 2380 init.parent_names = &parent_name; 2381 init.num_parents = 1; 2382 init.flags = 0; 2383 2384 gate->hw.init = &init; 2385 gate->offs = offset; 2386 gate->shift = shift; 2387 2388 return clk_register(NULL, &gate->hw); 2389 } 2390 2391 static void tegra_pmc_clock_register(struct tegra_pmc *pmc, 2392 struct device_node *np) 2393 { 2394 struct clk *clk; 2395 struct clk_onecell_data *clk_data; 2396 unsigned int num_clks; 2397 int i, err; 2398 2399 num_clks = pmc->soc->num_pmc_clks; 2400 if (pmc->soc->has_blink_output) 2401 num_clks += 1; 2402 2403 if (!num_clks) 2404 return; 2405 2406 clk_data = devm_kmalloc(pmc->dev, sizeof(*clk_data), GFP_KERNEL); 2407 if (!clk_data) 2408 return; 2409 2410 clk_data->clks = devm_kcalloc(pmc->dev, TEGRA_PMC_CLK_MAX, 2411 sizeof(*clk_data->clks), GFP_KERNEL); 2412 if (!clk_data->clks) 2413 return; 2414 2415 clk_data->clk_num = TEGRA_PMC_CLK_MAX; 2416 2417 for (i = 0; i < TEGRA_PMC_CLK_MAX; i++) 2418 clk_data->clks[i] = ERR_PTR(-ENOENT); 2419 2420 for (i = 0; i < pmc->soc->num_pmc_clks; i++) { 2421 const struct pmc_clk_init_data *data; 2422 2423 data = pmc->soc->pmc_clks_data + i; 2424 2425 clk = tegra_pmc_clk_out_register(pmc, data, PMC_CLK_OUT_CNTRL); 2426 if (IS_ERR(clk)) { 2427 dev_warn(pmc->dev, "unable to register clock %s: %d\n", 2428 data->name, PTR_ERR_OR_ZERO(clk)); 2429 return; 2430 } 2431 2432 err = clk_register_clkdev(clk, data->name, NULL); 2433 if (err) { 2434 dev_warn(pmc->dev, 2435 "unable to register %s clock lookup: %d\n", 2436 data->name, err); 2437 return; 2438 } 2439 2440 clk_data->clks[data->clk_id] = clk; 2441 } 2442 2443 if (pmc->soc->has_blink_output) { 2444 tegra_pmc_writel(pmc, 0x0, PMC_BLINK_TIMER); 2445 clk = tegra_pmc_clk_gate_register(pmc, 2446 "pmc_blink_override", 2447 "clk_32k", 2448 PMC_DPD_PADS_ORIDE, 2449 PMC_DPD_PADS_ORIDE_BLINK); 2450 if (IS_ERR(clk)) { 2451 dev_warn(pmc->dev, 2452 "unable to register pmc_blink_override: %d\n", 2453 PTR_ERR_OR_ZERO(clk)); 2454 return; 2455 } 2456 2457 clk = tegra_pmc_clk_gate_register(pmc, "pmc_blink", 2458 "pmc_blink_override", 2459 PMC_CNTRL, 2460 PMC_CNTRL_BLINK_EN); 2461 if (IS_ERR(clk)) { 2462 dev_warn(pmc->dev, 2463 "unable to register pmc_blink: %d\n", 2464 PTR_ERR_OR_ZERO(clk)); 2465 return; 2466 } 2467 2468 err = clk_register_clkdev(clk, "pmc_blink", NULL); 2469 if (err) { 2470 dev_warn(pmc->dev, 2471 "unable to register pmc_blink lookup: %d\n", 2472 err); 2473 return; 2474 } 2475 2476 clk_data->clks[TEGRA_PMC_CLK_BLINK] = clk; 2477 } 2478 2479 err = of_clk_add_provider(np, of_clk_src_onecell_get, clk_data); 2480 if (err) 2481 dev_warn(pmc->dev, "failed to add pmc clock provider: %d\n", 2482 err); 2483 } 2484 2485 static int tegra_pmc_probe(struct platform_device *pdev) 2486 { 2487 void __iomem *base; 2488 struct resource *res; 2489 int err; 2490 2491 /* 2492 * Early initialisation should have configured an initial 2493 * register mapping and setup the soc data pointer. If these 2494 * are not valid then something went badly wrong! 2495 */ 2496 if (WARN_ON(!pmc->base || !pmc->soc)) 2497 return -ENODEV; 2498 2499 err = tegra_pmc_parse_dt(pmc, pdev->dev.of_node); 2500 if (err < 0) 2501 return err; 2502 2503 /* take over the memory region from the early initialization */ 2504 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 2505 base = devm_ioremap_resource(&pdev->dev, res); 2506 if (IS_ERR(base)) 2507 return PTR_ERR(base); 2508 2509 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "wake"); 2510 if (res) { 2511 pmc->wake = devm_ioremap_resource(&pdev->dev, res); 2512 if (IS_ERR(pmc->wake)) 2513 return PTR_ERR(pmc->wake); 2514 } else { 2515 pmc->wake = base; 2516 } 2517 2518 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "aotag"); 2519 if (res) { 2520 pmc->aotag = devm_ioremap_resource(&pdev->dev, res); 2521 if (IS_ERR(pmc->aotag)) 2522 return PTR_ERR(pmc->aotag); 2523 } else { 2524 pmc->aotag = base; 2525 } 2526 2527 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "scratch"); 2528 if (res) { 2529 pmc->scratch = devm_ioremap_resource(&pdev->dev, res); 2530 if (IS_ERR(pmc->scratch)) 2531 return PTR_ERR(pmc->scratch); 2532 } else { 2533 pmc->scratch = base; 2534 } 2535 2536 pmc->clk = devm_clk_get(&pdev->dev, "pclk"); 2537 if (IS_ERR(pmc->clk)) { 2538 err = PTR_ERR(pmc->clk); 2539 2540 if (err != -ENOENT) { 2541 dev_err(&pdev->dev, "failed to get pclk: %d\n", err); 2542 return err; 2543 } 2544 2545 pmc->clk = NULL; 2546 } 2547 2548 /* 2549 * PCLK clock rate can't be retrieved using CLK API because it 2550 * causes lockup if CPU enters LP2 idle state from some other 2551 * CLK notifier, hence we're caching the rate's value locally. 2552 */ 2553 if (pmc->clk) { 2554 pmc->clk_nb.notifier_call = tegra_pmc_clk_notify_cb; 2555 err = clk_notifier_register(pmc->clk, &pmc->clk_nb); 2556 if (err) { 2557 dev_err(&pdev->dev, 2558 "failed to register clk notifier\n"); 2559 return err; 2560 } 2561 2562 pmc->rate = clk_get_rate(pmc->clk); 2563 } 2564 2565 pmc->dev = &pdev->dev; 2566 2567 tegra_pmc_init(pmc); 2568 2569 tegra_pmc_init_tsense_reset(pmc); 2570 2571 tegra_pmc_reset_sysfs_init(pmc); 2572 2573 if (IS_ENABLED(CONFIG_DEBUG_FS)) { 2574 err = tegra_powergate_debugfs_init(); 2575 if (err < 0) 2576 goto cleanup_sysfs; 2577 } 2578 2579 err = register_restart_handler(&tegra_pmc_restart_handler); 2580 if (err) { 2581 dev_err(&pdev->dev, "unable to register restart handler, %d\n", 2582 err); 2583 goto cleanup_debugfs; 2584 } 2585 2586 err = tegra_pmc_pinctrl_init(pmc); 2587 if (err) 2588 goto cleanup_restart_handler; 2589 2590 err = tegra_powergate_init(pmc, pdev->dev.of_node); 2591 if (err < 0) 2592 goto cleanup_powergates; 2593 2594 err = tegra_pmc_irq_init(pmc); 2595 if (err < 0) 2596 goto cleanup_powergates; 2597 2598 mutex_lock(&pmc->powergates_lock); 2599 iounmap(pmc->base); 2600 pmc->base = base; 2601 mutex_unlock(&pmc->powergates_lock); 2602 2603 tegra_pmc_clock_register(pmc, pdev->dev.of_node); 2604 platform_set_drvdata(pdev, pmc); 2605 2606 return 0; 2607 2608 cleanup_powergates: 2609 tegra_powergate_remove_all(pdev->dev.of_node); 2610 cleanup_restart_handler: 2611 unregister_restart_handler(&tegra_pmc_restart_handler); 2612 cleanup_debugfs: 2613 debugfs_remove(pmc->debugfs); 2614 cleanup_sysfs: 2615 device_remove_file(&pdev->dev, &dev_attr_reset_reason); 2616 device_remove_file(&pdev->dev, &dev_attr_reset_level); 2617 clk_notifier_unregister(pmc->clk, &pmc->clk_nb); 2618 2619 return err; 2620 } 2621 2622 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM) 2623 static int tegra_pmc_suspend(struct device *dev) 2624 { 2625 struct tegra_pmc *pmc = dev_get_drvdata(dev); 2626 2627 tegra_pmc_writel(pmc, virt_to_phys(tegra_resume), PMC_SCRATCH41); 2628 2629 return 0; 2630 } 2631 2632 static int tegra_pmc_resume(struct device *dev) 2633 { 2634 struct tegra_pmc *pmc = dev_get_drvdata(dev); 2635 2636 tegra_pmc_writel(pmc, 0x0, PMC_SCRATCH41); 2637 2638 return 0; 2639 } 2640 2641 static SIMPLE_DEV_PM_OPS(tegra_pmc_pm_ops, tegra_pmc_suspend, tegra_pmc_resume); 2642 2643 #endif 2644 2645 static const char * const tegra20_powergates[] = { 2646 [TEGRA_POWERGATE_CPU] = "cpu", 2647 [TEGRA_POWERGATE_3D] = "3d", 2648 [TEGRA_POWERGATE_VENC] = "venc", 2649 [TEGRA_POWERGATE_VDEC] = "vdec", 2650 [TEGRA_POWERGATE_PCIE] = "pcie", 2651 [TEGRA_POWERGATE_L2] = "l2", 2652 [TEGRA_POWERGATE_MPE] = "mpe", 2653 }; 2654 2655 static const struct tegra_pmc_regs tegra20_pmc_regs = { 2656 .scratch0 = 0x50, 2657 .dpd_req = 0x1b8, 2658 .dpd_status = 0x1bc, 2659 .dpd2_req = 0x1c0, 2660 .dpd2_status = 0x1c4, 2661 .rst_status = 0x1b4, 2662 .rst_source_shift = 0x0, 2663 .rst_source_mask = 0x7, 2664 .rst_level_shift = 0x0, 2665 .rst_level_mask = 0x0, 2666 }; 2667 2668 static void tegra20_pmc_init(struct tegra_pmc *pmc) 2669 { 2670 u32 value, osc, pmu, off; 2671 2672 /* Always enable CPU power request */ 2673 value = tegra_pmc_readl(pmc, PMC_CNTRL); 2674 value |= PMC_CNTRL_CPU_PWRREQ_OE; 2675 tegra_pmc_writel(pmc, value, PMC_CNTRL); 2676 2677 value = tegra_pmc_readl(pmc, PMC_CNTRL); 2678 2679 if (pmc->sysclkreq_high) 2680 value &= ~PMC_CNTRL_SYSCLK_POLARITY; 2681 else 2682 value |= PMC_CNTRL_SYSCLK_POLARITY; 2683 2684 if (pmc->corereq_high) 2685 value &= ~PMC_CNTRL_PWRREQ_POLARITY; 2686 else 2687 value |= PMC_CNTRL_PWRREQ_POLARITY; 2688 2689 /* configure the output polarity while the request is tristated */ 2690 tegra_pmc_writel(pmc, value, PMC_CNTRL); 2691 2692 /* now enable the request */ 2693 value = tegra_pmc_readl(pmc, PMC_CNTRL); 2694 value |= PMC_CNTRL_SYSCLK_OE; 2695 tegra_pmc_writel(pmc, value, PMC_CNTRL); 2696 2697 /* program core timings which are applicable only for suspend state */ 2698 if (pmc->suspend_mode != TEGRA_SUSPEND_NONE) { 2699 osc = DIV_ROUND_UP(pmc->core_osc_time * 8192, 1000000); 2700 pmu = DIV_ROUND_UP(pmc->core_pmu_time * 32768, 1000000); 2701 off = DIV_ROUND_UP(pmc->core_off_time * 32768, 1000000); 2702 tegra_pmc_writel(pmc, ((osc << 8) & 0xff00) | (pmu & 0xff), 2703 PMC_COREPWRGOOD_TIMER); 2704 tegra_pmc_writel(pmc, off, PMC_COREPWROFF_TIMER); 2705 } 2706 } 2707 2708 static void tegra20_pmc_setup_irq_polarity(struct tegra_pmc *pmc, 2709 struct device_node *np, 2710 bool invert) 2711 { 2712 u32 value; 2713 2714 value = tegra_pmc_readl(pmc, PMC_CNTRL); 2715 2716 if (invert) 2717 value |= PMC_CNTRL_INTR_POLARITY; 2718 else 2719 value &= ~PMC_CNTRL_INTR_POLARITY; 2720 2721 tegra_pmc_writel(pmc, value, PMC_CNTRL); 2722 } 2723 2724 static const struct tegra_pmc_soc tegra20_pmc_soc = { 2725 .num_powergates = ARRAY_SIZE(tegra20_powergates), 2726 .powergates = tegra20_powergates, 2727 .num_cpu_powergates = 0, 2728 .cpu_powergates = NULL, 2729 .has_tsense_reset = false, 2730 .has_gpu_clamps = false, 2731 .needs_mbist_war = false, 2732 .has_impl_33v_pwr = false, 2733 .maybe_tz_only = false, 2734 .num_io_pads = 0, 2735 .io_pads = NULL, 2736 .num_pin_descs = 0, 2737 .pin_descs = NULL, 2738 .regs = &tegra20_pmc_regs, 2739 .init = tegra20_pmc_init, 2740 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity, 2741 .reset_sources = NULL, 2742 .num_reset_sources = 0, 2743 .reset_levels = NULL, 2744 .num_reset_levels = 0, 2745 .pmc_clks_data = NULL, 2746 .num_pmc_clks = 0, 2747 .has_blink_output = true, 2748 }; 2749 2750 static const char * const tegra30_powergates[] = { 2751 [TEGRA_POWERGATE_CPU] = "cpu0", 2752 [TEGRA_POWERGATE_3D] = "3d0", 2753 [TEGRA_POWERGATE_VENC] = "venc", 2754 [TEGRA_POWERGATE_VDEC] = "vdec", 2755 [TEGRA_POWERGATE_PCIE] = "pcie", 2756 [TEGRA_POWERGATE_L2] = "l2", 2757 [TEGRA_POWERGATE_MPE] = "mpe", 2758 [TEGRA_POWERGATE_HEG] = "heg", 2759 [TEGRA_POWERGATE_SATA] = "sata", 2760 [TEGRA_POWERGATE_CPU1] = "cpu1", 2761 [TEGRA_POWERGATE_CPU2] = "cpu2", 2762 [TEGRA_POWERGATE_CPU3] = "cpu3", 2763 [TEGRA_POWERGATE_CELP] = "celp", 2764 [TEGRA_POWERGATE_3D1] = "3d1", 2765 }; 2766 2767 static const u8 tegra30_cpu_powergates[] = { 2768 TEGRA_POWERGATE_CPU, 2769 TEGRA_POWERGATE_CPU1, 2770 TEGRA_POWERGATE_CPU2, 2771 TEGRA_POWERGATE_CPU3, 2772 }; 2773 2774 static const struct tegra_pmc_soc tegra30_pmc_soc = { 2775 .num_powergates = ARRAY_SIZE(tegra30_powergates), 2776 .powergates = tegra30_powergates, 2777 .num_cpu_powergates = ARRAY_SIZE(tegra30_cpu_powergates), 2778 .cpu_powergates = tegra30_cpu_powergates, 2779 .has_tsense_reset = true, 2780 .has_gpu_clamps = false, 2781 .needs_mbist_war = false, 2782 .has_impl_33v_pwr = false, 2783 .maybe_tz_only = false, 2784 .num_io_pads = 0, 2785 .io_pads = NULL, 2786 .num_pin_descs = 0, 2787 .pin_descs = NULL, 2788 .regs = &tegra20_pmc_regs, 2789 .init = tegra20_pmc_init, 2790 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity, 2791 .reset_sources = tegra30_reset_sources, 2792 .num_reset_sources = ARRAY_SIZE(tegra30_reset_sources), 2793 .reset_levels = NULL, 2794 .num_reset_levels = 0, 2795 .pmc_clks_data = tegra_pmc_clks_data, 2796 .num_pmc_clks = ARRAY_SIZE(tegra_pmc_clks_data), 2797 .has_blink_output = true, 2798 }; 2799 2800 static const char * const tegra114_powergates[] = { 2801 [TEGRA_POWERGATE_CPU] = "crail", 2802 [TEGRA_POWERGATE_3D] = "3d", 2803 [TEGRA_POWERGATE_VENC] = "venc", 2804 [TEGRA_POWERGATE_VDEC] = "vdec", 2805 [TEGRA_POWERGATE_MPE] = "mpe", 2806 [TEGRA_POWERGATE_HEG] = "heg", 2807 [TEGRA_POWERGATE_CPU1] = "cpu1", 2808 [TEGRA_POWERGATE_CPU2] = "cpu2", 2809 [TEGRA_POWERGATE_CPU3] = "cpu3", 2810 [TEGRA_POWERGATE_CELP] = "celp", 2811 [TEGRA_POWERGATE_CPU0] = "cpu0", 2812 [TEGRA_POWERGATE_C0NC] = "c0nc", 2813 [TEGRA_POWERGATE_C1NC] = "c1nc", 2814 [TEGRA_POWERGATE_DIS] = "dis", 2815 [TEGRA_POWERGATE_DISB] = "disb", 2816 [TEGRA_POWERGATE_XUSBA] = "xusba", 2817 [TEGRA_POWERGATE_XUSBB] = "xusbb", 2818 [TEGRA_POWERGATE_XUSBC] = "xusbc", 2819 }; 2820 2821 static const u8 tegra114_cpu_powergates[] = { 2822 TEGRA_POWERGATE_CPU0, 2823 TEGRA_POWERGATE_CPU1, 2824 TEGRA_POWERGATE_CPU2, 2825 TEGRA_POWERGATE_CPU3, 2826 }; 2827 2828 static const struct tegra_pmc_soc tegra114_pmc_soc = { 2829 .num_powergates = ARRAY_SIZE(tegra114_powergates), 2830 .powergates = tegra114_powergates, 2831 .num_cpu_powergates = ARRAY_SIZE(tegra114_cpu_powergates), 2832 .cpu_powergates = tegra114_cpu_powergates, 2833 .has_tsense_reset = true, 2834 .has_gpu_clamps = false, 2835 .needs_mbist_war = false, 2836 .has_impl_33v_pwr = false, 2837 .maybe_tz_only = false, 2838 .num_io_pads = 0, 2839 .io_pads = NULL, 2840 .num_pin_descs = 0, 2841 .pin_descs = NULL, 2842 .regs = &tegra20_pmc_regs, 2843 .init = tegra20_pmc_init, 2844 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity, 2845 .reset_sources = tegra30_reset_sources, 2846 .num_reset_sources = ARRAY_SIZE(tegra30_reset_sources), 2847 .reset_levels = NULL, 2848 .num_reset_levels = 0, 2849 .pmc_clks_data = tegra_pmc_clks_data, 2850 .num_pmc_clks = ARRAY_SIZE(tegra_pmc_clks_data), 2851 .has_blink_output = true, 2852 }; 2853 2854 static const char * const tegra124_powergates[] = { 2855 [TEGRA_POWERGATE_CPU] = "crail", 2856 [TEGRA_POWERGATE_3D] = "3d", 2857 [TEGRA_POWERGATE_VENC] = "venc", 2858 [TEGRA_POWERGATE_PCIE] = "pcie", 2859 [TEGRA_POWERGATE_VDEC] = "vdec", 2860 [TEGRA_POWERGATE_MPE] = "mpe", 2861 [TEGRA_POWERGATE_HEG] = "heg", 2862 [TEGRA_POWERGATE_SATA] = "sata", 2863 [TEGRA_POWERGATE_CPU1] = "cpu1", 2864 [TEGRA_POWERGATE_CPU2] = "cpu2", 2865 [TEGRA_POWERGATE_CPU3] = "cpu3", 2866 [TEGRA_POWERGATE_CELP] = "celp", 2867 [TEGRA_POWERGATE_CPU0] = "cpu0", 2868 [TEGRA_POWERGATE_C0NC] = "c0nc", 2869 [TEGRA_POWERGATE_C1NC] = "c1nc", 2870 [TEGRA_POWERGATE_SOR] = "sor", 2871 [TEGRA_POWERGATE_DIS] = "dis", 2872 [TEGRA_POWERGATE_DISB] = "disb", 2873 [TEGRA_POWERGATE_XUSBA] = "xusba", 2874 [TEGRA_POWERGATE_XUSBB] = "xusbb", 2875 [TEGRA_POWERGATE_XUSBC] = "xusbc", 2876 [TEGRA_POWERGATE_VIC] = "vic", 2877 [TEGRA_POWERGATE_IRAM] = "iram", 2878 }; 2879 2880 static const u8 tegra124_cpu_powergates[] = { 2881 TEGRA_POWERGATE_CPU0, 2882 TEGRA_POWERGATE_CPU1, 2883 TEGRA_POWERGATE_CPU2, 2884 TEGRA_POWERGATE_CPU3, 2885 }; 2886 2887 #define TEGRA_IO_PAD(_id, _dpd, _voltage, _name) \ 2888 ((struct tegra_io_pad_soc) { \ 2889 .id = (_id), \ 2890 .dpd = (_dpd), \ 2891 .voltage = (_voltage), \ 2892 .name = (_name), \ 2893 }) 2894 2895 #define TEGRA_IO_PIN_DESC(_id, _dpd, _voltage, _name) \ 2896 ((struct pinctrl_pin_desc) { \ 2897 .number = (_id), \ 2898 .name = (_name) \ 2899 }) 2900 2901 #define TEGRA124_IO_PAD_TABLE(_pad) \ 2902 /* .id .dpd .voltage .name */ \ 2903 _pad(TEGRA_IO_PAD_AUDIO, 17, UINT_MAX, "audio"), \ 2904 _pad(TEGRA_IO_PAD_BB, 15, UINT_MAX, "bb"), \ 2905 _pad(TEGRA_IO_PAD_CAM, 36, UINT_MAX, "cam"), \ 2906 _pad(TEGRA_IO_PAD_COMP, 22, UINT_MAX, "comp"), \ 2907 _pad(TEGRA_IO_PAD_CSIA, 0, UINT_MAX, "csia"), \ 2908 _pad(TEGRA_IO_PAD_CSIB, 1, UINT_MAX, "csb"), \ 2909 _pad(TEGRA_IO_PAD_CSIE, 44, UINT_MAX, "cse"), \ 2910 _pad(TEGRA_IO_PAD_DSI, 2, UINT_MAX, "dsi"), \ 2911 _pad(TEGRA_IO_PAD_DSIB, 39, UINT_MAX, "dsib"), \ 2912 _pad(TEGRA_IO_PAD_DSIC, 40, UINT_MAX, "dsic"), \ 2913 _pad(TEGRA_IO_PAD_DSID, 41, UINT_MAX, "dsid"), \ 2914 _pad(TEGRA_IO_PAD_HDMI, 28, UINT_MAX, "hdmi"), \ 2915 _pad(TEGRA_IO_PAD_HSIC, 19, UINT_MAX, "hsic"), \ 2916 _pad(TEGRA_IO_PAD_HV, 38, UINT_MAX, "hv"), \ 2917 _pad(TEGRA_IO_PAD_LVDS, 57, UINT_MAX, "lvds"), \ 2918 _pad(TEGRA_IO_PAD_MIPI_BIAS, 3, UINT_MAX, "mipi-bias"), \ 2919 _pad(TEGRA_IO_PAD_NAND, 13, UINT_MAX, "nand"), \ 2920 _pad(TEGRA_IO_PAD_PEX_BIAS, 4, UINT_MAX, "pex-bias"), \ 2921 _pad(TEGRA_IO_PAD_PEX_CLK1, 5, UINT_MAX, "pex-clk1"), \ 2922 _pad(TEGRA_IO_PAD_PEX_CLK2, 6, UINT_MAX, "pex-clk2"), \ 2923 _pad(TEGRA_IO_PAD_PEX_CNTRL, 32, UINT_MAX, "pex-cntrl"), \ 2924 _pad(TEGRA_IO_PAD_SDMMC1, 33, UINT_MAX, "sdmmc1"), \ 2925 _pad(TEGRA_IO_PAD_SDMMC3, 34, UINT_MAX, "sdmmc3"), \ 2926 _pad(TEGRA_IO_PAD_SDMMC4, 35, UINT_MAX, "sdmmc4"), \ 2927 _pad(TEGRA_IO_PAD_SYS_DDC, 58, UINT_MAX, "sys_ddc"), \ 2928 _pad(TEGRA_IO_PAD_UART, 14, UINT_MAX, "uart"), \ 2929 _pad(TEGRA_IO_PAD_USB0, 9, UINT_MAX, "usb0"), \ 2930 _pad(TEGRA_IO_PAD_USB1, 10, UINT_MAX, "usb1"), \ 2931 _pad(TEGRA_IO_PAD_USB2, 11, UINT_MAX, "usb2"), \ 2932 _pad(TEGRA_IO_PAD_USB_BIAS, 12, UINT_MAX, "usb_bias") 2933 2934 static const struct tegra_io_pad_soc tegra124_io_pads[] = { 2935 TEGRA124_IO_PAD_TABLE(TEGRA_IO_PAD) 2936 }; 2937 2938 static const struct pinctrl_pin_desc tegra124_pin_descs[] = { 2939 TEGRA124_IO_PAD_TABLE(TEGRA_IO_PIN_DESC) 2940 }; 2941 2942 static const struct tegra_pmc_soc tegra124_pmc_soc = { 2943 .num_powergates = ARRAY_SIZE(tegra124_powergates), 2944 .powergates = tegra124_powergates, 2945 .num_cpu_powergates = ARRAY_SIZE(tegra124_cpu_powergates), 2946 .cpu_powergates = tegra124_cpu_powergates, 2947 .has_tsense_reset = true, 2948 .has_gpu_clamps = true, 2949 .needs_mbist_war = false, 2950 .has_impl_33v_pwr = false, 2951 .maybe_tz_only = false, 2952 .num_io_pads = ARRAY_SIZE(tegra124_io_pads), 2953 .io_pads = tegra124_io_pads, 2954 .num_pin_descs = ARRAY_SIZE(tegra124_pin_descs), 2955 .pin_descs = tegra124_pin_descs, 2956 .regs = &tegra20_pmc_regs, 2957 .init = tegra20_pmc_init, 2958 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity, 2959 .reset_sources = tegra30_reset_sources, 2960 .num_reset_sources = ARRAY_SIZE(tegra30_reset_sources), 2961 .reset_levels = NULL, 2962 .num_reset_levels = 0, 2963 .pmc_clks_data = tegra_pmc_clks_data, 2964 .num_pmc_clks = ARRAY_SIZE(tegra_pmc_clks_data), 2965 .has_blink_output = true, 2966 }; 2967 2968 static const char * const tegra210_powergates[] = { 2969 [TEGRA_POWERGATE_CPU] = "crail", 2970 [TEGRA_POWERGATE_3D] = "3d", 2971 [TEGRA_POWERGATE_VENC] = "venc", 2972 [TEGRA_POWERGATE_PCIE] = "pcie", 2973 [TEGRA_POWERGATE_MPE] = "mpe", 2974 [TEGRA_POWERGATE_SATA] = "sata", 2975 [TEGRA_POWERGATE_CPU1] = "cpu1", 2976 [TEGRA_POWERGATE_CPU2] = "cpu2", 2977 [TEGRA_POWERGATE_CPU3] = "cpu3", 2978 [TEGRA_POWERGATE_CPU0] = "cpu0", 2979 [TEGRA_POWERGATE_C0NC] = "c0nc", 2980 [TEGRA_POWERGATE_SOR] = "sor", 2981 [TEGRA_POWERGATE_DIS] = "dis", 2982 [TEGRA_POWERGATE_DISB] = "disb", 2983 [TEGRA_POWERGATE_XUSBA] = "xusba", 2984 [TEGRA_POWERGATE_XUSBB] = "xusbb", 2985 [TEGRA_POWERGATE_XUSBC] = "xusbc", 2986 [TEGRA_POWERGATE_VIC] = "vic", 2987 [TEGRA_POWERGATE_IRAM] = "iram", 2988 [TEGRA_POWERGATE_NVDEC] = "nvdec", 2989 [TEGRA_POWERGATE_NVJPG] = "nvjpg", 2990 [TEGRA_POWERGATE_AUD] = "aud", 2991 [TEGRA_POWERGATE_DFD] = "dfd", 2992 [TEGRA_POWERGATE_VE2] = "ve2", 2993 }; 2994 2995 static const u8 tegra210_cpu_powergates[] = { 2996 TEGRA_POWERGATE_CPU0, 2997 TEGRA_POWERGATE_CPU1, 2998 TEGRA_POWERGATE_CPU2, 2999 TEGRA_POWERGATE_CPU3, 3000 }; 3001 3002 #define TEGRA210_IO_PAD_TABLE(_pad) \ 3003 /* .id .dpd .voltage .name */ \ 3004 _pad(TEGRA_IO_PAD_AUDIO, 17, 5, "audio"), \ 3005 _pad(TEGRA_IO_PAD_AUDIO_HV, 61, 18, "audio-hv"), \ 3006 _pad(TEGRA_IO_PAD_CAM, 36, 10, "cam"), \ 3007 _pad(TEGRA_IO_PAD_CSIA, 0, UINT_MAX, "csia"), \ 3008 _pad(TEGRA_IO_PAD_CSIB, 1, UINT_MAX, "csib"), \ 3009 _pad(TEGRA_IO_PAD_CSIC, 42, UINT_MAX, "csic"), \ 3010 _pad(TEGRA_IO_PAD_CSID, 43, UINT_MAX, "csid"), \ 3011 _pad(TEGRA_IO_PAD_CSIE, 44, UINT_MAX, "csie"), \ 3012 _pad(TEGRA_IO_PAD_CSIF, 45, UINT_MAX, "csif"), \ 3013 _pad(TEGRA_IO_PAD_DBG, 25, 19, "dbg"), \ 3014 _pad(TEGRA_IO_PAD_DEBUG_NONAO, 26, UINT_MAX, "debug-nonao"), \ 3015 _pad(TEGRA_IO_PAD_DMIC, 50, 20, "dmic"), \ 3016 _pad(TEGRA_IO_PAD_DP, 51, UINT_MAX, "dp"), \ 3017 _pad(TEGRA_IO_PAD_DSI, 2, UINT_MAX, "dsi"), \ 3018 _pad(TEGRA_IO_PAD_DSIB, 39, UINT_MAX, "dsib"), \ 3019 _pad(TEGRA_IO_PAD_DSIC, 40, UINT_MAX, "dsic"), \ 3020 _pad(TEGRA_IO_PAD_DSID, 41, UINT_MAX, "dsid"), \ 3021 _pad(TEGRA_IO_PAD_EMMC, 35, UINT_MAX, "emmc"), \ 3022 _pad(TEGRA_IO_PAD_EMMC2, 37, UINT_MAX, "emmc2"), \ 3023 _pad(TEGRA_IO_PAD_GPIO, 27, 21, "gpio"), \ 3024 _pad(TEGRA_IO_PAD_HDMI, 28, UINT_MAX, "hdmi"), \ 3025 _pad(TEGRA_IO_PAD_HSIC, 19, UINT_MAX, "hsic"), \ 3026 _pad(TEGRA_IO_PAD_LVDS, 57, UINT_MAX, "lvds"), \ 3027 _pad(TEGRA_IO_PAD_MIPI_BIAS, 3, UINT_MAX, "mipi-bias"), \ 3028 _pad(TEGRA_IO_PAD_PEX_BIAS, 4, UINT_MAX, "pex-bias"), \ 3029 _pad(TEGRA_IO_PAD_PEX_CLK1, 5, UINT_MAX, "pex-clk1"), \ 3030 _pad(TEGRA_IO_PAD_PEX_CLK2, 6, UINT_MAX, "pex-clk2"), \ 3031 _pad(TEGRA_IO_PAD_PEX_CNTRL, UINT_MAX, 11, "pex-cntrl"), \ 3032 _pad(TEGRA_IO_PAD_SDMMC1, 33, 12, "sdmmc1"), \ 3033 _pad(TEGRA_IO_PAD_SDMMC3, 34, 13, "sdmmc3"), \ 3034 _pad(TEGRA_IO_PAD_SPI, 46, 22, "spi"), \ 3035 _pad(TEGRA_IO_PAD_SPI_HV, 47, 23, "spi-hv"), \ 3036 _pad(TEGRA_IO_PAD_UART, 14, 2, "uart"), \ 3037 _pad(TEGRA_IO_PAD_USB0, 9, UINT_MAX, "usb0"), \ 3038 _pad(TEGRA_IO_PAD_USB1, 10, UINT_MAX, "usb1"), \ 3039 _pad(TEGRA_IO_PAD_USB2, 11, UINT_MAX, "usb2"), \ 3040 _pad(TEGRA_IO_PAD_USB3, 18, UINT_MAX, "usb3"), \ 3041 _pad(TEGRA_IO_PAD_USB_BIAS, 12, UINT_MAX, "usb-bias") 3042 3043 static const struct tegra_io_pad_soc tegra210_io_pads[] = { 3044 TEGRA210_IO_PAD_TABLE(TEGRA_IO_PAD) 3045 }; 3046 3047 static const struct pinctrl_pin_desc tegra210_pin_descs[] = { 3048 TEGRA210_IO_PAD_TABLE(TEGRA_IO_PIN_DESC) 3049 }; 3050 3051 static const struct tegra_wake_event tegra210_wake_events[] = { 3052 TEGRA_WAKE_IRQ("rtc", 16, 2), 3053 TEGRA_WAKE_IRQ("pmu", 51, 86), 3054 }; 3055 3056 static const struct tegra_pmc_soc tegra210_pmc_soc = { 3057 .num_powergates = ARRAY_SIZE(tegra210_powergates), 3058 .powergates = tegra210_powergates, 3059 .num_cpu_powergates = ARRAY_SIZE(tegra210_cpu_powergates), 3060 .cpu_powergates = tegra210_cpu_powergates, 3061 .has_tsense_reset = true, 3062 .has_gpu_clamps = true, 3063 .needs_mbist_war = true, 3064 .has_impl_33v_pwr = false, 3065 .maybe_tz_only = true, 3066 .num_io_pads = ARRAY_SIZE(tegra210_io_pads), 3067 .io_pads = tegra210_io_pads, 3068 .num_pin_descs = ARRAY_SIZE(tegra210_pin_descs), 3069 .pin_descs = tegra210_pin_descs, 3070 .regs = &tegra20_pmc_regs, 3071 .init = tegra20_pmc_init, 3072 .setup_irq_polarity = tegra20_pmc_setup_irq_polarity, 3073 .irq_set_wake = tegra210_pmc_irq_set_wake, 3074 .irq_set_type = tegra210_pmc_irq_set_type, 3075 .reset_sources = tegra210_reset_sources, 3076 .num_reset_sources = ARRAY_SIZE(tegra210_reset_sources), 3077 .reset_levels = NULL, 3078 .num_reset_levels = 0, 3079 .num_wake_events = ARRAY_SIZE(tegra210_wake_events), 3080 .wake_events = tegra210_wake_events, 3081 .pmc_clks_data = tegra_pmc_clks_data, 3082 .num_pmc_clks = ARRAY_SIZE(tegra_pmc_clks_data), 3083 .has_blink_output = true, 3084 }; 3085 3086 #define TEGRA186_IO_PAD_TABLE(_pad) \ 3087 /* .id .dpd .voltage .name */ \ 3088 _pad(TEGRA_IO_PAD_CSIA, 0, UINT_MAX, "csia"), \ 3089 _pad(TEGRA_IO_PAD_CSIB, 1, UINT_MAX, "csib"), \ 3090 _pad(TEGRA_IO_PAD_DSI, 2, UINT_MAX, "dsi"), \ 3091 _pad(TEGRA_IO_PAD_MIPI_BIAS, 3, UINT_MAX, "mipi-bias"), \ 3092 _pad(TEGRA_IO_PAD_PEX_CLK_BIAS, 4, UINT_MAX, "pex-clk-bias"), \ 3093 _pad(TEGRA_IO_PAD_PEX_CLK3, 5, UINT_MAX, "pex-clk3"), \ 3094 _pad(TEGRA_IO_PAD_PEX_CLK2, 6, UINT_MAX, "pex-clk2"), \ 3095 _pad(TEGRA_IO_PAD_PEX_CLK1, 7, UINT_MAX, "pex-clk1"), \ 3096 _pad(TEGRA_IO_PAD_USB0, 9, UINT_MAX, "usb0"), \ 3097 _pad(TEGRA_IO_PAD_USB1, 10, UINT_MAX, "usb1"), \ 3098 _pad(TEGRA_IO_PAD_USB2, 11, UINT_MAX, "usb2"), \ 3099 _pad(TEGRA_IO_PAD_USB_BIAS, 12, UINT_MAX, "usb-bias"), \ 3100 _pad(TEGRA_IO_PAD_UART, 14, UINT_MAX, "uart"), \ 3101 _pad(TEGRA_IO_PAD_AUDIO, 17, UINT_MAX, "audio"), \ 3102 _pad(TEGRA_IO_PAD_HSIC, 19, UINT_MAX, "hsic"), \ 3103 _pad(TEGRA_IO_PAD_DBG, 25, UINT_MAX, "dbg"), \ 3104 _pad(TEGRA_IO_PAD_HDMI_DP0, 28, UINT_MAX, "hdmi-dp0"), \ 3105 _pad(TEGRA_IO_PAD_HDMI_DP1, 29, UINT_MAX, "hdmi-dp1"), \ 3106 _pad(TEGRA_IO_PAD_PEX_CNTRL, 32, UINT_MAX, "pex-cntrl"), \ 3107 _pad(TEGRA_IO_PAD_SDMMC2_HV, 34, 5, "sdmmc2-hv"), \ 3108 _pad(TEGRA_IO_PAD_SDMMC4, 36, UINT_MAX, "sdmmc4"), \ 3109 _pad(TEGRA_IO_PAD_CAM, 38, UINT_MAX, "cam"), \ 3110 _pad(TEGRA_IO_PAD_DSIB, 40, UINT_MAX, "dsib"), \ 3111 _pad(TEGRA_IO_PAD_DSIC, 41, UINT_MAX, "dsic"), \ 3112 _pad(TEGRA_IO_PAD_DSID, 42, UINT_MAX, "dsid"), \ 3113 _pad(TEGRA_IO_PAD_CSIC, 43, UINT_MAX, "csic"), \ 3114 _pad(TEGRA_IO_PAD_CSID, 44, UINT_MAX, "csid"), \ 3115 _pad(TEGRA_IO_PAD_CSIE, 45, UINT_MAX, "csie"), \ 3116 _pad(TEGRA_IO_PAD_CSIF, 46, UINT_MAX, "csif"), \ 3117 _pad(TEGRA_IO_PAD_SPI, 47, UINT_MAX, "spi"), \ 3118 _pad(TEGRA_IO_PAD_UFS, 49, UINT_MAX, "ufs"), \ 3119 _pad(TEGRA_IO_PAD_DMIC_HV, 52, 2, "dmic-hv"), \ 3120 _pad(TEGRA_IO_PAD_EDP, 53, UINT_MAX, "edp"), \ 3121 _pad(TEGRA_IO_PAD_SDMMC1_HV, 55, 4, "sdmmc1-hv"), \ 3122 _pad(TEGRA_IO_PAD_SDMMC3_HV, 56, 6, "sdmmc3-hv"), \ 3123 _pad(TEGRA_IO_PAD_CONN, 60, UINT_MAX, "conn"), \ 3124 _pad(TEGRA_IO_PAD_AUDIO_HV, 61, 1, "audio-hv"), \ 3125 _pad(TEGRA_IO_PAD_AO_HV, UINT_MAX, 0, "ao-hv") 3126 3127 static const struct tegra_io_pad_soc tegra186_io_pads[] = { 3128 TEGRA186_IO_PAD_TABLE(TEGRA_IO_PAD) 3129 }; 3130 3131 static const struct pinctrl_pin_desc tegra186_pin_descs[] = { 3132 TEGRA186_IO_PAD_TABLE(TEGRA_IO_PIN_DESC) 3133 }; 3134 3135 static const struct tegra_pmc_regs tegra186_pmc_regs = { 3136 .scratch0 = 0x2000, 3137 .dpd_req = 0x74, 3138 .dpd_status = 0x78, 3139 .dpd2_req = 0x7c, 3140 .dpd2_status = 0x80, 3141 .rst_status = 0x70, 3142 .rst_source_shift = 0x2, 3143 .rst_source_mask = 0x3c, 3144 .rst_level_shift = 0x0, 3145 .rst_level_mask = 0x3, 3146 }; 3147 3148 static void tegra186_pmc_setup_irq_polarity(struct tegra_pmc *pmc, 3149 struct device_node *np, 3150 bool invert) 3151 { 3152 struct resource regs; 3153 void __iomem *wake; 3154 u32 value; 3155 int index; 3156 3157 index = of_property_match_string(np, "reg-names", "wake"); 3158 if (index < 0) { 3159 dev_err(pmc->dev, "failed to find PMC wake registers\n"); 3160 return; 3161 } 3162 3163 of_address_to_resource(np, index, ®s); 3164 3165 wake = ioremap(regs.start, resource_size(®s)); 3166 if (!wake) { 3167 dev_err(pmc->dev, "failed to map PMC wake registers\n"); 3168 return; 3169 } 3170 3171 value = readl(wake + WAKE_AOWAKE_CTRL); 3172 3173 if (invert) 3174 value |= WAKE_AOWAKE_CTRL_INTR_POLARITY; 3175 else 3176 value &= ~WAKE_AOWAKE_CTRL_INTR_POLARITY; 3177 3178 writel(value, wake + WAKE_AOWAKE_CTRL); 3179 3180 iounmap(wake); 3181 } 3182 3183 static const struct tegra_wake_event tegra186_wake_events[] = { 3184 TEGRA_WAKE_IRQ("pmu", 24, 209), 3185 TEGRA_WAKE_GPIO("power", 29, 1, TEGRA186_AON_GPIO(FF, 0)), 3186 TEGRA_WAKE_IRQ("rtc", 73, 10), 3187 }; 3188 3189 static const struct tegra_pmc_soc tegra186_pmc_soc = { 3190 .num_powergates = 0, 3191 .powergates = NULL, 3192 .num_cpu_powergates = 0, 3193 .cpu_powergates = NULL, 3194 .has_tsense_reset = false, 3195 .has_gpu_clamps = false, 3196 .needs_mbist_war = false, 3197 .has_impl_33v_pwr = true, 3198 .maybe_tz_only = false, 3199 .num_io_pads = ARRAY_SIZE(tegra186_io_pads), 3200 .io_pads = tegra186_io_pads, 3201 .num_pin_descs = ARRAY_SIZE(tegra186_pin_descs), 3202 .pin_descs = tegra186_pin_descs, 3203 .regs = &tegra186_pmc_regs, 3204 .init = NULL, 3205 .setup_irq_polarity = tegra186_pmc_setup_irq_polarity, 3206 .irq_set_wake = tegra186_pmc_irq_set_wake, 3207 .irq_set_type = tegra186_pmc_irq_set_type, 3208 .reset_sources = tegra186_reset_sources, 3209 .num_reset_sources = ARRAY_SIZE(tegra186_reset_sources), 3210 .reset_levels = tegra186_reset_levels, 3211 .num_reset_levels = ARRAY_SIZE(tegra186_reset_levels), 3212 .num_wake_events = ARRAY_SIZE(tegra186_wake_events), 3213 .wake_events = tegra186_wake_events, 3214 .pmc_clks_data = NULL, 3215 .num_pmc_clks = 0, 3216 .has_blink_output = false, 3217 }; 3218 3219 #define TEGRA194_IO_PAD_TABLE(_pad) \ 3220 /* .id .dpd .voltage .name */ \ 3221 _pad(TEGRA_IO_PAD_CSIA, 0, UINT_MAX, "csia"), \ 3222 _pad(TEGRA_IO_PAD_CSIB, 1, UINT_MAX, "csib"), \ 3223 _pad(TEGRA_IO_PAD_MIPI_BIAS, 3, UINT_MAX, "mipi-bias"), \ 3224 _pad(TEGRA_IO_PAD_PEX_CLK_BIAS, 4, UINT_MAX, "pex-clk-bias"), \ 3225 _pad(TEGRA_IO_PAD_PEX_CLK3, 5, UINT_MAX, "pex-clk3"), \ 3226 _pad(TEGRA_IO_PAD_PEX_CLK2, 6, UINT_MAX, "pex-clk2"), \ 3227 _pad(TEGRA_IO_PAD_PEX_CLK1, 7, UINT_MAX, "pex-clk1"), \ 3228 _pad(TEGRA_IO_PAD_EQOS, 8, UINT_MAX, "eqos"), \ 3229 _pad(TEGRA_IO_PAD_PEX_CLK_2_BIAS, 9, UINT_MAX, "pex-clk-2-bias"), \ 3230 _pad(TEGRA_IO_PAD_PEX_CLK_2, 10, UINT_MAX, "pex-clk-2"), \ 3231 _pad(TEGRA_IO_PAD_DAP3, 11, UINT_MAX, "dap3"), \ 3232 _pad(TEGRA_IO_PAD_DAP5, 12, UINT_MAX, "dap5"), \ 3233 _pad(TEGRA_IO_PAD_UART, 14, UINT_MAX, "uart"), \ 3234 _pad(TEGRA_IO_PAD_PWR_CTL, 15, UINT_MAX, "pwr-ctl"), \ 3235 _pad(TEGRA_IO_PAD_SOC_GPIO53, 16, UINT_MAX, "soc-gpio53"), \ 3236 _pad(TEGRA_IO_PAD_AUDIO, 17, UINT_MAX, "audio"), \ 3237 _pad(TEGRA_IO_PAD_GP_PWM2, 18, UINT_MAX, "gp-pwm2"), \ 3238 _pad(TEGRA_IO_PAD_GP_PWM3, 19, UINT_MAX, "gp-pwm3"), \ 3239 _pad(TEGRA_IO_PAD_SOC_GPIO12, 20, UINT_MAX, "soc-gpio12"), \ 3240 _pad(TEGRA_IO_PAD_SOC_GPIO13, 21, UINT_MAX, "soc-gpio13"), \ 3241 _pad(TEGRA_IO_PAD_SOC_GPIO10, 22, UINT_MAX, "soc-gpio10"), \ 3242 _pad(TEGRA_IO_PAD_UART4, 23, UINT_MAX, "uart4"), \ 3243 _pad(TEGRA_IO_PAD_UART5, 24, UINT_MAX, "uart5"), \ 3244 _pad(TEGRA_IO_PAD_DBG, 25, UINT_MAX, "dbg"), \ 3245 _pad(TEGRA_IO_PAD_HDMI_DP3, 26, UINT_MAX, "hdmi-dp3"), \ 3246 _pad(TEGRA_IO_PAD_HDMI_DP2, 27, UINT_MAX, "hdmi-dp2"), \ 3247 _pad(TEGRA_IO_PAD_HDMI_DP0, 28, UINT_MAX, "hdmi-dp0"), \ 3248 _pad(TEGRA_IO_PAD_HDMI_DP1, 29, UINT_MAX, "hdmi-dp1"), \ 3249 _pad(TEGRA_IO_PAD_PEX_CNTRL, 32, UINT_MAX, "pex-cntrl"), \ 3250 _pad(TEGRA_IO_PAD_PEX_CTL2, 33, UINT_MAX, "pex-ctl2"), \ 3251 _pad(TEGRA_IO_PAD_PEX_L0_RST_N, 34, UINT_MAX, "pex-l0-rst"), \ 3252 _pad(TEGRA_IO_PAD_PEX_L1_RST_N, 35, UINT_MAX, "pex-l1-rst"), \ 3253 _pad(TEGRA_IO_PAD_SDMMC4, 36, UINT_MAX, "sdmmc4"), \ 3254 _pad(TEGRA_IO_PAD_PEX_L5_RST_N, 37, UINT_MAX, "pex-l5-rst"), \ 3255 _pad(TEGRA_IO_PAD_CAM, 38, UINT_MAX, "cam"), \ 3256 _pad(TEGRA_IO_PAD_CSIC, 43, UINT_MAX, "csic"), \ 3257 _pad(TEGRA_IO_PAD_CSID, 44, UINT_MAX, "csid"), \ 3258 _pad(TEGRA_IO_PAD_CSIE, 45, UINT_MAX, "csie"), \ 3259 _pad(TEGRA_IO_PAD_CSIF, 46, UINT_MAX, "csif"), \ 3260 _pad(TEGRA_IO_PAD_SPI, 47, UINT_MAX, "spi"), \ 3261 _pad(TEGRA_IO_PAD_UFS, 49, UINT_MAX, "ufs"), \ 3262 _pad(TEGRA_IO_PAD_CSIG, 50, UINT_MAX, "csig"), \ 3263 _pad(TEGRA_IO_PAD_CSIH, 51, UINT_MAX, "csih"), \ 3264 _pad(TEGRA_IO_PAD_EDP, 53, UINT_MAX, "edp"), \ 3265 _pad(TEGRA_IO_PAD_SDMMC1_HV, 55, 4, "sdmmc1-hv"), \ 3266 _pad(TEGRA_IO_PAD_SDMMC3_HV, 56, 6, "sdmmc3-hv"), \ 3267 _pad(TEGRA_IO_PAD_CONN, 60, UINT_MAX, "conn"), \ 3268 _pad(TEGRA_IO_PAD_AUDIO_HV, 61, 1, "audio-hv"), \ 3269 _pad(TEGRA_IO_PAD_AO_HV, UINT_MAX, 0, "ao-hv") 3270 3271 static const struct tegra_io_pad_soc tegra194_io_pads[] = { 3272 TEGRA194_IO_PAD_TABLE(TEGRA_IO_PAD) 3273 }; 3274 3275 static const struct pinctrl_pin_desc tegra194_pin_descs[] = { 3276 TEGRA194_IO_PAD_TABLE(TEGRA_IO_PIN_DESC) 3277 }; 3278 3279 static const struct tegra_pmc_regs tegra194_pmc_regs = { 3280 .scratch0 = 0x2000, 3281 .dpd_req = 0x74, 3282 .dpd_status = 0x78, 3283 .dpd2_req = 0x7c, 3284 .dpd2_status = 0x80, 3285 .rst_status = 0x70, 3286 .rst_source_shift = 0x2, 3287 .rst_source_mask = 0x7c, 3288 .rst_level_shift = 0x0, 3289 .rst_level_mask = 0x3, 3290 }; 3291 3292 static const char * const tegra194_reset_sources[] = { 3293 "SYS_RESET_N", 3294 "AOWDT", 3295 "BCCPLEXWDT", 3296 "BPMPWDT", 3297 "SCEWDT", 3298 "SPEWDT", 3299 "APEWDT", 3300 "LCCPLEXWDT", 3301 "SENSOR", 3302 "AOTAG", 3303 "VFSENSOR", 3304 "MAINSWRST", 3305 "SC7", 3306 "HSM", 3307 "CSITE", 3308 "RCEWDT", 3309 "PVA0WDT", 3310 "PVA1WDT", 3311 "L1A_ASYNC", 3312 "BPMPBOOT", 3313 "FUSECRC", 3314 }; 3315 3316 static const struct tegra_wake_event tegra194_wake_events[] = { 3317 TEGRA_WAKE_IRQ("pmu", 24, 209), 3318 TEGRA_WAKE_GPIO("power", 29, 1, TEGRA194_AON_GPIO(EE, 4)), 3319 TEGRA_WAKE_IRQ("rtc", 73, 10), 3320 }; 3321 3322 static const struct tegra_pmc_soc tegra194_pmc_soc = { 3323 .num_powergates = 0, 3324 .powergates = NULL, 3325 .num_cpu_powergates = 0, 3326 .cpu_powergates = NULL, 3327 .has_tsense_reset = false, 3328 .has_gpu_clamps = false, 3329 .needs_mbist_war = false, 3330 .has_impl_33v_pwr = true, 3331 .maybe_tz_only = false, 3332 .num_io_pads = ARRAY_SIZE(tegra194_io_pads), 3333 .io_pads = tegra194_io_pads, 3334 .num_pin_descs = ARRAY_SIZE(tegra194_pin_descs), 3335 .pin_descs = tegra194_pin_descs, 3336 .regs = &tegra194_pmc_regs, 3337 .init = NULL, 3338 .setup_irq_polarity = tegra186_pmc_setup_irq_polarity, 3339 .irq_set_wake = tegra186_pmc_irq_set_wake, 3340 .irq_set_type = tegra186_pmc_irq_set_type, 3341 .reset_sources = tegra194_reset_sources, 3342 .num_reset_sources = ARRAY_SIZE(tegra194_reset_sources), 3343 .reset_levels = tegra186_reset_levels, 3344 .num_reset_levels = ARRAY_SIZE(tegra186_reset_levels), 3345 .num_wake_events = ARRAY_SIZE(tegra194_wake_events), 3346 .wake_events = tegra194_wake_events, 3347 .pmc_clks_data = NULL, 3348 .num_pmc_clks = 0, 3349 .has_blink_output = false, 3350 }; 3351 3352 static const struct of_device_id tegra_pmc_match[] = { 3353 { .compatible = "nvidia,tegra194-pmc", .data = &tegra194_pmc_soc }, 3354 { .compatible = "nvidia,tegra186-pmc", .data = &tegra186_pmc_soc }, 3355 { .compatible = "nvidia,tegra210-pmc", .data = &tegra210_pmc_soc }, 3356 { .compatible = "nvidia,tegra132-pmc", .data = &tegra124_pmc_soc }, 3357 { .compatible = "nvidia,tegra124-pmc", .data = &tegra124_pmc_soc }, 3358 { .compatible = "nvidia,tegra114-pmc", .data = &tegra114_pmc_soc }, 3359 { .compatible = "nvidia,tegra30-pmc", .data = &tegra30_pmc_soc }, 3360 { .compatible = "nvidia,tegra20-pmc", .data = &tegra20_pmc_soc }, 3361 { } 3362 }; 3363 3364 static struct platform_driver tegra_pmc_driver = { 3365 .driver = { 3366 .name = "tegra-pmc", 3367 .suppress_bind_attrs = true, 3368 .of_match_table = tegra_pmc_match, 3369 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM) 3370 .pm = &tegra_pmc_pm_ops, 3371 #endif 3372 }, 3373 .probe = tegra_pmc_probe, 3374 }; 3375 builtin_platform_driver(tegra_pmc_driver); 3376 3377 static bool __init tegra_pmc_detect_tz_only(struct tegra_pmc *pmc) 3378 { 3379 u32 value, saved; 3380 3381 saved = readl(pmc->base + pmc->soc->regs->scratch0); 3382 value = saved ^ 0xffffffff; 3383 3384 if (value == 0xffffffff) 3385 value = 0xdeadbeef; 3386 3387 /* write pattern and read it back */ 3388 writel(value, pmc->base + pmc->soc->regs->scratch0); 3389 value = readl(pmc->base + pmc->soc->regs->scratch0); 3390 3391 /* if we read all-zeroes, access is restricted to TZ only */ 3392 if (value == 0) { 3393 pr_info("access to PMC is restricted to TZ\n"); 3394 return true; 3395 } 3396 3397 /* restore original value */ 3398 writel(saved, pmc->base + pmc->soc->regs->scratch0); 3399 3400 return false; 3401 } 3402 3403 /* 3404 * Early initialization to allow access to registers in the very early boot 3405 * process. 3406 */ 3407 static int __init tegra_pmc_early_init(void) 3408 { 3409 const struct of_device_id *match; 3410 struct device_node *np; 3411 struct resource regs; 3412 unsigned int i; 3413 bool invert; 3414 3415 mutex_init(&pmc->powergates_lock); 3416 3417 np = of_find_matching_node_and_match(NULL, tegra_pmc_match, &match); 3418 if (!np) { 3419 /* 3420 * Fall back to legacy initialization for 32-bit ARM only. All 3421 * 64-bit ARM device tree files for Tegra are required to have 3422 * a PMC node. 3423 * 3424 * This is for backwards-compatibility with old device trees 3425 * that didn't contain a PMC node. Note that in this case the 3426 * SoC data can't be matched and therefore powergating is 3427 * disabled. 3428 */ 3429 if (IS_ENABLED(CONFIG_ARM) && soc_is_tegra()) { 3430 pr_warn("DT node not found, powergating disabled\n"); 3431 3432 regs.start = 0x7000e400; 3433 regs.end = 0x7000e7ff; 3434 regs.flags = IORESOURCE_MEM; 3435 3436 pr_warn("Using memory region %pR\n", ®s); 3437 } else { 3438 /* 3439 * At this point we're not running on Tegra, so play 3440 * nice with multi-platform kernels. 3441 */ 3442 return 0; 3443 } 3444 } else { 3445 /* 3446 * Extract information from the device tree if we've found a 3447 * matching node. 3448 */ 3449 if (of_address_to_resource(np, 0, ®s) < 0) { 3450 pr_err("failed to get PMC registers\n"); 3451 of_node_put(np); 3452 return -ENXIO; 3453 } 3454 } 3455 3456 pmc->base = ioremap(regs.start, resource_size(®s)); 3457 if (!pmc->base) { 3458 pr_err("failed to map PMC registers\n"); 3459 of_node_put(np); 3460 return -ENXIO; 3461 } 3462 3463 if (np) { 3464 pmc->soc = match->data; 3465 3466 if (pmc->soc->maybe_tz_only) 3467 pmc->tz_only = tegra_pmc_detect_tz_only(pmc); 3468 3469 /* Create a bitmap of the available and valid partitions */ 3470 for (i = 0; i < pmc->soc->num_powergates; i++) 3471 if (pmc->soc->powergates[i]) 3472 set_bit(i, pmc->powergates_available); 3473 3474 /* 3475 * Invert the interrupt polarity if a PMC device tree node 3476 * exists and contains the nvidia,invert-interrupt property. 3477 */ 3478 invert = of_property_read_bool(np, "nvidia,invert-interrupt"); 3479 3480 pmc->soc->setup_irq_polarity(pmc, np, invert); 3481 3482 of_node_put(np); 3483 } 3484 3485 return 0; 3486 } 3487 early_initcall(tegra_pmc_early_init); 3488