pm.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Suspend/resume support. Currently supporting Armada XP only.
  4. *
  5. * Copyright (C) 2014 Marvell
  6. *
  7. * Thomas Petazzoni <[email protected]>
  8. */
  9. #include <linux/cpu_pm.h>
  10. #include <linux/delay.h>
  11. #include <linux/gpio.h>
  12. #include <linux/io.h>
  13. #include <linux/kernel.h>
  14. #include <linux/mbus.h>
  15. #include <linux/of_address.h>
  16. #include <linux/suspend.h>
  17. #include <asm/cacheflush.h>
  18. #include <asm/outercache.h>
  19. #include <asm/suspend.h>
  20. #include "coherency.h"
  21. #include "common.h"
  22. #include "pmsu.h"
  23. #define SDRAM_CONFIG_OFFS 0x0
  24. #define SDRAM_CONFIG_SR_MODE_BIT BIT(24)
  25. #define SDRAM_OPERATION_OFFS 0x18
  26. #define SDRAM_OPERATION_SELF_REFRESH 0x7
  27. #define SDRAM_DLB_EVICTION_OFFS 0x30c
  28. #define SDRAM_DLB_EVICTION_THRESHOLD_MASK 0xff
  29. static void (*mvebu_board_pm_enter)(void __iomem *sdram_reg, u32 srcmd);
  30. static void __iomem *sdram_ctrl;
  31. static int mvebu_pm_powerdown(unsigned long data)
  32. {
  33. u32 reg, srcmd;
  34. flush_cache_all();
  35. outer_flush_all();
  36. /*
  37. * Issue a Data Synchronization Barrier instruction to ensure
  38. * that all state saving has been completed.
  39. */
  40. dsb();
  41. /* Flush the DLB and wait ~7 usec */
  42. reg = readl(sdram_ctrl + SDRAM_DLB_EVICTION_OFFS);
  43. reg &= ~SDRAM_DLB_EVICTION_THRESHOLD_MASK;
  44. writel(reg, sdram_ctrl + SDRAM_DLB_EVICTION_OFFS);
  45. udelay(7);
  46. /* Set DRAM in battery backup mode */
  47. reg = readl(sdram_ctrl + SDRAM_CONFIG_OFFS);
  48. reg &= ~SDRAM_CONFIG_SR_MODE_BIT;
  49. writel(reg, sdram_ctrl + SDRAM_CONFIG_OFFS);
  50. /* Prepare to go to self-refresh */
  51. srcmd = readl(sdram_ctrl + SDRAM_OPERATION_OFFS);
  52. srcmd &= ~0x1F;
  53. srcmd |= SDRAM_OPERATION_SELF_REFRESH;
  54. mvebu_board_pm_enter(sdram_ctrl + SDRAM_OPERATION_OFFS, srcmd);
  55. return 0;
  56. }
  57. #define BOOT_INFO_ADDR 0x3000
  58. #define BOOT_MAGIC_WORD 0xdeadb002
  59. #define BOOT_MAGIC_LIST_END 0xffffffff
  60. /*
  61. * Those registers are accessed before switching the internal register
  62. * base, which is why we hardcode the 0xd0000000 base address, the one
  63. * used by the SoC out of reset.
  64. */
  65. #define MBUS_WINDOW_12_CTRL 0xd00200b0
  66. #define MBUS_INTERNAL_REG_ADDRESS 0xd0020080
  67. #define SDRAM_WIN_BASE_REG(x) (0x20180 + (0x8*x))
  68. #define SDRAM_WIN_CTRL_REG(x) (0x20184 + (0x8*x))
  69. static phys_addr_t mvebu_internal_reg_base(void)
  70. {
  71. struct device_node *np;
  72. __be32 in_addr[2];
  73. np = of_find_node_by_name(NULL, "internal-regs");
  74. BUG_ON(!np);
  75. /*
  76. * Ask the DT what is the internal register address on this
  77. * platform. In the mvebu-mbus DT binding, 0xf0010000
  78. * corresponds to the internal register window.
  79. */
  80. in_addr[0] = cpu_to_be32(0xf0010000);
  81. in_addr[1] = 0x0;
  82. return of_translate_address(np, in_addr);
  83. }
  84. static void mvebu_pm_store_armadaxp_bootinfo(u32 *store_addr)
  85. {
  86. phys_addr_t resume_pc;
  87. resume_pc = __pa_symbol(armada_370_xp_cpu_resume);
  88. /*
  89. * The bootloader expects the first two words to be a magic
  90. * value (BOOT_MAGIC_WORD), followed by the address of the
  91. * resume code to jump to. Then, it expects a sequence of
  92. * (address, value) pairs, which can be used to restore the
  93. * value of certain registers. This sequence must end with the
  94. * BOOT_MAGIC_LIST_END magic value.
  95. */
  96. writel(BOOT_MAGIC_WORD, store_addr++);
  97. writel(resume_pc, store_addr++);
  98. /*
  99. * Some platforms remap their internal register base address
  100. * to 0xf1000000. However, out of reset, window 12 starts at
  101. * 0xf0000000 and ends at 0xf7ffffff, which would overlap with
  102. * the internal registers. Therefore, disable window 12.
  103. */
  104. writel(MBUS_WINDOW_12_CTRL, store_addr++);
  105. writel(0x0, store_addr++);
  106. /*
  107. * Set the internal register base address to the value
  108. * expected by Linux, as read from the Device Tree.
  109. */
  110. writel(MBUS_INTERNAL_REG_ADDRESS, store_addr++);
  111. writel(mvebu_internal_reg_base(), store_addr++);
  112. /*
  113. * Ask the mvebu-mbus driver to store the SDRAM window
  114. * configuration, which has to be restored by the bootloader
  115. * before re-entering the kernel on resume.
  116. */
  117. store_addr += mvebu_mbus_save_cpu_target(store_addr);
  118. writel(BOOT_MAGIC_LIST_END, store_addr);
  119. }
  120. static int mvebu_pm_store_bootinfo(void)
  121. {
  122. u32 *store_addr;
  123. store_addr = phys_to_virt(BOOT_INFO_ADDR);
  124. if (of_machine_is_compatible("marvell,armadaxp"))
  125. mvebu_pm_store_armadaxp_bootinfo(store_addr);
  126. else
  127. return -ENODEV;
  128. return 0;
  129. }
  130. static int mvebu_enter_suspend(void)
  131. {
  132. int ret;
  133. ret = mvebu_pm_store_bootinfo();
  134. if (ret)
  135. return ret;
  136. cpu_pm_enter();
  137. cpu_suspend(0, mvebu_pm_powerdown);
  138. outer_resume();
  139. mvebu_v7_pmsu_idle_exit();
  140. set_cpu_coherent();
  141. cpu_pm_exit();
  142. return 0;
  143. }
  144. static int mvebu_pm_enter(suspend_state_t state)
  145. {
  146. switch (state) {
  147. case PM_SUSPEND_STANDBY:
  148. cpu_do_idle();
  149. break;
  150. case PM_SUSPEND_MEM:
  151. pr_warn("Entering suspend to RAM. Only special wake-up sources will resume the system\n");
  152. return mvebu_enter_suspend();
  153. default:
  154. return -EINVAL;
  155. }
  156. return 0;
  157. }
  158. static int mvebu_pm_valid(suspend_state_t state)
  159. {
  160. if (state == PM_SUSPEND_STANDBY)
  161. return 1;
  162. if (state == PM_SUSPEND_MEM && mvebu_board_pm_enter != NULL)
  163. return 1;
  164. return 0;
  165. }
  166. static const struct platform_suspend_ops mvebu_pm_ops = {
  167. .enter = mvebu_pm_enter,
  168. .valid = mvebu_pm_valid,
  169. };
  170. static int __init mvebu_pm_init(void)
  171. {
  172. if (!of_machine_is_compatible("marvell,armadaxp") &&
  173. !of_machine_is_compatible("marvell,armada370") &&
  174. !of_machine_is_compatible("marvell,armada380") &&
  175. !of_machine_is_compatible("marvell,armada390"))
  176. return -ENODEV;
  177. suspend_set_ops(&mvebu_pm_ops);
  178. return 0;
  179. }
  180. late_initcall(mvebu_pm_init);
  181. int __init mvebu_pm_suspend_init(void (*board_pm_enter)(void __iomem *sdram_reg,
  182. u32 srcmd))
  183. {
  184. struct device_node *np;
  185. struct resource res;
  186. np = of_find_compatible_node(NULL, NULL,
  187. "marvell,armada-xp-sdram-controller");
  188. if (!np)
  189. return -ENODEV;
  190. if (of_address_to_resource(np, 0, &res)) {
  191. of_node_put(np);
  192. return -ENODEV;
  193. }
  194. if (!request_mem_region(res.start, resource_size(&res),
  195. np->full_name)) {
  196. of_node_put(np);
  197. return -EBUSY;
  198. }
  199. sdram_ctrl = ioremap(res.start, resource_size(&res));
  200. if (!sdram_ctrl) {
  201. release_mem_region(res.start, resource_size(&res));
  202. of_node_put(np);
  203. return -ENOMEM;
  204. }
  205. of_node_put(np);
  206. mvebu_board_pm_enter = board_pm_enter;
  207. return 0;
  208. }