sb.c revision 1.10 1 /* $NetBSD: sb.c,v 1.10 1994/10/27 04:18:12 cgd 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 #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((struct sb_softc *));
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 #endif
211 sc->sc_ih.ih_fun = sbintr;
212 sc->sc_ih.ih_arg = sc;
213 sc->sc_ih.ih_level = IPL_BIO;
214 intr_establish(ia->ia_irq, &sc->sc_ih);
215
216 #ifdef NEWCONFIG
217 /*
218 * We limit DMA transfers to a page, and use the generic DMA handling
219 * code in isa.c. This code can end up copying a buffer, but since
220 * the audio driver uses relative small buffers this isn't likely.
221 *
222 * This allocation scheme means that the maximum transfer is limited
223 * by the page size (rather than 64k). This is reasonable. For 4K
224 * pages, the transfer time at 48KHz is 4096 / 48000 = 85ms. This
225 * is plenty long enough to amortize any fixed time overhead.
226 */
227 at_setup_dmachan(sc->sc_dmachan, NBPG);
228 #endif
229
230 vers = sbversion(sc);
231 printf(": dsp v%d.%d\n", vers >> 8, vers & 0xff);
232 }
233
234 #define SBUNIT(x) (minor(x) & 0xf)
235
236 struct sb_softc *
237 sbopen()
238 {
239 /* XXXX */
240 struct sb_softc *sc = sb_softc;
241
242 if (sc == 0)
243 return 0;
244
245 if (sc->sc_open == 0 && sbreset(sc) == 0) {
246 sc->sc_open = 1;
247 sc->sc_mintr = 0;
248 sc->sc_intr = 0;
249 return sc;
250 }
251 return 0;
252 }
253
254 void
255 sbclose(sc)
256 struct sb_softc *sc;
257 {
258
259 sc->sc_open = 0;
260 sb_spkroff(sc);
261 sc->sc_intr = 0;
262 sc->sc_mintr = 0;
263 /* XXX this will turn off any dma */
264 sbreset(sc);
265 }
266
267 /*
268 * Write a byte to the dsp.
269 * XXX We are at the mercy of the card as we use a
270 * polling loop and wait until it can take the byte.
271 */
272 static int
273 wdsp(u_short iobase, int v)
274 {
275 register int i;
276
277 for (i = 100; --i >= 0; ) {
278 if ((inb(iobase + SBP_DSP_WSTAT) & SB_DSP_BUSY) != 0)
279 continue;
280 outb(iobase + SBP_DSP_WRITE, v);
281 return 0;
282 }
283 ++sberr.wdsp;
284 return -1;
285 }
286
287 /*
288 * Read a byte from the DSP, using polling.
289 */
290 int
291 rdsp(u_short iobase)
292 {
293 register int i;
294
295 for (i = 100; --i >= 0; ) {
296 if ((inb(iobase + SBP_DSP_RSTAT) & SB_DSP_READY) == 0)
297 continue;
298 return inb(iobase + SBP_DSP_READ);
299 }
300 ++sberr.rdsp;
301 return -1;
302 }
303
304 /*
305 * Reset the card.
306 * Return non-zero if the card isn't detected.
307 */
308 int
309 sbreset(sc)
310 struct sb_softc *sc;
311 {
312 register u_short iobase = sc->sc_iobase;
313 register int i;
314
315 /*
316 * See SBK, section 11.3.
317 * We pulse a reset signal into the card.
318 * Gee, what a brilliant hardware design.
319 */
320 outb(iobase + SBP_DSP_RESET, 1);
321 delay(3);
322 outb(iobase + SBP_DSP_RESET, 0);
323 if (rdsp(iobase) != SB_MAGIC)
324 return -1;
325 return 0;
326 }
327
328 /*
329 * Turn on the speaker. The SBK documention says this operation
330 * can take up to 1/10 of a second. Higher level layers should
331 * probably let the task sleep for this amount of time after
332 * calling here. Otherwise, things might not work (because
333 * wdsp() and rdsp() will probably timeout.)
334 *
335 * These engineers had their heads up their ass when
336 * they designed this card.
337 */
338 void
339 sb_spkron(sc)
340 struct sb_softc *sc;
341 {
342
343 (void)wdsp(sc->sc_iobase, SB_DSP_SPKR_ON);
344 /* XXX bogus */
345 delay(1000);
346 }
347
348 /*
349 * Turn off the speaker; see comment above.
350 */
351 void
352 sb_spkroff(sc)
353 struct sb_softc *sc;
354 {
355
356 (void)wdsp(sc->sc_iobase, SB_DSP_SPKR_OFF);
357 }
358
359 /*
360 * Read the version number out of the card. Return major code
361 * in high byte, and minor code in low byte.
362 */
363 int
364 sbversion(sc)
365 struct sb_softc *sc;
366 {
367 register u_short iobase = sc->sc_iobase;
368 int v;
369
370 if (wdsp(iobase, SB_DSP_VERSION) < 0)
371 return 0;
372 v = rdsp(iobase) << 8;
373 v |= rdsp(iobase);
374 return ((v >= 0) ? v : 0);
375 }
376
377 /*
378 * Halt a DMA in progress. A low-speed transfer can be
379 * resumed with sb_contdma().
380 */
381 void
382 sb_haltdma(sc)
383 struct sb_softc *sc;
384 {
385
386 if (sc->sc_locked)
387 sbreset(sc);
388 else
389 (void)wdsp(sc->sc_iobase, SB_DSP_HALT);
390 }
391
392 void
393 sb_contdma(sc)
394 struct sb_softc *sc;
395 {
396
397 (void)wdsp(sc->sc_iobase, SB_DSP_CONT);
398 }
399
400 /*
401 * Time constant routines follow. See SBK, section 12.
402 * Although they don't come out and say it (in the docs),
403 * the card clearly uses a 1MHz countdown timer, as the
404 * low-speed formula (p. 12-4) is:
405 * tc = 256 - 10^6 / sr
406 * In high-speed mode, the constant is the upper byte of a 16-bit counter,
407 * and a 256MHz clock is used:
408 * tc = 65536 - 256 * 10^ 6 / sr
409 * Since we can only use the upper byte of the HS TC, the two formulae
410 * are equivalent. (Why didn't they say so?) E.g.,
411 * (65536 - 256 * 10 ^ 6 / x) >> 8 = 256 - 10^6 / x
412 *
413 * The crossover point (from low- to high-speed modes) is different
414 * for the SBPRO and SB20. The table on p. 12-5 gives the following data:
415 *
416 * SBPRO SB20
417 * ----- --------
418 * input ls min 4 KHz 4 HJz
419 * input ls max 23 KHz 13 KHz
420 * input hs max 44.1 KHz 15 KHz
421 * output ls min 4 KHz 4 KHz
422 * output ls max 23 KHz 23 KHz
423 * output hs max 44.1 KHz 44.1 KHz
424 */
425 #define SB_LS_MIN 0x06 /* 4000 Hz */
426 #ifdef SBPRO
427 #define SB_ADC_LS_MAX 0xd4 /* 22727 Hz */
428 #define SB_ADC_HS_MAX 0xe9 /* 43478 Hz */
429 #else
430 #define SB_ADC_LS_MAX 0xb3 /* 12987 Hz */
431 #define SB_ADC_HS_MAX 0xbd /* 14925 Hz */
432 #endif
433 #define SB_DAC_LS_MAX 0xd4 /* 22727 Hz */
434 #define SB_DAC_HS_MAX 0xe9 /* 43478 Hz */
435
436 /*
437 * Convert a linear sampling rate into the DAC time constant.
438 * Set *mode to indicate the high/low-speed DMA operation.
439 * Because of limitations of the card, not all rates are possible.
440 * We return the time constant of the closest possible rate.
441 * The sampling rate limits are different for the DAC and ADC,
442 * so isdac indicates output, and !isdac indicates input.
443 */
444 int
445 sb_srtotc(sr, mode, isdac)
446 int sr;
447 int *mode;
448 int isdac;
449 {
450 register int tc = 256 - 1000000 / sr;
451
452 if (tc < SB_LS_MIN) {
453 tc = SB_LS_MIN;
454 *mode = SB_ADAC_LS;
455 } else if (isdac) {
456 if (tc < SB_DAC_LS_MAX)
457 *mode = SB_ADAC_LS;
458 else {
459 *mode = SB_ADAC_HS;
460 if (tc > SB_DAC_HS_MAX)
461 tc = SB_DAC_HS_MAX;
462 }
463 } else {
464 if (tc < SB_ADC_LS_MAX)
465 *mode = SB_ADAC_LS;
466 else {
467 *mode = SB_ADAC_HS;
468 if (tc > SB_ADC_HS_MAX)
469 tc = SB_ADC_HS_MAX;
470 }
471 }
472 return tc;
473 }
474
475 /*
476 * Convert a DAC time constant to a sampling rate.
477 * See SBK, section 12.
478 */
479 int
480 sb_tctosr(tc)
481 int tc;
482 {
483 return (1000000 / (256 - tc));
484 }
485
486 int
487 sb_set_sr(sc, sr, isdac)
488 register struct sb_softc *sc;
489 u_long *sr;
490 int isdac;
491 {
492 register int tc;
493 int mode;
494
495 tc = sb_srtotc(*sr, &mode, isdac);
496 if (wdsp(sc->sc_iobase, SB_DSP_TIMECONST) < 0 ||
497 wdsp(sc->sc_iobase, tc) < 0)
498 return -1;
499
500 *sr = sb_tctosr(tc);
501 sc->sc_adacmode = mode;
502 sc->sc_adactc = tc;
503
504 return 0;
505 }
506
507 int
508 sb_round_sr(sr, isdac)
509 u_long sr;
510 int isdac;
511 {
512 int mode, tc;
513
514 tc = sb_srtotc(sr, &mode, isdac);
515 return sb_tctosr(tc);
516 }
517
518 int
519 sb_dma_input(sc, p, cc, intr, arg)
520 struct sb_softc *sc;
521 void *p;
522 int cc;
523 void (*intr)();
524 void *arg;
525 {
526 register u_short iobase;
527
528 at_dma(1, p, cc, sc->sc_dmachan);
529 sc->sc_intr = intr;
530 sc->sc_arg = arg;
531 iobase = sc->sc_iobase;
532 --cc;
533 if (sc->sc_adacmode == SB_ADAC_LS) {
534 if (wdsp(iobase, SB_DSP_RDMA) < 0 ||
535 wdsp(iobase, cc) < 0 ||
536 wdsp(iobase, cc >> 8) < 0) {
537 sbreset(sc);
538 return EIO;
539 }
540 } else {
541 if (wdsp(iobase, SB_DSP_BLOCKSIZE) < 0 ||
542 wdsp(iobase, cc) < 0 ||
543 wdsp(iobase, cc >> 8) < 0 ||
544 wdsp(iobase, SB_DSP_HS_INPUT) < 0) {
545 sbreset(sc);
546 return EIO;
547 }
548 sc->sc_locked = 1;
549 }
550 return 0;
551 }
552
553 int
554 sb_dma_output(sc, p, cc, intr, arg)
555 struct sb_softc *sc;
556 void *p;
557 int cc;
558 void (*intr)();
559 void *arg;
560 {
561 register u_short iobase;
562
563 at_dma(0, p, cc, sc->sc_dmachan);
564 sc->sc_intr = intr;
565 sc->sc_arg = arg;
566 iobase = sc->sc_iobase;
567 --cc;
568 if (sc->sc_adacmode == SB_ADAC_LS) {
569 if (wdsp(iobase, SB_DSP_WDMA) < 0 ||
570 wdsp(iobase, cc) < 0 ||
571 wdsp(iobase, cc >> 8) < 0) {
572 sbreset(sc);
573 return EIO;
574 }
575 } else {
576 if (wdsp(iobase, SB_DSP_BLOCKSIZE) < 0 ||
577 wdsp(iobase, cc) < 0 ||
578 wdsp(iobase, cc >> 8) < 0 ||
579 wdsp(iobase, SB_DSP_HS_OUTPUT) < 0) {
580 sbreset(sc);
581 return EIO;
582 }
583 sc->sc_locked = 1;
584 }
585 return 0;
586 }
587
588 /*
589 * Only the DSP unit on the sound blaster generates interrupts.
590 * There are three cases of interrupt: reception of a midi byte
591 * (when mode is enabled), completion of dma transmission, or
592 * completion of a dma reception. The three modes are mutually
593 * exclusive so we know a priori which event has occurred.
594 */
595 int
596 sbintr(sc)
597 register struct sb_softc *sc;
598 {
599
600 sc->sc_locked = 0;
601 /* clear interrupt */
602 inb(sc->sc_iobase + SBP_DSP_RSTAT);
603 if (sc->sc_mintr != 0) {
604 int c = rdsp(sc->sc_iobase);
605 (*sc->sc_mintr)(sc->sc_arg, c);
606 } else if (sc->sc_intr != 0)
607 (*sc->sc_intr)(sc->sc_arg);
608 else
609 return 0;
610 return 1;
611 }
612
613 /*
614 * Enter midi uart mode and arrange for read interrupts
615 * to vector to `intr'. This puts the card in a mode
616 * which allows only midi I/O; the card must be reset
617 * to leave this mode. Unfortunately, the card does not
618 * use transmit interrupts, so bytes must be output
619 * using polling. To keep the polling overhead to a
620 * minimum, output should be driven off a timer.
621 * This is a little tricky since only 320us separate
622 * consecutive midi bytes.
623 */
624 void
625 sb_set_midi_mode(sc, intr, arg)
626 struct sb_softc *sc;
627 void (*intr)();
628 void *arg;
629 {
630
631 wdsp(sc->sc_iobase, SB_MIDI_UART_INTR);
632 sc->sc_mintr = intr;
633 sc->sc_intr = 0;
634 sc->sc_arg = arg;
635 }
636
637 /*
638 * Write a byte to the midi port, when in midi uart mode.
639 */
640 void
641 sb_midi_output(sc, v)
642 struct sb_softc *sc;
643 int v;
644 {
645
646 if (wdsp(sc->sc_iobase, v) < 0)
647 ++sberr.wmidi;
648 }
649