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