xref: /openbsd-src/sys/dev/isa/sb.c (revision f1dd7b858388b4a23f4f67a4957ec5ff656ebbe8)
1 /*	$OpenBSD: sb.c,v 1.30 2021/03/07 06:17:04 jsg Exp $	*/
2 /*	$NetBSD: sb.c,v 1.57 1998/01/12 09:43:46 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 1991-1993 Regents of the University of California.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the Computer Systems
19  *	Engineering Group at Lawrence Berkeley Laboratory.
20  * 4. Neither the name of the University nor of the Laboratory may be used
21  *    to endorse or promote products derived from this software without
22  *    specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  */
37 
38 #include "midi.h"
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/errno.h>
43 #include <sys/ioctl.h>
44 #include <sys/syslog.h>
45 #include <sys/device.h>
46 
47 #include <machine/cpu.h>
48 #include <machine/intr.h>
49 #include <machine/bus.h>
50 
51 #include <sys/audioio.h>
52 #include <dev/audio_if.h>
53 #include <dev/midi_if.h>
54 
55 #include <dev/isa/isavar.h>
56 #include <dev/isa/isadmavar.h>
57 
58 #include <dev/isa/sbreg.h>
59 #include <dev/isa/sbvar.h>
60 #include <dev/isa/sbdspvar.h>
61 
62 struct cfdriver sb_cd = {
63 	NULL, "sb", DV_DULL
64 };
65 
66 #if NMIDI > 0
67 int	sb_mpu401_open(void *, int, void (*iintr)(void *, int),
68 		       void (*ointr)(void *), void *arg);
69 void	sb_mpu401_close(void *);
70 int	sb_mpu401_output(void *, int);
71 void	sb_mpu401_getinfo(void *, struct midi_info *);
72 
73 struct midi_hw_if sb_midi_hw_if = {
74 	sbdsp_midi_open,
75 	sbdsp_midi_close,
76 	sbdsp_midi_output,
77 	0,			/* flush */
78 	sbdsp_midi_getinfo,
79 	0,			/* ioctl */
80 };
81 
82 struct midi_hw_if sb_mpu401_hw_if = {
83 	sb_mpu401_open,
84 	sb_mpu401_close,
85 	sb_mpu401_output,
86 	0,			/* flush */
87 	sb_mpu401_getinfo,
88 	0,			/* ioctl */
89 };
90 #endif
91 
92 /*
93  * Define our interface to the higher level audio driver.
94  */
95 
96 struct audio_hw_if sb_hw_if = {
97 	sbdsp_open,
98 	sbdsp_close,
99 	sbdsp_set_params,
100 	sbdsp_round_blocksize,
101 	0,
102 	0,
103 	0,
104 	0,
105 	0,
106 	sbdsp_haltdma,
107 	sbdsp_haltdma,
108 	sbdsp_speaker_ctl,
109 	0,
110 	sbdsp_mixer_set_port,
111 	sbdsp_mixer_get_port,
112 	sbdsp_mixer_query_devinfo,
113 	sb_malloc,
114 	sb_free,
115 	sb_round,
116 	sbdsp_get_props,
117 	sbdsp_trigger_output,
118 	sbdsp_trigger_input
119 };
120 
121 #ifdef AUDIO_DEBUG
122 #define DPRINTF(x)	if (sbdebug) printf x
123 int	sbdebug = 0;
124 #else
125 #define DPRINTF(x)
126 #endif
127 
128 /*
129  * Probe / attach routines.
130  */
131 
132 
133 int
134 sbmatch(struct sbdsp_softc *sc)
135 {
136 	static u_char drq_conf[8] = {
137 		0x01, 0x02, -1, 0x08, -1, 0x20, 0x40, 0x80
138 	};
139 
140 	static u_char irq_conf[11] = {
141 		-1, -1, 0x01, -1, -1, 0x02, -1, 0x04, -1, 0x01, 0x08
142 	};
143 
144 	if (sbdsp_probe(sc) == 0)
145 		return 0;
146 
147 	/*
148 	 * Cannot auto-discover DMA channel.
149 	 */
150 	if (ISSBPROCLASS(sc)) {
151 		if (!SBP_DRQ_VALID(sc->sc_drq8)) {
152 			DPRINTF(("%s: configured dma chan %d invalid\n",
153 			    sc->sc_dev.dv_xname, sc->sc_drq8));
154 			return 0;
155 		}
156 	} else {
157 		if (!SB_DRQ_VALID(sc->sc_drq8)) {
158 			DPRINTF(("%s: configured dma chan %d invalid\n",
159 			    sc->sc_dev.dv_xname, sc->sc_drq8));
160 			return 0;
161 		}
162 	}
163 
164         if (0 <= sc->sc_drq16 && sc->sc_drq16 <= 3)
165         	/*
166                  * XXX Some ViBRA16 cards seem to have two 8 bit DMA
167                  * channels.  I've no clue how to use them, so ignore
168                  * one of them for now.  -- augustss@netbsd.org
169                  */
170         	sc->sc_drq16 = -1;
171 
172 	if (ISSB16CLASS(sc)) {
173 		if (sc->sc_drq16 == -1)
174 			sc->sc_drq16 = sc->sc_drq8;
175 		if (!SB16_DRQ_VALID(sc->sc_drq16)) {
176 			DPRINTF(("%s: configured dma chan %d invalid\n",
177 			    sc->sc_dev.dv_xname, sc->sc_drq16));
178 			return 0;
179 		}
180 	} else
181 		sc->sc_drq16 = sc->sc_drq8;
182 
183 	if (ISSBPROCLASS(sc)) {
184 		if (!SBP_IRQ_VALID(sc->sc_irq)) {
185 			DPRINTF(("%s: configured irq %d invalid\n",
186 			    sc->sc_dev.dv_xname, sc->sc_irq));
187 			return 0;
188 		}
189 	} else {
190 		if (!SB_IRQ_VALID(sc->sc_irq)) {
191 			DPRINTF(("%s: configured irq %d invalid\n",
192 			    sc->sc_dev.dv_xname, sc->sc_irq));
193 			return 0;
194 		}
195 	}
196 
197 	if (ISSB16CLASS(sc)) {
198 		int w, r;
199 #if 0
200 		DPRINTF(("%s: old drq conf %02x\n", sc->sc_dev.dv_xname,
201 		    sbdsp_mix_read(sc, SBP_SET_DRQ)));
202 		DPRINTF(("%s: try drq conf %02x\n", sc->sc_dev.dv_xname,
203 		    drq_conf[sc->sc_drq16] | drq_conf[sc->sc_drq8]));
204 #endif
205 		w = drq_conf[sc->sc_drq16] | drq_conf[sc->sc_drq8];
206 		sbdsp_mix_write(sc, SBP_SET_DRQ, w);
207 		r = sbdsp_mix_read(sc, SBP_SET_DRQ) & 0xeb;
208 		if (r != w) {
209 			DPRINTF(("%s: setting drq mask %02x failed, got %02x\n", sc->sc_dev.dv_xname, w, r));
210 			return 0;
211 		}
212 #if 0
213 		DPRINTF(("%s: new drq conf %02x\n", sc->sc_dev.dv_xname,
214 		    sbdsp_mix_read(sc, SBP_SET_DRQ)));
215 #endif
216 
217 #if 0
218 		DPRINTF(("%s: old irq conf %02x\n", sc->sc_dev.dv_xname,
219 		    sbdsp_mix_read(sc, SBP_SET_IRQ)));
220 		DPRINTF(("%s: try irq conf %02x\n", sc->sc_dev.dv_xname,
221 		    irq_conf[sc->sc_irq]));
222 #endif
223 		w = irq_conf[sc->sc_irq];
224 		sbdsp_mix_write(sc, SBP_SET_IRQ, w);
225 		r = sbdsp_mix_read(sc, SBP_SET_IRQ) & 0x0f;
226 		if (r != w) {
227 			DPRINTF(("%s: setting irq mask %02x failed, got %02x\n",
228 			    sc->sc_dev.dv_xname, w, r));
229 			return 0;
230 		}
231 #if 0
232 		DPRINTF(("%s: new irq conf %02x\n", sc->sc_dev.dv_xname,
233 		    sbdsp_mix_read(sc, SBP_SET_IRQ)));
234 #endif
235 	}
236 
237 	return 1;
238 }
239 
240 
241 void
242 sbattach(struct sbdsp_softc *sc)
243 {
244 	struct audio_attach_args arg;
245 #if NMIDI > 0
246 	struct midi_hw_if *mhw = &sb_midi_hw_if;
247 #endif
248 
249 	sc->sc_ih = isa_intr_establish(sc->sc_ic, sc->sc_irq,
250 	    IST_EDGE, IPL_AUDIO | IPL_MPSAFE,
251 	    sbdsp_intr, sc, sc->sc_dev.dv_xname);
252 
253 	sbdsp_attach(sc);
254 
255 #if NMIDI > 0
256 	sc->sc_hasmpu = 0;
257 	if (ISSB16CLASS(sc) && sc->sc_mpu_sc.iobase != 0) {
258 		sc->sc_mpu_sc.iot = sc->sc_iot;
259 		if (mpu_find(&sc->sc_mpu_sc)) {
260 			sc->sc_hasmpu = 1;
261 			mhw = &sb_mpu401_hw_if;
262 		}
263 	}
264 	midi_attach_mi(mhw, sc, &sc->sc_dev);
265 #endif
266 
267 	audio_attach_mi(&sb_hw_if, sc, &sc->sc_dev);
268 
269 	arg.type = AUDIODEV_TYPE_OPL;
270 	arg.hwif = 0;
271 	arg.hdl = 0;
272 	(void)config_found(&sc->sc_dev, &arg, audioprint);
273 }
274 
275 /*
276  * Various routines to interface to higher level audio driver
277  */
278 
279 #if NMIDI > 0
280 
281 #define SBMPU(a) (&((struct sbdsp_softc *)addr)->sc_mpu_sc)
282 
283 int
284 sb_mpu401_open(void *addr, int flags, void (*iintr)(void *, int),
285     void (*ointr)(void *), void *arg)
286 {
287 	return mpu_open(SBMPU(addr), flags, iintr, ointr, arg);
288 }
289 
290 int
291 sb_mpu401_output(void *addr, int d)
292 {
293 	return mpu_output(SBMPU(addr), d);
294 }
295 
296 void
297 sb_mpu401_close(void *addr)
298 {
299 	mpu_close(SBMPU(addr));
300 }
301 
302 void
303 sb_mpu401_getinfo(void *addr, struct midi_info *mi)
304 {
305 	mi->name = "SB MPU-401 UART";
306 	mi->props = 0;
307 }
308 #endif
309