xref: /openbsd-src/sys/arch/arm64/dev/aplsart.c (revision 25c4e8bd056e974b28f4a0ffd39d76c190a56013)
1 /*	$OpenBSD: aplsart.c,v 1.2 2022/06/13 12:40:30 kettenis Exp $	*/
2 /*
3  * Copyright (c) 2022 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 #include <sys/extent.h>
22 #include <sys/malloc.h>
23 #include <sys/mutex.h>
24 
25 #include <machine/intr.h>
26 #include <machine/bus.h>
27 #include <machine/fdt.h>
28 
29 #include <dev/ofw/openfirm.h>
30 #include <dev/ofw/fdt.h>
31 
32 #define SART2_CONFIG(idx)	(0x0000 + 4 * (idx))
33 #define  SART2_CONFIG_FLAGS_MASK	0xff000000
34 #define  SART2_CONFIG_FLAGS_ALLOW	0xff000000
35 #define SART2_ADDR(idx)		(0x0040 + 4 * (idx))
36 
37 #define SART3_CONFIG(idx)	(0x0000 + 4 * (idx))
38 #define  SART3_CONFIG_FLAGS_MASK	0x000000ff
39 #define  SART3_CONFIG_FLAGS_ALLOW	0x000000ff
40 #define SART3_ADDR(idx)		(0x0040 + 4 * (idx))
41 #define SART3_SIZE(idx)		(0x0080 + 4 * (idx))
42 
43 #define SART_NUM_ENTRIES	16
44 #define SART_ADDR_SHIFT		12
45 #define SART_SIZE_SHIFT		12
46 
47 #define HREAD4(sc, reg)							\
48 	(bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg)))
49 #define HWRITE4(sc, reg, val)						\
50 	bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val))
51 
52 struct aplsart_softc {
53 	struct device		sc_dev;
54 	bus_space_tag_t		sc_iot;
55 	bus_space_handle_t	sc_ioh;
56 	uint32_t		sc_phandle;
57 	int			sc_version;
58 };
59 
60 int	aplsart_match(struct device *, void *, void *);
61 void	aplsart_attach(struct device *, struct device *, void *);
62 
63 const struct cfattach aplsart_ca = {
64 	sizeof (struct aplsart_softc), aplsart_match, aplsart_attach
65 };
66 
67 struct cfdriver aplsart_cd = {
68 	NULL, "aplsart", DV_DULL
69 };
70 
71 int
72 aplsart_match(struct device *parent, void *match, void *aux)
73 {
74 	struct fdt_attach_args *faa = aux;
75 
76 	return OF_is_compatible(faa->fa_node, "apple,t6000-sart") ||
77 	    OF_is_compatible(faa->fa_node, "apple,t8103-sart");
78 }
79 
80 void
81 aplsart_attach(struct device *parent, struct device *self, void *aux)
82 {
83 	struct aplsart_softc *sc = (struct aplsart_softc *)self;
84 	struct fdt_attach_args *faa = aux;
85 
86 	if (faa->fa_nreg < 1) {
87 		printf(": no registers\n");
88 		return;
89 	}
90 
91 	sc->sc_iot = faa->fa_iot;
92 	if (bus_space_map(sc->sc_iot, faa->fa_reg[0].addr,
93 	    faa->fa_reg[0].size, 0, &sc->sc_ioh)) {
94 		printf(": can't map registers\n");
95 		return;
96 	}
97 
98 	sc->sc_phandle = OF_getpropint(faa->fa_node, "phandle", 0);
99 
100 	if (OF_is_compatible(faa->fa_node, "apple,t8103-sart"))
101 		sc->sc_version = 2;
102 	if (OF_is_compatible(faa->fa_node, "apple,t6000-sart"))
103 		sc->sc_version = 3;
104 
105 	printf("\n");
106 }
107 
108 int
109 aplsart2_map(struct aplsart_softc *sc, bus_addr_t addr, bus_size_t size)
110 {
111 	uint32_t conf;
112 	int i;
113 
114 	for (i = 0; i < SART_NUM_ENTRIES; i++) {
115 		conf = HREAD4(sc, SART2_CONFIG(i));
116 		if (conf & SART2_CONFIG_FLAGS_MASK)
117 			continue;
118 
119 		HWRITE4(sc, SART2_ADDR(i), addr >> SART_ADDR_SHIFT);
120 		HWRITE4(sc, SART2_CONFIG(i),
121 		    size >> SART_SIZE_SHIFT | SART2_CONFIG_FLAGS_ALLOW);
122 		return 0;
123 	}
124 
125 	return ENOENT;
126 }
127 
128 int
129 aplsart3_map(struct aplsart_softc *sc, bus_addr_t addr, bus_size_t size)
130 {
131 	uint32_t conf;
132 	int i;
133 
134 	for (i = 0; i < SART_NUM_ENTRIES; i++) {
135 		conf = HREAD4(sc, SART3_CONFIG(i));
136 		if (conf & SART3_CONFIG_FLAGS_MASK)
137 			continue;
138 
139 		HWRITE4(sc, SART3_ADDR(i), addr >> SART_ADDR_SHIFT);
140 		HWRITE4(sc, SART3_SIZE(i), size >> SART_SIZE_SHIFT);
141 		HWRITE4(sc, SART3_CONFIG(i), SART3_CONFIG_FLAGS_ALLOW);
142 		return 0;
143 	}
144 
145 	return ENOENT;
146 }
147 
148 int
149 aplsart_map(uint32_t phandle, bus_addr_t addr, bus_size_t size)
150 {
151 	struct aplsart_softc *sc;
152 	int i;
153 
154 	for (i = 0; i < aplsart_cd.cd_ndevs; i++) {
155 		sc = (struct aplsart_softc *)aplsart_cd.cd_devs[i];
156 
157 		if (sc->sc_phandle == phandle) {
158 			if (sc->sc_version == 2)
159 				return aplsart2_map(sc, addr, size);
160 			else
161 				return aplsart3_map(sc, addr, size);
162 		}
163 	}
164 
165 	return ENXIO;
166 }
167