blob: aed093c2af139032c3e52136d4eaea980d10a138 [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>
Mark Kettenise1311b62021-10-23 16:58:01 +020031#include <iommu.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070032#include <linux/err.h>
33#include <linux/list.h>
Peng Fan6fba32d2018-07-27 10:20:38 +080034#include <power-domain.h>
Simon Glassdd6ab882014-02-26 15:59:18 -070035
Simon Glassdb6f0202014-07-23 06:55:12 -060036DECLARE_GLOBAL_DATA_PTR;
37
Stephen Warren8c93df12016-05-11 15:26:24 -060038static int device_bind_common(struct udevice *parent, const struct driver *drv,
Simon Glass71fa5b42020-12-03 16:55:18 -070039 const char *name, void *plat,
Simon Glassb0cf4b32017-05-17 17:18:11 -060040 ulong driver_data, ofnode node,
Simon Glass39edb952020-12-03 16:55:19 -070041 uint of_plat_size, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -070042{
Heiko Schocherb74fcb42014-05-22 12:43:05 +020043 struct udevice *dev;
Simon Glassdd6ab882014-02-26 15:59:18 -070044 struct uclass *uc;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +020045 int size, ret = 0;
Simon Glass7272e922020-12-16 21:20:09 -070046 bool auto_seq = true;
Simon Glass2543b162020-12-22 19:30:29 -070047 void *ptr;
Simon Glassdd6ab882014-02-26 15:59:18 -070048
Simon Glass47fd4a22021-03-15 17:25:15 +130049 if (CONFIG_IS_ENABLED(OF_PLATDATA_NO_BIND))
50 return -ENOSYS;
51
Masahiro Yamadae1cc1f02015-08-27 12:44:28 +090052 if (devp)
53 *devp = NULL;
Simon Glassdd6ab882014-02-26 15:59:18 -070054 if (!name)
55 return -EINVAL;
56
57 ret = uclass_get(drv->id, &uc);
Simon Glass43313de2015-08-30 16:55:16 -060058 if (ret) {
59 debug("Missing uclass for driver %s\n", drv->name);
Simon Glassdd6ab882014-02-26 15:59:18 -070060 return ret;
Simon Glass43313de2015-08-30 16:55:16 -060061 }
Simon Glassdd6ab882014-02-26 15:59:18 -070062
Heiko Schocherb74fcb42014-05-22 12:43:05 +020063 dev = calloc(1, sizeof(struct udevice));
Simon Glassdd6ab882014-02-26 15:59:18 -070064 if (!dev)
65 return -ENOMEM;
66
67 INIT_LIST_HEAD(&dev->sibling_node);
68 INIT_LIST_HEAD(&dev->child_head);
69 INIT_LIST_HEAD(&dev->uclass_node);
Masahiro Yamada029bfca2015-07-25 21:52:37 +090070#ifdef CONFIG_DEVRES
Masahiro Yamada8b15b162015-07-25 21:52:35 +090071 INIT_LIST_HEAD(&dev->devres_head);
Masahiro Yamada029bfca2015-07-25 21:52:37 +090072#endif
Simon Glass2543b162020-12-22 19:30:29 -070073 dev_set_plat(dev, plat);
Stephen Warren8c93df12016-05-11 15:26:24 -060074 dev->driver_data = driver_data;
Simon Glassdd6ab882014-02-26 15:59:18 -070075 dev->name = name;
Simon Glassa7ece582020-12-19 10:40:14 -070076 dev_set_ofnode(dev, node);
Simon Glassdd6ab882014-02-26 15:59:18 -070077 dev->parent = parent;
78 dev->driver = drv;
79 dev->uclass = uc;
Simon Glassdb6f0202014-07-23 06:55:12 -060080
Simon Glass5e349922020-12-19 10:40:09 -070081 dev->seq_ = -1;
Jean-Jacques Hiblota5da3002018-12-07 14:50:39 +010082 if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
83 (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS)) {
Simon Glass1bd3deb2015-02-27 22:06:30 -070084 /*
Stefan Roese9d4ce6b2016-03-16 09:58:01 +010085 * Some devices, such as a SPI bus, I2C bus and serial ports
86 * are numbered using aliases.
Stefan Roese9d4ce6b2016-03-16 09:58:01 +010087 */
Simon Glasse422f8c2020-07-09 18:39:24 -060088 if (CONFIG_IS_ENABLED(OF_CONTROL) &&
89 !CONFIG_IS_ENABLED(OF_PLATDATA)) {
Simon Glass7272e922020-12-16 21:20:09 -070090 if (uc->uc_drv->name && ofnode_valid(node)) {
Simon Glass28823e32021-07-05 16:32:39 -060091 if (!dev_read_alias_seq(dev, &dev->seq_)) {
Simon Glass38040a92020-12-16 21:20:32 -070092 auto_seq = false;
Simon Glass28823e32021-07-05 16:32:39 -060093 log_debug(" - seq=%d\n", dev->seq_);
94 }
Simon Glass7272e922020-12-16 21:20:09 -070095 }
Simon Glass0ccb0972015-01-25 08:27:05 -070096 }
Simon Glassdb6f0202014-07-23 06:55:12 -060097 }
Simon Glass3a9d5f12020-12-16 21:20:17 -070098 if (auto_seq && !(uc->uc_drv->flags & DM_UC_FLAG_NO_AUTO_SEQ))
Simon Glass5e349922020-12-19 10:40:09 -070099 dev->seq_ = uclass_find_next_free_seq(uc);
Simon Glass1bd3deb2015-02-27 22:06:30 -0700100
Simon Glasse07434b2021-02-03 21:29:43 -0700101 /* Check if we need to allocate plat */
Simon Glass71fa5b42020-12-03 16:55:18 -0700102 if (drv->plat_auto) {
103 bool alloc = !plat;
Simon Glassafbf9b82016-07-04 11:58:18 -0600104
Simon Glasse07434b2021-02-03 21:29:43 -0700105 /*
106 * For of-platdata, we try use the existing data, but if
107 * plat_auto is larger, we must allocate a new space
108 */
Simon Glassafbf9b82016-07-04 11:58:18 -0600109 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
Simon Glasse07434b2021-02-03 21:29:43 -0700110 if (of_plat_size)
Simon Glass6211d762020-12-19 10:40:10 -0700111 dev_or_flags(dev, DM_FLAG_OF_PLATDATA);
Simon Glasse07434b2021-02-03 21:29:43 -0700112 if (of_plat_size < drv->plat_auto)
113 alloc = true;
Simon Glassafbf9b82016-07-04 11:58:18 -0600114 }
115 if (alloc) {
Simon Glass6211d762020-12-19 10:40:10 -0700116 dev_or_flags(dev, DM_FLAG_ALLOC_PDATA);
Simon Glass2543b162020-12-22 19:30:29 -0700117 ptr = calloc(1, drv->plat_auto);
118 if (!ptr) {
Simon Glassafbf9b82016-07-04 11:58:18 -0600119 ret = -ENOMEM;
120 goto fail_alloc1;
121 }
Simon Glasse07434b2021-02-03 21:29:43 -0700122
123 /*
124 * For of-platdata, copy the old plat into the new
125 * space
126 */
Simon Glass2543b162020-12-22 19:30:29 -0700127 if (CONFIG_IS_ENABLED(OF_PLATDATA) && plat)
128 memcpy(ptr, plat, of_plat_size);
129 dev_set_plat(dev, ptr);
Simon Glassba750492015-01-25 08:27:00 -0700130 }
131 }
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200132
Simon Glass71fa5b42020-12-03 16:55:18 -0700133 size = uc->uc_drv->per_device_plat_auto;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200134 if (size) {
Simon Glass6211d762020-12-19 10:40:10 -0700135 dev_or_flags(dev, DM_FLAG_ALLOC_UCLASS_PDATA);
Simon Glass2543b162020-12-22 19:30:29 -0700136 ptr = calloc(1, size);
137 if (!ptr) {
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200138 ret = -ENOMEM;
139 goto fail_alloc2;
140 }
Simon Glass2543b162020-12-22 19:30:29 -0700141 dev_set_uclass_plat(dev, ptr);
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200142 }
Simon Glass11b61732015-01-25 08:27:01 -0700143
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200144 if (parent) {
Simon Glass71fa5b42020-12-03 16:55:18 -0700145 size = parent->driver->per_child_plat_auto;
Simon Glassd6206a52021-02-03 21:29:44 -0700146 if (!size)
Simon Glass71fa5b42020-12-03 16:55:18 -0700147 size = parent->uclass->uc_drv->per_child_plat_auto;
Simon Glass11b61732015-01-25 08:27:01 -0700148 if (size) {
Simon Glass6211d762020-12-19 10:40:10 -0700149 dev_or_flags(dev, DM_FLAG_ALLOC_PARENT_PDATA);
Simon Glass2543b162020-12-22 19:30:29 -0700150 ptr = calloc(1, size);
151 if (!ptr) {
Simon Glass11b61732015-01-25 08:27:01 -0700152 ret = -ENOMEM;
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200153 goto fail_alloc3;
Simon Glass11b61732015-01-25 08:27:01 -0700154 }
Simon Glass2543b162020-12-22 19:30:29 -0700155 dev_set_parent_plat(dev, ptr);
Simon Glass11b61732015-01-25 08:27:01 -0700156 }
Heinrich Schuchardt5246b372020-02-15 21:38:48 +0100157 /* put dev into parent's successor list */
Simon Glassdd6ab882014-02-26 15:59:18 -0700158 list_add_tail(&dev->sibling_node, &parent->child_head);
Heinrich Schuchardt5246b372020-02-15 21:38:48 +0100159 }
Simon Glassdd6ab882014-02-26 15:59:18 -0700160
161 ret = uclass_bind_device(dev);
162 if (ret)
Simon Glass4ebe01b2015-01-25 08:26:59 -0700163 goto fail_uclass_bind;
Simon Glassdd6ab882014-02-26 15:59:18 -0700164
165 /* if we fail to bind we remove device from successors and free it */
166 if (drv->bind) {
167 ret = drv->bind(dev);
Simon Glass4ebe01b2015-01-25 08:26:59 -0700168 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700169 goto fail_bind;
Simon Glassdd6ab882014-02-26 15:59:18 -0700170 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700171 if (parent && parent->driver->child_post_bind) {
172 ret = parent->driver->child_post_bind(dev);
173 if (ret)
174 goto fail_child_post_bind;
175 }
Simon Glass286863d2016-01-05 09:30:59 -0700176 if (uc->uc_drv->post_bind) {
177 ret = uc->uc_drv->post_bind(dev);
178 if (ret)
179 goto fail_uclass_post_bind;
180 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700181
Simon Glassdd6ab882014-02-26 15:59:18 -0700182 if (parent)
Masahiro Yamadaf70f39f2017-09-29 12:31:20 +0900183 pr_debug("Bound device %s to %s\n", dev->name, parent->name);
Masahiro Yamadae1cc1f02015-08-27 12:44:28 +0900184 if (devp)
185 *devp = dev;
Simon Glassdd6ab882014-02-26 15:59:18 -0700186
Simon Glass6211d762020-12-19 10:40:10 -0700187 dev_or_flags(dev, DM_FLAG_BOUND);
Masahiro Yamadabdbb5dd2015-07-25 21:52:34 +0900188
Simon Glassdd6ab882014-02-26 15:59:18 -0700189 return 0;
190
Simon Glass286863d2016-01-05 09:30:59 -0700191fail_uclass_post_bind:
192 /* There is no child unbind() method, so no clean-up required */
Simon Glassa4a51a02015-01-25 08:27:03 -0700193fail_child_post_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900194 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700195 if (drv->unbind && drv->unbind(dev)) {
196 dm_warn("unbind() method failed on dev '%s' on error path\n",
197 dev->name);
198 }
Simon Glassa4a51a02015-01-25 08:27:03 -0700199 }
200
Simon Glassdd6ab882014-02-26 15:59:18 -0700201fail_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900202 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700203 if (uclass_unbind_device(dev)) {
204 dm_warn("Failed to unbind dev '%s' on error path\n",
205 dev->name);
206 }
Simon Glass4ebe01b2015-01-25 08:26:59 -0700207 }
208fail_uclass_bind:
Masahiro Yamada04aa00d2015-08-12 07:31:52 +0900209 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
Simon Glasse70a4f42015-02-27 22:06:33 -0700210 list_del(&dev->sibling_node);
Simon Glass6211d762020-12-19 10:40:10 -0700211 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PARENT_PDATA) {
Simon Glass2543b162020-12-22 19:30:29 -0700212 free(dev_get_parent_plat(dev));
213 dev_set_parent_plat(dev, NULL);
Simon Glasse70a4f42015-02-27 22:06:33 -0700214 }
Simon Glass11b61732015-01-25 08:27:01 -0700215 }
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200216fail_alloc3:
Simon Glassd6206a52021-02-03 21:29:44 -0700217 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
218 if (dev_get_flags(dev) & DM_FLAG_ALLOC_UCLASS_PDATA) {
219 free(dev_get_uclass_plat(dev));
220 dev_set_uclass_plat(dev, NULL);
221 }
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200222 }
Simon Glass11b61732015-01-25 08:27:01 -0700223fail_alloc2:
Simon Glassd6206a52021-02-03 21:29:44 -0700224 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
225 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PDATA) {
226 free(dev_get_plat(dev));
227 dev_set_plat(dev, NULL);
228 }
Simon Glassba750492015-01-25 08:27:00 -0700229 }
230fail_alloc1:
Masahiro Yamada8b15b162015-07-25 21:52:35 +0900231 devres_release_all(dev);
232
Simon Glassdd6ab882014-02-26 15:59:18 -0700233 free(dev);
Simon Glass4ebe01b2015-01-25 08:26:59 -0700234
Simon Glassdd6ab882014-02-26 15:59:18 -0700235 return ret;
236}
237
Stephen Warren8c93df12016-05-11 15:26:24 -0600238int device_bind_with_driver_data(struct udevice *parent,
239 const struct driver *drv, const char *name,
Simon Glass322b2902017-05-18 20:09:05 -0600240 ulong driver_data, ofnode node,
Stephen Warren8c93df12016-05-11 15:26:24 -0600241 struct udevice **devp)
242{
Simon Glass322b2902017-05-18 20:09:05 -0600243 return device_bind_common(parent, drv, name, NULL, driver_data, node,
244 0, devp);
Stephen Warren8c93df12016-05-11 15:26:24 -0600245}
246
Simon Glass884870f2020-11-28 17:50:01 -0700247int device_bind(struct udevice *parent, const struct driver *drv,
Simon Glass71fa5b42020-12-03 16:55:18 -0700248 const char *name, void *plat, ofnode node,
Simon Glass884870f2020-11-28 17:50:01 -0700249 struct udevice **devp)
Simon Glass9eb151e2018-06-11 13:07:15 -0600250{
Simon Glass71fa5b42020-12-03 16:55:18 -0700251 return device_bind_common(parent, drv, name, plat, 0, node, 0,
Simon Glass9eb151e2018-06-11 13:07:15 -0600252 devp);
253}
254
Simon Glassfef72b72014-07-23 06:55:03 -0600255int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
Simon Glasscfd6a002020-10-03 11:31:33 -0600256 const struct driver_info *info, struct udevice **devp)
Simon Glassdd6ab882014-02-26 15:59:18 -0700257{
258 struct driver *drv;
Simon Glass39edb952020-12-03 16:55:19 -0700259 uint plat_size = 0;
Walter Lozanoffc41b02020-06-25 01:10:11 -0300260 int ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700261
262 drv = lists_driver_lookup_name(info->name);
263 if (!drv)
264 return -ENOENT;
Simon Glassfef72b72014-07-23 06:55:03 -0600265 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
266 return -EPERM;
Simon Glassdd6ab882014-02-26 15:59:18 -0700267
Simon Glassafbf9b82016-07-04 11:58:18 -0600268#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass39edb952020-12-03 16:55:19 -0700269 plat_size = info->plat_size;
Simon Glassafbf9b82016-07-04 11:58:18 -0600270#endif
Simon Glass71fa5b42020-12-03 16:55:18 -0700271 ret = device_bind_common(parent, drv, info->name, (void *)info->plat, 0,
Simon Glass39edb952020-12-03 16:55:19 -0700272 ofnode_null(), plat_size, devp);
Walter Lozanoffc41b02020-06-25 01:10:11 -0300273 if (ret)
274 return ret;
Walter Lozanoffc41b02020-06-25 01:10:11 -0300275
276 return ret;
Simon Glassdd6ab882014-02-26 15:59:18 -0700277}
278
Claudiu Bezneabf5b8232020-09-07 17:46:33 +0300279int device_reparent(struct udevice *dev, struct udevice *new_parent)
280{
281 struct udevice *pos, *n;
282
283 assert(dev);
284 assert(new_parent);
285
286 list_for_each_entry_safe(pos, n, &dev->parent->child_head,
287 sibling_node) {
288 if (pos->driver != dev->driver)
289 continue;
290
291 list_del(&dev->sibling_node);
292 list_add_tail(&dev->sibling_node, &new_parent->child_head);
293 dev->parent = new_parent;
294
295 break;
296 }
297
298 return 0;
299}
300
Simon Glass825d3f92015-03-25 12:21:53 -0600301static void *alloc_priv(int size, uint flags)
302{
303 void *priv;
304
305 if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
Faiz Abbas03020a92017-09-19 16:53:50 +0530306 size = ROUND(size, ARCH_DMA_MINALIGN);
Simon Glass825d3f92015-03-25 12:21:53 -0600307 priv = memalign(ARCH_DMA_MINALIGN, size);
Simon Glass27bc9b12017-04-04 13:00:19 -0600308 if (priv) {
Simon Glass825d3f92015-03-25 12:21:53 -0600309 memset(priv, '\0', size);
Simon Glass27bc9b12017-04-04 13:00:19 -0600310
311 /*
312 * Ensure that the zero bytes are flushed to memory.
313 * This prevents problems if the driver uses this as
314 * both an input and an output buffer:
315 *
316 * 1. Zeroes written to buffer (here) and sit in the
317 * cache
318 * 2. Driver issues a read command to DMA
319 * 3. CPU runs out of cache space and evicts some cache
320 * data in the buffer, writing zeroes to RAM from
321 * the memset() above
322 * 4. DMA completes
323 * 5. Buffer now has some DMA data and some zeroes
324 * 6. Data being read is now incorrect
325 *
326 * To prevent this, ensure that the cache is clean
327 * within this range at the start. The driver can then
328 * use normal flush-after-write, invalidate-before-read
329 * procedures.
330 *
331 * TODO(sjg@chromium.org): Drop this microblaze
332 * exception.
333 */
334#ifndef CONFIG_MICROBLAZE
335 flush_dcache_range((ulong)priv, (ulong)priv + size);
336#endif
337 }
Simon Glass825d3f92015-03-25 12:21:53 -0600338 } else {
339 priv = calloc(1, size);
340 }
341
342 return priv;
343}
344
Simon Glassad337e62020-12-19 10:40:08 -0700345/**
346 * device_alloc_priv() - Allocate priv/plat data required by the device
347 *
348 * @dev: Device to process
349 * @return 0 if OK, -ENOMEM if out of memory
350 */
351static int device_alloc_priv(struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700352{
Simon Glassa626dff2015-03-25 12:21:54 -0600353 const struct driver *drv;
Simon Glass2543b162020-12-22 19:30:29 -0700354 void *ptr;
Simon Glassad337e62020-12-19 10:40:08 -0700355 int size;
Simon Glass44c47892020-04-05 15:38:19 -0600356
Simon Glassdd6ab882014-02-26 15:59:18 -0700357 drv = dev->driver;
358 assert(drv);
359
Bin Meng82cdd782015-08-24 01:14:02 -0700360 /* Allocate private data if requested and not reentered */
Simon Glass2543b162020-12-22 19:30:29 -0700361 if (drv->priv_auto && !dev_get_priv(dev)) {
362 ptr = alloc_priv(drv->priv_auto, drv->flags);
Simon Glassad337e62020-12-19 10:40:08 -0700363 if (!ptr)
364 return -ENOMEM;
Simon Glass2543b162020-12-22 19:30:29 -0700365 dev_set_priv(dev, ptr);
Simon Glassdd6ab882014-02-26 15:59:18 -0700366 }
Simon Glassad337e62020-12-19 10:40:08 -0700367
Bin Meng82cdd782015-08-24 01:14:02 -0700368 /* Allocate private data if requested and not reentered */
Simon Glass8a2b47f2020-12-03 16:55:17 -0700369 size = dev->uclass->uc_drv->per_device_auto;
Simon Glass2543b162020-12-22 19:30:29 -0700370 if (size && !dev_get_uclass_priv(dev)) {
371 ptr = alloc_priv(size, dev->uclass->uc_drv->flags);
Simon Glassad337e62020-12-19 10:40:08 -0700372 if (!ptr)
373 return -ENOMEM;
Simon Glass2543b162020-12-22 19:30:29 -0700374 dev_set_uclass_priv(dev, ptr);
Simon Glassdd6ab882014-02-26 15:59:18 -0700375 }
376
Simon Glassa8d8f162019-12-29 21:19:18 -0700377 /* Allocate parent data for this child */
Simon Glassdd6ab882014-02-26 15:59:18 -0700378 if (dev->parent) {
Simon Glass8a2b47f2020-12-03 16:55:17 -0700379 size = dev->parent->driver->per_child_auto;
Simon Glass2543b162020-12-22 19:30:29 -0700380 if (!size)
Simon Glass8a2b47f2020-12-03 16:55:17 -0700381 size = dev->parent->uclass->uc_drv->per_child_auto;
Simon Glass2543b162020-12-22 19:30:29 -0700382 if (size && !dev_get_parent_priv(dev)) {
383 ptr = alloc_priv(size, drv->flags);
Simon Glassad337e62020-12-19 10:40:08 -0700384 if (!ptr)
385 return -ENOMEM;
Simon Glass2543b162020-12-22 19:30:29 -0700386 dev_set_parent_priv(dev, ptr);
Simon Glass60d971b2014-07-23 06:55:20 -0600387 }
Simon Glassa8d8f162019-12-29 21:19:18 -0700388 }
389
Simon Glassad337e62020-12-19 10:40:08 -0700390 return 0;
391}
392
393int device_of_to_plat(struct udevice *dev)
394{
395 const struct driver *drv;
396 int ret;
397
398 if (!dev)
399 return -EINVAL;
400
Simon Glass6211d762020-12-19 10:40:10 -0700401 if (dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID)
Simon Glassad337e62020-12-19 10:40:08 -0700402 return 0;
403
Simon Glass47fd4a22021-03-15 17:25:15 +1300404 /*
405 * This is not needed if binding is disabled, since data is allocated
406 * at build time.
407 */
408 if (!CONFIG_IS_ENABLED(OF_PLATDATA_NO_BIND)) {
409 /* Ensure all parents have ofdata */
410 if (dev->parent) {
411 ret = device_of_to_plat(dev->parent);
412 if (ret)
413 goto fail;
414
415 /*
416 * The device might have already been probed during
417 * the call to device_probe() on its parent device
418 * (e.g. PCI bridge devices). Test the flags again
419 * so that we don't mess up the device.
420 */
421 if (dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID)
422 return 0;
423 }
424
425 ret = device_alloc_priv(dev);
Simon Glassad337e62020-12-19 10:40:08 -0700426 if (ret)
427 goto fail;
Simon Glassad337e62020-12-19 10:40:08 -0700428 }
Simon Glassad337e62020-12-19 10:40:08 -0700429 drv = dev->driver;
430 assert(drv);
431
Simon Glassaad29ae2020-12-03 16:55:21 -0700432 if (drv->of_to_plat &&
Simon Glass07c17772020-12-19 10:40:12 -0700433 (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_ofnode(dev))) {
Simon Glassaad29ae2020-12-03 16:55:21 -0700434 ret = drv->of_to_plat(dev);
Simon Glassa8d8f162019-12-29 21:19:18 -0700435 if (ret)
436 goto fail;
437 }
Simon Glass60d971b2014-07-23 06:55:20 -0600438
Simon Glass6211d762020-12-19 10:40:10 -0700439 dev_or_flags(dev, DM_FLAG_PLATDATA_VALID);
Simon Glassd3c9d302019-12-29 21:19:21 -0700440
Simon Glass4a263852019-12-29 21:19:20 -0700441 return 0;
442fail:
443 device_free(dev);
444
445 return ret;
446}
447
Nicolas Saenz Juliennea2c7ead2021-01-12 13:55:24 +0100448/**
449 * device_get_dma_constraints() - Populate device's DMA constraints
450 *
451 * Gets a device's DMA constraints from firmware. This information is later
452 * used by drivers to translate physcal addresses to the device's bus address
453 * space. For now only device-tree is supported.
454 *
455 * @dev: Pointer to target device
456 * Return: 0 if OK or if no DMA constraints were found, error otherwise
457 */
458static int device_get_dma_constraints(struct udevice *dev)
459{
460 struct udevice *parent = dev->parent;
461 phys_addr_t cpu = 0;
462 dma_addr_t bus = 0;
463 u64 size = 0;
464 int ret;
465
466 if (!CONFIG_IS_ENABLED(DM_DMA) || !parent || !dev_has_ofnode(parent))
467 return 0;
468
469 /*
470 * We start parsing for dma-ranges from the device's bus node. This is
471 * specially important on nested buses.
472 */
473 ret = dev_get_dma_range(parent, &cpu, &bus, &size);
474 /* Don't return an error if no 'dma-ranges' were found */
475 if (ret && ret != -ENOENT) {
476 dm_warn("%s: failed to get DMA range, %d\n", dev->name, ret);
477 return ret;
478 }
479
480 dev_set_dma_offset(dev, cpu - bus);
481
482 return 0;
483}
484
Simon Glass4a263852019-12-29 21:19:20 -0700485int device_probe(struct udevice *dev)
486{
487 const struct driver *drv;
488 int ret;
Simon Glass4a263852019-12-29 21:19:20 -0700489
490 if (!dev)
491 return -EINVAL;
492
Simon Glass6211d762020-12-19 10:40:10 -0700493 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
Simon Glass4a263852019-12-29 21:19:20 -0700494 return 0;
495
496 drv = dev->driver;
497 assert(drv);
498
Simon Glassaad29ae2020-12-03 16:55:21 -0700499 ret = device_of_to_plat(dev);
Simon Glass4a263852019-12-29 21:19:20 -0700500 if (ret)
501 goto fail;
502
Simon Glassa8d8f162019-12-29 21:19:18 -0700503 /* Ensure all parents are probed */
504 if (dev->parent) {
Simon Glassdd6ab882014-02-26 15:59:18 -0700505 ret = device_probe(dev->parent);
506 if (ret)
507 goto fail;
Bin Meng82cdd782015-08-24 01:14:02 -0700508
509 /*
510 * The device might have already been probed during
511 * the call to device_probe() on its parent device
512 * (e.g. PCI bridge devices). Test the flags again
513 * so that we don't mess up the device.
514 */
Simon Glass6211d762020-12-19 10:40:10 -0700515 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
Bin Meng82cdd782015-08-24 01:14:02 -0700516 return 0;
Simon Glassdd6ab882014-02-26 15:59:18 -0700517 }
Simon Glassdb6f0202014-07-23 06:55:12 -0600518
Simon Glass6211d762020-12-19 10:40:10 -0700519 dev_or_flags(dev, DM_FLAG_ACTIVATED);
Simon Glass8b04e732015-03-25 12:21:56 -0600520
Simon Glassbb53bda2015-09-12 08:45:19 -0600521 /*
522 * Process pinctrl for everything except the root device, and
Simon Glass27ce9622016-01-21 19:43:25 -0700523 * continue regardless of the result of pinctrl. Don't process pinctrl
524 * settings for pinctrl devices since the device may not yet be
525 * probed.
Simon Glass303c3c22021-01-21 13:57:13 -0700526 *
527 * This call can produce some non-intuitive results. For example, on an
528 * x86 device where dev is the main PCI bus, the pinctrl device may be
529 * child or grandchild of that bus, meaning that the child will be
530 * probed here. If the child happens to be the P2SB and the pinctrl
531 * device is a child of that, then both the pinctrl and P2SB will be
532 * probed by this call. This works because the DM_FLAG_ACTIVATED flag
533 * is set just above. However, the PCI bus' probe() method and
534 * associated uclass methods have not yet been called.
Simon Glassbb53bda2015-09-12 08:45:19 -0600535 */
Simon Glass27ce9622016-01-21 19:43:25 -0700536 if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
Simon Glassbb53bda2015-09-12 08:45:19 -0600537 pinctrl_select_state(dev, "default");
Masahiro Yamadaf8efa632015-08-27 12:44:29 +0900538
Anatolij Gustschin63a34672019-07-14 21:11:01 +0200539 if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
Lokesh Vutlac10e52b2019-09-27 13:48:12 +0530540 (device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) &&
541 !(drv->flags & DM_FLAG_DEFAULT_PD_CTRL_OFF)) {
Peng Fand2201112019-09-17 09:29:22 +0000542 ret = dev_power_domain_on(dev);
543 if (ret)
544 goto fail;
Peng Fan6fba32d2018-07-27 10:20:38 +0800545 }
546
Mark Kettenise1311b62021-10-23 16:58:01 +0200547 if (CONFIG_IS_ENABLED(IOMMU) && dev->parent &&
548 (device_get_uclass_id(dev) != UCLASS_IOMMU)) {
549 ret = dev_iommu_enable(dev);
550 if (ret)
551 goto fail;
552 }
553
Nicolas Saenz Juliennea2c7ead2021-01-12 13:55:24 +0100554 ret = device_get_dma_constraints(dev);
555 if (ret)
556 goto fail;
557
Simon Glass9c1f3822015-03-05 12:25:22 -0700558 ret = uclass_pre_probe_device(dev);
Simon Glass5104b982015-01-25 08:27:10 -0700559 if (ret)
560 goto fail;
561
Simon Glassd45560d2014-07-23 06:55:21 -0600562 if (dev->parent && dev->parent->driver->child_pre_probe) {
563 ret = dev->parent->driver->child_pre_probe(dev);
564 if (ret)
565 goto fail;
566 }
567
Bin Meng513cf402019-07-05 09:23:16 -0700568 /* Only handle devices that have a valid ofnode */
Simon Glassf1d50f72020-12-19 10:40:13 -0700569 if (dev_has_ofnode(dev)) {
Bin Meng513cf402019-07-05 09:23:16 -0700570 /*
571 * Process 'assigned-{clocks/clock-parents/clock-rates}'
572 * properties
573 */
Sean Anderson08d531c2021-06-11 00:16:07 -0400574 ret = clk_set_defaults(dev, CLK_DEFAULTS_PRE);
Bin Meng513cf402019-07-05 09:23:16 -0700575 if (ret)
576 goto fail;
577 }
Philipp Tomsich9cf03b02018-01-08 13:59:18 +0100578
Simon Glassdd6ab882014-02-26 15:59:18 -0700579 if (drv->probe) {
580 ret = drv->probe(dev);
Simon Glass9046dec2019-12-29 21:19:16 -0700581 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700582 goto fail;
583 }
584
Simon Glassdd6ab882014-02-26 15:59:18 -0700585 ret = uclass_post_probe_device(dev);
Simon Glass8b04e732015-03-25 12:21:56 -0600586 if (ret)
Simon Glassdd6ab882014-02-26 15:59:18 -0700587 goto fail_uclass;
Simon Glassdd6ab882014-02-26 15:59:18 -0700588
Peng Fan7cf58dd2016-03-12 13:17:38 +0800589 if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
590 pinctrl_select_state(dev, "default");
591
Simon Glassdd6ab882014-02-26 15:59:18 -0700592 return 0;
593fail_uclass:
Stefan Roese80b5bc92017-03-20 12:51:48 +0100594 if (device_remove(dev, DM_REMOVE_NORMAL)) {
Simon Glassdd6ab882014-02-26 15:59:18 -0700595 dm_warn("%s: Device '%s' failed to remove on error path\n",
596 __func__, dev->name);
597 }
598fail:
Simon Glass6211d762020-12-19 10:40:10 -0700599 dev_bic_flags(dev, DM_FLAG_ACTIVATED);
Simon Glass8b04e732015-03-25 12:21:56 -0600600
Simon Glassdd6ab882014-02-26 15:59:18 -0700601 device_free(dev);
602
603 return ret;
604}
605
Simon Glassfa20e932020-12-03 16:55:20 -0700606void *dev_get_plat(const struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700607{
608 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700609 dm_warn("%s: null device\n", __func__);
Simon Glassdd6ab882014-02-26 15:59:18 -0700610 return NULL;
611 }
612
Simon Glass781aa3e2021-03-15 17:25:40 +1300613 return dm_priv_to_rw(dev->plat_);
Simon Glassdd6ab882014-02-26 15:59:18 -0700614}
615
Simon Glass71fa5b42020-12-03 16:55:18 -0700616void *dev_get_parent_plat(const struct udevice *dev)
Simon Glass11b61732015-01-25 08:27:01 -0700617{
618 if (!dev) {
Simon Glasscdfc5052015-07-06 16:47:40 -0600619 dm_warn("%s: null device\n", __func__);
Simon Glass11b61732015-01-25 08:27:01 -0700620 return NULL;
621 }
622
Simon Glass781aa3e2021-03-15 17:25:40 +1300623 return dm_priv_to_rw(dev->parent_plat_);
Simon Glass11b61732015-01-25 08:27:01 -0700624}
625
Simon Glass71fa5b42020-12-03 16:55:18 -0700626void *dev_get_uclass_plat(const struct udevice *dev)
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200627{
628 if (!dev) {
Simon Glasscdfc5052015-07-06 16:47:40 -0600629 dm_warn("%s: null device\n", __func__);
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200630 return NULL;
631 }
632
Simon Glass781aa3e2021-03-15 17:25:40 +1300633 return dm_priv_to_rw(dev->uclass_plat_);
Przemyslaw Marczakd850e672015-04-15 13:07:18 +0200634}
635
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600636void *dev_get_priv(const struct udevice *dev)
Simon Glassdd6ab882014-02-26 15:59:18 -0700637{
638 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700639 dm_warn("%s: null device\n", __func__);
Simon Glassdd6ab882014-02-26 15:59:18 -0700640 return NULL;
641 }
642
Simon Glass781aa3e2021-03-15 17:25:40 +1300643 return dm_priv_to_rw(dev->priv_);
Simon Glassdd6ab882014-02-26 15:59:18 -0700644}
Simon Glass48d4e292014-07-23 06:55:19 -0600645
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600646void *dev_get_uclass_priv(const struct udevice *dev)
Simon Glassde0977b2015-03-05 12:25:20 -0700647{
648 if (!dev) {
649 dm_warn("%s: null device\n", __func__);
650 return NULL;
651 }
652
Simon Glass781aa3e2021-03-15 17:25:40 +1300653 return dm_priv_to_rw(dev->uclass_priv_);
Simon Glassde0977b2015-03-05 12:25:20 -0700654}
655
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600656void *dev_get_parent_priv(const struct udevice *dev)
Simon Glass60d971b2014-07-23 06:55:20 -0600657{
658 if (!dev) {
Simon Glassdf1e0722014-12-10 08:55:56 -0700659 dm_warn("%s: null device\n", __func__);
Simon Glass60d971b2014-07-23 06:55:20 -0600660 return NULL;
661 }
662
Simon Glass781aa3e2021-03-15 17:25:40 +1300663 return dm_priv_to_rw(dev->parent_priv_);
Simon Glass60d971b2014-07-23 06:55:20 -0600664}
665
Simon Glass48d4e292014-07-23 06:55:19 -0600666static int device_get_device_tail(struct udevice *dev, int ret,
667 struct udevice **devp)
668{
669 if (ret)
670 return ret;
671
672 ret = device_probe(dev);
673 if (ret)
674 return ret;
675
676 *devp = dev;
677
678 return 0;
679}
680
Simon Glass3580f6d2021-08-07 07:24:03 -0600681#if CONFIG_IS_ENABLED(OF_REAL)
Mario Six3bedfbe2018-06-26 08:46:50 +0200682/**
683 * device_find_by_ofnode() - Return device associated with given ofnode
684 *
685 * The returned device is *not* activated.
686 *
687 * @node: The ofnode for which a associated device should be looked up
688 * @devp: Pointer to structure to hold the found device
689 * Return: 0 if OK, -ve on error
690 */
691static int device_find_by_ofnode(ofnode node, struct udevice **devp)
692{
693 struct uclass *uc;
694 struct udevice *dev;
695 int ret;
696
Simon Glass784cd9e2020-12-19 10:40:17 -0700697 list_for_each_entry(uc, gd->uclass_root, sibling_node) {
Mario Six3bedfbe2018-06-26 08:46:50 +0200698 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node,
699 &dev);
700 if (!ret || dev) {
701 *devp = dev;
702 return 0;
703 }
704 }
705
706 return -ENODEV;
707}
Marek Vasut7f18c342019-08-31 18:03:28 +0200708#endif
Mario Six3bedfbe2018-06-26 08:46:50 +0200709
Simon Glass2a580882020-01-27 08:49:36 -0700710int device_get_child(const struct udevice *parent, int index,
711 struct udevice **devp)
Simon Glass48d4e292014-07-23 06:55:19 -0600712{
713 struct udevice *dev;
714
715 list_for_each_entry(dev, &parent->child_head, sibling_node) {
716 if (!index--)
717 return device_get_device_tail(dev, 0, devp);
718 }
719
720 return -ENODEV;
721}
722
Simon Glass2a580882020-01-27 08:49:36 -0700723int device_get_child_count(const struct udevice *parent)
Lokesh Vutlafa45a082019-09-04 16:01:26 +0530724{
725 struct udevice *dev;
726 int count = 0;
727
728 list_for_each_entry(dev, &parent->child_head, sibling_node)
729 count++;
730
731 return count;
732}
733
Simon Glass07e13382020-12-16 21:20:29 -0700734int device_find_child_by_seq(const struct udevice *parent, int seq,
735 struct udevice **devp)
Simon Glass48d4e292014-07-23 06:55:19 -0600736{
737 struct udevice *dev;
738
739 *devp = NULL;
Simon Glass48d4e292014-07-23 06:55:19 -0600740
741 list_for_each_entry(dev, &parent->child_head, sibling_node) {
Simon Glass5e349922020-12-19 10:40:09 -0700742 if (dev->seq_ == seq) {
Simon Glass48d4e292014-07-23 06:55:19 -0600743 *devp = dev;
744 return 0;
745 }
746 }
747
748 return -ENODEV;
749}
750
Simon Glass2a580882020-01-27 08:49:36 -0700751int device_get_child_by_seq(const struct udevice *parent, int seq,
Simon Glass48d4e292014-07-23 06:55:19 -0600752 struct udevice **devp)
753{
754 struct udevice *dev;
755 int ret;
756
757 *devp = NULL;
Simon Glass07e13382020-12-16 21:20:29 -0700758 ret = device_find_child_by_seq(parent, seq, &dev);
759
Simon Glass48d4e292014-07-23 06:55:19 -0600760 return device_get_device_tail(dev, ret, devp);
761}
762
Simon Glass2a580882020-01-27 08:49:36 -0700763int device_find_child_by_of_offset(const struct udevice *parent, int of_offset,
Simon Glass48d4e292014-07-23 06:55:19 -0600764 struct udevice **devp)
765{
766 struct udevice *dev;
767
768 *devp = NULL;
769
770 list_for_each_entry(dev, &parent->child_head, sibling_node) {
Simon Glassdd79d6e2017-01-17 16:52:55 -0700771 if (dev_of_offset(dev) == of_offset) {
Simon Glass48d4e292014-07-23 06:55:19 -0600772 *devp = dev;
773 return 0;
774 }
775 }
776
777 return -ENODEV;
778}
779
Simon Glass2a580882020-01-27 08:49:36 -0700780int device_get_child_by_of_offset(const struct udevice *parent, int node,
Simon Glass48d4e292014-07-23 06:55:19 -0600781 struct udevice **devp)
782{
783 struct udevice *dev;
784 int ret;
785
786 *devp = NULL;
Simon Glass861bc9f2015-06-23 15:38:38 -0600787 ret = device_find_child_by_of_offset(parent, node, &dev);
Simon Glass48d4e292014-07-23 06:55:19 -0600788 return device_get_device_tail(dev, ret, devp);
789}
Simon Glass44da7352014-10-13 23:41:49 -0600790
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200791static struct udevice *_device_find_global_by_ofnode(struct udevice *parent,
792 ofnode ofnode)
Simon Glassae2efac2015-06-23 15:38:37 -0600793{
794 struct udevice *dev, *found;
795
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200796 if (ofnode_equal(dev_ofnode(parent), ofnode))
Simon Glassae2efac2015-06-23 15:38:37 -0600797 return parent;
798
799 list_for_each_entry(dev, &parent->child_head, sibling_node) {
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200800 found = _device_find_global_by_ofnode(dev, ofnode);
Simon Glassae2efac2015-06-23 15:38:37 -0600801 if (found)
802 return found;
803 }
804
805 return NULL;
806}
807
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200808int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp)
809{
810 *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode);
811
812 return *devp ? 0 : -ENOENT;
813}
814
815int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
Simon Glassae2efac2015-06-23 15:38:37 -0600816{
817 struct udevice *dev;
818
Jean-Jacques Hiblota7b0d6a2018-08-09 16:17:44 +0200819 dev = _device_find_global_by_ofnode(gd->dm_root, ofnode);
Simon Glassae2efac2015-06-23 15:38:37 -0600820 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
821}
Walter Lozanoffc41b02020-06-25 01:10:11 -0300822
823#if CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass0000e0d2021-03-15 17:25:28 +1300824int device_get_by_ofplat_idx(uint idx, struct udevice **devp)
Simon Glass5792f4b2020-10-03 11:31:40 -0600825{
Simon Glass5792f4b2020-10-03 11:31:40 -0600826 struct udevice *dev;
827
Simon Glass0000e0d2021-03-15 17:25:28 +1300828 if (CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
829 struct udevice *base = ll_entry_start(struct udevice, udevice);
830
831 dev = base + idx;
832 } else {
833 struct driver_rt *drt = gd_dm_driver_rt() + idx;
834
835 dev = drt->dev;
836 }
Simon Glass5792f4b2020-10-03 11:31:40 -0600837 *devp = NULL;
838
839 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
840}
Walter Lozanoffc41b02020-06-25 01:10:11 -0300841#endif
Simon Glassae2efac2015-06-23 15:38:37 -0600842
Simon Glass2a580882020-01-27 08:49:36 -0700843int device_find_first_child(const struct udevice *parent, struct udevice **devp)
Simon Glass44da7352014-10-13 23:41:49 -0600844{
845 if (list_empty(&parent->child_head)) {
846 *devp = NULL;
847 } else {
848 *devp = list_first_entry(&parent->child_head, struct udevice,
849 sibling_node);
850 }
851
852 return 0;
853}
854
855int device_find_next_child(struct udevice **devp)
856{
857 struct udevice *dev = *devp;
858 struct udevice *parent = dev->parent;
859
860 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
861 *devp = NULL;
862 } else {
863 *devp = list_entry(dev->sibling_node.next, struct udevice,
864 sibling_node);
865 }
866
867 return 0;
868}
Simon Glass70c3a0e2014-11-11 10:46:18 -0700869
Simon Glass2a580882020-01-27 08:49:36 -0700870int device_find_first_inactive_child(const struct udevice *parent,
Simon Glassb775e872018-10-01 12:22:07 -0600871 enum uclass_id uclass_id,
872 struct udevice **devp)
873{
874 struct udevice *dev;
875
876 *devp = NULL;
877 list_for_each_entry(dev, &parent->child_head, sibling_node) {
878 if (!device_active(dev) &&
879 device_get_uclass_id(dev) == uclass_id) {
880 *devp = dev;
881 return 0;
882 }
883 }
884
885 return -ENODEV;
886}
887
Simon Glass2a580882020-01-27 08:49:36 -0700888int device_find_first_child_by_uclass(const struct udevice *parent,
Simon Glassa11341a2018-11-18 08:14:31 -0700889 enum uclass_id uclass_id,
890 struct udevice **devp)
891{
892 struct udevice *dev;
893
894 *devp = NULL;
895 list_for_each_entry(dev, &parent->child_head, sibling_node) {
896 if (device_get_uclass_id(dev) == uclass_id) {
897 *devp = dev;
898 return 0;
899 }
900 }
901
902 return -ENODEV;
903}
904
Simon Glass4b23e452021-10-23 17:26:08 -0600905int device_find_child_by_namelen(const struct udevice *parent, const char *name,
906 int len, struct udevice **devp)
Simon Glassa11341a2018-11-18 08:14:31 -0700907{
908 struct udevice *dev;
909
910 *devp = NULL;
911
912 list_for_each_entry(dev, &parent->child_head, sibling_node) {
Simon Glass4b23e452021-10-23 17:26:08 -0600913 if (!strncmp(dev->name, name, len) &&
914 strlen(dev->name) == len) {
Simon Glassa11341a2018-11-18 08:14:31 -0700915 *devp = dev;
916 return 0;
917 }
918 }
919
920 return -ENODEV;
921}
922
Simon Glass4b23e452021-10-23 17:26:08 -0600923int device_find_child_by_name(const struct udevice *parent, const char *name,
924 struct udevice **devp)
925{
926 return device_find_child_by_namelen(parent, name, strlen(name), devp);
927}
928
Simon Glass76db9b72020-01-27 08:49:48 -0700929int device_first_child_err(struct udevice *parent, struct udevice **devp)
930{
931 struct udevice *dev;
932
933 device_find_first_child(parent, &dev);
934 if (!dev)
935 return -ENODEV;
936
937 return device_get_device_tail(dev, 0, devp);
938}
939
940int device_next_child_err(struct udevice **devp)
941{
942 struct udevice *dev = *devp;
943
944 device_find_next_child(&dev);
945 if (!dev)
946 return -ENODEV;
947
948 return device_get_device_tail(dev, 0, devp);
949}
950
Simon Glass000676b2020-01-27 08:49:47 -0700951int device_first_child_ofdata_err(struct udevice *parent, struct udevice **devp)
952{
953 struct udevice *dev;
954 int ret;
955
956 device_find_first_child(parent, &dev);
957 if (!dev)
958 return -ENODEV;
959
Simon Glassaad29ae2020-12-03 16:55:21 -0700960 ret = device_of_to_plat(dev);
Simon Glass000676b2020-01-27 08:49:47 -0700961 if (ret)
962 return ret;
963
964 *devp = dev;
965
966 return 0;
967}
968
969int device_next_child_ofdata_err(struct udevice **devp)
970{
971 struct udevice *dev = *devp;
972 int ret;
973
974 device_find_next_child(&dev);
975 if (!dev)
976 return -ENODEV;
977
Simon Glassaad29ae2020-12-03 16:55:21 -0700978 ret = device_of_to_plat(dev);
Simon Glass000676b2020-01-27 08:49:47 -0700979 if (ret)
980 return ret;
981
982 *devp = dev;
983
984 return 0;
985}
986
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600987struct udevice *dev_get_parent(const struct udevice *child)
Simon Glass43f488a2014-11-11 10:46:19 -0700988{
989 return child->parent;
990}
991
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600992ulong dev_get_driver_data(const struct udevice *dev)
Simon Glass70c3a0e2014-11-11 10:46:18 -0700993{
Simon Glass46227bd2015-03-25 12:21:55 -0600994 return dev->driver_data;
Simon Glass70c3a0e2014-11-11 10:46:18 -0700995}
Simon Glass98fd5d12015-01-25 08:27:04 -0700996
Simon Glass2e5e5ec2018-10-01 12:22:06 -0600997const void *dev_get_driver_ops(const struct udevice *dev)
Przemyslaw Marczakd3ef0d72015-04-15 13:07:24 +0200998{
999 if (!dev || !dev->driver->ops)
1000 return NULL;
1001
1002 return dev->driver->ops;
1003}
1004
Simon Glass2e5e5ec2018-10-01 12:22:06 -06001005enum uclass_id device_get_uclass_id(const struct udevice *dev)
Simon Glass98fd5d12015-01-25 08:27:04 -07001006{
1007 return dev->uclass->uc_drv->id;
1008}
Peng Fan99b72352015-02-10 14:46:32 +08001009
Simon Glass2e5e5ec2018-10-01 12:22:06 -06001010const char *dev_get_uclass_name(const struct udevice *dev)
Przemyslaw Marczak5ed2e422015-04-15 13:07:25 +02001011{
1012 if (!dev)
1013 return NULL;
1014
1015 return dev->uclass->uc_drv->name;
1016}
1017
Simon Glass2e5e5ec2018-10-01 12:22:06 -06001018bool device_has_children(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -06001019{
1020 return !list_empty(&dev->child_head);
1021}
1022
Simon Glass2a580882020-01-27 08:49:36 -07001023bool device_has_active_children(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -06001024{
1025 struct udevice *child;
1026
1027 for (device_find_first_child(dev, &child);
1028 child;
1029 device_find_next_child(&child)) {
1030 if (device_active(child))
1031 return true;
1032 }
1033
1034 return false;
1035}
1036
Simon Glass2a580882020-01-27 08:49:36 -07001037bool device_is_last_sibling(const struct udevice *dev)
Simon Glass40f829a2015-03-25 12:21:57 -06001038{
1039 struct udevice *parent = dev->parent;
1040
1041 if (!parent)
1042 return false;
1043 return list_is_last(&dev->sibling_node, &parent->child_head);
1044}
Simon Glasse3b23e22015-07-30 13:40:39 -06001045
Simon Glass7760ba22016-05-01 13:52:23 -06001046void device_set_name_alloced(struct udevice *dev)
1047{
Simon Glass6211d762020-12-19 10:40:10 -07001048 dev_or_flags(dev, DM_FLAG_NAME_ALLOCED);
Simon Glass7760ba22016-05-01 13:52:23 -06001049}
1050
Simon Glasse3b23e22015-07-30 13:40:39 -06001051int device_set_name(struct udevice *dev, const char *name)
1052{
1053 name = strdup(name);
1054 if (!name)
1055 return -ENOMEM;
1056 dev->name = name;
Simon Glass7760ba22016-05-01 13:52:23 -06001057 device_set_name_alloced(dev);
Simon Glasse3b23e22015-07-30 13:40:39 -06001058
1059 return 0;
1060}
Mugunthan V N4666bd92016-04-28 15:36:02 +05301061
Simon Glassd4f0c942020-12-22 19:30:27 -07001062void dev_set_priv(struct udevice *dev, void *priv)
1063{
Simon Glass541f62c2020-12-22 19:30:30 -07001064 dev->priv_ = priv;
Simon Glassd4f0c942020-12-22 19:30:27 -07001065}
1066
1067void dev_set_parent_priv(struct udevice *dev, void *parent_priv)
1068{
Simon Glass541f62c2020-12-22 19:30:30 -07001069 dev->parent_priv_ = parent_priv;
Simon Glassd4f0c942020-12-22 19:30:27 -07001070}
1071
1072void dev_set_uclass_priv(struct udevice *dev, void *uclass_priv)
1073{
Simon Glass541f62c2020-12-22 19:30:30 -07001074 dev->uclass_priv_ = uclass_priv;
Simon Glassd4f0c942020-12-22 19:30:27 -07001075}
1076
1077void dev_set_plat(struct udevice *dev, void *plat)
1078{
Simon Glass541f62c2020-12-22 19:30:30 -07001079 dev->plat_ = plat;
Simon Glassd4f0c942020-12-22 19:30:27 -07001080}
1081
1082void dev_set_parent_plat(struct udevice *dev, void *parent_plat)
1083{
Simon Glass541f62c2020-12-22 19:30:30 -07001084 dev->parent_plat_ = parent_plat;
Simon Glassd4f0c942020-12-22 19:30:27 -07001085}
1086
1087void dev_set_uclass_plat(struct udevice *dev, void *uclass_plat)
1088{
Simon Glass541f62c2020-12-22 19:30:30 -07001089 dev->uclass_plat_ = uclass_plat;
Simon Glassd4f0c942020-12-22 19:30:27 -07001090}
1091
Simon Glass3580f6d2021-08-07 07:24:03 -06001092#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glass2a580882020-01-27 08:49:36 -07001093bool device_is_compatible(const struct udevice *dev, const char *compat)
Mugunthan V N4666bd92016-04-28 15:36:02 +05301094{
Masahiro Yamada9349bcc2018-04-19 12:14:02 +09001095 return ofnode_device_is_compatible(dev_ofnode(dev), compat);
Mugunthan V N4666bd92016-04-28 15:36:02 +05301096}
1097
1098bool of_machine_is_compatible(const char *compat)
1099{
1100 const void *fdt = gd->fdt_blob;
1101
1102 return !fdt_node_check_compatible(fdt, 0, compat);
1103}
Mario Six3bedfbe2018-06-26 08:46:50 +02001104
1105int dev_disable_by_path(const char *path)
1106{
1107 struct uclass *uc;
1108 ofnode node = ofnode_path(path);
1109 struct udevice *dev;
1110 int ret = 1;
1111
1112 if (!of_live_active())
1113 return -ENOSYS;
1114
Simon Glass784cd9e2020-12-19 10:40:17 -07001115 list_for_each_entry(uc, gd->uclass_root, sibling_node) {
Mario Six3bedfbe2018-06-26 08:46:50 +02001116 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev);
1117 if (!ret)
1118 break;
1119 }
1120
1121 if (ret)
1122 return ret;
1123
1124 ret = device_remove(dev, DM_REMOVE_NORMAL);
1125 if (ret)
1126 return ret;
1127
1128 ret = device_unbind(dev);
1129 if (ret)
1130 return ret;
1131
1132 return ofnode_set_enabled(node, false);
1133}
1134
1135int dev_enable_by_path(const char *path)
1136{
1137 ofnode node = ofnode_path(path);
1138 ofnode pnode = ofnode_get_parent(node);
1139 struct udevice *parent;
1140 int ret = 1;
1141
1142 if (!of_live_active())
1143 return -ENOSYS;
1144
1145 ret = device_find_by_ofnode(pnode, &parent);
1146 if (ret)
1147 return ret;
1148
1149 ret = ofnode_set_enabled(node, true);
1150 if (ret)
1151 return ret;
1152
Patrice Chotardcc523cf2021-09-10 16:16:20 +02001153 return lists_bind_fdt(parent, node, NULL, NULL, false);
Mario Six3bedfbe2018-06-26 08:46:50 +02001154}
Marek Vasut7f18c342019-08-31 18:03:28 +02001155#endif
Simon Glassb5660c42021-03-15 17:25:37 +13001156
1157#if CONFIG_IS_ENABLED(OF_PLATDATA_RT)
1158static struct udevice_rt *dev_get_rt(const struct udevice *dev)
1159{
1160 struct udevice *base = ll_entry_start(struct udevice, udevice);
1161 int idx = dev - base;
1162
1163 struct udevice_rt *urt = gd_dm_udevice_rt() + idx;
1164
1165 return urt;
1166}
1167
1168u32 dev_get_flags(const struct udevice *dev)
1169{
1170 const struct udevice_rt *urt = dev_get_rt(dev);
1171
1172 return urt->flags_;
1173}
1174
1175void dev_or_flags(const struct udevice *dev, u32 or)
1176{
1177 struct udevice_rt *urt = dev_get_rt(dev);
1178
1179 urt->flags_ |= or;
1180}
1181
1182void dev_bic_flags(const struct udevice *dev, u32 bic)
1183{
1184 struct udevice_rt *urt = dev_get_rt(dev);
1185
1186 urt->flags_ &= ~bic;
1187}
1188#endif /* OF_PLATDATA_RT */