blob: 06d2467825fd52c0934154eb7c065fe611e11941 [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>
Simon Glass3ba929a2020-10-30 21:38:53 -060014#include <asm/global_data.h>
Vignesh R0dff3702016-07-06 09:58:55 +053015#include <asm/io.h>
Philipp Tomsich9cf03b02018-01-08 13:59:18 +010016#include <clk.h>
Simon Glassdb6f0202014-07-23 06:55:12 -060017#include <fdtdec.h>
Stefan Roeseadc09052015-09-02 07:41:12 +020018#include <fdt_support.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070019#include <malloc.h>
Simon Glass274e0b02020-05-10 11:39:56 -060020#include <asm/cache.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070021#include <dm/device.h>
22#include <dm/device-internal.h>
23#include <dm/lists.h>
Mario Six1f1c1ec2018-01-15 11:07:20 +010024#include <dm/of_access.h>
Masahiro Yamadaf8efa632015-08-27 12:44:29 +090025#include <dm/pinctrl.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070026#include <dm/platdata.h>
Simon Glass322b2902017-05-18 20:09:05 -060027#include <dm/read.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070028#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 Fan6fba32d2018-07-27 10:20:38 +080033#include <power-domain.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070034
Simon Glassdb6f0202014-07-23 06:55:12 -060035DECLARE_GLOBAL_DATA_PTR;
36
Stephen Warren8c93df12016-05-11 15:26:24 -060037static int device_bind_common(struct udevice *parent, const struct driver *drv,
Simon Glass71fa5b42020-12-03 16:55:18 -070038 const char *name, void *plat,
Simon Glassb0cf4b32017-05-17 17:18:11 -060039 ulong driver_data, ofnode node,
Simon Glass39edb952020-12-03 16:55:19 -070040 uint of_plat_size, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -070041{
Heiko Schocherb74fcb42014-05-22 12:43:05 +020042 struct udevice *dev;
Simon Glassdd6ab882014-02-26 15:59:18 -070043 struct uclass *uc;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +020044 int size, ret = 0;
Simon Glass7272e922020-12-16 21:20:09 -070045 bool auto_seq = true;
Simon Glass2543b162020-12-22 19:30:29 -070046 void *ptr;
Simon Glassdd6ab882014-02-26 15:59:18 -070047
Simon Glass47fd4a22021-03-15 17:25:15 +130048 if (CONFIG_IS_ENABLED(OF_PLATDATA_NO_BIND))
49 return -ENOSYS;
50
Masahiro Yamadae1cc1f02015-08-27 12:44:28 +090051 if (devp)
52 *devp = NULL;
Simon Glassdd6ab882014-02-26 15:59:18 -070053 if (!name)
54 return -EINVAL;
55
56 ret = uclass_get(drv->id, &uc);
Simon Glass43313de2015-08-30 16:55:16 -060057 if (ret) {
58 debug("Missing uclass for driver %s\n", drv->name);
Simon Glassdd6ab882014-02-26 15:59:18 -070059 return ret;
Simon Glass43313de2015-08-30 16:55:16 -060060 }
Simon Glassdd6ab882014-02-26 15:59:18 -070061
Heiko Schocherb74fcb42014-05-22 12:43:05 +020062 dev = calloc(1, sizeof(struct udevice));
Simon Glassdd6ab882014-02-26 15:59:18 -070063 if (!dev)
64 return -ENOMEM;
65
66 INIT_LIST_HEAD(&dev->sibling_node);
67 INIT_LIST_HEAD(&dev->child_head);
68 INIT_LIST_HEAD(&dev->uclass_node);
Masahiro Yamada029bfca2015-07-25 21:52:37 +090069#ifdef CONFIG_DEVRES
Masahiro Yamada8b15b162015-07-25 21:52:35 +090070 INIT_LIST_HEAD(&dev->devres_head);
Masahiro Yamada029bfca2015-07-25 21:52:37 +090071#endif
Simon Glass2543b162020-12-22 19:30:29 -070072 dev_set_plat(dev, plat);
Stephen Warren8c93df12016-05-11 15:26:24 -060073 dev->driver_data = driver_data;
Simon Glassdd6ab882014-02-26 15:59:18 -070074 dev->name = name;
Simon Glassa7ece582020-12-19 10:40:14 -070075 dev_set_ofnode(dev, node);
Simon Glassdd6ab882014-02-26 15:59:18 -070076 dev->parent = parent;
77 dev->driver = drv;
78 dev->uclass = uc;
Simon Glassdb6f0202014-07-23 06:55:12 -060079
Simon Glass5e349922020-12-19 10:40:09 -070080 dev->seq_ = -1;
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +010081 if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
82 (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS)) {
Simon Glass1bd3deb2015-02-27 22:06:30 -070083 /*
Stefan Roese9d4ce6b2016-03-16 09:58:01 +010084 * Some devices, such as a SPI bus, I2C bus and serial ports
85 * are numbered using aliases.
Stefan Roese9d4ce6b2016-03-16 09:58:01 +010086 */
Simon Glasse422f8c2020-07-09 18:39:24 -060087 if (CONFIG_IS_ENABLED(OF_CONTROL) &&
88 !CONFIG_IS_ENABLED(OF_PLATDATA)) {
Simon Glass7272e922020-12-16 21:20:09 -070089 if (uc->uc_drv->name && ofnode_valid(node)) {
Simon Glass5e349922020-12-19 10:40:09 -070090 if (!dev_read_alias_seq(dev, &dev->seq_))
Simon Glass38040a92020-12-16 21:20:32 -070091 auto_seq = false;
Simon Glass7272e922020-12-16 21:20:09 -070092 }
Simon Glass0ccb0972015-01-25 08:27:05 -070093 }
Simon Glassdb6f0202014-07-23 06:55:12 -060094 }
Simon Glass3a9d5f12020-12-16 21:20:17 -070095 if (auto_seq && !(uc->uc_drv->flags & DM_UC_FLAG_NO_AUTO_SEQ))
Simon Glass5e349922020-12-19 10:40:09 -070096 dev->seq_ = uclass_find_next_free_seq(uc);
Simon Glass1bd3deb2015-02-27 22:06:30 -070097
Simon Glasse07434b2021-02-03 21:29:43 -070098 /* Check if we need to allocate plat */
Simon Glass71fa5b42020-12-03 16:55:18 -070099 if (drv->plat_auto) {
100 bool alloc = !plat;
Simon Glassafbf9b82016-07-04 11:58:18 -0600101
Simon Glasse07434b2021-02-03 21:29:43 -0700102 /*
103 * For of-platdata, we try use the existing data, but if
104 * plat_auto is larger, we must allocate a new space
105 */
Simon Glassafbf9b82016-07-04 11:58:18 -0600106 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
Simon Glasse07434b2021-02-03 21:29:43 -0700107 if (of_plat_size)
Simon Glass6211d762020-12-19 10:40:10 -0700108 dev_or_flags(dev, DM_FLAG_OF_PLATDATA);
Simon Glasse07434b2021-02-03 21:29:43 -0700109 if (of_plat_size < drv->plat_auto)
110 alloc = true;
Simon Glassafbf9b82016-07-04 11:58:18 -0600111 }
112 if (alloc) {
Simon Glass6211d762020-12-19 10:40:10 -0700113 dev_or_flags(dev, DM_FLAG_ALLOC_PDATA);
Simon Glass2543b162020-12-22 19:30:29 -0700114 ptr = calloc(1, drv->plat_auto);
115 if (!ptr) {
Simon Glassafbf9b82016-07-04 11:58:18 -0600116 ret = -ENOMEM;
117 goto fail_alloc1;
118 }
Simon Glasse07434b2021-02-03 21:29:43 -0700119
120 /*
121 * For of-platdata, copy the old plat into the new
122 * space
123 */
Simon Glass2543b162020-12-22 19:30:29 -0700124 if (CONFIG_IS_ENABLED(OF_PLATDATA) && plat)
125 memcpy(ptr, plat, of_plat_size);
126 dev_set_plat(dev, ptr);
Simon Glassba750492015-01-25 08:27:00 -0700127 }
128 }
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200129
Simon Glass71fa5b42020-12-03 16:55:18 -0700130 size = uc->uc_drv->per_device_plat_auto;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200131 if (size) {
Simon Glass6211d762020-12-19 10:40:10 -0700132 dev_or_flags(dev, DM_FLAG_ALLOC_UCLASS_PDATA);
Simon Glass2543b162020-12-22 19:30:29 -0700133 ptr = calloc(1, size);
134 if (!ptr) {
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200135 ret = -ENOMEM;
136 goto fail_alloc2;
137 }
Simon Glass2543b162020-12-22 19:30:29 -0700138 dev_set_uclass_plat(dev, ptr);
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200139 }
Simon Glass11b61732015-01-25 08:27:01 -0700140
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200141 if (parent) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700142 size = parent->driver->per_child_plat_auto;
Simon Glassd6206a52021-02-03 21:29:44 -0700143 if (!size)
Simon Glass71fa5b42020-12-03 16:55:18 -0700144 size = parent->uclass->uc_drv->per_child_plat_auto;
Simon Glass11b61732015-01-25 08:27:01 -0700145 if (size) {
Simon Glass6211d762020-12-19 10:40:10 -0700146 dev_or_flags(dev, DM_FLAG_ALLOC_PARENT_PDATA);
Simon Glass2543b162020-12-22 19:30:29 -0700147 ptr = calloc(1, size);
148 if (!ptr) {
Simon Glass11b61732015-01-25 08:27:01 -0700149 ret = -ENOMEM;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200150 goto fail_alloc3;
Simon Glass11b61732015-01-25 08:27:01 -0700151 }
Simon Glass2543b162020-12-22 19:30:29 -0700152 dev_set_parent_plat(dev, ptr);
Simon Glass11b61732015-01-25 08:27:01 -0700153 }
Heinrich Schuchardt5246b372020-02-15 21:38:48 +0100154 /* put dev into parent's successor list */
Simon Glassdd6ab882014-02-26 15:59:18 -0700155 list_add_tail(&dev->sibling_node, &parent->child_head);
Heinrich Schuchardt5246b372020-02-15 21:38:48 +0100156 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700157
158 ret = uclass_bind_device(dev);
159 if (ret)
Simon Glass4ebe01b2015-01-25 08:26:59 -0700160 goto fail_uclass_bind;
Simon Glassdd6ab882014-02-26 15:59:18 -0700161
162 /* if we fail to bind we remove device from successors and free it */
163 if (drv->bind) {
164 ret = drv->bind(dev);
Simon Glass4ebe01b2015-01-25 08:26:59 -0700165 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700166 goto fail_bind;
Simon Glassdd6ab882014-02-26 15:59:18 -0700167 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700168 if (parent && parent->driver->child_post_bind) {
169 ret = parent->driver->child_post_bind(dev);
170 if (ret)
171 goto fail_child_post_bind;
172 }
Simon Glass286863d2016-01-05 09:30:59 -0700173 if (uc->uc_drv->post_bind) {
174 ret = uc->uc_drv->post_bind(dev);
175 if (ret)
176 goto fail_uclass_post_bind;
177 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700178
Simon Glassdd6ab882014-02-26 15:59:18 -0700179 if (parent)
Masahiro Yamadaf70f39f2017-09-29 12:31:20 +0900180 pr_debug("Bound device %s to %s\n", dev->name, parent->name);
Masahiro Yamadae1cc1f02015-08-27 12:44:28 +0900181 if (devp)
182 *devp = dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700183
Simon Glass6211d762020-12-19 10:40:10 -0700184 dev_or_flags(dev, DM_FLAG_BOUND);
Masahiro Yamadabdbb5dd2015-07-25 21:52:34 +0900185
Simon Glassdd6ab882014-02-26 15:59:18 -0700186 return 0;
187
Simon Glass286863d2016-01-05 09:30:59 -0700188fail_uclass_post_bind:
189 /* There is no child unbind() method, so no clean-up required */
Simon Glassa4a51a02015-01-25 08:27:03 -0700190fail_child_post_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900191 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700192 if (drv->unbind && drv->unbind(dev)) {
193 dm_warn("unbind() method failed on dev '%s' on error path\n",
194 dev->name);
195 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700196 }
197
Simon Glassdd6ab882014-02-26 15:59:18 -0700198fail_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900199 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700200 if (uclass_unbind_device(dev)) {
201 dm_warn("Failed to unbind dev '%s' on error path\n",
202 dev->name);
203 }
Simon Glass4ebe01b2015-01-25 08:26:59 -0700204 }
205fail_uclass_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900206 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700207 list_del(&dev->sibling_node);
Simon Glass6211d762020-12-19 10:40:10 -0700208 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PARENT_PDATA) {
Simon Glass2543b162020-12-22 19:30:29 -0700209 free(dev_get_parent_plat(dev));
210 dev_set_parent_plat(dev, NULL);
Simon Glasse70a4f42015-02-27 22:06:33 -0700211 }
Simon Glass11b61732015-01-25 08:27:01 -0700212 }
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200213fail_alloc3:
Simon Glassd6206a52021-02-03 21:29:44 -0700214 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
215 if (dev_get_flags(dev) & DM_FLAG_ALLOC_UCLASS_PDATA) {
216 free(dev_get_uclass_plat(dev));
217 dev_set_uclass_plat(dev, NULL);
218 }
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200219 }
Simon Glass11b61732015-01-25 08:27:01 -0700220fail_alloc2:
Simon Glassd6206a52021-02-03 21:29:44 -0700221 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
222 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PDATA) {
223 free(dev_get_plat(dev));
224 dev_set_plat(dev, NULL);
225 }
Simon Glassba750492015-01-25 08:27:00 -0700226 }
227fail_alloc1:
Masahiro Yamada8b15b162015-07-25 21:52:35 +0900228 devres_release_all(dev);
229
Simon Glassdd6ab882014-02-26 15:59:18 -0700230 free(dev);
Simon Glass4ebe01b2015-01-25 08:26:59 -0700231
Simon Glassdd6ab882014-02-26 15:59:18 -0700232 return ret;
233}
234
Stephen Warren8c93df12016-05-11 15:26:24 -0600235int device_bind_with_driver_data(struct udevice *parent,
236 const struct driver *drv, const char *name,
Simon Glass322b2902017-05-18 20:09:05 -0600237 ulong driver_data, ofnode node,
Stephen Warren8c93df12016-05-11 15:26:24 -0600238 struct udevice **devp)
239{
Simon Glass322b2902017-05-18 20:09:05 -0600240 return device_bind_common(parent, drv, name, NULL, driver_data, node,
241 0, devp);
Stephen Warren8c93df12016-05-11 15:26:24 -0600242}
243
Simon Glass884870f2020-11-28 17:50:01 -0700244int device_bind(struct udevice *parent, const struct driver *drv,
Simon Glass71fa5b42020-12-03 16:55:18 -0700245 const char *name, void *plat, ofnode node,
Simon Glass884870f2020-11-28 17:50:01 -0700246 struct udevice **devp)
Simon Glass9eb151e2018-06-11 13:07:15 -0600247{
Simon Glass71fa5b42020-12-03 16:55:18 -0700248 return device_bind_common(parent, drv, name, plat, 0, node, 0,
Simon Glass9eb151e2018-06-11 13:07:15 -0600249 devp);
250}
251
Simon Glassfef72b72014-07-23 06:55:03 -0600252int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
Simon Glasscfd6a002020-10-03 11:31:33 -0600253 const struct driver_info *info, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700254{
255 struct driver *drv;
Simon Glass39edb952020-12-03 16:55:19 -0700256 uint plat_size = 0;
Walter Lozanoffc41b02020-06-25 01:10:11 -0300257 int ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700258
259 drv = lists_driver_lookup_name(info->name);
260 if (!drv)
261 return -ENOENT;
Simon Glassfef72b72014-07-23 06:55:03 -0600262 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
263 return -EPERM;
Simon Glassdd6ab882014-02-26 15:59:18 -0700264
Simon Glassafbf9b82016-07-04 11:58:18 -0600265#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass39edb952020-12-03 16:55:19 -0700266 plat_size = info->plat_size;
Simon Glassafbf9b82016-07-04 11:58:18 -0600267#endif
Simon Glass71fa5b42020-12-03 16:55:18 -0700268 ret = device_bind_common(parent, drv, info->name, (void *)info->plat, 0,
Simon Glass39edb952020-12-03 16:55:19 -0700269 ofnode_null(), plat_size, devp);
Walter Lozanoffc41b02020-06-25 01:10:11 -0300270 if (ret)
271 return ret;
Walter Lozanoffc41b02020-06-25 01:10:11 -0300272
273 return ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700274}
275
Claudiu Bezneabf5b8232020-09-07 17:46:33 +0300276int device_reparent(struct udevice *dev, struct udevice *new_parent)
277{
278 struct udevice *pos, *n;
279
280 assert(dev);
281 assert(new_parent);
282
283 list_for_each_entry_safe(pos, n, &dev->parent->child_head,
284 sibling_node) {
285 if (pos->driver != dev->driver)
286 continue;
287
288 list_del(&dev->sibling_node);
289 list_add_tail(&dev->sibling_node, &new_parent->child_head);
290 dev->parent = new_parent;
291
292 break;
293 }
294
295 return 0;
296}
297
Simon Glass825d3f92015-03-25 12:21:53 -0600298static void *alloc_priv(int size, uint flags)
299{
300 void *priv;
301
302 if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
Faiz Abbas03020a92017-09-19 16:53:50 +0530303 size = ROUND(size, ARCH_DMA_MINALIGN);
Simon Glass825d3f92015-03-25 12:21:53 -0600304 priv = memalign(ARCH_DMA_MINALIGN, size);
Simon Glass27bc9b12017-04-04 13:00:19 -0600305 if (priv) {
Simon Glass825d3f92015-03-25 12:21:53 -0600306 memset(priv, '\0', size);
Simon Glass27bc9b12017-04-04 13:00:19 -0600307
308 /*
309 * Ensure that the zero bytes are flushed to memory.
310 * This prevents problems if the driver uses this as
311 * both an input and an output buffer:
312 *
313 * 1. Zeroes written to buffer (here) and sit in the
314 * cache
315 * 2. Driver issues a read command to DMA
316 * 3. CPU runs out of cache space and evicts some cache
317 * data in the buffer, writing zeroes to RAM from
318 * the memset() above
319 * 4. DMA completes
320 * 5. Buffer now has some DMA data and some zeroes
321 * 6. Data being read is now incorrect
322 *
323 * To prevent this, ensure that the cache is clean
324 * within this range at the start. The driver can then
325 * use normal flush-after-write, invalidate-before-read
326 * procedures.
327 *
328 * TODO(sjg@chromium.org): Drop this microblaze
329 * exception.
330 */
331#ifndef CONFIG_MICROBLAZE
332 flush_dcache_range((ulong)priv, (ulong)priv + size);
333#endif
334 }
Simon Glass825d3f92015-03-25 12:21:53 -0600335 } else {
336 priv = calloc(1, size);
337 }
338
339 return priv;
340}
341
Simon Glassad337e62020-12-19 10:40:08 -0700342/**
343 * device_alloc_priv() - Allocate priv/plat data required by the device
344 *
345 * @dev: Device to process
346 * @return 0 if OK, -ENOMEM if out of memory
347 */
348static int device_alloc_priv(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700349{
Simon Glassa626dff2015-03-25 12:21:54 -0600350 const struct driver *drv;
Simon Glass2543b162020-12-22 19:30:29 -0700351 void *ptr;
Simon Glassad337e62020-12-19 10:40:08 -0700352 int size;
Simon Glass44c47892020-04-05 15:38:19 -0600353
Simon Glassdd6ab882014-02-26 15:59:18 -0700354 drv = dev->driver;
355 assert(drv);
356
Bin Meng82cdd782015-08-24 01:14:02 -0700357 /* Allocate private data if requested and not reentered */
Simon Glass2543b162020-12-22 19:30:29 -0700358 if (drv->priv_auto && !dev_get_priv(dev)) {
359 ptr = alloc_priv(drv->priv_auto, drv->flags);
Simon Glassad337e62020-12-19 10:40:08 -0700360 if (!ptr)
361 return -ENOMEM;
Simon Glass2543b162020-12-22 19:30:29 -0700362 dev_set_priv(dev, ptr);
Simon Glassdd6ab882014-02-26 15:59:18 -0700363 }
Simon Glassad337e62020-12-19 10:40:08 -0700364
Bin Meng82cdd782015-08-24 01:14:02 -0700365 /* Allocate private data if requested and not reentered */
Simon Glass8a2b47f2020-12-03 16:55:17 -0700366 size = dev->uclass->uc_drv->per_device_auto;
Simon Glass2543b162020-12-22 19:30:29 -0700367 if (size && !dev_get_uclass_priv(dev)) {
368 ptr = alloc_priv(size, dev->uclass->uc_drv->flags);
Simon Glassad337e62020-12-19 10:40:08 -0700369 if (!ptr)
370 return -ENOMEM;
Simon Glass2543b162020-12-22 19:30:29 -0700371 dev_set_uclass_priv(dev, ptr);
Simon Glassdd6ab882014-02-26 15:59:18 -0700372 }
373
Simon Glassa8d8f162019-12-29 21:19:18 -0700374 /* Allocate parent data for this child */
Simon Glassdd6ab882014-02-26 15:59:18 -0700375 if (dev->parent) {
Simon Glass8a2b47f2020-12-03 16:55:17 -0700376 size = dev->parent->driver->per_child_auto;
Simon Glass2543b162020-12-22 19:30:29 -0700377 if (!size)
Simon Glass8a2b47f2020-12-03 16:55:17 -0700378 size = dev->parent->uclass->uc_drv->per_child_auto;
Simon Glass2543b162020-12-22 19:30:29 -0700379 if (size && !dev_get_parent_priv(dev)) {
380 ptr = alloc_priv(size, drv->flags);
Simon Glassad337e62020-12-19 10:40:08 -0700381 if (!ptr)
382 return -ENOMEM;
Simon Glass2543b162020-12-22 19:30:29 -0700383 dev_set_parent_priv(dev, ptr);
Simon Glass60d971b2014-07-23 06:55:20 -0600384 }
Simon Glassa8d8f162019-12-29 21:19:18 -0700385 }
386
Simon Glassad337e62020-12-19 10:40:08 -0700387 return 0;
388}
389
390int device_of_to_plat(struct udevice *dev)
391{
392 const struct driver *drv;
393 int ret;
394
395 if (!dev)
396 return -EINVAL;
397
Simon Glass6211d762020-12-19 10:40:10 -0700398 if (dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID)
Simon Glassad337e62020-12-19 10:40:08 -0700399 return 0;
400
Simon Glass47fd4a22021-03-15 17:25:15 +1300401 /*
402 * This is not needed if binding is disabled, since data is allocated
403 * at build time.
404 */
405 if (!CONFIG_IS_ENABLED(OF_PLATDATA_NO_BIND)) {
406 /* Ensure all parents have ofdata */
407 if (dev->parent) {
408 ret = device_of_to_plat(dev->parent);
409 if (ret)
410 goto fail;
411
412 /*
413 * The device might have already been probed during
414 * the call to device_probe() on its parent device
415 * (e.g. PCI bridge devices). Test the flags again
416 * so that we don't mess up the device.
417 */
418 if (dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID)
419 return 0;
420 }
421
422 ret = device_alloc_priv(dev);
Simon Glassad337e62020-12-19 10:40:08 -0700423 if (ret)
424 goto fail;
Simon Glassad337e62020-12-19 10:40:08 -0700425 }
Simon Glassad337e62020-12-19 10:40:08 -0700426 drv = dev->driver;
427 assert(drv);
428
Simon Glassaad29ae2020-12-03 16:55:21 -0700429 if (drv->of_to_plat &&
Simon Glass07c17772020-12-19 10:40:12 -0700430 (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_ofnode(dev))) {
Simon Glassaad29ae2020-12-03 16:55:21 -0700431 ret = drv->of_to_plat(dev);
Simon Glassa8d8f162019-12-29 21:19:18 -0700432 if (ret)
433 goto fail;
434 }
Simon Glass60d971b2014-07-23 06:55:20 -0600435
Simon Glass6211d762020-12-19 10:40:10 -0700436 dev_or_flags(dev, DM_FLAG_PLATDATA_VALID);
Simon Glassd3c9d302019-12-29 21:19:21 -0700437
Simon Glass4a263852019-12-29 21:19:20 -0700438 return 0;
439fail:
440 device_free(dev);
441
442 return ret;
443}
444
Nicolas Saenz Juliennea2c7ead2021-01-12 13:55:24 +0100445/**
446 * device_get_dma_constraints() - Populate device's DMA constraints
447 *
448 * Gets a device's DMA constraints from firmware. This information is later
449 * used by drivers to translate physcal addresses to the device's bus address
450 * space. For now only device-tree is supported.
451 *
452 * @dev: Pointer to target device
453 * Return: 0 if OK or if no DMA constraints were found, error otherwise
454 */
455static int device_get_dma_constraints(struct udevice *dev)
456{
457 struct udevice *parent = dev->parent;
458 phys_addr_t cpu = 0;
459 dma_addr_t bus = 0;
460 u64 size = 0;
461 int ret;
462
463 if (!CONFIG_IS_ENABLED(DM_DMA) || !parent || !dev_has_ofnode(parent))
464 return 0;
465
466 /*
467 * We start parsing for dma-ranges from the device's bus node. This is
468 * specially important on nested buses.
469 */
470 ret = dev_get_dma_range(parent, &cpu, &bus, &size);
471 /* Don't return an error if no 'dma-ranges' were found */
472 if (ret && ret != -ENOENT) {
473 dm_warn("%s: failed to get DMA range, %d\n", dev->name, ret);
474 return ret;
475 }
476
477 dev_set_dma_offset(dev, cpu - bus);
478
479 return 0;
480}
481
Simon Glass4a263852019-12-29 21:19:20 -0700482int device_probe(struct udevice *dev)
483{
484 const struct driver *drv;
485 int ret;
Simon Glass4a263852019-12-29 21:19:20 -0700486
487 if (!dev)
488 return -EINVAL;
489
Simon Glass6211d762020-12-19 10:40:10 -0700490 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
Simon Glass4a263852019-12-29 21:19:20 -0700491 return 0;
492
493 drv = dev->driver;
494 assert(drv);
495
Simon Glassaad29ae2020-12-03 16:55:21 -0700496 ret = device_of_to_plat(dev);
Simon Glass4a263852019-12-29 21:19:20 -0700497 if (ret)
498 goto fail;
499
Simon Glassa8d8f162019-12-29 21:19:18 -0700500 /* Ensure all parents are probed */
501 if (dev->parent) {
Simon Glassdd6ab882014-02-26 15:59:18 -0700502 ret = device_probe(dev->parent);
503 if (ret)
504 goto fail;
Bin Meng82cdd782015-08-24 01:14:02 -0700505
506 /*
507 * The device might have already been probed during
508 * the call to device_probe() on its parent device
509 * (e.g. PCI bridge devices). Test the flags again
510 * so that we don't mess up the device.
511 */
Simon Glass6211d762020-12-19 10:40:10 -0700512 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
Bin Meng82cdd782015-08-24 01:14:02 -0700513 return 0;
Simon Glassdd6ab882014-02-26 15:59:18 -0700514 }
Simon Glassdb6f0202014-07-23 06:55:12 -0600515
Simon Glass6211d762020-12-19 10:40:10 -0700516 dev_or_flags(dev, DM_FLAG_ACTIVATED);
Simon Glass8b04e732015-03-25 12:21:56 -0600517
Simon Glassbb53bda2015-09-12 08:45:19 -0600518 /*
519 * Process pinctrl for everything except the root device, and
Simon Glass27ce9622016-01-21 19:43:25 -0700520 * continue regardless of the result of pinctrl. Don't process pinctrl
521 * settings for pinctrl devices since the device may not yet be
522 * probed.
Simon Glass303c3c22021-01-21 13:57:13 -0700523 *
524 * This call can produce some non-intuitive results. For example, on an
525 * x86 device where dev is the main PCI bus, the pinctrl device may be
526 * child or grandchild of that bus, meaning that the child will be
527 * probed here. If the child happens to be the P2SB and the pinctrl
528 * device is a child of that, then both the pinctrl and P2SB will be
529 * probed by this call. This works because the DM_FLAG_ACTIVATED flag
530 * is set just above. However, the PCI bus' probe() method and
531 * associated uclass methods have not yet been called.
Simon Glassbb53bda2015-09-12 08:45:19 -0600532 */
Simon Glass27ce9622016-01-21 19:43:25 -0700533 if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
Simon Glassbb53bda2015-09-12 08:45:19 -0600534 pinctrl_select_state(dev, "default");
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900535
Anatolij Gustschin63a34672019-07-14 21:11:01 +0200536 if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
Lokesh Vutlac10e52b2019-09-27 13:48:12 +0530537 (device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) &&
538 !(drv->flags & DM_FLAG_DEFAULT_PD_CTRL_OFF)) {
Peng Fand2201112019-09-17 09:29:22 +0000539 ret = dev_power_domain_on(dev);
540 if (ret)
541 goto fail;
Peng Fan6fba32d2018-07-27 10:20:38 +0800542 }
543
Nicolas Saenz Juliennea2c7ead2021-01-12 13:55:24 +0100544 ret = device_get_dma_constraints(dev);
545 if (ret)
546 goto fail;
547
Simon Glass9c1f3822015-03-05 12:25:22 -0700548 ret = uclass_pre_probe_device(dev);
Simon Glass5104b982015-01-25 08:27:10 -0700549 if (ret)
550 goto fail;
551
Simon Glassd45560d2014-07-23 06:55:21 -0600552 if (dev->parent && dev->parent->driver->child_pre_probe) {
553 ret = dev->parent->driver->child_pre_probe(dev);
554 if (ret)
555 goto fail;
556 }
557
Bin Meng513cf402019-07-05 09:23:16 -0700558 /* Only handle devices that have a valid ofnode */
Simon Glassf1d50f72020-12-19 10:40:13 -0700559 if (dev_has_ofnode(dev)) {
Bin Meng513cf402019-07-05 09:23:16 -0700560 /*
561 * Process 'assigned-{clocks/clock-parents/clock-rates}'
562 * properties
563 */
Jean-Jacques Hiblot9601f322019-10-22 14:00:06 +0200564 ret = clk_set_defaults(dev, 0);
Bin Meng513cf402019-07-05 09:23:16 -0700565 if (ret)
566 goto fail;
567 }
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100568
Simon Glassdd6ab882014-02-26 15:59:18 -0700569 if (drv->probe) {
570 ret = drv->probe(dev);
Simon Glass9046dec2019-12-29 21:19:16 -0700571 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700572 goto fail;
573 }
574
Simon Glassdd6ab882014-02-26 15:59:18 -0700575 ret = uclass_post_probe_device(dev);
Simon Glass8b04e732015-03-25 12:21:56 -0600576 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700577 goto fail_uclass;
Simon Glassdd6ab882014-02-26 15:59:18 -0700578
Peng Fan7cf58dd2016-03-12 13:17:38 +0800579 if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
580 pinctrl_select_state(dev, "default");
581
Simon Glassdd6ab882014-02-26 15:59:18 -0700582 return 0;
583fail_uclass:
Stefan Roese80b5bc92017-03-20 12:51:48 +0100584 if (device_remove(dev, DM_REMOVE_NORMAL)) {
Simon Glassdd6ab882014-02-26 15:59:18 -0700585 dm_warn("%s: Device '%s' failed to remove on error path\n",
586 __func__, dev->name);
587 }
588fail:
Simon Glass6211d762020-12-19 10:40:10 -0700589 dev_bic_flags(dev, DM_FLAG_ACTIVATED);
Simon Glass8b04e732015-03-25 12:21:56 -0600590
Simon Glassdd6ab882014-02-26 15:59:18 -0700591 device_free(dev);
592
593 return ret;
594}
595
Simon Glassfa20e932020-12-03 16:55:20 -0700596void *dev_get_plat(const struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700597{
598 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700599 dm_warn("%s: null device\n", __func__);
Simon Glassdd6ab882014-02-26 15:59:18 -0700600 return NULL;
601 }
602
Simon Glass541f62c2020-12-22 19:30:30 -0700603 return dev->plat_;
Simon Glassdd6ab882014-02-26 15:59:18 -0700604}
605
Simon Glass71fa5b42020-12-03 16:55:18 -0700606void *dev_get_parent_plat(const struct udevice *dev)
Simon Glass11b61732015-01-25 08:27:01 -0700607{
608 if (!dev) {
Simon Glasscdfc5052015-07-06 16:47:40 -0600609 dm_warn("%s: null device\n", __func__);
Simon Glass11b61732015-01-25 08:27:01 -0700610 return NULL;
611 }
612
Simon Glass541f62c2020-12-22 19:30:30 -0700613 return dev->parent_plat_;
Simon Glass11b61732015-01-25 08:27:01 -0700614}
615
Simon Glass71fa5b42020-12-03 16:55:18 -0700616void *dev_get_uclass_plat(const struct udevice *dev)
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200617{
618 if (!dev) {
Simon Glasscdfc5052015-07-06 16:47:40 -0600619 dm_warn("%s: null device\n", __func__);
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200620 return NULL;
621 }
622
Simon Glass541f62c2020-12-22 19:30:30 -0700623 return dev->uclass_plat_;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200624}
625
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600626void *dev_get_priv(const struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700627{
628 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700629 dm_warn("%s: null device\n", __func__);
Simon Glassdd6ab882014-02-26 15:59:18 -0700630 return NULL;
631 }
632
Simon Glass541f62c2020-12-22 19:30:30 -0700633 return dev->priv_;
Simon Glassdd6ab882014-02-26 15:59:18 -0700634}
Simon Glass48d4e292014-07-23 06:55:19 -0600635
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600636void *dev_get_uclass_priv(const struct udevice *dev)
Simon Glassde0977b2015-03-05 12:25:20 -0700637{
638 if (!dev) {
639 dm_warn("%s: null device\n", __func__);
640 return NULL;
641 }
642
Simon Glass541f62c2020-12-22 19:30:30 -0700643 return dev->uclass_priv_;
Simon Glassde0977b2015-03-05 12:25:20 -0700644}
645
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600646void *dev_get_parent_priv(const struct udevice *dev)
Simon Glass60d971b2014-07-23 06:55:20 -0600647{
648 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700649 dm_warn("%s: null device\n", __func__);
Simon Glass60d971b2014-07-23 06:55:20 -0600650 return NULL;
651 }
652
Simon Glass541f62c2020-12-22 19:30:30 -0700653 return dev->parent_priv_;
Simon Glass60d971b2014-07-23 06:55:20 -0600654}
655
Simon Glass48d4e292014-07-23 06:55:19 -0600656static int device_get_device_tail(struct udevice *dev, int ret,
657 struct udevice **devp)
658{
659 if (ret)
660 return ret;
661
662 ret = device_probe(dev);
663 if (ret)
664 return ret;
665
666 *devp = dev;
667
668 return 0;
669}
670
Marek Vasut7f18c342019-08-31 18:03:28 +0200671#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Mario Six3bedfbe2018-06-26 08:46:50 +0200672/**
673 * device_find_by_ofnode() - Return device associated with given ofnode
674 *
675 * The returned device is *not* activated.
676 *
677 * @node: The ofnode for which a associated device should be looked up
678 * @devp: Pointer to structure to hold the found device
679 * Return: 0 if OK, -ve on error
680 */
681static int device_find_by_ofnode(ofnode node, struct udevice **devp)
682{
683 struct uclass *uc;
684 struct udevice *dev;
685 int ret;
686
Simon Glass784cd9e2020-12-19 10:40:17 -0700687 list_for_each_entry(uc, gd->uclass_root, sibling_node) {
Mario Six3bedfbe2018-06-26 08:46:50 +0200688 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node,
689 &dev);
690 if (!ret || dev) {
691 *devp = dev;
692 return 0;
693 }
694 }
695
696 return -ENODEV;
697}
Marek Vasut7f18c342019-08-31 18:03:28 +0200698#endif
Mario Six3bedfbe2018-06-26 08:46:50 +0200699
Simon Glass2a580882020-01-27 08:49:36 -0700700int device_get_child(const struct udevice *parent, int index,
701 struct udevice **devp)
Simon Glass48d4e292014-07-23 06:55:19 -0600702{
703 struct udevice *dev;
704
705 list_for_each_entry(dev, &parent->child_head, sibling_node) {
706 if (!index--)
707 return device_get_device_tail(dev, 0, devp);
708 }
709
710 return -ENODEV;
711}
712
Simon Glass2a580882020-01-27 08:49:36 -0700713int device_get_child_count(const struct udevice *parent)
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530714{
715 struct udevice *dev;
716 int count = 0;
717
718 list_for_each_entry(dev, &parent->child_head, sibling_node)
719 count++;
720
721 return count;
722}
723
Simon Glass07e13382020-12-16 21:20:29 -0700724int device_find_child_by_seq(const struct udevice *parent, int seq,
725 struct udevice **devp)
Simon Glass48d4e292014-07-23 06:55:19 -0600726{
727 struct udevice *dev;
728
729 *devp = NULL;
Simon Glass48d4e292014-07-23 06:55:19 -0600730
731 list_for_each_entry(dev, &parent->child_head, sibling_node) {
Simon Glass5e349922020-12-19 10:40:09 -0700732 if (dev->seq_ == seq) {
Simon Glass48d4e292014-07-23 06:55:19 -0600733 *devp = dev;
734 return 0;
735 }
736 }
737
738 return -ENODEV;
739}
740
Simon Glass2a580882020-01-27 08:49:36 -0700741int device_get_child_by_seq(const struct udevice *parent, int seq,
Simon Glass48d4e292014-07-23 06:55:19 -0600742 struct udevice **devp)
743{
744 struct udevice *dev;
745 int ret;
746
747 *devp = NULL;
Simon Glass07e13382020-12-16 21:20:29 -0700748 ret = device_find_child_by_seq(parent, seq, &dev);
749
Simon Glass48d4e292014-07-23 06:55:19 -0600750 return device_get_device_tail(dev, ret, devp);
751}
752
Simon Glass2a580882020-01-27 08:49:36 -0700753int device_find_child_by_of_offset(const struct udevice *parent, int of_offset,
Simon Glass48d4e292014-07-23 06:55:19 -0600754 struct udevice **devp)
755{
756 struct udevice *dev;
757
758 *devp = NULL;
759
760 list_for_each_entry(dev, &parent->child_head, sibling_node) {
Simon Glassdd79d6e2017-01-17 16:52:55 -0700761 if (dev_of_offset(dev) == of_offset) {
Simon Glass48d4e292014-07-23 06:55:19 -0600762 *devp = dev;
763 return 0;
764 }
765 }
766
767 return -ENODEV;
768}
769
Simon Glass2a580882020-01-27 08:49:36 -0700770int device_get_child_by_of_offset(const struct udevice *parent, int node,
Simon Glass48d4e292014-07-23 06:55:19 -0600771 struct udevice **devp)
772{
773 struct udevice *dev;
774 int ret;
775
776 *devp = NULL;
Simon Glass861bc9f2015-06-23 15:38:38 -0600777 ret = device_find_child_by_of_offset(parent, node, &dev);
Simon Glass48d4e292014-07-23 06:55:19 -0600778 return device_get_device_tail(dev, ret, devp);
779}
Simon Glass44da7352014-10-13 23:41:49 -0600780
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200781static struct udevice *_device_find_global_by_ofnode(struct udevice *parent,
782 ofnode ofnode)
Simon Glassae2efac2015-06-23 15:38:37 -0600783{
784 struct udevice *dev, *found;
785
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200786 if (ofnode_equal(dev_ofnode(parent), ofnode))
Simon Glassae2efac2015-06-23 15:38:37 -0600787 return parent;
788
789 list_for_each_entry(dev, &parent->child_head, sibling_node) {
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200790 found = _device_find_global_by_ofnode(dev, ofnode);
Simon Glassae2efac2015-06-23 15:38:37 -0600791 if (found)
792 return found;
793 }
794
795 return NULL;
796}
797
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200798int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp)
799{
800 *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode);
801
802 return *devp ? 0 : -ENOENT;
803}
804
805int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
Simon Glassae2efac2015-06-23 15:38:37 -0600806{
807 struct udevice *dev;
808
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200809 dev = _device_find_global_by_ofnode(gd->dm_root, ofnode);
Simon Glassae2efac2015-06-23 15:38:37 -0600810 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
811}
Walter Lozanoffc41b02020-06-25 01:10:11 -0300812
813#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass0000e0d2021-03-15 17:25:28 +1300814int device_get_by_ofplat_idx(uint idx, struct udevice **devp)
Simon Glass5792f4b2020-10-03 11:31:40 -0600815{
Simon Glass5792f4b2020-10-03 11:31:40 -0600816 struct udevice *dev;
817
Simon Glass0000e0d2021-03-15 17:25:28 +1300818 if (CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
819 struct udevice *base = ll_entry_start(struct udevice, udevice);
820
821 dev = base + idx;
822 } else {
823 struct driver_rt *drt = gd_dm_driver_rt() + idx;
824
825 dev = drt->dev;
826 }
Simon Glass5792f4b2020-10-03 11:31:40 -0600827 *devp = NULL;
828
829 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
830}
Walter Lozanoffc41b02020-06-25 01:10:11 -0300831#endif
Simon Glassae2efac2015-06-23 15:38:37 -0600832
Simon Glass2a580882020-01-27 08:49:36 -0700833int device_find_first_child(const struct udevice *parent, struct udevice **devp)
Simon Glass44da7352014-10-13 23:41:49 -0600834{
835 if (list_empty(&parent->child_head)) {
836 *devp = NULL;
837 } else {
838 *devp = list_first_entry(&parent->child_head, struct udevice,
839 sibling_node);
840 }
841
842 return 0;
843}
844
845int device_find_next_child(struct udevice **devp)
846{
847 struct udevice *dev = *devp;
848 struct udevice *parent = dev->parent;
849
850 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
851 *devp = NULL;
852 } else {
853 *devp = list_entry(dev->sibling_node.next, struct udevice,
854 sibling_node);
855 }
856
857 return 0;
858}
Simon Glass70c3a0e2014-11-11 10:46:18 -0700859
Simon Glass2a580882020-01-27 08:49:36 -0700860int device_find_first_inactive_child(const struct udevice *parent,
Simon Glassb775e872018-10-01 12:22:07 -0600861 enum uclass_id uclass_id,
862 struct udevice **devp)
863{
864 struct udevice *dev;
865
866 *devp = NULL;
867 list_for_each_entry(dev, &parent->child_head, sibling_node) {
868 if (!device_active(dev) &&
869 device_get_uclass_id(dev) == uclass_id) {
870 *devp = dev;
871 return 0;
872 }
873 }
874
875 return -ENODEV;
876}
877
Simon Glass2a580882020-01-27 08:49:36 -0700878int device_find_first_child_by_uclass(const struct udevice *parent,
Simon Glassa11341a2018-11-18 08:14:31 -0700879 enum uclass_id uclass_id,
880 struct udevice **devp)
881{
882 struct udevice *dev;
883
884 *devp = NULL;
885 list_for_each_entry(dev, &parent->child_head, sibling_node) {
886 if (device_get_uclass_id(dev) == uclass_id) {
887 *devp = dev;
888 return 0;
889 }
890 }
891
892 return -ENODEV;
893}
894
Simon Glass2a580882020-01-27 08:49:36 -0700895int device_find_child_by_name(const struct udevice *parent, const char *name,
Simon Glassa11341a2018-11-18 08:14:31 -0700896 struct udevice **devp)
897{
898 struct udevice *dev;
899
900 *devp = NULL;
901
902 list_for_each_entry(dev, &parent->child_head, sibling_node) {
903 if (!strcmp(dev->name, name)) {
904 *devp = dev;
905 return 0;
906 }
907 }
908
909 return -ENODEV;
910}
911
Simon Glass76db9b72020-01-27 08:49:48 -0700912int device_first_child_err(struct udevice *parent, struct udevice **devp)
913{
914 struct udevice *dev;
915
916 device_find_first_child(parent, &dev);
917 if (!dev)
918 return -ENODEV;
919
920 return device_get_device_tail(dev, 0, devp);
921}
922
923int device_next_child_err(struct udevice **devp)
924{
925 struct udevice *dev = *devp;
926
927 device_find_next_child(&dev);
928 if (!dev)
929 return -ENODEV;
930
931 return device_get_device_tail(dev, 0, devp);
932}
933
Simon Glass000676b2020-01-27 08:49:47 -0700934int device_first_child_ofdata_err(struct udevice *parent, struct udevice **devp)
935{
936 struct udevice *dev;
937 int ret;
938
939 device_find_first_child(parent, &dev);
940 if (!dev)
941 return -ENODEV;
942
Simon Glassaad29ae2020-12-03 16:55:21 -0700943 ret = device_of_to_plat(dev);
Simon Glass000676b2020-01-27 08:49:47 -0700944 if (ret)
945 return ret;
946
947 *devp = dev;
948
949 return 0;
950}
951
952int device_next_child_ofdata_err(struct udevice **devp)
953{
954 struct udevice *dev = *devp;
955 int ret;
956
957 device_find_next_child(&dev);
958 if (!dev)
959 return -ENODEV;
960
Simon Glassaad29ae2020-12-03 16:55:21 -0700961 ret = device_of_to_plat(dev);
Simon Glass000676b2020-01-27 08:49:47 -0700962 if (ret)
963 return ret;
964
965 *devp = dev;
966
967 return 0;
968}
969
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600970struct udevice *dev_get_parent(const struct udevice *child)
Simon Glass43f488a2014-11-11 10:46:19 -0700971{
972 return child->parent;
973}
974
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600975ulong dev_get_driver_data(const struct udevice *dev)
Simon Glass70c3a0e2014-11-11 10:46:18 -0700976{
Simon Glass46227bd2015-03-25 12:21:55 -0600977 return dev->driver_data;
Simon Glass70c3a0e2014-11-11 10:46:18 -0700978}
Simon Glass98fd5d12015-01-25 08:27:04 -0700979
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600980const void *dev_get_driver_ops(const struct udevice *dev)
Przemyslaw Marczakd3ef0d72015-04-15 13:07:24 +0200981{
982 if (!dev || !dev->driver->ops)
983 return NULL;
984
985 return dev->driver->ops;
986}
987
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600988enum uclass_id device_get_uclass_id(const struct udevice *dev)
Simon Glass98fd5d12015-01-25 08:27:04 -0700989{
990 return dev->uclass->uc_drv->id;
991}
Peng Fan99b72352015-02-10 14:46:32 +0800992
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600993const char *dev_get_uclass_name(const struct udevice *dev)
Przemyslaw Marczak5ed2e422015-04-15 13:07:25 +0200994{
995 if (!dev)
996 return NULL;
997
998 return dev->uclass->uc_drv->name;
999}
1000
Simon Glass2e5e5ec2018-10-01 12:22:06 -06001001bool device_has_children(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -06001002{
1003 return !list_empty(&dev->child_head);
1004}
1005
Simon Glass2a580882020-01-27 08:49:36 -07001006bool device_has_active_children(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -06001007{
1008 struct udevice *child;
1009
1010 for (device_find_first_child(dev, &child);
1011 child;
1012 device_find_next_child(&child)) {
1013 if (device_active(child))
1014 return true;
1015 }
1016
1017 return false;
1018}
1019
Simon Glass2a580882020-01-27 08:49:36 -07001020bool device_is_last_sibling(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -06001021{
1022 struct udevice *parent = dev->parent;
1023
1024 if (!parent)
1025 return false;
1026 return list_is_last(&dev->sibling_node, &parent->child_head);
1027}
Simon Glasse3b23e22015-07-30 13:40:39 -06001028
Simon Glass7760ba22016-05-01 13:52:23 -06001029void device_set_name_alloced(struct udevice *dev)
1030{
Simon Glass6211d762020-12-19 10:40:10 -07001031 dev_or_flags(dev, DM_FLAG_NAME_ALLOCED);
Simon Glass7760ba22016-05-01 13:52:23 -06001032}
1033
Simon Glasse3b23e22015-07-30 13:40:39 -06001034int device_set_name(struct udevice *dev, const char *name)
1035{
1036 name = strdup(name);
1037 if (!name)
1038 return -ENOMEM;
1039 dev->name = name;
Simon Glass7760ba22016-05-01 13:52:23 -06001040 device_set_name_alloced(dev);
Simon Glasse3b23e22015-07-30 13:40:39 -06001041
1042 return 0;
1043}
Mugunthan V N4666bd92016-04-28 15:36:02 +05301044
Simon Glassd4f0c942020-12-22 19:30:27 -07001045void dev_set_priv(struct udevice *dev, void *priv)
1046{
Simon Glass541f62c2020-12-22 19:30:30 -07001047 dev->priv_ = priv;
Simon Glassd4f0c942020-12-22 19:30:27 -07001048}
1049
1050void dev_set_parent_priv(struct udevice *dev, void *parent_priv)
1051{
Simon Glass541f62c2020-12-22 19:30:30 -07001052 dev->parent_priv_ = parent_priv;
Simon Glassd4f0c942020-12-22 19:30:27 -07001053}
1054
1055void dev_set_uclass_priv(struct udevice *dev, void *uclass_priv)
1056{
Simon Glass541f62c2020-12-22 19:30:30 -07001057 dev->uclass_priv_ = uclass_priv;
Simon Glassd4f0c942020-12-22 19:30:27 -07001058}
1059
1060void dev_set_plat(struct udevice *dev, void *plat)
1061{
Simon Glass541f62c2020-12-22 19:30:30 -07001062 dev->plat_ = plat;
Simon Glassd4f0c942020-12-22 19:30:27 -07001063}
1064
1065void dev_set_parent_plat(struct udevice *dev, void *parent_plat)
1066{
Simon Glass541f62c2020-12-22 19:30:30 -07001067 dev->parent_plat_ = parent_plat;
Simon Glassd4f0c942020-12-22 19:30:27 -07001068}
1069
1070void dev_set_uclass_plat(struct udevice *dev, void *uclass_plat)
1071{
Simon Glass541f62c2020-12-22 19:30:30 -07001072 dev->uclass_plat_ = uclass_plat;
Simon Glassd4f0c942020-12-22 19:30:27 -07001073}
1074
Marek Vasut7f18c342019-08-31 18:03:28 +02001075#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass2a580882020-01-27 08:49:36 -07001076bool device_is_compatible(const struct udevice *dev, const char *compat)
Mugunthan V N4666bd92016-04-28 15:36:02 +05301077{
Masahiro Yamada9349bcc2018-04-19 12:14:02 +09001078 return ofnode_device_is_compatible(dev_ofnode(dev), compat);
Mugunthan V N4666bd92016-04-28 15:36:02 +05301079}
1080
1081bool of_machine_is_compatible(const char *compat)
1082{
1083 const void *fdt = gd->fdt_blob;
1084
1085 return !fdt_node_check_compatible(fdt, 0, compat);
1086}
Mario Six3bedfbe2018-06-26 08:46:50 +02001087
1088int dev_disable_by_path(const char *path)
1089{
1090 struct uclass *uc;
1091 ofnode node = ofnode_path(path);
1092 struct udevice *dev;
1093 int ret = 1;
1094
1095 if (!of_live_active())
1096 return -ENOSYS;
1097
Simon Glass784cd9e2020-12-19 10:40:17 -07001098 list_for_each_entry(uc, gd->uclass_root, sibling_node) {
Mario Six3bedfbe2018-06-26 08:46:50 +02001099 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev);
1100 if (!ret)
1101 break;
1102 }
1103
1104 if (ret)
1105 return ret;
1106
1107 ret = device_remove(dev, DM_REMOVE_NORMAL);
1108 if (ret)
1109 return ret;
1110
1111 ret = device_unbind(dev);
1112 if (ret)
1113 return ret;
1114
1115 return ofnode_set_enabled(node, false);
1116}
1117
1118int dev_enable_by_path(const char *path)
1119{
1120 ofnode node = ofnode_path(path);
1121 ofnode pnode = ofnode_get_parent(node);
1122 struct udevice *parent;
1123 int ret = 1;
1124
1125 if (!of_live_active())
1126 return -ENOSYS;
1127
1128 ret = device_find_by_ofnode(pnode, &parent);
1129 if (ret)
1130 return ret;
1131
1132 ret = ofnode_set_enabled(node, true);
1133 if (ret)
1134 return ret;
1135
Bin Meng9a9b0742018-10-10 22:06:58 -07001136 return lists_bind_fdt(parent, node, NULL, false);
Mario Six3bedfbe2018-06-26 08:46:50 +02001137}
Marek Vasut7f18c342019-08-31 18:03:28 +02001138#endif
Simon Glassb5660c42021-03-15 17:25:37 +13001139
1140#if CONFIG_IS_ENABLED(OF_PLATDATA_RT)
1141static struct udevice_rt *dev_get_rt(const struct udevice *dev)
1142{
1143 struct udevice *base = ll_entry_start(struct udevice, udevice);
1144 int idx = dev - base;
1145
1146 struct udevice_rt *urt = gd_dm_udevice_rt() + idx;
1147
1148 return urt;
1149}
1150
1151u32 dev_get_flags(const struct udevice *dev)
1152{
1153 const struct udevice_rt *urt = dev_get_rt(dev);
1154
1155 return urt->flags_;
1156}
1157
1158void dev_or_flags(const struct udevice *dev, u32 or)
1159{
1160 struct udevice_rt *urt = dev_get_rt(dev);
1161
1162 urt->flags_ |= or;
1163}
1164
1165void dev_bic_flags(const struct udevice *dev, u32 bic)
1166{
1167 struct udevice_rt *urt = dev_get_rt(dev);
1168
1169 urt->flags_ &= ~bic;
1170}
1171#endif /* OF_PLATDATA_RT */