2 * linux/arch/arm/mach-realview/localtimer.c
4 * Copyright (C) 2002 ARM Ltd.
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.
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>
16 #include <linux/jiffies.h>
17 #include <linux/percpu.h>
18 #include <linux/clockchips.h>
19 #include <linux/irq.h>
21 #include <asm/hardware/arm_twd.h>
22 #include <asm/hardware/gic.h>
23 #include <asm/hardware.h>
27 #define TWD_BASE(cpu) (__io_address(REALVIEW_EB11MP_TWD_BASE) + \
28 ((cpu) * REALVIEW_EB11MP_TWD_SIZE))
30 static DEFINE_PER_CPU(struct clock_event_device
, local_clockevent
);
33 * Used on SMP for either the local timer or IPI_TIMER
35 void local_timer_interrupt(void)
37 struct clock_event_device
*clk
= &__get_cpu_var(local_clockevent
);
39 clk
->event_handler(clk
);
42 #ifdef CONFIG_LOCAL_TIMERS
44 static unsigned long mpcore_timer_rate
;
46 static void local_timer_set_mode(enum clock_event_mode mode
,
47 struct clock_event_device
*clk
)
49 void __iomem
*base
= TWD_BASE(smp_processor_id());
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
;
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
;
62 case CLOCK_EVT_MODE_UNUSED
:
63 case CLOCK_EVT_MODE_SHUTDOWN
:
68 __raw_writel(ctrl
, base
+ TWD_TIMER_CONTROL
);
71 static int local_timer_set_next_event(unsigned long evt
,
72 struct clock_event_device
*unused
)
74 void __iomem
*base
= TWD_BASE(smp_processor_id());
75 unsigned long ctrl
= __raw_readl(base
+ TWD_TIMER_CONTROL
);
77 __raw_writel(evt
, base
+ TWD_TIMER_COUNTER
);
78 __raw_writel(ctrl
| TWD_TIMER_CONTROL_ENABLE
, base
+ TWD_TIMER_CONTROL
);
84 * local_timer_ack: checks for a local timer interrupt.
86 * If a local timer interrupt has occurred, acknowledge and return 1.
87 * Otherwise, return 0.
89 int local_timer_ack(void)
91 void __iomem
*base
= TWD_BASE(smp_processor_id());
93 if (__raw_readl(base
+ TWD_TIMER_INTSTAT
)) {
94 __raw_writel(1, base
+ TWD_TIMER_INTSTAT
);
101 static void __cpuinit
twd_calibrate_rate(unsigned int cpu
)
103 void __iomem
*base
= TWD_BASE(cpu
);
104 unsigned long load
, count
;
108 * If this is the first time round, we need to work out how fast
111 if (mpcore_timer_rate
== 0) {
112 printk("Calibrating local timer... ");
114 /* Wait for a tick to start */
115 waitjiffies
= get_jiffies_64() + 1;
117 while (get_jiffies_64() < waitjiffies
)
120 /* OK, now the tick has started, let's get the timer going */
123 /* enable, no interrupt or reload */
124 __raw_writel(0x1, base
+ TWD_TIMER_CONTROL
);
127 __raw_writel(0xFFFFFFFFU
, base
+ TWD_TIMER_COUNTER
);
129 while (get_jiffies_64() < waitjiffies
)
132 count
= __raw_readl(base
+ TWD_TIMER_COUNTER
);
134 mpcore_timer_rate
= (0xFFFFFFFFU
- count
) * (HZ
/ 5);
136 printk("%lu.%02luMHz.\n", mpcore_timer_rate
/ 1000000,
137 (mpcore_timer_rate
/ 100000) % 100);
140 load
= mpcore_timer_rate
/ HZ
;
142 __raw_writel(load
, base
+ TWD_TIMER_LOAD
);
146 * Setup the local clock events for a CPU.
148 void __cpuinit
local_timer_setup(unsigned int cpu
)
150 struct clock_event_device
*clk
= &per_cpu(local_clockevent
, cpu
);
153 twd_calibrate_rate(cpu
);
155 clk
->name
= "local_timer";
156 clk
->features
= CLOCK_EVT_FEAT_PERIODIC
| CLOCK_EVT_FEAT_ONESHOT
;
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
);
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
);
167 /* Make sure our local interrupt controller has this enabled */
168 local_irq_save(flags
);
169 get_irq_chip(IRQ_LOCALTIMER
)->unmask(IRQ_LOCALTIMER
);
170 local_irq_restore(flags
);
172 clockevents_register_device(clk
);
176 * take a local timer down
178 void __cpuexit
local_timer_stop(unsigned int cpu
)
180 __raw_writel(0, TWD_BASE(cpu
) + TWD_TIMER_CONTROL
);
183 #else /* CONFIG_LOCAL_TIMERS */
185 static void dummy_timer_set_mode(enum clock_event_mode mode
,
186 struct clock_event_device
*clk
)
190 void __cpuinit
local_timer_setup(unsigned int cpu
)
192 struct clock_event_device
*clk
= &per_cpu(local_clockevent
, cpu
);
194 clk
->name
= "dummy_timer";
195 clk
->features
= CLOCK_EVT_FEAT_DUMMY
;
197 clk
->set_mode
= dummy_timer_set_mode
;
198 clk
->broadcast
= smp_timer_broadcast
;
199 clk
->cpumask
= cpumask_of_cpu(cpu
);
201 clockevents_register_device(clk
);
204 #endif /* !CONFIG_LOCAL_TIMERS */