sleep-tegra20.S 14 KB

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