1/*	$OpenBSD: pmc.c,v 1.5 2021/10/24 17:05:04 mpi Exp $	*/
2
3/*
4 * Copyright (c) 2007 Mark Kettenis
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/*
20 * Driver for watchdog device on Blade 1000, Fire 280R, Fire V480 etc.
21 */
22
23#include <sys/param.h>
24#include <sys/kernel.h>
25#include <sys/device.h>
26#include <sys/malloc.h>
27#include <sys/systm.h>
28
29#include <machine/bus.h>
30#include <machine/autoconf.h>
31
32#include <sparc64/dev/ebusreg.h>
33#include <sparc64/dev/ebusvar.h>
34
35/*
36 * Register access is indirect, through an address and data port.
37 */
38
39#define	PMC_ADDR	0
40#define	PMC_DATA	1
41
42/* Watchdog time-out register. */
43#define PMC_WDTO	0x05
44
45struct pmc_softc {
46	struct device		sc_dv;
47	bus_space_tag_t		sc_iot;
48	bus_space_handle_t	sc_ioh;
49};
50
51int	pmc_match(struct device *, void *, void *);
52void	pmc_attach(struct device *, struct device *, void *);
53int	pmc_activate(struct device *, int);
54int	pmc_wdog_cb(void *, int);
55
56const struct cfattach pmc_ca = {
57	sizeof(struct pmc_softc), pmc_match, pmc_attach, NULL, pmc_activate
58};
59
60struct cfdriver pmc_cd = {
61	NULL, "pmc", DV_DULL
62};
63
64int
65pmc_match(struct device *parent, void *cf, void *aux)
66{
67	struct ebus_attach_args *ea = aux;
68
69	if (strcmp("pmc", ea->ea_name) == 0)
70		return (1);
71	return (0);
72}
73
74void
75pmc_attach(struct device *parent, struct device *self, void *aux)
76{
77	struct pmc_softc *sc = (void *)self;
78	struct ebus_attach_args *ea = aux;
79
80	/* Use prom address if available, otherwise map it. */
81	if (ea->ea_nvaddrs) {
82		if (bus_space_map(ea->ea_memtag, ea->ea_vaddrs[0], 0,
83		    BUS_SPACE_MAP_PROMADDRESS, &sc->sc_ioh)) {
84			printf(": can't map PROM register space\n");
85			return;
86		}
87		sc->sc_iot = ea->ea_memtag;
88	} else if (ebus_bus_map(ea->ea_iotag, 0,
89	    EBUS_PADDR_FROM_REG(&ea->ea_regs[0]),
90	    ea->ea_regs[0].size, 0, 0, &sc->sc_ioh) == 0) {
91		sc->sc_iot = ea->ea_iotag;
92	} else if (ebus_bus_map(ea->ea_memtag, 0,
93	    EBUS_PADDR_FROM_REG(&ea->ea_regs[0]),
94	    ea->ea_regs[0].size, 0, 0, &sc->sc_ioh) == 0) {
95		sc->sc_iot = ea->ea_memtag;
96	} else {
97		printf(": can't map register space\n");
98		return;
99	}
100
101	printf("\n");
102
103	wdog_register(pmc_wdog_cb, sc);
104}
105
106int
107pmc_activate(struct device *self, int act)
108{
109	switch (act) {
110	case DVACT_POWERDOWN:
111		wdog_shutdown(self);
112		break;
113	}
114
115	return (0);
116}
117
118int
119pmc_wdog_cb(void *arg, int period)
120{
121	struct pmc_softc *sc = arg;
122	int mins;
123
124	mins = (period + 59) / 60;
125	if (mins > 255)
126		mins = 255;
127
128	bus_space_write_1(sc->sc_iot, sc->sc_ioh, PMC_ADDR, PMC_WDTO);
129	bus_space_write_1(sc->sc_iot, sc->sc_ioh, PMC_DATA, mins);
130
131	return (mins * 60);
132}
133