1 /* $OpenBSD: dwdog.c,v 1.3 2021/10/24 17:52:26 mpi Exp $ */
2 /*
3 * Copyright (c) 2017 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 <armv7/armv7/armv7_machdep.h>
26
27 #include <dev/ofw/openfirm.h>
28 #include <dev/ofw/fdt.h>
29
30 /* Registers */
31 #define WDT_CR 0x0000
32 #define WDT_CR_WDT_EN (1 << 0)
33 #define WDT_CR_RESP_MODE (1 << 1)
34 #define WDT_TORR 0x0004
35 #define WDT_CCVR 0x0008
36 #define WDT_CRR 0x000c
37 #define WDT_CRR_KICK 0x76
38 #define WDT_STAT 0x0010
39 #define WDT_EOI 0x0014
40
41 #define HREAD4(sc, reg) \
42 (bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg)))
43 #define HWRITE4(sc, reg, val) \
44 bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
45 #define HSET4(sc, reg, bits) \
46 HWRITE4((sc), (reg), HREAD4((sc), (reg)) | (bits))
47 #define HCLR4(sc, reg, bits) \
48 HWRITE4((sc), (reg), HREAD4((sc), (reg)) & ~(bits))
49
50 struct dwdog_softc {
51 struct device sc_dev;
52 bus_space_tag_t sc_iot;
53 bus_space_handle_t sc_ioh;
54 };
55
56 int dwdog_match(struct device *, void *, void *);
57 void dwdog_attach(struct device *, struct device *, void *);
58
59 const struct cfattach dwdog_ca = {
60 sizeof(struct dwdog_softc), dwdog_match, dwdog_attach
61 };
62
63 struct cfdriver dwdog_cd = {
64 NULL, "dwdog", DV_DULL
65 };
66
67 void dwdog_reset(void);
68
69 int
dwdog_match(struct device * parent,void * match,void * aux)70 dwdog_match(struct device *parent, void *match, void *aux)
71 {
72 struct fdt_attach_args *faa = aux;
73
74 return OF_is_compatible(faa->fa_node, "snps,dw-wdt");
75 }
76
77 void
dwdog_attach(struct device * parent,struct device * self,void * aux)78 dwdog_attach(struct device *parent, struct device *self, void *aux)
79 {
80 struct dwdog_softc *sc = (struct dwdog_softc *)self;
81 struct fdt_attach_args *faa = aux;
82
83 if (faa->fa_nreg < 1) {
84 printf(": no registers\n");
85 return;
86 }
87
88 sc->sc_iot = faa->fa_iot;
89
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 }
94
95 printf("\n");
96
97 if (cpuresetfn == NULL)
98 cpuresetfn = dwdog_reset;
99 }
100
101 void
dwdog_reset(void)102 dwdog_reset(void)
103 {
104 struct dwdog_softc *sc = dwdog_cd.cd_devs[0];
105
106 /*
107 * Generate system reset when timer expires and select
108 * smallest timeout.
109 */
110 HCLR4(sc, WDT_CR, WDT_CR_RESP_MODE | WDT_CR_WDT_EN);
111 HWRITE4(sc, WDT_TORR, 0);
112 HSET4(sc, WDT_CR, WDT_CR_WDT_EN);
113
114 /* Kick the dog! */
115 HWRITE4(sc, WDT_CRR, WDT_CRR_KICK);
116
117 delay(1000000);
118 }
119