pm.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. * Copyright (c) 2011-2012 Samsung Electronics Co., Ltd.
  3. * http://www.samsung.com
  4. *
  5. * EXYNOS - Power Management support
  6. *
  7. * Based on arch/arm/mach-s3c2410/pm.c
  8. * Copyright (c) 2006 Simtec Electronics
  9. * Ben Dooks <ben@simtec.co.uk>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/init.h>
  16. #include <linux/suspend.h>
  17. #include <linux/syscore_ops.h>
  18. #include <linux/cpu_pm.h>
  19. #include <linux/io.h>
  20. #include <linux/irqchip/arm-gic.h>
  21. #include <linux/err.h>
  22. #include <linux/clk.h>
  23. #include <asm/cacheflush.h>
  24. #include <asm/hardware/cache-l2x0.h>
  25. #include <asm/smp_scu.h>
  26. #include <asm/suspend.h>
  27. #include <plat/pm-common.h>
  28. #include <plat/pll.h>
  29. #include <plat/regs-srom.h>
  30. #include <mach/map.h>
  31. #include "common.h"
  32. #include "regs-pmu.h"
  33. /**
  34. * struct exynos_wkup_irq - Exynos GIC to PMU IRQ mapping
  35. * @hwirq: Hardware IRQ signal of the GIC
  36. * @mask: Mask in PMU wake-up mask register
  37. */
  38. struct exynos_wkup_irq {
  39. unsigned int hwirq;
  40. u32 mask;
  41. };
  42. static struct sleep_save exynos5_sys_save[] = {
  43. SAVE_ITEM(EXYNOS5_SYS_I2C_CFG),
  44. };
  45. static struct sleep_save exynos_core_save[] = {
  46. /* SROM side */
  47. SAVE_ITEM(S5P_SROM_BW),
  48. SAVE_ITEM(S5P_SROM_BC0),
  49. SAVE_ITEM(S5P_SROM_BC1),
  50. SAVE_ITEM(S5P_SROM_BC2),
  51. SAVE_ITEM(S5P_SROM_BC3),
  52. };
  53. /*
  54. * GIC wake-up support
  55. */
  56. static u32 exynos_irqwake_intmask = 0xffffffff;
  57. static const struct exynos_wkup_irq exynos4_wkup_irq[] = {
  58. { 76, BIT(1) }, /* RTC alarm */
  59. { 77, BIT(2) }, /* RTC tick */
  60. { /* sentinel */ },
  61. };
  62. static const struct exynos_wkup_irq exynos5250_wkup_irq[] = {
  63. { 75, BIT(1) }, /* RTC alarm */
  64. { 76, BIT(2) }, /* RTC tick */
  65. { /* sentinel */ },
  66. };
  67. static int exynos_irq_set_wake(struct irq_data *data, unsigned int state)
  68. {
  69. const struct exynos_wkup_irq *wkup_irq;
  70. if (soc_is_exynos5250())
  71. wkup_irq = exynos5250_wkup_irq;
  72. else
  73. wkup_irq = exynos4_wkup_irq;
  74. while (wkup_irq->mask) {
  75. if (wkup_irq->hwirq == data->hwirq) {
  76. if (!state)
  77. exynos_irqwake_intmask |= wkup_irq->mask;
  78. else
  79. exynos_irqwake_intmask &= ~wkup_irq->mask;
  80. return 0;
  81. }
  82. ++wkup_irq;
  83. }
  84. return -ENOENT;
  85. }
  86. /**
  87. * exynos_core_power_down : power down the specified cpu
  88. * @cpu : the cpu to power down
  89. *
  90. * Power down the specified cpu. The sequence must be finished by a
  91. * call to cpu_do_idle()
  92. *
  93. */
  94. void exynos_cpu_power_down(int cpu)
  95. {
  96. __raw_writel(0, EXYNOS_ARM_CORE_CONFIGURATION(cpu));
  97. }
  98. /**
  99. * exynos_cpu_power_up : power up the specified cpu
  100. * @cpu : the cpu to power up
  101. *
  102. * Power up the specified cpu
  103. */
  104. void exynos_cpu_power_up(int cpu)
  105. {
  106. __raw_writel(S5P_CORE_LOCAL_PWR_EN,
  107. EXYNOS_ARM_CORE_CONFIGURATION(cpu));
  108. }
  109. /**
  110. * exynos_cpu_power_state : returns the power state of the cpu
  111. * @cpu : the cpu to retrieve the power state from
  112. *
  113. */
  114. int exynos_cpu_power_state(int cpu)
  115. {
  116. return (__raw_readl(EXYNOS_ARM_CORE_STATUS(cpu)) &
  117. S5P_CORE_LOCAL_PWR_EN);
  118. }
  119. /**
  120. * exynos_cluster_power_down : power down the specified cluster
  121. * @cluster : the cluster to power down
  122. */
  123. void exynos_cluster_power_down(int cluster)
  124. {
  125. __raw_writel(0, EXYNOS_COMMON_CONFIGURATION(cluster));
  126. }
  127. /**
  128. * exynos_cluster_power_up : power up the specified cluster
  129. * @cluster : the cluster to power up
  130. */
  131. void exynos_cluster_power_up(int cluster)
  132. {
  133. __raw_writel(S5P_CORE_LOCAL_PWR_EN,
  134. EXYNOS_COMMON_CONFIGURATION(cluster));
  135. }
  136. /**
  137. * exynos_cluster_power_state : returns the power state of the cluster
  138. * @cluster : the cluster to retrieve the power state from
  139. *
  140. */
  141. int exynos_cluster_power_state(int cluster)
  142. {
  143. return (__raw_readl(EXYNOS_COMMON_STATUS(cluster)) &
  144. S5P_CORE_LOCAL_PWR_EN);
  145. }
  146. #define EXYNOS_BOOT_VECTOR_ADDR (samsung_rev() == EXYNOS4210_REV_1_1 ? \
  147. S5P_INFORM7 : (samsung_rev() == EXYNOS4210_REV_1_0 ? \
  148. (sysram_base_addr + 0x24) : S5P_INFORM0))
  149. #define EXYNOS_BOOT_VECTOR_FLAG (samsung_rev() == EXYNOS4210_REV_1_1 ? \
  150. S5P_INFORM6 : (samsung_rev() == EXYNOS4210_REV_1_0 ? \
  151. (sysram_base_addr + 0x20) : S5P_INFORM1))
  152. #define S5P_CHECK_AFTR 0xFCBA0D10
  153. #define S5P_CHECK_SLEEP 0x00000BAD
  154. /* Ext-GIC nIRQ/nFIQ is the only wakeup source in AFTR */
  155. static void exynos_set_wakeupmask(long mask)
  156. {
  157. __raw_writel(mask, S5P_WAKEUP_MASK);
  158. }
  159. static void exynos_cpu_set_boot_vector(long flags)
  160. {
  161. __raw_writel(virt_to_phys(exynos_cpu_resume), EXYNOS_BOOT_VECTOR_ADDR);
  162. __raw_writel(flags, EXYNOS_BOOT_VECTOR_FLAG);
  163. }
  164. void exynos_enter_aftr(void)
  165. {
  166. exynos_set_wakeupmask(0x0000ff3e);
  167. exynos_cpu_set_boot_vector(S5P_CHECK_AFTR);
  168. /* Set value of power down register for aftr mode */
  169. exynos_sys_powerdown_conf(SYS_AFTR);
  170. }
  171. /* For Cortex-A9 Diagnostic and Power control register */
  172. static unsigned int save_arm_register[2];
  173. static void exynos_cpu_save_register(void)
  174. {
  175. unsigned long tmp;
  176. /* Save Power control register */
  177. asm ("mrc p15, 0, %0, c15, c0, 0"
  178. : "=r" (tmp) : : "cc");
  179. save_arm_register[0] = tmp;
  180. /* Save Diagnostic register */
  181. asm ("mrc p15, 0, %0, c15, c0, 1"
  182. : "=r" (tmp) : : "cc");
  183. save_arm_register[1] = tmp;
  184. }
  185. static void exynos_cpu_restore_register(void)
  186. {
  187. unsigned long tmp;
  188. /* Restore Power control register */
  189. tmp = save_arm_register[0];
  190. asm volatile ("mcr p15, 0, %0, c15, c0, 0"
  191. : : "r" (tmp)
  192. : "cc");
  193. /* Restore Diagnostic register */
  194. tmp = save_arm_register[1];
  195. asm volatile ("mcr p15, 0, %0, c15, c0, 1"
  196. : : "r" (tmp)
  197. : "cc");
  198. }
  199. static int exynos_cpu_suspend(unsigned long arg)
  200. {
  201. #ifdef CONFIG_CACHE_L2X0
  202. outer_flush_all();
  203. #endif
  204. if (soc_is_exynos5250())
  205. flush_cache_all();
  206. /* issue the standby signal into the pm unit. */
  207. cpu_do_idle();
  208. pr_info("Failed to suspend the system\n");
  209. return 1; /* Aborting suspend */
  210. }
  211. static void exynos_pm_prepare(void)
  212. {
  213. unsigned int tmp;
  214. /* Set wake-up mask registers */
  215. __raw_writel(exynos_get_eint_wake_mask(), S5P_EINT_WAKEUP_MASK);
  216. __raw_writel(exynos_irqwake_intmask & ~(1 << 31), S5P_WAKEUP_MASK);
  217. s3c_pm_do_save(exynos_core_save, ARRAY_SIZE(exynos_core_save));
  218. if (soc_is_exynos5250()) {
  219. s3c_pm_do_save(exynos5_sys_save, ARRAY_SIZE(exynos5_sys_save));
  220. /* Disable USE_RETENTION of JPEG_MEM_OPTION */
  221. tmp = __raw_readl(EXYNOS5_JPEG_MEM_OPTION);
  222. tmp &= ~EXYNOS5_OPTION_USE_RETENTION;
  223. __raw_writel(tmp, EXYNOS5_JPEG_MEM_OPTION);
  224. }
  225. /* Set value of power down register for sleep mode */
  226. exynos_sys_powerdown_conf(SYS_SLEEP);
  227. __raw_writel(S5P_CHECK_SLEEP, S5P_INFORM1);
  228. /* ensure at least INFORM0 has the resume address */
  229. __raw_writel(virt_to_phys(exynos_cpu_resume), S5P_INFORM0);
  230. }
  231. static void exynos_pm_central_suspend(void)
  232. {
  233. unsigned long tmp;
  234. /* Setting Central Sequence Register for power down mode */
  235. tmp = __raw_readl(S5P_CENTRAL_SEQ_CONFIGURATION);
  236. tmp &= ~S5P_CENTRAL_LOWPWR_CFG;
  237. __raw_writel(tmp, S5P_CENTRAL_SEQ_CONFIGURATION);
  238. }
  239. static int exynos_pm_suspend(void)
  240. {
  241. unsigned long tmp;
  242. exynos_pm_central_suspend();
  243. /* Setting SEQ_OPTION register */
  244. tmp = (S5P_USE_STANDBY_WFI0 | S5P_USE_STANDBY_WFE0);
  245. __raw_writel(tmp, S5P_CENTRAL_SEQ_OPTION);
  246. if (read_cpuid_part_number() == ARM_CPU_PART_CORTEX_A9)
  247. exynos_cpu_save_register();
  248. return 0;
  249. }
  250. static int exynos_pm_central_resume(void)
  251. {
  252. unsigned long tmp;
  253. /*
  254. * If PMU failed while entering sleep mode, WFI will be
  255. * ignored by PMU and then exiting cpu_do_idle().
  256. * S5P_CENTRAL_LOWPWR_CFG bit will not be set automatically
  257. * in this situation.
  258. */
  259. tmp = __raw_readl(S5P_CENTRAL_SEQ_CONFIGURATION);
  260. if (!(tmp & S5P_CENTRAL_LOWPWR_CFG)) {
  261. tmp |= S5P_CENTRAL_LOWPWR_CFG;
  262. __raw_writel(tmp, S5P_CENTRAL_SEQ_CONFIGURATION);
  263. /* clear the wakeup state register */
  264. __raw_writel(0x0, S5P_WAKEUP_STAT);
  265. /* No need to perform below restore code */
  266. return -1;
  267. }
  268. return 0;
  269. }
  270. static void exynos_pm_resume(void)
  271. {
  272. if (exynos_pm_central_resume())
  273. goto early_wakeup;
  274. if (read_cpuid_part_number() == ARM_CPU_PART_CORTEX_A9)
  275. exynos_cpu_restore_register();
  276. /* For release retention */
  277. __raw_writel((1 << 28), S5P_PAD_RET_MAUDIO_OPTION);
  278. __raw_writel((1 << 28), S5P_PAD_RET_GPIO_OPTION);
  279. __raw_writel((1 << 28), S5P_PAD_RET_UART_OPTION);
  280. __raw_writel((1 << 28), S5P_PAD_RET_MMCA_OPTION);
  281. __raw_writel((1 << 28), S5P_PAD_RET_MMCB_OPTION);
  282. __raw_writel((1 << 28), S5P_PAD_RET_EBIA_OPTION);
  283. __raw_writel((1 << 28), S5P_PAD_RET_EBIB_OPTION);
  284. if (soc_is_exynos5250())
  285. s3c_pm_do_restore(exynos5_sys_save,
  286. ARRAY_SIZE(exynos5_sys_save));
  287. s3c_pm_do_restore_core(exynos_core_save, ARRAY_SIZE(exynos_core_save));
  288. if (read_cpuid_part_number() == ARM_CPU_PART_CORTEX_A9)
  289. scu_enable(S5P_VA_SCU);
  290. early_wakeup:
  291. /* Clear SLEEP mode set in INFORM1 */
  292. __raw_writel(0x0, S5P_INFORM1);
  293. return;
  294. }
  295. static struct syscore_ops exynos_pm_syscore_ops = {
  296. .suspend = exynos_pm_suspend,
  297. .resume = exynos_pm_resume,
  298. };
  299. /*
  300. * Suspend Ops
  301. */
  302. static int exynos_suspend_enter(suspend_state_t state)
  303. {
  304. int ret;
  305. s3c_pm_debug_init();
  306. S3C_PMDBG("%s: suspending the system...\n", __func__);
  307. S3C_PMDBG("%s: wakeup masks: %08x,%08x\n", __func__,
  308. exynos_irqwake_intmask, exynos_get_eint_wake_mask());
  309. if (exynos_irqwake_intmask == -1U
  310. && exynos_get_eint_wake_mask() == -1U) {
  311. pr_err("%s: No wake-up sources!\n", __func__);
  312. pr_err("%s: Aborting sleep\n", __func__);
  313. return -EINVAL;
  314. }
  315. s3c_pm_save_uarts();
  316. exynos_pm_prepare();
  317. flush_cache_all();
  318. s3c_pm_check_store();
  319. ret = cpu_suspend(0, exynos_cpu_suspend);
  320. if (ret)
  321. return ret;
  322. s3c_pm_restore_uarts();
  323. S3C_PMDBG("%s: wakeup stat: %08x\n", __func__,
  324. __raw_readl(S5P_WAKEUP_STAT));
  325. s3c_pm_check_restore();
  326. S3C_PMDBG("%s: resuming the system...\n", __func__);
  327. return 0;
  328. }
  329. static int exynos_suspend_prepare(void)
  330. {
  331. s3c_pm_check_prepare();
  332. return 0;
  333. }
  334. static void exynos_suspend_finish(void)
  335. {
  336. s3c_pm_check_cleanup();
  337. }
  338. static const struct platform_suspend_ops exynos_suspend_ops = {
  339. .enter = exynos_suspend_enter,
  340. .prepare = exynos_suspend_prepare,
  341. .finish = exynos_suspend_finish,
  342. .valid = suspend_valid_only_mem,
  343. };
  344. static int exynos_cpu_pm_notifier(struct notifier_block *self,
  345. unsigned long cmd, void *v)
  346. {
  347. int cpu = smp_processor_id();
  348. switch (cmd) {
  349. case CPU_PM_ENTER:
  350. if (cpu == 0) {
  351. exynos_pm_central_suspend();
  352. if (read_cpuid_part_number() == ARM_CPU_PART_CORTEX_A9)
  353. exynos_cpu_save_register();
  354. }
  355. break;
  356. case CPU_PM_EXIT:
  357. if (cpu == 0) {
  358. if (read_cpuid_part_number() ==
  359. ARM_CPU_PART_CORTEX_A9) {
  360. scu_enable(S5P_VA_SCU);
  361. exynos_cpu_restore_register();
  362. }
  363. exynos_pm_central_resume();
  364. }
  365. break;
  366. }
  367. return NOTIFY_OK;
  368. }
  369. static struct notifier_block exynos_cpu_pm_notifier_block = {
  370. .notifier_call = exynos_cpu_pm_notifier,
  371. };
  372. void __init exynos_pm_init(void)
  373. {
  374. u32 tmp;
  375. cpu_pm_register_notifier(&exynos_cpu_pm_notifier_block);
  376. /* Platform-specific GIC callback */
  377. gic_arch_extn.irq_set_wake = exynos_irq_set_wake;
  378. /* All wakeup disable */
  379. tmp = __raw_readl(S5P_WAKEUP_MASK);
  380. tmp |= ((0xFF << 8) | (0x1F << 1));
  381. __raw_writel(tmp, S5P_WAKEUP_MASK);
  382. register_syscore_ops(&exynos_pm_syscore_ops);
  383. suspend_set_ops(&exynos_suspend_ops);
  384. }