imx8ulp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
  2. //
  3. // Copyright 2021-2022 NXP
  4. //
  5. // Author: Peng Zhang <[email protected]>
  6. //
  7. // Hardware interface for audio DSP on i.MX8ULP
  8. #include <linux/arm-smccc.h>
  9. #include <linux/clk.h>
  10. #include <linux/firmware.h>
  11. #include <linux/firmware/imx/dsp.h>
  12. #include <linux/firmware/imx/ipc.h>
  13. #include <linux/firmware/imx/svc/misc.h>
  14. #include <linux/mfd/syscon.h>
  15. #include <linux/module.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/of_platform.h>
  19. #include <linux/of_reserved_mem.h>
  20. #include <sound/sof.h>
  21. #include <sound/sof/xtensa.h>
  22. #include "../ops.h"
  23. #include "../sof-of-dev.h"
  24. #include "imx-common.h"
  25. #define FSL_SIP_HIFI_XRDC 0xc200000e
  26. /* SIM Domain register */
  27. #define SYSCTRL0 0x8
  28. #define EXECUTE_BIT BIT(13)
  29. #define RESET_BIT BIT(16)
  30. #define HIFI4_CLK_BIT BIT(17)
  31. #define PB_CLK_BIT BIT(18)
  32. #define PLAT_CLK_BIT BIT(19)
  33. #define DEBUG_LOGIC_BIT BIT(25)
  34. #define MBOX_OFFSET 0x800000
  35. #define MBOX_SIZE 0x1000
  36. static struct clk_bulk_data imx8ulp_dsp_clks[] = {
  37. { .id = "core" },
  38. { .id = "ipg" },
  39. { .id = "ocram" },
  40. { .id = "mu" },
  41. };
  42. struct imx8ulp_priv {
  43. struct device *dev;
  44. struct snd_sof_dev *sdev;
  45. /* DSP IPC handler */
  46. struct imx_dsp_ipc *dsp_ipc;
  47. struct platform_device *ipc_dev;
  48. struct regmap *regmap;
  49. struct imx_clocks *clks;
  50. };
  51. static void imx8ulp_sim_lpav_start(struct imx8ulp_priv *priv)
  52. {
  53. /* Controls the HiFi4 DSP Reset: 1 in reset, 0 out of reset */
  54. regmap_update_bits(priv->regmap, SYSCTRL0, RESET_BIT, 0);
  55. /* Reset HiFi4 DSP Debug logic: 1 debug reset, 0 out of reset*/
  56. regmap_update_bits(priv->regmap, SYSCTRL0, DEBUG_LOGIC_BIT, 0);
  57. /* Stall HIFI4 DSP Execution: 1 stall, 0 run */
  58. regmap_update_bits(priv->regmap, SYSCTRL0, EXECUTE_BIT, 0);
  59. }
  60. static int imx8ulp_get_mailbox_offset(struct snd_sof_dev *sdev)
  61. {
  62. return MBOX_OFFSET;
  63. }
  64. static int imx8ulp_get_window_offset(struct snd_sof_dev *sdev, u32 id)
  65. {
  66. return MBOX_OFFSET;
  67. }
  68. static void imx8ulp_dsp_handle_reply(struct imx_dsp_ipc *ipc)
  69. {
  70. struct imx8ulp_priv *priv = imx_dsp_get_data(ipc);
  71. unsigned long flags;
  72. spin_lock_irqsave(&priv->sdev->ipc_lock, flags);
  73. snd_sof_ipc_process_reply(priv->sdev, 0);
  74. spin_unlock_irqrestore(&priv->sdev->ipc_lock, flags);
  75. }
  76. static void imx8ulp_dsp_handle_request(struct imx_dsp_ipc *ipc)
  77. {
  78. struct imx8ulp_priv *priv = imx_dsp_get_data(ipc);
  79. u32 p; /* panic code */
  80. /* Read the message from the debug box. */
  81. sof_mailbox_read(priv->sdev, priv->sdev->debug_box.offset + 4, &p, sizeof(p));
  82. /* Check to see if the message is a panic code (0x0dead***) */
  83. if ((p & SOF_IPC_PANIC_MAGIC_MASK) == SOF_IPC_PANIC_MAGIC)
  84. snd_sof_dsp_panic(priv->sdev, p, true);
  85. else
  86. snd_sof_ipc_msgs_rx(priv->sdev);
  87. }
  88. static struct imx_dsp_ops dsp_ops = {
  89. .handle_reply = imx8ulp_dsp_handle_reply,
  90. .handle_request = imx8ulp_dsp_handle_request,
  91. };
  92. static int imx8ulp_send_msg(struct snd_sof_dev *sdev, struct snd_sof_ipc_msg *msg)
  93. {
  94. struct imx8ulp_priv *priv = sdev->pdata->hw_pdata;
  95. sof_mailbox_write(sdev, sdev->host_box.offset, msg->msg_data,
  96. msg->msg_size);
  97. imx_dsp_ring_doorbell(priv->dsp_ipc, 0);
  98. return 0;
  99. }
  100. static int imx8ulp_run(struct snd_sof_dev *sdev)
  101. {
  102. struct imx8ulp_priv *priv = sdev->pdata->hw_pdata;
  103. imx8ulp_sim_lpav_start(priv);
  104. return 0;
  105. }
  106. static int imx8ulp_reset(struct snd_sof_dev *sdev)
  107. {
  108. struct imx8ulp_priv *priv = sdev->pdata->hw_pdata;
  109. struct arm_smccc_res smc_resource;
  110. /* HiFi4 Platform Clock Enable: 1 enabled, 0 disabled */
  111. regmap_update_bits(priv->regmap, SYSCTRL0, PLAT_CLK_BIT, PLAT_CLK_BIT);
  112. /* HiFi4 PBCLK clock enable: 1 enabled, 0 disabled */
  113. regmap_update_bits(priv->regmap, SYSCTRL0, PB_CLK_BIT, PB_CLK_BIT);
  114. /* HiFi4 Clock Enable: 1 enabled, 0 disabled */
  115. regmap_update_bits(priv->regmap, SYSCTRL0, HIFI4_CLK_BIT, HIFI4_CLK_BIT);
  116. regmap_update_bits(priv->regmap, SYSCTRL0, RESET_BIT, RESET_BIT);
  117. usleep_range(1, 2);
  118. /* Stall HIFI4 DSP Execution: 1 stall, 0 not stall */
  119. regmap_update_bits(priv->regmap, SYSCTRL0, EXECUTE_BIT, EXECUTE_BIT);
  120. usleep_range(1, 2);
  121. arm_smccc_smc(FSL_SIP_HIFI_XRDC, 0, 0, 0, 0, 0, 0, 0, &smc_resource);
  122. return 0;
  123. }
  124. static int imx8ulp_probe(struct snd_sof_dev *sdev)
  125. {
  126. struct platform_device *pdev =
  127. container_of(sdev->dev, struct platform_device, dev);
  128. struct device_node *np = pdev->dev.of_node;
  129. struct device_node *res_node;
  130. struct resource *mmio;
  131. struct imx8ulp_priv *priv;
  132. struct resource res;
  133. u32 base, size;
  134. int ret = 0;
  135. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  136. if (!priv)
  137. return -ENOMEM;
  138. priv->clks = devm_kzalloc(&pdev->dev, sizeof(*priv->clks), GFP_KERNEL);
  139. if (!priv->clks)
  140. return -ENOMEM;
  141. sdev->num_cores = 1;
  142. sdev->pdata->hw_pdata = priv;
  143. priv->dev = sdev->dev;
  144. priv->sdev = sdev;
  145. /* System integration module(SIM) control dsp configuration */
  146. priv->regmap = syscon_regmap_lookup_by_phandle(np, "fsl,dsp-ctrl");
  147. if (IS_ERR(priv->regmap))
  148. return PTR_ERR(priv->regmap);
  149. priv->ipc_dev = platform_device_register_data(sdev->dev, "imx-dsp",
  150. PLATFORM_DEVID_NONE,
  151. pdev, sizeof(*pdev));
  152. if (IS_ERR(priv->ipc_dev))
  153. return PTR_ERR(priv->ipc_dev);
  154. priv->dsp_ipc = dev_get_drvdata(&priv->ipc_dev->dev);
  155. if (!priv->dsp_ipc) {
  156. /* DSP IPC driver not probed yet, try later */
  157. ret = -EPROBE_DEFER;
  158. dev_err(sdev->dev, "Failed to get drvdata\n");
  159. goto exit_pdev_unregister;
  160. }
  161. imx_dsp_set_data(priv->dsp_ipc, priv);
  162. priv->dsp_ipc->ops = &dsp_ops;
  163. /* DSP base */
  164. mmio = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  165. if (mmio) {
  166. base = mmio->start;
  167. size = resource_size(mmio);
  168. } else {
  169. dev_err(sdev->dev, "error: failed to get DSP base at idx 0\n");
  170. ret = -EINVAL;
  171. goto exit_pdev_unregister;
  172. }
  173. sdev->bar[SOF_FW_BLK_TYPE_IRAM] = devm_ioremap(sdev->dev, base, size);
  174. if (!sdev->bar[SOF_FW_BLK_TYPE_IRAM]) {
  175. dev_err(sdev->dev, "failed to ioremap base 0x%x size 0x%x\n",
  176. base, size);
  177. ret = -ENODEV;
  178. goto exit_pdev_unregister;
  179. }
  180. sdev->mmio_bar = SOF_FW_BLK_TYPE_IRAM;
  181. res_node = of_parse_phandle(np, "memory-reserved", 0);
  182. if (!res_node) {
  183. dev_err(&pdev->dev, "failed to get memory region node\n");
  184. ret = -ENODEV;
  185. goto exit_pdev_unregister;
  186. }
  187. ret = of_address_to_resource(res_node, 0, &res);
  188. of_node_put(res_node);
  189. if (ret) {
  190. dev_err(&pdev->dev, "failed to get reserved region address\n");
  191. goto exit_pdev_unregister;
  192. }
  193. sdev->bar[SOF_FW_BLK_TYPE_SRAM] = devm_ioremap_wc(sdev->dev, res.start,
  194. resource_size(&res));
  195. if (!sdev->bar[SOF_FW_BLK_TYPE_SRAM]) {
  196. dev_err(sdev->dev, "failed to ioremap mem 0x%x size 0x%x\n",
  197. base, size);
  198. ret = -ENOMEM;
  199. goto exit_pdev_unregister;
  200. }
  201. sdev->mailbox_bar = SOF_FW_BLK_TYPE_SRAM;
  202. /* set default mailbox offset for FW ready message */
  203. sdev->dsp_box.offset = MBOX_OFFSET;
  204. ret = of_reserved_mem_device_init(sdev->dev);
  205. if (ret) {
  206. dev_err(&pdev->dev, "failed to init reserved memory region %d\n", ret);
  207. goto exit_pdev_unregister;
  208. }
  209. priv->clks->dsp_clks = imx8ulp_dsp_clks;
  210. priv->clks->num_dsp_clks = ARRAY_SIZE(imx8ulp_dsp_clks);
  211. ret = imx8_parse_clocks(sdev, priv->clks);
  212. if (ret < 0)
  213. goto exit_pdev_unregister;
  214. ret = imx8_enable_clocks(sdev, priv->clks);
  215. if (ret < 0)
  216. goto exit_pdev_unregister;
  217. return 0;
  218. exit_pdev_unregister:
  219. platform_device_unregister(priv->ipc_dev);
  220. return ret;
  221. }
  222. static int imx8ulp_remove(struct snd_sof_dev *sdev)
  223. {
  224. struct imx8ulp_priv *priv = sdev->pdata->hw_pdata;
  225. imx8_disable_clocks(sdev, priv->clks);
  226. platform_device_unregister(priv->ipc_dev);
  227. return 0;
  228. }
  229. /* on i.MX8 there is 1 to 1 match between type and BAR idx */
  230. static int imx8ulp_get_bar_index(struct snd_sof_dev *sdev, u32 type)
  231. {
  232. return type;
  233. }
  234. static int imx8ulp_suspend(struct snd_sof_dev *sdev)
  235. {
  236. int i;
  237. struct imx8ulp_priv *priv = (struct imx8ulp_priv *)sdev->pdata->hw_pdata;
  238. /*Stall DSP, release in .run() */
  239. regmap_update_bits(priv->regmap, SYSCTRL0, EXECUTE_BIT, EXECUTE_BIT);
  240. for (i = 0; i < DSP_MU_CHAN_NUM; i++)
  241. imx_dsp_free_channel(priv->dsp_ipc, i);
  242. imx8_disable_clocks(sdev, priv->clks);
  243. return 0;
  244. }
  245. static int imx8ulp_resume(struct snd_sof_dev *sdev)
  246. {
  247. struct imx8ulp_priv *priv = (struct imx8ulp_priv *)sdev->pdata->hw_pdata;
  248. int i;
  249. imx8_enable_clocks(sdev, priv->clks);
  250. for (i = 0; i < DSP_MU_CHAN_NUM; i++)
  251. imx_dsp_request_channel(priv->dsp_ipc, i);
  252. return 0;
  253. }
  254. static int imx8ulp_dsp_runtime_resume(struct snd_sof_dev *sdev)
  255. {
  256. const struct sof_dsp_power_state target_dsp_state = {
  257. .state = SOF_DSP_PM_D0,
  258. .substate = 0,
  259. };
  260. imx8ulp_resume(sdev);
  261. return snd_sof_dsp_set_power_state(sdev, &target_dsp_state);
  262. }
  263. static int imx8ulp_dsp_runtime_suspend(struct snd_sof_dev *sdev)
  264. {
  265. const struct sof_dsp_power_state target_dsp_state = {
  266. .state = SOF_DSP_PM_D3,
  267. .substate = 0,
  268. };
  269. imx8ulp_suspend(sdev);
  270. return snd_sof_dsp_set_power_state(sdev, &target_dsp_state);
  271. }
  272. static int imx8ulp_dsp_suspend(struct snd_sof_dev *sdev, unsigned int target_state)
  273. {
  274. const struct sof_dsp_power_state target_dsp_state = {
  275. .state = target_state,
  276. .substate = 0,
  277. };
  278. if (!pm_runtime_suspended(sdev->dev))
  279. imx8ulp_suspend(sdev);
  280. return snd_sof_dsp_set_power_state(sdev, &target_dsp_state);
  281. }
  282. static int imx8ulp_dsp_resume(struct snd_sof_dev *sdev)
  283. {
  284. const struct sof_dsp_power_state target_dsp_state = {
  285. .state = SOF_DSP_PM_D0,
  286. .substate = 0,
  287. };
  288. imx8ulp_resume(sdev);
  289. if (pm_runtime_suspended(sdev->dev)) {
  290. pm_runtime_disable(sdev->dev);
  291. pm_runtime_set_active(sdev->dev);
  292. pm_runtime_mark_last_busy(sdev->dev);
  293. pm_runtime_enable(sdev->dev);
  294. pm_runtime_idle(sdev->dev);
  295. }
  296. return snd_sof_dsp_set_power_state(sdev, &target_dsp_state);
  297. }
  298. static struct snd_soc_dai_driver imx8ulp_dai[] = {
  299. {
  300. .name = "sai5",
  301. .playback = {
  302. .channels_min = 1,
  303. .channels_max = 32,
  304. },
  305. .capture = {
  306. .channels_min = 1,
  307. .channels_max = 32,
  308. },
  309. },
  310. {
  311. .name = "sai6",
  312. .playback = {
  313. .channels_min = 1,
  314. .channels_max = 32,
  315. },
  316. .capture = {
  317. .channels_min = 1,
  318. .channels_max = 32,
  319. },
  320. },
  321. };
  322. static int imx8ulp_dsp_set_power_state(struct snd_sof_dev *sdev,
  323. const struct sof_dsp_power_state *target_state)
  324. {
  325. sdev->dsp_power_state = *target_state;
  326. return 0;
  327. }
  328. /* i.MX8 ops */
  329. static struct snd_sof_dsp_ops sof_imx8ulp_ops = {
  330. /* probe and remove */
  331. .probe = imx8ulp_probe,
  332. .remove = imx8ulp_remove,
  333. /* DSP core boot */
  334. .run = imx8ulp_run,
  335. .reset = imx8ulp_reset,
  336. /* Block IO */
  337. .block_read = sof_block_read,
  338. .block_write = sof_block_write,
  339. /* Module IO */
  340. .read64 = sof_io_read64,
  341. /* Mailbox IO */
  342. .mailbox_read = sof_mailbox_read,
  343. .mailbox_write = sof_mailbox_write,
  344. /* ipc */
  345. .send_msg = imx8ulp_send_msg,
  346. .get_mailbox_offset = imx8ulp_get_mailbox_offset,
  347. .get_window_offset = imx8ulp_get_window_offset,
  348. .ipc_msg_data = sof_ipc_msg_data,
  349. .set_stream_data_offset = sof_set_stream_data_offset,
  350. /* stream callbacks */
  351. .pcm_open = sof_stream_pcm_open,
  352. .pcm_close = sof_stream_pcm_close,
  353. /* module loading */
  354. .get_bar_index = imx8ulp_get_bar_index,
  355. /* firmware loading */
  356. .load_firmware = snd_sof_load_firmware_memcpy,
  357. /* Debug information */
  358. .dbg_dump = imx8_dump,
  359. /* Firmware ops */
  360. .dsp_arch_ops = &sof_xtensa_arch_ops,
  361. /* DAI drivers */
  362. .drv = imx8ulp_dai,
  363. .num_drv = ARRAY_SIZE(imx8ulp_dai),
  364. /* ALSA HW info flags */
  365. .hw_info = SNDRV_PCM_INFO_MMAP |
  366. SNDRV_PCM_INFO_MMAP_VALID |
  367. SNDRV_PCM_INFO_INTERLEAVED |
  368. SNDRV_PCM_INFO_PAUSE |
  369. SNDRV_PCM_INFO_NO_PERIOD_WAKEUP,
  370. /* PM */
  371. .runtime_suspend = imx8ulp_dsp_runtime_suspend,
  372. .runtime_resume = imx8ulp_dsp_runtime_resume,
  373. .suspend = imx8ulp_dsp_suspend,
  374. .resume = imx8ulp_dsp_resume,
  375. .set_power_state = imx8ulp_dsp_set_power_state,
  376. };
  377. static struct sof_dev_desc sof_of_imx8ulp_desc = {
  378. .ipc_supported_mask = BIT(SOF_IPC),
  379. .ipc_default = SOF_IPC,
  380. .default_fw_path = {
  381. [SOF_IPC] = "imx/sof",
  382. },
  383. .default_tplg_path = {
  384. [SOF_IPC] = "imx/sof-tplg",
  385. },
  386. .default_fw_filename = {
  387. [SOF_IPC] = "sof-imx8ulp.ri",
  388. },
  389. .nocodec_tplg_filename = "sof-imx8ulp-nocodec.tplg",
  390. .ops = &sof_imx8ulp_ops,
  391. };
  392. static const struct of_device_id sof_of_imx8ulp_ids[] = {
  393. { .compatible = "fsl,imx8ulp-dsp", .data = &sof_of_imx8ulp_desc},
  394. { }
  395. };
  396. MODULE_DEVICE_TABLE(of, sof_of_imx8ulp_ids);
  397. /* DT driver definition */
  398. static struct platform_driver snd_sof_of_imx8ulp_driver = {
  399. .probe = sof_of_probe,
  400. .remove = sof_of_remove,
  401. .driver = {
  402. .name = "sof-audio-of-imx8ulp",
  403. .pm = &sof_of_pm,
  404. .of_match_table = sof_of_imx8ulp_ids,
  405. },
  406. };
  407. module_platform_driver(snd_sof_of_imx8ulp_driver);
  408. MODULE_IMPORT_NS(SND_SOC_SOF_XTENSA);
  409. MODULE_LICENSE("Dual BSD/GPL");