xref: /openbsd/sys/dev/fdt/rkgrf.c (revision d89ec533)
1 /*	$OpenBSD: rkgrf.c,v 1.5 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/intr.h>
23 #include <machine/bus.h>
24 #include <machine/fdt.h>
25 
26 #include <dev/ofw/openfirm.h>
27 #include <dev/ofw/ofw_misc.h>
28 #include <dev/ofw/fdt.h>
29 
30 #ifdef __armv7__
31 #include <arm/simplebus/simplebusvar.h>
32 #else
33 #include <arm64/dev/simplebusvar.h>
34 #endif
35 
36 struct rkgrf_softc {
37 	struct simplebus_softc	sc_sbus;
38 	bus_space_tag_t		sc_iot;
39 	bus_space_handle_t	sc_ioh;
40 };
41 
42 int rkgrf_match(struct device *, void *, void *);
43 void rkgrf_attach(struct device *, struct device *, void *);
44 
45 const struct cfattach	rkgrf_ca = {
46 	sizeof (struct rkgrf_softc), rkgrf_match, rkgrf_attach
47 };
48 
49 struct cfdriver rkgrf_cd = {
50 	NULL, "rkgrf", DV_DULL
51 };
52 
53 int
54 rkgrf_match(struct device *parent, void *match, void *aux)
55 {
56 	struct fdt_attach_args *faa = aux;
57 
58 	return (OF_is_compatible(faa->fa_node, "rockchip,rk3288-grf") ||
59 	    OF_is_compatible(faa->fa_node, "rockchip,rk3288-pmu") ||
60 	    OF_is_compatible(faa->fa_node, "rockchip,rk3288-sgrf") ||
61 	    OF_is_compatible(faa->fa_node, "rockchip,rk3308-grf") ||
62 	    OF_is_compatible(faa->fa_node, "rockchip,rk3399-grf") ||
63 	    OF_is_compatible(faa->fa_node, "rockchip,rk3399-pmugrf"));
64 }
65 
66 void
67 rkgrf_attach(struct device *parent, struct device *self, void *aux)
68 {
69 	struct rkgrf_softc *sc = (struct rkgrf_softc *)self;
70 	struct fdt_attach_args *faa = aux;
71 
72 	if (faa->fa_nreg < 1) {
73 		printf(": no registers\n");
74 		return;
75 	}
76 
77 	sc->sc_iot = faa->fa_iot;
78 
79 	if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr,
80 	    faa->fa_reg[0].size, 0, &sc->sc_ioh)) {
81 		printf(": can't map registers\n");
82 		return;
83 	}
84 
85 	printf("\n");
86 
87 	regmap_register(faa->fa_node, sc->sc_iot, sc->sc_ioh,
88 	    faa->fa_reg[0].size);
89 
90 	/* Attach PHYs. */
91 	simplebus_attach(parent, &sc->sc_sbus.sc_dev, faa);
92 }
93