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