Home | History | Annotate | Line # | Download | only in obio
asc.c revision 1.33
      1 /*	$NetBSD: asc.c,v 1.33 1999/07/08 18:08:54 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1997 Scott Reynolds
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 /*-
     30  * Copyright (C) 1993	Allen K. Briggs, Chris P. Caputo,
     31  *			Michael L. Finch, Bradley A. Grantham, and
     32  *			Lawrence A. Kesteloot
     33  * All rights reserved.
     34  *
     35  * Redistribution and use in source and binary forms, with or without
     36  * modification, are permitted provided that the following conditions
     37  * are met:
     38  * 1. Redistributions of source code must retain the above copyright
     39  *    notice, this list of conditions and the following disclaimer.
     40  * 2. Redistributions in binary form must reproduce the above copyright
     41  *    notice, this list of conditions and the following disclaimer in the
     42  *    documentation and/or other materials provided with the distribution.
     43  * 3. All advertising materials mentioning features or use of this software
     44  *    must display the following acknowledgement:
     45  *	This product includes software developed by the Alice Group.
     46  * 4. The names of the Alice Group or any of its members may not be used
     47  *    to endorse or promote products derived from this software without
     48  *    specific prior written permission.
     49  *
     50  * THIS SOFTWARE IS PROVIDED BY THE ALICE GROUP ``AS IS'' AND ANY EXPRESS OR
     51  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     52  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     53  * IN NO EVENT SHALL THE ALICE GROUP BE LIABLE FOR ANY DIRECT, INDIRECT,
     54  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     55  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     56  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     57  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     58  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     59  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     60  */
     61 
     62 /*
     63  * ASC driver code and console bell support
     64  */
     65 
     66 #include <sys/types.h>
     67 #include <sys/cdefs.h>
     68 #include <sys/errno.h>
     69 #include <sys/time.h>
     70 #include <sys/systm.h>
     71 #include <sys/param.h>
     72 #include <sys/device.h>
     73 #include <sys/poll.h>
     74 
     75 #include <vm/vm.h>
     76 #include <vm/pmap.h>
     77 
     78 #include <machine/autoconf.h>
     79 #include <machine/cpu.h>
     80 #include <machine/bus.h>
     81 #include <machine/viareg.h>
     82 
     83 #include <mac68k/obio/ascvar.h>
     84 #include <mac68k/obio/obiovar.h>
     85 
     86 #define	MAC68K_ASC_BASE		0x50f14000
     87 #define	MAC68K_IIFX_ASC_BASE	0x50f10000
     88 #define	MAC68K_ASC_LEN		0x01000
     89 
     90 #ifdef DEBUG
     91 #define ASC_DEBUG
     92 #endif
     93 
     94 #ifdef ASC_DEBUG
     95 int	asc_debug = 0;		/* non-zero enables debugging output */
     96 #endif
     97 
     98 static u_int8_t		asc_wave_tab[0x800];
     99 
    100 static int	asc_ring_bell __P((void *, int, int, int));
    101 static void	asc_stop_bell __P((void *));
    102 #if __notyet__
    103 static void	asc_intr_enable __P((void));
    104 static void	asc_intr __P((void *));
    105 #endif
    106 
    107 static int	ascmatch __P((struct device *, struct cfdata *, void *));
    108 static void	ascattach __P((struct device *, struct device *, void *));
    109 
    110 struct cfattach asc_ca = {
    111 	sizeof(struct asc_softc), ascmatch, ascattach
    112 };
    113 
    114 extern struct cfdriver asc_cd;
    115 
    116 static int
    117 ascmatch(parent, cf, aux)
    118 	struct device *parent;
    119 	struct cfdata *cf;
    120 	void *aux;
    121 {
    122 	struct obio_attach_args *oa = (struct obio_attach_args *)aux;
    123 	bus_addr_t addr;
    124 	bus_space_handle_t bsh;
    125 	int rval = 0;
    126 
    127 	if (oa->oa_addr != (-1))
    128 		addr = (bus_addr_t)oa->oa_addr;
    129 	else if (current_mac_model->machineid == MACH_MACIIFX)
    130 		addr = (bus_addr_t)MAC68K_IIFX_ASC_BASE;
    131 	else
    132 		addr = (bus_addr_t)MAC68K_ASC_BASE;
    133 
    134 	if (bus_space_map(oa->oa_tag, addr, MAC68K_ASC_LEN, 0, &bsh))
    135 		return (0);
    136 
    137 	if (mac68k_bus_space_probe(oa->oa_tag, bsh, 0, 1))
    138 		rval = 1;
    139 	else
    140 		rval = 0;
    141 
    142 	bus_space_unmap(oa->oa_tag, bsh, MAC68K_ASC_LEN);
    143 
    144 	return rval;
    145 }
    146 
    147 static void
    148 ascattach(parent, self, aux)
    149 	struct device *parent, *self;
    150 	void *aux;
    151 {
    152 	struct asc_softc *sc = (struct asc_softc *)self;
    153 	struct obio_attach_args *oa = (struct obio_attach_args *)aux;
    154 	bus_addr_t addr;
    155 	int i;
    156 
    157 	sc->sc_tag = oa->oa_tag;
    158 	if (oa->oa_addr != (-1))
    159 		addr = (bus_addr_t)oa->oa_addr;
    160 	else if (current_mac_model->machineid == MACH_MACIIFX)
    161 		addr = (bus_addr_t)MAC68K_IIFX_ASC_BASE;
    162 	else
    163 		addr = (bus_addr_t)MAC68K_ASC_BASE;
    164 	if (bus_space_map(sc->sc_tag, addr, MAC68K_ASC_LEN, 0,
    165 	    &sc->sc_handle)) {
    166 		printf(": can't map memory space\n");
    167 		return;
    168 	}
    169 	sc->sc_open = 0;
    170 	sc->sc_ringing = 0;
    171 
    172 	for (i = 0; i < 256; i++) {	/* up part of wave, four voices? */
    173 		asc_wave_tab[i] = i / 4;
    174 		asc_wave_tab[i + 512] = i / 4;
    175 		asc_wave_tab[i + 1024] = i / 4;
    176 		asc_wave_tab[i + 1536] = i / 4;
    177 	}
    178 	for (i = 0; i < 256; i++) {	/* down part of wave, four voices? */
    179 		asc_wave_tab[i + 256] = 0x3f - (i / 4);
    180 		asc_wave_tab[i + 768] = 0x3f - (i / 4);
    181 		asc_wave_tab[i + 1280] = 0x3f - (i / 4);
    182 		asc_wave_tab[i + 1792] = 0x3f - (i / 4);
    183 	}
    184 
    185 	printf(": Apple Sound Chip");
    186 	if (oa->oa_addr != (-1))
    187 		printf(" at %x", oa->oa_addr);
    188 	printf("\n");
    189 
    190 	mac68k_set_bell_callback(asc_ring_bell, sc);
    191 #if __notyet__
    192 	if (mac68k_machine.aux_interrupts) {
    193 		intr_establish((int (*)(void *))asc_intr, sc, 5);
    194 	} else {
    195 		via2_register_irq(VIA2_ASC, asc_intr, sc);
    196 	}
    197 	asc_intr_enable();
    198 #endif
    199 }
    200 
    201 int
    202 ascopen(dev, flag, mode, p)
    203 	dev_t dev;
    204 	int flag;
    205 	int mode;
    206 	struct proc *p;
    207 {
    208 	struct asc_softc *sc;
    209 	int unit;
    210 
    211 	unit = ASCUNIT(dev);
    212 	sc = asc_cd.cd_devs[unit];
    213 	if (unit >= asc_cd.cd_ndevs)
    214 		return (ENXIO);
    215 	if (sc->sc_open)
    216 		return (EBUSY);
    217 	sc->sc_open = 1;
    218 
    219 	return (0);
    220 }
    221 
    222 int
    223 ascclose(dev, flag, mode, p)
    224 	dev_t dev;
    225 	int flag;
    226 	int mode;
    227 	struct proc *p;
    228 {
    229 	struct asc_softc *sc;
    230 
    231 	sc = asc_cd.cd_devs[ASCUNIT(dev)];
    232 	sc->sc_open = 0;
    233 
    234 	return (0);
    235 }
    236 
    237 int
    238 ascread(dev, uio, ioflag)
    239 	dev_t dev;
    240 	struct uio *uio;
    241 	int ioflag;
    242 {
    243 	return (ENXIO);
    244 }
    245 
    246 int
    247 ascwrite(dev, uio, ioflag)
    248 	dev_t dev;
    249 	struct uio *uio;
    250 	int ioflag;
    251 {
    252 	return (ENXIO);
    253 }
    254 
    255 int
    256 ascioctl(dev, cmd, data, flag, p)
    257 	dev_t dev;
    258 	int cmd;
    259 	caddr_t data;
    260 	int flag;
    261 	struct proc *p;
    262 {
    263 	struct asc_softc *sc;
    264 	int error;
    265 	int unit = ASCUNIT(dev);
    266 
    267 	sc = asc_cd.cd_devs[unit];
    268 	error = 0;
    269 
    270 	switch (cmd) {
    271 	default:
    272 		error = EINVAL;
    273 		break;
    274 	}
    275 	return (error);
    276 }
    277 
    278 int
    279 ascpoll(dev, events, p)
    280 	dev_t dev;
    281 	int events;
    282 	struct proc *p;
    283 {
    284 	return (events & (POLLOUT | POLLWRNORM));
    285 }
    286 
    287 int
    288 ascmmap(dev, off, prot)
    289 	dev_t dev;
    290 	int off;
    291 	int prot;
    292 {
    293 	int unit = ASCUNIT(dev);
    294 	struct asc_softc *sc;
    295 	paddr_t pa;
    296 
    297 	sc = asc_cd.cd_devs[unit];
    298 	if ((u_int)off < MAC68K_ASC_LEN) {
    299 		(void) pmap_extract(pmap_kernel(), (vaddr_t)sc->sc_handle, &pa);
    300 		return m68k_btop(pa + off);
    301 	}
    302 
    303 	return (-1);
    304 }
    305 
    306 static int
    307 asc_ring_bell(arg, freq, length, volume)
    308 	void *arg;
    309 	int freq, length, volume;
    310 {
    311 	struct asc_softc *sc = (struct asc_softc *)arg;
    312 	unsigned long cfreq;
    313 	int i;
    314 
    315 	if (!sc)
    316 		return (ENODEV);
    317 
    318 	if (sc->sc_ringing == 0) {
    319 
    320 		bus_space_write_multi_1(sc->sc_tag, sc->sc_handle,
    321 		    0, 0, 0x800);
    322 		bus_space_write_region_1(sc->sc_tag, sc->sc_handle,
    323 		    0, asc_wave_tab, 0x800);
    324 
    325 		/* Fix this.  Need to find exact ASC sampling freq */
    326 		cfreq = 65536 * freq / 466;
    327 
    328 		/* printf("beep: from %d, %02x %02x %02x %02x\n",
    329 		 * cur_beep.freq, (cfreq >> 24) & 0xff, (cfreq >> 16) & 0xff,
    330 		 * (cfreq >> 8) & 0xff, (cfreq) & 0xff); */
    331 		for (i = 0; i < 8; i++) {
    332 			bus_space_write_1(sc->sc_tag, sc->sc_handle,
    333 			    0x814 + 8 * i, (cfreq >> 24) & 0xff);
    334 			bus_space_write_1(sc->sc_tag, sc->sc_handle,
    335 			    0x815 + 8 * i, (cfreq >> 16) & 0xff);
    336 			bus_space_write_1(sc->sc_tag, sc->sc_handle,
    337 			    0x816 + 8 * i, (cfreq >> 8) & 0xff);
    338 			bus_space_write_1(sc->sc_tag, sc->sc_handle,
    339 			    0x817 + 8 * i, (cfreq) & 0xff);
    340 		}		/* frequency; should put cur_beep.freq in here
    341 				 * somewhere. */
    342 
    343 		bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x807, 3); /* 44 ? */
    344 		bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x806,
    345 		    255 * volume / 100);
    346 		bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x805, 0);
    347 		bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x80f, 0);
    348 		bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x802, 2); /* sampled */
    349 		bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x801, 2); /* enable sampled */
    350 	}
    351 	sc->sc_ringing++;
    352 	timeout(asc_stop_bell, sc, length);
    353 
    354 	return (0);
    355 }
    356 
    357 static void
    358 asc_stop_bell(arg)
    359 	void *arg;
    360 {
    361 	struct asc_softc *sc = (struct asc_softc *)arg;
    362 
    363 	if (!sc)
    364 		return;
    365 
    366 	if (sc->sc_ringing > 1000 || sc->sc_ringing < 0)
    367 		panic("bell got out of sync?");
    368 
    369 	if (--sc->sc_ringing == 0)	/* disable ASC */
    370 		bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x801, 0);
    371 }
    372 
    373 #if __notyet__
    374 static void
    375 asc_intr_enable()
    376 {
    377 	int s;
    378 
    379 	s = splhigh();
    380 	if (VIA2 == VIA2OFF)
    381 		via2_reg(vIER) = 0x80 | V2IF_ASC;
    382 	else
    383 		via2_reg(rIER) = 0x80 | V2IF_ASC;
    384 	splx(s);
    385 }
    386 
    387 
    388 /*ARGSUSED*/
    389 static void
    390 asc_intr(arg)
    391         void *arg;
    392 {
    393 #ifdef ASC_DEBUG
    394 	struct asc_softc *sc = (struct asc_softc *)arg;
    395 
    396 	if (asc_debug)
    397 		printf("asc_intr(%p)\n", sc);
    398 #endif
    399 }
    400 #endif
    401