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