a9tmr.c revision 1.3.2.2 1 1.3.2.2 yamt /* $NetBSD: a9tmr.c,v 1.3.2.2 2012/10/30 17:19:00 yamt Exp $ */
2 1.3.2.2 yamt
3 1.3.2.2 yamt /*-
4 1.3.2.2 yamt * Copyright (c) 2012 The NetBSD Foundation, Inc.
5 1.3.2.2 yamt * All rights reserved.
6 1.3.2.2 yamt *
7 1.3.2.2 yamt * This code is derived from software contributed to The NetBSD Foundation
8 1.3.2.2 yamt * by Matt Thomas
9 1.3.2.2 yamt *
10 1.3.2.2 yamt * Redistribution and use in source and binary forms, with or without
11 1.3.2.2 yamt * modification, are permitted provided that the following conditions
12 1.3.2.2 yamt * are met:
13 1.3.2.2 yamt * 1. Redistributions of source code must retain the above copyright
14 1.3.2.2 yamt * notice, this list of conditions and the following disclaimer.
15 1.3.2.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
16 1.3.2.2 yamt * notice, this list of conditions and the following disclaimer in the
17 1.3.2.2 yamt * documentation and/or other materials provided with the distribution.
18 1.3.2.2 yamt *
19 1.3.2.2 yamt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.3.2.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.3.2.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.3.2.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.3.2.2 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.3.2.2 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.3.2.2 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.3.2.2 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.3.2.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.3.2.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.3.2.2 yamt * POSSIBILITY OF SUCH DAMAGE.
30 1.3.2.2 yamt */
31 1.3.2.2 yamt
32 1.3.2.2 yamt #include <sys/cdefs.h>
33 1.3.2.2 yamt __KERNEL_RCSID(0, "$NetBSD: a9tmr.c,v 1.3.2.2 2012/10/30 17:19:00 yamt Exp $");
34 1.3.2.2 yamt
35 1.3.2.2 yamt #include <sys/param.h>
36 1.3.2.2 yamt #include <sys/bus.h>
37 1.3.2.2 yamt #include <sys/device.h>
38 1.3.2.2 yamt #include <sys/intr.h>
39 1.3.2.2 yamt #include <sys/kernel.h>
40 1.3.2.2 yamt #include <sys/proc.h>
41 1.3.2.2 yamt #include <sys/systm.h>
42 1.3.2.2 yamt #include <sys/timetc.h>
43 1.3.2.2 yamt
44 1.3.2.2 yamt #include <prop/proplib.h>
45 1.3.2.2 yamt
46 1.3.2.2 yamt #include <arm/cortex/a9tmr_reg.h>
47 1.3.2.2 yamt #include <arm/cortex/a9tmr_var.h>
48 1.3.2.2 yamt
49 1.3.2.2 yamt #include <arm/cortex/mpcore_var.h>
50 1.3.2.2 yamt
51 1.3.2.2 yamt static int a9tmr_match(device_t, cfdata_t, void *);
52 1.3.2.2 yamt static void a9tmr_attach(device_t, device_t, void *);
53 1.3.2.2 yamt
54 1.3.2.2 yamt static int clockhandler(void *);
55 1.3.2.2 yamt
56 1.3.2.2 yamt static u_int a9tmr_get_timecount(struct timecounter *);
57 1.3.2.2 yamt
58 1.3.2.2 yamt static struct a9tmr_softc a9tmr_sc;
59 1.3.2.2 yamt
60 1.3.2.2 yamt static struct timecounter a9tmr_timecounter = {
61 1.3.2.2 yamt .tc_get_timecount = a9tmr_get_timecount,
62 1.3.2.2 yamt .tc_poll_pps = 0,
63 1.3.2.2 yamt .tc_counter_mask = ~0u,
64 1.3.2.2 yamt .tc_frequency = 0, /* set by cpu_initclocks() */
65 1.3.2.2 yamt .tc_name = NULL, /* set by attach */
66 1.3.2.2 yamt .tc_quality = 500,
67 1.3.2.2 yamt .tc_priv = &a9tmr_sc,
68 1.3.2.2 yamt .tc_next = NULL,
69 1.3.2.2 yamt };
70 1.3.2.2 yamt
71 1.3.2.2 yamt CFATTACH_DECL_NEW(a9tmr, 0, a9tmr_match, a9tmr_attach, NULL, NULL);
72 1.3.2.2 yamt
73 1.3.2.2 yamt static inline uint32_t
74 1.3.2.2 yamt a9tmr_global_read(struct a9tmr_softc *sc, bus_size_t o)
75 1.3.2.2 yamt {
76 1.3.2.2 yamt return bus_space_read_4(sc->sc_memt, sc->sc_global_memh, o);
77 1.3.2.2 yamt }
78 1.3.2.2 yamt
79 1.3.2.2 yamt static inline void
80 1.3.2.2 yamt a9tmr_global_write(struct a9tmr_softc *sc, bus_size_t o, uint32_t v)
81 1.3.2.2 yamt {
82 1.3.2.2 yamt bus_space_write_4(sc->sc_memt, sc->sc_global_memh, o, v);
83 1.3.2.2 yamt }
84 1.3.2.2 yamt
85 1.3.2.2 yamt
86 1.3.2.2 yamt /* ARGSUSED */
87 1.3.2.2 yamt static int
88 1.3.2.2 yamt a9tmr_match(device_t parent, cfdata_t cf, void *aux)
89 1.3.2.2 yamt {
90 1.3.2.2 yamt struct mpcore_attach_args * const mpcaa = aux;
91 1.3.2.2 yamt
92 1.3.2.2 yamt if (a9tmr_sc.sc_dev != NULL)
93 1.3.2.2 yamt return 0;
94 1.3.2.2 yamt
95 1.3.2.2 yamt if (!CPU_ID_CORTEX_A9_P(curcpu()->ci_arm_cpuid))
96 1.3.2.2 yamt return 0;
97 1.3.2.2 yamt
98 1.3.2.2 yamt if (strcmp(mpcaa->mpcaa_name, cf->cf_name) != 0)
99 1.3.2.2 yamt return 0;
100 1.3.2.2 yamt
101 1.3.2.2 yamt /*
102 1.3.2.2 yamt * This isn't present on UP A9s (since CBAR isn't present).
103 1.3.2.2 yamt */
104 1.3.2.2 yamt uint32_t mpidr = armreg_mpidr_read();
105 1.3.2.2 yamt if (mpidr == 0 || (mpidr & MPIDR_U))
106 1.3.2.2 yamt return 0;
107 1.3.2.2 yamt
108 1.3.2.2 yamt return 1;
109 1.3.2.2 yamt }
110 1.3.2.2 yamt
111 1.3.2.2 yamt static void
112 1.3.2.2 yamt a9tmr_attach(device_t parent, device_t self, void *aux)
113 1.3.2.2 yamt {
114 1.3.2.2 yamt struct a9tmr_softc *sc = &a9tmr_sc;
115 1.3.2.2 yamt struct mpcore_attach_args * const mpcaa = aux;
116 1.3.2.2 yamt prop_dictionary_t dict = device_properties(self);
117 1.3.2.2 yamt char freqbuf[sizeof("XXX SHz")];
118 1.3.2.2 yamt
119 1.3.2.2 yamt /*
120 1.3.2.2 yamt * This runs at the ARM PERIPHCLOCK which should be 1/2 of the CPU clock.
121 1.3.2.2 yamt * The MD code should have setup our frequency for us.
122 1.3.2.2 yamt */
123 1.3.2.2 yamt prop_number_t pn = prop_dictionary_get(dict, "frequency");
124 1.3.2.2 yamt KASSERT(pn != NULL);
125 1.3.2.2 yamt sc->sc_freq = prop_number_unsigned_integer_value(pn);
126 1.3.2.2 yamt
127 1.3.2.2 yamt humanize_number(freqbuf, sizeof(freqbuf), sc->sc_freq, "Hz", 1000);
128 1.3.2.2 yamt
129 1.3.2.2 yamt aprint_naive("\n");
130 1.3.2.2 yamt aprint_normal(": A9 Global 64-bit Timer (%s)\n", freqbuf);
131 1.3.2.2 yamt
132 1.3.2.2 yamt self->dv_private = sc;
133 1.3.2.2 yamt sc->sc_dev = self;
134 1.3.2.2 yamt sc->sc_memt = mpcaa->mpcaa_memt;
135 1.3.2.2 yamt sc->sc_memh = mpcaa->mpcaa_memh;
136 1.3.2.2 yamt
137 1.3.2.2 yamt evcnt_attach_dynamic(&sc->sc_ev_missing_ticks, EVCNT_TYPE_MISC, NULL,
138 1.3.2.2 yamt device_xname(self), "missing interrupts");
139 1.3.2.2 yamt
140 1.3.2.2 yamt bus_space_subregion(sc->sc_memt, sc->sc_memh,
141 1.3.2.2 yamt TMR_GLOBAL_BASE, TMR_GLOBAL_BASE, &sc->sc_global_memh);
142 1.3.2.2 yamt bus_space_subregion(sc->sc_memt, sc->sc_memh,
143 1.3.2.2 yamt TMR_PRIVATE_BASE, TMR_PRIVATE_SIZE, &sc->sc_private_memh);
144 1.3.2.2 yamt bus_space_subregion(sc->sc_memt, sc->sc_memh,
145 1.3.2.2 yamt TMR_WDOG_BASE, TMR_WDOG_SIZE, &sc->sc_wdog_memh);
146 1.3.2.2 yamt
147 1.3.2.2 yamt sc->sc_global_ih = intr_establish(IRQ_A9TMR_PPI_GTIMER, IPL_CLOCK,
148 1.3.2.2 yamt IST_EDGE, clockhandler, NULL);
149 1.3.2.2 yamt if (sc->sc_global_ih == NULL)
150 1.3.2.2 yamt panic("%s: unable to register timer interrupt", __func__);
151 1.3.2.2 yamt aprint_normal_dev(sc->sc_dev, "interrupting on irq %d\n",
152 1.3.2.2 yamt IRQ_A9TMR_PPI_GTIMER);
153 1.3.2.2 yamt }
154 1.3.2.2 yamt
155 1.3.2.2 yamt static inline uint64_t
156 1.3.2.2 yamt a9tmr_gettime(struct a9tmr_softc *sc)
157 1.3.2.2 yamt {
158 1.3.2.2 yamt uint32_t lo, hi;
159 1.3.2.2 yamt
160 1.3.2.2 yamt do {
161 1.3.2.2 yamt hi = a9tmr_global_read(sc, TMR_GBL_CTR_U);
162 1.3.2.2 yamt lo = a9tmr_global_read(sc, TMR_GBL_CTR_L);
163 1.3.2.2 yamt } while (hi != a9tmr_global_read(sc, TMR_GBL_CTR_U));
164 1.3.2.2 yamt
165 1.3.2.2 yamt return ((uint64_t)hi << 32) | lo;
166 1.3.2.2 yamt }
167 1.3.2.2 yamt
168 1.3.2.2 yamt void
169 1.3.2.2 yamt a9tmr_init_cpu_clock(struct cpu_info *ci)
170 1.3.2.2 yamt {
171 1.3.2.2 yamt struct a9tmr_softc * const sc = &a9tmr_sc;
172 1.3.2.2 yamt uint64_t now = a9tmr_gettime(sc);
173 1.3.2.2 yamt
174 1.3.2.2 yamt KASSERT(ci == curcpu());
175 1.3.2.2 yamt
176 1.3.2.2 yamt ci->ci_lastintr = now;
177 1.3.2.2 yamt
178 1.3.2.2 yamt a9tmr_global_write(sc, TMR_GBL_AUTOINC, sc->sc_autoinc);
179 1.3.2.2 yamt
180 1.3.2.2 yamt /*
181 1.3.2.2 yamt * To update the compare register we have to disable comparisions first.
182 1.3.2.2 yamt */
183 1.3.2.2 yamt uint32_t ctl = a9tmr_global_read(sc, TMR_GBL_CTL);
184 1.3.2.2 yamt if (ctl & TMR_GBL_CTL_CMP_ENABLE) {
185 1.3.2.2 yamt a9tmr_global_write(sc, TMR_GBL_CTL, ctl & ~TMR_GBL_CTL_CMP_ENABLE);
186 1.3.2.2 yamt }
187 1.3.2.2 yamt
188 1.3.2.2 yamt /*
189 1.3.2.2 yamt * Schedule the next interrupt.
190 1.3.2.2 yamt */
191 1.3.2.2 yamt now += sc->sc_autoinc;
192 1.3.2.2 yamt a9tmr_global_write(sc, TMR_GBL_CMP_L, (uint32_t) now);
193 1.3.2.2 yamt a9tmr_global_write(sc, TMR_GBL_CMP_H, (uint32_t) (now >> 32));
194 1.3.2.2 yamt
195 1.3.2.2 yamt /*
196 1.3.2.2 yamt * Re-enable the comparator and now enable interrupts.
197 1.3.2.2 yamt */
198 1.3.2.2 yamt a9tmr_global_write(sc, TMR_GBL_INT, 1); /* clear interrupt pending */
199 1.3.2.2 yamt ctl |= TMR_GBL_CTL_CMP_ENABLE | TMR_GBL_CTL_INT_ENABLE | TMR_GBL_CTL_AUTO_INC;
200 1.3.2.2 yamt a9tmr_global_write(sc, TMR_GBL_CTL, ctl);
201 1.3.2.2 yamt #if 0
202 1.3.2.2 yamt printf("%s: %s: ctl %#x autoinc %u cmp %#x%08x now %#"PRIx64"\n",
203 1.3.2.2 yamt __func__, ci->ci_data.cpu_name,
204 1.3.2.2 yamt a9tmr_global_read(sc, TMR_GBL_CTL),
205 1.3.2.2 yamt a9tmr_global_read(sc, TMR_GBL_AUTOINC),
206 1.3.2.2 yamt a9tmr_global_read(sc, TMR_GBL_CMP_H),
207 1.3.2.2 yamt a9tmr_global_read(sc, TMR_GBL_CMP_L),
208 1.3.2.2 yamt a9tmr_gettime(sc));
209 1.3.2.2 yamt
210 1.3.2.2 yamt int s = splsched();
211 1.3.2.2 yamt uint64_t when = now;
212 1.3.2.2 yamt u_int n = 0;
213 1.3.2.2 yamt while ((now = a9tmr_gettime(sc)) < when) {
214 1.3.2.2 yamt /* spin */
215 1.3.2.2 yamt n++;
216 1.3.2.2 yamt KASSERTMSG(n <= sc->sc_autoinc,
217 1.3.2.2 yamt "spun %u times but only %"PRIu64" has passed",
218 1.3.2.2 yamt n, when - now);
219 1.3.2.2 yamt }
220 1.3.2.2 yamt printf("%s: %s: status %#x cmp %#x%08x now %#"PRIx64"\n",
221 1.3.2.2 yamt __func__, ci->ci_data.cpu_name,
222 1.3.2.2 yamt a9tmr_global_read(sc, TMR_GBL_INT),
223 1.3.2.2 yamt a9tmr_global_read(sc, TMR_GBL_CMP_H),
224 1.3.2.2 yamt a9tmr_global_read(sc, TMR_GBL_CMP_L),
225 1.3.2.2 yamt a9tmr_gettime(sc));
226 1.3.2.2 yamt splx(s);
227 1.3.2.2 yamt #elif 0
228 1.3.2.2 yamt delay(1000000 / hz + 1000);
229 1.3.2.2 yamt #endif
230 1.3.2.2 yamt }
231 1.3.2.2 yamt
232 1.3.2.2 yamt void
233 1.3.2.2 yamt cpu_initclocks(void)
234 1.3.2.2 yamt {
235 1.3.2.2 yamt struct a9tmr_softc * const sc = &a9tmr_sc;
236 1.3.2.2 yamt
237 1.3.2.2 yamt KASSERT(sc->sc_dev != NULL);
238 1.3.2.2 yamt KASSERT(sc->sc_freq != 0);
239 1.3.2.2 yamt
240 1.3.2.2 yamt sc->sc_autoinc = sc->sc_freq / hz;
241 1.3.2.2 yamt
242 1.3.2.2 yamt a9tmr_init_cpu_clock(curcpu());
243 1.3.2.2 yamt
244 1.3.2.2 yamt a9tmr_timecounter.tc_name = device_xname(sc->sc_dev);
245 1.3.2.2 yamt a9tmr_timecounter.tc_frequency = sc->sc_freq;
246 1.3.2.2 yamt
247 1.3.2.2 yamt tc_init(&a9tmr_timecounter);
248 1.3.2.2 yamt }
249 1.3.2.2 yamt
250 1.3.2.2 yamt void
251 1.3.2.2 yamt a9tmr_delay(unsigned int n)
252 1.3.2.2 yamt {
253 1.3.2.2 yamt struct a9tmr_softc * const sc = &a9tmr_sc;
254 1.3.2.2 yamt
255 1.3.2.2 yamt KASSERT(sc != NULL);
256 1.3.2.2 yamt
257 1.3.2.2 yamt uint32_t freq = sc->sc_freq ? sc->sc_freq : curcpu()->ci_data.cpu_cc_freq / 2;
258 1.3.2.2 yamt KASSERT(freq != 0);
259 1.3.2.2 yamt
260 1.3.2.2 yamt /*
261 1.3.2.2 yamt * not quite divide by 1000000 but close enough
262 1.3.2.2 yamt * (higher by 1.3% which means we wait 1.3% longer).
263 1.3.2.2 yamt */
264 1.3.2.2 yamt const uint64_t incr_per_us = (freq >> 20) + (freq >> 24);
265 1.3.2.2 yamt
266 1.3.2.2 yamt const uint64_t delta = n * incr_per_us;
267 1.3.2.2 yamt const uint64_t base = a9tmr_gettime(sc);
268 1.3.2.2 yamt const uint64_t finish = base + delta;
269 1.3.2.2 yamt
270 1.3.2.2 yamt while (a9tmr_gettime(sc) < finish) {
271 1.3.2.2 yamt /* spin */
272 1.3.2.2 yamt }
273 1.3.2.2 yamt }
274 1.3.2.2 yamt
275 1.3.2.2 yamt /*
276 1.3.2.2 yamt * clockhandler:
277 1.3.2.2 yamt *
278 1.3.2.2 yamt * Handle the hardclock interrupt.
279 1.3.2.2 yamt */
280 1.3.2.2 yamt static int
281 1.3.2.2 yamt clockhandler(void *arg)
282 1.3.2.2 yamt {
283 1.3.2.2 yamt struct clockframe * const cf = arg;
284 1.3.2.2 yamt struct a9tmr_softc * const sc = &a9tmr_sc;
285 1.3.2.2 yamt struct cpu_info * const ci = curcpu();
286 1.3.2.2 yamt
287 1.3.2.2 yamt const uint64_t now = a9tmr_gettime(sc);
288 1.3.2.2 yamt uint64_t delta = now - ci->ci_lastintr;
289 1.3.2.2 yamt
290 1.3.2.2 yamt a9tmr_global_write(sc, TMR_GBL_INT, 1); // Ack the interrupt
291 1.3.2.2 yamt
292 1.3.2.2 yamt #if 0
293 1.3.2.2 yamt printf("%s(%p): %s: now %#"PRIx64" delta %"PRIu64"\n",
294 1.3.2.2 yamt __func__, cf, ci->ci_data.cpu_name, now, delta);
295 1.3.2.2 yamt #endif
296 1.3.2.2 yamt KASSERTMSG(delta > sc->sc_autoinc / 100,
297 1.3.2.2 yamt "%s: interrupting too quickly (delta=%"PRIu64")",
298 1.3.2.2 yamt ci->ci_data.cpu_name, delta);
299 1.3.2.2 yamt
300 1.3.2.2 yamt ci->ci_lastintr = now;
301 1.3.2.2 yamt
302 1.3.2.2 yamt hardclock(cf);
303 1.3.2.2 yamt
304 1.3.2.2 yamt #if 0
305 1.3.2.2 yamt /*
306 1.3.2.2 yamt * Try to make up up to a seconds amount of missed clock interrupts
307 1.3.2.2 yamt */
308 1.3.2.2 yamt u_int ticks = hz;
309 1.3.2.2 yamt for (delta -= sc->sc_autoinc;
310 1.3.2.2 yamt ticks > 0 && delta >= sc->sc_autoinc;
311 1.3.2.2 yamt delta -= sc->sc_autoinc, ticks--) {
312 1.3.2.2 yamt hardclock(cf);
313 1.3.2.2 yamt }
314 1.3.2.2 yamt #else
315 1.3.2.2 yamt if (delta > sc->sc_autoinc)
316 1.3.2.2 yamt sc->sc_ev_missing_ticks.ev_count += delta / sc->sc_autoinc;
317 1.3.2.2 yamt #endif
318 1.3.2.2 yamt
319 1.3.2.2 yamt return 1;
320 1.3.2.2 yamt }
321 1.3.2.2 yamt
322 1.3.2.2 yamt void
323 1.3.2.2 yamt setstatclockrate(int newhz)
324 1.3.2.2 yamt {
325 1.3.2.2 yamt }
326 1.3.2.2 yamt
327 1.3.2.2 yamt static u_int
328 1.3.2.2 yamt a9tmr_get_timecount(struct timecounter *tc)
329 1.3.2.2 yamt {
330 1.3.2.2 yamt struct a9tmr_softc * const sc = tc->tc_priv;
331 1.3.2.2 yamt
332 1.3.2.2 yamt return (u_int) (a9tmr_gettime(sc));
333 1.3.2.2 yamt }
334