pmc.c 8.1 KB

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