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