blob: 0c00a4e051e328fe302f5b0051a6f9b39bf10ba7 [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 */
Lokesh Vutla02ba17f2017-02-13 09:21:22 +053047 if (gd->dm_root) {
48 new_gd->uclass_root.next->prev = &new_gd->uclass_root;
49 new_gd->uclass_root.prev->next = &new_gd->uclass_root;
50 }
Simon Glass1b0deee2016-11-13 14:21:58 -070051}
52
Stefan Roesea0ea0f92015-12-14 16:18:15 +010053fdt_addr_t dm_get_translation_offset(void)
54{
55 struct udevice *root = dm_root();
56 struct root_priv *priv = dev_get_priv(root);
57
58 return priv->translation_offset;
59}
60
61void dm_set_translation_offset(fdt_addr_t offs)
62{
63 struct udevice *root = dm_root();
64 struct root_priv *priv = dev_get_priv(root);
65
66 priv->translation_offset = offs;
67}
68
Michal Simek0ec473b2015-02-02 16:31:59 +010069#if defined(CONFIG_NEEDS_MANUAL_RELOC)
70void fix_drivers(void)
71{
72 struct driver *drv =
73 ll_entry_start(struct driver, driver);
74 const int n_ents = ll_entry_count(struct driver, driver);
75 struct driver *entry;
76
77 for (entry = drv; entry != drv + n_ents; entry++) {
78 if (entry->of_match)
79 entry->of_match = (const struct udevice_id *)
80 ((u32)entry->of_match + gd->reloc_off);
81 if (entry->bind)
82 entry->bind += gd->reloc_off;
83 if (entry->probe)
84 entry->probe += gd->reloc_off;
85 if (entry->remove)
86 entry->remove += gd->reloc_off;
87 if (entry->unbind)
88 entry->unbind += gd->reloc_off;
89 if (entry->ofdata_to_platdata)
90 entry->ofdata_to_platdata += gd->reloc_off;
Michal Simek61809f52015-10-27 13:48:08 +010091 if (entry->child_post_bind)
92 entry->child_post_bind += gd->reloc_off;
Michal Simek0ec473b2015-02-02 16:31:59 +010093 if (entry->child_pre_probe)
94 entry->child_pre_probe += gd->reloc_off;
95 if (entry->child_post_remove)
96 entry->child_post_remove += gd->reloc_off;
97 /* OPS are fixed in every uclass post_probe function */
98 if (entry->ops)
99 entry->ops += gd->reloc_off;
100 }
101}
102
103void fix_uclass(void)
104{
105 struct uclass_driver *uclass =
106 ll_entry_start(struct uclass_driver, uclass);
107 const int n_ents = ll_entry_count(struct uclass_driver, uclass);
108 struct uclass_driver *entry;
109
110 for (entry = uclass; entry != uclass + n_ents; entry++) {
111 if (entry->post_bind)
112 entry->post_bind += gd->reloc_off;
113 if (entry->pre_unbind)
114 entry->pre_unbind += gd->reloc_off;
Michal Simek61809f52015-10-27 13:48:08 +0100115 if (entry->pre_probe)
116 entry->pre_probe += gd->reloc_off;
Michal Simek0ec473b2015-02-02 16:31:59 +0100117 if (entry->post_probe)
118 entry->post_probe += gd->reloc_off;
119 if (entry->pre_remove)
120 entry->pre_remove += gd->reloc_off;
Michal Simek61809f52015-10-27 13:48:08 +0100121 if (entry->child_post_bind)
122 entry->child_post_bind += gd->reloc_off;
123 if (entry->child_pre_probe)
124 entry->child_pre_probe += gd->reloc_off;
Michal Simek0ec473b2015-02-02 16:31:59 +0100125 if (entry->init)
126 entry->init += gd->reloc_off;
127 if (entry->destroy)
128 entry->destroy += gd->reloc_off;
129 /* FIXME maybe also need to fix these ops */
130 if (entry->ops)
131 entry->ops += gd->reloc_off;
132 }
133}
Angelo Dureghellof4d193b2016-05-21 12:05:49 +0200134
135void fix_devices(void)
136{
137 struct driver_info *dev =
138 ll_entry_start(struct driver_info, driver_info);
139 const int n_ents = ll_entry_count(struct driver_info, driver_info);
140 struct driver_info *entry;
141
142 for (entry = dev; entry != dev + n_ents; entry++) {
143 if (entry->platdata)
144 entry->platdata += gd->reloc_off;
145 }
146}
147
Michal Simek0ec473b2015-02-02 16:31:59 +0100148#endif
149
Simon Glassdd6ab882014-02-26 15:59:18 -0700150int dm_init(void)
151{
152 int ret;
153
154 if (gd->dm_root) {
155 dm_warn("Virtual root driver already exists!\n");
156 return -EINVAL;
157 }
Simon Glass34a1d352014-06-11 23:29:49 -0600158 INIT_LIST_HEAD(&DM_UCLASS_ROOT_NON_CONST);
Simon Glassdd6ab882014-02-26 15:59:18 -0700159
Michal Simek0ec473b2015-02-02 16:31:59 +0100160#if defined(CONFIG_NEEDS_MANUAL_RELOC)
161 fix_drivers();
162 fix_uclass();
Angelo Dureghellof4d193b2016-05-21 12:05:49 +0200163 fix_devices();
Michal Simek0ec473b2015-02-02 16:31:59 +0100164#endif
165
Simon Glassfef72b72014-07-23 06:55:03 -0600166 ret = device_bind_by_name(NULL, false, &root_info, &DM_ROOT_NON_CONST);
Simon Glassdd6ab882014-02-26 15:59:18 -0700167 if (ret)
168 return ret;
Masahiro Yamada366b24f2015-08-12 07:31:55 +0900169#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glass9a148602017-05-17 17:18:10 -0600170 DM_ROOT_NON_CONST->node = offset_to_ofnode(0);
Simon Glasscc7cf942015-01-25 08:26:58 -0700171#endif
Simon Glass6d3b3e22014-07-23 06:55:00 -0600172 ret = device_probe(DM_ROOT_NON_CONST);
173 if (ret)
174 return ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700175
176 return 0;
177}
178
Simon Glass00197582014-07-23 06:55:01 -0600179int dm_uninit(void)
180{
Stefan Roese80b5bc92017-03-20 12:51:48 +0100181 device_remove(dm_root(), DM_REMOVE_NORMAL);
Simon Glass00197582014-07-23 06:55:01 -0600182 device_unbind(dm_root());
183
184 return 0;
185}
186
Stefan Roese505045d2017-03-27 10:58:53 +0200187#if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)
188int dm_remove_devices_flags(uint flags)
189{
190 device_remove(dm_root(), flags);
191
192 return 0;
193}
194#endif
195
Simon Glassfef72b72014-07-23 06:55:03 -0600196int dm_scan_platdata(bool pre_reloc_only)
Simon Glassdd6ab882014-02-26 15:59:18 -0700197{
198 int ret;
199
Simon Glassfef72b72014-07-23 06:55:03 -0600200 ret = lists_bind_drivers(DM_ROOT_NON_CONST, pre_reloc_only);
Simon Glassdd6ab882014-02-26 15:59:18 -0700201 if (ret == -ENOENT) {
202 dm_warn("Some drivers were not found\n");
203 ret = 0;
204 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700205
Masahiro Yamada6cac81a2014-11-17 17:19:38 +0900206 return ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700207}
208
Simon Glass8d7e8162016-07-04 11:57:58 -0600209#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glassa4dbe252017-05-17 17:18:08 -0600210/**
211 * dm_scan_fdt_node() - Scan the device tree and bind drivers for a node
212 *
213 * This scans the subnodes of a device tree node and and creates a driver
214 * for each one.
215 *
216 * @parent: Parent device for the devices that will be created
217 * @blob: Pointer to device tree blob
218 * @offset: Offset of node to scan
219 * @pre_reloc_only: If true, bind only drivers with the DM_FLAG_PRE_RELOC
220 * flag. If false bind all drivers.
221 * @return 0 if OK, -ve on error
222 */
223static int dm_scan_fdt_node(struct udevice *parent, const void *blob,
224 int offset, bool pre_reloc_only)
Simon Glassdd6ab882014-02-26 15:59:18 -0700225{
Simon Glassdd6ab882014-02-26 15:59:18 -0700226 int ret = 0, err;
Simon Glassdd6ab882014-02-26 15:59:18 -0700227
Simon Glass40717422014-07-23 06:55:18 -0600228 for (offset = fdt_first_subnode(blob, offset);
229 offset > 0;
230 offset = fdt_next_subnode(blob, offset)) {
231 if (pre_reloc_only &&
Heiko Stübner9a2cdca2017-02-18 19:46:21 +0100232 !dm_fdt_pre_reloc(blob, offset))
Simon Glass40717422014-07-23 06:55:18 -0600233 continue;
Simon Glass528b1ef2015-01-25 08:27:16 -0700234 if (!fdtdec_get_is_enabled(blob, offset)) {
235 dm_dbg(" - ignoring disabled device\n");
236 continue;
237 }
Simon Glass802b2c62017-05-18 20:09:06 -0600238 err = lists_bind_fdt(parent, offset_to_ofnode(offset), NULL);
Simon Glassc90b9172015-08-30 16:55:17 -0600239 if (err && !ret) {
Simon Glass40717422014-07-23 06:55:18 -0600240 ret = err;
Simon Glassc90b9172015-08-30 16:55:17 -0600241 debug("%s: ret=%d\n", fdt_get_name(blob, offset, NULL),
242 ret);
243 }
Simon Glass40717422014-07-23 06:55:18 -0600244 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700245
246 if (ret)
247 dm_warn("Some drivers failed to bind\n");
248
249 return ret;
250}
Simon Glass40717422014-07-23 06:55:18 -0600251
Simon Glass5d5388d2016-07-05 17:10:08 -0600252int dm_scan_fdt_dev(struct udevice *dev)
253{
Simon Glassdd79d6e2017-01-17 16:52:55 -0700254 if (dev_of_offset(dev) == -1)
Simon Glass5d5388d2016-07-05 17:10:08 -0600255 return 0;
256
Simon Glassdd79d6e2017-01-17 16:52:55 -0700257 return dm_scan_fdt_node(dev, gd->fdt_blob, dev_of_offset(dev),
Simon Glass5d5388d2016-07-05 17:10:08 -0600258 gd->flags & GD_FLG_RELOC ? false : true);
259}
260
Simon Glass40717422014-07-23 06:55:18 -0600261int dm_scan_fdt(const void *blob, bool pre_reloc_only)
262{
263 return dm_scan_fdt_node(gd->dm_root, blob, 0, pre_reloc_only);
264}
Simon Glassdd6ab882014-02-26 15:59:18 -0700265#endif
266
Simon Glass97e22e32014-07-23 06:55:23 -0600267__weak int dm_scan_other(bool pre_reloc_only)
268{
269 return 0;
270}
271
Simon Glassa730c5d2014-07-23 06:55:04 -0600272int dm_init_and_scan(bool pre_reloc_only)
273{
274 int ret;
275
276 ret = dm_init();
277 if (ret) {
278 debug("dm_init() failed: %d\n", ret);
279 return ret;
280 }
281 ret = dm_scan_platdata(pre_reloc_only);
282 if (ret) {
283 debug("dm_scan_platdata() failed: %d\n", ret);
284 return ret;
285 }
Simon Glass0704b852015-02-27 22:06:41 -0700286
Simon Glass8d7e8162016-07-04 11:57:58 -0600287 if (CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)) {
Simon Glass0704b852015-02-27 22:06:41 -0700288 ret = dm_scan_fdt(gd->fdt_blob, pre_reloc_only);
289 if (ret) {
290 debug("dm_scan_fdt() failed: %d\n", ret);
291 return ret;
292 }
Simon Glassa730c5d2014-07-23 06:55:04 -0600293 }
Simon Glass0704b852015-02-27 22:06:41 -0700294
Simon Glass97e22e32014-07-23 06:55:23 -0600295 ret = dm_scan_other(pre_reloc_only);
296 if (ret)
297 return ret;
Simon Glassa730c5d2014-07-23 06:55:04 -0600298
299 return 0;
300}
301
Simon Glassdd6ab882014-02-26 15:59:18 -0700302/* This is the root driver - all drivers are children of this */
303U_BOOT_DRIVER(root_driver) = {
304 .name = "root_driver",
305 .id = UCLASS_ROOT,
Stefan Roesea0ea0f92015-12-14 16:18:15 +0100306 .priv_auto_alloc_size = sizeof(struct root_priv),
Simon Glassdd6ab882014-02-26 15:59:18 -0700307};
308
309/* This is the root uclass */
310UCLASS_DRIVER(root) = {
311 .name = "root",
312 .id = UCLASS_ROOT,
313};