mpc52xx_pm.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/init.h>
  3. #include <linux/suspend.h>
  4. #include <linux/io.h>
  5. #include <linux/of_address.h>
  6. #include <asm/time.h>
  7. #include <asm/cacheflush.h>
  8. #include <asm/mpc52xx.h>
  9. /* these are defined in mpc52xx_sleep.S, and only used here */
  10. extern void mpc52xx_deep_sleep(void __iomem *sram, void __iomem *sdram_regs,
  11. struct mpc52xx_cdm __iomem *, struct mpc52xx_intr __iomem*);
  12. extern void mpc52xx_ds_sram(void);
  13. extern const long mpc52xx_ds_sram_size;
  14. extern void mpc52xx_ds_cached(void);
  15. extern const long mpc52xx_ds_cached_size;
  16. static void __iomem *mbar;
  17. static void __iomem *sdram;
  18. static struct mpc52xx_cdm __iomem *cdm;
  19. static struct mpc52xx_intr __iomem *intr;
  20. static struct mpc52xx_gpio_wkup __iomem *gpiow;
  21. static void __iomem *sram;
  22. static int sram_size;
  23. struct mpc52xx_suspend mpc52xx_suspend;
  24. static int mpc52xx_pm_valid(suspend_state_t state)
  25. {
  26. switch (state) {
  27. case PM_SUSPEND_STANDBY:
  28. return 1;
  29. default:
  30. return 0;
  31. }
  32. }
  33. int mpc52xx_set_wakeup_gpio(u8 pin, u8 level)
  34. {
  35. u16 tmp;
  36. /* enable gpio */
  37. out_8(&gpiow->wkup_gpioe, in_8(&gpiow->wkup_gpioe) | (1 << pin));
  38. /* set as input */
  39. out_8(&gpiow->wkup_ddr, in_8(&gpiow->wkup_ddr) & ~(1 << pin));
  40. /* enable deep sleep interrupt */
  41. out_8(&gpiow->wkup_inten, in_8(&gpiow->wkup_inten) | (1 << pin));
  42. /* low/high level creates wakeup interrupt */
  43. tmp = in_be16(&gpiow->wkup_itype);
  44. tmp &= ~(0x3 << (pin * 2));
  45. tmp |= (!level + 1) << (pin * 2);
  46. out_be16(&gpiow->wkup_itype, tmp);
  47. /* master enable */
  48. out_8(&gpiow->wkup_maste, 1);
  49. return 0;
  50. }
  51. int mpc52xx_pm_prepare(void)
  52. {
  53. struct device_node *np;
  54. static const struct of_device_id immr_ids[] = {
  55. { .compatible = "fsl,mpc5200-immr", },
  56. { .compatible = "fsl,mpc5200b-immr", },
  57. { .type = "soc", .compatible = "mpc5200", }, /* lite5200 */
  58. { .type = "builtin", .compatible = "mpc5200", }, /* efika */
  59. {}
  60. };
  61. struct resource res;
  62. /* map the whole register space */
  63. np = of_find_matching_node(NULL, immr_ids);
  64. if (of_address_to_resource(np, 0, &res)) {
  65. pr_err("mpc52xx_pm_prepare(): could not get IMMR address\n");
  66. of_node_put(np);
  67. return -ENOSYS;
  68. }
  69. mbar = ioremap(res.start, 0xc000); /* we should map whole region including SRAM */
  70. of_node_put(np);
  71. if (!mbar) {
  72. pr_err("mpc52xx_pm_prepare(): could not map registers\n");
  73. return -ENOSYS;
  74. }
  75. /* these offsets are from mpc5200 users manual */
  76. sdram = mbar + 0x100;
  77. cdm = mbar + 0x200;
  78. intr = mbar + 0x500;
  79. gpiow = mbar + 0xc00;
  80. sram = mbar + 0x8000; /* Those will be handled by the */
  81. sram_size = 0x4000; /* bestcomm driver soon */
  82. /* call board suspend code, if applicable */
  83. if (mpc52xx_suspend.board_suspend_prepare)
  84. mpc52xx_suspend.board_suspend_prepare(mbar);
  85. else {
  86. printk(KERN_ALERT "%s: %i don't know how to wake up the board\n",
  87. __func__, __LINE__);
  88. goto out_unmap;
  89. }
  90. return 0;
  91. out_unmap:
  92. iounmap(mbar);
  93. return -ENOSYS;
  94. }
  95. char saved_sram[0x4000];
  96. int mpc52xx_pm_enter(suspend_state_t state)
  97. {
  98. u32 clk_enables;
  99. u32 msr, hid0;
  100. u32 intr_main_mask;
  101. void __iomem * irq_0x500 = (void __iomem *)CONFIG_KERNEL_START + 0x500;
  102. unsigned long irq_0x500_stop = (unsigned long)irq_0x500 + mpc52xx_ds_cached_size;
  103. char saved_0x500[0x600-0x500];
  104. if (WARN_ON(mpc52xx_ds_cached_size > sizeof(saved_0x500)))
  105. return -ENOMEM;
  106. /* disable all interrupts in PIC */
  107. intr_main_mask = in_be32(&intr->main_mask);
  108. out_be32(&intr->main_mask, intr_main_mask | 0x1ffff);
  109. /* don't let DEC expire any time soon */
  110. mtspr(SPRN_DEC, 0x7fffffff);
  111. /* save SRAM */
  112. memcpy(saved_sram, sram, sram_size);
  113. /* copy low level suspend code to sram */
  114. memcpy(sram, mpc52xx_ds_sram, mpc52xx_ds_sram_size);
  115. out_8(&cdm->ccs_sleep_enable, 1);
  116. out_8(&cdm->osc_sleep_enable, 1);
  117. out_8(&cdm->ccs_qreq_test, 1);
  118. /* disable all but SDRAM and bestcomm (SRAM) clocks */
  119. clk_enables = in_be32(&cdm->clk_enables);
  120. out_be32(&cdm->clk_enables, clk_enables & 0x00088000);
  121. /* disable power management */
  122. msr = mfmsr();
  123. mtmsr(msr & ~MSR_POW);
  124. /* enable sleep mode, disable others */
  125. hid0 = mfspr(SPRN_HID0);
  126. mtspr(SPRN_HID0, (hid0 & ~(HID0_DOZE | HID0_NAP | HID0_DPM)) | HID0_SLEEP);
  127. /* save original, copy our irq handler, flush from dcache and invalidate icache */
  128. memcpy(saved_0x500, irq_0x500, mpc52xx_ds_cached_size);
  129. memcpy(irq_0x500, mpc52xx_ds_cached, mpc52xx_ds_cached_size);
  130. flush_icache_range((unsigned long)irq_0x500, irq_0x500_stop);
  131. /* call low-level sleep code */
  132. mpc52xx_deep_sleep(sram, sdram, cdm, intr);
  133. /* restore original irq handler */
  134. memcpy(irq_0x500, saved_0x500, mpc52xx_ds_cached_size);
  135. flush_icache_range((unsigned long)irq_0x500, irq_0x500_stop);
  136. /* restore old power mode */
  137. mtmsr(msr & ~MSR_POW);
  138. mtspr(SPRN_HID0, hid0);
  139. mtmsr(msr);
  140. out_be32(&cdm->clk_enables, clk_enables);
  141. out_8(&cdm->ccs_sleep_enable, 0);
  142. out_8(&cdm->osc_sleep_enable, 0);
  143. /* restore SRAM */
  144. memcpy(sram, saved_sram, sram_size);
  145. /* reenable interrupts in PIC */
  146. out_be32(&intr->main_mask, intr_main_mask);
  147. return 0;
  148. }
  149. void mpc52xx_pm_finish(void)
  150. {
  151. /* call board resume code */
  152. if (mpc52xx_suspend.board_resume_finish)
  153. mpc52xx_suspend.board_resume_finish(mbar);
  154. iounmap(mbar);
  155. }
  156. static const struct platform_suspend_ops mpc52xx_pm_ops = {
  157. .valid = mpc52xx_pm_valid,
  158. .prepare = mpc52xx_pm_prepare,
  159. .enter = mpc52xx_pm_enter,
  160. .finish = mpc52xx_pm_finish,
  161. };
  162. int __init mpc52xx_pm_init(void)
  163. {
  164. suspend_set_ops(&mpc52xx_pm_ops);
  165. return 0;
  166. }