lists.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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, -1, devp);
  67. }
  68. int device_bind_driver_to_node(struct udevice *parent, const char *drv_name,
  69. const char *dev_name, int 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(parent, drv, dev_name, NULL, node, devp);
  80. if (ret) {
  81. debug("Cannot create device named '%s' (err=%d)\n",
  82. dev_name, ret);
  83. return ret;
  84. }
  85. return 0;
  86. }
  87. #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
  88. /**
  89. * driver_check_compatible() - Check if a driver matches a compatible string
  90. *
  91. * @param of_match: List of compatible strings to match
  92. * @param of_idp: Returns the match that was found
  93. * @param compat: The compatible string to search for
  94. * @return 0 if there is a match, -ENOENT if no match
  95. */
  96. static int driver_check_compatible(const struct udevice_id *of_match,
  97. const struct udevice_id **of_idp,
  98. const char *compat)
  99. {
  100. if (!of_match)
  101. return -ENOENT;
  102. while (of_match->compatible) {
  103. if (!strcmp(of_match->compatible, compat)) {
  104. *of_idp = of_match;
  105. return 0;
  106. }
  107. of_match++;
  108. }
  109. return -ENOENT;
  110. }
  111. int lists_bind_fdt(struct udevice *parent, ofnode node, struct udevice **devp)
  112. {
  113. struct driver *driver = ll_entry_start(struct driver, driver);
  114. const int n_ents = ll_entry_count(struct driver, driver);
  115. const struct udevice_id *id;
  116. struct driver *entry;
  117. struct udevice *dev;
  118. bool found = false;
  119. const char *name, *compat_list, *compat;
  120. int compat_length, i;
  121. int result = 0;
  122. int ret = 0;
  123. if (devp)
  124. *devp = NULL;
  125. name = ofnode_get_name(node);
  126. dm_dbg("bind node %s\n", name);
  127. compat_list = (const char *)ofnode_read_prop(node, "compatible",
  128. &compat_length);
  129. if (!compat_list) {
  130. if (compat_length == -FDT_ERR_NOTFOUND) {
  131. dm_dbg("Device '%s' has no compatible string\n", name);
  132. return 0;
  133. }
  134. dm_warn("Device tree error at node '%s'\n", name);
  135. return compat_length;
  136. }
  137. /*
  138. * Walk through the compatible string list, attempting to match each
  139. * compatible string in order such that we match in order of priority
  140. * from the first string to the last.
  141. */
  142. for (i = 0; i < compat_length; i += strlen(compat) + 1) {
  143. compat = compat_list + i;
  144. dm_dbg(" - attempt to match compatible string '%s'\n",
  145. compat);
  146. for (entry = driver; entry != driver + n_ents; entry++) {
  147. ret = driver_check_compatible(entry->of_match, &id,
  148. compat);
  149. if (!ret)
  150. break;
  151. }
  152. if (entry == driver + n_ents)
  153. continue;
  154. dm_dbg(" - found match at '%s'\n", entry->name);
  155. ret = device_bind_with_driver_data(parent, entry, name,
  156. id->data, node, &dev);
  157. if (ret == -ENODEV) {
  158. dm_dbg("Driver '%s' refuses to bind\n", entry->name);
  159. continue;
  160. }
  161. if (ret) {
  162. dm_warn("Error binding driver '%s': %d\n", entry->name,
  163. ret);
  164. return ret;
  165. } else {
  166. found = true;
  167. if (devp)
  168. *devp = dev;
  169. }
  170. break;
  171. }
  172. if (!found && !result && ret != -ENODEV)
  173. dm_dbg("No match for node '%s'\n", name);
  174. return result;
  175. }
  176. #endif