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