Home | History | Annotate | Line # | Download | only in obio
      1 /*	$NetBSD: asc.c,v 1.60 2025/04/09 13:09:49 nat 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/cdefs.h>
     67 __KERNEL_RCSID(0, "$NetBSD: asc.c,v 1.60 2025/04/09 13:09:49 nat Exp $");
     68 
     69 #include <sys/types.h>
     70 #include <sys/errno.h>
     71 #include <sys/time.h>
     72 #include <sys/systm.h>
     73 #include <sys/param.h>
     74 #include <sys/device.h>
     75 #include <sys/conf.h>
     76 
     77 #include <uvm/uvm_extern.h>
     78 
     79 #include <machine/autoconf.h>
     80 #include <machine/cpu.h>
     81 #include <machine/bus.h>
     82 #include <machine/viareg.h>
     83 
     84 #include <mac68k/obio/ascreg.h>
     85 #include <mac68k/obio/ascvar.h>
     86 #include <mac68k/obio/obiovar.h>
     87 
     88 #define	MAC68K_ASC_BASE		0x50f14000
     89 #define	MAC68K_IIFX_ASC_BASE	0x50f10000
     90 #define	MAC68K_ASC_LEN		0x01000
     91 
     92 #ifdef DEBUG
     93 #define ASC_DEBUG
     94 #endif
     95 
     96 #ifdef ASC_DEBUG
     97 int	asc_debug = 0;		/* non-zero enables debugging output */
     98 #endif
     99 
    100 static u_int8_t		asc_wave_tab[0x800];
    101 
    102 static int	asc_ring_bell(void *, int, int, int);
    103 static void	asc_stop_bell(void *);
    104 #if __notyet__
    105 static void	asc_intr_enable(void);
    106 static void	asc_intr(void *);
    107 #endif
    108 
    109 static int	ascmatch(device_t, cfdata_t, void *);
    110 static void	ascattach(device_t, device_t, void *);
    111 
    112 CFATTACH_DECL_NEW(asc, sizeof(struct asc_softc),
    113     ascmatch, ascattach, NULL, NULL);
    114 
    115 extern struct cfdriver asc_cd;
    116 
    117 dev_type_open(ascopen);
    118 dev_type_close(ascclose);
    119 dev_type_read(ascread);
    120 dev_type_write(ascwrite);
    121 dev_type_ioctl(ascioctl);
    122 dev_type_mmap(ascmmap);
    123 
    124 const struct cdevsw asc_cdevsw = {
    125 	.d_open = ascopen,
    126 	.d_close = ascclose,
    127 	.d_read = ascread,
    128 	.d_write = ascwrite,
    129 	.d_ioctl = ascioctl,
    130 	.d_stop = nostop,
    131 	.d_tty = notty,
    132 	.d_poll = nopoll,
    133 	.d_mmap = ascmmap,
    134 	.d_kqfilter = nokqfilter,
    135 	.d_discard = nodiscard,
    136 	.d_flag = 0
    137 };
    138 
    139 static const uint8_t easc_version_tab[] = { EASC_VER, EASC_VER2 };
    140 
    141 static int
    142 ascmatch(device_t parent, cfdata_t cf, void *aux)
    143 {
    144 	struct obio_attach_args *oa = (struct obio_attach_args *)aux;
    145 	bus_addr_t addr;
    146 	bus_space_handle_t bsh;
    147 	int rval = 0;
    148 	uint8_t ver;
    149 
    150 	if (oa->oa_addr != (-1))
    151 		addr = (bus_addr_t)oa->oa_addr;
    152 	else if (current_mac_model->machineid == MACH_MACTV)
    153 		return 0;
    154 	else if (current_mac_model->machineid == MACH_MACIIFX)
    155 		addr = (bus_addr_t)MAC68K_IIFX_ASC_BASE;
    156 	else
    157 		addr = (bus_addr_t)MAC68K_ASC_BASE;
    158 
    159 	if (bus_space_map(oa->oa_tag, addr, MAC68K_ASC_LEN, 0, &bsh))
    160 		return (0);
    161 
    162 	if (mac68k_bus_space_probe(oa->oa_tag, bsh, 0, 1)) {
    163 		rval = 1;
    164 
    165 		/*
    166 		 * Enhanced Apple Sound Chip (EASC) does not support wavetable
    167 		 * mode, exclude it for now.
    168 		 */
    169 		ver = bus_space_read_1(oa->oa_tag, bsh, VERLOC);
    170 		for (size_t i = 0; i < __arraycount(easc_version_tab); i++)
    171 			if (ver == easc_version_tab[i]) {
    172 				rval = 0;
    173 				break;
    174 			}
    175 	} else
    176 		rval = 0;
    177 
    178 	bus_space_unmap(oa->oa_tag, bsh, MAC68K_ASC_LEN);
    179 
    180 	return rval;
    181 }
    182 
    183 static void
    184 ascattach(device_t parent, device_t self, void *aux)
    185 {
    186 	struct asc_softc *sc = device_private(self);
    187 	struct obio_attach_args *oa = (struct obio_attach_args *)aux;
    188 	bus_addr_t addr;
    189 	int i;
    190 
    191 	sc->sc_dev = self;
    192 	sc->sc_tag = oa->oa_tag;
    193 	if (oa->oa_addr != (-1))
    194 		addr = (bus_addr_t)oa->oa_addr;
    195 	else if (current_mac_model->machineid == MACH_MACIIFX)
    196 		addr = (bus_addr_t)MAC68K_IIFX_ASC_BASE;
    197 	else
    198 		addr = (bus_addr_t)MAC68K_ASC_BASE;
    199 	if (bus_space_map(sc->sc_tag, addr, MAC68K_ASC_LEN, 0,
    200 	    &sc->sc_handle)) {
    201 		printf(": can't map memory space\n");
    202 		return;
    203 	}
    204 	sc->sc_open = 0;
    205 	sc->sc_ringing = 0;
    206 	callout_init(&sc->sc_bell_ch, 0);
    207 
    208 	for (i = 0; i < 256; i++) {	/* up part of wave, four voices? */
    209 		asc_wave_tab[i] = i / 4;
    210 		asc_wave_tab[i + 512] = i / 4;
    211 		asc_wave_tab[i + 1024] = i / 4;
    212 		asc_wave_tab[i + 1536] = i / 4;
    213 	}
    214 	for (i = 0; i < 256; i++) {	/* down part of wave, four voices? */
    215 		asc_wave_tab[i + 256] = 0x3f - (i / 4);
    216 		asc_wave_tab[i + 768] = 0x3f - (i / 4);
    217 		asc_wave_tab[i + 1280] = 0x3f - (i / 4);
    218 		asc_wave_tab[i + 1792] = 0x3f - (i / 4);
    219 	}
    220 
    221 	printf(": Apple Sound Chip");
    222 	if (oa->oa_addr != (-1))
    223 		printf(" at %x", oa->oa_addr);
    224 	printf("\n");
    225 
    226 	mac68k_set_bell_callback(asc_ring_bell, sc);
    227 #if __notyet__
    228 	if (mac68k_machine.aux_interrupts) {
    229 		intr_establish((int (*)(void *))asc_intr, sc, 5);
    230 	} else {
    231 		via2_register_irq(VIA2_ASC, asc_intr, sc);
    232 	}
    233 	asc_intr_enable();
    234 #endif
    235 }
    236 
    237 int
    238 ascopen(dev_t dev, int flag, int mode, struct lwp *l)
    239 {
    240 	struct asc_softc *sc;
    241 
    242 	sc = device_lookup_private(&asc_cd, ASCUNIT(dev));
    243 	if (sc == NULL)
    244 		return (ENXIO);
    245 	if (sc->sc_open)
    246 		return (EBUSY);
    247 	sc->sc_open = 1;
    248 
    249 	return (0);
    250 }
    251 
    252 int
    253 ascclose(dev_t dev, int flag, int mode, struct lwp *l)
    254 {
    255 	struct asc_softc *sc;
    256 
    257 	sc = device_lookup_private(&asc_cd, ASCUNIT(dev));
    258 	sc->sc_open = 0;
    259 
    260 	return (0);
    261 }
    262 
    263 int
    264 ascread(dev_t dev, struct uio *uio, int ioflag)
    265 {
    266 	return (ENXIO);
    267 }
    268 
    269 int
    270 ascwrite(dev_t dev, struct uio *uio, int ioflag)
    271 {
    272 	return (ENXIO);
    273 }
    274 
    275 int
    276 ascioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    277 {
    278 	int error;
    279 #ifdef not_yet
    280 	struct asc_softc *sc;
    281 	int unit = ASCUNIT(dev);
    282 
    283 	sc = device_lookup_private(&asc_cd, unit);
    284 #endif
    285 	error = 0;
    286 
    287 	switch (cmd) {
    288 	default:
    289 		error = EINVAL;
    290 		break;
    291 	}
    292 	return (error);
    293 }
    294 
    295 paddr_t
    296 ascmmap(dev_t dev, off_t off, int prot)
    297 {
    298 	struct asc_softc *sc;
    299 	paddr_t pa;
    300 
    301 	sc = device_lookup_private(&asc_cd, ASCUNIT(dev));
    302 	if ((u_int)off < MAC68K_ASC_LEN) {
    303 		(void) pmap_extract(pmap_kernel(), (vaddr_t)sc->sc_handle.base,
    304 		    &pa);
    305 		return m68k_btop(pa + off);
    306 	}
    307 
    308 	return (-1);
    309 }
    310 
    311 static int
    312 asc_ring_bell(void *arg, int freq, int length, int volume)
    313 {
    314 	struct asc_softc *sc = (struct asc_softc *)arg;
    315 	unsigned long cfreq;
    316 	int i;
    317 
    318 	if (!sc)
    319 		return (ENODEV);
    320 
    321 	if (sc->sc_ringing == 0) {
    322 
    323 		bus_space_write_multi_1(sc->sc_tag, sc->sc_handle,
    324 		    0, 0, 0x800);
    325 		bus_space_write_region_1(sc->sc_tag, sc->sc_handle,
    326 		    0, asc_wave_tab, 0x800);
    327 
    328 		/* Fix this.  Need to find exact ASC sampling freq */
    329 		cfreq = 65536 * freq / 466;
    330 
    331 		/* printf("beep: from %d, %02x %02x %02x %02x\n",
    332 		 * cur_beep.freq, (cfreq >> 24) & 0xff, (cfreq >> 16) & 0xff,
    333 		 * (cfreq >> 8) & 0xff, (cfreq) & 0xff); */
    334 		for (i = 0; i < 8; i++) {
    335 			bus_space_write_1(sc->sc_tag, sc->sc_handle,
    336 			    0x814 + 8 * i, (cfreq >> 24) & 0xff);
    337 			bus_space_write_1(sc->sc_tag, sc->sc_handle,
    338 			    0x815 + 8 * i, (cfreq >> 16) & 0xff);
    339 			bus_space_write_1(sc->sc_tag, sc->sc_handle,
    340 			    0x816 + 8 * i, (cfreq >> 8) & 0xff);
    341 			bus_space_write_1(sc->sc_tag, sc->sc_handle,
    342 			    0x817 + 8 * i, (cfreq) & 0xff);
    343 		}		/* frequency; should put cur_beep.freq in here
    344 				 * somewhere. */
    345 
    346 		bus_space_write_1(sc->sc_tag, sc->sc_handle, ASCRATE, F44KHZ);
    347 		bus_space_write_1(sc->sc_tag, sc->sc_handle, INTVOL,
    348 		    255 * volume / 100);
    349 		bus_space_write_1(sc->sc_tag, sc->sc_handle, 0x805, 0);
    350 		bus_space_write_1(sc->sc_tag, sc->sc_handle, ASCTEST, 0);
    351 		bus_space_write_1(sc->sc_tag, sc->sc_handle, ASCTRL, 2);
    352 		/* XXX NS 2 is identified in ascreg.h as STEREO, sampled */
    353 		bus_space_write_1(sc->sc_tag, sc->sc_handle, ASCMODE,
    354 		    MODEWAVE); /* enable sampled */
    355 		sc->sc_ringing = 1;
    356 		callout_reset(&sc->sc_bell_ch, length, asc_stop_bell, sc);
    357 	}
    358 
    359 	return (0);
    360 }
    361 
    362 static void
    363 asc_stop_bell(void *arg)
    364 {
    365 	struct asc_softc *sc = (struct asc_softc *)arg;
    366 
    367 	if (!sc)
    368 		return;
    369 
    370 	if (sc->sc_ringing > 1000 || sc->sc_ringing < 0)
    371 		panic("bell got out of sync?");
    372 
    373 	if (--sc->sc_ringing == 0) {	/* disable ASC */
    374 		bus_space_write_1(sc->sc_tag, sc->sc_handle, ASCMODE,
    375 		    MODESTOP);
    376 	}
    377 }
    378 
    379 #if __notyet__
    380 static void
    381 asc_intr_enable(void)
    382 {
    383 	int s;
    384 
    385 	s = splhigh();
    386 	if (VIA2 == VIA2OFF)
    387 		via2_reg(vIER) = 0x80 | V2IF_ASC;
    388 	else
    389 		via2_reg(rIER) = 0x80 | V2IF_ASC;
    390 	splx(s);
    391 }
    392 
    393 /*ARGSUSED*/
    394 static void
    395 asc_intr(void *arg)
    396 {
    397 #ifdef ASC_DEBUG
    398 	struct asc_softc *sc = (struct asc_softc *)arg;
    399 
    400 	if (asc_debug)
    401 		printf("asc_intr(%p)\n", sc);
    402 #endif
    403 }
    404 #endif
    405