linux/drivers/remoteproc/mtk_scp.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2//
   3// Copyright (c) 2019 MediaTek Inc.
   4
   5#include <asm/barrier.h>
   6#include <linux/clk.h>
   7#include <linux/dma-mapping.h>
   8#include <linux/err.h>
   9#include <linux/interrupt.h>
  10#include <linux/kernel.h>
  11#include <linux/module.h>
  12#include <linux/of_address.h>
  13#include <linux/of_platform.h>
  14#include <linux/of_reserved_mem.h>
  15#include <linux/platform_device.h>
  16#include <linux/remoteproc.h>
  17#include <linux/remoteproc/mtk_scp.h>
  18#include <linux/rpmsg/mtk_rpmsg.h>
  19
  20#include "mtk_common.h"
  21#include "remoteproc_internal.h"
  22
  23#define MAX_CODE_SIZE 0x500000
  24#define SECTION_NAME_IPI_BUFFER ".ipi_buffer"
  25
  26/**
  27 * scp_get() - get a reference to SCP.
  28 *
  29 * @pdev:       the platform device of the module requesting SCP platform
  30 *              device for using SCP API.
  31 *
  32 * Return: Return NULL if failed.  otherwise reference to SCP.
  33 **/
  34struct mtk_scp *scp_get(struct platform_device *pdev)
  35{
  36        struct device *dev = &pdev->dev;
  37        struct device_node *scp_node;
  38        struct platform_device *scp_pdev;
  39
  40        scp_node = of_parse_phandle(dev->of_node, "mediatek,scp", 0);
  41        if (!scp_node) {
  42                dev_err(dev, "can't get SCP node\n");
  43                return NULL;
  44        }
  45
  46        scp_pdev = of_find_device_by_node(scp_node);
  47        of_node_put(scp_node);
  48
  49        if (WARN_ON(!scp_pdev)) {
  50                dev_err(dev, "SCP pdev failed\n");
  51                return NULL;
  52        }
  53
  54        return platform_get_drvdata(scp_pdev);
  55}
  56EXPORT_SYMBOL_GPL(scp_get);
  57
  58/**
  59 * scp_put() - "free" the SCP
  60 *
  61 * @scp:        mtk_scp structure from scp_get().
  62 **/
  63void scp_put(struct mtk_scp *scp)
  64{
  65        put_device(scp->dev);
  66}
  67EXPORT_SYMBOL_GPL(scp_put);
  68
  69static void scp_wdt_handler(struct mtk_scp *scp, u32 scp_to_host)
  70{
  71        dev_err(scp->dev, "SCP watchdog timeout! 0x%x", scp_to_host);
  72        rproc_report_crash(scp->rproc, RPROC_WATCHDOG);
  73}
  74
  75static void scp_init_ipi_handler(void *data, unsigned int len, void *priv)
  76{
  77        struct mtk_scp *scp = (struct mtk_scp *)priv;
  78        struct scp_run *run = (struct scp_run *)data;
  79
  80        scp->run.signaled = run->signaled;
  81        strscpy(scp->run.fw_ver, run->fw_ver, SCP_FW_VER_LEN);
  82        scp->run.dec_capability = run->dec_capability;
  83        scp->run.enc_capability = run->enc_capability;
  84        wake_up_interruptible(&scp->run.wq);
  85}
  86
  87static void scp_ipi_handler(struct mtk_scp *scp)
  88{
  89        struct mtk_share_obj __iomem *rcv_obj = scp->recv_buf;
  90        struct scp_ipi_desc *ipi_desc = scp->ipi_desc;
  91        u8 tmp_data[SCP_SHARE_BUFFER_SIZE];
  92        scp_ipi_handler_t handler;
  93        u32 id = readl(&rcv_obj->id);
  94        u32 len = readl(&rcv_obj->len);
  95
  96        if (len > SCP_SHARE_BUFFER_SIZE) {
  97                dev_err(scp->dev, "ipi message too long (len %d, max %d)", len,
  98                        SCP_SHARE_BUFFER_SIZE);
  99                return;
 100        }
 101        if (id >= SCP_IPI_MAX) {
 102                dev_err(scp->dev, "No such ipi id = %d\n", id);
 103                return;
 104        }
 105
 106        scp_ipi_lock(scp, id);
 107        handler = ipi_desc[id].handler;
 108        if (!handler) {
 109                dev_err(scp->dev, "No such ipi id = %d\n", id);
 110                scp_ipi_unlock(scp, id);
 111                return;
 112        }
 113
 114        memcpy_fromio(tmp_data, &rcv_obj->share_buf, len);
 115        handler(tmp_data, len, ipi_desc[id].priv);
 116        scp_ipi_unlock(scp, id);
 117
 118        scp->ipi_id_ack[id] = true;
 119        wake_up(&scp->ack_wq);
 120}
 121
 122static int scp_elf_read_ipi_buf_addr(struct mtk_scp *scp,
 123                                     const struct firmware *fw,
 124                                     size_t *offset);
 125
 126static int scp_ipi_init(struct mtk_scp *scp, const struct firmware *fw)
 127{
 128        int ret;
 129        size_t offset;
 130
 131        /* read the ipi buf addr from FW itself first */
 132        ret = scp_elf_read_ipi_buf_addr(scp, fw, &offset);
 133        if (ret) {
 134                /* use default ipi buf addr if the FW doesn't have it */
 135                offset = scp->data->ipi_buf_offset;
 136                if (!offset)
 137                        return ret;
 138        }
 139        dev_info(scp->dev, "IPI buf addr %#010zx\n", offset);
 140
 141        scp->recv_buf = (struct mtk_share_obj __iomem *)
 142                        (scp->sram_base + offset);
 143        scp->send_buf = (struct mtk_share_obj __iomem *)
 144                        (scp->sram_base + offset + sizeof(*scp->recv_buf));
 145        memset_io(scp->recv_buf, 0, sizeof(*scp->recv_buf));
 146        memset_io(scp->send_buf, 0, sizeof(*scp->send_buf));
 147
 148        return 0;
 149}
 150
 151static void mt8183_scp_reset_assert(struct mtk_scp *scp)
 152{
 153        u32 val;
 154
 155        val = readl(scp->reg_base + MT8183_SW_RSTN);
 156        val &= ~MT8183_SW_RSTN_BIT;
 157        writel(val, scp->reg_base + MT8183_SW_RSTN);
 158}
 159
 160static void mt8183_scp_reset_deassert(struct mtk_scp *scp)
 161{
 162        u32 val;
 163
 164        val = readl(scp->reg_base + MT8183_SW_RSTN);
 165        val |= MT8183_SW_RSTN_BIT;
 166        writel(val, scp->reg_base + MT8183_SW_RSTN);
 167}
 168
 169static void mt8192_scp_reset_assert(struct mtk_scp *scp)
 170{
 171        writel(1, scp->reg_base + MT8192_CORE0_SW_RSTN_SET);
 172}
 173
 174static void mt8192_scp_reset_deassert(struct mtk_scp *scp)
 175{
 176        writel(1, scp->reg_base + MT8192_CORE0_SW_RSTN_CLR);
 177}
 178
 179static void mt8183_scp_irq_handler(struct mtk_scp *scp)
 180{
 181        u32 scp_to_host;
 182
 183        scp_to_host = readl(scp->reg_base + MT8183_SCP_TO_HOST);
 184        if (scp_to_host & MT8183_SCP_IPC_INT_BIT)
 185                scp_ipi_handler(scp);
 186        else
 187                scp_wdt_handler(scp, scp_to_host);
 188
 189        /* SCP won't send another interrupt until we set SCP_TO_HOST to 0. */
 190        writel(MT8183_SCP_IPC_INT_BIT | MT8183_SCP_WDT_INT_BIT,
 191               scp->reg_base + MT8183_SCP_TO_HOST);
 192}
 193
 194static void mt8192_scp_irq_handler(struct mtk_scp *scp)
 195{
 196        u32 scp_to_host;
 197
 198        scp_to_host = readl(scp->reg_base + MT8192_SCP2APMCU_IPC_SET);
 199
 200        if (scp_to_host & MT8192_SCP_IPC_INT_BIT) {
 201                scp_ipi_handler(scp);
 202
 203                /*
 204                 * SCP won't send another interrupt until we clear
 205                 * MT8192_SCP2APMCU_IPC.
 206                 */
 207                writel(MT8192_SCP_IPC_INT_BIT,
 208                       scp->reg_base + MT8192_SCP2APMCU_IPC_CLR);
 209        } else {
 210                scp_wdt_handler(scp, scp_to_host);
 211                writel(1, scp->reg_base + MT8192_CORE0_WDT_IRQ);
 212        }
 213}
 214
 215static irqreturn_t scp_irq_handler(int irq, void *priv)
 216{
 217        struct mtk_scp *scp = priv;
 218        int ret;
 219
 220        ret = clk_prepare_enable(scp->clk);
 221        if (ret) {
 222                dev_err(scp->dev, "failed to enable clocks\n");
 223                return IRQ_NONE;
 224        }
 225
 226        scp->data->scp_irq_handler(scp);
 227
 228        clk_disable_unprepare(scp->clk);
 229
 230        return IRQ_HANDLED;
 231}
 232
 233static int scp_elf_load_segments(struct rproc *rproc, const struct firmware *fw)
 234{
 235        struct device *dev = &rproc->dev;
 236        struct elf32_hdr *ehdr;
 237        struct elf32_phdr *phdr;
 238        int i, ret = 0;
 239        const u8 *elf_data = fw->data;
 240
 241        ehdr = (struct elf32_hdr *)elf_data;
 242        phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
 243
 244        /* go through the available ELF segments */
 245        for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
 246                u32 da = phdr->p_paddr;
 247                u32 memsz = phdr->p_memsz;
 248                u32 filesz = phdr->p_filesz;
 249                u32 offset = phdr->p_offset;
 250                void __iomem *ptr;
 251
 252                dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
 253                        phdr->p_type, da, memsz, filesz);
 254
 255                if (phdr->p_type != PT_LOAD)
 256                        continue;
 257                if (!filesz)
 258                        continue;
 259
 260                if (filesz > memsz) {
 261                        dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
 262                                filesz, memsz);
 263                        ret = -EINVAL;
 264                        break;
 265                }
 266
 267                if (offset + filesz > fw->size) {
 268                        dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n",
 269                                offset + filesz, fw->size);
 270                        ret = -EINVAL;
 271                        break;
 272                }
 273
 274                /* grab the kernel address for this device address */
 275                ptr = (void __iomem *)rproc_da_to_va(rproc, da, memsz, NULL);
 276                if (!ptr) {
 277                        dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
 278                        ret = -EINVAL;
 279                        break;
 280                }
 281
 282                /* put the segment where the remote processor expects it */
 283                scp_memcpy_aligned(ptr, elf_data + phdr->p_offset, filesz);
 284        }
 285
 286        return ret;
 287}
 288
 289static int scp_elf_read_ipi_buf_addr(struct mtk_scp *scp,
 290                                     const struct firmware *fw,
 291                                     size_t *offset)
 292{
 293        struct elf32_hdr *ehdr;
 294        struct elf32_shdr *shdr, *shdr_strtab;
 295        int i;
 296        const u8 *elf_data = fw->data;
 297        const char *strtab;
 298
 299        ehdr = (struct elf32_hdr *)elf_data;
 300        shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
 301        shdr_strtab = shdr + ehdr->e_shstrndx;
 302        strtab = (const char *)(elf_data + shdr_strtab->sh_offset);
 303
 304        for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
 305                if (strcmp(strtab + shdr->sh_name,
 306                           SECTION_NAME_IPI_BUFFER) == 0) {
 307                        *offset = shdr->sh_addr;
 308                        return 0;
 309                }
 310        }
 311
 312        return -ENOENT;
 313}
 314
 315static int mt8183_scp_before_load(struct mtk_scp *scp)
 316{
 317        /* Clear SCP to host interrupt */
 318        writel(MT8183_SCP_IPC_INT_BIT, scp->reg_base + MT8183_SCP_TO_HOST);
 319
 320        /* Reset clocks before loading FW */
 321        writel(0x0, scp->reg_base + MT8183_SCP_CLK_SW_SEL);
 322        writel(0x0, scp->reg_base + MT8183_SCP_CLK_DIV_SEL);
 323
 324        /* Initialize TCM before loading FW. */
 325        writel(0x0, scp->reg_base + MT8183_SCP_L1_SRAM_PD);
 326        writel(0x0, scp->reg_base + MT8183_SCP_TCM_TAIL_SRAM_PD);
 327
 328        /* Turn on the power of SCP's SRAM before using it. */
 329        writel(0x0, scp->reg_base + MT8183_SCP_SRAM_PDN);
 330
 331        /*
 332         * Set I-cache and D-cache size before loading SCP FW.
 333         * SCP SRAM logical address may change when cache size setting differs.
 334         */
 335        writel(MT8183_SCP_CACHE_CON_WAYEN | MT8183_SCP_CACHESIZE_8KB,
 336               scp->reg_base + MT8183_SCP_CACHE_CON);
 337        writel(MT8183_SCP_CACHESIZE_8KB, scp->reg_base + MT8183_SCP_DCACHE_CON);
 338
 339        return 0;
 340}
 341
 342static void mt8192_power_on_sram(void __iomem *addr)
 343{
 344        int i;
 345
 346        for (i = 31; i >= 0; i--)
 347                writel(GENMASK(i, 0), addr);
 348        writel(0, addr);
 349}
 350
 351static void mt8192_power_off_sram(void __iomem *addr)
 352{
 353        int i;
 354
 355        writel(0, addr);
 356        for (i = 0; i < 32; i++)
 357                writel(GENMASK(i, 0), addr);
 358}
 359
 360static int mt8192_scp_before_load(struct mtk_scp *scp)
 361{
 362        /* clear SPM interrupt, SCP2SPM_IPC_CLR */
 363        writel(0xff, scp->reg_base + MT8192_SCP2SPM_IPC_CLR);
 364
 365        writel(1, scp->reg_base + MT8192_CORE0_SW_RSTN_SET);
 366
 367        /* enable SRAM clock */
 368        mt8192_power_on_sram(scp->reg_base + MT8192_L2TCM_SRAM_PD_0);
 369        mt8192_power_on_sram(scp->reg_base + MT8192_L2TCM_SRAM_PD_1);
 370        mt8192_power_on_sram(scp->reg_base + MT8192_L2TCM_SRAM_PD_2);
 371        mt8192_power_on_sram(scp->reg_base + MT8192_L1TCM_SRAM_PDN);
 372        mt8192_power_on_sram(scp->reg_base + MT8192_CPU0_SRAM_PD);
 373
 374        /* enable MPU for all memory regions */
 375        writel(0xff, scp->reg_base + MT8192_CORE0_MEM_ATT_PREDEF);
 376
 377        return 0;
 378}
 379
 380static int scp_load(struct rproc *rproc, const struct firmware *fw)
 381{
 382        struct mtk_scp *scp = rproc->priv;
 383        struct device *dev = scp->dev;
 384        int ret;
 385
 386        ret = clk_prepare_enable(scp->clk);
 387        if (ret) {
 388                dev_err(dev, "failed to enable clocks\n");
 389                return ret;
 390        }
 391
 392        /* Hold SCP in reset while loading FW. */
 393        scp->data->scp_reset_assert(scp);
 394
 395        ret = scp->data->scp_before_load(scp);
 396        if (ret < 0)
 397                goto leave;
 398
 399        ret = scp_elf_load_segments(rproc, fw);
 400leave:
 401        clk_disable_unprepare(scp->clk);
 402
 403        return ret;
 404}
 405
 406static int scp_parse_fw(struct rproc *rproc, const struct firmware *fw)
 407{
 408        struct mtk_scp *scp = rproc->priv;
 409        struct device *dev = scp->dev;
 410        int ret;
 411
 412        ret = clk_prepare_enable(scp->clk);
 413        if (ret) {
 414                dev_err(dev, "failed to enable clocks\n");
 415                return ret;
 416        }
 417
 418        ret = scp_ipi_init(scp, fw);
 419        clk_disable_unprepare(scp->clk);
 420        return ret;
 421}
 422
 423static int scp_start(struct rproc *rproc)
 424{
 425        struct mtk_scp *scp = (struct mtk_scp *)rproc->priv;
 426        struct device *dev = scp->dev;
 427        struct scp_run *run = &scp->run;
 428        int ret;
 429
 430        ret = clk_prepare_enable(scp->clk);
 431        if (ret) {
 432                dev_err(dev, "failed to enable clocks\n");
 433                return ret;
 434        }
 435
 436        run->signaled = false;
 437
 438        scp->data->scp_reset_deassert(scp);
 439
 440        ret = wait_event_interruptible_timeout(
 441                                        run->wq,
 442                                        run->signaled,
 443                                        msecs_to_jiffies(2000));
 444
 445        if (ret == 0) {
 446                dev_err(dev, "wait SCP initialization timeout!\n");
 447                ret = -ETIME;
 448                goto stop;
 449        }
 450        if (ret == -ERESTARTSYS) {
 451                dev_err(dev, "wait SCP interrupted by a signal!\n");
 452                goto stop;
 453        }
 454
 455        clk_disable_unprepare(scp->clk);
 456        dev_info(dev, "SCP is ready. FW version %s\n", run->fw_ver);
 457
 458        return 0;
 459
 460stop:
 461        scp->data->scp_reset_assert(scp);
 462        clk_disable_unprepare(scp->clk);
 463        return ret;
 464}
 465
 466static void *mt8183_scp_da_to_va(struct mtk_scp *scp, u64 da, size_t len)
 467{
 468        int offset;
 469
 470        if (da < scp->sram_size) {
 471                offset = da;
 472                if (offset >= 0 && (offset + len) <= scp->sram_size)
 473                        return (void __force *)scp->sram_base + offset;
 474        } else if (scp->dram_size) {
 475                offset = da - scp->dma_addr;
 476                if (offset >= 0 && (offset + len) <= scp->dram_size)
 477                        return scp->cpu_addr + offset;
 478        }
 479
 480        return NULL;
 481}
 482
 483static void *mt8192_scp_da_to_va(struct mtk_scp *scp, u64 da, size_t len)
 484{
 485        int offset;
 486
 487        if (da >= scp->sram_phys &&
 488            (da + len) <= scp->sram_phys + scp->sram_size) {
 489                offset = da - scp->sram_phys;
 490                return (void __force *)scp->sram_base + offset;
 491        }
 492
 493        /* optional memory region */
 494        if (scp->l1tcm_size &&
 495            da >= scp->l1tcm_phys &&
 496            (da + len) <= scp->l1tcm_phys + scp->l1tcm_size) {
 497                offset = da - scp->l1tcm_phys;
 498                return (void __force *)scp->l1tcm_base + offset;
 499        }
 500
 501        /* optional memory region */
 502        if (scp->dram_size &&
 503            da >= scp->dma_addr &&
 504            (da + len) <= scp->dma_addr + scp->dram_size) {
 505                offset = da - scp->dma_addr;
 506                return scp->cpu_addr + offset;
 507        }
 508
 509        return NULL;
 510}
 511
 512static void *scp_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_iomem)
 513{
 514        struct mtk_scp *scp = (struct mtk_scp *)rproc->priv;
 515
 516        return scp->data->scp_da_to_va(scp, da, len);
 517}
 518
 519static void mt8183_scp_stop(struct mtk_scp *scp)
 520{
 521        /* Disable SCP watchdog */
 522        writel(0, scp->reg_base + MT8183_WDT_CFG);
 523}
 524
 525static void mt8192_scp_stop(struct mtk_scp *scp)
 526{
 527        /* Disable SRAM clock */
 528        mt8192_power_off_sram(scp->reg_base + MT8192_L2TCM_SRAM_PD_0);
 529        mt8192_power_off_sram(scp->reg_base + MT8192_L2TCM_SRAM_PD_1);
 530        mt8192_power_off_sram(scp->reg_base + MT8192_L2TCM_SRAM_PD_2);
 531        mt8192_power_off_sram(scp->reg_base + MT8192_L1TCM_SRAM_PDN);
 532        mt8192_power_off_sram(scp->reg_base + MT8192_CPU0_SRAM_PD);
 533
 534        /* Disable SCP watchdog */
 535        writel(0, scp->reg_base + MT8192_CORE0_WDT_CFG);
 536}
 537
 538static int scp_stop(struct rproc *rproc)
 539{
 540        struct mtk_scp *scp = (struct mtk_scp *)rproc->priv;
 541        int ret;
 542
 543        ret = clk_prepare_enable(scp->clk);
 544        if (ret) {
 545                dev_err(scp->dev, "failed to enable clocks\n");
 546                return ret;
 547        }
 548
 549        scp->data->scp_reset_assert(scp);
 550        scp->data->scp_stop(scp);
 551        clk_disable_unprepare(scp->clk);
 552
 553        return 0;
 554}
 555
 556static const struct rproc_ops scp_ops = {
 557        .start          = scp_start,
 558        .stop           = scp_stop,
 559        .load           = scp_load,
 560        .da_to_va       = scp_da_to_va,
 561        .parse_fw       = scp_parse_fw,
 562};
 563
 564/**
 565 * scp_get_device() - get device struct of SCP
 566 *
 567 * @scp:        mtk_scp structure
 568 **/
 569struct device *scp_get_device(struct mtk_scp *scp)
 570{
 571        return scp->dev;
 572}
 573EXPORT_SYMBOL_GPL(scp_get_device);
 574
 575/**
 576 * scp_get_rproc() - get rproc struct of SCP
 577 *
 578 * @scp:        mtk_scp structure
 579 **/
 580struct rproc *scp_get_rproc(struct mtk_scp *scp)
 581{
 582        return scp->rproc;
 583}
 584EXPORT_SYMBOL_GPL(scp_get_rproc);
 585
 586/**
 587 * scp_get_vdec_hw_capa() - get video decoder hardware capability
 588 *
 589 * @scp:        mtk_scp structure
 590 *
 591 * Return: video decoder hardware capability
 592 **/
 593unsigned int scp_get_vdec_hw_capa(struct mtk_scp *scp)
 594{
 595        return scp->run.dec_capability;
 596}
 597EXPORT_SYMBOL_GPL(scp_get_vdec_hw_capa);
 598
 599/**
 600 * scp_get_venc_hw_capa() - get video encoder hardware capability
 601 *
 602 * @scp:        mtk_scp structure
 603 *
 604 * Return: video encoder hardware capability
 605 **/
 606unsigned int scp_get_venc_hw_capa(struct mtk_scp *scp)
 607{
 608        return scp->run.enc_capability;
 609}
 610EXPORT_SYMBOL_GPL(scp_get_venc_hw_capa);
 611
 612/**
 613 * scp_mapping_dm_addr() - Mapping SRAM/DRAM to kernel virtual address
 614 *
 615 * @scp:        mtk_scp structure
 616 * @mem_addr:   SCP views memory address
 617 *
 618 * Mapping the SCP's SRAM address /
 619 * DMEM (Data Extended Memory) memory address /
 620 * Working buffer memory address to
 621 * kernel virtual address.
 622 *
 623 * Return: Return ERR_PTR(-EINVAL) if mapping failed,
 624 * otherwise the mapped kernel virtual address
 625 **/
 626void *scp_mapping_dm_addr(struct mtk_scp *scp, u32 mem_addr)
 627{
 628        void *ptr;
 629
 630        ptr = scp_da_to_va(scp->rproc, mem_addr, 0, NULL);
 631        if (!ptr)
 632                return ERR_PTR(-EINVAL);
 633
 634        return ptr;
 635}
 636EXPORT_SYMBOL_GPL(scp_mapping_dm_addr);
 637
 638static int scp_map_memory_region(struct mtk_scp *scp)
 639{
 640        int ret;
 641
 642        ret = of_reserved_mem_device_init(scp->dev);
 643
 644        /* reserved memory is optional. */
 645        if (ret == -ENODEV) {
 646                dev_info(scp->dev, "skipping reserved memory initialization.");
 647                return 0;
 648        }
 649
 650        if (ret) {
 651                dev_err(scp->dev, "failed to assign memory-region: %d\n", ret);
 652                return -ENOMEM;
 653        }
 654
 655        /* Reserved SCP code size */
 656        scp->dram_size = MAX_CODE_SIZE;
 657        scp->cpu_addr = dma_alloc_coherent(scp->dev, scp->dram_size,
 658                                           &scp->dma_addr, GFP_KERNEL);
 659        if (!scp->cpu_addr)
 660                return -ENOMEM;
 661
 662        return 0;
 663}
 664
 665static void scp_unmap_memory_region(struct mtk_scp *scp)
 666{
 667        if (scp->dram_size == 0)
 668                return;
 669
 670        dma_free_coherent(scp->dev, scp->dram_size, scp->cpu_addr,
 671                          scp->dma_addr);
 672        of_reserved_mem_device_release(scp->dev);
 673}
 674
 675static int scp_register_ipi(struct platform_device *pdev, u32 id,
 676                            ipi_handler_t handler, void *priv)
 677{
 678        struct mtk_scp *scp = platform_get_drvdata(pdev);
 679
 680        return scp_ipi_register(scp, id, handler, priv);
 681}
 682
 683static void scp_unregister_ipi(struct platform_device *pdev, u32 id)
 684{
 685        struct mtk_scp *scp = platform_get_drvdata(pdev);
 686
 687        scp_ipi_unregister(scp, id);
 688}
 689
 690static int scp_send_ipi(struct platform_device *pdev, u32 id, void *buf,
 691                        unsigned int len, unsigned int wait)
 692{
 693        struct mtk_scp *scp = platform_get_drvdata(pdev);
 694
 695        return scp_ipi_send(scp, id, buf, len, wait);
 696}
 697
 698static struct mtk_rpmsg_info mtk_scp_rpmsg_info = {
 699        .send_ipi = scp_send_ipi,
 700        .register_ipi = scp_register_ipi,
 701        .unregister_ipi = scp_unregister_ipi,
 702        .ns_ipi_id = SCP_IPI_NS_SERVICE,
 703};
 704
 705static void scp_add_rpmsg_subdev(struct mtk_scp *scp)
 706{
 707        scp->rpmsg_subdev =
 708                mtk_rpmsg_create_rproc_subdev(to_platform_device(scp->dev),
 709                                              &mtk_scp_rpmsg_info);
 710        if (scp->rpmsg_subdev)
 711                rproc_add_subdev(scp->rproc, scp->rpmsg_subdev);
 712}
 713
 714static void scp_remove_rpmsg_subdev(struct mtk_scp *scp)
 715{
 716        if (scp->rpmsg_subdev) {
 717                rproc_remove_subdev(scp->rproc, scp->rpmsg_subdev);
 718                mtk_rpmsg_destroy_rproc_subdev(scp->rpmsg_subdev);
 719                scp->rpmsg_subdev = NULL;
 720        }
 721}
 722
 723static int scp_probe(struct platform_device *pdev)
 724{
 725        struct device *dev = &pdev->dev;
 726        struct device_node *np = dev->of_node;
 727        struct mtk_scp *scp;
 728        struct rproc *rproc;
 729        struct resource *res;
 730        char *fw_name = "scp.img";
 731        int ret, i;
 732
 733        rproc = rproc_alloc(dev,
 734                            np->name,
 735                            &scp_ops,
 736                            fw_name,
 737                            sizeof(*scp));
 738        if (!rproc) {
 739                dev_err(dev, "unable to allocate remoteproc\n");
 740                return -ENOMEM;
 741        }
 742
 743        scp = (struct mtk_scp *)rproc->priv;
 744        scp->rproc = rproc;
 745        scp->dev = dev;
 746        scp->data = of_device_get_match_data(dev);
 747        platform_set_drvdata(pdev, scp);
 748
 749        res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sram");
 750        scp->sram_base = devm_ioremap_resource(dev, res);
 751        if (IS_ERR((__force void *)scp->sram_base)) {
 752                dev_err(dev, "Failed to parse and map sram memory\n");
 753                ret = PTR_ERR((__force void *)scp->sram_base);
 754                goto free_rproc;
 755        }
 756        scp->sram_size = resource_size(res);
 757        scp->sram_phys = res->start;
 758
 759        /* l1tcm is an optional memory region */
 760        res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "l1tcm");
 761        scp->l1tcm_base = devm_ioremap_resource(dev, res);
 762        if (IS_ERR((__force void *)scp->l1tcm_base)) {
 763                ret = PTR_ERR((__force void *)scp->l1tcm_base);
 764                if (ret != -EINVAL) {
 765                        dev_err(dev, "Failed to map l1tcm memory\n");
 766                        goto free_rproc;
 767                }
 768        } else {
 769                scp->l1tcm_size = resource_size(res);
 770                scp->l1tcm_phys = res->start;
 771        }
 772
 773        mutex_init(&scp->send_lock);
 774        for (i = 0; i < SCP_IPI_MAX; i++)
 775                mutex_init(&scp->ipi_desc[i].lock);
 776
 777        scp->reg_base = devm_platform_ioremap_resource_byname(pdev, "cfg");
 778        if (IS_ERR((__force void *)scp->reg_base)) {
 779                dev_err(dev, "Failed to parse and map cfg memory\n");
 780                ret = PTR_ERR((__force void *)scp->reg_base);
 781                goto destroy_mutex;
 782        }
 783
 784        ret = scp_map_memory_region(scp);
 785        if (ret)
 786                goto destroy_mutex;
 787
 788        scp->clk = devm_clk_get(dev, "main");
 789        if (IS_ERR(scp->clk)) {
 790                dev_err(dev, "Failed to get clock\n");
 791                ret = PTR_ERR(scp->clk);
 792                goto release_dev_mem;
 793        }
 794
 795        /* register SCP initialization IPI */
 796        ret = scp_ipi_register(scp, SCP_IPI_INIT, scp_init_ipi_handler, scp);
 797        if (ret) {
 798                dev_err(dev, "Failed to register IPI_SCP_INIT\n");
 799                goto release_dev_mem;
 800        }
 801
 802        init_waitqueue_head(&scp->run.wq);
 803        init_waitqueue_head(&scp->ack_wq);
 804
 805        scp_add_rpmsg_subdev(scp);
 806
 807        ret = devm_request_threaded_irq(dev, platform_get_irq(pdev, 0), NULL,
 808                                        scp_irq_handler, IRQF_ONESHOT,
 809                                        pdev->name, scp);
 810
 811        if (ret) {
 812                dev_err(dev, "failed to request irq\n");
 813                goto remove_subdev;
 814        }
 815
 816        ret = rproc_add(rproc);
 817        if (ret)
 818                goto remove_subdev;
 819
 820        return 0;
 821
 822remove_subdev:
 823        scp_remove_rpmsg_subdev(scp);
 824        scp_ipi_unregister(scp, SCP_IPI_INIT);
 825release_dev_mem:
 826        scp_unmap_memory_region(scp);
 827destroy_mutex:
 828        for (i = 0; i < SCP_IPI_MAX; i++)
 829                mutex_destroy(&scp->ipi_desc[i].lock);
 830        mutex_destroy(&scp->send_lock);
 831free_rproc:
 832        rproc_free(rproc);
 833
 834        return ret;
 835}
 836
 837static int scp_remove(struct platform_device *pdev)
 838{
 839        struct mtk_scp *scp = platform_get_drvdata(pdev);
 840        int i;
 841
 842        rproc_del(scp->rproc);
 843        scp_remove_rpmsg_subdev(scp);
 844        scp_ipi_unregister(scp, SCP_IPI_INIT);
 845        scp_unmap_memory_region(scp);
 846        for (i = 0; i < SCP_IPI_MAX; i++)
 847                mutex_destroy(&scp->ipi_desc[i].lock);
 848        mutex_destroy(&scp->send_lock);
 849        rproc_free(scp->rproc);
 850
 851        return 0;
 852}
 853
 854static const struct mtk_scp_of_data mt8183_of_data = {
 855        .scp_before_load = mt8183_scp_before_load,
 856        .scp_irq_handler = mt8183_scp_irq_handler,
 857        .scp_reset_assert = mt8183_scp_reset_assert,
 858        .scp_reset_deassert = mt8183_scp_reset_deassert,
 859        .scp_stop = mt8183_scp_stop,
 860        .scp_da_to_va = mt8183_scp_da_to_va,
 861        .host_to_scp_reg = MT8183_HOST_TO_SCP,
 862        .host_to_scp_int_bit = MT8183_HOST_IPC_INT_BIT,
 863        .ipi_buf_offset = 0x7bdb0,
 864};
 865
 866static const struct mtk_scp_of_data mt8192_of_data = {
 867        .scp_before_load = mt8192_scp_before_load,
 868        .scp_irq_handler = mt8192_scp_irq_handler,
 869        .scp_reset_assert = mt8192_scp_reset_assert,
 870        .scp_reset_deassert = mt8192_scp_reset_deassert,
 871        .scp_stop = mt8192_scp_stop,
 872        .scp_da_to_va = mt8192_scp_da_to_va,
 873        .host_to_scp_reg = MT8192_GIPC_IN_SET,
 874        .host_to_scp_int_bit = MT8192_HOST_IPC_INT_BIT,
 875};
 876
 877static const struct of_device_id mtk_scp_of_match[] = {
 878        { .compatible = "mediatek,mt8183-scp", .data = &mt8183_of_data },
 879        { .compatible = "mediatek,mt8192-scp", .data = &mt8192_of_data },
 880        {},
 881};
 882MODULE_DEVICE_TABLE(of, mtk_scp_of_match);
 883
 884static struct platform_driver mtk_scp_driver = {
 885        .probe = scp_probe,
 886        .remove = scp_remove,
 887        .driver = {
 888                .name = "mtk-scp",
 889                .of_match_table = mtk_scp_of_match,
 890        },
 891};
 892
 893module_platform_driver(mtk_scp_driver);
 894
 895MODULE_LICENSE("GPL v2");
 896MODULE_DESCRIPTION("MediaTek SCP control driver");
 897