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