xref: /openbsd/sys/dev/fdt/mvdog.c (revision 73471bf0)
1 /* $OpenBSD: mvdog.c,v 1.2 2021/10/24 17:52:26 mpi Exp $ */
2 /*
3  * Copyright (c) 2019 Patrick Wildt <patrick@blueri.se>
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 CNTR_RETRIGGER		0
29 #define CNTR_WDOG		1
30 
31 #define CNTR_CTRL(x)		((x) * 0x10)
32 #define  CNTR_CTRL_ENABLE		(1 << 0)
33 
34 #define WDT_TIMER_SELECT	0x64
35 
36 #define HREAD4(sc, reg)							\
37 	(bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg)))
38 #define HWRITE4(sc, reg, val)						\
39 	bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
40 #define HSET4(sc, reg, bits)						\
41 	HWRITE4((sc), (reg), HREAD4((sc), (reg)) | (bits))
42 #define HCLR4(sc, reg, bits)						\
43 	HWRITE4((sc), (reg), HREAD4((sc), (reg)) & ~(bits))
44 
45 struct mvdog_softc {
46 	struct device		 sc_dev;
47 	bus_space_tag_t		 sc_iot;
48 	bus_space_handle_t	 sc_ioh;
49 	struct regmap		*sc_rm;
50 };
51 
52 int	 mvdog_match(struct device *, void *, void *);
53 void	 mvdog_attach(struct device *, struct device *, void *);
54 
55 const struct cfattach mvdog_ca = {
56 	sizeof (struct mvdog_softc), mvdog_match, mvdog_attach
57 };
58 
59 struct cfdriver mvdog_cd = {
60 	NULL, "mvdog", DV_DULL
61 };
62 
63 int
64 mvdog_match(struct device *parent, void *cfdata, void *aux)
65 {
66 	struct fdt_attach_args *faa = aux;
67 
68 	return OF_is_compatible(faa->fa_node, "marvell,armada-3700-wdt");
69 }
70 
71 void
72 mvdog_attach(struct device *parent, struct device *self, void *aux)
73 {
74 	struct mvdog_softc *sc = (struct mvdog_softc *)self;
75 	struct fdt_attach_args *faa = aux;
76 
77 	if (faa->fa_nreg < 1) {
78 		printf(": no registers\n");
79 		return;
80 	}
81 
82 	sc->sc_iot = faa->fa_iot;
83 	if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr,
84 	    faa->fa_reg[0].size, 0, &sc->sc_ioh)) {
85 		printf(": can't map registers\n");
86 		return;
87 	}
88 
89 	sc->sc_rm = regmap_byphandle(OF_getpropint(faa->fa_node,
90 	    "marvell,system-controller", 0));
91 	if (sc->sc_rm == NULL) {
92 		printf(": can't get regmap\n");
93 		return;
94 	}
95 
96 	printf("\n");
97 
98 	/* Disable watchdog timer. */
99 	HCLR4(sc, CNTR_CTRL(CNTR_WDOG), CNTR_CTRL_ENABLE);
100 	HCLR4(sc, CNTR_CTRL(CNTR_RETRIGGER), CNTR_CTRL_ENABLE);
101 	regmap_write_4(sc->sc_rm, WDT_TIMER_SELECT, 0);
102 }
103