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