1 /* $NetBSD: bcm2835_pmwdog.c,v 1.1 2017/12/10 21:38:26 skrll Exp $ */ 2 3 /*- 4 * Copyright (c) 2012, 2016 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Nick Hudson 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: bcm2835_pmwdog.c,v 1.1 2017/12/10 21:38:26 skrll Exp $"); 34 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/device.h> 39 #include <sys/kernel.h> 40 #include <sys/timetc.h> 41 #include <sys/wdog.h> 42 #include <sys/bus.h> 43 44 #include <dev/sysmon/sysmonvar.h> 45 46 #include <arm/broadcom/bcm2835reg.h> 47 #include <arm/broadcom/bcm2835_intr.h> 48 #include <arm/broadcom/bcm2835_pmwdogvar.h> 49 50 #include <dev/fdt/fdtvar.h> 51 52 #ifndef BCM2835_PM_DEFAULT_PERIOD 53 #define BCM2835_PM_DEFAULT_PERIOD 15 /* seconds */ 54 #endif 55 56 #define BCM2835_PM_PASSWORD 0x5a000000 57 58 #define BCM2835_PM_RSTC 0x1c 59 #define BCM2835_PM_RSTC_CONFIGMASK 0x00000030 60 #define BCM2835_PM_RSTC_FULL_RESET 0x00000020 61 #define BCM2835_PM_RSTC_RESET 0x00000102 62 63 #define BCM2835_PM_WDOG 0x24 64 #define BCM2835_PM_WDOG_TIMEMASK 0x000fffff 65 66 struct bcm2835pmwdog_softc { 67 device_t sc_dev; 68 69 bus_space_tag_t sc_iot; 70 bus_space_handle_t sc_ioh; 71 72 struct sysmon_wdog sc_smw; 73 }; 74 75 static struct bcm2835pmwdog_softc *bcm2835pmwdog_sc; 76 77 static int bcmpmwdog_match(device_t, cfdata_t, void *); 78 static void bcmpmwdog_attach(device_t, device_t, void *); 79 80 static void bcmpmwdog_set_timeout(struct bcm2835pmwdog_softc *, uint32_t); 81 82 static int bcmpmwdog_setmode(struct sysmon_wdog *); 83 static int bcmpmwdog_tickle(struct sysmon_wdog *); 84 85 CFATTACH_DECL_NEW(bcmpmwdog_fdt, sizeof(struct bcm2835pmwdog_softc), 86 bcmpmwdog_match, bcmpmwdog_attach, NULL, NULL); 87 88 /* ARGSUSED */ 89 static int 90 bcmpmwdog_match(device_t parent, cfdata_t match, void *aux) 91 { 92 const char * const compatible[] = { "brcm,bcm2835-pm-wdt", NULL }; 93 struct fdt_attach_args * const faa = aux; 94 95 return of_match_compatible(faa->faa_phandle, compatible); 96 } 97 98 static void 99 bcmpmwdog_attach(device_t parent, device_t self, void *aux) 100 { 101 struct bcm2835pmwdog_softc *sc = device_private(self); 102 struct fdt_attach_args * const faa = aux; 103 const int phandle = faa->faa_phandle; 104 105 aprint_naive("\n"); 106 aprint_normal(": Power management, Reset and Watchdog controller\n"); 107 108 if (bcm2835pmwdog_sc == NULL) 109 bcm2835pmwdog_sc = sc; 110 111 sc->sc_dev = self; 112 sc->sc_iot = faa->faa_bst; 113 114 bus_addr_t addr; 115 bus_size_t size; 116 117 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 118 aprint_error(": couldn't get register address\n"); 119 return; 120 } 121 122 if (bus_space_map(faa->faa_bst, addr, size, 0, &sc->sc_ioh) != 0) { 123 aprint_error_dev(sc->sc_dev, "unable to map device\n"); 124 return; 125 } 126 127 /* watchdog */ 128 sc->sc_smw.smw_name = device_xname(sc->sc_dev); 129 sc->sc_smw.smw_cookie = sc; 130 sc->sc_smw.smw_setmode = bcmpmwdog_setmode; 131 sc->sc_smw.smw_tickle = bcmpmwdog_tickle; 132 sc->sc_smw.smw_period = BCM2835_PM_DEFAULT_PERIOD; 133 if (sysmon_wdog_register(&sc->sc_smw) != 0) 134 aprint_error_dev(self, "couldn't register watchdog\n"); 135 } 136 137 static void 138 bcmpmwdog_set_timeout(struct bcm2835pmwdog_softc *sc, uint32_t ticks) 139 { 140 uint32_t tmp, rstc, wdog; 141 142 tmp = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BCM2835_PM_RSTC); 143 144 rstc = wdog = BCM2835_PM_PASSWORD; 145 146 rstc |= tmp & ~BCM2835_PM_RSTC_CONFIGMASK; 147 rstc |= BCM2835_PM_RSTC_FULL_RESET; 148 149 wdog |= ticks & BCM2835_PM_WDOG_TIMEMASK; 150 151 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BCM2835_PM_WDOG, wdog); 152 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BCM2835_PM_RSTC, rstc); 153 } 154 155 static int 156 bcmpmwdog_setmode(struct sysmon_wdog *smw) 157 { 158 struct bcm2835pmwdog_softc *sc = smw->smw_cookie; 159 int error = 0; 160 161 if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) { 162 bus_space_write_4(sc->sc_iot, sc->sc_ioh, BCM2835_PM_RSTC, 163 BCM2835_PM_PASSWORD | BCM2835_PM_RSTC_RESET); 164 } else { 165 if (smw->smw_period == WDOG_PERIOD_DEFAULT) 166 smw->smw_period = BCM2835_PM_DEFAULT_PERIOD; 167 if (smw->smw_period > (BCM2835_PM_WDOG_TIMEMASK >> 16)) 168 return EINVAL; 169 error = bcmpmwdog_tickle(smw); 170 } 171 172 return error; 173 } 174 175 static int 176 bcmpmwdog_tickle(struct sysmon_wdog *smw) 177 { 178 struct bcm2835pmwdog_softc *sc = smw->smw_cookie; 179 uint32_t timeout = smw->smw_period << 16; 180 181 bcmpmwdog_set_timeout(sc, timeout); 182 183 return 0; 184 } 185 186 void 187 bcm2835_system_reset(void) 188 { 189 struct bcm2835pmwdog_softc *sc = bcm2835pmwdog_sc; 190 uint32_t timeout = 10; 191 192 bcmpmwdog_set_timeout(sc, timeout); 193 } 194