isa.c revision 1.28.2.1 1 /*-
2 * Copyright (c) 1991 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * William Jolitz.
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 the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * from: @(#)isa.c 7.2 (Berkeley) 5/13/91
37 * $Id: isa.c,v 1.28.2.1 1993/09/14 17:32:39 mycroft Exp $
38 */
39
40 /*
41 * code to manage AT bus
42 *
43 * 92/08/18 Frank P. MacLachlan (fpm (at) crash.cts.com):
44 * Fixed uninitialized variable problem and added code to deal
45 * with DMA page boundaries in isa_dmarangecheck(). Fixed word
46 * mode DMA count compution and reorganized DMA setup code in
47 * isa_dmastart()
48 */
49
50 #include "param.h"
51 #include "systm.h"
52 #include "conf.h"
53 #include "file.h"
54 #include "buf.h"
55 #include "uio.h"
56 #include "syslog.h"
57 #include "malloc.h"
58 #include "machine/segments.h"
59 #include "machine/cpufunc.h"
60 #include "sys/device.h"
61 #include "vm/vm.h"
62 #include "i386/isa/isa.h"
63 #include "i386/isa/isavar.h"
64 #include "i386/isa/icu.h"
65 #include "i386/isa/ic/i8237.h"
66 #include "i386/isa/ic/i8042.h"
67 #include "i386/isa/timerreg.h"
68 #include "i386/isa/spkr_reg.h"
69
70 /*
71 ** Register definitions for DMA controller 1 (channels 0..3):
72 */
73 #define DMA1_CHN(c) (IO_DMA1 + 1*(2*(c))) /* addr reg for channel c */
74 #define DMA1_SMSK (IO_DMA1 + 1*10) /* single mask register */
75 #define DMA1_MODE (IO_DMA1 + 1*11) /* mode register */
76 #define DMA1_FFC (IO_DMA1 + 1*12) /* clear first/last FF */
77
78 /*
79 ** Register definitions for DMA controller 2 (channels 4..7):
80 */
81 #define DMA2_CHN(c) (IO_DMA1 + 2*(2*(c))) /* addr reg for channel c */
82 #define DMA2_SMSK (IO_DMA2 + 2*10) /* single mask register */
83 #define DMA2_MODE (IO_DMA2 + 2*11) /* mode register */
84 #define DMA2_FFC (IO_DMA2 + 2*12) /* clear first/last FF */
85
86 isa_type isa_bustype; /* type of bus */
87
88 static int isaprobe __P((struct device *, struct cfdata *, void *));
89 static void icuattach __P((struct device *, struct device *, void *));
90
91 struct cfdriver isacd =
92 { NULL, "isa", isaprobe, isaattach, DV_DULL, sizeof(struct isa_softc) };
93
94 int
95 isaprobe(parent, cf, aux)
96 struct device *parent;
97 struct cfdata *cf;
98 void *aux;
99 {
100
101 /* XXX should do a real probe */
102 isa_bustype = BUS_ISA;
103 return 1;
104 }
105
106 static int
107 isasubmatch(parent, cf, aux)
108 struct device *parent;
109 struct cfdata *cf;
110 void *aux;
111 {
112 struct isa_attach_args *ia = aux;
113 int rv;
114
115 ia->ia_iobase = cf->cf_iobase;
116 ia->ia_iosize = cf->cf_iosize;
117 if (cf->cf_irq == -1)
118 ia->ia_irq = IRQUNK;
119 else
120 ia->ia_irq = 1 << cf->cf_irq;
121 ia->ia_drq = cf->cf_drq;
122 ia->ia_maddr = cf->cf_maddr;
123 ia->ia_msize = cf->cf_msize;
124
125 #ifdef DIAGNOSTIC
126 if (!cf->cf_driver->cd_match) {
127 printf("isasubmatch: no match function for `%s' device\n",
128 cf->cf_driver->cd_name);
129 panic("isasubmatch: no match function\n");
130 }
131 #endif
132
133 rv = (*cf->cf_driver->cd_match)(parent, cf, aux);
134
135 if (!rv)
136 return 0;
137
138 if (!isa_reserveports(ia->ia_iobase, ia->ia_iosize))
139 return 0;
140
141 if (!isa_reservemem(ia->ia_maddr, ia->ia_msize)) {
142 isa_unreserveports(ia->ia_iobase, ia->ia_iosize);
143 return 0;
144 }
145
146 return 1;
147 }
148
149 void
150 isaprint(aux, isaname)
151 void *aux;
152 char *isaname;
153 {
154 struct isa_attach_args *ia = aux;
155
156 if (ia->ia_iosize)
157 printf(" port 0x%x", ia->ia_iobase);
158 if (ia->ia_iosize > 1)
159 printf("-0x%x", ia->ia_iobase + ia->ia_iosize - 1);
160 if (ia->ia_irq != IRQUNK)
161 printf(" irq %d", ffs(ia->ia_irq) - 1);
162 if (ia->ia_drq != DRQUNK)
163 printf(" drq %d", ia->ia_drq);
164 if (ia->ia_msize)
165 printf(" iomem 0x%x", ia->ia_maddr);
166 if (ia->ia_msize > 1)
167 printf("-0x%x", ia->ia_maddr + ia->ia_msize - 1);
168 /* XXXX need to print flags */
169 }
170
171 void
172 isaattach(parent, self, aux)
173 struct device *parent, *self;
174 void *aux;
175 {
176
177 isa_defaultirq();
178
179 enable_intr();
180 splhigh();
181 intr_enable(IRQ_SLAVE);
182
183 for (;;) {
184 struct cfdata *child
185 struct isa_attach_args ia;
186 child = config_search(isasubmatch, self, &ia);
187 if (!child)
188 break;
189 config_attach(self, child, &ia, isaprint);
190 }
191
192 /* and the problem is... if netmask == 0, then the loopback
193 * code can do some really ugly things.
194 * workaround for this: if netmask == 0, set it to 0x8000, which
195 * is the value used by splsoftclock. this is nasty, but it
196 * should work until this interrupt system goes away. -- cgd
197 */
198 if (netmask == 0)
199 netmask = 0x8000; /* same as for softclock. XXXX */
200
201 printf("biomask %x ttymask %x netmask %x impmask %x\n",
202 biomask, ttymask, netmask, impmask);
203 splnone();
204 }
205
206
207 #define IDTVEC(name) __CONCAT(X,name)
208 /* default interrupt vector table entries */
209 extern IDTVEC(intr0), IDTVEC(intr1), IDTVEC(intr2), IDTVEC(intr3),
210 IDTVEC(intr4), IDTVEC(intr5), IDTVEC(intr6), IDTVEC(intr7),
211 IDTVEC(intr8), IDTVEC(intr9), IDTVEC(intr10), IDTVEC(intr11),
212 IDTVEC(intr12), IDTVEC(intr13), IDTVEC(intr14), IDTVEC(intr15);
213
214 static *defvec[16] = {
215 &IDTVEC(intr0), &IDTVEC(intr1), &IDTVEC(intr2), &IDTVEC(intr3),
216 &IDTVEC(intr4), &IDTVEC(intr5), &IDTVEC(intr6), &IDTVEC(intr7),
217 &IDTVEC(intr8), &IDTVEC(intr9), &IDTVEC(intr10), &IDTVEC(intr11),
218 &IDTVEC(intr12), &IDTVEC(intr13), &IDTVEC(intr14), &IDTVEC(intr15) };
219
220 /* out of range default interrupt vector gate entry */
221 extern IDTVEC(intrdefault);
222
223 /*
224 * Fill in default interrupt table, and mask all interrupts.
225 */
226 void
227 isa_defaultirq()
228 {
229 int i;
230
231 /* icu vectors */
232 for (i = ICU_OFFSET; i < ICU_OFFSET + ICU_LEN ; i++)
233 setidt(i, defvec[i], SDT_SYS386IGT, SEL_KPL);
234
235 /* out of range vectors */
236 for (; i < NIDT; i++)
237 setidt(i, &IDTVEC(intrdefault), SDT_SYS386IGT, SEL_KPL);
238
239 /* initialize 8259's */
240 outb(IO_ICU1, 0x11); /* reset; program device, four bytes */
241 outb(IO_ICU1+1, ICU_OFFSET); /* starting at this vector index */
242 outb(IO_ICU1+1, IRQ_SLAVE);
243 #ifdef AUTO_EOI_1
244 outb(IO_ICU1+1, 2 | 1); /* auto EOI, 8086 mode */
245 #else
246 outb(IO_ICU1+1, 1); /* 8086 mode */
247 #endif
248 outb(IO_ICU1+1, 0xff); /* leave interrupts masked */
249 outb(IO_ICU1, 0x0a); /* default to IRR on read */
250 #ifdef REORDER_IRQ
251 outb(IO_ICU1, 0xc0 | (3 - 1)); /* pri order 3-7, 0-2 (com2 first) */
252 #endif
253
254 outb(IO_ICU2, 0x11); /* reset; program device, four bytes */
255 outb(IO_ICU2+1, ICU_OFFSET+8); /* staring at this vector index */
256 outb(IO_ICU2+1, ffs(IRQ_SLAVE)-1);
257 #ifdef AUTO_EOI_2
258 outb(IO_ICU2+1, 2 | 1); /* auto EOI, 8086 mode */
259 #else
260 outb(IO_ICU2+1, 1); /* 8086 mode */
261 #endif
262 outb(IO_ICU2+1, 0xff); /* leave interrupts masked */
263 outb(IO_ICU2, 0x0a); /* default to IRR on read */
264 }
265
266
267 /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
268
269 int
270 config_isadev(isdp, mp)
271 struct isa_device *isdp;
272 u_int *mp;
273 {
274 if (isdp->id_irq) {
275 int intrno;
276
277 intrno = ffs(isdp->id_irq)-1;
278 setidt(ICU_OFFSET+intrno, isdp->id_intr,
279 SDT_SYS386IGT, SEL_KPL);
280 if(mp)
281 INTRMASK(*mp,isdp->id_irq);
282 INTREN(isdp->id_irq);
283 }
284 }
285
286
287 /* region of physical memory known to be contiguous */
288 vm_offset_t isaphysmem;
289 static caddr_t bouncebuf[8]; /* XXX */
290 static caddr_t bounced[8]; /* XXX */
291 static vm_size_t bouncesize[8]; /* XXX */
292 #define MAXDMASZ 512 /* XXX */
293
294 /* high byte of address is stored in this port for i-th dma channel */
295 static short dmapageport[8] =
296 { 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
297
298 /*
299 * isa_dmacascade(): program 8237 DMA controller channel to accept
300 * external dma control by a board.
301 */
302 void
303 at_dma_cascade(chan)
304 unsigned chan;
305 {
306 #ifdef DEBUG
307 if (chan > 7)
308 panic("at_dma_cascade: impossible request");
309 #endif
310
311 /* set dma channel mode, and set dma channel mode */
312 if ((chan & 4) == 0) {
313 outb(DMA1_MODE, DMA37MD_CASCADE | chan);
314 outb(DMA1_SMSK, chan);
315 } else {
316 outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
317 outb(DMA2_SMSK, chan & 3);
318 }
319 }
320
321 /*
322 * at_dma(): program 8237 DMA controller channel, avoid page alignment
323 * problems by using a bounce buffer.
324 */
325 void
326 at_dma(flags, addr, nbytes, chan)
327 int read;
328 caddr_t addr;
329 vm_size_t nbytes;
330 unsigned chan;
331 {
332 vm_offset_t phys;
333 int waport;
334 caddr_t newaddr;
335
336 ppp if ( chan > 7
337 || (chan < 4 && nbytes > (1<<16))
338 || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1)))
339 panic("isa_dmastart: impossible request");
340
341 if (at_dma_rangecheck(addr, nbytes, chan)) {
342 panic("bounce buffers don't work yet\n");
343 /* XXX totally braindead; NBPG is not enough */
344 if (bouncebuf[chan] == 0)
345 bouncebuf[chan] =
346 /*(caddr_t)malloc(MAXDMASZ, M_TEMP, M_WAITOK);*/
347 (caddr_t) isaphysmem + NBPG*chan;
348 bouncesize[chan] = nbytes;
349 newaddr = bouncebuf[chan];
350 /* copy bounce buffer on write */
351 if (!read)
352 bcopy(addr, newaddr, nbytes);
353 else
354 bounced[chan] = addr;
355 addr = newaddr;
356 }
357
358 /* translate to physical */
359 phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
360
361 if ((chan & 4) == 0) {
362 /*
363 * Program one of DMA channels 0..3. These are
364 * byte mode channels.
365 */
366 /* set dma channel mode, and reset address ff */
367 if (read)
368 outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
369 else
370 outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
371 outb(DMA1_FFC, 0);
372
373 /* send start address */
374 waport = DMA1_CHN(chan);
375 outb(waport, phys);
376 outb(waport, phys>>8);
377 outb(dmapageport[chan], phys>>16);
378
379 /* send count */
380 outb(waport + 1, --nbytes);
381 outb(waport + 1, nbytes>>8);
382
383 /* unmask channel */
384 outb(DMA1_SMSK, chan);
385 } else {
386 /*
387 * Program one of DMA channels 4..7. These are
388 * word mode channels.
389 */
390 /* set dma channel mode, and reset address ff */
391 if (read)
392 outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
393 else
394 outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
395 outb(DMA2_FFC, 0);
396
397 /* send start address */
398 waport = DMA2_CHN(chan - 4);
399 outb(waport, phys>>1);
400 outb(waport, phys>>9);
401 outb(dmapageport[chan], phys>>16);
402
403 /* send count */
404 nbytes >>= 1;
405 outb(waport + 2, --nbytes);
406 outb(waport + 2, nbytes>>8);
407
408 /* unmask channel */
409 outb(DMA2_SMSK, chan & 3);
410 }
411 }
412
413 void
414 at_dma_terminate(int flags, caddr_t addr, int nbytes, int chan)
415 {
416
417 if (bounced[chan]) {
418 bcopy(bouncebuf[chan], bounced[chan], bouncesize[chan]);
419 bounced[chan] = 0;
420 }
421 }
422
423 /*
424 * Check for problems with the address range of a DMA transfer
425 * (non-contiguous physical pages, outside of bus address space,
426 * crossing DMA page boundaries).
427 * Return true if special handling needed.
428 */
429
430 int
431 at_dma_rangecheck(caddr_t va, unsigned length, unsigned chan) {
432 vm_offset_t phys, priorpage = 0, endva;
433 u_int dma_pgmsk = (chan & 4) ? ~(128*1024-1) : ~(64*1024-1);
434
435 endva = (vm_offset_t)round_page(va + length);
436 for (; va < (caddr_t) endva ; va += NBPG) {
437 phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va));
438 if (phys == 0)
439 panic("isa_dmacheck: no physical page present");
440 if (phys > physmem)
441 return (1);
442 if (priorpage) {
443 if (priorpage + NBPG != phys)
444 return (1);
445 /* check if crossing a DMA page boundary */
446 if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
447 return (1);
448 }
449 priorpage = phys;
450 }
451 return (0);
452 }
453
454 /* head of queue waiting for physmem to become available */
455 struct buf isa_physmemq;
456
457 /* blocked waiting for resource to become free for exclusive use */
458 static isaphysmemflag;
459 /* if waited for and call requested when free (B_CALL) */
460 static void (*isaphysmemunblock)(); /* XXX needs to be a list */
461
462 /*
463 * Allocate contiguous physical memory for transfer, returning
464 * a *virtual* address to region. May block waiting for resource.
465 * (assumed to be called at splbio())
466 */
467 caddr_t
468 isa_allocphysmem(caddr_t va, unsigned length, void (*func)()) {
469
470 isaphysmemunblock = func;
471 while (isaphysmemflag & B_BUSY) {
472 isaphysmemflag |= B_WANTED;
473 sleep((caddr_t)&isaphysmemflag, PRIBIO);
474 }
475 isaphysmemflag |= B_BUSY;
476
477 return((caddr_t)isaphysmem);
478 }
479
480 /*
481 * Free contiguous physical memory used for transfer.
482 * (assumed to be called at splbio())
483 */
484 void
485 isa_freephysmem(caddr_t va, unsigned length) {
486
487 isaphysmemflag &= ~B_BUSY;
488 if (isaphysmemflag & B_WANTED) {
489 isaphysmemflag &= B_WANTED;
490 wakeup((caddr_t)&isaphysmemflag);
491 if (isaphysmemunblock)
492 (*isaphysmemunblock)();
493 }
494 }
495
496 /*
497 * Handle a NMI, possibly a machine check.
498 * return true to panic system, false to ignore.
499 */
500 int
501 isa_nmi(cd) {
502
503 log(LOG_CRIT, "\nNMI port 61 %x, port 70 %x\n", inb(0x61), inb(0x70));
504 return(0);
505 }
506
507 /*
508 * Caught a stray interrupt, notify
509 */
510 void
511 isa_strayintr(d)
512 int d;
513 {
514
515 /*
516 * Stray level 7 interrupts occur when someone raises an interrupt
517 * and then drops it before the CPU acknowledges it. This means
518 * either the device is screwed or something is cli'ing too long.
519 */
520 extern u_long intrcnt_stray;
521
522 intrcnt_stray++;
523 if (intrcnt_stray <= 5)
524 log(LOG_ERR, "stray interrupt %d\n", d);
525 if (intrcnt_stray == 5)
526 log(LOG_CRIT,"too many stray interrupts; stopped logging\n");
527 }
528
529 static beeping;
530 static void
531 sysbeepstop(int f)
532 {
533 int s = splhigh();
534
535 /* disable counter 2 */
536 disable_intr();
537 outb(PITAUX_PORT, inb(PITAUX_PORT) & ~PIT_SPKR);
538 enable_intr();
539 if (f)
540 timeout((timeout_t)sysbeepstop, (caddr_t)0, f);
541 else
542 beeping = 0;
543
544 splx(s);
545 }
546
547 void
548 sysbeep(int pitch, int period)
549 {
550 int s = splhigh();
551 static int last_pitch, last_period;
552
553 if (beeping) {
554 untimeout((timeout_t)sysbeepstop, (caddr_t)(last_period/2));
555 untimeout((timeout_t)sysbeepstop, (caddr_t)0);
556 }
557 if (!beeping || last_pitch != pitch) {
558 /*
559 * XXX - move timer stuff to clock.c.
560 */
561 disable_intr();
562 outb(TIMER_MODE, TIMER_SEL2|TIMER_16BIT|TIMER_SQWAVE);
563 outb(TIMER_CNTR2, TIMER_DIV(pitch)%256);
564 outb(TIMER_CNTR2, TIMER_DIV(pitch)/256);
565 outb(PITAUX_PORT, inb(PITAUX_PORT) | PIT_SPKR); /* enable counter 2 */
566 enable_intr();
567 }
568 last_pitch = pitch;
569 beeping = last_period = period;
570 timeout((timeout_t)sysbeepstop, (caddr_t)(period/2), period);
571
572 splx(s);
573 }
574
575 /*
576 * Pass command to keyboard controller (8042)
577 */
578 unsigned
579 kbc_8042cmd(int val)
580 {
581 while (inb(KBSTATP)&KBS_IBF);
582 if (val) outb(KBCMDP, val);
583 while (inb(KBSTATP)&KBS_IBF);
584 return (inb(KBDATAP));
585 }
586
587 /*
588 * find an ISA device in a given isa_devtab_* table, given
589 * the table to search, the expected id_driver entry, and the unit number.
590 *
591 * this function is defined in isa_device.h, and this location is debatable;
592 * i put it there because it's useless w/o, and directly operates on
593 * the other stuff in that file.
594 *
595 */
596
597 struct isa_device *find_isadev(table, driverp, unit)
598 struct isa_device *table;
599 struct isa_driver *driverp;
600 int unit;
601 {
602 if (driverp == NULL) /* sanity check */
603 return NULL;
604
605 while ((table->id_driver != driverp) || (table->id_unit != unit)) {
606 if (table->id_driver == 0)
607 return NULL;
608
609 table++;
610 }
611
612 return table;
613 }
614
615 /*
616 * Return nonzero if a (masked) irq is pending for a given device.
617 */
618 int
619 isa_irq_pending(dvp)
620 struct isa_device *dvp;
621 {
622 unsigned id_irq;
623
624 id_irq = (unsigned short) dvp->id_irq; /* XXX silly type in struct */
625 if (id_irq & 0xff)
626 return (inb(IO_ICU1) & id_irq);
627 return (inb(IO_ICU2) & (id_irq >> 8));
628 }
629