device.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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, struct driver *drv, const char *name,
  25. void *platdata, int of_offset, struct udevice **devp)
  26. {
  27. struct udevice *dev;
  28. struct uclass *uc;
  29. int ret = 0;
  30. *devp = NULL;
  31. if (!name)
  32. return -EINVAL;
  33. ret = uclass_get(drv->id, &uc);
  34. if (ret)
  35. return ret;
  36. dev = calloc(1, sizeof(struct udevice));
  37. if (!dev)
  38. return -ENOMEM;
  39. INIT_LIST_HEAD(&dev->sibling_node);
  40. INIT_LIST_HEAD(&dev->child_head);
  41. INIT_LIST_HEAD(&dev->uclass_node);
  42. dev->platdata = platdata;
  43. dev->name = name;
  44. dev->of_offset = of_offset;
  45. dev->parent = parent;
  46. dev->driver = drv;
  47. dev->uclass = uc;
  48. dev->seq = -1;
  49. dev->req_seq = -1;
  50. #ifdef CONFIG_OF_CONTROL
  51. /*
  52. * Some devices, such as a SPI bus, I2C bus and serial ports are
  53. * numbered using aliases.
  54. *
  55. * This is just a 'requested' sequence, and will be
  56. * resolved (and ->seq updated) when the device is probed.
  57. */
  58. if (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS) {
  59. if (uc->uc_drv->name && of_offset != -1) {
  60. fdtdec_get_alias_seq(gd->fdt_blob, uc->uc_drv->name,
  61. of_offset, &dev->req_seq);
  62. }
  63. }
  64. #endif
  65. if (!dev->platdata && drv->platdata_auto_alloc_size) {
  66. dev->flags |= DM_FLAG_ALLOC_PDATA;
  67. dev->platdata = calloc(1, drv->platdata_auto_alloc_size);
  68. if (!dev->platdata) {
  69. ret = -ENOMEM;
  70. goto fail_alloc1;
  71. }
  72. }
  73. if (parent) {
  74. int size = parent->driver->per_child_platdata_auto_alloc_size;
  75. if (!size) {
  76. size = parent->uclass->uc_drv->
  77. per_child_platdata_auto_alloc_size;
  78. }
  79. if (size) {
  80. dev->flags |= DM_FLAG_ALLOC_PARENT_PDATA;
  81. dev->parent_platdata = calloc(1, size);
  82. if (!dev->parent_platdata) {
  83. ret = -ENOMEM;
  84. goto fail_alloc2;
  85. }
  86. }
  87. }
  88. /* put dev into parent's successor list */
  89. if (parent)
  90. list_add_tail(&dev->sibling_node, &parent->child_head);
  91. ret = uclass_bind_device(dev);
  92. if (ret)
  93. goto fail_uclass_bind;
  94. /* if we fail to bind we remove device from successors and free it */
  95. if (drv->bind) {
  96. ret = drv->bind(dev);
  97. if (ret)
  98. goto fail_bind;
  99. }
  100. if (parent && parent->driver->child_post_bind) {
  101. ret = parent->driver->child_post_bind(dev);
  102. if (ret)
  103. goto fail_child_post_bind;
  104. }
  105. if (parent)
  106. dm_dbg("Bound device %s to %s\n", dev->name, parent->name);
  107. *devp = dev;
  108. return 0;
  109. fail_child_post_bind:
  110. if (drv->unbind && drv->unbind(dev)) {
  111. dm_warn("unbind() method failed on dev '%s' on error path\n",
  112. dev->name);
  113. }
  114. fail_bind:
  115. if (uclass_unbind_device(dev)) {
  116. dm_warn("Failed to unbind dev '%s' on error path\n",
  117. dev->name);
  118. }
  119. fail_uclass_bind:
  120. list_del(&dev->sibling_node);
  121. if (dev->flags & DM_FLAG_ALLOC_PARENT_PDATA) {
  122. free(dev->parent_platdata);
  123. dev->parent_platdata = NULL;
  124. }
  125. fail_alloc2:
  126. if (dev->flags & DM_FLAG_ALLOC_PDATA) {
  127. free(dev->platdata);
  128. dev->platdata = NULL;
  129. }
  130. fail_alloc1:
  131. free(dev);
  132. return ret;
  133. }
  134. int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
  135. const struct driver_info *info, struct udevice **devp)
  136. {
  137. struct driver *drv;
  138. drv = lists_driver_lookup_name(info->name);
  139. if (!drv)
  140. return -ENOENT;
  141. if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
  142. return -EPERM;
  143. return device_bind(parent, drv, info->name, (void *)info->platdata,
  144. -1, devp);
  145. }
  146. int device_probe_child(struct udevice *dev, void *parent_priv)
  147. {
  148. struct driver *drv;
  149. int size = 0;
  150. int ret;
  151. int seq;
  152. if (!dev)
  153. return -EINVAL;
  154. if (dev->flags & DM_FLAG_ACTIVATED)
  155. return 0;
  156. drv = dev->driver;
  157. assert(drv);
  158. /* Allocate private data if requested */
  159. if (drv->priv_auto_alloc_size) {
  160. dev->priv = calloc(1, drv->priv_auto_alloc_size);
  161. if (!dev->priv) {
  162. ret = -ENOMEM;
  163. goto fail;
  164. }
  165. }
  166. /* Allocate private data if requested */
  167. size = dev->uclass->uc_drv->per_device_auto_alloc_size;
  168. if (size) {
  169. dev->uclass_priv = calloc(1, size);
  170. if (!dev->uclass_priv) {
  171. ret = -ENOMEM;
  172. goto fail;
  173. }
  174. }
  175. /* Ensure all parents are probed */
  176. if (dev->parent) {
  177. size = dev->parent->driver->per_child_auto_alloc_size;
  178. if (!size) {
  179. size = dev->parent->uclass->uc_drv->
  180. per_child_auto_alloc_size;
  181. }
  182. if (size) {
  183. dev->parent_priv = calloc(1, size);
  184. if (!dev->parent_priv) {
  185. ret = -ENOMEM;
  186. goto fail;
  187. }
  188. if (parent_priv)
  189. memcpy(dev->parent_priv, parent_priv, size);
  190. }
  191. ret = device_probe(dev->parent);
  192. if (ret)
  193. goto fail;
  194. }
  195. seq = uclass_resolve_seq(dev);
  196. if (seq < 0) {
  197. ret = seq;
  198. goto fail;
  199. }
  200. dev->seq = seq;
  201. ret = uclass_pre_probe_device(dev);
  202. if (ret)
  203. goto fail;
  204. if (dev->parent && dev->parent->driver->child_pre_probe) {
  205. ret = dev->parent->driver->child_pre_probe(dev);
  206. if (ret)
  207. goto fail;
  208. }
  209. if (drv->ofdata_to_platdata && dev->of_offset >= 0) {
  210. ret = drv->ofdata_to_platdata(dev);
  211. if (ret)
  212. goto fail;
  213. }
  214. dev->flags |= DM_FLAG_ACTIVATED;
  215. if (drv->probe) {
  216. ret = drv->probe(dev);
  217. if (ret) {
  218. dev->flags &= ~DM_FLAG_ACTIVATED;
  219. goto fail;
  220. }
  221. }
  222. ret = uclass_post_probe_device(dev);
  223. if (ret) {
  224. dev->flags &= ~DM_FLAG_ACTIVATED;
  225. goto fail_uclass;
  226. }
  227. return 0;
  228. fail_uclass:
  229. if (device_remove(dev)) {
  230. dm_warn("%s: Device '%s' failed to remove on error path\n",
  231. __func__, dev->name);
  232. }
  233. fail:
  234. dev->seq = -1;
  235. device_free(dev);
  236. return ret;
  237. }
  238. int device_probe(struct udevice *dev)
  239. {
  240. return device_probe_child(dev, NULL);
  241. }
  242. void *dev_get_platdata(struct udevice *dev)
  243. {
  244. if (!dev) {
  245. dm_warn("%s: null device\n", __func__);
  246. return NULL;
  247. }
  248. return dev->platdata;
  249. }
  250. void *dev_get_parent_platdata(struct udevice *dev)
  251. {
  252. if (!dev) {
  253. dm_warn("%s: null device", __func__);
  254. return NULL;
  255. }
  256. return dev->parent_platdata;
  257. }
  258. void *dev_get_priv(struct udevice *dev)
  259. {
  260. if (!dev) {
  261. dm_warn("%s: null device\n", __func__);
  262. return NULL;
  263. }
  264. return dev->priv;
  265. }
  266. void *dev_get_uclass_priv(struct udevice *dev)
  267. {
  268. if (!dev) {
  269. dm_warn("%s: null device\n", __func__);
  270. return NULL;
  271. }
  272. return dev->uclass_priv;
  273. }
  274. void *dev_get_parentdata(struct udevice *dev)
  275. {
  276. if (!dev) {
  277. dm_warn("%s: null device\n", __func__);
  278. return NULL;
  279. }
  280. return dev->parent_priv;
  281. }
  282. static int device_get_device_tail(struct udevice *dev, int ret,
  283. struct udevice **devp)
  284. {
  285. if (ret)
  286. return ret;
  287. ret = device_probe(dev);
  288. if (ret)
  289. return ret;
  290. *devp = dev;
  291. return 0;
  292. }
  293. int device_get_child(struct udevice *parent, int index, struct udevice **devp)
  294. {
  295. struct udevice *dev;
  296. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  297. if (!index--)
  298. return device_get_device_tail(dev, 0, devp);
  299. }
  300. return -ENODEV;
  301. }
  302. int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
  303. bool find_req_seq, struct udevice **devp)
  304. {
  305. struct udevice *dev;
  306. *devp = NULL;
  307. if (seq_or_req_seq == -1)
  308. return -ENODEV;
  309. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  310. if ((find_req_seq ? dev->req_seq : dev->seq) ==
  311. seq_or_req_seq) {
  312. *devp = dev;
  313. return 0;
  314. }
  315. }
  316. return -ENODEV;
  317. }
  318. int device_get_child_by_seq(struct udevice *parent, int seq,
  319. struct udevice **devp)
  320. {
  321. struct udevice *dev;
  322. int ret;
  323. *devp = NULL;
  324. ret = device_find_child_by_seq(parent, seq, false, &dev);
  325. if (ret == -ENODEV) {
  326. /*
  327. * We didn't find it in probed devices. See if there is one
  328. * that will request this seq if probed.
  329. */
  330. ret = device_find_child_by_seq(parent, seq, true, &dev);
  331. }
  332. return device_get_device_tail(dev, ret, devp);
  333. }
  334. int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
  335. struct udevice **devp)
  336. {
  337. struct udevice *dev;
  338. *devp = NULL;
  339. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  340. if (dev->of_offset == of_offset) {
  341. *devp = dev;
  342. return 0;
  343. }
  344. }
  345. return -ENODEV;
  346. }
  347. int device_get_child_by_of_offset(struct udevice *parent, int seq,
  348. struct udevice **devp)
  349. {
  350. struct udevice *dev;
  351. int ret;
  352. *devp = NULL;
  353. ret = device_find_child_by_of_offset(parent, seq, &dev);
  354. return device_get_device_tail(dev, ret, devp);
  355. }
  356. int device_find_first_child(struct udevice *parent, struct udevice **devp)
  357. {
  358. if (list_empty(&parent->child_head)) {
  359. *devp = NULL;
  360. } else {
  361. *devp = list_first_entry(&parent->child_head, struct udevice,
  362. sibling_node);
  363. }
  364. return 0;
  365. }
  366. int device_find_next_child(struct udevice **devp)
  367. {
  368. struct udevice *dev = *devp;
  369. struct udevice *parent = dev->parent;
  370. if (list_is_last(&dev->sibling_node, &parent->child_head)) {
  371. *devp = NULL;
  372. } else {
  373. *devp = list_entry(dev->sibling_node.next, struct udevice,
  374. sibling_node);
  375. }
  376. return 0;
  377. }
  378. struct udevice *dev_get_parent(struct udevice *child)
  379. {
  380. return child->parent;
  381. }
  382. ulong dev_get_of_data(struct udevice *dev)
  383. {
  384. return dev->of_id->data;
  385. }
  386. enum uclass_id device_get_uclass_id(struct udevice *dev)
  387. {
  388. return dev->uclass->uc_drv->id;
  389. }
  390. #ifdef CONFIG_OF_CONTROL
  391. fdt_addr_t dev_get_addr(struct udevice *dev)
  392. {
  393. return fdtdec_get_addr(gd->fdt_blob, dev->of_offset, "reg");
  394. }
  395. #else
  396. fdt_addr_t dev_get_addr(struct udevice *dev)
  397. {
  398. return FDT_ADDR_T_NONE;
  399. }
  400. #endif