sb.c revision 1.14 1 /* $NetBSD: sb.c,v 1.14 1994/11/18 22:03:38 mycroft 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 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/errno.h>
39 #include <sys/ioctl.h>
40 #include <sys/syslog.h>
41 #include <sys/device.h>
42
43 #include <machine/cpu.h>
44 #include <machine/pio.h>
45
46 #include <i386/isa/isavar.h>
47 #include <i386/isa/dmavar.h>
48
49 #include "sbreg.h"
50
51 /*
52 * Software state, per SoundBlaster card.
53 * The soundblaster has multiple functionality, which we must demultiplex.
54 * One approach is to have one major device number for the soundblaster card,
55 * and use different minor numbers to indicate which hardware function
56 * we want. This would make for one large driver. Instead our approach
57 * is to partition the design into a set of drivers that share an underlying
58 * piece of hardware. Most things are hard to share, for example, the audio
59 * and midi ports. For audio, we might want to mix two processes' signals,
60 * and for midi we might want to merge streams (this is hard due to
61 * running status). Moreover, we should be able to re-use the high-level
62 * modules with other kinds of hardware. In this module, we only handle the
63 * most basic communications with the sb card.
64 */
65 struct sb_softc {
66 struct device sc_dev; /* base device */
67 struct isadev sc_id; /* ISA device */
68 struct intrhand sc_ih; /* interrupt vectoring */
69
70 int sc_open; /* reference count of open calls */
71 int sc_dmachan; /* dma channel */
72 int sc_locked; /* true when doing HS DMA */
73 int sc_iobase; /* I/O port base address */
74 int sc_adacmode; /* low/high speed mode indicator */
75 #define SB_ADAC_LS 0
76 #define SB_ADAC_HS 1
77 int sc_adactc; /* current adac time constant */
78 u_long sc_interrupts; /* number of interrupts taken */
79 void (*sc_intr)(void*); /* dma completion intr handler */
80 void (*sc_mintr)(void*, int);/* midi input intr handler */
81 void *sc_arg; /* arg for sc_intr() */
82 };
83
84 int sbreset __P((struct sb_softc *));
85 void sb_spkron __P((struct sb_softc *));
86 void sb_spkroff __P((struct sb_softc *));
87
88 static int wdsp(int iobase, int v);
89 static int rdsp(int iobase);
90
91 #define splsb splhigh /* XXX */
92 struct sb_softc *sb_softc; /* XXX */
93
94 #ifndef NEWCONFIG
95 #define at_dma(flags, ptr, cc, chan) isa_dmastart(flags, ptr, cc, chan)
96 #endif
97
98 struct {
99 int wdsp;
100 int rdsp;
101 int wmidi;
102 } sberr;
103
104 int sbprobe __P((struct device *, void *, void *));
105 #ifdef NEWCONFIG
106 void sbforceintr(void *);
107 #endif
108 void sbattach __P((struct device *, struct device *, void *));
109 int sbintr __P((struct sb_softc *));
110
111 struct cfdriver sbcd = {
112 NULL, "sb", sbprobe, sbattach, DV_DULL, sizeof(struct sb_softc)
113 };
114
115 int
116 sbprobe(parent, match, aux)
117 struct device *parent;
118 void *match, *aux;
119 {
120 register struct sb_softc *sc = match;
121 register struct isa_attach_args *ia = aux;
122 register int iobase = ia->ia_iobase;
123
124 if (!SB_BASE_VALID(ia->ia_iobase)) {
125 printf("sb: configured iobase %d invalid\n", ia->ia_iobase);
126 return 0;
127 }
128 sc->sc_iobase = iobase;
129 if (sbreset(sc) < 0) {
130 printf("sb: couldn't reset card\n");
131 return 0;
132 }
133 /*
134 * Cannot auto-discover DMA channel.
135 */
136 if (!SB_DRQ_VALID(ia->ia_drq)) {
137 printf("sb: configured dma chan %d invalid\n", ia->ia_drq);
138 return 0;
139 }
140 #ifdef NEWCONFIG
141 /*
142 * If the IRQ wasn't compiled in, auto-detect it.
143 */
144 if (ia->ia_irq == IRQUNK) {
145 ia->ia_irq = isa_discoverintr(sbforceintr, aux);
146 sbreset(iobase);
147 if (!SB_IRQ_VALID(ia->ia_irq)) {
148 printf("sb: couldn't auto-detect interrupt");
149 return 0;
150 }
151 } else
152 #endif
153 if (!SB_IRQ_VALID(ia->ia_irq)) {
154 int irq = ia->ia_irq;
155 printf("sb: configured irq %d invalid\n", irq);
156 return 0;
157 }
158 ia->ia_iosize = SB_NPORT;
159 return 1;
160 }
161
162 #ifdef NEWCONFIG
163 void
164 sbforceintr(aux)
165 void *aux;
166 {
167 static char dmabuf;
168 struct isa_attach_args *ia = aux;
169 int iobase = ia->ia_iobase;
170
171 /*
172 * Set up a DMA read of one byte.
173 * XXX Note that at this point we haven't called
174 * at_setup_dmachan(). This is okay because it just
175 * allocates a buffer in case it needs to make a copy,
176 * and it won't need to make a copy for a 1 byte buffer.
177 * (I think that calling at_setup_dmachan() should be optional;
178 * if you don't call it, it will be called the first time
179 * it is needed (and you pay the latency). Also, you might
180 * never need the buffer anyway.)
181 */
182 at_dma(1, &dmabuf, 1, ia->ia_drq);
183 if (wdsp(iobase, SB_DSP_RDMA) == 0) {
184 (void)wdsp(iobase, 0);
185 (void)wdsp(iobase, 0);
186 }
187 }
188 #endif
189
190 void
191 sbattach(parent, self, aux)
192 struct device *parent, *self;
193 void *aux;
194 {
195 register struct sb_softc *sc = (struct sb_softc *)self;
196 struct isa_attach_args *ia = (struct isa_attach_args *)aux;
197 register int iobase = ia->ia_iobase;
198 register int vers;
199
200 /* XXX */
201 sb_softc = sc;
202
203 sc->sc_iobase = iobase;
204 sc->sc_dmachan = ia->ia_drq;
205 sc->sc_locked = 0;
206
207 #ifdef NEWCONFIG
208 isa_establish(&sc->sc_id, &sc->sc_dev);
209 #endif
210 sc->sc_ih.ih_fun = sbintr;
211 sc->sc_ih.ih_arg = sc;
212 sc->sc_ih.ih_level = IPL_BIO;
213 intr_establish(ia->ia_irq, &sc->sc_ih);
214
215 #ifdef NEWCONFIG
216 /*
217 * We limit DMA transfers to a page, and use the generic DMA handling
218 * code in isa.c. This code can end up copying a buffer, but since
219 * the audio driver uses relative small buffers this isn't likely.
220 *
221 * This allocation scheme means that the maximum transfer is limited
222 * by the page size (rather than 64k). This is reasonable. For 4K
223 * pages, the transfer time at 48KHz is 4096 / 48000 = 85ms. This
224 * is plenty long enough to amortize any fixed time overhead.
225 */
226 at_setup_dmachan(sc->sc_dmachan, NBPG);
227 #endif
228
229 vers = sbversion(sc);
230 printf(": dsp v%d.%d\n", vers >> 8, vers & 0xff);
231 }
232
233 #define SBUNIT(x) (minor(x) & 0xf)
234
235 struct sb_softc *
236 sbopen()
237 {
238 /* XXXX */
239 struct sb_softc *sc = sb_softc;
240
241 if (sc == 0)
242 return 0;
243
244 if (sc->sc_open == 0 && sbreset(sc) == 0) {
245 sc->sc_open = 1;
246 sc->sc_mintr = 0;
247 sc->sc_intr = 0;
248 return sc;
249 }
250 return 0;
251 }
252
253 void
254 sbclose(sc)
255 struct sb_softc *sc;
256 {
257
258 sc->sc_open = 0;
259 sb_spkroff(sc);
260 sc->sc_intr = 0;
261 sc->sc_mintr = 0;
262 /* XXX this will turn off any dma */
263 sbreset(sc);
264 }
265
266 /*
267 * Write a byte to the dsp.
268 * XXX We are at the mercy of the card as we use a
269 * polling loop and wait until it can take the byte.
270 */
271 static int
272 wdsp(int iobase, int v)
273 {
274 register int i;
275
276 for (i = 100; --i >= 0; ) {
277 if ((inb(iobase + SBP_DSP_WSTAT) & SB_DSP_BUSY) != 0)
278 continue;
279 outb(iobase + SBP_DSP_WRITE, v);
280 return 0;
281 }
282 ++sberr.wdsp;
283 return -1;
284 }
285
286 /*
287 * Read a byte from the DSP, using polling.
288 */
289 int
290 rdsp(int iobase)
291 {
292 register int i;
293
294 for (i = 100; --i >= 0; ) {
295 if ((inb(iobase + SBP_DSP_RSTAT) & SB_DSP_READY) == 0)
296 continue;
297 return inb(iobase + SBP_DSP_READ);
298 }
299 ++sberr.rdsp;
300 return -1;
301 }
302
303 /*
304 * Reset the card.
305 * Return non-zero if the card isn't detected.
306 */
307 int
308 sbreset(sc)
309 struct sb_softc *sc;
310 {
311 register int iobase = sc->sc_iobase;
312 register int i;
313
314 /*
315 * See SBK, section 11.3.
316 * We pulse a reset signal into the card.
317 * Gee, what a brilliant hardware design.
318 */
319 outb(iobase + SBP_DSP_RESET, 1);
320 delay(3);
321 outb(iobase + SBP_DSP_RESET, 0);
322 if (rdsp(iobase) != SB_MAGIC)
323 return -1;
324 return 0;
325 }
326
327 /*
328 * Turn on the speaker. The SBK documention says this operation
329 * can take up to 1/10 of a second. Higher level layers should
330 * probably let the task sleep for this amount of time after
331 * calling here. Otherwise, things might not work (because
332 * wdsp() and rdsp() will probably timeout.)
333 *
334 * These engineers had their heads up their ass when
335 * they designed this card.
336 */
337 void
338 sb_spkron(sc)
339 struct sb_softc *sc;
340 {
341
342 (void)wdsp(sc->sc_iobase, SB_DSP_SPKR_ON);
343 /* XXX bogus */
344 delay(1000);
345 }
346
347 /*
348 * Turn off the speaker; see comment above.
349 */
350 void
351 sb_spkroff(sc)
352 struct sb_softc *sc;
353 {
354
355 (void)wdsp(sc->sc_iobase, SB_DSP_SPKR_OFF);
356 }
357
358 /*
359 * Read the version number out of the card. Return major code
360 * in high byte, and minor code in low byte.
361 */
362 int
363 sbversion(sc)
364 struct sb_softc *sc;
365 {
366 register int iobase = sc->sc_iobase;
367 int v;
368
369 if (wdsp(iobase, SB_DSP_VERSION) < 0)
370 return 0;
371 v = rdsp(iobase) << 8;
372 v |= rdsp(iobase);
373 return ((v >= 0) ? v : 0);
374 }
375
376 /*
377 * Halt a DMA in progress. A low-speed transfer can be
378 * resumed with sb_contdma().
379 */
380 void
381 sb_haltdma(sc)
382 struct sb_softc *sc;
383 {
384
385 if (sc->sc_locked)
386 sbreset(sc);
387 else
388 (void)wdsp(sc->sc_iobase, SB_DSP_HALT);
389 }
390
391 void
392 sb_contdma(sc)
393 struct sb_softc *sc;
394 {
395
396 (void)wdsp(sc->sc_iobase, SB_DSP_CONT);
397 }
398
399 /*
400 * Time constant routines follow. See SBK, section 12.
401 * Although they don't come out and say it (in the docs),
402 * the card clearly uses a 1MHz countdown timer, as the
403 * low-speed formula (p. 12-4) is:
404 * tc = 256 - 10^6 / sr
405 * In high-speed mode, the constant is the upper byte of a 16-bit counter,
406 * and a 256MHz clock is used:
407 * tc = 65536 - 256 * 10^ 6 / sr
408 * Since we can only use the upper byte of the HS TC, the two formulae
409 * are equivalent. (Why didn't they say so?) E.g.,
410 * (65536 - 256 * 10 ^ 6 / x) >> 8 = 256 - 10^6 / x
411 *
412 * The crossover point (from low- to high-speed modes) is different
413 * for the SBPRO and SB20. The table on p. 12-5 gives the following data:
414 *
415 * SBPRO SB20
416 * ----- --------
417 * input ls min 4 KHz 4 HJz
418 * input ls max 23 KHz 13 KHz
419 * input hs max 44.1 KHz 15 KHz
420 * output ls min 4 KHz 4 KHz
421 * output ls max 23 KHz 23 KHz
422 * output hs max 44.1 KHz 44.1 KHz
423 */
424 #define SB_LS_MIN 0x06 /* 4000 Hz */
425 #ifdef SBPRO
426 #define SB_ADC_LS_MAX 0xd4 /* 22727 Hz */
427 #define SB_ADC_HS_MAX 0xe9 /* 43478 Hz */
428 #else
429 #define SB_ADC_LS_MAX 0xb3 /* 12987 Hz */
430 #define SB_ADC_HS_MAX 0xbd /* 14925 Hz */
431 #endif
432 #define SB_DAC_LS_MAX 0xd4 /* 22727 Hz */
433 #define SB_DAC_HS_MAX 0xe9 /* 43478 Hz */
434
435 /*
436 * Convert a linear sampling rate into the DAC time constant.
437 * Set *mode to indicate the high/low-speed DMA operation.
438 * Because of limitations of the card, not all rates are possible.
439 * We return the time constant of the closest possible rate.
440 * The sampling rate limits are different for the DAC and ADC,
441 * so isdac indicates output, and !isdac indicates input.
442 */
443 int
444 sb_srtotc(sr, mode, isdac)
445 int sr;
446 int *mode;
447 int isdac;
448 {
449 register int tc = 256 - 1000000 / sr;
450
451 if (tc < SB_LS_MIN) {
452 tc = SB_LS_MIN;
453 *mode = SB_ADAC_LS;
454 } else if (isdac) {
455 if (tc < SB_DAC_LS_MAX)
456 *mode = SB_ADAC_LS;
457 else {
458 *mode = SB_ADAC_HS;
459 if (tc > SB_DAC_HS_MAX)
460 tc = SB_DAC_HS_MAX;
461 }
462 } else {
463 if (tc < SB_ADC_LS_MAX)
464 *mode = SB_ADAC_LS;
465 else {
466 *mode = SB_ADAC_HS;
467 if (tc > SB_ADC_HS_MAX)
468 tc = SB_ADC_HS_MAX;
469 }
470 }
471 return tc;
472 }
473
474 /*
475 * Convert a DAC time constant to a sampling rate.
476 * See SBK, section 12.
477 */
478 int
479 sb_tctosr(tc)
480 int tc;
481 {
482 return (1000000 / (256 - tc));
483 }
484
485 int
486 sb_set_sr(sc, sr, isdac)
487 register struct sb_softc *sc;
488 u_long *sr;
489 int isdac;
490 {
491 register int tc;
492 int mode;
493
494 tc = sb_srtotc(*sr, &mode, isdac);
495 if (wdsp(sc->sc_iobase, SB_DSP_TIMECONST) < 0 ||
496 wdsp(sc->sc_iobase, tc) < 0)
497 return -1;
498
499 *sr = sb_tctosr(tc);
500 sc->sc_adacmode = mode;
501 sc->sc_adactc = tc;
502
503 return 0;
504 }
505
506 int
507 sb_round_sr(sr, isdac)
508 u_long sr;
509 int isdac;
510 {
511 int mode, tc;
512
513 tc = sb_srtotc(sr, &mode, isdac);
514 return sb_tctosr(tc);
515 }
516
517 int
518 sb_dma_input(sc, p, cc, intr, arg)
519 struct sb_softc *sc;
520 void *p;
521 int cc;
522 void (*intr)();
523 void *arg;
524 {
525 register int iobase;
526
527 at_dma(1, p, cc, sc->sc_dmachan);
528 sc->sc_intr = intr;
529 sc->sc_arg = arg;
530 iobase = sc->sc_iobase;
531 --cc;
532 if (sc->sc_adacmode == SB_ADAC_LS) {
533 if (wdsp(iobase, SB_DSP_RDMA) < 0 ||
534 wdsp(iobase, cc) < 0 ||
535 wdsp(iobase, cc >> 8) < 0) {
536 sbreset(sc);
537 return EIO;
538 }
539 } else {
540 if (wdsp(iobase, SB_DSP_BLOCKSIZE) < 0 ||
541 wdsp(iobase, cc) < 0 ||
542 wdsp(iobase, cc >> 8) < 0 ||
543 wdsp(iobase, SB_DSP_HS_INPUT) < 0) {
544 sbreset(sc);
545 return EIO;
546 }
547 sc->sc_locked = 1;
548 }
549 return 0;
550 }
551
552 int
553 sb_dma_output(sc, p, cc, intr, arg)
554 struct sb_softc *sc;
555 void *p;
556 int cc;
557 void (*intr)();
558 void *arg;
559 {
560 register int iobase;
561
562 at_dma(0, p, cc, sc->sc_dmachan);
563 sc->sc_intr = intr;
564 sc->sc_arg = arg;
565 iobase = sc->sc_iobase;
566 --cc;
567 if (sc->sc_adacmode == SB_ADAC_LS) {
568 if (wdsp(iobase, SB_DSP_WDMA) < 0 ||
569 wdsp(iobase, cc) < 0 ||
570 wdsp(iobase, cc >> 8) < 0) {
571 sbreset(sc);
572 return EIO;
573 }
574 } else {
575 if (wdsp(iobase, SB_DSP_BLOCKSIZE) < 0 ||
576 wdsp(iobase, cc) < 0 ||
577 wdsp(iobase, cc >> 8) < 0 ||
578 wdsp(iobase, SB_DSP_HS_OUTPUT) < 0) {
579 sbreset(sc);
580 return EIO;
581 }
582 sc->sc_locked = 1;
583 }
584 return 0;
585 }
586
587 /*
588 * Only the DSP unit on the sound blaster generates interrupts.
589 * There are three cases of interrupt: reception of a midi byte
590 * (when mode is enabled), completion of dma transmission, or
591 * completion of a dma reception. The three modes are mutually
592 * exclusive so we know a priori which event has occurred.
593 */
594 int
595 sbintr(sc)
596 register struct sb_softc *sc;
597 {
598
599 sc->sc_locked = 0;
600 /* clear interrupt */
601 inb(sc->sc_iobase + SBP_DSP_RSTAT);
602 if (sc->sc_mintr != 0) {
603 int c = rdsp(sc->sc_iobase);
604 (*sc->sc_mintr)(sc->sc_arg, c);
605 } else if (sc->sc_intr != 0)
606 (*sc->sc_intr)(sc->sc_arg);
607 else
608 return 0;
609 return 1;
610 }
611
612 /*
613 * Enter midi uart mode and arrange for read interrupts
614 * to vector to `intr'. This puts the card in a mode
615 * which allows only midi I/O; the card must be reset
616 * to leave this mode. Unfortunately, the card does not
617 * use transmit interrupts, so bytes must be output
618 * using polling. To keep the polling overhead to a
619 * minimum, output should be driven off a timer.
620 * This is a little tricky since only 320us separate
621 * consecutive midi bytes.
622 */
623 void
624 sb_set_midi_mode(sc, intr, arg)
625 struct sb_softc *sc;
626 void (*intr)();
627 void *arg;
628 {
629
630 wdsp(sc->sc_iobase, SB_MIDI_UART_INTR);
631 sc->sc_mintr = intr;
632 sc->sc_intr = 0;
633 sc->sc_arg = arg;
634 }
635
636 /*
637 * Write a byte to the midi port, when in midi uart mode.
638 */
639 void
640 sb_midi_output(sc, v)
641 struct sb_softc *sc;
642 int v;
643 {
644
645 if (wdsp(sc->sc_iobase, v) < 0)
646 ++sberr.wmidi;
647 }
648