xref: /openbsd/sys/dev/pci/pwdog.c (revision 09467b48)
1 /*	$OpenBSD: pwdog.c,v 1.10 2017/09/08 05:36:52 deraadt Exp $ */
2 
3 /*
4  * Copyright (c) 2006 Marc Balmer <mbalmer@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/param.h>
20 #include <sys/device.h>
21 #include <sys/kernel.h>
22 #include <sys/systm.h>
23 
24 #include <machine/bus.h>
25 
26 #include <dev/pci/pcivar.h>
27 #include <dev/pci/pcireg.h>
28 #include <dev/pci/pcidevs.h>
29 
30 struct pwdog_softc {
31 	struct device		pwdog_dev;
32 	bus_space_tag_t		iot;
33 	bus_space_handle_t	ioh;
34 };
35 
36 /* registers */
37 #define PWDOG_ACTIVATE	0
38 #define PWDOG_DISABLE	1
39 
40 int pwdog_probe(struct device *, void *, void *);
41 void pwdog_attach(struct device *, struct device *, void *);
42 int pwdog_activate(struct device *, int);
43 int pwdog_set_timeout(void *, int);
44 
45 struct cfattach pwdog_ca = {
46 	sizeof(struct pwdog_softc), pwdog_probe, pwdog_attach,
47 	NULL, pwdog_activate
48 };
49 
50 struct cfdriver pwdog_cd = {
51 	NULL, "pwdog", DV_DULL
52 };
53 
54 const struct pci_matchid pwdog_devices[] = {
55 	{ PCI_VENDOR_QUANCOM, PCI_PRODUCT_QUANCOM_PWDOG1 }
56 };
57 
58 int
59 pwdog_probe(struct device *parent, void *match, void *aux)
60 {
61 	return pci_matchbyid((struct pci_attach_args *)aux, pwdog_devices,
62 	    sizeof(pwdog_devices) / sizeof(pwdog_devices[0]));
63 }
64 
65 void
66 pwdog_attach(struct device *parent, struct device *self, void *aux)
67 {
68 	struct pwdog_softc *pwdog = (struct pwdog_softc *)self;
69 	struct pci_attach_args *const pa = (struct pci_attach_args *)aux;
70 	pcireg_t memtype;
71 	bus_size_t iosize;
72 
73 	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, PCI_MAPREG_START);
74 	if (pci_mapreg_map(pa, PCI_MAPREG_START, memtype, 0, &pwdog->iot,
75 	    &pwdog->ioh, NULL, &iosize, 0)) {
76 		printf("\n%s: PCI %s region not found\n",
77 		    pwdog->pwdog_dev.dv_xname,
78 		    memtype == PCI_MAPREG_TYPE_IO ? "I/O" : "memory");
79 		return;
80 	}
81 	printf("\n");
82 	bus_space_write_1(pwdog->iot, pwdog->ioh, PWDOG_DISABLE, 0);
83 	wdog_register(pwdog_set_timeout, pwdog);
84 }
85 
86 int
87 pwdog_activate(struct device *self, int act)
88 {
89 	switch (act) {
90 	case DVACT_POWERDOWN:
91 		wdog_shutdown(self);
92 		break;
93 	}
94 
95 	return (0);
96 }
97 
98 int
99 pwdog_set_timeout(void *self, int seconds)
100 {
101 	struct pwdog_softc *pwdog = (struct pwdog_softc *)self;
102 	int s;
103 
104 	s = splclock();
105 	if (seconds)
106 		bus_space_write_1(pwdog->iot, pwdog->ioh, PWDOG_ACTIVATE, 0);
107 	else
108 		bus_space_write_1(pwdog->iot, pwdog->ioh, PWDOG_DISABLE, 0);
109 	splx(s);
110 	return seconds;
111 }
112