mpc52xx_pm.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include <linux/init.h>
  2. #include <linux/suspend.h>
  3. #include <linux/io.h>
  4. #include <asm/time.h>
  5. #include <asm/cacheflush.h>
  6. #include <asm/mpc52xx.h>
  7. #include "mpc52xx_pic.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. /* map the whole register space */
  61. np = of_find_matching_node(NULL, immr_ids);
  62. mbar = of_iomap(np, 0);
  63. of_node_put(np);
  64. if (!mbar) {
  65. pr_err("mpc52xx_pm_prepare(): could not map registers\n");
  66. return -ENOSYS;
  67. }
  68. /* these offsets are from mpc5200 users manual */
  69. sdram = mbar + 0x100;
  70. cdm = mbar + 0x200;
  71. intr = mbar + 0x500;
  72. gpiow = mbar + 0xc00;
  73. sram = mbar + 0x8000; /* Those will be handled by the */
  74. sram_size = 0x4000; /* bestcomm driver soon */
  75. /* call board suspend code, if applicable */
  76. if (mpc52xx_suspend.board_suspend_prepare)
  77. mpc52xx_suspend.board_suspend_prepare(mbar);
  78. else {
  79. printk(KERN_ALERT "%s: %i don't know how to wake up the board\n",
  80. __func__, __LINE__);
  81. goto out_unmap;
  82. }
  83. return 0;
  84. out_unmap:
  85. iounmap(mbar);
  86. return -ENOSYS;
  87. }
  88. char saved_sram[0x4000];
  89. int mpc52xx_pm_enter(suspend_state_t state)
  90. {
  91. u32 clk_enables;
  92. u32 msr, hid0;
  93. u32 intr_main_mask;
  94. void __iomem * irq_0x500 = (void __iomem *)CONFIG_KERNEL_START + 0x500;
  95. unsigned long irq_0x500_stop = (unsigned long)irq_0x500 + mpc52xx_ds_cached_size;
  96. char saved_0x500[mpc52xx_ds_cached_size];
  97. /* disable all interrupts in PIC */
  98. intr_main_mask = in_be32(&intr->main_mask);
  99. out_be32(&intr->main_mask, intr_main_mask | 0x1ffff);
  100. /* don't let DEC expire any time soon */
  101. mtspr(SPRN_DEC, 0x7fffffff);
  102. /* save SRAM */
  103. memcpy(saved_sram, sram, sram_size);
  104. /* copy low level suspend code to sram */
  105. memcpy(sram, mpc52xx_ds_sram, mpc52xx_ds_sram_size);
  106. out_8(&cdm->ccs_sleep_enable, 1);
  107. out_8(&cdm->osc_sleep_enable, 1);
  108. out_8(&cdm->ccs_qreq_test, 1);
  109. /* disable all but SDRAM and bestcomm (SRAM) clocks */
  110. clk_enables = in_be32(&cdm->clk_enables);
  111. out_be32(&cdm->clk_enables, clk_enables & 0x00088000);
  112. /* disable power management */
  113. msr = mfmsr();
  114. mtmsr(msr & ~MSR_POW);
  115. /* enable sleep mode, disable others */
  116. hid0 = mfspr(SPRN_HID0);
  117. mtspr(SPRN_HID0, (hid0 & ~(HID0_DOZE | HID0_NAP | HID0_DPM)) | HID0_SLEEP);
  118. /* save original, copy our irq handler, flush from dcache and invalidate icache */
  119. memcpy(saved_0x500, irq_0x500, mpc52xx_ds_cached_size);
  120. memcpy(irq_0x500, mpc52xx_ds_cached, mpc52xx_ds_cached_size);
  121. flush_icache_range((unsigned long)irq_0x500, irq_0x500_stop);
  122. /* call low-level sleep code */
  123. mpc52xx_deep_sleep(sram, sdram, cdm, intr);
  124. /* restore original irq handler */
  125. memcpy(irq_0x500, saved_0x500, mpc52xx_ds_cached_size);
  126. flush_icache_range((unsigned long)irq_0x500, irq_0x500_stop);
  127. /* restore old power mode */
  128. mtmsr(msr & ~MSR_POW);
  129. mtspr(SPRN_HID0, hid0);
  130. mtmsr(msr);
  131. out_be32(&cdm->clk_enables, clk_enables);
  132. out_8(&cdm->ccs_sleep_enable, 0);
  133. out_8(&cdm->osc_sleep_enable, 0);
  134. /* restore SRAM */
  135. memcpy(sram, saved_sram, sram_size);
  136. /* restart jiffies */
  137. wakeup_decrementer();
  138. /* reenable interrupts in PIC */
  139. out_be32(&intr->main_mask, intr_main_mask);
  140. return 0;
  141. }
  142. void mpc52xx_pm_finish(void)
  143. {
  144. /* call board resume code */
  145. if (mpc52xx_suspend.board_resume_finish)
  146. mpc52xx_suspend.board_resume_finish(mbar);
  147. iounmap(mbar);
  148. }
  149. static struct platform_suspend_ops mpc52xx_pm_ops = {
  150. .valid = mpc52xx_pm_valid,
  151. .prepare = mpc52xx_pm_prepare,
  152. .enter = mpc52xx_pm_enter,
  153. .finish = mpc52xx_pm_finish,
  154. };
  155. int __init mpc52xx_pm_init(void)
  156. {
  157. suspend_set_ops(&mpc52xx_pm_ops);
  158. return 0;
  159. }