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