sleep.S 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * This file contains sleep low-level functions for PowerBook G3.
  3. * Copyright (C) 1999 Benjamin Herrenschmidt (benh@kernel.crashing.org)
  4. * and Paul Mackerras (paulus@samba.org).
  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. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. */
  12. #include <asm/processor.h>
  13. #include <asm/page.h>
  14. #include <asm/ppc_asm.h>
  15. #include <asm/cputable.h>
  16. #include <asm/cache.h>
  17. #include <asm/thread_info.h>
  18. #include <asm/asm-offsets.h>
  19. #define MAGIC 0x4c617273 /* 'Lars' */
  20. /*
  21. * Structure for storing CPU registers on the stack.
  22. */
  23. #define SL_SP 0
  24. #define SL_PC 4
  25. #define SL_MSR 8
  26. #define SL_SDR1 0xc
  27. #define SL_SPRG0 0x10 /* 4 sprg's */
  28. #define SL_DBAT0 0x20
  29. #define SL_IBAT0 0x28
  30. #define SL_DBAT1 0x30
  31. #define SL_IBAT1 0x38
  32. #define SL_DBAT2 0x40
  33. #define SL_IBAT2 0x48
  34. #define SL_DBAT3 0x50
  35. #define SL_IBAT3 0x58
  36. #define SL_TB 0x60
  37. #define SL_R2 0x68
  38. #define SL_CR 0x6c
  39. #define SL_R12 0x70 /* r12 to r31 */
  40. #define SL_SIZE (SL_R12 + 80)
  41. .section .text
  42. .align 5
  43. #if defined(CONFIG_PM) || defined(CONFIG_CPU_FREQ_PMAC) || \
  44. (defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_PPC32))
  45. /* This gets called by via-pmu.c late during the sleep process.
  46. * The PMU was already send the sleep command and will shut us down
  47. * soon. We need to save all that is needed and setup the wakeup
  48. * vector that will be called by the ROM on wakeup
  49. */
  50. _GLOBAL(low_sleep_handler)
  51. #ifndef CONFIG_6xx
  52. blr
  53. #else
  54. mflr r0
  55. stw r0,4(r1)
  56. stwu r1,-SL_SIZE(r1)
  57. mfcr r0
  58. stw r0,SL_CR(r1)
  59. stw r2,SL_R2(r1)
  60. stmw r12,SL_R12(r1)
  61. /* Save MSR & SDR1 */
  62. mfmsr r4
  63. stw r4,SL_MSR(r1)
  64. mfsdr1 r4
  65. stw r4,SL_SDR1(r1)
  66. /* Get a stable timebase and save it */
  67. 1: mftbu r4
  68. stw r4,SL_TB(r1)
  69. mftb r5
  70. stw r5,SL_TB+4(r1)
  71. mftbu r3
  72. cmpw r3,r4
  73. bne 1b
  74. /* Save SPRGs */
  75. mfsprg r4,0
  76. stw r4,SL_SPRG0(r1)
  77. mfsprg r4,1
  78. stw r4,SL_SPRG0+4(r1)
  79. mfsprg r4,2
  80. stw r4,SL_SPRG0+8(r1)
  81. mfsprg r4,3
  82. stw r4,SL_SPRG0+12(r1)
  83. /* Save BATs */
  84. mfdbatu r4,0
  85. stw r4,SL_DBAT0(r1)
  86. mfdbatl r4,0
  87. stw r4,SL_DBAT0+4(r1)
  88. mfdbatu r4,1
  89. stw r4,SL_DBAT1(r1)
  90. mfdbatl r4,1
  91. stw r4,SL_DBAT1+4(r1)
  92. mfdbatu r4,2
  93. stw r4,SL_DBAT2(r1)
  94. mfdbatl r4,2
  95. stw r4,SL_DBAT2+4(r1)
  96. mfdbatu r4,3
  97. stw r4,SL_DBAT3(r1)
  98. mfdbatl r4,3
  99. stw r4,SL_DBAT3+4(r1)
  100. mfibatu r4,0
  101. stw r4,SL_IBAT0(r1)
  102. mfibatl r4,0
  103. stw r4,SL_IBAT0+4(r1)
  104. mfibatu r4,1
  105. stw r4,SL_IBAT1(r1)
  106. mfibatl r4,1
  107. stw r4,SL_IBAT1+4(r1)
  108. mfibatu r4,2
  109. stw r4,SL_IBAT2(r1)
  110. mfibatl r4,2
  111. stw r4,SL_IBAT2+4(r1)
  112. mfibatu r4,3
  113. stw r4,SL_IBAT3(r1)
  114. mfibatl r4,3
  115. stw r4,SL_IBAT3+4(r1)
  116. /* Backup various CPU config stuffs */
  117. bl __save_cpu_setup
  118. /* The ROM can wake us up via 2 different vectors:
  119. * - On wallstreet & lombard, we must write a magic
  120. * value 'Lars' at address 4 and a pointer to a
  121. * memory location containing the PC to resume from
  122. * at address 0.
  123. * - On Core99, we must store the wakeup vector at
  124. * address 0x80 and eventually it's parameters
  125. * at address 0x84. I've have some trouble with those
  126. * parameters however and I no longer use them.
  127. */
  128. lis r5,grackle_wake_up@ha
  129. addi r5,r5,grackle_wake_up@l
  130. tophys(r5,r5)
  131. stw r5,SL_PC(r1)
  132. lis r4,KERNELBASE@h
  133. tophys(r5,r1)
  134. addi r5,r5,SL_PC
  135. lis r6,MAGIC@ha
  136. addi r6,r6,MAGIC@l
  137. stw r5,0(r4)
  138. stw r6,4(r4)
  139. /* Setup stuffs at 0x80-0x84 for Core99 */
  140. lis r3,core99_wake_up@ha
  141. addi r3,r3,core99_wake_up@l
  142. tophys(r3,r3)
  143. stw r3,0x80(r4)
  144. stw r5,0x84(r4)
  145. /* Store a pointer to our backup storage into
  146. * a kernel global
  147. */
  148. lis r3,sleep_storage@ha
  149. addi r3,r3,sleep_storage@l
  150. stw r5,0(r3)
  151. .globl low_cpu_die
  152. low_cpu_die:
  153. /* Flush & disable all caches */
  154. bl flush_disable_caches
  155. /* Turn off data relocation. */
  156. mfmsr r3 /* Save MSR in r7 */
  157. rlwinm r3,r3,0,28,26 /* Turn off DR bit */
  158. sync
  159. mtmsr r3
  160. isync
  161. BEGIN_FTR_SECTION
  162. /* Flush any pending L2 data prefetches to work around HW bug */
  163. sync
  164. lis r3,0xfff0
  165. lwz r0,0(r3) /* perform cache-inhibited load to ROM */
  166. sync /* (caches are disabled at this point) */
  167. END_FTR_SECTION_IFSET(CPU_FTR_SPEC7450)
  168. /*
  169. * Set the HID0 and MSR for sleep.
  170. */
  171. mfspr r2,SPRN_HID0
  172. rlwinm r2,r2,0,10,7 /* clear doze, nap */
  173. oris r2,r2,HID0_SLEEP@h
  174. sync
  175. isync
  176. mtspr SPRN_HID0,r2
  177. sync
  178. /* This loop puts us back to sleep in case we have a spurrious
  179. * wakeup so that the host bridge properly stays asleep. The
  180. * CPU will be turned off, either after a known time (about 1
  181. * second) on wallstreet & lombard, or as soon as the CPU enters
  182. * SLEEP mode on core99
  183. */
  184. mfmsr r2
  185. oris r2,r2,MSR_POW@h
  186. 1: sync
  187. mtmsr r2
  188. isync
  189. b 1b
  190. /*
  191. * Here is the resume code.
  192. */
  193. /*
  194. * Core99 machines resume here
  195. * r4 has the physical address of SL_PC(sp) (unused)
  196. */
  197. _GLOBAL(core99_wake_up)
  198. /* Make sure HID0 no longer contains any sleep bit and that data cache
  199. * is disabled
  200. */
  201. mfspr r3,SPRN_HID0
  202. rlwinm r3,r3,0,11,7 /* clear SLEEP, NAP, DOZE bits */
  203. rlwinm 3,r3,0,18,15 /* clear DCE, ICE */
  204. mtspr SPRN_HID0,r3
  205. sync
  206. isync
  207. /* sanitize MSR */
  208. mfmsr r3
  209. ori r3,r3,MSR_EE|MSR_IP
  210. xori r3,r3,MSR_EE|MSR_IP
  211. sync
  212. isync
  213. mtmsr r3
  214. sync
  215. isync
  216. /* Recover sleep storage */
  217. lis r3,sleep_storage@ha
  218. addi r3,r3,sleep_storage@l
  219. tophys(r3,r3)
  220. lwz r1,0(r3)
  221. /* Pass thru to older resume code ... */
  222. /*
  223. * Here is the resume code for older machines.
  224. * r1 has the physical address of SL_PC(sp).
  225. */
  226. grackle_wake_up:
  227. /* Restore the kernel's segment registers before
  228. * we do any r1 memory access as we are not sure they
  229. * are in a sane state above the first 256Mb region
  230. */
  231. li r0,16 /* load up segment register values */
  232. mtctr r0 /* for context 0 */
  233. lis r3,0x2000 /* Ku = 1, VSID = 0 */
  234. li r4,0
  235. 3: mtsrin r3,r4
  236. addi r3,r3,0x111 /* increment VSID */
  237. addis r4,r4,0x1000 /* address of next segment */
  238. bdnz 3b
  239. sync
  240. isync
  241. subi r1,r1,SL_PC
  242. /* Restore various CPU config stuffs */
  243. bl __restore_cpu_setup
  244. /* Make sure all FPRs have been initialized */
  245. bl reloc_offset
  246. bl __init_fpu_registers
  247. /* Invalidate & enable L1 cache, we don't care about
  248. * whatever the ROM may have tried to write to memory
  249. */
  250. bl __inval_enable_L1
  251. /* Restore the BATs, and SDR1. Then we can turn on the MMU. */
  252. lwz r4,SL_SDR1(r1)
  253. mtsdr1 r4
  254. lwz r4,SL_SPRG0(r1)
  255. mtsprg 0,r4
  256. lwz r4,SL_SPRG0+4(r1)
  257. mtsprg 1,r4
  258. lwz r4,SL_SPRG0+8(r1)
  259. mtsprg 2,r4
  260. lwz r4,SL_SPRG0+12(r1)
  261. mtsprg 3,r4
  262. lwz r4,SL_DBAT0(r1)
  263. mtdbatu 0,r4
  264. lwz r4,SL_DBAT0+4(r1)
  265. mtdbatl 0,r4
  266. lwz r4,SL_DBAT1(r1)
  267. mtdbatu 1,r4
  268. lwz r4,SL_DBAT1+4(r1)
  269. mtdbatl 1,r4
  270. lwz r4,SL_DBAT2(r1)
  271. mtdbatu 2,r4
  272. lwz r4,SL_DBAT2+4(r1)
  273. mtdbatl 2,r4
  274. lwz r4,SL_DBAT3(r1)
  275. mtdbatu 3,r4
  276. lwz r4,SL_DBAT3+4(r1)
  277. mtdbatl 3,r4
  278. lwz r4,SL_IBAT0(r1)
  279. mtibatu 0,r4
  280. lwz r4,SL_IBAT0+4(r1)
  281. mtibatl 0,r4
  282. lwz r4,SL_IBAT1(r1)
  283. mtibatu 1,r4
  284. lwz r4,SL_IBAT1+4(r1)
  285. mtibatl 1,r4
  286. lwz r4,SL_IBAT2(r1)
  287. mtibatu 2,r4
  288. lwz r4,SL_IBAT2+4(r1)
  289. mtibatl 2,r4
  290. lwz r4,SL_IBAT3(r1)
  291. mtibatu 3,r4
  292. lwz r4,SL_IBAT3+4(r1)
  293. mtibatl 3,r4
  294. BEGIN_FTR_SECTION
  295. li r4,0
  296. mtspr SPRN_DBAT4U,r4
  297. mtspr SPRN_DBAT4L,r4
  298. mtspr SPRN_DBAT5U,r4
  299. mtspr SPRN_DBAT5L,r4
  300. mtspr SPRN_DBAT6U,r4
  301. mtspr SPRN_DBAT6L,r4
  302. mtspr SPRN_DBAT7U,r4
  303. mtspr SPRN_DBAT7L,r4
  304. mtspr SPRN_IBAT4U,r4
  305. mtspr SPRN_IBAT4L,r4
  306. mtspr SPRN_IBAT5U,r4
  307. mtspr SPRN_IBAT5L,r4
  308. mtspr SPRN_IBAT6U,r4
  309. mtspr SPRN_IBAT6L,r4
  310. mtspr SPRN_IBAT7U,r4
  311. mtspr SPRN_IBAT7L,r4
  312. END_FTR_SECTION_IFSET(CPU_FTR_HAS_HIGH_BATS)
  313. /* Flush all TLBs */
  314. lis r4,0x1000
  315. 1: addic. r4,r4,-0x1000
  316. tlbie r4
  317. blt 1b
  318. sync
  319. /* restore the MSR and turn on the MMU */
  320. lwz r3,SL_MSR(r1)
  321. bl turn_on_mmu
  322. /* get back the stack pointer */
  323. tovirt(r1,r1)
  324. /* Restore TB */
  325. li r3,0
  326. mttbl r3
  327. lwz r3,SL_TB(r1)
  328. lwz r4,SL_TB+4(r1)
  329. mttbu r3
  330. mttbl r4
  331. /* Restore the callee-saved registers and return */
  332. lwz r0,SL_CR(r1)
  333. mtcr r0
  334. lwz r2,SL_R2(r1)
  335. lmw r12,SL_R12(r1)
  336. addi r1,r1,SL_SIZE
  337. lwz r0,4(r1)
  338. mtlr r0
  339. blr
  340. turn_on_mmu:
  341. mflr r4
  342. tovirt(r4,r4)
  343. mtsrr0 r4
  344. mtsrr1 r3
  345. sync
  346. isync
  347. rfi
  348. #endif /* defined(CONFIG_PM) || defined(CONFIG_CPU_FREQ) */
  349. .section .data
  350. .balign L1_CACHE_BYTES
  351. sleep_storage:
  352. .long 0
  353. .balign L1_CACHE_BYTES, 0
  354. #endif /* CONFIG_6xx */
  355. .section .text