lists.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (c) 2013 Google, Inc
  3. *
  4. * (C) Copyright 2012
  5. * Marek Vasut <marex@denx.de>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  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. {
  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. dm_dbg("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. dm_dbg("Device '%s' has no compatible string\n", name);
  128. return 0;
  129. }
  130. dm_warn("Device tree error at node '%s'\n", name);
  131. return compat_length;
  132. }
  133. /*
  134. * Walk through the compatible string list, attempting to match each
  135. * compatible string in order such that we match in order of priority
  136. * from the first string to the last.
  137. */
  138. for (i = 0; i < compat_length; i += strlen(compat) + 1) {
  139. compat = compat_list + i;
  140. dm_dbg(" - attempt to match compatible string '%s'\n",
  141. compat);
  142. for (entry = driver; entry != driver + n_ents; entry++) {
  143. ret = driver_check_compatible(entry->of_match, &id,
  144. compat);
  145. if (!ret)
  146. break;
  147. }
  148. if (entry == driver + n_ents)
  149. continue;
  150. dm_dbg(" - found match at '%s'\n", entry->name);
  151. ret = device_bind_with_driver_data(parent, entry, name,
  152. id->data, node, &dev);
  153. if (ret == -ENODEV) {
  154. dm_dbg("Driver '%s' refuses to bind\n", entry->name);
  155. continue;
  156. }
  157. if (ret) {
  158. dm_warn("Error binding driver '%s': %d\n", entry->name,
  159. ret);
  160. return ret;
  161. } else {
  162. found = true;
  163. if (devp)
  164. *devp = dev;
  165. }
  166. break;
  167. }
  168. if (!found && !result && ret != -ENODEV)
  169. dm_dbg("No match for node '%s'\n", name);
  170. return result;
  171. }
  172. #endif