blob: 82b3c7de715edd509fff9738939883f6235610d1 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassdd6ab882014-02-26 15:59:18 -07002/*
3 * Copyright (c) 2013 Google, Inc
4 *
5 * (C) Copyright 2012
6 * Pavel Herrmann <morpheus.ibis@gmail.com>
Simon Glassdd6ab882014-02-26 15:59:18 -07007 */
8
9#include <common.h>
10#include <errno.h>
Simon Glass528b1ef2015-01-25 08:27:16 -070011#include <fdtdec.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070013#include <malloc.h>
Simon Glass8dee67a2021-03-15 17:25:38 +130014#include <asm-generic/sections.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060015#include <asm/global_data.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090016#include <linux/libfdt.h>
Simon Glass719ea082020-07-07 13:11:38 -060017#include <dm/acpi.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070018#include <dm/device.h>
19#include <dm/device-internal.h>
20#include <dm/lists.h>
Simon Glassf19d9f22017-05-18 20:09:08 -060021#include <dm/of.h>
22#include <dm/of_access.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070023#include <dm/platdata.h>
Simon Glassf19d9f22017-05-18 20:09:08 -060024#include <dm/read.h>
Jeroen Hofsteecf0cf822014-06-25 21:57:45 +020025#include <dm/root.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070026#include <dm/uclass.h>
27#include <dm/util.h>
28#include <linux/list.h>
29
30DECLARE_GLOBAL_DATA_PTR;
31
Walter Lozano95282d62020-06-25 01:10:10 -030032static struct driver_info root_info = {
Simon Glassdd6ab882014-02-26 15:59:18 -070033 .name = "root_driver",
34};
35
Heiko Schocherb74fcb42014-05-22 12:43:05 +020036struct udevice *dm_root(void)
Simon Glassdd6ab882014-02-26 15:59:18 -070037{
38 if (!gd->dm_root) {
39 dm_warn("Virtual root driver does not exist!\n");
40 return NULL;
41 }
42
43 return gd->dm_root;
44}
45
Simon Glass1b0deee2016-11-13 14:21:58 -070046void dm_fixup_for_gd_move(struct global_data *new_gd)
47{
48 /* The sentinel node has moved, so update things that point to it */
Lokesh Vutla02ba17f2017-02-13 09:21:22 +053049 if (gd->dm_root) {
Simon Glass784cd9e2020-12-19 10:40:17 -070050 new_gd->uclass_root->next->prev = new_gd->uclass_root;
51 new_gd->uclass_root->prev->next = new_gd->uclass_root;
Lokesh Vutla02ba17f2017-02-13 09:21:22 +053052 }
Simon Glass1b0deee2016-11-13 14:21:58 -070053}
54
Michal Simek0ec473b2015-02-02 16:31:59 +010055void fix_drivers(void)
56{
57 struct driver *drv =
58 ll_entry_start(struct driver, driver);
59 const int n_ents = ll_entry_count(struct driver, driver);
60 struct driver *entry;
61
62 for (entry = drv; entry != drv + n_ents; entry++) {
63 if (entry->of_match)
64 entry->of_match = (const struct udevice_id *)
Simon Glass40916e62020-10-03 09:25:22 -060065 ((ulong)entry->of_match + gd->reloc_off);
Michal Simek0ec473b2015-02-02 16:31:59 +010066 if (entry->bind)
67 entry->bind += gd->reloc_off;
68 if (entry->probe)
69 entry->probe += gd->reloc_off;
70 if (entry->remove)
71 entry->remove += gd->reloc_off;
72 if (entry->unbind)
73 entry->unbind += gd->reloc_off;
Simon Glassaad29ae2020-12-03 16:55:21 -070074 if (entry->of_to_plat)
75 entry->of_to_plat += gd->reloc_off;
Michal Simek61809f52015-10-27 13:48:08 +010076 if (entry->child_post_bind)
77 entry->child_post_bind += gd->reloc_off;
Michal Simek0ec473b2015-02-02 16:31:59 +010078 if (entry->child_pre_probe)
79 entry->child_pre_probe += gd->reloc_off;
80 if (entry->child_post_remove)
81 entry->child_post_remove += gd->reloc_off;
82 /* OPS are fixed in every uclass post_probe function */
83 if (entry->ops)
84 entry->ops += gd->reloc_off;
85 }
86}
87
88void fix_uclass(void)
89{
90 struct uclass_driver *uclass =
Simon Glass69ea20e2020-12-22 19:30:23 -070091 ll_entry_start(struct uclass_driver, uclass_driver);
92 const int n_ents = ll_entry_count(struct uclass_driver, uclass_driver);
Michal Simek0ec473b2015-02-02 16:31:59 +010093 struct uclass_driver *entry;
94
95 for (entry = uclass; entry != uclass + n_ents; entry++) {
96 if (entry->post_bind)
97 entry->post_bind += gd->reloc_off;
98 if (entry->pre_unbind)
99 entry->pre_unbind += gd->reloc_off;
Michal Simek61809f52015-10-27 13:48:08 +0100100 if (entry->pre_probe)
101 entry->pre_probe += gd->reloc_off;
Michal Simek0ec473b2015-02-02 16:31:59 +0100102 if (entry->post_probe)
103 entry->post_probe += gd->reloc_off;
104 if (entry->pre_remove)
105 entry->pre_remove += gd->reloc_off;
Michal Simek61809f52015-10-27 13:48:08 +0100106 if (entry->child_post_bind)
107 entry->child_post_bind += gd->reloc_off;
108 if (entry->child_pre_probe)
109 entry->child_pre_probe += gd->reloc_off;
Michal Simek0ec473b2015-02-02 16:31:59 +0100110 if (entry->init)
111 entry->init += gd->reloc_off;
112 if (entry->destroy)
113 entry->destroy += gd->reloc_off;
114 /* FIXME maybe also need to fix these ops */
115 if (entry->ops)
116 entry->ops += gd->reloc_off;
117 }
118}
Angelo Dureghellof4d193b2016-05-21 12:05:49 +0200119
120void fix_devices(void)
121{
122 struct driver_info *dev =
123 ll_entry_start(struct driver_info, driver_info);
124 const int n_ents = ll_entry_count(struct driver_info, driver_info);
125 struct driver_info *entry;
126
127 for (entry = dev; entry != dev + n_ents; entry++) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700128 if (entry->plat)
129 entry->plat += gd->reloc_off;
Angelo Dureghellof4d193b2016-05-21 12:05:49 +0200130 }
131}
132
Simon Glass43f7ea52021-03-15 17:25:17 +1300133static int dm_setup_inst(void)
134{
135 DM_ROOT_NON_CONST = DM_DEVICE_GET(root);
136
Simon Glass8beeb282021-03-15 17:25:36 +1300137 if (CONFIG_IS_ENABLED(OF_PLATDATA_RT)) {
138 struct udevice_rt *urt;
Simon Glass8dee67a2021-03-15 17:25:38 +1300139 void *base;
Simon Glass8beeb282021-03-15 17:25:36 +1300140 int n_ents;
Simon Glass8dee67a2021-03-15 17:25:38 +1300141 uint size;
Simon Glass8beeb282021-03-15 17:25:36 +1300142
143 /* Allocate the udevice_rt table */
144 n_ents = ll_entry_count(struct udevice, udevice);
145 urt = calloc(n_ents, sizeof(struct udevice_rt));
146 if (!urt)
147 return log_msg_ret("urt", -ENOMEM);
148 gd_set_dm_udevice_rt(urt);
Simon Glass8dee67a2021-03-15 17:25:38 +1300149
150 /* Now allocate space for the priv/plat data, and copy it in */
151 size = __priv_data_end - __priv_data_start;
152
153 base = calloc(1, size);
154 if (!base)
155 return log_msg_ret("priv", -ENOMEM);
156 memcpy(base, __priv_data_start, size);
157 gd_set_dm_priv_base(base);
Simon Glass8beeb282021-03-15 17:25:36 +1300158 }
159
Simon Glass43f7ea52021-03-15 17:25:17 +1300160 return 0;
161}
162
Simon Glassf19d9f22017-05-18 20:09:08 -0600163int dm_init(bool of_live)
Simon Glassdd6ab882014-02-26 15:59:18 -0700164{
165 int ret;
166
Dario Binacchib574d682020-12-30 00:16:21 +0100167 if (IS_ENABLED(CONFIG_OF_TRANSLATE_ZERO_SIZE_CELLS))
168 gd->dm_flags |= GD_DM_FLG_SIZE_CELLS_0;
169
Simon Glassdd6ab882014-02-26 15:59:18 -0700170 if (gd->dm_root) {
171 dm_warn("Virtual root driver already exists!\n");
172 return -EINVAL;
173 }
Simon Glass6f156532021-03-15 17:25:16 +1300174 if (CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
175 gd->uclass_root = &uclass_head;
176 } else {
177 gd->uclass_root = &DM_UCLASS_ROOT_S_NON_CONST;
178 INIT_LIST_HEAD(DM_UCLASS_ROOT_NON_CONST);
179 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700180
Simon Glasse2e89072020-10-03 11:31:37 -0600181 if (IS_ENABLED(CONFIG_NEEDS_MANUAL_RELOC)) {
182 fix_drivers();
183 fix_uclass();
184 fix_devices();
185 }
Michal Simek0ec473b2015-02-02 16:31:59 +0100186
Simon Glass43f7ea52021-03-15 17:25:17 +1300187 if (CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
188 ret = dm_setup_inst();
189 if (ret) {
190 log_debug("dm_setup_inst() failed: %d\n", ret);
191 return ret;
192 }
193 } else {
194 ret = device_bind_by_name(NULL, false, &root_info,
195 &DM_ROOT_NON_CONST);
196 if (ret)
197 return ret;
198 if (CONFIG_IS_ENABLED(OF_CONTROL))
199 dev_set_ofnode(DM_ROOT_NON_CONST, ofnode_root());
200 ret = device_probe(DM_ROOT_NON_CONST);
201 if (ret)
202 return ret;
203 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700204
205 return 0;
206}
207
Simon Glass00197582014-07-23 06:55:01 -0600208int dm_uninit(void)
209{
Marek Vasutabbdbbd2021-01-24 14:32:46 -0700210 /* Remove non-vital devices first */
211 device_remove(dm_root(), DM_REMOVE_NON_VITAL);
Stefan Roese80b5bc92017-03-20 12:51:48 +0100212 device_remove(dm_root(), DM_REMOVE_NORMAL);
Simon Glass00197582014-07-23 06:55:01 -0600213 device_unbind(dm_root());
Jean-Jacques Hiblot272b67d2018-12-07 14:50:54 +0100214 gd->dm_root = NULL;
Simon Glass00197582014-07-23 06:55:01 -0600215
216 return 0;
217}
218
Stefan Roese505045d2017-03-27 10:58:53 +0200219#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
220int dm_remove_devices_flags(uint flags)
221{
222 device_remove(dm_root(), flags);
223
224 return 0;
225}
226#endif
227
Simon Glassb75b15b2020-12-03 16:55:23 -0700228int dm_scan_plat(bool pre_reloc_only)
Simon Glassdd6ab882014-02-26 15:59:18 -0700229{
230 int ret;
231
Simon Glass8beeb282021-03-15 17:25:36 +1300232 if (CONFIG_IS_ENABLED(OF_PLATDATA_DRIVER_RT)) {
Simon Glasscfd6a002020-10-03 11:31:33 -0600233 struct driver_rt *dyn;
234 int n_ents;
235
236 n_ents = ll_entry_count(struct driver_info, driver_info);
237 dyn = calloc(n_ents, sizeof(struct driver_rt));
238 if (!dyn)
239 return -ENOMEM;
240 gd_set_dm_driver_rt(dyn);
241 }
242
Simon Glassfef72b72014-07-23 06:55:03 -0600243 ret = lists_bind_drivers(DM_ROOT_NON_CONST, pre_reloc_only);
Simon Glassdd6ab882014-02-26 15:59:18 -0700244 if (ret == -ENOENT) {
245 dm_warn("Some drivers were not found\n");
246 ret = 0;
247 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700248
Masahiro Yamada6cac81a2014-11-17 17:19:38 +0900249 return ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700250}
251
Simon Glass40916e62020-10-03 09:25:22 -0600252#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassa4dbe252017-05-17 17:18:08 -0600253/**
254 * dm_scan_fdt_node() - Scan the device tree and bind drivers for a node
255 *
256 * This scans the subnodes of a device tree node and and creates a driver
257 * for each one.
258 *
259 * @parent: Parent device for the devices that will be created
Simon Glass21a30882020-11-28 17:50:08 -0700260 * @node: Node to scan
Simon Glassa4dbe252017-05-17 17:18:08 -0600261 * @pre_reloc_only: If true, bind only drivers with the DM_FLAG_PRE_RELOC
262 * flag. If false bind all drivers.
263 * @return 0 if OK, -ve on error
264 */
Simon Glass21a30882020-11-28 17:50:08 -0700265static int dm_scan_fdt_node(struct udevice *parent, ofnode parent_node,
266 bool pre_reloc_only)
Simon Glassdd6ab882014-02-26 15:59:18 -0700267{
Simon Glassdd6ab882014-02-26 15:59:18 -0700268 int ret = 0, err;
Simon Glass21a30882020-11-28 17:50:08 -0700269 ofnode node;
Simon Glassdd6ab882014-02-26 15:59:18 -0700270
Simon Glass21a30882020-11-28 17:50:08 -0700271 if (!ofnode_valid(parent_node))
272 return 0;
273
274 for (node = ofnode_first_subnode(parent_node);
275 ofnode_valid(node);
276 node = ofnode_next_subnode(node)) {
277 const char *node_name = ofnode_get_name(node);
Jens Wiklander651a8362018-09-25 16:40:05 +0200278
Simon Glass21a30882020-11-28 17:50:08 -0700279 if (!ofnode_is_enabled(node)) {
Masahiro Yamadaf70f39f2017-09-29 12:31:20 +0900280 pr_debug(" - ignoring disabled device\n");
Simon Glass528b1ef2015-01-25 08:27:16 -0700281 continue;
282 }
Simon Glass21a30882020-11-28 17:50:08 -0700283 err = lists_bind_fdt(parent, node, NULL, pre_reloc_only);
Simon Glassc90b9172015-08-30 16:55:17 -0600284 if (err && !ret) {
Simon Glass40717422014-07-23 06:55:18 -0600285 ret = err;
Jens Wiklander651a8362018-09-25 16:40:05 +0200286 debug("%s: ret=%d\n", node_name, ret);
Simon Glassc90b9172015-08-30 16:55:17 -0600287 }
Simon Glass40717422014-07-23 06:55:18 -0600288 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700289
290 if (ret)
291 dm_warn("Some drivers failed to bind\n");
292
293 return ret;
294}
Simon Glass40717422014-07-23 06:55:18 -0600295
Simon Glass5d5388d2016-07-05 17:10:08 -0600296int dm_scan_fdt_dev(struct udevice *dev)
297{
Simon Glass21a30882020-11-28 17:50:08 -0700298 return dm_scan_fdt_node(dev, dev_ofnode(dev),
Simon Glass5d5388d2016-07-05 17:10:08 -0600299 gd->flags & GD_FLG_RELOC ? false : true);
300}
301
Simon Glass5039cab2020-11-28 17:50:09 -0700302int dm_scan_fdt(bool pre_reloc_only)
Simon Glass40717422014-07-23 06:55:18 -0600303{
Simon Glass21a30882020-11-28 17:50:08 -0700304 return dm_scan_fdt_node(gd->dm_root, ofnode_root(), pre_reloc_only);
Simon Glass40717422014-07-23 06:55:18 -0600305}
Simon Glassdd6ab882014-02-26 15:59:18 -0700306
Simon Glass5039cab2020-11-28 17:50:09 -0700307static int dm_scan_fdt_ofnode_path(const char *path, bool pre_reloc_only)
Rajan Vajafac64632018-08-10 01:45:33 -0700308{
309 ofnode node;
310
311 node = ofnode_path(path);
Simon Glass40916e62020-10-03 09:25:22 -0600312
Simon Glass21a30882020-11-28 17:50:08 -0700313 return dm_scan_fdt_node(gd->dm_root, node, pre_reloc_only);
Rajan Vajafac64632018-08-10 01:45:33 -0700314}
315
Simon Glass4d71d602020-11-28 17:50:10 -0700316int dm_extended_scan(bool pre_reloc_only)
Patrice Chotardbb3a45b2017-09-04 14:55:56 +0200317{
Patrick Delaunayc779e202020-02-18 15:43:46 +0100318 int ret, i;
319 const char * const nodes[] = {
320 "/chosen",
321 "/clocks",
322 "/firmware"
323 };
Patrice Chotardbb3a45b2017-09-04 14:55:56 +0200324
Simon Glass5039cab2020-11-28 17:50:09 -0700325 ret = dm_scan_fdt(pre_reloc_only);
Patrice Chotardbb3a45b2017-09-04 14:55:56 +0200326 if (ret) {
327 debug("dm_scan_fdt() failed: %d\n", ret);
328 return ret;
329 }
330
Patrick Delaunayc779e202020-02-18 15:43:46 +0100331 /* Some nodes aren't devices themselves but may contain some */
332 for (i = 0; i < ARRAY_SIZE(nodes); i++) {
Simon Glass5039cab2020-11-28 17:50:09 -0700333 ret = dm_scan_fdt_ofnode_path(nodes[i], pre_reloc_only);
Patrick Delaunayc779e202020-02-18 15:43:46 +0100334 if (ret) {
335 debug("dm_scan_fdt() scan for %s failed: %d\n",
336 nodes[i], ret);
337 return ret;
338 }
Rajan Vajae9fc81d2018-08-10 01:45:34 -0700339 }
340
Patrice Chotardbb3a45b2017-09-04 14:55:56 +0200341 return ret;
342}
Marek Vasut7f18c342019-08-31 18:03:28 +0200343#endif
Patrice Chotardbb3a45b2017-09-04 14:55:56 +0200344
Simon Glass97e22e32014-07-23 06:55:23 -0600345__weak int dm_scan_other(bool pre_reloc_only)
346{
347 return 0;
348}
349
Simon Glassc77695b2020-12-19 10:40:16 -0700350/**
351 * dm_scan() - Scan tables to bind devices
352 *
353 * Runs through the driver_info tables and binds the devices it finds. Then runs
354 * through the devicetree nodes. Finally calls dm_scan_other() to add any
355 * special devices
356 *
357 * @pre_reloc_only: If true, bind only nodes with special devicetree properties,
358 * or drivers with the DM_FLAG_PRE_RELOC flag. If false bind all drivers.
359 */
360static int dm_scan(bool pre_reloc_only)
Simon Glassa730c5d2014-07-23 06:55:04 -0600361{
362 int ret;
363
Simon Glassb75b15b2020-12-03 16:55:23 -0700364 ret = dm_scan_plat(pre_reloc_only);
Simon Glassa730c5d2014-07-23 06:55:04 -0600365 if (ret) {
Simon Glassb75b15b2020-12-03 16:55:23 -0700366 debug("dm_scan_plat() failed: %d\n", ret);
Simon Glassc77695b2020-12-19 10:40:16 -0700367 return ret;
Simon Glassa730c5d2014-07-23 06:55:04 -0600368 }
Simon Glass0704b852015-02-27 22:06:41 -0700369
Simon Glass8d7e8162016-07-04 11:57:58 -0600370 if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
Simon Glass4d71d602020-11-28 17:50:10 -0700371 ret = dm_extended_scan(pre_reloc_only);
Simon Glass0704b852015-02-27 22:06:41 -0700372 if (ret) {
Simon Glass4d71d602020-11-28 17:50:10 -0700373 debug("dm_extended_scan() failed: %d\n", ret);
Simon Glassc77695b2020-12-19 10:40:16 -0700374 return ret;
Simon Glass0704b852015-02-27 22:06:41 -0700375 }
Simon Glassa730c5d2014-07-23 06:55:04 -0600376 }
Simon Glass0704b852015-02-27 22:06:41 -0700377
Simon Glass97e22e32014-07-23 06:55:23 -0600378 ret = dm_scan_other(pre_reloc_only);
379 if (ret)
Simon Glassc77695b2020-12-19 10:40:16 -0700380 return ret;
381
382 return 0;
383}
384
385int dm_init_and_scan(bool pre_reloc_only)
386{
387 int ret;
388
Simon Glassc77695b2020-12-19 10:40:16 -0700389 ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
390 if (ret) {
391 debug("dm_init() failed: %d\n", ret);
392 return ret;
393 }
Simon Glass43f7ea52021-03-15 17:25:17 +1300394 if (!CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
395 ret = dm_scan(pre_reloc_only);
396 if (ret) {
397 log_debug("dm_scan() failed: %d\n", ret);
398 return ret;
399 }
Simon Glassc77695b2020-12-19 10:40:16 -0700400 }
Simon Glassa730c5d2014-07-23 06:55:04 -0600401
402 return 0;
403}
404
Simon Glass719ea082020-07-07 13:11:38 -0600405#ifdef CONFIG_ACPIGEN
406static int root_acpi_get_name(const struct udevice *dev, char *out_name)
407{
408 return acpi_copy_name(out_name, "\\_SB");
409}
410
411struct acpi_ops root_acpi_ops = {
412 .get_name = root_acpi_get_name,
413};
414#endif
415
Simon Glassdd6ab882014-02-26 15:59:18 -0700416/* This is the root driver - all drivers are children of this */
417U_BOOT_DRIVER(root_driver) = {
418 .name = "root_driver",
419 .id = UCLASS_ROOT,
Simon Glass719ea082020-07-07 13:11:38 -0600420 ACPI_OPS_PTR(&root_acpi_ops)
Simon Glassdd6ab882014-02-26 15:59:18 -0700421};
422
423/* This is the root uclass */
424UCLASS_DRIVER(root) = {
425 .name = "root",
426 .id = UCLASS_ROOT,
427};