blob: 0aeb44d0acc7e40ddd5e6db38726e73ccb6ed6fa [file] [log] [blame]
Pierre-Louis Bossarte149ca22020-05-01 09:58:50 -05001// SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
Daniel Baluta202acc52019-08-21 11:47:30 -05002//
3// Copyright 2019 NXP
4//
5// Author: Daniel Baluta <daniel.baluta@nxp.com>
6//
7// Hardware interface for audio DSP on i.MX8
8
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>
14
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>
20
21#include <linux/firmware/imx/svc/misc.h>
22#include <dt-bindings/firmware/imx/rsrc.h>
23#include "../ops.h"
Iulian Olaru18ebffe2020-09-17 13:56:26 +030024#include "imx-common.h"
Pierre-Louis Bossart3e5cdde2021-09-28 10:28:07 +030025#include "imx-ops.h"
Daniel Baluta202acc52019-08-21 11:47:30 -050026
27/* DSP memories */
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)
38
39#define RESET_VECTOR_VADDR 0x596f8000
40
41#define MBOX_OFFSET 0x800000
42#define MBOX_SIZE 0x1000
43
44struct imx8_priv {
45 struct device *dev;
46 struct snd_sof_dev *sdev;
47
48 /* DSP IPC handler */
49 struct imx_dsp_ipc *dsp_ipc;
50 struct platform_device *ipc_dev;
51
52 /* System Controller IPC handler */
53 struct imx_sc_ipc *sc_ipc;
54
55 /* Power domain handling */
56 int num_domains;
57 struct device **pd_dev;
58 struct device_link **link;
59
60};
61
Daniel Baluta202acc52019-08-21 11:47:30 -050062static int imx8_get_mailbox_offset(struct snd_sof_dev *sdev)
63{
64 return MBOX_OFFSET;
65}
66
67static int imx8_get_window_offset(struct snd_sof_dev *sdev, u32 id)
68{
69 return MBOX_OFFSET;
70}
71
YueHaibingb9a48052019-08-23 20:59:39 +080072static void imx8_dsp_handle_reply(struct imx_dsp_ipc *ipc)
Daniel Baluta202acc52019-08-21 11:47:30 -050073{
74 struct imx8_priv *priv = imx_dsp_get_data(ipc);
75 unsigned long flags;
76
77 spin_lock_irqsave(&priv->sdev->ipc_lock, flags);
Peter Ujfalusi18c45f22021-11-16 17:21:35 +020078 snd_sof_ipc_process_reply(priv->sdev, 0);
Daniel Baluta202acc52019-08-21 11:47:30 -050079 spin_unlock_irqrestore(&priv->sdev->ipc_lock, flags);
80}
81
YueHaibingb9a48052019-08-23 20:59:39 +080082static void imx8_dsp_handle_request(struct imx_dsp_ipc *ipc)
Daniel Baluta202acc52019-08-21 11:47:30 -050083{
84 struct imx8_priv *priv = imx_dsp_get_data(ipc);
Iulian Olaru18ebffe2020-09-17 13:56:26 +030085 u32 p; /* panic code */
Daniel Baluta202acc52019-08-21 11:47:30 -050086
Iulian Olaru18ebffe2020-09-17 13:56:26 +030087 /* Read the message from the debug box. */
88 sof_mailbox_read(priv->sdev, priv->sdev->debug_box.offset + 4, &p, sizeof(p));
89
90 /* Check to see if the message is a panic code (0x0dead***) */
91 if ((p & SOF_IPC_PANIC_MAGIC_MASK) == SOF_IPC_PANIC_MAGIC)
92 snd_sof_dsp_panic(priv->sdev, p);
93 else
94 snd_sof_ipc_msgs_rx(priv->sdev);
Daniel Baluta202acc52019-08-21 11:47:30 -050095}
96
Pierre-Louis Bossart35e7c092020-05-15 16:59:57 +030097static struct imx_dsp_ops dsp_ops = {
Daniel Baluta202acc52019-08-21 11:47:30 -050098 .handle_reply = imx8_dsp_handle_reply,
99 .handle_request = imx8_dsp_handle_request,
100};
101
102static int imx8_send_msg(struct snd_sof_dev *sdev, struct snd_sof_ipc_msg *msg)
103{
Iulian Olaru17b3f992020-08-25 16:50:39 -0700104 struct imx8_priv *priv = sdev->pdata->hw_pdata;
Daniel Baluta202acc52019-08-21 11:47:30 -0500105
106 sof_mailbox_write(sdev, sdev->host_box.offset, msg->msg_data,
107 msg->msg_size);
108 imx_dsp_ring_doorbell(priv->dsp_ipc, 0);
109
110 return 0;
111}
112
113/*
114 * DSP control.
115 */
Paul Olaru9da9ace2020-02-10 11:58:14 +0200116static int imx8x_run(struct snd_sof_dev *sdev)
Daniel Baluta202acc52019-08-21 11:47:30 -0500117{
Iulian Olaru17b3f992020-08-25 16:50:39 -0700118 struct imx8_priv *dsp_priv = sdev->pdata->hw_pdata;
Daniel Baluta202acc52019-08-21 11:47:30 -0500119 int ret;
120
121 ret = imx_sc_misc_set_control(dsp_priv->sc_ipc, IMX_SC_R_DSP,
122 IMX_SC_C_OFS_SEL, 1);
123 if (ret < 0) {
124 dev_err(sdev->dev, "Error system address offset source select\n");
125 return ret;
126 }
127
128 ret = imx_sc_misc_set_control(dsp_priv->sc_ipc, IMX_SC_R_DSP,
129 IMX_SC_C_OFS_AUDIO, 0x80);
130 if (ret < 0) {
131 dev_err(sdev->dev, "Error system address offset of AUDIO\n");
132 return ret;
133 }
134
135 ret = imx_sc_misc_set_control(dsp_priv->sc_ipc, IMX_SC_R_DSP,
136 IMX_SC_C_OFS_PERIPH, 0x5A);
137 if (ret < 0) {
138 dev_err(sdev->dev, "Error system address offset of PERIPH %d\n",
139 ret);
140 return ret;
141 }
142
143 ret = imx_sc_misc_set_control(dsp_priv->sc_ipc, IMX_SC_R_DSP,
144 IMX_SC_C_OFS_IRQ, 0x51);
145 if (ret < 0) {
146 dev_err(sdev->dev, "Error system address offset of IRQ\n");
147 return ret;
148 }
149
150 imx_sc_pm_cpu_start(dsp_priv->sc_ipc, IMX_SC_R_DSP, true,
151 RESET_VECTOR_VADDR);
152
153 return 0;
154}
155
Paul Olaruacfa5202020-02-10 11:58:15 +0200156static int imx8_run(struct snd_sof_dev *sdev)
157{
Iulian Olaru17b3f992020-08-25 16:50:39 -0700158 struct imx8_priv *dsp_priv = sdev->pdata->hw_pdata;
Paul Olaruacfa5202020-02-10 11:58:15 +0200159 int ret;
160
161 ret = imx_sc_misc_set_control(dsp_priv->sc_ipc, IMX_SC_R_DSP,
162 IMX_SC_C_OFS_SEL, 0);
163 if (ret < 0) {
164 dev_err(sdev->dev, "Error system address offset source select\n");
165 return ret;
166 }
167
168 imx_sc_pm_cpu_start(dsp_priv->sc_ipc, IMX_SC_R_DSP, true,
169 RESET_VECTOR_VADDR);
170
171 return 0;
172}
173
Daniel Baluta202acc52019-08-21 11:47:30 -0500174static int imx8_probe(struct snd_sof_dev *sdev)
175{
176 struct platform_device *pdev =
177 container_of(sdev->dev, struct platform_device, dev);
178 struct device_node *np = pdev->dev.of_node;
179 struct device_node *res_node;
180 struct resource *mmio;
181 struct imx8_priv *priv;
182 struct resource res;
183 u32 base, size;
184 int ret = 0;
185 int i;
186
187 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
188 if (!priv)
189 return -ENOMEM;
190
Iulian Olaru17b3f992020-08-25 16:50:39 -0700191 sdev->pdata->hw_pdata = priv;
Daniel Baluta202acc52019-08-21 11:47:30 -0500192 priv->dev = sdev->dev;
193 priv->sdev = sdev;
194
195 /* power up device associated power domains */
196 priv->num_domains = of_count_phandle_with_args(np, "power-domains",
197 "#power-domain-cells");
198 if (priv->num_domains < 0) {
199 dev_err(sdev->dev, "no power-domains property in %pOF\n", np);
200 return priv->num_domains;
201 }
202
203 priv->pd_dev = devm_kmalloc_array(&pdev->dev, priv->num_domains,
204 sizeof(*priv->pd_dev), GFP_KERNEL);
Colin Ian King98910e12019-12-04 12:48:16 +0000205 if (!priv->pd_dev)
Daniel Baluta202acc52019-08-21 11:47:30 -0500206 return -ENOMEM;
207
208 priv->link = devm_kmalloc_array(&pdev->dev, priv->num_domains,
209 sizeof(*priv->link), GFP_KERNEL);
210 if (!priv->link)
211 return -ENOMEM;
212
213 for (i = 0; i < priv->num_domains; i++) {
214 priv->pd_dev[i] = dev_pm_domain_attach_by_id(&pdev->dev, i);
215 if (IS_ERR(priv->pd_dev[i])) {
216 ret = PTR_ERR(priv->pd_dev[i]);
217 goto exit_unroll_pm;
218 }
219 priv->link[i] = device_link_add(&pdev->dev, priv->pd_dev[i],
220 DL_FLAG_STATELESS |
221 DL_FLAG_PM_RUNTIME |
222 DL_FLAG_RPM_ACTIVE);
Dan Carpentera325c7b2019-08-26 16:18:55 +0300223 if (!priv->link[i]) {
224 ret = -ENOMEM;
Daniel Baluta202acc52019-08-21 11:47:30 -0500225 dev_pm_domain_detach(priv->pd_dev[i], false);
226 goto exit_unroll_pm;
227 }
228 }
229
230 ret = imx_scu_get_handle(&priv->sc_ipc);
231 if (ret) {
232 dev_err(sdev->dev, "Cannot obtain SCU handle (err = %d)\n",
233 ret);
234 goto exit_unroll_pm;
235 }
236
237 priv->ipc_dev = platform_device_register_data(sdev->dev, "imx-dsp",
238 PLATFORM_DEVID_NONE,
239 pdev, sizeof(*pdev));
240 if (IS_ERR(priv->ipc_dev)) {
241 ret = PTR_ERR(priv->ipc_dev);
242 goto exit_unroll_pm;
243 }
244
245 priv->dsp_ipc = dev_get_drvdata(&priv->ipc_dev->dev);
246 if (!priv->dsp_ipc) {
247 /* DSP IPC driver not probed yet, try later */
248 ret = -EPROBE_DEFER;
249 dev_err(sdev->dev, "Failed to get drvdata\n");
250 goto exit_pdev_unregister;
251 }
252
253 imx_dsp_set_data(priv->dsp_ipc, priv);
254 priv->dsp_ipc->ops = &dsp_ops;
255
256 /* DSP base */
257 mmio = platform_get_resource(pdev, IORESOURCE_MEM, 0);
258 if (mmio) {
259 base = mmio->start;
260 size = resource_size(mmio);
261 } else {
262 dev_err(sdev->dev, "error: failed to get DSP base at idx 0\n");
263 ret = -EINVAL;
264 goto exit_pdev_unregister;
265 }
266
267 sdev->bar[SOF_FW_BLK_TYPE_IRAM] = devm_ioremap(sdev->dev, base, size);
268 if (!sdev->bar[SOF_FW_BLK_TYPE_IRAM]) {
269 dev_err(sdev->dev, "failed to ioremap base 0x%x size 0x%x\n",
270 base, size);
271 ret = -ENODEV;
272 goto exit_pdev_unregister;
273 }
274 sdev->mmio_bar = SOF_FW_BLK_TYPE_IRAM;
275
276 res_node = of_parse_phandle(np, "memory-region", 0);
277 if (!res_node) {
278 dev_err(&pdev->dev, "failed to get memory region node\n");
279 ret = -ENODEV;
280 goto exit_pdev_unregister;
281 }
282
283 ret = of_address_to_resource(res_node, 0, &res);
Yang Yingliang0ba0f442021-06-17 11:27:56 +0800284 of_node_put(res_node);
Daniel Baluta202acc52019-08-21 11:47:30 -0500285 if (ret) {
286 dev_err(&pdev->dev, "failed to get reserved region address\n");
287 goto exit_pdev_unregister;
288 }
289
290 sdev->bar[SOF_FW_BLK_TYPE_SRAM] = devm_ioremap_wc(sdev->dev, res.start,
Julia Lawall49f261e2020-01-01 18:49:46 +0100291 resource_size(&res));
Wei Yongjun393151c2019-08-26 12:00:03 +0000292 if (!sdev->bar[SOF_FW_BLK_TYPE_SRAM]) {
Daniel Baluta202acc52019-08-21 11:47:30 -0500293 dev_err(sdev->dev, "failed to ioremap mem 0x%x size 0x%x\n",
294 base, size);
Wei Yongjun393151c2019-08-26 12:00:03 +0000295 ret = -ENOMEM;
Daniel Baluta202acc52019-08-21 11:47:30 -0500296 goto exit_pdev_unregister;
297 }
298 sdev->mailbox_bar = SOF_FW_BLK_TYPE_SRAM;
299
Daniel Balutadcf08d02019-12-20 11:05:31 -0600300 /* set default mailbox offset for FW ready message */
301 sdev->dsp_box.offset = MBOX_OFFSET;
302
Daniel Baluta202acc52019-08-21 11:47:30 -0500303 return 0;
304
305exit_pdev_unregister:
306 platform_device_unregister(priv->ipc_dev);
307exit_unroll_pm:
308 while (--i >= 0) {
309 device_link_del(priv->link[i]);
310 dev_pm_domain_detach(priv->pd_dev[i], false);
311 }
312
313 return ret;
314}
315
316static int imx8_remove(struct snd_sof_dev *sdev)
317{
Iulian Olaru17b3f992020-08-25 16:50:39 -0700318 struct imx8_priv *priv = sdev->pdata->hw_pdata;
Daniel Baluta202acc52019-08-21 11:47:30 -0500319 int i;
320
321 platform_device_unregister(priv->ipc_dev);
322
323 for (i = 0; i < priv->num_domains; i++) {
324 device_link_del(priv->link[i]);
325 dev_pm_domain_detach(priv->pd_dev[i], false);
326 }
327
328 return 0;
329}
330
331/* on i.MX8 there is 1 to 1 match between type and BAR idx */
YueHaibingb9a48052019-08-23 20:59:39 +0800332static int imx8_get_bar_index(struct snd_sof_dev *sdev, u32 type)
Daniel Baluta202acc52019-08-21 11:47:30 -0500333{
Peter Ujfalusi10d93a92021-09-15 15:21:08 +0300334 /* Only IRAM and SRAM bars are valid */
335 switch (type) {
336 case SOF_FW_BLK_TYPE_IRAM:
337 case SOF_FW_BLK_TYPE_SRAM:
338 return type;
339 default:
340 return -EINVAL;
341 }
Daniel Baluta202acc52019-08-21 11:47:30 -0500342}
343
Daniel Baluta202acc52019-08-21 11:47:30 -0500344static struct snd_soc_dai_driver imx8_dai[] = {
345{
Daniel Balutabcba2c92020-07-20 10:20:42 +0300346 .name = "esai0",
Daniel Baluta4e7f8ca2020-07-07 16:04:39 -0500347 .playback = {
348 .channels_min = 1,
349 .channels_max = 8,
350 },
351 .capture = {
352 .channels_min = 1,
353 .channels_max = 8,
354 },
Daniel Baluta202acc52019-08-21 11:47:30 -0500355},
Daniel Baluta68f56f62020-07-20 10:20:44 +0300356{
357 .name = "sai1",
358 .playback = {
359 .channels_min = 1,
360 .channels_max = 32,
361 },
362 .capture = {
363 .channels_min = 1,
364 .channels_max = 32,
365 },
366},
Daniel Baluta202acc52019-08-21 11:47:30 -0500367};
368
Paul Olaruacfa5202020-02-10 11:58:15 +0200369/* i.MX8 ops */
370struct snd_sof_dsp_ops sof_imx8_ops = {
371 /* probe and remove */
372 .probe = imx8_probe,
373 .remove = imx8_remove,
374 /* DSP core boot */
375 .run = imx8_run,
376
377 /* Block IO */
378 .block_read = sof_block_read,
379 .block_write = sof_block_write,
380
Daniel Balutaf71f59d2021-10-04 18:21:44 +0300381 /* Mailbox IO */
382 .mailbox_read = sof_mailbox_read,
383 .mailbox_write = sof_mailbox_write,
384
Paul Olaruacfa5202020-02-10 11:58:15 +0200385 /* ipc */
386 .send_msg = imx8_send_msg,
387 .fw_ready = sof_fw_ready,
388 .get_mailbox_offset = imx8_get_mailbox_offset,
389 .get_window_offset = imx8_get_window_offset,
390
Daniel Baluta40834192021-10-04 18:21:46 +0300391 .ipc_msg_data = sof_ipc_msg_data,
392 .ipc_pcm_params = sof_ipc_pcm_params,
Paul Olaruacfa5202020-02-10 11:58:15 +0200393
394 /* module loading */
395 .load_module = snd_sof_parse_module_memcpy,
396 .get_bar_index = imx8_get_bar_index,
397 /* firmware loading */
398 .load_firmware = snd_sof_load_firmware_memcpy,
399
Iulian Olaru18ebffe2020-09-17 13:56:26 +0300400 /* Debug information */
401 .dbg_dump = imx8_dump,
Peter Ujfalusiff2f99b2021-09-15 15:21:13 +0300402 .debugfs_add_region_item = snd_sof_debugfs_add_region_item_iomem,
Iulian Olaru18ebffe2020-09-17 13:56:26 +0300403
Daniel Baluta40834192021-10-04 18:21:46 +0300404 /* stream callbacks */
405 .pcm_open = sof_stream_pcm_open,
406 .pcm_close = sof_stream_pcm_close,
407
Iulian Olaru5a1fa002020-08-25 16:50:40 -0700408 /* Firmware ops */
Peter Ujfalusi0ed66cb2021-09-16 16:03:08 +0300409 .dsp_arch_ops = &sof_xtensa_arch_ops,
Iulian Olaru5a1fa002020-08-25 16:50:40 -0700410
Paul Olaruacfa5202020-02-10 11:58:15 +0200411 /* DAI drivers */
412 .drv = imx8_dai,
Daniel Balutabeaa7bd2020-07-20 10:20:41 +0300413 .num_drv = ARRAY_SIZE(imx8_dai),
Daniel Baluta45b72622020-07-20 10:20:40 +0300414
415 /* ALSA HW info flags */
416 .hw_info = SNDRV_PCM_INFO_MMAP |
417 SNDRV_PCM_INFO_MMAP_VALID |
418 SNDRV_PCM_INFO_INTERLEAVED |
419 SNDRV_PCM_INFO_PAUSE |
420 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP,
Paul Olaruacfa5202020-02-10 11:58:15 +0200421};
422EXPORT_SYMBOL(sof_imx8_ops);
423
Paul Olaru9da9ace2020-02-10 11:58:14 +0200424/* i.MX8X ops */
425struct snd_sof_dsp_ops sof_imx8x_ops = {
Daniel Baluta202acc52019-08-21 11:47:30 -0500426 /* probe and remove */
427 .probe = imx8_probe,
428 .remove = imx8_remove,
429 /* DSP core boot */
Paul Olaru9da9ace2020-02-10 11:58:14 +0200430 .run = imx8x_run,
Daniel Baluta202acc52019-08-21 11:47:30 -0500431
432 /* Block IO */
433 .block_read = sof_block_read,
434 .block_write = sof_block_write,
435
Daniel Balutaf71f59d2021-10-04 18:21:44 +0300436 /* Mailbox IO */
437 .mailbox_read = sof_mailbox_read,
438 .mailbox_write = sof_mailbox_write,
439
Daniel Baluta202acc52019-08-21 11:47:30 -0500440 /* ipc */
441 .send_msg = imx8_send_msg,
442 .fw_ready = sof_fw_ready,
443 .get_mailbox_offset = imx8_get_mailbox_offset,
444 .get_window_offset = imx8_get_window_offset,
445
Daniel Baluta40834192021-10-04 18:21:46 +0300446 .ipc_msg_data = sof_ipc_msg_data,
447 .ipc_pcm_params = sof_ipc_pcm_params,
Daniel Baluta202acc52019-08-21 11:47:30 -0500448
449 /* module loading */
450 .load_module = snd_sof_parse_module_memcpy,
451 .get_bar_index = imx8_get_bar_index,
452 /* firmware loading */
453 .load_firmware = snd_sof_load_firmware_memcpy,
454
Iulian Olaru18ebffe2020-09-17 13:56:26 +0300455 /* Debug information */
456 .dbg_dump = imx8_dump,
Peter Ujfalusiff2f99b2021-09-15 15:21:13 +0300457 .debugfs_add_region_item = snd_sof_debugfs_add_region_item_iomem,
Iulian Olaru18ebffe2020-09-17 13:56:26 +0300458
Daniel Baluta40834192021-10-04 18:21:46 +0300459 /* stream callbacks */
460 .pcm_open = sof_stream_pcm_open,
461 .pcm_close = sof_stream_pcm_close,
462
Iulian Olaru5a1fa002020-08-25 16:50:40 -0700463 /* Firmware ops */
Peter Ujfalusi0ed66cb2021-09-16 16:03:08 +0300464 .dsp_arch_ops = &sof_xtensa_arch_ops,
Iulian Olaru5a1fa002020-08-25 16:50:40 -0700465
Daniel Baluta202acc52019-08-21 11:47:30 -0500466 /* DAI drivers */
467 .drv = imx8_dai,
Daniel Balutabeaa7bd2020-07-20 10:20:41 +0300468 .num_drv = ARRAY_SIZE(imx8_dai),
Pierre-Louis Bossart27e322f2019-10-24 16:03:17 -0500469
470 /* ALSA HW info flags */
471 .hw_info = SNDRV_PCM_INFO_MMAP |
472 SNDRV_PCM_INFO_MMAP_VALID |
473 SNDRV_PCM_INFO_INTERLEAVED |
474 SNDRV_PCM_INFO_PAUSE |
475 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP
Daniel Baluta202acc52019-08-21 11:47:30 -0500476};
Paul Olaru9da9ace2020-02-10 11:58:14 +0200477EXPORT_SYMBOL(sof_imx8x_ops);
Daniel Baluta202acc52019-08-21 11:47:30 -0500478
Iulian Olaru5a1fa002020-08-25 16:50:40 -0700479MODULE_IMPORT_NS(SND_SOC_SOF_XTENSA);
Daniel Baluta202acc52019-08-21 11:47:30 -0500480MODULE_LICENSE("Dual BSD/GPL");