blob: 73e3c7228e300e67da317098160d9c9c988446b3 [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
26static const struct driver_info root_info = {
27 .name = "root_driver",
28};
29
Heiko Schocher54c5d082014-05-22 12:43:05 +020030struct udevice *dm_root(void)
Simon Glass6494d702014-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
40int dm_init(void)
41{
42 int ret;
43
44 if (gd->dm_root) {
45 dm_warn("Virtual root driver already exists!\n");
46 return -EINVAL;
47 }
Simon Glass89876a52014-06-11 23:29:49 -060048 INIT_LIST_HEAD(&DM_UCLASS_ROOT_NON_CONST);
Simon Glass6494d702014-02-26 15:59:18 -070049
Simon Glass00606d72014-07-23 06:55:03 -060050 ret = device_bind_by_name(NULL, false, &root_info, &DM_ROOT_NON_CONST);
Simon Glass6494d702014-02-26 15:59:18 -070051 if (ret)
52 return ret;
Simon Glass2f3b95d2015-01-25 08:26:58 -070053#ifdef CONFIG_OF_CONTROL
54 DM_ROOT_NON_CONST->of_offset = 0;
55#endif
Simon Glass74978122014-07-23 06:55:00 -060056 ret = device_probe(DM_ROOT_NON_CONST);
57 if (ret)
58 return ret;
Simon Glass6494d702014-02-26 15:59:18 -070059
60 return 0;
61}
62
Simon Glass9adbd7a2014-07-23 06:55:01 -060063int dm_uninit(void)
64{
65 device_remove(dm_root());
66 device_unbind(dm_root());
67
68 return 0;
69}
70
Simon Glass00606d72014-07-23 06:55:03 -060071int dm_scan_platdata(bool pre_reloc_only)
Simon Glass6494d702014-02-26 15:59:18 -070072{
73 int ret;
74
Simon Glass00606d72014-07-23 06:55:03 -060075 ret = lists_bind_drivers(DM_ROOT_NON_CONST, pre_reloc_only);
Simon Glass6494d702014-02-26 15:59:18 -070076 if (ret == -ENOENT) {
77 dm_warn("Some drivers were not found\n");
78 ret = 0;
79 }
Simon Glass6494d702014-02-26 15:59:18 -070080
Masahiro Yamadacbf86d72014-11-17 17:19:38 +090081 return ret;
Simon Glass6494d702014-02-26 15:59:18 -070082}
83
84#ifdef CONFIG_OF_CONTROL
Simon Glass1ca7e202014-07-23 06:55:18 -060085int dm_scan_fdt_node(struct udevice *parent, const void *blob, int offset,
86 bool pre_reloc_only)
Simon Glass6494d702014-02-26 15:59:18 -070087{
Simon Glass6494d702014-02-26 15:59:18 -070088 int ret = 0, err;
Simon Glass6494d702014-02-26 15:59:18 -070089
Simon Glass1ca7e202014-07-23 06:55:18 -060090 for (offset = fdt_first_subnode(blob, offset);
91 offset > 0;
92 offset = fdt_next_subnode(blob, offset)) {
93 if (pre_reloc_only &&
94 !fdt_getprop(blob, offset, "u-boot,dm-pre-reloc", NULL))
95 continue;
Simon Glass94f7afd2015-01-25 08:27:16 -070096 if (!fdtdec_get_is_enabled(blob, offset)) {
97 dm_dbg(" - ignoring disabled device\n");
98 continue;
99 }
Simon Glass1f359e32014-09-04 16:27:25 -0600100 err = lists_bind_fdt(parent, blob, offset, NULL);
Simon Glass1ca7e202014-07-23 06:55:18 -0600101 if (err && !ret)
102 ret = err;
103 }
Simon Glass6494d702014-02-26 15:59:18 -0700104
105 if (ret)
106 dm_warn("Some drivers failed to bind\n");
107
108 return ret;
109}
Simon Glass1ca7e202014-07-23 06:55:18 -0600110
111int dm_scan_fdt(const void *blob, bool pre_reloc_only)
112{
113 return dm_scan_fdt_node(gd->dm_root, blob, 0, pre_reloc_only);
114}
Simon Glass6494d702014-02-26 15:59:18 -0700115#endif
116
Simon Glassbb585032014-07-23 06:55:23 -0600117__weak int dm_scan_other(bool pre_reloc_only)
118{
119 return 0;
120}
121
Simon Glassab7cd622014-07-23 06:55:04 -0600122int dm_init_and_scan(bool pre_reloc_only)
123{
124 int ret;
125
126 ret = dm_init();
127 if (ret) {
128 debug("dm_init() failed: %d\n", ret);
129 return ret;
130 }
131 ret = dm_scan_platdata(pre_reloc_only);
132 if (ret) {
133 debug("dm_scan_platdata() failed: %d\n", ret);
134 return ret;
135 }
136#ifdef CONFIG_OF_CONTROL
137 ret = dm_scan_fdt(gd->fdt_blob, pre_reloc_only);
138 if (ret) {
139 debug("dm_scan_fdt() failed: %d\n", ret);
140 return ret;
141 }
142#endif
Simon Glassbb585032014-07-23 06:55:23 -0600143 ret = dm_scan_other(pre_reloc_only);
144 if (ret)
145 return ret;
Simon Glassab7cd622014-07-23 06:55:04 -0600146
147 return 0;
148}
149
Simon Glass6494d702014-02-26 15:59:18 -0700150/* This is the root driver - all drivers are children of this */
151U_BOOT_DRIVER(root_driver) = {
152 .name = "root_driver",
153 .id = UCLASS_ROOT,
154};
155
156/* This is the root uclass */
157UCLASS_DRIVER(root) = {
158 .name = "root",
159 .id = UCLASS_ROOT,
160};