2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2006 Stephane E. Potvin <sepotvin@videotron.ca>
5 * Copyright (c) 2006 Ariff Abdullah <ariff@FreeBSD.org>
6 * Copyright (c) 2008-2012 Alexander Motin <mav@FreeBSD.org>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Intel High Definition Audio (Controller) driver for FreeBSD.
35 #ifdef HAVE_KERNEL_OPTION_HEADERS
39 #include <dev/sound/pcm/sound.h>
40 #include <dev/pci/pcireg.h>
41 #include <dev/pci/pcivar.h>
43 #include <sys/ctype.h>
44 #include <sys/endian.h>
45 #include <sys/taskqueue.h>
47 #include <dev/sound/pci/hda/hdac_private.h>
48 #include <dev/sound/pci/hda/hdac_reg.h>
49 #include <dev/sound/pci/hda/hda_reg.h>
50 #include <dev/sound/pci/hda/hdac.h>
52 #define HDA_DRV_TEST_REV "20120126_0002"
54 SND_DECLARE_FILE("$FreeBSD$");
56 #define hdac_lock(sc) snd_mtxlock((sc)->lock)
57 #define hdac_unlock(sc) snd_mtxunlock((sc)->lock)
58 #define hdac_lockassert(sc) snd_mtxassert((sc)->lock)
60 #define HDAC_QUIRK_64BIT (1 << 0)
61 #define HDAC_QUIRK_DMAPOS (1 << 1)
62 #define HDAC_QUIRK_MSI (1 << 2)
67 } hdac_quirks_tab[] = {
68 { "64bit", HDAC_QUIRK_64BIT },
69 { "dmapos", HDAC_QUIRK_DMAPOS },
70 { "msi", HDAC_QUIRK_MSI },
73 MALLOC_DEFINE(M_HDAC, "hdac", "HDA Controller");
81 { HDA_INTEL_OAK, "Intel Oaktrail", 0, 0 },
82 { HDA_INTEL_CMLKLP, "Intel Comet Lake-LP", 0, 0 },
83 { HDA_INTEL_CMLKH, "Intel Comet Lake-H", 0, 0 },
84 { HDA_INTEL_BAY, "Intel BayTrail", 0, 0 },
85 { HDA_INTEL_HSW1, "Intel Haswell", 0, 0 },
86 { HDA_INTEL_HSW2, "Intel Haswell", 0, 0 },
87 { HDA_INTEL_HSW3, "Intel Haswell", 0, 0 },
88 { HDA_INTEL_BDW1, "Intel Broadwell", 0, 0 },
89 { HDA_INTEL_BDW2, "Intel Broadwell", 0, 0 },
90 { HDA_INTEL_BXTNT, "Intel Broxton-T", 0, 0 },
91 { HDA_INTEL_CPT, "Intel Cougar Point", 0, 0 },
92 { HDA_INTEL_PATSBURG,"Intel Patsburg", 0, 0 },
93 { HDA_INTEL_PPT1, "Intel Panther Point", 0, 0 },
94 { HDA_INTEL_BR, "Intel Braswell", 0, 0 },
95 { HDA_INTEL_LPT1, "Intel Lynx Point", 0, 0 },
96 { HDA_INTEL_LPT2, "Intel Lynx Point", 0, 0 },
97 { HDA_INTEL_WCPT, "Intel Wildcat Point", 0, 0 },
98 { HDA_INTEL_WELLS1, "Intel Wellsburg", 0, 0 },
99 { HDA_INTEL_WELLS2, "Intel Wellsburg", 0, 0 },
100 { HDA_INTEL_LPTLP1, "Intel Lynx Point-LP", 0, 0 },
101 { HDA_INTEL_LPTLP2, "Intel Lynx Point-LP", 0, 0 },
102 { HDA_INTEL_SRPTLP, "Intel Sunrise Point-LP", 0, 0 },
103 { HDA_INTEL_KBLKLP, "Intel Kaby Lake-LP", 0, 0 },
104 { HDA_INTEL_SRPT, "Intel Sunrise Point", 0, 0 },
105 { HDA_INTEL_KBLK, "Intel Kaby Lake", 0, 0 },
106 { HDA_INTEL_KBLKH, "Intel Kaby Lake-H", 0, 0 },
107 { HDA_INTEL_CFLK, "Intel Coffee Lake", 0, 0 },
108 { HDA_INTEL_CMLKS, "Intel Comet Lake-S", 0, 0 },
109 { HDA_INTEL_CNLK, "Intel Cannon Lake", 0, 0 },
110 { HDA_INTEL_ICLK, "Intel Ice Lake", 0, 0 },
111 { HDA_INTEL_CMLKLP, "Intel Comet Lake-LP", 0, 0 },
112 { HDA_INTEL_CMLKH, "Intel Comet Lake-H", 0, 0 },
113 { HDA_INTEL_TGLK, "Intel Tiger Lake", 0, 0 },
114 { HDA_INTEL_GMLK, "Intel Gemini Lake", 0, 0 },
115 { HDA_INTEL_82801F, "Intel 82801F", 0, 0 },
116 { HDA_INTEL_63XXESB, "Intel 631x/632xESB", 0, 0 },
117 { HDA_INTEL_82801G, "Intel 82801G", 0, 0 },
118 { HDA_INTEL_82801H, "Intel 82801H", 0, 0 },
119 { HDA_INTEL_82801I, "Intel 82801I", 0, 0 },
120 { HDA_INTEL_JLK, "Intel Jasper Lake", 0, 0 },
121 { HDA_INTEL_82801JI, "Intel 82801JI", 0, 0 },
122 { HDA_INTEL_82801JD, "Intel 82801JD", 0, 0 },
123 { HDA_INTEL_PCH, "Intel Ibex Peak", 0, 0 },
124 { HDA_INTEL_PCH2, "Intel Ibex Peak", 0, 0 },
125 { HDA_INTEL_ELLK, "Intel Elkhart Lake", 0, 0 },
126 { HDA_INTEL_JLK2, "Intel Jasper Lake", 0, 0 },
127 { HDA_INTEL_BXTNP, "Intel Broxton-P", 0, 0 },
128 { HDA_INTEL_SCH, "Intel SCH", 0, 0 },
129 { HDA_NVIDIA_MCP51, "NVIDIA MCP51", 0, HDAC_QUIRK_MSI },
130 { HDA_NVIDIA_MCP55, "NVIDIA MCP55", 0, HDAC_QUIRK_MSI },
131 { HDA_NVIDIA_MCP61_1, "NVIDIA MCP61", 0, 0 },
132 { HDA_NVIDIA_MCP61_2, "NVIDIA MCP61", 0, 0 },
133 { HDA_NVIDIA_MCP65_1, "NVIDIA MCP65", 0, 0 },
134 { HDA_NVIDIA_MCP65_2, "NVIDIA MCP65", 0, 0 },
135 { HDA_NVIDIA_MCP67_1, "NVIDIA MCP67", 0, 0 },
136 { HDA_NVIDIA_MCP67_2, "NVIDIA MCP67", 0, 0 },
137 { HDA_NVIDIA_MCP73_1, "NVIDIA MCP73", 0, 0 },
138 { HDA_NVIDIA_MCP73_2, "NVIDIA MCP73", 0, 0 },
139 { HDA_NVIDIA_MCP78_1, "NVIDIA MCP78", 0, HDAC_QUIRK_64BIT },
140 { HDA_NVIDIA_MCP78_2, "NVIDIA MCP78", 0, HDAC_QUIRK_64BIT },
141 { HDA_NVIDIA_MCP78_3, "NVIDIA MCP78", 0, HDAC_QUIRK_64BIT },
142 { HDA_NVIDIA_MCP78_4, "NVIDIA MCP78", 0, HDAC_QUIRK_64BIT },
143 { HDA_NVIDIA_MCP79_1, "NVIDIA MCP79", 0, 0 },
144 { HDA_NVIDIA_MCP79_2, "NVIDIA MCP79", 0, 0 },
145 { HDA_NVIDIA_MCP79_3, "NVIDIA MCP79", 0, 0 },
146 { HDA_NVIDIA_MCP79_4, "NVIDIA MCP79", 0, 0 },
147 { HDA_NVIDIA_MCP89_1, "NVIDIA MCP89", 0, 0 },
148 { HDA_NVIDIA_MCP89_2, "NVIDIA MCP89", 0, 0 },
149 { HDA_NVIDIA_MCP89_3, "NVIDIA MCP89", 0, 0 },
150 { HDA_NVIDIA_MCP89_4, "NVIDIA MCP89", 0, 0 },
151 { HDA_NVIDIA_0BE2, "NVIDIA (0x0be2)", 0, HDAC_QUIRK_MSI },
152 { HDA_NVIDIA_0BE3, "NVIDIA (0x0be3)", 0, HDAC_QUIRK_MSI },
153 { HDA_NVIDIA_0BE4, "NVIDIA (0x0be4)", 0, HDAC_QUIRK_MSI },
154 { HDA_NVIDIA_GT100, "NVIDIA GT100", 0, HDAC_QUIRK_MSI },
155 { HDA_NVIDIA_GT104, "NVIDIA GT104", 0, HDAC_QUIRK_MSI },
156 { HDA_NVIDIA_GT106, "NVIDIA GT106", 0, HDAC_QUIRK_MSI },
157 { HDA_NVIDIA_GT108, "NVIDIA GT108", 0, HDAC_QUIRK_MSI },
158 { HDA_NVIDIA_GT116, "NVIDIA GT116", 0, HDAC_QUIRK_MSI },
159 { HDA_NVIDIA_GF119, "NVIDIA GF119", 0, 0 },
160 { HDA_NVIDIA_GF110_1, "NVIDIA GF110", 0, HDAC_QUIRK_MSI },
161 { HDA_NVIDIA_GF110_2, "NVIDIA GF110", 0, HDAC_QUIRK_MSI },
162 { HDA_ATI_SB450, "ATI SB450", 0, 0 },
163 { HDA_ATI_SB600, "ATI SB600", 0, 0 },
164 { HDA_ATI_RS600, "ATI RS600", 0, 0 },
165 { HDA_ATI_RS690, "ATI RS690", 0, 0 },
166 { HDA_ATI_RS780, "ATI RS780", 0, 0 },
167 { HDA_ATI_R600, "ATI R600", 0, 0 },
168 { HDA_ATI_RV610, "ATI RV610", 0, 0 },
169 { HDA_ATI_RV620, "ATI RV620", 0, 0 },
170 { HDA_ATI_RV630, "ATI RV630", 0, 0 },
171 { HDA_ATI_RV635, "ATI RV635", 0, 0 },
172 { HDA_ATI_RV710, "ATI RV710", 0, 0 },
173 { HDA_ATI_RV730, "ATI RV730", 0, 0 },
174 { HDA_ATI_RV740, "ATI RV740", 0, 0 },
175 { HDA_ATI_RV770, "ATI RV770", 0, 0 },
176 { HDA_ATI_RV810, "ATI RV810", 0, 0 },
177 { HDA_ATI_RV830, "ATI RV830", 0, 0 },
178 { HDA_ATI_RV840, "ATI RV840", 0, 0 },
179 { HDA_ATI_RV870, "ATI RV870", 0, 0 },
180 { HDA_ATI_RV910, "ATI RV910", 0, 0 },
181 { HDA_ATI_RV930, "ATI RV930", 0, 0 },
182 { HDA_ATI_RV940, "ATI RV940", 0, 0 },
183 { HDA_ATI_RV970, "ATI RV970", 0, 0 },
184 { HDA_ATI_R1000, "ATI R1000", 0, 0 },
185 { HDA_AMD_X370, "AMD X370", 0, 0 },
186 { HDA_AMD_X570, "AMD X570", 0, 0 },
187 { HDA_AMD_STONEY, "AMD Stoney", 0, 0 },
188 { HDA_AMD_RAVEN, "AMD Raven", 0, 0 },
189 { HDA_AMD_HUDSON2, "AMD Hudson-2", 0, 0 },
190 { HDA_RDC_M3010, "RDC M3010", 0, 0 },
191 { HDA_VIA_VT82XX, "VIA VT8251/8237A",0, 0 },
192 { HDA_SIS_966, "SiS 966/968", 0, 0 },
193 { HDA_ULI_M5461, "ULI M5461", 0, 0 },
195 { HDA_INTEL_ALL, "Intel", 0, 0 },
196 { HDA_NVIDIA_ALL, "NVIDIA", 0, 0 },
197 { HDA_ATI_ALL, "ATI", 0, 0 },
198 { HDA_AMD_ALL, "AMD", 0, 0 },
199 { HDA_CREATIVE_ALL, "Creative", 0, 0 },
200 { HDA_VIA_ALL, "VIA", 0, 0 },
201 { HDA_SIS_ALL, "SiS", 0, 0 },
202 { HDA_ULI_ALL, "ULI", 0, 0 },
205 static const struct {
210 } hdac_pcie_snoop[] = {
211 { INTEL_VENDORID, 0x00, 0x00, 0x00 },
212 { ATI_VENDORID, 0x42, 0xf8, 0x02 },
213 { AMD_VENDORID, 0x42, 0xf8, 0x02 },
214 { NVIDIA_VENDORID, 0x4e, 0xf0, 0x0f },
217 /****************************************************************************
218 * Function prototypes
219 ****************************************************************************/
220 static void hdac_intr_handler(void *);
221 static int hdac_reset(struct hdac_softc *, bool);
222 static int hdac_get_capabilities(struct hdac_softc *);
223 static void hdac_dma_cb(void *, bus_dma_segment_t *, int, int);
224 static int hdac_dma_alloc(struct hdac_softc *,
225 struct hdac_dma *, bus_size_t);
226 static void hdac_dma_free(struct hdac_softc *, struct hdac_dma *);
227 static int hdac_mem_alloc(struct hdac_softc *);
228 static void hdac_mem_free(struct hdac_softc *);
229 static int hdac_irq_alloc(struct hdac_softc *);
230 static void hdac_irq_free(struct hdac_softc *);
231 static void hdac_corb_init(struct hdac_softc *);
232 static void hdac_rirb_init(struct hdac_softc *);
233 static void hdac_corb_start(struct hdac_softc *);
234 static void hdac_rirb_start(struct hdac_softc *);
236 static void hdac_attach2(void *);
238 static uint32_t hdac_send_command(struct hdac_softc *, nid_t, uint32_t);
240 static int hdac_probe(device_t);
241 static int hdac_attach(device_t);
242 static int hdac_detach(device_t);
243 static int hdac_suspend(device_t);
244 static int hdac_resume(device_t);
246 static int hdac_rirb_flush(struct hdac_softc *sc);
247 static int hdac_unsolq_flush(struct hdac_softc *sc);
249 /* This function surely going to make its way into upper level someday. */
251 hdac_config_fetch(struct hdac_softc *sc, uint32_t *on, uint32_t *off)
253 const char *res = NULL;
254 int i = 0, j, k, len, inv;
256 if (resource_string_value(device_get_name(sc->dev),
257 device_get_unit(sc->dev), "config", &res) != 0)
259 if (!(res != NULL && strlen(res) > 0))
262 device_printf(sc->dev, "Config options:");
265 while (res[i] != '\0' &&
266 (res[i] == ',' || isspace(res[i]) != 0))
268 if (res[i] == '\0') {
275 while (res[j] != '\0' &&
276 !(res[j] == ',' || isspace(res[j]) != 0))
279 if (len > 2 && strncmp(res + i, "no", 2) == 0)
283 for (k = 0; len > inv && k < nitems(hdac_quirks_tab); k++) {
284 if (strncmp(res + i + inv,
285 hdac_quirks_tab[k].key, len - inv) != 0)
287 if (len - inv != strlen(hdac_quirks_tab[k].key))
290 printf(" %s%s", (inv != 0) ? "no" : "",
291 hdac_quirks_tab[k].key);
294 *on |= hdac_quirks_tab[k].value;
295 *off &= ~hdac_quirks_tab[k].value;
296 } else if (inv != 0) {
297 *off |= hdac_quirks_tab[k].value;
298 *on &= ~hdac_quirks_tab[k].value;
307 hdac_one_intr(struct hdac_softc *sc, uint32_t intsts)
313 /* Was this a controller interrupt? */
314 if (intsts & HDAC_INTSTS_CIS) {
316 * Placeholder: if we ever enable any bits in HDAC_WAKEEN, then
317 * we will need to check and clear HDAC_STATESTS.
318 * That event is used to report codec status changes such as
319 * a reset or a wake-up event.
322 * Placeholder: if we ever enable HDAC_CORBCTL_CMEIE, then we
323 * will need to check and clear HDAC_CORBSTS_CMEI in
325 * That event is used to report CORB memory errors.
328 * Placeholder: if we ever enable HDAC_RIRBCTL_RIRBOIC, then we
329 * will need to check and clear HDAC_RIRBSTS_RIRBOIS in
331 * That event is used to report response FIFO overruns.
334 /* Get as many responses that we can */
335 rirbsts = HDAC_READ_1(&sc->mem, HDAC_RIRBSTS);
336 while (rirbsts & HDAC_RIRBSTS_RINTFL) {
337 HDAC_WRITE_1(&sc->mem,
338 HDAC_RIRBSTS, HDAC_RIRBSTS_RINTFL);
340 rirbsts = HDAC_READ_1(&sc->mem, HDAC_RIRBSTS);
342 if (sc->unsolq_rp != sc->unsolq_wp)
343 taskqueue_enqueue(taskqueue_thread, &sc->unsolq_task);
346 if (intsts & HDAC_INTSTS_SIS_MASK) {
347 for (i = 0; i < sc->num_ss; i++) {
348 if ((intsts & (1 << i)) == 0)
350 HDAC_WRITE_1(&sc->mem, (i << 5) + HDAC_SDSTS,
351 HDAC_SDSTS_DESE | HDAC_SDSTS_FIFOE | HDAC_SDSTS_BCIS);
352 if ((dev = sc->streams[i].dev) != NULL) {
353 HDAC_STREAM_INTR(dev,
354 sc->streams[i].dir, sc->streams[i].stream);
360 /****************************************************************************
361 * void hdac_intr_handler(void *)
363 * Interrupt handler. Processes interrupts received from the hdac.
364 ****************************************************************************/
366 hdac_intr_handler(void *context)
368 struct hdac_softc *sc;
371 sc = (struct hdac_softc *)context;
374 * Loop until HDAC_INTSTS_GIS gets clear.
375 * It is plausible that hardware interrupts a host only when GIS goes
376 * from zero to one. GIS is formed by OR-ing multiple hardware
377 * statuses, so it's possible that a previously cleared status gets set
378 * again while another status has not been cleared yet. Thus, there
379 * will be no new interrupt as GIS always stayed set. If we don't
380 * re-examine GIS then we can leave it set and never get an interrupt
383 intsts = HDAC_READ_4(&sc->mem, HDAC_INTSTS);
384 while ((intsts & HDAC_INTSTS_GIS) != 0) {
386 hdac_one_intr(sc, intsts);
388 intsts = HDAC_READ_4(&sc->mem, HDAC_INTSTS);
393 hdac_poll_callback(void *arg)
395 struct hdac_softc *sc = arg;
401 if (sc->polling == 0) {
405 callout_reset(&sc->poll_callout, sc->poll_ival, hdac_poll_callback, sc);
408 hdac_intr_handler(sc);
411 /****************************************************************************
412 * int hdac_reset(hdac_softc *, bool)
414 * Reset the hdac to a quiescent and known state.
415 ****************************************************************************/
417 hdac_reset(struct hdac_softc *sc, bool wakeup)
423 * Stop all Streams DMA engine
425 for (i = 0; i < sc->num_iss; i++)
426 HDAC_WRITE_4(&sc->mem, HDAC_ISDCTL(sc, i), 0x0);
427 for (i = 0; i < sc->num_oss; i++)
428 HDAC_WRITE_4(&sc->mem, HDAC_OSDCTL(sc, i), 0x0);
429 for (i = 0; i < sc->num_bss; i++)
430 HDAC_WRITE_4(&sc->mem, HDAC_BSDCTL(sc, i), 0x0);
433 * Stop Control DMA engines.
435 HDAC_WRITE_1(&sc->mem, HDAC_CORBCTL, 0x0);
436 HDAC_WRITE_1(&sc->mem, HDAC_RIRBCTL, 0x0);
439 * Reset DMA position buffer.
441 HDAC_WRITE_4(&sc->mem, HDAC_DPIBLBASE, 0x0);
442 HDAC_WRITE_4(&sc->mem, HDAC_DPIBUBASE, 0x0);
445 * Reset the controller. The reset must remain asserted for
446 * a minimum of 100us.
448 gctl = HDAC_READ_4(&sc->mem, HDAC_GCTL);
449 HDAC_WRITE_4(&sc->mem, HDAC_GCTL, gctl & ~HDAC_GCTL_CRST);
452 gctl = HDAC_READ_4(&sc->mem, HDAC_GCTL);
453 if (!(gctl & HDAC_GCTL_CRST))
457 if (gctl & HDAC_GCTL_CRST) {
458 device_printf(sc->dev, "Unable to put hdac in reset\n");
462 /* If wakeup is not requested - leave the controller in reset state. */
467 gctl = HDAC_READ_4(&sc->mem, HDAC_GCTL);
468 HDAC_WRITE_4(&sc->mem, HDAC_GCTL, gctl | HDAC_GCTL_CRST);
471 gctl = HDAC_READ_4(&sc->mem, HDAC_GCTL);
472 if (gctl & HDAC_GCTL_CRST)
476 if (!(gctl & HDAC_GCTL_CRST)) {
477 device_printf(sc->dev, "Device stuck in reset\n");
482 * Wait for codecs to finish their own reset sequence. The delay here
483 * must be at least 521us (HDA 1.0a section 4.3 Codec Discovery).
490 /****************************************************************************
491 * int hdac_get_capabilities(struct hdac_softc *);
493 * Retreive the general capabilities of the hdac;
494 * Number of Input Streams
495 * Number of Output Streams
496 * Number of bidirectional Streams
498 * CORB and RIRB sizes
499 ****************************************************************************/
501 hdac_get_capabilities(struct hdac_softc *sc)
504 uint8_t corbsize, rirbsize;
506 gcap = HDAC_READ_2(&sc->mem, HDAC_GCAP);
507 sc->num_iss = HDAC_GCAP_ISS(gcap);
508 sc->num_oss = HDAC_GCAP_OSS(gcap);
509 sc->num_bss = HDAC_GCAP_BSS(gcap);
510 sc->num_ss = sc->num_iss + sc->num_oss + sc->num_bss;
511 sc->num_sdo = HDAC_GCAP_NSDO(gcap);
512 sc->support_64bit = (gcap & HDAC_GCAP_64OK) != 0;
513 if (sc->quirks_on & HDAC_QUIRK_64BIT)
514 sc->support_64bit = 1;
515 else if (sc->quirks_off & HDAC_QUIRK_64BIT)
516 sc->support_64bit = 0;
518 corbsize = HDAC_READ_1(&sc->mem, HDAC_CORBSIZE);
519 if ((corbsize & HDAC_CORBSIZE_CORBSZCAP_256) ==
520 HDAC_CORBSIZE_CORBSZCAP_256)
522 else if ((corbsize & HDAC_CORBSIZE_CORBSZCAP_16) ==
523 HDAC_CORBSIZE_CORBSZCAP_16)
525 else if ((corbsize & HDAC_CORBSIZE_CORBSZCAP_2) ==
526 HDAC_CORBSIZE_CORBSZCAP_2)
529 device_printf(sc->dev, "%s: Invalid corb size (%x)\n",
534 rirbsize = HDAC_READ_1(&sc->mem, HDAC_RIRBSIZE);
535 if ((rirbsize & HDAC_RIRBSIZE_RIRBSZCAP_256) ==
536 HDAC_RIRBSIZE_RIRBSZCAP_256)
538 else if ((rirbsize & HDAC_RIRBSIZE_RIRBSZCAP_16) ==
539 HDAC_RIRBSIZE_RIRBSZCAP_16)
541 else if ((rirbsize & HDAC_RIRBSIZE_RIRBSZCAP_2) ==
542 HDAC_RIRBSIZE_RIRBSZCAP_2)
545 device_printf(sc->dev, "%s: Invalid rirb size (%x)\n",
551 device_printf(sc->dev, "Caps: OSS %d, ISS %d, BSS %d, "
552 "NSDO %d%s, CORB %d, RIRB %d\n",
553 sc->num_oss, sc->num_iss, sc->num_bss, 1 << sc->num_sdo,
554 sc->support_64bit ? ", 64bit" : "",
555 sc->corb_size, sc->rirb_size);
562 /****************************************************************************
565 * This function is called by bus_dmamap_load when the mapping has been
566 * established. We just record the physical address of the mapping into
567 * the struct hdac_dma passed in.
568 ****************************************************************************/
570 hdac_dma_cb(void *callback_arg, bus_dma_segment_t *segs, int nseg, int error)
572 struct hdac_dma *dma;
575 dma = (struct hdac_dma *)callback_arg;
576 dma->dma_paddr = segs[0].ds_addr;
581 /****************************************************************************
584 * This function allocate and setup a dma region (struct hdac_dma).
585 * It must be freed by a corresponding hdac_dma_free.
586 ****************************************************************************/
588 hdac_dma_alloc(struct hdac_softc *sc, struct hdac_dma *dma, bus_size_t size)
593 roundsz = roundup2(size, HDA_DMA_ALIGNMENT);
594 bzero(dma, sizeof(*dma));
599 result = bus_dma_tag_create(
600 bus_get_dma_tag(sc->dev), /* parent */
601 HDA_DMA_ALIGNMENT, /* alignment */
603 (sc->support_64bit) ? BUS_SPACE_MAXADDR :
604 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
605 BUS_SPACE_MAXADDR, /* highaddr */
607 NULL, /* fistfuncarg */
608 roundsz, /* maxsize */
610 roundsz, /* maxsegsz */
613 NULL, /* lockfuncarg */
614 &dma->dma_tag); /* dmat */
616 device_printf(sc->dev, "%s: bus_dma_tag_create failed (%d)\n",
618 goto hdac_dma_alloc_fail;
622 * Allocate DMA memory
624 result = bus_dmamem_alloc(dma->dma_tag, (void **)&dma->dma_vaddr,
625 BUS_DMA_NOWAIT | BUS_DMA_ZERO |
626 ((sc->flags & HDAC_F_DMA_NOCACHE) ? BUS_DMA_NOCACHE :
630 device_printf(sc->dev, "%s: bus_dmamem_alloc failed (%d)\n",
632 goto hdac_dma_alloc_fail;
635 dma->dma_size = roundsz;
640 result = bus_dmamap_load(dma->dma_tag, dma->dma_map,
641 (void *)dma->dma_vaddr, roundsz, hdac_dma_cb, (void *)dma, 0);
642 if (result != 0 || dma->dma_paddr == 0) {
645 device_printf(sc->dev, "%s: bus_dmamem_load failed (%d)\n",
647 goto hdac_dma_alloc_fail;
651 device_printf(sc->dev, "%s: size=%ju -> roundsz=%ju\n",
652 __func__, (uintmax_t)size, (uintmax_t)roundsz);
658 hdac_dma_free(sc, dma);
663 /****************************************************************************
664 * void hdac_dma_free(struct hdac_softc *, struct hdac_dma *)
666 * Free a struct hdac_dma that has been previously allocated via the
667 * hdac_dma_alloc function.
668 ****************************************************************************/
670 hdac_dma_free(struct hdac_softc *sc, struct hdac_dma *dma)
672 if (dma->dma_paddr != 0) {
674 bus_dmamap_sync(dma->dma_tag, dma->dma_map,
675 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
676 bus_dmamap_unload(dma->dma_tag, dma->dma_map);
679 if (dma->dma_vaddr != NULL) {
680 bus_dmamem_free(dma->dma_tag, dma->dma_vaddr, dma->dma_map);
681 dma->dma_vaddr = NULL;
683 if (dma->dma_tag != NULL) {
684 bus_dma_tag_destroy(dma->dma_tag);
690 /****************************************************************************
691 * int hdac_mem_alloc(struct hdac_softc *)
693 * Allocate all the bus resources necessary to speak with the physical
695 ****************************************************************************/
697 hdac_mem_alloc(struct hdac_softc *sc)
699 struct hdac_mem *mem;
702 mem->mem_rid = PCIR_BAR(0);
703 mem->mem_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
704 &mem->mem_rid, RF_ACTIVE);
705 if (mem->mem_res == NULL) {
706 device_printf(sc->dev,
707 "%s: Unable to allocate memory resource\n", __func__);
710 mem->mem_tag = rman_get_bustag(mem->mem_res);
711 mem->mem_handle = rman_get_bushandle(mem->mem_res);
716 /****************************************************************************
717 * void hdac_mem_free(struct hdac_softc *)
719 * Free up resources previously allocated by hdac_mem_alloc.
720 ****************************************************************************/
722 hdac_mem_free(struct hdac_softc *sc)
724 struct hdac_mem *mem;
727 if (mem->mem_res != NULL)
728 bus_release_resource(sc->dev, SYS_RES_MEMORY, mem->mem_rid,
733 /****************************************************************************
734 * int hdac_irq_alloc(struct hdac_softc *)
736 * Allocate and setup the resources necessary for interrupt handling.
737 ****************************************************************************/
739 hdac_irq_alloc(struct hdac_softc *sc)
741 struct hdac_irq *irq;
747 if ((sc->quirks_off & HDAC_QUIRK_MSI) == 0 &&
748 (result = pci_msi_count(sc->dev)) == 1 &&
749 pci_alloc_msi(sc->dev, &result) == 0)
752 irq->irq_res = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ,
753 &irq->irq_rid, RF_SHAREABLE | RF_ACTIVE);
754 if (irq->irq_res == NULL) {
755 device_printf(sc->dev, "%s: Unable to allocate irq\n",
757 goto hdac_irq_alloc_fail;
759 result = bus_setup_intr(sc->dev, irq->irq_res, INTR_MPSAFE | INTR_TYPE_AV,
760 NULL, hdac_intr_handler, sc, &irq->irq_handle);
762 device_printf(sc->dev,
763 "%s: Unable to setup interrupt handler (%d)\n",
765 goto hdac_irq_alloc_fail;
776 /****************************************************************************
777 * void hdac_irq_free(struct hdac_softc *)
779 * Free up resources previously allocated by hdac_irq_alloc.
780 ****************************************************************************/
782 hdac_irq_free(struct hdac_softc *sc)
784 struct hdac_irq *irq;
787 if (irq->irq_res != NULL && irq->irq_handle != NULL)
788 bus_teardown_intr(sc->dev, irq->irq_res, irq->irq_handle);
789 if (irq->irq_res != NULL)
790 bus_release_resource(sc->dev, SYS_RES_IRQ, irq->irq_rid,
792 if (irq->irq_rid == 0x1)
793 pci_release_msi(sc->dev);
794 irq->irq_handle = NULL;
799 /****************************************************************************
800 * void hdac_corb_init(struct hdac_softc *)
802 * Initialize the corb registers for operations but do not start it up yet.
803 * The CORB engine must not be running when this function is called.
804 ****************************************************************************/
806 hdac_corb_init(struct hdac_softc *sc)
811 /* Setup the CORB size. */
812 switch (sc->corb_size) {
814 corbsize = HDAC_CORBSIZE_CORBSIZE(HDAC_CORBSIZE_CORBSIZE_256);
817 corbsize = HDAC_CORBSIZE_CORBSIZE(HDAC_CORBSIZE_CORBSIZE_16);
820 corbsize = HDAC_CORBSIZE_CORBSIZE(HDAC_CORBSIZE_CORBSIZE_2);
823 panic("%s: Invalid CORB size (%x)\n", __func__, sc->corb_size);
825 HDAC_WRITE_1(&sc->mem, HDAC_CORBSIZE, corbsize);
827 /* Setup the CORB Address in the hdac */
828 corbpaddr = (uint64_t)sc->corb_dma.dma_paddr;
829 HDAC_WRITE_4(&sc->mem, HDAC_CORBLBASE, (uint32_t)corbpaddr);
830 HDAC_WRITE_4(&sc->mem, HDAC_CORBUBASE, (uint32_t)(corbpaddr >> 32));
832 /* Set the WP and RP */
834 HDAC_WRITE_2(&sc->mem, HDAC_CORBWP, sc->corb_wp);
835 HDAC_WRITE_2(&sc->mem, HDAC_CORBRP, HDAC_CORBRP_CORBRPRST);
837 * The HDA specification indicates that the CORBRPRST bit will always
838 * read as zero. Unfortunately, it seems that at least the 82801G
839 * doesn't reset the bit to zero, which stalls the corb engine.
840 * manually reset the bit to zero before continuing.
842 HDAC_WRITE_2(&sc->mem, HDAC_CORBRP, 0x0);
844 /* Enable CORB error reporting */
846 HDAC_WRITE_1(&sc->mem, HDAC_CORBCTL, HDAC_CORBCTL_CMEIE);
850 /****************************************************************************
851 * void hdac_rirb_init(struct hdac_softc *)
853 * Initialize the rirb registers for operations but do not start it up yet.
854 * The RIRB engine must not be running when this function is called.
855 ****************************************************************************/
857 hdac_rirb_init(struct hdac_softc *sc)
862 /* Setup the RIRB size. */
863 switch (sc->rirb_size) {
865 rirbsize = HDAC_RIRBSIZE_RIRBSIZE(HDAC_RIRBSIZE_RIRBSIZE_256);
868 rirbsize = HDAC_RIRBSIZE_RIRBSIZE(HDAC_RIRBSIZE_RIRBSIZE_16);
871 rirbsize = HDAC_RIRBSIZE_RIRBSIZE(HDAC_RIRBSIZE_RIRBSIZE_2);
874 panic("%s: Invalid RIRB size (%x)\n", __func__, sc->rirb_size);
876 HDAC_WRITE_1(&sc->mem, HDAC_RIRBSIZE, rirbsize);
878 /* Setup the RIRB Address in the hdac */
879 rirbpaddr = (uint64_t)sc->rirb_dma.dma_paddr;
880 HDAC_WRITE_4(&sc->mem, HDAC_RIRBLBASE, (uint32_t)rirbpaddr);
881 HDAC_WRITE_4(&sc->mem, HDAC_RIRBUBASE, (uint32_t)(rirbpaddr >> 32));
883 /* Setup the WP and RP */
885 HDAC_WRITE_2(&sc->mem, HDAC_RIRBWP, HDAC_RIRBWP_RIRBWPRST);
887 /* Setup the interrupt threshold */
888 HDAC_WRITE_2(&sc->mem, HDAC_RINTCNT, sc->rirb_size / 2);
890 /* Enable Overrun and response received reporting */
892 HDAC_WRITE_1(&sc->mem, HDAC_RIRBCTL,
893 HDAC_RIRBCTL_RIRBOIC | HDAC_RIRBCTL_RINTCTL);
895 HDAC_WRITE_1(&sc->mem, HDAC_RIRBCTL, HDAC_RIRBCTL_RINTCTL);
899 * Make sure that the Host CPU cache doesn't contain any dirty
900 * cache lines that falls in the rirb. If I understood correctly, it
901 * should be sufficient to do this only once as the rirb is purely
902 * read-only from now on.
904 bus_dmamap_sync(sc->rirb_dma.dma_tag, sc->rirb_dma.dma_map,
905 BUS_DMASYNC_PREREAD);
908 /****************************************************************************
909 * void hdac_corb_start(hdac_softc *)
911 * Startup the corb DMA engine
912 ****************************************************************************/
914 hdac_corb_start(struct hdac_softc *sc)
918 corbctl = HDAC_READ_1(&sc->mem, HDAC_CORBCTL);
919 corbctl |= HDAC_CORBCTL_CORBRUN;
920 HDAC_WRITE_1(&sc->mem, HDAC_CORBCTL, corbctl);
923 /****************************************************************************
924 * void hdac_rirb_start(hdac_softc *)
926 * Startup the rirb DMA engine
927 ****************************************************************************/
929 hdac_rirb_start(struct hdac_softc *sc)
933 rirbctl = HDAC_READ_1(&sc->mem, HDAC_RIRBCTL);
934 rirbctl |= HDAC_RIRBCTL_RIRBDMAEN;
935 HDAC_WRITE_1(&sc->mem, HDAC_RIRBCTL, rirbctl);
939 hdac_rirb_flush(struct hdac_softc *sc)
941 struct hdac_rirb *rirb_base, *rirb;
943 uint32_t resp, resp_ex;
947 rirb_base = (struct hdac_rirb *)sc->rirb_dma.dma_vaddr;
948 rirbwp = HDAC_READ_1(&sc->mem, HDAC_RIRBWP);
949 bus_dmamap_sync(sc->rirb_dma.dma_tag, sc->rirb_dma.dma_map,
950 BUS_DMASYNC_POSTREAD);
953 while (sc->rirb_rp != rirbwp) {
955 sc->rirb_rp %= sc->rirb_size;
956 rirb = &rirb_base[sc->rirb_rp];
957 resp = le32toh(rirb->response);
958 resp_ex = le32toh(rirb->response_ex);
959 cad = HDAC_RIRB_RESPONSE_EX_SDATA_IN(resp_ex);
960 if (resp_ex & HDAC_RIRB_RESPONSE_EX_UNSOLICITED) {
961 sc->unsolq[sc->unsolq_wp++] = resp;
962 sc->unsolq_wp %= HDAC_UNSOLQ_MAX;
963 sc->unsolq[sc->unsolq_wp++] = cad;
964 sc->unsolq_wp %= HDAC_UNSOLQ_MAX;
965 } else if (sc->codecs[cad].pending <= 0) {
966 device_printf(sc->dev, "Unexpected unsolicited "
967 "response from address %d: %08x\n", cad, resp);
969 sc->codecs[cad].response = resp;
970 sc->codecs[cad].pending--;
975 bus_dmamap_sync(sc->rirb_dma.dma_tag, sc->rirb_dma.dma_map,
976 BUS_DMASYNC_PREREAD);
981 hdac_unsolq_flush(struct hdac_softc *sc)
988 if (sc->unsolq_st == HDAC_UNSOLQ_READY) {
989 sc->unsolq_st = HDAC_UNSOLQ_BUSY;
990 while (sc->unsolq_rp != sc->unsolq_wp) {
991 resp = sc->unsolq[sc->unsolq_rp++];
992 sc->unsolq_rp %= HDAC_UNSOLQ_MAX;
993 cad = sc->unsolq[sc->unsolq_rp++];
994 sc->unsolq_rp %= HDAC_UNSOLQ_MAX;
995 if ((child = sc->codecs[cad].dev) != NULL)
996 HDAC_UNSOL_INTR(child, resp);
999 sc->unsolq_st = HDAC_UNSOLQ_READY;
1005 /****************************************************************************
1006 * uint32_t hdac_send_command
1008 * Wrapper function that sends only one command to a given codec
1009 ****************************************************************************/
1011 hdac_send_command(struct hdac_softc *sc, nid_t cad, uint32_t verb)
1016 hdac_lockassert(sc);
1017 verb &= ~HDA_CMD_CAD_MASK;
1018 verb |= ((uint32_t)cad) << HDA_CMD_CAD_SHIFT;
1019 sc->codecs[cad].response = HDA_INVALID;
1021 sc->codecs[cad].pending++;
1023 sc->corb_wp %= sc->corb_size;
1024 corb = (uint32_t *)sc->corb_dma.dma_vaddr;
1025 bus_dmamap_sync(sc->corb_dma.dma_tag,
1026 sc->corb_dma.dma_map, BUS_DMASYNC_PREWRITE);
1027 corb[sc->corb_wp] = htole32(verb);
1028 bus_dmamap_sync(sc->corb_dma.dma_tag,
1029 sc->corb_dma.dma_map, BUS_DMASYNC_POSTWRITE);
1030 HDAC_WRITE_2(&sc->mem, HDAC_CORBWP, sc->corb_wp);
1034 if (hdac_rirb_flush(sc) == 0)
1036 } while (sc->codecs[cad].pending != 0 && --timeout);
1038 if (sc->codecs[cad].pending != 0) {
1039 device_printf(sc->dev, "Command 0x%08x timeout on address %d\n",
1041 sc->codecs[cad].pending = 0;
1044 if (sc->unsolq_rp != sc->unsolq_wp)
1045 taskqueue_enqueue(taskqueue_thread, &sc->unsolq_task);
1046 return (sc->codecs[cad].response);
1049 /****************************************************************************
1051 ****************************************************************************/
1053 /****************************************************************************
1054 * int hdac_probe(device_t)
1056 * Probe for the presence of an hdac. If none is found, check for a generic
1057 * match using the subclass of the device.
1058 ****************************************************************************/
1060 hdac_probe(device_t dev)
1064 uint16_t class, subclass;
1067 model = (uint32_t)pci_get_device(dev) << 16;
1068 model |= (uint32_t)pci_get_vendor(dev) & 0x0000ffff;
1069 class = pci_get_class(dev);
1070 subclass = pci_get_subclass(dev);
1072 bzero(desc, sizeof(desc));
1074 for (i = 0; i < nitems(hdac_devices); i++) {
1075 if (hdac_devices[i].model == model) {
1076 strlcpy(desc, hdac_devices[i].desc, sizeof(desc));
1077 result = BUS_PROBE_DEFAULT;
1080 if (HDA_DEV_MATCH(hdac_devices[i].model, model) &&
1081 class == PCIC_MULTIMEDIA &&
1082 subclass == PCIS_MULTIMEDIA_HDA) {
1083 snprintf(desc, sizeof(desc), "%s (0x%04x)",
1084 hdac_devices[i].desc, pci_get_device(dev));
1085 result = BUS_PROBE_GENERIC;
1089 if (result == ENXIO && class == PCIC_MULTIMEDIA &&
1090 subclass == PCIS_MULTIMEDIA_HDA) {
1091 snprintf(desc, sizeof(desc), "Generic (0x%08x)", model);
1092 result = BUS_PROBE_GENERIC;
1094 if (result != ENXIO) {
1095 strlcat(desc, " HDA Controller", sizeof(desc));
1096 device_set_desc_copy(dev, desc);
1103 hdac_unsolq_task(void *context, int pending)
1105 struct hdac_softc *sc;
1107 sc = (struct hdac_softc *)context;
1110 hdac_unsolq_flush(sc);
1114 /****************************************************************************
1115 * int hdac_attach(device_t)
1117 * Attach the device into the kernel. Interrupts usually won't be enabled
1118 * when this function is called. Setup everything that doesn't require
1119 * interrupts and defer probing of codecs until interrupts are enabled.
1120 ****************************************************************************/
1122 hdac_attach(device_t dev)
1124 struct hdac_softc *sc;
1128 uint16_t class, subclass;
1132 sc = device_get_softc(dev);
1134 device_printf(dev, "PCI card vendor: 0x%04x, device: 0x%04x\n",
1135 pci_get_subvendor(dev), pci_get_subdevice(dev));
1136 device_printf(dev, "HDA Driver Revision: %s\n",
1140 model = (uint32_t)pci_get_device(dev) << 16;
1141 model |= (uint32_t)pci_get_vendor(dev) & 0x0000ffff;
1142 class = pci_get_class(dev);
1143 subclass = pci_get_subclass(dev);
1145 for (i = 0; i < nitems(hdac_devices); i++) {
1146 if (hdac_devices[i].model == model) {
1150 if (HDA_DEV_MATCH(hdac_devices[i].model, model) &&
1151 class == PCIC_MULTIMEDIA &&
1152 subclass == PCIS_MULTIMEDIA_HDA) {
1158 sc->lock = snd_mtxcreate(device_get_nameunit(dev), "HDA driver mutex");
1160 TASK_INIT(&sc->unsolq_task, 0, hdac_unsolq_task, sc);
1161 callout_init(&sc->poll_callout, 1);
1162 for (i = 0; i < HDAC_CODEC_MAX; i++)
1163 sc->codecs[i].dev = NULL;
1165 sc->quirks_on = hdac_devices[devid].quirks_on;
1166 sc->quirks_off = hdac_devices[devid].quirks_off;
1171 if (resource_int_value(device_get_name(dev),
1172 device_get_unit(dev), "msi", &i) == 0) {
1174 sc->quirks_off |= HDAC_QUIRK_MSI;
1176 sc->quirks_on |= HDAC_QUIRK_MSI;
1177 sc->quirks_off |= ~HDAC_QUIRK_MSI;
1180 hdac_config_fetch(sc, &sc->quirks_on, &sc->quirks_off);
1182 device_printf(sc->dev,
1183 "Config options: on=0x%08x off=0x%08x\n",
1184 sc->quirks_on, sc->quirks_off);
1187 if (resource_int_value(device_get_name(dev),
1188 device_get_unit(dev), "polling", &i) == 0 && i != 0)
1193 pci_enable_busmaster(dev);
1195 vendor = pci_get_vendor(dev);
1196 if (vendor == INTEL_VENDORID) {
1198 v = pci_read_config(dev, 0x44, 1);
1199 pci_write_config(dev, 0x44, v & 0xf8, 1);
1201 device_printf(dev, "TCSEL: 0x%02d -> 0x%02d\n", v,
1202 pci_read_config(dev, 0x44, 1));
1206 #if defined(__i386__) || defined(__amd64__)
1207 sc->flags |= HDAC_F_DMA_NOCACHE;
1209 if (resource_int_value(device_get_name(dev),
1210 device_get_unit(dev), "snoop", &i) == 0 && i != 0) {
1212 sc->flags &= ~HDAC_F_DMA_NOCACHE;
1215 * Try to enable PCIe snoop to avoid messing around with
1216 * uncacheable DMA attribute. Since PCIe snoop register
1217 * config is pretty much vendor specific, there are no
1218 * general solutions on how to enable it, forcing us (even
1219 * Microsoft) to enable uncacheable or write combined DMA
1222 * http://msdn2.microsoft.com/en-us/library/ms790324.aspx
1224 for (i = 0; i < nitems(hdac_pcie_snoop); i++) {
1225 if (hdac_pcie_snoop[i].vendor != vendor)
1227 sc->flags &= ~HDAC_F_DMA_NOCACHE;
1228 if (hdac_pcie_snoop[i].reg == 0x00)
1230 v = pci_read_config(dev, hdac_pcie_snoop[i].reg, 1);
1231 if ((v & hdac_pcie_snoop[i].enable) ==
1232 hdac_pcie_snoop[i].enable)
1234 v &= hdac_pcie_snoop[i].mask;
1235 v |= hdac_pcie_snoop[i].enable;
1236 pci_write_config(dev, hdac_pcie_snoop[i].reg, v, 1);
1237 v = pci_read_config(dev, hdac_pcie_snoop[i].reg, 1);
1238 if ((v & hdac_pcie_snoop[i].enable) !=
1239 hdac_pcie_snoop[i].enable) {
1242 "WARNING: Failed to enable PCIe "
1245 #if defined(__i386__) || defined(__amd64__)
1246 sc->flags |= HDAC_F_DMA_NOCACHE;
1251 #if defined(__i386__) || defined(__amd64__)
1256 device_printf(dev, "DMA Coherency: %s / vendor=0x%04x\n",
1257 (sc->flags & HDAC_F_DMA_NOCACHE) ?
1258 "Uncacheable" : "PCIe snoop", vendor);
1261 /* Allocate resources */
1262 result = hdac_mem_alloc(sc);
1264 goto hdac_attach_fail;
1265 result = hdac_irq_alloc(sc);
1267 goto hdac_attach_fail;
1269 /* Get Capabilities */
1270 result = hdac_get_capabilities(sc);
1272 goto hdac_attach_fail;
1274 /* Allocate CORB, RIRB, POS and BDLs dma memory */
1275 result = hdac_dma_alloc(sc, &sc->corb_dma,
1276 sc->corb_size * sizeof(uint32_t));
1278 goto hdac_attach_fail;
1279 result = hdac_dma_alloc(sc, &sc->rirb_dma,
1280 sc->rirb_size * sizeof(struct hdac_rirb));
1282 goto hdac_attach_fail;
1283 sc->streams = malloc(sizeof(struct hdac_stream) * sc->num_ss,
1284 M_HDAC, M_ZERO | M_WAITOK);
1285 for (i = 0; i < sc->num_ss; i++) {
1286 result = hdac_dma_alloc(sc, &sc->streams[i].bdl,
1287 sizeof(struct hdac_bdle) * HDA_BDL_MAX);
1289 goto hdac_attach_fail;
1291 if (sc->quirks_on & HDAC_QUIRK_DMAPOS) {
1292 if (hdac_dma_alloc(sc, &sc->pos_dma, (sc->num_ss) * 8) != 0) {
1294 device_printf(dev, "Failed to "
1295 "allocate DMA pos buffer "
1299 uint64_t addr = sc->pos_dma.dma_paddr;
1301 HDAC_WRITE_4(&sc->mem, HDAC_DPIBUBASE, addr >> 32);
1302 HDAC_WRITE_4(&sc->mem, HDAC_DPIBLBASE,
1303 (addr & HDAC_DPLBASE_DPLBASE_MASK) |
1304 HDAC_DPLBASE_DPLBASE_DMAPBE);
1308 result = bus_dma_tag_create(
1309 bus_get_dma_tag(sc->dev), /* parent */
1310 HDA_DMA_ALIGNMENT, /* alignment */
1312 (sc->support_64bit) ? BUS_SPACE_MAXADDR :
1313 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
1314 BUS_SPACE_MAXADDR, /* highaddr */
1315 NULL, /* filtfunc */
1316 NULL, /* fistfuncarg */
1317 HDA_BUFSZ_MAX, /* maxsize */
1319 HDA_BUFSZ_MAX, /* maxsegsz */
1321 NULL, /* lockfunc */
1322 NULL, /* lockfuncarg */
1323 &sc->chan_dmat); /* dmat */
1325 device_printf(dev, "%s: bus_dma_tag_create failed (%d)\n",
1327 goto hdac_attach_fail;
1330 /* Quiesce everything */
1332 device_printf(dev, "Reset controller...\n");
1334 hdac_reset(sc, true);
1336 /* Initialize the CORB and RIRB */
1340 /* Defer remaining of initialization until interrupts are enabled */
1341 sc->intrhook.ich_func = hdac_attach2;
1342 sc->intrhook.ich_arg = (void *)sc;
1343 if (cold == 0 || config_intrhook_establish(&sc->intrhook) != 0) {
1344 sc->intrhook.ich_func = NULL;
1345 hdac_attach2((void *)sc);
1352 if (sc->streams != NULL)
1353 for (i = 0; i < sc->num_ss; i++)
1354 hdac_dma_free(sc, &sc->streams[i].bdl);
1355 free(sc->streams, M_HDAC);
1356 hdac_dma_free(sc, &sc->rirb_dma);
1357 hdac_dma_free(sc, &sc->corb_dma);
1359 snd_mtxfree(sc->lock);
1365 sysctl_hdac_pindump(SYSCTL_HANDLER_ARGS)
1367 struct hdac_softc *sc;
1370 int devcount, i, err, val;
1372 dev = oidp->oid_arg1;
1373 sc = device_get_softc(dev);
1377 err = sysctl_handle_int(oidp, &val, 0, req);
1378 if (err != 0 || req->newptr == NULL || val == 0)
1381 /* XXX: Temporary. For debugging. */
1385 } else if (val == 101) {
1390 if ((err = device_get_children(dev, &devlist, &devcount)) != 0)
1393 for (i = 0; i < devcount; i++)
1394 HDAC_PINDUMP(devlist[i]);
1396 free(devlist, M_TEMP);
1401 hdac_mdata_rate(uint16_t fmt)
1403 static const int mbits[8] = { 8, 16, 32, 32, 32, 32, 32, 32 };
1406 if (fmt & (1 << 14))
1410 rate *= ((fmt >> 11) & 0x07) + 1;
1411 rate /= ((fmt >> 8) & 0x07) + 1;
1412 bits = mbits[(fmt >> 4) & 0x03];
1413 bits *= (fmt & 0x0f) + 1;
1414 return (rate * bits);
1418 hdac_bdata_rate(uint16_t fmt, int output)
1420 static const int bbits[8] = { 8, 16, 20, 24, 32, 32, 32, 32 };
1424 rate *= ((fmt >> 11) & 0x07) + 1;
1425 bits = bbits[(fmt >> 4) & 0x03];
1426 bits *= (fmt & 0x0f) + 1;
1428 bits = ((bits + 7) & ~0x07) + 10;
1429 return (rate * bits);
1433 hdac_poll_reinit(struct hdac_softc *sc)
1435 int i, pollticks, min = 1000000;
1436 struct hdac_stream *s;
1438 if (sc->polling == 0)
1440 if (sc->unsol_registered > 0)
1442 for (i = 0; i < sc->num_ss; i++) {
1443 s = &sc->streams[i];
1444 if (s->running == 0)
1446 pollticks = ((uint64_t)hz * s->blksz) /
1447 (hdac_mdata_rate(s->format) / 8);
1453 if (min > pollticks)
1456 sc->poll_ival = min;
1458 callout_stop(&sc->poll_callout);
1460 callout_reset(&sc->poll_callout, 1, hdac_poll_callback, sc);
1464 sysctl_hdac_polling(SYSCTL_HANDLER_ARGS)
1466 struct hdac_softc *sc;
1471 dev = oidp->oid_arg1;
1472 sc = device_get_softc(dev);
1478 err = sysctl_handle_int(oidp, &val, 0, req);
1480 if (err != 0 || req->newptr == NULL)
1482 if (val < 0 || val > 1)
1486 if (val != sc->polling) {
1488 callout_stop(&sc->poll_callout);
1490 callout_drain(&sc->poll_callout);
1493 ctl = HDAC_READ_4(&sc->mem, HDAC_INTCTL);
1494 ctl |= HDAC_INTCTL_GIE;
1495 HDAC_WRITE_4(&sc->mem, HDAC_INTCTL, ctl);
1497 ctl = HDAC_READ_4(&sc->mem, HDAC_INTCTL);
1498 ctl &= ~HDAC_INTCTL_GIE;
1499 HDAC_WRITE_4(&sc->mem, HDAC_INTCTL, ctl);
1501 hdac_poll_reinit(sc);
1510 hdac_attach2(void *arg)
1512 struct hdac_softc *sc;
1514 uint32_t vendorid, revisionid;
1518 sc = (struct hdac_softc *)arg;
1522 /* Remove ourselves from the config hooks */
1523 if (sc->intrhook.ich_func != NULL) {
1524 config_intrhook_disestablish(&sc->intrhook);
1525 sc->intrhook.ich_func = NULL;
1529 device_printf(sc->dev, "Starting CORB Engine...\n");
1531 hdac_corb_start(sc);
1533 device_printf(sc->dev, "Starting RIRB Engine...\n");
1535 hdac_rirb_start(sc);
1538 * Clear HDAC_WAKEEN as at present we have no use for SDI wake
1539 * (status change) interrupts. The documentation says that we
1540 * should not make any assumptions about the state of this register
1541 * and set it explicitly.
1542 * NB: this needs to be done before the interrupt is enabled as
1543 * the handler does not expect this interrupt source.
1545 HDAC_WRITE_2(&sc->mem, HDAC_WAKEEN, 0);
1548 * Read and clear post-reset SDI wake status.
1549 * Each set bit corresponds to a codec that came out of reset.
1551 statests = HDAC_READ_2(&sc->mem, HDAC_STATESTS);
1552 HDAC_WRITE_2(&sc->mem, HDAC_STATESTS, statests);
1555 device_printf(sc->dev,
1556 "Enabling controller interrupt...\n");
1558 HDAC_WRITE_4(&sc->mem, HDAC_GCTL, HDAC_READ_4(&sc->mem, HDAC_GCTL) |
1560 if (sc->polling == 0) {
1561 HDAC_WRITE_4(&sc->mem, HDAC_INTCTL,
1562 HDAC_INTCTL_CIE | HDAC_INTCTL_GIE);
1567 device_printf(sc->dev, "Scanning HDA codecs ...\n");
1570 for (i = 0; i < HDAC_CODEC_MAX; i++) {
1571 if (HDAC_STATESTS_SDIWAKE(statests, i)) {
1573 device_printf(sc->dev,
1574 "Found CODEC at address %d\n", i);
1577 vendorid = hdac_send_command(sc, i,
1578 HDA_CMD_GET_PARAMETER(0, 0x0, HDA_PARAM_VENDOR_ID));
1579 revisionid = hdac_send_command(sc, i,
1580 HDA_CMD_GET_PARAMETER(0, 0x0, HDA_PARAM_REVISION_ID));
1582 if (vendorid == HDA_INVALID &&
1583 revisionid == HDA_INVALID) {
1584 device_printf(sc->dev,
1585 "CODEC at address %d not responding!\n", i);
1588 sc->codecs[i].vendor_id =
1589 HDA_PARAM_VENDOR_ID_VENDOR_ID(vendorid);
1590 sc->codecs[i].device_id =
1591 HDA_PARAM_VENDOR_ID_DEVICE_ID(vendorid);
1592 sc->codecs[i].revision_id =
1593 HDA_PARAM_REVISION_ID_REVISION_ID(revisionid);
1594 sc->codecs[i].stepping_id =
1595 HDA_PARAM_REVISION_ID_STEPPING_ID(revisionid);
1596 child = device_add_child(sc->dev, "hdacc", -1);
1597 if (child == NULL) {
1598 device_printf(sc->dev,
1599 "Failed to add CODEC device\n");
1602 device_set_ivars(child, (void *)(intptr_t)i);
1603 sc->codecs[i].dev = child;
1606 bus_generic_attach(sc->dev);
1608 SYSCTL_ADD_PROC(device_get_sysctl_ctx(sc->dev),
1609 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO,
1610 "pindump", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc->dev,
1611 sizeof(sc->dev), sysctl_hdac_pindump, "I", "Dump pin states/data");
1612 SYSCTL_ADD_PROC(device_get_sysctl_ctx(sc->dev),
1613 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO,
1614 "polling", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc->dev,
1615 sizeof(sc->dev), sysctl_hdac_polling, "I", "Enable polling mode");
1618 /****************************************************************************
1619 * int hdac_suspend(device_t)
1621 * Suspend and power down HDA bus and codecs.
1622 ****************************************************************************/
1624 hdac_suspend(device_t dev)
1626 struct hdac_softc *sc = device_get_softc(dev);
1629 device_printf(dev, "Suspend...\n");
1631 bus_generic_suspend(dev);
1635 device_printf(dev, "Reset controller...\n");
1637 callout_stop(&sc->poll_callout);
1638 hdac_reset(sc, false);
1640 callout_drain(&sc->poll_callout);
1641 taskqueue_drain(taskqueue_thread, &sc->unsolq_task);
1643 device_printf(dev, "Suspend done\n");
1648 /****************************************************************************
1649 * int hdac_resume(device_t)
1651 * Powerup and restore HDA bus and codecs state.
1652 ****************************************************************************/
1654 hdac_resume(device_t dev)
1656 struct hdac_softc *sc = device_get_softc(dev);
1660 device_printf(dev, "Resume...\n");
1664 /* Quiesce everything */
1666 device_printf(dev, "Reset controller...\n");
1668 hdac_reset(sc, true);
1670 /* Initialize the CORB and RIRB */
1675 device_printf(dev, "Starting CORB Engine...\n");
1677 hdac_corb_start(sc);
1679 device_printf(dev, "Starting RIRB Engine...\n");
1681 hdac_rirb_start(sc);
1684 * Clear HDAC_WAKEEN as at present we have no use for SDI wake
1685 * (status change) events. The documentation says that we should
1686 * not make any assumptions about the state of this register and
1687 * set it explicitly.
1688 * Also, clear HDAC_STATESTS.
1689 * NB: this needs to be done before the interrupt is enabled as
1690 * the handler does not expect this interrupt source.
1692 HDAC_WRITE_2(&sc->mem, HDAC_WAKEEN, 0);
1693 HDAC_WRITE_2(&sc->mem, HDAC_STATESTS, HDAC_STATESTS_SDIWAKE_MASK);
1696 device_printf(dev, "Enabling controller interrupt...\n");
1698 HDAC_WRITE_4(&sc->mem, HDAC_GCTL, HDAC_READ_4(&sc->mem, HDAC_GCTL) |
1700 HDAC_WRITE_4(&sc->mem, HDAC_INTCTL, HDAC_INTCTL_CIE | HDAC_INTCTL_GIE);
1702 hdac_poll_reinit(sc);
1705 error = bus_generic_resume(dev);
1707 device_printf(dev, "Resume done\n");
1712 /****************************************************************************
1713 * int hdac_detach(device_t)
1715 * Detach and free up resources utilized by the hdac device.
1716 ****************************************************************************/
1718 hdac_detach(device_t dev)
1720 struct hdac_softc *sc = device_get_softc(dev);
1722 int cad, i, devcount, error;
1724 if ((error = device_get_children(dev, &devlist, &devcount)) != 0)
1726 for (i = 0; i < devcount; i++) {
1727 cad = (intptr_t)device_get_ivars(devlist[i]);
1728 if ((error = device_delete_child(dev, devlist[i])) != 0) {
1729 free(devlist, M_TEMP);
1732 sc->codecs[cad].dev = NULL;
1734 free(devlist, M_TEMP);
1737 hdac_reset(sc, false);
1739 taskqueue_drain(taskqueue_thread, &sc->unsolq_task);
1742 for (i = 0; i < sc->num_ss; i++)
1743 hdac_dma_free(sc, &sc->streams[i].bdl);
1744 free(sc->streams, M_HDAC);
1745 hdac_dma_free(sc, &sc->pos_dma);
1746 hdac_dma_free(sc, &sc->rirb_dma);
1747 hdac_dma_free(sc, &sc->corb_dma);
1748 if (sc->chan_dmat != NULL) {
1749 bus_dma_tag_destroy(sc->chan_dmat);
1750 sc->chan_dmat = NULL;
1753 snd_mtxfree(sc->lock);
1757 static bus_dma_tag_t
1758 hdac_get_dma_tag(device_t dev, device_t child)
1760 struct hdac_softc *sc = device_get_softc(dev);
1762 return (sc->chan_dmat);
1766 hdac_print_child(device_t dev, device_t child)
1770 retval = bus_print_child_header(dev, child);
1771 retval += printf(" at cad %d", (int)(intptr_t)device_get_ivars(child));
1772 retval += bus_print_child_footer(dev, child);
1778 hdac_child_location_str(device_t dev, device_t child, char *buf, size_t buflen)
1781 snprintf(buf, buflen, "cad=%d", (int)(intptr_t)device_get_ivars(child));
1786 hdac_child_pnpinfo_str_method(device_t dev, device_t child, char *buf,
1789 struct hdac_softc *sc = device_get_softc(dev);
1790 nid_t cad = (uintptr_t)device_get_ivars(child);
1792 snprintf(buf, buflen,
1793 "vendor=0x%04x device=0x%04x revision=0x%02x stepping=0x%02x",
1794 sc->codecs[cad].vendor_id, sc->codecs[cad].device_id,
1795 sc->codecs[cad].revision_id, sc->codecs[cad].stepping_id);
1800 hdac_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
1802 struct hdac_softc *sc = device_get_softc(dev);
1803 nid_t cad = (uintptr_t)device_get_ivars(child);
1806 case HDA_IVAR_CODEC_ID:
1809 case HDA_IVAR_VENDOR_ID:
1810 *result = sc->codecs[cad].vendor_id;
1812 case HDA_IVAR_DEVICE_ID:
1813 *result = sc->codecs[cad].device_id;
1815 case HDA_IVAR_REVISION_ID:
1816 *result = sc->codecs[cad].revision_id;
1818 case HDA_IVAR_STEPPING_ID:
1819 *result = sc->codecs[cad].stepping_id;
1821 case HDA_IVAR_SUBVENDOR_ID:
1822 *result = pci_get_subvendor(dev);
1824 case HDA_IVAR_SUBDEVICE_ID:
1825 *result = pci_get_subdevice(dev);
1827 case HDA_IVAR_DMA_NOCACHE:
1828 *result = (sc->flags & HDAC_F_DMA_NOCACHE) != 0;
1830 case HDA_IVAR_STRIPES_MASK:
1831 *result = (1 << (1 << sc->num_sdo)) - 1;
1840 hdac_get_mtx(device_t dev, device_t child)
1842 struct hdac_softc *sc = device_get_softc(dev);
1848 hdac_codec_command(device_t dev, device_t child, uint32_t verb)
1851 return (hdac_send_command(device_get_softc(dev),
1852 (intptr_t)device_get_ivars(child), verb));
1856 hdac_find_stream(struct hdac_softc *sc, int dir, int stream)
1861 /* Allocate ISS/OSS first. */
1863 for (i = 0; i < sc->num_iss; i++) {
1864 if (sc->streams[i].stream == stream) {
1870 for (i = 0; i < sc->num_oss; i++) {
1871 if (sc->streams[i + sc->num_iss].stream == stream) {
1872 ss = i + sc->num_iss;
1877 /* Fallback to BSS. */
1879 for (i = 0; i < sc->num_bss; i++) {
1880 if (sc->streams[i + sc->num_iss + sc->num_oss].stream
1882 ss = i + sc->num_iss + sc->num_oss;
1891 hdac_stream_alloc(device_t dev, device_t child, int dir, int format, int stripe,
1894 struct hdac_softc *sc = device_get_softc(dev);
1895 nid_t cad = (uintptr_t)device_get_ivars(child);
1896 int stream, ss, bw, maxbw, prevbw;
1898 /* Look for empty stream. */
1899 ss = hdac_find_stream(sc, dir, 0);
1901 /* Return if found nothing. */
1905 /* Check bus bandwidth. */
1906 bw = hdac_bdata_rate(format, dir);
1908 bw *= 1 << (sc->num_sdo - stripe);
1909 prevbw = sc->sdo_bw_used;
1910 maxbw = 48000 * 960 * (1 << sc->num_sdo);
1912 prevbw = sc->codecs[cad].sdi_bw_used;
1913 maxbw = 48000 * 464;
1916 device_printf(dev, "%dKbps of %dKbps bandwidth used%s\n",
1917 (bw + prevbw) / 1000, maxbw / 1000,
1918 bw + prevbw > maxbw ? " -- OVERFLOW!" : "");
1920 if (bw + prevbw > maxbw)
1923 sc->sdo_bw_used += bw;
1925 sc->codecs[cad].sdi_bw_used += bw;
1927 /* Allocate stream number */
1928 if (ss >= sc->num_iss + sc->num_oss)
1929 stream = 15 - (ss - sc->num_iss - sc->num_oss);
1930 else if (ss >= sc->num_iss)
1931 stream = ss - sc->num_iss + 1;
1935 sc->streams[ss].dev = child;
1936 sc->streams[ss].dir = dir;
1937 sc->streams[ss].stream = stream;
1938 sc->streams[ss].bw = bw;
1939 sc->streams[ss].format = format;
1940 sc->streams[ss].stripe = stripe;
1941 if (dmapos != NULL) {
1942 if (sc->pos_dma.dma_vaddr != NULL)
1943 *dmapos = (uint32_t *)(sc->pos_dma.dma_vaddr + ss * 8);
1951 hdac_stream_free(device_t dev, device_t child, int dir, int stream)
1953 struct hdac_softc *sc = device_get_softc(dev);
1954 nid_t cad = (uintptr_t)device_get_ivars(child);
1957 ss = hdac_find_stream(sc, dir, stream);
1959 ("Free for not allocated stream (%d/%d)\n", dir, stream));
1961 sc->sdo_bw_used -= sc->streams[ss].bw;
1963 sc->codecs[cad].sdi_bw_used -= sc->streams[ss].bw;
1964 sc->streams[ss].stream = 0;
1965 sc->streams[ss].dev = NULL;
1969 hdac_stream_start(device_t dev, device_t child, int dir, int stream,
1970 bus_addr_t buf, int blksz, int blkcnt)
1972 struct hdac_softc *sc = device_get_softc(dev);
1973 struct hdac_bdle *bdle;
1978 ss = hdac_find_stream(sc, dir, stream);
1980 ("Start for not allocated stream (%d/%d)\n", dir, stream));
1982 addr = (uint64_t)buf;
1983 bdle = (struct hdac_bdle *)sc->streams[ss].bdl.dma_vaddr;
1984 for (i = 0; i < blkcnt; i++, bdle++) {
1985 bdle->addrl = htole32((uint32_t)addr);
1986 bdle->addrh = htole32((uint32_t)(addr >> 32));
1987 bdle->len = htole32(blksz);
1988 bdle->ioc = htole32(1);
1992 bus_dmamap_sync(sc->streams[ss].bdl.dma_tag,
1993 sc->streams[ss].bdl.dma_map, BUS_DMASYNC_PREWRITE);
1996 HDAC_WRITE_4(&sc->mem, off + HDAC_SDCBL, blksz * blkcnt);
1997 HDAC_WRITE_2(&sc->mem, off + HDAC_SDLVI, blkcnt - 1);
1998 addr = sc->streams[ss].bdl.dma_paddr;
1999 HDAC_WRITE_4(&sc->mem, off + HDAC_SDBDPL, (uint32_t)addr);
2000 HDAC_WRITE_4(&sc->mem, off + HDAC_SDBDPU, (uint32_t)(addr >> 32));
2002 ctl = HDAC_READ_1(&sc->mem, off + HDAC_SDCTL2);
2004 ctl |= HDAC_SDCTL2_DIR;
2006 ctl &= ~HDAC_SDCTL2_DIR;
2007 ctl &= ~HDAC_SDCTL2_STRM_MASK;
2008 ctl |= stream << HDAC_SDCTL2_STRM_SHIFT;
2009 ctl &= ~HDAC_SDCTL2_STRIPE_MASK;
2010 ctl |= sc->streams[ss].stripe << HDAC_SDCTL2_STRIPE_SHIFT;
2011 HDAC_WRITE_1(&sc->mem, off + HDAC_SDCTL2, ctl);
2013 HDAC_WRITE_2(&sc->mem, off + HDAC_SDFMT, sc->streams[ss].format);
2015 ctl = HDAC_READ_4(&sc->mem, HDAC_INTCTL);
2017 HDAC_WRITE_4(&sc->mem, HDAC_INTCTL, ctl);
2019 HDAC_WRITE_1(&sc->mem, off + HDAC_SDSTS,
2020 HDAC_SDSTS_DESE | HDAC_SDSTS_FIFOE | HDAC_SDSTS_BCIS);
2021 ctl = HDAC_READ_1(&sc->mem, off + HDAC_SDCTL0);
2022 ctl |= HDAC_SDCTL_IOCE | HDAC_SDCTL_FEIE | HDAC_SDCTL_DEIE |
2024 HDAC_WRITE_1(&sc->mem, off + HDAC_SDCTL0, ctl);
2026 sc->streams[ss].blksz = blksz;
2027 sc->streams[ss].running = 1;
2028 hdac_poll_reinit(sc);
2033 hdac_stream_stop(device_t dev, device_t child, int dir, int stream)
2035 struct hdac_softc *sc = device_get_softc(dev);
2039 ss = hdac_find_stream(sc, dir, stream);
2041 ("Stop for not allocated stream (%d/%d)\n", dir, stream));
2043 bus_dmamap_sync(sc->streams[ss].bdl.dma_tag,
2044 sc->streams[ss].bdl.dma_map, BUS_DMASYNC_POSTWRITE);
2047 ctl = HDAC_READ_1(&sc->mem, off + HDAC_SDCTL0);
2048 ctl &= ~(HDAC_SDCTL_IOCE | HDAC_SDCTL_FEIE | HDAC_SDCTL_DEIE |
2050 HDAC_WRITE_1(&sc->mem, off + HDAC_SDCTL0, ctl);
2052 ctl = HDAC_READ_4(&sc->mem, HDAC_INTCTL);
2054 HDAC_WRITE_4(&sc->mem, HDAC_INTCTL, ctl);
2056 sc->streams[ss].running = 0;
2057 hdac_poll_reinit(sc);
2061 hdac_stream_reset(device_t dev, device_t child, int dir, int stream)
2063 struct hdac_softc *sc = device_get_softc(dev);
2069 ss = hdac_find_stream(sc, dir, stream);
2071 ("Reset for not allocated stream (%d/%d)\n", dir, stream));
2074 ctl = HDAC_READ_1(&sc->mem, off + HDAC_SDCTL0);
2075 ctl |= HDAC_SDCTL_SRST;
2076 HDAC_WRITE_1(&sc->mem, off + HDAC_SDCTL0, ctl);
2078 ctl = HDAC_READ_1(&sc->mem, off + HDAC_SDCTL0);
2079 if (ctl & HDAC_SDCTL_SRST)
2083 if (!(ctl & HDAC_SDCTL_SRST))
2084 device_printf(dev, "Reset setting timeout\n");
2085 ctl &= ~HDAC_SDCTL_SRST;
2086 HDAC_WRITE_1(&sc->mem, off + HDAC_SDCTL0, ctl);
2089 ctl = HDAC_READ_1(&sc->mem, off + HDAC_SDCTL0);
2090 if (!(ctl & HDAC_SDCTL_SRST))
2094 if (ctl & HDAC_SDCTL_SRST)
2095 device_printf(dev, "Reset timeout!\n");
2099 hdac_stream_getptr(device_t dev, device_t child, int dir, int stream)
2101 struct hdac_softc *sc = device_get_softc(dev);
2104 ss = hdac_find_stream(sc, dir, stream);
2106 ("Reset for not allocated stream (%d/%d)\n", dir, stream));
2109 return (HDAC_READ_4(&sc->mem, off + HDAC_SDLPIB));
2113 hdac_unsol_alloc(device_t dev, device_t child, int tag)
2115 struct hdac_softc *sc = device_get_softc(dev);
2117 sc->unsol_registered++;
2118 hdac_poll_reinit(sc);
2123 hdac_unsol_free(device_t dev, device_t child, int tag)
2125 struct hdac_softc *sc = device_get_softc(dev);
2127 sc->unsol_registered--;
2128 hdac_poll_reinit(sc);
2131 static device_method_t hdac_methods[] = {
2132 /* device interface */
2133 DEVMETHOD(device_probe, hdac_probe),
2134 DEVMETHOD(device_attach, hdac_attach),
2135 DEVMETHOD(device_detach, hdac_detach),
2136 DEVMETHOD(device_suspend, hdac_suspend),
2137 DEVMETHOD(device_resume, hdac_resume),
2139 DEVMETHOD(bus_get_dma_tag, hdac_get_dma_tag),
2140 DEVMETHOD(bus_print_child, hdac_print_child),
2141 DEVMETHOD(bus_child_location_str, hdac_child_location_str),
2142 DEVMETHOD(bus_child_pnpinfo_str, hdac_child_pnpinfo_str_method),
2143 DEVMETHOD(bus_read_ivar, hdac_read_ivar),
2144 DEVMETHOD(hdac_get_mtx, hdac_get_mtx),
2145 DEVMETHOD(hdac_codec_command, hdac_codec_command),
2146 DEVMETHOD(hdac_stream_alloc, hdac_stream_alloc),
2147 DEVMETHOD(hdac_stream_free, hdac_stream_free),
2148 DEVMETHOD(hdac_stream_start, hdac_stream_start),
2149 DEVMETHOD(hdac_stream_stop, hdac_stream_stop),
2150 DEVMETHOD(hdac_stream_reset, hdac_stream_reset),
2151 DEVMETHOD(hdac_stream_getptr, hdac_stream_getptr),
2152 DEVMETHOD(hdac_unsol_alloc, hdac_unsol_alloc),
2153 DEVMETHOD(hdac_unsol_free, hdac_unsol_free),
2157 static driver_t hdac_driver = {
2160 sizeof(struct hdac_softc),
2163 static devclass_t hdac_devclass;
2165 DRIVER_MODULE(snd_hda, pci, hdac_driver, hdac_devclass, NULL, NULL);