xref: /linux/arch/arm/mach-tegra/pm.c (revision 89572c77cdffdf24f8fec50d3e38db6a18c04dbe)
1d457ef35SJoseph Lo /*
2d457ef35SJoseph Lo  * CPU complex suspend & resume functions for Tegra SoCs
3d457ef35SJoseph Lo  *
4d457ef35SJoseph Lo  * Copyright (c) 2009-2012, NVIDIA Corporation. All rights reserved.
5d457ef35SJoseph Lo  *
6d457ef35SJoseph Lo  * This program is free software; you can redistribute it and/or modify it
7d457ef35SJoseph Lo  * under the terms and conditions of the GNU General Public License,
8d457ef35SJoseph Lo  * version 2, as published by the Free Software Foundation.
9d457ef35SJoseph Lo  *
10d457ef35SJoseph Lo  * This program is distributed in the hope it will be useful, but WITHOUT
11d457ef35SJoseph Lo  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12d457ef35SJoseph Lo  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13d457ef35SJoseph Lo  * more details.
14d457ef35SJoseph Lo  *
15d457ef35SJoseph Lo  * You should have received a copy of the GNU General Public License
16d457ef35SJoseph Lo  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17d457ef35SJoseph Lo  */
18d457ef35SJoseph Lo 
19d457ef35SJoseph Lo #include <linux/kernel.h>
20d457ef35SJoseph Lo #include <linux/spinlock.h>
21d457ef35SJoseph Lo #include <linux/io.h>
22d457ef35SJoseph Lo #include <linux/cpumask.h>
23d552920aSJoseph Lo #include <linux/delay.h>
24d552920aSJoseph Lo #include <linux/cpu_pm.h>
25d552920aSJoseph Lo #include <linux/clk.h>
26d552920aSJoseph Lo #include <linux/err.h>
27*89572c77SPrashant Gaikwad #include <linux/clk/tegra.h>
28d552920aSJoseph Lo 
29d552920aSJoseph Lo #include <asm/smp_plat.h>
30d552920aSJoseph Lo #include <asm/cacheflush.h>
31d552920aSJoseph Lo #include <asm/suspend.h>
32d552920aSJoseph Lo #include <asm/idmap.h>
33d552920aSJoseph Lo #include <asm/proc-fns.h>
34d552920aSJoseph Lo #include <asm/tlbflush.h>
35d457ef35SJoseph Lo 
36d457ef35SJoseph Lo #include "iomap.h"
37d457ef35SJoseph Lo #include "reset.h"
38d552920aSJoseph Lo #include "flowctrl.h"
39d552920aSJoseph Lo #include "sleep.h"
40d552920aSJoseph Lo 
41d552920aSJoseph Lo #define TEGRA_POWER_CPU_PWRREQ_OE	(1 << 16)  /* CPU pwr req enable */
42d552920aSJoseph Lo 
43d552920aSJoseph Lo #define PMC_CTRL		0x0
44d552920aSJoseph Lo #define PMC_CPUPWRGOOD_TIMER	0xc8
45d552920aSJoseph Lo #define PMC_CPUPWROFF_TIMER	0xcc
46d457ef35SJoseph Lo 
47d457ef35SJoseph Lo #ifdef CONFIG_PM_SLEEP
48d457ef35SJoseph Lo static unsigned int g_diag_reg;
49d457ef35SJoseph Lo static DEFINE_SPINLOCK(tegra_lp2_lock);
50d552920aSJoseph Lo static void __iomem *pmc = IO_ADDRESS(TEGRA_PMC_BASE);
51d552920aSJoseph Lo static struct clk *tegra_pclk;
52d552920aSJoseph Lo void (*tegra_tear_down_cpu)(void);
53d457ef35SJoseph Lo 
54d457ef35SJoseph Lo void save_cpu_arch_register(void)
55d457ef35SJoseph Lo {
56d457ef35SJoseph Lo 	/* read diagnostic register */
57d457ef35SJoseph Lo 	asm("mrc p15, 0, %0, c15, c0, 1" : "=r"(g_diag_reg) : : "cc");
58d457ef35SJoseph Lo 	return;
59d457ef35SJoseph Lo }
60d457ef35SJoseph Lo 
61d457ef35SJoseph Lo void restore_cpu_arch_register(void)
62d457ef35SJoseph Lo {
63d457ef35SJoseph Lo 	/* write diagnostic register */
64d457ef35SJoseph Lo 	asm("mcr p15, 0, %0, c15, c0, 1" : : "r"(g_diag_reg) : "cc");
65d457ef35SJoseph Lo 	return;
66d457ef35SJoseph Lo }
67d457ef35SJoseph Lo 
68d552920aSJoseph Lo static void set_power_timers(unsigned long us_on, unsigned long us_off)
69d552920aSJoseph Lo {
70d552920aSJoseph Lo 	unsigned long long ticks;
71d552920aSJoseph Lo 	unsigned long long pclk;
72d552920aSJoseph Lo 	unsigned long rate;
73d552920aSJoseph Lo 	static unsigned long tegra_last_pclk;
74d552920aSJoseph Lo 
75d552920aSJoseph Lo 	if (tegra_pclk == NULL) {
76d552920aSJoseph Lo 		tegra_pclk = clk_get_sys(NULL, "pclk");
77d552920aSJoseph Lo 		WARN_ON(IS_ERR(tegra_pclk));
78d552920aSJoseph Lo 	}
79d552920aSJoseph Lo 
80d552920aSJoseph Lo 	rate = clk_get_rate(tegra_pclk);
81d552920aSJoseph Lo 
82d552920aSJoseph Lo 	if (WARN_ON_ONCE(rate <= 0))
83d552920aSJoseph Lo 		pclk = 100000000;
84d552920aSJoseph Lo 	else
85d552920aSJoseph Lo 		pclk = rate;
86d552920aSJoseph Lo 
87d552920aSJoseph Lo 	if ((rate != tegra_last_pclk)) {
88d552920aSJoseph Lo 		ticks = (us_on * pclk) + 999999ull;
89d552920aSJoseph Lo 		do_div(ticks, 1000000);
90d552920aSJoseph Lo 		writel((unsigned long)ticks, pmc + PMC_CPUPWRGOOD_TIMER);
91d552920aSJoseph Lo 
92d552920aSJoseph Lo 		ticks = (us_off * pclk) + 999999ull;
93d552920aSJoseph Lo 		do_div(ticks, 1000000);
94d552920aSJoseph Lo 		writel((unsigned long)ticks, pmc + PMC_CPUPWROFF_TIMER);
95d552920aSJoseph Lo 		wmb();
96d552920aSJoseph Lo 	}
97d552920aSJoseph Lo 	tegra_last_pclk = pclk;
98d552920aSJoseph Lo }
99d552920aSJoseph Lo 
100d552920aSJoseph Lo /*
101d552920aSJoseph Lo  * restore_cpu_complex
102d552920aSJoseph Lo  *
103d552920aSJoseph Lo  * restores cpu clock setting, clears flow controller
104d552920aSJoseph Lo  *
105d552920aSJoseph Lo  * Always called on CPU 0.
106d552920aSJoseph Lo  */
107d552920aSJoseph Lo static void restore_cpu_complex(void)
108d552920aSJoseph Lo {
109d552920aSJoseph Lo 	int cpu = smp_processor_id();
110d552920aSJoseph Lo 
111d552920aSJoseph Lo 	BUG_ON(cpu != 0);
112d552920aSJoseph Lo 
113d552920aSJoseph Lo #ifdef CONFIG_SMP
114d552920aSJoseph Lo 	cpu = cpu_logical_map(cpu);
115d552920aSJoseph Lo #endif
116d552920aSJoseph Lo 
117d552920aSJoseph Lo 	/* Restore the CPU clock settings */
118d552920aSJoseph Lo 	tegra_cpu_clock_resume();
119d552920aSJoseph Lo 
120d552920aSJoseph Lo 	flowctrl_cpu_suspend_exit(cpu);
121d552920aSJoseph Lo 
122d552920aSJoseph Lo 	restore_cpu_arch_register();
123d552920aSJoseph Lo }
124d552920aSJoseph Lo 
125d552920aSJoseph Lo /*
126d552920aSJoseph Lo  * suspend_cpu_complex
127d552920aSJoseph Lo  *
128d552920aSJoseph Lo  * saves pll state for use by restart_plls, prepares flow controller for
129d552920aSJoseph Lo  * transition to suspend state
130d552920aSJoseph Lo  *
131d552920aSJoseph Lo  * Must always be called on cpu 0.
132d552920aSJoseph Lo  */
133d552920aSJoseph Lo static void suspend_cpu_complex(void)
134d552920aSJoseph Lo {
135d552920aSJoseph Lo 	int cpu = smp_processor_id();
136d552920aSJoseph Lo 
137d552920aSJoseph Lo 	BUG_ON(cpu != 0);
138d552920aSJoseph Lo 
139d552920aSJoseph Lo #ifdef CONFIG_SMP
140d552920aSJoseph Lo 	cpu = cpu_logical_map(cpu);
141d552920aSJoseph Lo #endif
142d552920aSJoseph Lo 
143d552920aSJoseph Lo 	/* Save the CPU clock settings */
144d552920aSJoseph Lo 	tegra_cpu_clock_suspend();
145d552920aSJoseph Lo 
146d552920aSJoseph Lo 	flowctrl_cpu_suspend_enter(cpu);
147d552920aSJoseph Lo 
148d552920aSJoseph Lo 	save_cpu_arch_register();
149d552920aSJoseph Lo }
150d552920aSJoseph Lo 
1518c627fa6SJoseph Lo void tegra_clear_cpu_in_lp2(int phy_cpu_id)
152d457ef35SJoseph Lo {
153d457ef35SJoseph Lo 	u32 *cpu_in_lp2 = tegra_cpu_lp2_mask;
154d457ef35SJoseph Lo 
155d457ef35SJoseph Lo 	spin_lock(&tegra_lp2_lock);
156d457ef35SJoseph Lo 
157d457ef35SJoseph Lo 	BUG_ON(!(*cpu_in_lp2 & BIT(phy_cpu_id)));
158d457ef35SJoseph Lo 	*cpu_in_lp2 &= ~BIT(phy_cpu_id);
159d457ef35SJoseph Lo 
160d457ef35SJoseph Lo 	spin_unlock(&tegra_lp2_lock);
161d457ef35SJoseph Lo }
162d457ef35SJoseph Lo 
1638c627fa6SJoseph Lo bool tegra_set_cpu_in_lp2(int phy_cpu_id)
164d457ef35SJoseph Lo {
165d457ef35SJoseph Lo 	bool last_cpu = false;
166d457ef35SJoseph Lo 	cpumask_t *cpu_lp2_mask = tegra_cpu_lp2_mask;
167d457ef35SJoseph Lo 	u32 *cpu_in_lp2 = tegra_cpu_lp2_mask;
168d457ef35SJoseph Lo 
169d457ef35SJoseph Lo 	spin_lock(&tegra_lp2_lock);
170d457ef35SJoseph Lo 
171d457ef35SJoseph Lo 	BUG_ON((*cpu_in_lp2 & BIT(phy_cpu_id)));
172d457ef35SJoseph Lo 	*cpu_in_lp2 |= BIT(phy_cpu_id);
173d457ef35SJoseph Lo 
174d457ef35SJoseph Lo 	if ((phy_cpu_id == 0) && cpumask_equal(cpu_lp2_mask, cpu_online_mask))
175d457ef35SJoseph Lo 		last_cpu = true;
176d457ef35SJoseph Lo 
177d457ef35SJoseph Lo 	spin_unlock(&tegra_lp2_lock);
178d457ef35SJoseph Lo 	return last_cpu;
179d457ef35SJoseph Lo }
180d552920aSJoseph Lo 
181d552920aSJoseph Lo static int tegra_sleep_cpu(unsigned long v2p)
182d552920aSJoseph Lo {
183d552920aSJoseph Lo 	/* Switch to the identity mapping. */
184d552920aSJoseph Lo 	cpu_switch_mm(idmap_pgd, &init_mm);
185d552920aSJoseph Lo 
186d552920aSJoseph Lo 	/* Flush the TLB. */
187d552920aSJoseph Lo 	local_flush_tlb_all();
188d552920aSJoseph Lo 
189d552920aSJoseph Lo 	tegra_sleep_cpu_finish(v2p);
190d552920aSJoseph Lo 
191d552920aSJoseph Lo 	/* should never here */
192d552920aSJoseph Lo 	BUG();
193d552920aSJoseph Lo 
194d552920aSJoseph Lo 	return 0;
195d552920aSJoseph Lo }
196d552920aSJoseph Lo 
197d552920aSJoseph Lo void tegra_idle_lp2_last(u32 cpu_on_time, u32 cpu_off_time)
198d552920aSJoseph Lo {
199d552920aSJoseph Lo 	u32 mode;
200d552920aSJoseph Lo 
201d552920aSJoseph Lo 	/* Only the last cpu down does the final suspend steps */
202d552920aSJoseph Lo 	mode = readl(pmc + PMC_CTRL);
203d552920aSJoseph Lo 	mode |= TEGRA_POWER_CPU_PWRREQ_OE;
204d552920aSJoseph Lo 	writel(mode, pmc + PMC_CTRL);
205d552920aSJoseph Lo 
206d552920aSJoseph Lo 	set_power_timers(cpu_on_time, cpu_off_time);
207d552920aSJoseph Lo 
208d552920aSJoseph Lo 	cpu_cluster_pm_enter();
209d552920aSJoseph Lo 	suspend_cpu_complex();
210d552920aSJoseph Lo 
211d552920aSJoseph Lo 	cpu_suspend(PHYS_OFFSET - PAGE_OFFSET, &tegra_sleep_cpu);
212d552920aSJoseph Lo 
213d552920aSJoseph Lo 	restore_cpu_complex();
214d552920aSJoseph Lo 	cpu_cluster_pm_exit();
215d552920aSJoseph Lo }
216d457ef35SJoseph Lo #endif
217