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