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