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