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