xref: /netbsd-src/sys/arch/sparc64/dev/pld_wdog.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: pld_wdog.c,v 1.4 2005/12/11 12:19:09 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2003 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 #ifdef PLD_WDOG_DEBUG
82 static void pldwdog_regs(struct pldwdog_softc *);
83 #endif
84 
85 static int
86 pldwdog_tickle(struct sysmon_wdog *smw)
87 {
88 	struct pldwdog_softc *sc = smw->smw_cookie;
89 
90 #ifdef PLD_WDOG_DEBUG
91 	printf("%s: pldwdog_tickle: mode %x, period %d\n",
92 	       sc->sc_dev.dv_xname, smw->smw_mode, smw->smw_period);
93 /*	pldwdog_regs(sc); */
94 #endif
95 
96 	bus_space_write_2(sc->sc_btag, sc->sc_bh, PLD_WDOG2_LIMIT,
97 			  smw->smw_period * 10);
98 	bus_space_write_1(sc->sc_btag, sc->sc_bh, PLD_WDOG_INTR_MASK, 5);
99 
100 	return 0;
101 }
102 
103 static int
104 pldwdog_setmode(struct sysmon_wdog *smw)
105 {
106 	struct pldwdog_softc *sc = smw->smw_cookie;
107 
108 #ifdef PLD_WDOG_DEBUG
109 	printf("%s:pldwdog_setmode: mode %x\n", sc->sc_dev.dv_xname, smw->smw_mode);
110 #endif
111 
112 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
113 		bus_space_write_1(sc->sc_btag, sc->sc_bh, PLD_WDOG_INTR_MASK, 7);
114 	} else {
115 		if (smw->smw_period == WDOG_PERIOD_DEFAULT)
116 			smw->smw_period = sc->sc_wdog_period;
117 
118 		pldwdog_tickle(smw);
119 	}
120 	return (0);
121 }
122 
123 #if 0
124 static int pldwdog_intr(void);
125 static int
126 pldwdog_intr()
127 {
128 
129 	printf("pldwdog_intr:\n");
130 
131 	return 1;
132 }
133 #endif
134 
135 int
136 pldwdog_match(struct device *parent, struct cfdata *cf, void *aux)
137 {
138 	struct ebus_attach_args *ea = aux;
139 
140 	return (strcmp("watchdog", ea->ea_name) == 0);
141 }
142 
143 #ifdef PLD_WDOG_DEBUG
144 static void
145 pldwdog_regs(struct pldwdog_softc *sc)
146 {
147 
148 	printf("%s: status 0x%02x, intr mask 0x%02x\n",
149 	       sc->sc_dev.dv_xname,
150 	       bus_space_read_1(sc->sc_btag, sc->sc_bh, PLD_WDOG_INTR_MASK),
151 	       bus_space_read_1(sc->sc_btag, sc->sc_bh, PLD_WDOG_STATUS));
152 
153 	printf("%s: wdog1: count 0x%04x, limit 0x%04x, status 0x%02x\n",
154 	       sc->sc_dev.dv_xname,
155 	       bus_space_read_2(sc->sc_btag, sc->sc_bh, PLD_WDOG1_COUNTER),
156 	       bus_space_read_2(sc->sc_btag, sc->sc_bh, PLD_WDOG1_LIMIT),
157 	       bus_space_read_1(sc->sc_btag, sc->sc_bh, PLD_WDOG1_STATUS));
158 
159 	printf("%s: wdog2: count 0x%04x, limit 0x%04x, status 0x%02x\n",
160 	       sc->sc_dev.dv_xname,
161 	       bus_space_read_2(sc->sc_btag, sc->sc_bh, PLD_WDOG2_COUNTER),
162 	       bus_space_read_2(sc->sc_btag, sc->sc_bh, PLD_WDOG2_LIMIT),
163 	       bus_space_read_1(sc->sc_btag, sc->sc_bh, PLD_WDOG2_STATUS));
164 
165 	printf("%s: wdog3: count 0x%04x, limit 0x%04x, status 0x%02x\n",
166 	       sc->sc_dev.dv_xname,
167 	       bus_space_read_2(sc->sc_btag, sc->sc_bh, PLD_WDOG3_COUNTER),
168 	       bus_space_read_2(sc->sc_btag, sc->sc_bh, PLD_WDOG3_LIMIT),
169 	       bus_space_read_1(sc->sc_btag, sc->sc_bh, PLD_WDOG3_STATUS));
170 }
171 #endif
172 
173 void
174 pldwdog_attach(struct device *parent, struct device *self, void *aux)
175 {
176 	struct pldwdog_softc *sc = (struct pldwdog_softc *)self;
177 	struct ebus_attach_args *ea = aux;
178 
179 	printf("\n");
180 
181 	sc->sc_btag = ea->ea_bustag;
182 
183 	if (ea->ea_nreg < 1) {
184 		printf(": no registers??\n");
185 		return;
186 	}
187 
188 	if (ea->ea_nvaddr)
189 		sparc_promaddr_to_handle(sc->sc_btag, ea->ea_vaddr[0], &sc->sc_bh);
190 	else if (bus_space_map(sc->sc_btag, EBUS_ADDR_FROM_REG(&ea->ea_reg[0]),
191 				 ea->ea_reg[0].size, 0, &sc->sc_bh) != 0) {
192 		printf(": can't map register space\n");
193 		return;
194 	}
195 
196 	sc->sc_wdog_period = PLD_WDOG_PERIOD_DEFAULT;
197 
198 	sc->sc_smw.smw_name = sc->sc_dev.dv_xname;
199 	sc->sc_smw.smw_cookie = sc;
200 	sc->sc_smw.smw_setmode = pldwdog_setmode;
201 	sc->sc_smw.smw_tickle = pldwdog_tickle;
202 	sc->sc_smw.smw_period = sc->sc_wdog_period;
203 
204 	if (sysmon_wdog_register(&sc->sc_smw) != 0)
205 		printf("%s: unable to register with sysmon\n",
206 		    sc->sc_dev.dv_xname);
207 
208 /*	pldwdog_regs(sc); */
209 
210 #if 0
211 	bus_intr_establish(ea->ea_bustag, ea->ea_intr[0],
212 			   IPL_TTY, pldwdog_intr, sc);
213 #endif
214 }
215