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> |
| 28 | #include <linux/seq_file.h> |
| 29 | #include <linux/spinlock.h> |
Prashant Gaikwad | 61fd290 | 2013-01-11 13:16:26 +0530 | [diff] [blame] | 30 | #include <linux/clk/tegra.h> |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 31 | |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 32 | #include <mach/powergate.h> |
| 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; |
| 46 | static u8 *tegra_cpu_domains; |
| 47 | static u8 tegra30_cpu_domains[] = { |
| 48 | TEGRA_POWERGATE_CPU0, |
| 49 | TEGRA_POWERGATE_CPU1, |
| 50 | TEGRA_POWERGATE_CPU2, |
| 51 | TEGRA_POWERGATE_CPU3, |
| 52 | }; |
Peter De Schrijver | 8f5d6f1b | 2012-02-10 01:47:46 +0200 | [diff] [blame] | 53 | |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 54 | static DEFINE_SPINLOCK(tegra_powergate_lock); |
| 55 | |
| 56 | static void __iomem *pmc = IO_ADDRESS(TEGRA_PMC_BASE); |
| 57 | |
| 58 | static u32 pmc_read(unsigned long reg) |
| 59 | { |
| 60 | return readl(pmc + reg); |
| 61 | } |
| 62 | |
| 63 | static void pmc_write(u32 val, unsigned long reg) |
| 64 | { |
| 65 | writel(val, pmc + reg); |
| 66 | } |
| 67 | |
| 68 | static 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 | |
| 89 | int tegra_powergate_power_on(int id) |
| 90 | { |
Peter De Schrijver | 8f5d6f1b | 2012-02-10 01:47:46 +0200 | [diff] [blame] | 91 | if (id < 0 || id >= tegra_num_powerdomains) |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 92 | return -EINVAL; |
| 93 | |
| 94 | return tegra_powergate_set(id, true); |
| 95 | } |
| 96 | |
| 97 | int tegra_powergate_power_off(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, false); |
| 103 | } |
| 104 | |
Peter De Schrijver | 6ac8cb5 | 2012-02-10 01:47:47 +0200 | [diff] [blame] | 105 | int tegra_powergate_is_powered(int id) |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 106 | { |
| 107 | u32 status; |
| 108 | |
Peter De Schrijver | 8f5d6f1b | 2012-02-10 01:47:46 +0200 | [diff] [blame] | 109 | if (id < 0 || id >= tegra_num_powerdomains) |
| 110 | return -EINVAL; |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 111 | |
| 112 | status = pmc_read(PWRGATE_STATUS) & (1 << id); |
| 113 | return !!status; |
| 114 | } |
| 115 | |
| 116 | int tegra_powergate_remove_clamping(int id) |
| 117 | { |
| 118 | u32 mask; |
| 119 | |
Peter De Schrijver | 8f5d6f1b | 2012-02-10 01:47:46 +0200 | [diff] [blame] | 120 | if (id < 0 || id >= tegra_num_powerdomains) |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 121 | 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 */ |
| 140 | int 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 Gaikwad | 6a5278d | 2012-06-05 09:59:35 +0530 | [diff] [blame] | 150 | ret = clk_prepare_enable(clk); |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 151 | 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 | |
| 165 | err_clamp: |
Prashant Gaikwad | 6a5278d | 2012-06-05 09:59:35 +0530 | [diff] [blame] | 166 | clk_disable_unprepare(clk); |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 167 | err_clk: |
| 168 | tegra_powergate_power_off(id); |
| 169 | err_power: |
| 170 | return ret; |
| 171 | } |
Thierry Reding | 99f69fe | 2013-03-28 21:35:03 +0100 | [diff] [blame^] | 172 | EXPORT_SYMBOL(tegra_powergate_sequence_power_up); |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 173 | |
Peter De Schrijver | 65fe31d | 2012-02-10 01:47:49 +0200 | [diff] [blame] | 174 | int 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 Schrijver | 8f5d6f1b | 2012-02-10 01:47:46 +0200 | [diff] [blame] | 182 | int __init tegra_powergate_init(void) |
| 183 | { |
| 184 | switch (tegra_chip_id) { |
| 185 | case TEGRA20: |
| 186 | tegra_num_powerdomains = 7; |
| 187 | break; |
Peter De Schrijver | 6cafa97 | 2012-02-10 01:47:48 +0200 | [diff] [blame] | 188 | case TEGRA30: |
| 189 | tegra_num_powerdomains = 14; |
Peter De Schrijver | 65fe31d | 2012-02-10 01:47:49 +0200 | [diff] [blame] | 190 | tegra_num_cpu_domains = 4; |
| 191 | tegra_cpu_domains = tegra30_cpu_domains; |
Peter De Schrijver | 6cafa97 | 2012-02-10 01:47:48 +0200 | [diff] [blame] | 192 | break; |
Peter De Schrijver | 8f5d6f1b | 2012-02-10 01:47:46 +0200 | [diff] [blame] | 193 | default: |
| 194 | /* Unknown Tegra variant. Disable powergating */ |
| 195 | tegra_num_powerdomains = 0; |
| 196 | break; |
| 197 | } |
| 198 | |
| 199 | return 0; |
| 200 | } |
Peter De Schrijver | 8f5d6f1b | 2012-02-10 01:47:46 +0200 | [diff] [blame] | 201 | |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 202 | #ifdef CONFIG_DEBUG_FS |
| 203 | |
Peter De Schrijver | b48d6aa | 2012-09-06 17:55:29 +0300 | [diff] [blame] | 204 | static const char * const *powergate_name; |
| 205 | |
| 206 | static const char * const powergate_name_t20[] = { |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 207 | [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 Schrijver | b48d6aa | 2012-09-06 17:55:29 +0300 | [diff] [blame] | 216 | static 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 Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 233 | static 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 Schrijver | 8f5d6f1b | 2012-02-10 01:47:46 +0200 | [diff] [blame] | 240 | for (i = 0; i < tegra_num_powerdomains; i++) |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 241 | seq_printf(s, " %9s %7s\n", powergate_name[i], |
| 242 | tegra_powergate_is_powered(i) ? "yes" : "no"); |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | static int powergate_open(struct inode *inode, struct file *file) |
| 247 | { |
| 248 | return single_open(file, powergate_show, inode->i_private); |
| 249 | } |
| 250 | |
| 251 | static 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 Guo | 390e0cf | 2012-05-02 17:08:06 +0800 | [diff] [blame] | 258 | int __init tegra_powergate_debugfs_init(void) |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 259 | { |
| 260 | struct dentry *d; |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 261 | |
Peter De Schrijver | b48d6aa | 2012-09-06 17:55:29 +0300 | [diff] [blame] | 262 | 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 Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 277 | |
Peter De Schrijver | f858b6f | 2012-09-06 17:55:28 +0300 | [diff] [blame] | 278 | return 0; |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Colin Cross | ce1e326 | 2010-05-24 17:07:46 -0700 | [diff] [blame] | 281 | #endif |