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
12 #include <linux/config.h>
13 #include <linux/kernel.h>
14 #include <linux/pci.h>
19 * pci_enable_rom - enable ROM decoding for a PCI device
20 * @dev: PCI device to enable
22 * Enable ROM decoding on @dev. This involves simply turning on the last
23 * bit of the PCI ROM BAR. Note that some cards may share address decoders
24 * between the ROM and other resources, so enabling it may disable access
25 * to MMIO registers or other card memory.
28 pci_enable_rom(struct pci_dev *pdev)
32 pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
33 rom_addr |= PCI_ROM_ADDRESS_ENABLE;
34 pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
38 * pci_disable_rom - disable ROM decoding for a PCI device
39 * @dev: PCI device to disable
41 * Disable ROM decoding on a PCI device by turning off the last bit in the
45 pci_disable_rom(struct pci_dev *pdev)
48 pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
49 rom_addr &= ~PCI_ROM_ADDRESS_ENABLE;
50 pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
54 * pci_map_rom - map a PCI ROM to kernel space
55 * @dev: pointer to pci device struct
56 * @size: pointer to receive size of pci window over ROM
57 * @return: kernel virtual pointer to image of ROM
59 * Map a PCI ROM into kernel space. If ROM is boot video ROM,
60 * the shadow BIOS copy will be returned instead of the
63 void __iomem *pci_map_rom(struct pci_dev *pdev, size_t *size)
65 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
71 if (res->flags & IORESOURCE_ROM_SHADOW) { /* IORESOURCE_ROM_SHADOW only set on x86 */
72 start = (loff_t)0xC0000; /* primary video rom always starts here */
73 *size = 0x20000; /* cover C000:0 through E000:0 */
75 if (res->flags & IORESOURCE_ROM_COPY) {
76 *size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
77 return (void __iomem *)pci_resource_start(pdev, PCI_ROM_RESOURCE);
79 /* assign the ROM an address if it doesn't have one */
80 if (res->parent == NULL)
81 pci_assign_resource(pdev, PCI_ROM_RESOURCE);
83 start = pci_resource_start(pdev, PCI_ROM_RESOURCE);
84 *size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
88 /* Enable ROM space decodes */
93 rom = ioremap(start, *size);
95 /* restore enable if ioremap fails */
96 if (!(res->flags & (IORESOURCE_ROM_ENABLE | IORESOURCE_ROM_SHADOW | IORESOURCE_ROM_COPY)))
97 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. */
107 /* Standard PCI ROMs start out with these bytes 55 AA */
108 if (readb(image) != 0x55)
110 if (readb(image + 1) != 0xAA)
112 /* get the PCI data structure and check its signature */
113 pds = image + readw(image + 24);
114 if (readb(pds) != 'P')
116 if (readb(pds + 1) != 'C')
118 if (readb(pds + 2) != 'I')
120 if (readb(pds + 3) != 'R')
122 last_image = readb(pds + 21) & 0x80;
123 /* this length is reliable */
124 image += readw(pds + 16) * 512;
125 } while (!last_image);
133 * pci_map_rom_copy - map a PCI ROM to kernel space, create a copy
134 * @dev: pointer to pci device struct
135 * @size: pointer to receive size of pci window over ROM
136 * @return: kernel virtual pointer to image of ROM
138 * Map a PCI ROM into kernel space. If ROM is boot video ROM,
139 * the shadow BIOS copy will be returned instead of the
142 void __iomem *pci_map_rom_copy(struct pci_dev *pdev, size_t *size)
144 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
147 rom = pci_map_rom(pdev, size);
151 if (res->flags & (IORESOURCE_ROM_COPY | IORESOURCE_ROM_SHADOW))
154 res->start = (unsigned long)kmalloc(*size, GFP_KERNEL);
158 res->end = res->start + *size;
159 memcpy_fromio((void*)res->start, rom, *size);
160 pci_unmap_rom(pdev, rom);
161 res->flags |= IORESOURCE_ROM_COPY;
163 return (void __iomem *)res->start;
167 * pci_unmap_rom - unmap the ROM from kernel space
168 * @dev: pointer to pci device struct
169 * @rom: virtual address of the previous mapping
171 * Remove a mapping of a previously mapped ROM
174 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 * @dev: pointer to pci device struct
194 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 | IORESOURCE_ROM_SHADOW | IORESOURCE_ROM_COPY)))
201 pci_disable_rom(pdev);
205 * pci_cleanup_rom - internal routine for freeing the ROM copy created
206 * by pci_map_rom_copy called from remove.c
207 * @dev: pointer to pci device struct
211 pci_cleanup_rom(struct pci_dev *pdev)
213 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
214 if (res->flags & IORESOURCE_ROM_COPY) {
215 kfree((void*)res->start);
216 res->flags &= ~IORESOURCE_ROM_COPY;
222 EXPORT_SYMBOL(pci_map_rom);
223 EXPORT_SYMBOL(pci_map_rom_copy);
224 EXPORT_SYMBOL(pci_unmap_rom);
225 EXPORT_SYMBOL(pci_remove_rom);