blob: 976e2c4fdd9d6b3842f7db8a2781306a3739690f [file] [log] [blame]
Simon Glass6494d702014-02-26 15:59:18 -07001/*
2 * Copyright (c) 2013 Google, Inc
3 *
4 * (C) Copyright 2012
5 * Pavel Herrmann <morpheus.ibis@gmail.com>
6 *
7 * SPDX-License-Identifier: GPL-2.0+
8 */
9
10#include <common.h>
11#include <errno.h>
Simon Glass94f7afd2015-01-25 08:27:16 -070012#include <fdtdec.h>
Simon Glass6494d702014-02-26 15:59:18 -070013#include <malloc.h>
Simon Glass6a6d8fb2014-06-11 23:29:48 -060014#include <libfdt.h>
Simon Glass6494d702014-02-26 15:59:18 -070015#include <dm/device.h>
16#include <dm/device-internal.h>
17#include <dm/lists.h>
Simon Glass19c82052017-05-18 20:09:08 -060018#include <dm/of.h>
19#include <dm/of_access.h>
Simon Glass6494d702014-02-26 15:59:18 -070020#include <dm/platdata.h>
Simon Glass19c82052017-05-18 20:09:08 -060021#include <dm/read.h>
Jeroen Hofsteefd536d82014-06-25 21:57:45 +020022#include <dm/root.h>
Simon Glass6494d702014-02-26 15:59:18 -070023#include <dm/uclass.h>
24#include <dm/util.h>
25#include <linux/list.h>
26
27DECLARE_GLOBAL_DATA_PTR;
28
Stefan Roese66eaea62015-12-14 16:18:15 +010029struct root_priv {
30 fdt_addr_t translation_offset; /* optional translation offset */
31};
32
Simon Glass6494d702014-02-26 15:59:18 -070033static const struct driver_info root_info = {
34 .name = "root_driver",
35};
36
Heiko Schocher54c5d082014-05-22 12:43:05 +020037struct udevice *dm_root(void)
Simon Glass6494d702014-02-26 15:59:18 -070038{
39 if (!gd->dm_root) {
40 dm_warn("Virtual root driver does not exist!\n");
41 return NULL;
42 }
43
44 return gd->dm_root;
45}
46
Simon Glass2f11cd92016-11-13 14:21:58 -070047void dm_fixup_for_gd_move(struct global_data *new_gd)
48{
49 /* The sentinel node has moved, so update things that point to it */
Lokesh Vutlab0d95122017-02-13 09:21:22 +053050 if (gd->dm_root) {
51 new_gd->uclass_root.next->prev = &new_gd->uclass_root;
52 new_gd->uclass_root.prev->next = &new_gd->uclass_root;
53 }
Simon Glass2f11cd92016-11-13 14:21:58 -070054}
55
Stefan Roese66eaea62015-12-14 16:18:15 +010056fdt_addr_t dm_get_translation_offset(void)
57{
58 struct udevice *root = dm_root();
59 struct root_priv *priv = dev_get_priv(root);
60
61 return priv->translation_offset;
62}
63
64void dm_set_translation_offset(fdt_addr_t offs)
65{
66 struct udevice *root = dm_root();
67 struct root_priv *priv = dev_get_priv(root);
68
69 priv->translation_offset = offs;
70}
71
Michal Simek484fdf52015-02-02 16:31:59 +010072#if defined(CONFIG_NEEDS_MANUAL_RELOC)
73void fix_drivers(void)
74{
75 struct driver *drv =
76 ll_entry_start(struct driver, driver);
77 const int n_ents = ll_entry_count(struct driver, driver);
78 struct driver *entry;
79
80 for (entry = drv; entry != drv + n_ents; entry++) {
81 if (entry->of_match)
82 entry->of_match = (const struct udevice_id *)
83 ((u32)entry->of_match + gd->reloc_off);
84 if (entry->bind)
85 entry->bind += gd->reloc_off;
86 if (entry->probe)
87 entry->probe += gd->reloc_off;
88 if (entry->remove)
89 entry->remove += gd->reloc_off;
90 if (entry->unbind)
91 entry->unbind += gd->reloc_off;
92 if (entry->ofdata_to_platdata)
93 entry->ofdata_to_platdata += gd->reloc_off;
Michal Simek31e10292015-10-27 13:48:08 +010094 if (entry->child_post_bind)
95 entry->child_post_bind += gd->reloc_off;
Michal Simek484fdf52015-02-02 16:31:59 +010096 if (entry->child_pre_probe)
97 entry->child_pre_probe += gd->reloc_off;
98 if (entry->child_post_remove)
99 entry->child_post_remove += gd->reloc_off;
100 /* OPS are fixed in every uclass post_probe function */
101 if (entry->ops)
102 entry->ops += gd->reloc_off;
103 }
104}
105
106void fix_uclass(void)
107{
108 struct uclass_driver *uclass =
109 ll_entry_start(struct uclass_driver, uclass);
110 const int n_ents = ll_entry_count(struct uclass_driver, uclass);
111 struct uclass_driver *entry;
112
113 for (entry = uclass; entry != uclass + n_ents; entry++) {
114 if (entry->post_bind)
115 entry->post_bind += gd->reloc_off;
116 if (entry->pre_unbind)
117 entry->pre_unbind += gd->reloc_off;
Michal Simek31e10292015-10-27 13:48:08 +0100118 if (entry->pre_probe)
119 entry->pre_probe += gd->reloc_off;
Michal Simek484fdf52015-02-02 16:31:59 +0100120 if (entry->post_probe)
121 entry->post_probe += gd->reloc_off;
122 if (entry->pre_remove)
123 entry->pre_remove += gd->reloc_off;
Michal Simek31e10292015-10-27 13:48:08 +0100124 if (entry->child_post_bind)
125 entry->child_post_bind += gd->reloc_off;
126 if (entry->child_pre_probe)
127 entry->child_pre_probe += gd->reloc_off;
Michal Simek484fdf52015-02-02 16:31:59 +0100128 if (entry->init)
129 entry->init += gd->reloc_off;
130 if (entry->destroy)
131 entry->destroy += gd->reloc_off;
132 /* FIXME maybe also need to fix these ops */
133 if (entry->ops)
134 entry->ops += gd->reloc_off;
135 }
136}
Angelo Dureghello5aeedeb2016-05-21 12:05:49 +0200137
138void fix_devices(void)
139{
140 struct driver_info *dev =
141 ll_entry_start(struct driver_info, driver_info);
142 const int n_ents = ll_entry_count(struct driver_info, driver_info);
143 struct driver_info *entry;
144
145 for (entry = dev; entry != dev + n_ents; entry++) {
146 if (entry->platdata)
147 entry->platdata += gd->reloc_off;
148 }
149}
150
Michal Simek484fdf52015-02-02 16:31:59 +0100151#endif
152
Simon Glass19c82052017-05-18 20:09:08 -0600153int dm_init(bool of_live)
Simon Glass6494d702014-02-26 15:59:18 -0700154{
155 int ret;
156
157 if (gd->dm_root) {
158 dm_warn("Virtual root driver already exists!\n");
159 return -EINVAL;
160 }
Simon Glass89876a52014-06-11 23:29:49 -0600161 INIT_LIST_HEAD(&DM_UCLASS_ROOT_NON_CONST);
Simon Glass6494d702014-02-26 15:59:18 -0700162
Michal Simek484fdf52015-02-02 16:31:59 +0100163#if defined(CONFIG_NEEDS_MANUAL_RELOC)
164 fix_drivers();
165 fix_uclass();
Angelo Dureghello5aeedeb2016-05-21 12:05:49 +0200166 fix_devices();
Michal Simek484fdf52015-02-02 16:31:59 +0100167#endif
168
Simon Glass00606d72014-07-23 06:55:03 -0600169 ret = device_bind_by_name(NULL, false, &root_info, &DM_ROOT_NON_CONST);
Simon Glass6494d702014-02-26 15:59:18 -0700170 if (ret)
171 return ret;
Masahiro Yamada0f925822015-08-12 07:31:55 +0900172#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glass19c82052017-05-18 20:09:08 -0600173# if CONFIG_IS_ENABLED(OF_LIVE)
174 if (of_live)
175 DM_ROOT_NON_CONST->node = np_to_ofnode(gd->of_root);
176 else
177#endif
178 DM_ROOT_NON_CONST->node = offset_to_ofnode(0);
Simon Glass2f3b95d2015-01-25 08:26:58 -0700179#endif
Simon Glass74978122014-07-23 06:55:00 -0600180 ret = device_probe(DM_ROOT_NON_CONST);
181 if (ret)
182 return ret;
Simon Glass6494d702014-02-26 15:59:18 -0700183
184 return 0;
185}
186
Simon Glass9adbd7a2014-07-23 06:55:01 -0600187int dm_uninit(void)
188{
Stefan Roese706865a2017-03-20 12:51:48 +0100189 device_remove(dm_root(), DM_REMOVE_NORMAL);
Simon Glass9adbd7a2014-07-23 06:55:01 -0600190 device_unbind(dm_root());
191
192 return 0;
193}
194
Stefan Roesebc85aa42017-03-27 10:58:53 +0200195#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
196int dm_remove_devices_flags(uint flags)
197{
198 device_remove(dm_root(), flags);
199
200 return 0;
201}
202#endif
203
Simon Glass00606d72014-07-23 06:55:03 -0600204int dm_scan_platdata(bool pre_reloc_only)
Simon Glass6494d702014-02-26 15:59:18 -0700205{
206 int ret;
207
Simon Glass00606d72014-07-23 06:55:03 -0600208 ret = lists_bind_drivers(DM_ROOT_NON_CONST, pre_reloc_only);
Simon Glass6494d702014-02-26 15:59:18 -0700209 if (ret == -ENOENT) {
210 dm_warn("Some drivers were not found\n");
211 ret = 0;
212 }
Simon Glass6494d702014-02-26 15:59:18 -0700213
Masahiro Yamadacbf86d72014-11-17 17:19:38 +0900214 return ret;
Simon Glass6494d702014-02-26 15:59:18 -0700215}
216
Simon Glass19c82052017-05-18 20:09:08 -0600217#if CONFIG_IS_ENABLED(OF_LIVE)
218static int dm_scan_fdt_live(struct udevice *parent,
219 const struct device_node *node_parent,
220 bool pre_reloc_only)
221{
222 struct device_node *np;
223 int ret = 0, err;
224
225 for (np = node_parent->child; np; np = np->sibling) {
226 if (pre_reloc_only &&
227 !of_find_property(np, "u-boot,dm-pre-reloc", NULL))
228 continue;
229 if (!of_device_is_available(np)) {
Masahiro Yamadaceb91902017-09-29 12:31:20 +0900230 pr_debug(" - ignoring disabled device\n");
Simon Glass19c82052017-05-18 20:09:08 -0600231 continue;
232 }
233 err = lists_bind_fdt(parent, np_to_ofnode(np), NULL);
234 if (err && !ret) {
235 ret = err;
236 debug("%s: ret=%d\n", np->name, ret);
237 }
238 }
239
240 if (ret)
241 dm_warn("Some drivers failed to bind\n");
242
243 return ret;
244}
245#endif /* CONFIG_IS_ENABLED(OF_LIVE) */
246
Simon Glass29629eb2016-07-04 11:57:58 -0600247#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassa771a042017-05-17 17:18:08 -0600248/**
249 * dm_scan_fdt_node() - Scan the device tree and bind drivers for a node
250 *
251 * This scans the subnodes of a device tree node and and creates a driver
252 * for each one.
253 *
254 * @parent: Parent device for the devices that will be created
255 * @blob: Pointer to device tree blob
256 * @offset: Offset of node to scan
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 */
261static int dm_scan_fdt_node(struct udevice *parent, const void *blob,
262 int offset, bool pre_reloc_only)
Simon Glass6494d702014-02-26 15:59:18 -0700263{
Simon Glass6494d702014-02-26 15:59:18 -0700264 int ret = 0, err;
Simon Glass6494d702014-02-26 15:59:18 -0700265
Simon Glass1ca7e202014-07-23 06:55:18 -0600266 for (offset = fdt_first_subnode(blob, offset);
267 offset > 0;
268 offset = fdt_next_subnode(blob, offset)) {
269 if (pre_reloc_only &&
Heiko Stübner27326c72017-02-18 19:46:21 +0100270 !dm_fdt_pre_reloc(blob, offset))
Simon Glass1ca7e202014-07-23 06:55:18 -0600271 continue;
Simon Glass94f7afd2015-01-25 08:27:16 -0700272 if (!fdtdec_get_is_enabled(blob, offset)) {
Masahiro Yamadaceb91902017-09-29 12:31:20 +0900273 pr_debug(" - ignoring disabled device\n");
Simon Glass94f7afd2015-01-25 08:27:16 -0700274 continue;
275 }
Simon Glassf5b57192017-05-18 20:09:06 -0600276 err = lists_bind_fdt(parent, offset_to_ofnode(offset), NULL);
Simon Glassbc7b2f42015-08-30 16:55:17 -0600277 if (err && !ret) {
Simon Glass1ca7e202014-07-23 06:55:18 -0600278 ret = err;
Simon Glassbc7b2f42015-08-30 16:55:17 -0600279 debug("%s: ret=%d\n", fdt_get_name(blob, offset, NULL),
280 ret);
281 }
Simon Glass1ca7e202014-07-23 06:55:18 -0600282 }
Simon Glass6494d702014-02-26 15:59:18 -0700283
284 if (ret)
285 dm_warn("Some drivers failed to bind\n");
286
287 return ret;
288}
Simon Glass1ca7e202014-07-23 06:55:18 -0600289
Simon Glasscc7f66f2016-07-05 17:10:08 -0600290int dm_scan_fdt_dev(struct udevice *dev)
291{
Simon Glass19c82052017-05-18 20:09:08 -0600292 if (!dev_of_valid(dev))
Simon Glasscc7f66f2016-07-05 17:10:08 -0600293 return 0;
294
Simon Glass19c82052017-05-18 20:09:08 -0600295#if CONFIG_IS_ENABLED(OF_LIVE)
296 if (of_live_active())
297 return dm_scan_fdt_live(dev, dev_np(dev),
298 gd->flags & GD_FLG_RELOC ? false : true);
299 else
300#endif
Simon Glasse160f7d2017-01-17 16:52:55 -0700301 return dm_scan_fdt_node(dev, gd->fdt_blob, dev_of_offset(dev),
Simon Glasscc7f66f2016-07-05 17:10:08 -0600302 gd->flags & GD_FLG_RELOC ? false : true);
303}
304
Simon Glass1ca7e202014-07-23 06:55:18 -0600305int dm_scan_fdt(const void *blob, bool pre_reloc_only)
306{
Simon Glass19c82052017-05-18 20:09:08 -0600307#if CONFIG_IS_ENABLED(OF_LIVE)
308 if (of_live_active())
309 return dm_scan_fdt_live(gd->dm_root, gd->of_root,
310 pre_reloc_only);
311 else
312#endif
Simon Glass1ca7e202014-07-23 06:55:18 -0600313 return dm_scan_fdt_node(gd->dm_root, blob, 0, pre_reloc_only);
314}
Patrice Chotarde81c9862017-09-04 14:55:56 +0200315#else
316static int dm_scan_fdt_node(struct udevice *parent, const void *blob,
317 int offset, bool pre_reloc_only)
318{
319 return 0;
320}
Simon Glass6494d702014-02-26 15:59:18 -0700321#endif
322
Patrice Chotarde81c9862017-09-04 14:55:56 +0200323int dm_extended_scan_fdt(const void *blob, bool pre_reloc_only)
324{
325 int node, ret;
326
327 ret = dm_scan_fdt(gd->fdt_blob, pre_reloc_only);
328 if (ret) {
329 debug("dm_scan_fdt() failed: %d\n", ret);
330 return ret;
331 }
332
333 /* bind fixed-clock */
334 node = ofnode_to_offset(ofnode_path("/clocks"));
335 /* if no DT "clocks" node, no need to go further */
336 if (node < 0)
337 return ret;
338
339 ret = dm_scan_fdt_node(gd->dm_root, gd->fdt_blob, node,
340 pre_reloc_only);
341 if (ret)
342 debug("dm_scan_fdt_node() failed: %d\n", ret);
343
344 return ret;
345}
346
Simon Glassbb585032014-07-23 06:55:23 -0600347__weak int dm_scan_other(bool pre_reloc_only)
348{
349 return 0;
350}
351
Simon Glassab7cd622014-07-23 06:55:04 -0600352int dm_init_and_scan(bool pre_reloc_only)
353{
354 int ret;
355
Simon Glass19c82052017-05-18 20:09:08 -0600356 ret = dm_init(IS_ENABLED(CONFIG_OF_LIVE));
Simon Glassab7cd622014-07-23 06:55:04 -0600357 if (ret) {
358 debug("dm_init() failed: %d\n", ret);
359 return ret;
360 }
361 ret = dm_scan_platdata(pre_reloc_only);
362 if (ret) {
363 debug("dm_scan_platdata() failed: %d\n", ret);
364 return ret;
365 }
Simon Glassb2b0d3e2015-02-27 22:06:41 -0700366
Simon Glass29629eb2016-07-04 11:57:58 -0600367 if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
Patrice Chotarde81c9862017-09-04 14:55:56 +0200368 ret = dm_extended_scan_fdt(gd->fdt_blob, pre_reloc_only);
Simon Glassb2b0d3e2015-02-27 22:06:41 -0700369 if (ret) {
Patrice Chotarde81c9862017-09-04 14:55:56 +0200370 debug("dm_extended_scan_dt() failed: %d\n", ret);
Simon Glassb2b0d3e2015-02-27 22:06:41 -0700371 return ret;
372 }
Simon Glassab7cd622014-07-23 06:55:04 -0600373 }
Simon Glassb2b0d3e2015-02-27 22:06:41 -0700374
Simon Glassbb585032014-07-23 06:55:23 -0600375 ret = dm_scan_other(pre_reloc_only);
376 if (ret)
377 return ret;
Simon Glassab7cd622014-07-23 06:55:04 -0600378
379 return 0;
380}
381
Simon Glass6494d702014-02-26 15:59:18 -0700382/* This is the root driver - all drivers are children of this */
383U_BOOT_DRIVER(root_driver) = {
384 .name = "root_driver",
385 .id = UCLASS_ROOT,
Stefan Roese66eaea62015-12-14 16:18:15 +0100386 .priv_auto_alloc_size = sizeof(struct root_priv),
Simon Glass6494d702014-02-26 15:59:18 -0700387};
388
389/* This is the root uclass */
390UCLASS_DRIVER(root) = {
391 .name = "root",
392 .id = UCLASS_ROOT,
393};