device.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  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. #include <common.h>
  11. #include <cpu_func.h>
  12. #include <log.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 <asm/cache.h>
  19. #include <dm/device.h>
  20. #include <dm/device-internal.h>
  21. #include <dm/lists.h>
  22. #include <dm/of_access.h>
  23. #include <dm/pinctrl.h>
  24. #include <dm/platdata.h>
  25. #include <dm/read.h>
  26. #include <dm/uclass.h>
  27. #include <dm/uclass-internal.h>
  28. #include <dm/util.h>
  29. #include <linux/err.h>
  30. #include <linux/list.h>
  31. #include <power-domain.h>
  32. DECLARE_GLOBAL_DATA_PTR;
  33. static int device_bind_common(struct udevice *parent, const struct driver *drv,
  34. const char *name, void *plat,
  35. ulong driver_data, ofnode node,
  36. uint of_platdata_size, struct udevice **devp)
  37. {
  38. struct udevice *dev;
  39. struct uclass *uc;
  40. int size, ret = 0;
  41. if (devp)
  42. *devp = NULL;
  43. if (!name)
  44. return -EINVAL;
  45. ret = uclass_get(drv->id, &uc);
  46. if (ret) {
  47. debug("Missing uclass for driver %s\n", drv->name);
  48. return ret;
  49. }
  50. dev = calloc(1, sizeof(struct udevice));
  51. if (!dev)
  52. return -ENOMEM;
  53. INIT_LIST_HEAD(&dev->sibling_node);
  54. INIT_LIST_HEAD(&dev->child_head);
  55. INIT_LIST_HEAD(&dev->uclass_node);
  56. #ifdef CONFIG_DEVRES
  57. INIT_LIST_HEAD(&dev->devres_head);
  58. #endif
  59. dev->plat = plat;
  60. dev->driver_data = driver_data;
  61. dev->name = name;
  62. dev->node = node;
  63. dev->parent = parent;
  64. dev->driver = drv;
  65. dev->uclass = uc;
  66. dev->seq = -1;
  67. dev->req_seq = -1;
  68. if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
  69. (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS)) {
  70. /*
  71. * Some devices, such as a SPI bus, I2C bus and serial ports
  72. * are numbered using aliases.
  73. *
  74. * This is just a 'requested' sequence, and will be
  75. * resolved (and ->seq updated) when the device is probed.
  76. */
  77. if (CONFIG_IS_ENABLED(OF_CONTROL) &&
  78. !CONFIG_IS_ENABLED(OF_PLATDATA)) {
  79. if (uc->uc_drv->name && ofnode_valid(node))
  80. dev_read_alias_seq(dev, &dev->req_seq);
  81. #if CONFIG_IS_ENABLED(OF_PRIOR_STAGE)
  82. if (dev->req_seq == -1)
  83. dev->req_seq =
  84. uclass_find_next_free_req_seq(drv->id);
  85. #endif
  86. } else {
  87. dev->req_seq = uclass_find_next_free_req_seq(drv->id);
  88. }
  89. }
  90. if (drv->plat_auto) {
  91. bool alloc = !plat;
  92. if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
  93. if (of_platdata_size) {
  94. dev->flags |= DM_FLAG_OF_PLATDATA;
  95. if (of_platdata_size < drv->plat_auto)
  96. alloc = true;
  97. }
  98. }
  99. if (alloc) {
  100. dev->flags |= DM_FLAG_ALLOC_PDATA;
  101. dev->plat = calloc(1, drv->plat_auto);
  102. if (!dev->plat) {
  103. ret = -ENOMEM;
  104. goto fail_alloc1;
  105. }
  106. if (CONFIG_IS_ENABLED(OF_PLATDATA) && plat) {
  107. memcpy(dev->plat, plat,
  108. of_platdata_size);
  109. }
  110. }
  111. }
  112. size = uc->uc_drv->per_device_plat_auto;
  113. if (size) {
  114. dev->flags |= DM_FLAG_ALLOC_UCLASS_PDATA;
  115. dev->uclass_plat = calloc(1, size);
  116. if (!dev->uclass_plat) {
  117. ret = -ENOMEM;
  118. goto fail_alloc2;
  119. }
  120. }
  121. if (parent) {
  122. size = parent->driver->per_child_plat_auto;
  123. if (!size) {
  124. size = parent->uclass->uc_drv->per_child_plat_auto;
  125. }
  126. if (size) {
  127. dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA;
  128. dev->parent_plat = calloc(1, size);
  129. if (!dev->parent_plat) {
  130. ret = -ENOMEM;
  131. goto fail_alloc3;
  132. }
  133. }
  134. /* put dev into parent's successor list */
  135. list_add_tail(&dev->sibling_node, &parent->child_head);
  136. }
  137. ret = uclass_bind_device(dev);
  138. if (ret)
  139. goto fail_uclass_bind;
  140. /* if we fail to bind we remove device from successors and free it */
  141. if (drv->bind) {
  142. ret = drv->bind(dev);
  143. if (ret)
  144. goto fail_bind;
  145. }
  146. if (parent && parent->driver->child_post_bind) {
  147. ret = parent->driver->child_post_bind(dev);
  148. if (ret)
  149. goto fail_child_post_bind;
  150. }
  151. if (uc->uc_drv->post_bind) {
  152. ret = uc->uc_drv->post_bind(dev);
  153. if (ret)
  154. goto fail_uclass_post_bind;
  155. }
  156. if (parent)
  157. pr_debug("Bound device %s to %s\n", dev->name, parent->name);
  158. if (devp)
  159. *devp = dev;
  160. dev->flags |= DM_FLAG_BOUND;
  161. return 0;
  162. fail_uclass_post_bind:
  163. /* There is no child unbind() method, so no clean-up required */
  164. fail_child_post_bind:
  165. if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
  166. if (drv->unbind && drv->unbind(dev)) {
  167. dm_warn("unbind() method failed on dev '%s' on error path\n",
  168. dev->name);
  169. }
  170. }
  171. fail_bind:
  172. if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
  173. if (uclass_unbind_device(dev)) {
  174. dm_warn("Failed to unbind dev '%s' on error path\n",
  175. dev->name);
  176. }
  177. }
  178. fail_uclass_bind:
  179. if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
  180. list_del(&dev->sibling_node);
  181. if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
  182. free(dev->parent_plat);
  183. dev->parent_plat = NULL;
  184. }
  185. }
  186. fail_alloc3:
  187. if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) {
  188. free(dev->uclass_plat);
  189. dev->uclass_plat = NULL;
  190. }
  191. fail_alloc2:
  192. if (dev->flags & DM_FLAG_ALLOC_PDATA) {
  193. free(dev->plat);
  194. dev->plat = NULL;
  195. }
  196. fail_alloc1:
  197. devres_release_all(dev);
  198. free(dev);
  199. return ret;
  200. }
  201. int device_bind_with_driver_data(struct udevice *parent,
  202. const struct driver *drv, const char *name,
  203. ulong driver_data, ofnode node,
  204. struct udevice **devp)
  205. {
  206. return device_bind_common(parent, drv, name, NULL, driver_data, node,
  207. 0, devp);
  208. }
  209. int device_bind(struct udevice *parent, const struct driver *drv,
  210. const char *name, void *plat, ofnode node,
  211. struct udevice **devp)
  212. {
  213. return device_bind_common(parent, drv, name, plat, 0, node, 0,
  214. devp);
  215. }
  216. int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
  217. const struct driver_info *info, struct udevice **devp)
  218. {
  219. struct driver *drv;
  220. uint platdata_size = 0;
  221. int ret;
  222. drv = lists_driver_lookup_name(info->name);
  223. if (!drv)
  224. return -ENOENT;
  225. if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
  226. return -EPERM;
  227. #if CONFIG_IS_ENABLED(OF_PLATDATA)
  228. platdata_size = info->platdata_size;
  229. #endif
  230. ret = device_bind_common(parent, drv, info->name, (void *)info->plat, 0,
  231. ofnode_null(), platdata_size, devp);
  232. if (ret)
  233. return ret;
  234. return ret;
  235. }
  236. int device_reparent(struct udevice *dev, struct udevice *new_parent)
  237. {
  238. struct udevice *pos, *n;
  239. assert(dev);
  240. assert(new_parent);
  241. list_for_each_entry_safe(pos, n, &dev->parent->child_head,
  242. sibling_node) {
  243. if (pos->driver != dev->driver)
  244. continue;
  245. list_del(&dev->sibling_node);
  246. list_add_tail(&dev->sibling_node, &new_parent->child_head);
  247. dev->parent = new_parent;
  248. break;
  249. }
  250. return 0;
  251. }
  252. static void *alloc_priv(int size, uint flags)
  253. {
  254. void *priv;
  255. if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
  256. size = ROUND(size, ARCH_DMA_MINALIGN);
  257. priv = memalign(ARCH_DMA_MINALIGN, size);
  258. if (priv) {
  259. memset(priv, '\0', size);
  260. /*
  261. * Ensure that the zero bytes are flushed to memory.
  262. * This prevents problems if the driver uses this as
  263. * both an input and an output buffer:
  264. *
  265. * 1. Zeroes written to buffer (here) and sit in the
  266. * cache
  267. * 2. Driver issues a read command to DMA
  268. * 3. CPU runs out of cache space and evicts some cache
  269. * data in the buffer, writing zeroes to RAM from
  270. * the memset() above
  271. * 4. DMA completes
  272. * 5. Buffer now has some DMA data and some zeroes
  273. * 6. Data being read is now incorrect
  274. *
  275. * To prevent this, ensure that the cache is clean
  276. * within this range at the start. The driver can then
  277. * use normal flush-after-write, invalidate-before-read
  278. * procedures.
  279. *
  280. * TODO(sjg@chromium.org): Drop this microblaze
  281. * exception.
  282. */
  283. #ifndef CONFIG_MICROBLAZE
  284. flush_dcache_range((ulong)priv, (ulong)priv + size);
  285. #endif
  286. }
  287. } else {
  288. priv = calloc(1, size);
  289. }
  290. return priv;
  291. }
  292. int device_ofdata_to_platdata(struct udevice *dev)
  293. {
  294. const struct driver *drv;
  295. int size = 0;
  296. int ret;
  297. if (!dev)
  298. return -EINVAL;
  299. if (dev->flags & DM_FLAG_PLATDATA_VALID)
  300. return 0;
  301. /* Ensure all parents have ofdata */
  302. if (dev->parent) {
  303. ret = device_ofdata_to_platdata(dev->parent);
  304. if (ret)
  305. goto fail;
  306. /*
  307. * The device might have already been probed during
  308. * the call to device_probe() on its parent device
  309. * (e.g. PCI bridge devices). Test the flags again
  310. * so that we don't mess up the device.
  311. */
  312. if (dev->flags & DM_FLAG_PLATDATA_VALID)
  313. return 0;
  314. }
  315. drv = dev->driver;
  316. assert(drv);
  317. /* Allocate private data if requested and not reentered */
  318. if (drv->priv_auto && !dev->priv) {
  319. dev->priv = alloc_priv(drv->priv_auto, drv->flags);
  320. if (!dev->priv) {
  321. ret = -ENOMEM;
  322. goto fail;
  323. }
  324. }
  325. /* Allocate private data if requested and not reentered */
  326. size = dev->uclass->uc_drv->per_device_auto;
  327. if (size && !dev->uclass_priv) {
  328. dev->uclass_priv = alloc_priv(size,
  329. dev->uclass->uc_drv->flags);
  330. if (!dev->uclass_priv) {
  331. ret = -ENOMEM;
  332. goto fail;
  333. }
  334. }
  335. /* Allocate parent data for this child */
  336. if (dev->parent) {
  337. size = dev->parent->driver->per_child_auto;
  338. if (!size) {
  339. size = dev->parent->uclass->uc_drv->per_child_auto;
  340. }
  341. if (size && !dev->parent_priv) {
  342. dev->parent_priv = alloc_priv(size, drv->flags);
  343. if (!dev->parent_priv) {
  344. ret = -ENOMEM;
  345. goto fail;
  346. }
  347. }
  348. }
  349. if (drv->ofdata_to_platdata &&
  350. (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_of_node(dev))) {
  351. ret = drv->ofdata_to_platdata(dev);
  352. if (ret)
  353. goto fail;
  354. }
  355. dev->flags |= DM_FLAG_PLATDATA_VALID;
  356. return 0;
  357. fail:
  358. device_free(dev);
  359. return ret;
  360. }
  361. int device_probe(struct udevice *dev)
  362. {
  363. const struct driver *drv;
  364. int ret;
  365. int seq;
  366. if (!dev)
  367. return -EINVAL;
  368. if (dev->flags & DM_FLAG_ACTIVATED)
  369. return 0;
  370. drv = dev->driver;
  371. assert(drv);
  372. ret = device_ofdata_to_platdata(dev);
  373. if (ret)
  374. goto fail;
  375. /* Ensure all parents are probed */
  376. if (dev->parent) {
  377. ret = device_probe(dev->parent);
  378. if (ret)
  379. goto fail;
  380. /*
  381. * The device might have already been probed during
  382. * the call to device_probe() on its parent device
  383. * (e.g. PCI bridge devices). Test the flags again
  384. * so that we don't mess up the device.
  385. */
  386. if (dev->flags & DM_FLAG_ACTIVATED)
  387. return 0;
  388. }
  389. seq = uclass_resolve_seq(dev);
  390. if (seq < 0) {
  391. ret = seq;
  392. goto fail;
  393. }
  394. dev->seq = seq;
  395. dev->flags |= DM_FLAG_ACTIVATED;
  396. /*
  397. * Process pinctrl for everything except the root device, and
  398. * continue regardless of the result of pinctrl. Don't process pinctrl
  399. * settings for pinctrl devices since the device may not yet be
  400. * probed.
  401. */
  402. if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
  403. pinctrl_select_state(dev, "default");
  404. if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
  405. (device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) &&
  406. !(drv->flags & DM_FLAG_DEFAULT_PD_CTRL_OFF)) {
  407. ret = dev_power_domain_on(dev);
  408. if (ret)
  409. goto fail;
  410. }
  411. ret = uclass_pre_probe_device(dev);
  412. if (ret)
  413. goto fail;
  414. if (dev->parent && dev->parent->driver->child_pre_probe) {
  415. ret = dev->parent->driver->child_pre_probe(dev);
  416. if (ret)
  417. goto fail;
  418. }
  419. /* Only handle devices that have a valid ofnode */
  420. if (dev_of_valid(dev)) {
  421. /*
  422. * Process 'assigned-{clocks/clock-parents/clock-rates}'
  423. * properties
  424. */
  425. ret = clk_set_defaults(dev, 0);
  426. if (ret)
  427. goto fail;
  428. }
  429. if (drv->probe) {
  430. ret = drv->probe(dev);
  431. if (ret)
  432. goto fail;
  433. }
  434. ret = uclass_post_probe_device(dev);
  435. if (ret)
  436. goto fail_uclass;
  437. if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
  438. pinctrl_select_state(dev, "default");
  439. return 0;
  440. fail_uclass:
  441. if (device_remove(dev, DM_REMOVE_NORMAL)) {
  442. dm_warn("%s: Device '%s' failed to remove on error path\n",
  443. __func__, dev->name);
  444. }
  445. fail:
  446. dev->flags &= ~DM_FLAG_ACTIVATED;
  447. dev->seq = -1;
  448. device_free(dev);
  449. return ret;
  450. }
  451. void *dev_get_platdata(const struct udevice *dev)
  452. {
  453. if (!dev) {
  454. dm_warn("%s: null device\n", __func__);
  455. return NULL;
  456. }
  457. return dev->plat;
  458. }
  459. void *dev_get_parent_plat(const struct udevice *dev)
  460. {
  461. if (!dev) {
  462. dm_warn("%s: null device\n", __func__);
  463. return NULL;
  464. }
  465. return dev->parent_plat;
  466. }
  467. void *dev_get_uclass_plat(const struct udevice *dev)
  468. {
  469. if (!dev) {
  470. dm_warn("%s: null device\n", __func__);
  471. return NULL;
  472. }
  473. return dev->uclass_plat;
  474. }
  475. void *dev_get_priv(const struct udevice *dev)
  476. {
  477. if (!dev) {
  478. dm_warn("%s: null device\n", __func__);
  479. return NULL;
  480. }
  481. return dev->priv;
  482. }
  483. void *dev_get_uclass_priv(const struct udevice *dev)
  484. {
  485. if (!dev) {
  486. dm_warn("%s: null device\n", __func__);
  487. return NULL;
  488. }
  489. return dev->uclass_priv;
  490. }
  491. void *dev_get_parent_priv(const struct udevice *dev)
  492. {
  493. if (!dev) {
  494. dm_warn("%s: null device\n", __func__);
  495. return NULL;
  496. }
  497. return dev->parent_priv;
  498. }
  499. static int device_get_device_tail(struct udevice *dev, int ret,
  500. struct udevice **devp)
  501. {
  502. if (ret)
  503. return ret;
  504. ret = device_probe(dev);
  505. if (ret)
  506. return ret;
  507. *devp = dev;
  508. return 0;
  509. }
  510. #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
  511. /**
  512. * device_find_by_ofnode() - Return device associated with given ofnode
  513. *
  514. * The returned device is *not* activated.
  515. *
  516. * @node: The ofnode for which a associated device should be looked up
  517. * @devp: Pointer to structure to hold the found device
  518. * Return: 0 if OK, -ve on error
  519. */
  520. static int device_find_by_ofnode(ofnode node, struct udevice **devp)
  521. {
  522. struct uclass *uc;
  523. struct udevice *dev;
  524. int ret;
  525. list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
  526. ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node,
  527. &dev);
  528. if (!ret || dev) {
  529. *devp = dev;
  530. return 0;
  531. }
  532. }
  533. return -ENODEV;
  534. }
  535. #endif
  536. int device_get_child(const struct udevice *parent, int index,
  537. struct udevice **devp)
  538. {
  539. struct udevice *dev;
  540. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  541. if (!index--)
  542. return device_get_device_tail(dev, 0, devp);
  543. }
  544. return -ENODEV;
  545. }
  546. int device_get_child_count(const struct udevice *parent)
  547. {
  548. struct udevice *dev;
  549. int count = 0;
  550. list_for_each_entry(dev, &parent->child_head, sibling_node)
  551. count++;
  552. return count;
  553. }
  554. int device_find_child_by_seq(const struct udevice *parent, int seq_or_req_seq,
  555. bool find_req_seq, struct udevice **devp)
  556. {
  557. struct udevice *dev;
  558. *devp = NULL;
  559. if (seq_or_req_seq == -1)
  560. return -ENODEV;
  561. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  562. if ((find_req_seq ? dev->req_seq : dev->seq) ==
  563. seq_or_req_seq) {
  564. *devp = dev;
  565. return 0;
  566. }
  567. }
  568. return -ENODEV;
  569. }
  570. int device_get_child_by_seq(const struct udevice *parent, int seq,
  571. struct udevice **devp)
  572. {
  573. struct udevice *dev;
  574. int ret;
  575. *devp = NULL;
  576. ret = device_find_child_by_seq(parent, seq, false, &dev);
  577. if (ret == -ENODEV) {
  578. /*
  579. * We didn't find it in probed devices. See if there is one
  580. * that will request this seq if probed.
  581. */
  582. ret = device_find_child_by_seq(parent, seq, true, &dev);
  583. }
  584. return device_get_device_tail(dev, ret, devp);
  585. }
  586. int device_find_child_by_of_offset(const struct udevice *parent, int of_offset,
  587. struct udevice **devp)
  588. {
  589. struct udevice *dev;
  590. *devp = NULL;
  591. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  592. if (dev_of_offset(dev) == of_offset) {
  593. *devp = dev;
  594. return 0;
  595. }
  596. }
  597. return -ENODEV;
  598. }
  599. int device_get_child_by_of_offset(const struct udevice *parent, int node,
  600. struct udevice **devp)
  601. {
  602. struct udevice *dev;
  603. int ret;
  604. *devp = NULL;
  605. ret = device_find_child_by_of_offset(parent, node, &dev);
  606. return device_get_device_tail(dev, ret, devp);
  607. }
  608. static struct udevice *_device_find_global_by_ofnode(struct udevice *parent,
  609. ofnode ofnode)
  610. {
  611. struct udevice *dev, *found;
  612. if (ofnode_equal(dev_ofnode(parent), ofnode))
  613. return parent;
  614. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  615. found = _device_find_global_by_ofnode(dev, ofnode);
  616. if (found)
  617. return found;
  618. }
  619. return NULL;
  620. }
  621. int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp)
  622. {
  623. *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode);
  624. return *devp ? 0 : -ENOENT;
  625. }
  626. int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
  627. {
  628. struct udevice *dev;
  629. dev = _device_find_global_by_ofnode(gd->dm_root, ofnode);
  630. return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
  631. }
  632. #if CONFIG_IS_ENABLED(OF_PLATDATA)
  633. int device_get_by_driver_info(const struct driver_info *info,
  634. struct udevice **devp)
  635. {
  636. struct driver_info *info_base =
  637. ll_entry_start(struct driver_info, driver_info);
  638. int idx = info - info_base;
  639. struct driver_rt *drt = gd_dm_driver_rt() + idx;
  640. struct udevice *dev;
  641. dev = drt->dev;
  642. *devp = NULL;
  643. return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
  644. }
  645. int device_get_by_driver_info_idx(uint idx, struct udevice **devp)
  646. {
  647. struct driver_rt *drt = gd_dm_driver_rt() + idx;
  648. struct udevice *dev;
  649. dev = drt->dev;
  650. *devp = NULL;
  651. return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
  652. }
  653. #endif
  654. int device_find_first_child(const struct udevice *parent, struct udevice **devp)
  655. {
  656. if (list_empty(&parent->child_head)) {
  657. *devp = NULL;
  658. } else {
  659. *devp = list_first_entry(&parent->child_head, struct udevice,
  660. sibling_node);
  661. }
  662. return 0;
  663. }
  664. int device_find_next_child(struct udevice **devp)
  665. {
  666. struct udevice *dev = *devp;
  667. struct udevice *parent = dev->parent;
  668. if (list_is_last(&dev->sibling_node, &parent->child_head)) {
  669. *devp = NULL;
  670. } else {
  671. *devp = list_entry(dev->sibling_node.next, struct udevice,
  672. sibling_node);
  673. }
  674. return 0;
  675. }
  676. int device_find_first_inactive_child(const struct udevice *parent,
  677. enum uclass_id uclass_id,
  678. struct udevice **devp)
  679. {
  680. struct udevice *dev;
  681. *devp = NULL;
  682. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  683. if (!device_active(dev) &&
  684. device_get_uclass_id(dev) == uclass_id) {
  685. *devp = dev;
  686. return 0;
  687. }
  688. }
  689. return -ENODEV;
  690. }
  691. int device_find_first_child_by_uclass(const struct udevice *parent,
  692. enum uclass_id uclass_id,
  693. struct udevice **devp)
  694. {
  695. struct udevice *dev;
  696. *devp = NULL;
  697. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  698. if (device_get_uclass_id(dev) == uclass_id) {
  699. *devp = dev;
  700. return 0;
  701. }
  702. }
  703. return -ENODEV;
  704. }
  705. int device_find_child_by_name(const struct udevice *parent, const char *name,
  706. struct udevice **devp)
  707. {
  708. struct udevice *dev;
  709. *devp = NULL;
  710. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  711. if (!strcmp(dev->name, name)) {
  712. *devp = dev;
  713. return 0;
  714. }
  715. }
  716. return -ENODEV;
  717. }
  718. int device_first_child_err(struct udevice *parent, struct udevice **devp)
  719. {
  720. struct udevice *dev;
  721. device_find_first_child(parent, &dev);
  722. if (!dev)
  723. return -ENODEV;
  724. return device_get_device_tail(dev, 0, devp);
  725. }
  726. int device_next_child_err(struct udevice **devp)
  727. {
  728. struct udevice *dev = *devp;
  729. device_find_next_child(&dev);
  730. if (!dev)
  731. return -ENODEV;
  732. return device_get_device_tail(dev, 0, devp);
  733. }
  734. int device_first_child_ofdata_err(struct udevice *parent, struct udevice **devp)
  735. {
  736. struct udevice *dev;
  737. int ret;
  738. device_find_first_child(parent, &dev);
  739. if (!dev)
  740. return -ENODEV;
  741. ret = device_ofdata_to_platdata(dev);
  742. if (ret)
  743. return ret;
  744. *devp = dev;
  745. return 0;
  746. }
  747. int device_next_child_ofdata_err(struct udevice **devp)
  748. {
  749. struct udevice *dev = *devp;
  750. int ret;
  751. device_find_next_child(&dev);
  752. if (!dev)
  753. return -ENODEV;
  754. ret = device_ofdata_to_platdata(dev);
  755. if (ret)
  756. return ret;
  757. *devp = dev;
  758. return 0;
  759. }
  760. struct udevice *dev_get_parent(const struct udevice *child)
  761. {
  762. return child->parent;
  763. }
  764. ulong dev_get_driver_data(const struct udevice *dev)
  765. {
  766. return dev->driver_data;
  767. }
  768. const void *dev_get_driver_ops(const struct udevice *dev)
  769. {
  770. if (!dev || !dev->driver->ops)
  771. return NULL;
  772. return dev->driver->ops;
  773. }
  774. enum uclass_id device_get_uclass_id(const struct udevice *dev)
  775. {
  776. return dev->uclass->uc_drv->id;
  777. }
  778. const char *dev_get_uclass_name(const struct udevice *dev)
  779. {
  780. if (!dev)
  781. return NULL;
  782. return dev->uclass->uc_drv->name;
  783. }
  784. bool device_has_children(const struct udevice *dev)
  785. {
  786. return !list_empty(&dev->child_head);
  787. }
  788. bool device_has_active_children(const struct udevice *dev)
  789. {
  790. struct udevice *child;
  791. for (device_find_first_child(dev, &child);
  792. child;
  793. device_find_next_child(&child)) {
  794. if (device_active(child))
  795. return true;
  796. }
  797. return false;
  798. }
  799. bool device_is_last_sibling(const struct udevice *dev)
  800. {
  801. struct udevice *parent = dev->parent;
  802. if (!parent)
  803. return false;
  804. return list_is_last(&dev->sibling_node, &parent->child_head);
  805. }
  806. void device_set_name_alloced(struct udevice *dev)
  807. {
  808. dev->flags |= DM_FLAG_NAME_ALLOCED;
  809. }
  810. int device_set_name(struct udevice *dev, const char *name)
  811. {
  812. name = strdup(name);
  813. if (!name)
  814. return -ENOMEM;
  815. dev->name = name;
  816. device_set_name_alloced(dev);
  817. return 0;
  818. }
  819. #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
  820. bool device_is_compatible(const struct udevice *dev, const char *compat)
  821. {
  822. return ofnode_device_is_compatible(dev_ofnode(dev), compat);
  823. }
  824. bool of_machine_is_compatible(const char *compat)
  825. {
  826. const void *fdt = gd->fdt_blob;
  827. return !fdt_node_check_compatible(fdt, 0, compat);
  828. }
  829. int dev_disable_by_path(const char *path)
  830. {
  831. struct uclass *uc;
  832. ofnode node = ofnode_path(path);
  833. struct udevice *dev;
  834. int ret = 1;
  835. if (!of_live_active())
  836. return -ENOSYS;
  837. list_for_each_entry(uc, &gd->uclass_root, sibling_node) {
  838. ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev);
  839. if (!ret)
  840. break;
  841. }
  842. if (ret)
  843. return ret;
  844. ret = device_remove(dev, DM_REMOVE_NORMAL);
  845. if (ret)
  846. return ret;
  847. ret = device_unbind(dev);
  848. if (ret)
  849. return ret;
  850. return ofnode_set_enabled(node, false);
  851. }
  852. int dev_enable_by_path(const char *path)
  853. {
  854. ofnode node = ofnode_path(path);
  855. ofnode pnode = ofnode_get_parent(node);
  856. struct udevice *parent;
  857. int ret = 1;
  858. if (!of_live_active())
  859. return -ENOSYS;
  860. ret = device_find_by_ofnode(pnode, &parent);
  861. if (ret)
  862. return ret;
  863. ret = ofnode_set_enabled(node, true);
  864. if (ret)
  865. return ret;
  866. return lists_bind_fdt(parent, node, NULL, false);
  867. }
  868. #endif