mpc52xx_pm.c 4.7 KB

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