1 1.10 skrll /* $NetBSD: a9wdt.c,v 1.10 2019/08/10 17:03:59 skrll Exp $ */ 2 1.1 matt 3 1.1 matt /*- 4 1.1 matt * Copyright (c) 2012 The NetBSD Foundation, Inc. 5 1.1 matt * All rights reserved. 6 1.1 matt * 7 1.1 matt * This code is derived from software contributed to The NetBSD Foundation 8 1.1 matt * by Matt Thomas 9 1.1 matt * 10 1.1 matt * Redistribution and use in source and binary forms, with or without 11 1.1 matt * modification, are permitted provided that the following conditions 12 1.1 matt * are met: 13 1.1 matt * 1. Redistributions of source code must retain the above copyright 14 1.1 matt * notice, this list of conditions and the following disclaimer. 15 1.1 matt * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 matt * notice, this list of conditions and the following disclaimer in the 17 1.1 matt * documentation and/or other materials provided with the distribution. 18 1.1 matt * 19 1.1 matt * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 matt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 matt * POSSIBILITY OF SUCH DAMAGE. 30 1.1 matt */ 31 1.1 matt 32 1.1 matt #include <sys/cdefs.h> 33 1.10 skrll __KERNEL_RCSID(0, "$NetBSD: a9wdt.c,v 1.10 2019/08/10 17:03:59 skrll Exp $"); 34 1.1 matt 35 1.1 matt #include <sys/param.h> 36 1.1 matt #include <sys/bus.h> 37 1.1 matt #include <sys/cpu.h> 38 1.1 matt #include <sys/device.h> 39 1.1 matt #include <sys/wdog.h> 40 1.1 matt 41 1.1 matt #include <prop/proplib.h> 42 1.1 matt 43 1.1 matt #include <dev/sysmon/sysmonvar.h> 44 1.1 matt 45 1.1 matt #include <arm/cortex/a9tmr_reg.h> 46 1.1 matt 47 1.1 matt #include <arm/cortex/mpcore_var.h> 48 1.1 matt 49 1.1 matt static int a9wdt_match(device_t, cfdata_t, void *); 50 1.1 matt static void a9wdt_attach(device_t, device_t, void *); 51 1.1 matt 52 1.1 matt struct a9wdt_softc { 53 1.1 matt struct sysmon_wdog sc_smw; 54 1.1 matt device_t sc_dev; 55 1.1 matt bus_space_tag_t sc_memt; 56 1.1 matt bus_space_handle_t sc_wdog_memh; 57 1.1 matt u_int sc_wdog_max_period; 58 1.1 matt u_int sc_wdog_period; 59 1.1 matt u_int sc_wdog_prescaler; 60 1.1 matt uint32_t sc_freq; 61 1.1 matt uint32_t sc_wdog_load; 62 1.1 matt uint32_t sc_wdog_ctl; 63 1.1 matt bool sc_wdog_armed; 64 1.1 matt }; 65 1.1 matt 66 1.1 matt #ifndef A9WDT_PERIOD_DEFAULT 67 1.1 matt #define A9WDT_PERIOD_DEFAULT 12 68 1.1 matt #endif 69 1.1 matt 70 1.10 skrll CFATTACH_DECL_NEW(arma9wdt, sizeof(struct a9wdt_softc), 71 1.1 matt a9wdt_match, a9wdt_attach, NULL, NULL); 72 1.1 matt 73 1.1 matt static bool attached; 74 1.1 matt 75 1.1 matt static inline uint32_t 76 1.1 matt a9wdt_wdog_read(struct a9wdt_softc *sc, bus_size_t o) 77 1.1 matt { 78 1.1 matt return bus_space_read_4(sc->sc_memt, sc->sc_wdog_memh, o); 79 1.1 matt } 80 1.1 matt 81 1.1 matt static inline void 82 1.1 matt a9wdt_wdog_write(struct a9wdt_softc *sc, bus_size_t o, uint32_t v) 83 1.1 matt { 84 1.1 matt bus_space_write_4(sc->sc_memt, sc->sc_wdog_memh, o, v); 85 1.1 matt } 86 1.1 matt 87 1.1 matt 88 1.1 matt /* ARGSUSED */ 89 1.1 matt static int 90 1.1 matt a9wdt_match(device_t parent, cfdata_t cf, void *aux) 91 1.1 matt { 92 1.1 matt struct mpcore_attach_args * const mpcaa = aux; 93 1.1 matt 94 1.1 matt if (attached) 95 1.1 matt return 0; 96 1.1 matt 97 1.3 jmcneill if (!CPU_ID_CORTEX_A9_P(curcpu()->ci_arm_cpuid) && 98 1.3 jmcneill !CPU_ID_CORTEX_A5_P(curcpu()->ci_arm_cpuid)) 99 1.1 matt return 0; 100 1.1 matt 101 1.1 matt if (strcmp(mpcaa->mpcaa_name, cf->cf_name) != 0) 102 1.1 matt return 0; 103 1.1 matt 104 1.1 matt /* 105 1.1 matt * This isn't present on UP A9s (since CBAR isn't present). 106 1.1 matt */ 107 1.1 matt uint32_t mpidr = armreg_mpidr_read(); 108 1.1 matt if (mpidr == 0 || (mpidr & MPIDR_U)) 109 1.1 matt return 0; 110 1.1 matt 111 1.1 matt return 1; 112 1.1 matt } 113 1.1 matt 114 1.1 matt static int 115 1.1 matt a9wdt_tickle(struct sysmon_wdog *smw) 116 1.1 matt { 117 1.1 matt struct a9wdt_softc * const sc = smw->smw_cookie; 118 1.1 matt 119 1.1 matt /* 120 1.1 matt * Cause the WDOG to restart counting. 121 1.1 matt */ 122 1.1 matt a9wdt_wdog_write(sc, TMR_LOAD, sc->sc_wdog_load); 123 1.1 matt aprint_debug_dev(sc->sc_dev, "tickle\n"); 124 1.1 matt return 0; 125 1.1 matt } 126 1.1 matt 127 1.1 matt static int 128 1.1 matt a9wdt_setmode(struct sysmon_wdog *smw) 129 1.1 matt { 130 1.1 matt struct a9wdt_softc * const sc = smw->smw_cookie; 131 1.1 matt 132 1.1 matt if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) { 133 1.1 matt /* 134 1.1 matt * Emit magic sequence to turn off WDOG 135 1.1 matt */ 136 1.1 matt a9wdt_wdog_write(sc, TMR_WDOGDIS, TMR_WDOG_DISABLE_MAGIC1); 137 1.1 matt a9wdt_wdog_write(sc, TMR_WDOGDIS, TMR_WDOG_DISABLE_MAGIC2); 138 1.1 matt delay(1); 139 1.1 matt sc->sc_wdog_ctl = a9wdt_wdog_read(sc, TMR_CTL); 140 1.1 matt KASSERT((sc->sc_wdog_ctl & TMR_CTL_WDOG_MODE) == 0); 141 1.1 matt aprint_debug_dev(sc->sc_dev, "setmode disable\n"); 142 1.1 matt return 0; 143 1.1 matt } 144 1.1 matt 145 1.1 matt /* 146 1.1 matt * If no changes, just tickle it and return. 147 1.1 matt */ 148 1.1 matt if (sc->sc_wdog_armed && smw->smw_period == sc->sc_wdog_period) { 149 1.1 matt sc->sc_wdog_load = sc->sc_freq * sc->sc_wdog_period - 1; 150 1.1 matt sc->sc_wdog_ctl = TMR_CTL_ENABLE | TMR_CTL_WDOG_MODE 151 1.1 matt | __SHIFTIN(sc->sc_wdog_prescaler - 1, TMR_CTL_PRESCALER); 152 1.1 matt 153 1.1 matt a9wdt_wdog_write(sc, TMR_LOAD, sc->sc_wdog_load); 154 1.1 matt a9wdt_wdog_write(sc, TMR_CTL, sc->sc_wdog_ctl); 155 1.1 matt aprint_debug_dev(sc->sc_dev, "setmode refresh\n"); 156 1.1 matt return 0; 157 1.1 matt } 158 1.1 matt 159 1.1 matt if (smw->smw_period == WDOG_PERIOD_DEFAULT) { 160 1.1 matt sc->sc_wdog_period = A9WDT_PERIOD_DEFAULT; 161 1.1 matt smw->smw_period = A9WDT_PERIOD_DEFAULT; 162 1.1 matt } 163 1.1 matt 164 1.1 matt /* 165 1.1 matt * Make sure we don't overflow the counter. 166 1.1 matt */ 167 1.1 matt if (smw->smw_period >= sc->sc_wdog_max_period) { 168 1.1 matt return EINVAL; 169 1.1 matt } 170 1.1 matt 171 1.1 matt sc->sc_wdog_load = sc->sc_freq * sc->sc_wdog_period - 1; 172 1.1 matt sc->sc_wdog_ctl = TMR_CTL_ENABLE | TMR_CTL_WDOG_MODE 173 1.1 matt | __SHIFTIN(sc->sc_wdog_prescaler - 1, TMR_CTL_PRESCALER); 174 1.1 matt 175 1.1 matt a9wdt_wdog_write(sc, TMR_LOAD, sc->sc_wdog_load); 176 1.2 matt a9wdt_wdog_write(sc, TMR_CTL, sc->sc_wdog_ctl); 177 1.1 matt 178 1.1 matt aprint_debug_dev(sc->sc_dev, "setmode enable\n"); 179 1.1 matt return 0; 180 1.1 matt } 181 1.1 matt 182 1.1 matt 183 1.1 matt static void 184 1.1 matt a9wdt_attach(device_t parent, device_t self, void *aux) 185 1.1 matt { 186 1.9 skrll struct a9wdt_softc * const sc = device_private(self); 187 1.1 matt struct mpcore_attach_args * const mpcaa = aux; 188 1.1 matt prop_dictionary_t dict = device_properties(self); 189 1.4 jmcneill const char *cpu_type; 190 1.1 matt 191 1.1 matt sc->sc_dev = self; 192 1.1 matt sc->sc_memt = mpcaa->mpcaa_memt; 193 1.1 matt 194 1.7 skrll bus_space_subregion(sc->sc_memt, mpcaa->mpcaa_memh, 195 1.10 skrll mpcaa->mpcaa_off1, TMR_WDOG_SIZE, &sc->sc_wdog_memh); 196 1.1 matt 197 1.1 matt /* 198 1.1 matt * This runs at the ARM PERIPHCLOCK which should be 1/2 of the 199 1.1 matt * CPU clock. The MD code should have setup our frequency for us. 200 1.1 matt */ 201 1.1 matt prop_dictionary_get_uint32(dict, "frequency", &sc->sc_freq); 202 1.1 matt 203 1.1 matt sc->sc_wdog_ctl = a9wdt_wdog_read(sc, TMR_CTL); 204 1.1 matt sc->sc_wdog_armed = (sc->sc_wdog_ctl & TMR_CTL_WDOG_MODE) != 0; 205 1.1 matt if (sc->sc_wdog_armed) { 206 1.7 skrll sc->sc_wdog_prescaler = 207 1.1 matt __SHIFTOUT(sc->sc_wdog_ctl, TMR_CTL_PRESCALER) + 1; 208 1.1 matt sc->sc_freq /= sc->sc_wdog_prescaler; 209 1.1 matt sc->sc_wdog_load = a9wdt_wdog_read(sc, TMR_LOAD); 210 1.1 matt sc->sc_wdog_period = (sc->sc_wdog_load + 1) / sc->sc_freq; 211 1.1 matt } else { 212 1.1 matt sc->sc_wdog_period = A9WDT_PERIOD_DEFAULT; 213 1.1 matt sc->sc_wdog_prescaler = 1; 214 1.1 matt /* 215 1.1 matt * Let's hope the timer frequency isn't prime. 216 1.1 matt */ 217 1.8 skrll for (size_t div = 256; div >= 2; div--) { 218 1.1 matt if (sc->sc_freq % div == 0) { 219 1.1 matt sc->sc_wdog_prescaler = div; 220 1.1 matt break; 221 1.1 matt } 222 1.1 matt } 223 1.1 matt sc->sc_freq /= sc->sc_wdog_prescaler; 224 1.1 matt } 225 1.1 matt sc->sc_wdog_max_period = UINT32_MAX / sc->sc_freq; 226 1.1 matt 227 1.1 matt /* 228 1.1 matt * Does the config file tell us to turn on the watchdog? 229 1.1 matt */ 230 1.1 matt if (device_cfdata(self)->cf_flags & 1) 231 1.1 matt sc->sc_wdog_armed = true; 232 1.1 matt 233 1.1 matt aprint_naive("\n"); 234 1.4 jmcneill if (CPU_ID_CORTEX_A5_P(curcpu()->ci_arm_cpuid)) { 235 1.4 jmcneill cpu_type = "A5"; 236 1.4 jmcneill } else { 237 1.4 jmcneill cpu_type = "A9"; 238 1.4 jmcneill } 239 1.4 jmcneill aprint_normal(": %s Watchdog Timer, default period is %u seconds%s\n", 240 1.4 jmcneill cpu_type, sc->sc_wdog_period, 241 1.1 matt sc->sc_wdog_armed ? " (armed)" : ""); 242 1.1 matt 243 1.1 matt sc->sc_smw.smw_name = device_xname(self); 244 1.1 matt sc->sc_smw.smw_cookie = sc; 245 1.1 matt sc->sc_smw.smw_setmode = a9wdt_setmode; 246 1.1 matt sc->sc_smw.smw_tickle = a9wdt_tickle; 247 1.1 matt sc->sc_smw.smw_period = sc->sc_wdog_period; 248 1.1 matt 249 1.1 matt if (sc->sc_wdog_armed) { 250 1.1 matt int error = sysmon_wdog_setmode(&sc->sc_smw, WDOG_MODE_KTICKLE, 251 1.1 matt sc->sc_wdog_period); 252 1.1 matt if (error) 253 1.1 matt aprint_error_dev(self, 254 1.1 matt "failed to start kernel tickler: %d\n", error); 255 1.1 matt } 256 1.5 kiyohara 257 1.5 kiyohara if (sysmon_wdog_register(&sc->sc_smw) != 0) 258 1.5 kiyohara aprint_error("%s: unable to register with sysmon\n", 259 1.5 kiyohara device_xname(sc->sc_dev)); 260 1.6 skrll 261 1.6 skrll attached = true; 262 1.1 matt } 263