Home | History | Annotate | Line # | Download | only in pci
auixp.c revision 1.1
      1  1.1  reinoud /* $NetBSD: auixp.c,v 1.1 2005/01/12 00:05:43 reinoud Exp $ */
      2  1.1  reinoud 
      3  1.1  reinoud /*
      4  1.1  reinoud  * Copyright (c) 2004 Reinoud Zandijk <reinoud (at) netbsd.org>
      5  1.1  reinoud  * All rights reserved.
      6  1.1  reinoud  *
      7  1.1  reinoud  * Redistribution and use in source and binary forms, with or without
      8  1.1  reinoud  * modification, are permitted provided that the following conditions
      9  1.1  reinoud  * are met:
     10  1.1  reinoud  * 1. Redistributions of source code must retain the above copyright
     11  1.1  reinoud  *    notice, this list of conditions and the following disclaimer.
     12  1.1  reinoud  * 2. The name of the author may not be used to endorse or promote products
     13  1.1  reinoud  *    derived from this software without specific prior written permission.
     14  1.1  reinoud  * 3. All advertising materials mentioning features or use of this software
     15  1.1  reinoud  *    must display the following acknowledgement:
     16  1.1  reinoud  *	This product includes software developed by the NetBSD
     17  1.1  reinoud  *	Foundation, Inc. and its contributors.
     18  1.1  reinoud  * 4. Neither the name of The NetBSD Foundation nor the names of its
     19  1.1  reinoud  *    contributors may be used to endorse or promote products derived
     20  1.1  reinoud  *    from this software without specific prior written permission.
     21  1.1  reinoud  *
     22  1.1  reinoud  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  1.1  reinoud  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  1.1  reinoud  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  1.1  reinoud  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  1.1  reinoud  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     27  1.1  reinoud  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     28  1.1  reinoud  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     29  1.1  reinoud  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     30  1.1  reinoud  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.1  reinoud  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.1  reinoud  * SUCH DAMAGE.
     33  1.1  reinoud  */
     34  1.1  reinoud 
     35  1.1  reinoud 
     36  1.1  reinoud /*
     37  1.1  reinoud  * NetBSD audio driver for ATI IXP-{150,200,...} audio driver hardware.
     38  1.1  reinoud  *
     39  1.1  reinoud  * Recording and playback has been tested OK on various sample rates and
     40  1.1  reinoud  * encodings.
     41  1.1  reinoud  *
     42  1.1  reinoud  * Known problems and issues :
     43  1.1  reinoud  * - SPDIF is untested and needs some work still (LED stays off)
     44  1.1  reinoud  * - 32 bit audio playback failed last time i tried but that might an AC'97
     45  1.1  reinoud  *   codec support problem.
     46  1.1  reinoud  * - 32 bit recording works but can't try out playing: see above.
     47  1.1  reinoud  * - no suspend/resume support yet.
     48  1.1  reinoud  * - multiple codecs are `supported' but not tested; the implemetation needs
     49  1.1  reinoud  *   some cleaning up.
     50  1.1  reinoud  */
     51  1.1  reinoud 
     52  1.1  reinoud #include <sys/cdefs.h>
     53  1.1  reinoud __KERNEL_RCSID(0, "$NetBSD: auixp.c,v 1.1 2005/01/12 00:05:43 reinoud Exp $");
     54  1.1  reinoud 
     55  1.1  reinoud #include <sys/types.h>
     56  1.1  reinoud #include <sys/errno.h>
     57  1.1  reinoud #include <sys/null.h>
     58  1.1  reinoud #include <sys/param.h>
     59  1.1  reinoud #include <sys/systm.h>
     60  1.1  reinoud #include <sys/malloc.h>
     61  1.1  reinoud #include <sys/device.h>
     62  1.1  reinoud #include <sys/conf.h>
     63  1.1  reinoud #include <sys/exec.h>
     64  1.1  reinoud #include <sys/select.h>
     65  1.1  reinoud #include <sys/audioio.h>
     66  1.1  reinoud #include <sys/queue.h>
     67  1.1  reinoud 
     68  1.1  reinoud #include <machine/bus.h>
     69  1.1  reinoud #include <machine/intr.h>
     70  1.1  reinoud 
     71  1.1  reinoud #include <dev/pci/pcidevs.h>
     72  1.1  reinoud #include <dev/pci/pcivar.h>
     73  1.1  reinoud 
     74  1.1  reinoud #include <dev/audio_if.h>
     75  1.1  reinoud #include <dev/mulaw.h>
     76  1.1  reinoud #include <dev/auconv.h>
     77  1.1  reinoud #include <dev/ic/ac97var.h>
     78  1.1  reinoud #include <dev/ic/ac97reg.h>
     79  1.1  reinoud 
     80  1.1  reinoud #include <dev/pci/auixpreg.h>
     81  1.1  reinoud #include <dev/pci/auixpvar.h>
     82  1.1  reinoud 
     83  1.1  reinoud 
     84  1.1  reinoud //#define DEBUG_AUIXP
     85  1.1  reinoud 
     86  1.1  reinoud 
     87  1.1  reinoud /* why isn't this base address register not in the headerfile? */
     88  1.1  reinoud #define PCI_CBIO 0x10
     89  1.1  reinoud 
     90  1.1  reinoud 
     91  1.1  reinoud /* macro's used */
     92  1.1  reinoud #define KERNADDR(p)	((void *)((p)->addr))
     93  1.1  reinoud #define	DMAADDR(p)	((p)->map->dm_segs[0].ds_addr)
     94  1.1  reinoud 
     95  1.1  reinoud 
     96  1.1  reinoud /* the differences might be irrelevant */
     97  1.1  reinoud enum {
     98  1.1  reinoud 	IXP_200,
     99  1.1  reinoud 	IXP_300,
    100  1.1  reinoud 	IXP_400
    101  1.1  reinoud };
    102  1.1  reinoud 
    103  1.1  reinoud 
    104  1.1  reinoud /* our `cards' */
    105  1.1  reinoud static struct auixp_card_type {
    106  1.1  reinoud 	uint16_t pci_vendor_id;
    107  1.1  reinoud 	uint16_t pci_product_id;
    108  1.1  reinoud 	int type;
    109  1.1  reinoud } auixp_card_types[] = {
    110  1.1  reinoud 	{ PCI_VENDOR_ATI, PCI_PRODUCT_ATI_IXP_AUDIO_200, IXP_200 },
    111  1.1  reinoud 	{ PCI_VENDOR_ATI, PCI_PRODUCT_ATI_IXP_AUDIO_300, IXP_300 },
    112  1.1  reinoud 	{ PCI_VENDOR_ATI, PCI_PRODUCT_ATI_IXP_AUDIO_400, IXP_400 },
    113  1.1  reinoud 	{ 0, 0, 0 }
    114  1.1  reinoud };
    115  1.1  reinoud 
    116  1.1  reinoud 
    117  1.1  reinoud struct audio_device auixp_device = {
    118  1.1  reinoud 	"ATI IXP audio",
    119  1.1  reinoud 	"",
    120  1.1  reinoud 	"auixp"
    121  1.1  reinoud };
    122  1.1  reinoud 
    123  1.1  reinoud 
    124  1.1  reinoud /* codec detection constant indicating the interrupt flags */
    125  1.1  reinoud #define ALL_CODECS_NOT_READY \
    126  1.1  reinoud 	    (ATI_REG_ISR_CODEC0_NOT_READY |\
    127  1.1  reinoud 	     ATI_REG_ISR_CODEC1_NOT_READY |\
    128  1.1  reinoud 	     ATI_REG_ISR_CODEC2_NOT_READY)
    129  1.1  reinoud #define CODEC_CHECK_BITS (ALL_CODECS_NOT_READY|ATI_REG_ISR_NEW_FRAME)
    130  1.1  reinoud 
    131  1.1  reinoud 
    132  1.1  reinoud /* autoconfig */
    133  1.1  reinoud int	auixp_match( struct device *, struct cfdata *, void *);
    134  1.1  reinoud void	auixp_attach(struct device *, struct device *, void *);
    135  1.1  reinoud int	auixp_detach(struct device *, int);
    136  1.1  reinoud 
    137  1.1  reinoud 
    138  1.1  reinoud /* audio(9) function prototypes */
    139  1.1  reinoud int	auixp_open( void *, int);
    140  1.1  reinoud void	auixp_close(void *);
    141  1.1  reinoud int	auixp_query_encoding(void *, struct audio_encoding *);
    142  1.1  reinoud int	auixp_set_params(void *, int, int, audio_params_t *, audio_params_t *, stream_filter_list_t *, stream_filter_list_t *);
    143  1.1  reinoud int	auixp_commit_settings(void *);
    144  1.1  reinoud int	auixp_round_blocksize(void *, int, int, const audio_params_t *);
    145  1.1  reinoud int	auixp_trigger_output(void *, void *, void *, int, void (*)(void *), void *, const audio_params_t *);
    146  1.1  reinoud int	auixp_trigger_input(void *, void *, void *, int, void (*)(void *), void *, const audio_params_t *);
    147  1.1  reinoud int	auixp_halt_output(void *);
    148  1.1  reinoud int	auixp_halt_input(void *);
    149  1.1  reinoud int	auixp_set_port(void *, mixer_ctrl_t *);
    150  1.1  reinoud int	auixp_get_port(void *, mixer_ctrl_t *);
    151  1.1  reinoud int	auixp_query_devinfo(void *, mixer_devinfo_t *);
    152  1.1  reinoud void *	auixp_malloc(void *, int, size_t, struct malloc_type *, int);
    153  1.1  reinoud void	auixp_free(void *, void *, struct malloc_type *);
    154  1.1  reinoud int	auixp_getdev(void *, struct audio_device *);
    155  1.1  reinoud size_t	auixp_round_buffersize(void *, int, size_t);
    156  1.1  reinoud int	auixp_get_props(void *);
    157  1.1  reinoud int	auixp_intr(void *);
    158  1.1  reinoud int	auixp_allocmem(struct auixp_softc *, size_t, size_t, struct auixp_dma *);
    159  1.1  reinoud int	auixp_freemem(struct auixp_softc *, struct auixp_dma *);
    160  1.1  reinoud paddr_t	auixp_mappage(void *addr, void *mem, off_t off, int prot);
    161  1.1  reinoud 
    162  1.1  reinoud 
    163  1.1  reinoud /* power management (do we support that already?) */
    164  1.1  reinoud int	auixp_power(struct auixp_softc *, int);
    165  1.1  reinoud void	auixp_powerhook(int, void *);
    166  1.1  reinoud int	auixp_suspend(struct auixp_softc *);
    167  1.1  reinoud int	auixp_resume(struct auixp_softc *);
    168  1.1  reinoud void	auixp_config(struct auixp_softc *);	/* do we use it? */
    169  1.1  reinoud 
    170  1.1  reinoud 
    171  1.1  reinoud /* Supporting subroutines */
    172  1.1  reinoud int	auixp_init(struct auixp_softc *);
    173  1.1  reinoud void	auixp_autodetect_codecs(struct auixp_softc *sc);
    174  1.1  reinoud void	auixp_post_config(struct device *self);
    175  1.1  reinoud 
    176  1.1  reinoud void	auixp_reset_aclink(struct auixp_softc *sc);
    177  1.1  reinoud int	auixp_attach_codec(void *, struct ac97_codec_if *);
    178  1.1  reinoud int	auixp_read_codec(void *, u_int8_t, uint16_t *);
    179  1.1  reinoud int	auixp_write_codec(void *, u_int8_t, uint16_t);
    180  1.1  reinoud int	auixp_wait_for_codecs(struct auixp_softc *sc, char *func);
    181  1.1  reinoud int	auixp_reset_codec(void *);
    182  1.1  reinoud enum ac97_host_flags	auixp_flags_codec(void *);
    183  1.1  reinoud 
    184  1.1  reinoud void	auixp_enable_dma(struct auixp_softc *sc, struct auixp_dma *dma);
    185  1.1  reinoud void	auixp_disable_dma(struct auixp_softc *sc, struct auixp_dma *dma);
    186  1.1  reinoud void	auixp_enable_interrupts(struct auixp_softc *sc);
    187  1.1  reinoud void	auixp_disable_interrupts(struct auixp_softc *sc);
    188  1.1  reinoud 
    189  1.1  reinoud 
    190  1.1  reinoud /* statics */
    191  1.1  reinoud void	auixp_link_daisychain(struct auixp_softc *sc,
    192  1.1  reinoud 		struct auixp_dma *c_dma, struct auixp_dma *a_dma,
    193  1.1  reinoud 		int blksize, int blocks);
    194  1.1  reinoud int	auixp_allocate_dma_chain(struct auixp_softc *sc, struct auixp_dma **dmap);
    195  1.1  reinoud void	auixp_program_dma_chain(struct auixp_softc *sc, struct auixp_dma *dma);
    196  1.1  reinoud void	auixp_dma_update(struct auixp_softc *sc, struct auixp_dma *dma);
    197  1.1  reinoud void	auixp_update_busbusy(struct auixp_softc *sc);
    198  1.1  reinoud 
    199  1.1  reinoud 
    200  1.1  reinoud #ifdef DEBUG_AUIXP
    201  1.1  reinoud struct auixp_softc *static_sc;
    202  1.1  reinoud void auixp_dumpreg(void);
    203  1.1  reinoud #	define DPRINTF(x) printf x;
    204  1.1  reinoud #else
    205  1.1  reinoud #	define DPRINTF(x)
    206  1.1  reinoud #endif
    207  1.1  reinoud 
    208  1.1  reinoud 
    209  1.1  reinoud const struct audio_hw_if auixp_hw_if = {
    210  1.1  reinoud 	auixp_open,
    211  1.1  reinoud 	auixp_close,
    212  1.1  reinoud 	NULL,			/* drain */
    213  1.1  reinoud 	auixp_query_encoding,
    214  1.1  reinoud 	auixp_set_params,
    215  1.1  reinoud 	auixp_round_blocksize,
    216  1.1  reinoud 	auixp_commit_settings,
    217  1.1  reinoud 	NULL,			/* init_output  */
    218  1.1  reinoud 	NULL,			/* init_input   */
    219  1.1  reinoud 	NULL,			/* start_output */
    220  1.1  reinoud 	NULL,			/* start_input  */
    221  1.1  reinoud 	auixp_halt_output,
    222  1.1  reinoud 	auixp_halt_input,
    223  1.1  reinoud 	NULL,			/* speaker_ctl */
    224  1.1  reinoud 	auixp_getdev,
    225  1.1  reinoud 	NULL,			/* getfd */
    226  1.1  reinoud 	auixp_set_port,
    227  1.1  reinoud 	auixp_get_port,
    228  1.1  reinoud 	auixp_query_devinfo,
    229  1.1  reinoud 	auixp_malloc,
    230  1.1  reinoud 	auixp_free,
    231  1.1  reinoud 	auixp_round_buffersize,
    232  1.1  reinoud 	auixp_mappage,
    233  1.1  reinoud 	auixp_get_props,
    234  1.1  reinoud 	auixp_trigger_output,
    235  1.1  reinoud 	auixp_trigger_input
    236  1.1  reinoud };
    237  1.1  reinoud 
    238  1.1  reinoud 
    239  1.1  reinoud CFATTACH_DECL(auixp, sizeof(struct auixp_softc), auixp_match, auixp_attach,
    240  1.1  reinoud     auixp_detach, NULL);
    241  1.1  reinoud 
    242  1.1  reinoud 
    243  1.1  reinoud /*
    244  1.1  reinoud  * audio(9) functions
    245  1.1  reinoud  */
    246  1.1  reinoud 
    247  1.1  reinoud int auixp_open(void *hdl, int flags) {
    248  1.1  reinoud 	return (0);
    249  1.1  reinoud }
    250  1.1  reinoud 
    251  1.1  reinoud 
    252  1.1  reinoud void auixp_close(void *hdl) {
    253  1.1  reinoud 	return;
    254  1.1  reinoud }
    255  1.1  reinoud 
    256  1.1  reinoud 
    257  1.1  reinoud int auixp_query_encoding(void *hdl, struct audio_encoding *ae) {
    258  1.1  reinoud 	struct auixp_codec *co = (struct auixp_codec *) hdl;
    259  1.1  reinoud 	struct auixp_softc *sc = co->sc;
    260  1.1  reinoud 
    261  1.1  reinoud 	return auconv_query_encoding(sc->sc_encodings, ae);
    262  1.1  reinoud }
    263  1.1  reinoud 
    264  1.1  reinoud 
    265  1.1  reinoud static int auixp_set_rate(struct auixp_codec *co, int mode, u_long srate) {
    266  1.1  reinoud 	int ret;
    267  1.1  reinoud 	u_int ratetmp;
    268  1.1  reinoud 
    269  1.1  reinoud 	ratetmp = srate;
    270  1.1  reinoud 	if (mode == AUMODE_RECORD) {
    271  1.1  reinoud 		ret = co->codec_if->vtbl->set_rate(co->codec_if, AC97_REG_PCM_LR_ADC_RATE, &ratetmp);
    272  1.1  reinoud 		return ret;
    273  1.1  reinoud 	};
    274  1.1  reinoud 
    275  1.1  reinoud 	/* play mode */
    276  1.1  reinoud 	ret = co->codec_if->vtbl->set_rate(co->codec_if, AC97_REG_PCM_FRONT_DAC_RATE, &ratetmp);
    277  1.1  reinoud 	if (ret) return ret;
    278  1.1  reinoud 
    279  1.1  reinoud 	ratetmp = srate;
    280  1.1  reinoud 	ret = co->codec_if->vtbl->set_rate(co->codec_if, AC97_REG_PCM_SURR_DAC_RATE, &ratetmp);
    281  1.1  reinoud 	if (ret) return ret;
    282  1.1  reinoud 
    283  1.1  reinoud 	ratetmp = srate;
    284  1.1  reinoud 	ret = co->codec_if->vtbl->set_rate(co->codec_if, AC97_REG_PCM_LFE_DAC_RATE, &ratetmp);
    285  1.1  reinoud 	return ret;
    286  1.1  reinoud }
    287  1.1  reinoud 
    288  1.1  reinoud 
    289  1.1  reinoud /* commit setting and program ATI IXP chip */
    290  1.1  reinoud int auixp_commit_settings(void *hdl) {
    291  1.1  reinoud 	struct auixp_codec *co = (struct auixp_codec *) hdl;
    292  1.1  reinoud 	struct auixp_softc *sc = co->sc;
    293  1.1  reinoud 	bus_space_tag_t    iot = sc->sc_iot;
    294  1.1  reinoud 	bus_space_handle_t ioh = sc->sc_ioh;
    295  1.1  reinoud 	struct audio_params *params;
    296  1.1  reinoud 	uint32_t value;
    297  1.1  reinoud 
    298  1.1  reinoud 	/* XXX would it be better to stop interrupts first? XXX */
    299  1.1  reinoud 
    300  1.1  reinoud 	/* process input settings */
    301  1.1  reinoud 	params = &sc->sc_play_params;
    302  1.1  reinoud 
    303  1.1  reinoud 	/* set input interleaving (precision) */
    304  1.1  reinoud 	value  =  bus_space_read_4(iot, ioh, ATI_REG_CMD);
    305  1.1  reinoud 	value &= ~ATI_REG_CMD_INTERLEAVE_IN;
    306  1.1  reinoud 	if (params->precision <= 16) value |= ATI_REG_CMD_INTERLEAVE_IN;
    307  1.1  reinoud 	bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
    308  1.1  reinoud 
    309  1.1  reinoud 	/* process output settings */
    310  1.1  reinoud 	params = &sc->sc_play_params;
    311  1.1  reinoud 
    312  1.1  reinoud 	value  =  bus_space_read_4(iot, ioh, ATI_REG_OUT_DMA_SLOT);
    313  1.1  reinoud 	value &= ~ATI_REG_OUT_DMA_SLOT_MASK;
    314  1.1  reinoud 
    315  1.1  reinoud 	switch (params->channels) {
    316  1.1  reinoud 	case 6:
    317  1.1  reinoud 		value |= ATI_REG_OUT_DMA_SLOT_BIT(7) |
    318  1.1  reinoud 			 ATI_REG_OUT_DMA_SLOT_BIT(8);
    319  1.1  reinoud 		/* fallthru */
    320  1.1  reinoud 	case 4:
    321  1.1  reinoud 		value |= ATI_REG_OUT_DMA_SLOT_BIT(6) |
    322  1.1  reinoud 			 ATI_REG_OUT_DMA_SLOT_BIT(9);
    323  1.1  reinoud 		/* fallthru */
    324  1.1  reinoud 	default:
    325  1.1  reinoud 		value |= ATI_REG_OUT_DMA_SLOT_BIT(3) |
    326  1.1  reinoud 			 ATI_REG_OUT_DMA_SLOT_BIT(4);
    327  1.1  reinoud 		break;
    328  1.1  reinoud 	};
    329  1.1  reinoud 	/* set output threshold */
    330  1.1  reinoud 	value |= 0x04 << ATI_REG_OUT_DMA_THRESHOLD_SHIFT;
    331  1.1  reinoud 	bus_space_write_4(iot, ioh, ATI_REG_OUT_DMA_SLOT, value);
    332  1.1  reinoud 
    333  1.1  reinoud 	/* set output interleaving (precision) */
    334  1.1  reinoud 	value  =  bus_space_read_4(iot, ioh, ATI_REG_CMD);
    335  1.1  reinoud 	value &= ~ATI_REG_CMD_INTERLEAVE_OUT;
    336  1.1  reinoud 	if (params->precision <= 16) value |= ATI_REG_CMD_INTERLEAVE_OUT;
    337  1.1  reinoud 	bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
    338  1.1  reinoud 
    339  1.1  reinoud 	/* enable 6 channel reordering */
    340  1.1  reinoud 	value  =  bus_space_read_4(iot, ioh, ATI_REG_6CH_REORDER);
    341  1.1  reinoud 	value &= ~ATI_REG_6CH_REORDER_EN;
    342  1.1  reinoud 	if (params->channels == 6) value |= ATI_REG_6CH_REORDER_EN;
    343  1.1  reinoud 	bus_space_write_4(iot, ioh, ATI_REG_6CH_REORDER, value);
    344  1.1  reinoud 
    345  1.1  reinoud 	if (sc->has_spdif) {
    346  1.1  reinoud 		/* set SPDIF (if present) */
    347  1.1  reinoud 		value  =  bus_space_read_4(iot, ioh, ATI_REG_CMD);
    348  1.1  reinoud 		value &= ~ATI_REG_CMD_SPDF_CONFIG_MASK;
    349  1.1  reinoud 		value |=  ATI_REG_CMD_SPDF_CONFIG_34;		/* NetBSD AC'97 default */
    350  1.1  reinoud 
    351  1.1  reinoud 		/* XXX this prolly is not nessisary unless splitted XXX */
    352  1.1  reinoud 		value &= ~ATI_REG_CMD_INTERLEAVE_SPDF;
    353  1.1  reinoud 		if (params->precision <= 16) value |= ATI_REG_CMD_INTERLEAVE_SPDF;
    354  1.1  reinoud 		bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
    355  1.1  reinoud 	};
    356  1.1  reinoud 
    357  1.1  reinoud 	return 0;
    358  1.1  reinoud }
    359  1.1  reinoud 
    360  1.1  reinoud 
    361  1.1  reinoud /* set audio properties in desired setting */
    362  1.1  reinoud int auixp_set_params(void *hdl, int setmode, int usemode, audio_params_t *play, audio_params_t *rec, stream_filter_list_t *pfil, stream_filter_list_t *rfil) {
    363  1.1  reinoud 	struct auixp_codec *co = (struct auixp_codec *) hdl;
    364  1.1  reinoud 	struct auixp_softc *sc = co->sc;
    365  1.1  reinoud 	audio_params_t *params;
    366  1.1  reinoud 	stream_filter_list_t *fil;
    367  1.1  reinoud 	int mode, index;
    368  1.1  reinoud 
    369  1.1  reinoud 	/*
    370  1.1  reinoud 	 * In current NetBSD AC'97 implementation, SPDF is linked to channel 3
    371  1.1  reinoud 	 * and 4 i.e. stereo output.
    372  1.1  reinoud 	 */
    373  1.1  reinoud 
    374  1.1  reinoud 	for (mode = AUMODE_RECORD; mode != -1;
    375  1.1  reinoud 	     mode = (mode == AUMODE_RECORD) ? AUMODE_PLAY : -1) {
    376  1.1  reinoud 		if ((setmode & mode) == 0)
    377  1.1  reinoud 			continue;
    378  1.1  reinoud 
    379  1.1  reinoud 		params = (mode == AUMODE_PLAY) ? play :  rec;
    380  1.1  reinoud 		fil    = (mode == AUMODE_PLAY) ? pfil : rfil;
    381  1.1  reinoud 		if (params == NULL) continue;
    382  1.1  reinoud 
    383  1.1  reinoud 		/* AD1888 settings ... don't know the IXP limits */
    384  1.1  reinoud 		if (params->sample_rate < AUIXP_MINRATE) return EINVAL;
    385  1.1  reinoud 		if (params->sample_rate > AUIXP_MAXRATE) return EINVAL;
    386  1.1  reinoud 
    387  1.1  reinoud 		index = auconv_set_converter(sc->sc_formats, AUIXP_NFORMATS, mode, params, TRUE, fil);
    388  1.1  reinoud 
    389  1.1  reinoud 		/* nothing found? */
    390  1.1  reinoud 		if (index < 0) return EINVAL;
    391  1.1  reinoud 
    392  1.1  reinoud 		/* not sure yet as to why i have to change params here */
    393  1.1  reinoud 		if (fil->req_size > 0) params = &fil->filters[0].param;
    394  1.1  reinoud 
    395  1.1  reinoud 		/* if variable speed and we can't set the desired rate, fail */
    396  1.1  reinoud 		if ((sc->sc_formats[index].frequency_type != 1) &&
    397  1.1  reinoud 		    auixp_set_rate(co, mode, params->sample_rate)) return EINVAL;
    398  1.1  reinoud 
    399  1.1  reinoud 		/* preserve the settings */
    400  1.1  reinoud 		if (mode == AUMODE_PLAY)   sc->sc_play_params = *params;
    401  1.1  reinoud 		if (mode == AUMODE_RECORD) sc->sc_rec_params  = *params;
    402  1.1  reinoud 	}
    403  1.1  reinoud 
    404  1.1  reinoud 	return (0);
    405  1.1  reinoud }
    406  1.1  reinoud 
    407  1.1  reinoud 
    408  1.1  reinoud /* called to translate a requested blocksize to a hw-possible one */
    409  1.1  reinoud int auixp_round_blocksize(void *hdl, int bs, int mode, const audio_params_t *param) {
    410  1.1  reinoud 	uint32_t new_bs = bs;
    411  1.1  reinoud 
    412  1.1  reinoud 	/* Be conservative; align to 32 bytes and maximise it to 64 kb */
    413  1.1  reinoud 	/* 256 kb possible */
    414  1.1  reinoud 	if (new_bs > 0x10000) bs = 0x10000;	/* 64 kb max */
    415  1.1  reinoud 	new_bs = (bs & ~0x20);			/* 32 bytes align */
    416  1.1  reinoud 
    417  1.1  reinoud 	return new_bs;
    418  1.1  reinoud }
    419  1.1  reinoud 
    420  1.1  reinoud 
    421  1.1  reinoud /*
    422  1.1  reinoud  * allocate dma capable memory and record its information for later retrieval
    423  1.1  reinoud  * when we program the dma chain itself. The trigger routines passes on the
    424  1.1  reinoud  * kernel virtual address we return here as a reference to the mapping.
    425  1.1  reinoud  */
    426  1.1  reinoud 
    427  1.1  reinoud void *auixp_malloc(void *hdl, int direction, size_t size, struct malloc_type *type, int flags) {
    428  1.1  reinoud 	struct auixp_codec *co = (struct auixp_codec *) hdl;
    429  1.1  reinoud 	struct auixp_softc *sc = co->sc;
    430  1.1  reinoud 	struct auixp_dma *dma;
    431  1.1  reinoud 	int error;
    432  1.1  reinoud 
    433  1.1  reinoud 	/* get us a auixp_dma structure */
    434  1.1  reinoud 	dma = malloc(sizeof(*dma), type, flags);
    435  1.1  reinoud 	if (!dma) return NULL;
    436  1.1  reinoud 
    437  1.1  reinoud 	/* get us a dma buffer itself */
    438  1.1  reinoud 	error = auixp_allocmem(sc, size, 16, dma);
    439  1.1  reinoud 	if (error) {
    440  1.1  reinoud 		free(dma, type);
    441  1.1  reinoud 		printf("%s: auixp_malloc: not enough memory\n", sc->sc_dev.dv_xname);
    442  1.1  reinoud 
    443  1.1  reinoud 		return NULL;
    444  1.1  reinoud 	}
    445  1.1  reinoud 	SLIST_INSERT_HEAD(&sc->sc_dma_list, dma, dma_chain);
    446  1.1  reinoud 
    447  1.1  reinoud 	DPRINTF(("auixp_malloc: returning kern %p,   hw 0x%08x for %d bytes in %d segs\n",
    448  1.1  reinoud 				KERNADDR(dma), (uint32_t) DMAADDR(dma), dma->size, dma->nsegs)
    449  1.1  reinoud 	);
    450  1.1  reinoud 
    451  1.1  reinoud 	return (KERNADDR(dma));
    452  1.1  reinoud }
    453  1.1  reinoud 
    454  1.1  reinoud 
    455  1.1  reinoud /*
    456  1.1  reinoud  * free and release dma capable memory we allocated before and remove its
    457  1.1  reinoud  * recording
    458  1.1  reinoud  */
    459  1.1  reinoud void auixp_free(void *hdl, void *addr, struct malloc_type *type) {
    460  1.1  reinoud 	struct auixp_codec *co = (struct auixp_codec *) hdl;
    461  1.1  reinoud 	struct auixp_softc *sc = co->sc;
    462  1.1  reinoud 	struct auixp_dma *dma;
    463  1.1  reinoud 
    464  1.1  reinoud 	SLIST_FOREACH(dma, &sc->sc_dma_list, dma_chain) {
    465  1.1  reinoud 		if (KERNADDR(dma) == addr) {
    466  1.1  reinoud 			SLIST_REMOVE(&sc->sc_dma_list, dma, auixp_dma, dma_chain);
    467  1.1  reinoud 			free(dma, type);
    468  1.1  reinoud 			return;
    469  1.1  reinoud 		};
    470  1.1  reinoud 	};
    471  1.1  reinoud }
    472  1.1  reinoud 
    473  1.1  reinoud 
    474  1.1  reinoud int auixp_getdev(void *hdl, struct audio_device *ret) {
    475  1.1  reinoud 	*ret = auixp_device;
    476  1.1  reinoud 	return (0);
    477  1.1  reinoud }
    478  1.1  reinoud 
    479  1.1  reinoud 
    480  1.1  reinoud /* pass request to AC'97 codec code */
    481  1.1  reinoud int auixp_set_port(void *hdl, mixer_ctrl_t *mc) {
    482  1.1  reinoud 	struct auixp_codec *co = (struct auixp_codec *) hdl;
    483  1.1  reinoud 
    484  1.1  reinoud 	return co->codec_if->vtbl->mixer_set_port(co->codec_if, mc);
    485  1.1  reinoud }
    486  1.1  reinoud 
    487  1.1  reinoud 
    488  1.1  reinoud /* pass request to AC'97 codec code */
    489  1.1  reinoud int auixp_get_port(void *hdl, mixer_ctrl_t *mc) {
    490  1.1  reinoud 	struct auixp_codec *co = (struct auixp_codec *) hdl;
    491  1.1  reinoud 
    492  1.1  reinoud 	return co->codec_if->vtbl->mixer_get_port(co->codec_if, mc);
    493  1.1  reinoud }
    494  1.1  reinoud 
    495  1.1  reinoud /* pass request to AC'97 codec code */
    496  1.1  reinoud int auixp_query_devinfo(void *hdl, mixer_devinfo_t *di) {
    497  1.1  reinoud 	struct auixp_codec *co = (struct auixp_codec *) hdl;
    498  1.1  reinoud 
    499  1.1  reinoud 	return co->codec_if->vtbl->query_devinfo(co->codec_if, di);
    500  1.1  reinoud }
    501  1.1  reinoud 
    502  1.1  reinoud 
    503  1.1  reinoud size_t auixp_round_buffersize(void *hdl, int direction, size_t bufsize) {
    504  1.1  reinoud 	/* XXX force maximum? i.e. 256 kb? */
    505  1.1  reinoud 	return bufsize;
    506  1.1  reinoud }
    507  1.1  reinoud 
    508  1.1  reinoud 
    509  1.1  reinoud int auixp_get_props(void *hdl) {
    510  1.1  reinoud 	return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX;
    511  1.1  reinoud }
    512  1.1  reinoud 
    513  1.1  reinoud 
    514  1.1  reinoud /*
    515  1.1  reinoud  * A dma descriptor has dma->nsegs segments defined in dma->segs set up when
    516  1.1  reinoud  * we claimed the memory.
    517  1.1  reinoud  *
    518  1.1  reinoud  * Due to our demand for one contiguous DMA area, we only have one segment. A
    519  1.1  reinoud  * c_dma structure is about 3 kb for the 256 entries we maximally program
    520  1.1  reinoud  * -arbitrary limit AFAIK- so all is most likely to be in one segment/page
    521  1.1  reinoud  * anyway.
    522  1.1  reinoud  *
    523  1.1  reinoud  * XXX ought to implement fragmented dma area XXX
    524  1.1  reinoud  *
    525  1.1  reinoud  * Note that _v variables depict kernel virtual addresses, _p variables depict
    526  1.1  reinoud  * physical addresses.
    527  1.1  reinoud  */
    528  1.1  reinoud 
    529  1.1  reinoud void auixp_link_daisychain(struct auixp_softc *sc,
    530  1.1  reinoud 		struct auixp_dma *c_dma, struct auixp_dma *s_dma,
    531  1.1  reinoud 		int blksize, int blocks)
    532  1.1  reinoud {
    533  1.1  reinoud 	atiixp_dma_desc_t *caddr_v, *next_caddr_v;
    534  1.1  reinoud 	uint32_t caddr_p, next_caddr_p, saddr_p;
    535  1.1  reinoud 	int i;
    536  1.1  reinoud 
    537  1.1  reinoud 	/* just make sure we are not changing when its running */
    538  1.1  reinoud 	auixp_disable_dma(sc, c_dma);
    539  1.1  reinoud 
    540  1.1  reinoud 	/* setup dma chain start addresses */
    541  1.1  reinoud 	caddr_v = KERNADDR(c_dma);
    542  1.1  reinoud 	caddr_p = DMAADDR(c_dma);
    543  1.1  reinoud 	saddr_p = DMAADDR(s_dma);
    544  1.1  reinoud 
    545  1.1  reinoud 	/* program the requested number of blocks */
    546  1.1  reinoud 	for (i = 0; i < blocks; i++) {
    547  1.1  reinoud 		/* clear the block just in case */
    548  1.1  reinoud 		bzero(caddr_v, sizeof(atiixp_dma_desc_t));
    549  1.1  reinoud 
    550  1.1  reinoud 		/* round robin the chain dma addresses for its successor */
    551  1.1  reinoud 		next_caddr_v = caddr_v + 1;
    552  1.1  reinoud 		next_caddr_p = caddr_p + sizeof(atiixp_dma_desc_t);
    553  1.1  reinoud 
    554  1.1  reinoud 		if (i == blocks-1) {
    555  1.1  reinoud 			next_caddr_v = KERNADDR(c_dma);
    556  1.1  reinoud 			next_caddr_p = DMAADDR(c_dma);
    557  1.1  reinoud 		};
    558  1.1  reinoud 
    559  1.1  reinoud 		/* fill in the hardware dma chain descriptor in little-endian */
    560  1.1  reinoud 		caddr_v->addr   = htole32(saddr_p);
    561  1.1  reinoud 		caddr_v->status = htole16(0);
    562  1.1  reinoud 		caddr_v->size   = htole16((blksize >> 2));	/* in dwords (!!!) */
    563  1.1  reinoud 		caddr_v->next   = htole32(next_caddr_p);
    564  1.1  reinoud 
    565  1.1  reinoud 		/* advance slot */
    566  1.1  reinoud 		saddr_p += blksize;				/* XXX assuming contiguous XXX */
    567  1.1  reinoud 		caddr_v  = next_caddr_v;
    568  1.1  reinoud 		caddr_p  = next_caddr_p;
    569  1.1  reinoud 	};
    570  1.1  reinoud }
    571  1.1  reinoud 
    572  1.1  reinoud 
    573  1.1  reinoud int auixp_allocate_dma_chain(struct auixp_softc *sc, struct auixp_dma **dmap) {
    574  1.1  reinoud 	struct auixp_dma *dma;
    575  1.1  reinoud 	int error;
    576  1.1  reinoud 
    577  1.1  reinoud 	/* allocate keeper of dma area */
    578  1.1  reinoud 	*dmap = NULL;
    579  1.1  reinoud 	dma = malloc(sizeof(struct auixp_dma), M_DEVBUF, M_NOWAIT | M_ZERO);
    580  1.1  reinoud 	if (!dma) return ENOMEM;
    581  1.1  reinoud 
    582  1.1  reinoud 	/* allocate for daisychain of IXP hardware-dma descriptors */
    583  1.1  reinoud 	error = auixp_allocmem(sc, DMA_DESC_CHAIN * sizeof(atiixp_dma_desc_t), 16, dma);
    584  1.1  reinoud 	if (error) {
    585  1.1  reinoud 		printf("%s: can't malloc dma descriptor chain\n", sc->sc_dev.dv_xname);
    586  1.1  reinoud 		return ENOMEM;
    587  1.1  reinoud 	};
    588  1.1  reinoud 
    589  1.1  reinoud 	/* return info and initialise structure */
    590  1.1  reinoud 	dma->intr    = NULL;
    591  1.1  reinoud 	dma->intrarg = NULL;
    592  1.1  reinoud 
    593  1.1  reinoud 	*dmap = dma;
    594  1.1  reinoud 	return 0;
    595  1.1  reinoud }
    596  1.1  reinoud 
    597  1.1  reinoud 
    598  1.1  reinoud /* program dma chain in it's link address descriptor */
    599  1.1  reinoud void auixp_program_dma_chain(struct auixp_softc *sc, struct auixp_dma *dma) {
    600  1.1  reinoud 	bus_space_tag_t    iot = sc->sc_iot;
    601  1.1  reinoud 	bus_space_handle_t ioh = sc->sc_ioh;
    602  1.1  reinoud 	uint32_t value;
    603  1.1  reinoud 
    604  1.1  reinoud 	/* get hardware start address of DMA chain and set valid-flag in it */
    605  1.1  reinoud 	/* XXX allways at start? XXX */
    606  1.1  reinoud 	value = DMAADDR(dma);
    607  1.1  reinoud 	value = value | ATI_REG_LINKPTR_EN;
    608  1.1  reinoud 
    609  1.1  reinoud 	/* reset linkpointer */
    610  1.1  reinoud 	bus_space_write_4(iot, ioh, dma->linkptr, 0);
    611  1.1  reinoud 
    612  1.1  reinoud 	/* reset this DMA engine */
    613  1.1  reinoud 	auixp_disable_dma(sc, dma);
    614  1.1  reinoud 	auixp_enable_dma(sc, dma);
    615  1.1  reinoud 
    616  1.1  reinoud 	/* program new DMA linkpointer */
    617  1.1  reinoud 	bus_space_write_4(iot, ioh, dma->linkptr, value);
    618  1.1  reinoud }
    619  1.1  reinoud 
    620  1.1  reinoud 
    621  1.1  reinoud /* called from interrupt code to signal end of one dma-slot */
    622  1.1  reinoud void auixp_dma_update(struct auixp_softc *sc, struct auixp_dma *dma) {
    623  1.1  reinoud 	/* be very paranoid */
    624  1.1  reinoud 	if (!dma) panic("auixp: update: dma = NULL");
    625  1.1  reinoud 	if (!dma->intr) panic("auixp: update: dma->intr = NULL");
    626  1.1  reinoud 
    627  1.1  reinoud 	/* request more input from upper layer */
    628  1.1  reinoud 	(*dma->intr)(dma->intrarg);
    629  1.1  reinoud }
    630  1.1  reinoud 
    631  1.1  reinoud 
    632  1.1  reinoud /*
    633  1.1  reinoud  * The magic `busbusy' bit that needs to be set when dma is active; allowing
    634  1.1  reinoud  * busmastering?
    635  1.1  reinoud  */
    636  1.1  reinoud void auixp_update_busbusy(struct auixp_softc *sc) {
    637  1.1  reinoud 	bus_space_tag_t    iot = sc->sc_iot;
    638  1.1  reinoud 	bus_space_handle_t ioh = sc->sc_ioh;
    639  1.1  reinoud 	uint32_t value;
    640  1.1  reinoud 	int running;
    641  1.1  reinoud 
    642  1.1  reinoud 	/* set bus-busy flag when either recording or playing is performed */
    643  1.1  reinoud 	value  = bus_space_read_4(iot, ioh, ATI_REG_IER);
    644  1.1  reinoud 	value &= ~ATI_REG_IER_SET_BUS_BUSY;
    645  1.1  reinoud 
    646  1.1  reinoud 	running = ((sc->sc_output_dma->running) || (sc->sc_input_dma->running));
    647  1.1  reinoud 	if (running) value |= ATI_REG_IER_SET_BUS_BUSY;
    648  1.1  reinoud 
    649  1.1  reinoud 	bus_space_write_4(iot, ioh, ATI_REG_IER, value);
    650  1.1  reinoud 
    651  1.1  reinoud }
    652  1.1  reinoud 
    653  1.1  reinoud 
    654  1.1  reinoud /*
    655  1.1  reinoud  * Called from upper audio layer to request playing audio, only called once;
    656  1.1  reinoud  * audio is refilled by calling the intr() function when space is available
    657  1.1  reinoud  * again.
    658  1.1  reinoud  */
    659  1.1  reinoud /* XXX allmost literaly a copy of trigger-input; could be factorised XXX */
    660  1.1  reinoud int auixp_trigger_output(void *hdl, void *start, void *end, int blksize,
    661  1.1  reinoud 		void (*intr)(void *), void *intrarg, const audio_params_t *param) {
    662  1.1  reinoud 	struct auixp_codec *co  = (struct auixp_codec *) hdl;
    663  1.1  reinoud 	struct auixp_softc *sc  = co->sc;
    664  1.1  reinoud 	struct auixp_dma   *chain_dma = sc->sc_output_dma;
    665  1.1  reinoud 	struct auixp_dma   *sound_dma;
    666  1.1  reinoud 	uint32_t blocks;
    667  1.1  reinoud 
    668  1.1  reinoud 	/* add functions to call back */
    669  1.1  reinoud 	chain_dma->intr    = intr;
    670  1.1  reinoud 	chain_dma->intrarg = intrarg;
    671  1.1  reinoud 
    672  1.1  reinoud 	/*
    673  1.1  reinoud 	 * Program output DMA chain with blocks from [start...end] with
    674  1.1  reinoud 	 * blksize fragments.
    675  1.1  reinoud 	 *
    676  1.1  reinoud 	 * NOTE, we can assume its in one block since we asked for it to be in
    677  1.1  reinoud 	 * one contiguous blob; XXX change this? XXX
    678  1.1  reinoud 	 */
    679  1.1  reinoud 	blocks = (size_t) (((caddr_t) end) - ((caddr_t) start)) / blksize;
    680  1.1  reinoud 
    681  1.1  reinoud 	/* lookup `start' address in our list of DMA area's */
    682  1.1  reinoud 	SLIST_FOREACH(sound_dma, &sc->sc_dma_list, dma_chain) {
    683  1.1  reinoud 		if (KERNADDR(sound_dma) == start) break;
    684  1.1  reinoud 	};
    685  1.1  reinoud 
    686  1.1  reinoud 	/* not ours ? then bail out */
    687  1.1  reinoud 	if (!sound_dma) {
    688  1.1  reinoud 		printf("auixp_trigger_output: bad sound addr %p\n", start);
    689  1.1  reinoud 		return EINVAL;
    690  1.1  reinoud 	}
    691  1.1  reinoud 
    692  1.1  reinoud 	/* link round-robin daisychain and program hardware */
    693  1.1  reinoud 	auixp_link_daisychain(sc, chain_dma, sound_dma, blksize, blocks);
    694  1.1  reinoud 	auixp_program_dma_chain(sc, chain_dma);
    695  1.1  reinoud 
    696  1.1  reinoud 	/* mark we are now able to run now */
    697  1.1  reinoud 	chain_dma->running = 1;
    698  1.1  reinoud 
    699  1.1  reinoud 	/* update bus-flags; XXX programs more flags XXX */
    700  1.1  reinoud 	auixp_update_busbusy(sc);
    701  1.1  reinoud 
    702  1.1  reinoud 	/* callbacks happen in interrupt routine */
    703  1.1  reinoud 	return 0;
    704  1.1  reinoud }
    705  1.1  reinoud 
    706  1.1  reinoud 
    707  1.1  reinoud /* halt output of audio, just disable it's dma and update bus state */
    708  1.1  reinoud int auixp_halt_output(void *hdl) {
    709  1.1  reinoud 	struct auixp_codec *co  = (struct auixp_codec *) hdl;
    710  1.1  reinoud 	struct auixp_softc *sc  = co->sc;
    711  1.1  reinoud 	struct auixp_dma   *dma = sc->sc_output_dma;
    712  1.1  reinoud 
    713  1.1  reinoud 	auixp_disable_dma(sc, dma);
    714  1.1  reinoud 
    715  1.1  reinoud 	dma->running = 0;
    716  1.1  reinoud 	auixp_update_busbusy(sc);
    717  1.1  reinoud 
    718  1.1  reinoud 	return 0;
    719  1.1  reinoud }
    720  1.1  reinoud 
    721  1.1  reinoud 
    722  1.1  reinoud /* XXX allmost literaly a copy of trigger-output; could be factorised XXX */
    723  1.1  reinoud int auixp_trigger_input(void *hdl, void *start, void *end, int blksize,
    724  1.1  reinoud 		void (*intr)(void *), void *intrarg, const audio_params_t *param) {
    725  1.1  reinoud 	struct auixp_codec *co  = (struct auixp_codec *) hdl;
    726  1.1  reinoud 	struct auixp_softc *sc  = co->sc;
    727  1.1  reinoud 	struct auixp_dma   *chain_dma = sc->sc_input_dma;
    728  1.1  reinoud 	struct auixp_dma   *sound_dma;
    729  1.1  reinoud 	uint32_t blocks;
    730  1.1  reinoud 
    731  1.1  reinoud 	/* add functions to call back */
    732  1.1  reinoud 	chain_dma->intr    = intr;
    733  1.1  reinoud 	chain_dma->intrarg = intrarg;
    734  1.1  reinoud 
    735  1.1  reinoud 	/*
    736  1.1  reinoud 	 * Program output DMA chain with blocks from [start...end] with
    737  1.1  reinoud 	 * blksize fragments.
    738  1.1  reinoud 	 *
    739  1.1  reinoud 	 * NOTE, we can assume its in one block since we asked for it to be in
    740  1.1  reinoud 	 * one contiguous blob; XXX change this? XXX
    741  1.1  reinoud 	 */
    742  1.1  reinoud 	blocks = (size_t) (((caddr_t) end) - ((caddr_t) start)) / blksize;
    743  1.1  reinoud 
    744  1.1  reinoud 	/* lookup `start' address in our list of DMA area's */
    745  1.1  reinoud 	SLIST_FOREACH(sound_dma, &sc->sc_dma_list, dma_chain) {
    746  1.1  reinoud 		if (KERNADDR(sound_dma) == start) break;
    747  1.1  reinoud 	};
    748  1.1  reinoud 
    749  1.1  reinoud 	/* not ours ? then bail out */
    750  1.1  reinoud 	if (!sound_dma) {
    751  1.1  reinoud 		printf("auixp_trigger_input: bad sound addr %p\n", start);
    752  1.1  reinoud 		return EINVAL;
    753  1.1  reinoud 	}
    754  1.1  reinoud 
    755  1.1  reinoud 	/* link round-robin daisychain and program hardware */
    756  1.1  reinoud 	auixp_link_daisychain(sc, chain_dma, sound_dma, blksize, blocks);
    757  1.1  reinoud 	auixp_program_dma_chain(sc, chain_dma);
    758  1.1  reinoud 
    759  1.1  reinoud 	/* mark we are now able to run now */
    760  1.1  reinoud 	chain_dma->running = 1;
    761  1.1  reinoud 
    762  1.1  reinoud 	/* update bus-flags; XXX programs more flags XXX */
    763  1.1  reinoud 	auixp_update_busbusy(sc);
    764  1.1  reinoud 
    765  1.1  reinoud 	/* callbacks happen in interrupt routine */
    766  1.1  reinoud 	return 0;
    767  1.1  reinoud }
    768  1.1  reinoud 
    769  1.1  reinoud 
    770  1.1  reinoud /* halt sampling audio, just disable it's dma and update bus state */
    771  1.1  reinoud int auixp_halt_input(void *hdl) {
    772  1.1  reinoud 	struct auixp_codec *co  = (struct auixp_codec *) hdl;
    773  1.1  reinoud 	struct auixp_softc *sc  = co->sc;
    774  1.1  reinoud 	struct auixp_dma   *dma = sc->sc_output_dma;
    775  1.1  reinoud 
    776  1.1  reinoud 	auixp_disable_dma(sc, dma);
    777  1.1  reinoud 
    778  1.1  reinoud 	dma->running = 0;
    779  1.1  reinoud 	auixp_update_busbusy(sc);
    780  1.1  reinoud 
    781  1.1  reinoud 	return 0;
    782  1.1  reinoud }
    783  1.1  reinoud 
    784  1.1  reinoud 
    785  1.1  reinoud /*
    786  1.1  reinoud  * IXP audio interrupt handler
    787  1.1  reinoud  *
    788  1.1  reinoud  * note that we return the number of bits handled; the return value is not
    789  1.1  reinoud  * documentated but i saw it implemented in other drivers. Prolly returning a
    790  1.1  reinoud  * value > 0 means "i've dealt with it"
    791  1.1  reinoud  *
    792  1.1  reinoud  */
    793  1.1  reinoud int auixp_intr(void *softc) {
    794  1.1  reinoud 	struct auixp_softc *sc = softc;
    795  1.1  reinoud 	bus_space_tag_t    iot = sc->sc_iot;
    796  1.1  reinoud 	bus_space_handle_t ioh = sc->sc_ioh;
    797  1.1  reinoud 	uint32_t status, enable, detected_codecs;
    798  1.1  reinoud 	int ret = 0;
    799  1.1  reinoud 
    800  1.1  reinoud 	/* get status from the interrupt status register */
    801  1.1  reinoud 	status = bus_space_read_4(iot, ioh, ATI_REG_ISR);
    802  1.1  reinoud 
    803  1.1  reinoud 	/* nothing set?? but why do you call then??? */
    804  1.1  reinoud 	if (status == 0) {
    805  1.1  reinoud 		printf("%s: spurious hardware interrupt? status = 0\n", sc->sc_dev.dv_xname);
    806  1.1  reinoud 		return 1;	/* be safe */
    807  1.1  reinoud 	};
    808  1.1  reinoud 
    809  1.1  reinoud 	DPRINTF(("auixp: (status = %x)\n", status));
    810  1.1  reinoud 
    811  1.1  reinoud 	/* check DMA UPDATE flags for input & output */
    812  1.1  reinoud 	if (status & ATI_REG_ISR_IN_STATUS) {
    813  1.1  reinoud 		ret++; DPRINTF(("IN_STATUS\n"));
    814  1.1  reinoud 		auixp_dma_update(sc, sc->sc_input_dma);
    815  1.1  reinoud 	};
    816  1.1  reinoud 	if (status & ATI_REG_ISR_OUT_STATUS) {
    817  1.1  reinoud 		ret++; DPRINTF(("OUT_STATUS\n"));
    818  1.1  reinoud 		auixp_dma_update(sc, sc->sc_output_dma);
    819  1.1  reinoud 	};
    820  1.1  reinoud 
    821  1.1  reinoud 	/* XXX XRUN flags not used/needed yet; should i implement it? XXX */
    822  1.1  reinoud 	/* acknowledge the interrupts nevertheless */
    823  1.1  reinoud 	if (status & ATI_REG_ISR_IN_XRUN) {
    824  1.1  reinoud 		ret++; DPRINTF(("IN_XRUN\n"));
    825  1.1  reinoud 		/* auixp_dma_xrun(sc, sc->sc_input_dma);  */
    826  1.1  reinoud 	};
    827  1.1  reinoud 	if (status & ATI_REG_ISR_OUT_XRUN) {
    828  1.1  reinoud 		ret++; DPRINTF(("OUT_XRUN\n"));
    829  1.1  reinoud 		/* auixp_dma_xrun(sc, sc->sc_output_dma); */
    830  1.1  reinoud 	};
    831  1.1  reinoud 
    832  1.1  reinoud 	/* check if we are looking for codec detection */
    833  1.1  reinoud 	if (status & CODEC_CHECK_BITS) {
    834  1.1  reinoud 		ret++;
    835  1.1  reinoud 		/* mark missing codecs as not ready */
    836  1.1  reinoud 		detected_codecs = status & CODEC_CHECK_BITS;
    837  1.1  reinoud 		sc->sc_codec_not_ready_bits |= detected_codecs;
    838  1.1  reinoud 
    839  1.1  reinoud 		/* disable detected interupt sources */
    840  1.1  reinoud 		enable  = bus_space_read_4(iot, ioh, ATI_REG_IER);
    841  1.1  reinoud 		enable &= ~detected_codecs;
    842  1.1  reinoud 		bus_space_write_4(iot, ioh, ATI_REG_IER, enable);
    843  1.1  reinoud 	};
    844  1.1  reinoud 
    845  1.1  reinoud 	/* acknowledge interrupt sources */
    846  1.1  reinoud 	bus_space_write_4(iot, ioh, ATI_REG_ISR, status);
    847  1.1  reinoud 
    848  1.1  reinoud 	return ret;
    849  1.1  reinoud }
    850  1.1  reinoud 
    851  1.1  reinoud 
    852  1.1  reinoud /* allocate memory for dma purposes; on failure of any of the steps, roll back */
    853  1.1  reinoud int auixp_allocmem(struct auixp_softc *sc, size_t size, size_t align, struct auixp_dma *dma) {
    854  1.1  reinoud 	int error;
    855  1.1  reinoud 
    856  1.1  reinoud 	/* remember size */
    857  1.1  reinoud 	dma->size = size;
    858  1.1  reinoud 
    859  1.1  reinoud 	/* allocate DMA safe memory but in just one segment for now :( */
    860  1.1  reinoud 	error = bus_dmamem_alloc(sc->sc_dmat, dma->size, align, 0,
    861  1.1  reinoud 				 dma->segs, sizeof(dma->segs) / sizeof(dma->segs[0]),
    862  1.1  reinoud 				 &dma->nsegs, BUS_DMA_NOWAIT);
    863  1.1  reinoud 	if (error) return (error);
    864  1.1  reinoud 
    865  1.1  reinoud 	/*
    866  1.1  reinoud 	 * map allocated memory into kernel virtual address space and keep it
    867  1.1  reinoud 	 * coherent with the CPU.
    868  1.1  reinoud 	 */
    869  1.1  reinoud 	error = bus_dmamem_map(sc->sc_dmat, dma->segs, dma->nsegs, dma->size,
    870  1.1  reinoud 				&dma->addr, BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
    871  1.1  reinoud 	if (error) goto free;
    872  1.1  reinoud 
    873  1.1  reinoud 	/* allocate associated dma handle and initialize it. */
    874  1.1  reinoud 	error = bus_dmamap_create(sc->sc_dmat, dma->size, 1, dma->size, 0,
    875  1.1  reinoud 				  BUS_DMA_NOWAIT, &dma->map);
    876  1.1  reinoud 	if (error) goto unmap;
    877  1.1  reinoud 
    878  1.1  reinoud 	/*
    879  1.1  reinoud 	 * load the dma handle with mappings for a dma transfer; all pages
    880  1.1  reinoud 	 * need to be wired.
    881  1.1  reinoud 	 */
    882  1.1  reinoud 	error = bus_dmamap_load(sc->sc_dmat, dma->map, dma->addr, dma->size, NULL,
    883  1.1  reinoud 				BUS_DMA_NOWAIT);
    884  1.1  reinoud 	if (error) goto destroy;
    885  1.1  reinoud 
    886  1.1  reinoud 	return (0);
    887  1.1  reinoud 
    888  1.1  reinoud destroy:
    889  1.1  reinoud 	bus_dmamap_destroy(sc->sc_dmat, dma->map);
    890  1.1  reinoud unmap:
    891  1.1  reinoud 	bus_dmamem_unmap(sc->sc_dmat, dma->addr, dma->size);
    892  1.1  reinoud free:
    893  1.1  reinoud 	bus_dmamem_free(sc->sc_dmat, dma->segs, dma->nsegs);
    894  1.1  reinoud 
    895  1.1  reinoud 	return (error);
    896  1.1  reinoud }
    897  1.1  reinoud 
    898  1.1  reinoud 
    899  1.1  reinoud /* undo dma mapping and release memory allocated */
    900  1.1  reinoud int auixp_freemem(struct auixp_softc *sc, struct auixp_dma *p) {
    901  1.1  reinoud 
    902  1.1  reinoud 	bus_dmamap_unload(sc->sc_dmat, p->map);
    903  1.1  reinoud 	bus_dmamap_destroy(sc->sc_dmat, p->map);
    904  1.1  reinoud 	bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
    905  1.1  reinoud 	bus_dmamem_free(sc->sc_dmat, p->segs, p->nsegs);
    906  1.1  reinoud 
    907  1.1  reinoud 	return (0);
    908  1.1  reinoud }
    909  1.1  reinoud 
    910  1.1  reinoud 
    911  1.1  reinoud /* memory map dma memory */
    912  1.1  reinoud paddr_t auixp_mappage(void *hdl, void *mem, off_t off, int prot) {
    913  1.1  reinoud 	struct auixp_codec *co = (struct auixp_codec *) hdl;
    914  1.1  reinoud 	struct auixp_softc *sc  = co->sc;
    915  1.1  reinoud 	struct auixp_dma *p;
    916  1.1  reinoud 
    917  1.1  reinoud 	/* for sanity */
    918  1.1  reinoud 	if (off < 0) return (-1);
    919  1.1  reinoud 
    920  1.1  reinoud 	/* look up allocated DMA area */
    921  1.1  reinoud 	SLIST_FOREACH(p, &sc->sc_dma_list, dma_chain) {
    922  1.1  reinoud 		if (KERNADDR(p) == mem) break;
    923  1.1  reinoud 	};
    924  1.1  reinoud 
    925  1.1  reinoud 	/* have we found it ? */
    926  1.1  reinoud 	if (!p) return (-1);
    927  1.1  reinoud 
    928  1.1  reinoud 	/* return mmap'd region */
    929  1.1  reinoud 	return (bus_dmamem_mmap(sc->sc_dmat, p->segs, p->nsegs,
    930  1.1  reinoud 				off, prot, BUS_DMA_WAITOK));
    931  1.1  reinoud }
    932  1.1  reinoud 
    933  1.1  reinoud 
    934  1.1  reinoud /*
    935  1.1  reinoud  * Attachment section
    936  1.1  reinoud  */
    937  1.1  reinoud 
    938  1.1  reinoud /* Is it my hardware? */
    939  1.1  reinoud int auixp_match(struct device *dev, struct cfdata *match, void *aux) {
    940  1.1  reinoud 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    941  1.1  reinoud 
    942  1.1  reinoud 	switch(PCI_VENDOR(pa->pa_id)) {
    943  1.1  reinoud 	case PCI_VENDOR_ATI:
    944  1.1  reinoud 		switch(PCI_PRODUCT(pa->pa_id)) {
    945  1.1  reinoud 		case PCI_PRODUCT_ATI_IXP_AUDIO_200:
    946  1.1  reinoud 		case PCI_PRODUCT_ATI_IXP_AUDIO_300:
    947  1.1  reinoud 		case PCI_PRODUCT_ATI_IXP_AUDIO_400:
    948  1.1  reinoud 			return (1);
    949  1.1  reinoud 		}
    950  1.1  reinoud 	}
    951  1.1  reinoud 
    952  1.1  reinoud 	return (0);
    953  1.1  reinoud }
    954  1.1  reinoud 
    955  1.1  reinoud 
    956  1.1  reinoud /* it is... now hook up and set up the resources we need */
    957  1.1  reinoud void auixp_attach(struct device *parent, struct device *self, void *aux) {
    958  1.1  reinoud 	struct auixp_softc *sc = (struct auixp_softc *)self;
    959  1.1  reinoud 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    960  1.1  reinoud 	pcitag_t tag = pa->pa_tag;
    961  1.1  reinoud 	pci_chipset_tag_t pc = pa->pa_pc;
    962  1.1  reinoud 	pci_intr_handle_t ih;
    963  1.1  reinoud 	struct auixp_card_type *card;
    964  1.1  reinoud 	const char *intrstr;
    965  1.1  reinoud 	uint32_t data;
    966  1.1  reinoud 	char devinfo[256];
    967  1.1  reinoud 	int revision, len;
    968  1.1  reinoud 
    969  1.1  reinoud #ifdef DEBUG_AUIXP
    970  1.1  reinoud static_sc = sc;
    971  1.1  reinoud #endif
    972  1.1  reinoud 
    973  1.1  reinoud 	/* print information confirming attachment */
    974  1.1  reinoud 	aprint_naive(": Audio controller\n");
    975  1.1  reinoud 
    976  1.1  reinoud 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    977  1.1  reinoud 	revision = PCI_REVISION(pa->pa_class);
    978  1.1  reinoud 	aprint_normal(": %s (rev. 0x%02x)\n", devinfo, revision);
    979  1.1  reinoud 
    980  1.1  reinoud 	/* set up details from our set of known `cards'/chips */
    981  1.1  reinoud 	for (card = auixp_card_types; card->pci_vendor_id; card++)
    982  1.1  reinoud 		if (PCI_VENDOR(pa->pa_id) == card->pci_vendor_id &&
    983  1.1  reinoud 		    PCI_PRODUCT(pa->pa_id) == card->pci_product_id) {
    984  1.1  reinoud 			sc->type = card->type;
    985  1.1  reinoud 			break;
    986  1.1  reinoud 		}
    987  1.1  reinoud 
    988  1.1  reinoud 	/* device only has 32 bit non prefetchable memory		*/
    989  1.1  reinoud 	/* set MEM space access and enable the card's busmastering	*/
    990  1.1  reinoud 	data = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    991  1.1  reinoud 	data |= (PCI_COMMAND_MEM_ENABLE | PCI_COMMAND_MASTER_ENABLE);
    992  1.1  reinoud 	pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, data);
    993  1.1  reinoud 
    994  1.1  reinoud 	/* map memory; its not sized -> what is the size? max PCI slot size? */
    995  1.1  reinoud 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_MEM, 0,
    996  1.1  reinoud 	    &sc->sc_iot, &sc->sc_ioh, &sc->sc_iob, &sc->sc_ios)) {
    997  1.1  reinoud 		aprint_error("%s: can't map memory space\n", sc->sc_dev.dv_xname);
    998  1.1  reinoud 		return;
    999  1.1  reinoud 	}
   1000  1.1  reinoud 
   1001  1.1  reinoud 	/* Initialize softc */
   1002  1.1  reinoud 	sc->sc_tag = tag;
   1003  1.1  reinoud 	sc->sc_pct = pc;
   1004  1.1  reinoud 	sc->sc_dmat = pa->pa_dmat;
   1005  1.1  reinoud 	SLIST_INIT(&sc->sc_dma_list);
   1006  1.1  reinoud 
   1007  1.1  reinoud 	/* get us the auixp_dma structures */
   1008  1.1  reinoud 	auixp_allocate_dma_chain(sc, &sc->sc_output_dma);
   1009  1.1  reinoud 	auixp_allocate_dma_chain(sc, &sc->sc_input_dma);
   1010  1.1  reinoud 
   1011  1.1  reinoud 	/* when that fails we are dead in the water */
   1012  1.1  reinoud 	if (!sc->sc_output_dma || !sc->sc_input_dma) return;
   1013  1.1  reinoud 
   1014  1.1  reinoud 	/* fill in the missing details about the dma channels. */
   1015  1.1  reinoud 
   1016  1.1  reinoud 	/* for output */
   1017  1.1  reinoud 	sc->sc_output_dma->linkptr        = ATI_REG_OUT_DMA_LINKPTR;
   1018  1.1  reinoud 	sc->sc_output_dma->dma_enable_bit = ATI_REG_CMD_OUT_DMA_EN |
   1019  1.1  reinoud 					    ATI_REG_CMD_SEND_EN;
   1020  1.1  reinoud 	/* have spdif? then this too! XXX not seeing LED yet! XXX */
   1021  1.1  reinoud 	if (sc->has_spdif)
   1022  1.1  reinoud 		sc->sc_output_dma->dma_enable_bit |= ATI_REG_CMD_SPDF_OUT_EN;
   1023  1.1  reinoud 
   1024  1.1  reinoud 	/* and for input */
   1025  1.1  reinoud 	sc->sc_input_dma->linkptr         = ATI_REG_IN_DMA_LINKPTR;
   1026  1.1  reinoud 	sc->sc_input_dma->dma_enable_bit  = ATI_REG_CMD_IN_DMA_EN  |
   1027  1.1  reinoud 					    ATI_REG_CMD_RECEIVE_EN;
   1028  1.1  reinoud 
   1029  1.1  reinoud #if 0
   1030  1.1  reinoud 	/* could preliminary program DMA chain */
   1031  1.1  reinoud 	auixp_program_dma_chain(sc, sc->sc_output_dma);
   1032  1.1  reinoud 	auixp_program_dma_chain(sc, sc->sc_input_dma);
   1033  1.1  reinoud #endif
   1034  1.1  reinoud 
   1035  1.1  reinoud 	/* map interrupt on the pci bus */
   1036  1.1  reinoud 	if (pci_intr_map(pa, &ih)) {
   1037  1.1  reinoud 		aprint_error("%s: can't map interrupt\n", sc->sc_dev.dv_xname);
   1038  1.1  reinoud 		return;
   1039  1.1  reinoud 	}
   1040  1.1  reinoud 
   1041  1.1  reinoud 	/* where are we connected at ? */
   1042  1.1  reinoud 	intrstr = pci_intr_string(pc, ih);
   1043  1.1  reinoud 
   1044  1.1  reinoud 	/* establish interrupt routine hookup at IPL_AUDIO level */
   1045  1.1  reinoud 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, auixp_intr, self);
   1046  1.1  reinoud 	if (sc->sc_ih == NULL) {
   1047  1.1  reinoud 		aprint_error("%s: can't establish interrupt",
   1048  1.1  reinoud 		    sc->sc_dev.dv_xname);
   1049  1.1  reinoud 		if (intrstr != NULL)
   1050  1.1  reinoud 			aprint_normal(" at %s", intrstr);
   1051  1.1  reinoud 		aprint_normal("\n");
   1052  1.1  reinoud 		return;
   1053  1.1  reinoud 	}
   1054  1.1  reinoud 	aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
   1055  1.1  reinoud 
   1056  1.1  reinoud 	/* power up chip */
   1057  1.1  reinoud 	auixp_power(sc, PCI_PMCSR_STATE_D0);
   1058  1.1  reinoud 
   1059  1.1  reinoud 	/* init chip */
   1060  1.1  reinoud 	if (auixp_init(sc) == -1) {
   1061  1.1  reinoud 		aprint_error("%s: auixp_attach: unable to initialize the card\n",
   1062  1.1  reinoud 		    sc->sc_dev.dv_xname);
   1063  1.1  reinoud 		return;
   1064  1.1  reinoud 	}
   1065  1.1  reinoud 
   1066  1.1  reinoud 	/* XXX set up power hooks; not implemented yet XXX */
   1067  1.1  reinoud 
   1068  1.1  reinoud 	len=1;	/* shut up gcc */
   1069  1.1  reinoud #ifdef notyet
   1070  1.1  reinoud 	/* create suspend save area */
   1071  1.1  reinoud 	len = sizeof(uint16_t) * (ESA_REV_B_CODE_MEMORY_LENGTH
   1072  1.1  reinoud 	    + ESA_REV_B_DATA_MEMORY_LENGTH + 1);
   1073  1.1  reinoud 	sc->savemem = (uint16_t *)malloc(len, M_DEVBUF, M_NOWAIT | M_ZERO);
   1074  1.1  reinoud 	if (sc->savemem == NULL) {
   1075  1.1  reinoud 		aprint_error("%s: unable to allocate suspend buffer\n",
   1076  1.1  reinoud 		    sc->sc_dev.dv_xname);
   1077  1.1  reinoud 		return;
   1078  1.1  reinoud 	}
   1079  1.1  reinoud 
   1080  1.1  reinoud 	sc->powerhook = powerhook_establish(auixp_powerhook, sc);
   1081  1.1  reinoud 	if (sc->powerhook == NULL)
   1082  1.1  reinoud 		aprint_error("%s: WARNING: unable to establish powerhook\n",
   1083  1.1  reinoud 		    sc->sc_dev.dv_xname);
   1084  1.1  reinoud 
   1085  1.1  reinoud #endif
   1086  1.1  reinoud 
   1087  1.1  reinoud 	/*
   1088  1.1  reinoud 	 * delay further configuration of codecs and audio after interrupts
   1089  1.1  reinoud 	 * are enabled.
   1090  1.1  reinoud 	 */
   1091  1.1  reinoud 	config_interrupts(self, auixp_post_config);
   1092  1.1  reinoud }
   1093  1.1  reinoud 
   1094  1.1  reinoud 
   1095  1.1  reinoud /* called from autoconfigure system when interrupts are enabled */
   1096  1.1  reinoud void auixp_post_config(struct device *self) {
   1097  1.1  reinoud 	struct auixp_softc *sc = (struct auixp_softc *)self;
   1098  1.1  reinoud 	struct auixp_codec *codec;
   1099  1.1  reinoud 	int codec_nr;
   1100  1.1  reinoud 	int res, i;
   1101  1.1  reinoud 
   1102  1.1  reinoud 	/* detect the AC97 codecs */
   1103  1.1  reinoud 	auixp_autodetect_codecs(sc);
   1104  1.1  reinoud 
   1105  1.1  reinoud 	/* setup audio translation formats : following codec0 (!) */
   1106  1.1  reinoud 	codec = &sc->sc_codec[0];
   1107  1.1  reinoud 	if (!codec->present) {
   1108  1.1  reinoud 		/* nothing??? then invalidate all formats */
   1109  1.1  reinoud 		for (i = 0; i < AUIXP_NFORMATS; i++) {
   1110  1.1  reinoud 			AUFMT_INVALIDATE(&sc->sc_formats[i]);
   1111  1.1  reinoud 		};
   1112  1.1  reinoud 		return;
   1113  1.1  reinoud 	};
   1114  1.1  reinoud 
   1115  1.1  reinoud 	/* copy formats and invalidate entries not suitable for codec0 */
   1116  1.1  reinoud 	memcpy(sc->sc_formats, auixp_formats, sizeof(auixp_formats));
   1117  1.1  reinoud 	sc->has_4ch   = AC97_IS_4CH(codec->codec_if);
   1118  1.1  reinoud 	sc->has_6ch   = AC97_IS_6CH(codec->codec_if);
   1119  1.1  reinoud 	sc->is_fixed  = AC97_IS_FIXED_RATE(codec->codec_if);
   1120  1.1  reinoud 	sc->has_spdif = AC97_HAS_SPDIF(codec->codec_if);
   1121  1.1  reinoud 
   1122  1.1  reinoud 	for (i = 0; i < AUIXP_NFORMATS; i++) {
   1123  1.1  reinoud 		if (sc->is_fixed) {
   1124  1.1  reinoud 			sc->sc_formats[i].frequency_type = 1;
   1125  1.1  reinoud 			sc->sc_formats[i].frequency[0]   = 48000;
   1126  1.1  reinoud 		};
   1127  1.1  reinoud 		switch (sc->sc_formats[i].channels) {
   1128  1.1  reinoud 		case 4 :
   1129  1.1  reinoud 			if (sc->has_4ch) break;
   1130  1.1  reinoud 			AUFMT_INVALIDATE(&sc->sc_formats[i]);
   1131  1.1  reinoud 			break;
   1132  1.1  reinoud 		case 6 :
   1133  1.1  reinoud 			if (sc->has_6ch) break;
   1134  1.1  reinoud 			AUFMT_INVALIDATE(&sc->sc_formats[i]);
   1135  1.1  reinoud 			break;
   1136  1.1  reinoud 		default :
   1137  1.1  reinoud 			break;
   1138  1.1  reinoud 		};
   1139  1.1  reinoud 	}
   1140  1.1  reinoud 
   1141  1.1  reinoud 	/* create all encodings (and/or -translations) based on the formats supported */
   1142  1.1  reinoud 	res = auconv_create_encodings(sc->sc_formats, AUIXP_NFORMATS, &sc->sc_encodings);
   1143  1.1  reinoud 	if (res) {
   1144  1.1  reinoud 		printf("auixp: auconv_create_encodings failed; no attachments\n");
   1145  1.1  reinoud 		return;
   1146  1.1  reinoud 	};
   1147  1.1  reinoud 
   1148  1.1  reinoud 	/* attach audio devices for all detected codecs */
   1149  1.1  reinoud 	/* XXX wise? look at other multiple-codec able chipsets XXX */
   1150  1.1  reinoud 	for (codec_nr = 0; codec_nr < ATI_IXP_CODECS; codec_nr++) {
   1151  1.1  reinoud 		codec = &sc->sc_codec[codec_nr];
   1152  1.1  reinoud 		if (codec->present)
   1153  1.1  reinoud 			audio_attach_mi(&auixp_hw_if, codec, &sc->sc_dev);
   1154  1.1  reinoud 	};
   1155  1.1  reinoud 
   1156  1.1  reinoud 	/* done! now enable all interrupts we can service */
   1157  1.1  reinoud 	auixp_enable_interrupts(sc);
   1158  1.1  reinoud };
   1159  1.1  reinoud 
   1160  1.1  reinoud 
   1161  1.1  reinoud void auixp_enable_interrupts(struct auixp_softc *sc) {
   1162  1.1  reinoud 	bus_space_tag_t     iot = sc->sc_iot;
   1163  1.1  reinoud 	bus_space_handle_t  ioh = sc->sc_ioh;
   1164  1.1  reinoud 	uint32_t value;
   1165  1.1  reinoud 
   1166  1.1  reinoud 	/* clear all pending */
   1167  1.1  reinoud 	bus_space_write_4(iot, ioh, ATI_REG_ISR, 0xffffffff);
   1168  1.1  reinoud 
   1169  1.1  reinoud 	/* enable all relevant interrupt sources we can handle */
   1170  1.1  reinoud 	value = bus_space_read_4(iot, ioh, ATI_REG_IER);
   1171  1.1  reinoud 
   1172  1.1  reinoud 	value |= ATI_REG_IER_IO_STATUS_EN;
   1173  1.1  reinoud #ifdef notyet
   1174  1.1  reinoud 	value |= ATI_REG_IER_IN_XRUN_EN;
   1175  1.1  reinoud 	value |= ATI_REG_IER_OUT_XRUN_EN;
   1176  1.1  reinoud 
   1177  1.1  reinoud 	value |= ATI_REG_IER_SPDIF_XRUN_EN;
   1178  1.1  reinoud 	value |= ATI_REG_IER_SPDF_STATUS_EN;
   1179  1.1  reinoud #endif
   1180  1.1  reinoud 
   1181  1.1  reinoud 	bus_space_write_4(iot, ioh, ATI_REG_IER, value);
   1182  1.1  reinoud }
   1183  1.1  reinoud 
   1184  1.1  reinoud 
   1185  1.1  reinoud void auixp_disable_interrupts(struct auixp_softc *sc) {
   1186  1.1  reinoud 	bus_space_tag_t     iot = sc->sc_iot;
   1187  1.1  reinoud 	bus_space_handle_t  ioh = sc->sc_ioh;
   1188  1.1  reinoud 
   1189  1.1  reinoud 	/* disable all interrupt sources */
   1190  1.1  reinoud 	bus_space_write_4(iot, ioh, ATI_REG_IER, 0);
   1191  1.1  reinoud 
   1192  1.1  reinoud 	/* clear all pending */
   1193  1.1  reinoud 	bus_space_write_4(iot, ioh, ATI_REG_ISR, 0xffffffff);
   1194  1.1  reinoud }
   1195  1.1  reinoud 
   1196  1.1  reinoud 
   1197  1.1  reinoud /* dismantle what we've set up by undoing setup */
   1198  1.1  reinoud int auixp_detach(struct device *self, int flags) {
   1199  1.1  reinoud 	struct auixp_softc *sc = (struct auixp_softc *)self;
   1200  1.1  reinoud 
   1201  1.1  reinoud 	/* XXX shouldn't we just reset the chip? XXX */
   1202  1.1  reinoud 	/*
   1203  1.1  reinoud 	 * should we explicitly disable interrupt generation and acknowledge
   1204  1.1  reinoud 	 * what's left on? better be safe than sorry.
   1205  1.1  reinoud 	 */
   1206  1.1  reinoud 	auixp_disable_interrupts(sc);
   1207  1.1  reinoud 
   1208  1.1  reinoud 	/* tear down .... */
   1209  1.1  reinoud 	config_detach(&sc->sc_dev, flags);	/* XXX OK? XXX */
   1210  1.1  reinoud 
   1211  1.1  reinoud 	if (sc->sc_ih != NULL)
   1212  1.1  reinoud 		pci_intr_disestablish(sc->sc_pct, sc->sc_ih);
   1213  1.1  reinoud 	if (sc->sc_ios)
   1214  1.1  reinoud 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
   1215  1.1  reinoud 
   1216  1.1  reinoud 	if (sc->savemem) free(sc->savemem, M_DEVBUF);
   1217  1.1  reinoud 
   1218  1.1  reinoud 	return (0);
   1219  1.1  reinoud }
   1220  1.1  reinoud 
   1221  1.1  reinoud 
   1222  1.1  reinoud /*
   1223  1.1  reinoud  * codec handling
   1224  1.1  reinoud  *
   1225  1.1  reinoud  * IXP audio support can have upto 3 codecs! are they chained ? or
   1226  1.1  reinoud  * alternative outlets with the same audio feed i.e. with different mixer
   1227  1.1  reinoud  * settings? XXX does NetBSD support more than one audio codec? XXX
   1228  1.1  reinoud  */
   1229  1.1  reinoud 
   1230  1.1  reinoud 
   1231  1.1  reinoud int auixp_attach_codec(void *aux, struct ac97_codec_if *codec_if) {
   1232  1.1  reinoud 	struct auixp_codec *ixp_codec = aux;
   1233  1.1  reinoud 
   1234  1.1  reinoud 	ixp_codec->codec_if = codec_if;
   1235  1.1  reinoud 	ixp_codec->present  = 1;
   1236  1.1  reinoud 
   1237  1.1  reinoud 	return (0);
   1238  1.1  reinoud }
   1239  1.1  reinoud 
   1240  1.1  reinoud 
   1241  1.1  reinoud int auixp_read_codec(void *aux, u_int8_t reg, uint16_t *result) {
   1242  1.1  reinoud 	struct auixp_codec *co  = aux;
   1243  1.1  reinoud 	struct auixp_softc *sc  = co->sc;
   1244  1.1  reinoud 	bus_space_tag_t     iot = sc->sc_iot;
   1245  1.1  reinoud 	bus_space_handle_t  ioh = sc->sc_ioh;
   1246  1.1  reinoud 	uint32_t data;
   1247  1.1  reinoud 	int timeout;
   1248  1.1  reinoud 
   1249  1.1  reinoud 	if (auixp_wait_for_codecs(sc, "read_codec")) return 0xffff;
   1250  1.1  reinoud 
   1251  1.1  reinoud 	/* build up command for reading codec register */
   1252  1.1  reinoud 	data = (reg << ATI_REG_PHYS_OUT_ADDR_SHIFT) |
   1253  1.1  reinoud 		ATI_REG_PHYS_OUT_ADDR_EN |
   1254  1.1  reinoud 		ATI_REG_PHYS_OUT_RW |
   1255  1.1  reinoud 		co->codec_nr;
   1256  1.1  reinoud 
   1257  1.1  reinoud 	bus_space_write_4(iot, ioh, ATI_REG_PHYS_OUT_ADDR, data);
   1258  1.1  reinoud 
   1259  1.1  reinoud 	if (auixp_wait_for_codecs(sc, "read_codec")) return 0xffff;
   1260  1.1  reinoud 
   1261  1.1  reinoud 	/* wait until codec info is clocked in */
   1262  1.1  reinoud 	timeout = 500;		/* 500*2 usec -> 0.001 sec */
   1263  1.1  reinoud 	do {
   1264  1.1  reinoud 		data = bus_space_read_4(iot, ioh, ATI_REG_PHYS_IN_ADDR);
   1265  1.1  reinoud 		if (data & ATI_REG_PHYS_IN_READ_FLAG) {
   1266  1.1  reinoud 			DPRINTF(("read ac'97 codec reg 0x%x = 0x%08x\n",
   1267  1.1  reinoud 				reg, data >> ATI_REG_PHYS_IN_DATA_SHIFT)
   1268  1.1  reinoud 			);
   1269  1.1  reinoud 			*result = data >> ATI_REG_PHYS_IN_DATA_SHIFT;
   1270  1.1  reinoud 			return 0;
   1271  1.1  reinoud 		};
   1272  1.1  reinoud 		DELAY(2);
   1273  1.1  reinoud 		timeout--;
   1274  1.1  reinoud 	} while (timeout > 0);
   1275  1.1  reinoud 
   1276  1.1  reinoud 	if (reg < 0x7c)
   1277  1.1  reinoud 		printf("%s: codec read timeout! (reg %x)\n",sc->sc_dev.dv_xname, reg);
   1278  1.1  reinoud 
   1279  1.1  reinoud 	return 0xffff;
   1280  1.1  reinoud }
   1281  1.1  reinoud 
   1282  1.1  reinoud 
   1283  1.1  reinoud int auixp_write_codec(void *aux, u_int8_t reg, uint16_t data) {
   1284  1.1  reinoud 	struct auixp_codec *co  = aux;
   1285  1.1  reinoud 	struct auixp_softc *sc  = co->sc;
   1286  1.1  reinoud 	bus_space_tag_t     iot = sc->sc_iot;
   1287  1.1  reinoud 	bus_space_handle_t  ioh = sc->sc_ioh;
   1288  1.1  reinoud 	uint32_t value;
   1289  1.1  reinoud 
   1290  1.1  reinoud 	DPRINTF(("write ac'97 codec reg 0x%x = 0x%08x\n", reg, data));
   1291  1.1  reinoud 	if (auixp_wait_for_codecs(sc, "write_codec")) return -1;
   1292  1.1  reinoud 
   1293  1.1  reinoud 	/* build up command for writing codec register */
   1294  1.1  reinoud 	value = (((uint32_t) data) << ATI_REG_PHYS_OUT_DATA_SHIFT) |
   1295  1.1  reinoud 	        (((uint32_t)  reg) << ATI_REG_PHYS_OUT_ADDR_SHIFT) |
   1296  1.1  reinoud 		ATI_REG_PHYS_OUT_ADDR_EN |
   1297  1.1  reinoud 		co->codec_nr;
   1298  1.1  reinoud 
   1299  1.1  reinoud 	bus_space_write_4(iot, ioh, ATI_REG_PHYS_OUT_ADDR, value);
   1300  1.1  reinoud 
   1301  1.1  reinoud 	return 0;
   1302  1.1  reinoud }
   1303  1.1  reinoud 
   1304  1.1  reinoud 
   1305  1.1  reinoud int auixp_reset_codec(void *aux) {
   1306  1.1  reinoud 	/* nothing to be done? */
   1307  1.1  reinoud 	return 0;
   1308  1.1  reinoud }
   1309  1.1  reinoud 
   1310  1.1  reinoud 
   1311  1.1  reinoud enum ac97_host_flags auixp_flags_codec(void *aux) {
   1312  1.1  reinoud 	struct auixp_codec *ixp_codec = aux;
   1313  1.1  reinoud 
   1314  1.1  reinoud 	return (ixp_codec->codec_flags);
   1315  1.1  reinoud }
   1316  1.1  reinoud 
   1317  1.1  reinoud 
   1318  1.1  reinoud int auixp_wait_for_codecs(struct auixp_softc *sc, char *func) {
   1319  1.1  reinoud 	bus_space_tag_t      iot = sc->sc_iot;
   1320  1.1  reinoud 	bus_space_handle_t   ioh = sc->sc_ioh;
   1321  1.1  reinoud 	uint32_t value;
   1322  1.1  reinoud 	int timeout;
   1323  1.1  reinoud 
   1324  1.1  reinoud 	/* wait until all codec transfers are done */
   1325  1.1  reinoud 	timeout = 500;		/* 500*2 usec -> 0.001 sec */
   1326  1.1  reinoud 	do {
   1327  1.1  reinoud 		value = bus_space_read_4(iot, ioh, ATI_REG_PHYS_OUT_ADDR);
   1328  1.1  reinoud 		if ((value & ATI_REG_PHYS_OUT_ADDR_EN) == 0) return 0;
   1329  1.1  reinoud 
   1330  1.1  reinoud 		DELAY(2);
   1331  1.1  reinoud 		timeout--;
   1332  1.1  reinoud 	} while (timeout > 0);
   1333  1.1  reinoud 
   1334  1.1  reinoud 	printf("%s: %s: timed out\n", func, sc->sc_dev.dv_xname);
   1335  1.1  reinoud 	return -1;
   1336  1.1  reinoud }
   1337  1.1  reinoud 
   1338  1.1  reinoud 
   1339  1.1  reinoud 
   1340  1.1  reinoud void auixp_autodetect_codecs(struct auixp_softc *sc) {
   1341  1.1  reinoud 	bus_space_tag_t      iot = sc->sc_iot;
   1342  1.1  reinoud 	bus_space_handle_t   ioh = sc->sc_ioh;
   1343  1.1  reinoud 	struct auixp_codec  *codec;
   1344  1.1  reinoud 	int timeout, codec_nr;
   1345  1.1  reinoud 
   1346  1.1  reinoud 	/* ATI IXP can have upto 3 codecs; mark all codecs as not existing */
   1347  1.1  reinoud 	sc->sc_codec_not_ready_bits = 0;
   1348  1.1  reinoud 	sc->sc_num_codecs = 0;
   1349  1.1  reinoud 
   1350  1.1  reinoud 	/* enable all codecs to interrupt as well as the new frame interrupt */
   1351  1.1  reinoud 	bus_space_write_4(iot, ioh, ATI_REG_IER, CODEC_CHECK_BITS);
   1352  1.1  reinoud 
   1353  1.1  reinoud 	/* wait for the interrupts to happen */
   1354  1.1  reinoud 	timeout = 100;		/* 100.000 usec -> 0.1 sec */
   1355  1.1  reinoud 
   1356  1.1  reinoud 	while (timeout > 0) {
   1357  1.1  reinoud 		DELAY(1000);
   1358  1.1  reinoud 		if (sc->sc_codec_not_ready_bits) break;
   1359  1.1  reinoud 		timeout--;
   1360  1.1  reinoud 	};
   1361  1.1  reinoud 
   1362  1.1  reinoud 	if (timeout == 0)
   1363  1.1  reinoud 		printf( "WARNING: timeout during codec detection; "
   1364  1.1  reinoud 			"codecs might be present but haven't interrupted\n");
   1365  1.1  reinoud 
   1366  1.1  reinoud 	/* disable all interrupts for now */
   1367  1.1  reinoud 	auixp_disable_interrupts(sc);
   1368  1.1  reinoud 
   1369  1.1  reinoud 	/* Attach AC97 host interfaces */
   1370  1.1  reinoud 	for (codec_nr = 0; codec_nr < ATI_IXP_CODECS; codec_nr++) {
   1371  1.1  reinoud 		codec = &sc->sc_codec[codec_nr];
   1372  1.1  reinoud 		bzero(codec, sizeof(struct auixp_codec));
   1373  1.1  reinoud 
   1374  1.1  reinoud 		codec->sc       = sc;
   1375  1.1  reinoud 		codec->codec_nr = codec_nr;
   1376  1.1  reinoud 		codec->present  = 0;
   1377  1.1  reinoud 
   1378  1.1  reinoud 		codec->host_if.arg    = codec;
   1379  1.1  reinoud 		codec->host_if.attach = auixp_attach_codec;
   1380  1.1  reinoud 		codec->host_if.read   = auixp_read_codec;
   1381  1.1  reinoud 		codec->host_if.write  = auixp_write_codec;
   1382  1.1  reinoud 		codec->host_if.reset  = auixp_reset_codec;
   1383  1.1  reinoud 		codec->host_if.flags  = auixp_flags_codec;
   1384  1.1  reinoud 	};
   1385  1.1  reinoud 
   1386  1.1  reinoud 	if (!(sc->sc_codec_not_ready_bits & ATI_REG_ISR_CODEC0_NOT_READY)) {
   1387  1.1  reinoud 		/* codec 0 present */
   1388  1.1  reinoud 		// printf("auixp : YAY! codec 0 present!\n");
   1389  1.1  reinoud 		if (ac97_attach(&sc->sc_codec[0].host_if, &sc->sc_dev) == 0) sc->sc_num_codecs++;
   1390  1.1  reinoud 	};
   1391  1.1  reinoud 
   1392  1.1  reinoud 	if (!(sc->sc_codec_not_ready_bits & ATI_REG_ISR_CODEC1_NOT_READY)) {
   1393  1.1  reinoud 		/* codec 1 present */
   1394  1.1  reinoud 		printf("auixp : YAY! codec 1 present!\n");
   1395  1.1  reinoud 		if (ac97_attach(&sc->sc_codec[1].host_if, &sc->sc_dev) == 0) sc->sc_num_codecs++;
   1396  1.1  reinoud 	};
   1397  1.1  reinoud 
   1398  1.1  reinoud 	if (!(sc->sc_codec_not_ready_bits & ATI_REG_ISR_CODEC2_NOT_READY)) {
   1399  1.1  reinoud 		/* codec 2 present */
   1400  1.1  reinoud 		printf("auixp : YAY! codec 2 present!\n");
   1401  1.1  reinoud 		if (ac97_attach(&sc->sc_codec[2].host_if, &sc->sc_dev) == 0) sc->sc_num_codecs++;
   1402  1.1  reinoud 	};
   1403  1.1  reinoud 
   1404  1.1  reinoud 	if (sc->sc_num_codecs == 0) {
   1405  1.1  reinoud 		printf("auixp : BUMMER, no codecs detected or "
   1406  1.1  reinoud 				"no codecs managed to initialise\n");
   1407  1.1  reinoud 		return;
   1408  1.1  reinoud 	};
   1409  1.1  reinoud 
   1410  1.1  reinoud };
   1411  1.1  reinoud 
   1412  1.1  reinoud 
   1413  1.1  reinoud 
   1414  1.1  reinoud /* initialisation routines */
   1415  1.1  reinoud 
   1416  1.1  reinoud void auixp_disable_dma(struct auixp_softc *sc, struct auixp_dma *dma) {
   1417  1.1  reinoud 	bus_space_tag_t      iot = sc->sc_iot;
   1418  1.1  reinoud 	bus_space_handle_t   ioh = sc->sc_ioh;
   1419  1.1  reinoud 	uint32_t value;
   1420  1.1  reinoud 
   1421  1.1  reinoud 	/* lets not stress the DMA engine more than nessisary */
   1422  1.1  reinoud 	value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
   1423  1.1  reinoud 	if (value & dma->dma_enable_bit) {
   1424  1.1  reinoud 		value &= ~dma->dma_enable_bit;
   1425  1.1  reinoud 		bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
   1426  1.1  reinoud 	};
   1427  1.1  reinoud }
   1428  1.1  reinoud 
   1429  1.1  reinoud 
   1430  1.1  reinoud void auixp_enable_dma(struct auixp_softc *sc, struct auixp_dma *dma) {
   1431  1.1  reinoud 	bus_space_tag_t      iot = sc->sc_iot;
   1432  1.1  reinoud 	bus_space_handle_t   ioh = sc->sc_ioh;
   1433  1.1  reinoud 	uint32_t value;
   1434  1.1  reinoud 
   1435  1.1  reinoud 	/* lets not stress the DMA engine more than nessisary */
   1436  1.1  reinoud 	value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
   1437  1.1  reinoud 	if (!(value & dma->dma_enable_bit)) {
   1438  1.1  reinoud 		value |= dma->dma_enable_bit;
   1439  1.1  reinoud 		bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
   1440  1.1  reinoud 	};
   1441  1.1  reinoud }
   1442  1.1  reinoud 
   1443  1.1  reinoud 
   1444  1.1  reinoud void auixp_reset_aclink(struct auixp_softc *sc) {
   1445  1.1  reinoud 	/* XXX fix me! XXX */
   1446  1.1  reinoud 	printf("auixp : could reset aclink\n");
   1447  1.1  reinoud }
   1448  1.1  reinoud 
   1449  1.1  reinoud 
   1450  1.1  reinoud /* XXX setup saved configuration XXX */
   1451  1.1  reinoud void auixp_config(struct auixp_softc *sc) {
   1452  1.1  reinoud 	/* XXX fix me XXX */
   1453  1.1  reinoud 	return;
   1454  1.1  reinoud }
   1455  1.1  reinoud 
   1456  1.1  reinoud 
   1457  1.1  reinoud /* chip hard init */
   1458  1.1  reinoud int auixp_init(struct auixp_softc *sc) {
   1459  1.1  reinoud 	bus_space_tag_t      iot = sc->sc_iot;
   1460  1.1  reinoud 	bus_space_handle_t   ioh = sc->sc_ioh;
   1461  1.1  reinoud 	uint32_t value;
   1462  1.1  reinoud 
   1463  1.1  reinoud 	/* disable all interrupts and clear all sources */
   1464  1.1  reinoud 	auixp_disable_interrupts(sc);
   1465  1.1  reinoud 
   1466  1.1  reinoud 	/* clear all DMA enables (preserving rest of settings) */
   1467  1.1  reinoud 	value = bus_space_read_4(iot, ioh, ATI_REG_CMD);
   1468  1.1  reinoud 	value &= ~( ATI_REG_CMD_IN_DMA_EN  |
   1469  1.1  reinoud 		    ATI_REG_CMD_OUT_DMA_EN |
   1470  1.1  reinoud 		    ATI_REG_CMD_SPDF_OUT_EN );
   1471  1.1  reinoud 	bus_space_write_4(iot, ioh, ATI_REG_CMD, value);
   1472  1.1  reinoud 
   1473  1.1  reinoud 	/* Reset AC-link */
   1474  1.1  reinoud 	auixp_reset_aclink(sc);
   1475  1.1  reinoud 
   1476  1.1  reinoud 	/*
   1477  1.1  reinoud 	 * codecs get auto-detected later
   1478  1.1  reinoud 	 *
   1479  1.1  reinoud 	 * note: we are NOT enabling interrupts yet, no codecs have been
   1480  1.1  reinoud 	 * detected yet nor is anything else set up
   1481  1.1  reinoud 	 */
   1482  1.1  reinoud 
   1483  1.1  reinoud 	return 0;
   1484  1.1  reinoud }
   1485  1.1  reinoud 
   1486  1.1  reinoud 
   1487  1.1  reinoud /*
   1488  1.1  reinoud  * TODO power saving and suspend / resume support
   1489  1.1  reinoud  *
   1490  1.1  reinoud  */
   1491  1.1  reinoud 
   1492  1.1  reinoud int auixp_power(struct auixp_softc *sc, int state) {
   1493  1.1  reinoud 	pcitag_t tag = sc->sc_tag;
   1494  1.1  reinoud 	pci_chipset_tag_t pc = sc->sc_pct;
   1495  1.1  reinoud 	pcireg_t data;
   1496  1.1  reinoud 	int pmcapreg;
   1497  1.1  reinoud 
   1498  1.1  reinoud 	if (pci_get_capability(pc, tag, PCI_CAP_PWRMGMT, &pmcapreg, 0)) {
   1499  1.1  reinoud 		data = pci_conf_read(pc, tag, pmcapreg + PCI_PMCSR);
   1500  1.1  reinoud 		if ((data & PCI_PMCSR_STATE_MASK) != state)
   1501  1.1  reinoud 			pci_conf_write(pc, tag, pmcapreg + PCI_PMCSR, state);
   1502  1.1  reinoud 	}
   1503  1.1  reinoud 
   1504  1.1  reinoud 	return (0);
   1505  1.1  reinoud }
   1506  1.1  reinoud 
   1507  1.1  reinoud 
   1508  1.1  reinoud void auixp_powerhook(int why, void *hdl) {
   1509  1.1  reinoud 	struct auixp_softc *sc = (struct auixp_softc *)hdl;
   1510  1.1  reinoud 
   1511  1.1  reinoud 	switch (why) {
   1512  1.1  reinoud 	case PWR_SUSPEND:
   1513  1.1  reinoud 	case PWR_STANDBY:
   1514  1.1  reinoud 		auixp_suspend(sc);
   1515  1.1  reinoud 		break;
   1516  1.1  reinoud 	case PWR_RESUME:
   1517  1.1  reinoud 		auixp_resume(sc);
   1518  1.1  reinoud /* XXX fix me XXX */
   1519  1.1  reinoud //		(sc->codec_if->vtbl->restore_ports)(sc->codec_if);
   1520  1.1  reinoud 		break;
   1521  1.1  reinoud 	}
   1522  1.1  reinoud }
   1523  1.1  reinoud 
   1524  1.1  reinoud 
   1525  1.1  reinoud int auixp_suspend(struct auixp_softc *sc) {
   1526  1.1  reinoud 	/* XXX no power functions yet XXX */
   1527  1.1  reinoud 	return (0);
   1528  1.1  reinoud }
   1529  1.1  reinoud 
   1530  1.1  reinoud 
   1531  1.1  reinoud int auixp_resume(struct auixp_softc *sc) {
   1532  1.1  reinoud 	/* XXX no power functions yet XXX */
   1533  1.1  reinoud 	return (0);
   1534  1.1  reinoud }
   1535  1.1  reinoud 
   1536  1.1  reinoud 
   1537  1.1  reinoud #ifdef DEBUG_AUIXP
   1538  1.1  reinoud 
   1539  1.1  reinoud void auixp_dumpreg(void) {
   1540  1.1  reinoud 	struct auixp_softc  *sc  = static_sc;
   1541  1.1  reinoud 	bus_space_tag_t      iot = sc->sc_iot;
   1542  1.1  reinoud 	bus_space_handle_t   ioh = sc->sc_ioh;
   1543  1.1  reinoud 	int i;
   1544  1.1  reinoud 
   1545  1.1  reinoud 	printf("auixp register dump:\n");
   1546  1.1  reinoud 	for (i = 0; i < 256; i+=4) {
   1547  1.1  reinoud 		printf("\t0x%02x: 0x%08x\n", i, bus_space_read_4(iot, ioh, i));
   1548  1.1  reinoud 	};
   1549  1.1  reinoud 	printf("\n");
   1550  1.1  reinoud }
   1551  1.1  reinoud #endif
   1552  1.1  reinoud 
   1553  1.1  reinoud /* end of auixp.c */
   1554  1.1  reinoud 
   1555