blob: 1074364e77eecdfc3b3306e5d54926e72c23779f [file] [log] [blame]
Joseph Lo453689e2012-08-16 17:31:52 +08001/*
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
21#include <linux/linkage.h>
22
23#include <asm/assembler.h>
Joseph Lo5c1350b2013-01-15 22:10:38 +000024#include <asm/proc-fns.h>
25#include <asm/cp15.h>
Joseph Lo453689e2012-08-16 17:31:52 +080026
Joseph Lo453689e2012-08-16 17:31:52 +080027#include "sleep.h"
28#include "flowctrl.h"
29
30#if defined(CONFIG_HOTPLUG_CPU) || defined(CONFIG_PM_SLEEP)
31/*
32 * tegra20_hotplug_shutdown(void)
33 *
34 * puts the current cpu in reset
35 * should never return
36 */
37ENTRY(tegra20_hotplug_shutdown)
Joseph Lo453689e2012-08-16 17:31:52 +080038 /* Put this CPU down */
39 cpu_id r0
40 bl tegra20_cpu_shutdown
41 mov pc, lr @ should never get here
42ENDPROC(tegra20_hotplug_shutdown)
43
44/*
45 * tegra20_cpu_shutdown(int cpu)
46 *
47 * r0 is cpu to reset
48 *
49 * puts the specified CPU in wait-for-event mode on the flow controller
50 * and puts the CPU in reset
51 * can be called on the current cpu or another cpu
52 * if called on the current cpu, does not return
53 * MUST NOT BE CALLED FOR CPU 0.
54 *
55 * corrupts r0-r3, r12
56 */
57ENTRY(tegra20_cpu_shutdown)
58 cmp r0, #0
59 moveq pc, lr @ must not be called for CPU 0
60
61 cpu_to_halt_reg r1, r0
62 ldr r3, =TEGRA_FLOW_CTRL_VIRT
63 mov r2, #FLOW_CTRL_WAITEVENT | FLOW_CTRL_JTAG_RESUME
64 str r2, [r3, r1] @ put flow controller in wait event mode
65 ldr r2, [r3, r1]
66 isb
67 dsb
68 movw r1, 0x1011
69 mov r1, r1, lsl r0
70 ldr r3, =TEGRA_CLK_RESET_VIRT
71 str r1, [r3, #0x340] @ put slave CPU in reset
72 isb
73 dsb
74 cpu_id r3
75 cmp r3, r0
76 beq .
77 mov pc, lr
78ENDPROC(tegra20_cpu_shutdown)
79#endif
Joseph Lo5c1350b2013-01-15 22:10:38 +000080
81#ifdef CONFIG_PM_SLEEP
82/*
83 * tegra_pen_lock
84 *
85 * spinlock implementation with no atomic test-and-set and no coherence
86 * using Peterson's algorithm on strongly-ordered registers
87 * used to synchronize a cpu waking up from wfi with entering lp2 on idle
88 *
89 * The reference link of Peterson's algorithm:
90 * http://en.wikipedia.org/wiki/Peterson's_algorithm
91 *
92 * SCRATCH37 = r1 = !turn (inverted from Peterson's algorithm)
93 * on cpu 0:
94 * r2 = flag[0] (in SCRATCH38)
95 * r3 = flag[1] (in SCRATCH39)
96 * on cpu1:
97 * r2 = flag[1] (in SCRATCH39)
98 * r3 = flag[0] (in SCRATCH38)
99 *
100 * must be called with MMU on
101 * corrupts r0-r3, r12
102 */
103ENTRY(tegra_pen_lock)
104 mov32 r3, TEGRA_PMC_VIRT
105 cpu_id r0
106 add r1, r3, #PMC_SCRATCH37
107 cmp r0, #0
108 addeq r2, r3, #PMC_SCRATCH38
109 addeq r3, r3, #PMC_SCRATCH39
110 addne r2, r3, #PMC_SCRATCH39
111 addne r3, r3, #PMC_SCRATCH38
112
113 mov r12, #1
114 str r12, [r2] @ flag[cpu] = 1
115 dsb
116 str r12, [r1] @ !turn = cpu
1171: dsb
118 ldr r12, [r3]
119 cmp r12, #1 @ flag[!cpu] == 1?
120 ldreq r12, [r1]
121 cmpeq r12, r0 @ !turn == cpu?
122 beq 1b @ while !turn == cpu && flag[!cpu] == 1
123
124 mov pc, lr @ locked
125ENDPROC(tegra_pen_lock)
126
127ENTRY(tegra_pen_unlock)
128 dsb
129 mov32 r3, TEGRA_PMC_VIRT
130 cpu_id r0
131 cmp r0, #0
132 addeq r2, r3, #PMC_SCRATCH38
133 addne r2, r3, #PMC_SCRATCH39
134 mov r12, #0
135 str r12, [r2]
136 mov pc, lr
137ENDPROC(tegra_pen_unlock)
138
139/*
140 * tegra20_cpu_clear_resettable(void)
141 *
142 * Called to clear the "resettable soon" flag in PMC_SCRATCH41 when
143 * it is expected that the secondary CPU will be idle soon.
144 */
145ENTRY(tegra20_cpu_clear_resettable)
146 mov32 r1, TEGRA_PMC_VIRT + PMC_SCRATCH41
147 mov r12, #CPU_NOT_RESETTABLE
148 str r12, [r1]
149 mov pc, lr
150ENDPROC(tegra20_cpu_clear_resettable)
151
152/*
153 * tegra20_cpu_set_resettable_soon(void)
154 *
155 * Called to set the "resettable soon" flag in PMC_SCRATCH41 when
156 * it is expected that the secondary CPU will be idle soon.
157 */
158ENTRY(tegra20_cpu_set_resettable_soon)
159 mov32 r1, TEGRA_PMC_VIRT + PMC_SCRATCH41
160 mov r12, #CPU_RESETTABLE_SOON
161 str r12, [r1]
162 mov pc, lr
163ENDPROC(tegra20_cpu_set_resettable_soon)
164
165/*
166 * tegra20_sleep_cpu_secondary_finish(unsigned long v2p)
167 *
168 * Enters WFI on secondary CPU by exiting coherency.
169 */
170ENTRY(tegra20_sleep_cpu_secondary_finish)
171 stmfd sp!, {r4-r11, lr}
172
173 mrc p15, 0, r11, c1, c0, 1 @ save actlr before exiting coherency
174
175 /* Flush and disable the L1 data cache */
176 bl tegra_disable_clean_inv_dcache
177
178 mov32 r0, TEGRA_PMC_VIRT + PMC_SCRATCH41
179 mov r3, #CPU_RESETTABLE
180 str r3, [r0]
181
182 bl cpu_do_idle
183
184 /*
185 * cpu may be reset while in wfi, which will return through
186 * tegra_resume to cpu_resume
187 * or interrupt may wake wfi, which will return here
188 * cpu state is unchanged - MMU is on, cache is on, coherency
189 * is off, and the data cache is off
190 *
191 * r11 contains the original actlr
192 */
193
194 bl tegra_pen_lock
195
196 mov32 r3, TEGRA_PMC_VIRT
197 add r0, r3, #PMC_SCRATCH41
198 mov r3, #CPU_NOT_RESETTABLE
199 str r3, [r0]
200
201 bl tegra_pen_unlock
202
203 /* Re-enable the data cache */
204 mrc p15, 0, r10, c1, c0, 0
205 orr r10, r10, #CR_C
206 mcr p15, 0, r10, c1, c0, 0
207 isb
208
209 mcr p15, 0, r11, c1, c0, 1 @ reenable coherency
210
211 /* Invalidate the TLBs & BTAC */
212 mov r1, #0
213 mcr p15, 0, r1, c8, c3, 0 @ invalidate shared TLBs
214 mcr p15, 0, r1, c7, c1, 6 @ invalidate shared BTAC
215 dsb
216 isb
217
218 /* the cpu was running with coherency disabled,
219 * caches may be out of date */
220 bl v7_flush_kern_cache_louis
221
222 ldmfd sp!, {r4 - r11, pc}
223ENDPROC(tegra20_sleep_cpu_secondary_finish)
224#endif