device.c 13 KB

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