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