1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013 Google, Inc
6 * Pavel Herrmann <morpheus.ibis@gmail.com>
9 #define LOG_CATEGORY UCLASS_ROOT
16 #include <asm-generic/sections.h>
17 #include <asm/global_data.h>
18 #include <linux/libfdt.h>
20 #include <dm/device.h>
21 #include <dm/device-internal.h>
24 #include <dm/of_access.h>
25 #include <dm/platdata.h>
28 #include <dm/uclass.h>
29 #include <dm/uclass-internal.h>
31 #include <linux/list.h>
33 DECLARE_GLOBAL_DATA_PTR;
35 static struct driver_info root_info = {
36 .name = "root_driver",
39 struct udevice *dm_root(void)
42 dm_warn("Virtual root driver does not exist!\n");
49 void dm_fixup_for_gd_move(struct global_data *new_gd)
51 /* The sentinel node has moved, so update things that point to it */
53 new_gd->uclass_root->next->prev = new_gd->uclass_root;
54 new_gd->uclass_root->prev->next = new_gd->uclass_root;
58 void fix_drivers(void)
61 ll_entry_start(struct driver, driver);
62 const int n_ents = ll_entry_count(struct driver, driver);
65 for (entry = drv; entry != drv + n_ents; entry++) {
67 entry->of_match = (const struct udevice_id *)
68 ((ulong)entry->of_match + gd->reloc_off);
70 entry->bind += gd->reloc_off;
72 entry->probe += gd->reloc_off;
74 entry->remove += gd->reloc_off;
76 entry->unbind += gd->reloc_off;
77 if (entry->of_to_plat)
78 entry->of_to_plat += gd->reloc_off;
79 if (entry->child_post_bind)
80 entry->child_post_bind += gd->reloc_off;
81 if (entry->child_pre_probe)
82 entry->child_pre_probe += gd->reloc_off;
83 if (entry->child_post_remove)
84 entry->child_post_remove += gd->reloc_off;
85 /* OPS are fixed in every uclass post_probe function */
87 entry->ops += gd->reloc_off;
93 struct uclass_driver *uclass =
94 ll_entry_start(struct uclass_driver, uclass_driver);
95 const int n_ents = ll_entry_count(struct uclass_driver, uclass_driver);
96 struct uclass_driver *entry;
98 for (entry = uclass; entry != uclass + n_ents; entry++) {
100 entry->post_bind += gd->reloc_off;
101 if (entry->pre_unbind)
102 entry->pre_unbind += gd->reloc_off;
103 if (entry->pre_probe)
104 entry->pre_probe += gd->reloc_off;
105 if (entry->post_probe)
106 entry->post_probe += gd->reloc_off;
107 if (entry->pre_remove)
108 entry->pre_remove += gd->reloc_off;
109 if (entry->child_post_bind)
110 entry->child_post_bind += gd->reloc_off;
111 if (entry->child_pre_probe)
112 entry->child_pre_probe += gd->reloc_off;
114 entry->init += gd->reloc_off;
116 entry->destroy += gd->reloc_off;
120 void fix_devices(void)
122 struct driver_info *dev =
123 ll_entry_start(struct driver_info, driver_info);
124 const int n_ents = ll_entry_count(struct driver_info, driver_info);
125 struct driver_info *entry;
127 for (entry = dev; entry != dev + n_ents; entry++) {
129 entry->plat += gd->reloc_off;
133 static int dm_setup_inst(void)
135 DM_ROOT_NON_CONST = DM_DEVICE_GET(root);
137 if (CONFIG_IS_ENABLED(OF_PLATDATA_RT)) {
138 struct udevice_rt *urt;
143 /* Allocate the udevice_rt table */
144 n_ents = ll_entry_count(struct udevice, udevice);
145 urt = calloc(n_ents, sizeof(struct udevice_rt));
147 return log_msg_ret("urt", -ENOMEM);
148 gd_set_dm_udevice_rt(urt);
150 /* Now allocate space for the priv/plat data, and copy it in */
151 size = __priv_data_end - __priv_data_start;
153 base = calloc(1, size);
155 return log_msg_ret("priv", -ENOMEM);
156 memcpy(base, __priv_data_start, size);
157 gd_set_dm_priv_base(base);
163 int dm_init(bool of_live)
168 dm_warn("Virtual root driver already exists!\n");
171 if (CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
172 gd->uclass_root = &uclass_head;
174 gd->uclass_root = &DM_UCLASS_ROOT_S_NON_CONST;
175 INIT_LIST_HEAD(DM_UCLASS_ROOT_NON_CONST);
178 if (IS_ENABLED(CONFIG_NEEDS_MANUAL_RELOC)) {
184 if (CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
185 ret = dm_setup_inst();
187 log_debug("dm_setup_inst() failed: %d\n", ret);
191 ret = device_bind_by_name(NULL, false, &root_info,
195 if (CONFIG_IS_ENABLED(OF_CONTROL))
196 dev_set_ofnode(DM_ROOT_NON_CONST, ofnode_root());
197 ret = device_probe(DM_ROOT_NON_CONST);
207 /* Remove non-vital devices first */
208 device_remove(dm_root(), DM_REMOVE_NON_VITAL);
209 device_remove(dm_root(), DM_REMOVE_NORMAL);
210 device_unbind(dm_root());
216 #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
217 int dm_remove_devices_flags(uint flags)
219 device_remove(dm_root(), flags);
225 int dm_scan_plat(bool pre_reloc_only)
229 if (CONFIG_IS_ENABLED(OF_PLATDATA_DRIVER_RT)) {
230 struct driver_rt *dyn;
233 n_ents = ll_entry_count(struct driver_info, driver_info);
234 dyn = calloc(n_ents, sizeof(struct driver_rt));
237 gd_set_dm_driver_rt(dyn);
240 ret = lists_bind_drivers(DM_ROOT_NON_CONST, pre_reloc_only);
241 if (ret == -ENOENT) {
242 dm_warn("Some drivers were not found\n");
249 #if CONFIG_IS_ENABLED(OF_REAL)
251 * dm_scan_fdt_node() - Scan the device tree and bind drivers for a node
253 * This scans the subnodes of a device tree node and and creates a driver
256 * @parent: Parent device for the devices that will be created
257 * @node: Node to scan
258 * @pre_reloc_only: If true, bind only drivers with the DM_FLAG_PRE_RELOC
259 * flag. If false bind all drivers.
260 * @return 0 if OK, -ve on error
262 static int dm_scan_fdt_node(struct udevice *parent, ofnode parent_node,
265 int ret = 0, err = 0;
268 if (!ofnode_valid(parent_node))
271 for (node = ofnode_first_subnode(parent_node);
273 node = ofnode_next_subnode(node)) {
274 const char *node_name = ofnode_get_name(node);
276 if (!ofnode_is_enabled(node)) {
277 pr_debug(" - ignoring disabled device\n");
280 err = lists_bind_fdt(parent, node, NULL, NULL, pre_reloc_only);
283 debug("%s: ret=%d\n", node_name, ret);
288 dm_warn("Some drivers failed to bind\n");
293 int dm_scan_fdt_dev(struct udevice *dev)
295 return dm_scan_fdt_node(dev, dev_ofnode(dev),
296 gd->flags & GD_FLG_RELOC ? false : true);
299 int dm_scan_fdt(bool pre_reloc_only)
301 return dm_scan_fdt_node(gd->dm_root, ofnode_root(), pre_reloc_only);
304 static int dm_scan_fdt_ofnode_path(const char *path, bool pre_reloc_only)
308 node = ofnode_path(path);
310 return dm_scan_fdt_node(gd->dm_root, node, pre_reloc_only);
313 int dm_extended_scan(bool pre_reloc_only)
316 const char * const nodes[] = {
322 ret = dm_scan_fdt(pre_reloc_only);
324 debug("dm_scan_fdt() failed: %d\n", ret);
328 /* Some nodes aren't devices themselves but may contain some */
329 for (i = 0; i < ARRAY_SIZE(nodes); i++) {
330 ret = dm_scan_fdt_ofnode_path(nodes[i], pre_reloc_only);
332 debug("dm_scan_fdt() scan for %s failed: %d\n",
342 __weak int dm_scan_other(bool pre_reloc_only)
347 #if CONFIG_IS_ENABLED(OF_PLATDATA_INST) && CONFIG_IS_ENABLED(READ_ONLY)
348 void *dm_priv_to_rw(void *priv)
350 long offset = priv - (void *)__priv_data_start;
352 return gd_dm_priv_base() + offset;
357 * dm_scan() - Scan tables to bind devices
359 * Runs through the driver_info tables and binds the devices it finds. Then runs
360 * through the devicetree nodes. Finally calls dm_scan_other() to add any
363 * @pre_reloc_only: If true, bind only nodes with special devicetree properties,
364 * or drivers with the DM_FLAG_PRE_RELOC flag. If false bind all drivers.
366 static int dm_scan(bool pre_reloc_only)
370 ret = dm_scan_plat(pre_reloc_only);
372 debug("dm_scan_plat() failed: %d\n", ret);
376 if (CONFIG_IS_ENABLED(OF_REAL)) {
377 ret = dm_extended_scan(pre_reloc_only);
379 debug("dm_extended_scan() failed: %d\n", ret);
384 ret = dm_scan_other(pre_reloc_only);
391 int dm_init_and_scan(bool pre_reloc_only)
395 ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
397 debug("dm_init() failed: %d\n", ret);
400 if (!CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
401 ret = dm_scan(pre_reloc_only);
403 log_debug("dm_scan() failed: %d\n", ret);
411 void dm_get_stats(int *device_countp, int *uclass_countp)
413 *device_countp = device_get_decendent_count(gd->dm_root);
414 *uclass_countp = uclass_get_count();
417 #ifdef CONFIG_ACPIGEN
418 static int root_acpi_get_name(const struct udevice *dev, char *out_name)
420 return acpi_copy_name(out_name, "\\_SB");
423 struct acpi_ops root_acpi_ops = {
424 .get_name = root_acpi_get_name,
428 /* This is the root driver - all drivers are children of this */
429 U_BOOT_DRIVER(root_driver) = {
430 .name = "root_driver",
432 ACPI_OPS_PTR(&root_acpi_ops)
435 /* This is the root uclass */
436 UCLASS_DRIVER(root) = {