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