1 // SPDX-License-Identifier: GPL-2.0+
4 * Texas Instruments Incorporated - http://www.ti.com/
6 #define pr_fmt(fmt) "%s: " fmt, __func__
12 #include <remoteproc.h>
14 #include <dm/device-internal.h>
16 #include <dm/uclass.h>
17 #include <dm/uclass-internal.h>
19 DECLARE_GLOBAL_DATA_PTR;
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
28 * Return: 0 if none of the callback returned a non 0 result, else returns the
29 * result from the callback function
31 static int for_each_remoteproc_device(int (*fn) (struct udevice *dev,
32 struct dm_rproc_uclass_pdata *uc_pdata,
34 struct udevice *skip_dev,
38 struct dm_rproc_uclass_pdata *uc_pdata;
41 for (ret = uclass_find_first_device(UCLASS_REMOTEPROC, &dev); dev;
42 ret = uclass_find_next_device(&dev)) {
43 if (ret || dev == skip_dev)
45 uc_pdata = dev_get_uclass_platdata(dev);
46 ret = fn(dev, uc_pdata, data);
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)
60 * Return: 0 is there is no match(is unique); if there is a match(we dont
61 * have a unique name), return -EINVAL.
63 static int _rproc_name_is_unique(struct udevice *dev,
64 struct dm_rproc_uclass_pdata *uc_pdata,
67 const char *check_name = data;
69 /* devices not yet populated with data - so skip them */
70 if (!uc_pdata->name || !check_name)
73 /* Return 0 to search further if we dont match */
74 if (strlen(uc_pdata->name) != strlen(check_name))
77 if (!strcmp(uc_pdata->name, check_name))
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.
88 * Return: true if we have a unique name, false if name is not unique.
90 static bool rproc_name_is_unique(struct udevice *check_dev,
91 const char *check_name)
95 ret = for_each_remoteproc_device(_rproc_name_is_unique,
96 check_dev, check_name);
97 return ret ? false : true;
101 * rproc_pre_probe() - Pre probe accessor for the uclass
102 * @dev: device for which we are preprobing
104 * Parses and fills up the uclass pdata for use as needed by core and
105 * remote proc drivers.
107 * Return: 0 if all wernt ok, else appropriate error value.
109 static int rproc_pre_probe(struct udevice *dev)
111 struct dm_rproc_uclass_pdata *uc_pdata;
112 const struct dm_rproc_ops *ops;
114 uc_pdata = dev_get_uclass_platdata(dev);
116 /* See if we need to populate via fdt */
118 if (!dev->platdata) {
119 #if CONFIG_IS_ENABLED(OF_CONTROL)
120 int node = dev_of_offset(dev);
121 const void *blob = gd->fdt_blob;
124 debug("'%s' no dt?\n", dev->name);
127 debug("'%s': using fdt\n", dev->name);
128 uc_pdata->name = fdt_getprop(blob, node,
129 "remoteproc-name", NULL);
131 /* Default is internal memory mapped */
132 uc_pdata->mem_type = RPROC_INTERNAL_MEMORY_MAPPED;
133 tmp = fdtdec_get_bool(blob, node,
134 "remoteproc-internal-memory-mapped");
136 uc_pdata->mem_type = RPROC_INTERNAL_MEMORY_MAPPED;
138 /* Nothing much we can do about this, can we? */
143 struct dm_rproc_uclass_pdata *pdata = dev->platdata;
145 debug("'%s': using legacy data\n", dev->name);
147 uc_pdata->name = pdata->name;
148 uc_pdata->mem_type = pdata->mem_type;
149 uc_pdata->driver_plat_data = pdata->driver_plat_data;
152 /* Else try using device Name */
154 uc_pdata->name = dev->name;
155 if (!uc_pdata->name) {
156 debug("Unnamed device!");
160 if (!rproc_name_is_unique(dev, uc_pdata->name)) {
161 debug("%s duplicate name '%s'\n", dev->name, uc_pdata->name);
165 ops = rproc_get_ops(dev);
167 debug("%s driver has no ops?\n", dev->name);
171 if (!ops->load || !ops->start) {
172 debug("%s driver has missing mandatory ops?\n", dev->name);
180 * rproc_post_probe() - post probe accessor for the uclass
181 * @dev: deivce we finished probing
183 * initiate init function after the probe is completed. This allows
184 * the remote processor drivers to split up the initializations between
185 * probe and init as needed.
187 * Return: if the remote proc driver has a init routine, invokes it and
188 * hands over the return value. overall, 0 if all went well, else appropriate
191 static int rproc_post_probe(struct udevice *dev)
193 const struct dm_rproc_ops *ops;
195 ops = rproc_get_ops(dev);
197 debug("%s driver has no ops?\n", dev->name);
202 return ops->init(dev);
207 UCLASS_DRIVER(rproc) = {
208 .id = UCLASS_REMOTEPROC,
209 .name = "remoteproc",
210 .flags = DM_UC_FLAG_SEQ_ALIAS,
211 .pre_probe = rproc_pre_probe,
212 .post_probe = rproc_post_probe,
213 .per_device_platdata_auto_alloc_size =
214 sizeof(struct dm_rproc_uclass_pdata),
217 /* Remoteproc subsystem access functions */
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
224 * Return: 0 if all ok, else appropriate error value.
226 static int _rproc_probe_dev(struct udevice *dev,
227 struct dm_rproc_uclass_pdata *uc_pdata,
232 ret = device_probe(dev);
235 debug("%s: Failed to initialize - %d\n", dev->name, ret);
240 * _rproc_dev_is_probed() - check if the device has been probed
241 * @dev: device to check
245 * Return: -EAGAIN if not probed else return 0
247 static int _rproc_dev_is_probed(struct udevice *dev,
248 struct dm_rproc_uclass_pdata *uc_pdata,
251 if (dev->flags & DM_FLAG_ACTIVATED)
257 bool rproc_is_initialized(void)
259 int ret = for_each_remoteproc_device(_rproc_dev_is_probed, NULL, NULL);
260 return ret ? false : true;
267 if (rproc_is_initialized()) {
268 debug("Already initialized\n");
272 ret = for_each_remoteproc_device(_rproc_probe_dev, NULL, NULL);
276 int rproc_dev_init(int id)
278 struct udevice *dev = NULL;
281 ret = uclass_get_device_by_seq(UCLASS_REMOTEPROC, id, &dev);
283 debug("Unknown remote processor id '%d' requested(%d)\n",
288 ret = device_probe(dev);
290 debug("%s: Failed to initialize - %d\n", dev->name, ret);
295 int rproc_load(int id, ulong addr, ulong size)
297 struct udevice *dev = NULL;
298 struct dm_rproc_uclass_pdata *uc_pdata;
299 const struct dm_rproc_ops *ops;
302 ret = uclass_get_device_by_seq(UCLASS_REMOTEPROC, id, &dev);
304 debug("Unknown remote processor id '%d' requested(%d)\n",
309 uc_pdata = dev_get_uclass_platdata(dev);
311 ops = rproc_get_ops(dev);
313 debug("%s driver has no ops?\n", dev->name);
317 debug("Loading to '%s' from address 0x%08lX size of %lu bytes\n",
318 uc_pdata->name, addr, size);
320 return ops->load(dev, addr, size);
322 debug("%s: data corruption?? mandatory function is missing!\n",
329 * Completely internal helper enums..
330 * Keeping this isolated helps this code evolve independent of other
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
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.
350 * Return: 0 if all ok, else appropriate error value.
352 static int _rproc_ops_wrapper(int id, enum rproc_ops op)
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;
362 ret = uclass_get_device_by_seq(UCLASS_REMOTEPROC, id, &dev);
364 debug("Unknown remote processor id '%d' requested(%d)\n",
369 uc_pdata = dev_get_uclass_platdata(dev);
371 ops = rproc_get_ops(dev);
373 debug("%s driver has no ops?\n", dev->name);
388 op_str = "Resetting";
391 fn = ops->is_running;
392 op_str = "Checking if running:";
399 debug("what is '%d' operation??\n", op);
403 debug("%s %s...\n", op_str, uc_pdata->name);
408 debug("%s: data corruption?? mandatory function is missing!\n",
414 int rproc_start(int id)
416 return _rproc_ops_wrapper(id, RPROC_START);
419 int rproc_stop(int id)
421 return _rproc_ops_wrapper(id, RPROC_STOP);
424 int rproc_reset(int id)
426 return _rproc_ops_wrapper(id, RPROC_RESET);
429 int rproc_ping(int id)
431 return _rproc_ops_wrapper(id, RPROC_PING);
434 int rproc_is_running(int id)
436 return _rproc_ops_wrapper(id, RPROC_RUNNING);