1 // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
3 // This file is provided under a dual BSD/GPLv2 license. When using or
4 // redistributing this file, you may do so under either license.
6 // Copyright(c) 2018 Intel Corporation. All rights reserved.
8 // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
12 * Hardware interface for audio DSP on Baytrail, Braswell and Cherrytrail.
15 #include <linux/module.h>
16 #include <sound/sof.h>
17 #include <sound/sof/xtensa.h>
22 #define IRAM_OFFSET 0x0C0000
23 #define IRAM_SIZE (80 * 1024)
24 #define DRAM_OFFSET 0x100000
25 #define DRAM_SIZE (160 * 1024)
26 #define SHIM_OFFSET 0x140000
27 #define SHIM_SIZE 0x100
28 #define MBOX_OFFSET 0x144000
29 #define MBOX_SIZE 0x1000
30 #define EXCEPT_OFFSET 0x800
31 #define EXCEPT_MAX_HDR_SIZE 0x400
34 #define DMAC0_OFFSET 0x098000
35 #define DMAC1_OFFSET 0x09c000
36 #define DMAC2_OFFSET 0x094000
37 #define DMAC_SIZE 0x420
38 #define SSP0_OFFSET 0x0a0000
39 #define SSP1_OFFSET 0x0a1000
40 #define SSP2_OFFSET 0x0a2000
41 #define SSP3_OFFSET 0x0a4000
42 #define SSP4_OFFSET 0x0a5000
43 #define SSP5_OFFSET 0x0a6000
44 #define SSP_SIZE 0x100
46 #define BYT_STACK_DUMP_SIZE 32
48 #define BYT_PCI_BAR_SIZE 0x200000
50 #define BYT_PANIC_OFFSET(x) (((x) & GENMASK_ULL(47, 32)) >> 32)
56 #define MBOX_DUMP_SIZE 0x30
63 static const struct snd_sof_debugfs_map byt_debugfs[] = {
64 {"dmac0", BYT_DSP_BAR, DMAC0_OFFSET, DMAC_SIZE,
65 SOF_DEBUGFS_ACCESS_ALWAYS},
66 {"dmac1", BYT_DSP_BAR, DMAC1_OFFSET, DMAC_SIZE,
67 SOF_DEBUGFS_ACCESS_ALWAYS},
68 {"ssp0", BYT_DSP_BAR, SSP0_OFFSET, SSP_SIZE,
69 SOF_DEBUGFS_ACCESS_ALWAYS},
70 {"ssp1", BYT_DSP_BAR, SSP1_OFFSET, SSP_SIZE,
71 SOF_DEBUGFS_ACCESS_ALWAYS},
72 {"ssp2", BYT_DSP_BAR, SSP2_OFFSET, SSP_SIZE,
73 SOF_DEBUGFS_ACCESS_ALWAYS},
74 {"iram", BYT_DSP_BAR, IRAM_OFFSET, IRAM_SIZE,
75 SOF_DEBUGFS_ACCESS_D0_ONLY},
76 {"dram", BYT_DSP_BAR, DRAM_OFFSET, DRAM_SIZE,
77 SOF_DEBUGFS_ACCESS_D0_ONLY},
78 {"shim", BYT_DSP_BAR, SHIM_OFFSET, SHIM_SIZE,
79 SOF_DEBUGFS_ACCESS_ALWAYS},
82 static const struct snd_sof_debugfs_map cht_debugfs[] = {
83 {"dmac0", BYT_DSP_BAR, DMAC0_OFFSET, DMAC_SIZE,
84 SOF_DEBUGFS_ACCESS_ALWAYS},
85 {"dmac1", BYT_DSP_BAR, DMAC1_OFFSET, DMAC_SIZE,
86 SOF_DEBUGFS_ACCESS_ALWAYS},
87 {"dmac2", BYT_DSP_BAR, DMAC2_OFFSET, DMAC_SIZE,
88 SOF_DEBUGFS_ACCESS_ALWAYS},
89 {"ssp0", BYT_DSP_BAR, SSP0_OFFSET, SSP_SIZE,
90 SOF_DEBUGFS_ACCESS_ALWAYS},
91 {"ssp1", BYT_DSP_BAR, SSP1_OFFSET, SSP_SIZE,
92 SOF_DEBUGFS_ACCESS_ALWAYS},
93 {"ssp2", BYT_DSP_BAR, SSP2_OFFSET, SSP_SIZE,
94 SOF_DEBUGFS_ACCESS_ALWAYS},
95 {"ssp3", BYT_DSP_BAR, SSP3_OFFSET, SSP_SIZE,
96 SOF_DEBUGFS_ACCESS_ALWAYS},
97 {"ssp4", BYT_DSP_BAR, SSP4_OFFSET, SSP_SIZE,
98 SOF_DEBUGFS_ACCESS_ALWAYS},
99 {"ssp5", BYT_DSP_BAR, SSP5_OFFSET, SSP_SIZE,
100 SOF_DEBUGFS_ACCESS_ALWAYS},
101 {"iram", BYT_DSP_BAR, IRAM_OFFSET, IRAM_SIZE,
102 SOF_DEBUGFS_ACCESS_D0_ONLY},
103 {"dram", BYT_DSP_BAR, DRAM_OFFSET, DRAM_SIZE,
104 SOF_DEBUGFS_ACCESS_D0_ONLY},
105 {"shim", BYT_DSP_BAR, SHIM_OFFSET, SHIM_SIZE,
106 SOF_DEBUGFS_ACCESS_ALWAYS},
109 static void byt_host_done(struct snd_sof_dev *sdev);
110 static void byt_dsp_done(struct snd_sof_dev *sdev);
111 static void byt_get_reply(struct snd_sof_dev *sdev);
117 static void byt_get_registers(struct snd_sof_dev *sdev,
118 struct sof_ipc_dsp_oops_xtensa *xoops,
119 struct sof_ipc_panic_info *panic_info,
120 u32 *stack, size_t stack_words)
122 u32 offset = sdev->dsp_oops_offset;
124 /* first read regsisters */
125 sof_mailbox_read(sdev, offset, xoops, sizeof(*xoops));
127 /* note: variable AR register array is not read */
129 /* then get panic info */
130 if (xoops->arch_hdr.totalsize > EXCEPT_MAX_HDR_SIZE) {
131 dev_err(sdev->dev, "invalid header size 0x%x. FW oops is bogus\n",
132 xoops->arch_hdr.totalsize);
135 offset += xoops->arch_hdr.totalsize;
136 sof_mailbox_read(sdev, offset, panic_info, sizeof(*panic_info));
138 /* then get the stack */
139 offset += sizeof(*panic_info);
140 sof_mailbox_read(sdev, offset, stack, stack_words * sizeof(u32));
143 static void byt_dump(struct snd_sof_dev *sdev, u32 flags)
145 struct sof_ipc_dsp_oops_xtensa xoops;
146 struct sof_ipc_panic_info panic_info;
147 u32 stack[BYT_STACK_DUMP_SIZE];
150 /* now try generic SOF status messages */
151 status = snd_sof_dsp_read(sdev, BYT_DSP_BAR, SHIM_IPCD);
152 panic = snd_sof_dsp_read(sdev, BYT_DSP_BAR, SHIM_IPCX);
153 byt_get_registers(sdev, &xoops, &panic_info, stack,
154 BYT_STACK_DUMP_SIZE);
155 snd_sof_get_status(sdev, status, panic, &xoops, &panic_info, stack,
156 BYT_STACK_DUMP_SIZE);
160 * IPC Doorbell IRQ handler and thread.
163 static irqreturn_t byt_irq_handler(int irq, void *context)
165 struct snd_sof_dev *sdev = context;
169 /* Interrupt arrived, check src */
170 isr = snd_sof_dsp_read64(sdev, BYT_DSP_BAR, SHIM_ISRX);
171 if (isr & (SHIM_ISRX_DONE | SHIM_ISRX_BUSY))
172 ret = IRQ_WAKE_THREAD;
177 static irqreturn_t byt_irq_thread(int irq, void *context)
179 struct snd_sof_dev *sdev = context;
183 imrx = snd_sof_dsp_read64(sdev, BYT_DSP_BAR, SHIM_IMRX);
184 ipcx = snd_sof_dsp_read64(sdev, BYT_DSP_BAR, SHIM_IPCX);
186 /* reply message from DSP */
187 if (ipcx & SHIM_BYT_IPCX_DONE &&
188 !(imrx & SHIM_IMRX_DONE)) {
189 /* Mask Done interrupt before first */
190 snd_sof_dsp_update_bits64_unlocked(sdev, BYT_DSP_BAR,
195 spin_lock_irq(&sdev->ipc_lock);
198 * handle immediate reply from DSP core. If the msg is
199 * found, set done bit in cmd_done which is called at the
200 * end of message processing function, else set it here
201 * because the done bit can't be set in cmd_done function
202 * which is triggered by msg
205 snd_sof_ipc_reply(sdev, ipcx);
209 spin_unlock_irq(&sdev->ipc_lock);
212 /* new message from DSP */
213 ipcd = snd_sof_dsp_read64(sdev, BYT_DSP_BAR, SHIM_IPCD);
214 if (ipcd & SHIM_BYT_IPCD_BUSY &&
215 !(imrx & SHIM_IMRX_BUSY)) {
216 /* Mask Busy interrupt before return */
217 snd_sof_dsp_update_bits64_unlocked(sdev, BYT_DSP_BAR,
222 /* Handle messages from DSP Core */
223 if ((ipcd & SOF_IPC_PANIC_MAGIC_MASK) == SOF_IPC_PANIC_MAGIC) {
224 snd_sof_dsp_panic(sdev, BYT_PANIC_OFFSET(ipcd) +
227 snd_sof_ipc_msgs_rx(sdev);
236 static int byt_send_msg(struct snd_sof_dev *sdev, struct snd_sof_ipc_msg *msg)
238 /* send the message */
239 sof_mailbox_write(sdev, sdev->host_box.offset, msg->msg_data,
241 snd_sof_dsp_write64(sdev, BYT_DSP_BAR, SHIM_IPCX, SHIM_BYT_IPCX_BUSY);
246 static void byt_get_reply(struct snd_sof_dev *sdev)
248 struct snd_sof_ipc_msg *msg = sdev->msg;
249 struct sof_ipc_reply reply;
253 * Sometimes, there is unexpected reply ipc arriving. The reply
254 * ipc belongs to none of the ipcs sent from driver.
255 * In this case, the driver must ignore the ipc.
258 dev_warn(sdev->dev, "unexpected ipc interrupt raised!\n");
263 sof_mailbox_read(sdev, sdev->host_box.offset, &reply, sizeof(reply));
265 if (reply.error < 0) {
266 memcpy(msg->reply_data, &reply, sizeof(reply));
269 /* reply correct size ? */
270 if (reply.hdr.size != msg->reply_size) {
271 dev_err(sdev->dev, "error: reply expected %zu got %u bytes\n",
272 msg->reply_size, reply.hdr.size);
276 /* read the message */
277 if (msg->reply_size > 0)
278 sof_mailbox_read(sdev, sdev->host_box.offset,
279 msg->reply_data, msg->reply_size);
282 msg->reply_error = ret;
285 static int byt_get_mailbox_offset(struct snd_sof_dev *sdev)
290 static int byt_get_window_offset(struct snd_sof_dev *sdev, u32 id)
295 static void byt_host_done(struct snd_sof_dev *sdev)
297 /* clear BUSY bit and set DONE bit - accept new messages */
298 snd_sof_dsp_update_bits64_unlocked(sdev, BYT_DSP_BAR, SHIM_IPCD,
303 /* unmask busy interrupt */
304 snd_sof_dsp_update_bits64_unlocked(sdev, BYT_DSP_BAR, SHIM_IMRX,
308 static void byt_dsp_done(struct snd_sof_dev *sdev)
310 /* clear DONE bit - tell DSP we have completed */
311 snd_sof_dsp_update_bits64_unlocked(sdev, BYT_DSP_BAR, SHIM_IPCX,
312 SHIM_BYT_IPCX_DONE, 0);
314 /* unmask Done interrupt */
315 snd_sof_dsp_update_bits64_unlocked(sdev, BYT_DSP_BAR, SHIM_IMRX,
323 static int byt_run(struct snd_sof_dev *sdev)
327 /* release stall and wait to unstall */
328 snd_sof_dsp_update_bits64(sdev, BYT_DSP_BAR, SHIM_CSR,
329 SHIM_BYT_CSR_STALL, 0x0);
331 if (!(snd_sof_dsp_read64(sdev, BYT_DSP_BAR, SHIM_CSR) &
332 SHIM_BYT_CSR_PWAITMODE))
337 dev_err(sdev->dev, "error: unable to run DSP firmware\n");
338 byt_dump(sdev, SOF_DBG_REGS | SOF_DBG_MBOX);
342 /* return init core mask */
346 static int byt_reset(struct snd_sof_dev *sdev)
348 /* put DSP into reset, set reset vector and stall */
349 snd_sof_dsp_update_bits64(sdev, BYT_DSP_BAR, SHIM_CSR,
350 SHIM_BYT_CSR_RST | SHIM_BYT_CSR_VECTOR_SEL |
352 SHIM_BYT_CSR_RST | SHIM_BYT_CSR_VECTOR_SEL |
355 usleep_range(10, 15);
357 /* take DSP out of reset and keep stalled for FW loading */
358 snd_sof_dsp_update_bits64(sdev, BYT_DSP_BAR, SHIM_CSR,
359 SHIM_BYT_CSR_RST, 0);
365 static struct snd_soc_dai_driver byt_dai[] = {
390 #if IS_ENABLED(CONFIG_SND_SOC_SOF_MERRIFIELD)
392 static int tangier_pci_probe(struct snd_sof_dev *sdev)
394 struct snd_sof_pdata *pdata = sdev->pdata;
395 const struct sof_dev_desc *desc = pdata->desc;
396 struct pci_dev *pci = to_pci_dev(sdev->dev);
400 /* DSP DMA can only access low 31 bits of host memory */
401 ret = dma_coerce_mask_and_coherent(&pci->dev, DMA_BIT_MASK(31));
403 dev_err(sdev->dev, "error: failed to set DMA mask %d\n", ret);
408 base = pci_resource_start(pci, desc->resindex_lpe_base) - IRAM_OFFSET;
409 size = BYT_PCI_BAR_SIZE;
411 dev_dbg(sdev->dev, "LPE PHY base at 0x%x size 0x%x", base, size);
412 sdev->bar[BYT_DSP_BAR] = devm_ioremap(sdev->dev, base, size);
413 if (!sdev->bar[BYT_DSP_BAR]) {
414 dev_err(sdev->dev, "error: failed to ioremap LPE base 0x%x size 0x%x\n",
418 dev_dbg(sdev->dev, "LPE VADDR %p\n", sdev->bar[BYT_DSP_BAR]);
420 /* IMR base - optional */
421 if (desc->resindex_imr_base == -1)
424 base = pci_resource_start(pci, desc->resindex_imr_base);
425 size = pci_resource_len(pci, desc->resindex_imr_base);
427 /* some BIOSes don't map IMR */
428 if (base == 0x55aa55aa || base == 0x0) {
429 dev_info(sdev->dev, "IMR not set by BIOS. Ignoring\n");
433 dev_dbg(sdev->dev, "IMR base at 0x%x size 0x%x", base, size);
434 sdev->bar[BYT_IMR_BAR] = devm_ioremap(sdev->dev, base, size);
435 if (!sdev->bar[BYT_IMR_BAR]) {
436 dev_err(sdev->dev, "error: failed to ioremap IMR base 0x%x size 0x%x\n",
440 dev_dbg(sdev->dev, "IMR VADDR %p\n", sdev->bar[BYT_IMR_BAR]);
443 /* register our IRQ */
444 sdev->ipc_irq = pci->irq;
445 dev_dbg(sdev->dev, "using IRQ %d\n", sdev->ipc_irq);
446 ret = devm_request_threaded_irq(sdev->dev, sdev->ipc_irq,
447 byt_irq_handler, byt_irq_thread,
448 0, "AudioDSP", sdev);
450 dev_err(sdev->dev, "error: failed to register IRQ %d\n",
455 /* enable Interrupt from both sides */
456 snd_sof_dsp_update_bits64(sdev, BYT_DSP_BAR, SHIM_IMRX, 0x3, 0x0);
457 snd_sof_dsp_update_bits64(sdev, BYT_DSP_BAR, SHIM_IMRD, 0x3, 0x0);
459 /* set default mailbox offset for FW ready message */
460 sdev->dsp_box.offset = MBOX_OFFSET;
465 const struct snd_sof_dsp_ops sof_tng_ops = {
467 .probe = tangier_pci_probe,
469 /* DSP core boot / reset */
474 .write = sof_io_write,
476 .write64 = sof_io_write64,
477 .read64 = sof_io_read64,
480 .block_read = sof_block_read,
481 .block_write = sof_block_write,
484 .irq_handler = byt_irq_handler,
485 .irq_thread = byt_irq_thread,
488 .send_msg = byt_send_msg,
489 .fw_ready = sof_fw_ready,
490 .get_mailbox_offset = byt_get_mailbox_offset,
491 .get_window_offset = byt_get_window_offset,
493 .ipc_msg_data = intel_ipc_msg_data,
494 .ipc_pcm_params = intel_ipc_pcm_params,
497 .debug_map = byt_debugfs,
498 .debug_map_count = ARRAY_SIZE(byt_debugfs),
499 .dbg_dump = byt_dump,
501 /* stream callbacks */
502 .pcm_open = intel_pcm_open,
503 .pcm_close = intel_pcm_close,
506 .load_module = snd_sof_parse_module_memcpy,
508 /*Firmware loading */
509 .load_firmware = snd_sof_load_firmware_memcpy,
513 .num_drv = 3, /* we have only 3 SSPs on byt*/
515 EXPORT_SYMBOL(sof_tng_ops);
517 const struct sof_intel_dsp_desc tng_chip_info = {
521 EXPORT_SYMBOL(tng_chip_info);
523 #endif /* CONFIG_SND_SOC_SOF_MERRIFIELD */
525 #if IS_ENABLED(CONFIG_SND_SOC_SOF_BAYTRAIL)
527 static int byt_acpi_probe(struct snd_sof_dev *sdev)
529 struct snd_sof_pdata *pdata = sdev->pdata;
530 const struct sof_dev_desc *desc = pdata->desc;
531 struct platform_device *pdev =
532 container_of(sdev->dev, struct platform_device, dev);
533 struct resource *mmio;
537 /* DSP DMA can only access low 31 bits of host memory */
538 ret = dma_coerce_mask_and_coherent(sdev->dev, DMA_BIT_MASK(31));
540 dev_err(sdev->dev, "error: failed to set DMA mask %d\n", ret);
545 mmio = platform_get_resource(pdev, IORESOURCE_MEM,
546 desc->resindex_lpe_base);
549 size = resource_size(mmio);
551 dev_err(sdev->dev, "error: failed to get LPE base at idx %d\n",
552 desc->resindex_lpe_base);
556 dev_dbg(sdev->dev, "LPE PHY base at 0x%x size 0x%x", base, size);
557 sdev->bar[BYT_DSP_BAR] = devm_ioremap(sdev->dev, base, size);
558 if (!sdev->bar[BYT_DSP_BAR]) {
559 dev_err(sdev->dev, "error: failed to ioremap LPE base 0x%x size 0x%x\n",
563 dev_dbg(sdev->dev, "LPE VADDR %p\n", sdev->bar[BYT_DSP_BAR]);
565 /* TODO: add offsets */
566 sdev->mmio_bar = BYT_DSP_BAR;
567 sdev->mailbox_bar = BYT_DSP_BAR;
569 /* IMR base - optional */
570 if (desc->resindex_imr_base == -1)
573 mmio = platform_get_resource(pdev, IORESOURCE_MEM,
574 desc->resindex_imr_base);
577 size = resource_size(mmio);
579 dev_err(sdev->dev, "error: failed to get IMR base at idx %d\n",
580 desc->resindex_imr_base);
584 /* some BIOSes don't map IMR */
585 if (base == 0x55aa55aa || base == 0x0) {
586 dev_info(sdev->dev, "IMR not set by BIOS. Ignoring\n");
590 dev_dbg(sdev->dev, "IMR base at 0x%x size 0x%x", base, size);
591 sdev->bar[BYT_IMR_BAR] = devm_ioremap(sdev->dev, base, size);
592 if (!sdev->bar[BYT_IMR_BAR]) {
593 dev_err(sdev->dev, "error: failed to ioremap IMR base 0x%x size 0x%x\n",
597 dev_dbg(sdev->dev, "IMR VADDR %p\n", sdev->bar[BYT_IMR_BAR]);
600 /* register our IRQ */
601 sdev->ipc_irq = platform_get_irq(pdev, desc->irqindex_host_ipc);
602 if (sdev->ipc_irq < 0)
603 return sdev->ipc_irq;
605 dev_dbg(sdev->dev, "using IRQ %d\n", sdev->ipc_irq);
606 ret = devm_request_threaded_irq(sdev->dev, sdev->ipc_irq,
607 byt_irq_handler, byt_irq_thread,
608 IRQF_SHARED, "AudioDSP", sdev);
610 dev_err(sdev->dev, "error: failed to register IRQ %d\n",
615 /* enable Interrupt from both sides */
616 snd_sof_dsp_update_bits64(sdev, BYT_DSP_BAR, SHIM_IMRX, 0x3, 0x0);
617 snd_sof_dsp_update_bits64(sdev, BYT_DSP_BAR, SHIM_IMRD, 0x3, 0x0);
619 /* set default mailbox offset for FW ready message */
620 sdev->dsp_box.offset = MBOX_OFFSET;
626 const struct snd_sof_dsp_ops sof_byt_ops = {
628 .probe = byt_acpi_probe,
630 /* DSP core boot / reset */
635 .write = sof_io_write,
637 .write64 = sof_io_write64,
638 .read64 = sof_io_read64,
641 .block_read = sof_block_read,
642 .block_write = sof_block_write,
645 .irq_handler = byt_irq_handler,
646 .irq_thread = byt_irq_thread,
649 .send_msg = byt_send_msg,
650 .fw_ready = sof_fw_ready,
651 .get_mailbox_offset = byt_get_mailbox_offset,
652 .get_window_offset = byt_get_window_offset,
654 .ipc_msg_data = intel_ipc_msg_data,
655 .ipc_pcm_params = intel_ipc_pcm_params,
658 .debug_map = byt_debugfs,
659 .debug_map_count = ARRAY_SIZE(byt_debugfs),
660 .dbg_dump = byt_dump,
662 /* stream callbacks */
663 .pcm_open = intel_pcm_open,
664 .pcm_close = intel_pcm_close,
667 .load_module = snd_sof_parse_module_memcpy,
669 /*Firmware loading */
670 .load_firmware = snd_sof_load_firmware_memcpy,
674 .num_drv = 3, /* we have only 3 SSPs on byt*/
676 EXPORT_SYMBOL(sof_byt_ops);
678 const struct sof_intel_dsp_desc byt_chip_info = {
682 EXPORT_SYMBOL(byt_chip_info);
684 /* cherrytrail and braswell ops */
685 const struct snd_sof_dsp_ops sof_cht_ops = {
687 .probe = byt_acpi_probe,
689 /* DSP core boot / reset */
694 .write = sof_io_write,
696 .write64 = sof_io_write64,
697 .read64 = sof_io_read64,
700 .block_read = sof_block_read,
701 .block_write = sof_block_write,
704 .irq_handler = byt_irq_handler,
705 .irq_thread = byt_irq_thread,
708 .send_msg = byt_send_msg,
709 .fw_ready = sof_fw_ready,
710 .get_mailbox_offset = byt_get_mailbox_offset,
711 .get_window_offset = byt_get_window_offset,
713 .ipc_msg_data = intel_ipc_msg_data,
714 .ipc_pcm_params = intel_ipc_pcm_params,
717 .debug_map = cht_debugfs,
718 .debug_map_count = ARRAY_SIZE(cht_debugfs),
719 .dbg_dump = byt_dump,
721 /* stream callbacks */
722 .pcm_open = intel_pcm_open,
723 .pcm_close = intel_pcm_close,
726 .load_module = snd_sof_parse_module_memcpy,
728 /*Firmware loading */
729 .load_firmware = snd_sof_load_firmware_memcpy,
733 /* all 6 SSPs may be available for cherrytrail */
734 .num_drv = ARRAY_SIZE(byt_dai),
736 EXPORT_SYMBOL(sof_cht_ops);
738 const struct sof_intel_dsp_desc cht_chip_info = {
742 EXPORT_SYMBOL(cht_chip_info);
744 #endif /* CONFIG_SND_SOC_SOF_BAYTRAIL */
746 MODULE_LICENSE("Dual BSD/GPL");