2 * Copyright (c) 2013 Google, Inc
5 * Pavel Herrmann <morpheus.ibis@gmail.com>
7 * SPDX-License-Identifier: GPL-2.0+
15 #include <dm/device.h>
16 #include <dm/device-internal.h>
18 #include <dm/platdata.h>
20 #include <dm/uclass.h>
22 #include <linux/list.h>
24 DECLARE_GLOBAL_DATA_PTR;
27 fdt_addr_t translation_offset; /* optional translation offset */
30 static const 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 fdt_addr_t dm_get_translation_offset(void)
46 struct udevice *root = dm_root();
47 struct root_priv *priv = dev_get_priv(root);
49 return priv->translation_offset;
52 void dm_set_translation_offset(fdt_addr_t offs)
54 struct udevice *root = dm_root();
55 struct root_priv *priv = dev_get_priv(root);
57 priv->translation_offset = offs;
60 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
61 void fix_drivers(void)
64 ll_entry_start(struct driver, driver);
65 const int n_ents = ll_entry_count(struct driver, driver);
68 for (entry = drv; entry != drv + n_ents; entry++) {
70 entry->of_match = (const struct udevice_id *)
71 ((u32)entry->of_match + gd->reloc_off);
73 entry->bind += gd->reloc_off;
75 entry->probe += gd->reloc_off;
77 entry->remove += gd->reloc_off;
79 entry->unbind += gd->reloc_off;
80 if (entry->ofdata_to_platdata)
81 entry->ofdata_to_platdata += gd->reloc_off;
82 if (entry->child_post_bind)
83 entry->child_post_bind += gd->reloc_off;
84 if (entry->child_pre_probe)
85 entry->child_pre_probe += gd->reloc_off;
86 if (entry->child_post_remove)
87 entry->child_post_remove += gd->reloc_off;
88 /* OPS are fixed in every uclass post_probe function */
90 entry->ops += gd->reloc_off;
96 struct uclass_driver *uclass =
97 ll_entry_start(struct uclass_driver, uclass);
98 const int n_ents = ll_entry_count(struct uclass_driver, uclass);
99 struct uclass_driver *entry;
101 for (entry = uclass; entry != uclass + n_ents; entry++) {
102 if (entry->post_bind)
103 entry->post_bind += gd->reloc_off;
104 if (entry->pre_unbind)
105 entry->pre_unbind += gd->reloc_off;
106 if (entry->pre_probe)
107 entry->pre_probe += gd->reloc_off;
108 if (entry->post_probe)
109 entry->post_probe += gd->reloc_off;
110 if (entry->pre_remove)
111 entry->pre_remove += gd->reloc_off;
112 if (entry->child_post_bind)
113 entry->child_post_bind += gd->reloc_off;
114 if (entry->child_pre_probe)
115 entry->child_pre_probe += gd->reloc_off;
117 entry->init += gd->reloc_off;
119 entry->destroy += gd->reloc_off;
120 /* FIXME maybe also need to fix these ops */
122 entry->ops += gd->reloc_off;
132 dm_warn("Virtual root driver already exists!\n");
135 INIT_LIST_HEAD(&DM_UCLASS_ROOT_NON_CONST);
137 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
142 ret = device_bind_by_name(NULL, false, &root_info, &DM_ROOT_NON_CONST);
145 #if CONFIG_IS_ENABLED(OF_CONTROL)
146 DM_ROOT_NON_CONST->of_offset = 0;
148 ret = device_probe(DM_ROOT_NON_CONST);
157 device_remove(dm_root());
158 device_unbind(dm_root());
163 int dm_scan_platdata(bool pre_reloc_only)
167 ret = lists_bind_drivers(DM_ROOT_NON_CONST, pre_reloc_only);
168 if (ret == -ENOENT) {
169 dm_warn("Some drivers were not found\n");
176 #if CONFIG_IS_ENABLED(OF_CONTROL)
177 int dm_scan_fdt_node(struct udevice *parent, const void *blob, int offset,
182 for (offset = fdt_first_subnode(blob, offset);
184 offset = fdt_next_subnode(blob, offset)) {
185 if (pre_reloc_only &&
186 !fdt_getprop(blob, offset, "u-boot,dm-pre-reloc", NULL))
188 if (!fdtdec_get_is_enabled(blob, offset)) {
189 dm_dbg(" - ignoring disabled device\n");
192 err = lists_bind_fdt(parent, blob, offset, NULL);
195 debug("%s: ret=%d\n", fdt_get_name(blob, offset, NULL),
201 dm_warn("Some drivers failed to bind\n");
206 int dm_scan_fdt(const void *blob, bool pre_reloc_only)
208 return dm_scan_fdt_node(gd->dm_root, blob, 0, pre_reloc_only);
212 __weak int dm_scan_other(bool pre_reloc_only)
217 int dm_init_and_scan(bool pre_reloc_only)
223 debug("dm_init() failed: %d\n", ret);
226 ret = dm_scan_platdata(pre_reloc_only);
228 debug("dm_scan_platdata() failed: %d\n", ret);
232 if (CONFIG_IS_ENABLED(OF_CONTROL)) {
233 ret = dm_scan_fdt(gd->fdt_blob, pre_reloc_only);
235 debug("dm_scan_fdt() failed: %d\n", ret);
240 ret = dm_scan_other(pre_reloc_only);
247 /* This is the root driver - all drivers are children of this */
248 U_BOOT_DRIVER(root_driver) = {
249 .name = "root_driver",
251 .priv_auto_alloc_size = sizeof(struct root_priv),
254 /* This is the root uclass */
255 UCLASS_DRIVER(root) = {