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