Home | History | Annotate | Line # | Download | only in pci
tco.c revision 1.5
      1 /*	$NetBSD: tco.c,v 1.5 2022/09/22 14:41:49 riastradh 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.5 2022/09/22 14:41:49 riastradh 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_version;
     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 tco_attach_args *ta = aux;
     92 
     93 	if (ta->ta_iot == 0)
     94 		return 0;
     95 
     96 	switch (ta->ta_version) {
     97 	case TCO_VERSION_RCBA:
     98 	case TCO_VERSION_PCIB:
     99 		break;
    100 	default:
    101 		return 0;
    102 	}
    103 
    104 	return 1;
    105 }
    106 
    107 static void
    108 tco_attach(device_t parent, device_t self, void *aux)
    109 {
    110 	struct tco_softc *sc = device_private(self);
    111 	struct tco_attach_args *ta = aux;
    112 	uint32_t ioreg;
    113 
    114 	/* Retrieve bus info shared with parent/siblings */
    115 	sc->sc_version = ta->ta_version;
    116 	sc->sc_iot = ta->ta_iot;
    117 	sc->sc_ioh = ta->ta_ioh;
    118 	sc->sc_rcbat = ta->ta_rcbat;
    119 	sc->sc_rcbah = ta->ta_rcbah;
    120 	sc->sc_pcib = ta->ta_pcib;
    121 
    122 	aprint_normal(": TCO (watchdog) timer configured.\n");
    123 	aprint_naive("\n");
    124 
    125 	/* Explicitly stop the TCO timer. */
    126 	tcotimer_stop(sc);
    127 
    128 	/*
    129 	 * Enable TCO timeout SMI only if the hardware reset does not
    130 	 * work. We don't know what the SMBIOS does.
    131 	 */
    132 	ioreg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, LPCIB_SMI_EN);
    133 	ioreg &= ~LPCIB_SMI_EN_TCO_EN;
    134 
    135 	/*
    136 	 * Clear the No Reboot (NR) bit. If this fails, enabling the TCO_EN bit
    137 	 * in the SMI_EN register is the last chance.
    138 	 */
    139 	if (tcotimer_disable_noreboot(self)) {
    140 		ioreg |= LPCIB_SMI_EN_TCO_EN;
    141 	}
    142 	if ((ioreg & LPCIB_SMI_EN_GBL_SMI_EN) != 0) {
    143 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, LPCIB_SMI_EN, ioreg);
    144 	}
    145 
    146 	/* Reset the watchdog status registers. */
    147 	tcotimer_status_reset(sc);
    148 
    149 	/*
    150 	 * Register the driver with the sysmon watchdog framework.
    151 	 */
    152 	sc->sc_smw.smw_name = device_xname(self);
    153 	sc->sc_smw.smw_cookie = sc;
    154 	sc->sc_smw.smw_setmode = tcotimer_setmode;
    155 	sc->sc_smw.smw_tickle = tcotimer_tickle;
    156 
    157 	/*
    158 	 * ICH6 or newer are limited to 2ticks min and 613ticks max.
    159 	 *                              1sec           367secs
    160 	 *
    161 	 * ICH5 or older are limited to 4ticks min and 39ticks max.
    162 	 *                              2secs          23secs
    163 	 */
    164 	switch (sc->sc_version) {
    165 	case TCO_VERSION_RCBA:
    166 		sc->sc_max_t = LPCIB_TCOTIMER2_MAX_TICK;
    167 		sc->sc_min_t = LPCIB_TCOTIMER2_MIN_TICK;
    168 		break;
    169 	case TCO_VERSION_PCIB:
    170 		sc->sc_max_t = LPCIB_TCOTIMER_MAX_TICK;
    171 		sc->sc_min_t = LPCIB_TCOTIMER_MIN_TICK;
    172 		break;
    173 	}
    174 	sc->sc_smw.smw_period = lpcib_tcotimer_tick_to_second(sc->sc_max_t);
    175 
    176 	aprint_verbose_dev(self, "Min/Max interval %u/%u seconds\n",
    177 		lpcib_tcotimer_tick_to_second(sc->sc_min_t),
    178 		lpcib_tcotimer_tick_to_second(sc->sc_max_t));
    179 
    180 	if (sysmon_wdog_register(&sc->sc_smw))
    181 		aprint_error_dev(self, "unable to register TCO timer"
    182 		       "as a sysmon watchdog device.\n");
    183 
    184 	if (!pmf_device_register(self, tco_suspend, NULL))
    185 		aprint_error_dev(self, "unable to register with pmf\n");
    186 }
    187 
    188 static int
    189 tco_detach(device_t self, int flags)
    190 {
    191 	struct tco_softc *sc = device_private(self);
    192 	int rc;
    193 
    194 	if ((rc = sysmon_wdog_unregister(&sc->sc_smw)) != 0) {
    195 		if (rc == ERESTART)
    196 			rc = EINTR;
    197 		return rc;
    198 	}
    199 
    200 	/* Explicitly stop the TCO timer. */
    201 	tcotimer_stop(sc);
    202 
    203 	/* XXX Set No Reboot? */
    204 
    205 	pmf_device_deregister(self);
    206 
    207 	return 0;
    208 }
    209 
    210 static bool
    211 tco_suspend(device_t self, const pmf_qual_t *quals)
    212 {
    213 	struct tco_softc *sc = device_private(self);
    214 
    215 	/* Allow suspend only if watchdog is not armed */
    216 
    217 	return ((sc->sc_smw.smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED);
    218 }
    219 
    220 /*
    221  * Sysmon watchdog callbacks.
    222  */
    223 static int
    224 tcotimer_setmode(struct sysmon_wdog *smw)
    225 {
    226 	struct tco_softc *sc = smw->smw_cookie;
    227 	unsigned int period;
    228 	uint16_t ich6period = 0;
    229 	uint8_t ich5period = 0;
    230 
    231 	if ((smw->smw_mode & WDOG_MODE_MASK) == WDOG_MODE_DISARMED) {
    232 		/* Stop the TCO timer. */
    233 		tcotimer_stop(sc);
    234 	} else {
    235 		period = lpcib_tcotimer_second_to_tick(smw->smw_period);
    236 		if (period < sc->sc_min_t || period > sc->sc_max_t)
    237 			return EINVAL;
    238 
    239 		/* Stop the TCO timer, */
    240 		tcotimer_stop(sc);
    241 
    242 		/* set the timeout, */
    243 		switch (sc->sc_version) {
    244 		case TCO_VERSION_RCBA:
    245 			/* ICH6 or newer */
    246 			ich6period = bus_space_read_2(sc->sc_iot, sc->sc_ioh,
    247 			    LPCIB_TCO_TMR2);
    248 			ich6period &= 0xfc00;
    249 			bus_space_write_2(sc->sc_iot, sc->sc_ioh,
    250 			    LPCIB_TCO_TMR2, ich6period | period);
    251 			break;
    252 		case TCO_VERSION_PCIB:
    253 			/* ICH5 or older */
    254 			ich5period = bus_space_read_1(sc->sc_iot, sc->sc_ioh,
    255 			    LPCIB_TCO_TMR);
    256 			ich5period &= 0xc0;
    257 			bus_space_write_1(sc->sc_iot, sc->sc_ioh,
    258 			    LPCIB_TCO_TMR, ich5period | period);
    259 			break;
    260 		}
    261 
    262 		/* and start/reload the timer. */
    263 		tcotimer_start(sc);
    264 		tcotimer_tickle(smw);
    265 	}
    266 
    267 	return 0;
    268 }
    269 
    270 static int
    271 tcotimer_tickle(struct sysmon_wdog *smw)
    272 {
    273 	struct tco_softc *sc = smw->smw_cookie;
    274 
    275 	/* any value is allowed */
    276 	switch (sc->sc_version) {
    277 	case TCO_VERSION_RCBA:
    278 		bus_space_write_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO_RLD, 1);
    279 		break;
    280 	case TCO_VERSION_PCIB:
    281 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, LPCIB_TCO_RLD, 1);
    282 		break;
    283 	}
    284 
    285 	return 0;
    286 }
    287 
    288 static void
    289 tcotimer_stop(struct tco_softc *sc)
    290 {
    291 	uint16_t ioreg;
    292 
    293 	ioreg = bus_space_read_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO1_CNT);
    294 	ioreg |= LPCIB_TCO1_CNT_TCO_TMR_HLT;
    295 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO1_CNT, ioreg);
    296 }
    297 
    298 static void
    299 tcotimer_start(struct tco_softc *sc)
    300 {
    301 	uint16_t ioreg;
    302 
    303 	ioreg = bus_space_read_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO1_CNT);
    304 	ioreg &= ~LPCIB_TCO1_CNT_TCO_TMR_HLT;
    305 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO1_CNT, ioreg);
    306 }
    307 
    308 static void
    309 tcotimer_status_reset(struct tco_softc *sc)
    310 {
    311 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO1_STS,
    312 	    LPCIB_TCO1_STS_TIMEOUT);
    313 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO2_STS,
    314 	    LPCIB_TCO2_STS_BOOT_STS);
    315 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, LPCIB_TCO2_STS,
    316 	    LPCIB_TCO2_STS_SECONDS_TO_STS);
    317 }
    318 
    319 /*
    320  * Clear the No Reboot (NR) bit, this enables reboots when the timer
    321  * reaches the timeout for the second time.
    322  */
    323 static int
    324 tcotimer_disable_noreboot(device_t self)
    325 {
    326 	struct tco_softc *sc = device_private(self);
    327 
    328 	switch (sc->sc_version) {
    329 	case TCO_VERSION_RCBA: {
    330 		uint32_t status;
    331 
    332 		status = bus_space_read_4(sc->sc_rcbat, sc->sc_rcbah,
    333 		    LPCIB_GCS_OFFSET);
    334 		status &= ~LPCIB_GCS_NO_REBOOT;
    335 		bus_space_write_4(sc->sc_rcbat, sc->sc_rcbah,
    336 		    LPCIB_GCS_OFFSET, status);
    337 		status = bus_space_read_4(sc->sc_rcbat, sc->sc_rcbah,
    338 		    LPCIB_GCS_OFFSET);
    339 		if (status & LPCIB_GCS_NO_REBOOT)
    340 			goto error;
    341 		break;
    342 	}
    343 	case TCO_VERSION_PCIB: {
    344 		pcireg_t pcireg;
    345 
    346 		pcireg = pci_conf_read(sc->sc_pcib->sc_pc, sc->sc_pcib->sc_tag,
    347 		    LPCIB_PCI_GEN_STA);
    348 		if (pcireg & LPCIB_PCI_GEN_STA_NO_REBOOT) {
    349 			/* TCO timeout reset is disabled; try to enable it */
    350 			pcireg &= ~LPCIB_PCI_GEN_STA_NO_REBOOT;
    351 			pci_conf_write(sc->sc_pcib->sc_pc, sc->sc_pcib->sc_tag,
    352 			    LPCIB_PCI_GEN_STA, pcireg);
    353 			if (pcireg & LPCIB_PCI_GEN_STA_NO_REBOOT)
    354 				goto error;
    355 		}
    356 		break;
    357 	}
    358 	}
    359 
    360 	return 0;
    361 error:
    362 	aprint_error_dev(self, "TCO timer reboot disabled by hardware; "
    363 	    "hope SMBIOS properly handles it.\n");
    364 	return EINVAL;
    365 }
    366 
    367 MODULE(MODULE_CLASS_DRIVER, tco, "sysmon_wdog");
    368 
    369 #ifdef _MODULE
    370 #include "ioconf.c"
    371 #endif
    372 
    373 static int
    374 tco_modcmd(modcmd_t cmd, void *arg)
    375 {
    376 	int ret = 0;
    377 
    378 	switch (cmd) {
    379 	case MODULE_CMD_INIT:
    380 #ifdef _MODULE
    381 		ret = config_init_component(cfdriver_ioconf_tco,
    382 		    cfattach_ioconf_tco,
    383 		    cfdata_ioconf_tco);
    384 #endif
    385 		break;
    386 	case MODULE_CMD_FINI:
    387 #ifdef _MODULE
    388 		ret = config_fini_component(cfdriver_ioconf_tco,
    389 		    cfattach_ioconf_tco,
    390 		    cfdata_ioconf_tco);
    391 #endif
    392 		break;
    393 	default:
    394 		ret = ENOTTY;
    395 	}
    396 
    397 	return ret;
    398 }
    399