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>
30 #include <linux/list.h>
32 DECLARE_GLOBAL_DATA_PTR;
34 static struct driver_info root_info = {
35 .name = "root_driver",
38 struct udevice *dm_root(void)
41 dm_warn("Virtual root driver does not exist!\n");
48 void dm_fixup_for_gd_move(struct global_data *new_gd)
50 /* The sentinel node has moved, so update things that point to it */
52 new_gd->uclass_root->next->prev = new_gd->uclass_root;
53 new_gd->uclass_root->prev->next = new_gd->uclass_root;
57 void fix_drivers(void)
60 ll_entry_start(struct driver, driver);
61 const int n_ents = ll_entry_count(struct driver, driver);
64 for (entry = drv; entry != drv + n_ents; entry++) {
66 entry->of_match = (const struct udevice_id *)
67 ((ulong)entry->of_match + gd->reloc_off);
69 entry->bind += gd->reloc_off;
71 entry->probe += gd->reloc_off;
73 entry->remove += gd->reloc_off;
75 entry->unbind += gd->reloc_off;
76 if (entry->of_to_plat)
77 entry->of_to_plat += gd->reloc_off;
78 if (entry->child_post_bind)
79 entry->child_post_bind += gd->reloc_off;
80 if (entry->child_pre_probe)
81 entry->child_pre_probe += gd->reloc_off;
82 if (entry->child_post_remove)
83 entry->child_post_remove += gd->reloc_off;
84 /* OPS are fixed in every uclass post_probe function */
86 entry->ops += gd->reloc_off;
92 struct uclass_driver *uclass =
93 ll_entry_start(struct uclass_driver, uclass_driver);
94 const int n_ents = ll_entry_count(struct uclass_driver, uclass_driver);
95 struct uclass_driver *entry;
97 for (entry = uclass; entry != uclass + n_ents; entry++) {
99 entry->post_bind += gd->reloc_off;
100 if (entry->pre_unbind)
101 entry->pre_unbind += gd->reloc_off;
102 if (entry->pre_probe)
103 entry->pre_probe += gd->reloc_off;
104 if (entry->post_probe)
105 entry->post_probe += gd->reloc_off;
106 if (entry->pre_remove)
107 entry->pre_remove += gd->reloc_off;
108 if (entry->child_post_bind)
109 entry->child_post_bind += gd->reloc_off;
110 if (entry->child_pre_probe)
111 entry->child_pre_probe += gd->reloc_off;
113 entry->init += gd->reloc_off;
115 entry->destroy += gd->reloc_off;
119 void fix_devices(void)
121 struct driver_info *dev =
122 ll_entry_start(struct driver_info, driver_info);
123 const int n_ents = ll_entry_count(struct driver_info, driver_info);
124 struct driver_info *entry;
126 for (entry = dev; entry != dev + n_ents; entry++) {
128 entry->plat += gd->reloc_off;
132 static int dm_setup_inst(void)
134 DM_ROOT_NON_CONST = DM_DEVICE_GET(root);
136 if (CONFIG_IS_ENABLED(OF_PLATDATA_RT)) {
137 struct udevice_rt *urt;
142 /* Allocate the udevice_rt table */
143 n_ents = ll_entry_count(struct udevice, udevice);
144 urt = calloc(n_ents, sizeof(struct udevice_rt));
146 return log_msg_ret("urt", -ENOMEM);
147 gd_set_dm_udevice_rt(urt);
149 /* Now allocate space for the priv/plat data, and copy it in */
150 size = __priv_data_end - __priv_data_start;
152 base = calloc(1, size);
154 return log_msg_ret("priv", -ENOMEM);
155 memcpy(base, __priv_data_start, size);
156 gd_set_dm_priv_base(base);
162 int dm_init(bool of_live)
167 dm_warn("Virtual root driver already exists!\n");
170 if (CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
171 gd->uclass_root = &uclass_head;
173 gd->uclass_root = &DM_UCLASS_ROOT_S_NON_CONST;
174 INIT_LIST_HEAD(DM_UCLASS_ROOT_NON_CONST);
177 if (IS_ENABLED(CONFIG_NEEDS_MANUAL_RELOC)) {
183 if (CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
184 ret = dm_setup_inst();
186 log_debug("dm_setup_inst() failed: %d\n", ret);
190 ret = device_bind_by_name(NULL, false, &root_info,
194 if (CONFIG_IS_ENABLED(OF_CONTROL))
195 dev_set_ofnode(DM_ROOT_NON_CONST, ofnode_root());
196 ret = device_probe(DM_ROOT_NON_CONST);
206 /* Remove non-vital devices first */
207 device_remove(dm_root(), DM_REMOVE_NON_VITAL);
208 device_remove(dm_root(), DM_REMOVE_NORMAL);
209 device_unbind(dm_root());
215 #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
216 int dm_remove_devices_flags(uint flags)
218 device_remove(dm_root(), flags);
224 int dm_scan_plat(bool pre_reloc_only)
228 if (CONFIG_IS_ENABLED(OF_PLATDATA_DRIVER_RT)) {
229 struct driver_rt *dyn;
232 n_ents = ll_entry_count(struct driver_info, driver_info);
233 dyn = calloc(n_ents, sizeof(struct driver_rt));
236 gd_set_dm_driver_rt(dyn);
239 ret = lists_bind_drivers(DM_ROOT_NON_CONST, pre_reloc_only);
240 if (ret == -ENOENT) {
241 dm_warn("Some drivers were not found\n");
248 #if CONFIG_IS_ENABLED(OF_REAL)
250 * dm_scan_fdt_node() - Scan the device tree and bind drivers for a node
252 * This scans the subnodes of a device tree node and and creates a driver
255 * @parent: Parent device for the devices that will be created
256 * @node: 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, ofnode parent_node,
264 int ret = 0, err = 0;
267 if (!ofnode_valid(parent_node))
270 for (node = ofnode_first_subnode(parent_node);
272 node = ofnode_next_subnode(node)) {
273 const char *node_name = ofnode_get_name(node);
275 if (!ofnode_is_enabled(node)) {
276 pr_debug(" - ignoring disabled device\n");
279 err = lists_bind_fdt(parent, node, NULL, NULL, pre_reloc_only);
282 debug("%s: ret=%d\n", node_name, ret);
287 dm_warn("Some drivers failed to bind\n");
292 int dm_scan_fdt_dev(struct udevice *dev)
294 return dm_scan_fdt_node(dev, dev_ofnode(dev),
295 gd->flags & GD_FLG_RELOC ? false : true);
298 int dm_scan_fdt(bool pre_reloc_only)
300 return dm_scan_fdt_node(gd->dm_root, ofnode_root(), pre_reloc_only);
303 static int dm_scan_fdt_ofnode_path(const char *path, bool pre_reloc_only)
307 node = ofnode_path(path);
309 return dm_scan_fdt_node(gd->dm_root, node, pre_reloc_only);
312 int dm_extended_scan(bool pre_reloc_only)
315 const char * const nodes[] = {
321 ret = dm_scan_fdt(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(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 #if CONFIG_IS_ENABLED(OF_PLATDATA_INST) && CONFIG_IS_ENABLED(READ_ONLY)
347 void *dm_priv_to_rw(void *priv)
349 long offset = priv - (void *)__priv_data_start;
351 return gd_dm_priv_base() + offset;
356 * dm_scan() - Scan tables to bind devices
358 * Runs through the driver_info tables and binds the devices it finds. Then runs
359 * through the devicetree nodes. Finally calls dm_scan_other() to add any
362 * @pre_reloc_only: If true, bind only nodes with special devicetree properties,
363 * or drivers with the DM_FLAG_PRE_RELOC flag. If false bind all drivers.
365 static int dm_scan(bool pre_reloc_only)
369 ret = dm_scan_plat(pre_reloc_only);
371 debug("dm_scan_plat() failed: %d\n", ret);
375 if (CONFIG_IS_ENABLED(OF_REAL)) {
376 ret = dm_extended_scan(pre_reloc_only);
378 debug("dm_extended_scan() failed: %d\n", ret);
383 ret = dm_scan_other(pre_reloc_only);
390 int dm_init_and_scan(bool pre_reloc_only)
394 ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
396 debug("dm_init() failed: %d\n", ret);
399 if (!CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
400 ret = dm_scan(pre_reloc_only);
402 log_debug("dm_scan() failed: %d\n", ret);
410 #ifdef CONFIG_ACPIGEN
411 static int root_acpi_get_name(const struct udevice *dev, char *out_name)
413 return acpi_copy_name(out_name, "\\_SB");
416 struct acpi_ops root_acpi_ops = {
417 .get_name = root_acpi_get_name,
421 /* This is the root driver - all drivers are children of this */
422 U_BOOT_DRIVER(root_driver) = {
423 .name = "root_driver",
425 ACPI_OPS_PTR(&root_acpi_ops)
428 /* This is the root uclass */
429 UCLASS_DRIVER(root) = {