xref: /netbsd-src/sys/arch/sparc64/dev/upa.c (revision 3944ff70a48a86bb622139705a33f8ef461e24f7)
1 /*	$NetBSD: upa.c,v 1.24 2022/01/22 11:49:17 thorpej Exp $	*/
2 /*	$OpenBSD: upa.c,v 1.8 2008/01/17 22:53:18 kettenis Exp $	*/
3 
4 /*
5  * Copyright (c) 2002 Jason L. Wright (jason@thought.net)
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
21  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * Effort sponsored in part by the Defense Advanced Research Projects
30  * Agency (DARPA) and Air Force Research Laboratory, Air Force
31  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
32  */
33 
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: upa.c,v 1.24 2022/01/22 11:49:17 thorpej Exp $");
36 
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/device.h>
42 #include <sys/conf.h>
43 #include <sys/kmem.h>
44 
45 #include <sys/bus.h>
46 #include <machine/autoconf.h>
47 #include <machine/openfirm.h>
48 
49 struct upa_range {
50 	u_int64_t	ur_space;
51 	u_int64_t	ur_addr;
52 	u_int64_t	ur_len;
53 };
54 
55 struct upa_softc {
56 	device_t		sc_dev;
57 	bus_space_tag_t		sc_bt;
58 	bus_space_handle_t	sc_reg[3];
59 	struct upa_range	*sc_range;
60 	int			sc_node;
61 	int			sc_nrange;
62 	bus_space_tag_t		sc_cbt;
63 };
64 
65 int	upa_match(device_t, cfdata_t, void *);
66 void	upa_attach(device_t, device_t, void *);
67 
68 CFATTACH_DECL_NEW(upa, sizeof(struct upa_softc),
69     upa_match, upa_attach, NULL, NULL);
70 
71 int upa_print(void *, const char *);
72 bus_space_tag_t upa_alloc_bus_tag(struct upa_softc *);
73 int upa_bus_map(bus_space_tag_t, bus_addr_t,
74     bus_size_t, int, vaddr_t, bus_space_handle_t *);
75 paddr_t upa_bus_mmap(bus_space_tag_t, bus_addr_t, off_t, int, int);
76 
77 int
upa_match(device_t parent,cfdata_t match,void * aux)78 upa_match(device_t parent, cfdata_t match, void *aux)
79 {
80 	struct mainbus_attach_args *ma = aux;
81 
82 	if (strcmp(ma->ma_name, "upa") == 0)
83 		return (1);
84 
85 	return (0);
86 }
87 
88 void
upa_attach(device_t parent,device_t self,void * aux)89 upa_attach(device_t parent, device_t self, void *aux)
90 {
91 	struct upa_softc *sc = device_private(self);
92 	struct mainbus_attach_args *ma = aux;
93 	int i, node;
94 
95 	sc->sc_dev = self;
96 	sc->sc_bt = ma->ma_bustag;
97 	sc->sc_node = ma->ma_node;
98 
99 	for (i = 0; i < 3; i++) {
100 		if (i >= ma->ma_nreg) {
101 			printf(": no register %d\n", i);
102 			return;
103 		}
104 		if (bus_space_map(sc->sc_bt, ma->ma_reg[i].ur_paddr,
105 		    ma->ma_reg[i].ur_len, 0, &sc->sc_reg[i])) {
106 			printf(": failed to map reg%d\n", i);
107 			return;
108 		}
109 	}
110 
111 	if (prom_getprop(sc->sc_node, "ranges", sizeof(struct upa_range),
112 	    &sc->sc_nrange, (void **)&sc->sc_range))
113 		panic("upa: can't get ranges");
114 
115 	printf("\n");
116 
117 	sc->sc_cbt = upa_alloc_bus_tag(sc);
118 
119 	devhandle_t selfh = device_handle(sc->sc_dev);
120 	for (node = OF_child(sc->sc_node); node; node = OF_peer(node)) {
121 		char buf[32];
122 		struct mainbus_attach_args map;
123 
124 		bzero(&map, sizeof(map));
125 		if (OF_getprop(node, "device_type", buf, sizeof(buf)) <= 0)
126 			continue;
127 		if (prom_getprop(node, "reg", sizeof(*map.ma_reg),
128 		    &map.ma_nreg, (void **)&map.ma_reg) != 0)
129 			continue;
130 		if (OF_getprop(node, "name", buf, sizeof(buf)) <= 0)
131 			continue;
132 		map.ma_node = node;
133 		map.ma_name = buf;
134 		map.ma_bustag = sc->sc_cbt;
135 		map.ma_dmatag = ma->ma_dmatag;
136 		config_found(sc->sc_dev, &map, upa_print,
137 		    CFARGS(.devhandle = prom_node_to_devhandle(selfh, node)));
138 	}
139 }
140 
141 int
upa_print(void * args,const char * name)142 upa_print(void *args, const char *name)
143 {
144 	struct mainbus_attach_args *ma = args;
145 
146 	if (name)
147 		printf("\"%s\" at %s", ma->ma_name, name);
148 	return (UNCONF);
149 }
150 
151 bus_space_tag_t
upa_alloc_bus_tag(struct upa_softc * sc)152 upa_alloc_bus_tag(struct upa_softc *sc)
153 {
154 	struct sparc_bus_space_tag *bt;
155 
156 	bt = kmem_zalloc(sizeof(*bt), KM_SLEEP);
157 	*bt = *sc->sc_bt;
158 	bt->cookie = sc;
159 	bt->parent = sc->sc_bt;
160 	bt->sparc_bus_map = upa_bus_map;
161 	bt->sparc_bus_mmap = upa_bus_mmap;
162 
163 	return bt;
164 }
165 
166 int
upa_bus_map(bus_space_tag_t t,bus_addr_t offset,bus_size_t size,int flags,vaddr_t v,bus_space_handle_t * hp)167 upa_bus_map(bus_space_tag_t t, bus_addr_t offset,
168     bus_size_t size, int flags, vaddr_t v, bus_space_handle_t *hp)
169 {
170 	struct upa_softc *sc = t->cookie;
171 	int i;
172 
173 	if (t->parent == 0 || t->parent->sparc_bus_map == 0) {
174 		printf("\n__upa_bus_map: invalid parent");
175 		return (EINVAL);
176 	}
177 
178 	t = t->parent;
179 
180 	for (i = 0; i < sc->sc_nrange; i++) {
181 		if (offset < sc->sc_range[i].ur_space)
182 			continue;
183 		if (offset >= (sc->sc_range[i].ur_space +
184 		    sc->sc_range[i].ur_space))
185 			continue;
186 		break;
187 	}
188 	if (i == sc->sc_nrange)
189 		return (EINVAL);
190 
191 	offset -= sc->sc_range[i].ur_space;
192 	offset += sc->sc_range[i].ur_addr;
193 
194 	return ((*t->sparc_bus_map)(t, offset, size, flags, v, hp));
195 }
196 
197 paddr_t
upa_bus_mmap(bus_space_tag_t t,bus_addr_t paddr,off_t off,int prot,int flags)198 upa_bus_mmap(bus_space_tag_t t, bus_addr_t paddr, off_t off, int prot,
199     int flags)
200 {
201 	struct upa_softc *sc = t->cookie;
202 	int i;
203 
204 	if (t->parent == 0 || t->parent->sparc_bus_map == 0) {
205 		printf("\n__upa_bus_map: invalid parent");
206 		return (EINVAL);
207 	}
208 
209 	t = t->parent;
210 
211 	for (i = 0; i < sc->sc_nrange; i++) {
212 		if (paddr + off < sc->sc_range[i].ur_space)
213 			continue;
214 		if (paddr + off >= (sc->sc_range[i].ur_space +
215 		    sc->sc_range[i].ur_space))
216 			continue;
217 		break;
218 	}
219 	if (i == sc->sc_nrange)
220 		return (EINVAL);
221 
222 	paddr -= sc->sc_range[i].ur_space;
223 	paddr += sc->sc_range[i].ur_addr;
224 
225 	return ((*t->sparc_bus_mmap)(t, paddr, off, prot, flags));
226 }
227