device.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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 <fdtdec.h>
  13. #include <fdt_support.h>
  14. #include <malloc.h>
  15. #include <dm/device.h>
  16. #include <dm/device-internal.h>
  17. #include <dm/lists.h>
  18. #include <dm/pinctrl.h>
  19. #include <dm/platdata.h>
  20. #include <dm/uclass.h>
  21. #include <dm/uclass-internal.h>
  22. #include <dm/util.h>
  23. #include <linux/err.h>
  24. #include <linux/list.h>
  25. DECLARE_GLOBAL_DATA_PTR;
  26. int device_bind(struct udevice *parent, const struct driver *drv,
  27. const char *name, void *platdata, int of_offset,
  28. struct udevice **devp)
  29. {
  30. struct udevice *dev;
  31. struct uclass *uc;
  32. int size, ret = 0;
  33. if (devp)
  34. *devp = NULL;
  35. if (!name)
  36. return -EINVAL;
  37. ret = uclass_get(drv->id, &uc);
  38. if (ret) {
  39. debug("Missing uclass for driver %s\n", drv->name);
  40. return ret;
  41. }
  42. dev = calloc(1, sizeof(struct udevice));
  43. if (!dev)
  44. return -ENOMEM;
  45. INIT_LIST_HEAD(&dev->sibling_node);
  46. INIT_LIST_HEAD(&dev->child_head);
  47. INIT_LIST_HEAD(&dev->uclass_node);
  48. #ifdef CONFIG_DEVRES
  49. INIT_LIST_HEAD(&dev->devres_head);
  50. #endif
  51. dev->platdata = platdata;
  52. dev->name = name;
  53. dev->of_offset = of_offset;
  54. dev->parent = parent;
  55. dev->driver = drv;
  56. dev->uclass = uc;
  57. dev->seq = -1;
  58. dev->req_seq = -1;
  59. if (CONFIG_IS_ENABLED(OF_CONTROL) && IS_ENABLED(CONFIG_DM_SEQ_ALIAS)) {
  60. /*
  61. * Some devices, such as a SPI bus, I2C bus and serial ports
  62. * are numbered using aliases.
  63. *
  64. * This is just a 'requested' sequence, and will be
  65. * resolved (and ->seq updated) when the device is probed.
  66. */
  67. if (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS) {
  68. if (uc->uc_drv->name && of_offset != -1) {
  69. fdtdec_get_alias_seq(gd->fdt_blob,
  70. uc->uc_drv->name, of_offset,
  71. &dev->req_seq);
  72. }
  73. }
  74. }
  75. if (!dev->platdata && drv->platdata_auto_alloc_size) {
  76. dev->flags |= DM_FLAG_ALLOC_PDATA;
  77. dev->platdata = calloc(1, drv->platdata_auto_alloc_size);
  78. if (!dev->platdata) {
  79. ret = -ENOMEM;
  80. goto fail_alloc1;
  81. }
  82. }
  83. size = uc->uc_drv->per_device_platdata_auto_alloc_size;
  84. if (size) {
  85. dev->flags |= DM_FLAG_ALLOC_UCLASS_PDATA;
  86. dev->uclass_platdata = calloc(1, size);
  87. if (!dev->uclass_platdata) {
  88. ret = -ENOMEM;
  89. goto fail_alloc2;
  90. }
  91. }
  92. if (parent) {
  93. size = parent->driver->per_child_platdata_auto_alloc_size;
  94. if (!size) {
  95. size = parent->uclass->uc_drv->
  96. per_child_platdata_auto_alloc_size;
  97. }
  98. if (size) {
  99. dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA;
  100. dev->parent_platdata = calloc(1, size);
  101. if (!dev->parent_platdata) {
  102. ret = -ENOMEM;
  103. goto fail_alloc3;
  104. }
  105. }
  106. }
  107. /* put dev into parent's successor list */
  108. if (parent)
  109. list_add_tail(&dev->sibling_node, &parent->child_head);
  110. ret = uclass_bind_device(dev);
  111. if (ret)
  112. goto fail_uclass_bind;
  113. /* if we fail to bind we remove device from successors and free it */
  114. if (drv->bind) {
  115. ret = drv->bind(dev);
  116. if (ret)
  117. goto fail_bind;
  118. }
  119. if (parent && parent->driver->child_post_bind) {
  120. ret = parent->driver->child_post_bind(dev);
  121. if (ret)
  122. goto fail_child_post_bind;
  123. }
  124. if (parent)
  125. dm_dbg("Bound device %s to %s\n", dev->name, parent->name);
  126. if (devp)
  127. *devp = dev;
  128. dev->flags |= DM_FLAG_BOUND;
  129. return 0;
  130. fail_child_post_bind:
  131. if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
  132. if (drv->unbind && drv->unbind(dev)) {
  133. dm_warn("unbind() method failed on dev '%s' on error path\n",
  134. dev->name);
  135. }
  136. }
  137. fail_bind:
  138. if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
  139. if (uclass_unbind_device(dev)) {
  140. dm_warn("Failed to unbind dev '%s' on error path\n",
  141. dev->name);
  142. }
  143. }
  144. fail_uclass_bind:
  145. if (CONFIG_IS_ENABLED(DM_DEVICE_REMOVE)) {
  146. list_del(&dev->sibling_node);
  147. if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
  148. free(dev->parent_platdata);
  149. dev->parent_platdata = NULL;
  150. }
  151. }
  152. fail_alloc3:
  153. if (dev->flags & DM_FLAG_ALLOC_UCLASS_PDATA) {
  154. free(dev->uclass_platdata);
  155. dev->uclass_platdata = NULL;
  156. }
  157. fail_alloc2:
  158. if (dev->flags & DM_FLAG_ALLOC_PDATA) {
  159. free(dev->platdata);
  160. dev->platdata = NULL;
  161. }
  162. fail_alloc1:
  163. devres_release_all(dev);
  164. free(dev);
  165. return ret;
  166. }
  167. int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
  168. const struct driver_info *info, struct udevice **devp)
  169. {
  170. struct driver *drv;
  171. drv = lists_driver_lookup_name(info->name);
  172. if (!drv)
  173. return -ENOENT;
  174. if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
  175. return -EPERM;
  176. return device_bind(parent, drv, info->name, (void *)info->platdata,
  177. -1, devp);
  178. }
  179. static void *alloc_priv(int size, uint flags)
  180. {
  181. void *priv;
  182. if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
  183. priv = memalign(ARCH_DMA_MINALIGN, size);
  184. if (priv)
  185. memset(priv, '\0', size);
  186. } else {
  187. priv = calloc(1, size);
  188. }
  189. return priv;
  190. }
  191. int device_probe_child(struct udevice *dev, void *parent_priv)
  192. {
  193. const struct driver *drv;
  194. int size = 0;
  195. int ret;
  196. int seq;
  197. if (!dev)
  198. return -EINVAL;
  199. if (dev->flags & DM_FLAG_ACTIVATED)
  200. return 0;
  201. drv = dev->driver;
  202. assert(drv);
  203. /* Allocate private data if requested and not reentered */
  204. if (drv->priv_auto_alloc_size && !dev->priv) {
  205. dev->priv = alloc_priv(drv->priv_auto_alloc_size, drv->flags);
  206. if (!dev->priv) {
  207. ret = -ENOMEM;
  208. goto fail;
  209. }
  210. }
  211. /* Allocate private data if requested and not reentered */
  212. size = dev->uclass->uc_drv->per_device_auto_alloc_size;
  213. if (size && !dev->uclass_priv) {
  214. dev->uclass_priv = calloc(1, size);
  215. if (!dev->uclass_priv) {
  216. ret = -ENOMEM;
  217. goto fail;
  218. }
  219. }
  220. /* Ensure all parents are probed */
  221. if (dev->parent) {
  222. size = dev->parent->driver->per_child_auto_alloc_size;
  223. if (!size) {
  224. size = dev->parent->uclass->uc_drv->
  225. per_child_auto_alloc_size;
  226. }
  227. if (size && !dev->parent_priv) {
  228. dev->parent_priv = alloc_priv(size, drv->flags);
  229. if (!dev->parent_priv) {
  230. ret = -ENOMEM;
  231. goto fail;
  232. }
  233. if (parent_priv)
  234. memcpy(dev->parent_priv, parent_priv, size);
  235. }
  236. ret = device_probe(dev->parent);
  237. if (ret)
  238. goto fail;
  239. /*
  240. * The device might have already been probed during
  241. * the call to device_probe() on its parent device
  242. * (e.g. PCI bridge devices). Test the flags again
  243. * so that we don't mess up the device.
  244. */
  245. if (dev->flags & DM_FLAG_ACTIVATED)
  246. return 0;
  247. }
  248. seq = uclass_resolve_seq(dev);
  249. if (seq < 0) {
  250. ret = seq;
  251. goto fail;
  252. }
  253. dev->seq = seq;
  254. dev->flags |= DM_FLAG_ACTIVATED;
  255. /*
  256. * Process pinctrl for everything except the root device, and
  257. * continue regardless of the result of pinctrl.
  258. */
  259. if (dev->parent)
  260. pinctrl_select_state(dev, "default");
  261. ret = uclass_pre_probe_device(dev);
  262. if (ret)
  263. goto fail;
  264. if (dev->parent && dev->parent->driver->child_pre_probe) {
  265. ret = dev->parent->driver->child_pre_probe(dev);
  266. if (ret)
  267. goto fail;
  268. }
  269. if (drv->ofdata_to_platdata && dev->of_offset >= 0) {
  270. ret = drv->ofdata_to_platdata(dev);
  271. if (ret)
  272. goto fail;
  273. }
  274. if (drv->probe) {
  275. ret = drv->probe(dev);
  276. if (ret) {
  277. dev->flags &= ~DM_FLAG_ACTIVATED;
  278. goto fail;
  279. }
  280. }
  281. ret = uclass_post_probe_device(dev);
  282. if (ret)
  283. goto fail_uclass;
  284. return 0;
  285. fail_uclass:
  286. if (device_remove(dev)) {
  287. dm_warn("%s: Device '%s' failed to remove on error path\n",
  288. __func__, dev->name);
  289. }
  290. fail:
  291. dev->flags &= ~DM_FLAG_ACTIVATED;
  292. dev->seq = -1;
  293. device_free(dev);
  294. return ret;
  295. }
  296. int device_probe(struct udevice *dev)
  297. {
  298. return device_probe_child(dev, NULL);
  299. }
  300. void *dev_get_platdata(struct udevice *dev)
  301. {
  302. if (!dev) {
  303. dm_warn("%s: null device\n", __func__);
  304. return NULL;
  305. }
  306. return dev->platdata;
  307. }
  308. void *dev_get_parent_platdata(struct udevice *dev)
  309. {
  310. if (!dev) {
  311. dm_warn("%s: null device\n", __func__);
  312. return NULL;
  313. }
  314. return dev->parent_platdata;
  315. }
  316. void *dev_get_uclass_platdata(struct udevice *dev)
  317. {
  318. if (!dev) {
  319. dm_warn("%s: null device\n", __func__);
  320. return NULL;
  321. }
  322. return dev->uclass_platdata;
  323. }
  324. void *dev_get_priv(struct udevice *dev)
  325. {
  326. if (!dev) {
  327. dm_warn("%s: null device\n", __func__);
  328. return NULL;
  329. }
  330. return dev->priv;
  331. }
  332. void *dev_get_uclass_priv(struct udevice *dev)
  333. {
  334. if (!dev) {
  335. dm_warn("%s: null device\n", __func__);
  336. return NULL;
  337. }
  338. return dev->uclass_priv;
  339. }
  340. void *dev_get_parentdata(struct udevice *dev)
  341. {
  342. if (!dev) {
  343. dm_warn("%s: null device\n", __func__);
  344. return NULL;
  345. }
  346. return dev->parent_priv;
  347. }
  348. static int device_get_device_tail(struct udevice *dev, int ret,
  349. struct udevice **devp)
  350. {
  351. if (ret)
  352. return ret;
  353. ret = device_probe(dev);
  354. if (ret)
  355. return ret;
  356. *devp = dev;
  357. return 0;
  358. }
  359. int device_get_child(struct udevice *parent, int index, struct udevice **devp)
  360. {
  361. struct udevice *dev;
  362. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  363. if (!index--)
  364. return device_get_device_tail(dev, 0, devp);
  365. }
  366. return -ENODEV;
  367. }
  368. int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
  369. bool find_req_seq, struct udevice **devp)
  370. {
  371. struct udevice *dev;
  372. *devp = NULL;
  373. if (seq_or_req_seq == -1)
  374. return -ENODEV;
  375. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  376. if ((find_req_seq ? dev->req_seq : dev->seq) ==
  377. seq_or_req_seq) {
  378. *devp = dev;
  379. return 0;
  380. }
  381. }
  382. return -ENODEV;
  383. }
  384. int device_get_child_by_seq(struct udevice *parent, int seq,
  385. struct udevice **devp)
  386. {
  387. struct udevice *dev;
  388. int ret;
  389. *devp = NULL;
  390. ret = device_find_child_by_seq(parent, seq, false, &dev);
  391. if (ret == -ENODEV) {
  392. /*
  393. * We didn't find it in probed devices. See if there is one
  394. * that will request this seq if probed.
  395. */
  396. ret = device_find_child_by_seq(parent, seq, true, &dev);
  397. }
  398. return device_get_device_tail(dev, ret, devp);
  399. }
  400. int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
  401. struct udevice **devp)
  402. {
  403. struct udevice *dev;
  404. *devp = NULL;
  405. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  406. if (dev->of_offset == of_offset) {
  407. *devp = dev;
  408. return 0;
  409. }
  410. }
  411. return -ENODEV;
  412. }
  413. int device_get_child_by_of_offset(struct udevice *parent, int node,
  414. struct udevice **devp)
  415. {
  416. struct udevice *dev;
  417. int ret;
  418. *devp = NULL;
  419. ret = device_find_child_by_of_offset(parent, node, &dev);
  420. return device_get_device_tail(dev, ret, devp);
  421. }
  422. static struct udevice *_device_find_global_by_of_offset(struct udevice *parent,
  423. int of_offset)
  424. {
  425. struct udevice *dev, *found;
  426. if (parent->of_offset == of_offset)
  427. return parent;
  428. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  429. found = _device_find_global_by_of_offset(dev, of_offset);
  430. if (found)
  431. return found;
  432. }
  433. return NULL;
  434. }
  435. int device_get_global_by_of_offset(int of_offset, struct udevice **devp)
  436. {
  437. struct udevice *dev;
  438. dev = _device_find_global_by_of_offset(gd->dm_root, of_offset);
  439. return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
  440. }
  441. int device_find_first_child(struct udevice *parent, struct udevice **devp)
  442. {
  443. if (list_empty(&parent->child_head)) {
  444. *devp = NULL;
  445. } else {
  446. *devp = list_first_entry(&parent->child_head, struct udevice,
  447. sibling_node);
  448. }
  449. return 0;
  450. }
  451. int device_find_next_child(struct udevice **devp)
  452. {
  453. struct udevice *dev = *devp;
  454. struct udevice *parent = dev->parent;
  455. if (list_is_last(&dev->sibling_node, &parent->child_head)) {
  456. *devp = NULL;
  457. } else {
  458. *devp = list_entry(dev->sibling_node.next, struct udevice,
  459. sibling_node);
  460. }
  461. return 0;
  462. }
  463. struct udevice *dev_get_parent(struct udevice *child)
  464. {
  465. return child->parent;
  466. }
  467. ulong dev_get_driver_data(struct udevice *dev)
  468. {
  469. return dev->driver_data;
  470. }
  471. const void *dev_get_driver_ops(struct udevice *dev)
  472. {
  473. if (!dev || !dev->driver->ops)
  474. return NULL;
  475. return dev->driver->ops;
  476. }
  477. enum uclass_id device_get_uclass_id(struct udevice *dev)
  478. {
  479. return dev->uclass->uc_drv->id;
  480. }
  481. const char *dev_get_uclass_name(struct udevice *dev)
  482. {
  483. if (!dev)
  484. return NULL;
  485. return dev->uclass->uc_drv->name;
  486. }
  487. fdt_addr_t dev_get_addr(struct udevice *dev)
  488. {
  489. #if CONFIG_IS_ENABLED(OF_CONTROL)
  490. fdt_addr_t addr;
  491. if (CONFIG_IS_ENABLED(OF_TRANSLATE)) {
  492. const fdt32_t *reg;
  493. reg = fdt_getprop(gd->fdt_blob, dev->of_offset, "reg", NULL);
  494. if (!reg)
  495. return FDT_ADDR_T_NONE;
  496. /*
  497. * Use the full-fledged translate function for complex
  498. * bus setups.
  499. */
  500. return fdt_translate_address((void *)gd->fdt_blob,
  501. dev->of_offset, reg);
  502. }
  503. /*
  504. * Use the "simple" translate function for less complex
  505. * bus setups.
  506. */
  507. addr = fdtdec_get_addr_size_auto_parent(gd->fdt_blob,
  508. dev->parent->of_offset,
  509. dev->of_offset, "reg",
  510. 0, NULL);
  511. if (CONFIG_IS_ENABLED(SIMPLE_BUS) && addr != FDT_ADDR_T_NONE) {
  512. if (device_get_uclass_id(dev->parent) == UCLASS_SIMPLE_BUS)
  513. addr = simple_bus_translate(dev->parent, addr);
  514. }
  515. return addr;
  516. #else
  517. return FDT_ADDR_T_NONE;
  518. #endif
  519. }
  520. bool device_has_children(struct udevice *dev)
  521. {
  522. return !list_empty(&dev->child_head);
  523. }
  524. bool device_has_active_children(struct udevice *dev)
  525. {
  526. struct udevice *child;
  527. for (device_find_first_child(dev, &child);
  528. child;
  529. device_find_next_child(&child)) {
  530. if (device_active(child))
  531. return true;
  532. }
  533. return false;
  534. }
  535. bool device_is_last_sibling(struct udevice *dev)
  536. {
  537. struct udevice *parent = dev->parent;
  538. if (!parent)
  539. return false;
  540. return list_is_last(&dev->sibling_node, &parent->child_head);
  541. }
  542. int device_set_name(struct udevice *dev, const char *name)
  543. {
  544. name = strdup(name);
  545. if (!name)
  546. return -ENOMEM;
  547. dev->name = name;
  548. return 0;
  549. }