Merge tag 'u-boot-atmel-fixes-2020.07-a' of https://gitlab.denx.de/u-boot/custodians...
[platform/kernel/u-boot.git] / drivers / core / lists.c
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
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
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);
43         const int n_ents = ll_entry_count(struct uclass_driver, uclass);
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
54 int lists_bind_drivers(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         struct driver_info *entry;
60         struct udevice *dev;
61         int result = 0;
62         int ret;
63
64         for (entry = info; entry != info + n_ents; entry++) {
65                 ret = device_bind_by_name(parent, pre_reloc_only, entry, &dev);
66                 if (ret && ret != -EPERM) {
67                         dm_warn("No match for driver '%s'\n", entry->name);
68                         if (!result || ret != -ENOENT)
69                                 result = ret;
70                 }
71         }
72
73         return result;
74 }
75
76 int device_bind_driver(struct udevice *parent, const char *drv_name,
77                        const char *dev_name, struct udevice **devp)
78 {
79         return device_bind_driver_to_node(parent, drv_name, dev_name,
80                                           ofnode_null(), devp);
81 }
82
83 int device_bind_driver_to_node(struct udevice *parent, const char *drv_name,
84                                const char *dev_name, ofnode node,
85                                struct udevice **devp)
86 {
87         struct driver *drv;
88         int ret;
89
90         drv = lists_driver_lookup_name(drv_name);
91         if (!drv) {
92                 debug("Cannot find driver '%s'\n", drv_name);
93                 return -ENOENT;
94         }
95         ret = device_bind_with_driver_data(parent, drv, dev_name, 0 /* data */,
96                                            node, devp);
97
98         return ret;
99 }
100
101 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
102 /**
103  * driver_check_compatible() - Check if a driver matches a compatible string
104  *
105  * @param of_match:     List of compatible strings to match
106  * @param of_idp:       Returns the match that was found
107  * @param compat:       The compatible string to search for
108  * @return 0 if there is a match, -ENOENT if no match
109  */
110 static int driver_check_compatible(const struct udevice_id *of_match,
111                                    const struct udevice_id **of_idp,
112                                    const char *compat)
113 {
114         if (!of_match)
115                 return -ENOENT;
116
117         while (of_match->compatible) {
118                 if (!strcmp(of_match->compatible, compat)) {
119                         *of_idp = of_match;
120                         return 0;
121                 }
122                 of_match++;
123         }
124
125         return -ENOENT;
126 }
127
128 int lists_bind_fdt(struct udevice *parent, ofnode node, struct udevice **devp,
129                    bool pre_reloc_only)
130 {
131         struct driver *driver = ll_entry_start(struct driver, driver);
132         const int n_ents = ll_entry_count(struct driver, driver);
133         const struct udevice_id *id;
134         struct driver *entry;
135         struct udevice *dev;
136         bool found = false;
137         const char *name, *compat_list, *compat;
138         int compat_length, i;
139         int result = 0;
140         int ret = 0;
141
142         if (devp)
143                 *devp = NULL;
144         name = ofnode_get_name(node);
145         log_debug("bind node %s\n", name);
146
147         compat_list = ofnode_get_property(node, "compatible", &compat_length);
148         if (!compat_list) {
149                 if (compat_length == -FDT_ERR_NOTFOUND) {
150                         log_debug("Device '%s' has no compatible string\n",
151                                   name);
152                         return 0;
153                 }
154
155                 dm_warn("Device tree error at node '%s'\n", name);
156                 return compat_length;
157         }
158
159         /*
160          * Walk through the compatible string list, attempting to match each
161          * compatible string in order such that we match in order of priority
162          * from the first string to the last.
163          */
164         for (i = 0; i < compat_length; i += strlen(compat) + 1) {
165                 compat = compat_list + i;
166                 log_debug("   - attempt to match compatible string '%s'\n",
167                           compat);
168
169                 for (entry = driver; entry != driver + n_ents; entry++) {
170                         ret = driver_check_compatible(entry->of_match, &id,
171                                                       compat);
172                         if (!ret)
173                                 break;
174                 }
175                 if (entry == driver + n_ents)
176                         continue;
177
178                 if (pre_reloc_only) {
179                         if (!ofnode_pre_reloc(node) &&
180                             !(entry->flags & DM_FLAG_PRE_RELOC)) {
181                                 log_debug("Skipping device pre-relocation\n");
182                                 return 0;
183                         }
184                 }
185
186                 log_debug("   - found match at '%s': '%s' matches '%s'\n",
187                           entry->name, entry->of_match->compatible,
188                           id->compatible);
189                 ret = device_bind_with_driver_data(parent, entry, name,
190                                                    id->data, node, &dev);
191                 if (ret == -ENODEV) {
192                         log_debug("Driver '%s' refuses to bind\n", entry->name);
193                         continue;
194                 }
195                 if (ret) {
196                         dm_warn("Error binding driver '%s': %d\n", entry->name,
197                                 ret);
198                         return ret;
199                 } else {
200                         found = true;
201                         if (devp)
202                                 *devp = dev;
203                 }
204                 break;
205         }
206
207         if (!found && !result && ret != -ENODEV)
208                 log_debug("No match for node '%s'\n", name);
209
210         return result;
211 }
212 #endif