Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

pci_legacy.c 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #ifdef CONFIG_PCI
  2. /*
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU General Public License as
  5. * published by the Free Software Foundation; either version 2, or (at
  6. * your option) any later version.
  7. */
  8. #include "etherboot.h"
  9. #include "pci.h"
  10. /*#define DEBUG 1*/
  11. static void scan_drivers(
  12. int type,
  13. uint32_t class, uint16_t vendor, uint16_t device,
  14. const struct pci_driver *last_driver, struct pci_device *dev)
  15. {
  16. const struct pci_driver *skip_driver = last_driver;
  17. /* Assume there is only one match of the correct type */
  18. const struct pci_driver *driver;
  19. for(driver = pci_drivers; driver < pci_drivers_end; driver++) {
  20. int i;
  21. if (driver->type != type)
  22. continue;
  23. if (skip_driver) {
  24. if (skip_driver == driver)
  25. skip_driver = 0;
  26. continue;
  27. }
  28. for(i = 0; i < driver->id_count; i++) {
  29. if ((vendor == driver->ids[i].vendor) &&
  30. (device == driver->ids[i].dev_id)) {
  31. dev->driver = driver;
  32. dev->name = driver->ids[i].name;
  33. goto out;
  34. }
  35. }
  36. }
  37. if (!class) {
  38. goto out;
  39. }
  40. for(driver = pci_drivers; driver < pci_drivers_end; driver++) {
  41. if (driver->type != type)
  42. continue;
  43. if (skip_driver) {
  44. if (skip_driver == driver)
  45. skip_driver = 0;
  46. continue;
  47. }
  48. if (last_driver == driver)
  49. continue;
  50. if ((class >> 8) == driver->class) {
  51. dev->driver = driver;
  52. dev->name = driver->name;
  53. goto out;
  54. }
  55. }
  56. out:
  57. return;
  58. }
  59. void scan_pci_bus(int type, struct pci_device *dev)
  60. {
  61. unsigned int first_bus, first_devfn;
  62. const struct pci_driver *first_driver;
  63. unsigned int devfn, bus, buses;
  64. unsigned char hdr_type = 0;
  65. uint32_t class;
  66. uint16_t vendor, device;
  67. uint32_t l, membase, ioaddr, romaddr;
  68. uint8_t irq;
  69. int reg;
  70. first_bus = 0;
  71. first_devfn = 0;
  72. first_driver = 0;
  73. if (dev->driver || dev->use_specified) {
  74. first_driver = dev->driver;
  75. first_bus = dev->bus;
  76. first_devfn = dev->devfn;
  77. /* Re read the header type on a restart */
  78. pcibios_read_config_byte(first_bus, first_devfn & ~0x7,
  79. PCI_HEADER_TYPE, &hdr_type);
  80. dev->driver = 0;
  81. dev->bus = 0;
  82. dev->devfn = 0;
  83. }
  84. /* Scan all PCI buses, until we find our card.
  85. * We could be smart only scan the required buses but that
  86. * is error prone, and tricky.
  87. * By scanning all possible pci buses in order we should find
  88. * our card eventually.
  89. */
  90. buses=256;
  91. for (bus = first_bus; bus < buses; ++bus) {
  92. for (devfn = first_devfn; devfn < 0xff; ++devfn, first_driver = 0) {
  93. if (PCI_FUNC (devfn) == 0)
  94. pcibios_read_config_byte(bus, devfn, PCI_HEADER_TYPE, &hdr_type);
  95. else if (!(hdr_type & 0x80)) /* not a multi-function device */
  96. continue;
  97. pcibios_read_config_dword(bus, devfn, PCI_VENDOR_ID, &l);
  98. /* some broken boards return 0 if a slot is empty: */
  99. if (l == 0xffffffff || l == 0x00000000) {
  100. continue;
  101. }
  102. vendor = l & 0xffff;
  103. device = (l >> 16) & 0xffff;
  104. pcibios_read_config_dword(bus, devfn, PCI_REVISION, &l);
  105. class = (l >> 8) & 0xffffff;
  106. #if DEBUG
  107. {
  108. int i;
  109. printf("%hhx:%hhx.%hhx [%hX/%hX] Class %hX\n",
  110. bus, PCI_SLOT(devfn), PCI_FUNC(devfn),
  111. vendor, device, class >> 8);
  112. #if DEBUG > 1
  113. for(i = 0; i < 256; i++) {
  114. unsigned char byte;
  115. if ((i & 0xf) == 0) {
  116. printf("%hhx: ", i);
  117. }
  118. pcibios_read_config_byte(bus, devfn, i, &byte);
  119. printf("%hhx ", byte);
  120. if ((i & 0xf) == 0xf) {
  121. printf("\n");
  122. }
  123. }
  124. #endif
  125. }
  126. #endif
  127. scan_drivers(type, class, vendor, device, first_driver, dev);
  128. if (!dev->driver)
  129. continue;
  130. dev->devfn = devfn;
  131. dev->bus = bus;
  132. dev->class = class;
  133. dev->vendor = vendor;
  134. dev->dev_id = device;
  135. /* Get the ROM base address */
  136. pcibios_read_config_dword(bus, devfn,
  137. PCI_ROM_ADDRESS, &romaddr);
  138. romaddr >>= 10;
  139. dev->romaddr = romaddr;
  140. /* Get the ``membase'' */
  141. pcibios_read_config_dword(bus, devfn,
  142. PCI_BASE_ADDRESS_1, &membase);
  143. dev->membase = membase;
  144. /* Get the ``ioaddr'' */
  145. for (reg = PCI_BASE_ADDRESS_0; reg <= PCI_BASE_ADDRESS_5; reg += 4) {
  146. pcibios_read_config_dword(bus, devfn, reg, &ioaddr);
  147. if ((ioaddr & PCI_BASE_ADDRESS_IO_MASK) == 0 || (ioaddr & PCI_BASE_ADDRESS_SPACE_IO) == 0)
  148. continue;
  149. /* Strip the I/O address out of the returned value */
  150. ioaddr &= PCI_BASE_ADDRESS_IO_MASK;
  151. /* Take the first one or the one that matches in boot ROM address */
  152. dev->ioaddr = ioaddr;
  153. }
  154. /* Get the irq */
  155. pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &irq);
  156. if (irq) {
  157. pci_read_config_byte(dev, PCI_INTERRUPT_LINE,
  158. &irq);
  159. }
  160. dev->irq = irq;
  161. #if DEBUG > 2
  162. printf("Found %s ROM address %#hx\n",
  163. dev->name, romaddr);
  164. #endif
  165. return;
  166. }
  167. first_devfn = 0;
  168. }
  169. first_bus = 0;
  170. }
  171. #endif /* CONFIG_PCI */