163c13d61SErin Lo // SPDX-License-Identifier: GPL-2.0 263c13d61SErin Lo // 363c13d61SErin Lo // Copyright (c) 2019 MediaTek Inc. 463c13d61SErin Lo 563c13d61SErin Lo #include <asm/barrier.h> 663c13d61SErin Lo #include <linux/clk.h> 763c13d61SErin Lo #include <linux/dma-mapping.h> 863c13d61SErin Lo #include <linux/err.h> 963c13d61SErin Lo #include <linux/interrupt.h> 1063c13d61SErin Lo #include <linux/kernel.h> 1163c13d61SErin Lo #include <linux/module.h> 1263c13d61SErin Lo #include <linux/of_address.h> 1363c13d61SErin Lo #include <linux/of_platform.h> 1463c13d61SErin Lo #include <linux/of_reserved_mem.h> 1563c13d61SErin Lo #include <linux/platform_device.h> 1663c13d61SErin Lo #include <linux/remoteproc.h> 1763c13d61SErin Lo #include <linux/remoteproc/mtk_scp.h> 1870179969SPi-Hsun Shih #include <linux/rpmsg/mtk_rpmsg.h> 1963c13d61SErin Lo 2063c13d61SErin Lo #include "mtk_common.h" 2163c13d61SErin Lo #include "remoteproc_internal.h" 2263c13d61SErin Lo 2363c13d61SErin Lo #define MAX_CODE_SIZE 0x500000 243efa0ea7STzung-Bi Shih #define SECTION_NAME_IPI_BUFFER ".ipi_buffer" 2563c13d61SErin Lo 2663c13d61SErin Lo /** 2763c13d61SErin Lo * scp_get() - get a reference to SCP. 2863c13d61SErin Lo * 2963c13d61SErin Lo * @pdev: the platform device of the module requesting SCP platform 3063c13d61SErin Lo * device for using SCP API. 3163c13d61SErin Lo * 3263c13d61SErin Lo * Return: Return NULL if failed. otherwise reference to SCP. 3363c13d61SErin Lo **/ 3463c13d61SErin Lo struct mtk_scp *scp_get(struct platform_device *pdev) 3563c13d61SErin Lo { 3663c13d61SErin Lo struct device *dev = &pdev->dev; 3763c13d61SErin Lo struct device_node *scp_node; 3863c13d61SErin Lo struct platform_device *scp_pdev; 3963c13d61SErin Lo 4063c13d61SErin Lo scp_node = of_parse_phandle(dev->of_node, "mediatek,scp", 0); 4163c13d61SErin Lo if (!scp_node) { 4263c13d61SErin Lo dev_err(dev, "can't get SCP node\n"); 4363c13d61SErin Lo return NULL; 4463c13d61SErin Lo } 4563c13d61SErin Lo 4663c13d61SErin Lo scp_pdev = of_find_device_by_node(scp_node); 4763c13d61SErin Lo of_node_put(scp_node); 4863c13d61SErin Lo 4963c13d61SErin Lo if (WARN_ON(!scp_pdev)) { 5063c13d61SErin Lo dev_err(dev, "SCP pdev failed\n"); 5163c13d61SErin Lo return NULL; 5263c13d61SErin Lo } 5363c13d61SErin Lo 5463c13d61SErin Lo return platform_get_drvdata(scp_pdev); 5563c13d61SErin Lo } 5663c13d61SErin Lo EXPORT_SYMBOL_GPL(scp_get); 5763c13d61SErin Lo 5863c13d61SErin Lo /** 5963c13d61SErin Lo * scp_put() - "free" the SCP 6063c13d61SErin Lo * 6163c13d61SErin Lo * @scp: mtk_scp structure from scp_get(). 6263c13d61SErin Lo **/ 6363c13d61SErin Lo void scp_put(struct mtk_scp *scp) 6463c13d61SErin Lo { 6563c13d61SErin Lo put_device(scp->dev); 6663c13d61SErin Lo } 6763c13d61SErin Lo EXPORT_SYMBOL_GPL(scp_put); 6863c13d61SErin Lo 6963c13d61SErin Lo static void scp_wdt_handler(struct mtk_scp *scp, u32 scp_to_host) 7063c13d61SErin Lo { 7163c13d61SErin Lo dev_err(scp->dev, "SCP watchdog timeout! 0x%x", scp_to_host); 7263c13d61SErin Lo rproc_report_crash(scp->rproc, RPROC_WATCHDOG); 7363c13d61SErin Lo } 7463c13d61SErin Lo 7563c13d61SErin Lo static void scp_init_ipi_handler(void *data, unsigned int len, void *priv) 7663c13d61SErin Lo { 7763c13d61SErin Lo struct mtk_scp *scp = (struct mtk_scp *)priv; 7863c13d61SErin Lo struct scp_run *run = (struct scp_run *)data; 7963c13d61SErin Lo 8063c13d61SErin Lo scp->run.signaled = run->signaled; 8163c13d61SErin Lo strscpy(scp->run.fw_ver, run->fw_ver, SCP_FW_VER_LEN); 8263c13d61SErin Lo scp->run.dec_capability = run->dec_capability; 8363c13d61SErin Lo scp->run.enc_capability = run->enc_capability; 8463c13d61SErin Lo wake_up_interruptible(&scp->run.wq); 8563c13d61SErin Lo } 8663c13d61SErin Lo 8763c13d61SErin Lo static void scp_ipi_handler(struct mtk_scp *scp) 8863c13d61SErin Lo { 8963c13d61SErin Lo struct mtk_share_obj __iomem *rcv_obj = scp->recv_buf; 9063c13d61SErin Lo struct scp_ipi_desc *ipi_desc = scp->ipi_desc; 9163c13d61SErin Lo u8 tmp_data[SCP_SHARE_BUFFER_SIZE]; 9263c13d61SErin Lo scp_ipi_handler_t handler; 9363c13d61SErin Lo u32 id = readl(&rcv_obj->id); 9463c13d61SErin Lo u32 len = readl(&rcv_obj->len); 9563c13d61SErin Lo 9663c13d61SErin Lo if (len > SCP_SHARE_BUFFER_SIZE) { 9763c13d61SErin Lo dev_err(scp->dev, "ipi message too long (len %d, max %d)", len, 9863c13d61SErin Lo SCP_SHARE_BUFFER_SIZE); 9963c13d61SErin Lo return; 10063c13d61SErin Lo } 10163c13d61SErin Lo if (id >= SCP_IPI_MAX) { 10263c13d61SErin Lo dev_err(scp->dev, "No such ipi id = %d\n", id); 10363c13d61SErin Lo return; 10463c13d61SErin Lo } 10563c13d61SErin Lo 10663c13d61SErin Lo scp_ipi_lock(scp, id); 10763c13d61SErin Lo handler = ipi_desc[id].handler; 10863c13d61SErin Lo if (!handler) { 10963c13d61SErin Lo dev_err(scp->dev, "No such ipi id = %d\n", id); 11063c13d61SErin Lo scp_ipi_unlock(scp, id); 11163c13d61SErin Lo return; 11263c13d61SErin Lo } 11363c13d61SErin Lo 11463c13d61SErin Lo memcpy_fromio(tmp_data, &rcv_obj->share_buf, len); 11563c13d61SErin Lo handler(tmp_data, len, ipi_desc[id].priv); 11663c13d61SErin Lo scp_ipi_unlock(scp, id); 11763c13d61SErin Lo 11863c13d61SErin Lo scp->ipi_id_ack[id] = true; 11963c13d61SErin Lo wake_up(&scp->ack_wq); 12063c13d61SErin Lo } 12163c13d61SErin Lo 1223efa0ea7STzung-Bi Shih static int scp_elf_read_ipi_buf_addr(struct mtk_scp *scp, 1233efa0ea7STzung-Bi Shih const struct firmware *fw, 1243efa0ea7STzung-Bi Shih size_t *offset); 12563c13d61SErin Lo 1263efa0ea7STzung-Bi Shih static int scp_ipi_init(struct mtk_scp *scp, const struct firmware *fw) 1273efa0ea7STzung-Bi Shih { 1283efa0ea7STzung-Bi Shih int ret; 1293efa0ea7STzung-Bi Shih size_t offset; 1303efa0ea7STzung-Bi Shih 1313efa0ea7STzung-Bi Shih /* read the ipi buf addr from FW itself first */ 1323efa0ea7STzung-Bi Shih ret = scp_elf_read_ipi_buf_addr(scp, fw, &offset); 1333efa0ea7STzung-Bi Shih if (ret) { 1343efa0ea7STzung-Bi Shih /* use default ipi buf addr if the FW doesn't have it */ 1353efa0ea7STzung-Bi Shih offset = scp->data->ipi_buf_offset; 1363efa0ea7STzung-Bi Shih if (!offset) 1373efa0ea7STzung-Bi Shih return ret; 1383efa0ea7STzung-Bi Shih } 1393efa0ea7STzung-Bi Shih dev_info(scp->dev, "IPI buf addr %#010zx\n", offset); 1403efa0ea7STzung-Bi Shih 1413efa0ea7STzung-Bi Shih scp->recv_buf = (struct mtk_share_obj __iomem *) 1423efa0ea7STzung-Bi Shih (scp->sram_base + offset); 1433efa0ea7STzung-Bi Shih scp->send_buf = (struct mtk_share_obj __iomem *) 1443efa0ea7STzung-Bi Shih (scp->sram_base + offset + sizeof(*scp->recv_buf)); 1458096f80aSWei Yongjun memset_io(scp->recv_buf, 0, sizeof(*scp->recv_buf)); 1468096f80aSWei Yongjun memset_io(scp->send_buf, 0, sizeof(*scp->send_buf)); 14763c13d61SErin Lo 14863c13d61SErin Lo return 0; 14963c13d61SErin Lo } 15063c13d61SErin Lo 151fd0b6c1fSPi-Hsun Shih static void mt8183_scp_reset_assert(struct mtk_scp *scp) 15263c13d61SErin Lo { 15363c13d61SErin Lo u32 val; 15463c13d61SErin Lo 15563c13d61SErin Lo val = readl(scp->reg_base + MT8183_SW_RSTN); 15663c13d61SErin Lo val &= ~MT8183_SW_RSTN_BIT; 15763c13d61SErin Lo writel(val, scp->reg_base + MT8183_SW_RSTN); 15863c13d61SErin Lo } 15963c13d61SErin Lo 160fd0b6c1fSPi-Hsun Shih static void mt8183_scp_reset_deassert(struct mtk_scp *scp) 16163c13d61SErin Lo { 16263c13d61SErin Lo u32 val; 16363c13d61SErin Lo 16463c13d61SErin Lo val = readl(scp->reg_base + MT8183_SW_RSTN); 16563c13d61SErin Lo val |= MT8183_SW_RSTN_BIT; 16663c13d61SErin Lo writel(val, scp->reg_base + MT8183_SW_RSTN); 16763c13d61SErin Lo } 16863c13d61SErin Lo 169fd0b6c1fSPi-Hsun Shih static void mt8192_scp_reset_assert(struct mtk_scp *scp) 17063c13d61SErin Lo { 171fd0b6c1fSPi-Hsun Shih writel(1, scp->reg_base + MT8192_CORE0_SW_RSTN_SET); 17263c13d61SErin Lo } 17363c13d61SErin Lo 174fd0b6c1fSPi-Hsun Shih static void mt8192_scp_reset_deassert(struct mtk_scp *scp) 175fd0b6c1fSPi-Hsun Shih { 176fd0b6c1fSPi-Hsun Shih writel(1, scp->reg_base + MT8192_CORE0_SW_RSTN_CLR); 177fd0b6c1fSPi-Hsun Shih } 178fd0b6c1fSPi-Hsun Shih 179fd0b6c1fSPi-Hsun Shih static void mt8183_scp_irq_handler(struct mtk_scp *scp) 180fd0b6c1fSPi-Hsun Shih { 181fd0b6c1fSPi-Hsun Shih u32 scp_to_host; 182fd0b6c1fSPi-Hsun Shih 18363c13d61SErin Lo scp_to_host = readl(scp->reg_base + MT8183_SCP_TO_HOST); 18463c13d61SErin Lo if (scp_to_host & MT8183_SCP_IPC_INT_BIT) 18563c13d61SErin Lo scp_ipi_handler(scp); 18663c13d61SErin Lo else 18763c13d61SErin Lo scp_wdt_handler(scp, scp_to_host); 18863c13d61SErin Lo 18963c13d61SErin Lo /* SCP won't send another interrupt until we set SCP_TO_HOST to 0. */ 19063c13d61SErin Lo writel(MT8183_SCP_IPC_INT_BIT | MT8183_SCP_WDT_INT_BIT, 19163c13d61SErin Lo scp->reg_base + MT8183_SCP_TO_HOST); 192fd0b6c1fSPi-Hsun Shih } 193fd0b6c1fSPi-Hsun Shih 194fd0b6c1fSPi-Hsun Shih static void mt8192_scp_irq_handler(struct mtk_scp *scp) 195fd0b6c1fSPi-Hsun Shih { 196fd0b6c1fSPi-Hsun Shih u32 scp_to_host; 197fd0b6c1fSPi-Hsun Shih 198fd0b6c1fSPi-Hsun Shih scp_to_host = readl(scp->reg_base + MT8192_SCP2APMCU_IPC_SET); 199fd0b6c1fSPi-Hsun Shih 200*8c545f52STzung-Bi Shih if (scp_to_host & MT8192_SCP_IPC_INT_BIT) { 201fd0b6c1fSPi-Hsun Shih scp_ipi_handler(scp); 202fd0b6c1fSPi-Hsun Shih 203fd0b6c1fSPi-Hsun Shih /* 204fd0b6c1fSPi-Hsun Shih * SCP won't send another interrupt until we clear 205fd0b6c1fSPi-Hsun Shih * MT8192_SCP2APMCU_IPC. 206fd0b6c1fSPi-Hsun Shih */ 207fd0b6c1fSPi-Hsun Shih writel(MT8192_SCP_IPC_INT_BIT, 208fd0b6c1fSPi-Hsun Shih scp->reg_base + MT8192_SCP2APMCU_IPC_CLR); 209*8c545f52STzung-Bi Shih } else { 210*8c545f52STzung-Bi Shih scp_wdt_handler(scp, scp_to_host); 211*8c545f52STzung-Bi Shih writel(1, scp->reg_base + MT8192_CORE0_WDT_IRQ); 212*8c545f52STzung-Bi Shih } 213fd0b6c1fSPi-Hsun Shih } 214fd0b6c1fSPi-Hsun Shih 215fd0b6c1fSPi-Hsun Shih static irqreturn_t scp_irq_handler(int irq, void *priv) 216fd0b6c1fSPi-Hsun Shih { 217fd0b6c1fSPi-Hsun Shih struct mtk_scp *scp = priv; 218fd0b6c1fSPi-Hsun Shih int ret; 219fd0b6c1fSPi-Hsun Shih 220fd0b6c1fSPi-Hsun Shih ret = clk_prepare_enable(scp->clk); 221fd0b6c1fSPi-Hsun Shih if (ret) { 222fd0b6c1fSPi-Hsun Shih dev_err(scp->dev, "failed to enable clocks\n"); 223fd0b6c1fSPi-Hsun Shih return IRQ_NONE; 224fd0b6c1fSPi-Hsun Shih } 225fd0b6c1fSPi-Hsun Shih 226fd0b6c1fSPi-Hsun Shih scp->data->scp_irq_handler(scp); 227fd0b6c1fSPi-Hsun Shih 22863c13d61SErin Lo clk_disable_unprepare(scp->clk); 22963c13d61SErin Lo 23063c13d61SErin Lo return IRQ_HANDLED; 23163c13d61SErin Lo } 23263c13d61SErin Lo 23363c13d61SErin Lo static int scp_elf_load_segments(struct rproc *rproc, const struct firmware *fw) 23463c13d61SErin Lo { 23563c13d61SErin Lo struct device *dev = &rproc->dev; 23663c13d61SErin Lo struct elf32_hdr *ehdr; 23763c13d61SErin Lo struct elf32_phdr *phdr; 23863c13d61SErin Lo int i, ret = 0; 23963c13d61SErin Lo const u8 *elf_data = fw->data; 24063c13d61SErin Lo 24163c13d61SErin Lo ehdr = (struct elf32_hdr *)elf_data; 24263c13d61SErin Lo phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff); 24363c13d61SErin Lo 24463c13d61SErin Lo /* go through the available ELF segments */ 24563c13d61SErin Lo for (i = 0; i < ehdr->e_phnum; i++, phdr++) { 24663c13d61SErin Lo u32 da = phdr->p_paddr; 24763c13d61SErin Lo u32 memsz = phdr->p_memsz; 24863c13d61SErin Lo u32 filesz = phdr->p_filesz; 24963c13d61SErin Lo u32 offset = phdr->p_offset; 25063c13d61SErin Lo void __iomem *ptr; 25163c13d61SErin Lo 25263c13d61SErin Lo dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n", 25363c13d61SErin Lo phdr->p_type, da, memsz, filesz); 25463c13d61SErin Lo 25548cb5b68STzung-Bi Shih if (phdr->p_type != PT_LOAD) 25648cb5b68STzung-Bi Shih continue; 25748cb5b68STzung-Bi Shih if (!filesz) 25848cb5b68STzung-Bi Shih continue; 25948cb5b68STzung-Bi Shih 26063c13d61SErin Lo if (filesz > memsz) { 26163c13d61SErin Lo dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n", 26263c13d61SErin Lo filesz, memsz); 26363c13d61SErin Lo ret = -EINVAL; 26463c13d61SErin Lo break; 26563c13d61SErin Lo } 26663c13d61SErin Lo 26763c13d61SErin Lo if (offset + filesz > fw->size) { 26863c13d61SErin Lo dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n", 26963c13d61SErin Lo offset + filesz, fw->size); 27063c13d61SErin Lo ret = -EINVAL; 27163c13d61SErin Lo break; 27263c13d61SErin Lo } 27363c13d61SErin Lo 27463c13d61SErin Lo /* grab the kernel address for this device address */ 27563c13d61SErin Lo ptr = (void __iomem *)rproc_da_to_va(rproc, da, memsz); 27663c13d61SErin Lo if (!ptr) { 27763c13d61SErin Lo dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz); 27863c13d61SErin Lo ret = -EINVAL; 27963c13d61SErin Lo break; 28063c13d61SErin Lo } 28163c13d61SErin Lo 28263c13d61SErin Lo /* put the segment where the remote processor expects it */ 28348cb5b68STzung-Bi Shih scp_memcpy_aligned(ptr, elf_data + phdr->p_offset, filesz); 28463c13d61SErin Lo } 28563c13d61SErin Lo 28663c13d61SErin Lo return ret; 28763c13d61SErin Lo } 28863c13d61SErin Lo 2893efa0ea7STzung-Bi Shih static int scp_elf_read_ipi_buf_addr(struct mtk_scp *scp, 2903efa0ea7STzung-Bi Shih const struct firmware *fw, 2913efa0ea7STzung-Bi Shih size_t *offset) 2923efa0ea7STzung-Bi Shih { 2933efa0ea7STzung-Bi Shih struct elf32_hdr *ehdr; 2943efa0ea7STzung-Bi Shih struct elf32_shdr *shdr, *shdr_strtab; 2953efa0ea7STzung-Bi Shih int i; 2963efa0ea7STzung-Bi Shih const u8 *elf_data = fw->data; 2973efa0ea7STzung-Bi Shih const char *strtab; 2983efa0ea7STzung-Bi Shih 2993efa0ea7STzung-Bi Shih ehdr = (struct elf32_hdr *)elf_data; 3003efa0ea7STzung-Bi Shih shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff); 3013efa0ea7STzung-Bi Shih shdr_strtab = shdr + ehdr->e_shstrndx; 3023efa0ea7STzung-Bi Shih strtab = (const char *)(elf_data + shdr_strtab->sh_offset); 3033efa0ea7STzung-Bi Shih 3043efa0ea7STzung-Bi Shih for (i = 0; i < ehdr->e_shnum; i++, shdr++) { 3053efa0ea7STzung-Bi Shih if (strcmp(strtab + shdr->sh_name, 3063efa0ea7STzung-Bi Shih SECTION_NAME_IPI_BUFFER) == 0) { 3073efa0ea7STzung-Bi Shih *offset = shdr->sh_addr; 3083efa0ea7STzung-Bi Shih return 0; 3093efa0ea7STzung-Bi Shih } 3103efa0ea7STzung-Bi Shih } 3113efa0ea7STzung-Bi Shih 3123efa0ea7STzung-Bi Shih return -ENOENT; 3133efa0ea7STzung-Bi Shih } 3143efa0ea7STzung-Bi Shih 315fd0b6c1fSPi-Hsun Shih static int mt8183_scp_before_load(struct mtk_scp *scp) 31663c13d61SErin Lo { 317fd0b6c1fSPi-Hsun Shih /* Clear SCP to host interrupt */ 318fd0b6c1fSPi-Hsun Shih writel(MT8183_SCP_IPC_INT_BIT, scp->reg_base + MT8183_SCP_TO_HOST); 31963c13d61SErin Lo 32063c13d61SErin Lo /* Reset clocks before loading FW */ 32163c13d61SErin Lo writel(0x0, scp->reg_base + MT8183_SCP_CLK_SW_SEL); 32263c13d61SErin Lo writel(0x0, scp->reg_base + MT8183_SCP_CLK_DIV_SEL); 32363c13d61SErin Lo 32463c13d61SErin Lo /* Initialize TCM before loading FW. */ 32563c13d61SErin Lo writel(0x0, scp->reg_base + MT8183_SCP_L1_SRAM_PD); 32663c13d61SErin Lo writel(0x0, scp->reg_base + MT8183_SCP_TCM_TAIL_SRAM_PD); 32763c13d61SErin Lo 32863c13d61SErin Lo /* Turn on the power of SCP's SRAM before using it. */ 32963c13d61SErin Lo writel(0x0, scp->reg_base + MT8183_SCP_SRAM_PDN); 33063c13d61SErin Lo 33163c13d61SErin Lo /* 33263c13d61SErin Lo * Set I-cache and D-cache size before loading SCP FW. 33363c13d61SErin Lo * SCP SRAM logical address may change when cache size setting differs. 33463c13d61SErin Lo */ 33563c13d61SErin Lo writel(MT8183_SCP_CACHE_CON_WAYEN | MT8183_SCP_CACHESIZE_8KB, 33663c13d61SErin Lo scp->reg_base + MT8183_SCP_CACHE_CON); 33763c13d61SErin Lo writel(MT8183_SCP_CACHESIZE_8KB, scp->reg_base + MT8183_SCP_DCACHE_CON); 33863c13d61SErin Lo 339fd0b6c1fSPi-Hsun Shih return 0; 340fd0b6c1fSPi-Hsun Shih } 341fd0b6c1fSPi-Hsun Shih 342778f2664STzung-Bi Shih static void mt8192_power_on_sram(void __iomem *addr) 343fd0b6c1fSPi-Hsun Shih { 344fd0b6c1fSPi-Hsun Shih int i; 345fd0b6c1fSPi-Hsun Shih 346fd0b6c1fSPi-Hsun Shih for (i = 31; i >= 0; i--) 347fd0b6c1fSPi-Hsun Shih writel(GENMASK(i, 0), addr); 348fd0b6c1fSPi-Hsun Shih writel(0, addr); 349fd0b6c1fSPi-Hsun Shih } 350fd0b6c1fSPi-Hsun Shih 351778f2664STzung-Bi Shih static void mt8192_power_off_sram(void __iomem *addr) 352fd0b6c1fSPi-Hsun Shih { 353fd0b6c1fSPi-Hsun Shih int i; 354fd0b6c1fSPi-Hsun Shih 355fd0b6c1fSPi-Hsun Shih writel(0, addr); 356fd0b6c1fSPi-Hsun Shih for (i = 0; i < 32; i++) 357fd0b6c1fSPi-Hsun Shih writel(GENMASK(i, 0), addr); 358fd0b6c1fSPi-Hsun Shih } 359fd0b6c1fSPi-Hsun Shih 360fd0b6c1fSPi-Hsun Shih static int mt8192_scp_before_load(struct mtk_scp *scp) 361fd0b6c1fSPi-Hsun Shih { 362fd0b6c1fSPi-Hsun Shih /* clear SPM interrupt, SCP2SPM_IPC_CLR */ 363fd0b6c1fSPi-Hsun Shih writel(0xff, scp->reg_base + MT8192_SCP2SPM_IPC_CLR); 364fd0b6c1fSPi-Hsun Shih 365fd0b6c1fSPi-Hsun Shih writel(1, scp->reg_base + MT8192_CORE0_SW_RSTN_SET); 366fd0b6c1fSPi-Hsun Shih 367fd0b6c1fSPi-Hsun Shih /* enable SRAM clock */ 368fd0b6c1fSPi-Hsun Shih mt8192_power_on_sram(scp->reg_base + MT8192_L2TCM_SRAM_PD_0); 369fd0b6c1fSPi-Hsun Shih mt8192_power_on_sram(scp->reg_base + MT8192_L2TCM_SRAM_PD_1); 370fd0b6c1fSPi-Hsun Shih mt8192_power_on_sram(scp->reg_base + MT8192_L2TCM_SRAM_PD_2); 371fd0b6c1fSPi-Hsun Shih mt8192_power_on_sram(scp->reg_base + MT8192_L1TCM_SRAM_PDN); 372fd0b6c1fSPi-Hsun Shih mt8192_power_on_sram(scp->reg_base + MT8192_CPU0_SRAM_PD); 373fd0b6c1fSPi-Hsun Shih 374fd0b6c1fSPi-Hsun Shih return 0; 375fd0b6c1fSPi-Hsun Shih } 376fd0b6c1fSPi-Hsun Shih 377fd0b6c1fSPi-Hsun Shih static int scp_load(struct rproc *rproc, const struct firmware *fw) 378fd0b6c1fSPi-Hsun Shih { 379fd0b6c1fSPi-Hsun Shih struct mtk_scp *scp = rproc->priv; 380fd0b6c1fSPi-Hsun Shih struct device *dev = scp->dev; 381fd0b6c1fSPi-Hsun Shih int ret; 382fd0b6c1fSPi-Hsun Shih 383fd0b6c1fSPi-Hsun Shih ret = clk_prepare_enable(scp->clk); 384fd0b6c1fSPi-Hsun Shih if (ret) { 385fd0b6c1fSPi-Hsun Shih dev_err(dev, "failed to enable clocks\n"); 386fd0b6c1fSPi-Hsun Shih return ret; 387fd0b6c1fSPi-Hsun Shih } 388fd0b6c1fSPi-Hsun Shih 389fd0b6c1fSPi-Hsun Shih /* Hold SCP in reset while loading FW. */ 390fd0b6c1fSPi-Hsun Shih scp->data->scp_reset_assert(scp); 391fd0b6c1fSPi-Hsun Shih 392fd0b6c1fSPi-Hsun Shih ret = scp->data->scp_before_load(scp); 393fd0b6c1fSPi-Hsun Shih if (ret < 0) 39422c3df6fSTzung-Bi Shih goto leave; 395fd0b6c1fSPi-Hsun Shih 39663c13d61SErin Lo ret = scp_elf_load_segments(rproc, fw); 39722c3df6fSTzung-Bi Shih leave: 39863c13d61SErin Lo clk_disable_unprepare(scp->clk); 39963c13d61SErin Lo 40063c13d61SErin Lo return ret; 40163c13d61SErin Lo } 40263c13d61SErin Lo 4033efa0ea7STzung-Bi Shih static int scp_parse_fw(struct rproc *rproc, const struct firmware *fw) 4043efa0ea7STzung-Bi Shih { 4053efa0ea7STzung-Bi Shih struct mtk_scp *scp = rproc->priv; 4063efa0ea7STzung-Bi Shih struct device *dev = scp->dev; 4073efa0ea7STzung-Bi Shih int ret; 4083efa0ea7STzung-Bi Shih 4093efa0ea7STzung-Bi Shih ret = clk_prepare_enable(scp->clk); 4103efa0ea7STzung-Bi Shih if (ret) { 4113efa0ea7STzung-Bi Shih dev_err(dev, "failed to enable clocks\n"); 4123efa0ea7STzung-Bi Shih return ret; 4133efa0ea7STzung-Bi Shih } 4143efa0ea7STzung-Bi Shih 4153efa0ea7STzung-Bi Shih ret = scp_ipi_init(scp, fw); 4163efa0ea7STzung-Bi Shih clk_disable_unprepare(scp->clk); 4173efa0ea7STzung-Bi Shih return ret; 4183efa0ea7STzung-Bi Shih } 4193efa0ea7STzung-Bi Shih 42063c13d61SErin Lo static int scp_start(struct rproc *rproc) 42163c13d61SErin Lo { 42263c13d61SErin Lo struct mtk_scp *scp = (struct mtk_scp *)rproc->priv; 42363c13d61SErin Lo struct device *dev = scp->dev; 42463c13d61SErin Lo struct scp_run *run = &scp->run; 42563c13d61SErin Lo int ret; 42663c13d61SErin Lo 42763c13d61SErin Lo ret = clk_prepare_enable(scp->clk); 42863c13d61SErin Lo if (ret) { 42963c13d61SErin Lo dev_err(dev, "failed to enable clocks\n"); 43063c13d61SErin Lo return ret; 43163c13d61SErin Lo } 43263c13d61SErin Lo 43363c13d61SErin Lo run->signaled = false; 43463c13d61SErin Lo 435fd0b6c1fSPi-Hsun Shih scp->data->scp_reset_deassert(scp); 43663c13d61SErin Lo 43763c13d61SErin Lo ret = wait_event_interruptible_timeout( 43863c13d61SErin Lo run->wq, 43963c13d61SErin Lo run->signaled, 44063c13d61SErin Lo msecs_to_jiffies(2000)); 44163c13d61SErin Lo 44263c13d61SErin Lo if (ret == 0) { 44363c13d61SErin Lo dev_err(dev, "wait SCP initialization timeout!\n"); 44463c13d61SErin Lo ret = -ETIME; 44563c13d61SErin Lo goto stop; 44663c13d61SErin Lo } 44763c13d61SErin Lo if (ret == -ERESTARTSYS) { 44863c13d61SErin Lo dev_err(dev, "wait SCP interrupted by a signal!\n"); 44963c13d61SErin Lo goto stop; 45063c13d61SErin Lo } 451fd0b6c1fSPi-Hsun Shih 45263c13d61SErin Lo clk_disable_unprepare(scp->clk); 45363c13d61SErin Lo dev_info(dev, "SCP is ready. FW version %s\n", run->fw_ver); 45463c13d61SErin Lo 45563c13d61SErin Lo return 0; 45663c13d61SErin Lo 45763c13d61SErin Lo stop: 458fd0b6c1fSPi-Hsun Shih scp->data->scp_reset_assert(scp); 45963c13d61SErin Lo clk_disable_unprepare(scp->clk); 46063c13d61SErin Lo return ret; 46163c13d61SErin Lo } 46263c13d61SErin Lo 463e1833b9eSNathan Chancellor static void *scp_da_to_va(struct rproc *rproc, u64 da, size_t len) 46463c13d61SErin Lo { 46563c13d61SErin Lo struct mtk_scp *scp = (struct mtk_scp *)rproc->priv; 46663c13d61SErin Lo int offset; 46763c13d61SErin Lo 46863c13d61SErin Lo if (da < scp->sram_size) { 46963c13d61SErin Lo offset = da; 47071ffb5a2STzung-Bi Shih if (offset >= 0 && (offset + len) <= scp->sram_size) 47163c13d61SErin Lo return (void __force *)scp->sram_base + offset; 472fd0b6c1fSPi-Hsun Shih } else if (scp->dram_size) { 473c2781e4dSArnd Bergmann offset = da - scp->dma_addr; 47471ffb5a2STzung-Bi Shih if (offset >= 0 && (offset + len) <= scp->dram_size) 475903635cbSTzung-Bi Shih return scp->cpu_addr + offset; 47663c13d61SErin Lo } 47763c13d61SErin Lo 47863c13d61SErin Lo return NULL; 47963c13d61SErin Lo } 48063c13d61SErin Lo 481fd0b6c1fSPi-Hsun Shih static void mt8183_scp_stop(struct mtk_scp *scp) 482fd0b6c1fSPi-Hsun Shih { 483fd0b6c1fSPi-Hsun Shih /* Disable SCP watchdog */ 484fd0b6c1fSPi-Hsun Shih writel(0, scp->reg_base + MT8183_WDT_CFG); 485fd0b6c1fSPi-Hsun Shih } 486fd0b6c1fSPi-Hsun Shih 487fd0b6c1fSPi-Hsun Shih static void mt8192_scp_stop(struct mtk_scp *scp) 488fd0b6c1fSPi-Hsun Shih { 489fd0b6c1fSPi-Hsun Shih /* Disable SRAM clock */ 490fd0b6c1fSPi-Hsun Shih mt8192_power_off_sram(scp->reg_base + MT8192_L2TCM_SRAM_PD_0); 491fd0b6c1fSPi-Hsun Shih mt8192_power_off_sram(scp->reg_base + MT8192_L2TCM_SRAM_PD_1); 492fd0b6c1fSPi-Hsun Shih mt8192_power_off_sram(scp->reg_base + MT8192_L2TCM_SRAM_PD_2); 493fd0b6c1fSPi-Hsun Shih mt8192_power_off_sram(scp->reg_base + MT8192_L1TCM_SRAM_PDN); 494fd0b6c1fSPi-Hsun Shih mt8192_power_off_sram(scp->reg_base + MT8192_CPU0_SRAM_PD); 495fd0b6c1fSPi-Hsun Shih 496fd0b6c1fSPi-Hsun Shih /* Disable SCP watchdog */ 497fd0b6c1fSPi-Hsun Shih writel(0, scp->reg_base + MT8192_CORE0_WDT_CFG); 498fd0b6c1fSPi-Hsun Shih } 499fd0b6c1fSPi-Hsun Shih 50063c13d61SErin Lo static int scp_stop(struct rproc *rproc) 50163c13d61SErin Lo { 50263c13d61SErin Lo struct mtk_scp *scp = (struct mtk_scp *)rproc->priv; 50363c13d61SErin Lo int ret; 50463c13d61SErin Lo 50563c13d61SErin Lo ret = clk_prepare_enable(scp->clk); 50663c13d61SErin Lo if (ret) { 50763c13d61SErin Lo dev_err(scp->dev, "failed to enable clocks\n"); 50863c13d61SErin Lo return ret; 50963c13d61SErin Lo } 51063c13d61SErin Lo 511fd0b6c1fSPi-Hsun Shih scp->data->scp_reset_assert(scp); 512fd0b6c1fSPi-Hsun Shih scp->data->scp_stop(scp); 51363c13d61SErin Lo clk_disable_unprepare(scp->clk); 51463c13d61SErin Lo 51563c13d61SErin Lo return 0; 51663c13d61SErin Lo } 51763c13d61SErin Lo 51863c13d61SErin Lo static const struct rproc_ops scp_ops = { 51963c13d61SErin Lo .start = scp_start, 52063c13d61SErin Lo .stop = scp_stop, 52163c13d61SErin Lo .load = scp_load, 52263c13d61SErin Lo .da_to_va = scp_da_to_va, 5233efa0ea7STzung-Bi Shih .parse_fw = scp_parse_fw, 52463c13d61SErin Lo }; 52563c13d61SErin Lo 52663c13d61SErin Lo /** 52763c13d61SErin Lo * scp_get_device() - get device struct of SCP 52863c13d61SErin Lo * 52963c13d61SErin Lo * @scp: mtk_scp structure 53063c13d61SErin Lo **/ 53163c13d61SErin Lo struct device *scp_get_device(struct mtk_scp *scp) 53263c13d61SErin Lo { 53363c13d61SErin Lo return scp->dev; 53463c13d61SErin Lo } 53563c13d61SErin Lo EXPORT_SYMBOL_GPL(scp_get_device); 53663c13d61SErin Lo 53763c13d61SErin Lo /** 53863c13d61SErin Lo * scp_get_rproc() - get rproc struct of SCP 53963c13d61SErin Lo * 54063c13d61SErin Lo * @scp: mtk_scp structure 54163c13d61SErin Lo **/ 54263c13d61SErin Lo struct rproc *scp_get_rproc(struct mtk_scp *scp) 54363c13d61SErin Lo { 54463c13d61SErin Lo return scp->rproc; 54563c13d61SErin Lo } 54663c13d61SErin Lo EXPORT_SYMBOL_GPL(scp_get_rproc); 54763c13d61SErin Lo 54863c13d61SErin Lo /** 54963c13d61SErin Lo * scp_get_vdec_hw_capa() - get video decoder hardware capability 55063c13d61SErin Lo * 55163c13d61SErin Lo * @scp: mtk_scp structure 55263c13d61SErin Lo * 55363c13d61SErin Lo * Return: video decoder hardware capability 55463c13d61SErin Lo **/ 55563c13d61SErin Lo unsigned int scp_get_vdec_hw_capa(struct mtk_scp *scp) 55663c13d61SErin Lo { 55763c13d61SErin Lo return scp->run.dec_capability; 55863c13d61SErin Lo } 55963c13d61SErin Lo EXPORT_SYMBOL_GPL(scp_get_vdec_hw_capa); 56063c13d61SErin Lo 56163c13d61SErin Lo /** 56263c13d61SErin Lo * scp_get_venc_hw_capa() - get video encoder hardware capability 56363c13d61SErin Lo * 56463c13d61SErin Lo * @scp: mtk_scp structure 56563c13d61SErin Lo * 56663c13d61SErin Lo * Return: video encoder hardware capability 56763c13d61SErin Lo **/ 56863c13d61SErin Lo unsigned int scp_get_venc_hw_capa(struct mtk_scp *scp) 56963c13d61SErin Lo { 57063c13d61SErin Lo return scp->run.enc_capability; 57163c13d61SErin Lo } 57263c13d61SErin Lo EXPORT_SYMBOL_GPL(scp_get_venc_hw_capa); 57363c13d61SErin Lo 57463c13d61SErin Lo /** 57563c13d61SErin Lo * scp_mapping_dm_addr() - Mapping SRAM/DRAM to kernel virtual address 57663c13d61SErin Lo * 57763c13d61SErin Lo * @scp: mtk_scp structure 57863c13d61SErin Lo * @mem_addr: SCP views memory address 57963c13d61SErin Lo * 58063c13d61SErin Lo * Mapping the SCP's SRAM address / 58163c13d61SErin Lo * DMEM (Data Extended Memory) memory address / 58263c13d61SErin Lo * Working buffer memory address to 58363c13d61SErin Lo * kernel virtual address. 58463c13d61SErin Lo * 58563c13d61SErin Lo * Return: Return ERR_PTR(-EINVAL) if mapping failed, 58663c13d61SErin Lo * otherwise the mapped kernel virtual address 58763c13d61SErin Lo **/ 58863c13d61SErin Lo void *scp_mapping_dm_addr(struct mtk_scp *scp, u32 mem_addr) 58963c13d61SErin Lo { 59063c13d61SErin Lo void *ptr; 59163c13d61SErin Lo 59263c13d61SErin Lo ptr = scp_da_to_va(scp->rproc, mem_addr, 0); 59363c13d61SErin Lo if (!ptr) 59463c13d61SErin Lo return ERR_PTR(-EINVAL); 59563c13d61SErin Lo 59663c13d61SErin Lo return ptr; 59763c13d61SErin Lo } 59863c13d61SErin Lo EXPORT_SYMBOL_GPL(scp_mapping_dm_addr); 59963c13d61SErin Lo 60063c13d61SErin Lo static int scp_map_memory_region(struct mtk_scp *scp) 60163c13d61SErin Lo { 60263c13d61SErin Lo int ret; 60363c13d61SErin Lo 60463c13d61SErin Lo ret = of_reserved_mem_device_init(scp->dev); 605fd0b6c1fSPi-Hsun Shih 606fd0b6c1fSPi-Hsun Shih /* reserved memory is optional. */ 607fd0b6c1fSPi-Hsun Shih if (ret == -ENODEV) { 608fd0b6c1fSPi-Hsun Shih dev_info(scp->dev, "skipping reserved memory initialization."); 609fd0b6c1fSPi-Hsun Shih return 0; 610fd0b6c1fSPi-Hsun Shih } 611fd0b6c1fSPi-Hsun Shih 61263c13d61SErin Lo if (ret) { 61363c13d61SErin Lo dev_err(scp->dev, "failed to assign memory-region: %d\n", ret); 61463c13d61SErin Lo return -ENOMEM; 61563c13d61SErin Lo } 61663c13d61SErin Lo 61763c13d61SErin Lo /* Reserved SCP code size */ 61863c13d61SErin Lo scp->dram_size = MAX_CODE_SIZE; 61963c13d61SErin Lo scp->cpu_addr = dma_alloc_coherent(scp->dev, scp->dram_size, 620c2781e4dSArnd Bergmann &scp->dma_addr, GFP_KERNEL); 62163c13d61SErin Lo if (!scp->cpu_addr) 62263c13d61SErin Lo return -ENOMEM; 62363c13d61SErin Lo 62463c13d61SErin Lo return 0; 62563c13d61SErin Lo } 62663c13d61SErin Lo 62763c13d61SErin Lo static void scp_unmap_memory_region(struct mtk_scp *scp) 62863c13d61SErin Lo { 629fd0b6c1fSPi-Hsun Shih if (scp->dram_size == 0) 630fd0b6c1fSPi-Hsun Shih return; 631fd0b6c1fSPi-Hsun Shih 63263c13d61SErin Lo dma_free_coherent(scp->dev, scp->dram_size, scp->cpu_addr, 633c2781e4dSArnd Bergmann scp->dma_addr); 63463c13d61SErin Lo of_reserved_mem_device_release(scp->dev); 63563c13d61SErin Lo } 63663c13d61SErin Lo 63770179969SPi-Hsun Shih static int scp_register_ipi(struct platform_device *pdev, u32 id, 63870179969SPi-Hsun Shih ipi_handler_t handler, void *priv) 63970179969SPi-Hsun Shih { 64070179969SPi-Hsun Shih struct mtk_scp *scp = platform_get_drvdata(pdev); 64170179969SPi-Hsun Shih 64270179969SPi-Hsun Shih return scp_ipi_register(scp, id, handler, priv); 64370179969SPi-Hsun Shih } 64470179969SPi-Hsun Shih 64570179969SPi-Hsun Shih static void scp_unregister_ipi(struct platform_device *pdev, u32 id) 64670179969SPi-Hsun Shih { 64770179969SPi-Hsun Shih struct mtk_scp *scp = platform_get_drvdata(pdev); 64870179969SPi-Hsun Shih 64970179969SPi-Hsun Shih scp_ipi_unregister(scp, id); 65070179969SPi-Hsun Shih } 65170179969SPi-Hsun Shih 65270179969SPi-Hsun Shih static int scp_send_ipi(struct platform_device *pdev, u32 id, void *buf, 65370179969SPi-Hsun Shih unsigned int len, unsigned int wait) 65470179969SPi-Hsun Shih { 65570179969SPi-Hsun Shih struct mtk_scp *scp = platform_get_drvdata(pdev); 65670179969SPi-Hsun Shih 65770179969SPi-Hsun Shih return scp_ipi_send(scp, id, buf, len, wait); 65870179969SPi-Hsun Shih } 65970179969SPi-Hsun Shih 66070179969SPi-Hsun Shih static struct mtk_rpmsg_info mtk_scp_rpmsg_info = { 66170179969SPi-Hsun Shih .send_ipi = scp_send_ipi, 66270179969SPi-Hsun Shih .register_ipi = scp_register_ipi, 66370179969SPi-Hsun Shih .unregister_ipi = scp_unregister_ipi, 66470179969SPi-Hsun Shih .ns_ipi_id = SCP_IPI_NS_SERVICE, 66570179969SPi-Hsun Shih }; 66670179969SPi-Hsun Shih 66770179969SPi-Hsun Shih static void scp_add_rpmsg_subdev(struct mtk_scp *scp) 66870179969SPi-Hsun Shih { 66970179969SPi-Hsun Shih scp->rpmsg_subdev = 67070179969SPi-Hsun Shih mtk_rpmsg_create_rproc_subdev(to_platform_device(scp->dev), 67170179969SPi-Hsun Shih &mtk_scp_rpmsg_info); 67270179969SPi-Hsun Shih if (scp->rpmsg_subdev) 67370179969SPi-Hsun Shih rproc_add_subdev(scp->rproc, scp->rpmsg_subdev); 67470179969SPi-Hsun Shih } 67570179969SPi-Hsun Shih 67670179969SPi-Hsun Shih static void scp_remove_rpmsg_subdev(struct mtk_scp *scp) 67770179969SPi-Hsun Shih { 67870179969SPi-Hsun Shih if (scp->rpmsg_subdev) { 67970179969SPi-Hsun Shih rproc_remove_subdev(scp->rproc, scp->rpmsg_subdev); 68070179969SPi-Hsun Shih mtk_rpmsg_destroy_rproc_subdev(scp->rpmsg_subdev); 68170179969SPi-Hsun Shih scp->rpmsg_subdev = NULL; 68270179969SPi-Hsun Shih } 68370179969SPi-Hsun Shih } 68470179969SPi-Hsun Shih 68563c13d61SErin Lo static int scp_probe(struct platform_device *pdev) 68663c13d61SErin Lo { 68763c13d61SErin Lo struct device *dev = &pdev->dev; 68863c13d61SErin Lo struct device_node *np = dev->of_node; 68963c13d61SErin Lo struct mtk_scp *scp; 69063c13d61SErin Lo struct rproc *rproc; 69163c13d61SErin Lo struct resource *res; 69263c13d61SErin Lo char *fw_name = "scp.img"; 69363c13d61SErin Lo int ret, i; 69463c13d61SErin Lo 69563c13d61SErin Lo rproc = rproc_alloc(dev, 69663c13d61SErin Lo np->name, 69763c13d61SErin Lo &scp_ops, 69863c13d61SErin Lo fw_name, 69963c13d61SErin Lo sizeof(*scp)); 70063c13d61SErin Lo if (!rproc) { 70163c13d61SErin Lo dev_err(dev, "unable to allocate remoteproc\n"); 70263c13d61SErin Lo return -ENOMEM; 70363c13d61SErin Lo } 70463c13d61SErin Lo 70563c13d61SErin Lo scp = (struct mtk_scp *)rproc->priv; 70663c13d61SErin Lo scp->rproc = rproc; 70763c13d61SErin Lo scp->dev = dev; 708fd0b6c1fSPi-Hsun Shih scp->data = of_device_get_match_data(dev); 70963c13d61SErin Lo platform_set_drvdata(pdev, scp); 71063c13d61SErin Lo 71163c13d61SErin Lo res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sram"); 71263c13d61SErin Lo scp->sram_base = devm_ioremap_resource(dev, res); 71363c13d61SErin Lo if (IS_ERR((__force void *)scp->sram_base)) { 71463c13d61SErin Lo dev_err(dev, "Failed to parse and map sram memory\n"); 71563c13d61SErin Lo ret = PTR_ERR((__force void *)scp->sram_base); 71663c13d61SErin Lo goto free_rproc; 71763c13d61SErin Lo } 71863c13d61SErin Lo scp->sram_size = resource_size(res); 71963c13d61SErin Lo 72063c13d61SErin Lo mutex_init(&scp->send_lock); 72163c13d61SErin Lo for (i = 0; i < SCP_IPI_MAX; i++) 72263c13d61SErin Lo mutex_init(&scp->ipi_desc[i].lock); 72363c13d61SErin Lo 72463c13d61SErin Lo res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg"); 72563c13d61SErin Lo scp->reg_base = devm_ioremap_resource(dev, res); 72663c13d61SErin Lo if (IS_ERR((__force void *)scp->reg_base)) { 72763c13d61SErin Lo dev_err(dev, "Failed to parse and map cfg memory\n"); 72863c13d61SErin Lo ret = PTR_ERR((__force void *)scp->reg_base); 72963c13d61SErin Lo goto destroy_mutex; 73063c13d61SErin Lo } 73163c13d61SErin Lo 73263c13d61SErin Lo ret = scp_map_memory_region(scp); 73363c13d61SErin Lo if (ret) 73463c13d61SErin Lo goto destroy_mutex; 73563c13d61SErin Lo 73663c13d61SErin Lo scp->clk = devm_clk_get(dev, "main"); 73763c13d61SErin Lo if (IS_ERR(scp->clk)) { 73863c13d61SErin Lo dev_err(dev, "Failed to get clock\n"); 73963c13d61SErin Lo ret = PTR_ERR(scp->clk); 74063c13d61SErin Lo goto release_dev_mem; 74163c13d61SErin Lo } 74263c13d61SErin Lo 74363c13d61SErin Lo /* register SCP initialization IPI */ 74463c13d61SErin Lo ret = scp_ipi_register(scp, SCP_IPI_INIT, scp_init_ipi_handler, scp); 74563c13d61SErin Lo if (ret) { 74663c13d61SErin Lo dev_err(dev, "Failed to register IPI_SCP_INIT\n"); 74763c13d61SErin Lo goto release_dev_mem; 74863c13d61SErin Lo } 74963c13d61SErin Lo 75063c13d61SErin Lo init_waitqueue_head(&scp->run.wq); 75163c13d61SErin Lo init_waitqueue_head(&scp->ack_wq); 75263c13d61SErin Lo 75370179969SPi-Hsun Shih scp_add_rpmsg_subdev(scp); 75470179969SPi-Hsun Shih 75563c13d61SErin Lo ret = devm_request_threaded_irq(dev, platform_get_irq(pdev, 0), NULL, 75663c13d61SErin Lo scp_irq_handler, IRQF_ONESHOT, 75763c13d61SErin Lo pdev->name, scp); 75863c13d61SErin Lo 75963c13d61SErin Lo if (ret) { 76063c13d61SErin Lo dev_err(dev, "failed to request irq\n"); 76170179969SPi-Hsun Shih goto remove_subdev; 76263c13d61SErin Lo } 76363c13d61SErin Lo 76463c13d61SErin Lo ret = rproc_add(rproc); 76563c13d61SErin Lo if (ret) 76670179969SPi-Hsun Shih goto remove_subdev; 76763c13d61SErin Lo 76870179969SPi-Hsun Shih return 0; 76963c13d61SErin Lo 77070179969SPi-Hsun Shih remove_subdev: 77170179969SPi-Hsun Shih scp_remove_rpmsg_subdev(scp); 77263c13d61SErin Lo scp_ipi_unregister(scp, SCP_IPI_INIT); 77363c13d61SErin Lo release_dev_mem: 77463c13d61SErin Lo scp_unmap_memory_region(scp); 77563c13d61SErin Lo destroy_mutex: 77663c13d61SErin Lo for (i = 0; i < SCP_IPI_MAX; i++) 77763c13d61SErin Lo mutex_destroy(&scp->ipi_desc[i].lock); 77863c13d61SErin Lo mutex_destroy(&scp->send_lock); 77963c13d61SErin Lo free_rproc: 78063c13d61SErin Lo rproc_free(rproc); 78163c13d61SErin Lo 78263c13d61SErin Lo return ret; 78363c13d61SErin Lo } 78463c13d61SErin Lo 78563c13d61SErin Lo static int scp_remove(struct platform_device *pdev) 78663c13d61SErin Lo { 78763c13d61SErin Lo struct mtk_scp *scp = platform_get_drvdata(pdev); 78863c13d61SErin Lo int i; 78963c13d61SErin Lo 79063c13d61SErin Lo rproc_del(scp->rproc); 79170179969SPi-Hsun Shih scp_remove_rpmsg_subdev(scp); 79263c13d61SErin Lo scp_ipi_unregister(scp, SCP_IPI_INIT); 79363c13d61SErin Lo scp_unmap_memory_region(scp); 79463c13d61SErin Lo for (i = 0; i < SCP_IPI_MAX; i++) 79563c13d61SErin Lo mutex_destroy(&scp->ipi_desc[i].lock); 79663c13d61SErin Lo mutex_destroy(&scp->send_lock); 79763c13d61SErin Lo rproc_free(scp->rproc); 79863c13d61SErin Lo 79963c13d61SErin Lo return 0; 80063c13d61SErin Lo } 80163c13d61SErin Lo 802fd0b6c1fSPi-Hsun Shih static const struct mtk_scp_of_data mt8183_of_data = { 803fd0b6c1fSPi-Hsun Shih .scp_before_load = mt8183_scp_before_load, 804fd0b6c1fSPi-Hsun Shih .scp_irq_handler = mt8183_scp_irq_handler, 805fd0b6c1fSPi-Hsun Shih .scp_reset_assert = mt8183_scp_reset_assert, 806fd0b6c1fSPi-Hsun Shih .scp_reset_deassert = mt8183_scp_reset_deassert, 807fd0b6c1fSPi-Hsun Shih .scp_stop = mt8183_scp_stop, 808fd0b6c1fSPi-Hsun Shih .host_to_scp_reg = MT8183_HOST_TO_SCP, 809fd0b6c1fSPi-Hsun Shih .host_to_scp_int_bit = MT8183_HOST_IPC_INT_BIT, 8103efa0ea7STzung-Bi Shih .ipi_buf_offset = 0x7bdb0, 811fd0b6c1fSPi-Hsun Shih }; 812fd0b6c1fSPi-Hsun Shih 813fd0b6c1fSPi-Hsun Shih static const struct mtk_scp_of_data mt8192_of_data = { 814fd0b6c1fSPi-Hsun Shih .scp_before_load = mt8192_scp_before_load, 815fd0b6c1fSPi-Hsun Shih .scp_irq_handler = mt8192_scp_irq_handler, 816fd0b6c1fSPi-Hsun Shih .scp_reset_assert = mt8192_scp_reset_assert, 817fd0b6c1fSPi-Hsun Shih .scp_reset_deassert = mt8192_scp_reset_deassert, 818fd0b6c1fSPi-Hsun Shih .scp_stop = mt8192_scp_stop, 819fd0b6c1fSPi-Hsun Shih .host_to_scp_reg = MT8192_GIPC_IN_SET, 820fd0b6c1fSPi-Hsun Shih .host_to_scp_int_bit = MT8192_HOST_IPC_INT_BIT, 821fd0b6c1fSPi-Hsun Shih }; 822fd0b6c1fSPi-Hsun Shih 82363c13d61SErin Lo static const struct of_device_id mtk_scp_of_match[] = { 824fd0b6c1fSPi-Hsun Shih { .compatible = "mediatek,mt8183-scp", .data = &mt8183_of_data }, 825fd0b6c1fSPi-Hsun Shih { .compatible = "mediatek,mt8192-scp", .data = &mt8192_of_data }, 82663c13d61SErin Lo {}, 82763c13d61SErin Lo }; 82863c13d61SErin Lo MODULE_DEVICE_TABLE(of, mtk_scp_of_match); 82963c13d61SErin Lo 83063c13d61SErin Lo static struct platform_driver mtk_scp_driver = { 83163c13d61SErin Lo .probe = scp_probe, 83263c13d61SErin Lo .remove = scp_remove, 83363c13d61SErin Lo .driver = { 83463c13d61SErin Lo .name = "mtk-scp", 835cca21000SSouptick Joarder .of_match_table = mtk_scp_of_match, 83663c13d61SErin Lo }, 83763c13d61SErin Lo }; 83863c13d61SErin Lo 83963c13d61SErin Lo module_platform_driver(mtk_scp_driver); 84063c13d61SErin Lo 84163c13d61SErin Lo MODULE_LICENSE("GPL v2"); 84263c13d61SErin Lo MODULE_DESCRIPTION("MediaTek SCP control driver"); 843