blob: 9b5c6bb10cb13d1648c344970838d967494e26a4 [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;
62 if (entry->child_pre_probe)
63 entry->child_pre_probe += gd->reloc_off;
64 if (entry->child_post_remove)
65 entry->child_post_remove += gd->reloc_off;
66 /* OPS are fixed in every uclass post_probe function */
67 if (entry->ops)
68 entry->ops += gd->reloc_off;
69 }
70}
71
72void fix_uclass(void)
73{
74 struct uclass_driver *uclass =
75 ll_entry_start(struct uclass_driver, uclass);
76 const int n_ents = ll_entry_count(struct uclass_driver, uclass);
77 struct uclass_driver *entry;
78
79 for (entry = uclass; entry != uclass + n_ents; entry++) {
80 if (entry->post_bind)
81 entry->post_bind += gd->reloc_off;
82 if (entry->pre_unbind)
83 entry->pre_unbind += gd->reloc_off;
84 if (entry->post_probe)
85 entry->post_probe += gd->reloc_off;
86 if (entry->pre_remove)
87 entry->pre_remove += gd->reloc_off;
88 if (entry->init)
89 entry->init += gd->reloc_off;
90 if (entry->destroy)
91 entry->destroy += gd->reloc_off;
92 /* FIXME maybe also need to fix these ops */
93 if (entry->ops)
94 entry->ops += gd->reloc_off;
95 }
96}
97#endif
98
Simon Glassdd6ab882014-02-26 15:59:18 -070099int dm_init(void)
100{
101 int ret;
102
103 if (gd->dm_root) {
104 dm_warn("Virtual root driver already exists!\n");
105 return -EINVAL;
106 }
Simon Glass34a1d352014-06-11 23:29:49 -0600107 INIT_LIST_HEAD(&DM_UCLASS_ROOT_NON_CONST);
Simon Glassdd6ab882014-02-26 15:59:18 -0700108
Michal Simek0ec473b2015-02-02 16:31:59 +0100109#if defined(CONFIG_NEEDS_MANUAL_RELOC)
110 fix_drivers();
111 fix_uclass();
112#endif
113
Simon Glassfef72b72014-07-23 06:55:03 -0600114 ret = device_bind_by_name(NULL, false, &root_info, &DM_ROOT_NON_CONST);
Simon Glassdd6ab882014-02-26 15:59:18 -0700115 if (ret)
116 return ret;
Simon Glasscc7cf942015-01-25 08:26:58 -0700117#ifdef CONFIG_OF_CONTROL
118 DM_ROOT_NON_CONST->of_offset = 0;
119#endif
Simon Glass6d3b3e22014-07-23 06:55:00 -0600120 ret = device_probe(DM_ROOT_NON_CONST);
121 if (ret)
122 return ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700123
124 return 0;
125}
126
Simon Glass00197582014-07-23 06:55:01 -0600127int dm_uninit(void)
128{
129 device_remove(dm_root());
130 device_unbind(dm_root());
131
132 return 0;
133}
134
Simon Glassfef72b72014-07-23 06:55:03 -0600135int dm_scan_platdata(bool pre_reloc_only)
Simon Glassdd6ab882014-02-26 15:59:18 -0700136{
137 int ret;
138
Simon Glassfef72b72014-07-23 06:55:03 -0600139 ret = lists_bind_drivers(DM_ROOT_NON_CONST, pre_reloc_only);
Simon Glassdd6ab882014-02-26 15:59:18 -0700140 if (ret == -ENOENT) {
141 dm_warn("Some drivers were not found\n");
142 ret = 0;
143 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700144
Masahiro Yamada6cac81a2014-11-17 17:19:38 +0900145 return ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700146}
147
148#ifdef CONFIG_OF_CONTROL
Simon Glass40717422014-07-23 06:55:18 -0600149int dm_scan_fdt_node(struct udevice *parent, const void *blob, int offset,
150 bool pre_reloc_only)
Simon Glassdd6ab882014-02-26 15:59:18 -0700151{
Simon Glassdd6ab882014-02-26 15:59:18 -0700152 int ret = 0, err;
Simon Glassdd6ab882014-02-26 15:59:18 -0700153
Simon Glass40717422014-07-23 06:55:18 -0600154 for (offset = fdt_first_subnode(blob, offset);
155 offset > 0;
156 offset = fdt_next_subnode(blob, offset)) {
157 if (pre_reloc_only &&
158 !fdt_getprop(blob, offset, "u-boot,dm-pre-reloc", NULL))
159 continue;
Simon Glass528b1ef2015-01-25 08:27:16 -0700160 if (!fdtdec_get_is_enabled(blob, offset)) {
161 dm_dbg(" - ignoring disabled device\n");
162 continue;
163 }
Simon Glass439bc842014-09-04 16:27:25 -0600164 err = lists_bind_fdt(parent, blob, offset, NULL);
Simon Glass40717422014-07-23 06:55:18 -0600165 if (err && !ret)
166 ret = err;
167 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700168
169 if (ret)
170 dm_warn("Some drivers failed to bind\n");
171
172 return ret;
173}
Simon Glass40717422014-07-23 06:55:18 -0600174
175int dm_scan_fdt(const void *blob, bool pre_reloc_only)
176{
177 return dm_scan_fdt_node(gd->dm_root, blob, 0, pre_reloc_only);
178}
Simon Glassdd6ab882014-02-26 15:59:18 -0700179#endif
180
Simon Glass97e22e32014-07-23 06:55:23 -0600181__weak int dm_scan_other(bool pre_reloc_only)
182{
183 return 0;
184}
185
Simon Glassa730c5d2014-07-23 06:55:04 -0600186int dm_init_and_scan(bool pre_reloc_only)
187{
188 int ret;
189
190 ret = dm_init();
191 if (ret) {
192 debug("dm_init() failed: %d\n", ret);
193 return ret;
194 }
195 ret = dm_scan_platdata(pre_reloc_only);
196 if (ret) {
197 debug("dm_scan_platdata() failed: %d\n", ret);
198 return ret;
199 }
200#ifdef CONFIG_OF_CONTROL
201 ret = dm_scan_fdt(gd->fdt_blob, pre_reloc_only);
202 if (ret) {
203 debug("dm_scan_fdt() failed: %d\n", ret);
204 return ret;
205 }
206#endif
Simon Glass97e22e32014-07-23 06:55:23 -0600207 ret = dm_scan_other(pre_reloc_only);
208 if (ret)
209 return ret;
Simon Glassa730c5d2014-07-23 06:55:04 -0600210
211 return 0;
212}
213
Simon Glassdd6ab882014-02-26 15:59:18 -0700214/* This is the root driver - all drivers are children of this */
215U_BOOT_DRIVER(root_driver) = {
216 .name = "root_driver",
217 .id = UCLASS_ROOT,
218};
219
220/* This is the root uclass */
221UCLASS_DRIVER(root) = {
222 .name = "root",
223 .id = UCLASS_ROOT,
224};