device.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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. if (drv->probe) {
  250. ret = drv->probe(dev);
  251. if (ret) {
  252. dev->flags &= ~DM_FLAG_ACTIVATED;
  253. goto fail;
  254. }
  255. }
  256. ret = uclass_post_probe_device(dev);
  257. if (ret)
  258. goto fail_uclass;
  259. return 0;
  260. fail_uclass:
  261. if (device_remove(dev)) {
  262. dm_warn("%s: Device '%s' failed to remove on error path\n",
  263. __func__, dev->name);
  264. }
  265. fail:
  266. dev->flags &= ~DM_FLAG_ACTIVATED;
  267. dev->seq = -1;
  268. device_free(dev);
  269. return ret;
  270. }
  271. int device_probe(struct udevice *dev)
  272. {
  273. return device_probe_child(dev, NULL);
  274. }
  275. void *dev_get_platdata(struct udevice *dev)
  276. {
  277. if (!dev) {
  278. dm_warn("%s: null device\n", __func__);
  279. return NULL;
  280. }
  281. return dev->platdata;
  282. }
  283. void *dev_get_parent_platdata(struct udevice *dev)
  284. {
  285. if (!dev) {
  286. dm_warn("%s: null device\n", __func__);
  287. return NULL;
  288. }
  289. return dev->parent_platdata;
  290. }
  291. void *dev_get_uclass_platdata(struct udevice *dev)
  292. {
  293. if (!dev) {
  294. dm_warn("%s: null device\n", __func__);
  295. return NULL;
  296. }
  297. return dev->uclass_platdata;
  298. }
  299. void *dev_get_priv(struct udevice *dev)
  300. {
  301. if (!dev) {
  302. dm_warn("%s: null device\n", __func__);
  303. return NULL;
  304. }
  305. return dev->priv;
  306. }
  307. void *dev_get_uclass_priv(struct udevice *dev)
  308. {
  309. if (!dev) {
  310. dm_warn("%s: null device\n", __func__);
  311. return NULL;
  312. }
  313. return dev->uclass_priv;
  314. }
  315. void *dev_get_parentdata(struct udevice *dev)
  316. {
  317. if (!dev) {
  318. dm_warn("%s: null device\n", __func__);
  319. return NULL;
  320. }
  321. return dev->parent_priv;
  322. }
  323. static int device_get_device_tail(struct udevice *dev, int ret,
  324. struct udevice **devp)
  325. {
  326. if (ret)
  327. return ret;
  328. ret = device_probe(dev);
  329. if (ret)
  330. return ret;
  331. *devp = dev;
  332. return 0;
  333. }
  334. int device_get_child(struct udevice *parent, int index, struct udevice **devp)
  335. {
  336. struct udevice *dev;
  337. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  338. if (!index--)
  339. return device_get_device_tail(dev, 0, devp);
  340. }
  341. return -ENODEV;
  342. }
  343. int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
  344. bool find_req_seq, struct udevice **devp)
  345. {
  346. struct udevice *dev;
  347. *devp = NULL;
  348. if (seq_or_req_seq == -1)
  349. return -ENODEV;
  350. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  351. if ((find_req_seq ? dev->req_seq : dev->seq) ==
  352. seq_or_req_seq) {
  353. *devp = dev;
  354. return 0;
  355. }
  356. }
  357. return -ENODEV;
  358. }
  359. int device_get_child_by_seq(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_seq(parent, seq, false, &dev);
  366. if (ret == -ENODEV) {
  367. /*
  368. * We didn't find it in probed devices. See if there is one
  369. * that will request this seq if probed.
  370. */
  371. ret = device_find_child_by_seq(parent, seq, true, &dev);
  372. }
  373. return device_get_device_tail(dev, ret, devp);
  374. }
  375. int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
  376. struct udevice **devp)
  377. {
  378. struct udevice *dev;
  379. *devp = NULL;
  380. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  381. if (dev->of_offset == of_offset) {
  382. *devp = dev;
  383. return 0;
  384. }
  385. }
  386. return -ENODEV;
  387. }
  388. int device_get_child_by_of_offset(struct udevice *parent, int node,
  389. struct udevice **devp)
  390. {
  391. struct udevice *dev;
  392. int ret;
  393. *devp = NULL;
  394. ret = device_find_child_by_of_offset(parent, node, &dev);
  395. return device_get_device_tail(dev, ret, devp);
  396. }
  397. static struct udevice *_device_find_global_by_of_offset(struct udevice *parent,
  398. int of_offset)
  399. {
  400. struct udevice *dev, *found;
  401. if (parent->of_offset == of_offset)
  402. return parent;
  403. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  404. found = _device_find_global_by_of_offset(dev, of_offset);
  405. if (found)
  406. return found;
  407. }
  408. return NULL;
  409. }
  410. int device_get_global_by_of_offset(int of_offset, struct udevice **devp)
  411. {
  412. struct udevice *dev;
  413. dev = _device_find_global_by_of_offset(gd->dm_root, of_offset);
  414. return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
  415. }
  416. int device_find_first_child(struct udevice *parent, struct udevice **devp)
  417. {
  418. if (list_empty(&parent->child_head)) {
  419. *devp = NULL;
  420. } else {
  421. *devp = list_first_entry(&parent->child_head, struct udevice,
  422. sibling_node);
  423. }
  424. return 0;
  425. }
  426. int device_find_next_child(struct udevice **devp)
  427. {
  428. struct udevice *dev = *devp;
  429. struct udevice *parent = dev->parent;
  430. if (list_is_last(&dev->sibling_node, &parent->child_head)) {
  431. *devp = NULL;
  432. } else {
  433. *devp = list_entry(dev->sibling_node.next, struct udevice,
  434. sibling_node);
  435. }
  436. return 0;
  437. }
  438. struct udevice *dev_get_parent(struct udevice *child)
  439. {
  440. return child->parent;
  441. }
  442. ulong dev_get_driver_data(struct udevice *dev)
  443. {
  444. return dev->driver_data;
  445. }
  446. const void *dev_get_driver_ops(struct udevice *dev)
  447. {
  448. if (!dev || !dev->driver->ops)
  449. return NULL;
  450. return dev->driver->ops;
  451. }
  452. enum uclass_id device_get_uclass_id(struct udevice *dev)
  453. {
  454. return dev->uclass->uc_drv->id;
  455. }
  456. const char *dev_get_uclass_name(struct udevice *dev)
  457. {
  458. if (!dev)
  459. return NULL;
  460. return dev->uclass->uc_drv->name;
  461. }
  462. #ifdef CONFIG_OF_CONTROL
  463. fdt_addr_t dev_get_addr(struct udevice *dev)
  464. {
  465. return fdtdec_get_addr(gd->fdt_blob, dev->of_offset, "reg");
  466. }
  467. #else
  468. fdt_addr_t dev_get_addr(struct udevice *dev)
  469. {
  470. return FDT_ADDR_T_NONE;
  471. }
  472. #endif
  473. bool device_has_children(struct udevice *dev)
  474. {
  475. return !list_empty(&dev->child_head);
  476. }
  477. bool device_has_active_children(struct udevice *dev)
  478. {
  479. struct udevice *child;
  480. for (device_find_first_child(dev, &child);
  481. child;
  482. device_find_next_child(&child)) {
  483. if (device_active(child))
  484. return true;
  485. }
  486. return false;
  487. }
  488. bool device_is_last_sibling(struct udevice *dev)
  489. {
  490. struct udevice *parent = dev->parent;
  491. if (!parent)
  492. return false;
  493. return list_is_last(&dev->sibling_node, &parent->child_head);
  494. }