wdog.c revision 1.18 1 1.18 dholland /* $NetBSD: wdog.c,v 1.18 2014/07/25 08:10:34 dholland 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.18 dholland __KERNEL_RCSID(0, "$NetBSD: wdog.c,v 1.18 2014/07/25 08:10:34 dholland 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.17 dholland .d_open = wdogopen,
73 1.17 dholland .d_close = wdogclose,
74 1.17 dholland .d_read = noread,
75 1.17 dholland .d_write = nowrite,
76 1.17 dholland .d_ioctl = wdogioctl,
77 1.17 dholland .d_stop = nostop,
78 1.17 dholland .d_tty = notty,
79 1.17 dholland .d_poll = nopoll,
80 1.17 dholland .d_mmap = nommap,
81 1.17 dholland .d_kqfilter = nokqfilter,
82 1.18 dholland .d_discard = nodiscard,
83 1.17 dholland .d_flag = 0
84 1.7 gehenna };
85 1.1 msaitoh
86 1.1 msaitoh void
87 1.5 uch wdog_wr_cnt(unsigned char x)
88 1.1 msaitoh {
89 1.1 msaitoh
90 1.1 msaitoh SHREG_WTCNT_W = WTCNT_W_M | (unsigned short) x;
91 1.1 msaitoh }
92 1.1 msaitoh
93 1.1 msaitoh void
94 1.5 uch wdog_wr_csr(unsigned char x)
95 1.1 msaitoh {
96 1.1 msaitoh
97 1.1 msaitoh SHREG_WTCSR_W = WTCSR_W_M | (unsigned short) x;
98 1.1 msaitoh }
99 1.1 msaitoh
100 1.1 msaitoh static int
101 1.16 uwe wdogmatch(device_t parent, cfdata_t cfp, void *aux)
102 1.1 msaitoh {
103 1.1 msaitoh
104 1.8 thorpej if (strcmp(cfp->cf_name, "wdog"))
105 1.6 uch return (0);
106 1.1 msaitoh
107 1.1 msaitoh return (1);
108 1.1 msaitoh }
109 1.1 msaitoh
110 1.1 msaitoh /*
111 1.1 msaitoh * functions for probeing.
112 1.1 msaitoh */
113 1.1 msaitoh /* ARGSUSED */
114 1.1 msaitoh static void
115 1.16 uwe wdogattach(device_t parent, device_t self, void *aux)
116 1.1 msaitoh {
117 1.16 uwe struct wdog_softc *sc;
118 1.1 msaitoh
119 1.16 uwe sc = device_private(self);
120 1.16 uwe sc->sc_dev = self;
121 1.16 uwe
122 1.16 uwe aprint_naive("\n");
123 1.16 uwe aprint_normal(": internal watchdog timer\n");
124 1.1 msaitoh
125 1.4 msaitoh wdog_wr_csr(WTCSR_WT | WTCSR_CKS_4096); /* default to wt mode */
126 1.4 msaitoh
127 1.6 uch intc_intr_establish(SH_INTEVT_WDT_ITI, IST_LEVEL, IPL_SOFTCLOCK,
128 1.6 uch wdogintr, 0);
129 1.1 msaitoh }
130 1.1 msaitoh
131 1.1 msaitoh /*ARGSUSED*/
132 1.1 msaitoh int
133 1.14 christos wdogopen(dev_t dev, int flag, int mode, struct lwp *l)
134 1.1 msaitoh {
135 1.16 uwe struct wdog_softc *sc;
136 1.1 msaitoh
137 1.16 uwe sc = device_lookup_private(&wdog_cd, minor(dev));
138 1.16 uwe if (sc == NULL)
139 1.1 msaitoh return (ENXIO);
140 1.16 uwe
141 1.1 msaitoh if (sc->flags & WDOGF_OPEN)
142 1.1 msaitoh return (EBUSY);
143 1.1 msaitoh sc->flags |= WDOGF_OPEN;
144 1.1 msaitoh return (0);
145 1.1 msaitoh }
146 1.1 msaitoh
147 1.1 msaitoh /*ARGSUSED*/
148 1.1 msaitoh int
149 1.14 christos wdogclose(dev_t dev, int flag, int mode, struct lwp *l)
150 1.1 msaitoh {
151 1.16 uwe struct wdog_softc *sc;
152 1.16 uwe
153 1.16 uwe sc = device_lookup_private(&wdog_cd, minor(dev));
154 1.1 msaitoh
155 1.1 msaitoh if (sc->flags & WDOGF_OPEN)
156 1.1 msaitoh sc->flags = 0;
157 1.1 msaitoh
158 1.1 msaitoh return (0);
159 1.1 msaitoh }
160 1.1 msaitoh
161 1.1 msaitoh extern unsigned int maxwdog;
162 1.1 msaitoh
163 1.1 msaitoh /*ARGSUSED*/
164 1.1 msaitoh int
165 1.15 christos wdogioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
166 1.1 msaitoh {
167 1.1 msaitoh int error = 0;
168 1.1 msaitoh int request;
169 1.1 msaitoh
170 1.1 msaitoh switch (cmd) {
171 1.4 msaitoh case SIOWDOGSETMODE:
172 1.4 msaitoh request = *(int *)data;
173 1.4 msaitoh
174 1.4 msaitoh switch (request) {
175 1.4 msaitoh case WDOGM_RESET:
176 1.4 msaitoh wdog_wr_csr(SHREG_WTCSR_R | WTCSR_WT);
177 1.4 msaitoh break;
178 1.4 msaitoh case WDOGM_INTR:
179 1.4 msaitoh wdog_wr_csr(SHREG_WTCSR_R & ~WTCSR_WT);
180 1.4 msaitoh break;
181 1.4 msaitoh default:
182 1.4 msaitoh error = EINVAL;
183 1.4 msaitoh break;
184 1.4 msaitoh }
185 1.4 msaitoh break;
186 1.1 msaitoh case SIORESETWDOG:
187 1.1 msaitoh wdog_wr_cnt(0); /* reset to zero */
188 1.1 msaitoh break;
189 1.1 msaitoh case SIOSTARTWDOG:
190 1.1 msaitoh wdog_wr_csr(WTCSR_WT | WTCSR_CKS_4096);
191 1.1 msaitoh wdog_wr_cnt(0); /* reset to zero */
192 1.1 msaitoh wdog_wr_csr(SHREG_WTCSR_R | WTCSR_TME); /* start!!! */
193 1.1 msaitoh break;
194 1.1 msaitoh case SIOSTOPWDOG:
195 1.1 msaitoh wdog_wr_csr(SHREG_WTCSR_R & ~WTCSR_TME); /* stop */
196 1.1 msaitoh break;
197 1.1 msaitoh case SIOSETWDOG:
198 1.1 msaitoh request = *(int *)data;
199 1.1 msaitoh
200 1.1 msaitoh if (request > 2) {
201 1.1 msaitoh error = EINVAL;
202 1.1 msaitoh break;
203 1.1 msaitoh }
204 1.1 msaitoh break;
205 1.1 msaitoh default:
206 1.1 msaitoh error = EINVAL;
207 1.1 msaitoh break;
208 1.1 msaitoh }
209 1.1 msaitoh
210 1.1 msaitoh return (error);
211 1.4 msaitoh }
212 1.4 msaitoh
213 1.4 msaitoh int
214 1.5 uch wdogintr(void *arg)
215 1.4 msaitoh {
216 1.4 msaitoh struct trapframe *frame = arg;
217 1.4 msaitoh
218 1.4 msaitoh wdog_wr_csr(SHREG_WTCSR_R & ~WTCSR_IOVF); /* clear overflow bit */
219 1.4 msaitoh wdog_wr_cnt(0); /* reset to zero */
220 1.4 msaitoh printf("wdog trapped: spc = %x\n", frame->tf_spc);
221 1.4 msaitoh
222 1.4 msaitoh return (0);
223 1.1 msaitoh }
224