obio.c revision 1.21 1 1.21 thorpej /* $NetBSD: obio.c,v 1.21 1996/03/17 02:01:12 thorpej Exp $ */
2 1.1 deraadt
3 1.1 deraadt /*
4 1.1 deraadt * Copyright (c) 1993, 1994 Theo de Raadt
5 1.1 deraadt * All rights reserved.
6 1.1 deraadt *
7 1.1 deraadt * Redistribution and use in source and binary forms, with or without
8 1.1 deraadt * modification, are permitted provided that the following conditions
9 1.1 deraadt * are met:
10 1.1 deraadt * 1. Redistributions of source code must retain the above copyright
11 1.1 deraadt * notice, this list of conditions and the following disclaimer.
12 1.1 deraadt * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 deraadt * notice, this list of conditions and the following disclaimer in the
14 1.1 deraadt * documentation and/or other materials provided with the distribution.
15 1.1 deraadt * 3. All advertising materials mentioning features or use of this software
16 1.1 deraadt * must display the following acknowledgement:
17 1.1 deraadt * This product includes software developed by Theo de Raadt.
18 1.1 deraadt * 4. The name of the author may not be used to endorse or promote products
19 1.1 deraadt * derived from this software without specific prior written permission.
20 1.1 deraadt *
21 1.1 deraadt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 deraadt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 deraadt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 deraadt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 deraadt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 deraadt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 deraadt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 deraadt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 deraadt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 deraadt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 deraadt */
32 1.1 deraadt
33 1.1 deraadt #include <sys/param.h>
34 1.20 christos #include <sys/systm.h>
35 1.1 deraadt #include <sys/device.h>
36 1.1 deraadt #include <sys/malloc.h>
37 1.1 deraadt
38 1.1 deraadt #ifdef DEBUG
39 1.1 deraadt #include <sys/proc.h>
40 1.1 deraadt #include <sys/syslog.h>
41 1.1 deraadt #endif
42 1.1 deraadt
43 1.1 deraadt #include <vm/vm.h>
44 1.1 deraadt
45 1.1 deraadt #include <machine/autoconf.h>
46 1.1 deraadt #include <machine/pmap.h>
47 1.2 deraadt #include <machine/oldmon.h>
48 1.5 deraadt #include <machine/cpu.h>
49 1.2 deraadt #include <machine/ctlreg.h>
50 1.2 deraadt #include <sparc/sparc/asm.h>
51 1.2 deraadt #include <sparc/sparc/vaddrs.h>
52 1.1 deraadt
53 1.4 deraadt struct bus_softc {
54 1.1 deraadt struct device sc_dev; /* base device */
55 1.1 deraadt int nothing;
56 1.1 deraadt };
57 1.1 deraadt
58 1.1 deraadt /* autoconfiguration driver */
59 1.9 deraadt static int busmatch __P((struct device *, void *, void *));
60 1.9 deraadt static void obioattach __P((struct device *, struct device *, void *));
61 1.20 christos int busprint __P((void *, char *));
62 1.17 pk #if defined(SUN4)
63 1.9 deraadt static void vmesattach __P((struct device *, struct device *, void *));
64 1.9 deraadt static void vmelattach __P((struct device *, struct device *, void *));
65 1.17 pk static int busattach __P((struct device *, void *, void *, int));
66 1.17 pk void * bus_map __P((struct rom_reg *, int, int));
67 1.20 christos int obio_scan __P((struct device *, void *, void *));
68 1.20 christos int vmes_scan __P((struct device *, void *, void *));
69 1.20 christos int vmel_scan __P((struct device *, void *, void *));
70 1.20 christos int vmeintr __P((void *));
71 1.17 pk #endif
72 1.4 deraadt
73 1.21 thorpej struct cfattach obio_ca = {
74 1.21 thorpej sizeof(struct bus_softc), busmatch, obioattach
75 1.4 deraadt };
76 1.21 thorpej
77 1.21 thorpej struct cfdriver obio_cd = {
78 1.21 thorpej NULL, "obio", DV_DULL
79 1.21 thorpej };
80 1.21 thorpej
81 1.17 pk #if defined(SUN4)
82 1.21 thorpej struct cfattach vmel_ca = {
83 1.21 thorpej sizeof(struct bus_softc), busmatch, vmelattach
84 1.21 thorpej };
85 1.21 thorpej
86 1.21 thorpej struct cfdriver vmel_cd = {
87 1.21 thorpej NULL, "vmel", DV_DULL
88 1.21 thorpej };
89 1.21 thorpej
90 1.21 thorpej struct cfattach vmes_ca = {
91 1.21 thorpej sizeof(struct bus_softc), busmatch, vmesattach
92 1.1 deraadt };
93 1.21 thorpej
94 1.21 thorpej struct cfdriver vmes_cd = {
95 1.21 thorpej NULL, "vmes", DV_DULL
96 1.4 deraadt };
97 1.17 pk #endif
98 1.4 deraadt
99 1.2 deraadt
100 1.1 deraadt int
101 1.9 deraadt busmatch(parent, vcf, aux)
102 1.1 deraadt struct device *parent;
103 1.9 deraadt void *vcf, *aux;
104 1.1 deraadt {
105 1.9 deraadt struct cfdata *cf = vcf;
106 1.3 deraadt register struct confargs *ca = aux;
107 1.3 deraadt register struct romaux *ra = &ca->ca_ra;
108 1.3 deraadt
109 1.1 deraadt if (cputyp != CPU_SUN4)
110 1.3 deraadt return (0);
111 1.3 deraadt return (strcmp(cf->cf_driver->cd_name, ra->ra_name) == 0);
112 1.1 deraadt }
113 1.1 deraadt
114 1.1 deraadt int
115 1.4 deraadt busprint(args, obio)
116 1.1 deraadt void *args;
117 1.1 deraadt char *obio;
118 1.1 deraadt {
119 1.2 deraadt register struct confargs *ca = args;
120 1.2 deraadt
121 1.2 deraadt if (ca->ca_ra.ra_name == NULL)
122 1.2 deraadt ca->ca_ra.ra_name = "<unknown>";
123 1.2 deraadt if (obio)
124 1.3 deraadt printf("[%s at %s]", ca->ca_ra.ra_name, obio);
125 1.20 christos printf(" addr %p", ca->ca_ra.ra_paddr);
126 1.7 deraadt if (ca->ca_ra.ra_intr[0].int_vec != -1)
127 1.7 deraadt printf(" vec 0x%x", ca->ca_ra.ra_intr[0].int_vec);
128 1.1 deraadt return (UNCONF);
129 1.1 deraadt }
130 1.1 deraadt
131 1.14 pk
132 1.14 pk int
133 1.14 pk busattach(parent, child, args, bustype)
134 1.14 pk struct device *parent;
135 1.14 pk void *args, *child;
136 1.4 deraadt int bustype;
137 1.1 deraadt {
138 1.14 pk struct cfdata *cf = child;
139 1.3 deraadt register struct confargs *ca = args;
140 1.3 deraadt struct confargs oca;
141 1.4 deraadt caddr_t tmp;
142 1.1 deraadt
143 1.14 pk if (bustype == BUS_OBIO && cputyp == CPU_SUN4) {
144 1.14 pk /*
145 1.14 pk * On the 4/100 obio addresses must be mapped at
146 1.14 pk * 0x0YYYYYYY, but alias higher up (we avoid the
147 1.14 pk * alias condition because it causes pmap difficulties)
148 1.14 pk * XXX: We also assume that 4/[23]00 obio addresses
149 1.14 pk * must be 0xZYYYYYYY, where (Z != 0)
150 1.14 pk */
151 1.14 pk if (cpumod==SUN4_100 && (cf->cf_loc[0] & 0xf0000000))
152 1.14 pk return 0;
153 1.14 pk if (cpumod!=SUN4_100 && !(cf->cf_loc[0] & 0xf0000000))
154 1.14 pk return 0;
155 1.14 pk }
156 1.14 pk
157 1.14 pk if (parent->dv_cfdata->cf_driver->cd_indirect) {
158 1.14 pk printf(" indirect devices not supported\n");
159 1.14 pk return 0;
160 1.1 deraadt }
161 1.1 deraadt
162 1.14 pk oca.ca_ra.ra_iospace = -1;
163 1.14 pk oca.ca_ra.ra_paddr = (void *)cf->cf_loc[0];
164 1.14 pk oca.ca_ra.ra_len = 0;
165 1.14 pk oca.ca_ra.ra_nreg = 1;
166 1.14 pk tmp = NULL;
167 1.14 pk if (oca.ca_ra.ra_paddr)
168 1.14 pk tmp = bus_tmp(oca.ca_ra.ra_paddr,
169 1.14 pk bustype);
170 1.14 pk oca.ca_ra.ra_vaddr = tmp;
171 1.14 pk oca.ca_ra.ra_intr[0].int_pri = cf->cf_loc[1];
172 1.14 pk if (bustype == BUS_VME16 || bustype == BUS_VME32)
173 1.14 pk oca.ca_ra.ra_intr[0].int_vec = cf->cf_loc[2];
174 1.14 pk else
175 1.14 pk oca.ca_ra.ra_intr[0].int_vec = -1;
176 1.14 pk oca.ca_ra.ra_nintr = 1;
177 1.14 pk oca.ca_ra.ra_name = cf->cf_driver->cd_name;
178 1.15 pk if (ca->ca_ra.ra_bp != NULL &&
179 1.16 pk ((bustype == BUS_VME16 && strcmp(ca->ca_ra.ra_bp->name,"vmes") ==0) ||
180 1.16 pk (bustype == BUS_VME32 && strcmp(ca->ca_ra.ra_bp->name,"vmel") ==0) ||
181 1.16 pk (bustype == BUS_OBIO && strcmp(ca->ca_ra.ra_bp->name,"obio") == 0)))
182 1.15 pk oca.ca_ra.ra_bp = ca->ca_ra.ra_bp + 1;
183 1.15 pk else
184 1.15 pk oca.ca_ra.ra_bp = NULL;
185 1.14 pk oca.ca_bustype = bustype;
186 1.14 pk
187 1.21 thorpej if ((*cf->cf_attach->ca_match)(parent, cf, &oca) == 0)
188 1.14 pk return 0;
189 1.14 pk
190 1.14 pk /*
191 1.19 chuck * check if XXmatch routine replaced the temporary mapping with
192 1.19 chuck * a real mapping. If not, then make sure we don't pass the
193 1.19 chuck * tmp mapping to the attach routine.
194 1.14 pk */
195 1.19 chuck if (oca.ca_ra.ra_vaddr == tmp)
196 1.19 chuck oca.ca_ra.ra_vaddr = NULL; /* wipe out tmp address */
197 1.14 pk /*
198 1.19 chuck * the match routine will set "ra_len" if it wants us to
199 1.19 chuck * establish a mapping for it.
200 1.14 pk * (which won't be seen on future XXmatch calls,
201 1.14 pk * so not as useful as it seems.)
202 1.14 pk */
203 1.19 chuck if (oca.ca_ra.ra_len)
204 1.14 pk oca.ca_ra.ra_vaddr =
205 1.17 pk bus_map(oca.ca_ra.ra_reg,
206 1.14 pk oca.ca_ra.ra_len, oca.ca_bustype);
207 1.14 pk
208 1.14 pk config_attach(parent, cf, &oca, busprint);
209 1.14 pk return 1;
210 1.14 pk }
211 1.4 deraadt
212 1.17 pk #if defined(SUN4)
213 1.14 pk int
214 1.14 pk obio_scan(parent, child, args)
215 1.14 pk struct device *parent;
216 1.14 pk void *child, *args;
217 1.14 pk {
218 1.14 pk return busattach(parent, child, args, BUS_OBIO);
219 1.4 deraadt }
220 1.17 pk #endif
221 1.4 deraadt
222 1.4 deraadt void
223 1.4 deraadt obioattach(parent, self, args)
224 1.4 deraadt struct device *parent, *self;
225 1.4 deraadt void *args;
226 1.4 deraadt {
227 1.17 pk #if defined(SUN4)
228 1.17 pk if (cputyp == CPU_SUN4) {
229 1.17 pk if (self->dv_unit > 0) {
230 1.17 pk printf(" unsupported\n");
231 1.17 pk return;
232 1.17 pk }
233 1.17 pk printf("\n");
234 1.17 pk
235 1.17 pk (void)config_search(obio_scan, self, args);
236 1.17 pk bus_untmp();
237 1.14 pk }
238 1.17 pk #endif
239 1.2 deraadt }
240 1.2 deraadt
241 1.17 pk #if defined(SUN4)
242 1.5 deraadt struct intrhand **vmeints;
243 1.5 deraadt
244 1.14 pk int
245 1.14 pk vmes_scan(parent, child, args)
246 1.14 pk struct device *parent;
247 1.14 pk void *child, *args;
248 1.14 pk {
249 1.14 pk return busattach(parent, child, args, BUS_VME16);
250 1.14 pk }
251 1.14 pk
252 1.4 deraadt void
253 1.4 deraadt vmesattach(parent, self, args)
254 1.4 deraadt struct device *parent, *self;
255 1.4 deraadt void *args;
256 1.4 deraadt {
257 1.14 pk if (self->dv_unit > 0) {
258 1.14 pk printf(" unsupported\n");
259 1.14 pk return;
260 1.14 pk }
261 1.14 pk printf("\n");
262 1.14 pk
263 1.5 deraadt if (vmeints == NULL) {
264 1.5 deraadt vmeints = (struct intrhand **)malloc(256 *
265 1.5 deraadt sizeof(struct intrhand *), M_TEMP, M_NOWAIT);
266 1.5 deraadt bzero(vmeints, 256 * sizeof(struct intrhand *));
267 1.5 deraadt }
268 1.14 pk (void)config_search(vmes_scan, self, args);
269 1.14 pk bus_untmp();
270 1.14 pk }
271 1.14 pk
272 1.14 pk int
273 1.14 pk vmel_scan(parent, child, args)
274 1.14 pk struct device *parent;
275 1.14 pk void *child, *args;
276 1.14 pk {
277 1.14 pk return busattach(parent, child, args, BUS_VME32);
278 1.4 deraadt }
279 1.4 deraadt
280 1.4 deraadt void
281 1.4 deraadt vmelattach(parent, self, args)
282 1.4 deraadt struct device *parent, *self;
283 1.4 deraadt void *args;
284 1.4 deraadt {
285 1.14 pk if (self->dv_unit > 0) {
286 1.14 pk printf(" unsupported\n");
287 1.14 pk return;
288 1.14 pk }
289 1.14 pk printf("\n");
290 1.14 pk
291 1.5 deraadt if (vmeints == NULL) {
292 1.5 deraadt vmeints = (struct intrhand **)malloc(256 *
293 1.5 deraadt sizeof(struct intrhand *), M_TEMP, M_NOWAIT);
294 1.5 deraadt bzero(vmeints, 256 * sizeof(struct intrhand *));
295 1.5 deraadt }
296 1.14 pk (void)config_search(vmel_scan, self, args);
297 1.14 pk bus_untmp();
298 1.4 deraadt }
299 1.4 deraadt
300 1.5 deraadt int pil_to_vme[] = {
301 1.5 deraadt -1, /* pil 0 */
302 1.5 deraadt -1, /* pil 1 */
303 1.5 deraadt 1, /* pil 2 */
304 1.5 deraadt 2, /* pil 3 */
305 1.5 deraadt -1, /* pil 4 */
306 1.5 deraadt 3, /* pil 5 */
307 1.5 deraadt -1, /* pil 6 */
308 1.5 deraadt 4, /* pil 7 */
309 1.5 deraadt -1, /* pil 8 */
310 1.5 deraadt 5, /* pil 9 */
311 1.5 deraadt -1, /* pil 10 */
312 1.5 deraadt 6, /* pil 11 */
313 1.5 deraadt -1, /* pil 12 */
314 1.5 deraadt 7, /* pil 13 */
315 1.5 deraadt -1, /* pil 14 */
316 1.5 deraadt -1, /* pil 15 */
317 1.5 deraadt };
318 1.5 deraadt
319 1.5 deraadt int
320 1.5 deraadt vmeintr(arg)
321 1.5 deraadt void *arg;
322 1.5 deraadt {
323 1.5 deraadt int level = (int)arg, vec;
324 1.5 deraadt struct intrhand *ih;
325 1.5 deraadt int i = 0;
326 1.5 deraadt
327 1.20 christos vec = ldcontrolb((caddr_t)
328 1.20 christos (AC_VMEINTVEC | (pil_to_vme[level] << 1) | 1));
329 1.6 deraadt if (vec == -1) {
330 1.6 deraadt printf("vme: spurious interrupt\n");
331 1.6 deraadt return 0;
332 1.6 deraadt }
333 1.5 deraadt
334 1.5 deraadt for (ih = vmeints[vec]; ih; ih = ih->ih_next)
335 1.5 deraadt if (ih->ih_fun)
336 1.5 deraadt i += (ih->ih_fun)(ih->ih_arg);
337 1.5 deraadt return (i);
338 1.5 deraadt }
339 1.5 deraadt
340 1.5 deraadt void
341 1.5 deraadt vmeintr_establish(vec, level, ih)
342 1.5 deraadt int vec, level;
343 1.5 deraadt struct intrhand *ih;
344 1.5 deraadt {
345 1.5 deraadt struct intrhand *ihs;
346 1.5 deraadt
347 1.8 deraadt if (vec == -1)
348 1.8 deraadt panic("vmeintr_establish: uninitialized vec\n");
349 1.8 deraadt
350 1.5 deraadt if (vmeints[vec] == NULL)
351 1.5 deraadt vmeints[vec] = ih;
352 1.5 deraadt else {
353 1.5 deraadt for (ihs = vmeints[vec]; ihs->ih_next; ihs = ihs->ih_next)
354 1.5 deraadt ;
355 1.5 deraadt ihs->ih_next = ih;
356 1.5 deraadt }
357 1.5 deraadt
358 1.5 deraadt /* ensure the interrupt subsystem will call us at this level */
359 1.5 deraadt for (ihs = intrhand[level]; ihs; ihs = ihs->ih_next)
360 1.5 deraadt if (ihs->ih_fun == vmeintr)
361 1.5 deraadt return;
362 1.5 deraadt
363 1.5 deraadt ihs = (struct intrhand *)malloc(sizeof(struct intrhand),
364 1.5 deraadt M_TEMP, M_NOWAIT);
365 1.5 deraadt if (ihs == NULL)
366 1.5 deraadt panic("vme_addirq");
367 1.5 deraadt bzero(ihs, sizeof *ihs);
368 1.5 deraadt ihs->ih_fun = vmeintr;
369 1.5 deraadt ihs->ih_arg = (void *)level;
370 1.5 deraadt intr_establish(level, ihs);
371 1.5 deraadt }
372 1.4 deraadt
373 1.2 deraadt #define getpte(va) lda(va, ASI_PTE)
374 1.2 deraadt
375 1.2 deraadt /*
376 1.2 deraadt * If we can find a mapping that was established by the rom, use it.
377 1.2 deraadt * Else, create a new mapping.
378 1.2 deraadt */
379 1.2 deraadt void *
380 1.4 deraadt bus_map(pa, len, bustype)
381 1.17 pk struct rom_reg *pa;
382 1.2 deraadt int len;
383 1.4 deraadt int bustype;
384 1.2 deraadt {
385 1.18 pk u_long pf = (u_long)(pa->rr_paddr) >> PGSHIFT;
386 1.2 deraadt u_long va, pte;
387 1.20 christos int pgtype = -1;
388 1.4 deraadt
389 1.4 deraadt switch (bt2pmt[bustype]) {
390 1.4 deraadt case PMAP_OBIO:
391 1.4 deraadt pgtype = PG_OBIO;
392 1.4 deraadt break;
393 1.4 deraadt case PMAP_VME32:
394 1.4 deraadt pgtype = PG_VME32;
395 1.4 deraadt break;
396 1.4 deraadt case PMAP_VME16:
397 1.4 deraadt pgtype = PG_VME16;
398 1.4 deraadt break;
399 1.4 deraadt }
400 1.2 deraadt
401 1.3 deraadt if (len <= NBPG) {
402 1.3 deraadt for (va = OLDMON_STARTVADDR; va < OLDMON_ENDVADDR; va += NBPG) {
403 1.3 deraadt pte = getpte(va);
404 1.4 deraadt if ((pte & PG_V) != 0 && (pte & PG_TYPE) == pgtype &&
405 1.3 deraadt (pte & PG_PFNUM) == pf)
406 1.19 chuck return ((void *)
407 1.19 chuck (va | ((u_long)pa->rr_paddr & PGOFSET)) );
408 1.19 chuck /* note: preserve page offset */
409 1.3 deraadt }
410 1.2 deraadt }
411 1.17 pk return mapiodev(pa, 0, len, bustype);
412 1.2 deraadt }
413 1.2 deraadt
414 1.2 deraadt void *
415 1.4 deraadt bus_tmp(pa, bustype)
416 1.2 deraadt void *pa;
417 1.4 deraadt int bustype;
418 1.2 deraadt {
419 1.3 deraadt vm_offset_t addr = (vm_offset_t)pa & ~PGOFSET;
420 1.4 deraadt int pmtype = bt2pmt[bustype];
421 1.3 deraadt
422 1.13 mycroft pmap_enter(pmap_kernel(), TMPMAP_VA,
423 1.4 deraadt addr | pmtype | PMAP_NC,
424 1.2 deraadt VM_PROT_READ | VM_PROT_WRITE, 1);
425 1.19 chuck return ((void *)(TMPMAP_VA | ((u_long) pa & PGOFSET)) );
426 1.2 deraadt }
427 1.2 deraadt
428 1.2 deraadt void
429 1.4 deraadt bus_untmp()
430 1.2 deraadt {
431 1.13 mycroft pmap_remove(pmap_kernel(), TMPMAP_VA, TMPMAP_VA+NBPG);
432 1.1 deraadt }
433 1.17 pk #endif
434