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