device.c 21 KB

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