omap-mpuss-lowpower.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * OMAP MPUSS low power code
  3. *
  4. * Copyright (C) 2011 Texas Instruments, Inc.
  5. * Santosh Shilimkar <santosh.shilimkar@ti.com>
  6. *
  7. * OMAP4430 MPUSS mainly consists of dual Cortex-A9 with per-CPU
  8. * Local timer and Watchdog, GIC, SCU, PL310 L2 cache controller,
  9. * CPU0 and CPU1 LPRM modules.
  10. * CPU0, CPU1 and MPUSS each have there own power domain and
  11. * hence multiple low power combinations of MPUSS are possible.
  12. *
  13. * The CPU0 and CPU1 can't support Closed switch Retention (CSWR)
  14. * because the mode is not supported by hw constraints of dormant
  15. * mode. While waking up from the dormant mode, a reset signal
  16. * to the Cortex-A9 processor must be asserted by the external
  17. * power controller.
  18. *
  19. * With architectural inputs and hardware recommendations, only
  20. * below modes are supported from power gain vs latency point of view.
  21. *
  22. * CPU0 CPU1 MPUSS
  23. * ----------------------------------------------
  24. * ON ON ON
  25. * ON(Inactive) OFF ON(Inactive)
  26. * OFF OFF CSWR
  27. * OFF OFF OSWR
  28. * OFF OFF OFF(Device OFF *TBD)
  29. * ----------------------------------------------
  30. *
  31. * Note: CPU0 is the master core and it is the last CPU to go down
  32. * and first to wake-up when MPUSS low power states are excercised
  33. *
  34. *
  35. * This program is free software; you can redistribute it and/or modify
  36. * it under the terms of the GNU General Public License version 2 as
  37. * published by the Free Software Foundation.
  38. */
  39. #include <linux/kernel.h>
  40. #include <linux/io.h>
  41. #include <linux/errno.h>
  42. #include <linux/linkage.h>
  43. #include <linux/smp.h>
  44. #include <asm/cacheflush.h>
  45. #include <asm/tlbflush.h>
  46. #include <asm/smp_scu.h>
  47. #include <asm/pgalloc.h>
  48. #include <asm/suspend.h>
  49. #include <asm/hardware/cache-l2x0.h>
  50. #include <plat/omap44xx.h>
  51. #include "common.h"
  52. #include "omap4-sar-layout.h"
  53. #include "pm.h"
  54. #include "prcm_mpu44xx.h"
  55. #include "prminst44xx.h"
  56. #include "prcm44xx.h"
  57. #include "prm44xx.h"
  58. #include "prm-regbits-44xx.h"
  59. #ifdef CONFIG_SMP
  60. struct omap4_cpu_pm_info {
  61. struct powerdomain *pwrdm;
  62. void __iomem *scu_sar_addr;
  63. void __iomem *wkup_sar_addr;
  64. void __iomem *l2x0_sar_addr;
  65. };
  66. static DEFINE_PER_CPU(struct omap4_cpu_pm_info, omap4_pm_info);
  67. static struct powerdomain *mpuss_pd;
  68. static void __iomem *sar_base;
  69. /*
  70. * Program the wakeup routine address for the CPU0 and CPU1
  71. * used for OFF or DORMANT wakeup.
  72. */
  73. static inline void set_cpu_wakeup_addr(unsigned int cpu_id, u32 addr)
  74. {
  75. struct omap4_cpu_pm_info *pm_info = &per_cpu(omap4_pm_info, cpu_id);
  76. __raw_writel(addr, pm_info->wkup_sar_addr);
  77. }
  78. /*
  79. * Set the CPUx powerdomain's previous power state
  80. */
  81. static inline void set_cpu_next_pwrst(unsigned int cpu_id,
  82. unsigned int power_state)
  83. {
  84. struct omap4_cpu_pm_info *pm_info = &per_cpu(omap4_pm_info, cpu_id);
  85. pwrdm_set_next_pwrst(pm_info->pwrdm, power_state);
  86. }
  87. /*
  88. * Read CPU's previous power state
  89. */
  90. static inline unsigned int read_cpu_prev_pwrst(unsigned int cpu_id)
  91. {
  92. struct omap4_cpu_pm_info *pm_info = &per_cpu(omap4_pm_info, cpu_id);
  93. return pwrdm_read_prev_pwrst(pm_info->pwrdm);
  94. }
  95. /*
  96. * Clear the CPUx powerdomain's previous power state
  97. */
  98. static inline void clear_cpu_prev_pwrst(unsigned int cpu_id)
  99. {
  100. struct omap4_cpu_pm_info *pm_info = &per_cpu(omap4_pm_info, cpu_id);
  101. pwrdm_clear_all_prev_pwrst(pm_info->pwrdm);
  102. }
  103. /*
  104. * Store the SCU power status value to scratchpad memory
  105. */
  106. static void scu_pwrst_prepare(unsigned int cpu_id, unsigned int cpu_state)
  107. {
  108. struct omap4_cpu_pm_info *pm_info = &per_cpu(omap4_pm_info, cpu_id);
  109. u32 scu_pwr_st;
  110. switch (cpu_state) {
  111. case PWRDM_POWER_RET:
  112. scu_pwr_st = SCU_PM_DORMANT;
  113. break;
  114. case PWRDM_POWER_OFF:
  115. scu_pwr_st = SCU_PM_POWEROFF;
  116. break;
  117. case PWRDM_POWER_ON:
  118. case PWRDM_POWER_INACTIVE:
  119. default:
  120. scu_pwr_st = SCU_PM_NORMAL;
  121. break;
  122. }
  123. __raw_writel(scu_pwr_st, pm_info->scu_sar_addr);
  124. }
  125. /* Helper functions for MPUSS OSWR */
  126. static inline void mpuss_clear_prev_logic_pwrst(void)
  127. {
  128. u32 reg;
  129. reg = omap4_prminst_read_inst_reg(OMAP4430_PRM_PARTITION,
  130. OMAP4430_PRM_MPU_INST, OMAP4_RM_MPU_MPU_CONTEXT_OFFSET);
  131. omap4_prminst_write_inst_reg(reg, OMAP4430_PRM_PARTITION,
  132. OMAP4430_PRM_MPU_INST, OMAP4_RM_MPU_MPU_CONTEXT_OFFSET);
  133. }
  134. static inline void cpu_clear_prev_logic_pwrst(unsigned int cpu_id)
  135. {
  136. u32 reg;
  137. if (cpu_id) {
  138. reg = omap4_prcm_mpu_read_inst_reg(OMAP4430_PRCM_MPU_CPU1_INST,
  139. OMAP4_RM_CPU1_CPU1_CONTEXT_OFFSET);
  140. omap4_prcm_mpu_write_inst_reg(reg, OMAP4430_PRCM_MPU_CPU1_INST,
  141. OMAP4_RM_CPU1_CPU1_CONTEXT_OFFSET);
  142. } else {
  143. reg = omap4_prcm_mpu_read_inst_reg(OMAP4430_PRCM_MPU_CPU0_INST,
  144. OMAP4_RM_CPU0_CPU0_CONTEXT_OFFSET);
  145. omap4_prcm_mpu_write_inst_reg(reg, OMAP4430_PRCM_MPU_CPU0_INST,
  146. OMAP4_RM_CPU0_CPU0_CONTEXT_OFFSET);
  147. }
  148. }
  149. /**
  150. * omap4_mpuss_read_prev_context_state:
  151. * Function returns the MPUSS previous context state
  152. */
  153. u32 omap4_mpuss_read_prev_context_state(void)
  154. {
  155. u32 reg;
  156. reg = omap4_prminst_read_inst_reg(OMAP4430_PRM_PARTITION,
  157. OMAP4430_PRM_MPU_INST, OMAP4_RM_MPU_MPU_CONTEXT_OFFSET);
  158. reg &= OMAP4430_LOSTCONTEXT_DFF_MASK;
  159. return reg;
  160. }
  161. /*
  162. * Store the CPU cluster state for L2X0 low power operations.
  163. */
  164. static void l2x0_pwrst_prepare(unsigned int cpu_id, unsigned int save_state)
  165. {
  166. struct omap4_cpu_pm_info *pm_info = &per_cpu(omap4_pm_info, cpu_id);
  167. __raw_writel(save_state, pm_info->l2x0_sar_addr);
  168. }
  169. /*
  170. * Save the L2X0 AUXCTRL and POR value to SAR memory. Its used to
  171. * in every restore MPUSS OFF path.
  172. */
  173. #ifdef CONFIG_CACHE_L2X0
  174. static void save_l2x0_context(void)
  175. {
  176. u32 val;
  177. void __iomem *l2x0_base = omap4_get_l2cache_base();
  178. val = __raw_readl(l2x0_base + L2X0_AUX_CTRL);
  179. __raw_writel(val, sar_base + L2X0_AUXCTRL_OFFSET);
  180. val = __raw_readl(l2x0_base + L2X0_PREFETCH_CTRL);
  181. __raw_writel(val, sar_base + L2X0_PREFETCH_CTRL_OFFSET);
  182. }
  183. #else
  184. static void save_l2x0_context(void)
  185. {}
  186. #endif
  187. /**
  188. * omap4_enter_lowpower: OMAP4 MPUSS Low Power Entry Function
  189. * The purpose of this function is to manage low power programming
  190. * of OMAP4 MPUSS subsystem
  191. * @cpu : CPU ID
  192. * @power_state: Low power state.
  193. *
  194. * MPUSS states for the context save:
  195. * save_state =
  196. * 0 - Nothing lost and no need to save: MPUSS INACTIVE
  197. * 1 - CPUx L1 and logic lost: MPUSS CSWR
  198. * 2 - CPUx L1 and logic lost + GIC lost: MPUSS OSWR
  199. * 3 - CPUx L1 and logic lost + GIC + L2 lost: DEVICE OFF
  200. */
  201. int omap4_enter_lowpower(unsigned int cpu, unsigned int power_state)
  202. {
  203. unsigned int save_state = 0;
  204. unsigned int wakeup_cpu;
  205. if (omap_rev() == OMAP4430_REV_ES1_0)
  206. return -ENXIO;
  207. switch (power_state) {
  208. case PWRDM_POWER_ON:
  209. case PWRDM_POWER_INACTIVE:
  210. save_state = 0;
  211. break;
  212. case PWRDM_POWER_OFF:
  213. save_state = 1;
  214. break;
  215. case PWRDM_POWER_RET:
  216. default:
  217. /*
  218. * CPUx CSWR is invalid hardware state. Also CPUx OSWR
  219. * doesn't make much scense, since logic is lost and $L1
  220. * needs to be cleaned because of coherency. This makes
  221. * CPUx OSWR equivalent to CPUX OFF and hence not supported
  222. */
  223. WARN_ON(1);
  224. return -ENXIO;
  225. }
  226. pwrdm_pre_transition();
  227. /*
  228. * Check MPUSS next state and save interrupt controller if needed.
  229. * In MPUSS OSWR or device OFF, interrupt controller contest is lost.
  230. */
  231. mpuss_clear_prev_logic_pwrst();
  232. if ((pwrdm_read_next_pwrst(mpuss_pd) == PWRDM_POWER_RET) &&
  233. (pwrdm_read_logic_retst(mpuss_pd) == PWRDM_POWER_OFF))
  234. save_state = 2;
  235. cpu_clear_prev_logic_pwrst(cpu);
  236. set_cpu_next_pwrst(cpu, power_state);
  237. set_cpu_wakeup_addr(cpu, virt_to_phys(omap4_cpu_resume));
  238. scu_pwrst_prepare(cpu, power_state);
  239. l2x0_pwrst_prepare(cpu, save_state);
  240. /*
  241. * Call low level function with targeted low power state.
  242. */
  243. cpu_suspend(save_state, omap4_finish_suspend);
  244. /*
  245. * Restore the CPUx power state to ON otherwise CPUx
  246. * power domain can transitions to programmed low power
  247. * state while doing WFI outside the low powe code. On
  248. * secure devices, CPUx does WFI which can result in
  249. * domain transition
  250. */
  251. wakeup_cpu = smp_processor_id();
  252. set_cpu_next_pwrst(wakeup_cpu, PWRDM_POWER_ON);
  253. pwrdm_post_transition();
  254. return 0;
  255. }
  256. /**
  257. * omap4_hotplug_cpu: OMAP4 CPU hotplug entry
  258. * @cpu : CPU ID
  259. * @power_state: CPU low power state.
  260. */
  261. int __cpuinit omap4_hotplug_cpu(unsigned int cpu, unsigned int power_state)
  262. {
  263. unsigned int cpu_state = 0;
  264. if (omap_rev() == OMAP4430_REV_ES1_0)
  265. return -ENXIO;
  266. if (power_state == PWRDM_POWER_OFF)
  267. cpu_state = 1;
  268. clear_cpu_prev_pwrst(cpu);
  269. set_cpu_next_pwrst(cpu, power_state);
  270. set_cpu_wakeup_addr(cpu, virt_to_phys(omap_secondary_startup));
  271. scu_pwrst_prepare(cpu, power_state);
  272. /*
  273. * CPU never retuns back if targetted power state is OFF mode.
  274. * CPU ONLINE follows normal CPU ONLINE ptah via
  275. * omap_secondary_startup().
  276. */
  277. omap4_finish_suspend(cpu_state);
  278. set_cpu_next_pwrst(cpu, PWRDM_POWER_ON);
  279. return 0;
  280. }
  281. /*
  282. * Initialise OMAP4 MPUSS
  283. */
  284. int __init omap4_mpuss_init(void)
  285. {
  286. struct omap4_cpu_pm_info *pm_info;
  287. if (omap_rev() == OMAP4430_REV_ES1_0) {
  288. WARN(1, "Power Management not supported on OMAP4430 ES1.0\n");
  289. return -ENODEV;
  290. }
  291. sar_base = omap4_get_sar_ram_base();
  292. /* Initilaise per CPU PM information */
  293. pm_info = &per_cpu(omap4_pm_info, 0x0);
  294. pm_info->scu_sar_addr = sar_base + SCU_OFFSET0;
  295. pm_info->wkup_sar_addr = sar_base + CPU0_WAKEUP_NS_PA_ADDR_OFFSET;
  296. pm_info->l2x0_sar_addr = sar_base + L2X0_SAVE_OFFSET0;
  297. pm_info->pwrdm = pwrdm_lookup("cpu0_pwrdm");
  298. if (!pm_info->pwrdm) {
  299. pr_err("Lookup failed for CPU0 pwrdm\n");
  300. return -ENODEV;
  301. }
  302. /* Clear CPU previous power domain state */
  303. pwrdm_clear_all_prev_pwrst(pm_info->pwrdm);
  304. cpu_clear_prev_logic_pwrst(0);
  305. /* Initialise CPU0 power domain state to ON */
  306. pwrdm_set_next_pwrst(pm_info->pwrdm, PWRDM_POWER_ON);
  307. pm_info = &per_cpu(omap4_pm_info, 0x1);
  308. pm_info->scu_sar_addr = sar_base + SCU_OFFSET1;
  309. pm_info->wkup_sar_addr = sar_base + CPU1_WAKEUP_NS_PA_ADDR_OFFSET;
  310. pm_info->l2x0_sar_addr = sar_base + L2X0_SAVE_OFFSET1;
  311. pm_info->pwrdm = pwrdm_lookup("cpu1_pwrdm");
  312. if (!pm_info->pwrdm) {
  313. pr_err("Lookup failed for CPU1 pwrdm\n");
  314. return -ENODEV;
  315. }
  316. /* Clear CPU previous power domain state */
  317. pwrdm_clear_all_prev_pwrst(pm_info->pwrdm);
  318. cpu_clear_prev_logic_pwrst(1);
  319. /* Initialise CPU1 power domain state to ON */
  320. pwrdm_set_next_pwrst(pm_info->pwrdm, PWRDM_POWER_ON);
  321. mpuss_pd = pwrdm_lookup("mpu_pwrdm");
  322. if (!mpuss_pd) {
  323. pr_err("Failed to lookup MPUSS power domain\n");
  324. return -ENODEV;
  325. }
  326. pwrdm_clear_all_prev_pwrst(mpuss_pd);
  327. mpuss_clear_prev_logic_pwrst();
  328. /* Save device type on scratchpad for low level code to use */
  329. if (omap_type() != OMAP2_DEVICE_TYPE_GP)
  330. __raw_writel(1, sar_base + OMAP_TYPE_OFFSET);
  331. else
  332. __raw_writel(0, sar_base + OMAP_TYPE_OFFSET);
  333. save_l2x0_context();
  334. return 0;
  335. }
  336. #endif