vsbus.c revision 1.2 1 /* $NetBSD: vsbus.c,v 1.2 1996/08/27 21:58:51 cgd Exp $ */
2 /*
3 * Copyright (c) 1996 Ludd, University of Lule}, Sweden.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Ludd by Bertram Barth.
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 at Ludd, University of
19 * Lule}, Sweden and its contributors.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/buf.h>
38 #include <sys/conf.h>
39 #include <sys/file.h>
40 #include <sys/ioctl.h>
41 #include <sys/proc.h>
42 #include <sys/user.h>
43 #include <sys/map.h>
44 #include <sys/device.h>
45 #include <sys/dkstat.h>
46 #include <sys/disklabel.h>
47 #include <sys/syslog.h>
48 #include <sys/stat.h>
49
50 #include <machine/pte.h>
51 #include <machine/sid.h>
52 #include <machine/scb.h>
53 #include <machine/cpu.h>
54 #include <machine/trap.h>
55 #include <machine/nexus.h>
56
57 #include <machine/uvax.h>
58 #include <machine/ka410.h>
59 #include <machine/ka43.h>
60
61 #include <machine/vsbus.h>
62
63 #define trace(x)
64 #define debug(x)
65
66 int vsbus_match __P((struct device *, void *, void *));
67 void vsbus_attach __P((struct device *, struct device *, void *));
68 int vsbus_print __P((void *, const char *));
69
70 void ka410_attach __P((struct device *, struct device *, void *));
71 void ka43_attach __P((struct device *, struct device *, void *));
72
73 struct cfdriver vsbus_cd = {
74 NULL, "vsbus", DV_DULL
75 };
76 struct cfattach vsbus_ca = {
77 sizeof(struct device), vsbus_match, vsbus_attach
78 };
79
80 /*
81 void vsbus_intr_register __P((struct confargs *ca, int (*)(void*), void*));
82 void vsbus_intr_unregister __P((struct confargs *));
83 */
84
85 void vsbus_intr_dispatch __P((int i));
86
87 #define VSBUS_MAXDEVS 8
88 #define VSBUS_MAXINTR 8
89
90 struct confargs *vsbus_devs = NULL;
91
92 #ifdef VAX410 /* also: KA420 */
93 struct confargs ka410_devs[] = {
94 /* name intslot intpri intvec intbit ioaddr */
95 { "dc", 7, 7, 0x2C0, (1<<7), KA410_SER_BASE,
96 6, 6, 0x2C4, (1<<6), 0x01, },
97 { "dc (xmit)", 6, 6, 0x2C4, (1<<6), KA410_SER_BASE, },
98 { "le", 5, 5, 0x250, (1<<5), KA410_LAN_BASE,
99 KA410_NWA_BASE, 0x00, },
100 { "ncr", 1, 1, 0x3F8, (1<<1), KA410_SCS_BASE,
101 KA410_SCS_DADR, KA410_SCS_DCNT, KA410_SCS_DDIR,
102 KA410_DMA_BASE, KA410_DMA_SIZE, 0x00, 0x07, },
103 { "hdc", 0, 0, 0x3FC, (1<<0), KA410_DKC_BASE,
104 0, 0, 0,
105 KA410_DMA_BASE, KA410_DMA_SIZE, 0x00, },
106 #if 0
107 { "dc (recv)", 7, 7, 0x2C0, (1<<7), KA410_SER_BASE, },
108 { "dc (xmit)", 6, 6, 0x2C4, (1<<6), KA410_SER_BASE, },
109 { "hdc9224", 0, 0, 0x3FC, (1<<0), KA410_DKC_BASE, },
110 { "ncr5380", 1, 1, 0x3F8, (1<<1), KA410_SCS_BASE, },
111 { "am7990", 5, 5, 0x250, (1<<5), KA410_LAN_BASE, },
112 { "NETOPT", 4, 4, 0x254, (1<<4), KA410_LAN_BASE, },
113 #endif
114 { "" },
115 };
116 #endif
117
118 #ifdef VAX43
119 struct confargs ka43_devs[] = {
120 /* name intslot intpri intvec intbit ioaddr */
121 { "dc", 7, 7, 0x2C0, (1<<7), KA43_SER_BASE,
122 6, 6, 0x2C4, (1<<6), 0x01, },
123 { "dc (xmit)", 6, 6, 0x2C4, (1<<6), KA43_SER_BASE, },
124 { "le", 5, 5, 0x250, (1<<5), KA43_LAN_BASE,
125 KA43_NWA_BASE, 0x00, },
126 { "ncr", 1, 1, 0x3F8, (1<<1), KA43_SC1_BASE,
127 KA43_SC1_DADR, KA43_SC1_DCNT, KA43_SC1_DDIR,
128 KA43_DMA_BASE, KA43_DMA_SIZE, 0x01, 0x06, },
129 { "ncr", 0, 0, 0x3FC, (1<<0), KA43_SC2_BASE,
130 KA43_SC2_DADR, KA43_SC2_DCNT, KA43_SC2_DDIR,
131 KA43_DMA_BASE, KA43_DMA_SIZE, 0x01, 0x06, },
132 #if 0
133 { "le (2nd)", 4, 4, 0x254, (1<<4), 0x???, },
134 { "NETOPT", 4, 4, 0x254, (1<<4), 0x???, },
135 #endif
136 { "" },
137 };
138 #endif
139
140 int
141 vsbus_print(aux, name)
142 void *aux;
143 const char *name;
144 {
145 struct confargs *ca = aux;
146
147 trace(("vsbus_print(%x, %s)\n", ca->ca_name, name));
148
149 if (name) {
150 printf ("device %s at %s", ca->ca_name, name);
151 return (UNSUPP);
152 }
153 return (UNCONF);
154 }
155
156 int
157 vsbus_match(parent, cf, aux)
158 struct device *parent;
159 void *cf;
160 void *aux;
161 {
162 struct bp_conf *bp = aux;
163
164 trace(("vsbus_match: bp->type = \"%s\"\n", bp->type));
165
166 if (strcmp(bp->type, "vsbus"))
167 return 0;
168 /*
169 * on machines which can have it, the vsbus is always there
170 */
171 if ((vax_bustype & VAX_VSBUS) == 0)
172 return (0);
173
174 return (1);
175 }
176
177 #if 1 /*------------------------------------------------------------*/
178 #if 1
179 #define REG(name) short name; short X##name##X;
180 #else
181 #define REG(name) int name;
182 #endif
183 static volatile struct {/* base address of DZ-controller: 0x200A0000 */
184 REG(csr); /* 00 Csr: control/status register */
185 REG(rbuf); /* 04 Rbuf/Lpr: receive buffer/line param reg. */
186 REG(tcr); /* 08 Tcr: transmit console register */
187 REG(tdr); /* 0C Msr/Tdr: modem status reg/transmit data reg */
188 REG(lpr0); /* 10 Lpr0: */
189 REG(lpr1); /* 14 Lpr0: */
190 REG(lpr2); /* 18 Lpr0: */
191 REG(lpr3); /* 1C Lpr0: */
192 } *dz = (void*)0x200A0000;
193 extern int dzcnrint();
194 extern int dzcntint();
195 int hardclock_count = 0;
196 int
197 ka410_consintr_enable()
198 {
199 vsbus_intr_enable(&ka410_devs[0]);
200 vsbus_intr_enable(&ka410_devs[1]);
201 }
202
203 int
204 ka410_consRecv_intr(p)
205 void *p;
206 {
207 /* printf("ka410_consRecv_intr: hc-count=%d\n", hardclock_count); */
208 dzcnrint();
209 /* printf("gencnrint() returned.\n"); */
210 return(0);
211 }
212
213 int
214 ka410_consXmit_intr(p)
215 void *p;
216 {
217 /* printf("ka410_consXmit_intr: hc-count=%d\n", hardclock_count); */
218 dzcntint();
219 /* printf("gencntint() returned.\n"); */
220 return(0);
221 }
222 #endif /*------------------------------------------------------------*/
223
224 void
225 vsbus_attach(parent, self, aux)
226 struct device *parent, *self;
227 void *aux;
228 {
229 struct confargs *ca;
230 int i;
231
232 printf("\n");
233 trace (("vsbus_attach()\n"));
234
235 printf("vsbus_attach: boardtype = %x\n", vax_boardtype);
236
237 switch (vax_boardtype) {
238 case VAX_BTYP_410:
239 case VAX_BTYP_420:
240 vsbus_devs = ka410_devs;
241 break;
242
243 case VAX_BTYP_43:
244 case VAX_BTYP_46:
245 case VAX_BTYP_49:
246 vsbus_devs = ka43_devs;
247 break;
248
249 default:
250 printf ("unsupported boardtype 0x%x in vsbus_attach()\n",
251 vax_boardtype);
252 return;
253 }
254
255 /*
256 * first setup interrupt-table, so that devices can register
257 * their interrupt-routines...
258 */
259 vsbus_intr_setup();
260
261 /*
262 * now check for all possible devices on this "bus"
263 */
264 for (i=0; i<VSBUS_MAXDEVS; i++) {
265 ca = &vsbus_devs[i];
266 if (*ca->ca_name == '\0')
267 break;
268 config_found(self, (void*)ca, vsbus_print);
269 }
270
271 /*
272 * as long as there's no working DZ-driver, we use this dummy
273 */
274 vsbus_intr_register(&ka410_devs[0], ka410_consRecv_intr, NULL);
275 vsbus_intr_register(&ka410_devs[1], ka410_consXmit_intr, NULL);
276 }
277
278 #define VSBUS_MAX_INTR 8 /* 64? */
279 /*
280 * interrupt service routines are given an int as argument, which is
281 * pushed onto stack as LITERAL. Thus the value is between 0-63.
282 * This array of 64 might be oversized for now, but it's all which
283 * ever will be possible.
284 */
285 struct vsbus_ivec {
286 struct ivec_dsp intr_vec; /* this is referenced in SCB */
287 int intr_count; /* keep track of interrupts */
288 int intr_flags; /* valid, etc. */
289 void (*enab)(int); /* enable interrupt */
290 void (*disab)(int); /* disable interrupt */
291 void (*prep)(int); /* need pre-processing? */
292 int (*handler)(void*); /* isr-routine to call */
293 void *hndlarg; /* args to this routine */
294 void (*postp)(int); /* need post-processing? */
295 } vsbus_ivtab[VSBUS_MAX_INTR];
296
297 /*
298 *
299 */
300 int
301 vsbus_intr_setup()
302 {
303 int i;
304 struct vsbus_ivec *ip;
305 extern struct ivec_dsp idsptch; /* subr.s */
306
307 for (i=0; i<VSBUS_MAX_INTR; i++) {
308 ip = &vsbus_ivtab[i];
309 bcopy(&idsptch, &ip->intr_vec, sizeof(struct ivec_dsp));
310 ip->intr_vec.pushlarg = i;
311 ip->intr_vec.hoppaddr = vsbus_intr_dispatch;
312 ip->intr_count = 0;
313 ip->intr_flags = 0;
314 ip->enab = NULL;
315 ip->disab = NULL;
316 ip->postp = NULL;
317 }
318 switch (vax_boardtype) {
319 case VAX_BTYP_410:
320 case VAX_BTYP_420:
321 case VAX_BTYP_43:
322 case VAX_BTYP_46:
323 case VAX_BTYP_49:
324 ka410_intr_setup();
325 return(0);
326 default:
327 printf("unsupported board-type 0x%x in vsbus_intr_setup()\n",
328 vax_boardtype);
329 return(1);
330 }
331 }
332
333 int
334 vsbus_intr_register(ca, handler, arg)
335 struct confargs *ca;
336 int (*handler)(void*);
337 void *arg;
338 {
339 /* struct device *dev = arg; */
340 int i = ca->ca_intslot;
341 struct vsbus_ivec *ip = &vsbus_ivtab[i];
342
343 trace (("vsbus_intr_register(%s/%d)\n", ca->ca_name, ca->ca_intslot));
344
345 ip->handler = handler;
346 ip->hndlarg = arg;
347 }
348
349 int
350 vsbus_intr_enable(ca)
351 struct confargs *ca;
352 {
353 int i = ca->ca_intslot;
354 struct vsbus_ivec *ip = &vsbus_ivtab[i];
355
356 trace (("vsbus_intr_enable(%s/%d)\n", ca->ca_name, ca->ca_intslot));
357
358 /* XXX check for valid handler etc. !!! */
359 if (ip->handler == NULL) {
360 printf("interrupts for \"%s\"(%d) not enabled: null-handler\n",
361 ca->ca_name, ca->ca_intslot);
362 return;
363 }
364
365 ip->enab(i);
366 }
367
368 int
369 vsbus_intr_disable(ca)
370 struct confargs *ca;
371 {
372 int i = ca->ca_intslot;
373 struct vsbus_ivec *ip = &vsbus_ivtab[i];
374
375 trace (("vsbus_intr_disable(%s/%d)\n", ca->ca_name, i));
376
377 ip->disab(i);
378 }
379
380 int
381 vsbus_intr_unregister(ca)
382 struct confargs *ca;
383 {
384 int i = ca->ca_intslot;
385 struct vsbus_ivec *ip = &vsbus_ivtab[i];
386
387 trace (("vsbus_intr_unregister(%s/%d)\n", ca->ca_name, i));
388
389 ip->handler = NULL;
390 ip->hndlarg = NULL;
391 }
392
393 void
394 vsbus_intr_dispatch(i)
395 register int i;
396 {
397 register struct vsbus_ivec *ip = &vsbus_ivtab[i];
398
399 trace (("vsbus_intr_dispatch(%d)", i));
400
401 if (i < VSBUS_MAX_INTR && ip->handler != NULL) {
402 ip->intr_count++;
403 debug (("intr-count[%d] = %d\n", i, ip->intr_count));
404 (ip->handler)(ip->hndlarg);
405 if (ip->postp)
406 (ip->postp)(i);
407 return;
408 }
409
410 if (i < 0 || i >= VSBUS_MAX_INTR) {
411 printf ("stray interrupt %d on vsbus.\n", i);
412 return;
413 }
414
415 if (!ip->handler) {
416 printf ("unhandled interrupt %d on vsbus.\n", i);
417 return;
418 }
419 }
420
421 /*
422 * These addresses are invalid and will be updated/corrected by
423 * ka410_intr_setup(), but having them this way helps debugging
424 */
425 static volatile u_char *ka410_intmsk = (void*)KA410_INTMSK;
426 static volatile u_char *ka410_intreq = (void*)KA410_INTREQ;
427 static volatile u_char *ka410_intclr = (void*)KA410_INTCLR;
428
429 static void
430 ka410_intr_enable(i)
431 int i;
432 {
433 trace (("ka410_intr_enable(%d)\n", i));
434 *ka410_intmsk |= (1<<i);
435 }
436
437 static void
438 ka410_intr_disable(i)
439 int i;
440 {
441 trace (("ka410_intr_disable(%d)\n", i));
442 *ka410_intmsk &= ~(1<<i);
443 }
444
445 static void
446 ka410_intr_clear(i)
447 int i;
448 {
449 trace (("ka410_intr_clear(%d)\n", i));
450 *ka410_intclr = (1<<i);
451 }
452
453 ka410_intr_setup()
454 {
455 int i;
456 struct vsbus_ivec *ip;
457 void **scbP = (void*)scb;
458
459 trace (("ka410_intr_setup()\n"));
460
461 ka410_intmsk = (void*)uvax_phys2virt(KA410_INTMSK);
462 ka410_intreq = (void*)uvax_phys2virt(KA410_INTREQ);
463 ka410_intclr = (void*)uvax_phys2virt(KA410_INTCLR);
464
465 *ka410_intmsk = 0; /* disable all interrupts */
466 *ka410_intclr = 0xFF; /* clear all old interrupts */
467
468 /*
469 * insert the VS2000-specific routines into ivec-table...
470 */
471 for (i=0; i<8; i++) {
472 ip = &vsbus_ivtab[i];
473 ip->enab = ka410_intr_enable;
474 ip->disab = ka410_intr_disable;
475 /* ip->postp = ka410_intr_clear; bertram XXX */
476 }
477 /*
478 * ...and register the interrupt-vectors in SCB
479 */
480 scbP[IVEC_DC/4] = &vsbus_ivtab[0].intr_vec;
481 scbP[IVEC_SC/4] = &vsbus_ivtab[1].intr_vec;
482 scbP[IVEC_VS/4] = &vsbus_ivtab[2].intr_vec;
483 scbP[IVEC_VF/4] = &vsbus_ivtab[3].intr_vec;
484 scbP[IVEC_NS/4] = &vsbus_ivtab[4].intr_vec;
485 scbP[IVEC_NP/4] = &vsbus_ivtab[5].intr_vec;
486 scbP[IVEC_ST/4] = &vsbus_ivtab[6].intr_vec;
487 scbP[IVEC_SR/4] = &vsbus_ivtab[7].intr_vec;
488 }
489
490 /*
491 *
492 *
493 */
494
495 static volatile struct dma_lock {
496 int dl_locked;
497 int dl_wanted;
498 void *dl_owner;
499 int dl_count;
500 } dmalock = { 0, 0, NULL, 0 };
501
502 int
503 vsbus_lockDMA(ca)
504 struct confargs *ca;
505 {
506 while (dmalock.dl_locked) {
507 dmalock.dl_wanted++;
508 sleep((caddr_t)&dmalock, PRIBIO); /* PLOCK or PRIBIO ? */
509 dmalock.dl_wanted--;
510 }
511 dmalock.dl_locked++;
512 dmalock.dl_owner = ca;
513
514 /*
515 * no checks yet, no timeouts, nothing...
516 */
517
518 #ifdef DEBUG
519 if ((++dmalock.dl_count % 1000) == 0)
520 printf("%d locks, owner: %s\n", dmalock.dl_count, ca->ca_name);
521 #endif
522 return (0);
523 }
524
525 int
526 vsbus_unlockDMA(ca)
527 struct confargs *ca;
528 {
529 if (dmalock.dl_locked != 1 || dmalock.dl_owner != ca) {
530 printf("locking-problem: %d, %s\n", dmalock.dl_locked,
531 (dmalock.dl_owner ? dmalock.dl_owner : "null"));
532 dmalock.dl_locked = 0;
533 return (-1);
534 }
535 dmalock.dl_owner = NULL;
536 dmalock.dl_locked = 0;
537 if (dmalock.dl_wanted) {
538 wakeup((caddr_t)&dmalock);
539 }
540 return (0);
541 }
542
543 /*----------------------------------------------------------------------*/
544 #if 0
545 /*
546 * small set of routines needed for mapping when doing pseudo-DMA,
547 * quasi-DMA or virtual-DMA (choose whatever name you like).
548 *
549 * Once I know how VS3100 is doing real DMA (I hope it does), this
550 * should be rewritten to present a general interface...
551 *
552 */
553
554 extern u_long uVAX_physmap;
555
556 u_long
557 vsdma_mapin(bp, len)
558 struct buf *bp;
559 int len;
560 {
561 pt_entry_t *pte; /* pointer to Page-Table-Entry */
562 struct pcb *pcb; /* pointer to Process-Controll-Block */
563 pt_entry_t *xpte;
564 caddr_t addr;
565 int pgoff; /* offset into 1st page */
566 int pgcnt; /* number of pages needed */
567 int pfnum;
568 int i;
569
570 trace(("mapin(bp=%x, bp->data=%x)\n", bp, bp->b_data));
571
572 addr = bp->b_data;
573 pgoff = (int)bp->b_data & PGOFSET; /* get starting offset */
574 pgcnt = btoc(bp->b_bcount + pgoff) + 1; /* one more than needed */
575
576 /*
577 * Get a pointer to the pte pointing out the first virtual address.
578 * Use different ways in kernel and user space.
579 */
580 if ((bp->b_flags & B_PHYS) == 0) {
581 pte = kvtopte(addr);
582 } else {
583 pcb = bp->b_proc->p_vmspace->vm_pmap.pm_pcb;
584 pte = uvtopte(addr, pcb);
585 }
586
587 /*
588 * When we are doing DMA to user space, be sure that all pages
589 * we want to transfer to are mapped. WHY DO WE NEED THIS???
590 * SHOULDN'T THEY ALWAYS BE MAPPED WHEN DOING THIS???
591 */
592 for (i=0; i<(pgcnt-1); i++) {
593 if ((pte + i)->pg_pfn == 0) {
594 int rv;
595 rv = vm_fault(&bp->b_proc->p_vmspace->vm_map,
596 (unsigned)addr + i * NBPG,
597 VM_PROT_READ|VM_PROT_WRITE, FALSE);
598 if (rv)
599 panic("vs-DMA to nonexistent page, %d", rv);
600 }
601 }
602
603 /*
604 * now insert new mappings for this memory area into kernel's
605 * mapping-table
606 */
607 xpte = kvtopte(uVAX_physmap);
608 while (--pgcnt > 0) {
609 pfnum = pte->pg_pfn;
610 if (pfnum == 0)
611 panic("vsbus: zero entry");
612 *(int *)xpte++ = *(int *)pte++;
613 }
614 *(int *)xpte = 0; /* mark last mapped page as invalid! */
615
616 debug(("uVAX: 0x%x\n", uVAX_physmap + pgoff));
617
618 return (uVAX_physmap + pgoff); /* ??? */
619 }
620 #endif
621 /*----------------------------------------------------------------------*/
622 /*
623 * Here follows some currently(?) unused stuff. Someday this should be removed
624 */
625
626 #if 0
627 /*
628 * Configure devices on VS2000/KA410 directly attached to vsbus
629 */
630 void
631 ka410_attach(parent, self, aux)
632 struct device *parent;
633 struct device *self;
634 void *aux;
635 {
636 struct confargs *ca;
637 int i;
638
639 for (i=0; i<KA410_MAXDEVS; i++) {
640 ca = &ka410_devs[i];
641 if (*ca->ca_name == '\0')
642 break;
643 config_found(self, (void*)ca, vsbus_print);
644 }
645 /*
646 * as long as there's no real DZ-driver, we used this dummy
647 */
648 vsbus_intr_register(&ka410_devs[0], ka410_consRecv_intr, NULL);
649 vsbus_intr_register(&ka410_devs[1], ka410_consXmit_intr, NULL);
650 }
651
652 #endif
653