blob: 66a32dcdd7d091f4194ebd7f506dc13a638625fa [file] [log] [blame]
Suman Anna6dedbd12020-10-02 18:42:32 -05001// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * TI K3 R5F (MCU) Remote Processor driver
4 *
5 * Copyright (C) 2017-2020 Texas Instruments Incorporated - https://www.ti.com/
6 * Suman Anna <s-anna@ti.com>
7 */
8
9#include <linux/dma-mapping.h>
10#include <linux/err.h>
11#include <linux/interrupt.h>
12#include <linux/kernel.h>
13#include <linux/mailbox_client.h>
14#include <linux/module.h>
15#include <linux/of_address.h>
16#include <linux/of_device.h>
17#include <linux/of_reserved_mem.h>
18#include <linux/omap-mailbox.h>
19#include <linux/platform_device.h>
20#include <linux/pm_runtime.h>
21#include <linux/remoteproc.h>
22#include <linux/reset.h>
23#include <linux/slab.h>
24
25#include "omap_remoteproc.h"
26#include "remoteproc_internal.h"
27#include "ti_sci_proc.h"
28
29/* This address can either be for ATCM or BTCM with the other at address 0x0 */
30#define K3_R5_TCM_DEV_ADDR 0x41010000
31
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
Suman Anna7508ea12020-11-18 19:05:30 -060041/* Available from J7200 SoCs onwards */
42#define PROC_BOOT_CFG_FLAG_R5_MEM_INIT_DIS 0x00004000
Suman Anna6dedbd12020-10-02 18:42:32 -050043
44/* R5 TI-SCI Processor Control Flags */
45#define PROC_BOOT_CTRL_FLAG_R5_CORE_HALT 0x00000001
46
47/* R5 TI-SCI Processor Status Flags */
48#define PROC_BOOT_STATUS_FLAG_R5_WFE 0x00000001
49#define PROC_BOOT_STATUS_FLAG_R5_WFI 0x00000002
50#define PROC_BOOT_STATUS_FLAG_R5_CLK_GATED 0x00000004
51#define PROC_BOOT_STATUS_FLAG_R5_LOCKSTEP_PERMITTED 0x00000100
52
53/**
54 * struct k3_r5_mem - internal memory structure
55 * @cpu_addr: MPU virtual address of the memory region
56 * @bus_addr: Bus address used to access the memory region
57 * @dev_addr: Device address from remoteproc view
58 * @size: Size of the memory region
59 */
60struct k3_r5_mem {
61 void __iomem *cpu_addr;
62 phys_addr_t bus_addr;
63 u32 dev_addr;
64 size_t size;
65};
66
67enum cluster_mode {
68 CLUSTER_MODE_SPLIT = 0,
69 CLUSTER_MODE_LOCKSTEP,
70};
71
72/**
Suman Anna7508ea12020-11-18 19:05:30 -060073 * struct k3_r5_soc_data - match data to handle SoC variations
74 * @tcm_ecc_autoinit: flag to denote the auto-initialization of TCMs for ECC
75 */
76struct k3_r5_soc_data {
77 bool tcm_ecc_autoinit;
78};
79
80/**
Suman Anna6dedbd12020-10-02 18:42:32 -050081 * struct k3_r5_cluster - K3 R5F Cluster structure
82 * @dev: cached device pointer
83 * @mode: Mode to configure the Cluster - Split or LockStep
84 * @cores: list of R5 cores within the cluster
Suman Anna7508ea12020-11-18 19:05:30 -060085 * @soc_data: SoC-specific feature data for a R5FSS
Suman Anna6dedbd12020-10-02 18:42:32 -050086 */
87struct k3_r5_cluster {
88 struct device *dev;
89 enum cluster_mode mode;
90 struct list_head cores;
Suman Anna7508ea12020-11-18 19:05:30 -060091 const struct k3_r5_soc_data *soc_data;
Suman Anna6dedbd12020-10-02 18:42:32 -050092};
93
94/**
95 * struct k3_r5_core - K3 R5 core structure
96 * @elem: linked list item
97 * @dev: cached device pointer
98 * @rproc: rproc handle representing this core
99 * @mem: internal memory regions data
Suman Annaea47c682020-10-02 18:42:34 -0500100 * @sram: on-chip SRAM memory regions data
Suman Anna6dedbd12020-10-02 18:42:32 -0500101 * @num_mems: number of internal memory regions
Suman Annaea47c682020-10-02 18:42:34 -0500102 * @num_sram: number of on-chip SRAM memory regions
Suman Anna6dedbd12020-10-02 18:42:32 -0500103 * @reset: reset control handle
104 * @tsp: TI-SCI processor control handle
105 * @ti_sci: TI-SCI handle
106 * @ti_sci_id: TI-SCI device identifier
107 * @atcm_enable: flag to control ATCM enablement
108 * @btcm_enable: flag to control BTCM enablement
109 * @loczrama: flag to dictate which TCM is at device address 0x0
110 */
111struct k3_r5_core {
112 struct list_head elem;
113 struct device *dev;
114 struct rproc *rproc;
115 struct k3_r5_mem *mem;
Suman Annaea47c682020-10-02 18:42:34 -0500116 struct k3_r5_mem *sram;
Suman Anna6dedbd12020-10-02 18:42:32 -0500117 int num_mems;
Suman Annaea47c682020-10-02 18:42:34 -0500118 int num_sram;
Suman Anna6dedbd12020-10-02 18:42:32 -0500119 struct reset_control *reset;
120 struct ti_sci_proc *tsp;
121 const struct ti_sci_handle *ti_sci;
122 u32 ti_sci_id;
123 u32 atcm_enable;
124 u32 btcm_enable;
125 u32 loczrama;
126};
127
128/**
129 * struct k3_r5_rproc - K3 remote processor state
130 * @dev: cached device pointer
131 * @cluster: cached pointer to parent cluster structure
132 * @mbox: mailbox channel handle
133 * @client: mailbox client to request the mailbox channel
134 * @rproc: rproc handle
135 * @core: cached pointer to r5 core structure being used
136 * @rmem: reserved memory regions data
137 * @num_rmems: number of reserved memory regions
138 */
139struct k3_r5_rproc {
140 struct device *dev;
141 struct k3_r5_cluster *cluster;
142 struct mbox_chan *mbox;
143 struct mbox_client client;
144 struct rproc *rproc;
145 struct k3_r5_core *core;
146 struct k3_r5_mem *rmem;
147 int num_rmems;
148};
149
150/**
151 * k3_r5_rproc_mbox_callback() - inbound mailbox message handler
152 * @client: mailbox client pointer used for requesting the mailbox channel
153 * @data: mailbox payload
154 *
155 * This handler is invoked by the OMAP mailbox driver whenever a mailbox
156 * message is received. Usually, the mailbox payload simply contains
157 * the index of the virtqueue that is kicked by the remote processor,
158 * and we let remoteproc core handle it.
159 *
160 * In addition to virtqueue indices, we also have some out-of-band values
161 * that indicate different events. Those values are deliberately very
162 * large so they don't coincide with virtqueue indices.
163 */
164static void k3_r5_rproc_mbox_callback(struct mbox_client *client, void *data)
165{
166 struct k3_r5_rproc *kproc = container_of(client, struct k3_r5_rproc,
167 client);
168 struct device *dev = kproc->rproc->dev.parent;
169 const char *name = kproc->rproc->name;
170 u32 msg = omap_mbox_message(data);
171
172 dev_dbg(dev, "mbox msg: 0x%x\n", msg);
173
174 switch (msg) {
175 case RP_MBOX_CRASH:
176 /*
177 * remoteproc detected an exception, but error recovery is not
178 * supported. So, just log this for now
179 */
180 dev_err(dev, "K3 R5F rproc %s crashed\n", name);
181 break;
182 case RP_MBOX_ECHO_REPLY:
183 dev_info(dev, "received echo reply from %s\n", name);
184 break;
185 default:
186 /* silently handle all other valid messages */
187 if (msg >= RP_MBOX_READY && msg < RP_MBOX_END_MSG)
188 return;
189 if (msg > kproc->rproc->max_notifyid) {
190 dev_dbg(dev, "dropping unknown message 0x%x", msg);
191 return;
192 }
193 /* msg contains the index of the triggered vring */
194 if (rproc_vq_interrupt(kproc->rproc, msg) == IRQ_NONE)
195 dev_dbg(dev, "no message was found in vqid %d\n", msg);
196 }
197}
198
199/* kick a virtqueue */
200static void k3_r5_rproc_kick(struct rproc *rproc, int vqid)
201{
202 struct k3_r5_rproc *kproc = rproc->priv;
203 struct device *dev = rproc->dev.parent;
204 mbox_msg_t msg = (mbox_msg_t)vqid;
205 int ret;
206
207 /* send the index of the triggered virtqueue in the mailbox payload */
208 ret = mbox_send_message(kproc->mbox, (void *)msg);
209 if (ret < 0)
210 dev_err(dev, "failed to send mailbox message, status = %d\n",
211 ret);
212}
213
214static int k3_r5_split_reset(struct k3_r5_core *core)
215{
216 int ret;
217
218 ret = reset_control_assert(core->reset);
219 if (ret) {
220 dev_err(core->dev, "local-reset assert failed, ret = %d\n",
221 ret);
222 return ret;
223 }
224
225 ret = core->ti_sci->ops.dev_ops.put_device(core->ti_sci,
226 core->ti_sci_id);
227 if (ret) {
228 dev_err(core->dev, "module-reset assert failed, ret = %d\n",
229 ret);
230 if (reset_control_deassert(core->reset))
231 dev_warn(core->dev, "local-reset deassert back failed\n");
232 }
233
234 return ret;
235}
236
237static int k3_r5_split_release(struct k3_r5_core *core)
238{
239 int ret;
240
241 ret = core->ti_sci->ops.dev_ops.get_device(core->ti_sci,
242 core->ti_sci_id);
243 if (ret) {
244 dev_err(core->dev, "module-reset deassert failed, ret = %d\n",
245 ret);
246 return ret;
247 }
248
249 ret = reset_control_deassert(core->reset);
250 if (ret) {
251 dev_err(core->dev, "local-reset deassert failed, ret = %d\n",
252 ret);
253 if (core->ti_sci->ops.dev_ops.put_device(core->ti_sci,
254 core->ti_sci_id))
255 dev_warn(core->dev, "module-reset assert back failed\n");
256 }
257
258 return ret;
259}
260
261static int k3_r5_lockstep_reset(struct k3_r5_cluster *cluster)
262{
263 struct k3_r5_core *core;
264 int ret;
265
266 /* assert local reset on all applicable cores */
267 list_for_each_entry(core, &cluster->cores, elem) {
268 ret = reset_control_assert(core->reset);
269 if (ret) {
270 dev_err(core->dev, "local-reset assert failed, ret = %d\n",
271 ret);
272 core = list_prev_entry(core, elem);
273 goto unroll_local_reset;
274 }
275 }
276
277 /* disable PSC modules on all applicable cores */
278 list_for_each_entry(core, &cluster->cores, elem) {
279 ret = core->ti_sci->ops.dev_ops.put_device(core->ti_sci,
280 core->ti_sci_id);
281 if (ret) {
282 dev_err(core->dev, "module-reset assert failed, ret = %d\n",
283 ret);
284 goto unroll_module_reset;
285 }
286 }
287
288 return 0;
289
290unroll_module_reset:
291 list_for_each_entry_continue_reverse(core, &cluster->cores, elem) {
292 if (core->ti_sci->ops.dev_ops.put_device(core->ti_sci,
293 core->ti_sci_id))
294 dev_warn(core->dev, "module-reset assert back failed\n");
295 }
296 core = list_last_entry(&cluster->cores, struct k3_r5_core, elem);
297unroll_local_reset:
298 list_for_each_entry_from_reverse(core, &cluster->cores, elem) {
299 if (reset_control_deassert(core->reset))
300 dev_warn(core->dev, "local-reset deassert back failed\n");
301 }
302
303 return ret;
304}
305
306static int k3_r5_lockstep_release(struct k3_r5_cluster *cluster)
307{
308 struct k3_r5_core *core;
309 int ret;
310
311 /* enable PSC modules on all applicable cores */
312 list_for_each_entry_reverse(core, &cluster->cores, elem) {
313 ret = core->ti_sci->ops.dev_ops.get_device(core->ti_sci,
314 core->ti_sci_id);
315 if (ret) {
316 dev_err(core->dev, "module-reset deassert failed, ret = %d\n",
317 ret);
318 core = list_next_entry(core, elem);
319 goto unroll_module_reset;
320 }
321 }
322
323 /* deassert local reset on all applicable cores */
324 list_for_each_entry_reverse(core, &cluster->cores, elem) {
325 ret = reset_control_deassert(core->reset);
326 if (ret) {
327 dev_err(core->dev, "module-reset deassert failed, ret = %d\n",
328 ret);
329 goto unroll_local_reset;
330 }
331 }
332
333 return 0;
334
335unroll_local_reset:
336 list_for_each_entry_continue(core, &cluster->cores, elem) {
337 if (reset_control_assert(core->reset))
338 dev_warn(core->dev, "local-reset assert back failed\n");
339 }
340 core = list_first_entry(&cluster->cores, struct k3_r5_core, elem);
341unroll_module_reset:
342 list_for_each_entry_from(core, &cluster->cores, elem) {
343 if (core->ti_sci->ops.dev_ops.put_device(core->ti_sci,
344 core->ti_sci_id))
345 dev_warn(core->dev, "module-reset assert back failed\n");
346 }
347
348 return ret;
349}
350
351static inline int k3_r5_core_halt(struct k3_r5_core *core)
352{
353 return ti_sci_proc_set_control(core->tsp,
354 PROC_BOOT_CTRL_FLAG_R5_CORE_HALT, 0);
355}
356
357static inline int k3_r5_core_run(struct k3_r5_core *core)
358{
359 return ti_sci_proc_set_control(core->tsp,
360 0, PROC_BOOT_CTRL_FLAG_R5_CORE_HALT);
361}
362
363/*
364 * The R5F cores have controls for both a reset and a halt/run. The code
365 * execution from DDR requires the initial boot-strapping code to be run
366 * from the internal TCMs. This function is used to release the resets on
367 * applicable cores to allow loading into the TCMs. The .prepare() ops is
368 * invoked by remoteproc core before any firmware loading, and is followed
369 * by the .start() ops after loading to actually let the R5 cores run.
370 */
371static int k3_r5_rproc_prepare(struct rproc *rproc)
372{
373 struct k3_r5_rproc *kproc = rproc->priv;
374 struct k3_r5_cluster *cluster = kproc->cluster;
375 struct k3_r5_core *core = kproc->core;
376 struct device *dev = kproc->dev;
Suman Anna7508ea12020-11-18 19:05:30 -0600377 u32 ctrl = 0, cfg = 0, stat = 0;
378 u64 boot_vec = 0;
379 bool mem_init_dis;
Suman Anna6dedbd12020-10-02 18:42:32 -0500380 int ret;
381
Suman Anna7508ea12020-11-18 19:05:30 -0600382 ret = ti_sci_proc_get_status(core->tsp, &boot_vec, &cfg, &ctrl, &stat);
383 if (ret < 0)
384 return ret;
385 mem_init_dis = !!(cfg & PROC_BOOT_CFG_FLAG_R5_MEM_INIT_DIS);
386
Suman Anna6dedbd12020-10-02 18:42:32 -0500387 ret = (cluster->mode == CLUSTER_MODE_LOCKSTEP) ?
388 k3_r5_lockstep_release(cluster) : k3_r5_split_release(core);
Suman Anna34f26532020-10-02 18:42:33 -0500389 if (ret) {
Suman Anna6dedbd12020-10-02 18:42:32 -0500390 dev_err(dev, "unable to enable cores for TCM loading, ret = %d\n",
391 ret);
Suman Anna34f26532020-10-02 18:42:33 -0500392 return ret;
393 }
Suman Anna6dedbd12020-10-02 18:42:32 -0500394
Suman Anna34f26532020-10-02 18:42:33 -0500395 /*
Suman Anna7508ea12020-11-18 19:05:30 -0600396 * Newer IP revisions like on J7200 SoCs support h/w auto-initialization
397 * of TCMs, so there is no need to perform the s/w memzero. This bit is
398 * configurable through System Firmware, the default value does perform
399 * auto-init, but account for it in case it is disabled
400 */
401 if (cluster->soc_data->tcm_ecc_autoinit && !mem_init_dis) {
402 dev_dbg(dev, "leveraging h/w init for TCM memories\n");
403 return 0;
404 }
405
406 /*
Suman Anna34f26532020-10-02 18:42:33 -0500407 * Zero out both TCMs unconditionally (access from v8 Arm core is not
408 * affected by ATCM & BTCM enable configuration values) so that ECC
409 * can be effective on all TCM addresses.
410 */
411 dev_dbg(dev, "zeroing out ATCM memory\n");
412 memset(core->mem[0].cpu_addr, 0x00, core->mem[0].size);
413
414 dev_dbg(dev, "zeroing out BTCM memory\n");
415 memset(core->mem[1].cpu_addr, 0x00, core->mem[1].size);
416
417 return 0;
Suman Anna6dedbd12020-10-02 18:42:32 -0500418}
419
420/*
421 * This function implements the .unprepare() ops and performs the complimentary
422 * operations to that of the .prepare() ops. The function is used to assert the
423 * resets on all applicable cores for the rproc device (depending on LockStep
424 * or Split mode). This completes the second portion of powering down the R5F
425 * cores. The cores themselves are only halted in the .stop() ops, and the
426 * .unprepare() ops is invoked by the remoteproc core after the remoteproc is
427 * stopped.
428 */
429static int k3_r5_rproc_unprepare(struct rproc *rproc)
430{
431 struct k3_r5_rproc *kproc = rproc->priv;
432 struct k3_r5_cluster *cluster = kproc->cluster;
433 struct k3_r5_core *core = kproc->core;
434 struct device *dev = kproc->dev;
435 int ret;
436
437 ret = (cluster->mode == CLUSTER_MODE_LOCKSTEP) ?
438 k3_r5_lockstep_reset(cluster) : k3_r5_split_reset(core);
439 if (ret)
440 dev_err(dev, "unable to disable cores, ret = %d\n", ret);
441
442 return ret;
443}
444
445/*
446 * The R5F start sequence includes two different operations
447 * 1. Configure the boot vector for R5F core(s)
448 * 2. Unhalt/Run the R5F core(s)
449 *
450 * The sequence is different between LockStep and Split modes. The LockStep
451 * mode requires the boot vector to be configured only for Core0, and then
452 * unhalt both the cores to start the execution - Core1 needs to be unhalted
453 * first followed by Core0. The Split-mode requires that Core0 to be maintained
454 * always in a higher power state that Core1 (implying Core1 needs to be started
455 * always only after Core0 is started).
456 */
457static int k3_r5_rproc_start(struct rproc *rproc)
458{
459 struct k3_r5_rproc *kproc = rproc->priv;
460 struct k3_r5_cluster *cluster = kproc->cluster;
461 struct mbox_client *client = &kproc->client;
462 struct device *dev = kproc->dev;
463 struct k3_r5_core *core;
464 u32 boot_addr;
465 int ret;
466
467 client->dev = dev;
468 client->tx_done = NULL;
469 client->rx_callback = k3_r5_rproc_mbox_callback;
470 client->tx_block = false;
471 client->knows_txdone = false;
472
473 kproc->mbox = mbox_request_channel(client, 0);
474 if (IS_ERR(kproc->mbox)) {
475 ret = -EBUSY;
476 dev_err(dev, "mbox_request_channel failed: %ld\n",
477 PTR_ERR(kproc->mbox));
478 return ret;
479 }
480
481 /*
482 * Ping the remote processor, this is only for sanity-sake for now;
483 * there is no functional effect whatsoever.
484 *
485 * Note that the reply will _not_ arrive immediately: this message
486 * will wait in the mailbox fifo until the remote processor is booted.
487 */
488 ret = mbox_send_message(kproc->mbox, (void *)RP_MBOX_ECHO_REQUEST);
489 if (ret < 0) {
490 dev_err(dev, "mbox_send_message failed: %d\n", ret);
491 goto put_mbox;
492 }
493
494 boot_addr = rproc->bootaddr;
495 /* TODO: add boot_addr sanity checking */
496 dev_dbg(dev, "booting R5F core using boot addr = 0x%x\n", boot_addr);
497
498 /* boot vector need not be programmed for Core1 in LockStep mode */
499 core = kproc->core;
500 ret = ti_sci_proc_set_config(core->tsp, boot_addr, 0, 0);
501 if (ret)
502 goto put_mbox;
503
504 /* unhalt/run all applicable cores */
505 if (cluster->mode == CLUSTER_MODE_LOCKSTEP) {
506 list_for_each_entry_reverse(core, &cluster->cores, elem) {
507 ret = k3_r5_core_run(core);
508 if (ret)
509 goto unroll_core_run;
510 }
511 } else {
512 ret = k3_r5_core_run(core);
513 if (ret)
514 goto put_mbox;
515 }
516
517 return 0;
518
519unroll_core_run:
520 list_for_each_entry_continue(core, &cluster->cores, elem) {
521 if (k3_r5_core_halt(core))
522 dev_warn(core->dev, "core halt back failed\n");
523 }
524put_mbox:
525 mbox_free_channel(kproc->mbox);
526 return ret;
527}
528
529/*
530 * The R5F stop function includes the following operations
531 * 1. Halt R5F core(s)
532 *
533 * The sequence is different between LockStep and Split modes, and the order
534 * of cores the operations are performed are also in general reverse to that
535 * of the start function. The LockStep mode requires each operation to be
536 * performed first on Core0 followed by Core1. The Split-mode requires that
537 * Core0 to be maintained always in a higher power state that Core1 (implying
538 * Core1 needs to be stopped first before Core0).
539 *
540 * Note that the R5F halt operation in general is not effective when the R5F
541 * core is running, but is needed to make sure the core won't run after
542 * deasserting the reset the subsequent time. The asserting of reset can
543 * be done here, but is preferred to be done in the .unprepare() ops - this
544 * maintains the symmetric behavior between the .start(), .stop(), .prepare()
545 * and .unprepare() ops, and also balances them well between sysfs 'state'
546 * flow and device bind/unbind or module removal.
547 */
548static int k3_r5_rproc_stop(struct rproc *rproc)
549{
550 struct k3_r5_rproc *kproc = rproc->priv;
551 struct k3_r5_cluster *cluster = kproc->cluster;
552 struct k3_r5_core *core = kproc->core;
553 int ret;
554
555 /* halt all applicable cores */
556 if (cluster->mode == CLUSTER_MODE_LOCKSTEP) {
557 list_for_each_entry(core, &cluster->cores, elem) {
558 ret = k3_r5_core_halt(core);
559 if (ret) {
560 core = list_prev_entry(core, elem);
561 goto unroll_core_halt;
562 }
563 }
564 } else {
565 ret = k3_r5_core_halt(core);
566 if (ret)
567 goto out;
568 }
569
570 mbox_free_channel(kproc->mbox);
571
572 return 0;
573
574unroll_core_halt:
575 list_for_each_entry_from_reverse(core, &cluster->cores, elem) {
576 if (k3_r5_core_run(core))
577 dev_warn(core->dev, "core run back failed\n");
578 }
579out:
580 return ret;
581}
582
583/*
584 * Internal Memory translation helper
585 *
586 * Custom function implementing the rproc .da_to_va ops to provide address
587 * translation (device address to kernel virtual address) for internal RAMs
588 * present in a DSP or IPU device). The translated addresses can be used
589 * either by the remoteproc core for loading, or by any rpmsg bus drivers.
590 */
591static void *k3_r5_rproc_da_to_va(struct rproc *rproc, u64 da, size_t len)
592{
593 struct k3_r5_rproc *kproc = rproc->priv;
594 struct k3_r5_core *core = kproc->core;
595 void __iomem *va = NULL;
596 phys_addr_t bus_addr;
597 u32 dev_addr, offset;
598 size_t size;
599 int i;
600
601 if (len == 0)
602 return NULL;
603
604 /* handle both R5 and SoC views of ATCM and BTCM */
605 for (i = 0; i < core->num_mems; i++) {
606 bus_addr = core->mem[i].bus_addr;
607 dev_addr = core->mem[i].dev_addr;
608 size = core->mem[i].size;
609
610 /* handle R5-view addresses of TCMs */
611 if (da >= dev_addr && ((da + len) <= (dev_addr + size))) {
612 offset = da - dev_addr;
613 va = core->mem[i].cpu_addr + offset;
614 return (__force void *)va;
615 }
616
617 /* handle SoC-view addresses of TCMs */
618 if (da >= bus_addr && ((da + len) <= (bus_addr + size))) {
619 offset = da - bus_addr;
620 va = core->mem[i].cpu_addr + offset;
621 return (__force void *)va;
622 }
623 }
624
Suman Annaea47c682020-10-02 18:42:34 -0500625 /* handle any SRAM regions using SoC-view addresses */
626 for (i = 0; i < core->num_sram; i++) {
627 dev_addr = core->sram[i].dev_addr;
628 size = core->sram[i].size;
629
630 if (da >= dev_addr && ((da + len) <= (dev_addr + size))) {
631 offset = da - dev_addr;
632 va = core->sram[i].cpu_addr + offset;
633 return (__force void *)va;
634 }
635 }
636
Suman Anna6dedbd12020-10-02 18:42:32 -0500637 /* handle static DDR reserved memory regions */
638 for (i = 0; i < kproc->num_rmems; i++) {
639 dev_addr = kproc->rmem[i].dev_addr;
640 size = kproc->rmem[i].size;
641
642 if (da >= dev_addr && ((da + len) <= (dev_addr + size))) {
643 offset = da - dev_addr;
644 va = kproc->rmem[i].cpu_addr + offset;
645 return (__force void *)va;
646 }
647 }
648
649 return NULL;
650}
651
652static const struct rproc_ops k3_r5_rproc_ops = {
653 .prepare = k3_r5_rproc_prepare,
654 .unprepare = k3_r5_rproc_unprepare,
655 .start = k3_r5_rproc_start,
656 .stop = k3_r5_rproc_stop,
657 .kick = k3_r5_rproc_kick,
658 .da_to_va = k3_r5_rproc_da_to_va,
659};
660
661/*
662 * Internal R5F Core configuration
663 *
664 * Each R5FSS has a cluster-level setting for configuring the processor
665 * subsystem either in a safety/fault-tolerant LockStep mode or a performance
666 * oriented Split mode. Each R5F core has a number of settings to either
667 * enable/disable each of the TCMs, control which TCM appears at the R5F core's
668 * address 0x0. These settings need to be configured before the resets for the
669 * corresponding core are released. These settings are all protected and managed
670 * by the System Processor.
671 *
672 * This function is used to pre-configure these settings for each R5F core, and
673 * the configuration is all done through various ti_sci_proc functions that
674 * communicate with the System Processor. The function also ensures that both
675 * the cores are halted before the .prepare() step.
676 *
677 * The function is called from k3_r5_cluster_rproc_init() and is invoked either
678 * once (in LockStep mode) or twice (in Split mode). Support for LockStep-mode
679 * is dictated by an eFUSE register bit, and the config settings retrieved from
680 * DT are adjusted accordingly as per the permitted cluster mode. All cluster
681 * level settings like Cluster mode and TEINIT (exception handling state
682 * dictating ARM or Thumb mode) can only be set and retrieved using Core0.
683 *
684 * The function behavior is different based on the cluster mode. The R5F cores
685 * are configured independently as per their individual settings in Split mode.
686 * They are identically configured in LockStep mode using the primary Core0
687 * settings. However, some individual settings cannot be set in LockStep mode.
688 * This is overcome by switching to Split-mode initially and then programming
689 * both the cores with the same settings, before reconfiguing again for
690 * LockStep mode.
691 */
692static int k3_r5_rproc_configure(struct k3_r5_rproc *kproc)
693{
694 struct k3_r5_cluster *cluster = kproc->cluster;
695 struct device *dev = kproc->dev;
696 struct k3_r5_core *core0, *core, *temp;
697 u32 ctrl = 0, cfg = 0, stat = 0;
698 u32 set_cfg = 0, clr_cfg = 0;
699 u64 boot_vec = 0;
700 bool lockstep_en;
701 int ret;
702
703 core0 = list_first_entry(&cluster->cores, struct k3_r5_core, elem);
704 core = (cluster->mode == CLUSTER_MODE_LOCKSTEP) ? core0 : kproc->core;
705
706 ret = ti_sci_proc_get_status(core->tsp, &boot_vec, &cfg, &ctrl,
707 &stat);
708 if (ret < 0)
709 return ret;
710
711 dev_dbg(dev, "boot_vector = 0x%llx, cfg = 0x%x ctrl = 0x%x stat = 0x%x\n",
712 boot_vec, cfg, ctrl, stat);
713
714 lockstep_en = !!(stat & PROC_BOOT_STATUS_FLAG_R5_LOCKSTEP_PERMITTED);
715 if (!lockstep_en && cluster->mode == CLUSTER_MODE_LOCKSTEP) {
716 dev_err(cluster->dev, "lockstep mode not permitted, force configuring for split-mode\n");
717 cluster->mode = CLUSTER_MODE_SPLIT;
718 }
719
720 /* always enable ARM mode and set boot vector to 0 */
721 boot_vec = 0x0;
722 if (core == core0) {
723 clr_cfg = PROC_BOOT_CFG_FLAG_R5_TEINIT;
724 /*
725 * LockStep configuration bit is Read-only on Split-mode _only_
726 * devices and system firmware will NACK any requests with the
727 * bit configured, so program it only on permitted devices
728 */
729 if (lockstep_en)
730 clr_cfg |= PROC_BOOT_CFG_FLAG_R5_LOCKSTEP;
731 }
732
733 if (core->atcm_enable)
734 set_cfg |= PROC_BOOT_CFG_FLAG_R5_ATCM_EN;
735 else
736 clr_cfg |= PROC_BOOT_CFG_FLAG_R5_ATCM_EN;
737
738 if (core->btcm_enable)
739 set_cfg |= PROC_BOOT_CFG_FLAG_R5_BTCM_EN;
740 else
741 clr_cfg |= PROC_BOOT_CFG_FLAG_R5_BTCM_EN;
742
743 if (core->loczrama)
744 set_cfg |= PROC_BOOT_CFG_FLAG_R5_TCM_RSTBASE;
745 else
746 clr_cfg |= PROC_BOOT_CFG_FLAG_R5_TCM_RSTBASE;
747
748 if (cluster->mode == CLUSTER_MODE_LOCKSTEP) {
749 /*
750 * work around system firmware limitations to make sure both
751 * cores are programmed symmetrically in LockStep. LockStep
752 * and TEINIT config is only allowed with Core0.
753 */
754 list_for_each_entry(temp, &cluster->cores, elem) {
755 ret = k3_r5_core_halt(temp);
756 if (ret)
757 goto out;
758
759 if (temp != core) {
760 clr_cfg &= ~PROC_BOOT_CFG_FLAG_R5_LOCKSTEP;
761 clr_cfg &= ~PROC_BOOT_CFG_FLAG_R5_TEINIT;
762 }
763 ret = ti_sci_proc_set_config(temp->tsp, boot_vec,
764 set_cfg, clr_cfg);
765 if (ret)
766 goto out;
767 }
768
769 set_cfg = PROC_BOOT_CFG_FLAG_R5_LOCKSTEP;
770 clr_cfg = 0;
771 ret = ti_sci_proc_set_config(core->tsp, boot_vec,
772 set_cfg, clr_cfg);
773 } else {
774 ret = k3_r5_core_halt(core);
775 if (ret)
776 goto out;
777
778 ret = ti_sci_proc_set_config(core->tsp, boot_vec,
779 set_cfg, clr_cfg);
780 }
781
782out:
783 return ret;
784}
785
786static int k3_r5_reserved_mem_init(struct k3_r5_rproc *kproc)
787{
788 struct device *dev = kproc->dev;
789 struct device_node *np = dev_of_node(dev);
790 struct device_node *rmem_np;
791 struct reserved_mem *rmem;
792 int num_rmems;
793 int ret, i;
794
795 num_rmems = of_property_count_elems_of_size(np, "memory-region",
796 sizeof(phandle));
797 if (num_rmems <= 0) {
798 dev_err(dev, "device does not have reserved memory regions, ret = %d\n",
799 num_rmems);
800 return -EINVAL;
801 }
802 if (num_rmems < 2) {
803 dev_err(dev, "device needs atleast two memory regions to be defined, num = %d\n",
804 num_rmems);
805 return -EINVAL;
806 }
807
808 /* use reserved memory region 0 for vring DMA allocations */
809 ret = of_reserved_mem_device_init_by_idx(dev, np, 0);
810 if (ret) {
811 dev_err(dev, "device cannot initialize DMA pool, ret = %d\n",
812 ret);
813 return ret;
814 }
815
816 num_rmems--;
817 kproc->rmem = kcalloc(num_rmems, sizeof(*kproc->rmem), GFP_KERNEL);
818 if (!kproc->rmem) {
819 ret = -ENOMEM;
820 goto release_rmem;
821 }
822
823 /* use remaining reserved memory regions for static carveouts */
824 for (i = 0; i < num_rmems; i++) {
825 rmem_np = of_parse_phandle(np, "memory-region", i + 1);
826 if (!rmem_np) {
827 ret = -EINVAL;
828 goto unmap_rmem;
829 }
830
831 rmem = of_reserved_mem_lookup(rmem_np);
832 if (!rmem) {
833 of_node_put(rmem_np);
834 ret = -EINVAL;
835 goto unmap_rmem;
836 }
837 of_node_put(rmem_np);
838
839 kproc->rmem[i].bus_addr = rmem->base;
840 /*
841 * R5Fs do not have an MMU, but have a Region Address Translator
842 * (RAT) module that provides a fixed entry translation between
843 * the 32-bit processor addresses to 64-bit bus addresses. The
844 * RAT is programmable only by the R5F cores. Support for RAT
845 * is currently not supported, so 64-bit address regions are not
846 * supported. The absence of MMUs implies that the R5F device
847 * addresses/supported memory regions are restricted to 32-bit
848 * bus addresses, and are identical
849 */
850 kproc->rmem[i].dev_addr = (u32)rmem->base;
851 kproc->rmem[i].size = rmem->size;
852 kproc->rmem[i].cpu_addr = ioremap_wc(rmem->base, rmem->size);
853 if (!kproc->rmem[i].cpu_addr) {
854 dev_err(dev, "failed to map reserved memory#%d at %pa of size %pa\n",
855 i + 1, &rmem->base, &rmem->size);
856 ret = -ENOMEM;
857 goto unmap_rmem;
858 }
859
860 dev_dbg(dev, "reserved memory%d: bus addr %pa size 0x%zx va %pK da 0x%x\n",
861 i + 1, &kproc->rmem[i].bus_addr,
862 kproc->rmem[i].size, kproc->rmem[i].cpu_addr,
863 kproc->rmem[i].dev_addr);
864 }
865 kproc->num_rmems = num_rmems;
866
867 return 0;
868
869unmap_rmem:
870 for (i--; i >= 0; i--)
871 iounmap(kproc->rmem[i].cpu_addr);
872 kfree(kproc->rmem);
873release_rmem:
874 of_reserved_mem_device_release(dev);
875 return ret;
876}
877
878static void k3_r5_reserved_mem_exit(struct k3_r5_rproc *kproc)
879{
880 int i;
881
882 for (i = 0; i < kproc->num_rmems; i++)
883 iounmap(kproc->rmem[i].cpu_addr);
884 kfree(kproc->rmem);
885
886 of_reserved_mem_device_release(kproc->dev);
887}
888
889static int k3_r5_cluster_rproc_init(struct platform_device *pdev)
890{
891 struct k3_r5_cluster *cluster = platform_get_drvdata(pdev);
892 struct device *dev = &pdev->dev;
893 struct k3_r5_rproc *kproc;
894 struct k3_r5_core *core, *core1;
895 struct device *cdev;
896 const char *fw_name;
897 struct rproc *rproc;
898 int ret;
899
900 core1 = list_last_entry(&cluster->cores, struct k3_r5_core, elem);
901 list_for_each_entry(core, &cluster->cores, elem) {
902 cdev = core->dev;
903 ret = rproc_of_parse_firmware(cdev, 0, &fw_name);
904 if (ret) {
905 dev_err(dev, "failed to parse firmware-name property, ret = %d\n",
906 ret);
907 goto out;
908 }
909
910 rproc = rproc_alloc(cdev, dev_name(cdev), &k3_r5_rproc_ops,
911 fw_name, sizeof(*kproc));
912 if (!rproc) {
913 ret = -ENOMEM;
914 goto out;
915 }
916
917 /* K3 R5s have a Region Address Translator (RAT) but no MMU */
918 rproc->has_iommu = false;
919 /* error recovery is not supported at present */
920 rproc->recovery_disabled = true;
921
922 kproc = rproc->priv;
923 kproc->cluster = cluster;
924 kproc->core = core;
925 kproc->dev = cdev;
926 kproc->rproc = rproc;
927 core->rproc = rproc;
928
929 ret = k3_r5_rproc_configure(kproc);
930 if (ret) {
931 dev_err(dev, "initial configure failed, ret = %d\n",
932 ret);
933 goto err_config;
934 }
935
936 ret = k3_r5_reserved_mem_init(kproc);
937 if (ret) {
938 dev_err(dev, "reserved memory init failed, ret = %d\n",
939 ret);
940 goto err_config;
941 }
942
943 ret = rproc_add(rproc);
944 if (ret) {
945 dev_err(dev, "rproc_add failed, ret = %d\n", ret);
946 goto err_add;
947 }
948
949 /* create only one rproc in lockstep mode */
950 if (cluster->mode == CLUSTER_MODE_LOCKSTEP)
951 break;
952 }
953
954 return 0;
955
956err_split:
957 rproc_del(rproc);
958err_add:
959 k3_r5_reserved_mem_exit(kproc);
960err_config:
961 rproc_free(rproc);
962 core->rproc = NULL;
963out:
964 /* undo core0 upon any failures on core1 in split-mode */
965 if (cluster->mode == CLUSTER_MODE_SPLIT && core == core1) {
966 core = list_prev_entry(core, elem);
967 rproc = core->rproc;
968 kproc = rproc->priv;
969 goto err_split;
970 }
971 return ret;
972}
973
Arnd Bergmann23168222020-10-26 17:05:23 +0100974static void k3_r5_cluster_rproc_exit(void *data)
Suman Anna6dedbd12020-10-02 18:42:32 -0500975{
Arnd Bergmann23168222020-10-26 17:05:23 +0100976 struct k3_r5_cluster *cluster = platform_get_drvdata(data);
Suman Anna6dedbd12020-10-02 18:42:32 -0500977 struct k3_r5_rproc *kproc;
978 struct k3_r5_core *core;
979 struct rproc *rproc;
980
981 /*
982 * lockstep mode has only one rproc associated with first core, whereas
983 * split-mode has two rprocs associated with each core, and requires
984 * that core1 be powered down first
985 */
986 core = (cluster->mode == CLUSTER_MODE_LOCKSTEP) ?
987 list_first_entry(&cluster->cores, struct k3_r5_core, elem) :
988 list_last_entry(&cluster->cores, struct k3_r5_core, elem);
989
990 list_for_each_entry_from_reverse(core, &cluster->cores, elem) {
991 rproc = core->rproc;
992 kproc = rproc->priv;
993
994 rproc_del(rproc);
995
996 k3_r5_reserved_mem_exit(kproc);
997
998 rproc_free(rproc);
999 core->rproc = NULL;
1000 }
Suman Anna6dedbd12020-10-02 18:42:32 -05001001}
1002
1003static int k3_r5_core_of_get_internal_memories(struct platform_device *pdev,
1004 struct k3_r5_core *core)
1005{
1006 static const char * const mem_names[] = {"atcm", "btcm"};
1007 struct device *dev = &pdev->dev;
1008 struct resource *res;
1009 int num_mems;
1010 int i;
1011
1012 num_mems = ARRAY_SIZE(mem_names);
1013 core->mem = devm_kcalloc(dev, num_mems, sizeof(*core->mem), GFP_KERNEL);
1014 if (!core->mem)
1015 return -ENOMEM;
1016
1017 for (i = 0; i < num_mems; i++) {
1018 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1019 mem_names[i]);
1020 if (!res) {
1021 dev_err(dev, "found no memory resource for %s\n",
1022 mem_names[i]);
1023 return -EINVAL;
1024 }
1025 if (!devm_request_mem_region(dev, res->start,
1026 resource_size(res),
1027 dev_name(dev))) {
1028 dev_err(dev, "could not request %s region for resource\n",
1029 mem_names[i]);
1030 return -EBUSY;
1031 }
1032
1033 /*
1034 * TCMs are designed in general to support RAM-like backing
1035 * memories. So, map these as Normal Non-Cached memories. This
1036 * also avoids/fixes any potential alignment faults due to
1037 * unaligned data accesses when using memcpy() or memset()
1038 * functions (normally seen with device type memory).
1039 */
1040 core->mem[i].cpu_addr = devm_ioremap_wc(dev, res->start,
1041 resource_size(res));
1042 if (!core->mem[i].cpu_addr) {
1043 dev_err(dev, "failed to map %s memory\n", mem_names[i]);
1044 return -ENOMEM;
1045 }
1046 core->mem[i].bus_addr = res->start;
1047
1048 /*
1049 * TODO:
1050 * The R5F cores can place ATCM & BTCM anywhere in its address
1051 * based on the corresponding Region Registers in the System
1052 * Control coprocessor. For now, place ATCM and BTCM at
1053 * addresses 0 and 0x41010000 (same as the bus address on AM65x
1054 * SoCs) based on loczrama setting
1055 */
1056 if (!strcmp(mem_names[i], "atcm")) {
1057 core->mem[i].dev_addr = core->loczrama ?
1058 0 : K3_R5_TCM_DEV_ADDR;
1059 } else {
1060 core->mem[i].dev_addr = core->loczrama ?
1061 K3_R5_TCM_DEV_ADDR : 0;
1062 }
1063 core->mem[i].size = resource_size(res);
1064
1065 dev_dbg(dev, "memory %5s: bus addr %pa size 0x%zx va %pK da 0x%x\n",
1066 mem_names[i], &core->mem[i].bus_addr,
1067 core->mem[i].size, core->mem[i].cpu_addr,
1068 core->mem[i].dev_addr);
1069 }
1070 core->num_mems = num_mems;
1071
1072 return 0;
1073}
1074
Suman Annaea47c682020-10-02 18:42:34 -05001075static int k3_r5_core_of_get_sram_memories(struct platform_device *pdev,
1076 struct k3_r5_core *core)
1077{
1078 struct device_node *np = pdev->dev.of_node;
1079 struct device *dev = &pdev->dev;
1080 struct device_node *sram_np;
1081 struct resource res;
1082 int num_sram;
1083 int i, ret;
1084
1085 num_sram = of_property_count_elems_of_size(np, "sram", sizeof(phandle));
1086 if (num_sram <= 0) {
1087 dev_dbg(dev, "device does not use reserved on-chip memories, num_sram = %d\n",
1088 num_sram);
1089 return 0;
1090 }
1091
1092 core->sram = devm_kcalloc(dev, num_sram, sizeof(*core->sram), GFP_KERNEL);
1093 if (!core->sram)
1094 return -ENOMEM;
1095
1096 for (i = 0; i < num_sram; i++) {
1097 sram_np = of_parse_phandle(np, "sram", i);
1098 if (!sram_np)
1099 return -EINVAL;
1100
1101 if (!of_device_is_available(sram_np)) {
1102 of_node_put(sram_np);
1103 return -EINVAL;
1104 }
1105
1106 ret = of_address_to_resource(sram_np, 0, &res);
1107 of_node_put(sram_np);
1108 if (ret)
1109 return -EINVAL;
1110
1111 core->sram[i].bus_addr = res.start;
1112 core->sram[i].dev_addr = res.start;
1113 core->sram[i].size = resource_size(&res);
1114 core->sram[i].cpu_addr = devm_ioremap_wc(dev, res.start,
1115 resource_size(&res));
1116 if (!core->sram[i].cpu_addr) {
1117 dev_err(dev, "failed to parse and map sram%d memory at %pad\n",
1118 i, &res.start);
1119 return -ENOMEM;
1120 }
1121
1122 dev_dbg(dev, "memory sram%d: bus addr %pa size 0x%zx va %pK da 0x%x\n",
1123 i, &core->sram[i].bus_addr,
1124 core->sram[i].size, core->sram[i].cpu_addr,
1125 core->sram[i].dev_addr);
1126 }
1127 core->num_sram = num_sram;
1128
1129 return 0;
1130}
1131
Suman Anna6dedbd12020-10-02 18:42:32 -05001132static
1133struct ti_sci_proc *k3_r5_core_of_get_tsp(struct device *dev,
1134 const struct ti_sci_handle *sci)
1135{
1136 struct ti_sci_proc *tsp;
1137 u32 temp[2];
1138 int ret;
1139
1140 ret = of_property_read_u32_array(dev_of_node(dev), "ti,sci-proc-ids",
1141 temp, 2);
1142 if (ret < 0)
1143 return ERR_PTR(ret);
1144
1145 tsp = devm_kzalloc(dev, sizeof(*tsp), GFP_KERNEL);
1146 if (!tsp)
1147 return ERR_PTR(-ENOMEM);
1148
1149 tsp->dev = dev;
1150 tsp->sci = sci;
1151 tsp->ops = &sci->ops.proc_ops;
1152 tsp->proc_id = temp[0];
1153 tsp->host_id = temp[1];
1154
1155 return tsp;
1156}
1157
1158static int k3_r5_core_of_init(struct platform_device *pdev)
1159{
1160 struct device *dev = &pdev->dev;
1161 struct device_node *np = dev_of_node(dev);
1162 struct k3_r5_core *core;
1163 int ret;
1164
1165 if (!devres_open_group(dev, k3_r5_core_of_init, GFP_KERNEL))
1166 return -ENOMEM;
1167
1168 core = devm_kzalloc(dev, sizeof(*core), GFP_KERNEL);
1169 if (!core) {
1170 ret = -ENOMEM;
1171 goto err;
1172 }
1173
1174 core->dev = dev;
1175 /*
1176 * Use SoC Power-on-Reset values as default if no DT properties are
1177 * used to dictate the TCM configurations
1178 */
1179 core->atcm_enable = 0;
1180 core->btcm_enable = 1;
1181 core->loczrama = 1;
1182
1183 ret = of_property_read_u32(np, "ti,atcm-enable", &core->atcm_enable);
1184 if (ret < 0 && ret != -EINVAL) {
1185 dev_err(dev, "invalid format for ti,atcm-enable, ret = %d\n",
1186 ret);
1187 goto err;
1188 }
1189
1190 ret = of_property_read_u32(np, "ti,btcm-enable", &core->btcm_enable);
1191 if (ret < 0 && ret != -EINVAL) {
1192 dev_err(dev, "invalid format for ti,btcm-enable, ret = %d\n",
1193 ret);
1194 goto err;
1195 }
1196
1197 ret = of_property_read_u32(np, "ti,loczrama", &core->loczrama);
1198 if (ret < 0 && ret != -EINVAL) {
1199 dev_err(dev, "invalid format for ti,loczrama, ret = %d\n", ret);
1200 goto err;
1201 }
1202
1203 core->ti_sci = devm_ti_sci_get_by_phandle(dev, "ti,sci");
1204 if (IS_ERR(core->ti_sci)) {
1205 ret = PTR_ERR(core->ti_sci);
1206 if (ret != -EPROBE_DEFER) {
1207 dev_err(dev, "failed to get ti-sci handle, ret = %d\n",
1208 ret);
1209 }
1210 core->ti_sci = NULL;
1211 goto err;
1212 }
1213
1214 ret = of_property_read_u32(np, "ti,sci-dev-id", &core->ti_sci_id);
1215 if (ret) {
1216 dev_err(dev, "missing 'ti,sci-dev-id' property\n");
1217 goto err;
1218 }
1219
1220 core->reset = devm_reset_control_get_exclusive(dev, NULL);
1221 if (IS_ERR_OR_NULL(core->reset)) {
1222 ret = PTR_ERR_OR_ZERO(core->reset);
1223 if (!ret)
1224 ret = -ENODEV;
1225 if (ret != -EPROBE_DEFER) {
1226 dev_err(dev, "failed to get reset handle, ret = %d\n",
1227 ret);
1228 }
1229 goto err;
1230 }
1231
1232 core->tsp = k3_r5_core_of_get_tsp(dev, core->ti_sci);
1233 if (IS_ERR(core->tsp)) {
1234 dev_err(dev, "failed to construct ti-sci proc control, ret = %d\n",
1235 ret);
1236 ret = PTR_ERR(core->tsp);
1237 goto err;
1238 }
1239
1240 ret = k3_r5_core_of_get_internal_memories(pdev, core);
1241 if (ret) {
1242 dev_err(dev, "failed to get internal memories, ret = %d\n",
1243 ret);
1244 goto err;
1245 }
1246
Suman Annaea47c682020-10-02 18:42:34 -05001247 ret = k3_r5_core_of_get_sram_memories(pdev, core);
1248 if (ret) {
1249 dev_err(dev, "failed to get sram memories, ret = %d\n", ret);
1250 goto err;
1251 }
1252
Suman Anna6dedbd12020-10-02 18:42:32 -05001253 ret = ti_sci_proc_request(core->tsp);
1254 if (ret < 0) {
1255 dev_err(dev, "ti_sci_proc_request failed, ret = %d\n", ret);
1256 goto err;
1257 }
1258
1259 platform_set_drvdata(pdev, core);
1260 devres_close_group(dev, k3_r5_core_of_init);
1261
1262 return 0;
1263
1264err:
1265 devres_release_group(dev, k3_r5_core_of_init);
1266 return ret;
1267}
1268
1269/*
1270 * free the resources explicitly since driver model is not being used
1271 * for the child R5F devices
1272 */
1273static void k3_r5_core_of_exit(struct platform_device *pdev)
1274{
1275 struct k3_r5_core *core = platform_get_drvdata(pdev);
1276 struct device *dev = &pdev->dev;
1277 int ret;
1278
1279 ret = ti_sci_proc_release(core->tsp);
1280 if (ret)
1281 dev_err(dev, "failed to release proc, ret = %d\n", ret);
1282
1283 platform_set_drvdata(pdev, NULL);
1284 devres_release_group(dev, k3_r5_core_of_init);
1285}
1286
Arnd Bergmann23168222020-10-26 17:05:23 +01001287static void k3_r5_cluster_of_exit(void *data)
Suman Anna6dedbd12020-10-02 18:42:32 -05001288{
Arnd Bergmann23168222020-10-26 17:05:23 +01001289 struct k3_r5_cluster *cluster = platform_get_drvdata(data);
Suman Anna6dedbd12020-10-02 18:42:32 -05001290 struct platform_device *cpdev;
1291 struct k3_r5_core *core, *temp;
1292
1293 list_for_each_entry_safe_reverse(core, temp, &cluster->cores, elem) {
1294 list_del(&core->elem);
1295 cpdev = to_platform_device(core->dev);
1296 k3_r5_core_of_exit(cpdev);
1297 }
1298}
1299
1300static int k3_r5_cluster_of_init(struct platform_device *pdev)
1301{
1302 struct k3_r5_cluster *cluster = platform_get_drvdata(pdev);
1303 struct device *dev = &pdev->dev;
1304 struct device_node *np = dev_of_node(dev);
1305 struct platform_device *cpdev;
1306 struct device_node *child;
1307 struct k3_r5_core *core;
1308 int ret;
1309
1310 for_each_available_child_of_node(np, child) {
1311 cpdev = of_find_device_by_node(child);
1312 if (!cpdev) {
1313 ret = -ENODEV;
1314 dev_err(dev, "could not get R5 core platform device\n");
1315 goto fail;
1316 }
1317
1318 ret = k3_r5_core_of_init(cpdev);
1319 if (ret) {
1320 dev_err(dev, "k3_r5_core_of_init failed, ret = %d\n",
1321 ret);
1322 put_device(&cpdev->dev);
1323 goto fail;
1324 }
1325
1326 core = platform_get_drvdata(cpdev);
1327 put_device(&cpdev->dev);
1328 list_add_tail(&core->elem, &cluster->cores);
1329 }
1330
1331 return 0;
1332
1333fail:
1334 k3_r5_cluster_of_exit(pdev);
1335 return ret;
1336}
1337
1338static int k3_r5_probe(struct platform_device *pdev)
1339{
1340 struct device *dev = &pdev->dev;
1341 struct device_node *np = dev_of_node(dev);
1342 struct k3_r5_cluster *cluster;
Suman Anna7508ea12020-11-18 19:05:30 -06001343 const struct k3_r5_soc_data *data;
Suman Anna6dedbd12020-10-02 18:42:32 -05001344 int ret;
1345 int num_cores;
1346
Suman Anna7508ea12020-11-18 19:05:30 -06001347 data = of_device_get_match_data(&pdev->dev);
1348 if (!data) {
1349 dev_err(dev, "SoC-specific data is not defined\n");
1350 return -ENODEV;
1351 }
1352
Suman Anna6dedbd12020-10-02 18:42:32 -05001353 cluster = devm_kzalloc(dev, sizeof(*cluster), GFP_KERNEL);
1354 if (!cluster)
1355 return -ENOMEM;
1356
1357 cluster->dev = dev;
1358 cluster->mode = CLUSTER_MODE_LOCKSTEP;
Suman Anna7508ea12020-11-18 19:05:30 -06001359 cluster->soc_data = data;
Suman Anna6dedbd12020-10-02 18:42:32 -05001360 INIT_LIST_HEAD(&cluster->cores);
1361
1362 ret = of_property_read_u32(np, "ti,cluster-mode", &cluster->mode);
1363 if (ret < 0 && ret != -EINVAL) {
1364 dev_err(dev, "invalid format for ti,cluster-mode, ret = %d\n",
1365 ret);
1366 return ret;
1367 }
1368
1369 num_cores = of_get_available_child_count(np);
1370 if (num_cores != 2) {
1371 dev_err(dev, "MCU cluster requires both R5F cores to be enabled, num_cores = %d\n",
1372 num_cores);
1373 return -ENODEV;
1374 }
1375
1376 platform_set_drvdata(pdev, cluster);
1377
1378 ret = devm_of_platform_populate(dev);
1379 if (ret) {
1380 dev_err(dev, "devm_of_platform_populate failed, ret = %d\n",
1381 ret);
1382 return ret;
1383 }
1384
1385 ret = k3_r5_cluster_of_init(pdev);
1386 if (ret) {
1387 dev_err(dev, "k3_r5_cluster_of_init failed, ret = %d\n", ret);
1388 return ret;
1389 }
1390
Arnd Bergmann23168222020-10-26 17:05:23 +01001391 ret = devm_add_action_or_reset(dev, k3_r5_cluster_of_exit, pdev);
Suman Anna6dedbd12020-10-02 18:42:32 -05001392 if (ret)
1393 return ret;
1394
1395 ret = k3_r5_cluster_rproc_init(pdev);
1396 if (ret) {
1397 dev_err(dev, "k3_r5_cluster_rproc_init failed, ret = %d\n",
1398 ret);
1399 return ret;
1400 }
1401
Arnd Bergmann23168222020-10-26 17:05:23 +01001402 ret = devm_add_action_or_reset(dev, k3_r5_cluster_rproc_exit, pdev);
Suman Anna6dedbd12020-10-02 18:42:32 -05001403 if (ret)
1404 return ret;
1405
1406 return 0;
1407}
1408
Suman Anna7508ea12020-11-18 19:05:30 -06001409static const struct k3_r5_soc_data am65_j721e_soc_data = {
1410 .tcm_ecc_autoinit = false,
1411};
1412
1413static const struct k3_r5_soc_data j7200_soc_data = {
1414 .tcm_ecc_autoinit = true,
1415};
1416
Suman Anna6dedbd12020-10-02 18:42:32 -05001417static const struct of_device_id k3_r5_of_match[] = {
Suman Anna7508ea12020-11-18 19:05:30 -06001418 { .compatible = "ti,am654-r5fss", .data = &am65_j721e_soc_data, },
1419 { .compatible = "ti,j721e-r5fss", .data = &am65_j721e_soc_data, },
1420 { .compatible = "ti,j7200-r5fss", .data = &j7200_soc_data, },
Suman Anna6dedbd12020-10-02 18:42:32 -05001421 { /* sentinel */ },
1422};
1423MODULE_DEVICE_TABLE(of, k3_r5_of_match);
1424
1425static struct platform_driver k3_r5_rproc_driver = {
1426 .probe = k3_r5_probe,
1427 .driver = {
1428 .name = "k3_r5_rproc",
1429 .of_match_table = k3_r5_of_match,
1430 },
1431};
1432
1433module_platform_driver(k3_r5_rproc_driver);
1434
1435MODULE_LICENSE("GPL v2");
1436MODULE_DESCRIPTION("TI K3 R5F remote processor driver");
1437MODULE_AUTHOR("Suman Anna <s-anna@ti.com>");