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
31 * Grzybowski Rafal, Russell Davies, Mark Handley, Daniel O'Connor for
32 * comments, machine time, testing patches, and patience. VIA for
33 * providing specs. ALSA for helpful comments and some register poke
37 #include <dev/sound/pcm/sound.h>
38 #include <dev/sound/pcm/ac97.h>
40 #include <pci/pcireg.h>
41 #include <pci/pcivar.h>
42 #include <sys/sysctl.h>
44 #include <dev/sound/pci/via8233.h>
46 SND_DECLARE_FILE("$FreeBSD: src/sys/dev/sound/pci/via8233.c,v 1.2.2.2 2003/02/06 17:35:56 orion Exp $");
48 #define VIA8233_PCI_ID 0x30591106
50 #define SEGS_PER_CHAN 2 /* Segments per channel */
51 #define NCHANS 2 /* Lines-in,out (mic later) */
52 #define NSEGS NCHANS * SEGS_PER_CHAN /* Segments in SGD table */
54 #define VIA_DEFAULT_BUFSZ 0x1000
59 /* we rely on this struct being packed to 64 bits */
63 #define VIA_DMAOP_EOL 0x80000000
64 #define VIA_DMAOP_FLAG 0x40000000
65 #define VIA_DMAOP_STOP 0x20000000
66 #define VIA_DMAOP_COUNT(x) ((x)&0x00FFFFFF)
72 struct via_info *parent;
73 struct pcm_channel *channel;
74 struct snd_dbuf *buffer;
75 struct via_dma_op *sgd_table;
77 int rbase; /* base register for channel */
82 bus_space_handle_t sh;
83 bus_dma_tag_t parent_dmat;
84 bus_dma_tag_t sgd_dmat;
85 bus_dmamap_t sgd_dmamap;
87 struct resource *reg, *irq;
90 struct ac97_info *codec;
94 struct via_chinfo pch, rch;
95 struct via_dma_op *sgd_table;
99 static u_int32_t via_fmt[] = {
101 AFMT_STEREO | AFMT_U8,
103 AFMT_STEREO | AFMT_S16_LE,
107 static struct pcmchan_caps via_vracaps = { 4000, 48000, via_fmt, 0 };
108 static struct pcmchan_caps via_caps = { 48000, 48000, via_fmt, 0 };
111 via_rd(struct via_info *via, int regno, int size)
115 return bus_space_read_1(via->st, via->sh, regno);
117 return bus_space_read_2(via->st, via->sh, regno);
119 return bus_space_read_4(via->st, via->sh, regno);
126 via_wr(struct via_info *via, int regno, u_int32_t data, int size)
131 bus_space_write_1(via->st, via->sh, regno, data);
134 bus_space_write_2(via->st, via->sh, regno, data);
137 bus_space_write_4(via->st, via->sh, regno, data);
142 /* -------------------------------------------------------------------- */
143 /* Codec interface */
146 via_waitready_codec(struct via_info *via)
150 /* poll until codec not busy */
151 for (i = 0; i < 1000; i++) {
152 if ((via_rd(via, VIA_AC97_CONTROL, 4) & VIA_AC97_BUSY) == 0)
156 printf("via: codec busy\n");
161 via_waitvalid_codec(struct via_info *via)
165 /* poll until codec valid */
166 for (i = 0; i < 1000; i++) {
167 if (via_rd(via, VIA_AC97_CONTROL, 4) & VIA_AC97_CODEC00_VALID)
171 printf("via: codec invalid\n");
176 via_write_codec(kobj_t obj, void *addr, int reg, u_int32_t val)
178 struct via_info *via = addr;
180 if (via_waitready_codec(via)) return -1;
182 via_wr(via, VIA_AC97_CONTROL,
183 VIA_AC97_CODEC00_VALID | VIA_AC97_INDEX(reg) |
184 VIA_AC97_DATA(val), 4);
190 via_read_codec(kobj_t obj, void *addr, int reg)
192 struct via_info *via = addr;
194 if (via_waitready_codec(via))
197 via_wr(via, VIA_AC97_CONTROL, VIA_AC97_CODEC00_VALID |
198 VIA_AC97_READ | VIA_AC97_INDEX(reg), 4);
200 if (via_waitready_codec(via))
203 if (via_waitvalid_codec(via))
206 return via_rd(via, VIA_AC97_CONTROL, 2);
209 static kobj_method_t via_ac97_methods[] = {
210 KOBJMETHOD(ac97_read, via_read_codec),
211 KOBJMETHOD(ac97_write, via_write_codec),
214 AC97_DECLARE(via_ac97);
216 /* -------------------------------------------------------------------- */
219 via_buildsgdt(struct via_chinfo *ch)
221 u_int32_t phys_addr, flag;
225 * Build the scatter/gather DMA (SGD) table.
226 * There are four slots in the table: two for play, two for record.
227 * This creates two half-buffers, one of which is playing; the other
230 seg_size = sndbuf_getsize(ch->buffer) / SEGS_PER_CHAN;
232 phys_addr = vtophys(sndbuf_getbuf(ch->buffer));
234 for (i = 0; i < SEGS_PER_CHAN; i++) {
235 flag = (i == SEGS_PER_CHAN - 1) ? VIA_DMAOP_EOL : VIA_DMAOP_FLAG;
236 ch->sgd_table[i].ptr = phys_addr + (i * seg_size);
237 ch->sgd_table[i].flags = flag | seg_size;
244 via8233pchan_setformat(kobj_t obj, void *data, u_int32_t format)
246 struct via_chinfo *ch = data;
247 struct via_info *via = ch->parent;
249 u_int32_t s = 0xff000000;
250 u_int8_t v = (format & AFMT_S16_LE) ? MC_SGD_16BIT : MC_SGD_8BIT;
252 if (format & AFMT_STEREO) {
253 v |= MC_SGD_CHANNELS(2);
254 s |= SLOT3(1) | SLOT4(2);
256 v |= MC_SGD_CHANNELS(1);
257 s |= SLOT3(1) | SLOT4(1);
260 via_wr(via, VIA_MC_SLOT_SELECT, s, 4);
261 via_wr(via, VIA_MC_SGD_FORMAT, v, 1);
267 via8233rchan_setformat(kobj_t obj, void *data, u_int32_t format)
269 struct via_chinfo *ch = data;
270 struct via_info *via = ch->parent;
272 u_int32_t f = WR_FORMAT_STOP_INDEX;
273 if (format & AFMT_STEREO)
274 f |= WR_FORMAT_STEREO;
275 if (format & AFMT_S16_LE)
276 f |= WR_FORMAT_16BIT;
277 via_wr(via, VIA_WR0_FORMAT, f, 4);
283 via8233pchan_setspeed(kobj_t obj, void *data, u_int32_t speed)
285 struct via_chinfo *ch = data;
286 struct via_info *via = ch->parent;
288 if (via->codec_caps & AC97_EXTCAP_VRA)
289 return ac97_setrate(via->codec, AC97_REGEXT_FDACRATE, speed);
295 via8233rchan_setspeed(kobj_t obj, void *data, u_int32_t speed)
297 struct via_chinfo *ch = data;
298 struct via_info *via = ch->parent;
300 u_int32_t spd = 48000;
301 if (via->codec_caps & AC97_EXTCAP_VRA) {
302 spd = ac97_setrate(via->codec, AC97_REGEXT_LADCRATE, speed);
308 via8233chan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
310 struct via_chinfo *ch = data;
312 sndbuf_resize(ch->buffer, SEGS_PER_CHAN, blocksize);
313 ch->blksz = sndbuf_getblksz(ch->buffer);
318 via8233chan_getptr(kobj_t obj, void *data)
320 struct via_chinfo *ch = data;
321 struct via_info *via = ch->parent;
323 u_int32_t v = via_rd(via, ch->rbase + VIA_RP_CURRENT_COUNT, 4);
324 u_int32_t index = v >> 24; /* Last completed buffer */
325 u_int32_t count = v & 0x00ffffff; /* Bytes remaining */
326 int ptr = (index + 1) * ch->blksz - count;
327 ptr %= SEGS_PER_CHAN * ch->blksz; /* Wrap to available space */
332 static struct pcmchan_caps *
333 via8233chan_getcaps(kobj_t obj, void *data)
335 struct via_chinfo *ch = data;
336 struct via_info *via = ch->parent;
337 if (via->codec_caps & AC97_EXTCAP_VRA)
343 via8233chan_reset(struct via_info *via, struct via_chinfo *ch)
345 via_wr(via, ch->rbase + VIA_RP_CONTROL, SGD_CONTROL_STOP, 1);
346 via_wr(via, ch->rbase + VIA_RP_CONTROL, 0x00, 1);
347 via_wr(via, ch->rbase + VIA_RP_STATUS,
348 SGD_STATUS_EOL | SGD_STATUS_FLAG, 1);
352 via8233chan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b,
353 struct pcm_channel *c, int dir)
355 struct via_info *via = devinfo;
356 struct via_chinfo *ch = (dir == PCMDIR_PLAY)? &via->pch : &via->rch;
362 ch->sgd_table = &via->sgd_table[(dir == PCMDIR_PLAY)? 0 : SEGS_PER_CHAN];
364 if (ch->dir == PCMDIR_PLAY) {
365 ch->rbase = VIA_MC_SGD_STATUS;
367 ch->rbase = VIA_WR0_SGD_STATUS;
368 via_wr(via, VIA_WR0_SGD_FORMAT, WR_FIFO_ENABLE, 1);
371 if (sndbuf_alloc(ch->buffer, via->parent_dmat, via->bufsz) == -1)
374 via8233chan_reset(via, ch);
380 via8233chan_trigger(kobj_t obj, void* data, int go)
382 struct via_chinfo *ch = data;
383 struct via_info *via = ch->parent;
384 struct via_dma_op *ado = ch->sgd_table;
389 via_wr(via, ch->rbase + VIA_RP_TABLE_PTR, vtophys(ado), 4);
390 via_wr(via, ch->rbase + VIA_RP_CONTROL,
391 SGD_CONTROL_START | SGD_CONTROL_AUTOSTART |
392 SGD_CONTROL_I_EOL | SGD_CONTROL_I_FLAG, 1);
396 via_wr(via, ch->rbase + VIA_RP_CONTROL, SGD_CONTROL_STOP, 1);
397 via8233chan_reset(via, ch);
403 static kobj_method_t via8233pchan_methods[] = {
404 KOBJMETHOD(channel_init, via8233chan_init),
405 KOBJMETHOD(channel_setformat, via8233pchan_setformat),
406 KOBJMETHOD(channel_setspeed, via8233pchan_setspeed),
407 KOBJMETHOD(channel_setblocksize, via8233chan_setblocksize),
408 KOBJMETHOD(channel_trigger, via8233chan_trigger),
409 KOBJMETHOD(channel_getptr, via8233chan_getptr),
410 KOBJMETHOD(channel_getcaps, via8233chan_getcaps),
413 CHANNEL_DECLARE(via8233pchan);
415 static kobj_method_t via8233rchan_methods[] = {
416 KOBJMETHOD(channel_init, via8233chan_init),
417 KOBJMETHOD(channel_setformat, via8233rchan_setformat),
418 KOBJMETHOD(channel_setspeed, via8233rchan_setspeed),
419 KOBJMETHOD(channel_setblocksize, via8233chan_setblocksize),
420 KOBJMETHOD(channel_trigger, via8233chan_trigger),
421 KOBJMETHOD(channel_getptr, via8233chan_getptr),
422 KOBJMETHOD(channel_getcaps, via8233chan_getcaps),
425 CHANNEL_DECLARE(via8233rchan);
427 /* -------------------------------------------------------------------- */
432 struct via_info *via = p;
433 int r = via_rd(via, VIA_MC_SGD_STATUS, 1);
434 if (r & SGD_STATUS_INTR) {
435 via_wr(via, VIA_MC_SGD_STATUS, SGD_STATUS_INTR, 1);
436 chn_intr(via->pch.channel);
439 r = via_rd(via, VIA_WR0_SGD_STATUS, 1);
440 if (r & SGD_STATUS_INTR) {
441 via_wr(via, VIA_WR0_SGD_STATUS, SGD_STATUS_INTR, 1);
442 chn_intr(via->rch.channel);
447 * Probe and attach the card
450 via_probe(device_t dev)
452 switch(pci_get_devid(dev)) {
454 switch(pci_get_revid(dev)) {
456 device_set_desc(dev, "VIA VT8233 (pre)");
459 device_set_desc(dev, "VIA VT8233C");
462 device_set_desc(dev, "VIA VT8233");
465 device_set_desc(dev, "VIA VT8233A");
468 device_set_desc(dev, "VIA VT8235");
471 device_set_desc(dev, "VIA VT8233X"); /* Unknown */
479 dma_cb(void *p, bus_dma_segment_t *bds, int a, int b)
484 via_chip_init(device_t dev)
488 pci_write_config(dev, VIA_PCI_ACLINK_CTRL, 0, 1);
491 /* assert ACLink reset */
492 pci_write_config(dev, VIA_PCI_ACLINK_CTRL, VIA_PCI_ACLINK_NRST, 1);
495 /* deassert ACLink reset, force SYNC (warm AC'97 reset) */
496 pci_write_config(dev, VIA_PCI_ACLINK_CTRL,
497 VIA_PCI_ACLINK_NRST | VIA_PCI_ACLINK_SYNC, 1);
499 /* ACLink on, deassert ACLink reset, VSR, SGD data out */
500 pci_write_config(dev, VIA_PCI_ACLINK_CTRL,
501 VIA_PCI_ACLINK_EN | VIA_PCI_ACLINK_NRST
502 | VIA_PCI_ACLINK_VRATE | VIA_PCI_ACLINK_SGD, 1);
504 for (i = 0; i < 100; i++) {
505 s = pci_read_config(dev, VIA_PCI_ACLINK_STAT, 1);
506 if (s & VIA_PCI_ACLINK_C00_READY) {
507 s = pci_read_config(dev, VIA_PCI_ACLINK_CTRL, 1);
512 device_printf(dev, "primary codec not ready (s = 0x%02x)\n", s);
517 via_attach(device_t dev)
519 struct via_info *via = 0;
520 char status[SND_STATUSLEN];
522 if ((via = malloc(sizeof *via, M_DEVBUF, M_NOWAIT | M_ZERO)) == NULL) {
523 device_printf(dev, "cannot allocate softc\n");
527 pci_enable_io(dev, SYS_RES_IOPORT);
528 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
529 pci_enable_busmaster(dev);
531 via->regid = PCIR_MAPS;
532 via->reg = bus_alloc_resource(dev, SYS_RES_IOPORT, &via->regid, 0, ~0,
535 device_printf(dev, "cannot allocate bus resource.");
538 via->st = rman_get_bustag(via->reg);
539 via->sh = rman_get_bushandle(via->reg);
541 via->bufsz = pcm_getbuffersize(dev, 4096, VIA_DEFAULT_BUFSZ, 65536);
544 via->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &via->irqid, 0, ~0, 1,
545 RF_ACTIVE | RF_SHAREABLE);
547 snd_setup_intr(dev, via->irq, 0, via_intr, via, &via->ih)) {
548 device_printf(dev, "unable to map interrupt\n");
552 /* DMA tag for buffers */
553 if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0,
554 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
555 /*highaddr*/BUS_SPACE_MAXADDR,
556 /*filter*/NULL, /*filterarg*/NULL,
557 /*maxsize*/via->bufsz, /*nsegments*/1, /*maxsegz*/0x3ffff,
558 /*flags*/0, &via->parent_dmat) != 0) {
559 device_printf(dev, "unable to create dma tag\n");
564 * DMA tag for SGD table. The 686 uses scatter/gather DMA and
565 * requires a list in memory of work to do. We need only 16 bytes
566 * for this list, and it is wasteful to allocate 16K.
568 if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0,
569 /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
570 /*highaddr*/BUS_SPACE_MAXADDR,
571 /*filter*/NULL, /*filterarg*/NULL,
572 /*maxsize*/NSEGS * sizeof(struct via_dma_op),
573 /*nsegments*/1, /*maxsegz*/0x3ffff,
574 /*flags*/0, &via->sgd_dmat) != 0) {
575 device_printf(dev, "unable to create dma tag\n");
579 if (bus_dmamem_alloc(via->sgd_dmat, (void **)&via->sgd_table,
580 BUS_DMA_NOWAIT, &via->sgd_dmamap) == -1)
582 if (bus_dmamap_load(via->sgd_dmat, via->sgd_dmamap, via->sgd_table,
583 NSEGS * sizeof(struct via_dma_op), dma_cb, 0, 0))
586 if (via_chip_init(dev))
589 via->codec = AC97_CREATE(dev, via, via_ac97);
593 mixer_init(dev, ac97_getmixerclass(), via->codec);
595 via->codec_caps = ac97_getextcaps(via->codec);
597 /* Try to set VRA without generating an error, VRM not reqrd yet */
598 if (via->codec_caps &
599 (AC97_EXTCAP_VRA | AC97_EXTCAP_VRM | AC97_EXTCAP_DRA)) {
600 u_int16_t ext = ac97_getextmode(via->codec);
601 ext |= (via->codec_caps &
602 (AC97_EXTCAP_VRA | AC97_EXTCAP_VRM));
603 ext &= ~AC97_EXTCAP_DRA;
604 ac97_setextmode(via->codec, ext);
607 snprintf(status, SND_STATUSLEN, "at io 0x%lx irq %ld",
608 rman_get_start(via->reg), rman_get_start(via->irq));
611 if (pcm_register(dev, via, 1, 1)) goto bad;
613 pcm_addchan(dev, PCMDIR_PLAY, &via8233pchan_class, via);
614 pcm_addchan(dev, PCMDIR_REC, &via8233rchan_class, via);
616 pcm_setstatus(dev, status);
620 if (via->codec) ac97_destroy(via->codec);
621 if (via->reg) bus_release_resource(dev, SYS_RES_IOPORT, via->regid, via->reg);
622 if (via->ih) bus_teardown_intr(dev, via->irq, via->ih);
623 if (via->irq) bus_release_resource(dev, SYS_RES_IRQ, via->irqid, via->irq);
624 if (via->parent_dmat) bus_dma_tag_destroy(via->parent_dmat);
625 if (via->sgd_dmamap) bus_dmamap_unload(via->sgd_dmat, via->sgd_dmamap);
626 if (via->sgd_dmat) bus_dma_tag_destroy(via->sgd_dmat);
627 if (via) free(via, M_DEVBUF);
632 via_detach(device_t dev)
635 struct via_info *via = 0;
637 r = pcm_unregister(dev);
640 via = pcm_getdevinfo(dev);
641 bus_release_resource(dev, SYS_RES_IOPORT, via->regid, via->reg);
642 bus_teardown_intr(dev, via->irq, via->ih);
643 bus_release_resource(dev, SYS_RES_IRQ, via->irqid, via->irq);
644 bus_dma_tag_destroy(via->parent_dmat);
645 bus_dmamap_unload(via->sgd_dmat, via->sgd_dmamap);
646 bus_dma_tag_destroy(via->sgd_dmat);
652 static device_method_t via_methods[] = {
653 DEVMETHOD(device_probe, via_probe),
654 DEVMETHOD(device_attach, via_attach),
655 DEVMETHOD(device_detach, via_detach),
659 static driver_t via_driver = {
665 DRIVER_MODULE(snd_via8233, pci, via_driver, pcm_devclass, 0, 0);
666 MODULE_DEPEND(snd_via8233, snd_pcm, PCM_MINVER, PCM_PREFVER, PCM_MAXVER);
667 MODULE_VERSION(snd_via8233, 1);