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