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.MX8M
9 #include <linux/firmware.h>
10 #include <linux/of_platform.h>
11 #include <linux/of_address.h>
12 #include <linux/of_irq.h>
14 #include <linux/module.h>
15 #include <sound/sof.h>
16 #include <sound/sof/xtensa.h>
17 #include <linux/firmware/imx/dsp.h>
20 #include "imx-common.h"
22 #define MBOX_OFFSET 0x800000
23 #define MBOX_SIZE 0x1000
27 struct snd_sof_dev *sdev;
30 struct imx_dsp_ipc *dsp_ipc;
31 struct platform_device *ipc_dev;
34 static void imx8m_get_reply(struct snd_sof_dev *sdev)
36 struct snd_sof_ipc_msg *msg = sdev->msg;
37 struct sof_ipc_reply reply;
41 dev_warn(sdev->dev, "unexpected ipc interrupt\n");
46 sof_mailbox_read(sdev, sdev->host_box.offset, &reply, sizeof(reply));
48 if (reply.error < 0) {
49 memcpy(msg->reply_data, &reply, sizeof(reply));
52 /* reply has correct size? */
53 if (reply.hdr.size != msg->reply_size) {
54 dev_err(sdev->dev, "error: reply expected %zu got %u bytes\n",
55 msg->reply_size, reply.hdr.size);
59 /* read the message */
60 if (msg->reply_size > 0)
61 sof_mailbox_read(sdev, sdev->host_box.offset,
62 msg->reply_data, msg->reply_size);
65 msg->reply_error = ret;
68 static int imx8m_get_mailbox_offset(struct snd_sof_dev *sdev)
73 static int imx8m_get_window_offset(struct snd_sof_dev *sdev, u32 id)
78 static void imx8m_dsp_handle_reply(struct imx_dsp_ipc *ipc)
80 struct imx8m_priv *priv = imx_dsp_get_data(ipc);
83 spin_lock_irqsave(&priv->sdev->ipc_lock, flags);
84 imx8m_get_reply(priv->sdev);
85 snd_sof_ipc_reply(priv->sdev, 0);
86 spin_unlock_irqrestore(&priv->sdev->ipc_lock, flags);
89 static void imx8m_dsp_handle_request(struct imx_dsp_ipc *ipc)
91 struct imx8m_priv *priv = imx_dsp_get_data(ipc);
92 u32 p; /* Panic code */
94 /* Read the message from the debug box. */
95 sof_mailbox_read(priv->sdev, priv->sdev->debug_box.offset + 4, &p, sizeof(p));
97 /* Check to see if the message is a panic code (0x0dead***) */
98 if ((p & SOF_IPC_PANIC_MAGIC_MASK) == SOF_IPC_PANIC_MAGIC)
99 snd_sof_dsp_panic(priv->sdev, p);
101 snd_sof_ipc_msgs_rx(priv->sdev);
104 static struct imx_dsp_ops imx8m_dsp_ops = {
105 .handle_reply = imx8m_dsp_handle_reply,
106 .handle_request = imx8m_dsp_handle_request,
109 static int imx8m_send_msg(struct snd_sof_dev *sdev, struct snd_sof_ipc_msg *msg)
111 struct imx8m_priv *priv = sdev->pdata->hw_pdata;
113 sof_mailbox_write(sdev, sdev->host_box.offset, msg->msg_data,
115 imx_dsp_ring_doorbell(priv->dsp_ipc, 0);
123 static int imx8m_run(struct snd_sof_dev *sdev)
125 /* TODO: start DSP using Audio MIX bits */
129 static int imx8m_probe(struct snd_sof_dev *sdev)
131 struct platform_device *pdev =
132 container_of(sdev->dev, struct platform_device, dev);
133 struct device_node *np = pdev->dev.of_node;
134 struct device_node *res_node;
135 struct resource *mmio;
136 struct imx8m_priv *priv;
141 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
145 sdev->pdata->hw_pdata = priv;
146 priv->dev = sdev->dev;
149 priv->ipc_dev = platform_device_register_data(sdev->dev, "imx-dsp",
151 pdev, sizeof(*pdev));
152 if (IS_ERR(priv->ipc_dev))
153 return PTR_ERR(priv->ipc_dev);
155 priv->dsp_ipc = dev_get_drvdata(&priv->ipc_dev->dev);
156 if (!priv->dsp_ipc) {
157 /* DSP IPC driver not probed yet, try later */
159 dev_err(sdev->dev, "Failed to get drvdata\n");
160 goto exit_pdev_unregister;
163 imx_dsp_set_data(priv->dsp_ipc, priv);
164 priv->dsp_ipc->ops = &imx8m_dsp_ops;
167 mmio = platform_get_resource(pdev, IORESOURCE_MEM, 0);
170 size = resource_size(mmio);
172 dev_err(sdev->dev, "error: failed to get DSP base at idx 0\n");
174 goto exit_pdev_unregister;
177 sdev->bar[SOF_FW_BLK_TYPE_IRAM] = devm_ioremap(sdev->dev, base, size);
178 if (!sdev->bar[SOF_FW_BLK_TYPE_IRAM]) {
179 dev_err(sdev->dev, "failed to ioremap base 0x%x size 0x%x\n",
182 goto exit_pdev_unregister;
184 sdev->mmio_bar = SOF_FW_BLK_TYPE_IRAM;
186 res_node = of_parse_phandle(np, "memory-region", 0);
188 dev_err(&pdev->dev, "failed to get memory region node\n");
190 goto exit_pdev_unregister;
193 ret = of_address_to_resource(res_node, 0, &res);
195 dev_err(&pdev->dev, "failed to get reserved region address\n");
196 goto exit_pdev_unregister;
199 sdev->bar[SOF_FW_BLK_TYPE_SRAM] = devm_ioremap_wc(sdev->dev, res.start,
200 resource_size(&res));
201 if (!sdev->bar[SOF_FW_BLK_TYPE_SRAM]) {
202 dev_err(sdev->dev, "failed to ioremap mem 0x%x size 0x%x\n",
205 goto exit_pdev_unregister;
207 sdev->mailbox_bar = SOF_FW_BLK_TYPE_SRAM;
209 /* set default mailbox offset for FW ready message */
210 sdev->dsp_box.offset = MBOX_OFFSET;
214 exit_pdev_unregister:
215 platform_device_unregister(priv->ipc_dev);
219 static int imx8m_remove(struct snd_sof_dev *sdev)
221 struct imx8m_priv *priv = sdev->pdata->hw_pdata;
223 platform_device_unregister(priv->ipc_dev);
228 /* on i.MX8 there is 1 to 1 match between type and BAR idx */
229 static int imx8m_get_bar_index(struct snd_sof_dev *sdev, u32 type)
234 static void imx8m_ipc_msg_data(struct snd_sof_dev *sdev,
235 struct snd_pcm_substream *substream,
238 sof_mailbox_read(sdev, sdev->dsp_box.offset, p, sz);
241 static int imx8m_ipc_pcm_params(struct snd_sof_dev *sdev,
242 struct snd_pcm_substream *substream,
243 const struct sof_ipc_pcm_params_reply *reply)
248 static struct snd_soc_dai_driver imx8m_dai[] = {
263 struct snd_sof_dsp_ops sof_imx8m_ops = {
264 /* probe and remove */
265 .probe = imx8m_probe,
266 .remove = imx8m_remove,
271 .block_read = sof_block_read,
272 .block_write = sof_block_write,
275 .read64 = sof_io_read64,
278 .send_msg = imx8m_send_msg,
279 .fw_ready = sof_fw_ready,
280 .get_mailbox_offset = imx8m_get_mailbox_offset,
281 .get_window_offset = imx8m_get_window_offset,
283 .ipc_msg_data = imx8m_ipc_msg_data,
284 .ipc_pcm_params = imx8m_ipc_pcm_params,
287 .load_module = snd_sof_parse_module_memcpy,
288 .get_bar_index = imx8m_get_bar_index,
289 /* firmware loading */
290 .load_firmware = snd_sof_load_firmware_memcpy,
292 /* Debug information */
293 .dbg_dump = imx8_dump,
296 .arch_ops = &sof_xtensa_arch_ops,
300 .num_drv = ARRAY_SIZE(imx8m_dai),
302 .hw_info = SNDRV_PCM_INFO_MMAP |
303 SNDRV_PCM_INFO_MMAP_VALID |
304 SNDRV_PCM_INFO_INTERLEAVED |
305 SNDRV_PCM_INFO_PAUSE |
306 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP,
308 EXPORT_SYMBOL(sof_imx8m_ops);
310 MODULE_IMPORT_NS(SND_SOC_SOF_XTENSA);
311 MODULE_LICENSE("Dual BSD/GPL");