1 // SPDX-License-Identifier: GPL-2.0+
3 * Texas Instruments' K3 DSP Remoteproc driver
5 * Copyright (C) 2018-2020 Texas Instruments Incorporated - http://www.ti.com/
6 * Lokesh Vutla <lokeshvutla@ti.com>
7 * Suman Anna <s-anna@ti.com>
14 #include <remoteproc.h>
19 #include <power-domain.h>
20 #include <dm/device_compat.h>
21 #include <linux/err.h>
22 #include <linux/sizes.h>
23 #include <linux/soc/ti/ti_sci_protocol.h>
24 #include "ti_sci_proc.h"
26 #define KEYSTONE_RPROC_LOCAL_ADDRESS_MASK (SZ_16M - 1)
29 * struct k3_dsp_mem - internal memory structure
30 * @cpu_addr: MPU virtual address of the memory region
31 * @bus_addr: Bus address used to access the memory region
32 * @dev_addr: Device address from remoteproc view
33 * @size: Size of the memory region
36 void __iomem *cpu_addr;
43 * struct k3_dsp_boot_data - internal data structure used for boot
44 * @boot_align_addr: Boot vector address alignment granularity
45 * @uses_lreset: Flag to denote the need for local reset management
47 struct k3_dsp_boot_data {
53 * struct k3_dsp_privdata - Structure representing Remote processor data.
54 * @rproc_rst: rproc reset control data
55 * @tsp: Pointer to TISCI proc contrl handle
56 * @data: Pointer to DSP specific boot data structure
57 * @mem: Array of available memories
58 * @num_mem: Number of available memories
60 struct k3_dsp_privdata {
61 struct reset_ctl dsp_rst;
62 struct ti_sci_proc tsp;
63 struct k3_dsp_boot_data *data;
64 struct k3_dsp_mem *mem;
69 * The C66x DSP cores have a local reset that affects only the CPU, and a
70 * generic module reset that powers on the device and allows the DSP internal
71 * memories to be accessed while the local reset is asserted. This function is
72 * used to release the global reset on C66x DSPs to allow loading into the DSP
73 * internal RAMs. This helper function is invoked in k3_dsp_load() before any
74 * actual firmware loading and is undone only in k3_dsp_stop(). The local reset
75 * on C71x cores is a no-op and the global reset cannot be released on C71x
76 * cores until after the firmware images are loaded, so this function does
77 * nothing for C71x cores.
79 static int k3_dsp_prepare(struct udevice *dev)
81 struct k3_dsp_privdata *dsp = dev_get_priv(dev);
82 struct k3_dsp_boot_data *data = dsp->data;
85 /* local reset is no-op on C71x processors */
86 if (!data->uses_lreset)
89 ret = ti_sci_proc_power_domain_on(&dsp->tsp);
91 dev_err(dev, "cannot enable internal RAM loading, ret = %d\n",
98 * This function is the counterpart to k3_dsp_prepare() and is used to assert
99 * the global reset on C66x DSP cores (no-op for C71x DSP cores). This completes
100 * the second step of powering down the C66x DSP cores. The cores themselves
101 * are halted through the local reset in first step. This function is invoked
102 * in k3_dsp_stop() after the local reset is asserted.
104 static int k3_dsp_unprepare(struct udevice *dev)
106 struct k3_dsp_privdata *dsp = dev_get_priv(dev);
107 struct k3_dsp_boot_data *data = dsp->data;
109 /* local reset is no-op on C71x processors */
110 if (!data->uses_lreset)
113 return ti_sci_proc_power_domain_off(&dsp->tsp);
117 * k3_dsp_load() - Load up the Remote processor image
118 * @dev: rproc device pointer
119 * @addr: Address at which image is available
120 * @size: size of the image
122 * Return: 0 if all goes good, else appropriate error message.
124 static int k3_dsp_load(struct udevice *dev, ulong addr, ulong size)
126 struct k3_dsp_privdata *dsp = dev_get_priv(dev);
127 struct k3_dsp_boot_data *data = dsp->data;
131 dev_dbg(dev, "%s addr = 0x%lx, size = 0x%lx\n", __func__, addr, size);
132 ret = ti_sci_proc_request(&dsp->tsp);
136 ret = k3_dsp_prepare(dev);
138 dev_err(dev, "DSP prepare failed for core %d\n",
143 ret = rproc_elf_load_image(dev, addr, size);
145 dev_err(dev, "Loading elf failed %d\n", ret);
149 boot_vector = rproc_elf_get_boot_addr(dev, addr);
150 if (boot_vector & (data->boot_align_addr - 1)) {
152 dev_err(dev, "Boot vector 0x%x not aligned on 0x%x boundary\n",
153 boot_vector, data->boot_align_addr);
157 dev_dbg(dev, "%s: Boot vector = 0x%x\n", __func__, boot_vector);
159 ret = ti_sci_proc_set_config(&dsp->tsp, boot_vector, 0, 0);
162 k3_dsp_unprepare(dev);
164 ti_sci_proc_release(&dsp->tsp);
169 * k3_dsp_start() - Start the remote processor
170 * @dev: rproc device pointer
172 * Return: 0 if all went ok, else return appropriate error
174 static int k3_dsp_start(struct udevice *dev)
176 struct k3_dsp_privdata *dsp = dev_get_priv(dev);
177 struct k3_dsp_boot_data *data = dsp->data;
180 dev_dbg(dev, "%s\n", __func__);
182 ret = ti_sci_proc_request(&dsp->tsp);
186 if (!data->uses_lreset) {
187 ret = ti_sci_proc_power_domain_on(&dsp->tsp);
192 ret = reset_deassert(&dsp->dsp_rst);
194 if (!data->uses_lreset)
195 ti_sci_proc_power_domain_off(&dsp->tsp);
199 ti_sci_proc_release(&dsp->tsp);
204 static int k3_dsp_stop(struct udevice *dev)
206 struct k3_dsp_privdata *dsp = dev_get_priv(dev);
208 dev_dbg(dev, "%s\n", __func__);
210 ti_sci_proc_request(&dsp->tsp);
211 reset_assert(&dsp->dsp_rst);
212 ti_sci_proc_power_domain_off(&dsp->tsp);
213 ti_sci_proc_release(&dsp->tsp);
219 * k3_dsp_init() - Initialize the remote processor
220 * @dev: rproc device pointer
222 * Return: 0 if all went ok, else return appropriate error
224 static int k3_dsp_init(struct udevice *dev)
226 dev_dbg(dev, "%s\n", __func__);
231 static int k3_dsp_reset(struct udevice *dev)
233 dev_dbg(dev, "%s\n", __func__);
238 static void *k3_dsp_da_to_va(struct udevice *dev, ulong da, ulong len)
240 struct k3_dsp_privdata *dsp = dev_get_priv(dev);
241 phys_addr_t bus_addr, dev_addr;
242 void __iomem *va = NULL;
247 dev_dbg(dev, "%s\n", __func__);
252 for (i = 0; i < dsp->num_mems; i++) {
253 bus_addr = dsp->mem[i].bus_addr;
254 dev_addr = dsp->mem[i].dev_addr;
255 size = dsp->mem[i].size;
257 if (da >= dev_addr && ((da + len) <= (dev_addr + size))) {
258 offset = da - dev_addr;
259 va = dsp->mem[i].cpu_addr + offset;
260 return (__force void *)va;
263 if (da >= bus_addr && (da + len) <= (bus_addr + size)) {
264 offset = da - bus_addr;
265 va = dsp->mem[i].cpu_addr + offset;
266 return (__force void *)va;
270 /* Assume it is DDR region and return da */
271 return map_physmem(da, len, MAP_NOCACHE);
274 static const struct dm_rproc_ops k3_dsp_ops = {
277 .start = k3_dsp_start,
279 .reset = k3_dsp_reset,
280 .device_to_virt = k3_dsp_da_to_va,
283 static int ti_sci_proc_of_to_priv(struct udevice *dev, struct ti_sci_proc *tsp)
288 dev_dbg(dev, "%s\n", __func__);
290 tsp->sci = ti_sci_get_by_phandle(dev, "ti,sci");
291 if (IS_ERR(tsp->sci)) {
292 dev_err(dev, "ti_sci get failed: %ld\n", PTR_ERR(tsp->sci));
293 return PTR_ERR(tsp->sci);
296 ret = dev_read_u32_array(dev, "ti,sci-proc-ids", ids, 2);
298 dev_err(dev, "Proc IDs not populated %d\n", ret);
302 tsp->ops = &tsp->sci->ops.proc_ops;
303 tsp->proc_id = ids[0];
304 tsp->host_id = ids[1];
305 tsp->dev_id = dev_read_u32_default(dev, "ti,sci-dev-id",
306 TI_SCI_RESOURCE_NULL);
307 if (tsp->dev_id == TI_SCI_RESOURCE_NULL) {
308 dev_err(dev, "Device ID not populated %d\n", ret);
315 static int k3_dsp_of_get_memories(struct udevice *dev)
317 static const char * const mem_names[] = {"l2sram", "l1pram", "l1dram"};
318 struct k3_dsp_privdata *dsp = dev_get_priv(dev);
321 dev_dbg(dev, "%s\n", __func__);
323 dsp->num_mems = ARRAY_SIZE(mem_names);
324 dsp->mem = calloc(dsp->num_mems, sizeof(*dsp->mem));
328 for (i = 0; i < dsp->num_mems; i++) {
329 /* C71 cores only have a L1P Cache, there are no L1P SRAMs */
330 if (device_is_compatible(dev, "ti,j721e-c71-dsp") &&
331 !strcmp(mem_names[i], "l1pram")) {
332 dsp->mem[i].bus_addr = FDT_ADDR_T_NONE;
333 dsp->mem[i].dev_addr = FDT_ADDR_T_NONE;
334 dsp->mem[i].cpu_addr = NULL;
335 dsp->mem[i].size = 0;
339 dsp->mem[i].bus_addr = dev_read_addr_size_name(dev, mem_names[i],
340 (fdt_addr_t *)&dsp->mem[i].size);
341 if (dsp->mem[i].bus_addr == FDT_ADDR_T_NONE) {
342 dev_err(dev, "%s bus address not found\n", mem_names[i]);
345 dsp->mem[i].cpu_addr = map_physmem(dsp->mem[i].bus_addr,
348 dsp->mem[i].dev_addr = dsp->mem[i].bus_addr &
349 KEYSTONE_RPROC_LOCAL_ADDRESS_MASK;
351 dev_dbg(dev, "memory %8s: bus addr %pa size 0x%zx va %p da %pa\n",
352 mem_names[i], &dsp->mem[i].bus_addr,
353 dsp->mem[i].size, dsp->mem[i].cpu_addr,
354 &dsp->mem[i].dev_addr);
361 * k3_of_to_priv() - generate private data from device tree
362 * @dev: corresponding k3 dsp processor device
363 * @dsp: pointer to driver specific private data
365 * Return: 0 if all goes good, else appropriate error message.
367 static int k3_dsp_of_to_priv(struct udevice *dev, struct k3_dsp_privdata *dsp)
371 dev_dbg(dev, "%s\n", __func__);
373 ret = reset_get_by_index(dev, 0, &dsp->dsp_rst);
375 dev_err(dev, "reset_get() failed: %d\n", ret);
379 ret = ti_sci_proc_of_to_priv(dev, &dsp->tsp);
383 ret = k3_dsp_of_get_memories(dev);
387 dsp->data = (struct k3_dsp_boot_data *)dev_get_driver_data(dev);
393 * k3_dsp_probe() - Basic probe
394 * @dev: corresponding k3 remote processor device
396 * Return: 0 if all goes good, else appropriate error message.
398 static int k3_dsp_probe(struct udevice *dev)
400 struct k3_dsp_privdata *dsp;
403 dev_dbg(dev, "%s\n", __func__);
405 dsp = dev_get_priv(dev);
407 ret = k3_dsp_of_to_priv(dev, dsp);
409 dev_dbg(dev, "%s: Probe failed with error %d\n", __func__, ret);
414 * The DSP local resets are deasserted by default on Power-On-Reset.
415 * Assert the local resets to ensure the DSPs don't execute bogus code
416 * in .load() callback when the module reset is released to support
417 * internal memory loading. This is needed for C66x DSPs, and is a
418 * no-op on C71x DSPs.
420 reset_assert(&dsp->dsp_rst);
422 dev_dbg(dev, "Remoteproc successfully probed\n");
427 static int k3_dsp_remove(struct udevice *dev)
429 struct k3_dsp_privdata *dsp = dev_get_priv(dev);
436 static const struct k3_dsp_boot_data c66_data = {
437 .boot_align_addr = SZ_1K,
441 static const struct k3_dsp_boot_data c71_data = {
442 .boot_align_addr = SZ_2M,
443 .uses_lreset = false,
446 static const struct udevice_id k3_dsp_ids[] = {
447 { .compatible = "ti,j721e-c66-dsp", .data = (ulong)&c66_data, },
448 { .compatible = "ti,j721e-c71-dsp", .data = (ulong)&c71_data, },
452 U_BOOT_DRIVER(k3_dsp) = {
454 .of_match = k3_dsp_ids,
455 .id = UCLASS_REMOTEPROC,
457 .probe = k3_dsp_probe,
458 .remove = k3_dsp_remove,
459 .priv_auto_alloc_size = sizeof(struct k3_dsp_privdata),