xref: /freebsd/sys/dev/ftwd/ftwd.c (revision d5b0e70f7e04d971691517ce1304d86a1e367e2e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2020 Poul-Henning Kamp
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  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/bus.h>
34 #include <sys/eventhandler.h>
35 #include <sys/module.h>
36 #include <sys/rman.h>
37 #include <sys/systm.h>
38 #include <sys/watchdog.h>
39 
40 #include <dev/superio/superio.h>
41 
42 #include <machine/bus.h>
43 #include <machine/resource.h>
44 
45 struct ftwd_softc {
46 	eventhandler_tag	wd_ev;
47 };
48 
49 static void
50 ftwd_func(void *priv, u_int cmd, int *error)
51 {
52 	device_t dev = priv;
53 	uint64_t timeout;
54 	uint8_t val = 0;
55 	uint8_t minutes = 0;
56 
57 	if (cmd != 0) {
58 		cmd &= WD_INTERVAL;
59 
60 		/* Convert the requested timeout to seconds. */
61 		if (cmd >= WD_TO_1SEC)
62 			timeout = (uint64_t)1 << (cmd - WD_TO_1SEC);
63 		else
64 			timeout = 1;
65 
66 		if (timeout <= UINT8_MAX) {
67 			val = timeout;
68 			*error = 0;
69 		} else if ((timeout / 60) <= UINT8_MAX) {
70 			val = timeout / 60;
71 			minutes = 1;
72 			*error = 0;
73 		}
74 	}
75 	if (bootverbose) {
76                 if (val == 0) {
77 			device_printf(dev, "disabling watchdog\n");
78 		} else {
79 			device_printf(dev,
80 			    "arm watchdog to %d %s%s (Was: 0x%02x)\n",
81 			    val, minutes ? "minute" : "second",
82                             val == 1 ? "" : "s",
83 			    superio_read(dev, 0xf6)
84 			);
85 		}
86 	}
87 	superio_write(dev, 0xf0, 0x00);		// Disable WDTRST#
88 	superio_write(dev, 0xf6, val);		// Set Counter
89 
90 	if (minutes)
91 		superio_write(dev, 0xf5, 0x7d);	// minutes, act high, 125ms
92 	else
93 		superio_write(dev, 0xf5, 0x75);	// seconds, act high, 125ms
94 
95 	if (val)
96 		superio_write(dev, 0xf7, 0x01);	// Disable PME
97 	if (val)
98 		superio_write(dev, 0xf0, 0x81);	// Enable WDTRST#
99 	else
100 		superio_write(dev, 0xf0, 0x00);	// Disable WDTRST
101 }
102 
103 static int
104 ftwd_probe(device_t dev)
105 {
106 
107 	if (superio_vendor(dev) != SUPERIO_VENDOR_FINTEK ||
108 	    superio_get_type(dev) != SUPERIO_DEV_WDT)
109 		return (ENXIO);
110 	device_set_desc(dev, "Watchdog Timer on Fintek SuperIO");
111 	return (BUS_PROBE_DEFAULT);
112 }
113 
114 static int
115 ftwd_attach(device_t dev)
116 {
117 	struct ftwd_softc *sc = device_get_softc(dev);
118 
119 	/*
120 	 * We do not touch the watchdog at this time, it might be armed
121 	 * by firmware to protect the full boot sequence.
122 	 */
123 
124 	sc->wd_ev = EVENTHANDLER_REGISTER(watchdog_list, ftwd_func, dev, 0);
125 	return (0);
126 }
127 
128 static int
129 ftwd_detach(device_t dev)
130 {
131 	struct ftwd_softc *sc = device_get_softc(dev);
132 	int dummy;
133 
134 	if (sc->wd_ev != NULL)
135 		EVENTHANDLER_DEREGISTER(watchdog_list, sc->wd_ev);
136 	ftwd_func(dev, 0, &dummy);
137 	return (0);
138 }
139 
140 static device_method_t ftwd_methods[] = {
141 	DEVMETHOD(device_probe,		ftwd_probe),
142 	DEVMETHOD(device_attach,	ftwd_attach),
143 	DEVMETHOD(device_detach,	ftwd_detach),
144 	{ 0, 0 }
145 };
146 
147 static driver_t ftwd_driver = {
148 	"ftwd",
149 	ftwd_methods,
150 	sizeof (struct ftwd_softc)
151 };
152 
153 DRIVER_MODULE(ftwd, superio, ftwd_driver, NULL, NULL);
154 MODULE_DEPEND(ftwd, superio, 1, 1, 1);
155 MODULE_VERSION(ftwd, 1);
156