Home | History | Annotate | Line # | Download | only in isa
pas.c revision 1.16
      1 /*	$NetBSD: pas.c,v 1.16 1996/04/29 20:03:28 christos 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  * Todo:
     38  * 	- look at other PAS drivers (for PAS native suport)
     39  * 	- use common sb.c once emulation is setup
     40  */
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/errno.h>
     45 #include <sys/ioctl.h>
     46 #include <sys/syslog.h>
     47 #include <sys/device.h>
     48 #include <sys/proc.h>
     49 
     50 #include <machine/cpu.h>
     51 #include <machine/pio.h>
     52 
     53 #include <sys/audioio.h>
     54 #include <dev/audio_if.h>
     55 #include <dev/mulaw.h>
     56 
     57 #include <dev/isa/isavar.h>
     58 #include <dev/isa/isadmavar.h>
     59 
     60 #include <dev/isa/sbdspvar.h>
     61 #include <dev/isa/sbreg.h>
     62 
     63 #define DEFINE_TRANSLATIONS
     64 #include <dev/isa/pasreg.h>
     65 
     66 #ifdef AUDIO_DEBUG
     67 #define DPRINTF(x)	if (pasdebug) printf x
     68 int	pasdebug = 0;
     69 #else
     70 #define DPRINTF(x)
     71 #endif
     72 
     73 /*
     74  * Software state, per SoundBlaster card.
     75  * The soundblaster has multiple functionality, which we must demultiplex.
     76  * One approach is to have one major device number for the soundblaster card,
     77  * and use different minor numbers to indicate which hardware function
     78  * we want.  This would make for one large driver.  Instead our approach
     79  * is to partition the design into a set of drivers that share an underlying
     80  * piece of hardware.  Most things are hard to share, for example, the audio
     81  * and midi ports.  For audio, we might want to mix two processes' signals,
     82  * and for midi we might want to merge streams (this is hard due to
     83  * running status).  Moreover, we should be able to re-use the high-level
     84  * modules with other kinds of hardware.  In this module, we only handle the
     85  * most basic communications with the sb card.
     86  */
     87 struct pas_softc {
     88 	struct	device sc_dev;		/* base device */
     89 	struct	isadev sc_id;		/* ISA device */
     90 	void	*sc_ih;			/* interrupt vectoring */
     91 
     92 	int	sc_iobase;		/* PAS iobase */
     93 	int	sc_irq;			/* PAS irq */
     94 	int	sc_drq;			/* PAS drq */
     95 
     96 	int model;
     97 	int rev;
     98 
     99 	struct sbdsp_softc sc_sbdsp;
    100 };
    101 
    102 int	pasopen __P((dev_t, int));
    103 int	pas_getdev __P((void *, struct audio_device *));
    104 void	pasconf __P((int, int, int, int));
    105 
    106 
    107 /*
    108  * Define our interface to the higher level audio driver.
    109  */
    110 
    111 struct audio_hw_if pas_hw_if = {
    112 	pasopen,
    113 	sbdsp_close,
    114 	NULL,
    115 	sbdsp_set_in_sr,
    116 	sbdsp_get_in_sr,
    117 	sbdsp_set_out_sr,
    118 	sbdsp_get_out_sr,
    119 	sbdsp_query_encoding,
    120 	sbdsp_set_encoding,
    121 	sbdsp_get_encoding,
    122 	sbdsp_set_precision,
    123 	sbdsp_get_precision,
    124 	sbdsp_set_channels,
    125 	sbdsp_get_channels,
    126 	sbdsp_round_blocksize,
    127 	sbdsp_set_out_port,
    128 	sbdsp_get_out_port,
    129 	sbdsp_set_in_port,
    130 	sbdsp_get_in_port,
    131 	sbdsp_commit_settings,
    132 	sbdsp_get_silence,
    133 	mulaw_expand,
    134 	mulaw_compress,
    135 	sbdsp_dma_output,
    136 	sbdsp_dma_input,
    137 	sbdsp_haltdma,
    138 	sbdsp_haltdma,
    139 	sbdsp_contdma,
    140 	sbdsp_contdma,
    141 	sbdsp_speaker_ctl,
    142 	pas_getdev,
    143 	sbdsp_setfd,
    144 	sbdsp_mixer_set_port,
    145 	sbdsp_mixer_get_port,
    146 	sbdsp_mixer_query_devinfo,
    147 	0,	/* not full-duplex */
    148 	0
    149 };
    150 
    151 /* The Address Translation code is used to convert I/O register addresses to
    152    be relative to the given base -register */
    153 
    154 static char *pasnames[] = {
    155 	"",
    156 	"Plus",
    157 	"CDPC",
    158 	"16",
    159 	"16Basic"
    160 };
    161 
    162 static struct audio_device pas_device = {
    163 	"PAS,??",
    164 	"",
    165 	"pas"
    166 };
    167 
    168 /*XXX assume default I/O base address */
    169 #define pasread(p) inb(p)
    170 #define paswrite(d, p) outb(p, d)
    171 
    172 void
    173 pasconf(model, sbbase, sbirq, sbdrq)
    174 	int model;
    175 	int sbbase;
    176 	int sbirq;
    177 	int sbdrq;
    178 {
    179 	paswrite(0x00, INTERRUPT_MASK);
    180 	/* Local timer control register */
    181 	paswrite(0x36, SAMPLE_COUNTER_CONTROL);
    182 	/* Sample rate timer (16 bit) */
    183 	paswrite(0x36, SAMPLE_RATE_TIMER);
    184 	paswrite(0, SAMPLE_RATE_TIMER);
    185 	/* Local timer control register */
    186 	paswrite(0x74, SAMPLE_COUNTER_CONTROL);
    187 	/* Sample count register (16 bit) */
    188 	paswrite(0x74, SAMPLE_BUFFER_COUNTER);
    189 	paswrite(0, SAMPLE_BUFFER_COUNTER);
    190 
    191 	paswrite(P_C_PCM_MONO | P_C_PCM_DAC_MODE |
    192 		  P_C_MIXER_CROSS_L_TO_L | P_C_MIXER_CROSS_R_TO_R,
    193 		  PCM_CONTROL);
    194 	paswrite(S_M_PCM_RESET | S_M_FM_RESET |
    195 		  S_M_SB_RESET | S_M_MIXER_RESET, SERIAL_MIXER);
    196 
    197 /*XXX*/
    198 	paswrite(I_C_1_BOOT_RESET_ENABLE|1, IO_CONFIGURATION_1);
    199 
    200 	paswrite(I_C_2_PCM_DMA_DISABLED, IO_CONFIGURATION_2);
    201 	paswrite(I_C_3_PCM_IRQ_DISABLED, IO_CONFIGURATION_3);
    202 
    203 #ifdef BROKEN_BUS_CLOCK
    204 	paswrite(S_C_1_PCS_ENABLE | S_C_1_PCS_STEREO | S_C_1_PCS_REALSOUND |
    205 		  S_C_1_FM_EMULATE_CLOCK, SYSTEM_CONFIGURATION_1);
    206 #else
    207 	paswrite(S_C_1_PCS_ENABLE | S_C_1_PCS_STEREO | S_C_1_PCS_REALSOUND,
    208 		  SYSTEM_CONFIGURATION_1);
    209 #endif
    210 
    211 	/*XXX*/
    212 	paswrite(0, SYSTEM_CONFIGURATION_2);
    213 	paswrite(0, SYSTEM_CONFIGURATION_3);
    214 
    215 	/* Sets mute off and selects filter rate of 17.897 kHz */
    216 	paswrite(F_F_MIXER_UNMUTE | 0x01, FILTER_FREQUENCY);
    217 
    218 	if (model == PAS_16 || model == PAS_16BASIC)
    219 		paswrite(8, PRESCALE_DIVIDER);
    220 	else
    221 		paswrite(0, PRESCALE_DIVIDER);
    222 
    223 	paswrite(P_M_MV508_ADDRESS | P_M_MV508_PCM, PARALLEL_MIXER);
    224 	paswrite(5, PARALLEL_MIXER);
    225 
    226 	/*
    227 	 * Setup SoundBlaster emulation.
    228 	 */
    229 	paswrite((sbbase >> 4) & 0xf, EMULATION_ADDRESS);
    230 	paswrite(E_C_SB_IRQ_translate[sbirq] | E_C_SB_DMA_translate[sbdrq],
    231 		 EMULATION_CONFIGURATION);
    232 	paswrite(C_E_SB_ENABLE, COMPATIBILITY_ENABLE);
    233 
    234 	/*
    235 	 * Set mid-range levels.
    236 	 */
    237 	paswrite(P_M_MV508_ADDRESS | P_M_MV508_MODE, PARALLEL_MIXER);
    238 	paswrite(P_M_MV508_LOUDNESS | P_M_MV508_ENHANCE_NONE, PARALLEL_MIXER);
    239 
    240 	paswrite(P_M_MV508_ADDRESS | P_M_MV508_MASTER_A, PARALLEL_MIXER);
    241 	paswrite(50, PARALLEL_MIXER);
    242 	paswrite(P_M_MV508_ADDRESS | P_M_MV508_MASTER_B, PARALLEL_MIXER);
    243 	paswrite(50, PARALLEL_MIXER);
    244 
    245 	paswrite(P_M_MV508_ADDRESS | P_M_MV508_MIXER | P_M_MV508_SB, PARALLEL_MIXER);
    246 	paswrite(P_M_MV508_OUTPUTMIX | 30, PARALLEL_MIXER);
    247 
    248 	paswrite(P_M_MV508_ADDRESS | P_M_MV508_MIXER | P_M_MV508_MIC, PARALLEL_MIXER);
    249 	paswrite(P_M_MV508_INPUTMIX | 30, PARALLEL_MIXER);
    250 }
    251 
    252 int	pasprobe __P((struct device *, void *, void *));
    253 void	pasattach __P((struct device *, struct device *, void *));
    254 
    255 struct cfattach pas_ca = {
    256 	sizeof(struct pas_softc), pasprobe, pasattach
    257 };
    258 
    259 struct cfdriver pas_cd = {
    260 	NULL, "pas", DV_DULL
    261 };
    262 
    263 /*
    264  * Probe / attach routines.
    265  */
    266 
    267 /*
    268  * Probe for the soundblaster hardware.
    269  */
    270 int
    271 pasprobe(parent, match, aux)
    272 	struct device *parent;
    273 	void *match, *aux;
    274 {
    275 	register struct pas_softc *sc = match;
    276 	register struct isa_attach_args *ia = aux;
    277 	register int iobase;
    278 	u_char id, t;
    279 
    280 	/*
    281 	 * WARNING: Setting an option like W:1 or so that disables
    282 	 * warm boot reset of the card will screw up this detect code
    283 	 * something fierce.  Adding code to handle this means possibly
    284 	 * interfering with other cards on the bus if you have something
    285 	 * on base port 0x388.  SO be forewarned.
    286 	 */
    287 	/* Talk to first board */
    288 	outb(MASTER_DECODE, 0xbc);
    289 	/* Set base address */
    290 
    291 #if 0
    292 	/* XXX Need to setup pseudo device */
    293 	/* XXX What are good io addrs ? */
    294 	if (iobase != PAS_DEFAULT_BASE) {
    295 		printf("pas: configured iobase %d invalid\n", iobase);
    296 		return 0;
    297 	}
    298 #else
    299 	/* Start out talking to native PAS */
    300 	iobase = PAS_DEFAULT_BASE;
    301 #endif
    302 
    303 	outb(MASTER_DECODE, iobase >> 2);
    304 	/* One wait-state */
    305 	paswrite(1, WAIT_STATE);
    306 
    307 	id = pasread(INTERRUPT_MASK);
    308 	if (id == 0xff || id == 0xfe) {
    309 		/* sanity */
    310 		DPRINTF(("pas: bogus card id\n"));
    311 		return 0;
    312 	}
    313 	/*
    314 	 * We probably have a PAS-series board, now check for a
    315 	 * PAS2-series board by trying to change the board revision
    316 	 * bits.  PAS2-series hardware won't let you do this because
    317 	 * the bits are read-only.
    318 	 */
    319 	t = id ^ 0xe0;
    320 	paswrite(t, INTERRUPT_MASK);
    321 	t = inb(INTERRUPT_MASK);
    322 	paswrite(id, INTERRUPT_MASK);
    323 
    324 	if (t != id) {
    325 		/* Not a PAS2 */
    326 		printf("pas: detected card but PAS2 test failed\n");
    327 		return 0;
    328 	}
    329 	/*XXX*/
    330 	t = pasread(OPERATION_MODE_1) & 0xf;
    331 	sc->model = O_M_1_to_card[t];
    332 	if (sc->model != 0) {
    333 		sc->rev = pasread(BOARD_REV_ID);
    334 	}
    335 	else {
    336 		DPRINTF(("pas: bogus model id\n"));
    337 		return 0;
    338 	}
    339 
    340         if (sc->model >= 0) {
    341                 if (ia->ia_irq == IRQUNK) {
    342                         printf("pas: sb emulation requires known irq\n");
    343                         return (0);
    344                 }
    345                 pasconf(sc->model, ia->ia_iobase, ia->ia_irq, 1);
    346         } else {
    347                 DPRINTF(("pas: could not probe pas\n"));
    348                 return (0);
    349         }
    350 
    351 	/* Now a SoundBlaster */
    352 	sc->sc_iobase = ia->ia_iobase;
    353 	/* and set the SB iobase into the DSP as well ... */
    354 	sc->sc_sbdsp.sc_iobase = ia->ia_iobase;
    355 	if (sbdsp_reset(&sc->sc_sbdsp) < 0) {
    356 		DPRINTF(("pas: couldn't reset card\n"));
    357 		return 0;
    358 	}
    359 
    360 	/*
    361 	 * Cannot auto-discover DMA channel.
    362 	 */
    363 	if (!SB_DRQ_VALID(ia->ia_drq)) {
    364 		printf("pas: configured dma chan %d invalid\n", ia->ia_drq);
    365 		return 0;
    366 	}
    367 #ifdef NEWCONFIG
    368 	/*
    369 	 * If the IRQ wasn't compiled in, auto-detect it.
    370 	 */
    371 	if (ia->ia_irq == IRQUNK) {
    372 		ia->ia_irq = isa_discoverintr(pasforceintr, aux);
    373 		sbdsp_reset(&sc->sc_sbdsp);
    374 		if (!SB_IRQ_VALID(ia->ia_irq)) {
    375 			printf("pas: couldn't auto-detect interrupt");
    376 			return 0;
    377 		}
    378 	} else
    379 #endif
    380 	if (!SB_IRQ_VALID(ia->ia_irq)) {
    381 		printf("pas: configured irq chan %d invalid\n", ia->ia_irq);
    382 		return 0;
    383 	}
    384 
    385 	sc->sc_sbdsp.sc_irq = ia->ia_irq;
    386 	sc->sc_sbdsp.sc_drq = ia->ia_drq;
    387 
    388 	if (sbdsp_probe(&sc->sc_sbdsp) == 0) {
    389 		DPRINTF(("pas: sbdsp probe failed\n"));
    390 		return 0;
    391 	}
    392 
    393 	ia->ia_iosize = SB_NPORT;
    394 	return 1;
    395 }
    396 
    397 #ifdef NEWCONFIG
    398 void
    399 pasforceintr(aux)
    400 	void *aux;
    401 {
    402 	static char dmabuf;
    403 	struct isa_attach_args *ia = aux;
    404 	int iobase = ia->ia_iobase;
    405 
    406 	/*
    407 	 * Set up a DMA read of one byte.
    408 	 * XXX Note that at this point we haven't called
    409 	 * at_setup_dmachan().  This is okay because it just
    410 	 * allocates a buffer in case it needs to make a copy,
    411 	 * and it won't need to make a copy for a 1 byte buffer.
    412 	 * (I think that calling at_setup_dmachan() should be optional;
    413 	 * if you don't call it, it will be called the first time
    414 	 * it is needed (and you pay the latency).  Also, you might
    415 	 * never need the buffer anyway.)
    416 	 */
    417 	at_dma(DMAMODE_READ, &dmabuf, 1, ia->ia_drq);
    418 	if (pas_wdsp(iobase, SB_DSP_RDMA) == 0) {
    419 		(void)pas_wdsp(iobase, 0);
    420 		(void)pas_wdsp(iobase, 0);
    421 	}
    422 }
    423 #endif
    424 
    425 /*
    426  * Attach hardware to driver, attach hardware driver to audio
    427  * pseudo-device driver .
    428  */
    429 void
    430 pasattach(parent, self, aux)
    431 	struct device *parent, *self;
    432 	void *aux;
    433 {
    434 	register struct pas_softc *sc = (struct pas_softc *)self;
    435 	struct isa_attach_args *ia = (struct isa_attach_args *)aux;
    436 	register int iobase = ia->ia_iobase;
    437 	int err;
    438 
    439 	sc->sc_iobase = iobase;
    440 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
    441 	    IPL_AUDIO, sbdsp_intr, &sc->sc_sbdsp);
    442 
    443 	printf(" ProAudio Spectrum %s [rev %d] ", pasnames[sc->model], sc->rev);
    444 
    445 	sbdsp_attach(&sc->sc_sbdsp);
    446 
    447 	sprintf(pas_device.name, "pas,%s", pasnames[sc->model]);
    448 	sprintf(pas_device.version, "%d", sc->rev);
    449 
    450 	if ((err = audio_hardware_attach(&pas_hw_if, &sc->sc_sbdsp)) != 0)
    451 		printf("pas: could not attach to audio pseudo-device driver (%d)\n", err);
    452 }
    453 
    454 int
    455 pasopen(dev, flags)
    456     dev_t dev;
    457     int flags;
    458 {
    459     struct pas_softc *sc;
    460     int unit = AUDIOUNIT(dev);
    461 
    462     if (unit >= pas_cd.cd_ndevs)
    463 	return ENODEV;
    464 
    465     sc = pas_cd.cd_devs[unit];
    466     if (!sc)
    467 	return ENXIO;
    468 
    469     return sbdsp_open(&sc->sc_sbdsp, dev, flags);
    470 }
    471 
    472 int
    473 pas_getdev(addr, retp)
    474 	void *addr;
    475 	struct audio_device *retp;
    476 {
    477 	*retp = pas_device;
    478 	return 0;
    479 }
    480