xref: /rk3399_rockchip-uboot/drivers/core/device.c (revision 5395ac06d271209f5904a9da4392a3d8dad7597e)
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 {
101 					list_del_init(&dev->uclass_node);
102 				}
103 			}
104 		}
105 	}
106 #endif
107 	dev = calloc(1, sizeof(struct udevice));
108 	if (!dev)
109 		return -ENOMEM;
110 
111 	INIT_LIST_HEAD(&dev->sibling_node);
112 	INIT_LIST_HEAD(&dev->child_head);
113 	INIT_LIST_HEAD(&dev->uclass_node);
114 #ifdef CONFIG_DEVRES
115 	INIT_LIST_HEAD(&dev->devres_head);
116 #endif
117 	dev->platdata = platdata;
118 	dev->driver_data = driver_data;
119 	dev->name = name;
120 	dev->node = node;
121 	dev->parent = parent;
122 	dev->driver = drv;
123 	dev->uclass = uc;
124 
125 	dev->seq = -1;
126 	dev->req_seq = -1;
127 	if (CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(DM_SEQ_ALIAS)) {
128 		/*
129 		 * Some devices, such as a SPI bus, I2C bus and serial ports
130 		 * are numbered using aliases.
131 		 *
132 		 * This is just a 'requested' sequence, and will be
133 		 * resolved (and ->seq updated) when the device is probed.
134 		 */
135 		if (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS) {
136 			if (uc->uc_drv->name && ofnode_valid(node)) {
137 				dev_read_alias_seq(dev, &dev->req_seq);
138 			}
139 		}
140 	}
141 
142 	if (drv->platdata_auto_alloc_size) {
143 		bool alloc = !platdata;
144 
145 		if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
146 			if (of_platdata_size) {
147 				dev->flags |= DM_FLAG_OF_PLATDATA;
148 				if (of_platdata_size <
149 						drv->platdata_auto_alloc_size)
150 					alloc = true;
151 			}
152 		}
153 		if (alloc) {
154 			dev->flags |= DM_FLAG_ALLOC_PDATA;
155 			dev->platdata = calloc(1,
156 					       drv->platdata_auto_alloc_size);
157 			if (!dev->platdata) {
158 				ret = -ENOMEM;
159 				goto fail_alloc1;
160 			}
161 			if (CONFIG_IS_ENABLED(OF_PLATDATA) && platdata) {
162 				memcpy(dev->platdata, platdata,
163 				       of_platdata_size);
164 			}
165 		}
166 	}
167 
168 	size = uc->uc_drv->per_device_platdata_auto_alloc_size;
169 	if (size) {
170 		dev->flags |= DM_FLAG_ALLOC_UCLASS_PDATA;
171 		dev->uclass_platdata = calloc(1, size);
172 		if (!dev->uclass_platdata) {
173 			ret = -ENOMEM;
174 			goto fail_alloc2;
175 		}
176 	}
177 
178 	if (parent) {
179 		size = parent->driver->per_child_platdata_auto_alloc_size;
180 		if (!size) {
181 			size = parent->uclass->uc_drv->
182 					per_child_platdata_auto_alloc_size;
183 		}
184 		if (size) {
185 			dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA;
186 			dev->parent_platdata = calloc(1, size);
187 			if (!dev->parent_platdata) {
188 				ret = -ENOMEM;
189 				goto fail_alloc3;
190 			}
191 		}
192 	}
193 
194 	/* put dev into parent's successor list */
195 	if (parent)
196 		list_add_tail(&dev->sibling_node, &parent->child_head);
197 
198 #ifdef CONFIG_USING_KERNEL_DTB
199 #ifdef CONFIG_USING_KERNEL_DTB_V2
200 	/*
201 	 * Put these U-Boot devices in the head of uclass device list for
202 	 * the primary get by uclass_get_device_xxx().
203 	 *
204 	 * device-list: U0, U1, U2, ... K0, K1, K2, ... (prior u-boot dev)
205 	 * device-list: K0, K1, K2, ... U0, U1, U2, ... (normal)
206 	 *
207 	 * U: u-boot dev
208 	 * K: kernel dev
209 	 */
210 	u32 i, prior_u_boot_uclass_id[] = {
211 		UCLASS_AHCI,		/* boot device */
212 		UCLASS_BLK,
213 		UCLASS_MMC,
214 		UCLASS_MTD,
215 		UCLASS_PCI,
216 		UCLASS_RKNAND,
217 		UCLASS_SPI_FLASH,
218 
219 		UCLASS_CRYPTO,		/* RSA security */
220 		UCLASS_FIRMWARE,	/* psci sysreset */
221 		UCLASS_RNG,		/* ramdom number */
222 		UCLASS_SYSCON,		/* grf, pmugrf */
223 		UCLASS_SYSRESET,	/* psci sysreset */
224 		UCLASS_WDT,		/* reliable sysreset */
225 	};
226 
227 	if (gd->flags & GD_FLG_KDTB_READY) {
228 		after_u_boot_dev = false;
229 		dev->flags |= DM_FLAG_KNRL_DTB;
230 
231 		for (i = 0; i < ARRAY_SIZE(prior_u_boot_uclass_id); i++) {
232 			if (drv->id == prior_u_boot_uclass_id[i]) {
233 				after_u_boot_dev = true;
234 				break;
235 			}
236 		}
237 
238 		/* no u-boot dev ? */
239 		if (!dev->uclass->u_boot_dev_head)
240 			dev->uclass->u_boot_dev_head = &uc->dev_head;
241 	} else {
242 		if (!dev->uclass->u_boot_dev_head)
243 			dev->uclass->u_boot_dev_head = &dev->uclass_node;
244 	}
245 #else
246 	if (gd->flags & GD_FLG_KDTB_READY)
247 		dev->flags |= DM_FLAG_KNRL_DTB;
248 #endif
249 #endif
250 	ret = uclass_bind_device(dev, after_u_boot_dev);
251 	if (ret)
252 		goto fail_uclass_bind;
253 
254 	/* if we fail to bind we remove device from successors and free it */
255 	if (drv->bind) {
256 		ret = drv->bind(dev);
257 		if (ret)
258 			goto fail_bind;
259 	}
260 	if (parent && parent->driver->child_post_bind) {
261 		ret = parent->driver->child_post_bind(dev);
262 		if (ret)
263 			goto fail_child_post_bind;
264 	}
265 	if (uc->uc_drv->post_bind) {
266 		ret = uc->uc_drv->post_bind(dev);
267 		if (ret)
268 			goto fail_uclass_post_bind;
269 	}
270 
271 	if (parent)
272 		pr_debug("Bound device %s to %s\n", dev->name, parent->name);
273 	if (devp)
274 		*devp = dev;
275 
276 	dev->flags |= DM_FLAG_BOUND;
277 
278 	return 0;
279 
280 fail_uclass_post_bind:
281 	/* There is no child unbind() method, so no clean-up required */
282 fail_child_post_bind:
283 	if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
284 		if (drv->unbind && drv->unbind(dev)) {
285 			dm_warn("unbind() method failed on dev '%s' on error path\n",
286 				dev->name);
287 		}
288 	}
289 
290 fail_bind:
291 	if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
292 		if (uclass_unbind_device(dev)) {
293 			dm_warn("Failed to unbind dev '%s' on error path\n",
294 				dev->name);
295 		}
296 	}
297 fail_uclass_bind:
298 	if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
299 		list_del(&dev->sibling_node);
300 		if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
301 			free(dev->parent_platdata);
302 			dev->parent_platdata = NULL;
303 		}
304 	}
305 fail_alloc3:
306 	if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) {
307 		free(dev->uclass_platdata);
308 		dev->uclass_platdata = NULL;
309 	}
310 fail_alloc2:
311 	if (dev->flags & DM_FLAG_ALLOC_PDATA) {
312 		free(dev->platdata);
313 		dev->platdata = NULL;
314 	}
315 fail_alloc1:
316 	devres_release_all(dev);
317 
318 	free(dev);
319 
320 	return ret;
321 }
322 
323 int device_bind_with_driver_data(struct udevice *parent,
324 				 const struct driver *drv, const char *name,
325 				 ulong driver_data, ofnode node,
326 				 struct udevice **devp)
327 {
328 	return device_bind_common(parent, drv, name, NULL, driver_data, node,
329 				  0, devp);
330 }
331 
332 int device_bind(struct udevice *parent, const struct driver *drv,
333 		const char *name, void *platdata, int of_offset,
334 		struct udevice **devp)
335 {
336 	return device_bind_common(parent, drv, name, platdata, 0,
337 				  offset_to_ofnode(of_offset), 0, devp);
338 }
339 
340 int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
341 			const struct driver_info *info, struct udevice **devp)
342 {
343 	struct driver *drv;
344 	uint platdata_size = 0;
345 
346 	drv = lists_driver_lookup_name(info->name);
347 	if (!drv)
348 		return -ENOENT;
349 	if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
350 		return -EPERM;
351 
352 #if CONFIG_IS_ENABLED(OF_PLATDATA)
353 	platdata_size = info->platdata_size;
354 #endif
355 	return device_bind_common(parent, drv, info->name,
356 			(void *)info->platdata, 0, ofnode_null(), platdata_size,
357 			devp);
358 }
359 
360 static void *alloc_priv(int size, uint flags)
361 {
362 	void *priv;
363 
364 	if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
365 		size = ROUND(size, ARCH_DMA_MINALIGN);
366 		priv = memalign(ARCH_DMA_MINALIGN, size);
367 		if (priv) {
368 			memset(priv, '\0', size);
369 
370 			/*
371 			 * Ensure that the zero bytes are flushed to memory.
372 			 * This prevents problems if the driver uses this as
373 			 * both an input and an output buffer:
374 			 *
375 			 * 1. Zeroes written to buffer (here) and sit in the
376 			 *	cache
377 			 * 2. Driver issues a read command to DMA
378 			 * 3. CPU runs out of cache space and evicts some cache
379 			 *	data in the buffer, writing zeroes to RAM from
380 			 *	the memset() above
381 			 * 4. DMA completes
382 			 * 5. Buffer now has some DMA data and some zeroes
383 			 * 6. Data being read is now incorrect
384 			 *
385 			 * To prevent this, ensure that the cache is clean
386 			 * within this range at the start. The driver can then
387 			 * use normal flush-after-write, invalidate-before-read
388 			 * procedures.
389 			 *
390 			 * TODO(sjg@chromium.org): Drop this microblaze
391 			 * exception.
392 			 */
393 #ifndef CONFIG_MICROBLAZE
394 			flush_dcache_range((ulong)priv, (ulong)priv + size);
395 #endif
396 		}
397 	} else {
398 		priv = calloc(1, size);
399 	}
400 
401 	return priv;
402 }
403 
404 int device_probe(struct udevice *dev)
405 {
406 	const struct driver *drv;
407 	int size = 0;
408 	int ret;
409 	int seq;
410 
411 	if (!dev)
412 		return -EINVAL;
413 
414 	if (dev->flags & DM_FLAG_ACTIVATED)
415 		return 0;
416 
417 	drv = dev->driver;
418 	assert(drv);
419 
420 	/* Allocate private data if requested and not reentered */
421 	if (drv->priv_auto_alloc_size && !dev->priv) {
422 		dev->priv = alloc_priv(drv->priv_auto_alloc_size, drv->flags);
423 		if (!dev->priv) {
424 			ret = -ENOMEM;
425 			goto fail;
426 		}
427 	}
428 	/* Allocate private data if requested and not reentered */
429 	size = dev->uclass->uc_drv->per_device_auto_alloc_size;
430 	if (size && !dev->uclass_priv) {
431 		dev->uclass_priv = calloc(1, size);
432 		if (!dev->uclass_priv) {
433 			ret = -ENOMEM;
434 			goto fail;
435 		}
436 	}
437 
438 	/* Ensure all parents are probed */
439 	if (dev->parent) {
440 		size = dev->parent->driver->per_child_auto_alloc_size;
441 		if (!size) {
442 			size = dev->parent->uclass->uc_drv->
443 					per_child_auto_alloc_size;
444 		}
445 		if (size && !dev->parent_priv) {
446 			dev->parent_priv = alloc_priv(size, drv->flags);
447 			if (!dev->parent_priv) {
448 				ret = -ENOMEM;
449 				goto fail;
450 			}
451 		}
452 
453 		ret = device_probe(dev->parent);
454 		if (ret)
455 			goto fail;
456 
457 		/*
458 		 * The device might have already been probed during
459 		 * the call to device_probe() on its parent device
460 		 * (e.g. PCI bridge devices). Test the flags again
461 		 * so that we don't mess up the device.
462 		 */
463 		if (dev->flags & DM_FLAG_ACTIVATED)
464 			return 0;
465 	}
466 
467 	seq = uclass_resolve_seq(dev);
468 	if (seq < 0) {
469 		ret = seq;
470 		goto fail;
471 	}
472 	dev->seq = seq;
473 
474 	dev->flags |= DM_FLAG_ACTIVATED;
475 
476 	/*
477 	 * Process pinctrl for everything except the root device, and
478 	 * continue regardless of the result of pinctrl. Don't process pinctrl
479 	 * settings for pinctrl devices since the device may not yet be
480 	 * probed.
481 	 */
482 	if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
483 		pinctrl_select_state(dev, "default");
484 
485 	ret = uclass_pre_probe_device(dev);
486 	if (ret)
487 		goto fail;
488 
489 	if (dev->parent && dev->parent->driver->child_pre_probe) {
490 		ret = dev->parent->driver->child_pre_probe(dev);
491 		if (ret)
492 			goto fail;
493 	}
494 
495 	if (drv->ofdata_to_platdata && dev_has_of_node(dev)) {
496 		ret = drv->ofdata_to_platdata(dev);
497 		if (ret)
498 			goto fail;
499 	}
500 
501 	if (drv->probe) {
502 		ret = drv->probe(dev);
503 		if (ret) {
504 			dev->flags &= ~DM_FLAG_ACTIVATED;
505 			goto fail;
506 		}
507 	}
508 
509 	ret = uclass_post_probe_device(dev);
510 	if (ret)
511 		goto fail_uclass;
512 
513 	if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL) {
514 		pinctrl_select_state(dev, "init");
515 		pinctrl_select_state(dev, "default");
516 	}
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