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