pld_wdog.c revision 1.1 1 /* $NetBSD: pld_wdog.c,v 1.1 2003/07/17 20:54:41 petrov Exp $ */
2
3 /*-
4 * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the NetBSD
18 * Foundation, Inc. and its contributors.
19 * 4. Neither the name of The NetBSD Foundation nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/device.h>
40
41 #include <machine/autoconf.h>
42
43 #include <dev/ebus/ebusreg.h>
44 #include <dev/ebus/ebusvar.h>
45
46 #include <dev/sysmon/sysmonvar.h>
47
48 #define PLD_WDOG1_COUNTER 0x00
49 #define PLD_WDOG1_LIMIT 0x04
50 #define PLD_WDOG1_STATUS 0x08
51 #define PLD_WDOG2_COUNTER 0x10
52 #define PLD_WDOG2_LIMIT 0x14
53 #define PLD_WDOG2_STATUS 0x18
54 #define PLD_WDOG3_COUNTER 0x20
55 #define PLD_WDOG3_LIMIT 0x24
56 #define PLD_WDOG3_STATUS 0x28
57
58 #define PLD_WDOG_INTR_MASK 0x30
59 #define PLD_WDOG_STATUS 0x34
60
61 #define PLD_WDOG_PERIOD_DEFAULT 15 /* seconds */
62
63 /* #define PLD_WDOG_DEBUG 1 */
64
65 struct pldwdog_softc {
66 struct device sc_dev;
67
68 bus_space_tag_t sc_btag;
69 bus_space_handle_t sc_bh;
70
71 struct sysmon_wdog sc_smw;
72 int sc_wdog_period;
73 };
74
75 int pldwdog_match(struct device *, struct cfdata *, void *);
76 void pldwdog_attach(struct device *, struct device *, void *);
77
78 CFATTACH_DECL(pldwdog, sizeof(struct pldwdog_softc),
79 pldwdog_match, pldwdog_attach, NULL, NULL);
80
81 static void pldwdog_regs(struct pldwdog_softc *);
82
83 static int
84 pldwdog_tickle(struct sysmon_wdog *smw)
85 {
86 struct pldwdog_softc *sc = smw->smw_cookie;
87
88 #ifdef PLD_WDOG_DEBUG
89 printf("%s: pldwdog_tickle: mode %x, period %d\n",
90 sc->sc_dev.dv_xname, smw->smw_mode, smw->smw_period);
91 /* pldwdog_regs(sc); */
92 #endif
93
94 bus_space_write_2(sc->sc_btag, sc->sc_bh, PLD_WDOG2_LIMIT,
95 smw->smw_period * 10);
96 bus_space_write_1(sc->sc_btag, sc->sc_bh, PLD_WDOG_INTR_MASK, 5);
97
98 return 0;
99 }
100
101 static int
102 pldwdog_setmode(struct sysmon_wdog *smw)
103 {
104 struct pldwdog_softc *sc = smw->smw_cookie;
105
106 #ifdef PLD_WDOG_DEBUG
107 printf("%s:pldwdog_setmode: mode %x\n", sc->sc_dev.dv_xname, smw->smw_mode);
108 #endif
109
110 if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
111 bus_space_write_1(sc->sc_btag, sc->sc_bh, PLD_WDOG_INTR_MASK, 7);
112 } else {
113 if (smw->smw_period == WDOG_PERIOD_DEFAULT)
114 smw->smw_period = sc->sc_wdog_period;
115
116 pldwdog_tickle(smw);
117 }
118 return (0);
119 }
120
121 #if 0
122 static int pldwdog_intr(void);
123 static int
124 pldwdog_intr()
125 {
126
127 printf("pldwdog_intr:\n");
128
129 return 1;
130 }
131 #endif
132
133 int
134 pldwdog_match(parent, cf, aux)
135 struct device *parent;
136 struct cfdata *cf;
137 void *aux;
138 {
139 struct ebus_attach_args *ea = aux;
140
141 return (strcmp("watchdog", ea->ea_name) == 0);
142 }
143
144 #ifdef PLD_WDOG_DEBUG
145 static void
146 pldwdog_regs(sc)
147 struct pldwdog_softc *sc;
148 {
149
150 printf("%s: status 0x%02x, intr mask 0x%02x\n",
151 sc->sc_dev.dv_xname,
152 bus_space_read_1(sc->sc_btag, sc->sc_bh, PLD_WDOG_INTR_MASK),
153 bus_space_read_1(sc->sc_btag, sc->sc_bh, PLD_WDOG_STATUS));
154
155 printf("%s: wdog1: count 0x%04x, limit 0x%04x, status 0x%02x\n",
156 sc->sc_dev.dv_xname,
157 bus_space_read_2(sc->sc_btag, sc->sc_bh, PLD_WDOG1_COUNTER),
158 bus_space_read_2(sc->sc_btag, sc->sc_bh, PLD_WDOG1_LIMIT),
159 bus_space_read_1(sc->sc_btag, sc->sc_bh, PLD_WDOG1_STATUS));
160
161 printf("%s: wdog2: count 0x%04x, limit 0x%04x, status 0x%02x\n",
162 sc->sc_dev.dv_xname,
163 bus_space_read_2(sc->sc_btag, sc->sc_bh, PLD_WDOG2_COUNTER),
164 bus_space_read_2(sc->sc_btag, sc->sc_bh, PLD_WDOG2_LIMIT),
165 bus_space_read_1(sc->sc_btag, sc->sc_bh, PLD_WDOG2_STATUS));
166
167 printf("%s: wdog3: count 0x%04x, limit 0x%04x, status 0x%02x\n",
168 sc->sc_dev.dv_xname,
169 bus_space_read_2(sc->sc_btag, sc->sc_bh, PLD_WDOG3_COUNTER),
170 bus_space_read_2(sc->sc_btag, sc->sc_bh, PLD_WDOG3_LIMIT),
171 bus_space_read_1(sc->sc_btag, sc->sc_bh, PLD_WDOG3_STATUS));
172 }
173 #endif
174
175 void
176 pldwdog_attach(parent, self, aux)
177 struct device *parent, *self;
178 void *aux;
179 {
180 struct pldwdog_softc *sc = (struct pldwdog_softc *)self;
181 struct ebus_attach_args *ea = aux;
182
183 sc->sc_btag = ea->ea_bustag;
184
185 if (ea->ea_nreg < 1) {
186 printf(": no registers??\n");
187 return;
188 }
189
190 if (ea->ea_nvaddr)
191 sparc_promaddr_to_handle(sc->sc_btag, ea->ea_vaddr[0], &sc->sc_bh);
192 else if (bus_space_map(sc->sc_btag, EBUS_ADDR_FROM_REG(&ea->ea_reg[0]),
193 ea->ea_reg[0].size, 0, &sc->sc_bh) != 0) {
194 printf(": can't map register space\n");
195 return;
196 }
197
198 sc->sc_wdog_period = PLD_WDOG_PERIOD_DEFAULT;
199
200 sc->sc_smw.smw_name = sc->sc_dev.dv_xname;
201 sc->sc_smw.smw_cookie = sc;
202 sc->sc_smw.smw_setmode = pldwdog_setmode;
203 sc->sc_smw.smw_tickle = pldwdog_tickle;
204 sc->sc_smw.smw_period = sc->sc_wdog_period;
205
206 if (sysmon_wdog_register(&sc->sc_smw) != 0)
207 printf("%s: unable to register with sysmon\n",
208 sc->sc_dev.dv_xname);
209
210 /* pldwdog_regs(sc); */
211
212 #if 0
213 bus_intr_establish(ea->ea_bustag, ea->ea_intr[0],
214 IPL_TTY, pldwdog_intr, sc);
215 #endif
216 }
217