Home | History | Annotate | Line # | Download | only in pci
pwdog.c revision 1.1
      1 /*	$$NetBSD: pwdog.c,v 1.1 2011/08/11 07:33:24 mbalmer Exp $ */
      2 /*	$OpenBSD: pwdog.c,v 1.7 2010/04/08 00:23:53 tedu Exp $ */
      3 
      4 /*
      5  * Copyright (c) 2011 Marc Balmer <mbalmer (at) NetBSD.org>
      6  * Copyright (c) 2006 Marc Balmer <mbalmer (at) openbsd.org>
      7  *
      8  * Permission to use, copy, modify, and distribute this software for any
      9  * purpose with or without fee is hereby granted, provided that the above
     10  * copyright notice and this permission notice appear in all copies.
     11  *
     12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     19  */
     20 
     21 #include <sys/types.h>
     22 #include <sys/param.h>
     23 #include <sys/device.h>
     24 #include <sys/kernel.h>
     25 #ifdef _MODULE
     26 #include <sys/module.h>
     27 #endif
     28 #include <sys/systm.h>
     29 
     30 /* #include <machine/bus.h> */
     31 
     32 #include <dev/pci/pcivar.h>
     33 #include <dev/pci/pcireg.h>
     34 #include <dev/pci/pcidevs.h>
     35 
     36 #include <dev/sysmon/sysmonvar.h>
     37 
     38 struct pwdog_softc {
     39 	device_t		sc_dev;
     40 
     41 	bus_space_tag_t		sc_iot;
     42 	bus_space_handle_t	sc_ioh;
     43 	bus_size_t		sc_iosize;
     44 
     45 	struct sysmon_wdog	sc_smw;
     46 	bool			sc_smw_valid;
     47 };
     48 
     49 /* registers */
     50 #define PWDOG_ACTIVATE	0
     51 #define PWDOG_DISABLE	1
     52 
     53 /* maximum timeout period in seconds */
     54 #define PWDOG_MAX_PERIOD	(12*60)	/* 12 minutes */
     55 
     56 static int pwdog_match(device_t, cfdata_t, void *);
     57 static void pwdog_attach(device_t, device_t, void *);
     58 static int pwdog_detach(device_t, int);
     59 static bool pwdog_suspend(device_t, const pmf_qual_t *);
     60 static int pwdog_setmode(struct sysmon_wdog *smw);
     61 static int pwdog_tickle(struct sysmon_wdog *smw);
     62 
     63 CFATTACH_DECL_NEW(
     64     pwdog,
     65     sizeof(struct pwdog_softc),
     66     pwdog_match,
     67     pwdog_attach,
     68     pwdog_detach,
     69     NULL
     70 );
     71 
     72 static int
     73 pwdog_match(device_t parent, cfdata_t match, void *aux)
     74 {
     75 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
     76 
     77 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_QUANCOM &&
     78 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_QUANCOM_PWDOG1)
     79 		return 1;
     80 	return 0;
     81 }
     82 
     83 void
     84 pwdog_attach(device_t parent, device_t self, void *aux)
     85 {
     86 	struct pwdog_softc *sc = device_private(self);
     87 	struct pci_attach_args *const pa = (struct pci_attach_args *)aux;
     88 	pcireg_t memtype;
     89 
     90 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, PCI_MAPREG_START);
     91 	if (pci_mapreg_map(pa, PCI_MAPREG_START, memtype, 0, &sc->sc_iot,
     92 	    &sc->sc_ioh, NULL, &sc->sc_iosize)) {
     93 		aprint_error("\n");
     94 		aprint_error_dev(self, "PCI %s region not found\n",
     95 		    memtype == PCI_MAPREG_TYPE_IO ? "I/O" : "memory");
     96 		return;
     97 	}
     98 	printf("\n");
     99 
    100 	sc->sc_dev = self;
    101 
    102 	pmf_device_register(self, pwdog_suspend, NULL);
    103 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, PWDOG_DISABLE, 0);
    104 
    105 	sc->sc_smw.smw_name = device_xname(self);
    106 	sc->sc_smw.smw_cookie = sc;
    107 	sc->sc_smw.smw_setmode = pwdog_setmode;
    108 	sc->sc_smw.smw_tickle = pwdog_tickle;
    109 	sc->sc_smw.smw_period = PWDOG_MAX_PERIOD;
    110 
    111 	if (sysmon_wdog_register(&sc->sc_smw))
    112 		aprint_error_dev(self, "couldn't register with sysmon\n");
    113 	else
    114 		sc->sc_smw_valid = true;
    115 }
    116 
    117 static int
    118 pwdog_detach(device_t self, int flags)
    119 {
    120 	struct pwdog_softc *sc = device_private(self);
    121 
    122 	if (sc->sc_smw_valid) {
    123 		if ((sc->sc_smw.smw_mode & WDOG_MODE_MASK)
    124 		    != WDOG_MODE_DISARMED)
    125 			return EBUSY;
    126 
    127 		sysmon_wdog_unregister(&sc->sc_smw);
    128 		sc->sc_smw_valid = false;
    129 	}
    130 
    131 	pmf_device_deregister(self);
    132 
    133 	if (sc->sc_iosize)
    134 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize);
    135 	return 0;
    136 }
    137 
    138 static bool
    139 pwdog_suspend(device_t self, const pmf_qual_t *qual)
    140 {
    141 	struct pwdog_softc *sc = device_private(self);
    142 
    143 	if (sc->sc_smw_valid == false)
    144 		return true;
    145 
    146 	if ((sc->sc_smw.smw_mode & WDOG_MODE_MASK) != WDOG_MODE_DISARMED)
    147 		return false;
    148 
    149 	return true;
    150 }
    151 
    152 static int
    153 pwdog_setmode(struct sysmon_wdog *smw)
    154 {
    155 	struct pwdog_softc *sc = smw->smw_cookie;
    156 
    157 	switch (smw->smw_mode & WDOG_MODE_MASK) {
    158 	case WDOG_MODE_DISARMED:
    159 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, PWDOG_DISABLE, 0);
    160 		break;
    161 	default:
    162 		/* NB:  the timer period set by the user is ignored */
    163 		bus_space_write_1(sc->sc_iot, sc->sc_ioh, PWDOG_ACTIVATE, 0);
    164 	}
    165 	return 0;
    166 }
    167 
    168 static int
    169 pwdog_tickle(struct sysmon_wdog *smw)
    170 {
    171 	struct pwdog_softc *sc = smw->smw_cookie;
    172 
    173 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, PWDOG_ACTIVATE, 0);
    174 	return 0;
    175 }
    176 
    177 #ifdef _MODULE
    178 MODULE(MODULE_CLASS_DRIVER, pwdog, NULL);
    179 
    180 #include "ioconf.c"
    181 
    182 static int
    183 pwdog_modcmd(modcmd_t cmd, void *opaque)
    184 {
    185 	int error;
    186 
    187 	switch (cmd) {
    188 	case MODULE_CMD_INIT:
    189 		error = config_init_component(cfdriver_ioconf_pwdog,
    190 		    cfattach_ioconf_pwdog, cfdata_ioconf_pwdog);
    191 		if (error) {
    192 			aprint_error("%s: unable to init component\n",
    193 			    pwdog_cd.cd_name);
    194 			return error;
    195 		}
    196 		return 0;
    197 	case MODULE_CMD_FINI:
    198 		config_fini_component(cfdriver_ioconf_pwdog,
    199 		    cfattach_ioconf_pwdog, cfdata_ioconf_pwdog);
    200 		return 0;
    201 	default:
    202 		return ENOTTY;
    203 	}
    204 }
    205 
    206 #endif /* _MODULE */
    207