tco.c revision 1.1.2.2 1 /* $NetBSD: tco.c,v 1.1.2.2 2015/06/06 14:40:04 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 2015 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Minoura Makoto and Matthew R. Green.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Intel I/O Controller Hub (ICHn) watchdog timer
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: tco.c,v 1.1.2.2 2015/06/06 14:40:04 skrll Exp $");
38
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/device.h>
43 #include <sys/timetc.h>
44 #include <sys/module.h>
45
46 #include <dev/pci/pcivar.h>
47 #include <dev/pci/pcireg.h>
48 #include <dev/ic/i82801lpcreg.h>
49
50 #include <dev/sysmon/sysmonvar.h>
51
52 #include <arch/x86/pci/tco.h>
53
54 #include "pcibvar.h"
55
56 struct tco_softc{
57 struct sysmon_wdog sc_smw;
58 bus_space_tag_t sc_iot;
59 bus_space_handle_t sc_ioh;
60 bus_space_tag_t sc_rcbat;
61 bus_space_handle_t sc_rcbah;
62 struct pcib_softc * sc_pcib;
63 int sc_armed;
64 unsigned int sc_min_t;
65 unsigned int sc_max_t;
66 int sc_has_rcba;
67 };
68
69 static int tco_match(device_t, cfdata_t, void *);
70 static void tco_attach(device_t, device_t, void *);
71 static int tco_detach(device_t, int);
72
73 static bool tco_suspend(device_t, const pmf_qual_t *);
74
75 static int tcotimer_setmode(struct sysmon_wdog *);
76 static int tcotimer_tickle(struct sysmon_wdog *);
77 static void tcotimer_stop(struct tco_softc *);
78 static void tcotimer_start(struct tco_softc *);
79 static void tcotimer_status_reset(struct tco_softc *);
80 static int tcotimer_disable_noreboot(device_t);
81
82 CFATTACH_DECL3_NEW(tco, sizeof(struct tco_softc),
83 tco_match, tco_attach, tco_detach, NULL, NULL, NULL, 0);
84
85 /*
86 * Autoconf callbacks.
87 */
88 static int
89 tco_match(device_t parent, cfdata_t match, void *aux)
90 {
91 struct lpcib_tco_attach_args *ta = aux;
92
93 if (ta->ta_iot != 0)
94 return 1;
95
96 return 0;
97 }
98
99 static void
100 tco_attach(device_t parent, device_t self, void *aux)
101 {
102 struct tco_softc *sc = device_private(self);
103 struct lpcib_tco_attach_args *ta = aux;
104 uint32_t ioreg;
105
106 /* Retrieve bus info shared with parent/siblings */
107
108 sc->sc_iot = ta->ta_iot;
109 sc->sc_ioh = ta->ta_ioh;
110 sc->sc_rcbat = ta->ta_rcbat;
111 sc->sc_rcbah = ta->ta_rcbah;
112 sc->sc_pcib = ta->ta_pcib;
113 sc->sc_has_rcba = ta->ta_has_rcba;
114
115 /* Explicitly stop the TCO timer. */
116 tcotimer_stop(sc);
117
118 /*
119 * Enable TCO timeout SMI only if the hardware reset does not
120 * work. We don't know what the SMBIOS does.
121 */
122 ioreg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, LPCIB_SMI_EN);
123 ioreg &= ~LPCIB_SMI_EN_TCO_EN;
124
125 /*
126 * Clear the No Reboot (NR) bit. If this fails, enabling the TCO_EN bit
127 * in the SMI_EN register is the last chance.
128 */
129 if (tcotimer_disable_noreboot(self)) {
130 ioreg |= LPCIB_SMI_EN_TCO_EN;
131 }
132 if ((ioreg & LPCIB_SMI_EN_GBL_SMI_EN) != 0) {
133 bus_space_write_4(sc->sc_iot, sc->sc_ioh, LPCIB_SMI_EN, ioreg);
134 }
135
136 /* Reset the watchdog status registers. */
137 tcotimer_status_reset(sc);
138
139 /*
140 * Register the driver with the sysmon watchdog framework.
141 */
142 sc->sc_smw.smw_name = device_xname(self);
143 sc->sc_smw.smw_cookie = sc;
144 sc->sc_smw.smw_setmode = tcotimer_setmode;
145 sc->sc_smw.smw_tickle = tcotimer_tickle;
146
147 /*
148 * ICH6 or newer are limited to 2ticks min and 613ticks max.
149 * 1sec 367secs
150 *
151 * ICH5 or older are limited to 4ticks min and 39ticks max.
152 * 2secs 23secs
153 */
154 if (sc->sc_has_rcba) {
155 sc->sc_max_t = LPCIB_TCOTIMER2_MAX_TICK;
156 sc->sc_min_t = LPCIB_TCOTIMER2_MIN_TICK;
157 } else {
158 sc->sc_max_t = LPCIB_TCOTIMER_MAX_TICK;
159 sc->sc_min_t = LPCIB_TCOTIMER_MIN_TICK;
160 }
161 sc->sc_smw.smw_period = lpcib_tcotimer_tick_to_second(sc->sc_max_t);
162
163 aprint_normal(": TCO (watchdog) timer configured.\n");
164 aprint_naive("\n");
165 aprint_verbose_dev(self, "Min/Max interval %u/%u seconds\n",
166 lpcib_tcotimer_tick_to_second(sc->sc_min_t),
167 lpcib_tcotimer_tick_to_second(sc->sc_max_t));
168
169 if (sysmon_wdog_register(&sc->sc_smw))
170 aprint_error_dev(self, "unable to register TCO timer"
171 "as a sysmon watchdog device.\n");
172
173 if (!pmf_device_register(self, tco_suspend, NULL))
174 aprint_error_dev(self, "unable to register with pmf\n");
175 }
176
177 static int
178 tco_detach(device_t self, int flags)
179 {
180 struct tco_softc *sc = device_private(self);
181 int rc;
182
183 if ((rc = sysmon_wdog_unregister(&sc->sc_smw)) != 0) {
184 if (rc == ERESTART)
185 rc = EINTR;
186 return rc;
187 }
188
189 /* Explicitly stop the TCO timer. */
190 tcotimer_stop(sc);
191
192 /* XXX Set No Reboot? */
193
194 pmf_device_deregister(self);
195
196 return 0;
197 }
198
199 static bool
200 tco_suspend(device_t self, const pmf_qual_t *quals)
201 {
202 struct tco_softc *sc = device_private(self);
203
204 /* Allow suspend only if watchdog is not armed */
205
206 return ((sc->sc_smw.smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED);
207 }
208
209 /*
210 * Sysmon watchdog callbacks.
211 */
212 static int
213 tcotimer_setmode(struct sysmon_wdog *smw)
214 {
215 struct tco_softc *sc = smw->smw_cookie;
216 unsigned int period;
217 uint16_t ich6period = 0;
218 uint8_t ich5period = 0;
219
220 if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
221 /* Stop the TCO timer. */
222 tcotimer_stop(sc);
223 } else {
224 period = lpcib_tcotimer_second_to_tick(smw->smw_period);
225 if (period < sc->sc_min_t || period > sc->sc_max_t)
226 return EINVAL;
227
228 /* Stop the TCO timer, */
229 tcotimer_stop(sc);
230
231 /* set the timeout, */
232 if (sc->sc_has_rcba) {
233 /* ICH6 or newer */
234 ich6period = bus_space_read_2(sc->sc_iot, sc->sc_ioh,
235 LPCIB_TCO_TMR2);
236 ich6period &= 0xfc00;
237 bus_space_write_2(sc->sc_iot, sc->sc_ioh,
238 LPCIB_TCO_TMR2, ich6period | period);
239 } else {
240 /* ICH5 or older */
241 ich5period = bus_space_read_1(sc->sc_iot, sc->sc_ioh,
242 LPCIB_TCO_TMR);
243 ich5period &= 0xc0;
244 bus_space_write_1(sc->sc_iot, sc->sc_ioh,
245 LPCIB_TCO_TMR, ich5period | period);
246 }
247
248 /* and start/reload the timer. */
249 tcotimer_start(sc);
250 tcotimer_tickle(smw);
251 }
252
253 return 0;
254 }
255
256 static int
257 tcotimer_tickle(struct sysmon_wdog *smw)
258 {
259 struct tco_softc *sc = smw->smw_cookie;
260
261 /* any value is allowed */
262 if (sc->sc_has_rcba)
263 bus_space_write_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO_RLD, 1);
264 else
265 bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_TCO_RLD, 1);
266
267 return 0;
268 }
269
270 static void
271 tcotimer_stop(struct tco_softc *sc)
272 {
273 uint16_t ioreg;
274
275 ioreg = bus_space_read_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO1_CNT);
276 ioreg |= LPCIB_TCO1_CNT_TCO_TMR_HLT;
277 bus_space_write_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO1_CNT, ioreg);
278 }
279
280 static void
281 tcotimer_start(struct tco_softc *sc)
282 {
283 uint16_t ioreg;
284
285 ioreg = bus_space_read_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO1_CNT);
286 ioreg &= ~LPCIB_TCO1_CNT_TCO_TMR_HLT;
287 bus_space_write_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO1_CNT, ioreg);
288 }
289
290 static void
291 tcotimer_status_reset(struct tco_softc *sc)
292 {
293 bus_space_write_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO1_STS,
294 LPCIB_TCO1_STS_TIMEOUT);
295 bus_space_write_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO2_STS,
296 LPCIB_TCO2_STS_BOOT_STS);
297 bus_space_write_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO2_STS,
298 LPCIB_TCO2_STS_SECONDS_TO_STS);
299 }
300
301 /*
302 * Clear the No Reboot (NR) bit, this enables reboots when the timer
303 * reaches the timeout for the second time.
304 */
305 static int
306 tcotimer_disable_noreboot(device_t self)
307 {
308 struct tco_softc *sc = device_private(self);
309
310 if (sc->sc_has_rcba) {
311 uint32_t status;
312
313 status = bus_space_read_4(sc->sc_rcbat, sc->sc_rcbah,
314 LPCIB_GCS_OFFSET);
315 status &= ~LPCIB_GCS_NO_REBOOT;
316 bus_space_write_4(sc->sc_rcbat, sc->sc_rcbah,
317 LPCIB_GCS_OFFSET, status);
318 status = bus_space_read_4(sc->sc_rcbat, sc->sc_rcbah,
319 LPCIB_GCS_OFFSET);
320 if (status & LPCIB_GCS_NO_REBOOT)
321 goto error;
322 } else {
323 pcireg_t pcireg;
324
325 pcireg = pci_conf_read(sc->sc_pcib->sc_pc, sc->sc_pcib->sc_tag,
326 LPCIB_PCI_GEN_STA);
327 if (pcireg & LPCIB_PCI_GEN_STA_NO_REBOOT) {
328 /* TCO timeout reset is disabled; try to enable it */
329 pcireg &= ~LPCIB_PCI_GEN_STA_NO_REBOOT;
330 pci_conf_write(sc->sc_pcib->sc_pc, sc->sc_pcib->sc_tag,
331 LPCIB_PCI_GEN_STA, pcireg);
332 if (pcireg & LPCIB_PCI_GEN_STA_NO_REBOOT)
333 goto error;
334 }
335 }
336
337 return 0;
338 error:
339 aprint_error_dev(self, "TCO timer reboot disabled by hardware; "
340 "hope SMBIOS properly handles it.\n");
341 return EINVAL;
342 }
343
344 MODULE(MODULE_CLASS_DRIVER, tco, "sysmon_wdog");
345
346 #ifdef _MODULE
347 #include "ioconf.c"
348 #endif
349
350 static int
351 tco_modcmd(modcmd_t cmd, void *arg)
352 {
353 int ret = 0;
354
355 switch (cmd) {
356 case MODULE_CMD_INIT:
357 #ifdef _MODULE
358 ret = config_init_component(cfdriver_ioconf_tco,
359 cfattach_ioconf_tco,
360 cfdata_ioconf_tco);
361 #endif
362 break;
363 case MODULE_CMD_FINI:
364 #ifdef _MODULE
365 ret = config_fini_component(cfdriver_ioconf_tco,
366 cfattach_ioconf_tco,
367 cfdata_ioconf_tco);
368 #endif
369 break;
370 default:
371 ret = ENOTTY;
372 }
373
374 return ret;
375 }
376