1 // SPDX-License-Identifier: GPL-2.0+
3 * Texas Instruments' K3 R5 Remoteproc driver
5 * Copyright (C) 2018-2019 Texas Instruments Incorporated - http://www.ti.com/
6 * Lokesh Vutla <lokeshvutla@ti.com>
11 #include <remoteproc.h>
16 #include <linux/kernel.h>
17 #include <linux/soc/ti/ti_sci_protocol.h>
18 #include "ti_sci_proc.h"
21 * R5F's view of this address can either be for ATCM or BTCM with the other
22 * at address 0x0 based on loczrama signal.
24 #define K3_R5_TCM_DEV_ADDR 0x41010000
26 /* R5 TI-SCI Processor Configuration Flags */
27 #define PROC_BOOT_CFG_FLAG_R5_DBG_EN 0x00000001
28 #define PROC_BOOT_CFG_FLAG_R5_DBG_NIDEN 0x00000002
29 #define PROC_BOOT_CFG_FLAG_R5_LOCKSTEP 0x00000100
30 #define PROC_BOOT_CFG_FLAG_R5_TEINIT 0x00000200
31 #define PROC_BOOT_CFG_FLAG_R5_NMFI_EN 0x00000400
32 #define PROC_BOOT_CFG_FLAG_R5_TCM_RSTBASE 0x00000800
33 #define PROC_BOOT_CFG_FLAG_R5_BTCM_EN 0x00001000
34 #define PROC_BOOT_CFG_FLAG_R5_ATCM_EN 0x00002000
35 #define PROC_BOOT_CFG_FLAG_GEN_IGN_BOOTVECTOR 0x10000000
37 /* R5 TI-SCI Processor Control Flags */
38 #define PROC_BOOT_CTRL_FLAG_R5_CORE_HALT 0x00000001
40 /* R5 TI-SCI Processor Status Flags */
41 #define PROC_BOOT_STATUS_FLAG_R5_WFE 0x00000001
42 #define PROC_BOOT_STATUS_FLAG_R5_WFI 0x00000002
43 #define PROC_BOOT_STATUS_FLAG_R5_CLK_GATED 0x00000004
44 #define PROC_BOOT_STATUS_FLAG_R5_LOCKSTEP_PERMITTED 0x00000100
49 CLUSTER_MODE_SPLIT = 0,
50 CLUSTER_MODE_LOCKSTEP,
54 * struct k3_r5_mem - internal memory structure
55 * @cpu_addr: MPU virtual address of the memory region
56 * @bus_addr: Bus address used to access the memory region
57 * @dev_addr: Device address from remoteproc view
58 * @size: Size of the memory region
61 void __iomem *cpu_addr;
68 * struct k3_r5f_core - K3 R5 core structure
69 * @dev: cached device pointer
70 * @cluster: pointer to the parent cluster.
71 * @reset: reset control handle
72 * @tsp: TI-SCI processor control handle
73 * @mem: Array of available internal memories
74 * @num_mem: Number of available memories
75 * @atcm_enable: flag to control ATCM enablement
76 * @btcm_enable: flag to control BTCM enablement
77 * @loczrama: flag to dictate which TCM is at device address 0x0
78 * @in_use: flag to tell if the core is already in use.
82 struct k3_r5f_cluster *cluster;
83 struct reset_ctl reset;
84 struct ti_sci_proc tsp;
85 struct k3_r5f_mem *mem;
94 * struct k3_r5f_cluster - K3 R5F Cluster structure
95 * @mode: Mode to configure the Cluster - Split or LockStep
96 * @cores: Array of pointers to R5 cores within the cluster
98 struct k3_r5f_cluster {
99 enum cluster_mode mode;
100 struct k3_r5f_core *cores[NR_CORES];
103 static bool is_primary_core(struct k3_r5f_core *core)
105 return core == core->cluster->cores[0];
108 static int k3_r5f_proc_request(struct k3_r5f_core *core)
110 struct k3_r5f_cluster *cluster = core->cluster;
113 if (cluster->mode == CLUSTER_MODE_LOCKSTEP) {
114 for (i = 0; i < NR_CORES; i++) {
115 ret = ti_sci_proc_request(&cluster->cores[i]->tsp);
120 ret = ti_sci_proc_request(&core->tsp);
127 ti_sci_proc_release(&cluster->cores[i]->tsp);
133 static void k3_r5f_proc_release(struct k3_r5f_core *core)
135 struct k3_r5f_cluster *cluster = core->cluster;
138 if (cluster->mode == CLUSTER_MODE_LOCKSTEP)
139 for (i = 0; i < NR_CORES; i++)
140 ti_sci_proc_release(&cluster->cores[i]->tsp);
142 ti_sci_proc_release(&core->tsp);
145 static int k3_r5f_lockstep_release(struct k3_r5f_cluster *cluster)
149 dev_dbg(dev, "%s\n", __func__);
151 for (c = NR_CORES - 1; c >= 0; c--) {
152 ret = ti_sci_proc_power_domain_on(&cluster->cores[c]->tsp);
154 goto unroll_module_reset;
157 /* deassert local reset on all applicable cores */
158 for (c = NR_CORES - 1; c >= 0; c--) {
159 ret = reset_deassert(&cluster->cores[c]->reset);
161 goto unroll_local_reset;
167 while (c < NR_CORES) {
168 reset_assert(&cluster->cores[c]->reset);
173 while (c < NR_CORES) {
174 ti_sci_proc_power_domain_off(&cluster->cores[c]->tsp);
181 static int k3_r5f_split_release(struct k3_r5f_core *core)
185 dev_dbg(dev, "%s\n", __func__);
187 ret = ti_sci_proc_power_domain_on(&core->tsp);
189 dev_err(core->dev, "module-reset deassert failed, ret = %d\n",
194 ret = reset_deassert(&core->reset);
196 dev_err(core->dev, "local-reset deassert failed, ret = %d\n",
198 if (ti_sci_proc_power_domain_off(&core->tsp))
199 dev_warn(core->dev, "module-reset assert back failed\n");
205 static int k3_r5f_prepare(struct udevice *dev)
207 struct k3_r5f_core *core = dev_get_priv(dev);
208 struct k3_r5f_cluster *cluster = core->cluster;
211 dev_dbg(dev, "%s\n", __func__);
213 if (cluster->mode == CLUSTER_MODE_LOCKSTEP)
214 ret = k3_r5f_lockstep_release(cluster);
216 ret = k3_r5f_split_release(core);
219 dev_err(dev, "Unable to enable cores for TCM loading %d\n",
225 static int k3_r5f_core_sanity_check(struct k3_r5f_core *core)
227 struct k3_r5f_cluster *cluster = core->cluster;
230 dev_err(dev, "Invalid op: Trying to load/start on already running core %d\n",
235 if (cluster->mode == CLUSTER_MODE_LOCKSTEP && !cluster->cores[1]) {
236 printf("Secondary core is not probed in this cluster\n");
240 if (cluster->mode == CLUSTER_MODE_LOCKSTEP && !is_primary_core(core)) {
241 dev_err(dev, "Invalid op: Trying to start secondary core %d in lockstep mode\n",
246 if (cluster->mode == CLUSTER_MODE_SPLIT && !is_primary_core(core)) {
247 if (!core->cluster->cores[0]->in_use) {
248 dev_err(dev, "Invalid seq: Enable primary core before loading secondary core\n");
257 * k3_r5f_load() - Load up the Remote processor image
258 * @dev: rproc device pointer
259 * @addr: Address at which image is available
260 * @size: size of the image
262 * Return: 0 if all goes good, else appropriate error message.
264 static int k3_r5f_load(struct udevice *dev, ulong addr, ulong size)
266 struct k3_r5f_core *core = dev_get_priv(dev);
270 dev_dbg(dev, "%s addr = 0x%lx, size = 0x%lx\n", __func__, addr, size);
272 ret = k3_r5f_core_sanity_check(core);
276 ret = k3_r5f_proc_request(core);
280 ret = k3_r5f_prepare(dev);
282 dev_err(dev, "R5f prepare failed for core %d\n",
287 /* Zero out TCMs so that ECC can be effective on all TCM addresses */
288 if (core->atcm_enable)
289 memset(core->mem[0].cpu_addr, 0x00, core->mem[0].size);
290 if (core->btcm_enable)
291 memset(core->mem[1].cpu_addr, 0x00, core->mem[1].size);
293 ret = rproc_elf_load_image(dev, addr, size);
295 dev_err(dev, "Loading elf failedi %d\n", ret);
299 boot_vector = rproc_elf_get_boot_addr(dev, addr);
301 dev_dbg(dev, "%s: Boot vector = 0x%x\n", __func__, boot_vector);
303 ret = ti_sci_proc_set_config(&core->tsp, boot_vector, 0, 0);
306 k3_r5f_proc_release(core);
311 static int k3_r5f_core_halt(struct k3_r5f_core *core)
315 ret = ti_sci_proc_set_control(&core->tsp,
316 PROC_BOOT_CTRL_FLAG_R5_CORE_HALT, 0);
318 dev_err(core->dev, "Core %d failed to stop\n",
324 static int k3_r5f_core_run(struct k3_r5f_core *core)
328 ret = ti_sci_proc_set_control(&core->tsp,
329 0, PROC_BOOT_CTRL_FLAG_R5_CORE_HALT);
331 dev_err(core->dev, "Core %d failed to start\n",
340 * k3_r5f_start() - Start the remote processor
341 * @dev: rproc device pointer
343 * Return: 0 if all went ok, else return appropriate error
345 static int k3_r5f_start(struct udevice *dev)
347 struct k3_r5f_core *core = dev_get_priv(dev);
348 struct k3_r5f_cluster *cluster = core->cluster;
351 dev_dbg(dev, "%s\n", __func__);
353 ret = k3_r5f_core_sanity_check(core);
357 ret = k3_r5f_proc_request(core);
361 if (cluster->mode == CLUSTER_MODE_LOCKSTEP) {
362 if (is_primary_core(core)) {
363 for (c = NR_CORES - 1; c >= 0; c--) {
364 ret = k3_r5f_core_run(cluster->cores[c]);
366 goto unroll_core_run;
369 dev_err(dev, "Invalid op: Trying to start secondary core %d in lockstep mode\n",
375 ret = k3_r5f_core_run(core);
382 k3_r5f_proc_release(core);
386 while (c < NR_CORES) {
387 k3_r5f_core_halt(cluster->cores[c]);
391 k3_r5f_proc_release(core);
396 static int k3_r5f_split_reset(struct k3_r5f_core *core)
400 dev_dbg(dev, "%s\n", __func__);
402 if (reset_assert(&core->reset))
405 if (ti_sci_proc_power_domain_off(&core->tsp))
411 static int k3_r5f_lockstep_reset(struct k3_r5f_cluster *cluster)
415 dev_dbg(dev, "%s\n", __func__);
417 for (c = 0; c < NR_CORES; c++)
418 if (reset_assert(&cluster->cores[c]->reset))
421 /* disable PSC modules on all applicable cores */
422 for (c = 0; c < NR_CORES; c++)
423 if (ti_sci_proc_power_domain_off(&cluster->cores[c]->tsp))
429 static int k3_r5f_unprepare(struct udevice *dev)
431 struct k3_r5f_core *core = dev_get_priv(dev);
432 struct k3_r5f_cluster *cluster = core->cluster;
435 dev_dbg(dev, "%s\n", __func__);
437 if (cluster->mode == CLUSTER_MODE_LOCKSTEP) {
438 if (is_primary_core(core))
439 ret = k3_r5f_lockstep_reset(cluster);
441 ret = k3_r5f_split_reset(core);
445 dev_warn(dev, "Unable to enable cores for TCM loading %d\n",
451 static int k3_r5f_stop(struct udevice *dev)
453 struct k3_r5f_core *core = dev_get_priv(dev);
454 struct k3_r5f_cluster *cluster = core->cluster;
457 dev_dbg(dev, "%s\n", __func__);
459 ret = k3_r5f_proc_request(core);
463 core->in_use = false;
465 if (cluster->mode == CLUSTER_MODE_LOCKSTEP) {
466 if (is_primary_core(core)) {
467 for (c = 0; c < NR_CORES; c++)
468 k3_r5f_core_halt(cluster->cores[c]);
470 dev_err(dev, "Invalid op: Trying to stop secondary core in lockstep mode\n");
475 k3_r5f_core_halt(core);
478 ret = k3_r5f_unprepare(dev);
480 k3_r5f_proc_release(core);
484 static void *k3_r5f_da_to_va(struct udevice *dev, ulong da, ulong size)
486 struct k3_r5f_core *core = dev_get_priv(dev);
487 void __iomem *va = NULL;
488 phys_addr_t bus_addr;
489 u32 dev_addr, offset;
493 dev_dbg(dev, "%s\n", __func__);
498 for (i = 0; i < core->num_mems; i++) {
499 bus_addr = core->mem[i].bus_addr;
500 dev_addr = core->mem[i].dev_addr;
501 mem_size = core->mem[i].size;
503 if (da >= bus_addr && (da + size) <= (bus_addr + mem_size)) {
504 offset = da - bus_addr;
505 va = core->mem[i].cpu_addr + offset;
506 return (__force void *)va;
509 if (da >= dev_addr && (da + size) <= (dev_addr + mem_size)) {
510 offset = da - dev_addr;
511 va = core->mem[i].cpu_addr + offset;
512 return (__force void *)va;
516 /* Assume it is DDR region and return da */
517 return map_physmem(da, size, MAP_NOCACHE);
520 static int k3_r5f_init(struct udevice *dev)
525 static int k3_r5f_reset(struct udevice *dev)
530 static const struct dm_rproc_ops k3_r5f_rproc_ops = {
532 .reset = k3_r5f_reset,
533 .start = k3_r5f_start,
536 .device_to_virt = k3_r5f_da_to_va,
539 static int k3_r5f_rproc_configure(struct k3_r5f_core *core)
541 struct k3_r5f_cluster *cluster = core->cluster;
542 u32 set_cfg = 0, clr_cfg = 0, cfg, ctrl, sts;
546 dev_dbg(dev, "%s\n", __func__);
548 ret = ti_sci_proc_request(&core->tsp);
552 /* Do not touch boot vector now. Load will take care of it. */
553 clr_cfg |= PROC_BOOT_CFG_FLAG_GEN_IGN_BOOTVECTOR;
555 ret = ti_sci_proc_get_status(&core->tsp, &boot_vec, &cfg, &ctrl, &sts);
559 /* Sanity check for Lockstep mode */
560 if (cluster->mode && is_primary_core(core) &&
561 !(sts & PROC_BOOT_STATUS_FLAG_R5_LOCKSTEP_PERMITTED)) {
562 dev_err(core->dev, "LockStep mode not permitted on this device\n");
567 /* Primary core only configuration */
568 if (is_primary_core(core)) {
569 /* always enable ARM mode */
570 clr_cfg |= PROC_BOOT_CFG_FLAG_R5_TEINIT;
571 if (cluster->mode == CLUSTER_MODE_LOCKSTEP)
572 set_cfg |= PROC_BOOT_CFG_FLAG_R5_LOCKSTEP;
574 clr_cfg |= PROC_BOOT_CFG_FLAG_R5_LOCKSTEP;
577 if (core->atcm_enable)
578 set_cfg |= PROC_BOOT_CFG_FLAG_R5_ATCM_EN;
580 clr_cfg |= PROC_BOOT_CFG_FLAG_R5_ATCM_EN;
582 if (core->btcm_enable)
583 set_cfg |= PROC_BOOT_CFG_FLAG_R5_BTCM_EN;
585 clr_cfg |= PROC_BOOT_CFG_FLAG_R5_BTCM_EN;
588 set_cfg |= PROC_BOOT_CFG_FLAG_R5_TCM_RSTBASE;
590 clr_cfg |= PROC_BOOT_CFG_FLAG_R5_TCM_RSTBASE;
592 ret = k3_r5f_core_halt(core);
596 ret = ti_sci_proc_set_config(&core->tsp, boot_vec, set_cfg, clr_cfg);
598 ti_sci_proc_release(&core->tsp);
602 static int ti_sci_proc_of_to_priv(struct udevice *dev, struct ti_sci_proc *tsp)
607 dev_dbg(dev, "%s\n", __func__);
609 tsp->sci = ti_sci_get_by_phandle(dev, "ti,sci");
610 if (IS_ERR(tsp->sci)) {
611 dev_err(dev, "ti_sci get failed: %ld\n", PTR_ERR(tsp->sci));
612 return PTR_ERR(tsp->sci);
615 ret = dev_read_u32_array(dev, "ti,sci-proc-ids", ids, 2);
617 dev_err(dev, "Proc IDs not populated %d\n", ret);
621 tsp->ops = &tsp->sci->ops.proc_ops;
622 tsp->proc_id = ids[0];
623 tsp->host_id = ids[1];
624 tsp->dev_id = dev_read_u32_default(dev, "ti,sci-dev-id",
625 TI_SCI_RESOURCE_NULL);
626 if (tsp->dev_id == TI_SCI_RESOURCE_NULL) {
627 dev_err(dev, "Device ID not populated %d\n", ret);
634 static int k3_r5f_of_to_priv(struct k3_r5f_core *core)
638 dev_dbg(dev, "%s\n", __func__);
640 core->atcm_enable = dev_read_u32_default(core->dev, "atcm-enable", 0);
641 core->btcm_enable = dev_read_u32_default(core->dev, "btcm-enable", 1);
642 core->loczrama = dev_read_u32_default(core->dev, "loczrama", 1);
644 ret = ti_sci_proc_of_to_priv(core->dev, &core->tsp);
648 ret = reset_get_by_index(core->dev, 0, &core->reset);
650 dev_err(core->dev, "Reset lines not available: %d\n", ret);
657 static int k3_r5f_core_of_get_memories(struct k3_r5f_core *core)
659 static const char * const mem_names[] = {"atcm", "btcm"};
660 struct udevice *dev = core->dev;
663 dev_dbg(dev, "%s\n", __func__);
665 core->num_mems = ARRAY_SIZE(mem_names);
666 core->mem = calloc(core->num_mems, sizeof(*core->mem));
670 for (i = 0; i < core->num_mems; i++) {
671 core->mem[i].bus_addr = dev_read_addr_size_name(dev,
673 (fdt_addr_t *)&core->mem[i].size);
674 if (core->mem[i].bus_addr == FDT_ADDR_T_NONE) {
675 dev_err(dev, "%s bus address not found\n",
679 core->mem[i].cpu_addr = map_physmem(core->mem[i].bus_addr,
682 if (!strcmp(mem_names[i], "atcm")) {
683 core->mem[i].dev_addr = core->loczrama ?
684 0 : K3_R5_TCM_DEV_ADDR;
686 core->mem[i].dev_addr = core->loczrama ?
687 K3_R5_TCM_DEV_ADDR : 0;
690 dev_dbg(dev, "memory %8s: bus addr %pa size 0x%zx va %p da 0x%x\n",
691 mem_names[i], &core->mem[i].bus_addr,
692 core->mem[i].size, core->mem[i].cpu_addr,
693 core->mem[i].dev_addr);
700 * k3_r5f_probe() - Basic probe
701 * @dev: corresponding k3 remote processor device
703 * Return: 0 if all goes good, else appropriate error message.
705 static int k3_r5f_probe(struct udevice *dev)
707 struct k3_r5f_cluster *cluster = dev_get_priv(dev->parent);
708 struct k3_r5f_core *core = dev_get_priv(dev);
712 dev_dbg(dev, "%s\n", __func__);
715 ret = k3_r5f_of_to_priv(core);
719 core->cluster = cluster;
720 /* Assume Primary core gets probed first */
721 if (!cluster->cores[0])
722 cluster->cores[0] = core;
724 cluster->cores[1] = core;
726 ret = k3_r5f_core_of_get_memories(core);
728 dev_err(dev, "Rproc getting internal memories failed\n");
732 ret = core->tsp.sci->ops.dev_ops.is_on(core->tsp.sci, core->tsp.dev_id,
733 &r_state, &core->in_use);
738 dev_info(dev, "Core %d is already in use. No rproc commands work\n",
743 /* Make sure Local reset is asserted. Redundant? */
744 reset_assert(&core->reset);
746 ret = k3_r5f_rproc_configure(core);
748 dev_err(dev, "rproc configure failed %d\n", ret);
752 dev_dbg(dev, "Remoteproc successfully probed\n");
757 static int k3_r5f_remove(struct udevice *dev)
759 struct k3_r5f_core *core = dev_get_priv(dev);
763 ti_sci_proc_release(&core->tsp);
768 static const struct udevice_id k3_r5f_rproc_ids[] = {
769 { .compatible = "ti,am654-r5f"},
770 { .compatible = "ti,j721e-r5f"},
774 U_BOOT_DRIVER(k3_r5f_rproc) = {
775 .name = "k3_r5f_rproc",
776 .of_match = k3_r5f_rproc_ids,
777 .id = UCLASS_REMOTEPROC,
778 .ops = &k3_r5f_rproc_ops,
779 .probe = k3_r5f_probe,
780 .remove = k3_r5f_remove,
781 .priv_auto_alloc_size = sizeof(struct k3_r5f_core),
784 static int k3_r5f_cluster_probe(struct udevice *dev)
786 struct k3_r5f_cluster *cluster = dev_get_priv(dev);
788 dev_dbg(dev, "%s\n", __func__);
790 cluster->mode = dev_read_u32_default(dev, "lockstep-mode",
791 CLUSTER_MODE_LOCKSTEP);
793 if (device_get_child_count(dev) != 2) {
794 dev_err(dev, "Invalid number of R5 cores");
798 dev_dbg(dev, "%s: Cluster successfully probed in %s mode\n",
799 __func__, cluster->mode ? "lockstep" : "split");
804 static const struct udevice_id k3_r5fss_ids[] = {
805 { .compatible = "ti,am654-r5fss"},
806 { .compatible = "ti,j721e-r5fss"},
810 U_BOOT_DRIVER(k3_r5fss) = {
812 .of_match = k3_r5fss_ids,
814 .probe = k3_r5f_cluster_probe,
815 .priv_auto_alloc_size = sizeof(struct k3_r5f_cluster),