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