device.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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_by_name(struct udevice *parent, bool pre_reloc_only,
  209. const struct driver_info *info, struct udevice **devp)
  210. {
  211. struct driver *drv;
  212. uint platdata_size = 0;
  213. drv = lists_driver_lookup_name(info->name);
  214. if (!drv)
  215. return -ENOENT;
  216. if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
  217. return -EPERM;
  218. #if CONFIG_IS_ENABLED(OF_PLATDATA)
  219. platdata_size = info->platdata_size;
  220. #endif
  221. return device_bind_common(parent, drv, info->name,
  222. (void *)info->platdata, 0, ofnode_null(), platdata_size,
  223. devp);
  224. }
  225. static void *alloc_priv(int size, uint flags)
  226. {
  227. void *priv;
  228. if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
  229. size = ROUND(size, ARCH_DMA_MINALIGN);
  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_has_of_node(dev)) {
  344. ret = drv->ofdata_to_platdata(dev);
  345. if (ret)
  346. goto fail;
  347. }
  348. /* Process 'assigned-{clocks/clock-parents/clock-rates}' properties */
  349. ret = clk_set_defaults(dev);
  350. if (ret)
  351. goto fail;
  352. if (drv->probe) {
  353. ret = drv->probe(dev);
  354. if (ret) {
  355. dev->flags &= ~DM_FLAG_ACTIVATED;
  356. goto fail;
  357. }
  358. }
  359. ret = uclass_post_probe_device(dev);
  360. if (ret)
  361. goto fail_uclass;
  362. if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
  363. pinctrl_select_state(dev, "default");
  364. return 0;
  365. fail_uclass:
  366. if (device_remove(dev, DM_REMOVE_NORMAL)) {
  367. dm_warn("%s: Device '%s' failed to remove on error path\n",
  368. __func__, dev->name);
  369. }
  370. fail:
  371. dev->flags &= ~DM_FLAG_ACTIVATED;
  372. dev->seq = -1;
  373. device_free(dev);
  374. return ret;
  375. }
  376. void *dev_get_platdata(struct udevice *dev)
  377. {
  378. if (!dev) {
  379. dm_warn("%s: null device\n", __func__);
  380. return NULL;
  381. }
  382. return dev->platdata;
  383. }
  384. void *dev_get_parent_platdata(struct udevice *dev)
  385. {
  386. if (!dev) {
  387. dm_warn("%s: null device\n", __func__);
  388. return NULL;
  389. }
  390. return dev->parent_platdata;
  391. }
  392. void *dev_get_uclass_platdata(struct udevice *dev)
  393. {
  394. if (!dev) {
  395. dm_warn("%s: null device\n", __func__);
  396. return NULL;
  397. }
  398. return dev->uclass_platdata;
  399. }
  400. void *dev_get_priv(struct udevice *dev)
  401. {
  402. if (!dev) {
  403. dm_warn("%s: null device\n", __func__);
  404. return NULL;
  405. }
  406. return dev->priv;
  407. }
  408. void *dev_get_uclass_priv(struct udevice *dev)
  409. {
  410. if (!dev) {
  411. dm_warn("%s: null device\n", __func__);
  412. return NULL;
  413. }
  414. return dev->uclass_priv;
  415. }
  416. void *dev_get_parent_priv(struct udevice *dev)
  417. {
  418. if (!dev) {
  419. dm_warn("%s: null device\n", __func__);
  420. return NULL;
  421. }
  422. return dev->parent_priv;
  423. }
  424. static int device_get_device_tail(struct udevice *dev, int ret,
  425. struct udevice **devp)
  426. {
  427. if (ret)
  428. return ret;
  429. ret = device_probe(dev);
  430. if (ret)
  431. return ret;
  432. *devp = dev;
  433. return 0;
  434. }
  435. int device_get_child(struct udevice *parent, int index, struct udevice **devp)
  436. {
  437. struct udevice *dev;
  438. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  439. if (!index--)
  440. return device_get_device_tail(dev, 0, devp);
  441. }
  442. return -ENODEV;
  443. }
  444. int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
  445. bool find_req_seq, struct udevice **devp)
  446. {
  447. struct udevice *dev;
  448. *devp = NULL;
  449. if (seq_or_req_seq == -1)
  450. return -ENODEV;
  451. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  452. if ((find_req_seq ? dev->req_seq : dev->seq) ==
  453. seq_or_req_seq) {
  454. *devp = dev;
  455. return 0;
  456. }
  457. }
  458. return -ENODEV;
  459. }
  460. int device_get_child_by_seq(struct udevice *parent, int seq,
  461. struct udevice **devp)
  462. {
  463. struct udevice *dev;
  464. int ret;
  465. *devp = NULL;
  466. ret = device_find_child_by_seq(parent, seq, false, &dev);
  467. if (ret == -ENODEV) {
  468. /*
  469. * We didn't find it in probed devices. See if there is one
  470. * that will request this seq if probed.
  471. */
  472. ret = device_find_child_by_seq(parent, seq, true, &dev);
  473. }
  474. return device_get_device_tail(dev, ret, devp);
  475. }
  476. int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
  477. struct udevice **devp)
  478. {
  479. struct udevice *dev;
  480. *devp = NULL;
  481. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  482. if (dev_of_offset(dev) == of_offset) {
  483. *devp = dev;
  484. return 0;
  485. }
  486. }
  487. return -ENODEV;
  488. }
  489. int device_get_child_by_of_offset(struct udevice *parent, int node,
  490. struct udevice **devp)
  491. {
  492. struct udevice *dev;
  493. int ret;
  494. *devp = NULL;
  495. ret = device_find_child_by_of_offset(parent, node, &dev);
  496. return device_get_device_tail(dev, ret, devp);
  497. }
  498. static struct udevice *_device_find_global_by_of_offset(struct udevice *parent,
  499. int of_offset)
  500. {
  501. struct udevice *dev, *found;
  502. if (dev_of_offset(parent) == of_offset)
  503. return parent;
  504. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  505. found = _device_find_global_by_of_offset(dev, of_offset);
  506. if (found)
  507. return found;
  508. }
  509. return NULL;
  510. }
  511. int device_get_global_by_of_offset(int of_offset, struct udevice **devp)
  512. {
  513. struct udevice *dev;
  514. dev = _device_find_global_by_of_offset(gd->dm_root, of_offset);
  515. return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
  516. }
  517. int device_find_first_child(struct udevice *parent, struct udevice **devp)
  518. {
  519. if (list_empty(&parent->child_head)) {
  520. *devp = NULL;
  521. } else {
  522. *devp = list_first_entry(&parent->child_head, struct udevice,
  523. sibling_node);
  524. }
  525. return 0;
  526. }
  527. int device_find_next_child(struct udevice **devp)
  528. {
  529. struct udevice *dev = *devp;
  530. struct udevice *parent = dev->parent;
  531. if (list_is_last(&dev->sibling_node, &parent->child_head)) {
  532. *devp = NULL;
  533. } else {
  534. *devp = list_entry(dev->sibling_node.next, struct udevice,
  535. sibling_node);
  536. }
  537. return 0;
  538. }
  539. struct udevice *dev_get_parent(struct udevice *child)
  540. {
  541. return child->parent;
  542. }
  543. ulong dev_get_driver_data(struct udevice *dev)
  544. {
  545. return dev->driver_data;
  546. }
  547. const void *dev_get_driver_ops(struct udevice *dev)
  548. {
  549. if (!dev || !dev->driver->ops)
  550. return NULL;
  551. return dev->driver->ops;
  552. }
  553. enum uclass_id device_get_uclass_id(struct udevice *dev)
  554. {
  555. return dev->uclass->uc_drv->id;
  556. }
  557. const char *dev_get_uclass_name(struct udevice *dev)
  558. {
  559. if (!dev)
  560. return NULL;
  561. return dev->uclass->uc_drv->name;
  562. }
  563. bool device_has_children(struct udevice *dev)
  564. {
  565. return !list_empty(&dev->child_head);
  566. }
  567. bool device_has_active_children(struct udevice *dev)
  568. {
  569. struct udevice *child;
  570. for (device_find_first_child(dev, &child);
  571. child;
  572. device_find_next_child(&child)) {
  573. if (device_active(child))
  574. return true;
  575. }
  576. return false;
  577. }
  578. bool device_is_last_sibling(struct udevice *dev)
  579. {
  580. struct udevice *parent = dev->parent;
  581. if (!parent)
  582. return false;
  583. return list_is_last(&dev->sibling_node, &parent->child_head);
  584. }
  585. void device_set_name_alloced(struct udevice *dev)
  586. {
  587. dev->flags |= DM_FLAG_NAME_ALLOCED;
  588. }
  589. int device_set_name(struct udevice *dev, const char *name)
  590. {
  591. name = strdup(name);
  592. if (!name)
  593. return -ENOMEM;
  594. dev->name = name;
  595. device_set_name_alloced(dev);
  596. return 0;
  597. }
  598. bool device_is_compatible(struct udevice *dev, const char *compat)
  599. {
  600. return ofnode_device_is_compatible(dev_ofnode(dev), compat);
  601. }
  602. bool of_machine_is_compatible(const char *compat)
  603. {
  604. const void *fdt = gd->fdt_blob;
  605. return !fdt_node_check_compatible(fdt, 0, compat);
  606. }