blob: 93ab568296809b23dab818fd0d9bdc0e78d17e98 [file] [log] [blame]
Simon Glassdd6ab882014-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 Glass528b1ef2015-01-25 08:27:16 -070012#include <fdtdec.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070013#include <malloc.h>
Simon Glassdf6bd7d2014-06-11 23:29:48 -060014#include <libfdt.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070015#include <dm/device.h>
16#include <dm/device-internal.h>
17#include <dm/lists.h>
18#include <dm/platdata.h>
Jeroen Hofsteecf0cf822014-06-25 21:57:45 +020019#include <dm/root.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070020#include <dm/uclass.h>
21#include <dm/util.h>
22#include <linux/list.h>
23
24DECLARE_GLOBAL_DATA_PTR;
25
Stefan Roesea0ea0f92015-12-14 16:18:15 +010026struct root_priv {
27 fdt_addr_t translation_offset; /* optional translation offset */
28};
29
Simon Glassdd6ab882014-02-26 15:59:18 -070030static const struct driver_info root_info = {
31 .name = "root_driver",
32};
33
Heiko Schocherb74fcb42014-05-22 12:43:05 +020034struct udevice *dm_root(void)
Simon Glassdd6ab882014-02-26 15:59:18 -070035{
36 if (!gd->dm_root) {
37 dm_warn("Virtual root driver does not exist!\n");
38 return NULL;
39 }
40
41 return gd->dm_root;
42}
43
Simon Glass1b0deee2016-11-13 14:21:58 -070044void dm_fixup_for_gd_move(struct global_data *new_gd)
45{
46 /* The sentinel node has moved, so update things that point to it */
47 new_gd->uclass_root.next->prev = &new_gd->uclass_root;
48 new_gd->uclass_root.prev->next = &new_gd->uclass_root;
49}
50
Stefan Roesea0ea0f92015-12-14 16:18:15 +010051fdt_addr_t dm_get_translation_offset(void)
52{
53 struct udevice *root = dm_root();
54 struct root_priv *priv = dev_get_priv(root);
55
56 return priv->translation_offset;
57}
58
59void dm_set_translation_offset(fdt_addr_t offs)
60{
61 struct udevice *root = dm_root();
62 struct root_priv *priv = dev_get_priv(root);
63
64 priv->translation_offset = offs;
65}
66
Michal Simek0ec473b2015-02-02 16:31:59 +010067#if defined(CONFIG_NEEDS_MANUAL_RELOC)
68void fix_drivers(void)
69{
70 struct driver *drv =
71 ll_entry_start(struct driver, driver);
72 const int n_ents = ll_entry_count(struct driver, driver);
73 struct driver *entry;
74
75 for (entry = drv; entry != drv + n_ents; entry++) {
76 if (entry->of_match)
77 entry->of_match = (const struct udevice_id *)
78 ((u32)entry->of_match + gd->reloc_off);
79 if (entry->bind)
80 entry->bind += gd->reloc_off;
81 if (entry->probe)
82 entry->probe += gd->reloc_off;
83 if (entry->remove)
84 entry->remove += gd->reloc_off;
85 if (entry->unbind)
86 entry->unbind += gd->reloc_off;
87 if (entry->ofdata_to_platdata)
88 entry->ofdata_to_platdata += gd->reloc_off;
Michal Simek61809f52015-10-27 13:48:08 +010089 if (entry->child_post_bind)
90 entry->child_post_bind += gd->reloc_off;
Michal Simek0ec473b2015-02-02 16:31:59 +010091 if (entry->child_pre_probe)
92 entry->child_pre_probe += gd->reloc_off;
93 if (entry->child_post_remove)
94 entry->child_post_remove += gd->reloc_off;
95 /* OPS are fixed in every uclass post_probe function */
96 if (entry->ops)
97 entry->ops += gd->reloc_off;
98 }
99}
100
101void fix_uclass(void)
102{
103 struct uclass_driver *uclass =
104 ll_entry_start(struct uclass_driver, uclass);
105 const int n_ents = ll_entry_count(struct uclass_driver, uclass);
106 struct uclass_driver *entry;
107
108 for (entry = uclass; entry != uclass + n_ents; entry++) {
109 if (entry->post_bind)
110 entry->post_bind += gd->reloc_off;
111 if (entry->pre_unbind)
112 entry->pre_unbind += gd->reloc_off;
Michal Simek61809f52015-10-27 13:48:08 +0100113 if (entry->pre_probe)
114 entry->pre_probe += gd->reloc_off;
Michal Simek0ec473b2015-02-02 16:31:59 +0100115 if (entry->post_probe)
116 entry->post_probe += gd->reloc_off;
117 if (entry->pre_remove)
118 entry->pre_remove += gd->reloc_off;
Michal Simek61809f52015-10-27 13:48:08 +0100119 if (entry->child_post_bind)
120 entry->child_post_bind += gd->reloc_off;
121 if (entry->child_pre_probe)
122 entry->child_pre_probe += gd->reloc_off;
Michal Simek0ec473b2015-02-02 16:31:59 +0100123 if (entry->init)
124 entry->init += gd->reloc_off;
125 if (entry->destroy)
126 entry->destroy += gd->reloc_off;
127 /* FIXME maybe also need to fix these ops */
128 if (entry->ops)
129 entry->ops += gd->reloc_off;
130 }
131}
Angelo Dureghellof4d193b2016-05-21 12:05:49 +0200132
133void fix_devices(void)
134{
135 struct driver_info *dev =
136 ll_entry_start(struct driver_info, driver_info);
137 const int n_ents = ll_entry_count(struct driver_info, driver_info);
138 struct driver_info *entry;
139
140 for (entry = dev; entry != dev + n_ents; entry++) {
141 if (entry->platdata)
142 entry->platdata += gd->reloc_off;
143 }
144}
145
Michal Simek0ec473b2015-02-02 16:31:59 +0100146#endif
147
Simon Glassdd6ab882014-02-26 15:59:18 -0700148int dm_init(void)
149{
150 int ret;
151
152 if (gd->dm_root) {
153 dm_warn("Virtual root driver already exists!\n");
154 return -EINVAL;
155 }
Simon Glass34a1d352014-06-11 23:29:49 -0600156 INIT_LIST_HEAD(&DM_UCLASS_ROOT_NON_CONST);
Simon Glassdd6ab882014-02-26 15:59:18 -0700157
Michal Simek0ec473b2015-02-02 16:31:59 +0100158#if defined(CONFIG_NEEDS_MANUAL_RELOC)
159 fix_drivers();
160 fix_uclass();
Angelo Dureghellof4d193b2016-05-21 12:05:49 +0200161 fix_devices();
Michal Simek0ec473b2015-02-02 16:31:59 +0100162#endif
163
Simon Glassfef72b72014-07-23 06:55:03 -0600164 ret = device_bind_by_name(NULL, false, &root_info, &DM_ROOT_NON_CONST);
Simon Glassdd6ab882014-02-26 15:59:18 -0700165 if (ret)
166 return ret;
Masahiro Yamada366b24f2015-08-12 07:31:55 +0900167#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glasscc7cf942015-01-25 08:26:58 -0700168 DM_ROOT_NON_CONST->of_offset = 0;
169#endif
Simon Glass6d3b3e22014-07-23 06:55:00 -0600170 ret = device_probe(DM_ROOT_NON_CONST);
171 if (ret)
172 return ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700173
174 return 0;
175}
176
Simon Glass00197582014-07-23 06:55:01 -0600177int dm_uninit(void)
178{
179 device_remove(dm_root());
180 device_unbind(dm_root());
181
182 return 0;
183}
184
Simon Glassfef72b72014-07-23 06:55:03 -0600185int dm_scan_platdata(bool pre_reloc_only)
Simon Glassdd6ab882014-02-26 15:59:18 -0700186{
187 int ret;
188
Simon Glassfef72b72014-07-23 06:55:03 -0600189 ret = lists_bind_drivers(DM_ROOT_NON_CONST, pre_reloc_only);
Simon Glassdd6ab882014-02-26 15:59:18 -0700190 if (ret == -ENOENT) {
191 dm_warn("Some drivers were not found\n");
192 ret = 0;
193 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700194
Masahiro Yamada6cac81a2014-11-17 17:19:38 +0900195 return ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700196}
197
Simon Glass8d7e8162016-07-04 11:57:58 -0600198#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass40717422014-07-23 06:55:18 -0600199int dm_scan_fdt_node(struct udevice *parent, const void *blob, int offset,
200 bool pre_reloc_only)
Simon Glassdd6ab882014-02-26 15:59:18 -0700201{
Simon Glassdd6ab882014-02-26 15:59:18 -0700202 int ret = 0, err;
Simon Glassdd6ab882014-02-26 15:59:18 -0700203
Simon Glass40717422014-07-23 06:55:18 -0600204 for (offset = fdt_first_subnode(blob, offset);
205 offset > 0;
206 offset = fdt_next_subnode(blob, offset)) {
207 if (pre_reloc_only &&
Heiko Stübner9a2cdca2017-02-18 19:46:21 +0100208 !dm_fdt_pre_reloc(blob, offset))
Simon Glass40717422014-07-23 06:55:18 -0600209 continue;
Simon Glass528b1ef2015-01-25 08:27:16 -0700210 if (!fdtdec_get_is_enabled(blob, offset)) {
211 dm_dbg(" - ignoring disabled device\n");
212 continue;
213 }
Simon Glass439bc842014-09-04 16:27:25 -0600214 err = lists_bind_fdt(parent, blob, offset, NULL);
Simon Glassc90b9172015-08-30 16:55:17 -0600215 if (err && !ret) {
Simon Glass40717422014-07-23 06:55:18 -0600216 ret = err;
Simon Glassc90b9172015-08-30 16:55:17 -0600217 debug("%s: ret=%d\n", fdt_get_name(blob, offset, NULL),
218 ret);
219 }
Simon Glass40717422014-07-23 06:55:18 -0600220 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700221
222 if (ret)
223 dm_warn("Some drivers failed to bind\n");
224
225 return ret;
226}
Simon Glass40717422014-07-23 06:55:18 -0600227
Simon Glass5d5388d2016-07-05 17:10:08 -0600228int dm_scan_fdt_dev(struct udevice *dev)
229{
Simon Glassdd79d6e2017-01-17 16:52:55 -0700230 if (dev_of_offset(dev) == -1)
Simon Glass5d5388d2016-07-05 17:10:08 -0600231 return 0;
232
Simon Glassdd79d6e2017-01-17 16:52:55 -0700233 return dm_scan_fdt_node(dev, gd->fdt_blob, dev_of_offset(dev),
Simon Glass5d5388d2016-07-05 17:10:08 -0600234 gd->flags & GD_FLG_RELOC ? false : true);
235}
236
Simon Glass40717422014-07-23 06:55:18 -0600237int dm_scan_fdt(const void *blob, bool pre_reloc_only)
238{
239 return dm_scan_fdt_node(gd->dm_root, blob, 0, pre_reloc_only);
240}
Simon Glassdd6ab882014-02-26 15:59:18 -0700241#endif
242
Simon Glass97e22e32014-07-23 06:55:23 -0600243__weak int dm_scan_other(bool pre_reloc_only)
244{
245 return 0;
246}
247
Simon Glassa730c5d2014-07-23 06:55:04 -0600248int dm_init_and_scan(bool pre_reloc_only)
249{
250 int ret;
251
252 ret = dm_init();
253 if (ret) {
254 debug("dm_init() failed: %d\n", ret);
255 return ret;
256 }
257 ret = dm_scan_platdata(pre_reloc_only);
258 if (ret) {
259 debug("dm_scan_platdata() failed: %d\n", ret);
260 return ret;
261 }
Simon Glass0704b852015-02-27 22:06:41 -0700262
Simon Glass8d7e8162016-07-04 11:57:58 -0600263 if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
Simon Glass0704b852015-02-27 22:06:41 -0700264 ret = dm_scan_fdt(gd->fdt_blob, pre_reloc_only);
265 if (ret) {
266 debug("dm_scan_fdt() failed: %d\n", ret);
267 return ret;
268 }
Simon Glassa730c5d2014-07-23 06:55:04 -0600269 }
Simon Glass0704b852015-02-27 22:06:41 -0700270
Simon Glass97e22e32014-07-23 06:55:23 -0600271 ret = dm_scan_other(pre_reloc_only);
272 if (ret)
273 return ret;
Simon Glassa730c5d2014-07-23 06:55:04 -0600274
275 return 0;
276}
277
Simon Glassdd6ab882014-02-26 15:59:18 -0700278/* This is the root driver - all drivers are children of this */
279U_BOOT_DRIVER(root_driver) = {
280 .name = "root_driver",
281 .id = UCLASS_ROOT,
Stefan Roesea0ea0f92015-12-14 16:18:15 +0100282 .priv_auto_alloc_size = sizeof(struct root_priv),
Simon Glassdd6ab882014-02-26 15:59:18 -0700283};
284
285/* This is the root uclass */
286UCLASS_DRIVER(root) = {
287 .name = "root",
288 .id = UCLASS_ROOT,
289};