pmc.c 8.4 KB

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