Home | History | Annotate | Line # | Download | only in pci
geodewdg.c revision 1.7
      1 /*	$NetBSD: geodewdg.c,v 1.7 2008/01/03 04:52:55 dyoung Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2005 David Young.  All rights reserved.
      5  *
      6  * This code was written by David Young.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by David Young.
     19  * 4. The name of David Young may not be used to endorse or promote
     20  *    products derived from this software without specific prior
     21  *    written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY DAVID YOUNG ``AS IS'' AND ANY
     24  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     25  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
     26  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DAVID
     27  * YOUNG BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     28  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
     29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     31  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     32  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
     34  * OF SUCH DAMAGE.
     35  */
     36 /*-
     37  * Copyright (c) 2002 The NetBSD Foundation, Inc.
     38  * All rights reserved.
     39  *
     40  * This code is derived from software contributed to The NetBSD Foundation
     41  * by Jason R. Thorpe.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  * 3. All advertising materials mentioning features or use of this software
     52  *    must display the following acknowledgement:
     53  *	This product includes software developed by the NetBSD
     54  *	Foundation, Inc. and its contributors.
     55  * 4. Neither the name of The NetBSD Foundation nor the names of its
     56  *    contributors may be used to endorse or promote products derived
     57  *    from this software without specific prior written permission.
     58  *
     59  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     60  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     61  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     62  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     63  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     64  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     65  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     66  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     67  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     68  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     69  * POSSIBILITY OF SUCH DAMAGE.
     70  */
     71 
     72 /*
     73  * Device driver for the watchdog timer built into the
     74  * AMD Geode SC1100 processor.
     75  */
     76 
     77 #include <sys/cdefs.h>
     78 
     79 __KERNEL_RCSID(0, "$NetBSD: geodewdg.c,v 1.7 2008/01/03 04:52:55 dyoung Exp $");
     80 
     81 #include <sys/param.h>
     82 #include <sys/systm.h>
     83 #include <sys/device.h>
     84 #include <sys/wdog.h>
     85 #include <uvm/uvm_extern.h>
     86 #include <machine/bus.h>
     87 #include <dev/pci/pcivar.h>
     88 #include <dev/pci/pcidevs.h>
     89 #include <arch/i386/pci/geodevar.h>
     90 #include <arch/i386/pci/geodereg.h>
     91 #include <dev/sysmon/sysmonvar.h>
     92 
     93 #ifdef GEODE_DEBUG
     94 #define	GEODE_DPRINTF(__x) printf __x
     95 #else /* GEODE_DEBUG */
     96 #define	GEODE_DPRINTF(__x) /* nothing */
     97 #endif
     98 
     99 struct geode_wdog_softc {
    100 	struct device		sc_dev;
    101 	struct geode_gcb_softc *sc_gcb_dev;
    102 
    103 	uint16_t		sc_countdown;
    104 	uint8_t			sc_prescale;
    105 	struct sysmon_wdog      sc_smw;
    106 };
    107 
    108 static int attached = 0;
    109 
    110 static void
    111 geode_wdog_disable(struct geode_wdog_softc *sc)
    112 {
    113 	uint16_t wdcnfg;
    114 
    115 	/* cancel any pending countdown */
    116 	sc->sc_countdown = 0;
    117 	bus_space_write_2(sc->sc_gcb_dev->sc_iot, sc->sc_gcb_dev->sc_ioh,
    118 	    SC1100_GCB_WDTO, 0);
    119 	/* power-down clock */
    120 	wdcnfg = bus_space_read_2(sc->sc_gcb_dev->sc_iot,
    121 	    sc->sc_gcb_dev->sc_ioh, SC1100_GCB_WDCNFG);
    122 
    123 	GEODE_DPRINTF(("%s: wdcnfg %#04" PRIx16 " -> ", __func__, wdcnfg));
    124 
    125 	wdcnfg |= SC1100_WDCNFG_WD32KPD;
    126 	wdcnfg &= ~(SC1100_WDCNFG_WDTYPE2_MASK | SC1100_WDCNFG_WDTYPE1_MASK);
    127 	/* This no-op is for the reader's benefit. */
    128         wdcnfg |= SC1100_WDCNFG_WDTYPE1_NOACTION |
    129                   SC1100_WDCNFG_WDTYPE2_NOACTION;
    130 	bus_space_write_2(sc->sc_gcb_dev->sc_iot, sc->sc_gcb_dev->sc_ioh,
    131 	    SC1100_GCB_WDCNFG, wdcnfg);
    132 
    133 	GEODE_DPRINTF(("%#04" PRIx16 "\n", wdcnfg));
    134 }
    135 
    136 static void
    137 geode_wdog_enable(struct geode_wdog_softc *sc)
    138 {
    139 	uint16_t wdcnfg;
    140 
    141 	/* power-up clock and set prescale */
    142 	wdcnfg = bus_space_read_2(sc->sc_gcb_dev->sc_iot,
    143 	    sc->sc_gcb_dev->sc_ioh, SC1100_GCB_WDCNFG);
    144 
    145 	GEODE_DPRINTF(("%s: wdcnfg %#04" PRIx16 " -> ", __func__, wdcnfg));
    146 
    147 	wdcnfg &= ~(SC1100_WDCNFG_WD32KPD | SC1100_WDCNFG_WDPRES_MASK |
    148 	            SC1100_WDCNFG_WDTYPE1_MASK | SC1100_WDCNFG_WDTYPE2_MASK);
    149 	wdcnfg |= __SHIFTIN(sc->sc_prescale, SC1100_WDCNFG_WDPRES_MASK);
    150         wdcnfg |= SC1100_WDCNFG_WDTYPE1_RESET | SC1100_WDCNFG_WDTYPE2_NOACTION;
    151 
    152 	bus_space_write_2(sc->sc_gcb_dev->sc_iot, sc->sc_gcb_dev->sc_ioh,
    153 	    SC1100_GCB_WDCNFG, wdcnfg);
    154 
    155 	GEODE_DPRINTF(("%#04" PRIx16 "\n", wdcnfg));
    156 }
    157 
    158 static void
    159 geode_wdog_reset(struct geode_wdog_softc *sc)
    160 {
    161 	/* set countdown */
    162 	bus_space_write_2(sc->sc_gcb_dev->sc_iot, sc->sc_gcb_dev->sc_ioh,
    163 	    SC1100_GCB_WDTO, sc->sc_countdown);
    164 }
    165 
    166 static int
    167 geode_wdog_tickle(struct sysmon_wdog *smw)
    168 {
    169 	int s;
    170 	struct geode_wdog_softc *sc = smw->smw_cookie;
    171 
    172 	s = splhigh();
    173 	geode_wdog_reset(sc);
    174 	splx(s);
    175 	return 0;
    176 }
    177 
    178 static int
    179 geode_wdog_setmode(struct sysmon_wdog *smw)
    180 {
    181 	struct geode_wdog_softc *sc = smw->smw_cookie;
    182 	uint32_t ticks;
    183 	int prescale, s;
    184 
    185 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
    186 		s = splhigh();
    187 		geode_wdog_disable(sc);
    188 		splx(s);
    189 		return 0;
    190 	}
    191 	if (smw->smw_period == WDOG_PERIOD_DEFAULT)
    192 		smw->smw_period = 32;
    193 	else if (smw->smw_period > SC1100_WDIVL_MAX) /* too big? */
    194 		return EINVAL;
    195 
    196 	GEODE_DPRINTF(("%s: period %u\n", __func__, smw->smw_period));
    197 
    198 	ticks = smw->smw_period * SC1100_WDCLK_HZ;
    199 
    200 	GEODE_DPRINTF(("%s: ticks0 %" PRIu32 "\n", __func__, ticks));
    201 
    202 	for (prescale = 0; ticks > UINT16_MAX; prescale++)
    203 		ticks /= 2;
    204 
    205 	GEODE_DPRINTF(("%s: ticks %" PRIu32 "\n", __func__, ticks));
    206 	GEODE_DPRINTF(("%s: prescale %d\n", __func__, prescale));
    207 
    208 	KASSERT(prescale <= SC1100_WDCNFG_WDPRES_MAX);
    209 	KASSERT(ticks <= UINT16_MAX);
    210 
    211 	s = splhigh();
    212 
    213 	sc->sc_prescale = (uint8_t)prescale;
    214 	sc->sc_countdown = (uint16_t)ticks;
    215 
    216 	geode_wdog_enable(sc);
    217 
    218 	geode_wdog_reset(sc);
    219 
    220 	splx(s);
    221 	return 0;
    222 }
    223 
    224 static int
    225 geode_wdog_match(device_t parent, struct cfdata *match, void *aux)
    226 {
    227 	return !attached;
    228 }
    229 
    230 static void
    231 geode_wdog_attach(device_t parent, device_t self, void *aux)
    232 {
    233 	struct geode_wdog_softc *sc = device_private(self);
    234 	uint8_t wdsts;
    235 
    236 	aprint_naive(": Watchdog Timer\n");
    237 	aprint_normal(": AMD Geode SC1100 Watchdog Timer\n");
    238 
    239 
    240 	/*
    241 	 * Hook up the watchdog timer.
    242 	 */
    243 	sc->sc_gcb_dev = device_private(parent);
    244 	sc->sc_smw.smw_name = device_xname(self);
    245 	sc->sc_smw.smw_cookie = sc;
    246 	sc->sc_smw.smw_setmode = geode_wdog_setmode;
    247 	sc->sc_smw.smw_tickle = geode_wdog_tickle;
    248 	sc->sc_smw.smw_period = 32;
    249 
    250 	/*
    251 	 * Determine cause of the last reset, and issue a warning if it
    252 	 * was due to watchdog expiry.
    253 	 */
    254 	wdsts = bus_space_read_1(sc->sc_gcb_dev->sc_iot, sc->sc_gcb_dev->sc_ioh,
    255 	    SC1100_GCB_WDSTS);
    256 
    257 	GEODE_DPRINTF(("%s: status %#02" PRIx8 "\n", device_xname(self),
    258 	    wdsts));
    259 
    260 	if (wdsts & SC1100_WDSTS_WDRST)
    261 		aprint_error(
    262 		    "%s: WARNING: LAST RESET DUE TO WATCHDOG EXPIRATION!\n",
    263 		    device_xname(self));
    264 
    265 	/* reset WDOVF by writing 1 to it */
    266 	bus_space_write_1(sc->sc_gcb_dev->sc_iot, sc->sc_gcb_dev->sc_ioh,
    267 	    SC1100_GCB_WDSTS, wdsts & SC1100_WDSTS_WDOVF);
    268 
    269 	if (sysmon_wdog_register(&sc->sc_smw) != 0)
    270 		aprint_error("%s: unable to register watchdog with sysmon\n",
    271 		    device_xname(self));
    272 
    273 	/* cancel any pending countdown */
    274 	geode_wdog_disable(sc);
    275 
    276 	attached = 1;
    277 }
    278 
    279 static int
    280 geode_wdog_detach(device_t self, int flags)
    281 {
    282 	int rc;
    283 	struct geode_wdog_softc *sc = device_private(self);
    284 
    285 	if ((rc = sysmon_wdog_unregister(&sc->sc_smw)) != 0) {
    286 		if (rc == ERESTART)
    287 			rc = EINTR;
    288 		return rc;
    289 	}
    290 
    291 	/* cancel any pending countdown */
    292 	geode_wdog_disable(sc);
    293 
    294 	attached = 0;
    295 
    296 	return 0;
    297 }
    298 
    299 CFATTACH_DECL(geodewdog, sizeof(struct geode_wdog_softc),
    300 	      geode_wdog_match, geode_wdog_attach, geode_wdog_detach, NULL);
    301