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