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