429bcb7a2609e8e939f36b2a821102ace4857f11
[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 R5-view of ATCM addresses first using address 0 */
571         size = core->mem[0].size;
572         if (da >= 0 && ((da + len) <= size)) {
573                 offset = da;
574                 va = core->mem[0].cpu_addr + offset;
575                 return (__force void *)va;
576         }
578         /* handle SoC-view addresses for ATCM and BTCM */
579         for (i = 0; i < core->num_mems; i++) {
580                 bus_addr = core->mem[i].bus_addr;
581                 dev_addr = core->mem[i].dev_addr;
582                 size = core->mem[i].size;
584                 if (da >= bus_addr &&
585                     ((da + len) <= (bus_addr + size))) {
586                         offset = da - bus_addr;
587                         va = core->mem[i].cpu_addr + offset;
588                         return (__force void *)va;
589                 }
590         }
592         /* handle any SRAM regions using SoC-view addresses */
593         for (i = 0; i < core->num_sram; i++) {
594                 dev_addr = core->sram[i].dev_addr;
595                 size = core->sram[i].size;
597                 if (da >= dev_addr && ((da + len) <= (dev_addr + size))) {
598                         offset = da - dev_addr;
599                         va = core->sram[i].cpu_addr + offset;
600                         return (__force void *)va;
601                 }
602         }
604         /* handle static DDR reserved memory regions */
605         for (i = 0; i < kproc->num_rmems; i++) {
606                 dev_addr = kproc->rmem[i].dev_addr;
607                 size = kproc->rmem[i].size;
609                 if (da >= dev_addr && ((da + len) <= (dev_addr + size))) {
610                         offset = da - dev_addr;
611                         va = kproc->rmem[i].cpu_addr + offset;
612                         return (__force void *)va;
613                 }
614         }
616         return NULL;
619 static const struct rproc_ops k3_r5_rproc_ops = {
620         .prepare        = k3_r5_rproc_prepare,
621         .unprepare      = k3_r5_rproc_unprepare,
622         .start          = k3_r5_rproc_start,
623         .stop           = k3_r5_rproc_stop,
624         .kick           = k3_r5_rproc_kick,
625         .da_to_va       = k3_r5_rproc_da_to_va,
626 };
628 static const char *k3_r5_rproc_get_firmware(struct device *dev)
630         const struct k3_r5_rproc_dev_data *data =
631                                 of_device_get_match_data(dev->parent);
633         if (!data) {
634                 dev_err(dev, "data is NULL, %s\n", dev_name(dev));
635                 return ERR_PTR(-ENODEV);
636         }
638         for (; data && data->device_name; data++) {
639                 if (!strcmp(dev_name(dev), data->device_name))
640                         return data->fw_name;
641         }
643         return ERR_PTR(-ENODEV);
646 static int k3_r5_rproc_configure(struct k3_r5_rproc *kproc)
648         struct k3_r5_cluster *cluster = kproc->cluster;
649         struct device *dev = kproc->dev;
650         struct k3_r5_core *core0, *core, *temp;
651         u32 ctrl = 0, cfg = 0, stat = 0;
652         u32 set_cfg = 0, clr_cfg = 0;
653         u64 boot_vec = 0;
654         int ret;
656         core0 = list_first_entry(&cluster->cores, struct k3_r5_core, elem);
657         core = cluster->mode ? core0 : kproc->core;
659         ret = ti_sci_proc_get_status(core->tsp, &boot_vec, &cfg, &ctrl,
660                                      &stat);
661         if (ret < 0)
662                 return ret;
664         dev_dbg(dev, "boot_vector = 0x%llx, cfg = 0x%x ctrl = 0x%x stat = 0x%x\n",
665                 boot_vec, cfg, ctrl, stat);
667         if (!(stat & PROC_BOOT_STATUS_FLAG_R5_LOCKSTEP_PERMITTED) &&
668             cluster->mode) {
669                 dev_err(cluster->dev, "lockstep mode not permitted, force configuring for split-mode\n");
670                 cluster->mode = 0;
671         }
673         /* always enable ARM mode and set boot vector to 0 */
674         boot_vec = 0x0;
675         if (core == core0) {
676                 clr_cfg = PROC_BOOT_CFG_FLAG_R5_TEINIT;
677                 clr_cfg |= PROC_BOOT_CFG_FLAG_R5_LOCKSTEP;
678         }
680         if (core->atcm_enable)
681                 set_cfg |= PROC_BOOT_CFG_FLAG_R5_ATCM_EN;
682         else
683                 clr_cfg |= PROC_BOOT_CFG_FLAG_R5_ATCM_EN;
685         if (core->btcm_enable)
686                 set_cfg |= PROC_BOOT_CFG_FLAG_R5_BTCM_EN;
687         else
688                 clr_cfg |= PROC_BOOT_CFG_FLAG_R5_BTCM_EN;
690         if (core->loczrama)
691                 set_cfg |= PROC_BOOT_CFG_FLAG_R5_TCM_RSTBASE;
692         else
693                 clr_cfg |= PROC_BOOT_CFG_FLAG_R5_TCM_RSTBASE;
695         if (cluster->mode) {
696                 /*
697                  * work around system firmware limitations to make sure both
698                  * cores are programmed symmetrically in LockStep. LockStep
699                  * and TEINIT config is only allowed with Core0.
700                  */
701                 list_for_each_entry(temp, &cluster->cores, elem) {
702                         ret = k3_r5_core_halt(core);
703                         if (ret)
704                                 goto out;
706                         if (temp != core) {
707                                 clr_cfg &= ~PROC_BOOT_CFG_FLAG_R5_LOCKSTEP;
708                                 clr_cfg &= ~PROC_BOOT_CFG_FLAG_R5_TEINIT;
709                         }
710                         ret = ti_sci_proc_set_config(temp->tsp, boot_vec,
711                                                      set_cfg, clr_cfg);
712                         if (ret)
713                                 goto out;
714                 }
716                 set_cfg = PROC_BOOT_CFG_FLAG_R5_LOCKSTEP;
717                 clr_cfg = 0;
718                 ret = ti_sci_proc_set_config(core->tsp, boot_vec,
719                                              set_cfg, clr_cfg);
720         } else {
721                 ret = k3_r5_core_halt(core);
722                 if (ret)
723                         goto out;
725                 ret = ti_sci_proc_set_config(core->tsp, boot_vec,
726                                              set_cfg, clr_cfg);
727         }
729 out:
730         return ret;
733 static int k3_r5_reserved_mem_init(struct k3_r5_rproc *kproc)
735         struct device *dev = kproc->dev;
736         struct device_node *np = dev->of_node;
737         struct device_node *rmem_np;
738         struct reserved_mem *rmem;
739         int num_rmems;
740         int ret, i;
742         num_rmems = of_property_count_elems_of_size(np, "memory-region",
743                                                     sizeof(phandle));
744         if (num_rmems <= 0) {
745                 dev_err(dev, "device does not have reserved memory regions, ret = %d\n",
746                         num_rmems);
747                 return -EINVAL;
748         }
749         if (num_rmems < 2) {
750                 dev_err(dev, "device needs atleast two memory regions to be defined, num = %d\n",
751                         num_rmems);
752                 return -EINVAL;
753         }
755         /* use reserved memory region 0 for vring DMA allocations */
756         ret = of_reserved_mem_device_init_by_idx(dev, np, 0);
757         if (ret) {
758                 dev_err(dev, "device cannot initialize DMA pool, ret = %d\n",
759                         ret);
760                 return ret;
761         }
763         num_rmems--;
764         kproc->rmem = kcalloc(num_rmems, sizeof(*kproc->rmem), GFP_KERNEL);
765         if (!kproc->rmem) {
766                 ret = -ENOMEM;
767                 goto release_rmem;
768         }
770         /* use remaining reserved memory regions for static carveouts */
771         for (i = 0; i < num_rmems; i++) {
772                 rmem_np = of_parse_phandle(np, "memory-region", i + 1);
773                 if (!rmem_np) {
774                         ret = -EINVAL;
775                         goto unmap_rmem;
776                 }
778                 rmem = of_reserved_mem_lookup(rmem_np);
779                 if (!rmem) {
780                         of_node_put(rmem_np);
781                         ret = -EINVAL;
782                         goto unmap_rmem;
783                 }
784                 of_node_put(rmem_np);
786                 kproc->rmem[i].bus_addr = rmem->base;
787                 /* 64-bit address regions currently not supported */
788                 kproc->rmem[i].dev_addr = (u32)rmem->base;
789                 kproc->rmem[i].size = rmem->size;
790                 kproc->rmem[i].cpu_addr = ioremap_wc(rmem->base, rmem->size);
791                 if (!kproc->rmem[i].cpu_addr) {
792                         dev_err(dev, "failed to map reserved memory#%d at %pa of size %pa\n",
793                                 i + 1, &rmem->base, &rmem->size);
794                         ret = -ENOMEM;
795                         goto unmap_rmem;
796                 }
798                 dev_dbg(dev, "reserved memory%d: bus addr %pa size 0x%zx va %pK da 0x%x\n",
799                         i + 1, &kproc->rmem[i].bus_addr,
800                         kproc->rmem[i].size, kproc->rmem[i].cpu_addr,
801                         kproc->rmem[i].dev_addr);
802         }
803         kproc->num_rmems = num_rmems;
805         return 0;
807 unmap_rmem:
808         for (i--; i >= 0; i--) {
809                 if (kproc->rmem[i].cpu_addr)
810                         iounmap(kproc->rmem[i].cpu_addr);
811         }
812         kfree(kproc->rmem);
813 release_rmem:
814         of_reserved_mem_device_release(dev);
815         return ret;
818 static void k3_r5_reserved_mem_exit(struct k3_r5_rproc *kproc)
820         int i;
822         for (i = 0; i < kproc->num_rmems; i++)
823                 iounmap(kproc->rmem[i].cpu_addr);
824         kfree(kproc->rmem);
826         of_reserved_mem_device_release(kproc->dev);
829 static int k3_r5_cluster_rproc_init(struct platform_device *pdev)
831         struct k3_r5_cluster *cluster = platform_get_drvdata(pdev);
832         struct device *dev = &pdev->dev;
833         struct k3_r5_rproc *kproc;
834         struct k3_r5_core *core, *core1;
835         struct device *cdev;
836         const char *fw_name;
837         struct rproc *rproc;
838         int ret;
840         core1 = list_last_entry(&cluster->cores, struct k3_r5_core, elem);
841         list_for_each_entry(core, &cluster->cores, elem) {
842                 cdev = core->dev;
843                 fw_name = k3_r5_rproc_get_firmware(cdev);
844                 if (IS_ERR(fw_name)) {
845                         ret = PTR_ERR(fw_name);
846                         goto out;
847                 }
849                 rproc = rproc_alloc(cdev, dev_name(cdev), &k3_r5_rproc_ops,
850                                     fw_name, sizeof(*kproc));
851                 if (!rproc) {
852                         ret = -ENOMEM;
853                         goto out;
854                 }
856                 /* K3 R5s have a Region Address Translator (RAT) but no MMU */
857                 rproc->has_iommu = false;
858                 /* error recovery is not supported at present */
859                 rproc->recovery_disabled = true;
861                 kproc = rproc->priv;
862                 kproc->cluster = cluster;
863                 kproc->core = core;
864                 kproc->dev = cdev;
865                 kproc->rproc = rproc;
866                 core->rproc = rproc;
868                 ret = k3_r5_rproc_configure(kproc);
869                 if (ret) {
870                         dev_err(dev, "initial configure failed, ret = %d\n",
871                                 ret);
872                         goto err_config;
873                 }
875                 ret = k3_r5_reserved_mem_init(kproc);
876                 if (ret) {
877                         dev_err(dev, "reserved memory init failed, ret = %d\n",
878                                 ret);
879                         goto err_config;
880                 }
882                 ret = rproc_add(rproc);
883                 if (ret) {
884                         dev_err(dev, "rproc_add failed, ret = %d\n", ret);
885                         goto err_add;
886                 }
888                 /* create only one rproc in lockstep mode */
889                 if (cluster->mode)
890                         break;
891         }
893         return 0;
895 err_split:
896         rproc_del(rproc);
897 err_add:
898         k3_r5_reserved_mem_exit(kproc);
899 err_config:
900         rproc_free(rproc);
901         core->rproc = NULL;
902 out:
903         /* undo core0 upon any failures on core1 in split-mode */
904         if (!cluster->mode && core == core1) {
905                 core = list_prev_entry(core, elem);
906                 rproc = core->rproc;
907                 kproc = rproc->priv;
908                 goto err_split;
909         }
910         return ret;
913 static int k3_r5_cluster_rproc_exit(struct platform_device *pdev)
915         struct k3_r5_cluster *cluster = platform_get_drvdata(pdev);
916         struct k3_r5_rproc *kproc;
917         struct k3_r5_core *core;
918         struct rproc *rproc;
920         /*
921          * lockstep mode has only one rproc associated with first core, whereas
922          * split-mode has two rprocs associated with each core, and requires
923          * that core1 be powered down first
924          */
925         core = cluster->mode ?
926                 list_first_entry(&cluster->cores, struct k3_r5_core, elem) :
927                 list_last_entry(&cluster->cores, struct k3_r5_core, elem);
929         list_for_each_entry_from_reverse(core, &cluster->cores, elem) {
930                 rproc = core->rproc;
931                 kproc = rproc->priv;
933                 rproc_del(rproc);
935                 k3_r5_reserved_mem_exit(kproc);
937                 rproc_free(rproc);
938                 core->rproc = NULL;
939         }
941         return 0;
944 static int k3_r5_core_of_get_internal_memories(struct platform_device *pdev,
945                                                struct k3_r5_core *core)
947         static const char * const mem_names[] = {"atcm", "btcm"};
948         struct device *dev = &pdev->dev;
949         struct resource *res;
950         int num_mems;
951         int i, ret;
953         num_mems = ARRAY_SIZE(mem_names);
954         core->mem = devm_kcalloc(dev, num_mems, sizeof(*core->mem), GFP_KERNEL);
955         if (!core->mem)
956                 return -ENOMEM;
958         for (i = 0; i < num_mems; i++) {
959                 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
960                                                    mem_names[i]);
961                 if (!res) {
962                         dev_err(dev, "found no memory resource for %s\n",
963                                 mem_names[i]);
964                         ret = -EINVAL;
965                         goto fail;
966                 }
967                 if (!devm_request_mem_region(dev, res->start,
968                                              resource_size(res),
969                                              dev_name(dev))) {
970                         dev_err(dev, "could not request %s region for resource\n",
971                                 mem_names[i]);
972                         ret = -EBUSY;
973                         goto fail;
974                 }
976                 core->mem[i].cpu_addr = devm_ioremap_wc(dev, res->start,
977                                                         resource_size(res));
978                 if (IS_ERR(core->mem[i].cpu_addr)) {
979                         dev_err(dev, "failed to map %s memory\n", mem_names[i]);
980                         ret = PTR_ERR(core->mem[i].cpu_addr);
981                         devm_release_mem_region(dev, res->start,
982                                                 resource_size(res));
983                         goto fail;
984                 }
985                 core->mem[i].bus_addr = res->start;
987                 /*
988                  * TODO:
989                  * The R5F cores can place ATCM & BTCM anywhere in its address
990                  * based on the corresponding Region Registers in the System
991                  * Control coprocessor. For now, place ATCM and BTCM at
992                  * addresses 0 and 0x41010000 (same as the bus address on AM65x
993                  * SoCs) based on loczrama setting
994                  */
995                 if (!strcmp(mem_names[i], "atcm")) {
996                         core->mem[i].dev_addr = core->loczrama ?
997                                                         0 : K3_R5_TCM_DEV_ADDR;
998                 } else {
999                         core->mem[i].dev_addr = core->loczrama ?
1000                                                         K3_R5_TCM_DEV_ADDR : 0;
1001                 }
1002                 core->mem[i].size = resource_size(res);
1004                 dev_dbg(dev, "memory %8s: bus addr %pa size 0x%zx va %p da 0x%x\n",
1005                         mem_names[i], &core->mem[i].bus_addr,
1006                         core->mem[i].size, core->mem[i].cpu_addr,
1007                         core->mem[i].dev_addr);
1008         }
1009         core->num_mems = num_mems;
1011         return 0;
1013 fail:
1014         for (i--; i >= 0; i--) {
1015                 devm_release_mem_region(dev, core->mem[i].bus_addr,
1016                                         core->mem[i].size);
1017                 devm_iounmap(dev, core->mem[i].cpu_addr);
1018         }
1019         if (core->mem)
1020                 devm_kfree(dev, core->mem);
1021         return ret;
1024 static int k3_r5_core_of_get_sram_memories(struct platform_device *pdev,
1025                                            struct k3_r5_core *core)
1027         struct device_node *np = pdev->dev.of_node;
1028         struct device *dev = &pdev->dev;
1029         struct device_node *sram_np;
1030         struct resource res;
1031         int num_sram;
1032         int i, ret;
1034         num_sram = of_property_count_elems_of_size(np, "sram", sizeof(phandle));
1035         if (num_sram <= 0) {
1036                 dev_dbg(dev, "device does not use reserved on-chip memories, num_sram = %d\n",
1037                         num_sram);
1038                 return 0;
1039         }
1041         core->sram = kcalloc(num_sram, sizeof(*core->sram), GFP_KERNEL);
1042         if (!core->sram)
1043                 return -ENOMEM;
1045         for (i = 0; i < num_sram; i++) {
1046                 sram_np = of_parse_phandle(np, "sram", i);
1047                 if (!sram_np) {
1048                         ret = -EINVAL;
1049                         goto fail;
1050                 }
1052                 if (!of_device_is_available(sram_np)) {
1053                         of_node_put(sram_np);
1054                         ret = -EINVAL;
1055                         goto fail;
1056                 }
1058                 ret = of_address_to_resource(sram_np, 0, &res);
1059                 of_node_put(sram_np);
1060                 if (ret) {
1061                         ret = -EINVAL;
1062                         goto fail;
1063                 }
1064                 core->sram[i].bus_addr = res.start;
1065                 core->sram[i].dev_addr = res.start;
1066                 core->sram[i].size = resource_size(&res);
1067                 core->sram[i].cpu_addr = ioremap(res.start,
1068                                                  resource_size(&res));
1069                 if (!core->sram[i].cpu_addr) {
1070                         dev_err(dev, "failed to parse and map sram%d memory at %pad\n",
1071                                 i, &res.start);
1072                         ret = -ENOMEM;
1073                         goto fail;
1074                 }
1076                 dev_dbg(dev, "memory    sram%d: bus addr %pa size 0x%zx va %pK da 0x%x\n",
1077                         i, &core->sram[i].bus_addr,
1078                         core->sram[i].size, core->sram[i].cpu_addr,
1079                         core->sram[i].dev_addr);
1080         }
1081         core->num_sram = num_sram;
1083         return 0;
1085 fail:
1086         for (i--; i >= 0; i--) {
1087                 if (core->sram[i].cpu_addr)
1088                         iounmap(core->sram[i].cpu_addr);
1089         }
1090         kfree(core->sram);
1092         return ret;
1095 static
1096 struct ti_sci_proc *k3_r5_core_of_get_tsp(struct device *dev,
1097                                           const struct ti_sci_handle *sci)
1099         struct ti_sci_proc *tsp;
1100         u32 temp[2];
1101         int ret;
1103         ret = of_property_read_u32_array(dev->of_node, "ti,sci-proc-ids",
1104                                          temp, 2);
1105         if (ret < 0)
1106                 return ERR_PTR(ret);
1108         tsp = kzalloc(sizeof(*tsp), GFP_KERNEL);
1109         if (!tsp)
1110                 return ERR_PTR(-ENOMEM);
1112         tsp->dev = dev;
1113         tsp->sci = sci;
1114         tsp->ops = &sci->ops.proc_ops;
1115         tsp->proc_id = temp[0];
1116         tsp->host_id = temp[1];
1118         return tsp;
1121 static int k3_r5_core_of_init(struct platform_device *pdev)
1123         struct device *dev = &pdev->dev;
1124         struct device_node *np = dev->of_node;
1125         struct k3_r5_core *core;
1126         int ret, ret1, i;
1128         core = devm_kzalloc(dev, sizeof(*core), GFP_KERNEL);
1129         if (!core)
1130                 return -ENOMEM;
1132         core->dev = dev;
1133         core->atcm_enable = 0;
1134         core->btcm_enable = 1;
1135         core->loczrama = 1;
1137         ret = of_property_read_u32(np, "atcm-enable", &core->atcm_enable);
1138         if (ret < 0 && ret != -EINVAL) {
1139                 dev_err(dev, "invalid format for atcm-enable, ret = %d\n", ret);
1140                 goto err_of;
1141         }
1143         ret = of_property_read_u32(np, "btcm-enable", &core->btcm_enable);
1144         if (ret < 0 && ret != -EINVAL) {
1145                 dev_err(dev, "invalid format for btcm-enable, ret = %d\n", ret);
1146                 goto err_of;
1147         }
1149         ret = of_property_read_u32(np, "loczrama", &core->loczrama);
1150         if (ret < 0 && ret != -EINVAL) {
1151                 dev_err(dev, "invalid format for loczrama, ret = %d\n", ret);
1152                 goto err_of;
1153         }
1155         core->ti_sci = ti_sci_get_by_phandle(np, "ti,sci");
1156         if (IS_ERR(core->ti_sci)) {
1157                 ret = PTR_ERR(core->ti_sci);
1158                 if (ret != -EPROBE_DEFER) {
1159                         dev_err(dev, "failed to get ti-sci handle, ret = %d\n",
1160                                 ret);
1161                 }
1162                 core->ti_sci = NULL;
1163                 goto err_of;
1164         }
1166         ret = of_property_read_u32(np, "ti,sci-dev-id", &core->ti_sci_id);
1167         if (ret) {
1168                 dev_err(dev, "missing 'ti,sci-dev-id' property\n");
1169                 goto err_sci_id;
1170         }
1172         core->reset = reset_control_get_exclusive(dev, NULL);
1173         if (IS_ERR(core->reset)) {
1174                 ret = PTR_ERR(core->reset);
1175                 if (ret != -EPROBE_DEFER) {
1176                         dev_err(dev, "failed to get reset handle, ret = %d\n",
1177                                 ret);
1178                 }
1179                 goto err_sci_id;
1180         }
1182         core->tsp = k3_r5_core_of_get_tsp(dev, core->ti_sci);
1183         if (IS_ERR(core->tsp)) {
1184                 dev_err(dev, "failed to construct ti-sci proc control, ret = %d\n",
1185                         ret);
1186                 ret = PTR_ERR(core->tsp);
1187                 goto err_sci_proc;
1188         }
1190         ret = ti_sci_proc_request(core->tsp);
1191         if (ret < 0) {
1192                 dev_err(dev, "ti_sci_proc_request failed, ret = %d\n", ret);
1193                 goto err_proc;
1194         }
1196         ret = k3_r5_core_of_get_internal_memories(pdev, core);
1197         if (ret) {
1198                 dev_err(dev, "failed to get internal memories, ret = %d\n",
1199                         ret);
1200                 goto err_intmem;
1201         }
1203         ret = k3_r5_core_of_get_sram_memories(pdev, core);
1204         if (ret) {
1205                 dev_err(dev, "failed to get sram memories, ret = %d\n", ret);
1206                 goto err_sram;
1207         }
1209         platform_set_drvdata(pdev, core);
1211         return 0;
1213 err_sram:
1214         for (i = 0; i < core->num_mems; i++) {
1215                 devm_release_mem_region(dev, core->mem[i].bus_addr,
1216                                         core->mem[i].size);
1217                 devm_iounmap(dev, core->mem[i].cpu_addr);
1218         }
1219         devm_kfree(dev, core->mem);
1220 err_intmem:
1221         ret1 = ti_sci_proc_release(core->tsp);
1222         if (ret1)
1223                 dev_err(dev, "failed to release proc, ret1 = %d\n", ret1);
1224 err_proc:
1225         kfree(core->tsp);
1226 err_sci_proc:
1227         reset_control_put(core->reset);
1228 err_sci_id:
1229         ret1 = ti_sci_put_handle(core->ti_sci);
1230         if (ret1)
1231                 dev_err(dev, "failed to put ti_sci handle, ret = %d\n", ret1);
1232 err_of:
1233         devm_kfree(dev, core);
1234         return ret;
1237 /*
1238  * free the resources explicitly since driver model is not being used
1239  * for the child R5F devices
1240  */
1241 static int k3_r5_core_of_exit(struct platform_device *pdev)
1243         struct k3_r5_core *core = platform_get_drvdata(pdev);
1244         struct device *dev = &pdev->dev;
1245         int i, ret;
1247         for (i = 0; i < core->num_sram; i++)
1248                 iounmap(core->sram[i].cpu_addr);
1249         kfree(core->sram);
1251         for (i = 0; i < core->num_mems; i++) {
1252                 devm_release_mem_region(dev, core->mem[i].bus_addr,
1253                                         core->mem[i].size);
1254                 devm_iounmap(dev, core->mem[i].cpu_addr);
1255         }
1256         if (core->mem)
1257                 devm_kfree(dev, core->mem);
1259         ret = ti_sci_proc_release(core->tsp);
1260         if (ret)
1261                 dev_err(dev, "failed to release proc, ret = %d\n", ret);
1263         kfree(core->tsp);
1264         reset_control_put(core->reset);
1266         ret = ti_sci_put_handle(core->ti_sci);
1267         if (ret)
1268                 dev_err(dev, "failed to put ti_sci handle, ret = %d\n", ret);
1270         platform_set_drvdata(pdev, NULL);
1271         devm_kfree(dev, core);
1273         return ret;
1276 static int k3_r5_cluster_of_init(struct platform_device *pdev)
1278         struct k3_r5_cluster *cluster = platform_get_drvdata(pdev);
1279         struct device *dev = &pdev->dev;
1280         struct device_node *np = dev->of_node;
1281         struct platform_device *cpdev;
1282         struct device_node *child;
1283         struct k3_r5_core *core, *temp;
1284         int ret;
1286         for_each_available_child_of_node(np, child) {
1287                 cpdev = of_find_device_by_node(child);
1288                 if (!cpdev) {
1289                         ret = -ENODEV;
1290                         dev_err(dev, "could not get R5 core platform device\n");
1291                         goto fail;
1292                 }
1294                 ret = k3_r5_core_of_init(cpdev);
1295                 if (ret) {
1296                         dev_err(dev, "k3_r5_core_of_init failed, ret = %d\n",
1297                                 ret);
1298                         put_device(&cpdev->dev);
1299                         goto fail;
1300                 }
1302                 core = platform_get_drvdata(cpdev);
1303                 put_device(&cpdev->dev);
1304                 list_add_tail(&core->elem, &cluster->cores);
1305         }
1307         return 0;
1309 fail:
1310         list_for_each_entry_safe_reverse(core, temp, &cluster->cores, elem) {
1311                 list_del(&core->elem);
1312                 cpdev = to_platform_device(core->dev);
1313                 if (k3_r5_core_of_exit(cpdev))
1314                         dev_err(dev, "k3_r5_core_of_exit cleanup failed\n");
1315         }
1316         return ret;
1319 static int k3_r5_cluster_of_exit(struct platform_device *pdev)
1321         struct k3_r5_cluster *cluster = platform_get_drvdata(pdev);
1322         struct device *dev = &pdev->dev;
1323         struct platform_device *cpdev;
1324         struct k3_r5_core *core, *temp;
1325         int ret;
1327         list_for_each_entry_safe_reverse(core, temp, &cluster->cores, elem) {
1328                 list_del(&core->elem);
1329                 cpdev = to_platform_device(core->dev);
1330                 ret = k3_r5_core_of_exit(cpdev);
1331                 if (ret) {
1332                         dev_err(dev, "k3_r5_core_of_exit failed, ret = %d\n",
1333                                 ret);
1334                         break;
1335                 }
1336         }
1338         return ret;
1341 static int k3_r5_probe(struct platform_device *pdev)
1343         struct device *dev = &pdev->dev;
1344         struct device_node *np = dev->of_node;
1345         struct k3_r5_cluster *cluster;
1346         int ret, ret1;
1347         int num_cores;
1349         cluster = devm_kzalloc(dev, sizeof(*cluster), GFP_KERNEL);
1350         if (!cluster)
1351                 return -ENOMEM;
1353         cluster->dev = dev;
1354         cluster->mode = CLUSTER_MODE_LOCKSTEP;
1355         INIT_LIST_HEAD(&cluster->cores);
1357         ret = of_property_read_u32(np, "lockstep-mode", &cluster->mode);
1358         if (ret < 0 && ret != -EINVAL) {
1359                 dev_err(dev, "invalid format for lockstep-mode, ret = %d\n",
1360                         ret);
1361                 return ret;
1362         }
1364         num_cores = of_get_available_child_count(np);
1365         if (num_cores != 2) {
1366                 dev_err(dev, "MCU cluster requires both R5F cores to be enabled, num_cores = %d\n",
1367                         num_cores);
1368                 return -ENODEV;
1369         }
1371         platform_set_drvdata(pdev, cluster);
1373         dev_info(dev, "creating child devices for R5F cores\n");
1374         ret = of_platform_populate(np, NULL, NULL, dev);
1375         if (ret) {
1376                 dev_err(dev, "of_platform_populate failed, ret = %d\n", ret);
1377                 return ret;
1378         }
1380         ret = k3_r5_cluster_of_init(pdev);
1381         if (ret) {
1382                 dev_err(dev, "k3_r5_cluster_of_init failed, ret = %d\n", ret);
1383                 goto fail_of;
1384         }
1386         ret = k3_r5_cluster_rproc_init(pdev);
1387         if (ret) {
1388                 dev_err(dev, "k3_r5_cluster_rproc_init failed, ret = %d\n",
1389                         ret);
1390                 goto fail_rproc;
1391         }
1393         return 0;
1395 fail_rproc:
1396         ret1 = k3_r5_cluster_of_exit(pdev);
1397         if (ret1)
1398                 dev_err(dev, "k3_r5_cluster_of_exit failed, ret = %d\n", ret1);
1399 fail_of:
1400         of_platform_depopulate(dev);
1401         return ret;
1404 static int k3_r5_remove(struct platform_device *pdev)
1406         struct device *dev = &pdev->dev;
1407         int ret;
1409         ret = k3_r5_cluster_rproc_exit(pdev);
1410         if (ret) {
1411                 dev_err(dev, "k3_r5_cluster_rproc_exit failed, ret = %d\n",
1412                         ret);
1413                 goto fail;
1414         }
1416         ret = k3_r5_cluster_of_exit(pdev);
1417         if (ret) {
1418                 dev_err(dev, "k3_r5_cluster_of_exit failed, ret = %d\n", ret);
1419                 goto fail;
1420         }
1422         dev_info(dev, "removing child devices for R5F cores\n");
1423         of_platform_depopulate(dev);
1425 fail:
1426         return ret;
1429 static const struct k3_r5_rproc_dev_data am65x_r5f_dev_data[] = {
1430         {
1431                 .device_name    = "41000000.r5f",
1432                 .fw_name        = "am65x-mcu-r5f0_0-fw",
1433         },
1434         {
1435                 .device_name    = "41400000.r5f",
1436                 .fw_name        = "am65x-mcu-r5f0_1-fw",
1437         },
1438         {
1439                 /* sentinel */
1440         },
1441 };
1443 static const struct of_device_id k3_r5_of_match[] = {
1444         {
1445                 .compatible     = "ti,am654-r5fss",
1446                 .data           = am65x_r5f_dev_data,
1447         },
1448         { /* sentinel */ },
1449 };
1450 MODULE_DEVICE_TABLE(of, k3_r5_of_match);
1452 static struct platform_driver k3_r5_rproc_driver = {
1453         .probe = k3_r5_probe,
1454         .remove = k3_r5_remove,
1455         .driver = {
1456                 .name = "k3_r5_rproc",
1457                 .of_match_table = k3_r5_of_match,
1458         },
1459 };
1461 module_platform_driver(k3_r5_rproc_driver);
1463 MODULE_LICENSE("GPL v2");
1464 MODULE_DESCRIPTION("TI K3 R5F remote processor driver");
1465 MODULE_AUTHOR("Suman Anna <s-anna@ti.com>");