12023a998SMauro Carvalho Chehab // SPDX-License-Identifier: GPL-2.0-only
22023a998SMauro Carvalho Chehab /*
32023a998SMauro Carvalho Chehab * Copyright (c) 2016 MediaTek Inc.
42023a998SMauro Carvalho Chehab * Author: Andrew-CT Chen <andrew-ct.chen@mediatek.com>
52023a998SMauro Carvalho Chehab */
62023a998SMauro Carvalho Chehab #include <linux/clk.h>
72023a998SMauro Carvalho Chehab #include <linux/debugfs.h>
82023a998SMauro Carvalho Chehab #include <linux/firmware.h>
92023a998SMauro Carvalho Chehab #include <linux/interrupt.h>
102023a998SMauro Carvalho Chehab #include <linux/iommu.h>
112023a998SMauro Carvalho Chehab #include <linux/module.h>
127c7e33b7SRob Herring #include <linux/of.h>
132023a998SMauro Carvalho Chehab #include <linux/of_platform.h>
142023a998SMauro Carvalho Chehab #include <linux/of_reserved_mem.h>
157c7e33b7SRob Herring #include <linux/platform_device.h>
162023a998SMauro Carvalho Chehab #include <linux/sched.h>
172023a998SMauro Carvalho Chehab #include <linux/sizes.h>
182023a998SMauro Carvalho Chehab #include <linux/dma-mapping.h>
192023a998SMauro Carvalho Chehab
202023a998SMauro Carvalho Chehab #include "mtk_vpu.h"
212023a998SMauro Carvalho Chehab
222023a998SMauro Carvalho Chehab /*
232023a998SMauro Carvalho Chehab * VPU (video processor unit) is a tiny processor controlling video hardware
242023a998SMauro Carvalho Chehab * related to video codec, scaling and color format converting.
252023a998SMauro Carvalho Chehab * VPU interfaces with other blocks by share memory and interrupt.
262023a998SMauro Carvalho Chehab */
272023a998SMauro Carvalho Chehab
282023a998SMauro Carvalho Chehab #define INIT_TIMEOUT_MS 2000U
292023a998SMauro Carvalho Chehab #define IPI_TIMEOUT_MS 2000U
302023a998SMauro Carvalho Chehab #define VPU_IDLE_TIMEOUT_MS 1000U
312023a998SMauro Carvalho Chehab #define VPU_FW_VER_LEN 16
322023a998SMauro Carvalho Chehab
332023a998SMauro Carvalho Chehab /* maximum program/data TCM (Tightly-Coupled Memory) size */
342023a998SMauro Carvalho Chehab #define VPU_PTCM_SIZE (96 * SZ_1K)
352023a998SMauro Carvalho Chehab #define VPU_DTCM_SIZE (32 * SZ_1K)
362023a998SMauro Carvalho Chehab /* the offset to get data tcm address */
372023a998SMauro Carvalho Chehab #define VPU_DTCM_OFFSET 0x18000UL
382023a998SMauro Carvalho Chehab /* daynamic allocated maximum extended memory size */
392023a998SMauro Carvalho Chehab #define VPU_EXT_P_SIZE SZ_1M
402023a998SMauro Carvalho Chehab #define VPU_EXT_D_SIZE SZ_4M
412023a998SMauro Carvalho Chehab /* maximum binary firmware size */
422023a998SMauro Carvalho Chehab #define VPU_P_FW_SIZE (VPU_PTCM_SIZE + VPU_EXT_P_SIZE)
432023a998SMauro Carvalho Chehab #define VPU_D_FW_SIZE (VPU_DTCM_SIZE + VPU_EXT_D_SIZE)
442023a998SMauro Carvalho Chehab /* the size of share buffer between Host and VPU */
452023a998SMauro Carvalho Chehab #define SHARE_BUF_SIZE 48
462023a998SMauro Carvalho Chehab
472023a998SMauro Carvalho Chehab /* binary firmware name */
482023a998SMauro Carvalho Chehab #define VPU_P_FW "vpu_p.bin"
492023a998SMauro Carvalho Chehab #define VPU_D_FW "vpu_d.bin"
502023a998SMauro Carvalho Chehab #define VPU_P_FW_NEW "mediatek/mt8173/vpu_p.bin"
512023a998SMauro Carvalho Chehab #define VPU_D_FW_NEW "mediatek/mt8173/vpu_d.bin"
522023a998SMauro Carvalho Chehab
532023a998SMauro Carvalho Chehab #define VPU_RESET 0x0
542023a998SMauro Carvalho Chehab #define VPU_TCM_CFG 0x0008
552023a998SMauro Carvalho Chehab #define VPU_PMEM_EXT0_ADDR 0x000C
562023a998SMauro Carvalho Chehab #define VPU_PMEM_EXT1_ADDR 0x0010
572023a998SMauro Carvalho Chehab #define VPU_TO_HOST 0x001C
582023a998SMauro Carvalho Chehab #define VPU_DMEM_EXT0_ADDR 0x0014
592023a998SMauro Carvalho Chehab #define VPU_DMEM_EXT1_ADDR 0x0018
602023a998SMauro Carvalho Chehab #define HOST_TO_VPU 0x0024
612023a998SMauro Carvalho Chehab #define VPU_IDLE_REG 0x002C
622023a998SMauro Carvalho Chehab #define VPU_INT_STATUS 0x0034
632023a998SMauro Carvalho Chehab #define VPU_PC_REG 0x0060
642023a998SMauro Carvalho Chehab #define VPU_SP_REG 0x0064
652023a998SMauro Carvalho Chehab #define VPU_RA_REG 0x0068
662023a998SMauro Carvalho Chehab #define VPU_WDT_REG 0x0084
672023a998SMauro Carvalho Chehab
682023a998SMauro Carvalho Chehab /* vpu inter-processor communication interrupt */
692023a998SMauro Carvalho Chehab #define VPU_IPC_INT BIT(8)
702023a998SMauro Carvalho Chehab /* vpu idle state */
712023a998SMauro Carvalho Chehab #define VPU_IDLE_STATE BIT(23)
722023a998SMauro Carvalho Chehab
732023a998SMauro Carvalho Chehab /**
742023a998SMauro Carvalho Chehab * enum vpu_fw_type - VPU firmware type
752023a998SMauro Carvalho Chehab *
762023a998SMauro Carvalho Chehab * @P_FW: program firmware
772023a998SMauro Carvalho Chehab * @D_FW: data firmware
782023a998SMauro Carvalho Chehab *
792023a998SMauro Carvalho Chehab */
802023a998SMauro Carvalho Chehab enum vpu_fw_type {
812023a998SMauro Carvalho Chehab P_FW,
822023a998SMauro Carvalho Chehab D_FW,
832023a998SMauro Carvalho Chehab };
842023a998SMauro Carvalho Chehab
852023a998SMauro Carvalho Chehab /**
862023a998SMauro Carvalho Chehab * struct vpu_mem - VPU extended program/data memory information
872023a998SMauro Carvalho Chehab *
882023a998SMauro Carvalho Chehab * @va: the kernel virtual memory address of VPU extended memory
892023a998SMauro Carvalho Chehab * @pa: the physical memory address of VPU extended memory
902023a998SMauro Carvalho Chehab *
912023a998SMauro Carvalho Chehab */
922023a998SMauro Carvalho Chehab struct vpu_mem {
932023a998SMauro Carvalho Chehab void *va;
942023a998SMauro Carvalho Chehab dma_addr_t pa;
952023a998SMauro Carvalho Chehab };
962023a998SMauro Carvalho Chehab
972023a998SMauro Carvalho Chehab /**
982023a998SMauro Carvalho Chehab * struct vpu_regs - VPU TCM and configuration registers
992023a998SMauro Carvalho Chehab *
1002023a998SMauro Carvalho Chehab * @tcm: the register for VPU Tightly-Coupled Memory
1012023a998SMauro Carvalho Chehab * @cfg: the register for VPU configuration
1022023a998SMauro Carvalho Chehab * @irq: the irq number for VPU interrupt
1032023a998SMauro Carvalho Chehab */
1042023a998SMauro Carvalho Chehab struct vpu_regs {
1052023a998SMauro Carvalho Chehab void __iomem *tcm;
1062023a998SMauro Carvalho Chehab void __iomem *cfg;
1072023a998SMauro Carvalho Chehab int irq;
1082023a998SMauro Carvalho Chehab };
1092023a998SMauro Carvalho Chehab
1102023a998SMauro Carvalho Chehab /**
1112023a998SMauro Carvalho Chehab * struct vpu_wdt_handler - VPU watchdog reset handler
1122023a998SMauro Carvalho Chehab *
1132023a998SMauro Carvalho Chehab * @reset_func: reset handler
1142023a998SMauro Carvalho Chehab * @priv: private data
1152023a998SMauro Carvalho Chehab */
1162023a998SMauro Carvalho Chehab struct vpu_wdt_handler {
1172023a998SMauro Carvalho Chehab void (*reset_func)(void *);
1182023a998SMauro Carvalho Chehab void *priv;
1192023a998SMauro Carvalho Chehab };
1202023a998SMauro Carvalho Chehab
1212023a998SMauro Carvalho Chehab /**
1222023a998SMauro Carvalho Chehab * struct vpu_wdt - VPU watchdog workqueue
1232023a998SMauro Carvalho Chehab *
1242023a998SMauro Carvalho Chehab * @handler: VPU watchdog reset handler
1252023a998SMauro Carvalho Chehab * @ws: workstruct for VPU watchdog
1262023a998SMauro Carvalho Chehab * @wq: workqueue for VPU watchdog
1272023a998SMauro Carvalho Chehab */
1282023a998SMauro Carvalho Chehab struct vpu_wdt {
1292023a998SMauro Carvalho Chehab struct vpu_wdt_handler handler[VPU_RST_MAX];
1302023a998SMauro Carvalho Chehab struct work_struct ws;
1312023a998SMauro Carvalho Chehab struct workqueue_struct *wq;
1322023a998SMauro Carvalho Chehab };
1332023a998SMauro Carvalho Chehab
1342023a998SMauro Carvalho Chehab /**
1352023a998SMauro Carvalho Chehab * struct vpu_run - VPU initialization status
1362023a998SMauro Carvalho Chehab *
1372023a998SMauro Carvalho Chehab * @signaled: the signal of vpu initialization completed
1382023a998SMauro Carvalho Chehab * @fw_ver: VPU firmware version
1392023a998SMauro Carvalho Chehab * @dec_capability: decoder capability which is not used for now and
1402023a998SMauro Carvalho Chehab * the value is reserved for future use
1412023a998SMauro Carvalho Chehab * @enc_capability: encoder capability which is not used for now and
1422023a998SMauro Carvalho Chehab * the value is reserved for future use
1432023a998SMauro Carvalho Chehab * @wq: wait queue for VPU initialization status
1442023a998SMauro Carvalho Chehab */
1452023a998SMauro Carvalho Chehab struct vpu_run {
1462023a998SMauro Carvalho Chehab u32 signaled;
1472023a998SMauro Carvalho Chehab char fw_ver[VPU_FW_VER_LEN];
1482023a998SMauro Carvalho Chehab unsigned int dec_capability;
1492023a998SMauro Carvalho Chehab unsigned int enc_capability;
1502023a998SMauro Carvalho Chehab wait_queue_head_t wq;
1512023a998SMauro Carvalho Chehab };
1522023a998SMauro Carvalho Chehab
1532023a998SMauro Carvalho Chehab /**
1542023a998SMauro Carvalho Chehab * struct vpu_ipi_desc - VPU IPI descriptor
1552023a998SMauro Carvalho Chehab *
1562023a998SMauro Carvalho Chehab * @handler: IPI handler
1572023a998SMauro Carvalho Chehab * @name: the name of IPI handler
1582023a998SMauro Carvalho Chehab * @priv: the private data of IPI handler
1592023a998SMauro Carvalho Chehab */
1602023a998SMauro Carvalho Chehab struct vpu_ipi_desc {
1612023a998SMauro Carvalho Chehab ipi_handler_t handler;
1622023a998SMauro Carvalho Chehab const char *name;
1632023a998SMauro Carvalho Chehab void *priv;
1642023a998SMauro Carvalho Chehab };
1652023a998SMauro Carvalho Chehab
1662023a998SMauro Carvalho Chehab /**
1672023a998SMauro Carvalho Chehab * struct share_obj - DTCM (Data Tightly-Coupled Memory) buffer shared with
1682023a998SMauro Carvalho Chehab * AP and VPU
1692023a998SMauro Carvalho Chehab *
1702023a998SMauro Carvalho Chehab * @id: IPI id
1712023a998SMauro Carvalho Chehab * @len: share buffer length
1722023a998SMauro Carvalho Chehab * @share_buf: share buffer data
1732023a998SMauro Carvalho Chehab */
1742023a998SMauro Carvalho Chehab struct share_obj {
1752023a998SMauro Carvalho Chehab s32 id;
1762023a998SMauro Carvalho Chehab u32 len;
1772023a998SMauro Carvalho Chehab unsigned char share_buf[SHARE_BUF_SIZE];
1782023a998SMauro Carvalho Chehab };
1792023a998SMauro Carvalho Chehab
1802023a998SMauro Carvalho Chehab /**
1812023a998SMauro Carvalho Chehab * struct mtk_vpu - vpu driver data
1822023a998SMauro Carvalho Chehab * @extmem: VPU extended memory information
1832023a998SMauro Carvalho Chehab * @reg: VPU TCM and configuration registers
1842023a998SMauro Carvalho Chehab * @run: VPU initialization status
1852023a998SMauro Carvalho Chehab * @wdt: VPU watchdog workqueue
1862023a998SMauro Carvalho Chehab * @ipi_desc: VPU IPI descriptor
1872023a998SMauro Carvalho Chehab * @recv_buf: VPU DTCM share buffer for receiving. The
1882023a998SMauro Carvalho Chehab * receive buffer is only accessed in interrupt context.
1892023a998SMauro Carvalho Chehab * @send_buf: VPU DTCM share buffer for sending
1902023a998SMauro Carvalho Chehab * @dev: VPU struct device
1912023a998SMauro Carvalho Chehab * @clk: VPU clock on/off
1922023a998SMauro Carvalho Chehab * @fw_loaded: indicate VPU firmware loaded
1932023a998SMauro Carvalho Chehab * @enable_4GB: VPU 4GB mode on/off
1942023a998SMauro Carvalho Chehab * @vpu_mutex: protect mtk_vpu (except recv_buf) and ensure only
1952023a998SMauro Carvalho Chehab * one client to use VPU service at a time. For example,
1962023a998SMauro Carvalho Chehab * suppose a client is using VPU to decode VP8.
1972023a998SMauro Carvalho Chehab * If the other client wants to encode VP8,
1982023a998SMauro Carvalho Chehab * it has to wait until VP8 decode completes.
1992023a998SMauro Carvalho Chehab * @wdt_refcnt: WDT reference count to make sure the watchdog can be
2002023a998SMauro Carvalho Chehab * disabled if no other client is using VPU service
2012023a998SMauro Carvalho Chehab * @ack_wq: The wait queue for each codec and mdp. When sleeping
2022023a998SMauro Carvalho Chehab * processes wake up, they will check the condition
2032023a998SMauro Carvalho Chehab * "ipi_id_ack" to run the corresponding action or
2042023a998SMauro Carvalho Chehab * go back to sleep.
2052023a998SMauro Carvalho Chehab * @ipi_id_ack: The ACKs for registered IPI function sending
2062023a998SMauro Carvalho Chehab * interrupt to VPU
2072023a998SMauro Carvalho Chehab *
2082023a998SMauro Carvalho Chehab */
2092023a998SMauro Carvalho Chehab struct mtk_vpu {
2102023a998SMauro Carvalho Chehab struct vpu_mem extmem[2];
2112023a998SMauro Carvalho Chehab struct vpu_regs reg;
2122023a998SMauro Carvalho Chehab struct vpu_run run;
2132023a998SMauro Carvalho Chehab struct vpu_wdt wdt;
2142023a998SMauro Carvalho Chehab struct vpu_ipi_desc ipi_desc[IPI_MAX];
2152023a998SMauro Carvalho Chehab struct share_obj __iomem *recv_buf;
2162023a998SMauro Carvalho Chehab struct share_obj __iomem *send_buf;
2172023a998SMauro Carvalho Chehab struct device *dev;
2182023a998SMauro Carvalho Chehab struct clk *clk;
2192023a998SMauro Carvalho Chehab bool fw_loaded;
2202023a998SMauro Carvalho Chehab bool enable_4GB;
2212023a998SMauro Carvalho Chehab struct mutex vpu_mutex; /* for protecting vpu data data structure */
2222023a998SMauro Carvalho Chehab u32 wdt_refcnt;
2232023a998SMauro Carvalho Chehab wait_queue_head_t ack_wq;
2242023a998SMauro Carvalho Chehab bool ipi_id_ack[IPI_MAX];
2252023a998SMauro Carvalho Chehab };
2262023a998SMauro Carvalho Chehab
vpu_cfg_writel(struct mtk_vpu * vpu,u32 val,u32 offset)2272023a998SMauro Carvalho Chehab static inline void vpu_cfg_writel(struct mtk_vpu *vpu, u32 val, u32 offset)
2282023a998SMauro Carvalho Chehab {
2292023a998SMauro Carvalho Chehab writel(val, vpu->reg.cfg + offset);
2302023a998SMauro Carvalho Chehab }
2312023a998SMauro Carvalho Chehab
vpu_cfg_readl(struct mtk_vpu * vpu,u32 offset)2322023a998SMauro Carvalho Chehab static inline u32 vpu_cfg_readl(struct mtk_vpu *vpu, u32 offset)
2332023a998SMauro Carvalho Chehab {
2342023a998SMauro Carvalho Chehab return readl(vpu->reg.cfg + offset);
2352023a998SMauro Carvalho Chehab }
2362023a998SMauro Carvalho Chehab
vpu_running(struct mtk_vpu * vpu)2372023a998SMauro Carvalho Chehab static inline bool vpu_running(struct mtk_vpu *vpu)
2382023a998SMauro Carvalho Chehab {
2392023a998SMauro Carvalho Chehab return vpu_cfg_readl(vpu, VPU_RESET) & BIT(0);
2402023a998SMauro Carvalho Chehab }
2412023a998SMauro Carvalho Chehab
vpu_clock_disable(struct mtk_vpu * vpu)2422023a998SMauro Carvalho Chehab static void vpu_clock_disable(struct mtk_vpu *vpu)
2432023a998SMauro Carvalho Chehab {
2442023a998SMauro Carvalho Chehab /* Disable VPU watchdog */
2452023a998SMauro Carvalho Chehab mutex_lock(&vpu->vpu_mutex);
2462023a998SMauro Carvalho Chehab if (!--vpu->wdt_refcnt)
2472023a998SMauro Carvalho Chehab vpu_cfg_writel(vpu,
2482023a998SMauro Carvalho Chehab vpu_cfg_readl(vpu, VPU_WDT_REG) & ~(1L << 31),
2492023a998SMauro Carvalho Chehab VPU_WDT_REG);
2502023a998SMauro Carvalho Chehab mutex_unlock(&vpu->vpu_mutex);
2512023a998SMauro Carvalho Chehab
2522023a998SMauro Carvalho Chehab clk_disable(vpu->clk);
2532023a998SMauro Carvalho Chehab }
2542023a998SMauro Carvalho Chehab
vpu_clock_enable(struct mtk_vpu * vpu)2552023a998SMauro Carvalho Chehab static int vpu_clock_enable(struct mtk_vpu *vpu)
2562023a998SMauro Carvalho Chehab {
2572023a998SMauro Carvalho Chehab int ret;
2582023a998SMauro Carvalho Chehab
2592023a998SMauro Carvalho Chehab ret = clk_enable(vpu->clk);
2602023a998SMauro Carvalho Chehab if (ret)
2612023a998SMauro Carvalho Chehab return ret;
2622023a998SMauro Carvalho Chehab /* Enable VPU watchdog */
2632023a998SMauro Carvalho Chehab mutex_lock(&vpu->vpu_mutex);
2642023a998SMauro Carvalho Chehab if (!vpu->wdt_refcnt++)
2652023a998SMauro Carvalho Chehab vpu_cfg_writel(vpu,
2662023a998SMauro Carvalho Chehab vpu_cfg_readl(vpu, VPU_WDT_REG) | (1L << 31),
2672023a998SMauro Carvalho Chehab VPU_WDT_REG);
2682023a998SMauro Carvalho Chehab mutex_unlock(&vpu->vpu_mutex);
2692023a998SMauro Carvalho Chehab
2702023a998SMauro Carvalho Chehab return ret;
2712023a998SMauro Carvalho Chehab }
2722023a998SMauro Carvalho Chehab
vpu_dump_status(struct mtk_vpu * vpu)2732023a998SMauro Carvalho Chehab static void vpu_dump_status(struct mtk_vpu *vpu)
2742023a998SMauro Carvalho Chehab {
2752023a998SMauro Carvalho Chehab dev_info(vpu->dev,
2762023a998SMauro Carvalho Chehab "vpu: run %x, pc = 0x%x, ra = 0x%x, sp = 0x%x, idle = 0x%x\n"
2772023a998SMauro Carvalho Chehab "vpu: int %x, hv = 0x%x, vh = 0x%x, wdt = 0x%x\n",
2782023a998SMauro Carvalho Chehab vpu_running(vpu), vpu_cfg_readl(vpu, VPU_PC_REG),
2792023a998SMauro Carvalho Chehab vpu_cfg_readl(vpu, VPU_RA_REG), vpu_cfg_readl(vpu, VPU_SP_REG),
2802023a998SMauro Carvalho Chehab vpu_cfg_readl(vpu, VPU_IDLE_REG),
2812023a998SMauro Carvalho Chehab vpu_cfg_readl(vpu, VPU_INT_STATUS),
2822023a998SMauro Carvalho Chehab vpu_cfg_readl(vpu, HOST_TO_VPU),
2832023a998SMauro Carvalho Chehab vpu_cfg_readl(vpu, VPU_TO_HOST),
2842023a998SMauro Carvalho Chehab vpu_cfg_readl(vpu, VPU_WDT_REG));
2852023a998SMauro Carvalho Chehab }
2862023a998SMauro Carvalho Chehab
vpu_ipi_register(struct platform_device * pdev,enum ipi_id id,ipi_handler_t handler,const char * name,void * priv)2872023a998SMauro Carvalho Chehab int vpu_ipi_register(struct platform_device *pdev,
2882023a998SMauro Carvalho Chehab enum ipi_id id, ipi_handler_t handler,
2892023a998SMauro Carvalho Chehab const char *name, void *priv)
2902023a998SMauro Carvalho Chehab {
2912023a998SMauro Carvalho Chehab struct mtk_vpu *vpu = platform_get_drvdata(pdev);
2922023a998SMauro Carvalho Chehab struct vpu_ipi_desc *ipi_desc;
2932023a998SMauro Carvalho Chehab
2942023a998SMauro Carvalho Chehab if (!vpu) {
2952023a998SMauro Carvalho Chehab dev_err(&pdev->dev, "vpu device in not ready\n");
2962023a998SMauro Carvalho Chehab return -EPROBE_DEFER;
2972023a998SMauro Carvalho Chehab }
2982023a998SMauro Carvalho Chehab
2992023a998SMauro Carvalho Chehab if (id < IPI_MAX && handler) {
3002023a998SMauro Carvalho Chehab ipi_desc = vpu->ipi_desc;
3012023a998SMauro Carvalho Chehab ipi_desc[id].name = name;
3022023a998SMauro Carvalho Chehab ipi_desc[id].handler = handler;
3032023a998SMauro Carvalho Chehab ipi_desc[id].priv = priv;
3042023a998SMauro Carvalho Chehab return 0;
3052023a998SMauro Carvalho Chehab }
3062023a998SMauro Carvalho Chehab
3072023a998SMauro Carvalho Chehab dev_err(&pdev->dev, "register vpu ipi id %d with invalid arguments\n",
3082023a998SMauro Carvalho Chehab id);
3092023a998SMauro Carvalho Chehab return -EINVAL;
3102023a998SMauro Carvalho Chehab }
3112023a998SMauro Carvalho Chehab EXPORT_SYMBOL_GPL(vpu_ipi_register);
3122023a998SMauro Carvalho Chehab
vpu_ipi_send(struct platform_device * pdev,enum ipi_id id,void * buf,unsigned int len)3132023a998SMauro Carvalho Chehab int vpu_ipi_send(struct platform_device *pdev,
3142023a998SMauro Carvalho Chehab enum ipi_id id, void *buf,
3152023a998SMauro Carvalho Chehab unsigned int len)
3162023a998SMauro Carvalho Chehab {
3172023a998SMauro Carvalho Chehab struct mtk_vpu *vpu = platform_get_drvdata(pdev);
3182023a998SMauro Carvalho Chehab struct share_obj __iomem *send_obj = vpu->send_buf;
3192023a998SMauro Carvalho Chehab unsigned long timeout;
3202023a998SMauro Carvalho Chehab int ret = 0;
3212023a998SMauro Carvalho Chehab
3222023a998SMauro Carvalho Chehab if (id <= IPI_VPU_INIT || id >= IPI_MAX ||
3232023a998SMauro Carvalho Chehab len > sizeof(send_obj->share_buf) || !buf) {
3242023a998SMauro Carvalho Chehab dev_err(vpu->dev, "failed to send ipi message\n");
3252023a998SMauro Carvalho Chehab return -EINVAL;
3262023a998SMauro Carvalho Chehab }
3272023a998SMauro Carvalho Chehab
3282023a998SMauro Carvalho Chehab ret = vpu_clock_enable(vpu);
3292023a998SMauro Carvalho Chehab if (ret) {
3302023a998SMauro Carvalho Chehab dev_err(vpu->dev, "failed to enable vpu clock\n");
3312023a998SMauro Carvalho Chehab return ret;
3322023a998SMauro Carvalho Chehab }
3332023a998SMauro Carvalho Chehab if (!vpu_running(vpu)) {
3342023a998SMauro Carvalho Chehab dev_err(vpu->dev, "vpu_ipi_send: VPU is not running\n");
3352023a998SMauro Carvalho Chehab ret = -EINVAL;
3362023a998SMauro Carvalho Chehab goto clock_disable;
3372023a998SMauro Carvalho Chehab }
3382023a998SMauro Carvalho Chehab
3392023a998SMauro Carvalho Chehab mutex_lock(&vpu->vpu_mutex);
3402023a998SMauro Carvalho Chehab
3412023a998SMauro Carvalho Chehab /* Wait until VPU receives the last command */
3422023a998SMauro Carvalho Chehab timeout = jiffies + msecs_to_jiffies(IPI_TIMEOUT_MS);
3432023a998SMauro Carvalho Chehab do {
3442023a998SMauro Carvalho Chehab if (time_after(jiffies, timeout)) {
3452023a998SMauro Carvalho Chehab dev_err(vpu->dev, "vpu_ipi_send: IPI timeout!\n");
3462023a998SMauro Carvalho Chehab ret = -EIO;
3472023a998SMauro Carvalho Chehab vpu_dump_status(vpu);
3482023a998SMauro Carvalho Chehab goto mut_unlock;
3492023a998SMauro Carvalho Chehab }
3502023a998SMauro Carvalho Chehab } while (vpu_cfg_readl(vpu, HOST_TO_VPU));
3512023a998SMauro Carvalho Chehab
3522023a998SMauro Carvalho Chehab memcpy_toio(send_obj->share_buf, buf, len);
3532023a998SMauro Carvalho Chehab writel(len, &send_obj->len);
3542023a998SMauro Carvalho Chehab writel(id, &send_obj->id);
3552023a998SMauro Carvalho Chehab
3562023a998SMauro Carvalho Chehab vpu->ipi_id_ack[id] = false;
3572023a998SMauro Carvalho Chehab /* send the command to VPU */
3582023a998SMauro Carvalho Chehab vpu_cfg_writel(vpu, 0x1, HOST_TO_VPU);
3592023a998SMauro Carvalho Chehab
3602023a998SMauro Carvalho Chehab mutex_unlock(&vpu->vpu_mutex);
3612023a998SMauro Carvalho Chehab
3622023a998SMauro Carvalho Chehab /* wait for VPU's ACK */
3632023a998SMauro Carvalho Chehab timeout = msecs_to_jiffies(IPI_TIMEOUT_MS);
3642023a998SMauro Carvalho Chehab ret = wait_event_timeout(vpu->ack_wq, vpu->ipi_id_ack[id], timeout);
3652023a998SMauro Carvalho Chehab vpu->ipi_id_ack[id] = false;
3662023a998SMauro Carvalho Chehab if (ret == 0) {
3672023a998SMauro Carvalho Chehab dev_err(vpu->dev, "vpu ipi %d ack time out !\n", id);
3682023a998SMauro Carvalho Chehab ret = -EIO;
3692023a998SMauro Carvalho Chehab vpu_dump_status(vpu);
3702023a998SMauro Carvalho Chehab goto clock_disable;
3712023a998SMauro Carvalho Chehab }
3722023a998SMauro Carvalho Chehab vpu_clock_disable(vpu);
3732023a998SMauro Carvalho Chehab
3742023a998SMauro Carvalho Chehab return 0;
3752023a998SMauro Carvalho Chehab
3762023a998SMauro Carvalho Chehab mut_unlock:
3772023a998SMauro Carvalho Chehab mutex_unlock(&vpu->vpu_mutex);
3782023a998SMauro Carvalho Chehab clock_disable:
3792023a998SMauro Carvalho Chehab vpu_clock_disable(vpu);
3802023a998SMauro Carvalho Chehab
3812023a998SMauro Carvalho Chehab return ret;
3822023a998SMauro Carvalho Chehab }
3832023a998SMauro Carvalho Chehab EXPORT_SYMBOL_GPL(vpu_ipi_send);
3842023a998SMauro Carvalho Chehab
vpu_wdt_reset_func(struct work_struct * ws)3852023a998SMauro Carvalho Chehab static void vpu_wdt_reset_func(struct work_struct *ws)
3862023a998SMauro Carvalho Chehab {
3872023a998SMauro Carvalho Chehab struct vpu_wdt *wdt = container_of(ws, struct vpu_wdt, ws);
3882023a998SMauro Carvalho Chehab struct mtk_vpu *vpu = container_of(wdt, struct mtk_vpu, wdt);
3892023a998SMauro Carvalho Chehab struct vpu_wdt_handler *handler = wdt->handler;
3902023a998SMauro Carvalho Chehab int index, ret;
3912023a998SMauro Carvalho Chehab
3922023a998SMauro Carvalho Chehab dev_info(vpu->dev, "vpu reset\n");
3932023a998SMauro Carvalho Chehab ret = vpu_clock_enable(vpu);
3942023a998SMauro Carvalho Chehab if (ret) {
3952023a998SMauro Carvalho Chehab dev_err(vpu->dev, "[VPU] wdt enables clock failed %d\n", ret);
3962023a998SMauro Carvalho Chehab return;
3972023a998SMauro Carvalho Chehab }
3982023a998SMauro Carvalho Chehab mutex_lock(&vpu->vpu_mutex);
3992023a998SMauro Carvalho Chehab vpu_cfg_writel(vpu, 0x0, VPU_RESET);
4002023a998SMauro Carvalho Chehab vpu->fw_loaded = false;
4012023a998SMauro Carvalho Chehab mutex_unlock(&vpu->vpu_mutex);
4022023a998SMauro Carvalho Chehab vpu_clock_disable(vpu);
4032023a998SMauro Carvalho Chehab
4042023a998SMauro Carvalho Chehab for (index = 0; index < VPU_RST_MAX; index++) {
4052023a998SMauro Carvalho Chehab if (handler[index].reset_func) {
4062023a998SMauro Carvalho Chehab handler[index].reset_func(handler[index].priv);
4072023a998SMauro Carvalho Chehab dev_dbg(vpu->dev, "wdt handler func %d\n", index);
4082023a998SMauro Carvalho Chehab }
4092023a998SMauro Carvalho Chehab }
4102023a998SMauro Carvalho Chehab }
4112023a998SMauro Carvalho Chehab
vpu_wdt_reg_handler(struct platform_device * pdev,void wdt_reset (void *),void * priv,enum rst_id id)4122023a998SMauro Carvalho Chehab int vpu_wdt_reg_handler(struct platform_device *pdev,
4132023a998SMauro Carvalho Chehab void wdt_reset(void *),
4142023a998SMauro Carvalho Chehab void *priv, enum rst_id id)
4152023a998SMauro Carvalho Chehab {
4162023a998SMauro Carvalho Chehab struct mtk_vpu *vpu = platform_get_drvdata(pdev);
4172023a998SMauro Carvalho Chehab struct vpu_wdt_handler *handler;
4182023a998SMauro Carvalho Chehab
4192023a998SMauro Carvalho Chehab if (!vpu) {
4202023a998SMauro Carvalho Chehab dev_err(&pdev->dev, "vpu device in not ready\n");
4212023a998SMauro Carvalho Chehab return -EPROBE_DEFER;
4222023a998SMauro Carvalho Chehab }
4232023a998SMauro Carvalho Chehab
4242023a998SMauro Carvalho Chehab handler = vpu->wdt.handler;
4252023a998SMauro Carvalho Chehab
4262023a998SMauro Carvalho Chehab if (id < VPU_RST_MAX && wdt_reset) {
4272023a998SMauro Carvalho Chehab dev_dbg(vpu->dev, "wdt register id %d\n", id);
4282023a998SMauro Carvalho Chehab mutex_lock(&vpu->vpu_mutex);
4292023a998SMauro Carvalho Chehab handler[id].reset_func = wdt_reset;
4302023a998SMauro Carvalho Chehab handler[id].priv = priv;
4312023a998SMauro Carvalho Chehab mutex_unlock(&vpu->vpu_mutex);
4322023a998SMauro Carvalho Chehab return 0;
4332023a998SMauro Carvalho Chehab }
4342023a998SMauro Carvalho Chehab
4352023a998SMauro Carvalho Chehab dev_err(vpu->dev, "register vpu wdt handler failed\n");
4362023a998SMauro Carvalho Chehab return -EINVAL;
4372023a998SMauro Carvalho Chehab }
4382023a998SMauro Carvalho Chehab EXPORT_SYMBOL_GPL(vpu_wdt_reg_handler);
4392023a998SMauro Carvalho Chehab
vpu_get_vdec_hw_capa(struct platform_device * pdev)4402023a998SMauro Carvalho Chehab unsigned int vpu_get_vdec_hw_capa(struct platform_device *pdev)
4412023a998SMauro Carvalho Chehab {
4422023a998SMauro Carvalho Chehab struct mtk_vpu *vpu = platform_get_drvdata(pdev);
4432023a998SMauro Carvalho Chehab
4442023a998SMauro Carvalho Chehab return vpu->run.dec_capability;
4452023a998SMauro Carvalho Chehab }
4462023a998SMauro Carvalho Chehab EXPORT_SYMBOL_GPL(vpu_get_vdec_hw_capa);
4472023a998SMauro Carvalho Chehab
vpu_get_venc_hw_capa(struct platform_device * pdev)4482023a998SMauro Carvalho Chehab unsigned int vpu_get_venc_hw_capa(struct platform_device *pdev)
4492023a998SMauro Carvalho Chehab {
4502023a998SMauro Carvalho Chehab struct mtk_vpu *vpu = platform_get_drvdata(pdev);
4512023a998SMauro Carvalho Chehab
4522023a998SMauro Carvalho Chehab return vpu->run.enc_capability;
4532023a998SMauro Carvalho Chehab }
4542023a998SMauro Carvalho Chehab EXPORT_SYMBOL_GPL(vpu_get_venc_hw_capa);
4552023a998SMauro Carvalho Chehab
vpu_mapping_dm_addr(struct platform_device * pdev,u32 dtcm_dmem_addr)4562023a998SMauro Carvalho Chehab void *vpu_mapping_dm_addr(struct platform_device *pdev,
4572023a998SMauro Carvalho Chehab u32 dtcm_dmem_addr)
4582023a998SMauro Carvalho Chehab {
4592023a998SMauro Carvalho Chehab struct mtk_vpu *vpu = platform_get_drvdata(pdev);
4602023a998SMauro Carvalho Chehab
4612023a998SMauro Carvalho Chehab if (!dtcm_dmem_addr ||
4622023a998SMauro Carvalho Chehab (dtcm_dmem_addr > (VPU_DTCM_SIZE + VPU_EXT_D_SIZE))) {
4632023a998SMauro Carvalho Chehab dev_err(vpu->dev, "invalid virtual data memory address\n");
4642023a998SMauro Carvalho Chehab return ERR_PTR(-EINVAL);
4652023a998SMauro Carvalho Chehab }
4662023a998SMauro Carvalho Chehab
4672023a998SMauro Carvalho Chehab if (dtcm_dmem_addr < VPU_DTCM_SIZE)
4682023a998SMauro Carvalho Chehab return (__force void *)(dtcm_dmem_addr + vpu->reg.tcm +
4692023a998SMauro Carvalho Chehab VPU_DTCM_OFFSET);
4702023a998SMauro Carvalho Chehab
4712023a998SMauro Carvalho Chehab return vpu->extmem[D_FW].va + (dtcm_dmem_addr - VPU_DTCM_SIZE);
4722023a998SMauro Carvalho Chehab }
4732023a998SMauro Carvalho Chehab EXPORT_SYMBOL_GPL(vpu_mapping_dm_addr);
4742023a998SMauro Carvalho Chehab
vpu_get_plat_device(struct platform_device * pdev)4752023a998SMauro Carvalho Chehab struct platform_device *vpu_get_plat_device(struct platform_device *pdev)
4762023a998SMauro Carvalho Chehab {
4772023a998SMauro Carvalho Chehab struct device *dev = &pdev->dev;
4782023a998SMauro Carvalho Chehab struct device_node *vpu_node;
4792023a998SMauro Carvalho Chehab struct platform_device *vpu_pdev;
4802023a998SMauro Carvalho Chehab
4812023a998SMauro Carvalho Chehab vpu_node = of_parse_phandle(dev->of_node, "mediatek,vpu", 0);
4822023a998SMauro Carvalho Chehab if (!vpu_node) {
4832023a998SMauro Carvalho Chehab dev_err(dev, "can't get vpu node\n");
4842023a998SMauro Carvalho Chehab return NULL;
4852023a998SMauro Carvalho Chehab }
4862023a998SMauro Carvalho Chehab
4872023a998SMauro Carvalho Chehab vpu_pdev = of_find_device_by_node(vpu_node);
4882023a998SMauro Carvalho Chehab of_node_put(vpu_node);
4892023a998SMauro Carvalho Chehab if (WARN_ON(!vpu_pdev)) {
4902023a998SMauro Carvalho Chehab dev_err(dev, "vpu pdev failed\n");
4912023a998SMauro Carvalho Chehab return NULL;
4922023a998SMauro Carvalho Chehab }
4932023a998SMauro Carvalho Chehab
4942023a998SMauro Carvalho Chehab return vpu_pdev;
4952023a998SMauro Carvalho Chehab }
4962023a998SMauro Carvalho Chehab EXPORT_SYMBOL_GPL(vpu_get_plat_device);
4972023a998SMauro Carvalho Chehab
4982023a998SMauro Carvalho Chehab /* load vpu program/data memory */
load_requested_vpu(struct mtk_vpu * vpu,u8 fw_type)4992023a998SMauro Carvalho Chehab static int load_requested_vpu(struct mtk_vpu *vpu,
5002023a998SMauro Carvalho Chehab u8 fw_type)
5012023a998SMauro Carvalho Chehab {
5022023a998SMauro Carvalho Chehab size_t tcm_size = fw_type ? VPU_DTCM_SIZE : VPU_PTCM_SIZE;
5032023a998SMauro Carvalho Chehab size_t fw_size = fw_type ? VPU_D_FW_SIZE : VPU_P_FW_SIZE;
5042023a998SMauro Carvalho Chehab char *fw_name = fw_type ? VPU_D_FW : VPU_P_FW;
5052023a998SMauro Carvalho Chehab char *fw_new_name = fw_type ? VPU_D_FW_NEW : VPU_P_FW_NEW;
5062023a998SMauro Carvalho Chehab const struct firmware *vpu_fw;
5072023a998SMauro Carvalho Chehab size_t dl_size = 0;
5082023a998SMauro Carvalho Chehab size_t extra_fw_size = 0;
5092023a998SMauro Carvalho Chehab void *dest;
5102023a998SMauro Carvalho Chehab int ret;
5112023a998SMauro Carvalho Chehab
5122023a998SMauro Carvalho Chehab ret = request_firmware(&vpu_fw, fw_new_name, vpu->dev);
5132023a998SMauro Carvalho Chehab if (ret < 0) {
5142023a998SMauro Carvalho Chehab dev_info(vpu->dev, "Failed to load %s, %d, retry\n",
5152023a998SMauro Carvalho Chehab fw_new_name, ret);
5162023a998SMauro Carvalho Chehab
5172023a998SMauro Carvalho Chehab ret = request_firmware(&vpu_fw, fw_name, vpu->dev);
5182023a998SMauro Carvalho Chehab if (ret < 0) {
5192023a998SMauro Carvalho Chehab dev_err(vpu->dev, "Failed to load %s, %d\n", fw_name,
5202023a998SMauro Carvalho Chehab ret);
5212023a998SMauro Carvalho Chehab return ret;
5222023a998SMauro Carvalho Chehab }
5232023a998SMauro Carvalho Chehab }
5242023a998SMauro Carvalho Chehab dl_size = vpu_fw->size;
5252023a998SMauro Carvalho Chehab if (dl_size > fw_size) {
5262023a998SMauro Carvalho Chehab dev_err(vpu->dev, "fw %s size %zu is abnormal\n", fw_name,
5272023a998SMauro Carvalho Chehab dl_size);
5282023a998SMauro Carvalho Chehab release_firmware(vpu_fw);
5292023a998SMauro Carvalho Chehab return -EFBIG;
5302023a998SMauro Carvalho Chehab }
5312023a998SMauro Carvalho Chehab dev_dbg(vpu->dev, "Downloaded fw %s size: %zu.\n",
5322023a998SMauro Carvalho Chehab fw_name,
5332023a998SMauro Carvalho Chehab dl_size);
5342023a998SMauro Carvalho Chehab /* reset VPU */
5352023a998SMauro Carvalho Chehab vpu_cfg_writel(vpu, 0x0, VPU_RESET);
5362023a998SMauro Carvalho Chehab
5372023a998SMauro Carvalho Chehab /* handle extended firmware size */
5382023a998SMauro Carvalho Chehab if (dl_size > tcm_size) {
5392023a998SMauro Carvalho Chehab dev_dbg(vpu->dev, "fw size %zu > limited fw size %zu\n",
5402023a998SMauro Carvalho Chehab dl_size, tcm_size);
5412023a998SMauro Carvalho Chehab extra_fw_size = dl_size - tcm_size;
5422023a998SMauro Carvalho Chehab dev_dbg(vpu->dev, "extra_fw_size %zu\n", extra_fw_size);
5432023a998SMauro Carvalho Chehab dl_size = tcm_size;
5442023a998SMauro Carvalho Chehab }
5452023a998SMauro Carvalho Chehab dest = (__force void *)vpu->reg.tcm;
5462023a998SMauro Carvalho Chehab if (fw_type == D_FW)
5472023a998SMauro Carvalho Chehab dest += VPU_DTCM_OFFSET;
5482023a998SMauro Carvalho Chehab memcpy(dest, vpu_fw->data, dl_size);
5492023a998SMauro Carvalho Chehab /* download to extended memory if need */
5502023a998SMauro Carvalho Chehab if (extra_fw_size > 0) {
5512023a998SMauro Carvalho Chehab dest = vpu->extmem[fw_type].va;
5522023a998SMauro Carvalho Chehab dev_dbg(vpu->dev, "download extended memory type %x\n",
5532023a998SMauro Carvalho Chehab fw_type);
5542023a998SMauro Carvalho Chehab memcpy(dest, vpu_fw->data + tcm_size, extra_fw_size);
5552023a998SMauro Carvalho Chehab }
5562023a998SMauro Carvalho Chehab
5572023a998SMauro Carvalho Chehab release_firmware(vpu_fw);
5582023a998SMauro Carvalho Chehab
5592023a998SMauro Carvalho Chehab return 0;
5602023a998SMauro Carvalho Chehab }
5612023a998SMauro Carvalho Chehab
vpu_load_firmware(struct platform_device * pdev)5622023a998SMauro Carvalho Chehab int vpu_load_firmware(struct platform_device *pdev)
5632023a998SMauro Carvalho Chehab {
5642023a998SMauro Carvalho Chehab struct mtk_vpu *vpu;
5653df55cd7SHans Verkuil struct device *dev;
5662023a998SMauro Carvalho Chehab struct vpu_run *run;
5672023a998SMauro Carvalho Chehab int ret;
5682023a998SMauro Carvalho Chehab
5692023a998SMauro Carvalho Chehab if (!pdev) {
5703df55cd7SHans Verkuil pr_err("VPU platform device is invalid\n");
5712023a998SMauro Carvalho Chehab return -EINVAL;
5722023a998SMauro Carvalho Chehab }
5732023a998SMauro Carvalho Chehab
5743df55cd7SHans Verkuil dev = &pdev->dev;
5753df55cd7SHans Verkuil
5762023a998SMauro Carvalho Chehab vpu = platform_get_drvdata(pdev);
5772023a998SMauro Carvalho Chehab run = &vpu->run;
5782023a998SMauro Carvalho Chehab
5792023a998SMauro Carvalho Chehab mutex_lock(&vpu->vpu_mutex);
5802023a998SMauro Carvalho Chehab if (vpu->fw_loaded) {
5812023a998SMauro Carvalho Chehab mutex_unlock(&vpu->vpu_mutex);
5822023a998SMauro Carvalho Chehab return 0;
5832023a998SMauro Carvalho Chehab }
5842023a998SMauro Carvalho Chehab mutex_unlock(&vpu->vpu_mutex);
5852023a998SMauro Carvalho Chehab
5862023a998SMauro Carvalho Chehab ret = vpu_clock_enable(vpu);
5872023a998SMauro Carvalho Chehab if (ret) {
5882023a998SMauro Carvalho Chehab dev_err(dev, "enable clock failed %d\n", ret);
5892023a998SMauro Carvalho Chehab return ret;
5902023a998SMauro Carvalho Chehab }
5912023a998SMauro Carvalho Chehab
5922023a998SMauro Carvalho Chehab mutex_lock(&vpu->vpu_mutex);
5932023a998SMauro Carvalho Chehab
5942023a998SMauro Carvalho Chehab run->signaled = false;
5952023a998SMauro Carvalho Chehab dev_dbg(vpu->dev, "firmware request\n");
5962023a998SMauro Carvalho Chehab /* Downloading program firmware to device*/
5972023a998SMauro Carvalho Chehab ret = load_requested_vpu(vpu, P_FW);
5982023a998SMauro Carvalho Chehab if (ret < 0) {
5992023a998SMauro Carvalho Chehab dev_err(dev, "Failed to request %s, %d\n", VPU_P_FW, ret);
6002023a998SMauro Carvalho Chehab goto OUT_LOAD_FW;
6012023a998SMauro Carvalho Chehab }
6022023a998SMauro Carvalho Chehab
6032023a998SMauro Carvalho Chehab /* Downloading data firmware to device */
6042023a998SMauro Carvalho Chehab ret = load_requested_vpu(vpu, D_FW);
6052023a998SMauro Carvalho Chehab if (ret < 0) {
6062023a998SMauro Carvalho Chehab dev_err(dev, "Failed to request %s, %d\n", VPU_D_FW, ret);
6072023a998SMauro Carvalho Chehab goto OUT_LOAD_FW;
6082023a998SMauro Carvalho Chehab }
6092023a998SMauro Carvalho Chehab
6102023a998SMauro Carvalho Chehab vpu->fw_loaded = true;
6112023a998SMauro Carvalho Chehab /* boot up vpu */
6122023a998SMauro Carvalho Chehab vpu_cfg_writel(vpu, 0x1, VPU_RESET);
6132023a998SMauro Carvalho Chehab
6142023a998SMauro Carvalho Chehab ret = wait_event_interruptible_timeout(run->wq,
6152023a998SMauro Carvalho Chehab run->signaled,
6162023a998SMauro Carvalho Chehab msecs_to_jiffies(INIT_TIMEOUT_MS)
6172023a998SMauro Carvalho Chehab );
6182023a998SMauro Carvalho Chehab if (ret == 0) {
6192023a998SMauro Carvalho Chehab ret = -ETIME;
6202023a998SMauro Carvalho Chehab dev_err(dev, "wait vpu initialization timeout!\n");
6212023a998SMauro Carvalho Chehab goto OUT_LOAD_FW;
6222023a998SMauro Carvalho Chehab } else if (-ERESTARTSYS == ret) {
6232023a998SMauro Carvalho Chehab dev_err(dev, "wait vpu interrupted by a signal!\n");
6242023a998SMauro Carvalho Chehab goto OUT_LOAD_FW;
6252023a998SMauro Carvalho Chehab }
6262023a998SMauro Carvalho Chehab
6272023a998SMauro Carvalho Chehab ret = 0;
6282023a998SMauro Carvalho Chehab dev_info(dev, "vpu is ready. Fw version %s\n", run->fw_ver);
6292023a998SMauro Carvalho Chehab
6302023a998SMauro Carvalho Chehab OUT_LOAD_FW:
6312023a998SMauro Carvalho Chehab mutex_unlock(&vpu->vpu_mutex);
6322023a998SMauro Carvalho Chehab vpu_clock_disable(vpu);
6332023a998SMauro Carvalho Chehab
6342023a998SMauro Carvalho Chehab return ret;
6352023a998SMauro Carvalho Chehab }
6362023a998SMauro Carvalho Chehab EXPORT_SYMBOL_GPL(vpu_load_firmware);
6372023a998SMauro Carvalho Chehab
vpu_init_ipi_handler(void * data,unsigned int len,void * priv)638*bfb1b998SArnd Bergmann static void vpu_init_ipi_handler(void *data, unsigned int len, void *priv)
6392023a998SMauro Carvalho Chehab {
6402023a998SMauro Carvalho Chehab struct mtk_vpu *vpu = priv;
6412023a998SMauro Carvalho Chehab const struct vpu_run *run = data;
6422023a998SMauro Carvalho Chehab
6432023a998SMauro Carvalho Chehab vpu->run.signaled = run->signaled;
6442023a998SMauro Carvalho Chehab strscpy(vpu->run.fw_ver, run->fw_ver, sizeof(vpu->run.fw_ver));
6452023a998SMauro Carvalho Chehab vpu->run.dec_capability = run->dec_capability;
6462023a998SMauro Carvalho Chehab vpu->run.enc_capability = run->enc_capability;
6472023a998SMauro Carvalho Chehab wake_up_interruptible(&vpu->run.wq);
6482023a998SMauro Carvalho Chehab }
6492023a998SMauro Carvalho Chehab
6502023a998SMauro Carvalho Chehab #ifdef CONFIG_DEBUG_FS
vpu_debug_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)6512023a998SMauro Carvalho Chehab static ssize_t vpu_debug_read(struct file *file, char __user *user_buf,
6522023a998SMauro Carvalho Chehab size_t count, loff_t *ppos)
6532023a998SMauro Carvalho Chehab {
6542023a998SMauro Carvalho Chehab char buf[256];
6552023a998SMauro Carvalho Chehab unsigned int len;
6562023a998SMauro Carvalho Chehab unsigned int running, pc, vpu_to_host, host_to_vpu, wdt, idle, ra, sp;
6572023a998SMauro Carvalho Chehab int ret;
6582023a998SMauro Carvalho Chehab struct device *dev = file->private_data;
6592023a998SMauro Carvalho Chehab struct mtk_vpu *vpu = dev_get_drvdata(dev);
6602023a998SMauro Carvalho Chehab
6612023a998SMauro Carvalho Chehab ret = vpu_clock_enable(vpu);
6622023a998SMauro Carvalho Chehab if (ret) {
6632023a998SMauro Carvalho Chehab dev_err(vpu->dev, "[VPU] enable clock failed %d\n", ret);
6642023a998SMauro Carvalho Chehab return 0;
6652023a998SMauro Carvalho Chehab }
6662023a998SMauro Carvalho Chehab
6672023a998SMauro Carvalho Chehab /* vpu register status */
6682023a998SMauro Carvalho Chehab running = vpu_running(vpu);
6692023a998SMauro Carvalho Chehab pc = vpu_cfg_readl(vpu, VPU_PC_REG);
6702023a998SMauro Carvalho Chehab wdt = vpu_cfg_readl(vpu, VPU_WDT_REG);
6712023a998SMauro Carvalho Chehab host_to_vpu = vpu_cfg_readl(vpu, HOST_TO_VPU);
6722023a998SMauro Carvalho Chehab vpu_to_host = vpu_cfg_readl(vpu, VPU_TO_HOST);
6732023a998SMauro Carvalho Chehab ra = vpu_cfg_readl(vpu, VPU_RA_REG);
6742023a998SMauro Carvalho Chehab sp = vpu_cfg_readl(vpu, VPU_SP_REG);
6752023a998SMauro Carvalho Chehab idle = vpu_cfg_readl(vpu, VPU_IDLE_REG);
6762023a998SMauro Carvalho Chehab
6772023a998SMauro Carvalho Chehab vpu_clock_disable(vpu);
6782023a998SMauro Carvalho Chehab
6792023a998SMauro Carvalho Chehab if (running) {
6802023a998SMauro Carvalho Chehab len = snprintf(buf, sizeof(buf), "VPU is running\n\n"
6812023a998SMauro Carvalho Chehab "FW Version: %s\n"
6822023a998SMauro Carvalho Chehab "PC: 0x%x\n"
6832023a998SMauro Carvalho Chehab "WDT: 0x%x\n"
6842023a998SMauro Carvalho Chehab "Host to VPU: 0x%x\n"
6852023a998SMauro Carvalho Chehab "VPU to Host: 0x%x\n"
6862023a998SMauro Carvalho Chehab "SP: 0x%x\n"
6872023a998SMauro Carvalho Chehab "RA: 0x%x\n"
6882023a998SMauro Carvalho Chehab "idle: 0x%x\n",
6892023a998SMauro Carvalho Chehab vpu->run.fw_ver, pc, wdt,
6902023a998SMauro Carvalho Chehab host_to_vpu, vpu_to_host, sp, ra, idle);
6912023a998SMauro Carvalho Chehab } else {
6922023a998SMauro Carvalho Chehab len = snprintf(buf, sizeof(buf), "VPU not running\n");
6932023a998SMauro Carvalho Chehab }
6942023a998SMauro Carvalho Chehab
6952023a998SMauro Carvalho Chehab return simple_read_from_buffer(user_buf, count, ppos, buf, len);
6962023a998SMauro Carvalho Chehab }
6972023a998SMauro Carvalho Chehab
6982023a998SMauro Carvalho Chehab static const struct file_operations vpu_debug_fops = {
6992023a998SMauro Carvalho Chehab .open = simple_open,
7002023a998SMauro Carvalho Chehab .read = vpu_debug_read,
7012023a998SMauro Carvalho Chehab };
7022023a998SMauro Carvalho Chehab #endif /* CONFIG_DEBUG_FS */
7032023a998SMauro Carvalho Chehab
vpu_free_ext_mem(struct mtk_vpu * vpu,u8 fw_type)7042023a998SMauro Carvalho Chehab static void vpu_free_ext_mem(struct mtk_vpu *vpu, u8 fw_type)
7052023a998SMauro Carvalho Chehab {
7062023a998SMauro Carvalho Chehab struct device *dev = vpu->dev;
7072023a998SMauro Carvalho Chehab size_t fw_ext_size = fw_type ? VPU_EXT_D_SIZE : VPU_EXT_P_SIZE;
7082023a998SMauro Carvalho Chehab
7092023a998SMauro Carvalho Chehab dma_free_coherent(dev, fw_ext_size, vpu->extmem[fw_type].va,
7102023a998SMauro Carvalho Chehab vpu->extmem[fw_type].pa);
7112023a998SMauro Carvalho Chehab }
7122023a998SMauro Carvalho Chehab
vpu_alloc_ext_mem(struct mtk_vpu * vpu,u32 fw_type)7132023a998SMauro Carvalho Chehab static int vpu_alloc_ext_mem(struct mtk_vpu *vpu, u32 fw_type)
7142023a998SMauro Carvalho Chehab {
7152023a998SMauro Carvalho Chehab struct device *dev = vpu->dev;
7162023a998SMauro Carvalho Chehab size_t fw_ext_size = fw_type ? VPU_EXT_D_SIZE : VPU_EXT_P_SIZE;
7172023a998SMauro Carvalho Chehab u32 vpu_ext_mem0 = fw_type ? VPU_DMEM_EXT0_ADDR : VPU_PMEM_EXT0_ADDR;
7182023a998SMauro Carvalho Chehab u32 vpu_ext_mem1 = fw_type ? VPU_DMEM_EXT1_ADDR : VPU_PMEM_EXT1_ADDR;
7192023a998SMauro Carvalho Chehab u32 offset_4gb = vpu->enable_4GB ? 0x40000000 : 0;
7202023a998SMauro Carvalho Chehab
7212023a998SMauro Carvalho Chehab vpu->extmem[fw_type].va = dma_alloc_coherent(dev,
7222023a998SMauro Carvalho Chehab fw_ext_size,
7232023a998SMauro Carvalho Chehab &vpu->extmem[fw_type].pa,
7242023a998SMauro Carvalho Chehab GFP_KERNEL);
7252023a998SMauro Carvalho Chehab if (!vpu->extmem[fw_type].va) {
7262023a998SMauro Carvalho Chehab dev_err(dev, "Failed to allocate the extended program memory\n");
7272023a998SMauro Carvalho Chehab return -ENOMEM;
7282023a998SMauro Carvalho Chehab }
7292023a998SMauro Carvalho Chehab
7302023a998SMauro Carvalho Chehab /* Disable extend0. Enable extend1 */
7312023a998SMauro Carvalho Chehab vpu_cfg_writel(vpu, 0x1, vpu_ext_mem0);
7322023a998SMauro Carvalho Chehab vpu_cfg_writel(vpu, (vpu->extmem[fw_type].pa & 0xFFFFF000) + offset_4gb,
7332023a998SMauro Carvalho Chehab vpu_ext_mem1);
7342023a998SMauro Carvalho Chehab
7352023a998SMauro Carvalho Chehab dev_info(dev, "%s extend memory phy=0x%llx virt=0x%p\n",
7362023a998SMauro Carvalho Chehab fw_type ? "Data" : "Program",
7372023a998SMauro Carvalho Chehab (unsigned long long)vpu->extmem[fw_type].pa,
7382023a998SMauro Carvalho Chehab vpu->extmem[fw_type].va);
7392023a998SMauro Carvalho Chehab
7402023a998SMauro Carvalho Chehab return 0;
7412023a998SMauro Carvalho Chehab }
7422023a998SMauro Carvalho Chehab
vpu_ipi_handler(struct mtk_vpu * vpu)7432023a998SMauro Carvalho Chehab static void vpu_ipi_handler(struct mtk_vpu *vpu)
7442023a998SMauro Carvalho Chehab {
7452023a998SMauro Carvalho Chehab struct share_obj __iomem *rcv_obj = vpu->recv_buf;
7462023a998SMauro Carvalho Chehab struct vpu_ipi_desc *ipi_desc = vpu->ipi_desc;
7472023a998SMauro Carvalho Chehab unsigned char data[SHARE_BUF_SIZE];
7482023a998SMauro Carvalho Chehab s32 id = readl(&rcv_obj->id);
7492023a998SMauro Carvalho Chehab
7502023a998SMauro Carvalho Chehab memcpy_fromio(data, rcv_obj->share_buf, sizeof(data));
7512023a998SMauro Carvalho Chehab if (id < IPI_MAX && ipi_desc[id].handler) {
7522023a998SMauro Carvalho Chehab ipi_desc[id].handler(data, readl(&rcv_obj->len),
7532023a998SMauro Carvalho Chehab ipi_desc[id].priv);
7542023a998SMauro Carvalho Chehab if (id > IPI_VPU_INIT) {
7552023a998SMauro Carvalho Chehab vpu->ipi_id_ack[id] = true;
7562023a998SMauro Carvalho Chehab wake_up(&vpu->ack_wq);
7572023a998SMauro Carvalho Chehab }
7582023a998SMauro Carvalho Chehab } else {
7592023a998SMauro Carvalho Chehab dev_err(vpu->dev, "No such ipi id = %d\n", id);
7602023a998SMauro Carvalho Chehab }
7612023a998SMauro Carvalho Chehab }
7622023a998SMauro Carvalho Chehab
vpu_ipi_init(struct mtk_vpu * vpu)7632023a998SMauro Carvalho Chehab static int vpu_ipi_init(struct mtk_vpu *vpu)
7642023a998SMauro Carvalho Chehab {
7652023a998SMauro Carvalho Chehab /* Disable VPU to host interrupt */
7662023a998SMauro Carvalho Chehab vpu_cfg_writel(vpu, 0x0, VPU_TO_HOST);
7672023a998SMauro Carvalho Chehab
7682023a998SMauro Carvalho Chehab /* shared buffer initialization */
7692023a998SMauro Carvalho Chehab vpu->recv_buf = vpu->reg.tcm + VPU_DTCM_OFFSET;
7702023a998SMauro Carvalho Chehab vpu->send_buf = vpu->recv_buf + 1;
7712023a998SMauro Carvalho Chehab memset_io(vpu->recv_buf, 0, sizeof(struct share_obj));
7722023a998SMauro Carvalho Chehab memset_io(vpu->send_buf, 0, sizeof(struct share_obj));
7732023a998SMauro Carvalho Chehab
7742023a998SMauro Carvalho Chehab return 0;
7752023a998SMauro Carvalho Chehab }
7762023a998SMauro Carvalho Chehab
vpu_irq_handler(int irq,void * priv)7772023a998SMauro Carvalho Chehab static irqreturn_t vpu_irq_handler(int irq, void *priv)
7782023a998SMauro Carvalho Chehab {
7792023a998SMauro Carvalho Chehab struct mtk_vpu *vpu = priv;
7802023a998SMauro Carvalho Chehab u32 vpu_to_host;
7812023a998SMauro Carvalho Chehab int ret;
7822023a998SMauro Carvalho Chehab
7832023a998SMauro Carvalho Chehab /*
7842023a998SMauro Carvalho Chehab * Clock should have been enabled already.
7852023a998SMauro Carvalho Chehab * Enable again in case vpu_ipi_send times out
7862023a998SMauro Carvalho Chehab * and has disabled the clock.
7872023a998SMauro Carvalho Chehab */
7882023a998SMauro Carvalho Chehab ret = clk_enable(vpu->clk);
7892023a998SMauro Carvalho Chehab if (ret) {
7902023a998SMauro Carvalho Chehab dev_err(vpu->dev, "[VPU] enable clock failed %d\n", ret);
7912023a998SMauro Carvalho Chehab return IRQ_NONE;
7922023a998SMauro Carvalho Chehab }
7932023a998SMauro Carvalho Chehab vpu_to_host = vpu_cfg_readl(vpu, VPU_TO_HOST);
7942023a998SMauro Carvalho Chehab if (vpu_to_host & VPU_IPC_INT) {
7952023a998SMauro Carvalho Chehab vpu_ipi_handler(vpu);
7962023a998SMauro Carvalho Chehab } else {
7972023a998SMauro Carvalho Chehab dev_err(vpu->dev, "vpu watchdog timeout! 0x%x", vpu_to_host);
7982023a998SMauro Carvalho Chehab queue_work(vpu->wdt.wq, &vpu->wdt.ws);
7992023a998SMauro Carvalho Chehab }
8002023a998SMauro Carvalho Chehab
8012023a998SMauro Carvalho Chehab /* VPU won't send another interrupt until we set VPU_TO_HOST to 0. */
8022023a998SMauro Carvalho Chehab vpu_cfg_writel(vpu, 0x0, VPU_TO_HOST);
8032023a998SMauro Carvalho Chehab clk_disable(vpu->clk);
8042023a998SMauro Carvalho Chehab
8052023a998SMauro Carvalho Chehab return IRQ_HANDLED;
8062023a998SMauro Carvalho Chehab }
8072023a998SMauro Carvalho Chehab
8082023a998SMauro Carvalho Chehab #ifdef CONFIG_DEBUG_FS
8092023a998SMauro Carvalho Chehab static struct dentry *vpu_debugfs;
8102023a998SMauro Carvalho Chehab #endif
mtk_vpu_probe(struct platform_device * pdev)8112023a998SMauro Carvalho Chehab static int mtk_vpu_probe(struct platform_device *pdev)
8122023a998SMauro Carvalho Chehab {
8132023a998SMauro Carvalho Chehab struct mtk_vpu *vpu;
8142023a998SMauro Carvalho Chehab struct device *dev;
8152023a998SMauro Carvalho Chehab int ret = 0;
8162023a998SMauro Carvalho Chehab
8172023a998SMauro Carvalho Chehab dev_dbg(&pdev->dev, "initialization\n");
8182023a998SMauro Carvalho Chehab
8192023a998SMauro Carvalho Chehab dev = &pdev->dev;
8202023a998SMauro Carvalho Chehab vpu = devm_kzalloc(dev, sizeof(*vpu), GFP_KERNEL);
8212023a998SMauro Carvalho Chehab if (!vpu)
8222023a998SMauro Carvalho Chehab return -ENOMEM;
8232023a998SMauro Carvalho Chehab
8242023a998SMauro Carvalho Chehab vpu->dev = &pdev->dev;
8252023a998SMauro Carvalho Chehab vpu->reg.tcm = devm_platform_ioremap_resource_byname(pdev, "tcm");
8262023a998SMauro Carvalho Chehab if (IS_ERR((__force void *)vpu->reg.tcm))
8272023a998SMauro Carvalho Chehab return PTR_ERR((__force void *)vpu->reg.tcm);
8282023a998SMauro Carvalho Chehab
8292023a998SMauro Carvalho Chehab vpu->reg.cfg = devm_platform_ioremap_resource_byname(pdev, "cfg_reg");
8302023a998SMauro Carvalho Chehab if (IS_ERR((__force void *)vpu->reg.cfg))
8312023a998SMauro Carvalho Chehab return PTR_ERR((__force void *)vpu->reg.cfg);
8322023a998SMauro Carvalho Chehab
8332023a998SMauro Carvalho Chehab /* Get VPU clock */
8342023a998SMauro Carvalho Chehab vpu->clk = devm_clk_get(dev, "main");
8352023a998SMauro Carvalho Chehab if (IS_ERR(vpu->clk)) {
8362023a998SMauro Carvalho Chehab dev_err(dev, "get vpu clock failed\n");
8372023a998SMauro Carvalho Chehab return PTR_ERR(vpu->clk);
8382023a998SMauro Carvalho Chehab }
8392023a998SMauro Carvalho Chehab
8402023a998SMauro Carvalho Chehab platform_set_drvdata(pdev, vpu);
8412023a998SMauro Carvalho Chehab
8422023a998SMauro Carvalho Chehab ret = clk_prepare(vpu->clk);
8432023a998SMauro Carvalho Chehab if (ret) {
8442023a998SMauro Carvalho Chehab dev_err(dev, "prepare vpu clock failed\n");
8452023a998SMauro Carvalho Chehab return ret;
8462023a998SMauro Carvalho Chehab }
8472023a998SMauro Carvalho Chehab
8482023a998SMauro Carvalho Chehab /* VPU watchdog */
8492023a998SMauro Carvalho Chehab vpu->wdt.wq = create_singlethread_workqueue("vpu_wdt");
8502023a998SMauro Carvalho Chehab if (!vpu->wdt.wq) {
8512023a998SMauro Carvalho Chehab dev_err(dev, "initialize wdt workqueue failed\n");
8522023a998SMauro Carvalho Chehab ret = -ENOMEM;
8532023a998SMauro Carvalho Chehab goto clk_unprepare;
8542023a998SMauro Carvalho Chehab }
8552023a998SMauro Carvalho Chehab INIT_WORK(&vpu->wdt.ws, vpu_wdt_reset_func);
8562023a998SMauro Carvalho Chehab mutex_init(&vpu->vpu_mutex);
8572023a998SMauro Carvalho Chehab
8582023a998SMauro Carvalho Chehab ret = vpu_clock_enable(vpu);
8592023a998SMauro Carvalho Chehab if (ret) {
8602023a998SMauro Carvalho Chehab dev_err(dev, "enable vpu clock failed\n");
8612023a998SMauro Carvalho Chehab goto workqueue_destroy;
8622023a998SMauro Carvalho Chehab }
8632023a998SMauro Carvalho Chehab
8642023a998SMauro Carvalho Chehab dev_dbg(dev, "vpu ipi init\n");
8652023a998SMauro Carvalho Chehab ret = vpu_ipi_init(vpu);
8662023a998SMauro Carvalho Chehab if (ret) {
8672023a998SMauro Carvalho Chehab dev_err(dev, "Failed to init ipi\n");
8682023a998SMauro Carvalho Chehab goto disable_vpu_clk;
8692023a998SMauro Carvalho Chehab }
8702023a998SMauro Carvalho Chehab
8712023a998SMauro Carvalho Chehab /* register vpu initialization IPI */
8722023a998SMauro Carvalho Chehab ret = vpu_ipi_register(pdev, IPI_VPU_INIT, vpu_init_ipi_handler,
8732023a998SMauro Carvalho Chehab "vpu_init", vpu);
8742023a998SMauro Carvalho Chehab if (ret) {
8752023a998SMauro Carvalho Chehab dev_err(dev, "Failed to register IPI_VPU_INIT\n");
8762023a998SMauro Carvalho Chehab goto vpu_mutex_destroy;
8772023a998SMauro Carvalho Chehab }
8782023a998SMauro Carvalho Chehab
8792023a998SMauro Carvalho Chehab #ifdef CONFIG_DEBUG_FS
8802023a998SMauro Carvalho Chehab vpu_debugfs = debugfs_create_file("mtk_vpu", S_IRUGO, NULL, (void *)dev,
8812023a998SMauro Carvalho Chehab &vpu_debug_fops);
8822023a998SMauro Carvalho Chehab #endif
8832023a998SMauro Carvalho Chehab
8842023a998SMauro Carvalho Chehab /* Set PTCM to 96K and DTCM to 32K */
8852023a998SMauro Carvalho Chehab vpu_cfg_writel(vpu, 0x2, VPU_TCM_CFG);
8862023a998SMauro Carvalho Chehab
8872023a998SMauro Carvalho Chehab vpu->enable_4GB = !!(totalram_pages() > (SZ_2G >> PAGE_SHIFT));
8882023a998SMauro Carvalho Chehab dev_info(dev, "4GB mode %u\n", vpu->enable_4GB);
8892023a998SMauro Carvalho Chehab
8902023a998SMauro Carvalho Chehab if (vpu->enable_4GB) {
8912023a998SMauro Carvalho Chehab ret = of_reserved_mem_device_init(dev);
8922023a998SMauro Carvalho Chehab if (ret)
8932023a998SMauro Carvalho Chehab dev_info(dev, "init reserved memory failed\n");
8942023a998SMauro Carvalho Chehab /* continue to use dynamic allocation if failed */
8952023a998SMauro Carvalho Chehab }
8962023a998SMauro Carvalho Chehab
8972023a998SMauro Carvalho Chehab ret = vpu_alloc_ext_mem(vpu, D_FW);
8982023a998SMauro Carvalho Chehab if (ret) {
8992023a998SMauro Carvalho Chehab dev_err(dev, "Allocate DM failed\n");
9002023a998SMauro Carvalho Chehab goto remove_debugfs;
9012023a998SMauro Carvalho Chehab }
9022023a998SMauro Carvalho Chehab
9032023a998SMauro Carvalho Chehab ret = vpu_alloc_ext_mem(vpu, P_FW);
9042023a998SMauro Carvalho Chehab if (ret) {
9052023a998SMauro Carvalho Chehab dev_err(dev, "Allocate PM failed\n");
9062023a998SMauro Carvalho Chehab goto free_d_mem;
9072023a998SMauro Carvalho Chehab }
9082023a998SMauro Carvalho Chehab
9092023a998SMauro Carvalho Chehab init_waitqueue_head(&vpu->run.wq);
9102023a998SMauro Carvalho Chehab init_waitqueue_head(&vpu->ack_wq);
9112023a998SMauro Carvalho Chehab
9122023a998SMauro Carvalho Chehab ret = platform_get_irq(pdev, 0);
9132023a998SMauro Carvalho Chehab if (ret < 0)
9142023a998SMauro Carvalho Chehab goto free_p_mem;
9152023a998SMauro Carvalho Chehab vpu->reg.irq = ret;
9162023a998SMauro Carvalho Chehab ret = devm_request_irq(dev, vpu->reg.irq, vpu_irq_handler, 0,
9172023a998SMauro Carvalho Chehab pdev->name, vpu);
9182023a998SMauro Carvalho Chehab if (ret) {
9192023a998SMauro Carvalho Chehab dev_err(dev, "failed to request irq\n");
9202023a998SMauro Carvalho Chehab goto free_p_mem;
9212023a998SMauro Carvalho Chehab }
9222023a998SMauro Carvalho Chehab
9232023a998SMauro Carvalho Chehab vpu_clock_disable(vpu);
9242023a998SMauro Carvalho Chehab dev_dbg(dev, "initialization completed\n");
9252023a998SMauro Carvalho Chehab
9262023a998SMauro Carvalho Chehab return 0;
9272023a998SMauro Carvalho Chehab
9282023a998SMauro Carvalho Chehab free_p_mem:
9292023a998SMauro Carvalho Chehab vpu_free_ext_mem(vpu, P_FW);
9302023a998SMauro Carvalho Chehab free_d_mem:
9312023a998SMauro Carvalho Chehab vpu_free_ext_mem(vpu, D_FW);
9322023a998SMauro Carvalho Chehab remove_debugfs:
9332023a998SMauro Carvalho Chehab of_reserved_mem_device_release(dev);
9342023a998SMauro Carvalho Chehab #ifdef CONFIG_DEBUG_FS
9352023a998SMauro Carvalho Chehab debugfs_remove(vpu_debugfs);
9362023a998SMauro Carvalho Chehab #endif
9372023a998SMauro Carvalho Chehab memset(vpu->ipi_desc, 0, sizeof(struct vpu_ipi_desc) * IPI_MAX);
9382023a998SMauro Carvalho Chehab vpu_mutex_destroy:
9392023a998SMauro Carvalho Chehab mutex_destroy(&vpu->vpu_mutex);
9402023a998SMauro Carvalho Chehab disable_vpu_clk:
9412023a998SMauro Carvalho Chehab vpu_clock_disable(vpu);
9422023a998SMauro Carvalho Chehab workqueue_destroy:
9432023a998SMauro Carvalho Chehab destroy_workqueue(vpu->wdt.wq);
9442023a998SMauro Carvalho Chehab clk_unprepare:
9452023a998SMauro Carvalho Chehab clk_unprepare(vpu->clk);
9462023a998SMauro Carvalho Chehab
9472023a998SMauro Carvalho Chehab return ret;
9482023a998SMauro Carvalho Chehab }
9492023a998SMauro Carvalho Chehab
9502023a998SMauro Carvalho Chehab static const struct of_device_id mtk_vpu_match[] = {
9512023a998SMauro Carvalho Chehab {
9522023a998SMauro Carvalho Chehab .compatible = "mediatek,mt8173-vpu",
9532023a998SMauro Carvalho Chehab },
9542023a998SMauro Carvalho Chehab {},
9552023a998SMauro Carvalho Chehab };
9562023a998SMauro Carvalho Chehab MODULE_DEVICE_TABLE(of, mtk_vpu_match);
9572023a998SMauro Carvalho Chehab
mtk_vpu_remove(struct platform_device * pdev)958ce27e331SUwe Kleine-König static void mtk_vpu_remove(struct platform_device *pdev)
9592023a998SMauro Carvalho Chehab {
9602023a998SMauro Carvalho Chehab struct mtk_vpu *vpu = platform_get_drvdata(pdev);
9612023a998SMauro Carvalho Chehab
9622023a998SMauro Carvalho Chehab #ifdef CONFIG_DEBUG_FS
9632023a998SMauro Carvalho Chehab debugfs_remove(vpu_debugfs);
9642023a998SMauro Carvalho Chehab #endif
9652023a998SMauro Carvalho Chehab if (vpu->wdt.wq)
9662023a998SMauro Carvalho Chehab destroy_workqueue(vpu->wdt.wq);
9672023a998SMauro Carvalho Chehab vpu_free_ext_mem(vpu, P_FW);
9682023a998SMauro Carvalho Chehab vpu_free_ext_mem(vpu, D_FW);
9692023a998SMauro Carvalho Chehab mutex_destroy(&vpu->vpu_mutex);
9702023a998SMauro Carvalho Chehab clk_unprepare(vpu->clk);
9712023a998SMauro Carvalho Chehab }
9722023a998SMauro Carvalho Chehab
mtk_vpu_suspend(struct device * dev)9732023a998SMauro Carvalho Chehab static int mtk_vpu_suspend(struct device *dev)
9742023a998SMauro Carvalho Chehab {
9752023a998SMauro Carvalho Chehab struct mtk_vpu *vpu = dev_get_drvdata(dev);
9762023a998SMauro Carvalho Chehab unsigned long timeout;
9772023a998SMauro Carvalho Chehab int ret;
9782023a998SMauro Carvalho Chehab
9792023a998SMauro Carvalho Chehab ret = vpu_clock_enable(vpu);
9802023a998SMauro Carvalho Chehab if (ret) {
9812023a998SMauro Carvalho Chehab dev_err(dev, "failed to enable vpu clock\n");
9822023a998SMauro Carvalho Chehab return ret;
9832023a998SMauro Carvalho Chehab }
9842023a998SMauro Carvalho Chehab
9852023a998SMauro Carvalho Chehab if (!vpu_running(vpu)) {
9862023a998SMauro Carvalho Chehab vpu_clock_disable(vpu);
9872023a998SMauro Carvalho Chehab clk_unprepare(vpu->clk);
9882023a998SMauro Carvalho Chehab return 0;
9892023a998SMauro Carvalho Chehab }
9902023a998SMauro Carvalho Chehab
9912023a998SMauro Carvalho Chehab mutex_lock(&vpu->vpu_mutex);
9922023a998SMauro Carvalho Chehab /* disable vpu timer interrupt */
9932023a998SMauro Carvalho Chehab vpu_cfg_writel(vpu, vpu_cfg_readl(vpu, VPU_INT_STATUS) | VPU_IDLE_STATE,
9942023a998SMauro Carvalho Chehab VPU_INT_STATUS);
9952023a998SMauro Carvalho Chehab /* check if vpu is idle for system suspend */
9962023a998SMauro Carvalho Chehab timeout = jiffies + msecs_to_jiffies(VPU_IDLE_TIMEOUT_MS);
9972023a998SMauro Carvalho Chehab do {
9982023a998SMauro Carvalho Chehab if (time_after(jiffies, timeout)) {
9992023a998SMauro Carvalho Chehab dev_err(dev, "vpu idle timeout\n");
10002023a998SMauro Carvalho Chehab mutex_unlock(&vpu->vpu_mutex);
10012023a998SMauro Carvalho Chehab vpu_clock_disable(vpu);
10022023a998SMauro Carvalho Chehab return -EIO;
10032023a998SMauro Carvalho Chehab }
10042023a998SMauro Carvalho Chehab } while (!vpu_cfg_readl(vpu, VPU_IDLE_REG));
10052023a998SMauro Carvalho Chehab
10062023a998SMauro Carvalho Chehab mutex_unlock(&vpu->vpu_mutex);
10072023a998SMauro Carvalho Chehab vpu_clock_disable(vpu);
10082023a998SMauro Carvalho Chehab clk_unprepare(vpu->clk);
10092023a998SMauro Carvalho Chehab
10102023a998SMauro Carvalho Chehab return 0;
10112023a998SMauro Carvalho Chehab }
10122023a998SMauro Carvalho Chehab
mtk_vpu_resume(struct device * dev)10132023a998SMauro Carvalho Chehab static int mtk_vpu_resume(struct device *dev)
10142023a998SMauro Carvalho Chehab {
10152023a998SMauro Carvalho Chehab struct mtk_vpu *vpu = dev_get_drvdata(dev);
10162023a998SMauro Carvalho Chehab int ret;
10172023a998SMauro Carvalho Chehab
10182023a998SMauro Carvalho Chehab clk_prepare(vpu->clk);
10192023a998SMauro Carvalho Chehab ret = vpu_clock_enable(vpu);
10202023a998SMauro Carvalho Chehab if (ret) {
10212192fa95SHans Verkuil clk_unprepare(vpu->clk);
10222023a998SMauro Carvalho Chehab dev_err(dev, "failed to enable vpu clock\n");
10232023a998SMauro Carvalho Chehab return ret;
10242023a998SMauro Carvalho Chehab }
10252023a998SMauro Carvalho Chehab
10262023a998SMauro Carvalho Chehab mutex_lock(&vpu->vpu_mutex);
10272023a998SMauro Carvalho Chehab /* enable vpu timer interrupt */
10282023a998SMauro Carvalho Chehab vpu_cfg_writel(vpu,
10292023a998SMauro Carvalho Chehab vpu_cfg_readl(vpu, VPU_INT_STATUS) & ~(VPU_IDLE_STATE),
10302023a998SMauro Carvalho Chehab VPU_INT_STATUS);
10312023a998SMauro Carvalho Chehab mutex_unlock(&vpu->vpu_mutex);
10322023a998SMauro Carvalho Chehab vpu_clock_disable(vpu);
10332023a998SMauro Carvalho Chehab
10342023a998SMauro Carvalho Chehab return 0;
10352023a998SMauro Carvalho Chehab }
10362023a998SMauro Carvalho Chehab
10372023a998SMauro Carvalho Chehab static const struct dev_pm_ops mtk_vpu_pm = {
10382023a998SMauro Carvalho Chehab .suspend = mtk_vpu_suspend,
10392023a998SMauro Carvalho Chehab .resume = mtk_vpu_resume,
10402023a998SMauro Carvalho Chehab };
10412023a998SMauro Carvalho Chehab
10422023a998SMauro Carvalho Chehab static struct platform_driver mtk_vpu_driver = {
10432023a998SMauro Carvalho Chehab .probe = mtk_vpu_probe,
1044ce27e331SUwe Kleine-König .remove_new = mtk_vpu_remove,
10452023a998SMauro Carvalho Chehab .driver = {
10462023a998SMauro Carvalho Chehab .name = "mtk_vpu",
10472023a998SMauro Carvalho Chehab .pm = &mtk_vpu_pm,
10482023a998SMauro Carvalho Chehab .of_match_table = mtk_vpu_match,
10492023a998SMauro Carvalho Chehab },
10502023a998SMauro Carvalho Chehab };
10512023a998SMauro Carvalho Chehab
10522023a998SMauro Carvalho Chehab module_platform_driver(mtk_vpu_driver);
10532023a998SMauro Carvalho Chehab
10542023a998SMauro Carvalho Chehab MODULE_LICENSE("GPL v2");
10552023a998SMauro Carvalho Chehab MODULE_DESCRIPTION("Mediatek Video Processor Unit driver");
1056