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
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>
22 #include <linux/of_address.h>
29 #define TEGRA_POWER_EFFECT_LP0 (1 << 14) /* LP0 when CPU pwr gated */
30 #define TEGRA_POWER_CPU_PWRREQ_POLARITY (1 << 15) /* CPU pwr req polarity */
31 #define TEGRA_POWER_CPU_PWRREQ_OE (1 << 16) /* CPU pwr req enable */
34 #define PMC_CTRL_INTR_LOW (1 << 17)
35 #define PMC_PWRGATE_TOGGLE 0x30
36 #define PMC_PWRGATE_TOGGLE_START (1 << 8)
37 #define PMC_REMOVE_CLAMPING 0x34
38 #define PMC_PWRGATE_STATUS 0x38
40 #define PMC_CPUPWRGOOD_TIMER 0xc8
41 #define PMC_CPUPWROFF_TIMER 0xcc
43 #define TEGRA_POWERGATE_PCIE 3
44 #define TEGRA_POWERGATE_VDEC 4
45 #define TEGRA_POWERGATE_CPU1 9
46 #define TEGRA_POWERGATE_CPU2 10
47 #define TEGRA_POWERGATE_CPU3 11
49 static u8 tegra_cpu_domains
[] = {
50 0xFF, /* not available for CPU0 */
55 static DEFINE_SPINLOCK(tegra_powergate_lock
);
57 static void __iomem
*tegra_pmc_base
;
58 static bool tegra_pmc_invert_interrupt
;
59 static struct clk
*tegra_pclk
;
62 u32 cpu_good_time
; /* CPU power good time in uS */
63 u32 cpu_off_time
; /* CPU power off time in uS */
64 u32 core_osc_time
; /* Core power good osc time in uS */
65 u32 core_pmu_time
; /* Core power good pmu time in uS */
66 u32 core_off_time
; /* Core power off time in uS */
67 bool corereq_high
; /* Core power request active-high */
68 bool sysclkreq_high
; /* System clock request active-high */
69 bool combined_req
; /* Combined pwr req for CPU & Core */
70 bool cpu_pwr_good_en
; /* CPU power good signal is enabled */
71 u32 lp0_vec_phy_addr
; /* The phy addr of LP0 warm boot code */
72 u32 lp0_vec_size
; /* The size of LP0 warm boot code */
73 enum tegra_suspend_mode suspend_mode
;
75 static struct pmc_pm_data pmc_pm_data
;
77 static inline u32
tegra_pmc_readl(u32 reg
)
79 return readl(tegra_pmc_base
+ reg
);
82 static inline void tegra_pmc_writel(u32 val
, u32 reg
)
84 writel(val
, tegra_pmc_base
+ reg
);
87 static int tegra_pmc_get_cpu_powerdomain_id(int cpuid
)
89 if (cpuid
<= 0 || cpuid
>= num_possible_cpus())
91 return tegra_cpu_domains
[cpuid
];
94 static bool tegra_pmc_powergate_is_powered(int id
)
96 return (tegra_pmc_readl(PMC_PWRGATE_STATUS
) >> id
) & 1;
99 static int tegra_pmc_powergate_set(int id
, bool new_state
)
104 spin_lock_irqsave(&tegra_powergate_lock
, flags
);
106 old_state
= tegra_pmc_powergate_is_powered(id
);
107 WARN_ON(old_state
== new_state
);
109 tegra_pmc_writel(PMC_PWRGATE_TOGGLE_START
| id
, PMC_PWRGATE_TOGGLE
);
111 spin_unlock_irqrestore(&tegra_powergate_lock
, flags
);
116 static int tegra_pmc_powergate_remove_clamping(int id
)
121 * Tegra has a bug where PCIE and VDE clamping masks are
122 * swapped relatively to the partition ids.
124 if (id
== TEGRA_POWERGATE_VDEC
)
125 mask
= (1 << TEGRA_POWERGATE_PCIE
);
126 else if (id
== TEGRA_POWERGATE_PCIE
)
127 mask
= (1 << TEGRA_POWERGATE_VDEC
);
131 tegra_pmc_writel(mask
, PMC_REMOVE_CLAMPING
);
136 bool tegra_pmc_cpu_is_powered(int cpuid
)
140 id
= tegra_pmc_get_cpu_powerdomain_id(cpuid
);
143 return tegra_pmc_powergate_is_powered(id
);
146 int tegra_pmc_cpu_power_on(int cpuid
)
150 id
= tegra_pmc_get_cpu_powerdomain_id(cpuid
);
153 return tegra_pmc_powergate_set(id
, true);
156 int tegra_pmc_cpu_remove_clamping(int cpuid
)
160 id
= tegra_pmc_get_cpu_powerdomain_id(cpuid
);
163 return tegra_pmc_powergate_remove_clamping(id
);
166 #ifdef CONFIG_PM_SLEEP
167 static void set_power_timers(u32 us_on
, u32 us_off
, unsigned long rate
)
169 unsigned long long ticks
;
170 unsigned long long pclk
;
171 static unsigned long tegra_last_pclk
;
173 if (WARN_ON_ONCE(rate
<= 0))
178 if ((rate
!= tegra_last_pclk
)) {
179 ticks
= (us_on
* pclk
) + 999999ull;
180 do_div(ticks
, 1000000);
181 tegra_pmc_writel((unsigned long)ticks
, PMC_CPUPWRGOOD_TIMER
);
183 ticks
= (us_off
* pclk
) + 999999ull;
184 do_div(ticks
, 1000000);
185 tegra_pmc_writel((unsigned long)ticks
, PMC_CPUPWROFF_TIMER
);
188 tegra_last_pclk
= pclk
;
191 enum tegra_suspend_mode
tegra_pmc_get_suspend_mode(void)
193 return pmc_pm_data
.suspend_mode
;
196 void tegra_pmc_pm_set(enum tegra_suspend_mode mode
)
199 unsigned long rate
= 0;
201 reg
= tegra_pmc_readl(PMC_CTRL
);
202 reg
|= TEGRA_POWER_CPU_PWRREQ_OE
;
203 reg
&= ~TEGRA_POWER_EFFECT_LP0
;
206 case TEGRA_SUSPEND_LP2
:
207 rate
= clk_get_rate(tegra_pclk
);
213 set_power_timers(pmc_pm_data
.cpu_good_time
, pmc_pm_data
.cpu_off_time
,
216 tegra_pmc_writel(reg
, PMC_CTRL
);
219 void tegra_pmc_suspend_init(void)
223 /* Always enable CPU power request */
224 reg
= tegra_pmc_readl(PMC_CTRL
);
225 reg
|= TEGRA_POWER_CPU_PWRREQ_OE
;
226 tegra_pmc_writel(reg
, PMC_CTRL
);
230 static const struct of_device_id matches
[] __initconst
= {
231 { .compatible
= "nvidia,tegra114-pmc" },
232 { .compatible
= "nvidia,tegra30-pmc" },
233 { .compatible
= "nvidia,tegra20-pmc" },
237 static void tegra_pmc_parse_dt(void)
239 struct device_node
*np
;
241 enum tegra_suspend_mode suspend_mode
;
242 u32 core_good_time
[2] = {0, 0};
243 u32 lp0_vec
[2] = {0, 0};
245 np
= of_find_matching_node(NULL
, matches
);
248 tegra_pmc_base
= of_iomap(np
, 0);
250 tegra_pmc_invert_interrupt
= of_property_read_bool(np
,
251 "nvidia,invert-interrupt");
252 tegra_pclk
= of_clk_get_by_name(np
, "pclk");
253 WARN_ON(IS_ERR(tegra_pclk
));
255 /* Grabbing the power management configurations */
256 if (of_property_read_u32(np
, "nvidia,suspend-mode", &prop
)) {
257 suspend_mode
= TEGRA_SUSPEND_NONE
;
261 suspend_mode
= TEGRA_SUSPEND_LP0
;
264 suspend_mode
= TEGRA_SUSPEND_LP1
;
267 suspend_mode
= TEGRA_SUSPEND_LP2
;
270 suspend_mode
= TEGRA_SUSPEND_NONE
;
274 suspend_mode
= tegra_pm_validate_suspend_mode(suspend_mode
);
276 if (of_property_read_u32(np
, "nvidia,cpu-pwr-good-time", &prop
))
277 suspend_mode
= TEGRA_SUSPEND_NONE
;
278 pmc_pm_data
.cpu_good_time
= prop
;
280 if (of_property_read_u32(np
, "nvidia,cpu-pwr-off-time", &prop
))
281 suspend_mode
= TEGRA_SUSPEND_NONE
;
282 pmc_pm_data
.cpu_off_time
= prop
;
284 if (of_property_read_u32_array(np
, "nvidia,core-pwr-good-time",
285 core_good_time
, ARRAY_SIZE(core_good_time
)))
286 suspend_mode
= TEGRA_SUSPEND_NONE
;
287 pmc_pm_data
.core_osc_time
= core_good_time
[0];
288 pmc_pm_data
.core_pmu_time
= core_good_time
[1];
290 if (of_property_read_u32(np
, "nvidia,core-pwr-off-time",
292 suspend_mode
= TEGRA_SUSPEND_NONE
;
293 pmc_pm_data
.core_off_time
= prop
;
295 pmc_pm_data
.corereq_high
= of_property_read_bool(np
,
296 "nvidia,core-power-req-active-high");
298 pmc_pm_data
.sysclkreq_high
= of_property_read_bool(np
,
299 "nvidia,sys-clock-req-active-high");
301 pmc_pm_data
.combined_req
= of_property_read_bool(np
,
302 "nvidia,combined-power-req");
304 pmc_pm_data
.cpu_pwr_good_en
= of_property_read_bool(np
,
305 "nvidia,cpu-pwr-good-en");
307 if (of_property_read_u32_array(np
, "nvidia,lp0-vec", lp0_vec
,
308 ARRAY_SIZE(lp0_vec
)))
309 if (suspend_mode
== TEGRA_SUSPEND_LP0
)
310 suspend_mode
= TEGRA_SUSPEND_LP1
;
312 pmc_pm_data
.lp0_vec_phy_addr
= lp0_vec
[0];
313 pmc_pm_data
.lp0_vec_size
= lp0_vec
[1];
315 pmc_pm_data
.suspend_mode
= suspend_mode
;
318 void __init
tegra_pmc_init(void)
322 tegra_pmc_parse_dt();
324 val
= tegra_pmc_readl(PMC_CTRL
);
325 if (tegra_pmc_invert_interrupt
)
326 val
|= PMC_CTRL_INTR_LOW
;
328 val
&= ~PMC_CTRL_INTR_LOW
;
329 tegra_pmc_writel(val
, PMC_CTRL
);