2 * Copyright (c) 2005 Ariff Abdullah <ariff@FreeBSD.org>
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 * $FreeBSD: src/sys/dev/sound/pci/atiixp.c,v 1.2.2.7 2007/04/26 08:21:44 ariff Exp $
27 * $DragonFly: src/sys/dev/sound/pci/atiixp.c,v 1.5 2007/06/16 20:07:19 dillon Exp $
31 * FreeBSD pcm driver for ATI IXP 150/200/250/300 AC97 controllers
34 * * 16bit playback / recording
35 * * 32bit native playback - yay!
36 * * 32bit native recording (seems broken on few hardwares)
40 * * Support for more than 2 channels.
42 * * 32bit native recording seems broken on few hardwares, most
43 * probably because of incomplete VRA/DRA cleanup.
48 * Shaharil @ SCAN Associates whom relentlessly providing me the
49 * mind blowing Acer Ferrari 4002 WLMi with this ATI IXP hardware.
51 * Reinoud Zandijk <reinoud@NetBSD.org> (auixp), which this driver is
52 * largely based upon although large part of it has been reworked. His
53 * driver is the primary reference and pretty much well documented.
55 * Takashi Iwai (ALSA snd-atiixp), for register definitions and some
56 * random ninja hackery.
59 #include <dev/sound/pcm/sound.h>
60 #include <dev/sound/pcm/ac97.h>
62 #include <bus/pci/pcireg.h>
63 #include <bus/pci/pcivar.h>
64 #include <sys/sysctl.h>
65 #include <sys/endian.h>
67 #include <dev/sound/pci/atiixp.h>
69 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pci/atiixp.c,v 1.5 2007/06/16 20:07:19 dillon Exp $");
71 struct atiixp_dma_op
{
72 volatile uint32_t addr
;
73 volatile uint16_t status
;
74 volatile uint16_t size
;
75 volatile uint32_t next
;
80 struct atiixp_chinfo
{
81 struct snd_dbuf
*buffer
;
82 struct pcm_channel
*channel
;
83 struct atiixp_info
*parent
;
84 struct atiixp_dma_op
*sgd_table
;
86 uint32_t enable_bit
, flush_bit
, linkptr_bit
, dma_dt_cur_bit
;
89 int caps_32bit
, dir
, active
;
96 bus_space_handle_t sh
;
97 bus_dma_tag_t parent_dmat
;
98 bus_dma_tag_t sgd_dmat
;
99 bus_dmamap_t sgd_dmamap
;
102 struct resource
*reg
, *irq
;
103 int regtype
, regid
, irqid
;
105 struct ac97_info
*codec
;
107 struct atiixp_chinfo pch
;
108 struct atiixp_chinfo rch
;
109 struct atiixp_dma_op
*sgd_table
;
110 struct intr_config_hook delayed_attach
;
113 uint32_t codec_not_ready_bits
, codec_idx
, codec_found
;
115 int registered_channels
;
120 #define atiixp_rd(_sc, _reg) \
121 bus_space_read_4((_sc)->st, (_sc)->sh, _reg)
122 #define atiixp_wr(_sc, _reg, _val) \
123 bus_space_write_4((_sc)->st, (_sc)->sh, _reg, _val)
125 #define atiixp_lock(_sc) snd_mtxlock((_sc)->lock)
126 #define atiixp_unlock(_sc) snd_mtxunlock((_sc)->lock)
127 #define atiixp_assert(_sc) snd_mtxassert((_sc)->lock)
129 static uint32_t atiixp_fmt_32bit
[] = {
130 AFMT_STEREO
| AFMT_S16_LE
,
131 AFMT_STEREO
| AFMT_S32_LE
,
135 static uint32_t atiixp_fmt
[] = {
136 AFMT_STEREO
| AFMT_S16_LE
,
140 static struct pcmchan_caps atiixp_caps_32bit
= {
146 static struct pcmchan_caps atiixp_caps
= {
152 static const struct {
157 { ATI_VENDOR_ID
, ATI_IXP_200_ID
, "ATI IXP 200" },
158 { ATI_VENDOR_ID
, ATI_IXP_300_ID
, "ATI IXP 300" },
159 { ATI_VENDOR_ID
, ATI_IXP_400_ID
, "ATI IXP 400" },
162 static void atiixp_enable_interrupts(struct atiixp_info
*);
163 static void atiixp_disable_interrupts(struct atiixp_info
*);
164 static void atiixp_reset_aclink(struct atiixp_info
*);
165 static void atiixp_flush_dma(struct atiixp_info
*, struct atiixp_chinfo
*);
166 static void atiixp_enable_dma(struct atiixp_info
*, struct atiixp_chinfo
*);
167 static void atiixp_disable_dma(struct atiixp_info
*, struct atiixp_chinfo
*);
169 static int atiixp_waitready_codec(struct atiixp_info
*);
170 static int atiixp_rdcd(kobj_t
, void *, int);
171 static int atiixp_wrcd(kobj_t
, void *, int, uint32_t);
173 static void *atiixp_chan_init(kobj_t
, void *, struct snd_dbuf
*,
174 struct pcm_channel
*, int);
175 static int atiixp_chan_setformat(kobj_t
, void *, uint32_t);
176 static int atiixp_chan_setspeed(kobj_t
, void *, uint32_t);
177 static int atiixp_chan_setblocksize(kobj_t
, void *, uint32_t);
178 static void atiixp_buildsgdt(struct atiixp_chinfo
*);
179 static int atiixp_chan_trigger(kobj_t
, void *, int);
180 static int atiixp_chan_getptr(kobj_t
, void *);
181 static struct pcmchan_caps
*atiixp_chan_getcaps(kobj_t
, void *);
183 static void atiixp_intr(void *);
184 static void atiixp_dma_cb(void *, bus_dma_segment_t
*, int, int);
185 static void atiixp_chip_pre_init(struct atiixp_info
*);
186 static void atiixp_chip_post_init(void *);
187 static void atiixp_release_resource(struct atiixp_info
*);
188 static int atiixp_pci_probe(device_t
);
189 static int atiixp_pci_attach(device_t
);
190 static int atiixp_pci_detach(device_t
);
191 static int atiixp_pci_suspend(device_t
);
192 static int atiixp_pci_resume(device_t
);
195 * ATI IXP helper functions
198 atiixp_enable_interrupts(struct atiixp_info
*sc
)
202 /* clear all pending */
203 atiixp_wr(sc
, ATI_REG_ISR
, 0xffffffff);
205 /* enable all relevant interrupt sources we can handle */
206 value
= atiixp_rd(sc
, ATI_REG_IER
);
208 value
|= ATI_REG_IER_IO_STATUS_EN
;
211 * Disable / ignore internal xrun/spdf interrupt flags
212 * since it doesn't interest us (for now).
215 value
|= ATI_REG_IER_IN_XRUN_EN
;
216 value
|= ATI_REG_IER_OUT_XRUN_EN
;
218 value
|= ATI_REG_IER_SPDF_XRUN_EN
;
219 value
|= ATI_REG_IER_SPDF_STATUS_EN
;
222 atiixp_wr(sc
, ATI_REG_IER
, value
);
226 atiixp_disable_interrupts(struct atiixp_info
*sc
)
228 /* disable all interrupt sources */
229 atiixp_wr(sc
, ATI_REG_IER
, 0);
231 /* clear all pending */
232 atiixp_wr(sc
, ATI_REG_ISR
, 0xffffffff);
236 atiixp_reset_aclink(struct atiixp_info
*sc
)
238 uint32_t value
, timeout
;
240 /* if power is down, power it up */
241 value
= atiixp_rd(sc
, ATI_REG_CMD
);
242 if (value
& ATI_REG_CMD_POWERDOWN
) {
243 /* explicitly enable power */
244 value
&= ~ATI_REG_CMD_POWERDOWN
;
245 atiixp_wr(sc
, ATI_REG_CMD
, value
);
247 /* have to wait at least 10 usec for it to initialise */
251 /* perform a soft reset */
252 value
= atiixp_rd(sc
, ATI_REG_CMD
);
253 value
|= ATI_REG_CMD_AC_SOFT_RESET
;
254 atiixp_wr(sc
, ATI_REG_CMD
, value
);
256 /* need to read the CMD reg and wait aprox. 10 usec to init */
257 value
= atiixp_rd(sc
, ATI_REG_CMD
);
260 /* clear soft reset flag again */
261 value
= atiixp_rd(sc
, ATI_REG_CMD
);
262 value
&= ~ATI_REG_CMD_AC_SOFT_RESET
;
263 atiixp_wr(sc
, ATI_REG_CMD
, value
);
265 /* check if the ac-link is working; reset device otherwise */
267 value
= atiixp_rd(sc
, ATI_REG_CMD
);
268 while (!(value
& ATI_REG_CMD_ACLINK_ACTIVE
)
271 device_printf(sc
->dev
, "not up; resetting aclink hardware\n");
274 /* dip aclink reset but keep the acsync */
275 value
&= ~ATI_REG_CMD_AC_RESET
;
276 value
|= ATI_REG_CMD_AC_SYNC
;
277 atiixp_wr(sc
, ATI_REG_CMD
, value
);
279 /* need to read CMD again and wait again (clocking in issue?) */
280 value
= atiixp_rd(sc
, ATI_REG_CMD
);
283 /* assert aclink reset again */
284 value
= atiixp_rd(sc
, ATI_REG_CMD
);
285 value
|= ATI_REG_CMD_AC_RESET
;
286 atiixp_wr(sc
, ATI_REG_CMD
, value
);
288 /* check if its active now */
289 value
= atiixp_rd(sc
, ATI_REG_CMD
);
293 device_printf(sc
->dev
, "giving up aclink reset\n");
296 device_printf(sc
->dev
, "aclink hardware reset successful\n");
299 /* assert reset and sync for safety */
300 value
= atiixp_rd(sc
, ATI_REG_CMD
);
301 value
|= ATI_REG_CMD_AC_SYNC
| ATI_REG_CMD_AC_RESET
;
302 atiixp_wr(sc
, ATI_REG_CMD
, value
);
306 atiixp_flush_dma(struct atiixp_info
*sc
, struct atiixp_chinfo
*ch
)
308 atiixp_wr(sc
, ATI_REG_FIFO_FLUSH
, ch
->flush_bit
);
312 atiixp_enable_dma(struct atiixp_info
*sc
, struct atiixp_chinfo
*ch
)
316 value
= atiixp_rd(sc
, ATI_REG_CMD
);
317 if (!(value
& ch
->enable_bit
)) {
318 value
|= ch
->enable_bit
;
319 atiixp_wr(sc
, ATI_REG_CMD
, value
);
324 atiixp_disable_dma(struct atiixp_info
*sc
, struct atiixp_chinfo
*ch
)
328 value
= atiixp_rd(sc
, ATI_REG_CMD
);
329 if (value
& ch
->enable_bit
) {
330 value
&= ~ch
->enable_bit
;
331 atiixp_wr(sc
, ATI_REG_CMD
, value
);
339 atiixp_waitready_codec(struct atiixp_info
*sc
)
344 if ((atiixp_rd(sc
, ATI_REG_PHYS_OUT_ADDR
) &
345 ATI_REG_PHYS_OUT_ADDR_EN
) == 0)
354 atiixp_rdcd(kobj_t obj
, void *devinfo
, int reg
)
356 struct atiixp_info
*sc
= devinfo
;
360 if (atiixp_waitready_codec(sc
))
363 data
= (reg
<< ATI_REG_PHYS_OUT_ADDR_SHIFT
) |
364 ATI_REG_PHYS_OUT_ADDR_EN
|
365 ATI_REG_PHYS_OUT_RW
| sc
->codec_idx
;
367 atiixp_wr(sc
, ATI_REG_PHYS_OUT_ADDR
, data
);
369 if (atiixp_waitready_codec(sc
))
374 data
= atiixp_rd(sc
, ATI_REG_PHYS_IN_ADDR
);
375 if (data
& ATI_REG_PHYS_IN_READ_FLAG
)
376 return data
>> ATI_REG_PHYS_IN_DATA_SHIFT
;
381 device_printf(sc
->dev
, "codec read timeout! (reg 0x%x)\n", reg
);
387 atiixp_wrcd(kobj_t obj
, void *devinfo
, int reg
, uint32_t data
)
389 struct atiixp_info
*sc
= devinfo
;
391 if (atiixp_waitready_codec(sc
))
394 data
= (data
<< ATI_REG_PHYS_OUT_DATA_SHIFT
) |
395 (((uint32_t)reg
) << ATI_REG_PHYS_OUT_ADDR_SHIFT
) |
396 ATI_REG_PHYS_OUT_ADDR_EN
| sc
->codec_idx
;
398 atiixp_wr(sc
, ATI_REG_PHYS_OUT_ADDR
, data
);
403 static kobj_method_t atiixp_ac97_methods
[] = {
404 KOBJMETHOD(ac97_read
, atiixp_rdcd
),
405 KOBJMETHOD(ac97_write
, atiixp_wrcd
),
408 AC97_DECLARE(atiixp_ac97
);
411 * Playback / Record channel interface
414 atiixp_chan_init(kobj_t obj
, void *devinfo
, struct snd_dbuf
*b
,
415 struct pcm_channel
*c
, int dir
)
417 struct atiixp_info
*sc
= devinfo
;
418 struct atiixp_chinfo
*ch
;
423 if (dir
== PCMDIR_PLAY
) {
425 ch
->linkptr_bit
= ATI_REG_OUT_DMA_LINKPTR
;
426 ch
->enable_bit
= ATI_REG_CMD_OUT_DMA_EN
| ATI_REG_CMD_SEND_EN
;
427 ch
->flush_bit
= ATI_REG_FIFO_OUT_FLUSH
;
428 ch
->dma_dt_cur_bit
= ATI_REG_OUT_DMA_DT_CUR
;
429 /* Native 32bit playback working properly */
433 ch
->linkptr_bit
= ATI_REG_IN_DMA_LINKPTR
;
434 ch
->enable_bit
= ATI_REG_CMD_IN_DMA_EN
| ATI_REG_CMD_RECEIVE_EN
;
435 ch
->flush_bit
= ATI_REG_FIFO_IN_FLUSH
;
436 ch
->dma_dt_cur_bit
= ATI_REG_IN_DMA_DT_CUR
;
437 /* XXX Native 32bit recording appear to be broken */
445 ch
->dma_segs
= sc
->dma_segs
;
449 if (sndbuf_alloc(ch
->buffer
, sc
->parent_dmat
, sc
->bufsz
) == -1)
453 num
= sc
->registered_channels
++;
454 ch
->sgd_table
= &sc
->sgd_table
[num
* ch
->dma_segs
];
455 ch
->sgd_addr
= sc
->sgd_addr
+
456 (num
* ch
->dma_segs
* sizeof(struct atiixp_dma_op
));
457 atiixp_disable_dma(sc
, ch
);
464 atiixp_chan_setformat(kobj_t obj
, void *data
, uint32_t format
)
466 struct atiixp_chinfo
*ch
= data
;
467 struct atiixp_info
*sc
= ch
->parent
;
471 if (ch
->dir
== PCMDIR_REC
) {
472 value
= atiixp_rd(sc
, ATI_REG_CMD
);
473 value
&= ~ATI_REG_CMD_INTERLEAVE_IN
;
474 if ((format
& AFMT_32BIT
) == 0)
475 value
|= ATI_REG_CMD_INTERLEAVE_IN
;
476 atiixp_wr(sc
, ATI_REG_CMD
, value
);
478 value
= atiixp_rd(sc
, ATI_REG_OUT_DMA_SLOT
);
479 value
&= ~ATI_REG_OUT_DMA_SLOT_MASK
;
480 /* We do not have support for more than 2 channels, _yet_. */
481 value
|= ATI_REG_OUT_DMA_SLOT_BIT(3) |
482 ATI_REG_OUT_DMA_SLOT_BIT(4);
483 value
|= 0x04 << ATI_REG_OUT_DMA_THRESHOLD_SHIFT
;
484 atiixp_wr(sc
, ATI_REG_OUT_DMA_SLOT
, value
);
485 value
= atiixp_rd(sc
, ATI_REG_CMD
);
486 value
&= ~ATI_REG_CMD_INTERLEAVE_OUT
;
487 if ((format
& AFMT_32BIT
) == 0)
488 value
|= ATI_REG_CMD_INTERLEAVE_OUT
;
489 atiixp_wr(sc
, ATI_REG_CMD
, value
);
490 value
= atiixp_rd(sc
, ATI_REG_6CH_REORDER
);
491 value
&= ~ATI_REG_6CH_REORDER_EN
;
492 atiixp_wr(sc
, ATI_REG_6CH_REORDER
, value
);
501 atiixp_chan_setspeed(kobj_t obj
, void *data
, uint32_t spd
)
503 /* XXX We're supposed to do VRA/DRA processing right here */
504 return ATI_IXP_BASE_RATE
;
508 atiixp_chan_setblocksize(kobj_t obj
, void *data
, uint32_t blksz
)
510 struct atiixp_chinfo
*ch
= data
;
511 struct atiixp_info
*sc
= ch
->parent
;
513 if (blksz
> (sc
->bufsz
/ ch
->dma_segs
))
514 blksz
= sc
->bufsz
/ ch
->dma_segs
;
516 sndbuf_resize(ch
->buffer
, ch
->dma_segs
, blksz
);
518 return sndbuf_getblksz(ch
->buffer
);
522 atiixp_buildsgdt(struct atiixp_chinfo
*ch
)
524 uint32_t addr
, blksz
;
527 addr
= sndbuf_getbufaddr(ch
->buffer
);
528 blksz
= sndbuf_getblksz(ch
->buffer
);
530 for (i
= 0; i
< ch
->dma_segs
; i
++) {
531 ch
->sgd_table
[i
].addr
= htole32(addr
+ (i
* blksz
));
532 ch
->sgd_table
[i
].status
= htole16(0);
533 ch
->sgd_table
[i
].size
= htole16(blksz
>> 2);
534 ch
->sgd_table
[i
].next
= htole32((uint32_t)ch
->sgd_addr
+
535 (((i
+ 1) % ch
->dma_segs
) *
536 sizeof(struct atiixp_dma_op
)));
541 atiixp_chan_trigger(kobj_t obj
, void *data
, int go
)
543 struct atiixp_chinfo
*ch
= data
;
544 struct atiixp_info
*sc
= ch
->parent
;
551 atiixp_flush_dma(sc
, ch
);
552 atiixp_buildsgdt(ch
);
553 atiixp_wr(sc
, ch
->linkptr_bit
, 0);
554 atiixp_enable_dma(sc
, ch
);
555 atiixp_wr(sc
, ch
->linkptr_bit
,
556 (uint32_t)ch
->sgd_addr
| ATI_REG_LINKPTR_EN
);
560 atiixp_disable_dma(sc
, ch
);
561 atiixp_flush_dma(sc
, ch
);
569 /* Update bus busy status */
570 value
= atiixp_rd(sc
, ATI_REG_IER
);
571 if (atiixp_rd(sc
, ATI_REG_CMD
) & (
572 ATI_REG_CMD_SEND_EN
| ATI_REG_CMD_RECEIVE_EN
|
573 ATI_REG_CMD_SPDF_OUT_EN
))
574 value
|= ATI_REG_IER_SET_BUS_BUSY
;
576 value
&= ~ATI_REG_IER_SET_BUS_BUSY
;
577 atiixp_wr(sc
, ATI_REG_IER
, value
);
585 atiixp_chan_getptr(kobj_t obj
, void *data
)
587 struct atiixp_chinfo
*ch
= data
;
588 struct atiixp_info
*sc
= ch
->parent
;
589 uint32_t addr
, align
, retry
, sz
;
590 volatile uint32_t ptr
;
592 addr
= sndbuf_getbufaddr(ch
->buffer
);
593 align
= (ch
->fmt
& AFMT_32BIT
) ? 7 : 3;
595 sz
= sndbuf_getblksz(ch
->buffer
) * ch
->dma_segs
;
599 ptr
= atiixp_rd(sc
, ch
->dma_dt_cur_bit
);
603 if (ptr
< sz
&& !(ptr
& align
))
610 device_printf(sc
->dev
,
611 "%saligned hwptr: dir=PCMDIR_%s ptr=%u fmt=0x%08x retry=%d\n",
612 (ptr
& align
) ? "un" : "",
613 (ch
->dir
== PCMDIR_PLAY
) ? "PLAY" : "REC", ptr
,
614 ch
->fmt
, 100 - retry
);
618 return (retry
> 0) ? ptr
: 0;
621 static struct pcmchan_caps
*
622 atiixp_chan_getcaps(kobj_t obj
, void *data
)
624 struct atiixp_chinfo
*ch
= data
;
627 return &atiixp_caps_32bit
;
631 static kobj_method_t atiixp_chan_methods
[] = {
632 KOBJMETHOD(channel_init
, atiixp_chan_init
),
633 KOBJMETHOD(channel_setformat
, atiixp_chan_setformat
),
634 KOBJMETHOD(channel_setspeed
, atiixp_chan_setspeed
),
635 KOBJMETHOD(channel_setblocksize
, atiixp_chan_setblocksize
),
636 KOBJMETHOD(channel_trigger
, atiixp_chan_trigger
),
637 KOBJMETHOD(channel_getptr
, atiixp_chan_getptr
),
638 KOBJMETHOD(channel_getcaps
, atiixp_chan_getcaps
),
641 CHANNEL_DECLARE(atiixp_chan
);
644 * PCI driver interface
649 struct atiixp_info
*sc
= p
;
650 uint32_t status
, enable
, detected_codecs
;
653 status
= atiixp_rd(sc
, ATI_REG_ISR
);
660 if ((status
& ATI_REG_ISR_IN_STATUS
) && sc
->rch
.channel
) {
662 chn_intr(sc
->rch
.channel
);
665 if ((status
& ATI_REG_ISR_OUT_STATUS
) && sc
->pch
.channel
) {
667 chn_intr(sc
->pch
.channel
);
672 if (status
& ATI_REG_ISR_IN_XRUN
) {
673 device_printf(sc
->dev
,
674 "Receive IN XRUN interrupt\n");
676 if (status
& ATI_REG_ISR_OUT_XRUN
) {
677 device_printf(sc
->dev
,
678 "Receive OUT XRUN interrupt\n");
682 if (status
& CODEC_CHECK_BITS
) {
683 /* mark missing codecs as not ready */
684 detected_codecs
= status
& CODEC_CHECK_BITS
;
685 sc
->codec_not_ready_bits
|= detected_codecs
;
687 /* disable detected interupt sources */
688 enable
= atiixp_rd(sc
, ATI_REG_IER
);
689 enable
&= ~detected_codecs
;
690 atiixp_wr(sc
, ATI_REG_IER
, enable
);
694 atiixp_wr(sc
, ATI_REG_ISR
, status
);
699 atiixp_dma_cb(void *p
, bus_dma_segment_t
*bds
, int a
, int b
)
701 struct atiixp_info
*sc
= (struct atiixp_info
*)p
;
702 sc
->sgd_addr
= bds
->ds_addr
;
706 atiixp_chip_pre_init(struct atiixp_info
*sc
)
712 /* disable interrupts */
713 atiixp_disable_interrupts(sc
);
715 /* clear all DMA enables (preserving rest of settings) */
716 value
= atiixp_rd(sc
, ATI_REG_CMD
);
717 value
&= ~(ATI_REG_CMD_IN_DMA_EN
| ATI_REG_CMD_OUT_DMA_EN
|
718 ATI_REG_CMD_SPDF_OUT_EN
);
719 atiixp_wr(sc
, ATI_REG_CMD
, value
);
722 atiixp_reset_aclink(sc
);
724 sc
->codec_not_ready_bits
= 0;
726 /* enable all codecs to interrupt as well as the new frame interrupt */
727 atiixp_wr(sc
, ATI_REG_IER
, CODEC_CHECK_BITS
);
733 atiixp_chip_post_init(void *arg
)
735 struct atiixp_info
*sc
= (struct atiixp_info
*)arg
;
737 int i
, timeout
, found
;
738 char status
[SND_STATUSLEN
];
742 if (sc
->delayed_attach
.ich_func
) {
743 config_intrhook_disestablish(&sc
->delayed_attach
);
744 sc
->delayed_attach
.ich_func
= NULL
;
747 /* wait for the interrupts to happen */
750 snd_mtxsleep(sc
, sc
->lock
, 0, "ixpslp", 1);
751 if (sc
->codec_not_ready_bits
)
755 atiixp_disable_interrupts(sc
);
758 device_printf(sc
->dev
,
759 "WARNING: timeout during codec detection; "
760 "codecs might be present but haven't interrupted\n");
768 * ATI IXP can have upto 3 codecs, but single codec should be
771 if (!(sc
->codec_not_ready_bits
&
772 ATI_REG_ISR_CODEC0_NOT_READY
)) {
773 /* codec 0 present */
779 if (!(sc
->codec_not_ready_bits
&
780 ATI_REG_ISR_CODEC1_NOT_READY
)) {
781 /* codec 1 present */
785 if (!(sc
->codec_not_ready_bits
&
786 ATI_REG_ISR_CODEC2_NOT_READY
)) {
787 /* codec 2 present */
796 /* create/init mixer */
797 sc
->codec
= AC97_CREATE(sc
->dev
, sc
, atiixp_ac97
);
798 if (sc
->codec
== NULL
)
801 subdev
= (pci_get_subdevice(sc
->dev
) << 16) | pci_get_subvendor(sc
->dev
);
803 case 0x11831043: /* ASUS A6R */
804 case 0x2043161f: /* Maxselect x710s - http://maxselect.ru/ */
805 ac97_setflags(sc
->codec
, ac97_getflags(sc
->codec
) | AC97_F_EAPD_INV
);
811 mixer_init(sc
->dev
, ac97_getmixerclass(), sc
->codec
);
813 if (pcm_register(sc
->dev
, sc
, ATI_IXP_NPCHAN
, ATI_IXP_NRCHAN
))
816 for (i
= 0; i
< ATI_IXP_NPCHAN
; i
++)
817 pcm_addchan(sc
->dev
, PCMDIR_PLAY
, &atiixp_chan_class
, sc
);
818 for (i
= 0; i
< ATI_IXP_NRCHAN
; i
++)
819 pcm_addchan(sc
->dev
, PCMDIR_REC
, &atiixp_chan_class
, sc
);
821 ksnprintf(status
, SND_STATUSLEN
, "at memory 0x%lx irq %ld %s",
822 rman_get_start(sc
->reg
), rman_get_start(sc
->irq
),
823 PCM_KLDSTRING(snd_atiixp
));
825 pcm_setstatus(sc
->dev
, status
);
828 atiixp_enable_interrupts(sc
);
834 atiixp_release_resource(sc
);
838 atiixp_release_resource(struct atiixp_info
*sc
)
843 ac97_destroy(sc
->codec
);
847 bus_teardown_intr(sc
->dev
, sc
->irq
, sc
->ih
);
851 bus_release_resource(sc
->dev
, sc
->regtype
, sc
->regid
, sc
->reg
);
855 bus_release_resource(sc
->dev
, SYS_RES_IRQ
, sc
->irqid
, sc
->irq
);
858 if (sc
->parent_dmat
) {
859 bus_dma_tag_destroy(sc
->parent_dmat
);
860 sc
->parent_dmat
= NULL
;
863 bus_dmamap_unload(sc
->sgd_dmat
, sc
->sgd_dmamap
);
865 bus_dmamem_free(sc
->sgd_dmat
, sc
->sgd_table
, sc
->sgd_dmamap
);
866 sc
->sgd_table
= NULL
;
868 sc
->sgd_dmamap
= NULL
;
870 bus_dma_tag_destroy(sc
->sgd_dmat
);
874 snd_mtxfree(sc
->lock
);
880 atiixp_pci_probe(device_t dev
)
883 uint16_t devid
, vendor
;
885 vendor
= pci_get_vendor(dev
);
886 devid
= pci_get_device(dev
);
887 for (i
= 0; i
< sizeof(atiixp_hw
)/sizeof(atiixp_hw
[0]); i
++) {
888 if (vendor
== atiixp_hw
[i
].vendor
&&
889 devid
== atiixp_hw
[i
].devid
) {
890 device_set_desc(dev
, atiixp_hw
[i
].desc
);
891 return BUS_PROBE_DEFAULT
;
899 atiixp_pci_attach(device_t dev
)
901 struct atiixp_info
*sc
;
904 if ((sc
= kmalloc(sizeof(*sc
), M_DEVBUF
, M_NOWAIT
| M_ZERO
)) == NULL
) {
905 device_printf(dev
, "cannot allocate softc\n");
909 sc
->lock
= snd_mtxcreate(device_get_nameunit(dev
), "sound softc");
912 * Default DMA segments per playback / recording channel
914 sc
->dma_segs
= ATI_IXP_DMA_CHSEGS
;
916 pci_set_powerstate(dev
, PCI_POWERSTATE_D0
);
917 pci_enable_busmaster(dev
);
919 sc
->regid
= PCIR_BAR(0);
920 sc
->regtype
= SYS_RES_MEMORY
;
921 sc
->reg
= bus_alloc_resource_any(dev
, sc
->regtype
, &sc
->regid
,
925 device_printf(dev
, "unable to allocate register space\n");
929 sc
->st
= rman_get_bustag(sc
->reg
);
930 sc
->sh
= rman_get_bushandle(sc
->reg
);
932 sc
->bufsz
= pcm_getbuffersize(dev
, 4096, ATI_IXP_DEFAULT_BUFSZ
, 65536);
935 sc
->irq
= bus_alloc_resource_any(dev
, SYS_RES_IRQ
, &sc
->irqid
,
936 RF_ACTIVE
| RF_SHAREABLE
);
938 snd_setup_intr(dev
, sc
->irq
, INTR_MPSAFE
,
939 atiixp_intr
, sc
, &sc
->ih
)) {
940 device_printf(dev
, "unable to map interrupt\n");
945 * Let the user choose the best DMA segments.
947 if (resource_int_value(device_get_name(dev
),
948 device_get_unit(dev
), "dma_segs",
950 if (i
< ATI_IXP_DMA_CHSEGS_MIN
)
951 i
= ATI_IXP_DMA_CHSEGS_MIN
;
952 if (i
> ATI_IXP_DMA_CHSEGS_MAX
)
953 i
= ATI_IXP_DMA_CHSEGS_MAX
;
958 * round the value to the nearest ^2
961 while (sc
->dma_segs
>> i
)
963 sc
->dma_segs
= 1 << (i
- 1);
964 if (sc
->dma_segs
< ATI_IXP_DMA_CHSEGS_MIN
)
965 sc
->dma_segs
= ATI_IXP_DMA_CHSEGS_MIN
;
966 else if (sc
->dma_segs
> ATI_IXP_DMA_CHSEGS_MAX
)
967 sc
->dma_segs
= ATI_IXP_DMA_CHSEGS_MAX
;
970 * DMA tag for scatter-gather buffers and link pointers
972 if (bus_dma_tag_create(/*parent*/NULL
, /*alignment*/2, /*boundary*/0,
973 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT
,
974 /*highaddr*/BUS_SPACE_MAXADDR
,
975 /*filter*/NULL
, /*filterarg*/NULL
,
976 /*maxsize*/sc
->bufsz
, /*nsegments*/1, /*maxsegz*/0x3ffff,
978 &sc
->parent_dmat
) != 0) {
979 device_printf(dev
, "unable to create dma tag\n");
983 if (bus_dma_tag_create(/*parent*/NULL
, /*alignment*/2, /*boundary*/0,
984 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT
,
985 /*highaddr*/BUS_SPACE_MAXADDR
,
986 /*filter*/NULL
, /*filterarg*/NULL
,
987 /*maxsize*/sc
->dma_segs
* ATI_IXP_NCHANS
*
988 sizeof(struct atiixp_dma_op
),
989 /*nsegments*/1, /*maxsegz*/0x3ffff,
991 &sc
->sgd_dmat
) != 0) {
992 device_printf(dev
, "unable to create dma tag\n");
996 if (bus_dmamem_alloc(sc
->sgd_dmat
, (void **)&sc
->sgd_table
,
997 BUS_DMA_NOWAIT
, &sc
->sgd_dmamap
) == -1)
1000 if (bus_dmamap_load(sc
->sgd_dmat
, sc
->sgd_dmamap
, sc
->sgd_table
,
1001 sc
->dma_segs
* ATI_IXP_NCHANS
*
1002 sizeof(struct atiixp_dma_op
),
1003 atiixp_dma_cb
, sc
, 0))
1007 atiixp_chip_pre_init(sc
);
1009 sc
->delayed_attach
.ich_func
= atiixp_chip_post_init
;
1010 sc
->delayed_attach
.ich_arg
= sc
;
1012 config_intrhook_establish(&sc
->delayed_attach
) != 0) {
1013 sc
->delayed_attach
.ich_func
= NULL
;
1014 atiixp_chip_post_init(sc
);
1020 atiixp_release_resource(sc
);
1025 atiixp_pci_detach(device_t dev
)
1028 struct atiixp_info
*sc
;
1030 sc
= pcm_getdevinfo(dev
);
1032 if (sc
->codec
!= NULL
) {
1033 r
= pcm_unregister(dev
);
1038 atiixp_disable_interrupts(sc
);
1039 atiixp_release_resource(sc
);
1040 kfree(sc
, M_DEVBUF
);
1046 atiixp_pci_suspend(device_t dev
)
1048 struct atiixp_info
*sc
= pcm_getdevinfo(dev
);
1051 /* quickly disable interrupts and save channels active state */
1053 atiixp_disable_interrupts(sc
);
1054 value
= atiixp_rd(sc
, ATI_REG_CMD
);
1055 sc
->pch
.active
= (value
& ATI_REG_CMD_SEND_EN
) ? 1 : 0;
1056 sc
->rch
.active
= (value
& ATI_REG_CMD_RECEIVE_EN
) ? 1 : 0;
1059 /* stop everything */
1060 if (sc
->pch
.channel
&& sc
->pch
.active
)
1061 atiixp_chan_trigger(NULL
, &sc
->pch
, PCMTRIG_STOP
);
1062 if (sc
->rch
.channel
&& sc
->rch
.active
)
1063 atiixp_chan_trigger(NULL
, &sc
->rch
, PCMTRIG_STOP
);
1065 /* power down aclink and pci bus */
1067 value
= atiixp_rd(sc
, ATI_REG_CMD
);
1068 value
|= ATI_REG_CMD_POWERDOWN
| ATI_REG_CMD_AC_RESET
;
1069 atiixp_wr(sc
, ATI_REG_CMD
, ATI_REG_CMD_POWERDOWN
);
1070 pci_set_powerstate(dev
, PCI_POWERSTATE_D3
);
1077 atiixp_pci_resume(device_t dev
)
1079 struct atiixp_info
*sc
= pcm_getdevinfo(dev
);
1082 /* power up pci bus */
1083 pci_set_powerstate(dev
, PCI_POWERSTATE_D0
);
1084 pci_enable_io(dev
, SYS_RES_MEMORY
);
1085 pci_enable_busmaster(dev
);
1086 /* reset / power up aclink */
1087 atiixp_reset_aclink(sc
);
1090 if (mixer_reinit(dev
) == -1) {
1091 device_printf(dev
, "unable to reinitialize the mixer\n");
1096 * Resume channel activities. Reset channel format regardless
1097 * of its previous state.
1099 if (sc
->pch
.channel
) {
1101 atiixp_chan_setformat(NULL
, &sc
->pch
, sc
->pch
.fmt
);
1103 atiixp_chan_trigger(NULL
, &sc
->pch
, PCMTRIG_START
);
1105 if (sc
->rch
.channel
) {
1107 atiixp_chan_setformat(NULL
, &sc
->rch
, sc
->rch
.fmt
);
1109 atiixp_chan_trigger(NULL
, &sc
->rch
, PCMTRIG_START
);
1112 /* enable interrupts */
1114 atiixp_enable_interrupts(sc
);
1120 static device_method_t atiixp_methods
[] = {
1121 DEVMETHOD(device_probe
, atiixp_pci_probe
),
1122 DEVMETHOD(device_attach
, atiixp_pci_attach
),
1123 DEVMETHOD(device_detach
, atiixp_pci_detach
),
1124 DEVMETHOD(device_suspend
, atiixp_pci_suspend
),
1125 DEVMETHOD(device_resume
, atiixp_pci_resume
),
1129 static driver_t atiixp_driver
= {
1135 DRIVER_MODULE(snd_atiixp
, pci
, atiixp_driver
, pcm_devclass
, 0, 0);
1136 MODULE_DEPEND(snd_atiixp
, sound
, SOUND_MINVER
, SOUND_PREFVER
, SOUND_MAXVER
);
1137 MODULE_VERSION(snd_atiixp
, 1);