blob: 11e08796b2b0416d7cb3f29e96bfaab91f179a0f [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>
12#include <malloc.h>
Simon Glassdf6bd7d2014-06-11 23:29:48 -060013#include <libfdt.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070014#include <dm/device.h>
15#include <dm/device-internal.h>
16#include <dm/lists.h>
17#include <dm/platdata.h>
Jeroen Hofsteecf0cf822014-06-25 21:57:45 +020018#include <dm/root.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070019#include <dm/uclass.h>
20#include <dm/util.h>
21#include <linux/list.h>
22
23DECLARE_GLOBAL_DATA_PTR;
24
25static const struct driver_info root_info = {
26 .name = "root_driver",
27};
28
Heiko Schocherb74fcb42014-05-22 12:43:05 +020029struct udevice *dm_root(void)
Simon Glassdd6ab882014-02-26 15:59:18 -070030{
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
39int 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 }
Simon Glass34a1d352014-06-11 23:29:49 -060047 INIT_LIST_HEAD(&DM_UCLASS_ROOT_NON_CONST);
Simon Glassdd6ab882014-02-26 15:59:18 -070048
Simon Glass34a1d352014-06-11 23:29:49 -060049 ret = device_bind_by_name(NULL, &root_info, &DM_ROOT_NON_CONST);
Simon Glassdd6ab882014-02-26 15:59:18 -070050 if (ret)
51 return ret;
52
53 return 0;
54}
55
56int dm_scan_platdata(void)
57{
58 int ret;
59
Simon Glass34a1d352014-06-11 23:29:49 -060060 ret = lists_bind_drivers(DM_ROOT_NON_CONST);
Simon Glassdd6ab882014-02-26 15:59:18 -070061 if (ret == -ENOENT) {
62 dm_warn("Some drivers were not found\n");
63 ret = 0;
64 }
65 if (ret)
66 return ret;
67
68 return 0;
69}
70
71#ifdef CONFIG_OF_CONTROL
72int dm_scan_fdt(const void *blob)
73{
74 int offset = 0;
75 int ret = 0, err;
76 int depth = 0;
77
78 do {
79 offset = fdt_next_node(blob, offset, &depth);
80 if (offset > 0 && depth == 1) {
81 err = lists_bind_fdt(gd->dm_root, blob, offset);
82 if (err && !ret)
83 ret = err;
84 }
85 } while (offset > 0);
86
87 if (ret)
88 dm_warn("Some drivers failed to bind\n");
89
90 return ret;
91}
92#endif
93
94/* This is the root driver - all drivers are children of this */
95U_BOOT_DRIVER(root_driver) = {
96 .name = "root_driver",
97 .id = UCLASS_ROOT,
98};
99
100/* This is the root uclass */
101UCLASS_DRIVER(root) = {
102 .name = "root",
103 .id = UCLASS_ROOT,
104};