1
2
3
4
5
6
7
8
9#include <linux/kernel.h>
10#include <linux/export.h>
11#include <linux/pci.h>
12#include <linux/slab.h>
13
14#include "pci.h"
15
16
17
18
19
20
21
22
23
24
25int pci_enable_rom(struct pci_dev *pdev)
26{
27 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
28 struct pci_bus_region region;
29 u32 rom_addr;
30
31 if (!res->flags)
32 return -1;
33
34
35 if (res->flags & IORESOURCE_ROM_SHADOW)
36 return 0;
37
38
39
40
41
42
43 pcibios_resource_to_bus(pdev->bus, ®ion, res);
44 pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
45 rom_addr &= ~PCI_ROM_ADDRESS_MASK;
46 rom_addr |= region.start | PCI_ROM_ADDRESS_ENABLE;
47 pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
48 return 0;
49}
50EXPORT_SYMBOL_GPL(pci_enable_rom);
51
52
53
54
55
56
57
58
59void pci_disable_rom(struct pci_dev *pdev)
60{
61 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
62 u32 rom_addr;
63
64 if (res->flags & IORESOURCE_ROM_SHADOW)
65 return;
66
67 pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
68 rom_addr &= ~PCI_ROM_ADDRESS_ENABLE;
69 pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
70}
71EXPORT_SYMBOL_GPL(pci_disable_rom);
72
73
74
75
76
77
78
79
80
81
82
83
84size_t pci_get_rom_size(struct pci_dev *pdev, void __iomem *rom, 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 dev_err(&pdev->dev, "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 dev_err(&pdev->dev, "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 } while (length && !last_image);
113
114
115
116 return min((size_t)(image - rom), size);
117}
118
119
120
121
122
123
124
125
126
127
128
129
130void __iomem *pci_map_rom(struct pci_dev *pdev, size_t *size)
131{
132 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
133 loff_t start;
134 void __iomem *rom;
135
136
137 if (res->parent == NULL && pci_assign_resource(pdev, PCI_ROM_RESOURCE))
138 return NULL;
139
140 start = pci_resource_start(pdev, PCI_ROM_RESOURCE);
141 *size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
142 if (*size == 0)
143 return NULL;
144
145
146 if (pci_enable_rom(pdev))
147 return NULL;
148
149 rom = ioremap(start, *size);
150 if (!rom) {
151
152 if (!(res->flags & IORESOURCE_ROM_ENABLE))
153 pci_disable_rom(pdev);
154 return NULL;
155 }
156
157
158
159
160
161
162 *size = pci_get_rom_size(pdev, rom, *size);
163 return rom;
164}
165EXPORT_SYMBOL(pci_map_rom);
166
167
168
169
170
171
172
173
174void pci_unmap_rom(struct pci_dev *pdev, void __iomem *rom)
175{
176 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
177
178 iounmap(rom);
179
180
181 if (!(res->flags & IORESOURCE_ROM_ENABLE))
182 pci_disable_rom(pdev);
183}
184EXPORT_SYMBOL(pci_unmap_rom);
185
186
187
188
189
190
191
192void __iomem *pci_platform_rom(struct pci_dev *pdev, size_t *size)
193{
194 if (pdev->rom && pdev->romlen) {
195 *size = pdev->romlen;
196 return phys_to_virt((phys_addr_t)pdev->rom);
197 }
198
199 return NULL;
200}
201EXPORT_SYMBOL(pci_platform_rom);
202