2 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
9 * -sched_clock( ) no longer jiffies based. Uses the same clocksource
12 * Rajeshwarr/Vineetg: Mar 2008
13 * -Implemented CONFIG_GENERIC_TIME (rather deleted arch specific code)
14 * for arch independent gettimeofday()
15 * -Implemented CONFIG_GENERIC_CLOCKEVENTS as base for hrtimers
17 * Vineetg: Mar 2008: Forked off from time.c which now is time-jiff.c
20 /* ARC700 has two 32bit independent prog Timers: TIMER0 and TIMER1
21 * Each can programmed to go from @count to @limit and optionally
22 * interrupt when that happens.
23 * A write to Control Register clears the Interrupt
25 * We've designated TIMER0 for events (clockevents)
26 * while TIMER1 for free running (clocksource)
28 * Newer ARC700 cores have 64bit clk fetching RTSC insn, preferred over TIMER1
29 * which however is currently broken
32 #include <linux/interrupt.h>
33 #include <linux/module.h>
34 #include <linux/kernel.h>
35 #include <linux/time.h>
36 #include <linux/init.h>
37 #include <linux/clk.h>
38 #include <linux/clk-provider.h>
39 #include <linux/clocksource.h>
40 #include <linux/clockchips.h>
41 #include <linux/cpu.h>
43 #include <asm/arcregs.h>
45 #include <asm/mach_desc.h>
49 /* Timer related Aux registers */
50 #define ARC_REG_TIMER0_LIMIT 0x23 /* timer 0 limit */
51 #define ARC_REG_TIMER0_CTRL 0x22 /* timer 0 control */
52 #define ARC_REG_TIMER0_CNT 0x21 /* timer 0 count */
53 #define ARC_REG_TIMER1_LIMIT 0x102 /* timer 1 limit */
54 #define ARC_REG_TIMER1_CTRL 0x101 /* timer 1 control */
55 #define ARC_REG_TIMER1_CNT 0x100 /* timer 1 count */
57 #define TIMER_CTRL_IE (1 << 0) /* Interrupt when Count reaches limit */
58 #define TIMER_CTRL_NH (1 << 1) /* Count only when CPU NOT halted */
60 #define ARC_TIMER_MAX 0xFFFFFFFF
62 /********** Clock Source Device *********/
64 #ifdef CONFIG_ARC_HAS_GFRC
66 static int arc_counter_setup(void)
71 static cycle_t arc_counter_read(struct clocksource *cs)
75 #ifdef CONFIG_CPU_BIG_ENDIAN
83 local_irq_save(flags);
85 __mcip_cmd(CMD_GFRC_READ_LO, 0);
86 stamp.l = read_aux_reg(ARC_REG_MCIP_READBACK);
88 __mcip_cmd(CMD_GFRC_READ_HI, 0);
89 stamp.h = read_aux_reg(ARC_REG_MCIP_READBACK);
91 local_irq_restore(flags);
96 static struct clocksource arc_counter = {
97 .name = "ARConnect GFRC",
99 .read = arc_counter_read,
100 .mask = CLOCKSOURCE_MASK(64),
101 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
106 #ifdef CONFIG_ARC_HAS_RTC
108 #define AUX_RTC_CTRL 0x103
109 #define AUX_RTC_LOW 0x104
110 #define AUX_RTC_HIGH 0x105
112 int arc_counter_setup(void)
114 write_aux_reg(AUX_RTC_CTRL, 1);
116 /* Not usable in SMP */
117 return !IS_ENABLED(CONFIG_SMP);
120 static cycle_t arc_counter_read(struct clocksource *cs)
122 unsigned long status;
124 #ifdef CONFIG_CPU_BIG_ENDIAN
125 struct { u32 high, low; };
127 struct { u32 low, high; };
135 " lr %0, [AUX_RTC_LOW] \n"
136 " lr %1, [AUX_RTC_HIGH] \n"
137 " lr %2, [AUX_RTC_CTRL] \n"
138 " bbit0.nt %2, 31, 1b \n"
139 : "=r" (stamp.low), "=r" (stamp.high), "=r" (status));
144 static struct clocksource arc_counter = {
147 .read = arc_counter_read,
148 .mask = CLOCKSOURCE_MASK(64),
149 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
152 #else /* !CONFIG_ARC_HAS_RTC */
155 * set 32bit TIMER1 to keep counting monotonically and wraparound
157 int arc_counter_setup(void)
159 write_aux_reg(ARC_REG_TIMER1_LIMIT, ARC_TIMER_MAX);
160 write_aux_reg(ARC_REG_TIMER1_CNT, 0);
161 write_aux_reg(ARC_REG_TIMER1_CTRL, TIMER_CTRL_NH);
163 /* Not usable in SMP */
164 return !IS_ENABLED(CONFIG_SMP);
167 static cycle_t arc_counter_read(struct clocksource *cs)
169 return (cycle_t) read_aux_reg(ARC_REG_TIMER1_CNT);
172 static struct clocksource arc_counter = {
173 .name = "ARC Timer1",
175 .read = arc_counter_read,
176 .mask = CLOCKSOURCE_MASK(32),
177 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
183 /********** Clock Event Device *********/
185 static int arc_timer_irq = TIMER0_IRQ;
188 * Arm the timer to interrupt after @cycles
189 * The distinction for oneshot/periodic is done in arc_event_timer_ack() below
191 static void arc_timer_event_setup(unsigned int cycles)
193 write_aux_reg(ARC_REG_TIMER0_LIMIT, cycles);
194 write_aux_reg(ARC_REG_TIMER0_CNT, 0); /* start from 0 */
196 write_aux_reg(ARC_REG_TIMER0_CTRL, TIMER_CTRL_IE | TIMER_CTRL_NH);
200 static int arc_clkevent_set_next_event(unsigned long delta,
201 struct clock_event_device *dev)
203 arc_timer_event_setup(delta);
207 static int arc_clkevent_set_periodic(struct clock_event_device *dev)
210 * At X Hz, 1 sec = 1000ms -> X cycles;
211 * 10ms -> X / 100 cycles
213 arc_timer_event_setup(arc_get_core_freq() / HZ);
217 static DEFINE_PER_CPU(struct clock_event_device, arc_clockevent_device) = {
218 .name = "ARC Timer0",
219 .features = CLOCK_EVT_FEAT_ONESHOT |
220 CLOCK_EVT_FEAT_PERIODIC,
222 .set_next_event = arc_clkevent_set_next_event,
223 .set_state_periodic = arc_clkevent_set_periodic,
226 static irqreturn_t timer_irq_handler(int irq, void *dev_id)
229 * Note that generic IRQ core could have passed @evt for @dev_id if
230 * irq_set_chip_and_handler() asked for handle_percpu_devid_irq()
232 struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device);
233 int irq_reenable = clockevent_state_periodic(evt);
236 * Any write to CTRL reg ACks the interrupt, we rewrite the
237 * Count when [N]ot [H]alted bit.
238 * And re-arm it if perioid by [I]nterrupt [E]nable bit
240 write_aux_reg(ARC_REG_TIMER0_CTRL, irq_reenable | TIMER_CTRL_NH);
242 evt->event_handler(evt);
247 static int arc_timer_cpu_notify(struct notifier_block *self,
248 unsigned long action, void *hcpu)
250 struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device);
252 evt->cpumask = cpumask_of(smp_processor_id());
254 switch (action & ~CPU_TASKS_FROZEN) {
256 clockevents_config_and_register(evt, arc_get_core_freq(),
258 enable_percpu_irq(arc_timer_irq, 0);
261 disable_percpu_irq(arc_timer_irq);
268 static struct notifier_block arc_timer_cpu_nb = {
269 .notifier_call = arc_timer_cpu_notify,
273 * clockevent setup for boot CPU
275 static void __init arc_clockevent_setup(void)
277 struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device);
280 register_cpu_notifier(&arc_timer_cpu_nb);
282 evt->cpumask = cpumask_of(smp_processor_id());
283 clockevents_config_and_register(evt, arc_get_core_freq(),
286 /* Needs apriori irq_set_percpu_devid() done in intc map function */
287 ret = request_percpu_irq(arc_timer_irq, timer_irq_handler,
288 "Timer0 (per-cpu-tick)", evt);
290 pr_err("Unable to register interrupt\n");
292 enable_percpu_irq(arc_timer_irq, 0);
296 * Called from start_kernel() - boot CPU only
298 * -Sets up h/w timers as applicable on boot cpu
299 * -Also sets up any global state needed for timer subsystem:
300 * - for "counting" timer, registers a clocksource, usable across CPUs
301 * (provided that underlying counter h/w is synchronized across cores)
302 * - for "event" timer, sets up TIMER0 IRQ (as that is platform agnostic)
304 void __init time_init(void)
310 * sets up the timekeeping free-flowing counter which also returns
311 * whether the counter is usable as clocksource
313 if (arc_counter_setup())
315 * CLK upto 4.29 GHz can be safely represented in 32 bits
316 * because Max 32 bit number is 4,294,967,295
318 clocksource_register_hz(&arc_counter, arc_get_core_freq());
320 arc_clockevent_setup();