isa.c revision 1.6 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 * @(#)isa.c 7.2 (Berkeley) 5/13/91
37 */
38 static char rcsid[] = "$Header: /tank/opengrok/rsync2/NetBSD/src/sys/dev/isa/isa.c,v 1.6 1993/04/15 07:57:57 deraadt Exp $";
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 "rlist.h"
59 #include "machine/segments.h"
60 #include "vm/vm.h"
61 #include "i386/isa/isa_device.h"
62 #include "i386/isa/isa.h"
63 #include "i386/isa/icu.h"
64 #include "i386/isa/ic/i8237.h"
65 #include "i386/isa/ic/i8042.h"
66
67 /*
68 ** Register definitions for DMA controller 1 (channels 0..3):
69 */
70 #define DMA1_CHN(c) (IO_DMA1 + 1*(2*(c))) /* addr reg for channel c */
71 #define DMA1_SMSK (IO_DMA1 + 1*10) /* single mask register */
72 #define DMA1_MODE (IO_DMA1 + 1*11) /* mode register */
73 #define DMA1_FFC (IO_DMA1 + 1*12) /* clear first/last FF */
74
75 /*
76 ** Register definitions for DMA controller 2 (channels 4..7):
77 */
78 #define DMA2_CHN(c) (IO_DMA1 + 2*(2*(c))) /* addr reg for channel c */
79 #define DMA2_SMSK (IO_DMA2 + 2*10) /* single mask register */
80 #define DMA2_MODE (IO_DMA2 + 2*11) /* mode register */
81 #define DMA2_FFC (IO_DMA2 + 2*12) /* clear first/last FF */
82
83 int config_isadev(struct isa_device *, u_short *);
84 void config_attach(struct isa_driver *, struct isa_device *);
85
86 /*
87 * Configure all ISA devices
88 */
89 isa_configure() {
90 struct isa_device *dvp;
91 struct isa_driver *dp;
92
93 splhigh();
94 INTREN(IRQ_SLAVE);
95 for (dvp = isa_devtab_tty; config_isadev(dvp,&ttymask); dvp++)
96 ;
97 for (dvp = isa_devtab_bio; config_isadev(dvp,&biomask); dvp++)
98 ;
99 for (dvp = isa_devtab_net; config_isadev(dvp,&netmask); dvp++)
100 ;
101 for (dvp = isa_devtab_null; config_isadev(dvp,0); dvp++)
102 ;
103 #include "sl.h"
104 #if NSL > 0
105 netmask |= ttymask;
106 ttymask |= netmask;
107 #endif
108 /* biomask |= ttymask ; can some tty devices use buffers? */
109 /* printf("biomask %x ttymask %x netmask %x\n", biomask, ttymask, netmask); */
110 splnone();
111 }
112
113 /*
114 * Configure an ISA device.
115 */
116 config_isadev(isdp, mp)
117 struct isa_device *isdp;
118 u_short *mp;
119 {
120 struct isa_driver *dp;
121
122 if (dp = isdp->id_driver) {
123 if (isdp->id_maddr) {
124 extern u_int atdevbase;
125
126 isdp->id_maddr -= 0xa0000;
127 isdp->id_maddr += atdevbase;
128 }
129 isdp->id_alive = (*dp->probe)(isdp);
130 if (isdp->id_alive) {
131 printf("%s%d", dp->name, isdp->id_unit);
132 printf(" at 0x%x", isdp->id_iobase);
133 if ((isdp->id_iobase + isdp->id_alive - 1) !=
134 isdp->id_iobase)
135 printf("-0x%x",
136 isdp->id_iobase + isdp->id_alive - 1);
137 printf(" ");
138 if(isdp->id_irq)
139 printf("irq %d ", ffs(isdp->id_irq)-1);
140 if (isdp->id_drq != -1)
141 printf("drq %d ", isdp->id_drq);
142 if (isdp->id_maddr != 0)
143 printf("maddr 0x%x ", kvtop(isdp->id_maddr));
144 if (isdp->id_msize != 0)
145 printf("msize %d ", isdp->id_msize);
146 if (isdp->id_flags != 0)
147 printf("flags 0x%x ", isdp->id_flags);
148 printf("on isa\n");
149
150 config_attach(dp, isdp);
151
152 if(isdp->id_irq) {
153 int intrno;
154
155 intrno = ffs(isdp->id_irq)-1;
156 INTREN(isdp->id_irq);
157 if(mp)
158 INTRMASK(*mp,isdp->id_irq);
159 setidt(ICU_OFFSET+intrno, isdp->id_intr,
160 SDT_SYS386IGT, SEL_KPL);
161 }
162 }
163 return (1);
164 } else return(0);
165 }
166
167 void
168 config_attach(struct isa_driver *dp, struct isa_device *isdp)
169 {
170 extern struct isa_device isa_subdev[];
171 struct isa_device *dvp;
172
173 if(isdp->id_masunit==-1) {
174 (*dp->attach)(isdp);
175 return;
176 }
177
178 if(isdp->id_masunit==0) {
179 for(dvp = isa_subdev; dvp->id_driver; dvp++) {
180 if (dvp->id_driver != dp)
181 continue;
182 if (dvp->id_masunit != isdp->id_masunit)
183 continue;
184 if (dvp->id_physid == -1)
185 continue;
186 (*dp->attach)(dvp);
187 }
188 for(dvp = isa_subdev; dvp->id_driver; dvp++) {
189 if (dvp->id_driver != dp)
190 continue;
191 if (dvp->id_masunit != isdp->id_masunit)
192 continue;
193 if (dvp->id_physid != -1)
194 continue;
195 (*dp->attach)(dvp);
196 }
197 return;
198 }
199 printf("id_masunit has weird value\n");
200 }
201
202
203 #define IDTVEC(name) __CONCAT(X,name)
204 /* default interrupt vector table entries */
205 extern IDTVEC(intr0), IDTVEC(intr1), IDTVEC(intr2), IDTVEC(intr3),
206 IDTVEC(intr4), IDTVEC(intr5), IDTVEC(intr6), IDTVEC(intr7),
207 IDTVEC(intr8), IDTVEC(intr9), IDTVEC(intr10), IDTVEC(intr11),
208 IDTVEC(intr12), IDTVEC(intr13), IDTVEC(intr14), IDTVEC(intr15);
209
210 static *defvec[16] = {
211 &IDTVEC(intr0), &IDTVEC(intr1), &IDTVEC(intr2), &IDTVEC(intr3),
212 &IDTVEC(intr4), &IDTVEC(intr5), &IDTVEC(intr6), &IDTVEC(intr7),
213 &IDTVEC(intr8), &IDTVEC(intr9), &IDTVEC(intr10), &IDTVEC(intr11),
214 &IDTVEC(intr12), &IDTVEC(intr13), &IDTVEC(intr14), &IDTVEC(intr15) };
215
216 /* out of range default interrupt vector gate entry */
217 extern IDTVEC(intrdefault);
218
219 /*
220 * Fill in default interrupt table (in case of spuruious interrupt
221 * during configuration of kernel, setup interrupt control unit
222 */
223 isa_defaultirq() {
224 int i;
225
226 /* icu vectors */
227 for (i = NRSVIDT ; i < NRSVIDT+ICU_LEN ; i++)
228 setidt(i, defvec[i], SDT_SYS386IGT, SEL_KPL);
229
230 /* out of range vectors */
231 for (i = NRSVIDT; i < NIDT; i++)
232 setidt(i, &IDTVEC(intrdefault), SDT_SYS386IGT, SEL_KPL);
233
234 /* clear npx intr latch */
235 outb(0xf1,0);
236
237 /* initialize 8259's */
238 outb(IO_ICU1, 0x11); /* reset; program device, four bytes */
239 outb(IO_ICU1+1, NRSVIDT); /* starting at this vector index */
240 outb(IO_ICU1+1, 1<<2); /* slave on line 2 */
241 outb(IO_ICU1+1, 1); /* 8086 mode */
242 outb(IO_ICU1+1, 0xff); /* leave interrupts masked */
243 outb(IO_ICU1, 2); /* default to ISR on read */
244
245 outb(IO_ICU2, 0x11); /* reset; program device, four bytes */
246 outb(IO_ICU2+1, NRSVIDT+8); /* staring at this vector index */
247 outb(IO_ICU2+1,2); /* my slave id is 2 */
248 outb(IO_ICU2+1,1); /* 8086 mode */
249 outb(IO_ICU2+1, 0xff); /* leave interrupts masked */
250 outb(IO_ICU2, 2); /* default to ISR on read */
251 }
252
253 /* region of physical memory known to be contiguous */
254 vm_offset_t isaphysmem;
255 static caddr_t dma_bounce[8]; /* XXX */
256 static char bounced[8]; /* XXX */
257 #define MAXDMASZ 512 /* XXX */
258
259 /* high byte of address is stored in this port for i-th dma channel */
260 static short dmapageport[8] =
261 { 0x87, 0x83, 0x81, 0x82, 0x8f, 0x8b, 0x89, 0x8a };
262
263 /*
264 * isa_dmacascade(): program 8237 DMA controller channel to accept
265 * external dma control by a board.
266 */
267 void isa_dmacascade(unsigned chan)
268 {
269 if (chan > 7)
270 panic("isa_dmacascade: impossible request");
271
272 /* set dma channel mode, and set dma channel mode */
273 if ((chan & 4) == 0) {
274 outb(DMA1_MODE, DMA37MD_CASCADE | chan);
275 outb(DMA1_SMSK, chan);
276 } else {
277 outb(DMA2_MODE, DMA37MD_CASCADE | (chan & 3));
278 outb(DMA2_SMSK, chan & 3);
279 }
280 }
281
282 /*
283 * isa_dmastart(): program 8237 DMA controller channel, avoid page alignment
284 * problems by using a bounce buffer.
285 */
286 void isa_dmastart(int flags, caddr_t addr, unsigned nbytes, unsigned chan)
287 { vm_offset_t phys;
288 int waport;
289 caddr_t newaddr;
290
291 if ( chan > 7
292 || (chan < 4 && nbytes > (1<<16))
293 || (chan >= 4 && (nbytes > (1<<17) || (u_int)addr & 1)))
294 panic("isa_dmastart: impossible request");
295
296 if (isa_dmarangecheck(addr, nbytes, chan)) {
297 if (dma_bounce[chan] == 0)
298 dma_bounce[chan] =
299 /*(caddr_t)malloc(MAXDMASZ, M_TEMP, M_WAITOK);*/
300 (caddr_t) isaphysmem + NBPG*chan;
301 bounced[chan] = 1;
302 newaddr = dma_bounce[chan];
303 *(int *) newaddr = 0; /* XXX */
304
305 /* copy bounce buffer on write */
306 if (!(flags & B_READ))
307 bcopy(addr, newaddr, nbytes);
308 addr = newaddr;
309 }
310
311 /* translate to physical */
312 phys = pmap_extract(pmap_kernel(), (vm_offset_t)addr);
313
314 if ((chan & 4) == 0) {
315 /*
316 * Program one of DMA channels 0..3. These are
317 * byte mode channels.
318 */
319 /* set dma channel mode, and reset address ff */
320 if (flags & B_READ)
321 outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|chan);
322 else
323 outb(DMA1_MODE, DMA37MD_SINGLE|DMA37MD_READ|chan);
324 outb(DMA1_FFC, 0);
325
326 /* send start address */
327 waport = DMA1_CHN(chan);
328 outb(waport, phys);
329 outb(waport, phys>>8);
330 outb(dmapageport[chan], phys>>16);
331
332 /* send count */
333 outb(waport + 1, --nbytes);
334 outb(waport + 1, nbytes>>8);
335
336 /* unmask channel */
337 outb(DMA1_SMSK, chan);
338 } else {
339 /*
340 * Program one of DMA channels 4..7. These are
341 * word mode channels.
342 */
343 /* set dma channel mode, and reset address ff */
344 if (flags & B_READ)
345 outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_WRITE|(chan&3));
346 else
347 outb(DMA2_MODE, DMA37MD_SINGLE|DMA37MD_READ|(chan&3));
348 outb(DMA2_FFC, 0);
349
350 /* send start address */
351 waport = DMA2_CHN(chan - 4);
352 outb(waport, phys>>1);
353 outb(waport, phys>>9);
354 outb(dmapageport[chan], phys>>16);
355
356 /* send count */
357 nbytes >>= 1;
358 outb(waport + 2, --nbytes);
359 outb(waport + 2, nbytes>>8);
360
361 /* unmask channel */
362 outb(DMA2_SMSK, chan & 3);
363 }
364 }
365
366 void isa_dmadone(int flags, caddr_t addr, int nbytes, int chan)
367 {
368
369 /* copy bounce buffer on read */
370 /*if ((flags & (B_PHYS|B_READ)) == (B_PHYS|B_READ))*/
371 if (bounced[chan]) {
372 bcopy(dma_bounce[chan], addr, nbytes);
373 bounced[chan] = 0;
374 }
375 }
376
377 /*
378 * Check for problems with the address range of a DMA transfer
379 * (non-contiguous physical pages, outside of bus address space,
380 * crossing DMA page boundaries).
381 * Return true if special handling needed.
382 */
383
384 isa_dmarangecheck(caddr_t va, unsigned length, unsigned chan) {
385 vm_offset_t phys, priorpage = 0, endva;
386 u_int dma_pgmsk = (chan & 4) ? ~(128*1024-1) : ~(64*1024-1);
387
388 endva = (vm_offset_t)round_page(va + length);
389 for (; va < (caddr_t) endva ; va += NBPG) {
390 phys = trunc_page(pmap_extract(pmap_kernel(), (vm_offset_t)va));
391 #define ISARAM_END RAM_END
392 if (phys == 0)
393 panic("isa_dmacheck: no physical page present");
394 if (phys > ISARAM_END)
395 return (1);
396 if (priorpage) {
397 if (priorpage + NBPG != phys)
398 return (1);
399 /* check if crossing a DMA page boundary */
400 if (((u_int)priorpage ^ (u_int)phys) & dma_pgmsk)
401 return (1);
402 }
403 priorpage = phys;
404 }
405 return (0);
406 }
407
408 /* head of queue waiting for physmem to become available */
409 struct buf isa_physmemq;
410
411 /* blocked waiting for resource to become free for exclusive use */
412 static isaphysmemflag;
413 /* if waited for and call requested when free (B_CALL) */
414 static void (*isaphysmemunblock)(); /* needs to be a list */
415
416 /*
417 * Allocate contiguous physical memory for transfer, returning
418 * a *virtual* address to region. May block waiting for resource.
419 * (assumed to be called at splbio())
420 */
421 caddr_t
422 isa_allocphysmem(caddr_t va, unsigned length, void (*func)()) {
423
424 isaphysmemunblock = func;
425 while (isaphysmemflag & B_BUSY) {
426 isaphysmemflag |= B_WANTED;
427 sleep(&isaphysmemflag, PRIBIO);
428 }
429 isaphysmemflag |= B_BUSY;
430
431 return((caddr_t)isaphysmem);
432 }
433
434 /*
435 * Free contiguous physical memory used for transfer.
436 * (assumed to be called at splbio())
437 */
438 void
439 isa_freephysmem(caddr_t va, unsigned length) {
440
441 isaphysmemflag &= ~B_BUSY;
442 if (isaphysmemflag & B_WANTED) {
443 isaphysmemflag &= B_WANTED;
444 wakeup(&isaphysmemflag);
445 if (isaphysmemunblock)
446 (*isaphysmemunblock)();
447 }
448 }
449
450 /*
451 * Handle a NMI, possibly a machine check.
452 * return true to panic system, false to ignore.
453 */
454 isa_nmi(cd) {
455
456 log(LOG_CRIT, "\nNMI port 61 %x, port 70 %x\n", inb(0x61), inb(0x70));
457 return(0);
458 }
459
460 /*
461 * Caught a stray interrupt, notify
462 */
463 isa_strayintr(d) {
464
465 /* DON'T BOTHER FOR NOW! */
466 /* for some reason, we get bursts of intr #7, even if not enabled! */
467 /*
468 * Well the reason you got bursts of intr #7 is because someone
469 * raised an interrupt line and dropped it before the 8259 could
470 * prioritize it. This is documented in the intel data book. This
471 * means you have BAD hardware! I have changed this so that only
472 * the first 10 get logged, then it quits logging them, and puts
473 * out a special message. rgrimes 3/25/1993
474 */
475 extern u_long isa_stray_intrcnt;
476
477 isa_stray_intrcnt++;
478 if (isa_stray_intrcnt <= 10)
479 log(LOG_ERR,"ISA strayintr %x\n", d);
480 if (isa_stray_intrcnt == 10)
481 log(LOG_CRIT,"Too many ISA strayintr not logging any more\n");
482 }
483
484 /*
485 * Wait "n" microseconds. Relies on timer 0 to have 1Mhz clock, regardless
486 * of processor board speed. Note: timer had better have been programmed
487 * before this is first used!
488 */
489 DELAY(n) {
490 int tick = getit(0,0) & 1;
491
492 while (n--) {
493 /* wait approximately 1 micro second */
494 while (tick == getit(0,0) & 1) ;
495
496 tick = getit(0,0) & 1;
497 }
498 }
499
500 getit(unit, timer) {
501 int port = (unit ? IO_TIMER2 : IO_TIMER1) + timer, val;
502
503 val = inb(port);
504 val = (inb(port) << 8) + val;
505 return (val);
506 }
507
508 extern int hz;
509
510 static beeping;
511 static
512 sysbeepstop(f)
513 {
514 /* disable counter 2 */
515 outb(0x61, inb(0x61) & 0xFC);
516 if (f)
517 timeout(sysbeepstop, 0, f);
518 else
519 beeping = 0;
520 }
521
522 void sysbeep(int pitch, int period)
523 {
524
525 outb(0x61, inb(0x61) | 3); /* enable counter 2 */
526 outb(0x43, 0xb6); /* set command for counter 2, 2 byte write */
527
528 outb(0x42, pitch);
529 outb(0x42, (pitch>>8));
530
531 if (!beeping) {
532 beeping = period;
533 timeout(sysbeepstop, period/2, period);
534 }
535 }
536
537 /*
538 * Pass command to keyboard controller (8042)
539 */
540 unsigned kbc_8042cmd(val) {
541
542 while (inb(KBSTATP)&KBS_IBF);
543 if (val) outb(KBCMDP, val);
544 while (inb(KBSTATP)&KBS_IBF);
545 return (inb(KBDATAP));
546 }
547