mpc52xx_pm.c 5.2 KB

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