omap-mpuss-lowpower.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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/virt.h>
  50. #include <asm/hardware/cache-l2x0.h>
  51. #include "soc.h"
  52. #include "common.h"
  53. #include "omap44xx.h"
  54. #include "omap4-sar-layout.h"
  55. #include "pm.h"
  56. #include "prcm_mpu44xx.h"
  57. #include "prcm_mpu54xx.h"
  58. #include "prminst44xx.h"
  59. #include "prcm44xx.h"
  60. #include "prm44xx.h"
  61. #include "prm-regbits-44xx.h"
  62. static void __iomem *sar_base;
  63. static u32 old_cpu1_ns_pa_addr;
  64. #if defined(CONFIG_PM) && defined(CONFIG_SMP)
  65. struct omap4_cpu_pm_info {
  66. struct powerdomain *pwrdm;
  67. void __iomem *scu_sar_addr;
  68. void __iomem *wkup_sar_addr;
  69. void __iomem *l2x0_sar_addr;
  70. };
  71. /**
  72. * struct cpu_pm_ops - CPU pm operations
  73. * @finish_suspend: CPU suspend finisher function pointer
  74. * @resume: CPU resume function pointer
  75. * @scu_prepare: CPU Snoop Control program function pointer
  76. * @hotplug_restart: CPU restart function pointer
  77. *
  78. * Structure holds functions pointer for CPU low power operations like
  79. * suspend, resume and scu programming.
  80. */
  81. struct cpu_pm_ops {
  82. int (*finish_suspend)(unsigned long cpu_state);
  83. void (*resume)(void);
  84. void (*scu_prepare)(unsigned int cpu_id, unsigned int cpu_state);
  85. void (*hotplug_restart)(void);
  86. };
  87. static DEFINE_PER_CPU(struct omap4_cpu_pm_info, omap4_pm_info);
  88. static struct powerdomain *mpuss_pd;
  89. static u32 cpu_context_offset;
  90. static int default_finish_suspend(unsigned long cpu_state)
  91. {
  92. omap_do_wfi();
  93. return 0;
  94. }
  95. static void dummy_cpu_resume(void)
  96. {}
  97. static void dummy_scu_prepare(unsigned int cpu_id, unsigned int cpu_state)
  98. {}
  99. static struct cpu_pm_ops omap_pm_ops = {
  100. .finish_suspend = default_finish_suspend,
  101. .resume = dummy_cpu_resume,
  102. .scu_prepare = dummy_scu_prepare,
  103. .hotplug_restart = dummy_cpu_resume,
  104. };
  105. /*
  106. * Program the wakeup routine address for the CPU0 and CPU1
  107. * used for OFF or DORMANT wakeup.
  108. */
  109. static inline void set_cpu_wakeup_addr(unsigned int cpu_id, u32 addr)
  110. {
  111. struct omap4_cpu_pm_info *pm_info = &per_cpu(omap4_pm_info, cpu_id);
  112. if (pm_info->wkup_sar_addr)
  113. writel_relaxed(addr, pm_info->wkup_sar_addr);
  114. }
  115. /*
  116. * Store the SCU power status value to scratchpad memory
  117. */
  118. static void scu_pwrst_prepare(unsigned int cpu_id, unsigned int cpu_state)
  119. {
  120. struct omap4_cpu_pm_info *pm_info = &per_cpu(omap4_pm_info, cpu_id);
  121. u32 scu_pwr_st;
  122. switch (cpu_state) {
  123. case PWRDM_POWER_RET:
  124. scu_pwr_st = SCU_PM_DORMANT;
  125. break;
  126. case PWRDM_POWER_OFF:
  127. scu_pwr_st = SCU_PM_POWEROFF;
  128. break;
  129. case PWRDM_POWER_ON:
  130. case PWRDM_POWER_INACTIVE:
  131. default:
  132. scu_pwr_st = SCU_PM_NORMAL;
  133. break;
  134. }
  135. if (pm_info->scu_sar_addr)
  136. writel_relaxed(scu_pwr_st, pm_info->scu_sar_addr);
  137. }
  138. /* Helper functions for MPUSS OSWR */
  139. static inline void mpuss_clear_prev_logic_pwrst(void)
  140. {
  141. u32 reg;
  142. reg = omap4_prminst_read_inst_reg(OMAP4430_PRM_PARTITION,
  143. OMAP4430_PRM_MPU_INST, OMAP4_RM_MPU_MPU_CONTEXT_OFFSET);
  144. omap4_prminst_write_inst_reg(reg, OMAP4430_PRM_PARTITION,
  145. OMAP4430_PRM_MPU_INST, OMAP4_RM_MPU_MPU_CONTEXT_OFFSET);
  146. }
  147. static inline void cpu_clear_prev_logic_pwrst(unsigned int cpu_id)
  148. {
  149. u32 reg;
  150. if (cpu_id) {
  151. reg = omap4_prcm_mpu_read_inst_reg(OMAP4430_PRCM_MPU_CPU1_INST,
  152. cpu_context_offset);
  153. omap4_prcm_mpu_write_inst_reg(reg, OMAP4430_PRCM_MPU_CPU1_INST,
  154. cpu_context_offset);
  155. } else {
  156. reg = omap4_prcm_mpu_read_inst_reg(OMAP4430_PRCM_MPU_CPU0_INST,
  157. cpu_context_offset);
  158. omap4_prcm_mpu_write_inst_reg(reg, OMAP4430_PRCM_MPU_CPU0_INST,
  159. cpu_context_offset);
  160. }
  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. if (pm_info->l2x0_sar_addr)
  169. writel_relaxed(save_state, pm_info->l2x0_sar_addr);
  170. }
  171. /*
  172. * Save the L2X0 AUXCTRL and POR value to SAR memory. Its used to
  173. * in every restore MPUSS OFF path.
  174. */
  175. #ifdef CONFIG_CACHE_L2X0
  176. static void __init save_l2x0_context(void)
  177. {
  178. void __iomem *l2x0_base = omap4_get_l2cache_base();
  179. if (l2x0_base && sar_base) {
  180. writel_relaxed(l2x0_saved_regs.aux_ctrl,
  181. sar_base + L2X0_AUXCTRL_OFFSET);
  182. writel_relaxed(l2x0_saved_regs.prefetch_ctrl,
  183. sar_base + L2X0_PREFETCH_CTRL_OFFSET);
  184. }
  185. }
  186. #else
  187. static void __init save_l2x0_context(void)
  188. {}
  189. #endif
  190. /**
  191. * omap4_enter_lowpower: OMAP4 MPUSS Low Power Entry Function
  192. * The purpose of this function is to manage low power programming
  193. * of OMAP4 MPUSS subsystem
  194. * @cpu : CPU ID
  195. * @power_state: Low power state.
  196. *
  197. * MPUSS states for the context save:
  198. * save_state =
  199. * 0 - Nothing lost and no need to save: MPUSS INACTIVE
  200. * 1 - CPUx L1 and logic lost: MPUSS CSWR
  201. * 2 - CPUx L1 and logic lost + GIC lost: MPUSS OSWR
  202. * 3 - CPUx L1 and logic lost + GIC + L2 lost: DEVICE OFF
  203. */
  204. int omap4_enter_lowpower(unsigned int cpu, unsigned int power_state)
  205. {
  206. struct omap4_cpu_pm_info *pm_info = &per_cpu(omap4_pm_info, cpu);
  207. unsigned int save_state = 0, cpu_logic_state = PWRDM_POWER_RET;
  208. unsigned int wakeup_cpu;
  209. if (omap_rev() == OMAP4430_REV_ES1_0)
  210. return -ENXIO;
  211. switch (power_state) {
  212. case PWRDM_POWER_ON:
  213. case PWRDM_POWER_INACTIVE:
  214. save_state = 0;
  215. break;
  216. case PWRDM_POWER_OFF:
  217. cpu_logic_state = PWRDM_POWER_OFF;
  218. save_state = 1;
  219. break;
  220. case PWRDM_POWER_RET:
  221. if (IS_PM44XX_ERRATUM(PM_OMAP4_CPU_OSWR_DISABLE))
  222. save_state = 0;
  223. break;
  224. default:
  225. /*
  226. * CPUx CSWR is invalid hardware state. Also CPUx OSWR
  227. * doesn't make much scense, since logic is lost and $L1
  228. * needs to be cleaned because of coherency. This makes
  229. * CPUx OSWR equivalent to CPUX OFF and hence not supported
  230. */
  231. WARN_ON(1);
  232. return -ENXIO;
  233. }
  234. pwrdm_pre_transition(NULL);
  235. /*
  236. * Check MPUSS next state and save interrupt controller if needed.
  237. * In MPUSS OSWR or device OFF, interrupt controller contest is lost.
  238. */
  239. mpuss_clear_prev_logic_pwrst();
  240. if ((pwrdm_read_next_pwrst(mpuss_pd) == PWRDM_POWER_RET) &&
  241. (pwrdm_read_logic_retst(mpuss_pd) == PWRDM_POWER_OFF))
  242. save_state = 2;
  243. cpu_clear_prev_logic_pwrst(cpu);
  244. pwrdm_set_next_pwrst(pm_info->pwrdm, power_state);
  245. pwrdm_set_logic_retst(pm_info->pwrdm, cpu_logic_state);
  246. set_cpu_wakeup_addr(cpu, __pa_symbol(omap_pm_ops.resume));
  247. omap_pm_ops.scu_prepare(cpu, power_state);
  248. l2x0_pwrst_prepare(cpu, save_state);
  249. /*
  250. * Call low level function with targeted low power state.
  251. */
  252. if (save_state)
  253. cpu_suspend(save_state, omap_pm_ops.finish_suspend);
  254. else
  255. omap_pm_ops.finish_suspend(save_state);
  256. if (IS_PM44XX_ERRATUM(PM_OMAP4_ROM_SMP_BOOT_ERRATUM_GICD) && cpu)
  257. gic_dist_enable();
  258. /*
  259. * Restore the CPUx power state to ON otherwise CPUx
  260. * power domain can transitions to programmed low power
  261. * state while doing WFI outside the low powe code. On
  262. * secure devices, CPUx does WFI which can result in
  263. * domain transition
  264. */
  265. wakeup_cpu = smp_processor_id();
  266. pwrdm_set_next_pwrst(pm_info->pwrdm, PWRDM_POWER_ON);
  267. pwrdm_post_transition(NULL);
  268. return 0;
  269. }
  270. /**
  271. * omap4_hotplug_cpu: OMAP4 CPU hotplug entry
  272. * @cpu : CPU ID
  273. * @power_state: CPU low power state.
  274. */
  275. int omap4_hotplug_cpu(unsigned int cpu, unsigned int power_state)
  276. {
  277. struct omap4_cpu_pm_info *pm_info = &per_cpu(omap4_pm_info, cpu);
  278. unsigned int cpu_state = 0;
  279. if (omap_rev() == OMAP4430_REV_ES1_0)
  280. return -ENXIO;
  281. /* Use the achievable power state for the domain */
  282. power_state = pwrdm_get_valid_lp_state(pm_info->pwrdm,
  283. false, power_state);
  284. if (power_state == PWRDM_POWER_OFF)
  285. cpu_state = 1;
  286. pwrdm_clear_all_prev_pwrst(pm_info->pwrdm);
  287. pwrdm_set_next_pwrst(pm_info->pwrdm, power_state);
  288. set_cpu_wakeup_addr(cpu, __pa_symbol(omap_pm_ops.hotplug_restart));
  289. omap_pm_ops.scu_prepare(cpu, power_state);
  290. /*
  291. * CPU never retuns back if targeted power state is OFF mode.
  292. * CPU ONLINE follows normal CPU ONLINE ptah via
  293. * omap4_secondary_startup().
  294. */
  295. omap_pm_ops.finish_suspend(cpu_state);
  296. pwrdm_set_next_pwrst(pm_info->pwrdm, PWRDM_POWER_ON);
  297. return 0;
  298. }
  299. /*
  300. * Enable Mercury Fast HG retention mode by default.
  301. */
  302. static void enable_mercury_retention_mode(void)
  303. {
  304. u32 reg;
  305. reg = omap4_prcm_mpu_read_inst_reg(OMAP54XX_PRCM_MPU_DEVICE_INST,
  306. OMAP54XX_PRCM_MPU_PRM_PSCON_COUNT_OFFSET);
  307. /* Enable HG_EN, HG_RAMPUP = fast mode */
  308. reg |= BIT(24) | BIT(25);
  309. omap4_prcm_mpu_write_inst_reg(reg, OMAP54XX_PRCM_MPU_DEVICE_INST,
  310. OMAP54XX_PRCM_MPU_PRM_PSCON_COUNT_OFFSET);
  311. }
  312. /*
  313. * Initialise OMAP4 MPUSS
  314. */
  315. int __init omap4_mpuss_init(void)
  316. {
  317. struct omap4_cpu_pm_info *pm_info;
  318. if (omap_rev() == OMAP4430_REV_ES1_0) {
  319. WARN(1, "Power Management not supported on OMAP4430 ES1.0\n");
  320. return -ENODEV;
  321. }
  322. /* Initilaise per CPU PM information */
  323. pm_info = &per_cpu(omap4_pm_info, 0x0);
  324. if (sar_base) {
  325. pm_info->scu_sar_addr = sar_base + SCU_OFFSET0;
  326. if (cpu_is_omap44xx())
  327. pm_info->wkup_sar_addr = sar_base +
  328. CPU0_WAKEUP_NS_PA_ADDR_OFFSET;
  329. else
  330. pm_info->wkup_sar_addr = sar_base +
  331. OMAP5_CPU0_WAKEUP_NS_PA_ADDR_OFFSET;
  332. pm_info->l2x0_sar_addr = sar_base + L2X0_SAVE_OFFSET0;
  333. }
  334. pm_info->pwrdm = pwrdm_lookup("cpu0_pwrdm");
  335. if (!pm_info->pwrdm) {
  336. pr_err("Lookup failed for CPU0 pwrdm\n");
  337. return -ENODEV;
  338. }
  339. /* Clear CPU previous power domain state */
  340. pwrdm_clear_all_prev_pwrst(pm_info->pwrdm);
  341. cpu_clear_prev_logic_pwrst(0);
  342. /* Initialise CPU0 power domain state to ON */
  343. pwrdm_set_next_pwrst(pm_info->pwrdm, PWRDM_POWER_ON);
  344. pm_info = &per_cpu(omap4_pm_info, 0x1);
  345. if (sar_base) {
  346. pm_info->scu_sar_addr = sar_base + SCU_OFFSET1;
  347. if (cpu_is_omap44xx())
  348. pm_info->wkup_sar_addr = sar_base +
  349. CPU1_WAKEUP_NS_PA_ADDR_OFFSET;
  350. else
  351. pm_info->wkup_sar_addr = sar_base +
  352. OMAP5_CPU1_WAKEUP_NS_PA_ADDR_OFFSET;
  353. pm_info->l2x0_sar_addr = sar_base + L2X0_SAVE_OFFSET1;
  354. }
  355. pm_info->pwrdm = pwrdm_lookup("cpu1_pwrdm");
  356. if (!pm_info->pwrdm) {
  357. pr_err("Lookup failed for CPU1 pwrdm\n");
  358. return -ENODEV;
  359. }
  360. /* Clear CPU previous power domain state */
  361. pwrdm_clear_all_prev_pwrst(pm_info->pwrdm);
  362. cpu_clear_prev_logic_pwrst(1);
  363. /* Initialise CPU1 power domain state to ON */
  364. pwrdm_set_next_pwrst(pm_info->pwrdm, PWRDM_POWER_ON);
  365. mpuss_pd = pwrdm_lookup("mpu_pwrdm");
  366. if (!mpuss_pd) {
  367. pr_err("Failed to lookup MPUSS power domain\n");
  368. return -ENODEV;
  369. }
  370. pwrdm_clear_all_prev_pwrst(mpuss_pd);
  371. mpuss_clear_prev_logic_pwrst();
  372. if (sar_base) {
  373. /* Save device type on scratchpad for low level code to use */
  374. writel_relaxed((omap_type() != OMAP2_DEVICE_TYPE_GP) ? 1 : 0,
  375. sar_base + OMAP_TYPE_OFFSET);
  376. save_l2x0_context();
  377. }
  378. if (cpu_is_omap44xx()) {
  379. omap_pm_ops.finish_suspend = omap4_finish_suspend;
  380. omap_pm_ops.resume = omap4_cpu_resume;
  381. omap_pm_ops.scu_prepare = scu_pwrst_prepare;
  382. omap_pm_ops.hotplug_restart = omap4_secondary_startup;
  383. cpu_context_offset = OMAP4_RM_CPU0_CPU0_CONTEXT_OFFSET;
  384. } else if (soc_is_omap54xx() || soc_is_dra7xx()) {
  385. cpu_context_offset = OMAP54XX_RM_CPU0_CPU0_CONTEXT_OFFSET;
  386. enable_mercury_retention_mode();
  387. }
  388. if (cpu_is_omap446x())
  389. omap_pm_ops.hotplug_restart = omap4460_secondary_startup;
  390. return 0;
  391. }
  392. #endif
  393. u32 omap4_get_cpu1_ns_pa_addr(void)
  394. {
  395. return old_cpu1_ns_pa_addr;
  396. }
  397. /*
  398. * For kexec, we must set CPU1_WAKEUP_NS_PA_ADDR to point to
  399. * current kernel's secondary_startup() early before
  400. * clockdomains_init(). Otherwise clockdomain_init() can
  401. * wake CPU1 and cause a hang.
  402. */
  403. void __init omap4_mpuss_early_init(void)
  404. {
  405. unsigned long startup_pa;
  406. void __iomem *ns_pa_addr;
  407. if (!(soc_is_omap44xx() || soc_is_omap54xx()))
  408. return;
  409. sar_base = omap4_get_sar_ram_base();
  410. /* Save old NS_PA_ADDR for validity checks later on */
  411. if (soc_is_omap44xx())
  412. ns_pa_addr = sar_base + CPU1_WAKEUP_NS_PA_ADDR_OFFSET;
  413. else
  414. ns_pa_addr = sar_base + OMAP5_CPU1_WAKEUP_NS_PA_ADDR_OFFSET;
  415. old_cpu1_ns_pa_addr = readl_relaxed(ns_pa_addr);
  416. if (soc_is_omap443x())
  417. startup_pa = __pa_symbol(omap4_secondary_startup);
  418. else if (soc_is_omap446x())
  419. startup_pa = __pa_symbol(omap4460_secondary_startup);
  420. else if ((__boot_cpu_mode & MODE_MASK) == HYP_MODE)
  421. startup_pa = __pa_symbol(omap5_secondary_hyp_startup);
  422. else
  423. startup_pa = __pa_symbol(omap5_secondary_startup);
  424. if (soc_is_omap44xx())
  425. writel_relaxed(startup_pa, sar_base +
  426. CPU1_WAKEUP_NS_PA_ADDR_OFFSET);
  427. else
  428. writel_relaxed(startup_pa, sar_base +
  429. OMAP5_CPU1_WAKEUP_NS_PA_ADDR_OFFSET);
  430. }