device.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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. return 0;
  292. fail_uclass:
  293. if (device_remove(dev)) {
  294. dm_warn("%s: Device '%s' failed to remove on error path\n",
  295. __func__, dev->name);
  296. }
  297. fail:
  298. dev->flags &= ~DM_FLAG_ACTIVATED;
  299. dev->seq = -1;
  300. device_free(dev);
  301. return ret;
  302. }
  303. void *dev_get_platdata(struct udevice *dev)
  304. {
  305. if (!dev) {
  306. dm_warn("%s: null device\n", __func__);
  307. return NULL;
  308. }
  309. return dev->platdata;
  310. }
  311. void *dev_get_parent_platdata(struct udevice *dev)
  312. {
  313. if (!dev) {
  314. dm_warn("%s: null device\n", __func__);
  315. return NULL;
  316. }
  317. return dev->parent_platdata;
  318. }
  319. void *dev_get_uclass_platdata(struct udevice *dev)
  320. {
  321. if (!dev) {
  322. dm_warn("%s: null device\n", __func__);
  323. return NULL;
  324. }
  325. return dev->uclass_platdata;
  326. }
  327. void *dev_get_priv(struct udevice *dev)
  328. {
  329. if (!dev) {
  330. dm_warn("%s: null device\n", __func__);
  331. return NULL;
  332. }
  333. return dev->priv;
  334. }
  335. void *dev_get_uclass_priv(struct udevice *dev)
  336. {
  337. if (!dev) {
  338. dm_warn("%s: null device\n", __func__);
  339. return NULL;
  340. }
  341. return dev->uclass_priv;
  342. }
  343. void *dev_get_parent_priv(struct udevice *dev)
  344. {
  345. if (!dev) {
  346. dm_warn("%s: null device\n", __func__);
  347. return NULL;
  348. }
  349. return dev->parent_priv;
  350. }
  351. static int device_get_device_tail(struct udevice *dev, int ret,
  352. struct udevice **devp)
  353. {
  354. if (ret)
  355. return ret;
  356. ret = device_probe(dev);
  357. if (ret)
  358. return ret;
  359. *devp = dev;
  360. return 0;
  361. }
  362. int device_get_child(struct udevice *parent, int index, struct udevice **devp)
  363. {
  364. struct udevice *dev;
  365. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  366. if (!index--)
  367. return device_get_device_tail(dev, 0, devp);
  368. }
  369. return -ENODEV;
  370. }
  371. int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
  372. bool find_req_seq, struct udevice **devp)
  373. {
  374. struct udevice *dev;
  375. *devp = NULL;
  376. if (seq_or_req_seq == -1)
  377. return -ENODEV;
  378. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  379. if ((find_req_seq ? dev->req_seq : dev->seq) ==
  380. seq_or_req_seq) {
  381. *devp = dev;
  382. return 0;
  383. }
  384. }
  385. return -ENODEV;
  386. }
  387. int device_get_child_by_seq(struct udevice *parent, int seq,
  388. struct udevice **devp)
  389. {
  390. struct udevice *dev;
  391. int ret;
  392. *devp = NULL;
  393. ret = device_find_child_by_seq(parent, seq, false, &dev);
  394. if (ret == -ENODEV) {
  395. /*
  396. * We didn't find it in probed devices. See if there is one
  397. * that will request this seq if probed.
  398. */
  399. ret = device_find_child_by_seq(parent, seq, true, &dev);
  400. }
  401. return device_get_device_tail(dev, ret, devp);
  402. }
  403. int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
  404. struct udevice **devp)
  405. {
  406. struct udevice *dev;
  407. *devp = NULL;
  408. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  409. if (dev->of_offset == of_offset) {
  410. *devp = dev;
  411. return 0;
  412. }
  413. }
  414. return -ENODEV;
  415. }
  416. int device_get_child_by_of_offset(struct udevice *parent, int node,
  417. struct udevice **devp)
  418. {
  419. struct udevice *dev;
  420. int ret;
  421. *devp = NULL;
  422. ret = device_find_child_by_of_offset(parent, node, &dev);
  423. return device_get_device_tail(dev, ret, devp);
  424. }
  425. static struct udevice *_device_find_global_by_of_offset(struct udevice *parent,
  426. int of_offset)
  427. {
  428. struct udevice *dev, *found;
  429. if (parent->of_offset == of_offset)
  430. return parent;
  431. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  432. found = _device_find_global_by_of_offset(dev, of_offset);
  433. if (found)
  434. return found;
  435. }
  436. return NULL;
  437. }
  438. int device_get_global_by_of_offset(int of_offset, struct udevice **devp)
  439. {
  440. struct udevice *dev;
  441. dev = _device_find_global_by_of_offset(gd->dm_root, of_offset);
  442. return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
  443. }
  444. int device_find_first_child(struct udevice *parent, struct udevice **devp)
  445. {
  446. if (list_empty(&parent->child_head)) {
  447. *devp = NULL;
  448. } else {
  449. *devp = list_first_entry(&parent->child_head, struct udevice,
  450. sibling_node);
  451. }
  452. return 0;
  453. }
  454. int device_find_next_child(struct udevice **devp)
  455. {
  456. struct udevice *dev = *devp;
  457. struct udevice *parent = dev->parent;
  458. if (list_is_last(&dev->sibling_node, &parent->child_head)) {
  459. *devp = NULL;
  460. } else {
  461. *devp = list_entry(dev->sibling_node.next, struct udevice,
  462. sibling_node);
  463. }
  464. return 0;
  465. }
  466. struct udevice *dev_get_parent(struct udevice *child)
  467. {
  468. return child->parent;
  469. }
  470. ulong dev_get_driver_data(struct udevice *dev)
  471. {
  472. return dev->driver_data;
  473. }
  474. const void *dev_get_driver_ops(struct udevice *dev)
  475. {
  476. if (!dev || !dev->driver->ops)
  477. return NULL;
  478. return dev->driver->ops;
  479. }
  480. enum uclass_id device_get_uclass_id(struct udevice *dev)
  481. {
  482. return dev->uclass->uc_drv->id;
  483. }
  484. const char *dev_get_uclass_name(struct udevice *dev)
  485. {
  486. if (!dev)
  487. return NULL;
  488. return dev->uclass->uc_drv->name;
  489. }
  490. fdt_addr_t dev_get_addr_index(struct udevice *dev, int index)
  491. {
  492. #if CONFIG_IS_ENABLED(OF_CONTROL)
  493. fdt_addr_t addr;
  494. if (CONFIG_IS_ENABLED(OF_TRANSLATE)) {
  495. const fdt32_t *reg;
  496. int len = 0;
  497. int na, ns;
  498. na = fdt_address_cells(gd->fdt_blob, dev->parent->of_offset);
  499. if (na < 1) {
  500. debug("bad #address-cells\n");
  501. return FDT_ADDR_T_NONE;
  502. }
  503. ns = fdt_size_cells(gd->fdt_blob, dev->parent->of_offset);
  504. if (ns < 0) {
  505. debug("bad #size-cells\n");
  506. return FDT_ADDR_T_NONE;
  507. }
  508. reg = fdt_getprop(gd->fdt_blob, dev->of_offset, "reg", &len);
  509. if (!reg || (len <= (index * sizeof(fdt32_t) * (na + ns)))) {
  510. debug("Req index out of range\n");
  511. return FDT_ADDR_T_NONE;
  512. }
  513. reg += index * (na + ns);
  514. /*
  515. * Use the full-fledged translate function for complex
  516. * bus setups.
  517. */
  518. addr = fdt_translate_address((void *)gd->fdt_blob,
  519. dev->of_offset, reg);
  520. } else {
  521. /*
  522. * Use the "simple" translate function for less complex
  523. * bus setups.
  524. */
  525. addr = fdtdec_get_addr_size_auto_parent(gd->fdt_blob,
  526. dev->parent->of_offset,
  527. dev->of_offset, "reg",
  528. index, NULL);
  529. if (CONFIG_IS_ENABLED(SIMPLE_BUS) && addr != FDT_ADDR_T_NONE) {
  530. if (device_get_uclass_id(dev->parent) ==
  531. UCLASS_SIMPLE_BUS)
  532. addr = simple_bus_translate(dev->parent, addr);
  533. }
  534. }
  535. /*
  536. * Some platforms need a special address translation. Those
  537. * platforms (e.g. mvebu in SPL) can configure a translation
  538. * offset in the DM by calling dm_set_translation_offset() that
  539. * will get added to all addresses returned by dev_get_addr().
  540. */
  541. addr += dm_get_translation_offset();
  542. return addr;
  543. #else
  544. return FDT_ADDR_T_NONE;
  545. #endif
  546. }
  547. fdt_addr_t dev_get_addr(struct udevice *dev)
  548. {
  549. return dev_get_addr_index(dev, 0);
  550. }
  551. bool device_has_children(struct udevice *dev)
  552. {
  553. return !list_empty(&dev->child_head);
  554. }
  555. bool device_has_active_children(struct udevice *dev)
  556. {
  557. struct udevice *child;
  558. for (device_find_first_child(dev, &child);
  559. child;
  560. device_find_next_child(&child)) {
  561. if (device_active(child))
  562. return true;
  563. }
  564. return false;
  565. }
  566. bool device_is_last_sibling(struct udevice *dev)
  567. {
  568. struct udevice *parent = dev->parent;
  569. if (!parent)
  570. return false;
  571. return list_is_last(&dev->sibling_node, &parent->child_head);
  572. }
  573. int device_set_name(struct udevice *dev, const char *name)
  574. {
  575. name = strdup(name);
  576. if (!name)
  577. return -ENOMEM;
  578. dev->name = name;
  579. return 0;
  580. }