device.c 11 KB

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