Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 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> |
Pi-Hsun Shih | 7017996 | 2019-11-12 19:03:26 +0800 | [diff] [blame] | 18 | #include <linux/rpmsg/mtk_rpmsg.h> |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 19 | |
| 20 | #include "mtk_common.h" |
| 21 | #include "remoteproc_internal.h" |
| 22 | |
| 23 | #define MAX_CODE_SIZE 0x500000 |
| 24 | #define SCP_FW_END 0x7C000 |
| 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 | **/ |
| 34 | struct 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 | } |
| 56 | EXPORT_SYMBOL_GPL(scp_get); |
| 57 | |
| 58 | /** |
| 59 | * scp_put() - "free" the SCP |
| 60 | * |
| 61 | * @scp: mtk_scp structure from scp_get(). |
| 62 | **/ |
| 63 | void scp_put(struct mtk_scp *scp) |
| 64 | { |
| 65 | put_device(scp->dev); |
| 66 | } |
| 67 | EXPORT_SYMBOL_GPL(scp_put); |
| 68 | |
| 69 | static 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 | |
| 75 | static 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 | |
| 87 | static 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 | |
| 122 | static int scp_ipi_init(struct mtk_scp *scp) |
| 123 | { |
| 124 | size_t send_offset = SCP_FW_END - sizeof(struct mtk_share_obj); |
| 125 | size_t recv_offset = send_offset - sizeof(struct mtk_share_obj); |
| 126 | |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 127 | /* shared buffer initialization */ |
| 128 | scp->recv_buf = |
| 129 | (struct mtk_share_obj __iomem *)(scp->sram_base + recv_offset); |
| 130 | scp->send_buf = |
| 131 | (struct mtk_share_obj __iomem *)(scp->sram_base + send_offset); |
Wei Yongjun | 8096f80 | 2020-05-09 08:42:37 +0000 | [diff] [blame] | 132 | memset_io(scp->recv_buf, 0, sizeof(*scp->recv_buf)); |
| 133 | memset_io(scp->send_buf, 0, sizeof(*scp->send_buf)); |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
Pi-Hsun Shih | fd0b6c1 | 2020-09-21 17:48:46 +0800 | [diff] [blame] | 138 | static void mt8183_scp_reset_assert(struct mtk_scp *scp) |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 139 | { |
| 140 | u32 val; |
| 141 | |
| 142 | val = readl(scp->reg_base + MT8183_SW_RSTN); |
| 143 | val &= ~MT8183_SW_RSTN_BIT; |
| 144 | writel(val, scp->reg_base + MT8183_SW_RSTN); |
| 145 | } |
| 146 | |
Pi-Hsun Shih | fd0b6c1 | 2020-09-21 17:48:46 +0800 | [diff] [blame] | 147 | static void mt8183_scp_reset_deassert(struct mtk_scp *scp) |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 148 | { |
| 149 | u32 val; |
| 150 | |
| 151 | val = readl(scp->reg_base + MT8183_SW_RSTN); |
| 152 | val |= MT8183_SW_RSTN_BIT; |
| 153 | writel(val, scp->reg_base + MT8183_SW_RSTN); |
| 154 | } |
| 155 | |
Pi-Hsun Shih | fd0b6c1 | 2020-09-21 17:48:46 +0800 | [diff] [blame] | 156 | static void mt8192_scp_reset_assert(struct mtk_scp *scp) |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 157 | { |
Pi-Hsun Shih | fd0b6c1 | 2020-09-21 17:48:46 +0800 | [diff] [blame] | 158 | writel(1, scp->reg_base + MT8192_CORE0_SW_RSTN_SET); |
| 159 | } |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 160 | |
Pi-Hsun Shih | fd0b6c1 | 2020-09-21 17:48:46 +0800 | [diff] [blame] | 161 | static void mt8192_scp_reset_deassert(struct mtk_scp *scp) |
| 162 | { |
| 163 | writel(1, scp->reg_base + MT8192_CORE0_SW_RSTN_CLR); |
| 164 | } |
| 165 | |
| 166 | static void mt8183_scp_irq_handler(struct mtk_scp *scp) |
| 167 | { |
| 168 | u32 scp_to_host; |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 169 | |
| 170 | scp_to_host = readl(scp->reg_base + MT8183_SCP_TO_HOST); |
| 171 | if (scp_to_host & MT8183_SCP_IPC_INT_BIT) |
| 172 | scp_ipi_handler(scp); |
| 173 | else |
| 174 | scp_wdt_handler(scp, scp_to_host); |
| 175 | |
| 176 | /* SCP won't send another interrupt until we set SCP_TO_HOST to 0. */ |
| 177 | writel(MT8183_SCP_IPC_INT_BIT | MT8183_SCP_WDT_INT_BIT, |
| 178 | scp->reg_base + MT8183_SCP_TO_HOST); |
Pi-Hsun Shih | fd0b6c1 | 2020-09-21 17:48:46 +0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | static void mt8192_scp_irq_handler(struct mtk_scp *scp) |
| 182 | { |
| 183 | u32 scp_to_host; |
| 184 | |
| 185 | scp_to_host = readl(scp->reg_base + MT8192_SCP2APMCU_IPC_SET); |
| 186 | |
| 187 | if (scp_to_host & MT8192_SCP_IPC_INT_BIT) |
| 188 | scp_ipi_handler(scp); |
| 189 | else |
| 190 | scp_wdt_handler(scp, scp_to_host); |
| 191 | |
| 192 | /* |
| 193 | * SCP won't send another interrupt until we clear |
| 194 | * MT8192_SCP2APMCU_IPC. |
| 195 | */ |
| 196 | writel(MT8192_SCP_IPC_INT_BIT, |
| 197 | scp->reg_base + MT8192_SCP2APMCU_IPC_CLR); |
| 198 | } |
| 199 | |
| 200 | static irqreturn_t scp_irq_handler(int irq, void *priv) |
| 201 | { |
| 202 | struct mtk_scp *scp = priv; |
| 203 | int ret; |
| 204 | |
| 205 | ret = clk_prepare_enable(scp->clk); |
| 206 | if (ret) { |
| 207 | dev_err(scp->dev, "failed to enable clocks\n"); |
| 208 | return IRQ_NONE; |
| 209 | } |
| 210 | |
| 211 | scp->data->scp_irq_handler(scp); |
| 212 | |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 213 | clk_disable_unprepare(scp->clk); |
| 214 | |
| 215 | return IRQ_HANDLED; |
| 216 | } |
| 217 | |
| 218 | static int scp_elf_load_segments(struct rproc *rproc, const struct firmware *fw) |
| 219 | { |
| 220 | struct device *dev = &rproc->dev; |
| 221 | struct elf32_hdr *ehdr; |
| 222 | struct elf32_phdr *phdr; |
| 223 | int i, ret = 0; |
| 224 | const u8 *elf_data = fw->data; |
| 225 | |
| 226 | ehdr = (struct elf32_hdr *)elf_data; |
| 227 | phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff); |
| 228 | |
| 229 | /* go through the available ELF segments */ |
| 230 | for (i = 0; i < ehdr->e_phnum; i++, phdr++) { |
| 231 | u32 da = phdr->p_paddr; |
| 232 | u32 memsz = phdr->p_memsz; |
| 233 | u32 filesz = phdr->p_filesz; |
| 234 | u32 offset = phdr->p_offset; |
| 235 | void __iomem *ptr; |
| 236 | |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 237 | dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n", |
| 238 | phdr->p_type, da, memsz, filesz); |
| 239 | |
Tzung-Bi Shih | 48cb5b6 | 2020-11-16 16:44:12 +0800 | [diff] [blame] | 240 | if (phdr->p_type != PT_LOAD) |
| 241 | continue; |
| 242 | if (!filesz) |
| 243 | continue; |
| 244 | |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 245 | if (filesz > memsz) { |
| 246 | dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n", |
| 247 | filesz, memsz); |
| 248 | ret = -EINVAL; |
| 249 | break; |
| 250 | } |
| 251 | |
| 252 | if (offset + filesz > fw->size) { |
| 253 | dev_err(dev, "truncated fw: need 0x%x avail 0x%zx\n", |
| 254 | offset + filesz, fw->size); |
| 255 | ret = -EINVAL; |
| 256 | break; |
| 257 | } |
| 258 | |
| 259 | /* grab the kernel address for this device address */ |
| 260 | ptr = (void __iomem *)rproc_da_to_va(rproc, da, memsz); |
| 261 | if (!ptr) { |
| 262 | dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz); |
| 263 | ret = -EINVAL; |
| 264 | break; |
| 265 | } |
| 266 | |
| 267 | /* put the segment where the remote processor expects it */ |
Tzung-Bi Shih | 48cb5b6 | 2020-11-16 16:44:12 +0800 | [diff] [blame] | 268 | scp_memcpy_aligned(ptr, elf_data + phdr->p_offset, filesz); |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | return ret; |
| 272 | } |
| 273 | |
Pi-Hsun Shih | fd0b6c1 | 2020-09-21 17:48:46 +0800 | [diff] [blame] | 274 | static int mt8183_scp_before_load(struct mtk_scp *scp) |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 275 | { |
Pi-Hsun Shih | fd0b6c1 | 2020-09-21 17:48:46 +0800 | [diff] [blame] | 276 | /* Clear SCP to host interrupt */ |
| 277 | writel(MT8183_SCP_IPC_INT_BIT, scp->reg_base + MT8183_SCP_TO_HOST); |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 278 | |
| 279 | /* Reset clocks before loading FW */ |
| 280 | writel(0x0, scp->reg_base + MT8183_SCP_CLK_SW_SEL); |
| 281 | writel(0x0, scp->reg_base + MT8183_SCP_CLK_DIV_SEL); |
| 282 | |
| 283 | /* Initialize TCM before loading FW. */ |
| 284 | writel(0x0, scp->reg_base + MT8183_SCP_L1_SRAM_PD); |
| 285 | writel(0x0, scp->reg_base + MT8183_SCP_TCM_TAIL_SRAM_PD); |
| 286 | |
| 287 | /* Turn on the power of SCP's SRAM before using it. */ |
| 288 | writel(0x0, scp->reg_base + MT8183_SCP_SRAM_PDN); |
| 289 | |
| 290 | /* |
| 291 | * Set I-cache and D-cache size before loading SCP FW. |
| 292 | * SCP SRAM logical address may change when cache size setting differs. |
| 293 | */ |
| 294 | writel(MT8183_SCP_CACHE_CON_WAYEN | MT8183_SCP_CACHESIZE_8KB, |
| 295 | scp->reg_base + MT8183_SCP_CACHE_CON); |
| 296 | writel(MT8183_SCP_CACHESIZE_8KB, scp->reg_base + MT8183_SCP_DCACHE_CON); |
| 297 | |
Pi-Hsun Shih | fd0b6c1 | 2020-09-21 17:48:46 +0800 | [diff] [blame] | 298 | return 0; |
| 299 | } |
| 300 | |
Tzung-Bi Shih | 778f2664 | 2020-11-16 16:25:36 +0800 | [diff] [blame] | 301 | static void mt8192_power_on_sram(void __iomem *addr) |
Pi-Hsun Shih | fd0b6c1 | 2020-09-21 17:48:46 +0800 | [diff] [blame] | 302 | { |
| 303 | int i; |
| 304 | |
| 305 | for (i = 31; i >= 0; i--) |
| 306 | writel(GENMASK(i, 0), addr); |
| 307 | writel(0, addr); |
| 308 | } |
| 309 | |
Tzung-Bi Shih | 778f2664 | 2020-11-16 16:25:36 +0800 | [diff] [blame] | 310 | static void mt8192_power_off_sram(void __iomem *addr) |
Pi-Hsun Shih | fd0b6c1 | 2020-09-21 17:48:46 +0800 | [diff] [blame] | 311 | { |
| 312 | int i; |
| 313 | |
| 314 | writel(0, addr); |
| 315 | for (i = 0; i < 32; i++) |
| 316 | writel(GENMASK(i, 0), addr); |
| 317 | } |
| 318 | |
| 319 | static int mt8192_scp_before_load(struct mtk_scp *scp) |
| 320 | { |
| 321 | /* clear SPM interrupt, SCP2SPM_IPC_CLR */ |
| 322 | writel(0xff, scp->reg_base + MT8192_SCP2SPM_IPC_CLR); |
| 323 | |
| 324 | writel(1, scp->reg_base + MT8192_CORE0_SW_RSTN_SET); |
| 325 | |
Pi-Hsun Shih | fd0b6c1 | 2020-09-21 17:48:46 +0800 | [diff] [blame] | 326 | /* enable SRAM clock */ |
| 327 | mt8192_power_on_sram(scp->reg_base + MT8192_L2TCM_SRAM_PD_0); |
| 328 | mt8192_power_on_sram(scp->reg_base + MT8192_L2TCM_SRAM_PD_1); |
| 329 | mt8192_power_on_sram(scp->reg_base + MT8192_L2TCM_SRAM_PD_2); |
| 330 | mt8192_power_on_sram(scp->reg_base + MT8192_L1TCM_SRAM_PDN); |
| 331 | mt8192_power_on_sram(scp->reg_base + MT8192_CPU0_SRAM_PD); |
| 332 | |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | static int scp_load(struct rproc *rproc, const struct firmware *fw) |
| 337 | { |
| 338 | struct mtk_scp *scp = rproc->priv; |
| 339 | struct device *dev = scp->dev; |
| 340 | int ret; |
| 341 | |
| 342 | ret = clk_prepare_enable(scp->clk); |
| 343 | if (ret) { |
| 344 | dev_err(dev, "failed to enable clocks\n"); |
| 345 | return ret; |
| 346 | } |
| 347 | |
| 348 | /* Hold SCP in reset while loading FW. */ |
| 349 | scp->data->scp_reset_assert(scp); |
| 350 | |
| 351 | ret = scp->data->scp_before_load(scp); |
| 352 | if (ret < 0) |
Tzung-Bi Shih | 22c3df6 | 2020-12-03 23:59:14 +0800 | [diff] [blame^] | 353 | goto leave; |
Pi-Hsun Shih | fd0b6c1 | 2020-09-21 17:48:46 +0800 | [diff] [blame] | 354 | |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 355 | ret = scp_elf_load_segments(rproc, fw); |
Tzung-Bi Shih | 22c3df6 | 2020-12-03 23:59:14 +0800 | [diff] [blame^] | 356 | leave: |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 357 | clk_disable_unprepare(scp->clk); |
| 358 | |
| 359 | return ret; |
| 360 | } |
| 361 | |
| 362 | static int scp_start(struct rproc *rproc) |
| 363 | { |
| 364 | struct mtk_scp *scp = (struct mtk_scp *)rproc->priv; |
| 365 | struct device *dev = scp->dev; |
| 366 | struct scp_run *run = &scp->run; |
| 367 | int ret; |
| 368 | |
| 369 | ret = clk_prepare_enable(scp->clk); |
| 370 | if (ret) { |
| 371 | dev_err(dev, "failed to enable clocks\n"); |
| 372 | return ret; |
| 373 | } |
| 374 | |
| 375 | run->signaled = false; |
| 376 | |
Pi-Hsun Shih | fd0b6c1 | 2020-09-21 17:48:46 +0800 | [diff] [blame] | 377 | scp->data->scp_reset_deassert(scp); |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 378 | |
| 379 | ret = wait_event_interruptible_timeout( |
| 380 | run->wq, |
| 381 | run->signaled, |
| 382 | msecs_to_jiffies(2000)); |
| 383 | |
| 384 | if (ret == 0) { |
| 385 | dev_err(dev, "wait SCP initialization timeout!\n"); |
| 386 | ret = -ETIME; |
| 387 | goto stop; |
| 388 | } |
| 389 | if (ret == -ERESTARTSYS) { |
| 390 | dev_err(dev, "wait SCP interrupted by a signal!\n"); |
| 391 | goto stop; |
| 392 | } |
Pi-Hsun Shih | fd0b6c1 | 2020-09-21 17:48:46 +0800 | [diff] [blame] | 393 | |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 394 | clk_disable_unprepare(scp->clk); |
| 395 | dev_info(dev, "SCP is ready. FW version %s\n", run->fw_ver); |
| 396 | |
| 397 | return 0; |
| 398 | |
| 399 | stop: |
Pi-Hsun Shih | fd0b6c1 | 2020-09-21 17:48:46 +0800 | [diff] [blame] | 400 | scp->data->scp_reset_assert(scp); |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 401 | clk_disable_unprepare(scp->clk); |
| 402 | return ret; |
| 403 | } |
| 404 | |
Nathan Chancellor | e1833b9 | 2020-03-10 14:15:14 -0700 | [diff] [blame] | 405 | static void *scp_da_to_va(struct rproc *rproc, u64 da, size_t len) |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 406 | { |
| 407 | struct mtk_scp *scp = (struct mtk_scp *)rproc->priv; |
| 408 | int offset; |
| 409 | |
| 410 | if (da < scp->sram_size) { |
| 411 | offset = da; |
Tzung-Bi Shih | 71ffb5a | 2020-11-16 16:44:11 +0800 | [diff] [blame] | 412 | if (offset >= 0 && (offset + len) <= scp->sram_size) |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 413 | return (void __force *)scp->sram_base + offset; |
Pi-Hsun Shih | fd0b6c1 | 2020-09-21 17:48:46 +0800 | [diff] [blame] | 414 | } else if (scp->dram_size) { |
Arnd Bergmann | c2781e4 | 2020-04-08 17:54:29 +0200 | [diff] [blame] | 415 | offset = da - scp->dma_addr; |
Tzung-Bi Shih | 71ffb5a | 2020-11-16 16:44:11 +0800 | [diff] [blame] | 416 | if (offset >= 0 && (offset + len) <= scp->dram_size) |
Tzung-Bi Shih | 903635c | 2020-11-16 16:25:37 +0800 | [diff] [blame] | 417 | return scp->cpu_addr + offset; |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | return NULL; |
| 421 | } |
| 422 | |
Pi-Hsun Shih | fd0b6c1 | 2020-09-21 17:48:46 +0800 | [diff] [blame] | 423 | static void mt8183_scp_stop(struct mtk_scp *scp) |
| 424 | { |
| 425 | /* Disable SCP watchdog */ |
| 426 | writel(0, scp->reg_base + MT8183_WDT_CFG); |
| 427 | } |
| 428 | |
| 429 | static void mt8192_scp_stop(struct mtk_scp *scp) |
| 430 | { |
| 431 | /* Disable SRAM clock */ |
| 432 | mt8192_power_off_sram(scp->reg_base + MT8192_L2TCM_SRAM_PD_0); |
| 433 | mt8192_power_off_sram(scp->reg_base + MT8192_L2TCM_SRAM_PD_1); |
| 434 | mt8192_power_off_sram(scp->reg_base + MT8192_L2TCM_SRAM_PD_2); |
| 435 | mt8192_power_off_sram(scp->reg_base + MT8192_L1TCM_SRAM_PDN); |
| 436 | mt8192_power_off_sram(scp->reg_base + MT8192_CPU0_SRAM_PD); |
| 437 | |
| 438 | /* Disable SCP watchdog */ |
| 439 | writel(0, scp->reg_base + MT8192_CORE0_WDT_CFG); |
| 440 | } |
| 441 | |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 442 | static int scp_stop(struct rproc *rproc) |
| 443 | { |
| 444 | struct mtk_scp *scp = (struct mtk_scp *)rproc->priv; |
| 445 | int ret; |
| 446 | |
| 447 | ret = clk_prepare_enable(scp->clk); |
| 448 | if (ret) { |
| 449 | dev_err(scp->dev, "failed to enable clocks\n"); |
| 450 | return ret; |
| 451 | } |
| 452 | |
Pi-Hsun Shih | fd0b6c1 | 2020-09-21 17:48:46 +0800 | [diff] [blame] | 453 | scp->data->scp_reset_assert(scp); |
| 454 | scp->data->scp_stop(scp); |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 455 | clk_disable_unprepare(scp->clk); |
| 456 | |
| 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | static const struct rproc_ops scp_ops = { |
| 461 | .start = scp_start, |
| 462 | .stop = scp_stop, |
| 463 | .load = scp_load, |
| 464 | .da_to_va = scp_da_to_va, |
| 465 | }; |
| 466 | |
| 467 | /** |
| 468 | * scp_get_device() - get device struct of SCP |
| 469 | * |
| 470 | * @scp: mtk_scp structure |
| 471 | **/ |
| 472 | struct device *scp_get_device(struct mtk_scp *scp) |
| 473 | { |
| 474 | return scp->dev; |
| 475 | } |
| 476 | EXPORT_SYMBOL_GPL(scp_get_device); |
| 477 | |
| 478 | /** |
| 479 | * scp_get_rproc() - get rproc struct of SCP |
| 480 | * |
| 481 | * @scp: mtk_scp structure |
| 482 | **/ |
| 483 | struct rproc *scp_get_rproc(struct mtk_scp *scp) |
| 484 | { |
| 485 | return scp->rproc; |
| 486 | } |
| 487 | EXPORT_SYMBOL_GPL(scp_get_rproc); |
| 488 | |
| 489 | /** |
| 490 | * scp_get_vdec_hw_capa() - get video decoder hardware capability |
| 491 | * |
| 492 | * @scp: mtk_scp structure |
| 493 | * |
| 494 | * Return: video decoder hardware capability |
| 495 | **/ |
| 496 | unsigned int scp_get_vdec_hw_capa(struct mtk_scp *scp) |
| 497 | { |
| 498 | return scp->run.dec_capability; |
| 499 | } |
| 500 | EXPORT_SYMBOL_GPL(scp_get_vdec_hw_capa); |
| 501 | |
| 502 | /** |
| 503 | * scp_get_venc_hw_capa() - get video encoder hardware capability |
| 504 | * |
| 505 | * @scp: mtk_scp structure |
| 506 | * |
| 507 | * Return: video encoder hardware capability |
| 508 | **/ |
| 509 | unsigned int scp_get_venc_hw_capa(struct mtk_scp *scp) |
| 510 | { |
| 511 | return scp->run.enc_capability; |
| 512 | } |
| 513 | EXPORT_SYMBOL_GPL(scp_get_venc_hw_capa); |
| 514 | |
| 515 | /** |
| 516 | * scp_mapping_dm_addr() - Mapping SRAM/DRAM to kernel virtual address |
| 517 | * |
| 518 | * @scp: mtk_scp structure |
| 519 | * @mem_addr: SCP views memory address |
| 520 | * |
| 521 | * Mapping the SCP's SRAM address / |
| 522 | * DMEM (Data Extended Memory) memory address / |
| 523 | * Working buffer memory address to |
| 524 | * kernel virtual address. |
| 525 | * |
| 526 | * Return: Return ERR_PTR(-EINVAL) if mapping failed, |
| 527 | * otherwise the mapped kernel virtual address |
| 528 | **/ |
| 529 | void *scp_mapping_dm_addr(struct mtk_scp *scp, u32 mem_addr) |
| 530 | { |
| 531 | void *ptr; |
| 532 | |
| 533 | ptr = scp_da_to_va(scp->rproc, mem_addr, 0); |
| 534 | if (!ptr) |
| 535 | return ERR_PTR(-EINVAL); |
| 536 | |
| 537 | return ptr; |
| 538 | } |
| 539 | EXPORT_SYMBOL_GPL(scp_mapping_dm_addr); |
| 540 | |
| 541 | static int scp_map_memory_region(struct mtk_scp *scp) |
| 542 | { |
| 543 | int ret; |
| 544 | |
| 545 | ret = of_reserved_mem_device_init(scp->dev); |
Pi-Hsun Shih | fd0b6c1 | 2020-09-21 17:48:46 +0800 | [diff] [blame] | 546 | |
| 547 | /* reserved memory is optional. */ |
| 548 | if (ret == -ENODEV) { |
| 549 | dev_info(scp->dev, "skipping reserved memory initialization."); |
| 550 | return 0; |
| 551 | } |
| 552 | |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 553 | if (ret) { |
| 554 | dev_err(scp->dev, "failed to assign memory-region: %d\n", ret); |
| 555 | return -ENOMEM; |
| 556 | } |
| 557 | |
| 558 | /* Reserved SCP code size */ |
| 559 | scp->dram_size = MAX_CODE_SIZE; |
| 560 | scp->cpu_addr = dma_alloc_coherent(scp->dev, scp->dram_size, |
Arnd Bergmann | c2781e4 | 2020-04-08 17:54:29 +0200 | [diff] [blame] | 561 | &scp->dma_addr, GFP_KERNEL); |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 562 | if (!scp->cpu_addr) |
| 563 | return -ENOMEM; |
| 564 | |
| 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | static void scp_unmap_memory_region(struct mtk_scp *scp) |
| 569 | { |
Pi-Hsun Shih | fd0b6c1 | 2020-09-21 17:48:46 +0800 | [diff] [blame] | 570 | if (scp->dram_size == 0) |
| 571 | return; |
| 572 | |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 573 | dma_free_coherent(scp->dev, scp->dram_size, scp->cpu_addr, |
Arnd Bergmann | c2781e4 | 2020-04-08 17:54:29 +0200 | [diff] [blame] | 574 | scp->dma_addr); |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 575 | of_reserved_mem_device_release(scp->dev); |
| 576 | } |
| 577 | |
Pi-Hsun Shih | 7017996 | 2019-11-12 19:03:26 +0800 | [diff] [blame] | 578 | static int scp_register_ipi(struct platform_device *pdev, u32 id, |
| 579 | ipi_handler_t handler, void *priv) |
| 580 | { |
| 581 | struct mtk_scp *scp = platform_get_drvdata(pdev); |
| 582 | |
| 583 | return scp_ipi_register(scp, id, handler, priv); |
| 584 | } |
| 585 | |
| 586 | static void scp_unregister_ipi(struct platform_device *pdev, u32 id) |
| 587 | { |
| 588 | struct mtk_scp *scp = platform_get_drvdata(pdev); |
| 589 | |
| 590 | scp_ipi_unregister(scp, id); |
| 591 | } |
| 592 | |
| 593 | static int scp_send_ipi(struct platform_device *pdev, u32 id, void *buf, |
| 594 | unsigned int len, unsigned int wait) |
| 595 | { |
| 596 | struct mtk_scp *scp = platform_get_drvdata(pdev); |
| 597 | |
| 598 | return scp_ipi_send(scp, id, buf, len, wait); |
| 599 | } |
| 600 | |
| 601 | static struct mtk_rpmsg_info mtk_scp_rpmsg_info = { |
| 602 | .send_ipi = scp_send_ipi, |
| 603 | .register_ipi = scp_register_ipi, |
| 604 | .unregister_ipi = scp_unregister_ipi, |
| 605 | .ns_ipi_id = SCP_IPI_NS_SERVICE, |
| 606 | }; |
| 607 | |
| 608 | static void scp_add_rpmsg_subdev(struct mtk_scp *scp) |
| 609 | { |
| 610 | scp->rpmsg_subdev = |
| 611 | mtk_rpmsg_create_rproc_subdev(to_platform_device(scp->dev), |
| 612 | &mtk_scp_rpmsg_info); |
| 613 | if (scp->rpmsg_subdev) |
| 614 | rproc_add_subdev(scp->rproc, scp->rpmsg_subdev); |
| 615 | } |
| 616 | |
| 617 | static void scp_remove_rpmsg_subdev(struct mtk_scp *scp) |
| 618 | { |
| 619 | if (scp->rpmsg_subdev) { |
| 620 | rproc_remove_subdev(scp->rproc, scp->rpmsg_subdev); |
| 621 | mtk_rpmsg_destroy_rproc_subdev(scp->rpmsg_subdev); |
| 622 | scp->rpmsg_subdev = NULL; |
| 623 | } |
| 624 | } |
| 625 | |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 626 | static int scp_probe(struct platform_device *pdev) |
| 627 | { |
| 628 | struct device *dev = &pdev->dev; |
| 629 | struct device_node *np = dev->of_node; |
| 630 | struct mtk_scp *scp; |
| 631 | struct rproc *rproc; |
| 632 | struct resource *res; |
| 633 | char *fw_name = "scp.img"; |
| 634 | int ret, i; |
| 635 | |
| 636 | rproc = rproc_alloc(dev, |
| 637 | np->name, |
| 638 | &scp_ops, |
| 639 | fw_name, |
| 640 | sizeof(*scp)); |
| 641 | if (!rproc) { |
| 642 | dev_err(dev, "unable to allocate remoteproc\n"); |
| 643 | return -ENOMEM; |
| 644 | } |
| 645 | |
| 646 | scp = (struct mtk_scp *)rproc->priv; |
| 647 | scp->rproc = rproc; |
| 648 | scp->dev = dev; |
Pi-Hsun Shih | fd0b6c1 | 2020-09-21 17:48:46 +0800 | [diff] [blame] | 649 | scp->data = of_device_get_match_data(dev); |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 650 | platform_set_drvdata(pdev, scp); |
| 651 | |
| 652 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "sram"); |
| 653 | scp->sram_base = devm_ioremap_resource(dev, res); |
| 654 | if (IS_ERR((__force void *)scp->sram_base)) { |
| 655 | dev_err(dev, "Failed to parse and map sram memory\n"); |
| 656 | ret = PTR_ERR((__force void *)scp->sram_base); |
| 657 | goto free_rproc; |
| 658 | } |
| 659 | scp->sram_size = resource_size(res); |
| 660 | |
| 661 | mutex_init(&scp->send_lock); |
| 662 | for (i = 0; i < SCP_IPI_MAX; i++) |
| 663 | mutex_init(&scp->ipi_desc[i].lock); |
| 664 | |
| 665 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg"); |
| 666 | scp->reg_base = devm_ioremap_resource(dev, res); |
| 667 | if (IS_ERR((__force void *)scp->reg_base)) { |
| 668 | dev_err(dev, "Failed to parse and map cfg memory\n"); |
| 669 | ret = PTR_ERR((__force void *)scp->reg_base); |
| 670 | goto destroy_mutex; |
| 671 | } |
| 672 | |
| 673 | ret = scp_map_memory_region(scp); |
| 674 | if (ret) |
| 675 | goto destroy_mutex; |
| 676 | |
| 677 | scp->clk = devm_clk_get(dev, "main"); |
| 678 | if (IS_ERR(scp->clk)) { |
| 679 | dev_err(dev, "Failed to get clock\n"); |
| 680 | ret = PTR_ERR(scp->clk); |
| 681 | goto release_dev_mem; |
| 682 | } |
| 683 | |
| 684 | ret = clk_prepare_enable(scp->clk); |
| 685 | if (ret) { |
| 686 | dev_err(dev, "failed to enable clocks\n"); |
| 687 | goto release_dev_mem; |
| 688 | } |
| 689 | |
| 690 | ret = scp_ipi_init(scp); |
| 691 | clk_disable_unprepare(scp->clk); |
| 692 | if (ret) { |
| 693 | dev_err(dev, "Failed to init ipi\n"); |
| 694 | goto release_dev_mem; |
| 695 | } |
| 696 | |
| 697 | /* register SCP initialization IPI */ |
| 698 | ret = scp_ipi_register(scp, SCP_IPI_INIT, scp_init_ipi_handler, scp); |
| 699 | if (ret) { |
| 700 | dev_err(dev, "Failed to register IPI_SCP_INIT\n"); |
| 701 | goto release_dev_mem; |
| 702 | } |
| 703 | |
| 704 | init_waitqueue_head(&scp->run.wq); |
| 705 | init_waitqueue_head(&scp->ack_wq); |
| 706 | |
Pi-Hsun Shih | 7017996 | 2019-11-12 19:03:26 +0800 | [diff] [blame] | 707 | scp_add_rpmsg_subdev(scp); |
| 708 | |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 709 | ret = devm_request_threaded_irq(dev, platform_get_irq(pdev, 0), NULL, |
| 710 | scp_irq_handler, IRQF_ONESHOT, |
| 711 | pdev->name, scp); |
| 712 | |
| 713 | if (ret) { |
| 714 | dev_err(dev, "failed to request irq\n"); |
Pi-Hsun Shih | 7017996 | 2019-11-12 19:03:26 +0800 | [diff] [blame] | 715 | goto remove_subdev; |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | ret = rproc_add(rproc); |
| 719 | if (ret) |
Pi-Hsun Shih | 7017996 | 2019-11-12 19:03:26 +0800 | [diff] [blame] | 720 | goto remove_subdev; |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 721 | |
Pi-Hsun Shih | 7017996 | 2019-11-12 19:03:26 +0800 | [diff] [blame] | 722 | return 0; |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 723 | |
Pi-Hsun Shih | 7017996 | 2019-11-12 19:03:26 +0800 | [diff] [blame] | 724 | remove_subdev: |
| 725 | scp_remove_rpmsg_subdev(scp); |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 726 | scp_ipi_unregister(scp, SCP_IPI_INIT); |
| 727 | release_dev_mem: |
| 728 | scp_unmap_memory_region(scp); |
| 729 | destroy_mutex: |
| 730 | for (i = 0; i < SCP_IPI_MAX; i++) |
| 731 | mutex_destroy(&scp->ipi_desc[i].lock); |
| 732 | mutex_destroy(&scp->send_lock); |
| 733 | free_rproc: |
| 734 | rproc_free(rproc); |
| 735 | |
| 736 | return ret; |
| 737 | } |
| 738 | |
| 739 | static int scp_remove(struct platform_device *pdev) |
| 740 | { |
| 741 | struct mtk_scp *scp = platform_get_drvdata(pdev); |
| 742 | int i; |
| 743 | |
| 744 | rproc_del(scp->rproc); |
Pi-Hsun Shih | 7017996 | 2019-11-12 19:03:26 +0800 | [diff] [blame] | 745 | scp_remove_rpmsg_subdev(scp); |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 746 | scp_ipi_unregister(scp, SCP_IPI_INIT); |
| 747 | scp_unmap_memory_region(scp); |
| 748 | for (i = 0; i < SCP_IPI_MAX; i++) |
| 749 | mutex_destroy(&scp->ipi_desc[i].lock); |
| 750 | mutex_destroy(&scp->send_lock); |
| 751 | rproc_free(scp->rproc); |
| 752 | |
| 753 | return 0; |
| 754 | } |
| 755 | |
Pi-Hsun Shih | fd0b6c1 | 2020-09-21 17:48:46 +0800 | [diff] [blame] | 756 | static const struct mtk_scp_of_data mt8183_of_data = { |
| 757 | .scp_before_load = mt8183_scp_before_load, |
| 758 | .scp_irq_handler = mt8183_scp_irq_handler, |
| 759 | .scp_reset_assert = mt8183_scp_reset_assert, |
| 760 | .scp_reset_deassert = mt8183_scp_reset_deassert, |
| 761 | .scp_stop = mt8183_scp_stop, |
| 762 | .host_to_scp_reg = MT8183_HOST_TO_SCP, |
| 763 | .host_to_scp_int_bit = MT8183_HOST_IPC_INT_BIT, |
| 764 | }; |
| 765 | |
| 766 | static const struct mtk_scp_of_data mt8192_of_data = { |
| 767 | .scp_before_load = mt8192_scp_before_load, |
| 768 | .scp_irq_handler = mt8192_scp_irq_handler, |
| 769 | .scp_reset_assert = mt8192_scp_reset_assert, |
| 770 | .scp_reset_deassert = mt8192_scp_reset_deassert, |
| 771 | .scp_stop = mt8192_scp_stop, |
| 772 | .host_to_scp_reg = MT8192_GIPC_IN_SET, |
| 773 | .host_to_scp_int_bit = MT8192_HOST_IPC_INT_BIT, |
| 774 | }; |
| 775 | |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 776 | static const struct of_device_id mtk_scp_of_match[] = { |
Pi-Hsun Shih | fd0b6c1 | 2020-09-21 17:48:46 +0800 | [diff] [blame] | 777 | { .compatible = "mediatek,mt8183-scp", .data = &mt8183_of_data }, |
| 778 | { .compatible = "mediatek,mt8192-scp", .data = &mt8192_of_data }, |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 779 | {}, |
| 780 | }; |
| 781 | MODULE_DEVICE_TABLE(of, mtk_scp_of_match); |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 782 | |
| 783 | static struct platform_driver mtk_scp_driver = { |
| 784 | .probe = scp_probe, |
| 785 | .remove = scp_remove, |
| 786 | .driver = { |
| 787 | .name = "mtk-scp", |
Souptick Joarder | cca2100 | 2020-11-28 03:20:55 +0530 | [diff] [blame] | 788 | .of_match_table = mtk_scp_of_match, |
Erin Lo | 63c13d6 | 2019-11-12 19:03:25 +0800 | [diff] [blame] | 789 | }, |
| 790 | }; |
| 791 | |
| 792 | module_platform_driver(mtk_scp_driver); |
| 793 | |
| 794 | MODULE_LICENSE("GPL v2"); |
| 795 | MODULE_DESCRIPTION("MediaTek SCP control driver"); |