2 * HD-audio controller helpers
5 #include <linux/kernel.h>
6 #include <linux/delay.h>
7 #include <linux/export.h>
8 #include <sound/core.h>
9 #include <sound/hdaudio.h>
10 #include <sound/hda_register.h>
12 /* clear CORB read pointer properly */
13 static void azx_clear_corbrp(struct hdac_bus
*bus
)
17 for (timeout
= 1000; timeout
> 0; timeout
--) {
18 if (snd_hdac_chip_readw(bus
, CORBRP
) & AZX_CORBRP_RST
)
23 dev_err(bus
->dev
, "CORB reset timeout#1, CORBRP = %d\n",
24 snd_hdac_chip_readw(bus
, CORBRP
));
26 snd_hdac_chip_writew(bus
, CORBRP
, 0);
27 for (timeout
= 1000; timeout
> 0; timeout
--) {
28 if (snd_hdac_chip_readw(bus
, CORBRP
) == 0)
33 dev_err(bus
->dev
, "CORB reset timeout#2, CORBRP = %d\n",
34 snd_hdac_chip_readw(bus
, CORBRP
));
38 * snd_hdac_bus_init_cmd_io - set up CORB/RIRB buffers
39 * @bus: HD-audio core bus
41 void snd_hdac_bus_init_cmd_io(struct hdac_bus
*bus
)
43 spin_lock_irq(&bus
->reg_lock
);
45 bus
->corb
.addr
= bus
->rb
.addr
;
46 bus
->corb
.buf
= (__le32
*)bus
->rb
.area
;
47 snd_hdac_chip_writel(bus
, CORBLBASE
, (u32
)bus
->corb
.addr
);
48 snd_hdac_chip_writel(bus
, CORBUBASE
, upper_32_bits(bus
->corb
.addr
));
50 /* set the corb size to 256 entries (ULI requires explicitly) */
51 snd_hdac_chip_writeb(bus
, CORBSIZE
, 0x02);
52 /* set the corb write pointer to 0 */
53 snd_hdac_chip_writew(bus
, CORBWP
, 0);
55 /* reset the corb hw read pointer */
56 snd_hdac_chip_writew(bus
, CORBRP
, AZX_CORBRP_RST
);
57 if (!bus
->corbrp_self_clear
)
58 azx_clear_corbrp(bus
);
61 snd_hdac_chip_writeb(bus
, CORBCTL
, AZX_CORBCTL_RUN
);
64 bus
->rirb
.addr
= bus
->rb
.addr
+ 2048;
65 bus
->rirb
.buf
= (__le32
*)(bus
->rb
.area
+ 2048);
66 bus
->rirb
.wp
= bus
->rirb
.rp
= 0;
67 memset(bus
->rirb
.cmds
, 0, sizeof(bus
->rirb
.cmds
));
68 snd_hdac_chip_writel(bus
, RIRBLBASE
, (u32
)bus
->rirb
.addr
);
69 snd_hdac_chip_writel(bus
, RIRBUBASE
, upper_32_bits(bus
->rirb
.addr
));
71 /* set the rirb size to 256 entries (ULI requires explicitly) */
72 snd_hdac_chip_writeb(bus
, RIRBSIZE
, 0x02);
73 /* reset the rirb hw write pointer */
74 snd_hdac_chip_writew(bus
, RIRBWP
, AZX_RIRBWP_RST
);
75 /* set N=1, get RIRB response interrupt for new entry */
76 snd_hdac_chip_writew(bus
, RINTCNT
, 1);
77 /* enable rirb dma and response irq */
78 snd_hdac_chip_writeb(bus
, RIRBCTL
, AZX_RBCTL_DMA_EN
| AZX_RBCTL_IRQ_EN
);
79 spin_unlock_irq(&bus
->reg_lock
);
81 EXPORT_SYMBOL_GPL(snd_hdac_bus_init_cmd_io
);
83 /* wait for cmd dmas till they are stopped */
84 static void hdac_wait_for_cmd_dmas(struct hdac_bus
*bus
)
86 unsigned long timeout
;
88 timeout
= jiffies
+ msecs_to_jiffies(100);
89 while ((snd_hdac_chip_readb(bus
, RIRBCTL
) & AZX_RBCTL_DMA_EN
)
90 && time_before(jiffies
, timeout
))
93 timeout
= jiffies
+ msecs_to_jiffies(100);
94 while ((snd_hdac_chip_readb(bus
, CORBCTL
) & AZX_CORBCTL_RUN
)
95 && time_before(jiffies
, timeout
))
100 * snd_hdac_bus_stop_cmd_io - clean up CORB/RIRB buffers
101 * @bus: HD-audio core bus
103 void snd_hdac_bus_stop_cmd_io(struct hdac_bus
*bus
)
105 spin_lock_irq(&bus
->reg_lock
);
106 /* disable ringbuffer DMAs */
107 snd_hdac_chip_writeb(bus
, RIRBCTL
, 0);
108 snd_hdac_chip_writeb(bus
, CORBCTL
, 0);
109 hdac_wait_for_cmd_dmas(bus
);
110 /* disable unsolicited responses */
111 snd_hdac_chip_updatel(bus
, GCTL
, AZX_GCTL_UNSOL
, 0);
112 spin_unlock_irq(&bus
->reg_lock
);
114 EXPORT_SYMBOL_GPL(snd_hdac_bus_stop_cmd_io
);
116 static unsigned int azx_command_addr(u32 cmd
)
118 unsigned int addr
= cmd
>> 28;
120 if (snd_BUG_ON(addr
>= HDA_MAX_CODECS
))
126 * snd_hdac_bus_send_cmd - send a command verb via CORB
127 * @bus: HD-audio core bus
128 * @val: encoded verb value to send
130 * Returns zero for success or a negative error code.
132 int snd_hdac_bus_send_cmd(struct hdac_bus
*bus
, unsigned int val
)
134 unsigned int addr
= azx_command_addr(val
);
137 spin_lock_irq(&bus
->reg_lock
);
139 bus
->last_cmd
[azx_command_addr(val
)] = val
;
141 /* add command to corb */
142 wp
= snd_hdac_chip_readw(bus
, CORBWP
);
144 /* something wrong, controller likely turned to D3 */
145 spin_unlock_irq(&bus
->reg_lock
);
149 wp
%= AZX_MAX_CORB_ENTRIES
;
151 rp
= snd_hdac_chip_readw(bus
, CORBRP
);
153 /* oops, it's full */
154 spin_unlock_irq(&bus
->reg_lock
);
158 bus
->rirb
.cmds
[addr
]++;
159 bus
->corb
.buf
[wp
] = cpu_to_le32(val
);
160 snd_hdac_chip_writew(bus
, CORBWP
, wp
);
162 spin_unlock_irq(&bus
->reg_lock
);
166 EXPORT_SYMBOL_GPL(snd_hdac_bus_send_cmd
);
168 #define AZX_RIRB_EX_UNSOL_EV (1<<4)
171 * snd_hdac_bus_update_rirb - retrieve RIRB entries
172 * @bus: HD-audio core bus
174 * Usually called from interrupt handler.
176 void snd_hdac_bus_update_rirb(struct hdac_bus
*bus
)
182 wp
= snd_hdac_chip_readw(bus
, RIRBWP
);
184 /* something wrong, controller likely turned to D3 */
188 if (wp
== bus
->rirb
.wp
)
192 while (bus
->rirb
.rp
!= wp
) {
194 bus
->rirb
.rp
%= AZX_MAX_RIRB_ENTRIES
;
196 rp
= bus
->rirb
.rp
<< 1; /* an RIRB entry is 8-bytes */
197 res_ex
= le32_to_cpu(bus
->rirb
.buf
[rp
+ 1]);
198 res
= le32_to_cpu(bus
->rirb
.buf
[rp
]);
200 if (addr
>= HDA_MAX_CODECS
) {
202 "spurious response %#x:%#x, rp = %d, wp = %d",
203 res
, res_ex
, bus
->rirb
.rp
, wp
);
205 } else if (res_ex
& AZX_RIRB_EX_UNSOL_EV
)
206 snd_hdac_bus_queue_event(bus
, res
, res_ex
);
207 else if (bus
->rirb
.cmds
[addr
]) {
208 bus
->rirb
.res
[addr
] = res
;
209 bus
->rirb
.cmds
[addr
]--;
211 dev_err_ratelimited(bus
->dev
,
212 "spurious response %#x:%#x, last cmd=%#08x\n",
213 res
, res_ex
, bus
->last_cmd
[addr
]);
217 EXPORT_SYMBOL_GPL(snd_hdac_bus_update_rirb
);
220 * snd_hdac_bus_get_response - receive a response via RIRB
221 * @bus: HD-audio core bus
222 * @addr: codec address
223 * @res: pointer to store the value, NULL when not needed
225 * Returns zero if a value is read, or a negative error code.
227 int snd_hdac_bus_get_response(struct hdac_bus
*bus
, unsigned int addr
,
230 unsigned long timeout
;
231 unsigned long loopcounter
;
233 timeout
= jiffies
+ msecs_to_jiffies(1000);
235 for (loopcounter
= 0;; loopcounter
++) {
236 spin_lock_irq(&bus
->reg_lock
);
237 if (!bus
->rirb
.cmds
[addr
]) {
239 *res
= bus
->rirb
.res
[addr
]; /* the last value */
240 spin_unlock_irq(&bus
->reg_lock
);
243 spin_unlock_irq(&bus
->reg_lock
);
244 if (time_after(jiffies
, timeout
))
246 if (loopcounter
> 3000)
247 msleep(2); /* temporary workaround */
256 EXPORT_SYMBOL_GPL(snd_hdac_bus_get_response
);
263 * snd_hdac_bus_enter_link_reset - enter link reset
264 * @bus: HD-audio core bus
266 * Enter to the link reset state.
268 void snd_hdac_bus_enter_link_reset(struct hdac_bus
*bus
)
270 unsigned long timeout
;
272 /* reset controller */
273 snd_hdac_chip_updatel(bus
, GCTL
, AZX_GCTL_RESET
, 0);
275 timeout
= jiffies
+ msecs_to_jiffies(100);
276 while ((snd_hdac_chip_readb(bus
, GCTL
) & AZX_GCTL_RESET
) &&
277 time_before(jiffies
, timeout
))
278 usleep_range(500, 1000);
280 EXPORT_SYMBOL_GPL(snd_hdac_bus_enter_link_reset
);
283 * snd_hdac_bus_exit_link_reset - exit link reset
284 * @bus: HD-audio core bus
286 * Exit from the link reset state.
288 void snd_hdac_bus_exit_link_reset(struct hdac_bus
*bus
)
290 unsigned long timeout
;
292 snd_hdac_chip_updateb(bus
, GCTL
, 0, AZX_GCTL_RESET
);
294 timeout
= jiffies
+ msecs_to_jiffies(100);
295 while (!snd_hdac_chip_readb(bus
, GCTL
) && time_before(jiffies
, timeout
))
296 usleep_range(500, 1000);
298 EXPORT_SYMBOL_GPL(snd_hdac_bus_exit_link_reset
);
300 /* reset codec link */
301 static int azx_reset(struct hdac_bus
*bus
, bool full_reset
)
307 snd_hdac_chip_writew(bus
, STATESTS
, STATESTS_INT_MASK
);
309 /* reset controller */
310 snd_hdac_bus_enter_link_reset(bus
);
312 /* delay for >= 100us for codec PLL to settle per spec
313 * Rev 0.9 section 5.5.1
315 usleep_range(500, 1000);
317 /* Bring controller out of reset */
318 snd_hdac_bus_exit_link_reset(bus
);
320 /* Brent Chartrand said to wait >= 540us for codecs to initialize */
321 usleep_range(1000, 1200);
324 /* check to see if controller is ready */
325 if (!snd_hdac_chip_readb(bus
, GCTL
)) {
326 dev_dbg(bus
->dev
, "azx_reset: controller not ready!\n");
330 /* Accept unsolicited responses */
331 snd_hdac_chip_updatel(bus
, GCTL
, 0, AZX_GCTL_UNSOL
);
334 if (!bus
->codec_mask
) {
335 bus
->codec_mask
= snd_hdac_chip_readw(bus
, STATESTS
);
336 dev_dbg(bus
->dev
, "codec_mask = 0x%lx\n", bus
->codec_mask
);
342 /* enable interrupts */
343 static void azx_int_enable(struct hdac_bus
*bus
)
345 /* enable controller CIE and GIE */
346 snd_hdac_chip_updatel(bus
, INTCTL
, 0, AZX_INT_CTRL_EN
| AZX_INT_GLOBAL_EN
);
349 /* disable interrupts */
350 static void azx_int_disable(struct hdac_bus
*bus
)
352 struct hdac_stream
*azx_dev
;
354 /* disable interrupts in stream descriptor */
355 list_for_each_entry(azx_dev
, &bus
->stream_list
, list
)
356 snd_hdac_stream_updateb(azx_dev
, SD_CTL
, SD_INT_MASK
, 0);
358 /* disable SIE for all streams */
359 snd_hdac_chip_writeb(bus
, INTCTL
, 0);
361 /* disable controller CIE and GIE */
362 snd_hdac_chip_updatel(bus
, INTCTL
, AZX_INT_CTRL_EN
| AZX_INT_GLOBAL_EN
, 0);
365 /* clear interrupts */
366 static void azx_int_clear(struct hdac_bus
*bus
)
368 struct hdac_stream
*azx_dev
;
370 /* clear stream status */
371 list_for_each_entry(azx_dev
, &bus
->stream_list
, list
)
372 snd_hdac_stream_writeb(azx_dev
, SD_STS
, SD_INT_MASK
);
375 snd_hdac_chip_writew(bus
, STATESTS
, STATESTS_INT_MASK
);
377 /* clear rirb status */
378 snd_hdac_chip_writeb(bus
, RIRBSTS
, RIRB_INT_MASK
);
380 /* clear int status */
381 snd_hdac_chip_writel(bus
, INTSTS
, AZX_INT_CTRL_EN
| AZX_INT_ALL_STREAM
);
385 * snd_hdac_bus_init_chip - reset and start the controller registers
386 * @bus: HD-audio core bus
387 * @full_reset: Do full reset
389 bool snd_hdac_bus_init_chip(struct hdac_bus
*bus
, bool full_reset
)
394 /* reset controller */
395 azx_reset(bus
, full_reset
);
397 /* initialize interrupts */
401 /* initialize the codec command I/O */
402 snd_hdac_bus_init_cmd_io(bus
);
404 /* program the position buffer */
405 if (bus
->use_posbuf
&& bus
->posbuf
.addr
) {
406 snd_hdac_chip_writel(bus
, DPLBASE
, (u32
)bus
->posbuf
.addr
);
407 snd_hdac_chip_writel(bus
, DPUBASE
, upper_32_bits(bus
->posbuf
.addr
));
410 bus
->chip_init
= true;
413 EXPORT_SYMBOL_GPL(snd_hdac_bus_init_chip
);
416 * snd_hdac_bus_stop_chip - disable the whole IRQ and I/Os
417 * @bus: HD-audio core bus
419 void snd_hdac_bus_stop_chip(struct hdac_bus
*bus
)
424 /* disable interrupts */
425 azx_int_disable(bus
);
428 /* disable CORB/RIRB */
429 snd_hdac_bus_stop_cmd_io(bus
);
431 /* disable position buffer */
432 if (bus
->posbuf
.addr
) {
433 snd_hdac_chip_writel(bus
, DPLBASE
, 0);
434 snd_hdac_chip_writel(bus
, DPUBASE
, 0);
437 bus
->chip_init
= false;
439 EXPORT_SYMBOL_GPL(snd_hdac_bus_stop_chip
);
442 * snd_hdac_bus_handle_stream_irq - interrupt handler for streams
443 * @bus: HD-audio core bus
444 * @status: INTSTS register value
445 * @ask: callback to be called for woken streams
447 * Returns the bits of handled streams, or zero if no stream is handled.
449 int snd_hdac_bus_handle_stream_irq(struct hdac_bus
*bus
, unsigned int status
,
450 void (*ack
)(struct hdac_bus
*,
451 struct hdac_stream
*))
453 struct hdac_stream
*azx_dev
;
457 list_for_each_entry(azx_dev
, &bus
->stream_list
, list
) {
458 if (status
& azx_dev
->sd_int_sta_mask
) {
459 sd_status
= snd_hdac_stream_readb(azx_dev
, SD_STS
);
460 snd_hdac_stream_writeb(azx_dev
, SD_STS
, SD_INT_MASK
);
461 handled
|= 1 << azx_dev
->index
;
462 if (!azx_dev
->substream
|| !azx_dev
->running
||
463 !(sd_status
& SD_INT_COMPLETE
))
471 EXPORT_SYMBOL_GPL(snd_hdac_bus_handle_stream_irq
);
474 * snd_hdac_bus_alloc_stream_pages - allocate BDL and other buffers
475 * @bus: HD-audio core bus
477 * Call this after assigning the all streams.
478 * Returns zero for success, or a negative error code.
480 int snd_hdac_bus_alloc_stream_pages(struct hdac_bus
*bus
)
482 struct hdac_stream
*s
;
486 list_for_each_entry(s
, &bus
->stream_list
, list
) {
487 /* allocate memory for the BDL for each stream */
488 err
= bus
->io_ops
->dma_alloc_pages(bus
, SNDRV_DMA_TYPE_DEV
,
495 if (WARN_ON(!num_streams
))
497 /* allocate memory for the position buffer */
498 err
= bus
->io_ops
->dma_alloc_pages(bus
, SNDRV_DMA_TYPE_DEV
,
499 num_streams
* 8, &bus
->posbuf
);
502 list_for_each_entry(s
, &bus
->stream_list
, list
)
503 s
->posbuf
= (__le32
*)(bus
->posbuf
.area
+ s
->index
* 8);
505 /* single page (at least 4096 bytes) must suffice for both ringbuffes */
506 return bus
->io_ops
->dma_alloc_pages(bus
, SNDRV_DMA_TYPE_DEV
,
507 PAGE_SIZE
, &bus
->rb
);
509 EXPORT_SYMBOL_GPL(snd_hdac_bus_alloc_stream_pages
);
512 * snd_hdac_bus_free_stream_pages - release BDL and other buffers
513 * @bus: HD-audio core bus
515 void snd_hdac_bus_free_stream_pages(struct hdac_bus
*bus
)
517 struct hdac_stream
*s
;
519 list_for_each_entry(s
, &bus
->stream_list
, list
) {
521 bus
->io_ops
->dma_free_pages(bus
, &s
->bdl
);
525 bus
->io_ops
->dma_free_pages(bus
, &bus
->rb
);
526 if (bus
->posbuf
.area
)
527 bus
->io_ops
->dma_free_pages(bus
, &bus
->posbuf
);
529 EXPORT_SYMBOL_GPL(snd_hdac_bus_free_stream_pages
);