4 * (C) Copyright 2004 Jon Smirl <jonsmirl@yahoo.com>
5 * (C) Copyright 2004 Silicon Graphics, Inc. Jesse Barnes <jbarnes@sgi.com>
7 * PCI ROM access routines
9 #include <linux/kernel.h>
10 #include <linux/export.h>
11 #include <linux/pci.h>
12 #include <linux/slab.h>
17 * pci_enable_rom - enable ROM decoding for a PCI device
18 * @pdev: PCI device to enable
20 * Enable ROM decoding on @dev. This involves simply turning on the last
21 * bit of the PCI ROM BAR. Note that some cards may share address decoders
22 * between the ROM and other resources, so enabling it may disable access
23 * to MMIO registers or other card memory.
25 int pci_enable_rom(struct pci_dev *pdev)
27 struct resource *res = pdev->resource + PCI_ROM_RESOURCE;
28 struct pci_bus_region region;
34 pcibios_resource_to_bus(pdev, ®ion, res);
35 pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
36 rom_addr &= ~PCI_ROM_ADDRESS_MASK;
37 rom_addr |= region.start | PCI_ROM_ADDRESS_ENABLE;
38 pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
43 * pci_disable_rom - disable ROM decoding for a PCI device
44 * @pdev: PCI device to disable
46 * Disable ROM decoding on a PCI device by turning off the last bit in the
49 void pci_disable_rom(struct pci_dev *pdev)
52 pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
53 rom_addr &= ~PCI_ROM_ADDRESS_ENABLE;
54 pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
58 * pci_get_rom_size - obtain the actual size of the ROM image
59 * @pdev: target PCI device
60 * @rom: kernel virtual pointer to image of ROM
61 * @size: size of PCI window
62 * return: size of actual ROM image
64 * Determine the actual length of the ROM image.
65 * The PCI window size could be much larger than the
68 size_t pci_get_rom_size(struct pci_dev *pdev, void __iomem *rom, size_t size)
77 /* Standard PCI ROMs start out with these bytes 55 AA */
78 if (readb(image) != 0x55) {
79 dev_err(&pdev->dev, "Invalid ROM contents\n");
82 if (readb(image + 1) != 0xAA)
84 /* get the PCI data structure and check its signature */
85 pds = image + readw(image + 24);
86 if (readb(pds) != 'P')
88 if (readb(pds + 1) != 'C')
90 if (readb(pds + 2) != 'I')
92 if (readb(pds + 3) != 'R')
94 last_image = readb(pds + 21) & 0x80;
95 length = readw(pds + 16);
96 image += length * 512;
97 } while (length && !last_image);
99 /* never return a size larger than the PCI resource window */
100 /* there are known ROMs that get the size wrong */
101 return min((size_t)(image - rom), size);
105 * pci_map_rom - map a PCI ROM to kernel space
106 * @pdev: pointer to pci device struct
107 * @size: pointer to receive size of pci window over ROM
109 * Return: kernel virtual pointer to image of ROM
111 * Map a PCI ROM into kernel space. If ROM is boot video ROM,
112 * the shadow BIOS copy will be returned instead of the
115 void __iomem *pci_map_rom(struct pci_dev *pdev, size_t *size)
117 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
122 * IORESOURCE_ROM_SHADOW set on x86, x86_64 and IA64 supports legacy
123 * memory map if the VGA enable bit of the Bridge Control register is
124 * set for embedded VGA.
126 if (res->flags & IORESOURCE_ROM_SHADOW) {
127 /* primary video rom always starts here */
128 start = (loff_t)0xC0000;
129 *size = 0x20000; /* cover C000:0 through E000:0 */
132 (IORESOURCE_ROM_COPY | IORESOURCE_ROM_BIOS_COPY)) {
133 *size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
134 return (void __iomem *)(unsigned long)
135 pci_resource_start(pdev, PCI_ROM_RESOURCE);
137 /* assign the ROM an address if it doesn't have one */
138 if (res->parent == NULL &&
139 pci_assign_resource(pdev,PCI_ROM_RESOURCE))
141 start = pci_resource_start(pdev, PCI_ROM_RESOURCE);
142 *size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
146 /* Enable ROM space decodes */
147 if (pci_enable_rom(pdev))
152 rom = ioremap(start, *size);
154 /* restore enable if ioremap fails */
155 if (!(res->flags & (IORESOURCE_ROM_ENABLE |
156 IORESOURCE_ROM_SHADOW |
157 IORESOURCE_ROM_COPY)))
158 pci_disable_rom(pdev);
163 * Try to find the true size of the ROM since sometimes the PCI window
164 * size is much larger than the actual size of the ROM.
165 * True size is important if the ROM is going to be copied.
167 *size = pci_get_rom_size(pdev, rom, *size);
173 * pci_map_rom_copy - map a PCI ROM to kernel space, create a copy
174 * @pdev: pointer to pci device struct
175 * @size: pointer to receive size of pci window over ROM
177 * Return: kernel virtual pointer to image of ROM
179 * Map a PCI ROM into kernel space. If ROM is boot video ROM,
180 * the shadow BIOS copy will be returned instead of the
183 void __iomem *pci_map_rom_copy(struct pci_dev *pdev, size_t *size)
185 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
188 rom = pci_map_rom(pdev, size);
192 if (res->flags & (IORESOURCE_ROM_COPY | IORESOURCE_ROM_SHADOW |
193 IORESOURCE_ROM_BIOS_COPY))
196 res->start = (unsigned long)kmalloc(*size, GFP_KERNEL);
200 res->end = res->start + *size;
201 memcpy_fromio((void*)(unsigned long)res->start, rom, *size);
202 pci_unmap_rom(pdev, rom);
203 res->flags |= IORESOURCE_ROM_COPY;
205 return (void __iomem *)(unsigned long)res->start;
210 * pci_unmap_rom - unmap the ROM from kernel space
211 * @pdev: pointer to pci device struct
212 * @rom: virtual address of the previous mapping
214 * Remove a mapping of a previously mapped ROM
216 void pci_unmap_rom(struct pci_dev *pdev, void __iomem *rom)
218 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
220 if (res->flags & (IORESOURCE_ROM_COPY | IORESOURCE_ROM_BIOS_COPY))
225 /* Disable again before continuing, leave enabled if pci=rom */
226 if (!(res->flags & (IORESOURCE_ROM_ENABLE | IORESOURCE_ROM_SHADOW)))
227 pci_disable_rom(pdev);
232 * pci_remove_rom - disable the ROM and remove its sysfs attribute
233 * @pdev: pointer to pci device struct
235 * Remove the rom file in sysfs and disable ROM decoding.
237 void pci_remove_rom(struct pci_dev *pdev)
239 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
241 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
242 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
243 if (!(res->flags & (IORESOURCE_ROM_ENABLE |
244 IORESOURCE_ROM_SHADOW |
245 IORESOURCE_ROM_BIOS_COPY |
246 IORESOURCE_ROM_COPY)))
247 pci_disable_rom(pdev);
252 * pci_cleanup_rom - free the ROM copy created by pci_map_rom_copy
253 * @pdev: pointer to pci device struct
255 * Free the copied ROM if we allocated one.
257 void pci_cleanup_rom(struct pci_dev *pdev)
259 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
260 if (res->flags & IORESOURCE_ROM_COPY) {
261 kfree((void*)(unsigned long)res->start);
262 res->flags &= ~IORESOURCE_ROM_COPY;
268 EXPORT_SYMBOL(pci_map_rom);
269 EXPORT_SYMBOL(pci_unmap_rom);
270 EXPORT_SYMBOL_GPL(pci_enable_rom);
271 EXPORT_SYMBOL_GPL(pci_disable_rom);