mpc52xx_pm.c 5.3 KB

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