vme.c revision 1.7 1 1.7 thorpej /* $NetBSD: vme.c,v 1.7 2002/09/27 20:42:03 thorpej Exp $ */
2 1.3 drochner
3 1.3 drochner /*
4 1.3 drochner * Copyright (c) 1999
5 1.3 drochner * Matthias Drochner. All rights reserved.
6 1.1 pk *
7 1.1 pk * Redistribution and use in source and binary forms, with or without
8 1.1 pk * modification, are permitted provided that the following conditions
9 1.1 pk * are met:
10 1.1 pk * 1. Redistributions of source code must retain the above copyright
11 1.1 pk * notice, this list of conditions and the following disclaimer.
12 1.1 pk * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 pk * notice, this list of conditions and the following disclaimer in the
14 1.1 pk * documentation and/or other materials provided with the distribution.
15 1.3 drochner * 3. The name of the author may not be used to endorse or promote products
16 1.3 drochner * derived from this software without specific prior written permission.
17 1.3 drochner *
18 1.3 drochner * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.3 drochner * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.3 drochner * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.3 drochner * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.3 drochner * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 1.3 drochner * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 1.3 drochner * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 1.3 drochner * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 1.3 drochner * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 1.3 drochner * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1 pk *
29 1.1 pk */
30 1.4 lukem
31 1.4 lukem #include <sys/cdefs.h>
32 1.7 thorpej __KERNEL_RCSID(0, "$NetBSD: vme.c,v 1.7 2002/09/27 20:42:03 thorpej Exp $");
33 1.1 pk
34 1.1 pk #include <sys/param.h>
35 1.1 pk #include <sys/systm.h>
36 1.1 pk #include <sys/device.h>
37 1.1 pk #include <sys/malloc.h>
38 1.3 drochner #include <sys/extent.h>
39 1.3 drochner #include <machine/bus.h>
40 1.3 drochner
41 1.3 drochner #include <dev/vme/vmereg.h>
42 1.3 drochner #include <dev/vme/vmevar.h>
43 1.1 pk
44 1.3 drochner static void vme_extractlocators __P((int*, struct vme_attach_args*));
45 1.3 drochner static int vmeprint __P((struct vme_attach_args*, char*));
46 1.3 drochner static int vmesubmatch1 __P((struct device*, struct cfdata*, void*));
47 1.3 drochner static int vmesubmatch __P((struct device*, struct cfdata*, void*));
48 1.3 drochner int vmematch __P((struct device *, struct cfdata *, void *));
49 1.3 drochner void vmeattach __P((struct device*, struct device*,void*));
50 1.3 drochner static struct extent *vme_select_map __P((struct vmebus_softc*, vme_am_t));
51 1.3 drochner
52 1.3 drochner #ifdef notyet
53 1.3 drochner int vmedetach __P((struct device*));
54 1.3 drochner #endif
55 1.3 drochner
56 1.3 drochner #define VME_SLAVE_DUMMYDRV "vme_slv"
57 1.3 drochner
58 1.3 drochner #define VME_NUMCFRANGES 3 /* cf. "files.vme" */
59 1.3 drochner
60 1.7 thorpej const struct cfattach vme_ca = {
61 1.3 drochner sizeof(struct vmebus_softc), vmematch, vmeattach,
62 1.3 drochner };
63 1.3 drochner
64 1.7 thorpej const struct cfattach vme_slv_ca = {
65 1.3 drochner 0 /* never used */
66 1.3 drochner };
67 1.3 drochner
68 1.3 drochner static void
69 1.3 drochner vme_extractlocators(loc, aa)
70 1.3 drochner int *loc;
71 1.3 drochner struct vme_attach_args *aa;
72 1.3 drochner {
73 1.3 drochner int i = 0;
74 1.1 pk
75 1.3 drochner /* XXX can't use constants in locators.h this way */
76 1.1 pk
77 1.3 drochner while (i < VME_NUMCFRANGES && i < VME_MAXCFRANGES &&
78 1.3 drochner loc[i] != -1) {
79 1.3 drochner aa->r[i].offset = (vme_addr_t)loc[i];
80 1.3 drochner aa->r[i].size = (vme_size_t)loc[3 + i];
81 1.3 drochner aa->r[i].am = (vme_am_t)loc[6 + i];
82 1.3 drochner i++;
83 1.3 drochner }
84 1.3 drochner aa->numcfranges = i;
85 1.3 drochner aa->ilevel = loc[9];
86 1.3 drochner aa->ivector = loc[10];
87 1.3 drochner }
88 1.1 pk
89 1.3 drochner static int
90 1.3 drochner vmeprint(v, dummy)
91 1.3 drochner struct vme_attach_args *v;
92 1.3 drochner char *dummy;
93 1.3 drochner {
94 1.3 drochner int i;
95 1.1 pk
96 1.3 drochner for (i = 0; i < v->numcfranges; i++) {
97 1.3 drochner printf(" addr %x", v->r[i].offset);
98 1.3 drochner if (v->r[i].size != -1)
99 1.3 drochner printf("-%x", v->r[i].offset + v->r[i].size - 1);
100 1.3 drochner if (v->r[i].am != -1)
101 1.3 drochner printf(" am %02x", v->r[i].am);
102 1.3 drochner }
103 1.3 drochner if (v->ilevel != -1) {
104 1.3 drochner printf(" irq %d", v->ilevel);
105 1.3 drochner if (v->ivector != -1)
106 1.3 drochner printf(" vector %x", v->ivector);
107 1.3 drochner }
108 1.1 pk return (UNCONF);
109 1.1 pk }
110 1.1 pk
111 1.3 drochner /*
112 1.3 drochner * This looks for a (dummy) vme device "VME_SLAVE_DUMMYDRV".
113 1.3 drochner * A callback provided by the bus's parent is called for every such
114 1.3 drochner * entry in the config database.
115 1.3 drochner * This is a special hack allowing to communicate the address settings
116 1.3 drochner * of the VME master's slave side to its driver via the normal
117 1.3 drochner * configuration mechanism.
118 1.3 drochner * Needed in following cases:
119 1.3 drochner * -DMA windows are hardware settable but not readable by software
120 1.3 drochner * (driver gets offsets for DMA address calculations this way)
121 1.3 drochner * -DMA windows are software settable, but not persistent
122 1.3 drochner * (hardware is set up from config file entry)
123 1.3 drochner * -other adapter VME slave ranges which should be kept track of
124 1.3 drochner * for address space accounting
125 1.3 drochner * In any case, the adapter driver must get the data before VME
126 1.3 drochner * devices are attached.
127 1.3 drochner */
128 1.3 drochner static int
129 1.3 drochner vmesubmatch1(bus, dev, aux)
130 1.3 drochner struct device *bus;
131 1.3 drochner struct cfdata *dev;
132 1.3 drochner void *aux;
133 1.3 drochner {
134 1.3 drochner struct vmebus_softc *sc = (struct vmebus_softc*)bus;
135 1.3 drochner struct vme_attach_args v;
136 1.3 drochner
137 1.5 thorpej if (strcmp(dev->cf_name, VME_SLAVE_DUMMYDRV))
138 1.3 drochner return (0);
139 1.3 drochner
140 1.3 drochner vme_extractlocators(dev->cf_loc, &v);
141 1.3 drochner
142 1.3 drochner v.va_vct = sc->sc_vct; /* for space allocation */
143 1.3 drochner
144 1.3 drochner (*sc->slaveconfig)(bus->dv_parent, &v);
145 1.3 drochner return (0);
146 1.3 drochner }
147 1.3 drochner
148 1.3 drochner static int
149 1.3 drochner vmesubmatch(bus, dev, aux)
150 1.3 drochner struct device *bus;
151 1.3 drochner struct cfdata *dev;
152 1.3 drochner void *aux;
153 1.3 drochner {
154 1.3 drochner struct vmebus_softc *sc = (struct vmebus_softc*)bus;
155 1.3 drochner struct vme_attach_args v;
156 1.3 drochner
157 1.5 thorpej if (!strcmp(dev->cf_name, VME_SLAVE_DUMMYDRV))
158 1.3 drochner return (0);
159 1.3 drochner
160 1.3 drochner vme_extractlocators(dev->cf_loc, &v);
161 1.3 drochner
162 1.3 drochner v.va_vct = sc->sc_vct;
163 1.3 drochner v.va_bdt = sc->sc_bdt;
164 1.3 drochner
165 1.6 thorpej if (config_match(bus, dev, &v)) {
166 1.3 drochner config_attach(bus, dev, &v, (cfprint_t)vmeprint);
167 1.3 drochner return (1);
168 1.3 drochner }
169 1.3 drochner return (0);
170 1.3 drochner }
171 1.1 pk
172 1.1 pk int
173 1.3 drochner vmematch(parent, match, aux)
174 1.1 pk struct device *parent;
175 1.3 drochner struct cfdata *match;
176 1.1 pk void *aux;
177 1.1 pk {
178 1.3 drochner return (1);
179 1.3 drochner }
180 1.3 drochner
181 1.3 drochner void
182 1.3 drochner vmeattach(parent, self, aux)
183 1.3 drochner struct device *parent, *self;
184 1.3 drochner void *aux;
185 1.3 drochner {
186 1.3 drochner struct vmebus_softc *sc = (struct vmebus_softc *)self;
187 1.3 drochner
188 1.3 drochner struct vmebus_attach_args *aa =
189 1.3 drochner (struct vmebus_attach_args*)aux;
190 1.3 drochner
191 1.3 drochner sc->sc_vct = aa->va_vct;
192 1.3 drochner sc->sc_bdt = aa->va_bdt;
193 1.3 drochner
194 1.3 drochner /* the "bus" are we ourselves */
195 1.3 drochner sc->sc_vct->bus = sc;
196 1.3 drochner
197 1.3 drochner sc->slaveconfig = aa->va_slaveconfig;
198 1.3 drochner
199 1.3 drochner printf("\n");
200 1.3 drochner
201 1.3 drochner /*
202 1.3 drochner * set up address space accounting - assume incomplete decoding
203 1.3 drochner */
204 1.3 drochner sc->vme32ext = extent_create("vme32", 0, 0xffffffff,
205 1.3 drochner M_DEVBUF, 0, 0, 0);
206 1.3 drochner if (!sc->vme32ext) {
207 1.3 drochner printf("error creating A32 map\n");
208 1.3 drochner return;
209 1.3 drochner }
210 1.3 drochner
211 1.3 drochner sc->vme24ext = extent_create("vme24", 0, 0x00ffffff,
212 1.3 drochner M_DEVBUF, 0, 0, 0);
213 1.3 drochner if (!sc->vme24ext) {
214 1.3 drochner printf("error creating A24 map\n");
215 1.3 drochner return;
216 1.3 drochner }
217 1.3 drochner
218 1.3 drochner sc->vme16ext = extent_create("vme16", 0, 0x0000ffff,
219 1.3 drochner M_DEVBUF, 0, 0, 0);
220 1.3 drochner if (!sc->vme16ext) {
221 1.3 drochner printf("error creating A16 map\n");
222 1.3 drochner return;
223 1.3 drochner }
224 1.3 drochner
225 1.3 drochner if (sc->slaveconfig) {
226 1.3 drochner /* first get info about the bus master's slave side,
227 1.3 drochner if present */
228 1.3 drochner config_search((cfmatch_t)vmesubmatch1, self, 0);
229 1.3 drochner }
230 1.3 drochner config_search((cfmatch_t)vmesubmatch, self, 0);
231 1.3 drochner
232 1.3 drochner #ifdef VMEDEBUG
233 1.3 drochner if (sc->vme32ext)
234 1.3 drochner extent_print(sc->vme32ext);
235 1.3 drochner if (sc->vme24ext)
236 1.3 drochner extent_print(sc->vme24ext);
237 1.3 drochner if (sc->vme16ext)
238 1.3 drochner extent_print(sc->vme16ext);
239 1.3 drochner #endif
240 1.3 drochner }
241 1.3 drochner
242 1.3 drochner #ifdef notyet
243 1.3 drochner int
244 1.3 drochner vmedetach(dev)
245 1.3 drochner struct device *dev;
246 1.3 drochner {
247 1.3 drochner struct vmebus_softc *sc = (struct vmebus_softc*)dev;
248 1.1 pk
249 1.3 drochner if (sc->slaveconfig) {
250 1.3 drochner /* allow bus master to free its bus ressources */
251 1.3 drochner (*sc->slaveconfig)(dev->dv_parent, 0);
252 1.3 drochner }
253 1.3 drochner
254 1.3 drochner /* extent maps should be empty now */
255 1.3 drochner
256 1.3 drochner if (sc->vme32ext) {
257 1.3 drochner #ifdef VMEDEBUG
258 1.3 drochner extent_print(sc->vme32ext);
259 1.3 drochner #endif
260 1.3 drochner extent_destroy(sc->vme32ext);
261 1.3 drochner }
262 1.3 drochner if (sc->vme24ext) {
263 1.3 drochner #ifdef VMEDEBUG
264 1.3 drochner extent_print(sc->vme24ext);
265 1.3 drochner #endif
266 1.3 drochner extent_destroy(sc->vme24ext);
267 1.3 drochner }
268 1.3 drochner if (sc->vme16ext) {
269 1.3 drochner #ifdef VMEDEBUG
270 1.3 drochner extent_print(sc->vme16ext);
271 1.3 drochner #endif
272 1.3 drochner extent_destroy(sc->vme16ext);
273 1.3 drochner }
274 1.1 pk
275 1.3 drochner return (0);
276 1.3 drochner }
277 1.3 drochner #endif
278 1.1 pk
279 1.3 drochner static struct extent *
280 1.3 drochner vme_select_map(sc, ams)
281 1.3 drochner struct vmebus_softc *sc;
282 1.3 drochner vme_am_t ams;
283 1.3 drochner {
284 1.3 drochner if ((ams & VME_AM_ADRSIZEMASK) == VME_AM_A32)
285 1.3 drochner return (sc->vme32ext);
286 1.3 drochner else if ((ams & VME_AM_ADRSIZEMASK) == VME_AM_A24)
287 1.3 drochner return (sc->vme24ext);
288 1.3 drochner else if ((ams & VME_AM_ADRSIZEMASK) == VME_AM_A16)
289 1.3 drochner return (sc->vme16ext);
290 1.3 drochner else
291 1.1 pk return (0);
292 1.3 drochner }
293 1.1 pk
294 1.3 drochner int
295 1.3 drochner _vme_space_alloc(sc, addr, len, ams)
296 1.3 drochner struct vmebus_softc *sc;
297 1.3 drochner vme_addr_t addr;
298 1.3 drochner vme_size_t len;
299 1.3 drochner vme_am_t ams;
300 1.3 drochner {
301 1.3 drochner struct extent *ex;
302 1.3 drochner
303 1.3 drochner ex = vme_select_map(sc, ams);
304 1.3 drochner if (!ex)
305 1.3 drochner return (EINVAL);
306 1.3 drochner
307 1.3 drochner return (extent_alloc_region(ex, addr, len, EX_NOWAIT));
308 1.3 drochner }
309 1.3 drochner
310 1.3 drochner void
311 1.3 drochner _vme_space_free(sc, addr, len, ams)
312 1.3 drochner struct vmebus_softc *sc;
313 1.3 drochner vme_addr_t addr;
314 1.3 drochner vme_size_t len;
315 1.3 drochner vme_am_t ams;
316 1.3 drochner {
317 1.3 drochner struct extent *ex;
318 1.3 drochner
319 1.3 drochner ex = vme_select_map(sc, ams);
320 1.3 drochner if (!ex) {
321 1.3 drochner panic("vme_space_free: invalid am %x", ams);
322 1.3 drochner return;
323 1.3 drochner }
324 1.3 drochner
325 1.3 drochner extent_free(ex, addr, len, EX_NOWAIT);
326 1.3 drochner }
327 1.3 drochner
328 1.3 drochner int
329 1.3 drochner _vme_space_get(sc, len, ams, align, addr)
330 1.3 drochner struct vmebus_softc *sc;
331 1.3 drochner vme_size_t len;
332 1.3 drochner vme_am_t ams;
333 1.3 drochner u_long align;
334 1.3 drochner vme_addr_t *addr;
335 1.3 drochner {
336 1.3 drochner struct extent *ex;
337 1.3 drochner
338 1.3 drochner ex = vme_select_map(sc, ams);
339 1.3 drochner if (!ex)
340 1.3 drochner return (EINVAL);
341 1.3 drochner
342 1.3 drochner return (extent_alloc(ex, len, align, EX_NOBOUNDARY, EX_NOWAIT,
343 1.3 drochner (u_long *)addr));
344 1.1 pk }
345