wdog.c revision 1.14 1 /* $NetBSD: wdog.c,v 1.14 2005/12/11 12:18:58 christos 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.14 2005/12/11 12:18:58 christos 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 struct device sc_dev; /* generic device structures */
55 int flags;
56 };
57
58 static int wdogmatch(struct device *, struct cfdata *, void *);
59 static void wdogattach(struct device *, struct device *, void *);
60 static int wdogintr(void *);
61
62 CFATTACH_DECL(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 wdogopen, wdogclose, noread, nowrite, wdogioctl,
73 nostop, notty, nopoll, nommap, nokqfilter,
74 };
75
76 void
77 wdog_wr_cnt(unsigned char x)
78 {
79
80 SHREG_WTCNT_W = WTCNT_W_M | (unsigned short) x;
81 }
82
83 void
84 wdog_wr_csr(unsigned char x)
85 {
86
87 SHREG_WTCSR_W = WTCSR_W_M | (unsigned short) x;
88 }
89
90 static int
91 wdogmatch(struct device *parent, struct cfdata *cfp, void *aux)
92 {
93
94 if (strcmp(cfp->cf_name, "wdog"))
95 return (0);
96
97 return (1);
98 }
99
100 /*
101 * functions for probeing.
102 */
103 /* ARGSUSED */
104 static void
105 wdogattach(struct device *parent, struct device *self, void *aux)
106 {
107 struct wdog_softc *sc = (struct wdog_softc *)self;
108
109 sc->flags = 0;
110
111 wdog_wr_csr(WTCSR_WT | WTCSR_CKS_4096); /* default to wt mode */
112
113 intc_intr_establish(SH_INTEVT_WDT_ITI, IST_LEVEL, IPL_SOFTCLOCK,
114 wdogintr, 0);
115
116 printf("\nwdog0: internal watchdog timer\n");
117 }
118
119 /*ARGSUSED*/
120 int
121 wdogopen(dev_t dev, int flag, int mode, struct lwp *l)
122 {
123 struct wdog_softc *sc = wdog_cd.cd_devs[0]; /* XXX */
124
125 if (minor(dev) != 0)
126 return (ENXIO);
127 if (sc->flags & WDOGF_OPEN)
128 return (EBUSY);
129 sc->flags |= WDOGF_OPEN;
130 return (0);
131 }
132
133 /*ARGSUSED*/
134 int
135 wdogclose(dev_t dev, int flag, int mode, struct lwp *l)
136 {
137 struct wdog_softc *sc = wdog_cd.cd_devs[0]; /* XXX */
138
139 if (sc->flags & WDOGF_OPEN)
140 sc->flags = 0;
141
142 return (0);
143 }
144
145 extern unsigned int maxwdog;
146
147 /*ARGSUSED*/
148 int
149 wdogioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
150 {
151 int error = 0;
152 int request;
153
154 switch (cmd) {
155 case SIOWDOGSETMODE:
156 request = *(int *)data;
157
158 switch (request) {
159 case WDOGM_RESET:
160 wdog_wr_csr(SHREG_WTCSR_R | WTCSR_WT);
161 break;
162 case WDOGM_INTR:
163 wdog_wr_csr(SHREG_WTCSR_R & ~WTCSR_WT);
164 break;
165 default:
166 error = EINVAL;
167 break;
168 }
169 break;
170 case SIORESETWDOG:
171 wdog_wr_cnt(0); /* reset to zero */
172 break;
173 case SIOSTARTWDOG:
174 wdog_wr_csr(WTCSR_WT | WTCSR_CKS_4096);
175 wdog_wr_cnt(0); /* reset to zero */
176 wdog_wr_csr(SHREG_WTCSR_R | WTCSR_TME); /* start!!! */
177 break;
178 case SIOSTOPWDOG:
179 wdog_wr_csr(SHREG_WTCSR_R & ~WTCSR_TME); /* stop */
180 break;
181 case SIOSETWDOG:
182 request = *(int *)data;
183
184 if (request > 2) {
185 error = EINVAL;
186 break;
187 }
188 break;
189 default:
190 error = EINVAL;
191 break;
192 }
193
194 return (error);
195 }
196
197 int
198 wdogintr(void *arg)
199 {
200 struct trapframe *frame = arg;
201
202 wdog_wr_csr(SHREG_WTCSR_R & ~WTCSR_IOVF); /* clear overflow bit */
203 wdog_wr_cnt(0); /* reset to zero */
204 printf("wdog trapped: spc = %x\n", frame->tf_spc);
205
206 return (0);
207 }
208