2 * Copyright (c) 2013 Google, Inc
7 * SPDX-License-Identifier: GPL-2.0+
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;
30 fdt_addr_t translation_offset; /* optional translation offset */
33 static const struct driver_info root_info = {
34 .name = "root_driver",
37 struct udevice *dm_root(void)
40 dm_warn("Virtual root driver does not exist!\n");
47 void dm_fixup_for_gd_move(struct global_data *new_gd)
49 /* The sentinel node has moved, so update things that point to it */
51 new_gd->uclass_root.next->prev = &new_gd->uclass_root;
52 new_gd->uclass_root.prev->next = &new_gd->uclass_root;
56 fdt_addr_t dm_get_translation_offset(void)
58 struct udevice *root = dm_root();
59 struct root_priv *priv = dev_get_priv(root);
61 return priv->translation_offset;
64 void dm_set_translation_offset(fdt_addr_t offs)
66 struct udevice *root = dm_root();
67 struct root_priv *priv = dev_get_priv(root);
69 priv->translation_offset = offs;
72 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
73 void fix_drivers(void)
76 ll_entry_start(struct driver, driver);
77 const int n_ents = ll_entry_count(struct driver, driver);
80 for (entry = drv; entry != drv + n_ents; entry++) {
82 entry->of_match = (const struct udevice_id *)
83 ((u32)entry->of_match + gd->reloc_off);
85 entry->bind += gd->reloc_off;
87 entry->probe += gd->reloc_off;
89 entry->remove += gd->reloc_off;
91 entry->unbind += gd->reloc_off;
92 if (entry->ofdata_to_platdata)
93 entry->ofdata_to_platdata += gd->reloc_off;
94 if (entry->child_post_bind)
95 entry->child_post_bind += gd->reloc_off;
96 if (entry->child_pre_probe)
97 entry->child_pre_probe += gd->reloc_off;
98 if (entry->child_post_remove)
99 entry->child_post_remove += gd->reloc_off;
100 /* OPS are fixed in every uclass post_probe function */
102 entry->ops += gd->reloc_off;
106 void fix_uclass(void)
108 struct uclass_driver *uclass =
109 ll_entry_start(struct uclass_driver, uclass);
110 const int n_ents = ll_entry_count(struct uclass_driver, uclass);
111 struct uclass_driver *entry;
113 for (entry = uclass; entry != uclass + n_ents; entry++) {
114 if (entry->post_bind)
115 entry->post_bind += gd->reloc_off;
116 if (entry->pre_unbind)
117 entry->pre_unbind += gd->reloc_off;
118 if (entry->pre_probe)
119 entry->pre_probe += gd->reloc_off;
120 if (entry->post_probe)
121 entry->post_probe += gd->reloc_off;
122 if (entry->pre_remove)
123 entry->pre_remove += gd->reloc_off;
124 if (entry->child_post_bind)
125 entry->child_post_bind += gd->reloc_off;
126 if (entry->child_pre_probe)
127 entry->child_pre_probe += gd->reloc_off;
129 entry->init += gd->reloc_off;
131 entry->destroy += gd->reloc_off;
132 /* FIXME maybe also need to fix these ops */
134 entry->ops += gd->reloc_off;
138 void fix_devices(void)
140 struct driver_info *dev =
141 ll_entry_start(struct driver_info, driver_info);
142 const int n_ents = ll_entry_count(struct driver_info, driver_info);
143 struct driver_info *entry;
145 for (entry = dev; entry != dev + n_ents; entry++) {
147 entry->platdata += gd->reloc_off;
153 int dm_init(bool of_live)
158 dm_warn("Virtual root driver already exists!\n");
161 INIT_LIST_HEAD(&DM_UCLASS_ROOT_NON_CONST);
163 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
169 ret = device_bind_by_name(NULL, false, &root_info, &DM_ROOT_NON_CONST);
172 #if CONFIG_IS_ENABLED(OF_CONTROL)
173 # if CONFIG_IS_ENABLED(OF_LIVE)
175 DM_ROOT_NON_CONST->node = np_to_ofnode(gd->of_root);
178 DM_ROOT_NON_CONST->node = offset_to_ofnode(0);
180 ret = device_probe(DM_ROOT_NON_CONST);
189 device_remove(dm_root(), DM_REMOVE_NORMAL);
190 device_unbind(dm_root());
195 #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
196 int dm_remove_devices_flags(uint flags)
198 device_remove(dm_root(), flags);
204 int dm_scan_platdata(bool pre_reloc_only)
208 ret = lists_bind_drivers(DM_ROOT_NON_CONST, pre_reloc_only);
209 if (ret == -ENOENT) {
210 dm_warn("Some drivers were not found\n");
217 #if CONFIG_IS_ENABLED(OF_LIVE)
218 static int dm_scan_fdt_live(struct udevice *parent,
219 const struct device_node *node_parent,
222 struct device_node *np;
225 for (np = node_parent->child; np; np = np->sibling) {
226 if (pre_reloc_only &&
227 !of_find_property(np, "u-boot,dm-pre-reloc", NULL))
229 if (!of_device_is_available(np)) {
230 pr_debug(" - ignoring disabled device\n");
233 err = lists_bind_fdt(parent, np_to_ofnode(np), NULL);
236 debug("%s: ret=%d\n", np->name, ret);
241 dm_warn("Some drivers failed to bind\n");
245 #endif /* CONFIG_IS_ENABLED(OF_LIVE) */
247 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
249 * dm_scan_fdt_node() - Scan the device tree and bind drivers for a node
251 * This scans the subnodes of a device tree node and and creates a driver
254 * @parent: Parent device for the devices that will be created
255 * @blob: Pointer to device tree blob
256 * @offset: Offset of node to scan
257 * @pre_reloc_only: If true, bind only drivers with the DM_FLAG_PRE_RELOC
258 * flag. If false bind all drivers.
259 * @return 0 if OK, -ve on error
261 static int dm_scan_fdt_node(struct udevice *parent, const void *blob,
262 int offset, bool pre_reloc_only)
266 for (offset = fdt_first_subnode(blob, offset);
268 offset = fdt_next_subnode(blob, offset)) {
269 /* "chosen" node isn't a device itself but may contain some: */
270 if (!strcmp(fdt_get_name(blob, offset, NULL), "chosen")) {
271 pr_debug("parsing subnodes of \"chosen\"\n");
273 err = dm_scan_fdt_node(parent, blob, offset,
280 if (pre_reloc_only &&
281 !dm_fdt_pre_reloc(blob, offset))
283 if (!fdtdec_get_is_enabled(blob, offset)) {
284 pr_debug(" - ignoring disabled device\n");
287 err = lists_bind_fdt(parent, offset_to_ofnode(offset), NULL);
290 debug("%s: ret=%d\n", fdt_get_name(blob, offset, NULL),
296 dm_warn("Some drivers failed to bind\n");
301 int dm_scan_fdt_dev(struct udevice *dev)
303 if (!dev_of_valid(dev))
306 #if CONFIG_IS_ENABLED(OF_LIVE)
307 if (of_live_active())
308 return dm_scan_fdt_live(dev, dev_np(dev),
309 gd->flags & GD_FLG_RELOC ? false : true);
312 return dm_scan_fdt_node(dev, gd->fdt_blob, dev_of_offset(dev),
313 gd->flags & GD_FLG_RELOC ? false : true);
316 int dm_scan_fdt(const void *blob, bool pre_reloc_only)
318 #if CONFIG_IS_ENABLED(OF_LIVE)
319 if (of_live_active())
320 return dm_scan_fdt_live(gd->dm_root, gd->of_root,
324 return dm_scan_fdt_node(gd->dm_root, blob, 0, pre_reloc_only);
327 static int dm_scan_fdt_node(struct udevice *parent, const void *blob,
328 int offset, bool pre_reloc_only)
334 int dm_extended_scan_fdt(const void *blob, bool pre_reloc_only)
339 ret = dm_scan_fdt(gd->fdt_blob, pre_reloc_only);
341 debug("dm_scan_fdt() failed: %d\n", ret);
345 /* bind fixed-clock */
346 node = ofnode_path("/clocks");
347 /* if no DT "clocks" node, no need to go further */
348 if (!ofnode_valid(node))
351 #if CONFIG_IS_ENABLED(OF_LIVE)
352 if (of_live_active())
353 ret = dm_scan_fdt_live(gd->dm_root, node.np, pre_reloc_only);
356 ret = dm_scan_fdt_node(gd->dm_root, gd->fdt_blob, node.of_offset,
359 debug("dm_scan_fdt_node() failed: %d\n", ret);
364 __weak int dm_scan_other(bool pre_reloc_only)
369 int dm_init_and_scan(bool pre_reloc_only)
373 ret = dm_init(IS_ENABLED(CONFIG_OF_LIVE));
375 debug("dm_init() failed: %d\n", ret);
378 ret = dm_scan_platdata(pre_reloc_only);
380 debug("dm_scan_platdata() failed: %d\n", ret);
384 if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
385 ret = dm_extended_scan_fdt(gd->fdt_blob, pre_reloc_only);
387 debug("dm_extended_scan_dt() failed: %d\n", ret);
392 ret = dm_scan_other(pre_reloc_only);
399 /* This is the root driver - all drivers are children of this */
400 U_BOOT_DRIVER(root_driver) = {
401 .name = "root_driver",
403 .priv_auto_alloc_size = sizeof(struct root_priv),
406 /* This is the root uclass */
407 UCLASS_DRIVER(root) = {