device.c 12 KB

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