1 /* $NetBSD: cs4281.c,v 1.42 2009/05/12 08:23:00 cegger Exp $ */
4 * Copyright (c) 2000 Tatoku Ogaito. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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.
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
40 * 1: midi and FM support
45 #include <sys/cdefs.h>
46 __KERNEL_RCSID(0, "$NetBSD: cs4281.c,v 1.42 2009/05/12 08:23:00 cegger Exp $");
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>
56 #include <dev/pci/pcidevs.h>
57 #include <dev/pci/pcivar.h>
58 #include <dev/pci/cs4281reg.h>
59 #include <dev/pci/cs428xreg.h>
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>
67 #include <dev/ic/ac97reg.h>
68 #include <dev/ic/ac97var.h>
70 #include <dev/pci/cs428x.h>
74 #if defined(ENABLE_SECONDARY_CODEC)
75 #define MAX_CHANNELS (4)
76 #define MAX_FIFO_SIZE 32 /* 128/4channels */
78 #define MAX_CHANNELS (2)
79 #define MAX_FIFO_SIZE 64 /* 128/2channels */
82 /* IF functions for audio driver */
83 static int cs4281_match(device_t
, cfdata_t
, void *);
84 static void cs4281_attach(device_t
, device_t
, void *);
85 static int cs4281_intr(void *);
86 static int cs4281_query_encoding(void *, struct audio_encoding
*);
87 static int cs4281_set_params(void *, int, int, audio_params_t
*,
88 audio_params_t
*, stream_filter_list_t
*,
89 stream_filter_list_t
*);
90 static int cs4281_halt_output(void *);
91 static int cs4281_halt_input(void *);
92 static int cs4281_getdev(void *, struct audio_device
*);
93 static int cs4281_trigger_output(void *, void *, void *, int,
94 void (*)(void *), void *,
95 const audio_params_t
*);
96 static int cs4281_trigger_input(void *, void *, void *, int,
97 void (*)(void *), void *,
98 const audio_params_t
*);
100 static int cs4281_reset_codec(void *);
102 /* Internal functions */
103 static uint8_t cs4281_sr2regval(int);
104 static void cs4281_set_dac_rate(struct cs428x_softc
*, int);
105 static void cs4281_set_adc_rate(struct cs428x_softc
*, int);
106 static int cs4281_init(struct cs428x_softc
*, int);
108 /* Power Management */
109 static bool cs4281_suspend(device_t
, pmf_qual_t
);
110 static bool cs4281_resume(device_t
, pmf_qual_t
);
112 static const struct audio_hw_if cs4281_hw_if
= {
116 cs4281_query_encoding
,
118 cs428x_round_blocksize
,
129 cs428x_mixer_set_port
,
130 cs428x_mixer_get_port
,
131 cs428x_query_devinfo
,
134 cs428x_round_buffersize
,
137 cs4281_trigger_output
,
138 cs4281_trigger_input
,
145 static void cs4281_midi_close(void*);
146 static void cs4281_midi_getinfo(void *, struct midi_info
*);
147 static int cs4281_midi_open(void *, int, void (*)(void *, int),
148 void (*)(void *), void *);
149 static int cs4281_midi_output(void *, int);
151 static const struct midi_hw_if cs4281_midi_hw_if
= {
160 CFATTACH_DECL(clct
, sizeof(struct cs428x_softc
),
161 cs4281_match
, cs4281_attach
, NULL
, NULL
);
163 static struct audio_device cs4281_device
= {
171 cs4281_match(device_t parent
, cfdata_t match
, void *aux
)
173 struct pci_attach_args
*pa
;
175 pa
= (struct pci_attach_args
*)aux
;
176 if (PCI_VENDOR(pa
->pa_id
) != PCI_VENDOR_CIRRUS
)
178 if (PCI_PRODUCT(pa
->pa_id
) == PCI_PRODUCT_CIRRUS_CS4281
)
184 cs4281_attach(device_t parent
, device_t self
, void *aux
)
186 struct cs428x_softc
*sc
;
187 struct pci_attach_args
*pa
;
188 pci_chipset_tag_t pc
;
194 sc
= device_private(self
);
195 pa
= (struct pci_attach_args
*)aux
;
197 aprint_naive(": Audio controller\n");
199 pci_devinfo(pa
->pa_id
, pa
->pa_class
, 0, devinfo
, sizeof(devinfo
));
200 aprint_normal(": %s (rev. 0x%02x)\n", devinfo
,
201 PCI_REVISION(pa
->pa_class
));
203 sc
->sc_pc
= pa
->pa_pc
;
204 sc
->sc_pt
= pa
->pa_tag
;
206 /* Map I/O register */
207 if (pci_mapreg_map(pa
, PCI_BA0
,
208 PCI_MAPREG_TYPE_MEM
|PCI_MAPREG_MEM_TYPE_32BIT
, 0,
209 &sc
->ba0t
, &sc
->ba0h
, NULL
, NULL
)) {
210 aprint_error_dev(&sc
->sc_dev
, "can't map BA0 space\n");
213 if (pci_mapreg_map(pa
, PCI_BA1
,
214 PCI_MAPREG_TYPE_MEM
|PCI_MAPREG_MEM_TYPE_32BIT
, 0,
215 &sc
->ba1t
, &sc
->ba1h
, NULL
, NULL
)) {
216 aprint_error_dev(&sc
->sc_dev
, "can't map BA1 space\n");
220 sc
->sc_dmatag
= pa
->pa_dmat
;
223 if ((error
= pci_activate(pa
->pa_pc
, pa
->pa_tag
, self
,
224 pci_activate_null
)) && error
!= EOPNOTSUPP
) {
225 aprint_error_dev(&sc
->sc_dev
, "cannot activate %d\n", error
);
229 /* Enable the device (set bus master flag) */
230 reg
= pci_conf_read(pa
->pa_pc
, pa
->pa_tag
, PCI_COMMAND_STATUS_REG
);
231 pci_conf_write(pa
->pa_pc
, pa
->pa_tag
, PCI_COMMAND_STATUS_REG
,
232 reg
| PCI_COMMAND_MASTER_ENABLE
);
235 /* LATENCY_TIMER setting */
236 temp1
= pci_conf_read(pa
->pa_pc
, pa
->pa_tag
, PCI_BHLC_REG
);
237 if (PCI_LATTIMER(temp1
) < 32) {
240 pci_conf_write(pa
->pa_pc
, pa
->pa_tag
, PCI_BHLC_REG
, temp1
);
244 /* Map and establish the interrupt. */
245 if (pci_intr_map(pa
, &sc
->intrh
)) {
246 aprint_error_dev(&sc
->sc_dev
, "couldn't map interrupt\n");
249 intrstr
= pci_intr_string(pc
, sc
->intrh
);
251 sc
->sc_ih
= pci_intr_establish(sc
->sc_pc
, sc
->intrh
, IPL_AUDIO
,
253 if (sc
->sc_ih
== NULL
) {
254 aprint_error_dev(&sc
->sc_dev
, "couldn't establish interrupt");
256 aprint_error(" at %s", intrstr
);
260 aprint_normal_dev(&sc
->sc_dev
, "interrupting at %s\n", intrstr
);
263 * Sound System start-up
265 if (cs4281_init(sc
, 1) != 0)
268 sc
->type
= TYPE_CS4281
;
269 sc
->halt_input
= cs4281_halt_input
;
270 sc
->halt_output
= cs4281_halt_output
;
272 sc
->dma_size
= CS4281_BUFFER_SIZE
/ MAX_CHANNELS
;
273 sc
->dma_align
= 0x10;
274 sc
->hw_blocksize
= sc
->dma_size
/ 2;
276 /* AC 97 attachment */
277 sc
->host_if
.arg
= sc
;
278 sc
->host_if
.attach
= cs428x_attach_codec
;
279 sc
->host_if
.read
= cs428x_read_codec
;
280 sc
->host_if
.write
= cs428x_write_codec
;
281 sc
->host_if
.reset
= cs4281_reset_codec
;
282 if (ac97_attach(&sc
->host_if
, self
) != 0) {
283 aprint_error_dev(&sc
->sc_dev
, "ac97_attach failed\n");
286 audio_attach_mi(&cs4281_hw_if
, sc
, &sc
->sc_dev
);
289 midi_attach_mi(&cs4281_midi_hw_if
, sc
, &sc
->sc_dev
);
292 if (!pmf_device_register(self
, cs4281_suspend
, cs4281_resume
))
293 aprint_error_dev(self
, "couldn't establish power handler\n");
299 struct cs428x_softc
*sc
;
300 uint32_t intr
, hdsr0
, hdsr1
;
309 /* grab interrupt register */
310 intr
= BA0READ4(sc
, CS4281_HISR
);
312 DPRINTF(("cs4281_intr:"));
314 if ((intr
& HISR_INTENA
) == 0) {
315 /* clear the interrupt register */
316 BA0WRITE4(sc
, CS4281_HICR
, HICR_CHGM
| HICR_IEV
);
320 if (intr
& HISR_DMA0
)
321 hdsr0
= BA0READ4(sc
, CS4281_HDSR0
); /* clear intr condition */
322 if (intr
& HISR_DMA1
)
323 hdsr1
= BA0READ4(sc
, CS4281_HDSR1
); /* clear intr condition */
324 /* clear the interrupt register */
325 BA0WRITE4(sc
, CS4281_HICR
, HICR_CHGM
| HICR_IEV
);
327 DPRINTF(("intr = 0x%08x, hdsr0 = 0x%08x hdsr1 = 0x%08x\n",
328 intr
, hdsr0
, hdsr1
));
330 /* Playback Interrupt */
331 if (intr
& HISR_DMA0
) {
334 DPRINTF((" PB DMA 0x%x(%d)",
335 (int)BA0READ4(sc
, CS4281_DCA0
),
336 (int)BA0READ4(sc
, CS4281_DCC0
)));
337 if ((sc
->sc_pi
%sc
->sc_pcount
) == 0)
338 sc
->sc_pintr(sc
->sc_parg
);
341 empty_dma
= sc
->sc_pdma
->addr
;
343 empty_dma
+= sc
->hw_blocksize
;
344 memcpy(empty_dma
, sc
->sc_pn
, sc
->hw_blocksize
);
345 sc
->sc_pn
+= sc
->hw_blocksize
;
346 if (sc
->sc_pn
>= sc
->sc_pe
)
347 sc
->sc_pn
= sc
->sc_ps
;
349 aprint_error_dev(&sc
->sc_dev
, "unexpected play intr\n");
352 if (intr
& HISR_DMA1
) {
356 DPRINTF((" CP DMA 0x%x(%d)", (int)BA0READ4(sc
, CS4281_DCA1
),
357 (int)BA0READ4(sc
, CS4281_DCC1
)));
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 sc
->sc_rn
+= sc
->hw_blocksize
;
364 if (sc
->sc_rn
>= sc
->sc_re
)
365 sc
->sc_rn
= sc
->sc_rs
;
366 if ((sc
->sc_ri
% sc
->sc_rcount
) == 0)
367 sc
->sc_rintr(sc
->sc_rarg
);
369 aprint_error_dev(&sc
->sc_dev
,
370 "unexpected record intr\n");
379 cs4281_query_encoding(void *addr
, struct audio_encoding
*fp
)
384 strcpy(fp
->name
, AudioEulinear
);
385 fp
->encoding
= AUDIO_ENCODING_ULINEAR
;
390 strcpy(fp
->name
, AudioEmulaw
);
391 fp
->encoding
= AUDIO_ENCODING_ULAW
;
393 fp
->flags
= AUDIO_ENCODINGFLAG_EMULATED
;
396 strcpy(fp
->name
, AudioEalaw
);
397 fp
->encoding
= AUDIO_ENCODING_ALAW
;
399 fp
->flags
= AUDIO_ENCODINGFLAG_EMULATED
;
402 strcpy(fp
->name
, AudioEslinear
);
403 fp
->encoding
= AUDIO_ENCODING_SLINEAR
;
408 strcpy(fp
->name
, AudioEslinear_le
);
409 fp
->encoding
= AUDIO_ENCODING_SLINEAR_LE
;
414 strcpy(fp
->name
, AudioEulinear_le
);
415 fp
->encoding
= AUDIO_ENCODING_ULINEAR_LE
;
420 strcpy(fp
->name
, AudioEslinear_be
);
421 fp
->encoding
= AUDIO_ENCODING_SLINEAR_BE
;
426 strcpy(fp
->name
, AudioEulinear_be
);
427 fp
->encoding
= AUDIO_ENCODING_ULINEAR_BE
;
438 cs4281_set_params(void *addr
, int setmode
, int usemode
,
439 audio_params_t
*play
, audio_params_t
*rec
, stream_filter_list_t
*pfil
,
440 stream_filter_list_t
*rfil
)
443 struct cs428x_softc
*sc
;
445 stream_filter_list_t
*fil
;
449 for (mode
= AUMODE_RECORD
; mode
!= -1;
450 mode
= mode
== AUMODE_RECORD
? AUMODE_PLAY
: -1) {
451 if ((setmode
& mode
) == 0)
454 p
= mode
== AUMODE_PLAY
? play
: rec
;
458 ("play: sample=%u precision=%u channels=%u\n",
459 p
->sample_rate
, p
->precision
, p
->channels
));
460 if (p
->sample_rate
< 6023 || p
->sample_rate
> 48000 ||
461 (p
->precision
!= 8 && p
->precision
!= 16) ||
462 (p
->channels
!= 1 && p
->channels
!= 2)) {
467 ("rec: sample=%u precision=%u channels=%u\n",
468 p
->sample_rate
, p
->precision
, p
->channels
));
469 if (p
->sample_rate
< 6023 || p
->sample_rate
> 48000 ||
470 (p
->precision
!= 8 && p
->precision
!= 16) ||
471 (p
->channels
!= 1 && p
->channels
!= 2)) {
476 fil
= mode
== AUMODE_PLAY
? pfil
: rfil
;
478 switch (p
->encoding
) {
479 case AUDIO_ENCODING_SLINEAR_BE
:
481 case AUDIO_ENCODING_SLINEAR_LE
:
483 case AUDIO_ENCODING_ULINEAR_BE
:
485 case AUDIO_ENCODING_ULINEAR_LE
:
487 case AUDIO_ENCODING_ULAW
:
488 hw
.encoding
= AUDIO_ENCODING_SLINEAR_LE
;
489 fil
->append(fil
, mode
== AUMODE_PLAY
? mulaw_to_linear8
490 : linear8_to_mulaw
, &hw
);
492 case AUDIO_ENCODING_ALAW
:
493 hw
.encoding
= AUDIO_ENCODING_SLINEAR_LE
;
494 fil
->append(fil
, mode
== AUMODE_PLAY
? alaw_to_linear8
495 : linear8_to_alaw
, &hw
);
502 /* set sample rate */
503 cs4281_set_dac_rate(sc
, play
->sample_rate
);
504 cs4281_set_adc_rate(sc
, rec
->sample_rate
);
509 cs4281_halt_output(void *addr
)
511 struct cs428x_softc
*sc
;
514 BA0WRITE4(sc
, CS4281_DCR0
, BA0READ4(sc
, CS4281_DCR0
) | DCRn_MSK
);
520 cs4281_halt_input(void *addr
)
522 struct cs428x_softc
*sc
;
525 BA0WRITE4(sc
, CS4281_DCR1
, BA0READ4(sc
, CS4281_DCR1
) | DCRn_MSK
);
531 cs4281_getdev(void *addr
, struct audio_device
*retp
)
534 *retp
= cs4281_device
;
539 cs4281_trigger_output(void *addr
, void *start
, void *end
, int blksize
,
540 void (*intr
)(void *), void *arg
,
541 const audio_params_t
*param
)
543 struct cs428x_softc
*sc
;
545 struct cs428x_dma
*p
;
552 printf("cs4281_trigger_output: already running\n");
556 DPRINTF(("cs4281_trigger_output: sc=%p start=%p end=%p "
557 "blksize=%d intr=%p(%p)\n", addr
, start
, end
, blksize
, intr
, arg
));
561 /* stop playback DMA */
562 BA0WRITE4(sc
, CS4281_DCR0
, BA0READ4(sc
, CS4281_DCR0
) | DCRn_MSK
);
564 DPRINTF(("param: precision=%d channels=%d encoding=%d\n",
565 param
->precision
, param
->channels
, param
->encoding
));
566 for (p
= sc
->sc_dmas
; p
!= NULL
&& BUFADDR(p
) != start
; p
= p
->next
)
569 printf("cs4281_trigger_output: bad addr %p\n", start
);
573 sc
->sc_pcount
= blksize
/ sc
->hw_blocksize
;
574 sc
->sc_ps
= (char *)start
;
575 sc
->sc_pe
= (char *)end
;
577 sc
->sc_pbuf
= KERNADDR(p
);
579 sc
->sc_pn
= sc
->sc_ps
;
580 if (blksize
>= sc
->dma_size
) {
581 sc
->sc_pn
= sc
->sc_ps
+ sc
->dma_size
;
582 memcpy(sc
->sc_pbuf
, start
, sc
->dma_size
);
585 sc
->sc_pn
= sc
->sc_ps
+ sc
->hw_blocksize
;
586 memcpy(sc
->sc_pbuf
, start
, sc
->hw_blocksize
);
589 dma_count
= sc
->dma_size
;
590 if (param
->precision
!= 8)
591 dma_count
/= 2; /* 16 bit */
592 if (param
->channels
> 1)
593 dma_count
/= 2; /* Stereo */
595 DPRINTF(("cs4281_trigger_output: DMAADDR(p)=0x%x count=%d\n",
596 (int)DMAADDR(p
), dma_count
));
597 BA0WRITE4(sc
, CS4281_DBA0
, DMAADDR(p
));
598 BA0WRITE4(sc
, CS4281_DBC0
, dma_count
-1);
600 /* set playback format */
601 fmt
= BA0READ4(sc
, CS4281_DMR0
) & ~DMRn_FMTMSK
;
602 if (param
->precision
== 8)
604 if (param
->channels
== 1)
606 if (param
->encoding
== AUDIO_ENCODING_ULINEAR_BE
||
607 param
->encoding
== AUDIO_ENCODING_SLINEAR_BE
)
609 if (param
->encoding
== AUDIO_ENCODING_ULINEAR_BE
||
610 param
->encoding
== AUDIO_ENCODING_ULINEAR_LE
)
612 BA0WRITE4(sc
, CS4281_DMR0
, fmt
);
614 /* set sample rate */
615 sc
->sc_prate
= param
->sample_rate
;
616 cs4281_set_dac_rate(sc
, param
->sample_rate
);
619 BA0WRITE4(sc
, CS4281_DCR0
, BA0READ4(sc
, CS4281_DCR0
) & ~DCRn_MSK
);
620 /* Enable interrupts */
621 BA0WRITE4(sc
, CS4281_HICR
, HICR_IEV
| HICR_CHGM
);
623 DPRINTF(("HICR =0x%08x(expected 0x00000001)\n", BA0READ4(sc
, CS4281_HICR
)));
624 DPRINTF(("HIMR =0x%08x(expected 0x00f0fc3f)\n", BA0READ4(sc
, CS4281_HIMR
)));
625 DPRINTF(("DMR0 =0x%08x(expected 0x2???0018)\n", BA0READ4(sc
, CS4281_DMR0
)));
626 DPRINTF(("DCR0 =0x%08x(expected 0x00030000)\n", BA0READ4(sc
, CS4281_DCR0
)));
627 DPRINTF(("FCR0 =0x%08x(expected 0x81000f00)\n", BA0READ4(sc
, CS4281_FCR0
)));
628 DPRINTF(("DACSR=0x%08x(expected 1 for 44kHz 5 for 8kHz)\n",
629 BA0READ4(sc
, CS4281_DACSR
)));
630 DPRINTF(("SRCSA=0x%08x(expected 0x0b0a0100)\n", BA0READ4(sc
, CS4281_SRCSA
)));
631 DPRINTF(("SSPM&SSPM_PSRCEN =0x%08x(expected 0x00000010)\n",
632 BA0READ4(sc
, CS4281_SSPM
) & SSPM_PSRCEN
));
638 cs4281_trigger_input(void *addr
, void *start
, void *end
, int blksize
,
639 void (*intr
)(void *), void *arg
,
640 const audio_params_t
*param
)
642 struct cs428x_softc
*sc
;
643 struct cs428x_dma
*p
;
651 printf("cs4281_trigger_input: already running\n");
654 DPRINTF(("cs4281_trigger_input: sc=%p start=%p end=%p "
655 "blksize=%d intr=%p(%p)\n", addr
, start
, end
, blksize
, intr
, arg
));
659 /* stop recording DMA */
660 BA0WRITE4(sc
, CS4281_DCR1
, BA0READ4(sc
, CS4281_DCR1
) | DCRn_MSK
);
662 for (p
= sc
->sc_dmas
; p
&& BUFADDR(p
) != start
; p
= p
->next
)
665 printf("cs4281_trigger_input: bad addr %p\n", start
);
669 sc
->sc_rcount
= blksize
/ sc
->hw_blocksize
;
670 sc
->sc_rs
= (char *)start
;
671 sc
->sc_re
= (char *)end
;
673 sc
->sc_rbuf
= KERNADDR(p
);
675 sc
->sc_rn
= sc
->sc_rs
;
677 dma_count
= sc
->dma_size
;
678 if (param
->precision
!= 8)
680 if (param
->channels
> 1)
683 DPRINTF(("cs4281_trigger_input: DMAADDR(p)=0x%x count=%d\n",
684 (int)DMAADDR(p
), dma_count
));
685 BA0WRITE4(sc
, CS4281_DBA1
, DMAADDR(p
));
686 BA0WRITE4(sc
, CS4281_DBC1
, dma_count
-1);
688 /* set recording format */
689 fmt
= BA0READ4(sc
, CS4281_DMR1
) & ~DMRn_FMTMSK
;
690 if (param
->precision
== 8)
692 if (param
->channels
== 1)
694 if (param
->encoding
== AUDIO_ENCODING_ULINEAR_BE
||
695 param
->encoding
== AUDIO_ENCODING_SLINEAR_BE
)
697 if (param
->encoding
== AUDIO_ENCODING_ULINEAR_BE
||
698 param
->encoding
== AUDIO_ENCODING_ULINEAR_LE
)
700 BA0WRITE4(sc
, CS4281_DMR1
, fmt
);
702 /* set sample rate */
703 sc
->sc_rrate
= param
->sample_rate
;
704 cs4281_set_adc_rate(sc
, param
->sample_rate
);
707 BA0WRITE4(sc
, CS4281_DCR1
, BA0READ4(sc
, CS4281_DCR1
) & ~DCRn_MSK
);
708 /* Enable interrupts */
709 BA0WRITE4(sc
, CS4281_HICR
, HICR_IEV
| HICR_CHGM
);
711 DPRINTF(("HICR=0x%08x\n", BA0READ4(sc
, CS4281_HICR
)));
712 DPRINTF(("HIMR=0x%08x\n", BA0READ4(sc
, CS4281_HIMR
)));
713 DPRINTF(("DMR1=0x%08x\n", BA0READ4(sc
, CS4281_DMR1
)));
714 DPRINTF(("DCR1=0x%08x\n", BA0READ4(sc
, CS4281_DCR1
)));
720 cs4281_suspend(device_t dv
, pmf_qual_t qual
)
722 struct cs428x_softc
*sc
= device_private(dv
);
724 /* save current playback status */
726 sc
->sc_suspend_state
.cs4281
.dcr0
= BA0READ4(sc
, CS4281_DCR0
);
727 sc
->sc_suspend_state
.cs4281
.dmr0
= BA0READ4(sc
, CS4281_DMR0
);
728 sc
->sc_suspend_state
.cs4281
.dbc0
= BA0READ4(sc
, CS4281_DBC0
);
729 sc
->sc_suspend_state
.cs4281
.dba0
= BA0READ4(sc
, CS4281_DBA0
);
732 /* save current capture status */
734 sc
->sc_suspend_state
.cs4281
.dcr1
= BA0READ4(sc
, CS4281_DCR1
);
735 sc
->sc_suspend_state
.cs4281
.dmr1
= BA0READ4(sc
, CS4281_DMR1
);
736 sc
->sc_suspend_state
.cs4281
.dbc1
= BA0READ4(sc
, CS4281_DBC1
);
737 sc
->sc_suspend_state
.cs4281
.dba1
= BA0READ4(sc
, CS4281_DBA1
);
740 BA0WRITE4(sc
, CS4281_DCR0
, BA0READ4(sc
, CS4281_DCR0
) | DCRn_MSK
);
741 BA0WRITE4(sc
, CS4281_DCR1
, BA0READ4(sc
, CS4281_DCR1
) | DCRn_MSK
);
747 cs4281_resume(device_t dv
, pmf_qual_t qual
)
749 struct cs428x_softc
*sc
= device_private(dv
);
752 cs4281_reset_codec(sc
);
754 /* restore ac97 registers */
755 (*sc
->codec_if
->vtbl
->restore_ports
)(sc
->codec_if
);
757 /* restore DMA related status */
759 cs4281_set_dac_rate(sc
, sc
->sc_prate
);
760 BA0WRITE4(sc
, CS4281_DBA0
, sc
->sc_suspend_state
.cs4281
.dba0
);
761 BA0WRITE4(sc
, CS4281_DBC0
, sc
->sc_suspend_state
.cs4281
.dbc0
);
762 BA0WRITE4(sc
, CS4281_DMR0
, sc
->sc_suspend_state
.cs4281
.dmr0
);
763 BA0WRITE4(sc
, CS4281_DCR0
, sc
->sc_suspend_state
.cs4281
.dcr0
);
766 cs4281_set_adc_rate(sc
, sc
->sc_rrate
);
767 BA0WRITE4(sc
, CS4281_DBA1
, sc
->sc_suspend_state
.cs4281
.dba1
);
768 BA0WRITE4(sc
, CS4281_DBC1
, sc
->sc_suspend_state
.cs4281
.dbc1
);
769 BA0WRITE4(sc
, CS4281_DMR1
, sc
->sc_suspend_state
.cs4281
.dmr1
);
770 BA0WRITE4(sc
, CS4281_DCR1
, sc
->sc_suspend_state
.cs4281
.dcr1
);
772 /* enable intterupts */
773 if (sc
->sc_prun
|| sc
->sc_rrun
)
774 BA0WRITE4(sc
, CS4281_HICR
, HICR_IEV
| HICR_CHGM
);
779 /* control AC97 codec */
781 cs4281_reset_codec(void *addr
)
783 struct cs428x_softc
*sc
;
790 DPRINTFN(3, ("cs4281_reset_codec\n"));
793 BA0WRITE4(sc
, CS428X_ACCTL
, 0);
794 delay(50); /* delay 50us */
796 BA0WRITE4(sc
, CS4281_SPMC
, 0);
797 delay(100); /* delay 100us */
798 BA0WRITE4(sc
, CS4281_SPMC
, SPMC_RSTN
);
799 #if defined(ENABLE_SECONDARY_CODEC)
800 BA0WRITE4(sc
, CS4281_SPMC
, SPMC_RSTN
| SPCM_ASDIN2E
);
801 BA0WRITE4(sc
, CS4281_SERMC
, SERMC_TCID
);
803 delay(50000); /* XXX: delay 50ms */
805 /* Enable ASYNC generation */
806 BA0WRITE4(sc
, CS428X_ACCTL
, ACCTL_ESYN
);
808 /* Wait for codec ready. Linux driver waits 50ms here */
810 while ((BA0READ4(sc
, CS428X_ACSTS
) & ACSTS_CRDY
) == 0) {
813 printf("reset_codec: AC97 codec ready timeout\n");
817 #if defined(ENABLE_SECONDARY_CODEC)
818 /* secondary codec ready*/
820 while ((BA0READ4(sc
, CS4281_ACSTS2
) & ACSTS2_CRDY2
) == 0) {
826 /* Set the serial timing configuration */
827 /* XXX: undocumented but the Linux driver do this */
828 BA0WRITE4(sc
, CS4281_SERMC
, SERMC_PTCAC97
);
830 /* Wait for codec ready signal */
835 aprint_error_dev(&sc
->sc_dev
,
836 "timeout waiting for codec ready\n");
839 dat32
= BA0READ4(sc
, CS428X_ACSTS
) & ACSTS_CRDY
;
840 } while (dat32
== 0);
842 /* Enable Valid Frame output on ASDOUT */
843 BA0WRITE4(sc
, CS428X_ACCTL
, ACCTL_ESYN
| ACCTL_VFRM
);
845 /* Wait until codec calibration is finished. Codec register 26h */
850 aprint_error_dev(&sc
->sc_dev
,
851 "timeout waiting for codec calibration\n");
854 cs428x_read_codec(sc
, AC97_REG_POWER
, &data
);
855 } while ((data
& 0x0f) != 0x0f);
857 /* Set the serial timing configuration again */
858 /* XXX: undocumented but the Linux driver do this */
859 BA0WRITE4(sc
, CS4281_SERMC
, SERMC_PTCAC97
);
861 /* Wait until we've sampled input slots 3 & 4 as valid */
866 aprint_error_dev(&sc
->sc_dev
, "timeout waiting for "
867 "sampled input slots as valid\n");
870 dat32
= BA0READ4(sc
, CS428X_ACISV
) & (ACISV_ISV3
| ACISV_ISV4
) ;
871 } while (dat32
!= (ACISV_ISV3
| ACISV_ISV4
));
873 /* Start digital data transfer of audio data to the codec */
874 BA0WRITE4(sc
, CS428X_ACOSV
, (ACOSV_SLV3
| ACOSV_SLV4
));
879 /* Internal functions */
881 /* convert sample rate to register value */
883 cs4281_sr2regval(int rate
)
887 /* We don't have to change here. but anyway ... */
913 retval
= 1536000/rate
; /* == 24576000/(rate*16) */
919 cs4281_set_adc_rate(struct cs428x_softc
*sc
, int rate
)
922 BA0WRITE4(sc
, CS4281_ADCSR
, cs4281_sr2regval(rate
));
926 cs4281_set_dac_rate(struct cs428x_softc
*sc
, int rate
)
929 BA0WRITE4(sc
, CS4281_DACSR
, cs4281_sr2regval(rate
));
933 cs4281_init(struct cs428x_softc
*sc
, int init
)
939 /* set "Configuration Write Protect" register to
940 * 0x4281 to allow to write */
941 BA0WRITE4(sc
, CS4281_CWPR
, 0x4281);
944 * Unset "Full Power-Down bit of Extended PCI Power Management
945 * Control" register to release the reset state.
947 dat32
= BA0READ4(sc
, CS4281_EPPMC
);
948 if (dat32
& EPPMC_FPDN
) {
949 BA0WRITE4(sc
, CS4281_EPPMC
, dat32
& ~EPPMC_FPDN
);
952 /* Start PLL out in known state */
953 BA0WRITE4(sc
, CS4281_CLKCR1
, 0);
954 /* Start serial ports out in known state */
955 BA0WRITE4(sc
, CS4281_SERMC
, 0);
958 BA0WRITE4(sc
, CS428X_ACCTL
, 0);
959 delay(50); /* delay 50us */
961 BA0WRITE4(sc
, CS4281_SPMC
, 0);
962 delay(100); /* delay 100us */
963 BA0WRITE4(sc
, CS4281_SPMC
, SPMC_RSTN
);
964 #if defined(ENABLE_SECONDARY_CODEC)
965 BA0WRITE4(sc
, CS4281_SPMC
, SPMC_RSTN
| SPCM_ASDIN2E
);
966 BA0WRITE4(sc
, CS4281_SERMC
, SERMC_TCID
);
968 delay(50000); /* XXX: delay 50ms */
970 /* Turn on Sound System clocks based on ABITCLK */
971 BA0WRITE4(sc
, CS4281_CLKCR1
, CLKCR1_DLLP
);
972 delay(50000); /* XXX: delay 50ms */
973 BA0WRITE4(sc
, CS4281_CLKCR1
, CLKCR1_SWCE
| CLKCR1_DLLP
);
975 /* Set enables for sections that are needed in the SSPM registers */
976 BA0WRITE4(sc
, CS4281_SSPM
,
977 SSPM_MIXEN
| /* Mixer */
978 SSPM_CSRCEN
| /* Capture SRC */
979 SSPM_PSRCEN
| /* Playback SRC */
980 SSPM_JSEN
| /* Joystick */
981 SSPM_ACLEN
| /* AC LINK */
985 /* Wait for clock stabilization */
988 /* what document says */
989 while ((BA0READ4(sc
, CS4281_CLKCR1
)& (CLKCR1_DLLRDY
| CLKCR1_CLKON
))
990 != (CLKCR1_DLLRDY
| CLKCR1_CLKON
)) {
993 aprint_error_dev(&sc
->sc_dev
,
994 "timeout waiting for clock stabilization\n");
999 /* Cirrus driver for Linux does */
1000 while (!(BA0READ4(sc
, CS4281_CLKCR1
) & CLKCR1_DLLRDY
)) {
1003 aprint_error_dev(&sc
->sc_dev
,
1004 "timeout waiting for clock stabilization\n");
1010 /* Enable ASYNC generation */
1011 BA0WRITE4(sc
, CS428X_ACCTL
, ACCTL_ESYN
);
1013 /* Wait for codec ready. Linux driver waits 50ms here */
1015 while ((BA0READ4(sc
, CS428X_ACSTS
) & ACSTS_CRDY
) == 0) {
1018 aprint_error_dev(&sc
->sc_dev
,
1019 "timeout waiting for codec ready\n");
1024 #if defined(ENABLE_SECONDARY_CODEC)
1025 /* secondary codec ready*/
1027 while ((BA0READ4(sc
, CS4281_ACSTS2
) & ACSTS2_CRDY2
) == 0) {
1030 aprint_error_dev(&sc
->sc_dev
,
1031 "timeout waiting for secondary codec ready\n");
1037 /* Set the serial timing configuration */
1038 /* XXX: undocumented but the Linux driver do this */
1039 BA0WRITE4(sc
, CS4281_SERMC
, SERMC_PTCAC97
);
1041 /* Wait for codec ready signal */
1046 aprint_error_dev(&sc
->sc_dev
,
1047 "timeout waiting for codec ready\n");
1050 dat32
= BA0READ4(sc
, CS428X_ACSTS
) & ACSTS_CRDY
;
1051 } while (dat32
== 0);
1053 /* Enable Valid Frame output on ASDOUT */
1054 BA0WRITE4(sc
, CS428X_ACCTL
, ACCTL_ESYN
| ACCTL_VFRM
);
1056 /* Wait until codec calibration is finished. codec register 26h */
1061 aprint_error_dev(&sc
->sc_dev
,
1062 "timeout waiting for codec calibration\n");
1065 cs428x_read_codec(sc
, AC97_REG_POWER
, &data
);
1066 } while ((data
& 0x0f) != 0x0f);
1068 /* Set the serial timing configuration again */
1069 /* XXX: undocumented but the Linux driver do this */
1070 BA0WRITE4(sc
, CS4281_SERMC
, SERMC_PTCAC97
);
1072 /* Wait until we've sampled input slots 3 & 4 as valid */
1077 aprint_error_dev(&sc
->sc_dev
, "timeout waiting for "
1078 "sampled input slots as valid\n");
1081 dat32
= BA0READ4(sc
, CS428X_ACISV
) & (ACISV_ISV3
| ACISV_ISV4
);
1082 } while (dat32
!= (ACISV_ISV3
| ACISV_ISV4
));
1084 /* Start digital data transfer of audio data to the codec */
1085 BA0WRITE4(sc
, CS428X_ACOSV
, (ACOSV_SLV3
| ACOSV_SLV4
));
1087 cs428x_write_codec(sc
, AC97_REG_HEADPHONE_VOLUME
, 0);
1088 cs428x_write_codec(sc
, AC97_REG_MASTER_VOLUME
, 0);
1090 /* Power on the DAC */
1091 cs428x_read_codec(sc
, AC97_REG_POWER
, &data
);
1092 cs428x_write_codec(sc
, AC97_REG_POWER
, data
& 0xfdff);
1094 /* Wait until we sample a DAC ready state.
1095 * Not documented, but Linux driver does.
1097 for (n
= 0; n
< 32; ++n
) {
1099 cs428x_read_codec(sc
, AC97_REG_POWER
, &data
);
1104 /* Power on the ADC */
1105 cs428x_read_codec(sc
, AC97_REG_POWER
, &data
);
1106 cs428x_write_codec(sc
, AC97_REG_POWER
, data
& 0xfeff);
1108 /* Wait until we sample ADC ready state.
1109 * Not documented, but Linux driver does.
1111 for (n
= 0; n
< 32; ++n
) {
1113 cs428x_read_codec(sc
, AC97_REG_POWER
, &data
);
1119 /* Initialize AC-Link features */
1120 /* variable sample-rate support */
1121 mem
= BA0READ4(sc
, CS4281_SERMC
);
1122 mem
|= (SERMC_ODSEN1
| SERMC_ODSEN2
);
1123 BA0WRITE4(sc
, CS4281_SERMC
, mem
);
1126 /* Initialize SSCR register features */
1127 /* XXX: hardware volume setting */
1128 BA0WRITE4(sc
, CS4281_SSCR
, ~SSCR_HVC
); /* disable HW volume setting */
1131 /* disable Sound Blaster Pro emulation */
1133 * Cannot set since the documents does not describe which bit is
1134 * correspond to SSCR_SB. Since the reset value of SSCR is 0,
1135 * we can ignore it.*/
1137 BA0WRITE4(sc
, CS4281_SSCR
, SSCR_SB
);
1140 /* map AC97 PCM playback to DMA Channel 0 */
1141 /* Reset FEN bit to setup first */
1142 BA0WRITE4(sc
, CS4281_FCR0
, (BA0READ4(sc
, CS4281_FCR0
) & ~FCRn_FEN
));
1145 *| LS[4:0] | AC97 | Slot Function
1146 *|---------+--------+--------------------
1147 *| 0 | 3 | Left PCM Playback
1148 *| 1 | 4 | Right PCM Playback
1149 *| 2 | 5 | Phone Line 1 DAC
1150 *| 3 | 6 | Center PCM Playback
1152 * quoted from Table 29(p109)
1154 dat32
= 0x01 << 24 | /* RS[4:0] = 1 see above */
1155 0x00 << 16 | /* LS[4:0] = 0 see above */
1156 0x0f << 8 | /* SZ[6:0] = 15 size of buffer */
1157 0x00 << 0 ; /* OF[6:0] = 0 offset */
1158 BA0WRITE4(sc
, CS4281_FCR0
, dat32
);
1159 BA0WRITE4(sc
, CS4281_FCR0
, dat32
| FCRn_FEN
);
1161 /* map AC97 PCM record to DMA Channel 1 */
1162 /* Reset FEN bit to setup first */
1163 BA0WRITE4(sc
, CS4281_FCR1
, (BA0READ4(sc
, CS4281_FCR1
) & ~FCRn_FEN
));
1166 *| LS[4:0] | AC97 | Slot Function
1167 *|---------+------+-------------------
1168 *| 10 | 3 | Left PCM Record
1169 *| 11 | 4 | Right PCM Record
1170 *| 12 | 5 | Phone Line 1 ADC
1173 * quoted from Table 30(p109)
1175 dat32
= 0x0b << 24 | /* RS[4:0] = 11 See above */
1176 0x0a << 16 | /* LS[4:0] = 10 See above */
1177 0x0f << 8 | /* SZ[6:0] = 15 Size of buffer */
1178 0x10 << 0 ; /* OF[6:0] = 16 offset */
1180 /* XXX: I cannot understand why FCRn_PSH is needed here. */
1181 BA0WRITE4(sc
, CS4281_FCR1
, dat32
| FCRn_PSH
);
1182 BA0WRITE4(sc
, CS4281_FCR1
, dat32
| FCRn_FEN
);
1185 /* Disable DMA Channel 2, 3 */
1186 BA0WRITE4(sc
, CS4281_FCR2
, (BA0READ4(sc
, CS4281_FCR2
) & ~FCRn_FEN
));
1187 BA0WRITE4(sc
, CS4281_FCR3
, (BA0READ4(sc
, CS4281_FCR3
) & ~FCRn_FEN
));
1190 /* Set the SRC Slot Assignment accordingly */
1192 *| PRSS[4:0] | AC97 | Slot Function
1193 *|-----------+------+----------------
1194 *| 0 | 3 | Left PCM Playback
1195 *| 1 | 4 | Right PCM Playback
1196 *| 2 | 5 | phone line 1 DAC
1197 *| 3 | 6 | Center PCM Playback
1198 *| 4 | 7 | Left Surround PCM Playback
1199 *| 5 | 8 | Right Surround PCM Playback
1203 *| CRSS[4:0] | AC97 | Codec |Slot Function
1204 *|-----------+------+-------+-----------------
1205 *| 10 | 3 |Primary| Left PCM Record
1206 *| 11 | 4 |Primary| Right PCM Record
1207 *| 12 | 5 |Primary| Phone Line 1 ADC
1208 *| 13 | 6 |Primary| Mic ADC
1210 *| 20 | 3 | Sec. | Left PCM Record
1211 *| 21 | 4 | Sec. | Right PCM Record
1212 *| 22 | 5 | Sec. | Phone Line 1 ADC
1213 *| 23 | 6 | Sec. | Mic ADC
1215 dat32
= 0x0b << 24 | /* CRSS[4:0] Right PCM Record(primary) */
1216 0x0a << 16 | /* CLSS[4:0] Left PCM Record(primary) */
1217 0x01 << 8 | /* PRSS[4:0] Right PCM Playback */
1218 0x00 << 0; /* PLSS[4:0] Left PCM Playback */
1219 BA0WRITE4(sc
, CS4281_SRCSA
, dat32
);
1221 /* Set interrupt to occurred at Half and Full terminal
1222 * count interrupt enable for DMA channel 0 and 1.
1223 * To keep DMA stop, set MSK.
1225 dat32
= DCRn_HTCIE
| DCRn_TCIE
| DCRn_MSK
;
1226 BA0WRITE4(sc
, CS4281_DCR0
, dat32
);
1227 BA0WRITE4(sc
, CS4281_DCR1
, dat32
);
1229 /* Set Auto-Initialize Contorl enable */
1230 BA0WRITE4(sc
, CS4281_DMR0
,
1231 DMRn_DMA
| DMRn_AUTO
| DMRn_TR_READ
);
1232 BA0WRITE4(sc
, CS4281_DMR1
,
1233 DMRn_DMA
| DMRn_AUTO
| DMRn_TR_WRITE
);
1235 /* Clear DMA Mask in HIMR */
1236 dat32
= ~HIMR_DMAIM
& ~HIMR_D1IM
& ~HIMR_D0IM
;
1237 BA0WRITE4(sc
, CS4281_HIMR
,
1238 BA0READ4(sc
, CS4281_HIMR
) & dat32
);
1240 /* set current status */
1246 /* setup playback volume */
1247 BA0WRITE4(sc
, CS4281_PPRVC
, 7);
1248 BA0WRITE4(sc
, CS4281_PPLVC
, 7);