1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Texas Instruments' K3 DSP Remoteproc driver
4  *
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>
8  */
9 
10 #include <common.h>
11 #include <dm.h>
12 #include <log.h>
13 #include <malloc.h>
14 #include <remoteproc.h>
15 #include <errno.h>
16 #include <clk.h>
17 #include <reset.h>
18 #include <asm/io.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"
25 
26 #define KEYSTONE_RPROC_LOCAL_ADDRESS_MASK	(SZ_16M - 1)
27 
28 /**
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
34  */
35 struct k3_dsp_mem {
36 	void __iomem *cpu_addr;
37 	phys_addr_t bus_addr;
38 	phys_addr_t dev_addr;
39 	size_t size;
40 };
41 
42 /**
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
46  */
47 struct k3_dsp_boot_data {
48 	u32 boot_align_addr;
49 	bool uses_lreset;
50 };
51 
52 /**
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
59  */
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;
65 	int num_mems;
66 };
67 
68 /*
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.
78  */
k3_dsp_prepare(struct udevice * dev)79 static int k3_dsp_prepare(struct udevice *dev)
80 {
81 	struct k3_dsp_privdata *dsp = dev_get_priv(dev);
82 	struct k3_dsp_boot_data *data = dsp->data;
83 	int ret;
84 
85 	/* local reset is no-op on C71x processors */
86 	if (!data->uses_lreset)
87 		return 0;
88 
89 	ret = ti_sci_proc_power_domain_on(&dsp->tsp);
90 	if (ret)
91 		dev_err(dev, "cannot enable internal RAM loading, ret = %d\n",
92 			ret);
93 
94 	return ret;
95 }
96 
97 /*
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.
103  */
k3_dsp_unprepare(struct udevice * dev)104 static int k3_dsp_unprepare(struct udevice *dev)
105 {
106 	struct k3_dsp_privdata *dsp = dev_get_priv(dev);
107 	struct k3_dsp_boot_data *data = dsp->data;
108 
109 	/* local reset is no-op on C71x processors */
110 	if (!data->uses_lreset)
111 		return 0;
112 
113 	return ti_sci_proc_power_domain_off(&dsp->tsp);
114 }
115 
116 /**
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
121  *
122  * Return: 0 if all goes good, else appropriate error message.
123  */
k3_dsp_load(struct udevice * dev,ulong addr,ulong size)124 static int k3_dsp_load(struct udevice *dev, ulong addr, ulong size)
125 {
126 	struct k3_dsp_privdata *dsp = dev_get_priv(dev);
127 	struct k3_dsp_boot_data *data = dsp->data;
128 	u32 boot_vector;
129 	int ret;
130 
131 	dev_dbg(dev, "%s addr = 0x%lx, size = 0x%lx\n", __func__, addr, size);
132 	ret = ti_sci_proc_request(&dsp->tsp);
133 	if (ret)
134 		return ret;
135 
136 	ret = k3_dsp_prepare(dev);
137 	if (ret) {
138 		dev_err(dev, "DSP prepare failed for core %d\n",
139 			dsp->tsp.proc_id);
140 		goto proc_release;
141 	}
142 
143 	ret = rproc_elf_load_image(dev, addr, size);
144 	if (ret < 0) {
145 		dev_err(dev, "Loading elf failed %d\n", ret);
146 		goto unprepare;
147 	}
148 
149 	boot_vector = rproc_elf_get_boot_addr(dev, addr);
150 	if (boot_vector & (data->boot_align_addr - 1)) {
151 		ret = -EINVAL;
152 		dev_err(dev, "Boot vector 0x%x not aligned on 0x%x boundary\n",
153 			boot_vector, data->boot_align_addr);
154 		goto proc_release;
155 	}
156 
157 	dev_dbg(dev, "%s: Boot vector = 0x%x\n", __func__, boot_vector);
158 
159 	ret = ti_sci_proc_set_config(&dsp->tsp, boot_vector, 0, 0);
160 unprepare:
161 	if (ret)
162 		k3_dsp_unprepare(dev);
163 proc_release:
164 	ti_sci_proc_release(&dsp->tsp);
165 	return ret;
166 }
167 
168 /**
169  * k3_dsp_start() - Start the remote processor
170  * @dev:	rproc device pointer
171  *
172  * Return: 0 if all went ok, else return appropriate error
173  */
k3_dsp_start(struct udevice * dev)174 static int k3_dsp_start(struct udevice *dev)
175 {
176 	struct k3_dsp_privdata *dsp = dev_get_priv(dev);
177 	struct k3_dsp_boot_data *data = dsp->data;
178 	int ret;
179 
180 	dev_dbg(dev, "%s\n", __func__);
181 
182 	ret = ti_sci_proc_request(&dsp->tsp);
183 	if (ret)
184 		return ret;
185 
186 	if (!data->uses_lreset) {
187 		ret = ti_sci_proc_power_domain_on(&dsp->tsp);
188 		if (ret)
189 			goto proc_release;
190 	}
191 
192 	ret = reset_deassert(&dsp->dsp_rst);
193 	if (ret) {
194 		if (!data->uses_lreset)
195 			ti_sci_proc_power_domain_off(&dsp->tsp);
196 	}
197 
198 proc_release:
199 	ti_sci_proc_release(&dsp->tsp);
200 
201 	return ret;
202 }
203 
k3_dsp_stop(struct udevice * dev)204 static int k3_dsp_stop(struct udevice *dev)
205 {
206 	struct k3_dsp_privdata *dsp = dev_get_priv(dev);
207 
208 	dev_dbg(dev, "%s\n", __func__);
209 
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);
214 
215 	return 0;
216 }
217 
218 /**
219  * k3_dsp_init() - Initialize the remote processor
220  * @dev:	rproc device pointer
221  *
222  * Return: 0 if all went ok, else return appropriate error
223  */
k3_dsp_init(struct udevice * dev)224 static int k3_dsp_init(struct udevice *dev)
225 {
226 	dev_dbg(dev, "%s\n", __func__);
227 
228 	return 0;
229 }
230 
k3_dsp_reset(struct udevice * dev)231 static int k3_dsp_reset(struct udevice *dev)
232 {
233 	dev_dbg(dev, "%s\n", __func__);
234 
235 	return 0;
236 }
237 
k3_dsp_da_to_va(struct udevice * dev,ulong da,ulong len)238 static void *k3_dsp_da_to_va(struct udevice *dev, ulong da, ulong len)
239 {
240 	struct k3_dsp_privdata *dsp = dev_get_priv(dev);
241 	phys_addr_t bus_addr, dev_addr;
242 	void __iomem *va = NULL;
243 	size_t size;
244 	u32 offset;
245 	int i;
246 
247 	dev_dbg(dev, "%s\n", __func__);
248 
249 	if (len <= 0)
250 		return NULL;
251 
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;
256 
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;
261 		}
262 
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;
267 		}
268 	}
269 
270 	/* Assume it is DDR region and return da */
271 	return map_physmem(da, len, MAP_NOCACHE);
272 }
273 
274 static const struct dm_rproc_ops k3_dsp_ops = {
275 	.init = k3_dsp_init,
276 	.load = k3_dsp_load,
277 	.start = k3_dsp_start,
278 	.stop = k3_dsp_stop,
279 	.reset = k3_dsp_reset,
280 	.device_to_virt = k3_dsp_da_to_va,
281 };
282 
ti_sci_proc_of_to_priv(struct udevice * dev,struct ti_sci_proc * tsp)283 static int ti_sci_proc_of_to_priv(struct udevice *dev, struct ti_sci_proc *tsp)
284 {
285 	u32 ids[2];
286 	int ret;
287 
288 	dev_dbg(dev, "%s\n", __func__);
289 
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);
294 	}
295 
296 	ret = dev_read_u32_array(dev, "ti,sci-proc-ids", ids, 2);
297 	if (ret) {
298 		dev_err(dev, "Proc IDs not populated %d\n", ret);
299 		return ret;
300 	}
301 
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);
309 		return -ENODEV;
310 	}
311 
312 	return 0;
313 }
314 
k3_dsp_of_get_memories(struct udevice * dev)315 static int k3_dsp_of_get_memories(struct udevice *dev)
316 {
317 	static const char * const mem_names[] = {"l2sram", "l1pram", "l1dram"};
318 	struct k3_dsp_privdata *dsp = dev_get_priv(dev);
319 	int i;
320 
321 	dev_dbg(dev, "%s\n", __func__);
322 
323 	dsp->num_mems = ARRAY_SIZE(mem_names);
324 	dsp->mem = calloc(dsp->num_mems, sizeof(*dsp->mem));
325 	if (!dsp->mem)
326 		return -ENOMEM;
327 
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;
336 			continue;
337 		}
338 
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]);
343 			return -EINVAL;
344 		}
345 		dsp->mem[i].cpu_addr = map_physmem(dsp->mem[i].bus_addr,
346 						   dsp->mem[i].size,
347 						   MAP_NOCACHE);
348 		dsp->mem[i].dev_addr = dsp->mem[i].bus_addr &
349 					KEYSTONE_RPROC_LOCAL_ADDRESS_MASK;
350 
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);
355 	}
356 
357 	return 0;
358 }
359 
360 /**
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
364  *
365  * Return: 0 if all goes good, else appropriate error message.
366  */
k3_dsp_of_to_priv(struct udevice * dev,struct k3_dsp_privdata * dsp)367 static int k3_dsp_of_to_priv(struct udevice *dev, struct k3_dsp_privdata *dsp)
368 {
369 	int ret;
370 
371 	dev_dbg(dev, "%s\n", __func__);
372 
373 	ret = reset_get_by_index(dev, 0, &dsp->dsp_rst);
374 	if (ret) {
375 		dev_err(dev, "reset_get() failed: %d\n", ret);
376 		return ret;
377 	}
378 
379 	ret = ti_sci_proc_of_to_priv(dev, &dsp->tsp);
380 	if (ret)
381 		return ret;
382 
383 	ret =  k3_dsp_of_get_memories(dev);
384 	if (ret)
385 		return ret;
386 
387 	dsp->data = (struct k3_dsp_boot_data *)dev_get_driver_data(dev);
388 
389 	return 0;
390 }
391 
392 /**
393  * k3_dsp_probe() - Basic probe
394  * @dev:	corresponding k3 remote processor device
395  *
396  * Return: 0 if all goes good, else appropriate error message.
397  */
k3_dsp_probe(struct udevice * dev)398 static int k3_dsp_probe(struct udevice *dev)
399 {
400 	struct k3_dsp_privdata *dsp;
401 	int ret;
402 
403 	dev_dbg(dev, "%s\n", __func__);
404 
405 	dsp = dev_get_priv(dev);
406 
407 	ret = k3_dsp_of_to_priv(dev, dsp);
408 	if (ret) {
409 		dev_dbg(dev, "%s: Probe failed with error %d\n", __func__, ret);
410 		return ret;
411 	}
412 
413 	/*
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.
419 	 */
420 	reset_assert(&dsp->dsp_rst);
421 
422 	dev_dbg(dev, "Remoteproc successfully probed\n");
423 
424 	return 0;
425 }
426 
k3_dsp_remove(struct udevice * dev)427 static int k3_dsp_remove(struct udevice *dev)
428 {
429 	struct k3_dsp_privdata *dsp = dev_get_priv(dev);
430 
431 	free(dsp->mem);
432 
433 	return 0;
434 }
435 
436 static const struct k3_dsp_boot_data c66_data = {
437 	.boot_align_addr = SZ_1K,
438 	.uses_lreset = true,
439 };
440 
441 static const struct k3_dsp_boot_data c71_data = {
442 	.boot_align_addr = SZ_2M,
443 	.uses_lreset = false,
444 };
445 
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, },
449 	{}
450 };
451 
452 U_BOOT_DRIVER(k3_dsp) = {
453 	.name = "k3_dsp",
454 	.of_match = k3_dsp_ids,
455 	.id = UCLASS_REMOTEPROC,
456 	.ops = &k3_dsp_ops,
457 	.probe = k3_dsp_probe,
458 	.remove = k3_dsp_remove,
459 	.priv_auto	= sizeof(struct k3_dsp_privdata),
460 };
461