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 | c8c2e60 | 2013-04-03 19:31:47 +0800 | [diff] [blame^] | 42 | #include "pmc.h" |
Joseph Lo | d457ef35 | 2012-10-31 17:41:17 +0800 | [diff] [blame] | 43 | |
| 44 | #ifdef CONFIG_PM_SLEEP |
Joseph Lo | d457ef35 | 2012-10-31 17:41:17 +0800 | [diff] [blame] | 45 | static DEFINE_SPINLOCK(tegra_lp2_lock); |
Joseph Lo | d552920 | 2012-10-31 17:41:21 +0800 | [diff] [blame] | 46 | void (*tegra_tear_down_cpu)(void); |
Joseph Lo | d457ef35 | 2012-10-31 17:41:17 +0800 | [diff] [blame] | 47 | |
Joseph Lo | d552920 | 2012-10-31 17:41:21 +0800 | [diff] [blame] | 48 | /* |
| 49 | * restore_cpu_complex |
| 50 | * |
| 51 | * restores cpu clock setting, clears flow controller |
| 52 | * |
| 53 | * Always called on CPU 0. |
| 54 | */ |
| 55 | static void restore_cpu_complex(void) |
| 56 | { |
| 57 | int cpu = smp_processor_id(); |
| 58 | |
| 59 | BUG_ON(cpu != 0); |
| 60 | |
| 61 | #ifdef CONFIG_SMP |
| 62 | cpu = cpu_logical_map(cpu); |
| 63 | #endif |
| 64 | |
| 65 | /* Restore the CPU clock settings */ |
| 66 | tegra_cpu_clock_resume(); |
| 67 | |
| 68 | flowctrl_cpu_suspend_exit(cpu); |
Joseph Lo | d552920 | 2012-10-31 17:41:21 +0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /* |
| 72 | * suspend_cpu_complex |
| 73 | * |
| 74 | * saves pll state for use by restart_plls, prepares flow controller for |
| 75 | * transition to suspend state |
| 76 | * |
| 77 | * Must always be called on cpu 0. |
| 78 | */ |
| 79 | static void suspend_cpu_complex(void) |
| 80 | { |
| 81 | int cpu = smp_processor_id(); |
| 82 | |
| 83 | BUG_ON(cpu != 0); |
| 84 | |
| 85 | #ifdef CONFIG_SMP |
| 86 | cpu = cpu_logical_map(cpu); |
| 87 | #endif |
| 88 | |
| 89 | /* Save the CPU clock settings */ |
| 90 | tegra_cpu_clock_suspend(); |
| 91 | |
| 92 | flowctrl_cpu_suspend_enter(cpu); |
Joseph Lo | d552920 | 2012-10-31 17:41:21 +0800 | [diff] [blame] | 93 | } |
| 94 | |
Joseph Lo | 8c627fa | 2013-01-04 17:32:21 +0800 | [diff] [blame] | 95 | void tegra_clear_cpu_in_lp2(int phy_cpu_id) |
Joseph Lo | d457ef35 | 2012-10-31 17:41:17 +0800 | [diff] [blame] | 96 | { |
| 97 | u32 *cpu_in_lp2 = tegra_cpu_lp2_mask; |
| 98 | |
| 99 | spin_lock(&tegra_lp2_lock); |
| 100 | |
| 101 | BUG_ON(!(*cpu_in_lp2 & BIT(phy_cpu_id))); |
| 102 | *cpu_in_lp2 &= ~BIT(phy_cpu_id); |
| 103 | |
| 104 | spin_unlock(&tegra_lp2_lock); |
| 105 | } |
| 106 | |
Joseph Lo | 8c627fa | 2013-01-04 17:32:21 +0800 | [diff] [blame] | 107 | bool tegra_set_cpu_in_lp2(int phy_cpu_id) |
Joseph Lo | d457ef35 | 2012-10-31 17:41:17 +0800 | [diff] [blame] | 108 | { |
| 109 | bool last_cpu = false; |
| 110 | cpumask_t *cpu_lp2_mask = tegra_cpu_lp2_mask; |
| 111 | u32 *cpu_in_lp2 = tegra_cpu_lp2_mask; |
| 112 | |
| 113 | spin_lock(&tegra_lp2_lock); |
| 114 | |
| 115 | BUG_ON((*cpu_in_lp2 & BIT(phy_cpu_id))); |
| 116 | *cpu_in_lp2 |= BIT(phy_cpu_id); |
| 117 | |
| 118 | if ((phy_cpu_id == 0) && cpumask_equal(cpu_lp2_mask, cpu_online_mask)) |
| 119 | last_cpu = true; |
Joseph Lo | 5c1350b | 2013-01-15 22:10:38 +0000 | [diff] [blame] | 120 | else if (tegra_chip_id == TEGRA20 && phy_cpu_id == 1) |
| 121 | tegra20_cpu_set_resettable_soon(); |
Joseph Lo | d457ef35 | 2012-10-31 17:41:17 +0800 | [diff] [blame] | 122 | |
| 123 | spin_unlock(&tegra_lp2_lock); |
| 124 | return last_cpu; |
| 125 | } |
Joseph Lo | d552920 | 2012-10-31 17:41:21 +0800 | [diff] [blame] | 126 | |
| 127 | static int tegra_sleep_cpu(unsigned long v2p) |
| 128 | { |
| 129 | /* Switch to the identity mapping. */ |
| 130 | cpu_switch_mm(idmap_pgd, &init_mm); |
| 131 | |
| 132 | /* Flush the TLB. */ |
| 133 | local_flush_tlb_all(); |
| 134 | |
| 135 | tegra_sleep_cpu_finish(v2p); |
| 136 | |
| 137 | /* should never here */ |
| 138 | BUG(); |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | void tegra_idle_lp2_last(u32 cpu_on_time, u32 cpu_off_time) |
| 144 | { |
Joseph Lo | c8c2e60 | 2013-04-03 19:31:47 +0800 | [diff] [blame^] | 145 | tegra_pmc_pm_set(TEGRA_SUSPEND_LP2); |
Joseph Lo | d552920 | 2012-10-31 17:41:21 +0800 | [diff] [blame] | 146 | |
| 147 | cpu_cluster_pm_enter(); |
| 148 | suspend_cpu_complex(); |
Joseph Lo | d552920 | 2012-10-31 17:41:21 +0800 | [diff] [blame] | 149 | |
| 150 | cpu_suspend(PHYS_OFFSET - PAGE_OFFSET, &tegra_sleep_cpu); |
| 151 | |
Joseph Lo | d552920 | 2012-10-31 17:41:21 +0800 | [diff] [blame] | 152 | restore_cpu_complex(); |
| 153 | cpu_cluster_pm_exit(); |
| 154 | } |
Joseph Lo | c8c2e60 | 2013-04-03 19:31:47 +0800 | [diff] [blame^] | 155 | |
| 156 | enum tegra_suspend_mode tegra_pm_validate_suspend_mode( |
| 157 | enum tegra_suspend_mode mode) |
| 158 | { |
| 159 | /* Tegra114 didn't support any suspending mode yet. */ |
| 160 | if (tegra_chip_id == TEGRA114) |
| 161 | return TEGRA_SUSPEND_NONE; |
| 162 | |
| 163 | /* |
| 164 | * The Tegra devices only support suspending to LP2 currently. |
| 165 | */ |
| 166 | if (mode > TEGRA_SUSPEND_LP2) |
| 167 | return TEGRA_SUSPEND_LP2; |
| 168 | |
| 169 | return mode; |
| 170 | } |
| 171 | |
| 172 | static const char *lp_state[TEGRA_MAX_SUSPEND_MODE] = { |
| 173 | [TEGRA_SUSPEND_NONE] = "none", |
| 174 | [TEGRA_SUSPEND_LP2] = "LP2", |
| 175 | [TEGRA_SUSPEND_LP1] = "LP1", |
| 176 | [TEGRA_SUSPEND_LP0] = "LP0", |
| 177 | }; |
| 178 | |
| 179 | static int __cpuinit tegra_suspend_enter(suspend_state_t state) |
| 180 | { |
| 181 | enum tegra_suspend_mode mode = tegra_pmc_get_suspend_mode(); |
| 182 | |
| 183 | if (WARN_ON(mode < TEGRA_SUSPEND_NONE || |
| 184 | mode >= TEGRA_MAX_SUSPEND_MODE)) |
| 185 | return -EINVAL; |
| 186 | |
| 187 | pr_info("Entering suspend state %s\n", lp_state[mode]); |
| 188 | |
| 189 | tegra_pmc_pm_set(mode); |
| 190 | |
| 191 | local_fiq_disable(); |
| 192 | |
| 193 | suspend_cpu_complex(); |
| 194 | switch (mode) { |
| 195 | case TEGRA_SUSPEND_LP2: |
| 196 | tegra_set_cpu_in_lp2(0); |
| 197 | break; |
| 198 | default: |
| 199 | break; |
| 200 | } |
| 201 | |
| 202 | cpu_suspend(PHYS_OFFSET - PAGE_OFFSET, &tegra_sleep_cpu); |
| 203 | |
| 204 | switch (mode) { |
| 205 | case TEGRA_SUSPEND_LP2: |
| 206 | tegra_clear_cpu_in_lp2(0); |
| 207 | break; |
| 208 | default: |
| 209 | break; |
| 210 | } |
| 211 | restore_cpu_complex(); |
| 212 | |
| 213 | local_fiq_enable(); |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | static const struct platform_suspend_ops tegra_suspend_ops = { |
| 219 | .valid = suspend_valid_only_mem, |
| 220 | .enter = tegra_suspend_enter, |
| 221 | }; |
| 222 | |
| 223 | void __init tegra_init_suspend(void) |
| 224 | { |
| 225 | if (tegra_pmc_get_suspend_mode() == TEGRA_SUSPEND_NONE) |
| 226 | return; |
| 227 | |
| 228 | tegra_pmc_suspend_init(); |
| 229 | |
| 230 | suspend_set_ops(&tegra_suspend_ops); |
| 231 | } |
Joseph Lo | d457ef35 | 2012-10-31 17:41:17 +0800 | [diff] [blame] | 232 | #endif |