diff options
author | Theo de Raadt <deraadt@cvs.openbsd.org> | 2000-07-20 21:45:21 +0000 |
---|---|---|
committer | Theo de Raadt <deraadt@cvs.openbsd.org> | 2000-07-20 21:45:21 +0000 |
commit | 0274aef416f3d23ab431432f7a8d3d7773f4f53a (patch) | |
tree | 450582532d58e699c1b6e6665f6d7180879c9294 /sys/dev/pci | |
parent | 7d11957ea636eec5fa2901a61ea64cb29549aa35 (diff) |
new auvia and fms drivers from netbsd (testing requests sent out, since i cannot find any of these devices)
Diffstat (limited to 'sys/dev/pci')
-rw-r--r-- | sys/dev/pci/auvia.c | 969 | ||||
-rw-r--r-- | sys/dev/pci/auviavar.h | 77 | ||||
-rw-r--r-- | sys/dev/pci/files.pci | 12 | ||||
-rw-r--r-- | sys/dev/pci/fms.c | 970 | ||||
-rw-r--r-- | sys/dev/pci/fmsvar.h | 78 |
5 files changed, 2105 insertions, 1 deletions
diff --git a/sys/dev/pci/auvia.c b/sys/dev/pci/auvia.c new file mode 100644 index 00000000000..7ead2d7835c --- /dev/null +++ b/sys/dev/pci/auvia.c @@ -0,0 +1,969 @@ +/* $NetBSD: auvia.c,v 1.3.4.1 2000/06/30 16:27:49 simonb Exp $ */ + +/*- + * Copyright (c) 2000 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Tyler C. Sarna + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * VIA Technologies VT82C686A Southbridge Audio Driver + * + * Documentation links: + * + * ftp://ftp.alsa-project.org/pub/manuals/via/686a.pdf + * ftp://ftp.alsa-project.org/pub/manuals/general/ac97r21.pdf + * ftp://ftp.alsa-project.org/pub/manuals/ad/AD1881_0.pdf (example AC'97 codec) + */ + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/malloc.h> +#include <sys/device.h> +#include <sys/audioio.h> + +#include <dev/pci/pcidevs.h> +#include <dev/pci/pcivar.h> + +#include <dev/audio_if.h> +#include <dev/mulaw.h> +#include <dev/auconv.h> + +#include <dev/ic/ac97.h> + +#include <dev/pci/auviavar.h> + +struct auvia_dma { + struct auvia_dma *next; + caddr_t addr; + size_t size; + bus_dmamap_t map; + bus_dma_segment_t seg; +}; + +struct auvia_dma_op { + u_int32_t ptr; + u_int32_t flags; +#define AUVIA_DMAOP_EOL 0x80000000 +#define AUVIA_DMAOP_FLAG 0x40000000 +#define AUVIA_DMAOP_STOP 0x20000000 +#define AUVIA_DMAOP_COUNT(x) ((x)&0x00FFFFFF) +}; + +int auvia_match(struct device *, void *, void *); +void auvia_attach(struct device *, struct device *, void *); +int auvia_open(void *, int); +void auvia_close(void *); +int auvia_query_encoding(void *addr, struct audio_encoding *fp); +int auvia_set_params(void *, int, int, struct audio_params *, + struct audio_params *); +int auvia_round_blocksize(void *, int); +int auvia_halt_output(void *); +int auvia_halt_input(void *); +int auvia_getdev(void *, struct audio_device *); +int auvia_set_port(void *, mixer_ctrl_t *); +int auvia_get_port(void *, mixer_ctrl_t *); +int auvia_query_devinfo(void *, mixer_devinfo_t *); +void * auvia_malloc(void *, int, size_t, int, int); +void auvia_free(void *, void *, int); +size_t auvia_round_buffersize(void *, int, size_t); +int auvia_mappage(void *, void *, int, int); +int auvia_get_props(void *); +int auvia_build_dma_ops(struct auvia_softc *, struct auvia_softc_chan *, + struct auvia_dma *, void *, void *, int); +int auvia_trigger_output(void *, void *, void *, int, void (*)(void *), + void *, struct audio_params *); +int auvia_trigger_input(void *, void *, void *, int, void (*)(void *), + void *, struct audio_params *); + +int auvia_intr __P((void *)); + +struct cfdriver auvia_cd = { + NULL, "auvia", DV_DULL +}; + +struct cfattach auvia_ca = { + sizeof (struct auvia_softc), auvia_match, auvia_attach +}; + +#define AUVIA_PCICONF_JUNK 0x40 +#define AUVIA_PCICONF_ENABLES 0x00FF0000 /* reg 42 mask */ +#define AUVIA_PCICONF_ACLINKENAB 0x00008000 /* ac link enab */ +#define AUVIA_PCICONF_ACNOTRST 0x00004000 /* ~(ac reset) */ +#define AUVIA_PCICONF_ACSYNC 0x00002000 /* ac sync */ +#define AUVIA_PCICONF_ACVSR 0x00000800 /* var. samp. rate */ +#define AUVIA_PCICONF_ACSGD 0x00000400 /* SGD enab */ +#define AUVIA_PCICONF_ACFM 0x00000200 /* FM enab */ +#define AUVIA_PCICONF_ACSB 0x00000100 /* SB enab */ + +#define AUVIA_PLAY_STAT 0x00 +#define AUVIA_RECORD_STAT 0x10 +#define AUVIA_RPSTAT_INTR 0x03 +#define AUVIA_PLAY_CONTROL 0x01 +#define AUVIA_RECORD_CONTROL 0x11 +#define AUVIA_RPCTRL_START 0x80 +#define AUVIA_RPCTRL_TERMINATE 0x40 +#define AUVIA_PLAY_MODE 0x02 +#define AUVIA_RECORD_MODE 0x12 +#define AUVIA_RPMODE_INTR_FLAG 0x01 +#define AUVIA_RPMODE_INTR_EOL 0x02 +#define AUVIA_RPMODE_STEREO 0x10 +#define AUVIA_RPMODE_16BIT 0x20 +#define AUVIA_RPMODE_AUTOSTART 0x80 +#define AUVIA_PLAY_DMAOPS_BASE 0x04 +#define AUVIA_RECORD_DMAOPS_BASE 0x14 + +#define AUVIA_CODEC_CTL 0x80 +#define AUVIA_CODEC_READ 0x00800000 +#define AUVIA_CODEC_BUSY 0x01000000 +#define AUVIA_CODEC_PRIVALID 0x02000000 +#define AUVIA_CODEC_INDEX(x) ((x)<<16) + +#define TIMEOUT 50 + +#define AC97_REG_EXT_AUDIO_ID 0x28 +#define AC97_CODEC_DOES_VRA 0x0001 +#define AC97_REG_EXT_AUDIO_STAT 0x2A +#define AC97_ENAB_VRA 0x0001 +#define AC97_ENAB_MICVRA 0x0004 +#define AC97_REG_EXT_DAC_RATE 0x2C +#define AC97_REG_EXT_ADC_RATE 0x32 + +struct audio_hw_if auvia_hw_if = { + auvia_open, + auvia_close, + NULL, /* drain */ + auvia_query_encoding, + auvia_set_params, + auvia_round_blocksize, + NULL, /* commit_settings */ + NULL, /* init_output */ + NULL, /* init_input */ + NULL, /* start_output */ + NULL, /* start_input */ + auvia_halt_output, + auvia_halt_input, + NULL, /* speaker_ctl */ + auvia_getdev, + NULL, /* setfd */ + auvia_set_port, + auvia_get_port, + auvia_query_devinfo, + NULL, + auvia_free, + NULL, + auvia_mappage, + auvia_get_props, + auvia_trigger_output, + auvia_trigger_input, + auvia_malloc, + auvia_round_buffersize, +}; + +int auvia_attach_codec(void *, struct ac97_codec_if *); +int auvia_write_codec(void *, u_int8_t, u_int16_t); +int auvia_read_codec(void *, u_int8_t, u_int16_t *); +void auvia_reset_codec(void *); +int auvia_waitready_codec(struct auvia_softc *sc); +int auvia_waitvalid_codec(struct auvia_softc *sc); + + +int +auvia_match(struct device *parent, void *match, void *aux) +{ + struct pci_attach_args *pa = (struct pci_attach_args *) aux; + + if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_VIATECH) + return 0; + if (PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_VIATECH_VT82C686A_AC97) + return 0; + + return 1; +} + + +void +auvia_attach(struct device *parent, struct device *self, void *aux) +{ + struct pci_attach_args *pa = aux; + struct auvia_softc *sc = (struct auvia_softc *) self; + const char *intrstr = NULL; + struct mixer_ctrl ctl; + pci_chipset_tag_t pc = pa->pa_pc; + pcitag_t pt = pa->pa_tag; + pci_intr_handle_t ih; + pcireg_t pr; + u_int16_t v; + int r, i; + + r = PCI_REVISION(pa->pa_class); + sc->sc_revision[1] = '\0'; + if (r == 0x20) { + sc->sc_revision[0] = 'H'; + } else if ((r >= 0x10) && (r <= 0x14)) { + sc->sc_revision[0] = 'A' + (r - 0x10); + } else { + sprintf(sc->sc_revision, "0x%02X", r); + } + + printf(": VIA VT82C686A AC'97 Audio (rev %s)\n", + sc->sc_revision); + + if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin, pa->pa_intrline, + &ih)) { + printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname); + return; + } + intrstr = pci_intr_string(pc, ih); + + sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, auvia_intr, sc, + sc->sc_dev.dv_xname); + if (sc->sc_ih == NULL) { + printf("%s: couldn't establish interrupt",sc->sc_dev.dv_xname); + if (intrstr != NULL) + printf(" at %s", intrstr); + printf("\n"); + return; + } + + sc->sc_dmat = pa->pa_dmat; + sc->sc_pc = pc; + sc->sc_pt = pt; + + printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr); + + if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_IO, 0, &sc->sc_iot, + &sc->sc_ioh, &sc->sc_ioaddr, &sc->sc_iosize)) { + printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname); + return; + } + + /* disable SBPro compat & others */ + pr = pci_conf_read(pc, pt, AUVIA_PCICONF_JUNK); + + pr &= ~AUVIA_PCICONF_ENABLES; /* clear compat function enables */ + /* XXX what to do about MIDI, FM, joystick? */ + + pr |= (AUVIA_PCICONF_ACLINKENAB | AUVIA_PCICONF_ACNOTRST + | AUVIA_PCICONF_ACVSR | AUVIA_PCICONF_ACSGD); + + pr &= ~(AUVIA_PCICONF_ACFM | AUVIA_PCICONF_ACSB); + + pci_conf_write(pc, pt, AUVIA_PCICONF_JUNK, pr); + + sc->host_if.arg = sc; + sc->host_if.attach = auvia_attach_codec; + sc->host_if.read = auvia_read_codec; + sc->host_if.write = auvia_write_codec; + sc->host_if.reset = auvia_reset_codec; + + if ((r = ac97_attach(&sc->host_if)) != 0) { + printf("%s: can't attach codec (error 0x%X)\n", + sc->sc_dev.dv_xname, r); + return; + } + + if (auvia_read_codec(sc, AC97_REG_EXT_AUDIO_ID, &v) + || !(v & AC97_CODEC_DOES_VRA)) { + /* XXX */ + + printf("%s: codec must support AC'97 2.0 Variable Rate Audio\n", + sc->sc_dev.dv_xname); + return; + } else { + /* enable VRA */ + auvia_write_codec(sc, AC97_REG_EXT_AUDIO_STAT, + AC97_ENAB_VRA | AC97_ENAB_MICVRA); + } + + /* disable mutes */ + for (i = 0; i < 4; i++) { + static struct { + char *class, *device; + } d[] = { + { AudioCoutputs, AudioNmaster}, + { AudioCinputs, AudioNdac}, + { AudioCinputs, AudioNcd}, + { AudioCrecord, AudioNvolume}, + }; + + ctl.type = AUDIO_MIXER_ENUM; + ctl.un.ord = 0; + + ctl.dev = sc->codec_if->vtbl->get_portnum_by_name(sc->codec_if, + d[i].class, d[i].device, AudioNmute); + auvia_set_port(sc, &ctl); + } + + /* set a reasonable default volume */ + + ctl.type = AUDIO_MIXER_VALUE; + ctl.un.value.num_channels = 2; + ctl.un.value.level[AUDIO_MIXER_LEVEL_LEFT] = \ + ctl.un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = 199; + + ctl.dev = sc->codec_if->vtbl->get_portnum_by_name(sc->codec_if, + AudioCoutputs, AudioNmaster, NULL); + auvia_set_port(sc, &ctl); + + audio_attach_mi(&auvia_hw_if, sc, &sc->sc_dev); +} + + +int +auvia_attach_codec(void *addr, struct ac97_codec_if *cif) +{ + struct auvia_softc *sc = addr; + + sc->codec_if = cif; + + return 0; +} + + +void +auvia_reset_codec(void *addr) +{ +#ifdef notyet /* XXX seems to make codec become unready... ??? */ + struct auvia_softc *sc = addr; + pcireg_t r; + + /* perform a codec cold reset */ + + r = pci_conf_read(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK); + + r &= ~AUVIA_PCICONF_ACNOTRST; /* enable RESET (active low) */ + pci_conf_write(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK, r); + delay(2); + + r |= AUVIA_PCICONF_ACNOTRST; /* disable RESET (inactive high) */ + pci_conf_write(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK, r); + delay(200); + + auvia_waitready_codec(sc); +#endif +} + + +int +auvia_waitready_codec(struct auvia_softc *sc) +{ + int i; + + /* poll until codec not busy */ + for (i = 0; (i < TIMEOUT) && (bus_space_read_4(sc->sc_iot, sc->sc_ioh, + AUVIA_CODEC_CTL) & AUVIA_CODEC_BUSY); i++) + delay(1); + if (i >= TIMEOUT) { + printf("%s: codec busy\n", sc->sc_dev.dv_xname); + return 1; + } + + return 0; +} + + +int +auvia_waitvalid_codec(struct auvia_softc *sc) +{ + int i; + + /* poll until codec valid */ + for (i = 0; (i < TIMEOUT) && !(bus_space_read_4(sc->sc_iot, sc->sc_ioh, + AUVIA_CODEC_CTL) & AUVIA_CODEC_PRIVALID); i++) + delay(1); + if (i >= TIMEOUT) { + printf("%s: codec invalid\n", sc->sc_dev.dv_xname); + return 1; + } + + return 0; +} + + +int +auvia_write_codec(void *addr, u_int8_t reg, u_int16_t val) +{ + struct auvia_softc *sc = addr; + + if (auvia_waitready_codec(sc)) + return 1; + + bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL, + AUVIA_CODEC_PRIVALID | AUVIA_CODEC_INDEX(reg) | val); + + return 0; +} + + +int +auvia_read_codec(void *addr, u_int8_t reg, u_int16_t *val) +{ + struct auvia_softc *sc = addr; + + if (auvia_waitready_codec(sc)) + return 1; + + bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL, + AUVIA_CODEC_PRIVALID | AUVIA_CODEC_READ | AUVIA_CODEC_INDEX(reg)); + + if (auvia_waitready_codec(sc)) + return 1; + + if (auvia_waitvalid_codec(sc)) + return 1; + + *val = bus_space_read_2(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL); + + return 0; +} + + +int +auvia_open(void *addr, int flags) +{ + return 0; +} + + +void +auvia_close(void *addr) +{ + struct auvia_softc *sc = addr; + + auvia_halt_output(sc); + auvia_halt_input(sc); + + sc->sc_play.sc_intr = NULL; + sc->sc_record.sc_intr = NULL; +} + + +int +auvia_query_encoding(void *addr, struct audio_encoding *fp) +{ + switch (fp->index) { + case 0: + strcpy(fp->name, AudioEulinear); + fp->encoding = AUDIO_ENCODING_ULINEAR; + fp->precision = 8; + fp->flags = 0; + return (0); + case 1: + strcpy(fp->name, AudioEmulaw); + fp->encoding = AUDIO_ENCODING_ULAW; + fp->precision = 8; + fp->flags = AUDIO_ENCODINGFLAG_EMULATED; + return (0); + case 2: + strcpy(fp->name, AudioEalaw); + fp->encoding = AUDIO_ENCODING_ALAW; + fp->precision = 8; + fp->flags = AUDIO_ENCODINGFLAG_EMULATED; + return (0); + case 3: + strcpy(fp->name, AudioEslinear); + fp->encoding = AUDIO_ENCODING_SLINEAR; + fp->precision = 8; + fp->flags = AUDIO_ENCODINGFLAG_EMULATED; + return (0); + case 4: + strcpy(fp->name, AudioEslinear_le); + fp->encoding = AUDIO_ENCODING_SLINEAR_LE; + fp->precision = 16; + fp->flags = 0; + return (0); + case 5: + strcpy(fp->name, AudioEulinear_le); + fp->encoding = AUDIO_ENCODING_ULINEAR_LE; + fp->precision = 16; + fp->flags = AUDIO_ENCODINGFLAG_EMULATED; + return (0); + case 6: + strcpy(fp->name, AudioEslinear_be); + fp->encoding = AUDIO_ENCODING_SLINEAR_BE; + fp->precision = 16; + fp->flags = AUDIO_ENCODINGFLAG_EMULATED; + return (0); + case 7: + strcpy(fp->name, AudioEulinear_be); + fp->encoding = AUDIO_ENCODING_ULINEAR_BE; + fp->precision = 16; + fp->flags = AUDIO_ENCODINGFLAG_EMULATED; + return (0); + default: + return (EINVAL); + } +} + + +int +auvia_set_params(void *addr, int setmode, int usemode, + struct audio_params *play, struct audio_params *rec) +{ + struct auvia_softc *sc = addr; + struct audio_params *p; + u_int16_t regval; + int reg, mode; + + /* for mode in (RECORD, PLAY) */ + for (mode = AUMODE_RECORD; mode != -1; + mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) { + if ((setmode & mode) == 0) + continue; + + p = mode == AUMODE_PLAY ? play : rec; + + if (p->sample_rate < 4000 || p->sample_rate > 48000 || + (p->precision != 8 && p->precision != 16) || + (p->channels != 1 && p->channels != 2)) + return (EINVAL); + + reg = mode == AUMODE_PLAY ? + AC97_REG_EXT_DAC_RATE : AC97_REG_EXT_ADC_RATE; + + auvia_write_codec(sc, reg, (u_int16_t) p->sample_rate); + auvia_read_codec(sc, reg, ®val); + p->sample_rate = regval; + + p->factor = 1; + p->sw_code = 0; + switch (p->encoding) { + case AUDIO_ENCODING_SLINEAR_BE: + if (p->precision == 16) + p->sw_code = swap_bytes; + else + p->sw_code = change_sign8; + break; + case AUDIO_ENCODING_SLINEAR_LE: + if (p->precision != 16) + p->sw_code = change_sign8; + break; + case AUDIO_ENCODING_ULINEAR_BE: + if (p->precision == 16) { + if (mode == AUMODE_PLAY) + p->sw_code = swap_bytes_change_sign16_le; + else + p->sw_code = change_sign16_swap_bytes_le; + } + break; + case AUDIO_ENCODING_ULINEAR_LE: + if (p->precision == 16) + p->sw_code = change_sign16_le; + break; + case AUDIO_ENCODING_ULAW: + if (mode == AUMODE_PLAY) { + p->factor = 2; + p->sw_code = mulaw_to_slinear16_le; + } else + p->sw_code = ulinear8_to_mulaw; + break; + case AUDIO_ENCODING_ALAW: + if (mode == AUMODE_PLAY) { + p->factor = 2; + p->sw_code = alaw_to_slinear16_le; + } else + p->sw_code = ulinear8_to_alaw; + break; + default: + return (EINVAL); + } + + regval = (p->channels == 2 ? AUVIA_RPMODE_STEREO : 0) + | (p->precision * p->factor == 16 ? + AUVIA_RPMODE_16BIT : 0) + | AUVIA_RPMODE_INTR_FLAG | AUVIA_RPMODE_INTR_EOL + | AUVIA_RPMODE_AUTOSTART; + + if (mode == AUMODE_PLAY) { + sc->sc_play.sc_reg = regval; + } else { + sc->sc_record.sc_reg = regval; + } + } + + return 0; +} + + +int +auvia_round_blocksize(void *addr, int blk) +{ + return (blk & -32); +} + + +int +auvia_halt_output(void *addr) +{ + struct auvia_softc *sc = addr; + + bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_PLAY_CONTROL, + AUVIA_RPCTRL_TERMINATE); + + return 0; +} + + +int +auvia_halt_input(void *addr) +{ + struct auvia_softc *sc = addr; + + bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_RECORD_CONTROL, + AUVIA_RPCTRL_TERMINATE); + + return 0; +} + + +int +auvia_getdev(void *addr, struct audio_device *retp) +{ + struct auvia_softc *sc = addr; + + if (retp) { + strncpy(retp->name, "VIA VT82C686A", sizeof(retp->name)); + strncpy(retp->version, sc->sc_revision, sizeof(retp->version)); + strncpy(retp->config, "auvia", sizeof(retp->config)); + } + + return 0; +} + + +int +auvia_set_port(void *addr, mixer_ctrl_t *cp) +{ + struct auvia_softc *sc = addr; + + return (sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp)); +} + + +int +auvia_get_port(void *addr, mixer_ctrl_t *cp) +{ + struct auvia_softc *sc = addr; + + return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp)); +} + + +int +auvia_query_devinfo(void *addr, mixer_devinfo_t *dip) +{ + struct auvia_softc *sc = addr; + + return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip)); +} + + +void * +auvia_malloc(void *addr, int direction, size_t size, int pool, int flags) +{ + struct auvia_softc *sc = addr; + struct auvia_dma *p; + int error; + int rseg; + + p = malloc(sizeof(*p), pool, flags); + if (!p) + return 0; + + p->size = size; + if ((error = bus_dmamem_alloc(sc->sc_dmat, size, NBPG, 0, &p->seg, 1, + &rseg, BUS_DMA_NOWAIT)) != 0) { + printf("%s: unable to allocate dma, error = %d\n", + sc->sc_dev.dv_xname, error); + goto fail_alloc; + } + + if ((error = bus_dmamem_map(sc->sc_dmat, &p->seg, rseg, size, &p->addr, + BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) { + printf("%s: unable to map dma, error = %d\n", + sc->sc_dev.dv_xname, error); + goto fail_map; + } + + if ((error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0, + BUS_DMA_NOWAIT, &p->map)) != 0) { + printf("%s: unable to create dma map, error = %d\n", + sc->sc_dev.dv_xname, error); + goto fail_create; + } + + if ((error = bus_dmamap_load(sc->sc_dmat, p->map, p->addr, size, NULL, + BUS_DMA_NOWAIT)) != 0) { + printf("%s: unable to load dma map, error = %d\n", + sc->sc_dev.dv_xname, error); + goto fail_load; + } + + p->next = sc->sc_dmas; + sc->sc_dmas = p; + + return p->addr; + + +fail_load: + bus_dmamap_destroy(sc->sc_dmat, p->map); +fail_create: + bus_dmamem_unmap(sc->sc_dmat, p->addr, size); +fail_map: + bus_dmamem_free(sc->sc_dmat, &p->seg, 1); +fail_alloc: + free(p, pool); + return 0; +} + + +void +auvia_free(void *addr, void *ptr, int pool) +{ + struct auvia_softc *sc = addr; + struct auvia_dma **pp, *p; + + for (pp = &(sc->sc_dmas); (p = *pp) != NULL; pp = &p->next) + if (p->addr == ptr) { + bus_dmamap_unload(sc->sc_dmat, p->map); + bus_dmamap_destroy(sc->sc_dmat, p->map); + bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size); + bus_dmamem_free(sc->sc_dmat, &p->seg, 1); + + *pp = p->next; + free(p, pool); + return; + } + + panic("auvia_free: trying to free unallocated memory"); +} + + +size_t +auvia_round_buffersize(void *addr, int direction, size_t size) +{ + return size; +} + + +int +auvia_mappage(void *addr, void *mem, int off, int prot) +{ + struct auvia_softc *sc = addr; + struct auvia_dma *p; + + if (off < 0) + return -1; + + for (p = sc->sc_dmas; p && p->addr != mem; p = p->next) + ; + + if (!p) + return -1; + + return bus_dmamem_mmap(sc->sc_dmat, &p->seg, 1, off, prot, + BUS_DMA_WAITOK); +} + + +int +auvia_get_props(void *addr) +{ + return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT + | AUDIO_PROP_FULLDUPLEX; +} + + +int +auvia_build_dma_ops(struct auvia_softc *sc, struct auvia_softc_chan *ch, + struct auvia_dma *p, void *start, void *end, int blksize) +{ + struct auvia_dma_op *op; + struct auvia_dma *dp; + bus_addr_t s, e; + size_t l; + int segs; + + s = p->map->dm_segs[0].ds_addr; + l = ((char *)end - (char *)start); + e = s + l; + segs = (l + blksize - 1) / blksize; + + if (segs > (ch->sc_dma_op_count)) { + /* if old list was too small, free it */ + if (ch->sc_dma_ops) { + auvia_free(sc, ch->sc_dma_ops, M_DEVBUF); + } + + ch->sc_dma_ops = auvia_malloc(sc, 0, + sizeof(struct auvia_dma_op) * segs, M_DEVBUF, M_WAITOK); + + if (ch->sc_dma_ops == NULL) { + printf("%s: couldn't build dmaops\n", sc->sc_dev.dv_xname); + return 1; + } + + for (dp = sc->sc_dmas; + dp && dp->addr != (void *)(ch->sc_dma_ops); + dp = dp->next) + ; + + if (!dp) + panic("%s: build_dma_ops: where'd my memory go??? " + "address (%p)\n", sc->sc_dev.dv_xname, + ch->sc_dma_ops); + + ch->sc_dma_op_count = segs; + ch->sc_dma_ops_dma = dp; + } + + dp = ch->sc_dma_ops_dma; + op = ch->sc_dma_ops; + + while (l) { + op->ptr = s; + l = l - blksize; + if (!l) { + /* if last block */ + op->flags = AUVIA_DMAOP_EOL | blksize; + } else { + op->flags = AUVIA_DMAOP_FLAG | blksize; + } + s += blksize; + op++; + } + + return 0; +} + + +int +auvia_trigger_output(void *addr, void *start, void *end, + int blksize, void (*intr)(void *), void *arg, + struct audio_params *param) +{ + struct auvia_softc *sc = addr; + struct auvia_softc_chan *ch = &(sc->sc_play); + struct auvia_dma *p; + + for (p = sc->sc_dmas; p && p->addr != start; p = p->next) + ; + + if (!p) + panic("auvia_trigger_output: request with bad start " + "address (%p)\n", start); + + if (auvia_build_dma_ops(sc, ch, p, start, end, blksize)) { + return 1; + } + + ch->sc_intr = intr; + ch->sc_arg = arg; + + bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_PLAY_DMAOPS_BASE, + ch->sc_dma_ops_dma->map->dm_segs[0].ds_addr); + + bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_PLAY_MODE, + ch->sc_reg); + + bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_PLAY_CONTROL, + AUVIA_RPCTRL_START); + + return 0; +} + + +int +auvia_trigger_input(void *addr, void *start, void *end, + int blksize, void (*intr)(void *), void *arg, + struct audio_params *param) +{ + struct auvia_softc *sc = addr; + struct auvia_softc_chan *ch = &(sc->sc_record); + struct auvia_dma *p; + + for (p = sc->sc_dmas; p && p->addr != start; p = p->next) + ; + + if (!p) + panic("auvia_trigger_input: request with bad start " + "address (%p)\n", start); + + if (auvia_build_dma_ops(sc, ch, p, start, end, blksize)) { + return 1; + } + + ch->sc_intr = intr; + ch->sc_arg = arg; + + bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_RECORD_DMAOPS_BASE, + ch->sc_dma_ops_dma->map->dm_segs[0].ds_addr); + + bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_RECORD_MODE, + ch->sc_reg); + + bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_RECORD_CONTROL, + AUVIA_RPCTRL_START); + + return 0; +} + + +int +auvia_intr(void *arg) +{ + struct auvia_softc *sc = arg; + u_int8_t r; + + r = bus_space_read_1(sc->sc_iot, sc->sc_ioh, AUVIA_RECORD_STAT); + if (r & AUVIA_RPSTAT_INTR) { + if (sc->sc_record.sc_intr) + sc->sc_record.sc_intr(sc->sc_record.sc_arg); + + /* clear interrupts */ + bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_RECORD_STAT, + AUVIA_RPSTAT_INTR); + } + r = bus_space_read_1(sc->sc_iot, sc->sc_ioh, AUVIA_PLAY_STAT); + if (r & AUVIA_RPSTAT_INTR) { + if (sc->sc_play.sc_intr) + sc->sc_play.sc_intr(sc->sc_play.sc_arg); + + /* clear interrupts */ + bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_PLAY_STAT, + AUVIA_RPSTAT_INTR); + } + + return 1; +} diff --git a/sys/dev/pci/auviavar.h b/sys/dev/pci/auviavar.h new file mode 100644 index 00000000000..e5f23b65ac5 --- /dev/null +++ b/sys/dev/pci/auviavar.h @@ -0,0 +1,77 @@ +/* $NetBSD: auviavar.h,v 1.1 2000/03/31 04:45:29 tsarna Exp $ */ + +/*- + * Copyright (c) 2000 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Tyler C. Sarna. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + + +#ifndef _DEV_PCI_AUVIAVAR_H_ +#define _DEV_PCI_AUVIAVAR_H_ + +struct auvia_softc_chan { + void (*sc_intr)(void *); + void *sc_arg; + + struct auvia_dma_op *sc_dma_ops; + struct auvia_dma *sc_dma_ops_dma; + u_int16_t sc_dma_op_count; + u_int16_t sc_reg; +}; + +struct auvia_softc { + struct device sc_dev; + + char sc_revision[8]; + + void *sc_ih; /* interrupt handle */ + + pci_chipset_tag_t sc_pc; + pcitag_t sc_pt; + + bus_space_tag_t sc_iot; + bus_space_handle_t sc_ioh; + bus_addr_t sc_ioaddr; + bus_size_t sc_iosize; + bus_dma_tag_t sc_dmat; + + struct ac97_host_if host_if; + struct ac97_codec_if *codec_if; + + struct auvia_dma *sc_dmas; + + struct auvia_softc_chan sc_play, sc_record; +}; + +#endif diff --git a/sys/dev/pci/files.pci b/sys/dev/pci/files.pci index dd97e0fa83c..103da7c300d 100644 --- a/sys/dev/pci/files.pci +++ b/sys/dev/pci/files.pci @@ -1,4 +1,4 @@ -# $OpenBSD: files.pci,v 1.78 2000/07/03 19:30:22 mickey Exp $ +# $OpenBSD: files.pci,v 1.79 2000/07/20 21:45:19 deraadt Exp $ # $NetBSD: files.pci,v 1.20 1996/09/24 17:47:15 christos Exp $ # # Config file and device description for machine-independent PCI code. @@ -90,6 +90,16 @@ device clcs: audio, auconv, mulaw, ac97 attach clcs at pci file dev/pci/cs4280.c clcs +# Forte Media FM801 +device fms: audio, auconv, mulaw, ac97, midibus +attach fms at pci +file dev/pci/fms.c fms + +# VIA VT82C686A AC'97 Audio +device auvia: audio, auconv, mulaw, ac97 +attach auvia at pci +file dev/pci/auvia.c auvia + # ICP Vortex GDT PCI RAID controllers # device declaration in sys/conf/files attach gdt at pci with gdt_pci diff --git a/sys/dev/pci/fms.c b/sys/dev/pci/fms.c new file mode 100644 index 00000000000..cfe242b0d8c --- /dev/null +++ b/sys/dev/pci/fms.c @@ -0,0 +1,970 @@ +/* $NetBSD: fms.c,v 1.5.4.1 2000/06/30 16:27:50 simonb Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Witold J. Wnuk. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * Forte Media FM801 Audio Device Driver + */ + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/kernel.h> +#include <sys/malloc.h> +#include <sys/device.h> +#include <sys/audioio.h> + +#include <machine/bus.h> +#include <machine/cpu.h> + +#include <dev/pci/pcidevs.h> +#include <dev/pci/pcivar.h> + +#include <dev/audio_if.h> +#include <dev/mulaw.h> +#include <dev/auconv.h> + +#include <dev/ic/ac97.h> +#if 0 +#include <dev/ic/mpuvar.h> +#endif + +#include <dev/pci/fmsvar.h> + + +struct fms_dma { + struct fms_dma *next; + caddr_t addr; + size_t size; + bus_dmamap_t map; + bus_dma_segment_t seg; +}; + + + +int fms_match __P((struct device *, void *, void *)); +void fms_attach __P((struct device *, struct device *, void *)); +int fms_intr __P((void *)); + +int fms_open __P((void *, int)); +void fms_close __P((void *)); +int fms_query_encoding __P((void *, struct audio_encoding *)); +int fms_set_params __P((void *, int, int, struct audio_params *, + struct audio_params *)); +int fms_round_blocksize __P((void *, int)); +int fms_halt_output __P((void *)); +int fms_halt_input __P((void *)); +int fms_getdev __P((void *, struct audio_device *)); +int fms_set_port __P((void *, mixer_ctrl_t *)); +int fms_get_port __P((void *, mixer_ctrl_t *)); +int fms_query_devinfo __P((void *, mixer_devinfo_t *)); +void *fms_malloc __P((void *, int, size_t, int, int)); +void fms_free __P((void *, void *, int)); +size_t fms_round_buffersize __P((void *, int, size_t)); +int fms_mappage __P((void *, void *, int, int)); +int fms_get_props __P((void *)); +int fms_trigger_output __P((void *, void *, void *, int, void (*)(void *), + void *, struct audio_params *)); +int fms_trigger_input __P((void *, void *, void *, int, void (*)(void *), + void *, struct audio_params *)); + +struct cfdriver fms_cd = { + NULL, "fms", DV_DULL +}; + +struct cfattach fms_ca = { + sizeof (struct fms_softc), fms_match, fms_attach +}; + +struct audio_device fms_device = { + "Forte Media 801", + "1.0", + "fms" +}; + + +struct audio_hw_if fms_hw_if = { + fms_open, + fms_close, + NULL, + fms_query_encoding, + fms_set_params, + fms_round_blocksize, + NULL, + NULL, + NULL, + NULL, + NULL, + fms_halt_output, + fms_halt_input, + NULL, + fms_getdev, + NULL, + fms_set_port, + fms_get_port, + fms_query_devinfo, + NULL, + fms_free, + NULL, + fms_mappage, + fms_get_props, + fms_trigger_output, + fms_trigger_input, + fms_malloc, + fms_round_buffersize, +}; + +int fms_attach_codec __P((void *, struct ac97_codec_if *)); +int fms_read_codec __P((void *, u_int8_t, u_int16_t *)); +int fms_write_codec __P((void *, u_int8_t, u_int16_t)); +void fms_reset_codec __P((void *)); + +int fms_allocmem __P((struct fms_softc *, size_t, size_t, + struct fms_dma *)); +int fms_freemem __P((struct fms_softc *, struct fms_dma *)); + +#define FM_PCM_VOLUME 0x00 +#define FM_FM_VOLUME 0x02 +#define FM_I2S_VOLUME 0x04 +#define FM_RECORD_SOURCE 0x06 + +#define FM_PLAY_CTL 0x08 +#define FM_PLAY_RATE_MASK 0x0f00 +#define FM_PLAY_BUF1_LAST 0x0001 +#define FM_PLAY_BUF2_LAST 0x0002 +#define FM_PLAY_START 0x0020 +#define FM_PLAY_PAUSE 0x0040 +#define FM_PLAY_STOPNOW 0x0080 +#define FM_PLAY_16BIT 0x4000 +#define FM_PLAY_STEREO 0x8000 + +#define FM_PLAY_DMALEN 0x0a +#define FM_PLAY_DMABUF1 0x0c +#define FM_PLAY_DMABUF2 0x10 + + +#define FM_REC_CTL 0x14 +#define FM_REC_RATE_MASK 0x0f00 +#define FM_REC_BUF1_LAST 0x0001 +#define FM_REC_BUF2_LAST 0x0002 +#define FM_REC_START 0x0020 +#define FM_REC_PAUSE 0x0040 +#define FM_REC_STOPNOW 0x0080 +#define FM_REC_16BIT 0x4000 +#define FM_REC_STEREO 0x8000 + + +#define FM_REC_DMALEN 0x16 +#define FM_REC_DMABUF1 0x18 +#define FM_REC_DMABUF2 0x1c + +#define FM_CODEC_CTL 0x22 +#define FM_VOLUME 0x26 +#define FM_VOLUME_MUTE 0x8000 + +#define FM_CODEC_CMD 0x2a +#define FM_CODEC_CMD_READ 0x0080 +#define FM_CODEC_CMD_VALID 0x0100 +#define FM_CODEC_CMD_BUSY 0x0200 + +#define FM_CODEC_DATA 0x2c + +#define FM_IO_CTL 0x52 +#define FM_CARD_CTL 0x54 + +#define FM_INTMASK 0x56 +#define FM_INTMASK_PLAY 0x0001 +#define FM_INTMASK_REC 0x0002 +#define FM_INTMASK_VOL 0x0040 +#define FM_INTMASK_MPU 0x0080 + +#define FM_INTSTATUS 0x5a +#define FM_INTSTATUS_PLAY 0x0100 +#define FM_INTSTATUS_REC 0x0200 +#define FM_INTSTATUS_VOL 0x4000 +#define FM_INTSTATUS_MPU 0x8000 + + + +int +fms_match(parent, match, aux) + struct device *parent; + void *match; + void *aux; +{ + struct pci_attach_args *pa = (struct pci_attach_args *) aux; + + if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_FORTEMEDIA) + return 0; + if (PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_FORTEMEDIA_FM801) + return 0; + + return 1; +} + +void +fms_attach(parent, self, aux) + struct device *parent; + struct device *self; + void *aux; +{ + struct pci_attach_args *pa = aux; + struct fms_softc *sc = (struct fms_softc *) self; + struct audio_attach_args aa; + const char *intrstr = NULL; + pci_chipset_tag_t pc = pa->pa_pc; + pcitag_t pt = pa->pa_tag; + pci_intr_handle_t ih; + int i; + + u_int16_t k1; + + printf(": Forte Media FM-801\n"); + + if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin, pa->pa_intrline, + &ih)) { + printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname); + return; + } + intrstr = pci_intr_string(pc, ih); + + sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, fms_intr, sc, + sc->sc_dev.dv_xname); + if (sc->sc_ih == NULL) { + printf("%s: couldn't establish interrupt",sc->sc_dev.dv_xname); + if (intrstr != NULL) + printf(" at %s", intrstr); + printf("\n"); + return; + } + + sc->sc_dmat = pa->pa_dmat; + + printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr); + + if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_IO, 0, &sc->sc_iot, + &sc->sc_ioh, &sc->sc_ioaddr, &sc->sc_iosize)) { + printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname); + return; + } + + if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 0x30, 2, + &sc->sc_mpu_ioh)) + panic("fms_attach: can't get mpu subregion handle"); + + if (bus_space_subregion(sc->sc_iot, sc->sc_ioh, 0x68, 4, + &sc->sc_opl_ioh)) + panic("fms_attach: can't get opl subregion handle"); + + /* Disable legacy audio (SBPro compatibility) */ + pci_conf_write(pc, pt, 0x40, 0); + + /* Reset codec and AC'97 */ + bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CTL, 0x0020); + delay(2); /* > 1us according to AC'97 documentation */ + bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CTL, 0x0000); + delay(1); /* > 168.2ns according to AC'97 documentation */ + + /* Set up volume */ + bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_PCM_VOLUME, 0x0808); + bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_FM_VOLUME, 0x0808); + bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_I2S_VOLUME, 0x0808); + + bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_RECORD_SOURCE, 0x0000); + + /* Unmask playback, record and mpu interrupts, mask the rest */ + k1 = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_INTMASK); + bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_INTMASK, + (k1 & ~(FM_INTMASK_PLAY | FM_INTMASK_REC | FM_INTMASK_MPU)) | + FM_INTMASK_VOL); + bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_INTSTATUS, + FM_INTSTATUS_PLAY | FM_INTSTATUS_REC | FM_INTSTATUS_MPU | + FM_INTSTATUS_VOL); + + sc->host_if.arg = sc; + sc->host_if.attach = fms_attach_codec; + sc->host_if.read = fms_read_codec; + sc->host_if.write = fms_write_codec; + sc->host_if.reset = fms_reset_codec; + + if (ac97_attach(&sc->host_if) != 0) + return; + + /* Turn mute off */ + for (i = 0; i < 3; i++) { + static struct { + char *class, *device; + } d[] = { + { AudioCoutputs, AudioNmaster }, + { AudioCinputs, AudioNdac }, + { AudioCrecord, AudioNvolume } + }; + struct mixer_ctrl ctl; + + ctl.type = AUDIO_MIXER_ENUM; + ctl.un.ord = 0; + ctl.dev = sc->codec_if->vtbl->get_portnum_by_name(sc->codec_if, + d[i].class, d[i].device, AudioNmute); + fms_set_port(sc, &ctl); + } + + audio_attach_mi(&fms_hw_if, sc, &sc->sc_dev); + + aa.type = AUDIODEV_TYPE_OPL; + aa.hwif = NULL; + aa.hdl = NULL; + config_found(&sc->sc_dev, &aa, audioprint); + + aa.type = AUDIODEV_TYPE_MPU; + aa.hwif = NULL; + aa.hdl = NULL; + sc->sc_mpu_dev = config_found(&sc->sc_dev, &aa, audioprint); +} + +/* + * Each AC-link frame takes 20.8us, data should be ready in next frame, + * we allow more than two. + */ +#define TIMO 50 +int +fms_read_codec(addr, reg, val) + void *addr; + u_int8_t reg; + u_int16_t *val; +{ + struct fms_softc *sc = addr; + int i; + + /* Poll until codec is ready */ + for (i = 0; i < TIMO && bus_space_read_2(sc->sc_iot, sc->sc_ioh, + FM_CODEC_CMD) & FM_CODEC_CMD_BUSY; i++) + delay(1); + if (i >= TIMO) { + printf("fms: codec busy\n"); + return 1; + } + + /* Write register index, read access */ + bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CMD, + reg | FM_CODEC_CMD_READ); + + /* Poll until we have valid data */ + for (i = 0; i < TIMO && !(bus_space_read_2(sc->sc_iot, sc->sc_ioh, + FM_CODEC_CMD) & FM_CODEC_CMD_VALID); i++) + delay(1); + if (i >= TIMO) { + printf("fms: no data from codec\n"); + return 1; + } + + /* Read data */ + *val = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_DATA); + return 0; +} + +int +fms_write_codec(addr, reg, val) + void *addr; + u_int8_t reg; + u_int16_t val; +{ + struct fms_softc *sc = addr; + int i; + + /* Poll until codec is ready */ + for (i = 0; i < TIMO && bus_space_read_2(sc->sc_iot, sc->sc_ioh, + FM_CODEC_CMD) & FM_CODEC_CMD_BUSY; i++) + delay(1); + if (i >= TIMO) { + printf("fms: codec busy\n"); + return 1; + } + + /* Write data */ + bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_DATA, val); + /* Write index register, write access */ + bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CMD, reg); + return 0; +} +#undef TIMO + +int +fms_attach_codec(addr, cif) + void *addr; + struct ac97_codec_if *cif; +{ + struct fms_softc *sc = addr; + + sc->codec_if = cif; + return 0; +} + +/* Cold Reset */ +void +fms_reset_codec(addr) + void *addr; +{ + struct fms_softc *sc = addr; + bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CTL, 0x0020); + delay(2); + bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_CODEC_CTL, 0x0000); + delay(1); +} + +int +fms_intr(arg) + void *arg; +{ + struct fms_softc *sc = arg; + u_int16_t istat; + + istat = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_INTSTATUS); + + if (istat & FM_INTSTATUS_PLAY) { + if ((sc->sc_play_nextblk += sc->sc_play_blksize) >= + sc->sc_play_end) + sc->sc_play_nextblk = sc->sc_play_start; + + bus_space_write_4(sc->sc_iot, sc->sc_ioh, + sc->sc_play_flip++ & 1 ? + FM_PLAY_DMABUF2 : FM_PLAY_DMABUF1, sc->sc_play_nextblk); + + if (sc->sc_pintr) + sc->sc_pintr(sc->sc_parg); + else + printf("unexpected play intr\n"); + } + + if (istat & FM_INTSTATUS_REC) { + if ((sc->sc_rec_nextblk += sc->sc_rec_blksize) >= + sc->sc_rec_end) + sc->sc_rec_nextblk = sc->sc_rec_start; + + bus_space_write_4(sc->sc_iot, sc->sc_ioh, + sc->sc_rec_flip++ & 1 ? + FM_REC_DMABUF2 : FM_REC_DMABUF1, sc->sc_rec_nextblk); + + if (sc->sc_rintr) + sc->sc_rintr(sc->sc_rarg); + else + printf("unexpected rec intr\n"); + } + +#if 0 + if (istat & FM_INTSTATUS_MPU) + mpu_intr(sc->sc_mpu_dev); +#endif + + bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_INTSTATUS, + istat & (FM_INTSTATUS_PLAY | FM_INTSTATUS_REC)); + + return 1; +} + +int +fms_open(addr, flags) + void *addr; + int flags; +{ + /* UNUSED struct fms_softc *sc = addr;*/ + + return 0; +} + +void +fms_close(addr) + void *addr; +{ + /* UNUSED struct fms_softc *sc = addr;*/ +} + +int +fms_query_encoding(addr, fp) + void *addr; + struct audio_encoding *fp; +{ + + switch (fp->index) { + case 0: + strcpy(fp->name, AudioEmulaw); + fp->encoding = AUDIO_ENCODING_ULAW; + fp->precision = 8; + fp->flags = AUDIO_ENCODINGFLAG_EMULATED; + return 0; + case 1: + strcpy(fp->name, AudioEslinear_le); + fp->encoding = AUDIO_ENCODING_SLINEAR_LE; + fp->precision = 16; + fp->flags = 0; + return 0; + case 2: + strcpy(fp->name, AudioEulinear); + fp->encoding = AUDIO_ENCODING_ULINEAR; + fp->precision = 8; + fp->flags = 0; + return 0; + case 3: + strcpy(fp->name, AudioEalaw); + fp->encoding = AUDIO_ENCODING_ALAW; + fp->precision = 8; + fp->flags = AUDIO_ENCODINGFLAG_EMULATED; + return 0; + case 4: + strcpy(fp->name, AudioEulinear_le); + fp->encoding = AUDIO_ENCODING_ULINEAR_LE; + fp->precision = 16; + fp->flags = AUDIO_ENCODINGFLAG_EMULATED; + return 0; + case 5: + strcpy(fp->name, AudioEslinear); + fp->encoding = AUDIO_ENCODING_SLINEAR; + fp->precision = 8; + fp->flags = AUDIO_ENCODINGFLAG_EMULATED; + return 0; + case 6: + strcpy(fp->name, AudioEulinear_be); + fp->encoding = AUDIO_ENCODING_ULINEAR_BE; + fp->precision = 16; + fp->flags = AUDIO_ENCODINGFLAG_EMULATED; + return 0; + case 7: + strcpy(fp->name, AudioEslinear_be); + fp->encoding = AUDIO_ENCODING_SLINEAR_BE; + fp->precision = 16; + fp->flags = AUDIO_ENCODINGFLAG_EMULATED; + return 0; + default: + return EINVAL; + } +} + +/* + * Range below -limit- is set to -rate- + * What a pity FM801 does not have 24000 + * 24000 -> 22050 sounds rather poor + */ +struct { + int limit; + int rate; +} fms_rates[11] = { + { 6600, 5500 }, + { 8750, 8000 }, + { 10250, 9600 }, + { 13200, 11025 }, + { 17500, 16000 }, + { 20500, 19200 }, + { 26500, 22050 }, + { 35000, 32000 }, + { 41000, 38400 }, + { 46000, 44100 }, + { 48000, 48000 }, + /* anything above -> 48000 */ +}; + +int +fms_set_params(addr, setmode, usemode, play, rec) + void *addr; + int setmode, usemode; + struct audio_params *play, *rec; +{ + struct fms_softc *sc = addr; + int i; + + if (setmode & AUMODE_PLAY) { + play->factor = 1; + play->sw_code = 0; + switch(play->encoding) { + case AUDIO_ENCODING_ULAW: + play->factor = 2; + play->sw_code = mulaw_to_slinear16_le; + break; + case AUDIO_ENCODING_SLINEAR_LE: + if (play->precision == 8) + play->sw_code = change_sign8; + break; + case AUDIO_ENCODING_ULINEAR_LE: + if (play->precision == 16) + play->sw_code = change_sign16_le; + break; + case AUDIO_ENCODING_ALAW: + play->factor = 2; + play->sw_code = alaw_to_slinear16_le; + break; + case AUDIO_ENCODING_SLINEAR_BE: + if (play->precision == 16) + play->sw_code = swap_bytes; + else + play->sw_code = change_sign8; + break; + case AUDIO_ENCODING_ULINEAR_BE: + if (play->precision == 16) + play->sw_code = change_sign16_swap_bytes_le; + break; + default: + return EINVAL; + } + for (i = 0; i < 10 && play->sample_rate > fms_rates[i].limit; + i++) + ; + play->sample_rate = fms_rates[i].rate; + sc->sc_play_reg = (play->channels == 2 ? FM_PLAY_STEREO : 0) | + (play->precision * play->factor == 16 ? FM_PLAY_16BIT : 0) | + (i << 8); + } + + if (setmode & AUMODE_RECORD) { + + rec->factor = 1; + rec->sw_code = 0; + switch(rec->encoding) { + case AUDIO_ENCODING_ULAW: + rec->sw_code = ulinear8_to_mulaw; + break; + case AUDIO_ENCODING_SLINEAR_LE: + if (rec->precision == 8) + rec->sw_code = change_sign8; + break; + case AUDIO_ENCODING_ULINEAR_LE: + if (rec->precision == 16) + rec->sw_code = change_sign16_le; + break; + case AUDIO_ENCODING_ALAW: + rec->sw_code = ulinear8_to_alaw; + break; + case AUDIO_ENCODING_SLINEAR_BE: + if (play->precision == 16) + play->sw_code = swap_bytes; + else + play->sw_code = change_sign8; + break; + case AUDIO_ENCODING_ULINEAR_BE: + if (play->precision == 16) + play->sw_code = swap_bytes_change_sign16_le; + break; + default: + return EINVAL; + } + for (i = 0; i < 10 && rec->sample_rate > fms_rates[i].limit; + i++) + ; + rec->sample_rate = fms_rates[i].rate; + sc->sc_rec_reg = + (rec->channels == 2 ? FM_REC_STEREO : 0) | + (rec->precision * rec->factor == 16 ? FM_REC_16BIT : 0) | + (i << 8); + } + + return 0; +} + +int +fms_round_blocksize(addr, blk) + void *addr; + int blk; +{ + return blk & ~0xf; +} + +int +fms_halt_output(addr) + void *addr; +{ + struct fms_softc *sc = addr; + u_int16_t k1; + + k1 = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_CTL); + bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_CTL, + (k1 & ~(FM_PLAY_STOPNOW | FM_PLAY_START)) | + FM_PLAY_BUF1_LAST | FM_PLAY_BUF2_LAST); + + return 0; +} + +int +fms_halt_input(addr) + void *addr; +{ + struct fms_softc *sc = addr; + u_int16_t k1; + + k1 = bus_space_read_2(sc->sc_iot, sc->sc_ioh, FM_REC_CTL); + bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_REC_CTL, + (k1 & ~(FM_REC_STOPNOW | FM_REC_START)) | + FM_REC_BUF1_LAST | FM_REC_BUF2_LAST); + + return 0; +} + +int +fms_getdev(addr, retp) + void *addr; + struct audio_device *retp; +{ + *retp = fms_device; + return 0; +} + +int +fms_set_port(addr, cp) + void *addr; + mixer_ctrl_t *cp; +{ + struct fms_softc *sc = addr; + + return (sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp)); +} + +int +fms_get_port(addr, cp) + void *addr; + mixer_ctrl_t *cp; +{ + struct fms_softc *sc = addr; + + return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp)); +} + +void * +fms_malloc(addr, direction, size, pool, flags) + void *addr; + int direction; + size_t size; + int pool, flags; +{ + struct fms_softc *sc = addr; + struct fms_dma *p; + int error; + int rseg; + + p = malloc(sizeof(*p), pool, flags); + if (!p) + return 0; + + p->size = size; + if ((error = bus_dmamem_alloc(sc->sc_dmat, size, NBPG, 0, &p->seg, 1, + &rseg, BUS_DMA_NOWAIT)) != 0) { + printf("%s: unable to allocate dma, error = %d\n", + sc->sc_dev.dv_xname, error); + goto fail_alloc; + } + + if ((error = bus_dmamem_map(sc->sc_dmat, &p->seg, rseg, size, &p->addr, + BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) { + printf("%s: unable to map dma, error = %d\n", + sc->sc_dev.dv_xname, error); + goto fail_map; + } + + if ((error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0, + BUS_DMA_NOWAIT, &p->map)) != 0) { + printf("%s: unable to create dma map, error = %d\n", + sc->sc_dev.dv_xname, error); + goto fail_create; + } + + if ((error = bus_dmamap_load(sc->sc_dmat, p->map, p->addr, size, NULL, + BUS_DMA_NOWAIT)) != 0) { + printf("%s: unable to load dma map, error = %d\n", + sc->sc_dev.dv_xname, error); + goto fail_load; + } + + p->next = sc->sc_dmas; + sc->sc_dmas = p; + + return p->addr; + + +fail_load: + bus_dmamap_destroy(sc->sc_dmat, p->map); +fail_create: + bus_dmamem_unmap(sc->sc_dmat, p->addr, size); +fail_map: + bus_dmamem_free(sc->sc_dmat, &p->seg, 1); +fail_alloc: + free(p, pool); + return 0; +} + +void +fms_free(addr, ptr, pool) + void *addr; + void *ptr; + int pool; +{ + struct fms_softc *sc = addr; + struct fms_dma **pp, *p; + + for (pp = &(sc->sc_dmas); (p = *pp) != NULL; pp = &p->next) + if (p->addr == ptr) { + bus_dmamap_unload(sc->sc_dmat, p->map); + bus_dmamap_destroy(sc->sc_dmat, p->map); + bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size); + bus_dmamem_free(sc->sc_dmat, &p->seg, 1); + + *pp = p->next; + free(p, pool); + return; + } + + panic("fms_free: trying to free unallocated memory"); +} + +size_t +fms_round_buffersize(addr, direction, size) + void *addr; + int direction; + size_t size; +{ + return size; +} + +int +fms_mappage(addr, mem, off, prot) + void *addr; + void *mem; + int off; + int prot; +{ + struct fms_softc *sc = addr; + struct fms_dma *p; + + if (off < 0) + return -1; + + for (p = sc->sc_dmas; p && p->addr != mem; p = p->next) + ; + if (!p) + return -1; + + return bus_dmamem_mmap(sc->sc_dmat, &p->seg, 1, off, prot, + BUS_DMA_WAITOK); +} + +int +fms_get_props(addr) + void *addr; +{ + return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT | + AUDIO_PROP_FULLDUPLEX; +} + +int +fms_query_devinfo(addr, dip) + void *addr; + mixer_devinfo_t *dip; +{ + struct fms_softc *sc = addr; + + return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip)); +} + +int +fms_trigger_output(addr, start, end, blksize, intr, arg, param) + void *addr; + void *start, *end; + int blksize; + void (*intr) __P((void *)); + void *arg; + struct audio_params *param; +{ + struct fms_softc *sc = addr; + struct fms_dma *p; + + sc->sc_pintr = intr; + sc->sc_parg = arg; + + for (p = sc->sc_dmas; p && p->addr != start; p = p->next) + ; + + if (!p) + panic("fms_trigger_output: request with bad start " + "address (%p)\n", start); + + sc->sc_play_start = p->map->dm_segs[0].ds_addr; + sc->sc_play_end = sc->sc_play_start + ((char *)end - (char *)start); + sc->sc_play_blksize = blksize; + sc->sc_play_nextblk = sc->sc_play_start + sc->sc_play_blksize; + sc->sc_play_flip = 0; + bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_DMALEN, blksize - 1); + bus_space_write_4(sc->sc_iot, sc->sc_ioh, FM_PLAY_DMABUF1, + sc->sc_play_start); + bus_space_write_4(sc->sc_iot, sc->sc_ioh, FM_PLAY_DMABUF2, + sc->sc_play_nextblk); + bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_PLAY_CTL, + FM_PLAY_START | FM_PLAY_STOPNOW | sc->sc_play_reg); + return 0; +} + + +int +fms_trigger_input(addr, start, end, blksize, intr, arg, param) + void *addr; + void *start, *end; + int blksize; + void (*intr) __P((void *)); + void *arg; + struct audio_params *param; +{ + struct fms_softc *sc = addr; + struct fms_dma *p; + + sc->sc_rintr = intr; + sc->sc_rarg = arg; + + for (p = sc->sc_dmas; p && p->addr != start; p = p->next) + ; + + if (!p) + panic("fms_trigger_input: request with bad start " + "address (%p)\n", start); + + sc->sc_rec_start = p->map->dm_segs[0].ds_addr; + sc->sc_rec_end = sc->sc_rec_start + ((char *)end - (char *)start); + sc->sc_rec_blksize = blksize; + sc->sc_rec_nextblk = sc->sc_rec_start + sc->sc_rec_blksize; + sc->sc_rec_flip = 0; + bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_REC_DMALEN, blksize - 1); + bus_space_write_4(sc->sc_iot, sc->sc_ioh, FM_REC_DMABUF1, + sc->sc_rec_start); + bus_space_write_4(sc->sc_iot, sc->sc_ioh, FM_REC_DMABUF2, + sc->sc_rec_nextblk); + bus_space_write_2(sc->sc_iot, sc->sc_ioh, FM_REC_CTL, + FM_REC_START | FM_REC_STOPNOW | sc->sc_rec_reg); + return 0; +} + + diff --git a/sys/dev/pci/fmsvar.h b/sys/dev/pci/fmsvar.h new file mode 100644 index 00000000000..a47564be8dd --- /dev/null +++ b/sys/dev/pci/fmsvar.h @@ -0,0 +1,78 @@ +/* $NetBSD: fmsvar.h,v 1.1 1999/11/01 21:54:12 augustss Exp $ */ + +/*- + * Copyright (c) 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Witold J. Wnuk. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + + +#ifndef _DEV_PCI_FMSVAR_H_ +#define _DEV_PCI_FMSVAR_H_ + +struct fms_softc { + struct device sc_dev; + void *sc_ih; + + bus_space_tag_t sc_iot; + bus_space_handle_t sc_ioh; + bus_addr_t sc_ioaddr; + bus_size_t sc_iosize; + bus_dma_tag_t sc_dmat; + + bus_space_handle_t sc_opl_ioh; + + bus_space_handle_t sc_mpu_ioh; + struct device * sc_mpu_dev; + + struct ac97_codec_if *codec_if; + struct ac97_host_if host_if; + + struct fms_dma *sc_dmas; + + void (*sc_pintr)(void *); + void *sc_parg; + bus_addr_t sc_play_start, sc_play_end, sc_play_nextblk; + int sc_play_blksize; + int sc_play_flip; + u_int16_t sc_play_reg; + + void (*sc_rintr)(void *); + void *sc_rarg; + bus_addr_t sc_rec_start, sc_rec_end, sc_rec_nextblk; + int sc_rec_blksize; + int sc_rec_flip; + u_int16_t sc_rec_reg; +}; + +#endif |