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