blob: 78eee082c95433243466d1bc776b22dbfce6a8cc [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
Patrick Delaunay81313352021-04-27 11:02:19 +02009#define LOG_CATEGORY UCLASS_ROOT
10
Simon Glassdd6ab882014-02-26 15:59:18 -070011#include <common.h>
12#include <errno.h>
Simon Glass528b1ef2015-01-25 08:27:16 -070013#include <fdtdec.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070015#include <malloc.h>
Simon Glass8dee67a2021-03-15 17:25:38 +130016#include <asm-generic/sections.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060017#include <asm/global_data.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090018#include <linux/libfdt.h>
Simon Glass719ea082020-07-07 13:11:38 -060019#include <dm/acpi.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070020#include <dm/device.h>
21#include <dm/device-internal.h>
22#include <dm/lists.h>
Simon Glassf19d9f22017-05-18 20:09:08 -060023#include <dm/of.h>
24#include <dm/of_access.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070025#include <dm/platdata.h>
Simon Glassf19d9f22017-05-18 20:09:08 -060026#include <dm/read.h>
Jeroen Hofsteecf0cf822014-06-25 21:57:45 +020027#include <dm/root.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070028#include <dm/uclass.h>
29#include <dm/util.h>
30#include <linux/list.h>
31
32DECLARE_GLOBAL_DATA_PTR;
33
Walter Lozano95282d62020-06-25 01:10:10 -030034static struct driver_info root_info = {
Simon Glassdd6ab882014-02-26 15:59:18 -070035 .name = "root_driver",
36};
37
Heiko Schocherb74fcb42014-05-22 12:43:05 +020038struct udevice *dm_root(void)
Simon Glassdd6ab882014-02-26 15:59:18 -070039{
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
Simon Glass1b0deee2016-11-13 14:21:58 -070048void dm_fixup_for_gd_move(struct global_data *new_gd)
49{
50 /* The sentinel node has moved, so update things that point to it */
Lokesh Vutla02ba17f2017-02-13 09:21:22 +053051 if (gd->dm_root) {
Simon Glass784cd9e2020-12-19 10:40:17 -070052 new_gd->uclass_root->next->prev = new_gd->uclass_root;
53 new_gd->uclass_root->prev->next = new_gd->uclass_root;
Lokesh Vutla02ba17f2017-02-13 09:21:22 +053054 }
Simon Glass1b0deee2016-11-13 14:21:58 -070055}
56
Michal Simek0ec473b2015-02-02 16:31:59 +010057void 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 *)
Simon Glass40916e62020-10-03 09:25:22 -060067 ((ulong)entry->of_match + gd->reloc_off);
Michal Simek0ec473b2015-02-02 16:31:59 +010068 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;
Simon Glassaad29ae2020-12-03 16:55:21 -070076 if (entry->of_to_plat)
77 entry->of_to_plat += gd->reloc_off;
Michal Simek61809f52015-10-27 13:48:08 +010078 if (entry->child_post_bind)
79 entry->child_post_bind += gd->reloc_off;
Michal Simek0ec473b2015-02-02 16:31:59 +010080 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
90void fix_uclass(void)
91{
92 struct uclass_driver *uclass =
Simon Glass69ea20e2020-12-22 19:30:23 -070093 ll_entry_start(struct uclass_driver, uclass_driver);
94 const int n_ents = ll_entry_count(struct uclass_driver, uclass_driver);
Michal Simek0ec473b2015-02-02 16:31:59 +010095 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;
Michal Simek61809f52015-10-27 13:48:08 +0100102 if (entry->pre_probe)
103 entry->pre_probe += gd->reloc_off;
Michal Simek0ec473b2015-02-02 16:31:59 +0100104 if (entry->post_probe)
105 entry->post_probe += gd->reloc_off;
106 if (entry->pre_remove)
107 entry->pre_remove += gd->reloc_off;
Michal Simek61809f52015-10-27 13:48:08 +0100108 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;
Michal Simek0ec473b2015-02-02 16:31:59 +0100112 if (entry->init)
113 entry->init += gd->reloc_off;
114 if (entry->destroy)
115 entry->destroy += gd->reloc_off;
Michal Simek0ec473b2015-02-02 16:31:59 +0100116 }
117}
Angelo Dureghellof4d193b2016-05-21 12:05:49 +0200118
119void 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++) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700127 if (entry->plat)
128 entry->plat += gd->reloc_off;
Angelo Dureghellof4d193b2016-05-21 12:05:49 +0200129 }
130}
131
Simon Glass43f7ea52021-03-15 17:25:17 +1300132static int dm_setup_inst(void)
133{
134 DM_ROOT_NON_CONST = DM_DEVICE_GET(root);
135
Simon Glass8beeb282021-03-15 17:25:36 +1300136 if (CONFIG_IS_ENABLED(OF_PLATDATA_RT)) {
137 struct udevice_rt *urt;
Simon Glass8dee67a2021-03-15 17:25:38 +1300138 void *base;
Simon Glass8beeb282021-03-15 17:25:36 +1300139 int n_ents;
Simon Glass8dee67a2021-03-15 17:25:38 +1300140 uint size;
Simon Glass8beeb282021-03-15 17:25:36 +1300141
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);
Simon Glass8dee67a2021-03-15 17:25:38 +1300148
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);
Simon Glass8beeb282021-03-15 17:25:36 +1300157 }
158
Simon Glass43f7ea52021-03-15 17:25:17 +1300159 return 0;
160}
161
Simon Glassf19d9f22017-05-18 20:09:08 -0600162int dm_init(bool of_live)
Simon Glassdd6ab882014-02-26 15:59:18 -0700163{
164 int ret;
165
166 if (gd->dm_root) {
167 dm_warn("Virtual root driver already exists!\n");
168 return -EINVAL;
169 }
Simon Glass6f156532021-03-15 17:25:16 +1300170 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 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700176
Simon Glasse2e89072020-10-03 11:31:37 -0600177 if (IS_ENABLED(CONFIG_NEEDS_MANUAL_RELOC)) {
178 fix_drivers();
179 fix_uclass();
180 fix_devices();
181 }
Michal Simek0ec473b2015-02-02 16:31:59 +0100182
Simon Glass43f7ea52021-03-15 17:25:17 +1300183 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 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700200
201 return 0;
202}
203
Simon Glass00197582014-07-23 06:55:01 -0600204int dm_uninit(void)
205{
Marek Vasutabbdbbd2021-01-24 14:32:46 -0700206 /* Remove non-vital devices first */
207 device_remove(dm_root(), DM_REMOVE_NON_VITAL);
Stefan Roese80b5bc92017-03-20 12:51:48 +0100208 device_remove(dm_root(), DM_REMOVE_NORMAL);
Simon Glass00197582014-07-23 06:55:01 -0600209 device_unbind(dm_root());
Jean-Jacques Hiblot272b67d2018-12-07 14:50:54 +0100210 gd->dm_root = NULL;
Simon Glass00197582014-07-23 06:55:01 -0600211
212 return 0;
213}
214
Stefan Roese505045d2017-03-27 10:58:53 +0200215#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
216int dm_remove_devices_flags(uint flags)
217{
218 device_remove(dm_root(), flags);
219
220 return 0;
221}
222#endif
223
Simon Glassb75b15b2020-12-03 16:55:23 -0700224int dm_scan_plat(bool pre_reloc_only)
Simon Glassdd6ab882014-02-26 15:59:18 -0700225{
226 int ret;
227
Simon Glass8beeb282021-03-15 17:25:36 +1300228 if (CONFIG_IS_ENABLED(OF_PLATDATA_DRIVER_RT)) {
Simon Glasscfd6a002020-10-03 11:31:33 -0600229 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
Simon Glassfef72b72014-07-23 06:55:03 -0600239 ret = lists_bind_drivers(DM_ROOT_NON_CONST, pre_reloc_only);
Simon Glassdd6ab882014-02-26 15:59:18 -0700240 if (ret == -ENOENT) {
241 dm_warn("Some drivers were not found\n");
242 ret = 0;
243 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700244
Masahiro Yamada6cac81a2014-11-17 17:19:38 +0900245 return ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700246}
247
Simon Glass40916e62020-10-03 09:25:22 -0600248#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassa4dbe252017-05-17 17:18:08 -0600249/**
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
Simon Glass21a30882020-11-28 17:50:08 -0700256 * @node: Node to scan
Simon Glassa4dbe252017-05-17 17:18:08 -0600257 * @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 */
Simon Glass21a30882020-11-28 17:50:08 -0700261static int dm_scan_fdt_node(struct udevice *parent, ofnode parent_node,
262 bool pre_reloc_only)
Simon Glassdd6ab882014-02-26 15:59:18 -0700263{
Sean Anderson0449f062021-04-08 17:15:00 -0400264 int ret = 0, err = 0;
Simon Glass21a30882020-11-28 17:50:08 -0700265 ofnode node;
Simon Glassdd6ab882014-02-26 15:59:18 -0700266
Simon Glass21a30882020-11-28 17:50:08 -0700267 if (!ofnode_valid(parent_node))
268 return 0;
269
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);
Jens Wiklander651a8362018-09-25 16:40:05 +0200274
Simon Glass21a30882020-11-28 17:50:08 -0700275 if (!ofnode_is_enabled(node)) {
Masahiro Yamadaf70f39f2017-09-29 12:31:20 +0900276 pr_debug(" - ignoring disabled device\n");
Simon Glass528b1ef2015-01-25 08:27:16 -0700277 continue;
278 }
Simon Glass21a30882020-11-28 17:50:08 -0700279 err = lists_bind_fdt(parent, node, NULL, pre_reloc_only);
Simon Glassc90b9172015-08-30 16:55:17 -0600280 if (err && !ret) {
Simon Glass40717422014-07-23 06:55:18 -0600281 ret = err;
Jens Wiklander651a8362018-09-25 16:40:05 +0200282 debug("%s: ret=%d\n", node_name, ret);
Simon Glassc90b9172015-08-30 16:55:17 -0600283 }
Simon Glass40717422014-07-23 06:55:18 -0600284 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700285
286 if (ret)
287 dm_warn("Some drivers failed to bind\n");
288
289 return ret;
290}
Simon Glass40717422014-07-23 06:55:18 -0600291
Simon Glass5d5388d2016-07-05 17:10:08 -0600292int dm_scan_fdt_dev(struct udevice *dev)
293{
Simon Glass21a30882020-11-28 17:50:08 -0700294 return dm_scan_fdt_node(dev, dev_ofnode(dev),
Simon Glass5d5388d2016-07-05 17:10:08 -0600295 gd->flags & GD_FLG_RELOC ? false : true);
296}
297
Simon Glass5039cab2020-11-28 17:50:09 -0700298int dm_scan_fdt(bool pre_reloc_only)
Simon Glass40717422014-07-23 06:55:18 -0600299{
Simon Glass21a30882020-11-28 17:50:08 -0700300 return dm_scan_fdt_node(gd->dm_root, ofnode_root(), pre_reloc_only);
Simon Glass40717422014-07-23 06:55:18 -0600301}
Simon Glassdd6ab882014-02-26 15:59:18 -0700302
Simon Glass5039cab2020-11-28 17:50:09 -0700303static int dm_scan_fdt_ofnode_path(const char *path, bool pre_reloc_only)
Rajan Vajafac64632018-08-10 01:45:33 -0700304{
305 ofnode node;
306
307 node = ofnode_path(path);
Simon Glass40916e62020-10-03 09:25:22 -0600308
Simon Glass21a30882020-11-28 17:50:08 -0700309 return dm_scan_fdt_node(gd->dm_root, node, pre_reloc_only);
Rajan Vajafac64632018-08-10 01:45:33 -0700310}
311
Simon Glass4d71d602020-11-28 17:50:10 -0700312int dm_extended_scan(bool pre_reloc_only)
Patrice Chotardbb3a45b2017-09-04 14:55:56 +0200313{
Patrick Delaunayc779e202020-02-18 15:43:46 +0100314 int ret, i;
315 const char * const nodes[] = {
316 "/chosen",
317 "/clocks",
318 "/firmware"
319 };
Patrice Chotardbb3a45b2017-09-04 14:55:56 +0200320
Simon Glass5039cab2020-11-28 17:50:09 -0700321 ret = dm_scan_fdt(pre_reloc_only);
Patrice Chotardbb3a45b2017-09-04 14:55:56 +0200322 if (ret) {
323 debug("dm_scan_fdt() failed: %d\n", ret);
324 return ret;
325 }
326
Patrick Delaunayc779e202020-02-18 15:43:46 +0100327 /* Some nodes aren't devices themselves but may contain some */
328 for (i = 0; i < ARRAY_SIZE(nodes); i++) {
Simon Glass5039cab2020-11-28 17:50:09 -0700329 ret = dm_scan_fdt_ofnode_path(nodes[i], pre_reloc_only);
Patrick Delaunayc779e202020-02-18 15:43:46 +0100330 if (ret) {
331 debug("dm_scan_fdt() scan for %s failed: %d\n",
332 nodes[i], ret);
333 return ret;
334 }
Rajan Vajae9fc81d2018-08-10 01:45:34 -0700335 }
336
Patrice Chotardbb3a45b2017-09-04 14:55:56 +0200337 return ret;
338}
Marek Vasut7f18c342019-08-31 18:03:28 +0200339#endif
Patrice Chotardbb3a45b2017-09-04 14:55:56 +0200340
Simon Glass97e22e32014-07-23 06:55:23 -0600341__weak int dm_scan_other(bool pre_reloc_only)
342{
343 return 0;
344}
345
Simon Glass781aa3e2021-03-15 17:25:40 +1300346#if CONFIG_IS_ENABLED(OF_PLATDATA_INST) && CONFIG_IS_ENABLED(READ_ONLY)
347void *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
Simon Glassc77695b2020-12-19 10:40:16 -0700355/**
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 */
365static int dm_scan(bool pre_reloc_only)
Simon Glassa730c5d2014-07-23 06:55:04 -0600366{
367 int ret;
368
Simon Glassb75b15b2020-12-03 16:55:23 -0700369 ret = dm_scan_plat(pre_reloc_only);
Simon Glassa730c5d2014-07-23 06:55:04 -0600370 if (ret) {
Simon Glassb75b15b2020-12-03 16:55:23 -0700371 debug("dm_scan_plat() failed: %d\n", ret);
Simon Glassc77695b2020-12-19 10:40:16 -0700372 return ret;
Simon Glassa730c5d2014-07-23 06:55:04 -0600373 }
Simon Glass0704b852015-02-27 22:06:41 -0700374
Simon Glass8d7e8162016-07-04 11:57:58 -0600375 if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
Simon Glass4d71d602020-11-28 17:50:10 -0700376 ret = dm_extended_scan(pre_reloc_only);
Simon Glass0704b852015-02-27 22:06:41 -0700377 if (ret) {
Simon Glass4d71d602020-11-28 17:50:10 -0700378 debug("dm_extended_scan() failed: %d\n", ret);
Simon Glassc77695b2020-12-19 10:40:16 -0700379 return ret;
Simon Glass0704b852015-02-27 22:06:41 -0700380 }
Simon Glassa730c5d2014-07-23 06:55:04 -0600381 }
Simon Glass0704b852015-02-27 22:06:41 -0700382
Simon Glass97e22e32014-07-23 06:55:23 -0600383 ret = dm_scan_other(pre_reloc_only);
384 if (ret)
Simon Glassc77695b2020-12-19 10:40:16 -0700385 return ret;
386
387 return 0;
388}
389
390int dm_init_and_scan(bool pre_reloc_only)
391{
392 int ret;
393
Simon Glassc77695b2020-12-19 10:40:16 -0700394 ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE));
395 if (ret) {
396 debug("dm_init() failed: %d\n", ret);
397 return ret;
398 }
Simon Glass43f7ea52021-03-15 17:25:17 +1300399 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 }
Simon Glassc77695b2020-12-19 10:40:16 -0700405 }
Simon Glassa730c5d2014-07-23 06:55:04 -0600406
407 return 0;
408}
409
Simon Glass719ea082020-07-07 13:11:38 -0600410#ifdef CONFIG_ACPIGEN
411static int root_acpi_get_name(const struct udevice *dev, char *out_name)
412{
413 return acpi_copy_name(out_name, "\\_SB");
414}
415
416struct acpi_ops root_acpi_ops = {
417 .get_name = root_acpi_get_name,
418};
419#endif
420
Simon Glassdd6ab882014-02-26 15:59:18 -0700421/* This is the root driver - all drivers are children of this */
422U_BOOT_DRIVER(root_driver) = {
423 .name = "root_driver",
424 .id = UCLASS_ROOT,
Simon Glass719ea082020-07-07 13:11:38 -0600425 ACPI_OPS_PTR(&root_acpi_ops)
Simon Glassdd6ab882014-02-26 15:59:18 -0700426};
427
428/* This is the root uclass */
429UCLASS_DRIVER(root) = {
430 .name = "root",
431 .id = UCLASS_ROOT,
432};