isahint.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
  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/cdefs.h>
  29. __FBSDID("$FreeBSD$");
  30. #include <sys/param.h>
  31. #include <sys/systm.h>
  32. #include <sys/kernel.h>
  33. #include <sys/bus.h>
  34. #include <sys/module.h>
  35. #include <isa/isavar.h>
  36. #include <isa/isa_common.h>
  37. #include <machine/resource.h>
  38. void
  39. isa_hinted_child(device_t parent, const char *name, int unit)
  40. {
  41. device_t child;
  42. int sensitive, start, count;
  43. int order;
  44. /* device-specific flag overrides any wildcard */
  45. sensitive = 0;
  46. if (resource_int_value(name, unit, "sensitive", &sensitive) != 0)
  47. resource_int_value(name, -1, "sensitive", &sensitive);
  48. if (sensitive)
  49. order = ISA_ORDER_SENSITIVE;
  50. else
  51. order = ISA_ORDER_SPECULATIVE;
  52. child = BUS_ADD_CHILD(parent, order, name, unit);
  53. if (child == 0)
  54. return;
  55. start = 0;
  56. count = 0;
  57. resource_int_value(name, unit, "port", &start);
  58. resource_int_value(name, unit, "portsize", &count);
  59. if (start > 0 || count > 0)
  60. bus_set_resource(child, SYS_RES_IOPORT, 0, start, count);
  61. start = 0;
  62. count = 0;
  63. resource_int_value(name, unit, "maddr", &start);
  64. resource_int_value(name, unit, "msize", &count);
  65. if (start > 0 || count > 0)
  66. bus_set_resource(child, SYS_RES_MEMORY, 0, start, count);
  67. if (resource_int_value(name, unit, "irq", &start) == 0 && start > 0)
  68. bus_set_resource(child, SYS_RES_IRQ, 0, start, 1);
  69. if (resource_int_value(name, unit, "drq", &start) == 0 && start >= 0)
  70. bus_set_resource(child, SYS_RES_DRQ, 0, start, 1);
  71. if (resource_disabled(name, unit))
  72. device_disable(child);
  73. isa_set_configattr(child, (isa_get_configattr(child)|ISACFGATTR_HINTS));
  74. }
  75. static int
  76. isa_match_resource_hint(device_t dev, int type, long value)
  77. {
  78. struct isa_device* idev = DEVTOISA(dev);
  79. struct resource_list *rl = &idev->id_resources;
  80. struct resource_list_entry *rle;
  81. STAILQ_FOREACH(rle, rl, link) {
  82. if (rle->type != type)
  83. continue;
  84. if (rle->start <= value && rle->end >= value)
  85. return (1);
  86. }
  87. return (0);
  88. }
  89. void
  90. isa_hint_device_unit(device_t bus, device_t child, const char *name, int *unitp)
  91. {
  92. const char *s;
  93. long value;
  94. int line, matches, unit;
  95. line = 0;
  96. for (;;) {
  97. if (resource_find_dev(&line, name, &unit, "at", NULL) != 0)
  98. break;
  99. /* Must have an "at" for isa. */
  100. resource_string_value(name, unit, "at", &s);
  101. if (!(strcmp(s, device_get_nameunit(bus)) == 0 ||
  102. strcmp(s, device_get_name(bus)) == 0))
  103. continue;
  104. /*
  105. * Check for matching resources. We must have at
  106. * least one match. Since I/O and memory resources
  107. * cannot be shared, if we get a match on either of
  108. * those, ignore any mismatches in IRQs or DRQs.
  109. *
  110. * XXX: We may want to revisit this to be more lenient
  111. * and wire as long as it gets one match.
  112. */
  113. matches = 0;
  114. if (resource_long_value(name, unit, "port", &value) == 0) {
  115. /*
  116. * Floppy drive controllers are notorious for
  117. * having a wide variety of resources not all
  118. * of which include the first port that is
  119. * specified by the hint (typically 0x3f0)
  120. * (see the comment above
  121. * fdc_isa_alloc_resources() in fdc_isa.c).
  122. * However, they do all seem to include port +
  123. * 2 (e.g. 0x3f2) so for a floppy device, look
  124. * for 'value + 2' in the port resources
  125. * instead of the hint value.
  126. */
  127. if (strcmp(name, "fdc") == 0)
  128. value += 2;
  129. if (isa_match_resource_hint(child, SYS_RES_IOPORT,
  130. value))
  131. matches++;
  132. else
  133. continue;
  134. }
  135. if (resource_long_value(name, unit, "maddr", &value) == 0) {
  136. if (isa_match_resource_hint(child, SYS_RES_MEMORY,
  137. value))
  138. matches++;
  139. else
  140. continue;
  141. }
  142. if (matches > 0)
  143. goto matched;
  144. if (resource_long_value(name, unit, "irq", &value) == 0) {
  145. if (isa_match_resource_hint(child, SYS_RES_IRQ, value))
  146. matches++;
  147. else
  148. continue;
  149. }
  150. if (resource_long_value(name, unit, "drq", &value) == 0) {
  151. if (isa_match_resource_hint(child, SYS_RES_DRQ, value))
  152. matches++;
  153. else
  154. continue;
  155. }
  156. matched:
  157. if (matches > 0) {
  158. /* We have a winner! */
  159. *unitp = unit;
  160. break;
  161. }
  162. }
  163. }