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