pmc.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (C) 2012,2013 NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/io.h>
  19. #include <linux/of.h>
  20. #include <linux/of_address.h>
  21. #define PMC_CTRL 0x0
  22. #define PMC_CTRL_INTR_LOW (1 << 17)
  23. #define PMC_PWRGATE_TOGGLE 0x30
  24. #define PMC_PWRGATE_TOGGLE_START (1 << 8)
  25. #define PMC_REMOVE_CLAMPING 0x34
  26. #define PMC_PWRGATE_STATUS 0x38
  27. #define TEGRA_POWERGATE_PCIE 3
  28. #define TEGRA_POWERGATE_VDEC 4
  29. #define TEGRA_POWERGATE_CPU1 9
  30. #define TEGRA_POWERGATE_CPU2 10
  31. #define TEGRA_POWERGATE_CPU3 11
  32. static u8 tegra_cpu_domains[] = {
  33. 0xFF, /* not available for CPU0 */
  34. TEGRA_POWERGATE_CPU1,
  35. TEGRA_POWERGATE_CPU2,
  36. TEGRA_POWERGATE_CPU3,
  37. };
  38. static DEFINE_SPINLOCK(tegra_powergate_lock);
  39. static void __iomem *tegra_pmc_base;
  40. static bool tegra_pmc_invert_interrupt;
  41. static inline u32 tegra_pmc_readl(u32 reg)
  42. {
  43. return readl(tegra_pmc_base + reg);
  44. }
  45. static inline void tegra_pmc_writel(u32 val, u32 reg)
  46. {
  47. writel(val, tegra_pmc_base + reg);
  48. }
  49. static int tegra_pmc_get_cpu_powerdomain_id(int cpuid)
  50. {
  51. if (cpuid <= 0 || cpuid >= num_possible_cpus())
  52. return -EINVAL;
  53. return tegra_cpu_domains[cpuid];
  54. }
  55. static bool tegra_pmc_powergate_is_powered(int id)
  56. {
  57. return (tegra_pmc_readl(PMC_PWRGATE_STATUS) >> id) & 1;
  58. }
  59. static int tegra_pmc_powergate_set(int id, bool new_state)
  60. {
  61. bool old_state;
  62. unsigned long flags;
  63. spin_lock_irqsave(&tegra_powergate_lock, flags);
  64. old_state = tegra_pmc_powergate_is_powered(id);
  65. WARN_ON(old_state == new_state);
  66. tegra_pmc_writel(PMC_PWRGATE_TOGGLE_START | id, PMC_PWRGATE_TOGGLE);
  67. spin_unlock_irqrestore(&tegra_powergate_lock, flags);
  68. return 0;
  69. }
  70. static int tegra_pmc_powergate_remove_clamping(int id)
  71. {
  72. u32 mask;
  73. /*
  74. * Tegra has a bug where PCIE and VDE clamping masks are
  75. * swapped relatively to the partition ids.
  76. */
  77. if (id == TEGRA_POWERGATE_VDEC)
  78. mask = (1 << TEGRA_POWERGATE_PCIE);
  79. else if (id == TEGRA_POWERGATE_PCIE)
  80. mask = (1 << TEGRA_POWERGATE_VDEC);
  81. else
  82. mask = (1 << id);
  83. tegra_pmc_writel(mask, PMC_REMOVE_CLAMPING);
  84. return 0;
  85. }
  86. bool tegra_pmc_cpu_is_powered(int cpuid)
  87. {
  88. int id;
  89. id = tegra_pmc_get_cpu_powerdomain_id(cpuid);
  90. if (id < 0)
  91. return false;
  92. return tegra_pmc_powergate_is_powered(id);
  93. }
  94. int tegra_pmc_cpu_power_on(int cpuid)
  95. {
  96. int id;
  97. id = tegra_pmc_get_cpu_powerdomain_id(cpuid);
  98. if (id < 0)
  99. return id;
  100. return tegra_pmc_powergate_set(id, true);
  101. }
  102. int tegra_pmc_cpu_remove_clamping(int cpuid)
  103. {
  104. int id;
  105. id = tegra_pmc_get_cpu_powerdomain_id(cpuid);
  106. if (id < 0)
  107. return id;
  108. return tegra_pmc_powergate_remove_clamping(id);
  109. }
  110. static const struct of_device_id matches[] __initconst = {
  111. { .compatible = "nvidia,tegra114-pmc" },
  112. { .compatible = "nvidia,tegra30-pmc" },
  113. { .compatible = "nvidia,tegra20-pmc" },
  114. { }
  115. };
  116. static void tegra_pmc_parse_dt(void)
  117. {
  118. struct device_node *np;
  119. np = of_find_matching_node(NULL, matches);
  120. BUG_ON(!np);
  121. tegra_pmc_base = of_iomap(np, 0);
  122. tegra_pmc_invert_interrupt = of_property_read_bool(np,
  123. "nvidia,invert-interrupt");
  124. }
  125. void __init tegra_pmc_init(void)
  126. {
  127. u32 val;
  128. tegra_pmc_parse_dt();
  129. val = tegra_pmc_readl(PMC_CTRL);
  130. if (tegra_pmc_invert_interrupt)
  131. val |= PMC_CTRL_INTR_LOW;
  132. else
  133. val &= ~PMC_CTRL_INTR_LOW;
  134. tegra_pmc_writel(val, PMC_CTRL);
  135. }