Home | History | Annotate | Line # | Download | only in atheros
wdog.c revision 1.3
      1  1.3  gdamore /* $NetBSD: wdog.c,v 1.3 2006/08/29 02:35:44 gdamore Exp $ */
      2  1.1  gdamore /*-
      3  1.1  gdamore  * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
      4  1.1  gdamore  * Copyright (c) 2006 Garrett D'Amore.
      5  1.1  gdamore  * All rights reserved.
      6  1.1  gdamore  *
      7  1.1  gdamore  * Portions of this code were written by Garrett D'Amore for the
      8  1.1  gdamore  * Champaign-Urbana Community Wireless Network Project.
      9  1.1  gdamore  *
     10  1.1  gdamore  * Redistribution and use in source and binary forms, with or
     11  1.1  gdamore  * without modification, are permitted provided that the following
     12  1.1  gdamore  * conditions are met:
     13  1.1  gdamore  * 1. Redistributions of source code must retain the above copyright
     14  1.1  gdamore  *    notice, this list of conditions and the following disclaimer.
     15  1.1  gdamore  * 2. Redistributions in binary form must reproduce the above
     16  1.1  gdamore  *    copyright notice, this list of conditions and the following
     17  1.1  gdamore  *    disclaimer in the documentation and/or other materials provided
     18  1.1  gdamore  *    with the distribution.
     19  1.1  gdamore  * 3. All advertising materials mentioning features or use of this
     20  1.1  gdamore  *    software must display the following acknowledgements:
     21  1.1  gdamore  *      This product includes software developed by the Urbana-Champaign
     22  1.1  gdamore  *      Independent Media Center.
     23  1.1  gdamore  *	This product includes software developed by Garrett D'Amore.
     24  1.1  gdamore  * 4. Urbana-Champaign Independent Media Center's name and Garrett
     25  1.1  gdamore  *    D'Amore's name may not be used to endorse or promote products
     26  1.1  gdamore  *    derived from this software without specific prior written permission.
     27  1.1  gdamore  *
     28  1.1  gdamore  * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
     29  1.1  gdamore  * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
     30  1.1  gdamore  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     31  1.1  gdamore  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     32  1.1  gdamore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
     33  1.1  gdamore  * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
     34  1.1  gdamore  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     35  1.1  gdamore  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     36  1.1  gdamore  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     37  1.1  gdamore  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     38  1.1  gdamore  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     39  1.1  gdamore  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     40  1.1  gdamore  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     41  1.1  gdamore  */
     42  1.1  gdamore /*
     43  1.1  gdamore  * Copyright (c) 2002 Wasabi Systems, Inc.
     44  1.1  gdamore  * All rights reserved.
     45  1.1  gdamore  *
     46  1.1  gdamore  * Written by Jason R. Thorpe and Simon Burge for Wasabi Systems, Inc.
     47  1.1  gdamore  *
     48  1.1  gdamore  * Redistribution and use in source and binary forms, with or without
     49  1.1  gdamore  * modification, are permitted provided that the following conditions
     50  1.1  gdamore  * are met:
     51  1.1  gdamore  * 1. Redistributions of source code must retain the above copyright
     52  1.1  gdamore  *    notice, this list of conditions and the following disclaimer.
     53  1.1  gdamore  * 2. Redistributions in binary form must reproduce the above copyright
     54  1.1  gdamore  *    notice, this list of conditions and the following disclaimer in the
     55  1.1  gdamore  *    documentation and/or other materials provided with the distribution.
     56  1.1  gdamore  * 3. All advertising materials mentioning features or use of this software
     57  1.1  gdamore  *    must display the following acknowledgement:
     58  1.1  gdamore  *	This product includes software developed for the NetBSD Project by
     59  1.1  gdamore  *	Wasabi Systems, Inc.
     60  1.1  gdamore  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     61  1.1  gdamore  *    or promote products derived from this software without specific prior
     62  1.1  gdamore  *    written permission.
     63  1.1  gdamore  *
     64  1.1  gdamore  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     65  1.1  gdamore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     66  1.1  gdamore  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     67  1.1  gdamore  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     68  1.1  gdamore  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     69  1.1  gdamore  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     70  1.1  gdamore  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     71  1.1  gdamore  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     72  1.1  gdamore  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     73  1.1  gdamore  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     74  1.1  gdamore  * POSSIBILITY OF SUCH DAMAGE.
     75  1.1  gdamore  */
     76  1.1  gdamore 
     77  1.1  gdamore /*
     78  1.3  gdamore  * Watchdog timer support for the AR531x.
     79  1.1  gdamore  */
     80  1.1  gdamore 
     81  1.1  gdamore #include <sys/cdefs.h>
     82  1.3  gdamore __KERNEL_RCSID(0, "$NetBSD: wdog.c,v 1.3 2006/08/29 02:35:44 gdamore Exp $");
     83  1.1  gdamore 
     84  1.1  gdamore #include <sys/param.h>
     85  1.1  gdamore #include <sys/systm.h>
     86  1.1  gdamore #include <sys/device.h>
     87  1.1  gdamore #include <sys/wdog.h>
     88  1.1  gdamore 
     89  1.1  gdamore #include <machine/cpu.h>
     90  1.1  gdamore 
     91  1.1  gdamore #include <mips/atheros/include/ar531xvar.h>
     92  1.1  gdamore 
     93  1.1  gdamore #include <dev/sysmon/sysmonvar.h>
     94  1.1  gdamore 
     95  1.1  gdamore #ifndef	WDOG_DEFAULT_PERIOD
     96  1.1  gdamore #define	WDOG_DEFAULT_PERIOD	5
     97  1.1  gdamore #endif
     98  1.1  gdamore 
     99  1.1  gdamore static int wdog_match(struct device *, struct cfdata *, void *);
    100  1.1  gdamore static void wdog_attach(struct device *, struct device *, void *);
    101  1.1  gdamore static int wdog_tickle(struct sysmon_wdog *);
    102  1.1  gdamore static int wdog_setmode(struct sysmon_wdog *);
    103  1.1  gdamore 
    104  1.1  gdamore struct wdog_softc {
    105  1.1  gdamore 	struct device		sc_dev;
    106  1.1  gdamore 	struct sysmon_wdog	sc_smw;
    107  1.1  gdamore 	int			sc_wdog_period;
    108  1.1  gdamore 	int			sc_wdog_max;
    109  1.1  gdamore 	uint32_t		sc_wdog_reload;
    110  1.1  gdamore 	uint32_t		sc_mult;
    111  1.1  gdamore };
    112  1.1  gdamore 
    113  1.1  gdamore CFATTACH_DECL(wdog, sizeof(struct wdog_softc),
    114  1.1  gdamore     wdog_match, wdog_attach, NULL, NULL);
    115  1.1  gdamore 
    116  1.1  gdamore static int
    117  1.1  gdamore wdog_match(struct device *parent, struct cfdata *cf, void *aux)
    118  1.1  gdamore {
    119  1.1  gdamore 
    120  1.1  gdamore 	return (1);
    121  1.1  gdamore }
    122  1.1  gdamore 
    123  1.1  gdamore static void
    124  1.1  gdamore wdog_attach(struct device *parent, struct device *self, void *aux)
    125  1.1  gdamore {
    126  1.1  gdamore 	struct wdog_softc *sc = (void *)self;
    127  1.1  gdamore 
    128  1.2  gdamore 	sc->sc_mult = ar531x_sys_freq();
    129  1.1  gdamore 	sc->sc_wdog_period = WDOG_DEFAULT_PERIOD;
    130  1.1  gdamore 	sc->sc_wdog_max = 0xffffffffU / sc->sc_mult;
    131  1.1  gdamore 	sc->sc_wdog_reload = sc->sc_wdog_period * sc->sc_mult;
    132  1.1  gdamore 	printf(": %d second period\n", sc->sc_wdog_period);
    133  1.1  gdamore 
    134  1.1  gdamore 	sc->sc_smw.smw_name = sc->sc_dev.dv_xname;
    135  1.1  gdamore 	sc->sc_smw.smw_cookie = sc;
    136  1.1  gdamore 	sc->sc_smw.smw_setmode = wdog_setmode;
    137  1.1  gdamore 	sc->sc_smw.smw_tickle = wdog_tickle;
    138  1.1  gdamore 	sc->sc_smw.smw_period = sc->sc_wdog_period;
    139  1.1  gdamore 
    140  1.1  gdamore 	if (sysmon_wdog_register(&sc->sc_smw) != 0)
    141  1.1  gdamore 		printf("%s: unable to register with sysmon\n",
    142  1.1  gdamore 		    sc->sc_dev.dv_xname);
    143  1.1  gdamore }
    144  1.1  gdamore 
    145  1.1  gdamore static int
    146  1.1  gdamore wdog_tickle(struct sysmon_wdog *smw)
    147  1.1  gdamore {
    148  1.1  gdamore 	struct wdog_softc *sc = smw->smw_cookie;
    149  1.1  gdamore 
    150  1.2  gdamore 	ar531x_wdog(sc->sc_wdog_reload);
    151  1.1  gdamore 	return (0);
    152  1.1  gdamore }
    153  1.1  gdamore 
    154  1.1  gdamore static int
    155  1.1  gdamore wdog_setmode(struct sysmon_wdog *smw)
    156  1.1  gdamore {
    157  1.1  gdamore 	struct wdog_softc *sc = smw->smw_cookie;
    158  1.1  gdamore 
    159  1.1  gdamore 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
    160  1.2  gdamore 		ar531x_wdog(0);
    161  1.1  gdamore 	} else {
    162  1.1  gdamore 
    163  1.1  gdamore 		if (smw->smw_period == WDOG_PERIOD_DEFAULT)
    164  1.1  gdamore 			smw->smw_period = sc->sc_wdog_period;
    165  1.1  gdamore 		else if (smw->smw_period != sc->sc_wdog_period) {
    166  1.1  gdamore 			/*
    167  1.1  gdamore 			 * we can't set watchdog periods in excess of
    168  1.1  gdamore 			 * the processor maximum.
    169  1.1  gdamore 			 */
    170  1.1  gdamore 			if (smw->smw_period > sc->sc_wdog_max) {
    171  1.1  gdamore 				return EOPNOTSUPP;
    172  1.1  gdamore 			}
    173  1.1  gdamore 			sc->sc_wdog_period = smw->smw_period;
    174  1.1  gdamore 			sc->sc_wdog_reload = sc->sc_wdog_period * sc->sc_mult;
    175  1.1  gdamore 		}
    176  1.1  gdamore 
    177  1.2  gdamore 		ar531x_wdog(sc->sc_wdog_reload);
    178  1.1  gdamore 	}
    179  1.1  gdamore 
    180  1.1  gdamore 	return (0);
    181  1.1  gdamore }
    182