lists.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. if (!drv || !n_ents)
  26. return NULL;
  27. for (entry = drv; entry != drv + n_ents; entry++) {
  28. if (!strcmp(name, entry->name))
  29. return entry;
  30. }
  31. /* Not found */
  32. return NULL;
  33. }
  34. struct uclass_driver *lists_uclass_lookup(enum uclass_id id)
  35. {
  36. struct uclass_driver *uclass =
  37. ll_entry_start(struct uclass_driver, uclass);
  38. const int n_ents = ll_entry_count(struct uclass_driver, uclass);
  39. struct uclass_driver *entry;
  40. if ((id == UCLASS_INVALID) || !uclass)
  41. return NULL;
  42. for (entry = uclass; entry != uclass + n_ents; entry++) {
  43. if (entry->id == id)
  44. return entry;
  45. }
  46. return NULL;
  47. }
  48. int lists_bind_drivers(struct udevice *parent, bool pre_reloc_only)
  49. {
  50. struct driver_info *info =
  51. ll_entry_start(struct driver_info, driver_info);
  52. const int n_ents = ll_entry_count(struct driver_info, driver_info);
  53. struct driver_info *entry;
  54. struct udevice *dev;
  55. int result = 0;
  56. int ret;
  57. for (entry = info; entry != info + n_ents; entry++) {
  58. ret = device_bind_by_name(parent, pre_reloc_only, entry, &dev);
  59. if (ret && ret != -EPERM) {
  60. dm_warn("No match for driver '%s'\n", entry->name);
  61. if (!result || ret != -ENOENT)
  62. result = ret;
  63. }
  64. }
  65. return result;
  66. }
  67. int device_bind_driver(struct udevice *parent, const char *drv_name,
  68. const char *dev_name, struct udevice **devp)
  69. {
  70. struct driver *drv;
  71. int ret;
  72. drv = lists_driver_lookup_name(drv_name);
  73. if (!drv) {
  74. printf("Cannot find driver '%s'\n", drv_name);
  75. return -ENOENT;
  76. }
  77. ret = device_bind(parent, drv, dev_name, NULL, -1, devp);
  78. if (ret) {
  79. printf("Cannot create device named '%s' (err=%d)\n",
  80. dev_name, ret);
  81. return ret;
  82. }
  83. return 0;
  84. }
  85. #ifdef CONFIG_OF_CONTROL
  86. /**
  87. * driver_check_compatible() - Check if a driver is compatible with this node
  88. *
  89. * @param blob: Device tree pointer
  90. * @param offset: Offset of node in device tree
  91. * @param of_match: List of compatible strings to match
  92. * @param of_idp: Returns the match that was found
  93. * @return 0 if there is a match, -ENOENT if no match, -ENODEV if the node
  94. * does not have a compatible string, other error <0 if there is a device
  95. * tree error
  96. */
  97. static int driver_check_compatible(const void *blob, int offset,
  98. const struct udevice_id *of_match,
  99. const struct udevice_id **of_idp)
  100. {
  101. int ret;
  102. *of_idp = NULL;
  103. if (!of_match)
  104. return -ENOENT;
  105. while (of_match->compatible) {
  106. ret = fdt_node_check_compatible(blob, offset,
  107. of_match->compatible);
  108. if (!ret) {
  109. *of_idp = of_match;
  110. return 0;
  111. } else if (ret == -FDT_ERR_NOTFOUND) {
  112. return -ENODEV;
  113. } else if (ret < 0) {
  114. return -EINVAL;
  115. }
  116. of_match++;
  117. }
  118. return -ENOENT;
  119. }
  120. int lists_bind_fdt(struct udevice *parent, const void *blob, int offset,
  121. struct udevice **devp)
  122. {
  123. struct driver *driver = ll_entry_start(struct driver, driver);
  124. const int n_ents = ll_entry_count(struct driver, driver);
  125. const struct udevice_id *id;
  126. struct driver *entry;
  127. struct udevice *dev;
  128. bool found = false;
  129. const char *name;
  130. int result = 0;
  131. int ret = 0;
  132. dm_dbg("bind node %s\n", fdt_get_name(blob, offset, NULL));
  133. if (devp)
  134. *devp = NULL;
  135. for (entry = driver; entry != driver + n_ents; entry++) {
  136. ret = driver_check_compatible(blob, offset, entry->of_match,
  137. &id);
  138. name = fdt_get_name(blob, offset, NULL);
  139. if (ret == -ENOENT) {
  140. continue;
  141. } else if (ret == -ENODEV) {
  142. dm_dbg("Device '%s' has no compatible string\n", name);
  143. break;
  144. } else if (ret) {
  145. dm_warn("Device tree error at offset %d\n", offset);
  146. if (!result || ret != -ENOENT)
  147. result = ret;
  148. break;
  149. }
  150. dm_dbg(" - found match at '%s'\n", entry->name);
  151. ret = device_bind(parent, entry, name, NULL, offset, &dev);
  152. if (ret) {
  153. dm_warn("Error binding driver '%s'\n", entry->name);
  154. return ret;
  155. } else {
  156. dev->of_id = id;
  157. found = true;
  158. if (devp)
  159. *devp = dev;
  160. }
  161. break;
  162. }
  163. if (!found && !result && ret != -ENODEV) {
  164. dm_dbg("No match for node '%s'\n",
  165. fdt_get_name(blob, offset, NULL));
  166. }
  167. return result;
  168. }
  169. #endif