blob: 69df18090c8b308ba99f1b570c0bd9147661d9f7 [file] [log] [blame]
Stephen Warrend3b8bdd2012-01-25 14:43:28 -07001/*
Joseph Lo291fde32013-02-28 21:32:10 +00002 * Copyright (C) 2012,2013 NVIDIA CORPORATION. All rights reserved.
Stephen Warrend3b8bdd2012-01-25 14:43:28 -07003 *
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.
7 *
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.
12 *
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/>.
15 *
16 */
17
Joseph Lo0337c3e2013-04-03 19:31:28 +080018#include <linux/clk.h>
Stephen Warrend3b8bdd2012-01-25 14:43:28 -070019#include <linux/io.h>
Thierry Redinga0524ac2014-07-11 09:44:49 +020020#include <linux/kernel.h>
Stephen Warrend3b8bdd2012-01-25 14:43:28 -070021#include <linux/of.h>
Joseph Lo291fde32013-02-28 21:32:10 +000022#include <linux/of_address.h>
Thierry Reding306a7f92014-07-17 13:17:24 +020023
Thierry Reding304664e2014-07-11 09:52:41 +020024#include <soc/tegra/fuse.h>
Thierry Reding306a7f92014-07-17 13:17:24 +020025#include <soc/tegra/powergate.h>
Stephen Warrend3b8bdd2012-01-25 14:43:28 -070026
Joseph Lo3f1be812013-07-03 17:50:41 +080027#include "flowctrl.h"
Joseph Loc8c2e602013-04-03 19:31:47 +080028#include "pm.h"
Joseph Lo4b51ccb2013-04-03 19:31:46 +080029#include "pmc.h"
Joseph Loc8c2e602013-04-03 19:31:47 +080030#include "sleep.h"
31
Joseph Lo444f9a802013-08-12 17:40:01 +080032#define TEGRA_POWER_SYSCLK_POLARITY (1 << 10) /* sys clk polarity */
33#define TEGRA_POWER_SYSCLK_OE (1 << 11) /* system clock enable */
Joseph Loc8c2e602013-04-03 19:31:47 +080034#define TEGRA_POWER_EFFECT_LP0 (1 << 14) /* LP0 when CPU pwr gated */
35#define TEGRA_POWER_CPU_PWRREQ_POLARITY (1 << 15) /* CPU pwr req polarity */
36#define TEGRA_POWER_CPU_PWRREQ_OE (1 << 16) /* CPU pwr req enable */
Joseph Lo4b51ccb2013-04-03 19:31:46 +080037
Joseph Loc1417532013-02-28 21:32:11 +000038#define PMC_CTRL 0x0
39#define PMC_CTRL_INTR_LOW (1 << 17)
40#define PMC_PWRGATE_TOGGLE 0x30
41#define PMC_PWRGATE_TOGGLE_START (1 << 8)
42#define PMC_REMOVE_CLAMPING 0x34
43#define PMC_PWRGATE_STATUS 0x38
44
Thierry Reding498bb3d2014-05-06 17:04:11 +020045#define PMC_SCRATCH0 0x50
46#define PMC_SCRATCH0_MODE_RECOVERY (1 << 31)
47#define PMC_SCRATCH0_MODE_BOOTLOADER (1 << 30)
48#define PMC_SCRATCH0_MODE_RCM (1 << 1)
49#define PMC_SCRATCH0_MODE_MASK (PMC_SCRATCH0_MODE_RECOVERY | \
50 PMC_SCRATCH0_MODE_BOOTLOADER | \
51 PMC_SCRATCH0_MODE_RCM)
52
Joseph Lo0337c3e2013-04-03 19:31:28 +080053#define PMC_CPUPWRGOOD_TIMER 0xc8
54#define PMC_CPUPWROFF_TIMER 0xcc
55
Joseph Loc1417532013-02-28 21:32:11 +000056static u8 tegra_cpu_domains[] = {
57 0xFF, /* not available for CPU0 */
58 TEGRA_POWERGATE_CPU1,
59 TEGRA_POWERGATE_CPU2,
60 TEGRA_POWERGATE_CPU3,
61};
62static DEFINE_SPINLOCK(tegra_powergate_lock);
Stephen Warrend3b8bdd2012-01-25 14:43:28 -070063
Joseph Lo291fde32013-02-28 21:32:10 +000064static void __iomem *tegra_pmc_base;
65static bool tegra_pmc_invert_interrupt;
Joseph Lo0337c3e2013-04-03 19:31:28 +080066static struct clk *tegra_pclk;
Joseph Lo291fde32013-02-28 21:32:10 +000067
Joseph Lo4b51ccb2013-04-03 19:31:46 +080068struct pmc_pm_data {
69 u32 cpu_good_time; /* CPU power good time in uS */
70 u32 cpu_off_time; /* CPU power off time in uS */
71 u32 core_osc_time; /* Core power good osc time in uS */
72 u32 core_pmu_time; /* Core power good pmu time in uS */
73 u32 core_off_time; /* Core power off time in uS */
74 bool corereq_high; /* Core power request active-high */
75 bool sysclkreq_high; /* System clock request active-high */
76 bool combined_req; /* Combined pwr req for CPU & Core */
77 bool cpu_pwr_good_en; /* CPU power good signal is enabled */
78 u32 lp0_vec_phy_addr; /* The phy addr of LP0 warm boot code */
79 u32 lp0_vec_size; /* The size of LP0 warm boot code */
80 enum tegra_suspend_mode suspend_mode;
81};
82static struct pmc_pm_data pmc_pm_data;
83
Stephen Warrend3b8bdd2012-01-25 14:43:28 -070084static inline u32 tegra_pmc_readl(u32 reg)
85{
Joseph Lo291fde32013-02-28 21:32:10 +000086 return readl(tegra_pmc_base + reg);
Stephen Warrend3b8bdd2012-01-25 14:43:28 -070087}
88
89static inline void tegra_pmc_writel(u32 val, u32 reg)
90{
Joseph Lo291fde32013-02-28 21:32:10 +000091 writel(val, tegra_pmc_base + reg);
Stephen Warrend3b8bdd2012-01-25 14:43:28 -070092}
93
Joseph Loc1417532013-02-28 21:32:11 +000094static int tegra_pmc_get_cpu_powerdomain_id(int cpuid)
95{
96 if (cpuid <= 0 || cpuid >= num_possible_cpus())
97 return -EINVAL;
98 return tegra_cpu_domains[cpuid];
99}
100
101static bool tegra_pmc_powergate_is_powered(int id)
102{
103 return (tegra_pmc_readl(PMC_PWRGATE_STATUS) >> id) & 1;
104}
105
106static int tegra_pmc_powergate_set(int id, bool new_state)
107{
108 bool old_state;
109 unsigned long flags;
110
111 spin_lock_irqsave(&tegra_powergate_lock, flags);
112
113 old_state = tegra_pmc_powergate_is_powered(id);
114 WARN_ON(old_state == new_state);
115
116 tegra_pmc_writel(PMC_PWRGATE_TOGGLE_START | id, PMC_PWRGATE_TOGGLE);
117
118 spin_unlock_irqrestore(&tegra_powergate_lock, flags);
119
120 return 0;
121}
122
123static int tegra_pmc_powergate_remove_clamping(int id)
124{
125 u32 mask;
126
127 /*
128 * Tegra has a bug where PCIE and VDE clamping masks are
129 * swapped relatively to the partition ids.
130 */
131 if (id == TEGRA_POWERGATE_VDEC)
132 mask = (1 << TEGRA_POWERGATE_PCIE);
133 else if (id == TEGRA_POWERGATE_PCIE)
134 mask = (1 << TEGRA_POWERGATE_VDEC);
135 else
136 mask = (1 << id);
137
138 tegra_pmc_writel(mask, PMC_REMOVE_CLAMPING);
139
140 return 0;
141}
142
143bool tegra_pmc_cpu_is_powered(int cpuid)
144{
145 int id;
146
147 id = tegra_pmc_get_cpu_powerdomain_id(cpuid);
148 if (id < 0)
149 return false;
150 return tegra_pmc_powergate_is_powered(id);
151}
152
153int tegra_pmc_cpu_power_on(int cpuid)
154{
155 int id;
156
157 id = tegra_pmc_get_cpu_powerdomain_id(cpuid);
158 if (id < 0)
159 return id;
160 return tegra_pmc_powergate_set(id, true);
161}
162
163int tegra_pmc_cpu_remove_clamping(int cpuid)
164{
165 int id;
166
167 id = tegra_pmc_get_cpu_powerdomain_id(cpuid);
168 if (id < 0)
169 return id;
170 return tegra_pmc_powergate_remove_clamping(id);
171}
172
Stephen Warren51100bd2013-08-20 15:47:38 -0600173void tegra_pmc_restart(enum reboot_mode mode, const char *cmd)
174{
175 u32 val;
176
Thierry Reding498bb3d2014-05-06 17:04:11 +0200177 val = tegra_pmc_readl(PMC_SCRATCH0);
178 val &= ~PMC_SCRATCH0_MODE_MASK;
179
180 if (cmd) {
181 if (strcmp(cmd, "recovery") == 0)
182 val |= PMC_SCRATCH0_MODE_RECOVERY;
183
184 if (strcmp(cmd, "bootloader") == 0)
185 val |= PMC_SCRATCH0_MODE_BOOTLOADER;
186
187 if (strcmp(cmd, "forced-recovery") == 0)
188 val |= PMC_SCRATCH0_MODE_RCM;
189 }
190
191 tegra_pmc_writel(val, PMC_SCRATCH0);
192
Stephen Warren51100bd2013-08-20 15:47:38 -0600193 val = tegra_pmc_readl(0);
194 val |= 0x10;
195 tegra_pmc_writel(val, 0);
196}
197
Joseph Lo0337c3e2013-04-03 19:31:28 +0800198#ifdef CONFIG_PM_SLEEP
Joseph Loc8c2e602013-04-03 19:31:47 +0800199static void set_power_timers(u32 us_on, u32 us_off, unsigned long rate)
Joseph Lo0337c3e2013-04-03 19:31:28 +0800200{
201 unsigned long long ticks;
202 unsigned long long pclk;
Joseph Lo0337c3e2013-04-03 19:31:28 +0800203 static unsigned long tegra_last_pclk;
204
Joseph Lo0337c3e2013-04-03 19:31:28 +0800205 if (WARN_ON_ONCE(rate <= 0))
206 pclk = 100000000;
207 else
208 pclk = rate;
209
210 if ((rate != tegra_last_pclk)) {
211 ticks = (us_on * pclk) + 999999ull;
212 do_div(ticks, 1000000);
213 tegra_pmc_writel((unsigned long)ticks, PMC_CPUPWRGOOD_TIMER);
214
215 ticks = (us_off * pclk) + 999999ull;
216 do_div(ticks, 1000000);
217 tegra_pmc_writel((unsigned long)ticks, PMC_CPUPWROFF_TIMER);
218 wmb();
219 }
220 tegra_last_pclk = pclk;
221}
Joseph Loc8c2e602013-04-03 19:31:47 +0800222
223enum tegra_suspend_mode tegra_pmc_get_suspend_mode(void)
224{
225 return pmc_pm_data.suspend_mode;
226}
227
Joseph Lo95872f42013-08-12 17:40:03 +0800228void tegra_pmc_set_suspend_mode(enum tegra_suspend_mode mode)
229{
230 if (mode < TEGRA_SUSPEND_NONE || mode >= TEGRA_MAX_SUSPEND_MODE)
231 return;
232
233 pmc_pm_data.suspend_mode = mode;
234}
235
236void tegra_pmc_suspend(void)
237{
238 tegra_pmc_writel(virt_to_phys(tegra_resume), PMC_SCRATCH41);
239}
240
241void tegra_pmc_resume(void)
242{
243 tegra_pmc_writel(0x0, PMC_SCRATCH41);
244}
245
Joseph Loc8c2e602013-04-03 19:31:47 +0800246void tegra_pmc_pm_set(enum tegra_suspend_mode mode)
247{
Joseph Lo3f1be812013-07-03 17:50:41 +0800248 u32 reg, csr_reg;
Joseph Loc8c2e602013-04-03 19:31:47 +0800249 unsigned long rate = 0;
250
251 reg = tegra_pmc_readl(PMC_CTRL);
252 reg |= TEGRA_POWER_CPU_PWRREQ_OE;
253 reg &= ~TEGRA_POWER_EFFECT_LP0;
254
Thierry Reding304664e2014-07-11 09:52:41 +0200255 switch (tegra_get_chip_id()) {
Joseph Lo3f1be812013-07-03 17:50:41 +0800256 case TEGRA20:
257 case TEGRA30:
258 break;
259 default:
260 /* Turn off CRAIL */
261 csr_reg = flowctrl_read_cpu_csr(0);
262 csr_reg &= ~FLOW_CTRL_CSR_ENABLE_EXT_MASK;
263 csr_reg |= FLOW_CTRL_CSR_ENABLE_EXT_CRAIL;
264 flowctrl_write_cpu_csr(0, csr_reg);
265 break;
266 }
267
Joseph Loc8c2e602013-04-03 19:31:47 +0800268 switch (mode) {
Joseph Lo95872f42013-08-12 17:40:03 +0800269 case TEGRA_SUSPEND_LP1:
270 rate = 32768;
271 break;
Joseph Loc8c2e602013-04-03 19:31:47 +0800272 case TEGRA_SUSPEND_LP2:
273 rate = clk_get_rate(tegra_pclk);
274 break;
275 default:
276 break;
277 }
278
279 set_power_timers(pmc_pm_data.cpu_good_time, pmc_pm_data.cpu_off_time,
280 rate);
281
282 tegra_pmc_writel(reg, PMC_CTRL);
283}
284
285void tegra_pmc_suspend_init(void)
286{
287 u32 reg;
288
289 /* Always enable CPU power request */
290 reg = tegra_pmc_readl(PMC_CTRL);
291 reg |= TEGRA_POWER_CPU_PWRREQ_OE;
292 tegra_pmc_writel(reg, PMC_CTRL);
Joseph Lo444f9a802013-08-12 17:40:01 +0800293
294 reg = tegra_pmc_readl(PMC_CTRL);
295
296 if (!pmc_pm_data.sysclkreq_high)
297 reg |= TEGRA_POWER_SYSCLK_POLARITY;
298 else
299 reg &= ~TEGRA_POWER_SYSCLK_POLARITY;
300
301 /* configure the output polarity while the request is tristated */
302 tegra_pmc_writel(reg, PMC_CTRL);
303
304 /* now enable the request */
305 reg |= TEGRA_POWER_SYSCLK_OE;
306 tegra_pmc_writel(reg, PMC_CTRL);
Joseph Loc8c2e602013-04-03 19:31:47 +0800307}
Joseph Lo0337c3e2013-04-03 19:31:28 +0800308#endif
309
Stephen Warrend3b8bdd2012-01-25 14:43:28 -0700310static const struct of_device_id matches[] __initconst = {
Joseph Lo6ca91f92013-10-08 12:50:04 +0800311 { .compatible = "nvidia,tegra124-pmc" },
Joseph Lo88c4aba2013-02-26 16:27:42 +0000312 { .compatible = "nvidia,tegra114-pmc" },
313 { .compatible = "nvidia,tegra30-pmc" },
Stephen Warrend3b8bdd2012-01-25 14:43:28 -0700314 { .compatible = "nvidia,tegra20-pmc" },
315 { }
316};
Joseph Lo291fde32013-02-28 21:32:10 +0000317
Stephen Warrend2207072013-08-20 15:17:35 -0600318void __init tegra_pmc_init_irq(void)
319{
320 struct device_node *np;
321 u32 val;
322
323 np = of_find_matching_node(NULL, matches);
324 BUG_ON(!np);
325
326 tegra_pmc_base = of_iomap(np, 0);
327
328 tegra_pmc_invert_interrupt = of_property_read_bool(np,
329 "nvidia,invert-interrupt");
330
331 val = tegra_pmc_readl(PMC_CTRL);
332 if (tegra_pmc_invert_interrupt)
333 val |= PMC_CTRL_INTR_LOW;
334 else
335 val &= ~PMC_CTRL_INTR_LOW;
336 tegra_pmc_writel(val, PMC_CTRL);
337}
338
339void __init tegra_pmc_init(void)
Joseph Lo291fde32013-02-28 21:32:10 +0000340{
341 struct device_node *np;
Joseph Lo4b51ccb2013-04-03 19:31:46 +0800342 u32 prop;
343 enum tegra_suspend_mode suspend_mode;
344 u32 core_good_time[2] = {0, 0};
345 u32 lp0_vec[2] = {0, 0};
Joseph Lo291fde32013-02-28 21:32:10 +0000346
347 np = of_find_matching_node(NULL, matches);
348 BUG_ON(!np);
349
Joseph Lo0337c3e2013-04-03 19:31:28 +0800350 tegra_pclk = of_clk_get_by_name(np, "pclk");
351 WARN_ON(IS_ERR(tegra_pclk));
Joseph Lo4b51ccb2013-04-03 19:31:46 +0800352
353 /* Grabbing the power management configurations */
354 if (of_property_read_u32(np, "nvidia,suspend-mode", &prop)) {
355 suspend_mode = TEGRA_SUSPEND_NONE;
356 } else {
357 switch (prop) {
358 case 0:
359 suspend_mode = TEGRA_SUSPEND_LP0;
360 break;
361 case 1:
362 suspend_mode = TEGRA_SUSPEND_LP1;
363 break;
364 case 2:
365 suspend_mode = TEGRA_SUSPEND_LP2;
366 break;
367 default:
368 suspend_mode = TEGRA_SUSPEND_NONE;
369 break;
370 }
371 }
Joseph Loc8c2e602013-04-03 19:31:47 +0800372 suspend_mode = tegra_pm_validate_suspend_mode(suspend_mode);
Joseph Lo4b51ccb2013-04-03 19:31:46 +0800373
374 if (of_property_read_u32(np, "nvidia,cpu-pwr-good-time", &prop))
375 suspend_mode = TEGRA_SUSPEND_NONE;
376 pmc_pm_data.cpu_good_time = prop;
377
378 if (of_property_read_u32(np, "nvidia,cpu-pwr-off-time", &prop))
379 suspend_mode = TEGRA_SUSPEND_NONE;
380 pmc_pm_data.cpu_off_time = prop;
381
382 if (of_property_read_u32_array(np, "nvidia,core-pwr-good-time",
383 core_good_time, ARRAY_SIZE(core_good_time)))
384 suspend_mode = TEGRA_SUSPEND_NONE;
385 pmc_pm_data.core_osc_time = core_good_time[0];
386 pmc_pm_data.core_pmu_time = core_good_time[1];
387
388 if (of_property_read_u32(np, "nvidia,core-pwr-off-time",
389 &prop))
390 suspend_mode = TEGRA_SUSPEND_NONE;
391 pmc_pm_data.core_off_time = prop;
392
393 pmc_pm_data.corereq_high = of_property_read_bool(np,
394 "nvidia,core-power-req-active-high");
395
396 pmc_pm_data.sysclkreq_high = of_property_read_bool(np,
397 "nvidia,sys-clock-req-active-high");
398
399 pmc_pm_data.combined_req = of_property_read_bool(np,
400 "nvidia,combined-power-req");
401
402 pmc_pm_data.cpu_pwr_good_en = of_property_read_bool(np,
403 "nvidia,cpu-pwr-good-en");
404
405 if (of_property_read_u32_array(np, "nvidia,lp0-vec", lp0_vec,
406 ARRAY_SIZE(lp0_vec)))
407 if (suspend_mode == TEGRA_SUSPEND_LP0)
408 suspend_mode = TEGRA_SUSPEND_LP1;
409
410 pmc_pm_data.lp0_vec_phy_addr = lp0_vec[0];
411 pmc_pm_data.lp0_vec_size = lp0_vec[1];
412
413 pmc_pm_data.suspend_mode = suspend_mode;
Joseph Lo291fde32013-02-28 21:32:10 +0000414}