]> Git Repo - J-u-boot.git/blame - drivers/core/device.c
dm: Drop uclass_resolve_seq()
[J-u-boot.git] / drivers / core / device.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
6494d708
SG
2/*
3 * Device manager
4 *
5 * Copyright (c) 2013 Google, Inc
6 *
7 * (C) Copyright 2012
8 * Pavel Herrmann <[email protected]>
6494d708
SG
9 */
10
11#include <common.h>
1eb69ae4 12#include <cpu_func.h>
f7ae49fc 13#include <log.h>
7c616862 14#include <asm/io.h>
f4fcba5c 15#include <clk.h>
5a66a8ff 16#include <fdtdec.h>
ef5cd330 17#include <fdt_support.h>
6494d708 18#include <malloc.h>
90526e9f 19#include <asm/cache.h>
6494d708
SG
20#include <dm/device.h>
21#include <dm/device-internal.h>
22#include <dm/lists.h>
29d11b88 23#include <dm/of_access.h>
d90a5a30 24#include <dm/pinctrl.h>
6494d708 25#include <dm/platdata.h>
396e343b 26#include <dm/read.h>
6494d708
SG
27#include <dm/uclass.h>
28#include <dm/uclass-internal.h>
29#include <dm/util.h>
30#include <linux/err.h>
31#include <linux/list.h>
3ad30778 32#include <power-domain.h>
6494d708 33
5a66a8ff
SG
34DECLARE_GLOBAL_DATA_PTR;
35
daac3bfe 36static int device_bind_common(struct udevice *parent, const struct driver *drv,
caa4daa2 37 const char *name, void *plat,
7a61b0b5 38 ulong driver_data, ofnode node,
4f50086a 39 uint of_plat_size, struct udevice **devp)
6494d708 40{
54c5d08a 41 struct udevice *dev;
6494d708 42 struct uclass *uc;
5eaed880 43 int size, ret = 0;
cd53e5bf 44 bool auto_seq = true;
6494d708 45
e6cabe4a
MY
46 if (devp)
47 *devp = NULL;
6494d708
SG
48 if (!name)
49 return -EINVAL;
50
51 ret = uclass_get(drv->id, &uc);
3346c876
SG
52 if (ret) {
53 debug("Missing uclass for driver %s\n", drv->name);
6494d708 54 return ret;
3346c876 55 }
6494d708 56
54c5d08a 57 dev = calloc(1, sizeof(struct udevice));
6494d708
SG
58 if (!dev)
59 return -ENOMEM;
60
61 INIT_LIST_HEAD(&dev->sibling_node);
62 INIT_LIST_HEAD(&dev->child_head);
63 INIT_LIST_HEAD(&dev->uclass_node);
e2282d70 64#ifdef CONFIG_DEVRES
608f26c5 65 INIT_LIST_HEAD(&dev->devres_head);
e2282d70 66#endif
caa4daa2 67 dev->plat = plat;
daac3bfe 68 dev->driver_data = driver_data;
6494d708 69 dev->name = name;
7a61b0b5 70 dev->node = node;
6494d708
SG
71 dev->parent = parent;
72 dev->driver = drv;
73 dev->uclass = uc;
5a66a8ff 74
5a66a8ff 75 dev->seq = -1;
9cc36a2b 76 dev->req_seq = -1;
cd53e5bf 77 dev->sqq = -1;
3542ff29
JJH
78 if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
79 (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS)) {
36fa61dc 80 /*
770eb30e
SR
81 * Some devices, such as a SPI bus, I2C bus and serial ports
82 * are numbered using aliases.
83 *
84 * This is just a 'requested' sequence, and will be
85 * resolved (and ->seq updated) when the device is probed.
86 */
6d65ac31
SG
87 if (CONFIG_IS_ENABLED(OF_CONTROL) &&
88 !CONFIG_IS_ENABLED(OF_PLATDATA)) {
cd53e5bf
SG
89 if (uc->uc_drv->name && ofnode_valid(node)) {
90 dev_read_alias_seq(dev, &dev->sqq);
396e343b 91 dev_read_alias_seq(dev, &dev->req_seq);
cd53e5bf
SG
92 auto_seq = false;
93 }
94 if (CONFIG_IS_ENABLED(OF_PRIOR_STAGE)) {
95 if (dev->req_seq == -1) {
96 auto_seq = true;
97 dev->req_seq =
98 uclass_find_next_free_req_seq(
99 uc);
100 }
101 }
3542ff29 102 } else {
d03adb4a 103 dev->req_seq = uclass_find_next_free_req_seq(uc);
9cc36a2b 104 }
5a66a8ff 105 }
15a1196b 106 if (auto_seq && !(uc->uc_drv->flags & DM_UC_FLAG_NO_AUTO_SEQ))
cd53e5bf 107 dev->sqq = uclass_find_next_free_req_seq(uc);
36fa61dc 108
caa4daa2
SG
109 if (drv->plat_auto) {
110 bool alloc = !plat;
9fa28190
SG
111
112 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
4f50086a 113 if (of_plat_size) {
9fa28190 114 dev->flags |= DM_FLAG_OF_PLATDATA;
4f50086a 115 if (of_plat_size < drv->plat_auto)
9fa28190
SG
116 alloc = true;
117 }
118 }
119 if (alloc) {
120 dev->flags |= DM_FLAG_ALLOC_PDATA;
caa4daa2
SG
121 dev->plat = calloc(1, drv->plat_auto);
122 if (!dev->plat) {
9fa28190
SG
123 ret = -ENOMEM;
124 goto fail_alloc1;
125 }
caa4daa2 126 if (CONFIG_IS_ENABLED(OF_PLATDATA) && plat) {
4f50086a 127 memcpy(dev->plat, plat, of_plat_size);
9fa28190 128 }
f8a85449
SG
129 }
130 }
cdc133bd 131
caa4daa2 132 size = uc->uc_drv->per_device_plat_auto;
5eaed880
PM
133 if (size) {
134 dev->flags |= DM_FLAG_ALLOC_UCLASS_PDATA;
caa4daa2
SG
135 dev->uclass_plat = calloc(1, size);
136 if (!dev->uclass_plat) {
5eaed880
PM
137 ret = -ENOMEM;
138 goto fail_alloc2;
139 }
140 }
141
142 if (parent) {
caa4daa2 143 size = parent->driver->per_child_plat_auto;
ba8da9dc 144 if (!size) {
caa4daa2 145 size = parent->uclass->uc_drv->per_child_plat_auto;
ba8da9dc 146 }
cdc133bd
SG
147 if (size) {
148 dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA;
caa4daa2
SG
149 dev->parent_plat = calloc(1, size);
150 if (!dev->parent_plat) {
cdc133bd 151 ret = -ENOMEM;
5eaed880 152 goto fail_alloc3;
cdc133bd
SG
153 }
154 }
f93a07dd 155 /* put dev into parent's successor list */
6494d708 156 list_add_tail(&dev->sibling_node, &parent->child_head);
f93a07dd 157 }
6494d708
SG
158
159 ret = uclass_bind_device(dev);
160 if (ret)
72ebfe86 161 goto fail_uclass_bind;
6494d708
SG
162
163 /* if we fail to bind we remove device from successors and free it */
164 if (drv->bind) {
165 ret = drv->bind(dev);
72ebfe86 166 if (ret)
6494d708 167 goto fail_bind;
6494d708 168 }
0118ce79
SG
169 if (parent && parent->driver->child_post_bind) {
170 ret = parent->driver->child_post_bind(dev);
171 if (ret)
172 goto fail_child_post_bind;
173 }
20af3c0a
SG
174 if (uc->uc_drv->post_bind) {
175 ret = uc->uc_drv->post_bind(dev);
176 if (ret)
177 goto fail_uclass_post_bind;
178 }
0118ce79 179
6494d708 180 if (parent)
ceb91909 181 pr_debug("Bound device %s to %s\n", dev->name, parent->name);
e6cabe4a
MY
182 if (devp)
183 *devp = dev;
6494d708 184
aed1a4dd
MY
185 dev->flags |= DM_FLAG_BOUND;
186
6494d708
SG
187 return 0;
188
20af3c0a
SG
189fail_uclass_post_bind:
190 /* There is no child unbind() method, so no clean-up required */
0118ce79 191fail_child_post_bind:
0a5804b5 192 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
5a87c417
SG
193 if (drv->unbind && drv->unbind(dev)) {
194 dm_warn("unbind() method failed on dev '%s' on error path\n",
195 dev->name);
196 }
0118ce79
SG
197 }
198
6494d708 199fail_bind:
0a5804b5 200 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
5a87c417
SG
201 if (uclass_unbind_device(dev)) {
202 dm_warn("Failed to unbind dev '%s' on error path\n",
203 dev->name);
204 }
72ebfe86
SG
205 }
206fail_uclass_bind:
0a5804b5 207 if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
5a87c417
SG
208 list_del(&dev->sibling_node);
209 if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
caa4daa2
SG
210 free(dev->parent_plat);
211 dev->parent_plat = NULL;
5a87c417 212 }
cdc133bd 213 }
5eaed880
PM
214fail_alloc3:
215 if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) {
caa4daa2
SG
216 free(dev->uclass_plat);
217 dev->uclass_plat = NULL;
5eaed880 218 }
cdc133bd 219fail_alloc2:
f8a85449 220 if (dev->flags & DM_FLAG_ALLOC_PDATA) {
caa4daa2
SG
221 free(dev->plat);
222 dev->plat = NULL;
f8a85449
SG
223 }
224fail_alloc1:
608f26c5
MY
225 devres_release_all(dev);
226
6494d708 227 free(dev);
72ebfe86 228
6494d708
SG
229 return ret;
230}
231
daac3bfe
SW
232int device_bind_with_driver_data(struct udevice *parent,
233 const struct driver *drv, const char *name,
396e343b 234 ulong driver_data, ofnode node,
daac3bfe
SW
235 struct udevice **devp)
236{
396e343b
SG
237 return device_bind_common(parent, drv, name, NULL, driver_data, node,
238 0, devp);
daac3bfe
SW
239}
240
734206dd 241int device_bind(struct udevice *parent, const struct driver *drv,
caa4daa2 242 const char *name, void *plat, ofnode node,
734206dd 243 struct udevice **devp)
d677b00c 244{
caa4daa2 245 return device_bind_common(parent, drv, name, plat, 0, node, 0,
d677b00c
SG
246 devp);
247}
248
00606d7e 249int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
a294ead8 250 const struct driver_info *info, struct udevice **devp)
6494d708
SG
251{
252 struct driver *drv;
4f50086a 253 uint plat_size = 0;
fed0f891 254 int ret;
6494d708
SG
255
256 drv = lists_driver_lookup_name(info->name);
257 if (!drv)
258 return -ENOENT;
00606d7e
SG
259 if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
260 return -EPERM;
6494d708 261
9fa28190 262#if CONFIG_IS_ENABLED(OF_PLATDATA)
4f50086a 263 plat_size = info->plat_size;
9fa28190 264#endif
caa4daa2 265 ret = device_bind_common(parent, drv, info->name, (void *)info->plat, 0,
4f50086a 266 ofnode_null(), plat_size, devp);
fed0f891
WL
267 if (ret)
268 return ret;
fed0f891
WL
269
270 return ret;
6494d708
SG
271}
272
cfecbaf4
CB
273int 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
2c03c463
SG
295static void *alloc_priv(int size, uint flags)
296{
297 void *priv;
298
299 if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
5924da1d 300 size = ROUND(size, ARCH_DMA_MINALIGN);
2c03c463 301 priv = memalign(ARCH_DMA_MINALIGN, size);
5a8a8045 302 if (priv) {
2c03c463 303 memset(priv, '\0', size);
5a8a8045
SG
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([email protected]): Drop this microblaze
326 * exception.
327 */
328#ifndef CONFIG_MICROBLAZE
329 flush_dcache_range((ulong)priv, (ulong)priv + size);
330#endif
331 }
2c03c463
SG
332 } else {
333 priv = calloc(1, size);
334 }
335
336 return priv;
337}
338
d1998a9f 339int device_of_to_plat(struct udevice *dev)
6494d708 340{
3479253d 341 const struct driver *drv;
6494d708
SG
342 int size = 0;
343 int ret;
344
345 if (!dev)
346 return -EINVAL;
347
153851dd 348 if (dev->flags & DM_FLAG_PLATDATA_VALID)
6494d708
SG
349 return 0;
350
b0dcc871
SG
351 /* Ensure all parents have ofdata */
352 if (dev->parent) {
d1998a9f 353 ret = device_of_to_plat(dev->parent);
b0dcc871
SG
354 if (ret)
355 goto fail;
356
357 /*
358 * The device might have already been probed during
359 * the call to device_probe() on its parent device
360 * (e.g. PCI bridge devices). Test the flags again
361 * so that we don't mess up the device.
362 */
363 if (dev->flags & DM_FLAG_PLATDATA_VALID)
364 return 0;
365 }
366
6494d708
SG
367 drv = dev->driver;
368 assert(drv);
369
cdeb2ba9 370 /* Allocate private data if requested and not reentered */
41575d8e
SG
371 if (drv->priv_auto && !dev->priv) {
372 dev->priv = alloc_priv(drv->priv_auto, drv->flags);
6494d708
SG
373 if (!dev->priv) {
374 ret = -ENOMEM;
375 goto fail;
376 }
377 }
cdeb2ba9 378 /* Allocate private data if requested and not reentered */
41575d8e 379 size = dev->uclass->uc_drv->per_device_auto;
cdeb2ba9 380 if (size && !dev->uclass_priv) {
c7a3accc
SG
381 dev->uclass_priv = alloc_priv(size,
382 dev->uclass->uc_drv->flags);
6494d708
SG
383 if (!dev->uclass_priv) {
384 ret = -ENOMEM;
385 goto fail;
386 }
387 }
388
82de42fa 389 /* Allocate parent data for this child */
6494d708 390 if (dev->parent) {
41575d8e 391 size = dev->parent->driver->per_child_auto;
dac8db2c 392 if (!size) {
41575d8e 393 size = dev->parent->uclass->uc_drv->per_child_auto;
dac8db2c 394 }
cdeb2ba9 395 if (size && !dev->parent_priv) {
2c03c463 396 dev->parent_priv = alloc_priv(size, drv->flags);
e59f458d
SG
397 if (!dev->parent_priv) {
398 ret = -ENOMEM;
399 goto fail;
400 }
401 }
82de42fa
SG
402 }
403
d1998a9f 404 if (drv->of_to_plat &&
82de42fa 405 (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_of_node(dev))) {
d1998a9f 406 ret = drv->of_to_plat(dev);
82de42fa
SG
407 if (ret)
408 goto fail;
409 }
e59f458d 410
153851dd
SG
411 dev->flags |= DM_FLAG_PLATDATA_VALID;
412
bcd90cb6
SG
413 return 0;
414fail:
415 device_free(dev);
416
417 return ret;
418}
419
420int device_probe(struct udevice *dev)
421{
422 const struct driver *drv;
423 int ret;
bcd90cb6
SG
424
425 if (!dev)
426 return -EINVAL;
427
428 if (dev->flags & DM_FLAG_ACTIVATED)
429 return 0;
430
431 drv = dev->driver;
432 assert(drv);
433
d1998a9f 434 ret = device_of_to_plat(dev);
bcd90cb6
SG
435 if (ret)
436 goto fail;
437
82de42fa
SG
438 /* Ensure all parents are probed */
439 if (dev->parent) {
6494d708
SG
440 ret = device_probe(dev->parent);
441 if (ret)
442 goto fail;
cdeb2ba9
BM
443
444 /*
445 * The device might have already been probed during
446 * the call to device_probe() on its parent device
447 * (e.g. PCI bridge devices). Test the flags again
448 * so that we don't mess up the device.
449 */
450 if (dev->flags & DM_FLAG_ACTIVATED)
451 return 0;
6494d708
SG
452 }
453
206d4d2b
SG
454 dev->flags |= DM_FLAG_ACTIVATED;
455
84d26e29
SG
456 /*
457 * Process pinctrl for everything except the root device, and
0379597e
SG
458 * continue regardless of the result of pinctrl. Don't process pinctrl
459 * settings for pinctrl devices since the device may not yet be
460 * probed.
84d26e29 461 */
0379597e 462 if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
84d26e29 463 pinctrl_select_state(dev, "default");
d90a5a30 464
44e02e39 465 if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
af94ad41
LV
466 (device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) &&
467 !(drv->flags & DM_FLAG_DEFAULT_PD_CTRL_OFF)) {
f0cc4eae
PF
468 ret = dev_power_domain_on(dev);
469 if (ret)
470 goto fail;
3ad30778
PF
471 }
472
02c07b37 473 ret = uclass_pre_probe_device(dev);
83c7e434
SG
474 if (ret)
475 goto fail;
476
a327dee0
SG
477 if (dev->parent && dev->parent->driver->child_pre_probe) {
478 ret = dev->parent->driver->child_pre_probe(dev);
479 if (ret)
480 goto fail;
481 }
482
a1f99e46
BM
483 /* Only handle devices that have a valid ofnode */
484 if (dev_of_valid(dev)) {
485 /*
486 * Process 'assigned-{clocks/clock-parents/clock-rates}'
487 * properties
488 */
fd1ba296 489 ret = clk_set_defaults(dev, 0);
a1f99e46
BM
490 if (ret)
491 goto fail;
492 }
f4fcba5c 493
6494d708
SG
494 if (drv->probe) {
495 ret = drv->probe(dev);
a41e6daf 496 if (ret)
6494d708
SG
497 goto fail;
498 }
499
6494d708 500 ret = uclass_post_probe_device(dev);
206d4d2b 501 if (ret)
6494d708 502 goto fail_uclass;
6494d708 503
c3ab9853
PF
504 if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
505 pinctrl_select_state(dev, "default");
506
6494d708
SG
507 return 0;
508fail_uclass:
706865af 509 if (device_remove(dev, DM_REMOVE_NORMAL)) {
6494d708
SG
510 dm_warn("%s: Device '%s' failed to remove on error path\n",
511 __func__, dev->name);
512 }
513fail:
206d4d2b
SG
514 dev->flags &= ~DM_FLAG_ACTIVATED;
515
5a66a8ff 516 dev->seq = -1;
6494d708
SG
517 device_free(dev);
518
519 return ret;
520}
521
c69cda25 522void *dev_get_plat(const struct udevice *dev)
6494d708
SG
523{
524 if (!dev) {
964d153c 525 dm_warn("%s: null device\n", __func__);
6494d708
SG
526 return NULL;
527 }
528
caa4daa2 529 return dev->plat;
6494d708
SG
530}
531
caa4daa2 532void *dev_get_parent_plat(const struct udevice *dev)
cdc133bd
SG
533{
534 if (!dev) {
36d7cc17 535 dm_warn("%s: null device\n", __func__);
cdc133bd
SG
536 return NULL;
537 }
538
caa4daa2 539 return dev->parent_plat;
cdc133bd
SG
540}
541
caa4daa2 542void *dev_get_uclass_plat(const struct udevice *dev)
5eaed880
PM
543{
544 if (!dev) {
36d7cc17 545 dm_warn("%s: null device\n", __func__);
5eaed880
PM
546 return NULL;
547 }
548
caa4daa2 549 return dev->uclass_plat;
5eaed880
PM
550}
551
9f15cc14 552void *dev_get_priv(const struct udevice *dev)
6494d708
SG
553{
554 if (!dev) {
964d153c 555 dm_warn("%s: null device\n", __func__);
6494d708
SG
556 return NULL;
557 }
558
559 return dev->priv;
560}
997c87bb 561
9f15cc14 562void *dev_get_uclass_priv(const struct udevice *dev)
e564f054
SG
563{
564 if (!dev) {
565 dm_warn("%s: null device\n", __func__);
566 return NULL;
567 }
568
569 return dev->uclass_priv;
570}
571
9f15cc14 572void *dev_get_parent_priv(const struct udevice *dev)
e59f458d
SG
573{
574 if (!dev) {
964d153c 575 dm_warn("%s: null device\n", __func__);
e59f458d
SG
576 return NULL;
577 }
578
579 return dev->parent_priv;
580}
581
997c87bb
SG
582static int device_get_device_tail(struct udevice *dev, int ret,
583 struct udevice **devp)
584{
585 if (ret)
586 return ret;
587
588 ret = device_probe(dev);
589 if (ret)
590 return ret;
591
592 *devp = dev;
593
594 return 0;
595}
596
d7677bfc 597#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
e4c98a59
MS
598/**
599 * device_find_by_ofnode() - Return device associated with given ofnode
600 *
601 * The returned device is *not* activated.
602 *
603 * @node: The ofnode for which a associated device should be looked up
604 * @devp: Pointer to structure to hold the found device
605 * Return: 0 if OK, -ve on error
606 */
607static int device_find_by_ofnode(ofnode node, struct udevice **devp)
608{
609 struct uclass *uc;
610 struct udevice *dev;
611 int ret;
612
613 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
614 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node,
615 &dev);
616 if (!ret || dev) {
617 *devp = dev;
618 return 0;
619 }
620 }
621
622 return -ENODEV;
623}
d7677bfc 624#endif
e4c98a59 625
fc347fbd
SG
626int device_get_child(const struct udevice *parent, int index,
627 struct udevice **devp)
997c87bb
SG
628{
629 struct udevice *dev;
630
631 list_for_each_entry(dev, &parent->child_head, sibling_node) {
632 if (!index--)
633 return device_get_device_tail(dev, 0, devp);
634 }
635
636 return -ENODEV;
637}
638
fc347fbd 639int device_get_child_count(const struct udevice *parent)
240b9320
LV
640{
641 struct udevice *dev;
642 int count = 0;
643
644 list_for_each_entry(dev, &parent->child_head, sibling_node)
645 count++;
646
647 return count;
648}
649
fc347fbd 650int device_find_child_by_seq(const struct udevice *parent, int seq_or_req_seq,
997c87bb
SG
651 bool find_req_seq, struct udevice **devp)
652{
653 struct udevice *dev;
654
655 *devp = NULL;
997c87bb
SG
656
657 list_for_each_entry(dev, &parent->child_head, sibling_node) {
ba0e7dae 658 if (dev->sqq == seq_or_req_seq) {
997c87bb
SG
659 *devp = dev;
660 return 0;
661 }
662 }
663
664 return -ENODEV;
665}
666
fc347fbd 667int device_get_child_by_seq(const struct udevice *parent, int seq,
997c87bb
SG
668 struct udevice **devp)
669{
670 struct udevice *dev;
671 int ret;
672
673 *devp = NULL;
674 ret = device_find_child_by_seq(parent, seq, false, &dev);
675 if (ret == -ENODEV) {
676 /*
677 * We didn't find it in probed devices. See if there is one
678 * that will request this seq if probed.
679 */
680 ret = device_find_child_by_seq(parent, seq, true, &dev);
681 }
682 return device_get_device_tail(dev, ret, devp);
683}
684
fc347fbd 685int device_find_child_by_of_offset(const struct udevice *parent, int of_offset,
997c87bb
SG
686 struct udevice **devp)
687{
688 struct udevice *dev;
689
690 *devp = NULL;
691
692 list_for_each_entry(dev, &parent->child_head, sibling_node) {
e160f7d4 693 if (dev_of_offset(dev) == of_offset) {
997c87bb
SG
694 *devp = dev;
695 return 0;
696 }
697 }
698
699 return -ENODEV;
700}
701
fc347fbd 702int device_get_child_by_of_offset(const struct udevice *parent, int node,
997c87bb
SG
703 struct udevice **devp)
704{
705 struct udevice *dev;
706 int ret;
707
708 *devp = NULL;
132f9bfc 709 ret = device_find_child_by_of_offset(parent, node, &dev);
997c87bb
SG
710 return device_get_device_tail(dev, ret, devp);
711}
a8981d4f 712
7ec9181d
JJH
713static struct udevice *_device_find_global_by_ofnode(struct udevice *parent,
714 ofnode ofnode)
2693047a
SG
715{
716 struct udevice *dev, *found;
717
7ec9181d 718 if (ofnode_equal(dev_ofnode(parent), ofnode))
2693047a
SG
719 return parent;
720
721 list_for_each_entry(dev, &parent->child_head, sibling_node) {
7ec9181d 722 found = _device_find_global_by_ofnode(dev, ofnode);
2693047a
SG
723 if (found)
724 return found;
725 }
726
727 return NULL;
728}
729
7ec9181d
JJH
730int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp)
731{
732 *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode);
733
734 return *devp ? 0 : -ENOENT;
735}
736
737int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
2693047a
SG
738{
739 struct udevice *dev;
740
7ec9181d 741 dev = _device_find_global_by_ofnode(gd->dm_root, ofnode);
2693047a
SG
742 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
743}
744
fed0f891
WL
745#if CONFIG_IS_ENABLED(OF_PLATDATA)
746int device_get_by_driver_info(const struct driver_info *info,
747 struct udevice **devp)
748{
a294ead8
SG
749 struct driver_info *info_base =
750 ll_entry_start(struct driver_info, driver_info);
751 int idx = info - info_base;
752 struct driver_rt *drt = gd_dm_driver_rt() + idx;
fed0f891
WL
753 struct udevice *dev;
754
a294ead8 755 dev = drt->dev;
36af37b9 756 *devp = NULL;
fed0f891
WL
757
758 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
759}
8a38abfc
SG
760
761int device_get_by_driver_info_idx(uint idx, struct udevice **devp)
762{
763 struct driver_rt *drt = gd_dm_driver_rt() + idx;
764 struct udevice *dev;
765
766 dev = drt->dev;
767 *devp = NULL;
768
769 return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
770}
fed0f891
WL
771#endif
772
fc347fbd 773int device_find_first_child(const struct udevice *parent, struct udevice **devp)
a8981d4f
SG
774{
775 if (list_empty(&parent->child_head)) {
776 *devp = NULL;
777 } else {
778 *devp = list_first_entry(&parent->child_head, struct udevice,
779 sibling_node);
780 }
781
782 return 0;
783}
784
785int device_find_next_child(struct udevice **devp)
786{
787 struct udevice *dev = *devp;
788 struct udevice *parent = dev->parent;
789
790 if (list_is_last(&dev->sibling_node, &parent->child_head)) {
791 *devp = NULL;
792 } else {
793 *devp = list_entry(dev->sibling_node.next, struct udevice,
794 sibling_node);
795 }
796
797 return 0;
798}
2ef249b4 799
fc347fbd 800int device_find_first_inactive_child(const struct udevice *parent,
cdb6aa0a
SG
801 enum uclass_id uclass_id,
802 struct udevice **devp)
803{
804 struct udevice *dev;
805
806 *devp = NULL;
807 list_for_each_entry(dev, &parent->child_head, sibling_node) {
808 if (!device_active(dev) &&
809 device_get_uclass_id(dev) == uclass_id) {
810 *devp = dev;
811 return 0;
812 }
813 }
814
815 return -ENODEV;
816}
817
fc347fbd 818int device_find_first_child_by_uclass(const struct udevice *parent,
3abe1115
SG
819 enum uclass_id uclass_id,
820 struct udevice **devp)
821{
822 struct udevice *dev;
823
824 *devp = NULL;
825 list_for_each_entry(dev, &parent->child_head, sibling_node) {
826 if (device_get_uclass_id(dev) == uclass_id) {
827 *devp = dev;
828 return 0;
829 }
830 }
831
832 return -ENODEV;
833}
834
fc347fbd 835int device_find_child_by_name(const struct udevice *parent, const char *name,
3abe1115
SG
836 struct udevice **devp)
837{
838 struct udevice *dev;
839
840 *devp = NULL;
841
842 list_for_each_entry(dev, &parent->child_head, sibling_node) {
843 if (!strcmp(dev->name, name)) {
844 *devp = dev;
845 return 0;
846 }
847 }
848
849 return -ENODEV;
850}
851
903e83ee
SG
852int device_first_child_err(struct udevice *parent, struct udevice **devp)
853{
854 struct udevice *dev;
855
856 device_find_first_child(parent, &dev);
857 if (!dev)
858 return -ENODEV;
859
860 return device_get_device_tail(dev, 0, devp);
861}
862
863int device_next_child_err(struct udevice **devp)
864{
865 struct udevice *dev = *devp;
866
867 device_find_next_child(&dev);
868 if (!dev)
869 return -ENODEV;
870
871 return device_get_device_tail(dev, 0, devp);
872}
873
f262d4ca
SG
874int device_first_child_ofdata_err(struct udevice *parent, struct udevice **devp)
875{
876 struct udevice *dev;
877 int ret;
878
879 device_find_first_child(parent, &dev);
880 if (!dev)
881 return -ENODEV;
882
d1998a9f 883 ret = device_of_to_plat(dev);
f262d4ca
SG
884 if (ret)
885 return ret;
886
887 *devp = dev;
888
889 return 0;
890}
891
892int device_next_child_ofdata_err(struct udevice **devp)
893{
894 struct udevice *dev = *devp;
895 int ret;
896
897 device_find_next_child(&dev);
898 if (!dev)
899 return -ENODEV;
900
d1998a9f 901 ret = device_of_to_plat(dev);
f262d4ca
SG
902 if (ret)
903 return ret;
904
905 *devp = dev;
906
907 return 0;
908}
909
9f15cc14 910struct udevice *dev_get_parent(const struct udevice *child)
479728cb
SG
911{
912 return child->parent;
913}
914
9f15cc14 915ulong dev_get_driver_data(const struct udevice *dev)
2ef249b4 916{
39de8433 917 return dev->driver_data;
2ef249b4 918}
b3670531 919
9f15cc14 920const void *dev_get_driver_ops(const struct udevice *dev)
cc73d37b
PM
921{
922 if (!dev || !dev->driver->ops)
923 return NULL;
924
925 return dev->driver->ops;
926}
927
9f15cc14 928enum uclass_id device_get_uclass_id(const struct udevice *dev)
b3670531
SG
929{
930 return dev->uclass->uc_drv->id;
931}
c9cac3f8 932
9f15cc14 933const char *dev_get_uclass_name(const struct udevice *dev)
f9c370dc
PM
934{
935 if (!dev)
936 return NULL;
937
938 return dev->uclass->uc_drv->name;
939}
940
9f15cc14 941bool device_has_children(const struct udevice *dev)
c5785673
SG
942{
943 return !list_empty(&dev->child_head);
944}
945
fc347fbd 946bool device_has_active_children(const struct udevice *dev)
c5785673
SG
947{
948 struct udevice *child;
949
950 for (device_find_first_child(dev, &child);
951 child;
952 device_find_next_child(&child)) {
953 if (device_active(child))
954 return true;
955 }
956
957 return false;
958}
959
fc347fbd 960bool device_is_last_sibling(const struct udevice *dev)
c5785673
SG
961{
962 struct udevice *parent = dev->parent;
963
964 if (!parent)
965 return false;
966 return list_is_last(&dev->sibling_node, &parent->child_head);
967}
f5c67ea0 968
a2040fac
SG
969void device_set_name_alloced(struct udevice *dev)
970{
fd1c2d9b 971 dev->flags |= DM_FLAG_NAME_ALLOCED;
a2040fac
SG
972}
973
f5c67ea0
SG
974int device_set_name(struct udevice *dev, const char *name)
975{
976 name = strdup(name);
977 if (!name)
978 return -ENOMEM;
979 dev->name = name;
a2040fac 980 device_set_name_alloced(dev);
f5c67ea0
SG
981
982 return 0;
983}
73443b9e 984
d7677bfc 985#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
fc347fbd 986bool device_is_compatible(const struct udevice *dev, const char *compat)
73443b9e 987{
5ccc2c21 988 return ofnode_device_is_compatible(dev_ofnode(dev), compat);
73443b9e
M
989}
990
991bool of_machine_is_compatible(const char *compat)
992{
993 const void *fdt = gd->fdt_blob;
994
995 return !fdt_node_check_compatible(fdt, 0, compat);
996}
e4c98a59
MS
997
998int dev_disable_by_path(const char *path)
999{
1000 struct uclass *uc;
1001 ofnode node = ofnode_path(path);
1002 struct udevice *dev;
1003 int ret = 1;
1004
1005 if (!of_live_active())
1006 return -ENOSYS;
1007
1008 list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
1009 ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev);
1010 if (!ret)
1011 break;
1012 }
1013
1014 if (ret)
1015 return ret;
1016
1017 ret = device_remove(dev, DM_REMOVE_NORMAL);
1018 if (ret)
1019 return ret;
1020
1021 ret = device_unbind(dev);
1022 if (ret)
1023 return ret;
1024
1025 return ofnode_set_enabled(node, false);
1026}
1027
1028int dev_enable_by_path(const char *path)
1029{
1030 ofnode node = ofnode_path(path);
1031 ofnode pnode = ofnode_get_parent(node);
1032 struct udevice *parent;
1033 int ret = 1;
1034
1035 if (!of_live_active())
1036 return -ENOSYS;
1037
1038 ret = device_find_by_ofnode(pnode, &parent);
1039 if (ret)
1040 return ret;
1041
1042 ret = ofnode_set_enabled(node, true);
1043 if (ret)
1044 return ret;
1045
8d773c4a 1046 return lists_bind_fdt(parent, node, NULL, false);
e4c98a59 1047}
d7677bfc 1048#endif
This page took 0.563719 seconds and 4 git commands to generate.