]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
6494d708 SG |
2 | /* |
3 | * Copyright (c) 2013 Google, Inc | |
4 | * | |
5 | * (C) Copyright 2012 | |
6 | * Pavel Herrmann <[email protected]> | |
6494d708 SG |
7 | */ |
8 | ||
b953ec2b PD |
9 | #define LOG_CATEGORY UCLASS_ROOT |
10 | ||
6494d708 SG |
11 | #include <common.h> |
12 | #include <errno.h> | |
94f7afdf | 13 | #include <fdtdec.h> |
f7ae49fc | 14 | #include <log.h> |
6494d708 | 15 | #include <malloc.h> |
baf03718 | 16 | #include <asm-generic/sections.h> |
401d1c4f | 17 | #include <asm/global_data.h> |
b08c8c48 | 18 | #include <linux/libfdt.h> |
4b724a13 | 19 | #include <dm/acpi.h> |
6494d708 SG |
20 | #include <dm/device.h> |
21 | #include <dm/device-internal.h> | |
22 | #include <dm/lists.h> | |
19c8205e SG |
23 | #include <dm/of.h> |
24 | #include <dm/of_access.h> | |
6494d708 | 25 | #include <dm/platdata.h> |
19c8205e | 26 | #include <dm/read.h> |
fd536d81 | 27 | #include <dm/root.h> |
6494d708 SG |
28 | #include <dm/uclass.h> |
29 | #include <dm/util.h> | |
30 | #include <linux/list.h> | |
31 | ||
32 | DECLARE_GLOBAL_DATA_PTR; | |
33 | ||
908d0243 | 34 | static struct driver_info root_info = { |
6494d708 SG |
35 | .name = "root_driver", |
36 | }; | |
37 | ||
54c5d08a | 38 | struct udevice *dm_root(void) |
6494d708 SG |
39 | { |
40 | if (!gd->dm_root) { | |
41 | dm_warn("Virtual root driver does not exist!\n"); | |
42 | return NULL; | |
43 | } | |
44 | ||
45 | return gd->dm_root; | |
46 | } | |
47 | ||
2f11cd91 SG |
48 | void dm_fixup_for_gd_move(struct global_data *new_gd) |
49 | { | |
50 | /* The sentinel node has moved, so update things that point to it */ | |
b0d9512a | 51 | if (gd->dm_root) { |
8a715530 SG |
52 | new_gd->uclass_root->next->prev = new_gd->uclass_root; |
53 | new_gd->uclass_root->prev->next = new_gd->uclass_root; | |
b0d9512a | 54 | } |
2f11cd91 SG |
55 | } |
56 | ||
484fdf5b MS |
57 | void fix_drivers(void) |
58 | { | |
59 | struct driver *drv = | |
60 | ll_entry_start(struct driver, driver); | |
61 | const int n_ents = ll_entry_count(struct driver, driver); | |
62 | struct driver *entry; | |
63 | ||
64 | for (entry = drv; entry != drv + n_ents; entry++) { | |
65 | if (entry->of_match) | |
66 | entry->of_match = (const struct udevice_id *) | |
a652d9c7 | 67 | ((ulong)entry->of_match + gd->reloc_off); |
484fdf5b MS |
68 | if (entry->bind) |
69 | entry->bind += gd->reloc_off; | |
70 | if (entry->probe) | |
71 | entry->probe += gd->reloc_off; | |
72 | if (entry->remove) | |
73 | entry->remove += gd->reloc_off; | |
74 | if (entry->unbind) | |
75 | entry->unbind += gd->reloc_off; | |
d1998a9f SG |
76 | if (entry->of_to_plat) |
77 | entry->of_to_plat += gd->reloc_off; | |
31e1029a MS |
78 | if (entry->child_post_bind) |
79 | entry->child_post_bind += gd->reloc_off; | |
484fdf5b MS |
80 | if (entry->child_pre_probe) |
81 | entry->child_pre_probe += gd->reloc_off; | |
82 | if (entry->child_post_remove) | |
83 | entry->child_post_remove += gd->reloc_off; | |
84 | /* OPS are fixed in every uclass post_probe function */ | |
85 | if (entry->ops) | |
86 | entry->ops += gd->reloc_off; | |
87 | } | |
88 | } | |
89 | ||
90 | void fix_uclass(void) | |
91 | { | |
92 | struct uclass_driver *uclass = | |
9c503137 SG |
93 | ll_entry_start(struct uclass_driver, uclass_driver); |
94 | const int n_ents = ll_entry_count(struct uclass_driver, uclass_driver); | |
484fdf5b MS |
95 | struct uclass_driver *entry; |
96 | ||
97 | for (entry = uclass; entry != uclass + n_ents; entry++) { | |
98 | if (entry->post_bind) | |
99 | entry->post_bind += gd->reloc_off; | |
100 | if (entry->pre_unbind) | |
101 | entry->pre_unbind += gd->reloc_off; | |
31e1029a MS |
102 | if (entry->pre_probe) |
103 | entry->pre_probe += gd->reloc_off; | |
484fdf5b MS |
104 | if (entry->post_probe) |
105 | entry->post_probe += gd->reloc_off; | |
106 | if (entry->pre_remove) | |
107 | entry->pre_remove += gd->reloc_off; | |
31e1029a MS |
108 | if (entry->child_post_bind) |
109 | entry->child_post_bind += gd->reloc_off; | |
110 | if (entry->child_pre_probe) | |
111 | entry->child_pre_probe += gd->reloc_off; | |
484fdf5b MS |
112 | if (entry->init) |
113 | entry->init += gd->reloc_off; | |
114 | if (entry->destroy) | |
115 | entry->destroy += gd->reloc_off; | |
484fdf5b MS |
116 | } |
117 | } | |
5aeedebc AD |
118 | |
119 | void fix_devices(void) | |
120 | { | |
121 | struct driver_info *dev = | |
122 | ll_entry_start(struct driver_info, driver_info); | |
123 | const int n_ents = ll_entry_count(struct driver_info, driver_info); | |
124 | struct driver_info *entry; | |
125 | ||
126 | for (entry = dev; entry != dev + n_ents; entry++) { | |
caa4daa2 SG |
127 | if (entry->plat) |
128 | entry->plat += gd->reloc_off; | |
5aeedebc AD |
129 | } |
130 | } | |
131 | ||
967a7d48 SG |
132 | static int dm_setup_inst(void) |
133 | { | |
134 | DM_ROOT_NON_CONST = DM_DEVICE_GET(root); | |
135 | ||
ab933d80 SG |
136 | if (CONFIG_IS_ENABLED(OF_PLATDATA_RT)) { |
137 | struct udevice_rt *urt; | |
baf03718 | 138 | void *base; |
ab933d80 | 139 | int n_ents; |
baf03718 | 140 | uint size; |
ab933d80 SG |
141 | |
142 | /* Allocate the udevice_rt table */ | |
143 | n_ents = ll_entry_count(struct udevice, udevice); | |
144 | urt = calloc(n_ents, sizeof(struct udevice_rt)); | |
145 | if (!urt) | |
146 | return log_msg_ret("urt", -ENOMEM); | |
147 | gd_set_dm_udevice_rt(urt); | |
baf03718 SG |
148 | |
149 | /* Now allocate space for the priv/plat data, and copy it in */ | |
150 | size = __priv_data_end - __priv_data_start; | |
151 | ||
152 | base = calloc(1, size); | |
153 | if (!base) | |
154 | return log_msg_ret("priv", -ENOMEM); | |
155 | memcpy(base, __priv_data_start, size); | |
156 | gd_set_dm_priv_base(base); | |
ab933d80 SG |
157 | } |
158 | ||
967a7d48 SG |
159 | return 0; |
160 | } | |
161 | ||
19c8205e | 162 | int dm_init(bool of_live) |
6494d708 SG |
163 | { |
164 | int ret; | |
165 | ||
166 | if (gd->dm_root) { | |
167 | dm_warn("Virtual root driver already exists!\n"); | |
168 | return -EINVAL; | |
169 | } | |
3fa9f553 SG |
170 | if (CONFIG_IS_ENABLED(OF_PLATDATA_INST)) { |
171 | gd->uclass_root = &uclass_head; | |
172 | } else { | |
173 | gd->uclass_root = &DM_UCLASS_ROOT_S_NON_CONST; | |
174 | INIT_LIST_HEAD(DM_UCLASS_ROOT_NON_CONST); | |
175 | } | |
6494d708 | 176 | |
9e948b9b SG |
177 | if (IS_ENABLED(CONFIG_NEEDS_MANUAL_RELOC)) { |
178 | fix_drivers(); | |
179 | fix_uclass(); | |
180 | fix_devices(); | |
181 | } | |
484fdf5b | 182 | |
967a7d48 SG |
183 | if (CONFIG_IS_ENABLED(OF_PLATDATA_INST)) { |
184 | ret = dm_setup_inst(); | |
185 | if (ret) { | |
186 | log_debug("dm_setup_inst() failed: %d\n", ret); | |
187 | return ret; | |
188 | } | |
189 | } else { | |
190 | ret = device_bind_by_name(NULL, false, &root_info, | |
191 | &DM_ROOT_NON_CONST); | |
192 | if (ret) | |
193 | return ret; | |
194 | if (CONFIG_IS_ENABLED(OF_CONTROL)) | |
195 | dev_set_ofnode(DM_ROOT_NON_CONST, ofnode_root()); | |
196 | ret = device_probe(DM_ROOT_NON_CONST); | |
197 | if (ret) | |
198 | return ret; | |
199 | } | |
6494d708 SG |
200 | |
201 | return 0; | |
202 | } | |
203 | ||
9adbd7a1 SG |
204 | int dm_uninit(void) |
205 | { | |
cc6f4c8f MV |
206 | /* Remove non-vital devices first */ |
207 | device_remove(dm_root(), DM_REMOVE_NON_VITAL); | |
706865af | 208 | device_remove(dm_root(), DM_REMOVE_NORMAL); |
9adbd7a1 | 209 | device_unbind(dm_root()); |
c483f4ce | 210 | gd->dm_root = NULL; |
9adbd7a1 SG |
211 | |
212 | return 0; | |
213 | } | |
214 | ||
bc85aa40 SR |
215 | #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE) |
216 | int dm_remove_devices_flags(uint flags) | |
217 | { | |
218 | device_remove(dm_root(), flags); | |
219 | ||
220 | return 0; | |
221 | } | |
222 | #endif | |
223 | ||
8a8d24bd | 224 | int dm_scan_plat(bool pre_reloc_only) |
6494d708 SG |
225 | { |
226 | int ret; | |
227 | ||
ab933d80 | 228 | if (CONFIG_IS_ENABLED(OF_PLATDATA_DRIVER_RT)) { |
a294ead8 SG |
229 | struct driver_rt *dyn; |
230 | int n_ents; | |
231 | ||
232 | n_ents = ll_entry_count(struct driver_info, driver_info); | |
233 | dyn = calloc(n_ents, sizeof(struct driver_rt)); | |
234 | if (!dyn) | |
235 | return -ENOMEM; | |
236 | gd_set_dm_driver_rt(dyn); | |
237 | } | |
238 | ||
00606d7e | 239 | ret = lists_bind_drivers(DM_ROOT_NON_CONST, pre_reloc_only); |
6494d708 SG |
240 | if (ret == -ENOENT) { |
241 | dm_warn("Some drivers were not found\n"); | |
242 | ret = 0; | |
243 | } | |
6494d708 | 244 | |
cbf86d71 | 245 | return ret; |
6494d708 SG |
246 | } |
247 | ||
a652d9c7 | 248 | #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA) |
a771a04f SG |
249 | /** |
250 | * dm_scan_fdt_node() - Scan the device tree and bind drivers for a node | |
251 | * | |
252 | * This scans the subnodes of a device tree node and and creates a driver | |
253 | * for each one. | |
254 | * | |
255 | * @parent: Parent device for the devices that will be created | |
2ebea5ea | 256 | * @node: Node to scan |
a771a04f SG |
257 | * @pre_reloc_only: If true, bind only drivers with the DM_FLAG_PRE_RELOC |
258 | * flag. If false bind all drivers. | |
259 | * @return 0 if OK, -ve on error | |
260 | */ | |
2ebea5ea SG |
261 | static int dm_scan_fdt_node(struct udevice *parent, ofnode parent_node, |
262 | bool pre_reloc_only) | |
6494d708 | 263 | { |
6784cb35 | 264 | int ret = 0, err = 0; |
2ebea5ea SG |
265 | ofnode node; |
266 | ||
267 | if (!ofnode_valid(parent_node)) | |
268 | return 0; | |
1ca7e206 | 269 | |
2ebea5ea SG |
270 | for (node = ofnode_first_subnode(parent_node); |
271 | ofnode_valid(node); | |
272 | node = ofnode_next_subnode(node)) { | |
273 | const char *node_name = ofnode_get_name(node); | |
747558d0 | 274 | |
2ebea5ea | 275 | if (!ofnode_is_enabled(node)) { |
ceb91909 | 276 | pr_debug(" - ignoring disabled device\n"); |
94f7afdf SG |
277 | continue; |
278 | } | |
2ebea5ea | 279 | err = lists_bind_fdt(parent, node, NULL, pre_reloc_only); |
bc7b2f43 | 280 | if (err && !ret) { |
1ca7e206 | 281 | ret = err; |
747558d0 | 282 | debug("%s: ret=%d\n", node_name, ret); |
bc7b2f43 | 283 | } |
1ca7e206 | 284 | } |
6494d708 SG |
285 | |
286 | if (ret) | |
287 | dm_warn("Some drivers failed to bind\n"); | |
288 | ||
289 | return ret; | |
290 | } | |
1ca7e206 | 291 | |
cc7f66f7 SG |
292 | int dm_scan_fdt_dev(struct udevice *dev) |
293 | { | |
2ebea5ea | 294 | return dm_scan_fdt_node(dev, dev_ofnode(dev), |
cc7f66f7 SG |
295 | gd->flags & GD_FLG_RELOC ? false : true); |
296 | } | |
297 | ||
725e4fce | 298 | int dm_scan_fdt(bool pre_reloc_only) |
1ca7e206 | 299 | { |
2ebea5ea | 300 | return dm_scan_fdt_node(gd->dm_root, ofnode_root(), pre_reloc_only); |
1ca7e206 | 301 | } |
6494d708 | 302 | |
725e4fce | 303 | static int dm_scan_fdt_ofnode_path(const char *path, bool pre_reloc_only) |
68d215d9 RV |
304 | { |
305 | ofnode node; | |
306 | ||
307 | node = ofnode_path(path); | |
a652d9c7 | 308 | |
2ebea5ea | 309 | return dm_scan_fdt_node(gd->dm_root, node, pre_reloc_only); |
68d215d9 RV |
310 | } |
311 | ||
8ee05b5f | 312 | int dm_extended_scan(bool pre_reloc_only) |
e81c9864 | 313 | { |
0544ecbf PD |
314 | int ret, i; |
315 | const char * const nodes[] = { | |
316 | "/chosen", | |
317 | "/clocks", | |
318 | "/firmware" | |
319 | }; | |
e81c9864 | 320 | |
725e4fce | 321 | ret = dm_scan_fdt(pre_reloc_only); |
e81c9864 PC |
322 | if (ret) { |
323 | debug("dm_scan_fdt() failed: %d\n", ret); | |
324 | return ret; | |
325 | } | |
326 | ||
0544ecbf PD |
327 | /* Some nodes aren't devices themselves but may contain some */ |
328 | for (i = 0; i < ARRAY_SIZE(nodes); i++) { | |
725e4fce | 329 | ret = dm_scan_fdt_ofnode_path(nodes[i], pre_reloc_only); |
0544ecbf PD |
330 | if (ret) { |
331 | debug("dm_scan_fdt() scan for %s failed: %d\n", | |
332 | nodes[i], ret); | |
333 | return ret; | |
334 | } | |
1712ca21 RV |
335 | } |
336 | ||
e81c9864 PC |
337 | return ret; |
338 | } | |
d7677bfc | 339 | #endif |
e81c9864 | 340 | |
bb58503d SG |
341 | __weak int dm_scan_other(bool pre_reloc_only) |
342 | { | |
343 | return 0; | |
344 | } | |
345 | ||
cfb9c9b7 SG |
346 | #if CONFIG_IS_ENABLED(OF_PLATDATA_INST) && CONFIG_IS_ENABLED(READ_ONLY) |
347 | void *dm_priv_to_rw(void *priv) | |
348 | { | |
349 | long offset = priv - (void *)__priv_data_start; | |
350 | ||
351 | return gd_dm_priv_base() + offset; | |
352 | } | |
353 | #endif | |
354 | ||
49bbe6ea SG |
355 | /** |
356 | * dm_scan() - Scan tables to bind devices | |
357 | * | |
358 | * Runs through the driver_info tables and binds the devices it finds. Then runs | |
359 | * through the devicetree nodes. Finally calls dm_scan_other() to add any | |
360 | * special devices | |
361 | * | |
362 | * @pre_reloc_only: If true, bind only nodes with special devicetree properties, | |
363 | * or drivers with the DM_FLAG_PRE_RELOC flag. If false bind all drivers. | |
364 | */ | |
365 | static int dm_scan(bool pre_reloc_only) | |
ab7cd627 SG |
366 | { |
367 | int ret; | |
368 | ||
8a8d24bd | 369 | ret = dm_scan_plat(pre_reloc_only); |
ab7cd627 | 370 | if (ret) { |
8a8d24bd | 371 | debug("dm_scan_plat() failed: %d\n", ret); |
49bbe6ea | 372 | return ret; |
ab7cd627 | 373 | } |
b2b0d3e7 | 374 | |
29629eb8 | 375 | if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) { |
8ee05b5f | 376 | ret = dm_extended_scan(pre_reloc_only); |
b2b0d3e7 | 377 | if (ret) { |
8ee05b5f | 378 | debug("dm_extended_scan() failed: %d\n", ret); |
49bbe6ea | 379 | return ret; |
b2b0d3e7 | 380 | } |
ab7cd627 | 381 | } |
b2b0d3e7 | 382 | |
bb58503d SG |
383 | ret = dm_scan_other(pre_reloc_only); |
384 | if (ret) | |
49bbe6ea SG |
385 | return ret; |
386 | ||
387 | return 0; | |
388 | } | |
389 | ||
390 | int dm_init_and_scan(bool pre_reloc_only) | |
391 | { | |
392 | int ret; | |
393 | ||
49bbe6ea SG |
394 | ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE)); |
395 | if (ret) { | |
396 | debug("dm_init() failed: %d\n", ret); | |
397 | return ret; | |
398 | } | |
967a7d48 SG |
399 | if (!CONFIG_IS_ENABLED(OF_PLATDATA_INST)) { |
400 | ret = dm_scan(pre_reloc_only); | |
401 | if (ret) { | |
402 | log_debug("dm_scan() failed: %d\n", ret); | |
403 | return ret; | |
404 | } | |
49bbe6ea | 405 | } |
ab7cd627 SG |
406 | |
407 | return 0; | |
408 | } | |
409 | ||
4b724a13 SG |
410 | #ifdef CONFIG_ACPIGEN |
411 | static int root_acpi_get_name(const struct udevice *dev, char *out_name) | |
412 | { | |
413 | return acpi_copy_name(out_name, "\\_SB"); | |
414 | } | |
415 | ||
416 | struct acpi_ops root_acpi_ops = { | |
417 | .get_name = root_acpi_get_name, | |
418 | }; | |
419 | #endif | |
420 | ||
6494d708 SG |
421 | /* This is the root driver - all drivers are children of this */ |
422 | U_BOOT_DRIVER(root_driver) = { | |
423 | .name = "root_driver", | |
424 | .id = UCLASS_ROOT, | |
4b724a13 | 425 | ACPI_OPS_PTR(&root_acpi_ops) |
6494d708 SG |
426 | }; |
427 | ||
428 | /* This is the root uclass */ | |
429 | UCLASS_DRIVER(root) = { | |
430 | .name = "root", | |
431 | .id = UCLASS_ROOT, | |
432 | }; |