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