Home | History | Annotate | Line # | Download | only in isa
isa.c revision 1.28.2.16
      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.16 1993/10/18 09:30:37 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, ih, class)
    243 	int intr;
    244 	struct intrhand *ih;
    245 	enum devclass class;
    246 {
    247 	int irqnum = ffs(intr) - 1;
    248 	register struct intrhand **p, *q;
    249 
    250 #ifdef DIAGNOSTIC
    251 	if (intr == IRQUNK || intr == IRQNONE ||
    252 	    (intr &~ (1 << irqnum)) != IRQNONE)
    253 		panic("intr_establish: weird irq");
    254 #endif
    255 
    256 	switch (class) {
    257 	    case DV_DULL:
    258 		break;
    259 	    case DV_DISK:
    260 	    case DV_TAPE:
    261 		biomask |= intr;
    262 		break;
    263 	    case DV_IFNET:
    264 		netmask |= intr;
    265 		break;
    266 	    case DV_TTY:
    267 		ttymask |= intr;
    268 		break;
    269 	    case DV_CPU:
    270 	    default:
    271 		panic("intrhand: weird devclass");
    272 	}
    273 
    274 	ih->ih_count = 0;
    275 	ih->ih_next = NULL;
    276 
    277 	/*
    278 	 * This is O(N^2), but we want to preserve the order, and N is
    279 	 * always small.
    280 	 */
    281 	for (p = &intrhand[intr]; (q = *p) != NULL; p = &q->ih_next)
    282 		;
    283 	*p = ih;
    284 
    285 	intr_enable(intr);
    286 }
    287 
    288 /*
    289  * Set up the masks for each interrupt handler based on the information
    290  * recorded by intr_establish().
    291  */
    292 static void
    293 isa_intrmaskwickedness()
    294 {
    295 	int irq, mask;
    296 
    297 	for (irq = 0; irq < NIRQ; irq++) {
    298 		mask = (1 << irq) | astmask;
    299 		if (biomask & (1 << irq))
    300 			mask |= biomask;
    301 		if (ttymask & (1 << irq))
    302 			mask |= ttymask;
    303 		if (netmask & (1 << irq))
    304 			mask |= netmask;
    305 		intrmask[irq] = mask;
    306 	}
    307 
    308 	impmask = netmask | ttymask;
    309 	printf("biomask %x ttymask %x netmask %x impmask %x astmask %s\n",
    310 	       biomask, ttymask, netmask, impmask, astmask);
    311 }
    312 
    313 #define	IDTVEC(name)	__CONCAT(X,name)
    314 /* default interrupt vector table entries */
    315 extern	*IDTVEC(intr)[NIRQ];
    316 /* out of range default interrupt vector gate entry */
    317 extern	IDTVEC(stray);
    318 
    319 /*
    320  * Fill in default interrupt table, and mask all interrupts.
    321  */
    322 static void
    323 isa_defaultirq()
    324 {
    325 	int i;
    326 
    327 	/* icu vectors */
    328 	for (i = ICU_OFFSET; i < ICU_OFFSET + ICU_LEN ; i++)
    329 		setidt(i, IDTVEC(intr)[i],  SDT_SYS386IGT, SEL_KPL);
    330 
    331 	/* out of range vectors */
    332 	for (; i < NIDT; i++)
    333 		setidt(i, &IDTVEC(stray), SDT_SYS386IGT, SEL_KPL);
    334 
    335 	/* initialize 8259's */
    336 	outb(IO_ICU1, 0x11);		/* reset; program device, four bytes */
    337 	outb(IO_ICU1+1, ICU_OFFSET);	/* starting at this vector index */
    338 	outb(IO_ICU1+1, IRQ_SLAVE);
    339 #ifdef AUTO_EOI_1
    340 	outb(IO_ICU1+1, 2 | 1);		/* auto EOI, 8086 mode */
    341 #else
    342 	outb(IO_ICU1+1, 1);		/* 8086 mode */
    343 #endif
    344 	outb(IO_ICU1+1, 0xff);		/* leave interrupts masked */
    345 	outb(IO_ICU1, 0x0a);		/* default to IRR on read */
    346 #ifdef REORDER_IRQ
    347 	outb(IO_ICU1, 0xc0 | (3 - 1));	/* pri order 3-7, 0-2 (com2 first) */
    348 #endif
    349 
    350 	outb(IO_ICU2, 0x11);		/* reset; program device, four bytes */
    351 	outb(IO_ICU2+1, ICU_OFFSET+8);	/* staring at this vector index */
    352 	outb(IO_ICU2+1, ffs(IRQ_SLAVE)-1);
    353 #ifdef AUTO_EOI_2
    354 	outb(IO_ICU2+1, 2 | 1);		/* auto EOI, 8086 mode */
    355 #else
    356 	outb(IO_ICU2+1, 1);		/* 8086 mode */
    357 #endif
    358 	outb(IO_ICU2+1, 0xff);		/* leave interrupts masked */
    359 	outb(IO_ICU2, 0x0a);		/* default to IRR on read */
    360 }
    361 
    362 /*
    363  * Flush any pending interrupts.
    364  */
    365 static void
    366 isa_flushintrs()
    367 {
    368 	register int i;
    369 
    370 	/* clear any pending interrupts */
    371 	for (i = 0; i < 8; i++) {
    372 		outb(IO_ICU1, ICU_EOI);
    373 		outb(IO_ICU2, ICU_EOI);
    374 	}
    375 }
    376 
    377 /*
    378  * Determine what IRQ a device is using by trying to force it to generate an
    379  * interrupt and seeing which IRQ line goes high.  It is not safe to call
    380  * this function after autoconfig.
    381  */
    382 u_short
    383 isa_discoverintr(force, aux)
    384 	void (*force)();
    385 {
    386 	int time = TIMER_FREQ * 1;	/* wait up to 1 second */
    387 	u_int last, now;
    388 	u_short iobase = IO_TIMER1;
    389 
    390 	isa_flushintrs();
    391 	/* attempt to force interrupt */
    392 	force(aux);
    393 	/* set timer 2 to a known state */
    394 	outb(iobase + TIMER_MODE, TIMER_SEL2|TIMER_RATEGEN|TIMER_16BIT);
    395 	outb(iobase + TIMER_CNTR2, 0xff);
    396 	outb(iobase + TIMER_CNTR2, 0xff);
    397 	last = 0xffff;
    398 	while (time > 0) {
    399 		register u_char irr, lo, hi;
    400 		irr = inb(IO_ICU1) & ~IRQ_SLAVE;
    401 		if (irr)
    402 			return 1 << (ffs(irr) - 1);
    403 		irr = inb(IO_ICU2);
    404 		if (irr)
    405 			return 1 << (ffs(irr) + 7);
    406 		outb(iobase + TIMER_MODE, TIMER_SEL2|TIMER_LATCH);
    407 		lo = inb(iobase + TIMER_CNTR2);
    408 		hi = inb(iobase + TIMER_CNTR2);
    409 		now = (hi << 8) | lo;
    410 		if (now <= last)
    411 			time -= (last - now);
    412 		else
    413 			time -= 0x10000 - (now - last);
    414 		last = now;
    415 	}
    416 	return IRQNONE;
    417 }
    418 
    419 /*
    420  * Handle a NMI, possibly a machine check.
    421  * return true to panic system, false to ignore.
    422  *
    423  * This implementation is hist[oe]rical.
    424  */
    425 int
    426 isa_nmi()
    427 {
    428 
    429 	log(LOG_CRIT, "NMI port 61 %x, port 70 %x\n", inb(0x61), inb(0x70));
    430 	return 0;
    431 }
    432 
    433 /*
    434  * Caught a stray interrupt, notify
    435  */
    436 void
    437 isa_strayintr(d)
    438 	int d;
    439 {
    440 	extern u_long intrcnt_stray;
    441 
    442 	/*
    443 	 * Stray level 7 interrupts occur when someone raises an interrupt
    444 	 * and then drops it before the CPU acknowledges it.  This means
    445 	 * either the device is screwed or something is cli'ing too long
    446 	 * and it's timing out.
    447 	 *
    448 	 * -1 is passed by the generic handler for out of range exceptions,
    449 	 * since we don't really want 208 little vectors.  (It wouldn't be
    450 	 * all that much code, but why bother?)
    451 	 */
    452 	if (intrcnt_stray++ < 5)
    453 		if (d == -1)
    454 			log(LOG_ERR, "wild interrupt\n");
    455 		else
    456 			log(LOG_ERR, "stray interrupt %d\n", d);
    457 	if (intrcnt_stray == 5)
    458 		log(LOG_ERR, "too many stray interrupts; stopped logging\n");
    459 }
    460 
    461 static int beeping;
    462 
    463 static void
    464 sysbeepstop(int f)
    465 {
    466 
    467 	/* disable counter 2 */
    468 	disable_intr();
    469 	outb(PITAUX_PORT, inb(PITAUX_PORT) & ~PIT_SPKR);
    470 	enable_intr();
    471 	if (f)
    472 		timeout((timeout_t)sysbeepstop, (caddr_t)0, f);
    473 	else
    474 		beeping = 0;
    475 }
    476 
    477 void
    478 sysbeep(int pitch, int period)
    479 {
    480 	static int last_pitch, last_period;
    481 
    482 	if (beeping) {
    483 		untimeout((timeout_t)sysbeepstop, (caddr_t)(last_period/2));
    484 		untimeout((timeout_t)sysbeepstop, (caddr_t)0);
    485 	}
    486 	if (!beeping || last_pitch != pitch) {
    487 		/*
    488 		 * XXX - move timer stuff to clock.c.
    489 		 */
    490 		disable_intr();
    491 		outb(TIMER_MODE, TIMER_SEL2|TIMER_16BIT|TIMER_SQWAVE);
    492 		outb(TIMER_CNTR2, TIMER_DIV(pitch));
    493 		outb(TIMER_CNTR2, TIMER_DIV(pitch)>>8);
    494 		outb(PITAUX_PORT, inb(PITAUX_PORT) | PIT_SPKR);	/* enable counter 2 */
    495 		enable_intr();
    496 	}
    497 	last_pitch = pitch;
    498 	beeping = last_period = period;
    499 	timeout((timeout_t)sysbeepstop, (caddr_t)(period/2), period);
    500 }
    501