1 1.3 jmcneill /* $NetBSD: a9ptmr.c,v 1.3 2022/11/05 17:30:20 jmcneill 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.3 jmcneill __KERNEL_RCSID(0, "$NetBSD: a9ptmr.c,v 1.3 2022/11/05 17:30:20 jmcneill 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.3 jmcneill #include <sys/xcall.h> 41 1.1 skrll 42 1.1 skrll #include <prop/proplib.h> 43 1.1 skrll 44 1.1 skrll #include <arm/cortex/a9tmr_reg.h> 45 1.1 skrll #include <arm/cortex/a9ptmr_var.h> 46 1.1 skrll 47 1.1 skrll #include <arm/cortex/mpcore_var.h> 48 1.1 skrll 49 1.1 skrll static struct a9ptmr_softc *a9ptmr_sc; 50 1.1 skrll 51 1.1 skrll static int a9ptmr_match(device_t, cfdata_t, void *); 52 1.1 skrll static void a9ptmr_attach(device_t, device_t, void *); 53 1.1 skrll 54 1.1 skrll struct a9ptmr_softc { 55 1.1 skrll device_t sc_dev; 56 1.1 skrll bus_space_tag_t sc_memt; 57 1.1 skrll bus_space_handle_t sc_memh; 58 1.1 skrll 59 1.1 skrll uint32_t sc_ctl; 60 1.1 skrll uint32_t sc_freq; 61 1.1 skrll uint32_t sc_load; 62 1.1 skrll 63 1.1 skrll uint32_t sc_prescaler; 64 1.1 skrll }; 65 1.1 skrll 66 1.1 skrll 67 1.1 skrll CFATTACH_DECL_NEW(arma9ptmr, sizeof(struct a9ptmr_softc), 68 1.1 skrll a9ptmr_match, a9ptmr_attach, NULL, NULL); 69 1.1 skrll 70 1.1 skrll static bool attached; 71 1.1 skrll 72 1.1 skrll static inline uint32_t 73 1.1 skrll a9ptmr_read(struct a9ptmr_softc *sc, bus_size_t o) 74 1.1 skrll { 75 1.1 skrll return bus_space_read_4(sc->sc_memt, sc->sc_memh, o); 76 1.1 skrll } 77 1.1 skrll 78 1.1 skrll static inline void 79 1.1 skrll a9ptmr_write(struct a9ptmr_softc *sc, bus_size_t o, uint32_t v) 80 1.1 skrll { 81 1.1 skrll bus_space_write_4(sc->sc_memt, sc->sc_memh, o, v); 82 1.1 skrll } 83 1.1 skrll 84 1.1 skrll /* ARGSUSED */ 85 1.1 skrll static int 86 1.1 skrll a9ptmr_match(device_t parent, cfdata_t cf, void *aux) 87 1.1 skrll { 88 1.1 skrll struct mpcore_attach_args * const mpcaa = aux; 89 1.1 skrll 90 1.1 skrll if (attached) 91 1.1 skrll return 0; 92 1.1 skrll 93 1.1 skrll if (!CPU_ID_CORTEX_A9_P(curcpu()->ci_arm_cpuid) && 94 1.1 skrll !CPU_ID_CORTEX_A5_P(curcpu()->ci_arm_cpuid)) 95 1.1 skrll return 0; 96 1.1 skrll 97 1.1 skrll if (strcmp(mpcaa->mpcaa_name, cf->cf_name) != 0) 98 1.1 skrll return 0; 99 1.1 skrll 100 1.1 skrll #if 0 101 1.1 skrll /* 102 1.1 skrll * This isn't present on UP A9s (since CBAR isn't present). 103 1.1 skrll */ 104 1.1 skrll uint32_t mpidr = armreg_mpidr_read(); 105 1.1 skrll if (mpidr == 0 || (mpidr & MPIDR_U)) 106 1.1 skrll return 0; 107 1.1 skrll #endif 108 1.1 skrll 109 1.1 skrll return 1; 110 1.1 skrll } 111 1.1 skrll 112 1.1 skrll 113 1.1 skrll static void 114 1.1 skrll a9ptmr_attach(device_t parent, device_t self, void *aux) 115 1.1 skrll { 116 1.1 skrll struct a9ptmr_softc * const sc = device_private(self); 117 1.1 skrll struct mpcore_attach_args * const mpcaa = aux; 118 1.1 skrll prop_dictionary_t dict = device_properties(self); 119 1.1 skrll char freqbuf[sizeof("XXX SHz")]; 120 1.1 skrll const char *cpu_type; 121 1.1 skrll 122 1.1 skrll 123 1.1 skrll sc->sc_dev = self; 124 1.1 skrll sc->sc_memt = mpcaa->mpcaa_memt; 125 1.1 skrll 126 1.1 skrll bus_space_subregion(sc->sc_memt, mpcaa->mpcaa_memh, 127 1.1 skrll mpcaa->mpcaa_off1, TMR_PRIVATE_SIZE, &sc->sc_memh); 128 1.1 skrll 129 1.1 skrll /* 130 1.1 skrll * This runs at the ARM PERIPHCLOCK. 131 1.1 skrll * The MD code should have setup our frequency for us. 132 1.1 skrll */ 133 1.1 skrll if (!prop_dictionary_get_uint32(dict, "frequency", &sc->sc_freq)) { 134 1.1 skrll dict = device_properties(parent); 135 1.1 skrll prop_dictionary_get_uint32(dict, "frequency", &sc->sc_freq); 136 1.1 skrll } 137 1.1 skrll 138 1.1 skrll humanize_number(freqbuf, sizeof(freqbuf), sc->sc_freq, "Hz", 1000); 139 1.1 skrll 140 1.1 skrll a9ptmr_sc = sc; 141 1.1 skrll sc->sc_dev = self; 142 1.1 skrll sc->sc_memt = mpcaa->mpcaa_memt; 143 1.1 skrll sc->sc_memh = mpcaa->mpcaa_memh; 144 1.1 skrll 145 1.1 skrll sc->sc_ctl = a9ptmr_read(sc, TMR_CTL); 146 1.1 skrll 147 1.1 skrll sc->sc_prescaler = 1; 148 1.2 skrll #if 0 149 1.1 skrll /* 150 1.1 skrll * Let's hope the timer frequency isn't prime. 151 1.1 skrll */ 152 1.1 skrll for (size_t div = 256; div >= 2; div--) { 153 1.1 skrll if (sc->sc_freq % div == 0) { 154 1.1 skrll sc->sc_prescaler = div; 155 1.1 skrll break; 156 1.1 skrll } 157 1.1 skrll } 158 1.1 skrll sc->sc_freq /= sc->sc_prescaler; 159 1.2 skrll #endif 160 1.1 skrll 161 1.2 skrll aprint_debug(": freq %d prescaler %d", sc->sc_freq, 162 1.1 skrll sc->sc_prescaler); 163 1.1 skrll sc->sc_ctl = TMR_CTL_INT_ENABLE | TMR_CTL_AUTO_RELOAD | TMR_CTL_ENABLE; 164 1.1 skrll sc->sc_ctl |= __SHIFTIN(sc->sc_prescaler - 1, TMR_CTL_PRESCALER); 165 1.1 skrll 166 1.1 skrll sc->sc_load = (sc->sc_freq / hz) - 1; 167 1.1 skrll 168 1.2 skrll aprint_debug(": load %d ", sc->sc_load); 169 1.2 skrll 170 1.1 skrll a9ptmr_init_cpu_clock(curcpu()); 171 1.1 skrll 172 1.1 skrll aprint_naive("\n"); 173 1.1 skrll if (CPU_ID_CORTEX_A5_P(curcpu()->ci_arm_cpuid)) { 174 1.1 skrll cpu_type = "A5"; 175 1.1 skrll } else { 176 1.1 skrll cpu_type = "A9"; 177 1.1 skrll } 178 1.1 skrll aprint_normal(": %s Private Timer (%s)\n", cpu_type, freqbuf); 179 1.1 skrll 180 1.1 skrll attached = true; 181 1.1 skrll } 182 1.1 skrll 183 1.1 skrll 184 1.1 skrll 185 1.1 skrll void 186 1.1 skrll a9ptmr_delay(unsigned int n) 187 1.1 skrll { 188 1.1 skrll struct a9ptmr_softc * const sc = a9ptmr_sc; 189 1.1 skrll 190 1.1 skrll KASSERT(sc != NULL); 191 1.1 skrll 192 1.1 skrll uint32_t freq = sc->sc_freq ? sc->sc_freq : 193 1.1 skrll curcpu()->ci_data.cpu_cc_freq / 2; 194 1.1 skrll KASSERT(freq != 0); 195 1.1 skrll 196 1.1 skrll const uint64_t counts_per_usec = freq / 1000000; 197 1.1 skrll uint32_t delta, usecs, last, curr; 198 1.1 skrll 199 1.1 skrll KASSERT(sc != NULL); 200 1.1 skrll 201 1.1 skrll last = a9ptmr_read(sc, TMR_CTR); 202 1.1 skrll 203 1.1 skrll delta = usecs = 0; 204 1.1 skrll while (n > usecs) { 205 1.1 skrll curr = a9ptmr_read(sc, TMR_CTR); 206 1.1 skrll 207 1.2 skrll /* Check to see if the timer has reloaded. */ 208 1.2 skrll if (curr > last) 209 1.2 skrll delta += (sc->sc_load - curr) + last; 210 1.1 skrll else 211 1.2 skrll delta += last - curr; 212 1.1 skrll 213 1.1 skrll last = curr; 214 1.1 skrll 215 1.1 skrll if (delta >= counts_per_usec) { 216 1.1 skrll usecs += delta / counts_per_usec; 217 1.1 skrll delta %= counts_per_usec; 218 1.1 skrll } 219 1.1 skrll } 220 1.1 skrll } 221 1.1 skrll 222 1.1 skrll 223 1.1 skrll void 224 1.1 skrll a9ptmr_cpu_initclocks(void) 225 1.1 skrll { 226 1.1 skrll struct a9ptmr_softc * const sc __diagused = a9ptmr_sc; 227 1.1 skrll 228 1.1 skrll KASSERT(sc->sc_dev != NULL); 229 1.1 skrll KASSERT(sc->sc_freq != 0); 230 1.1 skrll 231 1.1 skrll } 232 1.1 skrll 233 1.1 skrll void 234 1.1 skrll a9ptmr_init_cpu_clock(struct cpu_info *ci) 235 1.1 skrll { 236 1.1 skrll struct a9ptmr_softc * const sc = a9ptmr_sc; 237 1.1 skrll 238 1.1 skrll /* Disable Private timer and acknowledge any event */ 239 1.1 skrll a9ptmr_write(sc, TMR_CTL, 0); 240 1.1 skrll a9ptmr_write(sc, TMR_INT, TMR_INT_EVENT); 241 1.1 skrll 242 1.1 skrll /* 243 1.1 skrll * Provide the auto load value for the decrementing counter and 244 1.1 skrll * start it. 245 1.1 skrll */ 246 1.1 skrll a9ptmr_write(sc, TMR_LOAD, sc->sc_load); 247 1.1 skrll a9ptmr_write(sc, TMR_CTL, sc->sc_ctl); 248 1.1 skrll 249 1.1 skrll } 250 1.1 skrll 251 1.1 skrll 252 1.1 skrll 253 1.1 skrll /* 254 1.1 skrll * a9ptmr_intr: 255 1.1 skrll * 256 1.1 skrll * Handle the hardclock interrupt. 257 1.1 skrll */ 258 1.1 skrll int 259 1.1 skrll a9ptmr_intr(void *arg) 260 1.1 skrll { 261 1.1 skrll struct clockframe * const cf = arg; 262 1.1 skrll struct a9ptmr_softc * const sc = a9ptmr_sc; 263 1.1 skrll 264 1.1 skrll a9ptmr_write(sc, TMR_INT, TMR_INT_EVENT); 265 1.1 skrll hardclock(cf); 266 1.1 skrll 267 1.1 skrll return 1; 268 1.1 skrll } 269 1.3 jmcneill 270 1.3 jmcneill static void 271 1.3 jmcneill a9ptmr_update_freq_cb(void *arg1, void *arg2) 272 1.3 jmcneill { 273 1.3 jmcneill a9ptmr_init_cpu_clock(curcpu()); 274 1.3 jmcneill } 275 1.3 jmcneill 276 1.3 jmcneill void 277 1.3 jmcneill a9ptmr_update_freq(uint32_t freq) 278 1.3 jmcneill { 279 1.3 jmcneill struct a9ptmr_softc * const sc = a9ptmr_sc; 280 1.3 jmcneill uint64_t xc; 281 1.3 jmcneill 282 1.3 jmcneill KASSERT(sc->sc_dev != NULL); 283 1.3 jmcneill KASSERT(freq != 0); 284 1.3 jmcneill 285 1.3 jmcneill sc->sc_freq = freq; 286 1.3 jmcneill sc->sc_load = (sc->sc_freq / hz) - 1; 287 1.3 jmcneill 288 1.3 jmcneill xc = xc_broadcast(0, a9ptmr_update_freq_cb, NULL, NULL); 289 1.3 jmcneill xc_wait(xc); 290 1.3 jmcneill } 291