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