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