1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (c) 2013 Google, Inc
4 *
5 * (C) Copyright 2012
6 * Marek Vasut <marex@denx.de>
7 */
8
9 #define LOG_CATEGORY LOGC_DM
10
11 #include <common.h>
12 #include <errno.h>
13 #include <dm/device.h>
14 #include <dm/device-internal.h>
15 #include <dm/lists.h>
16 #include <dm/platdata.h>
17 #include <dm/uclass.h>
18 #include <dm/util.h>
19 #include <fdtdec.h>
20 #include <linux/compiler.h>
21
lists_driver_lookup_name(const char * name)22 struct driver *lists_driver_lookup_name(const char *name)
23 {
24 struct driver *drv =
25 ll_entry_start(struct driver, driver);
26 const int n_ents = ll_entry_count(struct driver, driver);
27 struct driver *entry;
28
29 for (entry = drv; entry != drv + n_ents; entry++) {
30 if (!strcmp(name, entry->name))
31 return entry;
32 }
33
34 /* Not found */
35 return NULL;
36 }
37
lists_uclass_lookup(enum uclass_id id)38 struct uclass_driver *lists_uclass_lookup(enum uclass_id id)
39 {
40 struct uclass_driver *uclass =
41 ll_entry_start(struct uclass_driver, uclass);
42 const int n_ents = ll_entry_count(struct uclass_driver, uclass);
43 struct uclass_driver *entry;
44
45 for (entry = uclass; entry != uclass + n_ents; entry++) {
46 if (entry->id == id)
47 return entry;
48 }
49
50 return NULL;
51 }
52
lists_bind_drivers(struct udevice * parent,bool pre_reloc_only)53 int lists_bind_drivers(struct udevice *parent, bool pre_reloc_only)
54 {
55 struct driver_info *info =
56 ll_entry_start(struct driver_info, driver_info);
57 const int n_ents = ll_entry_count(struct driver_info, driver_info);
58 struct driver_info *entry;
59 struct udevice *dev;
60 int result = 0;
61 int ret;
62
63 for (entry = info; entry != info + n_ents; entry++) {
64 ret = device_bind_by_name(parent, pre_reloc_only, entry, &dev);
65 if (ret && ret != -EPERM) {
66 dm_warn("No match for driver '%s'\n", entry->name);
67 if (!result || ret != -ENOENT)
68 result = ret;
69 }
70 }
71
72 return result;
73 }
74
device_bind_driver(struct udevice * parent,const char * drv_name,const char * dev_name,struct udevice ** devp)75 int device_bind_driver(struct udevice *parent, const char *drv_name,
76 const char *dev_name, struct udevice **devp)
77 {
78 return device_bind_driver_to_node(parent, drv_name, dev_name,
79 ofnode_null(), devp);
80 }
81
device_bind_driver_to_node(struct udevice * parent,const char * drv_name,const char * dev_name,ofnode node,struct udevice ** devp)82 int device_bind_driver_to_node(struct udevice *parent, const char *drv_name,
83 const char *dev_name, ofnode node,
84 struct udevice **devp)
85 {
86 struct driver *drv;
87 int ret;
88
89 drv = lists_driver_lookup_name(drv_name);
90 if (!drv) {
91 debug("Cannot find driver '%s'\n", drv_name);
92 return -ENOENT;
93 }
94 ret = device_bind_with_driver_data(parent, drv, dev_name, 0 /* data */,
95 node, devp);
96
97 return ret;
98 }
99
100 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
101 /**
102 * driver_check_compatible() - Check if a driver matches a compatible string
103 *
104 * @param of_match: List of compatible strings to match
105 * @param of_idp: Returns the match that was found
106 * @param compat: The compatible string to search for
107 * @return 0 if there is a match, -ENOENT if no match
108 */
driver_check_compatible(const struct udevice_id * of_match,const struct udevice_id ** of_idp,const char * compat)109 static int driver_check_compatible(const struct udevice_id *of_match,
110 const struct udevice_id **of_idp,
111 const char *compat)
112 {
113 if (!of_match)
114 return -ENOENT;
115
116 while (of_match->compatible) {
117 if (!strcmp(of_match->compatible, compat)) {
118 *of_idp = of_match;
119 return 0;
120 }
121 of_match++;
122 }
123
124 return -ENOENT;
125 }
126
lists_bind_fdt(struct udevice * parent,ofnode node,struct udevice ** devp,bool pre_reloc_only)127 int lists_bind_fdt(struct udevice *parent, ofnode node, struct udevice **devp,
128 bool pre_reloc_only)
129 {
130 struct driver *driver = ll_entry_start(struct driver, driver);
131 const int n_ents = ll_entry_count(struct driver, driver);
132 const struct udevice_id *id;
133 struct driver *entry;
134 struct udevice *dev;
135 bool found = false;
136 const char *name, *compat_list, *compat;
137 int compat_length, i;
138 int result = 0;
139 int ret = 0;
140
141 if (devp)
142 *devp = NULL;
143 name = ofnode_get_name(node);
144 log_debug("bind node %s\n", name);
145
146 compat_list = ofnode_get_property(node, "compatible", &compat_length);
147 if (!compat_list) {
148 if (compat_length == -FDT_ERR_NOTFOUND) {
149 log_debug("Device '%s' has no compatible string\n",
150 name);
151 return 0;
152 }
153
154 dm_warn("Device tree error at node '%s'\n", name);
155 return compat_length;
156 }
157
158 /*
159 * Walk through the compatible string list, attempting to match each
160 * compatible string in order such that we match in order of priority
161 * from the first string to the last.
162 */
163 for (i = 0; i < compat_length; i += strlen(compat) + 1) {
164 compat = compat_list + i;
165 log_debug(" - attempt to match compatible string '%s'\n",
166 compat);
167
168 for (entry = driver; entry != driver + n_ents; entry++) {
169 ret = driver_check_compatible(entry->of_match, &id,
170 compat);
171 if (!ret)
172 break;
173 }
174 if (entry == driver + n_ents)
175 continue;
176
177 if (pre_reloc_only) {
178 if (!dm_ofnode_pre_reloc(node) &&
179 !(entry->flags & DM_FLAG_PRE_RELOC))
180 return 0;
181 }
182
183 log_debug(" - found match at '%s': '%s' matches '%s'\n",
184 entry->name, entry->of_match->compatible,
185 id->compatible);
186 ret = device_bind_with_driver_data(parent, entry, name,
187 id->data, node, &dev);
188 if (ret == -ENODEV) {
189 log_debug("Driver '%s' refuses to bind\n", entry->name);
190 continue;
191 }
192 if (ret) {
193 dm_warn("Error binding driver '%s': %d\n", entry->name,
194 ret);
195 return ret;
196 } else {
197 found = true;
198 if (devp)
199 *devp = dev;
200 }
201 break;
202 }
203
204 if (!found && !result && ret != -ENODEV)
205 log_debug("No match for node '%s'\n", name);
206
207 return result;
208 }
209 #endif
210