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