pas.c revision 1.65.6.1 1 /* $NetBSD: pas.c,v 1.65.6.1 2007/02/27 14:16:09 ad 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.65.6.1 2007/02/27 14:16:09 ad 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 <machine/cpu.h>
71 #include <machine/intr.h>
72 #include <machine/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
90 int 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 */
109 struct 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
117 int pas_getdev(void *, struct audio_device *);
118 void pasconf(int, int, int, int);
119
120
121 /*
122 * Define our interface to the higher level audio driver.
123 */
124
125 const struct audio_hw_if pas_hw_if = {
126 sbdsp_open,
127 sbdsp_close,
128 0,
129 sbdsp_query_encoding,
130 sbdsp_set_params,
131 sbdsp_round_blocksize,
132 0,
133 0,
134 0,
135 0,
136 0,
137 sbdsp_halt_output,
138 sbdsp_halt_input,
139 sbdsp_speaker_ctl,
140 pas_getdev,
141 0,
142 sbdsp_mixer_set_port,
143 sbdsp_mixer_get_port,
144 sbdsp_mixer_query_devinfo,
145 sb_malloc,
146 sb_free,
147 sb_round_buffersize,
148 sb_mappage,
149 sbdsp_get_props,
150 sbdsp_trigger_output,
151 sbdsp_trigger_input,
152 0,
153 NULL,
154 sbdsp_get_locks,
155 };
156
157 /* The Address Translation code is used to convert I/O register addresses to
158 be relative to the given base -register */
159
160 static const char *pasnames[] = {
161 "",
162 "Plus",
163 "CDPC",
164 "16",
165 "16Basic"
166 };
167
168 static struct audio_device pas_device = {
169 "PAS,??",
170 "",
171 "pas"
172 };
173
174 /*XXX assume default I/O base address */
175 #define pasread(p) inb((p))
176 #define paswrite(d, p) outb((p), (d))
177
178 void
179 pasconf(int model, int sbbase, int sbirq, int sbdrq)
180 {
181
182 paswrite(0x00, INTERRUPT_MASK);
183 /* Local timer control register */
184 paswrite(0x36, SAMPLE_COUNTER_CONTROL);
185 /* Sample rate timer (16 bit) */
186 paswrite(0x36, SAMPLE_RATE_TIMER);
187 paswrite(0, SAMPLE_RATE_TIMER);
188 /* Local timer control register */
189 paswrite(0x74, SAMPLE_COUNTER_CONTROL);
190 /* Sample count register (16 bit) */
191 paswrite(0x74, SAMPLE_BUFFER_COUNTER);
192 paswrite(0, SAMPLE_BUFFER_COUNTER);
193
194 paswrite(P_C_PCM_MONO | P_C_PCM_DAC_MODE |
195 P_C_MIXER_CROSS_L_TO_L | P_C_MIXER_CROSS_R_TO_R,
196 PCM_CONTROL);
197 paswrite(S_M_PCM_RESET | S_M_FM_RESET |
198 S_M_SB_RESET | S_M_MIXER_RESET, SERIAL_MIXER);
199
200 /*XXX*/
201 paswrite(I_C_1_BOOT_RESET_ENABLE|1, IO_CONFIGURATION_1);
202
203 paswrite(I_C_2_PCM_DMA_DISABLED, IO_CONFIGURATION_2);
204 paswrite(I_C_3_PCM_IRQ_DISABLED, IO_CONFIGURATION_3);
205
206 #ifdef BROKEN_BUS_CLOCK
207 paswrite(S_C_1_PCS_ENABLE | S_C_1_PCS_STEREO | S_C_1_PCS_REALSOUND |
208 S_C_1_FM_EMULATE_CLOCK, SYSTEM_CONFIGURATION_1);
209 #else
210 paswrite(S_C_1_PCS_ENABLE | S_C_1_PCS_STEREO | S_C_1_PCS_REALSOUND,
211 SYSTEM_CONFIGURATION_1);
212 #endif
213
214 /*XXX*/
215 paswrite(0, SYSTEM_CONFIGURATION_2);
216 paswrite(0, SYSTEM_CONFIGURATION_3);
217
218 /* Sets mute off and selects filter rate of 17.897 kHz */
219 paswrite(F_F_MIXER_UNMUTE | 0x01, FILTER_FREQUENCY);
220
221 if (model == PAS_16 || model == PAS_16BASIC)
222 paswrite(8, PRESCALE_DIVIDER);
223 else
224 paswrite(0, PRESCALE_DIVIDER);
225
226 paswrite(P_M_MV508_ADDRESS | P_M_MV508_PCM, PARALLEL_MIXER);
227 paswrite(5, PARALLEL_MIXER);
228
229 /*
230 * Setup SoundBlaster emulation.
231 */
232 paswrite((sbbase >> 4) & 0xf, EMULATION_ADDRESS);
233 paswrite(E_C_SB_IRQ_translate[sbirq] | E_C_SB_DMA_translate[sbdrq],
234 EMULATION_CONFIGURATION);
235 paswrite(C_E_SB_ENABLE, COMPATIBILITY_ENABLE);
236
237 /*
238 * Set mid-range levels.
239 */
240 paswrite(P_M_MV508_ADDRESS | P_M_MV508_MODE, PARALLEL_MIXER);
241 paswrite(P_M_MV508_LOUDNESS | P_M_MV508_ENHANCE_NONE, PARALLEL_MIXER);
242
243 paswrite(P_M_MV508_ADDRESS | P_M_MV508_MASTER_A, PARALLEL_MIXER);
244 paswrite(50, PARALLEL_MIXER);
245 paswrite(P_M_MV508_ADDRESS | P_M_MV508_MASTER_B, PARALLEL_MIXER);
246 paswrite(50, PARALLEL_MIXER);
247
248 paswrite(P_M_MV508_ADDRESS | P_M_MV508_MIXER | P_M_MV508_SB,
249 PARALLEL_MIXER);
250 paswrite(P_M_MV508_OUTPUTMIX | 30, PARALLEL_MIXER);
251
252 paswrite(P_M_MV508_ADDRESS | P_M_MV508_MIXER | P_M_MV508_MIC,
253 PARALLEL_MIXER);
254 paswrite(P_M_MV508_INPUTMIX | 30, PARALLEL_MIXER);
255 }
256
257 int pasprobe(struct device *, struct cfdata *, void *);
258 void pasattach(struct device *, struct device *, void *);
259 static int pasfind(struct device *, struct pas_softc *,
260 struct isa_attach_args *, int);
261 /* argument to pasfind */
262 #define PASPROBE 1
263 #define PASATTACH 0
264
265 CFATTACH_DECL(pas, sizeof(struct pas_softc),
266 pasprobe, pasattach, NULL, NULL);
267
268 /*
269 * Probe / attach routines.
270 */
271
272 int
273 pasprobe(struct device *parent, struct cfdata *match, void *aux)
274 {
275 struct isa_attach_args *ia;
276 struct pas_softc probesc, *sc;
277
278 ia = aux;
279 sc = &probesc;
280 if (ia->ia_nio < 1)
281 return 0;
282 if (ia->ia_nirq < 1)
283 return 0;
284 if (ia->ia_ndrq < 1)
285 return 0;
286
287 if (ISA_DIRECT_CONFIG(ia))
288 return 0;
289
290 memset(sc, 0, sizeof *sc);
291 sc->sc_sbdsp.sc_dev.dv_cfdata = match;
292 strcpy(sc->sc_sbdsp.sc_dev.dv_xname, "pas");
293 return pasfind(parent, sc, aux, PASPROBE);
294 }
295
296 /*
297 * Probe for the soundblaster hardware.
298 */
299 static int
300 pasfind(struct device *parent, struct pas_softc *sc,
301 struct isa_attach_args *ia, int probing)
302 {
303 int iobase;
304 u_char id, t;
305 int rc;
306
307 rc = 0; /* failure */
308 /* ensure we can set this up as a sound blaster */
309 if (!SB_BASE_VALID(ia->ia_io[0].ir_addr)) {
310 printf("pas: configured SB iobase 0x%x invalid\n",
311 ia->ia_io[0].ir_addr);
312 return 0;
313 }
314
315 if (bus_space_map(sc->sc_sbdsp.sc_iot, PAS_DEFAULT_BASE, 1, 0,
316 &sc->pas_port_handle)) {
317 printf("pas: can't map base register %x in probe\n",
318 PAS_DEFAULT_BASE);
319 return 0;
320 }
321
322 /*
323 * WARNING: Setting an option like W:1 or so that disables
324 * warm boot reset of the card will screw up this detect code
325 * something fierce. Adding code to handle this means possibly
326 * interfering with other cards on the bus if you have something
327 * on base port 0x388. SO be forewarned.
328 */
329 /* Talk to first board */
330 outb(MASTER_DECODE, 0xbc);
331 /* Set base address */
332
333 #if 0
334 /* XXX Need to setup pseudo device */
335 /* XXX What are good io addrs ? */
336 if (iobase != PAS_DEFAULT_BASE) {
337 printf("pas: configured iobase %d invalid\n", iobase);
338 return 0;
339 }
340 #else
341 /* Start out talking to native PAS */
342 iobase = PAS_DEFAULT_BASE;
343 #endif
344
345 outb(MASTER_DECODE, iobase >> 2);
346 /* One wait-state */
347 paswrite(1, WAIT_STATE);
348
349 id = pasread(INTERRUPT_MASK);
350 if (id == 0xff || id == 0xfe) {
351 /* sanity */
352 DPRINTF(("pas: bogus card id\n"));
353 goto unmap1;
354 }
355 /*
356 * We probably have a PAS-series board, now check for a
357 * PAS2-series board by trying to change the board revision
358 * bits. PAS2-series hardware won't let you do this because
359 * the bits are read-only.
360 */
361 t = id ^ 0xe0;
362 paswrite(t, INTERRUPT_MASK);
363 t = inb(INTERRUPT_MASK);
364 paswrite(id, INTERRUPT_MASK);
365
366 if (t != id) {
367 /* Not a PAS2 */
368 printf("pas: detected card but PAS2 test failed\n");
369 goto unmap1;
370 }
371 /*XXX*/
372 t = pasread(OPERATION_MODE_1) & 0xf;
373 sc->model = O_M_1_to_card[t];
374 if (sc->model != 0) {
375 sc->rev = pasread(BOARD_REV_ID);
376 } else {
377 DPRINTF(("pas: bogus model id\n"));
378 goto unmap1;
379 }
380
381 if (sc->model >= 0) {
382 if (ia->ia_irq[0].ir_irq == ISA_UNKNOWN_IRQ) {
383 printf("pas: sb emulation requires known irq\n");
384 goto unmap1;
385 }
386 pasconf(sc->model, ia->ia_io[0].ir_addr,
387 ia->ia_irq[0].ir_irq, 1);
388 } else {
389 DPRINTF(("pas: could not probe pas\n"));
390 goto unmap1;
391 }
392
393 /* Now a SoundBlaster, so set up proper bus-space hooks
394 * appropriately
395 */
396
397 sc->sc_sbdsp.sc_iobase = ia->ia_io[0].ir_addr;
398 sc->sc_sbdsp.sc_iot = ia->ia_iot;
399
400 /* Map i/o space [we map 24 ports which is the max of the sb and pro */
401 if (bus_space_map(sc->sc_sbdsp.sc_iot, ia->ia_io[0].ir_addr,
402 SBP_NPORT, 0, &sc->sc_sbdsp.sc_ioh)) {
403 printf("pas: can't map i/o space 0x%x/%d in probe\n",
404 ia->ia_io[0].ir_addr, SBP_NPORT);
405 goto unmap1;
406 }
407
408 if (sbdsp_reset(&sc->sc_sbdsp) < 0) {
409 DPRINTF(("pas: couldn't reset card\n"));
410 goto unmap;
411 }
412
413 /*
414 * Cannot auto-discover DMA channel.
415 */
416 if (!SB_DRQ_VALID(ia->ia_drq[0].ir_drq)) {
417 printf("pas: configured DMA chan %d invalid\n",
418 ia->ia_drq[0].ir_drq);
419 goto unmap;
420 }
421 if (!SB_IRQ_VALID(ia->ia_irq[0].ir_irq)) {
422 printf("pas: configured irq chan %d invalid\n",
423 ia->ia_drq[0].ir_drq);
424 goto unmap;
425 }
426
427 sc->sc_sbdsp.sc_irq = ia->ia_irq[0].ir_irq;
428 sc->sc_sbdsp.sc_drq8 = ia->ia_drq[0].ir_drq;
429 sc->sc_sbdsp.sc_drq16 = -1; /* XXX */
430
431 if (sbdsp_probe(&sc->sc_sbdsp) == 0) {
432 DPRINTF(("pas: sbdsp probe failed\n"));
433 goto unmap;
434 }
435
436 rc = 1;
437
438 if (probing) {
439 ia->ia_nio = 1;
440 ia->ia_io[0].ir_size = SBP_NPORT;
441
442 ia->ia_nirq = 1;
443 ia->ia_ndrq = 1;
444
445 ia->ia_niomem = 0;
446 }
447
448 unmap:
449 if (rc == 0 || probing)
450 bus_space_unmap(sc->sc_sbdsp.sc_iot, sc->sc_sbdsp.sc_ioh,
451 SBP_NPORT);
452 unmap1:
453 if (rc == 0 || probing)
454 bus_space_unmap(sc->sc_sbdsp.sc_iot, PAS_DEFAULT_BASE, 1);
455 return rc;
456 }
457
458 /*
459 * Attach hardware to driver, attach hardware driver to audio
460 * pseudo-device driver .
461 */
462 void
463 pasattach(struct device *parent, struct device *self, void *aux)
464 {
465 struct pas_softc *sc;
466 struct isa_attach_args *ia;
467 int iobase;
468
469 sc = (struct pas_softc *)self;
470 ia = (struct isa_attach_args *)aux;
471 iobase = ia->ia_io[0].ir_addr;
472 if (!pasfind(parent, sc, ia, PASATTACH)) {
473 printf("%s: pasfind failed\n", sc->sc_sbdsp.sc_dev.dv_xname);
474 return;
475 }
476
477 mutex_init(&sc->sc_sbdsp.sc_lock, MUTEX_DRIVER, IPL_NONE);
478 mutex_init(&sc->sc_sbdsp.sc_intr_lock, MUTEX_DRIVER, IPL_AUDIO);
479
480 sc->sc_sbdsp.sc_ic = ia->ia_ic;
481 sc->sc_sbdsp.sc_iobase = iobase;
482 sc->sc_sbdsp.sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
483 IST_EDGE, IPL_AUDIO, sbdsp_intr, &sc->sc_sbdsp);
484
485 printf(" ProAudio Spectrum %s [rev %d] ", pasnames[sc->model],
486 sc->rev);
487
488 sbdsp_attach(&sc->sc_sbdsp);
489
490 snprintf(pas_device.name, sizeof(pas_device.name), "pas,%s",
491 pasnames[sc->model]);
492 snprintf(pas_device.version, sizeof(pas_device.version), "%d",
493 sc->rev);
494
495 audio_attach_mi(&pas_hw_if, &sc->sc_sbdsp, &sc->sc_sbdsp.sc_dev);
496 }
497
498 int
499 pas_getdev(void *addr, struct audio_device *retp)
500 {
501
502 *retp = pas_device;
503 return 0;
504 }
505