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