lists.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 <dm/device.h>
  12. #include <dm/device-internal.h>
  13. #include <dm/lists.h>
  14. #include <dm/platdata.h>
  15. #include <dm/uclass.h>
  16. #include <dm/util.h>
  17. #include <fdtdec.h>
  18. #include <linux/compiler.h>
  19. struct driver *lists_driver_lookup_name(const char *name)
  20. {
  21. struct driver *drv =
  22. ll_entry_start(struct driver, driver);
  23. const int n_ents = ll_entry_count(struct driver, driver);
  24. struct driver *entry;
  25. for (entry = drv; entry != drv + n_ents; entry++) {
  26. if (!strcmp(name, entry->name))
  27. return entry;
  28. }
  29. /* Not found */
  30. return NULL;
  31. }
  32. struct uclass_driver *lists_uclass_lookup(enum uclass_id id)
  33. {
  34. struct uclass_driver *uclass =
  35. ll_entry_start(struct uclass_driver, uclass);
  36. const int n_ents = ll_entry_count(struct uclass_driver, uclass);
  37. struct uclass_driver *entry;
  38. for (entry = uclass; entry != uclass + n_ents; entry++) {
  39. if (entry->id == id)
  40. return entry;
  41. }
  42. return NULL;
  43. }
  44. int lists_bind_drivers(struct udevice *parent, bool pre_reloc_only)
  45. {
  46. struct driver_info *info =
  47. ll_entry_start(struct driver_info, driver_info);
  48. const int n_ents = ll_entry_count(struct driver_info, driver_info);
  49. struct driver_info *entry;
  50. struct udevice *dev;
  51. int result = 0;
  52. int ret;
  53. for (entry = info; entry != info + n_ents; entry++) {
  54. ret = device_bind_by_name(parent, pre_reloc_only, entry, &dev);
  55. if (ret && ret != -EPERM) {
  56. dm_warn("No match for driver '%s'\n", entry->name);
  57. if (!result || ret != -ENOENT)
  58. result = ret;
  59. }
  60. }
  61. return result;
  62. }
  63. int device_bind_driver(struct udevice *parent, const char *drv_name,
  64. const char *dev_name, struct udevice **devp)
  65. {
  66. return device_bind_driver_to_node(parent, drv_name, dev_name,
  67. ofnode_null(), devp);
  68. }
  69. int device_bind_driver_to_node(struct udevice *parent, const char *drv_name,
  70. const char *dev_name, ofnode node,
  71. struct udevice **devp)
  72. {
  73. struct driver *drv;
  74. int ret;
  75. drv = lists_driver_lookup_name(drv_name);
  76. if (!drv) {
  77. debug("Cannot find driver '%s'\n", drv_name);
  78. return -ENOENT;
  79. }
  80. ret = device_bind_with_driver_data(parent, drv, dev_name, 0 /* data */,
  81. node, devp);
  82. return ret;
  83. }
  84. #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
  85. /**
  86. * driver_check_compatible() - Check if a driver matches a compatible string
  87. *
  88. * @param of_match: List of compatible strings to match
  89. * @param of_idp: Returns the match that was found
  90. * @param compat: The compatible string to search for
  91. * @return 0 if there is a match, -ENOENT if no match
  92. */
  93. static int driver_check_compatible(const struct udevice_id *of_match,
  94. const struct udevice_id **of_idp,
  95. const char *compat)
  96. {
  97. if (!of_match)
  98. return -ENOENT;
  99. while (of_match->compatible) {
  100. if (!strcmp(of_match->compatible, compat)) {
  101. *of_idp = of_match;
  102. return 0;
  103. }
  104. of_match++;
  105. }
  106. return -ENOENT;
  107. }
  108. int lists_bind_fdt(struct udevice *parent, ofnode node, struct udevice **devp,
  109. bool pre_reloc_only)
  110. {
  111. struct driver *driver = ll_entry_start(struct driver, driver);
  112. const int n_ents = ll_entry_count(struct driver, driver);
  113. const struct udevice_id *id;
  114. struct driver *entry;
  115. struct udevice *dev;
  116. bool found = false;
  117. const char *name, *compat_list, *compat;
  118. int compat_length, i;
  119. int result = 0;
  120. int ret = 0;
  121. if (devp)
  122. *devp = NULL;
  123. name = ofnode_get_name(node);
  124. log_debug("bind node %s\n", name);
  125. compat_list = ofnode_get_property(node, "compatible", &compat_length);
  126. if (!compat_list) {
  127. if (compat_length == -FDT_ERR_NOTFOUND) {
  128. log_debug("Device '%s' has no compatible string\n",
  129. name);
  130. return 0;
  131. }
  132. dm_warn("Device tree error at node '%s'\n", name);
  133. return compat_length;
  134. }
  135. /*
  136. * Walk through the compatible string list, attempting to match each
  137. * compatible string in order such that we match in order of priority
  138. * from the first string to the last.
  139. */
  140. for (i = 0; i < compat_length; i += strlen(compat) + 1) {
  141. compat = compat_list + i;
  142. log_debug(" - attempt to match compatible string '%s'\n",
  143. compat);
  144. for (entry = driver; entry != driver + n_ents; entry++) {
  145. ret = driver_check_compatible(entry->of_match, &id,
  146. compat);
  147. if (!ret)
  148. break;
  149. }
  150. if (entry == driver + n_ents)
  151. continue;
  152. if (pre_reloc_only) {
  153. if (!ofnode_pre_reloc(node) &&
  154. !(entry->flags & DM_FLAG_PRE_RELOC)) {
  155. log_debug("Skipping device pre-relocation\n");
  156. return 0;
  157. }
  158. }
  159. log_debug(" - found match at '%s': '%s' matches '%s'\n",
  160. entry->name, entry->of_match->compatible,
  161. id->compatible);
  162. ret = device_bind_with_driver_data(parent, entry, name,
  163. id->data, node, &dev);
  164. if (ret == -ENODEV) {
  165. log_debug("Driver '%s' refuses to bind\n", entry->name);
  166. continue;
  167. }
  168. if (ret) {
  169. dm_warn("Error binding driver '%s': %d\n", entry->name,
  170. ret);
  171. return ret;
  172. } else {
  173. found = true;
  174. if (devp)
  175. *devp = dev;
  176. }
  177. break;
  178. }
  179. if (!found && !result && ret != -ENODEV)
  180. log_debug("No match for node '%s'\n", name);
  181. return result;
  182. }
  183. #endif