Home | History | Annotate | Line # | Download | only in pci
autri.c revision 1.15.2.7
      1  1.15.2.7     skrll /*	$NetBSD: autri.c,v 1.15.2.7 2005/01/17 19:31:23 skrll Exp $	*/
      2       1.1    someya 
      3       1.1    someya /*
      4       1.1    someya  * Copyright (c) 2001 SOMEYA Yoshihiko and KUROSAWA Takahiro.
      5       1.1    someya  * All rights reserved.
      6       1.1    someya  *
      7       1.1    someya  * Redistribution and use in source and binary forms, with or without
      8       1.1    someya  * modification, are permitted provided that the following conditions
      9       1.1    someya  * are met:
     10       1.1    someya  * 1. Redistributions of source code must retain the above copyright
     11       1.1    someya  *    notice, this list of conditions and the following disclaimer.
     12       1.1    someya  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1    someya  *    notice, this list of conditions and the following disclaimer in the
     14       1.1    someya  *    documentation and/or other materials provided with the distribution.
     15       1.1    someya  *
     16       1.1    someya  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17       1.1    someya  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18       1.1    someya  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19       1.1    someya  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20       1.1    someya  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21       1.1    someya  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22       1.1    someya  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23       1.1    someya  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24       1.1    someya  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25       1.1    someya  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26       1.1    someya  */
     27       1.1    someya 
     28  1.15.2.7     skrll /*
     29       1.1    someya  * Trident 4DWAVE-DX/NX, SiS 7018, ALi M5451 Sound Driver
     30  1.15.2.7     skrll  *
     31       1.1    someya  * The register information is taken from the ALSA driver.
     32       1.1    someya  *
     33       1.1    someya  * Documentation links:
     34       1.1    someya  * - ftp://ftp.alsa-project.org/pub/manuals/trident/
     35       1.1    someya  */
     36       1.1    someya 
     37  1.15.2.1     skrll #include <sys/cdefs.h>
     38  1.15.2.7     skrll __KERNEL_RCSID(0, "$NetBSD: autri.c,v 1.15.2.7 2005/01/17 19:31:23 skrll Exp $");
     39  1.15.2.1     skrll 
     40       1.1    someya #include "midi.h"
     41       1.1    someya 
     42       1.1    someya #include <sys/param.h>
     43       1.1    someya #include <sys/systm.h>
     44       1.1    someya #include <sys/kernel.h>
     45       1.1    someya #include <sys/fcntl.h>
     46       1.1    someya #include <sys/malloc.h>
     47       1.1    someya #include <sys/device.h>
     48       1.1    someya #include <sys/proc.h>
     49       1.1    someya 
     50       1.1    someya #include <dev/pci/pcidevs.h>
     51       1.1    someya #include <dev/pci/pcireg.h>
     52       1.1    someya #include <dev/pci/pcivar.h>
     53       1.1    someya 
     54       1.1    someya #include <sys/audioio.h>
     55       1.1    someya #include <dev/audio_if.h>
     56       1.1    someya #include <dev/midi_if.h>
     57       1.1    someya #include <dev/mulaw.h>
     58       1.1    someya #include <dev/auconv.h>
     59       1.1    someya #include <dev/ic/ac97reg.h>
     60       1.1    someya #include <dev/ic/ac97var.h>
     61       1.1    someya #include <dev/ic/mpuvar.h>
     62       1.1    someya 
     63       1.1    someya #include <machine/bus.h>
     64       1.1    someya #include <machine/intr.h>
     65       1.1    someya 
     66       1.1    someya #include <dev/pci/autrireg.h>
     67       1.1    someya #include <dev/pci/autrivar.h>
     68       1.1    someya 
     69       1.1    someya #ifdef AUDIO_DEBUG
     70       1.1    someya # define DPRINTF(x)	if (autridebug) printf x
     71       1.1    someya # define DPRINTFN(n,x)	if (autridebug > (n)) printf x
     72       1.1    someya int autridebug = 0;
     73       1.1    someya #else
     74       1.1    someya # define DPRINTF(x)
     75       1.1    someya # define DPRINTFN(n,x)
     76       1.1    someya #endif
     77       1.1    someya 
     78       1.3  augustss int	autri_match(struct device *, struct cfdata *, void *);
     79       1.3  augustss void	autri_attach(struct device *, struct device *, void *);
     80       1.3  augustss int	autri_intr(void *);
     81       1.1    someya 
     82       1.1    someya #define DMAADDR(p) ((p)->map->dm_segs[0].ds_addr)
     83       1.1    someya #define KERNADDR(p) ((void *)((p)->addr))
     84       1.1    someya 
     85       1.3  augustss int     autri_allocmem(struct autri_softc *, size_t,
     86       1.3  augustss 		       size_t, struct autri_dma *);
     87       1.3  augustss int     autri_freemem(struct autri_softc *, struct autri_dma *);
     88       1.1    someya 
     89       1.1    someya #define TWRITE1(sc, r, x) bus_space_write_1((sc)->memt, (sc)->memh, (r), (x))
     90       1.1    someya #define TWRITE2(sc, r, x) bus_space_write_2((sc)->memt, (sc)->memh, (r), (x))
     91       1.1    someya #define TWRITE4(sc, r, x) bus_space_write_4((sc)->memt, (sc)->memh, (r), (x))
     92       1.1    someya #define TREAD1(sc, r) bus_space_read_1((sc)->memt, (sc)->memh, (r))
     93       1.1    someya #define TREAD2(sc, r) bus_space_read_2((sc)->memt, (sc)->memh, (r))
     94       1.1    someya #define TREAD4(sc, r) bus_space_read_4((sc)->memt, (sc)->memh, (r))
     95       1.1    someya 
     96       1.3  augustss static __inline void autri_reg_set_1(struct autri_softc *, int, uint8_t);
     97       1.3  augustss static __inline void autri_reg_clear_1(struct autri_softc *, int, uint8_t);
     98       1.3  augustss static __inline void autri_reg_set_4(struct autri_softc *, int, uint32_t);
     99       1.3  augustss static __inline void autri_reg_clear_4(struct autri_softc *, int, uint32_t);
    100       1.3  augustss 
    101  1.15.2.7     skrll int	autri_attach_codec(void *, struct ac97_codec_if *);
    102  1.15.2.7     skrll int	autri_read_codec(void *, uint8_t, uint16_t *);
    103  1.15.2.7     skrll int	autri_write_codec(void *, uint8_t, uint16_t);
    104  1.15.2.7     skrll int	autri_reset_codec(void *);
    105  1.15.2.7     skrll enum ac97_host_flags	autri_flags_codec(void *);
    106       1.1    someya 
    107  1.15.2.7     skrll static void autri_powerhook(int, void *);
    108  1.15.2.7     skrll static int  autri_init(void *);
    109       1.3  augustss static struct autri_dma *autri_find_dma(struct autri_softc *, void *);
    110  1.15.2.7     skrll static void autri_setup_channel(struct autri_softc *, int,
    111  1.15.2.7     skrll 				const audio_params_t *param);
    112  1.15.2.7     skrll static void autri_enable_interrupt(struct autri_softc *, int);
    113  1.15.2.7     skrll static void autri_disable_interrupt(struct autri_softc *, int);
    114  1.15.2.7     skrll static void autri_startch(struct autri_softc *, int, int);
    115  1.15.2.7     skrll static void autri_stopch(struct autri_softc *, int, int);
    116  1.15.2.7     skrll static void autri_enable_loop_interrupt(void *);
    117       1.1    someya #if 0
    118  1.15.2.7     skrll static void autri_disable_loop_interrupt(void *);
    119       1.1    someya #endif
    120       1.1    someya 
    121       1.7   thorpej CFATTACH_DECL(autri, sizeof(struct autri_softc),
    122       1.8   thorpej     autri_match, autri_attach, NULL, NULL);
    123       1.1    someya 
    124       1.3  augustss int	autri_open(void *, int);
    125       1.3  augustss int	autri_query_encoding(void *, struct audio_encoding *);
    126  1.15.2.7     skrll int	autri_set_params(void *, int, int, audio_params_t *, audio_params_t *,
    127  1.15.2.7     skrll 			 stream_filter_list_t *, stream_filter_list_t *);
    128  1.15.2.7     skrll int	autri_round_blocksize(void *, int, int, const audio_params_t *);
    129       1.3  augustss int	autri_trigger_output(void *, void *, void *, int, void (*)(void *),
    130  1.15.2.7     skrll 			     void *, const audio_params_t *);
    131       1.3  augustss int	autri_trigger_input(void *, void *, void *, int, void (*)(void *),
    132  1.15.2.7     skrll 			    void *, const audio_params_t *);
    133       1.3  augustss int	autri_halt_output(void *);
    134       1.3  augustss int	autri_halt_input(void *);
    135       1.3  augustss int	autri_getdev(void *, struct audio_device *);
    136       1.3  augustss int	autri_mixer_set_port(void *, mixer_ctrl_t *);
    137       1.3  augustss int	autri_mixer_get_port(void *, mixer_ctrl_t *);
    138      1.14   thorpej void*	autri_malloc(void *, int, size_t, struct malloc_type *, int);
    139      1.14   thorpej void	autri_free(void *, void *, struct malloc_type *);
    140       1.3  augustss size_t	autri_round_buffersize(void *, int, size_t);
    141       1.3  augustss paddr_t autri_mappage(void *, void *, off_t, int);
    142       1.3  augustss int	autri_get_props(void *);
    143  1.15.2.7     skrll int	autri_query_devinfo(void *, mixer_devinfo_t *);
    144       1.1    someya 
    145  1.15.2.5     skrll static const struct audio_hw_if autri_hw_if = {
    146  1.15.2.7     skrll 	autri_open,
    147  1.15.2.7     skrll 	NULL,			/* close */
    148       1.1    someya 	NULL,			/* drain */
    149       1.1    someya 	autri_query_encoding,
    150       1.1    someya 	autri_set_params,
    151       1.1    someya 	autri_round_blocksize,
    152       1.1    someya 	NULL,			/* commit_settings */
    153       1.1    someya 	NULL,			/* init_output */
    154       1.1    someya 	NULL,			/* init_input */
    155       1.1    someya 	NULL,			/* start_output */
    156       1.1    someya 	NULL,			/* start_input */
    157       1.1    someya 	autri_halt_output,
    158       1.1    someya 	autri_halt_input,
    159       1.1    someya 	NULL,			/* speaker_ctl */
    160       1.1    someya 	autri_getdev,
    161       1.1    someya 	NULL,			/* setfd */
    162       1.1    someya 	autri_mixer_set_port,
    163       1.1    someya 	autri_mixer_get_port,
    164       1.1    someya 	autri_query_devinfo,
    165       1.1    someya 	autri_malloc,
    166       1.1    someya 	autri_free,
    167       1.1    someya 	autri_round_buffersize,
    168       1.1    someya 	autri_mappage,
    169       1.1    someya 	autri_get_props,
    170       1.1    someya 	autri_trigger_output,
    171       1.1    someya 	autri_trigger_input,
    172       1.1    someya 	NULL,			/* dev_ioctl */
    173       1.1    someya };
    174       1.1    someya 
    175       1.1    someya #if NMIDI > 0
    176       1.1    someya void	autri_midi_close(void *);
    177       1.1    someya void	autri_midi_getinfo(void *, struct midi_info *);
    178       1.1    someya int	autri_midi_open(void *, int, void (*)(void *, int),
    179       1.1    someya 			   void (*)(void *), void *);
    180       1.1    someya int	autri_midi_output(void *, int);
    181       1.1    someya 
    182  1.15.2.5     skrll const struct midi_hw_if autri_midi_hw_if = {
    183       1.1    someya 	autri_midi_open,
    184       1.1    someya 	autri_midi_close,
    185       1.1    someya 	autri_midi_output,
    186       1.1    someya 	autri_midi_getinfo,
    187       1.1    someya 	NULL,			/* ioctl */
    188       1.1    someya };
    189       1.1    someya #endif
    190       1.1    someya 
    191  1.15.2.7     skrll #define AUTRI_NFORMATS	8
    192  1.15.2.7     skrll static const struct audio_format autri_formats[AUTRI_NFORMATS] = {
    193  1.15.2.7     skrll 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
    194  1.15.2.7     skrll 	 2, AUFMT_STEREO, 0, {4000, 48000}},
    195  1.15.2.7     skrll 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
    196  1.15.2.7     skrll 	 1, AUFMT_MONAURAL, 0, {4000, 48000}},
    197  1.15.2.7     skrll 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 16, 16,
    198  1.15.2.7     skrll 	 2, AUFMT_STEREO, 0, {4000, 48000}},
    199  1.15.2.7     skrll 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 16, 16,
    200  1.15.2.7     skrll 	 1, AUFMT_MONAURAL, 0, {4000, 48000}},
    201  1.15.2.7     skrll 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
    202  1.15.2.7     skrll 	 2, AUFMT_STEREO, 0, {4000, 48000}},
    203  1.15.2.7     skrll 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_ULINEAR_LE, 8, 8,
    204  1.15.2.7     skrll 	 1, AUFMT_MONAURAL, 0, {4000, 48000}},
    205  1.15.2.7     skrll 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 8, 8,
    206  1.15.2.7     skrll 	 2, AUFMT_STEREO, 0, {4000, 48000}},
    207  1.15.2.7     skrll 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 8, 8,
    208  1.15.2.7     skrll 	 1, AUFMT_MONAURAL, 0, {4000, 48000}},
    209  1.15.2.7     skrll };
    210  1.15.2.7     skrll 
    211       1.1    someya /*
    212       1.1    someya  * register set/clear bit
    213       1.1    someya  */
    214       1.1    someya static __inline void
    215       1.3  augustss autri_reg_set_1(struct autri_softc *sc, int no, uint8_t mask)
    216       1.1    someya {
    217       1.1    someya 	bus_space_write_1(sc->memt, sc->memh, no,
    218       1.1    someya 	    (bus_space_read_1(sc->memt, sc->memh, no) | mask));
    219       1.1    someya }
    220       1.1    someya 
    221       1.1    someya static __inline void
    222       1.3  augustss autri_reg_clear_1(struct autri_softc *sc, int no, uint8_t mask)
    223       1.1    someya {
    224       1.1    someya 	bus_space_write_1(sc->memt, sc->memh, no,
    225       1.1    someya 	    (bus_space_read_1(sc->memt, sc->memh, no) & ~mask));
    226       1.1    someya }
    227       1.1    someya 
    228       1.1    someya static __inline void
    229       1.3  augustss autri_reg_set_4(struct autri_softc *sc, int no, uint32_t mask)
    230       1.1    someya {
    231       1.1    someya 	bus_space_write_4(sc->memt, sc->memh, no,
    232       1.1    someya 	    (bus_space_read_4(sc->memt, sc->memh, no) | mask));
    233       1.1    someya }
    234       1.1    someya 
    235       1.1    someya static __inline void
    236       1.3  augustss autri_reg_clear_4(struct autri_softc *sc, int no, uint32_t mask)
    237       1.1    someya {
    238       1.1    someya 	bus_space_write_4(sc->memt, sc->memh, no,
    239       1.1    someya 	    (bus_space_read_4(sc->memt, sc->memh, no) & ~mask));
    240       1.1    someya }
    241       1.1    someya 
    242       1.1    someya /*
    243       1.1    someya  * AC'97 codec
    244       1.1    someya  */
    245       1.1    someya int
    246       1.3  augustss autri_attach_codec(void *sc_, struct ac97_codec_if *codec_if)
    247       1.1    someya {
    248  1.15.2.7     skrll 	struct autri_codec_softc *sc;
    249       1.1    someya 
    250       1.1    someya 	DPRINTF(("autri_attach_codec()\n"));
    251  1.15.2.7     skrll 	sc = sc_;
    252       1.1    someya 	sc->codec_if = codec_if;
    253       1.1    someya 	return 0;
    254       1.1    someya }
    255       1.1    someya 
    256       1.1    someya int
    257  1.15.2.7     skrll autri_read_codec(void *sc_, uint8_t index, uint16_t *data)
    258       1.1    someya {
    259  1.15.2.7     skrll 	struct autri_codec_softc *codec;
    260  1.15.2.7     skrll 	struct autri_softc *sc;
    261  1.15.2.7     skrll 	uint32_t status, addr, cmd, busy;
    262  1.15.2.7     skrll 	uint16_t count;
    263       1.1    someya 
    264  1.15.2.7     skrll 	codec = sc_;
    265  1.15.2.7     skrll 	sc = codec->sc;
    266       1.1    someya 	/*DPRINTF(("sc->sc->type : 0x%X",sc->sc->type));*/
    267       1.1    someya 
    268       1.1    someya 	switch (sc->sc_devid) {
    269       1.1    someya 	case AUTRI_DEVICE_ID_4DWAVE_DX:
    270       1.1    someya 		addr = AUTRI_DX_ACR1;
    271       1.1    someya 		cmd  = AUTRI_DX_ACR1_CMD_READ;
    272       1.1    someya 		busy = AUTRI_DX_ACR1_BUSY_READ;
    273       1.1    someya 		break;
    274       1.1    someya 	case AUTRI_DEVICE_ID_4DWAVE_NX:
    275       1.1    someya 		addr = AUTRI_NX_ACR2;
    276       1.1    someya 		cmd  = AUTRI_NX_ACR2_CMD_READ;
    277       1.1    someya 		busy = AUTRI_NX_ACR2_BUSY_READ | AUTRI_NX_ACR2_RECV_WAIT;
    278       1.1    someya 		break;
    279       1.1    someya 	case AUTRI_DEVICE_ID_SIS_7018:
    280       1.1    someya 		addr = AUTRI_SIS_ACRD;
    281       1.1    someya 		cmd  = AUTRI_SIS_ACRD_CMD_READ;
    282       1.1    someya 		busy = AUTRI_SIS_ACRD_BUSY_READ | AUTRI_SIS_ACRD_AUDIO_BUSY;
    283       1.1    someya 		break;
    284       1.1    someya 	case AUTRI_DEVICE_ID_ALI_M5451:
    285       1.1    someya 		if (sc->sc_revision > 0x01)
    286       1.1    someya 			addr = AUTRI_ALI_ACWR;
    287       1.1    someya 		else
    288       1.1    someya 			addr = AUTRI_ALI_ACRD;
    289       1.1    someya 		cmd  = AUTRI_ALI_ACRD_CMD_READ;
    290       1.1    someya 		busy = AUTRI_ALI_ACRD_BUSY_READ;
    291       1.1    someya 		break;
    292       1.1    someya 	default:
    293       1.1    someya 		printf("%s: autri_read_codec : unknown device\n",
    294       1.1    someya 		       sc->sc_dev.dv_xname);
    295       1.1    someya 		return -1;
    296       1.1    someya 	}
    297       1.1    someya 
    298       1.1    someya 	/* wait for 'Ready to Read' */
    299       1.1    someya 	for (count=0; count<0xffff; count++) {
    300       1.1    someya 		if ((TREAD4(sc, addr) & busy) == 0)
    301       1.1    someya 			break;
    302       1.1    someya 	}
    303       1.1    someya 
    304       1.1    someya 	if (count == 0xffff) {
    305       1.1    someya 		printf("%s: Codec timeout. Busy reading AC'97 codec.\n",
    306       1.1    someya 		       sc->sc_dev.dv_xname);
    307       1.1    someya 		return -1;
    308       1.1    someya 	}
    309       1.1    someya 
    310       1.1    someya 	/* send Read Command to AC'97 */
    311       1.1    someya 	TWRITE4(sc, addr, (index & 0x7f) | cmd);
    312       1.1    someya 
    313       1.1    someya 	/* wait for 'Returned data is avalable' */
    314       1.1    someya 	for (count=0; count<0xffff; count++) {
    315       1.1    someya 		status = TREAD4(sc, addr);
    316       1.1    someya 		if ((status & busy) == 0)
    317       1.1    someya 			break;
    318       1.1    someya 	}
    319       1.1    someya 
    320       1.1    someya 	if (count == 0xffff) {
    321       1.1    someya 		printf("%s: Codec timeout. Busy reading AC'97 codec.\n",
    322       1.1    someya 		       sc->sc_dev.dv_xname);
    323       1.1    someya 		return -1;
    324       1.1    someya 	}
    325       1.1    someya 
    326       1.1    someya 	*data =  (status >> 16) & 0x0000ffff;
    327       1.1    someya 	/*DPRINTF(("autri_read_codec(0x%X) return 0x%X\n",reg,*data));*/
    328       1.1    someya 	return 0;
    329       1.1    someya }
    330       1.1    someya 
    331       1.1    someya int
    332  1.15.2.7     skrll autri_write_codec(void *sc_, uint8_t index, uint16_t data)
    333       1.1    someya {
    334  1.15.2.7     skrll 	struct autri_codec_softc *codec;
    335  1.15.2.7     skrll 	struct autri_softc *sc;
    336  1.15.2.7     skrll 	uint32_t addr, cmd, busy;
    337  1.15.2.7     skrll 	uint16_t count;
    338       1.1    someya 
    339  1.15.2.7     skrll 	codec = sc_;
    340  1.15.2.7     skrll 	sc = codec->sc;
    341       1.1    someya 	/*DPRINTF(("autri_write_codec(0x%X,0x%X)\n",index,data));*/
    342       1.1    someya 
    343       1.1    someya 	switch (sc->sc_devid) {
    344       1.1    someya 	case AUTRI_DEVICE_ID_4DWAVE_DX:
    345       1.1    someya 		addr = AUTRI_DX_ACR0;
    346       1.1    someya 		cmd  = AUTRI_DX_ACR0_CMD_WRITE;
    347       1.1    someya 		busy = AUTRI_DX_ACR0_BUSY_WRITE;
    348       1.1    someya 		break;
    349       1.1    someya 	case AUTRI_DEVICE_ID_4DWAVE_NX:
    350       1.1    someya 		addr = AUTRI_NX_ACR1;
    351       1.1    someya 		cmd  = AUTRI_NX_ACR1_CMD_WRITE;
    352       1.1    someya 		busy = AUTRI_NX_ACR1_BUSY_WRITE;
    353       1.1    someya 		break;
    354       1.1    someya 	case AUTRI_DEVICE_ID_SIS_7018:
    355       1.1    someya 		addr = AUTRI_SIS_ACWR;
    356       1.1    someya 		cmd  = AUTRI_SIS_ACWR_CMD_WRITE;
    357       1.1    someya 		busy = AUTRI_SIS_ACWR_BUSY_WRITE | AUTRI_SIS_ACWR_AUDIO_BUSY;
    358       1.1    someya 		break;
    359       1.1    someya 	case AUTRI_DEVICE_ID_ALI_M5451:
    360       1.1    someya 		addr = AUTRI_ALI_ACWR;
    361       1.1    someya 		cmd  = AUTRI_ALI_ACWR_CMD_WRITE;
    362       1.1    someya 		if (sc->sc_revision > 0x01)
    363       1.1    someya 			cmd  |= 0x0100;
    364       1.1    someya 		busy = AUTRI_ALI_ACWR_BUSY_WRITE;
    365       1.1    someya 		break;
    366       1.1    someya 	default:
    367       1.1    someya 		printf("%s: autri_write_codec : unknown device.\n",
    368       1.1    someya 		       sc->sc_dev.dv_xname);
    369       1.1    someya 		return -1;
    370       1.1    someya 	}
    371       1.1    someya 
    372       1.1    someya 	/* wait for 'Ready to Write' */
    373       1.1    someya 	for (count=0; count<0xffff; count++) {
    374       1.1    someya 		if ((TREAD4(sc, addr) & busy) == 0)
    375       1.1    someya 			break;
    376       1.1    someya 	}
    377       1.1    someya 
    378       1.1    someya 	if (count == 0xffff) {
    379       1.1    someya 		printf("%s: Codec timeout. Busy writing AC'97 codec\n",
    380       1.1    someya 		       sc->sc_dev.dv_xname);
    381       1.1    someya 		return -1;
    382       1.1    someya 	}
    383       1.1    someya 
    384       1.1    someya 	/* send Write Command to AC'97 */
    385       1.1    someya 	TWRITE4(sc, addr, (data << 16) | (index & 0x7f) | cmd);
    386       1.1    someya 
    387       1.1    someya 	return 0;
    388       1.1    someya }
    389       1.1    someya 
    390  1.15.2.4     skrll int
    391       1.3  augustss autri_reset_codec(void *sc_)
    392       1.1    someya {
    393  1.15.2.7     skrll 	struct autri_codec_softc *codec;
    394  1.15.2.7     skrll 	struct autri_softc *sc;
    395  1.15.2.7     skrll 	uint32_t reg, ready;
    396  1.15.2.7     skrll 	int addr, count;
    397  1.15.2.7     skrll 
    398  1.15.2.7     skrll 	codec = sc_;
    399  1.15.2.7     skrll 	sc = codec->sc;
    400  1.15.2.7     skrll 	count = 200;
    401  1.15.2.7     skrll 	DPRINTF(("autri_reset_codec(codec=%p,sc=%p)\n", codec, sc));
    402  1.15.2.7     skrll 	DPRINTF(("sc->sc_devid=%X\n", sc->sc_devid));
    403       1.1    someya 
    404       1.4       eeh 	switch (sc->sc_devid) {
    405       1.1    someya 	case AUTRI_DEVICE_ID_4DWAVE_DX:
    406       1.1    someya 		/* warm reset AC'97 codec */
    407       1.1    someya 		autri_reg_set_4(sc, AUTRI_DX_ACR2, 1);
    408       1.1    someya 		delay(100);
    409       1.1    someya 		/* release reset */
    410       1.1    someya 		autri_reg_clear_4(sc, AUTRI_DX_ACR2, 1);
    411       1.1    someya 		delay(100);
    412       1.1    someya 
    413       1.1    someya 		addr = AUTRI_DX_ACR2;
    414       1.1    someya 		ready = AUTRI_DX_ACR2_CODEC_READY;
    415       1.1    someya 		break;
    416       1.1    someya 	case AUTRI_DEVICE_ID_4DWAVE_NX:
    417       1.1    someya 		/* warm reset AC'97 codec */
    418       1.1    someya 		autri_reg_set_4(sc, AUTRI_NX_ACR0, 1);
    419       1.1    someya 		delay(100);
    420       1.1    someya 		/* release reset */
    421       1.1    someya 		autri_reg_clear_4(sc, AUTRI_NX_ACR0, 1);
    422       1.1    someya 		delay(100);
    423       1.1    someya 
    424       1.1    someya 		addr = AUTRI_NX_ACR0;
    425       1.1    someya 		ready = AUTRI_NX_ACR0_CODEC_READY;
    426       1.1    someya 		break;
    427       1.1    someya 	case AUTRI_DEVICE_ID_SIS_7018:
    428       1.5    someya 		/* cold reset AC'97 codec */
    429       1.5    someya 		autri_reg_set_4(sc, AUTRI_SIS_SCTRL, 2);
    430       1.5    someya 		delay(1000);
    431       1.1    someya 		/* release reset (warm & cold) */
    432       1.1    someya 		autri_reg_clear_4(sc, AUTRI_SIS_SCTRL, 3);
    433       1.5    someya 		delay(2000);
    434       1.1    someya 
    435       1.1    someya 		addr = AUTRI_SIS_SCTRL;
    436       1.1    someya 		ready = AUTRI_SIS_SCTRL_CODEC_READY;
    437       1.1    someya 		break;
    438       1.1    someya 	case AUTRI_DEVICE_ID_ALI_M5451:
    439       1.1    someya 		/* warm reset AC'97 codec */
    440       1.1    someya 		autri_reg_set_4(sc, AUTRI_ALI_SCTRL, 1);
    441       1.1    someya 		delay(100);
    442       1.1    someya 		/* release reset (warm & cold) */
    443       1.1    someya 		autri_reg_clear_4(sc, AUTRI_ALI_SCTRL, 3);
    444       1.1    someya 		delay(100);
    445       1.1    someya 
    446       1.1    someya 		addr = AUTRI_ALI_SCTRL;
    447       1.1    someya 		ready = AUTRI_ALI_SCTRL_CODEC_READY;
    448       1.1    someya 		break;
    449  1.15.2.1     skrll 	default:
    450  1.15.2.1     skrll 		printf("%s: autri_reset_codec : unknown device\n",
    451  1.15.2.1     skrll 		       sc->sc_dev.dv_xname);
    452  1.15.2.4     skrll 		return EOPNOTSUPP;
    453       1.1    someya 	}
    454       1.1    someya 
    455       1.1    someya 	/* wait for 'Codec Ready' */
    456       1.1    someya 	while (count--) {
    457       1.1    someya 		reg = TREAD4(sc, addr);
    458       1.1    someya 		if (reg & ready)
    459       1.1    someya 			break;
    460       1.1    someya 		delay(1000);
    461       1.1    someya 	}
    462       1.1    someya 
    463  1.15.2.4     skrll 	if (count == 0) {
    464       1.1    someya 		printf("%s: Codec timeout. AC'97 is not ready for operation.\n",
    465       1.1    someya 		       sc->sc_dev.dv_xname);
    466  1.15.2.4     skrll 		return ETIMEDOUT;
    467  1.15.2.4     skrll 	}
    468  1.15.2.4     skrll 	return 0;
    469       1.1    someya }
    470       1.1    someya 
    471       1.9    someya enum ac97_host_flags
    472       1.9    someya autri_flags_codec(void *sc_)
    473       1.9    someya {
    474       1.9    someya 	return AC97_HOST_DONT_READ;
    475       1.9    someya }
    476       1.9    someya 
    477       1.1    someya /*
    478       1.1    someya  *
    479       1.1    someya  */
    480       1.1    someya 
    481       1.1    someya int
    482       1.3  augustss autri_match(struct device *parent, struct cfdata *match, void *aux)
    483       1.1    someya {
    484  1.15.2.7     skrll 	struct pci_attach_args *pa;
    485       1.1    someya 
    486  1.15.2.7     skrll 	pa = (struct pci_attach_args *)aux;
    487       1.1    someya 	switch (PCI_VENDOR(pa->pa_id)) {
    488       1.1    someya 	case PCI_VENDOR_TRIDENT:
    489       1.1    someya 		switch (PCI_PRODUCT(pa->pa_id)) {
    490       1.1    someya 		case PCI_PRODUCT_TRIDENT_4DWAVE_DX:
    491       1.1    someya 		case PCI_PRODUCT_TRIDENT_4DWAVE_NX:
    492       1.1    someya 			return 1;
    493       1.1    someya 		}
    494       1.1    someya 		break;
    495       1.1    someya 	case PCI_VENDOR_SIS:
    496       1.1    someya 		switch (PCI_PRODUCT(pa->pa_id)) {
    497       1.1    someya 		case PCI_PRODUCT_SIS_7018:
    498       1.1    someya 			return 1;
    499       1.1    someya 		}
    500       1.1    someya 		break;
    501       1.1    someya 	case PCI_VENDOR_ALI:
    502       1.1    someya 		switch (PCI_PRODUCT(pa->pa_id)) {
    503       1.1    someya 		case PCI_PRODUCT_ALI_M5451:
    504       1.1    someya 			return 1;
    505       1.1    someya 		}
    506       1.1    someya 		break;
    507       1.1    someya 	}
    508       1.1    someya 
    509       1.1    someya 	return 0;
    510       1.1    someya }
    511       1.1    someya 
    512       1.1    someya void
    513       1.3  augustss autri_attach(struct device *parent, struct device *self, void *aux)
    514       1.1    someya {
    515  1.15.2.7     skrll 	struct autri_softc *sc;
    516  1.15.2.7     skrll 	struct pci_attach_args *pa;
    517  1.15.2.7     skrll 	pci_chipset_tag_t pc;
    518       1.1    someya 	struct autri_codec_softc *codec;
    519       1.1    someya 	pci_intr_handle_t ih;
    520       1.1    someya 	char const *intrstr;
    521       1.1    someya 	char devinfo[256];
    522  1.15.2.6     skrll 	int r;
    523  1.15.2.7     skrll 	uint32_t reg;
    524       1.1    someya 
    525  1.15.2.7     skrll 	sc = (struct autri_softc *)self;
    526  1.15.2.7     skrll 	pa = (struct pci_attach_args *)aux;
    527  1.15.2.7     skrll 	pc = pa->pa_pc;
    528      1.13   thorpej 	aprint_naive(": Audio controller\n");
    529      1.13   thorpej 
    530       1.1    someya 	sc->sc_devid = pa->pa_id;
    531       1.1    someya 	sc->sc_class = pa->pa_class;
    532       1.1    someya 
    533  1.15.2.1     skrll 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    534       1.1    someya 	sc->sc_revision = PCI_REVISION(pa->pa_class);
    535      1.13   thorpej 	aprint_normal(": %s (rev. 0x%02x)\n", devinfo, sc->sc_revision);
    536       1.1    someya 
    537       1.1    someya 	/* map register to memory */
    538       1.1    someya 	if (pci_mapreg_map(pa, AUTRI_PCI_MEMORY_BASE,
    539       1.1    someya 	    PCI_MAPREG_TYPE_MEM, 0, &sc->memt, &sc->memh, NULL, NULL)) {
    540      1.13   thorpej 		aprint_error("%s: can't map memory space\n",
    541      1.13   thorpej 		    sc->sc_dev.dv_xname);
    542       1.1    someya 		return;
    543       1.1    someya 	}
    544       1.1    someya 
    545       1.1    someya 	/* map and establish the interrupt */
    546       1.1    someya 	if (pci_intr_map(pa, &ih)) {
    547      1.13   thorpej 		aprint_error("%s: couldn't map interrupt\n",
    548      1.13   thorpej 		    sc->sc_dev.dv_xname);
    549       1.1    someya 		return;
    550       1.1    someya 	}
    551       1.1    someya 	intrstr = pci_intr_string(pc, ih);
    552       1.1    someya 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, autri_intr, sc);
    553       1.1    someya 	if (sc->sc_ih == NULL) {
    554      1.13   thorpej 		aprint_error("%s: couldn't establish interrupt",
    555       1.1    someya 		    sc->sc_dev.dv_xname);
    556       1.1    someya 		if (intrstr != NULL)
    557      1.13   thorpej 			aprint_normal(" at %s", intrstr);
    558      1.13   thorpej 		aprint_normal("\n");
    559       1.1    someya 		return;
    560       1.1    someya 	}
    561      1.13   thorpej 	aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    562       1.1    someya 
    563       1.1    someya 	sc->sc_dmatag = pa->pa_dmat;
    564       1.1    someya 	sc->sc_pc = pc;
    565       1.1    someya 	sc->sc_pt = pa->pa_tag;
    566       1.1    someya 
    567       1.1    someya 	/* enable the device */
    568       1.1    someya 	reg = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    569       1.1    someya 	reg |= (PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE);
    570       1.1    someya 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, reg);
    571       1.1    someya 
    572       1.1    someya 	/* initialize the device */
    573       1.1    someya 	autri_init(sc);
    574       1.1    someya 
    575       1.1    someya 	/* attach AC'97 codec */
    576       1.1    someya 	codec = &sc->sc_codec;
    577       1.1    someya 	memcpy(&codec->sc_dev, &sc->sc_dev, sizeof(codec->sc_dev));
    578       1.1    someya 	codec->sc = sc;
    579       1.1    someya 
    580       1.1    someya 	codec->host_if.arg = codec;
    581       1.1    someya 	codec->host_if.attach = autri_attach_codec;
    582       1.1    someya 	codec->host_if.reset = autri_reset_codec;
    583       1.1    someya 	codec->host_if.read = autri_read_codec;
    584       1.1    someya 	codec->host_if.write = autri_write_codec;
    585       1.9    someya 	codec->host_if.flags = autri_flags_codec;
    586       1.1    someya 
    587  1.15.2.7     skrll 	if ((r = ac97_attach(&codec->host_if, self)) != 0) {
    588      1.13   thorpej 		aprint_error("%s: can't attach codec (error 0x%X)\n",
    589       1.1    someya 		    sc->sc_dev.dv_xname, r);
    590       1.1    someya 		return;
    591       1.1    someya 	}
    592       1.1    someya 
    593       1.1    someya 	audio_attach_mi(&autri_hw_if, sc, &sc->sc_dev);
    594       1.1    someya 
    595       1.1    someya #if NMIDI > 0
    596       1.1    someya 	midi_attach_mi(&autri_midi_hw_if, sc, &sc->sc_dev);
    597       1.1    someya #endif
    598       1.1    someya 
    599       1.2  augustss 	sc->sc_old_power = PWR_RESUME;
    600       1.1    someya 	powerhook_establish(autri_powerhook, sc);
    601       1.1    someya }
    602       1.1    someya 
    603       1.1    someya static void
    604       1.2  augustss autri_powerhook(int why, void *addr)
    605       1.1    someya {
    606  1.15.2.7     skrll 	struct autri_softc *sc;
    607       1.1    someya 
    608  1.15.2.7     skrll 	sc = addr;
    609       1.2  augustss 	if (why == PWR_RESUME && sc->sc_old_power == PWR_SUSPEND) {
    610       1.1    someya 		DPRINTF(("PWR_RESUME\n"));
    611       1.1    someya 		autri_init(sc);
    612       1.1    someya 		/*autri_reset_codec(&sc->sc_codec);*/
    613       1.1    someya 		(sc->sc_codec.codec_if->vtbl->restore_ports)(sc->sc_codec.codec_if);
    614       1.1    someya 	}
    615       1.2  augustss 	sc->sc_old_power = why;
    616       1.1    someya }
    617       1.1    someya 
    618       1.1    someya int
    619       1.3  augustss autri_init(void *sc_)
    620       1.1    someya {
    621  1.15.2.7     skrll 	struct autri_softc *sc;
    622  1.15.2.7     skrll 	uint32_t reg;
    623  1.15.2.7     skrll 	pci_chipset_tag_t pc;
    624  1.15.2.7     skrll 	pcitag_t pt;
    625  1.15.2.7     skrll 
    626  1.15.2.7     skrll 	sc = sc_;
    627  1.15.2.7     skrll 	pc = sc->sc_pc;
    628  1.15.2.7     skrll 	pt = sc->sc_pt;
    629       1.1    someya 	DPRINTF(("in autri_init()\n"));
    630       1.1    someya 	DPRINTFN(5,("pci_conf_read(0x40) : 0x%X\n",pci_conf_read(pc,pt,0x40)));
    631       1.1    someya 	DPRINTFN(5,("pci_conf_read(0x44) : 0x%X\n",pci_conf_read(pc,pt,0x44)));
    632       1.1    someya 
    633       1.1    someya 	switch (sc->sc_devid) {
    634       1.1    someya 	case AUTRI_DEVICE_ID_4DWAVE_DX:
    635       1.1    someya 		/* disable Legacy Control */
    636       1.1    someya 		pci_conf_write(pc, pt, AUTRI_PCI_DDMA_CFG,0);
    637       1.1    someya 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
    638       1.1    someya 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & 0xffff0000);
    639       1.1    someya 		delay(100);
    640       1.1    someya 		/* audio engine reset */
    641       1.1    someya 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
    642       1.1    someya 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg | 0x00040000);
    643       1.1    someya 		delay(100);
    644       1.1    someya 		/* release reset */
    645       1.1    someya 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
    646       1.1    someya 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & ~0x00040000);
    647       1.1    someya 		delay(100);
    648       1.1    someya 		/* DAC on */
    649       1.1    someya 		autri_reg_set_4(sc,AUTRI_DX_ACR2,0x02);
    650       1.1    someya 		break;
    651       1.1    someya 	case AUTRI_DEVICE_ID_4DWAVE_NX:
    652       1.1    someya 		/* disable Legacy Control */
    653       1.1    someya 		pci_conf_write(pc, pt, AUTRI_PCI_DDMA_CFG,0);
    654       1.1    someya 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
    655       1.1    someya 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & 0xffff0000);
    656       1.1    someya 		delay(100);
    657       1.1    someya 		/* audio engine reset */
    658       1.1    someya 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
    659       1.1    someya 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg | 0x00010000);
    660       1.1    someya 		delay(100);
    661       1.1    someya 		/* release reset */
    662       1.1    someya 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
    663       1.1    someya 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & ~0x00010000);
    664       1.1    someya 		delay(100);
    665       1.1    someya 		/* DAC on */
    666       1.1    someya 		autri_reg_set_4(sc,AUTRI_NX_ACR0,0x02);
    667       1.1    someya 		break;
    668       1.1    someya 	case AUTRI_DEVICE_ID_SIS_7018:
    669       1.1    someya 		/* disable Legacy Control */
    670       1.1    someya 		pci_conf_write(pc, pt, AUTRI_PCI_DDMA_CFG,0);
    671       1.1    someya 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
    672       1.1    someya 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & 0xffff0000);
    673       1.1    someya 		delay(100);
    674       1.1    someya 		/* reset Digital Controller */
    675       1.1    someya 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
    676      1.15     perry 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg | 0x000c0000);
    677       1.1    someya 		delay(100);
    678       1.1    someya 		/* release reset */
    679       1.1    someya 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
    680       1.1    someya 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & ~0x00040000);
    681       1.1    someya 		delay(100);
    682       1.1    someya 		/* disable AC97 GPIO interrupt */
    683       1.1    someya 		TWRITE1(sc, AUTRI_SIS_ACGPIO, 0);
    684       1.1    someya 		/* enable 64 channel mode */
    685       1.1    someya 		autri_reg_set_4(sc, AUTRI_LFO_GC_CIR, BANK_B_EN);
    686       1.1    someya 		break;
    687       1.1    someya 	case AUTRI_DEVICE_ID_ALI_M5451:
    688       1.1    someya 		/* disable Legacy Control */
    689       1.1    someya 		pci_conf_write(pc, pt, AUTRI_PCI_DDMA_CFG,0);
    690       1.1    someya 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
    691       1.1    someya 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & 0xffff0000);
    692       1.1    someya 		delay(100);
    693       1.1    someya 		/* reset Digital Controller */
    694       1.1    someya 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
    695       1.1    someya 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg | 0x000c0000);
    696       1.1    someya 		delay(100);
    697       1.1    someya 		/* release reset */
    698       1.1    someya 		reg = pci_conf_read(pc, pt, AUTRI_PCI_LEGACY_IOBASE);
    699       1.1    someya 		pci_conf_write(pc, pt, AUTRI_PCI_LEGACY_IOBASE, reg & ~0x00040000);
    700       1.1    someya 		delay(100);
    701       1.1    someya 		/* enable PCM input */
    702       1.1    someya 		autri_reg_set_4(sc, AUTRI_ALI_GCONTROL, AUTRI_ALI_GCONTROL_PCM_IN);
    703       1.1    someya 		break;
    704       1.1    someya 	}
    705       1.1    someya 
    706       1.1    someya 	if (sc->sc_devid == AUTRI_DEVICE_ID_ALI_M5451) {
    707       1.1    someya 		sc->sc_play.ch      = 0;
    708       1.1    someya 		sc->sc_play.ch_intr = 1;
    709       1.1    someya 		sc->sc_rec.ch       = 31;
    710       1.1    someya 		sc->sc_rec.ch_intr  = 2;
    711       1.1    someya 	} else {
    712       1.1    someya 		sc->sc_play.ch      = 0x20;
    713       1.1    someya 		sc->sc_play.ch_intr = 0x21;
    714       1.1    someya 		sc->sc_rec.ch       = 0x22;
    715       1.1    someya 		sc->sc_rec.ch_intr  = 0x23;
    716       1.1    someya 	}
    717       1.1    someya 
    718       1.1    someya 	/* clear channel status */
    719       1.1    someya 	TWRITE4(sc, AUTRI_STOP_A, 0xffffffff);
    720       1.1    someya 	TWRITE4(sc, AUTRI_STOP_B, 0xffffffff);
    721       1.1    someya 
    722       1.1    someya 	/* disable channel interrupt */
    723       1.1    someya 	TWRITE4(sc, AUTRI_AINTEN_A, 0);
    724       1.1    someya 	TWRITE4(sc, AUTRI_AINTEN_B, 0);
    725       1.1    someya 
    726       1.1    someya #if 0
    727       1.1    someya 	/* TLB */
    728       1.1    someya 	if (sc->sc_devid == AUTRI_DEVICE_ID_4DWAVE_NX) {
    729       1.1    someya 		TWRITE4(sc,AUTRI_NX_TLBC,0);
    730       1.1    someya 	}
    731       1.1    someya #endif
    732       1.1    someya 
    733       1.1    someya 	autri_enable_loop_interrupt(sc);
    734       1.1    someya 
    735       1.1    someya 	DPRINTF(("out autri_init()\n"));
    736       1.1    someya 	return 0;
    737       1.1    someya }
    738       1.1    someya 
    739       1.1    someya static void
    740       1.3  augustss autri_enable_loop_interrupt(void *sc_)
    741       1.1    someya {
    742  1.15.2.7     skrll 	struct autri_softc *sc;
    743  1.15.2.7     skrll 	uint32_t reg;
    744       1.1    someya 
    745       1.1    someya 	/*reg = (ENDLP_IE | MIDLP_IE);*/
    746       1.1    someya 	reg = ENDLP_IE;
    747  1.15.2.7     skrll 	sc = sc_;
    748       1.1    someya 	if (sc->sc_devid == AUTRI_DEVICE_ID_SIS_7018)
    749       1.1    someya 		reg |= BANK_B_EN;
    750       1.5    someya 
    751  1.15.2.7     skrll 	autri_reg_set_4(sc, AUTRI_LFO_GC_CIR, reg);
    752       1.1    someya }
    753       1.1    someya 
    754       1.1    someya #if 0
    755       1.1    someya static void
    756       1.3  augustss autri_disable_loop_interrupt(void *sc_)
    757       1.1    someya {
    758  1.15.2.7     skrll 	struct autri_softc *sc;
    759  1.15.2.7     skrll 	uint32_t reg;
    760       1.1    someya 
    761       1.1    someya 	reg = (ENDLP_IE | MIDLP_IE);
    762  1.15.2.7     skrll 	sc = sc_;
    763  1.15.2.7     skrll 	autri_reg_clear_4(sc, AUTRI_LFO_GC_CIR, reg);
    764       1.1    someya }
    765       1.1    someya #endif
    766       1.1    someya 
    767       1.1    someya int
    768       1.3  augustss autri_intr(void *p)
    769       1.1    someya {
    770  1.15.2.7     skrll 	struct autri_softc *sc;
    771  1.15.2.7     skrll 	uint32_t intsrc;
    772  1.15.2.7     skrll 	uint32_t mask, active[2];
    773       1.1    someya 	int ch, endch;
    774       1.1    someya /*
    775       1.1    someya 	u_int32_t reg;
    776       1.1    someya 	u_int32_t cso,eso;
    777       1.1    someya */
    778  1.15.2.7     skrll 	sc = p;
    779  1.15.2.7     skrll 	intsrc = TREAD4(sc, AUTRI_MISCINT);
    780  1.15.2.7     skrll 	if ((intsrc & (ADDRESS_IRQ | MPU401_IRQ)) == 0)
    781       1.1    someya 		return 0;
    782       1.1    someya 
    783       1.1    someya 	if (intsrc & ADDRESS_IRQ) {
    784       1.1    someya 
    785       1.1    someya 		active[0] = TREAD4(sc,AUTRI_AIN_A);
    786       1.1    someya 		active[1] = TREAD4(sc,AUTRI_AIN_B);
    787       1.1    someya 
    788       1.1    someya 		if (sc->sc_devid == AUTRI_DEVICE_ID_ALI_M5451) {
    789       1.1    someya 			endch = 32;
    790       1.1    someya 		} else {
    791       1.1    someya 			endch = 64;
    792       1.1    someya 		}
    793       1.1    someya 
    794  1.15.2.7     skrll 		for (ch = 0; ch < endch; ch++) {
    795       1.1    someya 			mask = 1 << (ch & 0x1f);
    796       1.1    someya 			if (active[(ch & 0x20) ? 1 : 0] & mask) {
    797       1.1    someya 
    798      1.12       wiz 				/* clear interrupt */
    799       1.1    someya 				TWRITE4(sc, (ch & 0x20) ? AUTRI_AIN_B : AUTRI_AIN_A, mask);
    800      1.12       wiz 				/* disable interrupt */
    801       1.1    someya 				autri_reg_clear_4(sc,(ch & 0x20) ? AUTRI_AINTEN_B : AUTRI_AINTEN_A, mask);
    802       1.1    someya #if 0
    803       1.1    someya 				reg = TREAD4(sc,AUTRI_LFO_GC_CIR) & ~0x0000003f;
    804       1.1    someya 				TWRITE4(sc,AUTRI_LFO_GC_CIR, reg | ch);
    805       1.1    someya 
    806       1.1    someya 				if (sc->sc_devid == AUTRI_DEVICE_ID_4DWAVE_NX) {
    807  1.15.2.7     skrll 					cso = TREAD4(sc, 0xe0) & 0x00ffffff;
    808  1.15.2.7     skrll 					eso = TREAD4(sc, 0xe8) & 0x00ffffff;
    809       1.1    someya 				} else {
    810  1.15.2.7     skrll 					cso = (TREAD4(sc, 0xe0) >> 16) & 0x0000ffff;
    811  1.15.2.7     skrll 					eso = (TREAD4(sc, 0xe8) >> 16) & 0x0000ffff;
    812       1.1    someya 				}
    813       1.1    someya 				/*printf("cso=%d, eso=%d\n",cso,eso);*/
    814       1.1    someya #endif
    815       1.1    someya 				if (ch == sc->sc_play.ch_intr) {
    816       1.1    someya 					if (sc->sc_play.intr)
    817       1.1    someya 						sc->sc_play.intr(sc->sc_play.intr_arg);
    818       1.1    someya 				}
    819       1.1    someya 
    820       1.1    someya 				if (ch == sc->sc_rec.ch_intr) {
    821       1.1    someya 					if (sc->sc_rec.intr)
    822       1.1    someya 						sc->sc_rec.intr(sc->sc_rec.intr_arg);
    823       1.1    someya 				}
    824       1.1    someya 
    825       1.1    someya 				/* enable interrupt */
    826       1.1    someya 				autri_reg_set_4(sc, (ch & 0x20) ? AUTRI_AINTEN_B : AUTRI_AINTEN_A, mask);
    827       1.1    someya 			}
    828       1.1    someya 		}
    829       1.1    someya 	}
    830       1.1    someya 
    831       1.1    someya 	if (intsrc & MPU401_IRQ) {
    832       1.1    someya 		/* XXX */
    833       1.1    someya 	}
    834       1.1    someya 
    835       1.1    someya 	autri_reg_set_4(sc,AUTRI_MISCINT,
    836       1.1    someya 		ST_TARGET_REACHED | MIXER_OVERFLOW | MIXER_UNDERFLOW);
    837       1.1    someya 
    838       1.1    someya 	return 1;
    839       1.1    someya }
    840       1.1    someya 
    841       1.1    someya /*
    842       1.1    someya  *
    843       1.1    someya  */
    844       1.1    someya 
    845       1.1    someya int
    846  1.15.2.7     skrll autri_allocmem(struct autri_softc *sc, size_t size, size_t align,
    847       1.3  augustss 	       struct autri_dma *p)
    848       1.1    someya {
    849       1.1    someya 	int error;
    850       1.1    someya 
    851       1.1    someya 	p->size = size;
    852       1.1    someya 	error = bus_dmamem_alloc(sc->sc_dmatag, p->size, align, 0,
    853       1.1    someya 	    p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
    854       1.1    someya 	    &p->nsegs, BUS_DMA_NOWAIT);
    855       1.1    someya 	if (error)
    856  1.15.2.7     skrll 		return error;
    857       1.1    someya 
    858       1.1    someya 	error = bus_dmamem_map(sc->sc_dmatag, p->segs, p->nsegs, p->size,
    859       1.1    someya 	    &p->addr, BUS_DMA_NOWAIT|BUS_DMA_COHERENT);
    860       1.1    someya 	if (error)
    861       1.1    someya 		goto free;
    862       1.1    someya 
    863       1.1    someya 	error = bus_dmamap_create(sc->sc_dmatag, p->size, 1, p->size,
    864       1.1    someya 	    0, BUS_DMA_NOWAIT, &p->map);
    865       1.1    someya 	if (error)
    866       1.1    someya 		goto unmap;
    867       1.1    someya 
    868       1.1    someya 	error = bus_dmamap_load(sc->sc_dmatag, p->map, p->addr, p->size, NULL,
    869       1.1    someya 	    BUS_DMA_NOWAIT);
    870       1.1    someya 	if (error)
    871       1.1    someya 		goto destroy;
    872       1.1    someya 	return (0);
    873       1.1    someya 
    874       1.1    someya destroy:
    875       1.1    someya 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
    876       1.1    someya unmap:
    877       1.1    someya 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
    878       1.1    someya free:
    879       1.1    someya 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
    880  1.15.2.7     skrll 	return error;
    881       1.1    someya }
    882       1.1    someya 
    883       1.1    someya int
    884       1.3  augustss autri_freemem(struct autri_softc *sc, struct autri_dma *p)
    885       1.1    someya {
    886  1.15.2.7     skrll 
    887       1.1    someya 	bus_dmamap_unload(sc->sc_dmatag, p->map);
    888       1.1    someya 	bus_dmamap_destroy(sc->sc_dmatag, p->map);
    889       1.1    someya 	bus_dmamem_unmap(sc->sc_dmatag, p->addr, p->size);
    890       1.1    someya 	bus_dmamem_free(sc->sc_dmatag, p->segs, p->nsegs);
    891       1.1    someya 	return 0;
    892       1.1    someya }
    893       1.1    someya 
    894       1.1    someya int
    895       1.3  augustss autri_open(void *addr, int flags)
    896       1.1    someya {
    897       1.1    someya 	DPRINTF(("autri_open()\n"));
    898       1.1    someya 	DPRINTFN(5,("MISCINT    : 0x%08X\n",
    899       1.1    someya 		    TREAD4((struct autri_softc *)addr, AUTRI_MISCINT)));
    900       1.1    someya 	DPRINTFN(5,("LFO_GC_CIR : 0x%08X\n",
    901       1.1    someya 		    TREAD4((struct autri_softc *)addr, AUTRI_LFO_GC_CIR)));
    902  1.15.2.7     skrll 	return 0;
    903       1.1    someya }
    904       1.1    someya 
    905       1.1    someya int
    906       1.3  augustss autri_query_encoding(void *addr, struct audio_encoding *fp)
    907       1.1    someya {
    908  1.15.2.7     skrll 
    909       1.1    someya 	switch (fp->index) {
    910       1.1    someya 	case 0:
    911       1.1    someya 		strcpy(fp->name, AudioEulinear);
    912       1.1    someya 		fp->encoding = AUDIO_ENCODING_ULINEAR;
    913       1.1    someya 		fp->precision = 8;
    914       1.1    someya 		fp->flags = 0;
    915       1.1    someya 		break;
    916       1.1    someya 	case 1:
    917       1.1    someya 		strcpy(fp->name, AudioEmulaw);
    918       1.1    someya 		fp->encoding = AUDIO_ENCODING_ULAW;
    919       1.1    someya 		fp->precision = 8;
    920       1.1    someya 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    921       1.1    someya 		break;
    922       1.1    someya 	case 2:
    923       1.1    someya 		strcpy(fp->name, AudioEalaw);
    924       1.1    someya 		fp->encoding = AUDIO_ENCODING_ALAW;
    925       1.1    someya 		fp->precision = 8;
    926       1.1    someya 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    927       1.1    someya 		break;
    928       1.1    someya 	case 3:
    929       1.1    someya 		strcpy(fp->name, AudioEslinear);
    930       1.1    someya 		fp->encoding = AUDIO_ENCODING_SLINEAR;
    931       1.1    someya 		fp->precision = 8;
    932       1.1    someya 		fp->flags = 0;
    933       1.1    someya 		break;
    934       1.1    someya 	case 4:
    935       1.1    someya 		strcpy(fp->name, AudioEslinear_le);
    936       1.1    someya 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
    937       1.1    someya 		fp->precision = 16;
    938       1.1    someya 		fp->flags = 0;
    939       1.1    someya 		break;
    940       1.1    someya 	case 5:
    941       1.1    someya 		strcpy(fp->name, AudioEulinear_le);
    942       1.1    someya 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
    943       1.1    someya 		fp->precision = 16;
    944       1.1    someya 		fp->flags = 0;
    945       1.1    someya 		break;
    946       1.1    someya 	case 6:
    947       1.1    someya 		strcpy(fp->name, AudioEslinear_be);
    948       1.1    someya 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
    949       1.1    someya 		fp->precision = 16;
    950       1.1    someya 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    951       1.1    someya 		break;
    952       1.1    someya 	case 7:
    953       1.1    someya 		strcpy(fp->name, AudioEulinear_be);
    954       1.1    someya 		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
    955       1.1    someya 		fp->precision = 16;
    956       1.1    someya 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    957       1.1    someya 		break;
    958       1.1    someya 	default:
    959  1.15.2.7     skrll 		return EINVAL;
    960       1.1    someya 	}
    961       1.1    someya 
    962       1.1    someya 	return 0;
    963       1.1    someya }
    964       1.1    someya 
    965       1.1    someya int
    966       1.3  augustss autri_set_params(void *addr, int setmode, int usemode,
    967  1.15.2.7     skrll 		 audio_params_t *play, audio_params_t *rec,
    968  1.15.2.7     skrll 		 stream_filter_list_t *pfil, stream_filter_list_t *rfil)
    969       1.1    someya {
    970  1.15.2.7     skrll 	if (setmode & AUMODE_RECORD) {
    971  1.15.2.7     skrll 		if (auconv_set_converter(autri_formats, AUTRI_NFORMATS,
    972  1.15.2.7     skrll 					 AUMODE_RECORD, rec, FALSE, rfil) < 0)
    973  1.15.2.7     skrll 			return EINVAL;
    974  1.15.2.7     skrll 	}
    975  1.15.2.7     skrll 	if (setmode & AUMODE_PLAY) {
    976  1.15.2.7     skrll 		if (auconv_set_converter(autri_formats, AUTRI_NFORMATS,
    977  1.15.2.7     skrll 					 AUMODE_PLAY, play, FALSE, pfil) < 0)
    978  1.15.2.7     skrll 			return EINVAL;
    979       1.1    someya 	}
    980       1.1    someya 	return 0;
    981       1.1    someya }
    982       1.1    someya 
    983       1.1    someya int
    984  1.15.2.7     skrll autri_round_blocksize(void *addr, int block,
    985  1.15.2.7     skrll 		      int mode, const audio_params_t *param)
    986       1.1    someya {
    987  1.15.2.7     skrll 	return block & -4;
    988       1.1    someya }
    989       1.1    someya 
    990       1.1    someya int
    991       1.3  augustss autri_halt_output(void *addr)
    992       1.1    someya {
    993  1.15.2.7     skrll 	struct autri_softc *sc;
    994       1.1    someya 
    995       1.1    someya 	DPRINTF(("autri_halt_output()\n"));
    996  1.15.2.7     skrll 	sc = addr;
    997       1.1    someya 	sc->sc_play.intr = NULL;
    998       1.1    someya 	autri_stopch(sc, sc->sc_play.ch, sc->sc_play.ch_intr);
    999       1.1    someya 	autri_disable_interrupt(sc, sc->sc_play.ch_intr);
   1000       1.1    someya 
   1001       1.1    someya 	return 0;
   1002       1.1    someya }
   1003       1.1    someya 
   1004       1.1    someya int
   1005       1.3  augustss autri_halt_input(void *addr)
   1006       1.1    someya {
   1007  1.15.2.7     skrll 	struct autri_softc *sc;
   1008       1.1    someya 
   1009       1.1    someya 	DPRINTF(("autri_halt_input()\n"));
   1010  1.15.2.7     skrll 	sc = addr;
   1011       1.1    someya 	sc->sc_rec.intr = NULL;
   1012       1.1    someya 	autri_stopch(sc, sc->sc_rec.ch, sc->sc_rec.ch_intr);
   1013       1.1    someya 	autri_disable_interrupt(sc, sc->sc_rec.ch_intr);
   1014       1.1    someya 
   1015       1.1    someya 	return 0;
   1016       1.1    someya }
   1017       1.1    someya 
   1018       1.1    someya int
   1019       1.3  augustss autri_getdev(void *addr, struct audio_device *retp)
   1020       1.1    someya {
   1021  1.15.2.7     skrll 	struct autri_softc *sc;
   1022       1.1    someya 
   1023       1.1    someya 	DPRINTF(("autri_getdev().\n"));
   1024  1.15.2.7     skrll 	sc = addr;
   1025       1.1    someya 	strncpy(retp->name, "Trident 4DWAVE", sizeof(retp->name));
   1026       1.1    someya 	snprintf(retp->version, sizeof(retp->version), "0x%02x",
   1027       1.1    someya 	     PCI_REVISION(sc->sc_class));
   1028       1.1    someya 
   1029       1.1    someya 	switch (sc->sc_devid) {
   1030       1.1    someya 	case AUTRI_DEVICE_ID_4DWAVE_DX:
   1031       1.1    someya 		strncpy(retp->config, "4DWAVE-DX", sizeof(retp->config));
   1032       1.1    someya 		break;
   1033       1.1    someya 	case AUTRI_DEVICE_ID_4DWAVE_NX:
   1034       1.1    someya 		strncpy(retp->config, "4DWAVE-NX", sizeof(retp->config));
   1035       1.1    someya 		break;
   1036       1.1    someya 	case AUTRI_DEVICE_ID_SIS_7018:
   1037       1.1    someya 		strncpy(retp->config, "SiS 7018", sizeof(retp->config));
   1038       1.1    someya 		break;
   1039       1.1    someya 	case AUTRI_DEVICE_ID_ALI_M5451:
   1040       1.1    someya 		strncpy(retp->config, "ALi M5451", sizeof(retp->config));
   1041       1.1    someya 		break;
   1042       1.1    someya 	default:
   1043       1.1    someya 		strncpy(retp->config, "unknown", sizeof(retp->config));
   1044       1.1    someya 	}
   1045       1.1    someya 
   1046       1.1    someya 	return 0;
   1047       1.1    someya }
   1048       1.1    someya 
   1049       1.1    someya int
   1050       1.3  augustss autri_mixer_set_port(void *addr, mixer_ctrl_t *cp)
   1051       1.1    someya {
   1052  1.15.2.7     skrll 	struct autri_softc *sc;
   1053       1.1    someya 
   1054  1.15.2.7     skrll 	sc = addr;
   1055  1.15.2.7     skrll 	return sc->sc_codec.codec_if->vtbl->mixer_set_port(
   1056  1.15.2.7     skrll 	    sc->sc_codec.codec_if, cp);
   1057       1.1    someya }
   1058       1.1    someya 
   1059       1.1    someya int
   1060       1.3  augustss autri_mixer_get_port(void *addr, mixer_ctrl_t *cp)
   1061       1.1    someya {
   1062  1.15.2.7     skrll 	struct autri_softc *sc;
   1063       1.1    someya 
   1064  1.15.2.7     skrll 	sc = addr;
   1065  1.15.2.7     skrll 	return sc->sc_codec.codec_if->vtbl->mixer_get_port(
   1066  1.15.2.7     skrll 	    sc->sc_codec.codec_if, cp);
   1067       1.1    someya }
   1068       1.1    someya 
   1069       1.1    someya int
   1070       1.3  augustss autri_query_devinfo(void *addr, mixer_devinfo_t *dip)
   1071       1.1    someya {
   1072  1.15.2.7     skrll 	struct autri_softc *sc;
   1073       1.1    someya 
   1074  1.15.2.7     skrll 	sc = addr;
   1075  1.15.2.7     skrll 	return sc->sc_codec.codec_if->vtbl->query_devinfo(
   1076  1.15.2.7     skrll 	    sc->sc_codec.codec_if, dip);
   1077       1.1    someya }
   1078       1.1    someya 
   1079       1.1    someya void *
   1080      1.14   thorpej autri_malloc(void *addr, int direction, size_t size,
   1081      1.14   thorpej     struct malloc_type *pool, int flags)
   1082       1.1    someya {
   1083  1.15.2.7     skrll 	struct autri_softc *sc;
   1084       1.1    someya 	struct autri_dma *p;
   1085       1.1    someya 	int error;
   1086       1.1    someya 
   1087       1.1    someya 	p = malloc(sizeof(*p), pool, flags);
   1088       1.1    someya 	if (!p)
   1089       1.1    someya 		return NULL;
   1090  1.15.2.7     skrll 	sc = addr;
   1091       1.1    someya #if 0
   1092       1.1    someya 	error = autri_allocmem(sc, size, 16, p);
   1093       1.1    someya #endif
   1094       1.1    someya 	error = autri_allocmem(sc, size, 0x10000, p);
   1095       1.1    someya 	if (error) {
   1096       1.1    someya 		free(p, pool);
   1097       1.1    someya 		return NULL;
   1098       1.1    someya 	}
   1099       1.1    someya 
   1100       1.1    someya 	p->next = sc->sc_dmas;
   1101       1.1    someya 	sc->sc_dmas = p;
   1102       1.1    someya 	return KERNADDR(p);
   1103       1.1    someya }
   1104       1.1    someya 
   1105       1.1    someya void
   1106      1.14   thorpej autri_free(void *addr, void *ptr, struct malloc_type *pool)
   1107       1.1    someya {
   1108  1.15.2.7     skrll 	struct autri_softc *sc;
   1109       1.1    someya 	struct autri_dma **pp, *p;
   1110       1.1    someya 
   1111  1.15.2.7     skrll 	sc = addr;
   1112       1.1    someya 	for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
   1113       1.1    someya 		if (KERNADDR(p) == ptr) {
   1114       1.1    someya 			autri_freemem(sc, p);
   1115       1.1    someya 			*pp = p->next;
   1116       1.1    someya 			free(p, pool);
   1117       1.1    someya 			return;
   1118       1.1    someya 		}
   1119       1.1    someya 	}
   1120       1.1    someya }
   1121       1.1    someya 
   1122       1.1    someya static struct autri_dma *
   1123       1.3  augustss autri_find_dma(struct autri_softc *sc, void *addr)
   1124       1.1    someya {
   1125       1.1    someya 	struct autri_dma *p;
   1126       1.1    someya 
   1127       1.1    someya 	for (p = sc->sc_dmas; p && KERNADDR(p) != addr; p = p->next)
   1128  1.15.2.7     skrll 		continue;
   1129       1.1    someya 
   1130       1.1    someya 	return p;
   1131       1.1    someya }
   1132       1.1    someya 
   1133       1.1    someya size_t
   1134       1.3  augustss autri_round_buffersize(void *addr, int direction, size_t size)
   1135       1.1    someya {
   1136  1.15.2.7     skrll 
   1137       1.1    someya 	return size;
   1138       1.1    someya }
   1139       1.1    someya 
   1140       1.1    someya paddr_t
   1141       1.3  augustss autri_mappage(void *addr, void *mem, off_t off, int prot)
   1142       1.1    someya {
   1143  1.15.2.7     skrll 	struct autri_softc *sc;
   1144       1.1    someya 	struct autri_dma *p;
   1145       1.1    someya 
   1146       1.1    someya 	if (off < 0)
   1147  1.15.2.7     skrll 		return -1;
   1148  1.15.2.7     skrll 	sc = addr;
   1149       1.1    someya 	p = autri_find_dma(sc, mem);
   1150       1.1    someya 	if (!p)
   1151  1.15.2.7     skrll 		return -1;
   1152       1.1    someya 
   1153  1.15.2.7     skrll 	return bus_dmamem_mmap(sc->sc_dmatag, p->segs, p->nsegs,
   1154  1.15.2.7     skrll 	    off, prot, BUS_DMA_WAITOK);
   1155       1.1    someya }
   1156       1.1    someya 
   1157       1.1    someya int
   1158       1.3  augustss autri_get_props(void *addr)
   1159       1.1    someya {
   1160  1.15.2.7     skrll 	return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT |
   1161  1.15.2.7     skrll 	    AUDIO_PROP_FULLDUPLEX;
   1162       1.1    someya }
   1163       1.1    someya 
   1164       1.1    someya static void
   1165       1.3  augustss autri_setup_channel(struct autri_softc *sc, int mode,
   1166  1.15.2.7     skrll 		    const audio_params_t *param)
   1167       1.1    someya {
   1168       1.1    someya 	int i, ch, channel;
   1169  1.15.2.7     skrll 	uint32_t reg, cr[5];
   1170  1.15.2.7     skrll 	uint32_t cso, eso;
   1171  1.15.2.7     skrll 	uint32_t delta, dch[2], ctrl;
   1172  1.15.2.7     skrll 	uint32_t alpha_fms, fm_vol, attribute;
   1173       1.1    someya 
   1174  1.15.2.7     skrll 	uint32_t dmaaddr, dmalen;
   1175       1.1    someya 	int factor, rvol, cvol;
   1176       1.1    someya 	struct autri_chstatus *chst;
   1177       1.1    someya 
   1178       1.1    someya 	ctrl = AUTRI_CTRL_LOOPMODE;
   1179       1.1    someya 	switch (param->encoding) {
   1180       1.1    someya 	case AUDIO_ENCODING_SLINEAR_BE:
   1181       1.1    someya 	case AUDIO_ENCODING_SLINEAR_LE:
   1182       1.1    someya 		ctrl |= AUTRI_CTRL_SIGNED;
   1183       1.1    someya 		break;
   1184       1.1    someya 	}
   1185       1.1    someya 
   1186       1.1    someya 	factor = 0;
   1187       1.1    someya 	if (param->precision == 16) {
   1188       1.1    someya 		ctrl |= AUTRI_CTRL_16BIT;
   1189       1.1    someya 		factor++;
   1190       1.1    someya 	}
   1191       1.1    someya 
   1192       1.1    someya 	if (param->channels == 2) {
   1193       1.1    someya 		ctrl |= AUTRI_CTRL_STEREO;
   1194       1.1    someya 		factor++;
   1195       1.1    someya 	}
   1196       1.1    someya 
   1197  1.15.2.7     skrll 	delta = param->sample_rate;
   1198       1.1    someya 	if (delta < 4000)
   1199       1.1    someya 		delta = 4000;
   1200       1.1    someya 	if (delta > 48000)
   1201       1.1    someya 		delta = 48000;
   1202       1.1    someya 
   1203      1.11    someya 	attribute = 0;
   1204      1.11    someya 
   1205       1.1    someya 	dch[1] = ((delta << 12) / 48000) & 0x0000ffff;
   1206       1.1    someya 	if (mode == AUMODE_PLAY) {
   1207       1.1    someya 		chst = &sc->sc_play;
   1208       1.1    someya 		dch[0] = ((delta << 12) / 48000) & 0x0000ffff;
   1209       1.5    someya 		ctrl |= AUTRI_CTRL_WAVEVOL;
   1210       1.1    someya 	} else {
   1211       1.1    someya 		chst = &sc->sc_rec;
   1212       1.1    someya 		dch[0] = ((48000 << 12) / delta) & 0x0000ffff;
   1213      1.11    someya 		if (sc->sc_devid == AUTRI_DEVICE_ID_SIS_7018) {
   1214       1.5    someya 			ctrl |= AUTRI_CTRL_MUTEVOL_SIS;
   1215      1.11    someya 			attribute = AUTRI_ATTR_PCMREC_SIS;
   1216      1.11    someya 			if (delta != 48000)
   1217      1.11    someya 				attribute |= AUTRI_ATTR_ENASRC_SIS;
   1218      1.11    someya 		} else
   1219       1.5    someya 			ctrl |= AUTRI_CTRL_MUTEVOL;
   1220       1.1    someya 	}
   1221       1.1    someya 
   1222       1.1    someya 	dmaaddr = DMAADDR(chst->dma);
   1223       1.1    someya 	cso = alpha_fms = 0;
   1224       1.1    someya 	rvol = cvol = 0x7f;
   1225       1.1    someya 	fm_vol = 0x0 | ((rvol & 0x7f) << 7) | (cvol & 0x7f);
   1226       1.1    someya 
   1227  1.15.2.7     skrll 	for (ch = 0; ch < 2; ch++) {
   1228       1.1    someya 
   1229       1.1    someya 		if (ch == 0)
   1230       1.1    someya 			dmalen = (chst->length >> factor);
   1231       1.1    someya 		else {
   1232       1.1    someya 			/* channel for interrupt */
   1233       1.1    someya 			dmalen = (chst->blksize >> factor);
   1234       1.5    someya 			if (sc->sc_devid == AUTRI_DEVICE_ID_SIS_7018)
   1235       1.5    someya 				ctrl |= AUTRI_CTRL_MUTEVOL_SIS;
   1236       1.5    someya 			else
   1237       1.5    someya 				ctrl |= AUTRI_CTRL_MUTEVOL;
   1238      1.11    someya 			attribute = 0;
   1239       1.1    someya 		}
   1240       1.1    someya 
   1241       1.1    someya 		eso = dmalen - 1;
   1242       1.1    someya 
   1243       1.1    someya 		switch (sc->sc_devid) {
   1244       1.1    someya 		case AUTRI_DEVICE_ID_4DWAVE_DX:
   1245       1.1    someya 			cr[0] = (cso << 16) | (alpha_fms & 0x0000ffff);
   1246       1.1    someya 			cr[1] = dmaaddr;
   1247  1.15.2.7     skrll 			cr[2] = (eso << 16) | (dch[ch] & 0x0000ffff);
   1248       1.1    someya 			cr[3] = fm_vol;
   1249       1.1    someya 			cr[4] = ctrl;
   1250       1.1    someya 			break;
   1251       1.1    someya 		case AUTRI_DEVICE_ID_4DWAVE_NX:
   1252       1.1    someya 			cr[0] = (dch[ch] << 24) | (cso & 0x00ffffff);
   1253       1.1    someya 			cr[1] = dmaaddr;
   1254       1.1    someya 			cr[2] = ((dch[ch] << 16) & 0xff000000) | (eso & 0x00ffffff);
   1255       1.1    someya 			cr[3] = (alpha_fms << 16) | (fm_vol & 0x0000ffff);
   1256       1.1    someya 			cr[4] = ctrl;
   1257       1.1    someya 			break;
   1258       1.1    someya 		case AUTRI_DEVICE_ID_SIS_7018:
   1259       1.1    someya 			cr[0] = (cso << 16) | (alpha_fms & 0x0000ffff);
   1260       1.1    someya 			cr[1] = dmaaddr;
   1261  1.15.2.7     skrll 			cr[2] = (eso << 16) | (dch[ch] & 0x0000ffff);
   1262      1.11    someya 			cr[3] = attribute;
   1263       1.1    someya 			cr[4] = ctrl;
   1264       1.1    someya 			break;
   1265       1.1    someya 		case AUTRI_DEVICE_ID_ALI_M5451:
   1266       1.1    someya 			cr[0] = (cso << 16) | (alpha_fms & 0x0000ffff);
   1267       1.1    someya 			cr[1] = dmaaddr;
   1268       1.1    someya 			cr[2] = (eso << 16) | (dch[ch] & 0x0000ffff);
   1269       1.1    someya 			cr[3] = 0;
   1270       1.1    someya 			cr[4] = ctrl;
   1271       1.1    someya 			break;
   1272       1.1    someya 		}
   1273       1.1    someya 
   1274       1.1    someya 		/* write channel data */
   1275       1.1    someya 		channel = (ch == 0) ? chst->ch : chst->ch_intr;
   1276       1.1    someya 
   1277       1.1    someya 		reg = TREAD4(sc,AUTRI_LFO_GC_CIR) & ~0x0000003f;
   1278       1.1    someya 		TWRITE4(sc,AUTRI_LFO_GC_CIR, reg | channel);
   1279       1.1    someya 
   1280  1.15.2.7     skrll 		for (i = 0; i < 5; i++) {
   1281       1.1    someya 			TWRITE4(sc, AUTRI_ARAM_CR + i*sizeof(cr[0]), cr[i]);
   1282       1.1    someya 			DPRINTFN(5,("cr[%d] : 0x%08X\n", i, cr[i]));
   1283       1.1    someya 		}
   1284       1.1    someya 
   1285  1.15.2.7     skrll 		/* Bank A only */
   1286       1.1    someya 		if (channel < 0x20) {
   1287       1.1    someya 			TWRITE4(sc, AUTRI_EBUF1, AUTRI_EMOD_STILL);
   1288       1.1    someya 			TWRITE4(sc, AUTRI_EBUF2, AUTRI_EMOD_STILL);
   1289       1.1    someya 		}
   1290       1.1    someya 	}
   1291       1.1    someya 
   1292       1.1    someya }
   1293       1.1    someya 
   1294       1.1    someya int
   1295       1.3  augustss autri_trigger_output(void *addr, void *start, void *end, int blksize,
   1296       1.3  augustss 		     void (*intr)(void *), void *arg,
   1297  1.15.2.7     skrll 		     const audio_params_t *param)
   1298       1.1    someya {
   1299  1.15.2.7     skrll 	struct autri_softc *sc;
   1300  1.15.2.7     skrll 	struct autri_dma *p;
   1301       1.1    someya 
   1302       1.1    someya 	DPRINTFN(5,("autri_trigger_output: sc=%p start=%p end=%p "
   1303       1.1    someya 	    "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
   1304  1.15.2.7     skrll 	sc = addr;
   1305       1.1    someya 	sc->sc_play.intr = intr;
   1306       1.1    someya 	sc->sc_play.intr_arg = arg;
   1307       1.1    someya 	sc->sc_play.offset = 0;
   1308       1.1    someya 	sc->sc_play.blksize = blksize;
   1309       1.1    someya 	sc->sc_play.length = (char *)end - (char *)start;
   1310       1.1    someya 
   1311       1.1    someya 	p = autri_find_dma(sc, start);
   1312       1.1    someya 	if (!p) {
   1313       1.1    someya 		printf("autri_trigger_output: bad addr %p\n", start);
   1314       1.1    someya 		return (EINVAL);
   1315       1.1    someya 	}
   1316       1.1    someya 
   1317       1.1    someya 	sc->sc_play.dma = p;
   1318       1.1    someya 
   1319       1.1    someya 	/* */
   1320       1.1    someya 	autri_setup_channel(sc, AUMODE_PLAY, param);
   1321       1.1    someya 
   1322       1.1    someya 	/* volume set to no attenuation */
   1323       1.1    someya 	TWRITE4(sc, AUTRI_MUSICVOL_WAVEVOL, 0);
   1324       1.1    someya 
   1325       1.1    someya 	/* enable interrupt */
   1326       1.1    someya 	autri_enable_interrupt(sc, sc->sc_play.ch_intr);
   1327       1.1    someya 
   1328       1.1    someya 	/* start channel */
   1329       1.1    someya 	autri_startch(sc, sc->sc_play.ch, sc->sc_play.ch_intr);
   1330       1.1    someya 
   1331       1.1    someya 	return 0;
   1332       1.1    someya }
   1333       1.1    someya 
   1334       1.1    someya int
   1335       1.3  augustss autri_trigger_input(void *addr, void *start, void *end, int blksize,
   1336       1.3  augustss 		    void (*intr)(void *), void *arg,
   1337  1.15.2.7     skrll 		    const audio_params_t *param)
   1338       1.1    someya {
   1339  1.15.2.7     skrll 	struct autri_softc *sc;
   1340  1.15.2.7     skrll 	struct autri_dma *p;
   1341       1.1    someya 
   1342       1.1    someya 	DPRINTFN(5,("autri_trigger_input: sc=%p start=%p end=%p "
   1343       1.1    someya 	    "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
   1344  1.15.2.7     skrll 	sc = addr;
   1345       1.1    someya 	sc->sc_rec.intr = intr;
   1346       1.1    someya 	sc->sc_rec.intr_arg = arg;
   1347       1.1    someya 	sc->sc_rec.offset = 0;
   1348       1.1    someya 	sc->sc_rec.blksize = blksize;
   1349       1.1    someya 	sc->sc_rec.length = (char *)end - (char *)start;
   1350       1.1    someya 
   1351       1.1    someya 	/* */
   1352       1.1    someya 	p = autri_find_dma(sc, start);
   1353       1.1    someya 	if (!p) {
   1354       1.1    someya 		printf("autri_trigger_input: bad addr %p\n", start);
   1355       1.1    someya 		return (EINVAL);
   1356       1.1    someya 	}
   1357       1.1    someya 
   1358       1.1    someya 	sc->sc_rec.dma = p;
   1359       1.1    someya 
   1360       1.1    someya 	/* */
   1361       1.1    someya 	if (sc->sc_devid == AUTRI_DEVICE_ID_4DWAVE_NX) {
   1362       1.1    someya 		autri_reg_set_4(sc, AUTRI_NX_ACR0, AUTRI_NX_ACR0_PSB_CAPTURE);
   1363       1.1    someya 		TWRITE1(sc, AUTRI_NX_RCI3, AUTRI_NX_RCI3_ENABLE | sc->sc_rec.ch);
   1364       1.1    someya 	}
   1365       1.1    someya 
   1366       1.1    someya #if 0
   1367       1.1    someya 	/* 4DWAVE only allows capturing at a 48KHz rate */
   1368       1.1    someya 	if (sc->sc_devid == AUTRI_DEVICE_ID_4DWAVE_DX ||
   1369       1.1    someya 	    sc->sc_devid == AUTRI_DEVICE_ID_4DWAVE_NX)
   1370       1.1    someya 		param->sample_rate = 48000;
   1371       1.1    someya #endif
   1372       1.1    someya 
   1373       1.1    someya 	autri_setup_channel(sc, AUMODE_RECORD, param);
   1374       1.1    someya 
   1375       1.1    someya 	/* enable interrupt */
   1376       1.1    someya 	autri_enable_interrupt(sc, sc->sc_rec.ch_intr);
   1377       1.1    someya 
   1378       1.1    someya 	/* start channel */
   1379       1.1    someya 	autri_startch(sc, sc->sc_rec.ch, sc->sc_rec.ch_intr);
   1380       1.1    someya 
   1381       1.1    someya 	return 0;
   1382       1.1    someya }
   1383       1.1    someya 
   1384       1.1    someya #if 0
   1385       1.1    someya static int
   1386       1.3  augustss autri_halt(struct autri_softc *sc)
   1387       1.1    someya {
   1388  1.15.2.7     skrll 
   1389       1.1    someya 	DPRINTF(("autri_halt().\n"));
   1390       1.1    someya 	/*autri_stopch(sc);*/
   1391       1.1    someya 	autri_disable_interrupt(sc, sc->sc_play.channel);
   1392       1.1    someya 	autri_disable_interrupt(sc, sc->sc_rec.channel);
   1393       1.1    someya 	return 0;
   1394       1.1    someya }
   1395       1.1    someya #endif
   1396       1.1    someya 
   1397       1.1    someya static void
   1398       1.3  augustss autri_enable_interrupt(struct autri_softc *sc, int ch)
   1399       1.1    someya {
   1400       1.1    someya 	int reg;
   1401       1.1    someya 
   1402       1.1    someya 	reg = (ch & 0x20) ? AUTRI_AINTEN_B : AUTRI_AINTEN_A;
   1403       1.1    someya 	ch &= 0x1f;
   1404       1.1    someya 
   1405       1.1    someya 	autri_reg_set_4(sc, reg, 1 << ch);
   1406       1.1    someya }
   1407       1.1    someya 
   1408       1.1    someya static void
   1409       1.3  augustss autri_disable_interrupt(struct autri_softc *sc, int ch)
   1410       1.1    someya {
   1411       1.1    someya 	int reg;
   1412       1.1    someya 
   1413       1.1    someya 	reg = (ch & 0x20) ? AUTRI_AINTEN_B : AUTRI_AINTEN_A;
   1414       1.1    someya 	ch &= 0x1f;
   1415       1.1    someya 
   1416       1.1    someya 	autri_reg_clear_4(sc, reg, 1 << ch);
   1417       1.1    someya }
   1418       1.1    someya 
   1419       1.1    someya static void
   1420       1.3  augustss autri_startch(struct autri_softc *sc, int ch, int ch_intr)
   1421       1.1    someya {
   1422       1.1    someya 	int reg;
   1423  1.15.2.7     skrll 	uint32_t chmask;
   1424       1.1    someya 
   1425       1.1    someya 	reg = (ch & 0x20) ? AUTRI_START_B : AUTRI_START_A;
   1426       1.1    someya 	ch &= 0x1f;
   1427      1.10    someya 	ch_intr &= 0x1f;
   1428       1.1    someya 	chmask = (1 << ch) | (1 << ch_intr);
   1429       1.1    someya 
   1430       1.1    someya 	autri_reg_set_4(sc, reg, chmask);
   1431       1.1    someya }
   1432       1.1    someya 
   1433       1.1    someya static void
   1434       1.3  augustss autri_stopch(struct autri_softc *sc, int ch, int ch_intr)
   1435       1.1    someya {
   1436       1.1    someya 	int reg;
   1437  1.15.2.7     skrll 	uint32_t chmask;
   1438       1.1    someya 
   1439       1.1    someya 	reg = (ch & 0x20) ? AUTRI_STOP_B : AUTRI_STOP_A;
   1440       1.1    someya 	ch &= 0x1f;
   1441      1.10    someya 	ch_intr &= 0x1f;
   1442       1.1    someya 	chmask = (1 << ch) | (1 << ch_intr);
   1443       1.1    someya 
   1444       1.1    someya 	autri_reg_set_4(sc, reg, chmask);
   1445       1.1    someya }
   1446       1.1    someya 
   1447       1.1    someya #if NMIDI > 0
   1448       1.1    someya int
   1449       1.3  augustss autri_midi_open(void *addr, int flags, void (*iintr)(void *, int),
   1450       1.3  augustss 		void (*ointr)(void *), void *arg)
   1451       1.1    someya {
   1452  1.15.2.7     skrll 	struct autri_softc *sc;
   1453       1.1    someya 
   1454       1.1    someya 	DPRINTF(("autri_midi_open()\n"));
   1455  1.15.2.7     skrll 	sc = addr;
   1456  1.15.2.7     skrll 	DPRINTFN(5,("MPUR1 : 0x%02X\n", TREAD1(sc, AUTRI_MPUR1)));
   1457  1.15.2.7     skrll 	DPRINTFN(5,("MPUR2 : 0x%02X\n", TREAD1(sc, AUTRI_MPUR2)));
   1458       1.1    someya 
   1459       1.1    someya 	sc->sc_iintr = iintr;
   1460       1.1    someya 	sc->sc_ointr = ointr;
   1461       1.1    someya 	sc->sc_arg = arg;
   1462       1.1    someya 
   1463       1.1    someya 	if (flags & FREAD)
   1464       1.1    someya 		autri_reg_clear_1(sc, AUTRI_MPUR2, AUTRI_MIDIIN_ENABLE_INTR);
   1465       1.1    someya 
   1466       1.1    someya 	if (flags & FWRITE)
   1467       1.1    someya 		autri_reg_set_1(sc, AUTRI_MPUR2, AUTRI_MIDIOUT_CONNECT);
   1468  1.15.2.7     skrll 
   1469  1.15.2.7     skrll 	return 0;
   1470       1.1    someya }
   1471       1.1    someya 
   1472       1.1    someya void
   1473       1.1    someya autri_midi_close(void *addr)
   1474       1.1    someya {
   1475  1.15.2.7     skrll 	struct autri_softc *sc;
   1476       1.1    someya 
   1477       1.1    someya 	DPRINTF(("autri_midi_close()\n"));
   1478  1.15.2.7     skrll 	sc = addr;
   1479       1.1    someya 	tsleep(sc, PWAIT, "autri", hz/10); /* give uart a chance to drain */
   1480       1.1    someya 
   1481       1.1    someya 	sc->sc_iintr = NULL;
   1482       1.1    someya 	sc->sc_ointr = NULL;
   1483       1.1    someya }
   1484       1.1    someya 
   1485       1.1    someya int
   1486       1.1    someya autri_midi_output(void *addr, int d)
   1487       1.1    someya {
   1488  1.15.2.7     skrll 	struct autri_softc *sc;
   1489       1.1    someya 	int x;
   1490       1.1    someya 
   1491  1.15.2.7     skrll 	sc = addr;
   1492       1.1    someya 	for (x = 0; x != MIDI_BUSY_WAIT; x++) {
   1493       1.1    someya 		if ((TREAD1(sc, AUTRI_MPUR1) & AUTRI_MIDIOUT_READY) == 0) {
   1494       1.1    someya 			TWRITE1(sc, AUTRI_MPUR0, d);
   1495  1.15.2.7     skrll 			return 0;
   1496       1.1    someya 		}
   1497       1.1    someya 		delay(MIDI_BUSY_DELAY);
   1498       1.1    someya 	}
   1499  1.15.2.7     skrll 	return EIO;
   1500       1.1    someya }
   1501       1.1    someya 
   1502       1.1    someya void
   1503       1.1    someya autri_midi_getinfo(void *addr, struct midi_info *mi)
   1504       1.1    someya {
   1505  1.15.2.7     skrll 
   1506       1.1    someya 	mi->name = "4DWAVE MIDI UART";
   1507       1.1    someya 	mi->props = MIDI_PROP_CAN_INPUT;
   1508       1.1    someya }
   1509       1.1    someya 
   1510       1.1    someya #endif
   1511