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