Home | History | Annotate | Line # | Download | only in atheros
wdog.c revision 1.1
      1 /* $NetBSD: wdog.c,v 1.1 2006/06/08 06:15:59 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 AR5312.
     79  */
     80 
     81 #include <sys/cdefs.h>
     82 __KERNEL_RCSID(0, "$NetBSD: wdog.c,v 1.1 2006/06/08 06:15:59 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/ar531xreg.h>
     92 #include <mips/atheros/include/ar531xvar.h>
     93 
     94 #include <dev/sysmon/sysmonvar.h>
     95 
     96 #ifndef	WDOG_DEFAULT_PERIOD
     97 #define	WDOG_DEFAULT_PERIOD	5
     98 #endif
     99 
    100 static int wdog_match(struct device *, struct cfdata *, void *);
    101 static void wdog_attach(struct device *, struct device *, void *);
    102 static int wdog_tickle(struct sysmon_wdog *);
    103 static int wdog_setmode(struct sysmon_wdog *);
    104 
    105 struct wdog_softc {
    106 	struct device		sc_dev;
    107 	struct sysmon_wdog	sc_smw;
    108 	int			sc_wdog_period;
    109 	int			sc_wdog_max;
    110 	uint32_t		sc_wdog_reload;
    111 	uint32_t		sc_mult;
    112 };
    113 
    114 CFATTACH_DECL(wdog, sizeof(struct wdog_softc),
    115     wdog_match, wdog_attach, NULL, NULL);
    116 
    117 static int
    118 wdog_match(struct device *parent, struct cfdata *cf, void *aux)
    119 {
    120 
    121 	return (1);
    122 }
    123 
    124 static void
    125 wdog_attach(struct device *parent, struct device *self, void *aux)
    126 {
    127 	struct wdog_softc *sc = (void *)self;
    128 
    129 	sc->sc_mult = curcpu()->ci_cpu_freq / 4;
    130 	sc->sc_wdog_period = WDOG_DEFAULT_PERIOD;
    131 	sc->sc_wdog_max = 0xffffffffU / sc->sc_mult;
    132 	sc->sc_wdog_reload = sc->sc_wdog_period * sc->sc_mult;
    133 	printf(": %d second period\n", sc->sc_wdog_period);
    134 
    135 	sc->sc_smw.smw_name = sc->sc_dev.dv_xname;
    136 	sc->sc_smw.smw_cookie = sc;
    137 	sc->sc_smw.smw_setmode = wdog_setmode;
    138 	sc->sc_smw.smw_tickle = wdog_tickle;
    139 	sc->sc_smw.smw_period = sc->sc_wdog_period;
    140 
    141 	if (sysmon_wdog_register(&sc->sc_smw) != 0)
    142 		printf("%s: unable to register with sysmon\n",
    143 		    sc->sc_dev.dv_xname);
    144 
    145 }
    146 
    147 static int
    148 wdog_tickle(struct sysmon_wdog *smw)
    149 {
    150 	struct wdog_softc *sc = smw->smw_cookie;
    151 
    152 	PUTSYSREG(AR531X_SYSREG_WDOG_TIMER, sc->sc_wdog_reload);
    153 	return (0);
    154 }
    155 
    156 static int
    157 wdog_setmode(struct sysmon_wdog *smw)
    158 {
    159 	struct wdog_softc *sc = smw->smw_cookie;
    160 
    161 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
    162 		PUTSYSREG(AR531X_SYSREG_WDOG_CTL, AR531X_WDOG_CTL_IGNORE);
    163 		PUTSYSREG(AR531X_SYSREG_WDOG_TIMER, 0);
    164 	} else {
    165 
    166 		if (smw->smw_period == WDOG_PERIOD_DEFAULT)
    167 			smw->smw_period = sc->sc_wdog_period;
    168 		else if (smw->smw_period != sc->sc_wdog_period) {
    169 			/*
    170 			 * we can't set watchdog periods in excess of
    171 			 * the processor maximum.
    172 			 */
    173 			if (smw->smw_period > sc->sc_wdog_max) {
    174 				return EOPNOTSUPP;
    175 			}
    176 			sc->sc_wdog_period = smw->smw_period;
    177 			sc->sc_wdog_reload = sc->sc_wdog_period * sc->sc_mult;
    178 		}
    179 
    180 		PUTSYSREG(AR531X_SYSREG_WDOG_TIMER, sc->sc_wdog_reload);
    181 		PUTSYSREG(AR531X_SYSREG_WDOG_CTL, AR531X_WDOG_CTL_RESET);
    182 	}
    183 
    184 	return (0);
    185 }
    186