pmc.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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/clk.h>
  19. #include <linux/io.h>
  20. #include <linux/of.h>
  21. #include <linux/of_address.h>
  22. #include "pmc.h"
  23. #define PMC_CTRL 0x0
  24. #define PMC_CTRL_INTR_LOW (1 << 17)
  25. #define PMC_PWRGATE_TOGGLE 0x30
  26. #define PMC_PWRGATE_TOGGLE_START (1 << 8)
  27. #define PMC_REMOVE_CLAMPING 0x34
  28. #define PMC_PWRGATE_STATUS 0x38
  29. #define PMC_CPUPWRGOOD_TIMER 0xc8
  30. #define PMC_CPUPWROFF_TIMER 0xcc
  31. #define TEGRA_POWERGATE_PCIE 3
  32. #define TEGRA_POWERGATE_VDEC 4
  33. #define TEGRA_POWERGATE_CPU1 9
  34. #define TEGRA_POWERGATE_CPU2 10
  35. #define TEGRA_POWERGATE_CPU3 11
  36. static u8 tegra_cpu_domains[] = {
  37. 0xFF, /* not available for CPU0 */
  38. TEGRA_POWERGATE_CPU1,
  39. TEGRA_POWERGATE_CPU2,
  40. TEGRA_POWERGATE_CPU3,
  41. };
  42. static DEFINE_SPINLOCK(tegra_powergate_lock);
  43. static void __iomem *tegra_pmc_base;
  44. static bool tegra_pmc_invert_interrupt;
  45. static struct clk *tegra_pclk;
  46. struct pmc_pm_data {
  47. u32 cpu_good_time; /* CPU power good time in uS */
  48. u32 cpu_off_time; /* CPU power off time in uS */
  49. u32 core_osc_time; /* Core power good osc time in uS */
  50. u32 core_pmu_time; /* Core power good pmu time in uS */
  51. u32 core_off_time; /* Core power off time in uS */
  52. bool corereq_high; /* Core power request active-high */
  53. bool sysclkreq_high; /* System clock request active-high */
  54. bool combined_req; /* Combined pwr req for CPU & Core */
  55. bool cpu_pwr_good_en; /* CPU power good signal is enabled */
  56. u32 lp0_vec_phy_addr; /* The phy addr of LP0 warm boot code */
  57. u32 lp0_vec_size; /* The size of LP0 warm boot code */
  58. enum tegra_suspend_mode suspend_mode;
  59. };
  60. static struct pmc_pm_data pmc_pm_data;
  61. static inline u32 tegra_pmc_readl(u32 reg)
  62. {
  63. return readl(tegra_pmc_base + reg);
  64. }
  65. static inline void tegra_pmc_writel(u32 val, u32 reg)
  66. {
  67. writel(val, tegra_pmc_base + reg);
  68. }
  69. static int tegra_pmc_get_cpu_powerdomain_id(int cpuid)
  70. {
  71. if (cpuid <= 0 || cpuid >= num_possible_cpus())
  72. return -EINVAL;
  73. return tegra_cpu_domains[cpuid];
  74. }
  75. static bool tegra_pmc_powergate_is_powered(int id)
  76. {
  77. return (tegra_pmc_readl(PMC_PWRGATE_STATUS) >> id) & 1;
  78. }
  79. static int tegra_pmc_powergate_set(int id, bool new_state)
  80. {
  81. bool old_state;
  82. unsigned long flags;
  83. spin_lock_irqsave(&tegra_powergate_lock, flags);
  84. old_state = tegra_pmc_powergate_is_powered(id);
  85. WARN_ON(old_state == new_state);
  86. tegra_pmc_writel(PMC_PWRGATE_TOGGLE_START | id, PMC_PWRGATE_TOGGLE);
  87. spin_unlock_irqrestore(&tegra_powergate_lock, flags);
  88. return 0;
  89. }
  90. static int tegra_pmc_powergate_remove_clamping(int id)
  91. {
  92. u32 mask;
  93. /*
  94. * Tegra has a bug where PCIE and VDE clamping masks are
  95. * swapped relatively to the partition ids.
  96. */
  97. if (id == TEGRA_POWERGATE_VDEC)
  98. mask = (1 << TEGRA_POWERGATE_PCIE);
  99. else if (id == TEGRA_POWERGATE_PCIE)
  100. mask = (1 << TEGRA_POWERGATE_VDEC);
  101. else
  102. mask = (1 << id);
  103. tegra_pmc_writel(mask, PMC_REMOVE_CLAMPING);
  104. return 0;
  105. }
  106. bool tegra_pmc_cpu_is_powered(int cpuid)
  107. {
  108. int id;
  109. id = tegra_pmc_get_cpu_powerdomain_id(cpuid);
  110. if (id < 0)
  111. return false;
  112. return tegra_pmc_powergate_is_powered(id);
  113. }
  114. int tegra_pmc_cpu_power_on(int cpuid)
  115. {
  116. int id;
  117. id = tegra_pmc_get_cpu_powerdomain_id(cpuid);
  118. if (id < 0)
  119. return id;
  120. return tegra_pmc_powergate_set(id, true);
  121. }
  122. int tegra_pmc_cpu_remove_clamping(int cpuid)
  123. {
  124. int id;
  125. id = tegra_pmc_get_cpu_powerdomain_id(cpuid);
  126. if (id < 0)
  127. return id;
  128. return tegra_pmc_powergate_remove_clamping(id);
  129. }
  130. #ifdef CONFIG_PM_SLEEP
  131. void set_power_timers(unsigned long us_on, unsigned long us_off)
  132. {
  133. unsigned long long ticks;
  134. unsigned long long pclk;
  135. unsigned long rate;
  136. static unsigned long tegra_last_pclk;
  137. rate = clk_get_rate(tegra_pclk);
  138. if (WARN_ON_ONCE(rate <= 0))
  139. pclk = 100000000;
  140. else
  141. pclk = rate;
  142. if ((rate != tegra_last_pclk)) {
  143. ticks = (us_on * pclk) + 999999ull;
  144. do_div(ticks, 1000000);
  145. tegra_pmc_writel((unsigned long)ticks, PMC_CPUPWRGOOD_TIMER);
  146. ticks = (us_off * pclk) + 999999ull;
  147. do_div(ticks, 1000000);
  148. tegra_pmc_writel((unsigned long)ticks, PMC_CPUPWROFF_TIMER);
  149. wmb();
  150. }
  151. tegra_last_pclk = pclk;
  152. }
  153. #endif
  154. static const struct of_device_id matches[] __initconst = {
  155. { .compatible = "nvidia,tegra114-pmc" },
  156. { .compatible = "nvidia,tegra30-pmc" },
  157. { .compatible = "nvidia,tegra20-pmc" },
  158. { }
  159. };
  160. static void tegra_pmc_parse_dt(void)
  161. {
  162. struct device_node *np;
  163. u32 prop;
  164. enum tegra_suspend_mode suspend_mode;
  165. u32 core_good_time[2] = {0, 0};
  166. u32 lp0_vec[2] = {0, 0};
  167. np = of_find_matching_node(NULL, matches);
  168. BUG_ON(!np);
  169. tegra_pmc_base = of_iomap(np, 0);
  170. tegra_pmc_invert_interrupt = of_property_read_bool(np,
  171. "nvidia,invert-interrupt");
  172. tegra_pclk = of_clk_get_by_name(np, "pclk");
  173. WARN_ON(IS_ERR(tegra_pclk));
  174. /* Grabbing the power management configurations */
  175. if (of_property_read_u32(np, "nvidia,suspend-mode", &prop)) {
  176. suspend_mode = TEGRA_SUSPEND_NONE;
  177. } else {
  178. switch (prop) {
  179. case 0:
  180. suspend_mode = TEGRA_SUSPEND_LP0;
  181. break;
  182. case 1:
  183. suspend_mode = TEGRA_SUSPEND_LP1;
  184. break;
  185. case 2:
  186. suspend_mode = TEGRA_SUSPEND_LP2;
  187. break;
  188. default:
  189. suspend_mode = TEGRA_SUSPEND_NONE;
  190. break;
  191. }
  192. }
  193. if (of_property_read_u32(np, "nvidia,cpu-pwr-good-time", &prop))
  194. suspend_mode = TEGRA_SUSPEND_NONE;
  195. pmc_pm_data.cpu_good_time = prop;
  196. if (of_property_read_u32(np, "nvidia,cpu-pwr-off-time", &prop))
  197. suspend_mode = TEGRA_SUSPEND_NONE;
  198. pmc_pm_data.cpu_off_time = prop;
  199. if (of_property_read_u32_array(np, "nvidia,core-pwr-good-time",
  200. core_good_time, ARRAY_SIZE(core_good_time)))
  201. suspend_mode = TEGRA_SUSPEND_NONE;
  202. pmc_pm_data.core_osc_time = core_good_time[0];
  203. pmc_pm_data.core_pmu_time = core_good_time[1];
  204. if (of_property_read_u32(np, "nvidia,core-pwr-off-time",
  205. &prop))
  206. suspend_mode = TEGRA_SUSPEND_NONE;
  207. pmc_pm_data.core_off_time = prop;
  208. pmc_pm_data.corereq_high = of_property_read_bool(np,
  209. "nvidia,core-power-req-active-high");
  210. pmc_pm_data.sysclkreq_high = of_property_read_bool(np,
  211. "nvidia,sys-clock-req-active-high");
  212. pmc_pm_data.combined_req = of_property_read_bool(np,
  213. "nvidia,combined-power-req");
  214. pmc_pm_data.cpu_pwr_good_en = of_property_read_bool(np,
  215. "nvidia,cpu-pwr-good-en");
  216. if (of_property_read_u32_array(np, "nvidia,lp0-vec", lp0_vec,
  217. ARRAY_SIZE(lp0_vec)))
  218. if (suspend_mode == TEGRA_SUSPEND_LP0)
  219. suspend_mode = TEGRA_SUSPEND_LP1;
  220. pmc_pm_data.lp0_vec_phy_addr = lp0_vec[0];
  221. pmc_pm_data.lp0_vec_size = lp0_vec[1];
  222. pmc_pm_data.suspend_mode = suspend_mode;
  223. }
  224. void __init tegra_pmc_init(void)
  225. {
  226. u32 val;
  227. tegra_pmc_parse_dt();
  228. val = tegra_pmc_readl(PMC_CTRL);
  229. if (tegra_pmc_invert_interrupt)
  230. val |= PMC_CTRL_INTR_LOW;
  231. else
  232. val &= ~PMC_CTRL_INTR_LOW;
  233. tegra_pmc_writel(val, PMC_CTRL);
  234. }