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);
134 * pci_map_rom_copy - map a PCI ROM to kernel space, create a copy
135 * @pdev: pointer to pci device struct
136 * @size: pointer to receive size of pci window over ROM
137 * @return: kernel virtual pointer to image of ROM
139 * Map a PCI ROM into kernel space. If ROM is boot video ROM,
140 * the shadow BIOS copy will be returned instead of the
143 void __iomem *pci_map_rom_copy(struct pci_dev *pdev, size_t *size)
145 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
148 rom = pci_map_rom(pdev, size);
152 if (res->flags & (IORESOURCE_ROM_COPY | IORESOURCE_ROM_SHADOW))
155 res->start = (unsigned long)kmalloc(*size, GFP_KERNEL);
159 res->end = res->start + *size;
160 memcpy_fromio((void*)res->start, rom, *size);
161 pci_unmap_rom(pdev, rom);
162 res->flags |= IORESOURCE_ROM_COPY;
164 return (void __iomem *)res->start;
168 * pci_unmap_rom - unmap the ROM from kernel space
169 * @pdev: pointer to pci device struct
170 * @rom: virtual address of the previous mapping
172 * Remove a mapping of a previously mapped ROM
174 void pci_unmap_rom(struct pci_dev *pdev, void __iomem *rom)
176 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
178 if (res->flags & IORESOURCE_ROM_COPY)
183 /* Disable again before continuing, leave enabled if pci=rom */
184 if (!(res->flags & (IORESOURCE_ROM_ENABLE | IORESOURCE_ROM_SHADOW)))
185 pci_disable_rom(pdev);
189 * pci_remove_rom - disable the ROM and remove its sysfs attribute
190 * @pdev: pointer to pci device struct
192 * Remove the rom file in sysfs and disable ROM decoding.
194 void pci_remove_rom(struct pci_dev *pdev)
196 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
198 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
199 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
200 if (!(res->flags & (IORESOURCE_ROM_ENABLE |
201 IORESOURCE_ROM_SHADOW |
202 IORESOURCE_ROM_COPY)))
203 pci_disable_rom(pdev);
207 * pci_cleanup_rom - internal routine for freeing the ROM copy created
208 * by pci_map_rom_copy called from remove.c
209 * @pdev: pointer to pci device struct
211 * Free the copied ROM if we allocated one.
213 void pci_cleanup_rom(struct pci_dev *pdev)
215 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
216 if (res->flags & IORESOURCE_ROM_COPY) {
217 kfree((void*)res->start);
218 res->flags &= ~IORESOURCE_ROM_COPY;
224 EXPORT_SYMBOL(pci_map_rom);
225 EXPORT_SYMBOL(pci_map_rom_copy);
226 EXPORT_SYMBOL(pci_unmap_rom);
227 EXPORT_SYMBOL(pci_remove_rom);