| Stephen Warren | d3b8bdd | 2012-01-25 14:43:28 -0700 | [diff] [blame] | 1 | /* |
| Joseph Lo | 291fde3 | 2013-02-28 21:32:10 +0000 | [diff] [blame] | 2 | * Copyright (C) 2012,2013 NVIDIA CORPORATION. All rights reserved. |
| Stephen Warren | d3b8bdd | 2012-01-25 14:43:28 -0700 | [diff] [blame] | 3 | * |
| 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 Lo | 291fde3 | 2013-02-28 21:32:10 +0000 | [diff] [blame] | 21 | #include <linux/of_address.h> |
| Stephen Warren | d3b8bdd | 2012-01-25 14:43:28 -0700 | [diff] [blame] | 22 | |
| Joseph Lo | c141753 | 2013-02-28 21:32:11 +0000 | [diff] [blame^] | 23 | #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 | |
| 36 | static u8 tegra_cpu_domains[] = { |
| 37 | 0xFF, /* not available for CPU0 */ |
| 38 | TEGRA_POWERGATE_CPU1, |
| 39 | TEGRA_POWERGATE_CPU2, |
| 40 | TEGRA_POWERGATE_CPU3, |
| 41 | }; |
| 42 | static DEFINE_SPINLOCK(tegra_powergate_lock); |
| Stephen Warren | d3b8bdd | 2012-01-25 14:43:28 -0700 | [diff] [blame] | 43 | |
| Joseph Lo | 291fde3 | 2013-02-28 21:32:10 +0000 | [diff] [blame] | 44 | static void __iomem *tegra_pmc_base; |
| 45 | static bool tegra_pmc_invert_interrupt; |
| 46 | |
| Stephen Warren | d3b8bdd | 2012-01-25 14:43:28 -0700 | [diff] [blame] | 47 | static inline u32 tegra_pmc_readl(u32 reg) |
| 48 | { |
| Joseph Lo | 291fde3 | 2013-02-28 21:32:10 +0000 | [diff] [blame] | 49 | return readl(tegra_pmc_base + reg); |
| Stephen Warren | d3b8bdd | 2012-01-25 14:43:28 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | static inline void tegra_pmc_writel(u32 val, u32 reg) |
| 53 | { |
| Joseph Lo | 291fde3 | 2013-02-28 21:32:10 +0000 | [diff] [blame] | 54 | writel(val, tegra_pmc_base + reg); |
| Stephen Warren | d3b8bdd | 2012-01-25 14:43:28 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| Joseph Lo | c141753 | 2013-02-28 21:32:11 +0000 | [diff] [blame^] | 57 | static 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 | |
| 64 | static bool tegra_pmc_powergate_is_powered(int id) |
| 65 | { |
| 66 | return (tegra_pmc_readl(PMC_PWRGATE_STATUS) >> id) & 1; |
| 67 | } |
| 68 | |
| 69 | static 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 | |
| 86 | static 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 | |
| 106 | bool 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 | |
| 116 | int 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 | |
| 126 | int 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 Warren | d3b8bdd | 2012-01-25 14:43:28 -0700 | [diff] [blame] | 136 | static const struct of_device_id matches[] __initconst = { |
| Joseph Lo | 88c4aba | 2013-02-26 16:27:42 +0000 | [diff] [blame] | 137 | { .compatible = "nvidia,tegra114-pmc" }, |
| 138 | { .compatible = "nvidia,tegra30-pmc" }, |
| Stephen Warren | d3b8bdd | 2012-01-25 14:43:28 -0700 | [diff] [blame] | 139 | { .compatible = "nvidia,tegra20-pmc" }, |
| 140 | { } |
| 141 | }; |
| Joseph Lo | 291fde3 | 2013-02-28 21:32:10 +0000 | [diff] [blame] | 142 | |
| 143 | static 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 Warren | d3b8bdd | 2012-01-25 14:43:28 -0700 | [diff] [blame] | 155 | |
| 156 | void __init tegra_pmc_init(void) |
| 157 | { |
| Stephen Warren | d3b8bdd | 2012-01-25 14:43:28 -0700 | [diff] [blame] | 158 | u32 val; |
| 159 | |
| Joseph Lo | 291fde3 | 2013-02-28 21:32:10 +0000 | [diff] [blame] | 160 | tegra_pmc_parse_dt(); |
| Stephen Warren | d3b8bdd | 2012-01-25 14:43:28 -0700 | [diff] [blame] | 161 | |
| 162 | val = tegra_pmc_readl(PMC_CTRL); |
| Joseph Lo | 291fde3 | 2013-02-28 21:32:10 +0000 | [diff] [blame] | 163 | if (tegra_pmc_invert_interrupt) |
| Stephen Warren | d3b8bdd | 2012-01-25 14:43:28 -0700 | [diff] [blame] | 164 | val |= PMC_CTRL_INTR_LOW; |
| 165 | else |
| 166 | val &= ~PMC_CTRL_INTR_LOW; |
| 167 | tegra_pmc_writel(val, PMC_CTRL); |
| 168 | } |