auvia.c revision 1.45 1 /* $NetBSD: auvia.c,v 1.45 2004/11/08 06:20:58 kent 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 / VT8233 / VT8235 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/cdefs.h>
50 __KERNEL_RCSID(0, "$NetBSD: auvia.c,v 1.45 2004/11/08 06:20:58 kent Exp $");
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/malloc.h>
55 #include <sys/device.h>
56 #include <sys/audioio.h>
57
58 #include <uvm/uvm_extern.h>
59
60 #include <dev/pci/pcidevs.h>
61 #include <dev/pci/pcivar.h>
62
63 #include <dev/audio_if.h>
64 #include <dev/mulaw.h>
65 #include <dev/auconv.h>
66
67 #include <dev/ic/ac97reg.h>
68 #include <dev/ic/ac97var.h>
69
70 #include <dev/pci/auviavar.h>
71
72 struct auvia_dma {
73 struct auvia_dma *next;
74 caddr_t addr;
75 size_t size;
76 bus_dmamap_t map;
77 bus_dma_segment_t seg;
78 };
79
80 struct auvia_dma_op {
81 u_int32_t ptr;
82 u_int32_t flags;
83 #define AUVIA_DMAOP_EOL 0x80000000
84 #define AUVIA_DMAOP_FLAG 0x40000000
85 #define AUVIA_DMAOP_STOP 0x20000000
86 #define AUVIA_DMAOP_COUNT(x) ((x)&0x00FFFFFF)
87 };
88
89 int auvia_match(struct device *, struct cfdata *, void *);
90 void auvia_attach(struct device *, struct device *, void *);
91 int auvia_open(void *, int);
92 void auvia_close(void *);
93 int auvia_query_encoding(void *, struct audio_encoding *);
94 void auvia_set_params_sub(struct auvia_softc *, struct auvia_softc_chan *,
95 struct audio_params *);
96 int auvia_set_params(void *, int, int, struct audio_params *,
97 struct audio_params *);
98 int auvia_round_blocksize(void *, int);
99 int auvia_halt_output(void *);
100 int auvia_halt_input(void *);
101 int auvia_getdev(void *, struct audio_device *);
102 int auvia_set_port(void *, mixer_ctrl_t *);
103 int auvia_get_port(void *, mixer_ctrl_t *);
104 int auvia_query_devinfo(void *, mixer_devinfo_t *);
105 void * auvia_malloc(void *, int, size_t, struct malloc_type *, int);
106 void auvia_free(void *, void *, struct malloc_type *);
107 size_t auvia_round_buffersize(void *, int, size_t);
108 paddr_t auvia_mappage(void *, void *, off_t, int);
109 int auvia_get_props(void *);
110 int auvia_build_dma_ops(struct auvia_softc *, struct auvia_softc_chan *,
111 struct auvia_dma *, void *, void *, int);
112 int auvia_trigger_output(void *, void *, void *, int, void (*)(void *),
113 void *, struct audio_params *);
114 int auvia_trigger_input(void *, void *, void *, int, void (*)(void *),
115 void *, struct audio_params *);
116 void auvia_powerhook(int, void *);
117 int auvia_intr(void *);
118
119 CFATTACH_DECL(auvia, sizeof (struct auvia_softc),
120 auvia_match, auvia_attach, NULL, NULL);
121
122 /* VIA VT823xx revision number */
123 #define VIA_REV_8233C 0x20
124 #define VIA_REV_8233 0x30
125 #define VIA_REV_8233A 0x40
126 #define VIA_REV_8235 0x50
127 #define VIA_REV_8237 0x60
128
129 #define AUVIA_PCICONF_JUNK 0x40
130 #define AUVIA_PCICONF_ENABLES 0x00FF0000 /* reg 42 mask */
131 #define AUVIA_PCICONF_ACLINKENAB 0x00008000 /* ac link enab */
132 #define AUVIA_PCICONF_ACNOTRST 0x00004000 /* ~(ac reset) */
133 #define AUVIA_PCICONF_ACSYNC 0x00002000 /* ac sync */
134 #define AUVIA_PCICONF_ACVSR 0x00000800 /* var. samp. rate */
135 #define AUVIA_PCICONF_ACSGD 0x00000400 /* SGD enab */
136 #define AUVIA_PCICONF_ACFM 0x00000200 /* FM enab */
137 #define AUVIA_PCICONF_ACSB 0x00000100 /* SB enab */
138 #define AUVIA_PCICONF_PRIVALID 0x00000001 /* primary codec rdy */
139
140 #define AUVIA_PLAY_BASE 0x00
141 #define AUVIA_RECORD_BASE 0x10
142
143 /* *_RP_* are offsets from AUVIA_PLAY_BASE or AUVIA_RECORD_BASE */
144 #define AUVIA_RP_STAT 0x00
145 #define AUVIA_RPSTAT_INTR 0x03
146 #define AUVIA_RP_CONTROL 0x01
147 #define AUVIA_RPCTRL_START 0x80
148 #define AUVIA_RPCTRL_TERMINATE 0x40
149 #define AUVIA_RPCTRL_AUTOSTART 0x20
150 /* The following are 8233 specific */
151 #define AUVIA_RPCTRL_STOP 0x04
152 #define AUVIA_RPCTRL_EOL 0x02
153 #define AUVIA_RPCTRL_FLAG 0x01
154 #define AUVIA_RP_MODE 0x02 /* 82c686 specific */
155 #define AUVIA_RPMODE_INTR_FLAG 0x01
156 #define AUVIA_RPMODE_INTR_EOL 0x02
157 #define AUVIA_RPMODE_STEREO 0x10
158 #define AUVIA_RPMODE_16BIT 0x20
159 #define AUVIA_RPMODE_AUTOSTART 0x80
160 #define AUVIA_RP_DMAOPS_BASE 0x04
161
162 #define VIA8233_RP_DXS_LVOL 0x02
163 #define VIA8233_RP_DXS_RVOL 0x03
164 #define VIA8233_RP_RATEFMT 0x08
165 #define VIA8233_RATEFMT_48K 0xfffff
166 #define VIA8233_RATEFMT_STEREO 0x00100000
167 #define VIA8233_RATEFMT_16BIT 0x00200000
168
169 #define VIA_RP_DMAOPS_COUNT 0x0c
170
171 #define VIA8233_MP_BASE 0x40
172 /* STAT, CONTROL, DMAOPS_BASE, DMAOPS_COUNT are valid */
173 #define VIA8233_OFF_MP_FORMAT 0x02
174 #define VIA8233_MP_FORMAT_8BIT 0x00
175 #define VIA8233_MP_FORMAT_16BIT 0x80
176 #define VIA8233_MP_FORMAT_CHANNLE_MASK 0x70 /* 1, 2, 4, 6 */
177 #define VIA8233_OFF_MP_SCRATCH 0x03
178 #define VIA8233_OFF_MP_STOP 0x08
179
180 #define AUVIA_CODEC_CTL 0x80
181 #define AUVIA_CODEC_READ 0x00800000
182 #define AUVIA_CODEC_BUSY 0x01000000
183 #define AUVIA_CODEC_PRIVALID 0x02000000
184 #define AUVIA_CODEC_INDEX(x) ((x)<<16)
185
186 #define CH_WRITE1(sc, ch, off, v) \
187 bus_space_write_1((sc)->sc_iot, (sc)->sc_ioh, (ch)->sc_base + (off), v)
188 #define CH_WRITE4(sc, ch, off, v) \
189 bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (ch)->sc_base + (off), v)
190 #define CH_READ1(sc, ch, off) \
191 bus_space_read_1((sc)->sc_iot, (sc)->sc_ioh, (ch)->sc_base + (off))
192 #define CH_READ4(sc, ch, off) \
193 bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (ch)->sc_base + (off))
194
195 #define TIMEOUT 50
196
197 const struct audio_hw_if auvia_hw_if = {
198 auvia_open,
199 auvia_close,
200 NULL, /* drain */
201 auvia_query_encoding,
202 auvia_set_params,
203 auvia_round_blocksize,
204 NULL, /* commit_settings */
205 NULL, /* init_output */
206 NULL, /* init_input */
207 NULL, /* start_output */
208 NULL, /* start_input */
209 auvia_halt_output,
210 auvia_halt_input,
211 NULL, /* speaker_ctl */
212 auvia_getdev,
213 NULL, /* setfd */
214 auvia_set_port,
215 auvia_get_port,
216 auvia_query_devinfo,
217 auvia_malloc,
218 auvia_free,
219 auvia_round_buffersize,
220 auvia_mappage,
221 auvia_get_props,
222 auvia_trigger_output,
223 auvia_trigger_input,
224 NULL, /* dev_ioctl */
225 };
226
227 int auvia_attach_codec(void *, struct ac97_codec_if *);
228 int auvia_write_codec(void *, u_int8_t, u_int16_t);
229 int auvia_read_codec(void *, u_int8_t, u_int16_t *);
230 int auvia_reset_codec(void *);
231 int auvia_waitready_codec(struct auvia_softc *sc);
232 int auvia_waitvalid_codec(struct auvia_softc *sc);
233
234
235 int
236 auvia_match(struct device *parent, struct cfdata *match, void *aux)
237 {
238 struct pci_attach_args *pa = (struct pci_attach_args *) aux;
239
240 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_VIATECH)
241 return 0;
242 switch (PCI_PRODUCT(pa->pa_id)) {
243 case PCI_PRODUCT_VIATECH_VT82C686A_AC97:
244 case PCI_PRODUCT_VIATECH_VT8233_AC97:
245 break;
246 default:
247 return 0;
248 }
249
250 return 1;
251 }
252
253
254 void
255 auvia_attach(struct device *parent, struct device *self, void *aux)
256 {
257 struct pci_attach_args *pa = aux;
258 struct auvia_softc *sc = (struct auvia_softc *) self;
259 const char *intrstr = NULL;
260 pci_chipset_tag_t pc = pa->pa_pc;
261 pcitag_t pt = pa->pa_tag;
262 pci_intr_handle_t ih;
263 bus_size_t iosize;
264 pcireg_t pr;
265 int r;
266 const char *revnum = NULL; /* VT823xx revision number */
267
268 aprint_naive(": Audio controller\n");
269
270 sc->sc_play.sc_base = AUVIA_PLAY_BASE;
271 sc->sc_record.sc_base = AUVIA_RECORD_BASE;
272 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_VIATECH_VT8233_AC97) {
273 sc->sc_flags |= AUVIA_FLAGS_VT8233;
274 sc->sc_play.sc_base = VIA8233_MP_BASE;
275 }
276
277 if (pci_mapreg_map(pa, 0x10, PCI_MAPREG_TYPE_IO, 0, &sc->sc_iot,
278 &sc->sc_ioh, NULL, &iosize)) {
279 aprint_error(": can't map i/o space\n");
280 return;
281 }
282
283 sc->sc_dmat = pa->pa_dmat;
284 sc->sc_pc = pc;
285 sc->sc_pt = pt;
286
287 r = PCI_REVISION(pa->pa_class);
288 if (sc->sc_flags & AUVIA_FLAGS_VT8233) {
289 snprintf(sc->sc_revision, sizeof(sc->sc_revision), "0x%02X", r);
290 switch(r) {
291 case VIA_REV_8233C:
292 /* 2 rec, 4 pb, 1 multi-pb */
293 revnum = "3C";
294 break;
295 case VIA_REV_8233:
296 /* 2 rec, 4 pb, 1 multi-pb, spdif */
297 revnum = "3";
298 break;
299 case VIA_REV_8233A:
300 /* 1 rec, 1 multi-pb, spdif */
301 revnum = "3A";
302 break;
303 default:
304 break;
305 }
306 if (r >= VIA_REV_8235)
307 revnum = "7";
308 else if (r >= VIA_REV_8235) /* 2 rec, 4 pb, 1 multi-pb, spdif */
309 revnum = "5";
310 aprint_normal(": VIA Technologies VT823%s AC'97 Audio "
311 "(rev %s)\n", revnum, sc->sc_revision);
312 } else {
313 sc->sc_revision[1] = '\0';
314 if (r == 0x20) {
315 sc->sc_revision[0] = 'H';
316 } else if ((r >= 0x10) && (r <= 0x14)) {
317 sc->sc_revision[0] = 'A' + (r - 0x10);
318 } else {
319 snprintf(sc->sc_revision, sizeof(sc->sc_revision),
320 "0x%02X", r);
321 }
322
323 aprint_normal(": VIA Technologies VT82C686A AC'97 Audio "
324 "(rev %s)\n", sc->sc_revision);
325 }
326
327 if (pci_intr_map(pa, &ih)) {
328 aprint_error(": couldn't map interrupt\n");
329 bus_space_unmap(sc->sc_iot, sc->sc_ioh, iosize);
330 return;
331 }
332 intrstr = pci_intr_string(pc, ih);
333
334 sc->sc_ih = pci_intr_establish(pc, ih, IPL_AUDIO, auvia_intr, sc);
335 if (sc->sc_ih == NULL) {
336 aprint_error("%s: couldn't establish interrupt",
337 sc->sc_dev.dv_xname);
338 if (intrstr != NULL)
339 aprint_normal(" at %s", intrstr);
340 aprint_normal("\n");
341 bus_space_unmap(sc->sc_iot, sc->sc_ioh, iosize);
342 return;
343 }
344
345 aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
346
347 /* disable SBPro compat & others */
348 pr = pci_conf_read(pc, pt, AUVIA_PCICONF_JUNK);
349
350 pr &= ~AUVIA_PCICONF_ENABLES; /* clear compat function enables */
351 /* XXX what to do about MIDI, FM, joystick? */
352
353 pr |= (AUVIA_PCICONF_ACLINKENAB | AUVIA_PCICONF_ACNOTRST
354 | AUVIA_PCICONF_ACVSR | AUVIA_PCICONF_ACSGD);
355
356 pr &= ~(AUVIA_PCICONF_ACFM | AUVIA_PCICONF_ACSB);
357
358 pci_conf_write(pc, pt, AUVIA_PCICONF_JUNK, pr);
359
360 sc->host_if.arg = sc;
361 sc->host_if.attach = auvia_attach_codec;
362 sc->host_if.read = auvia_read_codec;
363 sc->host_if.write = auvia_write_codec;
364 sc->host_if.reset = auvia_reset_codec;
365
366 if ((r = ac97_attach(&sc->host_if)) != 0) {
367 aprint_error("%s: can't attach codec (error 0x%X)\n",
368 sc->sc_dev.dv_xname, r);
369 pci_intr_disestablish(pc, sc->sc_ih);
370 bus_space_unmap(sc->sc_iot, sc->sc_ioh, iosize);
371 return;
372 }
373
374 /* Watch for power change */
375 sc->sc_suspend = PWR_RESUME;
376 sc->sc_powerhook = powerhook_establish(auvia_powerhook, sc);
377
378 audio_attach_mi(&auvia_hw_if, sc, &sc->sc_dev);
379 }
380
381
382 int
383 auvia_attach_codec(void *addr, struct ac97_codec_if *cif)
384 {
385 struct auvia_softc *sc = addr;
386
387 sc->codec_if = cif;
388
389 return 0;
390 }
391
392
393 int
394 auvia_reset_codec(void *addr)
395 {
396 int i;
397 struct auvia_softc *sc = addr;
398 pcireg_t r;
399
400 /* perform a codec cold reset */
401
402 r = pci_conf_read(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK);
403
404 r &= ~AUVIA_PCICONF_ACNOTRST; /* enable RESET (active low) */
405 pci_conf_write(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK, r);
406 delay(2);
407
408 r |= AUVIA_PCICONF_ACNOTRST; /* disable RESET (inactive high) */
409 pci_conf_write(sc->sc_pc, sc->sc_pt, AUVIA_PCICONF_JUNK, r);
410 delay(200);
411
412 for (i = 500000; i != 0 && !(pci_conf_read(sc->sc_pc, sc->sc_pt,
413 AUVIA_PCICONF_JUNK) & AUVIA_PCICONF_PRIVALID); i--)
414 DELAY(1);
415 if (i == 0) {
416 printf("%s: codec reset timed out\n", sc->sc_dev.dv_xname);
417 return ETIMEDOUT;
418 }
419 return 0;
420 }
421
422
423 int
424 auvia_waitready_codec(struct auvia_softc *sc)
425 {
426 int i;
427
428 /* poll until codec not busy */
429 for (i = 0; (i < TIMEOUT) && (bus_space_read_4(sc->sc_iot, sc->sc_ioh,
430 AUVIA_CODEC_CTL) & AUVIA_CODEC_BUSY); i++)
431 delay(1);
432 if (i >= TIMEOUT) {
433 printf("%s: codec busy\n", sc->sc_dev.dv_xname);
434 return 1;
435 }
436
437 return 0;
438 }
439
440
441 int
442 auvia_waitvalid_codec(struct auvia_softc *sc)
443 {
444 int i;
445
446 /* poll until codec valid */
447 for (i = 0; (i < TIMEOUT) && !(bus_space_read_4(sc->sc_iot, sc->sc_ioh,
448 AUVIA_CODEC_CTL) & AUVIA_CODEC_PRIVALID); i++)
449 delay(1);
450 if (i >= TIMEOUT) {
451 printf("%s: codec invalid\n", sc->sc_dev.dv_xname);
452 return 1;
453 }
454
455 return 0;
456 }
457
458
459 int
460 auvia_write_codec(void *addr, u_int8_t reg, u_int16_t val)
461 {
462 struct auvia_softc *sc = addr;
463
464 if (auvia_waitready_codec(sc))
465 return 1;
466
467 bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL,
468 AUVIA_CODEC_PRIVALID | AUVIA_CODEC_INDEX(reg) | val);
469
470 return 0;
471 }
472
473
474 int
475 auvia_read_codec(void *addr, u_int8_t reg, u_int16_t *val)
476 {
477 struct auvia_softc *sc = addr;
478
479 if (auvia_waitready_codec(sc))
480 return 1;
481
482 bus_space_write_4(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL,
483 AUVIA_CODEC_PRIVALID | AUVIA_CODEC_READ | AUVIA_CODEC_INDEX(reg));
484
485 if (auvia_waitready_codec(sc))
486 return 1;
487
488 if (auvia_waitvalid_codec(sc))
489 return 1;
490
491 *val = bus_space_read_2(sc->sc_iot, sc->sc_ioh, AUVIA_CODEC_CTL);
492
493 return 0;
494 }
495
496
497 int
498 auvia_open(void *addr, int flags)
499 {
500 return 0;
501 }
502
503
504 void
505 auvia_close(void *addr)
506 {
507 }
508
509
510 int
511 auvia_query_encoding(void *addr, struct audio_encoding *fp)
512 {
513 switch (fp->index) {
514 case 0:
515 strcpy(fp->name, AudioEulinear);
516 fp->encoding = AUDIO_ENCODING_ULINEAR;
517 fp->precision = 8;
518 fp->flags = 0;
519 return (0);
520 case 1:
521 strcpy(fp->name, AudioEmulaw);
522 fp->encoding = AUDIO_ENCODING_ULAW;
523 fp->precision = 8;
524 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
525 return (0);
526 case 2:
527 strcpy(fp->name, AudioEalaw);
528 fp->encoding = AUDIO_ENCODING_ALAW;
529 fp->precision = 8;
530 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
531 return (0);
532 case 3:
533 strcpy(fp->name, AudioEslinear);
534 fp->encoding = AUDIO_ENCODING_SLINEAR;
535 fp->precision = 8;
536 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
537 return (0);
538 case 4:
539 strcpy(fp->name, AudioEslinear_le);
540 fp->encoding = AUDIO_ENCODING_SLINEAR_LE;
541 fp->precision = 16;
542 fp->flags = 0;
543 return (0);
544 case 5:
545 strcpy(fp->name, AudioEulinear_le);
546 fp->encoding = AUDIO_ENCODING_ULINEAR_LE;
547 fp->precision = 16;
548 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
549 return (0);
550 case 6:
551 strcpy(fp->name, AudioEslinear_be);
552 fp->encoding = AUDIO_ENCODING_SLINEAR_BE;
553 fp->precision = 16;
554 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
555 return (0);
556 case 7:
557 strcpy(fp->name, AudioEulinear_be);
558 fp->encoding = AUDIO_ENCODING_ULINEAR_BE;
559 fp->precision = 16;
560 fp->flags = AUDIO_ENCODINGFLAG_EMULATED;
561 return (0);
562 default:
563 return (EINVAL);
564 }
565 }
566
567 void
568 auvia_set_params_sub(struct auvia_softc *sc, struct auvia_softc_chan *ch,
569 struct audio_params *p)
570 {
571 u_int32_t v;
572 u_int16_t regval;
573
574 if (!(sc->sc_flags & AUVIA_FLAGS_VT8233)) {
575 regval = (p->channels == 2 ? AUVIA_RPMODE_STEREO : 0)
576 | (p->precision * p->factor == 16 ?
577 AUVIA_RPMODE_16BIT : 0)
578 | AUVIA_RPMODE_INTR_FLAG | AUVIA_RPMODE_INTR_EOL
579 | AUVIA_RPMODE_AUTOSTART;
580 ch->sc_reg = regval;
581 } else if (ch->sc_base != VIA8233_MP_BASE) {
582 v = CH_READ4(sc, ch, VIA8233_RP_RATEFMT);
583 v &= ~(VIA8233_RATEFMT_48K | VIA8233_RATEFMT_STEREO
584 | VIA8233_RATEFMT_16BIT);
585
586 v |= VIA8233_RATEFMT_48K * (p->sample_rate / 20)
587 / (48000 / 20);
588 if (p->channels == 2)
589 v |= VIA8233_RATEFMT_STEREO;
590 if (p->precision == 16)
591 v |= VIA8233_RATEFMT_16BIT;
592
593 CH_WRITE4(sc, ch, VIA8233_RP_RATEFMT, v);
594 } else {
595 static const u_int32_t slottab[7] =
596 { 0, 0xff000011, 0xff000021, 0,
597 0xff004321, 0, 0xff436521};
598
599 regval = (p->hw_precision == 16
600 ? VIA8233_MP_FORMAT_16BIT : VIA8233_MP_FORMAT_8BIT)
601 | (p->hw_channels << 4);
602 CH_WRITE1(sc, ch, VIA8233_OFF_MP_FORMAT, regval);
603 CH_WRITE4(sc, ch, VIA8233_OFF_MP_STOP, slottab[p->hw_channels]);
604 }
605 }
606
607 int
608 auvia_set_params(void *addr, int setmode, int usemode,
609 struct audio_params *play, struct audio_params *rec)
610 {
611 struct auvia_softc *sc = addr;
612 struct auvia_softc_chan *ch;
613 struct audio_params *p;
614 struct ac97_codec_if* codec;
615 int reg, mode;
616 u_int16_t ext_id;
617
618 codec = sc->codec_if;
619 /* for mode in (RECORD, PLAY) */
620 for (mode = AUMODE_RECORD; mode != -1;
621 mode = mode == AUMODE_RECORD ? AUMODE_PLAY : -1) {
622 if ((setmode & mode) == 0)
623 continue;
624
625 if (mode == AUMODE_PLAY ) {
626 p = play;
627 ch = &sc->sc_play;
628 reg = AC97_REG_PCM_FRONT_DAC_RATE;
629 } else {
630 p = rec;
631 ch = &sc->sc_record;
632 reg = AC97_REG_PCM_LR_ADC_RATE;
633 }
634
635 if (ch->sc_base == VIA8233_MP_BASE && mode == AUMODE_PLAY) {
636 ext_id = codec->vtbl->get_extcaps(codec);
637 if (p->channels == 1) {
638 /* ok */
639 } else if (p->channels == 2) {
640 /* ok */
641 } else if (p->channels == 4
642 && ext_id & AC97_EXT_AUDIO_SDAC) {
643 /* ok */
644 #define BITS_6CH (AC97_EXT_AUDIO_SDAC | AC97_EXT_AUDIO_CDAC | AC97_EXT_AUDIO_LDAC)
645 } else if (p->channels == 6
646 && (ext_id & BITS_6CH) == BITS_6CH) {
647 /* ok */
648 } else {
649 return (EINVAL);
650 }
651 } else {
652 if (p->channels != 1 && p->channels != 2)
653 return (EINVAL);
654 }
655 if (p->sample_rate < 4000 || p->sample_rate > 48000 ||
656 (p->precision != 8 && p->precision != 16))
657 return (EINVAL);
658
659 if (IS_FIXED_RATE(codec)) {
660 /* Enable aurateconv */
661 p->hw_sample_rate = AC97_SINGLE_RATE;
662 } else {
663 if (codec->vtbl->set_rate(codec, reg, &p->sample_rate))
664 return (EINVAL);
665 reg = AC97_REG_PCM_SURR_DAC_RATE;
666 if (p->channels >= 4
667 && codec->vtbl->set_rate(codec, reg,
668 &p->sample_rate))
669 return (EINVAL);
670 reg = AC97_REG_PCM_LFE_DAC_RATE;
671 if (p->channels == 6
672 && codec->vtbl->set_rate(codec, reg,
673 &p->sample_rate))
674 return (EINVAL);
675 }
676
677 p->factor = 1;
678 p->sw_code = 0;
679 switch (p->encoding) {
680 case AUDIO_ENCODING_SLINEAR_BE:
681 if (p->precision == 16) {
682 p->sw_code = swap_bytes;
683 p->hw_encoding = AUDIO_ENCODING_SLINEAR_LE;
684 } else {
685 p->sw_code = change_sign8;
686 p->hw_encoding = AUDIO_ENCODING_ULINEAR;
687 }
688 break;
689 case AUDIO_ENCODING_SLINEAR_LE:
690 if (p->precision != 16) {
691 p->sw_code = change_sign8;
692 p->hw_encoding = AUDIO_ENCODING_ULINEAR;
693 }
694 break;
695 case AUDIO_ENCODING_ULINEAR_BE:
696 if (p->precision == 16) {
697 if (mode == AUMODE_PLAY)
698 p->sw_code = swap_bytes_change_sign16_le;
699 else
700 p->sw_code = change_sign16_swap_bytes_le;
701 p->hw_encoding = AUDIO_ENCODING_SLINEAR_LE;
702 }
703 break;
704 case AUDIO_ENCODING_ULINEAR_LE:
705 if (p->precision == 16) {
706 p->sw_code = change_sign16_le;
707 p->hw_encoding = AUDIO_ENCODING_SLINEAR_LE;
708 }
709 break;
710 case AUDIO_ENCODING_ULAW:
711 if (p->precision != 8)
712 return (EINVAL);
713 if (mode == AUMODE_PLAY) {
714 p->factor = 2;
715 p->sw_code = mulaw_to_slinear16_le;
716 p->hw_encoding = AUDIO_ENCODING_SLINEAR_LE;
717 p->hw_precision = 16;
718 } else if (!IS_FIXED_RATE(codec)) {
719 p->sw_code = ulinear8_to_mulaw;
720 p->hw_encoding = AUDIO_ENCODING_ULINEAR;
721 } else {
722 /* aurateconv supports no 8 bit PCM */
723 p->factor = 2;
724 p->sw_code = slinear16_to_mulaw_le;
725 p->hw_encoding = AUDIO_ENCODING_SLINEAR_LE;
726 p->hw_precision = 16;
727 }
728 break;
729 case AUDIO_ENCODING_ALAW:
730 if (p->precision != 8)
731 return (EINVAL);
732 if (mode == AUMODE_PLAY) {
733 p->factor = 2;
734 p->sw_code = alaw_to_slinear16_le;
735 p->hw_encoding = AUDIO_ENCODING_SLINEAR_LE;
736 p->hw_precision = 16;
737 } else if (!IS_FIXED_RATE(codec)) {
738 p->sw_code = ulinear8_to_alaw;
739 p->hw_encoding = AUDIO_ENCODING_ULINEAR;
740 } else {
741 /* aurateconv supports no 8 bit PCM */
742 p->factor = 2;
743 p->sw_code = slinear16_to_alaw_le;
744 p->hw_encoding = AUDIO_ENCODING_SLINEAR_LE;
745 p->hw_precision = 16;
746 }
747 break;
748 default:
749 return (EINVAL);
750 }
751 auvia_set_params_sub(sc, ch, p);
752 }
753
754 return 0;
755 }
756
757
758 int
759 auvia_round_blocksize(void *addr, int blk)
760 {
761 struct auvia_softc *sc = addr;
762
763 /* XXX VT823x might have the limitation of dma_ops size */
764 if (sc->sc_flags & AUVIA_FLAGS_VT8233 && blk < 288)
765 blk = 288;
766
767 return (blk & -32);
768 }
769
770
771 int
772 auvia_halt_output(void *addr)
773 {
774 struct auvia_softc *sc = addr;
775 struct auvia_softc_chan *ch = &(sc->sc_play);
776
777 CH_WRITE1(sc, ch, AUVIA_RP_CONTROL, AUVIA_RPCTRL_TERMINATE);
778 ch->sc_intr = NULL;
779 return 0;
780 }
781
782
783 int
784 auvia_halt_input(void *addr)
785 {
786 struct auvia_softc *sc = addr;
787 struct auvia_softc_chan *ch = &(sc->sc_record);
788
789 CH_WRITE1(sc, ch, AUVIA_RP_CONTROL, AUVIA_RPCTRL_TERMINATE);
790 ch->sc_intr = NULL;
791 return 0;
792 }
793
794
795 int
796 auvia_getdev(void *addr, struct audio_device *retp)
797 {
798 struct auvia_softc *sc = addr;
799
800 if (retp) {
801 if (sc->sc_flags & AUVIA_FLAGS_VT8233) {
802 strncpy(retp->name, "VIA VT823x",
803 sizeof(retp->name));
804 } else {
805 strncpy(retp->name, "VIA VT82C686A",
806 sizeof(retp->name));
807 }
808 strncpy(retp->version, sc->sc_revision, sizeof(retp->version));
809 strncpy(retp->config, "auvia", sizeof(retp->config));
810 }
811
812 return 0;
813 }
814
815
816 int
817 auvia_set_port(void *addr, mixer_ctrl_t *cp)
818 {
819 struct auvia_softc *sc = addr;
820
821 return (sc->codec_if->vtbl->mixer_set_port(sc->codec_if, cp));
822 }
823
824
825 int
826 auvia_get_port(void *addr, mixer_ctrl_t *cp)
827 {
828 struct auvia_softc *sc = addr;
829
830 return (sc->codec_if->vtbl->mixer_get_port(sc->codec_if, cp));
831 }
832
833
834 int
835 auvia_query_devinfo(void *addr, mixer_devinfo_t *dip)
836 {
837 struct auvia_softc *sc = addr;
838
839 return (sc->codec_if->vtbl->query_devinfo(sc->codec_if, dip));
840 }
841
842
843 void *
844 auvia_malloc(void *addr, int direction, size_t size, struct malloc_type * pool,
845 int flags)
846 {
847 struct auvia_softc *sc = addr;
848 struct auvia_dma *p;
849 int error;
850 int rseg;
851
852 p = malloc(sizeof(*p), pool, flags);
853 if (!p)
854 return 0;
855
856 p->size = size;
857 if ((error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &p->seg,
858 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
859 printf("%s: unable to allocate DMA, error = %d\n",
860 sc->sc_dev.dv_xname, error);
861 goto fail_alloc;
862 }
863
864 if ((error = bus_dmamem_map(sc->sc_dmat, &p->seg, rseg, size, &p->addr,
865 BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
866 printf("%s: unable to map DMA, error = %d\n",
867 sc->sc_dev.dv_xname, error);
868 goto fail_map;
869 }
870
871 if ((error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
872 BUS_DMA_NOWAIT, &p->map)) != 0) {
873 printf("%s: unable to create DMA map, error = %d\n",
874 sc->sc_dev.dv_xname, error);
875 goto fail_create;
876 }
877
878 if ((error = bus_dmamap_load(sc->sc_dmat, p->map, p->addr, size, NULL,
879 BUS_DMA_NOWAIT)) != 0) {
880 printf("%s: unable to load DMA map, error = %d\n",
881 sc->sc_dev.dv_xname, error);
882 goto fail_load;
883 }
884
885 p->next = sc->sc_dmas;
886 sc->sc_dmas = p;
887
888 return p->addr;
889
890
891 fail_load:
892 bus_dmamap_destroy(sc->sc_dmat, p->map);
893 fail_create:
894 bus_dmamem_unmap(sc->sc_dmat, p->addr, size);
895 fail_map:
896 bus_dmamem_free(sc->sc_dmat, &p->seg, 1);
897 fail_alloc:
898 free(p, pool);
899 return 0;
900 }
901
902
903 void
904 auvia_free(void *addr, void *ptr, struct malloc_type *pool)
905 {
906 struct auvia_softc *sc = addr;
907 struct auvia_dma **pp, *p;
908
909 for (pp = &(sc->sc_dmas); (p = *pp) != NULL; pp = &p->next)
910 if (p->addr == ptr) {
911 bus_dmamap_unload(sc->sc_dmat, p->map);
912 bus_dmamap_destroy(sc->sc_dmat, p->map);
913 bus_dmamem_unmap(sc->sc_dmat, p->addr, p->size);
914 bus_dmamem_free(sc->sc_dmat, &p->seg, 1);
915
916 *pp = p->next;
917 free(p, pool);
918 return;
919 }
920
921 panic("auvia_free: trying to free unallocated memory");
922 }
923
924
925 size_t
926 auvia_round_buffersize(void *addr, int direction, size_t size)
927 {
928 return size;
929 }
930
931
932 paddr_t
933 auvia_mappage(void *addr, void *mem, off_t off, int prot)
934 {
935 struct auvia_softc *sc = addr;
936 struct auvia_dma *p;
937
938 if (off < 0)
939 return -1;
940
941 for (p = sc->sc_dmas; p && p->addr != mem; p = p->next)
942 ;
943
944 if (!p)
945 return -1;
946
947 return bus_dmamem_mmap(sc->sc_dmat, &p->seg, 1, off, prot,
948 BUS_DMA_WAITOK);
949 }
950
951
952 int
953 auvia_get_props(void *addr)
954 {
955 struct auvia_softc *sc = addr;
956 int props;
957
958 props = AUDIO_PROP_INDEPENDENT | AUDIO_PROP_FULLDUPLEX;
959 /*
960 * Even if the codec is fixed-rate, set_param() succeeds for any sample
961 * rate because of aurateconv. Applications can't know what rate the
962 * device can process in the case of mmap().
963 */
964 if (!IS_FIXED_RATE(sc->codec_if))
965 props |= AUDIO_PROP_MMAP;
966 return props;
967 }
968
969
970 int
971 auvia_build_dma_ops(struct auvia_softc *sc, struct auvia_softc_chan *ch,
972 struct auvia_dma *p, void *start, void *end, int blksize)
973 {
974 struct auvia_dma_op *op;
975 struct auvia_dma *dp;
976 bus_addr_t s;
977 size_t l;
978 int segs;
979
980 s = p->map->dm_segs[0].ds_addr;
981 l = ((char *)end - (char *)start);
982 segs = (l + blksize - 1) / blksize;
983
984 if (segs > (ch->sc_dma_op_count)) {
985 /* if old list was too small, free it */
986 if (ch->sc_dma_ops) {
987 auvia_free(sc, ch->sc_dma_ops, M_DEVBUF);
988 }
989
990 ch->sc_dma_ops = auvia_malloc(sc, 0,
991 sizeof(struct auvia_dma_op) * segs, M_DEVBUF, M_WAITOK);
992
993 if (ch->sc_dma_ops == NULL) {
994 printf("%s: couldn't build dmaops\n", sc->sc_dev.dv_xname);
995 return 1;
996 }
997
998 for (dp = sc->sc_dmas;
999 dp && dp->addr != (void *)(ch->sc_dma_ops);
1000 dp = dp->next)
1001 ;
1002
1003 if (!dp)
1004 panic("%s: build_dma_ops: where'd my memory go??? "
1005 "address (%p)\n", sc->sc_dev.dv_xname,
1006 ch->sc_dma_ops);
1007
1008 ch->sc_dma_op_count = segs;
1009 ch->sc_dma_ops_dma = dp;
1010 }
1011
1012 dp = ch->sc_dma_ops_dma;
1013 op = ch->sc_dma_ops;
1014
1015 while (l) {
1016 op->ptr = s;
1017 l = l - blksize;
1018 if (!l) {
1019 /* if last block */
1020 op->flags = AUVIA_DMAOP_EOL | blksize;
1021 } else {
1022 op->flags = AUVIA_DMAOP_FLAG | blksize;
1023 }
1024 s += blksize;
1025 op++;
1026 }
1027
1028 return 0;
1029 }
1030
1031
1032 int
1033 auvia_trigger_output(void *addr, void *start, void *end,
1034 int blksize, void (*intr)(void *), void *arg,
1035 struct audio_params *param)
1036 {
1037 struct auvia_softc *sc = addr;
1038 struct auvia_softc_chan *ch = &(sc->sc_play);
1039 struct auvia_dma *p;
1040
1041 for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
1042 ;
1043
1044 if (!p)
1045 panic("auvia_trigger_output: request with bad start "
1046 "address (%p)", start);
1047
1048 if (auvia_build_dma_ops(sc, ch, p, start, end, blksize)) {
1049 return 1;
1050 }
1051
1052 ch->sc_intr = intr;
1053 ch->sc_arg = arg;
1054
1055 CH_WRITE4(sc, ch, AUVIA_RP_DMAOPS_BASE,
1056 ch->sc_dma_ops_dma->map->dm_segs[0].ds_addr);
1057
1058 if (sc->sc_flags & AUVIA_FLAGS_VT8233) {
1059 if (ch->sc_base != VIA8233_MP_BASE) {
1060 CH_WRITE1(sc, ch, VIA8233_RP_DXS_LVOL, 0);
1061 CH_WRITE1(sc, ch, VIA8233_RP_DXS_RVOL, 0);
1062 }
1063 CH_WRITE1(sc, ch, AUVIA_RP_CONTROL,
1064 AUVIA_RPCTRL_START | AUVIA_RPCTRL_AUTOSTART |
1065 AUVIA_RPCTRL_STOP | AUVIA_RPCTRL_EOL | AUVIA_RPCTRL_FLAG);
1066 } else {
1067 CH_WRITE1(sc, ch, AUVIA_RP_MODE, ch->sc_reg);
1068 CH_WRITE1(sc, ch, AUVIA_RP_CONTROL, AUVIA_RPCTRL_START);
1069 }
1070
1071 return 0;
1072 }
1073
1074
1075 int
1076 auvia_trigger_input(void *addr, void *start, void *end,
1077 int blksize, void (*intr)(void *), void *arg,
1078 struct audio_params *param)
1079 {
1080 struct auvia_softc *sc = addr;
1081 struct auvia_softc_chan *ch = &(sc->sc_record);
1082 struct auvia_dma *p;
1083
1084 for (p = sc->sc_dmas; p && p->addr != start; p = p->next)
1085 ;
1086
1087 if (!p)
1088 panic("auvia_trigger_input: request with bad start "
1089 "address (%p)", start);
1090
1091 if (auvia_build_dma_ops(sc, ch, p, start, end, blksize)) {
1092 return 1;
1093 }
1094
1095 ch->sc_intr = intr;
1096 ch->sc_arg = arg;
1097
1098 CH_WRITE4(sc, ch, AUVIA_RP_DMAOPS_BASE,
1099 ch->sc_dma_ops_dma->map->dm_segs[0].ds_addr);
1100
1101 if (sc->sc_flags & AUVIA_FLAGS_VT8233) {
1102 CH_WRITE1(sc, ch, VIA8233_RP_DXS_LVOL, 0);
1103 CH_WRITE1(sc, ch, VIA8233_RP_DXS_RVOL, 0);
1104 CH_WRITE1(sc, ch, AUVIA_RP_CONTROL,
1105 AUVIA_RPCTRL_START | AUVIA_RPCTRL_AUTOSTART |
1106 AUVIA_RPCTRL_STOP | AUVIA_RPCTRL_EOL | AUVIA_RPCTRL_FLAG);
1107 } else {
1108 CH_WRITE1(sc, ch, AUVIA_RP_MODE, ch->sc_reg);
1109 CH_WRITE1(sc, ch, AUVIA_RP_CONTROL, AUVIA_RPCTRL_START);
1110 }
1111
1112 return 0;
1113 }
1114
1115
1116 int
1117 auvia_intr(void *arg)
1118 {
1119 struct auvia_softc *sc = arg;
1120 struct auvia_softc_chan *ch;
1121 u_int8_t r;
1122 int rval;
1123
1124 rval = 0;
1125
1126 ch = &sc->sc_record;
1127 r = CH_READ1(sc, ch, AUVIA_RP_STAT);
1128 if (r & AUVIA_RPSTAT_INTR) {
1129 if (sc->sc_record.sc_intr)
1130 sc->sc_record.sc_intr(sc->sc_record.sc_arg);
1131
1132 /* clear interrupts */
1133 CH_WRITE1(sc, ch, AUVIA_RP_STAT, AUVIA_RPSTAT_INTR);
1134 rval = 1;
1135 }
1136
1137 ch = &sc->sc_play;
1138 r = CH_READ1(sc, ch, AUVIA_RP_STAT);
1139 if (r & AUVIA_RPSTAT_INTR) {
1140 if (sc->sc_play.sc_intr)
1141 sc->sc_play.sc_intr(sc->sc_play.sc_arg);
1142
1143 /* clear interrupts */
1144 CH_WRITE1(sc, ch, AUVIA_RP_STAT, AUVIA_RPSTAT_INTR);
1145 rval = 1;
1146 }
1147
1148 return rval;
1149 }
1150
1151 void
1152 auvia_powerhook(int why, void *addr)
1153 {
1154 struct auvia_softc *sc = (struct auvia_softc *)addr;
1155
1156 switch (why) {
1157 case PWR_SUSPEND:
1158 case PWR_STANDBY:
1159 /* Power down */
1160 sc->sc_suspend = why;
1161 break;
1162
1163 case PWR_RESUME:
1164 /* Wake up */
1165 if (sc->sc_suspend == PWR_RESUME) {
1166 printf("%s: resume without suspend.\n",
1167 sc->sc_dev.dv_xname);
1168 sc->sc_suspend = why;
1169 return;
1170 }
1171 sc->sc_suspend = why;
1172 auvia_reset_codec(sc);
1173 DELAY(1000);
1174 (sc->codec_if->vtbl->restore_ports)(sc->codec_if);
1175 break;
1176
1177 case PWR_SOFTSUSPEND:
1178 case PWR_SOFTSTANDBY:
1179 case PWR_SOFTRESUME:
1180 break;
1181 }
1182 }
1183