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