device.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. static void *alloc_priv(int size, uint flags)
  147. {
  148. void *priv;
  149. if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
  150. priv = memalign(ARCH_DMA_MINALIGN, size);
  151. if (priv)
  152. memset(priv, '\0', size);
  153. } else {
  154. priv = calloc(1, size);
  155. }
  156. return priv;
  157. }
  158. int device_probe_child(struct udevice *dev, void *parent_priv)
  159. {
  160. struct driver *drv;
  161. int size = 0;
  162. int ret;
  163. int seq;
  164. if (!dev)
  165. return -EINVAL;
  166. if (dev->flags & DM_FLAG_ACTIVATED)
  167. return 0;
  168. drv = dev->driver;
  169. assert(drv);
  170. /* Allocate private data if requested */
  171. if (drv->priv_auto_alloc_size) {
  172. dev->priv = alloc_priv(drv->priv_auto_alloc_size, drv->flags);
  173. if (!dev->priv) {
  174. ret = -ENOMEM;
  175. goto fail;
  176. }
  177. }
  178. /* Allocate private data if requested */
  179. size = dev->uclass->uc_drv->per_device_auto_alloc_size;
  180. if (size) {
  181. dev->uclass_priv = calloc(1, size);
  182. if (!dev->uclass_priv) {
  183. ret = -ENOMEM;
  184. goto fail;
  185. }
  186. }
  187. /* Ensure all parents are probed */
  188. if (dev->parent) {
  189. size = dev->parent->driver->per_child_auto_alloc_size;
  190. if (!size) {
  191. size = dev->parent->uclass->uc_drv->
  192. per_child_auto_alloc_size;
  193. }
  194. if (size) {
  195. dev->parent_priv = alloc_priv(size, drv->flags);
  196. if (!dev->parent_priv) {
  197. ret = -ENOMEM;
  198. goto fail;
  199. }
  200. if (parent_priv)
  201. memcpy(dev->parent_priv, parent_priv, size);
  202. }
  203. ret = device_probe(dev->parent);
  204. if (ret)
  205. goto fail;
  206. }
  207. seq = uclass_resolve_seq(dev);
  208. if (seq < 0) {
  209. ret = seq;
  210. goto fail;
  211. }
  212. dev->seq = seq;
  213. ret = uclass_pre_probe_device(dev);
  214. if (ret)
  215. goto fail;
  216. if (dev->parent && dev->parent->driver->child_pre_probe) {
  217. ret = dev->parent->driver->child_pre_probe(dev);
  218. if (ret)
  219. goto fail;
  220. }
  221. if (drv->ofdata_to_platdata && dev->of_offset >= 0) {
  222. ret = drv->ofdata_to_platdata(dev);
  223. if (ret)
  224. goto fail;
  225. }
  226. dev->flags |= DM_FLAG_ACTIVATED;
  227. if (drv->probe) {
  228. ret = drv->probe(dev);
  229. if (ret) {
  230. dev->flags &= ~DM_FLAG_ACTIVATED;
  231. goto fail;
  232. }
  233. }
  234. ret = uclass_post_probe_device(dev);
  235. if (ret) {
  236. dev->flags &= ~DM_FLAG_ACTIVATED;
  237. goto fail_uclass;
  238. }
  239. return 0;
  240. fail_uclass:
  241. if (device_remove(dev)) {
  242. dm_warn("%s: Device '%s' failed to remove on error path\n",
  243. __func__, dev->name);
  244. }
  245. fail:
  246. dev->seq = -1;
  247. device_free(dev);
  248. return ret;
  249. }
  250. int device_probe(struct udevice *dev)
  251. {
  252. return device_probe_child(dev, NULL);
  253. }
  254. void *dev_get_platdata(struct udevice *dev)
  255. {
  256. if (!dev) {
  257. dm_warn("%s: null device\n", __func__);
  258. return NULL;
  259. }
  260. return dev->platdata;
  261. }
  262. void *dev_get_parent_platdata(struct udevice *dev)
  263. {
  264. if (!dev) {
  265. dm_warn("%s: null device", __func__);
  266. return NULL;
  267. }
  268. return dev->parent_platdata;
  269. }
  270. void *dev_get_priv(struct udevice *dev)
  271. {
  272. if (!dev) {
  273. dm_warn("%s: null device\n", __func__);
  274. return NULL;
  275. }
  276. return dev->priv;
  277. }
  278. void *dev_get_uclass_priv(struct udevice *dev)
  279. {
  280. if (!dev) {
  281. dm_warn("%s: null device\n", __func__);
  282. return NULL;
  283. }
  284. return dev->uclass_priv;
  285. }
  286. void *dev_get_parentdata(struct udevice *dev)
  287. {
  288. if (!dev) {
  289. dm_warn("%s: null device\n", __func__);
  290. return NULL;
  291. }
  292. return dev->parent_priv;
  293. }
  294. static int device_get_device_tail(struct udevice *dev, int ret,
  295. struct udevice **devp)
  296. {
  297. if (ret)
  298. return ret;
  299. ret = device_probe(dev);
  300. if (ret)
  301. return ret;
  302. *devp = dev;
  303. return 0;
  304. }
  305. int device_get_child(struct udevice *parent, int index, struct udevice **devp)
  306. {
  307. struct udevice *dev;
  308. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  309. if (!index--)
  310. return device_get_device_tail(dev, 0, devp);
  311. }
  312. return -ENODEV;
  313. }
  314. int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
  315. bool find_req_seq, struct udevice **devp)
  316. {
  317. struct udevice *dev;
  318. *devp = NULL;
  319. if (seq_or_req_seq == -1)
  320. return -ENODEV;
  321. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  322. if ((find_req_seq ? dev->req_seq : dev->seq) ==
  323. seq_or_req_seq) {
  324. *devp = dev;
  325. return 0;
  326. }
  327. }
  328. return -ENODEV;
  329. }
  330. int device_get_child_by_seq(struct udevice *parent, int seq,
  331. struct udevice **devp)
  332. {
  333. struct udevice *dev;
  334. int ret;
  335. *devp = NULL;
  336. ret = device_find_child_by_seq(parent, seq, false, &dev);
  337. if (ret == -ENODEV) {
  338. /*
  339. * We didn't find it in probed devices. See if there is one
  340. * that will request this seq if probed.
  341. */
  342. ret = device_find_child_by_seq(parent, seq, true, &dev);
  343. }
  344. return device_get_device_tail(dev, ret, devp);
  345. }
  346. int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
  347. struct udevice **devp)
  348. {
  349. struct udevice *dev;
  350. *devp = NULL;
  351. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  352. if (dev->of_offset == of_offset) {
  353. *devp = dev;
  354. return 0;
  355. }
  356. }
  357. return -ENODEV;
  358. }
  359. int device_get_child_by_of_offset(struct udevice *parent, int seq,
  360. struct udevice **devp)
  361. {
  362. struct udevice *dev;
  363. int ret;
  364. *devp = NULL;
  365. ret = device_find_child_by_of_offset(parent, seq, &dev);
  366. return device_get_device_tail(dev, ret, devp);
  367. }
  368. int device_find_first_child(struct udevice *parent, struct udevice **devp)
  369. {
  370. if (list_empty(&parent->child_head)) {
  371. *devp = NULL;
  372. } else {
  373. *devp = list_first_entry(&parent->child_head, struct udevice,
  374. sibling_node);
  375. }
  376. return 0;
  377. }
  378. int device_find_next_child(struct udevice **devp)
  379. {
  380. struct udevice *dev = *devp;
  381. struct udevice *parent = dev->parent;
  382. if (list_is_last(&dev->sibling_node, &parent->child_head)) {
  383. *devp = NULL;
  384. } else {
  385. *devp = list_entry(dev->sibling_node.next, struct udevice,
  386. sibling_node);
  387. }
  388. return 0;
  389. }
  390. struct udevice *dev_get_parent(struct udevice *child)
  391. {
  392. return child->parent;
  393. }
  394. ulong dev_get_of_data(struct udevice *dev)
  395. {
  396. return dev->of_id->data;
  397. }
  398. enum uclass_id device_get_uclass_id(struct udevice *dev)
  399. {
  400. return dev->uclass->uc_drv->id;
  401. }
  402. #ifdef CONFIG_OF_CONTROL
  403. fdt_addr_t dev_get_addr(struct udevice *dev)
  404. {
  405. return fdtdec_get_addr(gd->fdt_blob, dev->of_offset, "reg");
  406. }
  407. #else
  408. fdt_addr_t dev_get_addr(struct udevice *dev)
  409. {
  410. return FDT_ADDR_T_NONE;
  411. }
  412. #endif