]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/at91/at91_pit.c
Increase number of L2 tables required for kernel bootstrap
[FreeBSD/FreeBSD.git] / sys / arm / at91 / at91_pit.c
1 /*-
2  * Copyright (c) 2009 Gallon Sylvestre.  All rights reserved.
3  * Copyright (c) 2010 Greg Ansley.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include "opt_platform.h"
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/resource.h>
37 #include <sys/systm.h>
38 #include <sys/rman.h>
39 #include <sys/time.h>
40 #include <sys/timetc.h>
41 #include <sys/watchdog.h>
42
43 #include <machine/bus.h>
44 #include <machine/cpu.h>
45 #include <machine/frame.h>
46 #include <machine/intr.h>
47 #include <machine/resource.h>
48
49 #include <arm/at91/at91var.h>
50 #include <arm/at91/at91_pitreg.h>
51
52 #ifdef FDT
53 #include <dev/ofw/ofw_bus.h>
54 #include <dev/ofw/ofw_bus_subr.h>
55 #endif
56
57 #ifndef PIT_PRESCALE
58 #define PIT_PRESCALE (16)
59 #endif
60
61 static struct pit_softc {
62         struct resource *mem_res;       /* Memory resource */
63         void            *intrhand;      /* Interrupt handle */
64         device_t        sc_dev;
65 } *sc;
66
67 static uint32_t timecount = 0;
68 static unsigned at91_pit_get_timecount(struct timecounter *tc);
69 static int pit_intr(void *arg);
70
71 static inline uint32_t
72 RD4(struct pit_softc *sc, bus_size_t off)
73 {
74
75         return (bus_read_4(sc->mem_res, off));
76 }
77
78 static inline void
79 WR4(struct pit_softc *sc, bus_size_t off, uint32_t val)
80 {
81
82         bus_write_4(sc->mem_res, off, val);
83 }
84
85 void
86 at91_pit_delay(int us)
87 {
88         int32_t cnt, last, piv;
89         uint64_t pit_freq;
90         const uint64_t mhz  = 1E6;
91
92         if (sc == NULL)
93                 return;
94
95         last = PIT_PIV(RD4(sc, PIT_PIIR));
96
97         /* Max delay ~= 260s. @ 133Mhz */
98         pit_freq = at91_master_clock / PIT_PRESCALE;
99         cnt  = howmany(pit_freq * us, mhz);
100         cnt  = (cnt <= 0) ? 1 : cnt;
101
102         while (cnt > 0) {
103                 piv = PIT_PIV(RD4(sc, PIT_PIIR));
104                         cnt  -= piv - last ;
105                 if (piv < last)
106                         cnt -= PIT_PIV(~0u) - last;
107                 last = piv;
108         }
109 }
110
111 static struct timecounter at91_pit_timecounter = {
112         at91_pit_get_timecount, /* get_timecount */
113         NULL, /* no poll_pps */
114         0xffffffff, /* counter mask */
115         0 / PIT_PRESCALE, /* frequency */
116         "AT91SAM9 timer", /* name */
117         1000 /* quality */
118 };
119
120 static int
121 at91_pit_probe(device_t dev)
122 {
123 #ifdef FDT
124         if (!ofw_bus_is_compatible(dev, "atmel,at91sam9260-pit"))
125                 return (ENXIO);
126 #endif
127         device_set_desc(dev, "AT91SAM9 PIT");
128         return (0);
129 }
130
131 static int
132 at91_pit_attach(device_t dev)
133 {
134         void *ih;
135         int rid, err = 0;
136         struct resource *irq;
137
138         sc = device_get_softc(dev);
139         sc->sc_dev = dev;
140
141         rid = 0;
142         sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
143             RF_ACTIVE);
144
145         if (sc->mem_res == NULL)
146                 panic("couldn't allocate register resources");
147
148         rid = 0;
149         irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 1, 1, 1,
150             RF_ACTIVE | RF_SHAREABLE);
151         if (!irq) {
152                 device_printf(dev, "could not allocate interrupt resources.\n");
153                 err = ENOMEM;
154                 goto out;
155         }
156
157         /* Activate the interrupt. */
158         err = bus_setup_intr(dev, irq, INTR_TYPE_CLK, pit_intr, NULL, NULL,
159             &ih);
160
161         at91_pit_timecounter.tc_frequency =  at91_master_clock / PIT_PRESCALE;
162         tc_init(&at91_pit_timecounter);
163
164         /* Enable the PIT here. */
165         WR4(sc, PIT_MR, PIT_PIV(at91_master_clock / PIT_PRESCALE / hz) |
166             PIT_EN | PIT_IEN);
167 out:
168         return (err);
169 }
170
171
172 static int
173 pit_intr(void *arg)
174 {
175         struct trapframe *fp = arg;
176         uint32_t icnt;
177
178         if (RD4(sc, PIT_SR) & PIT_PITS_DONE) {
179                 icnt = RD4(sc, PIT_PIVR) >> 20;
180
181                 /* Just add in the overflows we just read */
182                 timecount +=  PIT_PIV(RD4(sc, PIT_MR)) * icnt;
183
184                 hardclock(TRAPF_USERMODE(fp), TRAPF_PC(fp));
185                 return (FILTER_HANDLED);
186         }
187         return (FILTER_STRAY);
188 }
189
190 static unsigned
191 at91_pit_get_timecount(struct timecounter *tc)
192 {
193         uint32_t piir, icnt;
194
195         piir = RD4(sc, PIT_PIIR); /* Current  count | over flows */
196         icnt = piir >> 20;      /* Overflows */
197         return (timecount + PIT_PIV(piir) + PIT_PIV(RD4(sc, PIT_MR)) * icnt);
198 }
199
200 static device_method_t at91_pit_methods[] = {
201         DEVMETHOD(device_probe, at91_pit_probe),
202         DEVMETHOD(device_attach, at91_pit_attach),
203         DEVMETHOD_END
204 };
205
206 static driver_t at91_pit_driver = {
207         "at91_pit",
208         at91_pit_methods,
209         sizeof(struct pit_softc),
210 };
211
212 static devclass_t at91_pit_devclass;
213
214 #ifdef FDT
215 EARLY_DRIVER_MODULE(at91_pit, simplebus, at91_pit_driver, at91_pit_devclass,
216     NULL, NULL, BUS_PASS_TIMER);
217 #else
218 EARLY_DRIVER_MODULE(at91_pit, atmelarm, at91_pit_driver, at91_pit_devclass,
219     NULL, NULL, BUS_PASS_TIMER);
220 #endif