1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013 Google, Inc
6 * Pavel Herrmann <morpheus.ibis@gmail.com>
14 #include <linux/libfdt.h>
15 #include <dm/device.h>
16 #include <dm/device-internal.h>
19 #include <dm/of_access.h>
20 #include <dm/platdata.h>
23 #include <dm/uclass.h>
25 #include <linux/list.h>
27 DECLARE_GLOBAL_DATA_PTR;
29 static const struct driver_info root_info = {
30 .name = "root_driver",
33 struct udevice *dm_root(void)
36 dm_warn("Virtual root driver does not exist!\n");
43 void dm_fixup_for_gd_move(struct global_data *new_gd)
45 /* The sentinel node has moved, so update things that point to it */
47 new_gd->uclass_root.next->prev = &new_gd->uclass_root;
48 new_gd->uclass_root.prev->next = &new_gd->uclass_root;
52 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
53 void fix_drivers(void)
56 ll_entry_start(struct driver, driver);
57 const int n_ents = ll_entry_count(struct driver, driver);
60 for (entry = drv; entry != drv + n_ents; entry++) {
62 entry->of_match = (const struct udevice_id *)
63 ((u32)entry->of_match + gd->reloc_off);
65 entry->bind += gd->reloc_off;
67 entry->probe += gd->reloc_off;
69 entry->remove += gd->reloc_off;
71 entry->unbind += gd->reloc_off;
72 if (entry->ofdata_to_platdata)
73 entry->ofdata_to_platdata += gd->reloc_off;
74 if (entry->child_post_bind)
75 entry->child_post_bind += gd->reloc_off;
76 if (entry->child_pre_probe)
77 entry->child_pre_probe += gd->reloc_off;
78 if (entry->child_post_remove)
79 entry->child_post_remove += gd->reloc_off;
80 /* OPS are fixed in every uclass post_probe function */
82 entry->ops += gd->reloc_off;
88 struct uclass_driver *uclass =
89 ll_entry_start(struct uclass_driver, uclass);
90 const int n_ents = ll_entry_count(struct uclass_driver, uclass);
91 struct uclass_driver *entry;
93 for (entry = uclass; entry != uclass + n_ents; entry++) {
95 entry->post_bind += gd->reloc_off;
96 if (entry->pre_unbind)
97 entry->pre_unbind += gd->reloc_off;
99 entry->pre_probe += gd->reloc_off;
100 if (entry->post_probe)
101 entry->post_probe += gd->reloc_off;
102 if (entry->pre_remove)
103 entry->pre_remove += gd->reloc_off;
104 if (entry->child_post_bind)
105 entry->child_post_bind += gd->reloc_off;
106 if (entry->child_pre_probe)
107 entry->child_pre_probe += gd->reloc_off;
109 entry->init += gd->reloc_off;
111 entry->destroy += gd->reloc_off;
112 /* FIXME maybe also need to fix these ops */
114 entry->ops += gd->reloc_off;
118 void fix_devices(void)
120 struct driver_info *dev =
121 ll_entry_start(struct driver_info, driver_info);
122 const int n_ents = ll_entry_count(struct driver_info, driver_info);
123 struct driver_info *entry;
125 for (entry = dev; entry != dev + n_ents; entry++) {
127 entry->platdata += gd->reloc_off;
133 int dm_init(bool of_live)
138 dm_warn("Virtual root driver already exists!\n");
141 INIT_LIST_HEAD(&DM_UCLASS_ROOT_NON_CONST);
143 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
149 ret = device_bind_by_name(NULL, false, &root_info, &DM_ROOT_NON_CONST);
152 #if CONFIG_IS_ENABLED(OF_CONTROL)
153 # if CONFIG_IS_ENABLED(OF_LIVE)
155 DM_ROOT_NON_CONST->node = np_to_ofnode(gd->of_root);
158 DM_ROOT_NON_CONST->node = offset_to_ofnode(0);
160 ret = device_probe(DM_ROOT_NON_CONST);
169 device_remove(dm_root(), DM_REMOVE_NORMAL);
170 device_unbind(dm_root());
176 #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
177 int dm_remove_devices_flags(uint flags)
179 device_remove(dm_root(), flags);
185 int dm_scan_platdata(bool pre_reloc_only)
189 ret = lists_bind_drivers(DM_ROOT_NON_CONST, pre_reloc_only);
190 if (ret == -ENOENT) {
191 dm_warn("Some drivers were not found\n");
198 #if CONFIG_IS_ENABLED(OF_LIVE)
199 static int dm_scan_fdt_live(struct udevice *parent,
200 const struct device_node *node_parent,
203 struct device_node *np;
206 for (np = node_parent->child; np; np = np->sibling) {
208 if (!of_device_is_available(np)) {
209 pr_debug(" - ignoring disabled device\n");
212 err = lists_bind_fdt(parent, np_to_ofnode(np), NULL,
216 debug("%s: ret=%d\n", np->name, ret);
221 dm_warn("Some drivers failed to bind\n");
225 #endif /* CONFIG_IS_ENABLED(OF_LIVE) */
227 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
229 * dm_scan_fdt_node() - Scan the device tree and bind drivers for a node
231 * This scans the subnodes of a device tree node and and creates a driver
234 * @parent: Parent device for the devices that will be created
235 * @blob: Pointer to device tree blob
236 * @offset: Offset of node to scan
237 * @pre_reloc_only: If true, bind only drivers with the DM_FLAG_PRE_RELOC
238 * flag. If false bind all drivers.
239 * @return 0 if OK, -ve on error
241 static int dm_scan_fdt_node(struct udevice *parent, const void *blob,
242 int offset, bool pre_reloc_only)
246 for (offset = fdt_first_subnode(blob, offset);
248 offset = fdt_next_subnode(blob, offset)) {
249 const char *node_name = fdt_get_name(blob, offset, NULL);
251 if (!fdtdec_get_is_enabled(blob, offset)) {
252 pr_debug(" - ignoring disabled device\n");
255 err = lists_bind_fdt(parent, offset_to_ofnode(offset), NULL,
259 debug("%s: ret=%d\n", node_name, ret);
264 dm_warn("Some drivers failed to bind\n");
269 int dm_scan_fdt_dev(struct udevice *dev)
271 if (!dev_of_valid(dev))
274 #if CONFIG_IS_ENABLED(OF_LIVE)
275 if (of_live_active())
276 return dm_scan_fdt_live(dev, dev_np(dev),
277 gd->flags & GD_FLG_RELOC ? false : true);
280 return dm_scan_fdt_node(dev, gd->fdt_blob, dev_of_offset(dev),
281 gd->flags & GD_FLG_RELOC ? false : true);
284 int dm_scan_fdt(const void *blob, bool pre_reloc_only)
286 #if CONFIG_IS_ENABLED(OF_LIVE)
287 if (of_live_active())
288 return dm_scan_fdt_live(gd->dm_root, gd->of_root,
292 return dm_scan_fdt_node(gd->dm_root, blob, 0, pre_reloc_only);
295 static int dm_scan_fdt_ofnode_path(const void *blob, const char *path,
300 node = ofnode_path(path);
301 if (!ofnode_valid(node))
304 #if CONFIG_IS_ENABLED(OF_LIVE)
305 if (of_live_active())
306 return dm_scan_fdt_live(gd->dm_root, node.np, pre_reloc_only);
308 return dm_scan_fdt_node(gd->dm_root, blob, node.of_offset,
312 int dm_extended_scan_fdt(const void *blob, bool pre_reloc_only)
315 const char * const nodes[] = {
321 ret = dm_scan_fdt(blob, pre_reloc_only);
323 debug("dm_scan_fdt() failed: %d\n", ret);
327 /* Some nodes aren't devices themselves but may contain some */
328 for (i = 0; i < ARRAY_SIZE(nodes); i++) {
329 ret = dm_scan_fdt_ofnode_path(blob, nodes[i], pre_reloc_only);
331 debug("dm_scan_fdt() scan for %s failed: %d\n",
341 __weak int dm_scan_other(bool pre_reloc_only)
346 int dm_init_and_scan(bool pre_reloc_only)
350 ret = dm_init(IS_ENABLED(CONFIG_OF_LIVE));
352 debug("dm_init() failed: %d\n", ret);
355 ret = dm_scan_platdata(pre_reloc_only);
357 debug("dm_scan_platdata() failed: %d\n", ret);
361 if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
362 ret = dm_extended_scan_fdt(gd->fdt_blob, pre_reloc_only);
364 debug("dm_extended_scan_dt() failed: %d\n", ret);
369 ret = dm_scan_other(pre_reloc_only);
376 /* This is the root driver - all drivers are children of this */
377 U_BOOT_DRIVER(root_driver) = {
378 .name = "root_driver",
382 /* This is the root uclass */
383 UCLASS_DRIVER(root) = {