via82c686.c revision 254263
11590Srgrimes/*-
21590Srgrimes * Copyright (c) 2000 David Jones <dej@ox.org>
31590Srgrimes * All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes *
141590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
151590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
161590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
171590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
181590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
191590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
201590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
211590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
221590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
231590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
241590Srgrimes * SUCH DAMAGE.
251590Srgrimes */
261590Srgrimes
271590Srgrimes#ifdef HAVE_KERNEL_OPTION_HEADERS
281590Srgrimes#include "opt_snd.h"
291590Srgrimes#endif
301590Srgrimes
311590Srgrimes#include <dev/sound/pcm/sound.h>
321590Srgrimes#include <dev/sound/pcm/ac97.h>
331590Srgrimes
341590Srgrimes#include <dev/pci/pcireg.h>
3541568Sarchie#include <dev/pci/pcivar.h>
361590Srgrimes#include <sys/sysctl.h>
371590Srgrimes
381590Srgrimes#include <dev/sound/pci/via82c686.h>
391590Srgrimes
401590SrgrimesSND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pci/via82c686.c 254263 2013-08-12 23:30:01Z scottl $");
4153508Scharnier
4253508Scharnier#define VIA_PCI_ID 0x30581106
4353508Scharnier#define	NSEGS		4	/* Number of segments in SGD table */
4453508Scharnier
4553508Scharnier#define SEGS_PER_CHAN	(NSEGS/2)
461590Srgrimes
471590Srgrimes#define TIMEOUT	50
481590Srgrimes#define	VIA_DEFAULT_BUFSZ	0x1000
491590Srgrimes
501590Srgrimes#undef DEB
511590Srgrimes#define DEB(x)
521590Srgrimes
531590Srgrimes/* we rely on this struct being packed to 64 bits */
541590Srgrimesstruct via_dma_op {
551590Srgrimes        u_int32_t ptr;
561590Srgrimes        u_int32_t flags;
571590Srgrimes#define VIA_DMAOP_EOL         0x80000000
581590Srgrimes#define VIA_DMAOP_FLAG        0x40000000
591590Srgrimes#define VIA_DMAOP_STOP        0x20000000
601590Srgrimes#define VIA_DMAOP_COUNT(x)    ((x)&0x00FFFFFF)
611590Srgrimes};
621590Srgrimes
631590Srgrimesstruct via_info;
641590Srgrimes
651590Srgrimesstruct via_chinfo {
661590Srgrimes	struct via_info *parent;
671590Srgrimes	struct pcm_channel *channel;
681590Srgrimes	struct snd_dbuf *buffer;
691590Srgrimes	struct via_dma_op *sgd_table;
701590Srgrimes	bus_addr_t sgd_addr;
711590Srgrimes	int dir, blksz;
721590Srgrimes	int base, count, mode, ctrl;
731590Srgrimes};
741590Srgrimes
751590Srgrimesstruct via_info {
761590Srgrimes	bus_space_tag_t st;
771590Srgrimes	bus_space_handle_t sh;
781590Srgrimes	bus_dma_tag_t parent_dmat;
791590Srgrimes	bus_dma_tag_t sgd_dmat;
801590Srgrimes	bus_dmamap_t sgd_dmamap;
811590Srgrimes	bus_addr_t sgd_addr;
8224360Simp
831590Srgrimes	struct resource *reg, *irq;
841590Srgrimes	int regid, irqid;
851590Srgrimes	void *ih;
861590Srgrimes	struct ac97_info *codec;
871590Srgrimes
881590Srgrimes	unsigned int bufsz;
891590Srgrimes
901590Srgrimes	struct via_chinfo pch, rch;
911590Srgrimes	struct via_dma_op *sgd_table;
921590Srgrimes	u_int16_t codec_caps;
931590Srgrimes	struct mtx *lock;
941590Srgrimes};
951590Srgrimes
961590Srgrimesstatic u_int32_t via_fmt[] = {
971590Srgrimes	SND_FORMAT(AFMT_U8, 1, 0),
981590Srgrimes	SND_FORMAT(AFMT_U8, 2, 0),
991590Srgrimes	SND_FORMAT(AFMT_S16_LE, 1, 0),
1001590Srgrimes	SND_FORMAT(AFMT_S16_LE, 2, 0),
1011590Srgrimes	0
1021590Srgrimes};
1031590Srgrimesstatic struct pcmchan_caps via_vracaps = {4000, 48000, via_fmt, 0};
1041590Srgrimesstatic struct pcmchan_caps via_caps = {48000, 48000, via_fmt, 0};
1051590Srgrimes
1061590Srgrimesstatic __inline u_int32_t
10753508Scharniervia_rd(struct via_info *via, int regno, int size)
1081590Srgrimes{
1091590Srgrimes
1101590Srgrimes	switch (size) {
1111590Srgrimes	case 1:
1121590Srgrimes		return bus_space_read_1(via->st, via->sh, regno);
1131590Srgrimes	case 2:
1141590Srgrimes		return bus_space_read_2(via->st, via->sh, regno);
1151590Srgrimes	case 4:
1161590Srgrimes		return bus_space_read_4(via->st, via->sh, regno);
1171590Srgrimes	default:
1181590Srgrimes		return 0xFFFFFFFF;
1191590Srgrimes	}
1201590Srgrimes}
1211590Srgrimes
1221590Srgrimes
1231590Srgrimesstatic __inline void
1241590Srgrimesvia_wr(struct via_info *via, int regno, u_int32_t data, int size)
1251590Srgrimes{
1261590Srgrimes
1271590Srgrimes	switch (size) {
1281590Srgrimes	case 1:
1291590Srgrimes		bus_space_write_1(via->st, via->sh, regno, data);
1301590Srgrimes		break;
1311590Srgrimes	case 2:
1321590Srgrimes		bus_space_write_2(via->st, via->sh, regno, data);
1331590Srgrimes		break;
1341590Srgrimes	case 4:
1351590Srgrimes		bus_space_write_4(via->st, via->sh, regno, data);
1361590Srgrimes		break;
1371590Srgrimes	}
1381590Srgrimes}
13977586Sru
1401590Srgrimes/* -------------------------------------------------------------------- */
1411590Srgrimes/* Codec interface */
1421590Srgrimes
1431590Srgrimesstatic int
1441590Srgrimesvia_waitready_codec(struct via_info *via)
1451590Srgrimes{
1461590Srgrimes	int i;
1471590Srgrimes
1481590Srgrimes	/* poll until codec not busy */
1491590Srgrimes	for (i = 0; (i < TIMEOUT) &&
1501590Srgrimes	    (via_rd(via, VIA_CODEC_CTL, 4) & VIA_CODEC_BUSY); i++)
1511590Srgrimes		DELAY(1);
1521590Srgrimes	if (i >= TIMEOUT) {
1531590Srgrimes		printf("via: codec busy\n");
1541590Srgrimes		return 1;
1551590Srgrimes	}
1561590Srgrimes
1571590Srgrimes	return 0;
1581590Srgrimes}
1591590Srgrimes
1601590Srgrimes
1611590Srgrimesstatic int
1621590Srgrimesvia_waitvalid_codec(struct via_info *via)
1631590Srgrimes{
16453508Scharnier	int i;
1651590Srgrimes
1661590Srgrimes	/* poll until codec valid */
1671590Srgrimes	for (i = 0; (i < TIMEOUT) &&
1681590Srgrimes	    !(via_rd(via, VIA_CODEC_CTL, 4) & VIA_CODEC_PRIVALID); i++)
16937453Sbde		    DELAY(1);
1701590Srgrimes	if (i >= TIMEOUT) {
1711590Srgrimes		printf("via: codec invalid\n");
1721590Srgrimes		return 1;
1731590Srgrimes	}
1741590Srgrimes
1751590Srgrimes	return 0;
1761590Srgrimes}
1771590Srgrimes
1781590Srgrimes
1791590Srgrimesstatic int
18079010Sddvia_write_codec(kobj_t obj, void *addr, int reg, u_int32_t val)
18179010Sdd{
1821590Srgrimes	struct via_info *via = addr;
1831590Srgrimes
1841590Srgrimes	if (via_waitready_codec(via)) return -1;
1851590Srgrimes
1861590Srgrimes	via_wr(via, VIA_CODEC_CTL, VIA_CODEC_PRIVALID | VIA_CODEC_INDEX(reg) | val, 4);
1871590Srgrimes
1881590Srgrimes	return 0;
1891590Srgrimes}
1901590Srgrimes
1911590Srgrimes
1921590Srgrimesstatic int
1931590Srgrimesvia_read_codec(kobj_t obj, void *addr, int reg)
1941590Srgrimes{
1951590Srgrimes	struct via_info *via = addr;
1961590Srgrimes
1971590Srgrimes	if (via_waitready_codec(via))
1981590Srgrimes		return -1;
19937453Sbde
2001590Srgrimes	via_wr(via, VIA_CODEC_CTL, VIA_CODEC_PRIVALID | VIA_CODEC_READ | VIA_CODEC_INDEX(reg),4);
2011590Srgrimes
2021590Srgrimes	if (via_waitready_codec(via))
2031590Srgrimes		return -1;
2041590Srgrimes
20577586Sru	if (via_waitvalid_codec(via))
20677586Sru		return -1;
2071590Srgrimes
20877586Sru	return via_rd(via, VIA_CODEC_CTL, 2);
2091590Srgrimes}
2101590Srgrimes
2111590Srgrimesstatic kobj_method_t via_ac97_methods[] = {
2121590Srgrimes    	KOBJMETHOD(ac97_read,		via_read_codec),
2131590Srgrimes    	KOBJMETHOD(ac97_write,		via_write_codec),
2141590Srgrimes	KOBJMETHOD_END
2151590Srgrimes};
2161590SrgrimesAC97_DECLARE(via_ac97);
2171590Srgrimes
2181590Srgrimes/* -------------------------------------------------------------------- */
2191590Srgrimes
2201590Srgrimesstatic int
2211590Srgrimesvia_buildsgdt(struct via_chinfo *ch)
2221590Srgrimes{
2231590Srgrimes	u_int32_t phys_addr, flag;
2241590Srgrimes	int i, segs, seg_size;
2251590Srgrimes
2261590Srgrimes	/*
22737453Sbde	 *  Build the scatter/gather DMA (SGD) table.
2281590Srgrimes	 *  There are four slots in the table: two for play, two for record.
2291590Srgrimes	 *  This creates two half-buffers, one of which is playing; the other
2301590Srgrimes	 *  is feeding.
2311590Srgrimes	 */
2321590Srgrimes	seg_size = ch->blksz;
2331590Srgrimes	segs = sndbuf_getsize(ch->buffer) / seg_size;
2341590Srgrimes	phys_addr = sndbuf_getbufaddr(ch->buffer);
2351590Srgrimes
2361590Srgrimes	for (i = 0; i < segs; i++) {
2371590Srgrimes		flag = (i == segs - 1)? VIA_DMAOP_EOL : VIA_DMAOP_FLAG;
2381590Srgrimes		ch->sgd_table[i].ptr = phys_addr + (i * seg_size);
2391590Srgrimes		ch->sgd_table[i].flags = flag | seg_size;
2401590Srgrimes	}
2411590Srgrimes
2421590Srgrimes	return 0;
2431590Srgrimes}
2441590Srgrimes
2451590Srgrimes/* channel interface */
2461590Srgrimesstatic void *
2471590Srgrimesviachan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
2481590Srgrimes{
2491590Srgrimes	struct via_info *via = devinfo;
2501590Srgrimes	struct via_chinfo *ch;
2511590Srgrimes
2521590Srgrimes	snd_mtxlock(via->lock);
25353508Scharnier	if (dir == PCMDIR_PLAY) {
2541590Srgrimes		ch = &via->pch;
2551590Srgrimes		ch->base = VIA_PLAY_DMAOPS_BASE;
256		ch->count = VIA_PLAY_DMAOPS_COUNT;
257		ch->ctrl = VIA_PLAY_CONTROL;
258		ch->mode = VIA_PLAY_MODE;
259		ch->sgd_addr = via->sgd_addr;
260		ch->sgd_table = &via->sgd_table[0];
261	} else {
262		ch = &via->rch;
263		ch->base = VIA_RECORD_DMAOPS_BASE;
264		ch->count = VIA_RECORD_DMAOPS_COUNT;
265		ch->ctrl = VIA_RECORD_CONTROL;
266		ch->mode = VIA_RECORD_MODE;
267		ch->sgd_addr = via->sgd_addr + sizeof(struct via_dma_op) * SEGS_PER_CHAN;
268		ch->sgd_table = &via->sgd_table[SEGS_PER_CHAN];
269	}
270
271	ch->parent = via;
272	ch->channel = c;
273	ch->buffer = b;
274	ch->dir = dir;
275	snd_mtxunlock(via->lock);
276
277	if (sndbuf_alloc(ch->buffer, via->parent_dmat, 0, via->bufsz) != 0)
278		return NULL;
279
280	return ch;
281}
282
283static int
284viachan_setformat(kobj_t obj, void *data, u_int32_t format)
285{
286	struct via_chinfo *ch = data;
287	struct via_info *via = ch->parent;
288	int mode, mode_set;
289
290	mode_set = 0;
291	if (AFMT_CHANNEL(format) > 1)
292		mode_set |= VIA_RPMODE_STEREO;
293	if (format & AFMT_S16_LE)
294		mode_set |= VIA_RPMODE_16BIT;
295
296	DEB(printf("set format: dir = %d, format=%x\n", ch->dir, format));
297	snd_mtxlock(via->lock);
298	mode = via_rd(via, ch->mode, 1);
299	mode &= ~(VIA_RPMODE_16BIT | VIA_RPMODE_STEREO);
300	mode |= mode_set;
301	via_wr(via, ch->mode, mode, 1);
302	snd_mtxunlock(via->lock);
303
304	return 0;
305}
306
307static u_int32_t
308viachan_setspeed(kobj_t obj, void *data, u_int32_t speed)
309{
310	struct via_chinfo *ch = data;
311	struct via_info *via = ch->parent;
312	int reg;
313
314	/*
315	 *  Basic AC'97 defines a 48 kHz sample rate only.  For other rates,
316	 *  upsampling is required.
317	 *
318	 *  The VT82C686A does not perform upsampling, and neither do we.
319	 *  If the codec supports variable-rate audio (i.e. does the upsampling
320	 *  itself), then negotiate the rate with the codec.  Otherwise,
321	 *  return 48 kHz cuz that's all you got.
322	 */
323	if (via->codec_caps & AC97_EXTCAP_VRA) {
324		reg = (ch->dir == PCMDIR_PLAY)? AC97_REGEXT_FDACRATE : AC97_REGEXT_LADCRATE;
325		return ac97_setrate(via->codec, reg, speed);
326	} else
327		return 48000;
328}
329
330static u_int32_t
331viachan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
332{
333	struct via_chinfo *ch = data;
334
335	ch->blksz = blocksize;
336	sndbuf_resize(ch->buffer, SEGS_PER_CHAN, ch->blksz);
337
338	return ch->blksz;
339}
340
341static int
342viachan_trigger(kobj_t obj, void *data, int go)
343{
344	struct via_chinfo *ch = data;
345	struct via_info *via = ch->parent;
346	struct via_dma_op *ado;
347	bus_addr_t sgd_addr = ch->sgd_addr;
348
349	if (!PCMTRIG_COMMON(go))
350		return 0;
351
352	ado = ch->sgd_table;
353	DEB(printf("ado located at va=%p pa=%x\n", ado, sgd_addr));
354
355	snd_mtxlock(via->lock);
356	if (go == PCMTRIG_START) {
357		via_buildsgdt(ch);
358		via_wr(via, ch->base, sgd_addr, 4);
359		via_wr(via, ch->ctrl, VIA_RPCTRL_START, 1);
360	} else
361		via_wr(via, ch->ctrl, VIA_RPCTRL_TERMINATE, 1);
362	snd_mtxunlock(via->lock);
363
364	DEB(printf("viachan_trigger: go=%d\n", go));
365	return 0;
366}
367
368static u_int32_t
369viachan_getptr(kobj_t obj, void *data)
370{
371	struct via_chinfo *ch = data;
372	struct via_info *via = ch->parent;
373	struct via_dma_op *ado;
374	bus_addr_t sgd_addr = ch->sgd_addr;
375	u_int32_t ptr, base, base1, len, seg;
376
377	ado = ch->sgd_table;
378	snd_mtxlock(via->lock);
379	base1 = via_rd(via, ch->base, 4);
380	len = via_rd(via, ch->count, 4);
381	base = via_rd(via, ch->base, 4);
382	if (base != base1) 	/* Avoid race hazard */
383		len = via_rd(via, ch->count, 4);
384	snd_mtxunlock(via->lock);
385
386	DEB(printf("viachan_getptr: len / base = %x / %x\n", len, base));
387
388	/* Base points to SGD segment to do, one past current */
389
390	/* Determine how many segments have been done */
391	seg = (base - sgd_addr) / sizeof(struct via_dma_op);
392	if (seg == 0)
393		seg = SEGS_PER_CHAN;
394
395	/* Now work out offset: seg less count */
396	ptr = (seg * sndbuf_getsize(ch->buffer) / SEGS_PER_CHAN) - len;
397	if (ch->dir == PCMDIR_REC) {
398		/* DMA appears to operate on memory 'lines' of 32 bytes	*/
399		/* so don't return any part line - it isn't in RAM yet	*/
400		ptr = ptr & ~0x1f;
401	}
402
403	DEB(printf("return ptr=%u\n", ptr));
404	return ptr;
405}
406
407static struct pcmchan_caps *
408viachan_getcaps(kobj_t obj, void *data)
409{
410	struct via_chinfo *ch = data;
411	struct via_info *via = ch->parent;
412
413	return (via->codec_caps & AC97_EXTCAP_VRA)? &via_vracaps : &via_caps;
414}
415
416static kobj_method_t viachan_methods[] = {
417    	KOBJMETHOD(channel_init,		viachan_init),
418    	KOBJMETHOD(channel_setformat,		viachan_setformat),
419    	KOBJMETHOD(channel_setspeed,		viachan_setspeed),
420    	KOBJMETHOD(channel_setblocksize,	viachan_setblocksize),
421    	KOBJMETHOD(channel_trigger,		viachan_trigger),
422    	KOBJMETHOD(channel_getptr,		viachan_getptr),
423    	KOBJMETHOD(channel_getcaps,		viachan_getcaps),
424	KOBJMETHOD_END
425};
426CHANNEL_DECLARE(viachan);
427
428/* -------------------------------------------------------------------- */
429
430static void
431via_intr(void *p)
432{
433	struct via_info *via = p;
434
435	/* DEB(printf("viachan_intr\n")); */
436	/* Read channel */
437	snd_mtxlock(via->lock);
438	if (via_rd(via, VIA_PLAY_STAT, 1) & VIA_RPSTAT_INTR) {
439		via_wr(via, VIA_PLAY_STAT, VIA_RPSTAT_INTR, 1);
440		snd_mtxunlock(via->lock);
441		chn_intr(via->pch.channel);
442		snd_mtxlock(via->lock);
443	}
444
445	/* Write channel */
446	if (via_rd(via, VIA_RECORD_STAT, 1) & VIA_RPSTAT_INTR) {
447		via_wr(via, VIA_RECORD_STAT, VIA_RPSTAT_INTR, 1);
448		snd_mtxunlock(via->lock);
449		chn_intr(via->rch.channel);
450		return;
451	}
452	snd_mtxunlock(via->lock);
453}
454
455/*
456 *  Probe and attach the card
457 */
458static int
459via_probe(device_t dev)
460{
461	if (pci_get_devid(dev) == VIA_PCI_ID) {
462		device_set_desc(dev, "VIA VT82C686A");
463		return BUS_PROBE_DEFAULT;
464	}
465	return ENXIO;
466}
467
468
469static void
470dma_cb(void *p, bus_dma_segment_t *bds, int a, int b)
471{
472	struct via_info *via = (struct via_info *)p;
473	via->sgd_addr = bds->ds_addr;
474}
475
476
477static int
478via_attach(device_t dev)
479{
480	struct via_info *via = 0;
481	char status[SND_STATUSLEN];
482	u_int32_t data, cnt;
483
484	via = malloc(sizeof(*via), M_DEVBUF, M_WAITOK | M_ZERO);
485	via->lock = snd_mtxcreate(device_get_nameunit(dev),
486	    "snd_via82c686 softc");
487
488	pci_enable_busmaster(dev);
489
490	/* Wake up and reset AC97 if necessary */
491	data = pci_read_config(dev, VIA_AC97STATUS, 1);
492
493	if ((data & VIA_AC97STATUS_RDY) == 0) {
494		/* Cold reset per ac97r2.3 spec (page 95) */
495		pci_write_config(dev, VIA_ACLINKCTRL, VIA_ACLINK_EN, 1);			/* Assert low */
496		DELAY(100);									/* Wait T_rst_low */
497		pci_write_config(dev, VIA_ACLINKCTRL, VIA_ACLINK_EN | VIA_ACLINK_NRST, 1);	/* Assert high */
498		DELAY(5);									/* Wait T_rst2clk */
499		pci_write_config(dev, VIA_ACLINKCTRL, VIA_ACLINK_EN, 1);			/* Assert low */
500	} else {
501		/* Warm reset */
502		pci_write_config(dev, VIA_ACLINKCTRL, VIA_ACLINK_EN, 1);			/* Force no sync */
503		DELAY(100);
504		pci_write_config(dev, VIA_ACLINKCTRL, VIA_ACLINK_EN | VIA_ACLINK_SYNC, 1);	/* Sync */
505		DELAY(5);									/* Wait T_sync_high */
506		pci_write_config(dev, VIA_ACLINKCTRL, VIA_ACLINK_EN, 1);			/* Force no sync */
507		DELAY(5);									/* Wait T_sync2clk */
508	}
509
510	/* Power everything up */
511	pci_write_config(dev, VIA_ACLINKCTRL, VIA_ACLINK_DESIRED, 1);
512
513	/* Wait for codec to become ready (largest reported delay here 310ms) */
514	for (cnt = 0; cnt < 2000; cnt++) {
515		data = pci_read_config(dev, VIA_AC97STATUS, 1);
516		if (data & VIA_AC97STATUS_RDY)
517			break;
518		DELAY(5000);
519	}
520
521	via->regid = PCIR_BAR(0);
522	via->reg = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
523		&via->regid, RF_ACTIVE);
524	if (!via->reg) {
525		device_printf(dev, "cannot allocate bus resource.");
526		goto bad;
527	}
528	via->st = rman_get_bustag(via->reg);
529	via->sh = rman_get_bushandle(via->reg);
530
531	via->bufsz = pcm_getbuffersize(dev, 4096, VIA_DEFAULT_BUFSZ, 65536);
532
533	via->irqid = 0;
534	via->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &via->irqid,
535		RF_ACTIVE | RF_SHAREABLE);
536	if (!via->irq || snd_setup_intr(dev, via->irq, INTR_MPSAFE, via_intr, via, &via->ih)) {
537		device_printf(dev, "unable to map interrupt\n");
538		goto bad;
539	}
540
541	via_wr(via, VIA_PLAY_MODE, VIA_RPMODE_AUTOSTART | VIA_RPMODE_INTR_FLAG | VIA_RPMODE_INTR_EOL, 1);
542	via_wr(via, VIA_RECORD_MODE, VIA_RPMODE_AUTOSTART | VIA_RPMODE_INTR_FLAG | VIA_RPMODE_INTR_EOL, 1);
543
544	via->codec = AC97_CREATE(dev, via, via_ac97);
545	if (!via->codec)
546		goto bad;
547
548	if (mixer_init(dev, ac97_getmixerclass(), via->codec))
549		goto bad;
550
551	via->codec_caps = ac97_getextcaps(via->codec);
552	ac97_setextmode(via->codec,
553			via->codec_caps & (AC97_EXTCAP_VRA | AC97_EXTCAP_VRM));
554
555	/* DMA tag for buffers */
556	if (bus_dma_tag_create(/*parent*/bus_get_dma_tag(dev), /*alignment*/2,
557		/*boundary*/0,
558		/*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
559		/*highaddr*/BUS_SPACE_MAXADDR,
560		/*filter*/NULL, /*filterarg*/NULL,
561		/*maxsize*/via->bufsz, /*nsegments*/1, /*maxsegz*/0x3ffff,
562		/*flags*/0, /*lockfunc*/NULL,
563		/*lockarg*/NULL, &via->parent_dmat) != 0) {
564		device_printf(dev, "unable to create dma tag\n");
565		goto bad;
566	}
567
568	/*
569	 *  DMA tag for SGD table.  The 686 uses scatter/gather DMA and
570	 *  requires a list in memory of work to do.  We need only 16 bytes
571	 *  for this list, and it is wasteful to allocate 16K.
572	 */
573	if (bus_dma_tag_create(/*parent*/bus_get_dma_tag(dev), /*alignment*/2,
574		/*boundary*/0,
575		/*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
576		/*highaddr*/BUS_SPACE_MAXADDR,
577		/*filter*/NULL, /*filterarg*/NULL,
578		/*maxsize*/NSEGS * sizeof(struct via_dma_op),
579		/*nsegments*/1, /*maxsegz*/0x3ffff,
580		/*flags*/0, /*lockfunc*/NULL,
581		/*lockarg*/NULL, &via->sgd_dmat) != 0) {
582		device_printf(dev, "unable to create dma tag\n");
583		goto bad;
584	}
585
586	if (bus_dmamem_alloc(via->sgd_dmat, (void **)&via->sgd_table,
587	    BUS_DMA_NOWAIT, &via->sgd_dmamap) != 0)
588		goto bad;
589	if (bus_dmamap_load(via->sgd_dmat, via->sgd_dmamap, via->sgd_table,
590	    NSEGS * sizeof(struct via_dma_op), dma_cb, via, 0) != 0)
591		goto bad;
592
593	snprintf(status, SND_STATUSLEN, "at io 0x%lx irq %ld %s",
594		 rman_get_start(via->reg), rman_get_start(via->irq),
595		 PCM_KLDSTRING(snd_via82c686));
596
597	/* Register */
598	if (pcm_register(dev, via, 1, 1)) goto bad;
599	pcm_addchan(dev, PCMDIR_PLAY, &viachan_class, via);
600	pcm_addchan(dev, PCMDIR_REC, &viachan_class, via);
601	pcm_setstatus(dev, status);
602	return 0;
603bad:
604	if (via->codec) ac97_destroy(via->codec);
605	if (via->reg) bus_release_resource(dev, SYS_RES_IOPORT, via->regid, via->reg);
606	if (via->ih) bus_teardown_intr(dev, via->irq, via->ih);
607	if (via->irq) bus_release_resource(dev, SYS_RES_IRQ, via->irqid, via->irq);
608	if (via->parent_dmat) bus_dma_tag_destroy(via->parent_dmat);
609	if (via->sgd_dmamap) bus_dmamap_unload(via->sgd_dmat, via->sgd_dmamap);
610	if (via->sgd_table) bus_dmamem_free(via->sgd_dmat, via->sgd_table, via->sgd_dmamap);
611	if (via->sgd_dmat) bus_dma_tag_destroy(via->sgd_dmat);
612	if (via->lock) snd_mtxfree(via->lock);
613	if (via) free(via, M_DEVBUF);
614	return ENXIO;
615}
616
617static int
618via_detach(device_t dev)
619{
620	int r;
621	struct via_info *via = 0;
622
623	r = pcm_unregister(dev);
624	if (r)
625		return r;
626
627	via = pcm_getdevinfo(dev);
628	bus_release_resource(dev, SYS_RES_IOPORT, via->regid, via->reg);
629	bus_teardown_intr(dev, via->irq, via->ih);
630	bus_release_resource(dev, SYS_RES_IRQ, via->irqid, via->irq);
631	bus_dma_tag_destroy(via->parent_dmat);
632	bus_dmamap_unload(via->sgd_dmat, via->sgd_dmamap);
633	bus_dmamem_free(via->sgd_dmat, via->sgd_table, via->sgd_dmamap);
634	bus_dma_tag_destroy(via->sgd_dmat);
635	snd_mtxfree(via->lock);
636	free(via, M_DEVBUF);
637	return 0;
638}
639
640
641static device_method_t via_methods[] = {
642	DEVMETHOD(device_probe,		via_probe),
643	DEVMETHOD(device_attach,	via_attach),
644	DEVMETHOD(device_detach,	via_detach),
645	{ 0, 0}
646};
647
648static driver_t via_driver = {
649	"pcm",
650	via_methods,
651	PCM_SOFTC_SIZE,
652};
653
654DRIVER_MODULE(snd_via82c686, pci, via_driver, pcm_devclass, 0, 0);
655MODULE_DEPEND(snd_via82c686, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
656MODULE_VERSION(snd_via82c686, 1);
657