device.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  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. /*
  232. * Ensure that the zero bytes are flushed to memory.
  233. * This prevents problems if the driver uses this as
  234. * both an input and an output buffer:
  235. *
  236. * 1. Zeroes written to buffer (here) and sit in the
  237. * cache
  238. * 2. Driver issues a read command to DMA
  239. * 3. CPU runs out of cache space and evicts some cache
  240. * data in the buffer, writing zeroes to RAM from
  241. * the memset() above
  242. * 4. DMA completes
  243. * 5. Buffer now has some DMA data and some zeroes
  244. * 6. Data being read is now incorrect
  245. *
  246. * To prevent this, ensure that the cache is clean
  247. * within this range at the start. The driver can then
  248. * use normal flush-after-write, invalidate-before-read
  249. * procedures.
  250. *
  251. * TODO(sjg@chromium.org): Drop this microblaze
  252. * exception.
  253. */
  254. #ifndef CONFIG_MICROBLAZE
  255. flush_dcache_range((ulong)priv, (ulong)priv + size);
  256. #endif
  257. }
  258. } else {
  259. priv = calloc(1, size);
  260. }
  261. return priv;
  262. }
  263. int device_probe(struct udevice *dev)
  264. {
  265. const struct driver *drv;
  266. int size = 0;
  267. int ret;
  268. int seq;
  269. if (!dev)
  270. return -EINVAL;
  271. if (dev->flags & DM_FLAG_ACTIVATED)
  272. return 0;
  273. drv = dev->driver;
  274. assert(drv);
  275. /* Allocate private data if requested and not reentered */
  276. if (drv->priv_auto_alloc_size && !dev->priv) {
  277. dev->priv = alloc_priv(drv->priv_auto_alloc_size, drv->flags);
  278. if (!dev->priv) {
  279. ret = -ENOMEM;
  280. goto fail;
  281. }
  282. }
  283. /* Allocate private data if requested and not reentered */
  284. size = dev->uclass->uc_drv->per_device_auto_alloc_size;
  285. if (size && !dev->uclass_priv) {
  286. dev->uclass_priv = calloc(1, size);
  287. if (!dev->uclass_priv) {
  288. ret = -ENOMEM;
  289. goto fail;
  290. }
  291. }
  292. /* Ensure all parents are probed */
  293. if (dev->parent) {
  294. size = dev->parent->driver->per_child_auto_alloc_size;
  295. if (!size) {
  296. size = dev->parent->uclass->uc_drv->
  297. per_child_auto_alloc_size;
  298. }
  299. if (size && !dev->parent_priv) {
  300. dev->parent_priv = alloc_priv(size, drv->flags);
  301. if (!dev->parent_priv) {
  302. ret = -ENOMEM;
  303. goto fail;
  304. }
  305. }
  306. ret = device_probe(dev->parent);
  307. if (ret)
  308. goto fail;
  309. /*
  310. * The device might have already been probed during
  311. * the call to device_probe() on its parent device
  312. * (e.g. PCI bridge devices). Test the flags again
  313. * so that we don't mess up the device.
  314. */
  315. if (dev->flags & DM_FLAG_ACTIVATED)
  316. return 0;
  317. }
  318. seq = uclass_resolve_seq(dev);
  319. if (seq < 0) {
  320. ret = seq;
  321. goto fail;
  322. }
  323. dev->seq = seq;
  324. dev->flags |= DM_FLAG_ACTIVATED;
  325. /*
  326. * Process pinctrl for everything except the root device, and
  327. * continue regardless of the result of pinctrl. Don't process pinctrl
  328. * settings for pinctrl devices since the device may not yet be
  329. * probed.
  330. */
  331. if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
  332. pinctrl_select_state(dev, "default");
  333. ret = uclass_pre_probe_device(dev);
  334. if (ret)
  335. goto fail;
  336. if (dev->parent && dev->parent->driver->child_pre_probe) {
  337. ret = dev->parent->driver->child_pre_probe(dev);
  338. if (ret)
  339. goto fail;
  340. }
  341. if (drv->ofdata_to_platdata && dev_of_offset(dev) >= 0) {
  342. ret = drv->ofdata_to_platdata(dev);
  343. if (ret)
  344. goto fail;
  345. }
  346. if (drv->probe) {
  347. ret = drv->probe(dev);
  348. if (ret) {
  349. dev->flags &= ~DM_FLAG_ACTIVATED;
  350. goto fail;
  351. }
  352. }
  353. ret = uclass_post_probe_device(dev);
  354. if (ret)
  355. goto fail_uclass;
  356. if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
  357. pinctrl_select_state(dev, "default");
  358. return 0;
  359. fail_uclass:
  360. if (device_remove(dev, DM_REMOVE_NORMAL)) {
  361. dm_warn("%s: Device '%s' failed to remove on error path\n",
  362. __func__, dev->name);
  363. }
  364. fail:
  365. dev->flags &= ~DM_FLAG_ACTIVATED;
  366. dev->seq = -1;
  367. device_free(dev);
  368. return ret;
  369. }
  370. void *dev_get_platdata(struct udevice *dev)
  371. {
  372. if (!dev) {
  373. dm_warn("%s: null device\n", __func__);
  374. return NULL;
  375. }
  376. return dev->platdata;
  377. }
  378. void *dev_get_parent_platdata(struct udevice *dev)
  379. {
  380. if (!dev) {
  381. dm_warn("%s: null device\n", __func__);
  382. return NULL;
  383. }
  384. return dev->parent_platdata;
  385. }
  386. void *dev_get_uclass_platdata(struct udevice *dev)
  387. {
  388. if (!dev) {
  389. dm_warn("%s: null device\n", __func__);
  390. return NULL;
  391. }
  392. return dev->uclass_platdata;
  393. }
  394. void *dev_get_priv(struct udevice *dev)
  395. {
  396. if (!dev) {
  397. dm_warn("%s: null device\n", __func__);
  398. return NULL;
  399. }
  400. return dev->priv;
  401. }
  402. void *dev_get_uclass_priv(struct udevice *dev)
  403. {
  404. if (!dev) {
  405. dm_warn("%s: null device\n", __func__);
  406. return NULL;
  407. }
  408. return dev->uclass_priv;
  409. }
  410. void *dev_get_parent_priv(struct udevice *dev)
  411. {
  412. if (!dev) {
  413. dm_warn("%s: null device\n", __func__);
  414. return NULL;
  415. }
  416. return dev->parent_priv;
  417. }
  418. static int device_get_device_tail(struct udevice *dev, int ret,
  419. struct udevice **devp)
  420. {
  421. if (ret)
  422. return ret;
  423. ret = device_probe(dev);
  424. if (ret)
  425. return ret;
  426. *devp = dev;
  427. return 0;
  428. }
  429. int device_get_child(struct udevice *parent, int index, struct udevice **devp)
  430. {
  431. struct udevice *dev;
  432. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  433. if (!index--)
  434. return device_get_device_tail(dev, 0, devp);
  435. }
  436. return -ENODEV;
  437. }
  438. int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
  439. bool find_req_seq, struct udevice **devp)
  440. {
  441. struct udevice *dev;
  442. *devp = NULL;
  443. if (seq_or_req_seq == -1)
  444. return -ENODEV;
  445. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  446. if ((find_req_seq ? dev->req_seq : dev->seq) ==
  447. seq_or_req_seq) {
  448. *devp = dev;
  449. return 0;
  450. }
  451. }
  452. return -ENODEV;
  453. }
  454. int device_get_child_by_seq(struct udevice *parent, int seq,
  455. struct udevice **devp)
  456. {
  457. struct udevice *dev;
  458. int ret;
  459. *devp = NULL;
  460. ret = device_find_child_by_seq(parent, seq, false, &dev);
  461. if (ret == -ENODEV) {
  462. /*
  463. * We didn't find it in probed devices. See if there is one
  464. * that will request this seq if probed.
  465. */
  466. ret = device_find_child_by_seq(parent, seq, true, &dev);
  467. }
  468. return device_get_device_tail(dev, ret, devp);
  469. }
  470. int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
  471. struct udevice **devp)
  472. {
  473. struct udevice *dev;
  474. *devp = NULL;
  475. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  476. if (dev_of_offset(dev) == of_offset) {
  477. *devp = dev;
  478. return 0;
  479. }
  480. }
  481. return -ENODEV;
  482. }
  483. int device_get_child_by_of_offset(struct udevice *parent, int node,
  484. struct udevice **devp)
  485. {
  486. struct udevice *dev;
  487. int ret;
  488. *devp = NULL;
  489. ret = device_find_child_by_of_offset(parent, node, &dev);
  490. return device_get_device_tail(dev, ret, devp);
  491. }
  492. static struct udevice *_device_find_global_by_of_offset(struct udevice *parent,
  493. int of_offset)
  494. {
  495. struct udevice *dev, *found;
  496. if (dev_of_offset(parent) == of_offset)
  497. return parent;
  498. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  499. found = _device_find_global_by_of_offset(dev, of_offset);
  500. if (found)
  501. return found;
  502. }
  503. return NULL;
  504. }
  505. int device_get_global_by_of_offset(int of_offset, struct udevice **devp)
  506. {
  507. struct udevice *dev;
  508. dev = _device_find_global_by_of_offset(gd->dm_root, of_offset);
  509. return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
  510. }
  511. int device_find_first_child(struct udevice *parent, struct udevice **devp)
  512. {
  513. if (list_empty(&parent->child_head)) {
  514. *devp = NULL;
  515. } else {
  516. *devp = list_first_entry(&parent->child_head, struct udevice,
  517. sibling_node);
  518. }
  519. return 0;
  520. }
  521. int device_find_next_child(struct udevice **devp)
  522. {
  523. struct udevice *dev = *devp;
  524. struct udevice *parent = dev->parent;
  525. if (list_is_last(&dev->sibling_node, &parent->child_head)) {
  526. *devp = NULL;
  527. } else {
  528. *devp = list_entry(dev->sibling_node.next, struct udevice,
  529. sibling_node);
  530. }
  531. return 0;
  532. }
  533. struct udevice *dev_get_parent(struct udevice *child)
  534. {
  535. return child->parent;
  536. }
  537. ulong dev_get_driver_data(struct udevice *dev)
  538. {
  539. return dev->driver_data;
  540. }
  541. const void *dev_get_driver_ops(struct udevice *dev)
  542. {
  543. if (!dev || !dev->driver->ops)
  544. return NULL;
  545. return dev->driver->ops;
  546. }
  547. enum uclass_id device_get_uclass_id(struct udevice *dev)
  548. {
  549. return dev->uclass->uc_drv->id;
  550. }
  551. const char *dev_get_uclass_name(struct udevice *dev)
  552. {
  553. if (!dev)
  554. return NULL;
  555. return dev->uclass->uc_drv->name;
  556. }
  557. fdt_addr_t dev_get_addr_index(struct udevice *dev, int index)
  558. {
  559. #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
  560. fdt_addr_t addr;
  561. if (CONFIG_IS_ENABLED(OF_TRANSLATE)) {
  562. const fdt32_t *reg;
  563. int len = 0;
  564. int na, ns;
  565. na = fdt_address_cells(gd->fdt_blob,
  566. dev_of_offset(dev->parent));
  567. if (na < 1) {
  568. debug("bad #address-cells\n");
  569. return FDT_ADDR_T_NONE;
  570. }
  571. ns = fdt_size_cells(gd->fdt_blob, dev_of_offset(dev->parent));
  572. if (ns < 0) {
  573. debug("bad #size-cells\n");
  574. return FDT_ADDR_T_NONE;
  575. }
  576. reg = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "reg",
  577. &len);
  578. if (!reg || (len <= (index * sizeof(fdt32_t) * (na + ns)))) {
  579. debug("Req index out of range\n");
  580. return FDT_ADDR_T_NONE;
  581. }
  582. reg += index * (na + ns);
  583. /*
  584. * Use the full-fledged translate function for complex
  585. * bus setups.
  586. */
  587. addr = fdt_translate_address((void *)gd->fdt_blob,
  588. dev_of_offset(dev), reg);
  589. } else {
  590. /*
  591. * Use the "simple" translate function for less complex
  592. * bus setups.
  593. */
  594. addr = fdtdec_get_addr_size_auto_parent(gd->fdt_blob,
  595. dev_of_offset(dev->parent), dev_of_offset(dev),
  596. "reg", index, NULL, false);
  597. if (CONFIG_IS_ENABLED(SIMPLE_BUS) && addr != FDT_ADDR_T_NONE) {
  598. if (device_get_uclass_id(dev->parent) ==
  599. UCLASS_SIMPLE_BUS)
  600. addr = simple_bus_translate(dev->parent, addr);
  601. }
  602. }
  603. /*
  604. * Some platforms need a special address translation. Those
  605. * platforms (e.g. mvebu in SPL) can configure a translation
  606. * offset in the DM by calling dm_set_translation_offset() that
  607. * will get added to all addresses returned by dev_get_addr().
  608. */
  609. addr += dm_get_translation_offset();
  610. return addr;
  611. #else
  612. return FDT_ADDR_T_NONE;
  613. #endif
  614. }
  615. fdt_addr_t dev_get_addr_size_index(struct udevice *dev, int index,
  616. fdt_size_t *size)
  617. {
  618. #if CONFIG_IS_ENABLED(OF_CONTROL)
  619. /*
  620. * Only get the size in this first call. We'll get the addr in the
  621. * next call to the exisiting dev_get_xxx function which handles
  622. * all config options.
  623. */
  624. fdtdec_get_addr_size_auto_noparent(gd->fdt_blob, dev_of_offset(dev),
  625. "reg", index, size, false);
  626. /*
  627. * Get the base address via the existing function which handles
  628. * all Kconfig cases
  629. */
  630. return dev_get_addr_index(dev, index);
  631. #else
  632. return FDT_ADDR_T_NONE;
  633. #endif
  634. }
  635. fdt_addr_t dev_get_addr_name(struct udevice *dev, const char *name)
  636. {
  637. #if CONFIG_IS_ENABLED(OF_CONTROL)
  638. int index;
  639. index = fdt_stringlist_search(gd->fdt_blob, dev_of_offset(dev),
  640. "reg-names", name);
  641. if (index < 0)
  642. return index;
  643. return dev_get_addr_index(dev, index);
  644. #else
  645. return FDT_ADDR_T_NONE;
  646. #endif
  647. }
  648. fdt_addr_t dev_get_addr(struct udevice *dev)
  649. {
  650. return dev_get_addr_index(dev, 0);
  651. }
  652. void *dev_get_addr_ptr(struct udevice *dev)
  653. {
  654. return (void *)(uintptr_t)dev_get_addr_index(dev, 0);
  655. }
  656. void *dev_map_physmem(struct udevice *dev, unsigned long size)
  657. {
  658. fdt_addr_t addr = dev_get_addr(dev);
  659. if (addr == FDT_ADDR_T_NONE)
  660. return NULL;
  661. return map_physmem(addr, size, MAP_NOCACHE);
  662. }
  663. bool device_has_children(struct udevice *dev)
  664. {
  665. return !list_empty(&dev->child_head);
  666. }
  667. bool device_has_active_children(struct udevice *dev)
  668. {
  669. struct udevice *child;
  670. for (device_find_first_child(dev, &child);
  671. child;
  672. device_find_next_child(&child)) {
  673. if (device_active(child))
  674. return true;
  675. }
  676. return false;
  677. }
  678. bool device_is_last_sibling(struct udevice *dev)
  679. {
  680. struct udevice *parent = dev->parent;
  681. if (!parent)
  682. return false;
  683. return list_is_last(&dev->sibling_node, &parent->child_head);
  684. }
  685. void device_set_name_alloced(struct udevice *dev)
  686. {
  687. dev->flags |= DM_FLAG_NAME_ALLOCED;
  688. }
  689. int device_set_name(struct udevice *dev, const char *name)
  690. {
  691. name = strdup(name);
  692. if (!name)
  693. return -ENOMEM;
  694. dev->name = name;
  695. device_set_name_alloced(dev);
  696. return 0;
  697. }
  698. bool of_device_is_compatible(struct udevice *dev, const char *compat)
  699. {
  700. const void *fdt = gd->fdt_blob;
  701. return !fdt_node_check_compatible(fdt, dev_of_offset(dev), compat);
  702. }
  703. bool of_machine_is_compatible(const char *compat)
  704. {
  705. const void *fdt = gd->fdt_blob;
  706. return !fdt_node_check_compatible(fdt, 0, compat);
  707. }