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