xref: /netbsd-src/sys/dev/isa/pas.c (revision 07bae7edddbb1ce4c926b2e8db425804589074c9)
1 /*	$NetBSD: pas.c,v 1.7 1995/05/08 22:02:01 brezak 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/pio.h>
52 
53 #include <sys/audioio.h>
54 #include <dev/audio_if.h>
55 
56 #include <dev/isa/isavar.h>
57 #include <dev/isa/isadmavar.h>
58 #include <i386/isa/icu.h>			/* XXX BROKEN; WHY? */
59 
60 #include <dev/isa/sbdspvar.h>
61 #include <dev/isa/sbreg.h>
62 
63 #define DEFINE_TRANSLATIONS
64 #include <dev/isa/pasreg.h>
65 
66 #define DEBUG	/*XXX*/
67 #ifdef DEBUG
68 #define DPRINTF(x)	if (pasdebug) printf x
69 int	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  */
88 struct pas_softc {
89 	struct	device sc_dev;		/* base device */
90 	struct	isadev sc_id;		/* ISA device */
91 	void	*sc_ih;			/* interrupt vectoring */
92 
93 	u_short sc_iobase;		/* PAS iobase */
94 	u_short sc_irq;			/* PAS irq */
95 	u_short sc_drq;			/* PAS drq */
96 
97 	int model;
98 	int rev;
99 
100 	struct sbdsp_softc sc_sbdsp;
101 };
102 
103 int	pasopen __P((dev_t, int));
104 
105 int	pasprobe();
106 void	pasattach();
107 
108 int	pas_getdev __P((void *, struct audio_device *));
109 
110 
111 /*
112  * Define our interface to the higher level audio driver.
113  */
114 
115 struct audio_hw_if pas_hw_if = {
116 	pasopen,
117 	sbdsp_close,
118 	NULL,
119 	sbdsp_set_in_sr,
120 	sbdsp_get_in_sr,
121 	sbdsp_set_out_sr,
122 	sbdsp_get_out_sr,
123 	sbdsp_query_encoding,
124 	sbdsp_set_encoding,
125 	sbdsp_get_encoding,
126 	sbdsp_set_precision,
127 	sbdsp_get_precision,
128 	sbdsp_set_channels,
129 	sbdsp_get_channels,
130 	sbdsp_round_blocksize,
131 	sbdsp_set_out_port,
132 	sbdsp_get_out_port,
133 	sbdsp_set_in_port,
134 	sbdsp_get_in_port,
135 	sbdsp_commit_settings,
136 	sbdsp_get_silence,
137 	sbdsp_expand,
138 	sbdsp_compress,
139 	sbdsp_dma_output,
140 	sbdsp_dma_input,
141 	sbdsp_haltdma,
142 	sbdsp_haltdma,
143 	sbdsp_contdma,
144 	sbdsp_contdma,
145 	sbdsp_speaker_ctl,
146 	pas_getdev,
147 	sbdsp_setfd,
148 	sbdsp_mixer_set_port,
149 	sbdsp_mixer_get_port,
150 	sbdsp_mixer_query_devinfo,
151 	0,	/* not full-duplex */
152 	0
153 };
154 
155 /* The Address Translation code is used to convert I/O register addresses to
156    be relative to the given base -register */
157 
158 static char *pasnames[] = {
159 	"",
160 	"Plus",
161 	"CDPC",
162 	"16",
163 	"16Basic"
164 };
165 
166 static struct audio_device pas_device = {
167 	"PAS,??",
168 	"",
169 	"pas"
170 };
171 
172 /*XXX assume default I/O base address */
173 #define pasread(p) inb(p)
174 #define paswrite(d, p) outb(p, d)
175 
176 void
177 pasconf(int model, int sbbase, int sbirq, int sbdrq)
178 {
179 	int i;
180 
181 	paswrite(0x00, INTERRUPT_MASK);
182 	/* Local timer control register */
183 	paswrite(0x36, SAMPLE_COUNTER_CONTROL);
184 	/* Sample rate timer (16 bit) */
185 	paswrite(0x36, SAMPLE_RATE_TIMER);
186 	paswrite(0, SAMPLE_RATE_TIMER);
187 	/* Local timer control register */
188 	paswrite(0x74, SAMPLE_COUNTER_CONTROL);
189 	/* Sample count register (16 bit) */
190 	paswrite(0x74, SAMPLE_BUFFER_COUNTER);
191 	paswrite(0, SAMPLE_BUFFER_COUNTER);
192 
193 	paswrite(P_C_PCM_MONO | P_C_PCM_DAC_MODE |
194 		  P_C_MIXER_CROSS_L_TO_L | P_C_MIXER_CROSS_R_TO_R,
195 		  PCM_CONTROL);
196 	paswrite(S_M_PCM_RESET | S_M_FM_RESET |
197 		  S_M_SB_RESET | S_M_MIXER_RESET, SERIAL_MIXER);
198 
199 /*XXX*/
200 	paswrite(I_C_1_BOOT_RESET_ENABLE|1, IO_CONFIGURATION_1);
201 
202 	paswrite(I_C_2_PCM_DMA_DISABLED, IO_CONFIGURATION_2);
203 	paswrite(I_C_3_PCM_IRQ_DISABLED, IO_CONFIGURATION_3);
204 
205 #ifdef BROKEN_BUS_CLOCK
206 	paswrite(S_C_1_PCS_ENABLE | S_C_1_PCS_STEREO | S_C_1_PCS_REALSOUND |
207 		  S_C_1_FM_EMULATE_CLOCK, SYSTEM_CONFIGURATION_1);
208 #else
209 	paswrite(S_C_1_PCS_ENABLE | S_C_1_PCS_STEREO | S_C_1_PCS_REALSOUND,
210 		  SYSTEM_CONFIGURATION_1);
211 #endif
212 
213 	/*XXX*/
214 	paswrite(0, SYSTEM_CONFIGURATION_2);
215 	paswrite(0, SYSTEM_CONFIGURATION_3);
216 
217 	/* Sets mute off and selects filter rate of 17.897 kHz */
218 	paswrite(F_F_MIXER_UNMUTE | 0x01, FILTER_FREQUENCY);
219 
220 	if (model == PAS_16 || model == PAS_16BASIC)
221 		paswrite(8, PRESCALE_DIVIDER);
222 	else
223 		paswrite(0, PRESCALE_DIVIDER);
224 
225 	paswrite(P_M_MV508_ADDRESS | P_M_MV508_PCM, PARALLEL_MIXER);
226 	paswrite(5, PARALLEL_MIXER);
227 
228 	/*
229 	 * Setup SoundBlaster emulation.
230 	 */
231 	paswrite((sbbase >> 4) & 0xf, EMULATION_ADDRESS);
232 	paswrite(E_C_SB_IRQ_translate[sbirq] | E_C_SB_DMA_translate[sbdrq],
233 		 EMULATION_CONFIGURATION);
234 	paswrite(C_E_SB_ENABLE, COMPATIBILITY_ENABLE);
235 
236 	/*
237 	 * Set mid-range levels.
238 	 */
239 	paswrite(P_M_MV508_ADDRESS | P_M_MV508_MODE, PARALLEL_MIXER);
240 	paswrite(P_M_MV508_LOUDNESS | P_M_MV508_ENHANCE_NONE, PARALLEL_MIXER);
241 
242 	paswrite(P_M_MV508_ADDRESS | P_M_MV508_MASTER_A, PARALLEL_MIXER);
243 	paswrite(50, PARALLEL_MIXER);
244 	paswrite(P_M_MV508_ADDRESS | P_M_MV508_MASTER_B, PARALLEL_MIXER);
245 	paswrite(50, PARALLEL_MIXER);
246 
247 	paswrite(P_M_MV508_ADDRESS | P_M_MV508_MIXER | P_M_MV508_SB, PARALLEL_MIXER);
248 	paswrite(P_M_MV508_OUTPUTMIX | 30, PARALLEL_MIXER);
249 
250 	paswrite(P_M_MV508_ADDRESS | P_M_MV508_MIXER | P_M_MV508_MIC, PARALLEL_MIXER);
251 	paswrite(P_M_MV508_INPUTMIX | 30, PARALLEL_MIXER);
252 }
253 
254 struct cfdriver pascd = {
255 	NULL, "pas", pasprobe, pasattach, DV_DULL, sizeof(struct pas_softc)
256 };
257 
258 /*
259  * Probe / attach routines.
260  */
261 
262 /*
263  * Probe for the soundblaster hardware.
264  */
265 int
266 pasprobe(parent, self, aux)
267 	struct device *parent, *self;
268 	void *aux;
269 {
270 	register struct pas_softc *sc = (void *)self;
271 	register struct isa_attach_args *ia = aux;
272 	register u_short iobase;
273 	u_char id, t;
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 		printf("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 		printf("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                 int irq = ia->ia_irq;
337                 if (irq == IRQUNK) {
338                         printf("pas: sb emulation requires known irq\n");
339                         return (0);
340                 }
341                 irq = ia->ia_irq;
342                 pasconf(sc->model, ia->ia_iobase, irq, 1);
343         } else {
344                 DPRINTF(("pas: could not probe pas\n"));
345                 return (0);
346         }
347 
348 	/* Now a SoundBlaster */
349 	sc->sc_iobase = ia->ia_iobase;
350 	/* and set the SB iobase into the DSP as well ... */
351 	sc->sc_sbdsp.sc_iobase = ia->ia_iobase;
352 	if (sbdsp_reset(&sc->sc_sbdsp) < 0) {
353 		DPRINTF(("pas: couldn't reset card\n"));
354 		return 0;
355 	}
356 
357 	/*
358 	 * Cannot auto-discover DMA channel.
359 	 */
360 	if (!SB_DRQ_VALID(ia->ia_drq)) {
361 		printf("pas: configured dma chan %d invalid\n", ia->ia_drq);
362 		return 0;
363 	}
364 #ifdef NEWCONFIG
365 	/*
366 	 * If the IRQ wasn't compiled in, auto-detect it.
367 	 */
368 	if (ia->ia_irq == IRQUNK) {
369 		ia->ia_irq = isa_discoverintr(pasforceintr, aux);
370 		sbdsp_reset(&sc->sc_sbdsp);
371 		if (!SB_IRQ_VALID(ia->ia_irq)) {
372 			printf("pas: couldn't auto-detect interrupt");
373 			return 0;
374 		}
375 	} else
376 #endif
377 	if (!SB_IRQ_VALID(ia->ia_irq)) {
378 		int irq = ia->ia_irq;
379 		printf("pas: configured irq %d invalid\n", irq);
380 		return 0;
381 	}
382 
383 	sc->sc_sbdsp.sc_irq = ia->ia_irq;
384 	sc->sc_sbdsp.sc_drq = ia->ia_drq;
385 
386 	if (sbdsp_probe(&sc->sc_sbdsp) == 0) {
387 		DPRINTF(("pas: sbdsp probe failed\n"));
388 		return 0;
389 	}
390 
391 	ia->ia_iosize = SB_NPORT;
392 	return 1;
393 }
394 
395 #ifdef NEWCONFIG
396 void
397 pasforceintr(aux)
398 	void *aux;
399 {
400 	static char dmabuf;
401 	struct isa_attach_args *ia = aux;
402 	u_short iobase = ia->ia_iobase;
403 
404 	/*
405 	 * Set up a DMA read of one byte.
406 	 * XXX Note that at this point we haven't called
407 	 * at_setup_dmachan().  This is okay because it just
408 	 * allocates a buffer in case it needs to make a copy,
409 	 * and it won't need to make a copy for a 1 byte buffer.
410 	 * (I think that calling at_setup_dmachan() should be optional;
411 	 * if you don't call it, it will be called the first time
412 	 * it is needed (and you pay the latency).  Also, you might
413 	 * never need the buffer anyway.)
414 	 */
415 	at_dma(1, &dmabuf, 1, ia->ia_drq);
416 	if (pas_wdsp(iobase, SB_DSP_RDMA) == 0) {
417 		(void)pas_wdsp(iobase, 0);
418 		(void)pas_wdsp(iobase, 0);
419 	}
420 }
421 #endif
422 
423 /*
424  * Attach hardware to driver, attach hardware driver to audio
425  * pseudo-device driver .
426  */
427 void
428 pasattach(parent, self, aux)
429 	struct device *parent, *self;
430 	void *aux;
431 {
432 	register struct pas_softc *sc = (struct pas_softc *)self;
433 	struct isa_attach_args *ia = (struct isa_attach_args *)aux;
434 	register u_short iobase = ia->ia_iobase;
435 
436 	sc->sc_iobase = iobase;
437 
438 #ifdef NEWCONFIG
439 	isa_establish(&sc->sc_id, &sc->sc_dev);
440 #endif
441 	sc->sc_ih = isa_intr_establish(ia->ia_irq, ISA_IST_EDGE, ISA_IPL_BIO,
442 	    sbdsp_intr, &sc->sc_sbdsp);
443 
444 #ifdef NEWCONFIG
445 	/*
446 	 * We limit DMA transfers to a page, and use the generic DMA handling
447 	 * code in isa.c.  This code can end up copying a buffer, but since
448 	 * the audio driver uses relative small buffers this isn't likely.
449 	 *
450 	 * This allocation scheme means that the maximum transfer is limited
451 	 * by the page size (rather than 64k).  This is reasonable.  For 4K
452 	 * pages, the transfer time at 48KHz is 4096 / 48000 = 85ms.  This
453 	 * is plenty long enough to amortize any fixed time overhead.
454 	 */
455 	at_setup_dmachan(sc->sc_dmachan, NBPG);
456 #endif
457 
458 	printf(" ProAudio Spectrum %s [rev %d] ", pasnames[sc->model], sc->rev);
459 
460 	sbdsp_attach(&sc->sc_sbdsp);
461 
462 	sprintf(pas_device.name, "pas,%s", pasnames[sc->model]);
463 	sprintf(pas_device.version, "%d", sc->rev);
464 
465 	if (audio_hardware_attach(&pas_hw_if, &sc->sc_sbdsp) != 0)
466 		printf("pas: could not attach to audio pseudo-device driver\n");
467 }
468 
469 int
470 pasopen(dev, flags)
471     dev_t dev;
472     int flags;
473 {
474     struct pas_softc *sc;
475     int unit = AUDIOUNIT(dev);
476 
477     if (unit >= pascd.cd_ndevs)
478 	return ENODEV;
479 
480     sc = pascd.cd_devs[unit];
481     if (!sc)
482 	return ENXIO;
483 
484     return sbdsp_open(&sc->sc_sbdsp, dev, flags);
485 }
486 
487 int
488 pas_getdev(addr, retp)
489 	void *addr;
490 	struct audio_device *retp;
491 {
492 	*retp = pas_device;
493 	return 0;
494 }
495