Merge tag 'tegra-for-3.10-soc' of git://git.kernel.org/pub/scm/linux/kernel/git/swarr...
[firefly-linux-kernel-4.4.55.git] / arch / arm / mach-tegra / pm.c
1 /*
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>
23 #include <linux/delay.h>
24 #include <linux/cpu_pm.h>
25 #include <linux/suspend.h>
26 #include <linux/err.h>
27 #include <linux/clk/tegra.h>
28
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>
35
36 #include "iomap.h"
37 #include "reset.h"
38 #include "flowctrl.h"
39 #include "fuse.h"
40 #include "pmc.h"
41 #include "sleep.h"
42
43 #ifdef CONFIG_PM_SLEEP
44 static DEFINE_SPINLOCK(tegra_lp2_lock);
45 void (*tegra_tear_down_cpu)(void);
46
47 /*
48  * restore_cpu_complex
49  *
50  * restores cpu clock setting, clears flow controller
51  *
52  * Always called on CPU 0.
53  */
54 static void restore_cpu_complex(void)
55 {
56         int cpu = smp_processor_id();
57
58         BUG_ON(cpu != 0);
59
60 #ifdef CONFIG_SMP
61         cpu = cpu_logical_map(cpu);
62 #endif
63
64         /* Restore the CPU clock settings */
65         tegra_cpu_clock_resume();
66
67         flowctrl_cpu_suspend_exit(cpu);
68 }
69
70 /*
71  * suspend_cpu_complex
72  *
73  * saves pll state for use by restart_plls, prepares flow controller for
74  * transition to suspend state
75  *
76  * Must always be called on cpu 0.
77  */
78 static void suspend_cpu_complex(void)
79 {
80         int cpu = smp_processor_id();
81
82         BUG_ON(cpu != 0);
83
84 #ifdef CONFIG_SMP
85         cpu = cpu_logical_map(cpu);
86 #endif
87
88         /* Save the CPU clock settings */
89         tegra_cpu_clock_suspend();
90
91         flowctrl_cpu_suspend_enter(cpu);
92 }
93
94 void tegra_clear_cpu_in_lp2(int phy_cpu_id)
95 {
96         u32 *cpu_in_lp2 = tegra_cpu_lp2_mask;
97
98         spin_lock(&tegra_lp2_lock);
99
100         BUG_ON(!(*cpu_in_lp2 & BIT(phy_cpu_id)));
101         *cpu_in_lp2 &= ~BIT(phy_cpu_id);
102
103         spin_unlock(&tegra_lp2_lock);
104 }
105
106 bool tegra_set_cpu_in_lp2(int phy_cpu_id)
107 {
108         bool last_cpu = false;
109         cpumask_t *cpu_lp2_mask = tegra_cpu_lp2_mask;
110         u32 *cpu_in_lp2 = tegra_cpu_lp2_mask;
111
112         spin_lock(&tegra_lp2_lock);
113
114         BUG_ON((*cpu_in_lp2 & BIT(phy_cpu_id)));
115         *cpu_in_lp2 |= BIT(phy_cpu_id);
116
117         if ((phy_cpu_id == 0) && cpumask_equal(cpu_lp2_mask, cpu_online_mask))
118                 last_cpu = true;
119         else if (tegra_chip_id == TEGRA20 && phy_cpu_id == 1)
120                 tegra20_cpu_set_resettable_soon();
121
122         spin_unlock(&tegra_lp2_lock);
123         return last_cpu;
124 }
125
126 static int tegra_sleep_cpu(unsigned long v2p)
127 {
128         setup_mm_for_reboot();
129         tegra_sleep_cpu_finish(v2p);
130
131         /* should never here */
132         BUG();
133
134         return 0;
135 }
136
137 void tegra_idle_lp2_last(void)
138 {
139         tegra_pmc_pm_set(TEGRA_SUSPEND_LP2);
140
141         cpu_cluster_pm_enter();
142         suspend_cpu_complex();
143
144         cpu_suspend(PHYS_OFFSET - PAGE_OFFSET, &tegra_sleep_cpu);
145
146         restore_cpu_complex();
147         cpu_cluster_pm_exit();
148 }
149
150 enum tegra_suspend_mode tegra_pm_validate_suspend_mode(
151                                 enum tegra_suspend_mode mode)
152 {
153         /* Tegra114 didn't support any suspending mode yet. */
154         if (tegra_chip_id == TEGRA114)
155                 return TEGRA_SUSPEND_NONE;
156
157         /*
158          * The Tegra devices only support suspending to LP2 currently.
159          */
160         if (mode > TEGRA_SUSPEND_LP2)
161                 return TEGRA_SUSPEND_LP2;
162
163         return mode;
164 }
165
166 static const char *lp_state[TEGRA_MAX_SUSPEND_MODE] = {
167         [TEGRA_SUSPEND_NONE] = "none",
168         [TEGRA_SUSPEND_LP2] = "LP2",
169         [TEGRA_SUSPEND_LP1] = "LP1",
170         [TEGRA_SUSPEND_LP0] = "LP0",
171 };
172
173 static int __cpuinit tegra_suspend_enter(suspend_state_t state)
174 {
175         enum tegra_suspend_mode mode = tegra_pmc_get_suspend_mode();
176
177         if (WARN_ON(mode < TEGRA_SUSPEND_NONE ||
178                     mode >= TEGRA_MAX_SUSPEND_MODE))
179                 return -EINVAL;
180
181         pr_info("Entering suspend state %s\n", lp_state[mode]);
182
183         tegra_pmc_pm_set(mode);
184
185         local_fiq_disable();
186
187         suspend_cpu_complex();
188         switch (mode) {
189         case TEGRA_SUSPEND_LP2:
190                 tegra_set_cpu_in_lp2(0);
191                 break;
192         default:
193                 break;
194         }
195
196         cpu_suspend(PHYS_OFFSET - PAGE_OFFSET, &tegra_sleep_cpu);
197
198         switch (mode) {
199         case TEGRA_SUSPEND_LP2:
200                 tegra_clear_cpu_in_lp2(0);
201                 break;
202         default:
203                 break;
204         }
205         restore_cpu_complex();
206
207         local_fiq_enable();
208
209         return 0;
210 }
211
212 static const struct platform_suspend_ops tegra_suspend_ops = {
213         .valid          = suspend_valid_only_mem,
214         .enter          = tegra_suspend_enter,
215 };
216
217 void __init tegra_init_suspend(void)
218 {
219         if (tegra_pmc_get_suspend_mode() == TEGRA_SUSPEND_NONE)
220                 return;
221
222         tegra_pmc_suspend_init();
223
224         suspend_set_ops(&tegra_suspend_ops);
225 }
226 #endif