of: MSI: Simplify irqdomain lookup
[linux/fpc-iii.git] / arch / arm / mach-tegra / cpuidle-tegra20.c
blob7469347b17493890ec43856b2a961a407bc6f7e0
1 /*
2 * CPU idle driver for Tegra CPUs
4 * Copyright (c) 2010-2012, NVIDIA Corporation.
5 * Copyright (c) 2011 Google, Inc.
6 * Author: Colin Cross <ccross@android.com>
7 * Gary King <gking@nvidia.com>
9 * Rework for 3.3 by Peter De Schrijver <pdeschrijver@nvidia.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 * more details.
22 #include <linux/clk/tegra.h>
23 #include <linux/tick.h>
24 #include <linux/cpuidle.h>
25 #include <linux/cpu_pm.h>
26 #include <linux/kernel.h>
27 #include <linux/module.h>
29 #include <asm/cpuidle.h>
30 #include <asm/smp_plat.h>
31 #include <asm/suspend.h>
33 #include "flowctrl.h"
34 #include "iomap.h"
35 #include "irq.h"
36 #include "pm.h"
37 #include "reset.h"
38 #include "sleep.h"
40 #ifdef CONFIG_PM_SLEEP
41 static bool abort_flag;
42 static atomic_t abort_barrier;
43 static int tegra20_idle_lp2_coupled(struct cpuidle_device *dev,
44 struct cpuidle_driver *drv,
45 int index);
46 #define TEGRA20_MAX_STATES 2
47 #else
48 #define TEGRA20_MAX_STATES 1
49 #endif
51 static struct cpuidle_driver tegra_idle_driver = {
52 .name = "tegra_idle",
53 .owner = THIS_MODULE,
54 .states = {
55 ARM_CPUIDLE_WFI_STATE_PWR(600),
56 #ifdef CONFIG_PM_SLEEP
58 .enter = tegra20_idle_lp2_coupled,
59 .exit_latency = 5000,
60 .target_residency = 10000,
61 .power_usage = 0,
62 .flags = CPUIDLE_FLAG_COUPLED,
63 .name = "powered-down",
64 .desc = "CPU power gated",
66 #endif
68 .state_count = TEGRA20_MAX_STATES,
69 .safe_state_index = 0,
72 #ifdef CONFIG_PM_SLEEP
73 #ifdef CONFIG_SMP
74 static int tegra20_reset_sleeping_cpu_1(void)
76 int ret = 0;
78 tegra_pen_lock();
80 if (readb(tegra20_cpu1_resettable_status) == CPU_RESETTABLE)
81 tegra20_cpu_shutdown(1);
82 else
83 ret = -EINVAL;
85 tegra_pen_unlock();
87 return ret;
90 static void tegra20_wake_cpu1_from_reset(void)
92 tegra_pen_lock();
94 tegra20_cpu_clear_resettable();
96 /* enable cpu clock on cpu */
97 tegra_enable_cpu_clock(1);
99 /* take the CPU out of reset */
100 tegra_cpu_out_of_reset(1);
102 /* unhalt the cpu */
103 flowctrl_write_cpu_halt(1, 0);
105 tegra_pen_unlock();
108 static int tegra20_reset_cpu_1(void)
110 if (!cpu_online(1) || !tegra20_reset_sleeping_cpu_1())
111 return 0;
113 tegra20_wake_cpu1_from_reset();
114 return -EBUSY;
116 #else
117 static inline void tegra20_wake_cpu1_from_reset(void)
121 static inline int tegra20_reset_cpu_1(void)
123 return 0;
125 #endif
127 static bool tegra20_cpu_cluster_power_down(struct cpuidle_device *dev,
128 struct cpuidle_driver *drv,
129 int index)
131 while (tegra20_cpu_is_resettable_soon())
132 cpu_relax();
134 if (tegra20_reset_cpu_1() || !tegra_cpu_rail_off_ready())
135 return false;
137 tick_broadcast_enter();
139 tegra_idle_lp2_last();
141 tick_broadcast_exit();
143 if (cpu_online(1))
144 tegra20_wake_cpu1_from_reset();
146 return true;
149 #ifdef CONFIG_SMP
150 static bool tegra20_idle_enter_lp2_cpu_1(struct cpuidle_device *dev,
151 struct cpuidle_driver *drv,
152 int index)
154 tick_broadcast_enter();
156 cpu_suspend(0, tegra20_sleep_cpu_secondary_finish);
158 tegra20_cpu_clear_resettable();
160 tick_broadcast_exit();
162 return true;
164 #else
165 static inline bool tegra20_idle_enter_lp2_cpu_1(struct cpuidle_device *dev,
166 struct cpuidle_driver *drv,
167 int index)
169 return true;
171 #endif
173 static int tegra20_idle_lp2_coupled(struct cpuidle_device *dev,
174 struct cpuidle_driver *drv,
175 int index)
177 bool entered_lp2 = false;
179 if (tegra_pending_sgi())
180 ACCESS_ONCE(abort_flag) = true;
182 cpuidle_coupled_parallel_barrier(dev, &abort_barrier);
184 if (abort_flag) {
185 cpuidle_coupled_parallel_barrier(dev, &abort_barrier);
186 abort_flag = false; /* clean flag for next coming */
187 return -EINTR;
190 local_fiq_disable();
192 tegra_set_cpu_in_lp2();
193 cpu_pm_enter();
195 if (dev->cpu == 0)
196 entered_lp2 = tegra20_cpu_cluster_power_down(dev, drv, index);
197 else
198 entered_lp2 = tegra20_idle_enter_lp2_cpu_1(dev, drv, index);
200 cpu_pm_exit();
201 tegra_clear_cpu_in_lp2();
203 local_fiq_enable();
205 smp_rmb();
207 return entered_lp2 ? index : 0;
209 #endif
212 * Tegra20 HW appears to have a bug such that PCIe device interrupts, whether
213 * they are legacy IRQs or MSI, are lost when LP2 is enabled. To work around
214 * this, simply disable LP2 if the PCI driver and DT node are both enabled.
216 void tegra20_cpuidle_pcie_irqs_in_use(void)
218 pr_info_once(
219 "Disabling cpuidle LP2 state, since PCIe IRQs are in use\n");
220 tegra_idle_driver.states[1].disabled = true;
223 int __init tegra20_cpuidle_init(void)
225 return cpuidle_register(&tegra_idle_driver, cpu_possible_mask);