Home | History | Annotate | Line # | Download | only in pci
auacer.c revision 1.30.8.1
      1  1.30.8.1       tls /*	$NetBSD: auacer.c,v 1.30.8.1 2012/11/20 03:02:13 tls Exp $	*/
      2       1.1  augustss 
      3       1.1  augustss /*-
      4      1.29  jmcneill  * Copyright (c) 2004, 2008 The NetBSD Foundation, Inc.
      5       1.1  augustss  * All rights reserved.
      6       1.1  augustss  *
      7       1.1  augustss  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1  augustss  * by Lennart Augustsson.
      9       1.1  augustss  *
     10       1.1  augustss  * Redistribution and use in source and binary forms, with or without
     11       1.1  augustss  * modification, are permitted provided that the following conditions
     12       1.1  augustss  * are met:
     13       1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     14       1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     15       1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     17       1.1  augustss  *    documentation and/or other materials provided with the distribution.
     18       1.1  augustss  *
     19       1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1  augustss  */
     31       1.1  augustss 
     32       1.1  augustss /*
     33       1.1  augustss  * Acer Labs M5455 audio driver
     34       1.1  augustss  *
     35       1.5  augustss  * Acer provides data sheets after signing an NDA, so this is guess work.
     36       1.1  augustss  * The chip behaves somewhat like the Intel i8x0, so this driver
     37       1.1  augustss  * is loosely based on the auich driver.  Additional information taken from
     38       1.1  augustss  * the ALSA intel8x0.c driver (which handles M5455 as well).
     39       1.1  augustss  *
     40       1.1  augustss  * As an historical note one can observe that the auich driver borrows
     41       1.1  augustss  * lot from the first NetBSD PCI audio driver, the eap driver.  But this
     42       1.1  augustss  * is not attributed anywhere.
     43       1.1  augustss  */
     44       1.1  augustss 
     45       1.1  augustss 
     46       1.1  augustss #include <sys/cdefs.h>
     47  1.30.8.1       tls __KERNEL_RCSID(0, "$NetBSD: auacer.c,v 1.30.8.1 2012/11/20 03:02:13 tls Exp $");
     48       1.1  augustss 
     49       1.1  augustss #include <sys/param.h>
     50       1.1  augustss #include <sys/systm.h>
     51       1.1  augustss #include <sys/kernel.h>
     52      1.29  jmcneill #include <sys/kmem.h>
     53       1.1  augustss #include <sys/device.h>
     54       1.1  augustss #include <sys/fcntl.h>
     55       1.1  augustss #include <sys/proc.h>
     56       1.1  augustss 
     57       1.1  augustss #include <dev/pci/pcidevs.h>
     58       1.1  augustss #include <dev/pci/pcivar.h>
     59       1.1  augustss #include <dev/pci/auacerreg.h>
     60       1.1  augustss 
     61       1.1  augustss #include <sys/audioio.h>
     62       1.1  augustss #include <dev/audio_if.h>
     63       1.1  augustss #include <dev/mulaw.h>
     64       1.1  augustss #include <dev/auconv.h>
     65       1.1  augustss 
     66      1.17        ad #include <sys/bus.h>
     67       1.1  augustss 
     68       1.1  augustss #include <dev/ic/ac97reg.h>
     69       1.1  augustss #include <dev/ic/ac97var.h>
     70       1.1  augustss 
     71       1.1  augustss struct auacer_dma {
     72       1.1  augustss 	bus_dmamap_t map;
     73      1.16  christos 	void *addr;
     74       1.1  augustss 	bus_dma_segment_t segs[1];
     75       1.1  augustss 	int nsegs;
     76       1.1  augustss 	size_t size;
     77       1.1  augustss 	struct auacer_dma *next;
     78       1.1  augustss };
     79       1.1  augustss 
     80       1.1  augustss #define	DMAADDR(p)	((p)->map->dm_segs[0].ds_addr)
     81       1.1  augustss #define	KERNADDR(p)	((void *)((p)->addr))
     82       1.1  augustss 
     83       1.1  augustss struct auacer_cdata {
     84       1.1  augustss 	struct auacer_dmalist ic_dmalist_pcmo[ALI_DMALIST_MAX];
     85       1.1  augustss };
     86       1.1  augustss 
     87       1.1  augustss struct auacer_chan {
     88       1.1  augustss 	uint32_t ptr;
     89       1.1  augustss 	uint32_t start, p, end;
     90       1.1  augustss 	uint32_t blksize, fifoe;
     91       1.1  augustss 	uint32_t ack;
     92       1.1  augustss 	uint32_t port;
     93       1.1  augustss 	struct auacer_dmalist *dmalist;
     94       1.1  augustss 	void (*intr)(void *);
     95       1.1  augustss 	void *arg;
     96       1.1  augustss };
     97       1.1  augustss 
     98       1.1  augustss struct auacer_softc {
     99  1.30.8.1       tls 	device_t sc_dev;
    100       1.1  augustss 	void *sc_ih;
    101      1.29  jmcneill 	kmutex_t sc_lock;
    102      1.29  jmcneill 	kmutex_t sc_intr_lock;
    103       1.1  augustss 
    104       1.1  augustss 	audio_device_t sc_audev;
    105       1.1  augustss 
    106       1.1  augustss 	bus_space_tag_t iot;
    107       1.1  augustss 	bus_space_handle_t mix_ioh;
    108       1.1  augustss 	bus_space_handle_t aud_ioh;
    109       1.1  augustss 	bus_dma_tag_t dmat;
    110       1.1  augustss 
    111       1.1  augustss 	struct ac97_codec_if *codec_if;
    112       1.1  augustss 	struct ac97_host_if host_if;
    113       1.1  augustss 
    114       1.1  augustss 	/* DMA scatter-gather lists. */
    115       1.1  augustss 	bus_dmamap_t sc_cddmamap;
    116       1.1  augustss #define	sc_cddma	sc_cddmamap->dm_segs[0].ds_addr
    117       1.1  augustss 
    118       1.1  augustss 	struct auacer_cdata *sc_cdata;
    119       1.1  augustss 
    120       1.1  augustss 	struct auacer_chan sc_pcmo;
    121       1.1  augustss 
    122       1.1  augustss 	struct auacer_dma *sc_dmas;
    123       1.1  augustss 
    124       1.1  augustss 	pci_chipset_tag_t sc_pc;
    125       1.1  augustss 	pcitag_t sc_pt;
    126       1.1  augustss 
    127       1.1  augustss 	int  sc_dmamap_flags;
    128       1.1  augustss 
    129       1.4      kent #define AUACER_NFORMATS	3
    130       1.4      kent 	struct audio_format sc_formats[AUACER_NFORMATS];
    131       1.4      kent 	struct audio_encoding_set *sc_encodings;
    132       1.1  augustss };
    133       1.1  augustss 
    134       1.1  augustss #define READ1(sc, a) bus_space_read_1(sc->iot, sc->aud_ioh, a)
    135       1.1  augustss #define READ2(sc, a) bus_space_read_2(sc->iot, sc->aud_ioh, a)
    136       1.1  augustss #define READ4(sc, a) bus_space_read_4(sc->iot, sc->aud_ioh, a)
    137       1.1  augustss #define WRITE1(sc, a, v) bus_space_write_1(sc->iot, sc->aud_ioh, a, v)
    138       1.1  augustss #define WRITE2(sc, a, v) bus_space_write_2(sc->iot, sc->aud_ioh, a, v)
    139       1.1  augustss #define WRITE4(sc, a, v) bus_space_write_4(sc->iot, sc->aud_ioh, a, v)
    140       1.1  augustss 
    141       1.1  augustss /* Debug */
    142       1.1  augustss #ifdef AUACER_DEBUG
    143       1.1  augustss #define	DPRINTF(l,x)	do { if (auacer_debug & (l)) printf x; } while(0)
    144       1.1  augustss int auacer_debug = 0;
    145       1.1  augustss #define	ALI_DEBUG_CODECIO	0x0001
    146       1.1  augustss #define	ALI_DEBUG_DMA		0x0002
    147       1.1  augustss #define	ALI_DEBUG_INTR		0x0004
    148       1.1  augustss #define ALI_DEBUG_API		0x0008
    149       1.1  augustss #define ALI_DEBUG_MIXERAPI	0x0010
    150       1.1  augustss #else
    151       1.1  augustss #define	DPRINTF(x,y)	/* nothing */
    152       1.1  augustss #endif
    153       1.1  augustss 
    154      1.10   thorpej static int	auacer_intr(void *);
    155       1.1  augustss 
    156      1.10   thorpej static int	auacer_query_encoding(void *, struct audio_encoding *);
    157      1.10   thorpej static int	auacer_set_params(void *, int, int, audio_params_t *,
    158      1.10   thorpej 				  audio_params_t *, stream_filter_list_t *,
    159      1.10   thorpej 				  stream_filter_list_t *);
    160      1.10   thorpej static int	auacer_round_blocksize(void *, int, int,
    161      1.10   thorpej 				       const audio_params_t *);
    162      1.10   thorpej static int	auacer_halt_output(void *);
    163      1.10   thorpej static int	auacer_halt_input(void *);
    164      1.10   thorpej static int	auacer_getdev(void *, struct audio_device *);
    165      1.10   thorpej static int	auacer_set_port(void *, mixer_ctrl_t *);
    166      1.10   thorpej static int	auacer_get_port(void *, mixer_ctrl_t *);
    167      1.10   thorpej static int	auacer_query_devinfo(void *, mixer_devinfo_t *);
    168      1.29  jmcneill static void	*auacer_allocm(void *, int, size_t);
    169      1.29  jmcneill static void	auacer_freem(void *, void *, size_t);
    170      1.10   thorpej static size_t	auacer_round_buffersize(void *, int, size_t);
    171      1.10   thorpej static paddr_t	auacer_mappage(void *, void *, off_t, int);
    172      1.10   thorpej static int	auacer_get_props(void *);
    173      1.10   thorpej static int	auacer_trigger_output(void *, void *, void *, int,
    174      1.10   thorpej 				      void (*)(void *), void *,
    175      1.10   thorpej 				      const audio_params_t *);
    176      1.10   thorpej static int	auacer_trigger_input(void *, void *, void *, int,
    177      1.10   thorpej 				     void (*)(void *), void *,
    178      1.10   thorpej 				     const audio_params_t *);
    179      1.10   thorpej 
    180      1.10   thorpej static int	auacer_alloc_cdata(struct auacer_softc *);
    181      1.10   thorpej 
    182      1.10   thorpej static int	auacer_allocmem(struct auacer_softc *, size_t, size_t,
    183      1.10   thorpej 				struct auacer_dma *);
    184      1.10   thorpej static int	auacer_freemem(struct auacer_softc *, struct auacer_dma *);
    185      1.29  jmcneill static void	auacer_get_locks(void *, kmutex_t **, kmutex_t **);
    186       1.1  augustss 
    187      1.27    dyoung static bool	auacer_resume(device_t, const pmf_qual_t *);
    188      1.10   thorpej static int	auacer_set_rate(struct auacer_softc *, int, u_int);
    189       1.1  augustss 
    190       1.1  augustss static void auacer_reset(struct auacer_softc *sc);
    191       1.1  augustss 
    192      1.10   thorpej static struct audio_hw_if auacer_hw_if = {
    193       1.6      kent 	NULL,			/* open */
    194       1.6      kent 	NULL,			/* close */
    195       1.1  augustss 	NULL,			/* drain */
    196       1.1  augustss 	auacer_query_encoding,
    197       1.1  augustss 	auacer_set_params,
    198       1.1  augustss 	auacer_round_blocksize,
    199       1.1  augustss 	NULL,			/* commit_setting */
    200       1.1  augustss 	NULL,			/* init_output */
    201       1.1  augustss 	NULL,			/* init_input */
    202       1.1  augustss 	NULL,			/* start_output */
    203       1.1  augustss 	NULL,			/* start_input */
    204       1.1  augustss 	auacer_halt_output,
    205       1.1  augustss 	auacer_halt_input,
    206       1.1  augustss 	NULL,			/* speaker_ctl */
    207       1.1  augustss 	auacer_getdev,
    208       1.1  augustss 	NULL,			/* getfd */
    209       1.1  augustss 	auacer_set_port,
    210       1.1  augustss 	auacer_get_port,
    211       1.1  augustss 	auacer_query_devinfo,
    212       1.1  augustss 	auacer_allocm,
    213       1.1  augustss 	auacer_freem,
    214       1.1  augustss 	auacer_round_buffersize,
    215       1.1  augustss 	auacer_mappage,
    216       1.1  augustss 	auacer_get_props,
    217       1.1  augustss 	auacer_trigger_output,
    218       1.1  augustss 	auacer_trigger_input,
    219       1.1  augustss 	NULL,			/* dev_ioctl */
    220      1.29  jmcneill 	auacer_get_locks,
    221       1.1  augustss };
    222       1.1  augustss 
    223       1.4      kent #define AUACER_FORMATS_4CH	1
    224       1.4      kent #define AUACER_FORMATS_6CH	2
    225       1.4      kent static const struct audio_format auacer_formats[AUACER_NFORMATS] = {
    226       1.5  augustss 	{NULL, AUMODE_PLAY | AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
    227       1.4      kent 	 2, AUFMT_STEREO, 0, {8000, 48000}},
    228       1.4      kent 	{NULL, AUMODE_PLAY, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
    229       1.4      kent 	 4, AUFMT_SURROUND4, 0, {8000, 48000}},
    230       1.4      kent 	{NULL, AUMODE_PLAY, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
    231       1.4      kent 	 6, AUFMT_DOLBY_5_1, 0, {8000, 48000}},
    232       1.4      kent };
    233       1.4      kent 
    234      1.10   thorpej static int	auacer_attach_codec(void *, struct ac97_codec_if *);
    235      1.10   thorpej static int	auacer_read_codec(void *, uint8_t, uint16_t *);
    236      1.10   thorpej static int	auacer_write_codec(void *, uint8_t, uint16_t);
    237      1.10   thorpej static int	auacer_reset_codec(void *);
    238       1.1  augustss 
    239      1.10   thorpej static int
    240      1.23    cegger auacer_match(device_t parent, cfdata_t match, void *aux)
    241       1.1  augustss {
    242       1.7      kent 	struct pci_attach_args *pa;
    243       1.1  augustss 
    244       1.7      kent 	pa = (struct pci_attach_args *)aux;
    245       1.1  augustss 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ALI &&
    246       1.1  augustss 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_ALI_M5455)
    247       1.1  augustss 		return 1;
    248       1.1  augustss 	return 0;
    249       1.1  augustss }
    250       1.1  augustss 
    251      1.10   thorpej static void
    252      1.23    cegger auacer_attach(device_t parent, device_t self, void *aux)
    253       1.1  augustss {
    254       1.7      kent 	struct auacer_softc *sc;
    255       1.7      kent 	struct pci_attach_args *pa;
    256       1.1  augustss 	pci_intr_handle_t ih;
    257       1.1  augustss 	bus_size_t aud_size;
    258       1.1  augustss 	pcireg_t v;
    259       1.1  augustss 	const char *intrstr;
    260       1.4      kent 	int i;
    261       1.1  augustss 
    262      1.24    cegger 	sc = device_private(self);
    263  1.30.8.1       tls 	sc->sc_dev = self;
    264       1.7      kent 	pa = aux;
    265       1.1  augustss 	aprint_normal(": Acer Labs M5455 Audio controller\n");
    266       1.1  augustss 
    267       1.1  augustss 	if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_IO, 0, &sc->iot,
    268       1.1  augustss 		&sc->aud_ioh, NULL, &aud_size)) {
    269       1.1  augustss 		aprint_error(": can't map i/o space\n");
    270       1.1  augustss 		return;
    271       1.1  augustss 	}
    272       1.1  augustss 
    273       1.1  augustss 	sc->sc_pc = pa->pa_pc;
    274       1.1  augustss 	sc->sc_pt = pa->pa_tag;
    275       1.1  augustss 	sc->dmat = pa->pa_dmat;
    276       1.1  augustss 
    277       1.1  augustss 	sc->sc_dmamap_flags = BUS_DMA_COHERENT;	/* XXX remove */
    278       1.1  augustss 
    279      1.29  jmcneill 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    280      1.30       mrg 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_AUDIO);
    281      1.29  jmcneill 
    282       1.1  augustss 	/* enable bus mastering */
    283       1.1  augustss 	v = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    284       1.1  augustss 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    285       1.1  augustss 	    v | PCI_COMMAND_MASTER_ENABLE);
    286       1.1  augustss 
    287       1.1  augustss 	/* Map and establish the interrupt. */
    288       1.1  augustss 	if (pci_intr_map(pa, &ih)) {
    289  1.30.8.1       tls 		aprint_error_dev(sc->sc_dev, "can't map interrupt\n");
    290      1.29  jmcneill 		mutex_destroy(&sc->sc_lock);
    291      1.29  jmcneill 		mutex_destroy(&sc->sc_intr_lock);
    292       1.1  augustss 		return;
    293       1.1  augustss 	}
    294       1.1  augustss 	intrstr = pci_intr_string(pa->pa_pc, ih);
    295      1.30       mrg 	sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_AUDIO,
    296       1.1  augustss 	    auacer_intr, sc);
    297       1.1  augustss 	if (sc->sc_ih == NULL) {
    298  1.30.8.1       tls 		aprint_error_dev(sc->sc_dev, "can't establish interrupt");
    299       1.1  augustss 		if (intrstr != NULL)
    300      1.25     njoly 			aprint_error(" at %s", intrstr);
    301      1.25     njoly 		aprint_error("\n");
    302      1.29  jmcneill 		mutex_destroy(&sc->sc_lock);
    303      1.29  jmcneill 		mutex_destroy(&sc->sc_intr_lock);
    304       1.1  augustss 		return;
    305       1.1  augustss 	}
    306  1.30.8.1       tls 	aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
    307       1.1  augustss 
    308       1.1  augustss 	strlcpy(sc->sc_audev.name, "M5455 AC97", MAX_AUDIO_DEV_LEN);
    309       1.1  augustss 	snprintf(sc->sc_audev.version, MAX_AUDIO_DEV_LEN,
    310       1.1  augustss 		 "0x%02x", PCI_REVISION(pa->pa_class));
    311  1.30.8.1       tls 	strlcpy(sc->sc_audev.config, device_xname(sc->sc_dev), MAX_AUDIO_DEV_LEN);
    312       1.1  augustss 
    313       1.1  augustss 	/* Set up DMA lists. */
    314       1.1  augustss 	auacer_alloc_cdata(sc);
    315       1.1  augustss 	sc->sc_pcmo.dmalist = sc->sc_cdata->ic_dmalist_pcmo;
    316       1.1  augustss 	sc->sc_pcmo.ptr = 0;
    317       1.1  augustss 	sc->sc_pcmo.port = ALI_BASE_PO;
    318       1.1  augustss 
    319       1.1  augustss 	DPRINTF(ALI_DEBUG_DMA, ("auacer_attach: lists %p\n",
    320       1.1  augustss 	    sc->sc_pcmo.dmalist));
    321       1.1  augustss 
    322       1.1  augustss 	sc->host_if.arg = sc;
    323       1.1  augustss 	sc->host_if.attach = auacer_attach_codec;
    324       1.1  augustss 	sc->host_if.read = auacer_read_codec;
    325       1.1  augustss 	sc->host_if.write = auacer_write_codec;
    326       1.1  augustss 	sc->host_if.reset = auacer_reset_codec;
    327       1.1  augustss 
    328      1.29  jmcneill 	if (ac97_attach(&sc->host_if, self, &sc->sc_lock) != 0) {
    329      1.29  jmcneill 		mutex_destroy(&sc->sc_lock);
    330      1.29  jmcneill 		mutex_destroy(&sc->sc_intr_lock);
    331       1.1  augustss 		return;
    332      1.29  jmcneill 	}
    333       1.1  augustss 
    334       1.4      kent 	/* setup audio_format */
    335       1.4      kent 	memcpy(sc->sc_formats, auacer_formats, sizeof(auacer_formats));
    336      1.29  jmcneill 	mutex_enter(&sc->sc_lock);
    337       1.4      kent 	if (!AC97_IS_4CH(sc->codec_if))
    338       1.4      kent 		AUFMT_INVALIDATE(&sc->sc_formats[AUACER_FORMATS_4CH]);
    339       1.4      kent 	if (!AC97_IS_6CH(sc->codec_if))
    340       1.4      kent 		AUFMT_INVALIDATE(&sc->sc_formats[AUACER_FORMATS_6CH]);
    341       1.4      kent 	if (AC97_IS_FIXED_RATE(sc->codec_if)) {
    342       1.4      kent 		for (i = 0; i < AUACER_NFORMATS; i++) {
    343       1.4      kent 			sc->sc_formats[i].frequency_type = 1;
    344       1.4      kent 			sc->sc_formats[i].frequency[0] = 48000;
    345       1.4      kent 		}
    346       1.4      kent 	}
    347      1.29  jmcneill 	mutex_exit(&sc->sc_lock);
    348       1.4      kent 
    349       1.4      kent 	if (0 != auconv_create_encodings(sc->sc_formats, AUACER_NFORMATS,
    350       1.4      kent 					 &sc->sc_encodings)) {
    351      1.29  jmcneill 		mutex_destroy(&sc->sc_lock);
    352      1.29  jmcneill 		mutex_destroy(&sc->sc_intr_lock);
    353       1.4      kent 		return;
    354       1.4      kent 	}
    355       1.4      kent 
    356      1.29  jmcneill 	mutex_enter(&sc->sc_lock);
    357      1.29  jmcneill 	mutex_spin_enter(&sc->sc_intr_lock);
    358      1.29  jmcneill 	auacer_reset(sc);
    359      1.29  jmcneill 	mutex_spin_exit(&sc->sc_intr_lock);
    360      1.29  jmcneill 	mutex_exit(&sc->sc_lock);
    361      1.29  jmcneill 
    362  1.30.8.1       tls 	audio_attach_mi(&auacer_hw_if, sc, sc->sc_dev);
    363       1.1  augustss 
    364      1.18  jmcneill 	if (!pmf_device_register(self, NULL, auacer_resume))
    365      1.18  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    366       1.1  augustss }
    367       1.1  augustss 
    368  1.30.8.1       tls CFATTACH_DECL_NEW(auacer, sizeof(struct auacer_softc),
    369      1.10   thorpej     auacer_match, auacer_attach, NULL, NULL);
    370      1.10   thorpej 
    371       1.1  augustss static int
    372       1.1  augustss auacer_ready_codec(struct auacer_softc *sc, int mask)
    373       1.1  augustss {
    374       1.7      kent 	int count;
    375       1.1  augustss 
    376       1.1  augustss 	for (count = 0; count < 0x7f; count++) {
    377       1.1  augustss 		int val = READ1(sc, ALI_CSPSR);
    378       1.1  augustss 		if (val & mask)
    379       1.1  augustss 			return 0;
    380       1.1  augustss 	}
    381       1.1  augustss 
    382       1.1  augustss 	aprint_normal("auacer_ready_codec: AC97 codec ready timeout.\n");
    383       1.1  augustss 	return EBUSY;
    384       1.1  augustss }
    385       1.1  augustss 
    386       1.1  augustss static int
    387       1.1  augustss auacer_sema_codec(struct auacer_softc *sc)
    388       1.1  augustss {
    389       1.9  christos 	int ttime;
    390       1.1  augustss 
    391       1.9  christos 	ttime = 100;
    392       1.9  christos 	while (ttime-- && (READ4(sc, ALI_CAS) & ALI_CAS_SEM_BUSY))
    393       1.1  augustss 		delay(1);
    394       1.9  christos 	if (!ttime)
    395       1.1  augustss 		aprint_normal("auacer_sema_codec: timeout\n");
    396       1.1  augustss 	return auacer_ready_codec(sc, ALI_CSPSR_CODEC_READY);
    397       1.1  augustss }
    398       1.1  augustss 
    399      1.10   thorpej static int
    400       1.7      kent auacer_read_codec(void *v, uint8_t reg, uint16_t *val)
    401       1.1  augustss {
    402       1.7      kent 	struct auacer_softc *sc;
    403       1.1  augustss 
    404       1.7      kent 	sc = v;
    405       1.1  augustss 	if (auacer_sema_codec(sc))
    406       1.1  augustss 		return EIO;
    407       1.1  augustss 
    408       1.1  augustss 	reg |= ALI_CPR_ADDR_READ;
    409       1.1  augustss #if 0
    410       1.1  augustss 	if (ac97->num)
    411       1.1  augustss 		reg |= ALI_CPR_ADDR_SECONDARY;
    412       1.1  augustss #endif
    413       1.1  augustss 	WRITE2(sc, ALI_CPR_ADDR, reg);
    414       1.1  augustss 	if (auacer_ready_codec(sc, ALI_CSPSR_READ_OK))
    415       1.1  augustss 		return EIO;
    416       1.1  augustss 	*val = READ2(sc, ALI_SPR);
    417       1.1  augustss 
    418       1.1  augustss 	DPRINTF(ALI_DEBUG_CODECIO, ("auacer_read_codec: reg=0x%x val=0x%x\n",
    419       1.1  augustss 				    reg, *val));
    420       1.1  augustss 
    421       1.1  augustss 	return 0;
    422       1.1  augustss }
    423       1.1  augustss 
    424       1.1  augustss int
    425       1.7      kent auacer_write_codec(void *v, uint8_t reg, uint16_t val)
    426       1.1  augustss {
    427       1.7      kent 	struct auacer_softc *sc;
    428       1.1  augustss 
    429       1.1  augustss 	DPRINTF(ALI_DEBUG_CODECIO, ("auacer_write_codec: reg=0x%x val=0x%x\n",
    430       1.1  augustss 				    reg, val));
    431       1.7      kent 	sc = v;
    432       1.1  augustss 	if (auacer_sema_codec(sc))
    433       1.1  augustss 		return EIO;
    434       1.1  augustss 	WRITE2(sc, ALI_CPR, val);
    435       1.1  augustss #if 0
    436       1.1  augustss 	if (ac97->num)
    437       1.1  augustss 		reg |= ALI_CPR_ADDR_SECONDARY;
    438       1.1  augustss #endif
    439       1.1  augustss 	WRITE2(sc, ALI_CPR_ADDR, reg);
    440       1.1  augustss 	auacer_ready_codec(sc, ALI_CSPSR_WRITE_OK);
    441       1.1  augustss 	return 0;
    442       1.1  augustss }
    443       1.1  augustss 
    444      1.10   thorpej static int
    445       1.1  augustss auacer_attach_codec(void *v, struct ac97_codec_if *cif)
    446       1.1  augustss {
    447       1.7      kent 	struct auacer_softc *sc;
    448       1.1  augustss 
    449       1.7      kent 	sc = v;
    450       1.1  augustss 	sc->codec_if = cif;
    451       1.1  augustss 	return 0;
    452       1.1  augustss }
    453       1.1  augustss 
    454      1.10   thorpej static int
    455       1.1  augustss auacer_reset_codec(void *v)
    456       1.1  augustss {
    457       1.7      kent 	struct auacer_softc *sc;
    458       1.7      kent 	uint32_t reg;
    459       1.7      kent 	int i;
    460       1.1  augustss 
    461       1.7      kent 	sc = v;
    462       1.7      kent 	i = 0;
    463       1.1  augustss 	reg = READ4(sc, ALI_SCR);
    464       1.1  augustss 	if ((reg & 2) == 0)	/* Cold required */
    465       1.1  augustss 		reg |= 2;
    466       1.1  augustss 	else
    467       1.1  augustss 		reg |= 1;	/* Warm */
    468       1.1  augustss 	reg &= ~0x80000000;	/* ACLink on */
    469       1.1  augustss 	WRITE4(sc, ALI_SCR, reg);
    470       1.1  augustss 
    471       1.1  augustss 	while (i < 10) {
    472       1.1  augustss 		if ((READ4(sc, ALI_INTERRUPTSR) & ALI_INT_GPIO) == 0)
    473       1.1  augustss 			break;
    474       1.1  augustss 		delay(50000);	/* XXX */
    475       1.1  augustss 		i++;
    476       1.1  augustss 	}
    477       1.1  augustss 	if (i == 10) {
    478       1.1  augustss 		return EIO;
    479       1.1  augustss 	}
    480       1.1  augustss 
    481       1.1  augustss 	for (i = 0; i < 10; i++) {
    482       1.1  augustss 		reg = READ4(sc, ALI_RTSR);
    483       1.1  augustss 		if (reg & 0x80) /* primary codec */
    484       1.1  augustss 			break;
    485       1.1  augustss 		WRITE4(sc, ALI_RTSR, reg | 0x80);
    486       1.1  augustss 		delay(50000);	/* XXX */
    487       1.1  augustss 	}
    488       1.1  augustss 
    489       1.1  augustss 	return 0;
    490       1.1  augustss }
    491       1.1  augustss 
    492       1.1  augustss static void
    493       1.1  augustss auacer_reset(struct auacer_softc *sc)
    494       1.1  augustss {
    495       1.1  augustss 	WRITE4(sc, ALI_SCR, ALI_SCR_RESET);
    496       1.1  augustss 	WRITE4(sc, ALI_FIFOCR1, 0x83838383);
    497       1.1  augustss 	WRITE4(sc, ALI_FIFOCR2, 0x83838383);
    498       1.1  augustss 	WRITE4(sc, ALI_FIFOCR3, 0x83838383);
    499       1.1  augustss 	WRITE4(sc, ALI_INTERFACECR, ALI_IF_PO); /* XXX pcm out only */
    500       1.1  augustss 	WRITE4(sc, ALI_INTERRUPTCR, 0x00000000);
    501       1.1  augustss 	WRITE4(sc, ALI_INTERRUPTSR, 0x00000000);
    502       1.1  augustss }
    503       1.1  augustss 
    504      1.10   thorpej static int
    505       1.1  augustss auacer_query_encoding(void *v, struct audio_encoding *aep)
    506       1.1  augustss {
    507       1.4      kent 	struct auacer_softc *sc;
    508       1.4      kent 
    509       1.1  augustss 	DPRINTF(ALI_DEBUG_API, ("auacer_query_encoding\n"));
    510       1.4      kent 	sc = v;
    511       1.4      kent 	return auconv_query_encoding(sc->sc_encodings, aep);
    512       1.1  augustss }
    513       1.1  augustss 
    514      1.10   thorpej static int
    515       1.6      kent auacer_set_rate(struct auacer_softc *sc, int mode, u_int srate)
    516       1.1  augustss {
    517       1.1  augustss 	int ret;
    518       1.6      kent 	u_int ratetmp;
    519       1.1  augustss 
    520       1.8  augustss 	DPRINTF(ALI_DEBUG_API, ("auacer_set_rate: srate=%u\n", srate));
    521       1.1  augustss 
    522       1.1  augustss 	ratetmp = srate;
    523       1.1  augustss 	if (mode == AUMODE_RECORD)
    524       1.1  augustss 		return sc->codec_if->vtbl->set_rate(sc->codec_if,
    525       1.1  augustss 		    AC97_REG_PCM_LR_ADC_RATE, &ratetmp);
    526       1.1  augustss 	ret = sc->codec_if->vtbl->set_rate(sc->codec_if,
    527       1.1  augustss 	    AC97_REG_PCM_FRONT_DAC_RATE, &ratetmp);
    528       1.1  augustss 	if (ret)
    529       1.1  augustss 		return ret;
    530       1.1  augustss 	ratetmp = srate;
    531       1.1  augustss 	ret = sc->codec_if->vtbl->set_rate(sc->codec_if,
    532       1.1  augustss 	    AC97_REG_PCM_SURR_DAC_RATE, &ratetmp);
    533       1.1  augustss 	if (ret)
    534       1.1  augustss 		return ret;
    535       1.1  augustss 	ratetmp = srate;
    536       1.1  augustss 	ret = sc->codec_if->vtbl->set_rate(sc->codec_if,
    537       1.1  augustss 	    AC97_REG_PCM_LFE_DAC_RATE, &ratetmp);
    538       1.1  augustss 	return ret;
    539       1.1  augustss }
    540       1.1  augustss 
    541      1.10   thorpej static int
    542      1.15  christos auacer_set_params(void *v, int setmode, int usemode,
    543      1.14  christos     audio_params_t *play, audio_params_t *rec, stream_filter_list_t *pfil,
    544      1.14  christos     stream_filter_list_t *rfil)
    545       1.1  augustss {
    546       1.7      kent 	struct auacer_softc *sc;
    547       1.1  augustss 	struct audio_params *p;
    548       1.6      kent 	stream_filter_list_t *fil;
    549       1.1  augustss 	uint32_t control;
    550       1.4      kent 	int mode, index;
    551       1.1  augustss 
    552       1.1  augustss 	DPRINTF(ALI_DEBUG_API, ("auacer_set_params\n"));
    553       1.7      kent 	sc = v;
    554       1.1  augustss 	for (mode = AUMODE_RECORD; mode != -1;
    555       1.1  augustss 	     mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
    556       1.1  augustss 		if ((setmode & mode) == 0)
    557       1.1  augustss 			continue;
    558       1.1  augustss 
    559       1.1  augustss 		p = mode == AUMODE_PLAY ? play : rec;
    560       1.1  augustss 		if (p == NULL)
    561       1.1  augustss 			continue;
    562       1.1  augustss 
    563       1.1  augustss 		if ((p->sample_rate !=  8000) &&
    564       1.1  augustss 		    (p->sample_rate != 11025) &&
    565       1.1  augustss 		    (p->sample_rate != 12000) &&
    566       1.1  augustss 		    (p->sample_rate != 16000) &&
    567       1.1  augustss 		    (p->sample_rate != 22050) &&
    568       1.1  augustss 		    (p->sample_rate != 24000) &&
    569       1.1  augustss 		    (p->sample_rate != 32000) &&
    570       1.1  augustss 		    (p->sample_rate != 44100) &&
    571       1.1  augustss 		    (p->sample_rate != 48000))
    572       1.1  augustss 			return (EINVAL);
    573       1.1  augustss 
    574       1.6      kent 		fil = mode == AUMODE_PLAY ? pfil : rfil;
    575       1.4      kent 		index = auconv_set_converter(sc->sc_formats, AUACER_NFORMATS,
    576       1.6      kent 					     mode, p, TRUE, fil);
    577       1.4      kent 		if (index < 0)
    578       1.4      kent 			return EINVAL;
    579       1.6      kent 		if (fil->req_size > 0)
    580       1.6      kent 			p = &fil->filters[0].param;
    581       1.6      kent 		/* p points HW encoding */
    582       1.4      kent 		if (sc->sc_formats[index].frequency_type != 1
    583       1.6      kent 		    && auacer_set_rate(sc, mode, p->sample_rate))
    584       1.4      kent 			return EINVAL;
    585       1.1  augustss 		if (mode == AUMODE_PLAY) {
    586       1.1  augustss 			control = READ4(sc, ALI_SCR);
    587       1.1  augustss 			control &= ~ALI_SCR_PCM_246_MASK;
    588       1.1  augustss 			if (p->channels == 4)
    589       1.1  augustss 				control |= ALI_SCR_PCM_4;
    590       1.1  augustss 			else if (p->channels == 6)
    591       1.1  augustss 				control |= ALI_SCR_PCM_6;
    592       1.1  augustss 			WRITE4(sc, ALI_SCR, control);
    593       1.1  augustss 		}
    594       1.1  augustss 	}
    595       1.1  augustss 
    596       1.1  augustss 	return (0);
    597       1.1  augustss }
    598       1.1  augustss 
    599      1.10   thorpej static int
    600      1.15  christos auacer_round_blocksize(void *v, int blk, int mode,
    601      1.15  christos     const audio_params_t *param)
    602       1.1  augustss {
    603       1.1  augustss 
    604       1.7      kent 	return blk & ~0x3f;		/* keep good alignment */
    605       1.1  augustss }
    606       1.1  augustss 
    607       1.1  augustss static void
    608       1.1  augustss auacer_halt(struct auacer_softc *sc, struct auacer_chan *chan)
    609       1.1  augustss {
    610       1.1  augustss 	uint32_t val;
    611       1.7      kent 	uint8_t port;
    612       1.1  augustss 	uint32_t slot;
    613       1.1  augustss 
    614       1.7      kent 	port = chan->port;
    615       1.1  augustss 	DPRINTF(ALI_DEBUG_API, ("auacer_halt: port=0x%x\n", port));
    616       1.1  augustss 	chan->intr = 0;
    617       1.1  augustss 
    618       1.1  augustss 	slot = ALI_PORT2SLOT(port);
    619       1.1  augustss 
    620       1.1  augustss 	val = READ4(sc, ALI_DMACR);
    621       1.1  augustss 	val |= 1 << (slot+16); /* pause */
    622       1.1  augustss 	val &= ~(1 << slot); /* no start */
    623       1.1  augustss 	WRITE4(sc, ALI_DMACR, val);
    624       1.1  augustss 	WRITE1(sc, port + ALI_OFF_CR, 0);
    625       1.1  augustss 	while (READ1(sc, port + ALI_OFF_CR))
    626       1.1  augustss 		;
    627       1.1  augustss 	/* reset whole DMA things */
    628       1.1  augustss 	WRITE1(sc, port + ALI_OFF_CR, ALI_CR_RR);
    629       1.1  augustss 	/* clear interrupts */
    630       1.1  augustss 	WRITE1(sc, port + ALI_OFF_SR, READ1(sc, port+ALI_OFF_SR) | ALI_SR_W1TC);
    631       1.1  augustss 	WRITE4(sc, ALI_INTERRUPTSR, ALI_PORT2INTR(port));
    632       1.1  augustss }
    633       1.1  augustss 
    634      1.10   thorpej static int
    635       1.1  augustss auacer_halt_output(void *v)
    636       1.1  augustss {
    637       1.7      kent 	struct auacer_softc *sc;
    638       1.1  augustss 
    639       1.1  augustss 	DPRINTF(ALI_DEBUG_DMA, ("auacer_halt_output\n"));
    640       1.7      kent 	sc = v;
    641       1.1  augustss 	auacer_halt(sc, &sc->sc_pcmo);
    642       1.1  augustss 
    643       1.7      kent 	return 0;
    644       1.1  augustss }
    645       1.1  augustss 
    646      1.10   thorpej static int
    647      1.15  christos auacer_halt_input(void *v)
    648       1.1  augustss {
    649       1.1  augustss 	DPRINTF(ALI_DEBUG_DMA, ("auacer_halt_input\n"));
    650       1.1  augustss 
    651       1.7      kent 	return 0;
    652       1.1  augustss }
    653       1.1  augustss 
    654      1.10   thorpej static int
    655       1.1  augustss auacer_getdev(void *v, struct audio_device *adp)
    656       1.1  augustss {
    657       1.7      kent 	struct auacer_softc *sc;
    658       1.1  augustss 
    659       1.1  augustss 	DPRINTF(ALI_DEBUG_API, ("auacer_getdev\n"));
    660       1.7      kent 	sc = v;
    661       1.1  augustss 	*adp = sc->sc_audev;
    662       1.7      kent 	return 0;
    663       1.1  augustss }
    664       1.1  augustss 
    665      1.10   thorpej static int
    666       1.1  augustss auacer_set_port(void *v, mixer_ctrl_t *cp)
    667       1.1  augustss {
    668       1.7      kent 	struct auacer_softc *sc;
    669       1.1  augustss 
    670       1.1  augustss 	DPRINTF(ALI_DEBUG_MIXERAPI, ("auacer_set_port\n"));
    671       1.7      kent 	sc = v;
    672       1.7      kent 	return sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp);
    673       1.1  augustss }
    674       1.1  augustss 
    675      1.10   thorpej static int
    676       1.1  augustss auacer_get_port(void *v, mixer_ctrl_t *cp)
    677       1.1  augustss {
    678       1.7      kent 	struct auacer_softc *sc;
    679       1.1  augustss 
    680       1.1  augustss 	DPRINTF(ALI_DEBUG_MIXERAPI, ("auacer_get_port\n"));
    681       1.7      kent 	sc = v;
    682       1.7      kent 	return sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp);
    683       1.1  augustss }
    684       1.1  augustss 
    685      1.10   thorpej static int
    686       1.1  augustss auacer_query_devinfo(void *v, mixer_devinfo_t *dp)
    687       1.1  augustss {
    688       1.7      kent 	struct auacer_softc *sc;
    689       1.1  augustss 
    690       1.1  augustss 	DPRINTF(ALI_DEBUG_MIXERAPI, ("auacer_query_devinfo\n"));
    691       1.7      kent 	sc = v;
    692       1.7      kent 	return sc->codec_if->vtbl->query_devinfo(sc->codec_if, dp);
    693       1.1  augustss }
    694       1.1  augustss 
    695      1.10   thorpej static void *
    696      1.29  jmcneill auacer_allocm(void *v, int direction, size_t size)
    697       1.1  augustss {
    698       1.7      kent 	struct auacer_softc *sc;
    699       1.1  augustss 	struct auacer_dma *p;
    700       1.1  augustss 	int error;
    701       1.1  augustss 
    702       1.1  augustss 	if (size > (ALI_DMALIST_MAX * ALI_DMASEG_MAX))
    703       1.7      kent 		return NULL;
    704       1.1  augustss 
    705      1.29  jmcneill 	p = kmem_zalloc(sizeof(*p), KM_SLEEP);
    706       1.1  augustss 	if (p == NULL)
    707       1.7      kent 		return NULL;
    708       1.7      kent 	sc = v;
    709       1.1  augustss 	error = auacer_allocmem(sc, size, 0, p);
    710       1.1  augustss 	if (error) {
    711      1.29  jmcneill 		kmem_free(p, sizeof(*p));
    712       1.7      kent 		return NULL;
    713       1.1  augustss 	}
    714       1.1  augustss 
    715       1.1  augustss 	p->next = sc->sc_dmas;
    716       1.1  augustss 	sc->sc_dmas = p;
    717       1.1  augustss 
    718       1.7      kent 	return KERNADDR(p);
    719       1.1  augustss }
    720       1.1  augustss 
    721      1.10   thorpej static void
    722      1.29  jmcneill auacer_freem(void *v, void *ptr, size_t size)
    723       1.1  augustss {
    724       1.7      kent 	struct auacer_softc *sc;
    725       1.1  augustss 	struct auacer_dma *p, **pp;
    726       1.1  augustss 
    727       1.7      kent 	sc = v;
    728       1.1  augustss 	for (pp = &sc->sc_dmas; (p = *pp) != NULL; pp = &p->next) {
    729       1.1  augustss 		if (KERNADDR(p) == ptr) {
    730       1.1  augustss 			auacer_freemem(sc, p);
    731       1.1  augustss 			*pp = p->next;
    732      1.29  jmcneill 			kmem_free(p, sizeof(*p));
    733       1.1  augustss 			return;
    734       1.1  augustss 		}
    735       1.1  augustss 	}
    736       1.1  augustss }
    737       1.1  augustss 
    738      1.10   thorpej static size_t
    739      1.15  christos auacer_round_buffersize(void *v, int direction, size_t size)
    740       1.1  augustss {
    741       1.1  augustss 
    742       1.1  augustss 	if (size > (ALI_DMALIST_MAX * ALI_DMASEG_MAX))
    743       1.1  augustss 		size = ALI_DMALIST_MAX * ALI_DMASEG_MAX;
    744       1.1  augustss 
    745       1.1  augustss 	return size;
    746       1.1  augustss }
    747       1.1  augustss 
    748      1.10   thorpej static paddr_t
    749       1.1  augustss auacer_mappage(void *v, void *mem, off_t off, int prot)
    750       1.1  augustss {
    751       1.7      kent 	struct auacer_softc *sc;
    752       1.1  augustss 	struct auacer_dma *p;
    753       1.1  augustss 
    754       1.1  augustss 	if (off < 0)
    755       1.7      kent 		return -1;
    756       1.7      kent 	sc = v;
    757       1.1  augustss 	for (p = sc->sc_dmas; p && KERNADDR(p) != mem; p = p->next)
    758       1.7      kent 		continue;
    759       1.7      kent 	if (p == NULL)
    760       1.7      kent 		return -1;
    761       1.7      kent 	return bus_dmamem_mmap(sc->dmat, p->segs, p->nsegs,
    762       1.7      kent 	    off, prot, BUS_DMA_WAITOK);
    763       1.1  augustss }
    764       1.1  augustss 
    765      1.10   thorpej static int
    766       1.1  augustss auacer_get_props(void *v)
    767       1.1  augustss {
    768       1.7      kent 	struct auacer_softc *sc;
    769       1.1  augustss 	int props;
    770       1.1  augustss 
    771       1.7      kent 	sc = v;
    772       1.1  augustss 	props = AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX;
    773       1.1  augustss 	/*
    774       1.1  augustss 	 * Even if the codec is fixed-rate, set_param() succeeds for any sample
    775       1.1  augustss 	 * rate because of aurateconv.  Applications can't know what rate the
    776       1.1  augustss 	 * device can process in the case of mmap().
    777       1.1  augustss 	 */
    778       1.3      kent 	if (!AC97_IS_FIXED_RATE(sc->codec_if))
    779       1.1  augustss 		props |= AUDIO_PROP_MMAP;
    780       1.1  augustss 	return props;
    781       1.1  augustss }
    782       1.1  augustss 
    783       1.1  augustss static void
    784      1.29  jmcneill auacer_get_locks(void *v, kmutex_t **intr, kmutex_t **proc)
    785      1.29  jmcneill {
    786      1.29  jmcneill 	struct auacer_softc *sc;
    787      1.29  jmcneill 
    788      1.29  jmcneill 	sc = v;
    789      1.29  jmcneill 	*intr = &sc->sc_intr_lock;
    790      1.29  jmcneill 	*proc = &sc->sc_lock;
    791      1.29  jmcneill }
    792      1.29  jmcneill 
    793      1.29  jmcneill static void
    794       1.1  augustss auacer_add_entry(struct auacer_chan *chan)
    795       1.1  augustss {
    796       1.1  augustss 	struct auacer_dmalist *q;
    797       1.1  augustss 
    798       1.1  augustss 	q = &chan->dmalist[chan->ptr];
    799       1.1  augustss 
    800       1.1  augustss 	DPRINTF(ALI_DEBUG_INTR,
    801       1.1  augustss 		("auacer_add_entry: %p = %x @ 0x%x\n",
    802       1.1  augustss 		 q, chan->blksize / 2, chan->p));
    803       1.1  augustss 
    804       1.1  augustss 	q->base = htole32(chan->p);
    805       1.1  augustss 	q->len = htole32((chan->blksize / ALI_SAMPLE_SIZE) | ALI_DMAF_IOC);
    806       1.1  augustss 	chan->p += chan->blksize;
    807       1.1  augustss 	if (chan->p >= chan->end)
    808       1.1  augustss 		chan->p = chan->start;
    809       1.7      kent 
    810       1.1  augustss 	if (++chan->ptr >= ALI_DMALIST_MAX)
    811       1.1  augustss 		chan->ptr = 0;
    812       1.1  augustss }
    813       1.1  augustss 
    814       1.1  augustss static void
    815       1.1  augustss auacer_upd_chan(struct auacer_softc *sc, struct auacer_chan *chan)
    816       1.1  augustss {
    817       1.1  augustss 	uint32_t sts;
    818       1.1  augustss 	uint32_t civ;
    819       1.1  augustss 
    820       1.1  augustss 	sts = READ2(sc, chan->port + ALI_OFF_SR);
    821       1.1  augustss 	/* intr ack */
    822       1.1  augustss 	WRITE2(sc, chan->port + ALI_OFF_SR, sts & ALI_SR_W1TC);
    823       1.1  augustss 	WRITE4(sc, ALI_INTERRUPTSR, ALI_PORT2INTR(chan->port));
    824       1.1  augustss 
    825       1.1  augustss 	DPRINTF(ALI_DEBUG_INTR, ("auacer_upd_chan: sts=0x%x\n", sts));
    826       1.1  augustss 
    827       1.1  augustss 	if (sts & ALI_SR_DMA_INT_FIFO) {
    828       1.1  augustss 		printf("%s: fifo underrun # %u\n",
    829  1.30.8.1       tls 		       device_xname(sc->sc_dev), ++chan->fifoe);
    830       1.1  augustss 	}
    831       1.1  augustss 
    832       1.1  augustss 	civ = READ1(sc, chan->port + ALI_OFF_CIV);
    833       1.7      kent 
    834       1.1  augustss 	DPRINTF(ALI_DEBUG_INTR,("auacer_intr: civ=%u ptr=%u\n",civ,chan->ptr));
    835       1.7      kent 
    836       1.1  augustss 	/* XXX */
    837       1.1  augustss 	while (chan->ptr != civ) {
    838       1.1  augustss 		auacer_add_entry(chan);
    839       1.1  augustss 	}
    840       1.1  augustss 
    841       1.1  augustss 	WRITE1(sc, chan->port + ALI_OFF_LVI, (chan->ptr - 1) & ALI_LVI_MASK);
    842       1.1  augustss 
    843       1.1  augustss 	while (chan->ack != civ) {
    844       1.1  augustss 		if (chan->intr) {
    845       1.1  augustss 			DPRINTF(ALI_DEBUG_INTR,("auacer_upd_chan: callback\n"));
    846       1.1  augustss 			chan->intr(chan->arg);
    847       1.1  augustss 		}
    848       1.1  augustss 		chan->ack++;
    849       1.1  augustss 		if (chan->ack >= ALI_DMALIST_MAX)
    850       1.1  augustss 			chan->ack = 0;
    851       1.1  augustss 	}
    852       1.1  augustss }
    853       1.1  augustss 
    854      1.10   thorpej static int
    855       1.1  augustss auacer_intr(void *v)
    856       1.1  augustss {
    857       1.7      kent 	struct auacer_softc *sc;
    858       1.1  augustss 	int ret, intrs;
    859       1.1  augustss 
    860       1.7      kent 	sc = v;
    861      1.29  jmcneill 
    862      1.29  jmcneill 	DPRINTF(ALI_DEBUG_INTR, ("auacer_intr: intrs=0x%x\n",
    863      1.29  jmcneill 	    READ4(sc, ALI_INTERRUPTSR)));
    864      1.29  jmcneill 
    865      1.29  jmcneill 	mutex_spin_enter(&sc->sc_intr_lock);
    866       1.1  augustss 	intrs = READ4(sc, ALI_INTERRUPTSR);
    867       1.1  augustss 	ret = 0;
    868       1.1  augustss 	if (intrs & ALI_INT_PCMOUT) {
    869       1.1  augustss 		auacer_upd_chan(sc, &sc->sc_pcmo);
    870       1.1  augustss 		ret++;
    871       1.1  augustss 	}
    872      1.29  jmcneill 	mutex_spin_exit(&sc->sc_intr_lock);
    873       1.1  augustss 
    874       1.1  augustss 	return ret != 0;
    875       1.1  augustss }
    876       1.1  augustss 
    877       1.1  augustss static void
    878       1.1  augustss auacer_setup_chan(struct auacer_softc *sc, struct auacer_chan *chan,
    879       1.1  augustss 		  uint32_t start, uint32_t size, uint32_t blksize,
    880       1.1  augustss 		  void (*intr)(void *), void *arg)
    881       1.1  augustss {
    882       1.1  augustss 	uint32_t port, slot;
    883       1.1  augustss 	uint32_t offs, val;
    884       1.1  augustss 
    885       1.1  augustss 	chan->start = start;
    886       1.1  augustss 	chan->ptr = 0;
    887       1.1  augustss 	chan->p = chan->start;
    888       1.1  augustss 	chan->end = chan->start + size;
    889       1.1  augustss 	chan->blksize = blksize;
    890       1.1  augustss 	chan->ack = 0;
    891       1.1  augustss 	chan->intr = intr;
    892       1.1  augustss 	chan->arg = arg;
    893       1.1  augustss 
    894       1.1  augustss 	auacer_add_entry(chan);
    895       1.1  augustss 	auacer_add_entry(chan);
    896       1.1  augustss 
    897       1.1  augustss 	port = chan->port;
    898       1.1  augustss 	slot = ALI_PORT2SLOT(port);
    899       1.1  augustss 
    900       1.1  augustss 	WRITE1(sc, port + ALI_OFF_CIV, 0);
    901       1.1  augustss 	WRITE1(sc, port + ALI_OFF_LVI, (chan->ptr - 1) & ALI_LVI_MASK);
    902       1.1  augustss 	offs = (char *)chan->dmalist - (char *)sc->sc_cdata;
    903       1.1  augustss 	WRITE4(sc, port + ALI_OFF_BDBAR, sc->sc_cddma + offs);
    904       1.1  augustss 	WRITE1(sc, port + ALI_OFF_CR,
    905       1.1  augustss 	       ALI_CR_IOCE | ALI_CR_FEIE | ALI_CR_LVBIE | ALI_CR_RPBM);
    906       1.1  augustss 	val = READ4(sc, ALI_DMACR);
    907       1.1  augustss 	val &= ~(1 << (slot+16)); /* no pause */
    908       1.1  augustss 	val |= 1 << slot;	/* start */
    909       1.1  augustss 	WRITE4(sc, ALI_DMACR, val);
    910       1.1  augustss }
    911       1.1  augustss 
    912      1.10   thorpej static int
    913       1.1  augustss auacer_trigger_output(void *v, void *start, void *end, int blksize,
    914      1.15  christos     void (*intr)(void *), void *arg, const audio_params_t *param)
    915       1.1  augustss {
    916       1.7      kent 	struct auacer_softc *sc;
    917       1.1  augustss 	struct auacer_dma *p;
    918       1.1  augustss 	uint32_t size;
    919       1.1  augustss 
    920       1.1  augustss 	DPRINTF(ALI_DEBUG_DMA,
    921       1.1  augustss 		("auacer_trigger_output(%p, %p, %d, %p, %p, %p)\n",
    922       1.1  augustss 		 start, end, blksize, intr, arg, param));
    923       1.7      kent 	sc = v;
    924       1.1  augustss 	for (p = sc->sc_dmas; p && KERNADDR(p) != start; p = p->next)
    925       1.7      kent 		continue;
    926       1.1  augustss 	if (!p) {
    927       1.1  augustss 		printf("auacer_trigger_output: bad addr %p\n", start);
    928       1.1  augustss 		return (EINVAL);
    929       1.1  augustss 	}
    930       1.1  augustss 
    931       1.1  augustss 	size = (char *)end - (char *)start;
    932       1.1  augustss 	auacer_setup_chan(sc, &sc->sc_pcmo, DMAADDR(p), size, blksize,
    933       1.1  augustss 			  intr, arg);
    934       1.1  augustss 
    935       1.1  augustss 	return 0;
    936       1.1  augustss }
    937       1.1  augustss 
    938      1.10   thorpej static int
    939      1.15  christos auacer_trigger_input(void *v, void *start, void *end,
    940      1.15  christos     int blksize, void (*intr)(void *), void *arg,
    941      1.15  christos     const audio_params_t *param)
    942       1.1  augustss {
    943       1.7      kent 	return EINVAL;
    944       1.1  augustss }
    945       1.1  augustss 
    946      1.10   thorpej static int
    947       1.1  augustss auacer_allocmem(struct auacer_softc *sc, size_t size, size_t align,
    948       1.1  augustss     struct auacer_dma *p)
    949       1.1  augustss {
    950       1.1  augustss 	int error;
    951       1.1  augustss 
    952       1.1  augustss 	p->size = size;
    953       1.1  augustss 	error = bus_dmamem_alloc(sc->dmat, p->size, align, 0,
    954       1.1  augustss 				 p->segs, sizeof(p->segs)/sizeof(p->segs[0]),
    955      1.29  jmcneill 				 &p->nsegs, BUS_DMA_WAITOK);
    956       1.1  augustss 	if (error)
    957       1.7      kent 		return error;
    958       1.1  augustss 
    959       1.1  augustss 	error = bus_dmamem_map(sc->dmat, p->segs, p->nsegs, p->size,
    960      1.29  jmcneill 			       &p->addr, BUS_DMA_WAITOK|sc->sc_dmamap_flags);
    961       1.1  augustss 	if (error)
    962       1.1  augustss 		goto free;
    963       1.1  augustss 
    964       1.1  augustss 	error = bus_dmamap_create(sc->dmat, p->size, 1, p->size,
    965      1.29  jmcneill 				  0, BUS_DMA_WAITOK, &p->map);
    966       1.1  augustss 	if (error)
    967       1.1  augustss 		goto unmap;
    968       1.1  augustss 
    969       1.1  augustss 	error = bus_dmamap_load(sc->dmat, p->map, p->addr, p->size, NULL,
    970      1.29  jmcneill 				BUS_DMA_WAITOK);
    971       1.1  augustss 	if (error)
    972       1.1  augustss 		goto destroy;
    973       1.1  augustss 	return (0);
    974       1.1  augustss 
    975       1.1  augustss  destroy:
    976       1.1  augustss 	bus_dmamap_destroy(sc->dmat, p->map);
    977       1.1  augustss  unmap:
    978       1.1  augustss 	bus_dmamem_unmap(sc->dmat, p->addr, p->size);
    979       1.1  augustss  free:
    980       1.1  augustss 	bus_dmamem_free(sc->dmat, p->segs, p->nsegs);
    981       1.7      kent 	return error;
    982       1.1  augustss }
    983       1.1  augustss 
    984      1.10   thorpej static int
    985       1.1  augustss auacer_freemem(struct auacer_softc *sc, struct auacer_dma *p)
    986       1.1  augustss {
    987       1.1  augustss 
    988       1.1  augustss 	bus_dmamap_unload(sc->dmat, p->map);
    989       1.1  augustss 	bus_dmamap_destroy(sc->dmat, p->map);
    990       1.1  augustss 	bus_dmamem_unmap(sc->dmat, p->addr, p->size);
    991       1.1  augustss 	bus_dmamem_free(sc->dmat, p->segs, p->nsegs);
    992       1.7      kent 	return 0;
    993       1.1  augustss }
    994       1.1  augustss 
    995      1.10   thorpej static int
    996       1.1  augustss auacer_alloc_cdata(struct auacer_softc *sc)
    997       1.1  augustss {
    998       1.1  augustss 	bus_dma_segment_t seg;
    999       1.1  augustss 	int error, rseg;
   1000       1.1  augustss 
   1001       1.1  augustss 	/*
   1002       1.1  augustss 	 * Allocate the control data structure, and create and load the
   1003       1.1  augustss 	 * DMA map for it.
   1004       1.1  augustss 	 */
   1005       1.1  augustss 	if ((error = bus_dmamem_alloc(sc->dmat,
   1006       1.1  augustss 				      sizeof(struct auacer_cdata),
   1007       1.1  augustss 				      PAGE_SIZE, 0, &seg, 1, &rseg, 0)) != 0) {
   1008  1.30.8.1       tls 		aprint_error_dev(sc->sc_dev, "unable to allocate control data, error = %d\n",
   1009      1.20    cegger 		    error);
   1010       1.1  augustss 		goto fail_0;
   1011       1.1  augustss 	}
   1012       1.1  augustss 
   1013       1.1  augustss 	if ((error = bus_dmamem_map(sc->dmat, &seg, rseg,
   1014       1.1  augustss 				    sizeof(struct auacer_cdata),
   1015      1.16  christos 				    (void **) &sc->sc_cdata,
   1016       1.1  augustss 				    sc->sc_dmamap_flags)) != 0) {
   1017  1.30.8.1       tls 		aprint_error_dev(sc->sc_dev, "unable to map control data, error = %d\n",
   1018      1.20    cegger 		    error);
   1019       1.1  augustss 		goto fail_1;
   1020       1.1  augustss 	}
   1021       1.1  augustss 
   1022       1.1  augustss 	if ((error = bus_dmamap_create(sc->dmat, sizeof(struct auacer_cdata), 1,
   1023       1.1  augustss 				       sizeof(struct auacer_cdata), 0, 0,
   1024       1.1  augustss 				       &sc->sc_cddmamap)) != 0) {
   1025  1.30.8.1       tls 		aprint_error_dev(sc->sc_dev, "unable to create control data DMA map, "
   1026      1.20    cegger 		    "error = %d\n", error);
   1027       1.1  augustss 		goto fail_2;
   1028       1.1  augustss 	}
   1029       1.1  augustss 
   1030       1.1  augustss 	if ((error = bus_dmamap_load(sc->dmat, sc->sc_cddmamap,
   1031       1.1  augustss 				     sc->sc_cdata, sizeof(struct auacer_cdata),
   1032       1.1  augustss 				     NULL, 0)) != 0) {
   1033  1.30.8.1       tls 		aprint_error_dev(sc->sc_dev, "unable to load control data DMA map, "
   1034      1.20    cegger 		    "error = %d\n", error);
   1035       1.1  augustss 		goto fail_3;
   1036       1.1  augustss 	}
   1037       1.1  augustss 
   1038       1.7      kent 	return 0;
   1039       1.1  augustss 
   1040       1.1  augustss  fail_3:
   1041       1.1  augustss 	bus_dmamap_destroy(sc->dmat, sc->sc_cddmamap);
   1042       1.1  augustss  fail_2:
   1043      1.16  christos 	bus_dmamem_unmap(sc->dmat, (void *) sc->sc_cdata,
   1044       1.1  augustss 	    sizeof(struct auacer_cdata));
   1045       1.1  augustss  fail_1:
   1046       1.1  augustss 	bus_dmamem_free(sc->dmat, &seg, rseg);
   1047       1.1  augustss  fail_0:
   1048       1.7      kent 	return error;
   1049       1.1  augustss }
   1050       1.1  augustss 
   1051      1.18  jmcneill static bool
   1052      1.27    dyoung auacer_resume(device_t dv, const pmf_qual_t *qual)
   1053       1.1  augustss {
   1054      1.18  jmcneill 	struct auacer_softc *sc = device_private(dv);
   1055      1.18  jmcneill 
   1056      1.29  jmcneill 	mutex_enter(&sc->sc_lock);
   1057      1.29  jmcneill 	mutex_spin_enter(&sc->sc_intr_lock);
   1058      1.18  jmcneill 	auacer_reset_codec(sc);
   1059      1.29  jmcneill 	mutex_spin_exit(&sc->sc_intr_lock);
   1060      1.18  jmcneill 	delay(1000);
   1061      1.18  jmcneill 	sc->codec_if->vtbl->restore_ports(sc->codec_if);
   1062      1.29  jmcneill 	mutex_exit(&sc->sc_lock);
   1063       1.1  augustss 
   1064      1.18  jmcneill 	return true;
   1065       1.1  augustss }
   1066