Lokesh Vutla | 58633f1 | 2019-09-04 16:01:34 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Texas Instruments' K3 R5 Remoteproc driver |
| 4 | * |
| 5 | * Copyright (C) 2018-2019 Texas Instruments Incorporated - http://www.ti.com/ |
| 6 | * Lokesh Vutla <lokeshvutla@ti.com> |
| 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 12 | #include <malloc.h> |
Lokesh Vutla | 58633f1 | 2019-09-04 16:01:34 +0530 | [diff] [blame] | 13 | #include <remoteproc.h> |
| 14 | #include <errno.h> |
| 15 | #include <clk.h> |
| 16 | #include <reset.h> |
| 17 | #include <asm/io.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 18 | #include <dm/device_compat.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 19 | #include <linux/err.h> |
Lokesh Vutla | 58633f1 | 2019-09-04 16:01:34 +0530 | [diff] [blame] | 20 | #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 |
| 40 | |
| 41 | /* R5 TI-SCI Processor Control Flags */ |
| 42 | #define PROC_BOOT_CTRL_FLAG_R5_CORE_HALT 0x00000001 |
| 43 | |
| 44 | /* R5 TI-SCI Processor Status Flags */ |
| 45 | #define PROC_BOOT_STATUS_FLAG_R5_WFE 0x00000001 |
| 46 | #define PROC_BOOT_STATUS_FLAG_R5_WFI 0x00000002 |
| 47 | #define PROC_BOOT_STATUS_FLAG_R5_CLK_GATED 0x00000004 |
| 48 | #define PROC_BOOT_STATUS_FLAG_R5_LOCKSTEP_PERMITTED 0x00000100 |
| 49 | |
| 50 | #define NR_CORES 2 |
| 51 | |
| 52 | enum cluster_mode { |
| 53 | CLUSTER_MODE_SPLIT = 0, |
| 54 | CLUSTER_MODE_LOCKSTEP, |
| 55 | }; |
| 56 | |
| 57 | /** |
| 58 | * struct k3_r5_mem - internal memory structure |
| 59 | * @cpu_addr: MPU virtual address of the memory region |
| 60 | * @bus_addr: Bus address used to access the memory region |
| 61 | * @dev_addr: Device address from remoteproc view |
| 62 | * @size: Size of the memory region |
| 63 | */ |
| 64 | struct k3_r5f_mem { |
| 65 | void __iomem *cpu_addr; |
| 66 | phys_addr_t bus_addr; |
| 67 | u32 dev_addr; |
| 68 | size_t size; |
| 69 | }; |
| 70 | |
| 71 | /** |
| 72 | * struct k3_r5f_core - K3 R5 core structure |
| 73 | * @dev: cached device pointer |
| 74 | * @cluster: pointer to the parent cluster. |
| 75 | * @reset: reset control handle |
| 76 | * @tsp: TI-SCI processor control handle |
| 77 | * @mem: Array of available internal memories |
| 78 | * @num_mem: Number of available memories |
| 79 | * @atcm_enable: flag to control ATCM enablement |
| 80 | * @btcm_enable: flag to control BTCM enablement |
| 81 | * @loczrama: flag to dictate which TCM is at device address 0x0 |
| 82 | * @in_use: flag to tell if the core is already in use. |
| 83 | */ |
| 84 | struct k3_r5f_core { |
| 85 | struct udevice *dev; |
| 86 | struct k3_r5f_cluster *cluster; |
| 87 | struct reset_ctl reset; |
| 88 | struct ti_sci_proc tsp; |
| 89 | struct k3_r5f_mem *mem; |
| 90 | int num_mems; |
| 91 | u32 atcm_enable; |
| 92 | u32 btcm_enable; |
| 93 | u32 loczrama; |
| 94 | bool in_use; |
| 95 | }; |
| 96 | |
| 97 | /** |
| 98 | * struct k3_r5f_cluster - K3 R5F Cluster structure |
| 99 | * @mode: Mode to configure the Cluster - Split or LockStep |
| 100 | * @cores: Array of pointers to R5 cores within the cluster |
| 101 | */ |
| 102 | struct k3_r5f_cluster { |
| 103 | enum cluster_mode mode; |
| 104 | struct k3_r5f_core *cores[NR_CORES]; |
| 105 | }; |
| 106 | |
| 107 | static bool is_primary_core(struct k3_r5f_core *core) |
| 108 | { |
| 109 | return core == core->cluster->cores[0]; |
| 110 | } |
| 111 | |
| 112 | static int k3_r5f_proc_request(struct k3_r5f_core *core) |
| 113 | { |
| 114 | struct k3_r5f_cluster *cluster = core->cluster; |
| 115 | int i, ret; |
| 116 | |
| 117 | if (cluster->mode == CLUSTER_MODE_LOCKSTEP) { |
| 118 | for (i = 0; i < NR_CORES; i++) { |
| 119 | ret = ti_sci_proc_request(&cluster->cores[i]->tsp); |
| 120 | if (ret) |
| 121 | goto proc_release; |
| 122 | } |
| 123 | } else { |
| 124 | ret = ti_sci_proc_request(&core->tsp); |
| 125 | } |
| 126 | |
| 127 | return 0; |
| 128 | |
| 129 | proc_release: |
| 130 | while (i >= 0) { |
| 131 | ti_sci_proc_release(&cluster->cores[i]->tsp); |
| 132 | i--; |
| 133 | } |
| 134 | return ret; |
| 135 | } |
| 136 | |
| 137 | static void k3_r5f_proc_release(struct k3_r5f_core *core) |
| 138 | { |
| 139 | struct k3_r5f_cluster *cluster = core->cluster; |
| 140 | int i; |
| 141 | |
| 142 | if (cluster->mode == CLUSTER_MODE_LOCKSTEP) |
| 143 | for (i = 0; i < NR_CORES; i++) |
| 144 | ti_sci_proc_release(&cluster->cores[i]->tsp); |
| 145 | else |
| 146 | ti_sci_proc_release(&core->tsp); |
| 147 | } |
| 148 | |
| 149 | static int k3_r5f_lockstep_release(struct k3_r5f_cluster *cluster) |
| 150 | { |
| 151 | int ret, c; |
| 152 | |
| 153 | dev_dbg(dev, "%s\n", __func__); |
| 154 | |
| 155 | for (c = NR_CORES - 1; c >= 0; c--) { |
| 156 | ret = ti_sci_proc_power_domain_on(&cluster->cores[c]->tsp); |
| 157 | if (ret) |
| 158 | goto unroll_module_reset; |
| 159 | } |
| 160 | |
| 161 | /* deassert local reset on all applicable cores */ |
| 162 | for (c = NR_CORES - 1; c >= 0; c--) { |
| 163 | ret = reset_deassert(&cluster->cores[c]->reset); |
| 164 | if (ret) |
| 165 | goto unroll_local_reset; |
| 166 | } |
| 167 | |
| 168 | return 0; |
| 169 | |
| 170 | unroll_local_reset: |
| 171 | while (c < NR_CORES) { |
| 172 | reset_assert(&cluster->cores[c]->reset); |
| 173 | c++; |
| 174 | } |
| 175 | c = 0; |
| 176 | unroll_module_reset: |
| 177 | while (c < NR_CORES) { |
| 178 | ti_sci_proc_power_domain_off(&cluster->cores[c]->tsp); |
| 179 | c++; |
| 180 | } |
| 181 | |
| 182 | return ret; |
| 183 | } |
| 184 | |
| 185 | static int k3_r5f_split_release(struct k3_r5f_core *core) |
| 186 | { |
| 187 | int ret; |
| 188 | |
| 189 | dev_dbg(dev, "%s\n", __func__); |
| 190 | |
| 191 | ret = ti_sci_proc_power_domain_on(&core->tsp); |
| 192 | if (ret) { |
| 193 | dev_err(core->dev, "module-reset deassert failed, ret = %d\n", |
| 194 | ret); |
| 195 | return ret; |
| 196 | } |
| 197 | |
| 198 | ret = reset_deassert(&core->reset); |
| 199 | if (ret) { |
| 200 | dev_err(core->dev, "local-reset deassert failed, ret = %d\n", |
| 201 | ret); |
| 202 | if (ti_sci_proc_power_domain_off(&core->tsp)) |
| 203 | dev_warn(core->dev, "module-reset assert back failed\n"); |
| 204 | } |
| 205 | |
| 206 | return ret; |
| 207 | } |
| 208 | |
| 209 | static int k3_r5f_prepare(struct udevice *dev) |
| 210 | { |
| 211 | struct k3_r5f_core *core = dev_get_priv(dev); |
| 212 | struct k3_r5f_cluster *cluster = core->cluster; |
| 213 | int ret = 0; |
| 214 | |
| 215 | dev_dbg(dev, "%s\n", __func__); |
| 216 | |
| 217 | if (cluster->mode == CLUSTER_MODE_LOCKSTEP) |
| 218 | ret = k3_r5f_lockstep_release(cluster); |
| 219 | else |
| 220 | ret = k3_r5f_split_release(core); |
| 221 | |
| 222 | if (ret) |
| 223 | dev_err(dev, "Unable to enable cores for TCM loading %d\n", |
| 224 | ret); |
| 225 | |
| 226 | return ret; |
| 227 | } |
| 228 | |
| 229 | static int k3_r5f_core_sanity_check(struct k3_r5f_core *core) |
| 230 | { |
| 231 | struct k3_r5f_cluster *cluster = core->cluster; |
| 232 | |
| 233 | if (core->in_use) { |
| 234 | dev_err(dev, "Invalid op: Trying to load/start on already running core %d\n", |
| 235 | core->tsp.proc_id); |
| 236 | return -EINVAL; |
| 237 | } |
| 238 | |
| 239 | if (cluster->mode == CLUSTER_MODE_LOCKSTEP && !cluster->cores[1]) { |
| 240 | printf("Secondary core is not probed in this cluster\n"); |
| 241 | return -EAGAIN; |
| 242 | } |
| 243 | |
| 244 | if (cluster->mode == CLUSTER_MODE_LOCKSTEP && !is_primary_core(core)) { |
| 245 | dev_err(dev, "Invalid op: Trying to start secondary core %d in lockstep mode\n", |
| 246 | core->tsp.proc_id); |
| 247 | return -EINVAL; |
| 248 | } |
| 249 | |
| 250 | if (cluster->mode == CLUSTER_MODE_SPLIT && !is_primary_core(core)) { |
| 251 | if (!core->cluster->cores[0]->in_use) { |
| 252 | dev_err(dev, "Invalid seq: Enable primary core before loading secondary core\n"); |
| 253 | return -EINVAL; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * k3_r5f_load() - Load up the Remote processor image |
| 262 | * @dev: rproc device pointer |
| 263 | * @addr: Address at which image is available |
| 264 | * @size: size of the image |
| 265 | * |
| 266 | * Return: 0 if all goes good, else appropriate error message. |
| 267 | */ |
| 268 | static int k3_r5f_load(struct udevice *dev, ulong addr, ulong size) |
| 269 | { |
| 270 | struct k3_r5f_core *core = dev_get_priv(dev); |
| 271 | u32 boot_vector; |
| 272 | int ret; |
| 273 | |
| 274 | dev_dbg(dev, "%s addr = 0x%lx, size = 0x%lx\n", __func__, addr, size); |
| 275 | |
| 276 | ret = k3_r5f_core_sanity_check(core); |
| 277 | if (ret) |
| 278 | return ret; |
| 279 | |
| 280 | ret = k3_r5f_proc_request(core); |
| 281 | if (ret) |
| 282 | return ret; |
| 283 | |
| 284 | ret = k3_r5f_prepare(dev); |
| 285 | if (ret) { |
| 286 | dev_err(dev, "R5f prepare failed for core %d\n", |
| 287 | core->tsp.proc_id); |
| 288 | goto proc_release; |
| 289 | } |
| 290 | |
| 291 | /* Zero out TCMs so that ECC can be effective on all TCM addresses */ |
| 292 | if (core->atcm_enable) |
| 293 | memset(core->mem[0].cpu_addr, 0x00, core->mem[0].size); |
| 294 | if (core->btcm_enable) |
| 295 | memset(core->mem[1].cpu_addr, 0x00, core->mem[1].size); |
| 296 | |
| 297 | ret = rproc_elf_load_image(dev, addr, size); |
| 298 | if (ret < 0) { |
| 299 | dev_err(dev, "Loading elf failedi %d\n", ret); |
| 300 | goto proc_release; |
| 301 | } |
| 302 | |
| 303 | boot_vector = rproc_elf_get_boot_addr(dev, addr); |
| 304 | |
| 305 | dev_dbg(dev, "%s: Boot vector = 0x%x\n", __func__, boot_vector); |
| 306 | |
| 307 | ret = ti_sci_proc_set_config(&core->tsp, boot_vector, 0, 0); |
| 308 | |
| 309 | proc_release: |
| 310 | k3_r5f_proc_release(core); |
| 311 | |
| 312 | return ret; |
| 313 | } |
| 314 | |
| 315 | static int k3_r5f_core_halt(struct k3_r5f_core *core) |
| 316 | { |
| 317 | int ret; |
| 318 | |
| 319 | ret = ti_sci_proc_set_control(&core->tsp, |
| 320 | PROC_BOOT_CTRL_FLAG_R5_CORE_HALT, 0); |
| 321 | if (ret) |
| 322 | dev_err(core->dev, "Core %d failed to stop\n", |
| 323 | core->tsp.proc_id); |
| 324 | |
| 325 | return ret; |
| 326 | } |
| 327 | |
| 328 | static int k3_r5f_core_run(struct k3_r5f_core *core) |
| 329 | { |
| 330 | int ret; |
| 331 | |
| 332 | ret = ti_sci_proc_set_control(&core->tsp, |
| 333 | 0, PROC_BOOT_CTRL_FLAG_R5_CORE_HALT); |
| 334 | if (ret) { |
| 335 | dev_err(core->dev, "Core %d failed to start\n", |
| 336 | core->tsp.proc_id); |
| 337 | return ret; |
| 338 | } |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | /** |
| 344 | * k3_r5f_start() - Start the remote processor |
| 345 | * @dev: rproc device pointer |
| 346 | * |
| 347 | * Return: 0 if all went ok, else return appropriate error |
| 348 | */ |
| 349 | static int k3_r5f_start(struct udevice *dev) |
| 350 | { |
| 351 | struct k3_r5f_core *core = dev_get_priv(dev); |
| 352 | struct k3_r5f_cluster *cluster = core->cluster; |
| 353 | int ret, c; |
| 354 | |
| 355 | dev_dbg(dev, "%s\n", __func__); |
| 356 | |
| 357 | ret = k3_r5f_core_sanity_check(core); |
| 358 | if (ret) |
| 359 | return ret; |
| 360 | |
| 361 | ret = k3_r5f_proc_request(core); |
| 362 | if (ret) |
| 363 | return ret; |
| 364 | |
| 365 | if (cluster->mode == CLUSTER_MODE_LOCKSTEP) { |
| 366 | if (is_primary_core(core)) { |
| 367 | for (c = NR_CORES - 1; c >= 0; c--) { |
| 368 | ret = k3_r5f_core_run(cluster->cores[c]); |
| 369 | if (ret) |
| 370 | goto unroll_core_run; |
| 371 | } |
| 372 | } else { |
| 373 | dev_err(dev, "Invalid op: Trying to start secondary core %d in lockstep mode\n", |
| 374 | core->tsp.proc_id); |
| 375 | ret = -EINVAL; |
| 376 | goto proc_release; |
| 377 | } |
| 378 | } else { |
| 379 | ret = k3_r5f_core_run(core); |
| 380 | if (ret) |
| 381 | goto proc_release; |
| 382 | } |
| 383 | |
| 384 | core->in_use = true; |
| 385 | |
| 386 | k3_r5f_proc_release(core); |
| 387 | return 0; |
| 388 | |
| 389 | unroll_core_run: |
| 390 | while (c < NR_CORES) { |
| 391 | k3_r5f_core_halt(cluster->cores[c]); |
| 392 | c++; |
| 393 | } |
| 394 | proc_release: |
| 395 | k3_r5f_proc_release(core); |
| 396 | |
| 397 | return ret; |
| 398 | } |
| 399 | |
| 400 | static int k3_r5f_split_reset(struct k3_r5f_core *core) |
| 401 | { |
| 402 | int ret; |
| 403 | |
| 404 | dev_dbg(dev, "%s\n", __func__); |
| 405 | |
| 406 | if (reset_assert(&core->reset)) |
| 407 | ret = -EINVAL; |
| 408 | |
| 409 | if (ti_sci_proc_power_domain_off(&core->tsp)) |
| 410 | ret = -EINVAL; |
| 411 | |
| 412 | return ret; |
| 413 | } |
| 414 | |
| 415 | static int k3_r5f_lockstep_reset(struct k3_r5f_cluster *cluster) |
| 416 | { |
| 417 | int ret = 0, c; |
| 418 | |
| 419 | dev_dbg(dev, "%s\n", __func__); |
| 420 | |
| 421 | for (c = 0; c < NR_CORES; c++) |
| 422 | if (reset_assert(&cluster->cores[c]->reset)) |
| 423 | ret = -EINVAL; |
| 424 | |
| 425 | /* disable PSC modules on all applicable cores */ |
| 426 | for (c = 0; c < NR_CORES; c++) |
| 427 | if (ti_sci_proc_power_domain_off(&cluster->cores[c]->tsp)) |
| 428 | ret = -EINVAL; |
| 429 | |
| 430 | return ret; |
| 431 | } |
| 432 | |
| 433 | static int k3_r5f_unprepare(struct udevice *dev) |
| 434 | { |
| 435 | struct k3_r5f_core *core = dev_get_priv(dev); |
| 436 | struct k3_r5f_cluster *cluster = core->cluster; |
| 437 | int ret; |
| 438 | |
| 439 | dev_dbg(dev, "%s\n", __func__); |
| 440 | |
| 441 | if (cluster->mode == CLUSTER_MODE_LOCKSTEP) { |
| 442 | if (is_primary_core(core)) |
| 443 | ret = k3_r5f_lockstep_reset(cluster); |
| 444 | } else { |
| 445 | ret = k3_r5f_split_reset(core); |
| 446 | } |
| 447 | |
| 448 | if (ret) |
| 449 | dev_warn(dev, "Unable to enable cores for TCM loading %d\n", |
| 450 | ret); |
| 451 | |
| 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | static int k3_r5f_stop(struct udevice *dev) |
| 456 | { |
| 457 | struct k3_r5f_core *core = dev_get_priv(dev); |
| 458 | struct k3_r5f_cluster *cluster = core->cluster; |
| 459 | int c, ret; |
| 460 | |
| 461 | dev_dbg(dev, "%s\n", __func__); |
| 462 | |
| 463 | ret = k3_r5f_proc_request(core); |
| 464 | if (ret) |
| 465 | return ret; |
| 466 | |
| 467 | core->in_use = false; |
| 468 | |
| 469 | if (cluster->mode == CLUSTER_MODE_LOCKSTEP) { |
| 470 | if (is_primary_core(core)) { |
| 471 | for (c = 0; c < NR_CORES; c++) |
| 472 | k3_r5f_core_halt(cluster->cores[c]); |
| 473 | } else { |
| 474 | dev_err(dev, "Invalid op: Trying to stop secondary core in lockstep mode\n"); |
| 475 | ret = -EINVAL; |
| 476 | goto proc_release; |
| 477 | } |
| 478 | } else { |
| 479 | k3_r5f_core_halt(core); |
| 480 | } |
| 481 | |
| 482 | ret = k3_r5f_unprepare(dev); |
| 483 | proc_release: |
| 484 | k3_r5f_proc_release(core); |
| 485 | return ret; |
| 486 | } |
| 487 | |
| 488 | static void *k3_r5f_da_to_va(struct udevice *dev, ulong da, ulong size) |
| 489 | { |
| 490 | struct k3_r5f_core *core = dev_get_priv(dev); |
| 491 | void __iomem *va = NULL; |
| 492 | phys_addr_t bus_addr; |
| 493 | u32 dev_addr, offset; |
| 494 | ulong mem_size; |
| 495 | int i; |
| 496 | |
| 497 | dev_dbg(dev, "%s\n", __func__); |
| 498 | |
| 499 | if (size <= 0) |
| 500 | return NULL; |
| 501 | |
| 502 | for (i = 0; i < core->num_mems; i++) { |
| 503 | bus_addr = core->mem[i].bus_addr; |
| 504 | dev_addr = core->mem[i].dev_addr; |
| 505 | mem_size = core->mem[i].size; |
| 506 | |
| 507 | if (da >= bus_addr && (da + size) <= (bus_addr + mem_size)) { |
| 508 | offset = da - bus_addr; |
| 509 | va = core->mem[i].cpu_addr + offset; |
| 510 | return (__force void *)va; |
| 511 | } |
| 512 | |
| 513 | if (da >= dev_addr && (da + size) <= (dev_addr + mem_size)) { |
| 514 | offset = da - dev_addr; |
| 515 | va = core->mem[i].cpu_addr + offset; |
| 516 | return (__force void *)va; |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | /* Assume it is DDR region and return da */ |
| 521 | return map_physmem(da, size, MAP_NOCACHE); |
| 522 | } |
| 523 | |
| 524 | static int k3_r5f_init(struct udevice *dev) |
| 525 | { |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | static int k3_r5f_reset(struct udevice *dev) |
| 530 | { |
| 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | static const struct dm_rproc_ops k3_r5f_rproc_ops = { |
| 535 | .init = k3_r5f_init, |
| 536 | .reset = k3_r5f_reset, |
| 537 | .start = k3_r5f_start, |
| 538 | .stop = k3_r5f_stop, |
| 539 | .load = k3_r5f_load, |
| 540 | .device_to_virt = k3_r5f_da_to_va, |
| 541 | }; |
| 542 | |
| 543 | static int k3_r5f_rproc_configure(struct k3_r5f_core *core) |
| 544 | { |
| 545 | struct k3_r5f_cluster *cluster = core->cluster; |
| 546 | u32 set_cfg = 0, clr_cfg = 0, cfg, ctrl, sts; |
Suman Anna | 9ff2930 | 2020-03-10 20:24:29 -0500 | [diff] [blame] | 547 | bool lockstep_permitted; |
Lokesh Vutla | 58633f1 | 2019-09-04 16:01:34 +0530 | [diff] [blame] | 548 | u64 boot_vec = 0; |
| 549 | int ret; |
| 550 | |
| 551 | dev_dbg(dev, "%s\n", __func__); |
| 552 | |
| 553 | ret = ti_sci_proc_request(&core->tsp); |
| 554 | if (ret < 0) |
| 555 | return ret; |
| 556 | |
| 557 | /* Do not touch boot vector now. Load will take care of it. */ |
| 558 | clr_cfg |= PROC_BOOT_CFG_FLAG_GEN_IGN_BOOTVECTOR; |
| 559 | |
| 560 | ret = ti_sci_proc_get_status(&core->tsp, &boot_vec, &cfg, &ctrl, &sts); |
| 561 | if (ret) |
| 562 | goto out; |
| 563 | |
| 564 | /* Sanity check for Lockstep mode */ |
Suman Anna | 9ff2930 | 2020-03-10 20:24:29 -0500 | [diff] [blame] | 565 | lockstep_permitted = !!(sts & |
| 566 | PROC_BOOT_STATUS_FLAG_R5_LOCKSTEP_PERMITTED); |
| 567 | if (cluster->mode && is_primary_core(core) && !lockstep_permitted) { |
Lokesh Vutla | 58633f1 | 2019-09-04 16:01:34 +0530 | [diff] [blame] | 568 | dev_err(core->dev, "LockStep mode not permitted on this device\n"); |
| 569 | ret = -EINVAL; |
| 570 | goto out; |
| 571 | } |
| 572 | |
| 573 | /* Primary core only configuration */ |
| 574 | if (is_primary_core(core)) { |
| 575 | /* always enable ARM mode */ |
| 576 | clr_cfg |= PROC_BOOT_CFG_FLAG_R5_TEINIT; |
| 577 | if (cluster->mode == CLUSTER_MODE_LOCKSTEP) |
| 578 | set_cfg |= PROC_BOOT_CFG_FLAG_R5_LOCKSTEP; |
Suman Anna | 9ff2930 | 2020-03-10 20:24:29 -0500 | [diff] [blame] | 579 | else if (lockstep_permitted) |
Lokesh Vutla | 58633f1 | 2019-09-04 16:01:34 +0530 | [diff] [blame] | 580 | clr_cfg |= PROC_BOOT_CFG_FLAG_R5_LOCKSTEP; |
| 581 | } |
| 582 | |
| 583 | if (core->atcm_enable) |
| 584 | set_cfg |= PROC_BOOT_CFG_FLAG_R5_ATCM_EN; |
| 585 | else |
| 586 | clr_cfg |= PROC_BOOT_CFG_FLAG_R5_ATCM_EN; |
| 587 | |
| 588 | if (core->btcm_enable) |
| 589 | set_cfg |= PROC_BOOT_CFG_FLAG_R5_BTCM_EN; |
| 590 | else |
| 591 | clr_cfg |= PROC_BOOT_CFG_FLAG_R5_BTCM_EN; |
| 592 | |
| 593 | if (core->loczrama) |
| 594 | set_cfg |= PROC_BOOT_CFG_FLAG_R5_TCM_RSTBASE; |
| 595 | else |
| 596 | clr_cfg |= PROC_BOOT_CFG_FLAG_R5_TCM_RSTBASE; |
| 597 | |
| 598 | ret = k3_r5f_core_halt(core); |
| 599 | if (ret) |
| 600 | goto out; |
| 601 | |
| 602 | ret = ti_sci_proc_set_config(&core->tsp, boot_vec, set_cfg, clr_cfg); |
| 603 | out: |
| 604 | ti_sci_proc_release(&core->tsp); |
| 605 | return ret; |
| 606 | } |
| 607 | |
| 608 | static int ti_sci_proc_of_to_priv(struct udevice *dev, struct ti_sci_proc *tsp) |
| 609 | { |
| 610 | u32 ids[2]; |
| 611 | int ret; |
| 612 | |
| 613 | dev_dbg(dev, "%s\n", __func__); |
| 614 | |
| 615 | tsp->sci = ti_sci_get_by_phandle(dev, "ti,sci"); |
| 616 | if (IS_ERR(tsp->sci)) { |
| 617 | dev_err(dev, "ti_sci get failed: %ld\n", PTR_ERR(tsp->sci)); |
| 618 | return PTR_ERR(tsp->sci); |
| 619 | } |
| 620 | |
| 621 | ret = dev_read_u32_array(dev, "ti,sci-proc-ids", ids, 2); |
| 622 | if (ret) { |
| 623 | dev_err(dev, "Proc IDs not populated %d\n", ret); |
| 624 | return ret; |
| 625 | } |
| 626 | |
| 627 | tsp->ops = &tsp->sci->ops.proc_ops; |
| 628 | tsp->proc_id = ids[0]; |
| 629 | tsp->host_id = ids[1]; |
| 630 | tsp->dev_id = dev_read_u32_default(dev, "ti,sci-dev-id", |
| 631 | TI_SCI_RESOURCE_NULL); |
| 632 | if (tsp->dev_id == TI_SCI_RESOURCE_NULL) { |
| 633 | dev_err(dev, "Device ID not populated %d\n", ret); |
| 634 | return -ENODEV; |
| 635 | } |
| 636 | |
| 637 | return 0; |
| 638 | } |
| 639 | |
| 640 | static int k3_r5f_of_to_priv(struct k3_r5f_core *core) |
| 641 | { |
| 642 | int ret; |
| 643 | |
| 644 | dev_dbg(dev, "%s\n", __func__); |
| 645 | |
| 646 | core->atcm_enable = dev_read_u32_default(core->dev, "atcm-enable", 0); |
| 647 | core->btcm_enable = dev_read_u32_default(core->dev, "btcm-enable", 1); |
| 648 | core->loczrama = dev_read_u32_default(core->dev, "loczrama", 1); |
| 649 | |
| 650 | ret = ti_sci_proc_of_to_priv(core->dev, &core->tsp); |
| 651 | if (ret) |
| 652 | return ret; |
| 653 | |
| 654 | ret = reset_get_by_index(core->dev, 0, &core->reset); |
| 655 | if (ret) { |
| 656 | dev_err(core->dev, "Reset lines not available: %d\n", ret); |
| 657 | return ret; |
| 658 | } |
| 659 | |
| 660 | return 0; |
| 661 | } |
| 662 | |
| 663 | static int k3_r5f_core_of_get_memories(struct k3_r5f_core *core) |
| 664 | { |
| 665 | static const char * const mem_names[] = {"atcm", "btcm"}; |
| 666 | struct udevice *dev = core->dev; |
| 667 | int i; |
| 668 | |
| 669 | dev_dbg(dev, "%s\n", __func__); |
| 670 | |
| 671 | core->num_mems = ARRAY_SIZE(mem_names); |
| 672 | core->mem = calloc(core->num_mems, sizeof(*core->mem)); |
| 673 | if (!core->mem) |
| 674 | return -ENOMEM; |
| 675 | |
| 676 | for (i = 0; i < core->num_mems; i++) { |
| 677 | core->mem[i].bus_addr = dev_read_addr_size_name(dev, |
| 678 | mem_names[i], |
| 679 | (fdt_addr_t *)&core->mem[i].size); |
| 680 | if (core->mem[i].bus_addr == FDT_ADDR_T_NONE) { |
| 681 | dev_err(dev, "%s bus address not found\n", |
| 682 | mem_names[i]); |
| 683 | return -EINVAL; |
| 684 | } |
| 685 | core->mem[i].cpu_addr = map_physmem(core->mem[i].bus_addr, |
| 686 | core->mem[i].size, |
| 687 | MAP_NOCACHE); |
| 688 | if (!strcmp(mem_names[i], "atcm")) { |
| 689 | core->mem[i].dev_addr = core->loczrama ? |
| 690 | 0 : K3_R5_TCM_DEV_ADDR; |
| 691 | } else { |
| 692 | core->mem[i].dev_addr = core->loczrama ? |
| 693 | K3_R5_TCM_DEV_ADDR : 0; |
| 694 | } |
| 695 | |
| 696 | dev_dbg(dev, "memory %8s: bus addr %pa size 0x%zx va %p da 0x%x\n", |
| 697 | mem_names[i], &core->mem[i].bus_addr, |
| 698 | core->mem[i].size, core->mem[i].cpu_addr, |
| 699 | core->mem[i].dev_addr); |
| 700 | } |
| 701 | |
| 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | /** |
| 706 | * k3_r5f_probe() - Basic probe |
| 707 | * @dev: corresponding k3 remote processor device |
| 708 | * |
| 709 | * Return: 0 if all goes good, else appropriate error message. |
| 710 | */ |
| 711 | static int k3_r5f_probe(struct udevice *dev) |
| 712 | { |
| 713 | struct k3_r5f_cluster *cluster = dev_get_priv(dev->parent); |
| 714 | struct k3_r5f_core *core = dev_get_priv(dev); |
| 715 | bool r_state; |
| 716 | int ret; |
| 717 | |
| 718 | dev_dbg(dev, "%s\n", __func__); |
| 719 | |
| 720 | core->dev = dev; |
| 721 | ret = k3_r5f_of_to_priv(core); |
| 722 | if (ret) |
| 723 | return ret; |
| 724 | |
| 725 | core->cluster = cluster; |
| 726 | /* Assume Primary core gets probed first */ |
| 727 | if (!cluster->cores[0]) |
| 728 | cluster->cores[0] = core; |
| 729 | else |
| 730 | cluster->cores[1] = core; |
| 731 | |
| 732 | ret = k3_r5f_core_of_get_memories(core); |
| 733 | if (ret) { |
| 734 | dev_err(dev, "Rproc getting internal memories failed\n"); |
| 735 | return ret; |
| 736 | } |
| 737 | |
| 738 | ret = core->tsp.sci->ops.dev_ops.is_on(core->tsp.sci, core->tsp.dev_id, |
| 739 | &r_state, &core->in_use); |
| 740 | if (ret) |
| 741 | return ret; |
| 742 | |
| 743 | if (core->in_use) { |
| 744 | dev_info(dev, "Core %d is already in use. No rproc commands work\n", |
| 745 | core->tsp.proc_id); |
| 746 | return 0; |
| 747 | } |
| 748 | |
| 749 | /* Make sure Local reset is asserted. Redundant? */ |
| 750 | reset_assert(&core->reset); |
| 751 | |
| 752 | ret = k3_r5f_rproc_configure(core); |
| 753 | if (ret) { |
| 754 | dev_err(dev, "rproc configure failed %d\n", ret); |
| 755 | return ret; |
| 756 | } |
| 757 | |
| 758 | dev_dbg(dev, "Remoteproc successfully probed\n"); |
| 759 | |
| 760 | return 0; |
| 761 | } |
| 762 | |
| 763 | static int k3_r5f_remove(struct udevice *dev) |
| 764 | { |
| 765 | struct k3_r5f_core *core = dev_get_priv(dev); |
| 766 | |
| 767 | free(core->mem); |
| 768 | |
| 769 | ti_sci_proc_release(&core->tsp); |
| 770 | |
| 771 | return 0; |
| 772 | } |
| 773 | |
| 774 | static const struct udevice_id k3_r5f_rproc_ids[] = { |
| 775 | { .compatible = "ti,am654-r5f"}, |
| 776 | { .compatible = "ti,j721e-r5f"}, |
| 777 | {} |
| 778 | }; |
| 779 | |
| 780 | U_BOOT_DRIVER(k3_r5f_rproc) = { |
| 781 | .name = "k3_r5f_rproc", |
| 782 | .of_match = k3_r5f_rproc_ids, |
| 783 | .id = UCLASS_REMOTEPROC, |
| 784 | .ops = &k3_r5f_rproc_ops, |
| 785 | .probe = k3_r5f_probe, |
| 786 | .remove = k3_r5f_remove, |
| 787 | .priv_auto_alloc_size = sizeof(struct k3_r5f_core), |
| 788 | }; |
| 789 | |
| 790 | static int k3_r5f_cluster_probe(struct udevice *dev) |
| 791 | { |
| 792 | struct k3_r5f_cluster *cluster = dev_get_priv(dev); |
| 793 | |
| 794 | dev_dbg(dev, "%s\n", __func__); |
| 795 | |
| 796 | cluster->mode = dev_read_u32_default(dev, "lockstep-mode", |
| 797 | CLUSTER_MODE_LOCKSTEP); |
| 798 | |
| 799 | if (device_get_child_count(dev) != 2) { |
| 800 | dev_err(dev, "Invalid number of R5 cores"); |
| 801 | return -EINVAL; |
| 802 | } |
| 803 | |
| 804 | dev_dbg(dev, "%s: Cluster successfully probed in %s mode\n", |
| 805 | __func__, cluster->mode ? "lockstep" : "split"); |
| 806 | |
| 807 | return 0; |
| 808 | } |
| 809 | |
| 810 | static const struct udevice_id k3_r5fss_ids[] = { |
| 811 | { .compatible = "ti,am654-r5fss"}, |
| 812 | { .compatible = "ti,j721e-r5fss"}, |
| 813 | {} |
| 814 | }; |
| 815 | |
| 816 | U_BOOT_DRIVER(k3_r5fss) = { |
| 817 | .name = "k3_r5fss", |
| 818 | .of_match = k3_r5fss_ids, |
| 819 | .id = UCLASS_MISC, |
| 820 | .probe = k3_r5f_cluster_probe, |
| 821 | .priv_auto_alloc_size = sizeof(struct k3_r5f_cluster), |
Suman Anna | 41e8986 | 2020-03-10 18:34:54 -0500 | [diff] [blame] | 822 | .flags = DM_FLAG_DEFAULT_PD_CTRL_OFF, |
Lokesh Vutla | 58633f1 | 2019-09-04 16:01:34 +0530 | [diff] [blame] | 823 | }; |