blob: 13c2713e615391e7ce2fb4194718d6b83f2fdabb [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>
18#include <dm/platdata.h>
Jeroen Hofsteefd536d82014-06-25 21:57:45 +020019#include <dm/root.h>
Simon Glass6494d702014-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 Roese66eaea62015-12-14 16:18:15 +010026struct root_priv {
27 fdt_addr_t translation_offset; /* optional translation offset */
28};
29
Simon Glass6494d702014-02-26 15:59:18 -070030static const struct driver_info root_info = {
31 .name = "root_driver",
32};
33
Heiko Schocher54c5d082014-05-22 12:43:05 +020034struct udevice *dm_root(void)
Simon Glass6494d702014-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
Stefan Roese66eaea62015-12-14 16:18:15 +010044fdt_addr_t dm_get_translation_offset(void)
45{
46 struct udevice *root = dm_root();
47 struct root_priv *priv = dev_get_priv(root);
48
49 return priv->translation_offset;
50}
51
52void dm_set_translation_offset(fdt_addr_t offs)
53{
54 struct udevice *root = dm_root();
55 struct root_priv *priv = dev_get_priv(root);
56
57 priv->translation_offset = offs;
58}
59
Michal Simek484fdf52015-02-02 16:31:59 +010060#if defined(CONFIG_NEEDS_MANUAL_RELOC)
61void fix_drivers(void)
62{
63 struct driver *drv =
64 ll_entry_start(struct driver, driver);
65 const int n_ents = ll_entry_count(struct driver, driver);
66 struct driver *entry;
67
68 for (entry = drv; entry != drv + n_ents; entry++) {
69 if (entry->of_match)
70 entry->of_match = (const struct udevice_id *)
71 ((u32)entry->of_match + gd->reloc_off);
72 if (entry->bind)
73 entry->bind += gd->reloc_off;
74 if (entry->probe)
75 entry->probe += gd->reloc_off;
76 if (entry->remove)
77 entry->remove += gd->reloc_off;
78 if (entry->unbind)
79 entry->unbind += gd->reloc_off;
80 if (entry->ofdata_to_platdata)
81 entry->ofdata_to_platdata += gd->reloc_off;
Michal Simek31e10292015-10-27 13:48:08 +010082 if (entry->child_post_bind)
83 entry->child_post_bind += gd->reloc_off;
Michal Simek484fdf52015-02-02 16:31:59 +010084 if (entry->child_pre_probe)
85 entry->child_pre_probe += gd->reloc_off;
86 if (entry->child_post_remove)
87 entry->child_post_remove += gd->reloc_off;
88 /* OPS are fixed in every uclass post_probe function */
89 if (entry->ops)
90 entry->ops += gd->reloc_off;
91 }
92}
93
94void fix_uclass(void)
95{
96 struct uclass_driver *uclass =
97 ll_entry_start(struct uclass_driver, uclass);
98 const int n_ents = ll_entry_count(struct uclass_driver, uclass);
99 struct uclass_driver *entry;
100
101 for (entry = uclass; entry != uclass + n_ents; entry++) {
102 if (entry->post_bind)
103 entry->post_bind += gd->reloc_off;
104 if (entry->pre_unbind)
105 entry->pre_unbind += gd->reloc_off;
Michal Simek31e10292015-10-27 13:48:08 +0100106 if (entry->pre_probe)
107 entry->pre_probe += gd->reloc_off;
Michal Simek484fdf52015-02-02 16:31:59 +0100108 if (entry->post_probe)
109 entry->post_probe += gd->reloc_off;
110 if (entry->pre_remove)
111 entry->pre_remove += gd->reloc_off;
Michal Simek31e10292015-10-27 13:48:08 +0100112 if (entry->child_post_bind)
113 entry->child_post_bind += gd->reloc_off;
114 if (entry->child_pre_probe)
115 entry->child_pre_probe += gd->reloc_off;
Michal Simek484fdf52015-02-02 16:31:59 +0100116 if (entry->init)
117 entry->init += gd->reloc_off;
118 if (entry->destroy)
119 entry->destroy += gd->reloc_off;
120 /* FIXME maybe also need to fix these ops */
121 if (entry->ops)
122 entry->ops += gd->reloc_off;
123 }
124}
125#endif
126
Simon Glass6494d702014-02-26 15:59:18 -0700127int dm_init(void)
128{
129 int ret;
130
131 if (gd->dm_root) {
132 dm_warn("Virtual root driver already exists!\n");
133 return -EINVAL;
134 }
Simon Glass89876a52014-06-11 23:29:49 -0600135 INIT_LIST_HEAD(&DM_UCLASS_ROOT_NON_CONST);
Simon Glass6494d702014-02-26 15:59:18 -0700136
Michal Simek484fdf52015-02-02 16:31:59 +0100137#if defined(CONFIG_NEEDS_MANUAL_RELOC)
138 fix_drivers();
139 fix_uclass();
140#endif
141
Simon Glass00606d72014-07-23 06:55:03 -0600142 ret = device_bind_by_name(NULL, false, &root_info, &DM_ROOT_NON_CONST);
Simon Glass6494d702014-02-26 15:59:18 -0700143 if (ret)
144 return ret;
Masahiro Yamada0f925822015-08-12 07:31:55 +0900145#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glass2f3b95d2015-01-25 08:26:58 -0700146 DM_ROOT_NON_CONST->of_offset = 0;
147#endif
Simon Glass74978122014-07-23 06:55:00 -0600148 ret = device_probe(DM_ROOT_NON_CONST);
149 if (ret)
150 return ret;
Simon Glass6494d702014-02-26 15:59:18 -0700151
152 return 0;
153}
154
Simon Glass9adbd7a2014-07-23 06:55:01 -0600155int dm_uninit(void)
156{
157 device_remove(dm_root());
158 device_unbind(dm_root());
159
160 return 0;
161}
162
Simon Glass00606d72014-07-23 06:55:03 -0600163int dm_scan_platdata(bool pre_reloc_only)
Simon Glass6494d702014-02-26 15:59:18 -0700164{
165 int ret;
166
Simon Glass00606d72014-07-23 06:55:03 -0600167 ret = lists_bind_drivers(DM_ROOT_NON_CONST, pre_reloc_only);
Simon Glass6494d702014-02-26 15:59:18 -0700168 if (ret == -ENOENT) {
169 dm_warn("Some drivers were not found\n");
170 ret = 0;
171 }
Simon Glass6494d702014-02-26 15:59:18 -0700172
Masahiro Yamadacbf86d72014-11-17 17:19:38 +0900173 return ret;
Simon Glass6494d702014-02-26 15:59:18 -0700174}
175
Masahiro Yamada0f925822015-08-12 07:31:55 +0900176#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glass1ca7e202014-07-23 06:55:18 -0600177int dm_scan_fdt_node(struct udevice *parent, const void *blob, int offset,
178 bool pre_reloc_only)
Simon Glass6494d702014-02-26 15:59:18 -0700179{
Simon Glass6494d702014-02-26 15:59:18 -0700180 int ret = 0, err;
Simon Glass6494d702014-02-26 15:59:18 -0700181
Simon Glass1ca7e202014-07-23 06:55:18 -0600182 for (offset = fdt_first_subnode(blob, offset);
183 offset > 0;
184 offset = fdt_next_subnode(blob, offset)) {
185 if (pre_reloc_only &&
186 !fdt_getprop(blob, offset, "u-boot,dm-pre-reloc", NULL))
187 continue;
Simon Glass94f7afd2015-01-25 08:27:16 -0700188 if (!fdtdec_get_is_enabled(blob, offset)) {
189 dm_dbg(" - ignoring disabled device\n");
190 continue;
191 }
Simon Glass1f359e32014-09-04 16:27:25 -0600192 err = lists_bind_fdt(parent, blob, offset, NULL);
Simon Glassbc7b2f42015-08-30 16:55:17 -0600193 if (err && !ret) {
Simon Glass1ca7e202014-07-23 06:55:18 -0600194 ret = err;
Simon Glassbc7b2f42015-08-30 16:55:17 -0600195 debug("%s: ret=%d\n", fdt_get_name(blob, offset, NULL),
196 ret);
197 }
Simon Glass1ca7e202014-07-23 06:55:18 -0600198 }
Simon Glass6494d702014-02-26 15:59:18 -0700199
200 if (ret)
201 dm_warn("Some drivers failed to bind\n");
202
203 return ret;
204}
Simon Glass1ca7e202014-07-23 06:55:18 -0600205
206int dm_scan_fdt(const void *blob, bool pre_reloc_only)
207{
208 return dm_scan_fdt_node(gd->dm_root, blob, 0, pre_reloc_only);
209}
Simon Glass6494d702014-02-26 15:59:18 -0700210#endif
211
Simon Glassbb585032014-07-23 06:55:23 -0600212__weak int dm_scan_other(bool pre_reloc_only)
213{
214 return 0;
215}
216
Simon Glassab7cd622014-07-23 06:55:04 -0600217int dm_init_and_scan(bool pre_reloc_only)
218{
219 int ret;
220
221 ret = dm_init();
222 if (ret) {
223 debug("dm_init() failed: %d\n", ret);
224 return ret;
225 }
226 ret = dm_scan_platdata(pre_reloc_only);
227 if (ret) {
228 debug("dm_scan_platdata() failed: %d\n", ret);
229 return ret;
230 }
Simon Glassb2b0d3e2015-02-27 22:06:41 -0700231
Masahiro Yamada0f925822015-08-12 07:31:55 +0900232 if (CONFIG_IS_ENABLED(OF_CONTROL)) {
Simon Glassb2b0d3e2015-02-27 22:06:41 -0700233 ret = dm_scan_fdt(gd->fdt_blob, pre_reloc_only);
234 if (ret) {
235 debug("dm_scan_fdt() failed: %d\n", ret);
236 return ret;
237 }
Simon Glassab7cd622014-07-23 06:55:04 -0600238 }
Simon Glassb2b0d3e2015-02-27 22:06:41 -0700239
Simon Glassbb585032014-07-23 06:55:23 -0600240 ret = dm_scan_other(pre_reloc_only);
241 if (ret)
242 return ret;
Simon Glassab7cd622014-07-23 06:55:04 -0600243
244 return 0;
245}
246
Simon Glass6494d702014-02-26 15:59:18 -0700247/* This is the root driver - all drivers are children of this */
248U_BOOT_DRIVER(root_driver) = {
249 .name = "root_driver",
250 .id = UCLASS_ROOT,
Stefan Roese66eaea62015-12-14 16:18:15 +0100251 .priv_auto_alloc_size = sizeof(struct root_priv),
Simon Glass6494d702014-02-26 15:59:18 -0700252};
253
254/* This is the root uclass */
255UCLASS_DRIVER(root) = {
256 .name = "root",
257 .id = UCLASS_ROOT,
258};