lists.c 4.9 KB

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