Home | History | Annotate | Line # | Download | only in broadcom
bcm2835_pmwdog.c revision 1.1.16.1
      1 /*	$NetBSD: bcm2835_pmwdog.c,v 1.1.16.1 2021/04/03 22:28:16 thorpej 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.16.1 2021/04/03 22:28:16 thorpej 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 static const struct device_compatible_entry compat_data[] = {
     89 	{ .compat = "brcm,bcm2835-pm-wdt" },
     90 	DEVICE_COMPAT_EOL
     91 };
     92 
     93 /* ARGSUSED */
     94 static int
     95 bcmpmwdog_match(device_t parent, cfdata_t match, void *aux)
     96 {
     97 	struct fdt_attach_args * const faa = aux;
     98 
     99 	return of_compatible_match(faa->faa_phandle, compat_data);
    100 }
    101 
    102 static void
    103 bcmpmwdog_attach(device_t parent, device_t self, void *aux)
    104 {
    105 	struct bcm2835pmwdog_softc *sc = device_private(self);
    106 	struct fdt_attach_args * const faa = aux;
    107 	const int phandle = faa->faa_phandle;
    108 
    109 	aprint_naive("\n");
    110 	aprint_normal(": Power management, Reset and Watchdog controller\n");
    111 
    112 	if (bcm2835pmwdog_sc == NULL)
    113 		bcm2835pmwdog_sc = sc;
    114 
    115 	sc->sc_dev = self;
    116 	sc->sc_iot = faa->faa_bst;
    117 
    118 	bus_addr_t addr;
    119 	bus_size_t size;
    120 
    121 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    122 		aprint_error(": couldn't get register address\n");
    123 		return;
    124 	}
    125 
    126 	if (bus_space_map(faa->faa_bst, addr, size, 0, &sc->sc_ioh) != 0) {
    127 		aprint_error_dev(sc->sc_dev, "unable to map device\n");
    128 		return;
    129 	}
    130 
    131 	/* watchdog */
    132 	sc->sc_smw.smw_name = device_xname(sc->sc_dev);
    133 	sc->sc_smw.smw_cookie = sc;
    134 	sc->sc_smw.smw_setmode = bcmpmwdog_setmode;
    135 	sc->sc_smw.smw_tickle = bcmpmwdog_tickle;
    136 	sc->sc_smw.smw_period = BCM2835_PM_DEFAULT_PERIOD;
    137 	if (sysmon_wdog_register(&sc->sc_smw) != 0)
    138 		aprint_error_dev(self, "couldn't register watchdog\n");
    139 }
    140 
    141 static void
    142 bcmpmwdog_set_timeout(struct bcm2835pmwdog_softc *sc, uint32_t ticks)
    143 {
    144 	uint32_t tmp, rstc, wdog;
    145 
    146 	tmp = bus_space_read_4(sc->sc_iot, sc->sc_ioh, BCM2835_PM_RSTC);
    147 
    148 	rstc = wdog = BCM2835_PM_PASSWORD;
    149 
    150 	rstc |= tmp & ~BCM2835_PM_RSTC_CONFIGMASK;
    151 	rstc |= BCM2835_PM_RSTC_FULL_RESET;
    152 
    153 	wdog |= ticks & BCM2835_PM_WDOG_TIMEMASK;
    154 
    155 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BCM2835_PM_WDOG, wdog);
    156 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, BCM2835_PM_RSTC, rstc);
    157 }
    158 
    159 static int
    160 bcmpmwdog_setmode(struct sysmon_wdog *smw)
    161 {
    162 	struct bcm2835pmwdog_softc *sc = smw->smw_cookie;
    163 	int error = 0;
    164 
    165 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
    166 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, BCM2835_PM_RSTC,
    167 		    BCM2835_PM_PASSWORD | BCM2835_PM_RSTC_RESET);
    168 	} else {
    169 		if (smw->smw_period == WDOG_PERIOD_DEFAULT)
    170 			smw->smw_period = BCM2835_PM_DEFAULT_PERIOD;
    171 		if (smw->smw_period > (BCM2835_PM_WDOG_TIMEMASK >> 16))
    172 			return EINVAL;
    173 		error = bcmpmwdog_tickle(smw);
    174 	}
    175 
    176 	return error;
    177 }
    178 
    179 static int
    180 bcmpmwdog_tickle(struct sysmon_wdog *smw)
    181 {
    182 	struct bcm2835pmwdog_softc *sc = smw->smw_cookie;
    183 	uint32_t timeout = smw->smw_period << 16;
    184 
    185 	bcmpmwdog_set_timeout(sc, timeout);
    186 
    187 	return 0;
    188 }
    189 
    190 void
    191 bcm2835_system_reset(void)
    192 {
    193 	struct bcm2835pmwdog_softc *sc = bcm2835pmwdog_sc;
    194 	uint32_t timeout = 10;
    195 
    196 	bcmpmwdog_set_timeout(sc, timeout);
    197 }
    198