remoteproc/k3-r5: initialize TCM memories for ECC
[rpmsg/rpmsg.git] / drivers / remoteproc / ti_k3_r5_remoteproc.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * TI K3 R5F (MCU) Remote Processor driver
4  *
5  * Copyright (C) 2017-2019 Texas Instruments Incorporated - http://www.ti.com/
6  *      Suman Anna <s-anna@ti.com>
7  */
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_device.h>
16 #include <linux/of_address.h>
17 #include <linux/of_reserved_mem.h>
18 #include <linux/platform_device.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/remoteproc.h>
21 #include <linux/omap-mailbox.h>
22 #include <linux/reset.h>
23 #include <linux/soc/ti/ti_sci_protocol.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
42 /* R5 TI-SCI Processor Control Flags */
43 #define PROC_BOOT_CTRL_FLAG_R5_CORE_HALT                0x00000001
45 /* R5 TI-SCI Processor Status Flags */
46 #define PROC_BOOT_STATUS_FLAG_R5_WFE                    0x00000001
47 #define PROC_BOOT_STATUS_FLAG_R5_WFI                    0x00000002
48 #define PROC_BOOT_STATUS_FLAG_R5_CLK_GATED              0x00000004
49 #define PROC_BOOT_STATUS_FLAG_R5_LOCKSTEP_PERMITTED     0x00000100
51 /**
52  * struct k3_r5_mem - internal memory structure
53  * @cpu_addr: MPU virtual address of the memory region
54  * @bus_addr: Bus address used to access the memory region
55  * @dev_addr: Device address from remoteproc view
56  * @size: Size of the memory region
57  */
58 struct k3_r5_mem {
59         void __iomem *cpu_addr;
60         phys_addr_t bus_addr;
61         u32 dev_addr;
62         size_t size;
63 };
65 enum cluster_mode {
66         CLUSTER_MODE_SPLIT = 0,
67         CLUSTER_MODE_LOCKSTEP,
68 };
70 /**
71  * struct k3_r5_cluster - K3 R5F Cluster structure
72  * @dev: cached device pointer
73  * @mode: Mode to configure the Cluster - Split or LockStep
74  * @cores: list of R5 cores within the cluster
75  */
76 struct k3_r5_cluster {
77         struct device *dev;
78         enum cluster_mode mode;
79         struct list_head cores;
80 };
82 /**
83  * struct k3_r5_core - K3 R5 core structure
84  * @dev: cached device pointer
85  * @rproc: rproc handle representing this core
86  * @mem: internal memory regions data
87  * @sram: on-chip SRAM memory regions data
88  * @num_mems: number of internal memory regions
89  * @num_sram: number of on-chip SRAM memory regions
90  * @reset: reset control handle
91  * @tsp: TI-SCI processor control handle
92  * @ti_sci: TI-SCI handle
93  * @ti_sci_id: TI-SCI device identifier
94  * @atcm_enable: flag to control ATCM enablement
95  * @btcm_enable: flag to control BTCM enablement
96  * @loczrama: flag to dictate which TCM is at device address 0x0
97  */
98 struct k3_r5_core {
99         struct list_head elem;
100         struct device *dev;
101         struct rproc *rproc;
102         struct k3_r5_mem *mem;
103         struct k3_r5_mem *sram;
104         int num_mems;
105         int num_sram;
106         struct reset_control *reset;
107         struct ti_sci_proc *tsp;
108         const struct ti_sci_handle *ti_sci;
109         u32 ti_sci_id;
110         u32 atcm_enable;
111         u32 btcm_enable;
112         u32 loczrama;
113 };
115 /**
116  * struct k3_r5_rproc - K3 remote processor state
117  * @dev: cached device pointer
118  * @cluster: cached pointer to parent cluster structure
119  * @mbox: mailbox channel handle
120  * @client: mailbox client to request the mailbox channel
121  * @rproc: rproc handle
122  * @core: cached pointer to r5 core structure being used
123  * @rmem: reserved memory regions data
124  * @num_rmems: number of reserved memory regions
125  */
126 struct k3_r5_rproc {
127         struct device *dev;
128         struct k3_r5_cluster *cluster;
129         struct mbox_chan *mbox;
130         struct mbox_client client;
131         struct rproc *rproc;
132         struct k3_r5_core *core;
133         struct k3_r5_mem *rmem;
134         int num_rmems;
135 };
137 /**
138  * struct k3_r5_rproc_dev_data - device data for the remote processor
139  * @device_name: device name of the remote processor
140  * @fw_name: firmware name to use
141  */
142 struct k3_r5_rproc_dev_data {
143         const char *device_name;
144         const char *fw_name;
145 };
147 /**
148  * k3_r5_rproc_mbox_callback() - inbound mailbox message handler
149  * @client: mailbox client pointer used for requesting the mailbox channel
150  * @data: mailbox payload
151  *
152  * This handler is invoked by the OMAP mailbox driver whenever a mailbox
153  * message is received. Usually, the mailbox payload simply contains
154  * the index of the virtqueue that is kicked by the remote processor,
155  * and we let remoteproc core handle it.
156  *
157  * In addition to virtqueue indices, we also have some out-of-band values
158  * that indicate different events. Those values are deliberately very
159  * large so they don't coincide with virtqueue indices.
160  */
161 static void k3_r5_rproc_mbox_callback(struct mbox_client *client, void *data)
163         struct k3_r5_rproc *kproc = container_of(client, struct k3_r5_rproc,
164                                                 client);
165         struct device *dev = kproc->rproc->dev.parent;
166         const char *name = kproc->rproc->name;
167         u32 msg = to_omap_mbox_msg(data);
169         dev_dbg(dev, "mbox msg: 0x%x\n", msg);
171         switch (msg) {
172         case RP_MBOX_CRASH:
173                 /*
174                  * remoteproc detected an exception, but error recovery is not
175                  * supported. So, just log this for now
176                  */
177                 dev_err(dev, "K3 R5F rproc %s crashed\n", name);
178                 break;
179         case RP_MBOX_ECHO_REPLY:
180                 dev_info(dev, "received echo reply from %s\n", name);
181                 break;
182         default:
183                 /* silently handle all other valid messages */
184                 if (msg >= RP_MBOX_READY && msg < RP_MBOX_END_MSG)
185                         return;
186                 if (msg > kproc->rproc->max_notifyid) {
187                         dev_dbg(dev, "dropping unknown message 0x%x", msg);
188                         return;
189                 }
190                 /* msg contains the index of the triggered vring */
191                 if (rproc_vq_interrupt(kproc->rproc, msg) == IRQ_NONE)
192                         dev_dbg(dev, "no message was found in vqid %d\n", msg);
193         }
196 /* kick a virtqueue */
197 static void k3_r5_rproc_kick(struct rproc *rproc, int vqid)
199         struct k3_r5_rproc *kproc = rproc->priv;
200         struct device *dev = rproc->dev.parent;
201         mbox_msg_t msg = (mbox_msg_t)vqid;
202         int ret;
204         /* send the index of the triggered virtqueue in the mailbox payload */
205         ret = mbox_send_message(kproc->mbox, (void *)msg);
206         if (ret < 0)
207                 dev_err(dev, "failed to send mailbox message, status = %d\n",
208                         ret);
211 static int k3_r5_split_reset(struct k3_r5_core *core)
213         int ret;
215         ret = reset_control_assert(core->reset);
216         if (ret) {
217                 dev_err(core->dev, "local-reset assert failed, ret = %d\n",
218                         ret);
219                 return ret;
220         }
222         ret = core->ti_sci->ops.dev_ops.put_device(core->ti_sci,
223                                                    core->ti_sci_id);
224         if (ret) {
225                 dev_err(core->dev, "module-reset assert failed, ret = %d\n",
226                         ret);
227                 if (reset_control_deassert(core->reset))
228                         dev_warn(core->dev, "local-reset deassert back failed\n");
229         }
231         return ret;
234 static int k3_r5_split_release(struct k3_r5_core *core)
236         int ret;
238         ret = core->ti_sci->ops.dev_ops.get_device(core->ti_sci,
239                                                    core->ti_sci_id);
240         if (ret) {
241                 dev_err(core->dev, "module-reset deassert failed, ret = %d\n",
242                         ret);
243                 return ret;
244         }
246         ret = reset_control_deassert(core->reset);
247         if (ret) {
248                 dev_err(core->dev, "local-reset deassert failed, ret = %d\n",
249                         ret);
250                 if (core->ti_sci->ops.dev_ops.put_device(core->ti_sci,
251                                                          core->ti_sci_id))
252                         dev_warn(core->dev, "module-reset assert back failed\n");
253         }
255         return ret;
258 static int k3_r5_lockstep_reset(struct k3_r5_cluster *cluster)
260         struct k3_r5_core *core;
261         int ret;
263         /* assert local reset on all applicable cores */
264         list_for_each_entry(core, &cluster->cores, elem) {
265                 ret = reset_control_assert(core->reset);
266                 if (ret) {
267                         dev_err(core->dev, "local-reset assert failed, ret = %d\n",
268                                 ret);
269                         core = list_prev_entry(core, elem);
270                         goto unroll_local_reset;
271                 }
272         }
274         /* disable PSC modules on all applicable cores */
275         list_for_each_entry(core, &cluster->cores, elem) {
276                 ret = core->ti_sci->ops.dev_ops.put_device(core->ti_sci,
277                                                            core->ti_sci_id);
278                 if (ret) {
279                         dev_err(core->dev, "module-reset assert failed, ret = %d\n",
280                                 ret);
281                         goto unroll_module_reset;
282                 }
283         }
285         return 0;
287 unroll_module_reset:
288         list_for_each_entry_continue_reverse(core, &cluster->cores, elem) {
289                 if (core->ti_sci->ops.dev_ops.put_device(core->ti_sci,
290                                                          core->ti_sci_id))
291                         dev_warn(core->dev, "module-reset assert back failed\n");
292         }
293         core = list_last_entry(&cluster->cores, struct k3_r5_core, elem);
294 unroll_local_reset:
295         list_for_each_entry_from_reverse(core, &cluster->cores, elem) {
296                 if (reset_control_deassert(core->reset))
297                         dev_warn(core->dev, "local-reset deassert back failed\n");
298         }
300         return ret;
303 static int k3_r5_lockstep_release(struct k3_r5_cluster *cluster)
305         struct k3_r5_core *core;
306         int ret;
308         /* enable PSC modules on all applicable cores */
309         list_for_each_entry_reverse(core, &cluster->cores, elem) {
310                 ret = core->ti_sci->ops.dev_ops.get_device(core->ti_sci,
311                                                            core->ti_sci_id);
312                 if (ret) {
313                         dev_err(core->dev, "module-reset deassert failed, ret = %d\n",
314                                 ret);
315                         core = list_next_entry(core, elem);
316                         goto unroll_module_reset;
317                 }
318         }
320         /* deassert local reset on all applicable cores */
321         list_for_each_entry_reverse(core, &cluster->cores, elem) {
322                 ret = reset_control_deassert(core->reset);
323                 if (ret) {
324                         dev_err(core->dev, "module-reset deassert failed, ret = %d\n",
325                                 ret);
326                         goto unroll_local_reset;
327                 }
328         }
330         return 0;
332 unroll_local_reset:
333         list_for_each_entry_continue(core, &cluster->cores, elem) {
334                 if (reset_control_assert(core->reset))
335                         dev_warn(core->dev, "local-reset assert back failed\n");
336         }
337         core = list_first_entry(&cluster->cores, struct k3_r5_core, elem);
338 unroll_module_reset:
339         list_for_each_entry_from(core, &cluster->cores, elem) {
340                 if (core->ti_sci->ops.dev_ops.put_device(core->ti_sci,
341                                                          core->ti_sci_id))
342                         dev_warn(core->dev, "module-reset assert back failed\n");
343         }
345         return ret;
348 static inline int k3_r5_core_halt(struct k3_r5_core *core)
350         return ti_sci_proc_set_control(core->tsp,
351                                        PROC_BOOT_CTRL_FLAG_R5_CORE_HALT, 0);
354 static inline int k3_r5_core_run(struct k3_r5_core *core)
356         return ti_sci_proc_set_control(core->tsp,
357                                        0, PROC_BOOT_CTRL_FLAG_R5_CORE_HALT);
360 /*
361  * The R5F cores have controls for both a reset and a halt/run. The code
362  * execution from DDR requires the initial boot-strapping code to be run
363  * from the internal TCMs. This function is used to release the resets on
364  * applicable cores to allow loading into the TCMs. The .prepare() ops is
365  * invoked by remoteproc core before any firmware loading, and is followed
366  * by the .start() ops after loading to actually let the R5 cores run.
367  */
368 static int k3_r5_rproc_prepare(struct rproc *rproc)
370         struct k3_r5_rproc *kproc = rproc->priv;
371         struct k3_r5_cluster *cluster = kproc->cluster;
372         struct k3_r5_core *core = kproc->core;
373         struct device *dev = kproc->dev;
374         int ret;
376         ret = cluster->mode ? k3_r5_lockstep_release(cluster) :
377                               k3_r5_split_release(core);
378         if (ret)
379                 dev_err(dev, "unable to enable cores for TCM loading, ret = %d\n",
380                         ret);
382         /*
383          * Zero out both TCMs unconditionally (access from v8 Arm core is not
384          * affected by ATCM & BTCM enable configuration values) so that ECC
385          * can be effective on all TCM addresses.
386          */
387         dev_dbg(dev, "zeroing out ATCM memory\n");
388         memset(core->mem[0].cpu_addr, 0x00, core->mem[0].size);
390         dev_dbg(dev, "zeroing out BTCM memory\n");
391         memset(core->mem[1].cpu_addr, 0x00, core->mem[1].size);
393         return ret;
396 /*
397  * This function implements the .unprepare() ops and performs the complimentary
398  * operations to that of the .prepare() ops. The function is used to assert the
399  * resets on all applicable cores for the rproc device (depending on LockStep
400  * or Split mode). This completes the second portion of powering down the R5F
401  * cores. The cores themselves are only halted in the .stop() ops, and the
402  * .unprepare() ops is invoked by the remoteproc core after the remoteproc is
403  * stopped.
404  */
405 static int k3_r5_rproc_unprepare(struct rproc *rproc)
407         struct k3_r5_rproc *kproc = rproc->priv;
408         struct k3_r5_cluster *cluster = kproc->cluster;
409         struct k3_r5_core *core = kproc->core;
410         struct device *dev = kproc->dev;
411         int ret;
413         ret = cluster->mode ? k3_r5_lockstep_reset(cluster) :
414                               k3_r5_split_reset(core);
415         if (ret)
416                 dev_err(dev, "unable to disable cores, ret = %d\n", ret);
418         return ret;
421 /*
422  * The R5F start sequence includes two different operations
423  * 1. Configure the boot vector for R5F core(s)
424  * 2. Unhalt/Run the R5F core(s)
425  *
426  * The sequence is different between LockStep and Split modes. The LockStep
427  * mode requires the boot vector to be configured only for Core0, and then
428  * unhalt both the cores to start the execution - Core1 needs to be unhalted
429  * first followed by Core0. The Split-mode requires that Core0 to be maintained
430  * always in a higher power state that Core1 (implying Core1 needs to be started
431  * always only after Core0 is started).
432  */
433 static int k3_r5_rproc_start(struct rproc *rproc)
435         struct k3_r5_rproc *kproc = rproc->priv;
436         struct k3_r5_cluster *cluster = kproc->cluster;
437         struct mbox_client *client = &kproc->client;
438         struct device *dev = kproc->dev;
439         struct k3_r5_core *core;
440         u32 boot_addr;
441         int ret;
443         client->dev = dev;
444         client->tx_done = NULL;
445         client->rx_callback = k3_r5_rproc_mbox_callback;
446         client->tx_block = false;
447         client->knows_txdone = false;
449         kproc->mbox = mbox_request_channel(client, 0);
450         if (IS_ERR(kproc->mbox)) {
451                 ret = -EBUSY;
452                 dev_err(dev, "mbox_request_channel failed: %ld\n",
453                         PTR_ERR(kproc->mbox));
454                 return ret;
455         }
457         /*
458          * Ping the remote processor, this is only for sanity-sake for now;
459          * there is no functional effect whatsoever.
460          *
461          * Note that the reply will _not_ arrive immediately: this message
462          * will wait in the mailbox fifo until the remote processor is booted.
463          */
464         ret = mbox_send_message(kproc->mbox, (void *)RP_MBOX_ECHO_REQUEST);
465         if (ret < 0) {
466                 dev_err(dev, "mbox_send_message failed: %d\n", ret);
467                 goto put_mbox;
468         }
470         boot_addr = rproc->bootaddr;
471         /* TODO: add boot_addr sanity checking */
472         dev_err(dev, "booting R5F core using boot addr = 0x%x\n", boot_addr);
474         /* boot vector need not be programmed for Core1 in LockStep mode */
475         core = kproc->core;
476         ret = ti_sci_proc_set_config(core->tsp, boot_addr, 0, 0);
477         if (ret)
478                 goto put_mbox;
480         /* unhalt/run all applicable cores */
481         if (cluster->mode) {
482                 list_for_each_entry_reverse(core, &cluster->cores, elem) {
483                         ret = k3_r5_core_run(core);
484                         if (ret)
485                                 goto unroll_core_run;
486                 }
487         } else {
488                 ret = k3_r5_core_run(core);
489                 if (ret)
490                         goto put_mbox;
491         }
493         return 0;
495 unroll_core_run:
496         list_for_each_entry_continue(core, &cluster->cores, elem) {
497                 if (k3_r5_core_halt(core))
498                         dev_warn(core->dev, "core halt back failed\n");
499         }
500 put_mbox:
501         mbox_free_channel(kproc->mbox);
502         return ret;
505 /*
506  * The R5F stop function includes the following operations
507  * 1. Halt R5F core(s)
508  *
509  * The sequence is different between LockStep and Split modes, and the order
510  * of cores the operations are performed are also in general reverse to that
511  * of the start function. The LockStep mode requires each operation to be
512  * performed first on Core0 followed by Core1. The Split-mode requires that
513  * Core0 to be maintained always in a higher power state that Core1 (implying
514  * Core1 needs to be stopped first before Core0).
515  *
516  * Note that the R5F halt operation in general is not effective when the R5F
517  * core is running, but is needed to make sure the core won't run after
518  * deasserting the reset the subsequent time. The asserting of reset can
519  * be done here, but is preferred to be done in the .unprepare() ops - this
520  * maintains the symmetric behavior between the .start(), .stop(), .prepare()
521  * and .unprepare() ops, and also balances them well between sysfs 'state'
522  * flow and device bind/unbind or module removal.
523  */
524 static int k3_r5_rproc_stop(struct rproc *rproc)
526         struct k3_r5_rproc *kproc = rproc->priv;
527         struct k3_r5_cluster *cluster = kproc->cluster;
528         struct k3_r5_core *core = kproc->core;
529         int ret;
531         /* halt all applicable cores */
532         if (cluster->mode) {
533                 list_for_each_entry(core, &cluster->cores, elem) {
534                         ret = k3_r5_core_halt(core);
535                         if (ret) {
536                                 core = list_prev_entry(core, elem);
537                                 goto unroll_core_halt;
538                         }
539                 }
540         } else {
541                 ret = k3_r5_core_halt(core);
542                 if (ret)
543                         goto out;
544         }
546         mbox_free_channel(kproc->mbox);
548         return 0;
550 unroll_core_halt:
551         list_for_each_entry_from_reverse(core, &cluster->cores, elem) {
552                 if (k3_r5_core_run(core))
553                         dev_warn(core->dev, "core run back failed\n");
554         }
555 out:
556         return ret;
559 /*
560  * Internal Memory translation helper
561  *
562  * Custom function implementing the rproc .da_to_va ops to provide address
563  * translation (device address to kernel virtual address) for internal RAMs
564  * present in a DSP or IPU device). The translated addresses can be used
565  * either by the remoteproc core for loading, or by any rpmsg bus drivers.
566  */
567 static void *k3_r5_rproc_da_to_va(struct rproc *rproc, u64 da, int len,
568                                   u32 flags)
570         struct k3_r5_rproc *kproc = rproc->priv;
571         struct k3_r5_core *core = kproc->core;
572         void __iomem *va = NULL;
573         phys_addr_t bus_addr;
574         u32 dev_addr, offset;
575         size_t size;
576         int i;
578         if (len <= 0)
579                 return NULL;
581         /* handle both R5 and SoC views of ATCM and BTCM */
582         for (i = 0; i < core->num_mems; i++) {
583                 bus_addr = core->mem[i].bus_addr;
584                 dev_addr = core->mem[i].dev_addr;
585                 size = core->mem[i].size;
587                 /* handle R5-view addresses of TCMs */
588                 if (da >= dev_addr && ((da + len) <= (dev_addr + size))) {
589                         offset = da - dev_addr;
590                         va = core->mem[i].cpu_addr + offset;
591                         return (__force void *)va;
592                 }
594                 /* handle SoC-view addresses of TCMs */
595                 if (da >= bus_addr && ((da + len) <= (bus_addr + size))) {
596                         offset = da - bus_addr;
597                         va = core->mem[i].cpu_addr + offset;
598                         return (__force void *)va;
599                 }
600         }
602         /* handle any SRAM regions using SoC-view addresses */
603         for (i = 0; i < core->num_sram; i++) {
604                 dev_addr = core->sram[i].dev_addr;
605                 size = core->sram[i].size;
607                 if (da >= dev_addr && ((da + len) <= (dev_addr + size))) {
608                         offset = da - dev_addr;
609                         va = core->sram[i].cpu_addr + offset;
610                         return (__force void *)va;
611                 }
612         }
614         /* handle static DDR reserved memory regions */
615         for (i = 0; i < kproc->num_rmems; i++) {
616                 dev_addr = kproc->rmem[i].dev_addr;
617                 size = kproc->rmem[i].size;
619                 if (da >= dev_addr && ((da + len) <= (dev_addr + size))) {
620                         offset = da - dev_addr;
621                         va = kproc->rmem[i].cpu_addr + offset;
622                         return (__force void *)va;
623                 }
624         }
626         return NULL;
629 static const struct rproc_ops k3_r5_rproc_ops = {
630         .prepare        = k3_r5_rproc_prepare,
631         .unprepare      = k3_r5_rproc_unprepare,
632         .start          = k3_r5_rproc_start,
633         .stop           = k3_r5_rproc_stop,
634         .kick           = k3_r5_rproc_kick,
635         .da_to_va       = k3_r5_rproc_da_to_va,
636 };
638 static const char *k3_r5_rproc_get_firmware(struct device *dev)
640         const struct k3_r5_rproc_dev_data *data =
641                                 of_device_get_match_data(dev->parent);
643         if (!data) {
644                 dev_err(dev, "data is NULL, %s\n", dev_name(dev));
645                 return ERR_PTR(-ENODEV);
646         }
648         for (; data && data->device_name; data++) {
649                 if (!strcmp(dev_name(dev), data->device_name))
650                         return data->fw_name;
651         }
653         return ERR_PTR(-ENODEV);
656 static int k3_r5_rproc_configure(struct k3_r5_rproc *kproc)
658         struct k3_r5_cluster *cluster = kproc->cluster;
659         struct device *dev = kproc->dev;
660         struct k3_r5_core *core0, *core, *temp;
661         u32 ctrl = 0, cfg = 0, stat = 0;
662         u32 set_cfg = 0, clr_cfg = 0;
663         u64 boot_vec = 0;
664         int ret;
666         core0 = list_first_entry(&cluster->cores, struct k3_r5_core, elem);
667         core = cluster->mode ? core0 : kproc->core;
669         ret = ti_sci_proc_get_status(core->tsp, &boot_vec, &cfg, &ctrl,
670                                      &stat);
671         if (ret < 0)
672                 return ret;
674         dev_dbg(dev, "boot_vector = 0x%llx, cfg = 0x%x ctrl = 0x%x stat = 0x%x\n",
675                 boot_vec, cfg, ctrl, stat);
677         if (!(stat & PROC_BOOT_STATUS_FLAG_R5_LOCKSTEP_PERMITTED) &&
678             cluster->mode) {
679                 dev_err(cluster->dev, "lockstep mode not permitted, force configuring for split-mode\n");
680                 cluster->mode = 0;
681         }
683         /* always enable ARM mode and set boot vector to 0 */
684         boot_vec = 0x0;
685         if (core == core0) {
686                 clr_cfg = PROC_BOOT_CFG_FLAG_R5_TEINIT;
687                 clr_cfg |= PROC_BOOT_CFG_FLAG_R5_LOCKSTEP;
688         }
690         if (core->atcm_enable)
691                 set_cfg |= PROC_BOOT_CFG_FLAG_R5_ATCM_EN;
692         else
693                 clr_cfg |= PROC_BOOT_CFG_FLAG_R5_ATCM_EN;
695         if (core->btcm_enable)
696                 set_cfg |= PROC_BOOT_CFG_FLAG_R5_BTCM_EN;
697         else
698                 clr_cfg |= PROC_BOOT_CFG_FLAG_R5_BTCM_EN;
700         if (core->loczrama)
701                 set_cfg |= PROC_BOOT_CFG_FLAG_R5_TCM_RSTBASE;
702         else
703                 clr_cfg |= PROC_BOOT_CFG_FLAG_R5_TCM_RSTBASE;
705         if (cluster->mode) {
706                 /*
707                  * work around system firmware limitations to make sure both
708                  * cores are programmed symmetrically in LockStep. LockStep
709                  * and TEINIT config is only allowed with Core0.
710                  */
711                 list_for_each_entry(temp, &cluster->cores, elem) {
712                         ret = k3_r5_core_halt(core);
713                         if (ret)
714                                 goto out;
716                         if (temp != core) {
717                                 clr_cfg &= ~PROC_BOOT_CFG_FLAG_R5_LOCKSTEP;
718                                 clr_cfg &= ~PROC_BOOT_CFG_FLAG_R5_TEINIT;
719                         }
720                         ret = ti_sci_proc_set_config(temp->tsp, boot_vec,
721                                                      set_cfg, clr_cfg);
722                         if (ret)
723                                 goto out;
724                 }
726                 set_cfg = PROC_BOOT_CFG_FLAG_R5_LOCKSTEP;
727                 clr_cfg = 0;
728                 ret = ti_sci_proc_set_config(core->tsp, boot_vec,
729                                              set_cfg, clr_cfg);
730         } else {
731                 ret = k3_r5_core_halt(core);
732                 if (ret)
733                         goto out;
735                 ret = ti_sci_proc_set_config(core->tsp, boot_vec,
736                                              set_cfg, clr_cfg);
737         }
739 out:
740         return ret;
743 static int k3_r5_reserved_mem_init(struct k3_r5_rproc *kproc)
745         struct device *dev = kproc->dev;
746         struct device_node *np = dev->of_node;
747         struct device_node *rmem_np;
748         struct reserved_mem *rmem;
749         int num_rmems;
750         int ret, i;
752         num_rmems = of_property_count_elems_of_size(np, "memory-region",
753                                                     sizeof(phandle));
754         if (num_rmems <= 0) {
755                 dev_err(dev, "device does not have reserved memory regions, ret = %d\n",
756                         num_rmems);
757                 return -EINVAL;
758         }
759         if (num_rmems < 2) {
760                 dev_err(dev, "device needs atleast two memory regions to be defined, num = %d\n",
761                         num_rmems);
762                 return -EINVAL;
763         }
765         /* use reserved memory region 0 for vring DMA allocations */
766         ret = of_reserved_mem_device_init_by_idx(dev, np, 0);
767         if (ret) {
768                 dev_err(dev, "device cannot initialize DMA pool, ret = %d\n",
769                         ret);
770                 return ret;
771         }
773         num_rmems--;
774         kproc->rmem = kcalloc(num_rmems, sizeof(*kproc->rmem), GFP_KERNEL);
775         if (!kproc->rmem) {
776                 ret = -ENOMEM;
777                 goto release_rmem;
778         }
780         /* use remaining reserved memory regions for static carveouts */
781         for (i = 0; i < num_rmems; i++) {
782                 rmem_np = of_parse_phandle(np, "memory-region", i + 1);
783                 if (!rmem_np) {
784                         ret = -EINVAL;
785                         goto unmap_rmem;
786                 }
788                 rmem = of_reserved_mem_lookup(rmem_np);
789                 if (!rmem) {
790                         of_node_put(rmem_np);
791                         ret = -EINVAL;
792                         goto unmap_rmem;
793                 }
794                 of_node_put(rmem_np);
796                 kproc->rmem[i].bus_addr = rmem->base;
797                 /* 64-bit address regions currently not supported */
798                 kproc->rmem[i].dev_addr = (u32)rmem->base;
799                 kproc->rmem[i].size = rmem->size;
800                 kproc->rmem[i].cpu_addr = ioremap_wc(rmem->base, rmem->size);
801                 if (!kproc->rmem[i].cpu_addr) {
802                         dev_err(dev, "failed to map reserved memory#%d at %pa of size %pa\n",
803                                 i + 1, &rmem->base, &rmem->size);
804                         ret = -ENOMEM;
805                         goto unmap_rmem;
806                 }
808                 dev_dbg(dev, "reserved memory%d: bus addr %pa size 0x%zx va %pK da 0x%x\n",
809                         i + 1, &kproc->rmem[i].bus_addr,
810                         kproc->rmem[i].size, kproc->rmem[i].cpu_addr,
811                         kproc->rmem[i].dev_addr);
812         }
813         kproc->num_rmems = num_rmems;
815         return 0;
817 unmap_rmem:
818         for (i--; i >= 0; i--) {
819                 if (kproc->rmem[i].cpu_addr)
820                         iounmap(kproc->rmem[i].cpu_addr);
821         }
822         kfree(kproc->rmem);
823 release_rmem:
824         of_reserved_mem_device_release(dev);
825         return ret;
828 static void k3_r5_reserved_mem_exit(struct k3_r5_rproc *kproc)
830         int i;
832         for (i = 0; i < kproc->num_rmems; i++)
833                 iounmap(kproc->rmem[i].cpu_addr);
834         kfree(kproc->rmem);
836         of_reserved_mem_device_release(kproc->dev);
839 static int k3_r5_cluster_rproc_init(struct platform_device *pdev)
841         struct k3_r5_cluster *cluster = platform_get_drvdata(pdev);
842         struct device *dev = &pdev->dev;
843         struct k3_r5_rproc *kproc;
844         struct k3_r5_core *core, *core1;
845         struct device *cdev;
846         const char *fw_name;
847         struct rproc *rproc;
848         int ret;
850         core1 = list_last_entry(&cluster->cores, struct k3_r5_core, elem);
851         list_for_each_entry(core, &cluster->cores, elem) {
852                 cdev = core->dev;
853                 fw_name = k3_r5_rproc_get_firmware(cdev);
854                 if (IS_ERR(fw_name)) {
855                         ret = PTR_ERR(fw_name);
856                         goto out;
857                 }
859                 rproc = rproc_alloc(cdev, dev_name(cdev), &k3_r5_rproc_ops,
860                                     fw_name, sizeof(*kproc));
861                 if (!rproc) {
862                         ret = -ENOMEM;
863                         goto out;
864                 }
866                 /* K3 R5s have a Region Address Translator (RAT) but no MMU */
867                 rproc->has_iommu = false;
868                 /* error recovery is not supported at present */
869                 rproc->recovery_disabled = true;
871                 kproc = rproc->priv;
872                 kproc->cluster = cluster;
873                 kproc->core = core;
874                 kproc->dev = cdev;
875                 kproc->rproc = rproc;
876                 core->rproc = rproc;
878                 ret = k3_r5_rproc_configure(kproc);
879                 if (ret) {
880                         dev_err(dev, "initial configure failed, ret = %d\n",
881                                 ret);
882                         goto err_config;
883                 }
885                 ret = k3_r5_reserved_mem_init(kproc);
886                 if (ret) {
887                         dev_err(dev, "reserved memory init failed, ret = %d\n",
888                                 ret);
889                         goto err_config;
890                 }
892                 ret = rproc_add(rproc);
893                 if (ret) {
894                         dev_err(dev, "rproc_add failed, ret = %d\n", ret);
895                         goto err_add;
896                 }
898                 /* create only one rproc in lockstep mode */
899                 if (cluster->mode)
900                         break;
901         }
903         return 0;
905 err_split:
906         rproc_del(rproc);
907 err_add:
908         k3_r5_reserved_mem_exit(kproc);
909 err_config:
910         rproc_free(rproc);
911         core->rproc = NULL;
912 out:
913         /* undo core0 upon any failures on core1 in split-mode */
914         if (!cluster->mode && core == core1) {
915                 core = list_prev_entry(core, elem);
916                 rproc = core->rproc;
917                 kproc = rproc->priv;
918                 goto err_split;
919         }
920         return ret;
923 static int k3_r5_cluster_rproc_exit(struct platform_device *pdev)
925         struct k3_r5_cluster *cluster = platform_get_drvdata(pdev);
926         struct k3_r5_rproc *kproc;
927         struct k3_r5_core *core;
928         struct rproc *rproc;
930         /*
931          * lockstep mode has only one rproc associated with first core, whereas
932          * split-mode has two rprocs associated with each core, and requires
933          * that core1 be powered down first
934          */
935         core = cluster->mode ?
936                 list_first_entry(&cluster->cores, struct k3_r5_core, elem) :
937                 list_last_entry(&cluster->cores, struct k3_r5_core, elem);
939         list_for_each_entry_from_reverse(core, &cluster->cores, elem) {
940                 rproc = core->rproc;
941                 kproc = rproc->priv;
943                 rproc_del(rproc);
945                 k3_r5_reserved_mem_exit(kproc);
947                 rproc_free(rproc);
948                 core->rproc = NULL;
949         }
951         return 0;
954 static int k3_r5_core_of_get_internal_memories(struct platform_device *pdev,
955                                                struct k3_r5_core *core)
957         static const char * const mem_names[] = {"atcm", "btcm"};
958         struct device *dev = &pdev->dev;
959         struct resource *res;
960         int num_mems;
961         int i, ret;
963         num_mems = ARRAY_SIZE(mem_names);
964         core->mem = devm_kcalloc(dev, num_mems, sizeof(*core->mem), GFP_KERNEL);
965         if (!core->mem)
966                 return -ENOMEM;
968         for (i = 0; i < num_mems; i++) {
969                 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
970                                                    mem_names[i]);
971                 if (!res) {
972                         dev_err(dev, "found no memory resource for %s\n",
973                                 mem_names[i]);
974                         ret = -EINVAL;
975                         goto fail;
976                 }
977                 if (!devm_request_mem_region(dev, res->start,
978                                              resource_size(res),
979                                              dev_name(dev))) {
980                         dev_err(dev, "could not request %s region for resource\n",
981                                 mem_names[i]);
982                         ret = -EBUSY;
983                         goto fail;
984                 }
986                 core->mem[i].cpu_addr = devm_ioremap_wc(dev, res->start,
987                                                         resource_size(res));
988                 if (IS_ERR(core->mem[i].cpu_addr)) {
989                         dev_err(dev, "failed to map %s memory\n", mem_names[i]);
990                         ret = PTR_ERR(core->mem[i].cpu_addr);
991                         devm_release_mem_region(dev, res->start,
992                                                 resource_size(res));
993                         goto fail;
994                 }
995                 core->mem[i].bus_addr = res->start;
997                 /*
998                  * TODO:
999                  * The R5F cores can place ATCM & BTCM anywhere in its address
1000                  * based on the corresponding Region Registers in the System
1001                  * Control coprocessor. For now, place ATCM and BTCM at
1002                  * addresses 0 and 0x41010000 (same as the bus address on AM65x
1003                  * SoCs) based on loczrama setting
1004                  */
1005                 if (!strcmp(mem_names[i], "atcm")) {
1006                         core->mem[i].dev_addr = core->loczrama ?
1007                                                         0 : K3_R5_TCM_DEV_ADDR;
1008                 } else {
1009                         core->mem[i].dev_addr = core->loczrama ?
1010                                                         K3_R5_TCM_DEV_ADDR : 0;
1011                 }
1012                 core->mem[i].size = resource_size(res);
1014                 dev_dbg(dev, "memory %8s: bus addr %pa size 0x%zx va %p da 0x%x\n",
1015                         mem_names[i], &core->mem[i].bus_addr,
1016                         core->mem[i].size, core->mem[i].cpu_addr,
1017                         core->mem[i].dev_addr);
1018         }
1019         core->num_mems = num_mems;
1021         return 0;
1023 fail:
1024         for (i--; i >= 0; i--) {
1025                 devm_release_mem_region(dev, core->mem[i].bus_addr,
1026                                         core->mem[i].size);
1027                 devm_iounmap(dev, core->mem[i].cpu_addr);
1028         }
1029         if (core->mem)
1030                 devm_kfree(dev, core->mem);
1031         return ret;
1034 static int k3_r5_core_of_get_sram_memories(struct platform_device *pdev,
1035                                            struct k3_r5_core *core)
1037         struct device_node *np = pdev->dev.of_node;
1038         struct device *dev = &pdev->dev;
1039         struct device_node *sram_np;
1040         struct resource res;
1041         int num_sram;
1042         int i, ret;
1044         num_sram = of_property_count_elems_of_size(np, "sram", sizeof(phandle));
1045         if (num_sram <= 0) {
1046                 dev_dbg(dev, "device does not use reserved on-chip memories, num_sram = %d\n",
1047                         num_sram);
1048                 return 0;
1049         }
1051         core->sram = kcalloc(num_sram, sizeof(*core->sram), GFP_KERNEL);
1052         if (!core->sram)
1053                 return -ENOMEM;
1055         for (i = 0; i < num_sram; i++) {
1056                 sram_np = of_parse_phandle(np, "sram", i);
1057                 if (!sram_np) {
1058                         ret = -EINVAL;
1059                         goto fail;
1060                 }
1062                 if (!of_device_is_available(sram_np)) {
1063                         of_node_put(sram_np);
1064                         ret = -EINVAL;
1065                         goto fail;
1066                 }
1068                 ret = of_address_to_resource(sram_np, 0, &res);
1069                 of_node_put(sram_np);
1070                 if (ret) {
1071                         ret = -EINVAL;
1072                         goto fail;
1073                 }
1074                 core->sram[i].bus_addr = res.start;
1075                 core->sram[i].dev_addr = res.start;
1076                 core->sram[i].size = resource_size(&res);
1077                 core->sram[i].cpu_addr = ioremap_wc(res.start,
1078                                                     resource_size(&res));
1079                 if (!core->sram[i].cpu_addr) {
1080                         dev_err(dev, "failed to parse and map sram%d memory at %pad\n",
1081                                 i, &res.start);
1082                         ret = -ENOMEM;
1083                         goto fail;
1084                 }
1086                 dev_dbg(dev, "memory    sram%d: bus addr %pa size 0x%zx va %pK da 0x%x\n",
1087                         i, &core->sram[i].bus_addr,
1088                         core->sram[i].size, core->sram[i].cpu_addr,
1089                         core->sram[i].dev_addr);
1090         }
1091         core->num_sram = num_sram;
1093         return 0;
1095 fail:
1096         for (i--; i >= 0; i--) {
1097                 if (core->sram[i].cpu_addr)
1098                         iounmap(core->sram[i].cpu_addr);
1099         }
1100         kfree(core->sram);
1102         return ret;
1105 static
1106 struct ti_sci_proc *k3_r5_core_of_get_tsp(struct device *dev,
1107                                           const struct ti_sci_handle *sci)
1109         struct ti_sci_proc *tsp;
1110         u32 temp[2];
1111         int ret;
1113         ret = of_property_read_u32_array(dev->of_node, "ti,sci-proc-ids",
1114                                          temp, 2);
1115         if (ret < 0)
1116                 return ERR_PTR(ret);
1118         tsp = kzalloc(sizeof(*tsp), GFP_KERNEL);
1119         if (!tsp)
1120                 return ERR_PTR(-ENOMEM);
1122         tsp->dev = dev;
1123         tsp->sci = sci;
1124         tsp->ops = &sci->ops.proc_ops;
1125         tsp->proc_id = temp[0];
1126         tsp->host_id = temp[1];
1128         return tsp;
1131 static int k3_r5_core_of_init(struct platform_device *pdev)
1133         struct device *dev = &pdev->dev;
1134         struct device_node *np = dev->of_node;
1135         struct k3_r5_core *core;
1136         int ret, ret1, i;
1138         core = devm_kzalloc(dev, sizeof(*core), GFP_KERNEL);
1139         if (!core)
1140                 return -ENOMEM;
1142         core->dev = dev;
1143         core->atcm_enable = 0;
1144         core->btcm_enable = 1;
1145         core->loczrama = 1;
1147         ret = of_property_read_u32(np, "atcm-enable", &core->atcm_enable);
1148         if (ret < 0 && ret != -EINVAL) {
1149                 dev_err(dev, "invalid format for atcm-enable, ret = %d\n", ret);
1150                 goto err_of;
1151         }
1153         ret = of_property_read_u32(np, "btcm-enable", &core->btcm_enable);
1154         if (ret < 0 && ret != -EINVAL) {
1155                 dev_err(dev, "invalid format for btcm-enable, ret = %d\n", ret);
1156                 goto err_of;
1157         }
1159         ret = of_property_read_u32(np, "loczrama", &core->loczrama);
1160         if (ret < 0 && ret != -EINVAL) {
1161                 dev_err(dev, "invalid format for loczrama, ret = %d\n", ret);
1162                 goto err_of;
1163         }
1165         core->ti_sci = ti_sci_get_by_phandle(np, "ti,sci");
1166         if (IS_ERR(core->ti_sci)) {
1167                 ret = PTR_ERR(core->ti_sci);
1168                 if (ret != -EPROBE_DEFER) {
1169                         dev_err(dev, "failed to get ti-sci handle, ret = %d\n",
1170                                 ret);
1171                 }
1172                 core->ti_sci = NULL;
1173                 goto err_of;
1174         }
1176         ret = of_property_read_u32(np, "ti,sci-dev-id", &core->ti_sci_id);
1177         if (ret) {
1178                 dev_err(dev, "missing 'ti,sci-dev-id' property\n");
1179                 goto err_sci_id;
1180         }
1182         core->reset = reset_control_get_exclusive(dev, NULL);
1183         if (IS_ERR(core->reset)) {
1184                 ret = PTR_ERR(core->reset);
1185                 if (ret != -EPROBE_DEFER) {
1186                         dev_err(dev, "failed to get reset handle, ret = %d\n",
1187                                 ret);
1188                 }
1189                 goto err_sci_id;
1190         }
1192         core->tsp = k3_r5_core_of_get_tsp(dev, core->ti_sci);
1193         if (IS_ERR(core->tsp)) {
1194                 dev_err(dev, "failed to construct ti-sci proc control, ret = %d\n",
1195                         ret);
1196                 ret = PTR_ERR(core->tsp);
1197                 goto err_sci_proc;
1198         }
1200         ret = ti_sci_proc_request(core->tsp);
1201         if (ret < 0) {
1202                 dev_err(dev, "ti_sci_proc_request failed, ret = %d\n", ret);
1203                 goto err_proc;
1204         }
1206         ret = k3_r5_core_of_get_internal_memories(pdev, core);
1207         if (ret) {
1208                 dev_err(dev, "failed to get internal memories, ret = %d\n",
1209                         ret);
1210                 goto err_intmem;
1211         }
1213         ret = k3_r5_core_of_get_sram_memories(pdev, core);
1214         if (ret) {
1215                 dev_err(dev, "failed to get sram memories, ret = %d\n", ret);
1216                 goto err_sram;
1217         }
1219         platform_set_drvdata(pdev, core);
1221         return 0;
1223 err_sram:
1224         for (i = 0; i < core->num_mems; i++) {
1225                 devm_release_mem_region(dev, core->mem[i].bus_addr,
1226                                         core->mem[i].size);
1227                 devm_iounmap(dev, core->mem[i].cpu_addr);
1228         }
1229         devm_kfree(dev, core->mem);
1230 err_intmem:
1231         ret1 = ti_sci_proc_release(core->tsp);
1232         if (ret1)
1233                 dev_err(dev, "failed to release proc, ret1 = %d\n", ret1);
1234 err_proc:
1235         kfree(core->tsp);
1236 err_sci_proc:
1237         reset_control_put(core->reset);
1238 err_sci_id:
1239         ret1 = ti_sci_put_handle(core->ti_sci);
1240         if (ret1)
1241                 dev_err(dev, "failed to put ti_sci handle, ret = %d\n", ret1);
1242 err_of:
1243         devm_kfree(dev, core);
1244         return ret;
1247 /*
1248  * free the resources explicitly since driver model is not being used
1249  * for the child R5F devices
1250  */
1251 static int k3_r5_core_of_exit(struct platform_device *pdev)
1253         struct k3_r5_core *core = platform_get_drvdata(pdev);
1254         struct device *dev = &pdev->dev;
1255         int i, ret;
1257         for (i = 0; i < core->num_sram; i++)
1258                 iounmap(core->sram[i].cpu_addr);
1259         kfree(core->sram);
1261         for (i = 0; i < core->num_mems; i++) {
1262                 devm_release_mem_region(dev, core->mem[i].bus_addr,
1263                                         core->mem[i].size);
1264                 devm_iounmap(dev, core->mem[i].cpu_addr);
1265         }
1266         if (core->mem)
1267                 devm_kfree(dev, core->mem);
1269         ret = ti_sci_proc_release(core->tsp);
1270         if (ret)
1271                 dev_err(dev, "failed to release proc, ret = %d\n", ret);
1273         kfree(core->tsp);
1274         reset_control_put(core->reset);
1276         ret = ti_sci_put_handle(core->ti_sci);
1277         if (ret)
1278                 dev_err(dev, "failed to put ti_sci handle, ret = %d\n", ret);
1280         platform_set_drvdata(pdev, NULL);
1281         devm_kfree(dev, core);
1283         return ret;
1286 static int k3_r5_cluster_of_init(struct platform_device *pdev)
1288         struct k3_r5_cluster *cluster = platform_get_drvdata(pdev);
1289         struct device *dev = &pdev->dev;
1290         struct device_node *np = dev->of_node;
1291         struct platform_device *cpdev;
1292         struct device_node *child;
1293         struct k3_r5_core *core, *temp;
1294         int ret;
1296         for_each_available_child_of_node(np, child) {
1297                 cpdev = of_find_device_by_node(child);
1298                 if (!cpdev) {
1299                         ret = -ENODEV;
1300                         dev_err(dev, "could not get R5 core platform device\n");
1301                         goto fail;
1302                 }
1304                 ret = k3_r5_core_of_init(cpdev);
1305                 if (ret) {
1306                         dev_err(dev, "k3_r5_core_of_init failed, ret = %d\n",
1307                                 ret);
1308                         put_device(&cpdev->dev);
1309                         goto fail;
1310                 }
1312                 core = platform_get_drvdata(cpdev);
1313                 put_device(&cpdev->dev);
1314                 list_add_tail(&core->elem, &cluster->cores);
1315         }
1317         return 0;
1319 fail:
1320         list_for_each_entry_safe_reverse(core, temp, &cluster->cores, elem) {
1321                 list_del(&core->elem);
1322                 cpdev = to_platform_device(core->dev);
1323                 if (k3_r5_core_of_exit(cpdev))
1324                         dev_err(dev, "k3_r5_core_of_exit cleanup failed\n");
1325         }
1326         return ret;
1329 static int k3_r5_cluster_of_exit(struct platform_device *pdev)
1331         struct k3_r5_cluster *cluster = platform_get_drvdata(pdev);
1332         struct device *dev = &pdev->dev;
1333         struct platform_device *cpdev;
1334         struct k3_r5_core *core, *temp;
1335         int ret;
1337         list_for_each_entry_safe_reverse(core, temp, &cluster->cores, elem) {
1338                 list_del(&core->elem);
1339                 cpdev = to_platform_device(core->dev);
1340                 ret = k3_r5_core_of_exit(cpdev);
1341                 if (ret) {
1342                         dev_err(dev, "k3_r5_core_of_exit failed, ret = %d\n",
1343                                 ret);
1344                         break;
1345                 }
1346         }
1348         return ret;
1351 static int k3_r5_probe(struct platform_device *pdev)
1353         struct device *dev = &pdev->dev;
1354         struct device_node *np = dev->of_node;
1355         struct k3_r5_cluster *cluster;
1356         int ret, ret1;
1357         int num_cores;
1359         cluster = devm_kzalloc(dev, sizeof(*cluster), GFP_KERNEL);
1360         if (!cluster)
1361                 return -ENOMEM;
1363         cluster->dev = dev;
1364         cluster->mode = CLUSTER_MODE_LOCKSTEP;
1365         INIT_LIST_HEAD(&cluster->cores);
1367         ret = of_property_read_u32(np, "lockstep-mode", &cluster->mode);
1368         if (ret < 0 && ret != -EINVAL) {
1369                 dev_err(dev, "invalid format for lockstep-mode, ret = %d\n",
1370                         ret);
1371                 return ret;
1372         }
1374         num_cores = of_get_available_child_count(np);
1375         if (num_cores != 2) {
1376                 dev_err(dev, "MCU cluster requires both R5F cores to be enabled, num_cores = %d\n",
1377                         num_cores);
1378                 return -ENODEV;
1379         }
1381         platform_set_drvdata(pdev, cluster);
1383         dev_info(dev, "creating child devices for R5F cores\n");
1384         ret = of_platform_populate(np, NULL, NULL, dev);
1385         if (ret) {
1386                 dev_err(dev, "of_platform_populate failed, ret = %d\n", ret);
1387                 return ret;
1388         }
1390         ret = k3_r5_cluster_of_init(pdev);
1391         if (ret) {
1392                 dev_err(dev, "k3_r5_cluster_of_init failed, ret = %d\n", ret);
1393                 goto fail_of;
1394         }
1396         ret = k3_r5_cluster_rproc_init(pdev);
1397         if (ret) {
1398                 dev_err(dev, "k3_r5_cluster_rproc_init failed, ret = %d\n",
1399                         ret);
1400                 goto fail_rproc;
1401         }
1403         return 0;
1405 fail_rproc:
1406         ret1 = k3_r5_cluster_of_exit(pdev);
1407         if (ret1)
1408                 dev_err(dev, "k3_r5_cluster_of_exit failed, ret = %d\n", ret1);
1409 fail_of:
1410         of_platform_depopulate(dev);
1411         return ret;
1414 static int k3_r5_remove(struct platform_device *pdev)
1416         struct device *dev = &pdev->dev;
1417         int ret;
1419         ret = k3_r5_cluster_rproc_exit(pdev);
1420         if (ret) {
1421                 dev_err(dev, "k3_r5_cluster_rproc_exit failed, ret = %d\n",
1422                         ret);
1423                 goto fail;
1424         }
1426         ret = k3_r5_cluster_of_exit(pdev);
1427         if (ret) {
1428                 dev_err(dev, "k3_r5_cluster_of_exit failed, ret = %d\n", ret);
1429                 goto fail;
1430         }
1432         dev_info(dev, "removing child devices for R5F cores\n");
1433         of_platform_depopulate(dev);
1435 fail:
1436         return ret;
1439 static const struct k3_r5_rproc_dev_data am65x_r5f_dev_data[] = {
1440         {
1441                 .device_name    = "41000000.r5f",
1442                 .fw_name        = "am65x-mcu-r5f0_0-fw",
1443         },
1444         {
1445                 .device_name    = "41400000.r5f",
1446                 .fw_name        = "am65x-mcu-r5f0_1-fw",
1447         },
1448         {
1449                 /* sentinel */
1450         },
1451 };
1453 static const struct of_device_id k3_r5_of_match[] = {
1454         {
1455                 .compatible     = "ti,am654-r5fss",
1456                 .data           = am65x_r5f_dev_data,
1457         },
1458         { /* sentinel */ },
1459 };
1460 MODULE_DEVICE_TABLE(of, k3_r5_of_match);
1462 static struct platform_driver k3_r5_rproc_driver = {
1463         .probe = k3_r5_probe,
1464         .remove = k3_r5_remove,
1465         .driver = {
1466                 .name = "k3_r5_rproc",
1467                 .of_match_table = k3_r5_of_match,
1468         },
1469 };
1471 module_platform_driver(k3_r5_rproc_driver);
1473 MODULE_LICENSE("GPL v2");
1474 MODULE_DESCRIPTION("TI K3 R5F remote processor driver");
1475 MODULE_AUTHOR("Suman Anna <s-anna@ti.com>");