blob: 1fc8193ad93fc290f992a7437a23ff842e236019 [file] [log] [blame]
Lokesh Vutla2c771852019-09-04 16:01:36 +05301// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Texas Instruments' K3 DSP Remoteproc driver
4 *
Suman Annae45fa382020-03-10 16:05:54 -05005 * Copyright (C) 2018-2020 Texas Instruments Incorporated - http://www.ti.com/
Lokesh Vutla2c771852019-09-04 16:01:36 +05306 * Lokesh Vutla <lokeshvutla@ti.com>
Suman Annabe7fa2d2020-03-10 16:05:56 -05007 * Suman Anna <s-anna@ti.com>
Lokesh Vutla2c771852019-09-04 16:01:36 +05308 */
9
10#include <common.h>
11#include <dm.h>
Simon Glass9bc15642020-02-03 07:36:16 -070012#include <malloc.h>
Lokesh Vutla2c771852019-09-04 16:01:36 +053013#include <remoteproc.h>
14#include <errno.h>
15#include <clk.h>
16#include <reset.h>
17#include <asm/io.h>
18#include <power-domain.h>
Simon Glass9bc15642020-02-03 07:36:16 -070019#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070020#include <linux/err.h>
Suman Annae45fa382020-03-10 16:05:54 -050021#include <linux/sizes.h>
Lokesh Vutla2c771852019-09-04 16:01:36 +053022#include <linux/soc/ti/ti_sci_protocol.h>
23#include "ti_sci_proc.h"
24
25#define KEYSTONE_RPROC_LOCAL_ADDRESS_MASK (SZ_16M - 1)
26
27/**
28 * struct k3_dsp_mem - internal memory structure
29 * @cpu_addr: MPU virtual address of the memory region
30 * @bus_addr: Bus address used to access the memory region
31 * @dev_addr: Device address from remoteproc view
32 * @size: Size of the memory region
33 */
34struct k3_dsp_mem {
35 void __iomem *cpu_addr;
36 phys_addr_t bus_addr;
37 phys_addr_t dev_addr;
38 size_t size;
39};
40
41/**
Suman Annae45fa382020-03-10 16:05:54 -050042 * struct k3_dsp_boot_data - internal data structure used for boot
43 * @boot_align_addr: Boot vector address alignment granularity
Suman Annabe7fa2d2020-03-10 16:05:56 -050044 * @uses_lreset: Flag to denote the need for local reset management
Suman Annae45fa382020-03-10 16:05:54 -050045 */
46struct k3_dsp_boot_data {
47 u32 boot_align_addr;
Suman Annabe7fa2d2020-03-10 16:05:56 -050048 bool uses_lreset;
Suman Annae45fa382020-03-10 16:05:54 -050049};
50
51/**
Lokesh Vutla2c771852019-09-04 16:01:36 +053052 * struct k3_dsp_privdata - Structure representing Remote processor data.
53 * @rproc_rst: rproc reset control data
54 * @tsp: Pointer to TISCI proc contrl handle
Suman Annae45fa382020-03-10 16:05:54 -050055 * @data: Pointer to DSP specific boot data structure
Lokesh Vutla2c771852019-09-04 16:01:36 +053056 * @mem: Array of available memories
57 * @num_mem: Number of available memories
58 */
59struct k3_dsp_privdata {
60 struct reset_ctl dsp_rst;
61 struct ti_sci_proc tsp;
Suman Annae45fa382020-03-10 16:05:54 -050062 struct k3_dsp_boot_data *data;
Lokesh Vutla2c771852019-09-04 16:01:36 +053063 struct k3_dsp_mem *mem;
64 int num_mems;
65};
66
Suman Annabe7fa2d2020-03-10 16:05:56 -050067/*
68 * The C66x DSP cores have a local reset that affects only the CPU, and a
69 * generic module reset that powers on the device and allows the DSP internal
70 * memories to be accessed while the local reset is asserted. This function is
71 * used to release the global reset on C66x DSPs to allow loading into the DSP
72 * internal RAMs. This helper function is invoked in k3_dsp_load() before any
73 * actual firmware loading and is undone only in k3_dsp_stop(). The local reset
74 * on C71x cores is a no-op and the global reset cannot be released on C71x
75 * cores until after the firmware images are loaded, so this function does
76 * nothing for C71x cores.
77 */
78static int k3_dsp_prepare(struct udevice *dev)
79{
80 struct k3_dsp_privdata *dsp = dev_get_priv(dev);
81 struct k3_dsp_boot_data *data = dsp->data;
82 int ret;
83
84 /* local reset is no-op on C71x processors */
85 if (!data->uses_lreset)
86 return 0;
87
88 ret = ti_sci_proc_power_domain_on(&dsp->tsp);
89 if (ret)
90 dev_err(dev, "cannot enable internal RAM loading, ret = %d\n",
91 ret);
92
93 return ret;
94}
95
96/*
97 * This function is the counterpart to k3_dsp_prepare() and is used to assert
98 * the global reset on C66x DSP cores (no-op for C71x DSP cores). This completes
99 * the second step of powering down the C66x DSP cores. The cores themselves
100 * are halted through the local reset in first step. This function is invoked
101 * in k3_dsp_stop() after the local reset is asserted.
102 */
103static int k3_dsp_unprepare(struct udevice *dev)
104{
105 struct k3_dsp_privdata *dsp = dev_get_priv(dev);
106 struct k3_dsp_boot_data *data = dsp->data;
107
108 /* local reset is no-op on C71x processors */
109 if (!data->uses_lreset)
110 return 0;
111
112 return ti_sci_proc_power_domain_off(&dsp->tsp);
113}
114
Lokesh Vutla2c771852019-09-04 16:01:36 +0530115/**
116 * k3_dsp_load() - Load up the Remote processor image
117 * @dev: rproc device pointer
118 * @addr: Address at which image is available
119 * @size: size of the image
120 *
121 * Return: 0 if all goes good, else appropriate error message.
122 */
123static int k3_dsp_load(struct udevice *dev, ulong addr, ulong size)
124{
125 struct k3_dsp_privdata *dsp = dev_get_priv(dev);
Suman Annae45fa382020-03-10 16:05:54 -0500126 struct k3_dsp_boot_data *data = dsp->data;
Lokesh Vutla2c771852019-09-04 16:01:36 +0530127 u32 boot_vector;
128 int ret;
129
130 dev_dbg(dev, "%s addr = 0x%lx, size = 0x%lx\n", __func__, addr, size);
131 ret = ti_sci_proc_request(&dsp->tsp);
132 if (ret)
133 return ret;
134
Suman Annabe7fa2d2020-03-10 16:05:56 -0500135 ret = k3_dsp_prepare(dev);
136 if (ret) {
137 dev_err(dev, "DSP prepare failed for core %d\n",
138 dsp->tsp.proc_id);
139 goto proc_release;
140 }
141
Lokesh Vutla2c771852019-09-04 16:01:36 +0530142 ret = rproc_elf_load_image(dev, addr, size);
143 if (ret < 0) {
144 dev_err(dev, "Loading elf failed %d\n", ret);
Suman Annabe7fa2d2020-03-10 16:05:56 -0500145 goto unprepare;
Lokesh Vutla2c771852019-09-04 16:01:36 +0530146 }
147
148 boot_vector = rproc_elf_get_boot_addr(dev, addr);
Suman Annae45fa382020-03-10 16:05:54 -0500149 if (boot_vector & (data->boot_align_addr - 1)) {
150 ret = -EINVAL;
151 dev_err(dev, "Boot vector 0x%x not aligned on 0x%x boundary\n",
152 boot_vector, data->boot_align_addr);
153 goto proc_release;
154 }
Lokesh Vutla2c771852019-09-04 16:01:36 +0530155
156 dev_dbg(dev, "%s: Boot vector = 0x%x\n", __func__, boot_vector);
157
158 ret = ti_sci_proc_set_config(&dsp->tsp, boot_vector, 0, 0);
Suman Annabe7fa2d2020-03-10 16:05:56 -0500159unprepare:
160 if (ret)
161 k3_dsp_unprepare(dev);
Lokesh Vutla2c771852019-09-04 16:01:36 +0530162proc_release:
163 ti_sci_proc_release(&dsp->tsp);
164 return ret;
165}
166
167/**
168 * k3_dsp_start() - Start the remote processor
169 * @dev: rproc device pointer
170 *
171 * Return: 0 if all went ok, else return appropriate error
172 */
173static int k3_dsp_start(struct udevice *dev)
174{
175 struct k3_dsp_privdata *dsp = dev_get_priv(dev);
Suman Annabe7fa2d2020-03-10 16:05:56 -0500176 struct k3_dsp_boot_data *data = dsp->data;
Lokesh Vutla2c771852019-09-04 16:01:36 +0530177 int ret;
178
179 dev_dbg(dev, "%s\n", __func__);
180
181 ret = ti_sci_proc_request(&dsp->tsp);
182 if (ret)
183 return ret;
Suman Anna83d8fab2020-03-10 16:05:53 -0500184
Suman Annabe7fa2d2020-03-10 16:05:56 -0500185 if (!data->uses_lreset) {
186 ret = ti_sci_proc_power_domain_on(&dsp->tsp);
187 if (ret)
188 goto proc_release;
189 }
Lokesh Vutla2c771852019-09-04 16:01:36 +0530190
191 ret = reset_deassert(&dsp->dsp_rst);
Suman Annabe7fa2d2020-03-10 16:05:56 -0500192 if (ret) {
193 if (!data->uses_lreset)
194 ti_sci_proc_power_domain_off(&dsp->tsp);
195 }
Lokesh Vutla2c771852019-09-04 16:01:36 +0530196
197proc_release:
198 ti_sci_proc_release(&dsp->tsp);
199
200 return ret;
201}
202
203static int k3_dsp_stop(struct udevice *dev)
204{
205 struct k3_dsp_privdata *dsp = dev_get_priv(dev);
206
207 dev_dbg(dev, "%s\n", __func__);
208
209 ti_sci_proc_request(&dsp->tsp);
210 reset_assert(&dsp->dsp_rst);
211 ti_sci_proc_power_domain_off(&dsp->tsp);
212 ti_sci_proc_release(&dsp->tsp);
213
214 return 0;
215}
216
217/**
218 * k3_dsp_init() - Initialize the remote processor
219 * @dev: rproc device pointer
220 *
221 * Return: 0 if all went ok, else return appropriate error
222 */
223static int k3_dsp_init(struct udevice *dev)
224{
225 dev_dbg(dev, "%s\n", __func__);
226
227 return 0;
228}
229
230static int k3_dsp_reset(struct udevice *dev)
231{
232 dev_dbg(dev, "%s\n", __func__);
233
234 return 0;
235}
236
237static void *k3_dsp_da_to_va(struct udevice *dev, ulong da, ulong len)
238{
239 struct k3_dsp_privdata *dsp = dev_get_priv(dev);
240 phys_addr_t bus_addr, dev_addr;
241 void __iomem *va = NULL;
242 size_t size;
243 u32 offset;
244 int i;
245
246 dev_dbg(dev, "%s\n", __func__);
247
248 if (len <= 0)
249 return NULL;
250
251 for (i = 0; i < dsp->num_mems; i++) {
252 bus_addr = dsp->mem[i].bus_addr;
253 dev_addr = dsp->mem[i].dev_addr;
254 size = dsp->mem[i].size;
255
256 if (da >= dev_addr && ((da + len) <= (dev_addr + size))) {
257 offset = da - dev_addr;
258 va = dsp->mem[i].cpu_addr + offset;
259 return (__force void *)va;
260 }
261
262 if (da >= bus_addr && (da + len) <= (bus_addr + size)) {
263 offset = da - bus_addr;
264 va = dsp->mem[i].cpu_addr + offset;
265 return (__force void *)va;
266 }
267 }
268
269 /* Assume it is DDR region and return da */
270 return map_physmem(da, len, MAP_NOCACHE);
271}
272
273static const struct dm_rproc_ops k3_dsp_ops = {
274 .init = k3_dsp_init,
275 .load = k3_dsp_load,
276 .start = k3_dsp_start,
277 .stop = k3_dsp_stop,
278 .reset = k3_dsp_reset,
279 .device_to_virt = k3_dsp_da_to_va,
280};
281
282static int ti_sci_proc_of_to_priv(struct udevice *dev, struct ti_sci_proc *tsp)
283{
284 u32 ids[2];
285 int ret;
286
287 dev_dbg(dev, "%s\n", __func__);
288
289 tsp->sci = ti_sci_get_by_phandle(dev, "ti,sci");
290 if (IS_ERR(tsp->sci)) {
291 dev_err(dev, "ti_sci get failed: %ld\n", PTR_ERR(tsp->sci));
292 return PTR_ERR(tsp->sci);
293 }
294
295 ret = dev_read_u32_array(dev, "ti,sci-proc-ids", ids, 2);
296 if (ret) {
297 dev_err(dev, "Proc IDs not populated %d\n", ret);
298 return ret;
299 }
300
301 tsp->ops = &tsp->sci->ops.proc_ops;
302 tsp->proc_id = ids[0];
303 tsp->host_id = ids[1];
304 tsp->dev_id = dev_read_u32_default(dev, "ti,sci-dev-id",
305 TI_SCI_RESOURCE_NULL);
306 if (tsp->dev_id == TI_SCI_RESOURCE_NULL) {
307 dev_err(dev, "Device ID not populated %d\n", ret);
308 return -ENODEV;
309 }
310
311 return 0;
312}
313
314static int k3_dsp_of_get_memories(struct udevice *dev)
315{
316 static const char * const mem_names[] = {"l2sram", "l1pram", "l1dram"};
317 struct k3_dsp_privdata *dsp = dev_get_priv(dev);
318 int i;
319
320 dev_dbg(dev, "%s\n", __func__);
321
322 dsp->num_mems = ARRAY_SIZE(mem_names);
323 dsp->mem = calloc(dsp->num_mems, sizeof(*dsp->mem));
324 if (!dsp->mem)
325 return -ENOMEM;
326
327 for (i = 0; i < dsp->num_mems; i++) {
328 /* C71 cores only have a L1P Cache, there are no L1P SRAMs */
329 if (device_is_compatible(dev, "ti,j721e-c71-dsp") &&
330 !strcmp(mem_names[i], "l1pram")) {
331 dsp->mem[i].bus_addr = FDT_ADDR_T_NONE;
332 dsp->mem[i].dev_addr = FDT_ADDR_T_NONE;
333 dsp->mem[i].cpu_addr = NULL;
334 dsp->mem[i].size = 0;
335 continue;
336 }
337
338 dsp->mem[i].bus_addr = dev_read_addr_size_name(dev, mem_names[i],
339 (fdt_addr_t *)&dsp->mem[i].size);
340 if (dsp->mem[i].bus_addr == FDT_ADDR_T_NONE) {
341 dev_err(dev, "%s bus address not found\n", mem_names[i]);
342 return -EINVAL;
343 }
344 dsp->mem[i].cpu_addr = map_physmem(dsp->mem[i].bus_addr,
345 dsp->mem[i].size,
346 MAP_NOCACHE);
347 dsp->mem[i].dev_addr = dsp->mem[i].bus_addr &
348 KEYSTONE_RPROC_LOCAL_ADDRESS_MASK;
349
350 dev_dbg(dev, "memory %8s: bus addr %pa size 0x%zx va %p da %pa\n",
351 mem_names[i], &dsp->mem[i].bus_addr,
352 dsp->mem[i].size, dsp->mem[i].cpu_addr,
353 &dsp->mem[i].dev_addr);
354 }
355
356 return 0;
357}
358
359/**
360 * k3_of_to_priv() - generate private data from device tree
361 * @dev: corresponding k3 dsp processor device
362 * @dsp: pointer to driver specific private data
363 *
364 * Return: 0 if all goes good, else appropriate error message.
365 */
366static int k3_dsp_of_to_priv(struct udevice *dev, struct k3_dsp_privdata *dsp)
367{
368 int ret;
369
370 dev_dbg(dev, "%s\n", __func__);
371
372 ret = reset_get_by_index(dev, 0, &dsp->dsp_rst);
373 if (ret) {
374 dev_err(dev, "reset_get() failed: %d\n", ret);
375 return ret;
376 }
377
378 ret = ti_sci_proc_of_to_priv(dev, &dsp->tsp);
379 if (ret)
380 return ret;
381
382 ret = k3_dsp_of_get_memories(dev);
383 if (ret)
384 return ret;
385
Suman Annae45fa382020-03-10 16:05:54 -0500386 dsp->data = (struct k3_dsp_boot_data *)dev_get_driver_data(dev);
387
Lokesh Vutla2c771852019-09-04 16:01:36 +0530388 return 0;
389}
390
391/**
392 * k3_dsp_probe() - Basic probe
393 * @dev: corresponding k3 remote processor device
394 *
395 * Return: 0 if all goes good, else appropriate error message.
396 */
397static int k3_dsp_probe(struct udevice *dev)
398{
399 struct k3_dsp_privdata *dsp;
400 int ret;
401
402 dev_dbg(dev, "%s\n", __func__);
403
404 dsp = dev_get_priv(dev);
405
406 ret = k3_dsp_of_to_priv(dev, dsp);
407 if (ret) {
408 dev_dbg(dev, "%s: Probe failed with error %d\n", __func__, ret);
409 return ret;
410 }
411
Suman Annabe7fa2d2020-03-10 16:05:56 -0500412 /*
413 * The DSP local resets are deasserted by default on Power-On-Reset.
414 * Assert the local resets to ensure the DSPs don't execute bogus code
415 * in .load() callback when the module reset is released to support
416 * internal memory loading. This is needed for C66x DSPs, and is a
417 * no-op on C71x DSPs.
418 */
419 reset_assert(&dsp->dsp_rst);
420
Lokesh Vutla2c771852019-09-04 16:01:36 +0530421 dev_dbg(dev, "Remoteproc successfully probed\n");
422
423 return 0;
424}
425
426static int k3_dsp_remove(struct udevice *dev)
427{
428 struct k3_dsp_privdata *dsp = dev_get_priv(dev);
429
430 free(dsp->mem);
431
432 return 0;
433}
434
Suman Annae45fa382020-03-10 16:05:54 -0500435static const struct k3_dsp_boot_data c66_data = {
436 .boot_align_addr = SZ_1K,
Suman Annabe7fa2d2020-03-10 16:05:56 -0500437 .uses_lreset = true,
Suman Annae45fa382020-03-10 16:05:54 -0500438};
439
440static const struct k3_dsp_boot_data c71_data = {
441 .boot_align_addr = SZ_2M,
Suman Annabe7fa2d2020-03-10 16:05:56 -0500442 .uses_lreset = false,
Suman Annae45fa382020-03-10 16:05:54 -0500443};
444
Lokesh Vutla2c771852019-09-04 16:01:36 +0530445static const struct udevice_id k3_dsp_ids[] = {
Suman Annae45fa382020-03-10 16:05:54 -0500446 { .compatible = "ti,j721e-c66-dsp", .data = (ulong)&c66_data, },
447 { .compatible = "ti,j721e-c71-dsp", .data = (ulong)&c71_data, },
Lokesh Vutla2c771852019-09-04 16:01:36 +0530448 {}
449};
450
451U_BOOT_DRIVER(k3_dsp) = {
452 .name = "k3_dsp",
453 .of_match = k3_dsp_ids,
454 .id = UCLASS_REMOTEPROC,
455 .ops = &k3_dsp_ops,
456 .probe = k3_dsp_probe,
457 .remove = k3_dsp_remove,
458 .priv_auto_alloc_size = sizeof(struct k3_dsp_privdata),
459};