Home | History | Annotate | Line # | Download | only in dev
      1 /* $NetBSD: wdog.c,v 1.13 2026/06/14 00:02:35 rkujawa Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2002 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Jason R. Thorpe and Simon Burge for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *	This product includes software developed for the NetBSD Project by
     20  *	Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 /*
     39  * Watchdog timer support for the IBM 405GP and 440/460EX processors.
     40  */
     41 
     42 #include <sys/cdefs.h>
     43 __KERNEL_RCSID(0, "$NetBSD: wdog.c,v 1.13 2026/06/14 00:02:35 rkujawa Exp $");
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/callout.h>
     48 #include <sys/device.h>
     49 #include <sys/cpu.h>
     50 #include <sys/wdog.h>
     51 
     52 #include <prop/proplib.h>
     53 
     54 #include <powerpc/spr.h>
     55 #include <powerpc/ibm4xx/spr.h>
     56 #include <powerpc/ibm4xx/cpu.h>
     57 #include <powerpc/ibm4xx/dev/opbvar.h>
     58 
     59 #include <dev/sysmon/sysmonvar.h>
     60 
     61 static int wdog_match(device_t, cfdata_t, void *);
     62 static void wdog_attach(device_t, device_t, void *);
     63 static int wdog_tickle(struct sysmon_wdog *);
     64 static int wdog_setmode(struct sysmon_wdog *);
     65 #ifdef PPC_IBM440
     66 static void wdog_ktickle(void *);
     67 #endif
     68 
     69 struct wdog_softc {
     70 	device_t sc_dev;
     71 	struct sysmon_wdog sc_smw;
     72 	bool sc_wdog_armed;
     73 	int sc_wdog_period;
     74 #ifdef PPC_IBM440
     75 	struct callout sc_tickler;	/* self-tickle after "disarm" */
     76 #endif
     77 };
     78 
     79 CFATTACH_DECL_NEW(wdog, sizeof(struct wdog_softc),
     80     wdog_match, wdog_attach, NULL, NULL);
     81 
     82 static int
     83 wdog_match(device_t parent, cfdata_t cf, void *aux)
     84 {
     85 	struct opb_attach_args * const oaa = aux;
     86 
     87 	/* match only watchdog devices */
     88 	if (strcmp(oaa->opb_name, cf->cf_name) != 0)
     89 		return (0);
     90 
     91 	return (1);
     92 }
     93 
     94 static void
     95 wdog_attach(device_t parent, device_t self, void *aux)
     96 {
     97 	struct wdog_softc * const sc = device_private(self);
     98 	unsigned int processor_freq;
     99 	prop_number_t freq;
    100 
    101 	freq = prop_dictionary_get(board_properties, "processor-frequency");
    102 	KASSERT(freq != NULL);
    103 	processor_freq = (unsigned int) prop_number_integer_value(freq);
    104 
    105 #ifdef PPC_IBM440
    106 	/*
    107 	 * The 440 watchdog period encodings sit four powers of two
    108 	 * above the 405's... for whatever reason.
    109 	 */
    110 	sc->sc_wdog_period = (2LL << 33) / processor_freq;
    111 #else
    112 	sc->sc_wdog_period = (2LL << 29) / processor_freq;
    113 #endif
    114 	aprint_normal(": %d second period\n", sc->sc_wdog_period);
    115 
    116 #ifdef PPC_IBM440
    117 	/*
    118 	 * TSR[WRS] survives a watchdog-forced reset; report and clear
    119 	 * it (TSR is write-1-to-clear) so a rebooted headless board
    120 	 * leaves evidence of why.
    121 	 *
    122 	 * Btw. on Sam460ex firmware messes with TSR[WRS], so it's
    123 	 * pointless.
    124 	 */
    125 	if (mfspr(SPR_TSR) & TSR_WRS_MASK) {
    126 		aprint_normal_dev(self,
    127 		    "previous reset was forced by the watchdog\n");
    128 		mtspr(SPR_TSR, TSR_WRS_MASK);
    129 	}
    130 
    131 	callout_init(&sc->sc_tickler, 0);
    132 	callout_setfunc(&sc->sc_tickler, wdog_ktickle, sc);
    133 #endif
    134 
    135 	sc->sc_dev = self;
    136 	sc->sc_smw.smw_name = device_xname(self);
    137 	sc->sc_smw.smw_cookie = sc;
    138 	sc->sc_smw.smw_setmode = wdog_setmode;
    139 	sc->sc_smw.smw_tickle = wdog_tickle;
    140 	sc->sc_smw.smw_period = sc->sc_wdog_period;
    141 
    142 	if (sysmon_wdog_register(&sc->sc_smw) != 0)
    143 		aprint_error_dev(self, "unable to register with sysmon\n");
    144 }
    145 
    146 static int
    147 wdog_tickle(struct sysmon_wdog *smw)
    148 {
    149 #ifdef PPC_IBM440
    150 	/*
    151 	 * TSR is write-1-to-clear
    152 	 */
    153 	mtspr(SPR_TSR, TSR_ENW | TSR_WIS);
    154 #else
    155 	uint32_t tsr;
    156 
    157 	tsr = mfspr(SPR_TSR);
    158 	tsr |= TSR_ENW | TSR_WIS;
    159 	mtspr(SPR_TSR, tsr);
    160 #endif
    161 	return (0);
    162 }
    163 
    164 #ifdef PPC_IBM440
    165 /*
    166  * TCR[WRC] is sticky: once the watchdog has been armed with a reset
    167  * action it cannot be turned off until the next reset.  "Disarming"
    168  * therefore hands the tickling duty to this callout instead.
    169  */
    170 static void
    171 wdog_ktickle(void *arg)
    172 {
    173 	struct wdog_softc * const sc = arg;
    174 
    175 	mtspr(SPR_TSR, TSR_ENW | TSR_WIS);
    176 	callout_schedule(&sc->sc_tickler, hz * sc->sc_wdog_period / 3);
    177 }
    178 #endif
    179 
    180 static int
    181 wdog_setmode(struct sysmon_wdog *smw)
    182 {
    183 	struct wdog_softc * const sc = smw->smw_cookie;
    184 
    185 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
    186 		if (sc->sc_wdog_armed) {
    187 #ifdef PPC_IBM440
    188 			/*
    189 			 * The hardware cannot be disarmed (sticky
    190 			 * TCR[WRC]); keep it fed from a callout so the
    191 			 * system stays up without userland's help.
    192 			 */
    193 			sc->sc_wdog_armed = false;
    194 			wdog_ktickle(sc);
    195 #else
    196 			uint32_t tsr = mfspr(SPR_TSR);
    197 			tsr &= ~(TSR_ENW | TSR_WIS);
    198 			mtspr(SPR_TSR, tsr);
    199 			sc->sc_wdog_armed = false;
    200 #endif
    201 		}
    202 	} else {
    203 		if (smw->smw_period == WDOG_PERIOD_DEFAULT)
    204 			smw->smw_period = sc->sc_wdog_period;
    205 		else if (smw->smw_period != sc->sc_wdog_period) {
    206 			/*
    207 			 * There's 4 set watchdog periods, but we only
    208 			 * support the longest one (2.684 seconds on a
    209 			 * 400MHz 405GP, 14.9 seconds on a 1.155GHz
    210 			 * 460EX).
    211 			 */
    212 			return (EOPNOTSUPP);
    213 		}
    214 		sc->sc_wdog_armed = true;
    215 
    216 #ifdef PPC_IBM440
    217 		/*
    218 		 * TCR is shared with the clock (PIE/FIE) and the tprof
    219 		 * FIT backend (FP), all read-modify-write: keep the
    220 		 * update atomic with respect to them.
    221 		 */
    222 		const int s = splhigh();
    223 		uint32_t tcr = mfspr(SPR_TCR);
    224 		tcr |= TCR_WP_2_33 | TCR_WRC_SYSTEM;
    225 		mtspr(SPR_TCR, tcr);
    226 		splx(s);
    227 		callout_stop(&sc->sc_tickler);
    228 #else
    229 		uint32_t tcr = mfspr(SPR_TCR);
    230 		tcr |= TCR_WP_2_29 | TCR_WRC_SYSTEM;
    231 		mtspr(SPR_TCR, tcr);
    232 #endif
    233 
    234 		/* Arm the watchdog. */
    235 		wdog_tickle(smw);
    236 	}
    237 	return (0);
    238 }
    239