blob: 4f87b4d09c7d207b1649f6e7913d7b28b14a06da [file] [log] [blame]
Russell King2a98beb2005-11-09 10:50:29 +00001/*
2 * linux/arch/arm/mach-realview/localtimer.c
3 *
4 * Copyright (C) 2002 ARM Ltd.
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11#include <linux/init.h>
12#include <linux/kernel.h>
13#include <linux/delay.h>
14#include <linux/device.h>
15#include <linux/smp.h>
Tim Schmielaude259682006-01-08 01:02:05 -080016#include <linux/jiffies.h>
Catalin Marinasa8655e82008-02-04 17:30:57 +010017#include <linux/percpu.h>
18#include <linux/clockchips.h>
Catalin Marinas93c29042008-02-04 17:32:57 +010019#include <linux/irq.h>
Russell King2a98beb2005-11-09 10:50:29 +000020
Russell King2a98beb2005-11-09 10:50:29 +000021#include <asm/hardware/arm_twd.h>
22#include <asm/hardware/gic.h>
23#include <asm/hardware.h>
24#include <asm/io.h>
25#include <asm/irq.h>
26
Russell King2a98beb2005-11-09 10:50:29 +000027#define TWD_BASE(cpu) (__io_address(REALVIEW_TWD_BASE) + \
28 ((cpu) * REALVIEW_TWD_SIZE))
29
Catalin Marinasa8655e82008-02-04 17:30:57 +010030static DEFINE_PER_CPU(struct clock_event_device, local_clockevent);
31
32/*
33 * Used on SMP for either the local timer or IPI_TIMER
34 */
35void local_timer_interrupt(void)
36{
37 struct clock_event_device *clk = &__get_cpu_var(local_clockevent);
38
39 clk->event_handler(clk);
40}
41
42#ifdef CONFIG_LOCAL_TIMERS
43
Russell King2a98beb2005-11-09 10:50:29 +000044static unsigned long mpcore_timer_rate;
45
Catalin Marinas93c29042008-02-04 17:32:57 +010046static void local_timer_set_mode(enum clock_event_mode mode,
47 struct clock_event_device *clk)
48{
49 void __iomem *base = TWD_BASE(smp_processor_id());
50 unsigned long ctrl;
51
52 switch(mode) {
53 case CLOCK_EVT_MODE_PERIODIC:
54 /* timer load already set up */
55 ctrl = TWD_TIMER_CONTROL_ENABLE | TWD_TIMER_CONTROL_IT_ENABLE
56 | TWD_TIMER_CONTROL_PERIODIC;
57 break;
58 case CLOCK_EVT_MODE_ONESHOT:
59 /* period set, and timer enabled in 'next_event' hook */
60 ctrl = TWD_TIMER_CONTROL_IT_ENABLE | TWD_TIMER_CONTROL_ONESHOT;
61 break;
62 case CLOCK_EVT_MODE_UNUSED:
63 case CLOCK_EVT_MODE_SHUTDOWN:
64 default:
65 ctrl = 0;
66 }
67
68 __raw_writel(ctrl, base + TWD_TIMER_CONTROL);
69}
70
71static int local_timer_set_next_event(unsigned long evt,
72 struct clock_event_device *unused)
73{
74 void __iomem *base = TWD_BASE(smp_processor_id());
75 unsigned long ctrl = __raw_readl(base + TWD_TIMER_CONTROL);
76
77 __raw_writel(evt, base + TWD_TIMER_COUNTER);
78 __raw_writel(ctrl | TWD_TIMER_CONTROL_ENABLE, base + TWD_TIMER_CONTROL);
79
80 return 0;
81}
82
Russell King2a98beb2005-11-09 10:50:29 +000083/*
84 * local_timer_ack: checks for a local timer interrupt.
85 *
Simon Arlott6cbdc8c2007-05-11 20:40:30 +010086 * If a local timer interrupt has occurred, acknowledge and return 1.
Russell King2a98beb2005-11-09 10:50:29 +000087 * Otherwise, return 0.
88 */
89int local_timer_ack(void)
90{
91 void __iomem *base = TWD_BASE(smp_processor_id());
92
93 if (__raw_readl(base + TWD_TIMER_INTSTAT)) {
94 __raw_writel(1, base + TWD_TIMER_INTSTAT);
95 return 1;
96 }
97
98 return 0;
99}
100
Catalin Marinas93c29042008-02-04 17:32:57 +0100101static void __cpuinit twd_calibrate_rate(unsigned int cpu)
Russell King2a98beb2005-11-09 10:50:29 +0000102{
103 void __iomem *base = TWD_BASE(cpu);
Catalin Marinas93c29042008-02-04 17:32:57 +0100104 unsigned long load, count;
Russell King2a98beb2005-11-09 10:50:29 +0000105 u64 waitjiffies;
Russell King2a98beb2005-11-09 10:50:29 +0000106
107 /*
108 * If this is the first time round, we need to work out how fast
109 * the timer ticks
110 */
111 if (mpcore_timer_rate == 0) {
112 printk("Calibrating local timer... ");
113
114 /* Wait for a tick to start */
115 waitjiffies = get_jiffies_64() + 1;
116
117 while (get_jiffies_64() < waitjiffies)
118 udelay(10);
119
120 /* OK, now the tick has started, let's get the timer going */
121 waitjiffies += 5;
122
123 /* enable, no interrupt or reload */
124 __raw_writel(0x1, base + TWD_TIMER_CONTROL);
125
126 /* maximum value */
127 __raw_writel(0xFFFFFFFFU, base + TWD_TIMER_COUNTER);
128
129 while (get_jiffies_64() < waitjiffies)
130 udelay(10);
131
132 count = __raw_readl(base + TWD_TIMER_COUNTER);
133
134 mpcore_timer_rate = (0xFFFFFFFFU - count) * (HZ / 5);
135
136 printk("%lu.%02luMHz.\n", mpcore_timer_rate / 1000000,
137 (mpcore_timer_rate / 100000) % 100);
138 }
139
140 load = mpcore_timer_rate / HZ;
141
142 __raw_writel(load, base + TWD_TIMER_LOAD);
Catalin Marinas93c29042008-02-04 17:32:57 +0100143}
Russell King2a98beb2005-11-09 10:50:29 +0000144
Catalin Marinas93c29042008-02-04 17:32:57 +0100145/*
146 * Setup the local clock events for a CPU.
147 */
148void __cpuinit local_timer_setup(unsigned int cpu)
149{
150 struct clock_event_device *clk = &per_cpu(local_clockevent, cpu);
151 unsigned long flags;
Russell King2a98beb2005-11-09 10:50:29 +0000152
Catalin Marinas93c29042008-02-04 17:32:57 +0100153 twd_calibrate_rate(cpu);
Russell King2a98beb2005-11-09 10:50:29 +0000154
Catalin Marinas93c29042008-02-04 17:32:57 +0100155 clk->name = "local_timer";
156 clk->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
157 clk->rating = 350;
158 clk->set_mode = local_timer_set_mode;
159 clk->set_next_event = local_timer_set_next_event;
160 clk->irq = IRQ_LOCALTIMER;
161 clk->cpumask = cpumask_of_cpu(cpu);
162 clk->shift = 20;
163 clk->mult = div_sc(mpcore_timer_rate, NSEC_PER_SEC, clk->shift);
164 clk->max_delta_ns = clockevent_delta2ns(0xffffffff, clk);
165 clk->min_delta_ns = clockevent_delta2ns(0xf, clk);
Russell King2a98beb2005-11-09 10:50:29 +0000166
167 /* Make sure our local interrupt controller has this enabled */
Catalin Marinas93c29042008-02-04 17:32:57 +0100168 local_irq_save(flags);
169 get_irq_chip(IRQ_LOCALTIMER)->unmask(IRQ_LOCALTIMER);
170 local_irq_restore(flags);
171
172 clockevents_register_device(clk);
Russell King2a98beb2005-11-09 10:50:29 +0000173}
174
175/*
176 * take a local timer down
177 */
178void __cpuexit local_timer_stop(unsigned int cpu)
179{
180 __raw_writel(0, TWD_BASE(cpu) + TWD_TIMER_CONTROL);
181}
Catalin Marinasa8655e82008-02-04 17:30:57 +0100182
183#else /* CONFIG_LOCAL_TIMERS */
184
185static void dummy_timer_set_mode(enum clock_event_mode mode,
186 struct clock_event_device *clk)
187{
188}
189
190void __cpuinit local_timer_setup(unsigned int cpu)
191{
192 struct clock_event_device *clk = &per_cpu(local_clockevent, cpu);
193
194 clk->name = "dummy_timer";
195 clk->features = CLOCK_EVT_FEAT_DUMMY;
196 clk->rating = 200;
197 clk->set_mode = dummy_timer_set_mode;
198 clk->broadcast = smp_timer_broadcast;
199 clk->cpumask = cpumask_of_cpu(cpu);
200
201 clockevents_register_device(clk);
202}
203
204#endif /* !CONFIG_LOCAL_TIMERS */