]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/arm/at91/at91_st.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / arm / at91 / at91_st.c
1 /*-
2  * Copyright (c) 2005 Olivier Houchard.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/module.h>
33 #include <sys/time.h>
34 #include <sys/bus.h>
35 #include <sys/resource.h>
36 #include <sys/rman.h>
37 #include <sys/timetc.h>
38 #include <sys/watchdog.h>
39
40 #include <machine/bus.h>
41 #include <machine/cpu.h>
42 #include <machine/cpufunc.h>
43 #include <machine/resource.h>
44 #include <machine/frame.h>
45 #include <machine/intr.h>
46 #include <arm/at91/at91rm92reg.h>
47 #include <arm/at91/at91var.h>
48 #include <arm/at91/at91_streg.h>
49
50 static struct at91st_softc {
51         bus_space_tag_t         sc_st;
52         bus_space_handle_t      sc_sh;
53         device_t                sc_dev;
54         eventhandler_tag        sc_wet; /* watchdog event handler tag */
55 } *timer_softc;
56
57 #define RD4(off) \
58         bus_space_read_4(timer_softc->sc_st, timer_softc->sc_sh, (off))
59 #define WR4(off, val) \
60         bus_space_write_4(timer_softc->sc_st, timer_softc->sc_sh, (off), (val))
61
62 static void at91st_watchdog(void *, u_int, int *);
63
64 static inline int
65 st_crtr(void)
66 {
67         int cur1, cur2;
68         do {
69                 cur1 = RD4(ST_CRTR);
70                 cur2 = RD4(ST_CRTR);
71         } while (cur1 != cur2);
72         return (cur1);
73 }
74
75 static unsigned at91st_get_timecount(struct timecounter *tc);
76
77 static struct timecounter at91st_timecounter = {
78         at91st_get_timecount, /* get_timecount */
79         NULL, /* no poll_pps */
80 #ifdef SKYEYE_WORKAROUNDS
81         0xffffffffu, /* counter_mask */
82 #else
83         0xfffffu, /* counter_mask */
84 #endif
85         32768, /* frequency */
86         "AT91RM9200 timer", /* name */
87         1000 /* quality */
88 };
89
90 static int
91 at91st_probe(device_t dev)
92 {
93
94         device_set_desc(dev, "ST");
95         return (0);
96 }
97
98 static int
99 at91st_attach(device_t dev)
100 {
101         struct at91_softc *sc = device_get_softc(device_get_parent(dev));
102
103         timer_softc = device_get_softc(dev);
104         timer_softc->sc_st = sc->sc_st;
105         timer_softc->sc_dev = dev;
106         if (bus_space_subregion(sc->sc_st, sc->sc_sh, AT91RM92_ST_BASE,
107             AT91RM92_ST_SIZE, &timer_softc->sc_sh) != 0)
108                 panic("couldn't subregion timer registers");
109         /*
110          * Real time counter increments every clock cycle, need to set before
111          * initializing clocks so that DELAY works.
112          */
113         WR4(ST_RTMR, 1);
114         /* Disable all interrupts */
115         WR4(ST_IDR, 0xffffffff);
116         /* disable watchdog timer */
117         WR4(ST_WDMR, 0);
118
119         timer_softc->sc_wet = EVENTHANDLER_REGISTER(watchdog_list,
120           at91st_watchdog, dev, 0);
121         device_printf(dev,
122           "watchdog registered, timeout intervall max. 64 sec\n");
123         return (0);
124 }
125
126 static device_method_t at91st_methods[] = {
127         DEVMETHOD(device_probe, at91st_probe),
128         DEVMETHOD(device_attach, at91st_attach),
129         {0, 0},
130 };
131
132 static driver_t at91st_driver = {
133         "at91_st",
134         at91st_methods,
135         sizeof(struct at91st_softc),
136 };
137 static devclass_t at91st_devclass;
138
139 DRIVER_MODULE(at91_st, atmelarm, at91st_driver, at91st_devclass, 0, 0);
140
141 #ifdef SKYEYE_WORKAROUNDS
142 static unsigned long tot_count = 0;
143 #endif
144
145 static unsigned
146 at91st_get_timecount(struct timecounter *tc)
147 {
148 #ifdef SKYEYE_WORKAROUNDS
149         return (tot_count);
150 #else
151         return (st_crtr());
152 #endif
153 }
154
155 /*
156  * t below is in a weird unit.  The watchdog is set to 2^t
157  * nanoseconds.  Since our watchdog timer can't really do that too
158  * well, we approximate it by assuming that the timeout interval for
159  * the lsb is 2^22 ns, which is 4.194ms.  This is an overestimation of
160  * the actual time (3.906ms), but close enough for watchdogging.
161  * These approximations, though a violation of the spec, improve the
162  * performance of the application which typically specifies things as
163  * WD_TO_32SEC.  In that last case, we'd wait 32s before the wdog
164  * reset.  The spec says we should wait closer to 34s, but given how
165  * it is likely to be used, and the extremely coarse nature time
166  * interval, I think this is the best solution.
167  */
168 static void
169 at91st_watchdog(void *argp, u_int cmd, int *error)
170 {
171         uint32_t wdog;
172         int t;
173
174         t = cmd & WD_INTERVAL;
175         if (t >= 22 && t <= 37) {
176                 wdog = (1 << (t - 22)) | ST_WDMR_RSTEN;
177                 *error = 0;
178         } else {
179                 wdog = 0;
180         }
181         WR4(ST_WDMR, wdog);
182         WR4(ST_CR, ST_CR_WDRST);
183 }
184
185 static int
186 clock_intr(void *arg)
187 {
188         struct trapframe *fp = arg;
189
190         /* The interrupt is shared, so we have to make sure it's for us. */
191         if (RD4(ST_SR) & ST_SR_PITS) {
192 #ifdef SKYEYE_WORKAROUNDS
193                 tot_count += 32768 / hz;
194 #endif
195                 hardclock(TRAPF_USERMODE(fp), TRAPF_PC(fp));
196                 return (FILTER_HANDLED);
197         }
198         return (FILTER_STRAY);
199 }
200
201 void
202 cpu_initclocks(void)
203 {
204         int rel_value;
205         struct resource *irq;
206         int rid = 0;
207         void *ih;
208         device_t dev = timer_softc->sc_dev;
209
210         rel_value = 32768 / hz;
211         if (rel_value < 1)
212                 rel_value = 1;
213         if (32768 % hz) {
214                 printf("Cannot get %d Hz clock; using %dHz\n", hz, 32768 / rel_value);
215                 hz = 32768 / rel_value;
216                 tick = 1000000 / hz;
217         }
218         /* Disable all interrupts. */
219         WR4(ST_IDR, 0xffffffff);
220         /* The system timer shares the system irq (1) */
221         irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 1, 1, 1,
222           RF_ACTIVE | RF_SHAREABLE);
223         if (!irq)
224                 panic("Unable to allocate irq for the system timer");
225         else
226                 bus_setup_intr(dev, irq, INTR_TYPE_CLK,
227                     clock_intr, NULL, NULL, &ih);
228
229         WR4(ST_PIMR, rel_value);
230
231         /* Enable PITS interrupts. */
232         WR4(ST_IER, ST_SR_PITS);
233         tc_init(&at91st_timecounter);
234 }
235
236 void
237 DELAY(int n)
238 {
239         uint32_t start, end, cur;
240
241         start = st_crtr();
242         n = (n * 1000) / 32768;
243         if (n <= 0)
244                 n = 1;
245         end = (start + n) & ST_CRTR_MASK;
246         cur = start;
247         if (start > end) {
248                 while (cur >= start || cur < end)
249                         cur = st_crtr();
250         } else {
251                 while (cur < end)
252                         cur = st_crtr();
253         }
254 }
255
256 void
257 cpu_reset(void)
258 {
259         /*
260          * Reset the CPU by programmig the watchdog timer to reset the
261          * CPU after 128 'slow' clocks, or about ~4ms.  Loop until
262          * the reset happens for safety.
263          */
264         WR4(ST_WDMR, ST_WDMR_RSTEN | 2);
265         WR4(ST_CR, ST_CR_WDRST);
266         while (1)
267                 continue;
268 }
269
270 void
271 cpu_startprofclock(void)
272 {
273 }
274
275 void
276 cpu_stopprofclock(void)
277 {
278 }