]> nv-tegra.nvidia Code Review - linux-3.10.git/blob - arch/arm/mach-tegra/pmc.c
ARM: tegra: mcerr: Update client arrays
[linux-3.10.git] / arch / arm / mach-tegra / pmc.c
1 /*
2  * Copyright (C) 2012,2013 NVIDIA CORPORATION. All rights reserved.
3  *
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
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>
23 #include <linux/export.h>
24
25 #include "pmc.h"
26
27 #define PMC_CTRL                        0x0
28 #define PMC_CTRL_INTR_LOW               (1 << 17)
29 #define PMC_PWRGATE_TOGGLE              0x30
30 #define PMC_PWRGATE_TOGGLE_START        (1 << 8)
31 #define PMC_REMOVE_CLAMPING             0x34
32 #define PMC_PWRGATE_STATUS              0x38
33
34 #define PMC_CPUPWRGOOD_TIMER    0xc8
35 #define PMC_CPUPWROFF_TIMER     0xcc
36
37 #define TEGRA_POWERGATE_PCIE    3
38 #define TEGRA_POWERGATE_VDEC    4
39 #define TEGRA_POWERGATE_CPU1    9
40 #define TEGRA_POWERGATE_CPU2    10
41 #define TEGRA_POWERGATE_CPU3    11
42
43 static u8 tegra_cpu_domains[] = {
44         0xFF,                   /* not available for CPU0 */
45         TEGRA_POWERGATE_CPU1,
46         TEGRA_POWERGATE_CPU2,
47         TEGRA_POWERGATE_CPU3,
48 };
49 static DEFINE_SPINLOCK(tegra_powergate_lock);
50
51 static void __iomem *tegra_pmc_base;
52 static bool tegra_pmc_invert_interrupt;
53 #if defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK)
54 static struct clk *tegra_pclk;
55 #endif
56
57 #ifdef CONFIG_OF
58 static struct pmc_pm_data pmc_pm_data;
59 #endif
60 struct pmc_pm_data *tegra_get_pm_data()
61 {
62 #ifdef CONFIG_OF
63         /*
64          * Some boards have CONFIG_OF defined but no dts files
65          */
66         if (!tegra_pmc_base)
67                 return NULL;
68         return &pmc_pm_data;
69 #else
70         return NULL;
71 #endif
72 }
73 EXPORT_SYMBOL(tegra_get_pm_data);
74
75 static inline u32 tegra_pmc_readl(u32 reg)
76 {
77         return readl(tegra_pmc_base + reg);
78 }
79
80 static inline void tegra_pmc_writel(u32 val, u32 reg)
81 {
82         writel(val, tegra_pmc_base + reg);
83 }
84
85 static int tegra_pmc_get_cpu_powerdomain_id(int cpuid)
86 {
87         if (cpuid <= 0 || cpuid >= num_possible_cpus())
88                 return -EINVAL;
89         return tegra_cpu_domains[cpuid];
90 }
91
92 static bool tegra_pmc_powergate_is_powered(int id)
93 {
94         return (tegra_pmc_readl(PMC_PWRGATE_STATUS) >> id) & 1;
95 }
96
97 static int tegra_pmc_powergate_set(int id, bool new_state)
98 {
99         bool old_state;
100         unsigned long flags;
101
102         spin_lock_irqsave(&tegra_powergate_lock, flags);
103
104         old_state = tegra_pmc_powergate_is_powered(id);
105         WARN_ON(old_state == new_state);
106
107         tegra_pmc_writel(PMC_PWRGATE_TOGGLE_START | id, PMC_PWRGATE_TOGGLE);
108
109         spin_unlock_irqrestore(&tegra_powergate_lock, flags);
110
111         return 0;
112 }
113
114 static int tegra_pmc_powergate_remove_clamping(int id)
115 {
116         u32 mask;
117
118         /*
119          * Tegra has a bug where PCIE and VDE clamping masks are
120          * swapped relatively to the partition ids.
121          */
122         if (id ==  TEGRA_POWERGATE_VDEC)
123                 mask = (1 << TEGRA_POWERGATE_PCIE);
124         else if (id == TEGRA_POWERGATE_PCIE)
125                 mask = (1 << TEGRA_POWERGATE_VDEC);
126         else
127                 mask = (1 << id);
128
129         tegra_pmc_writel(mask, PMC_REMOVE_CLAMPING);
130
131         return 0;
132 }
133
134 bool tegra_pmc_cpu_is_powered(int cpuid)
135 {
136         int id;
137
138         id = tegra_pmc_get_cpu_powerdomain_id(cpuid);
139         if (id < 0)
140                 return false;
141         return tegra_pmc_powergate_is_powered(id);
142 }
143
144 int tegra_pmc_cpu_power_on(int cpuid)
145 {
146         int id;
147
148         id = tegra_pmc_get_cpu_powerdomain_id(cpuid);
149         if (id < 0)
150                 return id;
151         return tegra_pmc_powergate_set(id, true);
152 }
153
154 int tegra_pmc_cpu_remove_clamping(int cpuid)
155 {
156         int id;
157
158         id = tegra_pmc_get_cpu_powerdomain_id(cpuid);
159         if (id < 0)
160                 return id;
161         return tegra_pmc_powergate_remove_clamping(id);
162 }
163
164 #if defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK) && defined(CONFIG_PM_SLEEP)
165 void set_power_timers(unsigned long us_on, unsigned long us_off)
166 {
167         unsigned long long ticks;
168         unsigned long long pclk;
169         unsigned long rate;
170         static unsigned long tegra_last_pclk;
171
172         rate = clk_get_rate(tegra_pclk);
173         if (WARN_ON_ONCE(rate <= 0))
174                 pclk = 100000000;
175         else
176                 pclk = rate;
177
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);
182
183                 ticks = (us_off * pclk) + 999999ull;
184                 do_div(ticks, 1000000);
185                 tegra_pmc_writel((unsigned long)ticks, PMC_CPUPWROFF_TIMER);
186                 wmb();
187         }
188         tegra_last_pclk = pclk;
189 }
190 #endif
191
192 static const struct of_device_id matches[] __initconst = {
193         { .compatible = "nvidia,tegra124-pmc" },
194         { .compatible = "nvidia,tegra148-pmc" },
195         { .compatible = "nvidia,tegra114-pmc" },
196         { .compatible = "nvidia,tegra30-pmc" },
197         { .compatible = "nvidia,tegra20-pmc" },
198         { }
199 };
200
201 static void tegra_pmc_parse_dt(void)
202 {
203         struct device_node *np;
204         u32 prop;
205         enum tegra_suspend_mode suspend_mode;
206         u32 core_good_time[2] = {0, 0};
207         u32 lp0_vec[2] = {0, 0};
208
209         np = of_find_matching_node(NULL, matches);
210         BUG_ON(!np);
211
212         tegra_pmc_base = of_iomap(np, 0);
213
214         tegra_pmc_invert_interrupt = of_property_read_bool(np,
215                                      "nvidia,invert-interrupt");
216 #if defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK)
217         tegra_pclk = of_clk_get_by_name(np, "pclk");
218         WARN_ON(IS_ERR(tegra_pclk));
219 #endif
220
221         /* Grabbing the power management configurations */
222         if (of_property_read_u32(np, "nvidia,suspend-mode", &prop)) {
223                 suspend_mode = TEGRA_SUSPEND_NONE;
224         } else {
225                 switch (prop) {
226                 case 0:
227                         suspend_mode = TEGRA_SUSPEND_LP0;
228                         break;
229                 case 1:
230                         suspend_mode = TEGRA_SUSPEND_LP1;
231                         break;
232                 case 2:
233                         suspend_mode = TEGRA_SUSPEND_LP2;
234                         break;
235                 default:
236                         suspend_mode = TEGRA_SUSPEND_NONE;
237                         break;
238                 }
239         }
240
241         if (of_property_read_u32(np, "nvidia,cpu-pwr-good-time", &prop))
242                 suspend_mode = TEGRA_SUSPEND_NONE;
243         pmc_pm_data.cpu_good_time = prop;
244
245         if (of_property_read_u32(np, "nvidia,cpu-pwr-off-time", &prop))
246                 suspend_mode = TEGRA_SUSPEND_NONE;
247         pmc_pm_data.cpu_off_time = prop;
248
249         if (of_property_read_u32_array(np, "nvidia,core-pwr-good-time",
250                         core_good_time, ARRAY_SIZE(core_good_time)))
251                 suspend_mode = TEGRA_SUSPEND_NONE;
252         pmc_pm_data.core_osc_time = core_good_time[0];
253         pmc_pm_data.core_pmu_time = core_good_time[1];
254
255         if (of_property_read_u32(np, "nvidia,core-pwr-off-time",
256                                  &prop))
257                 suspend_mode = TEGRA_SUSPEND_NONE;
258         pmc_pm_data.core_off_time = prop;
259
260         pmc_pm_data.corereq_high = of_property_read_bool(np,
261                                 "nvidia,core-power-req-active-high");
262
263         pmc_pm_data.sysclkreq_high = of_property_read_bool(np,
264                                 "nvidia,sys-clock-req-active-high");
265
266         pmc_pm_data.combined_req = of_property_read_bool(np,
267                                 "nvidia,combined-power-req");
268
269         pmc_pm_data.cpu_pwr_good_en = of_property_read_bool(np,
270                                 "nvidia,cpu-pwr-good-en");
271
272         if (of_property_read_u32_array(np, "nvidia,lp0-vec", lp0_vec,
273                                        ARRAY_SIZE(lp0_vec)))
274                 if (suspend_mode == TEGRA_SUSPEND_LP0)
275                         suspend_mode = TEGRA_SUSPEND_LP1;
276
277         pmc_pm_data.lp0_vec_phy_addr = lp0_vec[0];
278         pmc_pm_data.lp0_vec_size = lp0_vec[1];
279
280         pmc_pm_data.suspend_mode = suspend_mode;
281 }
282
283 void __init tegra_pmc_init(void)
284 {
285         u32 val;
286
287         tegra_pmc_parse_dt();
288
289         val = tegra_pmc_readl(PMC_CTRL);
290         if (tegra_pmc_invert_interrupt)
291                 val |= PMC_CTRL_INTR_LOW;
292         else
293                 val &= ~PMC_CTRL_INTR_LOW;
294         tegra_pmc_writel(val, PMC_CTRL);
295 }