1/*	$OpenBSD: pwdog.c,v 1.13 2024/05/24 06:02:58 jsg 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/systm.h>
22
23#include <machine/bus.h>
24
25#include <dev/pci/pcivar.h>
26#include <dev/pci/pcireg.h>
27#include <dev/pci/pcidevs.h>
28
29struct pwdog_softc {
30	struct device		pwdog_dev;
31	bus_space_tag_t		iot;
32	bus_space_handle_t	ioh;
33};
34
35/* registers */
36#define PWDOG_ACTIVATE	0
37#define PWDOG_DISABLE	1
38
39int pwdog_probe(struct device *, void *, void *);
40void pwdog_attach(struct device *, struct device *, void *);
41int pwdog_activate(struct device *, int);
42int pwdog_set_timeout(void *, int);
43
44const struct cfattach pwdog_ca = {
45	sizeof(struct pwdog_softc), pwdog_probe, pwdog_attach,
46	NULL, pwdog_activate
47};
48
49struct cfdriver pwdog_cd = {
50	NULL, "pwdog", DV_DULL
51};
52
53const struct pci_matchid pwdog_devices[] = {
54	{ PCI_VENDOR_QUANCOM, PCI_PRODUCT_QUANCOM_PWDOG1 }
55};
56
57int
58pwdog_probe(struct device *parent, void *match, void *aux)
59{
60	return pci_matchbyid((struct pci_attach_args *)aux, pwdog_devices,
61	    sizeof(pwdog_devices) / sizeof(pwdog_devices[0]));
62}
63
64void
65pwdog_attach(struct device *parent, struct device *self, void *aux)
66{
67	struct pwdog_softc *pwdog = (struct pwdog_softc *)self;
68	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
69	pcireg_t memtype;
70	bus_size_t iosize;
71
72	memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, PCI_MAPREG_START);
73	if (pci_mapreg_map(pa, PCI_MAPREG_START, memtype, 0, &pwdog->iot,
74	    &pwdog->ioh, NULL, &iosize, 0)) {
75		printf("\n%s: PCI %s region not found\n",
76		    pwdog->pwdog_dev.dv_xname,
77		    memtype == PCI_MAPREG_TYPE_IO ? "I/O" : "memory");
78		return;
79	}
80	printf("\n");
81	bus_space_write_1(pwdog->iot, pwdog->ioh, PWDOG_DISABLE, 0);
82	wdog_register(pwdog_set_timeout, pwdog);
83}
84
85int
86pwdog_activate(struct device *self, int act)
87{
88	switch (act) {
89	case DVACT_POWERDOWN:
90		wdog_shutdown(self);
91		break;
92	}
93
94	return (0);
95}
96
97int
98pwdog_set_timeout(void *self, int seconds)
99{
100	struct pwdog_softc *pwdog = (struct pwdog_softc *)self;
101	int s;
102
103	s = splclock();
104	if (seconds)
105		bus_space_write_1(pwdog->iot, pwdog->ioh, PWDOG_ACTIVATE, 0);
106	else
107		bus_space_write_1(pwdog->iot, pwdog->ioh, PWDOG_DISABLE, 0);
108	splx(s);
109	return seconds;
110}
111