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> |
Simon Glass | 528b1ef | 2015-01-25 08:27:16 -0700 | [diff] [blame] | 12 | #include <fdtdec.h> |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 13 | #include <malloc.h> |
Simon Glass | df6bd7d | 2014-06-11 23:29:48 -0600 | [diff] [blame] | 14 | #include <libfdt.h> |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 15 | #include <dm/device.h> |
| 16 | #include <dm/device-internal.h> |
| 17 | #include <dm/lists.h> |
| 18 | #include <dm/platdata.h> |
Jeroen Hofstee | cf0cf82 | 2014-06-25 21:57:45 +0200 | [diff] [blame] | 19 | #include <dm/root.h> |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 20 | #include <dm/uclass.h> |
| 21 | #include <dm/util.h> |
| 22 | #include <linux/list.h> |
| 23 | |
| 24 | DECLARE_GLOBAL_DATA_PTR; |
| 25 | |
| 26 | static const struct driver_info root_info = { |
| 27 | .name = "root_driver", |
| 28 | }; |
| 29 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 30 | struct udevice *dm_root(void) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 31 | { |
| 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 | |
Michal Simek | 0ec473b | 2015-02-02 16:31:59 +0100 | [diff] [blame] | 40 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
| 41 | void fix_drivers(void) |
| 42 | { |
| 43 | struct driver *drv = |
| 44 | ll_entry_start(struct driver, driver); |
| 45 | const int n_ents = ll_entry_count(struct driver, driver); |
| 46 | struct driver *entry; |
| 47 | |
| 48 | for (entry = drv; entry != drv + n_ents; entry++) { |
| 49 | if (entry->of_match) |
| 50 | entry->of_match = (const struct udevice_id *) |
| 51 | ((u32)entry->of_match + gd->reloc_off); |
| 52 | if (entry->bind) |
| 53 | entry->bind += gd->reloc_off; |
| 54 | if (entry->probe) |
| 55 | entry->probe += gd->reloc_off; |
| 56 | if (entry->remove) |
| 57 | entry->remove += gd->reloc_off; |
| 58 | if (entry->unbind) |
| 59 | entry->unbind += gd->reloc_off; |
| 60 | if (entry->ofdata_to_platdata) |
| 61 | entry->ofdata_to_platdata += gd->reloc_off; |
| 62 | if (entry->child_pre_probe) |
| 63 | entry->child_pre_probe += gd->reloc_off; |
| 64 | if (entry->child_post_remove) |
| 65 | entry->child_post_remove += gd->reloc_off; |
| 66 | /* OPS are fixed in every uclass post_probe function */ |
| 67 | if (entry->ops) |
| 68 | entry->ops += gd->reloc_off; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void fix_uclass(void) |
| 73 | { |
| 74 | struct uclass_driver *uclass = |
| 75 | ll_entry_start(struct uclass_driver, uclass); |
| 76 | const int n_ents = ll_entry_count(struct uclass_driver, uclass); |
| 77 | struct uclass_driver *entry; |
| 78 | |
| 79 | for (entry = uclass; entry != uclass + n_ents; entry++) { |
| 80 | if (entry->post_bind) |
| 81 | entry->post_bind += gd->reloc_off; |
| 82 | if (entry->pre_unbind) |
| 83 | entry->pre_unbind += gd->reloc_off; |
| 84 | if (entry->post_probe) |
| 85 | entry->post_probe += gd->reloc_off; |
| 86 | if (entry->pre_remove) |
| 87 | entry->pre_remove += gd->reloc_off; |
| 88 | if (entry->init) |
| 89 | entry->init += gd->reloc_off; |
| 90 | if (entry->destroy) |
| 91 | entry->destroy += gd->reloc_off; |
| 92 | /* FIXME maybe also need to fix these ops */ |
| 93 | if (entry->ops) |
| 94 | entry->ops += gd->reloc_off; |
| 95 | } |
| 96 | } |
| 97 | #endif |
| 98 | |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 99 | int dm_init(void) |
| 100 | { |
| 101 | int ret; |
| 102 | |
| 103 | if (gd->dm_root) { |
| 104 | dm_warn("Virtual root driver already exists!\n"); |
| 105 | return -EINVAL; |
| 106 | } |
Simon Glass | 34a1d35 | 2014-06-11 23:29:49 -0600 | [diff] [blame] | 107 | INIT_LIST_HEAD(&DM_UCLASS_ROOT_NON_CONST); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 108 | |
Michal Simek | 0ec473b | 2015-02-02 16:31:59 +0100 | [diff] [blame] | 109 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
| 110 | fix_drivers(); |
| 111 | fix_uclass(); |
| 112 | #endif |
| 113 | |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 114 | 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] | 115 | if (ret) |
| 116 | return ret; |
Masahiro Yamada | 366b24f | 2015-08-12 07:31:55 +0900 | [diff] [blame] | 117 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Simon Glass | cc7cf94 | 2015-01-25 08:26:58 -0700 | [diff] [blame] | 118 | DM_ROOT_NON_CONST->of_offset = 0; |
| 119 | #endif |
Simon Glass | 6d3b3e2 | 2014-07-23 06:55:00 -0600 | [diff] [blame] | 120 | ret = device_probe(DM_ROOT_NON_CONST); |
| 121 | if (ret) |
| 122 | return ret; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
Simon Glass | 0019758 | 2014-07-23 06:55:01 -0600 | [diff] [blame] | 127 | int dm_uninit(void) |
| 128 | { |
| 129 | device_remove(dm_root()); |
| 130 | device_unbind(dm_root()); |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 135 | int dm_scan_platdata(bool pre_reloc_only) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 136 | { |
| 137 | int ret; |
| 138 | |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 139 | ret = lists_bind_drivers(DM_ROOT_NON_CONST, pre_reloc_only); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 140 | if (ret == -ENOENT) { |
| 141 | dm_warn("Some drivers were not found\n"); |
| 142 | ret = 0; |
| 143 | } |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 144 | |
Masahiro Yamada | 6cac81a | 2014-11-17 17:19:38 +0900 | [diff] [blame] | 145 | return ret; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Masahiro Yamada | 366b24f | 2015-08-12 07:31:55 +0900 | [diff] [blame] | 148 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 149 | int dm_scan_fdt_node(struct udevice *parent, const void *blob, int offset, |
| 150 | bool pre_reloc_only) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 151 | { |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 152 | int ret = 0, err; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 153 | |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 154 | for (offset = fdt_first_subnode(blob, offset); |
| 155 | offset > 0; |
| 156 | offset = fdt_next_subnode(blob, offset)) { |
| 157 | if (pre_reloc_only && |
| 158 | !fdt_getprop(blob, offset, "u-boot,dm-pre-reloc", NULL)) |
| 159 | continue; |
Simon Glass | 528b1ef | 2015-01-25 08:27:16 -0700 | [diff] [blame] | 160 | if (!fdtdec_get_is_enabled(blob, offset)) { |
| 161 | dm_dbg(" - ignoring disabled device\n"); |
| 162 | continue; |
| 163 | } |
Simon Glass | 439bc84 | 2014-09-04 16:27:25 -0600 | [diff] [blame] | 164 | err = lists_bind_fdt(parent, blob, offset, NULL); |
Simon Glass | c90b917 | 2015-08-30 16:55:17 -0600 | [diff] [blame] | 165 | if (err && !ret) { |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 166 | ret = err; |
Simon Glass | c90b917 | 2015-08-30 16:55:17 -0600 | [diff] [blame] | 167 | debug("%s: ret=%d\n", fdt_get_name(blob, offset, NULL), |
| 168 | ret); |
| 169 | } |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 170 | } |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 171 | |
| 172 | if (ret) |
| 173 | dm_warn("Some drivers failed to bind\n"); |
| 174 | |
| 175 | return ret; |
| 176 | } |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 177 | |
| 178 | int dm_scan_fdt(const void *blob, bool pre_reloc_only) |
| 179 | { |
| 180 | return dm_scan_fdt_node(gd->dm_root, blob, 0, pre_reloc_only); |
| 181 | } |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 182 | #endif |
| 183 | |
Simon Glass | 97e22e3 | 2014-07-23 06:55:23 -0600 | [diff] [blame] | 184 | __weak int dm_scan_other(bool pre_reloc_only) |
| 185 | { |
| 186 | return 0; |
| 187 | } |
| 188 | |
Simon Glass | a730c5d | 2014-07-23 06:55:04 -0600 | [diff] [blame] | 189 | int dm_init_and_scan(bool pre_reloc_only) |
| 190 | { |
| 191 | int ret; |
| 192 | |
| 193 | ret = dm_init(); |
| 194 | if (ret) { |
| 195 | debug("dm_init() failed: %d\n", ret); |
| 196 | return ret; |
| 197 | } |
| 198 | ret = dm_scan_platdata(pre_reloc_only); |
| 199 | if (ret) { |
| 200 | debug("dm_scan_platdata() failed: %d\n", ret); |
| 201 | return ret; |
| 202 | } |
Simon Glass | 0704b85 | 2015-02-27 22:06:41 -0700 | [diff] [blame] | 203 | |
Masahiro Yamada | 366b24f | 2015-08-12 07:31:55 +0900 | [diff] [blame] | 204 | if (CONFIG_IS_ENABLED(OF_CONTROL)) { |
Simon Glass | 0704b85 | 2015-02-27 22:06:41 -0700 | [diff] [blame] | 205 | ret = dm_scan_fdt(gd->fdt_blob, pre_reloc_only); |
| 206 | if (ret) { |
| 207 | debug("dm_scan_fdt() failed: %d\n", ret); |
| 208 | return ret; |
| 209 | } |
Simon Glass | a730c5d | 2014-07-23 06:55:04 -0600 | [diff] [blame] | 210 | } |
Simon Glass | 0704b85 | 2015-02-27 22:06:41 -0700 | [diff] [blame] | 211 | |
Simon Glass | 97e22e3 | 2014-07-23 06:55:23 -0600 | [diff] [blame] | 212 | ret = dm_scan_other(pre_reloc_only); |
| 213 | if (ret) |
| 214 | return ret; |
Simon Glass | a730c5d | 2014-07-23 06:55:04 -0600 | [diff] [blame] | 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 219 | /* This is the root driver - all drivers are children of this */ |
| 220 | U_BOOT_DRIVER(root_driver) = { |
| 221 | .name = "root_driver", |
| 222 | .id = UCLASS_ROOT, |
| 223 | }; |
| 224 | |
| 225 | /* This is the root uclass */ |
| 226 | UCLASS_DRIVER(root) = { |
| 227 | .name = "root", |
| 228 | .id = UCLASS_ROOT, |
| 229 | }; |