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