via82c686.c revision 111898
1/*
2 * Copyright (c) 2000 David Jones <dej@ox.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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, WHETHER IN 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 THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <dev/sound/pcm/sound.h>
28#include <dev/sound/pcm/ac97.h>
29
30#include <pci/pcireg.h>
31#include <pci/pcivar.h>
32#include <sys/sysctl.h>
33
34#include <dev/sound/pci/via82c686.h>
35
36SND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pci/via82c686.c 111898 2003-03-05 05:56:18Z orion $");
37
38#define VIA_PCI_ID 0x30581106
39#define	NSEGS		4	/* Number of segments in SGD table */
40
41#define SEGS_PER_CHAN	(NSEGS/2)
42
43#define TIMEOUT	50
44#define	VIA_DEFAULT_BUFSZ	0x1000
45
46#undef DEB
47#define DEB(x)
48
49/* we rely on this struct being packed to 64 bits */
50struct via_dma_op {
51        u_int32_t ptr;
52        u_int32_t flags;
53#define VIA_DMAOP_EOL         0x80000000
54#define VIA_DMAOP_FLAG        0x40000000
55#define VIA_DMAOP_STOP        0x20000000
56#define VIA_DMAOP_COUNT(x)    ((x)&0x00FFFFFF)
57};
58
59struct via_info;
60
61struct via_chinfo {
62	struct via_info *parent;
63	struct pcm_channel *channel;
64	struct snd_dbuf *buffer;
65	struct via_dma_op *sgd_table;
66	bus_addr_t sgd_addr;
67	int dir, blksz;
68	int base, count, mode, ctrl;
69};
70
71struct via_info {
72	bus_space_tag_t st;
73	bus_space_handle_t sh;
74	bus_dma_tag_t parent_dmat;
75	bus_dma_tag_t sgd_dmat;
76	bus_dmamap_t sgd_dmamap;
77	bus_addr_t sgd_addr;
78
79	struct resource *reg, *irq;
80	int regid, irqid;
81	void *ih;
82	struct ac97_info *codec;
83
84	unsigned int bufsz;
85
86	struct via_chinfo pch, rch;
87	struct via_dma_op *sgd_table;
88	u_int16_t codec_caps;
89};
90
91static u_int32_t via_fmt[] = {
92	AFMT_U8,
93	AFMT_STEREO | AFMT_U8,
94	AFMT_S16_LE,
95	AFMT_STEREO | AFMT_S16_LE,
96	0
97};
98static struct pcmchan_caps via_vracaps = {4000, 48000, via_fmt, 0};
99static struct pcmchan_caps via_caps = {48000, 48000, via_fmt, 0};
100
101static u_int32_t
102via_rd(struct via_info *via, int regno, int size)
103{
104
105	switch (size) {
106	case 1:
107		return bus_space_read_1(via->st, via->sh, regno);
108	case 2:
109		return bus_space_read_2(via->st, via->sh, regno);
110	case 4:
111		return bus_space_read_4(via->st, via->sh, regno);
112	default:
113		return 0xFFFFFFFF;
114	}
115}
116
117
118static void
119via_wr(struct via_info *via, int regno, u_int32_t data, int size)
120{
121
122	switch (size) {
123	case 1:
124		bus_space_write_1(via->st, via->sh, regno, data);
125		break;
126	case 2:
127		bus_space_write_2(via->st, via->sh, regno, data);
128		break;
129	case 4:
130		bus_space_write_4(via->st, via->sh, regno, data);
131		break;
132	}
133}
134
135/* -------------------------------------------------------------------- */
136/* Codec interface */
137
138static int
139via_waitready_codec(struct via_info *via)
140{
141	int i;
142
143	/* poll until codec not busy */
144	for (i = 0; (i < TIMEOUT) &&
145	    (via_rd(via, VIA_CODEC_CTL, 4) & VIA_CODEC_BUSY); i++)
146		DELAY(1);
147	if (i >= TIMEOUT) {
148		printf("via: codec busy\n");
149		return 1;
150	}
151
152	return 0;
153}
154
155
156static int
157via_waitvalid_codec(struct via_info *via)
158{
159	int i;
160
161	/* poll until codec valid */
162	for (i = 0; (i < TIMEOUT) &&
163	    !(via_rd(via, VIA_CODEC_CTL, 4) & VIA_CODEC_PRIVALID); i++)
164		    DELAY(1);
165	if (i >= TIMEOUT) {
166		printf("via: codec invalid\n");
167		return 1;
168	}
169
170	return 0;
171}
172
173
174static int
175via_write_codec(kobj_t obj, void *addr, int reg, u_int32_t val)
176{
177	struct via_info *via = addr;
178
179	if (via_waitready_codec(via)) return -1;
180
181	via_wr(via, VIA_CODEC_CTL, VIA_CODEC_PRIVALID | VIA_CODEC_INDEX(reg) | val, 4);
182
183	return 0;
184}
185
186
187static int
188via_read_codec(kobj_t obj, void *addr, int reg)
189{
190	struct via_info *via = addr;
191
192	if (via_waitready_codec(via))
193		return -1;
194
195	via_wr(via, VIA_CODEC_CTL, VIA_CODEC_PRIVALID | VIA_CODEC_READ | VIA_CODEC_INDEX(reg),4);
196
197	if (via_waitready_codec(via))
198		return -1;
199
200	if (via_waitvalid_codec(via))
201		return -1;
202
203	return via_rd(via, VIA_CODEC_CTL, 2);
204}
205
206static kobj_method_t via_ac97_methods[] = {
207    	KOBJMETHOD(ac97_read,		via_read_codec),
208    	KOBJMETHOD(ac97_write,		via_write_codec),
209	{ 0, 0 }
210};
211AC97_DECLARE(via_ac97);
212
213/* -------------------------------------------------------------------- */
214
215static int
216via_buildsgdt(struct via_chinfo *ch)
217{
218	u_int32_t phys_addr, flag;
219	int i, segs, seg_size;
220
221	/*
222	 *  Build the scatter/gather DMA (SGD) table.
223	 *  There are four slots in the table: two for play, two for record.
224	 *  This creates two half-buffers, one of which is playing; the other
225	 *  is feeding.
226	 */
227	seg_size = ch->blksz;
228	segs = sndbuf_getsize(ch->buffer) / seg_size;
229	phys_addr = sndbuf_getbufaddr(ch->buffer);
230
231	for (i = 0; i < segs; i++) {
232		flag = (i == segs - 1)? VIA_DMAOP_EOL : VIA_DMAOP_FLAG;
233		ch->sgd_table[i].ptr = phys_addr + (i * seg_size);
234		ch->sgd_table[i].flags = flag | seg_size;
235	}
236
237	return 0;
238}
239
240/* channel interface */
241static void *
242viachan_init(kobj_t obj, void *devinfo, struct snd_dbuf *b, struct pcm_channel *c, int dir)
243{
244	struct via_info *via = devinfo;
245	struct via_chinfo *ch;
246
247	if (dir == PCMDIR_PLAY) {
248		ch = &via->pch;
249		ch->base = VIA_PLAY_DMAOPS_BASE;
250		ch->count = VIA_PLAY_DMAOPS_COUNT;
251		ch->ctrl = VIA_PLAY_CONTROL;
252		ch->mode = VIA_PLAY_MODE;
253		ch->sgd_addr = via->sgd_addr;
254		ch->sgd_table = &via->sgd_table[0];
255	} else {
256		ch = &via->rch;
257		ch->base = VIA_RECORD_DMAOPS_BASE;
258		ch->count = VIA_RECORD_DMAOPS_COUNT;
259		ch->ctrl = VIA_RECORD_CONTROL;
260		ch->mode = VIA_RECORD_MODE;
261		ch->sgd_addr = via->sgd_addr + sizeof(struct via_dma_op) * SEGS_PER_CHAN;
262		ch->sgd_table = &via->sgd_table[SEGS_PER_CHAN];
263	}
264
265	ch->parent = via;
266	ch->channel = c;
267	ch->buffer = b;
268	ch->dir = dir;
269
270	if (sndbuf_alloc(ch->buffer, via->parent_dmat, via->bufsz) == -1)
271		return NULL;
272	return ch;
273}
274
275static int
276viachan_setformat(kobj_t obj, void *data, u_int32_t format)
277{
278	struct via_chinfo *ch = data;
279	struct via_info *via = ch->parent;
280	int mode, mode_set;
281
282	mode_set = 0;
283	if (format & AFMT_STEREO)
284		mode_set |= VIA_RPMODE_STEREO;
285	if (format & AFMT_S16_LE)
286		mode_set |= VIA_RPMODE_16BIT;
287
288	DEB(printf("set format: dir = %d, format=%x\n", ch->dir, format));
289	mode = via_rd(via, ch->mode, 1);
290	mode &= ~(VIA_RPMODE_16BIT | VIA_RPMODE_STEREO);
291	mode |= mode_set;
292	via_wr(via, ch->mode, mode, 1);
293
294	return 0;
295}
296
297static int
298viachan_setspeed(kobj_t obj, void *data, u_int32_t speed)
299{
300	struct via_chinfo *ch = data;
301	struct via_info *via = ch->parent;
302	int reg;
303
304	/*
305	 *  Basic AC'97 defines a 48 kHz sample rate only.  For other rates,
306	 *  upsampling is required.
307	 *
308	 *  The VT82C686A does not perform upsampling, and neither do we.
309	 *  If the codec supports variable-rate audio (i.e. does the upsampling
310	 *  itself), then negotiate the rate with the codec.  Otherwise,
311	 *  return 48 kHz cuz that's all you got.
312	 */
313	if (via->codec_caps & AC97_EXTCAP_VRA) {
314		reg = (ch->dir == PCMDIR_PLAY)? AC97_REGEXT_FDACRATE : AC97_REGEXT_LADCRATE;
315		return ac97_setrate(via->codec, reg, speed);
316	} else
317		return 48000;
318}
319
320static int
321viachan_setblocksize(kobj_t obj, void *data, u_int32_t blocksize)
322{
323	struct via_chinfo *ch = data;
324
325	ch->blksz = blocksize;
326	sndbuf_resize(ch->buffer, SEGS_PER_CHAN, ch->blksz);
327
328	return ch->blksz;
329}
330
331static int
332viachan_trigger(kobj_t obj, void *data, int go)
333{
334	struct via_chinfo *ch = data;
335	struct via_info *via = ch->parent;
336	struct via_dma_op *ado;
337	bus_addr_t sgd_addr = ch->sgd_addr;
338
339	if (go == PCMTRIG_EMLDMAWR || go == PCMTRIG_EMLDMARD)
340		return 0;
341
342	ado = ch->sgd_table;
343	DEB(printf("ado located at va=%p pa=%x\n", ado, sgd_addr));
344
345	if (go == PCMTRIG_START) {
346		via_buildsgdt(ch);
347		via_wr(via, ch->base, sgd_addr, 4);
348		via_wr(via, ch->ctrl, VIA_RPCTRL_START, 1);
349	} else
350		via_wr(via, ch->ctrl, VIA_RPCTRL_TERMINATE, 1);
351
352	DEB(printf("viachan_trigger: go=%d\n", go));
353	return 0;
354}
355
356static int
357viachan_getptr(kobj_t obj, void *data)
358{
359	struct via_chinfo *ch = data;
360	struct via_info *via = ch->parent;
361	struct via_dma_op *ado;
362	bus_addr_t sgd_addr = ch->sgd_addr;
363	int ptr, base, base1, len, seg;
364
365	ado = ch->sgd_table;
366	base1 = via_rd(via, ch->base, 4);
367	len = via_rd(via, ch->count, 4);
368	base = via_rd(via, ch->base, 4);
369	if (base != base1) 	/* Avoid race hazard */
370		len = via_rd(via, ch->count, 4);
371
372	DEB(printf("viachan_getptr: len / base = %x / %x\n", len, base));
373
374	/* Base points to SGD segment to do, one past current */
375
376	/* Determine how many segments have been done */
377	seg = (base - sgd_addr) / sizeof(struct via_dma_op);
378	if (seg == 0)
379		seg = SEGS_PER_CHAN;
380
381	/* Now work out offset: seg less count */
382	ptr = (seg * sndbuf_getsize(ch->buffer) / SEGS_PER_CHAN) - len;
383	if (ch->dir == PCMDIR_REC) {
384		/* DMA appears to operate on memory 'lines' of 32 bytes	*/
385		/* so don't return any part line - it isn't in RAM yet	*/
386		ptr = ptr & ~0x1f;
387	}
388
389	DEB(printf("return ptr=%d\n", ptr));
390	return ptr;
391}
392
393static struct pcmchan_caps *
394viachan_getcaps(kobj_t obj, void *data)
395{
396	struct via_chinfo *ch = data;
397	struct via_info *via = ch->parent;
398
399	return (via->codec_caps & AC97_EXTCAP_VRA)? &via_vracaps : &via_caps;
400}
401
402static kobj_method_t viachan_methods[] = {
403    	KOBJMETHOD(channel_init,		viachan_init),
404    	KOBJMETHOD(channel_setformat,		viachan_setformat),
405    	KOBJMETHOD(channel_setspeed,		viachan_setspeed),
406    	KOBJMETHOD(channel_setblocksize,	viachan_setblocksize),
407    	KOBJMETHOD(channel_trigger,		viachan_trigger),
408    	KOBJMETHOD(channel_getptr,		viachan_getptr),
409    	KOBJMETHOD(channel_getcaps,		viachan_getcaps),
410	{ 0, 0 }
411};
412CHANNEL_DECLARE(viachan);
413
414/* -------------------------------------------------------------------- */
415
416static void
417via_intr(void *p)
418{
419	struct via_info *via = p;
420	int		st;
421
422	/* DEB(printf("viachan_intr\n")); */
423	/* Read channel */
424	st = via_rd(via, VIA_PLAY_STAT, 1);
425	if (st & VIA_RPSTAT_INTR) {
426		via_wr(via, VIA_PLAY_STAT, VIA_RPSTAT_INTR, 1);
427		chn_intr(via->pch.channel);
428	}
429
430	/* Write channel */
431	st = via_rd(via, VIA_RECORD_STAT, 1);
432	if (st & VIA_RPSTAT_INTR) {
433		via_wr(via, VIA_RECORD_STAT, VIA_RPSTAT_INTR, 1);
434		chn_intr(via->rch.channel);
435	}
436}
437
438/*
439 *  Probe and attach the card
440 */
441static int
442via_probe(device_t dev)
443{
444	if (pci_get_devid(dev) == VIA_PCI_ID) {
445	    device_set_desc(dev, "VIA VT82C686A");
446	    return 0;
447	}
448	return ENXIO;
449}
450
451
452static void
453dma_cb(void *p, bus_dma_segment_t *bds, int a, int b)
454{
455	struct via_info *via = (struct via_info *)p;
456	via->sgd_addr = bds->ds_addr;
457}
458
459
460static int
461via_attach(device_t dev)
462{
463	struct via_info *via = 0;
464	char status[SND_STATUSLEN];
465	u_int32_t data;
466
467	if ((via = malloc(sizeof *via, M_DEVBUF, M_NOWAIT | M_ZERO)) == NULL) {
468		device_printf(dev, "cannot allocate softc\n");
469		return ENXIO;
470	}
471
472	/* Get resources */
473	data = pci_read_config(dev, PCIR_COMMAND, 2);
474	data |= (PCIM_CMD_PORTEN | PCIM_CMD_BUSMASTEREN);
475	pci_write_config(dev, PCIR_COMMAND, data, 2);
476	data = pci_read_config(dev, PCIR_COMMAND, 2);
477
478	/* Wake up and reset AC97 if necessary */
479	if (!(pci_read_config(dev, VIA_AC97STATUS, 1) & VIA_AC97STATUS_RDY)) {
480		pci_write_config(dev, VIA_ACLINKCTRL, VIA_ACLINK_EN | VIA_ACLINK_NRST | VIA_ACLINK_SYNC, 1);
481		DELAY(1000);
482		pci_write_config(dev, VIA_ACLINKCTRL,  0, 1);
483		DELAY(1000);
484		pci_write_config(dev, VIA_ACLINKCTRL, VIA_ACLINK_DESIRED, 1);
485		DELAY(1000);
486	}
487
488	if (pci_read_config(dev, VIA_ACLINKCTRL, 1) != VIA_ACLINK_DESIRED) {
489		pci_write_config(dev, VIA_ACLINKCTRL, VIA_ACLINK_DESIRED, 1);
490		DELAY(1000);
491	}
492
493	via->regid = PCIR_MAPS;
494	via->reg = bus_alloc_resource(dev, SYS_RES_IOPORT, &via->regid, 0, ~0, 1, RF_ACTIVE);
495	if (!via->reg) {
496		device_printf(dev, "cannot allocate bus resource.");
497		goto bad;
498	}
499	via->st = rman_get_bustag(via->reg);
500	via->sh = rman_get_bushandle(via->reg);
501
502	via->bufsz = pcm_getbuffersize(dev, 4096, VIA_DEFAULT_BUFSZ, 65536);
503
504	via->irqid = 0;
505	via->irq = bus_alloc_resource(dev, SYS_RES_IRQ, &via->irqid, 0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
506	if (!via->irq || snd_setup_intr(dev, via->irq, 0, via_intr, via, &via->ih)) {
507		device_printf(dev, "unable to map interrupt\n");
508		goto bad;
509	}
510
511	via_wr(via, VIA_PLAY_MODE, VIA_RPMODE_AUTOSTART | VIA_RPMODE_INTR_FLAG | VIA_RPMODE_INTR_EOL, 1);
512	via_wr(via, VIA_RECORD_MODE, VIA_RPMODE_AUTOSTART | VIA_RPMODE_INTR_FLAG | VIA_RPMODE_INTR_EOL, 1);
513
514	via->codec = AC97_CREATE(dev, via, via_ac97);
515	if (!via->codec)
516		goto bad;
517
518	if (mixer_init(dev, ac97_getmixerclass(), via->codec))
519		goto bad;
520
521	via->codec_caps = ac97_getextcaps(via->codec);
522	ac97_setextmode(via->codec,
523			via->codec_caps & (AC97_EXTCAP_VRA | AC97_EXTCAP_VRM));
524
525	/* DMA tag for buffers */
526	if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0,
527		/*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
528		/*highaddr*/BUS_SPACE_MAXADDR,
529		/*filter*/NULL, /*filterarg*/NULL,
530		/*maxsize*/via->bufsz, /*nsegments*/1, /*maxsegz*/0x3ffff,
531		/*flags*/0, &via->parent_dmat) != 0) {
532		device_printf(dev, "unable to create dma tag\n");
533		goto bad;
534	}
535
536	/*
537	 *  DMA tag for SGD table.  The 686 uses scatter/gather DMA and
538	 *  requires a list in memory of work to do.  We need only 16 bytes
539	 *  for this list, and it is wasteful to allocate 16K.
540	 */
541	if (bus_dma_tag_create(/*parent*/NULL, /*alignment*/2, /*boundary*/0,
542		/*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
543		/*highaddr*/BUS_SPACE_MAXADDR,
544		/*filter*/NULL, /*filterarg*/NULL,
545		/*maxsize*/NSEGS * sizeof(struct via_dma_op),
546		/*nsegments*/1, /*maxsegz*/0x3ffff,
547		/*flags*/0, &via->sgd_dmat) != 0) {
548		device_printf(dev, "unable to create dma tag\n");
549		goto bad;
550	}
551
552	if (bus_dmamem_alloc(via->sgd_dmat, (void **)&via->sgd_table, BUS_DMA_NOWAIT, &via->sgd_dmamap) == -1)
553		goto bad;
554	if (bus_dmamap_load(via->sgd_dmat, via->sgd_dmamap, via->sgd_table, NSEGS * sizeof(struct via_dma_op), dma_cb, via, 0))
555		goto bad;
556
557	snprintf(status, SND_STATUSLEN, "at io 0x%lx irq %ld", rman_get_start(via->reg), rman_get_start(via->irq));
558
559	/* Register */
560	if (pcm_register(dev, via, 1, 1)) goto bad;
561	pcm_addchan(dev, PCMDIR_PLAY, &viachan_class, via);
562	pcm_addchan(dev, PCMDIR_REC, &viachan_class, via);
563	pcm_setstatus(dev, status);
564	return 0;
565bad:
566	if (via->codec) ac97_destroy(via->codec);
567	if (via->reg) bus_release_resource(dev, SYS_RES_IOPORT, via->regid, via->reg);
568	if (via->ih) bus_teardown_intr(dev, via->irq, via->ih);
569	if (via->irq) bus_release_resource(dev, SYS_RES_IRQ, via->irqid, via->irq);
570	if (via->parent_dmat) bus_dma_tag_destroy(via->parent_dmat);
571	if (via->sgd_dmamap) bus_dmamap_unload(via->sgd_dmat, via->sgd_dmamap);
572	if (via->sgd_dmat) bus_dma_tag_destroy(via->sgd_dmat);
573	if (via) free(via, M_DEVBUF);
574	return ENXIO;
575}
576
577static int
578via_detach(device_t dev)
579{
580	int r;
581	struct via_info *via = 0;
582
583	r = pcm_unregister(dev);
584	if (r)
585		return r;
586
587	via = pcm_getdevinfo(dev);
588	bus_release_resource(dev, SYS_RES_IOPORT, via->regid, via->reg);
589	bus_teardown_intr(dev, via->irq, via->ih);
590	bus_release_resource(dev, SYS_RES_IRQ, via->irqid, via->irq);
591	bus_dma_tag_destroy(via->parent_dmat);
592	bus_dmamap_unload(via->sgd_dmat, via->sgd_dmamap);
593	bus_dma_tag_destroy(via->sgd_dmat);
594	free(via, M_DEVBUF);
595	return 0;
596}
597
598
599static device_method_t via_methods[] = {
600	DEVMETHOD(device_probe,		via_probe),
601	DEVMETHOD(device_attach,	via_attach),
602	DEVMETHOD(device_detach,	via_detach),
603	{ 0, 0}
604};
605
606static driver_t via_driver = {
607	"pcm",
608	via_methods,
609	PCM_SOFTC_SIZE,
610};
611
612DRIVER_MODULE(snd_via82c686, pci, via_driver, pcm_devclass, 0, 0);
613MODULE_DEPEND(snd_via82c686, snd_pcm, PCM_MINVER, PCM_PREFVER, PCM_MAXVER);
614MODULE_VERSION(snd_via82c686, 1);
615