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/config.h>
10 #include <linux/kernel.h>
11 #include <linux/pci.h>
16 * pci_enable_rom - enable ROM decoding for a PCI device
17 * @pdev: PCI device to enable
19 * Enable ROM decoding on @dev. This involves simply turning on the last
20 * bit of the PCI ROM BAR. Note that some cards may share address decoders
21 * between the ROM and other resources, so enabling it may disable access
22 * to MMIO registers or other card memory.
24 static void pci_enable_rom(struct pci_dev *pdev)
28 pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
29 rom_addr |= PCI_ROM_ADDRESS_ENABLE;
30 pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
34 * pci_disable_rom - disable ROM decoding for a PCI device
35 * @pdev: PCI device to disable
37 * Disable ROM decoding on a PCI device by turning off the last bit in the
40 static void pci_disable_rom(struct pci_dev *pdev)
43 pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
44 rom_addr &= ~PCI_ROM_ADDRESS_ENABLE;
45 pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
49 * pci_map_rom - map a PCI ROM to kernel space
50 * @pdev: pointer to pci device struct
51 * @size: pointer to receive size of pci window over ROM
52 * @return: kernel virtual pointer to image of ROM
54 * Map a PCI ROM into kernel space. If ROM is boot video ROM,
55 * the shadow BIOS copy will be returned instead of the
58 void __iomem *pci_map_rom(struct pci_dev *pdev, size_t *size)
60 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
66 /* IORESOURCE_ROM_SHADOW only set on x86 */
67 if (res->flags & IORESOURCE_ROM_SHADOW) {
68 /* primary video rom always starts here */
69 start = (loff_t)0xC0000;
70 *size = 0x20000; /* cover C000:0 through E000:0 */
72 if (res->flags & IORESOURCE_ROM_COPY) {
73 *size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
74 return (void __iomem *)pci_resource_start(pdev, PCI_ROM_RESOURCE);
76 /* assign the ROM an address if it doesn't have one */
77 if (res->parent == NULL)
78 pci_assign_resource(pdev, PCI_ROM_RESOURCE);
80 start = pci_resource_start(pdev, PCI_ROM_RESOURCE);
81 *size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
85 /* Enable ROM space decodes */
90 rom = ioremap(start, *size);
92 /* restore enable if ioremap fails */
93 if (!(res->flags & (IORESOURCE_ROM_ENABLE |
94 IORESOURCE_ROM_SHADOW |
95 IORESOURCE_ROM_COPY)))
96 pci_disable_rom(pdev);
101 * Try to find the true size of the ROM since sometimes the PCI window
102 * size is much larger than the actual size of the ROM.
103 * True size is important if the ROM is going to be copied.
108 /* Standard PCI ROMs start out with these bytes 55 AA */
109 if (readb(image) != 0x55)
111 if (readb(image + 1) != 0xAA)
113 /* get the PCI data structure and check its signature */
114 pds = image + readw(image + 24);
115 if (readb(pds) != 'P')
117 if (readb(pds + 1) != 'C')
119 if (readb(pds + 2) != 'I')
121 if (readb(pds + 3) != 'R')
123 last_image = readb(pds + 21) & 0x80;
124 /* this length is reliable */
125 image += readw(pds + 16) * 512;
126 } while (!last_image);
128 /* never return a size larger than the PCI resource window */
129 /* there are known ROMs that get the size wrong */
130 *size = min((size_t)(image - rom), *size);
136 * pci_map_rom_copy - map a PCI ROM to kernel space, create a copy
137 * @pdev: pointer to pci device struct
138 * @size: pointer to receive size of pci window over ROM
139 * @return: kernel virtual pointer to image of ROM
141 * Map a PCI ROM into kernel space. If ROM is boot video ROM,
142 * the shadow BIOS copy will be returned instead of the
145 void __iomem *pci_map_rom_copy(struct pci_dev *pdev, size_t *size)
147 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
150 rom = pci_map_rom(pdev, size);
154 if (res->flags & (IORESOURCE_ROM_COPY | IORESOURCE_ROM_SHADOW))
157 res->start = (unsigned long)kmalloc(*size, GFP_KERNEL);
161 res->end = res->start + *size;
162 memcpy_fromio((void*)res->start, rom, *size);
163 pci_unmap_rom(pdev, rom);
164 res->flags |= IORESOURCE_ROM_COPY;
166 return (void __iomem *)res->start;
170 * pci_unmap_rom - unmap the ROM from kernel space
171 * @pdev: pointer to pci device struct
172 * @rom: virtual address of the previous mapping
174 * Remove a mapping of a previously mapped ROM
176 void pci_unmap_rom(struct pci_dev *pdev, void __iomem *rom)
178 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
180 if (res->flags & IORESOURCE_ROM_COPY)
185 /* Disable again before continuing, leave enabled if pci=rom */
186 if (!(res->flags & (IORESOURCE_ROM_ENABLE | IORESOURCE_ROM_SHADOW)))
187 pci_disable_rom(pdev);
191 * pci_remove_rom - disable the ROM and remove its sysfs attribute
192 * @pdev: pointer to pci device struct
194 * Remove the rom file in sysfs and disable ROM decoding.
196 void pci_remove_rom(struct pci_dev *pdev)
198 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
200 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
201 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
202 if (!(res->flags & (IORESOURCE_ROM_ENABLE |
203 IORESOURCE_ROM_SHADOW |
204 IORESOURCE_ROM_COPY)))
205 pci_disable_rom(pdev);
209 * pci_cleanup_rom - internal routine for freeing the ROM copy created
210 * by pci_map_rom_copy called from remove.c
211 * @pdev: pointer to pci device struct
213 * Free the copied ROM if we allocated one.
215 void pci_cleanup_rom(struct pci_dev *pdev)
217 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
218 if (res->flags & IORESOURCE_ROM_COPY) {
219 kfree((void*)res->start);
220 res->flags &= ~IORESOURCE_ROM_COPY;
226 EXPORT_SYMBOL(pci_map_rom);
227 EXPORT_SYMBOL(pci_map_rom_copy);
228 EXPORT_SYMBOL(pci_unmap_rom);
229 EXPORT_SYMBOL(pci_remove_rom);