xref: /openbsd/sys/dev/fdt/sxisyscon.c (revision 22652d99)
1 /*	$OpenBSD: sxisyscon.c,v 1.3 2024/02/07 22:00:38 uaa Exp $	*/
2 /*
3  * Copyright (c) 2019 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/ofw_misc.h>
27 #include <dev/ofw/fdt.h>
28 
29 struct sxisyscon_softc {
30 	struct device		sc_dev;
31 	bus_space_tag_t		sc_iot;
32 	bus_space_handle_t	sc_ioh;
33 };
34 
35 int	sxisyscon_match(struct device *, void *, void *);
36 void	sxisyscon_attach(struct device *, struct device *, void *);
37 
38 const struct cfattach sxisyscon_ca = {
39 	sizeof(struct sxisyscon_softc), sxisyscon_match, sxisyscon_attach
40 };
41 
42 struct cfdriver sxisyscon_cd = {
43 	NULL, "sxisyscon", DV_DULL
44 };
45 
46 int
sxisyscon_match(struct device * parent,void * match,void * aux)47 sxisyscon_match(struct device *parent, void *match, void *aux)
48 {
49 	struct fdt_attach_args *faa = aux;
50 	int node = faa->fa_node;
51 
52 	if (OF_is_compatible(node, "allwinner,sun8i-h3-system-control") ||
53 	    OF_is_compatible(node, "allwinner,sun50i-a64-system-control") ||
54 	    OF_is_compatible(node, "allwinner,sun50i-h5-system-control") ||
55 	    OF_is_compatible(node, "allwinner,sun50i-h6-system-control") ||
56 	    OF_is_compatible(node, "allwinner,sun50i-h616-system-control"))
57 		return 10;	/* Must beat syscon(4). */
58 
59 	return 0;
60 }
61 
62 void
sxisyscon_attach(struct device * parent,struct device * self,void * aux)63 sxisyscon_attach(struct device *parent, struct device *self, void *aux)
64 {
65 	struct sxisyscon_softc *sc = (struct sxisyscon_softc *)self;
66 	struct fdt_attach_args *faa = aux;
67 
68 	if (faa->fa_nreg < 1) {
69 		printf(": no registers\n");
70 		return;
71 	}
72 
73 	sc->sc_iot = faa->fa_iot;
74 	if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr,
75 	    faa->fa_reg[0].size, 0, &sc->sc_ioh)) {
76 		printf(": can't map registers\n");
77 		return;
78 	}
79 
80 	regmap_register(faa->fa_node, sc->sc_iot, sc->sc_ioh,
81 	    faa->fa_reg[0].size);
82 
83 	printf("\n");
84 }
85