Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 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> |
Simon Glass | df6bd7d | 2014-06-11 23:29:48 -0600 | [diff] [blame] | 13 | #include <libfdt.h> |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 14 | #include <dm/device.h> |
| 15 | #include <dm/device-internal.h> |
| 16 | #include <dm/lists.h> |
| 17 | #include <dm/platdata.h> |
Jeroen Hofstee | cf0cf82 | 2014-06-25 21:57:45 +0200 | [diff] [blame] | 18 | #include <dm/root.h> |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 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 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 29 | struct udevice *dm_root(void) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 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 | } |
Simon Glass | 34a1d35 | 2014-06-11 23:29:49 -0600 | [diff] [blame] | 47 | INIT_LIST_HEAD(&DM_UCLASS_ROOT_NON_CONST); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 48 | |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 49 | ret = device_bind_by_name(NULL, false, &root_info, &DM_ROOT_NON_CONST); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 50 | if (ret) |
| 51 | return ret; |
Simon Glass | 6d3b3e2 | 2014-07-23 06:55:00 -0600 | [diff] [blame] | 52 | ret = device_probe(DM_ROOT_NON_CONST); |
| 53 | if (ret) |
| 54 | return ret; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
Simon Glass | 0019758 | 2014-07-23 06:55:01 -0600 | [diff] [blame] | 59 | int dm_uninit(void) |
| 60 | { |
| 61 | device_remove(dm_root()); |
| 62 | device_unbind(dm_root()); |
| 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 67 | int dm_scan_platdata(bool pre_reloc_only) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 68 | { |
| 69 | int ret; |
| 70 | |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 71 | ret = lists_bind_drivers(DM_ROOT_NON_CONST, pre_reloc_only); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 72 | if (ret == -ENOENT) { |
| 73 | dm_warn("Some drivers were not found\n"); |
| 74 | ret = 0; |
| 75 | } |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 76 | |
Masahiro Yamada | 6cac81a | 2014-11-17 17:19:38 +0900 | [diff] [blame] | 77 | return ret; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | #ifdef CONFIG_OF_CONTROL |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 81 | int dm_scan_fdt_node(struct udevice *parent, const void *blob, int offset, |
| 82 | bool pre_reloc_only) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 83 | { |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 84 | int ret = 0, err; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 85 | |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 86 | for (offset = fdt_first_subnode(blob, offset); |
| 87 | offset > 0; |
| 88 | offset = fdt_next_subnode(blob, offset)) { |
| 89 | if (pre_reloc_only && |
| 90 | !fdt_getprop(blob, offset, "u-boot,dm-pre-reloc", NULL)) |
| 91 | continue; |
Simon Glass | 439bc84 | 2014-09-04 16:27:25 -0600 | [diff] [blame] | 92 | err = lists_bind_fdt(parent, blob, offset, NULL); |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 93 | if (err && !ret) |
| 94 | ret = err; |
| 95 | } |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 96 | |
| 97 | if (ret) |
| 98 | dm_warn("Some drivers failed to bind\n"); |
| 99 | |
| 100 | return ret; |
| 101 | } |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 102 | |
| 103 | int dm_scan_fdt(const void *blob, bool pre_reloc_only) |
| 104 | { |
| 105 | return dm_scan_fdt_node(gd->dm_root, blob, 0, pre_reloc_only); |
| 106 | } |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 107 | #endif |
| 108 | |
Simon Glass | 97e22e3 | 2014-07-23 06:55:23 -0600 | [diff] [blame] | 109 | __weak int dm_scan_other(bool pre_reloc_only) |
| 110 | { |
| 111 | return 0; |
| 112 | } |
| 113 | |
Simon Glass | a730c5d | 2014-07-23 06:55:04 -0600 | [diff] [blame] | 114 | int dm_init_and_scan(bool pre_reloc_only) |
| 115 | { |
| 116 | int ret; |
| 117 | |
| 118 | ret = dm_init(); |
| 119 | if (ret) { |
| 120 | debug("dm_init() failed: %d\n", ret); |
| 121 | return ret; |
| 122 | } |
| 123 | ret = dm_scan_platdata(pre_reloc_only); |
| 124 | if (ret) { |
| 125 | debug("dm_scan_platdata() failed: %d\n", ret); |
| 126 | return ret; |
| 127 | } |
| 128 | #ifdef CONFIG_OF_CONTROL |
| 129 | ret = dm_scan_fdt(gd->fdt_blob, pre_reloc_only); |
| 130 | if (ret) { |
| 131 | debug("dm_scan_fdt() failed: %d\n", ret); |
| 132 | return ret; |
| 133 | } |
| 134 | #endif |
Simon Glass | 97e22e3 | 2014-07-23 06:55:23 -0600 | [diff] [blame] | 135 | ret = dm_scan_other(pre_reloc_only); |
| 136 | if (ret) |
| 137 | return ret; |
Simon Glass | a730c5d | 2014-07-23 06:55:04 -0600 | [diff] [blame] | 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 142 | /* This is the root driver - all drivers are children of this */ |
| 143 | U_BOOT_DRIVER(root_driver) = { |
| 144 | .name = "root_driver", |
| 145 | .id = UCLASS_ROOT, |
| 146 | }; |
| 147 | |
| 148 | /* This is the root uclass */ |
| 149 | UCLASS_DRIVER(root) = { |
| 150 | .name = "root", |
| 151 | .id = UCLASS_ROOT, |
| 152 | }; |