1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013 Google, Inc
6 * Marek Vasut <marex@denx.de>
9 #define LOG_CATEGORY LOGC_DM
14 #include <dm/device.h>
15 #include <dm/device-internal.h>
17 #include <dm/platdata.h>
18 #include <dm/uclass.h>
21 #include <linux/compiler.h>
23 struct driver *lists_driver_lookup_name(const char *name)
26 ll_entry_start(struct driver, driver);
27 const int n_ents = ll_entry_count(struct driver, driver);
30 for (entry = drv; entry != drv + n_ents; entry++) {
31 if (!strcmp(name, entry->name))
39 struct uclass_driver *lists_uclass_lookup(enum uclass_id id)
41 struct uclass_driver *uclass =
42 ll_entry_start(struct uclass_driver, uclass);
43 const int n_ents = ll_entry_count(struct uclass_driver, uclass);
44 struct uclass_driver *entry;
46 for (entry = uclass; entry != uclass + n_ents; entry++) {
54 static int bind_drivers_pass(struct udevice *parent, bool pre_reloc_only)
56 struct driver_info *info =
57 ll_entry_start(struct driver_info, driver_info);
58 const int n_ents = ll_entry_count(struct driver_info, driver_info);
59 bool missing_parent = false;
64 * Do one iteration through the driver_info records. For of-platdata,
65 * bind only devices whose parent is already bound. If we find any
66 * device we can't bind, set missing_parent to true, which will cause
67 * this function to be called again.
69 for (idx = 0; idx < n_ents; idx++) {
70 struct udevice *par = parent;
71 const struct driver_info *entry = info + idx;
72 struct driver_rt *drt = gd_dm_driver_rt() + idx;
76 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
77 int parent_idx = driver_info_parent_id(entry);
82 if (CONFIG_IS_ENABLED(OF_PLATDATA_PARENT) &&
84 struct driver_rt *parent_drt;
86 parent_drt = gd_dm_driver_rt() + parent_idx;
87 if (!parent_drt->dev) {
88 missing_parent = true;
92 par = parent_drt->dev;
95 ret = device_bind_by_name(par, pre_reloc_only, entry, &dev);
97 if (CONFIG_IS_ENABLED(OF_PLATDATA))
99 } else if (ret != -EPERM) {
100 dm_warn("No match for driver '%s'\n", entry->name);
101 if (!result || ret != -ENOENT)
106 return result ? result : missing_parent ? -EAGAIN : 0;
109 int lists_bind_drivers(struct udevice *parent, bool pre_reloc_only)
115 * 10 passes is 10 levels deep in the devicetree, which is plenty. If
116 * OF_PLATDATA_PARENT is not enabled, then bind_drivers_pass() will
117 * always succeed on the first pass.
119 for (pass = 0; pass < 10; pass++) {
122 ret = bind_drivers_pass(parent, pre_reloc_only);
125 if (ret != -EAGAIN && !result)
132 int device_bind_driver(struct udevice *parent, const char *drv_name,
133 const char *dev_name, struct udevice **devp)
135 return device_bind_driver_to_node(parent, drv_name, dev_name,
136 ofnode_null(), devp);
139 int device_bind_driver_to_node(struct udevice *parent, const char *drv_name,
140 const char *dev_name, ofnode node,
141 struct udevice **devp)
146 drv = lists_driver_lookup_name(drv_name);
148 debug("Cannot find driver '%s'\n", drv_name);
151 ret = device_bind_with_driver_data(parent, drv, dev_name, 0 /* data */,
157 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
159 * driver_check_compatible() - Check if a driver matches a compatible string
161 * @param of_match: List of compatible strings to match
162 * @param of_idp: Returns the match that was found
163 * @param compat: The compatible string to search for
164 * @return 0 if there is a match, -ENOENT if no match
166 static int driver_check_compatible(const struct udevice_id *of_match,
167 const struct udevice_id **of_idp,
173 while (of_match->compatible) {
174 if (!strcmp(of_match->compatible, compat)) {
184 int lists_bind_fdt(struct udevice *parent, ofnode node, struct udevice **devp,
187 struct driver *driver = ll_entry_start(struct driver, driver);
188 const int n_ents = ll_entry_count(struct driver, driver);
189 const struct udevice_id *id;
190 struct driver *entry;
193 const char *name, *compat_list, *compat;
194 int compat_length, i;
200 name = ofnode_get_name(node);
201 log_debug("bind node %s\n", name);
203 compat_list = ofnode_get_property(node, "compatible", &compat_length);
205 if (compat_length == -FDT_ERR_NOTFOUND) {
206 log_debug("Device '%s' has no compatible string\n",
211 dm_warn("Device tree error at node '%s'\n", name);
212 return compat_length;
216 * Walk through the compatible string list, attempting to match each
217 * compatible string in order such that we match in order of priority
218 * from the first string to the last.
220 for (i = 0; i < compat_length; i += strlen(compat) + 1) {
221 compat = compat_list + i;
222 log_debug(" - attempt to match compatible string '%s'\n",
225 for (entry = driver; entry != driver + n_ents; entry++) {
226 ret = driver_check_compatible(entry->of_match, &id,
231 if (entry == driver + n_ents)
234 if (pre_reloc_only) {
235 if (!ofnode_pre_reloc(node) &&
236 !(entry->flags & DM_FLAG_PRE_RELOC)) {
237 log_debug("Skipping device pre-relocation\n");
242 log_debug(" - found match at '%s': '%s' matches '%s'\n",
243 entry->name, entry->of_match->compatible,
245 ret = device_bind_with_driver_data(parent, entry, name,
246 id->data, node, &dev);
247 if (ret == -ENODEV) {
248 log_debug("Driver '%s' refuses to bind\n", entry->name);
252 dm_warn("Error binding driver '%s': %d\n", entry->name,
263 if (!found && !result && ret != -ENODEV)
264 log_debug("No match for node '%s'\n", name);