]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * drivers/pci/rom.c | |
3 | * | |
4 | * (C) Copyright 2004 Jon Smirl <jonsmirl@yahoo.com> | |
5 | * (C) Copyright 2004 Silicon Graphics, Inc. Jesse Barnes <jbarnes@sgi.com> | |
6 | * | |
7 | * PCI ROM access routines | |
8 | */ | |
1da177e4 | 9 | #include <linux/kernel.h> |
363c75db | 10 | #include <linux/export.h> |
1da177e4 | 11 | #include <linux/pci.h> |
4e57b681 | 12 | #include <linux/slab.h> |
1da177e4 LT |
13 | |
14 | #include "pci.h" | |
15 | ||
16 | /** | |
17 | * pci_enable_rom - enable ROM decoding for a PCI device | |
67be2dd1 | 18 | * @pdev: PCI device to enable |
1da177e4 LT |
19 | * |
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. | |
24 | */ | |
e416de5e | 25 | int pci_enable_rom(struct pci_dev *pdev) |
1da177e4 | 26 | { |
4708f9a5 | 27 | struct resource *res = &pdev->resource[PCI_ROM_RESOURCE]; |
8085ce08 | 28 | struct pci_bus_region region; |
1da177e4 LT |
29 | u32 rom_addr; |
30 | ||
8085ce08 BH |
31 | if (!res->flags) |
32 | return -1; | |
33 | ||
4708f9a5 BH |
34 | /* Nothing to enable if we're using a shadow copy in RAM */ |
35 | if (res->flags & IORESOURCE_ROM_SHADOW) | |
36 | return 0; | |
37 | ||
fc279850 | 38 | pcibios_resource_to_bus(pdev->bus, ®ion, res); |
1da177e4 | 39 | pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr); |
8085ce08 BH |
40 | rom_addr &= ~PCI_ROM_ADDRESS_MASK; |
41 | rom_addr |= region.start | PCI_ROM_ADDRESS_ENABLE; | |
1da177e4 | 42 | pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr); |
8085ce08 | 43 | return 0; |
1da177e4 | 44 | } |
b7fe9434 | 45 | EXPORT_SYMBOL_GPL(pci_enable_rom); |
1da177e4 LT |
46 | |
47 | /** | |
48 | * pci_disable_rom - disable ROM decoding for a PCI device | |
67be2dd1 | 49 | * @pdev: PCI device to disable |
1da177e4 LT |
50 | * |
51 | * Disable ROM decoding on a PCI device by turning off the last bit in the | |
52 | * ROM BAR. | |
53 | */ | |
e416de5e | 54 | void pci_disable_rom(struct pci_dev *pdev) |
1da177e4 | 55 | { |
4708f9a5 | 56 | struct resource *res = &pdev->resource[PCI_ROM_RESOURCE]; |
1da177e4 | 57 | u32 rom_addr; |
4708f9a5 BH |
58 | |
59 | if (res->flags & IORESOURCE_ROM_SHADOW) | |
60 | return; | |
61 | ||
1da177e4 LT |
62 | pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr); |
63 | rom_addr &= ~PCI_ROM_ADDRESS_ENABLE; | |
64 | pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr); | |
65 | } | |
b7fe9434 | 66 | EXPORT_SYMBOL_GPL(pci_disable_rom); |
1da177e4 | 67 | |
d7ad2254 JK |
68 | /** |
69 | * pci_get_rom_size - obtain the actual size of the ROM image | |
4cc59c72 | 70 | * @pdev: target PCI device |
d7ad2254 JK |
71 | * @rom: kernel virtual pointer to image of ROM |
72 | * @size: size of PCI window | |
73 | * return: size of actual ROM image | |
74 | * | |
75 | * Determine the actual length of the ROM image. | |
76 | * The PCI window size could be much larger than the | |
77 | * actual image size. | |
78 | */ | |
97c44836 | 79 | size_t pci_get_rom_size(struct pci_dev *pdev, void __iomem *rom, size_t size) |
d7ad2254 JK |
80 | { |
81 | void __iomem *image; | |
82 | int last_image; | |
16b036af | 83 | unsigned length; |
d7ad2254 JK |
84 | |
85 | image = rom; | |
86 | do { | |
87 | void __iomem *pds; | |
88 | /* Standard PCI ROMs start out with these bytes 55 AA */ | |
4066df63 VD |
89 | if (readw(image) != 0xAA55) { |
90 | dev_err(&pdev->dev, "Invalid PCI ROM header signature: expecting 0xaa55, got %#06x\n", | |
91 | readw(image)); | |
d7ad2254 | 92 | break; |
97c44836 | 93 | } |
4066df63 | 94 | /* get the PCI data structure and check its "PCIR" signature */ |
d7ad2254 | 95 | pds = image + readw(image + 24); |
4066df63 VD |
96 | if (readl(pds) != 0x52494350) { |
97 | dev_err(&pdev->dev, "Invalid PCI ROM data signature: expecting 0x52494350, got %#010x\n", | |
98 | readl(pds)); | |
d7ad2254 | 99 | break; |
4066df63 | 100 | } |
d7ad2254 | 101 | last_image = readb(pds + 21) & 0x80; |
16b036af MD |
102 | length = readw(pds + 16); |
103 | image += length * 512; | |
47b975d2 EC |
104 | /* Avoid iterating through memory outside the resource window */ |
105 | if (image > rom + size) | |
106 | break; | |
16b036af | 107 | } while (length && !last_image); |
d7ad2254 JK |
108 | |
109 | /* never return a size larger than the PCI resource window */ | |
110 | /* there are known ROMs that get the size wrong */ | |
111 | return min((size_t)(image - rom), size); | |
112 | } | |
113 | ||
1da177e4 LT |
114 | /** |
115 | * pci_map_rom - map a PCI ROM to kernel space | |
67be2dd1 | 116 | * @pdev: pointer to pci device struct |
1da177e4 | 117 | * @size: pointer to receive size of pci window over ROM |
f5dafca5 RD |
118 | * |
119 | * Return: kernel virtual pointer to image of ROM | |
1da177e4 LT |
120 | * |
121 | * Map a PCI ROM into kernel space. If ROM is boot video ROM, | |
122 | * the shadow BIOS copy will be returned instead of the | |
123 | * actual ROM. | |
124 | */ | |
125 | void __iomem *pci_map_rom(struct pci_dev *pdev, size_t *size) | |
126 | { | |
127 | struct resource *res = &pdev->resource[PCI_ROM_RESOURCE]; | |
fffe01f7 | 128 | loff_t start; |
1da177e4 | 129 | void __iomem *rom; |
1da177e4 | 130 | |
a2302c68 JK |
131 | if (res->flags & |
132 | (IORESOURCE_ROM_COPY | IORESOURCE_ROM_BIOS_COPY)) { | |
1da177e4 | 133 | *size = pci_resource_len(pdev, PCI_ROM_RESOURCE); |
e31dd6e4 GKH |
134 | return (void __iomem *)(unsigned long) |
135 | pci_resource_start(pdev, PCI_ROM_RESOURCE); | |
1da177e4 | 136 | } else { |
fffe01f7 MG |
137 | /* assign the ROM an address if it doesn't have one */ |
138 | if (res->parent == NULL && | |
3c78bc61 | 139 | pci_assign_resource(pdev, PCI_ROM_RESOURCE)) |
fffe01f7 MG |
140 | return NULL; |
141 | start = pci_resource_start(pdev, PCI_ROM_RESOURCE); | |
142 | *size = pci_resource_len(pdev, PCI_ROM_RESOURCE); | |
143 | if (*size == 0) | |
144 | return NULL; | |
1da177e4 | 145 | |
fffe01f7 MG |
146 | /* Enable ROM space decodes */ |
147 | if (pci_enable_rom(pdev)) | |
148 | return NULL; | |
149 | } | |
547b5246 | 150 | |
1da177e4 LT |
151 | rom = ioremap(start, *size); |
152 | if (!rom) { | |
153 | /* restore enable if ioremap fails */ | |
154 | if (!(res->flags & (IORESOURCE_ROM_ENABLE | | |
1da177e4 LT |
155 | IORESOURCE_ROM_COPY))) |
156 | pci_disable_rom(pdev); | |
157 | return NULL; | |
158 | } | |
159 | ||
160 | /* | |
161 | * Try to find the true size of the ROM since sometimes the PCI window | |
162 | * size is much larger than the actual size of the ROM. | |
163 | * True size is important if the ROM is going to be copied. | |
164 | */ | |
97c44836 | 165 | *size = pci_get_rom_size(pdev, rom, *size); |
1da177e4 LT |
166 | return rom; |
167 | } | |
b7fe9434 | 168 | EXPORT_SYMBOL(pci_map_rom); |
1da177e4 | 169 | |
1da177e4 LT |
170 | /** |
171 | * pci_unmap_rom - unmap the ROM from kernel space | |
67be2dd1 | 172 | * @pdev: pointer to pci device struct |
1da177e4 LT |
173 | * @rom: virtual address of the previous mapping |
174 | * | |
175 | * Remove a mapping of a previously mapped ROM | |
176 | */ | |
177 | void pci_unmap_rom(struct pci_dev *pdev, void __iomem *rom) | |
178 | { | |
179 | struct resource *res = &pdev->resource[PCI_ROM_RESOURCE]; | |
180 | ||
a2302c68 | 181 | if (res->flags & (IORESOURCE_ROM_COPY | IORESOURCE_ROM_BIOS_COPY)) |
1da177e4 LT |
182 | return; |
183 | ||
fffe01f7 | 184 | iounmap(rom); |
1da177e4 | 185 | |
4708f9a5 BH |
186 | /* Disable again before continuing */ |
187 | if (!(res->flags & IORESOURCE_ROM_ENABLE)) | |
1da177e4 LT |
188 | pci_disable_rom(pdev); |
189 | } | |
b7fe9434 | 190 | EXPORT_SYMBOL(pci_unmap_rom); |
1da177e4 | 191 | |
1da177e4 | 192 | /** |
0643245f | 193 | * pci_cleanup_rom - free the ROM copy created by pci_map_rom_copy |
67be2dd1 | 194 | * @pdev: pointer to pci device struct |
1da177e4 LT |
195 | * |
196 | * Free the copied ROM if we allocated one. | |
197 | */ | |
198 | void pci_cleanup_rom(struct pci_dev *pdev) | |
199 | { | |
200 | struct resource *res = &pdev->resource[PCI_ROM_RESOURCE]; | |
bd064f0a | 201 | |
1da177e4 | 202 | if (res->flags & IORESOURCE_ROM_COPY) { |
3c78bc61 | 203 | kfree((void *)(unsigned long)res->start); |
bd064f0a | 204 | res->flags |= IORESOURCE_UNSET; |
1da177e4 LT |
205 | res->flags &= ~IORESOURCE_ROM_COPY; |
206 | res->start = 0; | |
207 | res->end = 0; | |
208 | } | |
209 | } | |
210 | ||
fffe01f7 MG |
211 | /** |
212 | * pci_platform_rom - provides a pointer to any ROM image provided by the | |
213 | * platform | |
214 | * @pdev: pointer to pci device struct | |
215 | * @size: pointer to receive size of pci window over ROM | |
216 | */ | |
217 | void __iomem *pci_platform_rom(struct pci_dev *pdev, size_t *size) | |
218 | { | |
219 | if (pdev->rom && pdev->romlen) { | |
220 | *size = pdev->romlen; | |
221 | return phys_to_virt((phys_addr_t)pdev->rom); | |
222 | } | |
223 | ||
224 | return NULL; | |
225 | } | |
fffe01f7 | 226 | EXPORT_SYMBOL(pci_platform_rom); |