Home | History | Annotate | Line # | Download | only in pci
esa.c revision 1.2
      1  1.2  augustss /* $NetBSD: esa.c,v 1.2 2002/01/06 16:58:23 augustss Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*
      4  1.1  jmcneill  * Copyright (c) 2001, 2002 Jared D. McNeill <jmcneill (at) invisible.yi.org>
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmcneill  * modification, are permitted provided that the following conditions
      9  1.1  jmcneill  * are met:
     10  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmcneill  * 2. The name of the author may not be used to endorse or promote products
     13  1.1  jmcneill  *    derived from this software without specific prior written permission.
     14  1.1  jmcneill  *
     15  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     20  1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     21  1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     22  1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     23  1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  1.1  jmcneill  * SUCH DAMAGE.
     26  1.1  jmcneill  */
     27  1.1  jmcneill 
     28  1.1  jmcneill /*
     29  1.1  jmcneill  * ESS Allegro-1 / Maestro3 Audio Driver
     30  1.1  jmcneill  *
     31  1.1  jmcneill  * Based on the FreeBSD maestro3 driver and the NetBSD eap driver.
     32  1.2  augustss  * Original driver by Don Kim.
     33  1.1  jmcneill  */
     34  1.1  jmcneill 
     35  1.1  jmcneill #include <sys/types.h>
     36  1.1  jmcneill #include <sys/errno.h>
     37  1.1  jmcneill #include <sys/null.h>
     38  1.1  jmcneill #include <sys/param.h>
     39  1.1  jmcneill #include <sys/systm.h>
     40  1.1  jmcneill #include <sys/malloc.h>
     41  1.1  jmcneill #include <sys/device.h>
     42  1.1  jmcneill #include <sys/conf.h>
     43  1.1  jmcneill #include <sys/exec.h>
     44  1.1  jmcneill #include <sys/select.h>
     45  1.1  jmcneill #include <sys/audioio.h>
     46  1.1  jmcneill 
     47  1.1  jmcneill #include <machine/bus.h>
     48  1.1  jmcneill #include <machine/intr.h>
     49  1.1  jmcneill 
     50  1.1  jmcneill #include <dev/pci/pcidevs.h>
     51  1.1  jmcneill #include <dev/pci/pcivar.h>
     52  1.1  jmcneill 
     53  1.1  jmcneill #include <dev/audio_if.h>
     54  1.1  jmcneill #include <dev/mulaw.h>
     55  1.1  jmcneill #include <dev/auconv.h>
     56  1.1  jmcneill #include <dev/ic/ac97var.h>
     57  1.1  jmcneill #include <dev/ic/ac97reg.h>
     58  1.1  jmcneill 
     59  1.1  jmcneill 
     60  1.1  jmcneill #include <dev/pci/esareg.h>
     61  1.1  jmcneill #include <dev/pci/esadsp.h>
     62  1.1  jmcneill #include <dev/pci/esavar.h>
     63  1.1  jmcneill 
     64  1.1  jmcneill #define PCI_CBIO	0x10
     65  1.1  jmcneill 
     66  1.1  jmcneill #define ESA_DAC_DATA	0x1100
     67  1.1  jmcneill 
     68  1.1  jmcneill enum {
     69  1.1  jmcneill 	ESS_ALLEGRO1,
     70  1.1  jmcneill 	ESS_MAESTRO3
     71  1.1  jmcneill };
     72  1.1  jmcneill 
     73  1.1  jmcneill static struct esa_card_type {
     74  1.1  jmcneill 	u_int16_t pci_vendor_id;
     75  1.1  jmcneill 	u_int16_t pci_product_id;
     76  1.1  jmcneill 	int type;
     77  1.1  jmcneill 	int delay1, delay2;
     78  1.1  jmcneill } esa_card_types[] = {
     79  1.1  jmcneill 	{ PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_ALLEGRO1,
     80  1.1  jmcneill 	  ESS_ALLEGRO1, 50, 800 },
     81  1.1  jmcneill 	{ PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_MAESTRO3,
     82  1.1  jmcneill 	  ESS_MAESTRO3, 20, 500 },
     83  1.1  jmcneill 	{ PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_MAESTRO3_2,
     84  1.1  jmcneill 	  ESS_MAESTRO3, 20, 500 },
     85  1.1  jmcneill 	{ 0, 0, 0, 0, 0 }
     86  1.1  jmcneill };
     87  1.1  jmcneill 
     88  1.1  jmcneill struct audio_device esa_device = {
     89  1.1  jmcneill 	"ESS Allegro",
     90  1.1  jmcneill 	"",
     91  1.1  jmcneill 	"esa"
     92  1.1  jmcneill };
     93  1.1  jmcneill 
     94  1.1  jmcneill int		esa_match(struct device *, struct cfdata *, void *);
     95  1.1  jmcneill void		esa_attach(struct device *, struct device *, void *);
     96  1.1  jmcneill int		esa_detach(struct device *, int);
     97  1.1  jmcneill 
     98  1.1  jmcneill /* audio(9) functions */
     99  1.1  jmcneill int		esa_open(void *, int);
    100  1.1  jmcneill void		esa_close(void *);
    101  1.1  jmcneill int		esa_query_encoding(void *, struct audio_encoding *);
    102  1.1  jmcneill int		esa_set_params(void *, int, int, struct audio_params *,
    103  1.1  jmcneill 			       struct audio_params *);
    104  1.1  jmcneill int		esa_round_blocksize(void *, int);
    105  1.1  jmcneill int		esa_init_output(void *, void *, int);
    106  1.1  jmcneill int		esa_halt_output(void *);
    107  1.1  jmcneill int		esa_halt_input(void *);
    108  1.1  jmcneill int		esa_set_port(void *, mixer_ctrl_t *);
    109  1.1  jmcneill int		esa_get_port(void *, mixer_ctrl_t *);
    110  1.1  jmcneill int		esa_query_devinfo(void *, mixer_devinfo_t *);
    111  1.1  jmcneill void *		esa_malloc(void *, int, size_t, int, int);
    112  1.1  jmcneill void		esa_free(void *, void *, int);
    113  1.1  jmcneill int		esa_getdev(void *, struct audio_device *);
    114  1.1  jmcneill size_t		esa_round_buffersize(void *, int, size_t);
    115  1.1  jmcneill int		esa_get_props(void *);
    116  1.1  jmcneill int		esa_trigger_output(void *, void *, void *, int,
    117  1.1  jmcneill 				   void (*)(void *), void *,
    118  1.1  jmcneill 				   struct audio_params *);
    119  1.1  jmcneill int		esa_trigger_input(void *, void *, void *, int,
    120  1.1  jmcneill 				  void (*)(void *), void *,
    121  1.1  jmcneill 				  struct audio_params *);
    122  1.1  jmcneill 
    123  1.1  jmcneill int		esa_intr(void *);
    124  1.1  jmcneill int		esa_allocmem(struct esa_softc *, size_t, size_t,
    125  1.1  jmcneill 			     struct esa_dma *);
    126  1.1  jmcneill int		esa_freemem(struct esa_softc *, struct esa_dma *);
    127  1.1  jmcneill paddr_t		esa_mappage(void *addr, void *mem, off_t off, int prot);
    128  1.1  jmcneill 
    129  1.1  jmcneill /* Supporting subroutines */
    130  1.1  jmcneill u_int16_t	esa_read_assp(struct esa_softc *, u_int16_t, u_int16_t);
    131  1.1  jmcneill void		esa_write_assp(struct esa_softc *, u_int16_t, u_int16_t,
    132  1.1  jmcneill 			       u_int16_t);
    133  1.1  jmcneill int		esa_init_codec(struct esa_softc *);
    134  1.1  jmcneill int		esa_attach_codec(void *, struct ac97_codec_if *);
    135  1.1  jmcneill int		esa_read_codec(void *, u_int8_t, u_int16_t *);
    136  1.1  jmcneill int		esa_write_codec(void *, u_int8_t, u_int16_t);
    137  1.1  jmcneill void		esa_reset_codec(void *);
    138  1.1  jmcneill enum ac97_host_flags	esa_flags_codec(void *);
    139  1.1  jmcneill int		esa_wait(struct esa_softc *);
    140  1.1  jmcneill int		esa_init(struct esa_softc *);
    141  1.1  jmcneill void		esa_config(struct esa_softc *);
    142  1.1  jmcneill u_int8_t	esa_assp_halt(struct esa_softc *);
    143  1.1  jmcneill void		esa_codec_reset(struct esa_softc *);
    144  1.1  jmcneill int		esa_amp_enable(struct esa_softc *);
    145  1.1  jmcneill void		esa_enable_interrupts(struct esa_softc *);
    146  1.1  jmcneill int		esa_power(struct esa_softc *, int);
    147  1.1  jmcneill u_int32_t	esa_get_pointer(struct esa_softc *);
    148  1.1  jmcneill 
    149  1.1  jmcneill struct device *	audio_attach_mi_lkm(struct audio_hw_if *, void *,
    150  1.1  jmcneill 				    struct device *);
    151  1.1  jmcneill 
    152  1.1  jmcneill static audio_encoding_t esa_encoding[] = {
    153  1.1  jmcneill 	{ 0, AudioEulinear, AUDIO_ENCODING_ULINEAR, 8, 0 },
    154  1.1  jmcneill 	{ 1, AudioEmulaw, AUDIO_ENCODING_ULAW, 8,
    155  1.1  jmcneill 		AUDIO_ENCODINGFLAG_EMULATED },
    156  1.1  jmcneill 	{ 2, AudioEalaw, AUDIO_ENCODING_ALAW, 8, AUDIO_ENCODINGFLAG_EMULATED },
    157  1.1  jmcneill 	{ 3, AudioEslinear, AUDIO_ENCODING_SLINEAR, 8,
    158  1.1  jmcneill 		AUDIO_ENCODINGFLAG_EMULATED }, /* XXX: Are you sure? */
    159  1.1  jmcneill 	{ 4, AudioEslinear_le, AUDIO_ENCODING_SLINEAR_LE, 16, 0 },
    160  1.1  jmcneill 	{ 5, AudioEulinear_le, AUDIO_ENCODING_ULINEAR_LE, 16,
    161  1.1  jmcneill 		AUDIO_ENCODINGFLAG_EMULATED },
    162  1.1  jmcneill 	{ 6, AudioEslinear_be, AUDIO_ENCODING_SLINEAR_BE, 16,
    163  1.1  jmcneill 		AUDIO_ENCODINGFLAG_EMULATED },
    164  1.1  jmcneill 	{ 7, AudioEulinear_be, AUDIO_ENCODING_ULINEAR_BE, 16,
    165  1.1  jmcneill 		AUDIO_ENCODINGFLAG_EMULATED }
    166  1.1  jmcneill };
    167  1.1  jmcneill 
    168  1.1  jmcneill #define ESA_NENCODINGS 8
    169  1.1  jmcneill 
    170  1.1  jmcneill struct audio_hw_if esa_hw_if = {
    171  1.1  jmcneill 	esa_open,
    172  1.1  jmcneill 	esa_close,
    173  1.1  jmcneill 	NULL,			/* drain */
    174  1.1  jmcneill 	esa_query_encoding,
    175  1.1  jmcneill 	esa_set_params,
    176  1.1  jmcneill 	esa_round_blocksize,
    177  1.1  jmcneill 	NULL,			/* commit_settings */
    178  1.1  jmcneill 	esa_init_output,
    179  1.1  jmcneill 	NULL,			/* esa_init_input */
    180  1.1  jmcneill 	NULL,			/* start_output */
    181  1.1  jmcneill 	NULL,			/* start_input */
    182  1.1  jmcneill 	esa_halt_output,
    183  1.1  jmcneill 	esa_halt_input,
    184  1.1  jmcneill 	NULL,			/* speaker_ctl */
    185  1.1  jmcneill 	esa_getdev,
    186  1.1  jmcneill 	NULL,			/* getfd */
    187  1.1  jmcneill 	esa_set_port,
    188  1.1  jmcneill 	esa_get_port,
    189  1.1  jmcneill 	esa_query_devinfo,
    190  1.1  jmcneill 	esa_malloc,
    191  1.1  jmcneill 	esa_free,
    192  1.1  jmcneill 	esa_round_buffersize,
    193  1.1  jmcneill 	esa_mappage,
    194  1.1  jmcneill 	esa_get_props,
    195  1.1  jmcneill 	esa_trigger_output,
    196  1.1  jmcneill 	esa_trigger_input
    197  1.1  jmcneill };
    198  1.1  jmcneill 
    199  1.1  jmcneill struct cfattach esa_ca = {
    200  1.1  jmcneill 	sizeof(struct esa_softc), esa_match, esa_attach,
    201  1.1  jmcneill 	esa_detach, /*esa_activate*/ NULL
    202  1.1  jmcneill };
    203  1.1  jmcneill 
    204  1.1  jmcneill /*
    205  1.1  jmcneill  * audio(9) functions
    206  1.1  jmcneill  */
    207  1.1  jmcneill 
    208  1.1  jmcneill int
    209  1.1  jmcneill esa_open(void *hdl, int flags)
    210  1.1  jmcneill {
    211  1.1  jmcneill 
    212  1.1  jmcneill 	return (0);
    213  1.1  jmcneill }
    214  1.1  jmcneill 
    215  1.1  jmcneill void
    216  1.1  jmcneill esa_close(void *hdl)
    217  1.1  jmcneill {
    218  1.1  jmcneill 
    219  1.1  jmcneill 	return;
    220  1.1  jmcneill }
    221  1.1  jmcneill 
    222  1.1  jmcneill int
    223  1.1  jmcneill esa_query_encoding(void *hdl, struct audio_encoding *ae)
    224  1.1  jmcneill {
    225  1.1  jmcneill 
    226  1.1  jmcneill 	if (ae->index < 0 || ae->index >= ESA_NENCODINGS)
    227  1.1  jmcneill 		return (EINVAL);
    228  1.1  jmcneill 	*ae = esa_encoding[ae->index];
    229  1.1  jmcneill 
    230  1.1  jmcneill 	return (0);
    231  1.1  jmcneill }
    232  1.1  jmcneill 
    233  1.1  jmcneill int
    234  1.1  jmcneill esa_set_params(void *hdl, int setmode, int usemode, struct audio_params *play,
    235  1.1  jmcneill 	       struct audio_params *rec)
    236  1.1  jmcneill {
    237  1.1  jmcneill 	struct esa_softc *sc = hdl;
    238  1.1  jmcneill 	u_int32_t data;
    239  1.1  jmcneill 	u_int32_t freq;
    240  1.1  jmcneill 
    241  1.1  jmcneill 	if ((setmode & AUMODE_PLAY) == 0) {
    242  1.1  jmcneill 		printf("%s: esa_set_params: only AUMODE_PLAY is supported\n",
    243  1.1  jmcneill 		    sc->sc_dev.dv_xname);
    244  1.1  jmcneill 		return (EINVAL);
    245  1.1  jmcneill 	}
    246  1.1  jmcneill 
    247  1.1  jmcneill 	if (play->sample_rate < ESA_MINRATE ||
    248  1.1  jmcneill 	    play->sample_rate > ESA_MAXRATE ||
    249  1.1  jmcneill 	    (play->precision != 8 && play->precision != 16) ||
    250  1.1  jmcneill 	    (play->channels < 1 && play->channels > 2)) /* XXX: Are you sure? */
    251  1.1  jmcneill 		return (EINVAL);
    252  1.1  jmcneill 
    253  1.1  jmcneill 	play->factor = 1;
    254  1.1  jmcneill 	play->sw_code = 0;
    255  1.1  jmcneill 
    256  1.1  jmcneill 	switch(play->encoding) {
    257  1.1  jmcneill 	case AUDIO_ENCODING_SLINEAR_BE:
    258  1.1  jmcneill 		if (play->precision == 16)
    259  1.1  jmcneill 			play->sw_code = swap_bytes;
    260  1.1  jmcneill 		else
    261  1.1  jmcneill 			play->sw_code = change_sign8;
    262  1.1  jmcneill 		break;
    263  1.1  jmcneill 	case AUDIO_ENCODING_SLINEAR_LE:
    264  1.1  jmcneill 		if (play->precision != 16)
    265  1.1  jmcneill 			play->sw_code = change_sign8;
    266  1.1  jmcneill 		break;
    267  1.1  jmcneill 	case AUDIO_ENCODING_ULINEAR_BE:
    268  1.1  jmcneill 		if (play->precision == 16)
    269  1.1  jmcneill 			play->sw_code = swap_bytes_change_sign16_le;
    270  1.1  jmcneill 		break;
    271  1.1  jmcneill 	case AUDIO_ENCODING_ULINEAR_LE:
    272  1.1  jmcneill 		if (play->precision == 16)
    273  1.1  jmcneill 			play->sw_code = change_sign16_le;
    274  1.1  jmcneill 		break;
    275  1.1  jmcneill 	case AUDIO_ENCODING_ULAW:
    276  1.1  jmcneill 		play->factor = 2;
    277  1.1  jmcneill 		play->sw_code = mulaw_to_slinear16_le;
    278  1.1  jmcneill 		break;
    279  1.1  jmcneill 	case AUDIO_ENCODING_ALAW:
    280  1.1  jmcneill 		play->factor = 2;
    281  1.1  jmcneill 		play->sw_code = alaw_to_slinear16_le;
    282  1.1  jmcneill 		break;
    283  1.1  jmcneill 	default:
    284  1.1  jmcneill 		return (EINVAL);
    285  1.1  jmcneill 	}
    286  1.1  jmcneill 
    287  1.1  jmcneill 	if (play->channels == 1)
    288  1.1  jmcneill 		data = 1;
    289  1.1  jmcneill 	else
    290  1.1  jmcneill 		data = 0;
    291  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
    292  1.1  jmcneill 		       ESA_DAC_DATA + ESA_SRC3_MODE_OFFSET,
    293  1.1  jmcneill 	    data);
    294  1.1  jmcneill 
    295  1.1  jmcneill 	if (play->precision * play->factor == 8)
    296  1.1  jmcneill 		data = 1;
    297  1.1  jmcneill 	else
    298  1.1  jmcneill 		data = 0;
    299  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
    300  1.1  jmcneill 		       ESA_DAC_DATA + ESA_SRC3_WORD_LENGTH_OFFSET, data);
    301  1.1  jmcneill 
    302  1.1  jmcneill 	if ((freq = ((play->sample_rate << 15) + 24000) / 48000) != 0) {
    303  1.1  jmcneill 		freq--;
    304  1.1  jmcneill 	}
    305  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
    306  1.1  jmcneill 		       ESA_DAC_DATA + ESA_CDATA_FREQUENCY, freq);
    307  1.1  jmcneill 
    308  1.1  jmcneill 	return (0);
    309  1.1  jmcneill }
    310  1.1  jmcneill 
    311  1.1  jmcneill int
    312  1.1  jmcneill esa_round_blocksize(void *hdl, int bs)
    313  1.1  jmcneill {
    314  1.1  jmcneill 	struct esa_softc *sc = hdl;
    315  1.1  jmcneill 
    316  1.1  jmcneill 	bs = 4096;		/* XXX why? */
    317  1.1  jmcneill 	sc->play.blksize = bs;
    318  1.1  jmcneill 
    319  1.1  jmcneill 	return (sc->play.blksize);
    320  1.1  jmcneill }
    321  1.1  jmcneill 
    322  1.1  jmcneill int
    323  1.1  jmcneill esa_init_output(void *hdl, void *buffer, int size)
    324  1.1  jmcneill {
    325  1.1  jmcneill 
    326  1.1  jmcneill 	return (0);
    327  1.1  jmcneill }
    328  1.1  jmcneill 
    329  1.1  jmcneill int
    330  1.1  jmcneill esa_halt_output(void *hdl)
    331  1.1  jmcneill {
    332  1.1  jmcneill 	struct esa_softc *sc = hdl;
    333  1.1  jmcneill 
    334  1.1  jmcneill 	if (sc->play.active == 0)
    335  1.1  jmcneill 		return (0);
    336  1.1  jmcneill 
    337  1.1  jmcneill 	sc->play.active = 0;
    338  1.1  jmcneill 
    339  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
    340  1.1  jmcneill 		       ESA_KDATA_INSTANCE0_MINISRC, 0);
    341  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_DMA_XFER0, 0);
    342  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_MIXER_XFER0, 0);
    343  1.1  jmcneill 
    344  1.1  jmcneill 	return (0);
    345  1.1  jmcneill }
    346  1.1  jmcneill 
    347  1.1  jmcneill int
    348  1.1  jmcneill esa_halt_input(void *hdl)
    349  1.1  jmcneill {
    350  1.1  jmcneill 
    351  1.1  jmcneill 	return (EOPNOTSUPP);
    352  1.1  jmcneill }
    353  1.1  jmcneill 
    354  1.1  jmcneill void *
    355  1.1  jmcneill esa_malloc(void *hdl, int direction, size_t size, int type, int flags)
    356  1.1  jmcneill {
    357  1.1  jmcneill 	struct esa_softc *sc = hdl;
    358  1.1  jmcneill 	struct esa_dma *p;
    359  1.1  jmcneill 	int error;
    360  1.1  jmcneill 
    361  1.1  jmcneill 	p = malloc(sizeof(*p), type, flags);
    362  1.1  jmcneill 	if (!p)
    363  1.1  jmcneill 		return (0);
    364  1.1  jmcneill 	error = esa_allocmem(sc, size, 16, p);
    365  1.1  jmcneill 	if (error) {
    366  1.1  jmcneill 		free(p, type);
    367  1.1  jmcneill 		printf("%s: esa_malloc: not enough memory\n",
    368  1.1  jmcneill 		    sc->sc_dev.dv_xname);
    369  1.1  jmcneill 		return (0);
    370  1.1  jmcneill 	}
    371  1.1  jmcneill 	p->next = sc->sc_dmas;
    372  1.1  jmcneill 	sc->sc_dmas = p;
    373  1.1  jmcneill 
    374  1.1  jmcneill 	return (KERNADDR(p));
    375  1.1  jmcneill }
    376  1.1  jmcneill 
    377  1.1  jmcneill void
    378  1.1  jmcneill esa_free(void *hdl, void *addr, int type)
    379  1.1  jmcneill {
    380  1.1  jmcneill 	struct esa_softc *sc = hdl;
    381  1.1  jmcneill 	struct esa_dma *p;
    382  1.1  jmcneill 	struct esa_dma **pp;
    383  1.1  jmcneill 
    384  1.1  jmcneill 	for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next)
    385  1.1  jmcneill 		if (KERNADDR(p) == addr) {
    386  1.1  jmcneill 			esa_freemem(sc, p);
    387  1.1  jmcneill 			*pp = p->next;
    388  1.1  jmcneill 			free(p, type);
    389  1.1  jmcneill 			return;
    390  1.1  jmcneill 		}
    391  1.1  jmcneill }
    392  1.1  jmcneill 
    393  1.1  jmcneill int
    394  1.1  jmcneill esa_getdev(void *hdl, struct audio_device *ret)
    395  1.1  jmcneill {
    396  1.1  jmcneill 
    397  1.1  jmcneill 	*ret = esa_device;
    398  1.1  jmcneill 
    399  1.1  jmcneill 	return (0);
    400  1.1  jmcneill }
    401  1.1  jmcneill 
    402  1.1  jmcneill int
    403  1.1  jmcneill esa_set_port(void *hdl, mixer_ctrl_t *mc)
    404  1.1  jmcneill {
    405  1.1  jmcneill 	struct esa_softc *sc = hdl;
    406  1.1  jmcneill 
    407  1.1  jmcneill 	return (sc->codec_if->vtbl->mixer_set_port(sc->codec_if, mc));
    408  1.1  jmcneill }
    409  1.1  jmcneill 
    410  1.1  jmcneill int
    411  1.1  jmcneill esa_get_port(void *hdl, mixer_ctrl_t *mc)
    412  1.1  jmcneill {
    413  1.1  jmcneill 	struct esa_softc *sc = hdl;
    414  1.1  jmcneill 
    415  1.1  jmcneill 	return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, mc));
    416  1.1  jmcneill }
    417  1.1  jmcneill 
    418  1.1  jmcneill int
    419  1.1  jmcneill esa_query_devinfo(void *hdl, mixer_devinfo_t *di)
    420  1.1  jmcneill {
    421  1.1  jmcneill 	struct esa_softc *sc = hdl;
    422  1.1  jmcneill 
    423  1.1  jmcneill 	return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, di));
    424  1.1  jmcneill }
    425  1.1  jmcneill 
    426  1.1  jmcneill size_t
    427  1.1  jmcneill esa_round_buffersize(void *hdl, int direction, size_t bufsize)
    428  1.1  jmcneill {
    429  1.1  jmcneill 	struct esa_softc *sc = hdl;
    430  1.1  jmcneill 
    431  1.1  jmcneill 	bufsize = 65536;	/* XXX why? */
    432  1.1  jmcneill 	sc->play.bufsize = bufsize;
    433  1.1  jmcneill 
    434  1.1  jmcneill 	return (sc->play.bufsize);
    435  1.1  jmcneill }
    436  1.1  jmcneill 
    437  1.1  jmcneill int
    438  1.1  jmcneill esa_get_props(void *hdl)
    439  1.1  jmcneill {
    440  1.1  jmcneill 
    441  1.1  jmcneill 	return (AUDIO_PROP_MMAP);
    442  1.1  jmcneill }
    443  1.1  jmcneill 
    444  1.1  jmcneill int
    445  1.1  jmcneill esa_trigger_output(void *hdl, void *start, void *end, int blksize,
    446  1.1  jmcneill 			void (*intr)(void *), void *intrarg,
    447  1.1  jmcneill 			struct audio_params *param)
    448  1.1  jmcneill {
    449  1.1  jmcneill 	struct esa_softc *sc = hdl;
    450  1.1  jmcneill 	struct esa_dma *p;
    451  1.1  jmcneill 	bus_space_tag_t iot = sc->sc_iot;
    452  1.1  jmcneill 	bus_space_handle_t ioh = sc->sc_ioh;
    453  1.1  jmcneill 	u_int32_t data;
    454  1.1  jmcneill 	u_int32_t bufaddr;
    455  1.1  jmcneill 	u_int32_t i;
    456  1.1  jmcneill 	size_t size;
    457  1.1  jmcneill 	int dsp_in_size = ESA_MINISRC_IN_BUFFER_SIZE - (0x20 * 2);
    458  1.1  jmcneill 	int dsp_out_size = ESA_MINISRC_OUT_BUFFER_SIZE - (0x20 * 2);
    459  1.1  jmcneill 	int dsp_in_buf = ESA_DAC_DATA + (ESA_MINISRC_TMP_BUFFER_SIZE / 2);
    460  1.1  jmcneill 	int dsp_out_buf = dsp_in_buf + (dsp_in_size / 2) + 1;
    461  1.1  jmcneill 
    462  1.1  jmcneill 	if (sc->play.active)
    463  1.1  jmcneill 		return (EINVAL);
    464  1.1  jmcneill 
    465  1.1  jmcneill 	for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
    466  1.1  jmcneill 		;
    467  1.1  jmcneill 	if (!p) {
    468  1.1  jmcneill 		printf("%s: esa_trigger_output: bad addr %p\n",
    469  1.1  jmcneill 		    sc->sc_dev.dv_xname, start);
    470  1.1  jmcneill 		return (EINVAL);
    471  1.1  jmcneill 	}
    472  1.1  jmcneill 
    473  1.1  jmcneill 	sc->play.active = 1;
    474  1.1  jmcneill 	sc->intr = intr;
    475  1.1  jmcneill 	sc->arg = intrarg;
    476  1.1  jmcneill 	sc->play.pos = 0;
    477  1.1  jmcneill 	sc->play.count = 0;
    478  1.1  jmcneill 	sc->play.buf = start;
    479  1.1  jmcneill 	size = (size_t)(((caddr_t)end - (caddr_t)start));
    480  1.1  jmcneill 	bufaddr = DMAADDR(p);
    481  1.1  jmcneill 	sc->play.start = bufaddr;
    482  1.1  jmcneill 
    483  1.1  jmcneill #define LO(x) ((x) & 0x0000ffff)
    484  1.1  jmcneill #define HI(x) ((x) >> 16)
    485  1.1  jmcneill 
    486  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_DAC_DATA +
    487  1.1  jmcneill 	    ESA_CDATA_HOST_SRC_ADDRL, LO(bufaddr));
    488  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_DAC_DATA +
    489  1.1  jmcneill 	    ESA_CDATA_HOST_SRC_ADDRH, HI(bufaddr));
    490  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_DAC_DATA +
    491  1.1  jmcneill 	    ESA_CDATA_HOST_SRC_END_PLUS_1L, LO(bufaddr + size));
    492  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_DAC_DATA +
    493  1.1  jmcneill 	    ESA_CDATA_HOST_SRC_END_PLUS_1H, HI(bufaddr + size));
    494  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_DAC_DATA +
    495  1.1  jmcneill 	    ESA_CDATA_HOST_SRC_CURRENTL, LO(bufaddr));
    496  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_DAC_DATA +
    497  1.1  jmcneill 	    ESA_CDATA_HOST_SRC_CURRENTH, HI(bufaddr));
    498  1.1  jmcneill 
    499  1.1  jmcneill 	/* DSP buffers */
    500  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_DAC_DATA +
    501  1.1  jmcneill 	    ESA_CDATA_IN_BUF_BEGIN, dsp_in_buf);
    502  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_DAC_DATA +
    503  1.1  jmcneill 	    ESA_CDATA_IN_BUF_END_PLUS_1, dsp_in_buf + (dsp_in_size / 2));
    504  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_DAC_DATA +
    505  1.1  jmcneill 	    ESA_CDATA_IN_BUF_HEAD, dsp_in_buf);
    506  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_DAC_DATA +
    507  1.1  jmcneill 	    ESA_CDATA_IN_BUF_TAIL, dsp_in_buf);
    508  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_DAC_DATA +
    509  1.1  jmcneill 	    ESA_CDATA_OUT_BUF_BEGIN, dsp_out_buf);
    510  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_DAC_DATA +
    511  1.1  jmcneill 	    ESA_CDATA_OUT_BUF_END_PLUS_1, dsp_out_buf + (dsp_out_size / 2));
    512  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_DAC_DATA +
    513  1.1  jmcneill 	    ESA_CDATA_OUT_BUF_HEAD, dsp_out_buf);
    514  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_DAC_DATA +
    515  1.1  jmcneill 	    ESA_CDATA_OUT_BUF_TAIL, dsp_out_buf);
    516  1.1  jmcneill 
    517  1.1  jmcneill 	/* Some per-client initializers */
    518  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_DAC_DATA +
    519  1.1  jmcneill 	    ESA_SRC3_DIRECTION_OFFSET + 12, ESA_DAC_DATA + 40 + 8);
    520  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_DAC_DATA +
    521  1.1  jmcneill 	    ESA_SRC3_DIRECTION_OFFSET + 19, 0x400 + ESA_MINISRC_COEF_LOC);
    522  1.1  jmcneill 	/* Enable or disable low-pass filter? (0xff if rate > 45000) */
    523  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_DAC_DATA +
    524  1.1  jmcneill 	    ESA_SRC3_DIRECTION_OFFSET + 22, 0);
    525  1.1  jmcneill 	/* Tell it which way DMA is going */
    526  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_DAC_DATA +
    527  1.1  jmcneill 	    ESA_CDATA_DMA_CONTROL,
    528  1.1  jmcneill 	    ESA_DMACONTROL_AUTOREPEAT + ESA_DMAC_PAGE3_SELECTOR +
    529  1.1  jmcneill 	    ESA_DMAC_BLOCKF_SELECTOR);
    530  1.1  jmcneill 
    531  1.1  jmcneill 	/* Set an armload of static initializers */
    532  1.1  jmcneill 	for (i = 0; i < (sizeof(esa_playvals) / sizeof(esa_playvals[0])); i++)
    533  1.1  jmcneill 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_DAC_DATA +
    534  1.1  jmcneill 		    esa_playvals[i].addr, esa_playvals[i].val);
    535  1.1  jmcneill 
    536  1.1  jmcneill 	/* Put us in the packed task lists */
    537  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
    538  1.1  jmcneill 	    ESA_KDATA_INSTANCE0_MINISRC,
    539  1.1  jmcneill 	    ESA_DAC_DATA >> ESA_DP_SHIFT_COUNT);
    540  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_DMA_XFER0,
    541  1.1  jmcneill 	    ESA_DAC_DATA >> ESA_DP_SHIFT_COUNT);
    542  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_MIXER_XFER0,
    543  1.1  jmcneill 	    ESA_DAC_DATA >> ESA_DP_SHIFT_COUNT);
    544  1.1  jmcneill #undef LO
    545  1.1  jmcneill #undef HI
    546  1.1  jmcneill 
    547  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
    548  1.1  jmcneill 	    ESA_KDATA_TIMER_COUNT_RELOAD, 240);
    549  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
    550  1.1  jmcneill 	    ESA_KDATA_TIMER_COUNT_CURRENT, 240);
    551  1.1  jmcneill 	data = bus_space_read_2(iot, ioh, ESA_HOST_INT_CTRL);
    552  1.1  jmcneill 	bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL,
    553  1.1  jmcneill 	    data | ESA_CLKRUN_GEN_ENABLE);
    554  1.1  jmcneill 
    555  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_DAC_DATA +
    556  1.1  jmcneill 	    ESA_CDATA_INSTANCE_READY, 1);
    557  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
    558  1.1  jmcneill 	    ESA_KDATA_MIXER_TASK_NUMBER, 1);
    559  1.1  jmcneill 
    560  1.1  jmcneill 	return (0);
    561  1.1  jmcneill }
    562  1.1  jmcneill 
    563  1.1  jmcneill int
    564  1.1  jmcneill esa_trigger_input(void *hdl, void *start, void *end, int blksize,
    565  1.1  jmcneill 			void (*intr)(void *), void *intrarg,
    566  1.1  jmcneill 			struct audio_params *param)
    567  1.1  jmcneill {
    568  1.1  jmcneill 
    569  1.1  jmcneill 	return (EOPNOTSUPP);
    570  1.1  jmcneill }
    571  1.1  jmcneill 
    572  1.1  jmcneill /* Interrupt handler */
    573  1.1  jmcneill 
    574  1.1  jmcneill int
    575  1.1  jmcneill esa_intr(void *hdl)
    576  1.1  jmcneill {
    577  1.1  jmcneill 	struct esa_softc *sc = hdl;
    578  1.1  jmcneill 	bus_space_tag_t iot = sc->sc_iot;
    579  1.1  jmcneill 	bus_space_handle_t ioh = sc->sc_ioh;
    580  1.1  jmcneill 	u_int32_t status, ctl;
    581  1.1  jmcneill 	u_int32_t pos;
    582  1.1  jmcneill 	u_int32_t diff;
    583  1.1  jmcneill 	u_int32_t blksize = sc->play.blksize;
    584  1.1  jmcneill 	u_int32_t bufsize = sc->play.bufsize;
    585  1.1  jmcneill 
    586  1.1  jmcneill 	status = bus_space_read_1(iot, ioh, ESA_HOST_INT_STATUS);
    587  1.1  jmcneill 	if (!status)
    588  1.1  jmcneill 		return (0);
    589  1.1  jmcneill 
    590  1.1  jmcneill 	/* ack the interrupt */
    591  1.1  jmcneill 	bus_space_write_1(iot, ioh, ESA_HOST_INT_STATUS, 0xff);
    592  1.1  jmcneill 
    593  1.1  jmcneill 	if (status & ESA_HV_INT_PENDING) {
    594  1.1  jmcneill 		u_int8_t event;
    595  1.1  jmcneill 
    596  1.1  jmcneill 		printf("%s: hardware volume interrupt\n", sc->sc_dev.dv_xname);
    597  1.1  jmcneill 		event = bus_space_read_1(iot, ioh, ESA_HW_VOL_COUNTER_MASTER);
    598  1.1  jmcneill 		switch(event) {
    599  1.1  jmcneill 		case 0x99:
    600  1.1  jmcneill 		case 0xaa:
    601  1.1  jmcneill 		case 0x66:
    602  1.1  jmcneill 		case 0x88:
    603  1.1  jmcneill 			printf("%s: esa_intr: FIXME\n", sc->sc_dev.dv_xname);
    604  1.1  jmcneill 			break;
    605  1.1  jmcneill 		default:
    606  1.1  jmcneill 			printf("%s: unknown hwvol event 0x%02x\n",
    607  1.1  jmcneill 			    sc->sc_dev.dv_xname, event);
    608  1.1  jmcneill 			break;
    609  1.1  jmcneill 		}
    610  1.1  jmcneill 		bus_space_write_1(iot, ioh, ESA_HW_VOL_COUNTER_MASTER, 0x88);
    611  1.1  jmcneill 	}
    612  1.1  jmcneill 
    613  1.1  jmcneill 	if (status & ESA_ASSP_INT_PENDING) {
    614  1.1  jmcneill 		ctl = bus_space_read_1(iot, ioh, ESA_ASSP_CONTROL_B);
    615  1.1  jmcneill 		if (!(ctl & ESA_STOP_ASSP_CLOCK)) {
    616  1.1  jmcneill 			ctl = bus_space_read_1(iot, ioh,
    617  1.1  jmcneill 					       ESA_ASSP_HOST_INT_STATUS);
    618  1.1  jmcneill 			if (ctl & ESA_DSP2HOST_REQ_TIMER) {
    619  1.1  jmcneill 				bus_space_write_1(iot, ioh,
    620  1.1  jmcneill 				    ESA_ASSP_HOST_INT_STATUS,
    621  1.1  jmcneill 				    ESA_DSP2HOST_REQ_TIMER);
    622  1.1  jmcneill 				if (sc->play.active) {
    623  1.1  jmcneill 					pos = esa_get_pointer(sc) % bufsize;
    624  1.1  jmcneill 					diff = (bufsize + pos - sc->play.pos)
    625  1.1  jmcneill 					    % bufsize;
    626  1.1  jmcneill 					sc->play.pos = pos;
    627  1.1  jmcneill 					sc->play.count += diff;
    628  1.1  jmcneill 					while(sc->play.count >= blksize) {
    629  1.1  jmcneill 						sc->play.count -= blksize;
    630  1.1  jmcneill 						(*sc->intr)(sc->arg);
    631  1.1  jmcneill 					}
    632  1.1  jmcneill 				}
    633  1.1  jmcneill 			}
    634  1.1  jmcneill 		}
    635  1.1  jmcneill 	}
    636  1.1  jmcneill 
    637  1.1  jmcneill 	return (1);
    638  1.1  jmcneill }
    639  1.1  jmcneill 
    640  1.1  jmcneill int
    641  1.1  jmcneill esa_allocmem(struct esa_softc *sc, size_t size, size_t align,
    642  1.1  jmcneill 		struct esa_dma *p)
    643  1.1  jmcneill {
    644  1.1  jmcneill 	int error;
    645  1.1  jmcneill 
    646  1.1  jmcneill 	p->size = size;
    647  1.1  jmcneill 	error = bus_dmamem_alloc(sc->sc_dmat, p->size, align, 0,
    648  1.1  jmcneill 				 p->segs, sizeof(p->segs) / sizeof(p->segs[0]),
    649  1.1  jmcneill 				 &p->nsegs, BUS_DMA_NOWAIT);
    650  1.1  jmcneill 	if (error)
    651  1.1  jmcneill 		return (error);
    652  1.1  jmcneill 
    653  1.1  jmcneill 	error = bus_dmamem_map(sc->sc_dmat, p->segs, p->nsegs, p->size,
    654  1.1  jmcneill 				&p->addr, BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
    655  1.1  jmcneill 	if (error)
    656  1.1  jmcneill 		goto free;
    657  1.1  jmcneill 
    658  1.1  jmcneill 	error = bus_dmamap_create(sc->sc_dmat, p->size, 1, p->size, 0,
    659  1.1  jmcneill 				  BUS_DMA_NOWAIT, &p->map);
    660  1.1  jmcneill 	if (error)
    661  1.1  jmcneill 		goto unmap;
    662  1.1  jmcneill 
    663  1.1  jmcneill 	error = bus_dmamap_load(sc->sc_dmat, p->map, p->addr, p->size, NULL,
    664  1.1  jmcneill 				BUS_DMA_NOWAIT);
    665  1.1  jmcneill 	if (error)
    666  1.1  jmcneill 		goto destroy;
    667  1.1  jmcneill 
    668  1.1  jmcneill 	return (0);
    669  1.1  jmcneill 
    670  1.1  jmcneill destroy:
    671  1.1  jmcneill 	bus_dmamap_destroy(sc->sc_dmat, p->map);
    672  1.1  jmcneill unmap:
    673  1.1  jmcneill 	bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
    674  1.1  jmcneill free:
    675  1.1  jmcneill 	bus_dmamem_free(sc->sc_dmat, p->segs, p->nsegs);
    676  1.1  jmcneill 
    677  1.1  jmcneill 	return (error);
    678  1.1  jmcneill }
    679  1.1  jmcneill 
    680  1.1  jmcneill int
    681  1.1  jmcneill esa_freemem(struct esa_softc *sc, struct esa_dma *p)
    682  1.1  jmcneill {
    683  1.1  jmcneill 
    684  1.1  jmcneill 	bus_dmamap_unload(sc->sc_dmat, p->map);
    685  1.1  jmcneill 	bus_dmamap_destroy(sc->sc_dmat, p->map);
    686  1.1  jmcneill 	bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
    687  1.1  jmcneill 	bus_dmamem_free(sc->sc_dmat, p->segs, p->nsegs);
    688  1.1  jmcneill 
    689  1.1  jmcneill 	return (0);
    690  1.1  jmcneill }
    691  1.1  jmcneill 
    692  1.1  jmcneill /*
    693  1.1  jmcneill  * Supporting Subroutines
    694  1.1  jmcneill  */
    695  1.1  jmcneill 
    696  1.1  jmcneill int
    697  1.1  jmcneill esa_match(struct device *dev, struct cfdata *match, void *aux)
    698  1.1  jmcneill {
    699  1.1  jmcneill 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    700  1.1  jmcneill 
    701  1.1  jmcneill 	switch(PCI_VENDOR(pa->pa_id)) {
    702  1.1  jmcneill 	case PCI_VENDOR_ESSTECH:
    703  1.1  jmcneill 		switch(PCI_PRODUCT(pa->pa_id)) {
    704  1.1  jmcneill 		case PCI_PRODUCT_ESSTECH_ALLEGRO1:
    705  1.1  jmcneill 		case PCI_PRODUCT_ESSTECH_MAESTRO3:
    706  1.1  jmcneill 		case PCI_PRODUCT_ESSTECH_MAESTRO3_2:
    707  1.1  jmcneill 			return (1);
    708  1.1  jmcneill 		}
    709  1.1  jmcneill 	}
    710  1.1  jmcneill 
    711  1.1  jmcneill 	return (0);
    712  1.1  jmcneill }
    713  1.1  jmcneill 
    714  1.1  jmcneill void
    715  1.1  jmcneill esa_attach(struct device *parent, struct device *self, void *aux)
    716  1.1  jmcneill {
    717  1.1  jmcneill 	struct esa_softc *sc = (struct esa_softc *)self;
    718  1.1  jmcneill 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    719  1.1  jmcneill 	pcitag_t tag = pa->pa_tag;
    720  1.1  jmcneill 	pci_chipset_tag_t pc = pa->pa_pc;
    721  1.1  jmcneill 	pci_intr_handle_t ih;
    722  1.1  jmcneill 	struct esa_card_type *card;
    723  1.1  jmcneill 	const char *intrstr;
    724  1.1  jmcneill 	u_int32_t data;
    725  1.1  jmcneill 	char devinfo[256];
    726  1.1  jmcneill 	int revision;
    727  1.1  jmcneill 
    728  1.1  jmcneill 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
    729  1.1  jmcneill 	revision = PCI_REVISION(pa->pa_class);
    730  1.1  jmcneill 	printf(": %s (rev. 0x%02x)\n", devinfo, revision);
    731  1.1  jmcneill 
    732  1.1  jmcneill 	for (card = esa_card_types; card->pci_vendor_id; card++)
    733  1.1  jmcneill 		if (PCI_VENDOR(pa->pa_id) == card->pci_vendor_id &&
    734  1.1  jmcneill 		    PCI_PRODUCT(pa->pa_id) == card->pci_product_id) {
    735  1.1  jmcneill 			sc->type = card->type;
    736  1.1  jmcneill 			sc->delay1 = card->delay1;
    737  1.1  jmcneill 			sc->delay2 = card->delay2;
    738  1.1  jmcneill 			break;
    739  1.1  jmcneill 		}
    740  1.1  jmcneill 
    741  1.1  jmcneill 	data = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    742  1.1  jmcneill 	data |= (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE
    743  1.1  jmcneill 	    | PCI_COMMAND_MASTER_ENABLE);
    744  1.1  jmcneill 	pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, data);
    745  1.1  jmcneill 
    746  1.1  jmcneill 	/* Map I/O register */
    747  1.1  jmcneill 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
    748  1.1  jmcneill 	    &sc->sc_iot, &sc->sc_ioh, &sc->sc_iob, &sc->sc_ios)) {
    749  1.1  jmcneill 		printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
    750  1.1  jmcneill 		return;
    751  1.1  jmcneill 	}
    752  1.1  jmcneill 
    753  1.1  jmcneill 	/* Initialize softc */
    754  1.1  jmcneill 	sc->sc_tag = tag;
    755  1.1  jmcneill 	sc->sc_pct = pc;
    756  1.1  jmcneill 	sc->sc_dmat = pa->pa_dmat;
    757  1.1  jmcneill 
    758  1.1  jmcneill 	/* Map and establish an interrupt */
    759  1.1  jmcneill 	if (pci_intr_map(pa, &ih)) {
    760  1.1  jmcneill 		printf("%s: can't map interrupt\n", sc->sc_dev.dv_xname);
    761  1.1  jmcneill 		return;
    762  1.1  jmcneill 	}
    763  1.1  jmcneill 	intrstr = pci_intr_string(pc, ih);
    764  1.1  jmcneill 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, esa_intr, self);
    765  1.1  jmcneill 	if (sc->sc_ih == NULL) {
    766  1.1  jmcneill 		printf("%s: can't establish interrupt", sc->sc_dev.dv_xname);
    767  1.1  jmcneill 		if (intrstr != NULL)
    768  1.1  jmcneill 			printf(" at %s", intrstr);
    769  1.1  jmcneill 		printf("\n");
    770  1.1  jmcneill 		return;
    771  1.1  jmcneill 	}
    772  1.1  jmcneill 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    773  1.1  jmcneill 
    774  1.1  jmcneill 	/* Power up chip */
    775  1.1  jmcneill 	esa_power(sc, 0);
    776  1.1  jmcneill 
    777  1.1  jmcneill 	/* Init chip */
    778  1.1  jmcneill 	if (esa_init(sc) == -1) {
    779  1.1  jmcneill 		printf("%s: esa_attach: unable to initialize the card\n",
    780  1.1  jmcneill 		    sc->sc_dev.dv_xname);
    781  1.1  jmcneill 		return;
    782  1.1  jmcneill 	}
    783  1.1  jmcneill 
    784  1.1  jmcneill 	/* Attach AC97 host interface */
    785  1.1  jmcneill 	sc->host_if.arg = self;
    786  1.1  jmcneill 	sc->host_if.attach = esa_attach_codec;
    787  1.1  jmcneill 	sc->host_if.read = esa_read_codec;
    788  1.1  jmcneill 	sc->host_if.write = esa_write_codec;
    789  1.1  jmcneill 	sc->host_if.reset = esa_reset_codec;
    790  1.1  jmcneill 	sc->host_if.flags = esa_flags_codec;
    791  1.1  jmcneill 
    792  1.1  jmcneill 	if (ac97_attach(&sc->host_if) != 0)
    793  1.1  jmcneill 		return;
    794  1.1  jmcneill 
    795  1.1  jmcneill 	sc->sc_audiodev = audio_attach_mi(&esa_hw_if, self, &sc->sc_dev);
    796  1.1  jmcneill 
    797  1.1  jmcneill 	return;
    798  1.1  jmcneill }
    799  1.1  jmcneill 
    800  1.1  jmcneill int
    801  1.1  jmcneill esa_detach(struct device *self, int flags)
    802  1.1  jmcneill {
    803  1.1  jmcneill 	struct esa_softc *sc = (struct esa_softc *)self;
    804  1.1  jmcneill 	int rv = 0;
    805  1.1  jmcneill 
    806  1.1  jmcneill 	if (sc->sc_audiodev != NULL)
    807  1.1  jmcneill 		rv = config_detach(sc->sc_audiodev, flags);
    808  1.1  jmcneill 	if (rv)
    809  1.1  jmcneill 		return (rv);
    810  1.1  jmcneill 
    811  1.1  jmcneill 	if (sc->sc_ih != NULL)
    812  1.1  jmcneill 		pci_intr_disestablish(sc->sc_pct, sc->sc_ih);
    813  1.1  jmcneill 	if (sc->sc_ios)
    814  1.1  jmcneill 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
    815  1.1  jmcneill 
    816  1.1  jmcneill 	return (0);
    817  1.1  jmcneill }
    818  1.1  jmcneill 
    819  1.1  jmcneill u_int16_t
    820  1.1  jmcneill esa_read_assp(struct esa_softc *sc, u_int16_t region, u_int16_t index)
    821  1.1  jmcneill {
    822  1.1  jmcneill 	u_int16_t data;
    823  1.1  jmcneill 	bus_space_tag_t iot = sc->sc_iot;
    824  1.1  jmcneill 	bus_space_handle_t ioh = sc->sc_ioh;
    825  1.1  jmcneill 
    826  1.1  jmcneill 	bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_TYPE,
    827  1.1  jmcneill 	    region & ESA_MEMTYPE_MASK);
    828  1.1  jmcneill 	bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_INDEX, index);
    829  1.1  jmcneill 	data = bus_space_read_2(iot, ioh, ESA_DSP_PORT_MEMORY_DATA);
    830  1.1  jmcneill 
    831  1.1  jmcneill 	return (data);
    832  1.1  jmcneill }
    833  1.1  jmcneill 
    834  1.1  jmcneill void
    835  1.1  jmcneill esa_write_assp(struct esa_softc *sc, u_int16_t region, u_int16_t index,
    836  1.1  jmcneill 		u_int16_t data)
    837  1.1  jmcneill {
    838  1.1  jmcneill 	bus_space_tag_t iot = sc->sc_iot;
    839  1.1  jmcneill 	bus_space_handle_t ioh = sc->sc_ioh;
    840  1.1  jmcneill 
    841  1.1  jmcneill 	bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_TYPE,
    842  1.1  jmcneill 	    region & ESA_MEMTYPE_MASK);
    843  1.1  jmcneill 	bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_INDEX, index);
    844  1.1  jmcneill 	bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_DATA, data);
    845  1.1  jmcneill 
    846  1.1  jmcneill 	return;
    847  1.1  jmcneill }
    848  1.1  jmcneill 
    849  1.1  jmcneill int
    850  1.1  jmcneill esa_init_codec(struct esa_softc *sc)
    851  1.1  jmcneill {
    852  1.1  jmcneill 	bus_space_tag_t iot = sc->sc_iot;
    853  1.1  jmcneill 	bus_space_handle_t ioh = sc->sc_ioh;
    854  1.1  jmcneill 	u_int32_t data;
    855  1.1  jmcneill 
    856  1.1  jmcneill 	data = bus_space_read_1(iot, ioh, ESA_CODEC_COMMAND);
    857  1.1  jmcneill 
    858  1.1  jmcneill 	return ((data & 0x1) ? 0 : 1);
    859  1.1  jmcneill }
    860  1.1  jmcneill 
    861  1.1  jmcneill int
    862  1.1  jmcneill esa_attach_codec(void *aux, struct ac97_codec_if *codec_if)
    863  1.1  jmcneill {
    864  1.1  jmcneill 	struct esa_softc *sc = aux;
    865  1.1  jmcneill 
    866  1.1  jmcneill 	sc->codec_if = codec_if;
    867  1.1  jmcneill 
    868  1.1  jmcneill 	return (0);
    869  1.1  jmcneill }
    870  1.1  jmcneill 
    871  1.1  jmcneill int
    872  1.1  jmcneill esa_read_codec(void *aux, u_int8_t reg, u_int16_t *result)
    873  1.1  jmcneill {
    874  1.1  jmcneill 	struct esa_softc *sc = aux;
    875  1.1  jmcneill 	bus_space_tag_t iot = sc->sc_iot;
    876  1.1  jmcneill 	bus_space_handle_t ioh = sc->sc_ioh;
    877  1.1  jmcneill 
    878  1.1  jmcneill 	if (esa_wait(sc))
    879  1.1  jmcneill 		printf("%s: esa_read_codec: timed out\n", sc->sc_dev.dv_xname);
    880  1.1  jmcneill 	bus_space_write_1(iot, ioh, ESA_CODEC_COMMAND, (reg & 0x7f) | 0x80);
    881  1.1  jmcneill 	delay(50);
    882  1.1  jmcneill 	if (esa_wait(sc))
    883  1.1  jmcneill 		printf("%s: esa_read_codec: timed out\n", sc->sc_dev.dv_xname);
    884  1.1  jmcneill 	*result = bus_space_read_2(iot, ioh, ESA_CODEC_DATA);
    885  1.1  jmcneill 
    886  1.1  jmcneill 	return (0);
    887  1.1  jmcneill }
    888  1.1  jmcneill 
    889  1.1  jmcneill int
    890  1.1  jmcneill esa_write_codec(void *aux, u_int8_t reg, u_int16_t data)
    891  1.1  jmcneill {
    892  1.1  jmcneill 	struct esa_softc *sc = aux;
    893  1.1  jmcneill 	bus_space_tag_t iot = sc->sc_iot;
    894  1.1  jmcneill 	bus_space_handle_t ioh = sc->sc_ioh;
    895  1.1  jmcneill 
    896  1.1  jmcneill 	if (esa_wait(sc)) {
    897  1.1  jmcneill 		printf("%s: esa_write_codec: timed out\n", sc->sc_dev.dv_xname);
    898  1.1  jmcneill 		return (-1);
    899  1.1  jmcneill 	}
    900  1.1  jmcneill 	bus_space_write_2(iot, ioh, ESA_CODEC_DATA, data);
    901  1.1  jmcneill 	bus_space_write_1(iot, ioh, ESA_CODEC_COMMAND, reg & 0x7f);
    902  1.1  jmcneill 	delay(50);
    903  1.1  jmcneill 
    904  1.1  jmcneill 	return (0);
    905  1.1  jmcneill }
    906  1.1  jmcneill 
    907  1.1  jmcneill void
    908  1.1  jmcneill esa_reset_codec(void *aux)
    909  1.1  jmcneill {
    910  1.1  jmcneill 
    911  1.1  jmcneill 	return;
    912  1.1  jmcneill }
    913  1.1  jmcneill 
    914  1.1  jmcneill enum ac97_host_flags
    915  1.1  jmcneill esa_flags_codec(void *aux)
    916  1.1  jmcneill {
    917  1.1  jmcneill 	struct esa_softc *sc = aux;
    918  1.1  jmcneill 
    919  1.1  jmcneill 	return (sc->codec_flags);
    920  1.1  jmcneill }
    921  1.1  jmcneill 
    922  1.1  jmcneill int
    923  1.1  jmcneill esa_wait(struct esa_softc *sc)
    924  1.1  jmcneill {
    925  1.1  jmcneill 	int i, val;
    926  1.1  jmcneill 	bus_space_tag_t iot = sc->sc_iot;
    927  1.1  jmcneill 	bus_space_handle_t ioh = sc->sc_ioh;
    928  1.1  jmcneill 
    929  1.1  jmcneill 	for (i = 0; i < 20; i++) {
    930  1.1  jmcneill 		val = bus_space_read_1(iot, ioh, ESA_CODEC_STATUS);
    931  1.1  jmcneill 		if ((val & 1) == 0)
    932  1.1  jmcneill 			return (0);
    933  1.1  jmcneill 		delay(2);
    934  1.1  jmcneill 	}
    935  1.1  jmcneill 
    936  1.1  jmcneill 	return (-1);
    937  1.1  jmcneill }
    938  1.1  jmcneill 
    939  1.1  jmcneill int
    940  1.1  jmcneill esa_init(struct esa_softc *sc)
    941  1.1  jmcneill {
    942  1.1  jmcneill 	bus_space_tag_t iot = sc->sc_iot;
    943  1.1  jmcneill 	bus_space_handle_t ioh = sc->sc_ioh;
    944  1.1  jmcneill 	pcitag_t tag = sc->sc_tag;
    945  1.1  jmcneill 	pci_chipset_tag_t pc = sc->sc_pct;
    946  1.1  jmcneill 	u_int32_t data, i, size;
    947  1.1  jmcneill 	u_int8_t reset_state;
    948  1.1  jmcneill 
    949  1.1  jmcneill 	/* Disable legacy emulation */
    950  1.1  jmcneill 	data = pci_conf_read(pc, tag, PCI_LEGACY_AUDIO_CTRL);
    951  1.1  jmcneill 	data |= DISABLE_LEGACY;
    952  1.1  jmcneill 	pci_conf_write(pc, tag, PCI_LEGACY_AUDIO_CTRL, data);
    953  1.1  jmcneill 
    954  1.1  jmcneill 	esa_config(sc);
    955  1.1  jmcneill 
    956  1.1  jmcneill 	reset_state = esa_assp_halt(sc);
    957  1.1  jmcneill 
    958  1.1  jmcneill 	esa_init_codec(sc);
    959  1.1  jmcneill 	esa_codec_reset(sc);
    960  1.1  jmcneill 
    961  1.1  jmcneill 	/* Zero kernel and mixer data */
    962  1.1  jmcneill 	size = ESA_REV_B_DATA_MEMORY_UNIT_LENGTH * ESA_NUM_UNITS_KERNEL_DATA;
    963  1.1  jmcneill 	for (i = 0; i < size / 2; i++) {
    964  1.1  jmcneill 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
    965  1.1  jmcneill 		    ESA_KDATA_BASE_ADDR + i, 0);
    966  1.1  jmcneill 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
    967  1.1  jmcneill 		    ESA_KDATA_BASE_ADDR2 + i, 0);
    968  1.1  jmcneill 	}
    969  1.1  jmcneill 
    970  1.1  jmcneill 	/* Init DMA pointer */
    971  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_CURRENT_DMA,
    972  1.1  jmcneill 	    ESA_KDATA_DMA_XFER0);
    973  1.1  jmcneill 
    974  1.1  jmcneill 	/* Write kernel code into memory */
    975  1.1  jmcneill 	size = sizeof(esa_assp_kernel_image);
    976  1.1  jmcneill 	for (i = 0; i < size / 2; i++)
    977  1.1  jmcneill 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE,
    978  1.1  jmcneill 		    ESA_REV_B_CODE_MEMORY_BEGIN + i, esa_assp_kernel_image[i]);
    979  1.1  jmcneill 
    980  1.1  jmcneill 	size = sizeof(esa_assp_minisrc_image);
    981  1.1  jmcneill 	for (i = 0; i < size / 2; i++)
    982  1.1  jmcneill 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE, 0x400 + i,
    983  1.1  jmcneill 		    esa_assp_minisrc_image[i]);
    984  1.1  jmcneill 
    985  1.1  jmcneill 	/* Write the coefficients for the low pass filter */
    986  1.1  jmcneill 	size = sizeof(esa_minisrc_lpf_image);
    987  1.1  jmcneill 	for (i = 0; i < size / 2; i++)
    988  1.1  jmcneill 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE,
    989  1.1  jmcneill 		    0x400 + ESA_MINISRC_COEF_LOC + i, esa_minisrc_lpf_image[i]);
    990  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE,
    991  1.1  jmcneill 	    0x400 + ESA_MINISRC_COEF_LOC + size, 0x8000);
    992  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_TASK0, 0x400);
    993  1.1  jmcneill 	/* Init the mixer number */
    994  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
    995  1.1  jmcneill              ESA_KDATA_MIXER_TASK_NUMBER, 0);
    996  1.1  jmcneill 	/* Extreme kernel master volume */
    997  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_DAC_LEFT_VOLUME,
    998  1.1  jmcneill 	    ESA_ARB_VOLUME);
    999  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
   1000  1.1  jmcneill             ESA_KDATA_DAC_RIGHT_VOLUME, ESA_ARB_VOLUME);
   1001  1.1  jmcneill 
   1002  1.1  jmcneill 	if (esa_amp_enable(sc))
   1003  1.1  jmcneill 		return (-1);
   1004  1.1  jmcneill 
   1005  1.1  jmcneill 	/* Zero entire DAC/ADC area */
   1006  1.1  jmcneill 	for (i = 0x1100; i < 0x1c00; i++)
   1007  1.1  jmcneill 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, i, 0);
   1008  1.1  jmcneill 
   1009  1.1  jmcneill 	esa_enable_interrupts(sc);
   1010  1.1  jmcneill 
   1011  1.1  jmcneill 	bus_space_write_1(iot, ioh, ESA_DSP_PORT_CONTROL_REG_B,
   1012  1.1  jmcneill 	    reset_state | ESA_REGB_ENABLE_RESET);
   1013  1.1  jmcneill 
   1014  1.1  jmcneill 	return (0);
   1015  1.1  jmcneill }
   1016  1.1  jmcneill 
   1017  1.1  jmcneill void
   1018  1.1  jmcneill esa_config(struct esa_softc *sc)
   1019  1.1  jmcneill {
   1020  1.1  jmcneill 	bus_space_tag_t iot = sc->sc_iot;
   1021  1.1  jmcneill 	bus_space_handle_t ioh = sc->sc_ioh;
   1022  1.1  jmcneill 	pcitag_t tag = sc->sc_tag;
   1023  1.1  jmcneill 	pci_chipset_tag_t pc = sc->sc_pct;
   1024  1.1  jmcneill 	u_int32_t data;
   1025  1.1  jmcneill 
   1026  1.1  jmcneill 	data = pci_conf_read(pc, tag, ESA_PCI_ALLEGRO_CONFIG);
   1027  1.1  jmcneill 	data &= ESA_REDUCED_DEBOUNCE;
   1028  1.1  jmcneill 	data |= ESA_PM_CTRL_ENABLE | ESA_CLK_DIV_BY_49 | ESA_USE_PCI_TIMING;
   1029  1.1  jmcneill 	pci_conf_write(pc, tag, ESA_PCI_ALLEGRO_CONFIG, data);
   1030  1.1  jmcneill 
   1031  1.1  jmcneill 	bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_B, ESA_RESET_ASSP);
   1032  1.1  jmcneill 	data = pci_conf_read(pc, tag, ESA_PCI_ALLEGRO_CONFIG);
   1033  1.1  jmcneill 	data &= ~ESA_INT_CLK_SELECT;
   1034  1.1  jmcneill 	if (sc->type == ESS_MAESTRO3) {
   1035  1.1  jmcneill 		data &= ~ESA_INT_CLK_MULT_ENABLE;
   1036  1.1  jmcneill 		data |= ESA_INT_CLK_SRC_NOT_PCI;
   1037  1.1  jmcneill 	}
   1038  1.1  jmcneill 	data &= ~(ESA_CLK_MULT_MODE_SELECT | ESA_CLK_MULT_MODE_SELECT_2);
   1039  1.1  jmcneill 	pci_conf_write(pc, tag, ESA_PCI_ALLEGRO_CONFIG, data);
   1040  1.1  jmcneill 
   1041  1.1  jmcneill 	if (sc->type == ESS_ALLEGRO1) {
   1042  1.1  jmcneill 		data = pci_conf_read(pc, tag, ESA_PCI_USER_CONFIG);
   1043  1.1  jmcneill 		data |= ESA_IN_CLK_12MHZ_SELECT;
   1044  1.1  jmcneill 		pci_conf_write(pc, tag, ESA_PCI_USER_CONFIG, data);
   1045  1.1  jmcneill 	}
   1046  1.1  jmcneill 
   1047  1.1  jmcneill 	data = bus_space_read_1(iot, ioh, ESA_ASSP_CONTROL_A);
   1048  1.1  jmcneill 	data &= ~(ESA_DSP_CLK_36MHZ_SELECT | ESA_ASSP_CLK_49MHZ_SELECT);
   1049  1.1  jmcneill 	data |= ESA_ASSP_CLK_49MHZ_SELECT;	/* XXX: Assumes 49MHz DSP */
   1050  1.1  jmcneill 	data |= ESA_ASSP_0_WS_ENABLE;
   1051  1.1  jmcneill 	bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_A, data);
   1052  1.1  jmcneill 
   1053  1.1  jmcneill 	bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_B, ESA_RUN_ASSP);
   1054  1.1  jmcneill 
   1055  1.1  jmcneill 	return;
   1056  1.1  jmcneill }
   1057  1.1  jmcneill 
   1058  1.1  jmcneill u_int8_t
   1059  1.1  jmcneill esa_assp_halt(struct esa_softc *sc)
   1060  1.1  jmcneill {
   1061  1.1  jmcneill 	bus_space_tag_t iot = sc->sc_iot;
   1062  1.1  jmcneill 	bus_space_handle_t ioh = sc->sc_ioh;
   1063  1.1  jmcneill 	u_int8_t data, reset_state;
   1064  1.1  jmcneill 
   1065  1.1  jmcneill 	data = bus_space_read_1(iot, ioh, ESA_DSP_PORT_CONTROL_REG_B);
   1066  1.1  jmcneill 	reset_state = data & ~ESA_REGB_STOP_CLOCK;
   1067  1.1  jmcneill 	delay(10000);		/* XXX use tsleep */
   1068  1.1  jmcneill 	bus_space_write_1(iot, ioh, ESA_DSP_PORT_CONTROL_REG_B,
   1069  1.1  jmcneill 			reset_state & ~ESA_REGB_ENABLE_RESET);
   1070  1.1  jmcneill 	delay(10000);		/* XXX use tsleep */
   1071  1.1  jmcneill 
   1072  1.1  jmcneill 	return (reset_state);
   1073  1.1  jmcneill }
   1074  1.1  jmcneill 
   1075  1.1  jmcneill void
   1076  1.1  jmcneill esa_codec_reset(struct esa_softc *sc)
   1077  1.1  jmcneill {
   1078  1.1  jmcneill 	bus_space_tag_t iot = sc->sc_iot;
   1079  1.1  jmcneill 	bus_space_handle_t ioh = sc->sc_ioh;
   1080  1.1  jmcneill 	u_int16_t data, dir;
   1081  1.1  jmcneill 	int retry = 0;
   1082  1.1  jmcneill 
   1083  1.1  jmcneill 	do {
   1084  1.1  jmcneill 		data = bus_space_read_2(iot, ioh, ESA_GPIO_DIRECTION);
   1085  1.1  jmcneill 		dir = data | 0x10; /* assuming pci bus master? */
   1086  1.1  jmcneill 
   1087  1.1  jmcneill 		/* remote codec config */
   1088  1.1  jmcneill 		data = bus_space_read_2(iot, ioh, ESA_RING_BUS_CTRL_B);
   1089  1.1  jmcneill 		bus_space_write_2(iot, ioh, ESA_RING_BUS_CTRL_B,
   1090  1.1  jmcneill 		    data & ~ESA_SECOND_CODEC_ID_MASK);
   1091  1.1  jmcneill 		data = bus_space_read_2(iot, ioh, ESA_SDO_OUT_DEST_CTRL);
   1092  1.1  jmcneill 		bus_space_write_2(iot, ioh, ESA_SDO_OUT_DEST_CTRL,
   1093  1.1  jmcneill 		    data & ~ESA_COMMAND_ADDR_OUT);
   1094  1.1  jmcneill 		data = bus_space_read_2(iot, ioh, ESA_SDO_IN_DEST_CTRL);
   1095  1.1  jmcneill 		bus_space_write_2(iot, ioh, ESA_SDO_IN_DEST_CTRL,
   1096  1.1  jmcneill 		    data & ~ESA_STATUS_ADDR_IN);
   1097  1.1  jmcneill 
   1098  1.1  jmcneill 		bus_space_write_2(iot, ioh, ESA_RING_BUS_CTRL_A,
   1099  1.1  jmcneill 				  ESA_IO_SRAM_ENABLE);
   1100  1.1  jmcneill 		delay(20);
   1101  1.1  jmcneill 
   1102  1.1  jmcneill 		bus_space_write_2(iot, ioh, ESA_GPIO_DIRECTION,
   1103  1.1  jmcneill 		    dir & ~ESA_GPO_PRIMARY_AC97);
   1104  1.1  jmcneill 		bus_space_write_2(iot, ioh, ESA_GPIO_MASK,
   1105  1.1  jmcneill 				  ~ESA_GPO_PRIMARY_AC97);
   1106  1.1  jmcneill 		bus_space_write_2(iot, ioh, ESA_GPIO_DATA, 0);
   1107  1.1  jmcneill 		bus_space_write_2(iot, ioh, ESA_GPIO_DIRECTION,
   1108  1.1  jmcneill 		    dir | ESA_GPO_PRIMARY_AC97);
   1109  1.1  jmcneill 		delay(sc->delay1 * 1000);
   1110  1.1  jmcneill 		bus_space_write_2(iot, ioh, ESA_GPIO_DATA,
   1111  1.1  jmcneill 				  ESA_GPO_PRIMARY_AC97);
   1112  1.1  jmcneill 		delay(5);
   1113  1.1  jmcneill 		bus_space_write_2(iot, ioh, ESA_RING_BUS_CTRL_A,
   1114  1.1  jmcneill 		    ESA_IO_SRAM_ENABLE | ESA_SERIAL_AC_LINK_ENABLE);
   1115  1.1  jmcneill 		bus_space_write_2(iot, ioh, ESA_GPIO_MASK, ~0);
   1116  1.1  jmcneill 		delay(sc->delay2 * 1000);
   1117  1.1  jmcneill 
   1118  1.1  jmcneill 		esa_read_codec(sc, 0x7c, &data);
   1119  1.1  jmcneill 		if ((data == 0) || (data == 0xffff)) {
   1120  1.1  jmcneill 			retry++;
   1121  1.1  jmcneill 			if (retry > 3) {
   1122  1.1  jmcneill 				printf("%s: esa_codec_reset: failed\n",
   1123  1.1  jmcneill 				    sc->sc_dev.dv_xname);
   1124  1.1  jmcneill 				break;
   1125  1.1  jmcneill 			}
   1126  1.1  jmcneill 			printf("%s: esa_codec_reset: retrying\n",
   1127  1.1  jmcneill 			    sc->sc_dev.dv_xname);
   1128  1.1  jmcneill 		} else
   1129  1.1  jmcneill 			retry = 0;
   1130  1.1  jmcneill 	} while (retry);
   1131  1.1  jmcneill 
   1132  1.1  jmcneill 	return;
   1133  1.1  jmcneill }
   1134  1.1  jmcneill 
   1135  1.1  jmcneill int
   1136  1.1  jmcneill esa_amp_enable(struct esa_softc *sc)
   1137  1.1  jmcneill {
   1138  1.1  jmcneill 	bus_space_tag_t iot = sc->sc_iot;
   1139  1.1  jmcneill 	bus_space_handle_t ioh = sc->sc_ioh;
   1140  1.1  jmcneill 	u_int32_t gpo, polarity_port, polarity;
   1141  1.1  jmcneill 	u_int16_t data;
   1142  1.1  jmcneill 
   1143  1.1  jmcneill 	switch (sc->type) {
   1144  1.1  jmcneill 	case ESS_ALLEGRO1:
   1145  1.1  jmcneill 		polarity_port = 0x1800;
   1146  1.1  jmcneill 		break;
   1147  1.1  jmcneill 	case ESS_MAESTRO3:
   1148  1.1  jmcneill 		polarity_port = 0x1100;
   1149  1.1  jmcneill 		break;
   1150  1.1  jmcneill 	default:
   1151  1.1  jmcneill 		printf("%s: esa_amp_enable: Unknown chip type!!!\n",
   1152  1.1  jmcneill 		    sc->sc_dev.dv_xname);
   1153  1.1  jmcneill 		return (1);
   1154  1.1  jmcneill 	}
   1155  1.1  jmcneill 
   1156  1.1  jmcneill 	gpo = (polarity_port >> 8) & 0x0f;
   1157  1.1  jmcneill 	polarity = polarity_port >> 12;
   1158  1.1  jmcneill 	polarity = !polarity;	/* Enable */
   1159  1.1  jmcneill 	polarity = polarity << gpo;
   1160  1.1  jmcneill 	gpo = 1 << gpo;
   1161  1.1  jmcneill 	bus_space_write_2(iot, ioh, ESA_GPIO_MASK, ~gpo);
   1162  1.1  jmcneill 	data = bus_space_read_2(iot, ioh, ESA_GPIO_DIRECTION);
   1163  1.1  jmcneill 	bus_space_write_2(iot, ioh, ESA_GPIO_DIRECTION, data | gpo);
   1164  1.1  jmcneill 	data = ESA_GPO_SECONDARY_AC97 | ESA_GPO_PRIMARY_AC97 | polarity;
   1165  1.1  jmcneill 	bus_space_write_2(iot, ioh, ESA_GPIO_DATA, data);
   1166  1.1  jmcneill 	bus_space_write_2(iot, ioh, ESA_GPIO_MASK, ~0);
   1167  1.1  jmcneill 
   1168  1.1  jmcneill 	return (0);
   1169  1.1  jmcneill }
   1170  1.1  jmcneill 
   1171  1.1  jmcneill void
   1172  1.1  jmcneill esa_enable_interrupts(struct esa_softc *sc)
   1173  1.1  jmcneill {
   1174  1.1  jmcneill 	bus_space_tag_t iot = sc->sc_iot;
   1175  1.1  jmcneill 	bus_space_handle_t ioh = sc->sc_ioh;
   1176  1.1  jmcneill 	u_int8_t data;
   1177  1.1  jmcneill 
   1178  1.1  jmcneill 	bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL,
   1179  1.1  jmcneill 	    ESA_ASSP_INT_ENABLE | ESA_HV_INT_ENABLE);
   1180  1.1  jmcneill 	data = bus_space_read_1(iot, ioh, ESA_ASSP_CONTROL_C);
   1181  1.1  jmcneill 	bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_C,
   1182  1.1  jmcneill 	    data | ESA_ASSP_HOST_INT_ENABLE);
   1183  1.1  jmcneill }
   1184  1.1  jmcneill 
   1185  1.1  jmcneill int
   1186  1.1  jmcneill esa_power(struct esa_softc *sc, int state)
   1187  1.1  jmcneill {
   1188  1.1  jmcneill 	pcitag_t tag = sc->sc_tag;
   1189  1.1  jmcneill 	pci_chipset_tag_t pc = sc->sc_pct;
   1190  1.1  jmcneill 	u_int32_t data;
   1191  1.1  jmcneill 
   1192  1.1  jmcneill 	data = pci_conf_read(pc, tag, 0x34);
   1193  1.1  jmcneill 	if (pci_conf_read(pc, tag, data) == 1)
   1194  1.1  jmcneill 		pci_conf_write(pc, tag, data + 4, state);
   1195  1.1  jmcneill 
   1196  1.1  jmcneill 	return (0);
   1197  1.1  jmcneill }
   1198  1.1  jmcneill 
   1199  1.1  jmcneill u_int32_t
   1200  1.1  jmcneill esa_get_pointer(struct esa_softc *sc)
   1201  1.1  jmcneill {
   1202  1.1  jmcneill 	u_int16_t hi = 0, lo = 0;
   1203  1.1  jmcneill 	u_int32_t addr;
   1204  1.1  jmcneill 
   1205  1.1  jmcneill 	hi = esa_read_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_DAC_DATA +
   1206  1.1  jmcneill 	    ESA_CDATA_HOST_SRC_CURRENTH);
   1207  1.1  jmcneill 	lo = esa_read_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_DAC_DATA +
   1208  1.1  jmcneill 	    ESA_CDATA_HOST_SRC_CURRENTL);
   1209  1.1  jmcneill 
   1210  1.1  jmcneill 	addr = lo | ((u_int32_t)hi << 16);
   1211  1.1  jmcneill 	return (addr - sc->play.start);
   1212  1.1  jmcneill }
   1213  1.1  jmcneill 
   1214  1.1  jmcneill paddr_t
   1215  1.1  jmcneill esa_mappage(void *addr, void *mem, off_t off, int prot)
   1216  1.1  jmcneill {
   1217  1.1  jmcneill 	struct esa_softc *sc = addr;
   1218  1.1  jmcneill 	struct esa_dma *p;
   1219  1.1  jmcneill 
   1220  1.1  jmcneill 	if (off < 0)
   1221  1.1  jmcneill 		return (-1);
   1222  1.1  jmcneill 	for (p = sc->sc_dmas; p && KERNADDR(p) != mem; p = p->next)
   1223  1.1  jmcneill 		;
   1224  1.1  jmcneill 	if (!p)
   1225  1.1  jmcneill 		return (-1);
   1226  1.1  jmcneill 	return (bus_dmamem_mmap(sc->sc_dmat, p->segs, p->nsegs,
   1227  1.1  jmcneill 				off, prot, BUS_DMA_WAITOK));
   1228  1.1  jmcneill }
   1229  1.1  jmcneill 
   1230