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