blob: e7b1f249682e3c6b282751f4965e2e3944fd999e [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
26static const struct driver_info root_info = {
27 .name = "root_driver",
28};
29
Heiko Schocherb74fcb42014-05-22 12:43:05 +020030struct udevice *dm_root(void)
Simon Glassdd6ab882014-02-26 15:59:18 -070031{
32 if (!gd->dm_root) {
33 dm_warn("Virtual root driver does not exist!\n");
34 return NULL;
35 }
36
37 return gd->dm_root;
38}
39
Michal Simek0ec473b2015-02-02 16:31:59 +010040#if defined(CONFIG_NEEDS_MANUAL_RELOC)
41void fix_drivers(void)
42{
43 struct driver *drv =
44 ll_entry_start(struct driver, driver);
45 const int n_ents = ll_entry_count(struct driver, driver);
46 struct driver *entry;
47
48 for (entry = drv; entry != drv + n_ents; entry++) {
49 if (entry->of_match)
50 entry->of_match = (const struct udevice_id *)
51 ((u32)entry->of_match + gd->reloc_off);
52 if (entry->bind)
53 entry->bind += gd->reloc_off;
54 if (entry->probe)
55 entry->probe += gd->reloc_off;
56 if (entry->remove)
57 entry->remove += gd->reloc_off;
58 if (entry->unbind)
59 entry->unbind += gd->reloc_off;
60 if (entry->ofdata_to_platdata)
61 entry->ofdata_to_platdata += gd->reloc_off;
Michal Simek61809f52015-10-27 13:48:08 +010062 if (entry->child_post_bind)
63 entry->child_post_bind += gd->reloc_off;
Michal Simek0ec473b2015-02-02 16:31:59 +010064 if (entry->child_pre_probe)
65 entry->child_pre_probe += gd->reloc_off;
66 if (entry->child_post_remove)
67 entry->child_post_remove += gd->reloc_off;
68 /* OPS are fixed in every uclass post_probe function */
69 if (entry->ops)
70 entry->ops += gd->reloc_off;
71 }
72}
73
74void fix_uclass(void)
75{
76 struct uclass_driver *uclass =
77 ll_entry_start(struct uclass_driver, uclass);
78 const int n_ents = ll_entry_count(struct uclass_driver, uclass);
79 struct uclass_driver *entry;
80
81 for (entry = uclass; entry != uclass + n_ents; entry++) {
82 if (entry->post_bind)
83 entry->post_bind += gd->reloc_off;
84 if (entry->pre_unbind)
85 entry->pre_unbind += gd->reloc_off;
Michal Simek61809f52015-10-27 13:48:08 +010086 if (entry->pre_probe)
87 entry->pre_probe += gd->reloc_off;
Michal Simek0ec473b2015-02-02 16:31:59 +010088 if (entry->post_probe)
89 entry->post_probe += gd->reloc_off;
90 if (entry->pre_remove)
91 entry->pre_remove += gd->reloc_off;
Michal Simek61809f52015-10-27 13:48:08 +010092 if (entry->child_post_bind)
93 entry->child_post_bind += gd->reloc_off;
94 if (entry->child_pre_probe)
95 entry->child_pre_probe += gd->reloc_off;
Michal Simek0ec473b2015-02-02 16:31:59 +010096 if (entry->init)
97 entry->init += gd->reloc_off;
98 if (entry->destroy)
99 entry->destroy += gd->reloc_off;
100 /* FIXME maybe also need to fix these ops */
101 if (entry->ops)
102 entry->ops += gd->reloc_off;
103 }
104}
105#endif
106
Simon Glassdd6ab882014-02-26 15:59:18 -0700107int dm_init(void)
108{
109 int ret;
110
111 if (gd->dm_root) {
112 dm_warn("Virtual root driver already exists!\n");
113 return -EINVAL;
114 }
Simon Glass34a1d352014-06-11 23:29:49 -0600115 INIT_LIST_HEAD(&DM_UCLASS_ROOT_NON_CONST);
Simon Glassdd6ab882014-02-26 15:59:18 -0700116
Michal Simek0ec473b2015-02-02 16:31:59 +0100117#if defined(CONFIG_NEEDS_MANUAL_RELOC)
118 fix_drivers();
119 fix_uclass();
120#endif
121
Simon Glassfef72b72014-07-23 06:55:03 -0600122 ret = device_bind_by_name(NULL, false, &root_info, &DM_ROOT_NON_CONST);
Simon Glassdd6ab882014-02-26 15:59:18 -0700123 if (ret)
124 return ret;
Masahiro Yamada366b24f2015-08-12 07:31:55 +0900125#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glasscc7cf942015-01-25 08:26:58 -0700126 DM_ROOT_NON_CONST->of_offset = 0;
127#endif
Simon Glass6d3b3e22014-07-23 06:55:00 -0600128 ret = device_probe(DM_ROOT_NON_CONST);
129 if (ret)
130 return ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700131
132 return 0;
133}
134
Simon Glass00197582014-07-23 06:55:01 -0600135int dm_uninit(void)
136{
137 device_remove(dm_root());
138 device_unbind(dm_root());
139
140 return 0;
141}
142
Simon Glassfef72b72014-07-23 06:55:03 -0600143int dm_scan_platdata(bool pre_reloc_only)
Simon Glassdd6ab882014-02-26 15:59:18 -0700144{
145 int ret;
146
Simon Glassfef72b72014-07-23 06:55:03 -0600147 ret = lists_bind_drivers(DM_ROOT_NON_CONST, pre_reloc_only);
Simon Glassdd6ab882014-02-26 15:59:18 -0700148 if (ret == -ENOENT) {
149 dm_warn("Some drivers were not found\n");
150 ret = 0;
151 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700152
Masahiro Yamada6cac81a2014-11-17 17:19:38 +0900153 return ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700154}
155
Masahiro Yamada366b24f2015-08-12 07:31:55 +0900156#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glass40717422014-07-23 06:55:18 -0600157int dm_scan_fdt_node(struct udevice *parent, const void *blob, int offset,
158 bool pre_reloc_only)
Simon Glassdd6ab882014-02-26 15:59:18 -0700159{
Simon Glassdd6ab882014-02-26 15:59:18 -0700160 int ret = 0, err;
Simon Glassdd6ab882014-02-26 15:59:18 -0700161
Simon Glass40717422014-07-23 06:55:18 -0600162 for (offset = fdt_first_subnode(blob, offset);
163 offset > 0;
164 offset = fdt_next_subnode(blob, offset)) {
165 if (pre_reloc_only &&
166 !fdt_getprop(blob, offset, "u-boot,dm-pre-reloc", NULL))
167 continue;
Simon Glass528b1ef2015-01-25 08:27:16 -0700168 if (!fdtdec_get_is_enabled(blob, offset)) {
169 dm_dbg(" - ignoring disabled device\n");
170 continue;
171 }
Simon Glass439bc842014-09-04 16:27:25 -0600172 err = lists_bind_fdt(parent, blob, offset, NULL);
Simon Glassc90b9172015-08-30 16:55:17 -0600173 if (err && !ret) {
Simon Glass40717422014-07-23 06:55:18 -0600174 ret = err;
Simon Glassc90b9172015-08-30 16:55:17 -0600175 debug("%s: ret=%d\n", fdt_get_name(blob, offset, NULL),
176 ret);
177 }
Simon Glass40717422014-07-23 06:55:18 -0600178 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700179
180 if (ret)
181 dm_warn("Some drivers failed to bind\n");
182
183 return ret;
184}
Simon Glass40717422014-07-23 06:55:18 -0600185
186int dm_scan_fdt(const void *blob, bool pre_reloc_only)
187{
188 return dm_scan_fdt_node(gd->dm_root, blob, 0, pre_reloc_only);
189}
Simon Glassdd6ab882014-02-26 15:59:18 -0700190#endif
191
Simon Glass97e22e32014-07-23 06:55:23 -0600192__weak int dm_scan_other(bool pre_reloc_only)
193{
194 return 0;
195}
196
Simon Glassa730c5d2014-07-23 06:55:04 -0600197int dm_init_and_scan(bool pre_reloc_only)
198{
199 int ret;
200
201 ret = dm_init();
202 if (ret) {
203 debug("dm_init() failed: %d\n", ret);
204 return ret;
205 }
206 ret = dm_scan_platdata(pre_reloc_only);
207 if (ret) {
208 debug("dm_scan_platdata() failed: %d\n", ret);
209 return ret;
210 }
Simon Glass0704b852015-02-27 22:06:41 -0700211
Masahiro Yamada366b24f2015-08-12 07:31:55 +0900212 if (CONFIG_IS_ENABLED(OF_CONTROL)) {
Simon Glass0704b852015-02-27 22:06:41 -0700213 ret = dm_scan_fdt(gd->fdt_blob, pre_reloc_only);
214 if (ret) {
215 debug("dm_scan_fdt() failed: %d\n", ret);
216 return ret;
217 }
Simon Glassa730c5d2014-07-23 06:55:04 -0600218 }
Simon Glass0704b852015-02-27 22:06:41 -0700219
Simon Glass97e22e32014-07-23 06:55:23 -0600220 ret = dm_scan_other(pre_reloc_only);
221 if (ret)
222 return ret;
Simon Glassa730c5d2014-07-23 06:55:04 -0600223
224 return 0;
225}
226
Simon Glassdd6ab882014-02-26 15:59:18 -0700227/* This is the root driver - all drivers are children of this */
228U_BOOT_DRIVER(root_driver) = {
229 .name = "root_driver",
230 .id = UCLASS_ROOT,
231};
232
233/* This is the root uclass */
234UCLASS_DRIVER(root) = {
235 .name = "root",
236 .id = UCLASS_ROOT,
237};