Home | History | Annotate | Line # | Download | only in dev
wdog.c revision 1.4.4.1
      1  1.4.4.1     fvdl /* $NetBSD: wdog.c,v 1.4.4.1 2001/10/10 11:56:30 fvdl 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.2  msaitoh 
     29      1.1  msaitoh #include <sys/param.h>
     30      1.1  msaitoh #include <sys/buf.h>
     31      1.1  msaitoh #include <sys/systm.h>
     32      1.1  msaitoh #include <sys/kernel.h>
     33      1.1  msaitoh #include <sys/uio.h>
     34      1.1  msaitoh #include <sys/device.h>
     35      1.1  msaitoh #include <sys/fcntl.h>
     36      1.1  msaitoh #include <sys/ioctl.h>
     37      1.1  msaitoh #include <sys/malloc.h>
     38      1.1  msaitoh #include <sys/proc.h>
     39      1.1  msaitoh #include <sys/syslog.h>
     40  1.4.4.1     fvdl #include <sys/vnode.h>
     41      1.1  msaitoh 
     42      1.1  msaitoh #include <machine/cpu.h>
     43      1.1  msaitoh #include <machine/conf.h>
     44      1.4  msaitoh #include <sh3/frame.h>
     45      1.1  msaitoh #include <sh3/shbvar.h>
     46      1.1  msaitoh #include <sh3/wdtreg.h>
     47      1.1  msaitoh #include <sh3/wdogvar.h>
     48      1.1  msaitoh 
     49      1.1  msaitoh struct wdog_softc {
     50      1.1  msaitoh 	struct device sc_dev;		/* generic device structures */
     51      1.1  msaitoh 	unsigned int iobase;
     52      1.1  msaitoh 	int flags;
     53      1.1  msaitoh };
     54      1.1  msaitoh 
     55      1.1  msaitoh static int wdogmatch __P((struct device *, struct cfdata *, void *));
     56      1.1  msaitoh static void wdogattach __P((struct device *, struct device *, void *));
     57      1.4  msaitoh static int wdogintr __P((void *));
     58      1.1  msaitoh 
     59      1.1  msaitoh struct cfattach wdog_ca = {
     60      1.1  msaitoh 	sizeof(struct wdog_softc), wdogmatch, wdogattach
     61      1.1  msaitoh };
     62      1.1  msaitoh 
     63      1.1  msaitoh extern struct cfdriver wdog_cd;
     64      1.1  msaitoh 
     65      1.1  msaitoh void
     66      1.1  msaitoh wdog_wr_cnt(x)
     67      1.1  msaitoh 	unsigned char x;
     68      1.1  msaitoh {
     69      1.1  msaitoh 
     70      1.1  msaitoh 	SHREG_WTCNT_W = WTCNT_W_M | (unsigned short) x;
     71      1.1  msaitoh }
     72      1.1  msaitoh 
     73      1.1  msaitoh void
     74      1.1  msaitoh wdog_wr_csr(x)
     75      1.1  msaitoh 	unsigned char x;
     76      1.1  msaitoh {
     77      1.1  msaitoh 
     78      1.1  msaitoh 	SHREG_WTCSR_W = WTCSR_W_M | (unsigned short) x;
     79      1.1  msaitoh }
     80      1.1  msaitoh 
     81      1.1  msaitoh static int
     82      1.1  msaitoh wdogmatch(parent, cfp, aux)
     83      1.1  msaitoh 	struct device *parent;
     84      1.1  msaitoh 	struct cfdata *cfp;
     85      1.1  msaitoh 	void *aux;
     86      1.1  msaitoh {
     87      1.1  msaitoh 	struct shb_attach_args *sa = aux;
     88      1.1  msaitoh 
     89      1.1  msaitoh 	if (strcmp(cfp->cf_driver->cd_name, "wdog"))
     90      1.1  msaitoh 		return 0;
     91      1.1  msaitoh 
     92      1.1  msaitoh 	sa->ia_iosize = 4;	/* XXX */
     93      1.1  msaitoh 
     94      1.1  msaitoh 	return (1);
     95      1.1  msaitoh }
     96      1.1  msaitoh 
     97      1.1  msaitoh /*
     98      1.1  msaitoh  *  functions for probeing.
     99      1.1  msaitoh  */
    100      1.1  msaitoh /* ARGSUSED */
    101      1.1  msaitoh static void
    102      1.1  msaitoh wdogattach(parent, self, aux)
    103      1.1  msaitoh 	struct device	*parent, *self;
    104      1.1  msaitoh 	void		*aux;
    105      1.1  msaitoh {
    106      1.1  msaitoh 	struct wdog_softc *sc = (struct wdog_softc *)self;
    107      1.1  msaitoh 	struct shb_attach_args *sa = aux;
    108      1.1  msaitoh 
    109      1.1  msaitoh 	sc->iobase = sa->ia_iobase;
    110      1.1  msaitoh 	sc->flags = 0;
    111      1.1  msaitoh 
    112      1.4  msaitoh 	wdog_wr_csr(WTCSR_WT | WTCSR_CKS_4096);	/* default to wt mode */
    113      1.4  msaitoh 
    114      1.4  msaitoh 	shb_intr_establish(WDOG_IRQ, IST_EDGE, IPL_SOFTCLOCK, 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.4.4.1     fvdl wdogopen(devvp, flag, mode, p)
    122  1.4.4.1     fvdl 	struct vnode *devvp;
    123      1.1  msaitoh 	int flag, mode;
    124      1.1  msaitoh 	struct proc *p;
    125      1.1  msaitoh {
    126  1.4.4.1     fvdl 	dev_t dev = vdev_rdev(devvp);
    127      1.1  msaitoh 	struct wdog_softc *sc = wdog_cd.cd_devs[0]; /* XXX */
    128      1.1  msaitoh 
    129      1.1  msaitoh 	if (minor(dev) != 0)
    130      1.1  msaitoh 		return (ENXIO);
    131      1.1  msaitoh 	if (sc->flags & WDOGF_OPEN)
    132      1.1  msaitoh 		return (EBUSY);
    133      1.1  msaitoh 	sc->flags |= WDOGF_OPEN;
    134      1.1  msaitoh 	return (0);
    135      1.1  msaitoh }
    136      1.1  msaitoh 
    137      1.1  msaitoh /*ARGSUSED*/
    138      1.1  msaitoh int
    139  1.4.4.1     fvdl wdogclose(devvp, flag, mode, p)
    140  1.4.4.1     fvdl 	struct vnode *devvp;
    141      1.1  msaitoh 	int flag, mode;
    142      1.1  msaitoh 	struct proc *p;
    143      1.1  msaitoh {
    144      1.1  msaitoh 	struct wdog_softc *sc = wdog_cd.cd_devs[0]; /* XXX */
    145      1.1  msaitoh 
    146      1.1  msaitoh 	if (sc->flags & WDOGF_OPEN)
    147      1.1  msaitoh 		sc->flags = 0;
    148      1.1  msaitoh 
    149      1.1  msaitoh 	return (0);
    150      1.1  msaitoh }
    151      1.1  msaitoh 
    152      1.1  msaitoh extern unsigned int maxwdog;
    153      1.1  msaitoh 
    154      1.1  msaitoh /*ARGSUSED*/
    155      1.1  msaitoh int
    156  1.4.4.1     fvdl wdogioctl(devvp, cmd, data, flag, p)
    157  1.4.4.1     fvdl 	struct vnode *devvp;
    158      1.1  msaitoh 	u_long cmd;
    159      1.1  msaitoh 	caddr_t data;
    160      1.1  msaitoh 	int flag;
    161      1.1  msaitoh 	struct proc *p;
    162      1.1  msaitoh {
    163      1.1  msaitoh 	int error = 0;
    164      1.1  msaitoh 	int request;
    165      1.1  msaitoh 
    166      1.1  msaitoh 	switch (cmd) {
    167      1.4  msaitoh 	case SIOWDOGSETMODE:
    168      1.4  msaitoh 		request = *(int *)data;
    169      1.4  msaitoh 
    170      1.4  msaitoh 		switch (request) {
    171      1.4  msaitoh 		case WDOGM_RESET:
    172      1.4  msaitoh 			wdog_wr_csr(SHREG_WTCSR_R | WTCSR_WT);
    173      1.4  msaitoh 			break;
    174      1.4  msaitoh 		case WDOGM_INTR:
    175      1.4  msaitoh 			wdog_wr_csr(SHREG_WTCSR_R & ~WTCSR_WT);
    176      1.4  msaitoh 			break;
    177      1.4  msaitoh 		default:
    178      1.4  msaitoh 			error = EINVAL;
    179      1.4  msaitoh 			break;
    180      1.4  msaitoh 		}
    181      1.4  msaitoh 		break;
    182      1.1  msaitoh 	case SIORESETWDOG:
    183      1.1  msaitoh 		wdog_wr_cnt(0);		/* reset to zero */
    184      1.1  msaitoh 		break;
    185      1.1  msaitoh 	case SIOSTARTWDOG:
    186      1.1  msaitoh 		wdog_wr_csr(WTCSR_WT | WTCSR_CKS_4096);
    187      1.1  msaitoh 		wdog_wr_cnt(0);		/* reset to zero */
    188      1.1  msaitoh 		wdog_wr_csr(SHREG_WTCSR_R | WTCSR_TME);	/* start!!! */
    189      1.1  msaitoh 		break;
    190      1.1  msaitoh 	case SIOSTOPWDOG:
    191      1.1  msaitoh 		wdog_wr_csr(SHREG_WTCSR_R & ~WTCSR_TME); /* stop */
    192      1.1  msaitoh 		break;
    193      1.1  msaitoh 	case SIOSETWDOG:
    194      1.1  msaitoh 		request = *(int *)data;
    195      1.1  msaitoh 
    196      1.1  msaitoh 		if (request > 2) {
    197      1.1  msaitoh 			error = EINVAL;
    198      1.1  msaitoh 			break;
    199      1.1  msaitoh 		}
    200      1.1  msaitoh 		break;
    201      1.1  msaitoh 	default:
    202      1.1  msaitoh 		error = EINVAL;
    203      1.1  msaitoh 		break;
    204      1.1  msaitoh 	}
    205      1.1  msaitoh 
    206      1.1  msaitoh 	return (error);
    207      1.4  msaitoh }
    208      1.4  msaitoh 
    209      1.4  msaitoh int
    210      1.4  msaitoh wdogintr(arg)
    211      1.4  msaitoh 	void *arg;
    212      1.4  msaitoh {
    213      1.4  msaitoh 	struct trapframe *frame = arg;
    214      1.4  msaitoh 
    215      1.4  msaitoh 	wdog_wr_csr(SHREG_WTCSR_R & ~WTCSR_IOVF); /* clear overflow bit */
    216      1.4  msaitoh 	wdog_wr_cnt(0);			/* reset to zero */
    217      1.4  msaitoh 	printf("wdog trapped: spc = %x\n", frame->tf_spc);
    218      1.4  msaitoh 
    219      1.4  msaitoh 	return (0);
    220      1.1  msaitoh }
    221