device.c 14 KB

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