pm.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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/regs-srom.h>
  29. #include <mach/map.h>
  30. #include "common.h"
  31. #include "regs-pmu.h"
  32. #include "regs-sys.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. #define EXYNOS_BOOT_VECTOR_ADDR (samsung_rev() == EXYNOS4210_REV_1_1 ? \
  87. pmu_base_addr + S5P_INFORM7 : \
  88. (samsung_rev() == EXYNOS4210_REV_1_0 ? \
  89. (sysram_base_addr + 0x24) : \
  90. pmu_base_addr + S5P_INFORM0))
  91. #define EXYNOS_BOOT_VECTOR_FLAG (samsung_rev() == EXYNOS4210_REV_1_1 ? \
  92. pmu_base_addr + S5P_INFORM6 : \
  93. (samsung_rev() == EXYNOS4210_REV_1_0 ? \
  94. (sysram_base_addr + 0x20) : \
  95. pmu_base_addr + S5P_INFORM1))
  96. #define S5P_CHECK_AFTR 0xFCBA0D10
  97. #define S5P_CHECK_SLEEP 0x00000BAD
  98. /* Ext-GIC nIRQ/nFIQ is the only wakeup source in AFTR */
  99. static void exynos_set_wakeupmask(long mask)
  100. {
  101. pmu_raw_writel(mask, S5P_WAKEUP_MASK);
  102. }
  103. static void exynos_cpu_set_boot_vector(long flags)
  104. {
  105. __raw_writel(virt_to_phys(exynos_cpu_resume), EXYNOS_BOOT_VECTOR_ADDR);
  106. __raw_writel(flags, EXYNOS_BOOT_VECTOR_FLAG);
  107. }
  108. void exynos_enter_aftr(void)
  109. {
  110. exynos_set_wakeupmask(0x0000ff3e);
  111. exynos_cpu_set_boot_vector(S5P_CHECK_AFTR);
  112. /* Set value of power down register for aftr mode */
  113. exynos_sys_powerdown_conf(SYS_AFTR);
  114. }
  115. /* For Cortex-A9 Diagnostic and Power control register */
  116. static unsigned int save_arm_register[2];
  117. static void exynos_cpu_save_register(void)
  118. {
  119. unsigned long tmp;
  120. /* Save Power control register */
  121. asm ("mrc p15, 0, %0, c15, c0, 0"
  122. : "=r" (tmp) : : "cc");
  123. save_arm_register[0] = tmp;
  124. /* Save Diagnostic register */
  125. asm ("mrc p15, 0, %0, c15, c0, 1"
  126. : "=r" (tmp) : : "cc");
  127. save_arm_register[1] = tmp;
  128. }
  129. static void exynos_cpu_restore_register(void)
  130. {
  131. unsigned long tmp;
  132. /* Restore Power control register */
  133. tmp = save_arm_register[0];
  134. asm volatile ("mcr p15, 0, %0, c15, c0, 0"
  135. : : "r" (tmp)
  136. : "cc");
  137. /* Restore Diagnostic register */
  138. tmp = save_arm_register[1];
  139. asm volatile ("mcr p15, 0, %0, c15, c0, 1"
  140. : : "r" (tmp)
  141. : "cc");
  142. }
  143. static int exynos_cpu_suspend(unsigned long arg)
  144. {
  145. #ifdef CONFIG_CACHE_L2X0
  146. outer_flush_all();
  147. #endif
  148. if (soc_is_exynos5250())
  149. flush_cache_all();
  150. /* issue the standby signal into the pm unit. */
  151. cpu_do_idle();
  152. pr_info("Failed to suspend the system\n");
  153. return 1; /* Aborting suspend */
  154. }
  155. static void exynos_pm_prepare(void)
  156. {
  157. unsigned int tmp;
  158. /* Set wake-up mask registers */
  159. pmu_raw_writel(exynos_get_eint_wake_mask(), S5P_EINT_WAKEUP_MASK);
  160. pmu_raw_writel(exynos_irqwake_intmask & ~(1 << 31), S5P_WAKEUP_MASK);
  161. s3c_pm_do_save(exynos_core_save, ARRAY_SIZE(exynos_core_save));
  162. if (soc_is_exynos5250()) {
  163. s3c_pm_do_save(exynos5_sys_save, ARRAY_SIZE(exynos5_sys_save));
  164. /* Disable USE_RETENTION of JPEG_MEM_OPTION */
  165. tmp = pmu_raw_readl(EXYNOS5_JPEG_MEM_OPTION);
  166. tmp &= ~EXYNOS5_OPTION_USE_RETENTION;
  167. pmu_raw_writel(tmp, EXYNOS5_JPEG_MEM_OPTION);
  168. }
  169. /* Set value of power down register for sleep mode */
  170. exynos_sys_powerdown_conf(SYS_SLEEP);
  171. pmu_raw_writel(S5P_CHECK_SLEEP, S5P_INFORM1);
  172. /* ensure at least INFORM0 has the resume address */
  173. pmu_raw_writel(virt_to_phys(exynos_cpu_resume), S5P_INFORM0);
  174. }
  175. static void exynos_pm_central_suspend(void)
  176. {
  177. unsigned long tmp;
  178. /* Setting Central Sequence Register for power down mode */
  179. tmp = pmu_raw_readl(S5P_CENTRAL_SEQ_CONFIGURATION);
  180. tmp &= ~S5P_CENTRAL_LOWPWR_CFG;
  181. pmu_raw_writel(tmp, S5P_CENTRAL_SEQ_CONFIGURATION);
  182. }
  183. static int exynos_pm_suspend(void)
  184. {
  185. unsigned long tmp;
  186. exynos_pm_central_suspend();
  187. /* Setting SEQ_OPTION register */
  188. tmp = (S5P_USE_STANDBY_WFI0 | S5P_USE_STANDBY_WFE0);
  189. pmu_raw_writel(tmp, S5P_CENTRAL_SEQ_OPTION);
  190. if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9)
  191. exynos_cpu_save_register();
  192. return 0;
  193. }
  194. static int exynos_pm_central_resume(void)
  195. {
  196. unsigned long tmp;
  197. /*
  198. * If PMU failed while entering sleep mode, WFI will be
  199. * ignored by PMU and then exiting cpu_do_idle().
  200. * S5P_CENTRAL_LOWPWR_CFG bit will not be set automatically
  201. * in this situation.
  202. */
  203. tmp = pmu_raw_readl(S5P_CENTRAL_SEQ_CONFIGURATION);
  204. if (!(tmp & S5P_CENTRAL_LOWPWR_CFG)) {
  205. tmp |= S5P_CENTRAL_LOWPWR_CFG;
  206. pmu_raw_writel(tmp, S5P_CENTRAL_SEQ_CONFIGURATION);
  207. /* clear the wakeup state register */
  208. pmu_raw_writel(0x0, S5P_WAKEUP_STAT);
  209. /* No need to perform below restore code */
  210. return -1;
  211. }
  212. return 0;
  213. }
  214. static void exynos_pm_resume(void)
  215. {
  216. if (exynos_pm_central_resume())
  217. goto early_wakeup;
  218. if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9)
  219. exynos_cpu_restore_register();
  220. /* For release retention */
  221. pmu_raw_writel((1 << 28), S5P_PAD_RET_MAUDIO_OPTION);
  222. pmu_raw_writel((1 << 28), S5P_PAD_RET_GPIO_OPTION);
  223. pmu_raw_writel((1 << 28), S5P_PAD_RET_UART_OPTION);
  224. pmu_raw_writel((1 << 28), S5P_PAD_RET_MMCA_OPTION);
  225. pmu_raw_writel((1 << 28), S5P_PAD_RET_MMCB_OPTION);
  226. pmu_raw_writel((1 << 28), S5P_PAD_RET_EBIA_OPTION);
  227. pmu_raw_writel((1 << 28), S5P_PAD_RET_EBIB_OPTION);
  228. if (soc_is_exynos5250())
  229. s3c_pm_do_restore(exynos5_sys_save,
  230. ARRAY_SIZE(exynos5_sys_save));
  231. s3c_pm_do_restore_core(exynos_core_save, ARRAY_SIZE(exynos_core_save));
  232. if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9)
  233. scu_enable(S5P_VA_SCU);
  234. early_wakeup:
  235. /* Clear SLEEP mode set in INFORM1 */
  236. pmu_raw_writel(0x0, S5P_INFORM1);
  237. return;
  238. }
  239. static struct syscore_ops exynos_pm_syscore_ops = {
  240. .suspend = exynos_pm_suspend,
  241. .resume = exynos_pm_resume,
  242. };
  243. /*
  244. * Suspend Ops
  245. */
  246. static int exynos_suspend_enter(suspend_state_t state)
  247. {
  248. int ret;
  249. s3c_pm_debug_init();
  250. S3C_PMDBG("%s: suspending the system...\n", __func__);
  251. S3C_PMDBG("%s: wakeup masks: %08x,%08x\n", __func__,
  252. exynos_irqwake_intmask, exynos_get_eint_wake_mask());
  253. if (exynos_irqwake_intmask == -1U
  254. && exynos_get_eint_wake_mask() == -1U) {
  255. pr_err("%s: No wake-up sources!\n", __func__);
  256. pr_err("%s: Aborting sleep\n", __func__);
  257. return -EINVAL;
  258. }
  259. s3c_pm_save_uarts();
  260. exynos_pm_prepare();
  261. flush_cache_all();
  262. s3c_pm_check_store();
  263. ret = cpu_suspend(0, exynos_cpu_suspend);
  264. if (ret)
  265. return ret;
  266. s3c_pm_restore_uarts();
  267. S3C_PMDBG("%s: wakeup stat: %08x\n", __func__,
  268. pmu_raw_readl(S5P_WAKEUP_STAT));
  269. s3c_pm_check_restore();
  270. S3C_PMDBG("%s: resuming the system...\n", __func__);
  271. return 0;
  272. }
  273. static int exynos_suspend_prepare(void)
  274. {
  275. s3c_pm_check_prepare();
  276. return 0;
  277. }
  278. static void exynos_suspend_finish(void)
  279. {
  280. s3c_pm_check_cleanup();
  281. }
  282. static const struct platform_suspend_ops exynos_suspend_ops = {
  283. .enter = exynos_suspend_enter,
  284. .prepare = exynos_suspend_prepare,
  285. .finish = exynos_suspend_finish,
  286. .valid = suspend_valid_only_mem,
  287. };
  288. static int exynos_cpu_pm_notifier(struct notifier_block *self,
  289. unsigned long cmd, void *v)
  290. {
  291. int cpu = smp_processor_id();
  292. switch (cmd) {
  293. case CPU_PM_ENTER:
  294. if (cpu == 0) {
  295. exynos_pm_central_suspend();
  296. if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9)
  297. exynos_cpu_save_register();
  298. }
  299. break;
  300. case CPU_PM_EXIT:
  301. if (cpu == 0) {
  302. if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9) {
  303. scu_enable(S5P_VA_SCU);
  304. exynos_cpu_restore_register();
  305. }
  306. exynos_pm_central_resume();
  307. }
  308. break;
  309. }
  310. return NOTIFY_OK;
  311. }
  312. static struct notifier_block exynos_cpu_pm_notifier_block = {
  313. .notifier_call = exynos_cpu_pm_notifier,
  314. };
  315. void __init exynos_pm_init(void)
  316. {
  317. u32 tmp;
  318. cpu_pm_register_notifier(&exynos_cpu_pm_notifier_block);
  319. /* Platform-specific GIC callback */
  320. gic_arch_extn.irq_set_wake = exynos_irq_set_wake;
  321. /* All wakeup disable */
  322. tmp = pmu_raw_readl(S5P_WAKEUP_MASK);
  323. tmp |= ((0xFF << 8) | (0x1F << 1));
  324. pmu_raw_writel(tmp, S5P_WAKEUP_MASK);
  325. register_syscore_ops(&exynos_pm_syscore_ops);
  326. suspend_set_ops(&exynos_suspend_ops);
  327. }