pmc.c 9.3 KB

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