1 1.2 thorpej /* $NetBSD: meson_wdt.c,v 1.2 2021/01/27 03:10:18 thorpej Exp $ */ 2 1.1 jmcneill 3 1.1 jmcneill /*- 4 1.1 jmcneill * Copyright (c) 2019 Jared McNeill <jmcneill (at) invisible.ca> 5 1.1 jmcneill * All rights reserved. 6 1.1 jmcneill * 7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without 8 1.1 jmcneill * modification, are permitted provided that the following conditions 9 1.1 jmcneill * are met: 10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright 11 1.1 jmcneill * notice, this list of conditions and the following disclaimer. 12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the 14 1.1 jmcneill * documentation and/or other materials provided with the distribution. 15 1.1 jmcneill * 16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 1.1 jmcneill * SUCH DAMAGE. 27 1.1 jmcneill */ 28 1.1 jmcneill 29 1.1 jmcneill #include <sys/cdefs.h> 30 1.2 thorpej __KERNEL_RCSID(0, "$NetBSD: meson_wdt.c,v 1.2 2021/01/27 03:10:18 thorpej Exp $"); 31 1.1 jmcneill 32 1.1 jmcneill #include <sys/param.h> 33 1.1 jmcneill #include <sys/bus.h> 34 1.1 jmcneill #include <sys/cpu.h> 35 1.1 jmcneill #include <sys/device.h> 36 1.1 jmcneill #include <sys/wdog.h> 37 1.1 jmcneill 38 1.1 jmcneill #include <dev/sysmon/sysmonvar.h> 39 1.1 jmcneill 40 1.1 jmcneill #include <dev/fdt/fdtvar.h> 41 1.1 jmcneill 42 1.1 jmcneill #define CBUS_REG(x) ((x) << 2) 43 1.1 jmcneill 44 1.1 jmcneill #define WATCHDOG_TC_REG CBUS_REG(0) 45 1.1 jmcneill #define WATCHDOG_TC_CPUS __BITS(27,24) 46 1.1 jmcneill #define WATCHDOG_TC_ENABLE __BIT(19) 47 1.1 jmcneill #define WATCHDOG_TC_TCNT __BITS(15,0) 48 1.1 jmcneill 49 1.1 jmcneill #define WATCHDOG_RESET_REG CBUS_REG(1) 50 1.1 jmcneill #define WATCHDOG_RESET_COUNT __BITS(15,0) 51 1.1 jmcneill 52 1.1 jmcneill #define WATCHDOG_PERIOD_DEFAULT 8 53 1.1 jmcneill #define WATCHDOG_PERIOD_MAX 8 54 1.1 jmcneill #define WATCHDOG_TICKS_PER_SEC 7812 55 1.1 jmcneill 56 1.2 thorpej static const struct device_compatible_entry compat_data[] = { 57 1.2 thorpej { .compat = "amlogic,meson8b-wdt" }, 58 1.2 thorpej DEVICE_COMPAT_EOL 59 1.1 jmcneill }; 60 1.1 jmcneill 61 1.1 jmcneill struct meson_wdt_softc { 62 1.1 jmcneill device_t sc_dev; 63 1.1 jmcneill bus_space_tag_t sc_bst; 64 1.1 jmcneill bus_space_handle_t sc_bsh; 65 1.1 jmcneill 66 1.1 jmcneill struct sysmon_wdog sc_wdog; 67 1.1 jmcneill }; 68 1.1 jmcneill 69 1.1 jmcneill #define WDT_READ(sc, reg) \ 70 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg)) 71 1.1 jmcneill #define WDT_WRITE(sc, reg, val) \ 72 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val)) 73 1.1 jmcneill 74 1.1 jmcneill static int 75 1.1 jmcneill meson_wdt_setmode(struct sysmon_wdog *smw) 76 1.1 jmcneill { 77 1.1 jmcneill struct meson_wdt_softc * const sc = smw->smw_cookie; 78 1.1 jmcneill uint32_t val; 79 1.1 jmcneill 80 1.1 jmcneill if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) { 81 1.1 jmcneill WDT_WRITE(sc, WATCHDOG_RESET_REG, 0); 82 1.1 jmcneill val = WDT_READ(sc, WATCHDOG_TC_REG); 83 1.1 jmcneill val &= ~WATCHDOG_TC_ENABLE; 84 1.1 jmcneill WDT_WRITE(sc, WATCHDOG_TC_REG, val); 85 1.1 jmcneill return 0; 86 1.1 jmcneill } 87 1.1 jmcneill 88 1.1 jmcneill if (smw->smw_period == WDOG_PERIOD_DEFAULT) { 89 1.1 jmcneill sc->sc_wdog.smw_period = WATCHDOG_PERIOD_DEFAULT; 90 1.1 jmcneill } else if (smw->smw_period == 0 || 91 1.1 jmcneill smw->smw_period > WATCHDOG_PERIOD_MAX) { 92 1.1 jmcneill return EINVAL; 93 1.1 jmcneill } else { 94 1.1 jmcneill sc->sc_wdog.smw_period = smw->smw_period; 95 1.1 jmcneill } 96 1.1 jmcneill 97 1.1 jmcneill const u_int tcnt = sc->sc_wdog.smw_period * WATCHDOG_TICKS_PER_SEC; 98 1.1 jmcneill WDT_WRITE(sc, WATCHDOG_RESET_REG, 0); 99 1.1 jmcneill WDT_WRITE(sc, WATCHDOG_TC_REG, WATCHDOG_TC_CPUS | WATCHDOG_TC_ENABLE | 100 1.1 jmcneill __SHIFTIN(tcnt, WATCHDOG_TC_TCNT)); 101 1.1 jmcneill 102 1.1 jmcneill return 0; 103 1.1 jmcneill } 104 1.1 jmcneill 105 1.1 jmcneill static int 106 1.1 jmcneill meson_wdt_tickle(struct sysmon_wdog *smw) 107 1.1 jmcneill { 108 1.1 jmcneill struct meson_wdt_softc * const sc = smw->smw_cookie; 109 1.1 jmcneill 110 1.1 jmcneill WDT_WRITE(sc, WATCHDOG_RESET_REG, 0); 111 1.1 jmcneill 112 1.1 jmcneill return 0; 113 1.1 jmcneill } 114 1.1 jmcneill 115 1.1 jmcneill static int 116 1.1 jmcneill meson_wdt_match(device_t parent, cfdata_t cf, void *aux) 117 1.1 jmcneill { 118 1.1 jmcneill struct fdt_attach_args * const faa = aux; 119 1.1 jmcneill 120 1.2 thorpej return of_compatible_match(faa->faa_phandle, compat_data); 121 1.1 jmcneill } 122 1.1 jmcneill 123 1.1 jmcneill static void 124 1.1 jmcneill meson_wdt_attach(device_t parent, device_t self, void *aux) 125 1.1 jmcneill { 126 1.1 jmcneill struct meson_wdt_softc * const sc = device_private(self); 127 1.1 jmcneill struct fdt_attach_args * const faa = aux; 128 1.1 jmcneill const int phandle = faa->faa_phandle; 129 1.1 jmcneill bus_addr_t addr; 130 1.1 jmcneill bus_size_t size; 131 1.1 jmcneill uint32_t val; 132 1.1 jmcneill 133 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 134 1.1 jmcneill aprint_error(": couldn't get registers\n"); 135 1.1 jmcneill return; 136 1.1 jmcneill } 137 1.1 jmcneill 138 1.1 jmcneill sc->sc_dev = self; 139 1.1 jmcneill sc->sc_bst = faa->faa_bst; 140 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) { 141 1.1 jmcneill aprint_error(": couldn't map registers\n"); 142 1.1 jmcneill return; 143 1.1 jmcneill } 144 1.1 jmcneill 145 1.1 jmcneill aprint_naive("\n"); 146 1.1 jmcneill aprint_normal(": EE-watchdog\n"); 147 1.1 jmcneill 148 1.1 jmcneill /* Disable watchdog */ 149 1.1 jmcneill WDT_WRITE(sc, WATCHDOG_RESET_REG, 0); 150 1.1 jmcneill val = WDT_READ(sc, WATCHDOG_TC_REG); 151 1.1 jmcneill val &= ~WATCHDOG_TC_ENABLE; 152 1.1 jmcneill WDT_WRITE(sc, WATCHDOG_TC_REG, val); 153 1.1 jmcneill 154 1.1 jmcneill /* Register watchdog */ 155 1.1 jmcneill sc->sc_wdog.smw_name = "EE-watchdog"; 156 1.1 jmcneill sc->sc_wdog.smw_setmode = meson_wdt_setmode; 157 1.1 jmcneill sc->sc_wdog.smw_tickle = meson_wdt_tickle; 158 1.1 jmcneill sc->sc_wdog.smw_period = WATCHDOG_PERIOD_DEFAULT; 159 1.1 jmcneill sc->sc_wdog.smw_cookie = sc; 160 1.1 jmcneill sysmon_wdog_register(&sc->sc_wdog); 161 1.1 jmcneill } 162 1.1 jmcneill 163 1.1 jmcneill CFATTACH_DECL_NEW(meson_wdt, sizeof(struct meson_wdt_softc), 164 1.1 jmcneill meson_wdt_match, meson_wdt_attach, NULL, NULL); 165