1 1.4 jdolecek /* $NetBSD: gemini_wdt.c,v 1.4 2019/01/08 19:41:10 jdolecek Exp $ */ 2 1.1 matt 3 1.1 matt /* 4 1.1 matt * OMAP watchdog timers, common code 5 1.1 matt * 6 1.1 matt * Copyright (c) 2007 Microsoft 7 1.1 matt * All rights reserved. 8 1.1 matt * 9 1.1 matt * Redistribution and use in source and binary forms, with or without 10 1.1 matt * modification, are permitted provided that the following conditions 11 1.1 matt * are met: 12 1.1 matt * 1. Redistributions of source code must retain the above copyright 13 1.1 matt * notice, this list of conditions and the following disclaimer. 14 1.1 matt * 2. Redistributions in binary form must reproduce the above copyright 15 1.1 matt * notice, this list of conditions and the following disclaimer in the 16 1.1 matt * documentation and/or other materials provided with the distribution. 17 1.1 matt * 3. All advertising materials mentioning features or use of this software 18 1.1 matt * must display the following acknowledgement: 19 1.1 matt * This product includes software developed by Microsoft 20 1.1 matt * 21 1.1 matt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 22 1.1 matt * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 23 1.1 matt * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 1.1 matt * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTERS BE LIABLE FOR ANY DIRECT, 25 1.1 matt * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 26 1.1 matt * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 27 1.1 matt * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 1.1 matt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 1.1 matt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 1.1 matt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 1.1 matt * SUCH DAMAGE. 32 1.1 matt */ 33 1.1 matt 34 1.1 matt #include <sys/cdefs.h> 35 1.1 matt 36 1.1 matt #include <sys/param.h> 37 1.1 matt #include <sys/callout.h> 38 1.1 matt #include <sys/cdefs.h> 39 1.1 matt #include <sys/device.h> 40 1.1 matt #include <sys/kernel.h> 41 1.1 matt #include <sys/systm.h> 42 1.1 matt #include <sys/wdog.h> 43 1.1 matt 44 1.3 dyoung #include <sys/bus.h> 45 1.1 matt #include <dev/sysmon/sysmonvar.h> 46 1.1 matt 47 1.1 matt #include <arm/gemini/gemini_wdtvar.h> 48 1.1 matt #include <arm/gemini/gemini_reg.h> 49 1.1 matt 50 1.1 matt geminiwdt_softc_t *geminiwdt_sc; 51 1.1 matt 52 1.1 matt #define WATCHDOG_COUNT(nsec) \ 53 1.1 matt (GEMINI_WDT_CLOCK_FREQ * (nsec)) 54 1.1 matt 55 1.1 matt 56 1.1 matt static void 57 1.1 matt geminiwdt_start(void) 58 1.1 matt { 59 1.1 matt geminiwdt_softc_t *sc = geminiwdt_sc; 60 1.1 matt uint32_t r; 61 1.1 matt 62 1.1 matt r = bus_space_read_4(sc->sc_iot, sc->sc_ioh, GEMINI_WDT_WDCR); 63 1.1 matt r |= (WDT_WDCR_RESET_ENB 64 1.1 matt |WDT_WDCR_ENB); 65 1.1 matt bus_space_write_4(sc->sc_iot, sc->sc_ioh, GEMINI_WDT_WDCR, r); 66 1.1 matt } 67 1.1 matt 68 1.1 matt static void 69 1.1 matt geminiwdt_stop(void) 70 1.1 matt { 71 1.1 matt geminiwdt_softc_t *sc = geminiwdt_sc; 72 1.1 matt uint32_t r; 73 1.1 matt 74 1.1 matt r = bus_space_read_4(sc->sc_iot, sc->sc_ioh, GEMINI_WDT_WDCR); 75 1.1 matt r &= ~(WDT_WDCR_EXTSIG_ENB 76 1.1 matt |WDT_WDCR_RESET_ENB 77 1.1 matt |WDT_WDCR_INTR_ENB 78 1.1 matt |WDT_WDCR_ENB); 79 1.1 matt bus_space_write_4(sc->sc_iot, sc->sc_ioh, GEMINI_WDT_WDCR, r); 80 1.1 matt bus_space_write_4(sc->sc_iot, sc->sc_ioh, 81 1.1 matt GEMINI_WDT_WDCLEAR, WDT_WDCLEAR_CLEAR); 82 1.1 matt } 83 1.1 matt 84 1.1 matt static void 85 1.1 matt geminiwdt_do_set_timeout(void) 86 1.1 matt { 87 1.1 matt geminiwdt_softc_t *sc = geminiwdt_sc; 88 1.1 matt uint32_t r; 89 1.1 matt 90 1.1 matt /* 91 1.1 matt * Disable the watchdog timer 92 1.1 matt */ 93 1.1 matt if (sc->sc_armed) 94 1.1 matt geminiwdt_stop(); 95 1.1 matt 96 1.1 matt /* 97 1.1 matt * Set WdLoad register 98 1.1 matt */ 99 1.1 matt r = (sc->sc_smw.smw_period != 0) ? 100 1.1 matt WATCHDOG_COUNT(sc->sc_smw.smw_period) : WDT_WDLOAD_DFLT; 101 1.1 matt bus_space_write_4(sc->sc_iot, sc->sc_ioh, GEMINI_WDT_WDLOAD, r); 102 1.1 matt 103 1.1 matt /* 104 1.1 matt * feed MAGIC treat to dog 105 1.1 matt */ 106 1.1 matt bus_space_write_4(sc->sc_iot, sc->sc_ioh, 107 1.1 matt GEMINI_WDT_WDRESTART, WDT_WDRESTART_MAGIC); 108 1.1 matt 109 1.1 matt /* 110 1.1 matt * Select PCLK clock source 111 1.1 matt */ 112 1.1 matt r = bus_space_read_4(sc->sc_iot, sc->sc_ioh, GEMINI_WDT_WDCR); 113 1.1 matt r &= ~WDCR_CLKSRC_5MHZ; 114 1.1 matt bus_space_write_4(sc->sc_iot, sc->sc_ioh, GEMINI_WDT_WDCR, r); 115 1.1 matt 116 1.1 matt /* 117 1.1 matt * Enable the timer 118 1.1 matt */ 119 1.1 matt if (sc->sc_armed) 120 1.1 matt geminiwdt_start(); 121 1.1 matt } 122 1.1 matt 123 1.1 matt void 124 1.1 matt geminiwdt_set_timeout(unsigned int period) 125 1.1 matt { 126 1.1 matt geminiwdt_softc_t *sc = geminiwdt_sc; 127 1.1 matt int s = splhigh(); 128 1.1 matt 129 1.1 matt if (period != sc->sc_smw.smw_period) { 130 1.1 matt sc->sc_smw.smw_period = period; 131 1.1 matt geminiwdt_do_set_timeout(); 132 1.1 matt } 133 1.1 matt 134 1.1 matt splx(s); 135 1.1 matt } 136 1.1 matt 137 1.1 matt int 138 1.1 matt geminiwdt_enable(int enable) 139 1.1 matt { 140 1.1 matt geminiwdt_softc_t *sc = geminiwdt_sc; 141 1.1 matt int s; 142 1.1 matt int prev_state = geminiwdt_sc->sc_armed; 143 1.1 matt 144 1.1 matt /* Normalize the int to a boolean so we can compare values directly. 145 1.1 matt */ 146 1.1 matt enable = !!enable; 147 1.1 matt 148 1.1 matt s = splhigh(); 149 1.1 matt 150 1.1 matt if (enable != sc->sc_armed) { 151 1.1 matt if (enable) { 152 1.1 matt /* Make sure that the watchdog timeout is up to date. 153 1.1 matt */ 154 1.1 matt geminiwdt_do_set_timeout(); 155 1.1 matt geminiwdt_start(); 156 1.1 matt } else { 157 1.1 matt geminiwdt_stop(); 158 1.1 matt } 159 1.1 matt sc->sc_armed = enable; 160 1.1 matt } 161 1.1 matt 162 1.1 matt splx(s); 163 1.1 matt return prev_state; 164 1.1 matt } 165 1.1 matt 166 1.1 matt int 167 1.1 matt geminiwdt_setmode(struct sysmon_wdog *smw) 168 1.1 matt { 169 1.1 matt geminiwdt_softc_t *sc = smw->smw_cookie; 170 1.1 matt int error = 0; 171 1.1 matt 172 1.1 matt if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) { 173 1.1 matt geminiwdt_enable(0); 174 1.1 matt } else { 175 1.1 matt if (smw->smw_period == WDOG_PERIOD_DEFAULT) 176 1.1 matt sc->sc_smw.smw_period = WDT_WDLOAD_DFLT; 177 1.1 matt else 178 1.1 matt sc->sc_smw.smw_period = smw->smw_period; 179 1.1 matt geminiwdt_set_timeout(sc->sc_smw.smw_period); 180 1.1 matt geminiwdt_enable(1); 181 1.1 matt } 182 1.1 matt return error; 183 1.1 matt } 184 1.1 matt 185 1.1 matt int 186 1.1 matt geminiwdt_tickle(struct sysmon_wdog *smw) 187 1.1 matt { 188 1.1 matt geminiwdt_softc_t *sc = geminiwdt_sc; 189 1.1 matt int s = splhigh(); 190 1.1 matt 191 1.1 matt /* 192 1.1 matt * feed the dog a MAGIC treat 193 1.1 matt */ 194 1.1 matt bus_space_write_4(sc->sc_iot, sc->sc_ioh, 195 1.1 matt GEMINI_WDT_WDRESTART, WDT_WDRESTART_MAGIC); 196 1.1 matt 197 1.1 matt splx(s); 198 1.1 matt return 0; 199 1.1 matt } 200