Home | History | Annotate | Line # | Download | only in isa
isa.c revision 1.28.2.9
      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.9 1993/10/15 03:16:19 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 
     73 /*
     74  * We think there might be an ISA bus here.  Check it out.
     75  */
     76 static int
     77 isaprobe(parent, cf, aux)
     78 	struct device *parent;
     79 	struct cfdata *cf;
     80 	void *aux;
     81 {
     82 
     83 	/* XXX should do a real probe */
     84 	isa_bustype = BUS_ISA;
     85 	return 1;
     86 }
     87 
     88 /*
     89  * Probe succeeded, someone called config_attach.  Now we have to
     90  * probe the ISA bus itself in our turn.
     91  */
     92 static void
     93 isaattach(parent, self, aux)
     94 	struct device *parent, *self;
     95 	void *aux;
     96 {
     97 
     98 	/* XXX should print ISA/EISA & bus clock frequency and anything else
     99 	   we can figure out? */
    100 	printf("\n");
    101 
    102 	isa_defaultirq();
    103 	disable_intr();
    104 	intr_enable(IRQ_SLAVE);
    105 
    106 	/* Iterate ``isasubmatch'' over all devices configured here. */
    107 	(void)config_search(isasubmatch, self, (void *)NULL);
    108 
    109 	printf("biomask %x ttymask %x netmask %x impmask %x astmask %s\n",
    110 	       biomask, ttymask, netmask, impmask, astmask);
    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 routine. */
    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 	/* Driver says it is there.  Try to reserve ports and memory. */
    163 	if (isa_reserveports(ia.ia_iobase, ia.ia_iosize)) {
    164 		if (isa_reservemem(ia.ia_maddr, ia.ia_msize))
    165 			config_attach(isa, cf, &ia, isaprint);	/* victory! */
    166 		else
    167 			isa_unreserveports(ia.ia_iobase, ia.ia_iosize);
    168 	}
    169 
    170 	/* In any case, move on to next config entry. */
    171 	return 0;
    172 }
    173 
    174 /*
    175  * Called indirectly via config_attach (see above).  Config has already
    176  * printed, e.g., "wdc0 at isa0".  We can ignore our ``isaname'' arg
    177  * (it is always NULL, by definition) and just extend the line with
    178  * the standard info (port(s), irq, drq, and iomem).
    179  */
    180 static int
    181 isaprint(aux, isaname)
    182 	void *aux;
    183 	char *isaname;
    184 {
    185 	struct isa_attach_args *ia = aux;
    186 
    187 	if (ia->ia_iosize)
    188 		printf(" port 0x%x", ia->ia_iobase);
    189 	if (ia->ia_iosize > 1)
    190 		printf("-0x%x", ia->ia_iobase + ia->ia_iosize - 1);
    191 #ifdef DIAGNOSTIC
    192 	if (ia->ia_irq == IRQUNK)
    193 		printf(" THIS IS A BUG ->");
    194 #endif
    195 	if (ia->ia_irq != IRQNONE)
    196 		printf(" irq %d", ffs(ia->ia_irq) - 1);
    197 	if (ia->ia_drq != DRQUNK)
    198 		printf(" drq %d", ia->ia_drq);
    199 	if (ia->ia_msize)
    200 		printf(" iomem 0x%x", ia->ia_maddr);
    201 	if (ia->ia_msize > 1)
    202 		printf("-0x%x", ia->ia_maddr + ia->ia_msize - 1);
    203 	/* XXXX print flags */
    204 	return QUIET;	/* actually, our return value is irrelevant. */
    205 }
    206 
    207 
    208 #define	IDTVEC(name)	__CONCAT(X,name)
    209 /* default interrupt vector table entries */
    210 extern	*IDTVEC(intr)[NIRQ];
    211 /* out of range default interrupt vector gate entry */
    212 extern	IDTVEC(stray);
    213 
    214 /*
    215  * Fill in default interrupt table, and mask all interrupts.
    216  */
    217 static void
    218 isa_defaultirq()
    219 {
    220 	int i;
    221 
    222 	/* icu vectors */
    223 	for (i = ICU_OFFSET; i < ICU_OFFSET + ICU_LEN ; i++)
    224 		setidt(i, IDTVEC(intr)[i],  SDT_SYS386IGT, SEL_KPL);
    225 
    226 	/* out of range vectors */
    227 	for (; i < NIDT; i++)
    228 		setidt(i, &IDTVEC(stray), SDT_SYS386IGT, SEL_KPL);
    229 
    230 	/* initialize 8259's */
    231 	outb(IO_ICU1, 0x11);		/* reset; program device, four bytes */
    232 	outb(IO_ICU1+1, ICU_OFFSET);	/* starting at this vector index */
    233 	outb(IO_ICU1+1, IRQ_SLAVE);
    234 #ifdef AUTO_EOI_1
    235 	outb(IO_ICU1+1, 2 | 1);		/* auto EOI, 8086 mode */
    236 #else
    237 	outb(IO_ICU1+1, 1);		/* 8086 mode */
    238 #endif
    239 	outb(IO_ICU1+1, 0xff);		/* leave interrupts masked */
    240 	outb(IO_ICU1, 0x0a);		/* default to IRR on read */
    241 #ifdef REORDER_IRQ
    242 	outb(IO_ICU1, 0xc0 | (3 - 1));	/* pri order 3-7, 0-2 (com2 first) */
    243 #endif
    244 
    245 	outb(IO_ICU2, 0x11);		/* reset; program device, four bytes */
    246 	outb(IO_ICU2+1, ICU_OFFSET+8);	/* staring at this vector index */
    247 	outb(IO_ICU2+1, ffs(IRQ_SLAVE)-1);
    248 #ifdef AUTO_EOI_2
    249 	outb(IO_ICU2+1, 2 | 1);		/* auto EOI, 8086 mode */
    250 #else
    251 	outb(IO_ICU2+1, 1);		/* 8086 mode */
    252 #endif
    253 	outb(IO_ICU2+1, 0xff);		/* leave interrupts masked */
    254 	outb(IO_ICU2, 0x0a);		/* default to IRR on read */
    255 }
    256 
    257 /*
    258  * Flush any pending interrupts.
    259  */
    260 static void
    261 isa_flushintrs()
    262 {
    263 	register int i;
    264 
    265 	/* clear any pending interrupts */
    266 	for (i = 0; i < 8; i++) {
    267 		outb(IO_ICU1, ICU_EOI);
    268 		outb(IO_ICU2, ICU_EOI);
    269 	}
    270 }
    271 
    272 /*
    273  * Determine what IRQ a device is using by trying to force it to generate an
    274  * interrupt and seeing which IRQ line goes high.  It is not safe to call
    275  * this function after autoconfig.
    276  */
    277 u_short
    278 isa_discoverintr(force, aux)
    279 	void (*force)();
    280 {
    281 	int time = TIMER_FREQ * 1;	/* wait up to 1 second */
    282 	u_int last, now;
    283 	u_short iobase = IO_TIMER1;
    284 
    285 	isa_flushintrs();
    286 	/* attempt to force interrupt */
    287 	force(aux);
    288 	/* set timer 2 to a known state */
    289 	outb(iobase + TIMER_MODE, TIMER_SEL2|TIMER_RATEGEN|TIMER_16BIT);
    290 	outb(iobase + TIMER_CNTR2, 0xff);
    291 	outb(iobase + TIMER_CNTR2, 0xff);
    292 	last = 0xffff;
    293 	while (time > 0) {
    294 		register u_char irr, lo, hi;
    295 		irr = inb(IO_ICU1);
    296 		if (irr)
    297 			return 1 << (ffs(irr) - 1);
    298 		irr = inb(IO_ICU2);
    299 		if (irr)
    300 			return 1 << (ffs(irr) + 7);
    301 		outb(iobase + TIMER_MODE, TIMER_SEL2|TIMER_LATCH);
    302 		lo = inb(iobase + TIMER_CNTR2);
    303 		hi = inb(iobase + TIMER_CNTR2);
    304 		now = (hi << 8) | lo;
    305 		if (now <= last)
    306 			time -= (last - now);
    307 		else
    308 			time -= 0xffff - (now - last);
    309 		last = now;
    310 	}
    311 	return IRQNONE;
    312 }
    313 
    314 /*
    315  * Handle a NMI, possibly a machine check.
    316  * return true to panic system, false to ignore.
    317  *
    318  * This implementation is hist[oe]rical.
    319  */
    320 int
    321 isa_nmi()
    322 {
    323 
    324 	log(LOG_CRIT, "NMI port 61 %x, port 70 %x\n", inb(0x61), inb(0x70));
    325 	return 0;
    326 }
    327 
    328 /*
    329  * Caught a stray interrupt, notify
    330  */
    331 void
    332 isa_strayintr(d)
    333 	int d;
    334 {
    335 	extern u_long intrcnt_stray;
    336 
    337 	/*
    338 	 * Stray level 7 interrupts occur when someone raises an interrupt
    339 	 * and then drops it before the CPU acknowledges it.  This means
    340 	 * either the device is screwed or something is cli'ing too long
    341 	 * and it's timing out.
    342 	 *
    343 	 * -1 is passed by the generic handler for out of range exceptions,
    344 	 * since we don't really want 208 little vectors.  (It wouldn't be
    345 	 * all that much code, but why bother?)
    346 	 */
    347 	if (intrcnt_stray++ < 5)
    348 		if (d == -1)
    349 			log(LOG_ERR, "wild interrupt\n");
    350 		else
    351 			log(LOG_ERR, "stray interrupt %d\n", d);
    352 	if (intrcnt_stray == 5)
    353 		log(LOG_ERR, "too many stray interrupts; stopped logging\n");
    354 }
    355 
    356 static int beeping;
    357 
    358 static void
    359 sysbeepstop(int f)
    360 {
    361 
    362 	/* disable counter 2 */
    363 	disable_intr();
    364 	outb(PITAUX_PORT, inb(PITAUX_PORT) & ~PIT_SPKR);
    365 	enable_intr();
    366 	if (f)
    367 		timeout((timeout_t)sysbeepstop, (caddr_t)0, f);
    368 	else
    369 		beeping = 0;
    370 }
    371 
    372 void
    373 sysbeep(int pitch, int period)
    374 {
    375 	static int last_pitch, last_period;
    376 
    377 	if (beeping) {
    378 		untimeout((timeout_t)sysbeepstop, (caddr_t)(last_period/2));
    379 		untimeout((timeout_t)sysbeepstop, (caddr_t)0);
    380 	}
    381 	if (!beeping || last_pitch != pitch) {
    382 		/*
    383 		 * XXX - move timer stuff to clock.c.
    384 		 */
    385 		disable_intr();
    386 		outb(TIMER_MODE, TIMER_SEL2|TIMER_16BIT|TIMER_SQWAVE);
    387 		outb(TIMER_CNTR2, TIMER_DIV(pitch));
    388 		outb(TIMER_CNTR2, TIMER_DIV(pitch)>>8);
    389 		outb(PITAUX_PORT, inb(PITAUX_PORT) | PIT_SPKR);	/* enable counter 2 */
    390 		enable_intr();
    391 	}
    392 	last_pitch = pitch;
    393 	beeping = last_period = period;
    394 	timeout((timeout_t)sysbeepstop, (caddr_t)(period/2), period);
    395 }
    396