blob: 7517f959cba762e484cdc1fb00a78cb95aee5fe0 [file] [log] [blame]
Vineet Guptad8005e62013-01-18 15:12:18 +05301/*
Vineet Guptac4c9a042016-10-31 13:46:38 -07002 * Copyright (C) 2016-17 Synopsys, Inc. (www.synopsys.com)
Vineet Guptad8005e62013-01-18 15:12:18 +05303 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
Vineet Guptad8005e62013-01-18 15:12:18 +05308 */
9
Vineet Guptac4c9a042016-10-31 13:46:38 -070010/* ARC700 has two 32bit independent prog Timers: TIMER0 and TIMER1, Each can be
11 * programmed to go from @count to @limit and optionally interrupt.
12 * We've designated TIMER0 for clockevents and TIMER1 for clocksource
Vineet Guptad8005e62013-01-18 15:12:18 +053013 *
Vineet Guptac4c9a042016-10-31 13:46:38 -070014 * ARCv2 based HS38 cores have RTC (in-core) and GFRC (inside ARConnect/MCIP)
15 * which are suitable for UP and SMP based clocksources respectively
Vineet Guptad8005e62013-01-18 15:12:18 +053016 */
17
Vineet Guptad8005e62013-01-18 15:12:18 +053018#include <linux/interrupt.h>
Noam Camus69fbd092016-01-14 12:20:08 +053019#include <linux/clk.h>
20#include <linux/clk-provider.h>
Vineet Guptad8005e62013-01-18 15:12:18 +053021#include <linux/clocksource.h>
22#include <linux/clockchips.h>
Noam Camuseec3c582016-01-01 15:48:49 +053023#include <linux/cpu.h>
Vineet Gupta77c8d0d2016-01-01 17:58:45 +053024#include <linux/of.h>
25#include <linux/of_irq.h>
Vineet Guptad8005e62013-01-18 15:12:18 +053026
Vineet Guptab26c2e32016-10-31 13:06:19 -070027#include <soc/arc/timers.h>
Vineet Gupta2d7f5c42016-10-31 11:27:08 -070028#include <soc/arc/mcip.h>
Vineet Gupta72d72882014-12-24 18:41:55 +053029
Vineet Guptad8005e62013-01-18 15:12:18 +053030
Vineet Gupta77c8d0d2016-01-01 17:58:45 +053031static unsigned long arc_timer_freq;
32
33static int noinline arc_get_timer_clk(struct device_node *node)
34{
35 struct clk *clk;
36 int ret;
37
38 clk = of_clk_get(node, 0);
39 if (IS_ERR(clk)) {
40 pr_err("timer missing clk");
41 return PTR_ERR(clk);
42 }
43
44 ret = clk_prepare_enable(clk);
45 if (ret) {
46 pr_err("Couldn't enable parent clk\n");
47 return ret;
48 }
49
50 arc_timer_freq = clk_get_rate(clk);
51
52 return 0;
53}
54
Vineet Guptad8005e62013-01-18 15:12:18 +053055/********** Clock Source Device *********/
56
Vineet Gupta04421422016-10-31 14:26:41 -070057#ifdef CONFIG_ARC_TIMERS_64BIT
Vineet Gupta72d72882014-12-24 18:41:55 +053058
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +010059static u64 arc_read_gfrc(struct clocksource *cs)
Vineet Gupta72d72882014-12-24 18:41:55 +053060{
61 unsigned long flags;
Vineet Gupta2cd690e2016-11-03 11:38:52 -070062 u32 l, h;
Vineet Gupta72d72882014-12-24 18:41:55 +053063
64 local_irq_save(flags);
65
Vineet Guptad584f0f2016-01-22 14:27:50 +053066 __mcip_cmd(CMD_GFRC_READ_LO, 0);
Vineet Gupta2cd690e2016-11-03 11:38:52 -070067 l = read_aux_reg(ARC_REG_MCIP_READBACK);
Vineet Gupta72d72882014-12-24 18:41:55 +053068
Vineet Guptad584f0f2016-01-22 14:27:50 +053069 __mcip_cmd(CMD_GFRC_READ_HI, 0);
Vineet Gupta2cd690e2016-11-03 11:38:52 -070070 h = read_aux_reg(ARC_REG_MCIP_READBACK);
Vineet Gupta72d72882014-12-24 18:41:55 +053071
72 local_irq_restore(flags);
73
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +010074 return (((u64)h) << 32) | l;
Vineet Gupta72d72882014-12-24 18:41:55 +053075}
76
Vineet Guptae608b532016-01-01 18:05:48 +053077static struct clocksource arc_counter_gfrc = {
Vineet Guptad584f0f2016-01-22 14:27:50 +053078 .name = "ARConnect GFRC",
Vineet Gupta72d72882014-12-24 18:41:55 +053079 .rating = 400,
Vineet Guptae608b532016-01-01 18:05:48 +053080 .read = arc_read_gfrc,
Vineet Gupta72d72882014-12-24 18:41:55 +053081 .mask = CLOCKSOURCE_MASK(64),
82 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
83};
84
Daniel Lezcano43d75602016-06-15 14:50:12 +020085static int __init arc_cs_setup_gfrc(struct device_node *node)
Vineet Guptae608b532016-01-01 18:05:48 +053086{
Vineet Guptaec7cb872016-10-31 13:02:31 -070087 struct mcip_bcr mp;
Vineet Guptae608b532016-01-01 18:05:48 +053088 int ret;
89
Vineet Guptaec7cb872016-10-31 13:02:31 -070090 READ_BCR(ARC_REG_MCIP_BCR, mp);
91 if (!mp.gfrc) {
92 pr_warn("Global-64-bit-Ctr clocksource not detected");
Daniel Lezcano43d75602016-06-15 14:50:12 +020093 return -ENXIO;
Vineet Guptaec7cb872016-10-31 13:02:31 -070094 }
Vineet Guptae608b532016-01-01 18:05:48 +053095
96 ret = arc_get_timer_clk(node);
97 if (ret)
Daniel Lezcano43d75602016-06-15 14:50:12 +020098 return ret;
Vineet Guptae608b532016-01-01 18:05:48 +053099
Daniel Lezcano43d75602016-06-15 14:50:12 +0200100 return clocksource_register_hz(&arc_counter_gfrc, arc_timer_freq);
Vineet Guptae608b532016-01-01 18:05:48 +0530101}
Daniel Lezcano177cf6e2016-06-07 00:27:44 +0200102CLOCKSOURCE_OF_DECLARE(arc_gfrc, "snps,archs-timer-gfrc", arc_cs_setup_gfrc);
Vineet Guptae608b532016-01-01 18:05:48 +0530103
Vineet Guptaaa93e8e2013-11-07 14:57:16 +0530104#define AUX_RTC_CTRL 0x103
105#define AUX_RTC_LOW 0x104
106#define AUX_RTC_HIGH 0x105
107
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100108static u64 arc_read_rtc(struct clocksource *cs)
Vineet Guptaaa93e8e2013-11-07 14:57:16 +0530109{
110 unsigned long status;
Vineet Gupta2cd690e2016-11-03 11:38:52 -0700111 u32 l, h;
Vineet Guptaaa93e8e2013-11-07 14:57:16 +0530112
Vineet Gupta922cc172016-10-31 14:09:52 -0700113 /*
114 * hardware has an internal state machine which tracks readout of
115 * low/high and updates the CTRL.status if
116 * - interrupt/exception taken between the two reads
117 * - high increments after low has been read
118 */
119 do {
Vineet Gupta2cd690e2016-11-03 11:38:52 -0700120 l = read_aux_reg(AUX_RTC_LOW);
121 h = read_aux_reg(AUX_RTC_HIGH);
Vineet Gupta922cc172016-10-31 14:09:52 -0700122 status = read_aux_reg(AUX_RTC_CTRL);
123 } while (!(status & _BITUL(31)));
Vineet Guptaaa93e8e2013-11-07 14:57:16 +0530124
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100125 return (((u64)h) << 32) | l;
Vineet Guptaaa93e8e2013-11-07 14:57:16 +0530126}
127
Vineet Guptae608b532016-01-01 18:05:48 +0530128static struct clocksource arc_counter_rtc = {
Vineet Guptaaa93e8e2013-11-07 14:57:16 +0530129 .name = "ARCv2 RTC",
130 .rating = 350,
Vineet Guptae608b532016-01-01 18:05:48 +0530131 .read = arc_read_rtc,
Vineet Guptaaa93e8e2013-11-07 14:57:16 +0530132 .mask = CLOCKSOURCE_MASK(64),
133 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
134};
135
Daniel Lezcano43d75602016-06-15 14:50:12 +0200136static int __init arc_cs_setup_rtc(struct device_node *node)
Vineet Guptae608b532016-01-01 18:05:48 +0530137{
Vineet Guptaec7cb872016-10-31 13:02:31 -0700138 struct bcr_timer timer;
Vineet Guptae608b532016-01-01 18:05:48 +0530139 int ret;
140
Vineet Guptaec7cb872016-10-31 13:02:31 -0700141 READ_BCR(ARC_REG_TIMERS_BCR, timer);
142 if (!timer.rtc) {
143 pr_warn("Local-64-bit-Ctr clocksource not detected");
Daniel Lezcano43d75602016-06-15 14:50:12 +0200144 return -ENXIO;
Vineet Guptaec7cb872016-10-31 13:02:31 -0700145 }
Vineet Guptae608b532016-01-01 18:05:48 +0530146
147 /* Local to CPU hence not usable in SMP */
Vineet Guptaec7cb872016-10-31 13:02:31 -0700148 if (IS_ENABLED(CONFIG_SMP)) {
149 pr_warn("Local-64-bit-Ctr not usable in SMP");
Daniel Lezcano43d75602016-06-15 14:50:12 +0200150 return -EINVAL;
Vineet Guptaec7cb872016-10-31 13:02:31 -0700151 }
Vineet Guptae608b532016-01-01 18:05:48 +0530152
153 ret = arc_get_timer_clk(node);
154 if (ret)
Daniel Lezcano43d75602016-06-15 14:50:12 +0200155 return ret;
Vineet Guptae608b532016-01-01 18:05:48 +0530156
157 write_aux_reg(AUX_RTC_CTRL, 1);
158
Daniel Lezcano43d75602016-06-15 14:50:12 +0200159 return clocksource_register_hz(&arc_counter_rtc, arc_timer_freq);
Vineet Guptae608b532016-01-01 18:05:48 +0530160}
Daniel Lezcano177cf6e2016-06-07 00:27:44 +0200161CLOCKSOURCE_OF_DECLARE(arc_rtc, "snps,archs-timer-rtc", arc_cs_setup_rtc);
Vineet Guptae608b532016-01-01 18:05:48 +0530162
163#endif
Vineet Guptaaa93e8e2013-11-07 14:57:16 +0530164
Vineet Guptad8005e62013-01-18 15:12:18 +0530165/*
Vineet Guptae608b532016-01-01 18:05:48 +0530166 * 32bit TIMER1 to keep counting monotonically and wraparound
Vineet Guptad8005e62013-01-18 15:12:18 +0530167 */
Vineet Guptad8005e62013-01-18 15:12:18 +0530168
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100169static u64 arc_read_timer1(struct clocksource *cs)
Vineet Guptad8005e62013-01-18 15:12:18 +0530170{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100171 return (u64) read_aux_reg(ARC_REG_TIMER1_CNT);
Vineet Guptad8005e62013-01-18 15:12:18 +0530172}
173
Vineet Guptae608b532016-01-01 18:05:48 +0530174static struct clocksource arc_counter_timer1 = {
Vineet Guptad8005e62013-01-18 15:12:18 +0530175 .name = "ARC Timer1",
176 .rating = 300,
Vineet Guptae608b532016-01-01 18:05:48 +0530177 .read = arc_read_timer1,
Vineet Guptad8005e62013-01-18 15:12:18 +0530178 .mask = CLOCKSOURCE_MASK(32),
179 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
180};
181
Daniel Lezcano43d75602016-06-15 14:50:12 +0200182static int __init arc_cs_setup_timer1(struct device_node *node)
Vineet Guptae608b532016-01-01 18:05:48 +0530183{
184 int ret;
185
186 /* Local to CPU hence not usable in SMP */
187 if (IS_ENABLED(CONFIG_SMP))
Daniel Lezcano43d75602016-06-15 14:50:12 +0200188 return -EINVAL;
Vineet Guptae608b532016-01-01 18:05:48 +0530189
190 ret = arc_get_timer_clk(node);
191 if (ret)
Daniel Lezcano43d75602016-06-15 14:50:12 +0200192 return ret;
Vineet Guptae608b532016-01-01 18:05:48 +0530193
Vineet Guptab26c2e32016-10-31 13:06:19 -0700194 write_aux_reg(ARC_REG_TIMER1_LIMIT, ARC_TIMERN_MAX);
Vineet Guptae608b532016-01-01 18:05:48 +0530195 write_aux_reg(ARC_REG_TIMER1_CNT, 0);
196 write_aux_reg(ARC_REG_TIMER1_CTRL, TIMER_CTRL_NH);
197
Daniel Lezcano43d75602016-06-15 14:50:12 +0200198 return clocksource_register_hz(&arc_counter_timer1, arc_timer_freq);
Vineet Guptae608b532016-01-01 18:05:48 +0530199}
Vineet Guptaaa93e8e2013-11-07 14:57:16 +0530200
Vineet Guptad8005e62013-01-18 15:12:18 +0530201/********** Clock Event Device *********/
202
Vineet Gupta77c8d0d2016-01-01 17:58:45 +0530203static int arc_timer_irq;
Noam Camuseec3c582016-01-01 15:48:49 +0530204
Vineet Guptad8005e62013-01-18 15:12:18 +0530205/*
Vineet Guptac9a98e182014-06-25 17:14:03 +0530206 * Arm the timer to interrupt after @cycles
Vineet Guptad8005e62013-01-18 15:12:18 +0530207 * The distinction for oneshot/periodic is done in arc_event_timer_ack() below
208 */
Vineet Guptac9a98e182014-06-25 17:14:03 +0530209static void arc_timer_event_setup(unsigned int cycles)
Vineet Guptad8005e62013-01-18 15:12:18 +0530210{
Vineet Guptac9a98e182014-06-25 17:14:03 +0530211 write_aux_reg(ARC_REG_TIMER0_LIMIT, cycles);
Vineet Guptad8005e62013-01-18 15:12:18 +0530212 write_aux_reg(ARC_REG_TIMER0_CNT, 0); /* start from 0 */
213
214 write_aux_reg(ARC_REG_TIMER0_CTRL, TIMER_CTRL_IE | TIMER_CTRL_NH);
215}
216
Vineet Guptad8005e62013-01-18 15:12:18 +0530217
218static int arc_clkevent_set_next_event(unsigned long delta,
219 struct clock_event_device *dev)
220{
221 arc_timer_event_setup(delta);
222 return 0;
223}
224
Viresh Kumaraeec6cd2015-07-16 16:56:14 +0530225static int arc_clkevent_set_periodic(struct clock_event_device *dev)
Vineet Guptad8005e62013-01-18 15:12:18 +0530226{
Viresh Kumaraeec6cd2015-07-16 16:56:14 +0530227 /*
228 * At X Hz, 1 sec = 1000ms -> X cycles;
229 * 10ms -> X / 100 cycles
230 */
Vineet Gupta77c8d0d2016-01-01 17:58:45 +0530231 arc_timer_event_setup(arc_timer_freq / HZ);
Viresh Kumaraeec6cd2015-07-16 16:56:14 +0530232 return 0;
Vineet Guptad8005e62013-01-18 15:12:18 +0530233}
234
235static DEFINE_PER_CPU(struct clock_event_device, arc_clockevent_device) = {
Viresh Kumaraeec6cd2015-07-16 16:56:14 +0530236 .name = "ARC Timer0",
237 .features = CLOCK_EVT_FEAT_ONESHOT |
238 CLOCK_EVT_FEAT_PERIODIC,
239 .rating = 300,
Viresh Kumaraeec6cd2015-07-16 16:56:14 +0530240 .set_next_event = arc_clkevent_set_next_event,
241 .set_state_periodic = arc_clkevent_set_periodic,
Vineet Guptad8005e62013-01-18 15:12:18 +0530242};
243
244static irqreturn_t timer_irq_handler(int irq, void *dev_id)
245{
Vineet Guptaf8b34c32014-01-25 00:42:37 +0530246 /*
247 * Note that generic IRQ core could have passed @evt for @dev_id if
248 * irq_set_chip_and_handler() asked for handle_percpu_devid_irq()
249 */
250 struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device);
Viresh Kumaraeec6cd2015-07-16 16:56:14 +0530251 int irq_reenable = clockevent_state_periodic(evt);
Vineet Guptad8005e62013-01-18 15:12:18 +0530252
Vineet Guptaf8b34c32014-01-25 00:42:37 +0530253 /*
254 * Any write to CTRL reg ACks the interrupt, we rewrite the
255 * Count when [N]ot [H]alted bit.
256 * And re-arm it if perioid by [I]nterrupt [E]nable bit
257 */
258 write_aux_reg(ARC_REG_TIMER0_CTRL, irq_reenable | TIMER_CTRL_NH);
259
260 evt->event_handler(evt);
261
Vineet Guptad8005e62013-01-18 15:12:18 +0530262 return IRQ_HANDLED;
263}
264
Anna-Maria Gleixnerecd80812016-07-13 17:17:07 +0000265
266static int arc_timer_starting_cpu(unsigned int cpu)
Vineet Guptad8005e62013-01-18 15:12:18 +0530267{
Vineet Gupta2d4899f2014-05-08 14:06:38 +0530268 struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device);
Vineet Guptad8005e62013-01-18 15:12:18 +0530269
Noam Camuseec3c582016-01-01 15:48:49 +0530270 evt->cpumask = cpumask_of(smp_processor_id());
271
Vineet Guptab26c2e32016-10-31 13:06:19 -0700272 clockevents_config_and_register(evt, arc_timer_freq, 0, ARC_TIMERN_MAX);
Anna-Maria Gleixnerecd80812016-07-13 17:17:07 +0000273 enable_percpu_irq(arc_timer_irq, 0);
274 return 0;
Noam Camuseec3c582016-01-01 15:48:49 +0530275}
276
Anna-Maria Gleixnerecd80812016-07-13 17:17:07 +0000277static int arc_timer_dying_cpu(unsigned int cpu)
278{
279 disable_percpu_irq(arc_timer_irq);
280 return 0;
281}
Noam Camuseec3c582016-01-01 15:48:49 +0530282
283/*
284 * clockevent setup for boot CPU
285 */
Daniel Lezcano43d75602016-06-15 14:50:12 +0200286static int __init arc_clockevent_setup(struct device_node *node)
Noam Camuseec3c582016-01-01 15:48:49 +0530287{
288 struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device);
289 int ret;
290
Vineet Gupta77c8d0d2016-01-01 17:58:45 +0530291 arc_timer_irq = irq_of_parse_and_map(node, 0);
Daniel Lezcano43d75602016-06-15 14:50:12 +0200292 if (arc_timer_irq <= 0) {
293 pr_err("clockevent: missing irq");
294 return -EINVAL;
295 }
Vineet Gupta77c8d0d2016-01-01 17:58:45 +0530296
297 ret = arc_get_timer_clk(node);
Daniel Lezcano43d75602016-06-15 14:50:12 +0200298 if (ret) {
299 pr_err("clockevent: missing clk");
300 return ret;
301 }
Vineet Gupta77c8d0d2016-01-01 17:58:45 +0530302
Noam Camuseec3c582016-01-01 15:48:49 +0530303 /* Needs apriori irq_set_percpu_devid() done in intc map function */
304 ret = request_percpu_irq(arc_timer_irq, timer_irq_handler,
305 "Timer0 (per-cpu-tick)", evt);
Daniel Lezcano43d75602016-06-15 14:50:12 +0200306 if (ret) {
307 pr_err("clockevent: unable to request irq\n");
308 return ret;
309 }
Vineet Gupta56957942016-01-28 12:56:03 +0530310
Anna-Maria Gleixnerecd80812016-07-13 17:17:07 +0000311 ret = cpuhp_setup_state(CPUHP_AP_ARC_TIMER_STARTING,
Thomas Gleixner73c1b412016-12-21 20:19:54 +0100312 "clockevents/arc/timer:starting",
Anna-Maria Gleixnerecd80812016-07-13 17:17:07 +0000313 arc_timer_starting_cpu,
314 arc_timer_dying_cpu);
315 if (ret) {
316 pr_err("Failed to setup hotplug state");
317 return ret;
318 }
Daniel Lezcano43d75602016-06-15 14:50:12 +0200319 return 0;
Vineet Guptad8005e62013-01-18 15:12:18 +0530320}
Vineet Guptae608b532016-01-01 18:05:48 +0530321
Daniel Lezcano43d75602016-06-15 14:50:12 +0200322static int __init arc_of_timer_init(struct device_node *np)
Vineet Guptae608b532016-01-01 18:05:48 +0530323{
324 static int init_count = 0;
Daniel Lezcano43d75602016-06-15 14:50:12 +0200325 int ret;
Vineet Guptae608b532016-01-01 18:05:48 +0530326
327 if (!init_count) {
328 init_count = 1;
Daniel Lezcano43d75602016-06-15 14:50:12 +0200329 ret = arc_clockevent_setup(np);
Vineet Guptae608b532016-01-01 18:05:48 +0530330 } else {
Daniel Lezcano43d75602016-06-15 14:50:12 +0200331 ret = arc_cs_setup_timer1(np);
Vineet Guptae608b532016-01-01 18:05:48 +0530332 }
Daniel Lezcano43d75602016-06-15 14:50:12 +0200333
334 return ret;
Vineet Guptae608b532016-01-01 18:05:48 +0530335}
Daniel Lezcano177cf6e2016-06-07 00:27:44 +0200336CLOCKSOURCE_OF_DECLARE(arc_clkevt, "snps,arc-timer", arc_of_timer_init);