obio.c revision 1.29 1 /* $NetBSD: obio.c,v 1.29 1996/12/10 23:24:56 pk Exp $ */
2
3 /*
4 * Copyright (c) 1993, 1994 Theo de Raadt
5 * Copyright (c) 1995 Paul Kranenburg
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Theo de Raadt.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/device.h>
37 #include <sys/malloc.h>
38
39 #ifdef DEBUG
40 #include <sys/proc.h>
41 #include <sys/syslog.h>
42 #endif
43
44 #include <vm/vm.h>
45
46 #include <machine/autoconf.h>
47 #include <machine/pmap.h>
48 #include <machine/oldmon.h>
49 #include <machine/cpu.h>
50 #include <machine/ctlreg.h>
51 #include <sparc/sparc/asm.h>
52 #include <sparc/sparc/vaddrs.h>
53 #include <sparc/dev/sbusvar.h>
54
55 struct bus_softc {
56 union {
57 struct device scu_dev; /* base device */
58 struct sbus_softc scu_sbus; /* obio is another sbus slot */
59 } bu;
60 #define sc_dev bu.scu_dev
61 };
62
63 /* autoconfiguration driver */
64 static int busmatch __P((struct device *, struct cfdata *, void *));
65 static void obioattach __P((struct device *, struct device *, void *));
66 static void vmesattach __P((struct device *, struct device *, void *));
67 static void vmelattach __P((struct device *, struct device *, void *));
68
69 int busprint __P((void *, const char *));
70 static int busattach __P((struct device *, struct cfdata *, void *, int));
71 void * bus_map __P((struct rom_reg *, int, int));
72 int obio_scan __P((struct device *, struct cfdata *, void *));
73 int vmes_scan __P((struct device *, struct cfdata *, void *));
74 int vmel_scan __P((struct device *, struct cfdata *, void *));
75 int vmeintr __P((void *));
76
77 struct cfattach obio_ca = {
78 sizeof(struct bus_softc), busmatch, obioattach
79 };
80
81 struct cfdriver obio_cd = {
82 NULL, "obio", DV_DULL
83 };
84
85 struct cfattach vmel_ca = {
86 sizeof(struct bus_softc), busmatch, vmelattach
87 };
88
89 struct cfdriver vmel_cd = {
90 NULL, "vmel", DV_DULL
91 };
92
93 struct cfattach vmes_ca = {
94 sizeof(struct bus_softc), busmatch, vmesattach
95 };
96
97 struct cfdriver vmes_cd = {
98 NULL, "vmes", DV_DULL
99 };
100
101 struct intrhand **vmeints;
102
103
104 int
105 busmatch(parent, cf, aux)
106 struct device *parent;
107 struct cfdata *cf;
108 void *aux;
109 {
110 register struct confargs *ca = aux;
111 register struct romaux *ra = &ca->ca_ra;
112
113 if (CPU_ISSUN4M)
114 return (strcmp(cf->cf_driver->cd_name, ra->ra_name) == 0);
115
116 if (!CPU_ISSUN4)
117 return (0);
118
119 return (strcmp(cf->cf_driver->cd_name, ra->ra_name) == 0);
120 }
121
122 int
123 busprint(args, obio)
124 void *args;
125 const char *obio;
126 {
127 register struct confargs *ca = args;
128
129 if (ca->ca_ra.ra_name == NULL)
130 ca->ca_ra.ra_name = "<unknown>";
131
132 if (obio)
133 printf("[%s at %s]", ca->ca_ra.ra_name, obio);
134
135 printf(" addr %p", ca->ca_ra.ra_paddr);
136
137 if (CPU_ISSUN4 && ca->ca_ra.ra_intr[0].int_vec != -1)
138 printf(" vec 0x%x", ca->ca_ra.ra_intr[0].int_vec);
139
140 return (UNCONF);
141 }
142
143
144 void
145 obioattach(parent, self, args)
146 struct device *parent, *self;
147 void *args;
148 {
149 #if defined(SUN4M)
150 register struct bus_softc *sc = (struct bus_softc *)self;
151 struct confargs oca, *ca = args;
152 register struct romaux *ra = &ca->ca_ra;
153 register int node0, node;
154 register char *name;
155 register const char *sp;
156 const char *const *ssp;
157 extern int autoconf_nzs;
158
159 static const char *const special4m[] = {
160 /* find these first */
161 "eeprom",
162 "counter",
163 #if 0 /* Not all sun4m's have an `auxio' */
164 "auxio",
165 #endif
166 "",
167 /* place device to ignore here */
168 "interrupt",
169 NULL
170 };
171 #endif
172
173 if (CPU_ISSUN4) {
174 if (self->dv_unit > 0) {
175 printf(" unsupported\n");
176 return;
177 }
178 printf("\n");
179
180 (void)config_search(obio_scan, self, args);
181 bus_untmp();
182 }
183
184 #if defined(SUN4M)
185 if (!CPU_ISSUN4M)
186 return;
187
188 /*
189 * There is only one obio bus (it is in fact one of the Sbus slots)
190 * How about VME?
191 */
192 if (sc->sc_dev.dv_unit > 0) {
193 printf(" unsupported\n");
194 return;
195 }
196
197 printf("\n");
198
199 if (ra->ra_bp != NULL && strcmp(ra->ra_bp->name, "obio") == 0)
200 oca.ca_ra.ra_bp = ra->ra_bp + 1;
201 else
202 oca.ca_ra.ra_bp = NULL;
203
204 sc->bu.scu_sbus.sc_range = ra->ra_range;
205 sc->bu.scu_sbus.sc_nrange = ra->ra_nrange;
206
207 /*
208 * Loop through ROM children, fixing any relative addresses
209 * and then configuring each device.
210 * We first do the crucial ones, such as eeprom, etc.
211 */
212 node0 = firstchild(ra->ra_node);
213 for (ssp = special4m ; *(sp = *ssp) != 0; ssp++) {
214 if ((node = findnode(node0, sp)) == 0) {
215 printf("could not find %s amongst obio devices\n", sp);
216 panic(sp);
217 }
218 if (!romprop(&oca.ca_ra, sp, node))
219 continue;
220
221 sbus_translate(self, &oca);
222 oca.ca_bustype = BUS_OBIO;
223 (void) config_found(&sc->sc_dev, (void *)&oca, busprint);
224 }
225
226 for (node = node0; node; node = nextsibling(node)) {
227 name = getpropstring(node, "name");
228 for (ssp = special4m ; (sp = *ssp) != NULL; ssp++)
229 if (strcmp(name, sp) == 0)
230 break;
231
232 if (sp != NULL || !romprop(&oca.ca_ra, name, node))
233 continue;
234
235 if (strcmp(name, "zs") == 0)
236 /* XXX - see autoconf.c for this hack */
237 autoconf_nzs++;
238
239 /* Translate into parent address spaces */
240 sbus_translate(self, &oca);
241 oca.ca_bustype = BUS_OBIO;
242 (void) config_found(&sc->sc_dev, (void *)&oca, busprint);
243 }
244 #endif
245 }
246
247 void
248 vmesattach(parent, self, args)
249 struct device *parent, *self;
250 void *args;
251 {
252 if (CPU_ISSUN4M || self->dv_unit > 0) {
253 printf(" unsupported\n");
254 return;
255 }
256 printf("\n");
257
258 if (vmeints == NULL) {
259 vmeints = (struct intrhand **)malloc(256 *
260 sizeof(struct intrhand *), M_TEMP, M_NOWAIT);
261 bzero(vmeints, 256 * sizeof(struct intrhand *));
262 }
263 (void)config_search(vmes_scan, self, args);
264 bus_untmp();
265 }
266
267 void
268 vmelattach(parent, self, args)
269 struct device *parent, *self;
270 void *args;
271 {
272 if (CPU_ISSUN4M || self->dv_unit > 0) {
273 printf(" unsupported\n");
274 return;
275 }
276 printf("\n");
277
278 if (vmeints == NULL) {
279 vmeints = (struct intrhand **)malloc(256 *
280 sizeof(struct intrhand *), M_TEMP, M_NOWAIT);
281 bzero(vmeints, 256 * sizeof(struct intrhand *));
282 }
283 (void)config_search(vmel_scan, self, args);
284 bus_untmp();
285 }
286
287 int
288 busattach(parent, cf, args, bustype)
289 struct device *parent;
290 struct cfdata *cf;
291 void *args;
292 int bustype;
293 {
294 #if defined(SUN4)
295 register struct confargs *ca = args;
296 struct confargs oca;
297 caddr_t tmp;
298
299 if (bustype == BUS_OBIO && CPU_ISSUN4) {
300
301 /*
302 * avoid sun4m entries which don't have valid PA's.
303 * no point in even probing them.
304 */
305 if (cf->cf_loc[0] == -1) return 0;
306
307 /*
308 * On the 4/100 obio addresses must be mapped at
309 * 0x0YYYYYYY, but alias higher up (we avoid the
310 * alias condition because it causes pmap difficulties)
311 * XXX: We also assume that 4/[23]00 obio addresses
312 * must be 0xZYYYYYYY, where (Z != 0)
313 */
314 if (cpumod == SUN4_100 && (cf->cf_loc[0] & 0xf0000000))
315 return 0;
316 if (cpumod != SUN4_100 && !(cf->cf_loc[0] & 0xf0000000))
317 return 0;
318 }
319
320 oca.ca_ra.ra_iospace = -1;
321 oca.ca_ra.ra_paddr = (void *)cf->cf_loc[0];
322 oca.ca_ra.ra_len = 0;
323 oca.ca_ra.ra_nreg = 1;
324 if (oca.ca_ra.ra_paddr)
325 tmp = (caddr_t)bus_tmp(oca.ca_ra.ra_paddr,
326 bustype);
327 else
328 tmp = NULL;
329 oca.ca_ra.ra_vaddr = tmp;
330 oca.ca_ra.ra_intr[0].int_pri = cf->cf_loc[1];
331 if (bustype == BUS_VME16 || bustype == BUS_VME32)
332 oca.ca_ra.ra_intr[0].int_vec = cf->cf_loc[2];
333 else
334 oca.ca_ra.ra_intr[0].int_vec = -1;
335 oca.ca_ra.ra_nintr = 1;
336 oca.ca_ra.ra_name = cf->cf_driver->cd_name;
337 if (ca->ca_ra.ra_bp != NULL &&
338 ((bustype == BUS_VME16 && strcmp(ca->ca_ra.ra_bp->name,"vmes") ==0) ||
339 (bustype == BUS_VME32 && strcmp(ca->ca_ra.ra_bp->name,"vmel") ==0) ||
340 (bustype == BUS_OBIO && strcmp(ca->ca_ra.ra_bp->name,"obio") == 0)))
341 oca.ca_ra.ra_bp = ca->ca_ra.ra_bp + 1;
342 else
343 oca.ca_ra.ra_bp = NULL;
344 oca.ca_bustype = bustype;
345
346 if ((*cf->cf_attach->ca_match)(parent, cf, &oca) == 0)
347 return 0;
348
349 /*
350 * check if XXmatch routine replaced the temporary mapping with
351 * a real mapping. If not, then make sure we don't pass the
352 * tmp mapping to the attach routine.
353 */
354 if (oca.ca_ra.ra_vaddr == tmp)
355 oca.ca_ra.ra_vaddr = NULL; /* wipe out tmp address */
356 /*
357 * the match routine will set "ra_len" if it wants us to
358 * establish a mapping for it.
359 * (which won't be seen on future XXmatch calls,
360 * so not as useful as it seems.)
361 */
362 if (oca.ca_ra.ra_len)
363 oca.ca_ra.ra_vaddr =
364 bus_map(oca.ca_ra.ra_reg,
365 oca.ca_ra.ra_len, oca.ca_bustype);
366
367 config_attach(parent, cf, &oca, busprint);
368 return 1;
369 #else
370 return 0;
371 #endif
372 }
373
374 int
375 obio_scan(parent, child, args)
376 struct device *parent;
377 struct cfdata *child;
378 void *args;
379 {
380 return busattach(parent, child, args, BUS_OBIO);
381 }
382
383 int
384 vmes_scan(parent, child, args)
385 struct device *parent;
386 struct cfdata *child;
387 void *args;
388 {
389 return busattach(parent, child, args, BUS_VME16);
390 }
391
392 int
393 vmel_scan(parent, child, args)
394 struct device *parent;
395 struct cfdata *child;
396 void *args;
397 {
398 return busattach(parent, child, args, BUS_VME32);
399 }
400
401 int pil_to_vme[] = {
402 -1, /* pil 0 */
403 -1, /* pil 1 */
404 1, /* pil 2 */
405 2, /* pil 3 */
406 -1, /* pil 4 */
407 3, /* pil 5 */
408 -1, /* pil 6 */
409 4, /* pil 7 */
410 -1, /* pil 8 */
411 5, /* pil 9 */
412 -1, /* pil 10 */
413 6, /* pil 11 */
414 -1, /* pil 12 */
415 7, /* pil 13 */
416 -1, /* pil 14 */
417 -1, /* pil 15 */
418 };
419
420 int
421 vmeintr(arg)
422 void *arg;
423 {
424 int level = (int)arg, vec;
425 struct intrhand *ih;
426 int i = 0;
427
428 #ifdef DIAGNOSTIC
429 if (!CPU_ISSUN4) {
430 panic("vme: spurious interrupt");
431 }
432 #endif
433
434 vec = ldcontrolb((caddr_t)
435 (AC_VMEINTVEC | (pil_to_vme[level] << 1) | 1));
436 if (vec == -1) {
437 printf("vme: spurious interrupt\n");
438 return 0;
439 }
440
441 for (ih = vmeints[vec]; ih; ih = ih->ih_next)
442 if (ih->ih_fun)
443 i += (ih->ih_fun)(ih->ih_arg);
444 return (i);
445 }
446
447 void
448 vmeintr_establish(vec, level, ih)
449 int vec, level;
450 struct intrhand *ih;
451 {
452 struct intrhand *ihs;
453
454 if (!CPU_ISSUN4) {
455 panic("vmeintr_establish: not supported on cpu-type %d",
456 cputyp);
457 }
458
459 if (vec == -1)
460 panic("vmeintr_establish: uninitialized vec\n");
461
462 if (vmeints[vec] == NULL)
463 vmeints[vec] = ih;
464 else {
465 for (ihs = vmeints[vec]; ihs->ih_next; ihs = ihs->ih_next)
466 ;
467 ihs->ih_next = ih;
468 }
469
470 /* ensure the interrupt subsystem will call us at this level */
471 for (ihs = intrhand[level]; ihs; ihs = ihs->ih_next)
472 if (ihs->ih_fun == vmeintr)
473 return;
474
475 ihs = (struct intrhand *)malloc(sizeof(struct intrhand),
476 M_TEMP, M_NOWAIT);
477 if (ihs == NULL)
478 panic("vme_addirq");
479 bzero(ihs, sizeof *ihs);
480 ihs->ih_fun = vmeintr;
481 ihs->ih_arg = (void *)level;
482 intr_establish(level, ihs);
483 }
484
485 #define getpte(va) lda(va, ASI_PTE)
486
487 /*
488 * If we can find a mapping that was established by the rom, use it.
489 * Else, create a new mapping.
490 */
491 void *
492 bus_map(pa, len, bustype)
493 struct rom_reg *pa;
494 int len;
495 int bustype;
496 {
497 u_long pf = (u_long)(pa->rr_paddr) >> PGSHIFT;
498 u_long va, pte;
499 int pgtype = -1;
500
501 switch (bt2pmt[bustype]) {
502 case PMAP_OBIO:
503 pgtype = PG_OBIO;
504 break;
505 case PMAP_VME32:
506 pgtype = PG_VME32;
507 break;
508 case PMAP_VME16:
509 pgtype = PG_VME16;
510 break;
511 }
512
513 if (len <= NBPG) {
514 for (va = OLDMON_STARTVADDR; va < OLDMON_ENDVADDR; va += NBPG) {
515 pte = getpte(va);
516 if ((pte & PG_V) != 0 && (pte & PG_TYPE) == pgtype &&
517 (pte & PG_PFNUM) == pf)
518 return ((void *)
519 (va | ((u_long)pa->rr_paddr & PGOFSET)) );
520 /* note: preserve page offset */
521 }
522 }
523 return mapiodev(pa, 0, len, bustype);
524 }
525
526 void *
527 bus_tmp(pa, bustype)
528 void *pa;
529 int bustype;
530 {
531 vm_offset_t addr = (vm_offset_t)pa & ~PGOFSET;
532 int pmtype = bt2pmt[bustype];
533
534 pmap_enter(pmap_kernel(), TMPMAP_VA,
535 addr | pmtype | PMAP_NC,
536 VM_PROT_READ | VM_PROT_WRITE, 1);
537 return ((void *)(TMPMAP_VA | ((u_long) pa & PGOFSET)) );
538 }
539
540 void
541 bus_untmp()
542 {
543 pmap_remove(pmap_kernel(), TMPMAP_VA, TMPMAP_VA+NBPG);
544 }
545