device.c 15 KB

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