xref: /openbsd/sys/dev/isa/gus_isa.c (revision d6d5e30b)
1 /*	$OpenBSD: gus_isa.c,v 1.10 2024/05/28 09:27:08 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 "gusreg.h"
119 #include "gusvar.h"
120 
121 /*
122  * ISA bus driver routines
123  */
124 
125 int	gus_isa_match(struct device *, void *, void *);
126 void	gus_isa_attach(struct device *, struct device *, void *);
127 
128 const struct cfattach gus_isa_ca = {
129 	sizeof(struct gus_softc), gus_isa_match, gus_isa_attach,
130 };
131 
132 int
gus_isa_match(struct device * parent,void * match,void * aux)133 gus_isa_match(struct device *parent, void *match, void *aux)
134 {
135 	struct isa_attach_args *ia = aux;
136 	int iobase = ia->ia_iobase;
137 	int recdrq = ia->ia_drq2;
138 
139 	/*
140 	 * Before we do anything else, make sure requested IRQ and DRQ are
141 	 * valid for this card.
142 	 */
143 
144 	/* XXX range check before indexing!! */
145 	if (ia->ia_irq == IRQUNK || gus_irq_map[ia->ia_irq] == IRQUNK) {
146 		DPRINTF(("gus: invalid irq %d, card not probed\n", ia->ia_irq));
147 		return 0;
148 	}
149 
150 	if (ia->ia_drq == DRQUNK || gus_drq_map[ia->ia_drq] == DRQUNK) {
151 		DPRINTF(("gus: invalid drq %d, card not probed\n", ia->ia_drq));
152 		return 0;
153 	}
154 
155 	if (recdrq != DRQUNK) {
156 		if (recdrq > 7 || gus_drq_map[recdrq] == DRQUNK) {
157 		   DPRINTF(("gus: invalid second DMA channel (%d), card not probed\n", recdrq));
158 		   return 0;
159 	        }
160 	} else
161 		recdrq = ia->ia_drq;
162 
163 	if (iobase == IOBASEUNK) {
164 		int i;
165 		for(i = 0; i < gus_addrs; i++)
166 			if (gus_test_iobase(ia->ia_iot, gus_base_addrs[i])) {
167 				iobase = gus_base_addrs[i];
168 				goto done;
169 			}
170 		return 0;
171 	} else if (!gus_test_iobase(ia->ia_iot, iobase))
172 			return 0;
173 
174 done:
175 	if ((ia->ia_drq    != -1 && !isa_drq_isfree(parent, ia->ia_drq)) ||
176 	    (recdrq != -1 && !isa_drq_isfree(parent, recdrq)))
177 		return 0;
178 
179 	ia->ia_iobase = iobase;
180 	ia->ia_iosize = GUS_NPORT1;
181 	return 1;
182 }
183 
184 
185 /*
186  * Setup the GUS for use; called shortly after probe
187  */
188 
189 void
gus_isa_attach(struct device * parent,struct device * self,void * aux)190 gus_isa_attach(struct device *parent, struct device *self, void *aux)
191 {
192 	struct gus_softc *sc = (void *) self;
193 	struct isa_attach_args *ia = aux;
194 
195 	sc->sc_iot = ia->ia_iot;
196 	sc->sc_iobase = ia->ia_iobase;
197 
198 	/* Map i/o space */
199 	if (bus_space_map(sc->sc_iot, sc->sc_iobase, GUS_NPORT1, 0, &sc->sc_ioh1))
200 		panic("%s: can't map io port range 1", self->dv_xname);
201 	if (bus_space_map(sc->sc_iot, sc->sc_iobase+GUS_IOH2_OFFSET, GUS_NPORT2, 0,
202 	    &sc->sc_ioh2))
203 		panic("%s: can't map io port range 2", self->dv_xname);
204 
205 	/* XXX Maybe we shouldn't fail on mapping this, but just assume
206 	 * the card is of revision 0? */
207 	if (bus_space_map(sc->sc_iot, sc->sc_iobase+GUS_IOH3_OFFSET, GUS_NPORT3, 0,
208 	    &sc->sc_ioh3))
209 		panic("%s: can't map io port range 3", self->dv_xname);
210 
211 	if (bus_space_map(sc->sc_iot, sc->sc_iobase+GUS_IOH4_OFFSET, GUS_NPORT4, 0,
212 	    &sc->sc_ioh4))
213 		panic("%s: can't map io port range 4", self->dv_xname);
214 
215 	sc->sc_irq = ia->ia_irq;
216 	sc->sc_drq = ia->ia_drq;
217 	sc->sc_recdrq = ia->ia_drq2;
218 	sc->sc_isa = parent;
219 
220 	gus_subattach(sc, aux);
221 }
222