1 // SPDX-License-Identifier: (GPL-2.0 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>
26 #define IRAM_OFFSET 0x10000
27 #define IRAM_SIZE (2 * 1024)
28 #define DRAM0_OFFSET 0x0
29 #define DRAM0_SIZE (32 * 1024)
30 #define DRAM1_OFFSET 0x8000
31 #define DRAM1_SIZE (32 * 1024)
32 #define SYSRAM_OFFSET 0x18000
33 #define SYSRAM_SIZE (256 * 1024)
34 #define SYSROM_OFFSET 0x58000
35 #define SYSROM_SIZE (192 * 1024)
37 #define RESET_VECTOR_VADDR 0x596f8000
39 #define MBOX_OFFSET 0x800000
40 #define MBOX_SIZE 0x1000
44 struct snd_sof_dev *sdev;
47 struct imx_dsp_ipc *dsp_ipc;
48 struct platform_device *ipc_dev;
50 /* System Controller IPC handler */
51 struct imx_sc_ipc *sc_ipc;
53 /* Power domain handling */
55 struct device **pd_dev;
56 struct device_link **link;
60 static void imx8_get_reply(struct snd_sof_dev *sdev)
62 struct snd_sof_ipc_msg *msg = sdev->msg;
63 struct sof_ipc_reply reply;
67 dev_warn(sdev->dev, "unexpected ipc interrupt\n");
72 sof_mailbox_read(sdev, sdev->host_box.offset, &reply, sizeof(reply));
74 if (reply.error < 0) {
75 memcpy(msg->reply_data, &reply, sizeof(reply));
78 /* reply has correct size? */
79 if (reply.hdr.size != msg->reply_size) {
80 dev_err(sdev->dev, "error: reply expected %zu got %u bytes\n",
81 msg->reply_size, reply.hdr.size);
85 /* read the message */
86 if (msg->reply_size > 0)
87 sof_mailbox_read(sdev, sdev->host_box.offset,
88 msg->reply_data, msg->reply_size);
91 msg->reply_error = ret;
94 static int imx8_get_mailbox_offset(struct snd_sof_dev *sdev)
99 static int imx8_get_window_offset(struct snd_sof_dev *sdev, u32 id)
104 static void imx8_dsp_handle_reply(struct imx_dsp_ipc *ipc)
106 struct imx8_priv *priv = imx_dsp_get_data(ipc);
109 spin_lock_irqsave(&priv->sdev->ipc_lock, flags);
110 imx8_get_reply(priv->sdev);
111 snd_sof_ipc_reply(priv->sdev, 0);
112 spin_unlock_irqrestore(&priv->sdev->ipc_lock, flags);
115 static void imx8_dsp_handle_request(struct imx_dsp_ipc *ipc)
117 struct imx8_priv *priv = imx_dsp_get_data(ipc);
119 snd_sof_ipc_msgs_rx(priv->sdev);
122 struct imx_dsp_ops dsp_ops = {
123 .handle_reply = imx8_dsp_handle_reply,
124 .handle_request = imx8_dsp_handle_request,
127 static int imx8_send_msg(struct snd_sof_dev *sdev, struct snd_sof_ipc_msg *msg)
129 struct imx8_priv *priv = (struct imx8_priv *)sdev->private;
131 sof_mailbox_write(sdev, sdev->host_box.offset, msg->msg_data,
133 imx_dsp_ring_doorbell(priv->dsp_ipc, 0);
141 static int imx8_run(struct snd_sof_dev *sdev)
143 struct imx8_priv *dsp_priv = (struct imx8_priv *)sdev->private;
146 ret = imx_sc_misc_set_control(dsp_priv->sc_ipc, IMX_SC_R_DSP,
147 IMX_SC_C_OFS_SEL, 1);
149 dev_err(sdev->dev, "Error system address offset source select\n");
153 ret = imx_sc_misc_set_control(dsp_priv->sc_ipc, IMX_SC_R_DSP,
154 IMX_SC_C_OFS_AUDIO, 0x80);
156 dev_err(sdev->dev, "Error system address offset of AUDIO\n");
160 ret = imx_sc_misc_set_control(dsp_priv->sc_ipc, IMX_SC_R_DSP,
161 IMX_SC_C_OFS_PERIPH, 0x5A);
163 dev_err(sdev->dev, "Error system address offset of PERIPH %d\n",
168 ret = imx_sc_misc_set_control(dsp_priv->sc_ipc, IMX_SC_R_DSP,
169 IMX_SC_C_OFS_IRQ, 0x51);
171 dev_err(sdev->dev, "Error system address offset of IRQ\n");
175 imx_sc_pm_cpu_start(dsp_priv->sc_ipc, IMX_SC_R_DSP, true,
181 static int imx8_probe(struct snd_sof_dev *sdev)
183 struct platform_device *pdev =
184 container_of(sdev->dev, struct platform_device, dev);
185 struct device_node *np = pdev->dev.of_node;
186 struct device_node *res_node;
187 struct resource *mmio;
188 struct imx8_priv *priv;
194 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
198 sdev->private = priv;
199 priv->dev = sdev->dev;
202 /* power up device associated power domains */
203 priv->num_domains = of_count_phandle_with_args(np, "power-domains",
204 "#power-domain-cells");
205 if (priv->num_domains < 0) {
206 dev_err(sdev->dev, "no power-domains property in %pOF\n", np);
207 return priv->num_domains;
210 priv->pd_dev = devm_kmalloc_array(&pdev->dev, priv->num_domains,
211 sizeof(*priv->pd_dev), GFP_KERNEL);
215 priv->link = devm_kmalloc_array(&pdev->dev, priv->num_domains,
216 sizeof(*priv->link), GFP_KERNEL);
220 for (i = 0; i < priv->num_domains; i++) {
221 priv->pd_dev[i] = dev_pm_domain_attach_by_id(&pdev->dev, i);
222 if (IS_ERR(priv->pd_dev[i])) {
223 ret = PTR_ERR(priv->pd_dev[i]);
226 priv->link[i] = device_link_add(&pdev->dev, priv->pd_dev[i],
230 if (!priv->link[i]) {
232 dev_pm_domain_detach(priv->pd_dev[i], false);
237 ret = imx_scu_get_handle(&priv->sc_ipc);
239 dev_err(sdev->dev, "Cannot obtain SCU handle (err = %d)\n",
244 priv->ipc_dev = platform_device_register_data(sdev->dev, "imx-dsp",
246 pdev, sizeof(*pdev));
247 if (IS_ERR(priv->ipc_dev)) {
248 ret = PTR_ERR(priv->ipc_dev);
252 priv->dsp_ipc = dev_get_drvdata(&priv->ipc_dev->dev);
253 if (!priv->dsp_ipc) {
254 /* DSP IPC driver not probed yet, try later */
256 dev_err(sdev->dev, "Failed to get drvdata\n");
257 goto exit_pdev_unregister;
260 imx_dsp_set_data(priv->dsp_ipc, priv);
261 priv->dsp_ipc->ops = &dsp_ops;
264 mmio = platform_get_resource(pdev, IORESOURCE_MEM, 0);
267 size = resource_size(mmio);
269 dev_err(sdev->dev, "error: failed to get DSP base at idx 0\n");
271 goto exit_pdev_unregister;
274 sdev->bar[SOF_FW_BLK_TYPE_IRAM] = devm_ioremap(sdev->dev, base, size);
275 if (!sdev->bar[SOF_FW_BLK_TYPE_IRAM]) {
276 dev_err(sdev->dev, "failed to ioremap base 0x%x size 0x%x\n",
279 goto exit_pdev_unregister;
281 sdev->mmio_bar = SOF_FW_BLK_TYPE_IRAM;
283 res_node = of_parse_phandle(np, "memory-region", 0);
285 dev_err(&pdev->dev, "failed to get memory region node\n");
287 goto exit_pdev_unregister;
290 ret = of_address_to_resource(res_node, 0, &res);
292 dev_err(&pdev->dev, "failed to get reserved region address\n");
293 goto exit_pdev_unregister;
296 sdev->bar[SOF_FW_BLK_TYPE_SRAM] = devm_ioremap_wc(sdev->dev, res.start,
297 resource_size(&res));
298 if (!sdev->bar[SOF_FW_BLK_TYPE_SRAM]) {
299 dev_err(sdev->dev, "failed to ioremap mem 0x%x size 0x%x\n",
302 goto exit_pdev_unregister;
304 sdev->mailbox_bar = SOF_FW_BLK_TYPE_SRAM;
306 /* set default mailbox offset for FW ready message */
307 sdev->dsp_box.offset = MBOX_OFFSET;
311 exit_pdev_unregister:
312 platform_device_unregister(priv->ipc_dev);
315 device_link_del(priv->link[i]);
316 dev_pm_domain_detach(priv->pd_dev[i], false);
322 static int imx8_remove(struct snd_sof_dev *sdev)
324 struct imx8_priv *priv = (struct imx8_priv *)sdev->private;
327 platform_device_unregister(priv->ipc_dev);
329 for (i = 0; i < priv->num_domains; i++) {
330 device_link_del(priv->link[i]);
331 dev_pm_domain_detach(priv->pd_dev[i], false);
337 /* on i.MX8 there is 1 to 1 match between type and BAR idx */
338 static int imx8_get_bar_index(struct snd_sof_dev *sdev, u32 type)
343 static void imx8_ipc_msg_data(struct snd_sof_dev *sdev,
344 struct snd_pcm_substream *substream,
347 sof_mailbox_read(sdev, sdev->dsp_box.offset, p, sz);
350 static int imx8_ipc_pcm_params(struct snd_sof_dev *sdev,
351 struct snd_pcm_substream *substream,
352 const struct sof_ipc_pcm_params_reply *reply)
357 static struct snd_soc_dai_driver imx8_dai[] = {
364 struct snd_sof_dsp_ops sof_imx8_ops = {
365 /* probe and remove */
367 .remove = imx8_remove,
372 .block_read = sof_block_read,
373 .block_write = sof_block_write,
376 .send_msg = imx8_send_msg,
377 .fw_ready = sof_fw_ready,
378 .get_mailbox_offset = imx8_get_mailbox_offset,
379 .get_window_offset = imx8_get_window_offset,
381 .ipc_msg_data = imx8_ipc_msg_data,
382 .ipc_pcm_params = imx8_ipc_pcm_params,
385 .load_module = snd_sof_parse_module_memcpy,
386 .get_bar_index = imx8_get_bar_index,
387 /* firmware loading */
388 .load_firmware = snd_sof_load_firmware_memcpy,
392 .num_drv = 1, /* we have only 1 ESAI interface on i.MX8 */
394 /* ALSA HW info flags */
395 .hw_info = SNDRV_PCM_INFO_MMAP |
396 SNDRV_PCM_INFO_MMAP_VALID |
397 SNDRV_PCM_INFO_INTERLEAVED |
398 SNDRV_PCM_INFO_PAUSE |
399 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP
401 EXPORT_SYMBOL(sof_imx8_ops);
403 MODULE_LICENSE("Dual BSD/GPL");