xref: /openbmc/u-boot/drivers/core/lists.c (revision 2f20aa82)
1 /*
2  * Copyright (c) 2013 Google, Inc
3  *
4  * (C) Copyright 2012
5  * Marek Vasut <marex@denx.de>
6  *
7  * SPDX-License-Identifier:	GPL-2.0+
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 
21 struct 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 	/* Not found */
34 	return NULL;
35 }
36 
37 struct 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 
52 int 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 
74 int device_bind_driver(struct udevice *parent, const char *drv_name,
75 		       const char *dev_name, struct udevice **devp)
76 {
77 	struct driver *drv;
78 	int ret;
79 
80 	drv = lists_driver_lookup_name(drv_name);
81 	if (!drv) {
82 		printf("Cannot find driver '%s'\n", drv_name);
83 		return -ENOENT;
84 	}
85 	ret = device_bind(parent, drv, dev_name, NULL, -1, devp);
86 	if (ret) {
87 		printf("Cannot create device named '%s' (err=%d)\n",
88 		       dev_name, ret);
89 		return ret;
90 	}
91 
92 	return 0;
93 }
94 
95 #ifdef CONFIG_OF_CONTROL
96 /**
97  * driver_check_compatible() - Check if a driver is compatible with this node
98  *
99  * @param blob:		Device tree pointer
100  * @param offset:	Offset of node in device tree
101  * @param of_match:	List of compatible strings to match
102  * @param of_idp:	Returns the match that was found
103  * @return 0 if there is a match, -ENOENT if no match, -ENODEV if the node
104  * does not have a compatible string, other error <0 if there is a device
105  * tree error
106  */
107 static int driver_check_compatible(const void *blob, int offset,
108 				   const struct udevice_id *of_match,
109 				   const struct udevice_id **of_idp)
110 {
111 	int ret;
112 
113 	*of_idp = NULL;
114 	if (!of_match)
115 		return -ENOENT;
116 
117 	while (of_match->compatible) {
118 		ret = fdt_node_check_compatible(blob, offset,
119 						of_match->compatible);
120 		if (!ret) {
121 			*of_idp = of_match;
122 			return 0;
123 		} else if (ret == -FDT_ERR_NOTFOUND) {
124 			return -ENODEV;
125 		} else if (ret < 0) {
126 			return -EINVAL;
127 		}
128 		of_match++;
129 	}
130 
131 	return -ENOENT;
132 }
133 
134 int lists_bind_fdt(struct udevice *parent, const void *blob, int offset,
135 		   struct udevice **devp)
136 {
137 	struct driver *driver = ll_entry_start(struct driver, driver);
138 	const int n_ents = ll_entry_count(struct driver, driver);
139 	const struct udevice_id *id;
140 	struct driver *entry;
141 	struct udevice *dev;
142 	bool found = false;
143 	const char *name;
144 	int result = 0;
145 	int ret = 0;
146 
147 	dm_dbg("bind node %s\n", fdt_get_name(blob, offset, NULL));
148 	if (devp)
149 		*devp = NULL;
150 	for (entry = driver; entry != driver + n_ents; entry++) {
151 		ret = driver_check_compatible(blob, offset, entry->of_match,
152 					      &id);
153 		name = fdt_get_name(blob, offset, NULL);
154 		if (ret == -ENOENT) {
155 			continue;
156 		} else if (ret == -ENODEV) {
157 			dm_dbg("Device '%s' has no compatible string\n", name);
158 			break;
159 		} else if (ret) {
160 			dm_warn("Device tree error at offset %d\n", offset);
161 			result = ret;
162 			break;
163 		}
164 
165 		dm_dbg("   - found match at '%s'\n", entry->name);
166 		ret = device_bind(parent, entry, name, NULL, offset, &dev);
167 		if (ret) {
168 			dm_warn("Error binding driver '%s'\n", entry->name);
169 			return ret;
170 		} else {
171 			dev->of_id = id;
172 			found = true;
173 			if (devp)
174 				*devp = dev;
175 		}
176 		break;
177 	}
178 
179 	if (!found && !result && ret != -ENODEV) {
180 		dm_dbg("No match for node '%s'\n",
181 		       fdt_get_name(blob, offset, NULL));
182 	}
183 
184 	return result;
185 }
186 #endif
187