blob: b30e921cc3a9c88b5295cb825b9f3a162f1cd20c [file] [log] [blame]
Stephen Warrend3b8bdd2012-01-25 14:43:28 -07001/*
Joseph Lo291fde32013-02-28 21:32:10 +00002 * Copyright (C) 2012,2013 NVIDIA CORPORATION. All rights reserved.
Stephen Warrend3b8bdd2012-01-25 14:43:28 -07003 *
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/io.h>
20#include <linux/of.h>
Joseph Lo291fde32013-02-28 21:32:10 +000021#include <linux/of_address.h>
Stephen Warrend3b8bdd2012-01-25 14:43:28 -070022
Joseph Loc1417532013-02-28 21:32:11 +000023#define PMC_CTRL 0x0
24#define PMC_CTRL_INTR_LOW (1 << 17)
25#define PMC_PWRGATE_TOGGLE 0x30
26#define PMC_PWRGATE_TOGGLE_START (1 << 8)
27#define PMC_REMOVE_CLAMPING 0x34
28#define PMC_PWRGATE_STATUS 0x38
29
30#define TEGRA_POWERGATE_PCIE 3
31#define TEGRA_POWERGATE_VDEC 4
32#define TEGRA_POWERGATE_CPU1 9
33#define TEGRA_POWERGATE_CPU2 10
34#define TEGRA_POWERGATE_CPU3 11
35
36static u8 tegra_cpu_domains[] = {
37 0xFF, /* not available for CPU0 */
38 TEGRA_POWERGATE_CPU1,
39 TEGRA_POWERGATE_CPU2,
40 TEGRA_POWERGATE_CPU3,
41};
42static DEFINE_SPINLOCK(tegra_powergate_lock);
Stephen Warrend3b8bdd2012-01-25 14:43:28 -070043
Joseph Lo291fde32013-02-28 21:32:10 +000044static void __iomem *tegra_pmc_base;
45static bool tegra_pmc_invert_interrupt;
46
Stephen Warrend3b8bdd2012-01-25 14:43:28 -070047static inline u32 tegra_pmc_readl(u32 reg)
48{
Joseph Lo291fde32013-02-28 21:32:10 +000049 return readl(tegra_pmc_base + reg);
Stephen Warrend3b8bdd2012-01-25 14:43:28 -070050}
51
52static inline void tegra_pmc_writel(u32 val, u32 reg)
53{
Joseph Lo291fde32013-02-28 21:32:10 +000054 writel(val, tegra_pmc_base + reg);
Stephen Warrend3b8bdd2012-01-25 14:43:28 -070055}
56
Joseph Loc1417532013-02-28 21:32:11 +000057static int tegra_pmc_get_cpu_powerdomain_id(int cpuid)
58{
59 if (cpuid <= 0 || cpuid >= num_possible_cpus())
60 return -EINVAL;
61 return tegra_cpu_domains[cpuid];
62}
63
64static bool tegra_pmc_powergate_is_powered(int id)
65{
66 return (tegra_pmc_readl(PMC_PWRGATE_STATUS) >> id) & 1;
67}
68
69static int tegra_pmc_powergate_set(int id, bool new_state)
70{
71 bool old_state;
72 unsigned long flags;
73
74 spin_lock_irqsave(&tegra_powergate_lock, flags);
75
76 old_state = tegra_pmc_powergate_is_powered(id);
77 WARN_ON(old_state == new_state);
78
79 tegra_pmc_writel(PMC_PWRGATE_TOGGLE_START | id, PMC_PWRGATE_TOGGLE);
80
81 spin_unlock_irqrestore(&tegra_powergate_lock, flags);
82
83 return 0;
84}
85
86static int tegra_pmc_powergate_remove_clamping(int id)
87{
88 u32 mask;
89
90 /*
91 * Tegra has a bug where PCIE and VDE clamping masks are
92 * swapped relatively to the partition ids.
93 */
94 if (id == TEGRA_POWERGATE_VDEC)
95 mask = (1 << TEGRA_POWERGATE_PCIE);
96 else if (id == TEGRA_POWERGATE_PCIE)
97 mask = (1 << TEGRA_POWERGATE_VDEC);
98 else
99 mask = (1 << id);
100
101 tegra_pmc_writel(mask, PMC_REMOVE_CLAMPING);
102
103 return 0;
104}
105
106bool tegra_pmc_cpu_is_powered(int cpuid)
107{
108 int id;
109
110 id = tegra_pmc_get_cpu_powerdomain_id(cpuid);
111 if (id < 0)
112 return false;
113 return tegra_pmc_powergate_is_powered(id);
114}
115
116int tegra_pmc_cpu_power_on(int cpuid)
117{
118 int id;
119
120 id = tegra_pmc_get_cpu_powerdomain_id(cpuid);
121 if (id < 0)
122 return id;
123 return tegra_pmc_powergate_set(id, true);
124}
125
126int tegra_pmc_cpu_remove_clamping(int cpuid)
127{
128 int id;
129
130 id = tegra_pmc_get_cpu_powerdomain_id(cpuid);
131 if (id < 0)
132 return id;
133 return tegra_pmc_powergate_remove_clamping(id);
134}
135
Stephen Warrend3b8bdd2012-01-25 14:43:28 -0700136static const struct of_device_id matches[] __initconst = {
Joseph Lo88c4aba2013-02-26 16:27:42 +0000137 { .compatible = "nvidia,tegra114-pmc" },
138 { .compatible = "nvidia,tegra30-pmc" },
Stephen Warrend3b8bdd2012-01-25 14:43:28 -0700139 { .compatible = "nvidia,tegra20-pmc" },
140 { }
141};
Joseph Lo291fde32013-02-28 21:32:10 +0000142
143static void tegra_pmc_parse_dt(void)
144{
145 struct device_node *np;
146
147 np = of_find_matching_node(NULL, matches);
148 BUG_ON(!np);
149
150 tegra_pmc_base = of_iomap(np, 0);
151
152 tegra_pmc_invert_interrupt = of_property_read_bool(np,
153 "nvidia,invert-interrupt");
154}
Stephen Warrend3b8bdd2012-01-25 14:43:28 -0700155
156void __init tegra_pmc_init(void)
157{
Stephen Warrend3b8bdd2012-01-25 14:43:28 -0700158 u32 val;
159
Joseph Lo291fde32013-02-28 21:32:10 +0000160 tegra_pmc_parse_dt();
Stephen Warrend3b8bdd2012-01-25 14:43:28 -0700161
162 val = tegra_pmc_readl(PMC_CTRL);
Joseph Lo291fde32013-02-28 21:32:10 +0000163 if (tegra_pmc_invert_interrupt)
Stephen Warrend3b8bdd2012-01-25 14:43:28 -0700164 val |= PMC_CTRL_INTR_LOW;
165 else
166 val &= ~PMC_CTRL_INTR_LOW;
167 tegra_pmc_writel(val, PMC_CTRL);
168}