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 pcibios_resource_to_bus(pdev->bus, ®ion, res);
39 pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
40 rom_addr &= ~PCI_ROM_ADDRESS_MASK;
41 rom_addr |= region.start | PCI_ROM_ADDRESS_ENABLE;
42 pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
43 return 0;
44}
45EXPORT_SYMBOL_GPL(pci_enable_rom);
46
47
48
49
50
51
52
53
54void pci_disable_rom(struct pci_dev *pdev)
55{
56 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
57 u32 rom_addr;
58
59 if (res->flags & IORESOURCE_ROM_SHADOW)
60 return;
61
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}
66EXPORT_SYMBOL_GPL(pci_disable_rom);
67
68
69
70
71
72
73
74
75
76
77
78
79size_t pci_get_rom_size(struct pci_dev *pdev, void __iomem *rom, size_t size)
80{
81 void __iomem *image;
82 int last_image;
83 unsigned length;
84
85 image = rom;
86 do {
87 void __iomem *pds;
88
89 if (readw(image) != 0xAA55) {
90 dev_err(&pdev->dev, "Invalid PCI ROM header signature: expecting 0xaa55, got %#06x\n",
91 readw(image));
92 break;
93 }
94
95 pds = image + readw(image + 24);
96 if (readl(pds) != 0x52494350) {
97 dev_err(&pdev->dev, "Invalid PCI ROM data signature: expecting 0x52494350, got %#010x\n",
98 readl(pds));
99 break;
100 }
101 last_image = readb(pds + 21) & 0x80;
102 length = readw(pds + 16);
103 image += length * 512;
104
105 if (image > rom + size)
106 break;
107 } while (length && !last_image);
108
109
110
111 return min((size_t)(image - rom), size);
112}
113
114
115
116
117
118
119
120
121
122
123
124
125void __iomem *pci_map_rom(struct pci_dev *pdev, size_t *size)
126{
127 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
128 loff_t start;
129 void __iomem *rom;
130
131
132 if (res->parent == NULL && pci_assign_resource(pdev, PCI_ROM_RESOURCE))
133 return NULL;
134
135 start = pci_resource_start(pdev, PCI_ROM_RESOURCE);
136 *size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
137 if (*size == 0)
138 return NULL;
139
140
141 if (pci_enable_rom(pdev))
142 return NULL;
143
144 rom = ioremap(start, *size);
145 if (!rom) {
146
147 if (!(res->flags & IORESOURCE_ROM_ENABLE))
148 pci_disable_rom(pdev);
149 return NULL;
150 }
151
152
153
154
155
156
157 *size = pci_get_rom_size(pdev, rom, *size);
158 return rom;
159}
160EXPORT_SYMBOL(pci_map_rom);
161
162
163
164
165
166
167
168
169void pci_unmap_rom(struct pci_dev *pdev, void __iomem *rom)
170{
171 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
172
173 iounmap(rom);
174
175
176 if (!(res->flags & IORESOURCE_ROM_ENABLE))
177 pci_disable_rom(pdev);
178}
179EXPORT_SYMBOL(pci_unmap_rom);
180
181
182
183
184
185
186
187void __iomem *pci_platform_rom(struct pci_dev *pdev, size_t *size)
188{
189 if (pdev->rom && pdev->romlen) {
190 *size = pdev->romlen;
191 return phys_to_virt((phys_addr_t)pdev->rom);
192 }
193
194 return NULL;
195}
196EXPORT_SYMBOL(pci_platform_rom);
197