1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
3 // Copyright 2021-2022 NXP
5 // Author: Peng Zhang <peng.zhang_8@nxp.com>
7 // Hardware interface for audio DSP on i.MX8ULP
9 #include <linux/arm-smccc.h>
10 #include <linux/clk.h>
11 #include <linux/firmware.h>
12 #include <linux/firmware/imx/dsp.h>
13 #include <linux/firmware/imx/ipc.h>
14 #include <linux/firmware/imx/svc/misc.h>
15 #include <linux/mfd/syscon.h>
16 #include <linux/module.h>
17 #include <linux/of_address.h>
18 #include <linux/of_irq.h>
19 #include <linux/of_platform.h>
20 #include <linux/of_reserved_mem.h>
22 #include <sound/sof.h>
23 #include <sound/sof/xtensa.h>
26 #include "../sof-of-dev.h"
27 #include "imx-common.h"
29 #define FSL_SIP_HIFI_XRDC 0xc200000e
31 /* SIM Domain register */
33 #define EXECUTE_BIT BIT(13)
34 #define RESET_BIT BIT(16)
35 #define HIFI4_CLK_BIT BIT(17)
36 #define PB_CLK_BIT BIT(18)
37 #define PLAT_CLK_BIT BIT(19)
38 #define DEBUG_LOGIC_BIT BIT(25)
40 #define MBOX_OFFSET 0x800000
41 #define MBOX_SIZE 0x1000
43 static struct clk_bulk_data imx8ulp_dsp_clks[] = {
52 struct snd_sof_dev *sdev;
55 struct imx_dsp_ipc *dsp_ipc;
56 struct platform_device *ipc_dev;
58 struct regmap *regmap;
59 struct imx_clocks *clks;
62 static void imx8ulp_sim_lpav_start(struct imx8ulp_priv *priv)
64 /* Controls the HiFi4 DSP Reset: 1 in reset, 0 out of reset */
65 regmap_update_bits(priv->regmap, SYSCTRL0, RESET_BIT, 0);
67 /* Reset HiFi4 DSP Debug logic: 1 debug reset, 0 out of reset*/
68 regmap_update_bits(priv->regmap, SYSCTRL0, DEBUG_LOGIC_BIT, 0);
70 /* Stall HIFI4 DSP Execution: 1 stall, 0 run */
71 regmap_update_bits(priv->regmap, SYSCTRL0, EXECUTE_BIT, 0);
74 static int imx8ulp_get_mailbox_offset(struct snd_sof_dev *sdev)
79 static int imx8ulp_get_window_offset(struct snd_sof_dev *sdev, u32 id)
84 static void imx8ulp_dsp_handle_reply(struct imx_dsp_ipc *ipc)
86 struct imx8ulp_priv *priv = imx_dsp_get_data(ipc);
89 spin_lock_irqsave(&priv->sdev->ipc_lock, flags);
91 snd_sof_ipc_process_reply(priv->sdev, 0);
93 spin_unlock_irqrestore(&priv->sdev->ipc_lock, flags);
96 static void imx8ulp_dsp_handle_request(struct imx_dsp_ipc *ipc)
98 struct imx8ulp_priv *priv = imx_dsp_get_data(ipc);
99 u32 p; /* panic code */
101 /* Read the message from the debug box. */
102 sof_mailbox_read(priv->sdev, priv->sdev->debug_box.offset + 4, &p, sizeof(p));
104 /* Check to see if the message is a panic code (0x0dead***) */
105 if ((p & SOF_IPC_PANIC_MAGIC_MASK) == SOF_IPC_PANIC_MAGIC)
106 snd_sof_dsp_panic(priv->sdev, p, true);
108 snd_sof_ipc_msgs_rx(priv->sdev);
111 static struct imx_dsp_ops dsp_ops = {
112 .handle_reply = imx8ulp_dsp_handle_reply,
113 .handle_request = imx8ulp_dsp_handle_request,
116 static int imx8ulp_send_msg(struct snd_sof_dev *sdev, struct snd_sof_ipc_msg *msg)
118 struct imx8ulp_priv *priv = sdev->pdata->hw_pdata;
120 sof_mailbox_write(sdev, sdev->host_box.offset, msg->msg_data,
122 imx_dsp_ring_doorbell(priv->dsp_ipc, 0);
127 static int imx8ulp_run(struct snd_sof_dev *sdev)
129 struct imx8ulp_priv *priv = sdev->pdata->hw_pdata;
131 imx8ulp_sim_lpav_start(priv);
136 static int imx8ulp_reset(struct snd_sof_dev *sdev)
138 struct imx8ulp_priv *priv = sdev->pdata->hw_pdata;
139 struct arm_smccc_res smc_resource;
141 /* HiFi4 Platform Clock Enable: 1 enabled, 0 disabled */
142 regmap_update_bits(priv->regmap, SYSCTRL0, PLAT_CLK_BIT, PLAT_CLK_BIT);
144 /* HiFi4 PBCLK clock enable: 1 enabled, 0 disabled */
145 regmap_update_bits(priv->regmap, SYSCTRL0, PB_CLK_BIT, PB_CLK_BIT);
147 /* HiFi4 Clock Enable: 1 enabled, 0 disabled */
148 regmap_update_bits(priv->regmap, SYSCTRL0, HIFI4_CLK_BIT, HIFI4_CLK_BIT);
150 regmap_update_bits(priv->regmap, SYSCTRL0, RESET_BIT, RESET_BIT);
153 /* Stall HIFI4 DSP Execution: 1 stall, 0 not stall */
154 regmap_update_bits(priv->regmap, SYSCTRL0, EXECUTE_BIT, EXECUTE_BIT);
157 arm_smccc_smc(FSL_SIP_HIFI_XRDC, 0, 0, 0, 0, 0, 0, 0, &smc_resource);
162 static int imx8ulp_probe(struct snd_sof_dev *sdev)
164 struct platform_device *pdev =
165 container_of(sdev->dev, struct platform_device, dev);
166 struct device_node *np = pdev->dev.of_node;
167 struct device_node *res_node;
168 struct resource *mmio;
169 struct imx8ulp_priv *priv;
174 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
178 priv->clks = devm_kzalloc(&pdev->dev, sizeof(*priv->clks), GFP_KERNEL);
183 sdev->pdata->hw_pdata = priv;
184 priv->dev = sdev->dev;
187 /* System integration module(SIM) control dsp configuration */
188 priv->regmap = syscon_regmap_lookup_by_phandle(np, "fsl,dsp-ctrl");
189 if (IS_ERR(priv->regmap))
190 return PTR_ERR(priv->regmap);
192 priv->ipc_dev = platform_device_register_data(sdev->dev, "imx-dsp",
194 pdev, sizeof(*pdev));
195 if (IS_ERR(priv->ipc_dev))
196 return PTR_ERR(priv->ipc_dev);
198 priv->dsp_ipc = dev_get_drvdata(&priv->ipc_dev->dev);
199 if (!priv->dsp_ipc) {
200 /* DSP IPC driver not probed yet, try later */
202 dev_err(sdev->dev, "Failed to get drvdata\n");
203 goto exit_pdev_unregister;
206 imx_dsp_set_data(priv->dsp_ipc, priv);
207 priv->dsp_ipc->ops = &dsp_ops;
210 mmio = platform_get_resource(pdev, IORESOURCE_MEM, 0);
213 size = resource_size(mmio);
215 dev_err(sdev->dev, "error: failed to get DSP base at idx 0\n");
217 goto exit_pdev_unregister;
220 sdev->bar[SOF_FW_BLK_TYPE_IRAM] = devm_ioremap(sdev->dev, base, size);
221 if (!sdev->bar[SOF_FW_BLK_TYPE_IRAM]) {
222 dev_err(sdev->dev, "failed to ioremap base 0x%x size 0x%x\n",
225 goto exit_pdev_unregister;
227 sdev->mmio_bar = SOF_FW_BLK_TYPE_IRAM;
229 res_node = of_parse_phandle(np, "memory-reserved", 0);
231 dev_err(&pdev->dev, "failed to get memory region node\n");
233 goto exit_pdev_unregister;
236 ret = of_address_to_resource(res_node, 0, &res);
237 of_node_put(res_node);
239 dev_err(&pdev->dev, "failed to get reserved region address\n");
240 goto exit_pdev_unregister;
243 sdev->bar[SOF_FW_BLK_TYPE_SRAM] = devm_ioremap_wc(sdev->dev, res.start,
244 resource_size(&res));
245 if (!sdev->bar[SOF_FW_BLK_TYPE_SRAM]) {
246 dev_err(sdev->dev, "failed to ioremap mem 0x%x size 0x%x\n",
249 goto exit_pdev_unregister;
251 sdev->mailbox_bar = SOF_FW_BLK_TYPE_SRAM;
253 /* set default mailbox offset for FW ready message */
254 sdev->dsp_box.offset = MBOX_OFFSET;
256 ret = of_reserved_mem_device_init(sdev->dev);
258 dev_err(&pdev->dev, "failed to init reserved memory region %d\n", ret);
259 goto exit_pdev_unregister;
262 priv->clks->dsp_clks = imx8ulp_dsp_clks;
263 priv->clks->num_dsp_clks = ARRAY_SIZE(imx8ulp_dsp_clks);
265 ret = imx8_parse_clocks(sdev, priv->clks);
267 goto exit_pdev_unregister;
269 ret = imx8_enable_clocks(sdev, priv->clks);
271 goto exit_pdev_unregister;
275 exit_pdev_unregister:
276 platform_device_unregister(priv->ipc_dev);
281 static int imx8ulp_remove(struct snd_sof_dev *sdev)
283 struct imx8ulp_priv *priv = sdev->pdata->hw_pdata;
285 imx8_disable_clocks(sdev, priv->clks);
286 platform_device_unregister(priv->ipc_dev);
291 /* on i.MX8 there is 1 to 1 match between type and BAR idx */
292 static int imx8ulp_get_bar_index(struct snd_sof_dev *sdev, u32 type)
297 static int imx8ulp_suspend(struct snd_sof_dev *sdev)
300 struct imx8ulp_priv *priv = (struct imx8ulp_priv *)sdev->pdata->hw_pdata;
302 /*Stall DSP, release in .run() */
303 regmap_update_bits(priv->regmap, SYSCTRL0, EXECUTE_BIT, EXECUTE_BIT);
305 for (i = 0; i < DSP_MU_CHAN_NUM; i++)
306 imx_dsp_free_channel(priv->dsp_ipc, i);
308 imx8_disable_clocks(sdev, priv->clks);
313 static int imx8ulp_resume(struct snd_sof_dev *sdev)
315 struct imx8ulp_priv *priv = (struct imx8ulp_priv *)sdev->pdata->hw_pdata;
318 imx8_enable_clocks(sdev, priv->clks);
320 for (i = 0; i < DSP_MU_CHAN_NUM; i++)
321 imx_dsp_request_channel(priv->dsp_ipc, i);
326 static int imx8ulp_dsp_runtime_resume(struct snd_sof_dev *sdev)
328 const struct sof_dsp_power_state target_dsp_state = {
329 .state = SOF_DSP_PM_D0,
333 imx8ulp_resume(sdev);
335 return snd_sof_dsp_set_power_state(sdev, &target_dsp_state);
338 static int imx8ulp_dsp_runtime_suspend(struct snd_sof_dev *sdev)
340 const struct sof_dsp_power_state target_dsp_state = {
341 .state = SOF_DSP_PM_D3,
345 imx8ulp_suspend(sdev);
347 return snd_sof_dsp_set_power_state(sdev, &target_dsp_state);
350 static int imx8ulp_dsp_suspend(struct snd_sof_dev *sdev, unsigned int target_state)
352 const struct sof_dsp_power_state target_dsp_state = {
353 .state = target_state,
357 if (!pm_runtime_suspended(sdev->dev))
358 imx8ulp_suspend(sdev);
360 return snd_sof_dsp_set_power_state(sdev, &target_dsp_state);
363 static int imx8ulp_dsp_resume(struct snd_sof_dev *sdev)
365 const struct sof_dsp_power_state target_dsp_state = {
366 .state = SOF_DSP_PM_D0,
370 imx8ulp_resume(sdev);
372 if (pm_runtime_suspended(sdev->dev)) {
373 pm_runtime_disable(sdev->dev);
374 pm_runtime_set_active(sdev->dev);
375 pm_runtime_mark_last_busy(sdev->dev);
376 pm_runtime_enable(sdev->dev);
377 pm_runtime_idle(sdev->dev);
380 return snd_sof_dsp_set_power_state(sdev, &target_dsp_state);
383 static struct snd_soc_dai_driver imx8ulp_dai[] = {
408 static int imx8ulp_dsp_set_power_state(struct snd_sof_dev *sdev,
409 const struct sof_dsp_power_state *target_state)
411 sdev->dsp_power_state = *target_state;
417 static struct snd_sof_dsp_ops sof_imx8ulp_ops = {
418 /* probe and remove */
419 .probe = imx8ulp_probe,
420 .remove = imx8ulp_remove,
423 .reset = imx8ulp_reset,
426 .block_read = sof_block_read,
427 .block_write = sof_block_write,
430 .read64 = sof_io_read64,
433 .mailbox_read = sof_mailbox_read,
434 .mailbox_write = sof_mailbox_write,
437 .send_msg = imx8ulp_send_msg,
438 .get_mailbox_offset = imx8ulp_get_mailbox_offset,
439 .get_window_offset = imx8ulp_get_window_offset,
441 .ipc_msg_data = sof_ipc_msg_data,
442 .set_stream_data_offset = sof_set_stream_data_offset,
444 /* stream callbacks */
445 .pcm_open = sof_stream_pcm_open,
446 .pcm_close = sof_stream_pcm_close,
449 .get_bar_index = imx8ulp_get_bar_index,
450 /* firmware loading */
451 .load_firmware = snd_sof_load_firmware_memcpy,
453 /* Debug information */
454 .dbg_dump = imx8_dump,
457 .dsp_arch_ops = &sof_xtensa_arch_ops,
461 .num_drv = ARRAY_SIZE(imx8ulp_dai),
463 /* ALSA HW info flags */
464 .hw_info = SNDRV_PCM_INFO_MMAP |
465 SNDRV_PCM_INFO_MMAP_VALID |
466 SNDRV_PCM_INFO_INTERLEAVED |
467 SNDRV_PCM_INFO_PAUSE |
468 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP,
471 .runtime_suspend = imx8ulp_dsp_runtime_suspend,
472 .runtime_resume = imx8ulp_dsp_runtime_resume,
474 .suspend = imx8ulp_dsp_suspend,
475 .resume = imx8ulp_dsp_resume,
477 .set_power_state = imx8ulp_dsp_set_power_state,
480 static struct sof_dev_desc sof_of_imx8ulp_desc = {
481 .ipc_supported_mask = BIT(SOF_IPC),
482 .ipc_default = SOF_IPC,
484 [SOF_IPC] = "imx/sof",
486 .default_tplg_path = {
487 [SOF_IPC] = "imx/sof-tplg",
489 .default_fw_filename = {
490 [SOF_IPC] = "sof-imx8ulp.ri",
492 .nocodec_tplg_filename = "sof-imx8ulp-nocodec.tplg",
493 .ops = &sof_imx8ulp_ops,
496 static const struct of_device_id sof_of_imx8ulp_ids[] = {
497 { .compatible = "fsl,imx8ulp-dsp", .data = &sof_of_imx8ulp_desc},
500 MODULE_DEVICE_TABLE(of, sof_of_imx8ulp_ids);
502 /* DT driver definition */
503 static struct platform_driver snd_sof_of_imx8ulp_driver = {
504 .probe = sof_of_probe,
505 .remove = sof_of_remove,
507 .name = "sof-audio-of-imx8ulp",
509 .of_match_table = sof_of_imx8ulp_ids,
512 module_platform_driver(snd_sof_of_imx8ulp_driver);
514 MODULE_IMPORT_NS(SND_SOC_SOF_XTENSA);
515 MODULE_LICENSE("Dual BSD/GPL");