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