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/spinlock.h>
33 #include <linux/interrupt.h>
34 #include <linux/module.h>
35 #include <linux/sched.h>
36 #include <linux/kernel.h>
37 #include <linux/time.h>
38 #include <linux/init.h>
39 #include <linux/timex.h>
40 #include <linux/profile.h>
41 #include <linux/clocksource.h>
42 #include <linux/clockchips.h>
44 #include <asm/arcregs.h>
46 #include <asm/mach_desc.h>
50 /* Timer related Aux registers */
51 #define ARC_REG_TIMER0_LIMIT 0x23 /* timer 0 limit */
52 #define ARC_REG_TIMER0_CTRL 0x22 /* timer 0 control */
53 #define ARC_REG_TIMER0_CNT 0x21 /* timer 0 count */
54 #define ARC_REG_TIMER1_LIMIT 0x102 /* timer 1 limit */
55 #define ARC_REG_TIMER1_CTRL 0x101 /* timer 1 control */
56 #define ARC_REG_TIMER1_CNT 0x100 /* timer 1 count */
58 #define TIMER_CTRL_IE (1 << 0) /* Interupt when Count reachs limit */
59 #define TIMER_CTRL_NH (1 << 1) /* Count only when CPU NOT halted */
61 #define ARC_TIMER_MAX 0xFFFFFFFF
63 /********** Clock Source Device *********/
65 #ifdef CONFIG_ARC_HAS_GRTC
67 static int arc_counter_setup(void)
72 static cycle_t
arc_counter_read(struct clocksource
*cs
)
76 #ifdef CONFIG_CPU_BIG_ENDIAN
84 local_irq_save(flags
);
86 __mcip_cmd(CMD_GRTC_READ_LO
, 0);
87 stamp
.l
= read_aux_reg(ARC_REG_MCIP_READBACK
);
89 __mcip_cmd(CMD_GRTC_READ_HI
, 0);
90 stamp
.h
= read_aux_reg(ARC_REG_MCIP_READBACK
);
92 local_irq_restore(flags
);
97 static struct clocksource arc_counter
= {
98 .name
= "ARConnect GRTC",
100 .read
= arc_counter_read
,
101 .mask
= CLOCKSOURCE_MASK(64),
102 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
107 #ifdef CONFIG_ARC_HAS_RTC
109 #define AUX_RTC_CTRL 0x103
110 #define AUX_RTC_LOW 0x104
111 #define AUX_RTC_HIGH 0x105
113 int arc_counter_setup(void)
115 write_aux_reg(AUX_RTC_CTRL
, 1);
117 /* Not usable in SMP */
118 return !IS_ENABLED(CONFIG_SMP
);
121 static cycle_t
arc_counter_read(struct clocksource
*cs
)
123 unsigned long status
;
125 #ifdef CONFIG_CPU_BIG_ENDIAN
126 struct { u32 high
, low
; };
128 struct { u32 low
, high
; };
134 * hardware has an internal state machine which tracks readout of
135 * low/high and updates the CTRL.status if
136 * - interrupt/exception taken between the two reads
137 * - high increments after low has been read
140 stamp
.low
= read_aux_reg(AUX_RTC_LOW
);
141 stamp
.high
= read_aux_reg(AUX_RTC_HIGH
);
142 status
= read_aux_reg(AUX_RTC_CTRL
);
143 } while (!(status
& _BITUL(31)));
148 static struct clocksource arc_counter
= {
151 .read
= arc_counter_read
,
152 .mask
= CLOCKSOURCE_MASK(64),
153 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
156 #else /* !CONFIG_ARC_HAS_RTC */
159 * set 32bit TIMER1 to keep counting monotonically and wraparound
161 int arc_counter_setup(void)
163 write_aux_reg(ARC_REG_TIMER1_LIMIT
, ARC_TIMER_MAX
);
164 write_aux_reg(ARC_REG_TIMER1_CNT
, 0);
165 write_aux_reg(ARC_REG_TIMER1_CTRL
, TIMER_CTRL_NH
);
167 /* Not usable in SMP */
168 return !IS_ENABLED(CONFIG_SMP
);
171 static cycle_t
arc_counter_read(struct clocksource
*cs
)
173 return (cycle_t
) read_aux_reg(ARC_REG_TIMER1_CNT
);
176 static struct clocksource arc_counter
= {
177 .name
= "ARC Timer1",
179 .read
= arc_counter_read
,
180 .mask
= CLOCKSOURCE_MASK(32),
181 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
,
187 /********** Clock Event Device *********/
190 * Arm the timer to interrupt after @cycles
191 * The distinction for oneshot/periodic is done in arc_event_timer_ack() below
193 static void arc_timer_event_setup(unsigned int cycles
)
195 write_aux_reg(ARC_REG_TIMER0_LIMIT
, cycles
);
196 write_aux_reg(ARC_REG_TIMER0_CNT
, 0); /* start from 0 */
198 write_aux_reg(ARC_REG_TIMER0_CTRL
, TIMER_CTRL_IE
| TIMER_CTRL_NH
);
202 static int arc_clkevent_set_next_event(unsigned long delta
,
203 struct clock_event_device
*dev
)
205 arc_timer_event_setup(delta
);
209 static int arc_clkevent_set_periodic(struct clock_event_device
*dev
)
212 * At X Hz, 1 sec = 1000ms -> X cycles;
213 * 10ms -> X / 100 cycles
215 arc_timer_event_setup(arc_get_core_freq() / HZ
);
219 static DEFINE_PER_CPU(struct clock_event_device
, arc_clockevent_device
) = {
220 .name
= "ARC Timer0",
221 .features
= CLOCK_EVT_FEAT_ONESHOT
|
222 CLOCK_EVT_FEAT_PERIODIC
,
224 .irq
= TIMER0_IRQ
, /* hardwired, no need for resources */
225 .set_next_event
= arc_clkevent_set_next_event
,
226 .set_state_periodic
= arc_clkevent_set_periodic
,
229 static irqreturn_t
timer_irq_handler(int irq
, void *dev_id
)
232 * Note that generic IRQ core could have passed @evt for @dev_id if
233 * irq_set_chip_and_handler() asked for handle_percpu_devid_irq()
235 struct clock_event_device
*evt
= this_cpu_ptr(&arc_clockevent_device
);
236 int irq_reenable
= clockevent_state_periodic(evt
);
239 * Any write to CTRL reg ACks the interrupt, we rewrite the
240 * Count when [N]ot [H]alted bit.
241 * And re-arm it if perioid by [I]nterrupt [E]nable bit
243 write_aux_reg(ARC_REG_TIMER0_CTRL
, irq_reenable
| TIMER_CTRL_NH
);
245 evt
->event_handler(evt
);
251 * Setup the local event timer for @cpu
253 void arc_local_timer_setup()
255 struct clock_event_device
*evt
= this_cpu_ptr(&arc_clockevent_device
);
256 int cpu
= smp_processor_id();
258 evt
->cpumask
= cpumask_of(cpu
);
259 clockevents_config_and_register(evt
, arc_get_core_freq(),
262 /* setup the per-cpu timer IRQ handler - for all cpus */
263 arc_request_percpu_irq(TIMER0_IRQ
, cpu
, timer_irq_handler
,
264 "Timer0 (per-cpu-tick)", evt
);
268 * Called from start_kernel() - boot CPU only
270 * -Sets up h/w timers as applicable on boot cpu
271 * -Also sets up any global state needed for timer subsystem:
272 * - for "counting" timer, registers a clocksource, usable across CPUs
273 * (provided that underlying counter h/w is synchronized across cores)
274 * - for "event" timer, sets up TIMER0 IRQ (as that is platform agnostic)
276 void __init
time_init(void)
279 * sets up the timekeeping free-flowing counter which also returns
280 * whether the counter is usable as clocksource
282 if (arc_counter_setup())
284 * CLK upto 4.29 GHz can be safely represented in 32 bits
285 * because Max 32 bit number is 4,294,967,295
287 clocksource_register_hz(&arc_counter
, arc_get_core_freq());
289 /* sets up the periodic event timer */
290 arc_local_timer_setup();