2 * arch/arm/mach-tegra/pm.c
4 * CPU complex suspend & resume functions for Tegra SoCs
6 * Copyright (c) 2009-2012, NVIDIA Corporation.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include <linux/kernel.h>
24 #include <linux/ctype.h>
25 #include <linux/init.h>
27 #include <linux/sched.h>
28 #include <linux/smp.h>
29 #include <linux/irq.h>
30 #include <linux/interrupt.h>
31 #include <linux/clk.h>
32 #include <linux/err.h>
33 #include <linux/debugfs.h>
34 #include <linux/delay.h>
35 #include <linux/suspend.h>
36 #include <linux/slab.h>
37 #include <linux/serial_reg.h>
38 #include <linux/seq_file.h>
39 #include <linux/uaccess.h>
40 #include <linux/syscore_ops.h>
41 #include <linux/cpu_pm.h>
42 #include <linux/clk/tegra.h>
43 #include <linux/export.h>
44 #include <linux/vmalloc.h>
45 #include <linux/memblock.h>
46 #include <linux/console.h>
47 #include <linux/tegra_audio.h>
49 #include <trace/events/power.h>
51 #include <asm/cacheflush.h>
52 #include <asm/idmap.h>
53 #include <asm/localtimer.h>
54 #include <asm/pgalloc.h>
55 #include <asm/pgtable.h>
56 #include <asm/tlbflush.h>
57 #include <asm/suspend.h>
58 #include <asm/smp_plat.h>
60 #include <mach/irqs.h>
61 #include <mach/powergate.h>
62 #include <mach/hardware.h>
77 #include "cpu-tegra.h"
79 struct suspend_context {
81 * The next 7 values are referenced by offset in __restart_plls
82 * in headsmp-t2.S, and should not be moved
99 struct tegra_twd_context twd;
102 #ifdef CONFIG_PM_SLEEP
103 phys_addr_t tegra_pgd_phys; /* pgd used by hotplug & LP2 bootup */
104 static pgd_t *tegra_pgd;
105 static DEFINE_SPINLOCK(tegra_lp2_lock);
106 static cpumask_t tegra_in_lp2;
107 static cpumask_t *iram_cpu_lp2_mask;
108 static unsigned long *iram_cpu_lp1_mask;
109 static u8 *iram_save;
110 static unsigned long iram_save_size;
111 static void __iomem *iram_code = IO_ADDRESS(TEGRA_IRAM_CODE_AREA);
112 static void __iomem *clk_rst = IO_ADDRESS(TEGRA_CLK_RESET_BASE);
113 static void __iomem *pmc = IO_ADDRESS(TEGRA_PMC_BASE);
114 static int tegra_last_pclk;
117 struct suspend_context tegra_sctx;
119 #define TEGRA_POWER_PWRREQ_POLARITY (1 << 8) /* core power request polarity */
120 #define TEGRA_POWER_PWRREQ_OE (1 << 9) /* core power request enable */
121 #define TEGRA_POWER_SYSCLK_POLARITY (1 << 10) /* sys clk polarity */
122 #define TEGRA_POWER_SYSCLK_OE (1 << 11) /* system clock enable */
123 #define TEGRA_POWER_PWRGATE_DIS (1 << 12) /* power gate disabled */
124 #define TEGRA_POWER_EFFECT_LP0 (1 << 14) /* enter LP0 when CPU pwr gated */
125 #define TEGRA_POWER_CPU_PWRREQ_POLARITY (1 << 15) /* CPU power request polarity */
126 #define TEGRA_POWER_CPU_PWRREQ_OE (1 << 16) /* CPU power request enable */
129 #define PMC_CTRL_LATCH_WAKEUPS (1 << 5)
130 #define PMC_WAKE_MASK 0xc
131 #define PMC_WAKE_LEVEL 0x10
132 #define PMC_DPAD_ORIDE 0x1C
133 #define PMC_WAKE_DELAY 0xe0
134 #define PMC_DPD_SAMPLE 0x20
136 #define PMC_WAKE_STATUS 0x14
137 #define PMC_SW_WAKE_STATUS 0x18
138 #define PMC_COREPWRGOOD_TIMER 0x3c
139 #define PMC_CPUPWRGOOD_TIMER 0xc8
140 #define PMC_CPUPWROFF_TIMER 0xcc
141 #define PMC_COREPWROFF_TIMER PMC_WAKE_DELAY
143 #define PMC_PWRGATE_TOGGLE 0x30
144 #define PWRGATE_TOGGLE_START (1 << 8)
145 #define UN_PWRGATE_CPU \
146 (PWRGATE_TOGGLE_START | TEGRA_CPU_POWERGATE_ID(TEGRA_POWERGATE_CPU))
148 #ifdef CONFIG_TEGRA_CLUSTER_CONTROL
149 #define PMC_SCRATCH4_WAKE_CLUSTER_MASK (1<<31)
152 #define CLK_RESET_CCLK_BURST 0x20
153 #define CLK_RESET_CCLK_DIVIDER 0x24
154 #define CLK_RESET_PLLC_BASE 0x80
155 #define CLK_RESET_PLLM_BASE 0x90
156 #define CLK_RESET_PLLX_BASE 0xe0
157 #define CLK_RESET_PLLX_MISC 0xe4
158 #define CLK_RESET_PLLP_BASE 0xa0
159 #define CLK_RESET_PLLP_OUTA 0xa4
160 #define CLK_RESET_PLLP_OUTB 0xa8
161 #define CLK_RESET_PLLP_MISC 0xac
163 #define CLK_RESET_SOURCE_CSITE 0x1d4
165 #define CLK_RESET_CCLK_BURST_POLICY_SHIFT 28
166 #define CLK_RESET_CCLK_RUN_POLICY_SHIFT 4
167 #define CLK_RESET_CCLK_IDLE_POLICY_SHIFT 0
168 #define CLK_RESET_CCLK_IDLE_POLICY 1
169 #define CLK_RESET_CCLK_RUN_POLICY 2
170 #define CLK_RESET_CCLK_BURST_POLICY_PLLM 3
171 #define CLK_RESET_CCLK_BURST_POLICY_PLLX 8
173 #define EMC_MRW_0 0x0e8
174 #define EMC_MRW_DEV_SELECTN 30
175 #define EMC_MRW_DEV_NONE (3 << EMC_MRW_DEV_SELECTN)
177 #define MC_SECURITY_START 0x6c
178 #define MC_SECURITY_SIZE 0x70
179 #define MC_SECURITY_CFG2 0x7c
181 struct dvfs_rail *tegra_cpu_rail;
182 static struct dvfs_rail *tegra_core_rail;
183 static struct clk *tegra_pclk;
184 static const struct tegra_suspend_platform_data *pdata;
185 static enum tegra_suspend_mode current_suspend_mode = TEGRA_SUSPEND_NONE;
187 #if defined(CONFIG_TEGRA_CLUSTER_CONTROL) && INSTRUMENT_CLUSTER_SWITCH
188 enum tegra_cluster_switch_time_id {
189 tegra_cluster_switch_time_id_start = 0,
190 tegra_cluster_switch_time_id_prolog,
191 tegra_cluster_switch_time_id_switch,
192 tegra_cluster_switch_time_id_epilog,
193 tegra_cluster_switch_time_id_max
197 tegra_cluster_switch_times[tegra_cluster_switch_time_id_max];
198 #define tegra_cluster_switch_time(flags, id) \
201 if (flags & TEGRA_POWER_CLUSTER_MASK) { \
202 void __iomem *timer_us = \
203 IO_ADDRESS(TEGRA_TMRUS_BASE); \
204 if (id < tegra_cluster_switch_time_id_max) \
205 tegra_cluster_switch_times[id] = \
212 #define tegra_cluster_switch_time(flags, id) do {} while(0)
215 #ifdef CONFIG_PM_SLEEP
216 static const char *tegra_suspend_name[TEGRA_MAX_SUSPEND_MODE] = {
217 [TEGRA_SUSPEND_NONE] = "none",
218 [TEGRA_SUSPEND_LP2] = "lp2",
219 [TEGRA_SUSPEND_LP1] = "lp1",
220 [TEGRA_SUSPEND_LP0] = "lp0",
223 unsigned long tegra_cpu_power_good_time(void)
225 if (WARN_ON_ONCE(!pdata))
228 return pdata->cpu_timer;
231 unsigned long tegra_cpu_power_off_time(void)
233 if (WARN_ON_ONCE(!pdata))
236 return pdata->cpu_off_timer;
239 unsigned long tegra_cpu_lp2_min_residency(void)
241 if (WARN_ON_ONCE(!pdata))
244 return pdata->cpu_lp2_min_residency;
248 * create_suspend_pgtable
250 * Creates a page table with identity mappings of physical memory and IRAM
251 * for use when the MMU is off, in addition to all the regular kernel mappings.
253 static __init int create_suspend_pgtable(void)
255 tegra_pgd = pgd_alloc(&init_mm);
259 /* Only identity-map size of lowmem (high_memory - PAGE_OFFSET) */
260 identity_mapping_add(tegra_pgd, phys_to_virt(PHYS_OFFSET),
262 identity_mapping_add(tegra_pgd, IO_IRAM_VIRT,
263 IO_IRAM_VIRT + SECTION_SIZE, 0);
265 /* inner/outer write-back/write-allocate, sharable */
266 tegra_pgd_phys = (virt_to_phys(tegra_pgd) & PAGE_MASK) | 0x4A;
271 /* ensures that sufficient time is passed for a register write to
272 * serialize into the 32KHz domain */
273 static void pmc_32kwritel(u32 val, unsigned long offs)
275 writel(val, pmc + offs);
279 static void set_power_timers(unsigned long us_on, unsigned long us_off,
282 static unsigned long last_us_off = 0;
283 unsigned long long ticks;
284 unsigned long long pclk;
286 if (WARN_ON_ONCE(rate <= 0))
291 if ((rate != tegra_last_pclk) || (us_off != last_us_off)) {
292 ticks = (us_on * pclk) + 999999ull;
293 do_div(ticks, 1000000);
294 writel((unsigned long)ticks, pmc + PMC_CPUPWRGOOD_TIMER);
296 ticks = (us_off * pclk) + 999999ull;
297 do_div(ticks, 1000000);
298 writel((unsigned long)ticks, pmc + PMC_CPUPWROFF_TIMER);
301 tegra_last_pclk = pclk;
302 last_us_off = us_off;
306 * restore_cpu_complex
308 * restores cpu clock setting, clears flow controller
310 * Always called on CPU 0.
312 static void restore_cpu_complex(u32 mode)
314 int cpu = smp_processor_id();
315 unsigned int reg, policy;
320 cpu = cpu_logical_map(cpu);
322 /* Is CPU complex already running on PLLX? */
323 reg = readl(clk_rst + CLK_RESET_CCLK_BURST);
324 policy = (reg >> CLK_RESET_CCLK_BURST_POLICY_SHIFT) & 0xF;
326 if (policy == CLK_RESET_CCLK_IDLE_POLICY)
327 reg = (reg >> CLK_RESET_CCLK_IDLE_POLICY_SHIFT) & 0xF;
328 else if (policy == CLK_RESET_CCLK_RUN_POLICY)
329 reg = (reg >> CLK_RESET_CCLK_RUN_POLICY_SHIFT) & 0xF;
333 if (reg != CLK_RESET_CCLK_BURST_POLICY_PLLX) {
334 /* restore PLLX settings if CPU is on different PLL */
335 writel(tegra_sctx.pllx_misc, clk_rst + CLK_RESET_PLLX_MISC);
336 writel(tegra_sctx.pllx_base, clk_rst + CLK_RESET_PLLX_BASE);
338 /* wait for PLL stabilization if PLLX was enabled */
339 if (tegra_sctx.pllx_base & (1<<30)) {
340 #if USE_PLL_LOCK_BITS
341 /* Enable lock detector */
342 reg = readl(clk_rst + CLK_RESET_PLLX_MISC);
344 writel(reg, clk_rst + CLK_RESET_PLLX_MISC);
345 while (!(readl(clk_rst + CLK_RESET_PLLX_BASE) &
349 udelay(PLL_POST_LOCK_DELAY);
356 /* Restore original burst policy setting for calls resulting from CPU
357 LP2 in idle or system suspend; keep cluster switch prolog setting
359 if (!(mode & TEGRA_POWER_CLUSTER_MASK)) {
360 writel(tegra_sctx.cclk_divider, clk_rst +
361 CLK_RESET_CCLK_DIVIDER);
362 writel(tegra_sctx.cpu_burst, clk_rst +
363 CLK_RESET_CCLK_BURST);
366 writel(tegra_sctx.clk_csite_src, clk_rst + CLK_RESET_SOURCE_CSITE);
368 /* Do not power-gate CPU 0 when flow controlled */
369 reg = readl(FLOW_CTRL_CPU_CSR(cpu));
370 reg &= ~FLOW_CTRL_CSR_WFE_BITMAP; /* clear wfe bitmap */
371 reg &= ~FLOW_CTRL_CSR_WFI_BITMAP; /* clear wfi bitmap */
372 reg &= ~FLOW_CTRL_CSR_ENABLE; /* clear enable */
373 reg |= FLOW_CTRL_CSR_INTR_FLAG; /* clear intr */
374 reg |= FLOW_CTRL_CSR_EVENT_FLAG; /* clear event */
375 flowctrl_writel(reg, FLOW_CTRL_CPU_CSR(cpu));
377 /* If an immedidate cluster switch is being perfomed, restore the
378 local timer registers. For calls resulting from CPU LP2 in
379 idle or system suspend, the local timer was shut down and
380 timekeeping switched over to the global system timer. In this
381 case keep local timer disabled, and restore only periodic load. */
382 if (!(mode & (TEGRA_POWER_CLUSTER_MASK |
383 TEGRA_POWER_CLUSTER_IMMEDIATE)))
384 tegra_sctx.twd.twd_ctrl = 0;
385 tegra_twd_resume(&tegra_sctx.twd);
389 * suspend_cpu_complex
391 * saves pll state for use by restart_plls, prepares flow controller for
392 * transition to suspend state
394 * Must always be called on cpu 0.
396 static void suspend_cpu_complex(u32 mode)
398 int cpu = smp_processor_id();
405 cpu = cpu_logical_map(cpu);
407 /* switch coresite to clk_m, save off original source */
408 tegra_sctx.clk_csite_src = readl(clk_rst + CLK_RESET_SOURCE_CSITE);
409 writel(3<<30, clk_rst + CLK_RESET_SOURCE_CSITE);
411 tegra_sctx.cpu_burst = readl(clk_rst + CLK_RESET_CCLK_BURST);
412 tegra_sctx.pllx_base = readl(clk_rst + CLK_RESET_PLLX_BASE);
413 tegra_sctx.pllx_misc = readl(clk_rst + CLK_RESET_PLLX_MISC);
414 tegra_sctx.pllp_base = readl(clk_rst + CLK_RESET_PLLP_BASE);
415 tegra_sctx.pllp_outa = readl(clk_rst + CLK_RESET_PLLP_OUTA);
416 tegra_sctx.pllp_outb = readl(clk_rst + CLK_RESET_PLLP_OUTB);
417 tegra_sctx.pllp_misc = readl(clk_rst + CLK_RESET_PLLP_MISC);
418 tegra_sctx.cclk_divider = readl(clk_rst + CLK_RESET_CCLK_DIVIDER);
420 tegra_twd_suspend(&tegra_sctx.twd);
422 reg = readl(FLOW_CTRL_CPU_CSR(cpu));
423 reg &= ~FLOW_CTRL_CSR_WFE_BITMAP; /* clear wfe bitmap */
424 reg &= ~FLOW_CTRL_CSR_WFI_BITMAP; /* clear wfi bitmap */
425 reg |= FLOW_CTRL_CSR_INTR_FLAG; /* clear intr flag */
426 reg |= FLOW_CTRL_CSR_EVENT_FLAG; /* clear event flag */
427 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
428 reg |= FLOW_CTRL_CSR_WFE_CPU0 << cpu; /* enable power gating on wfe */
430 reg |= FLOW_CTRL_CSR_WFI_CPU0 << cpu; /* enable power gating on wfi */
432 reg |= FLOW_CTRL_CSR_ENABLE; /* enable power gating */
433 flowctrl_writel(reg, FLOW_CTRL_CPU_CSR(cpu));
435 for (i = 0; i < num_possible_cpus(); i++) {
438 reg = readl(FLOW_CTRL_CPU_CSR(i));
439 reg |= FLOW_CTRL_CSR_EVENT_FLAG;
440 reg |= FLOW_CTRL_CSR_INTR_FLAG;
441 flowctrl_writel(reg, FLOW_CTRL_CPU_CSR(i));
444 tegra_gic_cpu_disable(true);
447 void tegra_clear_cpu_in_lp2(int cpu)
449 spin_lock(&tegra_lp2_lock);
450 BUG_ON(!cpumask_test_cpu(cpu, &tegra_in_lp2));
451 cpumask_clear_cpu(cpu, &tegra_in_lp2);
453 /* Update the IRAM copy used by the reset handler. The IRAM copy
454 can't use used directly by cpumask_clear_cpu() because it uses
455 LDREX/STREX which requires the addressed location to be inner
456 cacheable and sharable which IRAM isn't. */
457 writel(tegra_in_lp2.bits[0], iram_cpu_lp2_mask);
460 spin_unlock(&tegra_lp2_lock);
463 bool tegra_set_cpu_in_lp2(int cpu)
465 bool last_cpu = false;
467 spin_lock(&tegra_lp2_lock);
468 BUG_ON(cpumask_test_cpu(cpu, &tegra_in_lp2));
469 cpumask_set_cpu(cpu, &tegra_in_lp2);
471 /* Update the IRAM copy used by the reset handler. The IRAM copy
472 can't use used directly by cpumask_set_cpu() because it uses
473 LDREX/STREX which requires the addressed location to be inner
474 cacheable and sharable which IRAM isn't. */
475 writel(tegra_in_lp2.bits[0], iram_cpu_lp2_mask);
478 if ((cpu == 0) && cpumask_equal(&tegra_in_lp2, cpu_online_mask))
480 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
482 tegra2_cpu_set_resettable_soon();
485 spin_unlock(&tegra_lp2_lock);
489 static void tegra_sleep_core(enum tegra_suspend_mode mode,
492 #ifdef CONFIG_TRUSTED_FOUNDATIONS
493 if (mode == TEGRA_SUSPEND_LP0) {
494 tegra_generic_smc(0xFFFFFFFC, 0xFFFFFFE3,
495 virt_to_phys(tegra_resume));
497 tegra_generic_smc(0xFFFFFFFC, 0xFFFFFFE6,
498 (TEGRA_RESET_HANDLER_BASE +
499 tegra_cpu_reset_handler_offset));
502 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
503 cpu_suspend(v2p, tegra2_sleep_core_finish);
505 cpu_suspend(v2p, tegra3_sleep_core_finish);
509 static inline void tegra_sleep_cpu(unsigned long v2p)
511 #ifdef CONFIG_TRUSTED_FOUNDATIONS
512 tegra_generic_smc(0xFFFFFFFC, 0xFFFFFFE4,
513 (TEGRA_RESET_HANDLER_BASE +
514 tegra_cpu_reset_handler_offset));
516 cpu_suspend(v2p, tegra_sleep_cpu_finish);
519 unsigned int tegra_idle_lp2_last(unsigned int sleep_time, unsigned int flags)
521 u32 mode; /* hardware + software power mode flags */
525 /* Only the last cpu down does the final suspend steps */
526 mode = readl(pmc + PMC_CTRL);
527 mode |= TEGRA_POWER_CPU_PWRREQ_OE;
528 if (pdata->combined_req)
529 mode &= ~TEGRA_POWER_PWRREQ_OE;
531 mode |= TEGRA_POWER_PWRREQ_OE;
532 mode &= ~TEGRA_POWER_EFFECT_LP0;
533 writel(mode, pmc + PMC_CTRL);
536 tegra_cluster_switch_time(flags, tegra_cluster_switch_time_id_start);
539 * We can use clk_get_rate_all_locked() here, because all other cpus
540 * are in LP2 state and irqs are disabled
542 if (flags & TEGRA_POWER_CLUSTER_MASK) {
543 trace_cpu_cluster(POWER_CPU_CLUSTER_START);
544 set_power_timers(pdata->cpu_timer, 0,
545 clk_get_rate_all_locked(tegra_pclk));
546 if (flags & TEGRA_POWER_CLUSTER_G) {
548 * To reduce the vdd_cpu up latency when LP->G
549 * transition. Before the transition, enable
553 writel(UN_PWRGATE_CPU,
554 pmc + PMC_PWRGATE_TOGGLE);
556 tegra_cluster_switch_prolog(mode);
558 set_power_timers(pdata->cpu_timer, pdata->cpu_off_timer,
559 clk_get_rate_all_locked(tegra_pclk));
563 tegra_lp2_set_trigger(sleep_time);
565 cpu_cluster_pm_enter();
566 suspend_cpu_complex(mode);
567 tegra_cluster_switch_time(flags, tegra_cluster_switch_time_id_prolog);
570 * No need to flush complete L2. Cleaning kernel and IO mappings
571 * is enough for the LP code sequence that has L2 disabled but
575 outer_clean_range(__pa(pgd + USER_PTRS_PER_PGD),
576 __pa(pgd + PTRS_PER_PGD));
579 tegra_sleep_cpu(PHYS_OFFSET - PAGE_OFFSET);
581 tegra_init_cache(false);
582 tegra_cluster_switch_time(flags, tegra_cluster_switch_time_id_switch);
583 restore_cpu_complex(mode);
584 cpu_cluster_pm_exit();
586 remain = tegra_lp2_timer_remain();
588 tegra_lp2_set_trigger(0);
590 if (flags & TEGRA_POWER_CLUSTER_MASK) {
591 tegra_cluster_switch_epilog(mode);
592 trace_cpu_cluster(POWER_CPU_CLUSTER_DONE);
594 tegra_cluster_switch_time(flags, tegra_cluster_switch_time_id_epilog);
596 #if INSTRUMENT_CLUSTER_SWITCH
597 if (flags & TEGRA_POWER_CLUSTER_MASK) {
598 pr_err("%s: prolog %lu us, switch %lu us, epilog %lu us, total %lu us\n",
599 is_lp_cluster() ? "G=>LP" : "LP=>G",
600 tegra_cluster_switch_times[tegra_cluster_switch_time_id_prolog] -
601 tegra_cluster_switch_times[tegra_cluster_switch_time_id_start],
602 tegra_cluster_switch_times[tegra_cluster_switch_time_id_switch] -
603 tegra_cluster_switch_times[tegra_cluster_switch_time_id_prolog],
604 tegra_cluster_switch_times[tegra_cluster_switch_time_id_epilog] -
605 tegra_cluster_switch_times[tegra_cluster_switch_time_id_switch],
606 tegra_cluster_switch_times[tegra_cluster_switch_time_id_epilog] -
607 tegra_cluster_switch_times[tegra_cluster_switch_time_id_start]);
613 static int tegra_common_suspend(void)
615 void __iomem *mc = IO_ADDRESS(TEGRA_MC_BASE);
617 tegra_sctx.mc[0] = readl(mc + MC_SECURITY_START);
618 tegra_sctx.mc[1] = readl(mc + MC_SECURITY_SIZE);
619 tegra_sctx.mc[2] = readl(mc + MC_SECURITY_CFG2);
621 /* copy the reset vector and SDRAM shutdown code into IRAM */
622 memcpy(iram_save, iram_code, iram_save_size);
623 memcpy(iram_code, tegra_iram_start(), iram_save_size);
628 static void tegra_common_resume(void)
630 void __iomem *mc = IO_ADDRESS(TEGRA_MC_BASE);
631 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
632 void __iomem *emc = IO_ADDRESS(TEGRA_EMC_BASE);
635 /* Clear DPD sample */
636 writel(0x0, pmc + PMC_DPD_SAMPLE);
638 writel(tegra_sctx.mc[0], mc + MC_SECURITY_START);
639 writel(tegra_sctx.mc[1], mc + MC_SECURITY_SIZE);
640 writel(tegra_sctx.mc[2], mc + MC_SECURITY_CFG2);
641 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
642 /* trigger emc mode write */
643 writel(EMC_MRW_DEV_NONE, emc + EMC_MRW_0);
645 /* clear scratch registers shared by suspend and the reset pen */
646 writel(0x0, pmc + PMC_SCRATCH39);
647 writel(0x0, pmc + PMC_SCRATCH41);
650 memcpy(iram_code, iram_save, iram_save_size);
653 static int tegra_suspend_prepare_late(void)
655 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
656 disable_irq(INT_SYS_STATS_MON);
661 static void tegra_suspend_wake(void)
663 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
664 enable_irq(INT_SYS_STATS_MON);
668 static void tegra_pm_set(enum tegra_suspend_mode mode)
671 unsigned long rate = 32768;
673 reg = readl(pmc + PMC_CTRL);
674 reg |= TEGRA_POWER_CPU_PWRREQ_OE;
675 if (pdata->combined_req)
676 reg &= ~TEGRA_POWER_PWRREQ_OE;
678 reg |= TEGRA_POWER_PWRREQ_OE;
679 reg &= ~TEGRA_POWER_EFFECT_LP0;
682 case TEGRA_SUSPEND_LP0:
683 #ifdef CONFIG_ARCH_TEGRA_3x_SOC
684 rate = clk_get_rate_all_locked(tegra_pclk);
686 if (pdata->combined_req) {
687 reg |= TEGRA_POWER_PWRREQ_OE;
688 reg &= ~TEGRA_POWER_CPU_PWRREQ_OE;
692 * LP0 boots through the AVP, which then resumes the AVP to
693 * the address in scratch 39, and the cpu to the address in
694 * scratch 41 to tegra_resume
696 writel(0x0, pmc + PMC_SCRATCH39);
698 /* Enable DPD sample to trigger sampling pads data and direction
699 * in which pad will be driven during lp0 mode*/
700 writel(0x1, pmc + PMC_DPD_SAMPLE);
702 /* Set warmboot flag */
703 boot_flag = readl(pmc + PMC_SCRATCH0);
704 pmc_32kwritel(boot_flag | 1, PMC_SCRATCH0);
706 pmc_32kwritel(tegra_lp0_vec_start, PMC_SCRATCH1);
708 reg |= TEGRA_POWER_EFFECT_LP0;
709 /* No break here. LP0 code falls through to write SCRATCH41 */
710 case TEGRA_SUSPEND_LP1:
711 __raw_writel(virt_to_phys(tegra_resume), pmc + PMC_SCRATCH41);
714 case TEGRA_SUSPEND_LP2:
715 rate = clk_get_rate(tegra_pclk);
717 case TEGRA_SUSPEND_NONE:
723 set_power_timers(pdata->cpu_timer, pdata->cpu_off_timer, rate);
725 pmc_32kwritel(reg, PMC_CTRL);
728 static const char *lp_state[TEGRA_MAX_SUSPEND_MODE] = {
729 [TEGRA_SUSPEND_NONE] = "none",
730 [TEGRA_SUSPEND_LP2] = "LP2",
731 [TEGRA_SUSPEND_LP1] = "LP1",
732 [TEGRA_SUSPEND_LP0] = "LP0",
735 static int tegra_suspend_enter(suspend_state_t state)
739 struct timespec ts_entry, ts_exit;
741 if (pdata && pdata->board_suspend)
742 pdata->board_suspend(current_suspend_mode, TEGRA_SUSPEND_BEFORE_PERIPHERAL);
744 read_persistent_clock(&ts_entry);
746 ret = tegra_suspend_dram(current_suspend_mode, 0);
748 pr_info("Aborting suspend, tegra_suspend_dram error=%d\n", ret);
752 read_persistent_clock(&ts_exit);
754 if (timespec_compare(&ts_exit, &ts_entry) > 0) {
755 delta = timespec_to_ktime(timespec_sub(ts_exit, ts_entry));
757 tegra_dvfs_rail_pause(tegra_cpu_rail, delta, false);
758 if (current_suspend_mode == TEGRA_SUSPEND_LP0)
759 tegra_dvfs_rail_pause(tegra_core_rail, delta, false);
761 tegra_dvfs_rail_pause(tegra_core_rail, delta, true);
765 if (pdata && pdata->board_resume)
766 pdata->board_resume(current_suspend_mode, TEGRA_RESUME_AFTER_PERIPHERAL);
771 static void tegra_suspend_check_pwr_stats(void)
773 /* cpus and l2 are powered off later */
774 unsigned long pwrgate_partid_mask =
775 #if !defined(CONFIG_ARCH_TEGRA_2x_SOC)
776 (1 << TEGRA_POWERGATE_HEG) |
777 (1 << TEGRA_POWERGATE_SATA) |
778 (1 << TEGRA_POWERGATE_3D1) |
780 (1 << TEGRA_POWERGATE_3D) |
781 (1 << TEGRA_POWERGATE_VENC) |
782 (1 << TEGRA_POWERGATE_PCIE) |
783 (1 << TEGRA_POWERGATE_VDEC) |
784 (1 << TEGRA_POWERGATE_MPE);
788 for (partid = 0; partid < TEGRA_NUM_POWERGATE; partid++)
789 if ((1 << partid) & pwrgate_partid_mask)
790 if (tegra_powergate_is_powered(partid))
791 pr_warning("partition %s is left on before suspend\n",
792 tegra_powergate_get_name(partid));
797 int tegra_suspend_dram(enum tegra_suspend_mode mode, unsigned int flags)
800 u32 scratch37 = 0xDEADBEEF;
802 if (WARN_ON(mode <= TEGRA_SUSPEND_NONE ||
803 mode >= TEGRA_MAX_SUSPEND_MODE)) {
808 if (tegra_is_voice_call_active()) {
811 /* backup the current value of scratch37 */
812 scratch37 = readl(pmc + PMC_SCRATCH37);
814 /* If voice call is active, set a flag in PMC_SCRATCH37 */
815 reg = TEGRA_POWER_LP1_AUDIO;
816 pmc_32kwritel(reg, PMC_SCRATCH37);
819 if ((mode == TEGRA_SUSPEND_LP0) && !tegra_pm_irq_lp0_allowed()) {
820 pr_info("LP0 not used due to unsupported wakeup events\n");
821 mode = TEGRA_SUSPEND_LP1;
824 if ((mode == TEGRA_SUSPEND_LP0) || (mode == TEGRA_SUSPEND_LP1))
825 tegra_suspend_check_pwr_stats();
827 tegra_common_suspend();
831 if (pdata && pdata->board_suspend)
832 pdata->board_suspend(mode, TEGRA_SUSPEND_BEFORE_CPU);
836 trace_cpu_suspend(CPU_SUSPEND_START);
838 if (mode == TEGRA_SUSPEND_LP0) {
839 #ifdef CONFIG_TEGRA_CLUSTER_CONTROL
840 u32 reg = readl(pmc + PMC_SCRATCH4);
842 reg |= PMC_SCRATCH4_WAKE_CLUSTER_MASK;
844 reg &= (~PMC_SCRATCH4_WAKE_CLUSTER_MASK);
845 pmc_32kwritel(reg, PMC_SCRATCH4);
848 tegra_lp0_suspend_mc();
849 tegra_cpu_reset_handler_save();
850 tegra_tsc_wait_for_suspend();
852 else if (mode == TEGRA_SUSPEND_LP1)
853 *iram_cpu_lp1_mask = 1;
855 suspend_cpu_complex(flags);
861 if (mode == TEGRA_SUSPEND_LP2)
862 tegra_sleep_cpu(PHYS_OFFSET - PAGE_OFFSET);
864 tegra_sleep_core(mode, PHYS_OFFSET - PAGE_OFFSET);
866 tegra_init_cache(true);
868 if (mode == TEGRA_SUSPEND_LP0) {
870 tegra_cpu_reset_handler_restore();
871 tegra_lp0_resume_mc();
872 tegra_tsc_wait_for_resume();
873 } else if (mode == TEGRA_SUSPEND_LP1)
874 *iram_cpu_lp1_mask = 0;
876 /* if scratch37 was clobbered during LP1, restore it */
877 if (scratch37 != 0xDEADBEEF)
878 pmc_32kwritel(scratch37, PMC_SCRATCH37);
880 restore_cpu_complex(flags);
882 /* for platforms where the core & CPU power requests are
883 * combined as a single request to the PMU, transition out
884 * of LP0 state by temporarily enabling both requests
886 if (mode == TEGRA_SUSPEND_LP0 && pdata->combined_req) {
888 reg = readl(pmc + PMC_CTRL);
889 reg |= TEGRA_POWER_CPU_PWRREQ_OE;
890 pmc_32kwritel(reg, PMC_CTRL);
891 reg &= ~TEGRA_POWER_PWRREQ_OE;
892 pmc_32kwritel(reg, PMC_CTRL);
895 if (pdata && pdata->board_resume)
896 pdata->board_resume(mode, TEGRA_RESUME_AFTER_CPU);
898 trace_cpu_suspend(CPU_SUSPEND_DONE);
902 tegra_common_resume();
909 * Function pointers to optional board specific function
911 void (*tegra_deep_sleep)(int);
912 EXPORT_SYMBOL(tegra_deep_sleep);
914 static int tegra_suspend_prepare(void)
916 if ((current_suspend_mode == TEGRA_SUSPEND_LP0) && tegra_deep_sleep)
921 static void tegra_suspend_finish(void)
923 if (pdata && pdata->cpu_resume_boost) {
924 int ret = tegra_suspended_target(pdata->cpu_resume_boost);
925 pr_info("Tegra: resume CPU boost to %u KHz: %s (%d)\n",
926 pdata->cpu_resume_boost, ret ? "Failed" : "OK", ret);
929 if ((current_suspend_mode == TEGRA_SUSPEND_LP0) && tegra_deep_sleep)
933 static const struct platform_suspend_ops tegra_suspend_ops = {
934 .valid = suspend_valid_only_mem,
935 .prepare = tegra_suspend_prepare,
936 .finish = tegra_suspend_finish,
937 .prepare_late = tegra_suspend_prepare_late,
938 .wake = tegra_suspend_wake,
939 .enter = tegra_suspend_enter,
942 static ssize_t suspend_mode_show(struct kobject *kobj,
943 struct kobj_attribute *attr, char *buf)
946 char *end = buf + PAGE_SIZE;
948 start += scnprintf(start, end - start, "%s ", \
949 tegra_suspend_name[current_suspend_mode]);
950 start += scnprintf(start, end - start, "\n");
955 static ssize_t suspend_mode_store(struct kobject *kobj,
956 struct kobj_attribute *attr,
957 const char *buf, size_t n)
960 const char *name_ptr;
961 enum tegra_suspend_mode new_mode;
964 while (*name_ptr && !isspace(*name_ptr))
966 len = name_ptr - buf;
969 /* TEGRA_SUSPEND_NONE not allowed as suspend state */
970 if (!(strncmp(buf, tegra_suspend_name[TEGRA_SUSPEND_NONE], len))
971 || !(strncmp(buf, tegra_suspend_name[TEGRA_SUSPEND_LP2], len))) {
972 pr_info("Illegal tegra suspend state: %s\n", buf);
976 for (new_mode = TEGRA_SUSPEND_NONE; \
977 new_mode < TEGRA_MAX_SUSPEND_MODE; ++new_mode) {
978 if (!strncmp(buf, tegra_suspend_name[new_mode], len)) {
979 current_suspend_mode = new_mode;
988 static struct kobj_attribute suspend_mode_attribute =
989 __ATTR(mode, 0644, suspend_mode_show, suspend_mode_store);
991 static struct kobject *suspend_kobj;
993 static int tegra_pm_enter_suspend(void)
995 pr_info("Entering suspend state %s\n", lp_state[current_suspend_mode]);
996 if (current_suspend_mode == TEGRA_SUSPEND_LP0)
997 tegra_lp0_cpu_mode(true);
1001 static void tegra_pm_enter_resume(void)
1003 if (current_suspend_mode == TEGRA_SUSPEND_LP0)
1004 tegra_lp0_cpu_mode(false);
1005 pr_info("Exited suspend state %s\n", lp_state[current_suspend_mode]);
1008 static struct syscore_ops tegra_pm_enter_syscore_ops = {
1009 .suspend = tegra_pm_enter_suspend,
1010 .resume = tegra_pm_enter_resume,
1013 static __init int tegra_pm_enter_syscore_init(void)
1015 register_syscore_ops(&tegra_pm_enter_syscore_ops);
1018 subsys_initcall(tegra_pm_enter_syscore_init);
1021 void __init tegra_init_suspend(struct tegra_suspend_platform_data *plat)
1026 tegra_cpu_rail = tegra_dvfs_get_rail_by_name("vdd_cpu");
1027 tegra_core_rail = tegra_dvfs_get_rail_by_name("vdd_core");
1028 tegra_pclk = clk_get_sys(NULL, "pclk");
1029 BUG_ON(IS_ERR(tegra_pclk));
1034 if (plat->suspend_mode == TEGRA_SUSPEND_LP2)
1035 plat->suspend_mode = TEGRA_SUSPEND_LP0;
1037 #ifndef CONFIG_PM_SLEEP
1038 if (plat->suspend_mode != TEGRA_SUSPEND_NONE) {
1039 pr_warning("%s: Suspend requires CONFIG_PM_SLEEP -- "
1040 "disabling suspend\n", __func__);
1041 plat->suspend_mode = TEGRA_SUSPEND_NONE;
1044 if (create_suspend_pgtable() < 0) {
1045 pr_err("%s: PGD memory alloc failed -- LP0/LP1/LP2 unavailable\n",
1047 plat->suspend_mode = TEGRA_SUSPEND_NONE;
1051 if ((tegra_get_chipid() == TEGRA_CHIPID_TEGRA3) &&
1052 (tegra_revision == TEGRA_REVISION_A01) &&
1053 (plat->suspend_mode == TEGRA_SUSPEND_LP0)) {
1054 /* Tegra 3 A01 supports only LP1 */
1055 pr_warning("%s: Suspend mode LP0 is not supported on A01 "
1056 "-- disabling LP0\n", __func__);
1057 plat->suspend_mode = TEGRA_SUSPEND_LP1;
1059 if (plat->suspend_mode == TEGRA_SUSPEND_LP0 && tegra_lp0_vec_size &&
1060 tegra_lp0_vec_relocate) {
1061 unsigned char *reloc_lp0;
1064 reloc_lp0 = kmalloc(tegra_lp0_vec_size + L1_CACHE_BYTES - 1,
1066 WARN_ON(!reloc_lp0);
1068 pr_err("%s: Failed to allocate reloc_lp0\n",
1073 orig = ioremap(tegra_lp0_vec_start, tegra_lp0_vec_size);
1076 pr_err("%s: Failed to map tegra_lp0_vec_start %08lx\n",
1077 __func__, tegra_lp0_vec_start);
1082 tmp = (unsigned long) reloc_lp0;
1083 tmp = (tmp + L1_CACHE_BYTES - 1) & ~(L1_CACHE_BYTES - 1);
1084 reloc_lp0 = (unsigned char *)tmp;
1085 memcpy(reloc_lp0, orig, tegra_lp0_vec_size);
1087 tegra_lp0_vec_start = virt_to_phys(reloc_lp0);
1091 if (plat->suspend_mode == TEGRA_SUSPEND_LP0 && !tegra_lp0_vec_size) {
1092 pr_warning("%s: Suspend mode LP0 requested, no lp0_vec "
1093 "provided by bootlader -- disabling LP0\n",
1095 plat->suspend_mode = TEGRA_SUSPEND_LP1;
1098 iram_save_size = tegra_iram_end() - tegra_iram_start();
1100 iram_save = kmalloc(iram_save_size, GFP_KERNEL);
1101 if (!iram_save && (plat->suspend_mode >= TEGRA_SUSPEND_LP1)) {
1102 pr_err("%s: unable to allocate memory for SDRAM self-refresh "
1103 "-- LP0/LP1 unavailable\n", __func__);
1104 plat->suspend_mode = TEGRA_SUSPEND_LP2;
1107 /* !!!FIXME!!! THIS IS TEGRA2 ONLY */
1108 /* Initialize scratch registers used for CPU LP2 synchronization */
1109 writel(0, pmc + PMC_SCRATCH37);
1110 writel(0, pmc + PMC_SCRATCH38);
1111 writel(0, pmc + PMC_SCRATCH39);
1112 writel(0, pmc + PMC_SCRATCH41);
1114 /* Always enable CPU power request; just normal polarity is supported */
1115 reg = readl(pmc + PMC_CTRL);
1116 BUG_ON(reg & TEGRA_POWER_CPU_PWRREQ_POLARITY);
1117 reg |= TEGRA_POWER_CPU_PWRREQ_OE;
1118 pmc_32kwritel(reg, PMC_CTRL);
1120 /* Configure core power request and system clock control if LP0
1122 __raw_writel(pdata->core_timer, pmc + PMC_COREPWRGOOD_TIMER);
1123 __raw_writel(pdata->core_off_timer, pmc + PMC_COREPWROFF_TIMER);
1125 reg = readl(pmc + PMC_CTRL);
1127 if (!pdata->sysclkreq_high)
1128 reg |= TEGRA_POWER_SYSCLK_POLARITY;
1130 reg &= ~TEGRA_POWER_SYSCLK_POLARITY;
1132 if (!pdata->corereq_high)
1133 reg |= TEGRA_POWER_PWRREQ_POLARITY;
1135 reg &= ~TEGRA_POWER_PWRREQ_POLARITY;
1137 /* configure output inverters while the request is tristated */
1138 pmc_32kwritel(reg, PMC_CTRL);
1140 /* now enable requests */
1141 reg |= TEGRA_POWER_SYSCLK_OE;
1142 if (!pdata->combined_req)
1143 reg |= TEGRA_POWER_PWRREQ_OE;
1144 pmc_32kwritel(reg, PMC_CTRL);
1146 if (pdata->suspend_mode == TEGRA_SUSPEND_LP0)
1147 tegra_lp0_suspend_init();
1149 suspend_set_ops(&tegra_suspend_ops);
1151 /* Create /sys/power/suspend/type */
1152 suspend_kobj = kobject_create_and_add("suspend", power_kobj);
1154 if (sysfs_create_file(suspend_kobj, \
1155 &suspend_mode_attribute.attr))
1156 pr_err("%s: sysfs_create_file suspend type failed!\n",
1160 iram_cpu_lp2_mask = tegra_cpu_lp2_mask;
1161 iram_cpu_lp1_mask = tegra_cpu_lp1_mask;
1164 if (plat->suspend_mode == TEGRA_SUSPEND_NONE)
1165 tegra_lp2_in_idle(false);
1167 current_suspend_mode = plat->suspend_mode;
1170 unsigned long debug_uart_port_base = 0;
1171 EXPORT_SYMBOL(debug_uart_port_base);
1173 static int tegra_debug_uart_suspend(void)
1178 if (!debug_uart_port_base)
1181 uart = IO_ADDRESS(debug_uart_port_base);
1183 lcr = readb(uart + UART_LCR * 4);
1185 tegra_sctx.uart[0] = lcr;
1186 tegra_sctx.uart[1] = readb(uart + UART_MCR * 4);
1189 writeb(lcr & ~UART_LCR_DLAB, uart + UART_LCR * 4);
1191 tegra_sctx.uart[2] = readb(uart + UART_IER * 4);
1194 writeb(lcr | UART_LCR_DLAB, uart + UART_LCR * 4);
1196 tegra_sctx.uart[3] = readb(uart + UART_DLL * 4);
1197 tegra_sctx.uart[4] = readb(uart + UART_DLM * 4);
1199 writeb(lcr, uart + UART_LCR * 4);
1204 static void tegra_debug_uart_resume(void)
1209 if (!debug_uart_port_base)
1212 uart = IO_ADDRESS(debug_uart_port_base);
1214 lcr = tegra_sctx.uart[0];
1216 writeb(tegra_sctx.uart[1], uart + UART_MCR * 4);
1219 writeb(lcr & ~UART_LCR_DLAB, uart + UART_LCR * 4);
1221 writeb(UART_FCR_ENABLE_FIFO | UART_FCR_T_TRIG_01 | UART_FCR_R_TRIG_01,
1222 uart + UART_FCR * 4);
1224 writeb(tegra_sctx.uart[2], uart + UART_IER * 4);
1227 writeb(lcr | UART_LCR_DLAB, uart + UART_LCR * 4);
1229 writeb(tegra_sctx.uart[3], uart + UART_DLL * 4);
1230 writeb(tegra_sctx.uart[4], uart + UART_DLM * 4);
1232 writeb(lcr, uart + UART_LCR * 4);
1235 static struct syscore_ops tegra_debug_uart_syscore_ops = {
1236 .suspend = tegra_debug_uart_suspend,
1237 .resume = tegra_debug_uart_resume,
1240 struct clk *debug_uart_clk = NULL;
1241 EXPORT_SYMBOL(debug_uart_clk);
1243 void tegra_console_uart_suspend(void)
1245 if (console_suspend_enabled && debug_uart_clk)
1246 clk_disable(debug_uart_clk);
1249 void tegra_console_uart_resume(void)
1251 if (console_suspend_enabled && debug_uart_clk)
1252 clk_enable(debug_uart_clk);
1255 static int tegra_debug_uart_syscore_init(void)
1257 register_syscore_ops(&tegra_debug_uart_syscore_ops);
1260 arch_initcall(tegra_debug_uart_syscore_init);