device.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  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 <event.h>
  13. #include <log.h>
  14. #include <asm/global_data.h>
  15. #include <asm/io.h>
  16. #include <clk.h>
  17. #include <fdtdec.h>
  18. #include <fdt_support.h>
  19. #include <malloc.h>
  20. #include <asm/cache.h>
  21. #include <dm/device.h>
  22. #include <dm/device-internal.h>
  23. #include <dm/lists.h>
  24. #include <dm/of_access.h>
  25. #include <dm/pinctrl.h>
  26. #include <dm/platdata.h>
  27. #include <dm/read.h>
  28. #include <dm/uclass.h>
  29. #include <dm/uclass-internal.h>
  30. #include <dm/util.h>
  31. #include <iommu.h>
  32. #include <linux/err.h>
  33. #include <linux/list.h>
  34. #include <power-domain.h>
  35. DECLARE_GLOBAL_DATA_PTR;
  36. static int device_bind_common(struct udevice *parent, const struct driver *drv,
  37. const char *name, void *plat,
  38. ulong driver_data, ofnode node,
  39. uint of_plat_size, struct udevice **devp)
  40. {
  41. struct udevice *dev;
  42. struct uclass *uc;
  43. int size, ret = 0;
  44. bool auto_seq = true;
  45. void *ptr;
  46. if (CONFIG_IS_ENABLED(OF_PLATDATA_NO_BIND))
  47. return -ENOSYS;
  48. if (devp)
  49. *devp = NULL;
  50. if (!name)
  51. return -EINVAL;
  52. ret = uclass_get(drv->id, &uc);
  53. if (ret) {
  54. debug("Missing uclass for driver %s\n", drv->name);
  55. return ret;
  56. }
  57. dev = calloc(1, sizeof(struct udevice));
  58. if (!dev)
  59. return -ENOMEM;
  60. INIT_LIST_HEAD(&dev->sibling_node);
  61. INIT_LIST_HEAD(&dev->child_head);
  62. INIT_LIST_HEAD(&dev->uclass_node);
  63. #if CONFIG_IS_ENABLED(DEVRES)
  64. INIT_LIST_HEAD(&dev->devres_head);
  65. #endif
  66. dev_set_plat(dev, plat);
  67. dev->driver_data = driver_data;
  68. dev->name = name;
  69. dev_set_ofnode(dev, node);
  70. dev->parent = parent;
  71. dev->driver = drv;
  72. dev->uclass = uc;
  73. dev->seq_ = -1;
  74. if (CONFIG_IS_ENABLED(DM_SEQ_ALIAS) &&
  75. (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS)) {
  76. /*
  77. * Some devices, such as a SPI bus, I2C bus and serial ports
  78. * are numbered using aliases.
  79. */
  80. if (CONFIG_IS_ENABLED(OF_CONTROL) &&
  81. !CONFIG_IS_ENABLED(OF_PLATDATA)) {
  82. if (uc->uc_drv->name && ofnode_valid(node)) {
  83. if (!dev_read_alias_seq(dev, &dev->seq_)) {
  84. auto_seq = false;
  85. log_debug(" - seq=%d\n", dev->seq_);
  86. }
  87. }
  88. }
  89. }
  90. if (auto_seq && !(uc->uc_drv->flags & DM_UC_FLAG_NO_AUTO_SEQ))
  91. dev->seq_ = uclass_find_next_free_seq(uc);
  92. /* Check if we need to allocate plat */
  93. if (drv->plat_auto) {
  94. bool alloc = !plat;
  95. /*
  96. * For of-platdata, we try use the existing data, but if
  97. * plat_auto is larger, we must allocate a new space
  98. */
  99. if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
  100. if (of_plat_size)
  101. dev_or_flags(dev, DM_FLAG_OF_PLATDATA);
  102. if (of_plat_size < drv->plat_auto)
  103. alloc = true;
  104. }
  105. if (alloc) {
  106. dev_or_flags(dev, DM_FLAG_ALLOC_PDATA);
  107. ptr = calloc(1, drv->plat_auto);
  108. if (!ptr) {
  109. ret = -ENOMEM;
  110. goto fail_alloc1;
  111. }
  112. /*
  113. * For of-platdata, copy the old plat into the new
  114. * space
  115. */
  116. if (CONFIG_IS_ENABLED(OF_PLATDATA) && plat)
  117. memcpy(ptr, plat, of_plat_size);
  118. dev_set_plat(dev, ptr);
  119. }
  120. }
  121. size = uc->uc_drv->per_device_plat_auto;
  122. if (size) {
  123. dev_or_flags(dev, DM_FLAG_ALLOC_UCLASS_PDATA);
  124. ptr = calloc(1, size);
  125. if (!ptr) {
  126. ret = -ENOMEM;
  127. goto fail_alloc2;
  128. }
  129. dev_set_uclass_plat(dev, ptr);
  130. }
  131. if (parent) {
  132. size = parent->driver->per_child_plat_auto;
  133. if (!size)
  134. size = parent->uclass->uc_drv->per_child_plat_auto;
  135. if (size) {
  136. dev_or_flags(dev, DM_FLAG_ALLOC_PARENT_PDATA);
  137. ptr = calloc(1, size);
  138. if (!ptr) {
  139. ret = -ENOMEM;
  140. goto fail_alloc3;
  141. }
  142. dev_set_parent_plat(dev, ptr);
  143. }
  144. /* put dev into parent's successor list */
  145. list_add_tail(&dev->sibling_node, &parent->child_head);
  146. }
  147. ret = uclass_bind_device(dev);
  148. if (ret)
  149. goto fail_uclass_bind;
  150. /* if we fail to bind we remove device from successors and free it */
  151. if (drv->bind) {
  152. ret = drv->bind(dev);
  153. if (ret)
  154. goto fail_bind;
  155. }
  156. if (parent && parent->driver->child_post_bind) {
  157. ret = parent->driver->child_post_bind(dev);
  158. if (ret)
  159. goto fail_child_post_bind;
  160. }
  161. if (uc->uc_drv->post_bind) {
  162. ret = uc->uc_drv->post_bind(dev);
  163. if (ret)
  164. goto fail_uclass_post_bind;
  165. }
  166. if (parent)
  167. pr_debug("Bound device %s to %s\n", dev->name, parent->name);
  168. if (devp)
  169. *devp = dev;
  170. dev_or_flags(dev, DM_FLAG_BOUND);
  171. return 0;
  172. fail_uclass_post_bind:
  173. /* There is no child unbind() method, so no clean-up required */
  174. fail_child_post_bind:
  175. if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
  176. if (drv->unbind && drv->unbind(dev)) {
  177. dm_warn("unbind() method failed on dev '%s' on error path\n",
  178. dev->name);
  179. }
  180. }
  181. fail_bind:
  182. if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
  183. if (uclass_unbind_device(dev)) {
  184. dm_warn("Failed to unbind dev '%s' on error path\n",
  185. dev->name);
  186. }
  187. }
  188. fail_uclass_bind:
  189. if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
  190. list_del(&dev->sibling_node);
  191. if (dev_get_flags(dev) & DM_FLAG_ALLOC_PARENT_PDATA) {
  192. free(dev_get_parent_plat(dev));
  193. dev_set_parent_plat(dev, NULL);
  194. }
  195. }
  196. fail_alloc3:
  197. if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
  198. if (dev_get_flags(dev) & DM_FLAG_ALLOC_UCLASS_PDATA) {
  199. free(dev_get_uclass_plat(dev));
  200. dev_set_uclass_plat(dev, NULL);
  201. }
  202. }
  203. fail_alloc2:
  204. if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
  205. if (dev_get_flags(dev) & DM_FLAG_ALLOC_PDATA) {
  206. free(dev_get_plat(dev));
  207. dev_set_plat(dev, NULL);
  208. }
  209. }
  210. fail_alloc1:
  211. devres_release_all(dev);
  212. free(dev);
  213. return ret;
  214. }
  215. int device_bind_with_driver_data(struct udevice *parent,
  216. const struct driver *drv, const char *name,
  217. ulong driver_data, ofnode node,
  218. struct udevice **devp)
  219. {
  220. return device_bind_common(parent, drv, name, NULL, driver_data, node,
  221. 0, devp);
  222. }
  223. int device_bind(struct udevice *parent, const struct driver *drv,
  224. const char *name, void *plat, ofnode node,
  225. struct udevice **devp)
  226. {
  227. return device_bind_common(parent, drv, name, plat, 0, node, 0,
  228. devp);
  229. }
  230. int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
  231. const struct driver_info *info, struct udevice **devp)
  232. {
  233. struct driver *drv;
  234. uint plat_size = 0;
  235. int ret;
  236. drv = lists_driver_lookup_name(info->name);
  237. if (!drv)
  238. return -ENOENT;
  239. if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
  240. return -EPERM;
  241. #if CONFIG_IS_ENABLED(OF_PLATDATA)
  242. plat_size = info->plat_size;
  243. #endif
  244. ret = device_bind_common(parent, drv, info->name, (void *)info->plat, 0,
  245. ofnode_null(), plat_size, devp);
  246. if (ret)
  247. return ret;
  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. device_foreach_child_safe(pos, n, dev->parent) {
  256. if (pos->driver != dev->driver)
  257. continue;
  258. list_del(&dev->sibling_node);
  259. list_add_tail(&dev->sibling_node, &new_parent->child_head);
  260. dev->parent = new_parent;
  261. break;
  262. }
  263. return 0;
  264. }
  265. static void *alloc_priv(int size, uint flags)
  266. {
  267. void *priv;
  268. if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
  269. size = ROUND(size, ARCH_DMA_MINALIGN);
  270. priv = memalign(ARCH_DMA_MINALIGN, size);
  271. if (priv) {
  272. memset(priv, '\0', size);
  273. /*
  274. * Ensure that the zero bytes are flushed to memory.
  275. * This prevents problems if the driver uses this as
  276. * both an input and an output buffer:
  277. *
  278. * 1. Zeroes written to buffer (here) and sit in the
  279. * cache
  280. * 2. Driver issues a read command to DMA
  281. * 3. CPU runs out of cache space and evicts some cache
  282. * data in the buffer, writing zeroes to RAM from
  283. * the memset() above
  284. * 4. DMA completes
  285. * 5. Buffer now has some DMA data and some zeroes
  286. * 6. Data being read is now incorrect
  287. *
  288. * To prevent this, ensure that the cache is clean
  289. * within this range at the start. The driver can then
  290. * use normal flush-after-write, invalidate-before-read
  291. * procedures.
  292. */
  293. flush_dcache_range((ulong)priv, (ulong)priv + size);
  294. }
  295. } else {
  296. priv = calloc(1, size);
  297. }
  298. return priv;
  299. }
  300. /**
  301. * device_alloc_priv() - Allocate priv/plat data required by the device
  302. *
  303. * @dev: Device to process
  304. * Return: 0 if OK, -ENOMEM if out of memory
  305. */
  306. static int device_alloc_priv(struct udevice *dev)
  307. {
  308. const struct driver *drv;
  309. void *ptr;
  310. int size;
  311. drv = dev->driver;
  312. assert(drv);
  313. /* Allocate private data if requested and not reentered */
  314. if (drv->priv_auto && !dev_get_priv(dev)) {
  315. ptr = alloc_priv(drv->priv_auto, drv->flags);
  316. if (!ptr)
  317. return -ENOMEM;
  318. dev_set_priv(dev, ptr);
  319. }
  320. /* Allocate private data if requested and not reentered */
  321. size = dev->uclass->uc_drv->per_device_auto;
  322. if (size && !dev_get_uclass_priv(dev)) {
  323. ptr = alloc_priv(size, dev->uclass->uc_drv->flags);
  324. if (!ptr)
  325. return -ENOMEM;
  326. dev_set_uclass_priv(dev, ptr);
  327. }
  328. /* Allocate parent data for this child */
  329. if (dev->parent) {
  330. size = dev->parent->driver->per_child_auto;
  331. if (!size)
  332. size = dev->parent->uclass->uc_drv->per_child_auto;
  333. if (size && !dev_get_parent_priv(dev)) {
  334. ptr = alloc_priv(size, drv->flags);
  335. if (!ptr)
  336. return -ENOMEM;
  337. dev_set_parent_priv(dev, ptr);
  338. }
  339. }
  340. return 0;
  341. }
  342. int device_of_to_plat(struct udevice *dev)
  343. {
  344. const struct driver *drv;
  345. int ret;
  346. if (!dev)
  347. return -EINVAL;
  348. if (dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID)
  349. return 0;
  350. /*
  351. * This is not needed if binding is disabled, since data is allocated
  352. * at build time.
  353. */
  354. if (!CONFIG_IS_ENABLED(OF_PLATDATA_NO_BIND)) {
  355. /* Ensure all parents have ofdata */
  356. if (dev->parent) {
  357. ret = device_of_to_plat(dev->parent);
  358. if (ret)
  359. goto fail;
  360. /*
  361. * The device might have already been probed during
  362. * the call to device_probe() on its parent device
  363. * (e.g. PCI bridge devices). Test the flags again
  364. * so that we don't mess up the device.
  365. */
  366. if (dev_get_flags(dev) & DM_FLAG_PLATDATA_VALID)
  367. return 0;
  368. }
  369. ret = device_alloc_priv(dev);
  370. if (ret)
  371. goto fail;
  372. }
  373. drv = dev->driver;
  374. assert(drv);
  375. if (drv->of_to_plat &&
  376. (CONFIG_IS_ENABLED(OF_PLATDATA) || dev_has_ofnode(dev))) {
  377. ret = drv->of_to_plat(dev);
  378. if (ret)
  379. goto fail;
  380. }
  381. dev_or_flags(dev, DM_FLAG_PLATDATA_VALID);
  382. return 0;
  383. fail:
  384. device_free(dev);
  385. return ret;
  386. }
  387. /**
  388. * device_get_dma_constraints() - Populate device's DMA constraints
  389. *
  390. * Gets a device's DMA constraints from firmware. This information is later
  391. * used by drivers to translate physcal addresses to the device's bus address
  392. * space. For now only device-tree is supported.
  393. *
  394. * @dev: Pointer to target device
  395. * Return: 0 if OK or if no DMA constraints were found, error otherwise
  396. */
  397. static int device_get_dma_constraints(struct udevice *dev)
  398. {
  399. struct udevice *parent = dev->parent;
  400. phys_addr_t cpu = 0;
  401. dma_addr_t bus = 0;
  402. u64 size = 0;
  403. int ret;
  404. if (!CONFIG_IS_ENABLED(DM_DMA) || !parent || !dev_has_ofnode(parent))
  405. return 0;
  406. /*
  407. * We start parsing for dma-ranges from the device's bus node. This is
  408. * specially important on nested buses.
  409. */
  410. ret = dev_get_dma_range(parent, &cpu, &bus, &size);
  411. /* Don't return an error if no 'dma-ranges' were found */
  412. if (ret && ret != -ENOENT) {
  413. dm_warn("%s: failed to get DMA range, %d\n", dev->name, ret);
  414. return ret;
  415. }
  416. dev_set_dma_offset(dev, cpu - bus);
  417. return 0;
  418. }
  419. int device_probe(struct udevice *dev)
  420. {
  421. const struct driver *drv;
  422. int ret;
  423. if (!dev)
  424. return -EINVAL;
  425. if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
  426. return 0;
  427. ret = device_notify(dev, EVT_DM_PRE_PROBE);
  428. if (ret)
  429. return ret;
  430. drv = dev->driver;
  431. assert(drv);
  432. ret = device_of_to_plat(dev);
  433. if (ret)
  434. goto fail;
  435. /* Ensure all parents are probed */
  436. if (dev->parent) {
  437. ret = device_probe(dev->parent);
  438. if (ret)
  439. goto fail;
  440. /*
  441. * The device might have already been probed during
  442. * the call to device_probe() on its parent device
  443. * (e.g. PCI bridge devices). Test the flags again
  444. * so that we don't mess up the device.
  445. */
  446. if (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
  447. return 0;
  448. }
  449. dev_or_flags(dev, DM_FLAG_ACTIVATED);
  450. if (CONFIG_IS_ENABLED(POWER_DOMAIN) && dev->parent &&
  451. (device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) &&
  452. !(drv->flags & DM_FLAG_DEFAULT_PD_CTRL_OFF)) {
  453. ret = dev_power_domain_on(dev);
  454. if (ret)
  455. goto fail;
  456. }
  457. /*
  458. * Process pinctrl for everything except the root device, and
  459. * continue regardless of the result of pinctrl. Don't process pinctrl
  460. * settings for pinctrl devices since the device may not yet be
  461. * probed.
  462. *
  463. * This call can produce some non-intuitive results. For example, on an
  464. * x86 device where dev is the main PCI bus, the pinctrl device may be
  465. * child or grandchild of that bus, meaning that the child will be
  466. * probed here. If the child happens to be the P2SB and the pinctrl
  467. * device is a child of that, then both the pinctrl and P2SB will be
  468. * probed by this call. This works because the DM_FLAG_ACTIVATED flag
  469. * is set just above. However, the PCI bus' probe() method and
  470. * associated uclass methods have not yet been called.
  471. */
  472. if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL) {
  473. ret = pinctrl_select_state(dev, "default");
  474. if (ret && ret != -ENOSYS)
  475. log_debug("Device '%s' failed to configure default pinctrl: %d (%s)\n",
  476. dev->name, ret, errno_str(ret));
  477. }
  478. if (CONFIG_IS_ENABLED(IOMMU) && dev->parent &&
  479. (device_get_uclass_id(dev) != UCLASS_IOMMU)) {
  480. ret = dev_iommu_enable(dev);
  481. if (ret)
  482. goto fail;
  483. }
  484. ret = device_get_dma_constraints(dev);
  485. if (ret)
  486. goto fail;
  487. ret = uclass_pre_probe_device(dev);
  488. if (ret)
  489. goto fail;
  490. if (dev->parent && dev->parent->driver->child_pre_probe) {
  491. ret = dev->parent->driver->child_pre_probe(dev);
  492. if (ret)
  493. goto fail;
  494. }
  495. /* Only handle devices that have a valid ofnode */
  496. if (dev_has_ofnode(dev)) {
  497. /*
  498. * Process 'assigned-{clocks/clock-parents/clock-rates}'
  499. * properties
  500. */
  501. ret = clk_set_defaults(dev, CLK_DEFAULTS_PRE);
  502. if (ret)
  503. goto fail;
  504. }
  505. if (drv->probe) {
  506. ret = drv->probe(dev);
  507. if (ret)
  508. goto fail;
  509. }
  510. ret = uclass_post_probe_device(dev);
  511. if (ret)
  512. goto fail_uclass;
  513. if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL) {
  514. ret = pinctrl_select_state(dev, "default");
  515. if (ret && ret != -ENOSYS)
  516. log_debug("Device '%s' failed to configure default pinctrl: %d (%s)\n",
  517. dev->name, ret, errno_str(ret));
  518. }
  519. ret = device_notify(dev, EVT_DM_POST_PROBE);
  520. if (ret)
  521. return ret;
  522. return 0;
  523. fail_uclass:
  524. if (device_remove(dev, DM_REMOVE_NORMAL)) {
  525. dm_warn("%s: Device '%s' failed to remove on error path\n",
  526. __func__, dev->name);
  527. }
  528. fail:
  529. dev_bic_flags(dev, DM_FLAG_ACTIVATED);
  530. device_free(dev);
  531. return ret;
  532. }
  533. void *dev_get_plat(const struct udevice *dev)
  534. {
  535. if (!dev) {
  536. dm_warn("%s: null device\n", __func__);
  537. return NULL;
  538. }
  539. return dm_priv_to_rw(dev->plat_);
  540. }
  541. void *dev_get_parent_plat(const struct udevice *dev)
  542. {
  543. if (!dev) {
  544. dm_warn("%s: null device\n", __func__);
  545. return NULL;
  546. }
  547. return dm_priv_to_rw(dev->parent_plat_);
  548. }
  549. void *dev_get_uclass_plat(const struct udevice *dev)
  550. {
  551. if (!dev) {
  552. dm_warn("%s: null device\n", __func__);
  553. return NULL;
  554. }
  555. return dm_priv_to_rw(dev->uclass_plat_);
  556. }
  557. void *dev_get_priv(const struct udevice *dev)
  558. {
  559. if (!dev) {
  560. dm_warn("%s: null device\n", __func__);
  561. return NULL;
  562. }
  563. return dm_priv_to_rw(dev->priv_);
  564. }
  565. /* notrace is needed as this is called by timer_get_rate() */
  566. notrace void *dev_get_uclass_priv(const struct udevice *dev)
  567. {
  568. if (!dev) {
  569. dm_warn("%s: null device\n", __func__);
  570. return NULL;
  571. }
  572. return dm_priv_to_rw(dev->uclass_priv_);
  573. }
  574. void *dev_get_parent_priv(const struct udevice *dev)
  575. {
  576. if (!dev) {
  577. dm_warn("%s: null device\n", __func__);
  578. return NULL;
  579. }
  580. return dm_priv_to_rw(dev->parent_priv_);
  581. }
  582. void *dev_get_attach_ptr(const struct udevice *dev, enum dm_tag_t tag)
  583. {
  584. switch (tag) {
  585. case DM_TAG_PLAT:
  586. return dev_get_plat(dev);
  587. case DM_TAG_PARENT_PLAT:
  588. return dev_get_parent_plat(dev);
  589. case DM_TAG_UC_PLAT:
  590. return dev_get_uclass_plat(dev);
  591. case DM_TAG_PRIV:
  592. return dev_get_priv(dev);
  593. case DM_TAG_PARENT_PRIV:
  594. return dev_get_parent_priv(dev);
  595. case DM_TAG_UC_PRIV:
  596. return dev_get_uclass_priv(dev);
  597. default:
  598. return NULL;
  599. }
  600. }
  601. int dev_get_attach_size(const struct udevice *dev, enum dm_tag_t tag)
  602. {
  603. const struct udevice *parent = dev_get_parent(dev);
  604. const struct uclass *uc = dev->uclass;
  605. const struct uclass_driver *uc_drv = uc->uc_drv;
  606. const struct driver *parent_drv = NULL;
  607. int size = 0;
  608. if (parent)
  609. parent_drv = parent->driver;
  610. switch (tag) {
  611. case DM_TAG_PLAT:
  612. size = dev->driver->plat_auto;
  613. break;
  614. case DM_TAG_PARENT_PLAT:
  615. if (parent) {
  616. size = parent_drv->per_child_plat_auto;
  617. if (!size)
  618. size = parent->uclass->uc_drv->per_child_plat_auto;
  619. }
  620. break;
  621. case DM_TAG_UC_PLAT:
  622. size = uc_drv->per_device_plat_auto;
  623. break;
  624. case DM_TAG_PRIV:
  625. size = dev->driver->priv_auto;
  626. break;
  627. case DM_TAG_PARENT_PRIV:
  628. if (parent) {
  629. size = parent_drv->per_child_auto;
  630. if (!size)
  631. size = parent->uclass->uc_drv->per_child_auto;
  632. }
  633. break;
  634. case DM_TAG_UC_PRIV:
  635. size = uc_drv->per_device_auto;
  636. break;
  637. default:
  638. break;
  639. }
  640. return size;
  641. }
  642. static int device_get_device_tail(struct udevice *dev, int ret,
  643. struct udevice **devp)
  644. {
  645. if (ret)
  646. return ret;
  647. ret = device_probe(dev);
  648. if (ret)
  649. return ret;
  650. *devp = dev;
  651. return 0;
  652. }
  653. #if CONFIG_IS_ENABLED(OF_REAL)
  654. /**
  655. * device_find_by_ofnode() - Return device associated with given ofnode
  656. *
  657. * The returned device is *not* activated.
  658. *
  659. * @node: The ofnode for which a associated device should be looked up
  660. * @devp: Pointer to structure to hold the found device
  661. * Return: 0 if OK, -ve on error
  662. */
  663. static int device_find_by_ofnode(ofnode node, struct udevice **devp)
  664. {
  665. struct uclass *uc;
  666. struct udevice *dev;
  667. int ret;
  668. list_for_each_entry(uc, gd->uclass_root, sibling_node) {
  669. ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node,
  670. &dev);
  671. if (!ret || dev) {
  672. *devp = dev;
  673. return 0;
  674. }
  675. }
  676. return -ENODEV;
  677. }
  678. #endif
  679. int device_get_child(const struct udevice *parent, int index,
  680. struct udevice **devp)
  681. {
  682. struct udevice *dev;
  683. device_foreach_child(dev, parent) {
  684. if (!index--)
  685. return device_get_device_tail(dev, 0, devp);
  686. }
  687. return -ENODEV;
  688. }
  689. int device_get_child_count(const struct udevice *parent)
  690. {
  691. struct udevice *dev;
  692. int count = 0;
  693. device_foreach_child(dev, parent)
  694. count++;
  695. return count;
  696. }
  697. int device_get_decendent_count(const struct udevice *parent)
  698. {
  699. const struct udevice *dev;
  700. int count = 1;
  701. device_foreach_child(dev, parent)
  702. count += device_get_decendent_count(dev);
  703. return count;
  704. }
  705. int device_find_child_by_seq(const struct udevice *parent, int seq,
  706. struct udevice **devp)
  707. {
  708. struct udevice *dev;
  709. *devp = NULL;
  710. device_foreach_child(dev, parent) {
  711. if (dev->seq_ == seq) {
  712. *devp = dev;
  713. return 0;
  714. }
  715. }
  716. return -ENODEV;
  717. }
  718. int device_get_child_by_seq(const struct udevice *parent, int seq,
  719. struct udevice **devp)
  720. {
  721. struct udevice *dev;
  722. int ret;
  723. *devp = NULL;
  724. ret = device_find_child_by_seq(parent, seq, &dev);
  725. return device_get_device_tail(dev, ret, devp);
  726. }
  727. int device_find_child_by_of_offset(const struct udevice *parent, int of_offset,
  728. struct udevice **devp)
  729. {
  730. struct udevice *dev;
  731. *devp = NULL;
  732. device_foreach_child(dev, parent) {
  733. if (dev_of_offset(dev) == of_offset) {
  734. *devp = dev;
  735. return 0;
  736. }
  737. }
  738. return -ENODEV;
  739. }
  740. int device_get_child_by_of_offset(const struct udevice *parent, int node,
  741. struct udevice **devp)
  742. {
  743. struct udevice *dev;
  744. int ret;
  745. *devp = NULL;
  746. ret = device_find_child_by_of_offset(parent, node, &dev);
  747. return device_get_device_tail(dev, ret, devp);
  748. }
  749. static struct udevice *_device_find_global_by_ofnode(struct udevice *parent,
  750. ofnode ofnode)
  751. {
  752. struct udevice *dev, *found;
  753. if (ofnode_equal(dev_ofnode(parent), ofnode))
  754. return parent;
  755. device_foreach_child(dev, parent) {
  756. found = _device_find_global_by_ofnode(dev, ofnode);
  757. if (found)
  758. return found;
  759. }
  760. return NULL;
  761. }
  762. int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp)
  763. {
  764. *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode);
  765. return *devp ? 0 : -ENOENT;
  766. }
  767. int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
  768. {
  769. struct udevice *dev;
  770. dev = _device_find_global_by_ofnode(gd->dm_root, ofnode);
  771. return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
  772. }
  773. #if CONFIG_IS_ENABLED(OF_PLATDATA)
  774. int device_get_by_ofplat_idx(uint idx, struct udevice **devp)
  775. {
  776. struct udevice *dev;
  777. if (CONFIG_IS_ENABLED(OF_PLATDATA_INST)) {
  778. struct udevice *base = ll_entry_start(struct udevice, udevice);
  779. dev = base + idx;
  780. } else {
  781. struct driver_rt *drt = gd_dm_driver_rt() + idx;
  782. dev = drt->dev;
  783. }
  784. *devp = NULL;
  785. return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
  786. }
  787. #endif
  788. int device_find_first_child(const struct udevice *parent, struct udevice **devp)
  789. {
  790. if (list_empty(&parent->child_head)) {
  791. *devp = NULL;
  792. } else {
  793. *devp = list_first_entry(&parent->child_head, struct udevice,
  794. sibling_node);
  795. }
  796. return 0;
  797. }
  798. int device_find_next_child(struct udevice **devp)
  799. {
  800. struct udevice *dev = *devp;
  801. struct udevice *parent = dev->parent;
  802. if (list_is_last(&dev->sibling_node, &parent->child_head)) {
  803. *devp = NULL;
  804. } else {
  805. *devp = list_entry(dev->sibling_node.next, struct udevice,
  806. sibling_node);
  807. }
  808. return 0;
  809. }
  810. int device_find_first_inactive_child(const struct udevice *parent,
  811. enum uclass_id uclass_id,
  812. struct udevice **devp)
  813. {
  814. struct udevice *dev;
  815. *devp = NULL;
  816. device_foreach_child(dev, parent) {
  817. if (!device_active(dev) &&
  818. device_get_uclass_id(dev) == uclass_id) {
  819. *devp = dev;
  820. return 0;
  821. }
  822. }
  823. return -ENODEV;
  824. }
  825. int device_find_first_child_by_uclass(const struct udevice *parent,
  826. enum uclass_id uclass_id,
  827. struct udevice **devp)
  828. {
  829. struct udevice *dev;
  830. *devp = NULL;
  831. device_foreach_child(dev, parent) {
  832. if (device_get_uclass_id(dev) == uclass_id) {
  833. *devp = dev;
  834. return 0;
  835. }
  836. }
  837. return -ENODEV;
  838. }
  839. int device_find_child_by_namelen(const struct udevice *parent, const char *name,
  840. int len, struct udevice **devp)
  841. {
  842. struct udevice *dev;
  843. *devp = NULL;
  844. device_foreach_child(dev, parent) {
  845. if (!strncmp(dev->name, name, len) &&
  846. strlen(dev->name) == len) {
  847. *devp = dev;
  848. return 0;
  849. }
  850. }
  851. return -ENODEV;
  852. }
  853. int device_find_child_by_name(const struct udevice *parent, const char *name,
  854. struct udevice **devp)
  855. {
  856. return device_find_child_by_namelen(parent, name, strlen(name), devp);
  857. }
  858. int device_first_child_err(struct udevice *parent, struct udevice **devp)
  859. {
  860. struct udevice *dev;
  861. device_find_first_child(parent, &dev);
  862. if (!dev)
  863. return -ENODEV;
  864. return device_get_device_tail(dev, 0, devp);
  865. }
  866. int device_next_child_err(struct udevice **devp)
  867. {
  868. struct udevice *dev = *devp;
  869. device_find_next_child(&dev);
  870. if (!dev)
  871. return -ENODEV;
  872. return device_get_device_tail(dev, 0, devp);
  873. }
  874. int device_first_child_ofdata_err(struct udevice *parent, struct udevice **devp)
  875. {
  876. struct udevice *dev;
  877. int ret;
  878. device_find_first_child(parent, &dev);
  879. if (!dev)
  880. return -ENODEV;
  881. ret = device_of_to_plat(dev);
  882. if (ret)
  883. return ret;
  884. *devp = dev;
  885. return 0;
  886. }
  887. int device_next_child_ofdata_err(struct udevice **devp)
  888. {
  889. struct udevice *dev = *devp;
  890. int ret;
  891. device_find_next_child(&dev);
  892. if (!dev)
  893. return -ENODEV;
  894. ret = device_of_to_plat(dev);
  895. if (ret)
  896. return ret;
  897. *devp = dev;
  898. return 0;
  899. }
  900. struct udevice *dev_get_parent(const struct udevice *child)
  901. {
  902. return child->parent;
  903. }
  904. ulong dev_get_driver_data(const struct udevice *dev)
  905. {
  906. return dev->driver_data;
  907. }
  908. const void *dev_get_driver_ops(const struct udevice *dev)
  909. {
  910. if (!dev || !dev->driver->ops)
  911. return NULL;
  912. return dev->driver->ops;
  913. }
  914. enum uclass_id device_get_uclass_id(const struct udevice *dev)
  915. {
  916. return dev->uclass->uc_drv->id;
  917. }
  918. const char *dev_get_uclass_name(const struct udevice *dev)
  919. {
  920. if (!dev)
  921. return NULL;
  922. return dev->uclass->uc_drv->name;
  923. }
  924. bool device_has_children(const struct udevice *dev)
  925. {
  926. return !list_empty(&dev->child_head);
  927. }
  928. bool device_has_active_children(const struct udevice *dev)
  929. {
  930. struct udevice *child;
  931. for (device_find_first_child(dev, &child);
  932. child;
  933. device_find_next_child(&child)) {
  934. if (device_active(child))
  935. return true;
  936. }
  937. return false;
  938. }
  939. bool device_is_last_sibling(const struct udevice *dev)
  940. {
  941. struct udevice *parent = dev->parent;
  942. if (!parent)
  943. return false;
  944. return list_is_last(&dev->sibling_node, &parent->child_head);
  945. }
  946. void device_set_name_alloced(struct udevice *dev)
  947. {
  948. dev_or_flags(dev, DM_FLAG_NAME_ALLOCED);
  949. }
  950. int device_set_name(struct udevice *dev, const char *name)
  951. {
  952. name = strdup(name);
  953. if (!name)
  954. return -ENOMEM;
  955. dev->name = name;
  956. device_set_name_alloced(dev);
  957. return 0;
  958. }
  959. void dev_set_priv(struct udevice *dev, void *priv)
  960. {
  961. dev->priv_ = priv;
  962. }
  963. void dev_set_parent_priv(struct udevice *dev, void *parent_priv)
  964. {
  965. dev->parent_priv_ = parent_priv;
  966. }
  967. void dev_set_uclass_priv(struct udevice *dev, void *uclass_priv)
  968. {
  969. dev->uclass_priv_ = uclass_priv;
  970. }
  971. void dev_set_plat(struct udevice *dev, void *plat)
  972. {
  973. dev->plat_ = plat;
  974. }
  975. void dev_set_parent_plat(struct udevice *dev, void *parent_plat)
  976. {
  977. dev->parent_plat_ = parent_plat;
  978. }
  979. void dev_set_uclass_plat(struct udevice *dev, void *uclass_plat)
  980. {
  981. dev->uclass_plat_ = uclass_plat;
  982. }
  983. #if CONFIG_IS_ENABLED(OF_REAL)
  984. bool device_is_compatible(const struct udevice *dev, const char *compat)
  985. {
  986. return ofnode_device_is_compatible(dev_ofnode(dev), compat);
  987. }
  988. bool of_machine_is_compatible(const char *compat)
  989. {
  990. return ofnode_device_is_compatible(ofnode_root(), compat);
  991. }
  992. int dev_disable_by_path(const char *path)
  993. {
  994. struct uclass *uc;
  995. ofnode node = ofnode_path(path);
  996. struct udevice *dev;
  997. int ret = 1;
  998. if (!of_live_active())
  999. return -ENOSYS;
  1000. list_for_each_entry(uc, gd->uclass_root, sibling_node) {
  1001. ret = uclass_find_device_by_ofnode(uc->uc_drv->id, node, &dev);
  1002. if (!ret)
  1003. break;
  1004. }
  1005. if (ret)
  1006. return ret;
  1007. ret = device_remove(dev, DM_REMOVE_NORMAL);
  1008. if (ret)
  1009. return ret;
  1010. ret = device_unbind(dev);
  1011. if (ret)
  1012. return ret;
  1013. return ofnode_set_enabled(node, false);
  1014. }
  1015. int dev_enable_by_path(const char *path)
  1016. {
  1017. ofnode node = ofnode_path(path);
  1018. ofnode pnode = ofnode_get_parent(node);
  1019. struct udevice *parent;
  1020. int ret = 1;
  1021. if (!of_live_active())
  1022. return -ENOSYS;
  1023. ret = device_find_by_ofnode(pnode, &parent);
  1024. if (ret)
  1025. return ret;
  1026. ret = ofnode_set_enabled(node, true);
  1027. if (ret)
  1028. return ret;
  1029. return lists_bind_fdt(parent, node, NULL, NULL, false);
  1030. }
  1031. #endif
  1032. #if CONFIG_IS_ENABLED(OF_PLATDATA_RT)
  1033. static struct udevice_rt *dev_get_rt(const struct udevice *dev)
  1034. {
  1035. struct udevice *base = ll_entry_start(struct udevice, udevice);
  1036. uint each_size = dm_udevice_size();
  1037. int idx = ((void *)dev - (void *)base) / each_size;
  1038. struct udevice_rt *urt = gd_dm_udevice_rt() + idx;
  1039. return urt;
  1040. }
  1041. u32 dev_get_flags(const struct udevice *dev)
  1042. {
  1043. const struct udevice_rt *urt = dev_get_rt(dev);
  1044. return urt->flags_;
  1045. }
  1046. void dev_or_flags(const struct udevice *dev, u32 or)
  1047. {
  1048. struct udevice_rt *urt = dev_get_rt(dev);
  1049. urt->flags_ |= or;
  1050. }
  1051. void dev_bic_flags(const struct udevice *dev, u32 bic)
  1052. {
  1053. struct udevice_rt *urt = dev_get_rt(dev);
  1054. urt->flags_ &= ~bic;
  1055. }
  1056. #endif /* OF_PLATDATA_RT */