Home | History | Annotate | Line # | Download | only in isa
isa.c revision 1.52
      1 /*-
      2  * Copyright (c) 1993, 1994 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.52 1994/04/24 01:34:09 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/kernel.h>
     48 #include <sys/conf.h>
     49 #include <sys/malloc.h>
     50 #include <sys/device.h>
     51 
     52 #include <machine/pio.h>
     53 #include <machine/cpufunc.h>
     54 
     55 #include <i386/isa/isareg.h>
     56 #include <i386/isa/isa_device.h>
     57 #include <i386/isa/isavar.h>
     58 #include <i386/isa/ic/i8042.h>
     59 #include <i386/isa/timerreg.h>
     60 #include <i386/isa/spkrreg.h>
     61 
     62 /* sorry, has to be here, no place else really suitable */
     63 #include <machine/pc/display.h>
     64 u_short *Crtat = (u_short *)MONO_BUF;
     65 
     66 static void sysbeepstop(int);
     67 
     68 /*
     69  * Configure all ISA devices
     70  *
     71  * XXX This code is a hack.  It wants to be new config, but can't be until the
     72  * interrupt system is redone.  For now, we do some gross hacks to make it look
     73  * 99% like new config.
     74  */
     75 static char *msgs[3] = { "", " not configured\n", " unsupported\n" };
     76 
     77 struct cfdata *
     78 config_search(fn, parent, aux)
     79 	cfmatch_t fn;
     80 	struct device *parent;
     81 	void *aux;
     82 {
     83 	struct cfdata *cf = 0;
     84 	struct device *dv = 0;
     85 	size_t devsize;
     86 	struct cfdriver *cd;
     87 	struct isa_device *id,
     88 	    *idp =  parent ? (void *)parent->dv_cfdata->cf_loc : 0;
     89 
     90 	for (id = isa_devtab; id->id_driver; id++) {
     91 		if (id->id_state == FSTATE_FOUND)
     92 			continue;
     93 		if (id->id_parent != idp)
     94 			continue;
     95 		cd = id->id_driver;
     96 		if (id->id_unit < cd->cd_ndevs) {
     97 			if (cd->cd_devs[id->id_unit] != 0)
     98 				continue;
     99 		} else {
    100 			int old = cd->cd_ndevs, new;
    101 			void **nsp;
    102 
    103 			if (old == 0) {
    104 				nsp = malloc(MINALLOCSIZE, M_DEVBUF, M_NOWAIT);
    105 				if (!nsp)
    106 					panic("config_search: creating dev array");
    107 				bzero(nsp, MINALLOCSIZE);
    108 				cd->cd_ndevs = MINALLOCSIZE / sizeof(void *);
    109 			} else {
    110 				new = old;
    111 				do {
    112 					new *= 2;
    113 				} while (new <= id->id_unit);
    114 				cd->cd_ndevs = new;
    115 				nsp = malloc(new * sizeof(void *), M_DEVBUF,
    116 				    M_NOWAIT);
    117 				if (!nsp)
    118 					panic("config_search: expanding dev array");
    119 				bzero(nsp, new * sizeof(void *));
    120 				bcopy(cd->cd_devs, nsp, old * sizeof(void *));
    121 				free(cd->cd_devs, M_DEVBUF);
    122 			}
    123 			cd->cd_devs = nsp;
    124 		}
    125 		if (!cf) {
    126 			cf = malloc(sizeof(struct cfdata), M_DEVBUF, M_NOWAIT);
    127 			if (!cf)
    128 				panic("config_search: creating cfdata");
    129 		}
    130 		cf->cf_driver = cd;
    131 		cf->cf_unit = id->id_unit;
    132 		cf->cf_fstate = 0;
    133 		cf->cf_loc = (void *)id;
    134 		cf->cf_flags = id->id_flags;
    135 		cf->cf_parents = 0;
    136 		cf->cf_ivstubs = 0;
    137 		if (dv && devsize != cd->cd_devsize) {
    138 			free(dv, M_DEVBUF);
    139 			dv = 0;
    140 		}
    141 		if (!dv) {
    142 			devsize = cd->cd_devsize;
    143 			dv = malloc(devsize, M_DEVBUF, M_NOWAIT);
    144 			if (!dv)
    145 				panic("config_search: creating softc");
    146 		}
    147 		bzero(dv, cd->cd_devsize);
    148 		dv->dv_class = cd->cd_class;
    149 		dv->dv_cfdata = cf;
    150 		dv->dv_unit = id->id_unit;
    151 		sprintf(dv->dv_xname, "%s%d", cd->cd_name, id->id_unit);
    152 		dv->dv_parent = parent;
    153 		cd->cd_devs[id->id_unit] = dv;
    154 		if (fn) {
    155 			if ((*fn)(parent, dv, aux))
    156 				return cf;
    157 		} else {
    158 			if ((*cd->cd_match)(parent, dv, aux))
    159 				return cf;
    160 		}
    161 		cd->cd_devs[id->id_unit] = 0;
    162 	}
    163 	if (cf)
    164 		free(cf, M_DEVBUF);
    165 	if (dv)
    166 		free(dv, M_DEVBUF);
    167 	return 0;
    168 }
    169 
    170 void
    171 config_attach(parent, cf, aux, print)
    172 	struct device *parent;
    173 	struct cfdata *cf;
    174 	void *aux;
    175 	cfprint_t print;
    176 {
    177 	struct isa_device *id = (void *)cf->cf_loc;
    178 	struct cfdriver *cd = cf->cf_driver;
    179 	struct device *dv = cd->cd_devs[id->id_unit];
    180 
    181 	cf->cf_fstate = id->id_state = FSTATE_FOUND;
    182 	printf("%s at %s", dv->dv_xname, parent ? parent->dv_xname : "isa0");
    183 	if (print)
    184 		(void) (*print)(aux, (char *)0);
    185 	(*cd->cd_attach)(parent, dv, aux);
    186 }
    187 
    188 int
    189 config_found(parent, aux, print)
    190 	struct device *parent;
    191 	void *aux;
    192 	cfprint_t print;
    193 {
    194 	struct cfdata *cf;
    195 
    196 	if ((cf = config_search((cfmatch_t)NULL, parent, aux)) != NULL) {
    197 		config_attach(parent, cf, aux, print);
    198 		return 1;
    199 	}
    200 	if (print)
    201 		printf(msgs[(*print)(aux, parent->dv_xname)]);
    202 	return 0;
    203 }
    204 
    205 int
    206 isaprint(aux, isa)
    207 	void *aux;
    208 	char *isa;
    209 {
    210 	struct isa_attach_args *ia = aux;
    211 
    212 	if (ia->ia_iosize)
    213 		printf(" port 0x%x", ia->ia_iobase);
    214 	if (ia->ia_iosize > 1)
    215 		printf("-0x%x", ia->ia_iobase + ia->ia_iosize - 1);
    216 	if (ia->ia_msize)
    217 		printf(" iomem 0x%x", ia->ia_maddr - atdevbase + 0xa0000);
    218 	if (ia->ia_msize > 1)
    219 		printf("-0x%x",
    220 		    ia->ia_maddr - atdevbase + 0xa0000 + ia->ia_msize - 1);
    221 	if (ia->ia_irq)
    222 		printf(" irq %d", ffs(ia->ia_irq) - 1);
    223 	if (ia->ia_drq != (u_short)-1)
    224 		printf(" drq %d", ia->ia_drq);
    225 	/* XXXX print flags */
    226 	return QUIET;
    227 }
    228 
    229 int
    230 isasubmatch(parent, self, aux)
    231 	struct device *parent, *self;
    232 	void *aux;
    233 {
    234 	struct isa_device *id = (void *)self->dv_cfdata->cf_loc;
    235 	struct isa_attach_args ia;
    236 
    237 	ia.ia_iobase = id->id_iobase;
    238 	ia.ia_iosize = 0x666;
    239 	ia.ia_irq = id->id_irq;
    240 	ia.ia_drq = id->id_drq;
    241 	ia.ia_maddr = id->id_maddr - 0xa0000 + atdevbase;
    242 	ia.ia_msize = id->id_msize;
    243 
    244 	if (!(*id->id_driver->cd_match)(parent, self, &ia)) {
    245 		/*
    246 		 * If we don't do this, isa_configure() will repeatedly try to
    247 		 * probe devices that weren't found.  But we need to be careful
    248 		 * to do it only for the ISA bus, or we would cause things like
    249 		 * `com0 at ast? slave ?' to not probe on the second ast.
    250 		 */
    251 		if (!parent)
    252 			id->id_state = FSTATE_FOUND;
    253 
    254 		return 0;
    255 	}
    256 
    257 	config_attach(parent, self->dv_cfdata, &ia, isaprint);
    258 
    259 	return 1;
    260 }
    261 
    262 void
    263 isa_configure()
    264 {
    265 
    266 	while (config_search(isasubmatch, NULL, NULL));
    267 
    268 	printf("biomask %x netmask %x ttymask %x\n",
    269 	    (u_short)imask[IPL_BIO], (u_short)imask[IPL_NET],
    270 	    (u_short)imask[IPL_TTY]);
    271 
    272 	spl0();
    273 }
    274 
    275 static int beeping;
    276 
    277 static void
    278 sysbeepstop(int f)
    279 {
    280 	int s = splhigh();
    281 
    282 	/* disable counter 2 */
    283 	disable_intr();
    284 	outb(PITAUX_PORT, inb(PITAUX_PORT) & ~PIT_SPKR);
    285 	enable_intr();
    286 	if (f)
    287 		timeout((timeout_t)sysbeepstop, (caddr_t)0, f);
    288 	else
    289 		beeping = 0;
    290 
    291 	splx(s);
    292 }
    293 
    294 void
    295 sysbeep(int pitch, int period)
    296 {
    297 	int s = splhigh();
    298 	static int last_pitch, last_period;
    299 
    300 	if (beeping) {
    301 		untimeout((timeout_t)sysbeepstop, (caddr_t)(last_period/2));
    302 		untimeout((timeout_t)sysbeepstop, (caddr_t)0);
    303 	}
    304 	if (!beeping || last_pitch != pitch) {
    305 		/*
    306 	 	* XXX - move timer stuff to clock.c.
    307 	 	*/
    308 		disable_intr();
    309 		outb(TIMER_MODE, TIMER_SEL2|TIMER_16BIT|TIMER_SQWAVE);
    310 		outb(TIMER_CNTR2, TIMER_DIV(pitch)%256);
    311 		outb(TIMER_CNTR2, TIMER_DIV(pitch)/256);
    312 		outb(PITAUX_PORT, inb(PITAUX_PORT) | PIT_SPKR);	/* enable counter 2 */
    313 		enable_intr();
    314 	}
    315 	last_pitch = pitch;
    316 	beeping = last_period = period;
    317 	timeout((timeout_t)sysbeepstop, (caddr_t)(period/2), period);
    318 
    319 	splx(s);
    320 }
    321