Home | History | Annotate | Line # | Download | only in isa
sb.c revision 1.61
      1  1.61   mycroft /*	$NetBSD: sb.c,v 1.61 1998/08/10 00:20:39 mycroft Exp $	*/
      2  1.10       cgd 
      3   1.1       cgd /*
      4   1.1       cgd  * Copyright (c) 1991-1993 Regents of the University of California.
      5   1.1       cgd  * All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the Computer Systems
     18   1.1       cgd  *	Engineering Group at Lawrence Berkeley Laboratory.
     19   1.1       cgd  * 4. Neither the name of the University nor of the Laboratory may be used
     20   1.1       cgd  *    to endorse or promote products derived from this software without
     21   1.1       cgd  *    specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34  1.17    brezak  *
     35   1.1       cgd  */
     36   1.1       cgd 
     37  1.60  augustss #include "midi.h"
     38  1.60  augustss 
     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.6   mycroft #include <sys/device.h>
     45  1.17    brezak #include <sys/proc.h>
     46   1.1       cgd 
     47   1.1       cgd #include <machine/cpu.h>
     48  1.36   mycroft #include <machine/intr.h>
     49  1.49   thorpej #include <machine/bus.h>
     50   1.1       cgd 
     51  1.17    brezak #include <sys/audioio.h>
     52  1.17    brezak #include <dev/audio_if.h>
     53  1.17    brezak 
     54  1.23       cgd #include <dev/isa/isavar.h>
     55  1.23       cgd #include <dev/isa/isadmavar.h>
     56   1.1       cgd 
     57  1.39  christos #include <dev/isa/sbreg.h>
     58  1.39  christos #include <dev/isa/sbvar.h>
     59  1.24       cgd #include <dev/isa/sbdspvar.h>
     60  1.33   thorpej 
     61  1.60  augustss #if NMIDI > 0
     62  1.60  augustss int	sb_mpu401_open __P((void *, int,
     63  1.60  augustss 			     void (*iintr)__P((void *, int)),
     64  1.60  augustss 			     void (*ointr)__P((void *)), void *arg));
     65  1.60  augustss void	sb_mpu401_close __P((void *));
     66  1.60  augustss int	sb_mpu401_output __P((void *, int));
     67  1.60  augustss void	sb_mpu401_getinfo __P((void *, struct midi_info *));
     68  1.60  augustss 
     69  1.60  augustss struct midi_hw_if sb_midi_hw_if = {
     70  1.60  augustss 	sbdsp_midi_open,
     71  1.60  augustss 	sbdsp_midi_close,
     72  1.60  augustss 	sbdsp_midi_output,
     73  1.60  augustss 	sbdsp_midi_getinfo,
     74  1.60  augustss 	0,			/* ioctl */
     75  1.60  augustss };
     76  1.60  augustss 
     77  1.60  augustss struct midi_hw_if sb_mpu401_hw_if = {
     78  1.60  augustss 	sb_mpu401_open,
     79  1.60  augustss 	sb_mpu401_close,
     80  1.60  augustss 	sb_mpu401_output,
     81  1.60  augustss 	sb_mpu401_getinfo,
     82  1.60  augustss 	0,			/* ioctl */
     83  1.60  augustss };
     84  1.60  augustss #endif
     85  1.60  augustss 
     86  1.33   thorpej struct audio_device sb_device = {
     87  1.33   thorpej 	"SoundBlaster",
     88  1.33   thorpej 	"x",
     89  1.33   thorpej 	"sb"
     90  1.17    brezak };
     91   1.1       cgd 
     92  1.22   mycroft int	sb_getdev __P((void *, struct audio_device *));
     93   1.1       cgd 
     94  1.17    brezak /*
     95  1.17    brezak  * Define our interface to the higher level audio driver.
     96  1.17    brezak  */
     97  1.17    brezak 
     98  1.17    brezak struct audio_hw_if sb_hw_if = {
     99  1.53  augustss 	sbdsp_open,
    100  1.17    brezak 	sbdsp_close,
    101  1.51  augustss 	0,
    102  1.17    brezak 	sbdsp_query_encoding,
    103  1.46  augustss 	sbdsp_set_params,
    104  1.17    brezak 	sbdsp_round_blocksize,
    105  1.51  augustss 	0,
    106  1.61   mycroft 	0,
    107  1.61   mycroft 	0,
    108  1.61   mycroft 	0,
    109  1.61   mycroft 	0,
    110  1.17    brezak 	sbdsp_haltdma,
    111  1.17    brezak 	sbdsp_haltdma,
    112  1.17    brezak 	sbdsp_speaker_ctl,
    113  1.17    brezak 	sb_getdev,
    114  1.51  augustss 	0,
    115  1.17    brezak 	sbdsp_mixer_set_port,
    116  1.17    brezak 	sbdsp_mixer_get_port,
    117  1.17    brezak 	sbdsp_mixer_query_devinfo,
    118  1.50  augustss 	sb_malloc,
    119  1.50  augustss 	sb_free,
    120  1.50  augustss 	sb_round,
    121  1.52  augustss         sb_mappage,
    122  1.53  augustss 	sbdsp_get_props,
    123  1.61   mycroft 	sbdsp_trigger_output,
    124  1.61   mycroft 	sbdsp_trigger_input,
    125   1.6   mycroft };
    126   1.1       cgd 
    127  1.17    brezak /*
    128  1.17    brezak  * Probe / attach routines.
    129  1.17    brezak  */
    130  1.17    brezak 
    131  1.39  christos 
    132   1.1       cgd int
    133  1.39  christos sbmatch(sc)
    134  1.39  christos 	struct sbdsp_softc *sc;
    135   1.1       cgd {
    136  1.43   mycroft 	static u_char drq_conf[8] = {
    137  1.43   mycroft 		0x01, 0x02, -1, 0x08, -1, 0x20, 0x40, 0x80
    138  1.28   mycroft 	};
    139  1.39  christos 
    140  1.19    brezak 	static u_char irq_conf[11] = {
    141  1.28   mycroft 		-1, -1, 0x01, -1, -1, 0x02, -1, 0x04, -1, 0x01, 0x08
    142  1.19    brezak 	};
    143   1.1       cgd 
    144  1.44   mycroft 	if (sbdsp_probe(sc) == 0)
    145   1.6   mycroft 		return 0;
    146  1.43   mycroft 
    147   1.1       cgd 	/*
    148   1.1       cgd 	 * Cannot auto-discover DMA channel.
    149   1.1       cgd 	 */
    150  1.26    brezak 	if (ISSBPROCLASS(sc)) {
    151  1.43   mycroft 		if (!SBP_DRQ_VALID(sc->sc_drq8)) {
    152  1.43   mycroft 			printf("%s: configured dma chan %d invalid\n",
    153  1.43   mycroft 			    sc->sc_dev.dv_xname, sc->sc_drq8);
    154  1.43   mycroft 			return 0;
    155  1.43   mycroft 		}
    156  1.43   mycroft 	} else {
    157  1.43   mycroft 		if (!SB_DRQ_VALID(sc->sc_drq8)) {
    158  1.39  christos 			printf("%s: configured dma chan %d invalid\n",
    159  1.43   mycroft 			    sc->sc_dev.dv_xname, sc->sc_drq8);
    160  1.17    brezak 			return 0;
    161  1.17    brezak 		}
    162  1.17    brezak 	}
    163  1.49   thorpej 
    164  1.56  augustss         if (0 <= sc->sc_drq16 && sc->sc_drq16 <= 3)
    165  1.56  augustss         	/*
    166  1.56  augustss                  * XXX Some ViBRA16 cards seem to have two 8 bit DMA
    167  1.56  augustss                  * channels.  I've no clue how to use them, so ignore
    168  1.56  augustss                  * one of them for now.  -- augustss (at) netbsd.org
    169  1.56  augustss                  */
    170  1.56  augustss         	sc->sc_drq16 = -1;
    171  1.56  augustss 
    172  1.43   mycroft 	if (ISSB16CLASS(sc)) {
    173  1.43   mycroft 		if (sc->sc_drq16 == -1)
    174  1.43   mycroft 			sc->sc_drq16 = sc->sc_drq8;
    175  1.43   mycroft 		if (!SB16_DRQ_VALID(sc->sc_drq16)) {
    176  1.39  christos 			printf("%s: configured dma chan %d invalid\n",
    177  1.43   mycroft 			    sc->sc_dev.dv_xname, sc->sc_drq16);
    178  1.17    brezak 			return 0;
    179  1.17    brezak 		}
    180  1.43   mycroft 	} else
    181  1.43   mycroft 		sc->sc_drq16 = sc->sc_drq8;
    182  1.17    brezak 
    183   1.6   mycroft #ifdef NEWCONFIG
    184   1.1       cgd 	/*
    185   1.1       cgd 	 * If the IRQ wasn't compiled in, auto-detect it.
    186   1.1       cgd 	 */
    187  1.39  christos 	if (sc->sc_irq == IRQUNK) {
    188  1.39  christos 		sc->sc_irq = isa_discoverintr(sbforceintr, sc);
    189  1.26    brezak 		sbdsp_reset(sc);
    190  1.26    brezak 		if (ISSBPROCLASS(sc)) {
    191  1.39  christos 			if (!SBP_IRQ_VALID(sc->sc_irq)) {
    192  1.39  christos 				printf("%s: couldn't auto-detect interrupt\n",
    193  1.39  christos 					sc->sc_dev.dv_xname);
    194  1.17    brezak 				return 0;
    195  1.17    brezak 			}
    196  1.17    brezak 		}
    197  1.17    brezak 		else {
    198  1.39  christos 			if (!SB_IRQ_VALID(sc->sc_irq)) {
    199  1.60  augustss 				printf("%s: couldn't auto-detect interrupt\n",
    200  1.39  christos 					sc->sc_dev.dv_xname);
    201  1.17    brezak 				return 0;
    202  1.17    brezak 			}
    203  1.17    brezak 		}
    204  1.17    brezak 	} else
    205  1.17    brezak #endif
    206  1.26    brezak 	if (ISSBPROCLASS(sc)) {
    207  1.39  christos 		if (!SBP_IRQ_VALID(sc->sc_irq)) {
    208  1.39  christos 			printf("%s: configured irq %d invalid\n",
    209  1.39  christos 			    sc->sc_dev.dv_xname, sc->sc_irq);
    210  1.17    brezak 			return 0;
    211  1.19    brezak 		}
    212  1.43   mycroft 	} else {
    213  1.39  christos 		if (!SB_IRQ_VALID(sc->sc_irq)) {
    214  1.39  christos 			printf("%s: configured irq %d invalid\n",
    215  1.39  christos 			    sc->sc_dev.dv_xname, sc->sc_irq);
    216   1.6   mycroft 			return 0;
    217   1.1       cgd 		}
    218   1.1       cgd 	}
    219  1.43   mycroft 
    220  1.43   mycroft 	if (ISSB16CLASS(sc)) {
    221  1.47  augustss 		int w, r;
    222  1.43   mycroft #if 0
    223  1.43   mycroft 		printf("%s: old drq conf %02x\n", sc->sc_dev.dv_xname,
    224  1.43   mycroft 		    sbdsp_mix_read(sc, SBP_SET_DRQ));
    225  1.43   mycroft 		printf("%s: try drq conf %02x\n", sc->sc_dev.dv_xname,
    226  1.43   mycroft 		    drq_conf[sc->sc_drq16] | drq_conf[sc->sc_drq8]);
    227  1.43   mycroft #endif
    228  1.47  augustss 		w = drq_conf[sc->sc_drq16] | drq_conf[sc->sc_drq8];
    229  1.47  augustss 		sbdsp_mix_write(sc, SBP_SET_DRQ, w);
    230  1.47  augustss 		r = sbdsp_mix_read(sc, SBP_SET_DRQ) & 0xeb;
    231  1.47  augustss 		if (r != w) {
    232  1.47  augustss 			printf("%s: setting drq mask %02x failed, got %02x\n",
    233  1.47  augustss 			    sc->sc_dev.dv_xname, w, r);
    234  1.47  augustss 			return 0;
    235  1.47  augustss 		}
    236  1.43   mycroft #if 0
    237  1.43   mycroft 		printf("%s: new drq conf %02x\n", sc->sc_dev.dv_xname,
    238  1.43   mycroft 		    sbdsp_mix_read(sc, SBP_SET_DRQ));
    239  1.43   mycroft #endif
    240  1.43   mycroft 
    241  1.43   mycroft #if 0
    242  1.43   mycroft 		printf("%s: old irq conf %02x\n", sc->sc_dev.dv_xname,
    243  1.43   mycroft 		    sbdsp_mix_read(sc, SBP_SET_IRQ));
    244  1.43   mycroft 		printf("%s: try irq conf %02x\n", sc->sc_dev.dv_xname,
    245  1.43   mycroft 		    irq_conf[sc->sc_irq]);
    246  1.43   mycroft #endif
    247  1.47  augustss 		w = irq_conf[sc->sc_irq];
    248  1.47  augustss 		sbdsp_mix_write(sc, SBP_SET_IRQ, w);
    249  1.47  augustss 		r = sbdsp_mix_read(sc, SBP_SET_IRQ) & 0x0f;
    250  1.47  augustss 		if (r != w) {
    251  1.47  augustss 			printf("%s: setting irq mask %02x failed, got %02x\n",
    252  1.47  augustss 			    sc->sc_dev.dv_xname, w, r);
    253  1.47  augustss 			return 0;
    254  1.47  augustss 		}
    255  1.43   mycroft #if 0
    256  1.43   mycroft 		printf("%s: new irq conf %02x\n", sc->sc_dev.dv_xname,
    257  1.43   mycroft 		    sbdsp_mix_read(sc, SBP_SET_IRQ));
    258  1.43   mycroft #endif
    259  1.43   mycroft 	}
    260  1.43   mycroft 
    261  1.39  christos 	return 1;
    262  1.39  christos }
    263  1.39  christos 
    264  1.39  christos 
    265  1.39  christos void
    266  1.39  christos sbattach(sc)
    267  1.39  christos 	struct sbdsp_softc *sc;
    268  1.39  christos {
    269  1.60  augustss #if NMIDI > 0
    270  1.60  augustss 	struct midi_hw_if *mhw = &sb_midi_hw_if;
    271  1.60  augustss #else
    272  1.60  augustss 	struct midi_hw_if *mhw = 0;
    273  1.60  augustss #endif
    274  1.60  augustss 
    275  1.59   thorpej 	sc->sc_ih = isa_intr_establish(sc->sc_ic, sc->sc_irq, sc->sc_ist,
    276  1.39  christos 	    IPL_AUDIO, sbdsp_intr, sc);
    277  1.39  christos 
    278  1.39  christos 	sbdsp_attach(sc);
    279  1.39  christos 
    280  1.60  augustss #if NMIDI > 0
    281  1.60  augustss 	sc->sc_hasmpu = 0;
    282  1.60  augustss 	if (ISSB16CLASS(sc) && sc->sc_mpu_sc.iobase != 0) {
    283  1.60  augustss 		sc->sc_mpu_sc.iot = sc->sc_iot;
    284  1.60  augustss 		if (mpu401_find(&sc->sc_mpu_sc)) {
    285  1.60  augustss 			sc->sc_hasmpu = 1;
    286  1.60  augustss 			mhw = &sb_mpu401_hw_if;
    287  1.60  augustss 		}
    288   1.1       cgd 	}
    289   1.6   mycroft #endif
    290   1.1       cgd 
    291  1.60  augustss 	audio_attach_mi(&sb_hw_if, mhw, sc, &sc->sc_dev);
    292  1.60  augustss }
    293   1.1       cgd 
    294   1.1       cgd /*
    295  1.17    brezak  * Various routines to interface to higher level audio driver
    296   1.1       cgd  */
    297   1.1       cgd 
    298   1.1       cgd int
    299  1.17    brezak sb_getdev(addr, retp)
    300  1.22   mycroft 	void *addr;
    301  1.17    brezak 	struct audio_device *retp;
    302   1.1       cgd {
    303  1.32       jtk 	struct sbdsp_softc *sc = addr;
    304  1.53  augustss 	static char *names[] = SB_NAMES;
    305  1.53  augustss 	char *config;
    306  1.32       jtk 
    307  1.48  augustss 	if (sc->sc_model == SB_JAZZ)
    308  1.32       jtk 		strncpy(retp->name, "MV Jazz16", sizeof(retp->name));
    309  1.32       jtk 	else
    310  1.32       jtk 		strncpy(retp->name, "SoundBlaster", sizeof(retp->name));
    311  1.40     mikel 	sprintf(retp->version, "%d.%02d",
    312  1.48  augustss 		SBVER_MAJOR(sc->sc_version),
    313  1.48  augustss 		SBVER_MINOR(sc->sc_version));
    314  1.53  augustss 	if (0 <= sc->sc_model && sc->sc_model < sizeof names / sizeof names[0])
    315  1.53  augustss 		config = names[sc->sc_model];
    316  1.53  augustss 	else
    317  1.53  augustss 		config = "??";
    318  1.53  augustss 	strncpy(retp->config, config, sizeof(retp->config));
    319  1.32       jtk 
    320   1.6   mycroft 	return 0;
    321   1.1       cgd }
    322  1.60  augustss 
    323  1.60  augustss #if NMIDI > 0
    324  1.60  augustss 
    325  1.60  augustss #define SBMPU(a) (&((struct sbdsp_softc *)addr)->sc_mpu_sc)
    326  1.60  augustss 
    327  1.60  augustss int
    328  1.60  augustss sb_mpu401_open(addr, flags, iintr, ointr, arg)
    329  1.60  augustss 	void *addr;
    330  1.60  augustss 	int flags;
    331  1.60  augustss 	void (*iintr)__P((void *, int));
    332  1.60  augustss 	void (*ointr)__P((void *));
    333  1.60  augustss 	void *arg;
    334  1.60  augustss {
    335  1.60  augustss 	return mpu401_open(SBMPU(addr), flags, iintr, ointr, arg);
    336  1.60  augustss }
    337  1.60  augustss 
    338  1.60  augustss int
    339  1.60  augustss sb_mpu401_output(addr, d)
    340  1.60  augustss 	void *addr;
    341  1.60  augustss 	int d;
    342  1.60  augustss {
    343  1.60  augustss 	return mpu401_output(SBMPU(addr), d);
    344  1.60  augustss }
    345  1.60  augustss 
    346  1.60  augustss void
    347  1.60  augustss sb_mpu401_close(addr)
    348  1.60  augustss 	void *addr;
    349  1.60  augustss {
    350  1.60  augustss 	mpu401_close(SBMPU(addr));
    351  1.60  augustss }
    352  1.60  augustss 
    353  1.60  augustss 
    354  1.60  augustss 
    355  1.60  augustss void
    356  1.60  augustss sb_mpu401_getinfo(addr, mi)
    357  1.60  augustss 	void *addr;
    358  1.60  augustss 	struct midi_info *mi;
    359  1.60  augustss {
    360  1.60  augustss 	mi->name = "SB MPU-401 UART";
    361  1.60  augustss 	mi->props = 0;
    362  1.60  augustss }
    363  1.60  augustss #endif
    364  1.60  augustss 
    365