hdaudio.c revision 1.8 1 /* $NetBSD: hdaudio.c,v 1.8 2017/11/24 17:51:10 jmcneill Exp $ */
2
3 /*
4 * Copyright (c) 2009 Precedence Technologies Ltd <support (at) precedence.co.uk>
5 * Copyright (c) 2009 Jared D. McNeill <jmcneill (at) invisible.ca>
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Precedence Technologies Ltd
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: hdaudio.c,v 1.8 2017/11/24 17:51:10 jmcneill Exp $");
34
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/device.h>
39 #include <sys/conf.h>
40 #include <sys/bus.h>
41 #include <sys/kmem.h>
42 #include <sys/module.h>
43
44 #include "hdaudiovar.h"
45 #include "hdaudioreg.h"
46 #include "hdaudioio.h"
47 #include "hdaudio_verbose.h"
48
49 /* #define HDAUDIO_DEBUG */
50
51 #define HDAUDIO_RESET_TIMEOUT 5000
52 #define HDAUDIO_CORB_TIMEOUT 1000
53 #define HDAUDIO_RIRB_TIMEOUT 5000
54
55 #define HDAUDIO_CODEC_DELAY 1000 /* spec calls for 250 */
56
57 dev_type_open(hdaudioopen);
58 dev_type_close(hdaudioclose);
59 dev_type_ioctl(hdaudioioctl);
60
61 const struct cdevsw hdaudio_cdevsw = {
62 .d_open = hdaudioopen,
63 .d_close = hdaudioclose,
64 .d_read = noread,
65 .d_write = nowrite,
66 .d_ioctl = hdaudioioctl,
67 .d_stop = nostop,
68 .d_tty = notty,
69 .d_poll = nopoll,
70 .d_mmap = nommap,
71 .d_kqfilter = nokqfilter,
72 .d_discard = nodiscard,
73 .d_flag = D_OTHER
74 };
75
76 extern struct cfdriver hdaudio_cd;
77
78 #define HDAUDIOUNIT(x) minor((x))
79
80 static void
81 hdaudio_stream_init(struct hdaudio_softc *sc, int nis, int nos, int nbidir)
82 {
83 int i, cnt = 0;
84
85 for (i = 0; i < nis && cnt < HDAUDIO_MAX_STREAMS; i++) {
86 sc->sc_stream[cnt].st_host = sc;
87 sc->sc_stream[cnt].st_enable = true;
88 sc->sc_stream[cnt].st_shift = cnt;
89 sc->sc_stream[cnt++].st_type = HDAUDIO_STREAM_ISS;
90 }
91 for (i = 0; i < nos && cnt < HDAUDIO_MAX_STREAMS; i++) {
92 sc->sc_stream[cnt].st_host = sc;
93 sc->sc_stream[cnt].st_enable = true;
94 sc->sc_stream[cnt].st_shift = cnt;
95 sc->sc_stream[cnt++].st_type = HDAUDIO_STREAM_OSS;
96 }
97 for (i = 0; i < nbidir && cnt < HDAUDIO_MAX_STREAMS; i++) {
98 sc->sc_stream[cnt].st_host = sc;
99 sc->sc_stream[cnt].st_enable = true;
100 sc->sc_stream[cnt].st_shift = cnt;
101 sc->sc_stream[cnt++].st_type = HDAUDIO_STREAM_BSS;
102 }
103
104 for (i = 0; i < cnt; i++)
105 hdaudio_stream_stop(&sc->sc_stream[i]);
106
107 sc->sc_stream_mask = 0;
108 }
109
110 static void
111 hdaudio_codec_init(struct hdaudio_softc *sc)
112 {
113 int i;
114
115 for (i = 0; i < HDAUDIO_MAX_CODECS; i++) {
116 sc->sc_codec[i].co_addr = i;
117 sc->sc_codec[i].co_host = sc;
118 }
119 }
120
121 static void
122 hdaudio_init(struct hdaudio_softc *sc)
123 {
124 uint16_t gcap;
125 int nos, nis, nbidir;
126 #if defined(HDAUDIO_DEBUG)
127 uint8_t vmin, vmaj;
128 int nsdo, addr64;
129 #endif
130
131 #if defined(HDAUDIO_DEBUG)
132 vmaj = hda_read1(sc, HDAUDIO_MMIO_VMAJ);
133 vmin = hda_read1(sc, HDAUDIO_MMIO_VMIN);
134
135 hda_print(sc, "High Definition Audio version %d.%d\n", vmaj, vmin);
136 #endif
137
138 gcap = hda_read2(sc, HDAUDIO_MMIO_GCAP);
139 nis = HDAUDIO_GCAP_ISS(gcap);
140 nos = HDAUDIO_GCAP_OSS(gcap);
141 nbidir = HDAUDIO_GCAP_BSS(gcap);
142
143 /* Initialize codecs and streams */
144 hdaudio_codec_init(sc);
145 hdaudio_stream_init(sc, nis, nos, nbidir);
146
147 #if defined(HDAUDIO_DEBUG)
148 nsdo = HDAUDIO_GCAP_NSDO(gcap);
149 addr64 = HDAUDIO_GCAP_64OK(gcap);
150
151 hda_print(sc, "OSS %d ISS %d BSS %d SDO %d%s\n",
152 nos, nis, nbidir, nsdo, addr64 ? " 64-bit" : "");
153 #endif
154 }
155
156 static int
157 hdaudio_codec_probe(struct hdaudio_softc *sc)
158 {
159 uint16_t statests;
160 int codecid;
161
162 statests = hda_read2(sc, HDAUDIO_MMIO_STATESTS);
163 for (codecid = 0; codecid < HDAUDIO_MAX_CODECS; codecid++)
164 if (statests & (1 << codecid))
165 sc->sc_codec[codecid].co_valid = true;
166 hda_write2(sc, HDAUDIO_MMIO_STATESTS, statests);
167
168 return statests;
169 }
170
171 int
172 hdaudio_dma_alloc(struct hdaudio_softc *sc, struct hdaudio_dma *dma,
173 int flags)
174 {
175 int err;
176
177 KASSERT(dma->dma_size > 0);
178
179 err = bus_dmamem_alloc(sc->sc_dmat, dma->dma_size, 128, 0,
180 dma->dma_segs, sizeof(dma->dma_segs) / sizeof(dma->dma_segs[0]),
181 &dma->dma_nsegs, BUS_DMA_WAITOK);
182 if (err)
183 return err;
184 err = bus_dmamem_map(sc->sc_dmat, dma->dma_segs, dma->dma_nsegs,
185 dma->dma_size, &dma->dma_addr, BUS_DMA_WAITOK | flags);
186 if (err)
187 goto free;
188 err = bus_dmamap_create(sc->sc_dmat, dma->dma_size, dma->dma_nsegs,
189 dma->dma_size, 0, BUS_DMA_WAITOK, &dma->dma_map);
190 if (err)
191 goto unmap;
192 err = bus_dmamap_load(sc->sc_dmat, dma->dma_map, dma->dma_addr,
193 dma->dma_size, NULL, BUS_DMA_WAITOK | flags);
194 if (err)
195 goto destroy;
196
197 dma->dma_valid = true;
198 return 0;
199
200 destroy:
201 bus_dmamap_destroy(sc->sc_dmat, dma->dma_map);
202 unmap:
203 bus_dmamem_unmap(sc->sc_dmat, dma->dma_addr, dma->dma_size);
204 free:
205 bus_dmamem_free(sc->sc_dmat, dma->dma_segs, dma->dma_nsegs);
206
207 dma->dma_valid = false;
208 return err;
209 }
210
211 void
212 hdaudio_dma_free(struct hdaudio_softc *sc, struct hdaudio_dma *dma)
213 {
214 if (dma->dma_valid == false)
215 return;
216 bus_dmamap_unload(sc->sc_dmat, dma->dma_map);
217 bus_dmamap_destroy(sc->sc_dmat, dma->dma_map);
218 bus_dmamem_unmap(sc->sc_dmat, dma->dma_addr, dma->dma_size);
219 bus_dmamem_free(sc->sc_dmat, dma->dma_segs, dma->dma_nsegs);
220 dma->dma_valid = false;
221 }
222
223 static void
224 hdaudio_corb_enqueue(struct hdaudio_softc *sc, int addr, int nid,
225 uint32_t control, uint32_t param)
226 {
227 uint32_t *corb = DMA_KERNADDR(&sc->sc_corb);
228 uint32_t verb;
229 uint16_t corbrp;
230 int wp;
231
232 /* Build command */
233 verb = (addr << 28) | (nid << 20) | (control << 8) | param;
234
235 /* Fetch and update write pointer */
236 corbrp = hda_read2(sc, HDAUDIO_MMIO_CORBWP);
237 wp = (corbrp & 0xff) + 1;
238 if (wp >= (sc->sc_corb.dma_size / sizeof(*corb)))
239 wp = 0;
240
241 /* Enqueue command */
242 bus_dmamap_sync(sc->sc_dmat, sc->sc_corb.dma_map, 0,
243 sc->sc_corb.dma_size, BUS_DMASYNC_POSTWRITE);
244 corb[wp] = verb;
245 bus_dmamap_sync(sc->sc_dmat, sc->sc_corb.dma_map, 0,
246 sc->sc_corb.dma_size, BUS_DMASYNC_PREWRITE);
247
248 /* Commit updated write pointer */
249 hda_write2(sc, HDAUDIO_MMIO_CORBWP, wp);
250 }
251
252 static void
253 hdaudio_rirb_unsol(struct hdaudio_softc *sc, struct rirb_entry *entry)
254 {
255 struct hdaudio_codec *co;
256 struct hdaudio_function_group *fg;
257 uint8_t codecid = RIRB_CODEC_ID(entry);
258 unsigned int i;
259
260 if (codecid >= HDAUDIO_MAX_CODECS) {
261 hda_error(sc, "unsol: codec id 0x%02x out of range\n", codecid);
262 return;
263 }
264 co = &sc->sc_codec[codecid];
265 if (sc->sc_codec[codecid].co_valid == false) {
266 hda_error(sc, "unsol: codec id 0x%02x not valid\n", codecid);
267 return;
268 }
269
270 for (i = 0; i < co->co_nfg; i++) {
271 fg = &co->co_fg[i];
272 if (fg->fg_device && fg->fg_unsol)
273 fg->fg_unsol(fg->fg_device, entry->resp);
274 }
275 }
276
277 static uint32_t
278 hdaudio_rirb_dequeue(struct hdaudio_softc *sc, bool unsol)
279 {
280 uint16_t rirbwp;
281 uint64_t *rirb = DMA_KERNADDR(&sc->sc_rirb);
282 struct rirb_entry entry;
283 int retry;
284
285 for (;;) {
286 retry = HDAUDIO_RIRB_TIMEOUT;
287
288 rirbwp = hda_read2(sc, HDAUDIO_MMIO_RIRBWP);
289 while (--retry > 0 && (rirbwp & 0xff) == sc->sc_rirbrp) {
290 if (unsol) {
291 /* don't wait for more unsol events */
292 hda_trace(sc, "unsol: rirb empty\n");
293 return 0xffffffff;
294 }
295 hda_delay(10);
296 rirbwp = hda_read2(sc, HDAUDIO_MMIO_RIRBWP);
297 }
298 if (retry == 0) {
299 hda_error(sc, "RIRB timeout\n");
300 return 0xffffffff;
301 }
302
303 sc->sc_rirbrp++;
304 if (sc->sc_rirbrp >= (sc->sc_rirb.dma_size / sizeof(*rirb)))
305 sc->sc_rirbrp = 0;
306
307 bus_dmamap_sync(sc->sc_dmat, sc->sc_rirb.dma_map, 0,
308 sc->sc_rirb.dma_size, BUS_DMASYNC_POSTREAD);
309 entry = *(struct rirb_entry *)&rirb[sc->sc_rirbrp];
310 bus_dmamap_sync(sc->sc_dmat, sc->sc_rirb.dma_map, 0,
311 sc->sc_rirb.dma_size, BUS_DMASYNC_PREREAD);
312
313 hda_trace(sc, "%s: response %08X %08X\n",
314 unsol ? "unsol" : "cmd ",
315 entry.resp, entry.resp_ex);
316
317 if (RIRB_UNSOL(&entry)) {
318 hdaudio_rirb_unsol(sc, &entry);
319 continue;
320 }
321
322 return entry.resp;
323 }
324 }
325
326 uint32_t
327 hdaudio_command(struct hdaudio_codec *co, int nid, uint32_t control,
328 uint32_t param)
329 {
330 uint32_t result;
331 struct hdaudio_softc *sc = co->co_host;
332 mutex_enter(&sc->sc_corb_mtx);
333 result = hdaudio_command_unlocked(co, nid, control, param);
334 mutex_exit(&sc->sc_corb_mtx);
335 return result;
336 }
337
338 uint32_t
339 hdaudio_command_unlocked(struct hdaudio_codec *co, int nid, uint32_t control,
340 uint32_t param)
341 {
342 struct hdaudio_softc *sc = co->co_host;
343 uint32_t result;
344
345 hda_trace(sc, "cmd : request %08X %08X (%02X)\n",
346 control, param, nid);
347 hdaudio_corb_enqueue(sc, co->co_addr, nid, control, param);
348 result = hdaudio_rirb_dequeue(sc, false);
349
350 /* Clear response interrupt status */
351 hda_write1(sc, HDAUDIO_MMIO_RIRBSTS, hda_read1(sc, HDAUDIO_MMIO_RIRBSTS));
352
353 return result;
354 }
355
356 static int
357 hdaudio_corb_setsize(struct hdaudio_softc *sc)
358 {
359 uint8_t corbsize;
360 bus_size_t bufsize = 0;
361
362 /*
363 * The size of the CORB is programmable to 2, 16, or 256 entries
364 * by using the CORBSIZE register. Choose a size based on the
365 * controller capabilities, preferring a larger size when possible.
366 */
367 corbsize = hda_read1(sc, HDAUDIO_MMIO_CORBSIZE);
368 corbsize &= ~0x3;
369 if ((corbsize >> 4) & 0x4) {
370 corbsize |= 0x2;
371 bufsize = 1024;
372 } else if ((corbsize >> 4) & 0x2) {
373 corbsize |= 0x1;
374 bufsize = 64;
375 } else if ((corbsize >> 4) & 0x1) {
376 corbsize |= 0x0;
377 bufsize = 8;
378 } else {
379 hda_error(sc, "couldn't configure CORB size\n");
380 return ENXIO;
381 }
382
383 #if defined(HDAUDIO_DEBUG)
384 hda_print(sc, "using %d byte CORB (cap %X)\n",
385 (int)bufsize, corbsize >> 4);
386 #endif
387
388 sc->sc_corb.dma_size = bufsize;
389 sc->sc_corb.dma_sizereg = corbsize;
390
391 return 0;
392 }
393
394 static int
395 hdaudio_corb_config(struct hdaudio_softc *sc)
396 {
397 uint32_t corbubase, corblbase;
398 uint16_t corbrp;
399 int retry = HDAUDIO_CORB_TIMEOUT;
400
401 /* Program command buffer base address and size */
402 corblbase = (uint32_t)DMA_DMAADDR(&sc->sc_corb);
403 corbubase = (uint32_t)(((uint64_t)DMA_DMAADDR(&sc->sc_corb)) >> 32);
404 hda_write4(sc, HDAUDIO_MMIO_CORBLBASE, corblbase);
405 hda_write4(sc, HDAUDIO_MMIO_CORBUBASE, corbubase);
406 hda_write1(sc, HDAUDIO_MMIO_CORBSIZE, sc->sc_corb.dma_sizereg);
407
408 /* Clear the read and write pointers */
409 hda_write2(sc, HDAUDIO_MMIO_CORBRP, HDAUDIO_CORBRP_RP_RESET);
410 hda_write2(sc, HDAUDIO_MMIO_CORBRP, 0);
411 do {
412 hda_delay(10);
413 corbrp = hda_read2(sc, HDAUDIO_MMIO_CORBRP);
414 } while (--retry > 0 && (corbrp & HDAUDIO_CORBRP_RP_RESET) != 0);
415 if (retry == 0) {
416 hda_error(sc, "timeout resetting CORB\n");
417 return ETIME;
418 }
419 hda_write2(sc, HDAUDIO_MMIO_CORBWP, 0);
420
421 return 0;
422 }
423
424 static int
425 hdaudio_corb_stop(struct hdaudio_softc *sc)
426 {
427 uint8_t corbctl;
428 int retry = HDAUDIO_CORB_TIMEOUT;
429
430 /* Stop the CORB if necessary */
431 corbctl = hda_read1(sc, HDAUDIO_MMIO_CORBCTL);
432 if (corbctl & HDAUDIO_CORBCTL_RUN) {
433 corbctl &= ~HDAUDIO_CORBCTL_RUN;
434 hda_write1(sc, HDAUDIO_MMIO_CORBCTL, corbctl);
435 do {
436 hda_delay(10);
437 corbctl = hda_read1(sc, HDAUDIO_MMIO_CORBCTL);
438 } while (--retry > 0 && (corbctl & HDAUDIO_CORBCTL_RUN) != 0);
439 if (retry == 0) {
440 hda_error(sc, "timeout stopping CORB\n");
441 return ETIME;
442 }
443 }
444
445 return 0;
446 }
447
448 static int
449 hdaudio_corb_start(struct hdaudio_softc *sc)
450 {
451 uint8_t corbctl;
452 int retry = HDAUDIO_CORB_TIMEOUT;
453
454 /* Start the CORB if necessary */
455 corbctl = hda_read1(sc, HDAUDIO_MMIO_CORBCTL);
456 if ((corbctl & HDAUDIO_CORBCTL_RUN) == 0) {
457 corbctl |= HDAUDIO_CORBCTL_RUN;
458 hda_write1(sc, HDAUDIO_MMIO_CORBCTL, corbctl);
459 do {
460 hda_delay(10);
461 corbctl = hda_read1(sc, HDAUDIO_MMIO_CORBCTL);
462 } while (--retry > 0 && (corbctl & HDAUDIO_CORBCTL_RUN) == 0);
463 if (retry == 0) {
464 hda_error(sc, "timeout starting CORB\n");
465 return ETIME;
466 }
467 }
468
469 return 0;
470 }
471
472 static int
473 hdaudio_rirb_stop(struct hdaudio_softc *sc)
474 {
475 uint8_t rirbctl;
476 int retry = HDAUDIO_RIRB_TIMEOUT;
477
478 /* Stop the RIRB if necessary */
479 rirbctl = hda_read1(sc, HDAUDIO_MMIO_RIRBCTL);
480 if (rirbctl & (HDAUDIO_RIRBCTL_RUN|HDAUDIO_RIRBCTL_ROI_EN)) {
481 rirbctl &= ~HDAUDIO_RIRBCTL_RUN;
482 rirbctl &= ~HDAUDIO_RIRBCTL_ROI_EN;
483 hda_write1(sc, HDAUDIO_MMIO_RIRBCTL, rirbctl);
484 do {
485 hda_delay(10);
486 rirbctl = hda_read1(sc, HDAUDIO_MMIO_RIRBCTL);
487 } while (--retry > 0 && (rirbctl & HDAUDIO_RIRBCTL_RUN) != 0);
488 if (retry == 0) {
489 hda_error(sc, "timeout stopping RIRB\n");
490 return ETIME;
491 }
492 }
493
494 return 0;
495 }
496
497 static int
498 hdaudio_rirb_start(struct hdaudio_softc *sc)
499 {
500 uint8_t rirbctl;
501 int retry = HDAUDIO_RIRB_TIMEOUT;
502
503 /* Set the RIRB interrupt count */
504 hda_write2(sc, HDAUDIO_MMIO_RINTCNT, 1);
505
506 /* Start the RIRB */
507 rirbctl = hda_read1(sc, HDAUDIO_MMIO_RIRBCTL);
508 rirbctl |= HDAUDIO_RIRBCTL_RUN;
509 rirbctl |= HDAUDIO_RIRBCTL_INT_EN;
510 hda_write1(sc, HDAUDIO_MMIO_RIRBCTL, rirbctl);
511 do {
512 hda_delay(10);
513 rirbctl = hda_read1(sc, HDAUDIO_MMIO_RIRBCTL);
514 } while (--retry > 0 && (rirbctl & HDAUDIO_RIRBCTL_RUN) == 0);
515 if (retry == 0) {
516 hda_error(sc, "timeout starting RIRB\n");
517 return ETIME;
518 }
519
520 return 0;
521 }
522
523 static int
524 hdaudio_rirb_setsize(struct hdaudio_softc *sc)
525 {
526 uint8_t rirbsize;
527 bus_size_t bufsize = 0;
528
529 /*
530 * The size of the RIRB is programmable to 2, 16, or 256 entries
531 * by using the RIRBSIZE register. Choose a size based on the
532 * controller capabilities, preferring a larger size when possible.
533 */
534 rirbsize = hda_read1(sc, HDAUDIO_MMIO_RIRBSIZE);
535 rirbsize &= ~0x3;
536 if ((rirbsize >> 4) & 0x4) {
537 rirbsize |= 0x2;
538 bufsize = 2048;
539 } else if ((rirbsize >> 4) & 0x2) {
540 rirbsize |= 0x1;
541 bufsize = 128;
542 } else if ((rirbsize >> 4) & 0x1) {
543 rirbsize |= 0x0;
544 bufsize = 16;
545 } else {
546 hda_error(sc, "couldn't configure RIRB size\n");
547 return ENXIO;
548 }
549
550 #if defined(HDAUDIO_DEBUG)
551 hda_print(sc, "using %d byte RIRB (cap %X)\n",
552 (int)bufsize, rirbsize >> 4);
553 #endif
554
555 sc->sc_rirb.dma_size = bufsize;
556 sc->sc_rirb.dma_sizereg = rirbsize;
557
558 return 0;
559 }
560
561 static int
562 hdaudio_rirb_config(struct hdaudio_softc *sc)
563 {
564 uint32_t rirbubase, rirblbase;
565
566 /* Program command buffer base address and size */
567 rirblbase = (uint32_t)DMA_DMAADDR(&sc->sc_rirb);
568 rirbubase = (uint32_t)(((uint64_t)DMA_DMAADDR(&sc->sc_rirb)) >> 32);
569 hda_write4(sc, HDAUDIO_MMIO_RIRBLBASE, rirblbase);
570 hda_write4(sc, HDAUDIO_MMIO_RIRBUBASE, rirbubase);
571 hda_write1(sc, HDAUDIO_MMIO_RIRBSIZE, sc->sc_rirb.dma_sizereg);
572
573 /* Clear the write pointer */
574 hda_write2(sc, HDAUDIO_MMIO_RIRBWP, HDAUDIO_RIRBWP_WP_RESET);
575 sc->sc_rirbrp = 0;
576
577 return 0;
578 }
579
580 static int
581 hdaudio_reset(struct hdaudio_softc *sc)
582 {
583 int retry = HDAUDIO_RESET_TIMEOUT;
584 uint32_t gctl;
585 int err;
586
587 if ((err = hdaudio_rirb_stop(sc)) != 0) {
588 hda_error(sc, "couldn't reset because RIRB is busy\n");
589 return err;
590 }
591 if ((err = hdaudio_corb_stop(sc)) != 0) {
592 hda_error(sc, "couldn't reset because CORB is busy\n");
593 return err;
594 }
595
596 /* Disable wake events */
597 hda_write2(sc, HDAUDIO_MMIO_WAKEEN, 0);
598
599 /* Disable interrupts */
600 hda_write4(sc, HDAUDIO_MMIO_INTCTL, 0);
601
602 /* Clear state change status register */
603 hda_write2(sc, HDAUDIO_MMIO_STATESTS,
604 hda_read2(sc, HDAUDIO_MMIO_STATESTS));
605 hda_write1(sc, HDAUDIO_MMIO_RIRBSTS,
606 hda_read1(sc, HDAUDIO_MMIO_RIRBSTS));
607
608 /* Put the controller into reset state */
609 gctl = hda_read4(sc, HDAUDIO_MMIO_GCTL);
610 gctl &= ~HDAUDIO_GCTL_CRST;
611 hda_write4(sc, HDAUDIO_MMIO_GCTL, gctl);
612 do {
613 hda_delay(10);
614 gctl = hda_read4(sc, HDAUDIO_MMIO_GCTL);
615 } while (--retry > 0 && (gctl & HDAUDIO_GCTL_CRST) != 0);
616 if (retry == 0) {
617 hda_error(sc, "timeout entering reset state\n");
618 return ETIME;
619 }
620
621 hda_delay(1000);
622
623 /* Now the controller is in reset state, so bring it out */
624 retry = HDAUDIO_RESET_TIMEOUT;
625 hda_write4(sc, HDAUDIO_MMIO_GCTL, gctl | HDAUDIO_GCTL_CRST);
626 do {
627 hda_delay(10);
628 gctl = hda_read4(sc, HDAUDIO_MMIO_GCTL);
629 } while (--retry > 0 && (gctl & HDAUDIO_GCTL_CRST) == 0);
630 if (retry == 0) {
631 hda_error(sc, "timeout leaving reset state\n");
632 return ETIME;
633 }
634
635 hda_delay(2000);
636
637 /* Accept unsolicited responses */
638 hda_write4(sc, HDAUDIO_MMIO_GCTL, gctl | HDAUDIO_GCTL_UNSOL_EN);
639
640 return 0;
641 }
642
643 static void
644 hdaudio_intr_enable(struct hdaudio_softc *sc)
645 {
646 hda_write4(sc, HDAUDIO_MMIO_INTSTS,
647 hda_read4(sc, HDAUDIO_MMIO_INTSTS));
648 hda_write4(sc, HDAUDIO_MMIO_INTCTL,
649 HDAUDIO_INTCTL_GIE | HDAUDIO_INTCTL_CIE);
650 }
651
652 static void
653 hdaudio_intr_disable(struct hdaudio_softc *sc)
654 {
655 hda_write4(sc, HDAUDIO_MMIO_INTCTL, 0);
656 }
657
658 static int
659 hdaudio_config_print(void *opaque, const char *pnp)
660 {
661 prop_dictionary_t dict = opaque;
662 uint8_t fgtype, nid;
663 uint16_t vendor, product;
664 const char *type = "unknown";
665
666 prop_dictionary_get_uint8(dict, "function-group-type", &fgtype);
667 prop_dictionary_get_uint8(dict, "node-id", &nid);
668 prop_dictionary_get_uint16(dict, "vendor-id", &vendor);
669 prop_dictionary_get_uint16(dict, "product-id", &product);
670 if (pnp) {
671 if (fgtype == HDAUDIO_GROUP_TYPE_AFG)
672 type = "hdafg";
673 else if (fgtype == HDAUDIO_GROUP_TYPE_VSM_FG)
674 type = "hdvsmfg";
675
676 aprint_normal("%s at %s", type, pnp);
677 }
678 aprint_debug(" vendor 0x%04X product 0x%04X nid 0x%02X",
679 vendor, product, nid);
680
681 return UNCONF;
682 }
683
684 static void
685 hdaudio_attach_fg(struct hdaudio_function_group *fg, prop_array_t config)
686 {
687 struct hdaudio_codec *co = fg->fg_codec;
688 struct hdaudio_softc *sc = co->co_host;
689 prop_dictionary_t args = prop_dictionary_create();
690 uint64_t fgptr = (vaddr_t)fg;
691 int locs[1];
692
693 prop_dictionary_set_uint8(args, "function-group-type", fg->fg_type);
694 prop_dictionary_set_uint64(args, "function-group", fgptr);
695 prop_dictionary_set_uint8(args, "node-id", fg->fg_nid);
696 prop_dictionary_set_uint16(args, "vendor-id", fg->fg_vendor);
697 prop_dictionary_set_uint16(args, "product-id", fg->fg_product);
698 if (config)
699 prop_dictionary_set(args, "pin-config", config);
700
701 locs[0] = fg->fg_nid;
702
703 fg->fg_device = config_found_sm_loc(sc->sc_dev, "hdaudiobus",
704 locs, args, hdaudio_config_print, config_stdsubmatch);
705
706 prop_object_release(args);
707 }
708
709 static void
710 hdaudio_codec_attach(struct hdaudio_codec *co)
711 {
712 struct hdaudio_function_group *fg;
713 uint32_t vid, snc, fgrp;
714 int starting_node, num_nodes, nid;
715
716 if (co->co_valid == false)
717 return;
718
719 vid = hdaudio_command(co, 0, CORB_GET_PARAMETER, COP_VENDOR_ID);
720 snc = hdaudio_command(co, 0, CORB_GET_PARAMETER,
721 COP_SUBORDINATE_NODE_COUNT);
722
723 /* make sure the vendor and product IDs are valid */
724 if (vid == 0xffffffff || vid == 0x00000000)
725 return;
726
727 #ifdef HDAUDIO_DEBUG
728 struct hdaudio_softc *sc = co->co_host;
729 uint32_t rid = hdaudio_command(co, 0, CORB_GET_PARAMETER,
730 COP_REVISION_ID);
731 hda_print(sc, "Codec%02X: %04X:%04X HDA %d.%d rev %d stepping %d\n",
732 co->co_addr, vid >> 16, vid & 0xffff,
733 (rid >> 20) & 0xf, (rid >> 16) & 0xf,
734 (rid >> 8) & 0xff, rid & 0xff);
735 #endif
736 starting_node = (snc >> 16) & 0xff;
737 num_nodes = snc & 0xff;
738
739 co->co_nfg = num_nodes;
740 co->co_fg = kmem_zalloc(co->co_nfg * sizeof(*co->co_fg), KM_SLEEP);
741
742 for (nid = starting_node; nid < starting_node + num_nodes; nid++) {
743 fg = &co->co_fg[nid - starting_node];
744 fg->fg_codec = co;
745 fg->fg_nid = nid;
746 fg->fg_vendor = vid >> 16;
747 fg->fg_product = vid & 0xffff;
748
749 fgrp = hdaudio_command(co, nid, CORB_GET_PARAMETER,
750 COP_FUNCTION_GROUP_TYPE);
751 switch (fgrp & 0xff) {
752 case 0x01: /* Audio Function Group */
753 fg->fg_type = HDAUDIO_GROUP_TYPE_AFG;
754 break;
755 case 0x02: /* Vendor Specific Modem Function Group */
756 fg->fg_type = HDAUDIO_GROUP_TYPE_VSM_FG;
757 break;
758 default:
759 /* Function group type not supported */
760 fg->fg_type = HDAUDIO_GROUP_TYPE_UNKNOWN;
761 break;
762 }
763 hdaudio_attach_fg(fg, NULL);
764 }
765 }
766
767 int
768 hdaudio_stream_tag(struct hdaudio_stream *st)
769 {
770 int ret = 0;
771
772 switch (st->st_type) {
773 case HDAUDIO_STREAM_ISS:
774 ret = 1;
775 break;
776 case HDAUDIO_STREAM_OSS:
777 ret = 2;
778 break;
779 case HDAUDIO_STREAM_BSS:
780 ret = 3;
781 break;
782 }
783
784 return ret;
785 }
786
787 int
788 hdaudio_attach(device_t dev, struct hdaudio_softc *sc)
789 {
790 int err, i;
791
792 KASSERT(sc->sc_memvalid == true);
793
794 sc->sc_dev = dev;
795 mutex_init(&sc->sc_corb_mtx, MUTEX_DEFAULT, IPL_AUDIO);
796 mutex_init(&sc->sc_stream_mtx, MUTEX_DEFAULT, IPL_AUDIO);
797
798 hdaudio_init(sc);
799
800 /*
801 * Put the controller into a known state by entering and leaving
802 * CRST as necessary.
803 */
804 if ((err = hdaudio_reset(sc)) != 0)
805 goto fail;
806
807 /*
808 * From the spec:
809 *
810 * Must wait 250us after reading CRST as a 1 before assuming that
811 * codecs have all made status change requests and have been
812 * registered by the controller.
813 *
814 * In reality, we need to wait longer than this.
815 */
816 hda_delay(HDAUDIO_CODEC_DELAY);
817 if (hdaudio_codec_probe(sc) == 0) {
818 hda_error(sc, "no codecs found\n");
819 err = ENODEV;
820 goto fail;
821 }
822
823 /*
824 * Ensure that the device is in a known state
825 */
826 hda_write2(sc, HDAUDIO_MMIO_STATESTS, HDAUDIO_STATESTS_SDIWAKE);
827 hda_write1(sc, HDAUDIO_MMIO_RIRBSTS,
828 HDAUDIO_RIRBSTS_RIRBOIS | HDAUDIO_RIRBSTS_RINTFL);
829 hda_write4(sc, HDAUDIO_MMIO_INTSTS,
830 hda_read4(sc, HDAUDIO_MMIO_INTSTS));
831 hda_write4(sc, HDAUDIO_MMIO_DPLBASE, 0);
832 hda_write4(sc, HDAUDIO_MMIO_DPUBASE, 0);
833
834 /*
835 * Initialize the CORB. First negotiate a command buffer size,
836 * then allocate and configure it.
837 */
838 if ((err = hdaudio_corb_setsize(sc)) != 0)
839 goto fail;
840 if ((err = hdaudio_dma_alloc(sc, &sc->sc_corb, BUS_DMA_WRITE)) != 0)
841 goto fail;
842 if ((err = hdaudio_corb_config(sc)) != 0)
843 goto fail;
844
845 /*
846 * Initialize the RIRB.
847 */
848 if ((err = hdaudio_rirb_setsize(sc)) != 0)
849 goto fail;
850 if ((err = hdaudio_dma_alloc(sc, &sc->sc_rirb, BUS_DMA_READ)) != 0)
851 goto fail;
852 if ((err = hdaudio_rirb_config(sc)) != 0)
853 goto fail;
854
855 /*
856 * Start the CORB and RIRB
857 */
858 if ((err = hdaudio_corb_start(sc)) != 0)
859 goto fail;
860 if ((err = hdaudio_rirb_start(sc)) != 0)
861 goto fail;
862
863 /*
864 * Identify and attach discovered codecs
865 */
866 for (i = 0; i < HDAUDIO_MAX_CODECS; i++)
867 hdaudio_codec_attach(&sc->sc_codec[i]);
868
869 /*
870 * Enable interrupts
871 */
872 hdaudio_intr_enable(sc);
873
874 fail:
875 if (err)
876 hda_error(sc, "device driver failed to attach\n");
877 return err;
878 }
879
880 int
881 hdaudio_detach(struct hdaudio_softc *sc, int flags)
882 {
883 int error;
884
885 /* Disable interrupts */
886 hdaudio_intr_disable(sc);
887
888 error = config_detach_children(sc->sc_dev, flags);
889 if (error != 0) {
890 hdaudio_intr_enable(sc);
891 return error;
892 }
893
894 mutex_destroy(&sc->sc_corb_mtx);
895 mutex_destroy(&sc->sc_stream_mtx);
896
897 hdaudio_dma_free(sc, &sc->sc_corb);
898 hdaudio_dma_free(sc, &sc->sc_rirb);
899
900 return 0;
901 }
902
903 bool
904 hdaudio_resume(struct hdaudio_softc *sc)
905 {
906 if (hdaudio_reset(sc) != 0)
907 return false;
908
909 hda_delay(HDAUDIO_CODEC_DELAY);
910
911 /*
912 * Ensure that the device is in a known state
913 */
914 hda_write2(sc, HDAUDIO_MMIO_STATESTS, HDAUDIO_STATESTS_SDIWAKE);
915 hda_write1(sc, HDAUDIO_MMIO_RIRBSTS,
916 HDAUDIO_RIRBSTS_RIRBOIS | HDAUDIO_RIRBSTS_RINTFL);
917 hda_write4(sc, HDAUDIO_MMIO_INTSTS,
918 hda_read4(sc, HDAUDIO_MMIO_INTSTS));
919 hda_write4(sc, HDAUDIO_MMIO_DPLBASE, 0);
920 hda_write4(sc, HDAUDIO_MMIO_DPUBASE, 0);
921
922 if (hdaudio_corb_config(sc) != 0)
923 return false;
924 if (hdaudio_rirb_config(sc) != 0)
925 return false;
926 if (hdaudio_corb_start(sc) != 0)
927 return false;
928 if (hdaudio_rirb_start(sc) != 0)
929 return false;
930
931 hdaudio_intr_enable(sc);
932
933 return true;
934 }
935
936 int
937 hdaudio_rescan(struct hdaudio_softc *sc, const char *ifattr, const int *locs)
938 {
939 struct hdaudio_codec *co;
940 struct hdaudio_function_group *fg;
941 unsigned int codec;
942
943 if (!ifattr_match(ifattr, "hdaudiobus"))
944 return 0;
945
946 for (codec = 0; codec < HDAUDIO_MAX_CODECS; codec++) {
947 co = &sc->sc_codec[codec];
948 fg = co->co_fg;
949 if (!co->co_valid || fg == NULL)
950 continue;
951 if (fg->fg_device)
952 continue;
953 hdaudio_attach_fg(fg, NULL);
954 }
955
956 return 0;
957 }
958
959 void
960 hdaudio_childdet(struct hdaudio_softc *sc, device_t child)
961 {
962 struct hdaudio_codec *co;
963 struct hdaudio_function_group *fg;
964 unsigned int codec;
965
966 for (codec = 0; codec < HDAUDIO_MAX_CODECS; codec++) {
967 co = &sc->sc_codec[codec];
968 fg = co->co_fg;
969 if (!co->co_valid || fg == NULL)
970 continue;
971 if (fg->fg_device == child)
972 fg->fg_device = NULL;
973 }
974 }
975
976 int
977 hdaudio_intr(struct hdaudio_softc *sc)
978 {
979 struct hdaudio_stream *st;
980 uint32_t intsts, stream_mask;
981 int streamid = 0;
982 uint8_t rirbsts;
983
984 intsts = hda_read4(sc, HDAUDIO_MMIO_INTSTS);
985 if (!(intsts & HDAUDIO_INTSTS_GIS))
986 return 0;
987
988 if (intsts & HDAUDIO_INTSTS_CIS) {
989 rirbsts = hda_read1(sc, HDAUDIO_MMIO_RIRBSTS);
990 if (rirbsts & HDAUDIO_RIRBSTS_RINTFL) {
991 mutex_enter(&sc->sc_corb_mtx);
992 hdaudio_rirb_dequeue(sc, true);
993 mutex_exit(&sc->sc_corb_mtx);
994 }
995 if (rirbsts & (HDAUDIO_RIRBSTS_RIRBOIS|HDAUDIO_RIRBSTS_RINTFL))
996 hda_write1(sc, HDAUDIO_MMIO_RIRBSTS, rirbsts);
997 hda_write4(sc, HDAUDIO_MMIO_INTSTS, HDAUDIO_INTSTS_CIS);
998 }
999 if (intsts & HDAUDIO_INTSTS_SIS_MASK) {
1000 mutex_enter(&sc->sc_stream_mtx);
1001 stream_mask = intsts & sc->sc_stream_mask;
1002 while (streamid < HDAUDIO_MAX_STREAMS && stream_mask != 0) {
1003 st = &sc->sc_stream[streamid++];
1004 if ((stream_mask & 1) != 0 && st->st_intr) {
1005 st->st_intr(st);
1006 }
1007 stream_mask >>= 1;
1008 }
1009 mutex_exit(&sc->sc_stream_mtx);
1010 hda_write4(sc, HDAUDIO_MMIO_INTSTS, HDAUDIO_INTSTS_SIS_MASK);
1011 }
1012
1013 return 1;
1014 }
1015
1016 struct hdaudio_stream *
1017 hdaudio_stream_establish(struct hdaudio_softc *sc,
1018 enum hdaudio_stream_type type, int (*intr)(struct hdaudio_stream *),
1019 void *cookie)
1020 {
1021 struct hdaudio_stream *st;
1022 struct hdaudio_dma dma;
1023 int i, err;
1024
1025 dma.dma_size = sizeof(struct hdaudio_bdl_entry) * HDAUDIO_BDL_MAX;
1026 dma.dma_sizereg = 0;
1027 err = hdaudio_dma_alloc(sc, &dma, BUS_DMA_COHERENT | BUS_DMA_NOCACHE);
1028 if (err)
1029 return NULL;
1030
1031 mutex_enter(&sc->sc_stream_mtx);
1032 for (i = 0; i < HDAUDIO_MAX_STREAMS; i++) {
1033 st = &sc->sc_stream[i];
1034 if (st->st_enable == false)
1035 break;
1036 if (st->st_type != type)
1037 continue;
1038 if (sc->sc_stream_mask & (1 << i))
1039 continue;
1040
1041 /* Allocate stream */
1042 st->st_bdl = dma;
1043 st->st_intr = intr;
1044 st->st_cookie = cookie;
1045 sc->sc_stream_mask |= (1 << i);
1046 mutex_exit(&sc->sc_stream_mtx);
1047 return st;
1048 }
1049 mutex_exit(&sc->sc_stream_mtx);
1050
1051 /* No streams of requested type available */
1052 hdaudio_dma_free(sc, &dma);
1053 return NULL;
1054 }
1055
1056 void
1057 hdaudio_stream_disestablish(struct hdaudio_stream *st)
1058 {
1059 struct hdaudio_softc *sc = st->st_host;
1060 struct hdaudio_dma dma;
1061
1062 KASSERT(sc->sc_stream_mask & (1 << st->st_shift));
1063
1064 mutex_enter(&sc->sc_stream_mtx);
1065 sc->sc_stream_mask &= ~(1 << st->st_shift);
1066 st->st_intr = NULL;
1067 st->st_cookie = NULL;
1068 dma = st->st_bdl;
1069 st->st_bdl.dma_valid = false;
1070 mutex_exit(&sc->sc_stream_mtx);
1071
1072 /* Can't bus_dmamem_unmap while holding a mutex. */
1073 hdaudio_dma_free(sc, &dma);
1074 }
1075
1076 /*
1077 * Convert most of audio_params_t to stream fmt descriptor; noticably missing
1078 * is the # channels bits, as this is encoded differently in codec and
1079 * stream descriptors.
1080 *
1081 * TODO: validate that the stream and selected codecs can handle the fmt
1082 */
1083 uint16_t
1084 hdaudio_stream_param(struct hdaudio_stream *st, const audio_params_t *param)
1085 {
1086 uint16_t fmt = 0;
1087
1088 switch (param->encoding) {
1089 case AUDIO_ENCODING_AC3:
1090 fmt |= HDAUDIO_FMT_TYPE_NONPCM;
1091 break;
1092 default:
1093 fmt |= HDAUDIO_FMT_TYPE_PCM;
1094 break;
1095 }
1096
1097 switch (param->sample_rate) {
1098 case 8000:
1099 fmt |= HDAUDIO_FMT_BASE_48 | HDAUDIO_FMT_MULT(1) |
1100 HDAUDIO_FMT_DIV(6);
1101 break;
1102 case 11025:
1103 fmt |= HDAUDIO_FMT_BASE_44 | HDAUDIO_FMT_MULT(1) |
1104 HDAUDIO_FMT_DIV(4);
1105 break;
1106 case 16000:
1107 fmt |= HDAUDIO_FMT_BASE_48 | HDAUDIO_FMT_MULT(1) |
1108 HDAUDIO_FMT_DIV(3);
1109 break;
1110 case 22050:
1111 fmt |= HDAUDIO_FMT_BASE_44 | HDAUDIO_FMT_MULT(1) |
1112 HDAUDIO_FMT_DIV(2);
1113 break;
1114 case 32000:
1115 fmt |= HDAUDIO_FMT_BASE_48 | HDAUDIO_FMT_MULT(2) |
1116 HDAUDIO_FMT_DIV(3);
1117 break;
1118 case 44100:
1119 fmt |= HDAUDIO_FMT_BASE_44 | HDAUDIO_FMT_MULT(1);
1120 break;
1121 case 48000:
1122 fmt |= HDAUDIO_FMT_BASE_48 | HDAUDIO_FMT_MULT(1);
1123 break;
1124 case 88200:
1125 fmt |= HDAUDIO_FMT_BASE_44 | HDAUDIO_FMT_MULT(2);
1126 break;
1127 case 96000:
1128 fmt |= HDAUDIO_FMT_BASE_48 | HDAUDIO_FMT_MULT(2);
1129 break;
1130 case 176400:
1131 fmt |= HDAUDIO_FMT_BASE_44 | HDAUDIO_FMT_MULT(4);
1132 break;
1133 case 192000:
1134 fmt |= HDAUDIO_FMT_BASE_48 | HDAUDIO_FMT_MULT(4);
1135 break;
1136 default:
1137 return 0;
1138 }
1139
1140 if (param->precision == 16 && param->validbits == 8)
1141 fmt |= HDAUDIO_FMT_BITS_8_16;
1142 else if (param->precision == 16 && param->validbits == 16)
1143 fmt |= HDAUDIO_FMT_BITS_16_16;
1144 else if (param->precision == 32 && param->validbits == 20)
1145 fmt |= HDAUDIO_FMT_BITS_20_32;
1146 else if (param->precision == 32 && param->validbits == 24)
1147 fmt |= HDAUDIO_FMT_BITS_24_32;
1148 else if (param->precision == 32 && param->validbits == 32)
1149 fmt |= HDAUDIO_FMT_BITS_32_32;
1150 else
1151 return 0;
1152
1153 return fmt;
1154 }
1155
1156 void
1157 hdaudio_stream_reset(struct hdaudio_stream *st)
1158 {
1159 struct hdaudio_softc *sc = st->st_host;
1160 int snum = st->st_shift;
1161 int retry;
1162 uint8_t ctl0;
1163
1164 ctl0 = hda_read1(sc, HDAUDIO_SD_CTL0(snum));
1165 ctl0 |= HDAUDIO_CTL_SRST;
1166 hda_write1(sc, HDAUDIO_SD_CTL0(snum), ctl0);
1167
1168 retry = HDAUDIO_RESET_TIMEOUT;
1169 do {
1170 ctl0 = hda_read1(sc, HDAUDIO_SD_CTL0(snum));
1171 if (ctl0 & HDAUDIO_CTL_SRST)
1172 break;
1173 hda_delay(10);
1174 } while (--retry > 0);
1175 if (retry == 0) {
1176 hda_error(sc, "timeout entering stream reset state\n");
1177 return;
1178 }
1179
1180 ctl0 &= ~HDAUDIO_CTL_SRST;
1181 hda_write1(sc, HDAUDIO_SD_CTL0(snum), ctl0);
1182
1183 retry = HDAUDIO_RESET_TIMEOUT;
1184 do {
1185 ctl0 = hda_read1(sc, HDAUDIO_SD_CTL0(snum));
1186 if (!(ctl0 & HDAUDIO_CTL_SRST))
1187 break;
1188 hda_delay(10);
1189 } while (--retry > 0);
1190 if (retry == 0) {
1191 hda_error(sc, "timeout leaving stream reset state\n");
1192 return;
1193 }
1194 }
1195
1196 void
1197 hdaudio_stream_start(struct hdaudio_stream *st, int blksize,
1198 bus_size_t dmasize, const audio_params_t *params)
1199 {
1200 struct hdaudio_softc *sc = st->st_host;
1201 struct hdaudio_bdl_entry *bdl;
1202 uint64_t dmaaddr;
1203 uint32_t intctl;
1204 uint16_t fmt;
1205 uint8_t ctl0, ctl2;
1206 int cnt, snum = st->st_shift;
1207
1208 KASSERT(sc->sc_stream_mask & (1 << st->st_shift));
1209 KASSERT(st->st_data.dma_valid == true);
1210 KASSERT(st->st_bdl.dma_valid == true);
1211
1212 hdaudio_stream_stop(st);
1213 hdaudio_stream_reset(st);
1214
1215 /*
1216 * Configure buffer descriptor list
1217 */
1218 dmaaddr = DMA_DMAADDR(&st->st_data);
1219 bdl = DMA_KERNADDR(&st->st_bdl);
1220 for (cnt = 0; cnt < HDAUDIO_BDL_MAX; cnt++) {
1221 bdl[cnt].address_lo = (uint32_t)dmaaddr;
1222 bdl[cnt].address_hi = dmaaddr >> 32;
1223 bdl[cnt].length = blksize;
1224 bdl[cnt].flags = HDAUDIO_BDL_ENTRY_IOC;
1225 dmaaddr += blksize;
1226 if (dmaaddr >= DMA_DMAADDR(&st->st_data) + dmasize) {
1227 cnt++;
1228 break;
1229 }
1230 }
1231
1232 /*
1233 * Program buffer descriptor list
1234 */
1235 dmaaddr = DMA_DMAADDR(&st->st_bdl);
1236 hda_write4(sc, HDAUDIO_SD_BDPL(snum), (uint32_t)dmaaddr);
1237 hda_write4(sc, HDAUDIO_SD_BDPU(snum), (uint32_t)(dmaaddr >> 32));
1238 hda_write2(sc, HDAUDIO_SD_LVI(snum), (cnt - 1) & 0xff);
1239
1240 /*
1241 * Program cyclic buffer length
1242 */
1243 hda_write4(sc, HDAUDIO_SD_CBL(snum), dmasize);
1244
1245 /*
1246 * Program stream number (tag). Although controller hardware is
1247 * capable of transmitting any stream number (0-15), by convention
1248 * stream 0 is reserved as unused by software, so that converters
1249 * whose stream numbers have been reset to 0 do not unintentionally
1250 * decode data not intended for them.
1251 */
1252 ctl2 = hda_read1(sc, HDAUDIO_SD_CTL2(snum));
1253 ctl2 &= ~0xf0;
1254 ctl2 |= hdaudio_stream_tag(st) << 4;
1255 hda_write1(sc, HDAUDIO_SD_CTL2(snum), ctl2);
1256
1257 /*
1258 * Program stream format
1259 */
1260 fmt = hdaudio_stream_param(st, params) |
1261 HDAUDIO_FMT_CHAN(params->channels);
1262 hda_write2(sc, HDAUDIO_SD_FMT(snum), fmt);
1263
1264 /*
1265 * Switch on interrupts for this stream
1266 */
1267 intctl = hda_read4(sc, HDAUDIO_MMIO_INTCTL);
1268 intctl |= (1 << st->st_shift);
1269 hda_write4(sc, HDAUDIO_MMIO_INTCTL, intctl);
1270
1271 /*
1272 * Start running the stream
1273 */
1274 ctl0 = hda_read1(sc, HDAUDIO_SD_CTL0(snum));
1275 ctl0 |= HDAUDIO_CTL_DEIE | HDAUDIO_CTL_FEIE | HDAUDIO_CTL_IOCE |
1276 HDAUDIO_CTL_RUN;
1277 hda_write1(sc, HDAUDIO_SD_CTL0(snum), ctl0);
1278 }
1279
1280 void
1281 hdaudio_stream_stop(struct hdaudio_stream *st)
1282 {
1283 struct hdaudio_softc *sc = st->st_host;
1284 uint32_t intctl;
1285 uint8_t ctl0;
1286 int snum = st->st_shift;
1287
1288 /*
1289 * Stop running the stream
1290 */
1291 ctl0 = hda_read1(sc, HDAUDIO_SD_CTL0(snum));
1292 ctl0 &= ~(HDAUDIO_CTL_DEIE | HDAUDIO_CTL_FEIE | HDAUDIO_CTL_IOCE |
1293 HDAUDIO_CTL_RUN);
1294 hda_write1(sc, HDAUDIO_SD_CTL0(snum), ctl0);
1295
1296 /*
1297 * Switch off interrupts for this stream
1298 */
1299 intctl = hda_read4(sc, HDAUDIO_MMIO_INTCTL);
1300 intctl &= ~(1 << st->st_shift);
1301 hda_write4(sc, HDAUDIO_MMIO_INTCTL, intctl);
1302 }
1303
1304 /*
1305 * /dev/hdaudioN interface
1306 */
1307
1308 static const char *
1309 hdaudioioctl_fgrp_to_cstr(enum function_group_type type)
1310 {
1311 switch (type) {
1312 case HDAUDIO_GROUP_TYPE_AFG:
1313 return "afg";
1314 case HDAUDIO_GROUP_TYPE_VSM_FG:
1315 return "vsmfg";
1316 default:
1317 return "unknown";
1318 }
1319 }
1320
1321 static struct hdaudio_function_group *
1322 hdaudioioctl_fgrp_lookup(struct hdaudio_softc *sc, int codecid, int nid)
1323 {
1324 struct hdaudio_codec *co;
1325 struct hdaudio_function_group *fg = NULL;
1326 int i;
1327
1328 if (codecid < 0 || codecid >= HDAUDIO_MAX_CODECS)
1329 return NULL;
1330 co = &sc->sc_codec[codecid];
1331 if (co->co_valid == false)
1332 return NULL;
1333
1334 for (i = 0; i < co->co_nfg; i++)
1335 if (co->co_fg[i].fg_nid == nid) {
1336 fg = &co->co_fg[i];
1337 break;
1338 }
1339
1340 return fg;
1341 }
1342
1343 static int
1344 hdaudioioctl_fgrp_info(struct hdaudio_softc *sc, prop_dictionary_t request,
1345 prop_dictionary_t response)
1346 {
1347 struct hdaudio_codec *co;
1348 struct hdaudio_function_group *fg;
1349 prop_array_t array;
1350 prop_dictionary_t dict;
1351 int codecid, fgid;
1352
1353 array = prop_array_create();
1354 if (array == NULL)
1355 return ENOMEM;
1356
1357 for (codecid = 0; codecid < HDAUDIO_MAX_CODECS; codecid++) {
1358 co = &sc->sc_codec[codecid];
1359 if (co->co_valid == false)
1360 continue;
1361 for (fgid = 0; fgid < co->co_nfg; fgid++) {
1362 fg = &co->co_fg[fgid];
1363 dict = prop_dictionary_create();
1364 if (dict == NULL)
1365 return ENOMEM;
1366 prop_dictionary_set_cstring_nocopy(dict,
1367 "type", hdaudioioctl_fgrp_to_cstr(fg->fg_type));
1368 prop_dictionary_set_int16(dict, "nid", fg->fg_nid);
1369 prop_dictionary_set_int16(dict, "codecid", codecid);
1370 prop_dictionary_set_uint16(dict, "vendor-id",
1371 fg->fg_vendor);
1372 prop_dictionary_set_uint16(dict, "product-id",
1373 fg->fg_product);
1374 prop_dictionary_set_uint32(dict, "subsystem-id",
1375 sc->sc_subsystem);
1376 if (fg->fg_device)
1377 prop_dictionary_set_cstring(dict, "device",
1378 device_xname(fg->fg_device));
1379 else
1380 prop_dictionary_set_cstring_nocopy(dict,
1381 "device", "<none>");
1382 prop_array_add(array, dict);
1383 }
1384 }
1385
1386 prop_dictionary_set(response, "function-group-info", array);
1387 return 0;
1388 }
1389
1390 static int
1391 hdaudioioctl_fgrp_getconfig(struct hdaudio_softc *sc,
1392 prop_dictionary_t request, prop_dictionary_t response)
1393 {
1394 struct hdaudio_function_group *fg;
1395 prop_dictionary_t dict;
1396 prop_array_t array;
1397 uint32_t nodecnt, wcap, config;
1398 int16_t codecid, nid, i;
1399 int startnode, endnode;
1400
1401 if (!prop_dictionary_get_int16(request, "codecid", &codecid) ||
1402 !prop_dictionary_get_int16(request, "nid", &nid))
1403 return EINVAL;
1404
1405 fg = hdaudioioctl_fgrp_lookup(sc, codecid, nid);
1406 if (fg == NULL)
1407 return ENODEV;
1408
1409 array = prop_array_create();
1410 if (array == NULL)
1411 return ENOMEM;
1412
1413 nodecnt = hdaudio_command(fg->fg_codec, fg->fg_nid,
1414 CORB_GET_PARAMETER, COP_SUBORDINATE_NODE_COUNT);
1415 startnode = COP_NODECNT_STARTNODE(nodecnt);
1416 endnode = startnode + COP_NODECNT_NUMNODES(nodecnt);
1417
1418 for (i = startnode; i < endnode; i++) {
1419 wcap = hdaudio_command(fg->fg_codec, i,
1420 CORB_GET_PARAMETER, COP_AUDIO_WIDGET_CAPABILITIES);
1421 if (COP_AWCAP_TYPE(wcap) != COP_AWCAP_TYPE_PIN_COMPLEX)
1422 continue;
1423 config = hdaudio_command(fg->fg_codec, i,
1424 CORB_GET_CONFIGURATION_DEFAULT, 0);
1425 dict = prop_dictionary_create();
1426 if (dict == NULL)
1427 return ENOMEM;
1428 prop_dictionary_set_int16(dict, "nid", i);
1429 prop_dictionary_set_uint32(dict, "config", config);
1430 prop_array_add(array, dict);
1431 }
1432
1433 prop_dictionary_set(response, "pin-config", array);
1434
1435 return 0;
1436 }
1437
1438 static int
1439 hdaudioioctl_fgrp_setconfig(struct hdaudio_softc *sc,
1440 prop_dictionary_t request, prop_dictionary_t response)
1441 {
1442 struct hdaudio_function_group *fg;
1443 prop_array_t config;
1444 int16_t codecid, nid;
1445 int err;
1446
1447 if (!prop_dictionary_get_int16(request, "codecid", &codecid) ||
1448 !prop_dictionary_get_int16(request, "nid", &nid))
1449 return EINVAL;
1450
1451 fg = hdaudioioctl_fgrp_lookup(sc, codecid, nid);
1452 if (fg == NULL)
1453 return ENODEV;
1454
1455 if (fg->fg_device) {
1456 err = config_detach(fg->fg_device, 0);
1457 if (err)
1458 return err;
1459 fg->fg_device = NULL;
1460 }
1461
1462 /* "pin-config" may be NULL, this means "use BIOS configuration" */
1463 config = prop_dictionary_get(request, "pin-config");
1464 if (config && prop_object_type(config) != PROP_TYPE_ARRAY) {
1465 prop_object_release(config);
1466 return EINVAL;
1467 }
1468 hdaudio_attach_fg(fg, config);
1469 if (config)
1470 prop_object_release(config);
1471
1472 return 0;
1473 }
1474
1475 static int
1476 hdaudio_dispatch_fgrp_ioctl(struct hdaudio_softc *sc, u_long cmd,
1477 prop_dictionary_t request, prop_dictionary_t response)
1478 {
1479 struct hdaudio_function_group *fg;
1480 int (*infocb)(void *, prop_dictionary_t, prop_dictionary_t);
1481 prop_dictionary_t fgrp_dict;
1482 uint64_t info_fn;
1483 int16_t codecid, nid;
1484 void *fgrp_sc;
1485 bool rv;
1486 int err;
1487
1488 if (!prop_dictionary_get_int16(request, "codecid", &codecid) ||
1489 !prop_dictionary_get_int16(request, "nid", &nid))
1490 return EINVAL;
1491
1492 fg = hdaudioioctl_fgrp_lookup(sc, codecid, nid);
1493 if (fg == NULL)
1494 return ENODEV;
1495 if (fg->fg_device == NULL)
1496 return ENXIO;
1497 fgrp_sc = device_private(fg->fg_device);
1498 fgrp_dict = device_properties(fg->fg_device);
1499
1500 switch (fg->fg_type) {
1501 case HDAUDIO_GROUP_TYPE_AFG:
1502 switch (cmd) {
1503 case HDAUDIO_FGRP_CODEC_INFO:
1504 rv = prop_dictionary_get_uint64(fgrp_dict,
1505 "codecinfo-callback", &info_fn);
1506 if (!rv)
1507 return ENXIO;
1508 infocb = (void *)(uintptr_t)info_fn;
1509 err = infocb(fgrp_sc, request, response);
1510 break;
1511 case HDAUDIO_FGRP_WIDGET_INFO:
1512 rv = prop_dictionary_get_uint64(fgrp_dict,
1513 "widgetinfo-callback", &info_fn);
1514 if (!rv)
1515 return ENXIO;
1516 infocb = (void *)(uintptr_t)info_fn;
1517 err = infocb(fgrp_sc, request, response);
1518 break;
1519 default:
1520 err = EINVAL;
1521 break;
1522 }
1523 break;
1524
1525 default:
1526 err = EINVAL;
1527 break;
1528 }
1529 return err;
1530 }
1531
1532 int
1533 hdaudioopen(dev_t dev, int flag, int mode, struct lwp *l)
1534 {
1535 device_t self;
1536
1537 self = device_lookup(&hdaudio_cd, HDAUDIOUNIT(dev));
1538 if (self == NULL)
1539 return ENXIO;
1540
1541 return 0;
1542 }
1543
1544 int
1545 hdaudioclose(dev_t dev, int flag, int mode, struct lwp *l)
1546 {
1547 return 0;
1548 }
1549
1550 int
1551 hdaudioioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
1552 {
1553 struct hdaudio_softc *sc;
1554 struct plistref *pref = addr;
1555 prop_dictionary_t request, response;
1556 int err;
1557
1558 sc = device_lookup_private(&hdaudio_cd, HDAUDIOUNIT(dev));
1559 if (sc == NULL)
1560 return ENXIO;
1561
1562 response = prop_dictionary_create();
1563 if (response == NULL)
1564 return ENOMEM;
1565
1566 err = prop_dictionary_copyin_ioctl(pref, cmd, &request);
1567 if (err) {
1568 prop_object_release(response);
1569 return err;
1570 }
1571
1572 switch (cmd) {
1573 case HDAUDIO_FGRP_INFO:
1574 err = hdaudioioctl_fgrp_info(sc, request, response);
1575 break;
1576 case HDAUDIO_FGRP_GETCONFIG:
1577 err = hdaudioioctl_fgrp_getconfig(sc, request, response);
1578 break;
1579 case HDAUDIO_FGRP_SETCONFIG:
1580 err = hdaudioioctl_fgrp_setconfig(sc, request, response);
1581 break;
1582 case HDAUDIO_FGRP_CODEC_INFO:
1583 case HDAUDIO_FGRP_WIDGET_INFO:
1584 err = hdaudio_dispatch_fgrp_ioctl(sc, cmd, request, response);
1585 break;
1586 default:
1587 err = EINVAL;
1588 break;
1589 }
1590
1591 if (!err)
1592 err = prop_dictionary_copyout_ioctl(pref, cmd, response);
1593
1594 if (response)
1595 prop_object_release(response);
1596 prop_object_release(request);
1597 return err;
1598 }
1599
1600 MODULE(MODULE_CLASS_DRIVER, hdaudio, "audio");
1601 #ifdef _MODULE
1602 static const struct cfiattrdata hdaudiobuscf_iattrdata = {
1603 "hdaudiobus", 1, {
1604 { "nid", "-1", -1 },
1605 }
1606 };
1607 static const struct cfiattrdata * const hdaudio_attrs[] = {
1608 &hdaudiobuscf_iattrdata, NULL
1609 };
1610 CFDRIVER_DECL(hdaudio, DV_AUDIODEV, hdaudio_attrs);
1611 #endif
1612
1613 static int
1614 hdaudio_modcmd(modcmd_t cmd, void *opaque)
1615 {
1616 int error = 0;
1617 #ifdef _MODULE
1618 int bmaj = -1, cmaj = -1;
1619 #endif
1620
1621 switch (cmd) {
1622 case MODULE_CMD_INIT:
1623 #ifdef _MODULE
1624 error = devsw_attach("hdaudio", NULL, &bmaj,
1625 &hdaudio_cdevsw, &cmaj);
1626 if (error)
1627 break;
1628 error = config_cfdriver_attach(&hdaudio_cd);
1629 if (error)
1630 devsw_detach(NULL, &hdaudio_cdevsw);
1631 #endif
1632 break;
1633 case MODULE_CMD_FINI:
1634 #ifdef _MODULE
1635 error = config_cfdriver_detach(&hdaudio_cd);
1636 if (error)
1637 break;
1638 error = devsw_detach(NULL, &hdaudio_cdevsw);
1639 if (error) {
1640 config_cfdriver_attach(&hdaudio_cd);
1641 break;
1642 }
1643 #endif
1644 break;
1645 default:
1646 error = ENOTTY;
1647 break;
1648 }
1649 return error;
1650 }
1651
1652 DEV_VERBOSE_DEFINE(hdaudio);
1653