pm-imx5.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * Copyright (C) 2011 Freescale Semiconductor, Inc. All Rights Reserved.
  3. *
  4. * The code contained herein is licensed under the GNU General Public
  5. * License. You may obtain a copy of the GNU General Public License
  6. * Version 2 or later at the following locations:
  7. *
  8. * http://www.opensource.org/licenses/gpl-license.html
  9. * http://www.gnu.org/copyleft/gpl.html
  10. */
  11. #include <linux/suspend.h>
  12. #include <linux/clk.h>
  13. #include <linux/io.h>
  14. #include <linux/err.h>
  15. #include <linux/export.h>
  16. #include <linux/genalloc.h>
  17. #include <linux/of.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_platform.h>
  20. #include <asm/cacheflush.h>
  21. #include <asm/fncpy.h>
  22. #include <asm/system_misc.h>
  23. #include <asm/tlbflush.h>
  24. #include "common.h"
  25. #include "cpuidle.h"
  26. #include "hardware.h"
  27. #define MXC_CCM_CLPCR 0x54
  28. #define MXC_CCM_CLPCR_LPM_OFFSET 0
  29. #define MXC_CCM_CLPCR_LPM_MASK 0x3
  30. #define MXC_CCM_CLPCR_STBY_COUNT_OFFSET 9
  31. #define MXC_CCM_CLPCR_VSTBY (0x1 << 8)
  32. #define MXC_CCM_CLPCR_SBYOS (0x1 << 6)
  33. #define MXC_CORTEXA8_PLAT_LPC 0xc
  34. #define MXC_CORTEXA8_PLAT_LPC_DSM (1 << 0)
  35. #define MXC_CORTEXA8_PLAT_LPC_DBG_DSM (1 << 1)
  36. #define MXC_SRPG_NEON_SRPGCR 0x280
  37. #define MXC_SRPG_ARM_SRPGCR 0x2a0
  38. #define MXC_SRPG_EMPGC0_SRPGCR 0x2c0
  39. #define MXC_SRPG_EMPGC1_SRPGCR 0x2d0
  40. #define MXC_SRPGCR_PCR 1
  41. /*
  42. * The WAIT_UNCLOCKED_POWER_OFF state only requires <= 500ns to exit.
  43. * This is also the lowest power state possible without affecting
  44. * non-cpu parts of the system. For these reasons, imx5 should default
  45. * to always using this state for cpu idling. The PM_SUSPEND_STANDBY also
  46. * uses this state and needs to take no action when registers remain confgiured
  47. * for this state.
  48. */
  49. #define IMX5_DEFAULT_CPU_IDLE_STATE WAIT_UNCLOCKED_POWER_OFF
  50. struct imx5_suspend_io_state {
  51. u32 offset;
  52. u32 clear;
  53. u32 set;
  54. u32 saved_value;
  55. };
  56. struct imx5_pm_data {
  57. phys_addr_t ccm_addr;
  58. phys_addr_t cortex_addr;
  59. phys_addr_t gpc_addr;
  60. phys_addr_t m4if_addr;
  61. phys_addr_t iomuxc_addr;
  62. void (*suspend_asm)(void __iomem *ocram_vbase);
  63. const u32 *suspend_asm_sz;
  64. const struct imx5_suspend_io_state *suspend_io_config;
  65. int suspend_io_count;
  66. };
  67. static const struct imx5_suspend_io_state imx53_suspend_io_config[] = {
  68. #define MX53_DSE_HIGHZ_MASK (0x7 << 19)
  69. {.offset = 0x584, .clear = MX53_DSE_HIGHZ_MASK}, /* DQM0 */
  70. {.offset = 0x594, .clear = MX53_DSE_HIGHZ_MASK}, /* DQM1 */
  71. {.offset = 0x560, .clear = MX53_DSE_HIGHZ_MASK}, /* DQM2 */
  72. {.offset = 0x554, .clear = MX53_DSE_HIGHZ_MASK}, /* DQM3 */
  73. {.offset = 0x574, .clear = MX53_DSE_HIGHZ_MASK}, /* CAS */
  74. {.offset = 0x588, .clear = MX53_DSE_HIGHZ_MASK}, /* RAS */
  75. {.offset = 0x578, .clear = MX53_DSE_HIGHZ_MASK}, /* SDCLK_0 */
  76. {.offset = 0x570, .clear = MX53_DSE_HIGHZ_MASK}, /* SDCLK_1 */
  77. {.offset = 0x580, .clear = MX53_DSE_HIGHZ_MASK}, /* SDODT0 */
  78. {.offset = 0x564, .clear = MX53_DSE_HIGHZ_MASK}, /* SDODT1 */
  79. {.offset = 0x57c, .clear = MX53_DSE_HIGHZ_MASK}, /* SDQS0 */
  80. {.offset = 0x590, .clear = MX53_DSE_HIGHZ_MASK}, /* SDQS1 */
  81. {.offset = 0x568, .clear = MX53_DSE_HIGHZ_MASK}, /* SDQS2 */
  82. {.offset = 0x558, .clear = MX53_DSE_HIGHZ_MASK}, /* SDSQ3 */
  83. {.offset = 0x6f0, .clear = MX53_DSE_HIGHZ_MASK}, /* GRP_ADDS */
  84. {.offset = 0x718, .clear = MX53_DSE_HIGHZ_MASK}, /* GRP_BODS */
  85. {.offset = 0x71c, .clear = MX53_DSE_HIGHZ_MASK}, /* GRP_B1DS */
  86. {.offset = 0x728, .clear = MX53_DSE_HIGHZ_MASK}, /* GRP_B2DS */
  87. {.offset = 0x72c, .clear = MX53_DSE_HIGHZ_MASK}, /* GRP_B3DS */
  88. /* Controls the CKE signal which is required to leave self refresh */
  89. {.offset = 0x720, .clear = MX53_DSE_HIGHZ_MASK, .set = 1 << 19}, /* CTLDS */
  90. };
  91. static const struct imx5_pm_data imx51_pm_data __initconst = {
  92. .ccm_addr = 0x73fd4000,
  93. .cortex_addr = 0x83fa0000,
  94. .gpc_addr = 0x73fd8000,
  95. };
  96. static const struct imx5_pm_data imx53_pm_data __initconst = {
  97. .ccm_addr = 0x53fd4000,
  98. .cortex_addr = 0x63fa0000,
  99. .gpc_addr = 0x53fd8000,
  100. .m4if_addr = 0x63fd8000,
  101. .iomuxc_addr = 0x53fa8000,
  102. .suspend_asm = &imx53_suspend,
  103. .suspend_asm_sz = &imx53_suspend_sz,
  104. .suspend_io_config = imx53_suspend_io_config,
  105. .suspend_io_count = ARRAY_SIZE(imx53_suspend_io_config),
  106. };
  107. #define MX5_MAX_SUSPEND_IOSTATE ARRAY_SIZE(imx53_suspend_io_config)
  108. /*
  109. * This structure is for passing necessary data for low level ocram
  110. * suspend code(arch/arm/mach-imx/suspend-imx53.S), if this struct
  111. * definition is changed, the offset definition in that file
  112. * must be also changed accordingly otherwise, the suspend to ocram
  113. * function will be broken!
  114. */
  115. struct imx5_cpu_suspend_info {
  116. void __iomem *m4if_base;
  117. void __iomem *iomuxc_base;
  118. u32 io_count;
  119. struct imx5_suspend_io_state io_state[MX5_MAX_SUSPEND_IOSTATE];
  120. } __aligned(8);
  121. static void __iomem *ccm_base;
  122. static void __iomem *cortex_base;
  123. static void __iomem *gpc_base;
  124. static void __iomem *suspend_ocram_base;
  125. static void (*imx5_suspend_in_ocram_fn)(void __iomem *ocram_vbase);
  126. /*
  127. * set cpu low power mode before WFI instruction. This function is called
  128. * mx5 because it can be used for mx51, and mx53.
  129. */
  130. static void mx5_cpu_lp_set(enum mxc_cpu_pwr_mode mode)
  131. {
  132. u32 plat_lpc, arm_srpgcr, ccm_clpcr;
  133. u32 empgc0, empgc1;
  134. int stop_mode = 0;
  135. /* always allow platform to issue a deep sleep mode request */
  136. plat_lpc = imx_readl(cortex_base + MXC_CORTEXA8_PLAT_LPC) &
  137. ~(MXC_CORTEXA8_PLAT_LPC_DSM);
  138. ccm_clpcr = imx_readl(ccm_base + MXC_CCM_CLPCR) &
  139. ~(MXC_CCM_CLPCR_LPM_MASK);
  140. arm_srpgcr = imx_readl(gpc_base + MXC_SRPG_ARM_SRPGCR) &
  141. ~(MXC_SRPGCR_PCR);
  142. empgc0 = imx_readl(gpc_base + MXC_SRPG_EMPGC0_SRPGCR) &
  143. ~(MXC_SRPGCR_PCR);
  144. empgc1 = imx_readl(gpc_base + MXC_SRPG_EMPGC1_SRPGCR) &
  145. ~(MXC_SRPGCR_PCR);
  146. switch (mode) {
  147. case WAIT_CLOCKED:
  148. break;
  149. case WAIT_UNCLOCKED:
  150. ccm_clpcr |= 0x1 << MXC_CCM_CLPCR_LPM_OFFSET;
  151. break;
  152. case WAIT_UNCLOCKED_POWER_OFF:
  153. case STOP_POWER_OFF:
  154. plat_lpc |= MXC_CORTEXA8_PLAT_LPC_DSM
  155. | MXC_CORTEXA8_PLAT_LPC_DBG_DSM;
  156. if (mode == WAIT_UNCLOCKED_POWER_OFF) {
  157. ccm_clpcr |= 0x1 << MXC_CCM_CLPCR_LPM_OFFSET;
  158. ccm_clpcr &= ~MXC_CCM_CLPCR_VSTBY;
  159. ccm_clpcr &= ~MXC_CCM_CLPCR_SBYOS;
  160. stop_mode = 0;
  161. } else {
  162. ccm_clpcr |= 0x2 << MXC_CCM_CLPCR_LPM_OFFSET;
  163. ccm_clpcr |= 0x3 << MXC_CCM_CLPCR_STBY_COUNT_OFFSET;
  164. ccm_clpcr |= MXC_CCM_CLPCR_VSTBY;
  165. ccm_clpcr |= MXC_CCM_CLPCR_SBYOS;
  166. stop_mode = 1;
  167. }
  168. arm_srpgcr |= MXC_SRPGCR_PCR;
  169. break;
  170. case STOP_POWER_ON:
  171. ccm_clpcr |= 0x2 << MXC_CCM_CLPCR_LPM_OFFSET;
  172. break;
  173. default:
  174. printk(KERN_WARNING "UNKNOWN cpu power mode: %d\n", mode);
  175. return;
  176. }
  177. imx_writel(plat_lpc, cortex_base + MXC_CORTEXA8_PLAT_LPC);
  178. imx_writel(ccm_clpcr, ccm_base + MXC_CCM_CLPCR);
  179. imx_writel(arm_srpgcr, gpc_base + MXC_SRPG_ARM_SRPGCR);
  180. imx_writel(arm_srpgcr, gpc_base + MXC_SRPG_NEON_SRPGCR);
  181. if (stop_mode) {
  182. empgc0 |= MXC_SRPGCR_PCR;
  183. empgc1 |= MXC_SRPGCR_PCR;
  184. imx_writel(empgc0, gpc_base + MXC_SRPG_EMPGC0_SRPGCR);
  185. imx_writel(empgc1, gpc_base + MXC_SRPG_EMPGC1_SRPGCR);
  186. }
  187. }
  188. static int mx5_suspend_enter(suspend_state_t state)
  189. {
  190. switch (state) {
  191. case PM_SUSPEND_MEM:
  192. mx5_cpu_lp_set(STOP_POWER_OFF);
  193. break;
  194. case PM_SUSPEND_STANDBY:
  195. /* DEFAULT_IDLE_STATE already configured */
  196. break;
  197. default:
  198. return -EINVAL;
  199. }
  200. if (state == PM_SUSPEND_MEM) {
  201. local_flush_tlb_all();
  202. flush_cache_all();
  203. /*clear the EMPGC0/1 bits */
  204. imx_writel(0, gpc_base + MXC_SRPG_EMPGC0_SRPGCR);
  205. imx_writel(0, gpc_base + MXC_SRPG_EMPGC1_SRPGCR);
  206. if (imx5_suspend_in_ocram_fn)
  207. imx5_suspend_in_ocram_fn(suspend_ocram_base);
  208. else
  209. cpu_do_idle();
  210. } else {
  211. cpu_do_idle();
  212. }
  213. /* return registers to default idle state */
  214. mx5_cpu_lp_set(IMX5_DEFAULT_CPU_IDLE_STATE);
  215. return 0;
  216. }
  217. static int mx5_pm_valid(suspend_state_t state)
  218. {
  219. return (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX);
  220. }
  221. static const struct platform_suspend_ops mx5_suspend_ops = {
  222. .valid = mx5_pm_valid,
  223. .enter = mx5_suspend_enter,
  224. };
  225. static inline int imx5_cpu_do_idle(void)
  226. {
  227. int ret = tzic_enable_wake();
  228. if (likely(!ret))
  229. cpu_do_idle();
  230. return ret;
  231. }
  232. static void imx5_pm_idle(void)
  233. {
  234. imx5_cpu_do_idle();
  235. }
  236. static int __init imx_suspend_alloc_ocram(
  237. size_t size,
  238. void __iomem **virt_out,
  239. phys_addr_t *phys_out)
  240. {
  241. struct device_node *node;
  242. struct platform_device *pdev;
  243. struct gen_pool *ocram_pool;
  244. unsigned long ocram_base;
  245. void __iomem *virt;
  246. phys_addr_t phys;
  247. int ret = 0;
  248. /* Copied from imx6: TODO factorize */
  249. node = of_find_compatible_node(NULL, NULL, "mmio-sram");
  250. if (!node) {
  251. pr_warn("%s: failed to find ocram node!\n", __func__);
  252. return -ENODEV;
  253. }
  254. pdev = of_find_device_by_node(node);
  255. if (!pdev) {
  256. pr_warn("%s: failed to find ocram device!\n", __func__);
  257. ret = -ENODEV;
  258. goto put_node;
  259. }
  260. ocram_pool = gen_pool_get(&pdev->dev, NULL);
  261. if (!ocram_pool) {
  262. pr_warn("%s: ocram pool unavailable!\n", __func__);
  263. ret = -ENODEV;
  264. goto put_node;
  265. }
  266. ocram_base = gen_pool_alloc(ocram_pool, size);
  267. if (!ocram_base) {
  268. pr_warn("%s: unable to alloc ocram!\n", __func__);
  269. ret = -ENOMEM;
  270. goto put_node;
  271. }
  272. phys = gen_pool_virt_to_phys(ocram_pool, ocram_base);
  273. virt = __arm_ioremap_exec(phys, size, false);
  274. if (phys_out)
  275. *phys_out = phys;
  276. if (virt_out)
  277. *virt_out = virt;
  278. put_node:
  279. of_node_put(node);
  280. return ret;
  281. }
  282. static int __init imx5_suspend_init(const struct imx5_pm_data *soc_data)
  283. {
  284. struct imx5_cpu_suspend_info *suspend_info;
  285. int ret;
  286. /* Need this to avoid compile error due to const typeof in fncpy.h */
  287. void (*suspend_asm)(void __iomem *) = soc_data->suspend_asm;
  288. if (!suspend_asm)
  289. return 0;
  290. if (!soc_data->suspend_asm_sz || !*soc_data->suspend_asm_sz)
  291. return -EINVAL;
  292. ret = imx_suspend_alloc_ocram(
  293. *soc_data->suspend_asm_sz + sizeof(*suspend_info),
  294. &suspend_ocram_base, NULL);
  295. if (ret)
  296. return ret;
  297. suspend_info = suspend_ocram_base;
  298. suspend_info->io_count = soc_data->suspend_io_count;
  299. memcpy(suspend_info->io_state, soc_data->suspend_io_config,
  300. sizeof(*suspend_info->io_state) * soc_data->suspend_io_count);
  301. suspend_info->m4if_base = ioremap(soc_data->m4if_addr, SZ_16K);
  302. if (!suspend_info->m4if_base) {
  303. ret = -ENOMEM;
  304. goto failed_map_m4if;
  305. }
  306. suspend_info->iomuxc_base = ioremap(soc_data->iomuxc_addr, SZ_16K);
  307. if (!suspend_info->iomuxc_base) {
  308. ret = -ENOMEM;
  309. goto failed_map_iomuxc;
  310. }
  311. imx5_suspend_in_ocram_fn = fncpy(
  312. suspend_ocram_base + sizeof(*suspend_info),
  313. suspend_asm,
  314. *soc_data->suspend_asm_sz);
  315. return 0;
  316. failed_map_iomuxc:
  317. iounmap(suspend_info->m4if_base);
  318. failed_map_m4if:
  319. return ret;
  320. }
  321. static int __init imx5_pm_common_init(const struct imx5_pm_data *data)
  322. {
  323. int ret;
  324. struct clk *gpc_dvfs_clk = clk_get(NULL, "gpc_dvfs");
  325. if (IS_ERR(gpc_dvfs_clk))
  326. return PTR_ERR(gpc_dvfs_clk);
  327. ret = clk_prepare_enable(gpc_dvfs_clk);
  328. if (ret)
  329. return ret;
  330. arm_pm_idle = imx5_pm_idle;
  331. ccm_base = ioremap(data->ccm_addr, SZ_16K);
  332. cortex_base = ioremap(data->cortex_addr, SZ_16K);
  333. gpc_base = ioremap(data->gpc_addr, SZ_16K);
  334. WARN_ON(!ccm_base || !cortex_base || !gpc_base);
  335. /* Set the registers to the default cpu idle state. */
  336. mx5_cpu_lp_set(IMX5_DEFAULT_CPU_IDLE_STATE);
  337. ret = imx5_cpuidle_init();
  338. if (ret)
  339. pr_warn("%s: cpuidle init failed %d\n", __func__, ret);
  340. ret = imx5_suspend_init(data);
  341. if (ret)
  342. pr_warn("%s: No DDR LPM support with suspend %d!\n",
  343. __func__, ret);
  344. suspend_set_ops(&mx5_suspend_ops);
  345. return 0;
  346. }
  347. void __init imx51_pm_init(void)
  348. {
  349. if (IS_ENABLED(CONFIG_SOC_IMX51))
  350. imx5_pm_common_init(&imx51_pm_data);
  351. }
  352. void __init imx53_pm_init(void)
  353. {
  354. if (IS_ENABLED(CONFIG_SOC_IMX53))
  355. imx5_pm_common_init(&imx53_pm_data);
  356. }