Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013 Google, Inc |
| 4 | * |
| 5 | * (C) Copyright 2012 |
| 6 | * Pavel Herrmann <morpheus.ibis@gmail.com> |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
Patrick Delaunay | 8131335 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 9 | #define LOG_CATEGORY UCLASS_ROOT |
| 10 | |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 11 | #include <common.h> |
| 12 | #include <errno.h> |
Simon Glass | 528b1ef | 2015-01-25 08:27:16 -0700 | [diff] [blame] | 13 | #include <fdtdec.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 15 | #include <malloc.h> |
Simon Glass | 8dee67a | 2021-03-15 17:25:38 +1300 | [diff] [blame] | 16 | #include <asm-generic/sections.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 17 | #include <asm/global_data.h> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 18 | #include <linux/libfdt.h> |
Simon Glass | 719ea08 | 2020-07-07 13:11:38 -0600 | [diff] [blame] | 19 | #include <dm/acpi.h> |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 20 | #include <dm/device.h> |
| 21 | #include <dm/device-internal.h> |
| 22 | #include <dm/lists.h> |
Simon Glass | f19d9f2 | 2017-05-18 20:09:08 -0600 | [diff] [blame] | 23 | #include <dm/of.h> |
| 24 | #include <dm/of_access.h> |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 25 | #include <dm/platdata.h> |
Simon Glass | f19d9f2 | 2017-05-18 20:09:08 -0600 | [diff] [blame] | 26 | #include <dm/read.h> |
Jeroen Hofstee | cf0cf82 | 2014-06-25 21:57:45 +0200 | [diff] [blame] | 27 | #include <dm/root.h> |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 28 | #include <dm/uclass.h> |
Simon Glass | 51608c9 | 2021-12-16 20:59:32 -0700 | [diff] [blame] | 29 | #include <dm/uclass-internal.h> |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 30 | #include <dm/util.h> |
| 31 | #include <linux/list.h> |
| 32 | |
| 33 | DECLARE_GLOBAL_DATA_PTR; |
| 34 | |
Walter Lozano | 95282d6 | 2020-06-25 01:10:10 -0300 | [diff] [blame] | 35 | static struct driver_info root_info = { |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 36 | .name = "root_driver", |
| 37 | }; |
| 38 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 39 | struct udevice *dm_root(void) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 40 | { |
| 41 | if (!gd->dm_root) { |
| 42 | dm_warn("Virtual root driver does not exist!\n"); |
| 43 | return NULL; |
| 44 | } |
| 45 | |
| 46 | return gd->dm_root; |
| 47 | } |
| 48 | |
Simon Glass | 1b0deee | 2016-11-13 14:21:58 -0700 | [diff] [blame] | 49 | void dm_fixup_for_gd_move(struct global_data *new_gd) |
| 50 | { |
| 51 | /* The sentinel node has moved, so update things that point to it */ |
Lokesh Vutla | 02ba17f | 2017-02-13 09:21:22 +0530 | [diff] [blame] | 52 | if (gd->dm_root) { |
Simon Glass | 784cd9e | 2020-12-19 10:40:17 -0700 | [diff] [blame] | 53 | new_gd->uclass_root->next->prev = new_gd->uclass_root; |
| 54 | new_gd->uclass_root->prev->next = new_gd->uclass_root; |
Lokesh Vutla | 02ba17f | 2017-02-13 09:21:22 +0530 | [diff] [blame] | 55 | } |
Simon Glass | 1b0deee | 2016-11-13 14:21:58 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Michal Simek | 0ec473b | 2015-02-02 16:31:59 +0100 | [diff] [blame] | 58 | void fix_drivers(void) |
| 59 | { |
| 60 | struct driver *drv = |
| 61 | ll_entry_start(struct driver, driver); |
| 62 | const int n_ents = ll_entry_count(struct driver, driver); |
| 63 | struct driver *entry; |
| 64 | |
| 65 | for (entry = drv; entry != drv + n_ents; entry++) { |
| 66 | if (entry->of_match) |
| 67 | entry->of_match = (const struct udevice_id *) |
Simon Glass | 40916e6 | 2020-10-03 09:25:22 -0600 | [diff] [blame] | 68 | ((ulong)entry->of_match + gd->reloc_off); |
Michal Simek | 0ec473b | 2015-02-02 16:31:59 +0100 | [diff] [blame] | 69 | if (entry->bind) |
| 70 | entry->bind += gd->reloc_off; |
| 71 | if (entry->probe) |
| 72 | entry->probe += gd->reloc_off; |
| 73 | if (entry->remove) |
| 74 | entry->remove += gd->reloc_off; |
| 75 | if (entry->unbind) |
| 76 | entry->unbind += gd->reloc_off; |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 77 | if (entry->of_to_plat) |
| 78 | entry->of_to_plat += gd->reloc_off; |
Michal Simek | 61809f5 | 2015-10-27 13:48:08 +0100 | [diff] [blame] | 79 | if (entry->child_post_bind) |
| 80 | entry->child_post_bind += gd->reloc_off; |
Michal Simek | 0ec473b | 2015-02-02 16:31:59 +0100 | [diff] [blame] | 81 | if (entry->child_pre_probe) |
| 82 | entry->child_pre_probe += gd->reloc_off; |
| 83 | if (entry->child_post_remove) |
| 84 | entry->child_post_remove += gd->reloc_off; |
| 85 | /* OPS are fixed in every uclass post_probe function */ |
| 86 | if (entry->ops) |
| 87 | entry->ops += gd->reloc_off; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void fix_uclass(void) |
| 92 | { |
| 93 | struct uclass_driver *uclass = |
Simon Glass | 69ea20e | 2020-12-22 19:30:23 -0700 | [diff] [blame] | 94 | ll_entry_start(struct uclass_driver, uclass_driver); |
| 95 | const int n_ents = ll_entry_count(struct uclass_driver, uclass_driver); |
Michal Simek | 0ec473b | 2015-02-02 16:31:59 +0100 | [diff] [blame] | 96 | struct uclass_driver *entry; |
| 97 | |
| 98 | for (entry = uclass; entry != uclass + n_ents; entry++) { |
| 99 | if (entry->post_bind) |
| 100 | entry->post_bind += gd->reloc_off; |
| 101 | if (entry->pre_unbind) |
| 102 | entry->pre_unbind += gd->reloc_off; |
Michal Simek | 61809f5 | 2015-10-27 13:48:08 +0100 | [diff] [blame] | 103 | if (entry->pre_probe) |
| 104 | entry->pre_probe += gd->reloc_off; |
Michal Simek | 0ec473b | 2015-02-02 16:31:59 +0100 | [diff] [blame] | 105 | if (entry->post_probe) |
| 106 | entry->post_probe += gd->reloc_off; |
| 107 | if (entry->pre_remove) |
| 108 | entry->pre_remove += gd->reloc_off; |
Michal Simek | 61809f5 | 2015-10-27 13:48:08 +0100 | [diff] [blame] | 109 | if (entry->child_post_bind) |
| 110 | entry->child_post_bind += gd->reloc_off; |
| 111 | if (entry->child_pre_probe) |
| 112 | entry->child_pre_probe += gd->reloc_off; |
Michal Simek | 0ec473b | 2015-02-02 16:31:59 +0100 | [diff] [blame] | 113 | if (entry->init) |
| 114 | entry->init += gd->reloc_off; |
| 115 | if (entry->destroy) |
| 116 | entry->destroy += gd->reloc_off; |
Michal Simek | 0ec473b | 2015-02-02 16:31:59 +0100 | [diff] [blame] | 117 | } |
| 118 | } |
Angelo Dureghello | f4d193b | 2016-05-21 12:05:49 +0200 | [diff] [blame] | 119 | |
| 120 | void fix_devices(void) |
| 121 | { |
| 122 | struct driver_info *dev = |
| 123 | ll_entry_start(struct driver_info, driver_info); |
| 124 | const int n_ents = ll_entry_count(struct driver_info, driver_info); |
| 125 | struct driver_info *entry; |
| 126 | |
| 127 | for (entry = dev; entry != dev + n_ents; entry++) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 128 | if (entry->plat) |
| 129 | entry->plat += gd->reloc_off; |
Angelo Dureghello | f4d193b | 2016-05-21 12:05:49 +0200 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Simon Glass | 43f7ea5 | 2021-03-15 17:25:17 +1300 | [diff] [blame] | 133 | static int dm_setup_inst(void) |
| 134 | { |
| 135 | DM_ROOT_NON_CONST = DM_DEVICE_GET(root); |
| 136 | |
Simon Glass | 8beeb28 | 2021-03-15 17:25:36 +1300 | [diff] [blame] | 137 | if (CONFIG_IS_ENABLED(OF_PLATDATA_RT)) { |
| 138 | struct udevice_rt *urt; |
Simon Glass | 308d91b | 2022-03-27 14:26:20 -0600 | [diff] [blame^] | 139 | void *start, *end; |
| 140 | int each_size; |
Simon Glass | 8dee67a | 2021-03-15 17:25:38 +1300 | [diff] [blame] | 141 | void *base; |
Simon Glass | 8beeb28 | 2021-03-15 17:25:36 +1300 | [diff] [blame] | 142 | int n_ents; |
Simon Glass | 8dee67a | 2021-03-15 17:25:38 +1300 | [diff] [blame] | 143 | uint size; |
Simon Glass | 8beeb28 | 2021-03-15 17:25:36 +1300 | [diff] [blame] | 144 | |
| 145 | /* Allocate the udevice_rt table */ |
Simon Glass | 308d91b | 2022-03-27 14:26:20 -0600 | [diff] [blame^] | 146 | each_size = dm_udevice_size(); |
| 147 | start = ll_entry_start(struct udevice, udevice); |
| 148 | end = ll_entry_end(struct udevice, udevice); |
| 149 | size = end - start; |
| 150 | n_ents = size / each_size; |
Simon Glass | 8beeb28 | 2021-03-15 17:25:36 +1300 | [diff] [blame] | 151 | urt = calloc(n_ents, sizeof(struct udevice_rt)); |
| 152 | if (!urt) |
| 153 | return log_msg_ret("urt", -ENOMEM); |
| 154 | gd_set_dm_udevice_rt(urt); |
Simon Glass | 8dee67a | 2021-03-15 17:25:38 +1300 | [diff] [blame] | 155 | |
| 156 | /* Now allocate space for the priv/plat data, and copy it in */ |
| 157 | size = __priv_data_end - __priv_data_start; |
| 158 | |
| 159 | base = calloc(1, size); |
| 160 | if (!base) |
| 161 | return log_msg_ret("priv", -ENOMEM); |
| 162 | memcpy(base, __priv_data_start, size); |
| 163 | gd_set_dm_priv_base(base); |
Simon Glass | 8beeb28 | 2021-03-15 17:25:36 +1300 | [diff] [blame] | 164 | } |
| 165 | |
Simon Glass | 43f7ea5 | 2021-03-15 17:25:17 +1300 | [diff] [blame] | 166 | return 0; |
| 167 | } |
| 168 | |
Simon Glass | f19d9f2 | 2017-05-18 20:09:08 -0600 | [diff] [blame] | 169 | int dm_init(bool of_live) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 170 | { |
| 171 | int ret; |
| 172 | |
| 173 | if (gd->dm_root) { |
| 174 | dm_warn("Virtual root driver already exists!\n"); |
| 175 | return -EINVAL; |
| 176 | } |
Simon Glass | 6f15653 | 2021-03-15 17:25:16 +1300 | [diff] [blame] | 177 | if (CONFIG_IS_ENABLED(OF_PLATDATA_INST)) { |
| 178 | gd->uclass_root = &uclass_head; |
| 179 | } else { |
| 180 | gd->uclass_root = &DM_UCLASS_ROOT_S_NON_CONST; |
| 181 | INIT_LIST_HEAD(DM_UCLASS_ROOT_NON_CONST); |
| 182 | } |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 183 | |
Simon Glass | e2e8907 | 2020-10-03 11:31:37 -0600 | [diff] [blame] | 184 | if (IS_ENABLED(CONFIG_NEEDS_MANUAL_RELOC)) { |
| 185 | fix_drivers(); |
| 186 | fix_uclass(); |
| 187 | fix_devices(); |
| 188 | } |
Michal Simek | 0ec473b | 2015-02-02 16:31:59 +0100 | [diff] [blame] | 189 | |
Simon Glass | 43f7ea5 | 2021-03-15 17:25:17 +1300 | [diff] [blame] | 190 | if (CONFIG_IS_ENABLED(OF_PLATDATA_INST)) { |
| 191 | ret = dm_setup_inst(); |
| 192 | if (ret) { |
| 193 | log_debug("dm_setup_inst() failed: %d\n", ret); |
| 194 | return ret; |
| 195 | } |
| 196 | } else { |
| 197 | ret = device_bind_by_name(NULL, false, &root_info, |
| 198 | &DM_ROOT_NON_CONST); |
| 199 | if (ret) |
| 200 | return ret; |
| 201 | if (CONFIG_IS_ENABLED(OF_CONTROL)) |
| 202 | dev_set_ofnode(DM_ROOT_NON_CONST, ofnode_root()); |
| 203 | ret = device_probe(DM_ROOT_NON_CONST); |
| 204 | if (ret) |
| 205 | return ret; |
| 206 | } |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 207 | |
AKASHI Takahiro | e04f4fb | 2022-03-08 20:36:46 +0900 | [diff] [blame] | 208 | INIT_LIST_HEAD((struct list_head *)&gd->dmtag_list); |
| 209 | |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 210 | return 0; |
| 211 | } |
| 212 | |
Simon Glass | 0019758 | 2014-07-23 06:55:01 -0600 | [diff] [blame] | 213 | int dm_uninit(void) |
| 214 | { |
Marek Vasut | abbdbbd | 2021-01-24 14:32:46 -0700 | [diff] [blame] | 215 | /* Remove non-vital devices first */ |
| 216 | device_remove(dm_root(), DM_REMOVE_NON_VITAL); |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 217 | device_remove(dm_root(), DM_REMOVE_NORMAL); |
Simon Glass | 0019758 | 2014-07-23 06:55:01 -0600 | [diff] [blame] | 218 | device_unbind(dm_root()); |
Jean-Jacques Hiblot | 272b67d | 2018-12-07 14:50:54 +0100 | [diff] [blame] | 219 | gd->dm_root = NULL; |
Simon Glass | 0019758 | 2014-07-23 06:55:01 -0600 | [diff] [blame] | 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
Stefan Roese | 505045d | 2017-03-27 10:58:53 +0200 | [diff] [blame] | 224 | #if CONFIG_IS_ENABLED(DM_DEVICE_REMOVE) |
| 225 | int dm_remove_devices_flags(uint flags) |
| 226 | { |
| 227 | device_remove(dm_root(), flags); |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | #endif |
| 232 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 233 | int dm_scan_plat(bool pre_reloc_only) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 234 | { |
| 235 | int ret; |
| 236 | |
Simon Glass | 8beeb28 | 2021-03-15 17:25:36 +1300 | [diff] [blame] | 237 | if (CONFIG_IS_ENABLED(OF_PLATDATA_DRIVER_RT)) { |
Simon Glass | cfd6a00 | 2020-10-03 11:31:33 -0600 | [diff] [blame] | 238 | struct driver_rt *dyn; |
| 239 | int n_ents; |
| 240 | |
| 241 | n_ents = ll_entry_count(struct driver_info, driver_info); |
| 242 | dyn = calloc(n_ents, sizeof(struct driver_rt)); |
| 243 | if (!dyn) |
| 244 | return -ENOMEM; |
| 245 | gd_set_dm_driver_rt(dyn); |
| 246 | } |
| 247 | |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 248 | ret = lists_bind_drivers(DM_ROOT_NON_CONST, pre_reloc_only); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 249 | if (ret == -ENOENT) { |
| 250 | dm_warn("Some drivers were not found\n"); |
| 251 | ret = 0; |
| 252 | } |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 253 | |
Masahiro Yamada | 6cac81a | 2014-11-17 17:19:38 +0900 | [diff] [blame] | 254 | return ret; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Simon Glass | 3580f6d | 2021-08-07 07:24:03 -0600 | [diff] [blame] | 257 | #if CONFIG_IS_ENABLED(OF_REAL) |
Simon Glass | a4dbe25 | 2017-05-17 17:18:08 -0600 | [diff] [blame] | 258 | /** |
| 259 | * dm_scan_fdt_node() - Scan the device tree and bind drivers for a node |
| 260 | * |
| 261 | * This scans the subnodes of a device tree node and and creates a driver |
| 262 | * for each one. |
| 263 | * |
| 264 | * @parent: Parent device for the devices that will be created |
Simon Glass | 21a3088 | 2020-11-28 17:50:08 -0700 | [diff] [blame] | 265 | * @node: Node to scan |
Simon Glass | a4dbe25 | 2017-05-17 17:18:08 -0600 | [diff] [blame] | 266 | * @pre_reloc_only: If true, bind only drivers with the DM_FLAG_PRE_RELOC |
| 267 | * flag. If false bind all drivers. |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 268 | * Return: 0 if OK, -ve on error |
Simon Glass | a4dbe25 | 2017-05-17 17:18:08 -0600 | [diff] [blame] | 269 | */ |
Simon Glass | 21a3088 | 2020-11-28 17:50:08 -0700 | [diff] [blame] | 270 | static int dm_scan_fdt_node(struct udevice *parent, ofnode parent_node, |
| 271 | bool pre_reloc_only) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 272 | { |
Sean Anderson | 0449f06 | 2021-04-08 17:15:00 -0400 | [diff] [blame] | 273 | int ret = 0, err = 0; |
Simon Glass | 21a3088 | 2020-11-28 17:50:08 -0700 | [diff] [blame] | 274 | ofnode node; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 275 | |
Simon Glass | 21a3088 | 2020-11-28 17:50:08 -0700 | [diff] [blame] | 276 | if (!ofnode_valid(parent_node)) |
| 277 | return 0; |
| 278 | |
| 279 | for (node = ofnode_first_subnode(parent_node); |
| 280 | ofnode_valid(node); |
| 281 | node = ofnode_next_subnode(node)) { |
| 282 | const char *node_name = ofnode_get_name(node); |
Jens Wiklander | 651a836 | 2018-09-25 16:40:05 +0200 | [diff] [blame] | 283 | |
Simon Glass | 21a3088 | 2020-11-28 17:50:08 -0700 | [diff] [blame] | 284 | if (!ofnode_is_enabled(node)) { |
Masahiro Yamada | f70f39f | 2017-09-29 12:31:20 +0900 | [diff] [blame] | 285 | pr_debug(" - ignoring disabled device\n"); |
Simon Glass | 528b1ef | 2015-01-25 08:27:16 -0700 | [diff] [blame] | 286 | continue; |
| 287 | } |
Patrice Chotard | cc523cf | 2021-09-10 16:16:20 +0200 | [diff] [blame] | 288 | err = lists_bind_fdt(parent, node, NULL, NULL, pre_reloc_only); |
Simon Glass | c90b917 | 2015-08-30 16:55:17 -0600 | [diff] [blame] | 289 | if (err && !ret) { |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 290 | ret = err; |
Jens Wiklander | 651a836 | 2018-09-25 16:40:05 +0200 | [diff] [blame] | 291 | debug("%s: ret=%d\n", node_name, ret); |
Simon Glass | c90b917 | 2015-08-30 16:55:17 -0600 | [diff] [blame] | 292 | } |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 293 | } |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 294 | |
| 295 | if (ret) |
| 296 | dm_warn("Some drivers failed to bind\n"); |
| 297 | |
| 298 | return ret; |
| 299 | } |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 300 | |
Simon Glass | 5d5388d | 2016-07-05 17:10:08 -0600 | [diff] [blame] | 301 | int dm_scan_fdt_dev(struct udevice *dev) |
| 302 | { |
Simon Glass | 21a3088 | 2020-11-28 17:50:08 -0700 | [diff] [blame] | 303 | return dm_scan_fdt_node(dev, dev_ofnode(dev), |
Simon Glass | 5d5388d | 2016-07-05 17:10:08 -0600 | [diff] [blame] | 304 | gd->flags & GD_FLG_RELOC ? false : true); |
| 305 | } |
| 306 | |
Simon Glass | 5039cab | 2020-11-28 17:50:09 -0700 | [diff] [blame] | 307 | int dm_scan_fdt(bool pre_reloc_only) |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 308 | { |
Simon Glass | 21a3088 | 2020-11-28 17:50:08 -0700 | [diff] [blame] | 309 | return dm_scan_fdt_node(gd->dm_root, ofnode_root(), pre_reloc_only); |
Simon Glass | 4071742 | 2014-07-23 06:55:18 -0600 | [diff] [blame] | 310 | } |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 311 | |
Simon Glass | 5039cab | 2020-11-28 17:50:09 -0700 | [diff] [blame] | 312 | static int dm_scan_fdt_ofnode_path(const char *path, bool pre_reloc_only) |
Rajan Vaja | fac6463 | 2018-08-10 01:45:33 -0700 | [diff] [blame] | 313 | { |
| 314 | ofnode node; |
| 315 | |
| 316 | node = ofnode_path(path); |
Simon Glass | 40916e6 | 2020-10-03 09:25:22 -0600 | [diff] [blame] | 317 | |
Simon Glass | 21a3088 | 2020-11-28 17:50:08 -0700 | [diff] [blame] | 318 | return dm_scan_fdt_node(gd->dm_root, node, pre_reloc_only); |
Rajan Vaja | fac6463 | 2018-08-10 01:45:33 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Simon Glass | 4d71d60 | 2020-11-28 17:50:10 -0700 | [diff] [blame] | 321 | int dm_extended_scan(bool pre_reloc_only) |
Patrice Chotard | bb3a45b | 2017-09-04 14:55:56 +0200 | [diff] [blame] | 322 | { |
Patrick Delaunay | c779e20 | 2020-02-18 15:43:46 +0100 | [diff] [blame] | 323 | int ret, i; |
| 324 | const char * const nodes[] = { |
| 325 | "/chosen", |
| 326 | "/clocks", |
| 327 | "/firmware" |
| 328 | }; |
Patrice Chotard | bb3a45b | 2017-09-04 14:55:56 +0200 | [diff] [blame] | 329 | |
Simon Glass | 5039cab | 2020-11-28 17:50:09 -0700 | [diff] [blame] | 330 | ret = dm_scan_fdt(pre_reloc_only); |
Patrice Chotard | bb3a45b | 2017-09-04 14:55:56 +0200 | [diff] [blame] | 331 | if (ret) { |
| 332 | debug("dm_scan_fdt() failed: %d\n", ret); |
| 333 | return ret; |
| 334 | } |
| 335 | |
Patrick Delaunay | c779e20 | 2020-02-18 15:43:46 +0100 | [diff] [blame] | 336 | /* Some nodes aren't devices themselves but may contain some */ |
| 337 | for (i = 0; i < ARRAY_SIZE(nodes); i++) { |
Simon Glass | 5039cab | 2020-11-28 17:50:09 -0700 | [diff] [blame] | 338 | ret = dm_scan_fdt_ofnode_path(nodes[i], pre_reloc_only); |
Patrick Delaunay | c779e20 | 2020-02-18 15:43:46 +0100 | [diff] [blame] | 339 | if (ret) { |
| 340 | debug("dm_scan_fdt() scan for %s failed: %d\n", |
| 341 | nodes[i], ret); |
| 342 | return ret; |
| 343 | } |
Rajan Vaja | e9fc81d | 2018-08-10 01:45:34 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Patrice Chotard | bb3a45b | 2017-09-04 14:55:56 +0200 | [diff] [blame] | 346 | return ret; |
| 347 | } |
Marek Vasut | 7f18c34 | 2019-08-31 18:03:28 +0200 | [diff] [blame] | 348 | #endif |
Patrice Chotard | bb3a45b | 2017-09-04 14:55:56 +0200 | [diff] [blame] | 349 | |
Simon Glass | 97e22e3 | 2014-07-23 06:55:23 -0600 | [diff] [blame] | 350 | __weak int dm_scan_other(bool pre_reloc_only) |
| 351 | { |
| 352 | return 0; |
| 353 | } |
| 354 | |
Simon Glass | 781aa3e | 2021-03-15 17:25:40 +1300 | [diff] [blame] | 355 | #if CONFIG_IS_ENABLED(OF_PLATDATA_INST) && CONFIG_IS_ENABLED(READ_ONLY) |
| 356 | void *dm_priv_to_rw(void *priv) |
| 357 | { |
| 358 | long offset = priv - (void *)__priv_data_start; |
| 359 | |
| 360 | return gd_dm_priv_base() + offset; |
| 361 | } |
| 362 | #endif |
| 363 | |
Simon Glass | c77695b | 2020-12-19 10:40:16 -0700 | [diff] [blame] | 364 | /** |
| 365 | * dm_scan() - Scan tables to bind devices |
| 366 | * |
| 367 | * Runs through the driver_info tables and binds the devices it finds. Then runs |
| 368 | * through the devicetree nodes. Finally calls dm_scan_other() to add any |
| 369 | * special devices |
| 370 | * |
| 371 | * @pre_reloc_only: If true, bind only nodes with special devicetree properties, |
| 372 | * or drivers with the DM_FLAG_PRE_RELOC flag. If false bind all drivers. |
| 373 | */ |
| 374 | static int dm_scan(bool pre_reloc_only) |
Simon Glass | a730c5d | 2014-07-23 06:55:04 -0600 | [diff] [blame] | 375 | { |
| 376 | int ret; |
| 377 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 378 | ret = dm_scan_plat(pre_reloc_only); |
Simon Glass | a730c5d | 2014-07-23 06:55:04 -0600 | [diff] [blame] | 379 | if (ret) { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 380 | debug("dm_scan_plat() failed: %d\n", ret); |
Simon Glass | c77695b | 2020-12-19 10:40:16 -0700 | [diff] [blame] | 381 | return ret; |
Simon Glass | a730c5d | 2014-07-23 06:55:04 -0600 | [diff] [blame] | 382 | } |
Simon Glass | 0704b85 | 2015-02-27 22:06:41 -0700 | [diff] [blame] | 383 | |
Simon Glass | 3580f6d | 2021-08-07 07:24:03 -0600 | [diff] [blame] | 384 | if (CONFIG_IS_ENABLED(OF_REAL)) { |
Simon Glass | 4d71d60 | 2020-11-28 17:50:10 -0700 | [diff] [blame] | 385 | ret = dm_extended_scan(pre_reloc_only); |
Simon Glass | 0704b85 | 2015-02-27 22:06:41 -0700 | [diff] [blame] | 386 | if (ret) { |
Simon Glass | 4d71d60 | 2020-11-28 17:50:10 -0700 | [diff] [blame] | 387 | debug("dm_extended_scan() failed: %d\n", ret); |
Simon Glass | c77695b | 2020-12-19 10:40:16 -0700 | [diff] [blame] | 388 | return ret; |
Simon Glass | 0704b85 | 2015-02-27 22:06:41 -0700 | [diff] [blame] | 389 | } |
Simon Glass | a730c5d | 2014-07-23 06:55:04 -0600 | [diff] [blame] | 390 | } |
Simon Glass | 0704b85 | 2015-02-27 22:06:41 -0700 | [diff] [blame] | 391 | |
Simon Glass | 97e22e3 | 2014-07-23 06:55:23 -0600 | [diff] [blame] | 392 | ret = dm_scan_other(pre_reloc_only); |
| 393 | if (ret) |
Simon Glass | c77695b | 2020-12-19 10:40:16 -0700 | [diff] [blame] | 394 | return ret; |
| 395 | |
| 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | int dm_init_and_scan(bool pre_reloc_only) |
| 400 | { |
| 401 | int ret; |
| 402 | |
Simon Glass | c77695b | 2020-12-19 10:40:16 -0700 | [diff] [blame] | 403 | ret = dm_init(CONFIG_IS_ENABLED(OF_LIVE)); |
| 404 | if (ret) { |
| 405 | debug("dm_init() failed: %d\n", ret); |
| 406 | return ret; |
| 407 | } |
Simon Glass | 43f7ea5 | 2021-03-15 17:25:17 +1300 | [diff] [blame] | 408 | if (!CONFIG_IS_ENABLED(OF_PLATDATA_INST)) { |
| 409 | ret = dm_scan(pre_reloc_only); |
| 410 | if (ret) { |
| 411 | log_debug("dm_scan() failed: %d\n", ret); |
| 412 | return ret; |
| 413 | } |
Simon Glass | c77695b | 2020-12-19 10:40:16 -0700 | [diff] [blame] | 414 | } |
Simon Glass | fc55736 | 2022-03-04 08:43:05 -0700 | [diff] [blame] | 415 | if (CONFIG_IS_ENABLED(DM_EVENT)) { |
| 416 | ret = event_notify_null(EVT_DM_POST_INIT); |
| 417 | if (ret) |
| 418 | return log_msg_ret("ev", ret); |
| 419 | } |
Simon Glass | a730c5d | 2014-07-23 06:55:04 -0600 | [diff] [blame] | 420 | |
| 421 | return 0; |
| 422 | } |
| 423 | |
Simon Glass | 51608c9 | 2021-12-16 20:59:32 -0700 | [diff] [blame] | 424 | void dm_get_stats(int *device_countp, int *uclass_countp) |
| 425 | { |
| 426 | *device_countp = device_get_decendent_count(gd->dm_root); |
| 427 | *uclass_countp = uclass_get_count(); |
| 428 | } |
| 429 | |
Simon Glass | 719ea08 | 2020-07-07 13:11:38 -0600 | [diff] [blame] | 430 | #ifdef CONFIG_ACPIGEN |
| 431 | static int root_acpi_get_name(const struct udevice *dev, char *out_name) |
| 432 | { |
| 433 | return acpi_copy_name(out_name, "\\_SB"); |
| 434 | } |
| 435 | |
| 436 | struct acpi_ops root_acpi_ops = { |
| 437 | .get_name = root_acpi_get_name, |
| 438 | }; |
| 439 | #endif |
| 440 | |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 441 | /* This is the root driver - all drivers are children of this */ |
| 442 | U_BOOT_DRIVER(root_driver) = { |
| 443 | .name = "root_driver", |
| 444 | .id = UCLASS_ROOT, |
Simon Glass | 719ea08 | 2020-07-07 13:11:38 -0600 | [diff] [blame] | 445 | ACPI_OPS_PTR(&root_acpi_ops) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 446 | }; |
| 447 | |
| 448 | /* This is the root uclass */ |
| 449 | UCLASS_DRIVER(root) = { |
| 450 | .name = "root", |
| 451 | .id = UCLASS_ROOT, |
| 452 | }; |