device.c 15 KB

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