rom.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. if (res->flags &
  119. (IORESOURCE_ROM_COPY | IORESOURCE_ROM_BIOS_COPY)) {
  120. *size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
  121. return (void __iomem *)(unsigned long)
  122. pci_resource_start(pdev, PCI_ROM_RESOURCE);
  123. } else {
  124. /* assign the ROM an address if it doesn't have one */
  125. if (res->parent == NULL &&
  126. pci_assign_resource(pdev, PCI_ROM_RESOURCE))
  127. return NULL;
  128. start = pci_resource_start(pdev, PCI_ROM_RESOURCE);
  129. *size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
  130. if (*size == 0)
  131. return NULL;
  132. /* Enable ROM space decodes */
  133. if (pci_enable_rom(pdev))
  134. return NULL;
  135. }
  136. rom = ioremap(start, *size);
  137. if (!rom) {
  138. /* restore enable if ioremap fails */
  139. if (!(res->flags & (IORESOURCE_ROM_ENABLE |
  140. IORESOURCE_ROM_COPY)))
  141. pci_disable_rom(pdev);
  142. return NULL;
  143. }
  144. /*
  145. * Try to find the true size of the ROM since sometimes the PCI window
  146. * size is much larger than the actual size of the ROM.
  147. * True size is important if the ROM is going to be copied.
  148. */
  149. *size = pci_get_rom_size(pdev, rom, *size);
  150. return rom;
  151. }
  152. EXPORT_SYMBOL(pci_map_rom);
  153. /**
  154. * pci_unmap_rom - unmap the ROM from kernel space
  155. * @pdev: pointer to pci device struct
  156. * @rom: virtual address of the previous mapping
  157. *
  158. * Remove a mapping of a previously mapped ROM
  159. */
  160. void pci_unmap_rom(struct pci_dev *pdev, void __iomem *rom)
  161. {
  162. struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
  163. if (res->flags & (IORESOURCE_ROM_COPY | IORESOURCE_ROM_BIOS_COPY))
  164. return;
  165. iounmap(rom);
  166. /* Disable again before continuing */
  167. if (!(res->flags & IORESOURCE_ROM_ENABLE))
  168. pci_disable_rom(pdev);
  169. }
  170. EXPORT_SYMBOL(pci_unmap_rom);
  171. /**
  172. * pci_cleanup_rom - free the ROM copy created by pci_map_rom_copy
  173. * @pdev: pointer to pci device struct
  174. *
  175. * Free the copied ROM if we allocated one.
  176. */
  177. void pci_cleanup_rom(struct pci_dev *pdev)
  178. {
  179. struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
  180. if (res->flags & IORESOURCE_ROM_COPY) {
  181. kfree((void *)(unsigned long)res->start);
  182. res->flags |= IORESOURCE_UNSET;
  183. res->flags &= ~IORESOURCE_ROM_COPY;
  184. res->start = 0;
  185. res->end = 0;
  186. }
  187. }
  188. /**
  189. * pci_platform_rom - provides a pointer to any ROM image provided by the
  190. * platform
  191. * @pdev: pointer to pci device struct
  192. * @size: pointer to receive size of pci window over ROM
  193. */
  194. void __iomem *pci_platform_rom(struct pci_dev *pdev, size_t *size)
  195. {
  196. if (pdev->rom && pdev->romlen) {
  197. *size = pdev->romlen;
  198. return phys_to_virt((phys_addr_t)pdev->rom);
  199. }
  200. return NULL;
  201. }
  202. EXPORT_SYMBOL(pci_platform_rom);