Home | History | Annotate | Line # | Download | only in dev
wdog.c revision 1.16
      1  1.16       uwe /*	$NetBSD: wdog.c,v 1.16 2008/03/27 02:03:03 uwe 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.16       uwe __KERNEL_RCSID(0, "$NetBSD: wdog.c,v 1.16 2008/03/27 02:03:03 uwe 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.16       uwe 	device_t sc_dev;
     55   1.1   msaitoh 	int flags;
     56   1.1   msaitoh };
     57   1.1   msaitoh 
     58  1.16       uwe static int wdogmatch(device_t, cfdata_t, void *);
     59  1.16       uwe static void wdogattach(device_t, device_t, void *);
     60   1.5       uch static int wdogintr(void *);
     61   1.1   msaitoh 
     62  1.16       uwe CFATTACH_DECL_NEW(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.16       uwe wdogmatch(device_t parent, cfdata_t 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.16       uwe wdogattach(device_t parent, device_t self, void *aux)
    106   1.1   msaitoh {
    107  1.16       uwe 	struct wdog_softc *sc;
    108   1.1   msaitoh 
    109  1.16       uwe 	sc = device_private(self);
    110  1.16       uwe 	sc->sc_dev = self;
    111  1.16       uwe 
    112  1.16       uwe 	aprint_naive("\n");
    113  1.16       uwe 	aprint_normal(": internal watchdog timer\n");
    114   1.1   msaitoh 
    115   1.4   msaitoh 	wdog_wr_csr(WTCSR_WT | WTCSR_CKS_4096);	/* default to wt mode */
    116   1.4   msaitoh 
    117   1.6       uch 	intc_intr_establish(SH_INTEVT_WDT_ITI, IST_LEVEL, IPL_SOFTCLOCK,
    118   1.6       uch 	    wdogintr, 0);
    119   1.1   msaitoh }
    120   1.1   msaitoh 
    121   1.1   msaitoh /*ARGSUSED*/
    122   1.1   msaitoh int
    123  1.14  christos wdogopen(dev_t dev, int flag, int mode, struct lwp *l)
    124   1.1   msaitoh {
    125  1.16       uwe 	struct wdog_softc *sc;
    126   1.1   msaitoh 
    127  1.16       uwe 	sc = device_lookup_private(&wdog_cd, minor(dev));
    128  1.16       uwe 	if (sc == NULL)
    129   1.1   msaitoh 		return (ENXIO);
    130  1.16       uwe 
    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.14  christos wdogclose(dev_t dev, int flag, int mode, struct lwp *l)
    140   1.1   msaitoh {
    141  1.16       uwe 	struct wdog_softc *sc;
    142  1.16       uwe 
    143  1.16       uwe 	sc = device_lookup_private(&wdog_cd, minor(dev));
    144   1.1   msaitoh 
    145   1.1   msaitoh 	if (sc->flags & WDOGF_OPEN)
    146   1.1   msaitoh 		sc->flags = 0;
    147   1.1   msaitoh 
    148   1.1   msaitoh 	return (0);
    149   1.1   msaitoh }
    150   1.1   msaitoh 
    151   1.1   msaitoh extern unsigned int maxwdog;
    152   1.1   msaitoh 
    153   1.1   msaitoh /*ARGSUSED*/
    154   1.1   msaitoh int
    155  1.15  christos wdogioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    156   1.1   msaitoh {
    157   1.1   msaitoh 	int error = 0;
    158   1.1   msaitoh 	int request;
    159   1.1   msaitoh 
    160   1.1   msaitoh 	switch (cmd) {
    161   1.4   msaitoh 	case SIOWDOGSETMODE:
    162   1.4   msaitoh 		request = *(int *)data;
    163   1.4   msaitoh 
    164   1.4   msaitoh 		switch (request) {
    165   1.4   msaitoh 		case WDOGM_RESET:
    166   1.4   msaitoh 			wdog_wr_csr(SHREG_WTCSR_R | WTCSR_WT);
    167   1.4   msaitoh 			break;
    168   1.4   msaitoh 		case WDOGM_INTR:
    169   1.4   msaitoh 			wdog_wr_csr(SHREG_WTCSR_R & ~WTCSR_WT);
    170   1.4   msaitoh 			break;
    171   1.4   msaitoh 		default:
    172   1.4   msaitoh 			error = EINVAL;
    173   1.4   msaitoh 			break;
    174   1.4   msaitoh 		}
    175   1.4   msaitoh 		break;
    176   1.1   msaitoh 	case SIORESETWDOG:
    177   1.1   msaitoh 		wdog_wr_cnt(0);		/* reset to zero */
    178   1.1   msaitoh 		break;
    179   1.1   msaitoh 	case SIOSTARTWDOG:
    180   1.1   msaitoh 		wdog_wr_csr(WTCSR_WT | WTCSR_CKS_4096);
    181   1.1   msaitoh 		wdog_wr_cnt(0);		/* reset to zero */
    182   1.1   msaitoh 		wdog_wr_csr(SHREG_WTCSR_R | WTCSR_TME);	/* start!!! */
    183   1.1   msaitoh 		break;
    184   1.1   msaitoh 	case SIOSTOPWDOG:
    185   1.1   msaitoh 		wdog_wr_csr(SHREG_WTCSR_R & ~WTCSR_TME); /* stop */
    186   1.1   msaitoh 		break;
    187   1.1   msaitoh 	case SIOSETWDOG:
    188   1.1   msaitoh 		request = *(int *)data;
    189   1.1   msaitoh 
    190   1.1   msaitoh 		if (request > 2) {
    191   1.1   msaitoh 			error = EINVAL;
    192   1.1   msaitoh 			break;
    193   1.1   msaitoh 		}
    194   1.1   msaitoh 		break;
    195   1.1   msaitoh 	default:
    196   1.1   msaitoh 		error = EINVAL;
    197   1.1   msaitoh 		break;
    198   1.1   msaitoh 	}
    199   1.1   msaitoh 
    200   1.1   msaitoh 	return (error);
    201   1.4   msaitoh }
    202   1.4   msaitoh 
    203   1.4   msaitoh int
    204   1.5       uch wdogintr(void *arg)
    205   1.4   msaitoh {
    206   1.4   msaitoh 	struct trapframe *frame = arg;
    207   1.4   msaitoh 
    208   1.4   msaitoh 	wdog_wr_csr(SHREG_WTCSR_R & ~WTCSR_IOVF); /* clear overflow bit */
    209   1.4   msaitoh 	wdog_wr_cnt(0);			/* reset to zero */
    210   1.4   msaitoh 	printf("wdog trapped: spc = %x\n", frame->tf_spc);
    211   1.4   msaitoh 
    212   1.4   msaitoh 	return (0);
    213   1.1   msaitoh }
    214