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