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