rom.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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/config.h>
  10. #include <linux/kernel.h>
  11. #include <linux/pci.h>
  12. #include "pci.h"
  13. /**
  14. * pci_enable_rom - enable ROM decoding for a PCI device
  15. * @pdev: PCI device to enable
  16. *
  17. * Enable ROM decoding on @dev. This involves simply turning on the last
  18. * bit of the PCI ROM BAR. Note that some cards may share address decoders
  19. * between the ROM and other resources, so enabling it may disable access
  20. * to MMIO registers or other card memory.
  21. */
  22. static void pci_enable_rom(struct pci_dev *pdev)
  23. {
  24. u32 rom_addr;
  25. pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
  26. rom_addr |= PCI_ROM_ADDRESS_ENABLE;
  27. pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
  28. }
  29. /**
  30. * pci_disable_rom - disable ROM decoding for a PCI device
  31. * @pdev: PCI device to disable
  32. *
  33. * Disable ROM decoding on a PCI device by turning off the last bit in the
  34. * ROM BAR.
  35. */
  36. static void pci_disable_rom(struct pci_dev *pdev)
  37. {
  38. u32 rom_addr;
  39. pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
  40. rom_addr &= ~PCI_ROM_ADDRESS_ENABLE;
  41. pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
  42. }
  43. /**
  44. * pci_map_rom - map a PCI ROM to kernel space
  45. * @pdev: pointer to pci device struct
  46. * @size: pointer to receive size of pci window over ROM
  47. * @return: kernel virtual pointer to image of ROM
  48. *
  49. * Map a PCI ROM into kernel space. If ROM is boot video ROM,
  50. * the shadow BIOS copy will be returned instead of the
  51. * actual ROM.
  52. */
  53. void __iomem *pci_map_rom(struct pci_dev *pdev, size_t *size)
  54. {
  55. struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
  56. loff_t start;
  57. void __iomem *rom;
  58. void __iomem *image;
  59. int last_image;
  60. /* IORESOURCE_ROM_SHADOW only set on x86 */
  61. if (res->flags & IORESOURCE_ROM_SHADOW) {
  62. /* primary video rom always starts here */
  63. start = (loff_t)0xC0000;
  64. *size = 0x20000; /* cover C000:0 through E000:0 */
  65. } else {
  66. if (res->flags & IORESOURCE_ROM_COPY) {
  67. *size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
  68. return (void __iomem *)pci_resource_start(pdev, PCI_ROM_RESOURCE);
  69. } else {
  70. /* assign the ROM an address if it doesn't have one */
  71. if (res->parent == NULL)
  72. pci_assign_resource(pdev, PCI_ROM_RESOURCE);
  73. start = pci_resource_start(pdev, PCI_ROM_RESOURCE);
  74. *size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
  75. if (*size == 0)
  76. return NULL;
  77. /* Enable ROM space decodes */
  78. pci_enable_rom(pdev);
  79. }
  80. }
  81. rom = ioremap(start, *size);
  82. if (!rom) {
  83. /* restore enable if ioremap fails */
  84. if (!(res->flags & (IORESOURCE_ROM_ENABLE |
  85. IORESOURCE_ROM_SHADOW |
  86. IORESOURCE_ROM_COPY)))
  87. pci_disable_rom(pdev);
  88. return NULL;
  89. }
  90. /*
  91. * Try to find the true size of the ROM since sometimes the PCI window
  92. * size is much larger than the actual size of the ROM.
  93. * True size is important if the ROM is going to be copied.
  94. */
  95. image = rom;
  96. do {
  97. void __iomem *pds;
  98. /* Standard PCI ROMs start out with these bytes 55 AA */
  99. if (readb(image) != 0x55)
  100. break;
  101. if (readb(image + 1) != 0xAA)
  102. break;
  103. /* get the PCI data structure and check its signature */
  104. pds = image + readw(image + 24);
  105. if (readb(pds) != 'P')
  106. break;
  107. if (readb(pds + 1) != 'C')
  108. break;
  109. if (readb(pds + 2) != 'I')
  110. break;
  111. if (readb(pds + 3) != 'R')
  112. break;
  113. last_image = readb(pds + 21) & 0x80;
  114. /* this length is reliable */
  115. image += readw(pds + 16) * 512;
  116. } while (!last_image);
  117. *size = image - rom;
  118. return rom;
  119. }
  120. /**
  121. * pci_map_rom_copy - map a PCI ROM to kernel space, create a copy
  122. * @pdev: pointer to pci device struct
  123. * @size: pointer to receive size of pci window over ROM
  124. * @return: kernel virtual pointer to image of ROM
  125. *
  126. * Map a PCI ROM into kernel space. If ROM is boot video ROM,
  127. * the shadow BIOS copy will be returned instead of the
  128. * actual ROM.
  129. */
  130. void __iomem *pci_map_rom_copy(struct pci_dev *pdev, size_t *size)
  131. {
  132. struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
  133. void __iomem *rom;
  134. rom = pci_map_rom(pdev, size);
  135. if (!rom)
  136. return NULL;
  137. if (res->flags & (IORESOURCE_ROM_COPY | IORESOURCE_ROM_SHADOW))
  138. return rom;
  139. res->start = (unsigned long)kmalloc(*size, GFP_KERNEL);
  140. if (!res->start)
  141. return rom;
  142. res->end = res->start + *size;
  143. memcpy_fromio((void*)res->start, rom, *size);
  144. pci_unmap_rom(pdev, rom);
  145. res->flags |= IORESOURCE_ROM_COPY;
  146. return (void __iomem *)res->start;
  147. }
  148. /**
  149. * pci_unmap_rom - unmap the ROM from kernel space
  150. * @pdev: pointer to pci device struct
  151. * @rom: virtual address of the previous mapping
  152. *
  153. * Remove a mapping of a previously mapped ROM
  154. */
  155. void pci_unmap_rom(struct pci_dev *pdev, void __iomem *rom)
  156. {
  157. struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
  158. if (res->flags & IORESOURCE_ROM_COPY)
  159. return;
  160. iounmap(rom);
  161. /* Disable again before continuing, leave enabled if pci=rom */
  162. if (!(res->flags & (IORESOURCE_ROM_ENABLE | IORESOURCE_ROM_SHADOW)))
  163. pci_disable_rom(pdev);
  164. }
  165. /**
  166. * pci_remove_rom - disable the ROM and remove its sysfs attribute
  167. * @pdev: pointer to pci device struct
  168. *
  169. * Remove the rom file in sysfs and disable ROM decoding.
  170. */
  171. void pci_remove_rom(struct pci_dev *pdev)
  172. {
  173. struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
  174. if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
  175. sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
  176. if (!(res->flags & (IORESOURCE_ROM_ENABLE |
  177. IORESOURCE_ROM_SHADOW |
  178. IORESOURCE_ROM_COPY)))
  179. pci_disable_rom(pdev);
  180. }
  181. /**
  182. * pci_cleanup_rom - internal routine for freeing the ROM copy created
  183. * by pci_map_rom_copy called from remove.c
  184. * @pdev: pointer to pci device struct
  185. *
  186. * Free the copied ROM if we allocated one.
  187. */
  188. void pci_cleanup_rom(struct pci_dev *pdev)
  189. {
  190. struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
  191. if (res->flags & IORESOURCE_ROM_COPY) {
  192. kfree((void*)res->start);
  193. res->flags &= ~IORESOURCE_ROM_COPY;
  194. res->start = 0;
  195. res->end = 0;
  196. }
  197. }
  198. EXPORT_SYMBOL(pci_map_rom);
  199. EXPORT_SYMBOL(pci_map_rom_copy);
  200. EXPORT_SYMBOL(pci_unmap_rom);
  201. EXPORT_SYMBOL(pci_remove_rom);