isahint.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 1999 Doug Rabson
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. #include <sys/param.h>
  29. #include <sys/systm.h>
  30. #include <sys/kernel.h>
  31. #include <sys/bus.h>
  32. #include <sys/module.h>
  33. #include <isa/isavar.h>
  34. #include <isa/isa_common.h>
  35. #include <machine/resource.h>
  36. void
  37. isa_hinted_child(device_t parent, const char *name, int unit)
  38. {
  39. device_t child;
  40. int sensitive, start, count;
  41. int order;
  42. /* device-specific flag overrides any wildcard */
  43. sensitive = 0;
  44. if (resource_int_value(name, unit, "sensitive", &sensitive) != 0)
  45. resource_int_value(name, -1, "sensitive", &sensitive);
  46. if (sensitive)
  47. order = ISA_ORDER_SENSITIVE;
  48. else
  49. order = ISA_ORDER_SPECULATIVE;
  50. child = BUS_ADD_CHILD(parent, order, name, unit);
  51. if (child == 0)
  52. return;
  53. start = 0;
  54. count = 0;
  55. resource_int_value(name, unit, "port", &start);
  56. resource_int_value(name, unit, "portsize", &count);
  57. if (start > 0 || count > 0)
  58. bus_set_resource(child, SYS_RES_IOPORT, 0, start, count);
  59. start = 0;
  60. count = 0;
  61. resource_int_value(name, unit, "maddr", &start);
  62. resource_int_value(name, unit, "msize", &count);
  63. if (start > 0 || count > 0)
  64. bus_set_resource(child, SYS_RES_MEMORY, 0, start, count);
  65. if (resource_int_value(name, unit, "irq", &start) == 0 && start > 0)
  66. bus_set_resource(child, SYS_RES_IRQ, 0, start, 1);
  67. if (resource_int_value(name, unit, "drq", &start) == 0 && start >= 0)
  68. bus_set_resource(child, SYS_RES_DRQ, 0, start, 1);
  69. if (resource_disabled(name, unit))
  70. device_disable(child);
  71. isa_set_configattr(child, (isa_get_configattr(child)|ISACFGATTR_HINTS));
  72. }
  73. static int
  74. isa_match_resource_hint(device_t dev, int type, long value)
  75. {
  76. struct isa_device* idev = DEVTOISA(dev);
  77. struct resource_list *rl = &idev->id_resources;
  78. struct resource_list_entry *rle;
  79. STAILQ_FOREACH(rle, rl, link) {
  80. if (rle->type != type)
  81. continue;
  82. if (rle->start <= value && rle->end >= value)
  83. return (1);
  84. }
  85. return (0);
  86. }
  87. void
  88. isa_hint_device_unit(device_t bus, device_t child, const char *name, int *unitp)
  89. {
  90. const char *s;
  91. long value;
  92. int line, matches, unit;
  93. line = 0;
  94. for (;;) {
  95. if (resource_find_dev(&line, name, &unit, "at", NULL) != 0)
  96. break;
  97. /* Must have an "at" for isa. */
  98. resource_string_value(name, unit, "at", &s);
  99. if (!(strcmp(s, device_get_nameunit(bus)) == 0 ||
  100. strcmp(s, device_get_name(bus)) == 0))
  101. continue;
  102. /*
  103. * Check for matching resources. We must have at
  104. * least one match. Since I/O and memory resources
  105. * cannot be shared, if we get a match on either of
  106. * those, ignore any mismatches in IRQs or DRQs.
  107. *
  108. * XXX: We may want to revisit this to be more lenient
  109. * and wire as long as it gets one match.
  110. */
  111. matches = 0;
  112. if (resource_long_value(name, unit, "port", &value) == 0) {
  113. /*
  114. * Floppy drive controllers are notorious for
  115. * having a wide variety of resources not all
  116. * of which include the first port that is
  117. * specified by the hint (typically 0x3f0)
  118. * (see the comment above
  119. * fdc_isa_alloc_resources() in fdc_isa.c).
  120. * However, they do all seem to include port +
  121. * 2 (e.g. 0x3f2) so for a floppy device, look
  122. * for 'value + 2' in the port resources
  123. * instead of the hint value.
  124. */
  125. if (strcmp(name, "fdc") == 0)
  126. value += 2;
  127. if (isa_match_resource_hint(child, SYS_RES_IOPORT,
  128. value))
  129. matches++;
  130. else
  131. continue;
  132. }
  133. if (resource_long_value(name, unit, "maddr", &value) == 0) {
  134. if (isa_match_resource_hint(child, SYS_RES_MEMORY,
  135. value))
  136. matches++;
  137. else
  138. continue;
  139. }
  140. if (matches > 0)
  141. goto matched;
  142. if (resource_long_value(name, unit, "irq", &value) == 0) {
  143. if (isa_match_resource_hint(child, SYS_RES_IRQ, value))
  144. matches++;
  145. else
  146. continue;
  147. }
  148. if (resource_long_value(name, unit, "drq", &value) == 0) {
  149. if (isa_match_resource_hint(child, SYS_RES_DRQ, value))
  150. matches++;
  151. else
  152. continue;
  153. }
  154. matched:
  155. if (matches > 0) {
  156. /* We have a winner! */
  157. *unitp = unit;
  158. break;
  159. }
  160. }
  161. }