1 // SPDX-License-Identifier: GPL-2.0-only
3 * HD-audio controller helpers
6 #include <linux/kernel.h>
7 #include <linux/delay.h>
8 #include <linux/export.h>
9 #include <sound/core.h>
10 #include <sound/hdaudio.h>
11 #include <sound/hda_register.h>
14 /* clear CORB read pointer properly */
15 static void azx_clear_corbrp(struct hdac_bus *bus)
19 for (timeout = 1000; timeout > 0; timeout--) {
20 if (snd_hdac_chip_readw(bus, CORBRP) & AZX_CORBRP_RST)
25 dev_err(bus->dev, "CORB reset timeout#1, CORBRP = %d\n",
26 snd_hdac_chip_readw(bus, CORBRP));
28 snd_hdac_chip_writew(bus, CORBRP, 0);
29 for (timeout = 1000; timeout > 0; timeout--) {
30 if (snd_hdac_chip_readw(bus, CORBRP) == 0)
35 dev_err(bus->dev, "CORB reset timeout#2, CORBRP = %d\n",
36 snd_hdac_chip_readw(bus, CORBRP));
40 * snd_hdac_bus_init_cmd_io - set up CORB/RIRB buffers
41 * @bus: HD-audio core bus
43 void snd_hdac_bus_init_cmd_io(struct hdac_bus *bus)
45 WARN_ON_ONCE(!bus->rb.area);
47 spin_lock_irq(&bus->reg_lock);
49 bus->corb.addr = bus->rb.addr;
50 bus->corb.buf = (__le32 *)bus->rb.area;
51 snd_hdac_chip_writel(bus, CORBLBASE, (u32)bus->corb.addr);
52 snd_hdac_chip_writel(bus, CORBUBASE, upper_32_bits(bus->corb.addr));
54 /* set the corb size to 256 entries (ULI requires explicitly) */
55 snd_hdac_chip_writeb(bus, CORBSIZE, 0x02);
56 /* set the corb write pointer to 0 */
57 snd_hdac_chip_writew(bus, CORBWP, 0);
59 /* reset the corb hw read pointer */
60 snd_hdac_chip_writew(bus, CORBRP, AZX_CORBRP_RST);
61 if (!bus->corbrp_self_clear)
62 azx_clear_corbrp(bus);
65 snd_hdac_chip_writeb(bus, CORBCTL, AZX_CORBCTL_RUN);
68 bus->rirb.addr = bus->rb.addr + 2048;
69 bus->rirb.buf = (__le32 *)(bus->rb.area + 2048);
70 bus->rirb.wp = bus->rirb.rp = 0;
71 memset(bus->rirb.cmds, 0, sizeof(bus->rirb.cmds));
72 snd_hdac_chip_writel(bus, RIRBLBASE, (u32)bus->rirb.addr);
73 snd_hdac_chip_writel(bus, RIRBUBASE, upper_32_bits(bus->rirb.addr));
75 /* set the rirb size to 256 entries (ULI requires explicitly) */
76 snd_hdac_chip_writeb(bus, RIRBSIZE, 0x02);
77 /* reset the rirb hw write pointer */
78 snd_hdac_chip_writew(bus, RIRBWP, AZX_RIRBWP_RST);
79 /* set N=1, get RIRB response interrupt for new entry */
80 snd_hdac_chip_writew(bus, RINTCNT, 1);
81 /* enable rirb dma and response irq */
82 if (bus->not_use_interrupts)
83 snd_hdac_chip_writeb(bus, RIRBCTL, AZX_RBCTL_DMA_EN);
85 snd_hdac_chip_writeb(bus, RIRBCTL, AZX_RBCTL_DMA_EN | AZX_RBCTL_IRQ_EN);
86 /* Accept unsolicited responses */
87 snd_hdac_chip_updatel(bus, GCTL, AZX_GCTL_UNSOL, AZX_GCTL_UNSOL);
88 spin_unlock_irq(&bus->reg_lock);
90 EXPORT_SYMBOL_GPL(snd_hdac_bus_init_cmd_io);
92 /* wait for cmd dmas till they are stopped */
93 static void hdac_wait_for_cmd_dmas(struct hdac_bus *bus)
95 unsigned long timeout;
97 timeout = jiffies + msecs_to_jiffies(100);
98 while ((snd_hdac_chip_readb(bus, RIRBCTL) & AZX_RBCTL_DMA_EN)
99 && time_before(jiffies, timeout))
102 timeout = jiffies + msecs_to_jiffies(100);
103 while ((snd_hdac_chip_readb(bus, CORBCTL) & AZX_CORBCTL_RUN)
104 && time_before(jiffies, timeout))
109 * snd_hdac_bus_stop_cmd_io - clean up CORB/RIRB buffers
110 * @bus: HD-audio core bus
112 void snd_hdac_bus_stop_cmd_io(struct hdac_bus *bus)
114 spin_lock_irq(&bus->reg_lock);
115 /* disable ringbuffer DMAs */
116 snd_hdac_chip_writeb(bus, RIRBCTL, 0);
117 snd_hdac_chip_writeb(bus, CORBCTL, 0);
118 spin_unlock_irq(&bus->reg_lock);
120 hdac_wait_for_cmd_dmas(bus);
122 spin_lock_irq(&bus->reg_lock);
123 /* disable unsolicited responses */
124 snd_hdac_chip_updatel(bus, GCTL, AZX_GCTL_UNSOL, 0);
125 spin_unlock_irq(&bus->reg_lock);
127 EXPORT_SYMBOL_GPL(snd_hdac_bus_stop_cmd_io);
129 static unsigned int azx_command_addr(u32 cmd)
131 unsigned int addr = cmd >> 28;
133 if (snd_BUG_ON(addr >= HDA_MAX_CODECS))
139 * snd_hdac_bus_send_cmd - send a command verb via CORB
140 * @bus: HD-audio core bus
141 * @val: encoded verb value to send
143 * Returns zero for success or a negative error code.
145 int snd_hdac_bus_send_cmd(struct hdac_bus *bus, unsigned int val)
147 unsigned int addr = azx_command_addr(val);
150 spin_lock_irq(&bus->reg_lock);
152 bus->last_cmd[azx_command_addr(val)] = val;
154 /* add command to corb */
155 wp = snd_hdac_chip_readw(bus, CORBWP);
157 /* something wrong, controller likely turned to D3 */
158 spin_unlock_irq(&bus->reg_lock);
162 wp %= AZX_MAX_CORB_ENTRIES;
164 rp = snd_hdac_chip_readw(bus, CORBRP);
166 /* oops, it's full */
167 spin_unlock_irq(&bus->reg_lock);
171 bus->rirb.cmds[addr]++;
172 bus->corb.buf[wp] = cpu_to_le32(val);
173 snd_hdac_chip_writew(bus, CORBWP, wp);
175 spin_unlock_irq(&bus->reg_lock);
179 EXPORT_SYMBOL_GPL(snd_hdac_bus_send_cmd);
181 #define AZX_RIRB_EX_UNSOL_EV (1<<4)
184 * snd_hdac_bus_update_rirb - retrieve RIRB entries
185 * @bus: HD-audio core bus
187 * Usually called from interrupt handler.
188 * The caller needs bus->reg_lock spinlock before calling this.
190 void snd_hdac_bus_update_rirb(struct hdac_bus *bus)
196 wp = snd_hdac_chip_readw(bus, RIRBWP);
198 /* something wrong, controller likely turned to D3 */
202 if (wp == bus->rirb.wp)
206 while (bus->rirb.rp != wp) {
208 bus->rirb.rp %= AZX_MAX_RIRB_ENTRIES;
210 rp = bus->rirb.rp << 1; /* an RIRB entry is 8-bytes */
211 res_ex = le32_to_cpu(bus->rirb.buf[rp + 1]);
212 res = le32_to_cpu(bus->rirb.buf[rp]);
214 if (addr >= HDA_MAX_CODECS) {
216 "spurious response %#x:%#x, rp = %d, wp = %d",
217 res, res_ex, bus->rirb.rp, wp);
219 } else if (res_ex & AZX_RIRB_EX_UNSOL_EV)
220 snd_hdac_bus_queue_event(bus, res, res_ex);
221 else if (bus->rirb.cmds[addr]) {
222 bus->rirb.res[addr] = res;
223 bus->rirb.cmds[addr]--;
224 if (!bus->rirb.cmds[addr] &&
225 waitqueue_active(&bus->rirb_wq))
226 wake_up(&bus->rirb_wq);
228 dev_err_ratelimited(bus->dev,
229 "spurious response %#x:%#x, last cmd=%#08x\n",
230 res, res_ex, bus->last_cmd[addr]);
234 EXPORT_SYMBOL_GPL(snd_hdac_bus_update_rirb);
237 * snd_hdac_bus_get_response - receive a response via RIRB
238 * @bus: HD-audio core bus
239 * @addr: codec address
240 * @res: pointer to store the value, NULL when not needed
242 * Returns zero if a value is read, or a negative error code.
244 int snd_hdac_bus_get_response(struct hdac_bus *bus, unsigned int addr,
247 unsigned long timeout;
248 unsigned long loopcounter;
249 wait_queue_entry_t wait;
252 init_wait_entry(&wait, 0);
253 timeout = jiffies + msecs_to_jiffies(1000);
255 for (loopcounter = 0;; loopcounter++) {
256 spin_lock_irq(&bus->reg_lock);
257 if (!bus->polling_mode)
258 prepare_to_wait(&bus->rirb_wq, &wait,
259 TASK_UNINTERRUPTIBLE);
260 if (bus->polling_mode)
261 snd_hdac_bus_update_rirb(bus);
262 if (!bus->rirb.cmds[addr]) {
264 *res = bus->rirb.res[addr]; /* the last value */
265 if (!bus->polling_mode)
266 finish_wait(&bus->rirb_wq, &wait);
267 spin_unlock_irq(&bus->reg_lock);
270 spin_unlock_irq(&bus->reg_lock);
271 if (time_after(jiffies, timeout))
273 #define LOOP_COUNT_MAX 3000
274 if (!bus->polling_mode) {
275 schedule_timeout(msecs_to_jiffies(2));
276 } else if (bus->needs_damn_long_delay ||
277 loopcounter > LOOP_COUNT_MAX) {
278 if (loopcounter > LOOP_COUNT_MAX && !warned) {
279 dev_dbg_ratelimited(bus->dev,
280 "too slow response, last cmd=%#08x\n",
281 bus->last_cmd[addr]);
284 msleep(2); /* temporary workaround */
291 if (!bus->polling_mode)
292 finish_wait(&bus->rirb_wq, &wait);
296 EXPORT_SYMBOL_GPL(snd_hdac_bus_get_response);
298 #define HDAC_MAX_CAPS 10
300 * snd_hdac_bus_parse_capabilities - parse capability structure
301 * @bus: the pointer to bus object
303 * Returns 0 if successful, or a negative error code.
305 int snd_hdac_bus_parse_capabilities(struct hdac_bus *bus)
307 unsigned int cur_cap;
309 unsigned int counter = 0;
311 offset = snd_hdac_chip_readw(bus, LLCH);
313 /* Lets walk the linked capabilities list */
315 cur_cap = _snd_hdac_chip_readl(bus, offset);
317 dev_dbg(bus->dev, "Capability version: 0x%x\n",
318 (cur_cap & AZX_CAP_HDR_VER_MASK) >> AZX_CAP_HDR_VER_OFF);
320 dev_dbg(bus->dev, "HDA capability ID: 0x%x\n",
321 (cur_cap & AZX_CAP_HDR_ID_MASK) >> AZX_CAP_HDR_ID_OFF);
324 dev_dbg(bus->dev, "Invalid capability reg read\n");
328 switch ((cur_cap & AZX_CAP_HDR_ID_MASK) >> AZX_CAP_HDR_ID_OFF) {
330 dev_dbg(bus->dev, "Found ML capability\n");
331 bus->mlcap = bus->remap_addr + offset;
335 dev_dbg(bus->dev, "Found GTS capability offset=%x\n", offset);
336 bus->gtscap = bus->remap_addr + offset;
340 /* PP capability found, the Audio DSP is present */
341 dev_dbg(bus->dev, "Found PP capability offset=%x\n", offset);
342 bus->ppcap = bus->remap_addr + offset;
346 /* SPIB capability found, handler function */
347 dev_dbg(bus->dev, "Found SPB capability\n");
348 bus->spbcap = bus->remap_addr + offset;
351 case AZX_DRSM_CAP_ID:
352 /* DMA resume capability found, handler function */
353 dev_dbg(bus->dev, "Found DRSM capability\n");
354 bus->drsmcap = bus->remap_addr + offset;
358 dev_err(bus->dev, "Unknown capability %d\n", cur_cap);
365 if (counter > HDAC_MAX_CAPS) {
366 dev_err(bus->dev, "We exceeded HDAC capabilities!!!\n");
370 /* read the offset of next capability */
371 offset = cur_cap & AZX_CAP_HDR_NXT_PTR_MASK;
377 EXPORT_SYMBOL_GPL(snd_hdac_bus_parse_capabilities);
384 * snd_hdac_bus_enter_link_reset - enter link reset
385 * @bus: HD-audio core bus
387 * Enter to the link reset state.
389 void snd_hdac_bus_enter_link_reset(struct hdac_bus *bus)
391 unsigned long timeout;
393 /* reset controller */
394 snd_hdac_chip_updatel(bus, GCTL, AZX_GCTL_RESET, 0);
396 timeout = jiffies + msecs_to_jiffies(100);
397 while ((snd_hdac_chip_readb(bus, GCTL) & AZX_GCTL_RESET) &&
398 time_before(jiffies, timeout))
399 usleep_range(500, 1000);
401 EXPORT_SYMBOL_GPL(snd_hdac_bus_enter_link_reset);
404 * snd_hdac_bus_exit_link_reset - exit link reset
405 * @bus: HD-audio core bus
407 * Exit from the link reset state.
409 void snd_hdac_bus_exit_link_reset(struct hdac_bus *bus)
411 unsigned long timeout;
413 snd_hdac_chip_updateb(bus, GCTL, AZX_GCTL_RESET, AZX_GCTL_RESET);
415 timeout = jiffies + msecs_to_jiffies(100);
416 while (!snd_hdac_chip_readb(bus, GCTL) && time_before(jiffies, timeout))
417 usleep_range(500, 1000);
419 EXPORT_SYMBOL_GPL(snd_hdac_bus_exit_link_reset);
421 /* reset codec link */
422 int snd_hdac_bus_reset_link(struct hdac_bus *bus, bool full_reset)
427 /* clear STATESTS if not in reset */
428 if (snd_hdac_chip_readb(bus, GCTL) & AZX_GCTL_RESET)
429 snd_hdac_chip_writew(bus, STATESTS, STATESTS_INT_MASK);
431 /* reset controller */
432 snd_hdac_bus_enter_link_reset(bus);
434 /* delay for >= 100us for codec PLL to settle per spec
435 * Rev 0.9 section 5.5.1
437 usleep_range(500, 1000);
439 /* Bring controller out of reset */
440 snd_hdac_bus_exit_link_reset(bus);
442 /* Brent Chartrand said to wait >= 540us for codecs to initialize */
443 usleep_range(1000, 1200);
446 /* check to see if controller is ready */
447 if (!snd_hdac_chip_readb(bus, GCTL)) {
448 dev_dbg(bus->dev, "controller not ready!\n");
453 if (!bus->codec_mask) {
454 bus->codec_mask = snd_hdac_chip_readw(bus, STATESTS);
455 dev_dbg(bus->dev, "codec_mask = 0x%lx\n", bus->codec_mask);
460 EXPORT_SYMBOL_GPL(snd_hdac_bus_reset_link);
462 /* enable interrupts */
463 static void azx_int_enable(struct hdac_bus *bus)
465 /* enable controller CIE and GIE */
466 snd_hdac_chip_updatel(bus, INTCTL,
467 AZX_INT_CTRL_EN | AZX_INT_GLOBAL_EN,
468 AZX_INT_CTRL_EN | AZX_INT_GLOBAL_EN);
471 /* disable interrupts */
472 static void azx_int_disable(struct hdac_bus *bus)
474 struct hdac_stream *azx_dev;
476 /* disable interrupts in stream descriptor */
477 list_for_each_entry(azx_dev, &bus->stream_list, list)
478 snd_hdac_stream_updateb(azx_dev, SD_CTL, SD_INT_MASK, 0);
480 /* disable SIE for all streams & disable controller CIE and GIE */
481 snd_hdac_chip_writel(bus, INTCTL, 0);
484 /* clear interrupts */
485 static void azx_int_clear(struct hdac_bus *bus)
487 struct hdac_stream *azx_dev;
489 /* clear stream status */
490 list_for_each_entry(azx_dev, &bus->stream_list, list)
491 snd_hdac_stream_writeb(azx_dev, SD_STS, SD_INT_MASK);
494 snd_hdac_chip_writew(bus, STATESTS, STATESTS_INT_MASK);
496 /* clear rirb status */
497 snd_hdac_chip_writeb(bus, RIRBSTS, RIRB_INT_MASK);
499 /* clear int status */
500 snd_hdac_chip_writel(bus, INTSTS, AZX_INT_CTRL_EN | AZX_INT_ALL_STREAM);
504 * snd_hdac_bus_init_chip - reset and start the controller registers
505 * @bus: HD-audio core bus
506 * @full_reset: Do full reset
508 bool snd_hdac_bus_init_chip(struct hdac_bus *bus, bool full_reset)
513 /* reset controller */
514 snd_hdac_bus_reset_link(bus, full_reset);
516 /* clear interrupts */
519 /* initialize the codec command I/O */
520 snd_hdac_bus_init_cmd_io(bus);
522 /* enable interrupts after CORB/RIRB buffers are initialized above */
525 /* program the position buffer */
526 if (bus->use_posbuf && bus->posbuf.addr) {
527 snd_hdac_chip_writel(bus, DPLBASE, (u32)bus->posbuf.addr);
528 snd_hdac_chip_writel(bus, DPUBASE, upper_32_bits(bus->posbuf.addr));
531 bus->chip_init = true;
535 EXPORT_SYMBOL_GPL(snd_hdac_bus_init_chip);
538 * snd_hdac_bus_stop_chip - disable the whole IRQ and I/Os
539 * @bus: HD-audio core bus
541 void snd_hdac_bus_stop_chip(struct hdac_bus *bus)
546 /* disable interrupts */
547 azx_int_disable(bus);
550 /* disable CORB/RIRB */
551 snd_hdac_bus_stop_cmd_io(bus);
553 /* disable position buffer */
554 if (bus->posbuf.addr) {
555 snd_hdac_chip_writel(bus, DPLBASE, 0);
556 snd_hdac_chip_writel(bus, DPUBASE, 0);
559 bus->chip_init = false;
561 EXPORT_SYMBOL_GPL(snd_hdac_bus_stop_chip);
564 * snd_hdac_bus_handle_stream_irq - interrupt handler for streams
565 * @bus: HD-audio core bus
566 * @status: INTSTS register value
567 * @ack: callback to be called for woken streams
569 * Returns the bits of handled streams, or zero if no stream is handled.
571 int snd_hdac_bus_handle_stream_irq(struct hdac_bus *bus, unsigned int status,
572 void (*ack)(struct hdac_bus *,
573 struct hdac_stream *))
575 struct hdac_stream *azx_dev;
579 list_for_each_entry(azx_dev, &bus->stream_list, list) {
580 if (status & azx_dev->sd_int_sta_mask) {
581 sd_status = snd_hdac_stream_readb(azx_dev, SD_STS);
582 snd_hdac_stream_writeb(azx_dev, SD_STS, SD_INT_MASK);
583 handled |= 1 << azx_dev->index;
584 if ((!azx_dev->substream && !azx_dev->cstream) ||
585 !azx_dev->running || !(sd_status & SD_INT_COMPLETE))
593 EXPORT_SYMBOL_GPL(snd_hdac_bus_handle_stream_irq);
596 * snd_hdac_bus_alloc_stream_pages - allocate BDL and other buffers
597 * @bus: HD-audio core bus
599 * Call this after assigning the all streams.
600 * Returns zero for success, or a negative error code.
602 int snd_hdac_bus_alloc_stream_pages(struct hdac_bus *bus)
604 struct hdac_stream *s;
606 int dma_type = bus->dma_type ? bus->dma_type : SNDRV_DMA_TYPE_DEV;
609 list_for_each_entry(s, &bus->stream_list, list) {
610 /* allocate memory for the BDL for each stream */
611 err = snd_dma_alloc_pages(dma_type, bus->dev,
618 if (WARN_ON(!num_streams))
620 /* allocate memory for the position buffer */
621 err = snd_dma_alloc_pages(dma_type, bus->dev,
622 num_streams * 8, &bus->posbuf);
625 list_for_each_entry(s, &bus->stream_list, list)
626 s->posbuf = (__le32 *)(bus->posbuf.area + s->index * 8);
628 /* single page (at least 4096 bytes) must suffice for both ringbuffes */
629 return snd_dma_alloc_pages(dma_type, bus->dev, PAGE_SIZE, &bus->rb);
631 EXPORT_SYMBOL_GPL(snd_hdac_bus_alloc_stream_pages);
634 * snd_hdac_bus_free_stream_pages - release BDL and other buffers
635 * @bus: HD-audio core bus
637 void snd_hdac_bus_free_stream_pages(struct hdac_bus *bus)
639 struct hdac_stream *s;
641 list_for_each_entry(s, &bus->stream_list, list) {
643 snd_dma_free_pages(&s->bdl);
647 snd_dma_free_pages(&bus->rb);
648 if (bus->posbuf.area)
649 snd_dma_free_pages(&bus->posbuf);
651 EXPORT_SYMBOL_GPL(snd_hdac_bus_free_stream_pages);
654 * snd_hdac_bus_link_power - power up/down codec link
655 * @codec: HD-audio device
656 * @enable: whether to power-up the link
658 void snd_hdac_bus_link_power(struct hdac_device *codec, bool enable)
661 set_bit(codec->addr, &codec->bus->codec_powered);
663 clear_bit(codec->addr, &codec->bus->codec_powered);
665 EXPORT_SYMBOL_GPL(snd_hdac_bus_link_power);