pmc.c 9.4 KB

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