Home | History | Annotate | Line # | Download | only in pci
cs4281.c revision 1.48.2.1
      1  1.48.2.1     rmind /*	$NetBSD: cs4281.c,v 1.48.2.1 2014/05/18 17:45:39 rmind Exp $	*/
      2       1.1  augustss 
      3       1.1  augustss /*
      4       1.1  augustss  * Copyright (c) 2000 Tatoku Ogaito.  All rights reserved.
      5       1.1  augustss  *
      6       1.1  augustss  * Redistribution and use in source and binary forms, with or without
      7       1.1  augustss  * modification, are permitted provided that the following conditions
      8       1.1  augustss  * are met:
      9       1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     10       1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     11       1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     12       1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     13       1.1  augustss  *    documentation and/or other materials provided with the distribution.
     14       1.1  augustss  * 3. All advertising materials mentioning features or use of this software
     15       1.1  augustss  *    must display the following acknowledgement:
     16       1.1  augustss  *      This product includes software developed by Tatoku Ogaito
     17       1.1  augustss  *	for the NetBSD Project.
     18       1.1  augustss  * 4. The name of the author may not be used to endorse or promote products
     19       1.1  augustss  *    derived from this software without specific prior written permission
     20       1.1  augustss  *
     21       1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22       1.1  augustss  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23       1.1  augustss  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24       1.1  augustss  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25       1.1  augustss  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26       1.1  augustss  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27       1.1  augustss  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28       1.1  augustss  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29       1.1  augustss  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30       1.1  augustss  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31       1.1  augustss  */
     32       1.1  augustss 
     33       1.1  augustss /*
     34       1.1  augustss  * Cirrus Logic CS4281 driver.
     35       1.1  augustss  * Data sheets can be found
     36       1.1  augustss  * http://www.cirrus.com/ftp/pub/4281.pdf
     37       1.1  augustss  * ftp://ftp.alsa-project.org/pub/manuals/cirrus/cs4281tm.pdf
     38       1.1  augustss  *
     39       1.1  augustss  * TODO:
     40       1.3     tacha  *   1: midi and FM support
     41       1.3     tacha  *   2: ...
     42       1.1  augustss  *
     43       1.1  augustss  */
     44       1.7     lukem 
     45       1.7     lukem #include <sys/cdefs.h>
     46  1.48.2.1     rmind __KERNEL_RCSID(0, "$NetBSD: cs4281.c,v 1.48.2.1 2014/05/18 17:45:39 rmind Exp $");
     47       1.1  augustss 
     48       1.1  augustss #include <sys/param.h>
     49       1.1  augustss #include <sys/systm.h>
     50       1.1  augustss #include <sys/kernel.h>
     51       1.1  augustss #include <sys/malloc.h>
     52       1.1  augustss #include <sys/fcntl.h>
     53       1.1  augustss #include <sys/device.h>
     54       1.1  augustss #include <sys/systm.h>
     55       1.1  augustss 
     56       1.1  augustss #include <dev/pci/pcidevs.h>
     57       1.1  augustss #include <dev/pci/pcivar.h>
     58       1.1  augustss #include <dev/pci/cs4281reg.h>
     59       1.1  augustss #include <dev/pci/cs428xreg.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/midi_if.h>
     64       1.1  augustss #include <dev/mulaw.h>
     65       1.1  augustss #include <dev/auconv.h>
     66       1.1  augustss 
     67       1.1  augustss #include <dev/ic/ac97reg.h>
     68       1.1  augustss #include <dev/ic/ac97var.h>
     69       1.1  augustss 
     70       1.1  augustss #include <dev/pci/cs428x.h>
     71       1.1  augustss 
     72      1.35        ad #include <sys/bus.h>
     73       1.1  augustss 
     74       1.1  augustss #if defined(ENABLE_SECONDARY_CODEC)
     75       1.1  augustss #define MAX_CHANNELS  (4)
     76       1.1  augustss #define MAX_FIFO_SIZE 32 /* 128/4channels */
     77       1.1  augustss #else
     78       1.1  augustss #define MAX_CHANNELS  (2)
     79       1.1  augustss #define MAX_FIFO_SIZE 64 /* 128/2channels */
     80       1.1  augustss #endif
     81       1.1  augustss 
     82       1.1  augustss /* IF functions for audio driver */
     83      1.41    cegger static int	cs4281_match(device_t, cfdata_t, void *);
     84      1.41    cegger static void	cs4281_attach(device_t, device_t, void *);
     85      1.26   thorpej static int	cs4281_intr(void *);
     86      1.26   thorpej static int	cs4281_query_encoding(void *, struct audio_encoding *);
     87      1.26   thorpej static int	cs4281_set_params(void *, int, int, audio_params_t *,
     88      1.26   thorpej 				  audio_params_t *, stream_filter_list_t *,
     89      1.26   thorpej 				  stream_filter_list_t *);
     90      1.26   thorpej static int	cs4281_halt_output(void *);
     91      1.26   thorpej static int	cs4281_halt_input(void *);
     92      1.26   thorpej static int	cs4281_getdev(void *, struct audio_device *);
     93      1.26   thorpej static int	cs4281_trigger_output(void *, void *, void *, int,
     94      1.26   thorpej 				      void (*)(void *), void *,
     95      1.26   thorpej 				      const audio_params_t *);
     96      1.26   thorpej static int	cs4281_trigger_input(void *, void *, void *, int,
     97      1.26   thorpej 				     void (*)(void *), void *,
     98      1.26   thorpej 				     const audio_params_t *);
     99       1.1  augustss 
    100      1.26   thorpej static int     cs4281_reset_codec(void *);
    101       1.3     tacha 
    102       1.1  augustss /* Internal functions */
    103      1.26   thorpej static uint8_t cs4281_sr2regval(int);
    104      1.26   thorpej static void	 cs4281_set_dac_rate(struct cs428x_softc *, int);
    105      1.26   thorpej static void	 cs4281_set_adc_rate(struct cs428x_softc *, int);
    106      1.26   thorpej static int      cs4281_init(struct cs428x_softc *, int);
    107       1.1  augustss 
    108       1.1  augustss /* Power Management */
    109      1.44    dyoung static bool cs4281_suspend(device_t, const pmf_qual_t *);
    110      1.44    dyoung static bool cs4281_resume(device_t, const pmf_qual_t *);
    111       1.1  augustss 
    112      1.26   thorpej static const struct audio_hw_if cs4281_hw_if = {
    113      1.22      kent 	NULL,			/* open */
    114      1.22      kent 	NULL,			/* close */
    115       1.1  augustss 	NULL,
    116       1.1  augustss 	cs4281_query_encoding,
    117       1.1  augustss 	cs4281_set_params,
    118       1.3     tacha 	cs428x_round_blocksize,
    119       1.1  augustss 	NULL,
    120       1.1  augustss 	NULL,
    121       1.1  augustss 	NULL,
    122       1.1  augustss 	NULL,
    123       1.1  augustss 	NULL,
    124       1.1  augustss 	cs4281_halt_output,
    125       1.1  augustss 	cs4281_halt_input,
    126       1.1  augustss 	NULL,
    127       1.1  augustss 	cs4281_getdev,
    128       1.1  augustss 	NULL,
    129       1.3     tacha 	cs428x_mixer_set_port,
    130       1.3     tacha 	cs428x_mixer_get_port,
    131       1.3     tacha 	cs428x_query_devinfo,
    132       1.3     tacha 	cs428x_malloc,
    133       1.3     tacha 	cs428x_free,
    134       1.3     tacha 	cs428x_round_buffersize,
    135       1.3     tacha 	cs428x_mappage,
    136       1.3     tacha 	cs428x_get_props,
    137       1.1  augustss 	cs4281_trigger_output,
    138       1.1  augustss 	cs4281_trigger_input,
    139       1.6  augustss 	NULL,
    140      1.45  jmcneill 	cs428x_get_locks,
    141       1.1  augustss };
    142       1.1  augustss 
    143       1.2  augustss #if NMIDI > 0 && 0
    144       1.1  augustss /* Midi Interface */
    145      1.26   thorpej static void	cs4281_midi_close(void*);
    146      1.26   thorpej static void	cs4281_midi_getinfo(void *, struct midi_info *);
    147      1.26   thorpej static int	cs4281_midi_open(void *, int, void (*)(void *, int),
    148      1.23      kent 			 void (*)(void *), void *);
    149      1.26   thorpej static int	cs4281_midi_output(void *, int);
    150       1.1  augustss 
    151      1.26   thorpej static const struct midi_hw_if cs4281_midi_hw_if = {
    152       1.1  augustss 	cs4281_midi_open,
    153       1.1  augustss 	cs4281_midi_close,
    154       1.1  augustss 	cs4281_midi_output,
    155       1.1  augustss 	cs4281_midi_getinfo,
    156       1.1  augustss 	0,
    157      1.45  jmcneill 	cs428x_get_locks,
    158       1.1  augustss };
    159       1.1  augustss #endif
    160       1.1  augustss 
    161      1.48       chs CFATTACH_DECL_NEW(clct, sizeof(struct cs428x_softc),
    162      1.13   thorpej     cs4281_match, cs4281_attach, NULL, NULL);
    163       1.1  augustss 
    164      1.26   thorpej static struct audio_device cs4281_device = {
    165       1.1  augustss 	"CS4281",
    166       1.1  augustss 	"",
    167       1.1  augustss 	"cs4281"
    168       1.1  augustss };
    169       1.1  augustss 
    170       1.1  augustss 
    171      1.26   thorpej static int
    172      1.41    cegger cs4281_match(device_t parent, cfdata_t match, void *aux)
    173       1.1  augustss {
    174      1.23      kent 	struct pci_attach_args *pa;
    175      1.23      kent 
    176      1.23      kent 	pa = (struct pci_attach_args *)aux;
    177       1.1  augustss 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_CIRRUS)
    178       1.1  augustss 		return 0;
    179       1.1  augustss 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_CIRRUS_CS4281)
    180       1.1  augustss 		return 1;
    181       1.1  augustss 	return 0;
    182       1.1  augustss }
    183       1.1  augustss 
    184      1.26   thorpej static void
    185      1.41    cegger cs4281_attach(device_t parent, device_t self, void *aux)
    186       1.1  augustss {
    187      1.23      kent 	struct cs428x_softc *sc;
    188      1.23      kent 	struct pci_attach_args *pa;
    189      1.23      kent 	pci_chipset_tag_t pc;
    190       1.1  augustss 	char const *intrstr;
    191       1.3     tacha 	pcireg_t reg;
    192      1.29  christos 	int error;
    193  1.48.2.1     rmind 	char intrbuf[PCI_INTRSTR_LEN];
    194       1.1  augustss 
    195      1.42    cegger 	sc = device_private(self);
    196      1.48       chs 	sc->sc_dev = self;
    197      1.23      kent 	pa = (struct pci_attach_args *)aux;
    198      1.23      kent 	pc = pa->pa_pc;
    199      1.15   thorpej 
    200      1.47  drochner 	pci_aprint_devinfo(pa, "Audio controller");
    201       1.1  augustss 
    202      1.34     joerg 	sc->sc_pc = pa->pa_pc;
    203      1.34     joerg 	sc->sc_pt = pa->pa_tag;
    204      1.34     joerg 
    205       1.1  augustss 	/* Map I/O register */
    206       1.1  augustss 	if (pci_mapreg_map(pa, PCI_BA0,
    207       1.1  augustss 	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
    208       1.1  augustss 	    &sc->ba0t, &sc->ba0h, NULL, NULL)) {
    209      1.48       chs 		aprint_error_dev(sc->sc_dev, "can't map BA0 space\n");
    210       1.1  augustss 		return;
    211       1.1  augustss 	}
    212       1.1  augustss 	if (pci_mapreg_map(pa, PCI_BA1,
    213       1.1  augustss 	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
    214       1.1  augustss 	    &sc->ba1t, &sc->ba1h, NULL, NULL)) {
    215      1.48       chs 		aprint_error_dev(sc->sc_dev, "can't map BA1 space\n");
    216       1.1  augustss 		return;
    217       1.1  augustss 	}
    218       1.1  augustss 
    219       1.1  augustss 	sc->sc_dmatag = pa->pa_dmat;
    220       1.1  augustss 
    221      1.29  christos 	/* power up chip */
    222      1.38    dyoung 	if ((error = pci_activate(pa->pa_pc, pa->pa_tag, self,
    223      1.29  christos 	    pci_activate_null)) && error != EOPNOTSUPP) {
    224      1.48       chs 		aprint_error_dev(sc->sc_dev, "cannot activate %d\n", error);
    225      1.29  christos 		return;
    226       1.3     tacha 	}
    227       1.3     tacha 
    228       1.1  augustss 	/* Enable the device (set bus master flag) */
    229       1.3     tacha 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    230       1.1  augustss 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    231       1.3     tacha 	    reg | PCI_COMMAND_MASTER_ENABLE);
    232       1.1  augustss 
    233       1.1  augustss #if 0
    234       1.1  augustss 	/* LATENCY_TIMER setting */
    235       1.1  augustss 	temp1 = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG);
    236      1.10    simonb 	if (PCI_LATTIMER(temp1) < 32) {
    237       1.1  augustss 		temp1 &= 0xffff00ff;
    238       1.1  augustss 		temp1 |= 0x00002000;
    239       1.1  augustss 		pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_BHLC_REG, temp1);
    240       1.1  augustss 	}
    241       1.1  augustss #endif
    242      1.22      kent 
    243       1.1  augustss 	/* Map and establish the interrupt. */
    244      1.34     joerg 	if (pci_intr_map(pa, &sc->intrh)) {
    245      1.48       chs 		aprint_error_dev(sc->sc_dev, "couldn't map interrupt\n");
    246       1.1  augustss 		return;
    247       1.1  augustss 	}
    248  1.48.2.1     rmind 	intrstr = pci_intr_string(pc, sc->intrh, intrbuf, sizeof(intrbuf));
    249       1.1  augustss 
    250      1.45  jmcneill 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    251      1.46       mrg 	mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_AUDIO);
    252      1.45  jmcneill 
    253      1.46       mrg 	sc->sc_ih = pci_intr_establish(sc->sc_pc, sc->intrh, IPL_AUDIO,
    254      1.34     joerg 	    cs4281_intr, sc);
    255       1.1  augustss 	if (sc->sc_ih == NULL) {
    256      1.48       chs 		aprint_error_dev(sc->sc_dev, "couldn't establish interrupt");
    257       1.1  augustss 		if (intrstr != NULL)
    258      1.39    dyoung 			aprint_error(" at %s", intrstr);
    259      1.39    dyoung 		aprint_error("\n");
    260      1.45  jmcneill 		mutex_destroy(&sc->sc_lock);
    261      1.45  jmcneill 		mutex_destroy(&sc->sc_intr_lock);
    262       1.1  augustss 		return;
    263       1.1  augustss 	}
    264      1.48       chs 	aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
    265       1.1  augustss 
    266       1.1  augustss 	/*
    267       1.1  augustss 	 * Sound System start-up
    268       1.1  augustss 	 */
    269      1.45  jmcneill 	if (cs4281_init(sc, 1) != 0) {
    270      1.45  jmcneill 		mutex_destroy(&sc->sc_lock);
    271      1.45  jmcneill 		mutex_destroy(&sc->sc_intr_lock);
    272       1.1  augustss 		return;
    273      1.45  jmcneill 	}
    274       1.1  augustss 
    275       1.1  augustss 	sc->type = TYPE_CS4281;
    276       1.1  augustss 	sc->halt_input  = cs4281_halt_input;
    277       1.1  augustss 	sc->halt_output = cs4281_halt_output;
    278       1.1  augustss 
    279       1.1  augustss 	sc->dma_size     = CS4281_BUFFER_SIZE / MAX_CHANNELS;
    280       1.1  augustss 	sc->dma_align    = 0x10;
    281       1.1  augustss 	sc->hw_blocksize = sc->dma_size / 2;
    282      1.22      kent 
    283       1.1  augustss 	/* AC 97 attachment */
    284       1.1  augustss 	sc->host_if.arg = sc;
    285       1.3     tacha 	sc->host_if.attach = cs428x_attach_codec;
    286       1.3     tacha 	sc->host_if.read   = cs428x_read_codec;
    287       1.3     tacha 	sc->host_if.write  = cs428x_write_codec;
    288       1.1  augustss 	sc->host_if.reset  = cs4281_reset_codec;
    289      1.45  jmcneill 	if (ac97_attach(&sc->host_if, self, &sc->sc_lock) != 0) {
    290      1.48       chs 		aprint_error_dev(sc->sc_dev, "ac97_attach failed\n");
    291      1.45  jmcneill 		mutex_destroy(&sc->sc_lock);
    292      1.45  jmcneill 		mutex_destroy(&sc->sc_intr_lock);
    293       1.1  augustss 		return;
    294       1.1  augustss 	}
    295      1.48       chs 	audio_attach_mi(&cs4281_hw_if, sc, sc->sc_dev);
    296       1.1  augustss 
    297       1.2  augustss #if NMIDI > 0 && 0
    298      1.48       chs 	midi_attach_mi(&cs4281_midi_hw_if, sc, sc->sc_dev);
    299       1.1  augustss #endif
    300       1.1  augustss 
    301      1.36  jmcneill 	if (!pmf_device_register(self, cs4281_suspend, cs4281_resume))
    302      1.36  jmcneill 		aprint_error_dev(self, "couldn't establish power handler\n");
    303       1.1  augustss }
    304       1.1  augustss 
    305      1.26   thorpej static int
    306      1.23      kent cs4281_intr(void *p)
    307       1.1  augustss {
    308      1.23      kent 	struct cs428x_softc *sc;
    309      1.23      kent 	uint32_t intr, hdsr0, hdsr1;
    310       1.1  augustss 	char *empty_dma;
    311      1.23      kent 	int handled;
    312       1.1  augustss 
    313      1.23      kent 	sc = p;
    314      1.23      kent 	handled = 0;
    315       1.1  augustss 	hdsr0 = 0;
    316       1.1  augustss 	hdsr1 = 0;
    317      1.23      kent 
    318      1.45  jmcneill 	mutex_spin_enter(&sc->sc_intr_lock);
    319      1.45  jmcneill 
    320       1.1  augustss 	/* grab interrupt register */
    321       1.1  augustss 	intr = BA0READ4(sc, CS4281_HISR);
    322       1.1  augustss 
    323       1.1  augustss 	DPRINTF(("cs4281_intr:"));
    324       1.1  augustss 	/* not for me */
    325       1.1  augustss 	if ((intr & HISR_INTENA) == 0) {
    326       1.1  augustss 		/* clear the interrupt register */
    327       1.1  augustss 		BA0WRITE4(sc, CS4281_HICR, HICR_CHGM | HICR_IEV);
    328      1.45  jmcneill 		mutex_spin_exit(&sc->sc_intr_lock);
    329       1.1  augustss 		return 0;
    330       1.1  augustss 	}
    331       1.1  augustss 
    332       1.1  augustss 	if (intr & HISR_DMA0)
    333       1.1  augustss 		hdsr0 = BA0READ4(sc, CS4281_HDSR0); /* clear intr condition */
    334       1.1  augustss 	if (intr & HISR_DMA1)
    335       1.1  augustss 		hdsr1 = BA0READ4(sc, CS4281_HDSR1); /* clear intr condition */
    336       1.1  augustss 	/* clear the interrupt register */
    337       1.1  augustss 	BA0WRITE4(sc, CS4281_HICR, HICR_CHGM | HICR_IEV);
    338      1.23      kent 
    339  1.48.2.1     rmind #ifdef CS4280_DEBUG
    340       1.1  augustss 	DPRINTF(("intr = 0x%08x, hdsr0 = 0x%08x hdsr1 = 0x%08x\n",
    341       1.1  augustss 		 intr, hdsr0, hdsr1));
    342  1.48.2.1     rmind #else
    343  1.48.2.1     rmind 	__USE(hdsr0);
    344  1.48.2.1     rmind 	__USE(hdsr1);
    345  1.48.2.1     rmind #endif
    346      1.23      kent 
    347       1.1  augustss 	/* Playback Interrupt */
    348       1.1  augustss 	if (intr & HISR_DMA0) {
    349       1.3     tacha 		handled = 1;
    350      1.18   mycroft 		if (sc->sc_prun) {
    351      1.28  jmcneill 			DPRINTF((" PB DMA 0x%x(%d)",
    352      1.28  jmcneill 				(int)BA0READ4(sc, CS4281_DCA0),
    353      1.28  jmcneill 				(int)BA0READ4(sc, CS4281_DCC0)));
    354       1.1  augustss 			if ((sc->sc_pi%sc->sc_pcount) == 0)
    355       1.1  augustss 				sc->sc_pintr(sc->sc_parg);
    356      1.28  jmcneill 			/* copy buffer */
    357      1.28  jmcneill 			++sc->sc_pi;
    358      1.28  jmcneill 			empty_dma = sc->sc_pdma->addr;
    359      1.28  jmcneill 			if (sc->sc_pi&1)
    360      1.28  jmcneill 				empty_dma += sc->hw_blocksize;
    361      1.28  jmcneill 			memcpy(empty_dma, sc->sc_pn, sc->hw_blocksize);
    362      1.28  jmcneill 			sc->sc_pn += sc->hw_blocksize;
    363      1.28  jmcneill 			if (sc->sc_pn >= sc->sc_pe)
    364      1.28  jmcneill 				sc->sc_pn = sc->sc_ps;
    365       1.1  augustss 		} else {
    366      1.48       chs 			aprint_error_dev(sc->sc_dev, "unexpected play intr\n");
    367       1.1  augustss 		}
    368       1.1  augustss 	}
    369       1.1  augustss 	if (intr & HISR_DMA1) {
    370       1.3     tacha 		handled = 1;
    371      1.18   mycroft 		if (sc->sc_rrun) {
    372      1.28  jmcneill 			/* copy from DMA */
    373      1.28  jmcneill 			DPRINTF((" CP DMA 0x%x(%d)", (int)BA0READ4(sc, CS4281_DCA1),
    374      1.28  jmcneill 				(int)BA0READ4(sc, CS4281_DCC1)));
    375      1.28  jmcneill 			++sc->sc_ri;
    376      1.28  jmcneill 			empty_dma = sc->sc_rdma->addr;
    377      1.28  jmcneill 			if ((sc->sc_ri & 1) == 0)
    378      1.28  jmcneill 				empty_dma += sc->hw_blocksize;
    379      1.28  jmcneill 			memcpy(sc->sc_rn, empty_dma, sc->hw_blocksize);
    380      1.28  jmcneill 			sc->sc_rn += sc->hw_blocksize;
    381      1.28  jmcneill 			if (sc->sc_rn >= sc->sc_re)
    382      1.28  jmcneill 				sc->sc_rn = sc->sc_rs;
    383       1.1  augustss 			if ((sc->sc_ri % sc->sc_rcount) == 0)
    384       1.1  augustss 				sc->sc_rintr(sc->sc_rarg);
    385       1.1  augustss 		} else {
    386      1.48       chs 			aprint_error_dev(sc->sc_dev,
    387      1.39    dyoung 			    "unexpected record intr\n");
    388       1.1  augustss 		}
    389       1.1  augustss 	}
    390       1.1  augustss 	DPRINTF(("\n"));
    391       1.3     tacha 
    392      1.45  jmcneill 	mutex_spin_exit(&sc->sc_intr_lock);
    393      1.45  jmcneill 
    394       1.3     tacha 	return handled;
    395       1.1  augustss }
    396       1.1  augustss 
    397      1.26   thorpej static int
    398      1.33  christos cs4281_query_encoding(void *addr, struct audio_encoding *fp)
    399       1.1  augustss {
    400      1.10    simonb 
    401       1.1  augustss 	switch (fp->index) {
    402       1.1  augustss 	case 0:
    403       1.1  augustss 		strcpy(fp->name, AudioEulinear);
    404       1.1  augustss 		fp->encoding = AUDIO_ENCODING_ULINEAR;
    405       1.1  augustss 		fp->precision = 8;
    406       1.1  augustss 		fp->flags = 0;
    407       1.1  augustss 		break;
    408       1.1  augustss 	case 1:
    409       1.1  augustss 		strcpy(fp->name, AudioEmulaw);
    410       1.1  augustss 		fp->encoding = AUDIO_ENCODING_ULAW;
    411       1.1  augustss 		fp->precision = 8;
    412       1.1  augustss 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    413       1.1  augustss 		break;
    414       1.1  augustss 	case 2:
    415       1.1  augustss 		strcpy(fp->name, AudioEalaw);
    416       1.1  augustss 		fp->encoding = AUDIO_ENCODING_ALAW;
    417       1.1  augustss 		fp->precision = 8;
    418       1.1  augustss 		fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
    419       1.1  augustss 		break;
    420       1.1  augustss 	case 3:
    421       1.1  augustss 		strcpy(fp->name, AudioEslinear);
    422       1.1  augustss 		fp->encoding = AUDIO_ENCODING_SLINEAR;
    423       1.1  augustss 		fp->precision = 8;
    424       1.1  augustss 		fp->flags = 0;
    425       1.1  augustss 		break;
    426       1.1  augustss 	case 4:
    427       1.1  augustss 		strcpy(fp->name, AudioEslinear_le);
    428       1.1  augustss 		fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
    429       1.1  augustss 		fp->precision = 16;
    430       1.1  augustss 		fp->flags = 0;
    431       1.1  augustss 		break;
    432       1.1  augustss 	case 5:
    433       1.1  augustss 		strcpy(fp->name, AudioEulinear_le);
    434       1.1  augustss 		fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
    435       1.1  augustss 		fp->precision = 16;
    436       1.1  augustss 		fp->flags = 0;
    437       1.1  augustss 		break;
    438       1.1  augustss 	case 6:
    439       1.1  augustss 		strcpy(fp->name, AudioEslinear_be);
    440       1.1  augustss 		fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
    441       1.1  augustss 		fp->precision = 16;
    442       1.1  augustss 		fp->flags = 0;
    443       1.1  augustss 		break;
    444       1.1  augustss 	case 7:
    445       1.1  augustss 		strcpy(fp->name, AudioEulinear_be);
    446       1.1  augustss 		fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
    447       1.1  augustss 		fp->precision = 16;
    448       1.1  augustss 		fp->flags = 0;
    449       1.1  augustss 		break;
    450       1.1  augustss 	default:
    451       1.1  augustss 		return EINVAL;
    452       1.1  augustss 	}
    453       1.1  augustss 	return 0;
    454       1.1  augustss }
    455       1.1  augustss 
    456      1.26   thorpej static int
    457      1.33  christos cs4281_set_params(void *addr, int setmode, int usemode,
    458      1.32  christos     audio_params_t *play, audio_params_t *rec, stream_filter_list_t *pfil,
    459      1.32  christos     stream_filter_list_t *rfil)
    460       1.1  augustss {
    461      1.22      kent 	audio_params_t hw;
    462      1.23      kent 	struct cs428x_softc *sc;
    463      1.22      kent 	audio_params_t *p;
    464      1.22      kent 	stream_filter_list_t *fil;
    465       1.1  augustss 	int mode;
    466       1.1  augustss 
    467      1.23      kent 	sc = addr;
    468       1.1  augustss 	for (mode = AUMODE_RECORD; mode != -1;
    469       1.1  augustss 	    mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
    470       1.1  augustss 		if ((setmode & mode) == 0)
    471       1.1  augustss 			continue;
    472      1.22      kent 
    473       1.1  augustss 		p = mode == AUMODE_PLAY ? play : rec;
    474      1.22      kent 
    475       1.1  augustss 		if (p == play) {
    476      1.25      yamt 			DPRINTFN(5,
    477      1.25      yamt 			    ("play: sample=%u precision=%u channels=%u\n",
    478      1.25      yamt 			    p->sample_rate, p->precision, p->channels));
    479       1.1  augustss 			if (p->sample_rate < 6023 || p->sample_rate > 48000 ||
    480       1.1  augustss 			    (p->precision != 8 && p->precision != 16) ||
    481       1.1  augustss 			    (p->channels != 1  && p->channels != 2)) {
    482      1.23      kent 				return EINVAL;
    483       1.1  augustss 			}
    484       1.1  augustss 		} else {
    485      1.25      yamt 			DPRINTFN(5,
    486      1.25      yamt 			    ("rec: sample=%u precision=%u channels=%u\n",
    487      1.25      yamt 			    p->sample_rate, p->precision, p->channels));
    488       1.1  augustss 			if (p->sample_rate < 6023 || p->sample_rate > 48000 ||
    489       1.1  augustss 			    (p->precision != 8 && p->precision != 16) ||
    490       1.1  augustss 			    (p->channels != 1 && p->channels != 2)) {
    491      1.23      kent 				return EINVAL;
    492       1.1  augustss 			}
    493       1.1  augustss 		}
    494      1.22      kent 		hw = *p;
    495      1.22      kent 		fil = mode == AUMODE_PLAY ? pfil : rfil;
    496       1.1  augustss 
    497       1.1  augustss 		switch (p->encoding) {
    498       1.1  augustss 		case AUDIO_ENCODING_SLINEAR_BE:
    499       1.1  augustss 			break;
    500       1.1  augustss 		case AUDIO_ENCODING_SLINEAR_LE:
    501       1.1  augustss 			break;
    502       1.1  augustss 		case AUDIO_ENCODING_ULINEAR_BE:
    503       1.1  augustss 			break;
    504       1.1  augustss 		case AUDIO_ENCODING_ULINEAR_LE:
    505       1.1  augustss 			break;
    506       1.1  augustss 		case AUDIO_ENCODING_ULAW:
    507      1.22      kent 			hw.encoding = AUDIO_ENCODING_SLINEAR_LE;
    508      1.22      kent 			fil->append(fil, mode == AUMODE_PLAY ? mulaw_to_linear8
    509      1.22      kent 				    :  linear8_to_mulaw, &hw);
    510       1.1  augustss 			break;
    511       1.1  augustss 		case AUDIO_ENCODING_ALAW:
    512      1.22      kent 			hw.encoding = AUDIO_ENCODING_SLINEAR_LE;
    513      1.22      kent 			fil->append(fil, mode == AUMODE_PLAY ? alaw_to_linear8
    514      1.22      kent 				    : linear8_to_alaw, &hw);
    515       1.1  augustss 			break;
    516       1.1  augustss 		default:
    517      1.23      kent 			return EINVAL;
    518       1.1  augustss 		}
    519       1.1  augustss 	}
    520       1.1  augustss 
    521       1.1  augustss 	/* set sample rate */
    522       1.1  augustss 	cs4281_set_dac_rate(sc, play->sample_rate);
    523       1.1  augustss 	cs4281_set_adc_rate(sc, rec->sample_rate);
    524       1.1  augustss 	return 0;
    525       1.1  augustss }
    526       1.1  augustss 
    527      1.26   thorpej static int
    528      1.23      kent cs4281_halt_output(void *addr)
    529       1.1  augustss {
    530      1.23      kent 	struct cs428x_softc *sc;
    531      1.23      kent 
    532      1.23      kent 	sc = addr;
    533       1.1  augustss 	BA0WRITE4(sc, CS4281_DCR0, BA0READ4(sc, CS4281_DCR0) | DCRn_MSK);
    534       1.1  augustss 	sc->sc_prun = 0;
    535       1.1  augustss 	return 0;
    536       1.1  augustss }
    537       1.1  augustss 
    538      1.26   thorpej static int
    539      1.23      kent cs4281_halt_input(void *addr)
    540       1.1  augustss {
    541      1.23      kent 	struct cs428x_softc *sc;
    542       1.1  augustss 
    543      1.23      kent 	sc = addr;
    544       1.1  augustss 	BA0WRITE4(sc, CS4281_DCR1, BA0READ4(sc, CS4281_DCR1) | DCRn_MSK);
    545       1.1  augustss 	sc->sc_rrun = 0;
    546       1.1  augustss 	return 0;
    547       1.1  augustss }
    548       1.1  augustss 
    549      1.26   thorpej static int
    550      1.33  christos cs4281_getdev(void *addr, struct audio_device *retp)
    551       1.1  augustss {
    552      1.10    simonb 
    553       1.1  augustss 	*retp = cs4281_device;
    554       1.1  augustss 	return 0;
    555       1.1  augustss }
    556       1.1  augustss 
    557      1.26   thorpej static int
    558      1.23      kent cs4281_trigger_output(void *addr, void *start, void *end, int blksize,
    559      1.23      kent 		      void (*intr)(void *), void *arg,
    560      1.23      kent 		      const audio_params_t *param)
    561       1.1  augustss {
    562      1.23      kent 	struct cs428x_softc *sc;
    563      1.23      kent 	uint32_t fmt;
    564       1.1  augustss 	struct cs428x_dma *p;
    565       1.1  augustss 	int dma_count;
    566       1.1  augustss 
    567      1.23      kent 	sc = addr;
    568      1.23      kent 	fmt = 0;
    569       1.1  augustss #ifdef DIAGNOSTIC
    570       1.1  augustss 	if (sc->sc_prun)
    571       1.1  augustss 		printf("cs4281_trigger_output: already running\n");
    572       1.4     tacha #endif
    573       1.1  augustss 	sc->sc_prun = 1;
    574       1.1  augustss 
    575       1.1  augustss 	DPRINTF(("cs4281_trigger_output: sc=%p start=%p end=%p "
    576       1.1  augustss 		 "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
    577       1.1  augustss 	sc->sc_pintr = intr;
    578       1.1  augustss 	sc->sc_parg  = arg;
    579       1.1  augustss 
    580       1.1  augustss 	/* stop playback DMA */
    581       1.1  augustss 	BA0WRITE4(sc, CS4281_DCR0, BA0READ4(sc, CS4281_DCR0) | DCRn_MSK);
    582       1.1  augustss 
    583      1.22      kent 	DPRINTF(("param: precision=%d channels=%d encoding=%d\n",
    584      1.22      kent 	       param->precision, param->channels, param->encoding));
    585       1.1  augustss 	for (p = sc->sc_dmas; p != NULL && BUFADDR(p) != start; p = p->next)
    586      1.23      kent 		continue;
    587       1.1  augustss 	if (p == NULL) {
    588       1.1  augustss 		printf("cs4281_trigger_output: bad addr %p\n", start);
    589      1.23      kent 		return EINVAL;
    590       1.1  augustss 	}
    591       1.1  augustss 
    592       1.1  augustss 	sc->sc_pcount = blksize / sc->hw_blocksize;
    593       1.1  augustss 	sc->sc_ps = (char *)start;
    594       1.1  augustss 	sc->sc_pe = (char *)end;
    595       1.1  augustss 	sc->sc_pdma = p;
    596       1.1  augustss 	sc->sc_pbuf = KERNADDR(p);
    597       1.1  augustss 	sc->sc_pi = 0;
    598       1.1  augustss 	sc->sc_pn = sc->sc_ps;
    599       1.1  augustss 	if (blksize >= sc->dma_size) {
    600       1.1  augustss 		sc->sc_pn = sc->sc_ps + sc->dma_size;
    601       1.1  augustss 		memcpy(sc->sc_pbuf, start, sc->dma_size);
    602       1.1  augustss 		++sc->sc_pi;
    603       1.1  augustss 	} else {
    604       1.1  augustss 		sc->sc_pn = sc->sc_ps + sc->hw_blocksize;
    605       1.1  augustss 		memcpy(sc->sc_pbuf, start, sc->hw_blocksize);
    606       1.1  augustss 	}
    607       1.1  augustss 
    608       1.1  augustss 	dma_count = sc->dma_size;
    609      1.22      kent 	if (param->precision != 8)
    610       1.1  augustss 		dma_count /= 2;   /* 16 bit */
    611       1.1  augustss 	if (param->channels > 1)
    612       1.1  augustss 		dma_count /= 2;   /* Stereo */
    613       1.1  augustss 
    614       1.1  augustss 	DPRINTF(("cs4281_trigger_output: DMAADDR(p)=0x%x count=%d\n",
    615       1.1  augustss 		 (int)DMAADDR(p), dma_count));
    616       1.1  augustss 	BA0WRITE4(sc, CS4281_DBA0, DMAADDR(p));
    617       1.1  augustss 	BA0WRITE4(sc, CS4281_DBC0, dma_count-1);
    618       1.1  augustss 
    619       1.1  augustss 	/* set playback format */
    620       1.1  augustss 	fmt = BA0READ4(sc, CS4281_DMR0) & ~DMRn_FMTMSK;
    621      1.22      kent 	if (param->precision == 8)
    622       1.1  augustss 		fmt |= DMRn_SIZE8;
    623       1.1  augustss 	if (param->channels == 1)
    624       1.1  augustss 		fmt |= DMRn_MONO;
    625       1.1  augustss 	if (param->encoding == AUDIO_ENCODING_ULINEAR_BE ||
    626       1.1  augustss 	    param->encoding == AUDIO_ENCODING_SLINEAR_BE)
    627       1.1  augustss 		fmt |= DMRn_BEND;
    628       1.1  augustss 	if (param->encoding == AUDIO_ENCODING_ULINEAR_BE ||
    629       1.1  augustss 	    param->encoding == AUDIO_ENCODING_ULINEAR_LE)
    630       1.1  augustss 		fmt |= DMRn_USIGN;
    631       1.1  augustss 	BA0WRITE4(sc, CS4281_DMR0, fmt);
    632       1.1  augustss 
    633       1.1  augustss 	/* set sample rate */
    634       1.4     tacha 	sc->sc_prate = param->sample_rate;
    635       1.1  augustss 	cs4281_set_dac_rate(sc, param->sample_rate);
    636       1.1  augustss 
    637       1.1  augustss 	/* start DMA */
    638       1.1  augustss 	BA0WRITE4(sc, CS4281_DCR0, BA0READ4(sc, CS4281_DCR0) & ~DCRn_MSK);
    639       1.1  augustss 	/* Enable interrupts */
    640       1.1  augustss 	BA0WRITE4(sc, CS4281_HICR, HICR_IEV | HICR_CHGM);
    641       1.1  augustss 
    642       1.1  augustss 	DPRINTF(("HICR =0x%08x(expected 0x00000001)\n", BA0READ4(sc, CS4281_HICR)));
    643       1.1  augustss 	DPRINTF(("HIMR =0x%08x(expected 0x00f0fc3f)\n", BA0READ4(sc, CS4281_HIMR)));
    644       1.1  augustss 	DPRINTF(("DMR0 =0x%08x(expected 0x2???0018)\n", BA0READ4(sc, CS4281_DMR0)));
    645       1.1  augustss 	DPRINTF(("DCR0 =0x%08x(expected 0x00030000)\n", BA0READ4(sc, CS4281_DCR0)));
    646       1.1  augustss 	DPRINTF(("FCR0 =0x%08x(expected 0x81000f00)\n", BA0READ4(sc, CS4281_FCR0)));
    647       1.1  augustss 	DPRINTF(("DACSR=0x%08x(expected 1 for 44kHz 5 for 8kHz)\n",
    648       1.1  augustss 		 BA0READ4(sc, CS4281_DACSR)));
    649       1.1  augustss 	DPRINTF(("SRCSA=0x%08x(expected 0x0b0a0100)\n", BA0READ4(sc, CS4281_SRCSA)));
    650       1.1  augustss 	DPRINTF(("SSPM&SSPM_PSRCEN =0x%08x(expected 0x00000010)\n",
    651       1.1  augustss 		 BA0READ4(sc, CS4281_SSPM) & SSPM_PSRCEN));
    652       1.1  augustss 
    653       1.1  augustss 	return 0;
    654       1.1  augustss }
    655       1.1  augustss 
    656      1.26   thorpej static int
    657      1.23      kent cs4281_trigger_input(void *addr, void *start, void *end, int blksize,
    658      1.23      kent 		     void (*intr)(void *), void *arg,
    659      1.23      kent 		     const audio_params_t *param)
    660       1.1  augustss {
    661      1.23      kent 	struct cs428x_softc *sc;
    662       1.1  augustss 	struct cs428x_dma *p;
    663      1.23      kent 	uint32_t fmt;
    664       1.1  augustss 	int dma_count;
    665       1.1  augustss 
    666      1.23      kent 	sc = addr;
    667      1.23      kent 	fmt = 0;
    668       1.1  augustss #ifdef DIAGNOSTIC
    669       1.1  augustss 	if (sc->sc_rrun)
    670       1.1  augustss 		printf("cs4281_trigger_input: already running\n");
    671       1.4     tacha #endif
    672       1.1  augustss 	sc->sc_rrun = 1;
    673       1.1  augustss 	DPRINTF(("cs4281_trigger_input: sc=%p start=%p end=%p "
    674       1.1  augustss 	    "blksize=%d intr=%p(%p)\n", addr, start, end, blksize, intr, arg));
    675       1.1  augustss 	sc->sc_rintr = intr;
    676       1.1  augustss 	sc->sc_rarg  = arg;
    677       1.1  augustss 
    678       1.1  augustss 	/* stop recording DMA */
    679       1.1  augustss 	BA0WRITE4(sc, CS4281_DCR1, BA0READ4(sc, CS4281_DCR1) | DCRn_MSK);
    680       1.1  augustss 
    681       1.1  augustss 	for (p = sc->sc_dmas; p && BUFADDR(p) != start; p = p->next)
    682      1.23      kent 		continue;
    683       1.1  augustss 	if (!p) {
    684       1.1  augustss 		printf("cs4281_trigger_input: bad addr %p\n", start);
    685      1.23      kent 		return EINVAL;
    686       1.1  augustss 	}
    687       1.1  augustss 
    688       1.1  augustss 	sc->sc_rcount = blksize / sc->hw_blocksize;
    689       1.1  augustss 	sc->sc_rs = (char *)start;
    690       1.1  augustss 	sc->sc_re = (char *)end;
    691       1.1  augustss 	sc->sc_rdma = p;
    692       1.1  augustss 	sc->sc_rbuf = KERNADDR(p);
    693       1.1  augustss 	sc->sc_ri = 0;
    694       1.1  augustss 	sc->sc_rn = sc->sc_rs;
    695       1.1  augustss 
    696       1.1  augustss 	dma_count = sc->dma_size;
    697      1.22      kent 	if (param->precision != 8)
    698       1.1  augustss 		dma_count /= 2;
    699       1.1  augustss 	if (param->channels > 1)
    700       1.1  augustss 		dma_count /= 2;
    701       1.1  augustss 
    702       1.1  augustss 	DPRINTF(("cs4281_trigger_input: DMAADDR(p)=0x%x count=%d\n",
    703       1.1  augustss 		 (int)DMAADDR(p), dma_count));
    704       1.1  augustss 	BA0WRITE4(sc, CS4281_DBA1, DMAADDR(p));
    705       1.1  augustss 	BA0WRITE4(sc, CS4281_DBC1, dma_count-1);
    706       1.1  augustss 
    707       1.1  augustss 	/* set recording format */
    708       1.1  augustss 	fmt = BA0READ4(sc, CS4281_DMR1) & ~DMRn_FMTMSK;
    709      1.22      kent 	if (param->precision == 8)
    710       1.1  augustss 		fmt |= DMRn_SIZE8;
    711       1.1  augustss 	if (param->channels == 1)
    712       1.1  augustss 		fmt |= DMRn_MONO;
    713       1.1  augustss 	if (param->encoding == AUDIO_ENCODING_ULINEAR_BE ||
    714       1.1  augustss 	    param->encoding == AUDIO_ENCODING_SLINEAR_BE)
    715       1.1  augustss 		fmt |= DMRn_BEND;
    716       1.1  augustss 	if (param->encoding == AUDIO_ENCODING_ULINEAR_BE ||
    717       1.1  augustss 	    param->encoding == AUDIO_ENCODING_ULINEAR_LE)
    718       1.1  augustss 		fmt |= DMRn_USIGN;
    719       1.1  augustss 	BA0WRITE4(sc, CS4281_DMR1, fmt);
    720       1.1  augustss 
    721       1.1  augustss 	/* set sample rate */
    722       1.4     tacha 	sc->sc_rrate = param->sample_rate;
    723       1.1  augustss 	cs4281_set_adc_rate(sc, param->sample_rate);
    724       1.1  augustss 
    725       1.1  augustss 	/* Start DMA */
    726       1.1  augustss 	BA0WRITE4(sc, CS4281_DCR1, BA0READ4(sc, CS4281_DCR1) & ~DCRn_MSK);
    727       1.1  augustss 	/* Enable interrupts */
    728       1.1  augustss 	BA0WRITE4(sc, CS4281_HICR, HICR_IEV | HICR_CHGM);
    729       1.1  augustss 
    730       1.1  augustss 	DPRINTF(("HICR=0x%08x\n", BA0READ4(sc, CS4281_HICR)));
    731       1.1  augustss 	DPRINTF(("HIMR=0x%08x\n", BA0READ4(sc, CS4281_HIMR)));
    732       1.1  augustss 	DPRINTF(("DMR1=0x%08x\n", BA0READ4(sc, CS4281_DMR1)));
    733       1.1  augustss 	DPRINTF(("DCR1=0x%08x\n", BA0READ4(sc, CS4281_DCR1)));
    734       1.1  augustss 
    735       1.1  augustss 	return 0;
    736       1.1  augustss }
    737       1.1  augustss 
    738      1.36  jmcneill static bool
    739      1.44    dyoung cs4281_suspend(device_t dv, const pmf_qual_t *qual)
    740       1.3     tacha {
    741      1.36  jmcneill 	struct cs428x_softc *sc = device_private(dv);
    742       1.3     tacha 
    743      1.45  jmcneill 	mutex_enter(&sc->sc_lock);
    744      1.45  jmcneill 	mutex_spin_exit(&sc->sc_intr_lock);
    745      1.45  jmcneill 
    746      1.36  jmcneill 	/* save current playback status */
    747      1.36  jmcneill 	if (sc->sc_prun) {
    748      1.36  jmcneill 		sc->sc_suspend_state.cs4281.dcr0 = BA0READ4(sc, CS4281_DCR0);
    749      1.36  jmcneill 		sc->sc_suspend_state.cs4281.dmr0 = BA0READ4(sc, CS4281_DMR0);
    750      1.36  jmcneill 		sc->sc_suspend_state.cs4281.dbc0 = BA0READ4(sc, CS4281_DBC0);
    751      1.36  jmcneill 		sc->sc_suspend_state.cs4281.dba0 = BA0READ4(sc, CS4281_DBA0);
    752      1.36  jmcneill 	}
    753      1.36  jmcneill 
    754      1.36  jmcneill 	/* save current capture status */
    755      1.36  jmcneill 	if (sc->sc_rrun) {
    756      1.36  jmcneill 		sc->sc_suspend_state.cs4281.dcr1 = BA0READ4(sc, CS4281_DCR1);
    757      1.36  jmcneill 		sc->sc_suspend_state.cs4281.dmr1 = BA0READ4(sc, CS4281_DMR1);
    758      1.36  jmcneill 		sc->sc_suspend_state.cs4281.dbc1 = BA0READ4(sc, CS4281_DBC1);
    759      1.36  jmcneill 		sc->sc_suspend_state.cs4281.dba1 = BA0READ4(sc, CS4281_DBA1);
    760      1.36  jmcneill 	}
    761      1.36  jmcneill 	/* Stop DMA */
    762      1.36  jmcneill 	BA0WRITE4(sc, CS4281_DCR0, BA0READ4(sc, CS4281_DCR0) | DCRn_MSK);
    763      1.36  jmcneill 	BA0WRITE4(sc, CS4281_DCR1, BA0READ4(sc, CS4281_DCR1) | DCRn_MSK);
    764       1.3     tacha 
    765      1.45  jmcneill 	mutex_spin_exit(&sc->sc_intr_lock);
    766      1.45  jmcneill 	mutex_exit(&sc->sc_lock);
    767      1.45  jmcneill 
    768      1.36  jmcneill 	return true;
    769      1.36  jmcneill }
    770       1.4     tacha 
    771      1.36  jmcneill static bool
    772      1.44    dyoung cs4281_resume(device_t dv, const pmf_qual_t *qual)
    773      1.36  jmcneill {
    774      1.36  jmcneill 	struct cs428x_softc *sc = device_private(dv);
    775      1.34     joerg 
    776      1.45  jmcneill 	mutex_enter(&sc->sc_lock);
    777      1.45  jmcneill 	mutex_spin_enter(&sc->sc_intr_lock);
    778      1.45  jmcneill 
    779      1.36  jmcneill 	cs4281_init(sc, 0);
    780      1.36  jmcneill 	cs4281_reset_codec(sc);
    781      1.34     joerg 
    782      1.36  jmcneill 	/* restore ac97 registers */
    783      1.45  jmcneill 	mutex_spin_exit(&sc->sc_intr_lock);
    784      1.36  jmcneill 	(*sc->codec_if->vtbl->restore_ports)(sc->codec_if);
    785      1.45  jmcneill 	mutex_spin_enter(&sc->sc_intr_lock);
    786       1.3     tacha 
    787      1.36  jmcneill 	/* restore DMA related status */
    788      1.36  jmcneill 	if (sc->sc_prun) {
    789      1.36  jmcneill 		cs4281_set_dac_rate(sc, sc->sc_prate);
    790      1.36  jmcneill 		BA0WRITE4(sc, CS4281_DBA0, sc->sc_suspend_state.cs4281.dba0);
    791      1.36  jmcneill 		BA0WRITE4(sc, CS4281_DBC0, sc->sc_suspend_state.cs4281.dbc0);
    792      1.36  jmcneill 		BA0WRITE4(sc, CS4281_DMR0, sc->sc_suspend_state.cs4281.dmr0);
    793      1.36  jmcneill 		BA0WRITE4(sc, CS4281_DCR0, sc->sc_suspend_state.cs4281.dcr0);
    794      1.36  jmcneill 	}
    795      1.36  jmcneill 	if (sc->sc_rrun) {
    796      1.36  jmcneill 		cs4281_set_adc_rate(sc, sc->sc_rrate);
    797      1.36  jmcneill 		BA0WRITE4(sc, CS4281_DBA1, sc->sc_suspend_state.cs4281.dba1);
    798      1.36  jmcneill 		BA0WRITE4(sc, CS4281_DBC1, sc->sc_suspend_state.cs4281.dbc1);
    799      1.36  jmcneill 		BA0WRITE4(sc, CS4281_DMR1, sc->sc_suspend_state.cs4281.dmr1);
    800      1.36  jmcneill 		BA0WRITE4(sc, CS4281_DCR1, sc->sc_suspend_state.cs4281.dcr1);
    801      1.36  jmcneill 	}
    802      1.36  jmcneill 	/* enable intterupts */
    803      1.36  jmcneill 	if (sc->sc_prun || sc->sc_rrun)
    804      1.36  jmcneill 		BA0WRITE4(sc, CS4281_HICR, HICR_IEV | HICR_CHGM);
    805       1.4     tacha 
    806      1.45  jmcneill 	mutex_spin_exit(&sc->sc_intr_lock);
    807      1.45  jmcneill 	mutex_exit(&sc->sc_lock);
    808      1.45  jmcneill 
    809      1.36  jmcneill 	return true;
    810       1.3     tacha }
    811       1.3     tacha 
    812       1.3     tacha /* control AC97 codec */
    813      1.26   thorpej static int
    814       1.3     tacha cs4281_reset_codec(void *addr)
    815       1.3     tacha {
    816       1.3     tacha 	struct cs428x_softc *sc;
    817      1.23      kent 	uint16_t data;
    818      1.23      kent 	uint32_t dat32;
    819       1.3     tacha 	int n;
    820       1.3     tacha 
    821       1.3     tacha 	sc = addr;
    822       1.3     tacha 
    823      1.10    simonb 	DPRINTFN(3, ("cs4281_reset_codec\n"));
    824       1.3     tacha 
    825       1.3     tacha 	/* Reset codec */
    826       1.3     tacha 	BA0WRITE4(sc, CS428X_ACCTL, 0);
    827       1.3     tacha 	delay(50);    /* delay 50us */
    828       1.3     tacha 
    829       1.3     tacha 	BA0WRITE4(sc, CS4281_SPMC, 0);
    830       1.3     tacha 	delay(100);	/* delay 100us */
    831       1.3     tacha 	BA0WRITE4(sc, CS4281_SPMC, SPMC_RSTN);
    832       1.3     tacha #if defined(ENABLE_SECONDARY_CODEC)
    833       1.3     tacha 	BA0WRITE4(sc, CS4281_SPMC, SPMC_RSTN | SPCM_ASDIN2E);
    834       1.3     tacha 	BA0WRITE4(sc, CS4281_SERMC, SERMC_TCID);
    835       1.3     tacha #endif
    836       1.3     tacha 	delay(50000);   /* XXX: delay 50ms */
    837       1.3     tacha 
    838       1.3     tacha 	/* Enable ASYNC generation */
    839       1.3     tacha 	BA0WRITE4(sc, CS428X_ACCTL, ACCTL_ESYN);
    840       1.3     tacha 
    841      1.10    simonb 	/* Wait for codec ready. Linux driver waits 50ms here */
    842       1.3     tacha 	n = 0;
    843      1.10    simonb 	while ((BA0READ4(sc, CS428X_ACSTS) & ACSTS_CRDY) == 0) {
    844       1.3     tacha 		delay(100);
    845       1.3     tacha 		if (++n > 1000) {
    846       1.3     tacha 			printf("reset_codec: AC97 codec ready timeout\n");
    847      1.19      kent 			return ETIMEDOUT;
    848       1.3     tacha 		}
    849       1.3     tacha 	}
    850       1.3     tacha #if defined(ENABLE_SECONDARY_CODEC)
    851       1.3     tacha 	/* secondary codec ready*/
    852       1.3     tacha 	n = 0;
    853      1.10    simonb 	while ((BA0READ4(sc, CS4281_ACSTS2) & ACSTS2_CRDY2) == 0) {
    854       1.3     tacha 		delay(100);
    855       1.3     tacha 		if (++n > 1000)
    856      1.19      kent 			return 0;
    857       1.3     tacha 	}
    858       1.3     tacha #endif
    859       1.3     tacha 	/* Set the serial timing configuration */
    860       1.3     tacha 	/* XXX: undocumented but the Linux driver do this */
    861       1.3     tacha 	BA0WRITE4(sc, CS4281_SERMC, SERMC_PTCAC97);
    862      1.23      kent 
    863      1.10    simonb 	/* Wait for codec ready signal */
    864       1.3     tacha 	n = 0;
    865       1.3     tacha 	do {
    866       1.3     tacha 		delay(1000);
    867       1.3     tacha 		if (++n > 1000) {
    868      1.48       chs 			aprint_error_dev(sc->sc_dev,
    869      1.39    dyoung 			    "timeout waiting for codec ready\n");
    870      1.19      kent 			return ETIMEDOUT;
    871       1.3     tacha 		}
    872       1.3     tacha 		dat32 = BA0READ4(sc, CS428X_ACSTS) & ACSTS_CRDY;
    873       1.3     tacha 	} while (dat32 == 0);
    874       1.3     tacha 
    875       1.3     tacha 	/* Enable Valid Frame output on ASDOUT */
    876       1.3     tacha 	BA0WRITE4(sc, CS428X_ACCTL, ACCTL_ESYN | ACCTL_VFRM);
    877      1.23      kent 
    878      1.10    simonb 	/* Wait until codec calibration is finished. Codec register 26h */
    879       1.3     tacha 	n = 0;
    880       1.3     tacha 	do {
    881       1.3     tacha 		delay(1);
    882       1.3     tacha 		if (++n > 1000) {
    883      1.48       chs 			aprint_error_dev(sc->sc_dev,
    884      1.39    dyoung 			    "timeout waiting for codec calibration\n");
    885      1.19      kent 			return ETIMEDOUT;
    886       1.3     tacha 		}
    887       1.3     tacha 		cs428x_read_codec(sc, AC97_REG_POWER, &data);
    888       1.3     tacha 	} while ((data & 0x0f) != 0x0f);
    889       1.3     tacha 
    890       1.3     tacha 	/* Set the serial timing configuration again */
    891       1.3     tacha 	/* XXX: undocumented but the Linux driver do this */
    892       1.3     tacha 	BA0WRITE4(sc, CS4281_SERMC, SERMC_PTCAC97);
    893       1.3     tacha 
    894       1.3     tacha 	/* Wait until we've sampled input slots 3 & 4 as valid */
    895       1.3     tacha 	n = 0;
    896       1.3     tacha 	do {
    897       1.3     tacha 		delay(1000);
    898       1.3     tacha 		if (++n > 1000) {
    899      1.48       chs 			aprint_error_dev(sc->sc_dev, "timeout waiting for "
    900      1.39    dyoung 			    "sampled input slots as valid\n");
    901      1.19      kent 			return ETIMEDOUT;
    902       1.3     tacha 		}
    903       1.3     tacha 		dat32 = BA0READ4(sc, CS428X_ACISV) & (ACISV_ISV3 | ACISV_ISV4) ;
    904       1.3     tacha 	} while (dat32 != (ACISV_ISV3 | ACISV_ISV4));
    905      1.23      kent 
    906       1.3     tacha 	/* Start digital data transfer of audio data to the codec */
    907       1.3     tacha 	BA0WRITE4(sc, CS428X_ACOSV, (ACOSV_SLV3 | ACOSV_SLV4));
    908      1.19      kent 	return 0;
    909       1.3     tacha }
    910       1.3     tacha 
    911       1.3     tacha 
    912       1.3     tacha /* Internal functions */
    913       1.3     tacha 
    914       1.1  augustss /* convert sample rate to register value */
    915      1.26   thorpej static uint8_t
    916      1.23      kent cs4281_sr2regval(int rate)
    917       1.1  augustss {
    918      1.23      kent 	uint8_t retval;
    919       1.1  augustss 
    920       1.1  augustss 	/* We don't have to change here. but anyway ... */
    921       1.1  augustss 	if (rate > 48000)
    922       1.1  augustss 		rate = 48000;
    923       1.1  augustss 	if (rate < 6023)
    924       1.1  augustss 		rate = 6023;
    925       1.1  augustss 
    926       1.1  augustss 	switch (rate) {
    927       1.1  augustss 	case 8000:
    928       1.1  augustss 		retval = 5;
    929       1.1  augustss 		break;
    930       1.1  augustss 	case 11025:
    931       1.1  augustss 		retval = 4;
    932       1.1  augustss 		break;
    933       1.1  augustss 	case 16000:
    934       1.1  augustss 		retval = 3;
    935       1.1  augustss 		break;
    936       1.1  augustss 	case 22050:
    937       1.1  augustss 		retval = 2;
    938       1.1  augustss 		break;
    939       1.1  augustss 	case 44100:
    940       1.1  augustss 		retval = 1;
    941       1.1  augustss 		break;
    942       1.1  augustss 	case 48000:
    943       1.1  augustss 		retval = 0;
    944       1.1  augustss 		break;
    945       1.1  augustss 	default:
    946       1.1  augustss 		retval = 1536000/rate; /* == 24576000/(rate*16) */
    947       1.1  augustss 	}
    948       1.1  augustss 	return retval;
    949       1.1  augustss }
    950       1.1  augustss 
    951      1.26   thorpej static void
    952      1.23      kent cs4281_set_adc_rate(struct cs428x_softc *sc, int rate)
    953       1.1  augustss {
    954      1.10    simonb 
    955       1.3     tacha 	BA0WRITE4(sc, CS4281_ADCSR, cs4281_sr2regval(rate));
    956       1.1  augustss }
    957       1.1  augustss 
    958      1.26   thorpej static void
    959      1.23      kent cs4281_set_dac_rate(struct cs428x_softc *sc, int rate)
    960       1.1  augustss {
    961      1.10    simonb 
    962       1.3     tacha 	BA0WRITE4(sc, CS4281_DACSR, cs4281_sr2regval(rate));
    963       1.1  augustss }
    964       1.1  augustss 
    965      1.26   thorpej static int
    966      1.23      kent cs4281_init(struct cs428x_softc *sc, int init)
    967       1.1  augustss {
    968       1.1  augustss 	int n;
    969      1.23      kent 	uint16_t data;
    970      1.23      kent 	uint32_t dat32;
    971       1.1  augustss 
    972       1.1  augustss 	/* set "Configuration Write Protect" register to
    973       1.1  augustss 	 * 0x4281 to allow to write */
    974       1.1  augustss 	BA0WRITE4(sc, CS4281_CWPR, 0x4281);
    975       1.1  augustss 
    976       1.3     tacha 	/*
    977       1.3     tacha 	 * Unset "Full Power-Down bit of Extended PCI Power Management
    978       1.3     tacha 	 * Control" register to release the reset state.
    979       1.3     tacha 	 */
    980       1.3     tacha 	dat32 = BA0READ4(sc, CS4281_EPPMC);
    981       1.3     tacha 	if (dat32 & EPPMC_FPDN) {
    982       1.3     tacha 		BA0WRITE4(sc, CS4281_EPPMC, dat32 & ~EPPMC_FPDN);
    983       1.3     tacha 	}
    984       1.3     tacha 
    985       1.1  augustss 	/* Start PLL out in known state */
    986       1.1  augustss 	BA0WRITE4(sc, CS4281_CLKCR1, 0);
    987       1.1  augustss 	/* Start serial ports out in known state */
    988       1.1  augustss 	BA0WRITE4(sc, CS4281_SERMC, 0);
    989      1.23      kent 
    990       1.1  augustss 	/* Reset codec */
    991       1.1  augustss 	BA0WRITE4(sc, CS428X_ACCTL, 0);
    992       1.1  augustss 	delay(50);	/* delay 50us */
    993       1.1  augustss 
    994       1.1  augustss 	BA0WRITE4(sc, CS4281_SPMC, 0);
    995       1.1  augustss 	delay(100);	/* delay 100us */
    996       1.1  augustss 	BA0WRITE4(sc, CS4281_SPMC, SPMC_RSTN);
    997       1.1  augustss #if defined(ENABLE_SECONDARY_CODEC)
    998       1.1  augustss 	BA0WRITE4(sc, CS4281_SPMC, SPMC_RSTN | SPCM_ASDIN2E);
    999       1.1  augustss 	BA0WRITE4(sc, CS4281_SERMC, SERMC_TCID);
   1000       1.1  augustss #endif
   1001       1.1  augustss 	delay(50000);   /* XXX: delay 50ms */
   1002       1.1  augustss 
   1003       1.1  augustss 	/* Turn on Sound System clocks based on ABITCLK */
   1004       1.1  augustss 	BA0WRITE4(sc, CS4281_CLKCR1, CLKCR1_DLLP);
   1005       1.1  augustss 	delay(50000);   /* XXX: delay 50ms */
   1006       1.1  augustss 	BA0WRITE4(sc, CS4281_CLKCR1, CLKCR1_SWCE | CLKCR1_DLLP);
   1007       1.1  augustss 
   1008       1.1  augustss 	/* Set enables for sections that are needed in the SSPM registers */
   1009       1.1  augustss 	BA0WRITE4(sc, CS4281_SSPM,
   1010       1.1  augustss 		  SSPM_MIXEN |		/* Mixer */
   1011       1.1  augustss 		  SSPM_CSRCEN |		/* Capture SRC */
   1012       1.1  augustss 		  SSPM_PSRCEN |		/* Playback SRC */
   1013       1.1  augustss 		  SSPM_JSEN |		/* Joystick */
   1014       1.1  augustss 		  SSPM_ACLEN |		/* AC LINK */
   1015       1.1  augustss 		  SSPM_FMEN		/* FM */
   1016       1.1  augustss 		  );
   1017       1.1  augustss 
   1018       1.1  augustss 	/* Wait for clock stabilization */
   1019       1.1  augustss 	n = 0;
   1020       1.1  augustss #if 1
   1021       1.1  augustss 	/* what document says */
   1022      1.10    simonb 	while ((BA0READ4(sc, CS4281_CLKCR1)& (CLKCR1_DLLRDY | CLKCR1_CLKON))
   1023      1.10    simonb 		 != (CLKCR1_DLLRDY | CLKCR1_CLKON)) {
   1024       1.1  augustss 		delay(100);
   1025      1.10    simonb 		if (++n > 1000) {
   1026      1.48       chs 			aprint_error_dev(sc->sc_dev,
   1027      1.39    dyoung 			    "timeout waiting for clock stabilization\n");
   1028       1.1  augustss 			return -1;
   1029      1.10    simonb 		}
   1030       1.1  augustss 	}
   1031       1.1  augustss #else
   1032       1.1  augustss 	/* Cirrus driver for Linux does */
   1033      1.10    simonb 	while (!(BA0READ4(sc, CS4281_CLKCR1) & CLKCR1_DLLRDY)) {
   1034       1.1  augustss 		delay(1000);
   1035      1.10    simonb 		if (++n > 1000) {
   1036      1.48       chs 			aprint_error_dev(sc->sc_dev,
   1037      1.39    dyoung 			    "timeout waiting for clock stabilization\n");
   1038       1.1  augustss 			return -1;
   1039      1.10    simonb 		}
   1040       1.1  augustss 	}
   1041       1.1  augustss #endif
   1042       1.1  augustss 
   1043       1.1  augustss 	/* Enable ASYNC generation */
   1044       1.1  augustss 	BA0WRITE4(sc, CS428X_ACCTL, ACCTL_ESYN);
   1045       1.1  augustss 
   1046      1.10    simonb 	/* Wait for codec ready. Linux driver waits 50ms here */
   1047       1.1  augustss 	n = 0;
   1048      1.10    simonb 	while ((BA0READ4(sc, CS428X_ACSTS) & ACSTS_CRDY) == 0) {
   1049       1.1  augustss 		delay(100);
   1050      1.10    simonb 		if (++n > 1000) {
   1051      1.48       chs 			aprint_error_dev(sc->sc_dev,
   1052      1.39    dyoung 			    "timeout waiting for codec ready\n");
   1053       1.1  augustss 			return -1;
   1054      1.10    simonb 		}
   1055       1.1  augustss 	}
   1056       1.1  augustss 
   1057       1.1  augustss #if defined(ENABLE_SECONDARY_CODEC)
   1058       1.1  augustss 	/* secondary codec ready*/
   1059       1.1  augustss 	n = 0;
   1060      1.10    simonb 	while ((BA0READ4(sc, CS4281_ACSTS2) & ACSTS2_CRDY2) == 0) {
   1061       1.1  augustss 		delay(100);
   1062      1.10    simonb 		if (++n > 1000) {
   1063      1.48       chs 			aprint_error_dev(sc->sc_dev,
   1064      1.39    dyoung 			    "timeout waiting for secondary codec ready\n");
   1065       1.1  augustss 			return -1;
   1066      1.10    simonb 		}
   1067       1.1  augustss 	}
   1068       1.1  augustss #endif
   1069       1.1  augustss 
   1070       1.1  augustss 	/* Set the serial timing configuration */
   1071       1.1  augustss 	/* XXX: undocumented but the Linux driver do this */
   1072       1.1  augustss 	BA0WRITE4(sc, CS4281_SERMC, SERMC_PTCAC97);
   1073      1.23      kent 
   1074      1.10    simonb 	/* Wait for codec ready signal */
   1075       1.1  augustss 	n = 0;
   1076       1.1  augustss 	do {
   1077       1.1  augustss 		delay(1000);
   1078       1.1  augustss 		if (++n > 1000) {
   1079      1.48       chs 			aprint_error_dev(sc->sc_dev,
   1080      1.39    dyoung 			    "timeout waiting for codec ready\n");
   1081       1.1  augustss 			return -1;
   1082       1.1  augustss 		}
   1083       1.1  augustss 		dat32 = BA0READ4(sc, CS428X_ACSTS) & ACSTS_CRDY;
   1084       1.1  augustss 	} while (dat32 == 0);
   1085       1.1  augustss 
   1086       1.1  augustss 	/* Enable Valid Frame output on ASDOUT */
   1087       1.1  augustss 	BA0WRITE4(sc, CS428X_ACCTL, ACCTL_ESYN | ACCTL_VFRM);
   1088      1.23      kent 
   1089      1.10    simonb 	/* Wait until codec calibration is finished. codec register 26h */
   1090       1.1  augustss 	n = 0;
   1091       1.1  augustss 	do {
   1092       1.1  augustss 		delay(1);
   1093       1.1  augustss 		if (++n > 1000) {
   1094      1.48       chs 			aprint_error_dev(sc->sc_dev,
   1095      1.39    dyoung 			    "timeout waiting for codec calibration\n");
   1096       1.1  augustss 			return -1;
   1097       1.1  augustss 		}
   1098       1.3     tacha 		cs428x_read_codec(sc, AC97_REG_POWER, &data);
   1099       1.1  augustss 	} while ((data & 0x0f) != 0x0f);
   1100       1.1  augustss 
   1101       1.1  augustss 	/* Set the serial timing configuration again */
   1102       1.1  augustss 	/* XXX: undocumented but the Linux driver do this */
   1103       1.1  augustss 	BA0WRITE4(sc, CS4281_SERMC, SERMC_PTCAC97);
   1104       1.1  augustss 
   1105       1.1  augustss 	/* Wait until we've sampled input slots 3 & 4 as valid */
   1106       1.1  augustss 	n = 0;
   1107       1.1  augustss 	do {
   1108       1.1  augustss 		delay(1000);
   1109       1.1  augustss 		if (++n > 1000) {
   1110      1.48       chs 			aprint_error_dev(sc->sc_dev, "timeout waiting for "
   1111      1.39    dyoung 			    "sampled input slots as valid\n");
   1112       1.1  augustss 			return -1;
   1113       1.1  augustss 		}
   1114       1.1  augustss 		dat32 = BA0READ4(sc, CS428X_ACISV) & (ACISV_ISV3 | ACISV_ISV4);
   1115       1.1  augustss 	} while (dat32 != (ACISV_ISV3 | ACISV_ISV4));
   1116      1.23      kent 
   1117       1.1  augustss 	/* Start digital data transfer of audio data to the codec */
   1118       1.1  augustss 	BA0WRITE4(sc, CS428X_ACOSV, (ACOSV_SLV3 | ACOSV_SLV4));
   1119      1.23      kent 
   1120       1.3     tacha 	cs428x_write_codec(sc, AC97_REG_HEADPHONE_VOLUME, 0);
   1121       1.3     tacha 	cs428x_write_codec(sc, AC97_REG_MASTER_VOLUME, 0);
   1122      1.23      kent 
   1123       1.1  augustss 	/* Power on the DAC */
   1124       1.3     tacha 	cs428x_read_codec(sc, AC97_REG_POWER, &data);
   1125       1.3     tacha 	cs428x_write_codec(sc, AC97_REG_POWER, data & 0xfdff);
   1126       1.1  augustss 
   1127       1.1  augustss 	/* Wait until we sample a DAC ready state.
   1128       1.1  augustss 	 * Not documented, but Linux driver does.
   1129       1.1  augustss 	 */
   1130       1.1  augustss 	for (n = 0; n < 32; ++n) {
   1131       1.1  augustss 		delay(1000);
   1132       1.3     tacha 		cs428x_read_codec(sc, AC97_REG_POWER, &data);
   1133       1.1  augustss 		if (data & 0x02)
   1134       1.1  augustss 			break;
   1135       1.1  augustss 	}
   1136      1.23      kent 
   1137       1.1  augustss 	/* Power on the ADC */
   1138       1.3     tacha 	cs428x_read_codec(sc, AC97_REG_POWER, &data);
   1139       1.3     tacha 	cs428x_write_codec(sc, AC97_REG_POWER, data & 0xfeff);
   1140       1.1  augustss 
   1141       1.1  augustss 	/* Wait until we sample ADC ready state.
   1142       1.1  augustss 	 * Not documented, but Linux driver does.
   1143       1.1  augustss 	 */
   1144       1.1  augustss 	for (n = 0; n < 32; ++n) {
   1145       1.1  augustss 		delay(1000);
   1146       1.3     tacha 		cs428x_read_codec(sc, AC97_REG_POWER, &data);
   1147       1.1  augustss 		if (data & 0x01)
   1148       1.1  augustss 			break;
   1149       1.1  augustss 	}
   1150      1.23      kent 
   1151       1.1  augustss #if 0
   1152       1.1  augustss 	/* Initialize AC-Link features */
   1153       1.1  augustss 	/* variable sample-rate support */
   1154       1.1  augustss 	mem = BA0READ4(sc, CS4281_SERMC);
   1155       1.1  augustss 	mem |=  (SERMC_ODSEN1 | SERMC_ODSEN2);
   1156       1.1  augustss 	BA0WRITE4(sc, CS4281_SERMC, mem);
   1157       1.1  augustss 	/* XXX: more... */
   1158      1.23      kent 
   1159       1.1  augustss 	/* Initialize SSCR register features */
   1160       1.1  augustss 	/* XXX: hardware volume setting */
   1161       1.1  augustss 	BA0WRITE4(sc, CS4281_SSCR, ~SSCR_HVC); /* disable HW volume setting */
   1162       1.1  augustss #endif
   1163       1.1  augustss 
   1164       1.1  augustss 	/* disable Sound Blaster Pro emulation */
   1165      1.24     perry 	/* XXX:
   1166       1.1  augustss 	 * Cannot set since the documents does not describe which bit is
   1167       1.1  augustss 	 * correspond to SSCR_SB. Since the reset value of SSCR is 0,
   1168       1.1  augustss 	 * we can ignore it.*/
   1169       1.1  augustss #if 0
   1170       1.1  augustss 	BA0WRITE4(sc, CS4281_SSCR, SSCR_SB);
   1171       1.1  augustss #endif
   1172       1.1  augustss 
   1173       1.1  augustss 	/* map AC97 PCM playback to DMA Channel 0 */
   1174       1.1  augustss 	/* Reset FEN bit to setup first */
   1175      1.10    simonb 	BA0WRITE4(sc, CS4281_FCR0, (BA0READ4(sc, CS4281_FCR0) & ~FCRn_FEN));
   1176       1.1  augustss 	/*
   1177       1.1  augustss 	 *| RS[4:0]/|        |
   1178       1.1  augustss 	 *| LS[4:0] |  AC97  | Slot Function
   1179       1.1  augustss 	 *|---------+--------+--------------------
   1180       1.1  augustss 	 *|     0   |    3   | Left PCM Playback
   1181       1.1  augustss 	 *|     1   |    4   | Right PCM Playback
   1182       1.1  augustss 	 *|     2   |    5   | Phone Line 1 DAC
   1183       1.1  augustss 	 *|     3   |    6   | Center PCM Playback
   1184       1.1  augustss 	 *....
   1185       1.1  augustss 	 *  quoted from Table 29(p109)
   1186       1.1  augustss 	 */
   1187       1.1  augustss 	dat32 = 0x01 << 24 |   /* RS[4:0] =  1 see above */
   1188       1.1  augustss 		0x00 << 16 |   /* LS[4:0] =  0 see above */
   1189       1.1  augustss 		0x0f <<  8 |   /* SZ[6:0] = 15 size of buffer */
   1190       1.1  augustss 		0x00 <<  0 ;   /* OF[6:0] =  0 offset */
   1191       1.1  augustss 	BA0WRITE4(sc, CS4281_FCR0, dat32);
   1192       1.1  augustss 	BA0WRITE4(sc, CS4281_FCR0, dat32 | FCRn_FEN);
   1193       1.1  augustss 
   1194       1.1  augustss 	/* map AC97 PCM record to DMA Channel 1 */
   1195       1.1  augustss 	/* Reset FEN bit to setup first */
   1196      1.10    simonb 	BA0WRITE4(sc, CS4281_FCR1, (BA0READ4(sc, CS4281_FCR1) & ~FCRn_FEN));
   1197       1.1  augustss 	/*
   1198       1.1  augustss 	 *| RS[4:0]/|
   1199       1.1  augustss 	 *| LS[4:0] | AC97 | Slot Function
   1200       1.1  augustss 	 *|---------+------+-------------------
   1201       1.1  augustss 	 *|   10    |   3  | Left PCM Record
   1202       1.1  augustss 	 *|   11    |   4  | Right PCM Record
   1203       1.1  augustss 	 *|   12    |   5  | Phone Line 1 ADC
   1204       1.1  augustss 	 *|   13    |   6  | Mic ADC
   1205       1.1  augustss 	 *....
   1206       1.1  augustss 	 * quoted from Table 30(p109)
   1207       1.1  augustss 	 */
   1208       1.1  augustss 	dat32 = 0x0b << 24 |    /* RS[4:0] = 11 See above */
   1209       1.1  augustss 		0x0a << 16 |    /* LS[4:0] = 10 See above */
   1210       1.1  augustss 		0x0f <<  8 |    /* SZ[6:0] = 15 Size of buffer */
   1211       1.1  augustss 		0x10 <<  0 ;    /* OF[6:0] = 16 offset */
   1212       1.1  augustss 
   1213       1.1  augustss 	/* XXX: I cannot understand why FCRn_PSH is needed here. */
   1214       1.1  augustss 	BA0WRITE4(sc, CS4281_FCR1, dat32 | FCRn_PSH);
   1215       1.1  augustss 	BA0WRITE4(sc, CS4281_FCR1, dat32 | FCRn_FEN);
   1216       1.1  augustss 
   1217       1.1  augustss #if 0
   1218       1.1  augustss 	/* Disable DMA Channel 2, 3 */
   1219      1.10    simonb 	BA0WRITE4(sc, CS4281_FCR2, (BA0READ4(sc, CS4281_FCR2) & ~FCRn_FEN));
   1220      1.10    simonb 	BA0WRITE4(sc, CS4281_FCR3, (BA0READ4(sc, CS4281_FCR3) & ~FCRn_FEN));
   1221       1.1  augustss #endif
   1222       1.1  augustss 
   1223       1.1  augustss 	/* Set the SRC Slot Assignment accordingly */
   1224       1.1  augustss 	/*| PLSS[4:0]/
   1225       1.1  augustss 	 *| PRSS[4:0] | AC97 | Slot Function
   1226       1.1  augustss 	 *|-----------+------+----------------
   1227       1.1  augustss 	 *|     0     |  3   | Left PCM Playback
   1228       1.1  augustss 	 *|     1     |  4   | Right PCM Playback
   1229       1.1  augustss 	 *|     2     |  5   | phone line 1 DAC
   1230       1.1  augustss 	 *|     3     |  6   | Center PCM Playback
   1231       1.1  augustss 	 *|     4     |  7   | Left Surround PCM Playback
   1232       1.1  augustss 	 *|     5     |  8   | Right Surround PCM Playback
   1233       1.1  augustss 	 *......
   1234       1.1  augustss 	 *
   1235       1.1  augustss 	 *| CLSS[4:0]/
   1236       1.1  augustss 	 *| CRSS[4:0] | AC97 | Codec |Slot Function
   1237       1.1  augustss 	 *|-----------+------+-------+-----------------
   1238       1.1  augustss 	 *|    10     |   3  |Primary| Left PCM Record
   1239       1.1  augustss 	 *|    11     |   4  |Primary| Right PCM Record
   1240       1.1  augustss 	 *|    12     |   5  |Primary| Phone Line 1 ADC
   1241       1.1  augustss 	 *|    13     |   6  |Primary| Mic ADC
   1242       1.1  augustss 	 *|.....
   1243       1.1  augustss 	 *|    20     |   3  |  Sec. | Left PCM Record
   1244       1.1  augustss 	 *|    21     |   4  |  Sec. | Right PCM Record
   1245       1.1  augustss 	 *|    22     |   5  |  Sec. | Phone Line 1 ADC
   1246       1.1  augustss 	 *|    23     |   6  |  Sec. | Mic ADC
   1247       1.1  augustss 	 */
   1248       1.1  augustss 	dat32 = 0x0b << 24 |   /* CRSS[4:0] Right PCM Record(primary) */
   1249       1.1  augustss 		0x0a << 16 |   /* CLSS[4:0] Left  PCM Record(primary) */
   1250       1.1  augustss 		0x01 <<  8 |   /* PRSS[4:0] Right PCM Playback */
   1251       1.1  augustss 		0x00 <<  0;    /* PLSS[4:0] Left  PCM Playback */
   1252       1.1  augustss 	BA0WRITE4(sc, CS4281_SRCSA, dat32);
   1253      1.23      kent 
   1254       1.5       wiz 	/* Set interrupt to occurred at Half and Full terminal
   1255       1.1  augustss 	 * count interrupt enable for DMA channel 0 and 1.
   1256       1.1  augustss 	 * To keep DMA stop, set MSK.
   1257       1.1  augustss 	 */
   1258       1.1  augustss 	dat32 = DCRn_HTCIE | DCRn_TCIE | DCRn_MSK;
   1259       1.1  augustss 	BA0WRITE4(sc, CS4281_DCR0, dat32);
   1260       1.1  augustss 	BA0WRITE4(sc, CS4281_DCR1, dat32);
   1261      1.23      kent 
   1262       1.1  augustss 	/* Set Auto-Initialize Contorl enable */
   1263       1.1  augustss 	BA0WRITE4(sc, CS4281_DMR0,
   1264       1.1  augustss 		  DMRn_DMA | DMRn_AUTO | DMRn_TR_READ);
   1265       1.1  augustss 	BA0WRITE4(sc, CS4281_DMR1,
   1266       1.1  augustss 		  DMRn_DMA | DMRn_AUTO | DMRn_TR_WRITE);
   1267       1.1  augustss 
   1268       1.1  augustss 	/* Clear DMA Mask in HIMR */
   1269       1.1  augustss 	dat32 = ~HIMR_DMAIM & ~HIMR_D1IM & ~HIMR_D0IM;
   1270       1.1  augustss 	BA0WRITE4(sc, CS4281_HIMR,
   1271       1.1  augustss 		  BA0READ4(sc, CS4281_HIMR) & dat32);
   1272       1.4     tacha 
   1273       1.4     tacha 	/* set current status */
   1274       1.4     tacha 	if (init != 0) {
   1275       1.4     tacha 		sc->sc_prun = 0;
   1276       1.4     tacha 		sc->sc_rrun = 0;
   1277       1.4     tacha 	}
   1278       1.4     tacha 
   1279       1.4     tacha 	/* setup playback volume */
   1280       1.4     tacha 	BA0WRITE4(sc, CS4281_PPRVC, 7);
   1281       1.4     tacha 	BA0WRITE4(sc, CS4281_PPLVC, 7);
   1282       1.4     tacha 
   1283       1.1  augustss 	return 0;
   1284       1.1  augustss }
   1285