1 /* $OpenBSD: mvodog.c,v 1.1 2023/03/02 09:56:52 jmatthew Exp $ */
2 /*
3 * Copyright (c) 2022 Jonathan Matthew <jmatthew@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 #include <machine/bus.h>
22 #include <machine/fdt.h>
23
24 #include <dev/ofw/openfirm.h>
25 #include <dev/ofw/ofw_misc.h>
26 #include <dev/ofw/fdt.h>
27
28 #define WDT_ENABLE (1 << 8)
29
30 #define RSTOUT_ENABLE (1 << 8)
31 #define RSTOUT_MASK (1 << 10)
32
33 #define HREAD4(sc, ioh, reg) \
34 (bus_space_read_4((sc)->sc_iot, (ioh), (reg)))
35 #define HWRITE4(sc, ioh, reg, val) \
36 bus_space_write_4((sc)->sc_iot, (ioh), (reg), (val))
37 #define HSET4(sc, ioh, reg, bits) \
38 HWRITE4((sc), (ioh), (reg), HREAD4((sc), (ioh), (reg)) | (bits))
39 #define HCLR4(sc, ioh, reg, bits) \
40 HWRITE4((sc), (ioh), (reg), HREAD4((sc), (ioh), (reg)) & ~(bits))
41
42 struct mvodog_softc {
43 struct device sc_dev;
44 bus_space_tag_t sc_iot;
45 bus_space_handle_t sc_reg_ioh;
46 bus_space_handle_t sc_rstout_ioh;
47 bus_space_handle_t sc_rstout_mask_ioh;
48 };
49
50 int mvodog_match(struct device *, void *, void *);
51 void mvodog_attach(struct device *, struct device *, void *);
52
53 const struct cfattach mvodog_ca = {
54 sizeof (struct mvodog_softc), mvodog_match, mvodog_attach
55 };
56
57 struct cfdriver mvodog_cd = {
58 NULL, "mvodog", DV_DULL
59 };
60
61 int
mvodog_match(struct device * parent,void * cfdata,void * aux)62 mvodog_match(struct device *parent, void *cfdata, void *aux)
63 {
64 struct fdt_attach_args *faa = aux;
65
66 return OF_is_compatible(faa->fa_node, "marvell,armada-380-wdt");
67 }
68
69 void
mvodog_attach(struct device * parent,struct device * self,void * aux)70 mvodog_attach(struct device *parent, struct device *self, void *aux)
71 {
72 struct mvodog_softc *sc = (struct mvodog_softc *)self;
73 struct fdt_attach_args *faa = aux;
74
75 if (faa->fa_nreg < 1) {
76 printf(": no registers\n");
77 return;
78 }
79
80 sc->sc_iot = faa->fa_iot;
81 if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr,
82 faa->fa_reg[0].size, 0, &sc->sc_reg_ioh)) {
83 printf(": can't map registers\n");
84 return;
85 }
86
87 if (bus_space_map(sc->sc_iot, faa->fa_reg[1].addr,
88 faa->fa_reg[1].size, 0, &sc->sc_rstout_ioh)) {
89 printf(": can't map rstout\n");
90 return;
91 }
92
93 if (bus_space_map(sc->sc_iot, faa->fa_reg[2].addr,
94 faa->fa_reg[2].size, 0, &sc->sc_rstout_mask_ioh)) {
95 printf(": can't map rstout mask\n");
96 return;
97 }
98
99 printf("\n");
100
101 /* Disable watchdog timer. */
102 HSET4(sc, sc->sc_rstout_mask_ioh, 0, RSTOUT_MASK);
103 HCLR4(sc, sc->sc_rstout_ioh, 0, RSTOUT_ENABLE);
104 HCLR4(sc, sc->sc_reg_ioh, 0, WDT_ENABLE);
105 }
106