device.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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. #include <power-domain.h>
  29. DECLARE_GLOBAL_DATA_PTR;
  30. static int device_bind_common(struct udevice *parent, const struct driver *drv,
  31. const char *name, void *platdata,
  32. ulong driver_data, ofnode node,
  33. uint of_platdata_size, struct udevice **devp)
  34. {
  35. struct udevice *dev;
  36. struct uclass *uc;
  37. int size, ret = 0;
  38. if (devp)
  39. *devp = NULL;
  40. if (!name)
  41. return -EINVAL;
  42. ret = uclass_get(drv->id, &uc);
  43. if (ret) {
  44. debug("Missing uclass for driver %s\n", drv->name);
  45. return ret;
  46. }
  47. dev = calloc(1, sizeof(struct udevice));
  48. if (!dev)
  49. return -ENOMEM;
  50. INIT_LIST_HEAD(&dev->sibling_node);
  51. INIT_LIST_HEAD(&dev->child_head);
  52. INIT_LIST_HEAD(&dev->uclass_node);
  53. #ifdef CONFIG_DEVRES
  54. INIT_LIST_HEAD(&dev->devres_head);
  55. #endif
  56. dev->platdata = platdata;
  57. dev->driver_data = driver_data;
  58. dev->name = name;
  59. dev->node = node;
  60. dev->parent = parent;
  61. dev->driver = drv;
  62. dev->uclass = uc;
  63. dev->seq = -1;
  64. dev->req_seq = -1;
  65. if (CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(DM_SEQ_ALIAS)) {
  66. /*
  67. * Some devices, such as a SPI bus, I2C bus and serial ports
  68. * are numbered using aliases.
  69. *
  70. * This is just a 'requested' sequence, and will be
  71. * resolved (and ->seq updated) when the device is probed.
  72. */
  73. if (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS) {
  74. if (uc->uc_drv->name && ofnode_valid(node)) {
  75. dev_read_alias_seq(dev, &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. pr_debug("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, ofnode node,
  197. struct udevice **devp)
  198. {
  199. return device_bind_common(parent, drv, name, NULL, driver_data, node,
  200. 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_ofnode(struct udevice *parent, const struct driver *drv,
  210. const char *name, void *platdata, ofnode node,
  211. struct udevice **devp)
  212. {
  213. return device_bind_common(parent, drv, name, platdata, 0, node, 0,
  214. devp);
  215. }
  216. int device_bind_by_name(struct udevice *parent, bool pre_reloc_only,
  217. const struct driver_info *info, struct udevice **devp)
  218. {
  219. struct driver *drv;
  220. uint platdata_size = 0;
  221. drv = lists_driver_lookup_name(info->name);
  222. if (!drv)
  223. return -ENOENT;
  224. if (pre_reloc_only && !(drv->flags & DM_FLAG_PRE_RELOC))
  225. return -EPERM;
  226. #if CONFIG_IS_ENABLED(OF_PLATDATA)
  227. platdata_size = info->platdata_size;
  228. #endif
  229. return device_bind_common(parent, drv, info->name,
  230. (void *)info->platdata, 0, ofnode_null(), platdata_size,
  231. devp);
  232. }
  233. static void *alloc_priv(int size, uint flags)
  234. {
  235. void *priv;
  236. if (flags & DM_FLAG_ALLOC_PRIV_DMA) {
  237. size = ROUND(size, ARCH_DMA_MINALIGN);
  238. priv = memalign(ARCH_DMA_MINALIGN, size);
  239. if (priv) {
  240. memset(priv, '\0', size);
  241. /*
  242. * Ensure that the zero bytes are flushed to memory.
  243. * This prevents problems if the driver uses this as
  244. * both an input and an output buffer:
  245. *
  246. * 1. Zeroes written to buffer (here) and sit in the
  247. * cache
  248. * 2. Driver issues a read command to DMA
  249. * 3. CPU runs out of cache space and evicts some cache
  250. * data in the buffer, writing zeroes to RAM from
  251. * the memset() above
  252. * 4. DMA completes
  253. * 5. Buffer now has some DMA data and some zeroes
  254. * 6. Data being read is now incorrect
  255. *
  256. * To prevent this, ensure that the cache is clean
  257. * within this range at the start. The driver can then
  258. * use normal flush-after-write, invalidate-before-read
  259. * procedures.
  260. *
  261. * TODO(sjg@chromium.org): Drop this microblaze
  262. * exception.
  263. */
  264. #ifndef CONFIG_MICROBLAZE
  265. flush_dcache_range((ulong)priv, (ulong)priv + size);
  266. #endif
  267. }
  268. } else {
  269. priv = calloc(1, size);
  270. }
  271. return priv;
  272. }
  273. int device_probe(struct udevice *dev)
  274. {
  275. struct power_domain pd;
  276. const struct driver *drv;
  277. int size = 0;
  278. int ret;
  279. int seq;
  280. if (!dev)
  281. return -EINVAL;
  282. if (dev->flags & DM_FLAG_ACTIVATED)
  283. return 0;
  284. drv = dev->driver;
  285. assert(drv);
  286. /* Allocate private data if requested and not reentered */
  287. if (drv->priv_auto_alloc_size && !dev->priv) {
  288. dev->priv = alloc_priv(drv->priv_auto_alloc_size, drv->flags);
  289. if (!dev->priv) {
  290. ret = -ENOMEM;
  291. goto fail;
  292. }
  293. }
  294. /* Allocate private data if requested and not reentered */
  295. size = dev->uclass->uc_drv->per_device_auto_alloc_size;
  296. if (size && !dev->uclass_priv) {
  297. dev->uclass_priv = calloc(1, size);
  298. if (!dev->uclass_priv) {
  299. ret = -ENOMEM;
  300. goto fail;
  301. }
  302. }
  303. /* Ensure all parents are probed */
  304. if (dev->parent) {
  305. size = dev->parent->driver->per_child_auto_alloc_size;
  306. if (!size) {
  307. size = dev->parent->uclass->uc_drv->
  308. per_child_auto_alloc_size;
  309. }
  310. if (size && !dev->parent_priv) {
  311. dev->parent_priv = alloc_priv(size, drv->flags);
  312. if (!dev->parent_priv) {
  313. ret = -ENOMEM;
  314. goto fail;
  315. }
  316. }
  317. ret = device_probe(dev->parent);
  318. if (ret)
  319. goto fail;
  320. /*
  321. * The device might have already been probed during
  322. * the call to device_probe() on its parent device
  323. * (e.g. PCI bridge devices). Test the flags again
  324. * so that we don't mess up the device.
  325. */
  326. if (dev->flags & DM_FLAG_ACTIVATED)
  327. return 0;
  328. }
  329. seq = uclass_resolve_seq(dev);
  330. if (seq < 0) {
  331. ret = seq;
  332. goto fail;
  333. }
  334. dev->seq = seq;
  335. dev->flags |= DM_FLAG_ACTIVATED;
  336. /*
  337. * Process pinctrl for everything except the root device, and
  338. * continue regardless of the result of pinctrl. Don't process pinctrl
  339. * settings for pinctrl devices since the device may not yet be
  340. * probed.
  341. */
  342. if (dev->parent && device_get_uclass_id(dev) != UCLASS_PINCTRL)
  343. pinctrl_select_state(dev, "default");
  344. if (dev->parent && device_get_uclass_id(dev) != UCLASS_POWER_DOMAIN) {
  345. if (!power_domain_get(dev, &pd))
  346. power_domain_on(&pd);
  347. }
  348. ret = uclass_pre_probe_device(dev);
  349. if (ret)
  350. goto fail;
  351. if (dev->parent && dev->parent->driver->child_pre_probe) {
  352. ret = dev->parent->driver->child_pre_probe(dev);
  353. if (ret)
  354. goto fail;
  355. }
  356. if (drv->ofdata_to_platdata && dev_has_of_node(dev)) {
  357. ret = drv->ofdata_to_platdata(dev);
  358. if (ret)
  359. goto fail;
  360. }
  361. /* Process 'assigned-{clocks/clock-parents/clock-rates}' properties */
  362. ret = clk_set_defaults(dev);
  363. if (ret)
  364. goto fail;
  365. if (drv->probe) {
  366. ret = drv->probe(dev);
  367. if (ret) {
  368. dev->flags &= ~DM_FLAG_ACTIVATED;
  369. goto fail;
  370. }
  371. }
  372. ret = uclass_post_probe_device(dev);
  373. if (ret)
  374. goto fail_uclass;
  375. if (dev->parent && device_get_uclass_id(dev) == UCLASS_PINCTRL)
  376. pinctrl_select_state(dev, "default");
  377. return 0;
  378. fail_uclass:
  379. if (device_remove(dev, DM_REMOVE_NORMAL)) {
  380. dm_warn("%s: Device '%s' failed to remove on error path\n",
  381. __func__, dev->name);
  382. }
  383. fail:
  384. dev->flags &= ~DM_FLAG_ACTIVATED;
  385. dev->seq = -1;
  386. device_free(dev);
  387. return ret;
  388. }
  389. void *dev_get_platdata(struct udevice *dev)
  390. {
  391. if (!dev) {
  392. dm_warn("%s: null device\n", __func__);
  393. return NULL;
  394. }
  395. return dev->platdata;
  396. }
  397. void *dev_get_parent_platdata(struct udevice *dev)
  398. {
  399. if (!dev) {
  400. dm_warn("%s: null device\n", __func__);
  401. return NULL;
  402. }
  403. return dev->parent_platdata;
  404. }
  405. void *dev_get_uclass_platdata(struct udevice *dev)
  406. {
  407. if (!dev) {
  408. dm_warn("%s: null device\n", __func__);
  409. return NULL;
  410. }
  411. return dev->uclass_platdata;
  412. }
  413. void *dev_get_priv(struct udevice *dev)
  414. {
  415. if (!dev) {
  416. dm_warn("%s: null device\n", __func__);
  417. return NULL;
  418. }
  419. return dev->priv;
  420. }
  421. void *dev_get_uclass_priv(struct udevice *dev)
  422. {
  423. if (!dev) {
  424. dm_warn("%s: null device\n", __func__);
  425. return NULL;
  426. }
  427. return dev->uclass_priv;
  428. }
  429. void *dev_get_parent_priv(struct udevice *dev)
  430. {
  431. if (!dev) {
  432. dm_warn("%s: null device\n", __func__);
  433. return NULL;
  434. }
  435. return dev->parent_priv;
  436. }
  437. static int device_get_device_tail(struct udevice *dev, int ret,
  438. struct udevice **devp)
  439. {
  440. if (ret)
  441. return ret;
  442. ret = device_probe(dev);
  443. if (ret)
  444. return ret;
  445. *devp = dev;
  446. return 0;
  447. }
  448. int device_get_child(struct udevice *parent, int index, struct udevice **devp)
  449. {
  450. struct udevice *dev;
  451. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  452. if (!index--)
  453. return device_get_device_tail(dev, 0, devp);
  454. }
  455. return -ENODEV;
  456. }
  457. int device_find_child_by_seq(struct udevice *parent, int seq_or_req_seq,
  458. bool find_req_seq, struct udevice **devp)
  459. {
  460. struct udevice *dev;
  461. *devp = NULL;
  462. if (seq_or_req_seq == -1)
  463. return -ENODEV;
  464. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  465. if ((find_req_seq ? dev->req_seq : dev->seq) ==
  466. seq_or_req_seq) {
  467. *devp = dev;
  468. return 0;
  469. }
  470. }
  471. return -ENODEV;
  472. }
  473. int device_get_child_by_seq(struct udevice *parent, int seq,
  474. struct udevice **devp)
  475. {
  476. struct udevice *dev;
  477. int ret;
  478. *devp = NULL;
  479. ret = device_find_child_by_seq(parent, seq, false, &dev);
  480. if (ret == -ENODEV) {
  481. /*
  482. * We didn't find it in probed devices. See if there is one
  483. * that will request this seq if probed.
  484. */
  485. ret = device_find_child_by_seq(parent, seq, true, &dev);
  486. }
  487. return device_get_device_tail(dev, ret, devp);
  488. }
  489. int device_find_child_by_of_offset(struct udevice *parent, int of_offset,
  490. struct udevice **devp)
  491. {
  492. struct udevice *dev;
  493. *devp = NULL;
  494. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  495. if (dev_of_offset(dev) == of_offset) {
  496. *devp = dev;
  497. return 0;
  498. }
  499. }
  500. return -ENODEV;
  501. }
  502. int device_get_child_by_of_offset(struct udevice *parent, int node,
  503. struct udevice **devp)
  504. {
  505. struct udevice *dev;
  506. int ret;
  507. *devp = NULL;
  508. ret = device_find_child_by_of_offset(parent, node, &dev);
  509. return device_get_device_tail(dev, ret, devp);
  510. }
  511. static struct udevice *_device_find_global_by_ofnode(struct udevice *parent,
  512. ofnode ofnode)
  513. {
  514. struct udevice *dev, *found;
  515. if (ofnode_equal(dev_ofnode(parent), ofnode))
  516. return parent;
  517. list_for_each_entry(dev, &parent->child_head, sibling_node) {
  518. found = _device_find_global_by_ofnode(dev, ofnode);
  519. if (found)
  520. return found;
  521. }
  522. return NULL;
  523. }
  524. int device_find_global_by_ofnode(ofnode ofnode, struct udevice **devp)
  525. {
  526. *devp = _device_find_global_by_ofnode(gd->dm_root, ofnode);
  527. return *devp ? 0 : -ENOENT;
  528. }
  529. int device_get_global_by_ofnode(ofnode ofnode, struct udevice **devp)
  530. {
  531. struct udevice *dev;
  532. dev = _device_find_global_by_ofnode(gd->dm_root, ofnode);
  533. return device_get_device_tail(dev, dev ? 0 : -ENOENT, devp);
  534. }
  535. int device_find_first_child(struct udevice *parent, struct udevice **devp)
  536. {
  537. if (list_empty(&parent->child_head)) {
  538. *devp = NULL;
  539. } else {
  540. *devp = list_first_entry(&parent->child_head, struct udevice,
  541. sibling_node);
  542. }
  543. return 0;
  544. }
  545. int device_find_next_child(struct udevice **devp)
  546. {
  547. struct udevice *dev = *devp;
  548. struct udevice *parent = dev->parent;
  549. if (list_is_last(&dev->sibling_node, &parent->child_head)) {
  550. *devp = NULL;
  551. } else {
  552. *devp = list_entry(dev->sibling_node.next, struct udevice,
  553. sibling_node);
  554. }
  555. return 0;
  556. }
  557. struct udevice *dev_get_parent(struct udevice *child)
  558. {
  559. return child->parent;
  560. }
  561. ulong dev_get_driver_data(struct udevice *dev)
  562. {
  563. return dev->driver_data;
  564. }
  565. const void *dev_get_driver_ops(struct udevice *dev)
  566. {
  567. if (!dev || !dev->driver->ops)
  568. return NULL;
  569. return dev->driver->ops;
  570. }
  571. enum uclass_id device_get_uclass_id(struct udevice *dev)
  572. {
  573. return dev->uclass->uc_drv->id;
  574. }
  575. const char *dev_get_uclass_name(struct udevice *dev)
  576. {
  577. if (!dev)
  578. return NULL;
  579. return dev->uclass->uc_drv->name;
  580. }
  581. bool device_has_children(struct udevice *dev)
  582. {
  583. return !list_empty(&dev->child_head);
  584. }
  585. bool device_has_active_children(struct udevice *dev)
  586. {
  587. struct udevice *child;
  588. for (device_find_first_child(dev, &child);
  589. child;
  590. device_find_next_child(&child)) {
  591. if (device_active(child))
  592. return true;
  593. }
  594. return false;
  595. }
  596. bool device_is_last_sibling(struct udevice *dev)
  597. {
  598. struct udevice *parent = dev->parent;
  599. if (!parent)
  600. return false;
  601. return list_is_last(&dev->sibling_node, &parent->child_head);
  602. }
  603. void device_set_name_alloced(struct udevice *dev)
  604. {
  605. dev->flags |= DM_FLAG_NAME_ALLOCED;
  606. }
  607. int device_set_name(struct udevice *dev, const char *name)
  608. {
  609. name = strdup(name);
  610. if (!name)
  611. return -ENOMEM;
  612. dev->name = name;
  613. device_set_name_alloced(dev);
  614. return 0;
  615. }
  616. bool device_is_compatible(struct udevice *dev, const char *compat)
  617. {
  618. return ofnode_device_is_compatible(dev_ofnode(dev), compat);
  619. }
  620. bool of_machine_is_compatible(const char *compat)
  621. {
  622. const void *fdt = gd->fdt_blob;
  623. return !fdt_node_check_compatible(fdt, 0, compat);
  624. }