xref: /openbsd/sys/arch/arm64/dev/apldog.c (revision 73471bf0)
1 /*	$OpenBSD: apldog.c,v 1.2 2021/11/13 23:24:24 kettenis Exp $	*/
2 /*
3  * Copyright (c) 2021 Mark Kettenis <kettenis@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <sys/param.h>
19 #include <sys/systm.h>
20 #include <sys/device.h>
21 
22 #include <machine/bus.h>
23 #include <machine/fdt.h>
24 
25 #include <dev/ofw/openfirm.h>
26 #include <dev/ofw/fdt.h>
27 
28 extern void (*cpuresetfn)(void);
29 
30 /*
31  * This driver is based on preliminary device tree bindings and will
32  * almost certainly need changes once the official bindings land in
33  * mainline Linux.  Support for these preliminary bindings will be
34  * dropped as soon as official bindings are available.
35  */
36 
37 #define WDT_CHIP_CTL	0x000c
38 #define WDT_SYS_TMR	0x0010
39 #define WDT_SYS_RST	0x0014
40 #define  WDT_SYS_RST_IMMEDIATE	(1 << 0)
41 #define WDT_SYS_CTL	0x001c
42 #define  WDT_SYS_CTL_ENABLE	(1 << 2)
43 
44 #define HREAD4(sc, reg)							\
45 	(bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg)))
46 #define HWRITE4(sc, reg, val)						\
47 	bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
48 
49 struct apldog_softc {
50 	struct device		sc_dev;
51 	bus_space_tag_t		sc_iot;
52 	bus_space_handle_t	sc_ioh;
53 };
54 
55 struct apldog_softc *apldog_sc;
56 
57 int	apldog_match(struct device *, void *, void *);
58 void	apldog_attach(struct device *, struct device *, void *);
59 
60 struct cfattach	apldog_ca = {
61 	sizeof (struct apldog_softc), apldog_match, apldog_attach
62 };
63 
64 struct cfdriver apldog_cd = {
65 	NULL, "apldog", DV_DULL
66 };
67 
68 void	apldog_reset(void);
69 
70 int
71 apldog_match(struct device *parent, void *match, void *aux)
72 {
73 	struct fdt_attach_args *faa = aux;
74 
75 	return OF_is_compatible(faa->fa_node, "apple,wdt");
76 }
77 
78 void
79 apldog_attach(struct device *parent, struct device *self, void *aux)
80 {
81 	struct apldog_softc *sc = (struct apldog_softc *)self;
82 	struct fdt_attach_args *faa = aux;
83 
84 	if (faa->fa_nreg < 1) {
85 		printf(": no registers\n");
86 		return;
87 	}
88 
89 	sc->sc_iot = faa->fa_iot;
90 	if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr,
91 	    faa->fa_reg[0].size, 0, &sc->sc_ioh)) {
92 		printf(": can't map registers\n");
93 		return;
94 	}
95 
96 	printf("\n");
97 
98 	/* Reset the watchdog timers. */
99 	HWRITE4(sc, WDT_CHIP_CTL, 0);
100 	HWRITE4(sc, WDT_SYS_CTL, 0);
101 
102 	apldog_sc = sc;
103 	cpuresetfn = apldog_reset;
104 }
105 
106 void
107 apldog_reset(void)
108 {
109 	struct apldog_softc *sc = apldog_sc;
110 
111 	/* Trigger a system reset. */
112 	HWRITE4(sc, WDT_SYS_RST, WDT_SYS_RST_IMMEDIATE);
113 	HWRITE4(sc, WDT_SYS_CTL, WDT_SYS_CTL_ENABLE);
114 	HWRITE4(sc, WDT_SYS_TMR, 0);
115 
116 	delay(1000000);
117 }
118