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>
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 static 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 static 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_map_rom - map a PCI ROM to kernel space
59 * @pdev: pointer to pci device struct
60 * @size: pointer to receive size of pci window over ROM
61 * @return: kernel virtual pointer to image of ROM
63 * Map a PCI ROM into kernel space. If ROM is boot video ROM,
64 * the shadow BIOS copy will be returned instead of the
67 void __iomem
*pci_map_rom(struct pci_dev
*pdev
, size_t *size
)
69 struct resource
*res
= &pdev
->resource
[PCI_ROM_RESOURCE
];
75 /* IORESOURCE_ROM_SHADOW only set on x86 */
76 if (res
->flags
& IORESOURCE_ROM_SHADOW
) {
77 /* primary video rom always starts here */
78 start
= (loff_t
)0xC0000;
79 *size
= 0x20000; /* cover C000:0 through E000:0 */
81 if (res
->flags
& IORESOURCE_ROM_COPY
) {
82 *size
= pci_resource_len(pdev
, PCI_ROM_RESOURCE
);
83 return (void __iomem
*)pci_resource_start(pdev
,
86 /* assign the ROM an address if it doesn't have one */
87 if (res
->parent
== NULL
&&
88 pci_assign_resource(pdev
,PCI_ROM_RESOURCE
))
90 start
= pci_resource_start(pdev
, PCI_ROM_RESOURCE
);
91 *size
= pci_resource_len(pdev
, PCI_ROM_RESOURCE
);
95 /* Enable ROM space decodes */
96 if (pci_enable_rom(pdev
))
101 rom
= ioremap(start
, *size
);
103 /* restore enable if ioremap fails */
104 if (!(res
->flags
& (IORESOURCE_ROM_ENABLE
|
105 IORESOURCE_ROM_SHADOW
|
106 IORESOURCE_ROM_COPY
)))
107 pci_disable_rom(pdev
);
112 * Try to find the true size of the ROM since sometimes the PCI window
113 * size is much larger than the actual size of the ROM.
114 * True size is important if the ROM is going to be copied.
119 /* Standard PCI ROMs start out with these bytes 55 AA */
120 if (readb(image
) != 0x55)
122 if (readb(image
+ 1) != 0xAA)
124 /* get the PCI data structure and check its signature */
125 pds
= image
+ readw(image
+ 24);
126 if (readb(pds
) != 'P')
128 if (readb(pds
+ 1) != 'C')
130 if (readb(pds
+ 2) != 'I')
132 if (readb(pds
+ 3) != 'R')
134 last_image
= readb(pds
+ 21) & 0x80;
135 /* this length is reliable */
136 image
+= readw(pds
+ 16) * 512;
137 } while (!last_image
);
139 /* never return a size larger than the PCI resource window */
140 /* there are known ROMs that get the size wrong */
141 *size
= min((size_t)(image
- rom
), *size
);
147 * pci_map_rom_copy - map a PCI ROM to kernel space, create a copy
148 * @pdev: pointer to pci device struct
149 * @size: pointer to receive size of pci window over ROM
150 * @return: kernel virtual pointer to image of ROM
152 * Map a PCI ROM into kernel space. If ROM is boot video ROM,
153 * the shadow BIOS copy will be returned instead of the
156 void __iomem
*pci_map_rom_copy(struct pci_dev
*pdev
, size_t *size
)
158 struct resource
*res
= &pdev
->resource
[PCI_ROM_RESOURCE
];
161 rom
= pci_map_rom(pdev
, size
);
165 if (res
->flags
& (IORESOURCE_ROM_COPY
| IORESOURCE_ROM_SHADOW
))
168 res
->start
= (unsigned long)kmalloc(*size
, GFP_KERNEL
);
172 res
->end
= res
->start
+ *size
;
173 memcpy_fromio((void*)res
->start
, rom
, *size
);
174 pci_unmap_rom(pdev
, rom
);
175 res
->flags
|= IORESOURCE_ROM_COPY
;
177 return (void __iomem
*)res
->start
;
181 * pci_unmap_rom - unmap the ROM from kernel space
182 * @pdev: pointer to pci device struct
183 * @rom: virtual address of the previous mapping
185 * Remove a mapping of a previously mapped ROM
187 void pci_unmap_rom(struct pci_dev
*pdev
, void __iomem
*rom
)
189 struct resource
*res
= &pdev
->resource
[PCI_ROM_RESOURCE
];
191 if (res
->flags
& IORESOURCE_ROM_COPY
)
196 /* Disable again before continuing, leave enabled if pci=rom */
197 if (!(res
->flags
& (IORESOURCE_ROM_ENABLE
| IORESOURCE_ROM_SHADOW
)))
198 pci_disable_rom(pdev
);
202 * pci_remove_rom - disable the ROM and remove its sysfs attribute
203 * @pdev: pointer to pci device struct
205 * Remove the rom file in sysfs and disable ROM decoding.
207 void pci_remove_rom(struct pci_dev
*pdev
)
209 struct resource
*res
= &pdev
->resource
[PCI_ROM_RESOURCE
];
211 if (pci_resource_len(pdev
, PCI_ROM_RESOURCE
))
212 sysfs_remove_bin_file(&pdev
->dev
.kobj
, pdev
->rom_attr
);
213 if (!(res
->flags
& (IORESOURCE_ROM_ENABLE
|
214 IORESOURCE_ROM_SHADOW
|
215 IORESOURCE_ROM_COPY
)))
216 pci_disable_rom(pdev
);
220 * pci_cleanup_rom - internal routine for freeing the ROM copy created
221 * by pci_map_rom_copy called from remove.c
222 * @pdev: pointer to pci device struct
224 * Free the copied ROM if we allocated one.
226 void pci_cleanup_rom(struct pci_dev
*pdev
)
228 struct resource
*res
= &pdev
->resource
[PCI_ROM_RESOURCE
];
229 if (res
->flags
& IORESOURCE_ROM_COPY
) {
230 kfree((void*)res
->start
);
231 res
->flags
&= ~IORESOURCE_ROM_COPY
;
237 EXPORT_SYMBOL(pci_map_rom
);
238 EXPORT_SYMBOL(pci_map_rom_copy
);
239 EXPORT_SYMBOL(pci_unmap_rom
);
240 EXPORT_SYMBOL(pci_remove_rom
);