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