2 * drivers/powergate/tegra-powergate.c
4 * Copyright (c) 2010 Google, Inc
7 * Colin Cross <ccross@google.com>
9 * This software is licensed under the terms of the GNU General Public
10 * License version 2, as published by the Free Software Foundation, and
11 * may be copied, distributed, and modified under those terms.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
20 #include <linux/kernel.h>
21 #include <linux/clk.h>
22 #include <linux/debugfs.h>
23 #include <linux/delay.h>
24 #include <linux/err.h>
25 #include <linux/export.h>
26 #include <linux/init.h>
28 #include <linux/reset.h>
29 #include <linux/seq_file.h>
30 #include <linux/spinlock.h>
31 #include <linux/clk/tegra.h>
32 #include <linux/tegra-powergate.h>
37 #define PWRGATE_TOGGLE 0x30
38 #define PWRGATE_TOGGLE_START (1 << 8)
40 #define REMOVE_CLAMPING 0x34
42 #define PWRGATE_STATUS 0x38
44 static int tegra_num_powerdomains;
45 static int tegra_num_cpu_domains;
46 static const u8 *tegra_cpu_domains;
48 static const u8 tegra30_cpu_domains[] = {
55 static const u8 tegra114_cpu_domains[] = {
62 static DEFINE_SPINLOCK(tegra_powergate_lock);
64 static void __iomem *pmc = IO_ADDRESS(TEGRA_PMC_BASE);
66 static u32 pmc_read(unsigned long reg)
68 return readl(pmc + reg);
71 static void pmc_write(u32 val, unsigned long reg)
73 writel(val, pmc + reg);
76 static int tegra_powergate_set(int id, bool new_state)
81 spin_lock_irqsave(&tegra_powergate_lock, flags);
83 status = pmc_read(PWRGATE_STATUS) & (1 << id);
85 if (status == new_state) {
86 spin_unlock_irqrestore(&tegra_powergate_lock, flags);
90 pmc_write(PWRGATE_TOGGLE_START | id, PWRGATE_TOGGLE);
92 spin_unlock_irqrestore(&tegra_powergate_lock, flags);
97 int tegra_powergate_power_on(int id)
99 if (id < 0 || id >= tegra_num_powerdomains)
102 return tegra_powergate_set(id, true);
105 int tegra_powergate_power_off(int id)
107 if (id < 0 || id >= tegra_num_powerdomains)
110 return tegra_powergate_set(id, false);
113 int tegra_powergate_is_powered(int id)
117 if (id < 0 || id >= tegra_num_powerdomains)
120 status = pmc_read(PWRGATE_STATUS) & (1 << id);
124 int tegra_powergate_remove_clamping(int id)
128 if (id < 0 || id >= tegra_num_powerdomains)
132 * Tegra 2 has a bug where PCIE and VDE clamping masks are
133 * swapped relatively to the partition ids
135 if (id == TEGRA_POWERGATE_VDEC)
136 mask = (1 << TEGRA_POWERGATE_PCIE);
137 else if (id == TEGRA_POWERGATE_PCIE)
138 mask = (1 << TEGRA_POWERGATE_VDEC);
142 pmc_write(mask, REMOVE_CLAMPING);
147 /* Must be called with clk disabled, and returns with clk enabled */
148 int tegra_powergate_sequence_power_up(int id, struct clk *clk,
149 struct reset_control *rst)
153 reset_control_assert(rst);
155 ret = tegra_powergate_power_on(id);
159 ret = clk_prepare_enable(clk);
165 ret = tegra_powergate_remove_clamping(id);
170 reset_control_deassert(rst);
175 clk_disable_unprepare(clk);
177 tegra_powergate_power_off(id);
181 EXPORT_SYMBOL(tegra_powergate_sequence_power_up);
183 int tegra_cpu_powergate_id(int cpuid)
185 if (cpuid > 0 && cpuid < tegra_num_cpu_domains)
186 return tegra_cpu_domains[cpuid];
191 int __init tegra_powergate_init(void)
193 switch (tegra_chip_id) {
195 tegra_num_powerdomains = 7;
198 tegra_num_powerdomains = 14;
199 tegra_num_cpu_domains = 4;
200 tegra_cpu_domains = tegra30_cpu_domains;
203 tegra_num_powerdomains = 23;
204 tegra_num_cpu_domains = 4;
205 tegra_cpu_domains = tegra114_cpu_domains;
208 /* Unknown Tegra variant. Disable powergating */
209 tegra_num_powerdomains = 0;
216 #ifdef CONFIG_DEBUG_FS
218 static const char * const *powergate_name;
220 static const char * const powergate_name_t20[] = {
221 [TEGRA_POWERGATE_CPU] = "cpu",
222 [TEGRA_POWERGATE_3D] = "3d",
223 [TEGRA_POWERGATE_VENC] = "venc",
224 [TEGRA_POWERGATE_VDEC] = "vdec",
225 [TEGRA_POWERGATE_PCIE] = "pcie",
226 [TEGRA_POWERGATE_L2] = "l2",
227 [TEGRA_POWERGATE_MPE] = "mpe",
230 static const char * const powergate_name_t30[] = {
231 [TEGRA_POWERGATE_CPU] = "cpu0",
232 [TEGRA_POWERGATE_3D] = "3d0",
233 [TEGRA_POWERGATE_VENC] = "venc",
234 [TEGRA_POWERGATE_VDEC] = "vdec",
235 [TEGRA_POWERGATE_PCIE] = "pcie",
236 [TEGRA_POWERGATE_L2] = "l2",
237 [TEGRA_POWERGATE_MPE] = "mpe",
238 [TEGRA_POWERGATE_HEG] = "heg",
239 [TEGRA_POWERGATE_SATA] = "sata",
240 [TEGRA_POWERGATE_CPU1] = "cpu1",
241 [TEGRA_POWERGATE_CPU2] = "cpu2",
242 [TEGRA_POWERGATE_CPU3] = "cpu3",
243 [TEGRA_POWERGATE_CELP] = "celp",
244 [TEGRA_POWERGATE_3D1] = "3d1",
247 static const char * const powergate_name_t114[] = {
248 [TEGRA_POWERGATE_CPU] = "cpu0",
249 [TEGRA_POWERGATE_3D] = "3d",
250 [TEGRA_POWERGATE_VENC] = "venc",
251 [TEGRA_POWERGATE_VDEC] = "vdec",
252 [TEGRA_POWERGATE_MPE] = "mpe",
253 [TEGRA_POWERGATE_HEG] = "heg",
254 [TEGRA_POWERGATE_CPU1] = "cpu1",
255 [TEGRA_POWERGATE_CPU2] = "cpu2",
256 [TEGRA_POWERGATE_CPU3] = "cpu3",
257 [TEGRA_POWERGATE_CELP] = "celp",
258 [TEGRA_POWERGATE_CPU0] = "cpu0",
259 [TEGRA_POWERGATE_C0NC] = "c0nc",
260 [TEGRA_POWERGATE_C1NC] = "c1nc",
261 [TEGRA_POWERGATE_DIS] = "dis",
262 [TEGRA_POWERGATE_DISB] = "disb",
263 [TEGRA_POWERGATE_XUSBA] = "xusba",
264 [TEGRA_POWERGATE_XUSBB] = "xusbb",
265 [TEGRA_POWERGATE_XUSBC] = "xusbc",
268 static int powergate_show(struct seq_file *s, void *data)
272 seq_printf(s, " powergate powered\n");
273 seq_printf(s, "------------------\n");
275 for (i = 0; i < tegra_num_powerdomains; i++) {
276 if (!powergate_name[i])
279 seq_printf(s, " %9s %7s\n", powergate_name[i],
280 tegra_powergate_is_powered(i) ? "yes" : "no");
286 static int powergate_open(struct inode *inode, struct file *file)
288 return single_open(file, powergate_show, inode->i_private);
291 static const struct file_operations powergate_fops = {
292 .open = powergate_open,
295 .release = single_release,
298 int __init tegra_powergate_debugfs_init(void)
302 switch (tegra_chip_id) {
304 powergate_name = powergate_name_t20;
307 powergate_name = powergate_name_t30;
310 powergate_name = powergate_name_t114;
314 if (powergate_name) {
315 d = debugfs_create_file("powergate", S_IRUGO, NULL, NULL,