pas.c revision 1.41
1/*	$NetBSD: pas.c,v 1.41 1998/06/09 07:25:04 thorpej Exp $	*/
2
3/*
4 * Copyright (c) 1991-1993 Regents of the University of California.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *	This product includes software developed by the Computer Systems
18 *	Engineering Group at Lawrence Berkeley Laboratory.
19 * 4. Neither the name of the University nor of the Laboratory may be used
20 *    to endorse or promote products derived from this software without
21 *    specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 */
36/*
37 * jfw 7/13/97 - The soundblaster code requires the generic bus-space
38 * structures to be set up properly.  Rather than go to the effort of making
39 * code for a dead line fully generic, properly set up the SB structures and
40 * leave the rest x86/ISA/default-configuration specific.  If you have a
41 * REAL computer, go buy a REAL sound card.
42 */
43/*
44 * Todo:
45 * 	- look at other PAS drivers (for PAS native suport)
46 * 	- use common sb.c once emulation is setup
47 */
48
49#include <sys/param.h>
50#include <sys/systm.h>
51#include <sys/errno.h>
52#include <sys/ioctl.h>
53#include <sys/syslog.h>
54#include <sys/device.h>
55#include <sys/proc.h>
56
57#include <machine/cpu.h>
58#include <machine/intr.h>
59#include <machine/bus.h>
60#include <machine/pio.h>
61
62#include <sys/audioio.h>
63#include <dev/audio_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
76int	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 */
95struct pas_softc {
96	struct sbdsp_softc sc_sbdsp;	/* base device, &c. */
97
98	int model;
99	int rev;
100};
101
102int	pas_getdev __P((void *, struct audio_device *));
103void	pasconf __P((int, int, int, int));
104
105
106/*
107 * Define our interface to the higher level audio driver.
108 */
109
110struct audio_hw_if pas_hw_if = {
111	sbdsp_open,
112	sbdsp_close,
113	0,
114	sbdsp_query_encoding,
115	sbdsp_set_params,
116	sbdsp_round_blocksize,
117	0,
118	sbdsp_dma_init_output,
119	sbdsp_dma_init_input,
120	sbdsp_dma_output,
121	sbdsp_dma_input,
122	sbdsp_haltdma,
123	sbdsp_haltdma,
124	sbdsp_speaker_ctl,
125	pas_getdev,
126	0,
127	sbdsp_mixer_set_port,
128	sbdsp_mixer_get_port,
129	sbdsp_mixer_query_devinfo,
130	sb_malloc,
131	sb_free,
132	sb_round,
133        sb_mappage,
134	sbdsp_get_props,
135};
136
137/* The Address Translation code is used to convert I/O register addresses to
138   be relative to the given base -register */
139
140static char *pasnames[] = {
141	"",
142	"Plus",
143	"CDPC",
144	"16",
145	"16Basic"
146};
147
148static struct audio_device pas_device = {
149	"PAS,??",
150	"",
151	"pas"
152};
153
154/*XXX assume default I/O base address */
155#define pasread(p) inb(p)
156#define paswrite(d, p) outb(p, d)
157
158void
159pasconf(model, sbbase, sbirq, sbdrq)
160	int model;
161	int sbbase;
162	int sbirq;
163	int sbdrq;
164{
165	paswrite(0x00, INTERRUPT_MASK);
166	/* Local timer control register */
167	paswrite(0x36, SAMPLE_COUNTER_CONTROL);
168	/* Sample rate timer (16 bit) */
169	paswrite(0x36, SAMPLE_RATE_TIMER);
170	paswrite(0, SAMPLE_RATE_TIMER);
171	/* Local timer control register */
172	paswrite(0x74, SAMPLE_COUNTER_CONTROL);
173	/* Sample count register (16 bit) */
174	paswrite(0x74, SAMPLE_BUFFER_COUNTER);
175	paswrite(0, SAMPLE_BUFFER_COUNTER);
176
177	paswrite(P_C_PCM_MONO | P_C_PCM_DAC_MODE |
178		  P_C_MIXER_CROSS_L_TO_L | P_C_MIXER_CROSS_R_TO_R,
179		  PCM_CONTROL);
180	paswrite(S_M_PCM_RESET | S_M_FM_RESET |
181		  S_M_SB_RESET | S_M_MIXER_RESET, SERIAL_MIXER);
182
183/*XXX*/
184	paswrite(I_C_1_BOOT_RESET_ENABLE|1, IO_CONFIGURATION_1);
185
186	paswrite(I_C_2_PCM_DMA_DISABLED, IO_CONFIGURATION_2);
187	paswrite(I_C_3_PCM_IRQ_DISABLED, IO_CONFIGURATION_3);
188
189#ifdef BROKEN_BUS_CLOCK
190	paswrite(S_C_1_PCS_ENABLE | S_C_1_PCS_STEREO | S_C_1_PCS_REALSOUND |
191		  S_C_1_FM_EMULATE_CLOCK, SYSTEM_CONFIGURATION_1);
192#else
193	paswrite(S_C_1_PCS_ENABLE | S_C_1_PCS_STEREO | S_C_1_PCS_REALSOUND,
194		  SYSTEM_CONFIGURATION_1);
195#endif
196
197	/*XXX*/
198	paswrite(0, SYSTEM_CONFIGURATION_2);
199	paswrite(0, SYSTEM_CONFIGURATION_3);
200
201	/* Sets mute off and selects filter rate of 17.897 kHz */
202	paswrite(F_F_MIXER_UNMUTE | 0x01, FILTER_FREQUENCY);
203
204	if (model == PAS_16 || model == PAS_16BASIC)
205		paswrite(8, PRESCALE_DIVIDER);
206	else
207		paswrite(0, PRESCALE_DIVIDER);
208
209	paswrite(P_M_MV508_ADDRESS | P_M_MV508_PCM, PARALLEL_MIXER);
210	paswrite(5, PARALLEL_MIXER);
211
212	/*
213	 * Setup SoundBlaster emulation.
214	 */
215	paswrite((sbbase >> 4) & 0xf, EMULATION_ADDRESS);
216	paswrite(E_C_SB_IRQ_translate[sbirq] | E_C_SB_DMA_translate[sbdrq],
217		 EMULATION_CONFIGURATION);
218	paswrite(C_E_SB_ENABLE, COMPATIBILITY_ENABLE);
219
220	/*
221	 * Set mid-range levels.
222	 */
223	paswrite(P_M_MV508_ADDRESS | P_M_MV508_MODE, PARALLEL_MIXER);
224	paswrite(P_M_MV508_LOUDNESS | P_M_MV508_ENHANCE_NONE, PARALLEL_MIXER);
225
226	paswrite(P_M_MV508_ADDRESS | P_M_MV508_MASTER_A, PARALLEL_MIXER);
227	paswrite(50, PARALLEL_MIXER);
228	paswrite(P_M_MV508_ADDRESS | P_M_MV508_MASTER_B, PARALLEL_MIXER);
229	paswrite(50, PARALLEL_MIXER);
230
231	paswrite(P_M_MV508_ADDRESS | P_M_MV508_MIXER | P_M_MV508_SB, PARALLEL_MIXER);
232	paswrite(P_M_MV508_OUTPUTMIX | 30, PARALLEL_MIXER);
233
234	paswrite(P_M_MV508_ADDRESS | P_M_MV508_MIXER | P_M_MV508_MIC, PARALLEL_MIXER);
235	paswrite(P_M_MV508_INPUTMIX | 30, PARALLEL_MIXER);
236}
237
238int	pasprobe __P((struct device *, struct cfdata *, void *));
239void	pasattach __P((struct device *, struct device *, void *));
240static	int pasfind __P((struct device *, struct pas_softc *,
241			struct isa_attach_args *));
242
243struct cfattach pas_ca = {
244	sizeof(struct pas_softc), pasprobe, pasattach
245};
246
247/*
248 * Probe / attach routines.
249 */
250
251int
252pasprobe(parent, match, aux)
253	struct device *parent;
254	struct cfdata *match;
255	void *aux;
256{
257	struct pas_softc probesc, *sc = &probesc;
258
259	bzero(sc, sizeof *sc);
260	sc->sc_sbdsp.sc_dev.dv_cfdata = match;
261	strcpy(sc->sc_sbdsp.sc_dev.dv_xname, "pas");
262	return pasfind(parent, sc, aux);
263}
264
265/*
266 * Probe for the soundblaster hardware.
267 */
268static int
269pasfind(parent, sc, ia)
270	struct device *parent;
271	struct pas_softc *sc;
272	struct isa_attach_args *ia;
273{
274	int iobase;
275	u_char id, t;
276
277        /* ensure we can set this up as a sound blaster */
278       	if (!SB_BASE_VALID(ia->ia_iobase)) {
279		printf("pas: configured SB iobase 0x%x invalid\n", ia->ia_iobase);
280		return 0;
281	}
282
283	/*
284	 * WARNING: Setting an option like W:1 or so that disables
285	 * warm boot reset of the card will screw up this detect code
286	 * something fierce.  Adding code to handle this means possibly
287	 * interfering with other cards on the bus if you have something
288	 * on base port 0x388.  SO be forewarned.
289	 */
290	/* Talk to first board */
291	outb(MASTER_DECODE, 0xbc);
292	/* Set base address */
293
294#if 0
295	/* XXX Need to setup pseudo device */
296	/* XXX What are good io addrs ? */
297	if (iobase != PAS_DEFAULT_BASE) {
298		printf("pas: configured iobase %d invalid\n", iobase);
299		return 0;
300	}
301#else
302	/* Start out talking to native PAS */
303	iobase = PAS_DEFAULT_BASE;
304#endif
305
306	outb(MASTER_DECODE, iobase >> 2);
307	/* One wait-state */
308	paswrite(1, WAIT_STATE);
309
310	id = pasread(INTERRUPT_MASK);
311	if (id == 0xff || id == 0xfe) {
312		/* sanity */
313		DPRINTF(("pas: bogus card id\n"));
314		return 0;
315	}
316	/*
317	 * We probably have a PAS-series board, now check for a
318	 * PAS2-series board by trying to change the board revision
319	 * bits.  PAS2-series hardware won't let you do this because
320	 * the bits are read-only.
321	 */
322	t = id ^ 0xe0;
323	paswrite(t, INTERRUPT_MASK);
324	t = inb(INTERRUPT_MASK);
325	paswrite(id, INTERRUPT_MASK);
326
327	if (t != id) {
328		/* Not a PAS2 */
329		printf("pas: detected card but PAS2 test failed\n");
330		return 0;
331	}
332	/*XXX*/
333	t = pasread(OPERATION_MODE_1) & 0xf;
334	sc->model = O_M_1_to_card[t];
335	if (sc->model != 0) {
336		sc->rev = pasread(BOARD_REV_ID);
337	}
338	else {
339		DPRINTF(("pas: bogus model id\n"));
340		return 0;
341	}
342
343        if (sc->model >= 0) {
344                if (ia->ia_irq == IRQUNK) {
345                        printf("pas: sb emulation requires known irq\n");
346                        return (0);
347                }
348                pasconf(sc->model, ia->ia_iobase, ia->ia_irq, 1);
349        } else {
350                DPRINTF(("pas: could not probe pas\n"));
351                return (0);
352        }
353
354	/* Now a SoundBlaster, so set up proper bus-space hooks
355         * appropriately
356         */
357
358	sc->sc_sbdsp.sc_iobase = ia->ia_iobase;
359	sc->sc_sbdsp.sc_iot = ia->ia_iot;
360
361	/* Map i/o space [we map 24 ports which is the max of the sb and pro */
362	if (bus_space_map(sc->sc_sbdsp.sc_iot, ia->ia_iobase, SBP_NPORT, 0,
363	    &sc->sc_sbdsp.sc_ioh)) {
364		printf("pas: can't map i/o space 0x%x/%d in probe\n",
365		    ia->ia_iobase, SBP_NPORT);
366		return 0;
367	}
368
369	if (sbdsp_reset(&sc->sc_sbdsp) < 0) {
370		DPRINTF(("pas: couldn't reset card\n"));
371		goto unmap;
372	}
373
374	/*
375	 * Cannot auto-discover DMA channel.
376	 */
377	if (!SB_DRQ_VALID(ia->ia_drq)) {
378		printf("pas: configured dma chan %d invalid\n", ia->ia_drq);
379		goto unmap;
380	}
381#ifdef NEWCONFIG
382	/*
383	 * If the IRQ wasn't compiled in, auto-detect it.
384	 */
385	if (ia->ia_irq == IRQUNK) {
386		ia->ia_irq = isa_discoverintr(pasforceintr, aux);
387		sbdsp_reset(&sc->sc_sbdsp);
388		if (!SB_IRQ_VALID(ia->ia_irq)) {
389			printf("pas: couldn't auto-detect interrupt");
390			goto unmap;
391		}
392	} else
393#endif
394	if (!SB_IRQ_VALID(ia->ia_irq)) {
395		printf("pas: configured irq chan %d invalid\n", ia->ia_irq);
396		goto unmap;
397	}
398
399	sc->sc_sbdsp.sc_irq = ia->ia_irq;
400	sc->sc_sbdsp.sc_drq8 = ia->ia_drq;
401	sc->sc_sbdsp.sc_drq16 = -1; /* XXX */
402
403	if (sbdsp_probe(&sc->sc_sbdsp) == 0) {
404		DPRINTF(("pas: sbdsp probe failed\n"));
405		goto unmap;
406	}
407
408	ia->ia_iosize = SB_NPORT;
409	return 1;
410
411 unmap:
412	bus_space_unmap(sc->sc_sbdsp.sc_iot, sc->sc_sbdsp.sc_ioh, SBP_NPORT);
413	return 0;
414}
415
416#ifdef NEWCONFIG
417void
418pasforceintr(aux)
419	void *aux;
420{
421	static char dmabuf;
422	struct isa_attach_args *ia = aux;
423	int iobase = ia->ia_iobase;
424
425	/*
426	 * Set up a DMA read of one byte.
427	 * XXX Note that at this point we haven't called
428	 * at_setup_dmachan().  This is okay because it just
429	 * allocates a buffer in case it needs to make a copy,
430	 * and it won't need to make a copy for a 1 byte buffer.
431	 * (I think that calling at_setup_dmachan() should be optional;
432	 * if you don't call it, it will be called the first time
433	 * it is needed (and you pay the latency).  Also, you might
434	 * never need the buffer anyway.)
435	 */
436	at_dma(DMAMODE_READ, &dmabuf, 1, ia->ia_drq);
437	if (pas_wdsp(iobase, SB_DSP_RDMA) == 0) {
438		(void)pas_wdsp(iobase, 0);
439		(void)pas_wdsp(iobase, 0);
440	}
441}
442#endif
443
444/*
445 * Attach hardware to driver, attach hardware driver to audio
446 * pseudo-device driver .
447 */
448void
449pasattach(parent, self, aux)
450	struct device *parent, *self;
451	void *aux;
452{
453	struct pas_softc *sc = (struct pas_softc *)self;
454	struct isa_attach_args *ia = (struct isa_attach_args *)aux;
455	int iobase = ia->ia_iobase;
456
457	if (!pasfind(parent, sc, ia)) {
458		printf("%s: pasfind failed\n", sc->sc_sbdsp.sc_dev.dv_xname);
459		return;
460	}
461
462	sc->sc_sbdsp.sc_ic = ia->ia_ic;
463	sc->sc_sbdsp.sc_iobase = iobase;
464	sc->sc_sbdsp.sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq,
465	    IST_EDGE, IPL_AUDIO, sbdsp_intr, &sc->sc_sbdsp);
466
467	printf(" ProAudio Spectrum %s [rev %d] ", pasnames[sc->model],
468	    sc->rev);
469
470	sbdsp_attach(&sc->sc_sbdsp);
471
472	sprintf(pas_device.name, "pas,%s", pasnames[sc->model]);
473	sprintf(pas_device.version, "%d", sc->rev);
474
475	audio_attach_mi(&pas_hw_if, 0, &sc->sc_sbdsp, &sc->sc_sbdsp.sc_dev);
476}
477
478int
479pas_getdev(addr, retp)
480	void *addr;
481	struct audio_device *retp;
482{
483	*retp = pas_device;
484	return 0;
485}
486