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