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