mpc52xx_pm.c 4.7 KB

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