Joseph Lo | d457ef35 | 2012-10-31 17:41:17 +0800 | [diff] [blame] | 1 | /* |
| 2 | * CPU complex suspend & resume functions for Tegra SoCs |
| 3 | * |
| 4 | * Copyright (c) 2009-2012, NVIDIA Corporation. All rights reserved. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms and conditions of the GNU General Public License, |
| 8 | * version 2, as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | * more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/spinlock.h> |
| 21 | #include <linux/io.h> |
| 22 | #include <linux/cpumask.h> |
Joseph Lo | d552920 | 2012-10-31 17:41:21 +0800 | [diff] [blame] | 23 | #include <linux/delay.h> |
| 24 | #include <linux/cpu_pm.h> |
Joseph Lo | c8c2e60 | 2013-04-03 19:31:47 +0800 | [diff] [blame] | 25 | #include <linux/suspend.h> |
Joseph Lo | d552920 | 2012-10-31 17:41:21 +0800 | [diff] [blame] | 26 | #include <linux/err.h> |
Prashant Gaikwad | 89572c7 | 2013-01-11 13:16:21 +0530 | [diff] [blame] | 27 | #include <linux/clk/tegra.h> |
Joseph Lo | d552920 | 2012-10-31 17:41:21 +0800 | [diff] [blame] | 28 | |
| 29 | #include <asm/smp_plat.h> |
| 30 | #include <asm/cacheflush.h> |
| 31 | #include <asm/suspend.h> |
| 32 | #include <asm/idmap.h> |
| 33 | #include <asm/proc-fns.h> |
| 34 | #include <asm/tlbflush.h> |
Joseph Lo | d457ef35 | 2012-10-31 17:41:17 +0800 | [diff] [blame] | 35 | |
| 36 | #include "iomap.h" |
| 37 | #include "reset.h" |
Joseph Lo | d552920 | 2012-10-31 17:41:21 +0800 | [diff] [blame] | 38 | #include "flowctrl.h" |
Joseph Lo | 5c1350b | 2013-01-15 22:10:38 +0000 | [diff] [blame] | 39 | #include "fuse.h" |
Joseph Lo | 0337c3e | 2013-04-03 19:31:28 +0800 | [diff] [blame] | 40 | #include "pmc.h" |
Joseph Lo | d552920 | 2012-10-31 17:41:21 +0800 | [diff] [blame] | 41 | #include "sleep.h" |
Joseph Lo | d552920 | 2012-10-31 17:41:21 +0800 | [diff] [blame] | 42 | |
Joseph Lo | d457ef35 | 2012-10-31 17:41:17 +0800 | [diff] [blame] | 43 | #ifdef CONFIG_PM_SLEEP |
Joseph Lo | d457ef35 | 2012-10-31 17:41:17 +0800 | [diff] [blame] | 44 | static DEFINE_SPINLOCK(tegra_lp2_lock); |
Joseph Lo | d552920 | 2012-10-31 17:41:21 +0800 | [diff] [blame] | 45 | void (*tegra_tear_down_cpu)(void); |
Joseph Lo | d457ef35 | 2012-10-31 17:41:17 +0800 | [diff] [blame] | 46 | |
Joseph Lo | bf91add | 2013-06-04 18:47:33 +0800 | [diff] [blame^] | 47 | static void tegra_tear_down_cpu_init(void) |
| 48 | { |
| 49 | switch (tegra_chip_id) { |
| 50 | case TEGRA20: |
| 51 | if (IS_ENABLED(CONFIG_ARCH_TEGRA_2x_SOC)) |
| 52 | tegra_tear_down_cpu = tegra20_tear_down_cpu; |
| 53 | break; |
| 54 | case TEGRA30: |
| 55 | if (IS_ENABLED(CONFIG_ARCH_TEGRA_3x_SOC)) |
| 56 | tegra_tear_down_cpu = tegra30_tear_down_cpu; |
| 57 | break; |
| 58 | } |
| 59 | } |
| 60 | |
Joseph Lo | d552920 | 2012-10-31 17:41:21 +0800 | [diff] [blame] | 61 | /* |
| 62 | * restore_cpu_complex |
| 63 | * |
| 64 | * restores cpu clock setting, clears flow controller |
| 65 | * |
| 66 | * Always called on CPU 0. |
| 67 | */ |
| 68 | static void restore_cpu_complex(void) |
| 69 | { |
| 70 | int cpu = smp_processor_id(); |
| 71 | |
| 72 | BUG_ON(cpu != 0); |
| 73 | |
| 74 | #ifdef CONFIG_SMP |
| 75 | cpu = cpu_logical_map(cpu); |
| 76 | #endif |
| 77 | |
| 78 | /* Restore the CPU clock settings */ |
| 79 | tegra_cpu_clock_resume(); |
| 80 | |
| 81 | flowctrl_cpu_suspend_exit(cpu); |
Joseph Lo | d552920 | 2012-10-31 17:41:21 +0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /* |
| 85 | * suspend_cpu_complex |
| 86 | * |
| 87 | * saves pll state for use by restart_plls, prepares flow controller for |
| 88 | * transition to suspend state |
| 89 | * |
| 90 | * Must always be called on cpu 0. |
| 91 | */ |
| 92 | static void suspend_cpu_complex(void) |
| 93 | { |
| 94 | int cpu = smp_processor_id(); |
| 95 | |
| 96 | BUG_ON(cpu != 0); |
| 97 | |
| 98 | #ifdef CONFIG_SMP |
| 99 | cpu = cpu_logical_map(cpu); |
| 100 | #endif |
| 101 | |
| 102 | /* Save the CPU clock settings */ |
| 103 | tegra_cpu_clock_suspend(); |
| 104 | |
| 105 | flowctrl_cpu_suspend_enter(cpu); |
Joseph Lo | d552920 | 2012-10-31 17:41:21 +0800 | [diff] [blame] | 106 | } |
| 107 | |
Joseph Lo | 8c627fa | 2013-01-04 17:32:21 +0800 | [diff] [blame] | 108 | void tegra_clear_cpu_in_lp2(int phy_cpu_id) |
Joseph Lo | d457ef35 | 2012-10-31 17:41:17 +0800 | [diff] [blame] | 109 | { |
| 110 | u32 *cpu_in_lp2 = tegra_cpu_lp2_mask; |
| 111 | |
| 112 | spin_lock(&tegra_lp2_lock); |
| 113 | |
| 114 | BUG_ON(!(*cpu_in_lp2 & BIT(phy_cpu_id))); |
| 115 | *cpu_in_lp2 &= ~BIT(phy_cpu_id); |
| 116 | |
| 117 | spin_unlock(&tegra_lp2_lock); |
| 118 | } |
| 119 | |
Joseph Lo | 8c627fa | 2013-01-04 17:32:21 +0800 | [diff] [blame] | 120 | bool tegra_set_cpu_in_lp2(int phy_cpu_id) |
Joseph Lo | d457ef35 | 2012-10-31 17:41:17 +0800 | [diff] [blame] | 121 | { |
| 122 | bool last_cpu = false; |
| 123 | cpumask_t *cpu_lp2_mask = tegra_cpu_lp2_mask; |
| 124 | u32 *cpu_in_lp2 = tegra_cpu_lp2_mask; |
| 125 | |
| 126 | spin_lock(&tegra_lp2_lock); |
| 127 | |
| 128 | BUG_ON((*cpu_in_lp2 & BIT(phy_cpu_id))); |
| 129 | *cpu_in_lp2 |= BIT(phy_cpu_id); |
| 130 | |
| 131 | if ((phy_cpu_id == 0) && cpumask_equal(cpu_lp2_mask, cpu_online_mask)) |
| 132 | last_cpu = true; |
Joseph Lo | 5c1350b | 2013-01-15 22:10:38 +0000 | [diff] [blame] | 133 | else if (tegra_chip_id == TEGRA20 && phy_cpu_id == 1) |
| 134 | tegra20_cpu_set_resettable_soon(); |
Joseph Lo | d457ef35 | 2012-10-31 17:41:17 +0800 | [diff] [blame] | 135 | |
| 136 | spin_unlock(&tegra_lp2_lock); |
| 137 | return last_cpu; |
| 138 | } |
Joseph Lo | d552920 | 2012-10-31 17:41:21 +0800 | [diff] [blame] | 139 | |
Arnd Bergmann | 2058842 | 2013-04-23 15:36:26 +0200 | [diff] [blame] | 140 | int tegra_cpu_do_idle(void) |
| 141 | { |
| 142 | return cpu_do_idle(); |
| 143 | } |
| 144 | |
Joseph Lo | d552920 | 2012-10-31 17:41:21 +0800 | [diff] [blame] | 145 | static int tegra_sleep_cpu(unsigned long v2p) |
| 146 | { |
Will Deacon | 6affb48 | 2013-03-25 18:19:11 +0000 | [diff] [blame] | 147 | setup_mm_for_reboot(); |
Joseph Lo | d552920 | 2012-10-31 17:41:21 +0800 | [diff] [blame] | 148 | tegra_sleep_cpu_finish(v2p); |
| 149 | |
| 150 | /* should never here */ |
| 151 | BUG(); |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
Joseph Lo | 4d82d05 | 2013-04-02 01:20:50 +0000 | [diff] [blame] | 156 | void tegra_idle_lp2_last(void) |
Joseph Lo | d552920 | 2012-10-31 17:41:21 +0800 | [diff] [blame] | 157 | { |
Joseph Lo | c8c2e60 | 2013-04-03 19:31:47 +0800 | [diff] [blame] | 158 | tegra_pmc_pm_set(TEGRA_SUSPEND_LP2); |
Joseph Lo | d552920 | 2012-10-31 17:41:21 +0800 | [diff] [blame] | 159 | |
| 160 | cpu_cluster_pm_enter(); |
| 161 | suspend_cpu_complex(); |
Joseph Lo | d552920 | 2012-10-31 17:41:21 +0800 | [diff] [blame] | 162 | |
| 163 | cpu_suspend(PHYS_OFFSET - PAGE_OFFSET, &tegra_sleep_cpu); |
| 164 | |
Joseph Lo | d552920 | 2012-10-31 17:41:21 +0800 | [diff] [blame] | 165 | restore_cpu_complex(); |
| 166 | cpu_cluster_pm_exit(); |
| 167 | } |
Joseph Lo | c8c2e60 | 2013-04-03 19:31:47 +0800 | [diff] [blame] | 168 | |
| 169 | enum tegra_suspend_mode tegra_pm_validate_suspend_mode( |
| 170 | enum tegra_suspend_mode mode) |
| 171 | { |
| 172 | /* Tegra114 didn't support any suspending mode yet. */ |
| 173 | if (tegra_chip_id == TEGRA114) |
| 174 | return TEGRA_SUSPEND_NONE; |
| 175 | |
| 176 | /* |
| 177 | * The Tegra devices only support suspending to LP2 currently. |
| 178 | */ |
| 179 | if (mode > TEGRA_SUSPEND_LP2) |
| 180 | return TEGRA_SUSPEND_LP2; |
| 181 | |
| 182 | return mode; |
| 183 | } |
| 184 | |
| 185 | static const char *lp_state[TEGRA_MAX_SUSPEND_MODE] = { |
| 186 | [TEGRA_SUSPEND_NONE] = "none", |
| 187 | [TEGRA_SUSPEND_LP2] = "LP2", |
| 188 | [TEGRA_SUSPEND_LP1] = "LP1", |
| 189 | [TEGRA_SUSPEND_LP0] = "LP0", |
| 190 | }; |
| 191 | |
| 192 | static int __cpuinit tegra_suspend_enter(suspend_state_t state) |
| 193 | { |
| 194 | enum tegra_suspend_mode mode = tegra_pmc_get_suspend_mode(); |
| 195 | |
| 196 | if (WARN_ON(mode < TEGRA_SUSPEND_NONE || |
| 197 | mode >= TEGRA_MAX_SUSPEND_MODE)) |
| 198 | return -EINVAL; |
| 199 | |
| 200 | pr_info("Entering suspend state %s\n", lp_state[mode]); |
| 201 | |
| 202 | tegra_pmc_pm_set(mode); |
| 203 | |
| 204 | local_fiq_disable(); |
| 205 | |
| 206 | suspend_cpu_complex(); |
| 207 | switch (mode) { |
| 208 | case TEGRA_SUSPEND_LP2: |
| 209 | tegra_set_cpu_in_lp2(0); |
| 210 | break; |
| 211 | default: |
| 212 | break; |
| 213 | } |
| 214 | |
| 215 | cpu_suspend(PHYS_OFFSET - PAGE_OFFSET, &tegra_sleep_cpu); |
| 216 | |
| 217 | switch (mode) { |
| 218 | case TEGRA_SUSPEND_LP2: |
| 219 | tegra_clear_cpu_in_lp2(0); |
| 220 | break; |
| 221 | default: |
| 222 | break; |
| 223 | } |
| 224 | restore_cpu_complex(); |
| 225 | |
| 226 | local_fiq_enable(); |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | static const struct platform_suspend_ops tegra_suspend_ops = { |
| 232 | .valid = suspend_valid_only_mem, |
| 233 | .enter = tegra_suspend_enter, |
| 234 | }; |
| 235 | |
| 236 | void __init tegra_init_suspend(void) |
| 237 | { |
| 238 | if (tegra_pmc_get_suspend_mode() == TEGRA_SUSPEND_NONE) |
| 239 | return; |
| 240 | |
Joseph Lo | bf91add | 2013-06-04 18:47:33 +0800 | [diff] [blame^] | 241 | tegra_tear_down_cpu_init(); |
Joseph Lo | c8c2e60 | 2013-04-03 19:31:47 +0800 | [diff] [blame] | 242 | tegra_pmc_suspend_init(); |
| 243 | |
| 244 | suspend_set_ops(&tegra_suspend_ops); |
| 245 | } |
Joseph Lo | d457ef35 | 2012-10-31 17:41:17 +0800 | [diff] [blame] | 246 | #endif |