xref: /OK3568_Linux_fs/u-boot/drivers/core/device.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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 
device_bind_common(struct udevice * parent,const struct driver * drv,const char * name,void * platdata,ulong driver_data,ofnode node,uint of_platdata_size,struct udevice ** devp)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 
device_bind_with_driver_data(struct udevice * parent,const struct driver * drv,const char * name,ulong driver_data,ofnode node,struct udevice ** devp)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 
device_bind(struct udevice * parent,const struct driver * drv,const char * name,void * platdata,int of_offset,struct udevice ** devp)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 
device_bind_by_name(struct udevice * parent,bool pre_reloc_only,const struct driver_info * info,struct udevice ** devp)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 
alloc_priv(int size,uint flags)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 
device_probe(struct udevice * dev)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, "default");
515 
516 	return 0;
517 fail_uclass:
518 	if (device_remove(dev, DM_REMOVE_NORMAL)) {
519 		dm_warn("%s: Device '%s' failed to remove on error path\n",
520 			__func__, dev->name);
521 	}
522 fail:
523 	dev->flags &= ~DM_FLAG_ACTIVATED;
524 
525 	dev->seq = -1;
526 	device_free(dev);
527 
528 	return ret;
529 }
530 
dev_get_platdata(struct udevice * dev)531 void *dev_get_platdata(struct udevice *dev)
532 {
533 	if (!dev) {
534 		dm_warn("%s: null device\n", __func__);
535 		return NULL;
536 	}
537 
538 	return dev->platdata;
539 }
540 
dev_get_parent_platdata(struct udevice * dev)541 void *dev_get_parent_platdata(struct udevice *dev)
542 {
543 	if (!dev) {
544 		dm_warn("%s: null device\n", __func__);
545 		return NULL;
546 	}
547 
548 	return dev->parent_platdata;
549 }
550 
dev_get_uclass_platdata(struct udevice * dev)551 void *dev_get_uclass_platdata(struct udevice *dev)
552 {
553 	if (!dev) {
554 		dm_warn("%s: null device\n", __func__);
555 		return NULL;
556 	}
557 
558 	return dev->uclass_platdata;
559 }
560 
dev_get_priv(struct udevice * dev)561 void *dev_get_priv(struct udevice *dev)
562 {
563 	if (!dev) {
564 		dm_warn("%s: null device\n", __func__);
565 		return NULL;
566 	}
567 
568 	return dev->priv;
569 }
570 
dev_get_uclass_priv(struct udevice * dev)571 void *dev_get_uclass_priv(struct udevice *dev)
572 {
573 	if (!dev) {
574 		dm_warn("%s: null device\n", __func__);
575 		return NULL;
576 	}
577 
578 	return dev->uclass_priv;
579 }
580 
dev_get_parent_priv(struct udevice * dev)581 void *dev_get_parent_priv(struct udevice *dev)
582 {
583 	if (!dev) {
584 		dm_warn("%s: null device\n", __func__);
585 		return NULL;
586 	}
587 
588 	return dev->parent_priv;
589 }
590 
device_get_device_tail(struct udevice * dev,int ret,struct udevice ** devp)591 static int device_get_device_tail(struct udevice *dev, int ret,
592 				  struct udevice **devp)
593 {
594 	if (ret)
595 		return ret;
596 
597 	ret = device_probe(dev);
598 	if (ret)
599 		return ret;
600 
601 	*devp = dev;
602 
603 	return 0;
604 }
605 
device_get_child(struct udevice * parent,int index,struct udevice ** devp)606 int device_get_child(struct udevice *parent, int index, struct udevice **devp)
607 {
608 	struct udevice *dev;
609 
610 	list_for_each_entry(dev, &parent->child_head, sibling_node) {
611 		if (!index--)
612 			return device_get_device_tail(dev, 0, devp);
613 	}
614 
615 	return -ENODEV;
616 }
617 
device_find_child_by_seq(struct udevice * parent,int seq_or_req_seq,bool find_req_seq,struct udevice ** devp)618 int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
619 			     bool find_req_seq, struct udevice **devp)
620 {
621 	struct udevice *dev;
622 
623 	*devp = NULL;
624 	if (seq_or_req_seq == -1)
625 		return -ENODEV;
626 
627 	list_for_each_entry(dev, &parent->child_head, sibling_node) {
628 		if ((find_req_seq ? dev->req_seq : dev->seq) ==
629 				seq_or_req_seq) {
630 			*devp = dev;
631 			return 0;
632 		}
633 	}
634 
635 	return -ENODEV;
636 }
637 
device_get_child_by_seq(struct udevice * parent,int seq,struct udevice ** devp)638 int device_get_child_by_seq(struct udevice *parent, int seq,
639 			    struct udevice **devp)
640 {
641 	struct udevice *dev;
642 	int ret;
643 
644 	*devp = NULL;
645 	ret = device_find_child_by_seq(parent, seq, false, &dev);
646 	if (ret == -ENODEV) {
647 		/*
648 		 * We didn't find it in probed devices. See if there is one
649 		 * that will request this seq if probed.
650 		 */
651 		ret = device_find_child_by_seq(parent, seq, true, &dev);
652 	}
653 	return device_get_device_tail(dev, ret, devp);
654 }
655 
device_find_child_by_of_offset(struct udevice * parent,int of_offset,struct udevice ** devp)656 int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
657 				   struct udevice **devp)
658 {
659 	struct udevice *dev;
660 
661 	*devp = NULL;
662 
663 	list_for_each_entry(dev, &parent->child_head, sibling_node) {
664 		if (dev_of_offset(dev) == of_offset) {
665 			*devp = dev;
666 			return 0;
667 		}
668 	}
669 
670 	return -ENODEV;
671 }
672 
device_get_child_by_of_offset(struct udevice * parent,int node,struct udevice ** devp)673 int device_get_child_by_of_offset(struct udevice *parent, int node,
674 				  struct udevice **devp)
675 {
676 	struct udevice *dev;
677 	int ret;
678 
679 	*devp = NULL;
680 	ret = device_find_child_by_of_offset(parent, node, &dev);
681 	return device_get_device_tail(dev, ret, devp);
682 }
683 
_device_find_global_by_of_offset(struct udevice * parent,int of_offset)684 static struct udevice *_device_find_global_by_of_offset(struct udevice *parent,
685 							int of_offset)
686 {
687 	struct udevice *dev, *found;
688 
689 	if (dev_of_offset(parent) == of_offset)
690 		return parent;
691 
692 	list_for_each_entry(dev, &parent->child_head, sibling_node) {
693 		found = _device_find_global_by_of_offset(dev, of_offset);
694 		if (found)
695 			return found;
696 	}
697 
698 	return NULL;
699 }
700 
device_get_global_by_of_offset(int of_offset,struct udevice ** devp)701 int device_get_global_by_of_offset(int of_offset, struct udevice **devp)
702 {
703 	struct udevice *dev;
704 
705 	dev = _device_find_global_by_of_offset(gd->dm_root, of_offset);
706 	return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
707 }
708 
device_find_first_child(struct udevice * parent,struct udevice ** devp)709 int device_find_first_child(struct udevice *parent, struct udevice **devp)
710 {
711 	if (list_empty(&parent->child_head)) {
712 		*devp = NULL;
713 	} else {
714 		*devp = list_first_entry(&parent->child_head, struct udevice,
715 					 sibling_node);
716 	}
717 
718 	return 0;
719 }
720 
device_find_next_child(struct udevice ** devp)721 int device_find_next_child(struct udevice **devp)
722 {
723 	struct udevice *dev = *devp;
724 	struct udevice *parent = dev->parent;
725 
726 	if (list_is_last(&dev->sibling_node, &parent->child_head)) {
727 		*devp = NULL;
728 	} else {
729 		*devp = list_entry(dev->sibling_node.next, struct udevice,
730 				   sibling_node);
731 	}
732 
733 	return 0;
734 }
735 
dev_get_parent(struct udevice * child)736 struct udevice *dev_get_parent(struct udevice *child)
737 {
738 	return child->parent;
739 }
740 
dev_get_driver_data(struct udevice * dev)741 ulong dev_get_driver_data(struct udevice *dev)
742 {
743 	return dev->driver_data;
744 }
745 
dev_get_driver_ops(struct udevice * dev)746 const void *dev_get_driver_ops(struct udevice *dev)
747 {
748 	if (!dev || !dev->driver->ops)
749 		return NULL;
750 
751 	return dev->driver->ops;
752 }
753 
device_get_uclass_id(struct udevice * dev)754 enum uclass_id device_get_uclass_id(struct udevice *dev)
755 {
756 	return dev->uclass->uc_drv->id;
757 }
758 
dev_get_uclass_name(struct udevice * dev)759 const char *dev_get_uclass_name(struct udevice *dev)
760 {
761 	if (!dev)
762 		return NULL;
763 
764 	return dev->uclass->uc_drv->name;
765 }
766 
device_has_children(struct udevice * dev)767 bool device_has_children(struct udevice *dev)
768 {
769 	return !list_empty(&dev->child_head);
770 }
771 
device_has_active_children(struct udevice * dev)772 bool device_has_active_children(struct udevice *dev)
773 {
774 	struct udevice *child;
775 
776 	for (device_find_first_child(dev, &child);
777 	     child;
778 	     device_find_next_child(&child)) {
779 		if (device_active(child))
780 			return true;
781 	}
782 
783 	return false;
784 }
785 
device_is_last_sibling(struct udevice * dev)786 bool device_is_last_sibling(struct udevice *dev)
787 {
788 	struct udevice *parent = dev->parent;
789 
790 	if (!parent)
791 		return false;
792 	return list_is_last(&dev->sibling_node, &parent->child_head);
793 }
794 
device_set_name_alloced(struct udevice * dev)795 void device_set_name_alloced(struct udevice *dev)
796 {
797 	dev->flags |= DM_FLAG_NAME_ALLOCED;
798 }
799 
device_set_name(struct udevice * dev,const char * name)800 int device_set_name(struct udevice *dev, const char *name)
801 {
802 	name = strdup(name);
803 	if (!name)
804 		return -ENOMEM;
805 	dev->name = name;
806 	device_set_name_alloced(dev);
807 
808 	return 0;
809 }
810 
device_is_compatible(struct udevice * dev,const char * compat)811 bool device_is_compatible(struct udevice *dev, const char *compat)
812 {
813 	const void *fdt = gd->fdt_blob;
814 	ofnode node = dev_ofnode(dev);
815 
816 	if (ofnode_is_np(node))
817 		return of_device_is_compatible(ofnode_to_np(node), compat, NULL, NULL);
818 	else
819 		return !fdt_node_check_compatible(fdt, ofnode_to_offset(node), compat);
820 }
821 
of_machine_is_compatible(const char * compat)822 bool of_machine_is_compatible(const char *compat)
823 {
824 	const void *fdt = gd->fdt_blob;
825 
826 	return !fdt_node_check_compatible(fdt, 0, compat);
827 }
828