remoteproc/k3-r5: fix loading into BTCM when using R5 local addresses
[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         return ret;
385 /*
386  * This function implements the .unprepare() ops and performs the complimentary
387  * operations to that of the .prepare() ops. The function is used to assert the
388  * resets on all applicable cores for the rproc device (depending on LockStep
389  * or Split mode). This completes the second portion of powering down the R5F
390  * cores. The cores themselves are only halted in the .stop() ops, and the
391  * .unprepare() ops is invoked by the remoteproc core after the remoteproc is
392  * stopped.
393  */
394 static int k3_r5_rproc_unprepare(struct rproc *rproc)
396         struct k3_r5_rproc *kproc = rproc->priv;
397         struct k3_r5_cluster *cluster = kproc->cluster;
398         struct k3_r5_core *core = kproc->core;
399         struct device *dev = kproc->dev;
400         int ret;
402         ret = cluster->mode ? k3_r5_lockstep_reset(cluster) :
403                               k3_r5_split_reset(core);
404         if (ret)
405                 dev_err(dev, "unable to disable cores, ret = %d\n", ret);
407         return ret;
410 /*
411  * The R5F start sequence includes two different operations
412  * 1. Configure the boot vector for R5F core(s)
413  * 2. Unhalt/Run the R5F core(s)
414  *
415  * The sequence is different between LockStep and Split modes. The LockStep
416  * mode requires the boot vector to be configured only for Core0, and then
417  * unhalt both the cores to start the execution - Core1 needs to be unhalted
418  * first followed by Core0. The Split-mode requires that Core0 to be maintained
419  * always in a higher power state that Core1 (implying Core1 needs to be started
420  * always only after Core0 is started).
421  */
422 static int k3_r5_rproc_start(struct rproc *rproc)
424         struct k3_r5_rproc *kproc = rproc->priv;
425         struct k3_r5_cluster *cluster = kproc->cluster;
426         struct mbox_client *client = &kproc->client;
427         struct device *dev = kproc->dev;
428         struct k3_r5_core *core;
429         u32 boot_addr;
430         int ret;
432         client->dev = dev;
433         client->tx_done = NULL;
434         client->rx_callback = k3_r5_rproc_mbox_callback;
435         client->tx_block = false;
436         client->knows_txdone = false;
438         kproc->mbox = mbox_request_channel(client, 0);
439         if (IS_ERR(kproc->mbox)) {
440                 ret = -EBUSY;
441                 dev_err(dev, "mbox_request_channel failed: %ld\n",
442                         PTR_ERR(kproc->mbox));
443                 return ret;
444         }
446         /*
447          * Ping the remote processor, this is only for sanity-sake for now;
448          * there is no functional effect whatsoever.
449          *
450          * Note that the reply will _not_ arrive immediately: this message
451          * will wait in the mailbox fifo until the remote processor is booted.
452          */
453         ret = mbox_send_message(kproc->mbox, (void *)RP_MBOX_ECHO_REQUEST);
454         if (ret < 0) {
455                 dev_err(dev, "mbox_send_message failed: %d\n", ret);
456                 goto put_mbox;
457         }
459         boot_addr = rproc->bootaddr;
460         /* TODO: add boot_addr sanity checking */
461         dev_err(dev, "booting R5F core using boot addr = 0x%x\n", boot_addr);
463         /* boot vector need not be programmed for Core1 in LockStep mode */
464         core = kproc->core;
465         ret = ti_sci_proc_set_config(core->tsp, boot_addr, 0, 0);
466         if (ret)
467                 goto put_mbox;
469         /* unhalt/run all applicable cores */
470         if (cluster->mode) {
471                 list_for_each_entry_reverse(core, &cluster->cores, elem) {
472                         ret = k3_r5_core_run(core);
473                         if (ret)
474                                 goto unroll_core_run;
475                 }
476         } else {
477                 ret = k3_r5_core_run(core);
478                 if (ret)
479                         goto put_mbox;
480         }
482         return 0;
484 unroll_core_run:
485         list_for_each_entry_continue(core, &cluster->cores, elem) {
486                 if (k3_r5_core_halt(core))
487                         dev_warn(core->dev, "core halt back failed\n");
488         }
489 put_mbox:
490         mbox_free_channel(kproc->mbox);
491         return ret;
494 /*
495  * The R5F stop function includes the following operations
496  * 1. Halt R5F core(s)
497  *
498  * The sequence is different between LockStep and Split modes, and the order
499  * of cores the operations are performed are also in general reverse to that
500  * of the start function. The LockStep mode requires each operation to be
501  * performed first on Core0 followed by Core1. The Split-mode requires that
502  * Core0 to be maintained always in a higher power state that Core1 (implying
503  * Core1 needs to be stopped first before Core0).
504  *
505  * Note that the R5F halt operation in general is not effective when the R5F
506  * core is running, but is needed to make sure the core won't run after
507  * deasserting the reset the subsequent time. The asserting of reset can
508  * be done here, but is preferred to be done in the .unprepare() ops - this
509  * maintains the symmetric behavior between the .start(), .stop(), .prepare()
510  * and .unprepare() ops, and also balances them well between sysfs 'state'
511  * flow and device bind/unbind or module removal.
512  */
513 static int k3_r5_rproc_stop(struct rproc *rproc)
515         struct k3_r5_rproc *kproc = rproc->priv;
516         struct k3_r5_cluster *cluster = kproc->cluster;
517         struct k3_r5_core *core = kproc->core;
518         int ret;
520         /* halt all applicable cores */
521         if (cluster->mode) {
522                 list_for_each_entry(core, &cluster->cores, elem) {
523                         ret = k3_r5_core_halt(core);
524                         if (ret) {
525                                 core = list_prev_entry(core, elem);
526                                 goto unroll_core_halt;
527                         }
528                 }
529         } else {
530                 ret = k3_r5_core_halt(core);
531                 if (ret)
532                         goto out;
533         }
535         mbox_free_channel(kproc->mbox);
537         return 0;
539 unroll_core_halt:
540         list_for_each_entry_from_reverse(core, &cluster->cores, elem) {
541                 if (k3_r5_core_run(core))
542                         dev_warn(core->dev, "core run back failed\n");
543         }
544 out:
545         return ret;
548 /*
549  * Internal Memory translation helper
550  *
551  * Custom function implementing the rproc .da_to_va ops to provide address
552  * translation (device address to kernel virtual address) for internal RAMs
553  * present in a DSP or IPU device). The translated addresses can be used
554  * either by the remoteproc core for loading, or by any rpmsg bus drivers.
555  */
556 static void *k3_r5_rproc_da_to_va(struct rproc *rproc, u64 da, int len,
557                                   u32 flags)
559         struct k3_r5_rproc *kproc = rproc->priv;
560         struct k3_r5_core *core = kproc->core;
561         void __iomem *va = NULL;
562         phys_addr_t bus_addr;
563         u32 dev_addr, offset;
564         size_t size;
565         int i;
567         if (len <= 0)
568                 return NULL;
570         /* handle both R5 and SoC views of ATCM and BTCM */
571         for (i = 0; i < core->num_mems; i++) {
572                 bus_addr = core->mem[i].bus_addr;
573                 dev_addr = core->mem[i].dev_addr;
574                 size = core->mem[i].size;
576                 /* handle R5-view addresses of TCMs */
577                 if (da >= dev_addr && ((da + len) <= (dev_addr + size))) {
578                         offset = da - dev_addr;
579                         va = core->mem[i].cpu_addr + offset;
580                         return (__force void *)va;
581                 }
583                 /* handle SoC-view addresses of TCMs */
584                 if (da >= bus_addr && ((da + len) <= (bus_addr + size))) {
585                         offset = da - bus_addr;
586                         va = core->mem[i].cpu_addr + offset;
587                         return (__force void *)va;
588                 }
589         }
591         /* handle any SRAM regions using SoC-view addresses */
592         for (i = 0; i < core->num_sram; i++) {
593                 dev_addr = core->sram[i].dev_addr;
594                 size = core->sram[i].size;
596                 if (da >= dev_addr && ((da + len) <= (dev_addr + size))) {
597                         offset = da - dev_addr;
598                         va = core->sram[i].cpu_addr + offset;
599                         return (__force void *)va;
600                 }
601         }
603         /* handle static DDR reserved memory regions */
604         for (i = 0; i < kproc->num_rmems; i++) {
605                 dev_addr = kproc->rmem[i].dev_addr;
606                 size = kproc->rmem[i].size;
608                 if (da >= dev_addr && ((da + len) <= (dev_addr + size))) {
609                         offset = da - dev_addr;
610                         va = kproc->rmem[i].cpu_addr + offset;
611                         return (__force void *)va;
612                 }
613         }
615         return NULL;
618 static const struct rproc_ops k3_r5_rproc_ops = {
619         .prepare        = k3_r5_rproc_prepare,
620         .unprepare      = k3_r5_rproc_unprepare,
621         .start          = k3_r5_rproc_start,
622         .stop           = k3_r5_rproc_stop,
623         .kick           = k3_r5_rproc_kick,
624         .da_to_va       = k3_r5_rproc_da_to_va,
625 };
627 static const char *k3_r5_rproc_get_firmware(struct device *dev)
629         const struct k3_r5_rproc_dev_data *data =
630                                 of_device_get_match_data(dev->parent);
632         if (!data) {
633                 dev_err(dev, "data is NULL, %s\n", dev_name(dev));
634                 return ERR_PTR(-ENODEV);
635         }
637         for (; data && data->device_name; data++) {
638                 if (!strcmp(dev_name(dev), data->device_name))
639                         return data->fw_name;
640         }
642         return ERR_PTR(-ENODEV);
645 static int k3_r5_rproc_configure(struct k3_r5_rproc *kproc)
647         struct k3_r5_cluster *cluster = kproc->cluster;
648         struct device *dev = kproc->dev;
649         struct k3_r5_core *core0, *core, *temp;
650         u32 ctrl = 0, cfg = 0, stat = 0;
651         u32 set_cfg = 0, clr_cfg = 0;
652         u64 boot_vec = 0;
653         int ret;
655         core0 = list_first_entry(&cluster->cores, struct k3_r5_core, elem);
656         core = cluster->mode ? core0 : kproc->core;
658         ret = ti_sci_proc_get_status(core->tsp, &boot_vec, &cfg, &ctrl,
659                                      &stat);
660         if (ret < 0)
661                 return ret;
663         dev_dbg(dev, "boot_vector = 0x%llx, cfg = 0x%x ctrl = 0x%x stat = 0x%x\n",
664                 boot_vec, cfg, ctrl, stat);
666         if (!(stat & PROC_BOOT_STATUS_FLAG_R5_LOCKSTEP_PERMITTED) &&
667             cluster->mode) {
668                 dev_err(cluster->dev, "lockstep mode not permitted, force configuring for split-mode\n");
669                 cluster->mode = 0;
670         }
672         /* always enable ARM mode and set boot vector to 0 */
673         boot_vec = 0x0;
674         if (core == core0) {
675                 clr_cfg = PROC_BOOT_CFG_FLAG_R5_TEINIT;
676                 clr_cfg |= PROC_BOOT_CFG_FLAG_R5_LOCKSTEP;
677         }
679         if (core->atcm_enable)
680                 set_cfg |= PROC_BOOT_CFG_FLAG_R5_ATCM_EN;
681         else
682                 clr_cfg |= PROC_BOOT_CFG_FLAG_R5_ATCM_EN;
684         if (core->btcm_enable)
685                 set_cfg |= PROC_BOOT_CFG_FLAG_R5_BTCM_EN;
686         else
687                 clr_cfg |= PROC_BOOT_CFG_FLAG_R5_BTCM_EN;
689         if (core->loczrama)
690                 set_cfg |= PROC_BOOT_CFG_FLAG_R5_TCM_RSTBASE;
691         else
692                 clr_cfg |= PROC_BOOT_CFG_FLAG_R5_TCM_RSTBASE;
694         if (cluster->mode) {
695                 /*
696                  * work around system firmware limitations to make sure both
697                  * cores are programmed symmetrically in LockStep. LockStep
698                  * and TEINIT config is only allowed with Core0.
699                  */
700                 list_for_each_entry(temp, &cluster->cores, elem) {
701                         ret = k3_r5_core_halt(core);
702                         if (ret)
703                                 goto out;
705                         if (temp != core) {
706                                 clr_cfg &= ~PROC_BOOT_CFG_FLAG_R5_LOCKSTEP;
707                                 clr_cfg &= ~PROC_BOOT_CFG_FLAG_R5_TEINIT;
708                         }
709                         ret = ti_sci_proc_set_config(temp->tsp, boot_vec,
710                                                      set_cfg, clr_cfg);
711                         if (ret)
712                                 goto out;
713                 }
715                 set_cfg = PROC_BOOT_CFG_FLAG_R5_LOCKSTEP;
716                 clr_cfg = 0;
717                 ret = ti_sci_proc_set_config(core->tsp, boot_vec,
718                                              set_cfg, clr_cfg);
719         } else {
720                 ret = k3_r5_core_halt(core);
721                 if (ret)
722                         goto out;
724                 ret = ti_sci_proc_set_config(core->tsp, boot_vec,
725                                              set_cfg, clr_cfg);
726         }
728 out:
729         return ret;
732 static int k3_r5_reserved_mem_init(struct k3_r5_rproc *kproc)
734         struct device *dev = kproc->dev;
735         struct device_node *np = dev->of_node;
736         struct device_node *rmem_np;
737         struct reserved_mem *rmem;
738         int num_rmems;
739         int ret, i;
741         num_rmems = of_property_count_elems_of_size(np, "memory-region",
742                                                     sizeof(phandle));
743         if (num_rmems <= 0) {
744                 dev_err(dev, "device does not have reserved memory regions, ret = %d\n",
745                         num_rmems);
746                 return -EINVAL;
747         }
748         if (num_rmems < 2) {
749                 dev_err(dev, "device needs atleast two memory regions to be defined, num = %d\n",
750                         num_rmems);
751                 return -EINVAL;
752         }
754         /* use reserved memory region 0 for vring DMA allocations */
755         ret = of_reserved_mem_device_init_by_idx(dev, np, 0);
756         if (ret) {
757                 dev_err(dev, "device cannot initialize DMA pool, ret = %d\n",
758                         ret);
759                 return ret;
760         }
762         num_rmems--;
763         kproc->rmem = kcalloc(num_rmems, sizeof(*kproc->rmem), GFP_KERNEL);
764         if (!kproc->rmem) {
765                 ret = -ENOMEM;
766                 goto release_rmem;
767         }
769         /* use remaining reserved memory regions for static carveouts */
770         for (i = 0; i < num_rmems; i++) {
771                 rmem_np = of_parse_phandle(np, "memory-region", i + 1);
772                 if (!rmem_np) {
773                         ret = -EINVAL;
774                         goto unmap_rmem;
775                 }
777                 rmem = of_reserved_mem_lookup(rmem_np);
778                 if (!rmem) {
779                         of_node_put(rmem_np);
780                         ret = -EINVAL;
781                         goto unmap_rmem;
782                 }
783                 of_node_put(rmem_np);
785                 kproc->rmem[i].bus_addr = rmem->base;
786                 /* 64-bit address regions currently not supported */
787                 kproc->rmem[i].dev_addr = (u32)rmem->base;
788                 kproc->rmem[i].size = rmem->size;
789                 kproc->rmem[i].cpu_addr = ioremap_wc(rmem->base, rmem->size);
790                 if (!kproc->rmem[i].cpu_addr) {
791                         dev_err(dev, "failed to map reserved memory#%d at %pa of size %pa\n",
792                                 i + 1, &rmem->base, &rmem->size);
793                         ret = -ENOMEM;
794                         goto unmap_rmem;
795                 }
797                 dev_dbg(dev, "reserved memory%d: bus addr %pa size 0x%zx va %pK da 0x%x\n",
798                         i + 1, &kproc->rmem[i].bus_addr,
799                         kproc->rmem[i].size, kproc->rmem[i].cpu_addr,
800                         kproc->rmem[i].dev_addr);
801         }
802         kproc->num_rmems = num_rmems;
804         return 0;
806 unmap_rmem:
807         for (i--; i >= 0; i--) {
808                 if (kproc->rmem[i].cpu_addr)
809                         iounmap(kproc->rmem[i].cpu_addr);
810         }
811         kfree(kproc->rmem);
812 release_rmem:
813         of_reserved_mem_device_release(dev);
814         return ret;
817 static void k3_r5_reserved_mem_exit(struct k3_r5_rproc *kproc)
819         int i;
821         for (i = 0; i < kproc->num_rmems; i++)
822                 iounmap(kproc->rmem[i].cpu_addr);
823         kfree(kproc->rmem);
825         of_reserved_mem_device_release(kproc->dev);
828 static int k3_r5_cluster_rproc_init(struct platform_device *pdev)
830         struct k3_r5_cluster *cluster = platform_get_drvdata(pdev);
831         struct device *dev = &pdev->dev;
832         struct k3_r5_rproc *kproc;
833         struct k3_r5_core *core, *core1;
834         struct device *cdev;
835         const char *fw_name;
836         struct rproc *rproc;
837         int ret;
839         core1 = list_last_entry(&cluster->cores, struct k3_r5_core, elem);
840         list_for_each_entry(core, &cluster->cores, elem) {
841                 cdev = core->dev;
842                 fw_name = k3_r5_rproc_get_firmware(cdev);
843                 if (IS_ERR(fw_name)) {
844                         ret = PTR_ERR(fw_name);
845                         goto out;
846                 }
848                 rproc = rproc_alloc(cdev, dev_name(cdev), &k3_r5_rproc_ops,
849                                     fw_name, sizeof(*kproc));
850                 if (!rproc) {
851                         ret = -ENOMEM;
852                         goto out;
853                 }
855                 /* K3 R5s have a Region Address Translator (RAT) but no MMU */
856                 rproc->has_iommu = false;
857                 /* error recovery is not supported at present */
858                 rproc->recovery_disabled = true;
860                 kproc = rproc->priv;
861                 kproc->cluster = cluster;
862                 kproc->core = core;
863                 kproc->dev = cdev;
864                 kproc->rproc = rproc;
865                 core->rproc = rproc;
867                 ret = k3_r5_rproc_configure(kproc);
868                 if (ret) {
869                         dev_err(dev, "initial configure failed, ret = %d\n",
870                                 ret);
871                         goto err_config;
872                 }
874                 ret = k3_r5_reserved_mem_init(kproc);
875                 if (ret) {
876                         dev_err(dev, "reserved memory init failed, ret = %d\n",
877                                 ret);
878                         goto err_config;
879                 }
881                 ret = rproc_add(rproc);
882                 if (ret) {
883                         dev_err(dev, "rproc_add failed, ret = %d\n", ret);
884                         goto err_add;
885                 }
887                 /* create only one rproc in lockstep mode */
888                 if (cluster->mode)
889                         break;
890         }
892         return 0;
894 err_split:
895         rproc_del(rproc);
896 err_add:
897         k3_r5_reserved_mem_exit(kproc);
898 err_config:
899         rproc_free(rproc);
900         core->rproc = NULL;
901 out:
902         /* undo core0 upon any failures on core1 in split-mode */
903         if (!cluster->mode && core == core1) {
904                 core = list_prev_entry(core, elem);
905                 rproc = core->rproc;
906                 kproc = rproc->priv;
907                 goto err_split;
908         }
909         return ret;
912 static int k3_r5_cluster_rproc_exit(struct platform_device *pdev)
914         struct k3_r5_cluster *cluster = platform_get_drvdata(pdev);
915         struct k3_r5_rproc *kproc;
916         struct k3_r5_core *core;
917         struct rproc *rproc;
919         /*
920          * lockstep mode has only one rproc associated with first core, whereas
921          * split-mode has two rprocs associated with each core, and requires
922          * that core1 be powered down first
923          */
924         core = cluster->mode ?
925                 list_first_entry(&cluster->cores, struct k3_r5_core, elem) :
926                 list_last_entry(&cluster->cores, struct k3_r5_core, elem);
928         list_for_each_entry_from_reverse(core, &cluster->cores, elem) {
929                 rproc = core->rproc;
930                 kproc = rproc->priv;
932                 rproc_del(rproc);
934                 k3_r5_reserved_mem_exit(kproc);
936                 rproc_free(rproc);
937                 core->rproc = NULL;
938         }
940         return 0;
943 static int k3_r5_core_of_get_internal_memories(struct platform_device *pdev,
944                                                struct k3_r5_core *core)
946         static const char * const mem_names[] = {"atcm", "btcm"};
947         struct device *dev = &pdev->dev;
948         struct resource *res;
949         int num_mems;
950         int i, ret;
952         num_mems = ARRAY_SIZE(mem_names);
953         core->mem = devm_kcalloc(dev, num_mems, sizeof(*core->mem), GFP_KERNEL);
954         if (!core->mem)
955                 return -ENOMEM;
957         for (i = 0; i < num_mems; i++) {
958                 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
959                                                    mem_names[i]);
960                 if (!res) {
961                         dev_err(dev, "found no memory resource for %s\n",
962                                 mem_names[i]);
963                         ret = -EINVAL;
964                         goto fail;
965                 }
966                 if (!devm_request_mem_region(dev, res->start,
967                                              resource_size(res),
968                                              dev_name(dev))) {
969                         dev_err(dev, "could not request %s region for resource\n",
970                                 mem_names[i]);
971                         ret = -EBUSY;
972                         goto fail;
973                 }
975                 core->mem[i].cpu_addr = devm_ioremap_wc(dev, res->start,
976                                                         resource_size(res));
977                 if (IS_ERR(core->mem[i].cpu_addr)) {
978                         dev_err(dev, "failed to map %s memory\n", mem_names[i]);
979                         ret = PTR_ERR(core->mem[i].cpu_addr);
980                         devm_release_mem_region(dev, res->start,
981                                                 resource_size(res));
982                         goto fail;
983                 }
984                 core->mem[i].bus_addr = res->start;
986                 /*
987                  * TODO:
988                  * The R5F cores can place ATCM & BTCM anywhere in its address
989                  * based on the corresponding Region Registers in the System
990                  * Control coprocessor. For now, place ATCM and BTCM at
991                  * addresses 0 and 0x41010000 (same as the bus address on AM65x
992                  * SoCs) based on loczrama setting
993                  */
994                 if (!strcmp(mem_names[i], "atcm")) {
995                         core->mem[i].dev_addr = core->loczrama ?
996                                                         0 : K3_R5_TCM_DEV_ADDR;
997                 } else {
998                         core->mem[i].dev_addr = core->loczrama ?
999                                                         K3_R5_TCM_DEV_ADDR : 0;
1000                 }
1001                 core->mem[i].size = resource_size(res);
1003                 dev_dbg(dev, "memory %8s: bus addr %pa size 0x%zx va %p da 0x%x\n",
1004                         mem_names[i], &core->mem[i].bus_addr,
1005                         core->mem[i].size, core->mem[i].cpu_addr,
1006                         core->mem[i].dev_addr);
1007         }
1008         core->num_mems = num_mems;
1010         return 0;
1012 fail:
1013         for (i--; i >= 0; i--) {
1014                 devm_release_mem_region(dev, core->mem[i].bus_addr,
1015                                         core->mem[i].size);
1016                 devm_iounmap(dev, core->mem[i].cpu_addr);
1017         }
1018         if (core->mem)
1019                 devm_kfree(dev, core->mem);
1020         return ret;
1023 static int k3_r5_core_of_get_sram_memories(struct platform_device *pdev,
1024                                            struct k3_r5_core *core)
1026         struct device_node *np = pdev->dev.of_node;
1027         struct device *dev = &pdev->dev;
1028         struct device_node *sram_np;
1029         struct resource res;
1030         int num_sram;
1031         int i, ret;
1033         num_sram = of_property_count_elems_of_size(np, "sram", sizeof(phandle));
1034         if (num_sram <= 0) {
1035                 dev_dbg(dev, "device does not use reserved on-chip memories, num_sram = %d\n",
1036                         num_sram);
1037                 return 0;
1038         }
1040         core->sram = kcalloc(num_sram, sizeof(*core->sram), GFP_KERNEL);
1041         if (!core->sram)
1042                 return -ENOMEM;
1044         for (i = 0; i < num_sram; i++) {
1045                 sram_np = of_parse_phandle(np, "sram", i);
1046                 if (!sram_np) {
1047                         ret = -EINVAL;
1048                         goto fail;
1049                 }
1051                 if (!of_device_is_available(sram_np)) {
1052                         of_node_put(sram_np);
1053                         ret = -EINVAL;
1054                         goto fail;
1055                 }
1057                 ret = of_address_to_resource(sram_np, 0, &res);
1058                 of_node_put(sram_np);
1059                 if (ret) {
1060                         ret = -EINVAL;
1061                         goto fail;
1062                 }
1063                 core->sram[i].bus_addr = res.start;
1064                 core->sram[i].dev_addr = res.start;
1065                 core->sram[i].size = resource_size(&res);
1066                 core->sram[i].cpu_addr = ioremap(res.start,
1067                                                  resource_size(&res));
1068                 if (!core->sram[i].cpu_addr) {
1069                         dev_err(dev, "failed to parse and map sram%d memory at %pad\n",
1070                                 i, &res.start);
1071                         ret = -ENOMEM;
1072                         goto fail;
1073                 }
1075                 dev_dbg(dev, "memory    sram%d: bus addr %pa size 0x%zx va %pK da 0x%x\n",
1076                         i, &core->sram[i].bus_addr,
1077                         core->sram[i].size, core->sram[i].cpu_addr,
1078                         core->sram[i].dev_addr);
1079         }
1080         core->num_sram = num_sram;
1082         return 0;
1084 fail:
1085         for (i--; i >= 0; i--) {
1086                 if (core->sram[i].cpu_addr)
1087                         iounmap(core->sram[i].cpu_addr);
1088         }
1089         kfree(core->sram);
1091         return ret;
1094 static
1095 struct ti_sci_proc *k3_r5_core_of_get_tsp(struct device *dev,
1096                                           const struct ti_sci_handle *sci)
1098         struct ti_sci_proc *tsp;
1099         u32 temp[2];
1100         int ret;
1102         ret = of_property_read_u32_array(dev->of_node, "ti,sci-proc-ids",
1103                                          temp, 2);
1104         if (ret < 0)
1105                 return ERR_PTR(ret);
1107         tsp = kzalloc(sizeof(*tsp), GFP_KERNEL);
1108         if (!tsp)
1109                 return ERR_PTR(-ENOMEM);
1111         tsp->dev = dev;
1112         tsp->sci = sci;
1113         tsp->ops = &sci->ops.proc_ops;
1114         tsp->proc_id = temp[0];
1115         tsp->host_id = temp[1];
1117         return tsp;
1120 static int k3_r5_core_of_init(struct platform_device *pdev)
1122         struct device *dev = &pdev->dev;
1123         struct device_node *np = dev->of_node;
1124         struct k3_r5_core *core;
1125         int ret, ret1, i;
1127         core = devm_kzalloc(dev, sizeof(*core), GFP_KERNEL);
1128         if (!core)
1129                 return -ENOMEM;
1131         core->dev = dev;
1132         core->atcm_enable = 0;
1133         core->btcm_enable = 1;
1134         core->loczrama = 1;
1136         ret = of_property_read_u32(np, "atcm-enable", &core->atcm_enable);
1137         if (ret < 0 && ret != -EINVAL) {
1138                 dev_err(dev, "invalid format for atcm-enable, ret = %d\n", ret);
1139                 goto err_of;
1140         }
1142         ret = of_property_read_u32(np, "btcm-enable", &core->btcm_enable);
1143         if (ret < 0 && ret != -EINVAL) {
1144                 dev_err(dev, "invalid format for btcm-enable, ret = %d\n", ret);
1145                 goto err_of;
1146         }
1148         ret = of_property_read_u32(np, "loczrama", &core->loczrama);
1149         if (ret < 0 && ret != -EINVAL) {
1150                 dev_err(dev, "invalid format for loczrama, ret = %d\n", ret);
1151                 goto err_of;
1152         }
1154         core->ti_sci = ti_sci_get_by_phandle(np, "ti,sci");
1155         if (IS_ERR(core->ti_sci)) {
1156                 ret = PTR_ERR(core->ti_sci);
1157                 if (ret != -EPROBE_DEFER) {
1158                         dev_err(dev, "failed to get ti-sci handle, ret = %d\n",
1159                                 ret);
1160                 }
1161                 core->ti_sci = NULL;
1162                 goto err_of;
1163         }
1165         ret = of_property_read_u32(np, "ti,sci-dev-id", &core->ti_sci_id);
1166         if (ret) {
1167                 dev_err(dev, "missing 'ti,sci-dev-id' property\n");
1168                 goto err_sci_id;
1169         }
1171         core->reset = reset_control_get_exclusive(dev, NULL);
1172         if (IS_ERR(core->reset)) {
1173                 ret = PTR_ERR(core->reset);
1174                 if (ret != -EPROBE_DEFER) {
1175                         dev_err(dev, "failed to get reset handle, ret = %d\n",
1176                                 ret);
1177                 }
1178                 goto err_sci_id;
1179         }
1181         core->tsp = k3_r5_core_of_get_tsp(dev, core->ti_sci);
1182         if (IS_ERR(core->tsp)) {
1183                 dev_err(dev, "failed to construct ti-sci proc control, ret = %d\n",
1184                         ret);
1185                 ret = PTR_ERR(core->tsp);
1186                 goto err_sci_proc;
1187         }
1189         ret = ti_sci_proc_request(core->tsp);
1190         if (ret < 0) {
1191                 dev_err(dev, "ti_sci_proc_request failed, ret = %d\n", ret);
1192                 goto err_proc;
1193         }
1195         ret = k3_r5_core_of_get_internal_memories(pdev, core);
1196         if (ret) {
1197                 dev_err(dev, "failed to get internal memories, ret = %d\n",
1198                         ret);
1199                 goto err_intmem;
1200         }
1202         ret = k3_r5_core_of_get_sram_memories(pdev, core);
1203         if (ret) {
1204                 dev_err(dev, "failed to get sram memories, ret = %d\n", ret);
1205                 goto err_sram;
1206         }
1208         platform_set_drvdata(pdev, core);
1210         return 0;
1212 err_sram:
1213         for (i = 0; i < core->num_mems; i++) {
1214                 devm_release_mem_region(dev, core->mem[i].bus_addr,
1215                                         core->mem[i].size);
1216                 devm_iounmap(dev, core->mem[i].cpu_addr);
1217         }
1218         devm_kfree(dev, core->mem);
1219 err_intmem:
1220         ret1 = ti_sci_proc_release(core->tsp);
1221         if (ret1)
1222                 dev_err(dev, "failed to release proc, ret1 = %d\n", ret1);
1223 err_proc:
1224         kfree(core->tsp);
1225 err_sci_proc:
1226         reset_control_put(core->reset);
1227 err_sci_id:
1228         ret1 = ti_sci_put_handle(core->ti_sci);
1229         if (ret1)
1230                 dev_err(dev, "failed to put ti_sci handle, ret = %d\n", ret1);
1231 err_of:
1232         devm_kfree(dev, core);
1233         return ret;
1236 /*
1237  * free the resources explicitly since driver model is not being used
1238  * for the child R5F devices
1239  */
1240 static int k3_r5_core_of_exit(struct platform_device *pdev)
1242         struct k3_r5_core *core = platform_get_drvdata(pdev);
1243         struct device *dev = &pdev->dev;
1244         int i, ret;
1246         for (i = 0; i < core->num_sram; i++)
1247                 iounmap(core->sram[i].cpu_addr);
1248         kfree(core->sram);
1250         for (i = 0; i < core->num_mems; i++) {
1251                 devm_release_mem_region(dev, core->mem[i].bus_addr,
1252                                         core->mem[i].size);
1253                 devm_iounmap(dev, core->mem[i].cpu_addr);
1254         }
1255         if (core->mem)
1256                 devm_kfree(dev, core->mem);
1258         ret = ti_sci_proc_release(core->tsp);
1259         if (ret)
1260                 dev_err(dev, "failed to release proc, ret = %d\n", ret);
1262         kfree(core->tsp);
1263         reset_control_put(core->reset);
1265         ret = ti_sci_put_handle(core->ti_sci);
1266         if (ret)
1267                 dev_err(dev, "failed to put ti_sci handle, ret = %d\n", ret);
1269         platform_set_drvdata(pdev, NULL);
1270         devm_kfree(dev, core);
1272         return ret;
1275 static int k3_r5_cluster_of_init(struct platform_device *pdev)
1277         struct k3_r5_cluster *cluster = platform_get_drvdata(pdev);
1278         struct device *dev = &pdev->dev;
1279         struct device_node *np = dev->of_node;
1280         struct platform_device *cpdev;
1281         struct device_node *child;
1282         struct k3_r5_core *core, *temp;
1283         int ret;
1285         for_each_available_child_of_node(np, child) {
1286                 cpdev = of_find_device_by_node(child);
1287                 if (!cpdev) {
1288                         ret = -ENODEV;
1289                         dev_err(dev, "could not get R5 core platform device\n");
1290                         goto fail;
1291                 }
1293                 ret = k3_r5_core_of_init(cpdev);
1294                 if (ret) {
1295                         dev_err(dev, "k3_r5_core_of_init failed, ret = %d\n",
1296                                 ret);
1297                         put_device(&cpdev->dev);
1298                         goto fail;
1299                 }
1301                 core = platform_get_drvdata(cpdev);
1302                 put_device(&cpdev->dev);
1303                 list_add_tail(&core->elem, &cluster->cores);
1304         }
1306         return 0;
1308 fail:
1309         list_for_each_entry_safe_reverse(core, temp, &cluster->cores, elem) {
1310                 list_del(&core->elem);
1311                 cpdev = to_platform_device(core->dev);
1312                 if (k3_r5_core_of_exit(cpdev))
1313                         dev_err(dev, "k3_r5_core_of_exit cleanup failed\n");
1314         }
1315         return ret;
1318 static int k3_r5_cluster_of_exit(struct platform_device *pdev)
1320         struct k3_r5_cluster *cluster = platform_get_drvdata(pdev);
1321         struct device *dev = &pdev->dev;
1322         struct platform_device *cpdev;
1323         struct k3_r5_core *core, *temp;
1324         int ret;
1326         list_for_each_entry_safe_reverse(core, temp, &cluster->cores, elem) {
1327                 list_del(&core->elem);
1328                 cpdev = to_platform_device(core->dev);
1329                 ret = k3_r5_core_of_exit(cpdev);
1330                 if (ret) {
1331                         dev_err(dev, "k3_r5_core_of_exit failed, ret = %d\n",
1332                                 ret);
1333                         break;
1334                 }
1335         }
1337         return ret;
1340 static int k3_r5_probe(struct platform_device *pdev)
1342         struct device *dev = &pdev->dev;
1343         struct device_node *np = dev->of_node;
1344         struct k3_r5_cluster *cluster;
1345         int ret, ret1;
1346         int num_cores;
1348         cluster = devm_kzalloc(dev, sizeof(*cluster), GFP_KERNEL);
1349         if (!cluster)
1350                 return -ENOMEM;
1352         cluster->dev = dev;
1353         cluster->mode = CLUSTER_MODE_LOCKSTEP;
1354         INIT_LIST_HEAD(&cluster->cores);
1356         ret = of_property_read_u32(np, "lockstep-mode", &cluster->mode);
1357         if (ret < 0 && ret != -EINVAL) {
1358                 dev_err(dev, "invalid format for lockstep-mode, ret = %d\n",
1359                         ret);
1360                 return ret;
1361         }
1363         num_cores = of_get_available_child_count(np);
1364         if (num_cores != 2) {
1365                 dev_err(dev, "MCU cluster requires both R5F cores to be enabled, num_cores = %d\n",
1366                         num_cores);
1367                 return -ENODEV;
1368         }
1370         platform_set_drvdata(pdev, cluster);
1372         dev_info(dev, "creating child devices for R5F cores\n");
1373         ret = of_platform_populate(np, NULL, NULL, dev);
1374         if (ret) {
1375                 dev_err(dev, "of_platform_populate failed, ret = %d\n", ret);
1376                 return ret;
1377         }
1379         ret = k3_r5_cluster_of_init(pdev);
1380         if (ret) {
1381                 dev_err(dev, "k3_r5_cluster_of_init failed, ret = %d\n", ret);
1382                 goto fail_of;
1383         }
1385         ret = k3_r5_cluster_rproc_init(pdev);
1386         if (ret) {
1387                 dev_err(dev, "k3_r5_cluster_rproc_init failed, ret = %d\n",
1388                         ret);
1389                 goto fail_rproc;
1390         }
1392         return 0;
1394 fail_rproc:
1395         ret1 = k3_r5_cluster_of_exit(pdev);
1396         if (ret1)
1397                 dev_err(dev, "k3_r5_cluster_of_exit failed, ret = %d\n", ret1);
1398 fail_of:
1399         of_platform_depopulate(dev);
1400         return ret;
1403 static int k3_r5_remove(struct platform_device *pdev)
1405         struct device *dev = &pdev->dev;
1406         int ret;
1408         ret = k3_r5_cluster_rproc_exit(pdev);
1409         if (ret) {
1410                 dev_err(dev, "k3_r5_cluster_rproc_exit failed, ret = %d\n",
1411                         ret);
1412                 goto fail;
1413         }
1415         ret = k3_r5_cluster_of_exit(pdev);
1416         if (ret) {
1417                 dev_err(dev, "k3_r5_cluster_of_exit failed, ret = %d\n", ret);
1418                 goto fail;
1419         }
1421         dev_info(dev, "removing child devices for R5F cores\n");
1422         of_platform_depopulate(dev);
1424 fail:
1425         return ret;
1428 static const struct k3_r5_rproc_dev_data am65x_r5f_dev_data[] = {
1429         {
1430                 .device_name    = "41000000.r5f",
1431                 .fw_name        = "am65x-mcu-r5f0_0-fw",
1432         },
1433         {
1434                 .device_name    = "41400000.r5f",
1435                 .fw_name        = "am65x-mcu-r5f0_1-fw",
1436         },
1437         {
1438                 /* sentinel */
1439         },
1440 };
1442 static const struct of_device_id k3_r5_of_match[] = {
1443         {
1444                 .compatible     = "ti,am654-r5fss",
1445                 .data           = am65x_r5f_dev_data,
1446         },
1447         { /* sentinel */ },
1448 };
1449 MODULE_DEVICE_TABLE(of, k3_r5_of_match);
1451 static struct platform_driver k3_r5_rproc_driver = {
1452         .probe = k3_r5_probe,
1453         .remove = k3_r5_remove,
1454         .driver = {
1455                 .name = "k3_r5_rproc",
1456                 .of_match_table = k3_r5_of_match,
1457         },
1458 };
1460 module_platform_driver(k3_r5_rproc_driver);
1462 MODULE_LICENSE("GPL v2");
1463 MODULE_DESCRIPTION("TI K3 R5F remote processor driver");
1464 MODULE_AUTHOR("Suman Anna <s-anna@ti.com>");