vme.c revision 1.18 1 1.18 thorpej /* $NetBSD: vme.c,v 1.18 2006/03/25 23:26:51 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.18 thorpej __KERNEL_RCSID(0, "$NetBSD: vme.c,v 1.18 2006/03/25 23:26:51 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.15 perry static void vme_extractlocators(int*, struct vme_attach_args*);
45 1.15 perry static int vmeprint(struct vme_attach_args*, char*);
46 1.15 perry static int vmesubmatch1(struct device*, struct cfdata*,
47 1.16 drochner const int *, void*);
48 1.15 perry static int vmesubmatch(struct device*, struct cfdata*,
49 1.16 drochner const int *, void*);
50 1.15 perry int vmematch(struct device *, struct cfdata *, void *);
51 1.15 perry void vmeattach(struct device*, struct device*,void*);
52 1.15 perry static struct extent *vme_select_map(struct vmebus_softc*, vme_am_t);
53 1.3 drochner
54 1.3 drochner #ifdef notyet
55 1.15 perry int vmedetach(struct device*);
56 1.3 drochner #endif
57 1.3 drochner
58 1.3 drochner #define VME_SLAVE_DUMMYDRV "vme_slv"
59 1.3 drochner
60 1.3 drochner #define VME_NUMCFRANGES 3 /* cf. "files.vme" */
61 1.3 drochner
62 1.8 thorpej CFATTACH_DECL(vme, sizeof(struct vmebus_softc),
63 1.9 thorpej vmematch, vmeattach, NULL, NULL);
64 1.3 drochner
65 1.7 thorpej const struct cfattach vme_slv_ca = {
66 1.3 drochner 0 /* never used */
67 1.3 drochner };
68 1.3 drochner
69 1.3 drochner static void
70 1.3 drochner vme_extractlocators(loc, aa)
71 1.3 drochner int *loc;
72 1.3 drochner struct vme_attach_args *aa;
73 1.3 drochner {
74 1.3 drochner int i = 0;
75 1.1 pk
76 1.3 drochner /* XXX can't use constants in locators.h this way */
77 1.1 pk
78 1.3 drochner while (i < VME_NUMCFRANGES && i < VME_MAXCFRANGES &&
79 1.3 drochner loc[i] != -1) {
80 1.3 drochner aa->r[i].offset = (vme_addr_t)loc[i];
81 1.3 drochner aa->r[i].size = (vme_size_t)loc[3 + i];
82 1.3 drochner aa->r[i].am = (vme_am_t)loc[6 + i];
83 1.3 drochner i++;
84 1.3 drochner }
85 1.3 drochner aa->numcfranges = i;
86 1.3 drochner aa->ilevel = loc[9];
87 1.3 drochner aa->ivector = loc[10];
88 1.3 drochner }
89 1.1 pk
90 1.3 drochner static int
91 1.3 drochner vmeprint(v, dummy)
92 1.3 drochner struct vme_attach_args *v;
93 1.3 drochner char *dummy;
94 1.3 drochner {
95 1.3 drochner int i;
96 1.1 pk
97 1.3 drochner for (i = 0; i < v->numcfranges; i++) {
98 1.10 thorpej aprint_normal(" addr %x", v->r[i].offset);
99 1.3 drochner if (v->r[i].size != -1)
100 1.10 thorpej aprint_normal("-%x", v->r[i].offset + v->r[i].size - 1);
101 1.3 drochner if (v->r[i].am != -1)
102 1.10 thorpej aprint_normal(" am %02x", v->r[i].am);
103 1.3 drochner }
104 1.3 drochner if (v->ilevel != -1) {
105 1.10 thorpej aprint_normal(" irq %d", v->ilevel);
106 1.3 drochner if (v->ivector != -1)
107 1.10 thorpej aprint_normal(" vector %x", v->ivector);
108 1.3 drochner }
109 1.1 pk return (UNCONF);
110 1.1 pk }
111 1.1 pk
112 1.3 drochner /*
113 1.3 drochner * This looks for a (dummy) vme device "VME_SLAVE_DUMMYDRV".
114 1.3 drochner * A callback provided by the bus's parent is called for every such
115 1.3 drochner * entry in the config database.
116 1.3 drochner * This is a special hack allowing to communicate the address settings
117 1.3 drochner * of the VME master's slave side to its driver via the normal
118 1.3 drochner * configuration mechanism.
119 1.3 drochner * Needed in following cases:
120 1.3 drochner * -DMA windows are hardware settable but not readable by software
121 1.3 drochner * (driver gets offsets for DMA address calculations this way)
122 1.3 drochner * -DMA windows are software settable, but not persistent
123 1.3 drochner * (hardware is set up from config file entry)
124 1.3 drochner * -other adapter VME slave ranges which should be kept track of
125 1.3 drochner * for address space accounting
126 1.3 drochner * In any case, the adapter driver must get the data before VME
127 1.3 drochner * devices are attached.
128 1.3 drochner */
129 1.3 drochner static int
130 1.13 drochner vmesubmatch1(bus, dev, ldesc, aux)
131 1.3 drochner struct device *bus;
132 1.3 drochner struct cfdata *dev;
133 1.16 drochner const int *ldesc;
134 1.3 drochner void *aux;
135 1.3 drochner {
136 1.3 drochner struct vmebus_softc *sc = (struct vmebus_softc*)bus;
137 1.3 drochner struct vme_attach_args v;
138 1.3 drochner
139 1.5 thorpej if (strcmp(dev->cf_name, VME_SLAVE_DUMMYDRV))
140 1.3 drochner return (0);
141 1.3 drochner
142 1.3 drochner vme_extractlocators(dev->cf_loc, &v);
143 1.3 drochner
144 1.3 drochner v.va_vct = sc->sc_vct; /* for space allocation */
145 1.3 drochner
146 1.18 thorpej (*sc->slaveconfig)(device_parent(bus), &v);
147 1.3 drochner return (0);
148 1.3 drochner }
149 1.3 drochner
150 1.3 drochner static int
151 1.14 drochner vmesubmatch(bus, dev, ldesc, aux)
152 1.3 drochner struct device *bus;
153 1.3 drochner struct cfdata *dev;
154 1.16 drochner const int *ldesc;
155 1.3 drochner void *aux;
156 1.3 drochner {
157 1.3 drochner struct vmebus_softc *sc = (struct vmebus_softc*)bus;
158 1.3 drochner struct vme_attach_args v;
159 1.3 drochner
160 1.5 thorpej if (!strcmp(dev->cf_name, VME_SLAVE_DUMMYDRV))
161 1.3 drochner return (0);
162 1.3 drochner
163 1.3 drochner vme_extractlocators(dev->cf_loc, &v);
164 1.3 drochner
165 1.3 drochner v.va_vct = sc->sc_vct;
166 1.3 drochner v.va_bdt = sc->sc_bdt;
167 1.3 drochner
168 1.6 thorpej if (config_match(bus, dev, &v)) {
169 1.3 drochner config_attach(bus, dev, &v, (cfprint_t)vmeprint);
170 1.3 drochner return (1);
171 1.3 drochner }
172 1.3 drochner return (0);
173 1.3 drochner }
174 1.1 pk
175 1.1 pk int
176 1.3 drochner vmematch(parent, match, aux)
177 1.1 pk struct device *parent;
178 1.3 drochner struct cfdata *match;
179 1.1 pk void *aux;
180 1.1 pk {
181 1.3 drochner return (1);
182 1.3 drochner }
183 1.3 drochner
184 1.3 drochner void
185 1.3 drochner vmeattach(parent, self, aux)
186 1.3 drochner struct device *parent, *self;
187 1.3 drochner void *aux;
188 1.3 drochner {
189 1.3 drochner struct vmebus_softc *sc = (struct vmebus_softc *)self;
190 1.3 drochner
191 1.3 drochner struct vmebus_attach_args *aa =
192 1.3 drochner (struct vmebus_attach_args*)aux;
193 1.3 drochner
194 1.3 drochner sc->sc_vct = aa->va_vct;
195 1.3 drochner sc->sc_bdt = aa->va_bdt;
196 1.3 drochner
197 1.3 drochner /* the "bus" are we ourselves */
198 1.3 drochner sc->sc_vct->bus = sc;
199 1.3 drochner
200 1.3 drochner sc->slaveconfig = aa->va_slaveconfig;
201 1.3 drochner
202 1.3 drochner printf("\n");
203 1.3 drochner
204 1.3 drochner /*
205 1.3 drochner * set up address space accounting - assume incomplete decoding
206 1.3 drochner */
207 1.3 drochner sc->vme32ext = extent_create("vme32", 0, 0xffffffff,
208 1.3 drochner M_DEVBUF, 0, 0, 0);
209 1.3 drochner if (!sc->vme32ext) {
210 1.3 drochner printf("error creating A32 map\n");
211 1.3 drochner return;
212 1.3 drochner }
213 1.3 drochner
214 1.3 drochner sc->vme24ext = extent_create("vme24", 0, 0x00ffffff,
215 1.3 drochner M_DEVBUF, 0, 0, 0);
216 1.3 drochner if (!sc->vme24ext) {
217 1.3 drochner printf("error creating A24 map\n");
218 1.3 drochner return;
219 1.3 drochner }
220 1.3 drochner
221 1.3 drochner sc->vme16ext = extent_create("vme16", 0, 0x0000ffff,
222 1.3 drochner M_DEVBUF, 0, 0, 0);
223 1.3 drochner if (!sc->vme16ext) {
224 1.3 drochner printf("error creating A16 map\n");
225 1.3 drochner return;
226 1.3 drochner }
227 1.3 drochner
228 1.3 drochner if (sc->slaveconfig) {
229 1.3 drochner /* first get info about the bus master's slave side,
230 1.3 drochner if present */
231 1.13 drochner config_search_ia(vmesubmatch1, self, "vme", 0);
232 1.3 drochner }
233 1.13 drochner config_search_ia(vmesubmatch, self, "vme", 0);
234 1.3 drochner
235 1.3 drochner #ifdef VMEDEBUG
236 1.3 drochner if (sc->vme32ext)
237 1.3 drochner extent_print(sc->vme32ext);
238 1.3 drochner if (sc->vme24ext)
239 1.3 drochner extent_print(sc->vme24ext);
240 1.3 drochner if (sc->vme16ext)
241 1.3 drochner extent_print(sc->vme16ext);
242 1.3 drochner #endif
243 1.3 drochner }
244 1.3 drochner
245 1.3 drochner #ifdef notyet
246 1.3 drochner int
247 1.3 drochner vmedetach(dev)
248 1.3 drochner struct device *dev;
249 1.3 drochner {
250 1.3 drochner struct vmebus_softc *sc = (struct vmebus_softc*)dev;
251 1.1 pk
252 1.3 drochner if (sc->slaveconfig) {
253 1.3 drochner /* allow bus master to free its bus ressources */
254 1.18 thorpej (*sc->slaveconfig)(device_parent(dev), 0);
255 1.3 drochner }
256 1.3 drochner
257 1.3 drochner /* extent maps should be empty now */
258 1.3 drochner
259 1.3 drochner if (sc->vme32ext) {
260 1.3 drochner #ifdef VMEDEBUG
261 1.3 drochner extent_print(sc->vme32ext);
262 1.3 drochner #endif
263 1.3 drochner extent_destroy(sc->vme32ext);
264 1.3 drochner }
265 1.3 drochner if (sc->vme24ext) {
266 1.3 drochner #ifdef VMEDEBUG
267 1.3 drochner extent_print(sc->vme24ext);
268 1.3 drochner #endif
269 1.3 drochner extent_destroy(sc->vme24ext);
270 1.3 drochner }
271 1.3 drochner if (sc->vme16ext) {
272 1.3 drochner #ifdef VMEDEBUG
273 1.3 drochner extent_print(sc->vme16ext);
274 1.3 drochner #endif
275 1.3 drochner extent_destroy(sc->vme16ext);
276 1.3 drochner }
277 1.1 pk
278 1.3 drochner return (0);
279 1.3 drochner }
280 1.3 drochner #endif
281 1.1 pk
282 1.3 drochner static struct extent *
283 1.3 drochner vme_select_map(sc, ams)
284 1.3 drochner struct vmebus_softc *sc;
285 1.3 drochner vme_am_t ams;
286 1.3 drochner {
287 1.3 drochner if ((ams & VME_AM_ADRSIZEMASK) == VME_AM_A32)
288 1.3 drochner return (sc->vme32ext);
289 1.3 drochner else if ((ams & VME_AM_ADRSIZEMASK) == VME_AM_A24)
290 1.3 drochner return (sc->vme24ext);
291 1.3 drochner else if ((ams & VME_AM_ADRSIZEMASK) == VME_AM_A16)
292 1.3 drochner return (sc->vme16ext);
293 1.3 drochner else
294 1.1 pk return (0);
295 1.3 drochner }
296 1.1 pk
297 1.3 drochner int
298 1.3 drochner _vme_space_alloc(sc, addr, len, ams)
299 1.3 drochner struct vmebus_softc *sc;
300 1.3 drochner vme_addr_t addr;
301 1.3 drochner vme_size_t len;
302 1.3 drochner vme_am_t ams;
303 1.3 drochner {
304 1.3 drochner struct extent *ex;
305 1.3 drochner
306 1.3 drochner ex = vme_select_map(sc, ams);
307 1.3 drochner if (!ex)
308 1.3 drochner return (EINVAL);
309 1.3 drochner
310 1.3 drochner return (extent_alloc_region(ex, addr, len, EX_NOWAIT));
311 1.3 drochner }
312 1.3 drochner
313 1.3 drochner void
314 1.3 drochner _vme_space_free(sc, addr, len, ams)
315 1.3 drochner struct vmebus_softc *sc;
316 1.3 drochner vme_addr_t addr;
317 1.3 drochner vme_size_t len;
318 1.3 drochner vme_am_t ams;
319 1.3 drochner {
320 1.3 drochner struct extent *ex;
321 1.3 drochner
322 1.3 drochner ex = vme_select_map(sc, ams);
323 1.3 drochner if (!ex) {
324 1.3 drochner panic("vme_space_free: invalid am %x", ams);
325 1.3 drochner return;
326 1.3 drochner }
327 1.3 drochner
328 1.3 drochner extent_free(ex, addr, len, EX_NOWAIT);
329 1.3 drochner }
330 1.3 drochner
331 1.3 drochner int
332 1.3 drochner _vme_space_get(sc, len, ams, align, addr)
333 1.3 drochner struct vmebus_softc *sc;
334 1.3 drochner vme_size_t len;
335 1.3 drochner vme_am_t ams;
336 1.3 drochner u_long align;
337 1.3 drochner vme_addr_t *addr;
338 1.3 drochner {
339 1.3 drochner struct extent *ex;
340 1.12 drochner u_long help;
341 1.12 drochner int res;
342 1.3 drochner
343 1.3 drochner ex = vme_select_map(sc, ams);
344 1.3 drochner if (!ex)
345 1.3 drochner return (EINVAL);
346 1.3 drochner
347 1.12 drochner res = extent_alloc(ex, len, align, EX_NOBOUNDARY, EX_NOWAIT, &help);
348 1.12 drochner if (!res)
349 1.12 drochner *addr = help;
350 1.12 drochner return (res);
351 1.1 pk }
352