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