device.c 21 KB

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