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>
16 #include <dm/device.h>
17 #include <dm/device-internal.h>
20 #include <dm/of_access.h>
21 #include <dm/platdata.h>
24 #include <dm/uclass.h>
26 #include <linux/list.h>
28 DECLARE_GLOBAL_DATA_PTR;
30 static struct driver_info root_info = {
31 .name = "root_driver",
34 struct udevice *dm_root(void)
37 dm_warn("Virtual root driver does not exist!\n");
44 void dm_fixup_for_gd_move(struct global_data *new_gd)
46 /* The sentinel node has moved, so update things that point to it */
48 new_gd->uclass_root.next->prev = &new_gd->uclass_root;
49 new_gd->uclass_root.prev->next = &new_gd->uclass_root;
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 ((ulong)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;
131 int dm_init(bool of_live)
136 dm_warn("Virtual root driver already exists!\n");
139 INIT_LIST_HEAD(&DM_UCLASS_ROOT_NON_CONST);
141 if (IS_ENABLED(CONFIG_NEEDS_MANUAL_RELOC)) {
147 ret = device_bind_by_name(NULL, false, &root_info, &DM_ROOT_NON_CONST);
150 #if CONFIG_IS_ENABLED(OF_CONTROL)
151 if (CONFIG_IS_ENABLED(OF_LIVE) && of_live)
152 DM_ROOT_NON_CONST->node = np_to_ofnode(gd_of_root());
154 DM_ROOT_NON_CONST->node = offset_to_ofnode(0);
156 ret = device_probe(DM_ROOT_NON_CONST);
165 device_remove(dm_root(), DM_REMOVE_NORMAL);
166 device_unbind(dm_root());
172 #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
173 int dm_remove_devices_flags(uint flags)
175 device_remove(dm_root(), flags);
181 int dm_scan_platdata(bool pre_reloc_only)
185 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
186 struct driver_rt *dyn;
189 n_ents = ll_entry_count(struct driver_info, driver_info);
190 dyn = calloc(n_ents, sizeof(struct driver_rt));
193 gd_set_dm_driver_rt(dyn);
196 ret = lists_bind_drivers(DM_ROOT_NON_CONST, pre_reloc_only);
197 if (ret == -ENOENT) {
198 dm_warn("Some drivers were not found\n");
205 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
206 static int dm_scan_fdt_live(struct udevice *parent,
207 const struct device_node *node_parent,
210 struct device_node *np;
213 for (np = node_parent->child; np; np = np->sibling) {
215 if (!of_device_is_available(np)) {
216 pr_debug(" - ignoring disabled device\n");
219 err = lists_bind_fdt(parent, np_to_ofnode(np), NULL,
223 debug("%s: ret=%d\n", np->name, ret);
228 dm_warn("Some drivers failed to bind\n");
234 * dm_scan_fdt_node() - Scan the device tree and bind drivers for a node
236 * This scans the subnodes of a device tree node and and creates a driver
239 * @parent: Parent device for the devices that will be created
240 * @blob: Pointer to device tree blob
241 * @offset: Offset of node to scan
242 * @pre_reloc_only: If true, bind only drivers with the DM_FLAG_PRE_RELOC
243 * flag. If false bind all drivers.
244 * @return 0 if OK, -ve on error
246 static int dm_scan_fdt_node(struct udevice *parent, const void *blob,
247 int offset, bool pre_reloc_only)
251 for (offset = fdt_first_subnode(blob, offset);
253 offset = fdt_next_subnode(blob, offset)) {
254 const char *node_name = fdt_get_name(blob, offset, NULL);
256 if (!fdtdec_get_is_enabled(blob, offset)) {
257 pr_debug(" - ignoring disabled device\n");
260 err = lists_bind_fdt(parent, offset_to_ofnode(offset), NULL,
264 debug("%s: ret=%d\n", node_name, ret);
269 dm_warn("Some drivers failed to bind\n");
274 int dm_scan_fdt_dev(struct udevice *dev)
276 if (!dev_of_valid(dev))
279 if (of_live_active())
280 return dm_scan_fdt_live(dev, dev_np(dev),
281 gd->flags & GD_FLG_RELOC ? false : true);
283 return dm_scan_fdt_node(dev, gd->fdt_blob, dev_of_offset(dev),
284 gd->flags & GD_FLG_RELOC ? false : true);
287 int dm_scan_fdt(const void *blob, bool pre_reloc_only)
289 if (of_live_active())
290 return dm_scan_fdt_live(gd->dm_root, gd_of_root(),
293 return dm_scan_fdt_node(gd->dm_root, blob, 0, pre_reloc_only);
296 static int dm_scan_fdt_ofnode_path(const void *blob, const char *path,
301 node = ofnode_path(path);
302 if (!ofnode_valid(node))
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 if (CONFIG_IS_ENABLED(OF_PLATDATA))
351 dm_populate_phandle_data();
353 ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
355 debug("dm_init() failed: %d\n", ret);
358 ret = dm_scan_platdata(pre_reloc_only);
360 debug("dm_scan_platdata() failed: %d\n", ret);
364 if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
365 ret = dm_extended_scan_fdt(gd->fdt_blob, pre_reloc_only);
367 debug("dm_extended_scan_dt() failed: %d\n", ret);
372 ret = dm_scan_other(pre_reloc_only);
379 #ifdef CONFIG_ACPIGEN
380 static int root_acpi_get_name(const struct udevice *dev, char *out_name)
382 return acpi_copy_name(out_name, "\\_SB");
385 struct acpi_ops root_acpi_ops = {
386 .get_name = root_acpi_get_name,
390 /* This is the root driver - all drivers are children of this */
391 U_BOOT_DRIVER(root_driver) = {
392 .name = "root_driver",
394 ACPI_OPS_PTR(&root_acpi_ops)
397 /* This is the root uclass */
398 UCLASS_DRIVER(root) = {