2 * Copyright (c) 2000 Orion Hodson <O.Hodson@cs.ucl.ac.uk>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHERIN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THEPOSSIBILITY OF
26 * The order of pokes in the initiation sequence is based on Linux
27 * driver by Thomas Sailer, gw boynton (wesb@crystal.cirrus.com), tom
28 * woller (twoller@crystal.cirrus.com). Shingo Watanabe (nabe@nabechan.org)
29 * contributed towards power management.
31 * $FreeBSD: src/sys/dev/sound/pci/cs4281.c,v 1.22 2005/03/01 08:58:05 imp Exp $
34 #include <dev/sound/pcm/sound.h>
35 #include <dev/sound/pcm/ac97.h>
37 #include <bus/pci/pcireg.h>
38 #include <bus/pci/pcivar.h>
40 #include <dev/sound/pci/cs4281.h>
42 #define CS4281_DEFAULT_BUFSZ 16384
44 /* Max fifo size for full duplex is 64 */
45 #define CS4281_FIFO_SIZE 15
47 /* DMA Engine Indices */
48 #define CS4281_DMA_PLAY 0
49 #define CS4281_DMA_REC 1
53 #define inline __inline
56 #define DEB(x) /* x */
59 /* ------------------------------------------------------------------------- */
64 /* channel registers */
66 struct sc_info *parent;
68 struct snd_dbuf *buffer;
69 struct pcm_channel *channel;
71 u_int32_t spd, fmt, bps, blksz;
73 int dma_setup, dma_active, dma_chan;
76 /* device private data */
82 bus_space_handle_t sh;
83 bus_dma_tag_t parent_dmat;
85 struct resource *reg, *irq, *mem;
86 int regtype, regid, irqid, memid;
95 /* -------------------------------------------------------------------- */
99 static u_int32_t adcdac_go(struct sc_chinfo *ch, u_int32_t go);
100 static void adcdac_prog(struct sc_chinfo *ch);
102 /* power management and interrupt control */
103 static void cs4281_intr(void *);
104 static int cs4281_power(struct sc_info *, int);
105 static int cs4281_init(struct sc_info *);
107 /* talk to the card */
108 static u_int32_t cs4281_rd(struct sc_info *, int);
109 static void cs4281_wr(struct sc_info *, int, u_int32_t);
112 static u_int8_t cs4281_rate_to_rv(u_int32_t);
113 static u_int32_t cs4281_format_to_dmr(u_int32_t);
114 static u_int32_t cs4281_format_to_bps(u_int32_t);
116 /* -------------------------------------------------------------------- */
117 /* formats (do not add formats without editing cs_fmt_tab) */
119 static u_int32_t cs4281_fmts[] = {
121 AFMT_U8 | AFMT_STEREO,
123 AFMT_S8 | AFMT_STEREO,
125 AFMT_S16_LE | AFMT_STEREO,
127 AFMT_U16_LE | AFMT_STEREO,
129 AFMT_S16_BE | AFMT_STEREO,
131 AFMT_U16_BE | AFMT_STEREO,
135 static struct pcmchan_caps cs4281_caps = {6024, 48000, cs4281_fmts, 0};
137 /* -------------------------------------------------------------------- */
140 static inline u_int32_t
141 cs4281_rd(struct sc_info *sc, int regno)
143 return bus_space_read_4(sc->st, sc->sh, regno);
147 cs4281_wr(struct sc_info *sc, int regno, u_int32_t data)
149 bus_space_write_4(sc->st, sc->sh, regno, data);
154 cs4281_clr4(struct sc_info *sc, int regno, u_int32_t mask)
157 r = cs4281_rd(sc, regno);
158 cs4281_wr(sc, regno, r & ~mask);
162 cs4281_set4(struct sc_info *sc, int regno, u_int32_t mask)
165 v = cs4281_rd(sc, regno);
166 cs4281_wr(sc, regno, v | mask);
170 cs4281_waitset(struct sc_info *sc, int regno, u_int32_t mask, int tries)
176 v = cs4281_rd(sc, regno);
177 if ((v & mask) == mask) break;
184 cs4281_waitclr(struct sc_info *sc, int regno, u_int32_t mask, int tries)
190 v = ~ cs4281_rd(sc, regno);
197 /* ------------------------------------------------------------------------- */
198 /* Register value mapping functions */
200 static u_int32_t cs4281_rates[] = {48000, 44100, 22050, 16000, 11025, 8000};
201 #define CS4281_NUM_RATES NELEM(cs4281_rates)
204 cs4281_rate_to_rv(u_int32_t rate)
208 for (v = 0; v < CS4281_NUM_RATES; v++) {
209 if (rate == cs4281_rates[v]) return v;
213 if (v > 255 || v < 32) v = 5; /* default to 8k */
218 cs4281_rv_to_rate(u_int8_t rv)
222 if (rv < CS4281_NUM_RATES) return cs4281_rates[rv];
227 static inline u_int32_t
228 cs4281_format_to_dmr(u_int32_t format)
231 if (AFMT_8BIT & format) dmr |= CS4281PCI_DMR_SIZE8;
232 if (!(AFMT_STEREO & format)) dmr |= CS4281PCI_DMR_MONO;
233 if (AFMT_BIGENDIAN & format) dmr |= CS4281PCI_DMR_BEND;
234 if (!(AFMT_SIGNED & format)) dmr |= CS4281PCI_DMR_USIGN;
238 static inline u_int32_t
239 cs4281_format_to_bps(u_int32_t format)
241 return ((AFMT_8BIT & format) ? 1 : 2) * ((AFMT_STEREO & format) ? 2 : 1);
244 /* -------------------------------------------------------------------- */
248 cs4281_rdcd(kobj_t obj, void *devinfo, int regno)
250 struct sc_info *sc = (struct sc_info *)devinfo;
253 codecno = regno >> 8;
256 /* Remove old state */
257 cs4281_rd(sc, CS4281PCI_ACSDA);
259 /* Fill in AC97 register value request form */
260 cs4281_wr(sc, CS4281PCI_ACCAD, regno);
261 cs4281_wr(sc, CS4281PCI_ACCDA, 0);
262 cs4281_wr(sc, CS4281PCI_ACCTL, CS4281PCI_ACCTL_ESYN |
263 CS4281PCI_ACCTL_VFRM | CS4281PCI_ACCTL_DCV |
264 CS4281PCI_ACCTL_CRW);
266 /* Wait for read to complete */
267 if (cs4281_waitclr(sc, CS4281PCI_ACCTL, CS4281PCI_ACCTL_DCV, 250) == 0) {
268 device_printf(sc->dev, "cs4281_rdcd: DCV did not go\n");
272 /* Wait for valid status */
273 if (cs4281_waitset(sc, CS4281PCI_ACSTS, CS4281PCI_ACSTS_VSTS, 250) == 0) {
274 device_printf(sc->dev,"cs4281_rdcd: VSTS did not come\n");
278 return cs4281_rd(sc, CS4281PCI_ACSDA);
282 cs4281_wrcd(kobj_t obj, void *devinfo, int regno, u_int32_t data)
284 struct sc_info *sc = (struct sc_info *)devinfo;
287 codecno = regno >> 8;
290 cs4281_wr(sc, CS4281PCI_ACCAD, regno);
291 cs4281_wr(sc, CS4281PCI_ACCDA, data);
292 cs4281_wr(sc, CS4281PCI_ACCTL, CS4281PCI_ACCTL_ESYN |
293 CS4281PCI_ACCTL_VFRM | CS4281PCI_ACCTL_DCV);
295 if (cs4281_waitclr(sc, CS4281PCI_ACCTL, CS4281PCI_ACCTL_DCV, 250) == 0) {
296 device_printf(sc->dev,"cs4281_wrcd: DCV did not go\n");
300 static kobj_method_t cs4281_ac97_methods[] = {
301 KOBJMETHOD(ac97_read, cs4281_rdcd),
302 KOBJMETHOD(ac97_write, cs4281_wrcd),
305 AC97_DECLARE(cs4281_ac97);
307 /* ------------------------------------------------------------------------- */
308 /* shared rec/play channel interface */
311 cs4281chan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
313 struct sc_info *sc = devinfo;
314 struct sc_chinfo *ch = (dir == PCMDIR_PLAY) ? &sc->pch : &sc->rch;
317 if (sndbuf_alloc(ch->buffer, sc->parent_dmat, sc->bufsz) != 0) {
324 ch->spd = DSP_DEFAULT_SPEED;
326 ch->blksz = sndbuf_getsize(ch->buffer);
328 ch->dma_chan = (dir == PCMDIR_PLAY) ? CS4281_DMA_PLAY : CS4281_DMA_REC;
338 cs4281chan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
340 struct sc_chinfo *ch = data;
341 struct sc_info *sc = ch->parent;
344 go = adcdac_go(ch, 0);
346 /* 2 interrupts are possible and used in buffer (half-empty,empty),
347 * hence factor of 2. */
348 ch->blksz = MIN(blocksize, sc->bufsz / 2);
349 sndbuf_resize(ch->buffer, 2, ch->blksz);
354 DEB(kprintf("cs4281chan_setblocksize: blksz %d Setting %d\n", blocksize, ch->blksz));
360 cs4281chan_setspeed(kobj_t obj, void *data, u_int32_t speed)
362 struct sc_chinfo *ch = data;
363 struct sc_info *sc = ch->parent;
366 go = adcdac_go(ch, 0); /* pause */
367 r = (ch->dma_chan == CS4281_DMA_PLAY) ? CS4281PCI_DACSR : CS4281PCI_ADCSR;
368 v = cs4281_rate_to_rv(speed);
370 adcdac_go(ch, go); /* unpause */
372 ch->spd = cs4281_rv_to_rate(v);
377 cs4281chan_setformat(kobj_t obj, void *data, u_int32_t format)
379 struct sc_chinfo *ch = data;
380 struct sc_info *sc = ch->parent;
383 go = adcdac_go(ch, 0); /* pause */
385 if (ch->dma_chan == CS4281_DMA_PLAY)
386 v = CS4281PCI_DMR_TR_PLAY;
388 v = CS4281PCI_DMR_TR_REC;
389 v |= CS4281PCI_DMR_DMA | CS4281PCI_DMR_AUTO;
390 v |= cs4281_format_to_dmr(format);
391 cs4281_wr(sc, CS4281PCI_DMR(ch->dma_chan), v);
393 adcdac_go(ch, go); /* unpause */
396 ch->bps = cs4281_format_to_bps(format);
403 cs4281chan_getptr(kobj_t obj, void *data)
405 struct sc_chinfo *ch = data;
406 struct sc_info *sc = ch->parent;
407 u_int32_t dba, dca, ptr;
410 sz = sndbuf_getsize(ch->buffer);
411 dba = cs4281_rd(sc, CS4281PCI_DBA(ch->dma_chan));
412 dca = cs4281_rd(sc, CS4281PCI_DCA(ch->dma_chan));
413 ptr = (dca - dba + sz) % sz;
419 cs4281chan_trigger(kobj_t obj, void *data, int go)
421 struct sc_chinfo *ch = data;
439 static struct pcmchan_caps *
440 cs4281chan_getcaps(kobj_t obj, void *data)
445 static kobj_method_t cs4281chan_methods[] = {
446 KOBJMETHOD(channel_init, cs4281chan_init),
447 KOBJMETHOD(channel_setformat, cs4281chan_setformat),
448 KOBJMETHOD(channel_setspeed, cs4281chan_setspeed),
449 KOBJMETHOD(channel_setblocksize, cs4281chan_setblocksize),
450 KOBJMETHOD(channel_trigger, cs4281chan_trigger),
451 KOBJMETHOD(channel_getptr, cs4281chan_getptr),
452 KOBJMETHOD(channel_getcaps, cs4281chan_getcaps),
455 CHANNEL_DECLARE(cs4281chan);
457 /* -------------------------------------------------------------------- */
458 /* ADC/DAC control */
460 /* adcdac_go enables/disable DMA channel, returns non-zero if DMA was
461 * active before call */
464 adcdac_go(struct sc_chinfo *ch, u_int32_t go)
466 struct sc_info *sc = ch->parent;
469 going = !(cs4281_rd(sc, CS4281PCI_DCR(ch->dma_chan)) & CS4281PCI_DCR_MSK);
472 cs4281_clr4(sc, CS4281PCI_DCR(ch->dma_chan), CS4281PCI_DCR_MSK);
474 cs4281_set4(sc, CS4281PCI_DCR(ch->dma_chan), CS4281PCI_DCR_MSK);
476 cs4281_wr(sc, CS4281PCI_HICR, CS4281PCI_HICR_EOI);
482 adcdac_prog(struct sc_chinfo *ch)
484 struct sc_info *sc = ch->parent;
487 if (!ch->dma_setup) {
488 go = adcdac_go(ch, 0);
489 cs4281_wr(sc, CS4281PCI_DBA(ch->dma_chan),
490 sndbuf_getbufaddr(ch->buffer));
491 cs4281_wr(sc, CS4281PCI_DBC(ch->dma_chan),
492 sndbuf_getsize(ch->buffer) / ch->bps - 1);
498 /* -------------------------------------------------------------------- */
499 /* The interrupt handler */
504 struct sc_info *sc = (struct sc_info *)p;
507 hisr = cs4281_rd(sc, CS4281PCI_HISR);
509 if (hisr == 0) return;
511 if (hisr & CS4281PCI_HISR_DMA(CS4281_DMA_PLAY)) {
512 chn_intr(sc->pch.channel);
513 cs4281_rd(sc, CS4281PCI_HDSR(CS4281_DMA_PLAY)); /* Clear interrupt */
516 if (hisr & CS4281PCI_HISR_DMA(CS4281_DMA_REC)) {
517 chn_intr(sc->rch.channel);
518 cs4281_rd(sc, CS4281PCI_HDSR(CS4281_DMA_REC)); /* Clear interrupt */
521 /* Signal End-of-Interrupt */
522 cs4281_wr(sc, CS4281PCI_HICR, CS4281PCI_HICR_EOI);
525 /* -------------------------------------------------------------------- */
526 /* power management related */
529 cs4281_power(struct sc_info *sc, int state)
534 /* Permit r/w access to all BA0 registers */
535 cs4281_wr(sc, CS4281PCI_CWPR, CS4281PCI_CWPR_MAGIC);
537 cs4281_clr4(sc, CS4281PCI_EPPMC, CS4281PCI_EPPMC_FPDN);
540 /* Power off card and codec */
541 cs4281_set4(sc, CS4281PCI_EPPMC, CS4281PCI_EPPMC_FPDN);
542 cs4281_clr4(sc, CS4281PCI_SPMC, CS4281PCI_SPMC_RSTN);
546 DEB(kprintf("cs4281_power %d -> %d\n", sc->power, state));
553 cs4281_init(struct sc_info *sc)
557 /* (0) Blast clock register and serial port */
558 cs4281_wr(sc, CS4281PCI_CLKCR1, 0);
559 cs4281_wr(sc, CS4281PCI_SERMC, 0);
561 /* (1) Make ESYN 0 to turn sync pulse on AC97 link */
562 cs4281_wr(sc, CS4281PCI_ACCTL, 0);
565 /* (2) Effect Reset */
566 cs4281_wr(sc, CS4281PCI_SPMC, 0);
568 cs4281_wr(sc, CS4281PCI_SPMC, CS4281PCI_SPMC_RSTN);
569 /* Wait 50ms for ABITCLK to become stable */
572 /* (3) Enable Sound System Clocks */
573 cs4281_wr(sc, CS4281PCI_CLKCR1, CS4281PCI_CLKCR1_DLLP);
574 DELAY(50000); /* Wait for PLL to stabilize */
575 cs4281_wr(sc, CS4281PCI_CLKCR1,
576 CS4281PCI_CLKCR1_DLLP | CS4281PCI_CLKCR1_SWCE);
578 /* (4) Power Up - this combination is essential. */
579 cs4281_set4(sc, CS4281PCI_SSPM,
580 CS4281PCI_SSPM_ACLEN | CS4281PCI_SSPM_PSRCEN |
581 CS4281PCI_SSPM_CSRCEN | CS4281PCI_SSPM_MIXEN);
583 /* (5) Wait for clock stabilization */
584 if (cs4281_waitset(sc,
586 CS4281PCI_CLKCR1_DLLRDY,
588 device_printf(sc->dev, "Clock stabilization failed\n");
592 /* (6) Enable ASYNC generation. */
593 cs4281_wr(sc, CS4281PCI_ACCTL,CS4281PCI_ACCTL_ESYN);
595 /* Wait to allow AC97 to start generating clock bit */
598 /* Set AC97 timing */
599 cs4281_wr(sc, CS4281PCI_SERMC, CS4281PCI_SERMC_PTC_AC97);
601 /* (7) Wait for AC97 ready signal */
602 if (cs4281_waitset(sc, CS4281PCI_ACSTS, CS4281PCI_ACSTS_CRDY, 250) == 0) {
603 device_printf(sc->dev, "codec did not avail\n");
607 /* (8) Assert valid frame signal to begin sending commands to
611 CS4281PCI_ACCTL_VFRM | CS4281PCI_ACCTL_ESYN);
613 /* (9) Wait for codec calibration */
614 for(i = 0 ; i < 1000; i++) {
616 v = cs4281_rdcd(0, sc, AC97_REG_POWER);
617 if ((v & 0x0f) == 0x0f) {
622 device_printf(sc->dev, "codec failed to calibrate\n");
626 /* (10) Set AC97 timing */
627 cs4281_wr(sc, CS4281PCI_SERMC, CS4281PCI_SERMC_PTC_AC97);
629 /* (11) Wait for valid data to arrive */
630 if (cs4281_waitset(sc,
632 CS4281PCI_ACISV_ISV(3) | CS4281PCI_ACISV_ISV(4),
634 device_printf(sc->dev, "cs4281 never got valid data\n");
638 /* (12) Start digital data transfer of audio data to codec */
641 CS4281PCI_ACOSV_SLV(3) | CS4281PCI_ACOSV_SLV(4));
643 /* Set Master and headphone to max */
644 cs4281_wrcd(0, sc, AC97_MIX_AUXOUT, 0);
645 cs4281_wrcd(0, sc, AC97_MIX_MASTER, 0);
647 /* Power on the DAC */
648 v = cs4281_rdcd(0, sc, AC97_REG_POWER) & 0xfdff;
649 cs4281_wrcd(0, sc, AC97_REG_POWER, v);
651 /* Wait until DAC state ready */
652 for(i = 0; i < 320; i++) {
654 v = cs4281_rdcd(0, sc, AC97_REG_POWER);
658 /* Power on the ADC */
659 v = cs4281_rdcd(0, sc, AC97_REG_POWER) & 0xfeff;
660 cs4281_wrcd(0, sc, AC97_REG_POWER, v);
662 /* Wait until ADC state ready */
663 for(i = 0; i < 320; i++) {
665 v = cs4281_rdcd(0, sc, AC97_REG_POWER);
669 /* FIFO configuration (driver is DMA orientated, implicit FIFO) */
672 v = CS4281PCI_FCR_RS(CS4281PCI_RPCM_PLAY_SLOT) |
673 CS4281PCI_FCR_LS(CS4281PCI_LPCM_PLAY_SLOT) |
674 CS4281PCI_FCR_SZ(CS4281_FIFO_SIZE)|
676 cs4281_wr(sc, CS4281PCI_FCR(CS4281_DMA_PLAY), v);
678 cs4281_wr(sc, CS4281PCI_FCR(CS4281_DMA_PLAY), v | CS4281PCI_FCR_FEN);
681 v = CS4281PCI_FCR_RS(CS4281PCI_RPCM_REC_SLOT) |
682 CS4281PCI_FCR_LS(CS4281PCI_LPCM_REC_SLOT) |
683 CS4281PCI_FCR_SZ(CS4281_FIFO_SIZE)|
684 CS4281PCI_FCR_OF(CS4281_FIFO_SIZE + 1);
685 cs4281_wr(sc, CS4281PCI_FCR(CS4281_DMA_REC), v | CS4281PCI_FCR_PSH);
686 cs4281_wr(sc, CS4281PCI_FCR(CS4281_DMA_REC), v | CS4281PCI_FCR_FEN);
688 /* Match AC97 slots to FIFOs */
689 v = CS4281PCI_SRCSA_PLSS(CS4281PCI_LPCM_PLAY_SLOT) |
690 CS4281PCI_SRCSA_PRSS(CS4281PCI_RPCM_PLAY_SLOT) |
691 CS4281PCI_SRCSA_CLSS(CS4281PCI_LPCM_REC_SLOT) |
692 CS4281PCI_SRCSA_CRSS(CS4281PCI_RPCM_REC_SLOT);
693 cs4281_wr(sc, CS4281PCI_SRCSA, v);
695 /* Set Auto-Initialize and set directions */
697 CS4281PCI_DMR(CS4281_DMA_PLAY),
700 CS4281PCI_DMR_TR_PLAY);
702 CS4281PCI_DMR(CS4281_DMA_REC),
705 CS4281PCI_DMR_TR_REC);
707 /* Enable half and empty buffer interrupts keeping DMA paused */
709 CS4281PCI_DCR(CS4281_DMA_PLAY),
710 CS4281PCI_DCR_TCIE | CS4281PCI_DCR_HTCIE | CS4281PCI_DCR_MSK);
712 CS4281PCI_DCR(CS4281_DMA_REC),
713 CS4281PCI_DCR_TCIE | CS4281PCI_DCR_HTCIE | CS4281PCI_DCR_MSK);
715 /* Enable Interrupts */
718 CS4281PCI_HIMR_DMAI |
719 CS4281PCI_HIMR_DMA(CS4281_DMA_PLAY) |
720 CS4281PCI_HIMR_DMA(CS4281_DMA_REC));
722 /* Set playback volume */
723 cs4281_wr(sc, CS4281PCI_PPLVC, 7);
724 cs4281_wr(sc, CS4281PCI_PPRVC, 7);
729 /* -------------------------------------------------------------------- */
730 /* Probe and attach the card */
733 cs4281_pci_probe(device_t dev)
737 switch (pci_get_devid(dev)) {
739 s = "Crystal Semiconductor CS4281";
744 device_set_desc(dev, s);
745 return s ? BUS_PROBE_DEFAULT : ENXIO;
749 cs4281_pci_attach(device_t dev)
752 struct ac97_info *codec = NULL;
754 char status[SND_STATUSLEN];
756 sc = kmalloc(sizeof(*sc), M_DEVBUF, M_WAITOK | M_ZERO);
758 sc->type = pci_get_devid(dev);
760 data = pci_read_config(dev, PCIR_COMMAND, 2);
761 data |= (PCIM_CMD_PORTEN | PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN);
762 pci_write_config(dev, PCIR_COMMAND, data, 2);
764 #if __FreeBSD_version > 500000
765 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
766 /* Reset the power state. */
767 device_printf(dev, "chip is in D%d power mode "
768 "-- setting to D0\n", pci_get_powerstate(dev));
770 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
773 data = pci_read_config(dev, CS4281PCI_PMCS_OFFSET, 4);
774 if (data & CS4281PCI_PMCS_PS_MASK) {
775 /* Reset the power state. */
776 device_printf(dev, "chip is in D%d power mode "
777 "-- setting to D0\n",
778 data & CS4281PCI_PMCS_PS_MASK);
779 pci_write_config(dev, CS4281PCI_PMCS_OFFSET,
780 data & ~CS4281PCI_PMCS_PS_MASK, 4);
784 sc->regid = PCIR_BAR(0);
785 sc->regtype = SYS_RES_MEMORY;
786 sc->reg = bus_alloc_resource(dev, sc->regtype, &sc->regid,
787 0, ~0, CS4281PCI_BA0_SIZE, RF_ACTIVE);
789 sc->regtype = SYS_RES_IOPORT;
790 sc->reg = bus_alloc_resource(dev, sc->regtype, &sc->regid,
791 0, ~0, CS4281PCI_BA0_SIZE, RF_ACTIVE);
793 device_printf(dev, "unable to allocate register space\n");
797 sc->st = rman_get_bustag(sc->reg);
798 sc->sh = rman_get_bushandle(sc->reg);
800 sc->memid = PCIR_BAR(1);
801 sc->mem = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->memid, 0,
802 ~0, CS4281PCI_BA1_SIZE, RF_ACTIVE);
803 if (sc->mem == NULL) {
804 device_printf(dev, "unable to allocate fifo space\n");
809 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irqid,
810 RF_ACTIVE | RF_SHAREABLE);
812 device_printf(dev, "unable to allocate interrupt\n");
816 if (snd_setup_intr(dev, sc->irq, 0, cs4281_intr, sc, &sc->ih)) {
817 device_printf(dev, "unable to setup interrupt\n");
821 sc->bufsz = pcm_getbuffersize(dev, 4096, CS4281_DEFAULT_BUFSZ, 65536);
823 if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0,
824 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
825 /*highaddr*/BUS_SPACE_MAXADDR,
826 /*filter*/NULL, /*filterarg*/NULL,
827 /*maxsize*/sc->bufsz, /*nsegments*/1,
830 &sc->parent_dmat) != 0) {
831 device_printf(dev, "unable to create dma tag\n");
839 if (cs4281_init(sc) == -1) {
840 device_printf(dev, "unable to initialize the card\n");
844 /* create/init mixer */
845 codec = AC97_CREATE(dev, sc, cs4281_ac97);
849 mixer_init(dev, ac97_getmixerclass(), codec);
851 if (pcm_register(dev, sc, 1, 1))
854 pcm_addchan(dev, PCMDIR_PLAY, &cs4281chan_class, sc);
855 pcm_addchan(dev, PCMDIR_REC, &cs4281chan_class, sc);
857 ksnprintf(status, SND_STATUSLEN, "at %s 0x%lx irq %ld %s",
858 (sc->regtype == SYS_RES_IOPORT)? "io" : "memory",
859 rman_get_start(sc->reg), rman_get_start(sc->irq),PCM_KLDSTRING(snd_cs4281));
860 pcm_setstatus(dev, status);
868 bus_release_resource(dev, sc->regtype, sc->regid, sc->reg);
870 bus_release_resource(dev, SYS_RES_MEMORY, sc->memid, sc->mem);
872 bus_teardown_intr(dev, sc->irq, sc->ih);
874 bus_release_resource(dev, SYS_RES_IRQ, sc->irqid, sc->irq);
876 bus_dma_tag_destroy(sc->parent_dmat);
883 cs4281_pci_detach(device_t dev)
888 r = pcm_unregister(dev);
892 sc = pcm_getdevinfo(dev);
897 bus_release_resource(dev, sc->regtype, sc->regid, sc->reg);
898 bus_release_resource(dev, SYS_RES_MEMORY, sc->memid, sc->mem);
899 bus_teardown_intr(dev, sc->irq, sc->ih);
900 bus_release_resource(dev, SYS_RES_IRQ, sc->irqid, sc->irq);
901 bus_dma_tag_destroy(sc->parent_dmat);
908 cs4281_pci_suspend(device_t dev)
912 sc = pcm_getdevinfo(dev);
914 sc->rch.dma_active = adcdac_go(&sc->rch, 0);
915 sc->pch.dma_active = adcdac_go(&sc->pch, 0);
923 cs4281_pci_resume(device_t dev)
927 sc = pcm_getdevinfo(dev);
932 /* initialize chip */
933 if (cs4281_init(sc) == -1) {
934 device_printf(dev, "unable to reinitialize the card\n");
938 /* restore mixer state */
939 if (mixer_reinit(dev) == -1) {
940 device_printf(dev, "unable to reinitialize the mixer\n");
944 /* restore chip state */
945 cs4281chan_setspeed(NULL, &sc->rch, sc->rch.spd);
946 cs4281chan_setblocksize(NULL, &sc->rch, sc->rch.blksz);
947 cs4281chan_setformat(NULL, &sc->rch, sc->rch.fmt);
948 adcdac_go(&sc->rch, sc->rch.dma_active);
950 cs4281chan_setspeed(NULL, &sc->pch, sc->pch.spd);
951 cs4281chan_setblocksize(NULL, &sc->pch, sc->pch.blksz);
952 cs4281chan_setformat(NULL, &sc->pch, sc->pch.fmt);
953 adcdac_go(&sc->pch, sc->pch.dma_active);
958 static device_method_t cs4281_methods[] = {
959 /* Device interface */
960 DEVMETHOD(device_probe, cs4281_pci_probe),
961 DEVMETHOD(device_attach, cs4281_pci_attach),
962 DEVMETHOD(device_detach, cs4281_pci_detach),
963 DEVMETHOD(device_suspend, cs4281_pci_suspend),
964 DEVMETHOD(device_resume, cs4281_pci_resume),
968 static driver_t cs4281_driver = {
974 DRIVER_MODULE(snd_cs4281, pci, cs4281_driver, pcm_devclass, NULL, NULL);
975 MODULE_DEPEND(snd_cs4281, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
976 MODULE_VERSION(snd_cs4281, 1);