xref: /openbsd/sys/dev/isa/gus_isa.c (revision d485f761)
1 /*	$OpenBSD: gus_isa.c,v 1.2 2001/01/29 05:30:31 mickey Exp $	*/
2 /*	$NetBSD: gus.c,v 1.51 1998/01/25 23:48:06 mycroft Exp $	*/
3 
4 /*-
5  * Copyright (c) 1996 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Ken Hornstein and John Kohl.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *        This product includes software developed by the NetBSD
22  *	  Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  *
42  * TODO:
43  *	. figure out why mixer activity while sound is playing causes problems
44  *	  (phantom interrupts?)
45  *	. figure out a better deinterleave strategy that avoids sucking up
46  *	  CPU, memory and cache bandwidth.  (Maybe a special encoding?
47  *	  Maybe use the double-speed sampling/hardware deinterleave trick
48  *	  from the GUS SDK?)  A 486/33 isn't quite fast enough to keep
49  *	  up with 44.1kHz 16-bit stereo output without some drop-outs.
50  *	. use CS4231 for 16-bit sampling, for a-law and mu-law playback.
51  *	. actually test full-duplex sampling(recording) and playback.
52  */
53 
54 /*
55  * Gravis UltraSound driver
56  *
57  * For more detailed information, see the GUS developers' kit
58  * available on the net at:
59  *
60  * ftp://freedom.nmsu.edu/pub/ultrasound/gravis/util/
61  *	gusdkXXX.zip (developers' kit--get rev 2.22 or later)
62  *		See ultrawrd.doc inside--it's MS Word (ick), but it's the bible
63  *
64  */
65 
66 /*
67  * The GUS Max has a slightly strange set of connections between the CS4231
68  * and the GF1 and the DMA interconnects.  It's set up so that the CS4231 can
69  * be playing while the GF1 is loading patches from the system.
70  *
71  * Here's a recreation of the DMA interconnect diagram:
72  *
73  *       GF1
74  *   +---------+				 digital
75  *   |         |  record			 ASIC
76  *   |         |--------------+
77  *   |         |              |		       +--------+
78  *   |         | play (dram)  |      +----+    |	|
79  *   |         |--------------(------|-\  |    |   +-+  |
80  *   +---------+              |      |  >-|----|---|C|--|------  dma chan 1
81  *                            |  +---|-/  |    |   +-+	|
82  *                            |  |   +----+    |    |   |
83  *                            |	 |   +----+    |    |   |
84  *   +---------+        +-+   +--(---|-\  |    |    |   |
85  *   |         | play   |8|      |   |  >-|----|----+---|------  dma chan 2
86  *   | ---C----|--------|/|------(---|-/  |    |        |
87  *   |    ^    |record  |1|      |   +----+    |	|
88  *   |    |    |   /----|6|------+	       +--------+
89  *   | ---+----|--/     +-+
90  *   +---------+
91  *     CS4231	8-to-16 bit bus conversion, if needed
92  *
93  *
94  * "C" is an optional combiner.
95  *
96  */
97 
98 #include <sys/param.h>
99 #include <sys/systm.h>
100 #include <sys/errno.h>
101 #include <sys/ioctl.h>
102 #include <sys/syslog.h>
103 #include <sys/device.h>
104 #include <sys/proc.h>
105 #include <sys/buf.h>
106 #include <sys/fcntl.h>
107 #include <sys/malloc.h>
108 #include <sys/kernel.h>
109 #include <sys/timeout.h>
110 
111 #include <machine/cpu.h>
112 #include <machine/intr.h>
113 #include <machine/bus.h>
114 #include <machine/cpufunc.h>
115 #include <sys/audioio.h>
116 #include <dev/audio_if.h>
117 #include <dev/mulaw.h>
118 #include <dev/auconv.h>
119 
120 #include <dev/isa/isavar.h>
121 #include <dev/isa/isadmavar.h>
122 #include <i386/isa/icu.h>
123 
124 #include <dev/ic/ics2101reg.h>
125 #include <dev/ic/cs4231reg.h>
126 #include <dev/ic/ad1848reg.h>
127 #include <dev/isa/ics2101var.h>
128 #include <dev/isa/ad1848var.h>
129 #include <dev/isa/cs4231var.h>
130 #include "gusreg.h"
131 #include "gusvar.h"
132 
133 /*
134  * ISA bus driver routines
135  */
136 
137 int	gus_isa_match __P((struct device *, void *, void *));
138 void	gus_isa_attach __P((struct device *, struct device *, void *));
139 
140 struct cfattach gus_isa_ca = {
141 	sizeof(struct gus_softc), gus_isa_match, gus_isa_attach,
142 };
143 
144 int
145 gus_isa_match(parent, match, aux)
146 	struct device *parent;
147 	void *match;
148 	void *aux;
149 {
150 	struct isa_attach_args *ia = aux;
151 	int iobase = ia->ia_iobase;
152 	int recdrq = ia->ia_drq2;
153 
154 	/*
155 	 * Before we do anything else, make sure requested IRQ and DRQ are
156 	 * valid for this card.
157 	 */
158 
159 	/* XXX range check before indexing!! */
160 	if (ia->ia_irq == IRQUNK || gus_irq_map[ia->ia_irq] == IRQUNK) {
161 		DPRINTF(("gus: invalid irq %d, card not probed\n", ia->ia_irq));
162 		return 0;
163 	}
164 
165 	if (ia->ia_drq == DRQUNK || gus_drq_map[ia->ia_drq] == DRQUNK) {
166 		DPRINTF(("gus: invalid drq %d, card not probed\n", ia->ia_drq));
167 		return 0;
168 	}
169 
170 	if (recdrq != DRQUNK) {
171 		if (recdrq > 7 || gus_drq_map[recdrq] == DRQUNK) {
172 		   DPRINTF(("gus: invalid second DMA channel (%d), card not probed\n", recdrq));
173 		   return 0;
174 	        }
175 	} else
176 		recdrq = ia->ia_drq;
177 
178 	if (iobase == IOBASEUNK) {
179 		int i;
180 		for(i = 0; i < gus_addrs; i++)
181 			if (gus_test_iobase(ia->ia_iot, gus_base_addrs[i])) {
182 				iobase = gus_base_addrs[i];
183 				goto done;
184 			}
185 		return 0;
186 	} else if (!gus_test_iobase(ia->ia_iot, iobase))
187 			return 0;
188 
189 done:
190 	if ((ia->ia_drq    != -1 && !isa_drq_isfree(parent, ia->ia_drq)) ||
191 	    (recdrq != -1 && !isa_drq_isfree(parent, recdrq)))
192 		return 0;
193 
194 	ia->ia_iobase = iobase;
195 	ia->ia_iosize = GUS_NPORT1;
196 	return 1;
197 }
198 
199 
200 /*
201  * Setup the GUS for use; called shortly after probe
202  */
203 
204 void
205 gus_isa_attach(parent, self, aux)
206 	struct device *parent, *self;
207 	void *aux;
208 {
209 	struct gus_softc *sc = (void *) self;
210 	struct isa_attach_args *ia = aux;
211 
212 	sc->sc_iot = ia->ia_iot;
213 	sc->sc_iobase = ia->ia_iobase;
214 
215 	/* Map i/o space */
216 	if (bus_space_map(sc->sc_iot, sc->sc_iobase, GUS_NPORT1, 0, &sc->sc_ioh1))
217 		panic("%s: can't map io port range 1", self->dv_xname);
218 	if (bus_space_map(sc->sc_iot, sc->sc_iobase+GUS_IOH2_OFFSET, GUS_NPORT2, 0,
219 	    &sc->sc_ioh2))
220 		panic("%s: can't map io port range 2", self->dv_xname);
221 
222 	/* XXX Maybe we shouldn't fail on mapping this, but just assume
223 	 * the card is of revision 0? */
224 	if (bus_space_map(sc->sc_iot, sc->sc_iobase+GUS_IOH3_OFFSET, GUS_NPORT3, 0,
225 	    &sc->sc_ioh3))
226 		panic("%s: can't map io port range 3", self->dv_xname);
227 
228 	if (bus_space_map(sc->sc_iot, sc->sc_iobase+GUS_IOH4_OFFSET, GUS_NPORT4, 0,
229 	    &sc->sc_ioh4))
230 		panic("%s: can't map io port range 4", self->dv_xname);
231 
232 	sc->sc_irq = ia->ia_irq;
233 	sc->sc_drq = ia->ia_drq;
234 	sc->sc_recdrq = ia->ia_drq2;
235 	sc->sc_isa = parent;
236 
237 	gus_subattach(sc, aux);
238 }
239