xref: /openbmc/u-boot/drivers/core/lists.c (revision 3dc23f78)
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 	if (!drv || !n_ents)
29 		return NULL;
30 
31 	for (entry = drv; entry != drv + n_ents; entry++) {
32 		if (!strcmp(name, entry->name))
33 			return entry;
34 	}
35 
36 	/* Not found */
37 	return NULL;
38 }
39 
40 struct uclass_driver *lists_uclass_lookup(enum uclass_id id)
41 {
42 	struct uclass_driver *uclass =
43 		ll_entry_start(struct uclass_driver, uclass);
44 	const int n_ents = ll_entry_count(struct uclass_driver, uclass);
45 	struct uclass_driver *entry;
46 
47 	if ((id == UCLASS_INVALID) || !uclass)
48 		return NULL;
49 
50 	for (entry = uclass; entry != uclass + n_ents; entry++) {
51 		if (entry->id == id)
52 			return entry;
53 	}
54 
55 	return NULL;
56 }
57 
58 int lists_bind_drivers(struct udevice *parent, bool pre_reloc_only)
59 {
60 	struct driver_info *info =
61 		ll_entry_start(struct driver_info, driver_info);
62 	const int n_ents = ll_entry_count(struct driver_info, driver_info);
63 	struct driver_info *entry;
64 	struct udevice *dev;
65 	int result = 0;
66 	int ret;
67 
68 	for (entry = info; entry != info + n_ents; entry++) {
69 		ret = device_bind_by_name(parent, pre_reloc_only, entry, &dev);
70 		if (ret && ret != -EPERM) {
71 			dm_warn("No match for driver '%s'\n", entry->name);
72 			if (!result || ret != -ENOENT)
73 				result = ret;
74 		}
75 	}
76 
77 	return result;
78 }
79 
80 #ifdef CONFIG_OF_CONTROL
81 /**
82  * driver_check_compatible() - Check if a driver is compatible with this node
83  *
84  * @param blob:		Device tree pointer
85  * @param offset:	Offset of node in device tree
86  * @param of_matchL	List of compatible strings to match
87  * @return 0 if there is a match, -ENOENT if no match, -ENODEV if the node
88  * does not have a compatible string, other error <0 if there is a device
89  * tree error
90  */
91 static int driver_check_compatible(const void *blob, int offset,
92 				   const struct udevice_id *of_match)
93 {
94 	int ret;
95 
96 	if (!of_match)
97 		return -ENOENT;
98 
99 	while (of_match->compatible) {
100 		ret = fdt_node_check_compatible(blob, offset,
101 						of_match->compatible);
102 		if (!ret)
103 			return 0;
104 		else if (ret == -FDT_ERR_NOTFOUND)
105 			return -ENODEV;
106 		else if (ret < 0)
107 			return -EINVAL;
108 		of_match++;
109 	}
110 
111 	return -ENOENT;
112 }
113 
114 int lists_bind_fdt(struct udevice *parent, const void *blob, int offset,
115 		   struct udevice **devp)
116 {
117 	struct driver *driver = ll_entry_start(struct driver, driver);
118 	const int n_ents = ll_entry_count(struct driver, driver);
119 	struct driver *entry;
120 	struct udevice *dev;
121 	bool found = false;
122 	const char *name;
123 	int result = 0;
124 	int ret = 0;
125 
126 	dm_dbg("bind node %s\n", fdt_get_name(blob, offset, NULL));
127 	if (devp)
128 		*devp = NULL;
129 	for (entry = driver; entry != driver + n_ents; entry++) {
130 		ret = driver_check_compatible(blob, offset, entry->of_match);
131 		name = fdt_get_name(blob, offset, NULL);
132 		if (ret == -ENOENT) {
133 			continue;
134 		} else if (ret == -ENODEV) {
135 			dm_dbg("Device '%s' has no compatible string\n", name);
136 			break;
137 		} else if (ret) {
138 			dm_warn("Device tree error at offset %d\n", offset);
139 			if (!result || ret != -ENOENT)
140 				result = ret;
141 			break;
142 		}
143 
144 		dm_dbg("   - found match at '%s'\n", entry->name);
145 		ret = device_bind(parent, entry, name, NULL, offset, &dev);
146 		if (ret) {
147 			dm_warn("Error binding driver '%s'\n", entry->name);
148 			return ret;
149 		} else {
150 			found = true;
151 			if (devp)
152 				*devp = dev;
153 		}
154 		break;
155 	}
156 
157 	if (!found && !result && ret != -ENODEV) {
158 		dm_dbg("No match for node '%s'\n",
159 		       fdt_get_name(blob, offset, NULL));
160 	}
161 
162 	return result;
163 }
164 #endif
165