xref: /openbsd/sys/arch/sparc64/dev/upa.c (revision 7b36286a)
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/types.h>
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/device.h>
39 #include <sys/conf.h>
40 #include <sys/timeout.h>
41 #include <sys/malloc.h>
42 
43 #define _SPARC_BUS_DMA_PRIVATE
44 #include <machine/bus.h>
45 #include <machine/autoconf.h>
46 #include <machine/openfirm.h>
47 
48 struct upa_range {
49 	u_int64_t	ur_space;
50 	u_int64_t	ur_addr;
51 	u_int64_t	ur_len;
52 };
53 
54 struct upa_softc {
55 	struct device		sc_dev;
56 	bus_space_tag_t		sc_bt;
57 	bus_space_handle_t	sc_reg[3];
58 	struct upa_range	*sc_range;
59 	int			sc_node;
60 	int			sc_nrange;
61 	bus_space_tag_t		sc_cbt;
62 };
63 
64 int	upa_match(struct device *, void *, void *);
65 void	upa_attach(struct device *, struct device *, void *);
66 
67 struct cfattach upa_ca = {
68 	sizeof(struct upa_softc), upa_match, upa_attach
69 };
70 
71 struct cfdriver upa_cd = {
72 	NULL, "upa", DV_DULL
73 };
74 
75 int upa_print(void *, const char *);
76 bus_space_tag_t upa_alloc_bus_tag(struct upa_softc *);
77 int upa_bus_map(bus_space_tag_t, bus_space_tag_t, bus_addr_t,
78     bus_size_t, int, bus_space_handle_t *);
79 paddr_t upa_bus_mmap(bus_space_tag_t, bus_space_tag_t,
80     bus_addr_t, off_t, int, int);
81 
82 int
83 upa_match(struct device *parent, void *match, void *aux)
84 {
85 	struct mainbus_attach_args *ma = aux;
86 
87 	if (strcmp(ma->ma_name, "upa") == 0)
88 		return (1);
89 
90 	return (0);
91 }
92 
93 void
94 upa_attach(struct device *parent, struct device *self, void *aux)
95 {
96 	struct upa_softc *sc = (void *)self;
97 	struct mainbus_attach_args *ma = aux;
98 	int i, node;
99 
100 	sc->sc_bt = ma->ma_bustag;
101 	sc->sc_node = ma->ma_node;
102 
103 	for (i = 0; i < 3; i++) {
104 		if (i >= ma->ma_nreg) {
105 			printf(": no register %d\n", i);
106 			return;
107 		}
108 		if (bus_space_map(sc->sc_bt, ma->ma_reg[i].ur_paddr,
109 		    ma->ma_reg[i].ur_len, 0, &sc->sc_reg[i])) {
110 			printf(": failed to map reg%d\n", i);
111 			return;
112 		}
113 	}
114 
115 	if (getprop(sc->sc_node, "ranges", sizeof(struct upa_range),
116 	    &sc->sc_nrange, (void **)&sc->sc_range))
117 		panic("upa: can't get ranges");
118 
119 	printf("\n");
120 
121 	sc->sc_cbt = upa_alloc_bus_tag(sc);
122 
123 	for (node = OF_child(sc->sc_node); node; node = OF_peer(node)) {
124 		char buf[32];
125 		struct mainbus_attach_args map;
126 
127 		bzero(&map, sizeof(map));
128 		if (OF_getprop(node, "device_type", buf, sizeof(buf)) <= 0)
129 			continue;
130 		if (getprop(node, "reg", sizeof(*map.ma_reg),
131 		    &map.ma_nreg, (void **)&map.ma_reg) != 0)
132 			continue;
133 		if (OF_getprop(node, "name", buf, sizeof(buf)) <= 0)
134 			continue;
135 		map.ma_node = node;
136 		map.ma_name = buf;
137 		map.ma_bustag = sc->sc_cbt;
138 		map.ma_dmatag = ma->ma_dmatag;
139 		config_found(&sc->sc_dev, &map, upa_print);
140 	}
141 }
142 
143 int
144 upa_print(void *args, const char *name)
145 {
146 	struct mainbus_attach_args *ma = args;
147 
148 	if (name)
149 		printf("\"%s\" at %s", ma->ma_name, name);
150 	return (UNCONF);
151 }
152 
153 bus_space_tag_t
154 upa_alloc_bus_tag(struct upa_softc *sc)
155 {
156 	struct sparc_bus_space_tag *bt;
157 
158 	bt = malloc(sizeof(*bt), M_DEVBUF, M_NOWAIT | M_ZERO);
159 	if (bt == NULL)
160 		panic("upa: couldn't alloc bus tag");
161 
162 	snprintf(bt->name, sizeof(bt->name), "%s",
163 			sc->sc_dev.dv_xname);
164 	bt->cookie = sc;
165 	bt->parent = sc->sc_bt;
166 	bt->asi = bt->parent->asi;
167 	bt->sasi = bt->parent->sasi;
168 	bt->sparc_bus_map = upa_bus_map;
169 	bt->sparc_bus_mmap = upa_bus_mmap;
170 	/* XXX bt->sparc_intr_establish = upa_intr_establish; */
171 	return (bt);
172 }
173 
174 int
175 upa_bus_map(bus_space_tag_t t, bus_space_tag_t t0, bus_addr_t offset,
176     bus_size_t size, int flags, bus_space_handle_t *hp)
177 {
178 	struct upa_softc *sc = t->cookie;
179 	int i;
180 
181 	if (t->parent == 0 || t->parent->sparc_bus_map == 0) {
182 		printf("\n__upa_bus_map: invalid parent");
183 		return (EINVAL);
184 	}
185 
186 	t = t->parent;
187 
188         if (flags & BUS_SPACE_MAP_PROMADDRESS) {
189 		return ((*t->sparc_bus_map)
190 		    (t, t0, offset, size, flags, hp));
191 	}
192 
193 	for (i = 0; i < sc->sc_nrange; i++) {
194 		if (offset < sc->sc_range[i].ur_space)
195 			continue;
196 		if (offset >= (sc->sc_range[i].ur_space +
197 		    sc->sc_range[i].ur_space))
198 			continue;
199 		break;
200 	}
201 	if (i == sc->sc_nrange)
202 		return (EINVAL);
203 
204 	offset -= sc->sc_range[i].ur_space;
205 	offset += sc->sc_range[i].ur_addr;
206 
207 	return ((*t->sparc_bus_map)(t, t0, offset, size, flags, hp));
208 }
209 
210 paddr_t
211 upa_bus_mmap(bus_space_tag_t t, bus_space_tag_t t0, bus_addr_t paddr,
212     off_t off, int prot, int flags)
213 {
214 	struct upa_softc *sc = t->cookie;
215 	int i;
216 
217 	if (t->parent == 0 || t->parent->sparc_bus_map == 0) {
218 		printf("\n__upa_bus_map: invalid parent");
219 		return (EINVAL);
220 	}
221 
222 	t = t->parent;
223 
224         if (flags & BUS_SPACE_MAP_PROMADDRESS)
225 		return ((*t->sparc_bus_mmap)(t, t0, paddr, off, prot, flags));
226 
227 	for (i = 0; i < sc->sc_nrange; i++) {
228 		if (paddr + off < sc->sc_range[i].ur_space)
229 			continue;
230 		if (paddr + off >= (sc->sc_range[i].ur_space +
231 		    sc->sc_range[i].ur_space))
232 			continue;
233 		break;
234 	}
235 	if (i == sc->sc_nrange)
236 		return (EINVAL);
237 
238 	paddr -= sc->sc_range[i].ur_space;
239 	paddr += sc->sc_range[i].ur_addr;
240 
241 	return ((*t->sparc_bus_mmap)(t, t0, paddr, off, prot, flags));
242 }
243