Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 1 | /* |
| 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 Reding | 99f69fe | 2013-03-28 21:35:03 +0100 | [diff] [blame] | 25 | #include <linux/export.h> |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 26 | #include <linux/init.h> |
| 27 | #include <linux/io.h> |
Stephen Warren | 80b2879 | 2013-11-06 15:45:46 -0700 | [diff] [blame] | 28 | #include <linux/reset.h> |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 29 | #include <linux/seq_file.h> |
| 30 | #include <linux/spinlock.h> |
Prashant Gaikwad | 61fd290 | 2013-01-11 13:16:26 +0530 | [diff] [blame] | 31 | #include <linux/clk/tegra.h> |
Stephen Warren | e4bcda2 | 2013-03-29 17:38:18 -0600 | [diff] [blame] | 32 | #include <linux/tegra-powergate.h> |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 33 | |
Peter De Schrijver | 8f5d6f1b | 2012-02-10 01:47:46 +0200 | [diff] [blame] | 34 | #include "fuse.h" |
Stephen Warren | 2be39c0 | 2012-10-04 14:24:09 -0600 | [diff] [blame] | 35 | #include "iomap.h" |
Peter De Schrijver | 8f5d6f1b | 2012-02-10 01:47:46 +0200 | [diff] [blame] | 36 | |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 37 | #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 Schrijver | 8f5d6f1b | 2012-02-10 01:47:46 +0200 | [diff] [blame] | 44 | static int tegra_num_powerdomains; |
Peter De Schrijver | 65fe31d | 2012-02-10 01:47:49 +0200 | [diff] [blame] | 45 | static int tegra_num_cpu_domains; |
Thierry Reding | f0ea2e0 | 2013-10-16 19:19:01 +0200 | [diff] [blame] | 46 | static const u8 *tegra_cpu_domains; |
| 47 | |
| 48 | static const u8 tegra30_cpu_domains[] = { |
Thierry Reding | bd6a9dd | 2013-10-16 19:19:02 +0200 | [diff] [blame] | 49 | TEGRA_POWERGATE_CPU, |
| 50 | TEGRA_POWERGATE_CPU1, |
| 51 | TEGRA_POWERGATE_CPU2, |
| 52 | TEGRA_POWERGATE_CPU3, |
| 53 | }; |
| 54 | |
| 55 | static const u8 tegra114_cpu_domains[] = { |
Peter De Schrijver | 65fe31d | 2012-02-10 01:47:49 +0200 | [diff] [blame] | 56 | TEGRA_POWERGATE_CPU0, |
| 57 | TEGRA_POWERGATE_CPU1, |
| 58 | TEGRA_POWERGATE_CPU2, |
| 59 | TEGRA_POWERGATE_CPU3, |
| 60 | }; |
Peter De Schrijver | 8f5d6f1b | 2012-02-10 01:47:46 +0200 | [diff] [blame] | 61 | |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 62 | static DEFINE_SPINLOCK(tegra_powergate_lock); |
| 63 | |
| 64 | static void __iomem *pmc = IO_ADDRESS(TEGRA_PMC_BASE); |
| 65 | |
| 66 | static u32 pmc_read(unsigned long reg) |
| 67 | { |
| 68 | return readl(pmc + reg); |
| 69 | } |
| 70 | |
| 71 | static void pmc_write(u32 val, unsigned long reg) |
| 72 | { |
| 73 | writel(val, pmc + reg); |
| 74 | } |
| 75 | |
| 76 | static 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 Reding | eebd1fd | 2013-03-28 21:35:04 +0100 | [diff] [blame] | 87 | return 0; |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 88 | } |
| 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 | |
| 97 | int tegra_powergate_power_on(int id) |
| 98 | { |
Peter De Schrijver | 8f5d6f1b | 2012-02-10 01:47:46 +0200 | [diff] [blame] | 99 | if (id < 0 || id >= tegra_num_powerdomains) |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 100 | return -EINVAL; |
| 101 | |
| 102 | return tegra_powergate_set(id, true); |
| 103 | } |
| 104 | |
| 105 | int tegra_powergate_power_off(int id) |
| 106 | { |
Peter De Schrijver | 8f5d6f1b | 2012-02-10 01:47:46 +0200 | [diff] [blame] | 107 | if (id < 0 || id >= tegra_num_powerdomains) |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 108 | return -EINVAL; |
| 109 | |
| 110 | return tegra_powergate_set(id, false); |
| 111 | } |
Thierry Reding | 44374af | 2013-12-06 16:49:55 +0100 | [diff] [blame^] | 112 | EXPORT_SYMBOL(tegra_powergate_power_off); |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 113 | |
Peter De Schrijver | 6ac8cb5 | 2012-02-10 01:47:47 +0200 | [diff] [blame] | 114 | int tegra_powergate_is_powered(int id) |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 115 | { |
| 116 | u32 status; |
| 117 | |
Peter De Schrijver | 8f5d6f1b | 2012-02-10 01:47:46 +0200 | [diff] [blame] | 118 | if (id < 0 || id >= tegra_num_powerdomains) |
| 119 | return -EINVAL; |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 120 | |
| 121 | status = pmc_read(PWRGATE_STATUS) & (1 << id); |
| 122 | return !!status; |
| 123 | } |
| 124 | |
| 125 | int tegra_powergate_remove_clamping(int id) |
| 126 | { |
| 127 | u32 mask; |
| 128 | |
Peter De Schrijver | 8f5d6f1b | 2012-02-10 01:47:46 +0200 | [diff] [blame] | 129 | if (id < 0 || id >= tegra_num_powerdomains) |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 130 | 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 Reding | 7e25eb0 | 2013-12-06 16:27:12 +0100 | [diff] [blame] | 136 | if (id == TEGRA_POWERGATE_VDEC) |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 137 | mask = (1 << TEGRA_POWERGATE_PCIE); |
Thierry Reding | 7e25eb0 | 2013-12-06 16:27:12 +0100 | [diff] [blame] | 138 | else if (id == TEGRA_POWERGATE_PCIE) |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 139 | 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 Warren | 80b2879 | 2013-11-06 15:45:46 -0700 | [diff] [blame] | 149 | int tegra_powergate_sequence_power_up(int id, struct clk *clk, |
| 150 | struct reset_control *rst) |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 151 | { |
| 152 | int ret; |
| 153 | |
Stephen Warren | 80b2879 | 2013-11-06 15:45:46 -0700 | [diff] [blame] | 154 | reset_control_assert(rst); |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 155 | |
| 156 | ret = tegra_powergate_power_on(id); |
| 157 | if (ret) |
| 158 | goto err_power; |
| 159 | |
Prashant Gaikwad | 6a5278d | 2012-06-05 09:59:35 +0530 | [diff] [blame] | 160 | ret = clk_prepare_enable(clk); |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 161 | 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 Warren | 80b2879 | 2013-11-06 15:45:46 -0700 | [diff] [blame] | 171 | reset_control_deassert(rst); |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 172 | |
| 173 | return 0; |
| 174 | |
| 175 | err_clamp: |
Prashant Gaikwad | 6a5278d | 2012-06-05 09:59:35 +0530 | [diff] [blame] | 176 | clk_disable_unprepare(clk); |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 177 | err_clk: |
| 178 | tegra_powergate_power_off(id); |
| 179 | err_power: |
| 180 | return ret; |
| 181 | } |
Thierry Reding | 99f69fe | 2013-03-28 21:35:03 +0100 | [diff] [blame] | 182 | EXPORT_SYMBOL(tegra_powergate_sequence_power_up); |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 183 | |
Peter De Schrijver | 65fe31d | 2012-02-10 01:47:49 +0200 | [diff] [blame] | 184 | int 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 Schrijver | 8f5d6f1b | 2012-02-10 01:47:46 +0200 | [diff] [blame] | 192 | int __init tegra_powergate_init(void) |
| 193 | { |
| 194 | switch (tegra_chip_id) { |
| 195 | case TEGRA20: |
| 196 | tegra_num_powerdomains = 7; |
| 197 | break; |
Peter De Schrijver | 6cafa97 | 2012-02-10 01:47:48 +0200 | [diff] [blame] | 198 | case TEGRA30: |
| 199 | tegra_num_powerdomains = 14; |
Peter De Schrijver | 65fe31d | 2012-02-10 01:47:49 +0200 | [diff] [blame] | 200 | tegra_num_cpu_domains = 4; |
| 201 | tegra_cpu_domains = tegra30_cpu_domains; |
Peter De Schrijver | 6cafa97 | 2012-02-10 01:47:48 +0200 | [diff] [blame] | 202 | break; |
Thierry Reding | bd6a9dd | 2013-10-16 19:19:02 +0200 | [diff] [blame] | 203 | case TEGRA114: |
| 204 | tegra_num_powerdomains = 23; |
| 205 | tegra_num_cpu_domains = 4; |
| 206 | tegra_cpu_domains = tegra114_cpu_domains; |
| 207 | break; |
Peter De Schrijver | 8f5d6f1b | 2012-02-10 01:47:46 +0200 | [diff] [blame] | 208 | default: |
| 209 | /* Unknown Tegra variant. Disable powergating */ |
| 210 | tegra_num_powerdomains = 0; |
| 211 | break; |
| 212 | } |
| 213 | |
| 214 | return 0; |
| 215 | } |
Peter De Schrijver | 8f5d6f1b | 2012-02-10 01:47:46 +0200 | [diff] [blame] | 216 | |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 217 | #ifdef CONFIG_DEBUG_FS |
| 218 | |
Peter De Schrijver | b48d6aa | 2012-09-06 17:55:29 +0300 | [diff] [blame] | 219 | static const char * const *powergate_name; |
| 220 | |
| 221 | static const char * const powergate_name_t20[] = { |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 222 | [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 Schrijver | b48d6aa | 2012-09-06 17:55:29 +0300 | [diff] [blame] | 231 | static 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 Reding | bd6a9dd | 2013-10-16 19:19:02 +0200 | [diff] [blame] | 248 | static const char * const powergate_name_t114[] = { |
Thierry Reding | ccab798 | 2013-12-06 16:27:13 +0100 | [diff] [blame] | 249 | [TEGRA_POWERGATE_CPU] = "crail", |
Thierry Reding | bd6a9dd | 2013-10-16 19:19:02 +0200 | [diff] [blame] | 250 | [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 Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 269 | static 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 Reding | bd6a9dd | 2013-10-16 19:19:02 +0200 | [diff] [blame] | 276 | for (i = 0; i < tegra_num_powerdomains; i++) { |
| 277 | if (!powergate_name[i]) |
| 278 | continue; |
| 279 | |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 280 | seq_printf(s, " %9s %7s\n", powergate_name[i], |
| 281 | tegra_powergate_is_powered(i) ? "yes" : "no"); |
Thierry Reding | bd6a9dd | 2013-10-16 19:19:02 +0200 | [diff] [blame] | 282 | } |
| 283 | |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | static int powergate_open(struct inode *inode, struct file *file) |
| 288 | { |
| 289 | return single_open(file, powergate_show, inode->i_private); |
| 290 | } |
| 291 | |
| 292 | static 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 Guo | 390e0cf | 2012-05-02 17:08:06 +0800 | [diff] [blame] | 299 | int __init tegra_powergate_debugfs_init(void) |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 300 | { |
| 301 | struct dentry *d; |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 302 | |
Peter De Schrijver | b48d6aa | 2012-09-06 17:55:29 +0300 | [diff] [blame] | 303 | 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 Reding | bd6a9dd | 2013-10-16 19:19:02 +0200 | [diff] [blame] | 310 | case TEGRA114: |
| 311 | powergate_name = powergate_name_t114; |
| 312 | break; |
Peter De Schrijver | b48d6aa | 2012-09-06 17:55:29 +0300 | [diff] [blame] | 313 | } |
| 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 Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 321 | |
Peter De Schrijver | f858b6f | 2012-09-06 17:55:28 +0300 | [diff] [blame] | 322 | return 0; |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 325 | #endif |