2 * Copyright (c) 2002 Orion Hodson <orion@freebsd.org>
3 * Portions of this code derived from via82c686.c:
4 * Copyright (c) 2000 David Jones <dej@ox.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $FreeBSD: src/sys/dev/sound/pci/via8233.c,v 1.2.2.2 2003/02/06 17:35:56 orion Exp $
29 * $DragonFly: src/sys/dev/sound/pci/via8233.c,v 1.5 2006/09/05 00:55:43 dillon Exp $
34 * Grzybowski Rafal, Russell Davies, Mark Handley, Daniel O'Connor for
35 * comments, machine time, testing patches, and patience. VIA for
36 * providing specs. ALSA for helpful comments and some register poke
40 #include <dev/sound/pcm/sound.h>
41 #include <dev/sound/pcm/ac97.h>
43 #include <bus/pci/pcireg.h>
44 #include <bus/pci/pcivar.h>
45 #include <sys/sysctl.h>
47 #include <dev/sound/pci/via8233.h>
49 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pci/via8233.c,v 1.5 2006/09/05 00:55:43 dillon Exp $");
51 #define VIA8233_PCI_ID 0x30591106
53 #define SEGS_PER_CHAN 2 /* Segments per channel */
54 #define NCHANS 2 /* Lines-in,out (mic later) */
55 #define NSEGS NCHANS * SEGS_PER_CHAN /* Segments in SGD table */
57 #define VIA_DEFAULT_BUFSZ 0x1000
62 /* we rely on this struct being packed to 64 bits */
66 #define VIA_DMAOP_EOL 0x80000000
67 #define VIA_DMAOP_FLAG 0x40000000
68 #define VIA_DMAOP_STOP 0x20000000
69 #define VIA_DMAOP_COUNT(x) ((x)&0x00FFFFFF)
75 struct via_info *parent;
76 struct pcm_channel *channel;
77 struct snd_dbuf *buffer;
78 struct via_dma_op *sgd_table;
80 int rbase; /* base register for channel */
85 bus_space_handle_t sh;
86 bus_dma_tag_t parent_dmat;
87 bus_dma_tag_t sgd_dmat;
88 bus_dmamap_t sgd_dmamap;
90 struct resource *reg, *irq;
93 struct ac97_info *codec;
97 struct via_chinfo pch, rch;
98 struct via_dma_op *sgd_table;
102 static u_int32_t via_fmt[] = {
104 AFMT_STEREO | AFMT_U8,
106 AFMT_STEREO | AFMT_S16_LE,
110 static struct pcmchan_caps via_vracaps = { 4000, 48000, via_fmt, 0 };
111 static struct pcmchan_caps via_caps = { 48000, 48000, via_fmt, 0 };
114 via_rd(struct via_info *via, int regno, int size)
118 return bus_space_read_1(via->st, via->sh, regno);
120 return bus_space_read_2(via->st, via->sh, regno);
122 return bus_space_read_4(via->st, via->sh, regno);
129 via_wr(struct via_info *via, int regno, u_int32_t data, int size)
134 bus_space_write_1(via->st, via->sh, regno, data);
137 bus_space_write_2(via->st, via->sh, regno, data);
140 bus_space_write_4(via->st, via->sh, regno, data);
145 /* -------------------------------------------------------------------- */
146 /* Codec interface */
149 via_waitready_codec(struct via_info *via)
153 /* poll until codec not busy */
154 for (i = 0; i < 1000; i++) {
155 if ((via_rd(via, VIA_AC97_CONTROL, 4) & VIA_AC97_BUSY) == 0)
159 printf("via: codec busy\n");
164 via_waitvalid_codec(struct via_info *via)
168 /* poll until codec valid */
169 for (i = 0; i < 1000; i++) {
170 if (via_rd(via, VIA_AC97_CONTROL, 4) & VIA_AC97_CODEC00_VALID)
174 printf("via: codec invalid\n");
179 via_write_codec(kobj_t obj, void *addr, int reg, u_int32_t val)
181 struct via_info *via = addr;
183 if (via_waitready_codec(via)) return -1;
185 via_wr(via, VIA_AC97_CONTROL,
186 VIA_AC97_CODEC00_VALID | VIA_AC97_INDEX(reg) |
187 VIA_AC97_DATA(val), 4);
193 via_read_codec(kobj_t obj, void *addr, int reg)
195 struct via_info *via = addr;
197 if (via_waitready_codec(via))
200 via_wr(via, VIA_AC97_CONTROL, VIA_AC97_CODEC00_VALID |
201 VIA_AC97_READ | VIA_AC97_INDEX(reg), 4);
203 if (via_waitready_codec(via))
206 if (via_waitvalid_codec(via))
209 return via_rd(via, VIA_AC97_CONTROL, 2);
212 static kobj_method_t via_ac97_methods[] = {
213 KOBJMETHOD(ac97_read, via_read_codec),
214 KOBJMETHOD(ac97_write, via_write_codec),
217 AC97_DECLARE(via_ac97);
219 /* -------------------------------------------------------------------- */
222 via_buildsgdt(struct via_chinfo *ch)
224 u_int32_t phys_addr, flag;
228 * Build the scatter/gather DMA (SGD) table.
229 * There are four slots in the table: two for play, two for record.
230 * This creates two half-buffers, one of which is playing; the other
233 seg_size = sndbuf_getsize(ch->buffer) / SEGS_PER_CHAN;
235 phys_addr = vtophys(sndbuf_getbuf(ch->buffer));
237 for (i = 0; i < SEGS_PER_CHAN; i++) {
238 flag = (i == SEGS_PER_CHAN - 1) ? VIA_DMAOP_EOL : VIA_DMAOP_FLAG;
239 ch->sgd_table[i].ptr = phys_addr + (i * seg_size);
240 ch->sgd_table[i].flags = flag | seg_size;
247 via8233pchan_setformat(kobj_t obj, void *data, u_int32_t format)
249 struct via_chinfo *ch = data;
250 struct via_info *via = ch->parent;
252 u_int32_t s = 0xff000000;
253 u_int8_t v = (format & AFMT_S16_LE) ? MC_SGD_16BIT : MC_SGD_8BIT;
255 if (format & AFMT_STEREO) {
256 v |= MC_SGD_CHANNELS(2);
257 s |= SLOT3(1) | SLOT4(2);
259 v |= MC_SGD_CHANNELS(1);
260 s |= SLOT3(1) | SLOT4(1);
263 via_wr(via, VIA_MC_SLOT_SELECT, s, 4);
264 via_wr(via, VIA_MC_SGD_FORMAT, v, 1);
270 via8233rchan_setformat(kobj_t obj, void *data, u_int32_t format)
272 struct via_chinfo *ch = data;
273 struct via_info *via = ch->parent;
275 u_int32_t f = WR_FORMAT_STOP_INDEX;
276 if (format & AFMT_STEREO)
277 f |= WR_FORMAT_STEREO;
278 if (format & AFMT_S16_LE)
279 f |= WR_FORMAT_16BIT;
280 via_wr(via, VIA_WR0_FORMAT, f, 4);
286 via8233pchan_setspeed(kobj_t obj, void *data, u_int32_t speed)
288 struct via_chinfo *ch = data;
289 struct via_info *via = ch->parent;
291 if (via->codec_caps & AC97_EXTCAP_VRA)
292 return ac97_setrate(via->codec, AC97_REGEXT_FDACRATE, speed);
298 via8233rchan_setspeed(kobj_t obj, void *data, u_int32_t speed)
300 struct via_chinfo *ch = data;
301 struct via_info *via = ch->parent;
303 u_int32_t spd = 48000;
304 if (via->codec_caps & AC97_EXTCAP_VRA) {
305 spd = ac97_setrate(via->codec, AC97_REGEXT_LADCRATE, speed);
311 via8233chan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
313 struct via_chinfo *ch = data;
315 sndbuf_resize(ch->buffer, SEGS_PER_CHAN, blocksize);
316 ch->blksz = sndbuf_getblksz(ch->buffer);
321 via8233chan_getptr(kobj_t obj, void *data)
323 struct via_chinfo *ch = data;
324 struct via_info *via = ch->parent;
326 u_int32_t v = via_rd(via, ch->rbase + VIA_RP_CURRENT_COUNT, 4);
327 u_int32_t index = v >> 24; /* Last completed buffer */
328 u_int32_t count = v & 0x00ffffff; /* Bytes remaining */
329 int ptr = (index + 1) * ch->blksz - count;
330 ptr %= SEGS_PER_CHAN * ch->blksz; /* Wrap to available space */
335 static struct pcmchan_caps *
336 via8233chan_getcaps(kobj_t obj, void *data)
338 struct via_chinfo *ch = data;
339 struct via_info *via = ch->parent;
340 if (via->codec_caps & AC97_EXTCAP_VRA)
346 via8233chan_reset(struct via_info *via, struct via_chinfo *ch)
348 via_wr(via, ch->rbase + VIA_RP_CONTROL, SGD_CONTROL_STOP, 1);
349 via_wr(via, ch->rbase + VIA_RP_CONTROL, 0x00, 1);
350 via_wr(via, ch->rbase + VIA_RP_STATUS,
351 SGD_STATUS_EOL | SGD_STATUS_FLAG, 1);
355 via8233chan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b,
356 struct pcm_channel *c, int dir)
358 struct via_info *via = devinfo;
359 struct via_chinfo *ch = (dir == PCMDIR_PLAY)? &via->pch : &via->rch;
365 ch->sgd_table = &via->sgd_table[(dir == PCMDIR_PLAY)? 0 : SEGS_PER_CHAN];
367 if (ch->dir == PCMDIR_PLAY) {
368 ch->rbase = VIA_MC_SGD_STATUS;
370 ch->rbase = VIA_WR0_SGD_STATUS;
371 via_wr(via, VIA_WR0_SGD_FORMAT, WR_FIFO_ENABLE, 1);
374 if (sndbuf_alloc(ch->buffer, via->parent_dmat, via->bufsz) == -1)
377 via8233chan_reset(via, ch);
383 via8233chan_trigger(kobj_t obj, void* data, int go)
385 struct via_chinfo *ch = data;
386 struct via_info *via = ch->parent;
387 struct via_dma_op *ado = ch->sgd_table;
392 via_wr(via, ch->rbase + VIA_RP_TABLE_PTR, vtophys(ado), 4);
393 via_wr(via, ch->rbase + VIA_RP_CONTROL,
394 SGD_CONTROL_START | SGD_CONTROL_AUTOSTART |
395 SGD_CONTROL_I_EOL | SGD_CONTROL_I_FLAG, 1);
399 via_wr(via, ch->rbase + VIA_RP_CONTROL, SGD_CONTROL_STOP, 1);
400 via8233chan_reset(via, ch);
406 static kobj_method_t via8233pchan_methods[] = {
407 KOBJMETHOD(channel_init, via8233chan_init),
408 KOBJMETHOD(channel_setformat, via8233pchan_setformat),
409 KOBJMETHOD(channel_setspeed, via8233pchan_setspeed),
410 KOBJMETHOD(channel_setblocksize, via8233chan_setblocksize),
411 KOBJMETHOD(channel_trigger, via8233chan_trigger),
412 KOBJMETHOD(channel_getptr, via8233chan_getptr),
413 KOBJMETHOD(channel_getcaps, via8233chan_getcaps),
416 CHANNEL_DECLARE(via8233pchan);
418 static kobj_method_t via8233rchan_methods[] = {
419 KOBJMETHOD(channel_init, via8233chan_init),
420 KOBJMETHOD(channel_setformat, via8233rchan_setformat),
421 KOBJMETHOD(channel_setspeed, via8233rchan_setspeed),
422 KOBJMETHOD(channel_setblocksize, via8233chan_setblocksize),
423 KOBJMETHOD(channel_trigger, via8233chan_trigger),
424 KOBJMETHOD(channel_getptr, via8233chan_getptr),
425 KOBJMETHOD(channel_getcaps, via8233chan_getcaps),
428 CHANNEL_DECLARE(via8233rchan);
430 /* -------------------------------------------------------------------- */
435 struct via_info *via = p;
436 int r = via_rd(via, VIA_MC_SGD_STATUS, 1);
437 if (r & SGD_STATUS_INTR) {
438 via_wr(via, VIA_MC_SGD_STATUS, SGD_STATUS_INTR, 1);
439 chn_intr(via->pch.channel);
442 r = via_rd(via, VIA_WR0_SGD_STATUS, 1);
443 if (r & SGD_STATUS_INTR) {
444 via_wr(via, VIA_WR0_SGD_STATUS, SGD_STATUS_INTR, 1);
445 chn_intr(via->rch.channel);
450 * Probe and attach the card
453 via_probe(device_t dev)
455 switch(pci_get_devid(dev)) {
457 switch(pci_get_revid(dev)) {
459 device_set_desc(dev, "VIA VT8233 (pre)");
462 device_set_desc(dev, "VIA VT8233C");
465 device_set_desc(dev, "VIA VT8233");
468 device_set_desc(dev, "VIA VT8233A");
471 device_set_desc(dev, "VIA VT8235");
474 device_set_desc(dev, "VIA VT8233X"); /* Unknown */
482 dma_cb(void *p, bus_dma_segment_t *bds, int a, int b)
487 via_chip_init(device_t dev)
491 pci_write_config(dev, VIA_PCI_ACLINK_CTRL, 0, 1);
494 /* assert ACLink reset */
495 pci_write_config(dev, VIA_PCI_ACLINK_CTRL, VIA_PCI_ACLINK_NRST, 1);
498 /* deassert ACLink reset, force SYNC (warm AC'97 reset) */
499 pci_write_config(dev, VIA_PCI_ACLINK_CTRL,
500 VIA_PCI_ACLINK_NRST | VIA_PCI_ACLINK_SYNC, 1);
502 /* ACLink on, deassert ACLink reset, VSR, SGD data out */
503 pci_write_config(dev, VIA_PCI_ACLINK_CTRL,
504 VIA_PCI_ACLINK_EN | VIA_PCI_ACLINK_NRST
505 | VIA_PCI_ACLINK_VRATE | VIA_PCI_ACLINK_SGD, 1);
507 for (i = 0; i < 100; i++) {
508 s = pci_read_config(dev, VIA_PCI_ACLINK_STAT, 1);
509 if (s & VIA_PCI_ACLINK_C00_READY) {
510 s = pci_read_config(dev, VIA_PCI_ACLINK_CTRL, 1);
515 device_printf(dev, "primary codec not ready (s = 0x%02x)\n", s);
520 via_attach(device_t dev)
522 struct via_info *via = 0;
523 char status[SND_STATUSLEN];
525 if ((via = kmalloc(sizeof *via, M_DEVBUF, M_NOWAIT | M_ZERO)) == NULL) {
526 device_printf(dev, "cannot allocate softc\n");
530 pci_enable_io(dev, SYS_RES_IOPORT);
531 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
532 pci_enable_busmaster(dev);
534 via->regid = PCIR_MAPS;
535 via->reg = bus_alloc_resource(dev, SYS_RES_IOPORT, &via->regid, 0, ~0,
538 device_printf(dev, "cannot allocate bus resource.");
541 via->st = rman_get_bustag(via->reg);
542 via->sh = rman_get_bushandle(via->reg);
544 via->bufsz = pcm_getbuffersize(dev, 4096, VIA_DEFAULT_BUFSZ, 65536);
547 via->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &via->irqid, 0, ~0, 1,
548 RF_ACTIVE | RF_SHAREABLE);
550 snd_setup_intr(dev, via->irq, 0, via_intr, via, &via->ih, NULL)) {
551 device_printf(dev, "unable to map interrupt\n");
555 /* DMA tag for buffers */
556 if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0,
557 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
558 /*highaddr*/BUS_SPACE_MAXADDR,
559 /*filter*/NULL, /*filterarg*/NULL,
560 /*maxsize*/via->bufsz, /*nsegments*/1, /*maxsegz*/0x3ffff,
561 /*flags*/0, &via->parent_dmat) != 0) {
562 device_printf(dev, "unable to create dma tag\n");
567 * DMA tag for SGD table. The 686 uses scatter/gather DMA and
568 * requires a list in memory of work to do. We need only 16 bytes
569 * for this list, and it is wasteful to allocate 16K.
571 if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0,
572 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
573 /*highaddr*/BUS_SPACE_MAXADDR,
574 /*filter*/NULL, /*filterarg*/NULL,
575 /*maxsize*/NSEGS * sizeof(struct via_dma_op),
576 /*nsegments*/1, /*maxsegz*/0x3ffff,
577 /*flags*/0, &via->sgd_dmat) != 0) {
578 device_printf(dev, "unable to create dma tag\n");
582 if (bus_dmamem_alloc(via->sgd_dmat, (void **)&via->sgd_table,
583 BUS_DMA_NOWAIT, &via->sgd_dmamap) == -1)
585 if (bus_dmamap_load(via->sgd_dmat, via->sgd_dmamap, via->sgd_table,
586 NSEGS * sizeof(struct via_dma_op), dma_cb, 0, 0))
589 if (via_chip_init(dev))
592 via->codec = AC97_CREATE(dev, via, via_ac97);
596 mixer_init(dev, ac97_getmixerclass(), via->codec);
598 via->codec_caps = ac97_getextcaps(via->codec);
600 /* Try to set VRA without generating an error, VRM not reqrd yet */
601 if (via->codec_caps &
602 (AC97_EXTCAP_VRA | AC97_EXTCAP_VRM | AC97_EXTCAP_DRA)) {
603 u_int16_t ext = ac97_getextmode(via->codec);
604 ext |= (via->codec_caps &
605 (AC97_EXTCAP_VRA | AC97_EXTCAP_VRM));
606 ext &= ~AC97_EXTCAP_DRA;
607 ac97_setextmode(via->codec, ext);
610 snprintf(status, SND_STATUSLEN, "at io 0x%lx irq %ld",
611 rman_get_start(via->reg), rman_get_start(via->irq));
614 if (pcm_register(dev, via, 1, 1)) goto bad;
616 pcm_addchan(dev, PCMDIR_PLAY, &via8233pchan_class, via);
617 pcm_addchan(dev, PCMDIR_REC, &via8233rchan_class, via);
619 pcm_setstatus(dev, status);
623 if (via->codec) ac97_destroy(via->codec);
624 if (via->reg) bus_release_resource(dev, SYS_RES_IOPORT, via->regid, via->reg);
625 if (via->ih) bus_teardown_intr(dev, via->irq, via->ih);
626 if (via->irq) bus_release_resource(dev, SYS_RES_IRQ, via->irqid, via->irq);
627 if (via->parent_dmat) bus_dma_tag_destroy(via->parent_dmat);
628 if (via->sgd_dmamap) bus_dmamap_unload(via->sgd_dmat, via->sgd_dmamap);
629 if (via->sgd_dmat) bus_dma_tag_destroy(via->sgd_dmat);
630 if (via) kfree(via, M_DEVBUF);
635 via_detach(device_t dev)
638 struct via_info *via = 0;
640 r = pcm_unregister(dev);
643 via = pcm_getdevinfo(dev);
644 bus_release_resource(dev, SYS_RES_IOPORT, via->regid, via->reg);
645 bus_teardown_intr(dev, via->irq, via->ih);
646 bus_release_resource(dev, SYS_RES_IRQ, via->irqid, via->irq);
647 bus_dma_tag_destroy(via->parent_dmat);
648 bus_dmamap_unload(via->sgd_dmat, via->sgd_dmamap);
649 bus_dma_tag_destroy(via->sgd_dmat);
650 kfree(via, M_DEVBUF);
655 static device_method_t via_methods[] = {
656 DEVMETHOD(device_probe, via_probe),
657 DEVMETHOD(device_attach, via_attach),
658 DEVMETHOD(device_detach, via_detach),
662 static driver_t via_driver = {
668 DRIVER_MODULE(snd_via8233, pci, via_driver, pcm_devclass, 0, 0);
669 MODULE_DEPEND(snd_via8233, snd_pcm, PCM_MINVER, PCM_PREFVER, PCM_MAXVER);
670 MODULE_VERSION(snd_via8233, 1);