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