lists.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. struct driver *drv;
  67. int ret;
  68. drv = lists_driver_lookup_name(drv_name);
  69. if (!drv) {
  70. printf("Cannot find driver '%s'\n", drv_name);
  71. return -ENOENT;
  72. }
  73. ret = device_bind(parent, drv, dev_name, NULL, -1, devp);
  74. if (ret) {
  75. printf("Cannot create device named '%s' (err=%d)\n",
  76. dev_name, ret);
  77. return ret;
  78. }
  79. return 0;
  80. }
  81. #ifdef CONFIG_OF_CONTROL
  82. /**
  83. * driver_check_compatible() - Check if a driver is compatible with this node
  84. *
  85. * @param blob: Device tree pointer
  86. * @param offset: Offset of node in device tree
  87. * @param of_match: List of compatible strings to match
  88. * @param of_idp: Returns the match that was found
  89. * @return 0 if there is a match, -ENOENT if no match, -ENODEV if the node
  90. * does not have a compatible string, other error <0 if there is a device
  91. * tree error
  92. */
  93. static int driver_check_compatible(const void *blob, int offset,
  94. const struct udevice_id *of_match,
  95. const struct udevice_id **of_idp)
  96. {
  97. int ret;
  98. *of_idp = NULL;
  99. if (!of_match)
  100. return -ENOENT;
  101. while (of_match->compatible) {
  102. ret = fdt_node_check_compatible(blob, offset,
  103. of_match->compatible);
  104. if (!ret) {
  105. *of_idp = of_match;
  106. return 0;
  107. } else if (ret == -FDT_ERR_NOTFOUND) {
  108. return -ENODEV;
  109. } else if (ret < 0) {
  110. return -EINVAL;
  111. }
  112. of_match++;
  113. }
  114. return -ENOENT;
  115. }
  116. int lists_bind_fdt(struct udevice *parent, const void *blob, int offset,
  117. struct udevice **devp)
  118. {
  119. struct driver *driver = ll_entry_start(struct driver, driver);
  120. const int n_ents = ll_entry_count(struct driver, driver);
  121. const struct udevice_id *id;
  122. struct driver *entry;
  123. struct udevice *dev;
  124. bool found = false;
  125. const char *name;
  126. int result = 0;
  127. int ret = 0;
  128. dm_dbg("bind node %s\n", fdt_get_name(blob, offset, NULL));
  129. if (devp)
  130. *devp = NULL;
  131. for (entry = driver; entry != driver + n_ents; entry++) {
  132. ret = driver_check_compatible(blob, offset, entry->of_match,
  133. &id);
  134. name = fdt_get_name(blob, offset, NULL);
  135. if (ret == -ENOENT) {
  136. continue;
  137. } else if (ret == -ENODEV) {
  138. dm_dbg("Device '%s' has no compatible string\n", name);
  139. break;
  140. } else if (ret) {
  141. dm_warn("Device tree error at offset %d\n", offset);
  142. result = ret;
  143. break;
  144. }
  145. dm_dbg(" - found match at '%s'\n", entry->name);
  146. ret = device_bind(parent, entry, name, NULL, offset, &dev);
  147. if (ret) {
  148. dm_warn("Error binding driver '%s'\n", entry->name);
  149. return ret;
  150. } else {
  151. dev->of_id = id;
  152. found = true;
  153. if (devp)
  154. *devp = dev;
  155. }
  156. break;
  157. }
  158. if (!found && !result && ret != -ENODEV) {
  159. dm_dbg("No match for node '%s'\n",
  160. fdt_get_name(blob, offset, NULL));
  161. }
  162. return result;
  163. }
  164. #endif