sleep-tegra20.S 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /*
  2. * Copyright (c) 2010-2012, NVIDIA Corporation. All rights reserved.
  3. * Copyright (c) 2011, Google, Inc.
  4. *
  5. * Author: Colin Cross <ccross@android.com>
  6. * Gary King <gking@nvidia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <linux/linkage.h>
  21. #include <asm/assembler.h>
  22. #include <asm/proc-fns.h>
  23. #include <asm/cp15.h>
  24. #include <asm/cache.h>
  25. #include "sleep.h"
  26. #include "flowctrl.h"
  27. #define EMC_CFG 0xc
  28. #define EMC_ADR_CFG 0x10
  29. #define EMC_REFRESH 0x70
  30. #define EMC_NOP 0xdc
  31. #define EMC_SELF_REF 0xe0
  32. #define EMC_REQ_CTRL 0x2b0
  33. #define EMC_EMC_STATUS 0x2b4
  34. #define CLK_RESET_CCLK_BURST 0x20
  35. #define CLK_RESET_CCLK_DIVIDER 0x24
  36. #define CLK_RESET_SCLK_BURST 0x28
  37. #define CLK_RESET_SCLK_DIVIDER 0x2c
  38. #define CLK_RESET_PLLC_BASE 0x80
  39. #define CLK_RESET_PLLM_BASE 0x90
  40. #define CLK_RESET_PLLP_BASE 0xa0
  41. #define APB_MISC_XM2CFGCPADCTRL 0x8c8
  42. #define APB_MISC_XM2CFGDPADCTRL 0x8cc
  43. #define APB_MISC_XM2CLKCFGPADCTRL 0x8d0
  44. #define APB_MISC_XM2COMPPADCTRL 0x8d4
  45. #define APB_MISC_XM2VTTGENPADCTRL 0x8d8
  46. #define APB_MISC_XM2CFGCPADCTRL2 0x8e4
  47. #define APB_MISC_XM2CFGDPADCTRL2 0x8e8
  48. .macro pll_enable, rd, r_car_base, pll_base
  49. ldr \rd, [\r_car_base, #\pll_base]
  50. tst \rd, #(1 << 30)
  51. orreq \rd, \rd, #(1 << 30)
  52. streq \rd, [\r_car_base, #\pll_base]
  53. .endm
  54. .macro emc_device_mask, rd, base
  55. ldr \rd, [\base, #EMC_ADR_CFG]
  56. tst \rd, #(0x3 << 24)
  57. moveq \rd, #(0x1 << 8) @ just 1 device
  58. movne \rd, #(0x3 << 8) @ 2 devices
  59. .endm
  60. #if defined(CONFIG_HOTPLUG_CPU) || defined(CONFIG_PM_SLEEP)
  61. /*
  62. * tegra20_hotplug_shutdown(void)
  63. *
  64. * puts the current cpu in reset
  65. * should never return
  66. */
  67. ENTRY(tegra20_hotplug_shutdown)
  68. /* Put this CPU down */
  69. cpu_id r0
  70. bl tegra20_cpu_shutdown
  71. mov pc, lr @ should never get here
  72. ENDPROC(tegra20_hotplug_shutdown)
  73. /*
  74. * tegra20_cpu_shutdown(int cpu)
  75. *
  76. * r0 is cpu to reset
  77. *
  78. * puts the specified CPU in wait-for-event mode on the flow controller
  79. * and puts the CPU in reset
  80. * can be called on the current cpu or another cpu
  81. * if called on the current cpu, does not return
  82. * MUST NOT BE CALLED FOR CPU 0.
  83. *
  84. * corrupts r0-r3, r12
  85. */
  86. ENTRY(tegra20_cpu_shutdown)
  87. cmp r0, #0
  88. moveq pc, lr @ must not be called for CPU 0
  89. mov32 r1, TEGRA_PMC_VIRT + PMC_SCRATCH41
  90. mov r12, #CPU_RESETTABLE
  91. str r12, [r1]
  92. cpu_to_halt_reg r1, r0
  93. ldr r3, =TEGRA_FLOW_CTRL_VIRT
  94. mov r2, #FLOW_CTRL_WAITEVENT | FLOW_CTRL_JTAG_RESUME
  95. str r2, [r3, r1] @ put flow controller in wait event mode
  96. ldr r2, [r3, r1]
  97. isb
  98. dsb
  99. movw r1, 0x1011
  100. mov r1, r1, lsl r0
  101. ldr r3, =TEGRA_CLK_RESET_VIRT
  102. str r1, [r3, #0x340] @ put slave CPU in reset
  103. isb
  104. dsb
  105. cpu_id r3
  106. cmp r3, r0
  107. beq .
  108. mov pc, lr
  109. ENDPROC(tegra20_cpu_shutdown)
  110. #endif
  111. #ifdef CONFIG_PM_SLEEP
  112. /*
  113. * tegra_pen_lock
  114. *
  115. * spinlock implementation with no atomic test-and-set and no coherence
  116. * using Peterson's algorithm on strongly-ordered registers
  117. * used to synchronize a cpu waking up from wfi with entering lp2 on idle
  118. *
  119. * The reference link of Peterson's algorithm:
  120. * http://en.wikipedia.org/wiki/Peterson's_algorithm
  121. *
  122. * SCRATCH37 = r1 = !turn (inverted from Peterson's algorithm)
  123. * on cpu 0:
  124. * r2 = flag[0] (in SCRATCH38)
  125. * r3 = flag[1] (in SCRATCH39)
  126. * on cpu1:
  127. * r2 = flag[1] (in SCRATCH39)
  128. * r3 = flag[0] (in SCRATCH38)
  129. *
  130. * must be called with MMU on
  131. * corrupts r0-r3, r12
  132. */
  133. ENTRY(tegra_pen_lock)
  134. mov32 r3, TEGRA_PMC_VIRT
  135. cpu_id r0
  136. add r1, r3, #PMC_SCRATCH37
  137. cmp r0, #0
  138. addeq r2, r3, #PMC_SCRATCH38
  139. addeq r3, r3, #PMC_SCRATCH39
  140. addne r2, r3, #PMC_SCRATCH39
  141. addne r3, r3, #PMC_SCRATCH38
  142. mov r12, #1
  143. str r12, [r2] @ flag[cpu] = 1
  144. dsb
  145. str r12, [r1] @ !turn = cpu
  146. 1: dsb
  147. ldr r12, [r3]
  148. cmp r12, #1 @ flag[!cpu] == 1?
  149. ldreq r12, [r1]
  150. cmpeq r12, r0 @ !turn == cpu?
  151. beq 1b @ while !turn == cpu && flag[!cpu] == 1
  152. mov pc, lr @ locked
  153. ENDPROC(tegra_pen_lock)
  154. ENTRY(tegra_pen_unlock)
  155. dsb
  156. mov32 r3, TEGRA_PMC_VIRT
  157. cpu_id r0
  158. cmp r0, #0
  159. addeq r2, r3, #PMC_SCRATCH38
  160. addne r2, r3, #PMC_SCRATCH39
  161. mov r12, #0
  162. str r12, [r2]
  163. mov pc, lr
  164. ENDPROC(tegra_pen_unlock)
  165. /*
  166. * tegra20_cpu_clear_resettable(void)
  167. *
  168. * Called to clear the "resettable soon" flag in PMC_SCRATCH41 when
  169. * it is expected that the secondary CPU will be idle soon.
  170. */
  171. ENTRY(tegra20_cpu_clear_resettable)
  172. mov32 r1, TEGRA_PMC_VIRT + PMC_SCRATCH41
  173. mov r12, #CPU_NOT_RESETTABLE
  174. str r12, [r1]
  175. mov pc, lr
  176. ENDPROC(tegra20_cpu_clear_resettable)
  177. /*
  178. * tegra20_cpu_set_resettable_soon(void)
  179. *
  180. * Called to set the "resettable soon" flag in PMC_SCRATCH41 when
  181. * it is expected that the secondary CPU will be idle soon.
  182. */
  183. ENTRY(tegra20_cpu_set_resettable_soon)
  184. mov32 r1, TEGRA_PMC_VIRT + PMC_SCRATCH41
  185. mov r12, #CPU_RESETTABLE_SOON
  186. str r12, [r1]
  187. mov pc, lr
  188. ENDPROC(tegra20_cpu_set_resettable_soon)
  189. /*
  190. * tegra20_cpu_is_resettable_soon(void)
  191. *
  192. * Returns true if the "resettable soon" flag in PMC_SCRATCH41 has been
  193. * set because it is expected that the secondary CPU will be idle soon.
  194. */
  195. ENTRY(tegra20_cpu_is_resettable_soon)
  196. mov32 r1, TEGRA_PMC_VIRT + PMC_SCRATCH41
  197. ldr r12, [r1]
  198. cmp r12, #CPU_RESETTABLE_SOON
  199. moveq r0, #1
  200. movne r0, #0
  201. mov pc, lr
  202. ENDPROC(tegra20_cpu_is_resettable_soon)
  203. /*
  204. * tegra20_sleep_core_finish(unsigned long v2p)
  205. *
  206. * Enters suspend in LP0 or LP1 by turning off the mmu and jumping to
  207. * tegra20_tear_down_core in IRAM
  208. */
  209. ENTRY(tegra20_sleep_core_finish)
  210. /* Flush, disable the L1 data cache and exit SMP */
  211. bl tegra_disable_clean_inv_dcache
  212. mov32 r3, tegra_shut_off_mmu
  213. add r3, r3, r0
  214. mov32 r0, tegra20_tear_down_core
  215. mov32 r1, tegra20_iram_start
  216. sub r0, r0, r1
  217. mov32 r1, TEGRA_IRAM_CODE_AREA
  218. add r0, r0, r1
  219. mov pc, r3
  220. ENDPROC(tegra20_sleep_core_finish)
  221. /*
  222. * tegra20_sleep_cpu_secondary_finish(unsigned long v2p)
  223. *
  224. * Enters WFI on secondary CPU by exiting coherency.
  225. */
  226. ENTRY(tegra20_sleep_cpu_secondary_finish)
  227. stmfd sp!, {r4-r11, lr}
  228. mrc p15, 0, r11, c1, c0, 1 @ save actlr before exiting coherency
  229. /* Flush and disable the L1 data cache */
  230. mov r0, #TEGRA_FLUSH_CACHE_LOUIS
  231. bl tegra_disable_clean_inv_dcache
  232. mov32 r0, TEGRA_PMC_VIRT + PMC_SCRATCH41
  233. mov r3, #CPU_RESETTABLE
  234. str r3, [r0]
  235. bl tegra_cpu_do_idle
  236. /*
  237. * cpu may be reset while in wfi, which will return through
  238. * tegra_resume to cpu_resume
  239. * or interrupt may wake wfi, which will return here
  240. * cpu state is unchanged - MMU is on, cache is on, coherency
  241. * is off, and the data cache is off
  242. *
  243. * r11 contains the original actlr
  244. */
  245. bl tegra_pen_lock
  246. mov32 r3, TEGRA_PMC_VIRT
  247. add r0, r3, #PMC_SCRATCH41
  248. mov r3, #CPU_NOT_RESETTABLE
  249. str r3, [r0]
  250. bl tegra_pen_unlock
  251. /* Re-enable the data cache */
  252. mrc p15, 0, r10, c1, c0, 0
  253. orr r10, r10, #CR_C
  254. mcr p15, 0, r10, c1, c0, 0
  255. isb
  256. mcr p15, 0, r11, c1, c0, 1 @ reenable coherency
  257. /* Invalidate the TLBs & BTAC */
  258. mov r1, #0
  259. mcr p15, 0, r1, c8, c3, 0 @ invalidate shared TLBs
  260. mcr p15, 0, r1, c7, c1, 6 @ invalidate shared BTAC
  261. dsb
  262. isb
  263. /* the cpu was running with coherency disabled,
  264. * caches may be out of date */
  265. bl v7_flush_kern_cache_louis
  266. ldmfd sp!, {r4 - r11, pc}
  267. ENDPROC(tegra20_sleep_cpu_secondary_finish)
  268. /*
  269. * tegra20_tear_down_cpu
  270. *
  271. * Switches the CPU cluster to PLL-P and enters sleep.
  272. */
  273. ENTRY(tegra20_tear_down_cpu)
  274. bl tegra_switch_cpu_to_pllp
  275. b tegra20_enter_sleep
  276. ENDPROC(tegra20_tear_down_cpu)
  277. /* START OF ROUTINES COPIED TO IRAM */
  278. .align L1_CACHE_SHIFT
  279. .globl tegra20_iram_start
  280. tegra20_iram_start:
  281. /*
  282. * tegra20_lp1_reset
  283. *
  284. * reset vector for LP1 restore; copied into IRAM during suspend.
  285. * Brings the system back up to a safe staring point (SDRAM out of
  286. * self-refresh, PLLC, PLLM and PLLP reenabled, CPU running on PLLP,
  287. * system clock running on the same PLL that it suspended at), and
  288. * jumps to tegra_resume to restore virtual addressing and PLLX.
  289. * The physical address of tegra_resume expected to be stored in
  290. * PMC_SCRATCH41.
  291. *
  292. * NOTE: THIS *MUST* BE RELOCATED TO TEGRA_IRAM_CODE_AREA.
  293. */
  294. ENTRY(tegra20_lp1_reset)
  295. /*
  296. * The CPU and system bus are running at 32KHz and executing from
  297. * IRAM when this code is executed; immediately switch to CLKM and
  298. * enable PLLM, PLLP, PLLC.
  299. */
  300. mov32 r0, TEGRA_CLK_RESET_BASE
  301. mov r1, #(1 << 28)
  302. str r1, [r0, #CLK_RESET_SCLK_BURST]
  303. str r1, [r0, #CLK_RESET_CCLK_BURST]
  304. mov r1, #0
  305. str r1, [r0, #CLK_RESET_CCLK_DIVIDER]
  306. str r1, [r0, #CLK_RESET_SCLK_DIVIDER]
  307. pll_enable r1, r0, CLK_RESET_PLLM_BASE
  308. pll_enable r1, r0, CLK_RESET_PLLP_BASE
  309. pll_enable r1, r0, CLK_RESET_PLLC_BASE
  310. adr r2, tegra20_sdram_pad_address
  311. adr r4, tegra20_sdram_pad_save
  312. mov r5, #0
  313. ldr r6, tegra20_sdram_pad_size
  314. padload:
  315. ldr r7, [r2, r5] @ r7 is the addr in the pad_address
  316. ldr r1, [r4, r5]
  317. str r1, [r7] @ restore the value in pad_save
  318. add r5, r5, #4
  319. cmp r6, r5
  320. bne padload
  321. padload_done:
  322. /* 255uS delay for PLL stabilization */
  323. mov32 r7, TEGRA_TMRUS_BASE
  324. ldr r1, [r7]
  325. add r1, r1, #0xff
  326. wait_until r1, r7, r9
  327. adr r4, tegra20_sclk_save
  328. ldr r4, [r4]
  329. str r4, [r0, #CLK_RESET_SCLK_BURST]
  330. mov32 r4, ((1 << 28) | (4)) @ burst policy is PLLP
  331. str r4, [r0, #CLK_RESET_CCLK_BURST]
  332. mov32 r0, TEGRA_EMC_BASE
  333. ldr r1, [r0, #EMC_CFG]
  334. bic r1, r1, #(1 << 31) @ disable DRAM_CLK_STOP
  335. str r1, [r0, #EMC_CFG]
  336. mov r1, #0
  337. str r1, [r0, #EMC_SELF_REF] @ take DRAM out of self refresh
  338. mov r1, #1
  339. str r1, [r0, #EMC_NOP]
  340. str r1, [r0, #EMC_NOP]
  341. str r1, [r0, #EMC_REFRESH]
  342. emc_device_mask r1, r0
  343. exit_selfrefresh_loop:
  344. ldr r2, [r0, #EMC_EMC_STATUS]
  345. ands r2, r2, r1
  346. bne exit_selfrefresh_loop
  347. mov r1, #0 @ unstall all transactions
  348. str r1, [r0, #EMC_REQ_CTRL]
  349. mov32 r0, TEGRA_PMC_BASE
  350. ldr r0, [r0, #PMC_SCRATCH41]
  351. mov pc, r0 @ jump to tegra_resume
  352. ENDPROC(tegra20_lp1_reset)
  353. /*
  354. * tegra20_tear_down_core
  355. *
  356. * copied into and executed from IRAM
  357. * puts memory in self-refresh for LP0 and LP1
  358. */
  359. tegra20_tear_down_core:
  360. bl tegra20_sdram_self_refresh
  361. bl tegra20_switch_cpu_to_clk32k
  362. b tegra20_enter_sleep
  363. /*
  364. * tegra20_switch_cpu_to_clk32k
  365. *
  366. * In LP0 and LP1 all PLLs will be turned off. Switch the CPU and system clock
  367. * to the 32KHz clock.
  368. */
  369. tegra20_switch_cpu_to_clk32k:
  370. /*
  371. * start by switching to CLKM to safely disable PLLs, then switch to
  372. * CLKS.
  373. */
  374. mov r0, #(1 << 28)
  375. str r0, [r5, #CLK_RESET_SCLK_BURST]
  376. str r0, [r5, #CLK_RESET_CCLK_BURST]
  377. mov r0, #0
  378. str r0, [r5, #CLK_RESET_CCLK_DIVIDER]
  379. str r0, [r5, #CLK_RESET_SCLK_DIVIDER]
  380. /* 2uS delay delay between changing SCLK and disabling PLLs */
  381. mov32 r7, TEGRA_TMRUS_BASE
  382. ldr r1, [r7]
  383. add r1, r1, #2
  384. wait_until r1, r7, r9
  385. /* disable PLLM, PLLP and PLLC */
  386. ldr r0, [r5, #CLK_RESET_PLLM_BASE]
  387. bic r0, r0, #(1 << 30)
  388. str r0, [r5, #CLK_RESET_PLLM_BASE]
  389. ldr r0, [r5, #CLK_RESET_PLLP_BASE]
  390. bic r0, r0, #(1 << 30)
  391. str r0, [r5, #CLK_RESET_PLLP_BASE]
  392. ldr r0, [r5, #CLK_RESET_PLLC_BASE]
  393. bic r0, r0, #(1 << 30)
  394. str r0, [r5, #CLK_RESET_PLLC_BASE]
  395. /* switch to CLKS */
  396. mov r0, #0 /* brust policy = 32KHz */
  397. str r0, [r5, #CLK_RESET_SCLK_BURST]
  398. mov pc, lr
  399. /*
  400. * tegra20_enter_sleep
  401. *
  402. * uses flow controller to enter sleep state
  403. * executes from IRAM with SDRAM in selfrefresh when target state is LP0 or LP1
  404. * executes from SDRAM with target state is LP2
  405. */
  406. tegra20_enter_sleep:
  407. mov32 r6, TEGRA_FLOW_CTRL_BASE
  408. mov r0, #FLOW_CTRL_WAIT_FOR_INTERRUPT
  409. orr r0, r0, #FLOW_CTRL_HALT_CPU_IRQ | FLOW_CTRL_HALT_CPU_FIQ
  410. cpu_id r1
  411. cpu_to_halt_reg r1, r1
  412. str r0, [r6, r1]
  413. dsb
  414. ldr r0, [r6, r1] /* memory barrier */
  415. halted:
  416. dsb
  417. wfe /* CPU should be power gated here */
  418. isb
  419. b halted
  420. /*
  421. * tegra20_sdram_self_refresh
  422. *
  423. * called with MMU off and caches disabled
  424. * puts sdram in self refresh
  425. * must be executed from IRAM
  426. */
  427. tegra20_sdram_self_refresh:
  428. mov32 r1, TEGRA_EMC_BASE @ r1 reserved for emc base addr
  429. mov r2, #3
  430. str r2, [r1, #EMC_REQ_CTRL] @ stall incoming DRAM requests
  431. emcidle:
  432. ldr r2, [r1, #EMC_EMC_STATUS]
  433. tst r2, #4
  434. beq emcidle
  435. mov r2, #1
  436. str r2, [r1, #EMC_SELF_REF]
  437. emc_device_mask r2, r1
  438. emcself:
  439. ldr r3, [r1, #EMC_EMC_STATUS]
  440. and r3, r3, r2
  441. cmp r3, r2
  442. bne emcself @ loop until DDR in self-refresh
  443. adr r2, tegra20_sdram_pad_address
  444. adr r3, tegra20_sdram_pad_safe
  445. adr r4, tegra20_sdram_pad_save
  446. mov r5, #0
  447. ldr r6, tegra20_sdram_pad_size
  448. padsave:
  449. ldr r0, [r2, r5] @ r0 is the addr in the pad_address
  450. ldr r1, [r0]
  451. str r1, [r4, r5] @ save the content of the addr
  452. ldr r1, [r3, r5]
  453. str r1, [r0] @ set the save val to the addr
  454. add r5, r5, #4
  455. cmp r6, r5
  456. bne padsave
  457. padsave_done:
  458. mov32 r5, TEGRA_CLK_RESET_BASE
  459. ldr r0, [r5, #CLK_RESET_SCLK_BURST]
  460. adr r2, tegra20_sclk_save
  461. str r0, [r2]
  462. dsb
  463. mov pc, lr
  464. tegra20_sdram_pad_address:
  465. .word TEGRA_APB_MISC_BASE + APB_MISC_XM2CFGCPADCTRL
  466. .word TEGRA_APB_MISC_BASE + APB_MISC_XM2CFGDPADCTRL
  467. .word TEGRA_APB_MISC_BASE + APB_MISC_XM2CLKCFGPADCTRL
  468. .word TEGRA_APB_MISC_BASE + APB_MISC_XM2COMPPADCTRL
  469. .word TEGRA_APB_MISC_BASE + APB_MISC_XM2VTTGENPADCTRL
  470. .word TEGRA_APB_MISC_BASE + APB_MISC_XM2CFGCPADCTRL2
  471. .word TEGRA_APB_MISC_BASE + APB_MISC_XM2CFGDPADCTRL2
  472. tegra20_sdram_pad_size:
  473. .word tegra20_sdram_pad_size - tegra20_sdram_pad_address
  474. tegra20_sdram_pad_safe:
  475. .word 0x8
  476. .word 0x8
  477. .word 0x0
  478. .word 0x8
  479. .word 0x5500
  480. .word 0x08080040
  481. .word 0x0
  482. tegra20_sclk_save:
  483. .word 0x0
  484. tegra20_sdram_pad_save:
  485. .rept (tegra20_sdram_pad_size - tegra20_sdram_pad_address) / 4
  486. .long 0
  487. .endr
  488. .ltorg
  489. /* dummy symbol for end of IRAM */
  490. .align L1_CACHE_SHIFT
  491. .globl tegra20_iram_end
  492. tegra20_iram_end:
  493. b .
  494. #endif