ARM: tegra: power: Power off multiple CPUs on-line
[linux-3.10.git] / arch / arm / mach-tegra / timer-t3.c
1 /*
2  * arch/arch/mach-tegra/timer-t3.c
3  *
4  * Copyright (c) 2011, NVIDIA Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19  */
20
21 #include <linux/init.h>
22 #include <linux/err.h>
23 #include <linux/sched.h>
24 #include <linux/time.h>
25 #include <linux/interrupt.h>
26 #include <linux/irq.h>
27 #include <linux/clockchips.h>
28 #include <linux/clocksource.h>
29 #include <linux/clk.h>
30 #include <linux/io.h>
31 #include <linux/smp.h>
32 #include <linux/syscore_ops.h>
33 #include <linux/cpu.h>
34 #include <linux/export.h>
35
36 #include <asm/mach/time.h>
37 #include <asm/localtimer.h>
38 #include <asm/sched_clock.h>
39
40 #include <mach/hardware.h>
41 #include <mach/iomap.h>
42 #include <mach/irqs.h>
43
44 #include "board.h"
45 #include "clock.h"
46 #include "cpuidle.h"
47 #include "timer.h"
48
49 #define TEST_LP2_WAKE_TIMERS    0
50
51 /*
52  * Timers usage:
53  * TMR1 - used as general CPU timer.
54  * TMR2 - used by AVP.
55  * TMR3 - used by CPU0 for LP2 wakeup.
56  * TMR4 - used by CPU1 for LP2 wakeup.
57  * TMR5 - used by CPU2 for LP2 wakeup.
58  * TMR6 - used by CPU3 for LP2 wakeup.
59  * TMR7 - Free.
60  * TMR8 - Free.
61  * TMR9 - Free.
62  * TMR10 - used as source for watchdog controller 0.
63 */
64
65 #define TIMER1_OFFSET (TEGRA_TMR1_BASE-TEGRA_TMR1_BASE)
66 #define TIMER2_OFFSET (TEGRA_TMR2_BASE-TEGRA_TMR1_BASE)
67 #define TIMER3_OFFSET (TEGRA_TMR3_BASE-TEGRA_TMR1_BASE)
68 #define TIMER4_OFFSET (TEGRA_TMR4_BASE-TEGRA_TMR1_BASE)
69 #define TIMER5_OFFSET (TEGRA_TMR5_BASE-TEGRA_TMR1_BASE)
70 #define TIMER6_OFFSET (TEGRA_TMR6_BASE-TEGRA_TMR1_BASE)
71
72 static void __iomem *timer_reg_base = IO_ADDRESS(TEGRA_TMR1_BASE);
73 static cpumask_t wake_timer_ready;
74 static cpumask_t wake_timer_canceled;
75
76 #define timer_writel(value, reg) \
77         __raw_writel(value, timer_reg_base + (reg))
78 #define timer_readl(reg) \
79         __raw_readl(timer_reg_base + (reg))
80
81
82 #ifdef CONFIG_PM_SLEEP
83 static u32 lp2_wake_timers[] = {
84         TIMER3_OFFSET,
85 #ifdef CONFIG_SMP
86         TIMER4_OFFSET,
87         TIMER5_OFFSET,
88         TIMER6_OFFSET,
89 #endif
90 };
91
92 static irqreturn_t tegra_lp2wake_interrupt(int irq, void *dev_id)
93 {
94         int cpu = (int)dev_id;
95         int base;
96
97         base = lp2_wake_timers[cpu];
98         timer_writel(1<<30, base + TIMER_PCR);
99         return IRQ_HANDLED;
100 }
101
102 #define LP2_TIMER_IRQ_ACTION(cpu, irqnum) {                     \
103         .name           = "tmr_lp2wake_cpu" __stringify(cpu),   \
104         .flags          = IRQF_DISABLED,                        \
105         .handler        = tegra_lp2wake_interrupt,              \
106         .dev_id         = (void*)cpu,                           \
107         .irq            = irqnum }
108
109 static struct irqaction tegra_lp2wake_irq[] = {
110         LP2_TIMER_IRQ_ACTION(0, INT_TMR3),
111 #ifdef CONFIG_SMP
112         LP2_TIMER_IRQ_ACTION(1, INT_TMR4),
113         LP2_TIMER_IRQ_ACTION(2, INT_TMR5),
114         LP2_TIMER_IRQ_ACTION(3, INT_TMR6),
115 #endif
116 };
117
118 #ifdef CONFIG_SMP
119 #define hard_smp_processor_id()                                         \
120         ({                                                              \
121                 unsigned int cpunum;                                    \
122                 __asm__("\n"                                            \
123                         "1:     mrc p15, 0, %0, c0, c0, 5\n"            \
124                         "       .pushsection \".alt.smp.init\", \"a\"\n"\
125                         "       .long   1b\n"                           \
126                         "       mov     %0, #0\n"                       \
127                         "       .popsection"                            \
128                         : "=r" (cpunum));                               \
129                 cpunum &= 0x0F;                                         \
130         })
131 #define cpu_number()    hard_smp_processor_id()
132 #else
133 #define cpu_number()    0
134 #endif
135
136 /*
137  * To sanity test LP2 timer interrupts for CPU 0-3, enable this flag and check
138  * /proc/interrupts for timer interrupts. CPUs 0-3 should have one interrupt
139  * counted against them for tmr_lp2wake_cpu<n>, where <n> is the CPU number.
140  */
141 #if TEST_LP2_WAKE_TIMERS
142 static void test_lp2_wake_timer(unsigned int cpu)
143 {
144         unsigned long cycles = 50000;
145         unsigned int base = lp2_wake_timers[cpu];
146         static bool tested[4] = {false, false, false, false};
147
148         /* Don't repeat the test process on hotplug restart. */
149         if (!tested[cpu]) {
150                 timer_writel(0, base + TIMER_PTV);
151                 if (cycles) {
152                         u32 reg = 0x80000000ul | min(0x1ffffffful, cycles);
153                         timer_writel(reg, base + TIMER_PTV);
154                         tested[cpu] = true;
155                 }
156         }
157 }
158 #else
159 static inline void test_lp2_wake_timer(unsigned int cpu) {}
160 #endif
161
162 static void tegra3_register_wake_timer(unsigned int cpu)
163 {
164         int ret;
165
166         ret = setup_irq(tegra_lp2wake_irq[cpu].irq, &tegra_lp2wake_irq[cpu]);
167         if (ret) {
168                 pr_err("Failed to register LP2 timer IRQ for CPU %d: "
169                         "irq=%d, ret=%d\n", cpu,
170                         tegra_lp2wake_irq[cpu].irq, ret);
171                 goto fail;
172         }
173
174 #ifdef CONFIG_SMP
175         ret = irq_set_affinity(tegra_lp2wake_irq[cpu].irq, cpumask_of(cpu));
176         if (ret) {
177                 pr_err("Failed to set affinity for LP2 timer IRQ to "
178                         "CPU %d: irq=%d, ret=%d\n", cpu,
179                         tegra_lp2wake_irq[cpu].irq, ret);
180                 goto fail;
181         }
182 #endif
183         test_lp2_wake_timer(cpu);
184         cpumask_set_cpu(cpu, &wake_timer_ready);
185         return;
186 fail:
187         tegra_lp2_in_idle(false);
188 }
189
190 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_HOTPLUG_CPU)
191 static void tegra3_unregister_wake_timer(unsigned int cpu)
192 {
193         cpumask_clear_cpu(cpu, &wake_timer_ready);
194 #ifdef CONFIG_SMP
195         /* Reassign the affinity of the wake IRQ to CPU 0. */
196         (void)irq_set_affinity(tegra_lp2wake_irq[cpu].irq, cpumask_of(0));
197 #endif
198
199         /* Dispose of this IRQ. */
200         remove_irq(tegra_lp2wake_irq[cpu].irq, &tegra_lp2wake_irq[cpu]);
201 }
202 #endif
203
204 void tegra3_lp2_set_trigger(unsigned long cycles)
205 {
206         int cpu = cpu_number();
207         int base;
208
209         base = lp2_wake_timers[cpu];
210         timer_writel(0, base + TIMER_PTV);
211         if (cycles) {
212                 u32 reg = 0x80000000ul | min(0x1ffffffful, cycles);
213                 timer_writel(reg, base + TIMER_PTV);
214         }
215 }
216 EXPORT_SYMBOL(tegra3_lp2_set_trigger);
217
218 unsigned long tegra3_lp2_timer_remain(void)
219 {
220         int cpu = cpu_number();
221
222         if (cpumask_test_and_clear_cpu(cpu, &wake_timer_canceled))
223                 return -ETIME;
224
225         return timer_readl(lp2_wake_timers[cpu] + TIMER_PCR) & 0x1ffffffful;
226 }
227
228 int tegra3_is_lp2_timer_ready(unsigned int cpu)
229 {
230         return cpumask_test_cpu(cpu, &wake_timer_ready);
231 }
232
233 void tegra3_lp2_timer_cancel_secondary(void)
234 {
235         int cpu;
236         int base;
237
238         for (cpu = 1; cpu < ARRAY_SIZE(lp2_wake_timers); cpu++) {
239                 base = lp2_wake_timers[cpu];
240                 cpumask_set_cpu(cpu, &wake_timer_canceled);
241                 timer_writel(0, base + TIMER_PTV);
242                 timer_writel(1<<30, base + TIMER_PCR);
243         }
244 }
245 #endif
246
247 void __init tegra3_init_timer(u32 *offset, int *irq, unsigned long rate)
248 {
249         switch (rate) {
250         case 12000000:
251                 timer_writel(0x000b, TIMERUS_USEC_CFG);
252                 break;
253         case 13000000:
254                 timer_writel(0x000c, TIMERUS_USEC_CFG);
255                 break;
256         case 19200000:
257                 timer_writel(0x045f, TIMERUS_USEC_CFG);
258                 break;
259         case 26000000:
260                 timer_writel(0x0019, TIMERUS_USEC_CFG);
261                 break;
262         case 16800000:
263                 timer_writel(0x0453, TIMERUS_USEC_CFG);
264                 break;
265         case 38400000:
266                 timer_writel(0x04BF, TIMERUS_USEC_CFG);
267                 break;
268         case 48000000:
269                 timer_writel(0x002F, TIMERUS_USEC_CFG);
270                 break;
271         default:
272                 WARN(1, "Unknown clock rate");
273         }
274
275 #ifdef CONFIG_PM_SLEEP
276 #ifdef CONFIG_SMP
277         /* For T30.A01 use INT_TMR_SHARED instead of INT_TMR6 for CPU3. */
278         if ((tegra_chip_id == TEGRA30) &&
279                 (tegra_revision == TEGRA_REVISION_A01))
280                         tegra_lp2wake_irq[3].irq = INT_TMR_SHARED;
281 #endif
282
283         tegra3_register_wake_timer(0);
284 #endif
285
286         *offset = TIMER1_OFFSET;
287         *irq = INT_TMR1;
288 }
289
290 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_HOTPLUG_CPU)
291 static int hotplug_notify(struct notifier_block *self,
292                                       unsigned long action, void *cpu)
293 {
294         if (action == CPU_ONLINE)
295                 tegra3_register_wake_timer((unsigned int)cpu);
296         else if (action == CPU_DOWN_PREPARE)
297                 tegra3_unregister_wake_timer((unsigned int)cpu);
298
299         return NOTIFY_OK;
300 }
301
302 static struct notifier_block __cpuinitdata hotplug_notifier_block = {
303         .notifier_call = hotplug_notify,
304 };
305
306 static int __init hotplug_cpu_register(void)
307 {
308         return register_cpu_notifier(&hotplug_notifier_block);
309 }
310 early_initcall(hotplug_cpu_register);
311 #endif