ARC: breakout timer include code into separate header ...
[linux-2.6-microblaze.git] / arch / arc / kernel / time.c
1 /*
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
29  * which however is currently broken
30  */
31
32 #include <linux/interrupt.h>
33 #include <linux/clk.h>
34 #include <linux/clk-provider.h>
35 #include <linux/clocksource.h>
36 #include <linux/clockchips.h>
37 #include <linux/cpu.h>
38 #include <linux/of.h>
39 #include <linux/of_irq.h>
40 #include <asm/irq.h>
41
42 #include <soc/arc/timers.h>
43 #include <soc/arc/mcip.h>
44
45
46 static unsigned long arc_timer_freq;
47
48 static int noinline arc_get_timer_clk(struct device_node *node)
49 {
50         struct clk *clk;
51         int ret;
52
53         clk = of_clk_get(node, 0);
54         if (IS_ERR(clk)) {
55                 pr_err("timer missing clk");
56                 return PTR_ERR(clk);
57         }
58
59         ret = clk_prepare_enable(clk);
60         if (ret) {
61                 pr_err("Couldn't enable parent clk\n");
62                 return ret;
63         }
64
65         arc_timer_freq = clk_get_rate(clk);
66
67         return 0;
68 }
69
70 /********** Clock Source Device *********/
71
72 #ifdef CONFIG_ARC_TIMERS_64BIT
73
74 static cycle_t arc_read_gfrc(struct clocksource *cs)
75 {
76         unsigned long flags;
77         u32 l, h;
78
79         local_irq_save(flags);
80
81         __mcip_cmd(CMD_GFRC_READ_LO, 0);
82         l = read_aux_reg(ARC_REG_MCIP_READBACK);
83
84         __mcip_cmd(CMD_GFRC_READ_HI, 0);
85         h = read_aux_reg(ARC_REG_MCIP_READBACK);
86
87         local_irq_restore(flags);
88
89         return (((cycle_t)h) << 32) | l;
90 }
91
92 static struct clocksource arc_counter_gfrc = {
93         .name   = "ARConnect GFRC",
94         .rating = 400,
95         .read   = arc_read_gfrc,
96         .mask   = CLOCKSOURCE_MASK(64),
97         .flags  = CLOCK_SOURCE_IS_CONTINUOUS,
98 };
99
100 static int __init arc_cs_setup_gfrc(struct device_node *node)
101 {
102         struct mcip_bcr mp;
103         int ret;
104
105         READ_BCR(ARC_REG_MCIP_BCR, mp);
106         if (!mp.gfrc) {
107                 pr_warn("Global-64-bit-Ctr clocksource not detected");
108                 return -ENXIO;
109         }
110
111         ret = arc_get_timer_clk(node);
112         if (ret)
113                 return ret;
114
115         return clocksource_register_hz(&arc_counter_gfrc, arc_timer_freq);
116 }
117 CLOCKSOURCE_OF_DECLARE(arc_gfrc, "snps,archs-timer-gfrc", arc_cs_setup_gfrc);
118
119 #define AUX_RTC_CTRL    0x103
120 #define AUX_RTC_LOW     0x104
121 #define AUX_RTC_HIGH    0x105
122
123 static cycle_t arc_read_rtc(struct clocksource *cs)
124 {
125         unsigned long status;
126         u32 l, h;
127
128         /*
129          * hardware has an internal state machine which tracks readout of
130          * low/high and updates the CTRL.status if
131          *  - interrupt/exception taken between the two reads
132          *  - high increments after low has been read
133          */
134         do {
135                 l = read_aux_reg(AUX_RTC_LOW);
136                 h = read_aux_reg(AUX_RTC_HIGH);
137                 status = read_aux_reg(AUX_RTC_CTRL);
138         } while (!(status & _BITUL(31)));
139
140         return (((cycle_t)h) << 32) | l;
141 }
142
143 static struct clocksource arc_counter_rtc = {
144         .name   = "ARCv2 RTC",
145         .rating = 350,
146         .read   = arc_read_rtc,
147         .mask   = CLOCKSOURCE_MASK(64),
148         .flags  = CLOCK_SOURCE_IS_CONTINUOUS,
149 };
150
151 static int __init arc_cs_setup_rtc(struct device_node *node)
152 {
153         struct bcr_timer timer;
154         int ret;
155
156         READ_BCR(ARC_REG_TIMERS_BCR, timer);
157         if (!timer.rtc) {
158                 pr_warn("Local-64-bit-Ctr clocksource not detected");
159                 return -ENXIO;
160         }
161
162         /* Local to CPU hence not usable in SMP */
163         if (IS_ENABLED(CONFIG_SMP)) {
164                 pr_warn("Local-64-bit-Ctr not usable in SMP");
165                 return -EINVAL;
166         }
167
168         ret = arc_get_timer_clk(node);
169         if (ret)
170                 return ret;
171
172         write_aux_reg(AUX_RTC_CTRL, 1);
173
174         return clocksource_register_hz(&arc_counter_rtc, arc_timer_freq);
175 }
176 CLOCKSOURCE_OF_DECLARE(arc_rtc, "snps,archs-timer-rtc", arc_cs_setup_rtc);
177
178 #endif
179
180 /*
181  * 32bit TIMER1 to keep counting monotonically and wraparound
182  */
183
184 static cycle_t arc_read_timer1(struct clocksource *cs)
185 {
186         return (cycle_t) read_aux_reg(ARC_REG_TIMER1_CNT);
187 }
188
189 static struct clocksource arc_counter_timer1 = {
190         .name   = "ARC Timer1",
191         .rating = 300,
192         .read   = arc_read_timer1,
193         .mask   = CLOCKSOURCE_MASK(32),
194         .flags  = CLOCK_SOURCE_IS_CONTINUOUS,
195 };
196
197 static int __init arc_cs_setup_timer1(struct device_node *node)
198 {
199         int ret;
200
201         /* Local to CPU hence not usable in SMP */
202         if (IS_ENABLED(CONFIG_SMP))
203                 return -EINVAL;
204
205         ret = arc_get_timer_clk(node);
206         if (ret)
207                 return ret;
208
209         write_aux_reg(ARC_REG_TIMER1_LIMIT, ARC_TIMERN_MAX);
210         write_aux_reg(ARC_REG_TIMER1_CNT, 0);
211         write_aux_reg(ARC_REG_TIMER1_CTRL, TIMER_CTRL_NH);
212
213         return clocksource_register_hz(&arc_counter_timer1, arc_timer_freq);
214 }
215
216 /********** Clock Event Device *********/
217
218 static int arc_timer_irq;
219
220 /*
221  * Arm the timer to interrupt after @cycles
222  * The distinction for oneshot/periodic is done in arc_event_timer_ack() below
223  */
224 static void arc_timer_event_setup(unsigned int cycles)
225 {
226         write_aux_reg(ARC_REG_TIMER0_LIMIT, cycles);
227         write_aux_reg(ARC_REG_TIMER0_CNT, 0);   /* start from 0 */
228
229         write_aux_reg(ARC_REG_TIMER0_CTRL, TIMER_CTRL_IE | TIMER_CTRL_NH);
230 }
231
232
233 static int arc_clkevent_set_next_event(unsigned long delta,
234                                        struct clock_event_device *dev)
235 {
236         arc_timer_event_setup(delta);
237         return 0;
238 }
239
240 static int arc_clkevent_set_periodic(struct clock_event_device *dev)
241 {
242         /*
243          * At X Hz, 1 sec = 1000ms -> X cycles;
244          *                    10ms -> X / 100 cycles
245          */
246         arc_timer_event_setup(arc_timer_freq / HZ);
247         return 0;
248 }
249
250 static DEFINE_PER_CPU(struct clock_event_device, arc_clockevent_device) = {
251         .name                   = "ARC Timer0",
252         .features               = CLOCK_EVT_FEAT_ONESHOT |
253                                   CLOCK_EVT_FEAT_PERIODIC,
254         .rating                 = 300,
255         .set_next_event         = arc_clkevent_set_next_event,
256         .set_state_periodic     = arc_clkevent_set_periodic,
257 };
258
259 static irqreturn_t timer_irq_handler(int irq, void *dev_id)
260 {
261         /*
262          * Note that generic IRQ core could have passed @evt for @dev_id if
263          * irq_set_chip_and_handler() asked for handle_percpu_devid_irq()
264          */
265         struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device);
266         int irq_reenable = clockevent_state_periodic(evt);
267
268         /*
269          * Any write to CTRL reg ACks the interrupt, we rewrite the
270          * Count when [N]ot [H]alted bit.
271          * And re-arm it if perioid by [I]nterrupt [E]nable bit
272          */
273         write_aux_reg(ARC_REG_TIMER0_CTRL, irq_reenable | TIMER_CTRL_NH);
274
275         evt->event_handler(evt);
276
277         return IRQ_HANDLED;
278 }
279
280
281 static int arc_timer_starting_cpu(unsigned int cpu)
282 {
283         struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device);
284
285         evt->cpumask = cpumask_of(smp_processor_id());
286
287         clockevents_config_and_register(evt, arc_timer_freq, 0, ARC_TIMERN_MAX);
288         enable_percpu_irq(arc_timer_irq, 0);
289         return 0;
290 }
291
292 static int arc_timer_dying_cpu(unsigned int cpu)
293 {
294         disable_percpu_irq(arc_timer_irq);
295         return 0;
296 }
297
298 /*
299  * clockevent setup for boot CPU
300  */
301 static int __init arc_clockevent_setup(struct device_node *node)
302 {
303         struct clock_event_device *evt = this_cpu_ptr(&arc_clockevent_device);
304         int ret;
305
306         arc_timer_irq = irq_of_parse_and_map(node, 0);
307         if (arc_timer_irq <= 0) {
308                 pr_err("clockevent: missing irq");
309                 return -EINVAL;
310         }
311
312         ret = arc_get_timer_clk(node);
313         if (ret) {
314                 pr_err("clockevent: missing clk");
315                 return ret;
316         }
317
318         /* Needs apriori irq_set_percpu_devid() done in intc map function */
319         ret = request_percpu_irq(arc_timer_irq, timer_irq_handler,
320                                  "Timer0 (per-cpu-tick)", evt);
321         if (ret) {
322                 pr_err("clockevent: unable to request irq\n");
323                 return ret;
324         }
325
326         ret = cpuhp_setup_state(CPUHP_AP_ARC_TIMER_STARTING,
327                                 "AP_ARC_TIMER_STARTING",
328                                 arc_timer_starting_cpu,
329                                 arc_timer_dying_cpu);
330         if (ret) {
331                 pr_err("Failed to setup hotplug state");
332                 return ret;
333         }
334         return 0;
335 }
336
337 static int __init arc_of_timer_init(struct device_node *np)
338 {
339         static int init_count = 0;
340         int ret;
341
342         if (!init_count) {
343                 init_count = 1;
344                 ret = arc_clockevent_setup(np);
345         } else {
346                 ret = arc_cs_setup_timer1(np);
347         }
348
349         return ret;
350 }
351 CLOCKSOURCE_OF_DECLARE(arc_clkevt, "snps,arc-timer", arc_of_timer_init);