Home | History | Annotate | Line # | Download | only in pci
esa.c revision 1.7
      1  1.7     pooka /* $NetBSD: esa.c,v 1.7 2002/01/14 19:24:39 pooka 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 #include <dev/pci/esareg.h>
     60  1.1  jmcneill #include <dev/pci/esadsp.h>
     61  1.1  jmcneill #include <dev/pci/esavar.h>
     62  1.1  jmcneill 
     63  1.1  jmcneill #define PCI_CBIO	0x10
     64  1.1  jmcneill 
     65  1.1  jmcneill #define ESA_DAC_DATA	0x1100
     66  1.1  jmcneill 
     67  1.1  jmcneill enum {
     68  1.1  jmcneill 	ESS_ALLEGRO1,
     69  1.1  jmcneill 	ESS_MAESTRO3
     70  1.1  jmcneill };
     71  1.1  jmcneill 
     72  1.1  jmcneill static struct esa_card_type {
     73  1.1  jmcneill 	u_int16_t pci_vendor_id;
     74  1.1  jmcneill 	u_int16_t pci_product_id;
     75  1.1  jmcneill 	int type;
     76  1.1  jmcneill 	int delay1, delay2;
     77  1.1  jmcneill } esa_card_types[] = {
     78  1.1  jmcneill 	{ PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_ALLEGRO1,
     79  1.1  jmcneill 	  ESS_ALLEGRO1, 50, 800 },
     80  1.1  jmcneill 	{ PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_MAESTRO3,
     81  1.1  jmcneill 	  ESS_MAESTRO3, 20, 500 },
     82  1.1  jmcneill 	{ PCI_VENDOR_ESSTECH, PCI_PRODUCT_ESSTECH_MAESTRO3_2,
     83  1.1  jmcneill 	  ESS_MAESTRO3, 20, 500 },
     84  1.1  jmcneill 	{ 0, 0, 0, 0, 0 }
     85  1.1  jmcneill };
     86  1.1  jmcneill 
     87  1.1  jmcneill struct audio_device esa_device = {
     88  1.1  jmcneill 	"ESS Allegro",
     89  1.1  jmcneill 	"",
     90  1.1  jmcneill 	"esa"
     91  1.1  jmcneill };
     92  1.1  jmcneill 
     93  1.1  jmcneill int		esa_match(struct device *, struct cfdata *, void *);
     94  1.1  jmcneill void		esa_attach(struct device *, struct device *, void *);
     95  1.1  jmcneill int		esa_detach(struct device *, int);
     96  1.1  jmcneill 
     97  1.1  jmcneill /* audio(9) functions */
     98  1.1  jmcneill int		esa_open(void *, int);
     99  1.1  jmcneill void		esa_close(void *);
    100  1.1  jmcneill int		esa_query_encoding(void *, struct audio_encoding *);
    101  1.1  jmcneill int		esa_set_params(void *, int, int, struct audio_params *,
    102  1.1  jmcneill 			       struct audio_params *);
    103  1.1  jmcneill int		esa_round_blocksize(void *, int);
    104  1.1  jmcneill int		esa_init_output(void *, void *, int);
    105  1.1  jmcneill int		esa_halt_output(void *);
    106  1.1  jmcneill int		esa_halt_input(void *);
    107  1.1  jmcneill int		esa_set_port(void *, mixer_ctrl_t *);
    108  1.1  jmcneill int		esa_get_port(void *, mixer_ctrl_t *);
    109  1.1  jmcneill int		esa_query_devinfo(void *, mixer_devinfo_t *);
    110  1.1  jmcneill void *		esa_malloc(void *, int, size_t, int, int);
    111  1.1  jmcneill void		esa_free(void *, void *, int);
    112  1.1  jmcneill int		esa_getdev(void *, struct audio_device *);
    113  1.1  jmcneill size_t		esa_round_buffersize(void *, int, size_t);
    114  1.1  jmcneill int		esa_get_props(void *);
    115  1.1  jmcneill int		esa_trigger_output(void *, void *, void *, int,
    116  1.1  jmcneill 				   void (*)(void *), void *,
    117  1.1  jmcneill 				   struct audio_params *);
    118  1.1  jmcneill int		esa_trigger_input(void *, void *, void *, int,
    119  1.1  jmcneill 				  void (*)(void *), void *,
    120  1.1  jmcneill 				  struct audio_params *);
    121  1.1  jmcneill 
    122  1.1  jmcneill int		esa_intr(void *);
    123  1.1  jmcneill int		esa_allocmem(struct esa_softc *, size_t, size_t,
    124  1.1  jmcneill 			     struct esa_dma *);
    125  1.1  jmcneill int		esa_freemem(struct esa_softc *, struct esa_dma *);
    126  1.1  jmcneill paddr_t		esa_mappage(void *addr, void *mem, off_t off, int prot);
    127  1.1  jmcneill 
    128  1.1  jmcneill /* Supporting subroutines */
    129  1.1  jmcneill u_int16_t	esa_read_assp(struct esa_softc *, u_int16_t, u_int16_t);
    130  1.1  jmcneill void		esa_write_assp(struct esa_softc *, u_int16_t, u_int16_t,
    131  1.1  jmcneill 			       u_int16_t);
    132  1.1  jmcneill int		esa_init_codec(struct esa_softc *);
    133  1.1  jmcneill int		esa_attach_codec(void *, struct ac97_codec_if *);
    134  1.1  jmcneill int		esa_read_codec(void *, u_int8_t, u_int16_t *);
    135  1.1  jmcneill int		esa_write_codec(void *, u_int8_t, u_int16_t);
    136  1.1  jmcneill void		esa_reset_codec(void *);
    137  1.1  jmcneill enum ac97_host_flags	esa_flags_codec(void *);
    138  1.1  jmcneill int		esa_wait(struct esa_softc *);
    139  1.1  jmcneill int		esa_init(struct esa_softc *);
    140  1.1  jmcneill void		esa_config(struct esa_softc *);
    141  1.1  jmcneill u_int8_t	esa_assp_halt(struct esa_softc *);
    142  1.1  jmcneill void		esa_codec_reset(struct esa_softc *);
    143  1.1  jmcneill int		esa_amp_enable(struct esa_softc *);
    144  1.1  jmcneill void		esa_enable_interrupts(struct esa_softc *);
    145  1.4     pooka u_int32_t	esa_get_pointer(struct esa_softc *, struct esa_channel *);
    146  1.4     pooka 
    147  1.4     pooka /* power management */
    148  1.1  jmcneill int		esa_power(struct esa_softc *, int);
    149  1.4     pooka void		esa_powerhook(int, void *);
    150  1.4     pooka int		esa_suspend(struct esa_softc *);
    151  1.4     pooka int		esa_resume(struct esa_softc *);
    152  1.1  jmcneill 
    153  1.1  jmcneill struct device *	audio_attach_mi_lkm(struct audio_hw_if *, void *,
    154  1.1  jmcneill 				    struct device *);
    155  1.1  jmcneill 
    156  1.1  jmcneill static audio_encoding_t esa_encoding[] = {
    157  1.1  jmcneill 	{ 0, AudioEulinear, AUDIO_ENCODING_ULINEAR, 8, 0 },
    158  1.1  jmcneill 	{ 1, AudioEmulaw, AUDIO_ENCODING_ULAW, 8,
    159  1.1  jmcneill 		AUDIO_ENCODINGFLAG_EMULATED },
    160  1.1  jmcneill 	{ 2, AudioEalaw, AUDIO_ENCODING_ALAW, 8, AUDIO_ENCODINGFLAG_EMULATED },
    161  1.1  jmcneill 	{ 3, AudioEslinear, AUDIO_ENCODING_SLINEAR, 8,
    162  1.1  jmcneill 		AUDIO_ENCODINGFLAG_EMULATED }, /* XXX: Are you sure? */
    163  1.1  jmcneill 	{ 4, AudioEslinear_le, AUDIO_ENCODING_SLINEAR_LE, 16, 0 },
    164  1.1  jmcneill 	{ 5, AudioEulinear_le, AUDIO_ENCODING_ULINEAR_LE, 16,
    165  1.1  jmcneill 		AUDIO_ENCODINGFLAG_EMULATED },
    166  1.1  jmcneill 	{ 6, AudioEslinear_be, AUDIO_ENCODING_SLINEAR_BE, 16,
    167  1.1  jmcneill 		AUDIO_ENCODINGFLAG_EMULATED },
    168  1.1  jmcneill 	{ 7, AudioEulinear_be, AUDIO_ENCODING_ULINEAR_BE, 16,
    169  1.1  jmcneill 		AUDIO_ENCODINGFLAG_EMULATED }
    170  1.1  jmcneill };
    171  1.1  jmcneill 
    172  1.1  jmcneill #define ESA_NENCODINGS 8
    173  1.1  jmcneill 
    174  1.1  jmcneill struct audio_hw_if esa_hw_if = {
    175  1.1  jmcneill 	esa_open,
    176  1.1  jmcneill 	esa_close,
    177  1.1  jmcneill 	NULL,			/* drain */
    178  1.1  jmcneill 	esa_query_encoding,
    179  1.1  jmcneill 	esa_set_params,
    180  1.1  jmcneill 	esa_round_blocksize,
    181  1.1  jmcneill 	NULL,			/* commit_settings */
    182  1.1  jmcneill 	esa_init_output,
    183  1.1  jmcneill 	NULL,			/* esa_init_input */
    184  1.1  jmcneill 	NULL,			/* start_output */
    185  1.1  jmcneill 	NULL,			/* start_input */
    186  1.1  jmcneill 	esa_halt_output,
    187  1.1  jmcneill 	esa_halt_input,
    188  1.1  jmcneill 	NULL,			/* speaker_ctl */
    189  1.1  jmcneill 	esa_getdev,
    190  1.1  jmcneill 	NULL,			/* getfd */
    191  1.1  jmcneill 	esa_set_port,
    192  1.1  jmcneill 	esa_get_port,
    193  1.1  jmcneill 	esa_query_devinfo,
    194  1.1  jmcneill 	esa_malloc,
    195  1.1  jmcneill 	esa_free,
    196  1.1  jmcneill 	esa_round_buffersize,
    197  1.1  jmcneill 	esa_mappage,
    198  1.1  jmcneill 	esa_get_props,
    199  1.1  jmcneill 	esa_trigger_output,
    200  1.1  jmcneill 	esa_trigger_input
    201  1.1  jmcneill };
    202  1.1  jmcneill 
    203  1.1  jmcneill struct cfattach esa_ca = {
    204  1.1  jmcneill 	sizeof(struct esa_softc), esa_match, esa_attach,
    205  1.1  jmcneill 	esa_detach, /*esa_activate*/ NULL
    206  1.1  jmcneill };
    207  1.1  jmcneill 
    208  1.1  jmcneill /*
    209  1.1  jmcneill  * audio(9) functions
    210  1.1  jmcneill  */
    211  1.1  jmcneill 
    212  1.1  jmcneill int
    213  1.1  jmcneill esa_open(void *hdl, int flags)
    214  1.1  jmcneill {
    215  1.1  jmcneill 
    216  1.1  jmcneill 	return (0);
    217  1.1  jmcneill }
    218  1.1  jmcneill 
    219  1.1  jmcneill void
    220  1.1  jmcneill esa_close(void *hdl)
    221  1.1  jmcneill {
    222  1.1  jmcneill 
    223  1.1  jmcneill 	return;
    224  1.1  jmcneill }
    225  1.1  jmcneill 
    226  1.1  jmcneill int
    227  1.1  jmcneill esa_query_encoding(void *hdl, struct audio_encoding *ae)
    228  1.1  jmcneill {
    229  1.1  jmcneill 
    230  1.1  jmcneill 	if (ae->index < 0 || ae->index >= ESA_NENCODINGS)
    231  1.1  jmcneill 		return (EINVAL);
    232  1.1  jmcneill 	*ae = esa_encoding[ae->index];
    233  1.1  jmcneill 
    234  1.1  jmcneill 	return (0);
    235  1.1  jmcneill }
    236  1.1  jmcneill 
    237  1.1  jmcneill int
    238  1.1  jmcneill esa_set_params(void *hdl, int setmode, int usemode, struct audio_params *play,
    239  1.1  jmcneill 	       struct audio_params *rec)
    240  1.1  jmcneill {
    241  1.1  jmcneill 	struct esa_softc *sc = hdl;
    242  1.3  jmcneill 	struct esa_channel *ch;
    243  1.3  jmcneill 	struct audio_params *p;
    244  1.1  jmcneill 	u_int32_t data;
    245  1.1  jmcneill 	u_int32_t freq;
    246  1.3  jmcneill 	int mode;
    247  1.1  jmcneill 
    248  1.3  jmcneill 	for (mode = AUMODE_RECORD; mode != -1;
    249  1.3  jmcneill 	     mode = (mode == AUMODE_RECORD) ? AUMODE_PLAY : -1) {
    250  1.3  jmcneill 		if ((setmode & mode) == 0)
    251  1.3  jmcneill 			continue;
    252  1.3  jmcneill 
    253  1.3  jmcneill 		switch (mode) {
    254  1.3  jmcneill 		case AUMODE_PLAY:
    255  1.3  jmcneill 			p = play;
    256  1.3  jmcneill 			ch = &sc->play;
    257  1.3  jmcneill 			break;
    258  1.3  jmcneill 		case AUMODE_RECORD:
    259  1.3  jmcneill 			p = rec;
    260  1.3  jmcneill 			ch = &sc->rec;
    261  1.3  jmcneill 			break;
    262  1.3  jmcneill 		}
    263  1.1  jmcneill 
    264  1.3  jmcneill 		if (p->sample_rate < ESA_MINRATE ||
    265  1.3  jmcneill 		    p->sample_rate > ESA_MAXRATE ||
    266  1.3  jmcneill 		    (p->precision != 8 && p->precision != 16) ||
    267  1.3  jmcneill 		    (p->channels < 1 && p->channels > 2))
    268  1.3  jmcneill 			return (EINVAL);
    269  1.3  jmcneill 
    270  1.3  jmcneill 		p->factor = 1;
    271  1.3  jmcneill 		p->sw_code = 0;
    272  1.3  jmcneill 
    273  1.3  jmcneill 		switch(p->encoding) {
    274  1.3  jmcneill 		case AUDIO_ENCODING_SLINEAR_BE:
    275  1.3  jmcneill 			if (p->precision == 16)
    276  1.3  jmcneill 				p->sw_code = swap_bytes;
    277  1.3  jmcneill 			else
    278  1.3  jmcneill 				p->sw_code = change_sign8;
    279  1.3  jmcneill 			break;
    280  1.3  jmcneill 		case AUDIO_ENCODING_SLINEAR_LE:
    281  1.3  jmcneill 			if (p->precision != 16)
    282  1.3  jmcneill 				p->sw_code = change_sign8;
    283  1.3  jmcneill 			break;
    284  1.3  jmcneill 		case AUDIO_ENCODING_ULINEAR_BE:
    285  1.3  jmcneill 			if (p->precision == 16) {
    286  1.3  jmcneill 				if (mode == AUMODE_PLAY)
    287  1.3  jmcneill 					p->sw_code =
    288  1.3  jmcneill 					    swap_bytes_change_sign16_le;
    289  1.3  jmcneill 				else
    290  1.3  jmcneill 					p->sw_code =
    291  1.3  jmcneill 					    change_sign16_swap_bytes_le;
    292  1.3  jmcneill 			}
    293  1.3  jmcneill 			break;
    294  1.3  jmcneill 		case AUDIO_ENCODING_ULINEAR_LE:
    295  1.3  jmcneill 			if (p->precision == 16)
    296  1.3  jmcneill 				p->sw_code = change_sign16_le;
    297  1.3  jmcneill 			break;
    298  1.3  jmcneill 		case AUDIO_ENCODING_ULAW:
    299  1.3  jmcneill 			if (mode == AUMODE_PLAY) {
    300  1.3  jmcneill 				p->factor = 2;
    301  1.3  jmcneill 				p->sw_code = mulaw_to_slinear16_le;
    302  1.3  jmcneill 			} else
    303  1.3  jmcneill 				p->sw_code = ulinear8_to_mulaw;
    304  1.3  jmcneill 			break;
    305  1.3  jmcneill 		case AUDIO_ENCODING_ALAW:
    306  1.3  jmcneill 			if (mode == AUMODE_PLAY) {
    307  1.3  jmcneill 				p->factor = 2;
    308  1.3  jmcneill 				p->sw_code = alaw_to_slinear16_le;
    309  1.3  jmcneill 			} else
    310  1.3  jmcneill 				p->sw_code = ulinear8_to_alaw;
    311  1.3  jmcneill 			break;
    312  1.3  jmcneill 		default:
    313  1.3  jmcneill 			return (EINVAL);
    314  1.3  jmcneill 		}
    315  1.3  jmcneill 
    316  1.3  jmcneill 		if (p->channels == 1)
    317  1.3  jmcneill 			data = 1;
    318  1.3  jmcneill 		else
    319  1.3  jmcneill 			data = 0;
    320  1.3  jmcneill 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
    321  1.3  jmcneill 			       ch->data_offset + ESA_SRC3_MODE_OFFSET,
    322  1.3  jmcneill 		    data);
    323  1.3  jmcneill 
    324  1.3  jmcneill 		if (play->precision * play->factor == 8)
    325  1.3  jmcneill 			data = 1;
    326  1.3  jmcneill 		else
    327  1.3  jmcneill 			data = 0;
    328  1.3  jmcneill 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
    329  1.3  jmcneill 			       ch->data_offset + ESA_SRC3_WORD_LENGTH_OFFSET,
    330  1.3  jmcneill 			       data);
    331  1.1  jmcneill 
    332  1.3  jmcneill 		if ((freq = ((p->sample_rate << 15) + 24000) / 48000) != 0) {
    333  1.3  jmcneill 			freq--;
    334  1.3  jmcneill 		}
    335  1.3  jmcneill 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
    336  1.3  jmcneill 			       ch->data_offset + ESA_CDATA_FREQUENCY, freq);
    337  1.1  jmcneill 	}
    338  1.1  jmcneill 
    339  1.1  jmcneill 	return (0);
    340  1.1  jmcneill }
    341  1.1  jmcneill 
    342  1.1  jmcneill int
    343  1.1  jmcneill esa_round_blocksize(void *hdl, int bs)
    344  1.1  jmcneill {
    345  1.1  jmcneill 	struct esa_softc *sc = hdl;
    346  1.1  jmcneill 
    347  1.3  jmcneill 	sc->play.blksize = sc->rec.blksize = 4096;
    348  1.1  jmcneill 
    349  1.1  jmcneill 	return (sc->play.blksize);
    350  1.1  jmcneill }
    351  1.1  jmcneill 
    352  1.1  jmcneill int
    353  1.1  jmcneill esa_init_output(void *hdl, void *buffer, int size)
    354  1.1  jmcneill {
    355  1.1  jmcneill 
    356  1.1  jmcneill 	return (0);
    357  1.1  jmcneill }
    358  1.1  jmcneill 
    359  1.1  jmcneill int
    360  1.1  jmcneill esa_halt_output(void *hdl)
    361  1.1  jmcneill {
    362  1.1  jmcneill 	struct esa_softc *sc = hdl;
    363  1.1  jmcneill 
    364  1.1  jmcneill 	if (sc->play.active == 0)
    365  1.1  jmcneill 		return (0);
    366  1.1  jmcneill 
    367  1.1  jmcneill 	sc->play.active = 0;
    368  1.1  jmcneill 
    369  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
    370  1.1  jmcneill 		       ESA_KDATA_INSTANCE0_MINISRC, 0);
    371  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_DMA_XFER0, 0);
    372  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_MIXER_XFER0, 0);
    373  1.1  jmcneill 
    374  1.1  jmcneill 	return (0);
    375  1.1  jmcneill }
    376  1.1  jmcneill 
    377  1.1  jmcneill int
    378  1.1  jmcneill esa_halt_input(void *hdl)
    379  1.1  jmcneill {
    380  1.3  jmcneill 	struct esa_softc *sc = hdl;
    381  1.3  jmcneill 	bus_space_tag_t iot = sc->sc_iot;
    382  1.3  jmcneill 	bus_space_handle_t ioh = sc->sc_ioh;
    383  1.3  jmcneill 	u_int32_t data;
    384  1.3  jmcneill 
    385  1.3  jmcneill 	if (sc->rec.active == 0)
    386  1.3  jmcneill 		return (0);
    387  1.3  jmcneill 
    388  1.3  jmcneill 	sc->rec.active = 0;
    389  1.3  jmcneill 
    390  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
    391  1.3  jmcneill 		       ESA_KDATA_TIMER_COUNT_RELOAD, 0);
    392  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_TIMER_COUNT_CURRENT, 0);
    393  1.3  jmcneill 	data = bus_space_read_2(iot, ioh, ESA_HOST_INT_CTRL);
    394  1.3  jmcneill 	bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL, data & ~ESA_CLKRUN_GEN_ENABLE);
    395  1.3  jmcneill 
    396  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, sc->rec.data_offset +
    397  1.3  jmcneill 		       ESA_CDATA_INSTANCE_READY, 0);
    398  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_ADC1_REQUEST, 0);
    399  1.1  jmcneill 
    400  1.3  jmcneill 	return (0);
    401  1.1  jmcneill }
    402  1.1  jmcneill 
    403  1.1  jmcneill void *
    404  1.1  jmcneill esa_malloc(void *hdl, int direction, size_t size, int type, int flags)
    405  1.1  jmcneill {
    406  1.1  jmcneill 	struct esa_softc *sc = hdl;
    407  1.1  jmcneill 	struct esa_dma *p;
    408  1.1  jmcneill 	int error;
    409  1.1  jmcneill 
    410  1.1  jmcneill 	p = malloc(sizeof(*p), type, flags);
    411  1.1  jmcneill 	if (!p)
    412  1.1  jmcneill 		return (0);
    413  1.1  jmcneill 	error = esa_allocmem(sc, size, 16, p);
    414  1.1  jmcneill 	if (error) {
    415  1.1  jmcneill 		free(p, type);
    416  1.1  jmcneill 		printf("%s: esa_malloc: not enough memory\n",
    417  1.1  jmcneill 		    sc->sc_dev.dv_xname);
    418  1.1  jmcneill 		return (0);
    419  1.1  jmcneill 	}
    420  1.1  jmcneill 	p->next = sc->sc_dmas;
    421  1.1  jmcneill 	sc->sc_dmas = p;
    422  1.1  jmcneill 
    423  1.1  jmcneill 	return (KERNADDR(p));
    424  1.1  jmcneill }
    425  1.1  jmcneill 
    426  1.1  jmcneill void
    427  1.1  jmcneill esa_free(void *hdl, void *addr, int type)
    428  1.1  jmcneill {
    429  1.1  jmcneill 	struct esa_softc *sc = hdl;
    430  1.1  jmcneill 	struct esa_dma *p;
    431  1.1  jmcneill 	struct esa_dma **pp;
    432  1.1  jmcneill 
    433  1.1  jmcneill 	for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next)
    434  1.1  jmcneill 		if (KERNADDR(p) == addr) {
    435  1.1  jmcneill 			esa_freemem(sc, p);
    436  1.1  jmcneill 			*pp = p->next;
    437  1.1  jmcneill 			free(p, type);
    438  1.1  jmcneill 			return;
    439  1.1  jmcneill 		}
    440  1.1  jmcneill }
    441  1.1  jmcneill 
    442  1.1  jmcneill int
    443  1.1  jmcneill esa_getdev(void *hdl, struct audio_device *ret)
    444  1.1  jmcneill {
    445  1.1  jmcneill 
    446  1.1  jmcneill 	*ret = esa_device;
    447  1.1  jmcneill 
    448  1.1  jmcneill 	return (0);
    449  1.1  jmcneill }
    450  1.1  jmcneill 
    451  1.1  jmcneill int
    452  1.1  jmcneill esa_set_port(void *hdl, mixer_ctrl_t *mc)
    453  1.1  jmcneill {
    454  1.1  jmcneill 	struct esa_softc *sc = hdl;
    455  1.1  jmcneill 
    456  1.1  jmcneill 	return (sc->codec_if->vtbl->mixer_set_port(sc->codec_if, mc));
    457  1.1  jmcneill }
    458  1.1  jmcneill 
    459  1.1  jmcneill int
    460  1.1  jmcneill esa_get_port(void *hdl, mixer_ctrl_t *mc)
    461  1.1  jmcneill {
    462  1.1  jmcneill 	struct esa_softc *sc = hdl;
    463  1.1  jmcneill 
    464  1.1  jmcneill 	return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, mc));
    465  1.1  jmcneill }
    466  1.1  jmcneill 
    467  1.1  jmcneill int
    468  1.1  jmcneill esa_query_devinfo(void *hdl, mixer_devinfo_t *di)
    469  1.1  jmcneill {
    470  1.1  jmcneill 	struct esa_softc *sc = hdl;
    471  1.1  jmcneill 
    472  1.1  jmcneill 	return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, di));
    473  1.1  jmcneill }
    474  1.1  jmcneill 
    475  1.1  jmcneill size_t
    476  1.1  jmcneill esa_round_buffersize(void *hdl, int direction, size_t bufsize)
    477  1.1  jmcneill {
    478  1.1  jmcneill 	struct esa_softc *sc = hdl;
    479  1.1  jmcneill 
    480  1.3  jmcneill 	sc->play.bufsize = sc->rec.bufsize = 65536;
    481  1.1  jmcneill 
    482  1.1  jmcneill 	return (sc->play.bufsize);
    483  1.1  jmcneill }
    484  1.1  jmcneill 
    485  1.1  jmcneill int
    486  1.1  jmcneill esa_get_props(void *hdl)
    487  1.1  jmcneill {
    488  1.1  jmcneill 
    489  1.3  jmcneill 	return (AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX);
    490  1.1  jmcneill }
    491  1.1  jmcneill 
    492  1.1  jmcneill int
    493  1.1  jmcneill esa_trigger_output(void *hdl, void *start, void *end, int blksize,
    494  1.1  jmcneill 			void (*intr)(void *), void *intrarg,
    495  1.1  jmcneill 			struct audio_params *param)
    496  1.1  jmcneill {
    497  1.1  jmcneill 	struct esa_softc *sc = hdl;
    498  1.1  jmcneill 	struct esa_dma *p;
    499  1.1  jmcneill 	bus_space_tag_t iot = sc->sc_iot;
    500  1.1  jmcneill 	bus_space_handle_t ioh = sc->sc_ioh;
    501  1.1  jmcneill 	u_int32_t data;
    502  1.1  jmcneill 	u_int32_t bufaddr;
    503  1.1  jmcneill 	u_int32_t i;
    504  1.1  jmcneill 	size_t size;
    505  1.3  jmcneill 	int data_bytes = (((ESA_MINISRC_TMP_BUFFER_SIZE & ~1) +
    506  1.3  jmcneill 			   (ESA_MINISRC_IN_BUFFER_SIZE & ~1) +
    507  1.3  jmcneill 			   (ESA_MINISRC_OUT_BUFFER_SIZE & ~1) + 4) + 255)
    508  1.3  jmcneill 			   &~ 255;
    509  1.3  jmcneill 	int dac_data = ESA_DAC_DATA + data_bytes;
    510  1.1  jmcneill 	int dsp_in_size = ESA_MINISRC_IN_BUFFER_SIZE - (0x20 * 2);
    511  1.1  jmcneill 	int dsp_out_size = ESA_MINISRC_OUT_BUFFER_SIZE - (0x20 * 2);
    512  1.3  jmcneill 	int dsp_in_buf = dac_data + (ESA_MINISRC_TMP_BUFFER_SIZE / 2);
    513  1.1  jmcneill 	int dsp_out_buf = dsp_in_buf + (dsp_in_size / 2) + 1;
    514  1.3  jmcneill 	sc->play.data_offset = dac_data;
    515  1.1  jmcneill 
    516  1.1  jmcneill 	if (sc->play.active)
    517  1.1  jmcneill 		return (EINVAL);
    518  1.1  jmcneill 
    519  1.1  jmcneill 	for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
    520  1.1  jmcneill 		;
    521  1.1  jmcneill 	if (!p) {
    522  1.1  jmcneill 		printf("%s: esa_trigger_output: bad addr %p\n",
    523  1.1  jmcneill 		    sc->sc_dev.dv_xname, start);
    524  1.1  jmcneill 		return (EINVAL);
    525  1.1  jmcneill 	}
    526  1.1  jmcneill 
    527  1.1  jmcneill 	sc->play.active = 1;
    528  1.3  jmcneill 	sc->play.intr = intr;
    529  1.3  jmcneill 	sc->play.arg = intrarg;
    530  1.1  jmcneill 	sc->play.pos = 0;
    531  1.1  jmcneill 	sc->play.count = 0;
    532  1.1  jmcneill 	sc->play.buf = start;
    533  1.1  jmcneill 	size = (size_t)(((caddr_t)end - (caddr_t)start));
    534  1.1  jmcneill 	bufaddr = DMAADDR(p);
    535  1.1  jmcneill 	sc->play.start = bufaddr;
    536  1.1  jmcneill 
    537  1.1  jmcneill #define LO(x) ((x) & 0x0000ffff)
    538  1.1  jmcneill #define HI(x) ((x) >> 16)
    539  1.1  jmcneill 
    540  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
    541  1.1  jmcneill 	    ESA_CDATA_HOST_SRC_ADDRL, LO(bufaddr));
    542  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
    543  1.1  jmcneill 	    ESA_CDATA_HOST_SRC_ADDRH, HI(bufaddr));
    544  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
    545  1.1  jmcneill 	    ESA_CDATA_HOST_SRC_END_PLUS_1L, LO(bufaddr + size));
    546  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
    547  1.1  jmcneill 	    ESA_CDATA_HOST_SRC_END_PLUS_1H, HI(bufaddr + size));
    548  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
    549  1.1  jmcneill 	    ESA_CDATA_HOST_SRC_CURRENTL, LO(bufaddr));
    550  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
    551  1.1  jmcneill 	    ESA_CDATA_HOST_SRC_CURRENTH, HI(bufaddr));
    552  1.1  jmcneill 
    553  1.1  jmcneill 	/* DSP buffers */
    554  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
    555  1.1  jmcneill 	    ESA_CDATA_IN_BUF_BEGIN, dsp_in_buf);
    556  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
    557  1.1  jmcneill 	    ESA_CDATA_IN_BUF_END_PLUS_1, dsp_in_buf + (dsp_in_size / 2));
    558  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
    559  1.1  jmcneill 	    ESA_CDATA_IN_BUF_HEAD, dsp_in_buf);
    560  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
    561  1.1  jmcneill 	    ESA_CDATA_IN_BUF_TAIL, dsp_in_buf);
    562  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
    563  1.1  jmcneill 	    ESA_CDATA_OUT_BUF_BEGIN, dsp_out_buf);
    564  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
    565  1.1  jmcneill 	    ESA_CDATA_OUT_BUF_END_PLUS_1, dsp_out_buf + (dsp_out_size / 2));
    566  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
    567  1.1  jmcneill 	    ESA_CDATA_OUT_BUF_HEAD, dsp_out_buf);
    568  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
    569  1.1  jmcneill 	    ESA_CDATA_OUT_BUF_TAIL, dsp_out_buf);
    570  1.1  jmcneill 
    571  1.1  jmcneill 	/* Some per-client initializers */
    572  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
    573  1.3  jmcneill 	    ESA_SRC3_DIRECTION_OFFSET + 12, dac_data + 40 + 8);
    574  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
    575  1.1  jmcneill 	    ESA_SRC3_DIRECTION_OFFSET + 19, 0x400 + ESA_MINISRC_COEF_LOC);
    576  1.1  jmcneill 	/* Enable or disable low-pass filter? (0xff if rate > 45000) */
    577  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
    578  1.1  jmcneill 	    ESA_SRC3_DIRECTION_OFFSET + 22, 0);
    579  1.1  jmcneill 	/* Tell it which way DMA is going */
    580  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
    581  1.1  jmcneill 	    ESA_CDATA_DMA_CONTROL,
    582  1.1  jmcneill 	    ESA_DMACONTROL_AUTOREPEAT + ESA_DMAC_PAGE3_SELECTOR +
    583  1.1  jmcneill 	    ESA_DMAC_BLOCKF_SELECTOR);
    584  1.1  jmcneill 
    585  1.1  jmcneill 	/* Set an armload of static initializers */
    586  1.1  jmcneill 	for (i = 0; i < (sizeof(esa_playvals) / sizeof(esa_playvals[0])); i++)
    587  1.3  jmcneill 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
    588  1.1  jmcneill 		    esa_playvals[i].addr, esa_playvals[i].val);
    589  1.1  jmcneill 
    590  1.1  jmcneill 	/* Put us in the packed task lists */
    591  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
    592  1.1  jmcneill 	    ESA_KDATA_INSTANCE0_MINISRC,
    593  1.3  jmcneill 	    dac_data >> ESA_DP_SHIFT_COUNT);
    594  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_DMA_XFER0,
    595  1.3  jmcneill 	    dac_data >> ESA_DP_SHIFT_COUNT);
    596  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_MIXER_XFER0,
    597  1.3  jmcneill 	    dac_data >> ESA_DP_SHIFT_COUNT);
    598  1.1  jmcneill #undef LO
    599  1.1  jmcneill #undef HI
    600  1.1  jmcneill 
    601  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
    602  1.1  jmcneill 	    ESA_KDATA_TIMER_COUNT_RELOAD, 240);
    603  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
    604  1.1  jmcneill 	    ESA_KDATA_TIMER_COUNT_CURRENT, 240);
    605  1.1  jmcneill 	data = bus_space_read_2(iot, ioh, ESA_HOST_INT_CTRL);
    606  1.1  jmcneill 	bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL,
    607  1.1  jmcneill 	    data | ESA_CLKRUN_GEN_ENABLE);
    608  1.1  jmcneill 
    609  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, dac_data +
    610  1.1  jmcneill 	    ESA_CDATA_INSTANCE_READY, 1);
    611  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
    612  1.1  jmcneill 	    ESA_KDATA_MIXER_TASK_NUMBER, 1);
    613  1.1  jmcneill 
    614  1.1  jmcneill 	return (0);
    615  1.1  jmcneill }
    616  1.1  jmcneill 
    617  1.1  jmcneill int
    618  1.1  jmcneill esa_trigger_input(void *hdl, void *start, void *end, int blksize,
    619  1.1  jmcneill 			void (*intr)(void *), void *intrarg,
    620  1.1  jmcneill 			struct audio_params *param)
    621  1.1  jmcneill {
    622  1.3  jmcneill 	struct esa_softc *sc = hdl;
    623  1.3  jmcneill 	struct esa_dma *p;
    624  1.3  jmcneill 	bus_space_tag_t iot = sc->sc_iot;
    625  1.3  jmcneill 	bus_space_handle_t ioh = sc->sc_ioh;
    626  1.3  jmcneill 	u_int32_t data;
    627  1.3  jmcneill 	u_int32_t bufaddr;
    628  1.3  jmcneill 	u_int32_t i;
    629  1.3  jmcneill 	size_t size;
    630  1.3  jmcneill 	int data_bytes = (((ESA_MINISRC_TMP_BUFFER_SIZE & ~1) +
    631  1.3  jmcneill 			   (ESA_MINISRC_IN_BUFFER_SIZE & ~1) +
    632  1.3  jmcneill 			   (ESA_MINISRC_OUT_BUFFER_SIZE & ~1) + 4) + 255)
    633  1.3  jmcneill 			   &~ 255;
    634  1.3  jmcneill 	int adc_data = ESA_DAC_DATA + data_bytes + (data_bytes / 2);
    635  1.3  jmcneill 	int dsp_in_size = ESA_MINISRC_IN_BUFFER_SIZE - (0x10 * 2);
    636  1.3  jmcneill 	int dsp_out_size = ESA_MINISRC_OUT_BUFFER_SIZE - (0x10 * 2);
    637  1.3  jmcneill 	int dsp_in_buf = adc_data + (ESA_MINISRC_TMP_BUFFER_SIZE / 2);
    638  1.3  jmcneill 	int dsp_out_buf = dsp_in_buf + (dsp_in_size / 2) + 1;
    639  1.3  jmcneill 	sc->rec.data_offset = adc_data;
    640  1.3  jmcneill 
    641  1.3  jmcneill 	if (sc->rec.active)
    642  1.3  jmcneill 		return (EINVAL);
    643  1.3  jmcneill 
    644  1.3  jmcneill 	for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
    645  1.3  jmcneill 		;
    646  1.3  jmcneill 	if (!p) {
    647  1.3  jmcneill 		printf("%s: esa_trigger_input: bad addr %p\n",
    648  1.3  jmcneill 		    sc->sc_dev.dv_xname, start);
    649  1.3  jmcneill 		return (EINVAL);
    650  1.3  jmcneill 	}
    651  1.3  jmcneill 
    652  1.3  jmcneill 	sc->rec.active = 1;
    653  1.3  jmcneill 	sc->rec.intr = intr;
    654  1.3  jmcneill 	sc->rec.arg = intrarg;
    655  1.3  jmcneill 	sc->rec.pos = 0;
    656  1.3  jmcneill 	sc->rec.count = 0;
    657  1.3  jmcneill 	sc->rec.buf = start;
    658  1.3  jmcneill 	size = (size_t)(((caddr_t)end - (caddr_t)start));
    659  1.3  jmcneill 	bufaddr = DMAADDR(p);
    660  1.3  jmcneill 	sc->rec.start = bufaddr;
    661  1.3  jmcneill 
    662  1.3  jmcneill #define LO(x) ((x) & 0x0000ffff)
    663  1.3  jmcneill #define HI(x) ((x) >> 16)
    664  1.3  jmcneill 
    665  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
    666  1.3  jmcneill 	    ESA_CDATA_HOST_SRC_ADDRL, LO(bufaddr));
    667  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
    668  1.3  jmcneill 	    ESA_CDATA_HOST_SRC_ADDRH, HI(bufaddr));
    669  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
    670  1.3  jmcneill 	    ESA_CDATA_HOST_SRC_END_PLUS_1L, LO(bufaddr + size));
    671  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
    672  1.3  jmcneill 	    ESA_CDATA_HOST_SRC_END_PLUS_1H, HI(bufaddr + size));
    673  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
    674  1.3  jmcneill 	    ESA_CDATA_HOST_SRC_CURRENTL, LO(bufaddr));
    675  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
    676  1.3  jmcneill 	    ESA_CDATA_HOST_SRC_CURRENTH, HI(bufaddr));
    677  1.3  jmcneill 
    678  1.3  jmcneill 	/* DSP buffers */
    679  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
    680  1.3  jmcneill 	    ESA_CDATA_IN_BUF_BEGIN, dsp_in_buf);
    681  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
    682  1.3  jmcneill 	    ESA_CDATA_IN_BUF_END_PLUS_1, dsp_in_buf + (dsp_in_size / 2));
    683  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
    684  1.3  jmcneill 	    ESA_CDATA_IN_BUF_HEAD, dsp_in_buf);
    685  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
    686  1.3  jmcneill 	    ESA_CDATA_IN_BUF_TAIL, dsp_in_buf);
    687  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
    688  1.3  jmcneill 	    ESA_CDATA_OUT_BUF_BEGIN, dsp_out_buf);
    689  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
    690  1.3  jmcneill 	    ESA_CDATA_OUT_BUF_END_PLUS_1, dsp_out_buf + (dsp_out_size / 2));
    691  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
    692  1.3  jmcneill 	    ESA_CDATA_OUT_BUF_HEAD, dsp_out_buf);
    693  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
    694  1.3  jmcneill 	    ESA_CDATA_OUT_BUF_TAIL, dsp_out_buf);
    695  1.3  jmcneill 
    696  1.3  jmcneill 	/* Some per-client initializers */
    697  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
    698  1.3  jmcneill 	    ESA_SRC3_DIRECTION_OFFSET + 12, adc_data + 40 + 8);
    699  1.3  jmcneill 	/* Tell it which way DMA is going */
    700  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
    701  1.3  jmcneill 	    ESA_CDATA_DMA_CONTROL,
    702  1.3  jmcneill 	    ESA_DMACONTROL_DIRECTION + ESA_DMACONTROL_AUTOREPEAT +
    703  1.3  jmcneill 	    ESA_DMAC_PAGE3_SELECTOR + ESA_DMAC_BLOCKF_SELECTOR);
    704  1.3  jmcneill 
    705  1.3  jmcneill 	/* Set an armload of static initializers */
    706  1.3  jmcneill 	for (i = 0; i < (sizeof(esa_recvals) / sizeof(esa_recvals[0])); i++)
    707  1.3  jmcneill 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
    708  1.3  jmcneill 		    esa_recvals[i].addr, esa_recvals[i].val);
    709  1.3  jmcneill 
    710  1.3  jmcneill 	/* Put us in the packed task lists */
    711  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
    712  1.3  jmcneill 	    ESA_KDATA_INSTANCE0_MINISRC,
    713  1.3  jmcneill 	    adc_data >> ESA_DP_SHIFT_COUNT);
    714  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_DMA_XFER0,
    715  1.3  jmcneill 	    adc_data >> ESA_DP_SHIFT_COUNT);
    716  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_ADC1_XFER0,
    717  1.3  jmcneill 	    adc_data >> ESA_DP_SHIFT_COUNT);
    718  1.3  jmcneill #undef LO
    719  1.3  jmcneill #undef HI
    720  1.1  jmcneill 
    721  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
    722  1.3  jmcneill 	    ESA_KDATA_TIMER_COUNT_RELOAD, 240);
    723  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
    724  1.3  jmcneill 	    ESA_KDATA_TIMER_COUNT_CURRENT, 240);
    725  1.3  jmcneill 	data = bus_space_read_2(iot, ioh, ESA_HOST_INT_CTRL);
    726  1.3  jmcneill 	bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL,
    727  1.3  jmcneill 	    data | ESA_CLKRUN_GEN_ENABLE);
    728  1.3  jmcneill 
    729  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_ADC1_REQUEST, 1);
    730  1.3  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, adc_data +
    731  1.3  jmcneill 	    ESA_CDATA_INSTANCE_READY, 1);
    732  1.3  jmcneill 
    733  1.3  jmcneill 	return (0);
    734  1.1  jmcneill }
    735  1.1  jmcneill 
    736  1.1  jmcneill /* Interrupt handler */
    737  1.1  jmcneill 
    738  1.1  jmcneill int
    739  1.1  jmcneill esa_intr(void *hdl)
    740  1.1  jmcneill {
    741  1.1  jmcneill 	struct esa_softc *sc = hdl;
    742  1.1  jmcneill 	bus_space_tag_t iot = sc->sc_iot;
    743  1.1  jmcneill 	bus_space_handle_t ioh = sc->sc_ioh;
    744  1.1  jmcneill 	u_int32_t status, ctl;
    745  1.1  jmcneill 	u_int32_t pos;
    746  1.1  jmcneill 	u_int32_t diff;
    747  1.3  jmcneill 	u_int32_t play_blksize = sc->play.blksize;
    748  1.3  jmcneill 	u_int32_t play_bufsize = sc->play.bufsize;
    749  1.3  jmcneill 	u_int32_t rec_blksize = sc->rec.blksize;
    750  1.3  jmcneill 	u_int32_t rec_bufsize = sc->rec.bufsize;
    751  1.1  jmcneill 
    752  1.1  jmcneill 	status = bus_space_read_1(iot, ioh, ESA_HOST_INT_STATUS);
    753  1.1  jmcneill 	if (!status)
    754  1.1  jmcneill 		return (0);
    755  1.1  jmcneill 
    756  1.1  jmcneill 	/* ack the interrupt */
    757  1.1  jmcneill 	bus_space_write_1(iot, ioh, ESA_HOST_INT_STATUS, 0xff);
    758  1.1  jmcneill 
    759  1.1  jmcneill 	if (status & ESA_HV_INT_PENDING) {
    760  1.1  jmcneill 		u_int8_t event;
    761  1.1  jmcneill 
    762  1.1  jmcneill 		printf("%s: hardware volume interrupt\n", sc->sc_dev.dv_xname);
    763  1.1  jmcneill 		event = bus_space_read_1(iot, ioh, ESA_HW_VOL_COUNTER_MASTER);
    764  1.1  jmcneill 		switch(event) {
    765  1.1  jmcneill 		case 0x99:
    766  1.1  jmcneill 		case 0xaa:
    767  1.1  jmcneill 		case 0x66:
    768  1.1  jmcneill 		case 0x88:
    769  1.1  jmcneill 			printf("%s: esa_intr: FIXME\n", sc->sc_dev.dv_xname);
    770  1.1  jmcneill 			break;
    771  1.1  jmcneill 		default:
    772  1.1  jmcneill 			printf("%s: unknown hwvol event 0x%02x\n",
    773  1.1  jmcneill 			    sc->sc_dev.dv_xname, event);
    774  1.1  jmcneill 			break;
    775  1.1  jmcneill 		}
    776  1.1  jmcneill 		bus_space_write_1(iot, ioh, ESA_HW_VOL_COUNTER_MASTER, 0x88);
    777  1.1  jmcneill 	}
    778  1.1  jmcneill 
    779  1.1  jmcneill 	if (status & ESA_ASSP_INT_PENDING) {
    780  1.1  jmcneill 		ctl = bus_space_read_1(iot, ioh, ESA_ASSP_CONTROL_B);
    781  1.1  jmcneill 		if (!(ctl & ESA_STOP_ASSP_CLOCK)) {
    782  1.1  jmcneill 			ctl = bus_space_read_1(iot, ioh,
    783  1.1  jmcneill 					       ESA_ASSP_HOST_INT_STATUS);
    784  1.1  jmcneill 			if (ctl & ESA_DSP2HOST_REQ_TIMER) {
    785  1.1  jmcneill 				bus_space_write_1(iot, ioh,
    786  1.1  jmcneill 				    ESA_ASSP_HOST_INT_STATUS,
    787  1.1  jmcneill 				    ESA_DSP2HOST_REQ_TIMER);
    788  1.1  jmcneill 				if (sc->play.active) {
    789  1.3  jmcneill 					pos = esa_get_pointer(sc, &sc->play)
    790  1.3  jmcneill 					    % play_bufsize;
    791  1.3  jmcneill 					diff = (play_bufsize + pos - sc->play.pos)
    792  1.3  jmcneill 					    % play_bufsize;
    793  1.1  jmcneill 					sc->play.pos = pos;
    794  1.1  jmcneill 					sc->play.count += diff;
    795  1.3  jmcneill 					while(sc->play.count >= play_blksize) {
    796  1.3  jmcneill 						sc->play.count -= play_blksize;
    797  1.3  jmcneill 						(*sc->play.intr)(sc->play.arg);
    798  1.3  jmcneill 					}
    799  1.3  jmcneill 				}
    800  1.3  jmcneill 				if (sc->rec.active) {
    801  1.3  jmcneill 					pos = esa_get_pointer(sc, &sc->rec)
    802  1.3  jmcneill 					    % rec_bufsize;
    803  1.3  jmcneill 					diff = (rec_bufsize + pos - sc->rec.pos)
    804  1.3  jmcneill 					    % rec_bufsize;
    805  1.3  jmcneill 					sc->rec.pos = pos;
    806  1.3  jmcneill 					sc->rec.count += diff;
    807  1.3  jmcneill 					while(sc->rec.count >= rec_blksize) {
    808  1.3  jmcneill 						sc->rec.count -= rec_blksize;
    809  1.3  jmcneill 						(*sc->rec.intr)(sc->rec.arg);
    810  1.1  jmcneill 					}
    811  1.1  jmcneill 				}
    812  1.1  jmcneill 			}
    813  1.1  jmcneill 		}
    814  1.1  jmcneill 	}
    815  1.1  jmcneill 
    816  1.1  jmcneill 	return (1);
    817  1.1  jmcneill }
    818  1.1  jmcneill 
    819  1.1  jmcneill int
    820  1.1  jmcneill esa_allocmem(struct esa_softc *sc, size_t size, size_t align,
    821  1.1  jmcneill 		struct esa_dma *p)
    822  1.1  jmcneill {
    823  1.1  jmcneill 	int error;
    824  1.1  jmcneill 
    825  1.1  jmcneill 	p->size = size;
    826  1.1  jmcneill 	error = bus_dmamem_alloc(sc->sc_dmat, p->size, align, 0,
    827  1.1  jmcneill 				 p->segs, sizeof(p->segs) / sizeof(p->segs[0]),
    828  1.1  jmcneill 				 &p->nsegs, BUS_DMA_NOWAIT);
    829  1.1  jmcneill 	if (error)
    830  1.1  jmcneill 		return (error);
    831  1.1  jmcneill 
    832  1.1  jmcneill 	error = bus_dmamem_map(sc->sc_dmat, p->segs, p->nsegs, p->size,
    833  1.1  jmcneill 				&p->addr, BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
    834  1.1  jmcneill 	if (error)
    835  1.1  jmcneill 		goto free;
    836  1.1  jmcneill 
    837  1.1  jmcneill 	error = bus_dmamap_create(sc->sc_dmat, p->size, 1, p->size, 0,
    838  1.1  jmcneill 				  BUS_DMA_NOWAIT, &p->map);
    839  1.1  jmcneill 	if (error)
    840  1.1  jmcneill 		goto unmap;
    841  1.1  jmcneill 
    842  1.1  jmcneill 	error = bus_dmamap_load(sc->sc_dmat, p->map, p->addr, p->size, NULL,
    843  1.1  jmcneill 				BUS_DMA_NOWAIT);
    844  1.1  jmcneill 	if (error)
    845  1.1  jmcneill 		goto destroy;
    846  1.1  jmcneill 
    847  1.1  jmcneill 	return (0);
    848  1.1  jmcneill 
    849  1.1  jmcneill destroy:
    850  1.1  jmcneill 	bus_dmamap_destroy(sc->sc_dmat, p->map);
    851  1.1  jmcneill unmap:
    852  1.1  jmcneill 	bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
    853  1.1  jmcneill free:
    854  1.1  jmcneill 	bus_dmamem_free(sc->sc_dmat, p->segs, p->nsegs);
    855  1.1  jmcneill 
    856  1.1  jmcneill 	return (error);
    857  1.1  jmcneill }
    858  1.1  jmcneill 
    859  1.1  jmcneill int
    860  1.1  jmcneill esa_freemem(struct esa_softc *sc, struct esa_dma *p)
    861  1.1  jmcneill {
    862  1.1  jmcneill 
    863  1.1  jmcneill 	bus_dmamap_unload(sc->sc_dmat, p->map);
    864  1.1  jmcneill 	bus_dmamap_destroy(sc->sc_dmat, p->map);
    865  1.1  jmcneill 	bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
    866  1.1  jmcneill 	bus_dmamem_free(sc->sc_dmat, p->segs, p->nsegs);
    867  1.1  jmcneill 
    868  1.1  jmcneill 	return (0);
    869  1.1  jmcneill }
    870  1.1  jmcneill 
    871  1.1  jmcneill /*
    872  1.1  jmcneill  * Supporting Subroutines
    873  1.1  jmcneill  */
    874  1.1  jmcneill 
    875  1.1  jmcneill int
    876  1.1  jmcneill esa_match(struct device *dev, struct cfdata *match, void *aux)
    877  1.1  jmcneill {
    878  1.1  jmcneill 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    879  1.1  jmcneill 
    880  1.1  jmcneill 	switch(PCI_VENDOR(pa->pa_id)) {
    881  1.1  jmcneill 	case PCI_VENDOR_ESSTECH:
    882  1.1  jmcneill 		switch(PCI_PRODUCT(pa->pa_id)) {
    883  1.1  jmcneill 		case PCI_PRODUCT_ESSTECH_ALLEGRO1:
    884  1.1  jmcneill 		case PCI_PRODUCT_ESSTECH_MAESTRO3:
    885  1.1  jmcneill 		case PCI_PRODUCT_ESSTECH_MAESTRO3_2:
    886  1.1  jmcneill 			return (1);
    887  1.1  jmcneill 		}
    888  1.1  jmcneill 	}
    889  1.1  jmcneill 
    890  1.1  jmcneill 	return (0);
    891  1.1  jmcneill }
    892  1.1  jmcneill 
    893  1.1  jmcneill void
    894  1.1  jmcneill esa_attach(struct device *parent, struct device *self, void *aux)
    895  1.1  jmcneill {
    896  1.1  jmcneill 	struct esa_softc *sc = (struct esa_softc *)self;
    897  1.1  jmcneill 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    898  1.1  jmcneill 	pcitag_t tag = pa->pa_tag;
    899  1.1  jmcneill 	pci_chipset_tag_t pc = pa->pa_pc;
    900  1.1  jmcneill 	pci_intr_handle_t ih;
    901  1.1  jmcneill 	struct esa_card_type *card;
    902  1.1  jmcneill 	const char *intrstr;
    903  1.1  jmcneill 	u_int32_t data;
    904  1.1  jmcneill 	char devinfo[256];
    905  1.4     pooka 	int revision, len;
    906  1.1  jmcneill 
    907  1.1  jmcneill 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo);
    908  1.1  jmcneill 	revision = PCI_REVISION(pa->pa_class);
    909  1.1  jmcneill 	printf(": %s (rev. 0x%02x)\n", devinfo, revision);
    910  1.1  jmcneill 
    911  1.1  jmcneill 	for (card = esa_card_types; card->pci_vendor_id; card++)
    912  1.1  jmcneill 		if (PCI_VENDOR(pa->pa_id) == card->pci_vendor_id &&
    913  1.1  jmcneill 		    PCI_PRODUCT(pa->pa_id) == card->pci_product_id) {
    914  1.1  jmcneill 			sc->type = card->type;
    915  1.1  jmcneill 			sc->delay1 = card->delay1;
    916  1.1  jmcneill 			sc->delay2 = card->delay2;
    917  1.1  jmcneill 			break;
    918  1.1  jmcneill 		}
    919  1.1  jmcneill 
    920  1.1  jmcneill 	data = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    921  1.1  jmcneill 	data |= (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE
    922  1.1  jmcneill 	    | PCI_COMMAND_MASTER_ENABLE);
    923  1.1  jmcneill 	pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, data);
    924  1.1  jmcneill 
    925  1.1  jmcneill 	/* Map I/O register */
    926  1.1  jmcneill 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
    927  1.1  jmcneill 	    &sc->sc_iot, &sc->sc_ioh, &sc->sc_iob, &sc->sc_ios)) {
    928  1.1  jmcneill 		printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
    929  1.1  jmcneill 		return;
    930  1.1  jmcneill 	}
    931  1.1  jmcneill 
    932  1.1  jmcneill 	/* Initialize softc */
    933  1.1  jmcneill 	sc->sc_tag = tag;
    934  1.1  jmcneill 	sc->sc_pct = pc;
    935  1.1  jmcneill 	sc->sc_dmat = pa->pa_dmat;
    936  1.1  jmcneill 
    937  1.1  jmcneill 	/* Map and establish an interrupt */
    938  1.1  jmcneill 	if (pci_intr_map(pa, &ih)) {
    939  1.1  jmcneill 		printf("%s: can't map interrupt\n", sc->sc_dev.dv_xname);
    940  1.1  jmcneill 		return;
    941  1.1  jmcneill 	}
    942  1.1  jmcneill 	intrstr = pci_intr_string(pc, ih);
    943  1.1  jmcneill 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, esa_intr, self);
    944  1.1  jmcneill 	if (sc->sc_ih == NULL) {
    945  1.1  jmcneill 		printf("%s: can't establish interrupt", sc->sc_dev.dv_xname);
    946  1.1  jmcneill 		if (intrstr != NULL)
    947  1.1  jmcneill 			printf(" at %s", intrstr);
    948  1.1  jmcneill 		printf("\n");
    949  1.1  jmcneill 		return;
    950  1.1  jmcneill 	}
    951  1.1  jmcneill 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    952  1.1  jmcneill 
    953  1.1  jmcneill 	/* Power up chip */
    954  1.7     pooka 	esa_power(sc, PCI_PMCSR_STATE_D0);
    955  1.1  jmcneill 
    956  1.1  jmcneill 	/* Init chip */
    957  1.1  jmcneill 	if (esa_init(sc) == -1) {
    958  1.1  jmcneill 		printf("%s: esa_attach: unable to initialize the card\n",
    959  1.1  jmcneill 		    sc->sc_dev.dv_xname);
    960  1.1  jmcneill 		return;
    961  1.1  jmcneill 	}
    962  1.1  jmcneill 
    963  1.4     pooka 	/* create suspend save area */
    964  1.4     pooka 	len = sizeof(u_int16_t) * (ESA_REV_B_CODE_MEMORY_LENGTH
    965  1.4     pooka 	    + ESA_REV_B_DATA_MEMORY_LENGTH + 1);
    966  1.5  jmcneill 	sc->savemem = (u_int16_t *)malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
    967  1.4     pooka 	if (sc->savemem == NULL) {
    968  1.4     pooka 		printf("%s: unable to allocate suspend buffer\n",
    969  1.4     pooka 		    sc->sc_dev.dv_xname);
    970  1.4     pooka 		return;
    971  1.4     pooka 	}
    972  1.6  jmcneill 
    973  1.6  jmcneill 	/*
    974  1.6  jmcneill 	 * Every card I've seen has had their channels swapped with respect
    975  1.6  jmcneill 	 * to the mixer. Ie:
    976  1.6  jmcneill 	 *  $ mixerctl -w outputs.master=0,191
    977  1.6  jmcneill 	 * Would result in the _right_ speaker being turned off.
    978  1.6  jmcneill 	 *
    979  1.6  jmcneill 	 * So, we will swap the left and right mixer channels to compensate
    980  1.6  jmcneill 	 * for this.
    981  1.6  jmcneill 	 */
    982  1.6  jmcneill 	sc->codec_flags |= AC97_HOST_SWAPPED_CHANNELS;
    983  1.6  jmcneill 	sc->codec_flags |= AC97_HOST_DONT_READ;
    984  1.4     pooka 
    985  1.1  jmcneill 	/* Attach AC97 host interface */
    986  1.1  jmcneill 	sc->host_if.arg = self;
    987  1.1  jmcneill 	sc->host_if.attach = esa_attach_codec;
    988  1.1  jmcneill 	sc->host_if.read = esa_read_codec;
    989  1.1  jmcneill 	sc->host_if.write = esa_write_codec;
    990  1.1  jmcneill 	sc->host_if.reset = esa_reset_codec;
    991  1.1  jmcneill 	sc->host_if.flags = esa_flags_codec;
    992  1.1  jmcneill 
    993  1.1  jmcneill 	if (ac97_attach(&sc->host_if) != 0)
    994  1.1  jmcneill 		return;
    995  1.1  jmcneill 
    996  1.1  jmcneill 	sc->sc_audiodev = audio_attach_mi(&esa_hw_if, self, &sc->sc_dev);
    997  1.1  jmcneill 
    998  1.4     pooka 	sc->powerhook = powerhook_establish(esa_powerhook, sc);
    999  1.4     pooka 	if (sc->powerhook == NULL)
   1000  1.4     pooka 		printf("%s: WARNING: unable to establish powerhook\n",
   1001  1.4     pooka 		    sc->sc_dev.dv_xname);
   1002  1.4     pooka 
   1003  1.1  jmcneill 	return;
   1004  1.1  jmcneill }
   1005  1.1  jmcneill 
   1006  1.1  jmcneill int
   1007  1.1  jmcneill esa_detach(struct device *self, int flags)
   1008  1.1  jmcneill {
   1009  1.1  jmcneill 	struct esa_softc *sc = (struct esa_softc *)self;
   1010  1.1  jmcneill 	int rv = 0;
   1011  1.1  jmcneill 
   1012  1.1  jmcneill 	if (sc->sc_audiodev != NULL)
   1013  1.1  jmcneill 		rv = config_detach(sc->sc_audiodev, flags);
   1014  1.1  jmcneill 	if (rv)
   1015  1.1  jmcneill 		return (rv);
   1016  1.1  jmcneill 
   1017  1.1  jmcneill 	if (sc->sc_ih != NULL)
   1018  1.1  jmcneill 		pci_intr_disestablish(sc->sc_pct, sc->sc_ih);
   1019  1.1  jmcneill 	if (sc->sc_ios)
   1020  1.1  jmcneill 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
   1021  1.1  jmcneill 
   1022  1.1  jmcneill 	return (0);
   1023  1.1  jmcneill }
   1024  1.1  jmcneill 
   1025  1.1  jmcneill u_int16_t
   1026  1.1  jmcneill esa_read_assp(struct esa_softc *sc, u_int16_t region, u_int16_t index)
   1027  1.1  jmcneill {
   1028  1.1  jmcneill 	u_int16_t data;
   1029  1.1  jmcneill 	bus_space_tag_t iot = sc->sc_iot;
   1030  1.1  jmcneill 	bus_space_handle_t ioh = sc->sc_ioh;
   1031  1.1  jmcneill 
   1032  1.1  jmcneill 	bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_TYPE,
   1033  1.1  jmcneill 	    region & ESA_MEMTYPE_MASK);
   1034  1.1  jmcneill 	bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_INDEX, index);
   1035  1.1  jmcneill 	data = bus_space_read_2(iot, ioh, ESA_DSP_PORT_MEMORY_DATA);
   1036  1.1  jmcneill 
   1037  1.1  jmcneill 	return (data);
   1038  1.1  jmcneill }
   1039  1.1  jmcneill 
   1040  1.1  jmcneill void
   1041  1.1  jmcneill esa_write_assp(struct esa_softc *sc, u_int16_t region, u_int16_t index,
   1042  1.1  jmcneill 		u_int16_t data)
   1043  1.1  jmcneill {
   1044  1.1  jmcneill 	bus_space_tag_t iot = sc->sc_iot;
   1045  1.1  jmcneill 	bus_space_handle_t ioh = sc->sc_ioh;
   1046  1.1  jmcneill 
   1047  1.1  jmcneill 	bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_TYPE,
   1048  1.1  jmcneill 	    region & ESA_MEMTYPE_MASK);
   1049  1.1  jmcneill 	bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_INDEX, index);
   1050  1.1  jmcneill 	bus_space_write_2(iot, ioh, ESA_DSP_PORT_MEMORY_DATA, data);
   1051  1.1  jmcneill 
   1052  1.1  jmcneill 	return;
   1053  1.1  jmcneill }
   1054  1.1  jmcneill 
   1055  1.1  jmcneill int
   1056  1.1  jmcneill esa_init_codec(struct esa_softc *sc)
   1057  1.1  jmcneill {
   1058  1.1  jmcneill 	bus_space_tag_t iot = sc->sc_iot;
   1059  1.1  jmcneill 	bus_space_handle_t ioh = sc->sc_ioh;
   1060  1.1  jmcneill 	u_int32_t data;
   1061  1.1  jmcneill 
   1062  1.1  jmcneill 	data = bus_space_read_1(iot, ioh, ESA_CODEC_COMMAND);
   1063  1.1  jmcneill 
   1064  1.1  jmcneill 	return ((data & 0x1) ? 0 : 1);
   1065  1.1  jmcneill }
   1066  1.1  jmcneill 
   1067  1.1  jmcneill int
   1068  1.1  jmcneill esa_attach_codec(void *aux, struct ac97_codec_if *codec_if)
   1069  1.1  jmcneill {
   1070  1.1  jmcneill 	struct esa_softc *sc = aux;
   1071  1.1  jmcneill 
   1072  1.1  jmcneill 	sc->codec_if = codec_if;
   1073  1.1  jmcneill 
   1074  1.1  jmcneill 	return (0);
   1075  1.1  jmcneill }
   1076  1.1  jmcneill 
   1077  1.1  jmcneill int
   1078  1.1  jmcneill esa_read_codec(void *aux, u_int8_t reg, u_int16_t *result)
   1079  1.1  jmcneill {
   1080  1.1  jmcneill 	struct esa_softc *sc = aux;
   1081  1.1  jmcneill 	bus_space_tag_t iot = sc->sc_iot;
   1082  1.1  jmcneill 	bus_space_handle_t ioh = sc->sc_ioh;
   1083  1.1  jmcneill 
   1084  1.1  jmcneill 	if (esa_wait(sc))
   1085  1.1  jmcneill 		printf("%s: esa_read_codec: timed out\n", sc->sc_dev.dv_xname);
   1086  1.1  jmcneill 	bus_space_write_1(iot, ioh, ESA_CODEC_COMMAND, (reg & 0x7f) | 0x80);
   1087  1.1  jmcneill 	delay(50);
   1088  1.1  jmcneill 	if (esa_wait(sc))
   1089  1.1  jmcneill 		printf("%s: esa_read_codec: timed out\n", sc->sc_dev.dv_xname);
   1090  1.1  jmcneill 	*result = bus_space_read_2(iot, ioh, ESA_CODEC_DATA);
   1091  1.1  jmcneill 
   1092  1.1  jmcneill 	return (0);
   1093  1.1  jmcneill }
   1094  1.1  jmcneill 
   1095  1.1  jmcneill int
   1096  1.1  jmcneill esa_write_codec(void *aux, u_int8_t reg, u_int16_t data)
   1097  1.1  jmcneill {
   1098  1.1  jmcneill 	struct esa_softc *sc = aux;
   1099  1.1  jmcneill 	bus_space_tag_t iot = sc->sc_iot;
   1100  1.1  jmcneill 	bus_space_handle_t ioh = sc->sc_ioh;
   1101  1.1  jmcneill 
   1102  1.1  jmcneill 	if (esa_wait(sc)) {
   1103  1.1  jmcneill 		printf("%s: esa_write_codec: timed out\n", sc->sc_dev.dv_xname);
   1104  1.1  jmcneill 		return (-1);
   1105  1.1  jmcneill 	}
   1106  1.1  jmcneill 	bus_space_write_2(iot, ioh, ESA_CODEC_DATA, data);
   1107  1.1  jmcneill 	bus_space_write_1(iot, ioh, ESA_CODEC_COMMAND, reg & 0x7f);
   1108  1.1  jmcneill 	delay(50);
   1109  1.1  jmcneill 
   1110  1.1  jmcneill 	return (0);
   1111  1.1  jmcneill }
   1112  1.1  jmcneill 
   1113  1.1  jmcneill void
   1114  1.1  jmcneill esa_reset_codec(void *aux)
   1115  1.1  jmcneill {
   1116  1.1  jmcneill 
   1117  1.1  jmcneill 	return;
   1118  1.1  jmcneill }
   1119  1.1  jmcneill 
   1120  1.1  jmcneill enum ac97_host_flags
   1121  1.1  jmcneill esa_flags_codec(void *aux)
   1122  1.1  jmcneill {
   1123  1.1  jmcneill 	struct esa_softc *sc = aux;
   1124  1.1  jmcneill 
   1125  1.1  jmcneill 	return (sc->codec_flags);
   1126  1.1  jmcneill }
   1127  1.1  jmcneill 
   1128  1.1  jmcneill int
   1129  1.1  jmcneill esa_wait(struct esa_softc *sc)
   1130  1.1  jmcneill {
   1131  1.1  jmcneill 	int i, val;
   1132  1.1  jmcneill 	bus_space_tag_t iot = sc->sc_iot;
   1133  1.1  jmcneill 	bus_space_handle_t ioh = sc->sc_ioh;
   1134  1.1  jmcneill 
   1135  1.1  jmcneill 	for (i = 0; i < 20; i++) {
   1136  1.1  jmcneill 		val = bus_space_read_1(iot, ioh, ESA_CODEC_STATUS);
   1137  1.1  jmcneill 		if ((val & 1) == 0)
   1138  1.1  jmcneill 			return (0);
   1139  1.1  jmcneill 		delay(2);
   1140  1.1  jmcneill 	}
   1141  1.1  jmcneill 
   1142  1.1  jmcneill 	return (-1);
   1143  1.1  jmcneill }
   1144  1.1  jmcneill 
   1145  1.1  jmcneill int
   1146  1.1  jmcneill esa_init(struct esa_softc *sc)
   1147  1.1  jmcneill {
   1148  1.1  jmcneill 	bus_space_tag_t iot = sc->sc_iot;
   1149  1.1  jmcneill 	bus_space_handle_t ioh = sc->sc_ioh;
   1150  1.1  jmcneill 	pcitag_t tag = sc->sc_tag;
   1151  1.1  jmcneill 	pci_chipset_tag_t pc = sc->sc_pct;
   1152  1.1  jmcneill 	u_int32_t data, i, size;
   1153  1.1  jmcneill 	u_int8_t reset_state;
   1154  1.3  jmcneill 	int data_bytes = (((ESA_MINISRC_TMP_BUFFER_SIZE & ~1) +
   1155  1.3  jmcneill 			   (ESA_MINISRC_IN_BUFFER_SIZE & ~1) +
   1156  1.3  jmcneill 			   (ESA_MINISRC_OUT_BUFFER_SIZE & ~1) + 4) + 255)
   1157  1.3  jmcneill 			   &~ 255;
   1158  1.1  jmcneill 
   1159  1.1  jmcneill 	/* Disable legacy emulation */
   1160  1.1  jmcneill 	data = pci_conf_read(pc, tag, PCI_LEGACY_AUDIO_CTRL);
   1161  1.1  jmcneill 	data |= DISABLE_LEGACY;
   1162  1.1  jmcneill 	pci_conf_write(pc, tag, PCI_LEGACY_AUDIO_CTRL, data);
   1163  1.1  jmcneill 
   1164  1.1  jmcneill 	esa_config(sc);
   1165  1.1  jmcneill 
   1166  1.1  jmcneill 	reset_state = esa_assp_halt(sc);
   1167  1.1  jmcneill 
   1168  1.1  jmcneill 	esa_init_codec(sc);
   1169  1.1  jmcneill 	esa_codec_reset(sc);
   1170  1.1  jmcneill 
   1171  1.1  jmcneill 	/* Zero kernel and mixer data */
   1172  1.1  jmcneill 	size = ESA_REV_B_DATA_MEMORY_UNIT_LENGTH * ESA_NUM_UNITS_KERNEL_DATA;
   1173  1.1  jmcneill 	for (i = 0; i < size / 2; i++) {
   1174  1.1  jmcneill 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
   1175  1.1  jmcneill 		    ESA_KDATA_BASE_ADDR + i, 0);
   1176  1.1  jmcneill 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
   1177  1.1  jmcneill 		    ESA_KDATA_BASE_ADDR2 + i, 0);
   1178  1.1  jmcneill 	}
   1179  1.1  jmcneill 
   1180  1.1  jmcneill 	/* Init DMA pointer */
   1181  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_CURRENT_DMA,
   1182  1.1  jmcneill 	    ESA_KDATA_DMA_XFER0);
   1183  1.1  jmcneill 
   1184  1.1  jmcneill 	/* Write kernel code into memory */
   1185  1.1  jmcneill 	size = sizeof(esa_assp_kernel_image);
   1186  1.1  jmcneill 	for (i = 0; i < size / 2; i++)
   1187  1.1  jmcneill 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE,
   1188  1.1  jmcneill 		    ESA_REV_B_CODE_MEMORY_BEGIN + i, esa_assp_kernel_image[i]);
   1189  1.1  jmcneill 
   1190  1.1  jmcneill 	size = sizeof(esa_assp_minisrc_image);
   1191  1.1  jmcneill 	for (i = 0; i < size / 2; i++)
   1192  1.1  jmcneill 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE, 0x400 + i,
   1193  1.1  jmcneill 		    esa_assp_minisrc_image[i]);
   1194  1.1  jmcneill 
   1195  1.1  jmcneill 	/* Write the coefficients for the low pass filter */
   1196  1.1  jmcneill 	size = sizeof(esa_minisrc_lpf_image);
   1197  1.1  jmcneill 	for (i = 0; i < size / 2; i++)
   1198  1.1  jmcneill 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE,
   1199  1.1  jmcneill 		    0x400 + ESA_MINISRC_COEF_LOC + i, esa_minisrc_lpf_image[i]);
   1200  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE,
   1201  1.1  jmcneill 	    0x400 + ESA_MINISRC_COEF_LOC + size, 0x8000);
   1202  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_TASK0, 0x400);
   1203  1.1  jmcneill 	/* Init the mixer number */
   1204  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
   1205  1.1  jmcneill              ESA_KDATA_MIXER_TASK_NUMBER, 0);
   1206  1.1  jmcneill 	/* Extreme kernel master volume */
   1207  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_DAC_LEFT_VOLUME,
   1208  1.1  jmcneill 	    ESA_ARB_VOLUME);
   1209  1.1  jmcneill 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA,
   1210  1.1  jmcneill             ESA_KDATA_DAC_RIGHT_VOLUME, ESA_ARB_VOLUME);
   1211  1.1  jmcneill 
   1212  1.1  jmcneill 	if (esa_amp_enable(sc))
   1213  1.1  jmcneill 		return (-1);
   1214  1.1  jmcneill 
   1215  1.1  jmcneill 	/* Zero entire DAC/ADC area */
   1216  1.1  jmcneill 	for (i = 0x1100; i < 0x1c00; i++)
   1217  1.1  jmcneill 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, i, 0);
   1218  1.1  jmcneill 
   1219  1.3  jmcneill 	/* set some sane defaults */
   1220  1.3  jmcneill 	sc->play.data_offset = ESA_DAC_DATA + data_bytes;
   1221  1.3  jmcneill 	sc->rec.data_offset = ESA_DAC_DATA + data_bytes + (data_bytes / 2);
   1222  1.3  jmcneill 
   1223  1.1  jmcneill 	esa_enable_interrupts(sc);
   1224  1.1  jmcneill 
   1225  1.1  jmcneill 	bus_space_write_1(iot, ioh, ESA_DSP_PORT_CONTROL_REG_B,
   1226  1.1  jmcneill 	    reset_state | ESA_REGB_ENABLE_RESET);
   1227  1.1  jmcneill 
   1228  1.1  jmcneill 	return (0);
   1229  1.1  jmcneill }
   1230  1.1  jmcneill 
   1231  1.1  jmcneill void
   1232  1.1  jmcneill esa_config(struct esa_softc *sc)
   1233  1.1  jmcneill {
   1234  1.1  jmcneill 	bus_space_tag_t iot = sc->sc_iot;
   1235  1.1  jmcneill 	bus_space_handle_t ioh = sc->sc_ioh;
   1236  1.1  jmcneill 	pcitag_t tag = sc->sc_tag;
   1237  1.1  jmcneill 	pci_chipset_tag_t pc = sc->sc_pct;
   1238  1.1  jmcneill 	u_int32_t data;
   1239  1.1  jmcneill 
   1240  1.1  jmcneill 	data = pci_conf_read(pc, tag, ESA_PCI_ALLEGRO_CONFIG);
   1241  1.1  jmcneill 	data &= ESA_REDUCED_DEBOUNCE;
   1242  1.1  jmcneill 	data |= ESA_PM_CTRL_ENABLE | ESA_CLK_DIV_BY_49 | ESA_USE_PCI_TIMING;
   1243  1.1  jmcneill 	pci_conf_write(pc, tag, ESA_PCI_ALLEGRO_CONFIG, data);
   1244  1.1  jmcneill 
   1245  1.1  jmcneill 	bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_B, ESA_RESET_ASSP);
   1246  1.1  jmcneill 	data = pci_conf_read(pc, tag, ESA_PCI_ALLEGRO_CONFIG);
   1247  1.1  jmcneill 	data &= ~ESA_INT_CLK_SELECT;
   1248  1.1  jmcneill 	if (sc->type == ESS_MAESTRO3) {
   1249  1.1  jmcneill 		data &= ~ESA_INT_CLK_MULT_ENABLE;
   1250  1.1  jmcneill 		data |= ESA_INT_CLK_SRC_NOT_PCI;
   1251  1.1  jmcneill 	}
   1252  1.1  jmcneill 	data &= ~(ESA_CLK_MULT_MODE_SELECT | ESA_CLK_MULT_MODE_SELECT_2);
   1253  1.1  jmcneill 	pci_conf_write(pc, tag, ESA_PCI_ALLEGRO_CONFIG, data);
   1254  1.1  jmcneill 
   1255  1.1  jmcneill 	if (sc->type == ESS_ALLEGRO1) {
   1256  1.1  jmcneill 		data = pci_conf_read(pc, tag, ESA_PCI_USER_CONFIG);
   1257  1.1  jmcneill 		data |= ESA_IN_CLK_12MHZ_SELECT;
   1258  1.1  jmcneill 		pci_conf_write(pc, tag, ESA_PCI_USER_CONFIG, data);
   1259  1.1  jmcneill 	}
   1260  1.1  jmcneill 
   1261  1.1  jmcneill 	data = bus_space_read_1(iot, ioh, ESA_ASSP_CONTROL_A);
   1262  1.1  jmcneill 	data &= ~(ESA_DSP_CLK_36MHZ_SELECT | ESA_ASSP_CLK_49MHZ_SELECT);
   1263  1.1  jmcneill 	data |= ESA_ASSP_CLK_49MHZ_SELECT;	/* XXX: Assumes 49MHz DSP */
   1264  1.1  jmcneill 	data |= ESA_ASSP_0_WS_ENABLE;
   1265  1.1  jmcneill 	bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_A, data);
   1266  1.1  jmcneill 
   1267  1.1  jmcneill 	bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_B, ESA_RUN_ASSP);
   1268  1.1  jmcneill 
   1269  1.1  jmcneill 	return;
   1270  1.1  jmcneill }
   1271  1.1  jmcneill 
   1272  1.1  jmcneill u_int8_t
   1273  1.1  jmcneill esa_assp_halt(struct esa_softc *sc)
   1274  1.1  jmcneill {
   1275  1.1  jmcneill 	bus_space_tag_t iot = sc->sc_iot;
   1276  1.1  jmcneill 	bus_space_handle_t ioh = sc->sc_ioh;
   1277  1.1  jmcneill 	u_int8_t data, reset_state;
   1278  1.1  jmcneill 
   1279  1.1  jmcneill 	data = bus_space_read_1(iot, ioh, ESA_DSP_PORT_CONTROL_REG_B);
   1280  1.1  jmcneill 	reset_state = data & ~ESA_REGB_STOP_CLOCK;
   1281  1.1  jmcneill 	delay(10000);		/* XXX use tsleep */
   1282  1.1  jmcneill 	bus_space_write_1(iot, ioh, ESA_DSP_PORT_CONTROL_REG_B,
   1283  1.1  jmcneill 			reset_state & ~ESA_REGB_ENABLE_RESET);
   1284  1.1  jmcneill 	delay(10000);		/* XXX use tsleep */
   1285  1.1  jmcneill 
   1286  1.1  jmcneill 	return (reset_state);
   1287  1.1  jmcneill }
   1288  1.1  jmcneill 
   1289  1.1  jmcneill void
   1290  1.1  jmcneill esa_codec_reset(struct esa_softc *sc)
   1291  1.1  jmcneill {
   1292  1.1  jmcneill 	bus_space_tag_t iot = sc->sc_iot;
   1293  1.1  jmcneill 	bus_space_handle_t ioh = sc->sc_ioh;
   1294  1.1  jmcneill 	u_int16_t data, dir;
   1295  1.1  jmcneill 	int retry = 0;
   1296  1.1  jmcneill 
   1297  1.1  jmcneill 	do {
   1298  1.1  jmcneill 		data = bus_space_read_2(iot, ioh, ESA_GPIO_DIRECTION);
   1299  1.1  jmcneill 		dir = data | 0x10; /* assuming pci bus master? */
   1300  1.1  jmcneill 
   1301  1.1  jmcneill 		/* remote codec config */
   1302  1.1  jmcneill 		data = bus_space_read_2(iot, ioh, ESA_RING_BUS_CTRL_B);
   1303  1.1  jmcneill 		bus_space_write_2(iot, ioh, ESA_RING_BUS_CTRL_B,
   1304  1.1  jmcneill 		    data & ~ESA_SECOND_CODEC_ID_MASK);
   1305  1.1  jmcneill 		data = bus_space_read_2(iot, ioh, ESA_SDO_OUT_DEST_CTRL);
   1306  1.1  jmcneill 		bus_space_write_2(iot, ioh, ESA_SDO_OUT_DEST_CTRL,
   1307  1.1  jmcneill 		    data & ~ESA_COMMAND_ADDR_OUT);
   1308  1.1  jmcneill 		data = bus_space_read_2(iot, ioh, ESA_SDO_IN_DEST_CTRL);
   1309  1.1  jmcneill 		bus_space_write_2(iot, ioh, ESA_SDO_IN_DEST_CTRL,
   1310  1.1  jmcneill 		    data & ~ESA_STATUS_ADDR_IN);
   1311  1.1  jmcneill 
   1312  1.1  jmcneill 		bus_space_write_2(iot, ioh, ESA_RING_BUS_CTRL_A,
   1313  1.1  jmcneill 				  ESA_IO_SRAM_ENABLE);
   1314  1.1  jmcneill 		delay(20);
   1315  1.1  jmcneill 
   1316  1.1  jmcneill 		bus_space_write_2(iot, ioh, ESA_GPIO_DIRECTION,
   1317  1.1  jmcneill 		    dir & ~ESA_GPO_PRIMARY_AC97);
   1318  1.1  jmcneill 		bus_space_write_2(iot, ioh, ESA_GPIO_MASK,
   1319  1.1  jmcneill 				  ~ESA_GPO_PRIMARY_AC97);
   1320  1.1  jmcneill 		bus_space_write_2(iot, ioh, ESA_GPIO_DATA, 0);
   1321  1.1  jmcneill 		bus_space_write_2(iot, ioh, ESA_GPIO_DIRECTION,
   1322  1.1  jmcneill 		    dir | ESA_GPO_PRIMARY_AC97);
   1323  1.1  jmcneill 		delay(sc->delay1 * 1000);
   1324  1.1  jmcneill 		bus_space_write_2(iot, ioh, ESA_GPIO_DATA,
   1325  1.1  jmcneill 				  ESA_GPO_PRIMARY_AC97);
   1326  1.1  jmcneill 		delay(5);
   1327  1.1  jmcneill 		bus_space_write_2(iot, ioh, ESA_RING_BUS_CTRL_A,
   1328  1.1  jmcneill 		    ESA_IO_SRAM_ENABLE | ESA_SERIAL_AC_LINK_ENABLE);
   1329  1.1  jmcneill 		bus_space_write_2(iot, ioh, ESA_GPIO_MASK, ~0);
   1330  1.1  jmcneill 		delay(sc->delay2 * 1000);
   1331  1.1  jmcneill 
   1332  1.1  jmcneill 		esa_read_codec(sc, 0x7c, &data);
   1333  1.1  jmcneill 		if ((data == 0) || (data == 0xffff)) {
   1334  1.1  jmcneill 			retry++;
   1335  1.1  jmcneill 			if (retry > 3) {
   1336  1.1  jmcneill 				printf("%s: esa_codec_reset: failed\n",
   1337  1.1  jmcneill 				    sc->sc_dev.dv_xname);
   1338  1.1  jmcneill 				break;
   1339  1.1  jmcneill 			}
   1340  1.1  jmcneill 			printf("%s: esa_codec_reset: retrying\n",
   1341  1.1  jmcneill 			    sc->sc_dev.dv_xname);
   1342  1.1  jmcneill 		} else
   1343  1.1  jmcneill 			retry = 0;
   1344  1.1  jmcneill 	} while (retry);
   1345  1.1  jmcneill 
   1346  1.1  jmcneill 	return;
   1347  1.1  jmcneill }
   1348  1.1  jmcneill 
   1349  1.1  jmcneill int
   1350  1.1  jmcneill esa_amp_enable(struct esa_softc *sc)
   1351  1.1  jmcneill {
   1352  1.1  jmcneill 	bus_space_tag_t iot = sc->sc_iot;
   1353  1.1  jmcneill 	bus_space_handle_t ioh = sc->sc_ioh;
   1354  1.1  jmcneill 	u_int32_t gpo, polarity_port, polarity;
   1355  1.1  jmcneill 	u_int16_t data;
   1356  1.1  jmcneill 
   1357  1.1  jmcneill 	switch (sc->type) {
   1358  1.1  jmcneill 	case ESS_ALLEGRO1:
   1359  1.1  jmcneill 		polarity_port = 0x1800;
   1360  1.1  jmcneill 		break;
   1361  1.1  jmcneill 	case ESS_MAESTRO3:
   1362  1.1  jmcneill 		polarity_port = 0x1100;
   1363  1.1  jmcneill 		break;
   1364  1.1  jmcneill 	default:
   1365  1.1  jmcneill 		printf("%s: esa_amp_enable: Unknown chip type!!!\n",
   1366  1.1  jmcneill 		    sc->sc_dev.dv_xname);
   1367  1.1  jmcneill 		return (1);
   1368  1.1  jmcneill 	}
   1369  1.1  jmcneill 
   1370  1.1  jmcneill 	gpo = (polarity_port >> 8) & 0x0f;
   1371  1.1  jmcneill 	polarity = polarity_port >> 12;
   1372  1.1  jmcneill 	polarity = !polarity;	/* Enable */
   1373  1.1  jmcneill 	polarity = polarity << gpo;
   1374  1.1  jmcneill 	gpo = 1 << gpo;
   1375  1.1  jmcneill 	bus_space_write_2(iot, ioh, ESA_GPIO_MASK, ~gpo);
   1376  1.1  jmcneill 	data = bus_space_read_2(iot, ioh, ESA_GPIO_DIRECTION);
   1377  1.1  jmcneill 	bus_space_write_2(iot, ioh, ESA_GPIO_DIRECTION, data | gpo);
   1378  1.1  jmcneill 	data = ESA_GPO_SECONDARY_AC97 | ESA_GPO_PRIMARY_AC97 | polarity;
   1379  1.1  jmcneill 	bus_space_write_2(iot, ioh, ESA_GPIO_DATA, data);
   1380  1.1  jmcneill 	bus_space_write_2(iot, ioh, ESA_GPIO_MASK, ~0);
   1381  1.1  jmcneill 
   1382  1.1  jmcneill 	return (0);
   1383  1.1  jmcneill }
   1384  1.1  jmcneill 
   1385  1.1  jmcneill void
   1386  1.1  jmcneill esa_enable_interrupts(struct esa_softc *sc)
   1387  1.1  jmcneill {
   1388  1.1  jmcneill 	bus_space_tag_t iot = sc->sc_iot;
   1389  1.1  jmcneill 	bus_space_handle_t ioh = sc->sc_ioh;
   1390  1.1  jmcneill 	u_int8_t data;
   1391  1.1  jmcneill 
   1392  1.1  jmcneill 	bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL,
   1393  1.1  jmcneill 	    ESA_ASSP_INT_ENABLE | ESA_HV_INT_ENABLE);
   1394  1.1  jmcneill 	data = bus_space_read_1(iot, ioh, ESA_ASSP_CONTROL_C);
   1395  1.1  jmcneill 	bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_C,
   1396  1.1  jmcneill 	    data | ESA_ASSP_HOST_INT_ENABLE);
   1397  1.1  jmcneill }
   1398  1.1  jmcneill 
   1399  1.1  jmcneill int
   1400  1.1  jmcneill esa_power(struct esa_softc *sc, int state)
   1401  1.1  jmcneill {
   1402  1.1  jmcneill 	pcitag_t tag = sc->sc_tag;
   1403  1.1  jmcneill 	pci_chipset_tag_t pc = sc->sc_pct;
   1404  1.7     pooka 	pcireg_t data;
   1405  1.7     pooka 	int pmcapreg;
   1406  1.1  jmcneill 
   1407  1.7     pooka 	if (pci_get_capability(pc, tag, PCI_CAP_PWRMGMT, &pmcapreg, 0)) {
   1408  1.7     pooka 		data = pci_conf_read(pc, tag, pmcapreg + 4);
   1409  1.7     pooka 		if ((data && PCI_PMCSR_STATE_MASK) != state)
   1410  1.7     pooka 			pci_conf_write(pc, tag, pmcapreg + 4, state);
   1411  1.7     pooka 	}
   1412  1.7     pooka 
   1413  1.1  jmcneill 	return (0);
   1414  1.1  jmcneill }
   1415  1.1  jmcneill 
   1416  1.4     pooka void
   1417  1.4     pooka esa_powerhook(int why, void *hdl)
   1418  1.4     pooka {
   1419  1.4     pooka 	struct esa_softc *sc = (struct esa_softc *)hdl;
   1420  1.4     pooka 
   1421  1.4     pooka 	switch (why) {
   1422  1.4     pooka 	case PWR_SUSPEND:
   1423  1.4     pooka 	case PWR_STANDBY:
   1424  1.4     pooka 		esa_suspend(sc);
   1425  1.4     pooka 		break;
   1426  1.4     pooka 	case PWR_RESUME:
   1427  1.4     pooka 		esa_resume(sc);
   1428  1.4     pooka 		(sc->codec_if->vtbl->restore_ports)(sc->codec_if);
   1429  1.4     pooka 		break;
   1430  1.4     pooka 	}
   1431  1.4     pooka }
   1432  1.4     pooka 
   1433  1.4     pooka int
   1434  1.4     pooka esa_suspend(struct esa_softc *sc)
   1435  1.4     pooka {
   1436  1.4     pooka 	bus_space_tag_t iot = sc->sc_iot;
   1437  1.4     pooka 	bus_space_handle_t ioh = sc->sc_ioh;
   1438  1.4     pooka 	int x, i, index;
   1439  1.4     pooka 
   1440  1.4     pooka 	index = 0;
   1441  1.4     pooka 
   1442  1.4     pooka 	x = splaudio();
   1443  1.4     pooka 	esa_halt_output(sc);
   1444  1.4     pooka 	delay(10000);
   1445  1.4     pooka 	splx(x);
   1446  1.4     pooka 
   1447  1.4     pooka 	bus_space_write_2(iot, ioh, ESA_HOST_INT_CTRL, 0);
   1448  1.4     pooka 	bus_space_write_1(iot, ioh, ESA_ASSP_CONTROL_C, 0);
   1449  1.4     pooka 
   1450  1.4     pooka 	esa_assp_halt(sc);
   1451  1.4     pooka 
   1452  1.4     pooka 	/* Save ASSP state */
   1453  1.4     pooka 	for (i = ESA_REV_B_CODE_MEMORY_BEGIN; i <= ESA_REV_B_CODE_MEMORY_END;
   1454  1.4     pooka 	    i++)
   1455  1.4     pooka 		sc->savemem[index++] = esa_read_assp(sc,
   1456  1.4     pooka 		    ESA_MEMTYPE_INTERNAL_CODE, i);
   1457  1.4     pooka 	for (i = ESA_REV_B_DATA_MEMORY_BEGIN; i <= ESA_REV_B_DATA_MEMORY_END;
   1458  1.4     pooka 	    i++)
   1459  1.4     pooka 		sc->savemem[index++] = esa_read_assp(sc,
   1460  1.4     pooka 		    ESA_MEMTYPE_INTERNAL_DATA, i);
   1461  1.4     pooka 
   1462  1.7     pooka 	esa_power(sc, PCI_PMCSR_STATE_D3);
   1463  1.4     pooka 
   1464  1.4     pooka 	return (0);
   1465  1.4     pooka }
   1466  1.4     pooka 
   1467  1.4     pooka int
   1468  1.4     pooka esa_resume(struct esa_softc *sc) {
   1469  1.4     pooka 	bus_space_tag_t iot = sc->sc_iot;
   1470  1.4     pooka 	bus_space_handle_t ioh = sc->sc_ioh;
   1471  1.4     pooka 	int i, index;
   1472  1.4     pooka 	u_int8_t reset_state;
   1473  1.4     pooka 
   1474  1.4     pooka 	index = 0;
   1475  1.4     pooka 
   1476  1.7     pooka 	esa_power(sc, PCI_PMCSR_STATE_D0);
   1477  1.4     pooka 	delay(10000);
   1478  1.4     pooka 
   1479  1.4     pooka 	esa_config(sc);
   1480  1.4     pooka 
   1481  1.4     pooka 	reset_state = esa_assp_halt(sc);
   1482  1.4     pooka 
   1483  1.4     pooka 	/* restore ASSP */
   1484  1.4     pooka 	for (i = ESA_REV_B_CODE_MEMORY_BEGIN; i <= ESA_REV_B_CODE_MEMORY_END;
   1485  1.4     pooka 	    i++)
   1486  1.4     pooka 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_CODE, i,
   1487  1.4     pooka 		    sc->savemem[index++]);
   1488  1.4     pooka 	for (i = ESA_REV_B_DATA_MEMORY_BEGIN; i <= ESA_REV_B_DATA_MEMORY_END;
   1489  1.4     pooka 	    i++)
   1490  1.4     pooka 		esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, i,
   1491  1.4     pooka 		    sc->savemem[index++]);
   1492  1.4     pooka 
   1493  1.4     pooka 	esa_write_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, ESA_KDATA_DMA_ACTIVE, 0);
   1494  1.4     pooka 	bus_space_write_1(iot, ioh, ESA_DSP_PORT_CONTROL_REG_B,
   1495  1.4     pooka 	    reset_state | ESA_REGB_ENABLE_RESET);
   1496  1.4     pooka 
   1497  1.4     pooka 	esa_enable_interrupts(sc);
   1498  1.4     pooka 	esa_amp_enable(sc);
   1499  1.4     pooka 
   1500  1.4     pooka 	return (0);
   1501  1.4     pooka }
   1502  1.4     pooka 
   1503  1.1  jmcneill u_int32_t
   1504  1.3  jmcneill esa_get_pointer(struct esa_softc *sc, struct esa_channel *ch)
   1505  1.1  jmcneill {
   1506  1.1  jmcneill 	u_int16_t hi = 0, lo = 0;
   1507  1.1  jmcneill 	u_int32_t addr;
   1508  1.3  jmcneill 	int data_offset = ch->data_offset;
   1509  1.1  jmcneill 
   1510  1.3  jmcneill 	hi = esa_read_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, data_offset +
   1511  1.1  jmcneill 	    ESA_CDATA_HOST_SRC_CURRENTH);
   1512  1.3  jmcneill 	lo = esa_read_assp(sc, ESA_MEMTYPE_INTERNAL_DATA, data_offset +
   1513  1.1  jmcneill 	    ESA_CDATA_HOST_SRC_CURRENTL);
   1514  1.1  jmcneill 
   1515  1.1  jmcneill 	addr = lo | ((u_int32_t)hi << 16);
   1516  1.3  jmcneill 	return (addr - ch->start);
   1517  1.1  jmcneill }
   1518  1.1  jmcneill 
   1519  1.1  jmcneill paddr_t
   1520  1.1  jmcneill esa_mappage(void *addr, void *mem, off_t off, int prot)
   1521  1.1  jmcneill {
   1522  1.1  jmcneill 	struct esa_softc *sc = addr;
   1523  1.1  jmcneill 	struct esa_dma *p;
   1524  1.1  jmcneill 
   1525  1.1  jmcneill 	if (off < 0)
   1526  1.1  jmcneill 		return (-1);
   1527  1.1  jmcneill 	for (p = sc->sc_dmas; p && KERNADDR(p) != mem; p = p->next)
   1528  1.1  jmcneill 		;
   1529  1.1  jmcneill 	if (!p)
   1530  1.1  jmcneill 		return (-1);
   1531  1.1  jmcneill 	return (bus_dmamem_mmap(sc->sc_dmat, p->segs, p->nsegs,
   1532  1.1  jmcneill 				off, prot, BUS_DMA_WAITOK));
   1533  1.1  jmcneill }
   1534