]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/arm/generic_timer.c
Merge bmake-20150418
[FreeBSD/FreeBSD.git] / sys / arm / arm / generic_timer.c
1 /*-
2  * Copyright (c) 2011 The FreeBSD Foundation
3  * Copyright (c) 2013 Ruslan Bukin <br@bsdpad.com>
4  * All rights reserved.
5  *
6  * Based on mpcore_timer.c developed by Ben Gray <ben.r.gray@gmail.com>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the company nor the name of the author may be used to
17  *    endorse or promote products derived from this software without specific
18  *    prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 /**
34  *      Cortex-A15 (and probably A7) Generic Timer
35  */
36
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/bus.h>
43 #include <sys/kernel.h>
44 #include <sys/module.h>
45 #include <sys/malloc.h>
46 #include <sys/rman.h>
47 #include <sys/timeet.h>
48 #include <sys/timetc.h>
49 #include <sys/watchdog.h>
50 #include <machine/bus.h>
51 #include <machine/cpu.h>
52 #include <machine/intr.h>
53
54 #include <dev/fdt/fdt_common.h>
55 #include <dev/ofw/openfirm.h>
56 #include <dev/ofw/ofw_bus.h>
57 #include <dev/ofw/ofw_bus_subr.h>
58
59 #include <machine/bus.h>
60
61 #define GT_CTRL_ENABLE          (1 << 0)
62 #define GT_CTRL_INT_MASK        (1 << 1)
63 #define GT_CTRL_INT_STAT        (1 << 2)
64 #define GT_REG_CTRL             0
65 #define GT_REG_TVAL             1
66
67 #define GT_CNTKCTL_PL0PTEN      (1 << 9) /* PL0 Physical timer reg access */
68 #define GT_CNTKCTL_PL0VTEN      (1 << 8) /* PL0 Virtual timer reg access */
69 #define GT_CNTKCTL_EVNTI        (0xf << 4) /* Virtual counter event bits */
70 #define GT_CNTKCTL_EVNTDIR      (1 << 3) /* Virtual counter event transition */
71 #define GT_CNTKCTL_EVNTEN       (1 << 2) /* Enables virtual counter events */
72 #define GT_CNTKCTL_PL0VCTEN     (1 << 1) /* PL0 CNTVCT and CNTFRQ access */
73 #define GT_CNTKCTL_PL0PCTEN     (1 << 0) /* PL0 CNTPCT and CNTFRQ access */
74
75 struct arm_tmr_softc {
76         struct resource         *res[4];
77         void                    *ihl[4];
78         uint32_t                clkfreq;
79         struct eventtimer       et;
80         bool                    physical;
81 };
82
83 static struct arm_tmr_softc *arm_tmr_sc = NULL;
84
85 static struct resource_spec timer_spec[] = {
86         { SYS_RES_IRQ,          0,      RF_ACTIVE },    /* Secure */
87         { SYS_RES_IRQ,          1,      RF_ACTIVE },    /* Non-secure */
88         { SYS_RES_IRQ,          2,      RF_ACTIVE },    /* Virt */
89         { SYS_RES_IRQ,          3,      RF_ACTIVE | RF_OPTIONAL }, /* Hyp */
90         { -1, 0 }
91 };
92
93 static timecounter_get_t arm_tmr_get_timecount;
94
95 static struct timecounter arm_tmr_timecount = {
96         .tc_name           = "ARM MPCore Timecounter",
97         .tc_get_timecount  = arm_tmr_get_timecount,
98         .tc_poll_pps       = NULL,
99         .tc_counter_mask   = ~0u,
100         .tc_frequency      = 0,
101         .tc_quality        = 1000,
102 };
103
104 #ifdef __arm__
105 #define get_el0(x)      cp15_## x ##_get()
106 #define get_el1(x)      cp15_## x ##_get()
107 #define set_el0(x, val) cp15_## x ##_set(val)
108 #define set_el1(x, val) cp15_## x ##_set(val)
109 #else /* __aarch64__ */
110 #define get_el0(x)      READ_SPECIALREG(x ##_el0)
111 #define get_el1(x)      READ_SPECIALREG(x ##_el1)
112 #define set_el0(x, val) WRITE_SPECIALREG(x ##_el0, val)
113 #define set_el1(x, val) WRITE_SPECIALREG(x ##_el1, val)
114 #endif
115
116 static int
117 get_freq(void)
118 {
119         return (get_el0(cntfrq));
120 }
121
122 static long
123 get_cntxct(bool physical)
124 {
125         uint64_t val;
126
127         isb();
128         if (physical)
129                 val = get_el0(cntpct);
130         else
131                 val = get_el0(cntvct);
132
133         return (val);
134 }
135
136 static int
137 set_ctrl(uint32_t val, bool physical)
138 {
139
140         if (physical)
141                 set_el0(cntp_ctl, val);
142         else
143                 set_el0(cntv_ctl, val);
144         isb();
145
146         return (0);
147 }
148
149 static int
150 set_tval(uint32_t val, bool physical)
151 {
152
153         if (physical)
154                 set_el0(cntp_tval, val);
155         else
156                 set_el0(cntv_tval, val);
157         isb();
158
159         return (0);
160 }
161
162 static int
163 get_ctrl(bool physical)
164 {
165         uint32_t val;
166
167         if (physical)
168                 val = get_el0(cntp_ctl);
169         else
170                 val = get_el0(cntv_ctl);
171
172         return (val);
173 }
174
175 static void
176 disable_user_access(void)
177 {
178         uint32_t cntkctl;
179
180         cntkctl = get_el1(cntkctl);
181         cntkctl &= ~(GT_CNTKCTL_PL0PTEN | GT_CNTKCTL_PL0VTEN |
182             GT_CNTKCTL_EVNTEN | GT_CNTKCTL_PL0VCTEN | GT_CNTKCTL_PL0PCTEN);
183         set_el1(cntkctl, cntkctl);
184         isb();
185 }
186
187 static unsigned
188 arm_tmr_get_timecount(struct timecounter *tc)
189 {
190
191         return (get_cntxct(arm_tmr_sc->physical));
192 }
193
194 static int
195 arm_tmr_start(struct eventtimer *et, sbintime_t first, sbintime_t period)
196 {
197         struct arm_tmr_softc *sc;
198         int counts, ctrl;
199
200         sc = (struct arm_tmr_softc *)et->et_priv;
201
202         if (first != 0) {
203                 counts = ((uint32_t)et->et_frequency * first) >> 32;
204                 ctrl = get_ctrl(sc->physical);
205                 ctrl &= ~GT_CTRL_INT_MASK;
206                 ctrl |= GT_CTRL_ENABLE;
207                 set_tval(counts, sc->physical);
208                 set_ctrl(ctrl, sc->physical);
209                 return (0);
210         }
211
212         return (EINVAL);
213
214 }
215
216 static int
217 arm_tmr_stop(struct eventtimer *et)
218 {
219         struct arm_tmr_softc *sc;
220         int ctrl;
221
222         sc = (struct arm_tmr_softc *)et->et_priv;
223
224         ctrl = get_ctrl(sc->physical);
225         ctrl &= GT_CTRL_ENABLE;
226         set_ctrl(ctrl, sc->physical);
227
228         return (0);
229 }
230
231 static int
232 arm_tmr_intr(void *arg)
233 {
234         struct arm_tmr_softc *sc;
235         int ctrl;
236
237         sc = (struct arm_tmr_softc *)arg;
238         ctrl = get_ctrl(sc->physical);
239         if (ctrl & GT_CTRL_INT_STAT) {
240                 ctrl |= GT_CTRL_INT_MASK;
241                 set_ctrl(ctrl, sc->physical);
242         }
243
244         if (sc->et.et_active)
245                 sc->et.et_event_cb(&sc->et, sc->et.et_arg);
246
247         return (FILTER_HANDLED);
248 }
249
250 static int
251 arm_tmr_probe(device_t dev)
252 {
253
254         if (!ofw_bus_status_okay(dev))
255                 return (ENXIO);
256
257         if (ofw_bus_is_compatible(dev, "arm,armv7-timer")) {
258                 device_set_desc(dev, "ARMv7 Generic Timer");
259                 return (BUS_PROBE_DEFAULT);
260         } else if (ofw_bus_is_compatible(dev, "arm,armv8-timer")) {
261                 device_set_desc(dev, "ARMv8 Generic Timer");
262                 return (BUS_PROBE_DEFAULT);
263         }
264
265         return (ENXIO);
266 }
267
268
269 static int
270 arm_tmr_attach(device_t dev)
271 {
272         struct arm_tmr_softc *sc;
273         phandle_t node;
274         pcell_t clock;
275         int error;
276         int i;
277
278         sc = device_get_softc(dev);
279         if (arm_tmr_sc)
280                 return (ENXIO);
281
282         /* Get the base clock frequency */
283         node = ofw_bus_get_node(dev);
284         error = OF_getprop(node, "clock-frequency", &clock, sizeof(clock));
285         if (error > 0) {
286                 sc->clkfreq = fdt32_to_cpu(clock);
287         }
288
289         if (sc->clkfreq == 0) {
290                 /* Try to get clock frequency from timer */
291                 sc->clkfreq = get_freq();
292         }
293
294         if (sc->clkfreq == 0) {
295                 device_printf(dev, "No clock frequency specified\n");
296                 return (ENXIO);
297         }
298
299         if (bus_alloc_resources(dev, timer_spec, sc->res)) {
300                 device_printf(dev, "could not allocate resources\n");
301                 return (ENXIO);
302         }
303
304 #ifdef __arm__
305         sc->physical = true;
306 #else /* __aarch64__ */
307         sc->physical = false;
308 #endif
309
310         arm_tmr_sc = sc;
311
312         /* Setup secure, non-secure and virtual IRQs handler */
313         for (i = 0; i < 3; i++) {
314                 error = bus_setup_intr(dev, sc->res[i], INTR_TYPE_CLK,
315                     arm_tmr_intr, NULL, sc, &sc->ihl[i]);
316                 if (error) {
317                         device_printf(dev, "Unable to alloc int resource.\n");
318                         return (ENXIO);
319                 }
320         }
321
322         disable_user_access();
323
324         arm_tmr_timecount.tc_frequency = sc->clkfreq;
325         tc_init(&arm_tmr_timecount);
326
327         sc->et.et_name = "ARM MPCore Eventtimer";
328         sc->et.et_flags = ET_FLAGS_ONESHOT | ET_FLAGS_PERCPU;
329         sc->et.et_quality = 1000;
330
331         sc->et.et_frequency = sc->clkfreq;
332         sc->et.et_min_period = (0x00000002LLU << 32) / sc->et.et_frequency;
333         sc->et.et_max_period = (0xfffffffeLLU << 32) / sc->et.et_frequency;
334         sc->et.et_start = arm_tmr_start;
335         sc->et.et_stop = arm_tmr_stop;
336         sc->et.et_priv = sc;
337         et_register(&sc->et);
338
339         return (0);
340 }
341
342 static device_method_t arm_tmr_methods[] = {
343         DEVMETHOD(device_probe,         arm_tmr_probe),
344         DEVMETHOD(device_attach,        arm_tmr_attach),
345         { 0, 0 }
346 };
347
348 static driver_t arm_tmr_driver = {
349         "generic_timer",
350         arm_tmr_methods,
351         sizeof(struct arm_tmr_softc),
352 };
353
354 static devclass_t arm_tmr_devclass;
355
356 EARLY_DRIVER_MODULE(timer, simplebus, arm_tmr_driver, arm_tmr_devclass, 0, 0,
357     BUS_PASS_TIMER + BUS_PASS_ORDER_MIDDLE);
358 EARLY_DRIVER_MODULE(timer, ofwbus, arm_tmr_driver, arm_tmr_devclass, 0, 0,
359     BUS_PASS_TIMER + BUS_PASS_ORDER_MIDDLE);
360
361 void
362 DELAY(int usec)
363 {
364         int32_t counts, counts_per_usec;
365         uint32_t first, last;
366
367         /*
368          * Check the timers are setup, if not just
369          * use a for loop for the meantime
370          */
371         if (arm_tmr_sc == NULL) {
372                 for (; usec > 0; usec--)
373                         for (counts = 200; counts > 0; counts--)
374                                 /*
375                                  * Prevent the compiler from optimizing
376                                  * out the loop
377                                  */
378                                 cpufunc_nullop();
379                 return;
380         }
381
382         /* Get the number of times to count */
383         counts_per_usec = ((arm_tmr_timecount.tc_frequency / 1000000) + 1);
384
385         /*
386          * Clamp the timeout at a maximum value (about 32 seconds with
387          * a 66MHz clock). *Nobody* should be delay()ing for anywhere
388          * near that length of time and if they are, they should be hung
389          * out to dry.
390          */
391         if (usec >= (0x80000000U / counts_per_usec))
392                 counts = (0x80000000U / counts_per_usec) - 1;
393         else
394                 counts = usec * counts_per_usec;
395
396         first = get_cntxct(arm_tmr_sc->physical);
397
398         while (counts > 0) {
399                 last = get_cntxct(arm_tmr_sc->physical);
400                 counts -= (int32_t)(last - first);
401                 first = last;
402         }
403 }