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