Home | History | Annotate | Line # | Download | only in dev
wdog.c revision 1.1
      1  1.1  msaitoh #include <sys/param.h>
      2  1.1  msaitoh #include <sys/buf.h>
      3  1.1  msaitoh #include <sys/systm.h>
      4  1.1  msaitoh #include <sys/kernel.h>
      5  1.1  msaitoh #include <sys/uio.h>
      6  1.1  msaitoh #include <sys/device.h>
      7  1.1  msaitoh #include <sys/fcntl.h>
      8  1.1  msaitoh #include <sys/ioctl.h>
      9  1.1  msaitoh #include <sys/malloc.h>
     10  1.1  msaitoh #include <sys/proc.h>
     11  1.1  msaitoh #include <sys/syslog.h>
     12  1.1  msaitoh 
     13  1.1  msaitoh #include <machine/cpu.h>
     14  1.1  msaitoh #include <machine/conf.h>
     15  1.1  msaitoh #include <sh3/shbvar.h>
     16  1.1  msaitoh #include <sh3/wdtreg.h>
     17  1.1  msaitoh #include <sh3/wdogvar.h>
     18  1.1  msaitoh 
     19  1.1  msaitoh struct wdog_softc {
     20  1.1  msaitoh 	struct device sc_dev;		/* generic device structures */
     21  1.1  msaitoh 	unsigned int iobase;
     22  1.1  msaitoh 	int flags;
     23  1.1  msaitoh };
     24  1.1  msaitoh 
     25  1.1  msaitoh static int wdogmatch __P((struct device *, struct cfdata *, void *));
     26  1.1  msaitoh static void wdogattach __P((struct device *, struct device *, void *));
     27  1.1  msaitoh 
     28  1.1  msaitoh struct cfattach wdog_ca = {
     29  1.1  msaitoh 	sizeof(struct wdog_softc), wdogmatch, wdogattach
     30  1.1  msaitoh };
     31  1.1  msaitoh 
     32  1.1  msaitoh extern struct cfdriver wdog_cd;
     33  1.1  msaitoh 
     34  1.1  msaitoh void
     35  1.1  msaitoh wdog_wr_cnt(x)
     36  1.1  msaitoh 	unsigned char x;
     37  1.1  msaitoh {
     38  1.1  msaitoh 
     39  1.1  msaitoh 	SHREG_WTCNT_W = WTCNT_W_M | (unsigned short) x;
     40  1.1  msaitoh }
     41  1.1  msaitoh 
     42  1.1  msaitoh void
     43  1.1  msaitoh wdog_wr_csr(x)
     44  1.1  msaitoh 	unsigned char x;
     45  1.1  msaitoh {
     46  1.1  msaitoh 
     47  1.1  msaitoh 	SHREG_WTCSR_W = WTCSR_W_M | (unsigned short) x;
     48  1.1  msaitoh }
     49  1.1  msaitoh 
     50  1.1  msaitoh static int
     51  1.1  msaitoh wdogmatch(parent, cfp, aux)
     52  1.1  msaitoh 	struct device *parent;
     53  1.1  msaitoh 	struct cfdata *cfp;
     54  1.1  msaitoh 	void *aux;
     55  1.1  msaitoh {
     56  1.1  msaitoh 	struct shb_attach_args *sa = aux;
     57  1.1  msaitoh 
     58  1.1  msaitoh 	if (strcmp(cfp->cf_driver->cd_name, "wdog"))
     59  1.1  msaitoh 		return 0;
     60  1.1  msaitoh 
     61  1.1  msaitoh 	sa->ia_iosize = 4;	/* XXX */
     62  1.1  msaitoh 
     63  1.1  msaitoh 	return (1);
     64  1.1  msaitoh }
     65  1.1  msaitoh 
     66  1.1  msaitoh /*
     67  1.1  msaitoh  *  functions for probeing.
     68  1.1  msaitoh  */
     69  1.1  msaitoh /* ARGSUSED */
     70  1.1  msaitoh static void
     71  1.1  msaitoh wdogattach(parent, self, aux)
     72  1.1  msaitoh 	struct device	*parent, *self;
     73  1.1  msaitoh 	void		*aux;
     74  1.1  msaitoh {
     75  1.1  msaitoh 	struct wdog_softc *sc = (struct wdog_softc *)self;
     76  1.1  msaitoh 	struct shb_attach_args *sa = aux;
     77  1.1  msaitoh 
     78  1.1  msaitoh 	sc->iobase = sa->ia_iobase;
     79  1.1  msaitoh 	sc->flags = 0;
     80  1.1  msaitoh 
     81  1.1  msaitoh 	printf("\nwdog0: internal watchdog timer\n");
     82  1.1  msaitoh }
     83  1.1  msaitoh 
     84  1.1  msaitoh /*ARGSUSED*/
     85  1.1  msaitoh int
     86  1.1  msaitoh wdogopen(dev, flag, mode, p)
     87  1.1  msaitoh 	dev_t dev;
     88  1.1  msaitoh 	int flag, mode;
     89  1.1  msaitoh 	struct proc *p;
     90  1.1  msaitoh {
     91  1.1  msaitoh 	struct wdog_softc *sc = wdog_cd.cd_devs[0]; /* XXX */
     92  1.1  msaitoh 
     93  1.1  msaitoh 	if (minor(dev) != 0)
     94  1.1  msaitoh 		return (ENXIO);
     95  1.1  msaitoh 	if (sc->flags & WDOGF_OPEN)
     96  1.1  msaitoh 		return (EBUSY);
     97  1.1  msaitoh 	sc->flags |= WDOGF_OPEN;
     98  1.1  msaitoh 	return (0);
     99  1.1  msaitoh }
    100  1.1  msaitoh 
    101  1.1  msaitoh /*ARGSUSED*/
    102  1.1  msaitoh int
    103  1.1  msaitoh wdogclose(dev, flag, mode, p)
    104  1.1  msaitoh 	dev_t dev;
    105  1.1  msaitoh 	int flag, mode;
    106  1.1  msaitoh 	struct proc *p;
    107  1.1  msaitoh {
    108  1.1  msaitoh 	struct wdog_softc *sc = wdog_cd.cd_devs[0]; /* XXX */
    109  1.1  msaitoh 
    110  1.1  msaitoh 	if (sc->flags & WDOGF_OPEN)
    111  1.1  msaitoh 		sc->flags = 0;
    112  1.1  msaitoh 
    113  1.1  msaitoh 	return (0);
    114  1.1  msaitoh }
    115  1.1  msaitoh 
    116  1.1  msaitoh extern unsigned int maxwdog;
    117  1.1  msaitoh 
    118  1.1  msaitoh /*ARGSUSED*/
    119  1.1  msaitoh int
    120  1.1  msaitoh wdogioctl (dev, cmd, data, flag, p)
    121  1.1  msaitoh 	dev_t dev;
    122  1.1  msaitoh 	u_long cmd;
    123  1.1  msaitoh 	caddr_t data;
    124  1.1  msaitoh 	int flag;
    125  1.1  msaitoh 	struct proc *p;
    126  1.1  msaitoh {
    127  1.1  msaitoh 	int error = 0;
    128  1.1  msaitoh 	int request;
    129  1.1  msaitoh 
    130  1.1  msaitoh 	switch (cmd) {
    131  1.1  msaitoh 	case SIORESETWDOG:
    132  1.1  msaitoh 		wdog_wr_cnt(0);		/* reset to zero */
    133  1.1  msaitoh 		break;
    134  1.1  msaitoh 	case SIOSTARTWDOG:
    135  1.1  msaitoh 		wdog_wr_csr(WTCSR_WT | WTCSR_CKS_4096);
    136  1.1  msaitoh 		wdog_wr_cnt(0);		/* reset to zero */
    137  1.1  msaitoh 		wdog_wr_csr(SHREG_WTCSR_R | WTCSR_TME);	/* start!!! */
    138  1.1  msaitoh 		break;
    139  1.1  msaitoh 	case SIOSTOPWDOG:
    140  1.1  msaitoh 		wdog_wr_csr(SHREG_WTCSR_R & ~WTCSR_TME); /* stop */
    141  1.1  msaitoh 		log(LOG_SYSTEM | LOG_DEBUG, "wdog: maxwdog = %u\n", maxwdog);
    142  1.1  msaitoh 		break;
    143  1.1  msaitoh 	case SIOSETWDOG:
    144  1.1  msaitoh 		request = *(int *)data;
    145  1.1  msaitoh 
    146  1.1  msaitoh 		if (request > 2) {
    147  1.1  msaitoh 			error = EINVAL;
    148  1.1  msaitoh 			break;
    149  1.1  msaitoh 		}
    150  1.1  msaitoh 		break;
    151  1.1  msaitoh 	default:
    152  1.1  msaitoh 		error = EINVAL;
    153  1.1  msaitoh 		break;
    154  1.1  msaitoh 	}
    155  1.1  msaitoh 
    156  1.1  msaitoh 	return (error);
    157  1.1  msaitoh }
    158