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 | * Device manager |
| 4 | * |
| 5 | * Copyright (c) 2013 Google, Inc |
| 6 | * |
| 7 | * (C) Copyright 2012 |
| 8 | * Pavel Herrmann <morpheus.ibis@gmail.com> |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <common.h> |
Simon Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 12 | #include <cpu_func.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 14 | #include <asm/global_data.h> |
Vignesh R | 0dff370 | 2016-07-06 09:58:55 +0530 | [diff] [blame] | 15 | #include <asm/io.h> |
Philipp Tomsich | 9cf03b0 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 16 | #include <clk.h> |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 17 | #include <fdtdec.h> |
Stefan Roese | adc0905 | 2015-09-02 07:41:12 +0200 | [diff] [blame] | 18 | #include <fdt_support.h> |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 19 | #include <malloc.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 20 | #include <asm/cache.h> |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 21 | #include <dm/device.h> |
| 22 | #include <dm/device-internal.h> |
| 23 | #include <dm/lists.h> |
Mario Six | 1f1c1ec | 2018-01-15 11:07:20 +0100 | [diff] [blame] | 24 | #include <dm/of_access.h> |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 25 | #include <dm/pinctrl.h> |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 26 | #include <dm/platdata.h> |
Simon Glass | 322b290 | 2017-05-18 20:09:05 -0600 | [diff] [blame] | 27 | #include <dm/read.h> |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 28 | #include <dm/uclass.h> |
| 29 | #include <dm/uclass-internal.h> |
| 30 | #include <dm/util.h> |
| 31 | #include <linux/err.h> |
| 32 | #include <linux/list.h> |
Peng Fan | 6fba32d | 2018-07-27 10:20:38 +0800 | [diff] [blame] | 33 | #include <power-domain.h> |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 34 | |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 35 | DECLARE_GLOBAL_DATA_PTR; |
| 36 | |
Stephen Warren | 8c93df1 | 2016-05-11 15:26:24 -0600 | [diff] [blame] | 37 | static int device_bind_common(struct udevice *parent, const struct driver *drv, |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 38 | const char *name, void *plat, |
Simon Glass | b0cf4b3 | 2017-05-17 17:18:11 -0600 | [diff] [blame] | 39 | ulong driver_data, ofnode node, |
Simon Glass | 39edb95 | 2020-12-03 16:55:19 -0700 | [diff] [blame] | 40 | uint of_plat_size, struct udevice **devp) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 41 | { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 42 | struct udevice *dev; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 43 | struct uclass *uc; |
Przemyslaw Marczak | d850e67 | 2015-04-15 13:07:18 +0200 | [diff] [blame] | 44 | int size, ret = 0; |
Simon Glass | 7272e92 | 2020-12-16 21:20:09 -0700 | [diff] [blame] | 45 | bool auto_seq = true; |
Simon Glass | 2543b16 | 2020-12-22 19:30:29 -0700 | [diff] [blame] | 46 | void *ptr; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 47 | |
Masahiro Yamada | e1cc1f0 | 2015-08-27 12:44:28 +0900 | [diff] [blame] | 48 | if (devp) |
| 49 | *devp = NULL; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 50 | if (!name) |
| 51 | return -EINVAL; |
| 52 | |
| 53 | ret = uclass_get(drv->id, &uc); |
Simon Glass | 43313de | 2015-08-30 16:55:16 -0600 | [diff] [blame] | 54 | if (ret) { |
| 55 | debug("Missing uclass for driver %s\n", drv->name); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 56 | return ret; |
Simon Glass | 43313de | 2015-08-30 16:55:16 -0600 | [diff] [blame] | 57 | } |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 58 | |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 59 | dev = calloc(1, sizeof(struct udevice)); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 60 | if (!dev) |
| 61 | return -ENOMEM; |
| 62 | |
| 63 | INIT_LIST_HEAD(&dev->sibling_node); |
| 64 | INIT_LIST_HEAD(&dev->child_head); |
| 65 | INIT_LIST_HEAD(&dev->uclass_node); |
Masahiro Yamada | 029bfca | 2015-07-25 21:52:37 +0900 | [diff] [blame] | 66 | #ifdef CONFIG_DEVRES |
Masahiro Yamada | 8b15b16 | 2015-07-25 21:52:35 +0900 | [diff] [blame] | 67 | INIT_LIST_HEAD(&dev->devres_head); |
Masahiro Yamada | 029bfca | 2015-07-25 21:52:37 +0900 | [diff] [blame] | 68 | #endif |
Simon Glass | 2543b16 | 2020-12-22 19:30:29 -0700 | [diff] [blame] | 69 | dev_set_plat(dev, plat); |
Stephen Warren | 8c93df1 | 2016-05-11 15:26:24 -0600 | [diff] [blame] | 70 | dev->driver_data = driver_data; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 71 | dev->name = name; |
Simon Glass | a7ece58 | 2020-12-19 10:40:14 -0700 | [diff] [blame] | 72 | dev_set_ofnode(dev, node); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 73 | dev->parent = parent; |
| 74 | dev->driver = drv; |
| 75 | dev->uclass = uc; |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 76 | |
Simon Glass | 5e34992 | 2020-12-19 10:40:09 -0700 | [diff] [blame] | 77 | dev->seq_ = -1; |
Jean-Jacques Hiblot | a5da300 | 2018-12-07 14:50:39 +0100 | [diff] [blame] | 78 | if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) && |
| 79 | (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS)) { |
Simon Glass | 1bd3deb | 2015-02-27 22:06:30 -0700 | [diff] [blame] | 80 | /* |
Stefan Roese | 9d4ce6b | 2016-03-16 09:58:01 +0100 | [diff] [blame] | 81 | * Some devices, such as a SPI bus, I2C bus and serial ports |
| 82 | * are numbered using aliases. |
Stefan Roese | 9d4ce6b | 2016-03-16 09:58:01 +0100 | [diff] [blame] | 83 | */ |
Simon Glass | e422f8c | 2020-07-09 18:39:24 -0600 | [diff] [blame] | 84 | if (CONFIG_IS_ENABLED(OF_CONTROL) && |
| 85 | !CONFIG_IS_ENABLED(OF_PLATDATA)) { |
Simon Glass | 7272e92 | 2020-12-16 21:20:09 -0700 | [diff] [blame] | 86 | if (uc->uc_drv->name && ofnode_valid(node)) { |
Simon Glass | 5e34992 | 2020-12-19 10:40:09 -0700 | [diff] [blame] | 87 | if (!dev_read_alias_seq(dev, &dev->seq_)) |
Simon Glass | 38040a9 | 2020-12-16 21:20:32 -0700 | [diff] [blame] | 88 | auto_seq = false; |
Simon Glass | 7272e92 | 2020-12-16 21:20:09 -0700 | [diff] [blame] | 89 | } |
Simon Glass | 0ccb097 | 2015-01-25 08:27:05 -0700 | [diff] [blame] | 90 | } |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 91 | } |
Simon Glass | 3a9d5f1 | 2020-12-16 21:20:17 -0700 | [diff] [blame] | 92 | if (auto_seq && !(uc->uc_drv->flags & DM_UC_FLAG_NO_AUTO_SEQ)) |
Simon Glass | 5e34992 | 2020-12-19 10:40:09 -0700 | [diff] [blame] | 93 | dev->seq_ = uclass_find_next_free_seq(uc); |
Simon Glass | 1bd3deb | 2015-02-27 22:06:30 -0700 | [diff] [blame] | 94 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 95 | if (drv->plat_auto) { |
| 96 | bool alloc = !plat; |
Simon Glass | afbf9b8 | 2016-07-04 11:58:18 -0600 | [diff] [blame] | 97 | |
| 98 | if (CONFIG_IS_ENABLED(OF_PLATDATA)) { |
Simon Glass | 39edb95 | 2020-12-03 16:55:19 -0700 | [diff] [blame] | 99 | if (of_plat_size) { |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 100 | dev_or_flags(dev, DM_FLAG_OF_PLATDATA); |
Simon Glass | 39edb95 | 2020-12-03 16:55:19 -0700 | [diff] [blame] | 101 | if (of_plat_size < drv->plat_auto) |
Simon Glass | afbf9b8 | 2016-07-04 11:58:18 -0600 | [diff] [blame] | 102 | alloc = true; |
| 103 | } |
| 104 | } |
| 105 | if (alloc) { |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 106 | dev_or_flags(dev, DM_FLAG_ALLOC_PDATA); |
Simon Glass | 2543b16 | 2020-12-22 19:30:29 -0700 | [diff] [blame] | 107 | ptr = calloc(1, drv->plat_auto); |
| 108 | if (!ptr) { |
Simon Glass | afbf9b8 | 2016-07-04 11:58:18 -0600 | [diff] [blame] | 109 | ret = -ENOMEM; |
| 110 | goto fail_alloc1; |
| 111 | } |
Simon Glass | 2543b16 | 2020-12-22 19:30:29 -0700 | [diff] [blame] | 112 | if (CONFIG_IS_ENABLED(OF_PLATDATA) && plat) |
| 113 | memcpy(ptr, plat, of_plat_size); |
| 114 | dev_set_plat(dev, ptr); |
Simon Glass | ba75049 | 2015-01-25 08:27:00 -0700 | [diff] [blame] | 115 | } |
| 116 | } |
Przemyslaw Marczak | d850e67 | 2015-04-15 13:07:18 +0200 | [diff] [blame] | 117 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 118 | size = uc->uc_drv->per_device_plat_auto; |
Przemyslaw Marczak | d850e67 | 2015-04-15 13:07:18 +0200 | [diff] [blame] | 119 | if (size) { |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 120 | dev_or_flags(dev, DM_FLAG_ALLOC_UCLASS_PDATA); |
Simon Glass | 2543b16 | 2020-12-22 19:30:29 -0700 | [diff] [blame] | 121 | ptr = calloc(1, size); |
| 122 | if (!ptr) { |
Przemyslaw Marczak | d850e67 | 2015-04-15 13:07:18 +0200 | [diff] [blame] | 123 | ret = -ENOMEM; |
| 124 | goto fail_alloc2; |
| 125 | } |
Simon Glass | 2543b16 | 2020-12-22 19:30:29 -0700 | [diff] [blame] | 126 | dev_set_uclass_plat(dev, ptr); |
Przemyslaw Marczak | d850e67 | 2015-04-15 13:07:18 +0200 | [diff] [blame] | 127 | } |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 128 | |
Przemyslaw Marczak | d850e67 | 2015-04-15 13:07:18 +0200 | [diff] [blame] | 129 | if (parent) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 130 | size = parent->driver->per_child_plat_auto; |
Simon Glass | 57f9540 | 2015-01-25 08:27:02 -0700 | [diff] [blame] | 131 | if (!size) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 132 | size = parent->uclass->uc_drv->per_child_plat_auto; |
Simon Glass | 57f9540 | 2015-01-25 08:27:02 -0700 | [diff] [blame] | 133 | } |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 134 | if (size) { |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 135 | dev_or_flags(dev, DM_FLAG_ALLOC_PARENT_PDATA); |
Simon Glass | 2543b16 | 2020-12-22 19:30:29 -0700 | [diff] [blame] | 136 | ptr = calloc(1, size); |
| 137 | if (!ptr) { |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 138 | ret = -ENOMEM; |
Przemyslaw Marczak | d850e67 | 2015-04-15 13:07:18 +0200 | [diff] [blame] | 139 | goto fail_alloc3; |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 140 | } |
Simon Glass | 2543b16 | 2020-12-22 19:30:29 -0700 | [diff] [blame] | 141 | dev_set_parent_plat(dev, ptr); |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 142 | } |
Heinrich Schuchardt | 5246b37 | 2020-02-15 21:38:48 +0100 | [diff] [blame] | 143 | /* put dev into parent's successor list */ |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 144 | list_add_tail(&dev->sibling_node, &parent->child_head); |
Heinrich Schuchardt | 5246b37 | 2020-02-15 21:38:48 +0100 | [diff] [blame] | 145 | } |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 146 | |
| 147 | ret = uclass_bind_device(dev); |
| 148 | if (ret) |
Simon Glass | 4ebe01b | 2015-01-25 08:26:59 -0700 | [diff] [blame] | 149 | goto fail_uclass_bind; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 150 | |
| 151 | /* if we fail to bind we remove device from successors and free it */ |
| 152 | if (drv->bind) { |
| 153 | ret = drv->bind(dev); |
Simon Glass | 4ebe01b | 2015-01-25 08:26:59 -0700 | [diff] [blame] | 154 | if (ret) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 155 | goto fail_bind; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 156 | } |
Simon Glass | a4a51a0 | 2015-01-25 08:27:03 -0700 | [diff] [blame] | 157 | if (parent && parent->driver->child_post_bind) { |
| 158 | ret = parent->driver->child_post_bind(dev); |
| 159 | if (ret) |
| 160 | goto fail_child_post_bind; |
| 161 | } |
Simon Glass | 286863d | 2016-01-05 09:30:59 -0700 | [diff] [blame] | 162 | if (uc->uc_drv->post_bind) { |
| 163 | ret = uc->uc_drv->post_bind(dev); |
| 164 | if (ret) |
| 165 | goto fail_uclass_post_bind; |
| 166 | } |
Simon Glass | a4a51a0 | 2015-01-25 08:27:03 -0700 | [diff] [blame] | 167 | |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 168 | if (parent) |
Masahiro Yamada | f70f39f | 2017-09-29 12:31:20 +0900 | [diff] [blame] | 169 | pr_debug("Bound device %s to %s\n", dev->name, parent->name); |
Masahiro Yamada | e1cc1f0 | 2015-08-27 12:44:28 +0900 | [diff] [blame] | 170 | if (devp) |
| 171 | *devp = dev; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 172 | |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 173 | dev_or_flags(dev, DM_FLAG_BOUND); |
Masahiro Yamada | bdbb5dd | 2015-07-25 21:52:34 +0900 | [diff] [blame] | 174 | |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 175 | return 0; |
| 176 | |
Simon Glass | 286863d | 2016-01-05 09:30:59 -0700 | [diff] [blame] | 177 | fail_uclass_post_bind: |
| 178 | /* There is no child unbind() method, so no clean-up required */ |
Simon Glass | a4a51a0 | 2015-01-25 08:27:03 -0700 | [diff] [blame] | 179 | fail_child_post_bind: |
Masahiro Yamada | 04aa00d | 2015-08-12 07:31:52 +0900 | [diff] [blame] | 180 | if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) { |
Simon Glass | e70a4f4 | 2015-02-27 22:06:33 -0700 | [diff] [blame] | 181 | if (drv->unbind && drv->unbind(dev)) { |
| 182 | dm_warn("unbind() method failed on dev '%s' on error path\n", |
| 183 | dev->name); |
| 184 | } |
Simon Glass | a4a51a0 | 2015-01-25 08:27:03 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 187 | fail_bind: |
Masahiro Yamada | 04aa00d | 2015-08-12 07:31:52 +0900 | [diff] [blame] | 188 | if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) { |
Simon Glass | e70a4f4 | 2015-02-27 22:06:33 -0700 | [diff] [blame] | 189 | if (uclass_unbind_device(dev)) { |
| 190 | dm_warn("Failed to unbind dev '%s' on error path\n", |
| 191 | dev->name); |
| 192 | } |
Simon Glass | 4ebe01b | 2015-01-25 08:26:59 -0700 | [diff] [blame] | 193 | } |
| 194 | fail_uclass_bind: |
Masahiro Yamada | 04aa00d | 2015-08-12 07:31:52 +0900 | [diff] [blame] | 195 | if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) { |
Simon Glass | e70a4f4 | 2015-02-27 22:06:33 -0700 | [diff] [blame] | 196 | list_del(&dev->sibling_node); |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 197 | if (dev_get_flags(dev) & DM_FLAG_ALLOC_PARENT_PDATA) { |
Simon Glass | 2543b16 | 2020-12-22 19:30:29 -0700 | [diff] [blame] | 198 | free(dev_get_parent_plat(dev)); |
| 199 | dev_set_parent_plat(dev, NULL); |
Simon Glass | e70a4f4 | 2015-02-27 22:06:33 -0700 | [diff] [blame] | 200 | } |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 201 | } |
Przemyslaw Marczak | d850e67 | 2015-04-15 13:07:18 +0200 | [diff] [blame] | 202 | fail_alloc3: |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 203 | if (dev_get_flags(dev) & DM_FLAG_ALLOC_UCLASS_PDATA) { |
Simon Glass | 2543b16 | 2020-12-22 19:30:29 -0700 | [diff] [blame] | 204 | free(dev_get_uclass_plat(dev)); |
| 205 | dev_set_uclass_plat(dev, NULL); |
Przemyslaw Marczak | d850e67 | 2015-04-15 13:07:18 +0200 | [diff] [blame] | 206 | } |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 207 | fail_alloc2: |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 208 | if (dev_get_flags(dev) & DM_FLAG_ALLOC_PDATA) { |
Simon Glass | 2543b16 | 2020-12-22 19:30:29 -0700 | [diff] [blame] | 209 | free(dev_get_plat(dev)); |
| 210 | dev_set_plat(dev, NULL); |
Simon Glass | ba75049 | 2015-01-25 08:27:00 -0700 | [diff] [blame] | 211 | } |
| 212 | fail_alloc1: |
Masahiro Yamada | 8b15b16 | 2015-07-25 21:52:35 +0900 | [diff] [blame] | 213 | devres_release_all(dev); |
| 214 | |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 215 | free(dev); |
Simon Glass | 4ebe01b | 2015-01-25 08:26:59 -0700 | [diff] [blame] | 216 | |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 217 | return ret; |
| 218 | } |
| 219 | |
Stephen Warren | 8c93df1 | 2016-05-11 15:26:24 -0600 | [diff] [blame] | 220 | int device_bind_with_driver_data(struct udevice *parent, |
| 221 | const struct driver *drv, const char *name, |
Simon Glass | 322b290 | 2017-05-18 20:09:05 -0600 | [diff] [blame] | 222 | ulong driver_data, ofnode node, |
Stephen Warren | 8c93df1 | 2016-05-11 15:26:24 -0600 | [diff] [blame] | 223 | struct udevice **devp) |
| 224 | { |
Simon Glass | 322b290 | 2017-05-18 20:09:05 -0600 | [diff] [blame] | 225 | return device_bind_common(parent, drv, name, NULL, driver_data, node, |
| 226 | 0, devp); |
Stephen Warren | 8c93df1 | 2016-05-11 15:26:24 -0600 | [diff] [blame] | 227 | } |
| 228 | |
Simon Glass | 884870f | 2020-11-28 17:50:01 -0700 | [diff] [blame] | 229 | int device_bind(struct udevice *parent, const struct driver *drv, |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 230 | const char *name, void *plat, ofnode node, |
Simon Glass | 884870f | 2020-11-28 17:50:01 -0700 | [diff] [blame] | 231 | struct udevice **devp) |
Simon Glass | 9eb151e | 2018-06-11 13:07:15 -0600 | [diff] [blame] | 232 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 233 | return device_bind_common(parent, drv, name, plat, 0, node, 0, |
Simon Glass | 9eb151e | 2018-06-11 13:07:15 -0600 | [diff] [blame] | 234 | devp); |
| 235 | } |
| 236 | |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 237 | int device_bind_by_name(struct udevice *parent, bool pre_reloc_only, |
Simon Glass | cfd6a00 | 2020-10-03 11:31:33 -0600 | [diff] [blame] | 238 | const struct driver_info *info, struct udevice **devp) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 239 | { |
| 240 | struct driver *drv; |
Simon Glass | 39edb95 | 2020-12-03 16:55:19 -0700 | [diff] [blame] | 241 | uint plat_size = 0; |
Walter Lozano | ffc41b0 | 2020-06-25 01:10:11 -0300 | [diff] [blame] | 242 | int ret; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 243 | |
| 244 | drv = lists_driver_lookup_name(info->name); |
| 245 | if (!drv) |
| 246 | return -ENOENT; |
Simon Glass | fef72b7 | 2014-07-23 06:55:03 -0600 | [diff] [blame] | 247 | if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC)) |
| 248 | return -EPERM; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 249 | |
Simon Glass | afbf9b8 | 2016-07-04 11:58:18 -0600 | [diff] [blame] | 250 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | 39edb95 | 2020-12-03 16:55:19 -0700 | [diff] [blame] | 251 | plat_size = info->plat_size; |
Simon Glass | afbf9b8 | 2016-07-04 11:58:18 -0600 | [diff] [blame] | 252 | #endif |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 253 | ret = device_bind_common(parent, drv, info->name, (void *)info->plat, 0, |
Simon Glass | 39edb95 | 2020-12-03 16:55:19 -0700 | [diff] [blame] | 254 | ofnode_null(), plat_size, devp); |
Walter Lozano | ffc41b0 | 2020-06-25 01:10:11 -0300 | [diff] [blame] | 255 | if (ret) |
| 256 | return ret; |
Walter Lozano | ffc41b0 | 2020-06-25 01:10:11 -0300 | [diff] [blame] | 257 | |
| 258 | return ret; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Claudiu Beznea | bf5b823 | 2020-09-07 17:46:33 +0300 | [diff] [blame] | 261 | int device_reparent(struct udevice *dev, struct udevice *new_parent) |
| 262 | { |
| 263 | struct udevice *pos, *n; |
| 264 | |
| 265 | assert(dev); |
| 266 | assert(new_parent); |
| 267 | |
| 268 | list_for_each_entry_safe(pos, n, &dev->parent->child_head, |
| 269 | sibling_node) { |
| 270 | if (pos->driver != dev->driver) |
| 271 | continue; |
| 272 | |
| 273 | list_del(&dev->sibling_node); |
| 274 | list_add_tail(&dev->sibling_node, &new_parent->child_head); |
| 275 | dev->parent = new_parent; |
| 276 | |
| 277 | break; |
| 278 | } |
| 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
Simon Glass | 825d3f9 | 2015-03-25 12:21:53 -0600 | [diff] [blame] | 283 | static void *alloc_priv(int size, uint flags) |
| 284 | { |
| 285 | void *priv; |
| 286 | |
| 287 | if (flags & DM_FLAG_ALLOC_PRIV_DMA) { |
Faiz Abbas | 03020a9 | 2017-09-19 16:53:50 +0530 | [diff] [blame] | 288 | size = ROUND(size, ARCH_DMA_MINALIGN); |
Simon Glass | 825d3f9 | 2015-03-25 12:21:53 -0600 | [diff] [blame] | 289 | priv = memalign(ARCH_DMA_MINALIGN, size); |
Simon Glass | 27bc9b1 | 2017-04-04 13:00:19 -0600 | [diff] [blame] | 290 | if (priv) { |
Simon Glass | 825d3f9 | 2015-03-25 12:21:53 -0600 | [diff] [blame] | 291 | memset(priv, '\0', size); |
Simon Glass | 27bc9b1 | 2017-04-04 13:00:19 -0600 | [diff] [blame] | 292 | |
| 293 | /* |
| 294 | * Ensure that the zero bytes are flushed to memory. |
| 295 | * This prevents problems if the driver uses this as |
| 296 | * both an input and an output buffer: |
| 297 | * |
| 298 | * 1. Zeroes written to buffer (here) and sit in the |
| 299 | * cache |
| 300 | * 2. Driver issues a read command to DMA |
| 301 | * 3. CPU runs out of cache space and evicts some cache |
| 302 | * data in the buffer, writing zeroes to RAM from |
| 303 | * the memset() above |
| 304 | * 4. DMA completes |
| 305 | * 5. Buffer now has some DMA data and some zeroes |
| 306 | * 6. Data being read is now incorrect |
| 307 | * |
| 308 | * To prevent this, ensure that the cache is clean |
| 309 | * within this range at the start. The driver can then |
| 310 | * use normal flush-after-write, invalidate-before-read |
| 311 | * procedures. |
| 312 | * |
| 313 | * TODO(sjg@chromium.org): Drop this microblaze |
| 314 | * exception. |
| 315 | */ |
| 316 | #ifndef CONFIG_MICROBLAZE |
| 317 | flush_dcache_range((ulong)priv, (ulong)priv + size); |
| 318 | #endif |
| 319 | } |
Simon Glass | 825d3f9 | 2015-03-25 12:21:53 -0600 | [diff] [blame] | 320 | } else { |
| 321 | priv = calloc(1, size); |
| 322 | } |
| 323 | |
| 324 | return priv; |
| 325 | } |
| 326 | |
Simon Glass | ad337e6 | 2020-12-19 10:40:08 -0700 | [diff] [blame] | 327 | /** |
| 328 | * device_alloc_priv() - Allocate priv/plat data required by the device |
| 329 | * |
| 330 | * @dev: Device to process |
| 331 | * @return 0 if OK, -ENOMEM if out of memory |
| 332 | */ |
| 333 | static int device_alloc_priv(struct udevice *dev) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 334 | { |
Simon Glass | a626dff | 2015-03-25 12:21:54 -0600 | [diff] [blame] | 335 | const struct driver *drv; |
Simon Glass | 2543b16 | 2020-12-22 19:30:29 -0700 | [diff] [blame] | 336 | void *ptr; |
Simon Glass | ad337e6 | 2020-12-19 10:40:08 -0700 | [diff] [blame] | 337 | int size; |
Simon Glass | 44c4789 | 2020-04-05 15:38:19 -0600 | [diff] [blame] | 338 | |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 339 | drv = dev->driver; |
| 340 | assert(drv); |
| 341 | |
Bin Meng | 82cdd78 | 2015-08-24 01:14:02 -0700 | [diff] [blame] | 342 | /* Allocate private data if requested and not reentered */ |
Simon Glass | 2543b16 | 2020-12-22 19:30:29 -0700 | [diff] [blame] | 343 | if (drv->priv_auto && !dev_get_priv(dev)) { |
| 344 | ptr = alloc_priv(drv->priv_auto, drv->flags); |
Simon Glass | ad337e6 | 2020-12-19 10:40:08 -0700 | [diff] [blame] | 345 | if (!ptr) |
| 346 | return -ENOMEM; |
Simon Glass | 2543b16 | 2020-12-22 19:30:29 -0700 | [diff] [blame] | 347 | dev_set_priv(dev, ptr); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 348 | } |
Simon Glass | ad337e6 | 2020-12-19 10:40:08 -0700 | [diff] [blame] | 349 | |
Bin Meng | 82cdd78 | 2015-08-24 01:14:02 -0700 | [diff] [blame] | 350 | /* Allocate private data if requested and not reentered */ |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 351 | size = dev->uclass->uc_drv->per_device_auto; |
Simon Glass | 2543b16 | 2020-12-22 19:30:29 -0700 | [diff] [blame] | 352 | if (size && !dev_get_uclass_priv(dev)) { |
| 353 | ptr = alloc_priv(size, dev->uclass->uc_drv->flags); |
Simon Glass | ad337e6 | 2020-12-19 10:40:08 -0700 | [diff] [blame] | 354 | if (!ptr) |
| 355 | return -ENOMEM; |
Simon Glass | 2543b16 | 2020-12-22 19:30:29 -0700 | [diff] [blame] | 356 | dev_set_uclass_priv(dev, ptr); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Simon Glass | a8d8f16 | 2019-12-29 21:19:18 -0700 | [diff] [blame] | 359 | /* Allocate parent data for this child */ |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 360 | if (dev->parent) { |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 361 | size = dev->parent->driver->per_child_auto; |
Simon Glass | 2543b16 | 2020-12-22 19:30:29 -0700 | [diff] [blame] | 362 | if (!size) |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 363 | size = dev->parent->uclass->uc_drv->per_child_auto; |
Simon Glass | 2543b16 | 2020-12-22 19:30:29 -0700 | [diff] [blame] | 364 | if (size && !dev_get_parent_priv(dev)) { |
| 365 | ptr = alloc_priv(size, drv->flags); |
Simon Glass | ad337e6 | 2020-12-19 10:40:08 -0700 | [diff] [blame] | 366 | if (!ptr) |
| 367 | return -ENOMEM; |
Simon Glass | 2543b16 | 2020-12-22 19:30:29 -0700 | [diff] [blame] | 368 | dev_set_parent_priv(dev, ptr); |
Simon Glass | 60d971b | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 369 | } |
Simon Glass | a8d8f16 | 2019-12-29 21:19:18 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Simon Glass | ad337e6 | 2020-12-19 10:40:08 -0700 | [diff] [blame] | 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | int device_of_to_plat(struct udevice *dev) |
| 376 | { |
| 377 | const struct driver *drv; |
| 378 | int ret; |
| 379 | |
| 380 | if (!dev) |
| 381 | return -EINVAL; |
| 382 | |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 383 | if (dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID) |
Simon Glass | ad337e6 | 2020-12-19 10:40:08 -0700 | [diff] [blame] | 384 | return 0; |
| 385 | |
| 386 | /* Ensure all parents have ofdata */ |
| 387 | if (dev->parent) { |
| 388 | ret = device_of_to_plat(dev->parent); |
| 389 | if (ret) |
| 390 | goto fail; |
| 391 | |
| 392 | /* |
| 393 | * The device might have already been probed during |
| 394 | * the call to device_probe() on its parent device |
| 395 | * (e.g. PCI bridge devices). Test the flags again |
| 396 | * so that we don't mess up the device. |
| 397 | */ |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 398 | if (dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID) |
Simon Glass | ad337e6 | 2020-12-19 10:40:08 -0700 | [diff] [blame] | 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | ret = device_alloc_priv(dev); |
| 403 | if (ret) |
| 404 | goto fail; |
| 405 | |
| 406 | drv = dev->driver; |
| 407 | assert(drv); |
| 408 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 409 | if (drv->of_to_plat && |
Simon Glass | 07c1777 | 2020-12-19 10:40:12 -0700 | [diff] [blame] | 410 | (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_ofnode(dev))) { |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 411 | ret = drv->of_to_plat(dev); |
Simon Glass | a8d8f16 | 2019-12-29 21:19:18 -0700 | [diff] [blame] | 412 | if (ret) |
| 413 | goto fail; |
| 414 | } |
Simon Glass | 60d971b | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 415 | |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 416 | dev_or_flags(dev, DM_FLAG_PLATDATA_VALID); |
Simon Glass | d3c9d30 | 2019-12-29 21:19:21 -0700 | [diff] [blame] | 417 | |
Simon Glass | 4a26385 | 2019-12-29 21:19:20 -0700 | [diff] [blame] | 418 | return 0; |
| 419 | fail: |
| 420 | device_free(dev); |
| 421 | |
| 422 | return ret; |
| 423 | } |
| 424 | |
Nicolas Saenz Julienne | a2c7ead | 2021-01-12 13:55:24 +0100 | [diff] [blame] | 425 | /** |
| 426 | * device_get_dma_constraints() - Populate device's DMA constraints |
| 427 | * |
| 428 | * Gets a device's DMA constraints from firmware. This information is later |
| 429 | * used by drivers to translate physcal addresses to the device's bus address |
| 430 | * space. For now only device-tree is supported. |
| 431 | * |
| 432 | * @dev: Pointer to target device |
| 433 | * Return: 0 if OK or if no DMA constraints were found, error otherwise |
| 434 | */ |
| 435 | static int device_get_dma_constraints(struct udevice *dev) |
| 436 | { |
| 437 | struct udevice *parent = dev->parent; |
| 438 | phys_addr_t cpu = 0; |
| 439 | dma_addr_t bus = 0; |
| 440 | u64 size = 0; |
| 441 | int ret; |
| 442 | |
| 443 | if (!CONFIG_IS_ENABLED(DM_DMA) || !parent || !dev_has_ofnode(parent)) |
| 444 | return 0; |
| 445 | |
| 446 | /* |
| 447 | * We start parsing for dma-ranges from the device's bus node. This is |
| 448 | * specially important on nested buses. |
| 449 | */ |
| 450 | ret = dev_get_dma_range(parent, &cpu, &bus, &size); |
| 451 | /* Don't return an error if no 'dma-ranges' were found */ |
| 452 | if (ret && ret != -ENOENT) { |
| 453 | dm_warn("%s: failed to get DMA range, %d\n", dev->name, ret); |
| 454 | return ret; |
| 455 | } |
| 456 | |
| 457 | dev_set_dma_offset(dev, cpu - bus); |
| 458 | |
| 459 | return 0; |
| 460 | } |
| 461 | |
Simon Glass | 4a26385 | 2019-12-29 21:19:20 -0700 | [diff] [blame] | 462 | int device_probe(struct udevice *dev) |
| 463 | { |
| 464 | const struct driver *drv; |
| 465 | int ret; |
Simon Glass | 4a26385 | 2019-12-29 21:19:20 -0700 | [diff] [blame] | 466 | |
| 467 | if (!dev) |
| 468 | return -EINVAL; |
| 469 | |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 470 | if (dev_get_flags(dev) & DM_FLAG_ACTIVATED) |
Simon Glass | 4a26385 | 2019-12-29 21:19:20 -0700 | [diff] [blame] | 471 | return 0; |
| 472 | |
| 473 | drv = dev->driver; |
| 474 | assert(drv); |
| 475 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 476 | ret = device_of_to_plat(dev); |
Simon Glass | 4a26385 | 2019-12-29 21:19:20 -0700 | [diff] [blame] | 477 | if (ret) |
| 478 | goto fail; |
| 479 | |
Simon Glass | a8d8f16 | 2019-12-29 21:19:18 -0700 | [diff] [blame] | 480 | /* Ensure all parents are probed */ |
| 481 | if (dev->parent) { |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 482 | ret = device_probe(dev->parent); |
| 483 | if (ret) |
| 484 | goto fail; |
Bin Meng | 82cdd78 | 2015-08-24 01:14:02 -0700 | [diff] [blame] | 485 | |
| 486 | /* |
| 487 | * The device might have already been probed during |
| 488 | * the call to device_probe() on its parent device |
| 489 | * (e.g. PCI bridge devices). Test the flags again |
| 490 | * so that we don't mess up the device. |
| 491 | */ |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 492 | if (dev_get_flags(dev) & DM_FLAG_ACTIVATED) |
Bin Meng | 82cdd78 | 2015-08-24 01:14:02 -0700 | [diff] [blame] | 493 | return 0; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 494 | } |
Simon Glass | db6f020 | 2014-07-23 06:55:12 -0600 | [diff] [blame] | 495 | |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 496 | dev_or_flags(dev, DM_FLAG_ACTIVATED); |
Simon Glass | 8b04e73 | 2015-03-25 12:21:56 -0600 | [diff] [blame] | 497 | |
Simon Glass | bb53bda | 2015-09-12 08:45:19 -0600 | [diff] [blame] | 498 | /* |
| 499 | * Process pinctrl for everything except the root device, and |
Simon Glass | 27ce962 | 2016-01-21 19:43:25 -0700 | [diff] [blame] | 500 | * continue regardless of the result of pinctrl. Don't process pinctrl |
| 501 | * settings for pinctrl devices since the device may not yet be |
| 502 | * probed. |
Simon Glass | 303c3c2 | 2021-01-21 13:57:13 -0700 | [diff] [blame] | 503 | * |
| 504 | * This call can produce some non-intuitive results. For example, on an |
| 505 | * x86 device where dev is the main PCI bus, the pinctrl device may be |
| 506 | * child or grandchild of that bus, meaning that the child will be |
| 507 | * probed here. If the child happens to be the P2SB and the pinctrl |
| 508 | * device is a child of that, then both the pinctrl and P2SB will be |
| 509 | * probed by this call. This works because the DM_FLAG_ACTIVATED flag |
| 510 | * is set just above. However, the PCI bus' probe() method and |
| 511 | * associated uclass methods have not yet been called. |
Simon Glass | bb53bda | 2015-09-12 08:45:19 -0600 | [diff] [blame] | 512 | */ |
Simon Glass | 27ce962 | 2016-01-21 19:43:25 -0700 | [diff] [blame] | 513 | if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL) |
Simon Glass | bb53bda | 2015-09-12 08:45:19 -0600 | [diff] [blame] | 514 | pinctrl_select_state(dev, "default"); |
Masahiro Yamada | f8efa63 | 2015-08-27 12:44:29 +0900 | [diff] [blame] | 515 | |
Anatolij Gustschin | 63a3467 | 2019-07-14 21:11:01 +0200 | [diff] [blame] | 516 | if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent && |
Lokesh Vutla | c10e52b | 2019-09-27 13:48:12 +0530 | [diff] [blame] | 517 | (device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) && |
| 518 | !(drv->flags & DM_FLAG_DEFAULT_PD_CTRL_OFF)) { |
Peng Fan | d220111 | 2019-09-17 09:29:22 +0000 | [diff] [blame] | 519 | ret = dev_power_domain_on(dev); |
| 520 | if (ret) |
| 521 | goto fail; |
Peng Fan | 6fba32d | 2018-07-27 10:20:38 +0800 | [diff] [blame] | 522 | } |
| 523 | |
Nicolas Saenz Julienne | a2c7ead | 2021-01-12 13:55:24 +0100 | [diff] [blame] | 524 | ret = device_get_dma_constraints(dev); |
| 525 | if (ret) |
| 526 | goto fail; |
| 527 | |
Simon Glass | 9c1f382 | 2015-03-05 12:25:22 -0700 | [diff] [blame] | 528 | ret = uclass_pre_probe_device(dev); |
Simon Glass | 5104b98 | 2015-01-25 08:27:10 -0700 | [diff] [blame] | 529 | if (ret) |
| 530 | goto fail; |
| 531 | |
Simon Glass | d45560d | 2014-07-23 06:55:21 -0600 | [diff] [blame] | 532 | if (dev->parent && dev->parent->driver->child_pre_probe) { |
| 533 | ret = dev->parent->driver->child_pre_probe(dev); |
| 534 | if (ret) |
| 535 | goto fail; |
| 536 | } |
| 537 | |
Bin Meng | 513cf40 | 2019-07-05 09:23:16 -0700 | [diff] [blame] | 538 | /* Only handle devices that have a valid ofnode */ |
Simon Glass | f1d50f7 | 2020-12-19 10:40:13 -0700 | [diff] [blame] | 539 | if (dev_has_ofnode(dev)) { |
Bin Meng | 513cf40 | 2019-07-05 09:23:16 -0700 | [diff] [blame] | 540 | /* |
| 541 | * Process 'assigned-{clocks/clock-parents/clock-rates}' |
| 542 | * properties |
| 543 | */ |
Jean-Jacques Hiblot | 9601f32 | 2019-10-22 14:00:06 +0200 | [diff] [blame] | 544 | ret = clk_set_defaults(dev, 0); |
Bin Meng | 513cf40 | 2019-07-05 09:23:16 -0700 | [diff] [blame] | 545 | if (ret) |
| 546 | goto fail; |
| 547 | } |
Philipp Tomsich | 9cf03b0 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 548 | |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 549 | if (drv->probe) { |
| 550 | ret = drv->probe(dev); |
Simon Glass | 9046dec | 2019-12-29 21:19:16 -0700 | [diff] [blame] | 551 | if (ret) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 552 | goto fail; |
| 553 | } |
| 554 | |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 555 | ret = uclass_post_probe_device(dev); |
Simon Glass | 8b04e73 | 2015-03-25 12:21:56 -0600 | [diff] [blame] | 556 | if (ret) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 557 | goto fail_uclass; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 558 | |
Peng Fan | 7cf58dd | 2016-03-12 13:17:38 +0800 | [diff] [blame] | 559 | if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL) |
| 560 | pinctrl_select_state(dev, "default"); |
| 561 | |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 562 | return 0; |
| 563 | fail_uclass: |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 564 | if (device_remove(dev, DM_REMOVE_NORMAL)) { |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 565 | dm_warn("%s: Device '%s' failed to remove on error path\n", |
| 566 | __func__, dev->name); |
| 567 | } |
| 568 | fail: |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 569 | dev_bic_flags(dev, DM_FLAG_ACTIVATED); |
Simon Glass | 8b04e73 | 2015-03-25 12:21:56 -0600 | [diff] [blame] | 570 | |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 571 | device_free(dev); |
| 572 | |
| 573 | return ret; |
| 574 | } |
| 575 | |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 576 | void *dev_get_plat(const struct udevice *dev) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 577 | { |
| 578 | if (!dev) { |
Simon Glass | df1e072 | 2014-12-10 08:55:56 -0700 | [diff] [blame] | 579 | dm_warn("%s: null device\n", __func__); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 580 | return NULL; |
| 581 | } |
| 582 | |
Simon Glass | 541f62c | 2020-12-22 19:30:30 -0700 | [diff] [blame] | 583 | return dev->plat_; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 584 | } |
| 585 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 586 | void *dev_get_parent_plat(const struct udevice *dev) |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 587 | { |
| 588 | if (!dev) { |
Simon Glass | cdfc505 | 2015-07-06 16:47:40 -0600 | [diff] [blame] | 589 | dm_warn("%s: null device\n", __func__); |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 590 | return NULL; |
| 591 | } |
| 592 | |
Simon Glass | 541f62c | 2020-12-22 19:30:30 -0700 | [diff] [blame] | 593 | return dev->parent_plat_; |
Simon Glass | 11b6173 | 2015-01-25 08:27:01 -0700 | [diff] [blame] | 594 | } |
| 595 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 596 | void *dev_get_uclass_plat(const struct udevice *dev) |
Przemyslaw Marczak | d850e67 | 2015-04-15 13:07:18 +0200 | [diff] [blame] | 597 | { |
| 598 | if (!dev) { |
Simon Glass | cdfc505 | 2015-07-06 16:47:40 -0600 | [diff] [blame] | 599 | dm_warn("%s: null device\n", __func__); |
Przemyslaw Marczak | d850e67 | 2015-04-15 13:07:18 +0200 | [diff] [blame] | 600 | return NULL; |
| 601 | } |
| 602 | |
Simon Glass | 541f62c | 2020-12-22 19:30:30 -0700 | [diff] [blame] | 603 | return dev->uclass_plat_; |
Przemyslaw Marczak | d850e67 | 2015-04-15 13:07:18 +0200 | [diff] [blame] | 604 | } |
| 605 | |
Simon Glass | 2e5e5ec | 2018-10-01 12:22:06 -0600 | [diff] [blame] | 606 | void *dev_get_priv(const struct udevice *dev) |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 607 | { |
| 608 | if (!dev) { |
Simon Glass | df1e072 | 2014-12-10 08:55:56 -0700 | [diff] [blame] | 609 | dm_warn("%s: null device\n", __func__); |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 610 | return NULL; |
| 611 | } |
| 612 | |
Simon Glass | 541f62c | 2020-12-22 19:30:30 -0700 | [diff] [blame] | 613 | return dev->priv_; |
Simon Glass | dd6ab88 | 2014-02-26 15:59:18 -0700 | [diff] [blame] | 614 | } |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 615 | |
Simon Glass | 2e5e5ec | 2018-10-01 12:22:06 -0600 | [diff] [blame] | 616 | void *dev_get_uclass_priv(const struct udevice *dev) |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 617 | { |
| 618 | if (!dev) { |
| 619 | dm_warn("%s: null device\n", __func__); |
| 620 | return NULL; |
| 621 | } |
| 622 | |
Simon Glass | 541f62c | 2020-12-22 19:30:30 -0700 | [diff] [blame] | 623 | return dev->uclass_priv_; |
Simon Glass | de0977b | 2015-03-05 12:25:20 -0700 | [diff] [blame] | 624 | } |
| 625 | |
Simon Glass | 2e5e5ec | 2018-10-01 12:22:06 -0600 | [diff] [blame] | 626 | void *dev_get_parent_priv(const struct udevice *dev) |
Simon Glass | 60d971b | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 627 | { |
| 628 | if (!dev) { |
Simon Glass | df1e072 | 2014-12-10 08:55:56 -0700 | [diff] [blame] | 629 | dm_warn("%s: null device\n", __func__); |
Simon Glass | 60d971b | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 630 | return NULL; |
| 631 | } |
| 632 | |
Simon Glass | 541f62c | 2020-12-22 19:30:30 -0700 | [diff] [blame] | 633 | return dev->parent_priv_; |
Simon Glass | 60d971b | 2014-07-23 06:55:20 -0600 | [diff] [blame] | 634 | } |
| 635 | |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 636 | static int device_get_device_tail(struct udevice *dev, int ret, |
| 637 | struct udevice **devp) |
| 638 | { |
| 639 | if (ret) |
| 640 | return ret; |
| 641 | |
| 642 | ret = device_probe(dev); |
| 643 | if (ret) |
| 644 | return ret; |
| 645 | |
| 646 | *devp = dev; |
| 647 | |
| 648 | return 0; |
| 649 | } |
| 650 | |
Marek Vasut | 7f18c34 | 2019-08-31 18:03:28 +0200 | [diff] [blame] | 651 | #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA) |
Mario Six | 3bedfbe | 2018-06-26 08:46:50 +0200 | [diff] [blame] | 652 | /** |
| 653 | * device_find_by_ofnode() - Return device associated with given ofnode |
| 654 | * |
| 655 | * The returned device is *not* activated. |
| 656 | * |
| 657 | * @node: The ofnode for which a associated device should be looked up |
| 658 | * @devp: Pointer to structure to hold the found device |
| 659 | * Return: 0 if OK, -ve on error |
| 660 | */ |
| 661 | static int device_find_by_ofnode(ofnode node, struct udevice **devp) |
| 662 | { |
| 663 | struct uclass *uc; |
| 664 | struct udevice *dev; |
| 665 | int ret; |
| 666 | |
Simon Glass | 784cd9e | 2020-12-19 10:40:17 -0700 | [diff] [blame] | 667 | list_for_each_entry(uc, gd->uclass_root, sibling_node) { |
Mario Six | 3bedfbe | 2018-06-26 08:46:50 +0200 | [diff] [blame] | 668 | ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, |
| 669 | &dev); |
| 670 | if (!ret || dev) { |
| 671 | *devp = dev; |
| 672 | return 0; |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | return -ENODEV; |
| 677 | } |
Marek Vasut | 7f18c34 | 2019-08-31 18:03:28 +0200 | [diff] [blame] | 678 | #endif |
Mario Six | 3bedfbe | 2018-06-26 08:46:50 +0200 | [diff] [blame] | 679 | |
Simon Glass | 2a58088 | 2020-01-27 08:49:36 -0700 | [diff] [blame] | 680 | int device_get_child(const struct udevice *parent, int index, |
| 681 | struct udevice **devp) |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 682 | { |
| 683 | struct udevice *dev; |
| 684 | |
| 685 | list_for_each_entry(dev, &parent->child_head, sibling_node) { |
| 686 | if (!index--) |
| 687 | return device_get_device_tail(dev, 0, devp); |
| 688 | } |
| 689 | |
| 690 | return -ENODEV; |
| 691 | } |
| 692 | |
Simon Glass | 2a58088 | 2020-01-27 08:49:36 -0700 | [diff] [blame] | 693 | int device_get_child_count(const struct udevice *parent) |
Lokesh Vutla | fa45a08 | 2019-09-04 16:01:26 +0530 | [diff] [blame] | 694 | { |
| 695 | struct udevice *dev; |
| 696 | int count = 0; |
| 697 | |
| 698 | list_for_each_entry(dev, &parent->child_head, sibling_node) |
| 699 | count++; |
| 700 | |
| 701 | return count; |
| 702 | } |
| 703 | |
Simon Glass | 07e1338 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 704 | int device_find_child_by_seq(const struct udevice *parent, int seq, |
| 705 | struct udevice **devp) |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 706 | { |
| 707 | struct udevice *dev; |
| 708 | |
| 709 | *devp = NULL; |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 710 | |
| 711 | list_for_each_entry(dev, &parent->child_head, sibling_node) { |
Simon Glass | 5e34992 | 2020-12-19 10:40:09 -0700 | [diff] [blame] | 712 | if (dev->seq_ == seq) { |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 713 | *devp = dev; |
| 714 | return 0; |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | return -ENODEV; |
| 719 | } |
| 720 | |
Simon Glass | 2a58088 | 2020-01-27 08:49:36 -0700 | [diff] [blame] | 721 | int device_get_child_by_seq(const struct udevice *parent, int seq, |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 722 | struct udevice **devp) |
| 723 | { |
| 724 | struct udevice *dev; |
| 725 | int ret; |
| 726 | |
| 727 | *devp = NULL; |
Simon Glass | 07e1338 | 2020-12-16 21:20:29 -0700 | [diff] [blame] | 728 | ret = device_find_child_by_seq(parent, seq, &dev); |
| 729 | |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 730 | return device_get_device_tail(dev, ret, devp); |
| 731 | } |
| 732 | |
Simon Glass | 2a58088 | 2020-01-27 08:49:36 -0700 | [diff] [blame] | 733 | int device_find_child_by_of_offset(const struct udevice *parent, int of_offset, |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 734 | struct udevice **devp) |
| 735 | { |
| 736 | struct udevice *dev; |
| 737 | |
| 738 | *devp = NULL; |
| 739 | |
| 740 | list_for_each_entry(dev, &parent->child_head, sibling_node) { |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 741 | if (dev_of_offset(dev) == of_offset) { |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 742 | *devp = dev; |
| 743 | return 0; |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | return -ENODEV; |
| 748 | } |
| 749 | |
Simon Glass | 2a58088 | 2020-01-27 08:49:36 -0700 | [diff] [blame] | 750 | int device_get_child_by_of_offset(const struct udevice *parent, int node, |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 751 | struct udevice **devp) |
| 752 | { |
| 753 | struct udevice *dev; |
| 754 | int ret; |
| 755 | |
| 756 | *devp = NULL; |
Simon Glass | 861bc9f | 2015-06-23 15:38:38 -0600 | [diff] [blame] | 757 | ret = device_find_child_by_of_offset(parent, node, &dev); |
Simon Glass | 48d4e29 | 2014-07-23 06:55:19 -0600 | [diff] [blame] | 758 | return device_get_device_tail(dev, ret, devp); |
| 759 | } |
Simon Glass | 44da735 | 2014-10-13 23:41:49 -0600 | [diff] [blame] | 760 | |
Jean-Jacques Hiblot | a7b0d6a | 2018-08-09 16:17:44 +0200 | [diff] [blame] | 761 | static struct udevice *_device_find_global_by_ofnode(struct udevice *parent, |
| 762 | ofnode ofnode) |
Simon Glass | ae2efac | 2015-06-23 15:38:37 -0600 | [diff] [blame] | 763 | { |
| 764 | struct udevice *dev, *found; |
| 765 | |
Jean-Jacques Hiblot | a7b0d6a | 2018-08-09 16:17:44 +0200 | [diff] [blame] | 766 | if (ofnode_equal(dev_ofnode(parent), ofnode)) |
Simon Glass | ae2efac | 2015-06-23 15:38:37 -0600 | [diff] [blame] | 767 | return parent; |
| 768 | |
| 769 | list_for_each_entry(dev, &parent->child_head, sibling_node) { |
Jean-Jacques Hiblot | a7b0d6a | 2018-08-09 16:17:44 +0200 | [diff] [blame] | 770 | found = _device_find_global_by_ofnode(dev, ofnode); |
Simon Glass | ae2efac | 2015-06-23 15:38:37 -0600 | [diff] [blame] | 771 | if (found) |
| 772 | return found; |
| 773 | } |
| 774 | |
| 775 | return NULL; |
| 776 | } |
| 777 | |
Jean-Jacques Hiblot | a7b0d6a | 2018-08-09 16:17:44 +0200 | [diff] [blame] | 778 | int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp) |
| 779 | { |
| 780 | *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode); |
| 781 | |
| 782 | return *devp ? 0 : -ENOENT; |
| 783 | } |
| 784 | |
| 785 | int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp) |
Simon Glass | ae2efac | 2015-06-23 15:38:37 -0600 | [diff] [blame] | 786 | { |
| 787 | struct udevice *dev; |
| 788 | |
Jean-Jacques Hiblot | a7b0d6a | 2018-08-09 16:17:44 +0200 | [diff] [blame] | 789 | dev = _device_find_global_by_ofnode(gd->dm_root, ofnode); |
Simon Glass | ae2efac | 2015-06-23 15:38:37 -0600 | [diff] [blame] | 790 | return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp); |
| 791 | } |
Walter Lozano | ffc41b0 | 2020-06-25 01:10:11 -0300 | [diff] [blame] | 792 | |
| 793 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 794 | int device_get_by_driver_info(const struct driver_info *info, |
| 795 | struct udevice **devp) |
| 796 | { |
Simon Glass | cfd6a00 | 2020-10-03 11:31:33 -0600 | [diff] [blame] | 797 | struct driver_info *info_base = |
| 798 | ll_entry_start(struct driver_info, driver_info); |
| 799 | int idx = info - info_base; |
| 800 | struct driver_rt *drt = gd_dm_driver_rt() + idx; |
Walter Lozano | ffc41b0 | 2020-06-25 01:10:11 -0300 | [diff] [blame] | 801 | struct udevice *dev; |
| 802 | |
Simon Glass | cfd6a00 | 2020-10-03 11:31:33 -0600 | [diff] [blame] | 803 | dev = drt->dev; |
Simon Glass | d825ea7 | 2020-10-03 11:31:31 -0600 | [diff] [blame] | 804 | *devp = NULL; |
Walter Lozano | ffc41b0 | 2020-06-25 01:10:11 -0300 | [diff] [blame] | 805 | |
| 806 | return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp); |
| 807 | } |
Simon Glass | 5792f4b | 2020-10-03 11:31:40 -0600 | [diff] [blame] | 808 | |
| 809 | int device_get_by_driver_info_idx(uint idx, struct udevice **devp) |
| 810 | { |
| 811 | struct driver_rt *drt = gd_dm_driver_rt() + idx; |
| 812 | struct udevice *dev; |
| 813 | |
| 814 | dev = drt->dev; |
| 815 | *devp = NULL; |
| 816 | |
| 817 | return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp); |
| 818 | } |
Walter Lozano | ffc41b0 | 2020-06-25 01:10:11 -0300 | [diff] [blame] | 819 | #endif |
Simon Glass | ae2efac | 2015-06-23 15:38:37 -0600 | [diff] [blame] | 820 | |
Simon Glass | 2a58088 | 2020-01-27 08:49:36 -0700 | [diff] [blame] | 821 | int device_find_first_child(const struct udevice *parent, struct udevice **devp) |
Simon Glass | 44da735 | 2014-10-13 23:41:49 -0600 | [diff] [blame] | 822 | { |
| 823 | if (list_empty(&parent->child_head)) { |
| 824 | *devp = NULL; |
| 825 | } else { |
| 826 | *devp = list_first_entry(&parent->child_head, struct udevice, |
| 827 | sibling_node); |
| 828 | } |
| 829 | |
| 830 | return 0; |
| 831 | } |
| 832 | |
| 833 | int device_find_next_child(struct udevice **devp) |
| 834 | { |
| 835 | struct udevice *dev = *devp; |
| 836 | struct udevice *parent = dev->parent; |
| 837 | |
| 838 | if (list_is_last(&dev->sibling_node, &parent->child_head)) { |
| 839 | *devp = NULL; |
| 840 | } else { |
| 841 | *devp = list_entry(dev->sibling_node.next, struct udevice, |
| 842 | sibling_node); |
| 843 | } |
| 844 | |
| 845 | return 0; |
| 846 | } |
Simon Glass | 70c3a0e | 2014-11-11 10:46:18 -0700 | [diff] [blame] | 847 | |
Simon Glass | 2a58088 | 2020-01-27 08:49:36 -0700 | [diff] [blame] | 848 | int device_find_first_inactive_child(const struct udevice *parent, |
Simon Glass | b775e87 | 2018-10-01 12:22:07 -0600 | [diff] [blame] | 849 | enum uclass_id uclass_id, |
| 850 | struct udevice **devp) |
| 851 | { |
| 852 | struct udevice *dev; |
| 853 | |
| 854 | *devp = NULL; |
| 855 | list_for_each_entry(dev, &parent->child_head, sibling_node) { |
| 856 | if (!device_active(dev) && |
| 857 | device_get_uclass_id(dev) == uclass_id) { |
| 858 | *devp = dev; |
| 859 | return 0; |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | return -ENODEV; |
| 864 | } |
| 865 | |
Simon Glass | 2a58088 | 2020-01-27 08:49:36 -0700 | [diff] [blame] | 866 | int device_find_first_child_by_uclass(const struct udevice *parent, |
Simon Glass | a11341a | 2018-11-18 08:14:31 -0700 | [diff] [blame] | 867 | enum uclass_id uclass_id, |
| 868 | struct udevice **devp) |
| 869 | { |
| 870 | struct udevice *dev; |
| 871 | |
| 872 | *devp = NULL; |
| 873 | list_for_each_entry(dev, &parent->child_head, sibling_node) { |
| 874 | if (device_get_uclass_id(dev) == uclass_id) { |
| 875 | *devp = dev; |
| 876 | return 0; |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | return -ENODEV; |
| 881 | } |
| 882 | |
Simon Glass | 2a58088 | 2020-01-27 08:49:36 -0700 | [diff] [blame] | 883 | int device_find_child_by_name(const struct udevice *parent, const char *name, |
Simon Glass | a11341a | 2018-11-18 08:14:31 -0700 | [diff] [blame] | 884 | struct udevice **devp) |
| 885 | { |
| 886 | struct udevice *dev; |
| 887 | |
| 888 | *devp = NULL; |
| 889 | |
| 890 | list_for_each_entry(dev, &parent->child_head, sibling_node) { |
| 891 | if (!strcmp(dev->name, name)) { |
| 892 | *devp = dev; |
| 893 | return 0; |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | return -ENODEV; |
| 898 | } |
| 899 | |
Simon Glass | 76db9b7 | 2020-01-27 08:49:48 -0700 | [diff] [blame] | 900 | int device_first_child_err(struct udevice *parent, struct udevice **devp) |
| 901 | { |
| 902 | struct udevice *dev; |
| 903 | |
| 904 | device_find_first_child(parent, &dev); |
| 905 | if (!dev) |
| 906 | return -ENODEV; |
| 907 | |
| 908 | return device_get_device_tail(dev, 0, devp); |
| 909 | } |
| 910 | |
| 911 | int device_next_child_err(struct udevice **devp) |
| 912 | { |
| 913 | struct udevice *dev = *devp; |
| 914 | |
| 915 | device_find_next_child(&dev); |
| 916 | if (!dev) |
| 917 | return -ENODEV; |
| 918 | |
| 919 | return device_get_device_tail(dev, 0, devp); |
| 920 | } |
| 921 | |
Simon Glass | 000676b | 2020-01-27 08:49:47 -0700 | [diff] [blame] | 922 | int device_first_child_ofdata_err(struct udevice *parent, struct udevice **devp) |
| 923 | { |
| 924 | struct udevice *dev; |
| 925 | int ret; |
| 926 | |
| 927 | device_find_first_child(parent, &dev); |
| 928 | if (!dev) |
| 929 | return -ENODEV; |
| 930 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 931 | ret = device_of_to_plat(dev); |
Simon Glass | 000676b | 2020-01-27 08:49:47 -0700 | [diff] [blame] | 932 | if (ret) |
| 933 | return ret; |
| 934 | |
| 935 | *devp = dev; |
| 936 | |
| 937 | return 0; |
| 938 | } |
| 939 | |
| 940 | int device_next_child_ofdata_err(struct udevice **devp) |
| 941 | { |
| 942 | struct udevice *dev = *devp; |
| 943 | int ret; |
| 944 | |
| 945 | device_find_next_child(&dev); |
| 946 | if (!dev) |
| 947 | return -ENODEV; |
| 948 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 949 | ret = device_of_to_plat(dev); |
Simon Glass | 000676b | 2020-01-27 08:49:47 -0700 | [diff] [blame] | 950 | if (ret) |
| 951 | return ret; |
| 952 | |
| 953 | *devp = dev; |
| 954 | |
| 955 | return 0; |
| 956 | } |
| 957 | |
Simon Glass | 2e5e5ec | 2018-10-01 12:22:06 -0600 | [diff] [blame] | 958 | struct udevice *dev_get_parent(const struct udevice *child) |
Simon Glass | 43f488a | 2014-11-11 10:46:19 -0700 | [diff] [blame] | 959 | { |
| 960 | return child->parent; |
| 961 | } |
| 962 | |
Simon Glass | 2e5e5ec | 2018-10-01 12:22:06 -0600 | [diff] [blame] | 963 | ulong dev_get_driver_data(const struct udevice *dev) |
Simon Glass | 70c3a0e | 2014-11-11 10:46:18 -0700 | [diff] [blame] | 964 | { |
Simon Glass | 46227bd | 2015-03-25 12:21:55 -0600 | [diff] [blame] | 965 | return dev->driver_data; |
Simon Glass | 70c3a0e | 2014-11-11 10:46:18 -0700 | [diff] [blame] | 966 | } |
Simon Glass | 98fd5d1 | 2015-01-25 08:27:04 -0700 | [diff] [blame] | 967 | |
Simon Glass | 2e5e5ec | 2018-10-01 12:22:06 -0600 | [diff] [blame] | 968 | const void *dev_get_driver_ops(const struct udevice *dev) |
Przemyslaw Marczak | d3ef0d7 | 2015-04-15 13:07:24 +0200 | [diff] [blame] | 969 | { |
| 970 | if (!dev || !dev->driver->ops) |
| 971 | return NULL; |
| 972 | |
| 973 | return dev->driver->ops; |
| 974 | } |
| 975 | |
Simon Glass | 2e5e5ec | 2018-10-01 12:22:06 -0600 | [diff] [blame] | 976 | enum uclass_id device_get_uclass_id(const struct udevice *dev) |
Simon Glass | 98fd5d1 | 2015-01-25 08:27:04 -0700 | [diff] [blame] | 977 | { |
| 978 | return dev->uclass->uc_drv->id; |
| 979 | } |
Peng Fan | 99b7235 | 2015-02-10 14:46:32 +0800 | [diff] [blame] | 980 | |
Simon Glass | 2e5e5ec | 2018-10-01 12:22:06 -0600 | [diff] [blame] | 981 | const char *dev_get_uclass_name(const struct udevice *dev) |
Przemyslaw Marczak | 5ed2e42 | 2015-04-15 13:07:25 +0200 | [diff] [blame] | 982 | { |
| 983 | if (!dev) |
| 984 | return NULL; |
| 985 | |
| 986 | return dev->uclass->uc_drv->name; |
| 987 | } |
| 988 | |
Simon Glass | 2e5e5ec | 2018-10-01 12:22:06 -0600 | [diff] [blame] | 989 | bool device_has_children(const struct udevice *dev) |
Simon Glass | 40f829a | 2015-03-25 12:21:57 -0600 | [diff] [blame] | 990 | { |
| 991 | return !list_empty(&dev->child_head); |
| 992 | } |
| 993 | |
Simon Glass | 2a58088 | 2020-01-27 08:49:36 -0700 | [diff] [blame] | 994 | bool device_has_active_children(const struct udevice *dev) |
Simon Glass | 40f829a | 2015-03-25 12:21:57 -0600 | [diff] [blame] | 995 | { |
| 996 | struct udevice *child; |
| 997 | |
| 998 | for (device_find_first_child(dev, &child); |
| 999 | child; |
| 1000 | device_find_next_child(&child)) { |
| 1001 | if (device_active(child)) |
| 1002 | return true; |
| 1003 | } |
| 1004 | |
| 1005 | return false; |
| 1006 | } |
| 1007 | |
Simon Glass | 2a58088 | 2020-01-27 08:49:36 -0700 | [diff] [blame] | 1008 | bool device_is_last_sibling(const struct udevice *dev) |
Simon Glass | 40f829a | 2015-03-25 12:21:57 -0600 | [diff] [blame] | 1009 | { |
| 1010 | struct udevice *parent = dev->parent; |
| 1011 | |
| 1012 | if (!parent) |
| 1013 | return false; |
| 1014 | return list_is_last(&dev->sibling_node, &parent->child_head); |
| 1015 | } |
Simon Glass | e3b23e2 | 2015-07-30 13:40:39 -0600 | [diff] [blame] | 1016 | |
Simon Glass | 7760ba2 | 2016-05-01 13:52:23 -0600 | [diff] [blame] | 1017 | void device_set_name_alloced(struct udevice *dev) |
| 1018 | { |
Simon Glass | 6211d76 | 2020-12-19 10:40:10 -0700 | [diff] [blame] | 1019 | dev_or_flags(dev, DM_FLAG_NAME_ALLOCED); |
Simon Glass | 7760ba2 | 2016-05-01 13:52:23 -0600 | [diff] [blame] | 1020 | } |
| 1021 | |
Simon Glass | e3b23e2 | 2015-07-30 13:40:39 -0600 | [diff] [blame] | 1022 | int device_set_name(struct udevice *dev, const char *name) |
| 1023 | { |
| 1024 | name = strdup(name); |
| 1025 | if (!name) |
| 1026 | return -ENOMEM; |
| 1027 | dev->name = name; |
Simon Glass | 7760ba2 | 2016-05-01 13:52:23 -0600 | [diff] [blame] | 1028 | device_set_name_alloced(dev); |
Simon Glass | e3b23e2 | 2015-07-30 13:40:39 -0600 | [diff] [blame] | 1029 | |
| 1030 | return 0; |
| 1031 | } |
Mugunthan V N | 4666bd9 | 2016-04-28 15:36:02 +0530 | [diff] [blame] | 1032 | |
Simon Glass | d4f0c94 | 2020-12-22 19:30:27 -0700 | [diff] [blame] | 1033 | void dev_set_priv(struct udevice *dev, void *priv) |
| 1034 | { |
Simon Glass | 541f62c | 2020-12-22 19:30:30 -0700 | [diff] [blame] | 1035 | dev->priv_ = priv; |
Simon Glass | d4f0c94 | 2020-12-22 19:30:27 -0700 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | void dev_set_parent_priv(struct udevice *dev, void *parent_priv) |
| 1039 | { |
Simon Glass | 541f62c | 2020-12-22 19:30:30 -0700 | [diff] [blame] | 1040 | dev->parent_priv_ = parent_priv; |
Simon Glass | d4f0c94 | 2020-12-22 19:30:27 -0700 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | void dev_set_uclass_priv(struct udevice *dev, void *uclass_priv) |
| 1044 | { |
Simon Glass | 541f62c | 2020-12-22 19:30:30 -0700 | [diff] [blame] | 1045 | dev->uclass_priv_ = uclass_priv; |
Simon Glass | d4f0c94 | 2020-12-22 19:30:27 -0700 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | void dev_set_plat(struct udevice *dev, void *plat) |
| 1049 | { |
Simon Glass | 541f62c | 2020-12-22 19:30:30 -0700 | [diff] [blame] | 1050 | dev->plat_ = plat; |
Simon Glass | d4f0c94 | 2020-12-22 19:30:27 -0700 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | void dev_set_parent_plat(struct udevice *dev, void *parent_plat) |
| 1054 | { |
Simon Glass | 541f62c | 2020-12-22 19:30:30 -0700 | [diff] [blame] | 1055 | dev->parent_plat_ = parent_plat; |
Simon Glass | d4f0c94 | 2020-12-22 19:30:27 -0700 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | void dev_set_uclass_plat(struct udevice *dev, void *uclass_plat) |
| 1059 | { |
Simon Glass | 541f62c | 2020-12-22 19:30:30 -0700 | [diff] [blame] | 1060 | dev->uclass_plat_ = uclass_plat; |
Simon Glass | d4f0c94 | 2020-12-22 19:30:27 -0700 | [diff] [blame] | 1061 | } |
| 1062 | |
Marek Vasut | 7f18c34 | 2019-08-31 18:03:28 +0200 | [diff] [blame] | 1063 | #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | 2a58088 | 2020-01-27 08:49:36 -0700 | [diff] [blame] | 1064 | bool device_is_compatible(const struct udevice *dev, const char *compat) |
Mugunthan V N | 4666bd9 | 2016-04-28 15:36:02 +0530 | [diff] [blame] | 1065 | { |
Masahiro Yamada | 9349bcc | 2018-04-19 12:14:02 +0900 | [diff] [blame] | 1066 | return ofnode_device_is_compatible(dev_ofnode(dev), compat); |
Mugunthan V N | 4666bd9 | 2016-04-28 15:36:02 +0530 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | bool of_machine_is_compatible(const char *compat) |
| 1070 | { |
| 1071 | const void *fdt = gd->fdt_blob; |
| 1072 | |
| 1073 | return !fdt_node_check_compatible(fdt, 0, compat); |
| 1074 | } |
Mario Six | 3bedfbe | 2018-06-26 08:46:50 +0200 | [diff] [blame] | 1075 | |
| 1076 | int dev_disable_by_path(const char *path) |
| 1077 | { |
| 1078 | struct uclass *uc; |
| 1079 | ofnode node = ofnode_path(path); |
| 1080 | struct udevice *dev; |
| 1081 | int ret = 1; |
| 1082 | |
| 1083 | if (!of_live_active()) |
| 1084 | return -ENOSYS; |
| 1085 | |
Simon Glass | 784cd9e | 2020-12-19 10:40:17 -0700 | [diff] [blame] | 1086 | list_for_each_entry(uc, gd->uclass_root, sibling_node) { |
Mario Six | 3bedfbe | 2018-06-26 08:46:50 +0200 | [diff] [blame] | 1087 | ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev); |
| 1088 | if (!ret) |
| 1089 | break; |
| 1090 | } |
| 1091 | |
| 1092 | if (ret) |
| 1093 | return ret; |
| 1094 | |
| 1095 | ret = device_remove(dev, DM_REMOVE_NORMAL); |
| 1096 | if (ret) |
| 1097 | return ret; |
| 1098 | |
| 1099 | ret = device_unbind(dev); |
| 1100 | if (ret) |
| 1101 | return ret; |
| 1102 | |
| 1103 | return ofnode_set_enabled(node, false); |
| 1104 | } |
| 1105 | |
| 1106 | int dev_enable_by_path(const char *path) |
| 1107 | { |
| 1108 | ofnode node = ofnode_path(path); |
| 1109 | ofnode pnode = ofnode_get_parent(node); |
| 1110 | struct udevice *parent; |
| 1111 | int ret = 1; |
| 1112 | |
| 1113 | if (!of_live_active()) |
| 1114 | return -ENOSYS; |
| 1115 | |
| 1116 | ret = device_find_by_ofnode(pnode, &parent); |
| 1117 | if (ret) |
| 1118 | return ret; |
| 1119 | |
| 1120 | ret = ofnode_set_enabled(node, true); |
| 1121 | if (ret) |
| 1122 | return ret; |
| 1123 | |
Bin Meng | 9a9b074 | 2018-10-10 22:06:58 -0700 | [diff] [blame] | 1124 | return lists_bind_fdt(parent, node, NULL, false); |
Mario Six | 3bedfbe | 2018-06-26 08:46:50 +0200 | [diff] [blame] | 1125 | } |
Marek Vasut | 7f18c34 | 2019-08-31 18:03:28 +0200 | [diff] [blame] | 1126 | #endif |