1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Texas Instruments' K3 DSP Remoteproc driver
4 *
5 * Copyright (C) 2018-2019 Texas Instruments Incorporated - http://www.ti.com/
6 * Lokesh Vutla <lokeshvutla@ti.com>
7 *
8 */
9
10 #include <common.h>
11 #include <dm.h>
12 #include <remoteproc.h>
13 #include <errno.h>
14 #include <clk.h>
15 #include <reset.h>
16 #include <asm/io.h>
17 #include <power-domain.h>
18 #include <linux/soc/ti/ti_sci_protocol.h>
19 #include "ti_sci_proc.h"
20
21 #define KEYSTONE_RPROC_LOCAL_ADDRESS_MASK (SZ_16M - 1)
22
23 /**
24 * struct k3_dsp_mem - internal memory structure
25 * @cpu_addr: MPU virtual address of the memory region
26 * @bus_addr: Bus address used to access the memory region
27 * @dev_addr: Device address from remoteproc view
28 * @size: Size of the memory region
29 */
30 struct k3_dsp_mem {
31 void __iomem *cpu_addr;
32 phys_addr_t bus_addr;
33 phys_addr_t dev_addr;
34 size_t size;
35 };
36
37 /**
38 * struct k3_dsp_privdata - Structure representing Remote processor data.
39 * @rproc_rst: rproc reset control data
40 * @tsp: Pointer to TISCI proc contrl handle
41 * @mem: Array of available memories
42 * @num_mem: Number of available memories
43 */
44 struct k3_dsp_privdata {
45 struct reset_ctl dsp_rst;
46 struct ti_sci_proc tsp;
47 struct k3_dsp_mem *mem;
48 int num_mems;
49 };
50
51 /**
52 * k3_dsp_load() - Load up the Remote processor image
53 * @dev: rproc device pointer
54 * @addr: Address at which image is available
55 * @size: size of the image
56 *
57 * Return: 0 if all goes good, else appropriate error message.
58 */
k3_dsp_load(struct udevice * dev,ulong addr,ulong size)59 static int k3_dsp_load(struct udevice *dev, ulong addr, ulong size)
60 {
61 struct k3_dsp_privdata *dsp = dev_get_priv(dev);
62 u32 boot_vector;
63 int ret;
64
65 dev_dbg(dev, "%s addr = 0x%lx, size = 0x%lx\n", __func__, addr, size);
66 ret = ti_sci_proc_request(&dsp->tsp);
67 if (ret)
68 return ret;
69
70 ret = rproc_elf_load_image(dev, addr, size);
71 if (ret < 0) {
72 dev_err(dev, "Loading elf failed %d\n", ret);
73 goto proc_release;
74 }
75
76 boot_vector = rproc_elf_get_boot_addr(dev, addr);
77
78 dev_dbg(dev, "%s: Boot vector = 0x%x\n", __func__, boot_vector);
79
80 ret = ti_sci_proc_set_config(&dsp->tsp, boot_vector, 0, 0);
81 proc_release:
82 ti_sci_proc_release(&dsp->tsp);
83 return ret;
84 }
85
86 /**
87 * k3_dsp_start() - Start the remote processor
88 * @dev: rproc device pointer
89 *
90 * Return: 0 if all went ok, else return appropriate error
91 */
k3_dsp_start(struct udevice * dev)92 static int k3_dsp_start(struct udevice *dev)
93 {
94 struct k3_dsp_privdata *dsp = dev_get_priv(dev);
95 int ret;
96
97 dev_dbg(dev, "%s\n", __func__);
98
99 ret = ti_sci_proc_request(&dsp->tsp);
100 if (ret)
101 return ret;
102 /*
103 * Setting the right clock frequency would have taken care by
104 * assigned-clock-rates during the device probe. So no need to
105 * set the frequency again here.
106 */
107 ret = ti_sci_proc_power_domain_on(&dsp->tsp);
108 if (ret)
109 goto proc_release;
110
111 ret = reset_deassert(&dsp->dsp_rst);
112
113 proc_release:
114 ti_sci_proc_release(&dsp->tsp);
115
116 return ret;
117 }
118
k3_dsp_stop(struct udevice * dev)119 static int k3_dsp_stop(struct udevice *dev)
120 {
121 struct k3_dsp_privdata *dsp = dev_get_priv(dev);
122
123 dev_dbg(dev, "%s\n", __func__);
124
125 ti_sci_proc_request(&dsp->tsp);
126 reset_assert(&dsp->dsp_rst);
127 ti_sci_proc_power_domain_off(&dsp->tsp);
128 ti_sci_proc_release(&dsp->tsp);
129
130 return 0;
131 }
132
133 /**
134 * k3_dsp_init() - Initialize the remote processor
135 * @dev: rproc device pointer
136 *
137 * Return: 0 if all went ok, else return appropriate error
138 */
k3_dsp_init(struct udevice * dev)139 static int k3_dsp_init(struct udevice *dev)
140 {
141 dev_dbg(dev, "%s\n", __func__);
142
143 return 0;
144 }
145
k3_dsp_reset(struct udevice * dev)146 static int k3_dsp_reset(struct udevice *dev)
147 {
148 dev_dbg(dev, "%s\n", __func__);
149
150 return 0;
151 }
152
k3_dsp_da_to_va(struct udevice * dev,ulong da,ulong len)153 static void *k3_dsp_da_to_va(struct udevice *dev, ulong da, ulong len)
154 {
155 struct k3_dsp_privdata *dsp = dev_get_priv(dev);
156 phys_addr_t bus_addr, dev_addr;
157 void __iomem *va = NULL;
158 size_t size;
159 u32 offset;
160 int i;
161
162 dev_dbg(dev, "%s\n", __func__);
163
164 if (len <= 0)
165 return NULL;
166
167 for (i = 0; i < dsp->num_mems; i++) {
168 bus_addr = dsp->mem[i].bus_addr;
169 dev_addr = dsp->mem[i].dev_addr;
170 size = dsp->mem[i].size;
171
172 if (da >= dev_addr && ((da + len) <= (dev_addr + size))) {
173 offset = da - dev_addr;
174 va = dsp->mem[i].cpu_addr + offset;
175 return (__force void *)va;
176 }
177
178 if (da >= bus_addr && (da + len) <= (bus_addr + size)) {
179 offset = da - bus_addr;
180 va = dsp->mem[i].cpu_addr + offset;
181 return (__force void *)va;
182 }
183 }
184
185 /* Assume it is DDR region and return da */
186 return map_physmem(da, len, MAP_NOCACHE);
187 }
188
189 static const struct dm_rproc_ops k3_dsp_ops = {
190 .init = k3_dsp_init,
191 .load = k3_dsp_load,
192 .start = k3_dsp_start,
193 .stop = k3_dsp_stop,
194 .reset = k3_dsp_reset,
195 .device_to_virt = k3_dsp_da_to_va,
196 };
197
ti_sci_proc_of_to_priv(struct udevice * dev,struct ti_sci_proc * tsp)198 static int ti_sci_proc_of_to_priv(struct udevice *dev, struct ti_sci_proc *tsp)
199 {
200 u32 ids[2];
201 int ret;
202
203 dev_dbg(dev, "%s\n", __func__);
204
205 tsp->sci = ti_sci_get_by_phandle(dev, "ti,sci");
206 if (IS_ERR(tsp->sci)) {
207 dev_err(dev, "ti_sci get failed: %ld\n", PTR_ERR(tsp->sci));
208 return PTR_ERR(tsp->sci);
209 }
210
211 ret = dev_read_u32_array(dev, "ti,sci-proc-ids", ids, 2);
212 if (ret) {
213 dev_err(dev, "Proc IDs not populated %d\n", ret);
214 return ret;
215 }
216
217 tsp->ops = &tsp->sci->ops.proc_ops;
218 tsp->proc_id = ids[0];
219 tsp->host_id = ids[1];
220 tsp->dev_id = dev_read_u32_default(dev, "ti,sci-dev-id",
221 TI_SCI_RESOURCE_NULL);
222 if (tsp->dev_id == TI_SCI_RESOURCE_NULL) {
223 dev_err(dev, "Device ID not populated %d\n", ret);
224 return -ENODEV;
225 }
226
227 return 0;
228 }
229
k3_dsp_of_get_memories(struct udevice * dev)230 static int k3_dsp_of_get_memories(struct udevice *dev)
231 {
232 static const char * const mem_names[] = {"l2sram", "l1pram", "l1dram"};
233 struct k3_dsp_privdata *dsp = dev_get_priv(dev);
234 int i;
235
236 dev_dbg(dev, "%s\n", __func__);
237
238 dsp->num_mems = ARRAY_SIZE(mem_names);
239 dsp->mem = calloc(dsp->num_mems, sizeof(*dsp->mem));
240 if (!dsp->mem)
241 return -ENOMEM;
242
243 for (i = 0; i < dsp->num_mems; i++) {
244 /* C71 cores only have a L1P Cache, there are no L1P SRAMs */
245 if (device_is_compatible(dev, "ti,j721e-c71-dsp") &&
246 !strcmp(mem_names[i], "l1pram")) {
247 dsp->mem[i].bus_addr = FDT_ADDR_T_NONE;
248 dsp->mem[i].dev_addr = FDT_ADDR_T_NONE;
249 dsp->mem[i].cpu_addr = NULL;
250 dsp->mem[i].size = 0;
251 continue;
252 }
253
254 dsp->mem[i].bus_addr = dev_read_addr_size_name(dev, mem_names[i],
255 (fdt_addr_t *)&dsp->mem[i].size);
256 if (dsp->mem[i].bus_addr == FDT_ADDR_T_NONE) {
257 dev_err(dev, "%s bus address not found\n", mem_names[i]);
258 return -EINVAL;
259 }
260 dsp->mem[i].cpu_addr = map_physmem(dsp->mem[i].bus_addr,
261 dsp->mem[i].size,
262 MAP_NOCACHE);
263 dsp->mem[i].dev_addr = dsp->mem[i].bus_addr &
264 KEYSTONE_RPROC_LOCAL_ADDRESS_MASK;
265
266 dev_dbg(dev, "memory %8s: bus addr %pa size 0x%zx va %p da %pa\n",
267 mem_names[i], &dsp->mem[i].bus_addr,
268 dsp->mem[i].size, dsp->mem[i].cpu_addr,
269 &dsp->mem[i].dev_addr);
270 }
271
272 return 0;
273 }
274
275 /**
276 * k3_of_to_priv() - generate private data from device tree
277 * @dev: corresponding k3 dsp processor device
278 * @dsp: pointer to driver specific private data
279 *
280 * Return: 0 if all goes good, else appropriate error message.
281 */
k3_dsp_of_to_priv(struct udevice * dev,struct k3_dsp_privdata * dsp)282 static int k3_dsp_of_to_priv(struct udevice *dev, struct k3_dsp_privdata *dsp)
283 {
284 int ret;
285
286 dev_dbg(dev, "%s\n", __func__);
287
288 ret = reset_get_by_index(dev, 0, &dsp->dsp_rst);
289 if (ret) {
290 dev_err(dev, "reset_get() failed: %d\n", ret);
291 return ret;
292 }
293
294 ret = ti_sci_proc_of_to_priv(dev, &dsp->tsp);
295 if (ret)
296 return ret;
297
298 ret = k3_dsp_of_get_memories(dev);
299 if (ret)
300 return ret;
301
302 return 0;
303 }
304
305 /**
306 * k3_dsp_probe() - Basic probe
307 * @dev: corresponding k3 remote processor device
308 *
309 * Return: 0 if all goes good, else appropriate error message.
310 */
k3_dsp_probe(struct udevice * dev)311 static int k3_dsp_probe(struct udevice *dev)
312 {
313 struct k3_dsp_privdata *dsp;
314 int ret;
315
316 dev_dbg(dev, "%s\n", __func__);
317
318 dsp = dev_get_priv(dev);
319
320 ret = k3_dsp_of_to_priv(dev, dsp);
321 if (ret) {
322 dev_dbg(dev, "%s: Probe failed with error %d\n", __func__, ret);
323 return ret;
324 }
325
326 dev_dbg(dev, "Remoteproc successfully probed\n");
327
328 return 0;
329 }
330
k3_dsp_remove(struct udevice * dev)331 static int k3_dsp_remove(struct udevice *dev)
332 {
333 struct k3_dsp_privdata *dsp = dev_get_priv(dev);
334
335 free(dsp->mem);
336
337 return 0;
338 }
339
340 static const struct udevice_id k3_dsp_ids[] = {
341 { .compatible = "ti,j721e-c66-dsp"},
342 { .compatible = "ti,j721e-c71-dsp"},
343 {}
344 };
345
346 U_BOOT_DRIVER(k3_dsp) = {
347 .name = "k3_dsp",
348 .of_match = k3_dsp_ids,
349 .id = UCLASS_REMOTEPROC,
350 .ops = &k3_dsp_ops,
351 .probe = k3_dsp_probe,
352 .remove = k3_dsp_remove,
353 .priv_auto_alloc_size = sizeof(struct k3_dsp_privdata),
354 };
355