1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
5 // Author: Daniel Baluta <daniel.baluta@nxp.com>
7 // Hardware interface for audio DSP on i.MX8
9 #include <linux/firmware.h>
10 #include <linux/of_platform.h>
11 #include <linux/of_address.h>
12 #include <linux/of_irq.h>
13 #include <linux/pm_domain.h>
15 #include <linux/module.h>
16 #include <sound/sof.h>
17 #include <sound/sof/xtensa.h>
18 #include <linux/firmware/imx/ipc.h>
19 #include <linux/firmware/imx/dsp.h>
21 #include <linux/firmware/imx/svc/misc.h>
22 #include <dt-bindings/firmware/imx/rsrc.h>
24 #include "../sof-of-dev.h"
25 #include "imx-common.h"
28 #define IRAM_OFFSET 0x10000
29 #define IRAM_SIZE (2 * 1024)
30 #define DRAM0_OFFSET 0x0
31 #define DRAM0_SIZE (32 * 1024)
32 #define DRAM1_OFFSET 0x8000
33 #define DRAM1_SIZE (32 * 1024)
34 #define SYSRAM_OFFSET 0x18000
35 #define SYSRAM_SIZE (256 * 1024)
36 #define SYSROM_OFFSET 0x58000
37 #define SYSROM_SIZE (192 * 1024)
39 #define RESET_VECTOR_VADDR 0x596f8000
41 #define MBOX_OFFSET 0x800000
42 #define MBOX_SIZE 0x1000
45 static struct clk_bulk_data imx8_dsp_clks[] = {
53 struct snd_sof_dev *sdev;
56 struct imx_dsp_ipc *dsp_ipc;
57 struct platform_device *ipc_dev;
59 /* System Controller IPC handler */
60 struct imx_sc_ipc *sc_ipc;
62 /* Power domain handling */
64 struct device **pd_dev;
65 struct device_link **link;
67 struct imx_clocks *clks;
70 static int imx8_get_mailbox_offset(struct snd_sof_dev *sdev)
75 static int imx8_get_window_offset(struct snd_sof_dev *sdev, u32 id)
80 static void imx8_dsp_handle_reply(struct imx_dsp_ipc *ipc)
82 struct imx8_priv *priv = imx_dsp_get_data(ipc);
85 spin_lock_irqsave(&priv->sdev->ipc_lock, flags);
86 snd_sof_ipc_process_reply(priv->sdev, 0);
87 spin_unlock_irqrestore(&priv->sdev->ipc_lock, flags);
90 static void imx8_dsp_handle_request(struct imx_dsp_ipc *ipc)
92 struct imx8_priv *priv = imx_dsp_get_data(ipc);
93 u32 p; /* panic code */
95 /* Read the message from the debug box. */
96 sof_mailbox_read(priv->sdev, priv->sdev->debug_box.offset + 4, &p, sizeof(p));
98 /* Check to see if the message is a panic code (0x0dead***) */
99 if ((p & SOF_IPC_PANIC_MAGIC_MASK) == SOF_IPC_PANIC_MAGIC)
100 snd_sof_dsp_panic(priv->sdev, p, true);
102 snd_sof_ipc_msgs_rx(priv->sdev);
105 static struct imx_dsp_ops dsp_ops = {
106 .handle_reply = imx8_dsp_handle_reply,
107 .handle_request = imx8_dsp_handle_request,
110 static int imx8_send_msg(struct snd_sof_dev *sdev, struct snd_sof_ipc_msg *msg)
112 struct imx8_priv *priv = sdev->pdata->hw_pdata;
114 sof_mailbox_write(sdev, sdev->host_box.offset, msg->msg_data,
116 imx_dsp_ring_doorbell(priv->dsp_ipc, 0);
124 static int imx8x_run(struct snd_sof_dev *sdev)
126 struct imx8_priv *dsp_priv = sdev->pdata->hw_pdata;
129 ret = imx_sc_misc_set_control(dsp_priv->sc_ipc, IMX_SC_R_DSP,
130 IMX_SC_C_OFS_SEL, 1);
132 dev_err(sdev->dev, "Error system address offset source select\n");
136 ret = imx_sc_misc_set_control(dsp_priv->sc_ipc, IMX_SC_R_DSP,
137 IMX_SC_C_OFS_AUDIO, 0x80);
139 dev_err(sdev->dev, "Error system address offset of AUDIO\n");
143 ret = imx_sc_misc_set_control(dsp_priv->sc_ipc, IMX_SC_R_DSP,
144 IMX_SC_C_OFS_PERIPH, 0x5A);
146 dev_err(sdev->dev, "Error system address offset of PERIPH %d\n",
151 ret = imx_sc_misc_set_control(dsp_priv->sc_ipc, IMX_SC_R_DSP,
152 IMX_SC_C_OFS_IRQ, 0x51);
154 dev_err(sdev->dev, "Error system address offset of IRQ\n");
158 imx_sc_pm_cpu_start(dsp_priv->sc_ipc, IMX_SC_R_DSP, true,
164 static int imx8_run(struct snd_sof_dev *sdev)
166 struct imx8_priv *dsp_priv = sdev->pdata->hw_pdata;
169 ret = imx_sc_misc_set_control(dsp_priv->sc_ipc, IMX_SC_R_DSP,
170 IMX_SC_C_OFS_SEL, 0);
172 dev_err(sdev->dev, "Error system address offset source select\n");
176 imx_sc_pm_cpu_start(dsp_priv->sc_ipc, IMX_SC_R_DSP, true,
182 static int imx8_probe(struct snd_sof_dev *sdev)
184 struct platform_device *pdev =
185 container_of(sdev->dev, struct platform_device, dev);
186 struct device_node *np = pdev->dev.of_node;
187 struct device_node *res_node;
188 struct resource *mmio;
189 struct imx8_priv *priv;
195 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
199 priv->clks = devm_kzalloc(&pdev->dev, sizeof(*priv->clks), GFP_KERNEL);
204 sdev->pdata->hw_pdata = priv;
205 priv->dev = sdev->dev;
208 /* power up device associated power domains */
209 priv->num_domains = of_count_phandle_with_args(np, "power-domains",
210 "#power-domain-cells");
211 if (priv->num_domains < 0) {
212 dev_err(sdev->dev, "no power-domains property in %pOF\n", np);
213 return priv->num_domains;
216 priv->pd_dev = devm_kmalloc_array(&pdev->dev, priv->num_domains,
217 sizeof(*priv->pd_dev), GFP_KERNEL);
221 priv->link = devm_kmalloc_array(&pdev->dev, priv->num_domains,
222 sizeof(*priv->link), GFP_KERNEL);
226 for (i = 0; i < priv->num_domains; i++) {
227 priv->pd_dev[i] = dev_pm_domain_attach_by_id(&pdev->dev, i);
228 if (IS_ERR(priv->pd_dev[i])) {
229 ret = PTR_ERR(priv->pd_dev[i]);
232 priv->link[i] = device_link_add(&pdev->dev, priv->pd_dev[i],
236 if (!priv->link[i]) {
238 dev_pm_domain_detach(priv->pd_dev[i], false);
243 ret = imx_scu_get_handle(&priv->sc_ipc);
245 dev_err(sdev->dev, "Cannot obtain SCU handle (err = %d)\n",
250 priv->ipc_dev = platform_device_register_data(sdev->dev, "imx-dsp",
252 pdev, sizeof(*pdev));
253 if (IS_ERR(priv->ipc_dev)) {
254 ret = PTR_ERR(priv->ipc_dev);
258 priv->dsp_ipc = dev_get_drvdata(&priv->ipc_dev->dev);
259 if (!priv->dsp_ipc) {
260 /* DSP IPC driver not probed yet, try later */
262 dev_err(sdev->dev, "Failed to get drvdata\n");
263 goto exit_pdev_unregister;
266 imx_dsp_set_data(priv->dsp_ipc, priv);
267 priv->dsp_ipc->ops = &dsp_ops;
270 mmio = platform_get_resource(pdev, IORESOURCE_MEM, 0);
273 size = resource_size(mmio);
275 dev_err(sdev->dev, "error: failed to get DSP base at idx 0\n");
277 goto exit_pdev_unregister;
280 sdev->bar[SOF_FW_BLK_TYPE_IRAM] = devm_ioremap(sdev->dev, base, size);
281 if (!sdev->bar[SOF_FW_BLK_TYPE_IRAM]) {
282 dev_err(sdev->dev, "failed to ioremap base 0x%x size 0x%x\n",
285 goto exit_pdev_unregister;
287 sdev->mmio_bar = SOF_FW_BLK_TYPE_IRAM;
289 res_node = of_parse_phandle(np, "memory-region", 0);
291 dev_err(&pdev->dev, "failed to get memory region node\n");
293 goto exit_pdev_unregister;
296 ret = of_address_to_resource(res_node, 0, &res);
297 of_node_put(res_node);
299 dev_err(&pdev->dev, "failed to get reserved region address\n");
300 goto exit_pdev_unregister;
303 sdev->bar[SOF_FW_BLK_TYPE_SRAM] = devm_ioremap_wc(sdev->dev, res.start,
304 resource_size(&res));
305 if (!sdev->bar[SOF_FW_BLK_TYPE_SRAM]) {
306 dev_err(sdev->dev, "failed to ioremap mem 0x%x size 0x%x\n",
309 goto exit_pdev_unregister;
311 sdev->mailbox_bar = SOF_FW_BLK_TYPE_SRAM;
313 /* set default mailbox offset for FW ready message */
314 sdev->dsp_box.offset = MBOX_OFFSET;
316 /* init clocks info */
317 priv->clks->dsp_clks = imx8_dsp_clks;
318 priv->clks->num_dsp_clks = ARRAY_SIZE(imx8_dsp_clks);
320 ret = imx8_parse_clocks(sdev, priv->clks);
322 goto exit_pdev_unregister;
324 ret = imx8_enable_clocks(sdev, priv->clks);
326 goto exit_pdev_unregister;
330 exit_pdev_unregister:
331 platform_device_unregister(priv->ipc_dev);
334 device_link_del(priv->link[i]);
335 dev_pm_domain_detach(priv->pd_dev[i], false);
341 static int imx8_remove(struct snd_sof_dev *sdev)
343 struct imx8_priv *priv = sdev->pdata->hw_pdata;
346 imx8_disable_clocks(sdev, priv->clks);
347 platform_device_unregister(priv->ipc_dev);
349 for (i = 0; i < priv->num_domains; i++) {
350 device_link_del(priv->link[i]);
351 dev_pm_domain_detach(priv->pd_dev[i], false);
357 /* on i.MX8 there is 1 to 1 match between type and BAR idx */
358 static int imx8_get_bar_index(struct snd_sof_dev *sdev, u32 type)
360 /* Only IRAM and SRAM bars are valid */
362 case SOF_FW_BLK_TYPE_IRAM:
363 case SOF_FW_BLK_TYPE_SRAM:
370 static void imx8_suspend(struct snd_sof_dev *sdev)
373 struct imx8_priv *priv = (struct imx8_priv *)sdev->pdata->hw_pdata;
375 for (i = 0; i < DSP_MU_CHAN_NUM; i++)
376 imx_dsp_free_channel(priv->dsp_ipc, i);
378 imx8_disable_clocks(sdev, priv->clks);
381 static int imx8_resume(struct snd_sof_dev *sdev)
383 struct imx8_priv *priv = (struct imx8_priv *)sdev->pdata->hw_pdata;
387 ret = imx8_enable_clocks(sdev, priv->clks);
391 for (i = 0; i < DSP_MU_CHAN_NUM; i++)
392 imx_dsp_request_channel(priv->dsp_ipc, i);
397 static int imx8_dsp_runtime_resume(struct snd_sof_dev *sdev)
400 const struct sof_dsp_power_state target_dsp_state = {
401 .state = SOF_DSP_PM_D0,
404 ret = imx8_resume(sdev);
408 return snd_sof_dsp_set_power_state(sdev, &target_dsp_state);
411 static int imx8_dsp_runtime_suspend(struct snd_sof_dev *sdev)
413 const struct sof_dsp_power_state target_dsp_state = {
414 .state = SOF_DSP_PM_D3,
419 return snd_sof_dsp_set_power_state(sdev, &target_dsp_state);
422 static int imx8_dsp_suspend(struct snd_sof_dev *sdev, unsigned int target_state)
424 const struct sof_dsp_power_state target_dsp_state = {
425 .state = target_state,
428 if (!pm_runtime_suspended(sdev->dev))
431 return snd_sof_dsp_set_power_state(sdev, &target_dsp_state);
434 static int imx8_dsp_resume(struct snd_sof_dev *sdev)
437 const struct sof_dsp_power_state target_dsp_state = {
438 .state = SOF_DSP_PM_D0,
441 ret = imx8_resume(sdev);
445 if (pm_runtime_suspended(sdev->dev)) {
446 pm_runtime_disable(sdev->dev);
447 pm_runtime_set_active(sdev->dev);
448 pm_runtime_mark_last_busy(sdev->dev);
449 pm_runtime_enable(sdev->dev);
450 pm_runtime_idle(sdev->dev);
453 return snd_sof_dsp_set_power_state(sdev, &target_dsp_state);
456 static struct snd_soc_dai_driver imx8_dai[] = {
481 static int imx8_dsp_set_power_state(struct snd_sof_dev *sdev,
482 const struct sof_dsp_power_state *target_state)
484 sdev->dsp_power_state = *target_state;
490 static struct snd_sof_dsp_ops sof_imx8_ops = {
491 /* probe and remove */
493 .remove = imx8_remove,
498 .block_read = sof_block_read,
499 .block_write = sof_block_write,
502 .mailbox_read = sof_mailbox_read,
503 .mailbox_write = sof_mailbox_write,
506 .send_msg = imx8_send_msg,
507 .get_mailbox_offset = imx8_get_mailbox_offset,
508 .get_window_offset = imx8_get_window_offset,
510 .ipc_msg_data = sof_ipc_msg_data,
511 .set_stream_data_offset = sof_set_stream_data_offset,
513 .get_bar_index = imx8_get_bar_index,
515 /* firmware loading */
516 .load_firmware = snd_sof_load_firmware_memcpy,
518 /* Debug information */
519 .dbg_dump = imx8_dump,
520 .debugfs_add_region_item = snd_sof_debugfs_add_region_item_iomem,
522 /* stream callbacks */
523 .pcm_open = sof_stream_pcm_open,
524 .pcm_close = sof_stream_pcm_close,
527 .dsp_arch_ops = &sof_xtensa_arch_ops,
531 .num_drv = ARRAY_SIZE(imx8_dai),
533 /* ALSA HW info flags */
534 .hw_info = SNDRV_PCM_INFO_MMAP |
535 SNDRV_PCM_INFO_MMAP_VALID |
536 SNDRV_PCM_INFO_INTERLEAVED |
537 SNDRV_PCM_INFO_PAUSE |
538 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP,
541 .runtime_suspend = imx8_dsp_runtime_suspend,
542 .runtime_resume = imx8_dsp_runtime_resume,
544 .suspend = imx8_dsp_suspend,
545 .resume = imx8_dsp_resume,
547 .set_power_state = imx8_dsp_set_power_state,
551 static struct snd_sof_dsp_ops sof_imx8x_ops = {
552 /* probe and remove */
554 .remove = imx8_remove,
559 .block_read = sof_block_read,
560 .block_write = sof_block_write,
563 .mailbox_read = sof_mailbox_read,
564 .mailbox_write = sof_mailbox_write,
567 .send_msg = imx8_send_msg,
568 .get_mailbox_offset = imx8_get_mailbox_offset,
569 .get_window_offset = imx8_get_window_offset,
571 .ipc_msg_data = sof_ipc_msg_data,
572 .set_stream_data_offset = sof_set_stream_data_offset,
574 .get_bar_index = imx8_get_bar_index,
576 /* firmware loading */
577 .load_firmware = snd_sof_load_firmware_memcpy,
579 /* Debug information */
580 .dbg_dump = imx8_dump,
581 .debugfs_add_region_item = snd_sof_debugfs_add_region_item_iomem,
583 /* stream callbacks */
584 .pcm_open = sof_stream_pcm_open,
585 .pcm_close = sof_stream_pcm_close,
588 .dsp_arch_ops = &sof_xtensa_arch_ops,
592 .num_drv = ARRAY_SIZE(imx8_dai),
595 .runtime_suspend = imx8_dsp_runtime_suspend,
596 .runtime_resume = imx8_dsp_runtime_resume,
598 .suspend = imx8_dsp_suspend,
599 .resume = imx8_dsp_resume,
601 .set_power_state = imx8_dsp_set_power_state,
603 /* ALSA HW info flags */
604 .hw_info = SNDRV_PCM_INFO_MMAP |
605 SNDRV_PCM_INFO_MMAP_VALID |
606 SNDRV_PCM_INFO_INTERLEAVED |
607 SNDRV_PCM_INFO_PAUSE |
608 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP
611 static struct sof_dev_desc sof_of_imx8qxp_desc = {
612 .ipc_supported_mask = BIT(SOF_IPC),
613 .ipc_default = SOF_IPC,
615 [SOF_IPC] = "imx/sof",
617 .default_tplg_path = {
618 [SOF_IPC] = "imx/sof-tplg",
620 .default_fw_filename = {
621 [SOF_IPC] = "sof-imx8x.ri",
623 .nocodec_tplg_filename = "sof-imx8-nocodec.tplg",
624 .ops = &sof_imx8x_ops,
627 static struct sof_dev_desc sof_of_imx8qm_desc = {
628 .ipc_supported_mask = BIT(SOF_IPC),
629 .ipc_default = SOF_IPC,
631 [SOF_IPC] = "imx/sof",
633 .default_tplg_path = {
634 [SOF_IPC] = "imx/sof-tplg",
636 .default_fw_filename = {
637 [SOF_IPC] = "sof-imx8.ri",
639 .nocodec_tplg_filename = "sof-imx8-nocodec.tplg",
640 .ops = &sof_imx8_ops,
643 static const struct of_device_id sof_of_imx8_ids[] = {
644 { .compatible = "fsl,imx8qxp-dsp", .data = &sof_of_imx8qxp_desc},
645 { .compatible = "fsl,imx8qm-dsp", .data = &sof_of_imx8qm_desc},
648 MODULE_DEVICE_TABLE(of, sof_of_imx8_ids);
650 /* DT driver definition */
651 static struct platform_driver snd_sof_of_imx8_driver = {
652 .probe = sof_of_probe,
653 .remove = sof_of_remove,
655 .name = "sof-audio-of-imx8",
657 .of_match_table = sof_of_imx8_ids,
660 module_platform_driver(snd_sof_of_imx8_driver);
662 MODULE_IMPORT_NS(SND_SOC_SOF_XTENSA);
663 MODULE_LICENSE("Dual BSD/GPL");