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