device.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. /*
  2. * Device manager
  3. *
  4. * Copyright (c) 2013 Google, Inc
  5. *
  6. * (C) Copyright 2012
  7. * Pavel Herrmann <morpheus.ibis@gmail.com>
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. #include <common.h>
  12. #include <asm/io.h>
  13. #include <fdtdec.h>
  14. #include <fdt_support.h>
  15. #include <malloc.h>
  16. #include <dm/device.h>
  17. #include <dm/device-internal.h>
  18. #include <dm/lists.h>
  19. #include <dm/pinctrl.h>
  20. #include <dm/platdata.h>
  21. #include <dm/uclass.h>
  22. #include <dm/uclass-internal.h>
  23. #include <dm/util.h>
  24. #include <linux/err.h>
  25. #include <linux/list.h>
  26. DECLARE_GLOBAL_DATA_PTR;
  27. static int device_bind_common(struct udevice *parent, const struct driver *drv,
  28. const char *name, void *platdata,
  29. ulong driver_data, int of_offset,
  30. uint of_platdata_size, struct udevice **devp)
  31. {
  32. struct udevice *dev;
  33. struct uclass *uc;
  34. int size, ret = 0;
  35. if (devp)
  36. *devp = NULL;
  37. if (!name)
  38. return -EINVAL;
  39. ret = uclass_get(drv->id, &uc);
  40. if (ret) {
  41. debug("Missing uclass for driver %s\n", drv->name);
  42. return ret;
  43. }
  44. dev = calloc(1, sizeof(struct udevice));
  45. if (!dev)
  46. return -ENOMEM;
  47. INIT_LIST_HEAD(&dev->sibling_node);
  48. INIT_LIST_HEAD(&dev->child_head);
  49. INIT_LIST_HEAD(&dev->uclass_node);
  50. #ifdef CONFIG_DEVRES
  51. INIT_LIST_HEAD(&dev->devres_head);
  52. #endif
  53. dev->platdata = platdata;
  54. dev->driver_data = driver_data;
  55. dev->name = name;
  56. dev->of_offset = of_offset;
  57. dev->parent = parent;
  58. dev->driver = drv;
  59. dev->uclass = uc;
  60. dev->seq = -1;
  61. dev->req_seq = -1;
  62. if (CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(DM_SEQ_ALIAS)) {
  63. /*
  64. * Some devices, such as a SPI bus, I2C bus and serial ports
  65. * are numbered using aliases.
  66. *
  67. * This is just a 'requested' sequence, and will be
  68. * resolved (and ->seq updated) when the device is probed.
  69. */
  70. if (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS) {
  71. if (uc->uc_drv->name && of_offset != -1) {
  72. fdtdec_get_alias_seq(gd->fdt_blob,
  73. uc->uc_drv->name, of_offset,
  74. &dev->req_seq);
  75. }
  76. }
  77. }
  78. if (drv->platdata_auto_alloc_size) {
  79. bool alloc = !platdata;
  80. if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
  81. if (of_platdata_size) {
  82. dev->flags |= DM_FLAG_OF_PLATDATA;
  83. if (of_platdata_size <
  84. drv->platdata_auto_alloc_size)
  85. alloc = true;
  86. }
  87. }
  88. if (alloc) {
  89. dev->flags |= DM_FLAG_ALLOC_PDATA;
  90. dev->platdata = calloc(1,
  91. drv->platdata_auto_alloc_size);
  92. if (!dev->platdata) {
  93. ret = -ENOMEM;
  94. goto fail_alloc1;
  95. }
  96. if (CONFIG_IS_ENABLED(OF_PLATDATA) && platdata) {
  97. memcpy(dev->platdata, platdata,
  98. of_platdata_size);
  99. }
  100. }
  101. }
  102. size = uc->uc_drv->per_device_platdata_auto_alloc_size;
  103. if (size) {
  104. dev->flags |= DM_FLAG_ALLOC_UCLASS_PDATA;
  105. dev->uclass_platdata = calloc(1, size);
  106. if (!dev->uclass_platdata) {
  107. ret = -ENOMEM;
  108. goto fail_alloc2;
  109. }
  110. }
  111. if (parent) {
  112. size = parent->driver->per_child_platdata_auto_alloc_size;
  113. if (!size) {
  114. size = parent->uclass->uc_drv->
  115. per_child_platdata_auto_alloc_size;
  116. }
  117. if (size) {
  118. dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA;
  119. dev->parent_platdata = calloc(1, size);
  120. if (!dev->parent_platdata) {
  121. ret = -ENOMEM;
  122. goto fail_alloc3;
  123. }
  124. }
  125. }
  126. /* put dev into parent's successor list */
  127. if (parent)
  128. list_add_tail(&dev->sibling_node, &parent->child_head);
  129. ret = uclass_bind_device(dev);
  130. if (ret)
  131. goto fail_uclass_bind;
  132. /* if we fail to bind we remove device from successors and free it */
  133. if (drv->bind) {
  134. ret = drv->bind(dev);
  135. if (ret)
  136. goto fail_bind;
  137. }
  138. if (parent && parent->driver->child_post_bind) {
  139. ret = parent->driver->child_post_bind(dev);
  140. if (ret)
  141. goto fail_child_post_bind;
  142. }
  143. if (uc->uc_drv->post_bind) {
  144. ret = uc->uc_drv->post_bind(dev);
  145. if (ret)
  146. goto fail_uclass_post_bind;
  147. }
  148. if (parent)
  149. dm_dbg("Bound device %s to %s\n", dev->name, parent->name);
  150. if (devp)
  151. *devp = dev;
  152. dev->flags |= DM_FLAG_BOUND;
  153. return 0;
  154. fail_uclass_post_bind:
  155. /* There is no child unbind() method, so no clean-up required */
  156. fail_child_post_bind:
  157. if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
  158. if (drv->unbind && drv->unbind(dev)) {
  159. dm_warn("unbind() method failed on dev '%s' on error path\n",
  160. dev->name);
  161. }
  162. }
  163. fail_bind:
  164. if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
  165. if (uclass_unbind_device(dev)) {
  166. dm_warn("Failed to unbind dev '%s' on error path\n",
  167. dev->name);
  168. }
  169. }
  170. fail_uclass_bind:
  171. if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
  172. list_del(&dev->sibling_node);
  173. if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
  174. free(dev->parent_platdata);
  175. dev->parent_platdata = NULL;
  176. }
  177. }
  178. fail_alloc3:
  179. if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) {
  180. free(dev->uclass_platdata);
  181. dev->uclass_platdata = NULL;
  182. }
  183. fail_alloc2:
  184. if (dev->flags & DM_FLAG_ALLOC_PDATA) {
  185. free(dev->platdata);
  186. dev->platdata = NULL;
  187. }
  188. fail_alloc1:
  189. devres_release_all(dev);
  190. free(dev);
  191. return ret;
  192. }
  193. int device_bind_with_driver_data(struct udevice *parent,
  194. const struct driver *drv, const char *name,
  195. ulong driver_data, int of_offset,
  196. struct udevice **devp)
  197. {
  198. return device_bind_common(parent, drv, name, NULL, driver_data,
  199. of_offset, 0, devp);
  200. }
  201. int device_bind(struct udevice *parent, const struct driver *drv,
  202. const char *name, void *platdata, int of_offset,
  203. struct udevice **devp)
  204. {
  205. return device_bind_common(parent, drv, name, platdata, 0, of_offset, 0,
  206. devp);
  207. }
  208. int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
  209. const struct driver_info *info, struct udevice **devp)
  210. {
  211. struct driver *drv;
  212. uint platdata_size = 0;
  213. drv = lists_driver_lookup_name(info->name);
  214. if (!drv)
  215. return -ENOENT;
  216. if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
  217. return -EPERM;
  218. #if CONFIG_IS_ENABLED(OF_PLATDATA)
  219. platdata_size = info->platdata_size;
  220. #endif
  221. return device_bind_common(parent, drv, info->name,
  222. (void *)info->platdata, 0, -1, platdata_size, devp);
  223. }
  224. static void *alloc_priv(int size, uint flags)
  225. {
  226. void *priv;
  227. if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
  228. priv = memalign(ARCH_DMA_MINALIGN, size);
  229. if (priv)
  230. memset(priv, '\0', size);
  231. } else {
  232. priv = calloc(1, size);
  233. }
  234. return priv;
  235. }
  236. int device_probe(struct udevice *dev)
  237. {
  238. const struct driver *drv;
  239. int size = 0;
  240. int ret;
  241. int seq;
  242. if (!dev)
  243. return -EINVAL;
  244. if (dev->flags & DM_FLAG_ACTIVATED)
  245. return 0;
  246. drv = dev->driver;
  247. assert(drv);
  248. /* Allocate private data if requested and not reentered */
  249. if (drv->priv_auto_alloc_size && !dev->priv) {
  250. dev->priv = alloc_priv(drv->priv_auto_alloc_size, drv->flags);
  251. if (!dev->priv) {
  252. ret = -ENOMEM;
  253. goto fail;
  254. }
  255. }
  256. /* Allocate private data if requested and not reentered */
  257. size = dev->uclass->uc_drv->per_device_auto_alloc_size;
  258. if (size && !dev->uclass_priv) {
  259. dev->uclass_priv = calloc(1, size);
  260. if (!dev->uclass_priv) {
  261. ret = -ENOMEM;
  262. goto fail;
  263. }
  264. }
  265. /* Ensure all parents are probed */
  266. if (dev->parent) {
  267. size = dev->parent->driver->per_child_auto_alloc_size;
  268. if (!size) {
  269. size = dev->parent->uclass->uc_drv->
  270. per_child_auto_alloc_size;
  271. }
  272. if (size && !dev->parent_priv) {
  273. dev->parent_priv = alloc_priv(size, drv->flags);
  274. if (!dev->parent_priv) {
  275. ret = -ENOMEM;
  276. goto fail;
  277. }
  278. }
  279. ret = device_probe(dev->parent);
  280. if (ret)
  281. goto fail;
  282. /*
  283. * The device might have already been probed during
  284. * the call to device_probe() on its parent device
  285. * (e.g. PCI bridge devices). Test the flags again
  286. * so that we don't mess up the device.
  287. */
  288. if (dev->flags & DM_FLAG_ACTIVATED)
  289. return 0;
  290. }
  291. seq = uclass_resolve_seq(dev);
  292. if (seq < 0) {
  293. ret = seq;
  294. goto fail;
  295. }
  296. dev->seq = seq;
  297. dev->flags |= DM_FLAG_ACTIVATED;
  298. /*
  299. * Process pinctrl for everything except the root device, and
  300. * continue regardless of the result of pinctrl. Don't process pinctrl
  301. * settings for pinctrl devices since the device may not yet be
  302. * probed.
  303. */
  304. if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
  305. pinctrl_select_state(dev, "default");
  306. ret = uclass_pre_probe_device(dev);
  307. if (ret)
  308. goto fail;
  309. if (dev->parent && dev->parent->driver->child_pre_probe) {
  310. ret = dev->parent->driver->child_pre_probe(dev);
  311. if (ret)
  312. goto fail;
  313. }
  314. if (drv->ofdata_to_platdata && dev_of_offset(dev) >= 0) {
  315. ret = drv->ofdata_to_platdata(dev);
  316. if (ret)
  317. goto fail;
  318. }
  319. if (drv->probe) {
  320. ret = drv->probe(dev);
  321. if (ret) {
  322. dev->flags &= ~DM_FLAG_ACTIVATED;
  323. goto fail;
  324. }
  325. }
  326. ret = uclass_post_probe_device(dev);
  327. if (ret)
  328. goto fail_uclass;
  329. if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
  330. pinctrl_select_state(dev, "default");
  331. return 0;
  332. fail_uclass:
  333. if (device_remove(dev)) {
  334. dm_warn("%s: Device '%s' failed to remove on error path\n",
  335. __func__, dev->name);
  336. }
  337. fail:
  338. dev->flags &= ~DM_FLAG_ACTIVATED;
  339. dev->seq = -1;
  340. device_free(dev);
  341. return ret;
  342. }
  343. void *dev_get_platdata(struct udevice *dev)
  344. {
  345. if (!dev) {
  346. dm_warn("%s: null device\n", __func__);
  347. return NULL;
  348. }
  349. return dev->platdata;
  350. }
  351. void *dev_get_parent_platdata(struct udevice *dev)
  352. {
  353. if (!dev) {
  354. dm_warn("%s: null device\n", __func__);
  355. return NULL;
  356. }
  357. return dev->parent_platdata;
  358. }
  359. void *dev_get_uclass_platdata(struct udevice *dev)
  360. {
  361. if (!dev) {
  362. dm_warn("%s: null device\n", __func__);
  363. return NULL;
  364. }
  365. return dev->uclass_platdata;
  366. }
  367. void *dev_get_priv(struct udevice *dev)
  368. {
  369. if (!dev) {
  370. dm_warn("%s: null device\n", __func__);
  371. return NULL;
  372. }
  373. return dev->priv;
  374. }
  375. void *dev_get_uclass_priv(struct udevice *dev)
  376. {
  377. if (!dev) {
  378. dm_warn("%s: null device\n", __func__);
  379. return NULL;
  380. }
  381. return dev->uclass_priv;
  382. }
  383. void *dev_get_parent_priv(struct udevice *dev)
  384. {
  385. if (!dev) {
  386. dm_warn("%s: null device\n", __func__);
  387. return NULL;
  388. }
  389. return dev->parent_priv;
  390. }
  391. static int device_get_device_tail(struct udevice *dev, int ret,
  392. struct udevice **devp)
  393. {
  394. if (ret)
  395. return ret;
  396. ret = device_probe(dev);
  397. if (ret)
  398. return ret;
  399. *devp = dev;
  400. return 0;
  401. }
  402. int device_get_child(struct udevice *parent, int index, struct udevice **devp)
  403. {
  404. struct udevice *dev;
  405. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  406. if (!index--)
  407. return device_get_device_tail(dev, 0, devp);
  408. }
  409. return -ENODEV;
  410. }
  411. int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
  412. bool find_req_seq, struct udevice **devp)
  413. {
  414. struct udevice *dev;
  415. *devp = NULL;
  416. if (seq_or_req_seq == -1)
  417. return -ENODEV;
  418. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  419. if ((find_req_seq ? dev->req_seq : dev->seq) ==
  420. seq_or_req_seq) {
  421. *devp = dev;
  422. return 0;
  423. }
  424. }
  425. return -ENODEV;
  426. }
  427. int device_get_child_by_seq(struct udevice *parent, int seq,
  428. struct udevice **devp)
  429. {
  430. struct udevice *dev;
  431. int ret;
  432. *devp = NULL;
  433. ret = device_find_child_by_seq(parent, seq, false, &dev);
  434. if (ret == -ENODEV) {
  435. /*
  436. * We didn't find it in probed devices. See if there is one
  437. * that will request this seq if probed.
  438. */
  439. ret = device_find_child_by_seq(parent, seq, true, &dev);
  440. }
  441. return device_get_device_tail(dev, ret, devp);
  442. }
  443. int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
  444. struct udevice **devp)
  445. {
  446. struct udevice *dev;
  447. *devp = NULL;
  448. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  449. if (dev_of_offset(dev) == of_offset) {
  450. *devp = dev;
  451. return 0;
  452. }
  453. }
  454. return -ENODEV;
  455. }
  456. int device_get_child_by_of_offset(struct udevice *parent, int node,
  457. struct udevice **devp)
  458. {
  459. struct udevice *dev;
  460. int ret;
  461. *devp = NULL;
  462. ret = device_find_child_by_of_offset(parent, node, &dev);
  463. return device_get_device_tail(dev, ret, devp);
  464. }
  465. static struct udevice *_device_find_global_by_of_offset(struct udevice *parent,
  466. int of_offset)
  467. {
  468. struct udevice *dev, *found;
  469. if (dev_of_offset(parent) == of_offset)
  470. return parent;
  471. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  472. found = _device_find_global_by_of_offset(dev, of_offset);
  473. if (found)
  474. return found;
  475. }
  476. return NULL;
  477. }
  478. int device_get_global_by_of_offset(int of_offset, struct udevice **devp)
  479. {
  480. struct udevice *dev;
  481. dev = _device_find_global_by_of_offset(gd->dm_root, of_offset);
  482. return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
  483. }
  484. int device_find_first_child(struct udevice *parent, struct udevice **devp)
  485. {
  486. if (list_empty(&parent->child_head)) {
  487. *devp = NULL;
  488. } else {
  489. *devp = list_first_entry(&parent->child_head, struct udevice,
  490. sibling_node);
  491. }
  492. return 0;
  493. }
  494. int device_find_next_child(struct udevice **devp)
  495. {
  496. struct udevice *dev = *devp;
  497. struct udevice *parent = dev->parent;
  498. if (list_is_last(&dev->sibling_node, &parent->child_head)) {
  499. *devp = NULL;
  500. } else {
  501. *devp = list_entry(dev->sibling_node.next, struct udevice,
  502. sibling_node);
  503. }
  504. return 0;
  505. }
  506. struct udevice *dev_get_parent(struct udevice *child)
  507. {
  508. return child->parent;
  509. }
  510. ulong dev_get_driver_data(struct udevice *dev)
  511. {
  512. return dev->driver_data;
  513. }
  514. const void *dev_get_driver_ops(struct udevice *dev)
  515. {
  516. if (!dev || !dev->driver->ops)
  517. return NULL;
  518. return dev->driver->ops;
  519. }
  520. enum uclass_id device_get_uclass_id(struct udevice *dev)
  521. {
  522. return dev->uclass->uc_drv->id;
  523. }
  524. const char *dev_get_uclass_name(struct udevice *dev)
  525. {
  526. if (!dev)
  527. return NULL;
  528. return dev->uclass->uc_drv->name;
  529. }
  530. fdt_addr_t dev_get_addr_index(struct udevice *dev, int index)
  531. {
  532. #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
  533. fdt_addr_t addr;
  534. if (CONFIG_IS_ENABLED(OF_TRANSLATE)) {
  535. const fdt32_t *reg;
  536. int len = 0;
  537. int na, ns;
  538. na = fdt_address_cells(gd->fdt_blob,
  539. dev_of_offset(dev->parent));
  540. if (na < 1) {
  541. debug("bad #address-cells\n");
  542. return FDT_ADDR_T_NONE;
  543. }
  544. ns = fdt_size_cells(gd->fdt_blob, dev_of_offset(dev->parent));
  545. if (ns < 0) {
  546. debug("bad #size-cells\n");
  547. return FDT_ADDR_T_NONE;
  548. }
  549. reg = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "reg",
  550. &len);
  551. if (!reg || (len <= (index * sizeof(fdt32_t) * (na + ns)))) {
  552. debug("Req index out of range\n");
  553. return FDT_ADDR_T_NONE;
  554. }
  555. reg += index * (na + ns);
  556. /*
  557. * Use the full-fledged translate function for complex
  558. * bus setups.
  559. */
  560. addr = fdt_translate_address((void *)gd->fdt_blob,
  561. dev_of_offset(dev), reg);
  562. } else {
  563. /*
  564. * Use the "simple" translate function for less complex
  565. * bus setups.
  566. */
  567. addr = fdtdec_get_addr_size_auto_parent(gd->fdt_blob,
  568. dev_of_offset(dev->parent), dev_of_offset(dev),
  569. "reg", index, NULL, false);
  570. if (CONFIG_IS_ENABLED(SIMPLE_BUS) && addr != FDT_ADDR_T_NONE) {
  571. if (device_get_uclass_id(dev->parent) ==
  572. UCLASS_SIMPLE_BUS)
  573. addr = simple_bus_translate(dev->parent, addr);
  574. }
  575. }
  576. /*
  577. * Some platforms need a special address translation. Those
  578. * platforms (e.g. mvebu in SPL) can configure a translation
  579. * offset in the DM by calling dm_set_translation_offset() that
  580. * will get added to all addresses returned by dev_get_addr().
  581. */
  582. addr += dm_get_translation_offset();
  583. return addr;
  584. #else
  585. return FDT_ADDR_T_NONE;
  586. #endif
  587. }
  588. fdt_addr_t dev_get_addr_size_index(struct udevice *dev, int index,
  589. fdt_size_t *size)
  590. {
  591. #if CONFIG_IS_ENABLED(OF_CONTROL)
  592. /*
  593. * Only get the size in this first call. We'll get the addr in the
  594. * next call to the exisiting dev_get_xxx function which handles
  595. * all config options.
  596. */
  597. fdtdec_get_addr_size_auto_noparent(gd->fdt_blob, dev_of_offset(dev),
  598. "reg", index, size, false);
  599. /*
  600. * Get the base address via the existing function which handles
  601. * all Kconfig cases
  602. */
  603. return dev_get_addr_index(dev, index);
  604. #else
  605. return FDT_ADDR_T_NONE;
  606. #endif
  607. }
  608. fdt_addr_t dev_get_addr_name(struct udevice *dev, const char *name)
  609. {
  610. #if CONFIG_IS_ENABLED(OF_CONTROL)
  611. int index;
  612. index = fdt_stringlist_search(gd->fdt_blob, dev_of_offset(dev),
  613. "reg-names", name);
  614. if (index < 0)
  615. return index;
  616. return dev_get_addr_index(dev, index);
  617. #else
  618. return FDT_ADDR_T_NONE;
  619. #endif
  620. }
  621. fdt_addr_t dev_get_addr(struct udevice *dev)
  622. {
  623. return dev_get_addr_index(dev, 0);
  624. }
  625. void *dev_get_addr_ptr(struct udevice *dev)
  626. {
  627. return (void *)(uintptr_t)dev_get_addr_index(dev, 0);
  628. }
  629. void *dev_map_physmem(struct udevice *dev, unsigned long size)
  630. {
  631. fdt_addr_t addr = dev_get_addr(dev);
  632. if (addr == FDT_ADDR_T_NONE)
  633. return NULL;
  634. return map_physmem(addr, size, MAP_NOCACHE);
  635. }
  636. bool device_has_children(struct udevice *dev)
  637. {
  638. return !list_empty(&dev->child_head);
  639. }
  640. bool device_has_active_children(struct udevice *dev)
  641. {
  642. struct udevice *child;
  643. for (device_find_first_child(dev, &child);
  644. child;
  645. device_find_next_child(&child)) {
  646. if (device_active(child))
  647. return true;
  648. }
  649. return false;
  650. }
  651. bool device_is_last_sibling(struct udevice *dev)
  652. {
  653. struct udevice *parent = dev->parent;
  654. if (!parent)
  655. return false;
  656. return list_is_last(&dev->sibling_node, &parent->child_head);
  657. }
  658. void device_set_name_alloced(struct udevice *dev)
  659. {
  660. dev->flags |= DM_FLAG_NAME_ALLOCED;
  661. }
  662. int device_set_name(struct udevice *dev, const char *name)
  663. {
  664. name = strdup(name);
  665. if (!name)
  666. return -ENOMEM;
  667. dev->name = name;
  668. device_set_name_alloced(dev);
  669. return 0;
  670. }
  671. bool of_device_is_compatible(struct udevice *dev, const char *compat)
  672. {
  673. const void *fdt = gd->fdt_blob;
  674. return !fdt_node_check_compatible(fdt, dev_of_offset(dev), compat);
  675. }
  676. bool of_machine_is_compatible(const char *compat)
  677. {
  678. const void *fdt = gd->fdt_blob;
  679. return !fdt_node_check_compatible(fdt, 0, compat);
  680. }