xref: /netbsd/sys/dev/hdaudio/hdaudio.c (revision 778f5600)
1 /* $NetBSD: hdaudio.c,v 1.18 2022/04/07 19:33:37 andvar Exp $ */
2 
3 /*
4  * Copyright (c) 2009 Precedence Technologies Ltd <support@precedence.co.uk>
5  * Copyright (c) 2009 Jared D. McNeill <jmcneill@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.18 2022/04/07 19:33:37 andvar 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 #include "hdaudiodevs.h"
49 
50 /* #define	HDAUDIO_DEBUG */
51 
52 #define	HDAUDIO_RESET_TIMEOUT	5000
53 #define HDAUDIO_CORB_TIMEOUT	1000
54 #define	HDAUDIO_RIRB_TIMEOUT	5000
55 
56 #define	HDAUDIO_CODEC_DELAY	1000	/* spec calls for 250 */
57 
58 dev_type_open(hdaudioopen);
59 dev_type_close(hdaudioclose);
60 dev_type_ioctl(hdaudioioctl);
61 
62 const struct cdevsw hdaudio_cdevsw = {
63 	.d_open = hdaudioopen,
64 	.d_close = hdaudioclose,
65 	.d_read = noread,
66 	.d_write = nowrite,
67 	.d_ioctl = hdaudioioctl,
68 	.d_stop = nostop,
69 	.d_tty = notty,
70 	.d_poll = nopoll,
71 	.d_mmap = nommap,
72 	.d_kqfilter = nokqfilter,
73 	.d_discard = nodiscard,
74 	.d_flag = D_OTHER
75 };
76 
77 extern struct cfdriver hdaudio_cd;
78 
79 #define	HDAUDIOUNIT(x)	minor((x))
80 
81 static void
hdaudio_stream_init(struct hdaudio_softc * sc,int nis,int nos,int nbidir)82 hdaudio_stream_init(struct hdaudio_softc *sc, int nis, int nos, int nbidir)
83 {
84 	int i, cnt = 0;
85 
86 	for (i = 0; i < nis && cnt < HDAUDIO_MAX_STREAMS; i++) {
87 		sc->sc_stream[cnt].st_host = sc;
88 		sc->sc_stream[cnt].st_enable = true;
89 		sc->sc_stream[cnt].st_shift = cnt;
90 		sc->sc_stream[cnt++].st_type = HDAUDIO_STREAM_ISS;
91 	}
92 	for (i = 0; i < nos && cnt < HDAUDIO_MAX_STREAMS; i++) {
93 		sc->sc_stream[cnt].st_host = sc;
94 		sc->sc_stream[cnt].st_enable = true;
95 		sc->sc_stream[cnt].st_shift = cnt;
96 		sc->sc_stream[cnt++].st_type = HDAUDIO_STREAM_OSS;
97 	}
98 	for (i = 0; i < nbidir && cnt < HDAUDIO_MAX_STREAMS; i++) {
99 		sc->sc_stream[cnt].st_host = sc;
100 		sc->sc_stream[cnt].st_enable = true;
101 		sc->sc_stream[cnt].st_shift = cnt;
102 		sc->sc_stream[cnt++].st_type = HDAUDIO_STREAM_BSS;
103 	}
104 
105 	for (i = 0; i < cnt; i++)
106 		hdaudio_stream_stop(&sc->sc_stream[i]);
107 
108 	sc->sc_stream_mask = 0;
109 }
110 
111 static void
hdaudio_codec_init(struct hdaudio_softc * sc)112 hdaudio_codec_init(struct hdaudio_softc *sc)
113 {
114 	int i;
115 
116 	for (i = 0; i < HDAUDIO_MAX_CODECS; i++) {
117 		sc->sc_codec[i].co_addr = i;
118 		sc->sc_codec[i].co_host = sc;
119 	}
120 }
121 
122 static void
hdaudio_init(struct hdaudio_softc * sc)123 hdaudio_init(struct hdaudio_softc *sc)
124 {
125 	const uint8_t vmaj = hda_read1(sc, HDAUDIO_MMIO_VMAJ);
126 	const uint8_t vmin = hda_read1(sc, HDAUDIO_MMIO_VMIN);
127 	const uint16_t gcap = hda_read2(sc, HDAUDIO_MMIO_GCAP);
128 	const int nis = HDAUDIO_GCAP_ISS(gcap);
129 	const int nos = HDAUDIO_GCAP_OSS(gcap);
130 	const int nbidir = HDAUDIO_GCAP_BSS(gcap);
131 	const int nsdo = HDAUDIO_GCAP_NSDO(gcap);
132 	const int addr64 = HDAUDIO_GCAP_64OK(gcap);
133 
134 	hda_print(sc, "HDA ver. %d.%d, OSS %d, ISS %d, BSS %d, SDO %d%s\n",
135 	    vmaj, vmin, nos, nis, nbidir, nsdo, addr64 ? ", 64-bit" : "");
136 
137 	/* Initialize codecs and streams */
138 	hdaudio_codec_init(sc);
139 	hdaudio_stream_init(sc, nis, nos, nbidir);
140 }
141 
142 static int
hdaudio_codec_probe(struct hdaudio_softc * sc)143 hdaudio_codec_probe(struct hdaudio_softc *sc)
144 {
145 	uint16_t statests;
146 	int codecid;
147 
148 	statests = hda_read2(sc, HDAUDIO_MMIO_STATESTS);
149 	for (codecid = 0; codecid < HDAUDIO_MAX_CODECS; codecid++)
150 		if (statests & (1 << codecid))
151 			sc->sc_codec[codecid].co_valid = true;
152 	hda_write2(sc, HDAUDIO_MMIO_STATESTS, statests);
153 
154 	return statests;
155 }
156 
157 int
hdaudio_dma_alloc(struct hdaudio_softc * sc,struct hdaudio_dma * dma,int flags)158 hdaudio_dma_alloc(struct hdaudio_softc *sc, struct hdaudio_dma *dma,
159     int flags)
160 {
161 	int err;
162 
163 	KASSERT(dma->dma_size > 0);
164 
165 	err = bus_dmamem_alloc(sc->sc_dmat, dma->dma_size, 128, 0,
166 	    dma->dma_segs, sizeof(dma->dma_segs) / sizeof(dma->dma_segs[0]),
167 	    &dma->dma_nsegs, BUS_DMA_WAITOK);
168 	if (err)
169 		return err;
170 	err = bus_dmamem_map(sc->sc_dmat, dma->dma_segs, dma->dma_nsegs,
171 	    dma->dma_size, &dma->dma_addr, BUS_DMA_WAITOK | flags);
172 	if (err)
173 		goto free;
174 	err = bus_dmamap_create(sc->sc_dmat, dma->dma_size, dma->dma_nsegs,
175 	    dma->dma_size, 0, BUS_DMA_WAITOK, &dma->dma_map);
176 	if (err)
177 		goto unmap;
178 	err = bus_dmamap_load(sc->sc_dmat, dma->dma_map, dma->dma_addr,
179 	    dma->dma_size, NULL, BUS_DMA_WAITOK | flags);
180 	if (err)
181 		goto destroy;
182 
183 	memset(dma->dma_addr, 0, dma->dma_size);
184 	bus_dmamap_sync(sc->sc_dmat, dma->dma_map, 0, dma->dma_size,
185 	    BUS_DMASYNC_PREWRITE);
186 
187 	dma->dma_valid = true;
188 	return 0;
189 
190 destroy:
191 	bus_dmamap_destroy(sc->sc_dmat, dma->dma_map);
192 unmap:
193 	bus_dmamem_unmap(sc->sc_dmat, dma->dma_addr, dma->dma_size);
194 free:
195 	bus_dmamem_free(sc->sc_dmat, dma->dma_segs, dma->dma_nsegs);
196 
197 	dma->dma_valid = false;
198 	return err;
199 }
200 
201 void
hdaudio_dma_free(struct hdaudio_softc * sc,struct hdaudio_dma * dma)202 hdaudio_dma_free(struct hdaudio_softc *sc, struct hdaudio_dma *dma)
203 {
204 	if (dma->dma_valid == false)
205 		return;
206 	bus_dmamap_unload(sc->sc_dmat, dma->dma_map);
207 	bus_dmamap_destroy(sc->sc_dmat, dma->dma_map);
208 	bus_dmamem_unmap(sc->sc_dmat, dma->dma_addr, dma->dma_size);
209 	bus_dmamem_free(sc->sc_dmat, dma->dma_segs, dma->dma_nsegs);
210 	dma->dma_valid = false;
211 }
212 
213 static void
hdaudio_corb_enqueue(struct hdaudio_softc * sc,int addr,int nid,uint32_t control,uint32_t param)214 hdaudio_corb_enqueue(struct hdaudio_softc *sc, int addr, int nid,
215     uint32_t control, uint32_t param)
216 {
217 	uint32_t *corb = DMA_KERNADDR(&sc->sc_corb);
218 	uint32_t verb;
219 	uint16_t corbrp;
220 	int wp;
221 
222 	/* Build command */
223 	verb = (addr << 28) | (nid << 20) | (control << 8) | param;
224 
225 	/* Fetch and update write pointer */
226 	corbrp = hda_read2(sc, HDAUDIO_MMIO_CORBWP);
227 	wp = (corbrp & 0xff) + 1;
228 	if (wp >= (sc->sc_corb.dma_size / sizeof(*corb)))
229 		wp = 0;
230 
231 	/* Enqueue command */
232 	bus_dmamap_sync(sc->sc_dmat, sc->sc_corb.dma_map, 0,
233 	    sc->sc_corb.dma_size, BUS_DMASYNC_POSTWRITE);
234 	corb[wp] = verb;
235 	bus_dmamap_sync(sc->sc_dmat, sc->sc_corb.dma_map, 0,
236 	    sc->sc_corb.dma_size, BUS_DMASYNC_PREWRITE);
237 
238 	/* Commit updated write pointer */
239 	hda_write2(sc, HDAUDIO_MMIO_CORBWP, wp);
240 }
241 
242 static void
hdaudio_rirb_unsol(struct hdaudio_softc * sc,struct rirb_entry * entry)243 hdaudio_rirb_unsol(struct hdaudio_softc *sc, struct rirb_entry *entry)
244 {
245 	struct hdaudio_codec *co;
246 	struct hdaudio_function_group *fg;
247 	uint8_t codecid = RIRB_CODEC_ID(entry);
248 	unsigned int i;
249 
250 	if (codecid >= HDAUDIO_MAX_CODECS) {
251 		hda_error(sc, "unsol: codec id 0x%02x out of range\n", codecid);
252 		return;
253 	}
254 	co = &sc->sc_codec[codecid];
255 	if (sc->sc_codec[codecid].co_valid == false) {
256 		hda_error(sc, "unsol: codec id 0x%02x not valid\n", codecid);
257 		return;
258 	}
259 
260 	for (i = 0; i < co->co_nfg; i++) {
261 		fg = &co->co_fg[i];
262 		if (fg->fg_device && fg->fg_unsol)
263 			fg->fg_unsol(fg->fg_device, entry->resp);
264 	}
265 }
266 
267 static uint32_t
hdaudio_rirb_dequeue(struct hdaudio_softc * sc,bool unsol)268 hdaudio_rirb_dequeue(struct hdaudio_softc *sc, bool unsol)
269 {
270 	uint16_t rirbwp;
271 	uint64_t *rirb = DMA_KERNADDR(&sc->sc_rirb);
272 	struct rirb_entry entry;
273 	int retry;
274 
275 	for (;;) {
276 		retry = HDAUDIO_RIRB_TIMEOUT;
277 
278 		rirbwp = hda_read2(sc, HDAUDIO_MMIO_RIRBWP);
279 		while (--retry > 0 && (rirbwp & 0xff) == sc->sc_rirbrp) {
280 			if (unsol) {
281 				/* don't wait for more unsol events */
282 				hda_trace(sc, "unsol: rirb empty\n");
283 				return 0xffffffff;
284 			}
285 			hda_delay(10);
286 			rirbwp = hda_read2(sc, HDAUDIO_MMIO_RIRBWP);
287 		}
288 		if (retry == 0) {
289 			hda_error(sc, "RIRB timeout\n");
290 			return 0xffffffff;
291 		}
292 
293 		sc->sc_rirbrp++;
294 		if (sc->sc_rirbrp >= (sc->sc_rirb.dma_size / sizeof(*rirb)))
295 			sc->sc_rirbrp = 0;
296 
297 		bus_dmamap_sync(sc->sc_dmat, sc->sc_rirb.dma_map, 0,
298 		    sc->sc_rirb.dma_size, BUS_DMASYNC_POSTREAD);
299 		entry = *(struct rirb_entry *)&rirb[sc->sc_rirbrp];
300 		bus_dmamap_sync(sc->sc_dmat, sc->sc_rirb.dma_map, 0,
301 		    sc->sc_rirb.dma_size, BUS_DMASYNC_PREREAD);
302 
303 		hda_trace(sc, "%s: response %08X %08X\n",
304 		    unsol ? "unsol" : "cmd  ",
305 		    entry.resp, entry.resp_ex);
306 
307 		if (RIRB_UNSOL(&entry)) {
308 			hdaudio_rirb_unsol(sc, &entry);
309 			continue;
310 		}
311 
312 		return entry.resp;
313 	}
314 }
315 
316 uint32_t
hdaudio_command(struct hdaudio_codec * co,int nid,uint32_t control,uint32_t param)317 hdaudio_command(struct hdaudio_codec *co, int nid, uint32_t control,
318     uint32_t param)
319 {
320 	uint32_t result;
321 	struct hdaudio_softc *sc = co->co_host;
322 	mutex_enter(&sc->sc_corb_mtx);
323 	result = hdaudio_command_unlocked(co, nid, control, param);
324 	mutex_exit(&sc->sc_corb_mtx);
325 	return result;
326 }
327 
328 uint32_t
hdaudio_command_unlocked(struct hdaudio_codec * co,int nid,uint32_t control,uint32_t param)329 hdaudio_command_unlocked(struct hdaudio_codec *co, int nid, uint32_t control,
330     uint32_t param)
331 {
332 	struct hdaudio_softc *sc = co->co_host;
333 	uint32_t result;
334 
335 	hda_trace(sc, "cmd  : request %08X %08X (%02X)\n",
336 	    control, param, nid);
337 	hdaudio_corb_enqueue(sc, co->co_addr, nid, control, param);
338 	result = hdaudio_rirb_dequeue(sc, false);
339 
340 	/* Clear response interrupt status */
341 	hda_write1(sc, HDAUDIO_MMIO_RIRBSTS, hda_read1(sc, HDAUDIO_MMIO_RIRBSTS));
342 
343 	return result;
344 }
345 
346 static int
hdaudio_corb_setsize(struct hdaudio_softc * sc)347 hdaudio_corb_setsize(struct hdaudio_softc *sc)
348 {
349 	uint8_t corbsize;
350 	bus_size_t bufsize = 0;
351 
352 	/*
353 	 * The size of the CORB is programmable to 2, 16, or 256 entries
354 	 * by using the CORBSIZE register. Choose a size based on the
355 	 * controller capabilities, preferring a larger size when possible.
356 	 */
357 	corbsize = hda_read1(sc, HDAUDIO_MMIO_CORBSIZE);
358 	corbsize &= ~0x3;
359 	if ((corbsize >> 4) & 0x4) {
360 		corbsize |= 0x2;
361 		bufsize = 1024;
362 	} else if ((corbsize >> 4) & 0x2) {
363 		corbsize |= 0x1;
364 		bufsize = 64;
365 	} else if ((corbsize >> 4) & 0x1) {
366 		corbsize |= 0x0;
367 		bufsize = 8;
368 	} else {
369 		hda_error(sc, "couldn't configure CORB size\n");
370 		return ENXIO;
371 	}
372 
373 #if defined(HDAUDIO_DEBUG)
374 	hda_print(sc, "using %d byte CORB (cap %X)\n",
375 	    (int)bufsize, corbsize >> 4);
376 #endif
377 
378 	sc->sc_corb.dma_size = bufsize;
379 	sc->sc_corb.dma_sizereg = corbsize;
380 
381 	return 0;
382 }
383 
384 static int
hdaudio_corb_config(struct hdaudio_softc * sc)385 hdaudio_corb_config(struct hdaudio_softc *sc)
386 {
387 	uint32_t corbubase, corblbase;
388 	uint16_t corbrp;
389 	int retry = HDAUDIO_CORB_TIMEOUT;
390 
391 	/* Program command buffer base address and size */
392 	corblbase = (uint32_t)DMA_DMAADDR(&sc->sc_corb);
393 	corbubase = (uint32_t)(((uint64_t)DMA_DMAADDR(&sc->sc_corb)) >> 32);
394 	hda_write4(sc, HDAUDIO_MMIO_CORBLBASE, corblbase);
395 	hda_write4(sc, HDAUDIO_MMIO_CORBUBASE, corbubase);
396 	hda_write1(sc, HDAUDIO_MMIO_CORBSIZE, sc->sc_corb.dma_sizereg);
397 
398 	/* Clear the read and write pointers */
399 	hda_write2(sc, HDAUDIO_MMIO_CORBRP, HDAUDIO_CORBRP_RP_RESET);
400 	hda_write2(sc, HDAUDIO_MMIO_CORBRP, 0);
401 	do {
402 		hda_delay(10);
403 		corbrp = hda_read2(sc, HDAUDIO_MMIO_CORBRP);
404 	} while (--retry > 0 && (corbrp & HDAUDIO_CORBRP_RP_RESET) != 0);
405 	if (retry == 0) {
406 		hda_error(sc, "timeout resetting CORB\n");
407 		return ETIME;
408 	}
409 	hda_write2(sc, HDAUDIO_MMIO_CORBWP, 0);
410 
411 	return 0;
412 }
413 
414 static int
hdaudio_corb_stop(struct hdaudio_softc * sc)415 hdaudio_corb_stop(struct hdaudio_softc *sc)
416 {
417 	uint8_t corbctl;
418 	int retry = HDAUDIO_CORB_TIMEOUT;
419 
420 	/* Stop the CORB if necessary */
421 	corbctl = hda_read1(sc, HDAUDIO_MMIO_CORBCTL);
422 	if (corbctl & HDAUDIO_CORBCTL_RUN) {
423 		corbctl &= ~HDAUDIO_CORBCTL_RUN;
424 		hda_write1(sc, HDAUDIO_MMIO_CORBCTL, corbctl);
425 		do {
426 			hda_delay(10);
427 			corbctl = hda_read1(sc, HDAUDIO_MMIO_CORBCTL);
428 		} while (--retry > 0 && (corbctl & HDAUDIO_CORBCTL_RUN) != 0);
429 		if (retry == 0) {
430 			hda_error(sc, "timeout stopping CORB\n");
431 			return ETIME;
432 		}
433 	}
434 
435 	return 0;
436 }
437 
438 static int
hdaudio_corb_start(struct hdaudio_softc * sc)439 hdaudio_corb_start(struct hdaudio_softc *sc)
440 {
441 	uint8_t corbctl;
442 	int retry = HDAUDIO_CORB_TIMEOUT;
443 
444 	/* Start the CORB if necessary */
445 	corbctl = hda_read1(sc, HDAUDIO_MMIO_CORBCTL);
446 	if ((corbctl & HDAUDIO_CORBCTL_RUN) == 0) {
447 		corbctl |= HDAUDIO_CORBCTL_RUN;
448 		hda_write1(sc, HDAUDIO_MMIO_CORBCTL, corbctl);
449 		do {
450 			hda_delay(10);
451 			corbctl = hda_read1(sc, HDAUDIO_MMIO_CORBCTL);
452 		} while (--retry > 0 && (corbctl & HDAUDIO_CORBCTL_RUN) == 0);
453 		if (retry == 0) {
454 			hda_error(sc, "timeout starting CORB\n");
455 			return ETIME;
456 		}
457 	}
458 
459 	return 0;
460 }
461 
462 static int
hdaudio_rirb_stop(struct hdaudio_softc * sc)463 hdaudio_rirb_stop(struct hdaudio_softc *sc)
464 {
465 	uint8_t rirbctl;
466 	int retry = HDAUDIO_RIRB_TIMEOUT;
467 
468 	/* Stop the RIRB if necessary */
469 	rirbctl = hda_read1(sc, HDAUDIO_MMIO_RIRBCTL);
470 	if (rirbctl & (HDAUDIO_RIRBCTL_RUN|HDAUDIO_RIRBCTL_ROI_EN)) {
471 		rirbctl &= ~HDAUDIO_RIRBCTL_RUN;
472 		rirbctl &= ~HDAUDIO_RIRBCTL_ROI_EN;
473 		hda_write1(sc, HDAUDIO_MMIO_RIRBCTL, rirbctl);
474 		do {
475 			hda_delay(10);
476 			rirbctl = hda_read1(sc, HDAUDIO_MMIO_RIRBCTL);
477 		} while (--retry > 0 && (rirbctl & HDAUDIO_RIRBCTL_RUN) != 0);
478 		if (retry == 0) {
479 			hda_error(sc, "timeout stopping RIRB\n");
480 			return ETIME;
481 		}
482 	}
483 
484 	return 0;
485 }
486 
487 static int
hdaudio_rirb_start(struct hdaudio_softc * sc)488 hdaudio_rirb_start(struct hdaudio_softc *sc)
489 {
490 	uint8_t rirbctl;
491 	int retry = HDAUDIO_RIRB_TIMEOUT;
492 
493 	/* Set the RIRB interrupt count */
494 	hda_write2(sc, HDAUDIO_MMIO_RINTCNT, 1);
495 
496 	/* Start the RIRB */
497 	rirbctl = hda_read1(sc, HDAUDIO_MMIO_RIRBCTL);
498 	rirbctl |= HDAUDIO_RIRBCTL_RUN;
499 	rirbctl |= HDAUDIO_RIRBCTL_INT_EN;
500 	hda_write1(sc, HDAUDIO_MMIO_RIRBCTL, rirbctl);
501 	do {
502 		hda_delay(10);
503 		rirbctl = hda_read1(sc, HDAUDIO_MMIO_RIRBCTL);
504 	} while (--retry > 0 && (rirbctl & HDAUDIO_RIRBCTL_RUN) == 0);
505 	if (retry == 0) {
506 		hda_error(sc, "timeout starting RIRB\n");
507 		return ETIME;
508 	}
509 
510 	return 0;
511 }
512 
513 static int
hdaudio_rirb_setsize(struct hdaudio_softc * sc)514 hdaudio_rirb_setsize(struct hdaudio_softc *sc)
515 {
516 	uint8_t rirbsize;
517 	bus_size_t bufsize = 0;
518 
519 	/*
520 	 * The size of the RIRB is programmable to 2, 16, or 256 entries
521 	 * by using the RIRBSIZE register. Choose a size based on the
522 	 * controller capabilities, preferring a larger size when possible.
523 	 */
524 	rirbsize = hda_read1(sc, HDAUDIO_MMIO_RIRBSIZE);
525 	rirbsize &= ~0x3;
526 	if ((rirbsize >> 4) & 0x4) {
527 		rirbsize |= 0x2;
528 		bufsize = 2048;
529 	} else if ((rirbsize >> 4) & 0x2) {
530 		rirbsize |= 0x1;
531 		bufsize = 128;
532 	} else if ((rirbsize >> 4) & 0x1) {
533 		rirbsize |= 0x0;
534 		bufsize = 16;
535 	} else {
536 		hda_error(sc, "couldn't configure RIRB size\n");
537 		return ENXIO;
538 	}
539 
540 #if defined(HDAUDIO_DEBUG)
541 	hda_print(sc, "using %d byte RIRB (cap %X)\n",
542 	    (int)bufsize, rirbsize >> 4);
543 #endif
544 
545 	sc->sc_rirb.dma_size = bufsize;
546 	sc->sc_rirb.dma_sizereg = rirbsize;
547 
548 	return 0;
549 }
550 
551 static int
hdaudio_rirb_config(struct hdaudio_softc * sc)552 hdaudio_rirb_config(struct hdaudio_softc *sc)
553 {
554 	uint32_t rirbubase, rirblbase;
555 
556 	/* Program command buffer base address and size */
557 	rirblbase = (uint32_t)DMA_DMAADDR(&sc->sc_rirb);
558 	rirbubase = (uint32_t)(((uint64_t)DMA_DMAADDR(&sc->sc_rirb)) >> 32);
559 	hda_write4(sc, HDAUDIO_MMIO_RIRBLBASE, rirblbase);
560 	hda_write4(sc, HDAUDIO_MMIO_RIRBUBASE, rirbubase);
561 	hda_write1(sc, HDAUDIO_MMIO_RIRBSIZE, sc->sc_rirb.dma_sizereg);
562 
563 	/* Clear the write pointer */
564 	hda_write2(sc, HDAUDIO_MMIO_RIRBWP, HDAUDIO_RIRBWP_WP_RESET);
565 	sc->sc_rirbrp = 0;
566 
567 	return 0;
568 }
569 
570 static int
hdaudio_reset(struct hdaudio_softc * sc)571 hdaudio_reset(struct hdaudio_softc *sc)
572 {
573 	int retry = HDAUDIO_RESET_TIMEOUT;
574 	uint32_t gctl;
575 	int err;
576 
577 	if ((err = hdaudio_rirb_stop(sc)) != 0) {
578 		hda_error(sc, "couldn't reset because RIRB is busy\n");
579 		return err;
580 	}
581 	if ((err = hdaudio_corb_stop(sc)) != 0) {
582 		hda_error(sc, "couldn't reset because CORB is busy\n");
583 		return err;
584 	}
585 
586 	/* Disable wake events */
587 	hda_write2(sc, HDAUDIO_MMIO_WAKEEN, 0);
588 
589 	/* Disable interrupts */
590 	hda_write4(sc, HDAUDIO_MMIO_INTCTL, 0);
591 
592 	/* Clear state change status register */
593 	hda_write2(sc, HDAUDIO_MMIO_STATESTS,
594 	    hda_read2(sc, HDAUDIO_MMIO_STATESTS));
595 	hda_write1(sc, HDAUDIO_MMIO_RIRBSTS,
596 	    hda_read1(sc, HDAUDIO_MMIO_RIRBSTS));
597 
598 	/* Put the controller into reset state */
599 	gctl = hda_read4(sc, HDAUDIO_MMIO_GCTL);
600 	gctl &= ~HDAUDIO_GCTL_CRST;
601 	hda_write4(sc, HDAUDIO_MMIO_GCTL, gctl);
602 	do {
603 		hda_delay(10);
604 		gctl = hda_read4(sc, HDAUDIO_MMIO_GCTL);
605 	} while (--retry > 0 && (gctl & HDAUDIO_GCTL_CRST) != 0);
606 	if (retry == 0) {
607 		hda_error(sc, "timeout entering reset state\n");
608 		return ETIME;
609 	}
610 
611 	hda_delay(1000);
612 
613 	/* Now the controller is in reset state, so bring it out */
614 	retry = HDAUDIO_RESET_TIMEOUT;
615 	hda_write4(sc, HDAUDIO_MMIO_GCTL, gctl | HDAUDIO_GCTL_CRST);
616 	do {
617 		hda_delay(10);
618 		gctl = hda_read4(sc, HDAUDIO_MMIO_GCTL);
619 	} while (--retry > 0 && (gctl & HDAUDIO_GCTL_CRST) == 0);
620 	if (retry == 0) {
621 		hda_error(sc, "timeout leaving reset state\n");
622 		return ETIME;
623 	}
624 
625 	hda_delay(2000);
626 
627 	/* Accept unsolicited responses */
628 	hda_write4(sc, HDAUDIO_MMIO_GCTL, gctl | HDAUDIO_GCTL_UNSOL_EN);
629 
630 	return 0;
631 }
632 
633 static void
hdaudio_intr_enable(struct hdaudio_softc * sc)634 hdaudio_intr_enable(struct hdaudio_softc *sc)
635 {
636 	hda_write4(sc, HDAUDIO_MMIO_INTSTS,
637 	    hda_read4(sc, HDAUDIO_MMIO_INTSTS));
638 	hda_write4(sc, HDAUDIO_MMIO_INTCTL,
639 	    HDAUDIO_INTCTL_GIE | HDAUDIO_INTCTL_CIE);
640 }
641 
642 static void
hdaudio_intr_disable(struct hdaudio_softc * sc)643 hdaudio_intr_disable(struct hdaudio_softc *sc)
644 {
645 	hda_write4(sc, HDAUDIO_MMIO_INTCTL, 0);
646 }
647 
648 static int
hdaudio_config_print(void * opaque,const char * pnp)649 hdaudio_config_print(void *opaque, const char *pnp)
650 {
651 	prop_dictionary_t dict = opaque;
652 	uint8_t fgtype, nid;
653 	uint16_t vendor, product;
654 	const char *type = "unknown";
655 
656 	prop_dictionary_get_uint8(dict, "function-group-type", &fgtype);
657 	prop_dictionary_get_uint8(dict, "node-id", &nid);
658 	prop_dictionary_get_uint16(dict, "vendor-id", &vendor);
659 	prop_dictionary_get_uint16(dict, "product-id", &product);
660 	if (pnp) {
661 		if (fgtype == HDAUDIO_GROUP_TYPE_AFG)
662 			type = "hdafg";
663 		else if (fgtype == HDAUDIO_GROUP_TYPE_VSM_FG)
664 			type = "hdvsmfg";
665 
666 		aprint_normal("%s at %s", type, pnp);
667 	}
668 	aprint_debug(" vendor 0x%04X product 0x%04X nid 0x%02X",
669 	    vendor, product, nid);
670 
671 	return UNCONF;
672 }
673 
674 static void
hdaudio_attach_fg(struct hdaudio_function_group * fg,prop_array_t config)675 hdaudio_attach_fg(struct hdaudio_function_group *fg, prop_array_t config)
676 {
677 	struct hdaudio_codec *co = fg->fg_codec;
678 	struct hdaudio_softc *sc = co->co_host;
679 	prop_dictionary_t args = prop_dictionary_create();
680 	uint64_t fgptr = (vaddr_t)fg;
681 	int locs[1];
682 
683 	prop_dictionary_set_uint8(args, "function-group-type", fg->fg_type);
684 	prop_dictionary_set_uint64(args, "function-group", fgptr);
685 	prop_dictionary_set_uint8(args, "node-id", fg->fg_nid);
686 	prop_dictionary_set_uint16(args, "vendor-id", fg->fg_vendor);
687 	prop_dictionary_set_uint16(args, "product-id", fg->fg_product);
688 	if (config)
689 		prop_dictionary_set(args, "pin-config", config);
690 
691 	locs[0] = fg->fg_nid;
692 
693 	fg->fg_device = config_found(sc->sc_dev, args, hdaudio_config_print,
694 	    CFARGS(.submatch = config_stdsubmatch,
695 		   .locators = locs));
696 
697 	prop_object_release(args);
698 }
699 
700 static void
hdaudio_codec_attach(struct hdaudio_codec * co)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
hdaudio_stream_tag(struct hdaudio_stream * st)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
hdaudio_attach(device_t dev,struct hdaudio_softc * sc)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
hdaudio_detach(struct hdaudio_softc * sc,int flags)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
hdaudio_resume(struct hdaudio_softc * sc)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
hdaudio_rescan(struct hdaudio_softc * sc,const char * ifattr,const int * locs)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
hdaudio_childdet(struct hdaudio_softc * sc,device_t child)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
hdaudio_intr(struct hdaudio_softc * sc)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 *
hdaudio_stream_establish(struct hdaudio_softc * sc,enum hdaudio_stream_type type,int (* intr)(struct hdaudio_stream *),void * cookie)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
hdaudio_stream_disestablish(struct hdaudio_stream * st)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; noticeably 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
hdaudio_stream_param(struct hdaudio_stream * st,const audio_params_t * param)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
hdaudio_stream_reset(struct hdaudio_stream * st)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
hdaudio_stream_start(struct hdaudio_stream * st,int blksize,bus_size_t dmasize,const audio_params_t * params)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
hdaudio_stream_stop(struct hdaudio_stream * st)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 *
hdaudioioctl_fgrp_to_cstr(enum function_group_type type)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 *
hdaudioioctl_fgrp_lookup(struct hdaudio_softc * sc,int codecid,int nid)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
hdaudioioctl_fgrp_info(struct hdaudio_softc * sc,prop_dictionary_t request,prop_dictionary_t response)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
hdaudioioctl_fgrp_getconfig(struct hdaudio_softc * sc,prop_dictionary_t request,prop_dictionary_t response)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
hdaudioioctl_fgrp_setconfig(struct hdaudio_softc * sc,prop_dictionary_t request,prop_dictionary_t response)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
hdaudio_dispatch_fgrp_ioctl(struct hdaudio_softc * sc,u_long cmd,prop_dictionary_t request,prop_dictionary_t response)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
hdaudioopen(dev_t dev,int flag,int mode,struct lwp * l)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
hdaudioclose(dev_t dev,int flag,int mode,struct lwp * l)1546 hdaudioclose(dev_t dev, int flag, int mode, struct lwp *l)
1547 {
1548 	return 0;
1549 }
1550 
1551 int
hdaudioioctl(dev_t dev,u_long cmd,void * addr,int flag,struct lwp * l)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
hdaudio_modcmd(modcmd_t cmd,void * opaque)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 		devsw_detach(NULL, &hdaudio_cdevsw);
1640 #endif
1641 		break;
1642 	default:
1643 		error = ENOTTY;
1644 		break;
1645 	}
1646 	return error;
1647 }
1648 
1649 DEV_VERBOSE_DEFINE(hdaudio);
1650