xref: /rk3399_rockchip-uboot/drivers/core/device.c (revision 7a38549f8980099ed3f77d30d1ba3ee692380280)
1 /*
2  * Device manager
3  *
4  * Copyright (c) 2013 Google, Inc
5  *
6  * (C) Copyright 2012
7  * Pavel Herrmann <morpheus.ibis@gmail.com>
8  *
9  * SPDX-License-Identifier:	GPL-2.0+
10  */
11 
12 #include <common.h>
13 #include <asm/io.h>
14 #include <clk.h>
15 #include <fdtdec.h>
16 #include <fdt_support.h>
17 #include <malloc.h>
18 #include <dm/device.h>
19 #include <dm/device-internal.h>
20 #include <dm/lists.h>
21 #include <dm/of_access.h>
22 #include <dm/pinctrl.h>
23 #include <dm/platdata.h>
24 #include <dm/read.h>
25 #include <dm/uclass.h>
26 #include <dm/uclass-internal.h>
27 #include <dm/util.h>
28 #include <linux/err.h>
29 #include <linux/list.h>
30 
31 DECLARE_GLOBAL_DATA_PTR;
32 
33 static int device_bind_common(struct udevice *parent, const struct driver *drv,
34 			      const char *name, void *platdata,
35 			      ulong driver_data, ofnode node,
36 			      uint of_platdata_size, struct udevice **devp)
37 {
38 	struct udevice *dev;
39 	struct uclass *uc;
40 	bool after_u_boot_dev = true;
41 	int size, ret = 0;
42 
43 	if (devp)
44 		*devp = NULL;
45 	if (!name)
46 		return -EINVAL;
47 
48 	ret = uclass_get(drv->id, &uc);
49 	if (ret) {
50 		debug("Missing uclass for driver %s\n", drv->name);
51 		return ret;
52 	}
53 
54 #if defined(CONFIG_USING_KERNEL_DTB) && !defined(CONFIG_USING_KERNEL_DTB_V2)
55 	if (gd->flags & GD_FLG_RELOC) {
56 		/* For mmc/nand/spiflash, just update from kernel dtb instead bind again*/
57 		if (drv->id == UCLASS_MMC || drv->id == UCLASS_RKNAND ||
58 		    drv->id == UCLASS_SPI_FLASH || drv->id == UCLASS_MTD ||
59 		    drv->id == UCLASS_PCI || drv->id == UCLASS_AHCI) {
60 			/*
61 			 * Reject all mmc device from kernel.
62 			 *
63 			 * - we always follow the rule: use mmc device from U-Boot
64 			 * - avoid alias id on defferent device between U-Boot and kernel
65 			 */
66 			if ((gd->flags & GD_FLG_KDTB_READY) &&
67 			     (drv->id == UCLASS_MMC))
68 				return 0;
69 
70 			list_for_each_entry(dev, &uc->dev_head, uclass_node) {
71 				if (!strcmp(name, dev->name)) {
72 					debug("%s do not bind dev already in list %s\n",
73 					      __func__, dev->name);
74 					/*
75 					 * There is no clearly reason for this
76 					 * legacy code, but remain it here since
77 					 * everything seems fine with or without
78 					 * this. Maybe removed in the future.
79 					 */
80 					dev->node = node;
81 					return 0;
82 				}
83 			}
84 		}
85 
86 		/* Use other nodes from kernel dtb */
87 		struct udevice *n;
88 
89 		list_for_each_entry_safe(dev, n, &uc->dev_head, uclass_node) {
90 			if (!strcmp(name, dev->name) &&
91 			    (dev_read_bool(dev, "u-boot,dm-pre-reloc") ||
92 			     dev_read_bool(dev, "u-boot,dm-spl"))) {
93 
94 				/* Always use these node from U-Boot dtb */
95 				if (drv->id == UCLASS_CRYPTO ||
96 				    drv->id == UCLASS_WDT) {
97 					debug("%s do not delete uboot dev: %s\n",
98 					      __func__, dev->name);
99 					return 0;
100 				} else if (drv->id == UCLASS_REGULATOR) {
101 					/* stay in dm tree, in order to handle exclusion */
102 				} else {
103 					list_del_init(&dev->uclass_node);
104 				}
105 			}
106 		}
107 	}
108 #endif
109 	dev = calloc(1, sizeof(struct udevice));
110 	if (!dev)
111 		return -ENOMEM;
112 
113 	INIT_LIST_HEAD(&dev->sibling_node);
114 	INIT_LIST_HEAD(&dev->child_head);
115 	INIT_LIST_HEAD(&dev->uclass_node);
116 #ifdef CONFIG_DEVRES
117 	INIT_LIST_HEAD(&dev->devres_head);
118 #endif
119 	dev->platdata = platdata;
120 	dev->driver_data = driver_data;
121 	dev->name = name;
122 	dev->node = node;
123 	dev->parent = parent;
124 	dev->driver = drv;
125 	dev->uclass = uc;
126 
127 	dev->seq = -1;
128 	dev->req_seq = -1;
129 	if (CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(DM_SEQ_ALIAS)) {
130 		/*
131 		 * Some devices, such as a SPI bus, I2C bus and serial ports
132 		 * are numbered using aliases.
133 		 *
134 		 * This is just a 'requested' sequence, and will be
135 		 * resolved (and ->seq updated) when the device is probed.
136 		 */
137 		if (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS) {
138 			if (uc->uc_drv->name && ofnode_valid(node)) {
139 				dev_read_alias_seq(dev, &dev->req_seq);
140 			}
141 		}
142 	}
143 
144 	if (drv->platdata_auto_alloc_size) {
145 		bool alloc = !platdata;
146 
147 		if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
148 			if (of_platdata_size) {
149 				dev->flags |= DM_FLAG_OF_PLATDATA;
150 				if (of_platdata_size <
151 						drv->platdata_auto_alloc_size)
152 					alloc = true;
153 			}
154 		}
155 		if (alloc) {
156 			dev->flags |= DM_FLAG_ALLOC_PDATA;
157 			dev->platdata = calloc(1,
158 					       drv->platdata_auto_alloc_size);
159 			if (!dev->platdata) {
160 				ret = -ENOMEM;
161 				goto fail_alloc1;
162 			}
163 			if (CONFIG_IS_ENABLED(OF_PLATDATA) && platdata) {
164 				memcpy(dev->platdata, platdata,
165 				       of_platdata_size);
166 			}
167 		}
168 	}
169 
170 	size = uc->uc_drv->per_device_platdata_auto_alloc_size;
171 	if (size) {
172 		dev->flags |= DM_FLAG_ALLOC_UCLASS_PDATA;
173 		dev->uclass_platdata = calloc(1, size);
174 		if (!dev->uclass_platdata) {
175 			ret = -ENOMEM;
176 			goto fail_alloc2;
177 		}
178 	}
179 
180 	if (parent) {
181 		size = parent->driver->per_child_platdata_auto_alloc_size;
182 		if (!size) {
183 			size = parent->uclass->uc_drv->
184 					per_child_platdata_auto_alloc_size;
185 		}
186 		if (size) {
187 			dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA;
188 			dev->parent_platdata = calloc(1, size);
189 			if (!dev->parent_platdata) {
190 				ret = -ENOMEM;
191 				goto fail_alloc3;
192 			}
193 		}
194 	}
195 
196 	/* put dev into parent's successor list */
197 	if (parent)
198 		list_add_tail(&dev->sibling_node, &parent->child_head);
199 
200 #ifdef CONFIG_USING_KERNEL_DTB
201 #ifdef CONFIG_USING_KERNEL_DTB_V2
202 	/*
203 	 * Put these U-Boot devices in the head of uclass device list for
204 	 * the primary get by uclass_get_device_xxx().
205 	 *
206 	 * device-list: U0, U1, U2, ... K0, K1, K2, ... (prior u-boot dev)
207 	 * device-list: K0, K1, K2, ... U0, U1, U2, ... (normal)
208 	 *
209 	 * U: u-boot dev
210 	 * K: kernel dev
211 	 */
212 	u32 i, prior_u_boot_uclass_id[] = {
213 		UCLASS_AHCI,		/* boot device */
214 		UCLASS_BLK,
215 		UCLASS_MMC,
216 		UCLASS_MTD,
217 		UCLASS_PCI,
218 		UCLASS_RKNAND,
219 		UCLASS_SPI_FLASH,
220 
221 		UCLASS_CRYPTO,		/* RSA security */
222 		UCLASS_FIRMWARE,	/* psci sysreset */
223 		UCLASS_RNG,		/* ramdom number */
224 		UCLASS_SYSCON,		/* grf, pmugrf */
225 		UCLASS_SYSRESET,	/* psci sysreset */
226 		UCLASS_WDT,		/* reliable sysreset */
227 	};
228 
229 	if (gd->flags & GD_FLG_KDTB_READY) {
230 		after_u_boot_dev = false;
231 		dev->flags |= DM_FLAG_KNRL_DTB;
232 
233 		for (i = 0; i < ARRAY_SIZE(prior_u_boot_uclass_id); i++) {
234 			if (drv->id == prior_u_boot_uclass_id[i]) {
235 				after_u_boot_dev = true;
236 				break;
237 			}
238 		}
239 
240 		/* no u-boot dev ? */
241 		if (!dev->uclass->u_boot_dev_head)
242 			dev->uclass->u_boot_dev_head = &uc->dev_head;
243 	} else {
244 		if (!dev->uclass->u_boot_dev_head)
245 			dev->uclass->u_boot_dev_head = &dev->uclass_node;
246 	}
247 #else
248 	if (gd->flags & GD_FLG_KDTB_READY)
249 		dev->flags |= DM_FLAG_KNRL_DTB;
250 #endif
251 #endif
252 	ret = uclass_bind_device(dev, after_u_boot_dev);
253 	if (ret)
254 		goto fail_uclass_bind;
255 
256 	/* if we fail to bind we remove device from successors and free it */
257 	if (drv->bind) {
258 		ret = drv->bind(dev);
259 		if (ret)
260 			goto fail_bind;
261 	}
262 	if (parent && parent->driver->child_post_bind) {
263 		ret = parent->driver->child_post_bind(dev);
264 		if (ret)
265 			goto fail_child_post_bind;
266 	}
267 	if (uc->uc_drv->post_bind) {
268 		ret = uc->uc_drv->post_bind(dev);
269 		if (ret)
270 			goto fail_uclass_post_bind;
271 	}
272 
273 	if (parent)
274 		pr_debug("Bound device %s to %s\n", dev->name, parent->name);
275 	if (devp)
276 		*devp = dev;
277 
278 	dev->flags |= DM_FLAG_BOUND;
279 
280 	return 0;
281 
282 fail_uclass_post_bind:
283 	/* There is no child unbind() method, so no clean-up required */
284 fail_child_post_bind:
285 	if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
286 		if (drv->unbind && drv->unbind(dev)) {
287 			dm_warn("unbind() method failed on dev '%s' on error path\n",
288 				dev->name);
289 		}
290 	}
291 
292 fail_bind:
293 	if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
294 		if (uclass_unbind_device(dev)) {
295 			dm_warn("Failed to unbind dev '%s' on error path\n",
296 				dev->name);
297 		}
298 	}
299 fail_uclass_bind:
300 	if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
301 		list_del(&dev->sibling_node);
302 		if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
303 			free(dev->parent_platdata);
304 			dev->parent_platdata = NULL;
305 		}
306 	}
307 fail_alloc3:
308 	if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) {
309 		free(dev->uclass_platdata);
310 		dev->uclass_platdata = NULL;
311 	}
312 fail_alloc2:
313 	if (dev->flags & DM_FLAG_ALLOC_PDATA) {
314 		free(dev->platdata);
315 		dev->platdata = NULL;
316 	}
317 fail_alloc1:
318 	devres_release_all(dev);
319 
320 	free(dev);
321 
322 	return ret;
323 }
324 
325 int device_bind_with_driver_data(struct udevice *parent,
326 				 const struct driver *drv, const char *name,
327 				 ulong driver_data, ofnode node,
328 				 struct udevice **devp)
329 {
330 	return device_bind_common(parent, drv, name, NULL, driver_data, node,
331 				  0, devp);
332 }
333 
334 int device_bind(struct udevice *parent, const struct driver *drv,
335 		const char *name, void *platdata, int of_offset,
336 		struct udevice **devp)
337 {
338 	return device_bind_common(parent, drv, name, platdata, 0,
339 				  offset_to_ofnode(of_offset), 0, devp);
340 }
341 
342 int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
343 			const struct driver_info *info, struct udevice **devp)
344 {
345 	struct driver *drv;
346 	uint platdata_size = 0;
347 
348 	drv = lists_driver_lookup_name(info->name);
349 	if (!drv)
350 		return -ENOENT;
351 	if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
352 		return -EPERM;
353 
354 #if CONFIG_IS_ENABLED(OF_PLATDATA)
355 	platdata_size = info->platdata_size;
356 #endif
357 	return device_bind_common(parent, drv, info->name,
358 			(void *)info->platdata, 0, ofnode_null(), platdata_size,
359 			devp);
360 }
361 
362 static void *alloc_priv(int size, uint flags)
363 {
364 	void *priv;
365 
366 	if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
367 		size = ROUND(size, ARCH_DMA_MINALIGN);
368 		priv = memalign(ARCH_DMA_MINALIGN, size);
369 		if (priv) {
370 			memset(priv, '\0', size);
371 
372 			/*
373 			 * Ensure that the zero bytes are flushed to memory.
374 			 * This prevents problems if the driver uses this as
375 			 * both an input and an output buffer:
376 			 *
377 			 * 1. Zeroes written to buffer (here) and sit in the
378 			 *	cache
379 			 * 2. Driver issues a read command to DMA
380 			 * 3. CPU runs out of cache space and evicts some cache
381 			 *	data in the buffer, writing zeroes to RAM from
382 			 *	the memset() above
383 			 * 4. DMA completes
384 			 * 5. Buffer now has some DMA data and some zeroes
385 			 * 6. Data being read is now incorrect
386 			 *
387 			 * To prevent this, ensure that the cache is clean
388 			 * within this range at the start. The driver can then
389 			 * use normal flush-after-write, invalidate-before-read
390 			 * procedures.
391 			 *
392 			 * TODO(sjg@chromium.org): Drop this microblaze
393 			 * exception.
394 			 */
395 #ifndef CONFIG_MICROBLAZE
396 			flush_dcache_range((ulong)priv, (ulong)priv + size);
397 #endif
398 		}
399 	} else {
400 		priv = calloc(1, size);
401 	}
402 
403 	return priv;
404 }
405 
406 int device_probe(struct udevice *dev)
407 {
408 	const struct driver *drv;
409 	int size = 0;
410 	int ret;
411 	int seq;
412 
413 	if (!dev)
414 		return -EINVAL;
415 
416 	if (dev->flags & DM_FLAG_ACTIVATED)
417 		return 0;
418 
419 	drv = dev->driver;
420 	assert(drv);
421 
422 	/* Allocate private data if requested and not reentered */
423 	if (drv->priv_auto_alloc_size && !dev->priv) {
424 		dev->priv = alloc_priv(drv->priv_auto_alloc_size, drv->flags);
425 		if (!dev->priv) {
426 			ret = -ENOMEM;
427 			goto fail;
428 		}
429 	}
430 	/* Allocate private data if requested and not reentered */
431 	size = dev->uclass->uc_drv->per_device_auto_alloc_size;
432 	if (size && !dev->uclass_priv) {
433 		dev->uclass_priv = calloc(1, size);
434 		if (!dev->uclass_priv) {
435 			ret = -ENOMEM;
436 			goto fail;
437 		}
438 	}
439 
440 	/* Ensure all parents are probed */
441 	if (dev->parent) {
442 		size = dev->parent->driver->per_child_auto_alloc_size;
443 		if (!size) {
444 			size = dev->parent->uclass->uc_drv->
445 					per_child_auto_alloc_size;
446 		}
447 		if (size && !dev->parent_priv) {
448 			dev->parent_priv = alloc_priv(size, drv->flags);
449 			if (!dev->parent_priv) {
450 				ret = -ENOMEM;
451 				goto fail;
452 			}
453 		}
454 
455 		ret = device_probe(dev->parent);
456 		if (ret)
457 			goto fail;
458 
459 		/*
460 		 * The device might have already been probed during
461 		 * the call to device_probe() on its parent device
462 		 * (e.g. PCI bridge devices). Test the flags again
463 		 * so that we don't mess up the device.
464 		 */
465 		if (dev->flags & DM_FLAG_ACTIVATED)
466 			return 0;
467 	}
468 
469 	seq = uclass_resolve_seq(dev);
470 	if (seq < 0) {
471 		ret = seq;
472 		goto fail;
473 	}
474 	dev->seq = seq;
475 
476 	dev->flags |= DM_FLAG_ACTIVATED;
477 
478 	/*
479 	 * Process pinctrl for everything except the root device, and
480 	 * continue regardless of the result of pinctrl. Don't process pinctrl
481 	 * settings for pinctrl devices since the device may not yet be
482 	 * probed.
483 	 */
484 	if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
485 		pinctrl_select_state(dev, "default");
486 
487 	ret = uclass_pre_probe_device(dev);
488 	if (ret)
489 		goto fail;
490 
491 	if (dev->parent && dev->parent->driver->child_pre_probe) {
492 		ret = dev->parent->driver->child_pre_probe(dev);
493 		if (ret)
494 			goto fail;
495 	}
496 
497 	if (drv->ofdata_to_platdata && dev_has_of_node(dev)) {
498 		ret = drv->ofdata_to_platdata(dev);
499 		if (ret)
500 			goto fail;
501 	}
502 
503 	if (drv->probe) {
504 		ret = drv->probe(dev);
505 		if (ret) {
506 			dev->flags &= ~DM_FLAG_ACTIVATED;
507 			goto fail;
508 		}
509 	}
510 
511 	ret = uclass_post_probe_device(dev);
512 	if (ret)
513 		goto fail_uclass;
514 
515 	if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
516 		pinctrl_select_state(dev, "default");
517 
518 	return 0;
519 fail_uclass:
520 	if (device_remove(dev, DM_REMOVE_NORMAL)) {
521 		dm_warn("%s: Device '%s' failed to remove on error path\n",
522 			__func__, dev->name);
523 	}
524 fail:
525 	dev->flags &= ~DM_FLAG_ACTIVATED;
526 
527 	dev->seq = -1;
528 	device_free(dev);
529 
530 	return ret;
531 }
532 
533 void *dev_get_platdata(struct udevice *dev)
534 {
535 	if (!dev) {
536 		dm_warn("%s: null device\n", __func__);
537 		return NULL;
538 	}
539 
540 	return dev->platdata;
541 }
542 
543 void *dev_get_parent_platdata(struct udevice *dev)
544 {
545 	if (!dev) {
546 		dm_warn("%s: null device\n", __func__);
547 		return NULL;
548 	}
549 
550 	return dev->parent_platdata;
551 }
552 
553 void *dev_get_uclass_platdata(struct udevice *dev)
554 {
555 	if (!dev) {
556 		dm_warn("%s: null device\n", __func__);
557 		return NULL;
558 	}
559 
560 	return dev->uclass_platdata;
561 }
562 
563 void *dev_get_priv(struct udevice *dev)
564 {
565 	if (!dev) {
566 		dm_warn("%s: null device\n", __func__);
567 		return NULL;
568 	}
569 
570 	return dev->priv;
571 }
572 
573 void *dev_get_uclass_priv(struct udevice *dev)
574 {
575 	if (!dev) {
576 		dm_warn("%s: null device\n", __func__);
577 		return NULL;
578 	}
579 
580 	return dev->uclass_priv;
581 }
582 
583 void *dev_get_parent_priv(struct udevice *dev)
584 {
585 	if (!dev) {
586 		dm_warn("%s: null device\n", __func__);
587 		return NULL;
588 	}
589 
590 	return dev->parent_priv;
591 }
592 
593 static int device_get_device_tail(struct udevice *dev, int ret,
594 				  struct udevice **devp)
595 {
596 	if (ret)
597 		return ret;
598 
599 	ret = device_probe(dev);
600 	if (ret)
601 		return ret;
602 
603 	*devp = dev;
604 
605 	return 0;
606 }
607 
608 int device_get_child(struct udevice *parent, int index, struct udevice **devp)
609 {
610 	struct udevice *dev;
611 
612 	list_for_each_entry(dev, &parent->child_head, sibling_node) {
613 		if (!index--)
614 			return device_get_device_tail(dev, 0, devp);
615 	}
616 
617 	return -ENODEV;
618 }
619 
620 int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
621 			     bool find_req_seq, struct udevice **devp)
622 {
623 	struct udevice *dev;
624 
625 	*devp = NULL;
626 	if (seq_or_req_seq == -1)
627 		return -ENODEV;
628 
629 	list_for_each_entry(dev, &parent->child_head, sibling_node) {
630 		if ((find_req_seq ? dev->req_seq : dev->seq) ==
631 				seq_or_req_seq) {
632 			*devp = dev;
633 			return 0;
634 		}
635 	}
636 
637 	return -ENODEV;
638 }
639 
640 int device_get_child_by_seq(struct udevice *parent, int seq,
641 			    struct udevice **devp)
642 {
643 	struct udevice *dev;
644 	int ret;
645 
646 	*devp = NULL;
647 	ret = device_find_child_by_seq(parent, seq, false, &dev);
648 	if (ret == -ENODEV) {
649 		/*
650 		 * We didn't find it in probed devices. See if there is one
651 		 * that will request this seq if probed.
652 		 */
653 		ret = device_find_child_by_seq(parent, seq, true, &dev);
654 	}
655 	return device_get_device_tail(dev, ret, devp);
656 }
657 
658 int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
659 				   struct udevice **devp)
660 {
661 	struct udevice *dev;
662 
663 	*devp = NULL;
664 
665 	list_for_each_entry(dev, &parent->child_head, sibling_node) {
666 		if (dev_of_offset(dev) == of_offset) {
667 			*devp = dev;
668 			return 0;
669 		}
670 	}
671 
672 	return -ENODEV;
673 }
674 
675 int device_get_child_by_of_offset(struct udevice *parent, int node,
676 				  struct udevice **devp)
677 {
678 	struct udevice *dev;
679 	int ret;
680 
681 	*devp = NULL;
682 	ret = device_find_child_by_of_offset(parent, node, &dev);
683 	return device_get_device_tail(dev, ret, devp);
684 }
685 
686 static struct udevice *_device_find_global_by_of_offset(struct udevice *parent,
687 							int of_offset)
688 {
689 	struct udevice *dev, *found;
690 
691 	if (dev_of_offset(parent) == of_offset)
692 		return parent;
693 
694 	list_for_each_entry(dev, &parent->child_head, sibling_node) {
695 		found = _device_find_global_by_of_offset(dev, of_offset);
696 		if (found)
697 			return found;
698 	}
699 
700 	return NULL;
701 }
702 
703 int device_get_global_by_of_offset(int of_offset, struct udevice **devp)
704 {
705 	struct udevice *dev;
706 
707 	dev = _device_find_global_by_of_offset(gd->dm_root, of_offset);
708 	return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
709 }
710 
711 int device_find_first_child(struct udevice *parent, struct udevice **devp)
712 {
713 	if (list_empty(&parent->child_head)) {
714 		*devp = NULL;
715 	} else {
716 		*devp = list_first_entry(&parent->child_head, struct udevice,
717 					 sibling_node);
718 	}
719 
720 	return 0;
721 }
722 
723 int device_find_next_child(struct udevice **devp)
724 {
725 	struct udevice *dev = *devp;
726 	struct udevice *parent = dev->parent;
727 
728 	if (list_is_last(&dev->sibling_node, &parent->child_head)) {
729 		*devp = NULL;
730 	} else {
731 		*devp = list_entry(dev->sibling_node.next, struct udevice,
732 				   sibling_node);
733 	}
734 
735 	return 0;
736 }
737 
738 struct udevice *dev_get_parent(struct udevice *child)
739 {
740 	return child->parent;
741 }
742 
743 ulong dev_get_driver_data(struct udevice *dev)
744 {
745 	return dev->driver_data;
746 }
747 
748 const void *dev_get_driver_ops(struct udevice *dev)
749 {
750 	if (!dev || !dev->driver->ops)
751 		return NULL;
752 
753 	return dev->driver->ops;
754 }
755 
756 enum uclass_id device_get_uclass_id(struct udevice *dev)
757 {
758 	return dev->uclass->uc_drv->id;
759 }
760 
761 const char *dev_get_uclass_name(struct udevice *dev)
762 {
763 	if (!dev)
764 		return NULL;
765 
766 	return dev->uclass->uc_drv->name;
767 }
768 
769 bool device_has_children(struct udevice *dev)
770 {
771 	return !list_empty(&dev->child_head);
772 }
773 
774 bool device_has_active_children(struct udevice *dev)
775 {
776 	struct udevice *child;
777 
778 	for (device_find_first_child(dev, &child);
779 	     child;
780 	     device_find_next_child(&child)) {
781 		if (device_active(child))
782 			return true;
783 	}
784 
785 	return false;
786 }
787 
788 bool device_is_last_sibling(struct udevice *dev)
789 {
790 	struct udevice *parent = dev->parent;
791 
792 	if (!parent)
793 		return false;
794 	return list_is_last(&dev->sibling_node, &parent->child_head);
795 }
796 
797 void device_set_name_alloced(struct udevice *dev)
798 {
799 	dev->flags |= DM_FLAG_NAME_ALLOCED;
800 }
801 
802 int device_set_name(struct udevice *dev, const char *name)
803 {
804 	name = strdup(name);
805 	if (!name)
806 		return -ENOMEM;
807 	dev->name = name;
808 	device_set_name_alloced(dev);
809 
810 	return 0;
811 }
812 
813 bool device_is_compatible(struct udevice *dev, const char *compat)
814 {
815 	const void *fdt = gd->fdt_blob;
816 	ofnode node = dev_ofnode(dev);
817 
818 	if (ofnode_is_np(node))
819 		return of_device_is_compatible(ofnode_to_np(node), compat, NULL, NULL);
820 	else
821 		return !fdt_node_check_compatible(fdt, ofnode_to_offset(node), compat);
822 }
823 
824 bool of_machine_is_compatible(const char *compat)
825 {
826 	const void *fdt = gd->fdt_blob;
827 
828 	return !fdt_node_check_compatible(fdt, 0, compat);
829 }
830