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 DPD_SAMPLE 0x020
38 #define DPD_SAMPLE_ENABLE (1 << 0)
39 #define DPD_SAMPLE_DISABLE (0 << 0)
41 #define PWRGATE_TOGGLE 0x30
42 #define PWRGATE_TOGGLE_START (1 << 8)
44 #define REMOVE_CLAMPING 0x34
46 #define PWRGATE_STATUS 0x38
48 #define IO_DPD_REQ 0x1b8
49 #define IO_DPD_REQ_CODE_IDLE (0 << 30)
50 #define IO_DPD_REQ_CODE_OFF (1 << 30)
51 #define IO_DPD_REQ_CODE_ON (2 << 30)
52 #define IO_DPD_REQ_CODE_MASK (3 << 30)
54 #define IO_DPD_STATUS 0x1bc
55 #define IO_DPD2_REQ 0x1c0
56 #define IO_DPD2_STATUS 0x1c4
57 #define SEL_DPD_TIM 0x1c8
59 #define GPU_RG_CNTRL 0x2d4
61 static int tegra_num_powerdomains
;
62 static int tegra_num_cpu_domains
;
63 static const u8
*tegra_cpu_domains
;
65 static const u8 tegra30_cpu_domains
[] = {
72 static const u8 tegra114_cpu_domains
[] = {
79 static const u8 tegra124_cpu_domains
[] = {
86 static DEFINE_SPINLOCK(tegra_powergate_lock
);
88 static void __iomem
*pmc
= IO_ADDRESS(TEGRA_PMC_BASE
);
90 static u32
pmc_read(unsigned long reg
)
92 return readl(pmc
+ reg
);
95 static void pmc_write(u32 val
, unsigned long reg
)
97 writel(val
, pmc
+ reg
);
100 static int tegra_powergate_set(int id
, bool new_state
)
105 spin_lock_irqsave(&tegra_powergate_lock
, flags
);
107 status
= pmc_read(PWRGATE_STATUS
) & (1 << id
);
109 if (status
== new_state
) {
110 spin_unlock_irqrestore(&tegra_powergate_lock
, flags
);
114 pmc_write(PWRGATE_TOGGLE_START
| id
, PWRGATE_TOGGLE
);
116 spin_unlock_irqrestore(&tegra_powergate_lock
, flags
);
121 int tegra_powergate_power_on(int id
)
123 if (id
< 0 || id
>= tegra_num_powerdomains
)
126 return tegra_powergate_set(id
, true);
129 int tegra_powergate_power_off(int id
)
131 if (id
< 0 || id
>= tegra_num_powerdomains
)
134 return tegra_powergate_set(id
, false);
136 EXPORT_SYMBOL(tegra_powergate_power_off
);
138 int tegra_powergate_is_powered(int id
)
142 if (id
< 0 || id
>= tegra_num_powerdomains
)
145 status
= pmc_read(PWRGATE_STATUS
) & (1 << id
);
149 int tegra_powergate_remove_clamping(int id
)
153 if (id
< 0 || id
>= tegra_num_powerdomains
)
157 * The Tegra124 GPU has a separate register (with different semantics)
160 if (tegra_chip_id
== TEGRA124
) {
161 if (id
== TEGRA_POWERGATE_3D
) {
162 pmc_write(0, GPU_RG_CNTRL
);
168 * Tegra 2 has a bug where PCIE and VDE clamping masks are
169 * swapped relatively to the partition ids
171 if (id
== TEGRA_POWERGATE_VDEC
)
172 mask
= (1 << TEGRA_POWERGATE_PCIE
);
173 else if (id
== TEGRA_POWERGATE_PCIE
)
174 mask
= (1 << TEGRA_POWERGATE_VDEC
);
178 pmc_write(mask
, REMOVE_CLAMPING
);
182 EXPORT_SYMBOL(tegra_powergate_remove_clamping
);
184 /* Must be called with clk disabled, and returns with clk enabled */
185 int tegra_powergate_sequence_power_up(int id
, struct clk
*clk
,
186 struct reset_control
*rst
)
190 reset_control_assert(rst
);
192 ret
= tegra_powergate_power_on(id
);
196 ret
= clk_prepare_enable(clk
);
202 ret
= tegra_powergate_remove_clamping(id
);
207 reset_control_deassert(rst
);
212 clk_disable_unprepare(clk
);
214 tegra_powergate_power_off(id
);
218 EXPORT_SYMBOL(tegra_powergate_sequence_power_up
);
220 int tegra_cpu_powergate_id(int cpuid
)
222 if (cpuid
> 0 && cpuid
< tegra_num_cpu_domains
)
223 return tegra_cpu_domains
[cpuid
];
228 int __init
tegra_powergate_init(void)
230 switch (tegra_chip_id
) {
232 tegra_num_powerdomains
= 7;
235 tegra_num_powerdomains
= 14;
236 tegra_num_cpu_domains
= 4;
237 tegra_cpu_domains
= tegra30_cpu_domains
;
240 tegra_num_powerdomains
= 23;
241 tegra_num_cpu_domains
= 4;
242 tegra_cpu_domains
= tegra114_cpu_domains
;
245 tegra_num_powerdomains
= 25;
246 tegra_num_cpu_domains
= 4;
247 tegra_cpu_domains
= tegra124_cpu_domains
;
250 /* Unknown Tegra variant. Disable powergating */
251 tegra_num_powerdomains
= 0;
258 #ifdef CONFIG_DEBUG_FS
260 static const char * const *powergate_name
;
262 static const char * const powergate_name_t20
[] = {
263 [TEGRA_POWERGATE_CPU
] = "cpu",
264 [TEGRA_POWERGATE_3D
] = "3d",
265 [TEGRA_POWERGATE_VENC
] = "venc",
266 [TEGRA_POWERGATE_VDEC
] = "vdec",
267 [TEGRA_POWERGATE_PCIE
] = "pcie",
268 [TEGRA_POWERGATE_L2
] = "l2",
269 [TEGRA_POWERGATE_MPE
] = "mpe",
272 static const char * const powergate_name_t30
[] = {
273 [TEGRA_POWERGATE_CPU
] = "cpu0",
274 [TEGRA_POWERGATE_3D
] = "3d0",
275 [TEGRA_POWERGATE_VENC
] = "venc",
276 [TEGRA_POWERGATE_VDEC
] = "vdec",
277 [TEGRA_POWERGATE_PCIE
] = "pcie",
278 [TEGRA_POWERGATE_L2
] = "l2",
279 [TEGRA_POWERGATE_MPE
] = "mpe",
280 [TEGRA_POWERGATE_HEG
] = "heg",
281 [TEGRA_POWERGATE_SATA
] = "sata",
282 [TEGRA_POWERGATE_CPU1
] = "cpu1",
283 [TEGRA_POWERGATE_CPU2
] = "cpu2",
284 [TEGRA_POWERGATE_CPU3
] = "cpu3",
285 [TEGRA_POWERGATE_CELP
] = "celp",
286 [TEGRA_POWERGATE_3D1
] = "3d1",
289 static const char * const powergate_name_t114
[] = {
290 [TEGRA_POWERGATE_CPU
] = "crail",
291 [TEGRA_POWERGATE_3D
] = "3d",
292 [TEGRA_POWERGATE_VENC
] = "venc",
293 [TEGRA_POWERGATE_VDEC
] = "vdec",
294 [TEGRA_POWERGATE_MPE
] = "mpe",
295 [TEGRA_POWERGATE_HEG
] = "heg",
296 [TEGRA_POWERGATE_CPU1
] = "cpu1",
297 [TEGRA_POWERGATE_CPU2
] = "cpu2",
298 [TEGRA_POWERGATE_CPU3
] = "cpu3",
299 [TEGRA_POWERGATE_CELP
] = "celp",
300 [TEGRA_POWERGATE_CPU0
] = "cpu0",
301 [TEGRA_POWERGATE_C0NC
] = "c0nc",
302 [TEGRA_POWERGATE_C1NC
] = "c1nc",
303 [TEGRA_POWERGATE_DIS
] = "dis",
304 [TEGRA_POWERGATE_DISB
] = "disb",
305 [TEGRA_POWERGATE_XUSBA
] = "xusba",
306 [TEGRA_POWERGATE_XUSBB
] = "xusbb",
307 [TEGRA_POWERGATE_XUSBC
] = "xusbc",
310 static const char * const powergate_name_t124
[] = {
311 [TEGRA_POWERGATE_CPU
] = "crail",
312 [TEGRA_POWERGATE_3D
] = "3d",
313 [TEGRA_POWERGATE_VENC
] = "venc",
314 [TEGRA_POWERGATE_PCIE
] = "pcie",
315 [TEGRA_POWERGATE_VDEC
] = "vdec",
316 [TEGRA_POWERGATE_L2
] = "l2",
317 [TEGRA_POWERGATE_MPE
] = "mpe",
318 [TEGRA_POWERGATE_HEG
] = "heg",
319 [TEGRA_POWERGATE_SATA
] = "sata",
320 [TEGRA_POWERGATE_CPU1
] = "cpu1",
321 [TEGRA_POWERGATE_CPU2
] = "cpu2",
322 [TEGRA_POWERGATE_CPU3
] = "cpu3",
323 [TEGRA_POWERGATE_CELP
] = "celp",
324 [TEGRA_POWERGATE_CPU0
] = "cpu0",
325 [TEGRA_POWERGATE_C0NC
] = "c0nc",
326 [TEGRA_POWERGATE_C1NC
] = "c1nc",
327 [TEGRA_POWERGATE_SOR
] = "sor",
328 [TEGRA_POWERGATE_DIS
] = "dis",
329 [TEGRA_POWERGATE_DISB
] = "disb",
330 [TEGRA_POWERGATE_XUSBA
] = "xusba",
331 [TEGRA_POWERGATE_XUSBB
] = "xusbb",
332 [TEGRA_POWERGATE_XUSBC
] = "xusbc",
333 [TEGRA_POWERGATE_VIC
] = "vic",
334 [TEGRA_POWERGATE_IRAM
] = "iram",
337 static int powergate_show(struct seq_file
*s
, void *data
)
341 seq_printf(s
, " powergate powered\n");
342 seq_printf(s
, "------------------\n");
344 for (i
= 0; i
< tegra_num_powerdomains
; i
++) {
345 if (!powergate_name
[i
])
348 seq_printf(s
, " %9s %7s\n", powergate_name
[i
],
349 tegra_powergate_is_powered(i
) ? "yes" : "no");
355 static int powergate_open(struct inode
*inode
, struct file
*file
)
357 return single_open(file
, powergate_show
, inode
->i_private
);
360 static const struct file_operations powergate_fops
= {
361 .open
= powergate_open
,
364 .release
= single_release
,
367 int __init
tegra_powergate_debugfs_init(void)
371 switch (tegra_chip_id
) {
373 powergate_name
= powergate_name_t20
;
376 powergate_name
= powergate_name_t30
;
379 powergate_name
= powergate_name_t114
;
382 powergate_name
= powergate_name_t124
;
386 if (powergate_name
) {
387 d
= debugfs_create_file("powergate", S_IRUGO
, NULL
, NULL
,
398 static int tegra_io_rail_prepare(int id
, unsigned long *request
,
399 unsigned long *status
, unsigned int *bit
)
401 unsigned long rate
, value
;
407 * There are two sets of 30 bits to select IO rails, but bits 30 and
408 * 31 are control bits rather than IO rail selection bits.
410 if (id
> 63 || *bit
== 30 || *bit
== 31)
414 *status
= IO_DPD_STATUS
;
415 *request
= IO_DPD_REQ
;
417 *status
= IO_DPD2_STATUS
;
418 *request
= IO_DPD2_REQ
;
421 clk
= clk_get_sys(NULL
, "pclk");
425 rate
= clk_get_rate(clk
);
428 pmc_write(DPD_SAMPLE_ENABLE
, DPD_SAMPLE
);
430 /* must be at least 200 ns, in APB (PCLK) clock cycles */
431 value
= DIV_ROUND_UP(1000000000, rate
);
432 value
= DIV_ROUND_UP(200, value
);
433 pmc_write(value
, SEL_DPD_TIM
);
438 static int tegra_io_rail_poll(unsigned long offset
, unsigned long mask
,
439 unsigned long val
, unsigned long timeout
)
443 timeout
= jiffies
+ msecs_to_jiffies(timeout
);
445 while (time_after(timeout
, jiffies
)) {
446 value
= pmc_read(offset
);
447 if ((value
& mask
) == val
)
450 usleep_range(250, 1000);
456 static void tegra_io_rail_unprepare(void)
458 pmc_write(DPD_SAMPLE_DISABLE
, DPD_SAMPLE
);
461 int tegra_io_rail_power_on(int id
)
463 unsigned long request
, status
, value
;
464 unsigned int bit
, mask
;
467 err
= tegra_io_rail_prepare(id
, &request
, &status
, &bit
);
473 value
= pmc_read(request
);
475 value
&= ~IO_DPD_REQ_CODE_MASK
;
476 value
|= IO_DPD_REQ_CODE_OFF
;
477 pmc_write(value
, request
);
479 err
= tegra_io_rail_poll(status
, mask
, 0, 250);
483 tegra_io_rail_unprepare();
488 int tegra_io_rail_power_off(int id
)
490 unsigned long request
, status
, value
;
491 unsigned int bit
, mask
;
494 err
= tegra_io_rail_prepare(id
, &request
, &status
, &bit
);
500 value
= pmc_read(request
);
502 value
&= ~IO_DPD_REQ_CODE_MASK
;
503 value
|= IO_DPD_REQ_CODE_ON
;
504 pmc_write(value
, request
);
506 err
= tegra_io_rail_poll(status
, mask
, mask
, 250);
510 tegra_io_rail_unprepare();