1 // SPDX-License-Identifier: GPL-2.0-only
3 * TI K3 R5F (MCU) Remote Processor driver
5 * Copyright (C) 2017-2022 Texas Instruments Incorporated - https://www.ti.com/
6 * Suman Anna <s-anna@ti.com>
9 #include <linux/dma-mapping.h>
10 #include <linux/err.h>
11 #include <linux/interrupt.h>
12 #include <linux/kernel.h>
13 #include <linux/mailbox_client.h>
14 #include <linux/module.h>
15 #include <linux/of_address.h>
16 #include <linux/of_device.h>
17 #include <linux/of_reserved_mem.h>
18 #include <linux/omap-mailbox.h>
19 #include <linux/platform_device.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/remoteproc.h>
22 #include <linux/reset.h>
23 #include <linux/slab.h>
25 #include "omap_remoteproc.h"
26 #include "remoteproc_internal.h"
27 #include "ti_sci_proc.h"
29 /* This address can either be for ATCM or BTCM with the other at address 0x0 */
30 #define K3_R5_TCM_DEV_ADDR 0x41010000
32 /* R5 TI-SCI Processor Configuration Flags */
33 #define PROC_BOOT_CFG_FLAG_R5_DBG_EN 0x00000001
34 #define PROC_BOOT_CFG_FLAG_R5_DBG_NIDEN 0x00000002
35 #define PROC_BOOT_CFG_FLAG_R5_LOCKSTEP 0x00000100
36 #define PROC_BOOT_CFG_FLAG_R5_TEINIT 0x00000200
37 #define PROC_BOOT_CFG_FLAG_R5_NMFI_EN 0x00000400
38 #define PROC_BOOT_CFG_FLAG_R5_TCM_RSTBASE 0x00000800
39 #define PROC_BOOT_CFG_FLAG_R5_BTCM_EN 0x00001000
40 #define PROC_BOOT_CFG_FLAG_R5_ATCM_EN 0x00002000
41 /* Available from J7200 SoCs onwards */
42 #define PROC_BOOT_CFG_FLAG_R5_MEM_INIT_DIS 0x00004000
43 /* Applicable to only AM64x SoCs */
44 #define PROC_BOOT_CFG_FLAG_R5_SINGLE_CORE 0x00008000
46 /* R5 TI-SCI Processor Control Flags */
47 #define PROC_BOOT_CTRL_FLAG_R5_CORE_HALT 0x00000001
49 /* R5 TI-SCI Processor Status Flags */
50 #define PROC_BOOT_STATUS_FLAG_R5_WFE 0x00000001
51 #define PROC_BOOT_STATUS_FLAG_R5_WFI 0x00000002
52 #define PROC_BOOT_STATUS_FLAG_R5_CLK_GATED 0x00000004
53 #define PROC_BOOT_STATUS_FLAG_R5_LOCKSTEP_PERMITTED 0x00000100
54 /* Applicable to only AM64x SoCs */
55 #define PROC_BOOT_STATUS_FLAG_R5_SINGLECORE_ONLY 0x00000200
58 * struct k3_r5_mem - internal memory structure
59 * @cpu_addr: MPU virtual address of the memory region
60 * @bus_addr: Bus address used to access the memory region
61 * @dev_addr: Device address from remoteproc view
62 * @size: Size of the memory region
65 void __iomem *cpu_addr;
72 * All cluster mode values are not applicable on all SoCs. The following
73 * are the modes supported on various SoCs:
74 * Split mode : AM65x, J721E, J7200 and AM64x SoCs
75 * LockStep mode : AM65x, J721E and J7200 SoCs
76 * Single-CPU mode : AM64x SoCs only
77 * Single-Core mode : AM62x, AM62A SoCs
80 CLUSTER_MODE_SPLIT = 0,
81 CLUSTER_MODE_LOCKSTEP,
82 CLUSTER_MODE_SINGLECPU,
83 CLUSTER_MODE_SINGLECORE
87 * struct k3_r5_soc_data - match data to handle SoC variations
88 * @tcm_is_double: flag to denote the larger unified TCMs in certain modes
89 * @tcm_ecc_autoinit: flag to denote the auto-initialization of TCMs for ECC
90 * @single_cpu_mode: flag to denote if SoC/IP supports Single-CPU mode
91 * @is_single_core: flag to denote if SoC/IP has only single core R5
93 struct k3_r5_soc_data {
95 bool tcm_ecc_autoinit;
101 * struct k3_r5_cluster - K3 R5F Cluster structure
102 * @dev: cached device pointer
103 * @mode: Mode to configure the Cluster - Split or LockStep
104 * @cores: list of R5 cores within the cluster
105 * @soc_data: SoC-specific feature data for a R5FSS
107 struct k3_r5_cluster {
109 enum cluster_mode mode;
110 struct list_head cores;
111 const struct k3_r5_soc_data *soc_data;
115 * struct k3_r5_core - K3 R5 core structure
116 * @elem: linked list item
117 * @dev: cached device pointer
118 * @rproc: rproc handle representing this core
119 * @mem: internal memory regions data
120 * @sram: on-chip SRAM memory regions data
121 * @num_mems: number of internal memory regions
122 * @num_sram: number of on-chip SRAM memory regions
123 * @reset: reset control handle
124 * @tsp: TI-SCI processor control handle
125 * @ti_sci: TI-SCI handle
126 * @ti_sci_id: TI-SCI device identifier
127 * @atcm_enable: flag to control ATCM enablement
128 * @btcm_enable: flag to control BTCM enablement
129 * @loczrama: flag to dictate which TCM is at device address 0x0
132 struct list_head elem;
135 struct k3_r5_mem *mem;
136 struct k3_r5_mem *sram;
139 struct reset_control *reset;
140 struct ti_sci_proc *tsp;
141 const struct ti_sci_handle *ti_sci;
149 * struct k3_r5_rproc - K3 remote processor state
150 * @dev: cached device pointer
151 * @cluster: cached pointer to parent cluster structure
152 * @mbox: mailbox channel handle
153 * @client: mailbox client to request the mailbox channel
154 * @rproc: rproc handle
155 * @core: cached pointer to r5 core structure being used
156 * @rmem: reserved memory regions data
157 * @num_rmems: number of reserved memory regions
161 struct k3_r5_cluster *cluster;
162 struct mbox_chan *mbox;
163 struct mbox_client client;
165 struct k3_r5_core *core;
166 struct k3_r5_mem *rmem;
171 * k3_r5_rproc_mbox_callback() - inbound mailbox message handler
172 * @client: mailbox client pointer used for requesting the mailbox channel
173 * @data: mailbox payload
175 * This handler is invoked by the OMAP mailbox driver whenever a mailbox
176 * message is received. Usually, the mailbox payload simply contains
177 * the index of the virtqueue that is kicked by the remote processor,
178 * and we let remoteproc core handle it.
180 * In addition to virtqueue indices, we also have some out-of-band values
181 * that indicate different events. Those values are deliberately very
182 * large so they don't coincide with virtqueue indices.
184 static void k3_r5_rproc_mbox_callback(struct mbox_client *client, void *data)
186 struct k3_r5_rproc *kproc = container_of(client, struct k3_r5_rproc,
188 struct device *dev = kproc->rproc->dev.parent;
189 const char *name = kproc->rproc->name;
190 u32 msg = omap_mbox_message(data);
192 dev_dbg(dev, "mbox msg: 0x%x\n", msg);
197 * remoteproc detected an exception, but error recovery is not
198 * supported. So, just log this for now
200 dev_err(dev, "K3 R5F rproc %s crashed\n", name);
202 case RP_MBOX_ECHO_REPLY:
203 dev_info(dev, "received echo reply from %s\n", name);
206 /* silently handle all other valid messages */
207 if (msg >= RP_MBOX_READY && msg < RP_MBOX_END_MSG)
209 if (msg > kproc->rproc->max_notifyid) {
210 dev_dbg(dev, "dropping unknown message 0x%x", msg);
213 /* msg contains the index of the triggered vring */
214 if (rproc_vq_interrupt(kproc->rproc, msg) == IRQ_NONE)
215 dev_dbg(dev, "no message was found in vqid %d\n", msg);
219 /* kick a virtqueue */
220 static void k3_r5_rproc_kick(struct rproc *rproc, int vqid)
222 struct k3_r5_rproc *kproc = rproc->priv;
223 struct device *dev = rproc->dev.parent;
224 mbox_msg_t msg = (mbox_msg_t)vqid;
227 /* send the index of the triggered virtqueue in the mailbox payload */
228 ret = mbox_send_message(kproc->mbox, (void *)msg);
230 dev_err(dev, "failed to send mailbox message, status = %d\n",
234 static int k3_r5_split_reset(struct k3_r5_core *core)
238 ret = reset_control_assert(core->reset);
240 dev_err(core->dev, "local-reset assert failed, ret = %d\n",
245 ret = core->ti_sci->ops.dev_ops.put_device(core->ti_sci,
248 dev_err(core->dev, "module-reset assert failed, ret = %d\n",
250 if (reset_control_deassert(core->reset))
251 dev_warn(core->dev, "local-reset deassert back failed\n");
257 static int k3_r5_split_release(struct k3_r5_core *core)
261 ret = core->ti_sci->ops.dev_ops.get_device(core->ti_sci,
264 dev_err(core->dev, "module-reset deassert failed, ret = %d\n",
269 ret = reset_control_deassert(core->reset);
271 dev_err(core->dev, "local-reset deassert failed, ret = %d\n",
273 if (core->ti_sci->ops.dev_ops.put_device(core->ti_sci,
275 dev_warn(core->dev, "module-reset assert back failed\n");
281 static int k3_r5_lockstep_reset(struct k3_r5_cluster *cluster)
283 struct k3_r5_core *core;
286 /* assert local reset on all applicable cores */
287 list_for_each_entry(core, &cluster->cores, elem) {
288 ret = reset_control_assert(core->reset);
290 dev_err(core->dev, "local-reset assert failed, ret = %d\n",
292 core = list_prev_entry(core, elem);
293 goto unroll_local_reset;
297 /* disable PSC modules on all applicable cores */
298 list_for_each_entry(core, &cluster->cores, elem) {
299 ret = core->ti_sci->ops.dev_ops.put_device(core->ti_sci,
302 dev_err(core->dev, "module-reset assert failed, ret = %d\n",
304 goto unroll_module_reset;
311 list_for_each_entry_continue_reverse(core, &cluster->cores, elem) {
312 if (core->ti_sci->ops.dev_ops.put_device(core->ti_sci,
314 dev_warn(core->dev, "module-reset assert back failed\n");
316 core = list_last_entry(&cluster->cores, struct k3_r5_core, elem);
318 list_for_each_entry_from_reverse(core, &cluster->cores, elem) {
319 if (reset_control_deassert(core->reset))
320 dev_warn(core->dev, "local-reset deassert back failed\n");
326 static int k3_r5_lockstep_release(struct k3_r5_cluster *cluster)
328 struct k3_r5_core *core;
331 /* enable PSC modules on all applicable cores */
332 list_for_each_entry_reverse(core, &cluster->cores, elem) {
333 ret = core->ti_sci->ops.dev_ops.get_device(core->ti_sci,
336 dev_err(core->dev, "module-reset deassert failed, ret = %d\n",
338 core = list_next_entry(core, elem);
339 goto unroll_module_reset;
343 /* deassert local reset on all applicable cores */
344 list_for_each_entry_reverse(core, &cluster->cores, elem) {
345 ret = reset_control_deassert(core->reset);
347 dev_err(core->dev, "module-reset deassert failed, ret = %d\n",
349 goto unroll_local_reset;
356 list_for_each_entry_continue(core, &cluster->cores, elem) {
357 if (reset_control_assert(core->reset))
358 dev_warn(core->dev, "local-reset assert back failed\n");
360 core = list_first_entry(&cluster->cores, struct k3_r5_core, elem);
362 list_for_each_entry_from(core, &cluster->cores, elem) {
363 if (core->ti_sci->ops.dev_ops.put_device(core->ti_sci,
365 dev_warn(core->dev, "module-reset assert back failed\n");
371 static inline int k3_r5_core_halt(struct k3_r5_core *core)
373 return ti_sci_proc_set_control(core->tsp,
374 PROC_BOOT_CTRL_FLAG_R5_CORE_HALT, 0);
377 static inline int k3_r5_core_run(struct k3_r5_core *core)
379 return ti_sci_proc_set_control(core->tsp,
380 0, PROC_BOOT_CTRL_FLAG_R5_CORE_HALT);
383 static int k3_r5_rproc_request_mbox(struct rproc *rproc)
385 struct k3_r5_rproc *kproc = rproc->priv;
386 struct mbox_client *client = &kproc->client;
387 struct device *dev = kproc->dev;
391 client->tx_done = NULL;
392 client->rx_callback = k3_r5_rproc_mbox_callback;
393 client->tx_block = false;
394 client->knows_txdone = false;
396 kproc->mbox = mbox_request_channel(client, 0);
397 if (IS_ERR(kproc->mbox)) {
399 dev_err(dev, "mbox_request_channel failed: %ld\n",
400 PTR_ERR(kproc->mbox));
405 * Ping the remote processor, this is only for sanity-sake for now;
406 * there is no functional effect whatsoever.
408 * Note that the reply will _not_ arrive immediately: this message
409 * will wait in the mailbox fifo until the remote processor is booted.
411 ret = mbox_send_message(kproc->mbox, (void *)RP_MBOX_ECHO_REQUEST);
413 dev_err(dev, "mbox_send_message failed: %d\n", ret);
414 mbox_free_channel(kproc->mbox);
422 * The R5F cores have controls for both a reset and a halt/run. The code
423 * execution from DDR requires the initial boot-strapping code to be run
424 * from the internal TCMs. This function is used to release the resets on
425 * applicable cores to allow loading into the TCMs. The .prepare() ops is
426 * invoked by remoteproc core before any firmware loading, and is followed
427 * by the .start() ops after loading to actually let the R5 cores run.
429 * The Single-CPU mode on applicable SoCs (eg: AM64x) only uses Core0 to
430 * execute code, but combines the TCMs from both cores. The resets for both
431 * cores need to be released to make this possible, as the TCMs are in general
432 * private to each core. Only Core0 needs to be unhalted for running the
433 * cluster in this mode. The function uses the same reset logic as LockStep
434 * mode for this (though the behavior is agnostic of the reset release order).
435 * This callback is invoked only in remoteproc mode.
437 static int k3_r5_rproc_prepare(struct rproc *rproc)
439 struct k3_r5_rproc *kproc = rproc->priv;
440 struct k3_r5_cluster *cluster = kproc->cluster;
441 struct k3_r5_core *core = kproc->core;
442 struct device *dev = kproc->dev;
443 u32 ctrl = 0, cfg = 0, stat = 0;
448 ret = ti_sci_proc_get_status(core->tsp, &boot_vec, &cfg, &ctrl, &stat);
451 mem_init_dis = !!(cfg & PROC_BOOT_CFG_FLAG_R5_MEM_INIT_DIS);
453 /* Re-use LockStep-mode reset logic for Single-CPU mode */
454 ret = (cluster->mode == CLUSTER_MODE_LOCKSTEP ||
455 cluster->mode == CLUSTER_MODE_SINGLECPU) ?
456 k3_r5_lockstep_release(cluster) : k3_r5_split_release(core);
458 dev_err(dev, "unable to enable cores for TCM loading, ret = %d\n",
464 * Newer IP revisions like on J7200 SoCs support h/w auto-initialization
465 * of TCMs, so there is no need to perform the s/w memzero. This bit is
466 * configurable through System Firmware, the default value does perform
467 * auto-init, but account for it in case it is disabled
469 if (cluster->soc_data->tcm_ecc_autoinit && !mem_init_dis) {
470 dev_dbg(dev, "leveraging h/w init for TCM memories\n");
475 * Zero out both TCMs unconditionally (access from v8 Arm core is not
476 * affected by ATCM & BTCM enable configuration values) so that ECC
477 * can be effective on all TCM addresses.
479 dev_dbg(dev, "zeroing out ATCM memory\n");
480 memset(core->mem[0].cpu_addr, 0x00, core->mem[0].size);
482 dev_dbg(dev, "zeroing out BTCM memory\n");
483 memset(core->mem[1].cpu_addr, 0x00, core->mem[1].size);
489 * This function implements the .unprepare() ops and performs the complimentary
490 * operations to that of the .prepare() ops. The function is used to assert the
491 * resets on all applicable cores for the rproc device (depending on LockStep
492 * or Split mode). This completes the second portion of powering down the R5F
493 * cores. The cores themselves are only halted in the .stop() ops, and the
494 * .unprepare() ops is invoked by the remoteproc core after the remoteproc is
497 * The Single-CPU mode on applicable SoCs (eg: AM64x) combines the TCMs from
498 * both cores. The access is made possible only with releasing the resets for
499 * both cores, but with only Core0 unhalted. This function re-uses the same
500 * reset assert logic as LockStep mode for this mode (though the behavior is
501 * agnostic of the reset assert order). This callback is invoked only in
504 static int k3_r5_rproc_unprepare(struct rproc *rproc)
506 struct k3_r5_rproc *kproc = rproc->priv;
507 struct k3_r5_cluster *cluster = kproc->cluster;
508 struct k3_r5_core *core = kproc->core;
509 struct device *dev = kproc->dev;
512 /* Re-use LockStep-mode reset logic for Single-CPU mode */
513 ret = (cluster->mode == CLUSTER_MODE_LOCKSTEP ||
514 cluster->mode == CLUSTER_MODE_SINGLECPU) ?
515 k3_r5_lockstep_reset(cluster) : k3_r5_split_reset(core);
517 dev_err(dev, "unable to disable cores, ret = %d\n", ret);
523 * The R5F start sequence includes two different operations
524 * 1. Configure the boot vector for R5F core(s)
525 * 2. Unhalt/Run the R5F core(s)
527 * The sequence is different between LockStep and Split modes. The LockStep
528 * mode requires the boot vector to be configured only for Core0, and then
529 * unhalt both the cores to start the execution - Core1 needs to be unhalted
530 * first followed by Core0. The Split-mode requires that Core0 to be maintained
531 * always in a higher power state that Core1 (implying Core1 needs to be started
532 * always only after Core0 is started).
534 * The Single-CPU mode on applicable SoCs (eg: AM64x) only uses Core0 to execute
535 * code, so only Core0 needs to be unhalted. The function uses the same logic
536 * flow as Split-mode for this. This callback is invoked only in remoteproc
539 static int k3_r5_rproc_start(struct rproc *rproc)
541 struct k3_r5_rproc *kproc = rproc->priv;
542 struct k3_r5_cluster *cluster = kproc->cluster;
543 struct device *dev = kproc->dev;
544 struct k3_r5_core *core;
548 ret = k3_r5_rproc_request_mbox(rproc);
552 boot_addr = rproc->bootaddr;
553 /* TODO: add boot_addr sanity checking */
554 dev_dbg(dev, "booting R5F core using boot addr = 0x%x\n", boot_addr);
556 /* boot vector need not be programmed for Core1 in LockStep mode */
558 ret = ti_sci_proc_set_config(core->tsp, boot_addr, 0, 0);
562 /* unhalt/run all applicable cores */
563 if (cluster->mode == CLUSTER_MODE_LOCKSTEP) {
564 list_for_each_entry_reverse(core, &cluster->cores, elem) {
565 ret = k3_r5_core_run(core);
567 goto unroll_core_run;
570 ret = k3_r5_core_run(core);
578 list_for_each_entry_continue(core, &cluster->cores, elem) {
579 if (k3_r5_core_halt(core))
580 dev_warn(core->dev, "core halt back failed\n");
583 mbox_free_channel(kproc->mbox);
588 * The R5F stop function includes the following operations
589 * 1. Halt R5F core(s)
591 * The sequence is different between LockStep and Split modes, and the order
592 * of cores the operations are performed are also in general reverse to that
593 * of the start function. The LockStep mode requires each operation to be
594 * performed first on Core0 followed by Core1. The Split-mode requires that
595 * Core0 to be maintained always in a higher power state that Core1 (implying
596 * Core1 needs to be stopped first before Core0).
598 * The Single-CPU mode on applicable SoCs (eg: AM64x) only uses Core0 to execute
599 * code, so only Core0 needs to be halted. The function uses the same logic
600 * flow as Split-mode for this.
602 * Note that the R5F halt operation in general is not effective when the R5F
603 * core is running, but is needed to make sure the core won't run after
604 * deasserting the reset the subsequent time. The asserting of reset can
605 * be done here, but is preferred to be done in the .unprepare() ops - this
606 * maintains the symmetric behavior between the .start(), .stop(), .prepare()
607 * and .unprepare() ops, and also balances them well between sysfs 'state'
608 * flow and device bind/unbind or module removal. This callback is invoked
609 * only in remoteproc mode.
611 static int k3_r5_rproc_stop(struct rproc *rproc)
613 struct k3_r5_rproc *kproc = rproc->priv;
614 struct k3_r5_cluster *cluster = kproc->cluster;
615 struct k3_r5_core *core = kproc->core;
618 /* halt all applicable cores */
619 if (cluster->mode == CLUSTER_MODE_LOCKSTEP) {
620 list_for_each_entry(core, &cluster->cores, elem) {
621 ret = k3_r5_core_halt(core);
623 core = list_prev_entry(core, elem);
624 goto unroll_core_halt;
628 ret = k3_r5_core_halt(core);
633 mbox_free_channel(kproc->mbox);
638 list_for_each_entry_from_reverse(core, &cluster->cores, elem) {
639 if (k3_r5_core_run(core))
640 dev_warn(core->dev, "core run back failed\n");
647 * Attach to a running R5F remote processor (IPC-only mode)
649 * The R5F attach callback only needs to request the mailbox, the remote
650 * processor is already booted, so there is no need to issue any TI-SCI
651 * commands to boot the R5F cores in IPC-only mode. This callback is invoked
652 * only in IPC-only mode.
654 static int k3_r5_rproc_attach(struct rproc *rproc)
656 struct k3_r5_rproc *kproc = rproc->priv;
657 struct device *dev = kproc->dev;
660 ret = k3_r5_rproc_request_mbox(rproc);
664 dev_info(dev, "R5F core initialized in IPC-only mode\n");
669 * Detach from a running R5F remote processor (IPC-only mode)
671 * The R5F detach callback performs the opposite operation to attach callback
672 * and only needs to release the mailbox, the R5F cores are not stopped and
673 * will be left in booted state in IPC-only mode. This callback is invoked
674 * only in IPC-only mode.
676 static int k3_r5_rproc_detach(struct rproc *rproc)
678 struct k3_r5_rproc *kproc = rproc->priv;
679 struct device *dev = kproc->dev;
681 mbox_free_channel(kproc->mbox);
682 dev_info(dev, "R5F core deinitialized in IPC-only mode\n");
687 * This function implements the .get_loaded_rsc_table() callback and is used
688 * to provide the resource table for the booted R5F in IPC-only mode. The K3 R5F
689 * firmwares follow a design-by-contract approach and are expected to have the
690 * resource table at the base of the DDR region reserved for firmware usage.
691 * This provides flexibility for the remote processor to be booted by different
692 * bootloaders that may or may not have the ability to publish the resource table
693 * address and size through a DT property. This callback is invoked only in
696 static struct resource_table *k3_r5_get_loaded_rsc_table(struct rproc *rproc,
697 size_t *rsc_table_sz)
699 struct k3_r5_rproc *kproc = rproc->priv;
700 struct device *dev = kproc->dev;
702 if (!kproc->rmem[0].cpu_addr) {
703 dev_err(dev, "memory-region #1 does not exist, loaded rsc table can't be found");
704 return ERR_PTR(-ENOMEM);
708 * NOTE: The resource table size is currently hard-coded to a maximum
709 * of 256 bytes. The most common resource table usage for K3 firmwares
710 * is to only have the vdev resource entry and an optional trace entry.
711 * The exact size could be computed based on resource table address, but
712 * the hard-coded value suffices to support the IPC-only mode.
715 return (struct resource_table *)kproc->rmem[0].cpu_addr;
719 * Internal Memory translation helper
721 * Custom function implementing the rproc .da_to_va ops to provide address
722 * translation (device address to kernel virtual address) for internal RAMs
723 * present in a DSP or IPU device). The translated addresses can be used
724 * either by the remoteproc core for loading, or by any rpmsg bus drivers.
726 static void *k3_r5_rproc_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_iomem)
728 struct k3_r5_rproc *kproc = rproc->priv;
729 struct k3_r5_core *core = kproc->core;
730 void __iomem *va = NULL;
731 phys_addr_t bus_addr;
732 u32 dev_addr, offset;
739 /* handle both R5 and SoC views of ATCM and BTCM */
740 for (i = 0; i < core->num_mems; i++) {
741 bus_addr = core->mem[i].bus_addr;
742 dev_addr = core->mem[i].dev_addr;
743 size = core->mem[i].size;
745 /* handle R5-view addresses of TCMs */
746 if (da >= dev_addr && ((da + len) <= (dev_addr + size))) {
747 offset = da - dev_addr;
748 va = core->mem[i].cpu_addr + offset;
749 return (__force void *)va;
752 /* handle SoC-view addresses of TCMs */
753 if (da >= bus_addr && ((da + len) <= (bus_addr + size))) {
754 offset = da - bus_addr;
755 va = core->mem[i].cpu_addr + offset;
756 return (__force void *)va;
760 /* handle any SRAM regions using SoC-view addresses */
761 for (i = 0; i < core->num_sram; i++) {
762 dev_addr = core->sram[i].dev_addr;
763 size = core->sram[i].size;
765 if (da >= dev_addr && ((da + len) <= (dev_addr + size))) {
766 offset = da - dev_addr;
767 va = core->sram[i].cpu_addr + offset;
768 return (__force void *)va;
772 /* handle static DDR reserved memory regions */
773 for (i = 0; i < kproc->num_rmems; i++) {
774 dev_addr = kproc->rmem[i].dev_addr;
775 size = kproc->rmem[i].size;
777 if (da >= dev_addr && ((da + len) <= (dev_addr + size))) {
778 offset = da - dev_addr;
779 va = kproc->rmem[i].cpu_addr + offset;
780 return (__force void *)va;
787 static const struct rproc_ops k3_r5_rproc_ops = {
788 .prepare = k3_r5_rproc_prepare,
789 .unprepare = k3_r5_rproc_unprepare,
790 .start = k3_r5_rproc_start,
791 .stop = k3_r5_rproc_stop,
792 .kick = k3_r5_rproc_kick,
793 .da_to_va = k3_r5_rproc_da_to_va,
797 * Internal R5F Core configuration
799 * Each R5FSS has a cluster-level setting for configuring the processor
800 * subsystem either in a safety/fault-tolerant LockStep mode or a performance
801 * oriented Split mode on most SoCs. A fewer SoCs support a non-safety mode
802 * as an alternate for LockStep mode that exercises only a single R5F core
803 * called Single-CPU mode. Each R5F core has a number of settings to either
804 * enable/disable each of the TCMs, control which TCM appears at the R5F core's
805 * address 0x0. These settings need to be configured before the resets for the
806 * corresponding core are released. These settings are all protected and managed
807 * by the System Processor.
809 * This function is used to pre-configure these settings for each R5F core, and
810 * the configuration is all done through various ti_sci_proc functions that
811 * communicate with the System Processor. The function also ensures that both
812 * the cores are halted before the .prepare() step.
814 * The function is called from k3_r5_cluster_rproc_init() and is invoked either
815 * once (in LockStep mode or Single-CPU modes) or twice (in Split mode). Support
816 * for LockStep-mode is dictated by an eFUSE register bit, and the config
817 * settings retrieved from DT are adjusted accordingly as per the permitted
818 * cluster mode. Another eFUSE register bit dictates if the R5F cluster only
819 * supports a Single-CPU mode. All cluster level settings like Cluster mode and
820 * TEINIT (exception handling state dictating ARM or Thumb mode) can only be set
821 * and retrieved using Core0.
823 * The function behavior is different based on the cluster mode. The R5F cores
824 * are configured independently as per their individual settings in Split mode.
825 * They are identically configured in LockStep mode using the primary Core0
826 * settings. However, some individual settings cannot be set in LockStep mode.
827 * This is overcome by switching to Split-mode initially and then programming
828 * both the cores with the same settings, before reconfiguing again for
831 static int k3_r5_rproc_configure(struct k3_r5_rproc *kproc)
833 struct k3_r5_cluster *cluster = kproc->cluster;
834 struct device *dev = kproc->dev;
835 struct k3_r5_core *core0, *core, *temp;
836 u32 ctrl = 0, cfg = 0, stat = 0;
837 u32 set_cfg = 0, clr_cfg = 0;
843 core0 = list_first_entry(&cluster->cores, struct k3_r5_core, elem);
844 if (cluster->mode == CLUSTER_MODE_LOCKSTEP ||
845 cluster->mode == CLUSTER_MODE_SINGLECPU ||
846 cluster->mode == CLUSTER_MODE_SINGLECORE) {
852 ret = ti_sci_proc_get_status(core->tsp, &boot_vec, &cfg, &ctrl,
857 dev_dbg(dev, "boot_vector = 0x%llx, cfg = 0x%x ctrl = 0x%x stat = 0x%x\n",
858 boot_vec, cfg, ctrl, stat);
860 single_cpu = !!(stat & PROC_BOOT_STATUS_FLAG_R5_SINGLECORE_ONLY);
861 lockstep_en = !!(stat & PROC_BOOT_STATUS_FLAG_R5_LOCKSTEP_PERMITTED);
863 /* Override to single CPU mode if set in status flag */
864 if (single_cpu && cluster->mode == CLUSTER_MODE_SPLIT) {
865 dev_err(cluster->dev, "split-mode not permitted, force configuring for single-cpu mode\n");
866 cluster->mode = CLUSTER_MODE_SINGLECPU;
869 /* Override to split mode if lockstep enable bit is not set in status flag */
870 if (!lockstep_en && cluster->mode == CLUSTER_MODE_LOCKSTEP) {
871 dev_err(cluster->dev, "lockstep mode not permitted, force configuring for split-mode\n");
872 cluster->mode = CLUSTER_MODE_SPLIT;
875 /* always enable ARM mode and set boot vector to 0 */
878 clr_cfg = PROC_BOOT_CFG_FLAG_R5_TEINIT;
880 * Single-CPU configuration bit can only be configured
881 * on Core0 and system firmware will NACK any requests
882 * with the bit configured, so program it only on
885 if (cluster->mode == CLUSTER_MODE_SINGLECPU ||
886 cluster->mode == CLUSTER_MODE_SINGLECORE) {
887 set_cfg = PROC_BOOT_CFG_FLAG_R5_SINGLE_CORE;
890 * LockStep configuration bit is Read-only on Split-mode
891 * _only_ devices and system firmware will NACK any
892 * requests with the bit configured, so program it only
893 * on permitted devices
896 clr_cfg |= PROC_BOOT_CFG_FLAG_R5_LOCKSTEP;
900 if (core->atcm_enable)
901 set_cfg |= PROC_BOOT_CFG_FLAG_R5_ATCM_EN;
903 clr_cfg |= PROC_BOOT_CFG_FLAG_R5_ATCM_EN;
905 if (core->btcm_enable)
906 set_cfg |= PROC_BOOT_CFG_FLAG_R5_BTCM_EN;
908 clr_cfg |= PROC_BOOT_CFG_FLAG_R5_BTCM_EN;
911 set_cfg |= PROC_BOOT_CFG_FLAG_R5_TCM_RSTBASE;
913 clr_cfg |= PROC_BOOT_CFG_FLAG_R5_TCM_RSTBASE;
915 if (cluster->mode == CLUSTER_MODE_LOCKSTEP) {
917 * work around system firmware limitations to make sure both
918 * cores are programmed symmetrically in LockStep. LockStep
919 * and TEINIT config is only allowed with Core0.
921 list_for_each_entry(temp, &cluster->cores, elem) {
922 ret = k3_r5_core_halt(temp);
927 clr_cfg &= ~PROC_BOOT_CFG_FLAG_R5_LOCKSTEP;
928 clr_cfg &= ~PROC_BOOT_CFG_FLAG_R5_TEINIT;
930 ret = ti_sci_proc_set_config(temp->tsp, boot_vec,
936 set_cfg = PROC_BOOT_CFG_FLAG_R5_LOCKSTEP;
938 ret = ti_sci_proc_set_config(core->tsp, boot_vec,
941 ret = k3_r5_core_halt(core);
945 ret = ti_sci_proc_set_config(core->tsp, boot_vec,
953 static int k3_r5_reserved_mem_init(struct k3_r5_rproc *kproc)
955 struct device *dev = kproc->dev;
956 struct device_node *np = dev_of_node(dev);
957 struct device_node *rmem_np;
958 struct reserved_mem *rmem;
962 num_rmems = of_property_count_elems_of_size(np, "memory-region",
964 if (num_rmems <= 0) {
965 dev_err(dev, "device does not have reserved memory regions, ret = %d\n",
970 dev_err(dev, "device needs at least two memory regions to be defined, num = %d\n",
975 /* use reserved memory region 0 for vring DMA allocations */
976 ret = of_reserved_mem_device_init_by_idx(dev, np, 0);
978 dev_err(dev, "device cannot initialize DMA pool, ret = %d\n",
984 kproc->rmem = kcalloc(num_rmems, sizeof(*kproc->rmem), GFP_KERNEL);
990 /* use remaining reserved memory regions for static carveouts */
991 for (i = 0; i < num_rmems; i++) {
992 rmem_np = of_parse_phandle(np, "memory-region", i + 1);
998 rmem = of_reserved_mem_lookup(rmem_np);
1000 of_node_put(rmem_np);
1004 of_node_put(rmem_np);
1006 kproc->rmem[i].bus_addr = rmem->base;
1008 * R5Fs do not have an MMU, but have a Region Address Translator
1009 * (RAT) module that provides a fixed entry translation between
1010 * the 32-bit processor addresses to 64-bit bus addresses. The
1011 * RAT is programmable only by the R5F cores. Support for RAT
1012 * is currently not supported, so 64-bit address regions are not
1013 * supported. The absence of MMUs implies that the R5F device
1014 * addresses/supported memory regions are restricted to 32-bit
1015 * bus addresses, and are identical
1017 kproc->rmem[i].dev_addr = (u32)rmem->base;
1018 kproc->rmem[i].size = rmem->size;
1019 kproc->rmem[i].cpu_addr = ioremap_wc(rmem->base, rmem->size);
1020 if (!kproc->rmem[i].cpu_addr) {
1021 dev_err(dev, "failed to map reserved memory#%d at %pa of size %pa\n",
1022 i + 1, &rmem->base, &rmem->size);
1027 dev_dbg(dev, "reserved memory%d: bus addr %pa size 0x%zx va %pK da 0x%x\n",
1028 i + 1, &kproc->rmem[i].bus_addr,
1029 kproc->rmem[i].size, kproc->rmem[i].cpu_addr,
1030 kproc->rmem[i].dev_addr);
1032 kproc->num_rmems = num_rmems;
1037 for (i--; i >= 0; i--)
1038 iounmap(kproc->rmem[i].cpu_addr);
1041 of_reserved_mem_device_release(dev);
1045 static void k3_r5_reserved_mem_exit(struct k3_r5_rproc *kproc)
1049 for (i = 0; i < kproc->num_rmems; i++)
1050 iounmap(kproc->rmem[i].cpu_addr);
1053 of_reserved_mem_device_release(kproc->dev);
1057 * Each R5F core within a typical R5FSS instance has a total of 64 KB of TCMs,
1058 * split equally into two 32 KB banks between ATCM and BTCM. The TCMs from both
1059 * cores are usable in Split-mode, but only the Core0 TCMs can be used in
1060 * LockStep-mode. The newer revisions of the R5FSS IP maximizes these TCMs by
1061 * leveraging the Core1 TCMs as well in certain modes where they would have
1062 * otherwise been unusable (Eg: LockStep-mode on J7200 SoCs, Single-CPU mode on
1063 * AM64x SoCs). This is done by making a Core1 TCM visible immediately after the
1064 * corresponding Core0 TCM. The SoC memory map uses the larger 64 KB sizes for
1065 * the Core0 TCMs, and the dts representation reflects this increased size on
1066 * supported SoCs. The Core0 TCM sizes therefore have to be adjusted to only
1067 * half the original size in Split mode.
1069 static void k3_r5_adjust_tcm_sizes(struct k3_r5_rproc *kproc)
1071 struct k3_r5_cluster *cluster = kproc->cluster;
1072 struct k3_r5_core *core = kproc->core;
1073 struct device *cdev = core->dev;
1074 struct k3_r5_core *core0;
1076 if (cluster->mode == CLUSTER_MODE_LOCKSTEP ||
1077 cluster->mode == CLUSTER_MODE_SINGLECPU ||
1078 cluster->mode == CLUSTER_MODE_SINGLECORE ||
1079 !cluster->soc_data->tcm_is_double)
1082 core0 = list_first_entry(&cluster->cores, struct k3_r5_core, elem);
1083 if (core == core0) {
1084 WARN_ON(core->mem[0].size != SZ_64K);
1085 WARN_ON(core->mem[1].size != SZ_64K);
1087 core->mem[0].size /= 2;
1088 core->mem[1].size /= 2;
1090 dev_dbg(cdev, "adjusted TCM sizes, ATCM = 0x%zx BTCM = 0x%zx\n",
1091 core->mem[0].size, core->mem[1].size);
1096 * This function checks and configures a R5F core for IPC-only or remoteproc
1097 * mode. The driver is configured to be in IPC-only mode for a R5F core when
1098 * the core has been loaded and started by a bootloader. The IPC-only mode is
1099 * detected by querying the System Firmware for reset, power on and halt status
1100 * and ensuring that the core is running. Any incomplete steps at bootloader
1101 * are validated and errored out.
1103 * In IPC-only mode, the driver state flags for ATCM, BTCM and LOCZRAMA settings
1104 * and cluster mode parsed originally from kernel DT are updated to reflect the
1105 * actual values configured by bootloader. The driver internal device memory
1106 * addresses for TCMs are also updated.
1108 static int k3_r5_rproc_configure_mode(struct k3_r5_rproc *kproc)
1110 struct k3_r5_cluster *cluster = kproc->cluster;
1111 struct k3_r5_core *core = kproc->core;
1112 struct device *cdev = core->dev;
1113 bool r_state = false, c_state = false, lockstep_en = false, single_cpu = false;
1114 u32 ctrl = 0, cfg = 0, stat = 0, halted = 0;
1116 u32 atcm_enable, btcm_enable, loczrama;
1117 struct k3_r5_core *core0;
1118 enum cluster_mode mode = cluster->mode;
1121 core0 = list_first_entry(&cluster->cores, struct k3_r5_core, elem);
1123 ret = core->ti_sci->ops.dev_ops.is_on(core->ti_sci, core->ti_sci_id,
1124 &r_state, &c_state);
1126 dev_err(cdev, "failed to get initial state, mode cannot be determined, ret = %d\n",
1130 if (r_state != c_state) {
1131 dev_warn(cdev, "R5F core may have been powered on by a different host, programmed state (%d) != actual state (%d)\n",
1135 ret = reset_control_status(core->reset);
1137 dev_err(cdev, "failed to get initial local reset status, ret = %d\n",
1142 ret = ti_sci_proc_get_status(core->tsp, &boot_vec, &cfg, &ctrl,
1145 dev_err(cdev, "failed to get initial processor status, ret = %d\n",
1149 atcm_enable = cfg & PROC_BOOT_CFG_FLAG_R5_ATCM_EN ? 1 : 0;
1150 btcm_enable = cfg & PROC_BOOT_CFG_FLAG_R5_BTCM_EN ? 1 : 0;
1151 loczrama = cfg & PROC_BOOT_CFG_FLAG_R5_TCM_RSTBASE ? 1 : 0;
1152 single_cpu = cfg & PROC_BOOT_CFG_FLAG_R5_SINGLE_CORE ? 1 : 0;
1153 lockstep_en = cfg & PROC_BOOT_CFG_FLAG_R5_LOCKSTEP ? 1 : 0;
1155 if (single_cpu && mode != CLUSTER_MODE_SINGLECORE)
1156 mode = CLUSTER_MODE_SINGLECPU;
1158 mode = CLUSTER_MODE_LOCKSTEP;
1160 halted = ctrl & PROC_BOOT_CTRL_FLAG_R5_CORE_HALT;
1163 * IPC-only mode detection requires both local and module resets to
1164 * be deasserted and R5F core to be unhalted. Local reset status is
1165 * irrelevant if module reset is asserted (POR value has local reset
1166 * deasserted), and is deemed as remoteproc mode
1168 if (c_state && !ret && !halted) {
1169 dev_info(cdev, "configured R5F for IPC-only mode\n");
1170 kproc->rproc->state = RPROC_DETACHED;
1172 /* override rproc ops with only required IPC-only mode ops */
1173 kproc->rproc->ops->prepare = NULL;
1174 kproc->rproc->ops->unprepare = NULL;
1175 kproc->rproc->ops->start = NULL;
1176 kproc->rproc->ops->stop = NULL;
1177 kproc->rproc->ops->attach = k3_r5_rproc_attach;
1178 kproc->rproc->ops->detach = k3_r5_rproc_detach;
1179 kproc->rproc->ops->get_loaded_rsc_table =
1180 k3_r5_get_loaded_rsc_table;
1181 } else if (!c_state) {
1182 dev_info(cdev, "configured R5F for remoteproc mode\n");
1185 dev_err(cdev, "mismatched mode: local_reset = %s, module_reset = %s, core_state = %s\n",
1186 !ret ? "deasserted" : "asserted",
1187 c_state ? "deasserted" : "asserted",
1188 halted ? "halted" : "unhalted");
1192 /* fixup TCMs, cluster & core flags to actual values in IPC-only mode */
1195 cluster->mode = mode;
1196 core->atcm_enable = atcm_enable;
1197 core->btcm_enable = btcm_enable;
1198 core->loczrama = loczrama;
1199 core->mem[0].dev_addr = loczrama ? 0 : K3_R5_TCM_DEV_ADDR;
1200 core->mem[1].dev_addr = loczrama ? K3_R5_TCM_DEV_ADDR : 0;
1206 static int k3_r5_cluster_rproc_init(struct platform_device *pdev)
1208 struct k3_r5_cluster *cluster = platform_get_drvdata(pdev);
1209 struct device *dev = &pdev->dev;
1210 struct k3_r5_rproc *kproc;
1211 struct k3_r5_core *core, *core1;
1212 struct device *cdev;
1213 const char *fw_name;
1214 struct rproc *rproc;
1217 core1 = list_last_entry(&cluster->cores, struct k3_r5_core, elem);
1218 list_for_each_entry(core, &cluster->cores, elem) {
1220 ret = rproc_of_parse_firmware(cdev, 0, &fw_name);
1222 dev_err(dev, "failed to parse firmware-name property, ret = %d\n",
1227 rproc = rproc_alloc(cdev, dev_name(cdev), &k3_r5_rproc_ops,
1228 fw_name, sizeof(*kproc));
1234 /* K3 R5s have a Region Address Translator (RAT) but no MMU */
1235 rproc->has_iommu = false;
1236 /* error recovery is not supported at present */
1237 rproc->recovery_disabled = true;
1239 kproc = rproc->priv;
1240 kproc->cluster = cluster;
1243 kproc->rproc = rproc;
1244 core->rproc = rproc;
1246 ret = k3_r5_rproc_configure_mode(kproc);
1252 ret = k3_r5_rproc_configure(kproc);
1254 dev_err(dev, "initial configure failed, ret = %d\n",
1260 k3_r5_adjust_tcm_sizes(kproc);
1262 ret = k3_r5_reserved_mem_init(kproc);
1264 dev_err(dev, "reserved memory init failed, ret = %d\n",
1269 ret = rproc_add(rproc);
1271 dev_err(dev, "rproc_add failed, ret = %d\n", ret);
1275 /* create only one rproc in lockstep, single-cpu or
1278 if (cluster->mode == CLUSTER_MODE_LOCKSTEP ||
1279 cluster->mode == CLUSTER_MODE_SINGLECPU ||
1280 cluster->mode == CLUSTER_MODE_SINGLECORE)
1287 if (rproc->state == RPROC_ATTACHED) {
1288 ret1 = rproc_detach(rproc);
1290 dev_err(kproc->dev, "failed to detach rproc, ret = %d\n",
1298 k3_r5_reserved_mem_exit(kproc);
1303 /* undo core0 upon any failures on core1 in split-mode */
1304 if (cluster->mode == CLUSTER_MODE_SPLIT && core == core1) {
1305 core = list_prev_entry(core, elem);
1306 rproc = core->rproc;
1307 kproc = rproc->priv;
1313 static void k3_r5_cluster_rproc_exit(void *data)
1315 struct k3_r5_cluster *cluster = platform_get_drvdata(data);
1316 struct k3_r5_rproc *kproc;
1317 struct k3_r5_core *core;
1318 struct rproc *rproc;
1322 * lockstep mode and single-cpu modes have only one rproc associated
1323 * with first core, whereas split-mode has two rprocs associated with
1324 * each core, and requires that core1 be powered down first
1326 core = (cluster->mode == CLUSTER_MODE_LOCKSTEP ||
1327 cluster->mode == CLUSTER_MODE_SINGLECPU) ?
1328 list_first_entry(&cluster->cores, struct k3_r5_core, elem) :
1329 list_last_entry(&cluster->cores, struct k3_r5_core, elem);
1331 list_for_each_entry_from_reverse(core, &cluster->cores, elem) {
1332 rproc = core->rproc;
1333 kproc = rproc->priv;
1335 if (rproc->state == RPROC_ATTACHED) {
1336 ret = rproc_detach(rproc);
1338 dev_err(kproc->dev, "failed to detach rproc, ret = %d\n", ret);
1345 k3_r5_reserved_mem_exit(kproc);
1352 static int k3_r5_core_of_get_internal_memories(struct platform_device *pdev,
1353 struct k3_r5_core *core)
1355 static const char * const mem_names[] = {"atcm", "btcm"};
1356 struct device *dev = &pdev->dev;
1357 struct resource *res;
1361 num_mems = ARRAY_SIZE(mem_names);
1362 core->mem = devm_kcalloc(dev, num_mems, sizeof(*core->mem), GFP_KERNEL);
1366 for (i = 0; i < num_mems; i++) {
1367 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1370 dev_err(dev, "found no memory resource for %s\n",
1374 if (!devm_request_mem_region(dev, res->start,
1377 dev_err(dev, "could not request %s region for resource\n",
1383 * TCMs are designed in general to support RAM-like backing
1384 * memories. So, map these as Normal Non-Cached memories. This
1385 * also avoids/fixes any potential alignment faults due to
1386 * unaligned data accesses when using memcpy() or memset()
1387 * functions (normally seen with device type memory).
1389 core->mem[i].cpu_addr = devm_ioremap_wc(dev, res->start,
1390 resource_size(res));
1391 if (!core->mem[i].cpu_addr) {
1392 dev_err(dev, "failed to map %s memory\n", mem_names[i]);
1395 core->mem[i].bus_addr = res->start;
1399 * The R5F cores can place ATCM & BTCM anywhere in its address
1400 * based on the corresponding Region Registers in the System
1401 * Control coprocessor. For now, place ATCM and BTCM at
1402 * addresses 0 and 0x41010000 (same as the bus address on AM65x
1403 * SoCs) based on loczrama setting
1405 if (!strcmp(mem_names[i], "atcm")) {
1406 core->mem[i].dev_addr = core->loczrama ?
1407 0 : K3_R5_TCM_DEV_ADDR;
1409 core->mem[i].dev_addr = core->loczrama ?
1410 K3_R5_TCM_DEV_ADDR : 0;
1412 core->mem[i].size = resource_size(res);
1414 dev_dbg(dev, "memory %5s: bus addr %pa size 0x%zx va %pK da 0x%x\n",
1415 mem_names[i], &core->mem[i].bus_addr,
1416 core->mem[i].size, core->mem[i].cpu_addr,
1417 core->mem[i].dev_addr);
1419 core->num_mems = num_mems;
1424 static int k3_r5_core_of_get_sram_memories(struct platform_device *pdev,
1425 struct k3_r5_core *core)
1427 struct device_node *np = pdev->dev.of_node;
1428 struct device *dev = &pdev->dev;
1429 struct device_node *sram_np;
1430 struct resource res;
1434 num_sram = of_property_count_elems_of_size(np, "sram", sizeof(phandle));
1435 if (num_sram <= 0) {
1436 dev_dbg(dev, "device does not use reserved on-chip memories, num_sram = %d\n",
1441 core->sram = devm_kcalloc(dev, num_sram, sizeof(*core->sram), GFP_KERNEL);
1445 for (i = 0; i < num_sram; i++) {
1446 sram_np = of_parse_phandle(np, "sram", i);
1450 if (!of_device_is_available(sram_np)) {
1451 of_node_put(sram_np);
1455 ret = of_address_to_resource(sram_np, 0, &res);
1456 of_node_put(sram_np);
1460 core->sram[i].bus_addr = res.start;
1461 core->sram[i].dev_addr = res.start;
1462 core->sram[i].size = resource_size(&res);
1463 core->sram[i].cpu_addr = devm_ioremap_wc(dev, res.start,
1464 resource_size(&res));
1465 if (!core->sram[i].cpu_addr) {
1466 dev_err(dev, "failed to parse and map sram%d memory at %pad\n",
1471 dev_dbg(dev, "memory sram%d: bus addr %pa size 0x%zx va %pK da 0x%x\n",
1472 i, &core->sram[i].bus_addr,
1473 core->sram[i].size, core->sram[i].cpu_addr,
1474 core->sram[i].dev_addr);
1476 core->num_sram = num_sram;
1482 struct ti_sci_proc *k3_r5_core_of_get_tsp(struct device *dev,
1483 const struct ti_sci_handle *sci)
1485 struct ti_sci_proc *tsp;
1489 ret = of_property_read_u32_array(dev_of_node(dev), "ti,sci-proc-ids",
1492 return ERR_PTR(ret);
1494 tsp = devm_kzalloc(dev, sizeof(*tsp), GFP_KERNEL);
1496 return ERR_PTR(-ENOMEM);
1500 tsp->ops = &sci->ops.proc_ops;
1501 tsp->proc_id = temp[0];
1502 tsp->host_id = temp[1];
1507 static int k3_r5_core_of_init(struct platform_device *pdev)
1509 struct device *dev = &pdev->dev;
1510 struct device_node *np = dev_of_node(dev);
1511 struct k3_r5_core *core;
1514 if (!devres_open_group(dev, k3_r5_core_of_init, GFP_KERNEL))
1517 core = devm_kzalloc(dev, sizeof(*core), GFP_KERNEL);
1525 * Use SoC Power-on-Reset values as default if no DT properties are
1526 * used to dictate the TCM configurations
1528 core->atcm_enable = 0;
1529 core->btcm_enable = 1;
1532 ret = of_property_read_u32(np, "ti,atcm-enable", &core->atcm_enable);
1533 if (ret < 0 && ret != -EINVAL) {
1534 dev_err(dev, "invalid format for ti,atcm-enable, ret = %d\n",
1539 ret = of_property_read_u32(np, "ti,btcm-enable", &core->btcm_enable);
1540 if (ret < 0 && ret != -EINVAL) {
1541 dev_err(dev, "invalid format for ti,btcm-enable, ret = %d\n",
1546 ret = of_property_read_u32(np, "ti,loczrama", &core->loczrama);
1547 if (ret < 0 && ret != -EINVAL) {
1548 dev_err(dev, "invalid format for ti,loczrama, ret = %d\n", ret);
1552 core->ti_sci = devm_ti_sci_get_by_phandle(dev, "ti,sci");
1553 if (IS_ERR(core->ti_sci)) {
1554 ret = PTR_ERR(core->ti_sci);
1555 if (ret != -EPROBE_DEFER) {
1556 dev_err(dev, "failed to get ti-sci handle, ret = %d\n",
1559 core->ti_sci = NULL;
1563 ret = of_property_read_u32(np, "ti,sci-dev-id", &core->ti_sci_id);
1565 dev_err(dev, "missing 'ti,sci-dev-id' property\n");
1569 core->reset = devm_reset_control_get_exclusive(dev, NULL);
1570 if (IS_ERR_OR_NULL(core->reset)) {
1571 ret = PTR_ERR_OR_ZERO(core->reset);
1574 if (ret != -EPROBE_DEFER) {
1575 dev_err(dev, "failed to get reset handle, ret = %d\n",
1581 core->tsp = k3_r5_core_of_get_tsp(dev, core->ti_sci);
1582 if (IS_ERR(core->tsp)) {
1583 ret = PTR_ERR(core->tsp);
1584 dev_err(dev, "failed to construct ti-sci proc control, ret = %d\n",
1589 ret = k3_r5_core_of_get_internal_memories(pdev, core);
1591 dev_err(dev, "failed to get internal memories, ret = %d\n",
1596 ret = k3_r5_core_of_get_sram_memories(pdev, core);
1598 dev_err(dev, "failed to get sram memories, ret = %d\n", ret);
1602 ret = ti_sci_proc_request(core->tsp);
1604 dev_err(dev, "ti_sci_proc_request failed, ret = %d\n", ret);
1608 platform_set_drvdata(pdev, core);
1609 devres_close_group(dev, k3_r5_core_of_init);
1614 devres_release_group(dev, k3_r5_core_of_init);
1619 * free the resources explicitly since driver model is not being used
1620 * for the child R5F devices
1622 static void k3_r5_core_of_exit(struct platform_device *pdev)
1624 struct k3_r5_core *core = platform_get_drvdata(pdev);
1625 struct device *dev = &pdev->dev;
1628 ret = ti_sci_proc_release(core->tsp);
1630 dev_err(dev, "failed to release proc, ret = %d\n", ret);
1632 platform_set_drvdata(pdev, NULL);
1633 devres_release_group(dev, k3_r5_core_of_init);
1636 static void k3_r5_cluster_of_exit(void *data)
1638 struct k3_r5_cluster *cluster = platform_get_drvdata(data);
1639 struct platform_device *cpdev;
1640 struct k3_r5_core *core, *temp;
1642 list_for_each_entry_safe_reverse(core, temp, &cluster->cores, elem) {
1643 list_del(&core->elem);
1644 cpdev = to_platform_device(core->dev);
1645 k3_r5_core_of_exit(cpdev);
1649 static int k3_r5_cluster_of_init(struct platform_device *pdev)
1651 struct k3_r5_cluster *cluster = platform_get_drvdata(pdev);
1652 struct device *dev = &pdev->dev;
1653 struct device_node *np = dev_of_node(dev);
1654 struct platform_device *cpdev;
1655 struct device_node *child;
1656 struct k3_r5_core *core;
1659 for_each_available_child_of_node(np, child) {
1660 cpdev = of_find_device_by_node(child);
1663 dev_err(dev, "could not get R5 core platform device\n");
1668 ret = k3_r5_core_of_init(cpdev);
1670 dev_err(dev, "k3_r5_core_of_init failed, ret = %d\n",
1672 put_device(&cpdev->dev);
1677 core = platform_get_drvdata(cpdev);
1678 put_device(&cpdev->dev);
1679 list_add_tail(&core->elem, &cluster->cores);
1685 k3_r5_cluster_of_exit(pdev);
1689 static int k3_r5_probe(struct platform_device *pdev)
1691 struct device *dev = &pdev->dev;
1692 struct device_node *np = dev_of_node(dev);
1693 struct k3_r5_cluster *cluster;
1694 const struct k3_r5_soc_data *data;
1698 data = of_device_get_match_data(&pdev->dev);
1700 dev_err(dev, "SoC-specific data is not defined\n");
1704 cluster = devm_kzalloc(dev, sizeof(*cluster), GFP_KERNEL);
1709 cluster->soc_data = data;
1710 INIT_LIST_HEAD(&cluster->cores);
1712 ret = of_property_read_u32(np, "ti,cluster-mode", &cluster->mode);
1713 if (ret < 0 && ret != -EINVAL) {
1714 dev_err(dev, "invalid format for ti,cluster-mode, ret = %d\n",
1719 if (ret == -EINVAL) {
1721 * default to most common efuse configurations - Split-mode on AM64x
1722 * and LockStep-mode on all others
1723 * default to most common efuse configurations -
1724 * Split-mode on AM64x
1725 * Single core on AM62x
1726 * LockStep-mode on all others
1728 if (!data->is_single_core)
1729 cluster->mode = data->single_cpu_mode ?
1730 CLUSTER_MODE_SPLIT : CLUSTER_MODE_LOCKSTEP;
1732 cluster->mode = CLUSTER_MODE_SINGLECORE;
1735 if ((cluster->mode == CLUSTER_MODE_SINGLECPU && !data->single_cpu_mode) ||
1736 (cluster->mode == CLUSTER_MODE_SINGLECORE && !data->is_single_core)) {
1737 dev_err(dev, "Cluster mode = %d is not supported on this SoC\n", cluster->mode);
1741 num_cores = of_get_available_child_count(np);
1742 if (num_cores != 2 && !data->is_single_core) {
1743 dev_err(dev, "MCU cluster requires both R5F cores to be enabled but num_cores is set to = %d\n",
1748 if (num_cores != 1 && data->is_single_core) {
1749 dev_err(dev, "SoC supports only single core R5 but num_cores is set to %d\n",
1754 platform_set_drvdata(pdev, cluster);
1756 ret = devm_of_platform_populate(dev);
1758 dev_err(dev, "devm_of_platform_populate failed, ret = %d\n",
1763 ret = k3_r5_cluster_of_init(pdev);
1765 dev_err(dev, "k3_r5_cluster_of_init failed, ret = %d\n", ret);
1769 ret = devm_add_action_or_reset(dev, k3_r5_cluster_of_exit, pdev);
1773 ret = k3_r5_cluster_rproc_init(pdev);
1775 dev_err(dev, "k3_r5_cluster_rproc_init failed, ret = %d\n",
1780 ret = devm_add_action_or_reset(dev, k3_r5_cluster_rproc_exit, pdev);
1787 static const struct k3_r5_soc_data am65_j721e_soc_data = {
1788 .tcm_is_double = false,
1789 .tcm_ecc_autoinit = false,
1790 .single_cpu_mode = false,
1791 .is_single_core = false,
1794 static const struct k3_r5_soc_data j7200_j721s2_soc_data = {
1795 .tcm_is_double = true,
1796 .tcm_ecc_autoinit = true,
1797 .single_cpu_mode = false,
1798 .is_single_core = false,
1801 static const struct k3_r5_soc_data am64_soc_data = {
1802 .tcm_is_double = true,
1803 .tcm_ecc_autoinit = true,
1804 .single_cpu_mode = true,
1805 .is_single_core = false,
1808 static const struct k3_r5_soc_data am62_soc_data = {
1809 .tcm_is_double = false,
1810 .tcm_ecc_autoinit = true,
1811 .single_cpu_mode = false,
1812 .is_single_core = true,
1815 static const struct of_device_id k3_r5_of_match[] = {
1816 { .compatible = "ti,am654-r5fss", .data = &am65_j721e_soc_data, },
1817 { .compatible = "ti,j721e-r5fss", .data = &am65_j721e_soc_data, },
1818 { .compatible = "ti,j7200-r5fss", .data = &j7200_j721s2_soc_data, },
1819 { .compatible = "ti,am64-r5fss", .data = &am64_soc_data, },
1820 { .compatible = "ti,am62-r5fss", .data = &am62_soc_data, },
1821 { .compatible = "ti,j721s2-r5fss", .data = &j7200_j721s2_soc_data, },
1824 MODULE_DEVICE_TABLE(of, k3_r5_of_match);
1826 static struct platform_driver k3_r5_rproc_driver = {
1827 .probe = k3_r5_probe,
1829 .name = "k3_r5_rproc",
1830 .of_match_table = k3_r5_of_match,
1834 module_platform_driver(k3_r5_rproc_driver);
1836 MODULE_LICENSE("GPL v2");
1837 MODULE_DESCRIPTION("TI K3 R5F remote processor driver");
1838 MODULE_AUTHOR("Suman Anna <s-anna@ti.com>");