Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

remoteproc/mediatek: add SCP support for mt8183

Provide a basic driver to control Cortex M4 co-processor

Signed-off-by: Erin Lo <erin.lo@mediatek.com>
Signed-off-by: Nicolas Boichat <drinkcat@chromium.org>
Signed-off-by: Pi-Hsun Shih <pihsun@chromium.org>
Link: https://lore.kernel.org/r/20191112110330.179649-3-pihsun@chromium.org
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>

authored by

Erin Lo and committed by
Bjorn Andersson
63c13d61 e47e9887

+995
+9
drivers/remoteproc/Kconfig
··· 23 23 24 24 It's safe to say N here. 25 25 26 + config MTK_SCP 27 + tristate "Mediatek SCP support" 28 + depends on ARCH_MEDIATEK 29 + help 30 + Say y here to support Mediatek's System Companion Processor (SCP) via 31 + the remote processor framework. 32 + 33 + It's safe to say N here. 34 + 26 35 config OMAP_REMOTEPROC 27 36 tristate "OMAP remoteproc support" 28 37 depends on ARCH_OMAP4 || SOC_OMAP5
+1
drivers/remoteproc/Makefile
··· 10 10 remoteproc-y += remoteproc_virtio.o 11 11 remoteproc-y += remoteproc_elf_loader.o 12 12 obj-$(CONFIG_IMX_REMOTEPROC) += imx_rproc.o 13 + obj-$(CONFIG_MTK_SCP) += mtk_scp.o mtk_scp_ipi.o 13 14 obj-$(CONFIG_OMAP_REMOTEPROC) += omap_remoteproc.o 14 15 obj-$(CONFIG_WKUP_M3_RPROC) += wkup_m3_rproc.o 15 16 obj-$(CONFIG_DA8XX_REMOTEPROC) += da8xx_remoteproc.o
+92
drivers/remoteproc/mtk_common.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + /* 3 + * Copyright (c) 2019 MediaTek Inc. 4 + */ 5 + 6 + #ifndef __RPROC_MTK_COMMON_H 7 + #define __RPROC_MTK_COMMON_H 8 + 9 + #include <linux/interrupt.h> 10 + #include <linux/kernel.h> 11 + #include <linux/platform_device.h> 12 + #include <linux/remoteproc.h> 13 + #include <linux/remoteproc/mtk_scp.h> 14 + 15 + #define MT8183_SW_RSTN 0x0 16 + #define MT8183_SW_RSTN_BIT BIT(0) 17 + #define MT8183_SCP_TO_HOST 0x1C 18 + #define MT8183_SCP_IPC_INT_BIT BIT(0) 19 + #define MT8183_SCP_WDT_INT_BIT BIT(8) 20 + #define MT8183_HOST_TO_SCP 0x28 21 + #define MT8183_HOST_IPC_INT_BIT BIT(0) 22 + #define MT8183_WDT_CFG 0x84 23 + #define MT8183_SCP_CLK_SW_SEL 0x4000 24 + #define MT8183_SCP_CLK_DIV_SEL 0x4024 25 + #define MT8183_SCP_SRAM_PDN 0x402C 26 + #define MT8183_SCP_L1_SRAM_PD 0x4080 27 + #define MT8183_SCP_TCM_TAIL_SRAM_PD 0x4094 28 + 29 + #define MT8183_SCP_CACHE_SEL(x) (0x14000 + (x) * 0x3000) 30 + #define MT8183_SCP_CACHE_CON MT8183_SCP_CACHE_SEL(0) 31 + #define MT8183_SCP_DCACHE_CON MT8183_SCP_CACHE_SEL(1) 32 + #define MT8183_SCP_CACHESIZE_8KB BIT(8) 33 + #define MT8183_SCP_CACHE_CON_WAYEN BIT(10) 34 + 35 + #define SCP_FW_VER_LEN 32 36 + #define SCP_SHARE_BUFFER_SIZE 288 37 + 38 + struct scp_run { 39 + u32 signaled; 40 + s8 fw_ver[SCP_FW_VER_LEN]; 41 + u32 dec_capability; 42 + u32 enc_capability; 43 + wait_queue_head_t wq; 44 + }; 45 + 46 + struct scp_ipi_desc { 47 + /* For protecting handler. */ 48 + struct mutex lock; 49 + scp_ipi_handler_t handler; 50 + void *priv; 51 + }; 52 + 53 + struct mtk_scp { 54 + struct device *dev; 55 + struct rproc *rproc; 56 + struct clk *clk; 57 + void __iomem *reg_base; 58 + void __iomem *sram_base; 59 + size_t sram_size; 60 + 61 + struct mtk_share_obj __iomem *recv_buf; 62 + struct mtk_share_obj __iomem *send_buf; 63 + struct scp_run run; 64 + /* To prevent multiple ipi_send run concurrently. */ 65 + struct mutex send_lock; 66 + struct scp_ipi_desc ipi_desc[SCP_IPI_MAX]; 67 + bool ipi_id_ack[SCP_IPI_MAX]; 68 + wait_queue_head_t ack_wq; 69 + 70 + void __iomem *cpu_addr; 71 + phys_addr_t phys_addr; 72 + size_t dram_size; 73 + }; 74 + 75 + /** 76 + * struct mtk_share_obj - SRAM buffer shared with AP and SCP 77 + * 78 + * @id: IPI id 79 + * @len: share buffer length 80 + * @share_buf: share buffer data 81 + */ 82 + struct mtk_share_obj { 83 + u32 id; 84 + u32 len; 85 + u8 share_buf[SCP_SHARE_BUFFER_SIZE]; 86 + }; 87 + 88 + void scp_memcpy_aligned(void __iomem *dst, const void *src, unsigned int len); 89 + void scp_ipi_lock(struct mtk_scp *scp, u32 id); 90 + void scp_ipi_unlock(struct mtk_scp *scp, u32 id); 91 + 92 + #endif
+610
drivers/remoteproc/mtk_scp.c
··· 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 + 19 + #include "mtk_common.h" 20 + #include "remoteproc_internal.h" 21 + 22 + #define MAX_CODE_SIZE 0x500000 23 + #define SCP_FW_END 0x7C000 24 + 25 + /** 26 + * scp_get() - get a reference to SCP. 27 + * 28 + * @pdev: the platform device of the module requesting SCP platform 29 + * device for using SCP API. 30 + * 31 + * Return: Return NULL if failed. otherwise reference to SCP. 32 + **/ 33 + struct mtk_scp *scp_get(struct platform_device *pdev) 34 + { 35 + struct device *dev = &pdev->dev; 36 + struct device_node *scp_node; 37 + struct platform_device *scp_pdev; 38 + 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; 43 + } 44 + 45 + scp_pdev = of_find_device_by_node(scp_node); 46 + of_node_put(scp_node); 47 + 48 + if (WARN_ON(!scp_pdev)) { 49 + dev_err(dev, "SCP pdev failed\n"); 50 + return NULL; 51 + } 52 + 53 + return platform_get_drvdata(scp_pdev); 54 + } 55 + EXPORT_SYMBOL_GPL(scp_get); 56 + 57 + /** 58 + * scp_put() - "free" the SCP 59 + * 60 + * @scp: mtk_scp structure from scp_get(). 61 + **/ 62 + void scp_put(struct mtk_scp *scp) 63 + { 64 + put_device(scp->dev); 65 + } 66 + EXPORT_SYMBOL_GPL(scp_put); 67 + 68 + static void scp_wdt_handler(struct mtk_scp *scp, u32 scp_to_host) 69 + { 70 + dev_err(scp->dev, "SCP watchdog timeout! 0x%x", scp_to_host); 71 + rproc_report_crash(scp->rproc, RPROC_WATCHDOG); 72 + } 73 + 74 + static void scp_init_ipi_handler(void *data, unsigned int len, void *priv) 75 + { 76 + struct mtk_scp *scp = (struct mtk_scp *)priv; 77 + struct scp_run *run = (struct scp_run *)data; 78 + 79 + scp->run.signaled = run->signaled; 80 + strscpy(scp->run.fw_ver, run->fw_ver, SCP_FW_VER_LEN); 81 + scp->run.dec_capability = run->dec_capability; 82 + scp->run.enc_capability = run->enc_capability; 83 + wake_up_interruptible(&scp->run.wq); 84 + } 85 + 86 + static void scp_ipi_handler(struct mtk_scp *scp) 87 + { 88 + struct mtk_share_obj __iomem *rcv_obj = scp->recv_buf; 89 + struct scp_ipi_desc *ipi_desc = scp->ipi_desc; 90 + u8 tmp_data[SCP_SHARE_BUFFER_SIZE]; 91 + scp_ipi_handler_t handler; 92 + u32 id = readl(&rcv_obj->id); 93 + u32 len = readl(&rcv_obj->len); 94 + 95 + if (len > SCP_SHARE_BUFFER_SIZE) { 96 + dev_err(scp->dev, "ipi message too long (len %d, max %d)", len, 97 + SCP_SHARE_BUFFER_SIZE); 98 + return; 99 + } 100 + if (id >= SCP_IPI_MAX) { 101 + dev_err(scp->dev, "No such ipi id = %d\n", id); 102 + return; 103 + } 104 + 105 + scp_ipi_lock(scp, id); 106 + handler = ipi_desc[id].handler; 107 + if (!handler) { 108 + dev_err(scp->dev, "No such ipi id = %d\n", id); 109 + scp_ipi_unlock(scp, id); 110 + return; 111 + } 112 + 113 + memcpy_fromio(tmp_data, &rcv_obj->share_buf, len); 114 + handler(tmp_data, len, ipi_desc[id].priv); 115 + scp_ipi_unlock(scp, id); 116 + 117 + scp->ipi_id_ack[id] = true; 118 + wake_up(&scp->ack_wq); 119 + } 120 + 121 + static int scp_ipi_init(struct mtk_scp *scp) 122 + { 123 + size_t send_offset = SCP_FW_END - sizeof(struct mtk_share_obj); 124 + size_t recv_offset = send_offset - sizeof(struct mtk_share_obj); 125 + 126 + /* Disable SCP to host interrupt */ 127 + writel(MT8183_SCP_IPC_INT_BIT, scp->reg_base + MT8183_SCP_TO_HOST); 128 + 129 + /* shared buffer initialization */ 130 + scp->recv_buf = 131 + (struct mtk_share_obj __iomem *)(scp->sram_base + recv_offset); 132 + scp->send_buf = 133 + (struct mtk_share_obj __iomem *)(scp->sram_base + send_offset); 134 + memset_io(scp->recv_buf, 0, sizeof(scp->recv_buf)); 135 + memset_io(scp->send_buf, 0, sizeof(scp->send_buf)); 136 + 137 + return 0; 138 + } 139 + 140 + static void scp_reset_assert(const struct mtk_scp *scp) 141 + { 142 + u32 val; 143 + 144 + val = readl(scp->reg_base + MT8183_SW_RSTN); 145 + val &= ~MT8183_SW_RSTN_BIT; 146 + writel(val, scp->reg_base + MT8183_SW_RSTN); 147 + } 148 + 149 + static void scp_reset_deassert(const struct mtk_scp *scp) 150 + { 151 + u32 val; 152 + 153 + val = readl(scp->reg_base + MT8183_SW_RSTN); 154 + val |= MT8183_SW_RSTN_BIT; 155 + writel(val, scp->reg_base + MT8183_SW_RSTN); 156 + } 157 + 158 + static irqreturn_t scp_irq_handler(int irq, void *priv) 159 + { 160 + struct mtk_scp *scp = priv; 161 + u32 scp_to_host; 162 + int ret; 163 + 164 + ret = clk_prepare_enable(scp->clk); 165 + if (ret) { 166 + dev_err(scp->dev, "failed to enable clocks\n"); 167 + return IRQ_NONE; 168 + } 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 + clk_disable_unprepare(scp->clk); 180 + 181 + return IRQ_HANDLED; 182 + } 183 + 184 + static int scp_elf_load_segments(struct rproc *rproc, const struct firmware *fw) 185 + { 186 + struct device *dev = &rproc->dev; 187 + struct elf32_hdr *ehdr; 188 + struct elf32_phdr *phdr; 189 + int i, ret = 0; 190 + const u8 *elf_data = fw->data; 191 + 192 + ehdr = (struct elf32_hdr *)elf_data; 193 + phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff); 194 + 195 + /* go through the available ELF segments */ 196 + for (i = 0; i < ehdr->e_phnum; i++, phdr++) { 197 + u32 da = phdr->p_paddr; 198 + u32 memsz = phdr->p_memsz; 199 + u32 filesz = phdr->p_filesz; 200 + u32 offset = phdr->p_offset; 201 + void __iomem *ptr; 202 + 203 + if (phdr->p_type != PT_LOAD) 204 + continue; 205 + 206 + dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n", 207 + phdr->p_type, da, memsz, filesz); 208 + 209 + if (filesz > memsz) { 210 + dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n", 211 + filesz, memsz); 212 + ret = -EINVAL; 213 + break; 214 + } 215 + 216 + if (offset + filesz > fw->size) { 217 + dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n", 218 + offset + filesz, fw->size); 219 + ret = -EINVAL; 220 + break; 221 + } 222 + 223 + /* grab the kernel address for this device address */ 224 + ptr = (void __iomem *)rproc_da_to_va(rproc, da, memsz); 225 + if (!ptr) { 226 + dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz); 227 + ret = -EINVAL; 228 + break; 229 + } 230 + 231 + /* put the segment where the remote processor expects it */ 232 + if (phdr->p_filesz) 233 + scp_memcpy_aligned(ptr, elf_data + phdr->p_offset, 234 + filesz); 235 + } 236 + 237 + return ret; 238 + } 239 + 240 + static int scp_load(struct rproc *rproc, const struct firmware *fw) 241 + { 242 + const struct mtk_scp *scp = rproc->priv; 243 + struct device *dev = scp->dev; 244 + int ret; 245 + 246 + ret = clk_prepare_enable(scp->clk); 247 + if (ret) { 248 + dev_err(dev, "failed to enable clocks\n"); 249 + return ret; 250 + } 251 + 252 + /* Hold SCP in reset while loading FW. */ 253 + scp_reset_assert(scp); 254 + 255 + /* Reset clocks before loading FW */ 256 + writel(0x0, scp->reg_base + MT8183_SCP_CLK_SW_SEL); 257 + writel(0x0, scp->reg_base + MT8183_SCP_CLK_DIV_SEL); 258 + 259 + /* Initialize TCM before loading FW. */ 260 + writel(0x0, scp->reg_base + MT8183_SCP_L1_SRAM_PD); 261 + writel(0x0, scp->reg_base + MT8183_SCP_TCM_TAIL_SRAM_PD); 262 + 263 + /* Turn on the power of SCP's SRAM before using it. */ 264 + writel(0x0, scp->reg_base + MT8183_SCP_SRAM_PDN); 265 + 266 + /* 267 + * Set I-cache and D-cache size before loading SCP FW. 268 + * SCP SRAM logical address may change when cache size setting differs. 269 + */ 270 + writel(MT8183_SCP_CACHE_CON_WAYEN | MT8183_SCP_CACHESIZE_8KB, 271 + scp->reg_base + MT8183_SCP_CACHE_CON); 272 + writel(MT8183_SCP_CACHESIZE_8KB, scp->reg_base + MT8183_SCP_DCACHE_CON); 273 + 274 + ret = scp_elf_load_segments(rproc, fw); 275 + clk_disable_unprepare(scp->clk); 276 + 277 + return ret; 278 + } 279 + 280 + static int scp_start(struct rproc *rproc) 281 + { 282 + struct mtk_scp *scp = (struct mtk_scp *)rproc->priv; 283 + struct device *dev = scp->dev; 284 + struct scp_run *run = &scp->run; 285 + int ret; 286 + 287 + ret = clk_prepare_enable(scp->clk); 288 + if (ret) { 289 + dev_err(dev, "failed to enable clocks\n"); 290 + return ret; 291 + } 292 + 293 + run->signaled = false; 294 + 295 + scp_reset_deassert(scp); 296 + 297 + ret = wait_event_interruptible_timeout( 298 + run->wq, 299 + run->signaled, 300 + msecs_to_jiffies(2000)); 301 + 302 + if (ret == 0) { 303 + dev_err(dev, "wait SCP initialization timeout!\n"); 304 + ret = -ETIME; 305 + goto stop; 306 + } 307 + if (ret == -ERESTARTSYS) { 308 + dev_err(dev, "wait SCP interrupted by a signal!\n"); 309 + goto stop; 310 + } 311 + clk_disable_unprepare(scp->clk); 312 + dev_info(dev, "SCP is ready. FW version %s\n", run->fw_ver); 313 + 314 + return 0; 315 + 316 + stop: 317 + scp_reset_assert(scp); 318 + clk_disable_unprepare(scp->clk); 319 + return ret; 320 + } 321 + 322 + static void *scp_da_to_va(struct rproc *rproc, u64 da, int len) 323 + { 324 + struct mtk_scp *scp = (struct mtk_scp *)rproc->priv; 325 + int offset; 326 + 327 + if (da < scp->sram_size) { 328 + offset = da; 329 + if (offset >= 0 && (offset + len) < scp->sram_size) 330 + return (void __force *)scp->sram_base + offset; 331 + } else { 332 + offset = da - scp->phys_addr; 333 + if (offset >= 0 && (offset + len) < scp->dram_size) 334 + return (void __force *)scp->cpu_addr + offset; 335 + } 336 + 337 + return NULL; 338 + } 339 + 340 + static int scp_stop(struct rproc *rproc) 341 + { 342 + struct mtk_scp *scp = (struct mtk_scp *)rproc->priv; 343 + int ret; 344 + 345 + ret = clk_prepare_enable(scp->clk); 346 + if (ret) { 347 + dev_err(scp->dev, "failed to enable clocks\n"); 348 + return ret; 349 + } 350 + 351 + scp_reset_assert(scp); 352 + /* Disable SCP watchdog */ 353 + writel(0, scp->reg_base + MT8183_WDT_CFG); 354 + clk_disable_unprepare(scp->clk); 355 + 356 + return 0; 357 + } 358 + 359 + static const struct rproc_ops scp_ops = { 360 + .start = scp_start, 361 + .stop = scp_stop, 362 + .load = scp_load, 363 + .da_to_va = scp_da_to_va, 364 + }; 365 + 366 + /** 367 + * scp_get_device() - get device struct of SCP 368 + * 369 + * @scp: mtk_scp structure 370 + **/ 371 + struct device *scp_get_device(struct mtk_scp *scp) 372 + { 373 + return scp->dev; 374 + } 375 + EXPORT_SYMBOL_GPL(scp_get_device); 376 + 377 + /** 378 + * scp_get_rproc() - get rproc struct of SCP 379 + * 380 + * @scp: mtk_scp structure 381 + **/ 382 + struct rproc *scp_get_rproc(struct mtk_scp *scp) 383 + { 384 + return scp->rproc; 385 + } 386 + EXPORT_SYMBOL_GPL(scp_get_rproc); 387 + 388 + /** 389 + * scp_get_vdec_hw_capa() - get video decoder hardware capability 390 + * 391 + * @scp: mtk_scp structure 392 + * 393 + * Return: video decoder hardware capability 394 + **/ 395 + unsigned int scp_get_vdec_hw_capa(struct mtk_scp *scp) 396 + { 397 + return scp->run.dec_capability; 398 + } 399 + EXPORT_SYMBOL_GPL(scp_get_vdec_hw_capa); 400 + 401 + /** 402 + * scp_get_venc_hw_capa() - get video encoder hardware capability 403 + * 404 + * @scp: mtk_scp structure 405 + * 406 + * Return: video encoder hardware capability 407 + **/ 408 + unsigned int scp_get_venc_hw_capa(struct mtk_scp *scp) 409 + { 410 + return scp->run.enc_capability; 411 + } 412 + EXPORT_SYMBOL_GPL(scp_get_venc_hw_capa); 413 + 414 + /** 415 + * scp_mapping_dm_addr() - Mapping SRAM/DRAM to kernel virtual address 416 + * 417 + * @scp: mtk_scp structure 418 + * @mem_addr: SCP views memory address 419 + * 420 + * Mapping the SCP's SRAM address / 421 + * DMEM (Data Extended Memory) memory address / 422 + * Working buffer memory address to 423 + * kernel virtual address. 424 + * 425 + * Return: Return ERR_PTR(-EINVAL) if mapping failed, 426 + * otherwise the mapped kernel virtual address 427 + **/ 428 + void *scp_mapping_dm_addr(struct mtk_scp *scp, u32 mem_addr) 429 + { 430 + void *ptr; 431 + 432 + ptr = scp_da_to_va(scp->rproc, mem_addr, 0); 433 + if (!ptr) 434 + return ERR_PTR(-EINVAL); 435 + 436 + return ptr; 437 + } 438 + EXPORT_SYMBOL_GPL(scp_mapping_dm_addr); 439 + 440 + static int scp_map_memory_region(struct mtk_scp *scp) 441 + { 442 + int ret; 443 + 444 + ret = of_reserved_mem_device_init(scp->dev); 445 + if (ret) { 446 + dev_err(scp->dev, "failed to assign memory-region: %d\n", ret); 447 + return -ENOMEM; 448 + } 449 + 450 + /* Reserved SCP code size */ 451 + scp->dram_size = MAX_CODE_SIZE; 452 + scp->cpu_addr = dma_alloc_coherent(scp->dev, scp->dram_size, 453 + &scp->phys_addr, GFP_KERNEL); 454 + if (!scp->cpu_addr) 455 + return -ENOMEM; 456 + 457 + return 0; 458 + } 459 + 460 + static void scp_unmap_memory_region(struct mtk_scp *scp) 461 + { 462 + dma_free_coherent(scp->dev, scp->dram_size, scp->cpu_addr, 463 + scp->phys_addr); 464 + of_reserved_mem_device_release(scp->dev); 465 + } 466 + 467 + static int scp_probe(struct platform_device *pdev) 468 + { 469 + struct device *dev = &pdev->dev; 470 + struct device_node *np = dev->of_node; 471 + struct mtk_scp *scp; 472 + struct rproc *rproc; 473 + struct resource *res; 474 + char *fw_name = "scp.img"; 475 + int ret, i; 476 + 477 + rproc = rproc_alloc(dev, 478 + np->name, 479 + &scp_ops, 480 + fw_name, 481 + sizeof(*scp)); 482 + if (!rproc) { 483 + dev_err(dev, "unable to allocate remoteproc\n"); 484 + return -ENOMEM; 485 + } 486 + 487 + scp = (struct mtk_scp *)rproc->priv; 488 + scp->rproc = rproc; 489 + scp->dev = dev; 490 + platform_set_drvdata(pdev, scp); 491 + 492 + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sram"); 493 + scp->sram_base = devm_ioremap_resource(dev, res); 494 + if (IS_ERR((__force void *)scp->sram_base)) { 495 + dev_err(dev, "Failed to parse and map sram memory\n"); 496 + ret = PTR_ERR((__force void *)scp->sram_base); 497 + goto free_rproc; 498 + } 499 + scp->sram_size = resource_size(res); 500 + 501 + mutex_init(&scp->send_lock); 502 + for (i = 0; i < SCP_IPI_MAX; i++) 503 + mutex_init(&scp->ipi_desc[i].lock); 504 + 505 + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg"); 506 + scp->reg_base = devm_ioremap_resource(dev, res); 507 + if (IS_ERR((__force void *)scp->reg_base)) { 508 + dev_err(dev, "Failed to parse and map cfg memory\n"); 509 + ret = PTR_ERR((__force void *)scp->reg_base); 510 + goto destroy_mutex; 511 + } 512 + 513 + ret = scp_map_memory_region(scp); 514 + if (ret) 515 + goto destroy_mutex; 516 + 517 + scp->clk = devm_clk_get(dev, "main"); 518 + if (IS_ERR(scp->clk)) { 519 + dev_err(dev, "Failed to get clock\n"); 520 + ret = PTR_ERR(scp->clk); 521 + goto release_dev_mem; 522 + } 523 + 524 + ret = clk_prepare_enable(scp->clk); 525 + if (ret) { 526 + dev_err(dev, "failed to enable clocks\n"); 527 + goto release_dev_mem; 528 + } 529 + 530 + ret = scp_ipi_init(scp); 531 + clk_disable_unprepare(scp->clk); 532 + if (ret) { 533 + dev_err(dev, "Failed to init ipi\n"); 534 + goto release_dev_mem; 535 + } 536 + 537 + /* register SCP initialization IPI */ 538 + ret = scp_ipi_register(scp, SCP_IPI_INIT, scp_init_ipi_handler, scp); 539 + if (ret) { 540 + dev_err(dev, "Failed to register IPI_SCP_INIT\n"); 541 + goto release_dev_mem; 542 + } 543 + 544 + init_waitqueue_head(&scp->run.wq); 545 + init_waitqueue_head(&scp->ack_wq); 546 + 547 + ret = devm_request_threaded_irq(dev, platform_get_irq(pdev, 0), NULL, 548 + scp_irq_handler, IRQF_ONESHOT, 549 + pdev->name, scp); 550 + 551 + if (ret) { 552 + dev_err(dev, "failed to request irq\n"); 553 + goto unregister_ipi; 554 + } 555 + 556 + ret = rproc_add(rproc); 557 + if (ret) 558 + goto unregister_ipi; 559 + 560 + return ret; 561 + 562 + unregister_ipi: 563 + scp_ipi_unregister(scp, SCP_IPI_INIT); 564 + release_dev_mem: 565 + scp_unmap_memory_region(scp); 566 + destroy_mutex: 567 + for (i = 0; i < SCP_IPI_MAX; i++) 568 + mutex_destroy(&scp->ipi_desc[i].lock); 569 + mutex_destroy(&scp->send_lock); 570 + free_rproc: 571 + rproc_free(rproc); 572 + 573 + return ret; 574 + } 575 + 576 + static int scp_remove(struct platform_device *pdev) 577 + { 578 + struct mtk_scp *scp = platform_get_drvdata(pdev); 579 + int i; 580 + 581 + rproc_del(scp->rproc); 582 + scp_ipi_unregister(scp, SCP_IPI_INIT); 583 + scp_unmap_memory_region(scp); 584 + for (i = 0; i < SCP_IPI_MAX; i++) 585 + mutex_destroy(&scp->ipi_desc[i].lock); 586 + mutex_destroy(&scp->send_lock); 587 + rproc_free(scp->rproc); 588 + 589 + return 0; 590 + } 591 + 592 + static const struct of_device_id mtk_scp_of_match[] = { 593 + { .compatible = "mediatek,mt8183-scp"}, 594 + {}, 595 + }; 596 + MODULE_DEVICE_TABLE(of, mtk_scp_of_match); 597 + 598 + static struct platform_driver mtk_scp_driver = { 599 + .probe = scp_probe, 600 + .remove = scp_remove, 601 + .driver = { 602 + .name = "mtk-scp", 603 + .of_match_table = of_match_ptr(mtk_scp_of_match), 604 + }, 605 + }; 606 + 607 + module_platform_driver(mtk_scp_driver); 608 + 609 + MODULE_LICENSE("GPL v2"); 610 + MODULE_DESCRIPTION("MediaTek SCP control driver");
+218
drivers/remoteproc/mtk_scp_ipi.c
··· 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/err.h> 8 + #include <linux/io.h> 9 + #include <linux/kernel.h> 10 + #include <linux/module.h> 11 + #include <linux/platform_device.h> 12 + #include <linux/remoteproc/mtk_scp.h> 13 + 14 + #include "mtk_common.h" 15 + 16 + /** 17 + * scp_ipi_register() - register an ipi function 18 + * 19 + * @scp: mtk_scp structure 20 + * @id: IPI ID 21 + * @handler: IPI handler 22 + * @priv: private data for IPI handler 23 + * 24 + * Register an ipi function to receive ipi interrupt from SCP. 25 + * 26 + * Returns 0 if ipi registers successfully, -error on error. 27 + */ 28 + int scp_ipi_register(struct mtk_scp *scp, 29 + u32 id, 30 + scp_ipi_handler_t handler, 31 + void *priv) 32 + { 33 + if (!scp) { 34 + dev_err(scp->dev, "scp device is not ready\n"); 35 + return -EPROBE_DEFER; 36 + } 37 + 38 + if (WARN_ON(id >= SCP_IPI_MAX) || WARN_ON(handler == NULL)) 39 + return -EINVAL; 40 + 41 + scp_ipi_lock(scp, id); 42 + scp->ipi_desc[id].handler = handler; 43 + scp->ipi_desc[id].priv = priv; 44 + scp_ipi_unlock(scp, id); 45 + 46 + return 0; 47 + } 48 + EXPORT_SYMBOL_GPL(scp_ipi_register); 49 + 50 + /** 51 + * scp_ipi_unregister() - unregister an ipi function 52 + * 53 + * @scp: mtk_scp structure 54 + * @id: IPI ID 55 + * 56 + * Unregister an ipi function to receive ipi interrupt from SCP. 57 + */ 58 + void scp_ipi_unregister(struct mtk_scp *scp, u32 id) 59 + { 60 + if (!scp) 61 + return; 62 + 63 + if (WARN_ON(id >= SCP_IPI_MAX)) 64 + return; 65 + 66 + scp_ipi_lock(scp, id); 67 + scp->ipi_desc[id].handler = NULL; 68 + scp->ipi_desc[id].priv = NULL; 69 + scp_ipi_unlock(scp, id); 70 + } 71 + EXPORT_SYMBOL_GPL(scp_ipi_unregister); 72 + 73 + /* 74 + * scp_memcpy_aligned() - Copy src to dst, where dst is in SCP SRAM region. 75 + * 76 + * @dst: Pointer to the destination buffer, should be in SCP SRAM region. 77 + * @src: Pointer to the source buffer. 78 + * @len: Length of the source buffer to be copied. 79 + * 80 + * Since AP access of SCP SRAM don't support byte write, this always write a 81 + * full word at a time, and may cause some extra bytes to be written at the 82 + * beginning & ending of dst. 83 + */ 84 + void scp_memcpy_aligned(void __iomem *dst, const void *src, unsigned int len) 85 + { 86 + void __iomem *ptr; 87 + u32 val; 88 + unsigned int i = 0, remain; 89 + 90 + if (!IS_ALIGNED((unsigned long)dst, 4)) { 91 + ptr = (void __iomem *)ALIGN_DOWN((unsigned long)dst, 4); 92 + i = 4 - (dst - ptr); 93 + val = readl_relaxed(ptr); 94 + memcpy((u8 *)&val + (4 - i), src, i); 95 + writel_relaxed(val, ptr); 96 + } 97 + 98 + __iowrite32_copy(dst + i, src + i, (len - i) / 4); 99 + remain = (len - i) % 4; 100 + 101 + if (remain > 0) { 102 + val = readl_relaxed(dst + len - remain); 103 + memcpy(&val, src + len - remain, remain); 104 + writel_relaxed(val, dst + len - remain); 105 + } 106 + } 107 + EXPORT_SYMBOL_GPL(scp_memcpy_aligned); 108 + 109 + /** 110 + * scp_ipi_lock() - Lock before operations of an IPI ID 111 + * 112 + * @scp: mtk_scp structure 113 + * @id: IPI ID 114 + * 115 + * Note: This should not be used by drivers other than mtk_scp. 116 + */ 117 + void scp_ipi_lock(struct mtk_scp *scp, u32 id) 118 + { 119 + if (WARN_ON(id >= SCP_IPI_MAX)) 120 + return; 121 + mutex_lock(&scp->ipi_desc[id].lock); 122 + } 123 + EXPORT_SYMBOL_GPL(scp_ipi_lock); 124 + 125 + /** 126 + * scp_ipi_lock() - Unlock after operations of an IPI ID 127 + * 128 + * @scp: mtk_scp structure 129 + * @id: IPI ID 130 + * 131 + * Note: This should not be used by drivers other than mtk_scp. 132 + */ 133 + void scp_ipi_unlock(struct mtk_scp *scp, u32 id) 134 + { 135 + if (WARN_ON(id >= SCP_IPI_MAX)) 136 + return; 137 + mutex_unlock(&scp->ipi_desc[id].lock); 138 + } 139 + EXPORT_SYMBOL_GPL(scp_ipi_unlock); 140 + 141 + /** 142 + * scp_ipi_send() - send data from AP to scp. 143 + * 144 + * @scp: mtk_scp structure 145 + * @id: IPI ID 146 + * @buf: the data buffer 147 + * @len: the data buffer length 148 + * @wait: number of msecs to wait for ack. 0 to skip waiting. 149 + * 150 + * This function is thread-safe. When this function returns, 151 + * SCP has received the data and starts the processing. 152 + * When the processing completes, IPI handler registered 153 + * by scp_ipi_register will be called in interrupt context. 154 + * 155 + * Returns 0 if sending data successfully, -error on error. 156 + **/ 157 + int scp_ipi_send(struct mtk_scp *scp, u32 id, void *buf, unsigned int len, 158 + unsigned int wait) 159 + { 160 + struct mtk_share_obj __iomem *send_obj = scp->send_buf; 161 + unsigned long timeout; 162 + int ret; 163 + 164 + if (WARN_ON(id <= SCP_IPI_INIT) || WARN_ON(id >= SCP_IPI_MAX) || 165 + WARN_ON(len > sizeof(send_obj->share_buf)) || WARN_ON(!buf)) 166 + return -EINVAL; 167 + 168 + mutex_lock(&scp->send_lock); 169 + 170 + ret = clk_prepare_enable(scp->clk); 171 + if (ret) { 172 + dev_err(scp->dev, "failed to enable clock\n"); 173 + goto unlock_mutex; 174 + } 175 + 176 + /* Wait until SCP receives the last command */ 177 + timeout = jiffies + msecs_to_jiffies(2000); 178 + do { 179 + if (time_after(jiffies, timeout)) { 180 + dev_err(scp->dev, "%s: IPI timeout!\n", __func__); 181 + ret = -ETIMEDOUT; 182 + goto clock_disable; 183 + } 184 + } while (readl(scp->reg_base + MT8183_HOST_TO_SCP)); 185 + 186 + scp_memcpy_aligned(send_obj->share_buf, buf, len); 187 + 188 + writel(len, &send_obj->len); 189 + writel(id, &send_obj->id); 190 + 191 + scp->ipi_id_ack[id] = false; 192 + /* send the command to SCP */ 193 + writel(MT8183_HOST_IPC_INT_BIT, scp->reg_base + MT8183_HOST_TO_SCP); 194 + 195 + if (wait) { 196 + /* wait for SCP's ACK */ 197 + timeout = msecs_to_jiffies(wait); 198 + ret = wait_event_timeout(scp->ack_wq, 199 + scp->ipi_id_ack[id], 200 + timeout); 201 + scp->ipi_id_ack[id] = false; 202 + if (WARN(!ret, "scp ipi %d ack time out !", id)) 203 + ret = -EIO; 204 + else 205 + ret = 0; 206 + } 207 + 208 + clock_disable: 209 + clk_disable_unprepare(scp->clk); 210 + unlock_mutex: 211 + mutex_unlock(&scp->send_lock); 212 + 213 + return ret; 214 + } 215 + EXPORT_SYMBOL_GPL(scp_ipi_send); 216 + 217 + MODULE_LICENSE("GPL v2"); 218 + MODULE_DESCRIPTION("MediaTek scp IPI interface");
+65
include/linux/remoteproc/mtk_scp.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + /* 3 + * Copyright (c) 2019 MediaTek Inc. 4 + */ 5 + 6 + #ifndef _MTK_SCP_H 7 + #define _MTK_SCP_H 8 + 9 + #include <linux/platform_device.h> 10 + 11 + typedef void (*scp_ipi_handler_t) (void *data, 12 + unsigned int len, 13 + void *priv); 14 + struct mtk_scp; 15 + 16 + /** 17 + * enum ipi_id - the id of inter-processor interrupt 18 + * 19 + * @SCP_IPI_INIT: The interrupt from scp is to notfiy kernel 20 + * SCP initialization completed. 21 + * IPI_SCP_INIT is sent from SCP when firmware is 22 + * loaded. AP doesn't need to send IPI_SCP_INIT 23 + * command to SCP. 24 + * For other IPI below, AP should send the request 25 + * to SCP to trigger the interrupt. 26 + * @SCP_IPI_MAX: The maximum IPI number 27 + */ 28 + 29 + enum scp_ipi_id { 30 + SCP_IPI_INIT = 0, 31 + SCP_IPI_VDEC_H264, 32 + SCP_IPI_VDEC_VP8, 33 + SCP_IPI_VDEC_VP9, 34 + SCP_IPI_VENC_H264, 35 + SCP_IPI_VENC_VP8, 36 + SCP_IPI_MDP_INIT, 37 + SCP_IPI_MDP_DEINIT, 38 + SCP_IPI_MDP_FRAME, 39 + SCP_IPI_DIP, 40 + SCP_IPI_ISP_CMD, 41 + SCP_IPI_ISP_FRAME, 42 + SCP_IPI_FD_CMD, 43 + SCP_IPI_CROS_HOST_CMD, 44 + SCP_IPI_MAX, 45 + }; 46 + 47 + struct mtk_scp *scp_get(struct platform_device *pdev); 48 + void scp_put(struct mtk_scp *scp); 49 + 50 + struct device *scp_get_device(struct mtk_scp *scp); 51 + struct rproc *scp_get_rproc(struct mtk_scp *scp); 52 + 53 + int scp_ipi_register(struct mtk_scp *scp, u32 id, scp_ipi_handler_t handler, 54 + void *priv); 55 + void scp_ipi_unregister(struct mtk_scp *scp, u32 id); 56 + 57 + int scp_ipi_send(struct mtk_scp *scp, u32 id, void *buf, unsigned int len, 58 + unsigned int wait); 59 + 60 + unsigned int scp_get_vdec_hw_capa(struct mtk_scp *scp); 61 + unsigned int scp_get_venc_hw_capa(struct mtk_scp *scp); 62 + 63 + void *scp_mapping_dm_addr(struct mtk_scp *scp, u32 mem_addr); 64 + 65 + #endif /* _MTK_SCP_H */