device.c 12 KB

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