xref: /freebsd/sys/dev/ftwd/ftwd.c (revision 3494f7c0)
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/param.h>
29 #include <sys/kernel.h>
30 #include <sys/bus.h>
31 #include <sys/eventhandler.h>
32 #include <sys/module.h>
33 #include <sys/rman.h>
34 #include <sys/systm.h>
35 #include <sys/watchdog.h>
36 
37 #include <dev/superio/superio.h>
38 
39 #include <machine/bus.h>
40 #include <machine/resource.h>
41 
42 struct ftwd_softc {
43 	eventhandler_tag	wd_ev;
44 };
45 
46 static void
47 ftwd_func(void *priv, u_int cmd, int *error)
48 {
49 	device_t dev = priv;
50 	uint64_t timeout;
51 	uint8_t val = 0;
52 	uint8_t minutes = 0;
53 
54 	if (cmd != 0) {
55 		cmd &= WD_INTERVAL;
56 
57 		/* Convert the requested timeout to seconds. */
58 		if (cmd >= WD_TO_1SEC)
59 			timeout = (uint64_t)1 << (cmd - WD_TO_1SEC);
60 		else
61 			timeout = 1;
62 
63 		if (timeout <= UINT8_MAX) {
64 			val = timeout;
65 			*error = 0;
66 		} else if ((timeout / 60) <= UINT8_MAX) {
67 			val = timeout / 60;
68 			minutes = 1;
69 			*error = 0;
70 		}
71 	}
72 	if (bootverbose) {
73                 if (val == 0) {
74 			device_printf(dev, "disabling watchdog\n");
75 		} else {
76 			device_printf(dev,
77 			    "arm watchdog to %d %s%s (Was: 0x%02x)\n",
78 			    val, minutes ? "minute" : "second",
79                             val == 1 ? "" : "s",
80 			    superio_read(dev, 0xf6)
81 			);
82 		}
83 	}
84 	superio_write(dev, 0xf0, 0x00);		// Disable WDTRST#
85 	superio_write(dev, 0xf6, val);		// Set Counter
86 
87 	if (minutes)
88 		superio_write(dev, 0xf5, 0x7d);	// minutes, act high, 125ms
89 	else
90 		superio_write(dev, 0xf5, 0x75);	// seconds, act high, 125ms
91 
92 	if (val)
93 		superio_write(dev, 0xf7, 0x01);	// Disable PME
94 	if (val)
95 		superio_write(dev, 0xf0, 0x81);	// Enable WDTRST#
96 	else
97 		superio_write(dev, 0xf0, 0x00);	// Disable WDTRST
98 }
99 
100 static int
101 ftwd_probe(device_t dev)
102 {
103 
104 	if (superio_vendor(dev) != SUPERIO_VENDOR_FINTEK ||
105 	    superio_get_type(dev) != SUPERIO_DEV_WDT)
106 		return (ENXIO);
107 	device_set_desc(dev, "Watchdog Timer on Fintek SuperIO");
108 	return (BUS_PROBE_DEFAULT);
109 }
110 
111 static int
112 ftwd_attach(device_t dev)
113 {
114 	struct ftwd_softc *sc = device_get_softc(dev);
115 
116 	/*
117 	 * We do not touch the watchdog at this time, it might be armed
118 	 * by firmware to protect the full boot sequence.
119 	 */
120 
121 	sc->wd_ev = EVENTHANDLER_REGISTER(watchdog_list, ftwd_func, dev, 0);
122 	return (0);
123 }
124 
125 static int
126 ftwd_detach(device_t dev)
127 {
128 	struct ftwd_softc *sc = device_get_softc(dev);
129 	int dummy;
130 
131 	if (sc->wd_ev != NULL)
132 		EVENTHANDLER_DEREGISTER(watchdog_list, sc->wd_ev);
133 	ftwd_func(dev, 0, &dummy);
134 	return (0);
135 }
136 
137 static device_method_t ftwd_methods[] = {
138 	DEVMETHOD(device_probe,		ftwd_probe),
139 	DEVMETHOD(device_attach,	ftwd_attach),
140 	DEVMETHOD(device_detach,	ftwd_detach),
141 	{ 0, 0 }
142 };
143 
144 static driver_t ftwd_driver = {
145 	"ftwd",
146 	ftwd_methods,
147 	sizeof (struct ftwd_softc)
148 };
149 
150 DRIVER_MODULE(ftwd, superio, ftwd_driver, NULL, NULL);
151 MODULE_DEPEND(ftwd, superio, 1, 1, 1);
152 MODULE_VERSION(ftwd, 1);
153