1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Device manager
4 *
5 * Copyright (c) 2013 Google, Inc
6 *
7 * (C) Copyright 2012
8 * Pavel Herrmann <morpheus.ibis@gmail.com>
9 */
10
11 #include <common.h>
12 #include <cpu_func.h>
13 #include <log.h>
14 #include <asm/global_data.h>
15 #include <asm/io.h>
16 #include <clk.h>
17 #include <fdtdec.h>
18 #include <fdt_support.h>
19 #include <malloc.h>
20 #include <asm/cache.h>
21 #include <dm/device.h>
22 #include <dm/device-internal.h>
23 #include <dm/lists.h>
24 #include <dm/of_access.h>
25 #include <dm/pinctrl.h>
26 #include <dm/platdata.h>
27 #include <dm/read.h>
28 #include <dm/uclass.h>
29 #include <dm/uclass-internal.h>
30 #include <dm/util.h>
31 #include <linux/err.h>
32 #include <linux/list.h>
33 #include <power-domain.h>
34
35 DECLARE_GLOBAL_DATA_PTR;
36
device_bind_common(struct udevice * parent,const struct driver * drv,const char * name,void * plat,ulong driver_data,ofnode node,uint of_plat_size,struct udevice ** devp)37 static int device_bind_common(struct udevice *parent, const struct driver *drv,
38 const char *name, void *plat,
39 ulong driver_data, ofnode node,
40 uint of_plat_size, struct udevice **devp)
41 {
42 struct udevice *dev;
43 struct uclass *uc;
44 int size, ret = 0;
45 bool auto_seq = true;
46 void *ptr;
47
48 if (devp)
49 *devp = NULL;
50 if (!name)
51 return -EINVAL;
52
53 ret = uclass_get(drv->id, &uc);
54 if (ret) {
55 debug("Missing uclass for driver %s\n", drv->name);
56 return ret;
57 }
58
59 dev = calloc(1, sizeof(struct udevice));
60 if (!dev)
61 return -ENOMEM;
62
63 INIT_LIST_HEAD(&dev->sibling_node);
64 INIT_LIST_HEAD(&dev->child_head);
65 INIT_LIST_HEAD(&dev->uclass_node);
66 #ifdef CONFIG_DEVRES
67 INIT_LIST_HEAD(&dev->devres_head);
68 #endif
69 dev_set_plat(dev, plat);
70 dev->driver_data = driver_data;
71 dev->name = name;
72 dev_set_ofnode(dev, node);
73 dev->parent = parent;
74 dev->driver = drv;
75 dev->uclass = uc;
76
77 dev->seq_ = -1;
78 if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
79 (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS)) {
80 /*
81 * Some devices, such as a SPI bus, I2C bus and serial ports
82 * are numbered using aliases.
83 */
84 if (CONFIG_IS_ENABLED(OF_CONTROL) &&
85 !CONFIG_IS_ENABLED(OF_PLATDATA)) {
86 if (uc->uc_drv->name && ofnode_valid(node)) {
87 if (!dev_read_alias_seq(dev, &dev->seq_))
88 auto_seq = false;
89 }
90 }
91 }
92 if (auto_seq && !(uc->uc_drv->flags & DM_UC_FLAG_NO_AUTO_SEQ))
93 dev->seq_ = uclass_find_next_free_seq(uc);
94
95 /* Check if we need to allocate plat */
96 if (drv->plat_auto) {
97 bool alloc = !plat;
98
99 /*
100 * For of-platdata, we try use the existing data, but if
101 * plat_auto is larger, we must allocate a new space
102 */
103 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
104 if (of_plat_size)
105 dev_or_flags(dev, DM_FLAG_OF_PLATDATA);
106 if (of_plat_size < drv->plat_auto)
107 alloc = true;
108 }
109 if (alloc) {
110 dev_or_flags(dev, DM_FLAG_ALLOC_PDATA);
111 ptr = calloc(1, drv->plat_auto);
112 if (!ptr) {
113 ret = -ENOMEM;
114 goto fail_alloc1;
115 }
116
117 /*
118 * For of-platdata, copy the old plat into the new
119 * space
120 */
121 if (CONFIG_IS_ENABLED(OF_PLATDATA) && plat)
122 memcpy(ptr, plat, of_plat_size);
123 dev_set_plat(dev, ptr);
124 }
125 }
126
127 size = uc->uc_drv->per_device_plat_auto;
128 if (size) {
129 dev_or_flags(dev, DM_FLAG_ALLOC_UCLASS_PDATA);
130 ptr = calloc(1, size);
131 if (!ptr) {
132 ret = -ENOMEM;
133 goto fail_alloc2;
134 }
135 dev_set_uclass_plat(dev, ptr);
136 }
137
138 if (parent) {
139 size = parent->driver->per_child_plat_auto;
140 if (!size)
141 size = parent->uclass->uc_drv->per_child_plat_auto;
142 if (size) {
143 dev_or_flags(dev, DM_FLAG_ALLOC_PARENT_PDATA);
144 ptr = calloc(1, size);
145 if (!ptr) {
146 ret = -ENOMEM;
147 goto fail_alloc3;
148 }
149 dev_set_parent_plat(dev, ptr);
150 }
151 /* put dev into parent's successor list */
152 list_add_tail(&dev->sibling_node, &parent->child_head);
153 }
154
155 ret = uclass_bind_device(dev);
156 if (ret)
157 goto fail_uclass_bind;
158
159 /* if we fail to bind we remove device from successors and free it */
160 if (drv->bind) {
161 ret = drv->bind(dev);
162 if (ret)
163 goto fail_bind;
164 }
165 if (parent && parent->driver->child_post_bind) {
166 ret = parent->driver->child_post_bind(dev);
167 if (ret)
168 goto fail_child_post_bind;
169 }
170 if (uc->uc_drv->post_bind) {
171 ret = uc->uc_drv->post_bind(dev);
172 if (ret)
173 goto fail_uclass_post_bind;
174 }
175
176 if (parent)
177 pr_debug("Bound device %s to %s\n", dev->name, parent->name);
178 if (devp)
179 *devp = dev;
180
181 dev_or_flags(dev, DM_FLAG_BOUND);
182
183 return 0;
184
185 fail_uclass_post_bind:
186 /* There is no child unbind() method, so no clean-up required */
187 fail_child_post_bind:
188 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
189 if (drv->unbind && drv->unbind(dev)) {
190 dm_warn("unbind() method failed on dev '%s' on error path\n",
191 dev->name);
192 }
193 }
194
195 fail_bind:
196 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
197 if (uclass_unbind_device(dev)) {
198 dm_warn("Failed to unbind dev '%s' on error path\n",
199 dev->name);
200 }
201 }
202 fail_uclass_bind:
203 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
204 list_del(&dev->sibling_node);
205 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PARENT_PDATA) {
206 free(dev_get_parent_plat(dev));
207 dev_set_parent_plat(dev, NULL);
208 }
209 }
210 fail_alloc3:
211 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
212 if (dev_get_flags(dev) & DM_FLAG_ALLOC_UCLASS_PDATA) {
213 free(dev_get_uclass_plat(dev));
214 dev_set_uclass_plat(dev, NULL);
215 }
216 }
217 fail_alloc2:
218 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
219 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PDATA) {
220 free(dev_get_plat(dev));
221 dev_set_plat(dev, NULL);
222 }
223 }
224 fail_alloc1:
225 devres_release_all(dev);
226
227 free(dev);
228
229 return ret;
230 }
231
device_bind_with_driver_data(struct udevice * parent,const struct driver * drv,const char * name,ulong driver_data,ofnode node,struct udevice ** devp)232 int device_bind_with_driver_data(struct udevice *parent,
233 const struct driver *drv, const char *name,
234 ulong driver_data, ofnode node,
235 struct udevice **devp)
236 {
237 return device_bind_common(parent, drv, name, NULL, driver_data, node,
238 0, devp);
239 }
240
device_bind(struct udevice * parent,const struct driver * drv,const char * name,void * plat,ofnode node,struct udevice ** devp)241 int device_bind(struct udevice *parent, const struct driver *drv,
242 const char *name, void *plat, ofnode node,
243 struct udevice **devp)
244 {
245 return device_bind_common(parent, drv, name, plat, 0, node, 0,
246 devp);
247 }
248
device_bind_by_name(struct udevice * parent,bool pre_reloc_only,const struct driver_info * info,struct udevice ** devp)249 int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
250 const struct driver_info *info, struct udevice **devp)
251 {
252 struct driver *drv;
253 uint plat_size = 0;
254 int ret;
255
256 drv = lists_driver_lookup_name(info->name);
257 if (!drv)
258 return -ENOENT;
259 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
260 return -EPERM;
261
262 #if CONFIG_IS_ENABLED(OF_PLATDATA)
263 plat_size = info->plat_size;
264 #endif
265 ret = device_bind_common(parent, drv, info->name, (void *)info->plat, 0,
266 ofnode_null(), plat_size, devp);
267 if (ret)
268 return ret;
269
270 return ret;
271 }
272
device_reparent(struct udevice * dev,struct udevice * new_parent)273 int device_reparent(struct udevice *dev, struct udevice *new_parent)
274 {
275 struct udevice *pos, *n;
276
277 assert(dev);
278 assert(new_parent);
279
280 list_for_each_entry_safe(pos, n, &dev->parent->child_head,
281 sibling_node) {
282 if (pos->driver != dev->driver)
283 continue;
284
285 list_del(&dev->sibling_node);
286 list_add_tail(&dev->sibling_node, &new_parent->child_head);
287 dev->parent = new_parent;
288
289 break;
290 }
291
292 return 0;
293 }
294
alloc_priv(int size,uint flags)295 static void *alloc_priv(int size, uint flags)
296 {
297 void *priv;
298
299 if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
300 size = ROUND(size, ARCH_DMA_MINALIGN);
301 priv = memalign(ARCH_DMA_MINALIGN, size);
302 if (priv) {
303 memset(priv, '\0', size);
304
305 /*
306 * Ensure that the zero bytes are flushed to memory.
307 * This prevents problems if the driver uses this as
308 * both an input and an output buffer:
309 *
310 * 1. Zeroes written to buffer (here) and sit in the
311 * cache
312 * 2. Driver issues a read command to DMA
313 * 3. CPU runs out of cache space and evicts some cache
314 * data in the buffer, writing zeroes to RAM from
315 * the memset() above
316 * 4. DMA completes
317 * 5. Buffer now has some DMA data and some zeroes
318 * 6. Data being read is now incorrect
319 *
320 * To prevent this, ensure that the cache is clean
321 * within this range at the start. The driver can then
322 * use normal flush-after-write, invalidate-before-read
323 * procedures.
324 *
325 * TODO(sjg@chromium.org): Drop this microblaze
326 * exception.
327 */
328 #ifndef CONFIG_MICROBLAZE
329 flush_dcache_range((ulong)priv, (ulong)priv + size);
330 #endif
331 }
332 } else {
333 priv = calloc(1, size);
334 }
335
336 return priv;
337 }
338
339 /**
340 * device_alloc_priv() - Allocate priv/plat data required by the device
341 *
342 * @dev: Device to process
343 * @return 0 if OK, -ENOMEM if out of memory
344 */
device_alloc_priv(struct udevice * dev)345 static int device_alloc_priv(struct udevice *dev)
346 {
347 const struct driver *drv;
348 void *ptr;
349 int size;
350
351 drv = dev->driver;
352 assert(drv);
353
354 /* Allocate private data if requested and not reentered */
355 if (drv->priv_auto && !dev_get_priv(dev)) {
356 ptr = alloc_priv(drv->priv_auto, drv->flags);
357 if (!ptr)
358 return -ENOMEM;
359 dev_set_priv(dev, ptr);
360 }
361
362 /* Allocate private data if requested and not reentered */
363 size = dev->uclass->uc_drv->per_device_auto;
364 if (size && !dev_get_uclass_priv(dev)) {
365 ptr = alloc_priv(size, dev->uclass->uc_drv->flags);
366 if (!ptr)
367 return -ENOMEM;
368 dev_set_uclass_priv(dev, ptr);
369 }
370
371 /* Allocate parent data for this child */
372 if (dev->parent) {
373 size = dev->parent->driver->per_child_auto;
374 if (!size)
375 size = dev->parent->uclass->uc_drv->per_child_auto;
376 if (size && !dev_get_parent_priv(dev)) {
377 ptr = alloc_priv(size, drv->flags);
378 if (!ptr)
379 return -ENOMEM;
380 dev_set_parent_priv(dev, ptr);
381 }
382 }
383
384 return 0;
385 }
386
device_of_to_plat(struct udevice * dev)387 int device_of_to_plat(struct udevice *dev)
388 {
389 const struct driver *drv;
390 int ret;
391
392 if (!dev)
393 return -EINVAL;
394
395 if (dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID)
396 return 0;
397
398 /* Ensure all parents have ofdata */
399 if (dev->parent) {
400 ret = device_of_to_plat(dev->parent);
401 if (ret)
402 goto fail;
403
404 /*
405 * The device might have already been probed during
406 * the call to device_probe() on its parent device
407 * (e.g. PCI bridge devices). Test the flags again
408 * so that we don't mess up the device.
409 */
410 if (dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID)
411 return 0;
412 }
413
414 ret = device_alloc_priv(dev);
415 if (ret)
416 goto fail;
417
418 drv = dev->driver;
419 assert(drv);
420
421 if (drv->of_to_plat &&
422 (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_ofnode(dev))) {
423 ret = drv->of_to_plat(dev);
424 if (ret)
425 goto fail;
426 }
427
428 dev_or_flags(dev, DM_FLAG_PLATDATA_VALID);
429
430 return 0;
431 fail:
432 device_free(dev);
433
434 return ret;
435 }
436
437 /**
438 * device_get_dma_constraints() - Populate device's DMA constraints
439 *
440 * Gets a device's DMA constraints from firmware. This information is later
441 * used by drivers to translate physcal addresses to the device's bus address
442 * space. For now only device-tree is supported.
443 *
444 * @dev: Pointer to target device
445 * Return: 0 if OK or if no DMA constraints were found, error otherwise
446 */
device_get_dma_constraints(struct udevice * dev)447 static int device_get_dma_constraints(struct udevice *dev)
448 {
449 struct udevice *parent = dev->parent;
450 phys_addr_t cpu = 0;
451 dma_addr_t bus = 0;
452 u64 size = 0;
453 int ret;
454
455 if (!CONFIG_IS_ENABLED(DM_DMA) || !parent || !dev_has_ofnode(parent))
456 return 0;
457
458 /*
459 * We start parsing for dma-ranges from the device's bus node. This is
460 * specially important on nested buses.
461 */
462 ret = dev_get_dma_range(parent, &cpu, &bus, &size);
463 /* Don't return an error if no 'dma-ranges' were found */
464 if (ret && ret != -ENOENT) {
465 dm_warn("%s: failed to get DMA range, %d\n", dev->name, ret);
466 return ret;
467 }
468
469 dev_set_dma_offset(dev, cpu - bus);
470
471 return 0;
472 }
473
device_probe(struct udevice * dev)474 int device_probe(struct udevice *dev)
475 {
476 const struct driver *drv;
477 int ret;
478
479 if (!dev)
480 return -EINVAL;
481
482 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
483 return 0;
484
485 drv = dev->driver;
486 assert(drv);
487
488 ret = device_of_to_plat(dev);
489 if (ret)
490 goto fail;
491
492 /* Ensure all parents are probed */
493 if (dev->parent) {
494 ret = device_probe(dev->parent);
495 if (ret)
496 goto fail;
497
498 /*
499 * The device might have already been probed during
500 * the call to device_probe() on its parent device
501 * (e.g. PCI bridge devices). Test the flags again
502 * so that we don't mess up the device.
503 */
504 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
505 return 0;
506 }
507
508 dev_or_flags(dev, DM_FLAG_ACTIVATED);
509
510 /*
511 * Process pinctrl for everything except the root device, and
512 * continue regardless of the result of pinctrl. Don't process pinctrl
513 * settings for pinctrl devices since the device may not yet be
514 * probed.
515 *
516 * This call can produce some non-intuitive results. For example, on an
517 * x86 device where dev is the main PCI bus, the pinctrl device may be
518 * child or grandchild of that bus, meaning that the child will be
519 * probed here. If the child happens to be the P2SB and the pinctrl
520 * device is a child of that, then both the pinctrl and P2SB will be
521 * probed by this call. This works because the DM_FLAG_ACTIVATED flag
522 * is set just above. However, the PCI bus' probe() method and
523 * associated uclass methods have not yet been called.
524 */
525 if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
526 pinctrl_select_state(dev, "default");
527
528 if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
529 (device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) &&
530 !(drv->flags & DM_FLAG_DEFAULT_PD_CTRL_OFF)) {
531 ret = dev_power_domain_on(dev);
532 if (ret)
533 goto fail;
534 }
535
536 ret = device_get_dma_constraints(dev);
537 if (ret)
538 goto fail;
539
540 ret = uclass_pre_probe_device(dev);
541 if (ret)
542 goto fail;
543
544 if (dev->parent && dev->parent->driver->child_pre_probe) {
545 ret = dev->parent->driver->child_pre_probe(dev);
546 if (ret)
547 goto fail;
548 }
549
550 /* Only handle devices that have a valid ofnode */
551 if (dev_has_ofnode(dev)) {
552 /*
553 * Process 'assigned-{clocks/clock-parents/clock-rates}'
554 * properties
555 */
556 ret = clk_set_defaults(dev, 0);
557 if (ret)
558 goto fail;
559 }
560
561 if (drv->probe) {
562 ret = drv->probe(dev);
563 if (ret)
564 goto fail;
565 }
566
567 ret = uclass_post_probe_device(dev);
568 if (ret)
569 goto fail_uclass;
570
571 if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
572 pinctrl_select_state(dev, "default");
573
574 return 0;
575 fail_uclass:
576 if (device_remove(dev, DM_REMOVE_NORMAL)) {
577 dm_warn("%s: Device '%s' failed to remove on error path\n",
578 __func__, dev->name);
579 }
580 fail:
581 dev_bic_flags(dev, DM_FLAG_ACTIVATED);
582
583 device_free(dev);
584
585 return ret;
586 }
587
dev_get_plat(const struct udevice * dev)588 void *dev_get_plat(const struct udevice *dev)
589 {
590 if (!dev) {
591 dm_warn("%s: null device\n", __func__);
592 return NULL;
593 }
594
595 return dev->plat_;
596 }
597
dev_get_parent_plat(const struct udevice * dev)598 void *dev_get_parent_plat(const struct udevice *dev)
599 {
600 if (!dev) {
601 dm_warn("%s: null device\n", __func__);
602 return NULL;
603 }
604
605 return dev->parent_plat_;
606 }
607
dev_get_uclass_plat(const struct udevice * dev)608 void *dev_get_uclass_plat(const struct udevice *dev)
609 {
610 if (!dev) {
611 dm_warn("%s: null device\n", __func__);
612 return NULL;
613 }
614
615 return dev->uclass_plat_;
616 }
617
dev_get_priv(const struct udevice * dev)618 void *dev_get_priv(const struct udevice *dev)
619 {
620 if (!dev) {
621 dm_warn("%s: null device\n", __func__);
622 return NULL;
623 }
624
625 return dev->priv_;
626 }
627
dev_get_uclass_priv(const struct udevice * dev)628 void *dev_get_uclass_priv(const struct udevice *dev)
629 {
630 if (!dev) {
631 dm_warn("%s: null device\n", __func__);
632 return NULL;
633 }
634
635 return dev->uclass_priv_;
636 }
637
dev_get_parent_priv(const struct udevice * dev)638 void *dev_get_parent_priv(const struct udevice *dev)
639 {
640 if (!dev) {
641 dm_warn("%s: null device\n", __func__);
642 return NULL;
643 }
644
645 return dev->parent_priv_;
646 }
647
device_get_device_tail(struct udevice * dev,int ret,struct udevice ** devp)648 static int device_get_device_tail(struct udevice *dev, int ret,
649 struct udevice **devp)
650 {
651 if (ret)
652 return ret;
653
654 ret = device_probe(dev);
655 if (ret)
656 return ret;
657
658 *devp = dev;
659
660 return 0;
661 }
662
663 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
664 /**
665 * device_find_by_ofnode() - Return device associated with given ofnode
666 *
667 * The returned device is *not* activated.
668 *
669 * @node: The ofnode for which a associated device should be looked up
670 * @devp: Pointer to structure to hold the found device
671 * Return: 0 if OK, -ve on error
672 */
device_find_by_ofnode(ofnode node,struct udevice ** devp)673 static int device_find_by_ofnode(ofnode node, struct udevice **devp)
674 {
675 struct uclass *uc;
676 struct udevice *dev;
677 int ret;
678
679 list_for_each_entry(uc, gd->uclass_root, sibling_node) {
680 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node,
681 &dev);
682 if (!ret || dev) {
683 *devp = dev;
684 return 0;
685 }
686 }
687
688 return -ENODEV;
689 }
690 #endif
691
device_get_child(const struct udevice * parent,int index,struct udevice ** devp)692 int device_get_child(const struct udevice *parent, int index,
693 struct udevice **devp)
694 {
695 struct udevice *dev;
696
697 list_for_each_entry(dev, &parent->child_head, sibling_node) {
698 if (!index--)
699 return device_get_device_tail(dev, 0, devp);
700 }
701
702 return -ENODEV;
703 }
704
device_get_child_count(const struct udevice * parent)705 int device_get_child_count(const struct udevice *parent)
706 {
707 struct udevice *dev;
708 int count = 0;
709
710 list_for_each_entry(dev, &parent->child_head, sibling_node)
711 count++;
712
713 return count;
714 }
715
device_find_child_by_seq(const struct udevice * parent,int seq,struct udevice ** devp)716 int device_find_child_by_seq(const struct udevice *parent, int seq,
717 struct udevice **devp)
718 {
719 struct udevice *dev;
720
721 *devp = NULL;
722
723 list_for_each_entry(dev, &parent->child_head, sibling_node) {
724 if (dev->seq_ == seq) {
725 *devp = dev;
726 return 0;
727 }
728 }
729
730 return -ENODEV;
731 }
732
device_get_child_by_seq(const struct udevice * parent,int seq,struct udevice ** devp)733 int device_get_child_by_seq(const struct udevice *parent, int seq,
734 struct udevice **devp)
735 {
736 struct udevice *dev;
737 int ret;
738
739 *devp = NULL;
740 ret = device_find_child_by_seq(parent, seq, &dev);
741
742 return device_get_device_tail(dev, ret, devp);
743 }
744
device_find_child_by_of_offset(const struct udevice * parent,int of_offset,struct udevice ** devp)745 int device_find_child_by_of_offset(const struct udevice *parent, int of_offset,
746 struct udevice **devp)
747 {
748 struct udevice *dev;
749
750 *devp = NULL;
751
752 list_for_each_entry(dev, &parent->child_head, sibling_node) {
753 if (dev_of_offset(dev) == of_offset) {
754 *devp = dev;
755 return 0;
756 }
757 }
758
759 return -ENODEV;
760 }
761
device_get_child_by_of_offset(const struct udevice * parent,int node,struct udevice ** devp)762 int device_get_child_by_of_offset(const struct udevice *parent, int node,
763 struct udevice **devp)
764 {
765 struct udevice *dev;
766 int ret;
767
768 *devp = NULL;
769 ret = device_find_child_by_of_offset(parent, node, &dev);
770 return device_get_device_tail(dev, ret, devp);
771 }
772
_device_find_global_by_ofnode(struct udevice * parent,ofnode ofnode)773 static struct udevice *_device_find_global_by_ofnode(struct udevice *parent,
774 ofnode ofnode)
775 {
776 struct udevice *dev, *found;
777
778 if (ofnode_equal(dev_ofnode(parent), ofnode))
779 return parent;
780
781 list_for_each_entry(dev, &parent->child_head, sibling_node) {
782 found = _device_find_global_by_ofnode(dev, ofnode);
783 if (found)
784 return found;
785 }
786
787 return NULL;
788 }
789
device_find_global_by_ofnode(ofnode ofnode,struct udevice ** devp)790 int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp)
791 {
792 *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode);
793
794 return *devp ? 0 : -ENOENT;
795 }
796
device_get_global_by_ofnode(ofnode ofnode,struct udevice ** devp)797 int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
798 {
799 struct udevice *dev;
800
801 dev = _device_find_global_by_ofnode(gd->dm_root, ofnode);
802 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
803 }
804
805 #if CONFIG_IS_ENABLED(OF_PLATDATA)
device_get_by_driver_info(const struct driver_info * info,struct udevice ** devp)806 int device_get_by_driver_info(const struct driver_info *info,
807 struct udevice **devp)
808 {
809 struct driver_info *info_base =
810 ll_entry_start(struct driver_info, driver_info);
811 int idx = info - info_base;
812 struct driver_rt *drt = gd_dm_driver_rt() + idx;
813 struct udevice *dev;
814
815 dev = drt->dev;
816 *devp = NULL;
817
818 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
819 }
820
device_get_by_driver_info_idx(uint idx,struct udevice ** devp)821 int device_get_by_driver_info_idx(uint idx, struct udevice **devp)
822 {
823 struct driver_rt *drt = gd_dm_driver_rt() + idx;
824 struct udevice *dev;
825
826 dev = drt->dev;
827 *devp = NULL;
828
829 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
830 }
831 #endif
832
device_find_first_child(const struct udevice * parent,struct udevice ** devp)833 int device_find_first_child(const struct udevice *parent, struct udevice **devp)
834 {
835 if (list_empty(&parent->child_head)) {
836 *devp = NULL;
837 } else {
838 *devp = list_first_entry(&parent->child_head, struct udevice,
839 sibling_node);
840 }
841
842 return 0;
843 }
844
device_find_next_child(struct udevice ** devp)845 int device_find_next_child(struct udevice **devp)
846 {
847 struct udevice *dev = *devp;
848 struct udevice *parent = dev->parent;
849
850 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
851 *devp = NULL;
852 } else {
853 *devp = list_entry(dev->sibling_node.next, struct udevice,
854 sibling_node);
855 }
856
857 return 0;
858 }
859
device_find_first_inactive_child(const struct udevice * parent,enum uclass_id uclass_id,struct udevice ** devp)860 int device_find_first_inactive_child(const struct udevice *parent,
861 enum uclass_id uclass_id,
862 struct udevice **devp)
863 {
864 struct udevice *dev;
865
866 *devp = NULL;
867 list_for_each_entry(dev, &parent->child_head, sibling_node) {
868 if (!device_active(dev) &&
869 device_get_uclass_id(dev) == uclass_id) {
870 *devp = dev;
871 return 0;
872 }
873 }
874
875 return -ENODEV;
876 }
877
device_find_first_child_by_uclass(const struct udevice * parent,enum uclass_id uclass_id,struct udevice ** devp)878 int device_find_first_child_by_uclass(const struct udevice *parent,
879 enum uclass_id uclass_id,
880 struct udevice **devp)
881 {
882 struct udevice *dev;
883
884 *devp = NULL;
885 list_for_each_entry(dev, &parent->child_head, sibling_node) {
886 if (device_get_uclass_id(dev) == uclass_id) {
887 *devp = dev;
888 return 0;
889 }
890 }
891
892 return -ENODEV;
893 }
894
device_find_child_by_name(const struct udevice * parent,const char * name,struct udevice ** devp)895 int device_find_child_by_name(const struct udevice *parent, const char *name,
896 struct udevice **devp)
897 {
898 struct udevice *dev;
899
900 *devp = NULL;
901
902 list_for_each_entry(dev, &parent->child_head, sibling_node) {
903 if (!strcmp(dev->name, name)) {
904 *devp = dev;
905 return 0;
906 }
907 }
908
909 return -ENODEV;
910 }
911
device_first_child_err(struct udevice * parent,struct udevice ** devp)912 int device_first_child_err(struct udevice *parent, struct udevice **devp)
913 {
914 struct udevice *dev;
915
916 device_find_first_child(parent, &dev);
917 if (!dev)
918 return -ENODEV;
919
920 return device_get_device_tail(dev, 0, devp);
921 }
922
device_next_child_err(struct udevice ** devp)923 int device_next_child_err(struct udevice **devp)
924 {
925 struct udevice *dev = *devp;
926
927 device_find_next_child(&dev);
928 if (!dev)
929 return -ENODEV;
930
931 return device_get_device_tail(dev, 0, devp);
932 }
933
device_first_child_ofdata_err(struct udevice * parent,struct udevice ** devp)934 int device_first_child_ofdata_err(struct udevice *parent, struct udevice **devp)
935 {
936 struct udevice *dev;
937 int ret;
938
939 device_find_first_child(parent, &dev);
940 if (!dev)
941 return -ENODEV;
942
943 ret = device_of_to_plat(dev);
944 if (ret)
945 return ret;
946
947 *devp = dev;
948
949 return 0;
950 }
951
device_next_child_ofdata_err(struct udevice ** devp)952 int device_next_child_ofdata_err(struct udevice **devp)
953 {
954 struct udevice *dev = *devp;
955 int ret;
956
957 device_find_next_child(&dev);
958 if (!dev)
959 return -ENODEV;
960
961 ret = device_of_to_plat(dev);
962 if (ret)
963 return ret;
964
965 *devp = dev;
966
967 return 0;
968 }
969
dev_get_parent(const struct udevice * child)970 struct udevice *dev_get_parent(const struct udevice *child)
971 {
972 return child->parent;
973 }
974
dev_get_driver_data(const struct udevice * dev)975 ulong dev_get_driver_data(const struct udevice *dev)
976 {
977 return dev->driver_data;
978 }
979
dev_get_driver_ops(const struct udevice * dev)980 const void *dev_get_driver_ops(const struct udevice *dev)
981 {
982 if (!dev || !dev->driver->ops)
983 return NULL;
984
985 return dev->driver->ops;
986 }
987
device_get_uclass_id(const struct udevice * dev)988 enum uclass_id device_get_uclass_id(const struct udevice *dev)
989 {
990 return dev->uclass->uc_drv->id;
991 }
992
dev_get_uclass_name(const struct udevice * dev)993 const char *dev_get_uclass_name(const struct udevice *dev)
994 {
995 if (!dev)
996 return NULL;
997
998 return dev->uclass->uc_drv->name;
999 }
1000
device_has_children(const struct udevice * dev)1001 bool device_has_children(const struct udevice *dev)
1002 {
1003 return !list_empty(&dev->child_head);
1004 }
1005
device_has_active_children(const struct udevice * dev)1006 bool device_has_active_children(const struct udevice *dev)
1007 {
1008 struct udevice *child;
1009
1010 for (device_find_first_child(dev, &child);
1011 child;
1012 device_find_next_child(&child)) {
1013 if (device_active(child))
1014 return true;
1015 }
1016
1017 return false;
1018 }
1019
device_is_last_sibling(const struct udevice * dev)1020 bool device_is_last_sibling(const struct udevice *dev)
1021 {
1022 struct udevice *parent = dev->parent;
1023
1024 if (!parent)
1025 return false;
1026 return list_is_last(&dev->sibling_node, &parent->child_head);
1027 }
1028
device_set_name_alloced(struct udevice * dev)1029 void device_set_name_alloced(struct udevice *dev)
1030 {
1031 dev_or_flags(dev, DM_FLAG_NAME_ALLOCED);
1032 }
1033
device_set_name(struct udevice * dev,const char * name)1034 int device_set_name(struct udevice *dev, const char *name)
1035 {
1036 name = strdup(name);
1037 if (!name)
1038 return -ENOMEM;
1039 dev->name = name;
1040 device_set_name_alloced(dev);
1041
1042 return 0;
1043 }
1044
dev_set_priv(struct udevice * dev,void * priv)1045 void dev_set_priv(struct udevice *dev, void *priv)
1046 {
1047 dev->priv_ = priv;
1048 }
1049
dev_set_parent_priv(struct udevice * dev,void * parent_priv)1050 void dev_set_parent_priv(struct udevice *dev, void *parent_priv)
1051 {
1052 dev->parent_priv_ = parent_priv;
1053 }
1054
dev_set_uclass_priv(struct udevice * dev,void * uclass_priv)1055 void dev_set_uclass_priv(struct udevice *dev, void *uclass_priv)
1056 {
1057 dev->uclass_priv_ = uclass_priv;
1058 }
1059
dev_set_plat(struct udevice * dev,void * plat)1060 void dev_set_plat(struct udevice *dev, void *plat)
1061 {
1062 dev->plat_ = plat;
1063 }
1064
dev_set_parent_plat(struct udevice * dev,void * parent_plat)1065 void dev_set_parent_plat(struct udevice *dev, void *parent_plat)
1066 {
1067 dev->parent_plat_ = parent_plat;
1068 }
1069
dev_set_uclass_plat(struct udevice * dev,void * uclass_plat)1070 void dev_set_uclass_plat(struct udevice *dev, void *uclass_plat)
1071 {
1072 dev->uclass_plat_ = uclass_plat;
1073 }
1074
1075 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
device_is_compatible(const struct udevice * dev,const char * compat)1076 bool device_is_compatible(const struct udevice *dev, const char *compat)
1077 {
1078 return ofnode_device_is_compatible(dev_ofnode(dev), compat);
1079 }
1080
of_machine_is_compatible(const char * compat)1081 bool of_machine_is_compatible(const char *compat)
1082 {
1083 const void *fdt = gd->fdt_blob;
1084
1085 return !fdt_node_check_compatible(fdt, 0, compat);
1086 }
1087
dev_disable_by_path(const char * path)1088 int dev_disable_by_path(const char *path)
1089 {
1090 struct uclass *uc;
1091 ofnode node = ofnode_path(path);
1092 struct udevice *dev;
1093 int ret = 1;
1094
1095 if (!of_live_active())
1096 return -ENOSYS;
1097
1098 list_for_each_entry(uc, gd->uclass_root, sibling_node) {
1099 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev);
1100 if (!ret)
1101 break;
1102 }
1103
1104 if (ret)
1105 return ret;
1106
1107 ret = device_remove(dev, DM_REMOVE_NORMAL);
1108 if (ret)
1109 return ret;
1110
1111 ret = device_unbind(dev);
1112 if (ret)
1113 return ret;
1114
1115 return ofnode_set_enabled(node, false);
1116 }
1117
dev_enable_by_path(const char * path)1118 int dev_enable_by_path(const char *path)
1119 {
1120 ofnode node = ofnode_path(path);
1121 ofnode pnode = ofnode_get_parent(node);
1122 struct udevice *parent;
1123 int ret = 1;
1124
1125 if (!of_live_active())
1126 return -ENOSYS;
1127
1128 ret = device_find_by_ofnode(pnode, &parent);
1129 if (ret)
1130 return ret;
1131
1132 ret = ofnode_set_enabled(node, true);
1133 if (ret)
1134 return ret;
1135
1136 return lists_bind_fdt(parent, node, NULL, false);
1137 }
1138 #endif
1139