device.c 21 KB

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