3 * Texas Instruments Incorporated - http://www.ti.com/
4 * SPDX-License-Identifier: GPL-2.0+
6 #define pr_fmt(fmt) "%s: " fmt, __func__
11 #include <remoteproc.h>
13 #include <dm/device-internal.h>
15 #include <dm/uclass.h>
16 #include <dm/uclass-internal.h>
18 DECLARE_GLOBAL_DATA_PTR;
21 * for_each_remoteproc_device() - iterate through the list of rproc devices
22 * @fn: check function to call per match, if this function returns fail,
23 * iteration is aborted with the resultant error value
24 * @skip_dev: Device to skip calling the callback about.
25 * @data: Data to pass to the callback function
27 * Return: 0 if none of the callback returned a non 0 result, else returns the
28 * result from the callback function
30 static int for_each_remoteproc_device(int (*fn) (struct udevice *dev,
31 struct dm_rproc_uclass_pdata *uc_pdata,
33 struct udevice *skip_dev,
37 struct dm_rproc_uclass_pdata *uc_pdata;
40 for (ret = uclass_find_first_device(UCLASS_REMOTEPROC, &dev); dev;
41 ret = uclass_find_next_device(&dev)) {
42 if (ret || dev == skip_dev)
44 uc_pdata = dev_get_uclass_platdata(dev);
45 ret = fn(dev, uc_pdata, data);
54 * _rproc_name_is_unique() - iteration helper to check if rproc name is unique
55 * @dev: device that we are checking name for
56 * @uc_pdata: uclass platform data
57 * @data: compare data (this is the name we want to ensure is unique)
59 * Return: 0 is there is no match(is unique); if there is a match(we dont
60 * have a unique name), return -EINVAL.
62 static int _rproc_name_is_unique(struct udevice *dev,
63 struct dm_rproc_uclass_pdata *uc_pdata,
66 const char *check_name = data;
68 /* devices not yet populated with data - so skip them */
69 if (!uc_pdata->name || !check_name)
72 /* Return 0 to search further if we dont match */
73 if (strlen(uc_pdata->name) != strlen(check_name))
76 if (!strcmp(uc_pdata->name, check_name))
83 * rproc_name_is_unique() - Check if the rproc name is unique
84 * @check_dev: Device we are attempting to ensure is unique
85 * @check_name: Name we are trying to ensure is unique.
87 * Return: true if we have a unique name, false if name is not unique.
89 static bool rproc_name_is_unique(struct udevice *check_dev,
90 const char *check_name)
94 ret = for_each_remoteproc_device(_rproc_name_is_unique,
95 check_dev, check_name);
96 return ret ? false : true;
100 * rproc_pre_probe() - Pre probe accessor for the uclass
101 * @dev: device for which we are preprobing
103 * Parses and fills up the uclass pdata for use as needed by core and
104 * remote proc drivers.
106 * Return: 0 if all wernt ok, else appropriate error value.
108 static int rproc_pre_probe(struct udevice *dev)
110 struct dm_rproc_uclass_pdata *uc_pdata;
111 const struct dm_rproc_ops *ops;
113 uc_pdata = dev_get_uclass_platdata(dev);
115 /* See if we need to populate via fdt */
117 if (!dev->platdata) {
118 #if CONFIG_IS_ENABLED(OF_CONTROL)
119 int node = dev->of_offset;
120 const void *blob = gd->fdt_blob;
123 debug("'%s' no dt?\n", dev->name);
126 debug("'%s': using fdt\n", dev->name);
127 uc_pdata->name = fdt_getprop(blob, node,
128 "remoteproc-name", NULL);
130 /* Default is internal memory mapped */
131 uc_pdata->mem_type = RPROC_INTERNAL_MEMORY_MAPPED;
132 tmp = fdtdec_get_bool(blob, node,
133 "remoteproc-internal-memory-mapped");
135 uc_pdata->mem_type = RPROC_INTERNAL_MEMORY_MAPPED;
137 /* Nothing much we can do about this, can we? */
142 struct dm_rproc_uclass_pdata *pdata = dev->platdata;
144 debug("'%s': using legacy data\n", dev->name);
146 uc_pdata->name = pdata->name;
147 uc_pdata->mem_type = pdata->mem_type;
148 uc_pdata->driver_plat_data = pdata->driver_plat_data;
151 /* Else try using device Name */
153 uc_pdata->name = dev->name;
154 if (!uc_pdata->name) {
155 debug("Unnamed device!");
159 if (!rproc_name_is_unique(dev, uc_pdata->name)) {
160 debug("%s duplicate name '%s'\n", dev->name, uc_pdata->name);
164 ops = rproc_get_ops(dev);
166 debug("%s driver has no ops?\n", dev->name);
170 if (!ops->load || !ops->start) {
171 debug("%s driver has missing mandatory ops?\n", dev->name);
179 * rproc_post_probe() - post probe accessor for the uclass
180 * @dev: deivce we finished probing
182 * initiate init function after the probe is completed. This allows
183 * the remote processor drivers to split up the initializations between
184 * probe and init as needed.
186 * Return: if the remote proc driver has a init routine, invokes it and
187 * hands over the return value. overall, 0 if all went well, else appropriate
190 static int rproc_post_probe(struct udevice *dev)
192 const struct dm_rproc_ops *ops;
194 ops = rproc_get_ops(dev);
196 debug("%s driver has no ops?\n", dev->name);
201 return ops->init(dev);
206 UCLASS_DRIVER(rproc) = {
207 .id = UCLASS_REMOTEPROC,
208 .name = "remoteproc",
209 .flags = DM_UC_FLAG_SEQ_ALIAS,
210 .pre_probe = rproc_pre_probe,
211 .post_probe = rproc_post_probe,
212 .per_device_platdata_auto_alloc_size =
213 sizeof(struct dm_rproc_uclass_pdata),
216 /* Remoteproc subsystem access functions */
218 * _rproc_probe_dev() - iteration helper to probe a rproc device
219 * @dev: device to probe
220 * @uc_pdata: uclass data allocated for the device
223 * Return: 0 if all ok, else appropriate error value.
225 static int _rproc_probe_dev(struct udevice *dev,
226 struct dm_rproc_uclass_pdata *uc_pdata,
231 ret = device_probe(dev);
234 debug("%s: Failed to initialize - %d\n", dev->name, ret);
239 * _rproc_dev_is_probed() - check if the device has been probed
240 * @dev: device to check
244 * Return: -EAGAIN if not probed else return 0
246 static int _rproc_dev_is_probed(struct udevice *dev,
247 struct dm_rproc_uclass_pdata *uc_pdata,
250 if (dev->flags & DM_FLAG_ACTIVATED)
256 bool rproc_is_initialized(void)
258 int ret = for_each_remoteproc_device(_rproc_dev_is_probed, NULL, NULL);
259 return ret ? false : true;
266 if (rproc_is_initialized()) {
267 debug("Already initialized\n");
271 ret = for_each_remoteproc_device(_rproc_probe_dev, NULL, NULL);
275 int rproc_load(int id, ulong addr, ulong size)
277 struct udevice *dev = NULL;
278 struct dm_rproc_uclass_pdata *uc_pdata;
279 const struct dm_rproc_ops *ops;
282 ret = uclass_get_device_by_seq(UCLASS_REMOTEPROC, id, &dev);
284 debug("Unknown remote processor id '%d' requested(%d)\n",
289 uc_pdata = dev_get_uclass_platdata(dev);
291 ops = rproc_get_ops(dev);
293 debug("%s driver has no ops?\n", dev->name);
297 debug("Loading to '%s' from address 0x%08lX size of %lu bytes\n",
298 uc_pdata->name, addr, size);
300 return ops->load(dev, addr, size);
302 debug("%s: data corruption?? mandatory function is missing!\n",
309 * Completely internal helper enums..
310 * Keeping this isolated helps this code evolve independent of other
322 * _rproc_ops_wrapper() - wrapper for invoking remote proc driver callback
323 * @id: id of the remote processor
324 * @op: one of rproc_ops that indicate what operation to invoke
326 * Most of the checks and verification for remoteproc operations are more
327 * or less same for almost all operations. This allows us to put a wrapper
328 * and use the common checks to allow the driver to function appropriately.
330 * Return: 0 if all ok, else appropriate error value.
332 static int _rproc_ops_wrapper(int id, enum rproc_ops op)
334 struct udevice *dev = NULL;
335 struct dm_rproc_uclass_pdata *uc_pdata;
336 const struct dm_rproc_ops *ops;
337 int (*fn)(struct udevice *dev);
338 bool mandatory = false;
342 ret = uclass_get_device_by_seq(UCLASS_REMOTEPROC, id, &dev);
344 debug("Unknown remote processor id '%d' requested(%d)\n",
349 uc_pdata = dev_get_uclass_platdata(dev);
351 ops = rproc_get_ops(dev);
353 debug("%s driver has no ops?\n", dev->name);
368 op_str = "Resetting";
371 fn = ops->is_running;
372 op_str = "Checking if running:";
379 debug("what is '%d' operation??\n", op);
383 debug("%s %s...\n", op_str, uc_pdata->name);
388 debug("%s: data corruption?? mandatory function is missing!\n",
394 int rproc_start(int id)
396 return _rproc_ops_wrapper(id, RPROC_START);
399 int rproc_stop(int id)
401 return _rproc_ops_wrapper(id, RPROC_STOP);
404 int rproc_reset(int id)
406 return _rproc_ops_wrapper(id, RPROC_RESET);
409 int rproc_ping(int id)
411 return _rproc_ops_wrapper(id, RPROC_PING);
414 int rproc_is_running(int id)
416 return _rproc_ops_wrapper(id, RPROC_RUNNING);