device.c 9.5 KB

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