xref: /freebsd/sys/dev/viawd/viawd.c (revision 1cba6077beafdf29297854b8f6ab0a11e06d2782)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2011 Fabien Thomas <fabient@FreeBSD.org>
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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/eventhandler.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/systm.h>
37 #include <sys/bus.h>
38 #include <machine/bus.h>
39 #include <sys/rman.h>
40 #include <machine/resource.h>
41 #include <sys/watchdog.h>
42 
43 #include <isa/isavar.h>
44 #include <dev/pci/pcivar.h>
45 
46 #include "viawd.h"
47 
48 #define	viawd_read_4(sc, off)	bus_read_4((sc)->wd_res, (off))
49 #define	viawd_write_4(sc, off, val)	\
50 	bus_write_4((sc)->wd_res, (off), (val))
51 
52 static struct viawd_device viawd_devices[] = {
53 	{ DEVICEID_VT8251, "VIA VT8251 watchdog timer" },
54 	{ DEVICEID_CX700,  "VIA CX700 watchdog timer" },
55 	{ DEVICEID_VX800,  "VIA VX800 watchdog timer" },
56 	{ DEVICEID_VX855,  "VIA VX855 watchdog timer" },
57 	{ DEVICEID_VX900,  "VIA VX900 watchdog timer" },
58 	{ 0, NULL },
59 };
60 
61 static devclass_t viawd_devclass;
62 
63 static void
64 viawd_tmr_state(struct viawd_softc *sc, int enable)
65 {
66 	uint32_t reg;
67 
68 	reg = viawd_read_4(sc, VIAWD_MEM_CTRL);
69 	if (enable)
70 		reg |= VIAWD_MEM_CTRL_TRIGGER | VIAWD_MEM_CTRL_ENABLE;
71 	else
72 		reg &= ~VIAWD_MEM_CTRL_ENABLE;
73 	viawd_write_4(sc, VIAWD_MEM_CTRL, reg);
74 }
75 
76 static void
77 viawd_tmr_set(struct viawd_softc *sc, unsigned int timeout)
78 {
79 
80 	/* Keep value in range. */
81 	if (timeout < VIAWD_MEM_COUNT_MIN)
82 		timeout = VIAWD_MEM_COUNT_MIN;
83 	else if (timeout > VIAWD_MEM_COUNT_MAX)
84 		timeout = VIAWD_MEM_COUNT_MAX;
85 
86 	viawd_write_4(sc, VIAWD_MEM_COUNT, timeout);
87 	sc->timeout = timeout;
88 }
89 
90 /*
91  * Watchdog event handler - called by the framework to enable or disable
92  * the watchdog or change the initial timeout value.
93  */
94 static void
95 viawd_event(void *arg, unsigned int cmd, int *error)
96 {
97 	struct viawd_softc *sc = arg;
98 	unsigned int timeout;
99 
100 	/* Convert from power-of-two-ns to second. */
101 	cmd &= WD_INTERVAL;
102 	timeout = ((uint64_t)1 << cmd) / 1000000000;
103 	if (cmd) {
104 		if (timeout != sc->timeout)
105 			viawd_tmr_set(sc, timeout);
106 		viawd_tmr_state(sc, 1);
107 		*error = 0;
108 	} else
109 		viawd_tmr_state(sc, 0);
110 }
111 
112 /* Look for a supported VIA south bridge. */
113 static struct viawd_device *
114 viawd_find(device_t dev)
115 {
116 	struct viawd_device *id;
117 
118 	if (pci_get_vendor(dev) != VENDORID_VIA)
119 		return (NULL);
120 	for (id = viawd_devices; id->desc != NULL; id++)
121 		if (pci_get_device(dev) == id->device)
122 			return (id);
123 	return (NULL);
124 }
125 
126 static void
127 viawd_identify(driver_t *driver, device_t parent)
128 {
129 
130 	if (viawd_find(parent) == NULL)
131 		return;
132 
133 	if (device_find_child(parent, driver->name, -1) == NULL)
134 		BUS_ADD_CHILD(parent, 0, driver->name, 0);
135 }
136 
137 static int
138 viawd_probe(device_t dev)
139 {
140 	struct viawd_device *id;
141 
142 	id = viawd_find(device_get_parent(dev));
143 	KASSERT(id != NULL, ("parent should be a valid VIA SB"));
144 	device_set_desc(dev, id->desc);
145 	return (BUS_PROBE_GENERIC);
146 }
147 
148 static int
149 viawd_attach(device_t dev)
150 {
151 	device_t sb_dev;
152 	struct viawd_softc *sc;
153 	uint32_t pmbase, reg;
154 
155 	sc = device_get_softc(dev);
156 	sc->dev = dev;
157 
158 	sb_dev = device_get_parent(dev);
159 	if (sb_dev == NULL) {
160 		device_printf(dev, "Can not find watchdog device.\n");
161 		goto fail;
162 	}
163 	sc->sb_dev = sb_dev;
164 
165 	/* Get watchdog memory base. */
166 	pmbase = pci_read_config(sb_dev, VIAWD_CONFIG_BASE, 4);
167 	if (pmbase == 0) {
168 		device_printf(dev,
169 		    "Watchdog disabled in BIOS or hardware\n");
170 		goto fail;
171 	}
172 
173 	/* Allocate I/O register space. */
174 	sc->wd_rid = VIAWD_CONFIG_BASE;
175 	sc->wd_res = bus_alloc_resource_any(sb_dev, SYS_RES_MEMORY, &sc->wd_rid,
176 	    RF_ACTIVE | RF_SHAREABLE);
177 	if (sc->wd_res == NULL) {
178 		device_printf(dev, "Unable to map watchdog memory\n");
179 		goto fail;
180 	}
181 	if (rman_get_size(sc->wd_res) < VIAWD_MEM_LEN) {
182 		device_printf(dev, "Bad size for watchdog memory: %#x\n",
183 		    (unsigned)rman_get_size(sc->wd_res));
184 		goto fail;
185 	}
186 
187 	/* Check if watchdog fired last boot. */
188 	reg = viawd_read_4(sc, VIAWD_MEM_CTRL);
189 	if (reg & VIAWD_MEM_CTRL_FIRED) {
190 		device_printf(dev,
191 		    "ERROR: watchdog rebooted the system\n");
192 		/* Reset bit state. */
193 		viawd_write_4(sc, VIAWD_MEM_CTRL, reg);
194 	}
195 
196 	/* Register the watchdog event handler. */
197 	sc->ev_tag = EVENTHANDLER_REGISTER(watchdog_list, viawd_event, sc, 0);
198 
199 	return (0);
200 fail:
201 	if (sc->wd_res != NULL)
202 		bus_release_resource(sb_dev, SYS_RES_MEMORY,
203 		    sc->wd_rid, sc->wd_res);
204 	return (ENXIO);
205 }
206 
207 static int
208 viawd_detach(device_t dev)
209 {
210 	struct viawd_softc *sc;
211 	uint32_t reg;
212 
213 	sc = device_get_softc(dev);
214 
215 	/* Deregister event handler. */
216 	if (sc->ev_tag != NULL)
217 		EVENTHANDLER_DEREGISTER(watchdog_list, sc->ev_tag);
218 	sc->ev_tag = NULL;
219 
220 	/*
221 	 * Do not stop the watchdog on shutdown if active but bump the
222 	 * timer to avoid spurious reset.
223 	 */
224 	reg = viawd_read_4(sc, VIAWD_MEM_CTRL);
225 	if (reg & VIAWD_MEM_CTRL_ENABLE) {
226 		viawd_tmr_set(sc, VIAWD_TIMEOUT_SHUTDOWN);
227 		viawd_tmr_state(sc, 1);
228 		device_printf(dev,
229 		    "Keeping watchdog alive during shutdown for %d seconds\n",
230 		    VIAWD_TIMEOUT_SHUTDOWN);
231 	}
232 
233 	if (sc->wd_res != NULL)
234 		bus_release_resource(sc->sb_dev, SYS_RES_MEMORY,
235 		    sc->wd_rid, sc->wd_res);
236 
237 	return (0);
238 }
239 
240 static device_method_t viawd_methods[] = {
241 	DEVMETHOD(device_identify, viawd_identify),
242 	DEVMETHOD(device_probe,	viawd_probe),
243 	DEVMETHOD(device_attach, viawd_attach),
244 	DEVMETHOD(device_detach, viawd_detach),
245 	DEVMETHOD(device_shutdown, viawd_detach),
246 	{0,0}
247 };
248 
249 static driver_t viawd_driver = {
250 	"viawd",
251 	viawd_methods,
252 	sizeof(struct viawd_softc),
253 };
254 
255 DRIVER_MODULE(viawd, isab, viawd_driver, viawd_devclass, NULL, NULL);
256