drm/nouveau: fix kernel-doc comments
[drm/drm-misc.git] / drivers / remoteproc / mtk_scp.c
blob0f4a7065d0bd9e7f00931b1207499420cdc37f8f
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Copyright (c) 2019 MediaTek Inc.
5 #include <asm/barrier.h>
6 #include <linux/clk.h>
7 #include <linux/dma-mapping.h>
8 #include <linux/err.h>
9 #include <linux/interrupt.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/of_address.h>
13 #include <linux/of_platform.h>
14 #include <linux/of_reserved_mem.h>
15 #include <linux/platform_device.h>
16 #include <linux/remoteproc.h>
17 #include <linux/remoteproc/mtk_scp.h>
18 #include <linux/rpmsg/mtk_rpmsg.h>
20 #include "mtk_common.h"
21 #include "remoteproc_internal.h"
23 #define SECTION_NAME_IPI_BUFFER ".ipi_buffer"
25 /**
26 * scp_get() - get a reference to SCP.
28 * @pdev: the platform device of the module requesting SCP platform
29 * device for using SCP API.
31 * Return: Return NULL if failed. otherwise reference to SCP.
32 **/
33 struct mtk_scp *scp_get(struct platform_device *pdev)
35 struct device *dev = &pdev->dev;
36 struct device_node *scp_node;
37 struct platform_device *scp_pdev;
39 scp_node = of_parse_phandle(dev->of_node, "mediatek,scp", 0);
40 if (!scp_node) {
41 dev_err(dev, "can't get SCP node\n");
42 return NULL;
45 scp_pdev = of_find_device_by_node(scp_node);
46 of_node_put(scp_node);
48 if (WARN_ON(!scp_pdev)) {
49 dev_err(dev, "SCP pdev failed\n");
50 return NULL;
53 return platform_get_drvdata(scp_pdev);
55 EXPORT_SYMBOL_GPL(scp_get);
57 /**
58 * scp_put() - "free" the SCP
60 * @scp: mtk_scp structure from scp_get().
61 **/
62 void scp_put(struct mtk_scp *scp)
64 put_device(scp->dev);
66 EXPORT_SYMBOL_GPL(scp_put);
68 static void scp_wdt_handler(struct mtk_scp *scp, u32 scp_to_host)
70 struct mtk_scp_of_cluster *scp_cluster = scp->cluster;
71 struct mtk_scp *scp_node;
73 dev_err(scp->dev, "SCP watchdog timeout! 0x%x", scp_to_host);
75 /* report watchdog timeout to all cores */
76 list_for_each_entry(scp_node, &scp_cluster->mtk_scp_list, elem)
77 rproc_report_crash(scp_node->rproc, RPROC_WATCHDOG);
80 static void scp_init_ipi_handler(void *data, unsigned int len, void *priv)
82 struct mtk_scp *scp = priv;
83 struct scp_run *run = data;
85 scp->run.signaled = run->signaled;
86 strscpy(scp->run.fw_ver, run->fw_ver, SCP_FW_VER_LEN);
87 scp->run.dec_capability = run->dec_capability;
88 scp->run.enc_capability = run->enc_capability;
89 wake_up_interruptible(&scp->run.wq);
92 static void scp_ipi_handler(struct mtk_scp *scp)
94 struct mtk_share_obj __iomem *rcv_obj = scp->recv_buf;
95 struct scp_ipi_desc *ipi_desc = scp->ipi_desc;
96 scp_ipi_handler_t handler;
97 u32 id = readl(&rcv_obj->id);
98 u32 len = readl(&rcv_obj->len);
99 const struct mtk_scp_sizes_data *scp_sizes;
101 scp_sizes = scp->data->scp_sizes;
102 if (len > scp_sizes->ipi_share_buffer_size) {
103 dev_err(scp->dev, "ipi message too long (len %d, max %zd)", len,
104 scp_sizes->ipi_share_buffer_size);
105 return;
107 if (id >= SCP_IPI_MAX) {
108 dev_err(scp->dev, "No such ipi id = %d\n", id);
109 return;
112 scp_ipi_lock(scp, id);
113 handler = ipi_desc[id].handler;
114 if (!handler) {
115 dev_err(scp->dev, "No handler for ipi id = %d\n", id);
116 scp_ipi_unlock(scp, id);
117 return;
120 memcpy_fromio(scp->share_buf, &rcv_obj->share_buf, len);
121 memset(&scp->share_buf[len], 0, scp_sizes->ipi_share_buffer_size - len);
122 handler(scp->share_buf, len, ipi_desc[id].priv);
123 scp_ipi_unlock(scp, id);
125 scp->ipi_id_ack[id] = true;
126 wake_up(&scp->ack_wq);
129 static int scp_elf_read_ipi_buf_addr(struct mtk_scp *scp,
130 const struct firmware *fw,
131 size_t *offset);
133 static int scp_ipi_init(struct mtk_scp *scp, const struct firmware *fw)
135 int ret;
136 size_t buf_sz, offset;
137 size_t share_buf_offset;
138 const struct mtk_scp_sizes_data *scp_sizes;
140 /* read the ipi buf addr from FW itself first */
141 ret = scp_elf_read_ipi_buf_addr(scp, fw, &offset);
142 if (ret) {
143 /* use default ipi buf addr if the FW doesn't have it */
144 offset = scp->data->ipi_buf_offset;
145 if (!offset)
146 return ret;
148 dev_info(scp->dev, "IPI buf addr %#010zx\n", offset);
150 /* Make sure IPI buffer fits in the L2TCM range assigned to this core */
151 buf_sz = sizeof(*scp->recv_buf) + sizeof(*scp->send_buf);
153 if (scp->sram_size < buf_sz + offset) {
154 dev_err(scp->dev, "IPI buffer does not fit in SRAM.\n");
155 return -EOVERFLOW;
158 scp_sizes = scp->data->scp_sizes;
159 scp->recv_buf = (struct mtk_share_obj __iomem *)
160 (scp->sram_base + offset);
161 share_buf_offset = sizeof(scp->recv_buf->id)
162 + sizeof(scp->recv_buf->len) + scp_sizes->ipi_share_buffer_size;
163 scp->send_buf = (struct mtk_share_obj __iomem *)
164 (scp->sram_base + offset + share_buf_offset);
165 memset_io(scp->recv_buf, 0, share_buf_offset);
166 memset_io(scp->send_buf, 0, share_buf_offset);
168 return 0;
171 static void mt8183_scp_reset_assert(struct mtk_scp *scp)
173 u32 val;
175 val = readl(scp->cluster->reg_base + MT8183_SW_RSTN);
176 val &= ~MT8183_SW_RSTN_BIT;
177 writel(val, scp->cluster->reg_base + MT8183_SW_RSTN);
180 static void mt8183_scp_reset_deassert(struct mtk_scp *scp)
182 u32 val;
184 val = readl(scp->cluster->reg_base + MT8183_SW_RSTN);
185 val |= MT8183_SW_RSTN_BIT;
186 writel(val, scp->cluster->reg_base + MT8183_SW_RSTN);
189 static void mt8192_scp_reset_assert(struct mtk_scp *scp)
191 writel(1, scp->cluster->reg_base + MT8192_CORE0_SW_RSTN_SET);
194 static void mt8192_scp_reset_deassert(struct mtk_scp *scp)
196 writel(1, scp->cluster->reg_base + MT8192_CORE0_SW_RSTN_CLR);
199 static void mt8195_scp_c1_reset_assert(struct mtk_scp *scp)
201 writel(1, scp->cluster->reg_base + MT8195_CORE1_SW_RSTN_SET);
204 static void mt8195_scp_c1_reset_deassert(struct mtk_scp *scp)
206 writel(1, scp->cluster->reg_base + MT8195_CORE1_SW_RSTN_CLR);
209 static void mt8183_scp_irq_handler(struct mtk_scp *scp)
211 u32 scp_to_host;
213 scp_to_host = readl(scp->cluster->reg_base + MT8183_SCP_TO_HOST);
214 if (scp_to_host & MT8183_SCP_IPC_INT_BIT)
215 scp_ipi_handler(scp);
216 else
217 scp_wdt_handler(scp, scp_to_host);
219 /* SCP won't send another interrupt until we set SCP_TO_HOST to 0. */
220 writel(MT8183_SCP_IPC_INT_BIT | MT8183_SCP_WDT_INT_BIT,
221 scp->cluster->reg_base + MT8183_SCP_TO_HOST);
224 static void mt8192_scp_irq_handler(struct mtk_scp *scp)
226 u32 scp_to_host;
228 scp_to_host = readl(scp->cluster->reg_base + MT8192_SCP2APMCU_IPC_SET);
230 if (scp_to_host & MT8192_SCP_IPC_INT_BIT) {
231 scp_ipi_handler(scp);
234 * SCP won't send another interrupt until we clear
235 * MT8192_SCP2APMCU_IPC.
237 writel(MT8192_SCP_IPC_INT_BIT,
238 scp->cluster->reg_base + MT8192_SCP2APMCU_IPC_CLR);
239 } else {
240 scp_wdt_handler(scp, scp_to_host);
241 writel(1, scp->cluster->reg_base + MT8192_CORE0_WDT_IRQ);
245 static void mt8195_scp_irq_handler(struct mtk_scp *scp)
247 u32 scp_to_host;
249 scp_to_host = readl(scp->cluster->reg_base + MT8192_SCP2APMCU_IPC_SET);
251 if (scp_to_host & MT8192_SCP_IPC_INT_BIT) {
252 scp_ipi_handler(scp);
253 } else {
254 u32 reason = readl(scp->cluster->reg_base + MT8195_SYS_STATUS);
256 if (reason & MT8195_CORE0_WDT)
257 writel(1, scp->cluster->reg_base + MT8192_CORE0_WDT_IRQ);
259 if (reason & MT8195_CORE1_WDT)
260 writel(1, scp->cluster->reg_base + MT8195_CORE1_WDT_IRQ);
262 scp_wdt_handler(scp, reason);
265 writel(scp_to_host, scp->cluster->reg_base + MT8192_SCP2APMCU_IPC_CLR);
268 static void mt8195_scp_c1_irq_handler(struct mtk_scp *scp)
270 u32 scp_to_host;
272 scp_to_host = readl(scp->cluster->reg_base + MT8195_SSHUB2APMCU_IPC_SET);
274 if (scp_to_host & MT8192_SCP_IPC_INT_BIT)
275 scp_ipi_handler(scp);
277 writel(scp_to_host, scp->cluster->reg_base + MT8195_SSHUB2APMCU_IPC_CLR);
280 static irqreturn_t scp_irq_handler(int irq, void *priv)
282 struct mtk_scp *scp = priv;
283 int ret;
285 ret = clk_prepare_enable(scp->clk);
286 if (ret) {
287 dev_err(scp->dev, "failed to enable clocks\n");
288 return IRQ_NONE;
291 scp->data->scp_irq_handler(scp);
293 clk_disable_unprepare(scp->clk);
295 return IRQ_HANDLED;
298 static int scp_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
300 struct device *dev = &rproc->dev;
301 struct elf32_hdr *ehdr;
302 struct elf32_phdr *phdr;
303 int i, ret = 0;
304 const u8 *elf_data = fw->data;
306 ehdr = (struct elf32_hdr *)elf_data;
307 phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
309 /* go through the available ELF segments */
310 for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
311 u32 da = phdr->p_paddr;
312 u32 memsz = phdr->p_memsz;
313 u32 filesz = phdr->p_filesz;
314 u32 offset = phdr->p_offset;
315 void __iomem *ptr;
317 dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
318 phdr->p_type, da, memsz, filesz);
320 if (phdr->p_type != PT_LOAD)
321 continue;
322 if (!filesz)
323 continue;
325 if (filesz > memsz) {
326 dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
327 filesz, memsz);
328 ret = -EINVAL;
329 break;
332 if (offset + filesz > fw->size) {
333 dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
334 offset + filesz, fw->size);
335 ret = -EINVAL;
336 break;
339 /* grab the kernel address for this device address */
340 ptr = (void __iomem *)rproc_da_to_va(rproc, da, memsz, NULL);
341 if (!ptr) {
342 dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
343 ret = -EINVAL;
344 break;
347 /* put the segment where the remote processor expects it */
348 scp_memcpy_aligned(ptr, elf_data + phdr->p_offset, filesz);
351 return ret;
354 static int scp_elf_read_ipi_buf_addr(struct mtk_scp *scp,
355 const struct firmware *fw,
356 size_t *offset)
358 struct elf32_hdr *ehdr;
359 struct elf32_shdr *shdr, *shdr_strtab;
360 int i;
361 const u8 *elf_data = fw->data;
362 const char *strtab;
364 ehdr = (struct elf32_hdr *)elf_data;
365 shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
366 shdr_strtab = shdr + ehdr->e_shstrndx;
367 strtab = (const char *)(elf_data + shdr_strtab->sh_offset);
369 for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
370 if (strcmp(strtab + shdr->sh_name,
371 SECTION_NAME_IPI_BUFFER) == 0) {
372 *offset = shdr->sh_addr;
373 return 0;
377 return -ENOENT;
380 static int mt8183_scp_clk_get(struct mtk_scp *scp)
382 struct device *dev = scp->dev;
383 int ret = 0;
385 scp->clk = devm_clk_get(dev, "main");
386 if (IS_ERR(scp->clk)) {
387 dev_err(dev, "Failed to get clock\n");
388 ret = PTR_ERR(scp->clk);
391 return ret;
394 static int mt8192_scp_clk_get(struct mtk_scp *scp)
396 return mt8183_scp_clk_get(scp);
399 static int mt8195_scp_clk_get(struct mtk_scp *scp)
401 scp->clk = NULL;
403 return 0;
406 static int mt8183_scp_before_load(struct mtk_scp *scp)
408 /* Clear SCP to host interrupt */
409 writel(MT8183_SCP_IPC_INT_BIT, scp->cluster->reg_base + MT8183_SCP_TO_HOST);
411 /* Reset clocks before loading FW */
412 writel(0x0, scp->cluster->reg_base + MT8183_SCP_CLK_SW_SEL);
413 writel(0x0, scp->cluster->reg_base + MT8183_SCP_CLK_DIV_SEL);
415 /* Initialize TCM before loading FW. */
416 writel(0x0, scp->cluster->reg_base + MT8183_SCP_L1_SRAM_PD);
417 writel(0x0, scp->cluster->reg_base + MT8183_SCP_TCM_TAIL_SRAM_PD);
419 /* Turn on the power of SCP's SRAM before using it. */
420 writel(0x0, scp->cluster->reg_base + MT8183_SCP_SRAM_PDN);
423 * Set I-cache and D-cache size before loading SCP FW.
424 * SCP SRAM logical address may change when cache size setting differs.
426 writel(MT8183_SCP_CACHE_CON_WAYEN | MT8183_SCP_CACHESIZE_8KB,
427 scp->cluster->reg_base + MT8183_SCP_CACHE_CON);
428 writel(MT8183_SCP_CACHESIZE_8KB, scp->cluster->reg_base + MT8183_SCP_DCACHE_CON);
430 return 0;
433 static void scp_sram_power_on(void __iomem *addr, u32 reserved_mask)
435 int i;
437 for (i = 31; i >= 0; i--)
438 writel(GENMASK(i, 0) & ~reserved_mask, addr);
439 writel(0, addr);
442 static void scp_sram_power_off(void __iomem *addr, u32 reserved_mask)
444 int i;
446 writel(0, addr);
447 for (i = 0; i < 32; i++)
448 writel(GENMASK(i, 0) & ~reserved_mask, addr);
451 static int mt8186_scp_before_load(struct mtk_scp *scp)
453 /* Clear SCP to host interrupt */
454 writel(MT8183_SCP_IPC_INT_BIT, scp->cluster->reg_base + MT8183_SCP_TO_HOST);
456 /* Reset clocks before loading FW */
457 writel(0x0, scp->cluster->reg_base + MT8183_SCP_CLK_SW_SEL);
458 writel(0x0, scp->cluster->reg_base + MT8183_SCP_CLK_DIV_SEL);
460 /* Turn on the power of SCP's SRAM before using it. Enable 1 block per time*/
461 scp_sram_power_on(scp->cluster->reg_base + MT8183_SCP_SRAM_PDN, 0);
463 /* Initialize TCM before loading FW. */
464 writel(0x0, scp->cluster->reg_base + MT8183_SCP_L1_SRAM_PD);
465 writel(0x0, scp->cluster->reg_base + MT8183_SCP_TCM_TAIL_SRAM_PD);
466 writel(0x0, scp->cluster->reg_base + MT8186_SCP_L1_SRAM_PD_P1);
467 writel(0x0, scp->cluster->reg_base + MT8186_SCP_L1_SRAM_PD_p2);
470 * Set I-cache and D-cache size before loading SCP FW.
471 * SCP SRAM logical address may change when cache size setting differs.
473 writel(MT8183_SCP_CACHE_CON_WAYEN | MT8183_SCP_CACHESIZE_8KB,
474 scp->cluster->reg_base + MT8183_SCP_CACHE_CON);
475 writel(MT8183_SCP_CACHESIZE_8KB, scp->cluster->reg_base + MT8183_SCP_DCACHE_CON);
477 return 0;
480 static int mt8188_scp_l2tcm_on(struct mtk_scp *scp)
482 struct mtk_scp_of_cluster *scp_cluster = scp->cluster;
484 mutex_lock(&scp_cluster->cluster_lock);
486 if (scp_cluster->l2tcm_refcnt == 0) {
487 /* clear SPM interrupt, SCP2SPM_IPC_CLR */
488 writel(0xff, scp->cluster->reg_base + MT8192_SCP2SPM_IPC_CLR);
490 /* Power on L2TCM */
491 scp_sram_power_on(scp->cluster->reg_base + MT8192_L2TCM_SRAM_PD_0, 0);
492 scp_sram_power_on(scp->cluster->reg_base + MT8192_L2TCM_SRAM_PD_1, 0);
493 scp_sram_power_on(scp->cluster->reg_base + MT8192_L2TCM_SRAM_PD_2, 0);
494 scp_sram_power_on(scp->cluster->reg_base + MT8192_L1TCM_SRAM_PDN, 0);
497 scp_cluster->l2tcm_refcnt += 1;
499 mutex_unlock(&scp_cluster->cluster_lock);
501 return 0;
504 static int mt8188_scp_before_load(struct mtk_scp *scp)
506 writel(1, scp->cluster->reg_base + MT8192_CORE0_SW_RSTN_SET);
508 mt8188_scp_l2tcm_on(scp);
510 scp_sram_power_on(scp->cluster->reg_base + MT8192_CPU0_SRAM_PD, 0);
512 /* enable MPU for all memory regions */
513 writel(0xff, scp->cluster->reg_base + MT8192_CORE0_MEM_ATT_PREDEF);
515 return 0;
518 static int mt8188_scp_c1_before_load(struct mtk_scp *scp)
520 u32 sec_ctrl;
521 struct mtk_scp *scp_c0;
522 struct mtk_scp_of_cluster *scp_cluster = scp->cluster;
524 scp->data->scp_reset_assert(scp);
526 mt8188_scp_l2tcm_on(scp);
528 scp_sram_power_on(scp->cluster->reg_base + MT8195_CPU1_SRAM_PD, 0);
530 /* enable MPU for all memory regions */
531 writel(0xff, scp->cluster->reg_base + MT8195_CORE1_MEM_ATT_PREDEF);
534 * The L2TCM_OFFSET_RANGE and L2TCM_OFFSET shift the destination address
535 * on SRAM when SCP core 1 accesses SRAM.
537 * This configuration solves booting the SCP core 0 and core 1 from
538 * different SRAM address because core 0 and core 1 both boot from
539 * the head of SRAM by default. this must be configured before boot SCP core 1.
541 * The value of L2TCM_OFFSET_RANGE is from the viewpoint of SCP core 1.
542 * When SCP core 1 issues address within the range (L2TCM_OFFSET_RANGE),
543 * the address will be added with a fixed offset (L2TCM_OFFSET) on the bus.
544 * The shift action is tranparent to software.
546 writel(0, scp->cluster->reg_base + MT8195_L2TCM_OFFSET_RANGE_0_LOW);
547 writel(scp->sram_size, scp->cluster->reg_base + MT8195_L2TCM_OFFSET_RANGE_0_HIGH);
549 scp_c0 = list_first_entry(&scp_cluster->mtk_scp_list, struct mtk_scp, elem);
550 writel(scp->sram_phys - scp_c0->sram_phys, scp->cluster->reg_base + MT8195_L2TCM_OFFSET);
552 /* enable SRAM offset when fetching instruction and data */
553 sec_ctrl = readl(scp->cluster->reg_base + MT8195_SEC_CTRL);
554 sec_ctrl |= MT8195_CORE_OFFSET_ENABLE_I | MT8195_CORE_OFFSET_ENABLE_D;
555 writel(sec_ctrl, scp->cluster->reg_base + MT8195_SEC_CTRL);
557 return 0;
560 static int mt8192_scp_before_load(struct mtk_scp *scp)
562 /* clear SPM interrupt, SCP2SPM_IPC_CLR */
563 writel(0xff, scp->cluster->reg_base + MT8192_SCP2SPM_IPC_CLR);
565 writel(1, scp->cluster->reg_base + MT8192_CORE0_SW_RSTN_SET);
567 /* enable SRAM clock */
568 scp_sram_power_on(scp->cluster->reg_base + MT8192_L2TCM_SRAM_PD_0, 0);
569 scp_sram_power_on(scp->cluster->reg_base + MT8192_L2TCM_SRAM_PD_1, 0);
570 scp_sram_power_on(scp->cluster->reg_base + MT8192_L2TCM_SRAM_PD_2, 0);
571 scp_sram_power_on(scp->cluster->reg_base + MT8192_L1TCM_SRAM_PDN, 0);
572 scp_sram_power_on(scp->cluster->reg_base + MT8192_CPU0_SRAM_PD, 0);
574 /* enable MPU for all memory regions */
575 writel(0xff, scp->cluster->reg_base + MT8192_CORE0_MEM_ATT_PREDEF);
577 return 0;
580 static int mt8195_scp_l2tcm_on(struct mtk_scp *scp)
582 struct mtk_scp_of_cluster *scp_cluster = scp->cluster;
584 mutex_lock(&scp_cluster->cluster_lock);
586 if (scp_cluster->l2tcm_refcnt == 0) {
587 /* clear SPM interrupt, SCP2SPM_IPC_CLR */
588 writel(0xff, scp->cluster->reg_base + MT8192_SCP2SPM_IPC_CLR);
590 /* Power on L2TCM */
591 scp_sram_power_on(scp->cluster->reg_base + MT8192_L2TCM_SRAM_PD_0, 0);
592 scp_sram_power_on(scp->cluster->reg_base + MT8192_L2TCM_SRAM_PD_1, 0);
593 scp_sram_power_on(scp->cluster->reg_base + MT8192_L2TCM_SRAM_PD_2, 0);
594 scp_sram_power_on(scp->cluster->reg_base + MT8192_L1TCM_SRAM_PDN,
595 MT8195_L1TCM_SRAM_PDN_RESERVED_RSI_BITS);
598 scp_cluster->l2tcm_refcnt += 1;
600 mutex_unlock(&scp_cluster->cluster_lock);
602 return 0;
605 static int mt8195_scp_before_load(struct mtk_scp *scp)
607 writel(1, scp->cluster->reg_base + MT8192_CORE0_SW_RSTN_SET);
609 mt8195_scp_l2tcm_on(scp);
611 scp_sram_power_on(scp->cluster->reg_base + MT8192_CPU0_SRAM_PD, 0);
613 /* enable MPU for all memory regions */
614 writel(0xff, scp->cluster->reg_base + MT8192_CORE0_MEM_ATT_PREDEF);
616 return 0;
619 static int mt8195_scp_c1_before_load(struct mtk_scp *scp)
621 u32 sec_ctrl;
622 struct mtk_scp *scp_c0;
623 struct mtk_scp_of_cluster *scp_cluster = scp->cluster;
625 scp->data->scp_reset_assert(scp);
627 mt8195_scp_l2tcm_on(scp);
629 scp_sram_power_on(scp->cluster->reg_base + MT8195_CPU1_SRAM_PD, 0);
631 /* enable MPU for all memory regions */
632 writel(0xff, scp->cluster->reg_base + MT8195_CORE1_MEM_ATT_PREDEF);
635 * The L2TCM_OFFSET_RANGE and L2TCM_OFFSET shift the destination address
636 * on SRAM when SCP core 1 accesses SRAM.
638 * This configuration solves booting the SCP core 0 and core 1 from
639 * different SRAM address because core 0 and core 1 both boot from
640 * the head of SRAM by default. this must be configured before boot SCP core 1.
642 * The value of L2TCM_OFFSET_RANGE is from the viewpoint of SCP core 1.
643 * When SCP core 1 issues address within the range (L2TCM_OFFSET_RANGE),
644 * the address will be added with a fixed offset (L2TCM_OFFSET) on the bus.
645 * The shift action is tranparent to software.
647 writel(0, scp->cluster->reg_base + MT8195_L2TCM_OFFSET_RANGE_0_LOW);
648 writel(scp->sram_size, scp->cluster->reg_base + MT8195_L2TCM_OFFSET_RANGE_0_HIGH);
650 scp_c0 = list_first_entry(&scp_cluster->mtk_scp_list, struct mtk_scp, elem);
651 writel(scp->sram_phys - scp_c0->sram_phys, scp->cluster->reg_base + MT8195_L2TCM_OFFSET);
653 /* enable SRAM offset when fetching instruction and data */
654 sec_ctrl = readl(scp->cluster->reg_base + MT8195_SEC_CTRL);
655 sec_ctrl |= MT8195_CORE_OFFSET_ENABLE_I | MT8195_CORE_OFFSET_ENABLE_D;
656 writel(sec_ctrl, scp->cluster->reg_base + MT8195_SEC_CTRL);
658 return 0;
661 static int scp_load(struct rproc *rproc, const struct firmware *fw)
663 struct mtk_scp *scp = rproc->priv;
664 struct device *dev = scp->dev;
665 int ret;
667 ret = clk_prepare_enable(scp->clk);
668 if (ret) {
669 dev_err(dev, "failed to enable clocks\n");
670 return ret;
673 /* Hold SCP in reset while loading FW. */
674 scp->data->scp_reset_assert(scp);
676 ret = scp->data->scp_before_load(scp);
677 if (ret < 0)
678 goto leave;
680 ret = scp_elf_load_segments(rproc, fw);
681 leave:
682 clk_disable_unprepare(scp->clk);
684 return ret;
687 static int scp_parse_fw(struct rproc *rproc, const struct firmware *fw)
689 struct mtk_scp *scp = rproc->priv;
690 struct device *dev = scp->dev;
691 int ret;
693 ret = clk_prepare_enable(scp->clk);
694 if (ret) {
695 dev_err(dev, "failed to enable clocks\n");
696 return ret;
699 ret = scp_ipi_init(scp, fw);
700 clk_disable_unprepare(scp->clk);
701 return ret;
704 static int scp_start(struct rproc *rproc)
706 struct mtk_scp *scp = rproc->priv;
707 struct device *dev = scp->dev;
708 struct scp_run *run = &scp->run;
709 int ret;
711 ret = clk_prepare_enable(scp->clk);
712 if (ret) {
713 dev_err(dev, "failed to enable clocks\n");
714 return ret;
717 run->signaled = false;
719 scp->data->scp_reset_deassert(scp);
721 ret = wait_event_interruptible_timeout(
722 run->wq,
723 run->signaled,
724 msecs_to_jiffies(2000));
726 if (ret == 0) {
727 dev_err(dev, "wait SCP initialization timeout!\n");
728 ret = -ETIME;
729 goto stop;
731 if (ret == -ERESTARTSYS) {
732 dev_err(dev, "wait SCP interrupted by a signal!\n");
733 goto stop;
736 clk_disable_unprepare(scp->clk);
737 dev_info(dev, "SCP is ready. FW version %s\n", run->fw_ver);
739 return 0;
741 stop:
742 scp->data->scp_reset_assert(scp);
743 clk_disable_unprepare(scp->clk);
744 return ret;
747 static void *mt8183_scp_da_to_va(struct mtk_scp *scp, u64 da, size_t len)
749 int offset;
750 const struct mtk_scp_sizes_data *scp_sizes;
752 scp_sizes = scp->data->scp_sizes;
753 if (da < scp->sram_size) {
754 offset = da;
755 if (offset >= 0 && (offset + len) <= scp->sram_size)
756 return (void __force *)scp->sram_base + offset;
757 } else if (scp_sizes->max_dram_size) {
758 offset = da - scp->dma_addr;
759 if (offset >= 0 && (offset + len) <= scp_sizes->max_dram_size)
760 return scp->cpu_addr + offset;
763 return NULL;
766 static void *mt8192_scp_da_to_va(struct mtk_scp *scp, u64 da, size_t len)
768 int offset;
769 const struct mtk_scp_sizes_data *scp_sizes;
771 scp_sizes = scp->data->scp_sizes;
772 if (da >= scp->sram_phys &&
773 (da + len) <= scp->sram_phys + scp->sram_size) {
774 offset = da - scp->sram_phys;
775 return (void __force *)scp->sram_base + offset;
778 /* optional memory region */
779 if (scp->cluster->l1tcm_size &&
780 da >= scp->cluster->l1tcm_phys &&
781 (da + len) <= scp->cluster->l1tcm_phys + scp->cluster->l1tcm_size) {
782 offset = da - scp->cluster->l1tcm_phys;
783 return (void __force *)scp->cluster->l1tcm_base + offset;
786 /* optional memory region */
787 if (scp_sizes->max_dram_size &&
788 da >= scp->dma_addr &&
789 (da + len) <= scp->dma_addr + scp_sizes->max_dram_size) {
790 offset = da - scp->dma_addr;
791 return scp->cpu_addr + offset;
794 return NULL;
797 static void *scp_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_iomem)
799 struct mtk_scp *scp = rproc->priv;
801 return scp->data->scp_da_to_va(scp, da, len);
804 static void mt8183_scp_stop(struct mtk_scp *scp)
806 /* Disable SCP watchdog */
807 writel(0, scp->cluster->reg_base + MT8183_WDT_CFG);
810 static void mt8188_scp_l2tcm_off(struct mtk_scp *scp)
812 struct mtk_scp_of_cluster *scp_cluster = scp->cluster;
814 mutex_lock(&scp_cluster->cluster_lock);
816 if (scp_cluster->l2tcm_refcnt > 0)
817 scp_cluster->l2tcm_refcnt -= 1;
819 if (scp_cluster->l2tcm_refcnt == 0) {
820 /* Power off L2TCM */
821 scp_sram_power_off(scp->cluster->reg_base + MT8192_L2TCM_SRAM_PD_0, 0);
822 scp_sram_power_off(scp->cluster->reg_base + MT8192_L2TCM_SRAM_PD_1, 0);
823 scp_sram_power_off(scp->cluster->reg_base + MT8192_L2TCM_SRAM_PD_2, 0);
824 scp_sram_power_off(scp->cluster->reg_base + MT8192_L1TCM_SRAM_PDN, 0);
827 mutex_unlock(&scp_cluster->cluster_lock);
830 static void mt8188_scp_stop(struct mtk_scp *scp)
832 mt8188_scp_l2tcm_off(scp);
834 scp_sram_power_off(scp->cluster->reg_base + MT8192_CPU0_SRAM_PD, 0);
836 /* Disable SCP watchdog */
837 writel(0, scp->cluster->reg_base + MT8192_CORE0_WDT_CFG);
840 static void mt8188_scp_c1_stop(struct mtk_scp *scp)
842 mt8188_scp_l2tcm_off(scp);
844 /* Power off CPU SRAM */
845 scp_sram_power_off(scp->cluster->reg_base + MT8195_CPU1_SRAM_PD, 0);
847 /* Disable SCP watchdog */
848 writel(0, scp->cluster->reg_base + MT8195_CORE1_WDT_CFG);
851 static void mt8192_scp_stop(struct mtk_scp *scp)
853 /* Disable SRAM clock */
854 scp_sram_power_off(scp->cluster->reg_base + MT8192_L2TCM_SRAM_PD_0, 0);
855 scp_sram_power_off(scp->cluster->reg_base + MT8192_L2TCM_SRAM_PD_1, 0);
856 scp_sram_power_off(scp->cluster->reg_base + MT8192_L2TCM_SRAM_PD_2, 0);
857 scp_sram_power_off(scp->cluster->reg_base + MT8192_L1TCM_SRAM_PDN, 0);
858 scp_sram_power_off(scp->cluster->reg_base + MT8192_CPU0_SRAM_PD, 0);
860 /* Disable SCP watchdog */
861 writel(0, scp->cluster->reg_base + MT8192_CORE0_WDT_CFG);
864 static void mt8195_scp_l2tcm_off(struct mtk_scp *scp)
866 struct mtk_scp_of_cluster *scp_cluster = scp->cluster;
868 mutex_lock(&scp_cluster->cluster_lock);
870 if (scp_cluster->l2tcm_refcnt > 0)
871 scp_cluster->l2tcm_refcnt -= 1;
873 if (scp_cluster->l2tcm_refcnt == 0) {
874 /* Power off L2TCM */
875 scp_sram_power_off(scp->cluster->reg_base + MT8192_L2TCM_SRAM_PD_0, 0);
876 scp_sram_power_off(scp->cluster->reg_base + MT8192_L2TCM_SRAM_PD_1, 0);
877 scp_sram_power_off(scp->cluster->reg_base + MT8192_L2TCM_SRAM_PD_2, 0);
878 scp_sram_power_off(scp->cluster->reg_base + MT8192_L1TCM_SRAM_PDN,
879 MT8195_L1TCM_SRAM_PDN_RESERVED_RSI_BITS);
882 mutex_unlock(&scp_cluster->cluster_lock);
885 static void mt8195_scp_stop(struct mtk_scp *scp)
887 mt8195_scp_l2tcm_off(scp);
889 scp_sram_power_off(scp->cluster->reg_base + MT8192_CPU0_SRAM_PD, 0);
891 /* Disable SCP watchdog */
892 writel(0, scp->cluster->reg_base + MT8192_CORE0_WDT_CFG);
895 static void mt8195_scp_c1_stop(struct mtk_scp *scp)
897 mt8195_scp_l2tcm_off(scp);
899 /* Power off CPU SRAM */
900 scp_sram_power_off(scp->cluster->reg_base + MT8195_CPU1_SRAM_PD, 0);
902 /* Disable SCP watchdog */
903 writel(0, scp->cluster->reg_base + MT8195_CORE1_WDT_CFG);
906 static int scp_stop(struct rproc *rproc)
908 struct mtk_scp *scp = rproc->priv;
909 int ret;
911 ret = clk_prepare_enable(scp->clk);
912 if (ret) {
913 dev_err(scp->dev, "failed to enable clocks\n");
914 return ret;
917 scp->data->scp_reset_assert(scp);
918 scp->data->scp_stop(scp);
919 clk_disable_unprepare(scp->clk);
921 return 0;
924 static const struct rproc_ops scp_ops = {
925 .start = scp_start,
926 .stop = scp_stop,
927 .load = scp_load,
928 .da_to_va = scp_da_to_va,
929 .parse_fw = scp_parse_fw,
930 .sanity_check = rproc_elf_sanity_check,
934 * scp_get_device() - get device struct of SCP
936 * @scp: mtk_scp structure
938 struct device *scp_get_device(struct mtk_scp *scp)
940 return scp->dev;
942 EXPORT_SYMBOL_GPL(scp_get_device);
945 * scp_get_rproc() - get rproc struct of SCP
947 * @scp: mtk_scp structure
949 struct rproc *scp_get_rproc(struct mtk_scp *scp)
951 return scp->rproc;
953 EXPORT_SYMBOL_GPL(scp_get_rproc);
956 * scp_get_vdec_hw_capa() - get video decoder hardware capability
958 * @scp: mtk_scp structure
960 * Return: video decoder hardware capability
962 unsigned int scp_get_vdec_hw_capa(struct mtk_scp *scp)
964 return scp->run.dec_capability;
966 EXPORT_SYMBOL_GPL(scp_get_vdec_hw_capa);
969 * scp_get_venc_hw_capa() - get video encoder hardware capability
971 * @scp: mtk_scp structure
973 * Return: video encoder hardware capability
975 unsigned int scp_get_venc_hw_capa(struct mtk_scp *scp)
977 return scp->run.enc_capability;
979 EXPORT_SYMBOL_GPL(scp_get_venc_hw_capa);
982 * scp_mapping_dm_addr() - Mapping SRAM/DRAM to kernel virtual address
984 * @scp: mtk_scp structure
985 * @mem_addr: SCP views memory address
987 * Mapping the SCP's SRAM address /
988 * DMEM (Data Extended Memory) memory address /
989 * Working buffer memory address to
990 * kernel virtual address.
992 * Return: Return ERR_PTR(-EINVAL) if mapping failed,
993 * otherwise the mapped kernel virtual address
995 void *scp_mapping_dm_addr(struct mtk_scp *scp, u32 mem_addr)
997 void *ptr;
999 ptr = scp_da_to_va(scp->rproc, mem_addr, 0, NULL);
1000 if (!ptr)
1001 return ERR_PTR(-EINVAL);
1003 return ptr;
1005 EXPORT_SYMBOL_GPL(scp_mapping_dm_addr);
1007 static int scp_map_memory_region(struct mtk_scp *scp)
1009 int ret;
1010 const struct mtk_scp_sizes_data *scp_sizes;
1012 ret = of_reserved_mem_device_init(scp->dev);
1014 /* reserved memory is optional. */
1015 if (ret == -ENODEV) {
1016 dev_info(scp->dev, "skipping reserved memory initialization.");
1017 return 0;
1020 if (ret) {
1021 dev_err(scp->dev, "failed to assign memory-region: %d\n", ret);
1022 return -ENOMEM;
1025 /* Reserved SCP code size */
1026 scp_sizes = scp->data->scp_sizes;
1027 scp->cpu_addr = dma_alloc_coherent(scp->dev, scp_sizes->max_dram_size,
1028 &scp->dma_addr, GFP_KERNEL);
1029 if (!scp->cpu_addr)
1030 return -ENOMEM;
1032 return 0;
1035 static void scp_unmap_memory_region(struct mtk_scp *scp)
1037 const struct mtk_scp_sizes_data *scp_sizes;
1039 scp_sizes = scp->data->scp_sizes;
1040 if (scp_sizes->max_dram_size == 0)
1041 return;
1043 dma_free_coherent(scp->dev, scp_sizes->max_dram_size, scp->cpu_addr,
1044 scp->dma_addr);
1045 of_reserved_mem_device_release(scp->dev);
1048 static int scp_register_ipi(struct platform_device *pdev, u32 id,
1049 ipi_handler_t handler, void *priv)
1051 struct mtk_scp *scp = platform_get_drvdata(pdev);
1053 return scp_ipi_register(scp, id, handler, priv);
1056 static void scp_unregister_ipi(struct platform_device *pdev, u32 id)
1058 struct mtk_scp *scp = platform_get_drvdata(pdev);
1060 scp_ipi_unregister(scp, id);
1063 static int scp_send_ipi(struct platform_device *pdev, u32 id, void *buf,
1064 unsigned int len, unsigned int wait)
1066 struct mtk_scp *scp = platform_get_drvdata(pdev);
1068 return scp_ipi_send(scp, id, buf, len, wait);
1071 static struct mtk_rpmsg_info mtk_scp_rpmsg_info = {
1072 .send_ipi = scp_send_ipi,
1073 .register_ipi = scp_register_ipi,
1074 .unregister_ipi = scp_unregister_ipi,
1075 .ns_ipi_id = SCP_IPI_NS_SERVICE,
1078 static void scp_add_rpmsg_subdev(struct mtk_scp *scp)
1080 scp->rpmsg_subdev =
1081 mtk_rpmsg_create_rproc_subdev(to_platform_device(scp->dev),
1082 &mtk_scp_rpmsg_info);
1083 if (scp->rpmsg_subdev)
1084 rproc_add_subdev(scp->rproc, scp->rpmsg_subdev);
1087 static void scp_remove_rpmsg_subdev(struct mtk_scp *scp)
1089 if (scp->rpmsg_subdev) {
1090 rproc_remove_subdev(scp->rproc, scp->rpmsg_subdev);
1091 mtk_rpmsg_destroy_rproc_subdev(scp->rpmsg_subdev);
1092 scp->rpmsg_subdev = NULL;
1096 static struct mtk_scp *scp_rproc_init(struct platform_device *pdev,
1097 struct mtk_scp_of_cluster *scp_cluster,
1098 const struct mtk_scp_of_data *of_data)
1100 struct device *dev = &pdev->dev;
1101 struct device_node *np = dev->of_node;
1102 struct mtk_scp *scp;
1103 struct rproc *rproc;
1104 struct resource *res;
1105 const char *fw_name = "scp.img";
1106 int ret, i;
1107 const struct mtk_scp_sizes_data *scp_sizes;
1109 ret = rproc_of_parse_firmware(dev, 0, &fw_name);
1110 if (ret < 0 && ret != -EINVAL)
1111 return ERR_PTR(ret);
1113 rproc = devm_rproc_alloc(dev, np->name, &scp_ops, fw_name, sizeof(*scp));
1114 if (!rproc) {
1115 dev_err(dev, "unable to allocate remoteproc\n");
1116 return ERR_PTR(-ENOMEM);
1119 scp = rproc->priv;
1120 scp->rproc = rproc;
1121 scp->dev = dev;
1122 scp->data = of_data;
1123 scp->cluster = scp_cluster;
1124 platform_set_drvdata(pdev, scp);
1126 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sram");
1127 scp->sram_base = devm_ioremap_resource(dev, res);
1128 if (IS_ERR(scp->sram_base)) {
1129 dev_err(dev, "Failed to parse and map sram memory\n");
1130 return ERR_CAST(scp->sram_base);
1133 scp->sram_size = resource_size(res);
1134 scp->sram_phys = res->start;
1136 ret = scp->data->scp_clk_get(scp);
1137 if (ret)
1138 return ERR_PTR(ret);
1140 ret = scp_map_memory_region(scp);
1141 if (ret)
1142 return ERR_PTR(ret);
1144 mutex_init(&scp->send_lock);
1145 for (i = 0; i < SCP_IPI_MAX; i++)
1146 mutex_init(&scp->ipi_desc[i].lock);
1148 /* register SCP initialization IPI */
1149 ret = scp_ipi_register(scp, SCP_IPI_INIT, scp_init_ipi_handler, scp);
1150 if (ret) {
1151 dev_err(dev, "Failed to register IPI_SCP_INIT\n");
1152 goto release_dev_mem;
1155 scp_sizes = scp->data->scp_sizes;
1156 scp->share_buf = kzalloc(scp_sizes->ipi_share_buffer_size, GFP_KERNEL);
1157 if (!scp->share_buf) {
1158 dev_err(dev, "Failed to allocate IPI share buffer\n");
1159 ret = -ENOMEM;
1160 goto release_dev_mem;
1163 init_waitqueue_head(&scp->run.wq);
1164 init_waitqueue_head(&scp->ack_wq);
1166 scp_add_rpmsg_subdev(scp);
1168 ret = devm_request_threaded_irq(dev, platform_get_irq(pdev, 0), NULL,
1169 scp_irq_handler, IRQF_ONESHOT,
1170 pdev->name, scp);
1172 if (ret) {
1173 dev_err(dev, "failed to request irq\n");
1174 goto remove_subdev;
1177 return scp;
1179 remove_subdev:
1180 scp_remove_rpmsg_subdev(scp);
1181 scp_ipi_unregister(scp, SCP_IPI_INIT);
1182 kfree(scp->share_buf);
1183 scp->share_buf = NULL;
1184 release_dev_mem:
1185 scp_unmap_memory_region(scp);
1186 for (i = 0; i < SCP_IPI_MAX; i++)
1187 mutex_destroy(&scp->ipi_desc[i].lock);
1188 mutex_destroy(&scp->send_lock);
1190 return ERR_PTR(ret);
1193 static void scp_free(struct mtk_scp *scp)
1195 int i;
1197 scp_remove_rpmsg_subdev(scp);
1198 scp_ipi_unregister(scp, SCP_IPI_INIT);
1199 kfree(scp->share_buf);
1200 scp->share_buf = NULL;
1201 scp_unmap_memory_region(scp);
1202 for (i = 0; i < SCP_IPI_MAX; i++)
1203 mutex_destroy(&scp->ipi_desc[i].lock);
1204 mutex_destroy(&scp->send_lock);
1207 static int scp_add_single_core(struct platform_device *pdev,
1208 struct mtk_scp_of_cluster *scp_cluster)
1210 struct device *dev = &pdev->dev;
1211 struct list_head *scp_list = &scp_cluster->mtk_scp_list;
1212 struct mtk_scp *scp;
1213 int ret;
1215 scp = scp_rproc_init(pdev, scp_cluster, of_device_get_match_data(dev));
1216 if (IS_ERR(scp))
1217 return PTR_ERR(scp);
1219 ret = rproc_add(scp->rproc);
1220 if (ret) {
1221 dev_err(dev, "Failed to add rproc\n");
1222 scp_free(scp);
1223 return ret;
1226 list_add_tail(&scp->elem, scp_list);
1228 return 0;
1231 static int scp_add_multi_core(struct platform_device *pdev,
1232 struct mtk_scp_of_cluster *scp_cluster)
1234 struct device *dev = &pdev->dev;
1235 struct device_node *np = dev_of_node(dev);
1236 struct platform_device *cpdev;
1237 struct device_node *child;
1238 struct list_head *scp_list = &scp_cluster->mtk_scp_list;
1239 const struct mtk_scp_of_data **cluster_of_data;
1240 struct mtk_scp *scp, *temp;
1241 int core_id = 0;
1242 int ret;
1244 cluster_of_data = (const struct mtk_scp_of_data **)of_device_get_match_data(dev);
1246 for_each_available_child_of_node(np, child) {
1247 if (!cluster_of_data[core_id]) {
1248 ret = -EINVAL;
1249 dev_err(dev, "Not support core %d\n", core_id);
1250 of_node_put(child);
1251 goto init_fail;
1254 cpdev = of_find_device_by_node(child);
1255 if (!cpdev) {
1256 ret = -ENODEV;
1257 dev_err(dev, "Not found platform device for core %d\n", core_id);
1258 of_node_put(child);
1259 goto init_fail;
1262 scp = scp_rproc_init(cpdev, scp_cluster, cluster_of_data[core_id]);
1263 put_device(&cpdev->dev);
1264 if (IS_ERR(scp)) {
1265 ret = PTR_ERR(scp);
1266 dev_err(dev, "Failed to initialize core %d rproc\n", core_id);
1267 of_node_put(child);
1268 goto init_fail;
1271 ret = rproc_add(scp->rproc);
1272 if (ret) {
1273 dev_err(dev, "Failed to add rproc of core %d\n", core_id);
1274 of_node_put(child);
1275 scp_free(scp);
1276 goto init_fail;
1279 list_add_tail(&scp->elem, scp_list);
1280 core_id++;
1284 * Here we are setting the platform device for @pdev to the last @scp that was
1285 * created, which is needed because (1) scp_rproc_init() is calling
1286 * platform_set_drvdata() on the child platform devices and (2) we need a handle to
1287 * the cluster list in scp_remove().
1289 platform_set_drvdata(pdev, scp);
1291 return 0;
1293 init_fail:
1294 list_for_each_entry_safe_reverse(scp, temp, scp_list, elem) {
1295 list_del(&scp->elem);
1296 rproc_del(scp->rproc);
1297 scp_free(scp);
1300 return ret;
1303 static bool scp_is_single_core(struct platform_device *pdev)
1305 struct device *dev = &pdev->dev;
1306 struct device_node *np = dev_of_node(dev);
1307 struct device_node *child;
1308 int num_cores = 0;
1310 for_each_child_of_node(np, child)
1311 if (of_device_is_compatible(child, "mediatek,scp-core"))
1312 num_cores++;
1314 return num_cores < 2;
1317 static int scp_cluster_init(struct platform_device *pdev, struct mtk_scp_of_cluster *scp_cluster)
1319 int ret;
1321 if (scp_is_single_core(pdev))
1322 ret = scp_add_single_core(pdev, scp_cluster);
1323 else
1324 ret = scp_add_multi_core(pdev, scp_cluster);
1326 return ret;
1329 static int scp_probe(struct platform_device *pdev)
1331 struct device *dev = &pdev->dev;
1332 struct mtk_scp_of_cluster *scp_cluster;
1333 struct resource *res;
1334 int ret;
1336 scp_cluster = devm_kzalloc(dev, sizeof(*scp_cluster), GFP_KERNEL);
1337 if (!scp_cluster)
1338 return -ENOMEM;
1340 scp_cluster->reg_base = devm_platform_ioremap_resource_byname(pdev, "cfg");
1341 if (IS_ERR(scp_cluster->reg_base))
1342 return dev_err_probe(dev, PTR_ERR(scp_cluster->reg_base),
1343 "Failed to parse and map cfg memory\n");
1345 /* l1tcm is an optional memory region */
1346 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "l1tcm");
1347 if (res) {
1348 scp_cluster->l1tcm_base = devm_ioremap_resource(dev, res);
1349 if (IS_ERR(scp_cluster->l1tcm_base))
1350 return dev_err_probe(dev, PTR_ERR(scp_cluster->l1tcm_base),
1351 "Failed to map l1tcm memory\n");
1353 scp_cluster->l1tcm_size = resource_size(res);
1354 scp_cluster->l1tcm_phys = res->start;
1357 INIT_LIST_HEAD(&scp_cluster->mtk_scp_list);
1358 mutex_init(&scp_cluster->cluster_lock);
1360 ret = devm_of_platform_populate(dev);
1361 if (ret)
1362 return dev_err_probe(dev, ret, "Failed to populate platform devices\n");
1364 ret = scp_cluster_init(pdev, scp_cluster);
1365 if (ret)
1366 return ret;
1368 return 0;
1371 static void scp_remove(struct platform_device *pdev)
1373 struct mtk_scp *scp = platform_get_drvdata(pdev);
1374 struct mtk_scp_of_cluster *scp_cluster = scp->cluster;
1375 struct mtk_scp *temp;
1377 list_for_each_entry_safe_reverse(scp, temp, &scp_cluster->mtk_scp_list, elem) {
1378 list_del(&scp->elem);
1379 rproc_del(scp->rproc);
1380 scp_free(scp);
1382 mutex_destroy(&scp_cluster->cluster_lock);
1385 static const struct mtk_scp_sizes_data default_scp_sizes = {
1386 .max_dram_size = 0x500000,
1387 .ipi_share_buffer_size = 288,
1390 static const struct mtk_scp_sizes_data mt8188_scp_sizes = {
1391 .max_dram_size = 0x800000,
1392 .ipi_share_buffer_size = 600,
1395 static const struct mtk_scp_sizes_data mt8188_scp_c1_sizes = {
1396 .max_dram_size = 0xA00000,
1397 .ipi_share_buffer_size = 600,
1400 static const struct mtk_scp_sizes_data mt8195_scp_sizes = {
1401 .max_dram_size = 0x800000,
1402 .ipi_share_buffer_size = 288,
1405 static const struct mtk_scp_of_data mt8183_of_data = {
1406 .scp_clk_get = mt8183_scp_clk_get,
1407 .scp_before_load = mt8183_scp_before_load,
1408 .scp_irq_handler = mt8183_scp_irq_handler,
1409 .scp_reset_assert = mt8183_scp_reset_assert,
1410 .scp_reset_deassert = mt8183_scp_reset_deassert,
1411 .scp_stop = mt8183_scp_stop,
1412 .scp_da_to_va = mt8183_scp_da_to_va,
1413 .host_to_scp_reg = MT8183_HOST_TO_SCP,
1414 .host_to_scp_int_bit = MT8183_HOST_IPC_INT_BIT,
1415 .ipi_buf_offset = 0x7bdb0,
1416 .scp_sizes = &default_scp_sizes,
1419 static const struct mtk_scp_of_data mt8186_of_data = {
1420 .scp_clk_get = mt8195_scp_clk_get,
1421 .scp_before_load = mt8186_scp_before_load,
1422 .scp_irq_handler = mt8183_scp_irq_handler,
1423 .scp_reset_assert = mt8183_scp_reset_assert,
1424 .scp_reset_deassert = mt8183_scp_reset_deassert,
1425 .scp_stop = mt8183_scp_stop,
1426 .scp_da_to_va = mt8183_scp_da_to_va,
1427 .host_to_scp_reg = MT8183_HOST_TO_SCP,
1428 .host_to_scp_int_bit = MT8183_HOST_IPC_INT_BIT,
1429 .ipi_buf_offset = 0x3bdb0,
1430 .scp_sizes = &default_scp_sizes,
1433 static const struct mtk_scp_of_data mt8188_of_data = {
1434 .scp_clk_get = mt8195_scp_clk_get,
1435 .scp_before_load = mt8188_scp_before_load,
1436 .scp_irq_handler = mt8195_scp_irq_handler,
1437 .scp_reset_assert = mt8192_scp_reset_assert,
1438 .scp_reset_deassert = mt8192_scp_reset_deassert,
1439 .scp_stop = mt8188_scp_stop,
1440 .scp_da_to_va = mt8192_scp_da_to_va,
1441 .host_to_scp_reg = MT8192_GIPC_IN_SET,
1442 .host_to_scp_int_bit = MT8192_HOST_IPC_INT_BIT,
1443 .scp_sizes = &mt8188_scp_sizes,
1446 static const struct mtk_scp_of_data mt8188_of_data_c1 = {
1447 .scp_clk_get = mt8195_scp_clk_get,
1448 .scp_before_load = mt8188_scp_c1_before_load,
1449 .scp_irq_handler = mt8195_scp_c1_irq_handler,
1450 .scp_reset_assert = mt8195_scp_c1_reset_assert,
1451 .scp_reset_deassert = mt8195_scp_c1_reset_deassert,
1452 .scp_stop = mt8188_scp_c1_stop,
1453 .scp_da_to_va = mt8192_scp_da_to_va,
1454 .host_to_scp_reg = MT8192_GIPC_IN_SET,
1455 .host_to_scp_int_bit = MT8195_CORE1_HOST_IPC_INT_BIT,
1456 .scp_sizes = &mt8188_scp_c1_sizes,
1459 static const struct mtk_scp_of_data mt8192_of_data = {
1460 .scp_clk_get = mt8192_scp_clk_get,
1461 .scp_before_load = mt8192_scp_before_load,
1462 .scp_irq_handler = mt8192_scp_irq_handler,
1463 .scp_reset_assert = mt8192_scp_reset_assert,
1464 .scp_reset_deassert = mt8192_scp_reset_deassert,
1465 .scp_stop = mt8192_scp_stop,
1466 .scp_da_to_va = mt8192_scp_da_to_va,
1467 .host_to_scp_reg = MT8192_GIPC_IN_SET,
1468 .host_to_scp_int_bit = MT8192_HOST_IPC_INT_BIT,
1469 .scp_sizes = &default_scp_sizes,
1472 static const struct mtk_scp_of_data mt8195_of_data = {
1473 .scp_clk_get = mt8195_scp_clk_get,
1474 .scp_before_load = mt8195_scp_before_load,
1475 .scp_irq_handler = mt8195_scp_irq_handler,
1476 .scp_reset_assert = mt8192_scp_reset_assert,
1477 .scp_reset_deassert = mt8192_scp_reset_deassert,
1478 .scp_stop = mt8195_scp_stop,
1479 .scp_da_to_va = mt8192_scp_da_to_va,
1480 .host_to_scp_reg = MT8192_GIPC_IN_SET,
1481 .host_to_scp_int_bit = MT8192_HOST_IPC_INT_BIT,
1482 .scp_sizes = &mt8195_scp_sizes,
1485 static const struct mtk_scp_of_data mt8195_of_data_c1 = {
1486 .scp_clk_get = mt8195_scp_clk_get,
1487 .scp_before_load = mt8195_scp_c1_before_load,
1488 .scp_irq_handler = mt8195_scp_c1_irq_handler,
1489 .scp_reset_assert = mt8195_scp_c1_reset_assert,
1490 .scp_reset_deassert = mt8195_scp_c1_reset_deassert,
1491 .scp_stop = mt8195_scp_c1_stop,
1492 .scp_da_to_va = mt8192_scp_da_to_va,
1493 .host_to_scp_reg = MT8192_GIPC_IN_SET,
1494 .host_to_scp_int_bit = MT8195_CORE1_HOST_IPC_INT_BIT,
1495 .scp_sizes = &default_scp_sizes,
1498 static const struct mtk_scp_of_data *mt8188_of_data_cores[] = {
1499 &mt8188_of_data,
1500 &mt8188_of_data_c1,
1501 NULL
1504 static const struct mtk_scp_of_data *mt8195_of_data_cores[] = {
1505 &mt8195_of_data,
1506 &mt8195_of_data_c1,
1507 NULL
1510 static const struct of_device_id mtk_scp_of_match[] = {
1511 { .compatible = "mediatek,mt8183-scp", .data = &mt8183_of_data },
1512 { .compatible = "mediatek,mt8186-scp", .data = &mt8186_of_data },
1513 { .compatible = "mediatek,mt8188-scp", .data = &mt8188_of_data },
1514 { .compatible = "mediatek,mt8188-scp-dual", .data = &mt8188_of_data_cores },
1515 { .compatible = "mediatek,mt8192-scp", .data = &mt8192_of_data },
1516 { .compatible = "mediatek,mt8195-scp", .data = &mt8195_of_data },
1517 { .compatible = "mediatek,mt8195-scp-dual", .data = &mt8195_of_data_cores },
1520 MODULE_DEVICE_TABLE(of, mtk_scp_of_match);
1522 static struct platform_driver mtk_scp_driver = {
1523 .probe = scp_probe,
1524 .remove = scp_remove,
1525 .driver = {
1526 .name = "mtk-scp",
1527 .of_match_table = mtk_scp_of_match,
1531 module_platform_driver(mtk_scp_driver);
1533 MODULE_LICENSE("GPL v2");
1534 MODULE_DESCRIPTION("MediaTek SCP control driver");