blob: 5919c8bb978bb37d3ea49ff1172903a2dd1bd9b3 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Nishanth Menonddf56bc2015-09-17 15:42:39 -05002/*
3 * (C) Copyright 2015
4 * Texas Instruments Incorporated - http://www.ti.com/
Nishanth Menonddf56bc2015-09-17 15:42:39 -05005 */
6#define pr_fmt(fmt) "%s: " fmt, __func__
7#include <common.h>
8#include <errno.h>
9#include <fdtdec.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Nishanth Menonddf56bc2015-09-17 15:42:39 -050011#include <malloc.h>
12#include <remoteproc.h>
Simon Glass401d1c42020-10-30 21:38:53 -060013#include <asm/global_data.h>
Nishanth Menonddf56bc2015-09-17 15:42:39 -050014#include <asm/io.h>
15#include <dm/device-internal.h>
16#include <dm.h>
17#include <dm/uclass.h>
18#include <dm/uclass-internal.h>
19
20DECLARE_GLOBAL_DATA_PTR;
21
22/**
23 * for_each_remoteproc_device() - iterate through the list of rproc devices
24 * @fn: check function to call per match, if this function returns fail,
25 * iteration is aborted with the resultant error value
26 * @skip_dev: Device to skip calling the callback about.
27 * @data: Data to pass to the callback function
28 *
29 * Return: 0 if none of the callback returned a non 0 result, else returns the
30 * result from the callback function
31 */
32static int for_each_remoteproc_device(int (*fn) (struct udevice *dev,
33 struct dm_rproc_uclass_pdata *uc_pdata,
34 const void *data),
35 struct udevice *skip_dev,
36 const void *data)
37{
38 struct udevice *dev;
39 struct dm_rproc_uclass_pdata *uc_pdata;
40 int ret;
41
42 for (ret = uclass_find_first_device(UCLASS_REMOTEPROC, &dev); dev;
43 ret = uclass_find_next_device(&dev)) {
44 if (ret || dev == skip_dev)
45 continue;
Simon Glasscaa4daa2020-12-03 16:55:18 -070046 uc_pdata = dev_get_uclass_plat(dev);
Nishanth Menonddf56bc2015-09-17 15:42:39 -050047 ret = fn(dev, uc_pdata, data);
48 if (ret)
49 return ret;
50 }
51
52 return 0;
53}
54
55/**
56 * _rproc_name_is_unique() - iteration helper to check if rproc name is unique
57 * @dev: device that we are checking name for
58 * @uc_pdata: uclass platform data
59 * @data: compare data (this is the name we want to ensure is unique)
60 *
61 * Return: 0 is there is no match(is unique); if there is a match(we dont
62 * have a unique name), return -EINVAL.
63 */
64static int _rproc_name_is_unique(struct udevice *dev,
65 struct dm_rproc_uclass_pdata *uc_pdata,
66 const void *data)
67{
68 const char *check_name = data;
69
70 /* devices not yet populated with data - so skip them */
Nishanth Menon9cb05a82015-11-30 22:05:58 -060071 if (!uc_pdata->name || !check_name)
Nishanth Menonddf56bc2015-09-17 15:42:39 -050072 return 0;
73
74 /* Return 0 to search further if we dont match */
75 if (strlen(uc_pdata->name) != strlen(check_name))
76 return 0;
77
78 if (!strcmp(uc_pdata->name, check_name))
79 return -EINVAL;
80
81 return 0;
82}
83
84/**
85 * rproc_name_is_unique() - Check if the rproc name is unique
86 * @check_dev: Device we are attempting to ensure is unique
87 * @check_name: Name we are trying to ensure is unique.
88 *
89 * Return: true if we have a unique name, false if name is not unique.
90 */
91static bool rproc_name_is_unique(struct udevice *check_dev,
92 const char *check_name)
93{
94 int ret;
95
96 ret = for_each_remoteproc_device(_rproc_name_is_unique,
97 check_dev, check_name);
98 return ret ? false : true;
99}
100
101/**
102 * rproc_pre_probe() - Pre probe accessor for the uclass
103 * @dev: device for which we are preprobing
104 *
105 * Parses and fills up the uclass pdata for use as needed by core and
106 * remote proc drivers.
107 *
108 * Return: 0 if all wernt ok, else appropriate error value.
109 */
110static int rproc_pre_probe(struct udevice *dev)
111{
112 struct dm_rproc_uclass_pdata *uc_pdata;
113 const struct dm_rproc_ops *ops;
114
Simon Glasscaa4daa2020-12-03 16:55:18 -0700115 uc_pdata = dev_get_uclass_plat(dev);
Nishanth Menonddf56bc2015-09-17 15:42:39 -0500116
117 /* See if we need to populate via fdt */
118
Simon Glass0fd3d912020-12-22 19:30:28 -0700119 if (!dev_get_plat(dev)) {
Nishanth Menonddf56bc2015-09-17 15:42:39 -0500120#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glasse160f7d2017-01-17 16:52:55 -0700121 int node = dev_of_offset(dev);
Nishanth Menonddf56bc2015-09-17 15:42:39 -0500122 const void *blob = gd->fdt_blob;
123 bool tmp;
124 if (!blob) {
125 debug("'%s' no dt?\n", dev->name);
126 return -EINVAL;
127 }
128 debug("'%s': using fdt\n", dev->name);
129 uc_pdata->name = fdt_getprop(blob, node,
130 "remoteproc-name", NULL);
131
132 /* Default is internal memory mapped */
133 uc_pdata->mem_type = RPROC_INTERNAL_MEMORY_MAPPED;
134 tmp = fdtdec_get_bool(blob, node,
135 "remoteproc-internal-memory-mapped");
136 if (tmp)
137 uc_pdata->mem_type = RPROC_INTERNAL_MEMORY_MAPPED;
138#else
139 /* Nothing much we can do about this, can we? */
140 return -EINVAL;
141#endif
142
143 } else {
Simon Glass0fd3d912020-12-22 19:30:28 -0700144 struct dm_rproc_uclass_pdata *pdata = dev_get_plat(dev);
Nishanth Menonddf56bc2015-09-17 15:42:39 -0500145
146 debug("'%s': using legacy data\n", dev->name);
147 if (pdata->name)
148 uc_pdata->name = pdata->name;
149 uc_pdata->mem_type = pdata->mem_type;
150 uc_pdata->driver_plat_data = pdata->driver_plat_data;
151 }
152
153 /* Else try using device Name */
154 if (!uc_pdata->name)
155 uc_pdata->name = dev->name;
156 if (!uc_pdata->name) {
157 debug("Unnamed device!");
158 return -EINVAL;
159 }
160
161 if (!rproc_name_is_unique(dev, uc_pdata->name)) {
162 debug("%s duplicate name '%s'\n", dev->name, uc_pdata->name);
163 return -EINVAL;
164 }
165
166 ops = rproc_get_ops(dev);
167 if (!ops) {
168 debug("%s driver has no ops?\n", dev->name);
169 return -EINVAL;
170 }
171
172 if (!ops->load || !ops->start) {
173 debug("%s driver has missing mandatory ops?\n", dev->name);
174 return -EINVAL;
175 }
176
177 return 0;
178}
179
180/**
181 * rproc_post_probe() - post probe accessor for the uclass
182 * @dev: deivce we finished probing
183 *
184 * initiate init function after the probe is completed. This allows
185 * the remote processor drivers to split up the initializations between
186 * probe and init as needed.
187 *
188 * Return: if the remote proc driver has a init routine, invokes it and
189 * hands over the return value. overall, 0 if all went well, else appropriate
190 * error value.
191 */
192static int rproc_post_probe(struct udevice *dev)
193{
194 const struct dm_rproc_ops *ops;
195
196 ops = rproc_get_ops(dev);
197 if (!ops) {
198 debug("%s driver has no ops?\n", dev->name);
199 return -EINVAL;
200 }
201
202 if (ops->init)
203 return ops->init(dev);
204
205 return 0;
206}
207
208UCLASS_DRIVER(rproc) = {
209 .id = UCLASS_REMOTEPROC,
210 .name = "remoteproc",
211 .flags = DM_UC_FLAG_SEQ_ALIAS,
212 .pre_probe = rproc_pre_probe,
213 .post_probe = rproc_post_probe,
Simon Glassb012ff12020-12-03 16:55:22 -0700214 .per_device_plat_auto = sizeof(struct dm_rproc_uclass_pdata),
Nishanth Menonddf56bc2015-09-17 15:42:39 -0500215};
216
217/* Remoteproc subsystem access functions */
218/**
219 * _rproc_probe_dev() - iteration helper to probe a rproc device
220 * @dev: device to probe
221 * @uc_pdata: uclass data allocated for the device
222 * @data: unused
223 *
224 * Return: 0 if all ok, else appropriate error value.
225 */
226static int _rproc_probe_dev(struct udevice *dev,
227 struct dm_rproc_uclass_pdata *uc_pdata,
228 const void *data)
229{
230 int ret;
231
232 ret = device_probe(dev);
233
234 if (ret)
235 debug("%s: Failed to initialize - %d\n", dev->name, ret);
236 return ret;
237}
238
239/**
240 * _rproc_dev_is_probed() - check if the device has been probed
241 * @dev: device to check
242 * @uc_pdata: unused
243 * @data: unused
244 *
245 * Return: -EAGAIN if not probed else return 0
246 */
247static int _rproc_dev_is_probed(struct udevice *dev,
248 struct dm_rproc_uclass_pdata *uc_pdata,
249 const void *data)
250{
Simon Glass73466df2020-12-19 10:40:10 -0700251 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
Nishanth Menonddf56bc2015-09-17 15:42:39 -0500252 return 0;
253
254 return -EAGAIN;
255}
256
257bool rproc_is_initialized(void)
258{
259 int ret = for_each_remoteproc_device(_rproc_dev_is_probed, NULL, NULL);
260 return ret ? false : true;
261}
262
263int rproc_init(void)
264{
265 int ret;
266
267 if (rproc_is_initialized()) {
268 debug("Already initialized\n");
269 return -EINVAL;
270 }
271
272 ret = for_each_remoteproc_device(_rproc_probe_dev, NULL, NULL);
273 return ret;
274}
275
Lokesh Vutla81ae6e62018-08-27 15:57:50 +0530276int rproc_dev_init(int id)
277{
278 struct udevice *dev = NULL;
279 int ret;
280
281 ret = uclass_get_device_by_seq(UCLASS_REMOTEPROC, id, &dev);
282 if (ret) {
283 debug("Unknown remote processor id '%d' requested(%d)\n",
284 id, ret);
285 return ret;
286 }
287
288 ret = device_probe(dev);
289 if (ret)
290 debug("%s: Failed to initialize - %d\n", dev->name, ret);
291
292 return ret;
293}
294
Nishanth Menonddf56bc2015-09-17 15:42:39 -0500295int rproc_load(int id, ulong addr, ulong size)
296{
297 struct udevice *dev = NULL;
298 struct dm_rproc_uclass_pdata *uc_pdata;
299 const struct dm_rproc_ops *ops;
300 int ret;
301
302 ret = uclass_get_device_by_seq(UCLASS_REMOTEPROC, id, &dev);
303 if (ret) {
304 debug("Unknown remote processor id '%d' requested(%d)\n",
305 id, ret);
306 return ret;
307 }
308
Simon Glasscaa4daa2020-12-03 16:55:18 -0700309 uc_pdata = dev_get_uclass_plat(dev);
Nishanth Menonddf56bc2015-09-17 15:42:39 -0500310
311 ops = rproc_get_ops(dev);
312 if (!ops) {
313 debug("%s driver has no ops?\n", dev->name);
314 return -EINVAL;
315 }
316
317 debug("Loading to '%s' from address 0x%08lX size of %lu bytes\n",
318 uc_pdata->name, addr, size);
319 if (ops->load)
320 return ops->load(dev, addr, size);
321
322 debug("%s: data corruption?? mandatory function is missing!\n",
323 dev->name);
324
325 return -EINVAL;
326};
327
328/*
329 * Completely internal helper enums..
330 * Keeping this isolated helps this code evolve independent of other
331 * parts..
332 */
333enum rproc_ops {
334 RPROC_START,
335 RPROC_STOP,
336 RPROC_RESET,
337 RPROC_PING,
338 RPROC_RUNNING,
339};
340
341/**
342 * _rproc_ops_wrapper() - wrapper for invoking remote proc driver callback
343 * @id: id of the remote processor
344 * @op: one of rproc_ops that indicate what operation to invoke
345 *
346 * Most of the checks and verification for remoteproc operations are more
347 * or less same for almost all operations. This allows us to put a wrapper
348 * and use the common checks to allow the driver to function appropriately.
349 *
350 * Return: 0 if all ok, else appropriate error value.
351 */
352static int _rproc_ops_wrapper(int id, enum rproc_ops op)
353{
354 struct udevice *dev = NULL;
355 struct dm_rproc_uclass_pdata *uc_pdata;
356 const struct dm_rproc_ops *ops;
357 int (*fn)(struct udevice *dev);
358 bool mandatory = false;
359 char *op_str;
360 int ret;
361
362 ret = uclass_get_device_by_seq(UCLASS_REMOTEPROC, id, &dev);
363 if (ret) {
364 debug("Unknown remote processor id '%d' requested(%d)\n",
365 id, ret);
366 return ret;
367 }
368
Simon Glasscaa4daa2020-12-03 16:55:18 -0700369 uc_pdata = dev_get_uclass_plat(dev);
Nishanth Menonddf56bc2015-09-17 15:42:39 -0500370
371 ops = rproc_get_ops(dev);
372 if (!ops) {
373 debug("%s driver has no ops?\n", dev->name);
374 return -EINVAL;
375 }
376 switch (op) {
377 case RPROC_START:
378 fn = ops->start;
379 mandatory = true;
380 op_str = "Starting";
381 break;
382 case RPROC_STOP:
383 fn = ops->stop;
384 op_str = "Stopping";
385 break;
386 case RPROC_RESET:
387 fn = ops->reset;
388 op_str = "Resetting";
389 break;
390 case RPROC_RUNNING:
391 fn = ops->is_running;
392 op_str = "Checking if running:";
393 break;
394 case RPROC_PING:
395 fn = ops->ping;
396 op_str = "Pinging";
397 break;
398 default:
399 debug("what is '%d' operation??\n", op);
400 return -EINVAL;
401 }
402
403 debug("%s %s...\n", op_str, uc_pdata->name);
404 if (fn)
405 return fn(dev);
406
407 if (mandatory)
408 debug("%s: data corruption?? mandatory function is missing!\n",
409 dev->name);
410
411 return -ENOSYS;
412}
413
414int rproc_start(int id)
415{
416 return _rproc_ops_wrapper(id, RPROC_START);
417};
418
419int rproc_stop(int id)
420{
421 return _rproc_ops_wrapper(id, RPROC_STOP);
422};
423
424int rproc_reset(int id)
425{
426 return _rproc_ops_wrapper(id, RPROC_RESET);
427};
428
429int rproc_ping(int id)
430{
431 return _rproc_ops_wrapper(id, RPROC_PING);
432};
433
434int rproc_is_running(int id)
435{
436 return _rproc_ops_wrapper(id, RPROC_RUNNING);
437};