device.c 15 KB

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