blob: b55b1dc10d4a9567f2ecd7904b2b2a7b066c15c8 [file] [log] [blame]
Lokesh Vutla58633f12019-09-04 16:01:34 +05301// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Texas Instruments' K3 R5 Remoteproc driver
4 *
Suman Anna5d56d252020-08-17 18:15:08 -05005 * Copyright (C) 2018-2020 Texas Instruments Incorporated - https://www.ti.com/
Lokesh Vutla58633f12019-09-04 16:01:34 +05306 * Lokesh Vutla <lokeshvutla@ti.com>
Suman Anna5d56d252020-08-17 18:15:08 -05007 * Suman Anna <s-anna@ti.com>
Lokesh Vutla58633f12019-09-04 16:01:34 +05308 */
9
Lokesh Vutla58633f12019-09-04 16:01:34 +053010#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070012#include <malloc.h>
Lokesh Vutla58633f12019-09-04 16:01:34 +053013#include <remoteproc.h>
14#include <errno.h>
15#include <clk.h>
16#include <reset.h>
17#include <asm/io.h>
Simon Glass9bc15642020-02-03 07:36:16 -070018#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070019#include <linux/err.h>
Lokesh Vutla58633f12019-09-04 16:01:34 +053020#include <linux/kernel.h>
21#include <linux/soc/ti/ti_sci_protocol.h>
22#include "ti_sci_proc.h"
23
24/*
25 * R5F's view of this address can either be for ATCM or BTCM with the other
26 * at address 0x0 based on loczrama signal.
27 */
28#define K3_R5_TCM_DEV_ADDR 0x41010000
29
30/* R5 TI-SCI Processor Configuration Flags */
31#define PROC_BOOT_CFG_FLAG_R5_DBG_EN 0x00000001
32#define PROC_BOOT_CFG_FLAG_R5_DBG_NIDEN 0x00000002
33#define PROC_BOOT_CFG_FLAG_R5_LOCKSTEP 0x00000100
34#define PROC_BOOT_CFG_FLAG_R5_TEINIT 0x00000200
35#define PROC_BOOT_CFG_FLAG_R5_NMFI_EN 0x00000400
36#define PROC_BOOT_CFG_FLAG_R5_TCM_RSTBASE 0x00000800
37#define PROC_BOOT_CFG_FLAG_R5_BTCM_EN 0x00001000
38#define PROC_BOOT_CFG_FLAG_R5_ATCM_EN 0x00002000
39#define PROC_BOOT_CFG_FLAG_GEN_IGN_BOOTVECTOR 0x10000000
Suman Anna5d56d252020-08-17 18:15:08 -050040/* Available from J7200 SoCs onwards */
41#define PROC_BOOT_CFG_FLAG_R5_MEM_INIT_DIS 0x00004000
Lokesh Vutla58633f12019-09-04 16:01:34 +053042
43/* R5 TI-SCI Processor Control Flags */
44#define PROC_BOOT_CTRL_FLAG_R5_CORE_HALT 0x00000001
45
46/* R5 TI-SCI Processor Status Flags */
47#define PROC_BOOT_STATUS_FLAG_R5_WFE 0x00000001
48#define PROC_BOOT_STATUS_FLAG_R5_WFI 0x00000002
49#define PROC_BOOT_STATUS_FLAG_R5_CLK_GATED 0x00000004
50#define PROC_BOOT_STATUS_FLAG_R5_LOCKSTEP_PERMITTED 0x00000100
51
52#define NR_CORES 2
53
54enum cluster_mode {
55 CLUSTER_MODE_SPLIT = 0,
56 CLUSTER_MODE_LOCKSTEP,
57};
58
59/**
Suman Anna5d56d252020-08-17 18:15:08 -050060 * struct k3_r5f_ip_data - internal data structure used for IP variations
61 * @tcm_is_double: flag to denote the larger unified TCMs in certain modes
62 * @tcm_ecc_autoinit: flag to denote the auto-initialization of TCMs for ECC
63 */
64struct k3_r5f_ip_data {
65 bool tcm_is_double;
66 bool tcm_ecc_autoinit;
67};
68
69/**
Lokesh Vutla58633f12019-09-04 16:01:34 +053070 * struct k3_r5_mem - internal memory structure
71 * @cpu_addr: MPU virtual address of the memory region
72 * @bus_addr: Bus address used to access the memory region
73 * @dev_addr: Device address from remoteproc view
74 * @size: Size of the memory region
75 */
76struct k3_r5f_mem {
77 void __iomem *cpu_addr;
78 phys_addr_t bus_addr;
79 u32 dev_addr;
80 size_t size;
81};
82
83/**
84 * struct k3_r5f_core - K3 R5 core structure
85 * @dev: cached device pointer
86 * @cluster: pointer to the parent cluster.
87 * @reset: reset control handle
88 * @tsp: TI-SCI processor control handle
Suman Anna5d56d252020-08-17 18:15:08 -050089 * @ipdata: cached pointer to R5F IP specific feature data
Lokesh Vutla58633f12019-09-04 16:01:34 +053090 * @mem: Array of available internal memories
91 * @num_mem: Number of available memories
92 * @atcm_enable: flag to control ATCM enablement
93 * @btcm_enable: flag to control BTCM enablement
94 * @loczrama: flag to dictate which TCM is at device address 0x0
95 * @in_use: flag to tell if the core is already in use.
96 */
97struct k3_r5f_core {
98 struct udevice *dev;
99 struct k3_r5f_cluster *cluster;
100 struct reset_ctl reset;
101 struct ti_sci_proc tsp;
Suman Anna5d56d252020-08-17 18:15:08 -0500102 struct k3_r5f_ip_data *ipdata;
Lokesh Vutla58633f12019-09-04 16:01:34 +0530103 struct k3_r5f_mem *mem;
104 int num_mems;
105 u32 atcm_enable;
106 u32 btcm_enable;
107 u32 loczrama;
108 bool in_use;
109};
110
111/**
112 * struct k3_r5f_cluster - K3 R5F Cluster structure
113 * @mode: Mode to configure the Cluster - Split or LockStep
114 * @cores: Array of pointers to R5 cores within the cluster
115 */
116struct k3_r5f_cluster {
117 enum cluster_mode mode;
118 struct k3_r5f_core *cores[NR_CORES];
119};
120
121static bool is_primary_core(struct k3_r5f_core *core)
122{
123 return core == core->cluster->cores[0];
124}
125
126static int k3_r5f_proc_request(struct k3_r5f_core *core)
127{
128 struct k3_r5f_cluster *cluster = core->cluster;
129 int i, ret;
130
131 if (cluster->mode == CLUSTER_MODE_LOCKSTEP) {
132 for (i = 0; i < NR_CORES; i++) {
133 ret = ti_sci_proc_request(&cluster->cores[i]->tsp);
134 if (ret)
135 goto proc_release;
136 }
137 } else {
138 ret = ti_sci_proc_request(&core->tsp);
139 }
140
141 return 0;
142
143proc_release:
144 while (i >= 0) {
145 ti_sci_proc_release(&cluster->cores[i]->tsp);
146 i--;
147 }
148 return ret;
149}
150
151static void k3_r5f_proc_release(struct k3_r5f_core *core)
152{
153 struct k3_r5f_cluster *cluster = core->cluster;
154 int i;
155
156 if (cluster->mode == CLUSTER_MODE_LOCKSTEP)
157 for (i = 0; i < NR_CORES; i++)
158 ti_sci_proc_release(&cluster->cores[i]->tsp);
159 else
160 ti_sci_proc_release(&core->tsp);
161}
162
163static int k3_r5f_lockstep_release(struct k3_r5f_cluster *cluster)
164{
165 int ret, c;
166
Sean Andersonf13dc372020-09-15 10:45:08 -0400167 debug("%s\n", __func__);
Lokesh Vutla58633f12019-09-04 16:01:34 +0530168
169 for (c = NR_CORES - 1; c >= 0; c--) {
170 ret = ti_sci_proc_power_domain_on(&cluster->cores[c]->tsp);
171 if (ret)
172 goto unroll_module_reset;
173 }
174
175 /* deassert local reset on all applicable cores */
176 for (c = NR_CORES - 1; c >= 0; c--) {
177 ret = reset_deassert(&cluster->cores[c]->reset);
178 if (ret)
179 goto unroll_local_reset;
180 }
181
182 return 0;
183
184unroll_local_reset:
185 while (c < NR_CORES) {
186 reset_assert(&cluster->cores[c]->reset);
187 c++;
188 }
189 c = 0;
190unroll_module_reset:
191 while (c < NR_CORES) {
192 ti_sci_proc_power_domain_off(&cluster->cores[c]->tsp);
193 c++;
194 }
195
196 return ret;
197}
198
199static int k3_r5f_split_release(struct k3_r5f_core *core)
200{
201 int ret;
202
Sean Andersonf13dc372020-09-15 10:45:08 -0400203 dev_dbg(core->dev, "%s\n", __func__);
Lokesh Vutla58633f12019-09-04 16:01:34 +0530204
205 ret = ti_sci_proc_power_domain_on(&core->tsp);
206 if (ret) {
207 dev_err(core->dev, "module-reset deassert failed, ret = %d\n",
208 ret);
209 return ret;
210 }
211
212 ret = reset_deassert(&core->reset);
213 if (ret) {
214 dev_err(core->dev, "local-reset deassert failed, ret = %d\n",
215 ret);
216 if (ti_sci_proc_power_domain_off(&core->tsp))
217 dev_warn(core->dev, "module-reset assert back failed\n");
218 }
219
220 return ret;
221}
222
223static int k3_r5f_prepare(struct udevice *dev)
224{
225 struct k3_r5f_core *core = dev_get_priv(dev);
226 struct k3_r5f_cluster *cluster = core->cluster;
227 int ret = 0;
228
229 dev_dbg(dev, "%s\n", __func__);
230
231 if (cluster->mode == CLUSTER_MODE_LOCKSTEP)
232 ret = k3_r5f_lockstep_release(cluster);
233 else
234 ret = k3_r5f_split_release(core);
235
236 if (ret)
237 dev_err(dev, "Unable to enable cores for TCM loading %d\n",
238 ret);
239
240 return ret;
241}
242
243static int k3_r5f_core_sanity_check(struct k3_r5f_core *core)
244{
245 struct k3_r5f_cluster *cluster = core->cluster;
246
247 if (core->in_use) {
Sean Andersonf13dc372020-09-15 10:45:08 -0400248 dev_err(core->dev,
249 "Invalid op: Trying to load/start on already running core %d\n",
Lokesh Vutla58633f12019-09-04 16:01:34 +0530250 core->tsp.proc_id);
251 return -EINVAL;
252 }
253
254 if (cluster->mode == CLUSTER_MODE_LOCKSTEP && !cluster->cores[1]) {
Sean Andersonf13dc372020-09-15 10:45:08 -0400255 dev_err(core->dev,
256 "Secondary core is not probed in this cluster\n");
Lokesh Vutla58633f12019-09-04 16:01:34 +0530257 return -EAGAIN;
258 }
259
260 if (cluster->mode == CLUSTER_MODE_LOCKSTEP && !is_primary_core(core)) {
Sean Andersonf13dc372020-09-15 10:45:08 -0400261 dev_err(core->dev,
262 "Invalid op: Trying to start secondary core %d in lockstep mode\n",
Lokesh Vutla58633f12019-09-04 16:01:34 +0530263 core->tsp.proc_id);
264 return -EINVAL;
265 }
266
267 if (cluster->mode == CLUSTER_MODE_SPLIT && !is_primary_core(core)) {
268 if (!core->cluster->cores[0]->in_use) {
Sean Andersonf13dc372020-09-15 10:45:08 -0400269 dev_err(core->dev,
270 "Invalid seq: Enable primary core before loading secondary core\n");
Lokesh Vutla58633f12019-09-04 16:01:34 +0530271 return -EINVAL;
272 }
273 }
274
275 return 0;
276}
277
Suman Anna5d56d252020-08-17 18:15:08 -0500278/* Zero out TCMs so that ECC can be effective on all TCM addresses */
279void k3_r5f_init_tcm_memories(struct k3_r5f_core *core, bool auto_inited)
280{
281 if (core->ipdata->tcm_ecc_autoinit && auto_inited)
282 return;
283
284 if (core->atcm_enable)
285 memset(core->mem[0].cpu_addr, 0x00, core->mem[0].size);
286 if (core->btcm_enable)
287 memset(core->mem[1].cpu_addr, 0x00, core->mem[1].size);
288}
289
Lokesh Vutla58633f12019-09-04 16:01:34 +0530290/**
291 * k3_r5f_load() - Load up the Remote processor image
292 * @dev: rproc device pointer
293 * @addr: Address at which image is available
294 * @size: size of the image
295 *
296 * Return: 0 if all goes good, else appropriate error message.
297 */
298static int k3_r5f_load(struct udevice *dev, ulong addr, ulong size)
299{
300 struct k3_r5f_core *core = dev_get_priv(dev);
Suman Anna5d56d252020-08-17 18:15:08 -0500301 u64 boot_vector;
302 u32 ctrl, sts, cfg = 0;
303 bool mem_auto_init;
Lokesh Vutla58633f12019-09-04 16:01:34 +0530304 int ret;
305
306 dev_dbg(dev, "%s addr = 0x%lx, size = 0x%lx\n", __func__, addr, size);
307
308 ret = k3_r5f_core_sanity_check(core);
309 if (ret)
310 return ret;
311
312 ret = k3_r5f_proc_request(core);
313 if (ret)
314 return ret;
Suman Anna5d56d252020-08-17 18:15:08 -0500315
316 ret = ti_sci_proc_get_status(&core->tsp, &boot_vector, &cfg, &ctrl,
317 &sts);
318 if (ret)
319 return ret;
320 mem_auto_init = !(cfg & PROC_BOOT_CFG_FLAG_R5_MEM_INIT_DIS);
Lokesh Vutla58633f12019-09-04 16:01:34 +0530321
322 ret = k3_r5f_prepare(dev);
323 if (ret) {
324 dev_err(dev, "R5f prepare failed for core %d\n",
325 core->tsp.proc_id);
326 goto proc_release;
327 }
328
Suman Anna5d56d252020-08-17 18:15:08 -0500329 k3_r5f_init_tcm_memories(core, mem_auto_init);
Lokesh Vutla58633f12019-09-04 16:01:34 +0530330
331 ret = rproc_elf_load_image(dev, addr, size);
332 if (ret < 0) {
333 dev_err(dev, "Loading elf failedi %d\n", ret);
334 goto proc_release;
335 }
336
337 boot_vector = rproc_elf_get_boot_addr(dev, addr);
338
Suman Anna5d56d252020-08-17 18:15:08 -0500339 dev_dbg(dev, "%s: Boot vector = 0x%llx\n", __func__, boot_vector);
Lokesh Vutla58633f12019-09-04 16:01:34 +0530340
341 ret = ti_sci_proc_set_config(&core->tsp, boot_vector, 0, 0);
342
343proc_release:
344 k3_r5f_proc_release(core);
345
346 return ret;
347}
348
349static int k3_r5f_core_halt(struct k3_r5f_core *core)
350{
351 int ret;
352
353 ret = ti_sci_proc_set_control(&core->tsp,
354 PROC_BOOT_CTRL_FLAG_R5_CORE_HALT, 0);
355 if (ret)
356 dev_err(core->dev, "Core %d failed to stop\n",
357 core->tsp.proc_id);
358
359 return ret;
360}
361
362static int k3_r5f_core_run(struct k3_r5f_core *core)
363{
364 int ret;
365
366 ret = ti_sci_proc_set_control(&core->tsp,
367 0, PROC_BOOT_CTRL_FLAG_R5_CORE_HALT);
368 if (ret) {
369 dev_err(core->dev, "Core %d failed to start\n",
370 core->tsp.proc_id);
371 return ret;
372 }
373
374 return 0;
375}
376
377/**
378 * k3_r5f_start() - Start the remote processor
379 * @dev: rproc device pointer
380 *
381 * Return: 0 if all went ok, else return appropriate error
382 */
383static int k3_r5f_start(struct udevice *dev)
384{
385 struct k3_r5f_core *core = dev_get_priv(dev);
386 struct k3_r5f_cluster *cluster = core->cluster;
387 int ret, c;
388
389 dev_dbg(dev, "%s\n", __func__);
390
391 ret = k3_r5f_core_sanity_check(core);
392 if (ret)
393 return ret;
394
395 ret = k3_r5f_proc_request(core);
396 if (ret)
397 return ret;
398
399 if (cluster->mode == CLUSTER_MODE_LOCKSTEP) {
400 if (is_primary_core(core)) {
401 for (c = NR_CORES - 1; c >= 0; c--) {
402 ret = k3_r5f_core_run(cluster->cores[c]);
403 if (ret)
404 goto unroll_core_run;
405 }
406 } else {
407 dev_err(dev, "Invalid op: Trying to start secondary core %d in lockstep mode\n",
408 core->tsp.proc_id);
409 ret = -EINVAL;
410 goto proc_release;
411 }
412 } else {
413 ret = k3_r5f_core_run(core);
414 if (ret)
415 goto proc_release;
416 }
417
418 core->in_use = true;
419
420 k3_r5f_proc_release(core);
421 return 0;
422
423unroll_core_run:
424 while (c < NR_CORES) {
425 k3_r5f_core_halt(cluster->cores[c]);
426 c++;
427 }
428proc_release:
429 k3_r5f_proc_release(core);
430
431 return ret;
432}
433
434static int k3_r5f_split_reset(struct k3_r5f_core *core)
435{
436 int ret;
437
Sean Andersonf13dc372020-09-15 10:45:08 -0400438 dev_dbg(core->dev, "%s\n", __func__);
Lokesh Vutla58633f12019-09-04 16:01:34 +0530439
440 if (reset_assert(&core->reset))
441 ret = -EINVAL;
442
443 if (ti_sci_proc_power_domain_off(&core->tsp))
444 ret = -EINVAL;
445
446 return ret;
447}
448
449static int k3_r5f_lockstep_reset(struct k3_r5f_cluster *cluster)
450{
451 int ret = 0, c;
452
Sean Andersonf13dc372020-09-15 10:45:08 -0400453 debug("%s\n", __func__);
Lokesh Vutla58633f12019-09-04 16:01:34 +0530454
455 for (c = 0; c < NR_CORES; c++)
456 if (reset_assert(&cluster->cores[c]->reset))
457 ret = -EINVAL;
458
459 /* disable PSC modules on all applicable cores */
460 for (c = 0; c < NR_CORES; c++)
461 if (ti_sci_proc_power_domain_off(&cluster->cores[c]->tsp))
462 ret = -EINVAL;
463
464 return ret;
465}
466
467static int k3_r5f_unprepare(struct udevice *dev)
468{
469 struct k3_r5f_core *core = dev_get_priv(dev);
470 struct k3_r5f_cluster *cluster = core->cluster;
471 int ret;
472
473 dev_dbg(dev, "%s\n", __func__);
474
475 if (cluster->mode == CLUSTER_MODE_LOCKSTEP) {
476 if (is_primary_core(core))
477 ret = k3_r5f_lockstep_reset(cluster);
478 } else {
479 ret = k3_r5f_split_reset(core);
480 }
481
482 if (ret)
483 dev_warn(dev, "Unable to enable cores for TCM loading %d\n",
484 ret);
485
486 return 0;
487}
488
489static int k3_r5f_stop(struct udevice *dev)
490{
491 struct k3_r5f_core *core = dev_get_priv(dev);
492 struct k3_r5f_cluster *cluster = core->cluster;
493 int c, ret;
494
495 dev_dbg(dev, "%s\n", __func__);
496
497 ret = k3_r5f_proc_request(core);
498 if (ret)
499 return ret;
500
501 core->in_use = false;
502
503 if (cluster->mode == CLUSTER_MODE_LOCKSTEP) {
504 if (is_primary_core(core)) {
505 for (c = 0; c < NR_CORES; c++)
506 k3_r5f_core_halt(cluster->cores[c]);
507 } else {
508 dev_err(dev, "Invalid op: Trying to stop secondary core in lockstep mode\n");
509 ret = -EINVAL;
510 goto proc_release;
511 }
512 } else {
513 k3_r5f_core_halt(core);
514 }
515
516 ret = k3_r5f_unprepare(dev);
517proc_release:
518 k3_r5f_proc_release(core);
519 return ret;
520}
521
522static void *k3_r5f_da_to_va(struct udevice *dev, ulong da, ulong size)
523{
524 struct k3_r5f_core *core = dev_get_priv(dev);
525 void __iomem *va = NULL;
526 phys_addr_t bus_addr;
527 u32 dev_addr, offset;
528 ulong mem_size;
529 int i;
530
531 dev_dbg(dev, "%s\n", __func__);
532
533 if (size <= 0)
534 return NULL;
535
536 for (i = 0; i < core->num_mems; i++) {
537 bus_addr = core->mem[i].bus_addr;
538 dev_addr = core->mem[i].dev_addr;
539 mem_size = core->mem[i].size;
540
541 if (da >= bus_addr && (da + size) <= (bus_addr + mem_size)) {
542 offset = da - bus_addr;
543 va = core->mem[i].cpu_addr + offset;
544 return (__force void *)va;
545 }
546
547 if (da >= dev_addr && (da + size) <= (dev_addr + mem_size)) {
548 offset = da - dev_addr;
549 va = core->mem[i].cpu_addr + offset;
550 return (__force void *)va;
551 }
552 }
553
554 /* Assume it is DDR region and return da */
555 return map_physmem(da, size, MAP_NOCACHE);
556}
557
558static int k3_r5f_init(struct udevice *dev)
559{
560 return 0;
561}
562
563static int k3_r5f_reset(struct udevice *dev)
564{
565 return 0;
566}
567
568static const struct dm_rproc_ops k3_r5f_rproc_ops = {
569 .init = k3_r5f_init,
570 .reset = k3_r5f_reset,
571 .start = k3_r5f_start,
572 .stop = k3_r5f_stop,
573 .load = k3_r5f_load,
574 .device_to_virt = k3_r5f_da_to_va,
575};
576
577static int k3_r5f_rproc_configure(struct k3_r5f_core *core)
578{
579 struct k3_r5f_cluster *cluster = core->cluster;
580 u32 set_cfg = 0, clr_cfg = 0, cfg, ctrl, sts;
Suman Anna9ff29302020-03-10 20:24:29 -0500581 bool lockstep_permitted;
Lokesh Vutla58633f12019-09-04 16:01:34 +0530582 u64 boot_vec = 0;
583 int ret;
584
Sean Andersonf13dc372020-09-15 10:45:08 -0400585 dev_dbg(core->dev, "%s\n", __func__);
Lokesh Vutla58633f12019-09-04 16:01:34 +0530586
587 ret = ti_sci_proc_request(&core->tsp);
588 if (ret < 0)
589 return ret;
590
591 /* Do not touch boot vector now. Load will take care of it. */
592 clr_cfg |= PROC_BOOT_CFG_FLAG_GEN_IGN_BOOTVECTOR;
593
594 ret = ti_sci_proc_get_status(&core->tsp, &boot_vec, &cfg, &ctrl, &sts);
595 if (ret)
596 goto out;
597
598 /* Sanity check for Lockstep mode */
Suman Anna9ff29302020-03-10 20:24:29 -0500599 lockstep_permitted = !!(sts &
600 PROC_BOOT_STATUS_FLAG_R5_LOCKSTEP_PERMITTED);
601 if (cluster->mode && is_primary_core(core) && !lockstep_permitted) {
Lokesh Vutla58633f12019-09-04 16:01:34 +0530602 dev_err(core->dev, "LockStep mode not permitted on this device\n");
603 ret = -EINVAL;
604 goto out;
605 }
606
607 /* Primary core only configuration */
608 if (is_primary_core(core)) {
609 /* always enable ARM mode */
610 clr_cfg |= PROC_BOOT_CFG_FLAG_R5_TEINIT;
611 if (cluster->mode == CLUSTER_MODE_LOCKSTEP)
612 set_cfg |= PROC_BOOT_CFG_FLAG_R5_LOCKSTEP;
Suman Anna9ff29302020-03-10 20:24:29 -0500613 else if (lockstep_permitted)
Lokesh Vutla58633f12019-09-04 16:01:34 +0530614 clr_cfg |= PROC_BOOT_CFG_FLAG_R5_LOCKSTEP;
615 }
616
617 if (core->atcm_enable)
618 set_cfg |= PROC_BOOT_CFG_FLAG_R5_ATCM_EN;
619 else
620 clr_cfg |= PROC_BOOT_CFG_FLAG_R5_ATCM_EN;
621
622 if (core->btcm_enable)
623 set_cfg |= PROC_BOOT_CFG_FLAG_R5_BTCM_EN;
624 else
625 clr_cfg |= PROC_BOOT_CFG_FLAG_R5_BTCM_EN;
626
627 if (core->loczrama)
628 set_cfg |= PROC_BOOT_CFG_FLAG_R5_TCM_RSTBASE;
629 else
630 clr_cfg |= PROC_BOOT_CFG_FLAG_R5_TCM_RSTBASE;
631
632 ret = k3_r5f_core_halt(core);
633 if (ret)
634 goto out;
635
636 ret = ti_sci_proc_set_config(&core->tsp, boot_vec, set_cfg, clr_cfg);
637out:
638 ti_sci_proc_release(&core->tsp);
639 return ret;
640}
641
642static int ti_sci_proc_of_to_priv(struct udevice *dev, struct ti_sci_proc *tsp)
643{
644 u32 ids[2];
645 int ret;
646
647 dev_dbg(dev, "%s\n", __func__);
648
649 tsp->sci = ti_sci_get_by_phandle(dev, "ti,sci");
650 if (IS_ERR(tsp->sci)) {
651 dev_err(dev, "ti_sci get failed: %ld\n", PTR_ERR(tsp->sci));
652 return PTR_ERR(tsp->sci);
653 }
654
655 ret = dev_read_u32_array(dev, "ti,sci-proc-ids", ids, 2);
656 if (ret) {
657 dev_err(dev, "Proc IDs not populated %d\n", ret);
658 return ret;
659 }
660
661 tsp->ops = &tsp->sci->ops.proc_ops;
662 tsp->proc_id = ids[0];
663 tsp->host_id = ids[1];
664 tsp->dev_id = dev_read_u32_default(dev, "ti,sci-dev-id",
665 TI_SCI_RESOURCE_NULL);
666 if (tsp->dev_id == TI_SCI_RESOURCE_NULL) {
667 dev_err(dev, "Device ID not populated %d\n", ret);
668 return -ENODEV;
669 }
670
671 return 0;
672}
673
674static int k3_r5f_of_to_priv(struct k3_r5f_core *core)
675{
676 int ret;
677
Sean Andersonf13dc372020-09-15 10:45:08 -0400678 dev_dbg(core->dev, "%s\n", __func__);
Lokesh Vutla58633f12019-09-04 16:01:34 +0530679
Suman Annaa45e6db2021-01-26 18:20:56 -0600680 core->atcm_enable = dev_read_u32_default(core->dev, "ti,atcm-enable", 0);
681 core->btcm_enable = dev_read_u32_default(core->dev, "ti,btcm-enable", 1);
682 core->loczrama = dev_read_u32_default(core->dev, "ti,loczrama", 1);
Lokesh Vutla58633f12019-09-04 16:01:34 +0530683
684 ret = ti_sci_proc_of_to_priv(core->dev, &core->tsp);
685 if (ret)
686 return ret;
687
688 ret = reset_get_by_index(core->dev, 0, &core->reset);
689 if (ret) {
690 dev_err(core->dev, "Reset lines not available: %d\n", ret);
691 return ret;
692 }
693
Suman Anna5d56d252020-08-17 18:15:08 -0500694 core->ipdata = (struct k3_r5f_ip_data *)dev_get_driver_data(core->dev);
695
Lokesh Vutla58633f12019-09-04 16:01:34 +0530696 return 0;
697}
698
699static int k3_r5f_core_of_get_memories(struct k3_r5f_core *core)
700{
701 static const char * const mem_names[] = {"atcm", "btcm"};
702 struct udevice *dev = core->dev;
703 int i;
704
705 dev_dbg(dev, "%s\n", __func__);
706
707 core->num_mems = ARRAY_SIZE(mem_names);
708 core->mem = calloc(core->num_mems, sizeof(*core->mem));
709 if (!core->mem)
710 return -ENOMEM;
711
712 for (i = 0; i < core->num_mems; i++) {
713 core->mem[i].bus_addr = dev_read_addr_size_name(dev,
714 mem_names[i],
715 (fdt_addr_t *)&core->mem[i].size);
716 if (core->mem[i].bus_addr == FDT_ADDR_T_NONE) {
717 dev_err(dev, "%s bus address not found\n",
718 mem_names[i]);
719 return -EINVAL;
720 }
721 core->mem[i].cpu_addr = map_physmem(core->mem[i].bus_addr,
722 core->mem[i].size,
723 MAP_NOCACHE);
724 if (!strcmp(mem_names[i], "atcm")) {
725 core->mem[i].dev_addr = core->loczrama ?
726 0 : K3_R5_TCM_DEV_ADDR;
727 } else {
728 core->mem[i].dev_addr = core->loczrama ?
729 K3_R5_TCM_DEV_ADDR : 0;
730 }
731
732 dev_dbg(dev, "memory %8s: bus addr %pa size 0x%zx va %p da 0x%x\n",
733 mem_names[i], &core->mem[i].bus_addr,
734 core->mem[i].size, core->mem[i].cpu_addr,
735 core->mem[i].dev_addr);
736 }
737
738 return 0;
739}
740
Suman Anna5d56d252020-08-17 18:15:08 -0500741/*
742 * Each R5F core within a typical R5FSS instance has a total of 64 KB of TCMs,
743 * split equally into two 32 KB banks between ATCM and BTCM. The TCMs from both
744 * cores are usable in Split-mode, but only the Core0 TCMs can be used in
745 * LockStep-mode. The newer revisions of the R5FSS IP maximizes these TCMs by
746 * leveraging the Core1 TCMs as well in certain modes where they would have
747 * otherwise been unusable (Eg: LockStep-mode on J7200 SoCs). This is done by
748 * making a Core1 TCM visible immediately after the corresponding Core0 TCM.
749 * The SoC memory map uses the larger 64 KB sizes for the Core0 TCMs, and the
750 * dts representation reflects this increased size on supported SoCs. The Core0
751 * TCM sizes therefore have to be adjusted to only half the original size in
752 * Split mode.
753 */
754static void k3_r5f_core_adjust_tcm_sizes(struct k3_r5f_core *core)
755{
756 struct k3_r5f_cluster *cluster = core->cluster;
757
758 if (cluster->mode == CLUSTER_MODE_LOCKSTEP)
759 return;
760
761 if (!core->ipdata->tcm_is_double)
762 return;
763
764 if (core == cluster->cores[0]) {
765 core->mem[0].size /= 2;
766 core->mem[1].size /= 2;
767
768 dev_dbg(core->dev, "adjusted TCM sizes, ATCM = 0x%zx BTCM = 0x%zx\n",
769 core->mem[0].size, core->mem[1].size);
770 }
771}
772
Lokesh Vutla58633f12019-09-04 16:01:34 +0530773/**
774 * k3_r5f_probe() - Basic probe
775 * @dev: corresponding k3 remote processor device
776 *
777 * Return: 0 if all goes good, else appropriate error message.
778 */
779static int k3_r5f_probe(struct udevice *dev)
780{
781 struct k3_r5f_cluster *cluster = dev_get_priv(dev->parent);
782 struct k3_r5f_core *core = dev_get_priv(dev);
783 bool r_state;
784 int ret;
785
786 dev_dbg(dev, "%s\n", __func__);
787
788 core->dev = dev;
789 ret = k3_r5f_of_to_priv(core);
790 if (ret)
791 return ret;
792
793 core->cluster = cluster;
794 /* Assume Primary core gets probed first */
795 if (!cluster->cores[0])
796 cluster->cores[0] = core;
797 else
798 cluster->cores[1] = core;
799
800 ret = k3_r5f_core_of_get_memories(core);
801 if (ret) {
802 dev_err(dev, "Rproc getting internal memories failed\n");
803 return ret;
804 }
805
Tero Kristof454d612021-06-11 11:45:04 +0300806 /*
807 * The PM functionality is not supported by the firmware during
808 * SPL execution with the separated DM firmware image. The following
809 * piece of code is not compiled in that case.
810 */
811 if (!IS_ENABLED(CONFIG_K3_DM_FW)) {
812 ret = core->tsp.sci->ops.dev_ops.is_on(core->tsp.sci,
813 core->tsp.dev_id,
814 &r_state, &core->in_use);
815 if (ret)
816 return ret;
Lokesh Vutla58633f12019-09-04 16:01:34 +0530817
Tero Kristof454d612021-06-11 11:45:04 +0300818 if (core->in_use) {
819 dev_info(dev, "Core %d is already in use. No rproc commands work\n",
820 core->tsp.proc_id);
821 return 0;
822 }
Lokesh Vutla58633f12019-09-04 16:01:34 +0530823
Tero Kristof454d612021-06-11 11:45:04 +0300824 /* Make sure Local reset is asserted. Redundant? */
825 reset_assert(&core->reset);
826 }
Lokesh Vutla58633f12019-09-04 16:01:34 +0530827
828 ret = k3_r5f_rproc_configure(core);
829 if (ret) {
830 dev_err(dev, "rproc configure failed %d\n", ret);
831 return ret;
832 }
833
Suman Anna5d56d252020-08-17 18:15:08 -0500834 k3_r5f_core_adjust_tcm_sizes(core);
835
Lokesh Vutla58633f12019-09-04 16:01:34 +0530836 dev_dbg(dev, "Remoteproc successfully probed\n");
837
838 return 0;
839}
840
841static int k3_r5f_remove(struct udevice *dev)
842{
843 struct k3_r5f_core *core = dev_get_priv(dev);
844
845 free(core->mem);
846
847 ti_sci_proc_release(&core->tsp);
848
849 return 0;
850}
851
Suman Anna5d56d252020-08-17 18:15:08 -0500852static const struct k3_r5f_ip_data k3_data = {
853 .tcm_is_double = false,
854 .tcm_ecc_autoinit = false,
855};
856
Hari Nagalla225271d2024-03-12 15:14:35 +0530857static const struct k3_r5f_ip_data j7200_j721s2_data = {
Suman Anna5d56d252020-08-17 18:15:08 -0500858 .tcm_is_double = true,
859 .tcm_ecc_autoinit = true,
860};
861
Lokesh Vutla58633f12019-09-04 16:01:34 +0530862static const struct udevice_id k3_r5f_rproc_ids[] = {
Suman Anna5d56d252020-08-17 18:15:08 -0500863 { .compatible = "ti,am654-r5f", .data = (ulong)&k3_data, },
864 { .compatible = "ti,j721e-r5f", .data = (ulong)&k3_data, },
Hari Nagalla225271d2024-03-12 15:14:35 +0530865 { .compatible = "ti,j7200-r5f", .data = (ulong)&j7200_j721s2_data, },
866 { .compatible = "ti,j721s2-r5f", .data = (ulong)&j7200_j721s2_data, },
Lokesh Vutla58633f12019-09-04 16:01:34 +0530867 {}
868};
869
870U_BOOT_DRIVER(k3_r5f_rproc) = {
871 .name = "k3_r5f_rproc",
872 .of_match = k3_r5f_rproc_ids,
873 .id = UCLASS_REMOTEPROC,
874 .ops = &k3_r5f_rproc_ops,
875 .probe = k3_r5f_probe,
876 .remove = k3_r5f_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700877 .priv_auto = sizeof(struct k3_r5f_core),
Lokesh Vutla58633f12019-09-04 16:01:34 +0530878};
879
880static int k3_r5f_cluster_probe(struct udevice *dev)
881{
882 struct k3_r5f_cluster *cluster = dev_get_priv(dev);
883
884 dev_dbg(dev, "%s\n", __func__);
885
Suman Annaa45e6db2021-01-26 18:20:56 -0600886 cluster->mode = dev_read_u32_default(dev, "ti,cluster-mode",
Lokesh Vutla58633f12019-09-04 16:01:34 +0530887 CLUSTER_MODE_LOCKSTEP);
888
889 if (device_get_child_count(dev) != 2) {
890 dev_err(dev, "Invalid number of R5 cores");
891 return -EINVAL;
892 }
893
894 dev_dbg(dev, "%s: Cluster successfully probed in %s mode\n",
895 __func__, cluster->mode ? "lockstep" : "split");
896
897 return 0;
898}
899
900static const struct udevice_id k3_r5fss_ids[] = {
901 { .compatible = "ti,am654-r5fss"},
902 { .compatible = "ti,j721e-r5fss"},
Suman Anna5d56d252020-08-17 18:15:08 -0500903 { .compatible = "ti,j7200-r5fss"},
Hari Nagalla225271d2024-03-12 15:14:35 +0530904 { .compatible = "ti,j721s2-r5fss"},
Lokesh Vutla58633f12019-09-04 16:01:34 +0530905 {}
906};
907
908U_BOOT_DRIVER(k3_r5fss) = {
909 .name = "k3_r5fss",
910 .of_match = k3_r5fss_ids,
911 .id = UCLASS_MISC,
912 .probe = k3_r5f_cluster_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700913 .priv_auto = sizeof(struct k3_r5f_cluster),
Suman Anna41e89862020-03-10 18:34:54 -0500914 .flags = DM_FLAG_DEFAULT_PD_CTRL_OFF,
Lokesh Vutla58633f12019-09-04 16:01:34 +0530915};