Home | History | Annotate | Line # | Download | only in isa
isa.c revision 1.28.2.19
      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.19 1993/10/27 06:48:36 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/device.h>
     50 
     51 #include <machine/cpu.h>
     52 #include <machine/pio.h>
     53 
     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 int isaprint __P((void *, char *));
     70 void isa_defaultirq __P((void));
     71 void isa_flushintrs __P((void));
     72 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 	intr_enable(IRQ_SLAVE);
    105 
    106 	/* Iterate ``isasubmatch'' over all devices configured here. */
    107 	(void)config_search(isasubmatch, self, (void *)NULL);
    108 
    109 	isa_intrmaskwickedness();
    110 	isa_flushintrs();
    111 }
    112 
    113 /*
    114  * isaattach (above) iterates this function over all the sub-devices that
    115  * were configured at the ``isa'' device.  Our job is to provide defaults
    116  * and do some internal/external representation conversion (some of which
    117  * is scheduled to get cleaned up later), then call the device's probe
    118  * function.  If this says the device is there, we try to reserve ISA
    119  * bus memory and ports for the device, and attach it.
    120  *
    121  * Note that we always return 0, but our ultimate caller (isaattach)
    122  * does not care that we never `match' anything.  (In fact, our return
    123  * value is entirely irrelevant; this function is run entirely for its
    124  * side effects.)
    125  */
    126 static int
    127 isasubmatch(isa, cf, aux)
    128 	struct device *isa;
    129 	struct cfdata *cf;
    130 	void *aux;
    131 {
    132 	struct isa_attach_args ia;
    133 
    134 #ifdef DIAGNOSTIC
    135 	if (cf->cf_driver->cd_match == NULL) {
    136 		/* we really ought to add printf formats to panic(). */
    137 		printf("isasubmatch: no match function for `%s' device\n",
    138 			cf->cf_driver->cd_name);
    139 		panic("isasubmatch: no match function\n");
    140 	}
    141 #endif
    142 
    143 	/* Init the info needed in the device probe and attach routines. */
    144 	ia.ia_iobase = cf->cf_iobase;
    145 	ia.ia_iosize = cf->cf_iosize;
    146 	if (cf->cf_irq == -1)
    147 		ia.ia_irq = IRQUNK;
    148 	else
    149 		ia.ia_irq = 1 << cf->cf_irq;
    150 	ia.ia_drq = cf->cf_drq;
    151 	ia.ia_maddr = (caddr_t)cf->cf_maddr;
    152 	ia.ia_msize = cf->cf_msize;
    153 
    154 	/* If driver says it's not there, believe it. */
    155 	if (!cf->cf_driver->cd_match(isa, cf, &ia))
    156 		return 0;
    157 
    158 	/* Check for port or shared memory conflicts. */
    159 	if (ia.ia_iosize && !isa_portcheck(ia.ia_iobase, ia.ia_iosize))
    160 		return 0;
    161 	if (ia.ia_msize && !isa_memcheck(ia.ia_maddr, ia.ia_msize))
    162 		return 0;
    163 
    164 	/* Reserve I/O ports and/or shared memory. */
    165 	if (ia.ia_iosize)
    166 		isa_portalloc(ia.ia_iobase, ia.ia_iosize);
    167 	if (ia.ia_msize)
    168 		isa_memalloc(ia.ia_maddr, ia.ia_msize);
    169 
    170 	/* Configure device. */
    171 	config_attach(isa, cf, &ia, isaprint);
    172 
    173 	return 0;
    174 }
    175 
    176 /*
    177  * XXX It's not clear to me why this is useful.
    178  */
    179 void
    180 isa_establish(id, dv)
    181 	struct isadev *id;
    182 	struct device *dv;
    183 {
    184 	struct isa_softc *idv = (struct isa_softc *)dv->dv_parent;
    185 	id->id_dev = dv;
    186 	id->id_bchain = idv->sc_isadev;
    187 	idv->sc_isadev = id;
    188 }
    189 
    190 /*
    191  * Called indirectly via config_attach (see above).  Config has already
    192  * printed, e.g., "wdc0 at isa0".  We can ignore our ``isaname'' arg
    193  * (it is always NULL, by definition) and just extend the line with
    194  * the standard info (port(s), irq, drq, and iomem).
    195  */
    196 static int
    197 isaprint(aux, isaname)
    198 	void *aux;
    199 	char *isaname;
    200 {
    201 	struct isa_attach_args *ia = aux;
    202 
    203 	if (ia->ia_iosize)
    204 		printf(" port 0x%x", ia->ia_iobase);
    205 	if (ia->ia_iosize > 1)
    206 		printf("-0x%x", ia->ia_iobase + ia->ia_iosize - 1);
    207 	if (ia->ia_msize)
    208 		printf(" iomem 0x%x", ia->ia_maddr);
    209 	if (ia->ia_msize > 1)
    210 		printf("-0x%x", ia->ia_maddr + ia->ia_msize - 1);
    211 #ifdef DIAGNOSTIC
    212 	if (ia->ia_irq == IRQUNK)
    213 		printf(" THIS IS A BUG ->");
    214 #endif
    215 	if (ia->ia_irq != IRQNONE)
    216 		printf(" irq %d", ffs(ia->ia_irq) - 1);
    217 	if (ia->ia_drq != DRQUNK)
    218 		printf(" drq %d", ia->ia_drq);
    219 	/* XXXX print flags */
    220 	return QUIET;	/* actually, our return value is irrelevant. */
    221 }
    222 
    223 /*
    224 int isa_portcheck __P((u_int, u_int));
    225 int isa_memcheck __P((u_int, u_int));
    226 void isa_portalloc __P((u_int, u_int));
    227 void isa_memalloc __P((u_int, u_int));
    228 */
    229 
    230 static int beeping;
    231 
    232 static void
    233 sysbeepstop(int f)
    234 {
    235 
    236 	/* disable counter 2 */
    237 	disable_intr();
    238 	outb(PITAUX_PORT, inb(PITAUX_PORT) & ~PIT_SPKR);
    239 	enable_intr();
    240 	if (f)
    241 		timeout((timeout_t)sysbeepstop, (caddr_t)0, f);
    242 	else
    243 		beeping = 0;
    244 }
    245 
    246 void
    247 sysbeep(int pitch, int period)
    248 {
    249 	static int last_pitch, last_period;
    250 
    251 	if (beeping) {
    252 		untimeout((timeout_t)sysbeepstop, (caddr_t)(last_period/2));
    253 		untimeout((timeout_t)sysbeepstop, (caddr_t)0);
    254 	}
    255 	if (!beeping || last_pitch != pitch) {
    256 		/*
    257 		 * XXX - move timer stuff to clock.c.
    258 		 */
    259 		disable_intr();
    260 		outb(TIMER_MODE, TIMER_SEL2|TIMER_16BIT|TIMER_SQWAVE);
    261 		outb(TIMER_CNTR2, TIMER_DIV(pitch));
    262 		outb(TIMER_CNTR2, TIMER_DIV(pitch)>>8);
    263 		outb(PITAUX_PORT, inb(PITAUX_PORT) | PIT_SPKR);	/* enable counter 2 */
    264 		enable_intr();
    265 	}
    266 	last_pitch = pitch;
    267 	beeping = last_period = period;
    268 	timeout((timeout_t)sysbeepstop, (caddr_t)(period/2), period);
    269 }
    270