a9ptmr.c revision 1.1 1 1.1 skrll /* $NetBSD: a9ptmr.c,v 1.1 2019/08/10 17:03:59 skrll Exp $ */
2 1.1 skrll
3 1.1 skrll /*-
4 1.1 skrll * Copyright (c) 2019 The NetBSD Foundation, Inc.
5 1.1 skrll * All rights reserved.
6 1.1 skrll *
7 1.1 skrll * This code is derived from software contributed to The NetBSD Foundation
8 1.1 skrll * by Nick Hudson
9 1.1 skrll *
10 1.1 skrll * Redistribution and use in source and binary forms, with or without
11 1.1 skrll * modification, are permitted provided that the following conditions
12 1.1 skrll * are met:
13 1.1 skrll * 1. Redistributions of source code must retain the above copyright
14 1.1 skrll * notice, this list of conditions and the following disclaimer.
15 1.1 skrll * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 skrll * notice, this list of conditions and the following disclaimer in the
17 1.1 skrll * documentation and/or other materials provided with the distribution.
18 1.1 skrll *
19 1.1 skrll * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 skrll * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 skrll * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 skrll * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 skrll * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 skrll * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 skrll * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 skrll * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 skrll * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 skrll * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 skrll * POSSIBILITY OF SUCH DAMAGE.
30 1.1 skrll */
31 1.1 skrll
32 1.1 skrll #include <sys/cdefs.h>
33 1.1 skrll __KERNEL_RCSID(0, "$NetBSD: a9ptmr.c,v 1.1 2019/08/10 17:03:59 skrll Exp $");
34 1.1 skrll
35 1.1 skrll #include <sys/param.h>
36 1.1 skrll #include <sys/bus.h>
37 1.1 skrll #include <sys/cpu.h>
38 1.1 skrll #include <sys/device.h>
39 1.1 skrll #include <sys/kernel.h>
40 1.1 skrll
41 1.1 skrll #include <prop/proplib.h>
42 1.1 skrll
43 1.1 skrll #include <arm/cortex/a9tmr_reg.h>
44 1.1 skrll #include <arm/cortex/a9ptmr_var.h>
45 1.1 skrll
46 1.1 skrll #include <arm/cortex/mpcore_var.h>
47 1.1 skrll
48 1.1 skrll static struct a9ptmr_softc *a9ptmr_sc;
49 1.1 skrll
50 1.1 skrll static int a9ptmr_match(device_t, cfdata_t, void *);
51 1.1 skrll static void a9ptmr_attach(device_t, device_t, void *);
52 1.1 skrll
53 1.1 skrll struct a9ptmr_softc {
54 1.1 skrll device_t sc_dev;
55 1.1 skrll bus_space_tag_t sc_memt;
56 1.1 skrll bus_space_handle_t sc_memh;
57 1.1 skrll
58 1.1 skrll uint32_t sc_ctl;
59 1.1 skrll uint32_t sc_freq;
60 1.1 skrll uint32_t sc_load;
61 1.1 skrll
62 1.1 skrll uint32_t sc_prescaler;
63 1.1 skrll };
64 1.1 skrll
65 1.1 skrll
66 1.1 skrll CFATTACH_DECL_NEW(arma9ptmr, sizeof(struct a9ptmr_softc),
67 1.1 skrll a9ptmr_match, a9ptmr_attach, NULL, NULL);
68 1.1 skrll
69 1.1 skrll static bool attached;
70 1.1 skrll
71 1.1 skrll static inline uint32_t
72 1.1 skrll a9ptmr_read(struct a9ptmr_softc *sc, bus_size_t o)
73 1.1 skrll {
74 1.1 skrll return bus_space_read_4(sc->sc_memt, sc->sc_memh, o);
75 1.1 skrll }
76 1.1 skrll
77 1.1 skrll static inline void
78 1.1 skrll a9ptmr_write(struct a9ptmr_softc *sc, bus_size_t o, uint32_t v)
79 1.1 skrll {
80 1.1 skrll bus_space_write_4(sc->sc_memt, sc->sc_memh, o, v);
81 1.1 skrll }
82 1.1 skrll
83 1.1 skrll /* ARGSUSED */
84 1.1 skrll static int
85 1.1 skrll a9ptmr_match(device_t parent, cfdata_t cf, void *aux)
86 1.1 skrll {
87 1.1 skrll struct mpcore_attach_args * const mpcaa = aux;
88 1.1 skrll
89 1.1 skrll if (attached)
90 1.1 skrll return 0;
91 1.1 skrll
92 1.1 skrll if (!CPU_ID_CORTEX_A9_P(curcpu()->ci_arm_cpuid) &&
93 1.1 skrll !CPU_ID_CORTEX_A5_P(curcpu()->ci_arm_cpuid))
94 1.1 skrll return 0;
95 1.1 skrll
96 1.1 skrll if (strcmp(mpcaa->mpcaa_name, cf->cf_name) != 0)
97 1.1 skrll return 0;
98 1.1 skrll
99 1.1 skrll #if 0
100 1.1 skrll /*
101 1.1 skrll * This isn't present on UP A9s (since CBAR isn't present).
102 1.1 skrll */
103 1.1 skrll uint32_t mpidr = armreg_mpidr_read();
104 1.1 skrll if (mpidr == 0 || (mpidr & MPIDR_U))
105 1.1 skrll return 0;
106 1.1 skrll #endif
107 1.1 skrll
108 1.1 skrll return 1;
109 1.1 skrll }
110 1.1 skrll
111 1.1 skrll
112 1.1 skrll static void
113 1.1 skrll a9ptmr_attach(device_t parent, device_t self, void *aux)
114 1.1 skrll {
115 1.1 skrll struct a9ptmr_softc * const sc = device_private(self);
116 1.1 skrll struct mpcore_attach_args * const mpcaa = aux;
117 1.1 skrll prop_dictionary_t dict = device_properties(self);
118 1.1 skrll char freqbuf[sizeof("XXX SHz")];
119 1.1 skrll const char *cpu_type;
120 1.1 skrll
121 1.1 skrll
122 1.1 skrll sc->sc_dev = self;
123 1.1 skrll sc->sc_memt = mpcaa->mpcaa_memt;
124 1.1 skrll
125 1.1 skrll bus_space_subregion(sc->sc_memt, mpcaa->mpcaa_memh,
126 1.1 skrll mpcaa->mpcaa_off1, TMR_PRIVATE_SIZE, &sc->sc_memh);
127 1.1 skrll
128 1.1 skrll /*
129 1.1 skrll * This runs at the ARM PERIPHCLOCK.
130 1.1 skrll * The MD code should have setup our frequency for us.
131 1.1 skrll */
132 1.1 skrll if (!prop_dictionary_get_uint32(dict, "frequency", &sc->sc_freq)) {
133 1.1 skrll dict = device_properties(parent);
134 1.1 skrll prop_dictionary_get_uint32(dict, "frequency", &sc->sc_freq);
135 1.1 skrll }
136 1.1 skrll
137 1.1 skrll humanize_number(freqbuf, sizeof(freqbuf), sc->sc_freq, "Hz", 1000);
138 1.1 skrll
139 1.1 skrll a9ptmr_sc = sc;
140 1.1 skrll sc->sc_dev = self;
141 1.1 skrll sc->sc_memt = mpcaa->mpcaa_memt;
142 1.1 skrll sc->sc_memh = mpcaa->mpcaa_memh;
143 1.1 skrll
144 1.1 skrll sc->sc_ctl = a9ptmr_read(sc, TMR_CTL);
145 1.1 skrll
146 1.1 skrll sc->sc_prescaler = 1;
147 1.1 skrll /*
148 1.1 skrll * Let's hope the timer frequency isn't prime.
149 1.1 skrll */
150 1.1 skrll for (size_t div = 256; div >= 2; div--) {
151 1.1 skrll if (sc->sc_freq % div == 0) {
152 1.1 skrll sc->sc_prescaler = div;
153 1.1 skrll break;
154 1.1 skrll }
155 1.1 skrll }
156 1.1 skrll sc->sc_freq /= sc->sc_prescaler;
157 1.1 skrll
158 1.1 skrll aprint_debug_dev(sc->sc_dev, ": freq %d prescaler %d", sc->sc_freq,
159 1.1 skrll sc->sc_prescaler);
160 1.1 skrll sc->sc_ctl = TMR_CTL_INT_ENABLE | TMR_CTL_AUTO_RELOAD | TMR_CTL_ENABLE;
161 1.1 skrll sc->sc_ctl |= __SHIFTIN(sc->sc_prescaler - 1, TMR_CTL_PRESCALER);
162 1.1 skrll
163 1.1 skrll sc->sc_load = (sc->sc_freq / hz) - 1;
164 1.1 skrll
165 1.1 skrll a9ptmr_init_cpu_clock(curcpu());
166 1.1 skrll
167 1.1 skrll aprint_naive("\n");
168 1.1 skrll if (CPU_ID_CORTEX_A5_P(curcpu()->ci_arm_cpuid)) {
169 1.1 skrll cpu_type = "A5";
170 1.1 skrll } else {
171 1.1 skrll cpu_type = "A9";
172 1.1 skrll }
173 1.1 skrll aprint_normal(": %s Private Timer (%s)\n", cpu_type, freqbuf);
174 1.1 skrll
175 1.1 skrll attached = true;
176 1.1 skrll }
177 1.1 skrll
178 1.1 skrll
179 1.1 skrll
180 1.1 skrll void
181 1.1 skrll a9ptmr_delay(unsigned int n)
182 1.1 skrll {
183 1.1 skrll struct a9ptmr_softc * const sc = a9ptmr_sc;
184 1.1 skrll
185 1.1 skrll KASSERT(sc != NULL);
186 1.1 skrll
187 1.1 skrll uint32_t freq = sc->sc_freq ? sc->sc_freq :
188 1.1 skrll curcpu()->ci_data.cpu_cc_freq / 2;
189 1.1 skrll KASSERT(freq != 0);
190 1.1 skrll
191 1.1 skrll
192 1.1 skrll const uint64_t counts_per_usec = freq / 1000000;
193 1.1 skrll uint32_t delta, usecs, last, curr;
194 1.1 skrll
195 1.1 skrll KASSERT(sc != NULL);
196 1.1 skrll
197 1.1 skrll last = a9ptmr_read(sc, TMR_CTR);
198 1.1 skrll
199 1.1 skrll delta = usecs = 0;
200 1.1 skrll while (n > usecs) {
201 1.1 skrll curr = a9ptmr_read(sc, TMR_CTR);
202 1.1 skrll
203 1.1 skrll /* Check to see if the timer has wrapped around. */
204 1.1 skrll if (curr < last)
205 1.1 skrll delta += curr + (sc->sc_load - last);
206 1.1 skrll else
207 1.1 skrll delta += curr - last;
208 1.1 skrll
209 1.1 skrll last = curr;
210 1.1 skrll
211 1.1 skrll if (delta >= counts_per_usec) {
212 1.1 skrll usecs += delta / counts_per_usec;
213 1.1 skrll delta %= counts_per_usec;
214 1.1 skrll }
215 1.1 skrll }
216 1.1 skrll }
217 1.1 skrll
218 1.1 skrll
219 1.1 skrll void
220 1.1 skrll a9ptmr_cpu_initclocks(void)
221 1.1 skrll {
222 1.1 skrll struct a9ptmr_softc * const sc __diagused = a9ptmr_sc;
223 1.1 skrll
224 1.1 skrll KASSERT(sc->sc_dev != NULL);
225 1.1 skrll KASSERT(sc->sc_freq != 0);
226 1.1 skrll
227 1.1 skrll }
228 1.1 skrll
229 1.1 skrll void
230 1.1 skrll a9ptmr_init_cpu_clock(struct cpu_info *ci)
231 1.1 skrll {
232 1.1 skrll struct a9ptmr_softc * const sc = a9ptmr_sc;
233 1.1 skrll
234 1.1 skrll /* Disable Private timer and acknowledge any event */
235 1.1 skrll a9ptmr_write(sc, TMR_CTL, 0);
236 1.1 skrll a9ptmr_write(sc, TMR_INT, TMR_INT_EVENT);
237 1.1 skrll
238 1.1 skrll /*
239 1.1 skrll * Provide the auto load value for the decrementing counter and
240 1.1 skrll * start it.
241 1.1 skrll */
242 1.1 skrll a9ptmr_write(sc, TMR_LOAD, sc->sc_load);
243 1.1 skrll a9ptmr_write(sc, TMR_CTL, sc->sc_ctl);
244 1.1 skrll
245 1.1 skrll }
246 1.1 skrll
247 1.1 skrll
248 1.1 skrll
249 1.1 skrll /*
250 1.1 skrll * a9ptmr_intr:
251 1.1 skrll *
252 1.1 skrll * Handle the hardclock interrupt.
253 1.1 skrll */
254 1.1 skrll int
255 1.1 skrll a9ptmr_intr(void *arg)
256 1.1 skrll {
257 1.1 skrll struct clockframe * const cf = arg;
258 1.1 skrll struct a9ptmr_softc * const sc = a9ptmr_sc;
259 1.1 skrll
260 1.1 skrll a9ptmr_write(sc, TMR_INT, TMR_INT_EVENT);
261 1.1 skrll hardclock(cf);
262 1.1 skrll
263 1.1 skrll return 1;
264 1.1 skrll }
265