lists.c 4.7 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, -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 is compatible with this node
  90. *
  91. * @param blob: Device tree pointer
  92. * @param offset: Offset of node in device tree
  93. * @param of_match: List of compatible strings to match
  94. * @param of_idp: Returns the match that was found
  95. * @return 0 if there is a match, -ENOENT if no match, -ENODEV if the node
  96. * does not have a compatible string, other error <0 if there is a device
  97. * tree error
  98. */
  99. static int driver_check_compatible(const void *blob, int offset,
  100. const struct udevice_id *of_match,
  101. const struct udevice_id **of_idp)
  102. {
  103. int ret;
  104. *of_idp = NULL;
  105. if (!of_match)
  106. return -ENOENT;
  107. while (of_match->compatible) {
  108. ret = fdt_node_check_compatible(blob, offset,
  109. of_match->compatible);
  110. if (!ret) {
  111. *of_idp = of_match;
  112. return 0;
  113. } else if (ret == -FDT_ERR_NOTFOUND) {
  114. return -ENODEV;
  115. } else if (ret < 0) {
  116. return -EINVAL;
  117. }
  118. of_match++;
  119. }
  120. return -ENOENT;
  121. }
  122. int lists_bind_fdt(struct udevice *parent, const void *blob, int offset,
  123. struct udevice **devp)
  124. {
  125. struct driver *driver = ll_entry_start(struct driver, driver);
  126. const int n_ents = ll_entry_count(struct driver, driver);
  127. const struct udevice_id *id;
  128. struct driver *entry;
  129. struct udevice *dev;
  130. bool found = false;
  131. const char *name;
  132. int result = 0;
  133. int ret = 0;
  134. dm_dbg("bind node %s\n", fdt_get_name(blob, offset, NULL));
  135. if (devp)
  136. *devp = NULL;
  137. for (entry = driver; entry != driver + n_ents; entry++) {
  138. ret = driver_check_compatible(blob, offset, entry->of_match,
  139. &id);
  140. name = fdt_get_name(blob, offset, NULL);
  141. if (ret == -ENOENT) {
  142. continue;
  143. } else if (ret == -ENODEV) {
  144. dm_dbg("Device '%s' has no compatible string\n", name);
  145. break;
  146. } else if (ret) {
  147. dm_warn("Device tree error at offset %d\n", offset);
  148. result = ret;
  149. break;
  150. }
  151. dm_dbg(" - found match at '%s'\n", entry->name);
  152. ret = device_bind_with_driver_data(parent, entry, name,
  153. id->data, offset, &dev);
  154. if (ret == -ENODEV) {
  155. dm_dbg("Driver '%s' refuses to bind\n", entry->name);
  156. continue;
  157. }
  158. if (ret) {
  159. dm_warn("Error binding driver '%s': %d\n", entry->name,
  160. ret);
  161. return ret;
  162. } else {
  163. found = true;
  164. if (devp)
  165. *devp = dev;
  166. }
  167. break;
  168. }
  169. if (!found && !result && ret != -ENODEV) {
  170. dm_dbg("No match for node '%s'\n",
  171. fdt_get_name(blob, offset, NULL));
  172. }
  173. return result;
  174. }
  175. #endif