1
2
3
4
5
6
7
8#include <linux/kernel.h>
9#include <linux/export.h>
10#include <linux/pci.h>
11#include <linux/slab.h>
12
13#include "pci.h"
14
15
16
17
18
19
20
21
22
23
24int pci_enable_rom(struct pci_dev *pdev)
25{
26 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
27 struct pci_bus_region region;
28 u32 rom_addr;
29
30 if (!res->flags)
31 return -1;
32
33
34 if (res->flags & IORESOURCE_ROM_SHADOW)
35 return 0;
36
37
38
39
40
41
42 pcibios_resource_to_bus(pdev->bus, ®ion, res);
43 pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
44 rom_addr &= ~PCI_ROM_ADDRESS_MASK;
45 rom_addr |= region.start | PCI_ROM_ADDRESS_ENABLE;
46 pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
47 return 0;
48}
49EXPORT_SYMBOL_GPL(pci_enable_rom);
50
51
52
53
54
55
56
57
58void pci_disable_rom(struct pci_dev *pdev)
59{
60 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
61 u32 rom_addr;
62
63 if (res->flags & IORESOURCE_ROM_SHADOW)
64 return;
65
66 pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
67 rom_addr &= ~PCI_ROM_ADDRESS_ENABLE;
68 pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
69}
70EXPORT_SYMBOL_GPL(pci_disable_rom);
71
72
73
74
75
76
77
78
79
80
81
82
83static size_t pci_get_rom_size(struct pci_dev *pdev, void __iomem *rom,
84 size_t size)
85{
86 void __iomem *image;
87 int last_image;
88 unsigned length;
89
90 image = rom;
91 do {
92 void __iomem *pds;
93
94 if (readw(image) != 0xAA55) {
95 pci_info(pdev, "Invalid PCI ROM header signature: expecting 0xaa55, got %#06x\n",
96 readw(image));
97 break;
98 }
99
100 pds = image + readw(image + 24);
101 if (readl(pds) != 0x52494350) {
102 pci_info(pdev, "Invalid PCI ROM data signature: expecting 0x52494350, got %#010x\n",
103 readl(pds));
104 break;
105 }
106 last_image = readb(pds + 21) & 0x80;
107 length = readw(pds + 16);
108 image += length * 512;
109
110 if (image >= rom + size)
111 break;
112 if (!last_image) {
113 if (readw(image) != 0xAA55) {
114 pci_info(pdev, "No more image in the PCI ROM\n");
115 break;
116 }
117 }
118 } while (length && !last_image);
119
120
121
122 return min((size_t)(image - rom), size);
123}
124
125
126
127
128
129
130
131
132
133
134
135
136void __iomem *pci_map_rom(struct pci_dev *pdev, size_t *size)
137{
138 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
139 loff_t start;
140 void __iomem *rom;
141
142
143 if (res->parent == NULL && pci_assign_resource(pdev, PCI_ROM_RESOURCE))
144 return NULL;
145
146 start = pci_resource_start(pdev, PCI_ROM_RESOURCE);
147 *size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
148 if (*size == 0)
149 return NULL;
150
151
152 if (pci_enable_rom(pdev))
153 return NULL;
154
155 rom = ioremap(start, *size);
156 if (!rom)
157 goto err_ioremap;
158
159
160
161
162
163
164 *size = pci_get_rom_size(pdev, rom, *size);
165 if (!*size)
166 goto invalid_rom;
167
168 return rom;
169
170invalid_rom:
171 iounmap(rom);
172err_ioremap:
173
174 if (!(res->flags & IORESOURCE_ROM_ENABLE))
175 pci_disable_rom(pdev);
176 return NULL;
177}
178EXPORT_SYMBOL(pci_map_rom);
179
180
181
182
183
184
185
186
187void pci_unmap_rom(struct pci_dev *pdev, void __iomem *rom)
188{
189 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
190
191 iounmap(rom);
192
193
194 if (!(res->flags & IORESOURCE_ROM_ENABLE))
195 pci_disable_rom(pdev);
196}
197EXPORT_SYMBOL(pci_unmap_rom);
198
199
200
201
202
203
204
205void __iomem *pci_platform_rom(struct pci_dev *pdev, size_t *size)
206{
207 if (pdev->rom && pdev->romlen) {
208 *size = pdev->romlen;
209 return phys_to_virt((phys_addr_t)pdev->rom);
210 }
211
212 return NULL;
213}
214EXPORT_SYMBOL(pci_platform_rom);
215