omap-mpuss-lowpower.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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/system.h>
  48. #include <asm/pgalloc.h>
  49. #include <asm/suspend.h>
  50. #include <asm/hardware/cache-l2x0.h>
  51. #include <plat/omap44xx.h>
  52. #include "common.h"
  53. #include "omap4-sar-layout.h"
  54. #include "pm.h"
  55. #include "prcm_mpu44xx.h"
  56. #include "prminst44xx.h"
  57. #include "prcm44xx.h"
  58. #include "prm44xx.h"
  59. #include "prm-regbits-44xx.h"
  60. #ifdef CONFIG_SMP
  61. struct omap4_cpu_pm_info {
  62. struct powerdomain *pwrdm;
  63. void __iomem *scu_sar_addr;
  64. void __iomem *wkup_sar_addr;
  65. void __iomem *l2x0_sar_addr;
  66. };
  67. static DEFINE_PER_CPU(struct omap4_cpu_pm_info, omap4_pm_info);
  68. static struct powerdomain *mpuss_pd;
  69. static void __iomem *sar_base;
  70. /*
  71. * Program the wakeup routine address for the CPU0 and CPU1
  72. * used for OFF or DORMANT wakeup.
  73. */
  74. static inline void set_cpu_wakeup_addr(unsigned int cpu_id, u32 addr)
  75. {
  76. struct omap4_cpu_pm_info *pm_info = &per_cpu(omap4_pm_info, cpu_id);
  77. __raw_writel(addr, pm_info->wkup_sar_addr);
  78. }
  79. /*
  80. * Set the CPUx powerdomain's previous power state
  81. */
  82. static inline void set_cpu_next_pwrst(unsigned int cpu_id,
  83. unsigned int power_state)
  84. {
  85. struct omap4_cpu_pm_info *pm_info = &per_cpu(omap4_pm_info, cpu_id);
  86. pwrdm_set_next_pwrst(pm_info->pwrdm, power_state);
  87. }
  88. /*
  89. * Read CPU's previous power state
  90. */
  91. static inline unsigned int read_cpu_prev_pwrst(unsigned int cpu_id)
  92. {
  93. struct omap4_cpu_pm_info *pm_info = &per_cpu(omap4_pm_info, cpu_id);
  94. return pwrdm_read_prev_pwrst(pm_info->pwrdm);
  95. }
  96. /*
  97. * Clear the CPUx powerdomain's previous power state
  98. */
  99. static inline void clear_cpu_prev_pwrst(unsigned int cpu_id)
  100. {
  101. struct omap4_cpu_pm_info *pm_info = &per_cpu(omap4_pm_info, cpu_id);
  102. pwrdm_clear_all_prev_pwrst(pm_info->pwrdm);
  103. }
  104. /*
  105. * Store the SCU power status value to scratchpad memory
  106. */
  107. static void scu_pwrst_prepare(unsigned int cpu_id, unsigned int cpu_state)
  108. {
  109. struct omap4_cpu_pm_info *pm_info = &per_cpu(omap4_pm_info, cpu_id);
  110. u32 scu_pwr_st;
  111. switch (cpu_state) {
  112. case PWRDM_POWER_RET:
  113. scu_pwr_st = SCU_PM_DORMANT;
  114. break;
  115. case PWRDM_POWER_OFF:
  116. scu_pwr_st = SCU_PM_POWEROFF;
  117. break;
  118. case PWRDM_POWER_ON:
  119. case PWRDM_POWER_INACTIVE:
  120. default:
  121. scu_pwr_st = SCU_PM_NORMAL;
  122. break;
  123. }
  124. __raw_writel(scu_pwr_st, pm_info->scu_sar_addr);
  125. }
  126. /* Helper functions for MPUSS OSWR */
  127. static inline void mpuss_clear_prev_logic_pwrst(void)
  128. {
  129. u32 reg;
  130. reg = omap4_prminst_read_inst_reg(OMAP4430_PRM_PARTITION,
  131. OMAP4430_PRM_MPU_INST, OMAP4_RM_MPU_MPU_CONTEXT_OFFSET);
  132. omap4_prminst_write_inst_reg(reg, OMAP4430_PRM_PARTITION,
  133. OMAP4430_PRM_MPU_INST, OMAP4_RM_MPU_MPU_CONTEXT_OFFSET);
  134. }
  135. static inline void cpu_clear_prev_logic_pwrst(unsigned int cpu_id)
  136. {
  137. u32 reg;
  138. if (cpu_id) {
  139. reg = omap4_prcm_mpu_read_inst_reg(OMAP4430_PRCM_MPU_CPU1_INST,
  140. OMAP4_RM_CPU1_CPU1_CONTEXT_OFFSET);
  141. omap4_prcm_mpu_write_inst_reg(reg, OMAP4430_PRCM_MPU_CPU1_INST,
  142. OMAP4_RM_CPU1_CPU1_CONTEXT_OFFSET);
  143. } else {
  144. reg = omap4_prcm_mpu_read_inst_reg(OMAP4430_PRCM_MPU_CPU0_INST,
  145. OMAP4_RM_CPU0_CPU0_CONTEXT_OFFSET);
  146. omap4_prcm_mpu_write_inst_reg(reg, OMAP4430_PRCM_MPU_CPU0_INST,
  147. OMAP4_RM_CPU0_CPU0_CONTEXT_OFFSET);
  148. }
  149. }
  150. /**
  151. * omap4_mpuss_read_prev_context_state:
  152. * Function returns the MPUSS previous context state
  153. */
  154. u32 omap4_mpuss_read_prev_context_state(void)
  155. {
  156. u32 reg;
  157. reg = omap4_prminst_read_inst_reg(OMAP4430_PRM_PARTITION,
  158. OMAP4430_PRM_MPU_INST, OMAP4_RM_MPU_MPU_CONTEXT_OFFSET);
  159. reg &= OMAP4430_LOSTCONTEXT_DFF_MASK;
  160. return reg;
  161. }
  162. /*
  163. * Store the CPU cluster state for L2X0 low power operations.
  164. */
  165. static void l2x0_pwrst_prepare(unsigned int cpu_id, unsigned int save_state)
  166. {
  167. struct omap4_cpu_pm_info *pm_info = &per_cpu(omap4_pm_info, cpu_id);
  168. __raw_writel(save_state, pm_info->l2x0_sar_addr);
  169. }
  170. /*
  171. * Save the L2X0 AUXCTRL and POR value to SAR memory. Its used to
  172. * in every restore MPUSS OFF path.
  173. */
  174. #ifdef CONFIG_CACHE_L2X0
  175. static void save_l2x0_context(void)
  176. {
  177. u32 val;
  178. void __iomem *l2x0_base = omap4_get_l2cache_base();
  179. val = __raw_readl(l2x0_base + L2X0_AUX_CTRL);
  180. __raw_writel(val, sar_base + L2X0_AUXCTRL_OFFSET);
  181. val = __raw_readl(l2x0_base + L2X0_PREFETCH_CTRL);
  182. __raw_writel(val, sar_base + L2X0_PREFETCH_CTRL_OFFSET);
  183. }
  184. #else
  185. static void save_l2x0_context(void)
  186. {}
  187. #endif
  188. /**
  189. * omap4_enter_lowpower: OMAP4 MPUSS Low Power Entry Function
  190. * The purpose of this function is to manage low power programming
  191. * of OMAP4 MPUSS subsystem
  192. * @cpu : CPU ID
  193. * @power_state: Low power state.
  194. *
  195. * MPUSS states for the context save:
  196. * save_state =
  197. * 0 - Nothing lost and no need to save: MPUSS INACTIVE
  198. * 1 - CPUx L1 and logic lost: MPUSS CSWR
  199. * 2 - CPUx L1 and logic lost + GIC lost: MPUSS OSWR
  200. * 3 - CPUx L1 and logic lost + GIC + L2 lost: DEVICE OFF
  201. */
  202. int omap4_enter_lowpower(unsigned int cpu, unsigned int power_state)
  203. {
  204. unsigned int save_state = 0;
  205. unsigned int wakeup_cpu;
  206. if (omap_rev() == OMAP4430_REV_ES1_0)
  207. return -ENXIO;
  208. switch (power_state) {
  209. case PWRDM_POWER_ON:
  210. case PWRDM_POWER_INACTIVE:
  211. save_state = 0;
  212. break;
  213. case PWRDM_POWER_OFF:
  214. save_state = 1;
  215. break;
  216. case PWRDM_POWER_RET:
  217. default:
  218. /*
  219. * CPUx CSWR is invalid hardware state. Also CPUx OSWR
  220. * doesn't make much scense, since logic is lost and $L1
  221. * needs to be cleaned because of coherency. This makes
  222. * CPUx OSWR equivalent to CPUX OFF and hence not supported
  223. */
  224. WARN_ON(1);
  225. return -ENXIO;
  226. }
  227. pwrdm_pre_transition();
  228. /*
  229. * Check MPUSS next state and save interrupt controller if needed.
  230. * In MPUSS OSWR or device OFF, interrupt controller contest is lost.
  231. */
  232. mpuss_clear_prev_logic_pwrst();
  233. pwrdm_clear_all_prev_pwrst(mpuss_pd);
  234. if ((pwrdm_read_next_pwrst(mpuss_pd) == PWRDM_POWER_RET) &&
  235. (pwrdm_read_logic_retst(mpuss_pd) == PWRDM_POWER_OFF))
  236. save_state = 2;
  237. clear_cpu_prev_pwrst(cpu);
  238. cpu_clear_prev_logic_pwrst(cpu);
  239. set_cpu_next_pwrst(cpu, power_state);
  240. set_cpu_wakeup_addr(cpu, virt_to_phys(omap4_cpu_resume));
  241. scu_pwrst_prepare(cpu, power_state);
  242. l2x0_pwrst_prepare(cpu, save_state);
  243. /*
  244. * Call low level function with targeted low power state.
  245. */
  246. cpu_suspend(save_state, omap4_finish_suspend);
  247. /*
  248. * Restore the CPUx power state to ON otherwise CPUx
  249. * power domain can transitions to programmed low power
  250. * state while doing WFI outside the low powe code. On
  251. * secure devices, CPUx does WFI which can result in
  252. * domain transition
  253. */
  254. wakeup_cpu = smp_processor_id();
  255. set_cpu_next_pwrst(wakeup_cpu, PWRDM_POWER_ON);
  256. pwrdm_post_transition();
  257. return 0;
  258. }
  259. /**
  260. * omap4_hotplug_cpu: OMAP4 CPU hotplug entry
  261. * @cpu : CPU ID
  262. * @power_state: CPU low power state.
  263. */
  264. int omap4_hotplug_cpu(unsigned int cpu, unsigned int power_state)
  265. {
  266. unsigned int cpu_state = 0;
  267. if (omap_rev() == OMAP4430_REV_ES1_0)
  268. return -ENXIO;
  269. if (power_state == PWRDM_POWER_OFF)
  270. cpu_state = 1;
  271. clear_cpu_prev_pwrst(cpu);
  272. set_cpu_next_pwrst(cpu, power_state);
  273. set_cpu_wakeup_addr(cpu, virt_to_phys(omap_secondary_startup));
  274. scu_pwrst_prepare(cpu, power_state);
  275. /*
  276. * CPU never retuns back if targetted power state is OFF mode.
  277. * CPU ONLINE follows normal CPU ONLINE ptah via
  278. * omap_secondary_startup().
  279. */
  280. omap4_finish_suspend(cpu_state);
  281. set_cpu_next_pwrst(cpu, PWRDM_POWER_ON);
  282. return 0;
  283. }
  284. /*
  285. * Initialise OMAP4 MPUSS
  286. */
  287. int __init omap4_mpuss_init(void)
  288. {
  289. struct omap4_cpu_pm_info *pm_info;
  290. if (omap_rev() == OMAP4430_REV_ES1_0) {
  291. WARN(1, "Power Management not supported on OMAP4430 ES1.0\n");
  292. return -ENODEV;
  293. }
  294. sar_base = omap4_get_sar_ram_base();
  295. /* Initilaise per CPU PM information */
  296. pm_info = &per_cpu(omap4_pm_info, 0x0);
  297. pm_info->scu_sar_addr = sar_base + SCU_OFFSET0;
  298. pm_info->wkup_sar_addr = sar_base + CPU0_WAKEUP_NS_PA_ADDR_OFFSET;
  299. pm_info->l2x0_sar_addr = sar_base + L2X0_SAVE_OFFSET0;
  300. pm_info->pwrdm = pwrdm_lookup("cpu0_pwrdm");
  301. if (!pm_info->pwrdm) {
  302. pr_err("Lookup failed for CPU0 pwrdm\n");
  303. return -ENODEV;
  304. }
  305. /* Clear CPU previous power domain state */
  306. pwrdm_clear_all_prev_pwrst(pm_info->pwrdm);
  307. cpu_clear_prev_logic_pwrst(0);
  308. /* Initialise CPU0 power domain state to ON */
  309. pwrdm_set_next_pwrst(pm_info->pwrdm, PWRDM_POWER_ON);
  310. pm_info = &per_cpu(omap4_pm_info, 0x1);
  311. pm_info->scu_sar_addr = sar_base + SCU_OFFSET1;
  312. pm_info->wkup_sar_addr = sar_base + CPU1_WAKEUP_NS_PA_ADDR_OFFSET;
  313. pm_info->l2x0_sar_addr = sar_base + L2X0_SAVE_OFFSET1;
  314. pm_info->pwrdm = pwrdm_lookup("cpu1_pwrdm");
  315. if (!pm_info->pwrdm) {
  316. pr_err("Lookup failed for CPU1 pwrdm\n");
  317. return -ENODEV;
  318. }
  319. /* Clear CPU previous power domain state */
  320. pwrdm_clear_all_prev_pwrst(pm_info->pwrdm);
  321. cpu_clear_prev_logic_pwrst(1);
  322. /* Initialise CPU1 power domain state to ON */
  323. pwrdm_set_next_pwrst(pm_info->pwrdm, PWRDM_POWER_ON);
  324. mpuss_pd = pwrdm_lookup("mpu_pwrdm");
  325. if (!mpuss_pd) {
  326. pr_err("Failed to lookup MPUSS power domain\n");
  327. return -ENODEV;
  328. }
  329. pwrdm_clear_all_prev_pwrst(mpuss_pd);
  330. mpuss_clear_prev_logic_pwrst();
  331. /* Save device type on scratchpad for low level code to use */
  332. if (omap_type() != OMAP2_DEVICE_TYPE_GP)
  333. __raw_writel(1, sar_base + OMAP_TYPE_OFFSET);
  334. else
  335. __raw_writel(0, sar_base + OMAP_TYPE_OFFSET);
  336. save_l2x0_context();
  337. return 0;
  338. }
  339. #endif