1 /* $NetBSD: wdog.c,v 1.18 2014/07/25 08:10:34 dholland 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/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: wdog.c,v 1.18 2014/07/25 08:10:34 dholland Exp $"); 31 32 #include <sys/param.h> 33 #include <sys/buf.h> 34 #include <sys/systm.h> 35 #include <sys/kernel.h> 36 #include <sys/uio.h> 37 #include <sys/device.h> 38 #include <sys/fcntl.h> 39 #include <sys/ioctl.h> 40 #include <sys/malloc.h> 41 #include <sys/proc.h> 42 #include <sys/syslog.h> 43 #include <sys/conf.h> 44 45 #include <machine/cpu.h> 46 #include <machine/intr.h> 47 48 #include <sh3/frame.h> 49 #include <sh3/wdtreg.h> 50 #include <sh3/wdogvar.h> 51 #include <sh3/exception.h> 52 53 struct wdog_softc { 54 device_t sc_dev; 55 int flags; 56 }; 57 58 static int wdogmatch(device_t, cfdata_t, void *); 59 static void wdogattach(device_t, device_t, void *); 60 static int wdogintr(void *); 61 62 CFATTACH_DECL_NEW(wdog, sizeof(struct wdog_softc), 63 wdogmatch, wdogattach, NULL, NULL); 64 65 extern struct cfdriver wdog_cd; 66 67 dev_type_open(wdogopen); 68 dev_type_close(wdogclose); 69 dev_type_ioctl(wdogioctl); 70 71 const struct cdevsw wdog_cdevsw = { 72 .d_open = wdogopen, 73 .d_close = wdogclose, 74 .d_read = noread, 75 .d_write = nowrite, 76 .d_ioctl = wdogioctl, 77 .d_stop = nostop, 78 .d_tty = notty, 79 .d_poll = nopoll, 80 .d_mmap = nommap, 81 .d_kqfilter = nokqfilter, 82 .d_discard = nodiscard, 83 .d_flag = 0 84 }; 85 86 void 87 wdog_wr_cnt(unsigned char x) 88 { 89 90 SHREG_WTCNT_W = WTCNT_W_M | (unsigned short) x; 91 } 92 93 void 94 wdog_wr_csr(unsigned char x) 95 { 96 97 SHREG_WTCSR_W = WTCSR_W_M | (unsigned short) x; 98 } 99 100 static int 101 wdogmatch(device_t parent, cfdata_t cfp, void *aux) 102 { 103 104 if (strcmp(cfp->cf_name, "wdog")) 105 return (0); 106 107 return (1); 108 } 109 110 /* 111 * functions for probeing. 112 */ 113 /* ARGSUSED */ 114 static void 115 wdogattach(device_t parent, device_t self, void *aux) 116 { 117 struct wdog_softc *sc; 118 119 sc = device_private(self); 120 sc->sc_dev = self; 121 122 aprint_naive("\n"); 123 aprint_normal(": internal watchdog timer\n"); 124 125 wdog_wr_csr(WTCSR_WT | WTCSR_CKS_4096); /* default to wt mode */ 126 127 intc_intr_establish(SH_INTEVT_WDT_ITI, IST_LEVEL, IPL_SOFTCLOCK, 128 wdogintr, 0); 129 } 130 131 /*ARGSUSED*/ 132 int 133 wdogopen(dev_t dev, int flag, int mode, struct lwp *l) 134 { 135 struct wdog_softc *sc; 136 137 sc = device_lookup_private(&wdog_cd, minor(dev)); 138 if (sc == NULL) 139 return (ENXIO); 140 141 if (sc->flags & WDOGF_OPEN) 142 return (EBUSY); 143 sc->flags |= WDOGF_OPEN; 144 return (0); 145 } 146 147 /*ARGSUSED*/ 148 int 149 wdogclose(dev_t dev, int flag, int mode, struct lwp *l) 150 { 151 struct wdog_softc *sc; 152 153 sc = device_lookup_private(&wdog_cd, minor(dev)); 154 155 if (sc->flags & WDOGF_OPEN) 156 sc->flags = 0; 157 158 return (0); 159 } 160 161 extern unsigned int maxwdog; 162 163 /*ARGSUSED*/ 164 int 165 wdogioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 166 { 167 int error = 0; 168 int request; 169 170 switch (cmd) { 171 case SIOWDOGSETMODE: 172 request = *(int *)data; 173 174 switch (request) { 175 case WDOGM_RESET: 176 wdog_wr_csr(SHREG_WTCSR_R | WTCSR_WT); 177 break; 178 case WDOGM_INTR: 179 wdog_wr_csr(SHREG_WTCSR_R & ~WTCSR_WT); 180 break; 181 default: 182 error = EINVAL; 183 break; 184 } 185 break; 186 case SIORESETWDOG: 187 wdog_wr_cnt(0); /* reset to zero */ 188 break; 189 case SIOSTARTWDOG: 190 wdog_wr_csr(WTCSR_WT | WTCSR_CKS_4096); 191 wdog_wr_cnt(0); /* reset to zero */ 192 wdog_wr_csr(SHREG_WTCSR_R | WTCSR_TME); /* start!!! */ 193 break; 194 case SIOSTOPWDOG: 195 wdog_wr_csr(SHREG_WTCSR_R & ~WTCSR_TME); /* stop */ 196 break; 197 case SIOSETWDOG: 198 request = *(int *)data; 199 200 if (request > 2) { 201 error = EINVAL; 202 break; 203 } 204 break; 205 default: 206 error = EINVAL; 207 break; 208 } 209 210 return (error); 211 } 212 213 int 214 wdogintr(void *arg) 215 { 216 struct trapframe *frame = arg; 217 218 wdog_wr_csr(SHREG_WTCSR_R & ~WTCSR_IOVF); /* clear overflow bit */ 219 wdog_wr_cnt(0); /* reset to zero */ 220 printf("wdog trapped: spc = %x\n", frame->tf_spc); 221 222 return (0); 223 } 224