blob: d3f3750a0d2d648e8a66a9c063cada5d117294b0 [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>
Noam Camus69fbd092016-01-14 12:20:08 +053033#include <linux/clk.h>
34#include <linux/clk-provider.h>
Vineet Guptad8005e62013-01-18 15:12:18 +053035#include <linux/clocksource.h>
36#include <linux/clockchips.h>
Noam Camuseec3c582016-01-01 15:48:49 +053037#include <linux/cpu.h>
Vineet Gupta77c8d0d2016-01-01 17:58:45 +053038#include <linux/of.h>
39#include <linux/of_irq.h>
Vineet Guptad8005e62013-01-18 15:12:18 +053040#include <asm/irq.h>
41#include <asm/arcregs.h>
Vineet Guptad8005e62013-01-18 15:12:18 +053042
Vineet Gupta72d72882014-12-24 18:41:55 +053043#include <asm/mcip.h>
44
Vineet Guptada1677b2013-05-14 13:28:17 +053045/* Timer related Aux registers */
46#define ARC_REG_TIMER0_LIMIT 0x23 /* timer 0 limit */
47#define ARC_REG_TIMER0_CTRL 0x22 /* timer 0 control */
48#define ARC_REG_TIMER0_CNT 0x21 /* timer 0 count */
49#define ARC_REG_TIMER1_LIMIT 0x102 /* timer 1 limit */
50#define ARC_REG_TIMER1_CTRL 0x101 /* timer 1 control */
51#define ARC_REG_TIMER1_CNT 0x100 /* timer 1 count */
52
Adam Buchbinder7423cc02016-02-23 15:24:55 -080053#define TIMER_CTRL_IE (1 << 0) /* Interrupt when Count reaches limit */
54#define TIMER_CTRL_NH (1 << 1) /* Count only when CPU NOT halted */
Vineet Guptada1677b2013-05-14 13:28:17 +053055
Vineet Guptad8005e62013-01-18 15:12:18 +053056#define ARC_TIMER_MAX 0xFFFFFFFF
57
Vineet Gupta77c8d0d2016-01-01 17:58:45 +053058static unsigned long arc_timer_freq;
59
60static int noinline arc_get_timer_clk(struct device_node *node)
61{
62 struct clk *clk;
63 int ret;
64
65 clk = of_clk_get(node, 0);
66 if (IS_ERR(clk)) {
67 pr_err("timer missing clk");
68 return PTR_ERR(clk);
69 }
70
71 ret = clk_prepare_enable(clk);
72 if (ret) {
73 pr_err("Couldn't enable parent clk\n");
74 return ret;
75 }
76
77 arc_timer_freq = clk_get_rate(clk);
78
79 return 0;
80}
81
Vineet Guptad8005e62013-01-18 15:12:18 +053082/********** Clock Source Device *********/
83
Vineet Guptad584f0f2016-01-22 14:27:50 +053084#ifdef CONFIG_ARC_HAS_GFRC
Vineet Gupta72d72882014-12-24 18:41:55 +053085
Vineet Guptae608b532016-01-01 18:05:48 +053086static cycle_t arc_read_gfrc(struct clocksource *cs)
Vineet Gupta72d72882014-12-24 18:41:55 +053087{
88 unsigned long flags;
Vineet Gupta2cd690e2016-11-03 11:38:52 -070089 u32 l, h;
Vineet Gupta72d72882014-12-24 18:41:55 +053090
91 local_irq_save(flags);
92
Vineet Guptad584f0f2016-01-22 14:27:50 +053093 __mcip_cmd(CMD_GFRC_READ_LO, 0);
Vineet Gupta2cd690e2016-11-03 11:38:52 -070094 l = read_aux_reg(ARC_REG_MCIP_READBACK);
Vineet Gupta72d72882014-12-24 18:41:55 +053095
Vineet Guptad584f0f2016-01-22 14:27:50 +053096 __mcip_cmd(CMD_GFRC_READ_HI, 0);
Vineet Gupta2cd690e2016-11-03 11:38:52 -070097 h = read_aux_reg(ARC_REG_MCIP_READBACK);
Vineet Gupta72d72882014-12-24 18:41:55 +053098
99 local_irq_restore(flags);
100
Vineet Gupta2cd690e2016-11-03 11:38:52 -0700101 return (((cycle_t)h) << 32) | l;
Vineet Gupta72d72882014-12-24 18:41:55 +0530102}
103
Vineet Guptae608b532016-01-01 18:05:48 +0530104static struct clocksource arc_counter_gfrc = {
Vineet Guptad584f0f2016-01-22 14:27:50 +0530105 .name = "ARConnect GFRC",
Vineet Gupta72d72882014-12-24 18:41:55 +0530106 .rating = 400,
Vineet Guptae608b532016-01-01 18:05:48 +0530107 .read = arc_read_gfrc,
Vineet Gupta72d72882014-12-24 18:41:55 +0530108 .mask = CLOCKSOURCE_MASK(64),
109 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
110};
111
Daniel Lezcano43d75602016-06-15 14:50:12 +0200112static int __init arc_cs_setup_gfrc(struct device_node *node)
Vineet Guptae608b532016-01-01 18:05:48 +0530113{
Vineet Guptaec7cb872016-10-31 13:02:31 -0700114 struct mcip_bcr mp;
Vineet Guptae608b532016-01-01 18:05:48 +0530115 int ret;
116
Vineet Guptaec7cb872016-10-31 13:02:31 -0700117 READ_BCR(ARC_REG_MCIP_BCR, mp);
118 if (!mp.gfrc) {
119 pr_warn("Global-64-bit-Ctr clocksource not detected");
Daniel Lezcano43d75602016-06-15 14:50:12 +0200120 return -ENXIO;
Vineet Guptaec7cb872016-10-31 13:02:31 -0700121 }
Vineet Guptae608b532016-01-01 18:05:48 +0530122
123 ret = arc_get_timer_clk(node);
124 if (ret)
Daniel Lezcano43d75602016-06-15 14:50:12 +0200125 return ret;
Vineet Guptae608b532016-01-01 18:05:48 +0530126
Daniel Lezcano43d75602016-06-15 14:50:12 +0200127 return clocksource_register_hz(&arc_counter_gfrc, arc_timer_freq);
Vineet Guptae608b532016-01-01 18:05:48 +0530128}
Daniel Lezcano177cf6e2016-06-07 00:27:44 +0200129CLOCKSOURCE_OF_DECLARE(arc_gfrc, "snps,archs-timer-gfrc", arc_cs_setup_gfrc);
Vineet Guptae608b532016-01-01 18:05:48 +0530130
131#endif
Vineet Gupta72d72882014-12-24 18:41:55 +0530132
Vineet Guptaaa93e8e2013-11-07 14:57:16 +0530133#ifdef CONFIG_ARC_HAS_RTC
134
135#define AUX_RTC_CTRL 0x103
136#define AUX_RTC_LOW 0x104
137#define AUX_RTC_HIGH 0x105
138
Vineet Guptae608b532016-01-01 18:05:48 +0530139static cycle_t arc_read_rtc(struct clocksource *cs)
Vineet Guptaaa93e8e2013-11-07 14:57:16 +0530140{
141 unsigned long status;
Vineet Gupta2cd690e2016-11-03 11:38:52 -0700142 u32 l, h;
Vineet Guptaaa93e8e2013-11-07 14:57:16 +0530143
Vineet Gupta922cc172016-10-31 14:09:52 -0700144 /*
145 * hardware has an internal state machine which tracks readout of
146 * low/high and updates the CTRL.status if
147 * - interrupt/exception taken between the two reads
148 * - high increments after low has been read
149 */
150 do {
Vineet Gupta2cd690e2016-11-03 11:38:52 -0700151 l = read_aux_reg(AUX_RTC_LOW);
152 h = read_aux_reg(AUX_RTC_HIGH);
Vineet Gupta922cc172016-10-31 14:09:52 -0700153 status = read_aux_reg(AUX_RTC_CTRL);
154 } while (!(status & _BITUL(31)));
Vineet Guptaaa93e8e2013-11-07 14:57:16 +0530155
Vineet Gupta2cd690e2016-11-03 11:38:52 -0700156 return (((cycle_t)h) << 32) | l;
Vineet Guptaaa93e8e2013-11-07 14:57:16 +0530157}
158
Vineet Guptae608b532016-01-01 18:05:48 +0530159static struct clocksource arc_counter_rtc = {
Vineet Guptaaa93e8e2013-11-07 14:57:16 +0530160 .name = "ARCv2 RTC",
161 .rating = 350,
Vineet Guptae608b532016-01-01 18:05:48 +0530162 .read = arc_read_rtc,
Vineet Guptaaa93e8e2013-11-07 14:57:16 +0530163 .mask = CLOCKSOURCE_MASK(64),
164 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
165};
166
Daniel Lezcano43d75602016-06-15 14:50:12 +0200167static int __init arc_cs_setup_rtc(struct device_node *node)
Vineet Guptae608b532016-01-01 18:05:48 +0530168{
Vineet Guptaec7cb872016-10-31 13:02:31 -0700169 struct bcr_timer timer;
Vineet Guptae608b532016-01-01 18:05:48 +0530170 int ret;
171
Vineet Guptaec7cb872016-10-31 13:02:31 -0700172 READ_BCR(ARC_REG_TIMERS_BCR, timer);
173 if (!timer.rtc) {
174 pr_warn("Local-64-bit-Ctr clocksource not detected");
Daniel Lezcano43d75602016-06-15 14:50:12 +0200175 return -ENXIO;
Vineet Guptaec7cb872016-10-31 13:02:31 -0700176 }
Vineet Guptae608b532016-01-01 18:05:48 +0530177
178 /* Local to CPU hence not usable in SMP */
Vineet Guptaec7cb872016-10-31 13:02:31 -0700179 if (IS_ENABLED(CONFIG_SMP)) {
180 pr_warn("Local-64-bit-Ctr not usable in SMP");
Daniel Lezcano43d75602016-06-15 14:50:12 +0200181 return -EINVAL;
Vineet Guptaec7cb872016-10-31 13:02:31 -0700182 }
Vineet Guptae608b532016-01-01 18:05:48 +0530183
184 ret = arc_get_timer_clk(node);
185 if (ret)
Daniel Lezcano43d75602016-06-15 14:50:12 +0200186 return ret;
Vineet Guptae608b532016-01-01 18:05:48 +0530187
188 write_aux_reg(AUX_RTC_CTRL, 1);
189
Daniel Lezcano43d75602016-06-15 14:50:12 +0200190 return clocksource_register_hz(&arc_counter_rtc, arc_timer_freq);
Vineet Guptae608b532016-01-01 18:05:48 +0530191}
Daniel Lezcano177cf6e2016-06-07 00:27:44 +0200192CLOCKSOURCE_OF_DECLARE(arc_rtc, "snps,archs-timer-rtc", arc_cs_setup_rtc);
Vineet Guptae608b532016-01-01 18:05:48 +0530193
194#endif
Vineet Guptaaa93e8e2013-11-07 14:57:16 +0530195
Vineet Guptad8005e62013-01-18 15:12:18 +0530196/*
Vineet Guptae608b532016-01-01 18:05:48 +0530197 * 32bit TIMER1 to keep counting monotonically and wraparound
Vineet Guptad8005e62013-01-18 15:12:18 +0530198 */
Vineet Guptad8005e62013-01-18 15:12:18 +0530199
Vineet Guptae608b532016-01-01 18:05:48 +0530200static cycle_t arc_read_timer1(struct clocksource *cs)
Vineet Guptad8005e62013-01-18 15:12:18 +0530201{
202 return (cycle_t) read_aux_reg(ARC_REG_TIMER1_CNT);
203}
204
Vineet Guptae608b532016-01-01 18:05:48 +0530205static struct clocksource arc_counter_timer1 = {
Vineet Guptad8005e62013-01-18 15:12:18 +0530206 .name = "ARC Timer1",
207 .rating = 300,
Vineet Guptae608b532016-01-01 18:05:48 +0530208 .read = arc_read_timer1,
Vineet Guptad8005e62013-01-18 15:12:18 +0530209 .mask = CLOCKSOURCE_MASK(32),
210 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
211};
212
Daniel Lezcano43d75602016-06-15 14:50:12 +0200213static int __init arc_cs_setup_timer1(struct device_node *node)
Vineet Guptae608b532016-01-01 18:05:48 +0530214{
215 int ret;
216
217 /* Local to CPU hence not usable in SMP */
218 if (IS_ENABLED(CONFIG_SMP))
Daniel Lezcano43d75602016-06-15 14:50:12 +0200219 return -EINVAL;
Vineet Guptae608b532016-01-01 18:05:48 +0530220
221 ret = arc_get_timer_clk(node);
222 if (ret)
Daniel Lezcano43d75602016-06-15 14:50:12 +0200223 return ret;
Vineet Guptae608b532016-01-01 18:05:48 +0530224
225 write_aux_reg(ARC_REG_TIMER1_LIMIT, ARC_TIMER_MAX);
226 write_aux_reg(ARC_REG_TIMER1_CNT, 0);
227 write_aux_reg(ARC_REG_TIMER1_CTRL, TIMER_CTRL_NH);
228
Daniel Lezcano43d75602016-06-15 14:50:12 +0200229 return clocksource_register_hz(&arc_counter_timer1, arc_timer_freq);
Vineet Guptae608b532016-01-01 18:05:48 +0530230}
Vineet Guptaaa93e8e2013-11-07 14:57:16 +0530231
Vineet Guptad8005e62013-01-18 15:12:18 +0530232/********** Clock Event Device *********/
233
Vineet Gupta77c8d0d2016-01-01 17:58:45 +0530234static int arc_timer_irq;
Noam Camuseec3c582016-01-01 15:48:49 +0530235
Vineet Guptad8005e62013-01-18 15:12:18 +0530236/*
Vineet Guptac9a98e182014-06-25 17:14:03 +0530237 * Arm the timer to interrupt after @cycles
Vineet Guptad8005e62013-01-18 15:12:18 +0530238 * The distinction for oneshot/periodic is done in arc_event_timer_ack() below
239 */
Vineet Guptac9a98e182014-06-25 17:14:03 +0530240static void arc_timer_event_setup(unsigned int cycles)
Vineet Guptad8005e62013-01-18 15:12:18 +0530241{
Vineet Guptac9a98e182014-06-25 17:14:03 +0530242 write_aux_reg(ARC_REG_TIMER0_LIMIT, cycles);
Vineet Guptad8005e62013-01-18 15:12:18 +0530243 write_aux_reg(ARC_REG_TIMER0_CNT, 0); /* start from 0 */
244
245 write_aux_reg(ARC_REG_TIMER0_CTRL, TIMER_CTRL_IE | TIMER_CTRL_NH);
246}
247
Vineet Guptad8005e62013-01-18 15:12:18 +0530248
249static int arc_clkevent_set_next_event(unsigned long delta,
250 struct clock_event_device *dev)
251{
252 arc_timer_event_setup(delta);
253 return 0;
254}
255
Viresh Kumaraeec6cd2015-07-16 16:56:14 +0530256static int arc_clkevent_set_periodic(struct clock_event_device *dev)
Vineet Guptad8005e62013-01-18 15:12:18 +0530257{
Viresh Kumaraeec6cd2015-07-16 16:56:14 +0530258 /*
259 * At X Hz, 1 sec = 1000ms -> X cycles;
260 * 10ms -> X / 100 cycles
261 */
Vineet Gupta77c8d0d2016-01-01 17:58:45 +0530262 arc_timer_event_setup(arc_timer_freq / HZ);
Viresh Kumaraeec6cd2015-07-16 16:56:14 +0530263 return 0;
Vineet Guptad8005e62013-01-18 15:12:18 +0530264}
265
266static DEFINE_PER_CPU(struct clock_event_device, arc_clockevent_device) = {
Viresh Kumaraeec6cd2015-07-16 16:56:14 +0530267 .name = "ARC Timer0",
268 .features = CLOCK_EVT_FEAT_ONESHOT |
269 CLOCK_EVT_FEAT_PERIODIC,
270 .rating = 300,
Viresh Kumaraeec6cd2015-07-16 16:56:14 +0530271 .set_next_event = arc_clkevent_set_next_event,
272 .set_state_periodic = arc_clkevent_set_periodic,
Vineet Guptad8005e62013-01-18 15:12:18 +0530273};
274
275static irqreturn_t timer_irq_handler(int irq, void *dev_id)
276{
Vineet Guptaf8b34c32014-01-25 00:42:37 +0530277 /*
278 * Note that generic IRQ core could have passed @evt for @dev_id if
279 * irq_set_chip_and_handler() asked for handle_percpu_devid_irq()
280 */
281 struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device);
Viresh Kumaraeec6cd2015-07-16 16:56:14 +0530282 int irq_reenable = clockevent_state_periodic(evt);
Vineet Guptad8005e62013-01-18 15:12:18 +0530283
Vineet Guptaf8b34c32014-01-25 00:42:37 +0530284 /*
285 * Any write to CTRL reg ACks the interrupt, we rewrite the
286 * Count when [N]ot [H]alted bit.
287 * And re-arm it if perioid by [I]nterrupt [E]nable bit
288 */
289 write_aux_reg(ARC_REG_TIMER0_CTRL, irq_reenable | TIMER_CTRL_NH);
290
291 evt->event_handler(evt);
292
Vineet Guptad8005e62013-01-18 15:12:18 +0530293 return IRQ_HANDLED;
294}
295
Anna-Maria Gleixnerecd80812016-07-13 17:17:07 +0000296
297static int arc_timer_starting_cpu(unsigned int cpu)
Vineet Guptad8005e62013-01-18 15:12:18 +0530298{
Vineet Gupta2d4899f2014-05-08 14:06:38 +0530299 struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device);
Vineet Guptad8005e62013-01-18 15:12:18 +0530300
Noam Camuseec3c582016-01-01 15:48:49 +0530301 evt->cpumask = cpumask_of(smp_processor_id());
302
Anna-Maria Gleixnerecd80812016-07-13 17:17:07 +0000303 clockevents_config_and_register(evt, arc_timer_freq, 0, ARC_TIMER_MAX);
304 enable_percpu_irq(arc_timer_irq, 0);
305 return 0;
Noam Camuseec3c582016-01-01 15:48:49 +0530306}
307
Anna-Maria Gleixnerecd80812016-07-13 17:17:07 +0000308static int arc_timer_dying_cpu(unsigned int cpu)
309{
310 disable_percpu_irq(arc_timer_irq);
311 return 0;
312}
Noam Camuseec3c582016-01-01 15:48:49 +0530313
314/*
315 * clockevent setup for boot CPU
316 */
Daniel Lezcano43d75602016-06-15 14:50:12 +0200317static int __init arc_clockevent_setup(struct device_node *node)
Noam Camuseec3c582016-01-01 15:48:49 +0530318{
319 struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device);
320 int ret;
321
Vineet Gupta77c8d0d2016-01-01 17:58:45 +0530322 arc_timer_irq = irq_of_parse_and_map(node, 0);
Daniel Lezcano43d75602016-06-15 14:50:12 +0200323 if (arc_timer_irq <= 0) {
324 pr_err("clockevent: missing irq");
325 return -EINVAL;
326 }
Vineet Gupta77c8d0d2016-01-01 17:58:45 +0530327
328 ret = arc_get_timer_clk(node);
Daniel Lezcano43d75602016-06-15 14:50:12 +0200329 if (ret) {
330 pr_err("clockevent: missing clk");
331 return ret;
332 }
Vineet Gupta77c8d0d2016-01-01 17:58:45 +0530333
Noam Camuseec3c582016-01-01 15:48:49 +0530334 /* Needs apriori irq_set_percpu_devid() done in intc map function */
335 ret = request_percpu_irq(arc_timer_irq, timer_irq_handler,
336 "Timer0 (per-cpu-tick)", evt);
Daniel Lezcano43d75602016-06-15 14:50:12 +0200337 if (ret) {
338 pr_err("clockevent: unable to request irq\n");
339 return ret;
340 }
Vineet Gupta56957942016-01-28 12:56:03 +0530341
Anna-Maria Gleixnerecd80812016-07-13 17:17:07 +0000342 ret = cpuhp_setup_state(CPUHP_AP_ARC_TIMER_STARTING,
343 "AP_ARC_TIMER_STARTING",
344 arc_timer_starting_cpu,
345 arc_timer_dying_cpu);
346 if (ret) {
347 pr_err("Failed to setup hotplug state");
348 return ret;
349 }
Daniel Lezcano43d75602016-06-15 14:50:12 +0200350 return 0;
Vineet Guptad8005e62013-01-18 15:12:18 +0530351}
Vineet Guptae608b532016-01-01 18:05:48 +0530352
Daniel Lezcano43d75602016-06-15 14:50:12 +0200353static int __init arc_of_timer_init(struct device_node *np)
Vineet Guptae608b532016-01-01 18:05:48 +0530354{
355 static int init_count = 0;
Daniel Lezcano43d75602016-06-15 14:50:12 +0200356 int ret;
Vineet Guptae608b532016-01-01 18:05:48 +0530357
358 if (!init_count) {
359 init_count = 1;
Daniel Lezcano43d75602016-06-15 14:50:12 +0200360 ret = arc_clockevent_setup(np);
Vineet Guptae608b532016-01-01 18:05:48 +0530361 } else {
Daniel Lezcano43d75602016-06-15 14:50:12 +0200362 ret = arc_cs_setup_timer1(np);
Vineet Guptae608b532016-01-01 18:05:48 +0530363 }
Daniel Lezcano43d75602016-06-15 14:50:12 +0200364
365 return ret;
Vineet Guptae608b532016-01-01 18:05:48 +0530366}
Daniel Lezcano177cf6e2016-06-07 00:27:44 +0200367CLOCKSOURCE_OF_DECLARE(arc_clkevt, "snps,arc-timer", arc_of_timer_init);
Vineet Guptad8005e62013-01-18 15:12:18 +0530368
369/*
370 * Called from start_kernel() - boot CPU only
Vineet Guptad8005e62013-01-18 15:12:18 +0530371 */
372void __init time_init(void)
373{
Noam Camus69fbd092016-01-14 12:20:08 +0530374 of_clk_init(NULL);
375 clocksource_probe();
Vineet Guptad8005e62013-01-18 15:12:18 +0530376}