obio.c revision 1.22 1 /* $NetBSD: obio.c,v 1.22 1996/03/31 22:28:38 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 *, void *, 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 *, char *));
70 static int busattach __P((struct device *, void *, void *, int));
71 void * bus_map __P((struct rom_reg *, int, int));
72 int obio_scan __P((struct device *, void *, void *));
73 int vmes_scan __P((struct device *, void *, void *));
74 int vmel_scan __P((struct device *, void *, 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, vcf, aux)
106 struct device *parent;
107 void *vcf, *aux;
108 {
109 struct cfdata *cf = vcf;
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 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 "auxio",
164 "",
165 /* place device to ignore here */
166 "interrupt",
167 NULL
168 };
169 #endif
170
171 if (CPU_ISSUN4) {
172 if (self->dv_unit > 0) {
173 printf(" unsupported\n");
174 return;
175 }
176 printf("\n");
177
178 (void)config_search(obio_scan, self, args);
179 bus_untmp();
180 }
181
182 #if defined(SUN4M)
183 if (!CPU_ISSUN4M)
184 return;
185
186 /*
187 * There is only one obio bus (it is in fact one of the Sbus slots)
188 * How about VME?
189 */
190 if (sc->sc_dev.dv_unit > 0) {
191 printf(" unsupported\n");
192 return;
193 }
194
195 printf("\n");
196
197 if (ra->ra_bp != NULL && strcmp(ra->ra_bp->name, "obio") == 0)
198 oca.ca_ra.ra_bp = ra->ra_bp + 1;
199 else
200 oca.ca_ra.ra_bp = NULL;
201
202 sc->bu.scu_sbus.sc_range = ra->ra_range;
203 sc->bu.scu_sbus.sc_nrange = ra->ra_nrange;
204
205 /*
206 * Loop through ROM children, fixing any relative addresses
207 * and then configuring each device.
208 * We first do the crucial ones, such as eeprom, etc.
209 */
210 node0 = firstchild(ra->ra_node);
211 for (ssp = special4m ; *(sp = *ssp) != 0; ssp++) {
212 if ((node = findnode(node0, sp)) == 0) {
213 printf("could not find %s amongst obio devices\n", sp);
214 panic(sp);
215 }
216 if (!romprop(&oca.ca_ra, sp, node))
217 continue;
218
219 sbus_translate(self, &oca);
220 oca.ca_bustype = BUS_OBIO;
221 (void) config_found(&sc->sc_dev, (void *)&oca, busprint);
222 }
223
224 for (node = node0; node; node = nextsibling(node)) {
225 name = getpropstring(node, "name");
226 for (ssp = special4m ; (sp = *ssp) != NULL; ssp++)
227 if (strcmp(name, sp) == 0)
228 break;
229
230 if (sp != NULL || !romprop(&oca.ca_ra, name, node))
231 continue;
232
233 if (strcmp(name, "zs") == 0)
234 /* XXX - see autoconf.c for this hack */
235 autoconf_nzs++;
236
237 /* Translate into parent address spaces */
238 sbus_translate(self, &oca);
239 oca.ca_bustype = BUS_OBIO;
240 (void) config_found(&sc->sc_dev, (void *)&oca, busprint);
241 }
242 #endif
243 }
244
245 void
246 vmesattach(parent, self, args)
247 struct device *parent, *self;
248 void *args;
249 {
250 if (CPU_ISSUN4M || self->dv_unit > 0) {
251 printf(" unsupported\n");
252 return;
253 }
254 printf("\n");
255
256 if (vmeints == NULL) {
257 vmeints = (struct intrhand **)malloc(256 *
258 sizeof(struct intrhand *), M_TEMP, M_NOWAIT);
259 bzero(vmeints, 256 * sizeof(struct intrhand *));
260 }
261 (void)config_search(vmes_scan, self, args);
262 bus_untmp();
263 }
264
265 void
266 vmelattach(parent, self, args)
267 struct device *parent, *self;
268 void *args;
269 {
270 if (CPU_ISSUN4M || self->dv_unit > 0) {
271 printf(" unsupported\n");
272 return;
273 }
274 printf("\n");
275
276 if (vmeints == NULL) {
277 vmeints = (struct intrhand **)malloc(256 *
278 sizeof(struct intrhand *), M_TEMP, M_NOWAIT);
279 bzero(vmeints, 256 * sizeof(struct intrhand *));
280 }
281 (void)config_search(vmel_scan, self, args);
282 bus_untmp();
283 }
284
285 int
286 busattach(parent, child, args, bustype)
287 struct device *parent;
288 void *args, *child;
289 int bustype;
290 {
291 #if defined(SUN4)
292 struct cfdata *cf = child;
293 register struct confargs *ca = args;
294 struct confargs oca;
295 caddr_t tmp;
296
297 if (bustype == BUS_OBIO && CPU_ISSUN4) {
298 /*
299 * On the 4/100 obio addresses must be mapped at
300 * 0x0YYYYYYY, but alias higher up (we avoid the
301 * alias condition because it causes pmap difficulties)
302 * XXX: We also assume that 4/[23]00 obio addresses
303 * must be 0xZYYYYYYY, where (Z != 0)
304 */
305 if (cpumod == SUN4_100 && (cf->cf_loc[0] & 0xf0000000))
306 return 0;
307 if (cpumod != SUN4_100 && !(cf->cf_loc[0] & 0xf0000000))
308 return 0;
309 }
310
311 if (parent->dv_cfdata->cf_driver->cd_indirect) {
312 printf(" indirect devices not supported\n");
313 return 0;
314 }
315
316 oca.ca_ra.ra_iospace = -1;
317 oca.ca_ra.ra_paddr = (void *)cf->cf_loc[0];
318 oca.ca_ra.ra_len = 0;
319 oca.ca_ra.ra_nreg = 1;
320 tmp = NULL;
321 if (oca.ca_ra.ra_paddr)
322 tmp = bus_tmp(oca.ca_ra.ra_paddr,
323 bustype);
324 oca.ca_ra.ra_vaddr = tmp;
325 oca.ca_ra.ra_intr[0].int_pri = cf->cf_loc[1];
326 if (bustype == BUS_VME16 || bustype == BUS_VME32)
327 oca.ca_ra.ra_intr[0].int_vec = cf->cf_loc[2];
328 else
329 oca.ca_ra.ra_intr[0].int_vec = -1;
330 oca.ca_ra.ra_nintr = 1;
331 oca.ca_ra.ra_name = cf->cf_driver->cd_name;
332 if (ca->ca_ra.ra_bp != NULL &&
333 ((bustype == BUS_VME16 && strcmp(ca->ca_ra.ra_bp->name,"vmes") ==0) ||
334 (bustype == BUS_VME32 && strcmp(ca->ca_ra.ra_bp->name,"vmel") ==0) ||
335 (bustype == BUS_OBIO && strcmp(ca->ca_ra.ra_bp->name,"obio") == 0)))
336 oca.ca_ra.ra_bp = ca->ca_ra.ra_bp + 1;
337 else
338 oca.ca_ra.ra_bp = NULL;
339 oca.ca_bustype = bustype;
340
341 if ((*cf->cf_attach->ca_match)(parent, cf, &oca) == 0)
342 return 0;
343
344 /*
345 * check if XXmatch routine replaced the temporary mapping with
346 * a real mapping. If not, then make sure we don't pass the
347 * tmp mapping to the attach routine.
348 */
349 if (oca.ca_ra.ra_vaddr == tmp)
350 oca.ca_ra.ra_vaddr = NULL; /* wipe out tmp address */
351 /*
352 * the match routine will set "ra_len" if it wants us to
353 * establish a mapping for it.
354 * (which won't be seen on future XXmatch calls,
355 * so not as useful as it seems.)
356 */
357 if (oca.ca_ra.ra_len)
358 oca.ca_ra.ra_vaddr =
359 bus_map(oca.ca_ra.ra_reg,
360 oca.ca_ra.ra_len, oca.ca_bustype);
361
362 config_attach(parent, cf, &oca, busprint);
363 return 1;
364 #else
365 return 0;
366 #endif
367 }
368
369 int
370 obio_scan(parent, child, args)
371 struct device *parent;
372 void *child, *args;
373 {
374 return busattach(parent, child, args, BUS_OBIO);
375 }
376
377 int
378 vmes_scan(parent, child, args)
379 struct device *parent;
380 void *child, *args;
381 {
382 return busattach(parent, child, args, BUS_VME16);
383 }
384
385 int
386 vmel_scan(parent, child, args)
387 struct device *parent;
388 void *child, *args;
389 {
390 return busattach(parent, child, args, BUS_VME32);
391 }
392
393 int pil_to_vme[] = {
394 -1, /* pil 0 */
395 -1, /* pil 1 */
396 1, /* pil 2 */
397 2, /* pil 3 */
398 -1, /* pil 4 */
399 3, /* pil 5 */
400 -1, /* pil 6 */
401 4, /* pil 7 */
402 -1, /* pil 8 */
403 5, /* pil 9 */
404 -1, /* pil 10 */
405 6, /* pil 11 */
406 -1, /* pil 12 */
407 7, /* pil 13 */
408 -1, /* pil 14 */
409 -1, /* pil 15 */
410 };
411
412 int
413 vmeintr(arg)
414 void *arg;
415 {
416 int level = (int)arg, vec;
417 struct intrhand *ih;
418 int i = 0;
419
420 #ifdef DIAGNOSTIC
421 if (!CPU_ISSUN4) {
422 panic("vme: spurious interrupt");
423 }
424 #endif
425
426 vec = ldcontrolb((caddr_t)
427 (AC_VMEINTVEC | (pil_to_vme[level] << 1) | 1));
428 if (vec == -1) {
429 printf("vme: spurious interrupt\n");
430 return 0;
431 }
432
433 for (ih = vmeints[vec]; ih; ih = ih->ih_next)
434 if (ih->ih_fun)
435 i += (ih->ih_fun)(ih->ih_arg);
436 return (i);
437 }
438
439 void
440 vmeintr_establish(vec, level, ih)
441 int vec, level;
442 struct intrhand *ih;
443 {
444 struct intrhand *ihs;
445
446 if (!CPU_ISSUN4) {
447 panic("vmeintr_establish: not supported on cpu-type %d",
448 cputyp);
449 }
450
451 if (vec == -1)
452 panic("vmeintr_establish: uninitialized vec\n");
453
454 if (vmeints[vec] == NULL)
455 vmeints[vec] = ih;
456 else {
457 for (ihs = vmeints[vec]; ihs->ih_next; ihs = ihs->ih_next)
458 ;
459 ihs->ih_next = ih;
460 }
461
462 /* ensure the interrupt subsystem will call us at this level */
463 for (ihs = intrhand[level]; ihs; ihs = ihs->ih_next)
464 if (ihs->ih_fun == vmeintr)
465 return;
466
467 ihs = (struct intrhand *)malloc(sizeof(struct intrhand),
468 M_TEMP, M_NOWAIT);
469 if (ihs == NULL)
470 panic("vme_addirq");
471 bzero(ihs, sizeof *ihs);
472 ihs->ih_fun = vmeintr;
473 ihs->ih_arg = (void *)level;
474 intr_establish(level, ihs);
475 }
476
477 #define getpte(va) lda(va, ASI_PTE)
478
479 /*
480 * If we can find a mapping that was established by the rom, use it.
481 * Else, create a new mapping.
482 */
483 void *
484 bus_map(pa, len, bustype)
485 struct rom_reg *pa;
486 int len;
487 int bustype;
488 {
489 u_long pf = (u_long)(pa->rr_paddr) >> PGSHIFT;
490 u_long va, pte;
491 int pgtype = -1;
492
493 switch (bt2pmt[bustype]) {
494 case PMAP_OBIO:
495 pgtype = PG_OBIO;
496 break;
497 case PMAP_VME32:
498 pgtype = PG_VME32;
499 break;
500 case PMAP_VME16:
501 pgtype = PG_VME16;
502 break;
503 }
504
505 if (len <= NBPG) {
506 for (va = OLDMON_STARTVADDR; va < OLDMON_ENDVADDR; va += NBPG) {
507 pte = getpte(va);
508 if ((pte & PG_V) != 0 && (pte & PG_TYPE) == pgtype &&
509 (pte & PG_PFNUM) == pf)
510 return ((void *)
511 (va | ((u_long)pa->rr_paddr & PGOFSET)) );
512 /* note: preserve page offset */
513 }
514 }
515 return mapiodev(pa, 0, len, bustype);
516 }
517
518 void *
519 bus_tmp(pa, bustype)
520 void *pa;
521 int bustype;
522 {
523 vm_offset_t addr = (vm_offset_t)pa & ~PGOFSET;
524 int pmtype = bt2pmt[bustype];
525
526 pmap_enter(pmap_kernel(), TMPMAP_VA,
527 addr | pmtype | PMAP_NC,
528 VM_PROT_READ | VM_PROT_WRITE, 1);
529 return ((void *)(TMPMAP_VA | ((u_long) pa & PGOFSET)) );
530 }
531
532 void
533 bus_untmp()
534 {
535 pmap_remove(pmap_kernel(), TMPMAP_VA, TMPMAP_VA+NBPG);
536 }
537