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