Home | History | Annotate | Line # | Download | only in dev
wdog.c revision 1.14
      1  1.14  christos /* $NetBSD: wdog.c,v 1.14 2005/12/11 12:18:58 christos Exp $ */
      2   1.2   msaitoh 
      3   1.2   msaitoh /*-
      4   1.2   msaitoh  * Copyright (C) 2000 SAITOH Masanobu.  All rights reserved.
      5   1.2   msaitoh  *
      6   1.2   msaitoh  * Redistribution and use in source and binary forms, with or without
      7   1.2   msaitoh  * modification, are permitted provided that the following conditions
      8   1.2   msaitoh  * are met:
      9   1.2   msaitoh  * 1. Redistributions of source code must retain the above copyright
     10   1.2   msaitoh  *    notice, this list of conditions and the following disclaimer.
     11   1.2   msaitoh  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.2   msaitoh  *    notice, this list of conditions and the following disclaimer in the
     13   1.2   msaitoh  *    documentation and/or other materials provided with the distribution.
     14   1.2   msaitoh  * 3. The name of the author may not be used to endorse or promote products
     15   1.2   msaitoh  *    derived from this software without specific prior written permission.
     16   1.2   msaitoh  *
     17   1.2   msaitoh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18   1.2   msaitoh  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19   1.2   msaitoh  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20   1.2   msaitoh  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21   1.2   msaitoh  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22   1.2   msaitoh  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23   1.2   msaitoh  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24   1.2   msaitoh  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25   1.2   msaitoh  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26   1.2   msaitoh  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27   1.2   msaitoh  */
     28  1.13     lukem 
     29  1.13     lukem #include <sys/cdefs.h>
     30  1.14  christos __KERNEL_RCSID(0, "$NetBSD: wdog.c,v 1.14 2005/12/11 12:18:58 christos Exp $");
     31   1.2   msaitoh 
     32   1.1   msaitoh #include <sys/param.h>
     33   1.1   msaitoh #include <sys/buf.h>
     34   1.1   msaitoh #include <sys/systm.h>
     35   1.1   msaitoh #include <sys/kernel.h>
     36   1.1   msaitoh #include <sys/uio.h>
     37   1.1   msaitoh #include <sys/device.h>
     38   1.1   msaitoh #include <sys/fcntl.h>
     39   1.1   msaitoh #include <sys/ioctl.h>
     40   1.1   msaitoh #include <sys/malloc.h>
     41   1.1   msaitoh #include <sys/proc.h>
     42   1.1   msaitoh #include <sys/syslog.h>
     43   1.7   gehenna #include <sys/conf.h>
     44   1.1   msaitoh 
     45   1.1   msaitoh #include <machine/cpu.h>
     46   1.6       uch #include <machine/intr.h>
     47   1.6       uch 
     48   1.4   msaitoh #include <sh3/frame.h>
     49   1.1   msaitoh #include <sh3/wdtreg.h>
     50   1.1   msaitoh #include <sh3/wdogvar.h>
     51   1.6       uch #include <sh3/exception.h>
     52   1.1   msaitoh 
     53   1.1   msaitoh struct wdog_softc {
     54   1.1   msaitoh 	struct device sc_dev;		/* generic device structures */
     55   1.1   msaitoh 	int flags;
     56   1.1   msaitoh };
     57   1.1   msaitoh 
     58   1.5       uch static int wdogmatch(struct device *, struct cfdata *, void *);
     59   1.5       uch static void wdogattach(struct device *, struct device *, void *);
     60   1.5       uch static int wdogintr(void *);
     61   1.1   msaitoh 
     62  1.10   thorpej CFATTACH_DECL(wdog, sizeof(struct wdog_softc),
     63  1.11   thorpej     wdogmatch, wdogattach, NULL, NULL);
     64   1.1   msaitoh 
     65   1.1   msaitoh extern struct cfdriver wdog_cd;
     66   1.7   gehenna 
     67   1.7   gehenna dev_type_open(wdogopen);
     68   1.7   gehenna dev_type_close(wdogclose);
     69   1.7   gehenna dev_type_ioctl(wdogioctl);
     70   1.7   gehenna 
     71   1.7   gehenna const struct cdevsw wdog_cdevsw = {
     72   1.7   gehenna 	wdogopen, wdogclose, noread, nowrite, wdogioctl,
     73  1.12  jdolecek 	nostop, notty, nopoll, nommap, nokqfilter,
     74   1.7   gehenna };
     75   1.1   msaitoh 
     76   1.1   msaitoh void
     77   1.5       uch wdog_wr_cnt(unsigned char x)
     78   1.1   msaitoh {
     79   1.1   msaitoh 
     80   1.1   msaitoh 	SHREG_WTCNT_W = WTCNT_W_M | (unsigned short) x;
     81   1.1   msaitoh }
     82   1.1   msaitoh 
     83   1.1   msaitoh void
     84   1.5       uch wdog_wr_csr(unsigned char x)
     85   1.1   msaitoh {
     86   1.1   msaitoh 
     87   1.1   msaitoh 	SHREG_WTCSR_W = WTCSR_W_M | (unsigned short) x;
     88   1.1   msaitoh }
     89   1.1   msaitoh 
     90   1.1   msaitoh static int
     91   1.5       uch wdogmatch(struct device *parent, struct cfdata *cfp, void *aux)
     92   1.1   msaitoh {
     93   1.1   msaitoh 
     94   1.8   thorpej 	if (strcmp(cfp->cf_name, "wdog"))
     95   1.6       uch 		return (0);
     96   1.1   msaitoh 
     97   1.1   msaitoh 	return (1);
     98   1.1   msaitoh }
     99   1.1   msaitoh 
    100   1.1   msaitoh /*
    101   1.1   msaitoh  *  functions for probeing.
    102   1.1   msaitoh  */
    103   1.1   msaitoh /* ARGSUSED */
    104   1.1   msaitoh static void
    105   1.5       uch wdogattach(struct device *parent, struct device *self, void *aux)
    106   1.1   msaitoh {
    107   1.1   msaitoh 	struct wdog_softc *sc = (struct wdog_softc *)self;
    108   1.1   msaitoh 
    109   1.1   msaitoh 	sc->flags = 0;
    110   1.1   msaitoh 
    111   1.4   msaitoh 	wdog_wr_csr(WTCSR_WT | WTCSR_CKS_4096);	/* default to wt mode */
    112   1.4   msaitoh 
    113   1.6       uch 	intc_intr_establish(SH_INTEVT_WDT_ITI, IST_LEVEL, IPL_SOFTCLOCK,
    114   1.6       uch 	    wdogintr, 0);
    115   1.4   msaitoh 
    116   1.1   msaitoh 	printf("\nwdog0: internal watchdog timer\n");
    117   1.1   msaitoh }
    118   1.1   msaitoh 
    119   1.1   msaitoh /*ARGSUSED*/
    120   1.1   msaitoh int
    121  1.14  christos wdogopen(dev_t dev, int flag, int mode, struct lwp *l)
    122   1.1   msaitoh {
    123   1.1   msaitoh 	struct wdog_softc *sc = wdog_cd.cd_devs[0]; /* XXX */
    124   1.1   msaitoh 
    125   1.1   msaitoh 	if (minor(dev) != 0)
    126   1.1   msaitoh 		return (ENXIO);
    127   1.1   msaitoh 	if (sc->flags & WDOGF_OPEN)
    128   1.1   msaitoh 		return (EBUSY);
    129   1.1   msaitoh 	sc->flags |= WDOGF_OPEN;
    130   1.1   msaitoh 	return (0);
    131   1.1   msaitoh }
    132   1.1   msaitoh 
    133   1.1   msaitoh /*ARGSUSED*/
    134   1.1   msaitoh int
    135  1.14  christos wdogclose(dev_t dev, int flag, int mode, struct lwp *l)
    136   1.1   msaitoh {
    137   1.1   msaitoh 	struct wdog_softc *sc = wdog_cd.cd_devs[0]; /* XXX */
    138   1.1   msaitoh 
    139   1.1   msaitoh 	if (sc->flags & WDOGF_OPEN)
    140   1.1   msaitoh 		sc->flags = 0;
    141   1.1   msaitoh 
    142   1.1   msaitoh 	return (0);
    143   1.1   msaitoh }
    144   1.1   msaitoh 
    145   1.1   msaitoh extern unsigned int maxwdog;
    146   1.1   msaitoh 
    147   1.1   msaitoh /*ARGSUSED*/
    148   1.1   msaitoh int
    149  1.14  christos wdogioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
    150   1.1   msaitoh {
    151   1.1   msaitoh 	int error = 0;
    152   1.1   msaitoh 	int request;
    153   1.1   msaitoh 
    154   1.1   msaitoh 	switch (cmd) {
    155   1.4   msaitoh 	case SIOWDOGSETMODE:
    156   1.4   msaitoh 		request = *(int *)data;
    157   1.4   msaitoh 
    158   1.4   msaitoh 		switch (request) {
    159   1.4   msaitoh 		case WDOGM_RESET:
    160   1.4   msaitoh 			wdog_wr_csr(SHREG_WTCSR_R | WTCSR_WT);
    161   1.4   msaitoh 			break;
    162   1.4   msaitoh 		case WDOGM_INTR:
    163   1.4   msaitoh 			wdog_wr_csr(SHREG_WTCSR_R & ~WTCSR_WT);
    164   1.4   msaitoh 			break;
    165   1.4   msaitoh 		default:
    166   1.4   msaitoh 			error = EINVAL;
    167   1.4   msaitoh 			break;
    168   1.4   msaitoh 		}
    169   1.4   msaitoh 		break;
    170   1.1   msaitoh 	case SIORESETWDOG:
    171   1.1   msaitoh 		wdog_wr_cnt(0);		/* reset to zero */
    172   1.1   msaitoh 		break;
    173   1.1   msaitoh 	case SIOSTARTWDOG:
    174   1.1   msaitoh 		wdog_wr_csr(WTCSR_WT | WTCSR_CKS_4096);
    175   1.1   msaitoh 		wdog_wr_cnt(0);		/* reset to zero */
    176   1.1   msaitoh 		wdog_wr_csr(SHREG_WTCSR_R | WTCSR_TME);	/* start!!! */
    177   1.1   msaitoh 		break;
    178   1.1   msaitoh 	case SIOSTOPWDOG:
    179   1.1   msaitoh 		wdog_wr_csr(SHREG_WTCSR_R & ~WTCSR_TME); /* stop */
    180   1.1   msaitoh 		break;
    181   1.1   msaitoh 	case SIOSETWDOG:
    182   1.1   msaitoh 		request = *(int *)data;
    183   1.1   msaitoh 
    184   1.1   msaitoh 		if (request > 2) {
    185   1.1   msaitoh 			error = EINVAL;
    186   1.1   msaitoh 			break;
    187   1.1   msaitoh 		}
    188   1.1   msaitoh 		break;
    189   1.1   msaitoh 	default:
    190   1.1   msaitoh 		error = EINVAL;
    191   1.1   msaitoh 		break;
    192   1.1   msaitoh 	}
    193   1.1   msaitoh 
    194   1.1   msaitoh 	return (error);
    195   1.4   msaitoh }
    196   1.4   msaitoh 
    197   1.4   msaitoh int
    198   1.5       uch wdogintr(void *arg)
    199   1.4   msaitoh {
    200   1.4   msaitoh 	struct trapframe *frame = arg;
    201   1.4   msaitoh 
    202   1.4   msaitoh 	wdog_wr_csr(SHREG_WTCSR_R & ~WTCSR_IOVF); /* clear overflow bit */
    203   1.4   msaitoh 	wdog_wr_cnt(0);			/* reset to zero */
    204   1.4   msaitoh 	printf("wdog trapped: spc = %x\n", frame->tf_spc);
    205   1.4   msaitoh 
    206   1.4   msaitoh 	return (0);
    207   1.1   msaitoh }
    208