isa.c revision 1.28.2.15 1 /*-
2 * Copyright (c) 1993 Charles Hannum.
3 * Copyright (c) 1991 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * William Jolitz.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * from: @(#)isa.c 7.2 (Berkeley) 5/13/91
38 * $Id: isa.c,v 1.28.2.15 1993/10/17 05:34:29 mycroft Exp $
39 */
40
41 /*
42 * code to manage AT bus
43 */
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/conf.h>
48 #include <sys/file.h>
49 #include <sys/syslog.h>
50 #include <sys/device.h>
51 #include <vm/vm.h>
52
53 #include <machine/cpu.h>
54 #include <machine/pio.h>
55
56 #include <i386/isa/isa.h>
57 #include <i386/isa/isavar.h>
58 #include <i386/isa/icu.h>
59 #include <i386/isa/timerreg.h>
60 #include <i386/isa/spkr_reg.h>
61
62 isa_type isa_bustype; /* type of bus */
63
64 static int isaprobe __P((struct device *, struct cfdata *, void *));
65 static void isaattach __P((struct device *, struct device *, void *));
66 static int isasubmatch __P((struct device *, struct cfdata *, void *));
67
68 struct cfdriver isacd =
69 { NULL, "isa", isaprobe, isaattach, DV_DULL, sizeof(struct isa_softc) };
70
71 static void isa_defaultirq __P((void));
72 static int isaprint __P((void *, char *));
73 static void isa_flushintrs __P((void));
74 static void isa_intrmaskwickedness __P((void));
75
76 /*
77 * We think there might be an ISA bus here. Check it out.
78 */
79 static int
80 isaprobe(parent, cf, aux)
81 struct device *parent;
82 struct cfdata *cf;
83 void *aux;
84 {
85
86 /* XXX should do a real probe */
87 isa_bustype = BUS_ISA;
88 return 1;
89 }
90
91 /*
92 * Probe succeeded, someone called config_attach. Now we have to
93 * probe the ISA bus itself in our turn.
94 */
95 static void
96 isaattach(parent, self, aux)
97 struct device *parent, *self;
98 void *aux;
99 {
100
101 /* XXX should print ISA/EISA & bus clock frequency and anything else
102 we can figure out? */
103 printf("\n");
104
105 isa_defaultirq();
106 disable_intr();
107 intr_enable(IRQ_SLAVE);
108
109 /* Iterate ``isasubmatch'' over all devices configured here. */
110 (void)config_search(isasubmatch, self, (void *)NULL);
111
112 isa_intrmaskwickedness();
113
114 isa_flushintrs();
115 enable_intr();
116 }
117
118 /*
119 * isaattach (above) iterates this function over all the sub-devices that
120 * were configured at the ``isa'' device. Our job is to provide defaults
121 * and do some internal/external representation conversion (some of which
122 * is scheduled to get cleaned up later), then call the device's probe
123 * function. If this says the device is there, we try to reserve ISA
124 * bus memory and ports for the device, and attach it.
125 *
126 * Note that we always return 0, but our ultimate caller (isaattach)
127 * does not care that we never `match' anything. (In fact, our return
128 * value is entirely irrelevant; this function is run entirely for its
129 * side effects.)
130 */
131 static int
132 isasubmatch(isa, cf, aux)
133 struct device *isa;
134 struct cfdata *cf;
135 void *aux;
136 {
137 struct isa_attach_args ia;
138
139 #ifdef DIAGNOSTIC
140 if (cf->cf_driver->cd_match == NULL) {
141 /* we really ought to add printf formats to panic(). */
142 printf("isasubmatch: no match function for `%s' device\n",
143 cf->cf_driver->cd_name);
144 panic("isasubmatch: no match function\n");
145 }
146 #endif
147
148 /* Init the info needed in the device probe and attach routines. */
149 ia.ia_iobase = cf->cf_iobase;
150 ia.ia_iosize = cf->cf_iosize;
151 if (cf->cf_irq == -1)
152 ia.ia_irq = IRQUNK;
153 else
154 ia.ia_irq = 1 << cf->cf_irq;
155 ia.ia_drq = cf->cf_drq;
156 ia.ia_maddr = (caddr_t)cf->cf_maddr;
157 ia.ia_msize = cf->cf_msize;
158
159 /* If driver says it's not there, believe it. */
160 if (!cf->cf_driver->cd_match(isa, cf, &ia))
161 return 0;
162
163 /* Check for port or shared memory conflicts. */
164 if (ia.ia_iosize && !isa_portcheck(ia.ia_iobase, ia.ia_iosize))
165 return 0;
166 if (ia.ia_msize && !isa_memcheck(ia.ia_maddr, ia.ia_msize))
167 return 0;
168
169 /* Reserve I/O ports and/or shared memory. */
170 if (ia.ia_iosize)
171 isa_portalloc(ia.ia_iobase, ia.ia_iosize);
172 if (ia.ia_msize)
173 isa_memalloc(ia.ia_maddr, ia.ia_msize);
174
175 /* Configure device. */
176 config_attach(isa, cf, &ia, isaprint);
177
178 return 0;
179 }
180
181 /*
182 * XXX It's not clear to me why this is useful.
183 */
184 void
185 isa_establish(id, dv)
186 struct isadev *id;
187 struct device *dv;
188 {
189 struct isa_softc *idv = (struct isa_softc *)dv->dv_parent;
190 id->id_dev = dv;
191 id->id_bchain = idv->sc_isadev;
192 idv->sc_isadev = id;
193 }
194
195 /*
196 * Called indirectly via config_attach (see above). Config has already
197 * printed, e.g., "wdc0 at isa0". We can ignore our ``isaname'' arg
198 * (it is always NULL, by definition) and just extend the line with
199 * the standard info (port(s), irq, drq, and iomem).
200 */
201 static int
202 isaprint(aux, isaname)
203 void *aux;
204 char *isaname;
205 {
206 struct isa_attach_args *ia = aux;
207
208 if (ia->ia_iosize)
209 printf(" port 0x%x", ia->ia_iobase);
210 if (ia->ia_iosize > 1)
211 printf("-0x%x", ia->ia_iobase + ia->ia_iosize - 1);
212 if (ia->ia_msize)
213 printf(" iomem 0x%x", ia->ia_maddr);
214 if (ia->ia_msize > 1)
215 printf("-0x%x", ia->ia_maddr + ia->ia_msize - 1);
216 #ifdef DIAGNOSTIC
217 if (ia->ia_irq == IRQUNK)
218 printf(" THIS IS A BUG ->");
219 #endif
220 if (ia->ia_irq != IRQNONE)
221 printf(" irq %d", ffs(ia->ia_irq) - 1);
222 if (ia->ia_drq != DRQUNK)
223 printf(" drq %d", ia->ia_drq);
224 /* XXXX print flags */
225 return QUIET; /* actually, our return value is irrelevant. */
226 }
227
228 /*
229 int isa_portcheck __P((u_int, u_int));
230 int isa_memcheck __P((u_int, u_int));
231 void isa_portalloc __P((u_int, u_int));
232 void isa_memalloc __P((u_int, u_int));
233 */
234
235 int intrmask[NIRQ];
236 struct intrhand *intrhand[NIRQ];
237
238 /*
239 * Register an interrupt handler.
240 */
241 void
242 intr_establish(intr, handler, class)
243 int intr;
244 struct intrhand *handler;
245 enum devclass class;
246 {
247 int irqnum = ffs(intr) - 1;
248
249 #ifdef DIAGNOSTIC
250 if (intr == IRQUNK || intr == IRQNONE ||
251 (intr &~ (1 << irqnum)) != IRQNONE)
252 panic("intr_establish: weird irq");
253 #endif
254
255 switch (class) {
256 case DV_DULL:
257 break;
258 case DV_DISK:
259 case DV_TAPE:
260 biomask |= intr;
261 break;
262 case DV_IFNET:
263 netmask |= intr;
264 break;
265 case DV_TTY:
266 ttymask |= intr;
267 break;
268 case DV_CPU:
269 default:
270 panic("intrhand: weird devclass");
271 }
272
273 handler->ih_count = 0;
274 handler->ih_next = intrhand[irqnum];
275 intrhand[irqnum] = handler;
276
277 intr_enable(intr);
278 }
279
280 /*
281 * Set up the masks for each interrupt handler based on the information
282 * recorded by intr_establish().
283 */
284 static void
285 isa_intrmaskwickedness()
286 {
287 int irq, mask;
288
289 for (irq = 0; irq < NIRQ; irq++) {
290 mask = (1 << irq) | astmask;
291 if (biomask & (1 << irq))
292 mask |= biomask;
293 if (ttymask & (1 << irq))
294 mask |= ttymask;
295 if (netmask & (1 << irq))
296 mask |= netmask;
297 intrmask[irq] = mask;
298 }
299
300 impmask = netmask | ttymask;
301 printf("biomask %x ttymask %x netmask %x impmask %x astmask %s\n",
302 biomask, ttymask, netmask, impmask, astmask);
303 }
304
305 #define IDTVEC(name) __CONCAT(X,name)
306 /* default interrupt vector table entries */
307 extern *IDTVEC(intr)[NIRQ];
308 /* out of range default interrupt vector gate entry */
309 extern IDTVEC(stray);
310
311 /*
312 * Fill in default interrupt table, and mask all interrupts.
313 */
314 static void
315 isa_defaultirq()
316 {
317 int i;
318
319 /* icu vectors */
320 for (i = ICU_OFFSET; i < ICU_OFFSET + ICU_LEN ; i++)
321 setidt(i, IDTVEC(intr)[i], SDT_SYS386IGT, SEL_KPL);
322
323 /* out of range vectors */
324 for (; i < NIDT; i++)
325 setidt(i, &IDTVEC(stray), SDT_SYS386IGT, SEL_KPL);
326
327 /* initialize 8259's */
328 outb(IO_ICU1, 0x11); /* reset; program device, four bytes */
329 outb(IO_ICU1+1, ICU_OFFSET); /* starting at this vector index */
330 outb(IO_ICU1+1, IRQ_SLAVE);
331 #ifdef AUTO_EOI_1
332 outb(IO_ICU1+1, 2 | 1); /* auto EOI, 8086 mode */
333 #else
334 outb(IO_ICU1+1, 1); /* 8086 mode */
335 #endif
336 outb(IO_ICU1+1, 0xff); /* leave interrupts masked */
337 outb(IO_ICU1, 0x0a); /* default to IRR on read */
338 #ifdef REORDER_IRQ
339 outb(IO_ICU1, 0xc0 | (3 - 1)); /* pri order 3-7, 0-2 (com2 first) */
340 #endif
341
342 outb(IO_ICU2, 0x11); /* reset; program device, four bytes */
343 outb(IO_ICU2+1, ICU_OFFSET+8); /* staring at this vector index */
344 outb(IO_ICU2+1, ffs(IRQ_SLAVE)-1);
345 #ifdef AUTO_EOI_2
346 outb(IO_ICU2+1, 2 | 1); /* auto EOI, 8086 mode */
347 #else
348 outb(IO_ICU2+1, 1); /* 8086 mode */
349 #endif
350 outb(IO_ICU2+1, 0xff); /* leave interrupts masked */
351 outb(IO_ICU2, 0x0a); /* default to IRR on read */
352 }
353
354 /*
355 * Flush any pending interrupts.
356 */
357 static void
358 isa_flushintrs()
359 {
360 register int i;
361
362 /* clear any pending interrupts */
363 for (i = 0; i < 8; i++) {
364 outb(IO_ICU1, ICU_EOI);
365 outb(IO_ICU2, ICU_EOI);
366 }
367 }
368
369 /*
370 * Determine what IRQ a device is using by trying to force it to generate an
371 * interrupt and seeing which IRQ line goes high. It is not safe to call
372 * this function after autoconfig.
373 */
374 u_short
375 isa_discoverintr(force, aux)
376 void (*force)();
377 {
378 int time = TIMER_FREQ * 1; /* wait up to 1 second */
379 u_int last, now;
380 u_short iobase = IO_TIMER1;
381
382 isa_flushintrs();
383 /* attempt to force interrupt */
384 force(aux);
385 /* set timer 2 to a known state */
386 outb(iobase + TIMER_MODE, TIMER_SEL2|TIMER_RATEGEN|TIMER_16BIT);
387 outb(iobase + TIMER_CNTR2, 0xff);
388 outb(iobase + TIMER_CNTR2, 0xff);
389 last = 0xffff;
390 while (time > 0) {
391 register u_char irr, lo, hi;
392 irr = inb(IO_ICU1) & ~IRQ_SLAVE;
393 if (irr)
394 return 1 << (ffs(irr) - 1);
395 irr = inb(IO_ICU2);
396 if (irr)
397 return 1 << (ffs(irr) + 7);
398 outb(iobase + TIMER_MODE, TIMER_SEL2|TIMER_LATCH);
399 lo = inb(iobase + TIMER_CNTR2);
400 hi = inb(iobase + TIMER_CNTR2);
401 now = (hi << 8) | lo;
402 if (now <= last)
403 time -= (last - now);
404 else
405 time -= 0x10000 - (now - last);
406 last = now;
407 }
408 return IRQNONE;
409 }
410
411 /*
412 * Handle a NMI, possibly a machine check.
413 * return true to panic system, false to ignore.
414 *
415 * This implementation is hist[oe]rical.
416 */
417 int
418 isa_nmi()
419 {
420
421 log(LOG_CRIT, "NMI port 61 %x, port 70 %x\n", inb(0x61), inb(0x70));
422 return 0;
423 }
424
425 /*
426 * Caught a stray interrupt, notify
427 */
428 void
429 isa_strayintr(d)
430 int d;
431 {
432 extern u_long intrcnt_stray;
433
434 /*
435 * Stray level 7 interrupts occur when someone raises an interrupt
436 * and then drops it before the CPU acknowledges it. This means
437 * either the device is screwed or something is cli'ing too long
438 * and it's timing out.
439 *
440 * -1 is passed by the generic handler for out of range exceptions,
441 * since we don't really want 208 little vectors. (It wouldn't be
442 * all that much code, but why bother?)
443 */
444 if (intrcnt_stray++ < 5)
445 if (d == -1)
446 log(LOG_ERR, "wild interrupt\n");
447 else
448 log(LOG_ERR, "stray interrupt %d\n", d);
449 if (intrcnt_stray == 5)
450 log(LOG_ERR, "too many stray interrupts; stopped logging\n");
451 }
452
453 static int beeping;
454
455 static void
456 sysbeepstop(int f)
457 {
458
459 /* disable counter 2 */
460 disable_intr();
461 outb(PITAUX_PORT, inb(PITAUX_PORT) & ~PIT_SPKR);
462 enable_intr();
463 if (f)
464 timeout((timeout_t)sysbeepstop, (caddr_t)0, f);
465 else
466 beeping = 0;
467 }
468
469 void
470 sysbeep(int pitch, int period)
471 {
472 static int last_pitch, last_period;
473
474 if (beeping) {
475 untimeout((timeout_t)sysbeepstop, (caddr_t)(last_period/2));
476 untimeout((timeout_t)sysbeepstop, (caddr_t)0);
477 }
478 if (!beeping || last_pitch != pitch) {
479 /*
480 * XXX - move timer stuff to clock.c.
481 */
482 disable_intr();
483 outb(TIMER_MODE, TIMER_SEL2|TIMER_16BIT|TIMER_SQWAVE);
484 outb(TIMER_CNTR2, TIMER_DIV(pitch));
485 outb(TIMER_CNTR2, TIMER_DIV(pitch)>>8);
486 outb(PITAUX_PORT, inb(PITAUX_PORT) | PIT_SPKR); /* enable counter 2 */
487 enable_intr();
488 }
489 last_pitch = pitch;
490 beeping = last_period = period;
491 timeout((timeout_t)sysbeepstop, (caddr_t)(period/2), period);
492 }
493