]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - sys/arm/broadcom/bcm2835/bcm2835_systimer.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / sys / arm / broadcom / bcm2835 / bcm2835_systimer.c
1 /*
2  * Copyright (c) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org>
3  * Copyright (c) 2012 Damjan Marion <dmarion@freebsd.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/bus.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/malloc.h>
37 #include <sys/rman.h>
38 #include <sys/timeet.h>
39 #include <sys/timetc.h>
40 #include <sys/watchdog.h>
41 #include <machine/bus.h>
42 #include <machine/cpu.h>
43 #include <machine/intr.h>
44
45 #include <dev/fdt/fdt_common.h>
46 #include <dev/ofw/openfirm.h>
47 #include <dev/ofw/ofw_bus.h>
48 #include <dev/ofw/ofw_bus_subr.h>
49
50 #include <machine/bus.h>
51 #include <machine/fdt.h>
52
53 #define BCM2835_NUM_TIMERS      4
54
55 #define DEFAULT_TIMER           3
56 #define DEFAULT_FREQUENCY       1000000
57 #define MIN_PERIOD              5LLU
58
59 #define SYSTIMER_CS     0x00
60 #define SYSTIMER_CLO    0x04
61 #define SYSTIMER_CHI    0x08
62 #define SYSTIMER_C0     0x0C
63 #define SYSTIMER_C1     0x10
64 #define SYSTIMER_C2     0x14
65 #define SYSTIMER_C3     0x18
66
67 struct systimer {
68         int                     index;
69         bool                    enabled;
70         struct eventtimer       et;
71 };
72
73 struct bcm_systimer_softc {
74         struct resource*        mem_res;
75         struct resource*        irq_res[BCM2835_NUM_TIMERS];
76         void*                   intr_hl[BCM2835_NUM_TIMERS];
77         uint32_t                sysclk_freq;
78         bus_space_tag_t         bst;
79         bus_space_handle_t      bsh;
80         struct systimer         st[BCM2835_NUM_TIMERS];
81 };
82
83 static struct resource_spec bcm_systimer_irq_spec[] = {
84         { SYS_RES_IRQ,      0,  RF_ACTIVE },
85         { SYS_RES_IRQ,      1,  RF_ACTIVE },
86         { SYS_RES_IRQ,      2,  RF_ACTIVE },
87         { SYS_RES_IRQ,      3,  RF_ACTIVE },
88         { -1,               0,  0 }
89 };
90
91 static struct bcm_systimer_softc *bcm_systimer_sc = NULL;
92
93 /* Read/Write macros for Timer used as timecounter */
94 #define bcm_systimer_tc_read_4(reg)             \
95         bus_space_read_4(bcm_systimer_sc->bst, \
96                 bcm_systimer_sc->bsh, reg)
97
98 #define bcm_systimer_tc_write_4(reg, val)       \
99         bus_space_write_4(bcm_systimer_sc->bst, \
100                 bcm_systimer_sc->bsh, reg, val)
101
102 static unsigned bcm_systimer_tc_get_timecount(struct timecounter *);
103
104 static struct timecounter bcm_systimer_tc = {
105         .tc_name           = "BCM2835 Timecounter",
106         .tc_get_timecount  = bcm_systimer_tc_get_timecount,
107         .tc_poll_pps       = NULL,
108         .tc_counter_mask   = ~0u,
109         .tc_frequency      = 0,
110         .tc_quality        = 1000,
111 };
112
113 static unsigned
114 bcm_systimer_tc_get_timecount(struct timecounter *tc)
115 {
116         return bcm_systimer_tc_read_4(SYSTIMER_CLO);
117 }
118
119 static int
120 bcm_systimer_start(struct eventtimer *et, sbintime_t first, sbintime_t period)
121 {
122         struct systimer *st = et->et_priv;
123         uint32_t clo, clo1;
124         uint32_t count;
125         register_t s;
126
127         if (first != 0) {
128
129                 count = ((uint32_t)et->et_frequency * first) >> 32;
130
131                 s = intr_disable();
132                 clo = bcm_systimer_tc_read_4(SYSTIMER_CLO);
133 restart:
134                 clo += count;
135                 /*
136                  * Clear pending interrupts
137                  */
138                 bcm_systimer_tc_write_4(SYSTIMER_CS, (1 << st->index));
139                 bcm_systimer_tc_write_4(SYSTIMER_C0 + st->index*4, clo);
140                 clo1 = bcm_systimer_tc_read_4(SYSTIMER_CLO);
141                 if ((int32_t)(clo1 - clo) >= 0) {
142                         count *= 2;
143                         clo = clo1;
144                         goto restart;
145                 }
146                 st->enabled = 1;
147                 intr_restore(s);
148
149                 return (0);
150         } 
151
152         return (EINVAL);
153 }
154
155 static int
156 bcm_systimer_stop(struct eventtimer *et)
157 {
158         struct systimer *st = et->et_priv;
159         st->enabled = 0;
160
161         return (0);
162 }
163
164 static int
165 bcm_systimer_intr(void *arg)
166 {
167         struct systimer *st = (struct systimer *)arg;
168         uint32_t cs;
169
170         cs = bcm_systimer_tc_read_4(SYSTIMER_CS);
171         if ((cs & (1 << st->index)) == 0)
172                 return (FILTER_STRAY);
173
174         /* ACK interrupt */
175         bcm_systimer_tc_write_4(SYSTIMER_CS, (1 << st->index));
176         if (st->enabled) {
177                 if (st->et.et_active) {
178                         st->et.et_event_cb(&st->et, st->et.et_arg);
179                 }
180         }
181
182         return (FILTER_HANDLED);
183 }
184
185 static int
186 bcm_systimer_probe(device_t dev)
187 {
188
189         if (!ofw_bus_status_okay(dev))
190                 return (ENXIO);
191
192         if (ofw_bus_is_compatible(dev, "broadcom,bcm2835-system-timer")) {
193                 device_set_desc(dev, "BCM2835 System Timer");
194                 return (BUS_PROBE_DEFAULT);
195         }
196
197         return (ENXIO);
198 }
199
200 static int
201 bcm_systimer_attach(device_t dev)
202 {
203         struct bcm_systimer_softc *sc = device_get_softc(dev);
204         int err;
205         int rid = 0;
206
207         if (bcm_systimer_sc != NULL)
208                 return (EINVAL);
209
210         sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
211         if (sc->mem_res == NULL) {
212                 device_printf(dev, "could not allocate memory resource\n");
213                 return (ENXIO);
214         }
215
216         sc->bst = rman_get_bustag(sc->mem_res);
217         sc->bsh = rman_get_bushandle(sc->mem_res);
218
219         /* Request the IRQ resources */
220         err = bus_alloc_resources(dev, bcm_systimer_irq_spec,
221                 sc->irq_res);
222         if (err) {
223                 device_printf(dev, "Error: could not allocate irq resources\n");
224                 return (ENXIO);
225         }
226
227         /* TODO: get frequency from FDT */
228         sc->sysclk_freq = DEFAULT_FREQUENCY;
229
230         /* Setup and enable the timer */
231         if (bus_setup_intr(dev, sc->irq_res[DEFAULT_TIMER], INTR_TYPE_CLK,
232                         bcm_systimer_intr, NULL, &sc->st[DEFAULT_TIMER],
233                         &sc->intr_hl[DEFAULT_TIMER]) != 0) {
234                 bus_release_resources(dev, bcm_systimer_irq_spec,
235                         sc->irq_res);
236                 device_printf(dev, "Unable to setup the clock irq handler.\n");
237                 return (ENXIO);
238         }
239
240         sc->st[DEFAULT_TIMER].index = DEFAULT_TIMER;
241         sc->st[DEFAULT_TIMER].enabled = 0;
242         sc->st[DEFAULT_TIMER].et.et_name = malloc(64, M_DEVBUF, M_NOWAIT | M_ZERO);
243         sprintf(sc->st[DEFAULT_TIMER].et.et_name, "BCM2835 Event Timer %d", DEFAULT_TIMER);
244         sc->st[DEFAULT_TIMER].et.et_flags = ET_FLAGS_ONESHOT;
245         sc->st[DEFAULT_TIMER].et.et_quality = 1000;
246         sc->st[DEFAULT_TIMER].et.et_frequency = sc->sysclk_freq;
247         sc->st[DEFAULT_TIMER].et.et_min_period =
248             (MIN_PERIOD << 32) / sc->st[DEFAULT_TIMER].et.et_frequency + 1;
249         sc->st[DEFAULT_TIMER].et.et_max_period =
250             (0x7ffffffeLLU << 32) / sc->st[DEFAULT_TIMER].et.et_frequency;
251         sc->st[DEFAULT_TIMER].et.et_start = bcm_systimer_start;
252         sc->st[DEFAULT_TIMER].et.et_stop = bcm_systimer_stop;
253         sc->st[DEFAULT_TIMER].et.et_priv = &sc->st[DEFAULT_TIMER];
254         et_register(&sc->st[DEFAULT_TIMER].et);
255
256         bcm_systimer_sc = sc;
257
258         bcm_systimer_tc.tc_frequency = DEFAULT_FREQUENCY;
259         tc_init(&bcm_systimer_tc);
260
261         return (0);
262 }
263
264 static device_method_t bcm_systimer_methods[] = {
265         DEVMETHOD(device_probe,         bcm_systimer_probe),
266         DEVMETHOD(device_attach,        bcm_systimer_attach),
267         { 0, 0 }
268 };
269
270 static driver_t bcm_systimer_driver = {
271         "systimer",
272         bcm_systimer_methods,
273         sizeof(struct bcm_systimer_softc),
274 };
275
276 static devclass_t bcm_systimer_devclass;
277
278 DRIVER_MODULE(bcm_systimer, simplebus, bcm_systimer_driver, bcm_systimer_devclass, 0, 0);
279
280 void
281 DELAY(int usec)
282 {
283         int32_t counts;
284         uint32_t first, last;
285
286         if (bcm_systimer_sc == NULL) {
287                 for (; usec > 0; usec--)
288                         for (counts = 200; counts > 0; counts--)
289                                 /* Prevent gcc from optimizing  out the loop */
290                                 cpufunc_nullop();
291                 return;
292         }
293
294         /* Get the number of times to count */
295         counts = usec * (bcm_systimer_tc.tc_frequency / 1000000) + 1;
296
297         first = bcm_systimer_tc_read_4(SYSTIMER_CLO);
298
299         while (counts > 0) {
300                 last = bcm_systimer_tc_read_4(SYSTIMER_CLO);
301                 if (last == first)
302                         continue;
303                 if (last>first) {
304                         counts -= (int32_t)(last - first);
305                 } else {
306                         counts -= (int32_t)((0xFFFFFFFF - first) + last);
307                 }
308                 first = last;
309         }
310 }