xref: /openbmc/linux/drivers/remoteproc/mtk_scp.c (revision 36fe4655)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Copyright (c) 2019 MediaTek Inc.
4 
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>
19 
20 #include "mtk_common.h"
21 #include "remoteproc_internal.h"
22 
23 #define MAX_CODE_SIZE 0x500000
24 #define SECTION_NAME_IPI_BUFFER ".ipi_buffer"
25 
26 /**
27  * scp_get() - get a reference to SCP.
28  *
29  * @pdev:	the platform device of the module requesting SCP platform
30  *		device for using SCP API.
31  *
32  * Return: Return NULL if failed.  otherwise reference to SCP.
33  **/
34 struct mtk_scp *scp_get(struct platform_device *pdev)
35 {
36 	struct device *dev = &pdev->dev;
37 	struct device_node *scp_node;
38 	struct platform_device *scp_pdev;
39 
40 	scp_node = of_parse_phandle(dev->of_node, "mediatek,scp", 0);
41 	if (!scp_node) {
42 		dev_err(dev, "can't get SCP node\n");
43 		return NULL;
44 	}
45 
46 	scp_pdev = of_find_device_by_node(scp_node);
47 	of_node_put(scp_node);
48 
49 	if (WARN_ON(!scp_pdev)) {
50 		dev_err(dev, "SCP pdev failed\n");
51 		return NULL;
52 	}
53 
54 	return platform_get_drvdata(scp_pdev);
55 }
56 EXPORT_SYMBOL_GPL(scp_get);
57 
58 /**
59  * scp_put() - "free" the SCP
60  *
61  * @scp:	mtk_scp structure from scp_get().
62  **/
63 void scp_put(struct mtk_scp *scp)
64 {
65 	put_device(scp->dev);
66 }
67 EXPORT_SYMBOL_GPL(scp_put);
68 
69 static void scp_wdt_handler(struct mtk_scp *scp, u32 scp_to_host)
70 {
71 	dev_err(scp->dev, "SCP watchdog timeout! 0x%x", scp_to_host);
72 	rproc_report_crash(scp->rproc, RPROC_WATCHDOG);
73 }
74 
75 static void scp_init_ipi_handler(void *data, unsigned int len, void *priv)
76 {
77 	struct mtk_scp *scp = (struct mtk_scp *)priv;
78 	struct scp_run *run = (struct scp_run *)data;
79 
80 	scp->run.signaled = run->signaled;
81 	strscpy(scp->run.fw_ver, run->fw_ver, SCP_FW_VER_LEN);
82 	scp->run.dec_capability = run->dec_capability;
83 	scp->run.enc_capability = run->enc_capability;
84 	wake_up_interruptible(&scp->run.wq);
85 }
86 
87 static void scp_ipi_handler(struct mtk_scp *scp)
88 {
89 	struct mtk_share_obj __iomem *rcv_obj = scp->recv_buf;
90 	struct scp_ipi_desc *ipi_desc = scp->ipi_desc;
91 	u8 tmp_data[SCP_SHARE_BUFFER_SIZE];
92 	scp_ipi_handler_t handler;
93 	u32 id = readl(&rcv_obj->id);
94 	u32 len = readl(&rcv_obj->len);
95 
96 	if (len > SCP_SHARE_BUFFER_SIZE) {
97 		dev_err(scp->dev, "ipi message too long (len %d, max %d)", len,
98 			SCP_SHARE_BUFFER_SIZE);
99 		return;
100 	}
101 	if (id >= SCP_IPI_MAX) {
102 		dev_err(scp->dev, "No such ipi id = %d\n", id);
103 		return;
104 	}
105 
106 	scp_ipi_lock(scp, id);
107 	handler = ipi_desc[id].handler;
108 	if (!handler) {
109 		dev_err(scp->dev, "No such ipi id = %d\n", id);
110 		scp_ipi_unlock(scp, id);
111 		return;
112 	}
113 
114 	memcpy_fromio(tmp_data, &rcv_obj->share_buf, len);
115 	handler(tmp_data, len, ipi_desc[id].priv);
116 	scp_ipi_unlock(scp, id);
117 
118 	scp->ipi_id_ack[id] = true;
119 	wake_up(&scp->ack_wq);
120 }
121 
122 static int scp_elf_read_ipi_buf_addr(struct mtk_scp *scp,
123 				     const struct firmware *fw,
124 				     size_t *offset);
125 
126 static int scp_ipi_init(struct mtk_scp *scp, const struct firmware *fw)
127 {
128 	int ret;
129 	size_t offset;
130 
131 	/* read the ipi buf addr from FW itself first */
132 	ret = scp_elf_read_ipi_buf_addr(scp, fw, &offset);
133 	if (ret) {
134 		/* use default ipi buf addr if the FW doesn't have it */
135 		offset = scp->data->ipi_buf_offset;
136 		if (!offset)
137 			return ret;
138 	}
139 	dev_info(scp->dev, "IPI buf addr %#010zx\n", offset);
140 
141 	scp->recv_buf = (struct mtk_share_obj __iomem *)
142 			(scp->sram_base + offset);
143 	scp->send_buf = (struct mtk_share_obj __iomem *)
144 			(scp->sram_base + offset + sizeof(*scp->recv_buf));
145 	memset_io(scp->recv_buf, 0, sizeof(*scp->recv_buf));
146 	memset_io(scp->send_buf, 0, sizeof(*scp->send_buf));
147 
148 	return 0;
149 }
150 
151 static void mt8183_scp_reset_assert(struct mtk_scp *scp)
152 {
153 	u32 val;
154 
155 	val = readl(scp->reg_base + MT8183_SW_RSTN);
156 	val &= ~MT8183_SW_RSTN_BIT;
157 	writel(val, scp->reg_base + MT8183_SW_RSTN);
158 }
159 
160 static void mt8183_scp_reset_deassert(struct mtk_scp *scp)
161 {
162 	u32 val;
163 
164 	val = readl(scp->reg_base + MT8183_SW_RSTN);
165 	val |= MT8183_SW_RSTN_BIT;
166 	writel(val, scp->reg_base + MT8183_SW_RSTN);
167 }
168 
169 static void mt8192_scp_reset_assert(struct mtk_scp *scp)
170 {
171 	writel(1, scp->reg_base + MT8192_CORE0_SW_RSTN_SET);
172 }
173 
174 static void mt8192_scp_reset_deassert(struct mtk_scp *scp)
175 {
176 	writel(1, scp->reg_base + MT8192_CORE0_SW_RSTN_CLR);
177 }
178 
179 static void mt8183_scp_irq_handler(struct mtk_scp *scp)
180 {
181 	u32 scp_to_host;
182 
183 	scp_to_host = readl(scp->reg_base + MT8183_SCP_TO_HOST);
184 	if (scp_to_host & MT8183_SCP_IPC_INT_BIT)
185 		scp_ipi_handler(scp);
186 	else
187 		scp_wdt_handler(scp, scp_to_host);
188 
189 	/* SCP won't send another interrupt until we set SCP_TO_HOST to 0. */
190 	writel(MT8183_SCP_IPC_INT_BIT | MT8183_SCP_WDT_INT_BIT,
191 	       scp->reg_base + MT8183_SCP_TO_HOST);
192 }
193 
194 static void mt8192_scp_irq_handler(struct mtk_scp *scp)
195 {
196 	u32 scp_to_host;
197 
198 	scp_to_host = readl(scp->reg_base + MT8192_SCP2APMCU_IPC_SET);
199 
200 	if (scp_to_host & MT8192_SCP_IPC_INT_BIT)
201 		scp_ipi_handler(scp);
202 	else
203 		scp_wdt_handler(scp, scp_to_host);
204 
205 	/*
206 	 * SCP won't send another interrupt until we clear
207 	 * MT8192_SCP2APMCU_IPC.
208 	 */
209 	writel(MT8192_SCP_IPC_INT_BIT,
210 	       scp->reg_base + MT8192_SCP2APMCU_IPC_CLR);
211 }
212 
213 static irqreturn_t scp_irq_handler(int irq, void *priv)
214 {
215 	struct mtk_scp *scp = priv;
216 	int ret;
217 
218 	ret = clk_prepare_enable(scp->clk);
219 	if (ret) {
220 		dev_err(scp->dev, "failed to enable clocks\n");
221 		return IRQ_NONE;
222 	}
223 
224 	scp->data->scp_irq_handler(scp);
225 
226 	clk_disable_unprepare(scp->clk);
227 
228 	return IRQ_HANDLED;
229 }
230 
231 static int scp_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
232 {
233 	struct device *dev = &rproc->dev;
234 	struct elf32_hdr *ehdr;
235 	struct elf32_phdr *phdr;
236 	int i, ret = 0;
237 	const u8 *elf_data = fw->data;
238 
239 	ehdr = (struct elf32_hdr *)elf_data;
240 	phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
241 
242 	/* go through the available ELF segments */
243 	for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
244 		u32 da = phdr->p_paddr;
245 		u32 memsz = phdr->p_memsz;
246 		u32 filesz = phdr->p_filesz;
247 		u32 offset = phdr->p_offset;
248 		void __iomem *ptr;
249 
250 		dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
251 			phdr->p_type, da, memsz, filesz);
252 
253 		if (phdr->p_type != PT_LOAD)
254 			continue;
255 		if (!filesz)
256 			continue;
257 
258 		if (filesz > memsz) {
259 			dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
260 				filesz, memsz);
261 			ret = -EINVAL;
262 			break;
263 		}
264 
265 		if (offset + filesz > fw->size) {
266 			dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
267 				offset + filesz, fw->size);
268 			ret = -EINVAL;
269 			break;
270 		}
271 
272 		/* grab the kernel address for this device address */
273 		ptr = (void __iomem *)rproc_da_to_va(rproc, da, memsz);
274 		if (!ptr) {
275 			dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
276 			ret = -EINVAL;
277 			break;
278 		}
279 
280 		/* put the segment where the remote processor expects it */
281 		scp_memcpy_aligned(ptr, elf_data + phdr->p_offset, filesz);
282 	}
283 
284 	return ret;
285 }
286 
287 static int scp_elf_read_ipi_buf_addr(struct mtk_scp *scp,
288 				     const struct firmware *fw,
289 				     size_t *offset)
290 {
291 	struct elf32_hdr *ehdr;
292 	struct elf32_shdr *shdr, *shdr_strtab;
293 	int i;
294 	const u8 *elf_data = fw->data;
295 	const char *strtab;
296 
297 	ehdr = (struct elf32_hdr *)elf_data;
298 	shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
299 	shdr_strtab = shdr + ehdr->e_shstrndx;
300 	strtab = (const char *)(elf_data + shdr_strtab->sh_offset);
301 
302 	for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
303 		if (strcmp(strtab + shdr->sh_name,
304 			   SECTION_NAME_IPI_BUFFER) == 0) {
305 			*offset = shdr->sh_addr;
306 			return 0;
307 		}
308 	}
309 
310 	return -ENOENT;
311 }
312 
313 static int mt8183_scp_before_load(struct mtk_scp *scp)
314 {
315 	/* Clear SCP to host interrupt */
316 	writel(MT8183_SCP_IPC_INT_BIT, scp->reg_base + MT8183_SCP_TO_HOST);
317 
318 	/* Reset clocks before loading FW */
319 	writel(0x0, scp->reg_base + MT8183_SCP_CLK_SW_SEL);
320 	writel(0x0, scp->reg_base + MT8183_SCP_CLK_DIV_SEL);
321 
322 	/* Initialize TCM before loading FW. */
323 	writel(0x0, scp->reg_base + MT8183_SCP_L1_SRAM_PD);
324 	writel(0x0, scp->reg_base + MT8183_SCP_TCM_TAIL_SRAM_PD);
325 
326 	/* Turn on the power of SCP's SRAM before using it. */
327 	writel(0x0, scp->reg_base + MT8183_SCP_SRAM_PDN);
328 
329 	/*
330 	 * Set I-cache and D-cache size before loading SCP FW.
331 	 * SCP SRAM logical address may change when cache size setting differs.
332 	 */
333 	writel(MT8183_SCP_CACHE_CON_WAYEN | MT8183_SCP_CACHESIZE_8KB,
334 	       scp->reg_base + MT8183_SCP_CACHE_CON);
335 	writel(MT8183_SCP_CACHESIZE_8KB, scp->reg_base + MT8183_SCP_DCACHE_CON);
336 
337 	return 0;
338 }
339 
340 static void mt8192_power_on_sram(void __iomem *addr)
341 {
342 	int i;
343 
344 	for (i = 31; i >= 0; i--)
345 		writel(GENMASK(i, 0), addr);
346 	writel(0, addr);
347 }
348 
349 static void mt8192_power_off_sram(void __iomem *addr)
350 {
351 	int i;
352 
353 	writel(0, addr);
354 	for (i = 0; i < 32; i++)
355 		writel(GENMASK(i, 0), addr);
356 }
357 
358 static int mt8192_scp_before_load(struct mtk_scp *scp)
359 {
360 	/* clear SPM interrupt, SCP2SPM_IPC_CLR */
361 	writel(0xff, scp->reg_base + MT8192_SCP2SPM_IPC_CLR);
362 
363 	writel(1, scp->reg_base + MT8192_CORE0_SW_RSTN_SET);
364 
365 	/* enable SRAM clock */
366 	mt8192_power_on_sram(scp->reg_base + MT8192_L2TCM_SRAM_PD_0);
367 	mt8192_power_on_sram(scp->reg_base + MT8192_L2TCM_SRAM_PD_1);
368 	mt8192_power_on_sram(scp->reg_base + MT8192_L2TCM_SRAM_PD_2);
369 	mt8192_power_on_sram(scp->reg_base + MT8192_L1TCM_SRAM_PDN);
370 	mt8192_power_on_sram(scp->reg_base + MT8192_CPU0_SRAM_PD);
371 
372 	return 0;
373 }
374 
375 static int scp_load(struct rproc *rproc, const struct firmware *fw)
376 {
377 	struct mtk_scp *scp = rproc->priv;
378 	struct device *dev = scp->dev;
379 	int ret;
380 
381 	ret = clk_prepare_enable(scp->clk);
382 	if (ret) {
383 		dev_err(dev, "failed to enable clocks\n");
384 		return ret;
385 	}
386 
387 	/* Hold SCP in reset while loading FW. */
388 	scp->data->scp_reset_assert(scp);
389 
390 	ret = scp->data->scp_before_load(scp);
391 	if (ret < 0)
392 		goto leave;
393 
394 	ret = scp_elf_load_segments(rproc, fw);
395 leave:
396 	clk_disable_unprepare(scp->clk);
397 
398 	return ret;
399 }
400 
401 static int scp_parse_fw(struct rproc *rproc, const struct firmware *fw)
402 {
403 	struct mtk_scp *scp = rproc->priv;
404 	struct device *dev = scp->dev;
405 	int ret;
406 
407 	ret = clk_prepare_enable(scp->clk);
408 	if (ret) {
409 		dev_err(dev, "failed to enable clocks\n");
410 		return ret;
411 	}
412 
413 	ret = scp_ipi_init(scp, fw);
414 	clk_disable_unprepare(scp->clk);
415 	return ret;
416 }
417 
418 static int scp_start(struct rproc *rproc)
419 {
420 	struct mtk_scp *scp = (struct mtk_scp *)rproc->priv;
421 	struct device *dev = scp->dev;
422 	struct scp_run *run = &scp->run;
423 	int ret;
424 
425 	ret = clk_prepare_enable(scp->clk);
426 	if (ret) {
427 		dev_err(dev, "failed to enable clocks\n");
428 		return ret;
429 	}
430 
431 	run->signaled = false;
432 
433 	scp->data->scp_reset_deassert(scp);
434 
435 	ret = wait_event_interruptible_timeout(
436 					run->wq,
437 					run->signaled,
438 					msecs_to_jiffies(2000));
439 
440 	if (ret == 0) {
441 		dev_err(dev, "wait SCP initialization timeout!\n");
442 		ret = -ETIME;
443 		goto stop;
444 	}
445 	if (ret == -ERESTARTSYS) {
446 		dev_err(dev, "wait SCP interrupted by a signal!\n");
447 		goto stop;
448 	}
449 
450 	clk_disable_unprepare(scp->clk);
451 	dev_info(dev, "SCP is ready. FW version %s\n", run->fw_ver);
452 
453 	return 0;
454 
455 stop:
456 	scp->data->scp_reset_assert(scp);
457 	clk_disable_unprepare(scp->clk);
458 	return ret;
459 }
460 
461 static void *scp_da_to_va(struct rproc *rproc, u64 da, size_t len)
462 {
463 	struct mtk_scp *scp = (struct mtk_scp *)rproc->priv;
464 	int offset;
465 
466 	if (da < scp->sram_size) {
467 		offset = da;
468 		if (offset >= 0 && (offset + len) <= scp->sram_size)
469 			return (void __force *)scp->sram_base + offset;
470 	} else if (scp->dram_size) {
471 		offset = da - scp->dma_addr;
472 		if (offset >= 0 && (offset + len) <= scp->dram_size)
473 			return scp->cpu_addr + offset;
474 	}
475 
476 	return NULL;
477 }
478 
479 static void mt8183_scp_stop(struct mtk_scp *scp)
480 {
481 	/* Disable SCP watchdog */
482 	writel(0, scp->reg_base + MT8183_WDT_CFG);
483 }
484 
485 static void mt8192_scp_stop(struct mtk_scp *scp)
486 {
487 	/* Disable SRAM clock */
488 	mt8192_power_off_sram(scp->reg_base + MT8192_L2TCM_SRAM_PD_0);
489 	mt8192_power_off_sram(scp->reg_base + MT8192_L2TCM_SRAM_PD_1);
490 	mt8192_power_off_sram(scp->reg_base + MT8192_L2TCM_SRAM_PD_2);
491 	mt8192_power_off_sram(scp->reg_base + MT8192_L1TCM_SRAM_PDN);
492 	mt8192_power_off_sram(scp->reg_base + MT8192_CPU0_SRAM_PD);
493 
494 	/* Disable SCP watchdog */
495 	writel(0, scp->reg_base + MT8192_CORE0_WDT_CFG);
496 }
497 
498 static int scp_stop(struct rproc *rproc)
499 {
500 	struct mtk_scp *scp = (struct mtk_scp *)rproc->priv;
501 	int ret;
502 
503 	ret = clk_prepare_enable(scp->clk);
504 	if (ret) {
505 		dev_err(scp->dev, "failed to enable clocks\n");
506 		return ret;
507 	}
508 
509 	scp->data->scp_reset_assert(scp);
510 	scp->data->scp_stop(scp);
511 	clk_disable_unprepare(scp->clk);
512 
513 	return 0;
514 }
515 
516 static const struct rproc_ops scp_ops = {
517 	.start		= scp_start,
518 	.stop		= scp_stop,
519 	.load		= scp_load,
520 	.da_to_va	= scp_da_to_va,
521 	.parse_fw	= scp_parse_fw,
522 };
523 
524 /**
525  * scp_get_device() - get device struct of SCP
526  *
527  * @scp:	mtk_scp structure
528  **/
529 struct device *scp_get_device(struct mtk_scp *scp)
530 {
531 	return scp->dev;
532 }
533 EXPORT_SYMBOL_GPL(scp_get_device);
534 
535 /**
536  * scp_get_rproc() - get rproc struct of SCP
537  *
538  * @scp:	mtk_scp structure
539  **/
540 struct rproc *scp_get_rproc(struct mtk_scp *scp)
541 {
542 	return scp->rproc;
543 }
544 EXPORT_SYMBOL_GPL(scp_get_rproc);
545 
546 /**
547  * scp_get_vdec_hw_capa() - get video decoder hardware capability
548  *
549  * @scp:	mtk_scp structure
550  *
551  * Return: video decoder hardware capability
552  **/
553 unsigned int scp_get_vdec_hw_capa(struct mtk_scp *scp)
554 {
555 	return scp->run.dec_capability;
556 }
557 EXPORT_SYMBOL_GPL(scp_get_vdec_hw_capa);
558 
559 /**
560  * scp_get_venc_hw_capa() - get video encoder hardware capability
561  *
562  * @scp:	mtk_scp structure
563  *
564  * Return: video encoder hardware capability
565  **/
566 unsigned int scp_get_venc_hw_capa(struct mtk_scp *scp)
567 {
568 	return scp->run.enc_capability;
569 }
570 EXPORT_SYMBOL_GPL(scp_get_venc_hw_capa);
571 
572 /**
573  * scp_mapping_dm_addr() - Mapping SRAM/DRAM to kernel virtual address
574  *
575  * @scp:	mtk_scp structure
576  * @mem_addr:	SCP views memory address
577  *
578  * Mapping the SCP's SRAM address /
579  * DMEM (Data Extended Memory) memory address /
580  * Working buffer memory address to
581  * kernel virtual address.
582  *
583  * Return: Return ERR_PTR(-EINVAL) if mapping failed,
584  * otherwise the mapped kernel virtual address
585  **/
586 void *scp_mapping_dm_addr(struct mtk_scp *scp, u32 mem_addr)
587 {
588 	void *ptr;
589 
590 	ptr = scp_da_to_va(scp->rproc, mem_addr, 0);
591 	if (!ptr)
592 		return ERR_PTR(-EINVAL);
593 
594 	return ptr;
595 }
596 EXPORT_SYMBOL_GPL(scp_mapping_dm_addr);
597 
598 static int scp_map_memory_region(struct mtk_scp *scp)
599 {
600 	int ret;
601 
602 	ret = of_reserved_mem_device_init(scp->dev);
603 
604 	/* reserved memory is optional. */
605 	if (ret == -ENODEV) {
606 		dev_info(scp->dev, "skipping reserved memory initialization.");
607 		return 0;
608 	}
609 
610 	if (ret) {
611 		dev_err(scp->dev, "failed to assign memory-region: %d\n", ret);
612 		return -ENOMEM;
613 	}
614 
615 	/* Reserved SCP code size */
616 	scp->dram_size = MAX_CODE_SIZE;
617 	scp->cpu_addr = dma_alloc_coherent(scp->dev, scp->dram_size,
618 					   &scp->dma_addr, GFP_KERNEL);
619 	if (!scp->cpu_addr)
620 		return -ENOMEM;
621 
622 	return 0;
623 }
624 
625 static void scp_unmap_memory_region(struct mtk_scp *scp)
626 {
627 	if (scp->dram_size == 0)
628 		return;
629 
630 	dma_free_coherent(scp->dev, scp->dram_size, scp->cpu_addr,
631 			  scp->dma_addr);
632 	of_reserved_mem_device_release(scp->dev);
633 }
634 
635 static int scp_register_ipi(struct platform_device *pdev, u32 id,
636 			    ipi_handler_t handler, void *priv)
637 {
638 	struct mtk_scp *scp = platform_get_drvdata(pdev);
639 
640 	return scp_ipi_register(scp, id, handler, priv);
641 }
642 
643 static void scp_unregister_ipi(struct platform_device *pdev, u32 id)
644 {
645 	struct mtk_scp *scp = platform_get_drvdata(pdev);
646 
647 	scp_ipi_unregister(scp, id);
648 }
649 
650 static int scp_send_ipi(struct platform_device *pdev, u32 id, void *buf,
651 			unsigned int len, unsigned int wait)
652 {
653 	struct mtk_scp *scp = platform_get_drvdata(pdev);
654 
655 	return scp_ipi_send(scp, id, buf, len, wait);
656 }
657 
658 static struct mtk_rpmsg_info mtk_scp_rpmsg_info = {
659 	.send_ipi = scp_send_ipi,
660 	.register_ipi = scp_register_ipi,
661 	.unregister_ipi = scp_unregister_ipi,
662 	.ns_ipi_id = SCP_IPI_NS_SERVICE,
663 };
664 
665 static void scp_add_rpmsg_subdev(struct mtk_scp *scp)
666 {
667 	scp->rpmsg_subdev =
668 		mtk_rpmsg_create_rproc_subdev(to_platform_device(scp->dev),
669 					      &mtk_scp_rpmsg_info);
670 	if (scp->rpmsg_subdev)
671 		rproc_add_subdev(scp->rproc, scp->rpmsg_subdev);
672 }
673 
674 static void scp_remove_rpmsg_subdev(struct mtk_scp *scp)
675 {
676 	if (scp->rpmsg_subdev) {
677 		rproc_remove_subdev(scp->rproc, scp->rpmsg_subdev);
678 		mtk_rpmsg_destroy_rproc_subdev(scp->rpmsg_subdev);
679 		scp->rpmsg_subdev = NULL;
680 	}
681 }
682 
683 static int scp_probe(struct platform_device *pdev)
684 {
685 	struct device *dev = &pdev->dev;
686 	struct device_node *np = dev->of_node;
687 	struct mtk_scp *scp;
688 	struct rproc *rproc;
689 	struct resource *res;
690 	char *fw_name = "scp.img";
691 	int ret, i;
692 
693 	rproc = rproc_alloc(dev,
694 			    np->name,
695 			    &scp_ops,
696 			    fw_name,
697 			    sizeof(*scp));
698 	if (!rproc) {
699 		dev_err(dev, "unable to allocate remoteproc\n");
700 		return -ENOMEM;
701 	}
702 
703 	scp = (struct mtk_scp *)rproc->priv;
704 	scp->rproc = rproc;
705 	scp->dev = dev;
706 	scp->data = of_device_get_match_data(dev);
707 	platform_set_drvdata(pdev, scp);
708 
709 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sram");
710 	scp->sram_base = devm_ioremap_resource(dev, res);
711 	if (IS_ERR((__force void *)scp->sram_base)) {
712 		dev_err(dev, "Failed to parse and map sram memory\n");
713 		ret = PTR_ERR((__force void *)scp->sram_base);
714 		goto free_rproc;
715 	}
716 	scp->sram_size = resource_size(res);
717 
718 	mutex_init(&scp->send_lock);
719 	for (i = 0; i < SCP_IPI_MAX; i++)
720 		mutex_init(&scp->ipi_desc[i].lock);
721 
722 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg");
723 	scp->reg_base = devm_ioremap_resource(dev, res);
724 	if (IS_ERR((__force void *)scp->reg_base)) {
725 		dev_err(dev, "Failed to parse and map cfg memory\n");
726 		ret = PTR_ERR((__force void *)scp->reg_base);
727 		goto destroy_mutex;
728 	}
729 
730 	ret = scp_map_memory_region(scp);
731 	if (ret)
732 		goto destroy_mutex;
733 
734 	scp->clk = devm_clk_get(dev, "main");
735 	if (IS_ERR(scp->clk)) {
736 		dev_err(dev, "Failed to get clock\n");
737 		ret = PTR_ERR(scp->clk);
738 		goto release_dev_mem;
739 	}
740 
741 	/* register SCP initialization IPI */
742 	ret = scp_ipi_register(scp, SCP_IPI_INIT, scp_init_ipi_handler, scp);
743 	if (ret) {
744 		dev_err(dev, "Failed to register IPI_SCP_INIT\n");
745 		goto release_dev_mem;
746 	}
747 
748 	init_waitqueue_head(&scp->run.wq);
749 	init_waitqueue_head(&scp->ack_wq);
750 
751 	scp_add_rpmsg_subdev(scp);
752 
753 	ret = devm_request_threaded_irq(dev, platform_get_irq(pdev, 0), NULL,
754 					scp_irq_handler, IRQF_ONESHOT,
755 					pdev->name, scp);
756 
757 	if (ret) {
758 		dev_err(dev, "failed to request irq\n");
759 		goto remove_subdev;
760 	}
761 
762 	ret = rproc_add(rproc);
763 	if (ret)
764 		goto remove_subdev;
765 
766 	return 0;
767 
768 remove_subdev:
769 	scp_remove_rpmsg_subdev(scp);
770 	scp_ipi_unregister(scp, SCP_IPI_INIT);
771 release_dev_mem:
772 	scp_unmap_memory_region(scp);
773 destroy_mutex:
774 	for (i = 0; i < SCP_IPI_MAX; i++)
775 		mutex_destroy(&scp->ipi_desc[i].lock);
776 	mutex_destroy(&scp->send_lock);
777 free_rproc:
778 	rproc_free(rproc);
779 
780 	return ret;
781 }
782 
783 static int scp_remove(struct platform_device *pdev)
784 {
785 	struct mtk_scp *scp = platform_get_drvdata(pdev);
786 	int i;
787 
788 	rproc_del(scp->rproc);
789 	scp_remove_rpmsg_subdev(scp);
790 	scp_ipi_unregister(scp, SCP_IPI_INIT);
791 	scp_unmap_memory_region(scp);
792 	for (i = 0; i < SCP_IPI_MAX; i++)
793 		mutex_destroy(&scp->ipi_desc[i].lock);
794 	mutex_destroy(&scp->send_lock);
795 	rproc_free(scp->rproc);
796 
797 	return 0;
798 }
799 
800 static const struct mtk_scp_of_data mt8183_of_data = {
801 	.scp_before_load = mt8183_scp_before_load,
802 	.scp_irq_handler = mt8183_scp_irq_handler,
803 	.scp_reset_assert = mt8183_scp_reset_assert,
804 	.scp_reset_deassert = mt8183_scp_reset_deassert,
805 	.scp_stop = mt8183_scp_stop,
806 	.host_to_scp_reg = MT8183_HOST_TO_SCP,
807 	.host_to_scp_int_bit = MT8183_HOST_IPC_INT_BIT,
808 	.ipi_buf_offset = 0x7bdb0,
809 };
810 
811 static const struct mtk_scp_of_data mt8192_of_data = {
812 	.scp_before_load = mt8192_scp_before_load,
813 	.scp_irq_handler = mt8192_scp_irq_handler,
814 	.scp_reset_assert = mt8192_scp_reset_assert,
815 	.scp_reset_deassert = mt8192_scp_reset_deassert,
816 	.scp_stop = mt8192_scp_stop,
817 	.host_to_scp_reg = MT8192_GIPC_IN_SET,
818 	.host_to_scp_int_bit = MT8192_HOST_IPC_INT_BIT,
819 };
820 
821 static const struct of_device_id mtk_scp_of_match[] = {
822 	{ .compatible = "mediatek,mt8183-scp", .data = &mt8183_of_data },
823 	{ .compatible = "mediatek,mt8192-scp", .data = &mt8192_of_data },
824 	{},
825 };
826 MODULE_DEVICE_TABLE(of, mtk_scp_of_match);
827 
828 static struct platform_driver mtk_scp_driver = {
829 	.probe = scp_probe,
830 	.remove = scp_remove,
831 	.driver = {
832 		.name = "mtk-scp",
833 		.of_match_table = mtk_scp_of_match,
834 	},
835 };
836 
837 module_platform_driver(mtk_scp_driver);
838 
839 MODULE_LICENSE("GPL v2");
840 MODULE_DESCRIPTION("MediaTek SCP control driver");
841