blob: e718350367e4a3df7ca898635f43203ce7118d73 [file] [log] [blame]
Joseph Lod457ef352012-10-31 17:41:17 +08001/*
2 * CPU complex suspend & resume functions for Tegra SoCs
3 *
4 * Copyright (c) 2009-2012, NVIDIA Corporation. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <linux/kernel.h>
20#include <linux/spinlock.h>
21#include <linux/io.h>
22#include <linux/cpumask.h>
Joseph Lod5529202012-10-31 17:41:21 +080023#include <linux/delay.h>
24#include <linux/cpu_pm.h>
Joseph Loc8c2e602013-04-03 19:31:47 +080025#include <linux/suspend.h>
Joseph Lod5529202012-10-31 17:41:21 +080026#include <linux/err.h>
Prashant Gaikwad89572c72013-01-11 13:16:21 +053027#include <linux/clk/tegra.h>
Joseph Lod5529202012-10-31 17:41:21 +080028
29#include <asm/smp_plat.h>
30#include <asm/cacheflush.h>
31#include <asm/suspend.h>
32#include <asm/idmap.h>
33#include <asm/proc-fns.h>
34#include <asm/tlbflush.h>
Joseph Lod457ef352012-10-31 17:41:17 +080035
36#include "iomap.h"
37#include "reset.h"
Joseph Lod5529202012-10-31 17:41:21 +080038#include "flowctrl.h"
Joseph Lo5c1350b2013-01-15 22:10:38 +000039#include "fuse.h"
Joseph Lo95872f42013-08-12 17:40:03 +080040#include "pm.h"
Joseph Lo0337c3e2013-04-03 19:31:28 +080041#include "pmc.h"
Joseph Lod5529202012-10-31 17:41:21 +080042#include "sleep.h"
Joseph Lod5529202012-10-31 17:41:21 +080043
Joseph Lod457ef352012-10-31 17:41:17 +080044#ifdef CONFIG_PM_SLEEP
Joseph Lod457ef352012-10-31 17:41:17 +080045static DEFINE_SPINLOCK(tegra_lp2_lock);
Joseph Lo95872f42013-08-12 17:40:03 +080046static u32 iram_save_size;
47static void *iram_save_addr;
48struct tegra_lp1_iram tegra_lp1_iram;
Joseph Lod5529202012-10-31 17:41:21 +080049void (*tegra_tear_down_cpu)(void);
Joseph Lo95872f42013-08-12 17:40:03 +080050void (*tegra_sleep_core_finish)(unsigned long v2p);
51static int (*tegra_sleep_func)(unsigned long v2p);
Joseph Lod457ef352012-10-31 17:41:17 +080052
Joseph Lobf91add2013-06-04 18:47:33 +080053static void tegra_tear_down_cpu_init(void)
54{
55 switch (tegra_chip_id) {
56 case TEGRA20:
57 if (IS_ENABLED(CONFIG_ARCH_TEGRA_2x_SOC))
58 tegra_tear_down_cpu = tegra20_tear_down_cpu;
59 break;
60 case TEGRA30:
Joseph Lob573ad92013-07-03 17:50:42 +080061 case TEGRA114:
62 if (IS_ENABLED(CONFIG_ARCH_TEGRA_3x_SOC) ||
63 IS_ENABLED(CONFIG_ARCH_TEGRA_114_SOC))
Joseph Lobf91add2013-06-04 18:47:33 +080064 tegra_tear_down_cpu = tegra30_tear_down_cpu;
65 break;
66 }
67}
68
Joseph Lod5529202012-10-31 17:41:21 +080069/*
70 * restore_cpu_complex
71 *
72 * restores cpu clock setting, clears flow controller
73 *
74 * Always called on CPU 0.
75 */
76static void restore_cpu_complex(void)
77{
78 int cpu = smp_processor_id();
79
80 BUG_ON(cpu != 0);
81
82#ifdef CONFIG_SMP
83 cpu = cpu_logical_map(cpu);
84#endif
85
86 /* Restore the CPU clock settings */
87 tegra_cpu_clock_resume();
88
89 flowctrl_cpu_suspend_exit(cpu);
Joseph Lod5529202012-10-31 17:41:21 +080090}
91
92/*
93 * suspend_cpu_complex
94 *
95 * saves pll state for use by restart_plls, prepares flow controller for
96 * transition to suspend state
97 *
98 * Must always be called on cpu 0.
99 */
100static void suspend_cpu_complex(void)
101{
102 int cpu = smp_processor_id();
103
104 BUG_ON(cpu != 0);
105
106#ifdef CONFIG_SMP
107 cpu = cpu_logical_map(cpu);
108#endif
109
110 /* Save the CPU clock settings */
111 tegra_cpu_clock_suspend();
112
113 flowctrl_cpu_suspend_enter(cpu);
Joseph Lod5529202012-10-31 17:41:21 +0800114}
115
Joseph Lo8f6a0b62013-06-04 18:47:35 +0800116void tegra_clear_cpu_in_lp2(void)
Joseph Lod457ef352012-10-31 17:41:17 +0800117{
Joseph Lo8f6a0b62013-06-04 18:47:35 +0800118 int phy_cpu_id = cpu_logical_map(smp_processor_id());
Joseph Lod457ef352012-10-31 17:41:17 +0800119 u32 *cpu_in_lp2 = tegra_cpu_lp2_mask;
120
121 spin_lock(&tegra_lp2_lock);
122
123 BUG_ON(!(*cpu_in_lp2 & BIT(phy_cpu_id)));
124 *cpu_in_lp2 &= ~BIT(phy_cpu_id);
125
126 spin_unlock(&tegra_lp2_lock);
127}
128
Joseph Lo8f6a0b62013-06-04 18:47:35 +0800129bool tegra_set_cpu_in_lp2(void)
Joseph Lod457ef352012-10-31 17:41:17 +0800130{
Joseph Lo8f6a0b62013-06-04 18:47:35 +0800131 int phy_cpu_id = cpu_logical_map(smp_processor_id());
Joseph Lod457ef352012-10-31 17:41:17 +0800132 bool last_cpu = false;
133 cpumask_t *cpu_lp2_mask = tegra_cpu_lp2_mask;
134 u32 *cpu_in_lp2 = tegra_cpu_lp2_mask;
135
136 spin_lock(&tegra_lp2_lock);
137
138 BUG_ON((*cpu_in_lp2 & BIT(phy_cpu_id)));
139 *cpu_in_lp2 |= BIT(phy_cpu_id);
140
141 if ((phy_cpu_id == 0) && cpumask_equal(cpu_lp2_mask, cpu_online_mask))
142 last_cpu = true;
Joseph Lo5c1350b2013-01-15 22:10:38 +0000143 else if (tegra_chip_id == TEGRA20 && phy_cpu_id == 1)
144 tegra20_cpu_set_resettable_soon();
Joseph Lod457ef352012-10-31 17:41:17 +0800145
146 spin_unlock(&tegra_lp2_lock);
147 return last_cpu;
148}
Joseph Lod5529202012-10-31 17:41:21 +0800149
Arnd Bergmann20588422013-04-23 15:36:26 +0200150int tegra_cpu_do_idle(void)
151{
152 return cpu_do_idle();
153}
154
Joseph Lod5529202012-10-31 17:41:21 +0800155static int tegra_sleep_cpu(unsigned long v2p)
156{
Will Deacon6affb482013-03-25 18:19:11 +0000157 setup_mm_for_reboot();
Joseph Lod5529202012-10-31 17:41:21 +0800158 tegra_sleep_cpu_finish(v2p);
159
160 /* should never here */
161 BUG();
162
163 return 0;
164}
165
Joseph Lo4d82d052013-04-02 01:20:50 +0000166void tegra_idle_lp2_last(void)
Joseph Lod5529202012-10-31 17:41:21 +0800167{
Joseph Loc8c2e602013-04-03 19:31:47 +0800168 tegra_pmc_pm_set(TEGRA_SUSPEND_LP2);
Joseph Lod5529202012-10-31 17:41:21 +0800169
170 cpu_cluster_pm_enter();
171 suspend_cpu_complex();
Joseph Lod5529202012-10-31 17:41:21 +0800172
173 cpu_suspend(PHYS_OFFSET - PAGE_OFFSET, &tegra_sleep_cpu);
174
Joseph Lod5529202012-10-31 17:41:21 +0800175 restore_cpu_complex();
176 cpu_cluster_pm_exit();
177}
Joseph Loc8c2e602013-04-03 19:31:47 +0800178
179enum tegra_suspend_mode tegra_pm_validate_suspend_mode(
180 enum tegra_suspend_mode mode)
181{
Joseph Loc8c2e602013-04-03 19:31:47 +0800182 /*
Joseph Lo95872f42013-08-12 17:40:03 +0800183 * The Tegra devices support suspending to LP1 or lower currently.
Joseph Loc8c2e602013-04-03 19:31:47 +0800184 */
Joseph Lo95872f42013-08-12 17:40:03 +0800185 if (mode > TEGRA_SUSPEND_LP1)
186 return TEGRA_SUSPEND_LP1;
Joseph Loc8c2e602013-04-03 19:31:47 +0800187
188 return mode;
189}
190
Joseph Lo95872f42013-08-12 17:40:03 +0800191static int tegra_sleep_core(unsigned long v2p)
192{
193 setup_mm_for_reboot();
194 tegra_sleep_core_finish(v2p);
195
196 /* should never here */
197 BUG();
198
199 return 0;
200}
201
202/*
203 * tegra_lp1_iram_hook
204 *
205 * Hooking the address of LP1 reset vector and SDRAM self-refresh code in
206 * SDRAM. These codes not be copied to IRAM in this fuction. We need to
207 * copy these code to IRAM before LP0/LP1 suspend and restore the content
208 * of IRAM after resume.
209 */
210static bool tegra_lp1_iram_hook(void)
211{
Joseph Loe7a932b2013-08-12 17:40:04 +0800212 switch (tegra_chip_id) {
Joseph Lo731a9272013-08-12 17:40:05 +0800213 case TEGRA20:
214 if (IS_ENABLED(CONFIG_ARCH_TEGRA_2x_SOC))
215 tegra20_lp1_iram_hook();
216 break;
Joseph Loe7a932b2013-08-12 17:40:04 +0800217 case TEGRA30:
218 if (IS_ENABLED(CONFIG_ARCH_TEGRA_3x_SOC))
219 tegra30_lp1_iram_hook();
220 break;
221 default:
222 break;
223 }
224
Joseph Lo95872f42013-08-12 17:40:03 +0800225 if (!tegra_lp1_iram.start_addr || !tegra_lp1_iram.end_addr)
226 return false;
227
228 iram_save_size = tegra_lp1_iram.end_addr - tegra_lp1_iram.start_addr;
229 iram_save_addr = kmalloc(iram_save_size, GFP_KERNEL);
230 if (!iram_save_addr)
231 return false;
232
233 return true;
234}
235
236static bool tegra_sleep_core_init(void)
237{
Joseph Loe7a932b2013-08-12 17:40:04 +0800238 switch (tegra_chip_id) {
Joseph Lo731a9272013-08-12 17:40:05 +0800239 case TEGRA20:
240 if (IS_ENABLED(CONFIG_ARCH_TEGRA_2x_SOC))
241 tegra20_sleep_core_init();
242 break;
Joseph Loe7a932b2013-08-12 17:40:04 +0800243 case TEGRA30:
244 if (IS_ENABLED(CONFIG_ARCH_TEGRA_3x_SOC))
245 tegra30_sleep_core_init();
246 break;
247 default:
248 break;
249 }
250
Joseph Lo95872f42013-08-12 17:40:03 +0800251 if (!tegra_sleep_core_finish)
252 return false;
253
254 return true;
255}
256
257static void tegra_suspend_enter_lp1(void)
258{
259 tegra_pmc_suspend();
260
261 /* copy the reset vector & SDRAM shutdown code into IRAM */
262 memcpy(iram_save_addr, IO_ADDRESS(TEGRA_IRAM_CODE_AREA),
263 iram_save_size);
264 memcpy(IO_ADDRESS(TEGRA_IRAM_CODE_AREA), tegra_lp1_iram.start_addr,
265 iram_save_size);
266
267 *((u32 *)tegra_cpu_lp1_mask) = 1;
268}
269
270static void tegra_suspend_exit_lp1(void)
271{
272 tegra_pmc_resume();
273
274 /* restore IRAM */
275 memcpy(IO_ADDRESS(TEGRA_IRAM_CODE_AREA), iram_save_addr,
276 iram_save_size);
277
278 *(u32 *)tegra_cpu_lp1_mask = 0;
279}
280
Joseph Loc8c2e602013-04-03 19:31:47 +0800281static const char *lp_state[TEGRA_MAX_SUSPEND_MODE] = {
282 [TEGRA_SUSPEND_NONE] = "none",
283 [TEGRA_SUSPEND_LP2] = "LP2",
284 [TEGRA_SUSPEND_LP1] = "LP1",
285 [TEGRA_SUSPEND_LP0] = "LP0",
286};
287
288static int __cpuinit tegra_suspend_enter(suspend_state_t state)
289{
290 enum tegra_suspend_mode mode = tegra_pmc_get_suspend_mode();
291
292 if (WARN_ON(mode < TEGRA_SUSPEND_NONE ||
293 mode >= TEGRA_MAX_SUSPEND_MODE))
294 return -EINVAL;
295
296 pr_info("Entering suspend state %s\n", lp_state[mode]);
297
298 tegra_pmc_pm_set(mode);
299
300 local_fiq_disable();
301
302 suspend_cpu_complex();
303 switch (mode) {
Joseph Lo95872f42013-08-12 17:40:03 +0800304 case TEGRA_SUSPEND_LP1:
305 tegra_suspend_enter_lp1();
306 break;
Joseph Loc8c2e602013-04-03 19:31:47 +0800307 case TEGRA_SUSPEND_LP2:
Joseph Lo8f6a0b62013-06-04 18:47:35 +0800308 tegra_set_cpu_in_lp2();
Joseph Loc8c2e602013-04-03 19:31:47 +0800309 break;
310 default:
311 break;
312 }
313
Joseph Lo95872f42013-08-12 17:40:03 +0800314 cpu_suspend(PHYS_OFFSET - PAGE_OFFSET, tegra_sleep_func);
Joseph Loc8c2e602013-04-03 19:31:47 +0800315
316 switch (mode) {
Joseph Lo95872f42013-08-12 17:40:03 +0800317 case TEGRA_SUSPEND_LP1:
318 tegra_suspend_exit_lp1();
319 break;
Joseph Loc8c2e602013-04-03 19:31:47 +0800320 case TEGRA_SUSPEND_LP2:
Joseph Lo8f6a0b62013-06-04 18:47:35 +0800321 tegra_clear_cpu_in_lp2();
Joseph Loc8c2e602013-04-03 19:31:47 +0800322 break;
323 default:
324 break;
325 }
326 restore_cpu_complex();
327
328 local_fiq_enable();
329
330 return 0;
331}
332
333static const struct platform_suspend_ops tegra_suspend_ops = {
334 .valid = suspend_valid_only_mem,
335 .enter = tegra_suspend_enter,
336};
337
338void __init tegra_init_suspend(void)
339{
Joseph Lo95872f42013-08-12 17:40:03 +0800340 enum tegra_suspend_mode mode = tegra_pmc_get_suspend_mode();
341
342 if (mode == TEGRA_SUSPEND_NONE)
Joseph Loc8c2e602013-04-03 19:31:47 +0800343 return;
344
Joseph Lobf91add2013-06-04 18:47:33 +0800345 tegra_tear_down_cpu_init();
Joseph Loc8c2e602013-04-03 19:31:47 +0800346 tegra_pmc_suspend_init();
347
Joseph Lo95872f42013-08-12 17:40:03 +0800348 if (mode >= TEGRA_SUSPEND_LP1) {
349 if (!tegra_lp1_iram_hook() || !tegra_sleep_core_init()) {
350 pr_err("%s: unable to allocate memory for SDRAM"
351 "self-refresh -- LP0/LP1 unavailable\n",
352 __func__);
353 tegra_pmc_set_suspend_mode(TEGRA_SUSPEND_LP2);
354 mode = TEGRA_SUSPEND_LP2;
355 }
356 }
357
358 /* set up sleep function for cpu_suspend */
359 switch (mode) {
360 case TEGRA_SUSPEND_LP1:
361 tegra_sleep_func = tegra_sleep_core;
362 break;
363 case TEGRA_SUSPEND_LP2:
364 tegra_sleep_func = tegra_sleep_cpu;
365 break;
366 default:
367 break;
368 }
369
Joseph Loc8c2e602013-04-03 19:31:47 +0800370 suspend_set_ops(&tegra_suspend_ops);
371}
Joseph Lod457ef352012-10-31 17:41:17 +0800372#endif