rom.c 5.3 KB

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