blob: e97be743d47b3af275528b3d6e02ce500735368f [file] [log] [blame]
Vineet Guptad8005e62013-01-18 15:12:18 +05301/*
2 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
3 *
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.
7 *
8 * vineetg: Jan 1011
9 * -sched_clock( ) no longer jiffies based. Uses the same clocksource
10 * as gtod
11 *
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
16 *
17 * Vineetg: Mar 2008: Forked off from time.c which now is time-jiff.c
18 */
19
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
24 *
25 * We've designated TIMER0 for events (clockevents)
26 * while TIMER1 for free running (clocksource)
27 *
28 * Newer ARC700 cores have 64bit clk fetching RTSC insn, preferred over TIMER1
Vineet Gupta565a9b42015-03-07 17:06:09 +053029 * which however is currently broken
Vineet Guptad8005e62013-01-18 15:12:18 +053030 */
31
Vineet Guptad8005e62013-01-18 15:12:18 +053032#include <linux/interrupt.h>
33#include <linux/module.h>
Vineet Guptad8005e62013-01-18 15:12:18 +053034#include <linux/kernel.h>
Vineet Guptad8005e62013-01-18 15:12:18 +053035#include <linux/time.h>
36#include <linux/init.h>
Vineet Guptad8005e62013-01-18 15:12:18 +053037#include <linux/clocksource.h>
38#include <linux/clockchips.h>
Noam Camuseec3c582016-01-01 15:48:49 +053039#include <linux/cpu.h>
Vineet Guptad8005e62013-01-18 15:12:18 +053040#include <asm/irq.h>
41#include <asm/arcregs.h>
42#include <asm/clk.h>
Vineet Gupta03a6d282013-01-18 15:12:26 +053043#include <asm/mach_desc.h>
Vineet Guptad8005e62013-01-18 15:12:18 +053044
Vineet Gupta72d72882014-12-24 18:41:55 +053045#include <asm/mcip.h>
46
Vineet Guptada1677b02013-05-14 13:28:17 +053047/* Timer related Aux registers */
48#define ARC_REG_TIMER0_LIMIT 0x23 /* timer 0 limit */
49#define ARC_REG_TIMER0_CTRL 0x22 /* timer 0 control */
50#define ARC_REG_TIMER0_CNT 0x21 /* timer 0 count */
51#define ARC_REG_TIMER1_LIMIT 0x102 /* timer 1 limit */
52#define ARC_REG_TIMER1_CTRL 0x101 /* timer 1 control */
53#define ARC_REG_TIMER1_CNT 0x100 /* timer 1 count */
54
Adam Buchbinder7423cc02016-02-23 15:24:55 -080055#define TIMER_CTRL_IE (1 << 0) /* Interrupt when Count reaches limit */
56#define TIMER_CTRL_NH (1 << 1) /* Count only when CPU NOT halted */
Vineet Guptada1677b02013-05-14 13:28:17 +053057
Vineet Guptad8005e62013-01-18 15:12:18 +053058#define ARC_TIMER_MAX 0xFFFFFFFF
59
60/********** Clock Source Device *********/
61
Vineet Guptad584f0f2016-01-22 14:27:50 +053062#ifdef CONFIG_ARC_HAS_GFRC
Vineet Gupta72d72882014-12-24 18:41:55 +053063
64static int arc_counter_setup(void)
65{
66 return 1;
67}
68
69static cycle_t arc_counter_read(struct clocksource *cs)
70{
71 unsigned long flags;
72 union {
73#ifdef CONFIG_CPU_BIG_ENDIAN
74 struct { u32 h, l; };
75#else
76 struct { u32 l, h; };
77#endif
78 cycle_t full;
79 } stamp;
80
81 local_irq_save(flags);
82
Vineet Guptad584f0f2016-01-22 14:27:50 +053083 __mcip_cmd(CMD_GFRC_READ_LO, 0);
Vineet Gupta72d72882014-12-24 18:41:55 +053084 stamp.l = read_aux_reg(ARC_REG_MCIP_READBACK);
85
Vineet Guptad584f0f2016-01-22 14:27:50 +053086 __mcip_cmd(CMD_GFRC_READ_HI, 0);
Vineet Gupta72d72882014-12-24 18:41:55 +053087 stamp.h = read_aux_reg(ARC_REG_MCIP_READBACK);
88
89 local_irq_restore(flags);
90
91 return stamp.full;
92}
93
94static struct clocksource arc_counter = {
Vineet Guptad584f0f2016-01-22 14:27:50 +053095 .name = "ARConnect GFRC",
Vineet Gupta72d72882014-12-24 18:41:55 +053096 .rating = 400,
97 .read = arc_counter_read,
98 .mask = CLOCKSOURCE_MASK(64),
99 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
100};
101
102#else
103
Vineet Guptaaa93e8e2013-11-07 14:57:16 +0530104#ifdef CONFIG_ARC_HAS_RTC
105
106#define AUX_RTC_CTRL 0x103
107#define AUX_RTC_LOW 0x104
108#define AUX_RTC_HIGH 0x105
109
110int arc_counter_setup(void)
111{
112 write_aux_reg(AUX_RTC_CTRL, 1);
113
114 /* Not usable in SMP */
115 return !IS_ENABLED(CONFIG_SMP);
116}
117
118static cycle_t arc_counter_read(struct clocksource *cs)
119{
120 unsigned long status;
121 union {
122#ifdef CONFIG_CPU_BIG_ENDIAN
123 struct { u32 high, low; };
124#else
125 struct { u32 low, high; };
126#endif
127 cycle_t full;
128 } stamp;
129
130
131 __asm__ __volatile(
132 "1: \n"
133 " lr %0, [AUX_RTC_LOW] \n"
134 " lr %1, [AUX_RTC_HIGH] \n"
135 " lr %2, [AUX_RTC_CTRL] \n"
136 " bbit0.nt %2, 31, 1b \n"
137 : "=r" (stamp.low), "=r" (stamp.high), "=r" (status));
138
139 return stamp.full;
140}
141
142static struct clocksource arc_counter = {
143 .name = "ARCv2 RTC",
144 .rating = 350,
145 .read = arc_counter_read,
146 .mask = CLOCKSOURCE_MASK(64),
147 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
148};
149
150#else /* !CONFIG_ARC_HAS_RTC */
151
Vineet Guptad8005e62013-01-18 15:12:18 +0530152/*
153 * set 32bit TIMER1 to keep counting monotonically and wraparound
154 */
Paul Gortmakerce759952013-06-24 15:30:15 -0400155int arc_counter_setup(void)
Vineet Guptad8005e62013-01-18 15:12:18 +0530156{
157 write_aux_reg(ARC_REG_TIMER1_LIMIT, ARC_TIMER_MAX);
158 write_aux_reg(ARC_REG_TIMER1_CNT, 0);
159 write_aux_reg(ARC_REG_TIMER1_CTRL, TIMER_CTRL_NH);
160
Vineet Gupta5b9bd172015-03-07 16:59:38 +0530161 /* Not usable in SMP */
162 return !IS_ENABLED(CONFIG_SMP);
Vineet Guptad8005e62013-01-18 15:12:18 +0530163}
164
165static cycle_t arc_counter_read(struct clocksource *cs)
166{
167 return (cycle_t) read_aux_reg(ARC_REG_TIMER1_CNT);
168}
169
170static struct clocksource arc_counter = {
171 .name = "ARC Timer1",
172 .rating = 300,
173 .read = arc_counter_read,
174 .mask = CLOCKSOURCE_MASK(32),
175 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
176};
177
Vineet Guptaaa93e8e2013-11-07 14:57:16 +0530178#endif
Vineet Gupta72d72882014-12-24 18:41:55 +0530179#endif
Vineet Guptaaa93e8e2013-11-07 14:57:16 +0530180
Vineet Guptad8005e62013-01-18 15:12:18 +0530181/********** Clock Event Device *********/
182
Noam Camuseec3c582016-01-01 15:48:49 +0530183static int arc_timer_irq = TIMER0_IRQ;
184
Vineet Guptad8005e62013-01-18 15:12:18 +0530185/*
Vineet Guptac9a98e182014-06-25 17:14:03 +0530186 * Arm the timer to interrupt after @cycles
Vineet Guptad8005e62013-01-18 15:12:18 +0530187 * The distinction for oneshot/periodic is done in arc_event_timer_ack() below
188 */
Vineet Guptac9a98e182014-06-25 17:14:03 +0530189static void arc_timer_event_setup(unsigned int cycles)
Vineet Guptad8005e62013-01-18 15:12:18 +0530190{
Vineet Guptac9a98e182014-06-25 17:14:03 +0530191 write_aux_reg(ARC_REG_TIMER0_LIMIT, cycles);
Vineet Guptad8005e62013-01-18 15:12:18 +0530192 write_aux_reg(ARC_REG_TIMER0_CNT, 0); /* start from 0 */
193
194 write_aux_reg(ARC_REG_TIMER0_CTRL, TIMER_CTRL_IE | TIMER_CTRL_NH);
195}
196
Vineet Guptad8005e62013-01-18 15:12:18 +0530197
198static int arc_clkevent_set_next_event(unsigned long delta,
199 struct clock_event_device *dev)
200{
201 arc_timer_event_setup(delta);
202 return 0;
203}
204
Viresh Kumaraeec6cd2015-07-16 16:56:14 +0530205static int arc_clkevent_set_periodic(struct clock_event_device *dev)
Vineet Guptad8005e62013-01-18 15:12:18 +0530206{
Viresh Kumaraeec6cd2015-07-16 16:56:14 +0530207 /*
208 * At X Hz, 1 sec = 1000ms -> X cycles;
209 * 10ms -> X / 100 cycles
210 */
211 arc_timer_event_setup(arc_get_core_freq() / HZ);
212 return 0;
Vineet Guptad8005e62013-01-18 15:12:18 +0530213}
214
215static DEFINE_PER_CPU(struct clock_event_device, arc_clockevent_device) = {
Viresh Kumaraeec6cd2015-07-16 16:56:14 +0530216 .name = "ARC Timer0",
217 .features = CLOCK_EVT_FEAT_ONESHOT |
218 CLOCK_EVT_FEAT_PERIODIC,
219 .rating = 300,
Viresh Kumaraeec6cd2015-07-16 16:56:14 +0530220 .set_next_event = arc_clkevent_set_next_event,
221 .set_state_periodic = arc_clkevent_set_periodic,
Vineet Guptad8005e62013-01-18 15:12:18 +0530222};
223
224static irqreturn_t timer_irq_handler(int irq, void *dev_id)
225{
Vineet Guptaf8b34c32014-01-25 00:42:37 +0530226 /*
227 * Note that generic IRQ core could have passed @evt for @dev_id if
228 * irq_set_chip_and_handler() asked for handle_percpu_devid_irq()
229 */
230 struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device);
Viresh Kumaraeec6cd2015-07-16 16:56:14 +0530231 int irq_reenable = clockevent_state_periodic(evt);
Vineet Guptad8005e62013-01-18 15:12:18 +0530232
Vineet Guptaf8b34c32014-01-25 00:42:37 +0530233 /*
234 * Any write to CTRL reg ACks the interrupt, we rewrite the
235 * Count when [N]ot [H]alted bit.
236 * And re-arm it if perioid by [I]nterrupt [E]nable bit
237 */
238 write_aux_reg(ARC_REG_TIMER0_CTRL, irq_reenable | TIMER_CTRL_NH);
239
240 evt->event_handler(evt);
241
Vineet Guptad8005e62013-01-18 15:12:18 +0530242 return IRQ_HANDLED;
243}
244
Noam Camuseec3c582016-01-01 15:48:49 +0530245static int arc_timer_cpu_notify(struct notifier_block *self,
246 unsigned long action, void *hcpu)
Vineet Guptad8005e62013-01-18 15:12:18 +0530247{
Vineet Gupta2d4899f2014-05-08 14:06:38 +0530248 struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device);
Vineet Guptad8005e62013-01-18 15:12:18 +0530249
Noam Camuseec3c582016-01-01 15:48:49 +0530250 evt->cpumask = cpumask_of(smp_processor_id());
251
252 switch (action & ~CPU_TASKS_FROZEN) {
253 case CPU_STARTING:
254 clockevents_config_and_register(evt, arc_get_core_freq(),
255 0, ULONG_MAX);
256 enable_percpu_irq(arc_timer_irq, 0);
257 break;
258 case CPU_DYING:
259 disable_percpu_irq(arc_timer_irq);
260 break;
261 }
262
263 return NOTIFY_OK;
264}
265
266static struct notifier_block arc_timer_cpu_nb = {
267 .notifier_call = arc_timer_cpu_notify,
268};
269
270/*
271 * clockevent setup for boot CPU
272 */
273static void __init arc_clockevent_setup(void)
274{
275 struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device);
276 int ret;
277
278 register_cpu_notifier(&arc_timer_cpu_nb);
279
280 evt->cpumask = cpumask_of(smp_processor_id());
Vineet Gupta2d4899f2014-05-08 14:06:38 +0530281 clockevents_config_and_register(evt, arc_get_core_freq(),
Uwe Kleine-König55c2e262013-09-24 23:05:37 +0200282 0, ARC_TIMER_MAX);
Vineet Guptad8005e62013-01-18 15:12:18 +0530283
Noam Camuseec3c582016-01-01 15:48:49 +0530284 /* Needs apriori irq_set_percpu_devid() done in intc map function */
285 ret = request_percpu_irq(arc_timer_irq, timer_irq_handler,
286 "Timer0 (per-cpu-tick)", evt);
287 if (ret)
288 pr_err("Unable to register interrupt\n");
Vineet Gupta56957942016-01-28 12:56:03 +0530289
Noam Camuseec3c582016-01-01 15:48:49 +0530290 enable_percpu_irq(arc_timer_irq, 0);
Vineet Guptad8005e62013-01-18 15:12:18 +0530291}
292
293/*
294 * Called from start_kernel() - boot CPU only
295 *
296 * -Sets up h/w timers as applicable on boot cpu
297 * -Also sets up any global state needed for timer subsystem:
298 * - for "counting" timer, registers a clocksource, usable across CPUs
299 * (provided that underlying counter h/w is synchronized across cores)
300 * - for "event" timer, sets up TIMER0 IRQ (as that is platform agnostic)
301 */
302void __init time_init(void)
303{
304 /*
305 * sets up the timekeeping free-flowing counter which also returns
306 * whether the counter is usable as clocksource
307 */
308 if (arc_counter_setup())
309 /*
310 * CLK upto 4.29 GHz can be safely represented in 32 bits
311 * because Max 32 bit number is 4,294,967,295
312 */
313 clocksource_register_hz(&arc_counter, arc_get_core_freq());
314
Noam Camuseec3c582016-01-01 15:48:49 +0530315 arc_clockevent_setup();
Vineet Guptad8005e62013-01-18 15:12:18 +0530316}