auvia.c revision 1.9 1 /* $NetBSD: auvia.c,v 1.9 2000/12/28 22:59:11 sommerfeld Exp $ */
2
3 /*-
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Tyler C. Sarna
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * VIA Technologies VT82C686A Southbridge Audio Driver
41 *
42 * Documentation links:
43 *
44 * ftp://ftp.alsa-project.org/pub/manuals/via/686a.pdf
45 * ftp://ftp.alsa-project.org/pub/manuals/general/ac97r21.pdf
46 * ftp://ftp.alsa-project.org/pub/manuals/ad/AD1881_0.pdf (example AC'97 codec)
47 */
48
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/malloc.h>
52 #include <sys/device.h>
53 #include <sys/audioio.h>
54
55 #include <uvm/uvm_extern.h>
56
57 #include <dev/pci/pcidevs.h>
58 #include <dev/pci/pcivar.h>
59
60 #include <dev/audio_if.h>
61 #include <dev/mulaw.h>
62 #include <dev/auconv.h>
63
64 #include <dev/ic/ac97var.h>
65
66 #include <dev/pci/auviavar.h>
67
68 struct auvia_dma {
69 struct auvia_dma *next;
70 caddr_t addr;
71 size_t size;
72 bus_dmamap_t map;
73 bus_dma_segment_t seg;
74 };
75
76 struct auvia_dma_op {
77 u_int32_t ptr;
78 u_int32_t flags;
79 #define AUVIA_DMAOP_EOL 0x80000000
80 #define AUVIA_DMAOP_FLAG 0x40000000
81 #define AUVIA_DMAOP_STOP 0x20000000
82 #define AUVIA_DMAOP_COUNT(x) ((x)&0x00FFFFFF)
83 };
84
85 /* rev. H and later seem to support only fixed rate 44.1 kHz */
86 #define AUVIA_FIXED_RATE 44100
87
88 int auvia_match(struct device *, struct cfdata *, void *);
89 void auvia_attach(struct device *, struct device *, void *);
90 int auvia_open(void *, int);
91 void auvia_close(void *);
92 int auvia_query_encoding(void *addr, struct audio_encoding *fp);
93 int auvia_set_params(void *, int, int, struct audio_params *,
94 struct audio_params *);
95 int auvia_round_blocksize(void *, int);
96 int auvia_halt_output(void *);
97 int auvia_halt_input(void *);
98 int auvia_getdev(void *, struct audio_device *);
99 int auvia_set_port(void *, mixer_ctrl_t *);
100 int auvia_get_port(void *, mixer_ctrl_t *);
101 int auvia_query_devinfo(void *, mixer_devinfo_t *);
102 void * auvia_malloc(void *, int, size_t, int, int);
103 void auvia_free(void *, void *, int);
104 size_t auvia_round_buffersize(void *, int, size_t);
105 paddr_t auvia_mappage(void *, void *, off_t, int);
106 int auvia_get_props(void *);
107 int auvia_build_dma_ops(struct auvia_softc *, struct auvia_softc_chan *,
108 struct auvia_dma *, void *, void *, int);
109 int auvia_trigger_output(void *, void *, void *, int, void (*)(void *),
110 void *, struct audio_params *);
111 int auvia_trigger_input(void *, void *, void *, int, void (*)(void *),
112 void *, struct audio_params *);
113
114 int auvia_intr __P((void *));
115
116 struct cfattach auvia_ca = {
117 sizeof (struct auvia_softc), auvia_match, auvia_attach
118 };
119
120 #define AUVIA_PCICONF_JUNK 0x40
121 #define AUVIA_PCICONF_ENABLES 0x00FF0000 /* reg 42 mask */
122 #define AUVIA_PCICONF_ACLINKENAB 0x00008000 /* ac link enab */
123 #define AUVIA_PCICONF_ACNOTRST 0x00004000 /* ~(ac reset) */
124 #define AUVIA_PCICONF_ACSYNC 0x00002000 /* ac sync */
125 #define AUVIA_PCICONF_ACVSR 0x00000800 /* var. samp. rate */
126 #define AUVIA_PCICONF_ACSGD 0x00000400 /* SGD enab */
127 #define AUVIA_PCICONF_ACFM 0x00000200 /* FM enab */
128 #define AUVIA_PCICONF_ACSB 0x00000100 /* SB enab */
129
130 #define AUVIA_PLAY_STAT 0x00
131 #define AUVIA_RECORD_STAT 0x10
132 #define AUVIA_RPSTAT_INTR 0x03
133 #define AUVIA_PLAY_CONTROL 0x01
134 #define AUVIA_RECORD_CONTROL 0x11
135 #define AUVIA_RPCTRL_START 0x80
136 #define AUVIA_RPCTRL_TERMINATE 0x40
137 #define AUVIA_PLAY_MODE 0x02
138 #define AUVIA_RECORD_MODE 0x12
139 #define AUVIA_RPMODE_INTR_FLAG 0x01
140 #define AUVIA_RPMODE_INTR_EOL 0x02
141 #define AUVIA_RPMODE_STEREO 0x10
142 #define AUVIA_RPMODE_16BIT 0x20
143 #define AUVIA_RPMODE_AUTOSTART 0x80
144 #define AUVIA_PLAY_DMAOPS_BASE 0x04
145 #define AUVIA_RECORD_DMAOPS_BASE 0x14
146
147 #define AUVIA_CODEC_CTL 0x80
148 #define AUVIA_CODEC_READ 0x00800000
149 #define AUVIA_CODEC_BUSY 0x01000000
150 #define AUVIA_CODEC_PRIVALID 0x02000000
151 #define AUVIA_CODEC_INDEX(x) ((x)<<16)
152
153 #define TIMEOUT 50
154
155 #define AC97_REG_EXT_AUDIO_ID 0x28
156 #define AC97_CODEC_DOES_VRA 0x0001
157 #define AC97_REG_EXT_AUDIO_STAT 0x2A
158 #define AC97_ENAB_VRA 0x0001
159 #define AC97_ENAB_MICVRA 0x0004
160 #define AC97_REG_EXT_DAC_RATE 0x2C
161 #define AC97_REG_EXT_ADC_RATE 0x32
162
163 struct audio_hw_if auvia_hw_if = {
164 auvia_open,
165 auvia_close,
166 NULL, /* drain */
167 auvia_query_encoding,
168 auvia_set_params,
169 auvia_round_blocksize,
170 NULL, /* commit_settings */
171 NULL, /* init_output */
172 NULL, /* init_input */
173 NULL, /* start_output */
174 NULL, /* start_input */
175 auvia_halt_output,
176 auvia_halt_input,
177 NULL, /* speaker_ctl */
178 auvia_getdev,
179 NULL, /* setfd */
180 auvia_set_port,
181 auvia_get_port,
182 auvia_query_devinfo,
183 auvia_malloc,
184 auvia_free,
185 auvia_round_buffersize,
186 auvia_mappage,
187 auvia_get_props,
188 auvia_trigger_output,
189 auvia_trigger_input,
190 };
191
192 int auvia_attach_codec(void *, struct ac97_codec_if *);
193 int auvia_write_codec(void *, u_int8_t, u_int16_t);
194 int auvia_read_codec(void *, u_int8_t, u_int16_t *);
195 void auvia_reset_codec(void *);
196 int auvia_waitready_codec(struct auvia_softc *sc);
197 int auvia_waitvalid_codec(struct auvia_softc *sc);
198
199
200 int
201 auvia_match(struct device *parent, struct cfdata *match, void *aux)
202 {
203 struct pci_attach_args *pa = (struct pci_attach_args *) aux;
204
205 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_VIATECH)
206 return 0;
207 if (PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_VIATECH_VT82C686A_AC97)
208 return 0;
209
210 return 1;
211 }
212
213
214 void
215 auvia_attach(struct device *parent, struct device *self, void *aux)
216 {
217 struct pci_attach_args *pa = aux;
218 struct auvia_softc *sc = (struct auvia_softc *) self;
219 const char *intrstr = NULL;
220 struct mixer_ctrl ctl;
221 pci_chipset_tag_t pc = pa->pa_pc;
222 pcitag_t pt = pa->pa_tag;
223 pci_intr_handle_t ih;
224 pcireg_t pr;
225 u_int16_t v;
226 int r, i;
227
228 r = PCI_REVISION(pa->pa_class);
229 sc->sc_revision[1] = '\0';
230 if (r == 0x20) {
231 sc->sc_revision[0] = 'H';
232 } else if ((r >= 0x10) && (r <= 0x14)) {
233 sc->sc_revision[0] = 'A' + (r - 0x10);
234 } else {
235 sprintf(sc->sc_revision, "0x%02X", r);
236 }
237
238 printf(": VIA VT82C686A AC'97 Audio (rev %s)\n",
239 sc->sc_revision);
240
241 if (pci_intr_map(pa, &ih)) {
242 printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
243 return;
244 }
245 intrstr = pci_intr_string(pc, ih);
246
247 sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, auvia_intr, sc);
248 if (sc->sc_ih == NULL) {
249 printf("%s: couldn't establish interrupt",sc->sc_dev.dv_xname);
250 if (intrstr != NULL)
251 printf(" at %s", intrstr);
252 printf("\n");
253 return;
254 }
255
256 sc->sc_dmat = pa->pa_dmat;
257 sc->sc_pc = pc;
258 sc->sc_pt = pt;
259
260 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
261
262 if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_IO, 0, &sc->sc_iot,
263 &sc->sc_ioh, &sc->sc_ioaddr, &sc->sc_iosize)) {
264 printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
265 return;
266 }
267
268 /* disable SBPro compat & others */
269 pr = pci_conf_read(pc, pt, AUVIA_PCICONF_JUNK);
270
271 pr &= ~AUVIA_PCICONF_ENABLES; /* clear compat function enables */
272 /* XXX what to do about MIDI, FM, joystick? */
273
274 pr |= (AUVIA_PCICONF_ACLINKENAB | AUVIA_PCICONF_ACNOTRST
275 | AUVIA_PCICONF_ACVSR | AUVIA_PCICONF_ACSGD);
276
277 pr &= ~(AUVIA_PCICONF_ACFM | AUVIA_PCICONF_ACSB);
278
279 pci_conf_write(pc, pt, AUVIA_PCICONF_JUNK, pr);
280
281 sc->host_if.arg = sc;
282 sc->host_if.attach = auvia_attach_codec;
283 sc->host_if.read = auvia_read_codec;
284 sc->host_if.write = auvia_write_codec;
285 sc->host_if.reset = auvia_reset_codec;
286
287 if ((r = ac97_attach(&sc->host_if)) != 0) {
288 printf("%s: can't attach codec (error 0x%X)\n",
289 sc->sc_dev.dv_xname, r);
290 return;
291 }
292
293 /*
294 * Print a warning if the codec doesn't support hardware variable
295 * rate audio.
296 */
297 if (auvia_read_codec(sc, AC97_REG_EXT_AUDIO_ID, &v)
298 || !(v & AC97_CODEC_DOES_VRA)) {
299 printf("%s: warning: codec doesn't support hardware AC'97 2.0 Variable Rate Audio\n",
300 sc->sc_dev.dv_xname);
301 sc->sc_fixed_rate = AUVIA_FIXED_RATE;
302 } else {
303 /* enable VRA */
304 auvia_write_codec(sc, AC97_REG_EXT_AUDIO_STAT,
305 AC97_ENAB_VRA | AC97_ENAB_MICVRA);
306 sc->sc_fixed_rate = 0;
307 }
308
309 /* disable mutes */
310 for (i = 0; i < 4; i++) {
311 static struct {
312 char *class, *device;
313 } d[] = {
314 { AudioCoutputs, AudioNmaster},
315 { AudioCinputs, AudioNdac},
316 { AudioCinputs, AudioNcd},
317 { AudioCrecord, AudioNvolume},
318 };
319
320 ctl.type = AUDIO_MIXER_ENUM;
321 ctl.un.ord = 0;
322
323 ctl.dev = sc->codec_if->vtbl->get_portnum_by_name(sc->codec_if,
324 d[i].class, d[i].device, AudioNmute);
325 auvia_set_port(sc, &ctl);
326 }
327
328 /* set a reasonable default volume */
329
330 ctl.type = AUDIO_MIXER_VALUE;
331 ctl.un.value.num_channels = 2;
332 ctl.un.value.level[AUDIO_MIXER_LEVEL_LEFT] = \
333 ctl.un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = 199;
334
335 ctl.dev = sc->codec_if->vtbl->get_portnum_by_name(sc->codec_if,
336 AudioCoutputs, AudioNmaster, NULL);
337 auvia_set_port(sc, &ctl);
338
339 audio_attach_mi(&auvia_hw_if, sc, &sc->sc_dev);
340 }
341
342
343 int
344 auvia_attach_codec(void *addr, struct ac97_codec_if *cif)
345 {
346 struct auvia_softc *sc = addr;
347
348 sc->codec_if = cif;
349
350 return 0;
351 }
352
353
354 void
355 auvia_reset_codec(void *addr)
356 {
357 #ifdef notyet /* XXX seems to make codec become unready... ??? */
358 struct auvia_softc *sc = addr;
359 pcireg_t r;
360
361 /* perform a codec cold reset */
362
363 r = pci_conf_read(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK);
364
365 r &= ~AUVIA_PCICONF_ACNOTRST; /* enable RESET (active low) */
366 pci_conf_write(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK, r);
367 delay(2);
368
369 r |= AUVIA_PCICONF_ACNOTRST; /* disable RESET (inactive high) */
370 pci_conf_write(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK, r);
371 delay(200);
372
373 auvia_waitready_codec(sc);
374 #endif
375 }
376
377
378 int
379 auvia_waitready_codec(struct auvia_softc *sc)
380 {
381 int i;
382
383 /* poll until codec not busy */
384 for (i = 0; (i < TIMEOUT) && (bus_space_read_4(sc->sc_iot, sc->sc_ioh,
385 AUVIA_CODEC_CTL) & AUVIA_CODEC_BUSY); i++)
386 delay(1);
387 if (i >= TIMEOUT) {
388 printf("%s: codec busy\n", sc->sc_dev.dv_xname);
389 return 1;
390 }
391
392 return 0;
393 }
394
395
396 int
397 auvia_waitvalid_codec(struct auvia_softc *sc)
398 {
399 int i;
400
401 /* poll until codec valid */
402 for (i = 0; (i < TIMEOUT) && !(bus_space_read_4(sc->sc_iot, sc->sc_ioh,
403 AUVIA_CODEC_CTL) & AUVIA_CODEC_PRIVALID); i++)
404 delay(1);
405 if (i >= TIMEOUT) {
406 printf("%s: codec invalid\n", sc->sc_dev.dv_xname);
407 return 1;
408 }
409
410 return 0;
411 }
412
413
414 int
415 auvia_write_codec(void *addr, u_int8_t reg, u_int16_t val)
416 {
417 struct auvia_softc *sc = addr;
418
419 if (auvia_waitready_codec(sc))
420 return 1;
421
422 bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL,
423 AUVIA_CODEC_PRIVALID | AUVIA_CODEC_INDEX(reg) | val);
424
425 return 0;
426 }
427
428
429 int
430 auvia_read_codec(void *addr, u_int8_t reg, u_int16_t *val)
431 {
432 struct auvia_softc *sc = addr;
433
434 if (auvia_waitready_codec(sc))
435 return 1;
436
437 bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL,
438 AUVIA_CODEC_PRIVALID | AUVIA_CODEC_READ | AUVIA_CODEC_INDEX(reg));
439
440 if (auvia_waitready_codec(sc))
441 return 1;
442
443 if (auvia_waitvalid_codec(sc))
444 return 1;
445
446 *val = bus_space_read_2(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL);
447
448 return 0;
449 }
450
451
452 int
453 auvia_open(void *addr, int flags)
454 {
455 return 0;
456 }
457
458
459 void
460 auvia_close(void *addr)
461 {
462 struct auvia_softc *sc = addr;
463
464 auvia_halt_output(sc);
465 auvia_halt_input(sc);
466
467 sc->sc_play.sc_intr = NULL;
468 sc->sc_record.sc_intr = NULL;
469 }
470
471
472 int
473 auvia_query_encoding(void *addr, struct audio_encoding *fp)
474 {
475 switch (fp->index) {
476 case 0:
477 strcpy(fp->name, AudioEulinear);
478 fp->encoding = AUDIO_ENCODING_ULINEAR;
479 fp->precision = 8;
480 fp->flags = 0;
481 return (0);
482 case 1:
483 strcpy(fp->name, AudioEmulaw);
484 fp->encoding = AUDIO_ENCODING_ULAW;
485 fp->precision = 8;
486 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
487 return (0);
488 case 2:
489 strcpy(fp->name, AudioEalaw);
490 fp->encoding = AUDIO_ENCODING_ALAW;
491 fp->precision = 8;
492 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
493 return (0);
494 case 3:
495 strcpy(fp->name, AudioEslinear);
496 fp->encoding = AUDIO_ENCODING_SLINEAR;
497 fp->precision = 8;
498 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
499 return (0);
500 case 4:
501 strcpy(fp->name, AudioEslinear_le);
502 fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
503 fp->precision = 16;
504 fp->flags = 0;
505 return (0);
506 case 5:
507 strcpy(fp->name, AudioEulinear_le);
508 fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
509 fp->precision = 16;
510 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
511 return (0);
512 case 6:
513 strcpy(fp->name, AudioEslinear_be);
514 fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
515 fp->precision = 16;
516 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
517 return (0);
518 case 7:
519 strcpy(fp->name, AudioEulinear_be);
520 fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
521 fp->precision = 16;
522 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
523 return (0);
524 default:
525 return (EINVAL);
526 }
527 }
528
529
530 int
531 auvia_set_params(void *addr, int setmode, int usemode,
532 struct audio_params *play, struct audio_params *rec)
533 {
534 struct auvia_softc *sc = addr;
535 struct audio_params *p;
536 u_int16_t regval;
537 int reg, mode;
538
539 /* for mode in (RECORD, PLAY) */
540 for (mode = AUMODE_RECORD; mode != -1;
541 mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
542 if ((setmode & mode) == 0)
543 continue;
544
545 p = mode == AUMODE_PLAY ? play : rec;
546
547 if (p->sample_rate < 4000 || p->sample_rate > 48000 ||
548 (p->precision != 8 && p->precision != 16) ||
549 (p->channels != 1 && p->channels != 2))
550 return (EINVAL);
551
552 reg = mode == AUMODE_PLAY ?
553 AC97_REG_EXT_DAC_RATE : AC97_REG_EXT_ADC_RATE;
554
555 if (!sc->sc_fixed_rate) {
556 auvia_write_codec(sc, reg, (u_int16_t) p->sample_rate);
557 auvia_read_codec(sc, reg, ®val);
558 p->sample_rate = regval;
559 } else
560 p->sample_rate = sc->sc_fixed_rate;
561
562 p->factor = 1;
563 p->sw_code = 0;
564 switch (p->encoding) {
565 case AUDIO_ENCODING_SLINEAR_BE:
566 if (p->precision == 16)
567 p->sw_code = swap_bytes;
568 else
569 p->sw_code = change_sign8;
570 break;
571 case AUDIO_ENCODING_SLINEAR_LE:
572 if (p->precision != 16)
573 p->sw_code = change_sign8;
574 break;
575 case AUDIO_ENCODING_ULINEAR_BE:
576 if (p->precision == 16) {
577 if (mode == AUMODE_PLAY)
578 p->sw_code = swap_bytes_change_sign16_le;
579 else
580 p->sw_code = change_sign16_swap_bytes_le;
581 }
582 break;
583 case AUDIO_ENCODING_ULINEAR_LE:
584 if (p->precision == 16)
585 p->sw_code = change_sign16_le;
586 break;
587 case AUDIO_ENCODING_ULAW:
588 if (mode == AUMODE_PLAY) {
589 p->factor = 2;
590 p->sw_code = mulaw_to_slinear16_le;
591 } else
592 p->sw_code = ulinear8_to_mulaw;
593 break;
594 case AUDIO_ENCODING_ALAW:
595 if (mode == AUMODE_PLAY) {
596 p->factor = 2;
597 p->sw_code = alaw_to_slinear16_le;
598 } else
599 p->sw_code = ulinear8_to_alaw;
600 break;
601 default:
602 return (EINVAL);
603 }
604
605 regval = (p->channels == 2 ? AUVIA_RPMODE_STEREO : 0)
606 | (p->precision * p->factor == 16 ?
607 AUVIA_RPMODE_16BIT : 0)
608 | AUVIA_RPMODE_INTR_FLAG | AUVIA_RPMODE_INTR_EOL
609 | AUVIA_RPMODE_AUTOSTART;
610
611 if (mode == AUMODE_PLAY) {
612 sc->sc_play.sc_reg = regval;
613 } else {
614 sc->sc_record.sc_reg = regval;
615 }
616 }
617
618 return 0;
619 }
620
621
622 int
623 auvia_round_blocksize(void *addr, int blk)
624 {
625 return (blk & -32);
626 }
627
628
629 int
630 auvia_halt_output(void *addr)
631 {
632 struct auvia_softc *sc = addr;
633
634 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_PLAY_CONTROL,
635 AUVIA_RPCTRL_TERMINATE);
636
637 return 0;
638 }
639
640
641 int
642 auvia_halt_input(void *addr)
643 {
644 struct auvia_softc *sc = addr;
645
646 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_RECORD_CONTROL,
647 AUVIA_RPCTRL_TERMINATE);
648
649 return 0;
650 }
651
652
653 int
654 auvia_getdev(void *addr, struct audio_device *retp)
655 {
656 struct auvia_softc *sc = addr;
657
658 if (retp) {
659 strncpy(retp->name, "VIA VT82C686A", sizeof(retp->name));
660 strncpy(retp->version, sc->sc_revision, sizeof(retp->version));
661 strncpy(retp->config, "auvia", sizeof(retp->config));
662 }
663
664 return 0;
665 }
666
667
668 int
669 auvia_set_port(void *addr, mixer_ctrl_t *cp)
670 {
671 struct auvia_softc *sc = addr;
672
673 return (sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp));
674 }
675
676
677 int
678 auvia_get_port(void *addr, mixer_ctrl_t *cp)
679 {
680 struct auvia_softc *sc = addr;
681
682 return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp));
683 }
684
685
686 int
687 auvia_query_devinfo(void *addr, mixer_devinfo_t *dip)
688 {
689 struct auvia_softc *sc = addr;
690
691 return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip));
692 }
693
694
695 void *
696 auvia_malloc(void *addr, int direction, size_t size, int pool, int flags)
697 {
698 struct auvia_softc *sc = addr;
699 struct auvia_dma *p;
700 int error;
701 int rseg;
702
703 p = malloc(sizeof(*p), pool, flags);
704 if (!p)
705 return 0;
706
707 p->size = size;
708 if ((error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &p->seg,
709 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
710 printf("%s: unable to allocate dma, error = %d\n",
711 sc->sc_dev.dv_xname, error);
712 goto fail_alloc;
713 }
714
715 if ((error = bus_dmamem_map(sc->sc_dmat, &p->seg, rseg, size, &p->addr,
716 BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
717 printf("%s: unable to map dma, error = %d\n",
718 sc->sc_dev.dv_xname, error);
719 goto fail_map;
720 }
721
722 if ((error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
723 BUS_DMA_NOWAIT, &p->map)) != 0) {
724 printf("%s: unable to create dma map, error = %d\n",
725 sc->sc_dev.dv_xname, error);
726 goto fail_create;
727 }
728
729 if ((error = bus_dmamap_load(sc->sc_dmat, p->map, p->addr, size, NULL,
730 BUS_DMA_NOWAIT)) != 0) {
731 printf("%s: unable to load dma map, error = %d\n",
732 sc->sc_dev.dv_xname, error);
733 goto fail_load;
734 }
735
736 p->next = sc->sc_dmas;
737 sc->sc_dmas = p;
738
739 return p->addr;
740
741
742 fail_load:
743 bus_dmamap_destroy(sc->sc_dmat, p->map);
744 fail_create:
745 bus_dmamem_unmap(sc->sc_dmat, p->addr, size);
746 fail_map:
747 bus_dmamem_free(sc->sc_dmat, &p->seg, 1);
748 fail_alloc:
749 free(p, pool);
750 return 0;
751 }
752
753
754 void
755 auvia_free(void *addr, void *ptr, int pool)
756 {
757 struct auvia_softc *sc = addr;
758 struct auvia_dma **pp, *p;
759
760 for (pp = &(sc->sc_dmas); (p = *pp) != NULL; pp = &p->next)
761 if (p->addr == ptr) {
762 bus_dmamap_unload(sc->sc_dmat, p->map);
763 bus_dmamap_destroy(sc->sc_dmat, p->map);
764 bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
765 bus_dmamem_free(sc->sc_dmat, &p->seg, 1);
766
767 *pp = p->next;
768 free(p, pool);
769 return;
770 }
771
772 panic("auvia_free: trying to free unallocated memory");
773 }
774
775
776 size_t
777 auvia_round_buffersize(void *addr, int direction, size_t size)
778 {
779 return size;
780 }
781
782
783 paddr_t
784 auvia_mappage(void *addr, void *mem, off_t off, int prot)
785 {
786 struct auvia_softc *sc = addr;
787 struct auvia_dma *p;
788
789 if (off < 0)
790 return -1;
791
792 for (p = sc->sc_dmas; p && p->addr != mem; p = p->next)
793 ;
794
795 if (!p)
796 return -1;
797
798 return bus_dmamem_mmap(sc->sc_dmat, &p->seg, 1, off, prot,
799 BUS_DMA_WAITOK);
800 }
801
802
803 int
804 auvia_get_props(void *addr)
805 {
806 return AUDIO_PROP_MMAP | AUDIO_PROP_INDEPENDENT
807 | AUDIO_PROP_FULLDUPLEX;
808 }
809
810
811 int
812 auvia_build_dma_ops(struct auvia_softc *sc, struct auvia_softc_chan *ch,
813 struct auvia_dma *p, void *start, void *end, int blksize)
814 {
815 struct auvia_dma_op *op;
816 struct auvia_dma *dp;
817 bus_addr_t s, e;
818 size_t l;
819 int segs;
820
821 s = p->map->dm_segs[0].ds_addr;
822 l = ((char *)end - (char *)start);
823 e = s + l;
824 segs = (l + blksize - 1) / blksize;
825
826 if (segs > (ch->sc_dma_op_count)) {
827 /* if old list was too small, free it */
828 if (ch->sc_dma_ops) {
829 auvia_free(sc, ch->sc_dma_ops, M_DEVBUF);
830 }
831
832 ch->sc_dma_ops = auvia_malloc(sc, 0,
833 sizeof(struct auvia_dma_op) * segs, M_DEVBUF, M_WAITOK);
834
835 if (ch->sc_dma_ops == NULL) {
836 printf("%s: couldn't build dmaops\n", sc->sc_dev.dv_xname);
837 return 1;
838 }
839
840 for (dp = sc->sc_dmas;
841 dp && dp->addr != (void *)(ch->sc_dma_ops);
842 dp = dp->next)
843 ;
844
845 if (!dp)
846 panic("%s: build_dma_ops: where'd my memory go??? "
847 "address (%p)\n", sc->sc_dev.dv_xname,
848 ch->sc_dma_ops);
849
850 ch->sc_dma_op_count = segs;
851 ch->sc_dma_ops_dma = dp;
852 }
853
854 dp = ch->sc_dma_ops_dma;
855 op = ch->sc_dma_ops;
856
857 while (l) {
858 op->ptr = s;
859 l = l - blksize;
860 if (!l) {
861 /* if last block */
862 op->flags = AUVIA_DMAOP_EOL | blksize;
863 } else {
864 op->flags = AUVIA_DMAOP_FLAG | blksize;
865 }
866 s += blksize;
867 op++;
868 }
869
870 return 0;
871 }
872
873
874 int
875 auvia_trigger_output(void *addr, void *start, void *end,
876 int blksize, void (*intr)(void *), void *arg,
877 struct audio_params *param)
878 {
879 struct auvia_softc *sc = addr;
880 struct auvia_softc_chan *ch = &(sc->sc_play);
881 struct auvia_dma *p;
882
883 for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
884 ;
885
886 if (!p)
887 panic("auvia_trigger_output: request with bad start "
888 "address (%p)\n", start);
889
890 if (auvia_build_dma_ops(sc, ch, p, start, end, blksize)) {
891 return 1;
892 }
893
894 ch->sc_intr = intr;
895 ch->sc_arg = arg;
896
897 bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_PLAY_DMAOPS_BASE,
898 ch->sc_dma_ops_dma->map->dm_segs[0].ds_addr);
899
900 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_PLAY_MODE,
901 ch->sc_reg);
902
903 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_PLAY_CONTROL,
904 AUVIA_RPCTRL_START);
905
906 return 0;
907 }
908
909
910 int
911 auvia_trigger_input(void *addr, void *start, void *end,
912 int blksize, void (*intr)(void *), void *arg,
913 struct audio_params *param)
914 {
915 struct auvia_softc *sc = addr;
916 struct auvia_softc_chan *ch = &(sc->sc_record);
917 struct auvia_dma *p;
918
919 for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
920 ;
921
922 if (!p)
923 panic("auvia_trigger_input: request with bad start "
924 "address (%p)\n", start);
925
926 if (auvia_build_dma_ops(sc, ch, p, start, end, blksize)) {
927 return 1;
928 }
929
930 ch->sc_intr = intr;
931 ch->sc_arg = arg;
932
933 bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_RECORD_DMAOPS_BASE,
934 ch->sc_dma_ops_dma->map->dm_segs[0].ds_addr);
935
936 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_RECORD_MODE,
937 ch->sc_reg);
938
939 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_RECORD_CONTROL,
940 AUVIA_RPCTRL_START);
941
942 return 0;
943 }
944
945
946 int
947 auvia_intr(void *arg)
948 {
949 struct auvia_softc *sc = arg;
950 u_int8_t r;
951
952 r = bus_space_read_1(sc->sc_iot, sc->sc_ioh, AUVIA_RECORD_STAT);
953 if (r & AUVIA_RPSTAT_INTR) {
954 if (sc->sc_record.sc_intr)
955 sc->sc_record.sc_intr(sc->sc_record.sc_arg);
956
957 /* clear interrupts */
958 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_RECORD_STAT,
959 AUVIA_RPSTAT_INTR);
960 }
961 r = bus_space_read_1(sc->sc_iot, sc->sc_ioh, AUVIA_PLAY_STAT);
962 if (r & AUVIA_RPSTAT_INTR) {
963 if (sc->sc_play.sc_intr)
964 sc->sc_play.sc_intr(sc->sc_play.sc_arg);
965
966 /* clear interrupts */
967 bus_space_write_1(sc->sc_iot, sc->sc_ioh, AUVIA_PLAY_STAT,
968 AUVIA_RPSTAT_INTR);
969 }
970
971 return 1;
972 }
973