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 (CONFIG_IS_ENABLED(OF_PLATDATA_NO_BIND))
49 return -ENOSYS;
50
51 if (devp)
52 *devp = NULL;
53 if (!name)
54 return -EINVAL;
55
56 ret = uclass_get(drv->id, &uc);
57 if (ret) {
58 debug("Missing uclass for driver %s\n", drv->name);
59 return ret;
60 }
61
62 dev = calloc(1, sizeof(struct udevice));
63 if (!dev)
64 return -ENOMEM;
65
66 INIT_LIST_HEAD(&dev->sibling_node);
67 INIT_LIST_HEAD(&dev->child_head);
68 INIT_LIST_HEAD(&dev->uclass_node);
69 #ifdef CONFIG_DEVRES
70 INIT_LIST_HEAD(&dev->devres_head);
71 #endif
72 dev_set_plat(dev, plat);
73 dev->driver_data = driver_data;
74 dev->name = name;
75 dev_set_ofnode(dev, node);
76 dev->parent = parent;
77 dev->driver = drv;
78 dev->uclass = uc;
79
80 dev->seq_ = -1;
81 if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
82 (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS)) {
83 /*
84 * Some devices, such as a SPI bus, I2C bus and serial ports
85 * are numbered using aliases.
86 */
87 if (CONFIG_IS_ENABLED(OF_CONTROL) &&
88 !CONFIG_IS_ENABLED(OF_PLATDATA)) {
89 if (uc->uc_drv->name && ofnode_valid(node)) {
90 if (!dev_read_alias_seq(dev, &dev->seq_))
91 auto_seq = false;
92 }
93 }
94 }
95 if (auto_seq && !(uc->uc_drv->flags & DM_UC_FLAG_NO_AUTO_SEQ))
96 dev->seq_ = uclass_find_next_free_seq(uc);
97
98 /* Check if we need to allocate plat */
99 if (drv->plat_auto) {
100 bool alloc = !plat;
101
102 /*
103 * For of-platdata, we try use the existing data, but if
104 * plat_auto is larger, we must allocate a new space
105 */
106 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
107 if (of_plat_size)
108 dev_or_flags(dev, DM_FLAG_OF_PLATDATA);
109 if (of_plat_size < drv->plat_auto)
110 alloc = true;
111 }
112 if (alloc) {
113 dev_or_flags(dev, DM_FLAG_ALLOC_PDATA);
114 ptr = calloc(1, drv->plat_auto);
115 if (!ptr) {
116 ret = -ENOMEM;
117 goto fail_alloc1;
118 }
119
120 /*
121 * For of-platdata, copy the old plat into the new
122 * space
123 */
124 if (CONFIG_IS_ENABLED(OF_PLATDATA) && plat)
125 memcpy(ptr, plat, of_plat_size);
126 dev_set_plat(dev, ptr);
127 }
128 }
129
130 size = uc->uc_drv->per_device_plat_auto;
131 if (size) {
132 dev_or_flags(dev, DM_FLAG_ALLOC_UCLASS_PDATA);
133 ptr = calloc(1, size);
134 if (!ptr) {
135 ret = -ENOMEM;
136 goto fail_alloc2;
137 }
138 dev_set_uclass_plat(dev, ptr);
139 }
140
141 if (parent) {
142 size = parent->driver->per_child_plat_auto;
143 if (!size)
144 size = parent->uclass->uc_drv->per_child_plat_auto;
145 if (size) {
146 dev_or_flags(dev, DM_FLAG_ALLOC_PARENT_PDATA);
147 ptr = calloc(1, size);
148 if (!ptr) {
149 ret = -ENOMEM;
150 goto fail_alloc3;
151 }
152 dev_set_parent_plat(dev, ptr);
153 }
154 /* put dev into parent's successor list */
155 list_add_tail(&dev->sibling_node, &parent->child_head);
156 }
157
158 ret = uclass_bind_device(dev);
159 if (ret)
160 goto fail_uclass_bind;
161
162 /* if we fail to bind we remove device from successors and free it */
163 if (drv->bind) {
164 ret = drv->bind(dev);
165 if (ret)
166 goto fail_bind;
167 }
168 if (parent && parent->driver->child_post_bind) {
169 ret = parent->driver->child_post_bind(dev);
170 if (ret)
171 goto fail_child_post_bind;
172 }
173 if (uc->uc_drv->post_bind) {
174 ret = uc->uc_drv->post_bind(dev);
175 if (ret)
176 goto fail_uclass_post_bind;
177 }
178
179 if (parent)
180 pr_debug("Bound device %s to %s\n", dev->name, parent->name);
181 if (devp)
182 *devp = dev;
183
184 dev_or_flags(dev, DM_FLAG_BOUND);
185
186 return 0;
187
188 fail_uclass_post_bind:
189 /* There is no child unbind() method, so no clean-up required */
190 fail_child_post_bind:
191 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
192 if (drv->unbind && drv->unbind(dev)) {
193 dm_warn("unbind() method failed on dev '%s' on error path\n",
194 dev->name);
195 }
196 }
197
198 fail_bind:
199 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
200 if (uclass_unbind_device(dev)) {
201 dm_warn("Failed to unbind dev '%s' on error path\n",
202 dev->name);
203 }
204 }
205 fail_uclass_bind:
206 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
207 list_del(&dev->sibling_node);
208 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PARENT_PDATA) {
209 free(dev_get_parent_plat(dev));
210 dev_set_parent_plat(dev, NULL);
211 }
212 }
213 fail_alloc3:
214 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
215 if (dev_get_flags(dev) & DM_FLAG_ALLOC_UCLASS_PDATA) {
216 free(dev_get_uclass_plat(dev));
217 dev_set_uclass_plat(dev, NULL);
218 }
219 }
220 fail_alloc2:
221 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
222 if (dev_get_flags(dev) & DM_FLAG_ALLOC_PDATA) {
223 free(dev_get_plat(dev));
224 dev_set_plat(dev, NULL);
225 }
226 }
227 fail_alloc1:
228 devres_release_all(dev);
229
230 free(dev);
231
232 return ret;
233 }
234
device_bind_with_driver_data(struct udevice * parent,const struct driver * drv,const char * name,ulong driver_data,ofnode node,struct udevice ** devp)235 int device_bind_with_driver_data(struct udevice *parent,
236 const struct driver *drv, const char *name,
237 ulong driver_data, ofnode node,
238 struct udevice **devp)
239 {
240 return device_bind_common(parent, drv, name, NULL, driver_data, node,
241 0, devp);
242 }
243
device_bind(struct udevice * parent,const struct driver * drv,const char * name,void * plat,ofnode node,struct udevice ** devp)244 int device_bind(struct udevice *parent, const struct driver *drv,
245 const char *name, void *plat, ofnode node,
246 struct udevice **devp)
247 {
248 return device_bind_common(parent, drv, name, plat, 0, node, 0,
249 devp);
250 }
251
device_bind_by_name(struct udevice * parent,bool pre_reloc_only,const struct driver_info * info,struct udevice ** devp)252 int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
253 const struct driver_info *info, struct udevice **devp)
254 {
255 struct driver *drv;
256 uint plat_size = 0;
257 int ret;
258
259 drv = lists_driver_lookup_name(info->name);
260 if (!drv)
261 return -ENOENT;
262 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
263 return -EPERM;
264
265 #if CONFIG_IS_ENABLED(OF_PLATDATA)
266 plat_size = info->plat_size;
267 #endif
268 ret = device_bind_common(parent, drv, info->name, (void *)info->plat, 0,
269 ofnode_null(), plat_size, devp);
270 if (ret)
271 return ret;
272
273 return ret;
274 }
275
device_reparent(struct udevice * dev,struct udevice * new_parent)276 int device_reparent(struct udevice *dev, struct udevice *new_parent)
277 {
278 struct udevice *pos, *n;
279
280 assert(dev);
281 assert(new_parent);
282
283 list_for_each_entry_safe(pos, n, &dev->parent->child_head,
284 sibling_node) {
285 if (pos->driver != dev->driver)
286 continue;
287
288 list_del(&dev->sibling_node);
289 list_add_tail(&dev->sibling_node, &new_parent->child_head);
290 dev->parent = new_parent;
291
292 break;
293 }
294
295 return 0;
296 }
297
alloc_priv(int size,uint flags)298 static void *alloc_priv(int size, uint flags)
299 {
300 void *priv;
301
302 if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
303 size = ROUND(size, ARCH_DMA_MINALIGN);
304 priv = memalign(ARCH_DMA_MINALIGN, size);
305 if (priv) {
306 memset(priv, '\0', size);
307
308 /*
309 * Ensure that the zero bytes are flushed to memory.
310 * This prevents problems if the driver uses this as
311 * both an input and an output buffer:
312 *
313 * 1. Zeroes written to buffer (here) and sit in the
314 * cache
315 * 2. Driver issues a read command to DMA
316 * 3. CPU runs out of cache space and evicts some cache
317 * data in the buffer, writing zeroes to RAM from
318 * the memset() above
319 * 4. DMA completes
320 * 5. Buffer now has some DMA data and some zeroes
321 * 6. Data being read is now incorrect
322 *
323 * To prevent this, ensure that the cache is clean
324 * within this range at the start. The driver can then
325 * use normal flush-after-write, invalidate-before-read
326 * procedures.
327 *
328 * TODO(sjg@chromium.org): Drop this microblaze
329 * exception.
330 */
331 #ifndef CONFIG_MICROBLAZE
332 flush_dcache_range((ulong)priv, (ulong)priv + size);
333 #endif
334 }
335 } else {
336 priv = calloc(1, size);
337 }
338
339 return priv;
340 }
341
342 /**
343 * device_alloc_priv() - Allocate priv/plat data required by the device
344 *
345 * @dev: Device to process
346 * @return 0 if OK, -ENOMEM if out of memory
347 */
device_alloc_priv(struct udevice * dev)348 static int device_alloc_priv(struct udevice *dev)
349 {
350 const struct driver *drv;
351 void *ptr;
352 int size;
353
354 drv = dev->driver;
355 assert(drv);
356
357 /* Allocate private data if requested and not reentered */
358 if (drv->priv_auto && !dev_get_priv(dev)) {
359 ptr = alloc_priv(drv->priv_auto, drv->flags);
360 if (!ptr)
361 return -ENOMEM;
362 dev_set_priv(dev, ptr);
363 }
364
365 /* Allocate private data if requested and not reentered */
366 size = dev->uclass->uc_drv->per_device_auto;
367 if (size && !dev_get_uclass_priv(dev)) {
368 ptr = alloc_priv(size, dev->uclass->uc_drv->flags);
369 if (!ptr)
370 return -ENOMEM;
371 dev_set_uclass_priv(dev, ptr);
372 }
373
374 /* Allocate parent data for this child */
375 if (dev->parent) {
376 size = dev->parent->driver->per_child_auto;
377 if (!size)
378 size = dev->parent->uclass->uc_drv->per_child_auto;
379 if (size && !dev_get_parent_priv(dev)) {
380 ptr = alloc_priv(size, drv->flags);
381 if (!ptr)
382 return -ENOMEM;
383 dev_set_parent_priv(dev, ptr);
384 }
385 }
386
387 return 0;
388 }
389
device_of_to_plat(struct udevice * dev)390 int device_of_to_plat(struct udevice *dev)
391 {
392 const struct driver *drv;
393 int ret;
394
395 if (!dev)
396 return -EINVAL;
397
398 if (dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID)
399 return 0;
400
401 /*
402 * This is not needed if binding is disabled, since data is allocated
403 * at build time.
404 */
405 if (!CONFIG_IS_ENABLED(OF_PLATDATA_NO_BIND)) {
406 /* Ensure all parents have ofdata */
407 if (dev->parent) {
408 ret = device_of_to_plat(dev->parent);
409 if (ret)
410 goto fail;
411
412 /*
413 * The device might have already been probed during
414 * the call to device_probe() on its parent device
415 * (e.g. PCI bridge devices). Test the flags again
416 * so that we don't mess up the device.
417 */
418 if (dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID)
419 return 0;
420 }
421
422 ret = device_alloc_priv(dev);
423 if (ret)
424 goto fail;
425 }
426 drv = dev->driver;
427 assert(drv);
428
429 if (drv->of_to_plat &&
430 (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_ofnode(dev))) {
431 ret = drv->of_to_plat(dev);
432 if (ret)
433 goto fail;
434 }
435
436 dev_or_flags(dev, DM_FLAG_PLATDATA_VALID);
437
438 return 0;
439 fail:
440 device_free(dev);
441
442 return ret;
443 }
444
445 /**
446 * device_get_dma_constraints() - Populate device's DMA constraints
447 *
448 * Gets a device's DMA constraints from firmware. This information is later
449 * used by drivers to translate physcal addresses to the device's bus address
450 * space. For now only device-tree is supported.
451 *
452 * @dev: Pointer to target device
453 * Return: 0 if OK or if no DMA constraints were found, error otherwise
454 */
device_get_dma_constraints(struct udevice * dev)455 static int device_get_dma_constraints(struct udevice *dev)
456 {
457 struct udevice *parent = dev->parent;
458 phys_addr_t cpu = 0;
459 dma_addr_t bus = 0;
460 u64 size = 0;
461 int ret;
462
463 if (!CONFIG_IS_ENABLED(DM_DMA) || !parent || !dev_has_ofnode(parent))
464 return 0;
465
466 /*
467 * We start parsing for dma-ranges from the device's bus node. This is
468 * specially important on nested buses.
469 */
470 ret = dev_get_dma_range(parent, &cpu, &bus, &size);
471 /* Don't return an error if no 'dma-ranges' were found */
472 if (ret && ret != -ENOENT) {
473 dm_warn("%s: failed to get DMA range, %d\n", dev->name, ret);
474 return ret;
475 }
476
477 dev_set_dma_offset(dev, cpu - bus);
478
479 return 0;
480 }
481
device_probe(struct udevice * dev)482 int device_probe(struct udevice *dev)
483 {
484 const struct driver *drv;
485 int ret;
486
487 if (!dev)
488 return -EINVAL;
489
490 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
491 return 0;
492
493 drv = dev->driver;
494 assert(drv);
495
496 ret = device_of_to_plat(dev);
497 if (ret)
498 goto fail;
499
500 /* Ensure all parents are probed */
501 if (dev->parent) {
502 ret = device_probe(dev->parent);
503 if (ret)
504 goto fail;
505
506 /*
507 * The device might have already been probed during
508 * the call to device_probe() on its parent device
509 * (e.g. PCI bridge devices). Test the flags again
510 * so that we don't mess up the device.
511 */
512 if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
513 return 0;
514 }
515
516 dev_or_flags(dev, DM_FLAG_ACTIVATED);
517
518 /*
519 * Process pinctrl for everything except the root device, and
520 * continue regardless of the result of pinctrl. Don't process pinctrl
521 * settings for pinctrl devices since the device may not yet be
522 * probed.
523 *
524 * This call can produce some non-intuitive results. For example, on an
525 * x86 device where dev is the main PCI bus, the pinctrl device may be
526 * child or grandchild of that bus, meaning that the child will be
527 * probed here. If the child happens to be the P2SB and the pinctrl
528 * device is a child of that, then both the pinctrl and P2SB will be
529 * probed by this call. This works because the DM_FLAG_ACTIVATED flag
530 * is set just above. However, the PCI bus' probe() method and
531 * associated uclass methods have not yet been called.
532 */
533 if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
534 pinctrl_select_state(dev, "default");
535
536 if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
537 (device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) &&
538 !(drv->flags & DM_FLAG_DEFAULT_PD_CTRL_OFF)) {
539 ret = dev_power_domain_on(dev);
540 if (ret)
541 goto fail;
542 }
543
544 ret = device_get_dma_constraints(dev);
545 if (ret)
546 goto fail;
547
548 ret = uclass_pre_probe_device(dev);
549 if (ret)
550 goto fail;
551
552 if (dev->parent && dev->parent->driver->child_pre_probe) {
553 ret = dev->parent->driver->child_pre_probe(dev);
554 if (ret)
555 goto fail;
556 }
557
558 /* Only handle devices that have a valid ofnode */
559 if (dev_has_ofnode(dev)) {
560 /*
561 * Process 'assigned-{clocks/clock-parents/clock-rates}'
562 * properties
563 */
564 ret = clk_set_defaults(dev, 0);
565 if (ret)
566 goto fail;
567 }
568
569 if (drv->probe) {
570 ret = drv->probe(dev);
571 if (ret)
572 goto fail;
573 }
574
575 ret = uclass_post_probe_device(dev);
576 if (ret)
577 goto fail_uclass;
578
579 if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
580 pinctrl_select_state(dev, "default");
581
582 return 0;
583 fail_uclass:
584 if (device_remove(dev, DM_REMOVE_NORMAL)) {
585 dm_warn("%s: Device '%s' failed to remove on error path\n",
586 __func__, dev->name);
587 }
588 fail:
589 dev_bic_flags(dev, DM_FLAG_ACTIVATED);
590
591 device_free(dev);
592
593 return ret;
594 }
595
dev_get_plat(const struct udevice * dev)596 void *dev_get_plat(const struct udevice *dev)
597 {
598 if (!dev) {
599 dm_warn("%s: null device\n", __func__);
600 return NULL;
601 }
602
603 return dm_priv_to_rw(dev->plat_);
604 }
605
dev_get_parent_plat(const struct udevice * dev)606 void *dev_get_parent_plat(const struct udevice *dev)
607 {
608 if (!dev) {
609 dm_warn("%s: null device\n", __func__);
610 return NULL;
611 }
612
613 return dm_priv_to_rw(dev->parent_plat_);
614 }
615
dev_get_uclass_plat(const struct udevice * dev)616 void *dev_get_uclass_plat(const struct udevice *dev)
617 {
618 if (!dev) {
619 dm_warn("%s: null device\n", __func__);
620 return NULL;
621 }
622
623 return dm_priv_to_rw(dev->uclass_plat_);
624 }
625
dev_get_priv(const struct udevice * dev)626 void *dev_get_priv(const struct udevice *dev)
627 {
628 if (!dev) {
629 dm_warn("%s: null device\n", __func__);
630 return NULL;
631 }
632
633 return dm_priv_to_rw(dev->priv_);
634 }
635
dev_get_uclass_priv(const struct udevice * dev)636 void *dev_get_uclass_priv(const struct udevice *dev)
637 {
638 if (!dev) {
639 dm_warn("%s: null device\n", __func__);
640 return NULL;
641 }
642
643 return dm_priv_to_rw(dev->uclass_priv_);
644 }
645
dev_get_parent_priv(const struct udevice * dev)646 void *dev_get_parent_priv(const struct udevice *dev)
647 {
648 if (!dev) {
649 dm_warn("%s: null device\n", __func__);
650 return NULL;
651 }
652
653 return dm_priv_to_rw(dev->parent_priv_);
654 }
655
device_get_device_tail(struct udevice * dev,int ret,struct udevice ** devp)656 static int device_get_device_tail(struct udevice *dev, int ret,
657 struct udevice **devp)
658 {
659 if (ret)
660 return ret;
661
662 ret = device_probe(dev);
663 if (ret)
664 return ret;
665
666 *devp = dev;
667
668 return 0;
669 }
670
671 #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
672 /**
673 * device_find_by_ofnode() - Return device associated with given ofnode
674 *
675 * The returned device is *not* activated.
676 *
677 * @node: The ofnode for which a associated device should be looked up
678 * @devp: Pointer to structure to hold the found device
679 * Return: 0 if OK, -ve on error
680 */
device_find_by_ofnode(ofnode node,struct udevice ** devp)681 static int device_find_by_ofnode(ofnode node, struct udevice **devp)
682 {
683 struct uclass *uc;
684 struct udevice *dev;
685 int ret;
686
687 list_for_each_entry(uc, gd->uclass_root, sibling_node) {
688 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node,
689 &dev);
690 if (!ret || dev) {
691 *devp = dev;
692 return 0;
693 }
694 }
695
696 return -ENODEV;
697 }
698 #endif
699
device_get_child(const struct udevice * parent,int index,struct udevice ** devp)700 int device_get_child(const struct udevice *parent, int index,
701 struct udevice **devp)
702 {
703 struct udevice *dev;
704
705 list_for_each_entry(dev, &parent->child_head, sibling_node) {
706 if (!index--)
707 return device_get_device_tail(dev, 0, devp);
708 }
709
710 return -ENODEV;
711 }
712
device_get_child_count(const struct udevice * parent)713 int device_get_child_count(const struct udevice *parent)
714 {
715 struct udevice *dev;
716 int count = 0;
717
718 list_for_each_entry(dev, &parent->child_head, sibling_node)
719 count++;
720
721 return count;
722 }
723
device_find_child_by_seq(const struct udevice * parent,int seq,struct udevice ** devp)724 int device_find_child_by_seq(const struct udevice *parent, int seq,
725 struct udevice **devp)
726 {
727 struct udevice *dev;
728
729 *devp = NULL;
730
731 list_for_each_entry(dev, &parent->child_head, sibling_node) {
732 if (dev->seq_ == seq) {
733 *devp = dev;
734 return 0;
735 }
736 }
737
738 return -ENODEV;
739 }
740
device_get_child_by_seq(const struct udevice * parent,int seq,struct udevice ** devp)741 int device_get_child_by_seq(const struct udevice *parent, int seq,
742 struct udevice **devp)
743 {
744 struct udevice *dev;
745 int ret;
746
747 *devp = NULL;
748 ret = device_find_child_by_seq(parent, seq, &dev);
749
750 return device_get_device_tail(dev, ret, devp);
751 }
752
device_find_child_by_of_offset(const struct udevice * parent,int of_offset,struct udevice ** devp)753 int device_find_child_by_of_offset(const struct udevice *parent, int of_offset,
754 struct udevice **devp)
755 {
756 struct udevice *dev;
757
758 *devp = NULL;
759
760 list_for_each_entry(dev, &parent->child_head, sibling_node) {
761 if (dev_of_offset(dev) == of_offset) {
762 *devp = dev;
763 return 0;
764 }
765 }
766
767 return -ENODEV;
768 }
769
device_get_child_by_of_offset(const struct udevice * parent,int node,struct udevice ** devp)770 int device_get_child_by_of_offset(const struct udevice *parent, int node,
771 struct udevice **devp)
772 {
773 struct udevice *dev;
774 int ret;
775
776 *devp = NULL;
777 ret = device_find_child_by_of_offset(parent, node, &dev);
778 return device_get_device_tail(dev, ret, devp);
779 }
780
_device_find_global_by_ofnode(struct udevice * parent,ofnode ofnode)781 static struct udevice *_device_find_global_by_ofnode(struct udevice *parent,
782 ofnode ofnode)
783 {
784 struct udevice *dev, *found;
785
786 if (ofnode_equal(dev_ofnode(parent), ofnode))
787 return parent;
788
789 list_for_each_entry(dev, &parent->child_head, sibling_node) {
790 found = _device_find_global_by_ofnode(dev, ofnode);
791 if (found)
792 return found;
793 }
794
795 return NULL;
796 }
797
device_find_global_by_ofnode(ofnode ofnode,struct udevice ** devp)798 int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp)
799 {
800 *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode);
801
802 return *devp ? 0 : -ENOENT;
803 }
804
device_get_global_by_ofnode(ofnode ofnode,struct udevice ** devp)805 int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
806 {
807 struct udevice *dev;
808
809 dev = _device_find_global_by_ofnode(gd->dm_root, ofnode);
810 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
811 }
812
813 #if CONFIG_IS_ENABLED(OF_PLATDATA)
device_get_by_ofplat_idx(uint idx,struct udevice ** devp)814 int device_get_by_ofplat_idx(uint idx, struct udevice **devp)
815 {
816 struct udevice *dev;
817
818 if (CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
819 struct udevice *base = ll_entry_start(struct udevice, udevice);
820
821 dev = base + idx;
822 } else {
823 struct driver_rt *drt = gd_dm_driver_rt() + idx;
824
825 dev = drt->dev;
826 }
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
1140 #if CONFIG_IS_ENABLED(OF_PLATDATA_RT)
dev_get_rt(const struct udevice * dev)1141 static struct udevice_rt *dev_get_rt(const struct udevice *dev)
1142 {
1143 struct udevice *base = ll_entry_start(struct udevice, udevice);
1144 int idx = dev - base;
1145
1146 struct udevice_rt *urt = gd_dm_udevice_rt() + idx;
1147
1148 return urt;
1149 }
1150
dev_get_flags(const struct udevice * dev)1151 u32 dev_get_flags(const struct udevice *dev)
1152 {
1153 const struct udevice_rt *urt = dev_get_rt(dev);
1154
1155 return urt->flags_;
1156 }
1157
dev_or_flags(const struct udevice * dev,u32 or)1158 void dev_or_flags(const struct udevice *dev, u32 or)
1159 {
1160 struct udevice_rt *urt = dev_get_rt(dev);
1161
1162 urt->flags_ |= or;
1163 }
1164
dev_bic_flags(const struct udevice * dev,u32 bic)1165 void dev_bic_flags(const struct udevice *dev, u32 bic)
1166 {
1167 struct udevice_rt *urt = dev_get_rt(dev);
1168
1169 urt->flags_ &= ~bic;
1170 }
1171 #endif /* OF_PLATDATA_RT */
1172