Home | History | Annotate | Line # | Download | only in obio
asc.c revision 1.29
      1 /*	$NetBSD: asc.c,v 1.29 1998/11/19 15:38:23 mrg 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 static void	asc_intr_enable __P((void));
    103 static void	asc_intr __P((void *));
    104 
    105 static int	ascmatch __P((struct device *, struct cfdata *, void *));
    106 static void	ascattach __P((struct device *, struct device *, void *));
    107 
    108 struct cfattach asc_ca = {
    109 	sizeof(struct asc_softc), ascmatch, ascattach
    110 };
    111 
    112 extern struct cfdriver asc_cd;
    113 
    114 static int
    115 ascmatch(parent, cf, aux)
    116 	struct device *parent;
    117 	struct cfdata *cf;
    118 	void *aux;
    119 {
    120 	struct obio_attach_args *oa = (struct obio_attach_args *)aux;
    121 	bus_addr_t addr;
    122 	bus_space_handle_t bsh;
    123 	int rval = 0;
    124 
    125 	if (oa->oa_addr != (-1))
    126 		addr = (bus_addr_t)oa->oa_addr;
    127 	else if (current_mac_model->machineid == MACH_MACIIFX)
    128 		addr = (bus_addr_t)MAC68K_IIFX_ASC_BASE;
    129 	else
    130 		addr = (bus_addr_t)MAC68K_ASC_BASE;
    131 
    132 	if (bus_space_map(oa->oa_tag, addr, MAC68K_ASC_LEN, 0, &bsh))
    133 		return (0);
    134 
    135 	if (mac68k_bus_space_probe(oa->oa_tag, bsh, 0, 1))
    136 		rval = 1;
    137 	else
    138 		rval = 0;
    139 
    140 	bus_space_unmap(oa->oa_tag, bsh, MAC68K_ASC_LEN);
    141 
    142 	return rval;
    143 }
    144 
    145 static void
    146 ascattach(parent, self, aux)
    147 	struct device *parent, *self;
    148 	void *aux;
    149 {
    150 	struct asc_softc *sc = (struct asc_softc *)self;
    151 	struct obio_attach_args *oa = (struct obio_attach_args *)aux;
    152 	bus_addr_t addr;
    153 	int i;
    154 
    155 	sc->sc_tag = oa->oa_tag;
    156 	if (oa->oa_addr != (-1))
    157 		addr = (bus_addr_t)oa->oa_addr;
    158 	else if (current_mac_model->machineid == MACH_MACIIFX)
    159 		addr = (bus_addr_t)MAC68K_IIFX_ASC_BASE;
    160 	else
    161 		addr = (bus_addr_t)MAC68K_ASC_BASE;
    162 	if (bus_space_map(sc->sc_tag, addr, MAC68K_ASC_LEN, 0,
    163 	    &sc->sc_handle)) {
    164 		printf(": can't map memory space\n");
    165 		return;
    166 	}
    167 	sc->sc_open = 0;
    168 	sc->sc_ringing = 0;
    169 
    170 	for (i = 0; i < 256; i++) {	/* up part of wave, four voices? */
    171 		asc_wave_tab[i] = i / 4;
    172 		asc_wave_tab[i + 512] = i / 4;
    173 		asc_wave_tab[i + 1024] = i / 4;
    174 		asc_wave_tab[i + 1536] = i / 4;
    175 	}
    176 	for (i = 0; i < 256; i++) {	/* down part of wave, four voices? */
    177 		asc_wave_tab[i + 256] = 0x3f - (i / 4);
    178 		asc_wave_tab[i + 768] = 0x3f - (i / 4);
    179 		asc_wave_tab[i + 1280] = 0x3f - (i / 4);
    180 		asc_wave_tab[i + 1792] = 0x3f - (i / 4);
    181 	}
    182 
    183 	printf(": Apple Sound Chip");
    184 	if (oa->oa_addr != (-1))
    185 		printf(" at %x", oa->oa_addr);
    186 	printf("\n");
    187 
    188 	mac68k_set_bell_callback(asc_ring_bell, sc);
    189 
    190 	via2_register_irq(VIA2_ASC, asc_intr, sc);
    191 	asc_intr_enable();
    192 }
    193 
    194 int
    195 ascopen(dev, flag, mode, p)
    196 	dev_t dev;
    197 	int flag;
    198 	int mode;
    199 	struct proc *p;
    200 {
    201 	struct asc_softc *sc;
    202 	int unit;
    203 
    204 	unit = ASCUNIT(dev);
    205 	sc = asc_cd.cd_devs[unit];
    206 	if (unit >= asc_cd.cd_ndevs)
    207 		return (ENXIO);
    208 	if (sc->sc_open)
    209 		return (EBUSY);
    210 	sc->sc_open = 1;
    211 
    212 	return (0);
    213 }
    214 
    215 int
    216 ascclose(dev, flag, mode, p)
    217 	dev_t dev;
    218 	int flag;
    219 	int mode;
    220 	struct proc *p;
    221 {
    222 	struct asc_softc *sc;
    223 
    224 	sc = asc_cd.cd_devs[ASCUNIT(dev)];
    225 	sc->sc_open = 0;
    226 
    227 	return (0);
    228 }
    229 
    230 int
    231 ascread(dev, uio, ioflag)
    232 	dev_t dev;
    233 	struct uio *uio;
    234 	int ioflag;
    235 {
    236 	return (ENXIO);
    237 }
    238 
    239 int
    240 ascwrite(dev, uio, ioflag)
    241 	dev_t dev;
    242 	struct uio *uio;
    243 	int ioflag;
    244 {
    245 	return (ENXIO);
    246 }
    247 
    248 int
    249 ascioctl(dev, cmd, data, flag, p)
    250 	dev_t dev;
    251 	int cmd;
    252 	caddr_t data;
    253 	int flag;
    254 	struct proc *p;
    255 {
    256 	struct asc_softc *sc;
    257 	int error;
    258 	int unit = ASCUNIT(dev);
    259 
    260 	sc = asc_cd.cd_devs[unit];
    261 	error = 0;
    262 
    263 	switch (cmd) {
    264 	default:
    265 		error = EINVAL;
    266 		break;
    267 	}
    268 	return (error);
    269 }
    270 
    271 int
    272 ascpoll(dev, events, p)
    273 	dev_t dev;
    274 	int events;
    275 	struct proc *p;
    276 {
    277 	return (events & (POLLOUT | POLLWRNORM));
    278 }
    279 
    280 int
    281 ascmmap(dev, off, prot)
    282 	dev_t dev;
    283 	int off;
    284 	int prot;
    285 {
    286 	int unit = ASCUNIT(dev);
    287 	struct asc_softc *sc;
    288 	vm_offset_t pa;
    289 
    290 	sc = asc_cd.cd_devs[unit];
    291 	if ((u_int)off < MAC68K_ASC_LEN) {
    292 		pa = pmap_extract(pmap_kernel(), (vm_offset_t)sc->sc_handle);
    293 		return m68k_btop(pa + off);
    294 	}
    295 
    296 	return (-1);
    297 }
    298 
    299 static int
    300 asc_ring_bell(arg, freq, length, volume)
    301 	void *arg;
    302 	int freq, length, volume;
    303 {
    304 	struct asc_softc *sc = (struct asc_softc *)arg;
    305 	unsigned long cfreq;
    306 	int i;
    307 
    308 	if (!sc)
    309 		return (ENODEV);
    310 
    311 	if (sc->sc_ringing == 0) {
    312 
    313 		bus_space_write_multi_1(sc->sc_tag, sc->sc_handle,
    314 		    0, 0, 0x800);
    315 		bus_space_write_region_1(sc->sc_tag, sc->sc_handle,
    316 		    0, asc_wave_tab, 0x800);
    317 
    318 		/* Fix this.  Need to find exact ASC sampling freq */
    319 		cfreq = 65536 * freq / 466;
    320 
    321 		/* printf("beep: from %d, %02x %02x %02x %02x\n",
    322 		 * cur_beep.freq, (cfreq >> 24) & 0xff, (cfreq >> 16) & 0xff,
    323 		 * (cfreq >> 8) & 0xff, (cfreq) & 0xff); */
    324 		for (i = 0; i < 8; i++) {
    325 			bus_space_write_1(sc->sc_tag, sc->sc_handle,
    326 			    0x814 + 8 * i, (cfreq >> 24) & 0xff);
    327 			bus_space_write_1(sc->sc_tag, sc->sc_handle,
    328 			    0x815 + 8 * i, (cfreq >> 16) & 0xff);
    329 			bus_space_write_1(sc->sc_tag, sc->sc_handle,
    330 			    0x816 + 8 * i, (cfreq >> 8) & 0xff);
    331 			bus_space_write_1(sc->sc_tag, sc->sc_handle,
    332 			    0x817 + 8 * i, (cfreq) & 0xff);
    333 		}		/* frequency; should put cur_beep.freq in here
    334 				 * somewhere. */
    335 
    336 		bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x807, 3); /* 44 ? */
    337 		bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x806,
    338 		    255 * volume / 100);
    339 		bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x805, 0);
    340 		bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x80f, 0);
    341 		bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x802, 2); /* sampled */
    342 		bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x801, 2); /* enable sampled */
    343 	}
    344 	sc->sc_ringing++;
    345 	timeout(asc_stop_bell, sc, length);
    346 
    347 	return (0);
    348 }
    349 
    350 static void
    351 asc_stop_bell(arg)
    352 	void *arg;
    353 {
    354 	struct asc_softc *sc = (struct asc_softc *)arg;
    355 
    356 	if (!sc)
    357 		return;
    358 
    359 	if (sc->sc_ringing > 1000 || sc->sc_ringing < 0)
    360 		panic("bell got out of sync?");
    361 
    362 	if (--sc->sc_ringing == 0)	/* disable ASC */
    363 		bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x801, 0);
    364 }
    365 
    366 static void
    367 asc_intr_enable()
    368 {
    369 	int s;
    370 
    371 	s = splhigh();
    372 	if (VIA2 == VIA2OFF)
    373 		via2_reg(vIER) = 0x80 | V2IF_ASC;
    374 	else
    375 		via2_reg(rIER) = 0x80 | V2IF_ASC;
    376 	splx(s);
    377 }
    378 
    379 
    380 /*ARGSUSED*/
    381 static void
    382 asc_intr(arg)
    383         void *arg;
    384 {
    385 #ifdef ASC_DEBUG
    386 	struct asc_softc *sc = (struct asc_softc *)arg;
    387 
    388 	if (asc_debug)
    389 		printf("asc_intr(%p)\n", sc);
    390 #endif
    391 }
    392