ixp425_timer.c revision 1.14 1 1.14 joerg /* $NetBSD: ixp425_timer.c,v 1.14 2008/01/20 16:28:24 joerg Exp $ */
2 1.1 ichiro
3 1.1 ichiro /*
4 1.1 ichiro * Copyright (c) 2003
5 1.1 ichiro * Ichiro FUKUHARA <ichiro (at) ichiro.org>.
6 1.1 ichiro * All rights reserved.
7 1.1 ichiro *
8 1.1 ichiro * Redistribution and use in source and binary forms, with or without
9 1.1 ichiro * modification, are permitted provided that the following conditions
10 1.1 ichiro * are met:
11 1.1 ichiro * 1. Redistributions of source code must retain the above copyright
12 1.1 ichiro * notice, this list of conditions and the following disclaimer.
13 1.1 ichiro * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 ichiro * notice, this list of conditions and the following disclaimer in the
15 1.1 ichiro * documentation and/or other materials provided with the distribution.
16 1.1 ichiro * 3. All advertising materials mentioning features or use of this software
17 1.1 ichiro * must display the following acknowledgement:
18 1.1 ichiro * This product includes software developed by Ichiro FUKUHARA.
19 1.1 ichiro * 4. The name of the company nor the name of the author may be used to
20 1.1 ichiro * endorse or promote products derived from this software without specific
21 1.1 ichiro * prior written permission.
22 1.1 ichiro *
23 1.1 ichiro * THIS SOFTWARE IS PROVIDED BY ICHIRO FUKUHARA ``AS IS'' AND ANY EXPRESS OR
24 1.1 ichiro * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 1.1 ichiro * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 1.1 ichiro * IN NO EVENT SHALL ICHIRO FUKUHARA OR THE VOICES IN HIS HEAD BE LIABLE FOR
27 1.1 ichiro * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 ichiro * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 ichiro * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 ichiro * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 ichiro * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 ichiro * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 ichiro * SUCH DAMAGE.
34 1.1 ichiro */
35 1.1 ichiro
36 1.1 ichiro #include <sys/cdefs.h>
37 1.14 joerg __KERNEL_RCSID(0, "$NetBSD: ixp425_timer.c,v 1.14 2008/01/20 16:28:24 joerg Exp $");
38 1.1 ichiro
39 1.10 scw #include "opt_ixp425.h"
40 1.1 ichiro #include "opt_perfctrs.h"
41 1.1 ichiro
42 1.1 ichiro #include <sys/types.h>
43 1.1 ichiro #include <sys/param.h>
44 1.1 ichiro #include <sys/systm.h>
45 1.1 ichiro #include <sys/kernel.h>
46 1.14 joerg #include <sys/atomic.h>
47 1.1 ichiro #include <sys/time.h>
48 1.14 joerg #include <sys/timetc.h>
49 1.1 ichiro #include <sys/device.h>
50 1.1 ichiro
51 1.2 thorpej #include <dev/clock_subr.h>
52 1.2 thorpej
53 1.1 ichiro #include <machine/bus.h>
54 1.1 ichiro #include <machine/intr.h>
55 1.1 ichiro
56 1.1 ichiro #include <arm/cpufunc.h>
57 1.1 ichiro
58 1.1 ichiro #include <arm/xscale/ixp425reg.h>
59 1.1 ichiro #include <arm/xscale/ixp425var.h>
60 1.1 ichiro #include <arm/xscale/ixp425_sipvar.h>
61 1.1 ichiro
62 1.1 ichiro static int ixpclk_match(struct device *, struct cfdata *, void *);
63 1.1 ichiro static void ixpclk_attach(struct device *, struct device *, void *);
64 1.14 joerg static u_int ixpclk_get_timecount(struct timecounter *);
65 1.1 ichiro
66 1.1 ichiro static uint32_t counts_per_hz;
67 1.1 ichiro
68 1.1 ichiro static void *clock_ih;
69 1.1 ichiro
70 1.1 ichiro /* callback functions for intr_functions */
71 1.1 ichiro int ixpclk_intr(void *);
72 1.1 ichiro
73 1.1 ichiro struct ixpclk_softc {
74 1.11 simonb struct device sc_dev;
75 1.11 simonb bus_addr_t sc_baseaddr;
76 1.11 simonb bus_space_tag_t sc_iot;
77 1.11 simonb bus_space_handle_t sc_ioh;
78 1.1 ichiro };
79 1.1 ichiro
80 1.10 scw #ifndef IXP425_CLOCK_FREQ
81 1.4 scw #define COUNTS_PER_SEC 66666600 /* 66MHz */
82 1.10 scw #else
83 1.10 scw #define COUNTS_PER_SEC IXP425_CLOCK_FREQ
84 1.10 scw #endif
85 1.4 scw #define COUNTS_PER_USEC ((COUNTS_PER_SEC / 1000000) + 1)
86 1.1 ichiro
87 1.4 scw static struct ixpclk_softc *ixpclk_sc;
88 1.1 ichiro
89 1.14 joerg static struct timecounter ixpclk_timecounter = {
90 1.14 joerg ixpclk_get_timecount, /* get_timecount */
91 1.14 joerg 0, /* no poll_pps */
92 1.14 joerg 0xffffffff, /* counter_mask */
93 1.14 joerg COUNTS_PER_SEC, /* frequency */
94 1.14 joerg "ixpclk", /* name */
95 1.14 joerg 100, /* quality */
96 1.14 joerg NULL, /* prev */
97 1.14 joerg NULL, /* next */
98 1.14 joerg };
99 1.14 joerg
100 1.14 joerg static volatile uint32_t ixpclk_base;
101 1.14 joerg
102 1.1 ichiro CFATTACH_DECL(ixpclk, sizeof(struct ixpclk_softc),
103 1.1 ichiro ixpclk_match, ixpclk_attach, NULL, NULL);
104 1.1 ichiro
105 1.1 ichiro #define GET_TIMER_VALUE(sc) (bus_space_read_4((sc)->sc_iot, \
106 1.1 ichiro (sc)->sc_ioh, \
107 1.1 ichiro IXP425_OST_TIM0))
108 1.1 ichiro
109 1.5 scw #define GET_TS_VALUE(sc) (*(volatile u_int32_t *) \
110 1.5 scw (IXP425_TIMER_VBASE + IXP425_OST_TS))
111 1.5 scw
112 1.1 ichiro static int
113 1.1 ichiro ixpclk_match(struct device *parent, struct cfdata *match, void *aux)
114 1.1 ichiro {
115 1.11 simonb return 2;
116 1.1 ichiro }
117 1.1 ichiro
118 1.1 ichiro static void
119 1.1 ichiro ixpclk_attach(struct device *parent, struct device *self, void *aux)
120 1.1 ichiro {
121 1.1 ichiro struct ixpclk_softc *sc = (struct ixpclk_softc*) self;
122 1.1 ichiro struct ixpsip_attach_args *sa = aux;
123 1.1 ichiro
124 1.1 ichiro printf("\n");
125 1.1 ichiro
126 1.5 scw ixpclk_sc = sc;
127 1.5 scw
128 1.1 ichiro sc->sc_iot = sa->sa_iot;
129 1.1 ichiro sc->sc_baseaddr = sa->sa_addr;
130 1.1 ichiro
131 1.1 ichiro if (bus_space_map(sc->sc_iot, sa->sa_addr, sa->sa_size, 0,
132 1.1 ichiro &sc->sc_ioh))
133 1.1 ichiro panic("%s: Cannot map registers", self->dv_xname);
134 1.1 ichiro
135 1.5 scw aprint_normal("%s: IXP425 Interval Timer\n", sc->sc_dev.dv_xname);
136 1.1 ichiro }
137 1.1 ichiro
138 1.1 ichiro /*
139 1.1 ichiro * cpu_initclocks:
140 1.1 ichiro *
141 1.1 ichiro * Initialize the clock and get them going.
142 1.1 ichiro */
143 1.1 ichiro void
144 1.1 ichiro cpu_initclocks(void)
145 1.1 ichiro {
146 1.1 ichiro struct ixpclk_softc* sc = ixpclk_sc;
147 1.1 ichiro u_int oldirqstate;
148 1.1 ichiro #if defined(PERFCTRS)
149 1.1 ichiro void *pmu_ih;
150 1.1 ichiro #endif
151 1.1 ichiro
152 1.1 ichiro if (hz < 50 || COUNTS_PER_SEC % hz) {
153 1.1 ichiro aprint_error("Cannot get %d Hz clock; using 100 Hz\n", hz);
154 1.1 ichiro hz = 100;
155 1.1 ichiro }
156 1.1 ichiro
157 1.1 ichiro /*
158 1.1 ichiro * We only have one timer available; stathz and profhz are
159 1.1 ichiro * always left as 0 (the upper-layer clock code deals with
160 1.1 ichiro * this situation).
161 1.1 ichiro */
162 1.1 ichiro if (stathz != 0)
163 1.1 ichiro aprint_error("Cannot get %d Hz statclock\n", stathz);
164 1.1 ichiro stathz = 0;
165 1.1 ichiro
166 1.1 ichiro if (profhz != 0)
167 1.1 ichiro aprint_error("Cannot get %d Hz profclock\n", profhz);
168 1.1 ichiro profhz = 0;
169 1.1 ichiro
170 1.1 ichiro /* Report the clock frequency. */
171 1.1 ichiro aprint_normal("clock: hz=%d stathz=%d profhz=%d\n", hz, stathz, profhz);
172 1.1 ichiro
173 1.1 ichiro oldirqstate = disable_interrupts(I32_bit);
174 1.1 ichiro
175 1.1 ichiro /* Hook up the clock interrupt handler. */
176 1.1 ichiro clock_ih = ixp425_intr_establish(IXP425_INT_TMR0, IPL_CLOCK,
177 1.1 ichiro ixpclk_intr, NULL);
178 1.1 ichiro if (clock_ih == NULL)
179 1.1 ichiro panic("cpu_initclocks: unable to register timer interrupt");
180 1.1 ichiro
181 1.1 ichiro #if defined(PERFCTRS)
182 1.1 ichiro pmu_ih = ixp425_intr_establish(IXP425_INT_XPMU, IPL_STATCLOCK,
183 1.1 ichiro xscale_pmc_dispatch, NULL);
184 1.1 ichiro if (pmu_ih == NULL)
185 1.1 ichiro panic("cpu_initclocks: unable to register timer interrupt");
186 1.1 ichiro #endif
187 1.1 ichiro
188 1.1 ichiro /* Set up the new clock parameters. */
189 1.1 ichiro
190 1.1 ichiro /* clear interrupt */
191 1.1 ichiro bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXP425_OST_STATUS,
192 1.1 ichiro OST_WARM_RESET | OST_WDOG_INT | OST_TS_INT |
193 1.1 ichiro OST_TIM1_INT | OST_TIM0_INT);
194 1.1 ichiro
195 1.1 ichiro counts_per_hz = COUNTS_PER_SEC / hz;
196 1.1 ichiro
197 1.1 ichiro /* reload value & Timer enable */
198 1.1 ichiro bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXP425_OST_TIM0_RELOAD,
199 1.1 ichiro (counts_per_hz & TIMERRELOAD_MASK) | OST_TIMER_EN);
200 1.1 ichiro
201 1.1 ichiro restore_interrupts(oldirqstate);
202 1.14 joerg
203 1.14 joerg tc_init(&ixpclk_timecounter);
204 1.1 ichiro }
205 1.1 ichiro
206 1.1 ichiro /*
207 1.1 ichiro * setstatclockrate:
208 1.1 ichiro *
209 1.1 ichiro * Set the rate of the statistics clock.
210 1.1 ichiro *
211 1.1 ichiro * We assume that hz is either stathz or profhz, and that neither
212 1.1 ichiro * will change after being set by cpu_initclocks(). We could
213 1.1 ichiro * recalculate the intervals here, but that would be a pain.
214 1.1 ichiro */
215 1.1 ichiro void
216 1.8 he setstatclockrate(int newhz)
217 1.1 ichiro {
218 1.1 ichiro
219 1.1 ichiro /*
220 1.1 ichiro * XXX Use TMR1?
221 1.1 ichiro */
222 1.1 ichiro }
223 1.1 ichiro
224 1.14 joerg static u_int
225 1.14 joerg ixpclk_get_timecount(struct timecounter *tc)
226 1.1 ichiro {
227 1.14 joerg u_int savedints, base, counter;
228 1.1 ichiro
229 1.14 joerg savedints = disable_interrupts(I32_bit);
230 1.14 joerg base = ixpclk_base;
231 1.14 joerg counter = GET_TIMER_VALUE(ixpclk_sc);
232 1.14 joerg restore_interrupts(savedints);
233 1.1 ichiro
234 1.14 joerg return base - counter;
235 1.1 ichiro }
236 1.1 ichiro
237 1.1 ichiro /*
238 1.1 ichiro * delay:
239 1.1 ichiro *
240 1.1 ichiro * Delay for at least N microseconds.
241 1.1 ichiro */
242 1.1 ichiro void
243 1.1 ichiro delay(u_int n)
244 1.1 ichiro {
245 1.5 scw u_int32_t first, last;
246 1.5 scw int usecs;
247 1.5 scw
248 1.5 scw if (n == 0)
249 1.5 scw return;
250 1.1 ichiro
251 1.1 ichiro /*
252 1.5 scw * Clamp the timeout at a maximum value (about 32 seconds with
253 1.5 scw * a 66MHz clock). *Nobody* should be delay()ing for anywhere
254 1.5 scw * near that length of time and if they are, they should be hung
255 1.5 scw * out to dry.
256 1.1 ichiro */
257 1.5 scw if (n >= (0x80000000U / COUNTS_PER_USEC))
258 1.5 scw usecs = (0x80000000U / COUNTS_PER_USEC) - 1;
259 1.5 scw else
260 1.5 scw usecs = n * COUNTS_PER_USEC;
261 1.5 scw
262 1.5 scw /* Note: Timestamp timer counts *up*, unlike the other timers */
263 1.5 scw first = GET_TS_VALUE();
264 1.5 scw
265 1.5 scw while (usecs > 0) {
266 1.5 scw last = GET_TS_VALUE();
267 1.5 scw usecs -= (int)(last - first);
268 1.5 scw first = last;
269 1.1 ichiro }
270 1.1 ichiro }
271 1.1 ichiro
272 1.1 ichiro /*
273 1.1 ichiro * ixpclk_intr:
274 1.1 ichiro *
275 1.1 ichiro * Handle the hardclock interrupt.
276 1.1 ichiro */
277 1.1 ichiro int
278 1.1 ichiro ixpclk_intr(void *arg)
279 1.1 ichiro {
280 1.1 ichiro struct ixpclk_softc* sc = ixpclk_sc;
281 1.1 ichiro struct clockframe *frame = arg;
282 1.1 ichiro
283 1.1 ichiro bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXP425_OST_STATUS,
284 1.1 ichiro OST_TIM0_INT);
285 1.1 ichiro
286 1.14 joerg atomic_add_32(&ixpclk_base, counts_per_hz);
287 1.14 joerg
288 1.1 ichiro hardclock(frame);
289 1.1 ichiro
290 1.1 ichiro return (1);
291 1.1 ichiro }
292