lists.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2013 Google, Inc
  4. *
  5. * (C) Copyright 2012
  6. * Marek Vasut <marex@denx.de>
  7. */
  8. #define LOG_CATEGORY LOGC_DM
  9. #include <common.h>
  10. #include <errno.h>
  11. #include <log.h>
  12. #include <dm/device.h>
  13. #include <dm/device-internal.h>
  14. #include <dm/lists.h>
  15. #include <dm/platdata.h>
  16. #include <dm/uclass.h>
  17. #include <dm/util.h>
  18. #include <fdtdec.h>
  19. #include <linux/compiler.h>
  20. struct driver *lists_driver_lookup_name(const char *name)
  21. {
  22. struct driver *drv =
  23. ll_entry_start(struct driver, driver);
  24. const int n_ents = ll_entry_count(struct driver, driver);
  25. struct driver *entry;
  26. for (entry = drv; entry != drv + n_ents; entry++) {
  27. if (!strcmp(name, entry->name))
  28. return entry;
  29. }
  30. /* Not found */
  31. return NULL;
  32. }
  33. struct uclass_driver *lists_uclass_lookup(enum uclass_id id)
  34. {
  35. struct uclass_driver *uclass =
  36. ll_entry_start(struct uclass_driver, uclass);
  37. const int n_ents = ll_entry_count(struct uclass_driver, uclass);
  38. struct uclass_driver *entry;
  39. for (entry = uclass; entry != uclass + n_ents; entry++) {
  40. if (entry->id == id)
  41. return entry;
  42. }
  43. return NULL;
  44. }
  45. static int bind_drivers_pass(struct udevice *parent, bool pre_reloc_only)
  46. {
  47. struct driver_info *info =
  48. ll_entry_start(struct driver_info, driver_info);
  49. const int n_ents = ll_entry_count(struct driver_info, driver_info);
  50. bool missing_parent = false;
  51. int result = 0;
  52. uint idx;
  53. /*
  54. * Do one iteration through the driver_info records. For of-platdata,
  55. * bind only devices whose parent is already bound. If we find any
  56. * device we can't bind, set missing_parent to true, which will cause
  57. * this function to be called again.
  58. */
  59. for (idx = 0; idx < n_ents; idx++) {
  60. struct udevice *par = parent;
  61. const struct driver_info *entry = info + idx;
  62. struct driver_rt *drt = gd_dm_driver_rt() + idx;
  63. struct udevice *dev;
  64. int ret;
  65. if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
  66. int parent_idx = driver_info_parent_id(entry);
  67. if (drt->dev)
  68. continue;
  69. if (CONFIG_IS_ENABLED(OF_PLATDATA_PARENT) &&
  70. parent_idx != -1) {
  71. struct driver_rt *parent_drt;
  72. parent_drt = gd_dm_driver_rt() + parent_idx;
  73. if (!parent_drt->dev) {
  74. missing_parent = true;
  75. continue;
  76. }
  77. par = parent_drt->dev;
  78. }
  79. }
  80. ret = device_bind_by_name(par, pre_reloc_only, entry, &dev);
  81. if (!ret) {
  82. if (CONFIG_IS_ENABLED(OF_PLATDATA))
  83. drt->dev = dev;
  84. } else if (ret != -EPERM) {
  85. dm_warn("No match for driver '%s'\n", entry->name);
  86. if (!result || ret != -ENOENT)
  87. result = ret;
  88. }
  89. }
  90. return result ? result : missing_parent ? -EAGAIN : 0;
  91. }
  92. int lists_bind_drivers(struct udevice *parent, bool pre_reloc_only)
  93. {
  94. int result = 0;
  95. int pass;
  96. /*
  97. * 10 passes is 10 levels deep in the devicetree, which is plenty. If
  98. * OF_PLATDATA_PARENT is not enabled, then bind_drivers_pass() will
  99. * always succeed on the first pass.
  100. */
  101. for (pass = 0; pass < 10; pass++) {
  102. int ret;
  103. ret = bind_drivers_pass(parent, pre_reloc_only);
  104. if (!ret)
  105. break;
  106. if (ret != -EAGAIN && !result)
  107. result = ret;
  108. }
  109. return result;
  110. }
  111. int device_bind_driver(struct udevice *parent, const char *drv_name,
  112. const char *dev_name, struct udevice **devp)
  113. {
  114. return device_bind_driver_to_node(parent, drv_name, dev_name,
  115. ofnode_null(), devp);
  116. }
  117. int device_bind_driver_to_node(struct udevice *parent, const char *drv_name,
  118. const char *dev_name, ofnode node,
  119. struct udevice **devp)
  120. {
  121. struct driver *drv;
  122. int ret;
  123. drv = lists_driver_lookup_name(drv_name);
  124. if (!drv) {
  125. debug("Cannot find driver '%s'\n", drv_name);
  126. return -ENOENT;
  127. }
  128. ret = device_bind_with_driver_data(parent, drv, dev_name, 0 /* data */,
  129. node, devp);
  130. return ret;
  131. }
  132. #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
  133. /**
  134. * driver_check_compatible() - Check if a driver matches a compatible string
  135. *
  136. * @param of_match: List of compatible strings to match
  137. * @param of_idp: Returns the match that was found
  138. * @param compat: The compatible string to search for
  139. * @return 0 if there is a match, -ENOENT if no match
  140. */
  141. static int driver_check_compatible(const struct udevice_id *of_match,
  142. const struct udevice_id **of_idp,
  143. const char *compat)
  144. {
  145. if (!of_match)
  146. return -ENOENT;
  147. while (of_match->compatible) {
  148. if (!strcmp(of_match->compatible, compat)) {
  149. *of_idp = of_match;
  150. return 0;
  151. }
  152. of_match++;
  153. }
  154. return -ENOENT;
  155. }
  156. int lists_bind_fdt(struct udevice *parent, ofnode node, struct udevice **devp,
  157. bool pre_reloc_only)
  158. {
  159. struct driver *driver = ll_entry_start(struct driver, driver);
  160. const int n_ents = ll_entry_count(struct driver, driver);
  161. const struct udevice_id *id;
  162. struct driver *entry;
  163. struct udevice *dev;
  164. bool found = false;
  165. const char *name, *compat_list, *compat;
  166. int compat_length, i;
  167. int result = 0;
  168. int ret = 0;
  169. if (devp)
  170. *devp = NULL;
  171. name = ofnode_get_name(node);
  172. log_debug("bind node %s\n", name);
  173. compat_list = ofnode_get_property(node, "compatible", &compat_length);
  174. if (!compat_list) {
  175. if (compat_length == -FDT_ERR_NOTFOUND) {
  176. log_debug("Device '%s' has no compatible string\n",
  177. name);
  178. return 0;
  179. }
  180. dm_warn("Device tree error at node '%s'\n", name);
  181. return compat_length;
  182. }
  183. /*
  184. * Walk through the compatible string list, attempting to match each
  185. * compatible string in order such that we match in order of priority
  186. * from the first string to the last.
  187. */
  188. for (i = 0; i < compat_length; i += strlen(compat) + 1) {
  189. compat = compat_list + i;
  190. log_debug(" - attempt to match compatible string '%s'\n",
  191. compat);
  192. for (entry = driver; entry != driver + n_ents; entry++) {
  193. ret = driver_check_compatible(entry->of_match, &id,
  194. compat);
  195. if (!ret)
  196. break;
  197. }
  198. if (entry == driver + n_ents)
  199. continue;
  200. if (pre_reloc_only) {
  201. if (!ofnode_pre_reloc(node) &&
  202. !(entry->flags & DM_FLAG_PRE_RELOC)) {
  203. log_debug("Skipping device pre-relocation\n");
  204. return 0;
  205. }
  206. }
  207. log_debug(" - found match at '%s': '%s' matches '%s'\n",
  208. entry->name, entry->of_match->compatible,
  209. id->compatible);
  210. ret = device_bind_with_driver_data(parent, entry, name,
  211. id->data, node, &dev);
  212. if (ret == -ENODEV) {
  213. log_debug("Driver '%s' refuses to bind\n", entry->name);
  214. continue;
  215. }
  216. if (ret) {
  217. dm_warn("Error binding driver '%s': %d\n", entry->name,
  218. ret);
  219. return ret;
  220. } else {
  221. found = true;
  222. if (devp)
  223. *devp = dev;
  224. }
  225. break;
  226. }
  227. if (!found && !result && ret != -ENODEV)
  228. log_debug("No match for node '%s'\n", name);
  229. return result;
  230. }
  231. #endif