Home | History | Annotate | Line # | Download | only in isa
wss_isa.c revision 1.1
      1  1.1  augustss /*	$NetBSD: wss_isa.c,v 1.1 1998/01/19 22:18:24 augustss Exp $	*/
      2  1.1  augustss 
      3  1.1  augustss /*
      4  1.1  augustss  * Copyright (c) 1994 John Brezak
      5  1.1  augustss  * Copyright (c) 1991-1993 Regents of the University of California.
      6  1.1  augustss  * All rights reserved.
      7  1.1  augustss  *
      8  1.1  augustss  * MAD support:
      9  1.1  augustss  * Copyright (c) 1996 Lennart Augustsson
     10  1.1  augustss  * Based on code which is
     11  1.1  augustss  * Copyright (c) 1994 Hannu Savolainen
     12  1.1  augustss  *
     13  1.1  augustss  * Redistribution and use in source and binary forms, with or without
     14  1.1  augustss  * modification, are permitted provided that the following conditions
     15  1.1  augustss  * are met:
     16  1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     17  1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     18  1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     19  1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     20  1.1  augustss  *    documentation and/or other materials provided with the distribution.
     21  1.1  augustss  * 3. All advertising materials mentioning features or use of this software
     22  1.1  augustss  *    must display the following acknowledgement:
     23  1.1  augustss  *	This product includes software developed by the Computer Systems
     24  1.1  augustss  *	Engineering Group at Lawrence Berkeley Laboratory.
     25  1.1  augustss  * 4. Neither the name of the University nor of the Laboratory may be used
     26  1.1  augustss  *    to endorse or promote products derived from this software without
     27  1.1  augustss  *    specific prior written permission.
     28  1.1  augustss  *
     29  1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     30  1.1  augustss  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     31  1.1  augustss  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     32  1.1  augustss  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     33  1.1  augustss  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     34  1.1  augustss  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     35  1.1  augustss  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     36  1.1  augustss  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     37  1.1  augustss  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     38  1.1  augustss  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     39  1.1  augustss  * SUCH DAMAGE.
     40  1.1  augustss  *
     41  1.1  augustss  */
     42  1.1  augustss #include <sys/param.h>
     43  1.1  augustss #include <sys/systm.h>
     44  1.1  augustss #include <sys/errno.h>
     45  1.1  augustss #include <sys/ioctl.h>
     46  1.1  augustss #include <sys/syslog.h>
     47  1.1  augustss #include <sys/device.h>
     48  1.1  augustss #include <sys/proc.h>
     49  1.1  augustss #include <sys/buf.h>
     50  1.1  augustss 
     51  1.1  augustss #include <machine/cpu.h>
     52  1.1  augustss #include <machine/intr.h>
     53  1.1  augustss #include <machine/bus.h>
     54  1.1  augustss #include <machine/pio.h>
     55  1.1  augustss 
     56  1.1  augustss #include <sys/audioio.h>
     57  1.1  augustss #include <dev/audio_if.h>
     58  1.1  augustss 
     59  1.1  augustss #include <dev/isa/isavar.h>
     60  1.1  augustss #include <dev/isa/isadmavar.h>
     61  1.1  augustss 
     62  1.1  augustss #include <dev/ic/ad1848reg.h>
     63  1.1  augustss #include <dev/isa/ad1848var.h>
     64  1.1  augustss #include <dev/isa/wssreg.h>
     65  1.1  augustss #include <dev/isa/wssvar.h>
     66  1.1  augustss #include <dev/isa/madreg.h>
     67  1.1  augustss 
     68  1.1  augustss #ifdef AUDIO_DEBUG
     69  1.1  augustss #define DPRINTF(x)	if (wssdebug) printf x
     70  1.1  augustss extern int	wssdebug;
     71  1.1  augustss #else
     72  1.1  augustss #define DPRINTF(x)
     73  1.1  augustss #endif
     74  1.1  augustss 
     75  1.1  augustss static int	wssfind __P((struct device *, struct wss_softc *, struct isa_attach_args *));
     76  1.1  augustss 
     77  1.1  augustss static void	madprobe __P((struct wss_softc *, int));
     78  1.1  augustss static void	madunmap __P((struct wss_softc *));
     79  1.1  augustss static int	detect_mad16 __P((struct wss_softc *, int));
     80  1.1  augustss 
     81  1.1  augustss int		wss_isa_probe __P((struct device *, void *, void *));
     82  1.1  augustss void		wss_isa_attach __P((struct device *, struct device *, void *));
     83  1.1  augustss 
     84  1.1  augustss struct cfattach wss_isa_ca = {
     85  1.1  augustss 	sizeof(struct wss_softc), wss_isa_probe, wss_isa_attach
     86  1.1  augustss };
     87  1.1  augustss 
     88  1.1  augustss /*
     89  1.1  augustss  * Probe for the Microsoft Sound System hardware.
     90  1.1  augustss  */
     91  1.1  augustss int
     92  1.1  augustss wss_isa_probe(parent, match, aux)
     93  1.1  augustss     struct device *parent;
     94  1.1  augustss #ifdef __BROKEN_INDIRECT_CONFIG
     95  1.1  augustss     void *match;
     96  1.1  augustss #else
     97  1.1  augustss     struct cfdata *match;
     98  1.1  augustss #endif
     99  1.1  augustss     void *aux;
    100  1.1  augustss {
    101  1.1  augustss     struct wss_softc probesc, *sc = &probesc;
    102  1.1  augustss 
    103  1.1  augustss     bzero(sc, sizeof *sc);
    104  1.1  augustss #ifdef __BROKEN_INDIRECT_CONFIG
    105  1.1  augustss     sc->sc_dev.dv_cfdata = ((struct device *)match)->dv_cfdata;
    106  1.1  augustss #else
    107  1.1  augustss     sc->sc_dev.dv_cfdata = match;
    108  1.1  augustss #endif
    109  1.1  augustss     if (wssfind(parent, sc, aux)) {
    110  1.1  augustss         bus_space_unmap(sc->sc_iot, sc->sc_ioh, WSS_CODEC);
    111  1.1  augustss         ad1848_unmap(&sc->sc_ad1848);
    112  1.1  augustss         madunmap(sc);
    113  1.1  augustss         return 1;
    114  1.1  augustss     } else
    115  1.1  augustss         /* Everything is already unmapped */
    116  1.1  augustss         return 0;
    117  1.1  augustss }
    118  1.1  augustss 
    119  1.1  augustss static int
    120  1.1  augustss wssfind(parent, sc, ia)
    121  1.1  augustss     struct device *parent;
    122  1.1  augustss     struct wss_softc *sc;
    123  1.1  augustss     struct isa_attach_args *ia;
    124  1.1  augustss {
    125  1.1  augustss     static u_char interrupt_bits[12] = {
    126  1.1  augustss 	-1, -1, -1, -1, -1, -1, -1, 0x08, -1, 0x10, 0x18, 0x20
    127  1.1  augustss     };
    128  1.1  augustss     static u_char dma_bits[4] = {1, 2, 0, 3};
    129  1.1  augustss 
    130  1.1  augustss     sc->sc_iot = ia->ia_iot;
    131  1.1  augustss     if (sc->sc_dev.dv_cfdata->cf_flags & 1)
    132  1.1  augustss 	madprobe(sc, ia->ia_iobase);
    133  1.1  augustss     else
    134  1.1  augustss 	sc->mad_chip_type = MAD_NONE;
    135  1.1  augustss 
    136  1.1  augustss     if (!WSS_BASE_VALID(ia->ia_iobase)) {
    137  1.1  augustss 	DPRINTF(("wss: configured iobase %x invalid\n", ia->ia_iobase));
    138  1.1  augustss 	goto bad1;
    139  1.1  augustss     }
    140  1.1  augustss 
    141  1.1  augustss     /* Map the ports upto the AD1848 port */
    142  1.1  augustss     if (bus_space_map(sc->sc_iot, ia->ia_iobase, WSS_CODEC, 0, &sc->sc_ioh))
    143  1.1  augustss 	goto bad1;
    144  1.1  augustss 
    145  1.1  augustss     sc->sc_ad1848.sc_iot = sc->sc_iot;
    146  1.1  augustss 
    147  1.1  augustss     /* Is there an ad1848 chip at (WSS iobase + WSS_CODEC)? */
    148  1.1  augustss     if (ad1848_mapprobe(&sc->sc_ad1848, ia->ia_iobase + WSS_CODEC) == 0)
    149  1.1  augustss 	goto bad;
    150  1.1  augustss 
    151  1.1  augustss     ia->ia_iosize = WSS_NPORT;
    152  1.1  augustss 
    153  1.1  augustss     /* Setup WSS interrupt and DMA */
    154  1.1  augustss     if (!WSS_DRQ_VALID(ia->ia_drq)) {
    155  1.1  augustss 	DPRINTF(("wss: configured dma chan %d invalid\n", ia->ia_drq));
    156  1.1  augustss 	goto bad;
    157  1.1  augustss     }
    158  1.1  augustss     sc->wss_drq = ia->ia_drq;
    159  1.1  augustss 
    160  1.1  augustss     if (sc->wss_drq != DRQUNK && !isa_drq_isfree(parent, sc->wss_drq))
    161  1.1  augustss 	    goto bad;
    162  1.1  augustss 
    163  1.1  augustss     if (!WSS_IRQ_VALID(ia->ia_irq)) {
    164  1.1  augustss 	DPRINTF(("wss: configured interrupt %d invalid\n", ia->ia_irq));
    165  1.1  augustss 	goto bad;
    166  1.1  augustss     }
    167  1.1  augustss 
    168  1.1  augustss     sc->wss_irq = ia->ia_irq;
    169  1.1  augustss 
    170  1.1  augustss     if (sc->sc_ad1848.mode <= 1)
    171  1.1  augustss 	ia->ia_drq2 = DRQUNK;
    172  1.1  augustss     sc->wss_recdrq =
    173  1.1  augustss 	sc->sc_ad1848.mode > 1 && ia->ia_drq2 != DRQUNK ?
    174  1.1  augustss 	ia->ia_drq2 : ia->ia_drq;
    175  1.1  augustss     if (sc->wss_recdrq != sc->wss_drq && !isa_drq_isfree(parent, sc->wss_recdrq))
    176  1.1  augustss 	goto bad;
    177  1.1  augustss 
    178  1.1  augustss     /* XXX recdrq */
    179  1.1  augustss     bus_space_write_1(sc->sc_iot, sc->sc_ioh, WSS_CONFIG,
    180  1.1  augustss 		      (interrupt_bits[ia->ia_irq] | dma_bits[ia->ia_drq]));
    181  1.1  augustss 
    182  1.1  augustss     return 1;
    183  1.1  augustss 
    184  1.1  augustss bad:
    185  1.1  augustss     bus_space_unmap(sc->sc_iot, sc->sc_ioh, WSS_CODEC);
    186  1.1  augustss bad1:
    187  1.1  augustss     madunmap(sc);
    188  1.1  augustss     return 0;
    189  1.1  augustss }
    190  1.1  augustss 
    191  1.1  augustss /*
    192  1.1  augustss  * Attach hardware to driver, attach hardware driver to audio
    193  1.1  augustss  * pseudo-device driver .
    194  1.1  augustss  */
    195  1.1  augustss void
    196  1.1  augustss wss_isa_attach(parent, self, aux)
    197  1.1  augustss     struct device *parent, *self;
    198  1.1  augustss     void *aux;
    199  1.1  augustss {
    200  1.1  augustss     struct wss_softc *sc = (struct wss_softc *)self;
    201  1.1  augustss     struct isa_attach_args *ia = (struct isa_attach_args *)aux;
    202  1.1  augustss 
    203  1.1  augustss     if (!wssfind(parent, sc, ia)) {
    204  1.1  augustss         printf("%s: wssfind failed\n", sc->sc_dev.dv_xname);
    205  1.1  augustss         return;
    206  1.1  augustss     }
    207  1.1  augustss 
    208  1.1  augustss     sc->sc_ic = ia->ia_ic;
    209  1.1  augustss     sc->sc_ad1848.sc_isa = parent;
    210  1.1  augustss 
    211  1.1  augustss     wssattach(sc);
    212  1.1  augustss }
    213  1.1  augustss 
    214  1.1  augustss /*
    215  1.1  augustss  * Copyright by Hannu Savolainen 1994
    216  1.1  augustss  *
    217  1.1  augustss  * Redistribution and use in source and binary forms, with or without
    218  1.1  augustss  * modification, are permitted provided that the following conditions are
    219  1.1  augustss  * met: 1. Redistributions of source code must retain the above copyright
    220  1.1  augustss  * notice, this list of conditions and the following disclaimer. 2.
    221  1.1  augustss  * Redistributions in binary form must reproduce the above copyright notice,
    222  1.1  augustss  * this list of conditions and the following disclaimer in the documentation
    223  1.1  augustss  * and/or other materials provided with the distribution.
    224  1.1  augustss  *
    225  1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
    226  1.1  augustss  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    227  1.1  augustss  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    228  1.1  augustss  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
    229  1.1  augustss  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    230  1.1  augustss  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    231  1.1  augustss  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    232  1.1  augustss  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    233  1.1  augustss  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    234  1.1  augustss  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    235  1.1  augustss  * SUCH DAMAGE.
    236  1.1  augustss  *
    237  1.1  augustss  */
    238  1.1  augustss 
    239  1.1  augustss /*
    240  1.1  augustss  * Initialization code for OPTi MAD16 compatible audio chips. Including
    241  1.1  augustss  *
    242  1.1  augustss  *      OPTi 82C928     MAD16           (replaced by C929)
    243  1.1  augustss  *      OAK OTI-601D    Mozart
    244  1.1  augustss  *      OPTi 82C929     MAD16 Pro
    245  1.1  augustss  *	OPTi 82C931
    246  1.1  augustss  */
    247  1.1  augustss 
    248  1.1  augustss static int
    249  1.1  augustss detect_mad16(sc, chip_type)
    250  1.1  augustss     struct wss_softc *sc;
    251  1.1  augustss     int chip_type;
    252  1.1  augustss {
    253  1.1  augustss     unsigned char tmp, tmp2;
    254  1.1  augustss 
    255  1.1  augustss     sc->mad_chip_type = chip_type;
    256  1.1  augustss     /*
    257  1.1  augustss      * Check that reading a register doesn't return bus float (0xff)
    258  1.1  augustss      * when the card is accessed using password. This may fail in case
    259  1.1  augustss      * the card is in low power mode. Normally at least the power saving mode
    260  1.1  augustss      * bit should be 0.
    261  1.1  augustss      */
    262  1.1  augustss     if ((tmp = mad_read(sc, MC1_PORT)) == 0xff) {
    263  1.1  augustss 	DPRINTF(("MC1_PORT returned 0xff\n"));
    264  1.1  augustss 	return 0;
    265  1.1  augustss     }
    266  1.1  augustss 
    267  1.1  augustss     /*
    268  1.1  augustss      * Now check that the gate is closed on first I/O after writing
    269  1.1  augustss      * the password. (This is how a MAD16 compatible card works).
    270  1.1  augustss      */
    271  1.1  augustss     if ((tmp2 = bus_space_read_1(sc->sc_iot, sc->mad_ioh, MC1_PORT)) == tmp)	{
    272  1.1  augustss 	DPRINTF(("MC1_PORT didn't close after read (0x%02x)\n", tmp2));
    273  1.1  augustss 	return 0;
    274  1.1  augustss     }
    275  1.1  augustss 
    276  1.1  augustss     mad_write(sc, MC1_PORT, tmp ^ 0x80);	/* Toggle a bit */
    277  1.1  augustss 
    278  1.1  augustss     /* Compare the bit */
    279  1.1  augustss     if ((tmp2 = mad_read(sc, MC1_PORT)) != (tmp ^ 0x80)) {
    280  1.1  augustss 	mad_write(sc, MC1_PORT, tmp);	/* Restore */
    281  1.1  augustss 	DPRINTF(("Bit revert test failed (0x%02x, 0x%02x)\n", tmp, tmp2));
    282  1.1  augustss 	return 0;
    283  1.1  augustss     }
    284  1.1  augustss 
    285  1.1  augustss     mad_write(sc, MC1_PORT, tmp);	/* Restore */
    286  1.1  augustss     return 1;
    287  1.1  augustss }
    288  1.1  augustss 
    289  1.1  augustss static void
    290  1.1  augustss madprobe(sc, iobase)
    291  1.1  augustss     struct wss_softc *sc;
    292  1.1  augustss     int iobase;
    293  1.1  augustss {
    294  1.1  augustss     static int valid_ports[M_WSS_NPORTS] =
    295  1.1  augustss         { M_WSS_PORT0, M_WSS_PORT1, M_WSS_PORT2, M_WSS_PORT3 };
    296  1.1  augustss     int i;
    297  1.1  augustss 
    298  1.1  augustss     /* Allocate bus space that the MAD chip wants */
    299  1.1  augustss     if (bus_space_map(sc->sc_iot, MAD_BASE, MAD_NPORT, 0, &sc->mad_ioh))
    300  1.1  augustss 	goto bad0;
    301  1.1  augustss     if (bus_space_map(sc->sc_iot, MAD_REG1, MAD_LEN1, 0, &sc->mad_ioh1))
    302  1.1  augustss         goto bad1;
    303  1.1  augustss     if (bus_space_map(sc->sc_iot, MAD_REG2, MAD_LEN2, 0, &sc->mad_ioh2))
    304  1.1  augustss         goto bad2;
    305  1.1  augustss     if (bus_space_map(sc->sc_iot, MAD_REG3, MAD_LEN3, 0, &sc->mad_ioh3))
    306  1.1  augustss         goto bad3;
    307  1.1  augustss 
    308  1.1  augustss     DPRINTF(("mad: Detect using password = 0xE2\n"));
    309  1.1  augustss     if (!detect_mad16(sc, MAD_82C928)) {
    310  1.1  augustss 	/* No luck. Try different model */
    311  1.1  augustss 	DPRINTF(("mad: Detect using password = 0xE3\n"));
    312  1.1  augustss 	if (!detect_mad16(sc, MAD_82C929))
    313  1.1  augustss 	    goto bad;
    314  1.1  augustss 	sc->mad_chip_type = MAD_82C929;
    315  1.1  augustss 	DPRINTF(("mad: 82C929 detected\n"));
    316  1.1  augustss     } else {
    317  1.1  augustss 	sc->mad_chip_type = MAD_82C928;
    318  1.1  augustss 	if ((mad_read(sc, MC3_PORT) & 0x03) == 0x03) {
    319  1.1  augustss 	    DPRINTF(("mad: Mozart detected\n"));
    320  1.1  augustss 	    sc->mad_chip_type = MAD_OTI601D;
    321  1.1  augustss 	} else {
    322  1.1  augustss 	    DPRINTF(("mad: 82C928 detected?\n"));
    323  1.1  augustss 	    sc->mad_chip_type = MAD_82C928;
    324  1.1  augustss 	}
    325  1.1  augustss     }
    326  1.1  augustss 
    327  1.1  augustss #ifdef AUDIO_DEBUG
    328  1.1  augustss     if (wssdebug)
    329  1.1  augustss 	for (i = MC1_PORT; i <= MC7_PORT; i++)
    330  1.1  augustss 	    printf("mad: port %03x = %02x\n", i, mad_read(sc, i));
    331  1.1  augustss #endif
    332  1.1  augustss 
    333  1.1  augustss     /* Set the WSS address. */
    334  1.1  augustss     for (i = 0; i < M_WSS_NPORTS; i++)
    335  1.1  augustss 	if (valid_ports[i] == iobase)
    336  1.1  augustss 	    break;
    337  1.1  augustss     if (i >= M_WSS_NPORTS) {		/* Not a valid port */
    338  1.1  augustss 	printf("mad: Bad WSS base address 0x%x\n", iobase);
    339  1.1  augustss 	goto bad;
    340  1.1  augustss     }
    341  1.1  augustss     sc->mad_ioindex = i;
    342  1.1  augustss     /* enable WSS emulation at the I/O port, no joystick */
    343  1.1  augustss     mad_write(sc, MC1_PORT, M_WSS_PORT_SELECT(i) | MC1_JOYDISABLE);
    344  1.1  augustss     mad_write(sc, MC2_PORT, 0x03); /* ? */
    345  1.1  augustss     mad_write(sc, MC3_PORT, 0xf0); /* Disable SB */
    346  1.1  augustss     return;
    347  1.1  augustss 
    348  1.1  augustss bad:
    349  1.1  augustss     bus_space_unmap(sc->sc_iot, sc->mad_ioh3, MAD_LEN3);
    350  1.1  augustss bad3:
    351  1.1  augustss     bus_space_unmap(sc->sc_iot, sc->mad_ioh2, MAD_LEN2);
    352  1.1  augustss bad2:
    353  1.1  augustss     bus_space_unmap(sc->sc_iot, sc->mad_ioh1, MAD_LEN1);
    354  1.1  augustss bad1:
    355  1.1  augustss     bus_space_unmap(sc->sc_iot, sc->mad_ioh, MAD_NPORT);
    356  1.1  augustss bad0:
    357  1.1  augustss     sc->mad_chip_type = MAD_NONE;
    358  1.1  augustss }
    359  1.1  augustss 
    360  1.1  augustss static void
    361  1.1  augustss madunmap(sc)
    362  1.1  augustss     struct wss_softc *sc;
    363  1.1  augustss {
    364  1.1  augustss     if (sc->mad_chip_type == MAD_NONE)
    365  1.1  augustss         return;
    366  1.1  augustss     bus_space_unmap(sc->sc_iot, sc->mad_ioh, MAD_NPORT);
    367  1.1  augustss     bus_space_unmap(sc->sc_iot, sc->mad_ioh1, MAD_LEN1);
    368  1.1  augustss     bus_space_unmap(sc->sc_iot, sc->mad_ioh2, MAD_LEN2);
    369  1.1  augustss     bus_space_unmap(sc->sc_iot, sc->mad_ioh3, MAD_LEN3);
    370  1.1  augustss }
    371