xref: /netbsd-src/sys/arch/sparc64/dev/upa.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$OpenBSD: upa.c,v 1.8 2008/01/17 22:53:18 kettenis Exp $	*/
2 
3 /*
4  * Copyright (c) 2002 Jason L. Wright (jason@thought.net)
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  *
28  * Effort sponsored in part by the Defense Advanced Research Projects
29  * Agency (DARPA) and Air Force Research Laboratory, Air Force
30  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
31  *
32  */
33 
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: upa.c,v 1.18 2012/03/18 05:26:58 mrg 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/malloc.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
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
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 	for (node = OF_child(sc->sc_node); node; node = OF_peer(node)) {
120 		char buf[32];
121 		struct mainbus_attach_args map;
122 
123 		bzero(&map, sizeof(map));
124 		if (OF_getprop(node, "device_type", buf, sizeof(buf)) <= 0)
125 			continue;
126 		if (prom_getprop(node, "reg", sizeof(*map.ma_reg),
127 		    &map.ma_nreg, (void **)&map.ma_reg) != 0)
128 			continue;
129 		if (OF_getprop(node, "name", buf, sizeof(buf)) <= 0)
130 			continue;
131 		map.ma_node = node;
132 		map.ma_name = buf;
133 		map.ma_bustag = sc->sc_cbt;
134 		map.ma_dmatag = ma->ma_dmatag;
135 		config_found(sc->sc_dev, &map, upa_print);
136 	}
137 }
138 
139 int
140 upa_print(void *args, const char *name)
141 {
142 	struct mainbus_attach_args *ma = args;
143 
144 	if (name)
145 		printf("\"%s\" at %s", ma->ma_name, name);
146 	return (UNCONF);
147 }
148 
149 bus_space_tag_t
150 upa_alloc_bus_tag(struct upa_softc *sc)
151 {
152 	struct sparc_bus_space_tag *bt;
153 
154 	bt = malloc(sizeof(*bt), M_DEVBUF, M_NOWAIT | M_ZERO);
155 	if (bt == NULL)
156 		panic("upa: couldn't alloc bus tag");
157 
158 	*bt = *sc->sc_bt;
159 	bt->cookie = sc;
160 	bt->parent = sc->sc_bt;
161 	bt->sparc_bus_map = upa_bus_map;
162 	bt->sparc_bus_mmap = upa_bus_mmap;
163 
164 	return bt;
165 }
166 
167 int
168 upa_bus_map(bus_space_tag_t t, bus_addr_t offset,
169     bus_size_t size, int flags, vaddr_t v, bus_space_handle_t *hp)
170 {
171 	struct upa_softc *sc = t->cookie;
172 	int i;
173 
174 	if (t->parent == 0 || t->parent->sparc_bus_map == 0) {
175 		printf("\n__upa_bus_map: invalid parent");
176 		return (EINVAL);
177 	}
178 
179 	t = t->parent;
180 
181 	for (i = 0; i < sc->sc_nrange; i++) {
182 		if (offset < sc->sc_range[i].ur_space)
183 			continue;
184 		if (offset >= (sc->sc_range[i].ur_space +
185 		    sc->sc_range[i].ur_space))
186 			continue;
187 		break;
188 	}
189 	if (i == sc->sc_nrange)
190 		return (EINVAL);
191 
192 	offset -= sc->sc_range[i].ur_space;
193 	offset += sc->sc_range[i].ur_addr;
194 
195 	return ((*t->sparc_bus_map)(t, offset, size, flags, v, hp));
196 }
197 
198 paddr_t
199 upa_bus_mmap(bus_space_tag_t t, bus_addr_t paddr, off_t off, int prot,
200     int flags)
201 {
202 	struct upa_softc *sc = t->cookie;
203 	int i;
204 
205 	if (t->parent == 0 || t->parent->sparc_bus_map == 0) {
206 		printf("\n__upa_bus_map: invalid parent");
207 		return (EINVAL);
208 	}
209 
210 	t = t->parent;
211 
212 	for (i = 0; i < sc->sc_nrange; i++) {
213 		if (paddr + off < sc->sc_range[i].ur_space)
214 			continue;
215 		if (paddr + off >= (sc->sc_range[i].ur_space +
216 		    sc->sc_range[i].ur_space))
217 			continue;
218 		break;
219 	}
220 	if (i == sc->sc_nrange)
221 		return (EINVAL);
222 
223 	paddr -= sc->sc_range[i].ur_space;
224 	paddr += sc->sc_range[i].ur_addr;
225 
226 	return ((*t->sparc_bus_mmap)(t, paddr, off, prot, flags));
227 }
228