]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/core/root.c
dm: Support driver model prior to relocation
[karo-tx-uboot.git] / drivers / core / root.c
1 /*
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>
12 #include <malloc.h>
13 #include <libfdt.h>
14 #include <dm/device.h>
15 #include <dm/device-internal.h>
16 #include <dm/lists.h>
17 #include <dm/platdata.h>
18 #include <dm/root.h>
19 #include <dm/uclass.h>
20 #include <dm/util.h>
21 #include <linux/list.h>
22
23 DECLARE_GLOBAL_DATA_PTR;
24
25 static const struct driver_info root_info = {
26         .name           = "root_driver",
27 };
28
29 struct udevice *dm_root(void)
30 {
31         if (!gd->dm_root) {
32                 dm_warn("Virtual root driver does not exist!\n");
33                 return NULL;
34         }
35
36         return gd->dm_root;
37 }
38
39 int dm_init(void)
40 {
41         int ret;
42
43         if (gd->dm_root) {
44                 dm_warn("Virtual root driver already exists!\n");
45                 return -EINVAL;
46         }
47         INIT_LIST_HEAD(&DM_UCLASS_ROOT_NON_CONST);
48
49         ret = device_bind_by_name(NULL, false, &root_info, &DM_ROOT_NON_CONST);
50         if (ret)
51                 return ret;
52         ret = device_probe(DM_ROOT_NON_CONST);
53         if (ret)
54                 return ret;
55
56         return 0;
57 }
58
59 int dm_uninit(void)
60 {
61         device_remove(dm_root());
62         device_unbind(dm_root());
63
64         return 0;
65 }
66
67 int dm_scan_platdata(bool pre_reloc_only)
68 {
69         int ret;
70
71         ret = lists_bind_drivers(DM_ROOT_NON_CONST, pre_reloc_only);
72         if (ret == -ENOENT) {
73                 dm_warn("Some drivers were not found\n");
74                 ret = 0;
75         }
76         if (ret)
77                 return ret;
78
79         return 0;
80 }
81
82 #ifdef CONFIG_OF_CONTROL
83 int dm_scan_fdt(const void *blob, bool pre_reloc_only)
84 {
85         int offset = 0;
86         int ret = 0, err;
87         int depth = 0;
88
89         do {
90                 offset = fdt_next_node(blob, offset, &depth);
91                 if (offset > 0 && depth == 1) {
92                         if (pre_reloc_only &&
93                             !fdt_getprop(blob, offset, "u-boot,dm-pre-reloc", NULL))
94                                 continue;
95                         err = lists_bind_fdt(gd->dm_root, blob, offset);
96                         if (err && !ret)
97                                 ret = err;
98                 }
99         } while (offset > 0);
100
101         if (ret)
102                 dm_warn("Some drivers failed to bind\n");
103
104         return ret;
105 }
106 #endif
107
108 int dm_init_and_scan(bool pre_reloc_only)
109 {
110         int ret;
111
112         ret = dm_init();
113         if (ret) {
114                 debug("dm_init() failed: %d\n", ret);
115                 return ret;
116         }
117         ret = dm_scan_platdata(pre_reloc_only);
118         if (ret) {
119                 debug("dm_scan_platdata() failed: %d\n", ret);
120                 return ret;
121         }
122 #ifdef CONFIG_OF_CONTROL
123         ret = dm_scan_fdt(gd->fdt_blob, pre_reloc_only);
124         if (ret) {
125                 debug("dm_scan_fdt() failed: %d\n", ret);
126                 return ret;
127         }
128 #endif
129
130         return 0;
131 }
132
133 /* This is the root driver - all drivers are children of this */
134 U_BOOT_DRIVER(root_driver) = {
135         .name   = "root_driver",
136         .id     = UCLASS_ROOT,
137 };
138
139 /* This is the root uclass */
140 UCLASS_DRIVER(root) = {
141         .name   = "root",
142         .id     = UCLASS_ROOT,
143 };