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 2008c545f52STzung-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); 2098c545f52STzung-Bi Shih } else { 2108c545f52STzung-Bi Shih scp_wdt_handler(scp, scp_to_host); 2118c545f52STzung-Bi Shih writel(1, scp->reg_base + MT8192_CORE0_WDT_IRQ); 2128c545f52STzung-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 */ 27540df0a91SPeng Fan ptr = (void __iomem *)rproc_da_to_va(rproc, da, memsz, NULL); 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 31579111df4STinghan Shen static int mt8183_scp_clk_get(struct mtk_scp *scp) 31679111df4STinghan Shen { 31779111df4STinghan Shen struct device *dev = scp->dev; 31879111df4STinghan Shen int ret = 0; 31979111df4STinghan Shen 32079111df4STinghan Shen scp->clk = devm_clk_get(dev, "main"); 32179111df4STinghan Shen if (IS_ERR(scp->clk)) { 32279111df4STinghan Shen dev_err(dev, "Failed to get clock\n"); 32379111df4STinghan Shen ret = PTR_ERR(scp->clk); 32479111df4STinghan Shen } 32579111df4STinghan Shen 32679111df4STinghan Shen return ret; 32779111df4STinghan Shen } 32879111df4STinghan Shen 32979111df4STinghan Shen static int mt8192_scp_clk_get(struct mtk_scp *scp) 33079111df4STinghan Shen { 33179111df4STinghan Shen return mt8183_scp_clk_get(scp); 33279111df4STinghan Shen } 33379111df4STinghan Shen 33479111df4STinghan Shen static int mt8195_scp_clk_get(struct mtk_scp *scp) 33579111df4STinghan Shen { 33679111df4STinghan Shen scp->clk = NULL; 33779111df4STinghan Shen 33879111df4STinghan Shen return 0; 33979111df4STinghan Shen } 34079111df4STinghan Shen 341fd0b6c1fSPi-Hsun Shih static int mt8183_scp_before_load(struct mtk_scp *scp) 34263c13d61SErin Lo { 343fd0b6c1fSPi-Hsun Shih /* Clear SCP to host interrupt */ 344fd0b6c1fSPi-Hsun Shih writel(MT8183_SCP_IPC_INT_BIT, scp->reg_base + MT8183_SCP_TO_HOST); 34563c13d61SErin Lo 34663c13d61SErin Lo /* Reset clocks before loading FW */ 34763c13d61SErin Lo writel(0x0, scp->reg_base + MT8183_SCP_CLK_SW_SEL); 34863c13d61SErin Lo writel(0x0, scp->reg_base + MT8183_SCP_CLK_DIV_SEL); 34963c13d61SErin Lo 35063c13d61SErin Lo /* Initialize TCM before loading FW. */ 35163c13d61SErin Lo writel(0x0, scp->reg_base + MT8183_SCP_L1_SRAM_PD); 35263c13d61SErin Lo writel(0x0, scp->reg_base + MT8183_SCP_TCM_TAIL_SRAM_PD); 35363c13d61SErin Lo 35463c13d61SErin Lo /* Turn on the power of SCP's SRAM before using it. */ 35563c13d61SErin Lo writel(0x0, scp->reg_base + MT8183_SCP_SRAM_PDN); 35663c13d61SErin Lo 35763c13d61SErin Lo /* 35863c13d61SErin Lo * Set I-cache and D-cache size before loading SCP FW. 35963c13d61SErin Lo * SCP SRAM logical address may change when cache size setting differs. 36063c13d61SErin Lo */ 36163c13d61SErin Lo writel(MT8183_SCP_CACHE_CON_WAYEN | MT8183_SCP_CACHESIZE_8KB, 36263c13d61SErin Lo scp->reg_base + MT8183_SCP_CACHE_CON); 36363c13d61SErin Lo writel(MT8183_SCP_CACHESIZE_8KB, scp->reg_base + MT8183_SCP_DCACHE_CON); 36463c13d61SErin Lo 365fd0b6c1fSPi-Hsun Shih return 0; 366fd0b6c1fSPi-Hsun Shih } 367fd0b6c1fSPi-Hsun Shih 368f20e232dSTinghan Shen static void scp_sram_power_on(void __iomem *addr, u32 reserved_mask) 369fd0b6c1fSPi-Hsun Shih { 370fd0b6c1fSPi-Hsun Shih int i; 371fd0b6c1fSPi-Hsun Shih 372fd0b6c1fSPi-Hsun Shih for (i = 31; i >= 0; i--) 373f20e232dSTinghan Shen writel(GENMASK(i, 0) & ~reserved_mask, addr); 374fd0b6c1fSPi-Hsun Shih writel(0, addr); 375fd0b6c1fSPi-Hsun Shih } 376fd0b6c1fSPi-Hsun Shih 377f20e232dSTinghan Shen static void scp_sram_power_off(void __iomem *addr, u32 reserved_mask) 378fd0b6c1fSPi-Hsun Shih { 379fd0b6c1fSPi-Hsun Shih int i; 380fd0b6c1fSPi-Hsun Shih 381fd0b6c1fSPi-Hsun Shih writel(0, addr); 382fd0b6c1fSPi-Hsun Shih for (i = 0; i < 32; i++) 383f20e232dSTinghan Shen writel(GENMASK(i, 0) & ~reserved_mask, addr); 384fd0b6c1fSPi-Hsun Shih } 385fd0b6c1fSPi-Hsun Shih 38680d69185SAllen-KH Cheng static int mt8186_scp_before_load(struct mtk_scp *scp) 38780d69185SAllen-KH Cheng { 38880d69185SAllen-KH Cheng /* Clear SCP to host interrupt */ 38980d69185SAllen-KH Cheng writel(MT8183_SCP_IPC_INT_BIT, scp->reg_base + MT8183_SCP_TO_HOST); 39080d69185SAllen-KH Cheng 39180d69185SAllen-KH Cheng /* Reset clocks before loading FW */ 39280d69185SAllen-KH Cheng writel(0x0, scp->reg_base + MT8183_SCP_CLK_SW_SEL); 39380d69185SAllen-KH Cheng writel(0x0, scp->reg_base + MT8183_SCP_CLK_DIV_SEL); 39480d69185SAllen-KH Cheng 39580d69185SAllen-KH Cheng /* Turn on the power of SCP's SRAM before using it. Enable 1 block per time*/ 396f20e232dSTinghan Shen scp_sram_power_on(scp->reg_base + MT8183_SCP_SRAM_PDN, 0); 39780d69185SAllen-KH Cheng 39880d69185SAllen-KH Cheng /* Initialize TCM before loading FW. */ 39980d69185SAllen-KH Cheng writel(0x0, scp->reg_base + MT8183_SCP_L1_SRAM_PD); 40080d69185SAllen-KH Cheng writel(0x0, scp->reg_base + MT8183_SCP_TCM_TAIL_SRAM_PD); 40180d69185SAllen-KH Cheng writel(0x0, scp->reg_base + MT8186_SCP_L1_SRAM_PD_P1); 40280d69185SAllen-KH Cheng writel(0x0, scp->reg_base + MT8186_SCP_L1_SRAM_PD_p2); 40380d69185SAllen-KH Cheng 40454439d20SAllen-KH Cheng /* 40554439d20SAllen-KH Cheng * Set I-cache and D-cache size before loading SCP FW. 40654439d20SAllen-KH Cheng * SCP SRAM logical address may change when cache size setting differs. 40754439d20SAllen-KH Cheng */ 40854439d20SAllen-KH Cheng writel(MT8183_SCP_CACHE_CON_WAYEN | MT8183_SCP_CACHESIZE_8KB, 40954439d20SAllen-KH Cheng scp->reg_base + MT8183_SCP_CACHE_CON); 41054439d20SAllen-KH Cheng writel(MT8183_SCP_CACHESIZE_8KB, scp->reg_base + MT8183_SCP_DCACHE_CON); 41154439d20SAllen-KH Cheng 41280d69185SAllen-KH Cheng return 0; 41380d69185SAllen-KH Cheng } 41480d69185SAllen-KH Cheng 415fd0b6c1fSPi-Hsun Shih static int mt8192_scp_before_load(struct mtk_scp *scp) 416fd0b6c1fSPi-Hsun Shih { 417fd0b6c1fSPi-Hsun Shih /* clear SPM interrupt, SCP2SPM_IPC_CLR */ 418fd0b6c1fSPi-Hsun Shih writel(0xff, scp->reg_base + MT8192_SCP2SPM_IPC_CLR); 419fd0b6c1fSPi-Hsun Shih 420fd0b6c1fSPi-Hsun Shih writel(1, scp->reg_base + MT8192_CORE0_SW_RSTN_SET); 421fd0b6c1fSPi-Hsun Shih 422fd0b6c1fSPi-Hsun Shih /* enable SRAM clock */ 423f20e232dSTinghan Shen scp_sram_power_on(scp->reg_base + MT8192_L2TCM_SRAM_PD_0, 0); 424f20e232dSTinghan Shen scp_sram_power_on(scp->reg_base + MT8192_L2TCM_SRAM_PD_1, 0); 425f20e232dSTinghan Shen scp_sram_power_on(scp->reg_base + MT8192_L2TCM_SRAM_PD_2, 0); 426f20e232dSTinghan Shen scp_sram_power_on(scp->reg_base + MT8192_L1TCM_SRAM_PDN, 0); 427f20e232dSTinghan Shen scp_sram_power_on(scp->reg_base + MT8192_CPU0_SRAM_PD, 0); 428f20e232dSTinghan Shen 429f20e232dSTinghan Shen /* enable MPU for all memory regions */ 430f20e232dSTinghan Shen writel(0xff, scp->reg_base + MT8192_CORE0_MEM_ATT_PREDEF); 431f20e232dSTinghan Shen 432f20e232dSTinghan Shen return 0; 433f20e232dSTinghan Shen } 434f20e232dSTinghan Shen 435f20e232dSTinghan Shen static int mt8195_scp_before_load(struct mtk_scp *scp) 436f20e232dSTinghan Shen { 437f20e232dSTinghan Shen /* clear SPM interrupt, SCP2SPM_IPC_CLR */ 438f20e232dSTinghan Shen writel(0xff, scp->reg_base + MT8192_SCP2SPM_IPC_CLR); 439f20e232dSTinghan Shen 440f20e232dSTinghan Shen writel(1, scp->reg_base + MT8192_CORE0_SW_RSTN_SET); 441f20e232dSTinghan Shen 442f20e232dSTinghan Shen /* enable SRAM clock */ 443f20e232dSTinghan Shen scp_sram_power_on(scp->reg_base + MT8192_L2TCM_SRAM_PD_0, 0); 444f20e232dSTinghan Shen scp_sram_power_on(scp->reg_base + MT8192_L2TCM_SRAM_PD_1, 0); 445f20e232dSTinghan Shen scp_sram_power_on(scp->reg_base + MT8192_L2TCM_SRAM_PD_2, 0); 446f20e232dSTinghan Shen scp_sram_power_on(scp->reg_base + MT8192_L1TCM_SRAM_PDN, 447f20e232dSTinghan Shen MT8195_L1TCM_SRAM_PDN_RESERVED_RSI_BITS); 448f20e232dSTinghan Shen scp_sram_power_on(scp->reg_base + MT8192_CPU0_SRAM_PD, 0); 449fd0b6c1fSPi-Hsun Shih 450ff3ea536STzung-Bi Shih /* enable MPU for all memory regions */ 451ff3ea536STzung-Bi Shih writel(0xff, scp->reg_base + MT8192_CORE0_MEM_ATT_PREDEF); 452ff3ea536STzung-Bi Shih 453fd0b6c1fSPi-Hsun Shih return 0; 454fd0b6c1fSPi-Hsun Shih } 455fd0b6c1fSPi-Hsun Shih 456fd0b6c1fSPi-Hsun Shih static int scp_load(struct rproc *rproc, const struct firmware *fw) 457fd0b6c1fSPi-Hsun Shih { 458fd0b6c1fSPi-Hsun Shih struct mtk_scp *scp = rproc->priv; 459fd0b6c1fSPi-Hsun Shih struct device *dev = scp->dev; 460fd0b6c1fSPi-Hsun Shih int ret; 461fd0b6c1fSPi-Hsun Shih 462fd0b6c1fSPi-Hsun Shih ret = clk_prepare_enable(scp->clk); 463fd0b6c1fSPi-Hsun Shih if (ret) { 464fd0b6c1fSPi-Hsun Shih dev_err(dev, "failed to enable clocks\n"); 465fd0b6c1fSPi-Hsun Shih return ret; 466fd0b6c1fSPi-Hsun Shih } 467fd0b6c1fSPi-Hsun Shih 468fd0b6c1fSPi-Hsun Shih /* Hold SCP in reset while loading FW. */ 469fd0b6c1fSPi-Hsun Shih scp->data->scp_reset_assert(scp); 470fd0b6c1fSPi-Hsun Shih 471fd0b6c1fSPi-Hsun Shih ret = scp->data->scp_before_load(scp); 472fd0b6c1fSPi-Hsun Shih if (ret < 0) 47322c3df6fSTzung-Bi Shih goto leave; 474fd0b6c1fSPi-Hsun Shih 47563c13d61SErin Lo ret = scp_elf_load_segments(rproc, fw); 47622c3df6fSTzung-Bi Shih leave: 47763c13d61SErin Lo clk_disable_unprepare(scp->clk); 47863c13d61SErin Lo 47963c13d61SErin Lo return ret; 48063c13d61SErin Lo } 48163c13d61SErin Lo 4823efa0ea7STzung-Bi Shih static int scp_parse_fw(struct rproc *rproc, const struct firmware *fw) 4833efa0ea7STzung-Bi Shih { 4843efa0ea7STzung-Bi Shih struct mtk_scp *scp = rproc->priv; 4853efa0ea7STzung-Bi Shih struct device *dev = scp->dev; 4863efa0ea7STzung-Bi Shih int ret; 4873efa0ea7STzung-Bi Shih 4883efa0ea7STzung-Bi Shih ret = clk_prepare_enable(scp->clk); 4893efa0ea7STzung-Bi Shih if (ret) { 4903efa0ea7STzung-Bi Shih dev_err(dev, "failed to enable clocks\n"); 4913efa0ea7STzung-Bi Shih return ret; 4923efa0ea7STzung-Bi Shih } 4933efa0ea7STzung-Bi Shih 4943efa0ea7STzung-Bi Shih ret = scp_ipi_init(scp, fw); 4953efa0ea7STzung-Bi Shih clk_disable_unprepare(scp->clk); 4963efa0ea7STzung-Bi Shih return ret; 4973efa0ea7STzung-Bi Shih } 4983efa0ea7STzung-Bi Shih 49963c13d61SErin Lo static int scp_start(struct rproc *rproc) 50063c13d61SErin Lo { 50163c13d61SErin Lo struct mtk_scp *scp = (struct mtk_scp *)rproc->priv; 50263c13d61SErin Lo struct device *dev = scp->dev; 50363c13d61SErin Lo struct scp_run *run = &scp->run; 50463c13d61SErin Lo int ret; 50563c13d61SErin Lo 50663c13d61SErin Lo ret = clk_prepare_enable(scp->clk); 50763c13d61SErin Lo if (ret) { 50863c13d61SErin Lo dev_err(dev, "failed to enable clocks\n"); 50963c13d61SErin Lo return ret; 51063c13d61SErin Lo } 51163c13d61SErin Lo 51263c13d61SErin Lo run->signaled = false; 51363c13d61SErin Lo 514fd0b6c1fSPi-Hsun Shih scp->data->scp_reset_deassert(scp); 51563c13d61SErin Lo 51663c13d61SErin Lo ret = wait_event_interruptible_timeout( 51763c13d61SErin Lo run->wq, 51863c13d61SErin Lo run->signaled, 51963c13d61SErin Lo msecs_to_jiffies(2000)); 52063c13d61SErin Lo 52163c13d61SErin Lo if (ret == 0) { 52263c13d61SErin Lo dev_err(dev, "wait SCP initialization timeout!\n"); 52363c13d61SErin Lo ret = -ETIME; 52463c13d61SErin Lo goto stop; 52563c13d61SErin Lo } 52663c13d61SErin Lo if (ret == -ERESTARTSYS) { 52763c13d61SErin Lo dev_err(dev, "wait SCP interrupted by a signal!\n"); 52863c13d61SErin Lo goto stop; 52963c13d61SErin Lo } 530fd0b6c1fSPi-Hsun Shih 53163c13d61SErin Lo clk_disable_unprepare(scp->clk); 53263c13d61SErin Lo dev_info(dev, "SCP is ready. FW version %s\n", run->fw_ver); 53363c13d61SErin Lo 53463c13d61SErin Lo return 0; 53563c13d61SErin Lo 53663c13d61SErin Lo stop: 537fd0b6c1fSPi-Hsun Shih scp->data->scp_reset_assert(scp); 53863c13d61SErin Lo clk_disable_unprepare(scp->clk); 53963c13d61SErin Lo return ret; 54063c13d61SErin Lo } 54163c13d61SErin Lo 542ca23ecfdSTzung-Bi Shih static void *mt8183_scp_da_to_va(struct mtk_scp *scp, u64 da, size_t len) 54363c13d61SErin Lo { 54463c13d61SErin Lo int offset; 54563c13d61SErin Lo 54663c13d61SErin Lo if (da < scp->sram_size) { 54763c13d61SErin Lo offset = da; 54871ffb5a2STzung-Bi Shih if (offset >= 0 && (offset + len) <= scp->sram_size) 54963c13d61SErin Lo return (void __force *)scp->sram_base + offset; 550fd0b6c1fSPi-Hsun Shih } else if (scp->dram_size) { 551c2781e4dSArnd Bergmann offset = da - scp->dma_addr; 55271ffb5a2STzung-Bi Shih if (offset >= 0 && (offset + len) <= scp->dram_size) 553903635cbSTzung-Bi Shih return scp->cpu_addr + offset; 55463c13d61SErin Lo } 55563c13d61SErin Lo 55663c13d61SErin Lo return NULL; 55763c13d61SErin Lo } 55863c13d61SErin Lo 559ca23ecfdSTzung-Bi Shih static void *mt8192_scp_da_to_va(struct mtk_scp *scp, u64 da, size_t len) 560ca23ecfdSTzung-Bi Shih { 561ca23ecfdSTzung-Bi Shih int offset; 562ca23ecfdSTzung-Bi Shih 563ca23ecfdSTzung-Bi Shih if (da >= scp->sram_phys && 564ca23ecfdSTzung-Bi Shih (da + len) <= scp->sram_phys + scp->sram_size) { 565ca23ecfdSTzung-Bi Shih offset = da - scp->sram_phys; 566ca23ecfdSTzung-Bi Shih return (void __force *)scp->sram_base + offset; 567ca23ecfdSTzung-Bi Shih } 568ca23ecfdSTzung-Bi Shih 569ca23ecfdSTzung-Bi Shih /* optional memory region */ 570ca23ecfdSTzung-Bi Shih if (scp->l1tcm_size && 571ca23ecfdSTzung-Bi Shih da >= scp->l1tcm_phys && 572ca23ecfdSTzung-Bi Shih (da + len) <= scp->l1tcm_phys + scp->l1tcm_size) { 573ca23ecfdSTzung-Bi Shih offset = da - scp->l1tcm_phys; 574ca23ecfdSTzung-Bi Shih return (void __force *)scp->l1tcm_base + offset; 575ca23ecfdSTzung-Bi Shih } 576ca23ecfdSTzung-Bi Shih 577ca23ecfdSTzung-Bi Shih /* optional memory region */ 578ca23ecfdSTzung-Bi Shih if (scp->dram_size && 579ca23ecfdSTzung-Bi Shih da >= scp->dma_addr && 580ca23ecfdSTzung-Bi Shih (da + len) <= scp->dma_addr + scp->dram_size) { 581ca23ecfdSTzung-Bi Shih offset = da - scp->dma_addr; 582ca23ecfdSTzung-Bi Shih return scp->cpu_addr + offset; 583ca23ecfdSTzung-Bi Shih } 584ca23ecfdSTzung-Bi Shih 585ca23ecfdSTzung-Bi Shih return NULL; 586ca23ecfdSTzung-Bi Shih } 587ca23ecfdSTzung-Bi Shih 58840df0a91SPeng Fan static void *scp_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_iomem) 589ca23ecfdSTzung-Bi Shih { 590ca23ecfdSTzung-Bi Shih struct mtk_scp *scp = (struct mtk_scp *)rproc->priv; 591ca23ecfdSTzung-Bi Shih 592ca23ecfdSTzung-Bi Shih return scp->data->scp_da_to_va(scp, da, len); 593ca23ecfdSTzung-Bi Shih } 594ca23ecfdSTzung-Bi Shih 595fd0b6c1fSPi-Hsun Shih static void mt8183_scp_stop(struct mtk_scp *scp) 596fd0b6c1fSPi-Hsun Shih { 597fd0b6c1fSPi-Hsun Shih /* Disable SCP watchdog */ 598fd0b6c1fSPi-Hsun Shih writel(0, scp->reg_base + MT8183_WDT_CFG); 599fd0b6c1fSPi-Hsun Shih } 600fd0b6c1fSPi-Hsun Shih 601fd0b6c1fSPi-Hsun Shih static void mt8192_scp_stop(struct mtk_scp *scp) 602fd0b6c1fSPi-Hsun Shih { 603fd0b6c1fSPi-Hsun Shih /* Disable SRAM clock */ 604f20e232dSTinghan Shen scp_sram_power_off(scp->reg_base + MT8192_L2TCM_SRAM_PD_0, 0); 605f20e232dSTinghan Shen scp_sram_power_off(scp->reg_base + MT8192_L2TCM_SRAM_PD_1, 0); 606f20e232dSTinghan Shen scp_sram_power_off(scp->reg_base + MT8192_L2TCM_SRAM_PD_2, 0); 607f20e232dSTinghan Shen scp_sram_power_off(scp->reg_base + MT8192_L1TCM_SRAM_PDN, 0); 608f20e232dSTinghan Shen scp_sram_power_off(scp->reg_base + MT8192_CPU0_SRAM_PD, 0); 609f20e232dSTinghan Shen 610f20e232dSTinghan Shen /* Disable SCP watchdog */ 611f20e232dSTinghan Shen writel(0, scp->reg_base + MT8192_CORE0_WDT_CFG); 612f20e232dSTinghan Shen } 613f20e232dSTinghan Shen 614f20e232dSTinghan Shen static void mt8195_scp_stop(struct mtk_scp *scp) 615f20e232dSTinghan Shen { 616f20e232dSTinghan Shen /* Disable SRAM clock */ 617f20e232dSTinghan Shen scp_sram_power_off(scp->reg_base + MT8192_L2TCM_SRAM_PD_0, 0); 618f20e232dSTinghan Shen scp_sram_power_off(scp->reg_base + MT8192_L2TCM_SRAM_PD_1, 0); 619f20e232dSTinghan Shen scp_sram_power_off(scp->reg_base + MT8192_L2TCM_SRAM_PD_2, 0); 620f20e232dSTinghan Shen scp_sram_power_off(scp->reg_base + MT8192_L1TCM_SRAM_PDN, 621f20e232dSTinghan Shen MT8195_L1TCM_SRAM_PDN_RESERVED_RSI_BITS); 622f20e232dSTinghan Shen scp_sram_power_off(scp->reg_base + MT8192_CPU0_SRAM_PD, 0); 623fd0b6c1fSPi-Hsun Shih 624fd0b6c1fSPi-Hsun Shih /* Disable SCP watchdog */ 625fd0b6c1fSPi-Hsun Shih writel(0, scp->reg_base + MT8192_CORE0_WDT_CFG); 626fd0b6c1fSPi-Hsun Shih } 627fd0b6c1fSPi-Hsun Shih 62863c13d61SErin Lo static int scp_stop(struct rproc *rproc) 62963c13d61SErin Lo { 63063c13d61SErin Lo struct mtk_scp *scp = (struct mtk_scp *)rproc->priv; 63163c13d61SErin Lo int ret; 63263c13d61SErin Lo 63363c13d61SErin Lo ret = clk_prepare_enable(scp->clk); 63463c13d61SErin Lo if (ret) { 63563c13d61SErin Lo dev_err(scp->dev, "failed to enable clocks\n"); 63663c13d61SErin Lo return ret; 63763c13d61SErin Lo } 63863c13d61SErin Lo 639fd0b6c1fSPi-Hsun Shih scp->data->scp_reset_assert(scp); 640fd0b6c1fSPi-Hsun Shih scp->data->scp_stop(scp); 64163c13d61SErin Lo clk_disable_unprepare(scp->clk); 64263c13d61SErin Lo 64363c13d61SErin Lo return 0; 64463c13d61SErin Lo } 64563c13d61SErin Lo 64663c13d61SErin Lo static const struct rproc_ops scp_ops = { 64763c13d61SErin Lo .start = scp_start, 64863c13d61SErin Lo .stop = scp_stop, 64963c13d61SErin Lo .load = scp_load, 65063c13d61SErin Lo .da_to_va = scp_da_to_va, 6513efa0ea7STzung-Bi Shih .parse_fw = scp_parse_fw, 65263c13d61SErin Lo }; 65363c13d61SErin Lo 65463c13d61SErin Lo /** 65563c13d61SErin Lo * scp_get_device() - get device struct of SCP 65663c13d61SErin Lo * 65763c13d61SErin Lo * @scp: mtk_scp structure 65863c13d61SErin Lo **/ 65963c13d61SErin Lo struct device *scp_get_device(struct mtk_scp *scp) 66063c13d61SErin Lo { 66163c13d61SErin Lo return scp->dev; 66263c13d61SErin Lo } 66363c13d61SErin Lo EXPORT_SYMBOL_GPL(scp_get_device); 66463c13d61SErin Lo 66563c13d61SErin Lo /** 66663c13d61SErin Lo * scp_get_rproc() - get rproc struct of SCP 66763c13d61SErin Lo * 66863c13d61SErin Lo * @scp: mtk_scp structure 66963c13d61SErin Lo **/ 67063c13d61SErin Lo struct rproc *scp_get_rproc(struct mtk_scp *scp) 67163c13d61SErin Lo { 67263c13d61SErin Lo return scp->rproc; 67363c13d61SErin Lo } 67463c13d61SErin Lo EXPORT_SYMBOL_GPL(scp_get_rproc); 67563c13d61SErin Lo 67663c13d61SErin Lo /** 67763c13d61SErin Lo * scp_get_vdec_hw_capa() - get video decoder hardware capability 67863c13d61SErin Lo * 67963c13d61SErin Lo * @scp: mtk_scp structure 68063c13d61SErin Lo * 68163c13d61SErin Lo * Return: video decoder hardware capability 68263c13d61SErin Lo **/ 68363c13d61SErin Lo unsigned int scp_get_vdec_hw_capa(struct mtk_scp *scp) 68463c13d61SErin Lo { 68563c13d61SErin Lo return scp->run.dec_capability; 68663c13d61SErin Lo } 68763c13d61SErin Lo EXPORT_SYMBOL_GPL(scp_get_vdec_hw_capa); 68863c13d61SErin Lo 68963c13d61SErin Lo /** 69063c13d61SErin Lo * scp_get_venc_hw_capa() - get video encoder hardware capability 69163c13d61SErin Lo * 69263c13d61SErin Lo * @scp: mtk_scp structure 69363c13d61SErin Lo * 69463c13d61SErin Lo * Return: video encoder hardware capability 69563c13d61SErin Lo **/ 69663c13d61SErin Lo unsigned int scp_get_venc_hw_capa(struct mtk_scp *scp) 69763c13d61SErin Lo { 69863c13d61SErin Lo return scp->run.enc_capability; 69963c13d61SErin Lo } 70063c13d61SErin Lo EXPORT_SYMBOL_GPL(scp_get_venc_hw_capa); 70163c13d61SErin Lo 70263c13d61SErin Lo /** 70363c13d61SErin Lo * scp_mapping_dm_addr() - Mapping SRAM/DRAM to kernel virtual address 70463c13d61SErin Lo * 70563c13d61SErin Lo * @scp: mtk_scp structure 70663c13d61SErin Lo * @mem_addr: SCP views memory address 70763c13d61SErin Lo * 70863c13d61SErin Lo * Mapping the SCP's SRAM address / 70963c13d61SErin Lo * DMEM (Data Extended Memory) memory address / 71063c13d61SErin Lo * Working buffer memory address to 71163c13d61SErin Lo * kernel virtual address. 71263c13d61SErin Lo * 71363c13d61SErin Lo * Return: Return ERR_PTR(-EINVAL) if mapping failed, 71463c13d61SErin Lo * otherwise the mapped kernel virtual address 71563c13d61SErin Lo **/ 71663c13d61SErin Lo void *scp_mapping_dm_addr(struct mtk_scp *scp, u32 mem_addr) 71763c13d61SErin Lo { 71863c13d61SErin Lo void *ptr; 71963c13d61SErin Lo 72040df0a91SPeng Fan ptr = scp_da_to_va(scp->rproc, mem_addr, 0, NULL); 72163c13d61SErin Lo if (!ptr) 72263c13d61SErin Lo return ERR_PTR(-EINVAL); 72363c13d61SErin Lo 72463c13d61SErin Lo return ptr; 72563c13d61SErin Lo } 72663c13d61SErin Lo EXPORT_SYMBOL_GPL(scp_mapping_dm_addr); 72763c13d61SErin Lo 72863c13d61SErin Lo static int scp_map_memory_region(struct mtk_scp *scp) 72963c13d61SErin Lo { 73063c13d61SErin Lo int ret; 73163c13d61SErin Lo 73263c13d61SErin Lo ret = of_reserved_mem_device_init(scp->dev); 733fd0b6c1fSPi-Hsun Shih 734fd0b6c1fSPi-Hsun Shih /* reserved memory is optional. */ 735fd0b6c1fSPi-Hsun Shih if (ret == -ENODEV) { 736fd0b6c1fSPi-Hsun Shih dev_info(scp->dev, "skipping reserved memory initialization."); 737fd0b6c1fSPi-Hsun Shih return 0; 738fd0b6c1fSPi-Hsun Shih } 739fd0b6c1fSPi-Hsun Shih 74063c13d61SErin Lo if (ret) { 74163c13d61SErin Lo dev_err(scp->dev, "failed to assign memory-region: %d\n", ret); 74263c13d61SErin Lo return -ENOMEM; 74363c13d61SErin Lo } 74463c13d61SErin Lo 74563c13d61SErin Lo /* Reserved SCP code size */ 74663c13d61SErin Lo scp->dram_size = MAX_CODE_SIZE; 74763c13d61SErin Lo scp->cpu_addr = dma_alloc_coherent(scp->dev, scp->dram_size, 748c2781e4dSArnd Bergmann &scp->dma_addr, GFP_KERNEL); 74963c13d61SErin Lo if (!scp->cpu_addr) 75063c13d61SErin Lo return -ENOMEM; 75163c13d61SErin Lo 75263c13d61SErin Lo return 0; 75363c13d61SErin Lo } 75463c13d61SErin Lo 75563c13d61SErin Lo static void scp_unmap_memory_region(struct mtk_scp *scp) 75663c13d61SErin Lo { 757fd0b6c1fSPi-Hsun Shih if (scp->dram_size == 0) 758fd0b6c1fSPi-Hsun Shih return; 759fd0b6c1fSPi-Hsun Shih 76063c13d61SErin Lo dma_free_coherent(scp->dev, scp->dram_size, scp->cpu_addr, 761c2781e4dSArnd Bergmann scp->dma_addr); 76263c13d61SErin Lo of_reserved_mem_device_release(scp->dev); 76363c13d61SErin Lo } 76463c13d61SErin Lo 76570179969SPi-Hsun Shih static int scp_register_ipi(struct platform_device *pdev, u32 id, 76670179969SPi-Hsun Shih ipi_handler_t handler, void *priv) 76770179969SPi-Hsun Shih { 76870179969SPi-Hsun Shih struct mtk_scp *scp = platform_get_drvdata(pdev); 76970179969SPi-Hsun Shih 77070179969SPi-Hsun Shih return scp_ipi_register(scp, id, handler, priv); 77170179969SPi-Hsun Shih } 77270179969SPi-Hsun Shih 77370179969SPi-Hsun Shih static void scp_unregister_ipi(struct platform_device *pdev, u32 id) 77470179969SPi-Hsun Shih { 77570179969SPi-Hsun Shih struct mtk_scp *scp = platform_get_drvdata(pdev); 77670179969SPi-Hsun Shih 77770179969SPi-Hsun Shih scp_ipi_unregister(scp, id); 77870179969SPi-Hsun Shih } 77970179969SPi-Hsun Shih 78070179969SPi-Hsun Shih static int scp_send_ipi(struct platform_device *pdev, u32 id, void *buf, 78170179969SPi-Hsun Shih unsigned int len, unsigned int wait) 78270179969SPi-Hsun Shih { 78370179969SPi-Hsun Shih struct mtk_scp *scp = platform_get_drvdata(pdev); 78470179969SPi-Hsun Shih 78570179969SPi-Hsun Shih return scp_ipi_send(scp, id, buf, len, wait); 78670179969SPi-Hsun Shih } 78770179969SPi-Hsun Shih 78870179969SPi-Hsun Shih static struct mtk_rpmsg_info mtk_scp_rpmsg_info = { 78970179969SPi-Hsun Shih .send_ipi = scp_send_ipi, 79070179969SPi-Hsun Shih .register_ipi = scp_register_ipi, 79170179969SPi-Hsun Shih .unregister_ipi = scp_unregister_ipi, 79270179969SPi-Hsun Shih .ns_ipi_id = SCP_IPI_NS_SERVICE, 79370179969SPi-Hsun Shih }; 79470179969SPi-Hsun Shih 79570179969SPi-Hsun Shih static void scp_add_rpmsg_subdev(struct mtk_scp *scp) 79670179969SPi-Hsun Shih { 79770179969SPi-Hsun Shih scp->rpmsg_subdev = 79870179969SPi-Hsun Shih mtk_rpmsg_create_rproc_subdev(to_platform_device(scp->dev), 79970179969SPi-Hsun Shih &mtk_scp_rpmsg_info); 80070179969SPi-Hsun Shih if (scp->rpmsg_subdev) 80170179969SPi-Hsun Shih rproc_add_subdev(scp->rproc, scp->rpmsg_subdev); 80270179969SPi-Hsun Shih } 80370179969SPi-Hsun Shih 80470179969SPi-Hsun Shih static void scp_remove_rpmsg_subdev(struct mtk_scp *scp) 80570179969SPi-Hsun Shih { 80670179969SPi-Hsun Shih if (scp->rpmsg_subdev) { 80770179969SPi-Hsun Shih rproc_remove_subdev(scp->rproc, scp->rpmsg_subdev); 80870179969SPi-Hsun Shih mtk_rpmsg_destroy_rproc_subdev(scp->rpmsg_subdev); 80970179969SPi-Hsun Shih scp->rpmsg_subdev = NULL; 81070179969SPi-Hsun Shih } 81170179969SPi-Hsun Shih } 81270179969SPi-Hsun Shih 81363c13d61SErin Lo static int scp_probe(struct platform_device *pdev) 81463c13d61SErin Lo { 81563c13d61SErin Lo struct device *dev = &pdev->dev; 81663c13d61SErin Lo struct device_node *np = dev->of_node; 81763c13d61SErin Lo struct mtk_scp *scp; 81863c13d61SErin Lo struct rproc *rproc; 81963c13d61SErin Lo struct resource *res; 8201552de67SAllen-KH Cheng const char *fw_name = "scp.img"; 82163c13d61SErin Lo int ret, i; 82263c13d61SErin Lo 8231552de67SAllen-KH Cheng ret = rproc_of_parse_firmware(dev, 0, &fw_name); 8241552de67SAllen-KH Cheng if (ret < 0 && ret != -EINVAL) 8251552de67SAllen-KH Cheng return ret; 8261552de67SAllen-KH Cheng 827c1407ac1SAngeloGioacchino Del Regno rproc = devm_rproc_alloc(dev, np->name, &scp_ops, fw_name, sizeof(*scp)); 828ae6062c4SAngeloGioacchino Del Regno if (!rproc) 829ae6062c4SAngeloGioacchino Del Regno return dev_err_probe(dev, -ENOMEM, "unable to allocate remoteproc\n"); 83063c13d61SErin Lo 83163c13d61SErin Lo scp = (struct mtk_scp *)rproc->priv; 83263c13d61SErin Lo scp->rproc = rproc; 83363c13d61SErin Lo scp->dev = dev; 834fd0b6c1fSPi-Hsun Shih scp->data = of_device_get_match_data(dev); 83563c13d61SErin Lo platform_set_drvdata(pdev, scp); 83663c13d61SErin Lo 83763c13d61SErin Lo res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sram"); 83863c13d61SErin Lo scp->sram_base = devm_ioremap_resource(dev, res); 839ae6062c4SAngeloGioacchino Del Regno if (IS_ERR(scp->sram_base)) 840ae6062c4SAngeloGioacchino Del Regno return dev_err_probe(dev, PTR_ERR(scp->sram_base), 841ae6062c4SAngeloGioacchino Del Regno "Failed to parse and map sram memory\n"); 842ae6062c4SAngeloGioacchino Del Regno 84363c13d61SErin Lo scp->sram_size = resource_size(res); 844ca23ecfdSTzung-Bi Shih scp->sram_phys = res->start; 845ca23ecfdSTzung-Bi Shih 846ca23ecfdSTzung-Bi Shih /* l1tcm is an optional memory region */ 847ca23ecfdSTzung-Bi Shih res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "l1tcm"); 848ca23ecfdSTzung-Bi Shih scp->l1tcm_base = devm_ioremap_resource(dev, res); 849ae6062c4SAngeloGioacchino Del Regno if (IS_ERR(scp->l1tcm_base)) { 850ae6062c4SAngeloGioacchino Del Regno ret = PTR_ERR(scp->l1tcm_base); 851ca23ecfdSTzung-Bi Shih if (ret != -EINVAL) { 852ae6062c4SAngeloGioacchino Del Regno return dev_err_probe(dev, ret, "Failed to map l1tcm memory\n"); 853ca23ecfdSTzung-Bi Shih } 854ca23ecfdSTzung-Bi Shih } else { 855ca23ecfdSTzung-Bi Shih scp->l1tcm_size = resource_size(res); 856ca23ecfdSTzung-Bi Shih scp->l1tcm_phys = res->start; 857ca23ecfdSTzung-Bi Shih } 85863c13d61SErin Lo 8592e88e8fcSTzung-Bi Shih scp->reg_base = devm_platform_ioremap_resource_byname(pdev, "cfg"); 860ae6062c4SAngeloGioacchino Del Regno if (IS_ERR(scp->reg_base)) 861ae6062c4SAngeloGioacchino Del Regno return dev_err_probe(dev, PTR_ERR(scp->reg_base), 862ae6062c4SAngeloGioacchino Del Regno "Failed to parse and map cfg memory\n"); 86363c13d61SErin Lo 86479111df4STinghan Shen ret = scp->data->scp_clk_get(scp); 86579111df4STinghan Shen if (ret) 86677c792b9SAngeloGioacchino Del Regno return ret; 86777c792b9SAngeloGioacchino Del Regno 86877c792b9SAngeloGioacchino Del Regno ret = scp_map_memory_region(scp); 86977c792b9SAngeloGioacchino Del Regno if (ret) 87077c792b9SAngeloGioacchino Del Regno return ret; 87177c792b9SAngeloGioacchino Del Regno 87277c792b9SAngeloGioacchino Del Regno mutex_init(&scp->send_lock); 87377c792b9SAngeloGioacchino Del Regno for (i = 0; i < SCP_IPI_MAX; i++) 87477c792b9SAngeloGioacchino Del Regno mutex_init(&scp->ipi_desc[i].lock); 87563c13d61SErin Lo 87663c13d61SErin Lo /* register SCP initialization IPI */ 87763c13d61SErin Lo ret = scp_ipi_register(scp, SCP_IPI_INIT, scp_init_ipi_handler, scp); 87863c13d61SErin Lo if (ret) { 87963c13d61SErin Lo dev_err(dev, "Failed to register IPI_SCP_INIT\n"); 88063c13d61SErin Lo goto release_dev_mem; 88163c13d61SErin Lo } 88263c13d61SErin Lo 88363c13d61SErin Lo init_waitqueue_head(&scp->run.wq); 88463c13d61SErin Lo init_waitqueue_head(&scp->ack_wq); 88563c13d61SErin Lo 88670179969SPi-Hsun Shih scp_add_rpmsg_subdev(scp); 88770179969SPi-Hsun Shih 88863c13d61SErin Lo ret = devm_request_threaded_irq(dev, platform_get_irq(pdev, 0), NULL, 88963c13d61SErin Lo scp_irq_handler, IRQF_ONESHOT, 89063c13d61SErin Lo pdev->name, scp); 89163c13d61SErin Lo 89263c13d61SErin Lo if (ret) { 89363c13d61SErin Lo dev_err(dev, "failed to request irq\n"); 89470179969SPi-Hsun Shih goto remove_subdev; 89563c13d61SErin Lo } 89663c13d61SErin Lo 89763c13d61SErin Lo ret = rproc_add(rproc); 89863c13d61SErin Lo if (ret) 89970179969SPi-Hsun Shih goto remove_subdev; 90063c13d61SErin Lo 90170179969SPi-Hsun Shih return 0; 90263c13d61SErin Lo 90370179969SPi-Hsun Shih remove_subdev: 90470179969SPi-Hsun Shih scp_remove_rpmsg_subdev(scp); 90563c13d61SErin Lo scp_ipi_unregister(scp, SCP_IPI_INIT); 90663c13d61SErin Lo release_dev_mem: 90763c13d61SErin Lo scp_unmap_memory_region(scp); 90863c13d61SErin Lo for (i = 0; i < SCP_IPI_MAX; i++) 90963c13d61SErin Lo mutex_destroy(&scp->ipi_desc[i].lock); 91063c13d61SErin Lo mutex_destroy(&scp->send_lock); 91163c13d61SErin Lo 91263c13d61SErin Lo return ret; 91363c13d61SErin Lo } 91463c13d61SErin Lo 91563c13d61SErin Lo static int scp_remove(struct platform_device *pdev) 91663c13d61SErin Lo { 91763c13d61SErin Lo struct mtk_scp *scp = platform_get_drvdata(pdev); 91863c13d61SErin Lo int i; 91963c13d61SErin Lo 92063c13d61SErin Lo rproc_del(scp->rproc); 92170179969SPi-Hsun Shih scp_remove_rpmsg_subdev(scp); 92263c13d61SErin Lo scp_ipi_unregister(scp, SCP_IPI_INIT); 92363c13d61SErin Lo scp_unmap_memory_region(scp); 92463c13d61SErin Lo for (i = 0; i < SCP_IPI_MAX; i++) 92563c13d61SErin Lo mutex_destroy(&scp->ipi_desc[i].lock); 92663c13d61SErin Lo mutex_destroy(&scp->send_lock); 92763c13d61SErin Lo 92863c13d61SErin Lo return 0; 92963c13d61SErin Lo } 93063c13d61SErin Lo 931fd0b6c1fSPi-Hsun Shih static const struct mtk_scp_of_data mt8183_of_data = { 93279111df4STinghan Shen .scp_clk_get = mt8183_scp_clk_get, 933fd0b6c1fSPi-Hsun Shih .scp_before_load = mt8183_scp_before_load, 934fd0b6c1fSPi-Hsun Shih .scp_irq_handler = mt8183_scp_irq_handler, 935fd0b6c1fSPi-Hsun Shih .scp_reset_assert = mt8183_scp_reset_assert, 936fd0b6c1fSPi-Hsun Shih .scp_reset_deassert = mt8183_scp_reset_deassert, 937fd0b6c1fSPi-Hsun Shih .scp_stop = mt8183_scp_stop, 938ca23ecfdSTzung-Bi Shih .scp_da_to_va = mt8183_scp_da_to_va, 939fd0b6c1fSPi-Hsun Shih .host_to_scp_reg = MT8183_HOST_TO_SCP, 940fd0b6c1fSPi-Hsun Shih .host_to_scp_int_bit = MT8183_HOST_IPC_INT_BIT, 9413efa0ea7STzung-Bi Shih .ipi_buf_offset = 0x7bdb0, 942fd0b6c1fSPi-Hsun Shih }; 943fd0b6c1fSPi-Hsun Shih 94480d69185SAllen-KH Cheng static const struct mtk_scp_of_data mt8186_of_data = { 94580d69185SAllen-KH Cheng .scp_clk_get = mt8195_scp_clk_get, 94680d69185SAllen-KH Cheng .scp_before_load = mt8186_scp_before_load, 94780d69185SAllen-KH Cheng .scp_irq_handler = mt8183_scp_irq_handler, 94880d69185SAllen-KH Cheng .scp_reset_assert = mt8183_scp_reset_assert, 94980d69185SAllen-KH Cheng .scp_reset_deassert = mt8183_scp_reset_deassert, 95080d69185SAllen-KH Cheng .scp_stop = mt8183_scp_stop, 95180d69185SAllen-KH Cheng .scp_da_to_va = mt8183_scp_da_to_va, 95280d69185SAllen-KH Cheng .host_to_scp_reg = MT8183_HOST_TO_SCP, 95380d69185SAllen-KH Cheng .host_to_scp_int_bit = MT8183_HOST_IPC_INT_BIT, 95454439d20SAllen-KH Cheng .ipi_buf_offset = 0x3bdb0, 95580d69185SAllen-KH Cheng }; 95680d69185SAllen-KH Cheng 957*42c2b553STinghan Shen static const struct mtk_scp_of_data mt8188_of_data = { 958*42c2b553STinghan Shen .scp_clk_get = mt8195_scp_clk_get, 959*42c2b553STinghan Shen .scp_before_load = mt8192_scp_before_load, 960*42c2b553STinghan Shen .scp_irq_handler = mt8192_scp_irq_handler, 961*42c2b553STinghan Shen .scp_reset_assert = mt8192_scp_reset_assert, 962*42c2b553STinghan Shen .scp_reset_deassert = mt8192_scp_reset_deassert, 963*42c2b553STinghan Shen .scp_stop = mt8192_scp_stop, 964*42c2b553STinghan Shen .scp_da_to_va = mt8192_scp_da_to_va, 965*42c2b553STinghan Shen .host_to_scp_reg = MT8192_GIPC_IN_SET, 966*42c2b553STinghan Shen .host_to_scp_int_bit = MT8192_HOST_IPC_INT_BIT, 967*42c2b553STinghan Shen }; 968*42c2b553STinghan Shen 969fd0b6c1fSPi-Hsun Shih static const struct mtk_scp_of_data mt8192_of_data = { 97079111df4STinghan Shen .scp_clk_get = mt8192_scp_clk_get, 97179111df4STinghan Shen .scp_before_load = mt8192_scp_before_load, 97279111df4STinghan Shen .scp_irq_handler = mt8192_scp_irq_handler, 97379111df4STinghan Shen .scp_reset_assert = mt8192_scp_reset_assert, 97479111df4STinghan Shen .scp_reset_deassert = mt8192_scp_reset_deassert, 97579111df4STinghan Shen .scp_stop = mt8192_scp_stop, 97679111df4STinghan Shen .scp_da_to_va = mt8192_scp_da_to_va, 97779111df4STinghan Shen .host_to_scp_reg = MT8192_GIPC_IN_SET, 97879111df4STinghan Shen .host_to_scp_int_bit = MT8192_HOST_IPC_INT_BIT, 97979111df4STinghan Shen }; 98079111df4STinghan Shen 98179111df4STinghan Shen static const struct mtk_scp_of_data mt8195_of_data = { 98279111df4STinghan Shen .scp_clk_get = mt8195_scp_clk_get, 983f20e232dSTinghan Shen .scp_before_load = mt8195_scp_before_load, 984fd0b6c1fSPi-Hsun Shih .scp_irq_handler = mt8192_scp_irq_handler, 985fd0b6c1fSPi-Hsun Shih .scp_reset_assert = mt8192_scp_reset_assert, 986fd0b6c1fSPi-Hsun Shih .scp_reset_deassert = mt8192_scp_reset_deassert, 987f20e232dSTinghan Shen .scp_stop = mt8195_scp_stop, 988ca23ecfdSTzung-Bi Shih .scp_da_to_va = mt8192_scp_da_to_va, 989fd0b6c1fSPi-Hsun Shih .host_to_scp_reg = MT8192_GIPC_IN_SET, 990fd0b6c1fSPi-Hsun Shih .host_to_scp_int_bit = MT8192_HOST_IPC_INT_BIT, 991fd0b6c1fSPi-Hsun Shih }; 992fd0b6c1fSPi-Hsun Shih 99363c13d61SErin Lo static const struct of_device_id mtk_scp_of_match[] = { 994fd0b6c1fSPi-Hsun Shih { .compatible = "mediatek,mt8183-scp", .data = &mt8183_of_data }, 99580d69185SAllen-KH Cheng { .compatible = "mediatek,mt8186-scp", .data = &mt8186_of_data }, 996*42c2b553STinghan Shen { .compatible = "mediatek,mt8188-scp", .data = &mt8188_of_data }, 997fd0b6c1fSPi-Hsun Shih { .compatible = "mediatek,mt8192-scp", .data = &mt8192_of_data }, 99879111df4STinghan Shen { .compatible = "mediatek,mt8195-scp", .data = &mt8195_of_data }, 99963c13d61SErin Lo {}, 100063c13d61SErin Lo }; 100163c13d61SErin Lo MODULE_DEVICE_TABLE(of, mtk_scp_of_match); 100263c13d61SErin Lo 100363c13d61SErin Lo static struct platform_driver mtk_scp_driver = { 100463c13d61SErin Lo .probe = scp_probe, 100563c13d61SErin Lo .remove = scp_remove, 100663c13d61SErin Lo .driver = { 100763c13d61SErin Lo .name = "mtk-scp", 1008cca21000SSouptick Joarder .of_match_table = mtk_scp_of_match, 100963c13d61SErin Lo }, 101063c13d61SErin Lo }; 101163c13d61SErin Lo 101263c13d61SErin Lo module_platform_driver(mtk_scp_driver); 101363c13d61SErin Lo 101463c13d61SErin Lo MODULE_LICENSE("GPL v2"); 101563c13d61SErin Lo MODULE_DESCRIPTION("MediaTek SCP control driver"); 1016