blob: e8ccb297333313e2ba8eac596987a83c83084245 [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>
Stephen Warren80b28792013-11-06 15:45:46 -070028#include <linux/reset.h>
Colin Crossce1e3262010-05-24 17:07:46 -070029#include <linux/seq_file.h>
30#include <linux/spinlock.h>
Prashant Gaikwad61fd2902013-01-11 13:16:26 +053031#include <linux/clk/tegra.h>
Stephen Warrene4bcda22013-03-29 17:38:18 -060032#include <linux/tegra-powergate.h>
Colin Crossce1e3262010-05-24 17:07:46 -070033
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;
Thierry Redingf0ea2e02013-10-16 19:19:01 +020046static const u8 *tegra_cpu_domains;
47
48static const u8 tegra30_cpu_domains[] = {
Thierry Redingbd6a9dd2013-10-16 19:19:02 +020049 TEGRA_POWERGATE_CPU,
50 TEGRA_POWERGATE_CPU1,
51 TEGRA_POWERGATE_CPU2,
52 TEGRA_POWERGATE_CPU3,
53};
54
55static const u8 tegra114_cpu_domains[] = {
Peter De Schrijver65fe31d2012-02-10 01:47:49 +020056 TEGRA_POWERGATE_CPU0,
57 TEGRA_POWERGATE_CPU1,
58 TEGRA_POWERGATE_CPU2,
59 TEGRA_POWERGATE_CPU3,
60};
Peter De Schrijver8f5d6f1b2012-02-10 01:47:46 +020061
Colin Crossce1e3262010-05-24 17:07:46 -070062static DEFINE_SPINLOCK(tegra_powergate_lock);
63
64static void __iomem *pmc = IO_ADDRESS(TEGRA_PMC_BASE);
65
66static u32 pmc_read(unsigned long reg)
67{
68 return readl(pmc + reg);
69}
70
71static void pmc_write(u32 val, unsigned long reg)
72{
73 writel(val, pmc + reg);
74}
75
76static int tegra_powergate_set(int id, bool new_state)
77{
78 bool status;
79 unsigned long flags;
80
81 spin_lock_irqsave(&tegra_powergate_lock, flags);
82
83 status = pmc_read(PWRGATE_STATUS) & (1 << id);
84
85 if (status == new_state) {
86 spin_unlock_irqrestore(&tegra_powergate_lock, flags);
Thierry Redingeebd1fd2013-03-28 21:35:04 +010087 return 0;
Colin Crossce1e3262010-05-24 17:07:46 -070088 }
89
90 pmc_write(PWRGATE_TOGGLE_START | id, PWRGATE_TOGGLE);
91
92 spin_unlock_irqrestore(&tegra_powergate_lock, flags);
93
94 return 0;
95}
96
97int tegra_powergate_power_on(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, true);
103}
104
105int tegra_powergate_power_off(int id)
106{
Peter De Schrijver8f5d6f1b2012-02-10 01:47:46 +0200107 if (id < 0 || id >= tegra_num_powerdomains)
Colin Crossce1e3262010-05-24 17:07:46 -0700108 return -EINVAL;
109
110 return tegra_powergate_set(id, false);
111}
Thierry Reding44374af2013-12-06 16:49:55 +0100112EXPORT_SYMBOL(tegra_powergate_power_off);
Colin Crossce1e3262010-05-24 17:07:46 -0700113
Peter De Schrijver6ac8cb52012-02-10 01:47:47 +0200114int tegra_powergate_is_powered(int id)
Colin Crossce1e3262010-05-24 17:07:46 -0700115{
116 u32 status;
117
Peter De Schrijver8f5d6f1b2012-02-10 01:47:46 +0200118 if (id < 0 || id >= tegra_num_powerdomains)
119 return -EINVAL;
Colin Crossce1e3262010-05-24 17:07:46 -0700120
121 status = pmc_read(PWRGATE_STATUS) & (1 << id);
122 return !!status;
123}
124
125int tegra_powergate_remove_clamping(int id)
126{
127 u32 mask;
128
Peter De Schrijver8f5d6f1b2012-02-10 01:47:46 +0200129 if (id < 0 || id >= tegra_num_powerdomains)
Colin Crossce1e3262010-05-24 17:07:46 -0700130 return -EINVAL;
131
132 /*
133 * Tegra 2 has a bug where PCIE and VDE clamping masks are
134 * swapped relatively to the partition ids
135 */
Thierry Reding7e25eb02013-12-06 16:27:12 +0100136 if (id == TEGRA_POWERGATE_VDEC)
Colin Crossce1e3262010-05-24 17:07:46 -0700137 mask = (1 << TEGRA_POWERGATE_PCIE);
Thierry Reding7e25eb02013-12-06 16:27:12 +0100138 else if (id == TEGRA_POWERGATE_PCIE)
Colin Crossce1e3262010-05-24 17:07:46 -0700139 mask = (1 << TEGRA_POWERGATE_VDEC);
140 else
141 mask = (1 << id);
142
143 pmc_write(mask, REMOVE_CLAMPING);
144
145 return 0;
146}
147
148/* Must be called with clk disabled, and returns with clk enabled */
Stephen Warren80b28792013-11-06 15:45:46 -0700149int tegra_powergate_sequence_power_up(int id, struct clk *clk,
150 struct reset_control *rst)
Colin Crossce1e3262010-05-24 17:07:46 -0700151{
152 int ret;
153
Stephen Warren80b28792013-11-06 15:45:46 -0700154 reset_control_assert(rst);
Colin Crossce1e3262010-05-24 17:07:46 -0700155
156 ret = tegra_powergate_power_on(id);
157 if (ret)
158 goto err_power;
159
Prashant Gaikwad6a5278d2012-06-05 09:59:35 +0530160 ret = clk_prepare_enable(clk);
Colin Crossce1e3262010-05-24 17:07:46 -0700161 if (ret)
162 goto err_clk;
163
164 udelay(10);
165
166 ret = tegra_powergate_remove_clamping(id);
167 if (ret)
168 goto err_clamp;
169
170 udelay(10);
Stephen Warren80b28792013-11-06 15:45:46 -0700171 reset_control_deassert(rst);
Colin Crossce1e3262010-05-24 17:07:46 -0700172
173 return 0;
174
175err_clamp:
Prashant Gaikwad6a5278d2012-06-05 09:59:35 +0530176 clk_disable_unprepare(clk);
Colin Crossce1e3262010-05-24 17:07:46 -0700177err_clk:
178 tegra_powergate_power_off(id);
179err_power:
180 return ret;
181}
Thierry Reding99f69fe2013-03-28 21:35:03 +0100182EXPORT_SYMBOL(tegra_powergate_sequence_power_up);
Colin Crossce1e3262010-05-24 17:07:46 -0700183
Peter De Schrijver65fe31d2012-02-10 01:47:49 +0200184int tegra_cpu_powergate_id(int cpuid)
185{
186 if (cpuid > 0 && cpuid < tegra_num_cpu_domains)
187 return tegra_cpu_domains[cpuid];
188
189 return -EINVAL;
190}
191
Peter De Schrijver8f5d6f1b2012-02-10 01:47:46 +0200192int __init tegra_powergate_init(void)
193{
194 switch (tegra_chip_id) {
195 case TEGRA20:
196 tegra_num_powerdomains = 7;
197 break;
Peter De Schrijver6cafa972012-02-10 01:47:48 +0200198 case TEGRA30:
199 tegra_num_powerdomains = 14;
Peter De Schrijver65fe31d2012-02-10 01:47:49 +0200200 tegra_num_cpu_domains = 4;
201 tegra_cpu_domains = tegra30_cpu_domains;
Peter De Schrijver6cafa972012-02-10 01:47:48 +0200202 break;
Thierry Redingbd6a9dd2013-10-16 19:19:02 +0200203 case TEGRA114:
204 tegra_num_powerdomains = 23;
205 tegra_num_cpu_domains = 4;
206 tegra_cpu_domains = tegra114_cpu_domains;
207 break;
Peter De Schrijver8f5d6f1b2012-02-10 01:47:46 +0200208 default:
209 /* Unknown Tegra variant. Disable powergating */
210 tegra_num_powerdomains = 0;
211 break;
212 }
213
214 return 0;
215}
Peter De Schrijver8f5d6f1b2012-02-10 01:47:46 +0200216
Colin Crossce1e3262010-05-24 17:07:46 -0700217#ifdef CONFIG_DEBUG_FS
218
Peter De Schrijverb48d6aa2012-09-06 17:55:29 +0300219static const char * const *powergate_name;
220
221static const char * const powergate_name_t20[] = {
Colin Crossce1e3262010-05-24 17:07:46 -0700222 [TEGRA_POWERGATE_CPU] = "cpu",
223 [TEGRA_POWERGATE_3D] = "3d",
224 [TEGRA_POWERGATE_VENC] = "venc",
225 [TEGRA_POWERGATE_VDEC] = "vdec",
226 [TEGRA_POWERGATE_PCIE] = "pcie",
227 [TEGRA_POWERGATE_L2] = "l2",
228 [TEGRA_POWERGATE_MPE] = "mpe",
229};
230
Peter De Schrijverb48d6aa2012-09-06 17:55:29 +0300231static const char * const powergate_name_t30[] = {
232 [TEGRA_POWERGATE_CPU] = "cpu0",
233 [TEGRA_POWERGATE_3D] = "3d0",
234 [TEGRA_POWERGATE_VENC] = "venc",
235 [TEGRA_POWERGATE_VDEC] = "vdec",
236 [TEGRA_POWERGATE_PCIE] = "pcie",
237 [TEGRA_POWERGATE_L2] = "l2",
238 [TEGRA_POWERGATE_MPE] = "mpe",
239 [TEGRA_POWERGATE_HEG] = "heg",
240 [TEGRA_POWERGATE_SATA] = "sata",
241 [TEGRA_POWERGATE_CPU1] = "cpu1",
242 [TEGRA_POWERGATE_CPU2] = "cpu2",
243 [TEGRA_POWERGATE_CPU3] = "cpu3",
244 [TEGRA_POWERGATE_CELP] = "celp",
245 [TEGRA_POWERGATE_3D1] = "3d1",
246};
247
Thierry Redingbd6a9dd2013-10-16 19:19:02 +0200248static const char * const powergate_name_t114[] = {
Thierry Redingccab7982013-12-06 16:27:13 +0100249 [TEGRA_POWERGATE_CPU] = "crail",
Thierry Redingbd6a9dd2013-10-16 19:19:02 +0200250 [TEGRA_POWERGATE_3D] = "3d",
251 [TEGRA_POWERGATE_VENC] = "venc",
252 [TEGRA_POWERGATE_VDEC] = "vdec",
253 [TEGRA_POWERGATE_MPE] = "mpe",
254 [TEGRA_POWERGATE_HEG] = "heg",
255 [TEGRA_POWERGATE_CPU1] = "cpu1",
256 [TEGRA_POWERGATE_CPU2] = "cpu2",
257 [TEGRA_POWERGATE_CPU3] = "cpu3",
258 [TEGRA_POWERGATE_CELP] = "celp",
259 [TEGRA_POWERGATE_CPU0] = "cpu0",
260 [TEGRA_POWERGATE_C0NC] = "c0nc",
261 [TEGRA_POWERGATE_C1NC] = "c1nc",
262 [TEGRA_POWERGATE_DIS] = "dis",
263 [TEGRA_POWERGATE_DISB] = "disb",
264 [TEGRA_POWERGATE_XUSBA] = "xusba",
265 [TEGRA_POWERGATE_XUSBB] = "xusbb",
266 [TEGRA_POWERGATE_XUSBC] = "xusbc",
267};
268
Colin Crossce1e3262010-05-24 17:07:46 -0700269static int powergate_show(struct seq_file *s, void *data)
270{
271 int i;
272
273 seq_printf(s, " powergate powered\n");
274 seq_printf(s, "------------------\n");
275
Thierry Redingbd6a9dd2013-10-16 19:19:02 +0200276 for (i = 0; i < tegra_num_powerdomains; i++) {
277 if (!powergate_name[i])
278 continue;
279
Colin Crossce1e3262010-05-24 17:07:46 -0700280 seq_printf(s, " %9s %7s\n", powergate_name[i],
281 tegra_powergate_is_powered(i) ? "yes" : "no");
Thierry Redingbd6a9dd2013-10-16 19:19:02 +0200282 }
283
Colin Crossce1e3262010-05-24 17:07:46 -0700284 return 0;
285}
286
287static int powergate_open(struct inode *inode, struct file *file)
288{
289 return single_open(file, powergate_show, inode->i_private);
290}
291
292static const struct file_operations powergate_fops = {
293 .open = powergate_open,
294 .read = seq_read,
295 .llseek = seq_lseek,
296 .release = single_release,
297};
298
Shawn Guo390e0cf2012-05-02 17:08:06 +0800299int __init tegra_powergate_debugfs_init(void)
Colin Crossce1e3262010-05-24 17:07:46 -0700300{
301 struct dentry *d;
Colin Crossce1e3262010-05-24 17:07:46 -0700302
Peter De Schrijverb48d6aa2012-09-06 17:55:29 +0300303 switch (tegra_chip_id) {
304 case TEGRA20:
305 powergate_name = powergate_name_t20;
306 break;
307 case TEGRA30:
308 powergate_name = powergate_name_t30;
309 break;
Thierry Redingbd6a9dd2013-10-16 19:19:02 +0200310 case TEGRA114:
311 powergate_name = powergate_name_t114;
312 break;
Peter De Schrijverb48d6aa2012-09-06 17:55:29 +0300313 }
314
315 if (powergate_name) {
316 d = debugfs_create_file("powergate", S_IRUGO, NULL, NULL,
317 &powergate_fops);
318 if (!d)
319 return -ENOMEM;
320 }
Colin Crossce1e3262010-05-24 17:07:46 -0700321
Peter De Schrijverf858b6f2012-09-06 17:55:28 +0300322 return 0;
Colin Crossce1e3262010-05-24 17:07:46 -0700323}
324
Colin Crossce1e3262010-05-24 17:07:46 -0700325#endif