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