blob: ba50d46effeb9b23903a97318012df80b6e4c210 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassdd6ab882014-02-26 15:59:18 -07002/*
3 * Device manager
4 *
5 * Copyright (c) 2013 Google, Inc
6 *
7 * (C) Copyright 2012
8 * Pavel Herrmann <morpheus.ibis@gmail.com>
Simon Glassdd6ab882014-02-26 15:59:18 -07009 */
10
11#include <common.h>
Simon Glass63334482019-11-14 12:57:39 -070012#include <cpu_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Vignesh R0dff3702016-07-06 09:58:55 +053014#include <asm/io.h>
Philipp Tomsich9cf03b02018-01-08 13:59:18 +010015#include <clk.h>
Simon Glassdb6f0202014-07-23 06:55:12 -060016#include <fdtdec.h>
Stefan Roeseadc09052015-09-02 07:41:12 +020017#include <fdt_support.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070018#include <malloc.h>
Simon Glass274e0b02020-05-10 11:39:56 -060019#include <asm/cache.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070020#include <dm/device.h>
21#include <dm/device-internal.h>
22#include <dm/lists.h>
Mario Six1f1c1ec2018-01-15 11:07:20 +010023#include <dm/of_access.h>
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090024#include <dm/pinctrl.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070025#include <dm/platdata.h>
Simon Glass322b2902017-05-18 20:09:05 -060026#include <dm/read.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070027#include <dm/uclass.h>
28#include <dm/uclass-internal.h>
29#include <dm/util.h>
30#include <linux/err.h>
31#include <linux/list.h>
Peng Fan6fba32d2018-07-27 10:20:38 +080032#include <power-domain.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070033
Simon Glassdb6f0202014-07-23 06:55:12 -060034DECLARE_GLOBAL_DATA_PTR;
35
Stephen Warren8c93df12016-05-11 15:26:24 -060036static int device_bind_common(struct udevice *parent, const struct driver *drv,
Simon Glass71fa5b42020-12-03 16:55:18 -070037 const char *name, void *plat,
Simon Glassb0cf4b32017-05-17 17:18:11 -060038 ulong driver_data, ofnode node,
Simon Glass39edb952020-12-03 16:55:19 -070039 uint of_plat_size, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -070040{
Heiko Schocherb74fcb42014-05-22 12:43:05 +020041 struct udevice *dev;
Simon Glassdd6ab882014-02-26 15:59:18 -070042 struct uclass *uc;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +020043 int size, ret = 0;
Simon Glass7272e922020-12-16 21:20:09 -070044 bool auto_seq = true;
Simon Glass2543b162020-12-22 19:30:29 -070045 void *ptr;
Simon Glassdd6ab882014-02-26 15:59:18 -070046
Masahiro Yamadae1cc1f02015-08-27 12:44:28 +090047 if (devp)
48 *devp = NULL;
Simon Glassdd6ab882014-02-26 15:59:18 -070049 if (!name)
50 return -EINVAL;
51
52 ret = uclass_get(drv->id, &uc);
Simon Glass43313de2015-08-30 16:55:16 -060053 if (ret) {
54 debug("Missing uclass for driver %s\n", drv->name);
Simon Glassdd6ab882014-02-26 15:59:18 -070055 return ret;
Simon Glass43313de2015-08-30 16:55:16 -060056 }
Simon Glassdd6ab882014-02-26 15:59:18 -070057
Heiko Schocherb74fcb42014-05-22 12:43:05 +020058 dev = calloc(1, sizeof(struct udevice));
Simon Glassdd6ab882014-02-26 15:59:18 -070059 if (!dev)
60 return -ENOMEM;
61
62 INIT_LIST_HEAD(&dev->sibling_node);
63 INIT_LIST_HEAD(&dev->child_head);
64 INIT_LIST_HEAD(&dev->uclass_node);
Masahiro Yamada029bfca2015-07-25 21:52:37 +090065#ifdef CONFIG_DEVRES
Masahiro Yamada8b15b162015-07-25 21:52:35 +090066 INIT_LIST_HEAD(&dev->devres_head);
Masahiro Yamada029bfca2015-07-25 21:52:37 +090067#endif
Simon Glass2543b162020-12-22 19:30:29 -070068 dev_set_plat(dev, plat);
Stephen Warren8c93df12016-05-11 15:26:24 -060069 dev->driver_data = driver_data;
Simon Glassdd6ab882014-02-26 15:59:18 -070070 dev->name = name;
Simon Glassb0cf4b32017-05-17 17:18:11 -060071 dev->node = node;
Simon Glassdd6ab882014-02-26 15:59:18 -070072 dev->parent = parent;
73 dev->driver = drv;
74 dev->uclass = uc;
Simon Glassdb6f0202014-07-23 06:55:12 -060075
Simon Glass5e349922020-12-19 10:40:09 -070076 dev->seq_ = -1;
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +010077 if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
78 (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS)) {
Simon Glass1bd3deb2015-02-27 22:06:30 -070079 /*
Stefan Roese9d4ce6b2016-03-16 09:58:01 +010080 * Some devices, such as a SPI bus, I2C bus and serial ports
81 * are numbered using aliases.
Stefan Roese9d4ce6b2016-03-16 09:58:01 +010082 */
Simon Glasse422f8c2020-07-09 18:39:24 -060083 if (CONFIG_IS_ENABLED(OF_CONTROL) &&
84 !CONFIG_IS_ENABLED(OF_PLATDATA)) {
Simon Glass7272e922020-12-16 21:20:09 -070085 if (uc->uc_drv->name && ofnode_valid(node)) {
Simon Glass5e349922020-12-19 10:40:09 -070086 if (!dev_read_alias_seq(dev, &dev->seq_))
Simon Glass38040a92020-12-16 21:20:32 -070087 auto_seq = false;
Simon Glass7272e922020-12-16 21:20:09 -070088 }
Simon Glass0ccb0972015-01-25 08:27:05 -070089 }
Simon Glassdb6f0202014-07-23 06:55:12 -060090 }
Simon Glass3a9d5f12020-12-16 21:20:17 -070091 if (auto_seq && !(uc->uc_drv->flags & DM_UC_FLAG_NO_AUTO_SEQ))
Simon Glass5e349922020-12-19 10:40:09 -070092 dev->seq_ = uclass_find_next_free_seq(uc);
Simon Glass1bd3deb2015-02-27 22:06:30 -070093
Simon Glass71fa5b42020-12-03 16:55:18 -070094 if (drv->plat_auto) {
95 bool alloc = !plat;
Simon Glassafbf9b82016-07-04 11:58:18 -060096
97 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
Simon Glass39edb952020-12-03 16:55:19 -070098 if (of_plat_size) {
Simon Glass6211d762020-12-19 10:40:10 -070099 dev_or_flags(dev, DM_FLAG_OF_PLATDATA);
Simon Glass39edb952020-12-03 16:55:19 -0700100 if (of_plat_size < drv->plat_auto)
Simon Glassafbf9b82016-07-04 11:58:18 -0600101 alloc = true;
102 }
103 }
104 if (alloc) {
Simon Glass6211d762020-12-19 10:40:10 -0700105 dev_or_flags(dev, DM_FLAG_ALLOC_PDATA);
Simon Glass2543b162020-12-22 19:30:29 -0700106 ptr = calloc(1, drv->plat_auto);
107 if (!ptr) {
Simon Glassafbf9b82016-07-04 11:58:18 -0600108 ret = -ENOMEM;
109 goto fail_alloc1;
110 }
Simon Glass2543b162020-12-22 19:30:29 -0700111 if (CONFIG_IS_ENABLED(OF_PLATDATA) && plat)
112 memcpy(ptr, plat, of_plat_size);
113 dev_set_plat(dev, ptr);
Simon Glassba750492015-01-25 08:27:00 -0700114 }
115 }
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200116
Simon Glass71fa5b42020-12-03 16:55:18 -0700117 size = uc->uc_drv->per_device_plat_auto;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200118 if (size) {
Simon Glass6211d762020-12-19 10:40:10 -0700119 dev_or_flags(dev, DM_FLAG_ALLOC_UCLASS_PDATA);
Simon Glass2543b162020-12-22 19:30:29 -0700120 ptr = calloc(1, size);
121 if (!ptr) {
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200122 ret = -ENOMEM;
123 goto fail_alloc2;
124 }
Simon Glass2543b162020-12-22 19:30:29 -0700125 dev_set_uclass_plat(dev, ptr);
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200126 }
Simon Glass11b61732015-01-25 08:27:01 -0700127
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200128 if (parent) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700129 size = parent->driver->per_child_plat_auto;
Simon Glass57f95402015-01-25 08:27:02 -0700130 if (!size) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700131 size = parent->uclass->uc_drv->per_child_plat_auto;
Simon Glass57f95402015-01-25 08:27:02 -0700132 }
Simon Glass11b61732015-01-25 08:27:01 -0700133 if (size) {
Simon Glass6211d762020-12-19 10:40:10 -0700134 dev_or_flags(dev, DM_FLAG_ALLOC_PARENT_PDATA);
Simon Glass2543b162020-12-22 19:30:29 -0700135 ptr = calloc(1, size);
136 if (!ptr) {
Simon Glass11b61732015-01-25 08:27:01 -0700137 ret = -ENOMEM;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200138 goto fail_alloc3;
Simon Glass11b61732015-01-25 08:27:01 -0700139 }
Simon Glass2543b162020-12-22 19:30:29 -0700140 dev_set_parent_plat(dev, ptr);
Simon Glass11b61732015-01-25 08:27:01 -0700141 }
Heinrich Schuchardt5246b372020-02-15 21:38:48 +0100142 /* put dev into parent's successor list */
Simon Glassdd6ab882014-02-26 15:59:18 -0700143 list_add_tail(&dev->sibling_node, &parent->child_head);
Heinrich Schuchardt5246b372020-02-15 21:38:48 +0100144 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700145
146 ret = uclass_bind_device(dev);
147 if (ret)
Simon Glass4ebe01b2015-01-25 08:26:59 -0700148 goto fail_uclass_bind;
Simon Glassdd6ab882014-02-26 15:59:18 -0700149
150 /* if we fail to bind we remove device from successors and free it */
151 if (drv->bind) {
152 ret = drv->bind(dev);
Simon Glass4ebe01b2015-01-25 08:26:59 -0700153 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700154 goto fail_bind;
Simon Glassdd6ab882014-02-26 15:59:18 -0700155 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700156 if (parent && parent->driver->child_post_bind) {
157 ret = parent->driver->child_post_bind(dev);
158 if (ret)
159 goto fail_child_post_bind;
160 }
Simon Glass286863d2016-01-05 09:30:59 -0700161 if (uc->uc_drv->post_bind) {
162 ret = uc->uc_drv->post_bind(dev);
163 if (ret)
164 goto fail_uclass_post_bind;
165 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700166
Simon Glassdd6ab882014-02-26 15:59:18 -0700167 if (parent)
Masahiro Yamadaf70f39f2017-09-29 12:31:20 +0900168 pr_debug("Bound device %s to %s\n", dev->name, parent->name);
Masahiro Yamadae1cc1f02015-08-27 12:44:28 +0900169 if (devp)
170 *devp = dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700171
Simon Glass6211d762020-12-19 10:40:10 -0700172 dev_or_flags(dev, DM_FLAG_BOUND);
Masahiro Yamadabdbb5dd2015-07-25 21:52:34 +0900173
Simon Glassdd6ab882014-02-26 15:59:18 -0700174 return 0;
175
Simon Glass286863d2016-01-05 09:30:59 -0700176fail_uclass_post_bind:
177 /* There is no child unbind() method, so no clean-up required */
Simon Glassa4a51a02015-01-25 08:27:03 -0700178fail_child_post_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900179 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700180 if (drv->unbind && drv->unbind(dev)) {
181 dm_warn("unbind() method failed on dev '%s' on error path\n",
182 dev->name);
183 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700184 }
185
Simon Glassdd6ab882014-02-26 15:59:18 -0700186fail_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900187 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700188 if (uclass_unbind_device(dev)) {
189 dm_warn("Failed to unbind dev '%s' on error path\n",
190 dev->name);
191 }
Simon Glass4ebe01b2015-01-25 08:26:59 -0700192 }
193fail_uclass_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900194 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700195 list_del(&dev->sibling_node);
Simon Glass6211d762020-12-19 10:40:10 -0700196 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PARENT_PDATA) {
Simon Glass2543b162020-12-22 19:30:29 -0700197 free(dev_get_parent_plat(dev));
198 dev_set_parent_plat(dev, NULL);
Simon Glasse70a4f42015-02-27 22:06:33 -0700199 }
Simon Glass11b61732015-01-25 08:27:01 -0700200 }
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200201fail_alloc3:
Simon Glass6211d762020-12-19 10:40:10 -0700202 if (dev_get_flags(dev) & DM_FLAG_ALLOC_UCLASS_PDATA) {
Simon Glass2543b162020-12-22 19:30:29 -0700203 free(dev_get_uclass_plat(dev));
204 dev_set_uclass_plat(dev, NULL);
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200205 }
Simon Glass11b61732015-01-25 08:27:01 -0700206fail_alloc2:
Simon Glass6211d762020-12-19 10:40:10 -0700207 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PDATA) {
Simon Glass2543b162020-12-22 19:30:29 -0700208 free(dev_get_plat(dev));
209 dev_set_plat(dev, NULL);
Simon Glassba750492015-01-25 08:27:00 -0700210 }
211fail_alloc1:
Masahiro Yamada8b15b162015-07-25 21:52:35 +0900212 devres_release_all(dev);
213
Simon Glassdd6ab882014-02-26 15:59:18 -0700214 free(dev);
Simon Glass4ebe01b2015-01-25 08:26:59 -0700215
Simon Glassdd6ab882014-02-26 15:59:18 -0700216 return ret;
217}
218
Stephen Warren8c93df12016-05-11 15:26:24 -0600219int device_bind_with_driver_data(struct udevice *parent,
220 const struct driver *drv, const char *name,
Simon Glass322b2902017-05-18 20:09:05 -0600221 ulong driver_data, ofnode node,
Stephen Warren8c93df12016-05-11 15:26:24 -0600222 struct udevice **devp)
223{
Simon Glass322b2902017-05-18 20:09:05 -0600224 return device_bind_common(parent, drv, name, NULL, driver_data, node,
225 0, devp);
Stephen Warren8c93df12016-05-11 15:26:24 -0600226}
227
Simon Glass884870f2020-11-28 17:50:01 -0700228int device_bind(struct udevice *parent, const struct driver *drv,
Simon Glass71fa5b42020-12-03 16:55:18 -0700229 const char *name, void *plat, ofnode node,
Simon Glass884870f2020-11-28 17:50:01 -0700230 struct udevice **devp)
Simon Glass9eb151e2018-06-11 13:07:15 -0600231{
Simon Glass71fa5b42020-12-03 16:55:18 -0700232 return device_bind_common(parent, drv, name, plat, 0, node, 0,
Simon Glass9eb151e2018-06-11 13:07:15 -0600233 devp);
234}
235
Simon Glassfef72b72014-07-23 06:55:03 -0600236int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
Simon Glasscfd6a002020-10-03 11:31:33 -0600237 const struct driver_info *info, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700238{
239 struct driver *drv;
Simon Glass39edb952020-12-03 16:55:19 -0700240 uint plat_size = 0;
Walter Lozanoffc41b02020-06-25 01:10:11 -0300241 int ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700242
243 drv = lists_driver_lookup_name(info->name);
244 if (!drv)
245 return -ENOENT;
Simon Glassfef72b72014-07-23 06:55:03 -0600246 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
247 return -EPERM;
Simon Glassdd6ab882014-02-26 15:59:18 -0700248
Simon Glassafbf9b82016-07-04 11:58:18 -0600249#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass39edb952020-12-03 16:55:19 -0700250 plat_size = info->plat_size;
Simon Glassafbf9b82016-07-04 11:58:18 -0600251#endif
Simon Glass71fa5b42020-12-03 16:55:18 -0700252 ret = device_bind_common(parent, drv, info->name, (void *)info->plat, 0,
Simon Glass39edb952020-12-03 16:55:19 -0700253 ofnode_null(), plat_size, devp);
Walter Lozanoffc41b02020-06-25 01:10:11 -0300254 if (ret)
255 return ret;
Walter Lozanoffc41b02020-06-25 01:10:11 -0300256
257 return ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700258}
259
Claudiu Bezneabf5b8232020-09-07 17:46:33 +0300260int device_reparent(struct udevice *dev, struct udevice *new_parent)
261{
262 struct udevice *pos, *n;
263
264 assert(dev);
265 assert(new_parent);
266
267 list_for_each_entry_safe(pos, n, &dev->parent->child_head,
268 sibling_node) {
269 if (pos->driver != dev->driver)
270 continue;
271
272 list_del(&dev->sibling_node);
273 list_add_tail(&dev->sibling_node, &new_parent->child_head);
274 dev->parent = new_parent;
275
276 break;
277 }
278
279 return 0;
280}
281
Simon Glass825d3f92015-03-25 12:21:53 -0600282static void *alloc_priv(int size, uint flags)
283{
284 void *priv;
285
286 if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
Faiz Abbas03020a92017-09-19 16:53:50 +0530287 size = ROUND(size, ARCH_DMA_MINALIGN);
Simon Glass825d3f92015-03-25 12:21:53 -0600288 priv = memalign(ARCH_DMA_MINALIGN, size);
Simon Glass27bc9b12017-04-04 13:00:19 -0600289 if (priv) {
Simon Glass825d3f92015-03-25 12:21:53 -0600290 memset(priv, '\0', size);
Simon Glass27bc9b12017-04-04 13:00:19 -0600291
292 /*
293 * Ensure that the zero bytes are flushed to memory.
294 * This prevents problems if the driver uses this as
295 * both an input and an output buffer:
296 *
297 * 1. Zeroes written to buffer (here) and sit in the
298 * cache
299 * 2. Driver issues a read command to DMA
300 * 3. CPU runs out of cache space and evicts some cache
301 * data in the buffer, writing zeroes to RAM from
302 * the memset() above
303 * 4. DMA completes
304 * 5. Buffer now has some DMA data and some zeroes
305 * 6. Data being read is now incorrect
306 *
307 * To prevent this, ensure that the cache is clean
308 * within this range at the start. The driver can then
309 * use normal flush-after-write, invalidate-before-read
310 * procedures.
311 *
312 * TODO(sjg@chromium.org): Drop this microblaze
313 * exception.
314 */
315#ifndef CONFIG_MICROBLAZE
316 flush_dcache_range((ulong)priv, (ulong)priv + size);
317#endif
318 }
Simon Glass825d3f92015-03-25 12:21:53 -0600319 } else {
320 priv = calloc(1, size);
321 }
322
323 return priv;
324}
325
Simon Glassad337e62020-12-19 10:40:08 -0700326/**
327 * device_alloc_priv() - Allocate priv/plat data required by the device
328 *
329 * @dev: Device to process
330 * @return 0 if OK, -ENOMEM if out of memory
331 */
332static int device_alloc_priv(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700333{
Simon Glassa626dff2015-03-25 12:21:54 -0600334 const struct driver *drv;
Simon Glass2543b162020-12-22 19:30:29 -0700335 void *ptr;
Simon Glassad337e62020-12-19 10:40:08 -0700336 int size;
Simon Glass44c47892020-04-05 15:38:19 -0600337
Simon Glassdd6ab882014-02-26 15:59:18 -0700338 drv = dev->driver;
339 assert(drv);
340
Bin Meng82cdd782015-08-24 01:14:02 -0700341 /* Allocate private data if requested and not reentered */
Simon Glass2543b162020-12-22 19:30:29 -0700342 if (drv->priv_auto && !dev_get_priv(dev)) {
343 ptr = alloc_priv(drv->priv_auto, drv->flags);
Simon Glassad337e62020-12-19 10:40:08 -0700344 if (!ptr)
345 return -ENOMEM;
Simon Glass2543b162020-12-22 19:30:29 -0700346 dev_set_priv(dev, ptr);
Simon Glassdd6ab882014-02-26 15:59:18 -0700347 }
Simon Glassad337e62020-12-19 10:40:08 -0700348
Bin Meng82cdd782015-08-24 01:14:02 -0700349 /* Allocate private data if requested and not reentered */
Simon Glass8a2b47f2020-12-03 16:55:17 -0700350 size = dev->uclass->uc_drv->per_device_auto;
Simon Glass2543b162020-12-22 19:30:29 -0700351 if (size && !dev_get_uclass_priv(dev)) {
352 ptr = alloc_priv(size, dev->uclass->uc_drv->flags);
Simon Glassad337e62020-12-19 10:40:08 -0700353 if (!ptr)
354 return -ENOMEM;
Simon Glass2543b162020-12-22 19:30:29 -0700355 dev_set_uclass_priv(dev, ptr);
Simon Glassdd6ab882014-02-26 15:59:18 -0700356 }
357
Simon Glassa8d8f162019-12-29 21:19:18 -0700358 /* Allocate parent data for this child */
Simon Glassdd6ab882014-02-26 15:59:18 -0700359 if (dev->parent) {
Simon Glass8a2b47f2020-12-03 16:55:17 -0700360 size = dev->parent->driver->per_child_auto;
Simon Glass2543b162020-12-22 19:30:29 -0700361 if (!size)
Simon Glass8a2b47f2020-12-03 16:55:17 -0700362 size = dev->parent->uclass->uc_drv->per_child_auto;
Simon Glass2543b162020-12-22 19:30:29 -0700363 if (size && !dev_get_parent_priv(dev)) {
364 ptr = alloc_priv(size, drv->flags);
Simon Glassad337e62020-12-19 10:40:08 -0700365 if (!ptr)
366 return -ENOMEM;
Simon Glass2543b162020-12-22 19:30:29 -0700367 dev_set_parent_priv(dev, ptr);
Simon Glass60d971b2014-07-23 06:55:20 -0600368 }
Simon Glassa8d8f162019-12-29 21:19:18 -0700369 }
370
Simon Glassad337e62020-12-19 10:40:08 -0700371 return 0;
372}
373
374int device_of_to_plat(struct udevice *dev)
375{
376 const struct driver *drv;
377 int ret;
378
379 if (!dev)
380 return -EINVAL;
381
Simon Glass6211d762020-12-19 10:40:10 -0700382 if (dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID)
Simon Glassad337e62020-12-19 10:40:08 -0700383 return 0;
384
385 /* Ensure all parents have ofdata */
386 if (dev->parent) {
387 ret = device_of_to_plat(dev->parent);
388 if (ret)
389 goto fail;
390
391 /*
392 * The device might have already been probed during
393 * the call to device_probe() on its parent device
394 * (e.g. PCI bridge devices). Test the flags again
395 * so that we don't mess up the device.
396 */
Simon Glass6211d762020-12-19 10:40:10 -0700397 if (dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID)
Simon Glassad337e62020-12-19 10:40:08 -0700398 return 0;
399 }
400
401 ret = device_alloc_priv(dev);
402 if (ret)
403 goto fail;
404
405 drv = dev->driver;
406 assert(drv);
407
Simon Glassaad29ae2020-12-03 16:55:21 -0700408 if (drv->of_to_plat &&
Simon Glassa8d8f162019-12-29 21:19:18 -0700409 (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_of_node(dev))) {
Simon Glassaad29ae2020-12-03 16:55:21 -0700410 ret = drv->of_to_plat(dev);
Simon Glassa8d8f162019-12-29 21:19:18 -0700411 if (ret)
412 goto fail;
413 }
Simon Glass60d971b2014-07-23 06:55:20 -0600414
Simon Glass6211d762020-12-19 10:40:10 -0700415 dev_or_flags(dev, DM_FLAG_PLATDATA_VALID);
Simon Glassd3c9d302019-12-29 21:19:21 -0700416
Simon Glass4a263852019-12-29 21:19:20 -0700417 return 0;
418fail:
419 device_free(dev);
420
421 return ret;
422}
423
424int device_probe(struct udevice *dev)
425{
426 const struct driver *drv;
427 int ret;
Simon Glass4a263852019-12-29 21:19:20 -0700428
429 if (!dev)
430 return -EINVAL;
431
Simon Glass6211d762020-12-19 10:40:10 -0700432 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
Simon Glass4a263852019-12-29 21:19:20 -0700433 return 0;
434
435 drv = dev->driver;
436 assert(drv);
437
Simon Glassaad29ae2020-12-03 16:55:21 -0700438 ret = device_of_to_plat(dev);
Simon Glass4a263852019-12-29 21:19:20 -0700439 if (ret)
440 goto fail;
441
Simon Glassa8d8f162019-12-29 21:19:18 -0700442 /* Ensure all parents are probed */
443 if (dev->parent) {
Simon Glassdd6ab882014-02-26 15:59:18 -0700444 ret = device_probe(dev->parent);
445 if (ret)
446 goto fail;
Bin Meng82cdd782015-08-24 01:14:02 -0700447
448 /*
449 * The device might have already been probed during
450 * the call to device_probe() on its parent device
451 * (e.g. PCI bridge devices). Test the flags again
452 * so that we don't mess up the device.
453 */
Simon Glass6211d762020-12-19 10:40:10 -0700454 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
Bin Meng82cdd782015-08-24 01:14:02 -0700455 return 0;
Simon Glassdd6ab882014-02-26 15:59:18 -0700456 }
Simon Glassdb6f0202014-07-23 06:55:12 -0600457
Simon Glass6211d762020-12-19 10:40:10 -0700458 dev_or_flags(dev, DM_FLAG_ACTIVATED);
Simon Glass8b04e732015-03-25 12:21:56 -0600459
Simon Glassbb53bda2015-09-12 08:45:19 -0600460 /*
461 * Process pinctrl for everything except the root device, and
Simon Glass27ce9622016-01-21 19:43:25 -0700462 * continue regardless of the result of pinctrl. Don't process pinctrl
463 * settings for pinctrl devices since the device may not yet be
464 * probed.
Simon Glassbb53bda2015-09-12 08:45:19 -0600465 */
Simon Glass27ce9622016-01-21 19:43:25 -0700466 if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
Simon Glassbb53bda2015-09-12 08:45:19 -0600467 pinctrl_select_state(dev, "default");
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900468
Anatolij Gustschin63a34672019-07-14 21:11:01 +0200469 if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
Lokesh Vutlac10e52b2019-09-27 13:48:12 +0530470 (device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) &&
471 !(drv->flags & DM_FLAG_DEFAULT_PD_CTRL_OFF)) {
Peng Fand2201112019-09-17 09:29:22 +0000472 ret = dev_power_domain_on(dev);
473 if (ret)
474 goto fail;
Peng Fan6fba32d2018-07-27 10:20:38 +0800475 }
476
Simon Glass9c1f3822015-03-05 12:25:22 -0700477 ret = uclass_pre_probe_device(dev);
Simon Glass5104b982015-01-25 08:27:10 -0700478 if (ret)
479 goto fail;
480
Simon Glassd45560d2014-07-23 06:55:21 -0600481 if (dev->parent && dev->parent->driver->child_pre_probe) {
482 ret = dev->parent->driver->child_pre_probe(dev);
483 if (ret)
484 goto fail;
485 }
486
Bin Meng513cf402019-07-05 09:23:16 -0700487 /* Only handle devices that have a valid ofnode */
488 if (dev_of_valid(dev)) {
489 /*
490 * Process 'assigned-{clocks/clock-parents/clock-rates}'
491 * properties
492 */
Jean-Jacques Hiblot9601f322019-10-22 14:00:06 +0200493 ret = clk_set_defaults(dev, 0);
Bin Meng513cf402019-07-05 09:23:16 -0700494 if (ret)
495 goto fail;
496 }
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100497
Simon Glassdd6ab882014-02-26 15:59:18 -0700498 if (drv->probe) {
499 ret = drv->probe(dev);
Simon Glass9046dec2019-12-29 21:19:16 -0700500 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700501 goto fail;
502 }
503
Simon Glassdd6ab882014-02-26 15:59:18 -0700504 ret = uclass_post_probe_device(dev);
Simon Glass8b04e732015-03-25 12:21:56 -0600505 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700506 goto fail_uclass;
Simon Glassdd6ab882014-02-26 15:59:18 -0700507
Peng Fan7cf58dd2016-03-12 13:17:38 +0800508 if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
509 pinctrl_select_state(dev, "default");
510
Simon Glassdd6ab882014-02-26 15:59:18 -0700511 return 0;
512fail_uclass:
Stefan Roese80b5bc92017-03-20 12:51:48 +0100513 if (device_remove(dev, DM_REMOVE_NORMAL)) {
Simon Glassdd6ab882014-02-26 15:59:18 -0700514 dm_warn("%s: Device '%s' failed to remove on error path\n",
515 __func__, dev->name);
516 }
517fail:
Simon Glass6211d762020-12-19 10:40:10 -0700518 dev_bic_flags(dev, DM_FLAG_ACTIVATED);
Simon Glass8b04e732015-03-25 12:21:56 -0600519
Simon Glassdd6ab882014-02-26 15:59:18 -0700520 device_free(dev);
521
522 return ret;
523}
524
Simon Glassfa20e932020-12-03 16:55:20 -0700525void *dev_get_plat(const struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700526{
527 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700528 dm_warn("%s: null device\n", __func__);
Simon Glassdd6ab882014-02-26 15:59:18 -0700529 return NULL;
530 }
531
Simon Glass541f62c2020-12-22 19:30:30 -0700532 return dev->plat_;
Simon Glassdd6ab882014-02-26 15:59:18 -0700533}
534
Simon Glass71fa5b42020-12-03 16:55:18 -0700535void *dev_get_parent_plat(const struct udevice *dev)
Simon Glass11b61732015-01-25 08:27:01 -0700536{
537 if (!dev) {
Simon Glasscdfc5052015-07-06 16:47:40 -0600538 dm_warn("%s: null device\n", __func__);
Simon Glass11b61732015-01-25 08:27:01 -0700539 return NULL;
540 }
541
Simon Glass541f62c2020-12-22 19:30:30 -0700542 return dev->parent_plat_;
Simon Glass11b61732015-01-25 08:27:01 -0700543}
544
Simon Glass71fa5b42020-12-03 16:55:18 -0700545void *dev_get_uclass_plat(const struct udevice *dev)
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200546{
547 if (!dev) {
Simon Glasscdfc5052015-07-06 16:47:40 -0600548 dm_warn("%s: null device\n", __func__);
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200549 return NULL;
550 }
551
Simon Glass541f62c2020-12-22 19:30:30 -0700552 return dev->uclass_plat_;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200553}
554
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600555void *dev_get_priv(const struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700556{
557 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700558 dm_warn("%s: null device\n", __func__);
Simon Glassdd6ab882014-02-26 15:59:18 -0700559 return NULL;
560 }
561
Simon Glass541f62c2020-12-22 19:30:30 -0700562 return dev->priv_;
Simon Glassdd6ab882014-02-26 15:59:18 -0700563}
Simon Glass48d4e292014-07-23 06:55:19 -0600564
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600565void *dev_get_uclass_priv(const struct udevice *dev)
Simon Glassde0977b2015-03-05 12:25:20 -0700566{
567 if (!dev) {
568 dm_warn("%s: null device\n", __func__);
569 return NULL;
570 }
571
Simon Glass541f62c2020-12-22 19:30:30 -0700572 return dev->uclass_priv_;
Simon Glassde0977b2015-03-05 12:25:20 -0700573}
574
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600575void *dev_get_parent_priv(const struct udevice *dev)
Simon Glass60d971b2014-07-23 06:55:20 -0600576{
577 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700578 dm_warn("%s: null device\n", __func__);
Simon Glass60d971b2014-07-23 06:55:20 -0600579 return NULL;
580 }
581
Simon Glass541f62c2020-12-22 19:30:30 -0700582 return dev->parent_priv_;
Simon Glass60d971b2014-07-23 06:55:20 -0600583}
584
Simon Glass48d4e292014-07-23 06:55:19 -0600585static int device_get_device_tail(struct udevice *dev, int ret,
586 struct udevice **devp)
587{
588 if (ret)
589 return ret;
590
591 ret = device_probe(dev);
592 if (ret)
593 return ret;
594
595 *devp = dev;
596
597 return 0;
598}
599
Marek Vasut7f18c342019-08-31 18:03:28 +0200600#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Mario Six3bedfbe2018-06-26 08:46:50 +0200601/**
602 * device_find_by_ofnode() - Return device associated with given ofnode
603 *
604 * The returned device is *not* activated.
605 *
606 * @node: The ofnode for which a associated device should be looked up
607 * @devp: Pointer to structure to hold the found device
608 * Return: 0 if OK, -ve on error
609 */
610static int device_find_by_ofnode(ofnode node, struct udevice **devp)
611{
612 struct uclass *uc;
613 struct udevice *dev;
614 int ret;
615
616 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
617 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node,
618 &dev);
619 if (!ret || dev) {
620 *devp = dev;
621 return 0;
622 }
623 }
624
625 return -ENODEV;
626}
Marek Vasut7f18c342019-08-31 18:03:28 +0200627#endif
Mario Six3bedfbe2018-06-26 08:46:50 +0200628
Simon Glass2a580882020-01-27 08:49:36 -0700629int device_get_child(const struct udevice *parent, int index,
630 struct udevice **devp)
Simon Glass48d4e292014-07-23 06:55:19 -0600631{
632 struct udevice *dev;
633
634 list_for_each_entry(dev, &parent->child_head, sibling_node) {
635 if (!index--)
636 return device_get_device_tail(dev, 0, devp);
637 }
638
639 return -ENODEV;
640}
641
Simon Glass2a580882020-01-27 08:49:36 -0700642int device_get_child_count(const struct udevice *parent)
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530643{
644 struct udevice *dev;
645 int count = 0;
646
647 list_for_each_entry(dev, &parent->child_head, sibling_node)
648 count++;
649
650 return count;
651}
652
Simon Glass07e13382020-12-16 21:20:29 -0700653int device_find_child_by_seq(const struct udevice *parent, int seq,
654 struct udevice **devp)
Simon Glass48d4e292014-07-23 06:55:19 -0600655{
656 struct udevice *dev;
657
658 *devp = NULL;
Simon Glass48d4e292014-07-23 06:55:19 -0600659
660 list_for_each_entry(dev, &parent->child_head, sibling_node) {
Simon Glass5e349922020-12-19 10:40:09 -0700661 if (dev->seq_ == seq) {
Simon Glass48d4e292014-07-23 06:55:19 -0600662 *devp = dev;
663 return 0;
664 }
665 }
666
667 return -ENODEV;
668}
669
Simon Glass2a580882020-01-27 08:49:36 -0700670int device_get_child_by_seq(const struct udevice *parent, int seq,
Simon Glass48d4e292014-07-23 06:55:19 -0600671 struct udevice **devp)
672{
673 struct udevice *dev;
674 int ret;
675
676 *devp = NULL;
Simon Glass07e13382020-12-16 21:20:29 -0700677 ret = device_find_child_by_seq(parent, seq, &dev);
678
Simon Glass48d4e292014-07-23 06:55:19 -0600679 return device_get_device_tail(dev, ret, devp);
680}
681
Simon Glass2a580882020-01-27 08:49:36 -0700682int device_find_child_by_of_offset(const struct udevice *parent, int of_offset,
Simon Glass48d4e292014-07-23 06:55:19 -0600683 struct udevice **devp)
684{
685 struct udevice *dev;
686
687 *devp = NULL;
688
689 list_for_each_entry(dev, &parent->child_head, sibling_node) {
Simon Glassdd79d6e2017-01-17 16:52:55 -0700690 if (dev_of_offset(dev) == of_offset) {
Simon Glass48d4e292014-07-23 06:55:19 -0600691 *devp = dev;
692 return 0;
693 }
694 }
695
696 return -ENODEV;
697}
698
Simon Glass2a580882020-01-27 08:49:36 -0700699int device_get_child_by_of_offset(const struct udevice *parent, int node,
Simon Glass48d4e292014-07-23 06:55:19 -0600700 struct udevice **devp)
701{
702 struct udevice *dev;
703 int ret;
704
705 *devp = NULL;
Simon Glass861bc9f2015-06-23 15:38:38 -0600706 ret = device_find_child_by_of_offset(parent, node, &dev);
Simon Glass48d4e292014-07-23 06:55:19 -0600707 return device_get_device_tail(dev, ret, devp);
708}
Simon Glass44da7352014-10-13 23:41:49 -0600709
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200710static struct udevice *_device_find_global_by_ofnode(struct udevice *parent,
711 ofnode ofnode)
Simon Glassae2efac2015-06-23 15:38:37 -0600712{
713 struct udevice *dev, *found;
714
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200715 if (ofnode_equal(dev_ofnode(parent), ofnode))
Simon Glassae2efac2015-06-23 15:38:37 -0600716 return parent;
717
718 list_for_each_entry(dev, &parent->child_head, sibling_node) {
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200719 found = _device_find_global_by_ofnode(dev, ofnode);
Simon Glassae2efac2015-06-23 15:38:37 -0600720 if (found)
721 return found;
722 }
723
724 return NULL;
725}
726
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200727int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp)
728{
729 *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode);
730
731 return *devp ? 0 : -ENOENT;
732}
733
734int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
Simon Glassae2efac2015-06-23 15:38:37 -0600735{
736 struct udevice *dev;
737
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200738 dev = _device_find_global_by_ofnode(gd->dm_root, ofnode);
Simon Glassae2efac2015-06-23 15:38:37 -0600739 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
740}
Walter Lozanoffc41b02020-06-25 01:10:11 -0300741
742#if CONFIG_IS_ENABLED(OF_PLATDATA)
743int device_get_by_driver_info(const struct driver_info *info,
744 struct udevice **devp)
745{
Simon Glasscfd6a002020-10-03 11:31:33 -0600746 struct driver_info *info_base =
747 ll_entry_start(struct driver_info, driver_info);
748 int idx = info - info_base;
749 struct driver_rt *drt = gd_dm_driver_rt() + idx;
Walter Lozanoffc41b02020-06-25 01:10:11 -0300750 struct udevice *dev;
751
Simon Glasscfd6a002020-10-03 11:31:33 -0600752 dev = drt->dev;
Simon Glassd825ea72020-10-03 11:31:31 -0600753 *devp = NULL;
Walter Lozanoffc41b02020-06-25 01:10:11 -0300754
755 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
756}
Simon Glass5792f4b2020-10-03 11:31:40 -0600757
758int device_get_by_driver_info_idx(uint idx, struct udevice **devp)
759{
760 struct driver_rt *drt = gd_dm_driver_rt() + idx;
761 struct udevice *dev;
762
763 dev = drt->dev;
764 *devp = NULL;
765
766 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
767}
Walter Lozanoffc41b02020-06-25 01:10:11 -0300768#endif
Simon Glassae2efac2015-06-23 15:38:37 -0600769
Simon Glass2a580882020-01-27 08:49:36 -0700770int device_find_first_child(const struct udevice *parent, struct udevice **devp)
Simon Glass44da7352014-10-13 23:41:49 -0600771{
772 if (list_empty(&parent->child_head)) {
773 *devp = NULL;
774 } else {
775 *devp = list_first_entry(&parent->child_head, struct udevice,
776 sibling_node);
777 }
778
779 return 0;
780}
781
782int device_find_next_child(struct udevice **devp)
783{
784 struct udevice *dev = *devp;
785 struct udevice *parent = dev->parent;
786
787 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
788 *devp = NULL;
789 } else {
790 *devp = list_entry(dev->sibling_node.next, struct udevice,
791 sibling_node);
792 }
793
794 return 0;
795}
Simon Glass70c3a0e2014-11-11 10:46:18 -0700796
Simon Glass2a580882020-01-27 08:49:36 -0700797int device_find_first_inactive_child(const struct udevice *parent,
Simon Glassb775e872018-10-01 12:22:07 -0600798 enum uclass_id uclass_id,
799 struct udevice **devp)
800{
801 struct udevice *dev;
802
803 *devp = NULL;
804 list_for_each_entry(dev, &parent->child_head, sibling_node) {
805 if (!device_active(dev) &&
806 device_get_uclass_id(dev) == uclass_id) {
807 *devp = dev;
808 return 0;
809 }
810 }
811
812 return -ENODEV;
813}
814
Simon Glass2a580882020-01-27 08:49:36 -0700815int device_find_first_child_by_uclass(const struct udevice *parent,
Simon Glassa11341a2018-11-18 08:14:31 -0700816 enum uclass_id uclass_id,
817 struct udevice **devp)
818{
819 struct udevice *dev;
820
821 *devp = NULL;
822 list_for_each_entry(dev, &parent->child_head, sibling_node) {
823 if (device_get_uclass_id(dev) == uclass_id) {
824 *devp = dev;
825 return 0;
826 }
827 }
828
829 return -ENODEV;
830}
831
Simon Glass2a580882020-01-27 08:49:36 -0700832int device_find_child_by_name(const struct udevice *parent, const char *name,
Simon Glassa11341a2018-11-18 08:14:31 -0700833 struct udevice **devp)
834{
835 struct udevice *dev;
836
837 *devp = NULL;
838
839 list_for_each_entry(dev, &parent->child_head, sibling_node) {
840 if (!strcmp(dev->name, name)) {
841 *devp = dev;
842 return 0;
843 }
844 }
845
846 return -ENODEV;
847}
848
Simon Glass76db9b72020-01-27 08:49:48 -0700849int device_first_child_err(struct udevice *parent, struct udevice **devp)
850{
851 struct udevice *dev;
852
853 device_find_first_child(parent, &dev);
854 if (!dev)
855 return -ENODEV;
856
857 return device_get_device_tail(dev, 0, devp);
858}
859
860int device_next_child_err(struct udevice **devp)
861{
862 struct udevice *dev = *devp;
863
864 device_find_next_child(&dev);
865 if (!dev)
866 return -ENODEV;
867
868 return device_get_device_tail(dev, 0, devp);
869}
870
Simon Glass000676b2020-01-27 08:49:47 -0700871int device_first_child_ofdata_err(struct udevice *parent, struct udevice **devp)
872{
873 struct udevice *dev;
874 int ret;
875
876 device_find_first_child(parent, &dev);
877 if (!dev)
878 return -ENODEV;
879
Simon Glassaad29ae2020-12-03 16:55:21 -0700880 ret = device_of_to_plat(dev);
Simon Glass000676b2020-01-27 08:49:47 -0700881 if (ret)
882 return ret;
883
884 *devp = dev;
885
886 return 0;
887}
888
889int device_next_child_ofdata_err(struct udevice **devp)
890{
891 struct udevice *dev = *devp;
892 int ret;
893
894 device_find_next_child(&dev);
895 if (!dev)
896 return -ENODEV;
897
Simon Glassaad29ae2020-12-03 16:55:21 -0700898 ret = device_of_to_plat(dev);
Simon Glass000676b2020-01-27 08:49:47 -0700899 if (ret)
900 return ret;
901
902 *devp = dev;
903
904 return 0;
905}
906
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600907struct udevice *dev_get_parent(const struct udevice *child)
Simon Glass43f488a2014-11-11 10:46:19 -0700908{
909 return child->parent;
910}
911
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600912ulong dev_get_driver_data(const struct udevice *dev)
Simon Glass70c3a0e2014-11-11 10:46:18 -0700913{
Simon Glass46227bd2015-03-25 12:21:55 -0600914 return dev->driver_data;
Simon Glass70c3a0e2014-11-11 10:46:18 -0700915}
Simon Glass98fd5d12015-01-25 08:27:04 -0700916
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600917const void *dev_get_driver_ops(const struct udevice *dev)
Przemyslaw Marczakd3ef0d72015-04-15 13:07:24 +0200918{
919 if (!dev || !dev->driver->ops)
920 return NULL;
921
922 return dev->driver->ops;
923}
924
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600925enum uclass_id device_get_uclass_id(const struct udevice *dev)
Simon Glass98fd5d12015-01-25 08:27:04 -0700926{
927 return dev->uclass->uc_drv->id;
928}
Peng Fan99b72352015-02-10 14:46:32 +0800929
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600930const char *dev_get_uclass_name(const struct udevice *dev)
Przemyslaw Marczak5ed2e422015-04-15 13:07:25 +0200931{
932 if (!dev)
933 return NULL;
934
935 return dev->uclass->uc_drv->name;
936}
937
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600938bool device_has_children(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -0600939{
940 return !list_empty(&dev->child_head);
941}
942
Simon Glass2a580882020-01-27 08:49:36 -0700943bool device_has_active_children(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -0600944{
945 struct udevice *child;
946
947 for (device_find_first_child(dev, &child);
948 child;
949 device_find_next_child(&child)) {
950 if (device_active(child))
951 return true;
952 }
953
954 return false;
955}
956
Simon Glass2a580882020-01-27 08:49:36 -0700957bool device_is_last_sibling(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -0600958{
959 struct udevice *parent = dev->parent;
960
961 if (!parent)
962 return false;
963 return list_is_last(&dev->sibling_node, &parent->child_head);
964}
Simon Glasse3b23e22015-07-30 13:40:39 -0600965
Simon Glass7760ba22016-05-01 13:52:23 -0600966void device_set_name_alloced(struct udevice *dev)
967{
Simon Glass6211d762020-12-19 10:40:10 -0700968 dev_or_flags(dev, DM_FLAG_NAME_ALLOCED);
Simon Glass7760ba22016-05-01 13:52:23 -0600969}
970
Simon Glasse3b23e22015-07-30 13:40:39 -0600971int device_set_name(struct udevice *dev, const char *name)
972{
973 name = strdup(name);
974 if (!name)
975 return -ENOMEM;
976 dev->name = name;
Simon Glass7760ba22016-05-01 13:52:23 -0600977 device_set_name_alloced(dev);
Simon Glasse3b23e22015-07-30 13:40:39 -0600978
979 return 0;
980}
Mugunthan V N4666bd92016-04-28 15:36:02 +0530981
Simon Glassd4f0c942020-12-22 19:30:27 -0700982void dev_set_priv(struct udevice *dev, void *priv)
983{
Simon Glass541f62c2020-12-22 19:30:30 -0700984 dev->priv_ = priv;
Simon Glassd4f0c942020-12-22 19:30:27 -0700985}
986
987void dev_set_parent_priv(struct udevice *dev, void *parent_priv)
988{
Simon Glass541f62c2020-12-22 19:30:30 -0700989 dev->parent_priv_ = parent_priv;
Simon Glassd4f0c942020-12-22 19:30:27 -0700990}
991
992void dev_set_uclass_priv(struct udevice *dev, void *uclass_priv)
993{
Simon Glass541f62c2020-12-22 19:30:30 -0700994 dev->uclass_priv_ = uclass_priv;
Simon Glassd4f0c942020-12-22 19:30:27 -0700995}
996
997void dev_set_plat(struct udevice *dev, void *plat)
998{
Simon Glass541f62c2020-12-22 19:30:30 -0700999 dev->plat_ = plat;
Simon Glassd4f0c942020-12-22 19:30:27 -07001000}
1001
1002void dev_set_parent_plat(struct udevice *dev, void *parent_plat)
1003{
Simon Glass541f62c2020-12-22 19:30:30 -07001004 dev->parent_plat_ = parent_plat;
Simon Glassd4f0c942020-12-22 19:30:27 -07001005}
1006
1007void dev_set_uclass_plat(struct udevice *dev, void *uclass_plat)
1008{
Simon Glass541f62c2020-12-22 19:30:30 -07001009 dev->uclass_plat_ = uclass_plat;
Simon Glassd4f0c942020-12-22 19:30:27 -07001010}
1011
Marek Vasut7f18c342019-08-31 18:03:28 +02001012#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass2a580882020-01-27 08:49:36 -07001013bool device_is_compatible(const struct udevice *dev, const char *compat)
Mugunthan V N4666bd92016-04-28 15:36:02 +05301014{
Masahiro Yamada9349bcc2018-04-19 12:14:02 +09001015 return ofnode_device_is_compatible(dev_ofnode(dev), compat);
Mugunthan V N4666bd92016-04-28 15:36:02 +05301016}
1017
1018bool of_machine_is_compatible(const char *compat)
1019{
1020 const void *fdt = gd->fdt_blob;
1021
1022 return !fdt_node_check_compatible(fdt, 0, compat);
1023}
Mario Six3bedfbe2018-06-26 08:46:50 +02001024
1025int dev_disable_by_path(const char *path)
1026{
1027 struct uclass *uc;
1028 ofnode node = ofnode_path(path);
1029 struct udevice *dev;
1030 int ret = 1;
1031
1032 if (!of_live_active())
1033 return -ENOSYS;
1034
1035 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
1036 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev);
1037 if (!ret)
1038 break;
1039 }
1040
1041 if (ret)
1042 return ret;
1043
1044 ret = device_remove(dev, DM_REMOVE_NORMAL);
1045 if (ret)
1046 return ret;
1047
1048 ret = device_unbind(dev);
1049 if (ret)
1050 return ret;
1051
1052 return ofnode_set_enabled(node, false);
1053}
1054
1055int dev_enable_by_path(const char *path)
1056{
1057 ofnode node = ofnode_path(path);
1058 ofnode pnode = ofnode_get_parent(node);
1059 struct udevice *parent;
1060 int ret = 1;
1061
1062 if (!of_live_active())
1063 return -ENOSYS;
1064
1065 ret = device_find_by_ofnode(pnode, &parent);
1066 if (ret)
1067 return ret;
1068
1069 ret = ofnode_set_enabled(node, true);
1070 if (ret)
1071 return ret;
1072
Bin Meng9a9b0742018-10-10 22:06:58 -07001073 return lists_bind_fdt(parent, node, NULL, false);
Mario Six3bedfbe2018-06-26 08:46:50 +02001074}
Marek Vasut7f18c342019-08-31 18:03:28 +02001075#endif