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