x86/efi: Enforce CONFIG_RELOCATABLE for EFI boot stub
[linux/fpc-iii.git] / arch / arm / mach-tegra / pmc.c
blob8acb881f7cfe5c8025f4c133cb5a1a60d2ec9181
1 /*
2 * Copyright (C) 2012,2013 NVIDIA CORPORATION. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include <linux/kernel.h>
19 #include <linux/clk.h>
20 #include <linux/io.h>
21 #include <linux/of.h>
22 #include <linux/of_address.h>
24 #include "flowctrl.h"
25 #include "fuse.h"
26 #include "pm.h"
27 #include "pmc.h"
28 #include "sleep.h"
30 #define TEGRA_POWER_SYSCLK_POLARITY (1 << 10) /* sys clk polarity */
31 #define TEGRA_POWER_SYSCLK_OE (1 << 11) /* system clock enable */
32 #define TEGRA_POWER_EFFECT_LP0 (1 << 14) /* LP0 when CPU pwr gated */
33 #define TEGRA_POWER_CPU_PWRREQ_POLARITY (1 << 15) /* CPU pwr req polarity */
34 #define TEGRA_POWER_CPU_PWRREQ_OE (1 << 16) /* CPU pwr req enable */
36 #define PMC_CTRL 0x0
37 #define PMC_CTRL_INTR_LOW (1 << 17)
38 #define PMC_PWRGATE_TOGGLE 0x30
39 #define PMC_PWRGATE_TOGGLE_START (1 << 8)
40 #define PMC_REMOVE_CLAMPING 0x34
41 #define PMC_PWRGATE_STATUS 0x38
43 #define PMC_CPUPWRGOOD_TIMER 0xc8
44 #define PMC_CPUPWROFF_TIMER 0xcc
46 #define TEGRA_POWERGATE_PCIE 3
47 #define TEGRA_POWERGATE_VDEC 4
48 #define TEGRA_POWERGATE_CPU1 9
49 #define TEGRA_POWERGATE_CPU2 10
50 #define TEGRA_POWERGATE_CPU3 11
52 static u8 tegra_cpu_domains[] = {
53 0xFF, /* not available for CPU0 */
54 TEGRA_POWERGATE_CPU1,
55 TEGRA_POWERGATE_CPU2,
56 TEGRA_POWERGATE_CPU3,
58 static DEFINE_SPINLOCK(tegra_powergate_lock);
60 static void __iomem *tegra_pmc_base;
61 static bool tegra_pmc_invert_interrupt;
62 static struct clk *tegra_pclk;
64 struct pmc_pm_data {
65 u32 cpu_good_time; /* CPU power good time in uS */
66 u32 cpu_off_time; /* CPU power off time in uS */
67 u32 core_osc_time; /* Core power good osc time in uS */
68 u32 core_pmu_time; /* Core power good pmu time in uS */
69 u32 core_off_time; /* Core power off time in uS */
70 bool corereq_high; /* Core power request active-high */
71 bool sysclkreq_high; /* System clock request active-high */
72 bool combined_req; /* Combined pwr req for CPU & Core */
73 bool cpu_pwr_good_en; /* CPU power good signal is enabled */
74 u32 lp0_vec_phy_addr; /* The phy addr of LP0 warm boot code */
75 u32 lp0_vec_size; /* The size of LP0 warm boot code */
76 enum tegra_suspend_mode suspend_mode;
78 static struct pmc_pm_data pmc_pm_data;
80 static inline u32 tegra_pmc_readl(u32 reg)
82 return readl(tegra_pmc_base + reg);
85 static inline void tegra_pmc_writel(u32 val, u32 reg)
87 writel(val, tegra_pmc_base + reg);
90 static int tegra_pmc_get_cpu_powerdomain_id(int cpuid)
92 if (cpuid <= 0 || cpuid >= num_possible_cpus())
93 return -EINVAL;
94 return tegra_cpu_domains[cpuid];
97 static bool tegra_pmc_powergate_is_powered(int id)
99 return (tegra_pmc_readl(PMC_PWRGATE_STATUS) >> id) & 1;
102 static int tegra_pmc_powergate_set(int id, bool new_state)
104 bool old_state;
105 unsigned long flags;
107 spin_lock_irqsave(&tegra_powergate_lock, flags);
109 old_state = tegra_pmc_powergate_is_powered(id);
110 WARN_ON(old_state == new_state);
112 tegra_pmc_writel(PMC_PWRGATE_TOGGLE_START | id, PMC_PWRGATE_TOGGLE);
114 spin_unlock_irqrestore(&tegra_powergate_lock, flags);
116 return 0;
119 static int tegra_pmc_powergate_remove_clamping(int id)
121 u32 mask;
124 * Tegra has a bug where PCIE and VDE clamping masks are
125 * swapped relatively to the partition ids.
127 if (id == TEGRA_POWERGATE_VDEC)
128 mask = (1 << TEGRA_POWERGATE_PCIE);
129 else if (id == TEGRA_POWERGATE_PCIE)
130 mask = (1 << TEGRA_POWERGATE_VDEC);
131 else
132 mask = (1 << id);
134 tegra_pmc_writel(mask, PMC_REMOVE_CLAMPING);
136 return 0;
139 bool tegra_pmc_cpu_is_powered(int cpuid)
141 int id;
143 id = tegra_pmc_get_cpu_powerdomain_id(cpuid);
144 if (id < 0)
145 return false;
146 return tegra_pmc_powergate_is_powered(id);
149 int tegra_pmc_cpu_power_on(int cpuid)
151 int id;
153 id = tegra_pmc_get_cpu_powerdomain_id(cpuid);
154 if (id < 0)
155 return id;
156 return tegra_pmc_powergate_set(id, true);
159 int tegra_pmc_cpu_remove_clamping(int cpuid)
161 int id;
163 id = tegra_pmc_get_cpu_powerdomain_id(cpuid);
164 if (id < 0)
165 return id;
166 return tegra_pmc_powergate_remove_clamping(id);
169 #ifdef CONFIG_PM_SLEEP
170 static void set_power_timers(u32 us_on, u32 us_off, unsigned long rate)
172 unsigned long long ticks;
173 unsigned long long pclk;
174 static unsigned long tegra_last_pclk;
176 if (WARN_ON_ONCE(rate <= 0))
177 pclk = 100000000;
178 else
179 pclk = rate;
181 if ((rate != tegra_last_pclk)) {
182 ticks = (us_on * pclk) + 999999ull;
183 do_div(ticks, 1000000);
184 tegra_pmc_writel((unsigned long)ticks, PMC_CPUPWRGOOD_TIMER);
186 ticks = (us_off * pclk) + 999999ull;
187 do_div(ticks, 1000000);
188 tegra_pmc_writel((unsigned long)ticks, PMC_CPUPWROFF_TIMER);
189 wmb();
191 tegra_last_pclk = pclk;
194 enum tegra_suspend_mode tegra_pmc_get_suspend_mode(void)
196 return pmc_pm_data.suspend_mode;
199 void tegra_pmc_set_suspend_mode(enum tegra_suspend_mode mode)
201 if (mode < TEGRA_SUSPEND_NONE || mode >= TEGRA_MAX_SUSPEND_MODE)
202 return;
204 pmc_pm_data.suspend_mode = mode;
207 void tegra_pmc_suspend(void)
209 tegra_pmc_writel(virt_to_phys(tegra_resume), PMC_SCRATCH41);
212 void tegra_pmc_resume(void)
214 tegra_pmc_writel(0x0, PMC_SCRATCH41);
217 void tegra_pmc_pm_set(enum tegra_suspend_mode mode)
219 u32 reg, csr_reg;
220 unsigned long rate = 0;
222 reg = tegra_pmc_readl(PMC_CTRL);
223 reg |= TEGRA_POWER_CPU_PWRREQ_OE;
224 reg &= ~TEGRA_POWER_EFFECT_LP0;
226 switch (tegra_chip_id) {
227 case TEGRA20:
228 case TEGRA30:
229 break;
230 default:
231 /* Turn off CRAIL */
232 csr_reg = flowctrl_read_cpu_csr(0);
233 csr_reg &= ~FLOW_CTRL_CSR_ENABLE_EXT_MASK;
234 csr_reg |= FLOW_CTRL_CSR_ENABLE_EXT_CRAIL;
235 flowctrl_write_cpu_csr(0, csr_reg);
236 break;
239 switch (mode) {
240 case TEGRA_SUSPEND_LP1:
241 rate = 32768;
242 break;
243 case TEGRA_SUSPEND_LP2:
244 rate = clk_get_rate(tegra_pclk);
245 break;
246 default:
247 break;
250 set_power_timers(pmc_pm_data.cpu_good_time, pmc_pm_data.cpu_off_time,
251 rate);
253 tegra_pmc_writel(reg, PMC_CTRL);
256 void tegra_pmc_suspend_init(void)
258 u32 reg;
260 /* Always enable CPU power request */
261 reg = tegra_pmc_readl(PMC_CTRL);
262 reg |= TEGRA_POWER_CPU_PWRREQ_OE;
263 tegra_pmc_writel(reg, PMC_CTRL);
265 reg = tegra_pmc_readl(PMC_CTRL);
267 if (!pmc_pm_data.sysclkreq_high)
268 reg |= TEGRA_POWER_SYSCLK_POLARITY;
269 else
270 reg &= ~TEGRA_POWER_SYSCLK_POLARITY;
272 /* configure the output polarity while the request is tristated */
273 tegra_pmc_writel(reg, PMC_CTRL);
275 /* now enable the request */
276 reg |= TEGRA_POWER_SYSCLK_OE;
277 tegra_pmc_writel(reg, PMC_CTRL);
279 #endif
281 static const struct of_device_id matches[] __initconst = {
282 { .compatible = "nvidia,tegra114-pmc" },
283 { .compatible = "nvidia,tegra30-pmc" },
284 { .compatible = "nvidia,tegra20-pmc" },
288 static void __init tegra_pmc_parse_dt(void)
290 struct device_node *np;
291 u32 prop;
292 enum tegra_suspend_mode suspend_mode;
293 u32 core_good_time[2] = {0, 0};
294 u32 lp0_vec[2] = {0, 0};
296 np = of_find_matching_node(NULL, matches);
297 BUG_ON(!np);
299 tegra_pmc_base = of_iomap(np, 0);
301 tegra_pmc_invert_interrupt = of_property_read_bool(np,
302 "nvidia,invert-interrupt");
303 tegra_pclk = of_clk_get_by_name(np, "pclk");
304 WARN_ON(IS_ERR(tegra_pclk));
306 /* Grabbing the power management configurations */
307 if (of_property_read_u32(np, "nvidia,suspend-mode", &prop)) {
308 suspend_mode = TEGRA_SUSPEND_NONE;
309 } else {
310 switch (prop) {
311 case 0:
312 suspend_mode = TEGRA_SUSPEND_LP0;
313 break;
314 case 1:
315 suspend_mode = TEGRA_SUSPEND_LP1;
316 break;
317 case 2:
318 suspend_mode = TEGRA_SUSPEND_LP2;
319 break;
320 default:
321 suspend_mode = TEGRA_SUSPEND_NONE;
322 break;
325 suspend_mode = tegra_pm_validate_suspend_mode(suspend_mode);
327 if (of_property_read_u32(np, "nvidia,cpu-pwr-good-time", &prop))
328 suspend_mode = TEGRA_SUSPEND_NONE;
329 pmc_pm_data.cpu_good_time = prop;
331 if (of_property_read_u32(np, "nvidia,cpu-pwr-off-time", &prop))
332 suspend_mode = TEGRA_SUSPEND_NONE;
333 pmc_pm_data.cpu_off_time = prop;
335 if (of_property_read_u32_array(np, "nvidia,core-pwr-good-time",
336 core_good_time, ARRAY_SIZE(core_good_time)))
337 suspend_mode = TEGRA_SUSPEND_NONE;
338 pmc_pm_data.core_osc_time = core_good_time[0];
339 pmc_pm_data.core_pmu_time = core_good_time[1];
341 if (of_property_read_u32(np, "nvidia,core-pwr-off-time",
342 &prop))
343 suspend_mode = TEGRA_SUSPEND_NONE;
344 pmc_pm_data.core_off_time = prop;
346 pmc_pm_data.corereq_high = of_property_read_bool(np,
347 "nvidia,core-power-req-active-high");
349 pmc_pm_data.sysclkreq_high = of_property_read_bool(np,
350 "nvidia,sys-clock-req-active-high");
352 pmc_pm_data.combined_req = of_property_read_bool(np,
353 "nvidia,combined-power-req");
355 pmc_pm_data.cpu_pwr_good_en = of_property_read_bool(np,
356 "nvidia,cpu-pwr-good-en");
358 if (of_property_read_u32_array(np, "nvidia,lp0-vec", lp0_vec,
359 ARRAY_SIZE(lp0_vec)))
360 if (suspend_mode == TEGRA_SUSPEND_LP0)
361 suspend_mode = TEGRA_SUSPEND_LP1;
363 pmc_pm_data.lp0_vec_phy_addr = lp0_vec[0];
364 pmc_pm_data.lp0_vec_size = lp0_vec[1];
366 pmc_pm_data.suspend_mode = suspend_mode;
369 void __init tegra_pmc_init(void)
371 u32 val;
373 tegra_pmc_parse_dt();
375 val = tegra_pmc_readl(PMC_CTRL);
376 if (tegra_pmc_invert_interrupt)
377 val |= PMC_CTRL_INTR_LOW;
378 else
379 val &= ~PMC_CTRL_INTR_LOW;
380 tegra_pmc_writel(val, PMC_CTRL);