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 <log.h>
14 #include <dm/device.h>
15 #include <dm/device-internal.h>
16 #include <dm/lists.h>
17 #include <dm/platdata.h>
18 #include <dm/uclass.h>
19 #include <dm/util.h>
20 #include <fdtdec.h>
21 #include <linux/compiler.h>
22 
lists_driver_lookup_name(const char * name)23 struct driver *lists_driver_lookup_name(const char *name)
24 {
25 	struct driver *drv =
26 		ll_entry_start(struct driver, driver);
27 	const int n_ents = ll_entry_count(struct driver, driver);
28 	struct driver *entry;
29 
30 	for (entry = drv; entry != drv + n_ents; entry++) {
31 		if (!strcmp(name, entry->name))
32 			return entry;
33 	}
34 
35 	/* Not found */
36 	return NULL;
37 }
38 
lists_uclass_lookup(enum uclass_id id)39 struct uclass_driver *lists_uclass_lookup(enum uclass_id id)
40 {
41 	struct uclass_driver *uclass =
42 		ll_entry_start(struct uclass_driver, uclass_driver);
43 	const int n_ents = ll_entry_count(struct uclass_driver, uclass_driver);
44 	struct uclass_driver *entry;
45 
46 	for (entry = uclass; entry != uclass + n_ents; entry++) {
47 		if (entry->id == id)
48 			return entry;
49 	}
50 
51 	return NULL;
52 }
53 
bind_drivers_pass(struct udevice * parent,bool pre_reloc_only)54 static int bind_drivers_pass(struct udevice *parent, bool pre_reloc_only)
55 {
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;
60 	int result = 0;
61 	uint idx;
62 
63 	/*
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.
68 	 */
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;
73 		struct udevice *dev;
74 		int ret;
75 
76 		if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
77 			int parent_idx = driver_info_parent_id(entry);
78 
79 			if (drt->dev)
80 				continue;
81 
82 			if (CONFIG_IS_ENABLED(OF_PLATDATA_PARENT) &&
83 			    parent_idx != -1) {
84 				struct driver_rt *parent_drt;
85 
86 				parent_drt = gd_dm_driver_rt() + parent_idx;
87 				if (!parent_drt->dev) {
88 					missing_parent = true;
89 					continue;
90 				}
91 
92 				par = parent_drt->dev;
93 			}
94 		}
95 		ret = device_bind_by_name(par, pre_reloc_only, entry, &dev);
96 		if (!ret) {
97 			if (CONFIG_IS_ENABLED(OF_PLATDATA))
98 				drt->dev = dev;
99 		} else if (ret != -EPERM) {
100 			dm_warn("No match for driver '%s'\n", entry->name);
101 			if (!result || ret != -ENOENT)
102 				result = ret;
103 		}
104 	}
105 
106 	return result ? result : missing_parent ? -EAGAIN : 0;
107 }
108 
lists_bind_drivers(struct udevice * parent,bool pre_reloc_only)109 int lists_bind_drivers(struct udevice *parent, bool pre_reloc_only)
110 {
111 	int result = 0;
112 	int pass;
113 
114 	/*
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.
118 	 */
119 	for (pass = 0; pass < 10; pass++) {
120 		int ret;
121 
122 		ret = bind_drivers_pass(parent, pre_reloc_only);
123 		if (!ret)
124 			break;
125 		if (ret != -EAGAIN && !result)
126 			result = ret;
127 	}
128 
129 	return result;
130 }
131 
device_bind_driver(struct udevice * parent,const char * drv_name,const char * dev_name,struct udevice ** devp)132 int device_bind_driver(struct udevice *parent, const char *drv_name,
133 		       const char *dev_name, struct udevice **devp)
134 {
135 	return device_bind_driver_to_node(parent, drv_name, dev_name,
136 					  ofnode_null(), devp);
137 }
138 
device_bind_driver_to_node(struct udevice * parent,const char * drv_name,const char * dev_name,ofnode node,struct udevice ** 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)
142 {
143 	struct driver *drv;
144 	int ret;
145 
146 	drv = lists_driver_lookup_name(drv_name);
147 	if (!drv) {
148 		debug("Cannot find driver '%s'\n", drv_name);
149 		return -ENOENT;
150 	}
151 	ret = device_bind_with_driver_data(parent, drv, dev_name, 0 /* data */,
152 					   node, devp);
153 
154 	return ret;
155 }
156 
157 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
158 /**
159  * driver_check_compatible() - Check if a driver matches a compatible string
160  *
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
165  */
driver_check_compatible(const struct udevice_id * of_match,const struct udevice_id ** of_idp,const char * compat)166 static int driver_check_compatible(const struct udevice_id *of_match,
167 				   const struct udevice_id **of_idp,
168 				   const char *compat)
169 {
170 	if (!of_match)
171 		return -ENOENT;
172 
173 	while (of_match->compatible) {
174 		if (!strcmp(of_match->compatible, compat)) {
175 			*of_idp = of_match;
176 			return 0;
177 		}
178 		of_match++;
179 	}
180 
181 	return -ENOENT;
182 }
183 
lists_bind_fdt(struct udevice * parent,ofnode node,struct udevice ** devp,bool pre_reloc_only)184 int lists_bind_fdt(struct udevice *parent, ofnode node, struct udevice **devp,
185 		   bool pre_reloc_only)
186 {
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;
191 	struct udevice *dev;
192 	bool found = false;
193 	const char *name, *compat_list, *compat;
194 	int compat_length, i;
195 	int result = 0;
196 	int ret = 0;
197 
198 	if (devp)
199 		*devp = NULL;
200 	name = ofnode_get_name(node);
201 	log_debug("bind node %s\n", name);
202 
203 	compat_list = ofnode_get_property(node, "compatible", &compat_length);
204 	if (!compat_list) {
205 		if (compat_length == -FDT_ERR_NOTFOUND) {
206 			log_debug("Device '%s' has no compatible string\n",
207 				  name);
208 			return 0;
209 		}
210 
211 		dm_warn("Device tree error at node '%s'\n", name);
212 		return compat_length;
213 	}
214 
215 	/*
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.
219 	 */
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",
223 			  compat);
224 
225 		for (entry = driver; entry != driver + n_ents; entry++) {
226 			ret = driver_check_compatible(entry->of_match, &id,
227 						      compat);
228 			if (!ret)
229 				break;
230 		}
231 		if (entry == driver + n_ents)
232 			continue;
233 
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");
238 				return 0;
239 			}
240 		}
241 
242 		log_debug("   - found match at '%s': '%s' matches '%s'\n",
243 			  entry->name, entry->of_match->compatible,
244 			  id->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);
249 			continue;
250 		}
251 		if (ret) {
252 			dm_warn("Error binding driver '%s': %d\n", entry->name,
253 				ret);
254 			return log_msg_ret("bind", ret);
255 		} else {
256 			found = true;
257 			if (devp)
258 				*devp = dev;
259 		}
260 		break;
261 	}
262 
263 	if (!found && !result && ret != -ENODEV)
264 		log_debug("No match for node '%s'\n", name);
265 
266 	return result;
267 }
268 #endif
269