xref: /openbsd/sys/dev/isa/pas.c (revision 09467b48)
1 /*	$OpenBSD: pas.c,v 1.29 2016/09/19 06:46:44 ratchov Exp $	*/
2 /*	$NetBSD: pas.c,v 1.37 1998/01/12 09:43:43 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  * jfw 7/13/97 - The soundblaster code requires the generic bus-space
39  * structures to be set up properly.  Rather than go to the effort of making
40  * code for a dead line fully generic, properly set up the SB structures and
41  * leave the rest x86/ISA/default-configuration specific.  If you have a
42  * REAL computer, go buy a REAL sound card.
43  */
44 /*
45  * Todo:
46  * 	- look at other PAS drivers (for PAS native support)
47  * 	- use common sb.c once emulation is setup
48  */
49 
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/errno.h>
53 #include <sys/ioctl.h>
54 #include <sys/syslog.h>
55 #include <sys/device.h>
56 
57 #include <machine/cpu.h>
58 #include <machine/intr.h>
59 #include <machine/bus.h>
60 
61 #include <sys/audioio.h>
62 #include <dev/audio_if.h>
63 #include <dev/midi_if.h>
64 
65 #include <dev/isa/isavar.h>
66 #include <dev/isa/isadmavar.h>
67 
68 #include <dev/isa/sbdspvar.h>
69 #include <dev/isa/sbreg.h>
70 
71 #define DEFINE_TRANSLATIONS
72 #include <dev/isa/pasreg.h>
73 
74 #ifdef AUDIO_DEBUG
75 #define DPRINTF(x)	if (pasdebug) printf x
76 int	pasdebug = 0;
77 #else
78 #define DPRINTF(x)
79 #endif
80 
81 /*
82  * Software state, per SoundBlaster card.
83  * The soundblaster has multiple functionality, which we must demultiplex.
84  * One approach is to have one major device number for the soundblaster card,
85  * and use different minor numbers to indicate which hardware function
86  * we want.  This would make for one large driver.  Instead our approach
87  * is to partition the design into a set of drivers that share an underlying
88  * piece of hardware.  Most things are hard to share, for example, the audio
89  * and midi ports.  For audio, we might want to mix two processes' signals,
90  * and for midi we might want to merge streams (this is hard due to
91  * running status).  Moreover, we should be able to re-use the high-level
92  * modules with other kinds of hardware.  In this module, we only handle the
93  * most basic communications with the sb card.
94  */
95 struct pas_softc {
96 	struct sbdsp_softc sc_sbdsp;	/* use sc_dev, sc_id, sc_ih,
97 					 *     sc_iobase, sc_irq, sc_drq
98 					 * from here */
99 	int model;	/* unique to PAS */
100 	int rev;
101 
102 };
103 
104 void	pasconf(int, int, int, int);
105 
106 
107 /*
108  * Define our interface to the higher level audio driver.
109  */
110 
111 struct audio_hw_if pas_hw_if = {
112 	sbdsp_open,
113 	sbdsp_close,
114 	sbdsp_set_params,
115 	sbdsp_round_blocksize,
116 	0,
117 	0,
118 	0,
119 	0,
120 	0,
121 	sbdsp_haltdma,
122 	sbdsp_haltdma,
123 	sbdsp_speaker_ctl,
124 	0,
125 	sbdsp_mixer_set_port,
126 	sbdsp_mixer_get_port,
127 	sbdsp_mixer_query_devinfo,
128 	sb_malloc,
129 	sb_free,
130 	sb_round,
131 	sbdsp_get_props,
132 	sbdsp_trigger_output,
133 	sbdsp_trigger_input
134 };
135 
136 /* The Address Translation code is used to convert I/O register addresses to
137    be relative to the given base -register */
138 
139 static char *pasnames[] = {
140 	"",
141 	"Plus",
142 	"CDPC",
143 	"16",
144 	"16Basic"
145 };
146 
147 /*XXX assume default I/O base address */
148 #define pasread(p) inb(p)
149 #define paswrite(d, p) outb(p, d)
150 
151 void
152 pasconf(model, sbbase, sbirq, sbdrq)
153 	int model;
154 	int sbbase;
155 	int sbirq;
156 	int sbdrq;
157 {
158 	paswrite(0x00, INTERRUPT_MASK);
159 	/* Local timer control register */
160 	paswrite(0x36, SAMPLE_COUNTER_CONTROL);
161 	/* Sample rate timer (16 bit) */
162 	paswrite(0x36, SAMPLE_RATE_TIMER);
163 	paswrite(0, SAMPLE_RATE_TIMER);
164 	/* Local timer control register */
165 	paswrite(0x74, SAMPLE_COUNTER_CONTROL);
166 	/* Sample count register (16 bit) */
167 	paswrite(0x74, SAMPLE_BUFFER_COUNTER);
168 	paswrite(0, SAMPLE_BUFFER_COUNTER);
169 
170 	paswrite(P_C_PCM_MONO | P_C_PCM_DAC_MODE |
171 		  P_C_MIXER_CROSS_L_TO_L | P_C_MIXER_CROSS_R_TO_R,
172 		  PCM_CONTROL);
173 	paswrite(S_M_PCM_RESET | S_M_FM_RESET |
174 		  S_M_SB_RESET | S_M_MIXER_RESET, SERIAL_MIXER);
175 
176 /*XXX*/
177 	paswrite(I_C_1_BOOT_RESET_ENABLE|1, IO_CONFIGURATION_1);
178 
179 	paswrite(I_C_2_PCM_DMA_DISABLED, IO_CONFIGURATION_2);
180 	paswrite(I_C_3_PCM_IRQ_DISABLED, IO_CONFIGURATION_3);
181 
182 #ifdef BROKEN_BUS_CLOCK
183 	paswrite(S_C_1_PCS_ENABLE | S_C_1_PCS_STEREO | S_C_1_PCS_REALSOUND |
184 		  S_C_1_FM_EMULATE_CLOCK, SYSTEM_CONFIGURATION_1);
185 #else
186 	paswrite(S_C_1_PCS_ENABLE | S_C_1_PCS_STEREO | S_C_1_PCS_REALSOUND,
187 		  SYSTEM_CONFIGURATION_1);
188 #endif
189 
190 	/*XXX*/
191 	paswrite(0, SYSTEM_CONFIGURATION_2);
192 	paswrite(0, SYSTEM_CONFIGURATION_3);
193 
194 	/* Sets mute off and selects filter rate of 17.897 kHz */
195 	paswrite(F_F_MIXER_UNMUTE | 0x01, FILTER_FREQUENCY);
196 
197 	if (model == PAS_16 || model == PAS_16BASIC)
198 		paswrite(8, PRESCALE_DIVIDER);
199 	else
200 		paswrite(0, PRESCALE_DIVIDER);
201 
202 	paswrite(P_M_MV508_ADDRESS | P_M_MV508_PCM, PARALLEL_MIXER);
203 	paswrite(5, PARALLEL_MIXER);
204 
205 	/*
206 	 * Setup SoundBlaster emulation.
207 	 */
208 	paswrite((sbbase >> 4) & 0xf, EMULATION_ADDRESS);
209 	paswrite(E_C_SB_IRQ_translate[sbirq] | E_C_SB_DMA_translate[sbdrq],
210 		 EMULATION_CONFIGURATION);
211 	paswrite(C_E_SB_ENABLE, COMPATIBILITY_ENABLE);
212 
213 	/*
214 	 * Set mid-range levels.
215 	 */
216 	paswrite(P_M_MV508_ADDRESS | P_M_MV508_MODE, PARALLEL_MIXER);
217 	paswrite(P_M_MV508_LOUDNESS | P_M_MV508_ENHANCE_NONE, PARALLEL_MIXER);
218 
219 	paswrite(P_M_MV508_ADDRESS | P_M_MV508_MASTER_A, PARALLEL_MIXER);
220 	paswrite(50, PARALLEL_MIXER);
221 	paswrite(P_M_MV508_ADDRESS | P_M_MV508_MASTER_B, PARALLEL_MIXER);
222 	paswrite(50, PARALLEL_MIXER);
223 
224 	paswrite(P_M_MV508_ADDRESS | P_M_MV508_MIXER | P_M_MV508_SB, PARALLEL_MIXER);
225 	paswrite(P_M_MV508_OUTPUTMIX | 30, PARALLEL_MIXER);
226 
227 	paswrite(P_M_MV508_ADDRESS | P_M_MV508_MIXER | P_M_MV508_MIC, PARALLEL_MIXER);
228 	paswrite(P_M_MV508_INPUTMIX | 30, PARALLEL_MIXER);
229 }
230 
231 int	pasprobe(struct device *, void *, void *);
232 void	pasattach(struct device *, struct device *, void *);
233 
234 struct cfattach pas_ca = {
235 	sizeof(struct pas_softc), pasprobe, pasattach
236 };
237 
238 struct cfdriver pas_cd = {
239 	NULL, "pas", DV_DULL
240 };
241 
242 /*
243  * Probe / attach routines.
244  */
245 
246 /*
247  * Probe for the soundblaster hardware.
248  */
249 int
250 pasprobe(parent, match, aux)
251 	struct device *parent;
252 	void *match, *aux;
253 {
254 	struct pas_softc *sc = match;
255 	struct isa_attach_args *ia = aux;
256 	int iobase;
257 	u_char id, t;
258 
259         /* ensure we can set this up as a sound blaster */
260        	if (!SB_BASE_VALID(ia->ia_iobase)) {
261 		DPRINTF(("pas: configured SB iobase 0x%x invalid\n", ia->ia_iobase));
262 		return 0;
263 	}
264 
265 	/*
266 	 * WARNING: Setting an option like W:1 or so that disables
267 	 * warm boot reset of the card will screw up this detect code
268 	 * something fierce.  Adding code to handle this means possibly
269 	 * interfering with other cards on the bus if you have something
270 	 * on base port 0x388.  SO be forewarned.
271 	 */
272 	/* Talk to first board */
273 	outb(MASTER_DECODE, 0xbc);
274 	/* Set base address */
275 
276 #if 0
277 	/* XXX Need to setup pseudo device */
278 	/* XXX What are good io addrs ? */
279 	if (iobase != PAS_DEFAULT_BASE) {
280 		DPRINTF(("pas: configured iobase %d invalid\n", iobase));
281 		return 0;
282 	}
283 #else
284 	/* Start out talking to native PAS */
285 	iobase = PAS_DEFAULT_BASE;
286 #endif
287 
288 	outb(MASTER_DECODE, iobase >> 2);
289 	/* One wait-state */
290 	paswrite(1, WAIT_STATE);
291 
292 	id = pasread(INTERRUPT_MASK);
293 	if (id == 0xff || id == 0xfe) {
294 		/* sanity */
295 		DPRINTF(("pas: bogus card id\n"));
296 		return 0;
297 	}
298 	/*
299 	 * We probably have a PAS-series board, now check for a
300 	 * PAS2-series board by trying to change the board revision
301 	 * bits.  PAS2-series hardware won't let you do this because
302 	 * the bits are read-only.
303 	 */
304 	t = id ^ 0xe0;
305 	paswrite(t, INTERRUPT_MASK);
306 	t = inb(INTERRUPT_MASK);
307 	paswrite(id, INTERRUPT_MASK);
308 
309 	if (t != id) {
310 		/* Not a PAS2 */
311 		DPRINTF(("pas: detected card but PAS2 test failed\n"));
312 		return 0;
313 	}
314 	/*XXX*/
315 	t = pasread(OPERATION_MODE_1) & 0xf;
316 	sc->model = O_M_1_to_card[t];
317 	if (sc->model != 0) {
318 		sc->rev = pasread(BOARD_REV_ID);
319 	}
320 	else {
321 		DPRINTF(("pas: bogus model id\n"));
322 		return 0;
323 	}
324 
325         if (sc->model >= 0) {
326                 if (ia->ia_irq == IRQUNK) {
327                         DPRINTF(("pas: sb emulation requires known irq\n"));
328                         return (0);
329                 }
330                 pasconf(sc->model, ia->ia_iobase, ia->ia_irq, 1);
331         } else {
332                 DPRINTF(("pas: could not probe pas\n"));
333                 return (0);
334         }
335 
336 	/* Now a SoundBlaster, so set up proper bus-space hooks
337          * appropriately
338          */
339 
340 	sc->sc_sbdsp.sc_iobase = ia->ia_iobase;
341 	sc->sc_sbdsp.sc_iot = ia->ia_iot;
342 
343 	/* Map i/o space [we map 24 ports which is the max of the sb and pro */
344 	if (bus_space_map(sc->sc_sbdsp.sc_iot, ia->ia_iobase, SBP_NPORT, 0,
345 	    &sc->sc_sbdsp.sc_ioh)) {
346 		DPRINTF(("pas: can't map i/o space 0x%x/%d in probe\n",
347 		    ia->ia_iobase, SBP_NPORT));
348 		return 0;
349 	}
350 
351 	if (sbdsp_reset(&sc->sc_sbdsp) < 0) {
352 		DPRINTF(("pas: couldn't reset card\n"));
353 		goto unmap;
354 	}
355 
356 	/*
357 	 * Cannot auto-discover DMA channel.
358 	 */
359 	if (!SB_DRQ_VALID(ia->ia_drq)) {
360 		DPRINTF(("pas: configured dma chan %d invalid\n", ia->ia_drq));
361 		goto unmap;
362 	}
363 	if (!SB_IRQ_VALID(ia->ia_irq)) {
364 		DPRINTF(("pas: configured irq chan %d invalid\n", ia->ia_irq));
365 		goto unmap;
366 	}
367 
368 	sc->sc_sbdsp.sc_irq = ia->ia_irq;
369 	sc->sc_sbdsp.sc_drq8 = ia->ia_drq;
370 	sc->sc_sbdsp.sc_drq16 = -1; /* XXX */
371 	sc->sc_sbdsp.sc_ic = ia->ia_ic;
372 
373 	if (sbdsp_probe(&sc->sc_sbdsp) == 0) {
374 		DPRINTF(("pas: sbdsp probe failed\n"));
375 		goto unmap;
376 	}
377 
378 	ia->ia_iosize = SB_NPORT;
379 	return 1;
380 
381  unmap:
382 	bus_space_unmap(sc->sc_sbdsp.sc_iot, sc->sc_sbdsp.sc_ioh, SBP_NPORT);
383 	return 0;
384 }
385 
386 /*
387  * Attach hardware to driver, attach hardware driver to audio
388  * pseudo-device driver .
389  */
390 void
391 pasattach(parent, self, aux)
392 	struct device *parent, *self;
393 	void *aux;
394 {
395 	struct pas_softc *sc = (struct pas_softc *)self;
396 	struct isa_attach_args *ia = (struct isa_attach_args *)aux;
397 	int iobase = ia->ia_iobase;
398 
399 	sc->sc_sbdsp.sc_isa = parent;
400 	sc->sc_sbdsp.sc_iobase = iobase;
401 	sc->sc_sbdsp.sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq,
402 	    IST_EDGE, IPL_AUDIO | IPL_MPSAFE,
403 	    sbdsp_intr, &sc->sc_sbdsp, sc->sc_sbdsp.sc_dev.dv_xname);
404 
405 	printf(" ProAudio Spectrum %s [rev %d] ", pasnames[sc->model], sc->rev);
406 
407 	sbdsp_attach(&sc->sc_sbdsp);
408 
409 	audio_attach_mi(&pas_hw_if, &sc->sc_sbdsp, &sc->sc_sbdsp.sc_dev);
410 }
411