device.c 12 KB

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