blob: c472bed3245e974da8ae1779a5c72ba35696b417 [file] [log] [blame]
Colin Crossce1e3262010-05-24 17:07:46 -07001/*
2 * drivers/powergate/tegra-powergate.c
3 *
4 * Copyright (c) 2010 Google, Inc
5 *
6 * Author:
7 * Colin Cross <ccross@google.com>
8 *
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.
12 *
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.
17 *
18 */
19
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>
Thierry Reding99f69fe2013-03-28 21:35:03 +010025#include <linux/export.h>
Colin Crossce1e3262010-05-24 17:07:46 -070026#include <linux/init.h>
27#include <linux/io.h>
28#include <linux/seq_file.h>
29#include <linux/spinlock.h>
Prashant Gaikwad61fd2902013-01-11 13:16:26 +053030#include <linux/clk/tegra.h>
Colin Crossce1e3262010-05-24 17:07:46 -070031
Colin Crossce1e3262010-05-24 17:07:46 -070032#include <mach/powergate.h>
33
Peter De Schrijver8f5d6f1b2012-02-10 01:47:46 +020034#include "fuse.h"
Stephen Warren2be39c02012-10-04 14:24:09 -060035#include "iomap.h"
Peter De Schrijver8f5d6f1b2012-02-10 01:47:46 +020036
Colin Crossce1e3262010-05-24 17:07:46 -070037#define PWRGATE_TOGGLE 0x30
38#define PWRGATE_TOGGLE_START (1 << 8)
39
40#define REMOVE_CLAMPING 0x34
41
42#define PWRGATE_STATUS 0x38
43
Peter De Schrijver8f5d6f1b2012-02-10 01:47:46 +020044static int tegra_num_powerdomains;
Peter De Schrijver65fe31d2012-02-10 01:47:49 +020045static int tegra_num_cpu_domains;
46static u8 *tegra_cpu_domains;
47static u8 tegra30_cpu_domains[] = {
48 TEGRA_POWERGATE_CPU0,
49 TEGRA_POWERGATE_CPU1,
50 TEGRA_POWERGATE_CPU2,
51 TEGRA_POWERGATE_CPU3,
52};
Peter De Schrijver8f5d6f1b2012-02-10 01:47:46 +020053
Colin Crossce1e3262010-05-24 17:07:46 -070054static DEFINE_SPINLOCK(tegra_powergate_lock);
55
56static void __iomem *pmc = IO_ADDRESS(TEGRA_PMC_BASE);
57
58static u32 pmc_read(unsigned long reg)
59{
60 return readl(pmc + reg);
61}
62
63static void pmc_write(u32 val, unsigned long reg)
64{
65 writel(val, pmc + reg);
66}
67
68static int tegra_powergate_set(int id, bool new_state)
69{
70 bool status;
71 unsigned long flags;
72
73 spin_lock_irqsave(&tegra_powergate_lock, flags);
74
75 status = pmc_read(PWRGATE_STATUS) & (1 << id);
76
77 if (status == new_state) {
78 spin_unlock_irqrestore(&tegra_powergate_lock, flags);
79 return -EINVAL;
80 }
81
82 pmc_write(PWRGATE_TOGGLE_START | id, PWRGATE_TOGGLE);
83
84 spin_unlock_irqrestore(&tegra_powergate_lock, flags);
85
86 return 0;
87}
88
89int tegra_powergate_power_on(int id)
90{
Peter De Schrijver8f5d6f1b2012-02-10 01:47:46 +020091 if (id < 0 || id >= tegra_num_powerdomains)
Colin Crossce1e3262010-05-24 17:07:46 -070092 return -EINVAL;
93
94 return tegra_powergate_set(id, true);
95}
96
97int tegra_powergate_power_off(int id)
98{
Peter De Schrijver8f5d6f1b2012-02-10 01:47:46 +020099 if (id < 0 || id >= tegra_num_powerdomains)
Colin Crossce1e3262010-05-24 17:07:46 -0700100 return -EINVAL;
101
102 return tegra_powergate_set(id, false);
103}
104
Peter De Schrijver6ac8cb52012-02-10 01:47:47 +0200105int tegra_powergate_is_powered(int id)
Colin Crossce1e3262010-05-24 17:07:46 -0700106{
107 u32 status;
108
Peter De Schrijver8f5d6f1b2012-02-10 01:47:46 +0200109 if (id < 0 || id >= tegra_num_powerdomains)
110 return -EINVAL;
Colin Crossce1e3262010-05-24 17:07:46 -0700111
112 status = pmc_read(PWRGATE_STATUS) & (1 << id);
113 return !!status;
114}
115
116int tegra_powergate_remove_clamping(int id)
117{
118 u32 mask;
119
Peter De Schrijver8f5d6f1b2012-02-10 01:47:46 +0200120 if (id < 0 || id >= tegra_num_powerdomains)
Colin Crossce1e3262010-05-24 17:07:46 -0700121 return -EINVAL;
122
123 /*
124 * Tegra 2 has a bug where PCIE and VDE clamping masks are
125 * swapped relatively to the partition ids
126 */
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);
133
134 pmc_write(mask, REMOVE_CLAMPING);
135
136 return 0;
137}
138
139/* Must be called with clk disabled, and returns with clk enabled */
140int tegra_powergate_sequence_power_up(int id, struct clk *clk)
141{
142 int ret;
143
144 tegra_periph_reset_assert(clk);
145
146 ret = tegra_powergate_power_on(id);
147 if (ret)
148 goto err_power;
149
Prashant Gaikwad6a5278d2012-06-05 09:59:35 +0530150 ret = clk_prepare_enable(clk);
Colin Crossce1e3262010-05-24 17:07:46 -0700151 if (ret)
152 goto err_clk;
153
154 udelay(10);
155
156 ret = tegra_powergate_remove_clamping(id);
157 if (ret)
158 goto err_clamp;
159
160 udelay(10);
161 tegra_periph_reset_deassert(clk);
162
163 return 0;
164
165err_clamp:
Prashant Gaikwad6a5278d2012-06-05 09:59:35 +0530166 clk_disable_unprepare(clk);
Colin Crossce1e3262010-05-24 17:07:46 -0700167err_clk:
168 tegra_powergate_power_off(id);
169err_power:
170 return ret;
171}
Thierry Reding99f69fe2013-03-28 21:35:03 +0100172EXPORT_SYMBOL(tegra_powergate_sequence_power_up);
Colin Crossce1e3262010-05-24 17:07:46 -0700173
Peter De Schrijver65fe31d2012-02-10 01:47:49 +0200174int tegra_cpu_powergate_id(int cpuid)
175{
176 if (cpuid > 0 && cpuid < tegra_num_cpu_domains)
177 return tegra_cpu_domains[cpuid];
178
179 return -EINVAL;
180}
181
Peter De Schrijver8f5d6f1b2012-02-10 01:47:46 +0200182int __init tegra_powergate_init(void)
183{
184 switch (tegra_chip_id) {
185 case TEGRA20:
186 tegra_num_powerdomains = 7;
187 break;
Peter De Schrijver6cafa972012-02-10 01:47:48 +0200188 case TEGRA30:
189 tegra_num_powerdomains = 14;
Peter De Schrijver65fe31d2012-02-10 01:47:49 +0200190 tegra_num_cpu_domains = 4;
191 tegra_cpu_domains = tegra30_cpu_domains;
Peter De Schrijver6cafa972012-02-10 01:47:48 +0200192 break;
Peter De Schrijver8f5d6f1b2012-02-10 01:47:46 +0200193 default:
194 /* Unknown Tegra variant. Disable powergating */
195 tegra_num_powerdomains = 0;
196 break;
197 }
198
199 return 0;
200}
Peter De Schrijver8f5d6f1b2012-02-10 01:47:46 +0200201
Colin Crossce1e3262010-05-24 17:07:46 -0700202#ifdef CONFIG_DEBUG_FS
203
Peter De Schrijverb48d6aa2012-09-06 17:55:29 +0300204static const char * const *powergate_name;
205
206static const char * const powergate_name_t20[] = {
Colin Crossce1e3262010-05-24 17:07:46 -0700207 [TEGRA_POWERGATE_CPU] = "cpu",
208 [TEGRA_POWERGATE_3D] = "3d",
209 [TEGRA_POWERGATE_VENC] = "venc",
210 [TEGRA_POWERGATE_VDEC] = "vdec",
211 [TEGRA_POWERGATE_PCIE] = "pcie",
212 [TEGRA_POWERGATE_L2] = "l2",
213 [TEGRA_POWERGATE_MPE] = "mpe",
214};
215
Peter De Schrijverb48d6aa2012-09-06 17:55:29 +0300216static const char * const powergate_name_t30[] = {
217 [TEGRA_POWERGATE_CPU] = "cpu0",
218 [TEGRA_POWERGATE_3D] = "3d0",
219 [TEGRA_POWERGATE_VENC] = "venc",
220 [TEGRA_POWERGATE_VDEC] = "vdec",
221 [TEGRA_POWERGATE_PCIE] = "pcie",
222 [TEGRA_POWERGATE_L2] = "l2",
223 [TEGRA_POWERGATE_MPE] = "mpe",
224 [TEGRA_POWERGATE_HEG] = "heg",
225 [TEGRA_POWERGATE_SATA] = "sata",
226 [TEGRA_POWERGATE_CPU1] = "cpu1",
227 [TEGRA_POWERGATE_CPU2] = "cpu2",
228 [TEGRA_POWERGATE_CPU3] = "cpu3",
229 [TEGRA_POWERGATE_CELP] = "celp",
230 [TEGRA_POWERGATE_3D1] = "3d1",
231};
232
Colin Crossce1e3262010-05-24 17:07:46 -0700233static int powergate_show(struct seq_file *s, void *data)
234{
235 int i;
236
237 seq_printf(s, " powergate powered\n");
238 seq_printf(s, "------------------\n");
239
Peter De Schrijver8f5d6f1b2012-02-10 01:47:46 +0200240 for (i = 0; i < tegra_num_powerdomains; i++)
Colin Crossce1e3262010-05-24 17:07:46 -0700241 seq_printf(s, " %9s %7s\n", powergate_name[i],
242 tegra_powergate_is_powered(i) ? "yes" : "no");
243 return 0;
244}
245
246static int powergate_open(struct inode *inode, struct file *file)
247{
248 return single_open(file, powergate_show, inode->i_private);
249}
250
251static const struct file_operations powergate_fops = {
252 .open = powergate_open,
253 .read = seq_read,
254 .llseek = seq_lseek,
255 .release = single_release,
256};
257
Shawn Guo390e0cf2012-05-02 17:08:06 +0800258int __init tegra_powergate_debugfs_init(void)
Colin Crossce1e3262010-05-24 17:07:46 -0700259{
260 struct dentry *d;
Colin Crossce1e3262010-05-24 17:07:46 -0700261
Peter De Schrijverb48d6aa2012-09-06 17:55:29 +0300262 switch (tegra_chip_id) {
263 case TEGRA20:
264 powergate_name = powergate_name_t20;
265 break;
266 case TEGRA30:
267 powergate_name = powergate_name_t30;
268 break;
269 }
270
271 if (powergate_name) {
272 d = debugfs_create_file("powergate", S_IRUGO, NULL, NULL,
273 &powergate_fops);
274 if (!d)
275 return -ENOMEM;
276 }
Colin Crossce1e3262010-05-24 17:07:46 -0700277
Peter De Schrijverf858b6f2012-09-06 17:55:28 +0300278 return 0;
Colin Crossce1e3262010-05-24 17:07:46 -0700279}
280
Colin Crossce1e3262010-05-24 17:07:46 -0700281#endif