pm.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * AVR32 AP Power Management
  3. *
  4. * Copyright (C) 2008 Atmel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * version 2 as published by the Free Software Foundation.
  9. */
  10. #include <linux/io.h>
  11. #include <linux/suspend.h>
  12. #include <linux/vmalloc.h>
  13. #include <asm/cacheflush.h>
  14. #include <asm/sysreg.h>
  15. #include <mach/chip.h>
  16. #include <mach/pm.h>
  17. #include <mach/sram.h>
  18. #include "sdramc.h"
  19. #define SRAM_PAGE_FLAGS (SYSREG_BIT(TLBELO_D) | SYSREG_BF(SZ, 1) \
  20. | SYSREG_BF(AP, 3) | SYSREG_BIT(G))
  21. static unsigned long pm_sram_start;
  22. static size_t pm_sram_size;
  23. static struct vm_struct *pm_sram_area;
  24. static void (*avr32_pm_enter_standby)(unsigned long sdramc_base);
  25. static void (*avr32_pm_enter_str)(unsigned long sdramc_base);
  26. /*
  27. * Must be called with interrupts disabled. Exceptions will be masked
  28. * on return (i.e. all exceptions will be "unrecoverable".)
  29. */
  30. static void *avr32_pm_map_sram(void)
  31. {
  32. unsigned long vaddr;
  33. unsigned long page_addr;
  34. u32 tlbehi;
  35. u32 mmucr;
  36. vaddr = (unsigned long)pm_sram_area->addr;
  37. page_addr = pm_sram_start & PAGE_MASK;
  38. /*
  39. * Mask exceptions and grab the first TLB entry. We won't be
  40. * needing it while sleeping.
  41. */
  42. asm volatile("ssrf %0" : : "i"(SYSREG_EM_OFFSET) : "memory");
  43. mmucr = sysreg_read(MMUCR);
  44. tlbehi = sysreg_read(TLBEHI);
  45. sysreg_write(MMUCR, SYSREG_BFINS(DRP, 0, mmucr));
  46. tlbehi = SYSREG_BF(ASID, SYSREG_BFEXT(ASID, tlbehi));
  47. tlbehi |= vaddr & PAGE_MASK;
  48. tlbehi |= SYSREG_BIT(TLBEHI_V);
  49. sysreg_write(TLBELO, page_addr | SRAM_PAGE_FLAGS);
  50. sysreg_write(TLBEHI, tlbehi);
  51. __builtin_tlbw();
  52. return (void *)(vaddr + pm_sram_start - page_addr);
  53. }
  54. /*
  55. * Must be called with interrupts disabled. Exceptions will be
  56. * unmasked on return.
  57. */
  58. static void avr32_pm_unmap_sram(void)
  59. {
  60. u32 mmucr;
  61. u32 tlbehi;
  62. u32 tlbarlo;
  63. /* Going to update TLB entry at index 0 */
  64. mmucr = sysreg_read(MMUCR);
  65. tlbehi = sysreg_read(TLBEHI);
  66. sysreg_write(MMUCR, SYSREG_BFINS(DRP, 0, mmucr));
  67. /* Clear the "valid" bit */
  68. tlbehi = SYSREG_BF(ASID, SYSREG_BFEXT(ASID, tlbehi));
  69. sysreg_write(TLBEHI, tlbehi);
  70. /* Mark it as "not accessed" */
  71. tlbarlo = sysreg_read(TLBARLO);
  72. sysreg_write(TLBARLO, tlbarlo | 0x80000000U);
  73. /* Update the TLB */
  74. __builtin_tlbw();
  75. /* Unmask exceptions */
  76. asm volatile("csrf %0" : : "i"(SYSREG_EM_OFFSET) : "memory");
  77. }
  78. static int avr32_pm_valid_state(suspend_state_t state)
  79. {
  80. switch (state) {
  81. case PM_SUSPEND_ON:
  82. case PM_SUSPEND_STANDBY:
  83. case PM_SUSPEND_MEM:
  84. return 1;
  85. default:
  86. return 0;
  87. }
  88. }
  89. static int avr32_pm_enter(suspend_state_t state)
  90. {
  91. u32 lpr_saved;
  92. u32 evba_saved;
  93. void *sram;
  94. switch (state) {
  95. case PM_SUSPEND_STANDBY:
  96. sram = avr32_pm_map_sram();
  97. /* Switch to in-sram exception handlers */
  98. evba_saved = sysreg_read(EVBA);
  99. sysreg_write(EVBA, (unsigned long)sram);
  100. /*
  101. * Save the LPR register so that we can re-enable
  102. * SDRAM Low Power mode on resume.
  103. */
  104. lpr_saved = sdramc_readl(LPR);
  105. pr_debug("%s: Entering standby...\n", __func__);
  106. avr32_pm_enter_standby(SDRAMC_BASE);
  107. sdramc_writel(LPR, lpr_saved);
  108. /* Switch back to regular exception handlers */
  109. sysreg_write(EVBA, evba_saved);
  110. avr32_pm_unmap_sram();
  111. break;
  112. case PM_SUSPEND_MEM:
  113. sram = avr32_pm_map_sram();
  114. /* Switch to in-sram exception handlers */
  115. evba_saved = sysreg_read(EVBA);
  116. sysreg_write(EVBA, (unsigned long)sram);
  117. /*
  118. * Save the LPR register so that we can re-enable
  119. * SDRAM Low Power mode on resume.
  120. */
  121. lpr_saved = sdramc_readl(LPR);
  122. pr_debug("%s: Entering suspend-to-ram...\n", __func__);
  123. avr32_pm_enter_str(SDRAMC_BASE);
  124. sdramc_writel(LPR, lpr_saved);
  125. /* Switch back to regular exception handlers */
  126. sysreg_write(EVBA, evba_saved);
  127. avr32_pm_unmap_sram();
  128. break;
  129. case PM_SUSPEND_ON:
  130. pr_debug("%s: Entering idle...\n", __func__);
  131. cpu_enter_idle();
  132. break;
  133. default:
  134. pr_debug("%s: Invalid suspend state %d\n", __func__, state);
  135. goto out;
  136. }
  137. pr_debug("%s: wakeup\n", __func__);
  138. out:
  139. return 0;
  140. }
  141. static const struct platform_suspend_ops avr32_pm_ops = {
  142. .valid = avr32_pm_valid_state,
  143. .enter = avr32_pm_enter,
  144. };
  145. static unsigned long __init avr32_pm_offset(void *symbol)
  146. {
  147. extern u8 pm_exception[];
  148. return (unsigned long)symbol - (unsigned long)pm_exception;
  149. }
  150. static int __init avr32_pm_init(void)
  151. {
  152. extern u8 pm_exception[];
  153. extern u8 pm_irq0[];
  154. extern u8 pm_standby[];
  155. extern u8 pm_suspend_to_ram[];
  156. extern u8 pm_sram_end[];
  157. void *dst;
  158. /*
  159. * To keep things simple, we depend on not needing more than a
  160. * single page.
  161. */
  162. pm_sram_size = avr32_pm_offset(pm_sram_end);
  163. if (pm_sram_size > PAGE_SIZE)
  164. goto err;
  165. pm_sram_start = sram_alloc(pm_sram_size);
  166. if (!pm_sram_start)
  167. goto err_alloc_sram;
  168. /* Grab a virtual area we can use later on. */
  169. pm_sram_area = get_vm_area(pm_sram_size, VM_IOREMAP);
  170. if (!pm_sram_area)
  171. goto err_vm_area;
  172. pm_sram_area->phys_addr = pm_sram_start;
  173. local_irq_disable();
  174. dst = avr32_pm_map_sram();
  175. memcpy(dst, pm_exception, pm_sram_size);
  176. flush_dcache_region(dst, pm_sram_size);
  177. invalidate_icache_region(dst, pm_sram_size);
  178. avr32_pm_unmap_sram();
  179. local_irq_enable();
  180. avr32_pm_enter_standby = dst + avr32_pm_offset(pm_standby);
  181. avr32_pm_enter_str = dst + avr32_pm_offset(pm_suspend_to_ram);
  182. intc_set_suspend_handler(avr32_pm_offset(pm_irq0));
  183. suspend_set_ops(&avr32_pm_ops);
  184. printk("AVR32 AP Power Management enabled\n");
  185. return 0;
  186. err_vm_area:
  187. sram_free(pm_sram_start, pm_sram_size);
  188. err_alloc_sram:
  189. err:
  190. pr_err("AVR32 Power Management initialization failed\n");
  191. return -ENOMEM;
  192. }
  193. arch_initcall(avr32_pm_init);