rom.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * drivers/pci/rom.c
  3. *
  4. * (C) Copyright 2004 Jon Smirl <[email protected]>
  5. * (C) Copyright 2004 Silicon Graphics, Inc. Jesse Barnes <[email protected]>
  6. *
  7. * PCI ROM access routines
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/export.h>
  11. #include <linux/pci.h>
  12. #include <linux/slab.h>
  13. #include "pci.h"
  14. /**
  15. * pci_enable_rom - enable ROM decoding for a PCI device
  16. * @pdev: PCI device to enable
  17. *
  18. * Enable ROM decoding on @dev. This involves simply turning on the last
  19. * bit of the PCI ROM BAR. Note that some cards may share address decoders
  20. * between the ROM and other resources, so enabling it may disable access
  21. * to MMIO registers or other card memory.
  22. */
  23. int pci_enable_rom(struct pci_dev *pdev)
  24. {
  25. struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
  26. struct pci_bus_region region;
  27. u32 rom_addr;
  28. if (!res->flags)
  29. return -1;
  30. /* Nothing to enable if we're using a shadow copy in RAM */
  31. if (res->flags & IORESOURCE_ROM_SHADOW)
  32. return 0;
  33. /*
  34. * Ideally pci_update_resource() would update the ROM BAR address,
  35. * and we would only set the enable bit here. But apparently some
  36. * devices have buggy ROM BARs that read as zero when disabled.
  37. */
  38. pcibios_resource_to_bus(pdev->bus, &region, res);
  39. pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
  40. rom_addr &= ~PCI_ROM_ADDRESS_MASK;
  41. rom_addr |= region.start | PCI_ROM_ADDRESS_ENABLE;
  42. pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
  43. return 0;
  44. }
  45. EXPORT_SYMBOL_GPL(pci_enable_rom);
  46. /**
  47. * pci_disable_rom - disable ROM decoding for a PCI device
  48. * @pdev: PCI device to disable
  49. *
  50. * Disable ROM decoding on a PCI device by turning off the last bit in the
  51. * ROM BAR.
  52. */
  53. void pci_disable_rom(struct pci_dev *pdev)
  54. {
  55. struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
  56. u32 rom_addr;
  57. if (res->flags & IORESOURCE_ROM_SHADOW)
  58. return;
  59. pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
  60. rom_addr &= ~PCI_ROM_ADDRESS_ENABLE;
  61. pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
  62. }
  63. EXPORT_SYMBOL_GPL(pci_disable_rom);
  64. /**
  65. * pci_get_rom_size - obtain the actual size of the ROM image
  66. * @pdev: target PCI device
  67. * @rom: kernel virtual pointer to image of ROM
  68. * @size: size of PCI window
  69. * return: size of actual ROM image
  70. *
  71. * Determine the actual length of the ROM image.
  72. * The PCI window size could be much larger than the
  73. * actual image size.
  74. */
  75. size_t pci_get_rom_size(struct pci_dev *pdev, void __iomem *rom, size_t size)
  76. {
  77. void __iomem *image;
  78. int last_image;
  79. unsigned length;
  80. image = rom;
  81. do {
  82. void __iomem *pds;
  83. /* Standard PCI ROMs start out with these bytes 55 AA */
  84. if (readw(image) != 0xAA55) {
  85. dev_err(&pdev->dev, "Invalid PCI ROM header signature: expecting 0xaa55, got %#06x\n",
  86. readw(image));
  87. break;
  88. }
  89. /* get the PCI data structure and check its "PCIR" signature */
  90. pds = image + readw(image + 24);
  91. if (readl(pds) != 0x52494350) {
  92. dev_err(&pdev->dev, "Invalid PCI ROM data signature: expecting 0x52494350, got %#010x\n",
  93. readl(pds));
  94. break;
  95. }
  96. last_image = readb(pds + 21) & 0x80;
  97. length = readw(pds + 16);
  98. image += length * 512;
  99. /* Avoid iterating through memory outside the resource window */
  100. if (image > rom + size)
  101. break;
  102. } while (length && !last_image);
  103. /* never return a size larger than the PCI resource window */
  104. /* there are known ROMs that get the size wrong */
  105. return min((size_t)(image - rom), size);
  106. }
  107. /**
  108. * pci_map_rom - map a PCI ROM to kernel space
  109. * @pdev: pointer to pci device struct
  110. * @size: pointer to receive size of pci window over ROM
  111. *
  112. * Return: kernel virtual pointer to image of ROM
  113. *
  114. * Map a PCI ROM into kernel space. If ROM is boot video ROM,
  115. * the shadow BIOS copy will be returned instead of the
  116. * actual ROM.
  117. */
  118. void __iomem *pci_map_rom(struct pci_dev *pdev, size_t *size)
  119. {
  120. struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
  121. loff_t start;
  122. void __iomem *rom;
  123. /* assign the ROM an address if it doesn't have one */
  124. if (res->parent == NULL && pci_assign_resource(pdev, PCI_ROM_RESOURCE))
  125. return NULL;
  126. start = pci_resource_start(pdev, PCI_ROM_RESOURCE);
  127. *size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
  128. if (*size == 0)
  129. return NULL;
  130. /* Enable ROM space decodes */
  131. if (pci_enable_rom(pdev))
  132. return NULL;
  133. rom = ioremap(start, *size);
  134. if (!rom) {
  135. /* restore enable if ioremap fails */
  136. if (!(res->flags & IORESOURCE_ROM_ENABLE))
  137. pci_disable_rom(pdev);
  138. return NULL;
  139. }
  140. /*
  141. * Try to find the true size of the ROM since sometimes the PCI window
  142. * size is much larger than the actual size of the ROM.
  143. * True size is important if the ROM is going to be copied.
  144. */
  145. *size = pci_get_rom_size(pdev, rom, *size);
  146. return rom;
  147. }
  148. EXPORT_SYMBOL(pci_map_rom);
  149. /**
  150. * pci_unmap_rom - unmap the ROM from kernel space
  151. * @pdev: pointer to pci device struct
  152. * @rom: virtual address of the previous mapping
  153. *
  154. * Remove a mapping of a previously mapped ROM
  155. */
  156. void pci_unmap_rom(struct pci_dev *pdev, void __iomem *rom)
  157. {
  158. struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
  159. iounmap(rom);
  160. /* Disable again before continuing */
  161. if (!(res->flags & IORESOURCE_ROM_ENABLE))
  162. pci_disable_rom(pdev);
  163. }
  164. EXPORT_SYMBOL(pci_unmap_rom);
  165. /**
  166. * pci_platform_rom - provides a pointer to any ROM image provided by the
  167. * platform
  168. * @pdev: pointer to pci device struct
  169. * @size: pointer to receive size of pci window over ROM
  170. */
  171. void __iomem *pci_platform_rom(struct pci_dev *pdev, size_t *size)
  172. {
  173. if (pdev->rom && pdev->romlen) {
  174. *size = pdev->romlen;
  175. return phys_to_virt((phys_addr_t)pdev->rom);
  176. }
  177. return NULL;
  178. }
  179. EXPORT_SYMBOL(pci_platform_rom);