1
2
3
4
5
6
7
8
9
10#include <common.h>
11#include <errno.h>
12#include <dm/device.h>
13#include <dm/device-internal.h>
14#include <dm/lists.h>
15#include <dm/platdata.h>
16#include <dm/uclass.h>
17#include <dm/util.h>
18#include <fdtdec.h>
19#include <linux/compiler.h>
20
21struct driver *lists_driver_lookup_name(const char *name)
22{
23 struct driver *drv =
24 ll_entry_start(struct driver, driver);
25 const int n_ents = ll_entry_count(struct driver, driver);
26 struct driver *entry;
27
28 for (entry = drv; entry != drv + n_ents; entry++) {
29 if (!strcmp(name, entry->name))
30 return entry;
31 }
32
33
34 return NULL;
35}
36
37struct uclass_driver *lists_uclass_lookup(enum uclass_id id)
38{
39 struct uclass_driver *uclass =
40 ll_entry_start(struct uclass_driver, uclass);
41 const int n_ents = ll_entry_count(struct uclass_driver, uclass);
42 struct uclass_driver *entry;
43
44 for (entry = uclass; entry != uclass + n_ents; entry++) {
45 if (entry->id == id)
46 return entry;
47 }
48
49 return NULL;
50}
51
52int lists_bind_drivers(struct udevice *parent, bool pre_reloc_only)
53{
54 struct driver_info *info =
55 ll_entry_start(struct driver_info, driver_info);
56 const int n_ents = ll_entry_count(struct driver_info, driver_info);
57 struct driver_info *entry;
58 struct udevice *dev;
59 int result = 0;
60 int ret;
61
62 for (entry = info; entry != info + n_ents; entry++) {
63 ret = device_bind_by_name(parent, pre_reloc_only, entry, &dev);
64 if (ret && ret != -EPERM) {
65 dm_warn("No match for driver '%s'\n", entry->name);
66 if (!result || ret != -ENOENT)
67 result = ret;
68 }
69 }
70
71 return result;
72}
73
74int device_bind_driver(struct udevice *parent, const char *drv_name,
75 const char *dev_name, struct udevice **devp)
76{
77 return device_bind_driver_to_node(parent, drv_name, dev_name, -1, devp);
78}
79
80int device_bind_driver_to_node(struct udevice *parent, const char *drv_name,
81 const char *dev_name, int node,
82 struct udevice **devp)
83{
84 struct driver *drv;
85 int ret;
86
87 drv = lists_driver_lookup_name(drv_name);
88 if (!drv) {
89 debug("Cannot find driver '%s'\n", drv_name);
90 return -ENOENT;
91 }
92 ret = device_bind(parent, drv, dev_name, NULL, node, devp);
93 if (ret) {
94 debug("Cannot create device named '%s' (err=%d)\n",
95 dev_name, ret);
96 return ret;
97 }
98
99 return 0;
100}
101
102#if CONFIG_IS_ENABLED(OF_CONTROL)
103
104
105
106
107
108
109
110
111
112
113
114static int driver_check_compatible(const void *blob, int offset,
115 const struct udevice_id *of_match,
116 const struct udevice_id **of_idp)
117{
118 int ret;
119
120 *of_idp = NULL;
121 if (!of_match)
122 return -ENOENT;
123
124 while (of_match->compatible) {
125 ret = fdt_node_check_compatible(blob, offset,
126 of_match->compatible);
127 if (!ret) {
128 *of_idp = of_match;
129 return 0;
130 } else if (ret == -FDT_ERR_NOTFOUND) {
131 return -ENODEV;
132 } else if (ret < 0) {
133 return -EINVAL;
134 }
135 of_match++;
136 }
137
138 return -ENOENT;
139}
140
141int lists_bind_fdt(struct udevice *parent, const void *blob, int offset,
142 struct udevice **devp)
143{
144 struct driver *driver = ll_entry_start(struct driver, driver);
145 const int n_ents = ll_entry_count(struct driver, driver);
146 const struct udevice_id *id;
147 struct driver *entry;
148 struct udevice *dev;
149 bool found = false;
150 const char *name;
151 int result = 0;
152 int ret = 0;
153
154 dm_dbg("bind node %s\n", fdt_get_name(blob, offset, NULL));
155 if (devp)
156 *devp = NULL;
157 for (entry = driver; entry != driver + n_ents; entry++) {
158 ret = driver_check_compatible(blob, offset, entry->of_match,
159 &id);
160 name = fdt_get_name(blob, offset, NULL);
161 if (ret == -ENOENT) {
162 continue;
163 } else if (ret == -ENODEV) {
164 dm_dbg("Device '%s' has no compatible string\n", name);
165 break;
166 } else if (ret) {
167 dm_warn("Device tree error at offset %d\n", offset);
168 result = ret;
169 break;
170 }
171
172 dm_dbg(" - found match at '%s'\n", entry->name);
173 ret = device_bind_with_driver_data(parent, entry, name,
174 id->data, offset, &dev);
175 if (ret == -ENODEV) {
176 dm_dbg("Driver '%s' refuses to bind\n", entry->name);
177 continue;
178 }
179 if (ret) {
180 dm_warn("Error binding driver '%s': %d\n", entry->name,
181 ret);
182 return ret;
183 } else {
184 found = true;
185 if (devp)
186 *devp = dev;
187 }
188 break;
189 }
190
191 if (!found && !result && ret != -ENODEV) {
192 dm_dbg("No match for node '%s'\n",
193 fdt_get_name(blob, offset, NULL));
194 }
195
196 return result;
197}
198#endif
199