x86/bitops: Move BIT_64() for a wider use
[pandora-kernel.git] / drivers / pci / rom.c
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  */
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  * pci_enable_rom - enable ROM decoding for a PCI device
18  * @pdev: PCI device to enable
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  */
25 int 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         pcibios_resource_to_bus(pdev, &region, 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);
39         return 0;
40 }
41
42 /**
43  * pci_disable_rom - disable ROM decoding for a PCI device
44  * @pdev: PCI device to disable
45  *
46  * Disable ROM decoding on a PCI device by turning off the last bit in the
47  * ROM BAR.
48  */
49 void pci_disable_rom(struct pci_dev *pdev)
50 {
51         u32 rom_addr;
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);
55 }
56
57 /**
58  * pci_get_rom_size - obtain the actual size of the ROM image
59  * @pdev: target PCI device
60  * @rom: kernel virtual pointer to image of ROM
61  * @size: size of PCI window
62  *  return: size of actual ROM image
63  *
64  * Determine the actual length of the ROM image.
65  * The PCI window size could be much larger than the
66  * actual image size.
67  */
68 size_t pci_get_rom_size(struct pci_dev *pdev, void __iomem *rom, size_t size)
69 {
70         void __iomem *image;
71         int last_image;
72         unsigned length;
73
74         image = rom;
75         do {
76                 void __iomem *pds;
77                 /* Standard PCI ROMs start out with these bytes 55 AA */
78                 if (readb(image) != 0x55) {
79                         dev_err(&pdev->dev, "Invalid ROM contents\n");
80                         break;
81                 }
82                 if (readb(image + 1) != 0xAA)
83                         break;
84                 /* get the PCI data structure and check its signature */
85                 pds = image + readw(image + 24);
86                 if (readb(pds) != 'P')
87                         break;
88                 if (readb(pds + 1) != 'C')
89                         break;
90                 if (readb(pds + 2) != 'I')
91                         break;
92                 if (readb(pds + 3) != 'R')
93                         break;
94                 last_image = readb(pds + 21) & 0x80;
95                 length = readw(pds + 16);
96                 image += length * 512;
97         } while (length && !last_image);
98
99         /* never return a size larger than the PCI resource window */
100         /* there are known ROMs that get the size wrong */
101         return min((size_t)(image - rom), size);
102 }
103
104 /**
105  * pci_map_rom - map a PCI ROM to kernel space
106  * @pdev: pointer to pci device struct
107  * @size: pointer to receive size of pci window over ROM
108  *
109  * Return: kernel virtual pointer to image of ROM
110  *
111  * Map a PCI ROM into kernel space. If ROM is boot video ROM,
112  * the shadow BIOS copy will be returned instead of the
113  * actual ROM.
114  */
115 void __iomem *pci_map_rom(struct pci_dev *pdev, size_t *size)
116 {
117         struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
118         loff_t start;
119         void __iomem *rom;
120
121         /*
122          * IORESOURCE_ROM_SHADOW set on x86, x86_64 and IA64 supports legacy
123          * memory map if the VGA enable bit of the Bridge Control register is
124          * set for embedded VGA.
125          */
126         if (res->flags & IORESOURCE_ROM_SHADOW) {
127                 /* primary video rom always starts here */
128                 start = (loff_t)0xC0000;
129                 *size = 0x20000; /* cover C000:0 through E000:0 */
130         } else {
131                 if (res->flags &
132                         (IORESOURCE_ROM_COPY | IORESOURCE_ROM_BIOS_COPY)) {
133                         *size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
134                         return (void __iomem *)(unsigned long)
135                                 pci_resource_start(pdev, PCI_ROM_RESOURCE);
136                 } else {
137                         /* assign the ROM an address if it doesn't have one */
138                         if (res->parent == NULL &&
139                             pci_assign_resource(pdev,PCI_ROM_RESOURCE))
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;
145
146                         /* Enable ROM space decodes */
147                         if (pci_enable_rom(pdev))
148                                 return NULL;
149                 }
150         }
151
152         rom = ioremap(start, *size);
153         if (!rom) {
154                 /* restore enable if ioremap fails */
155                 if (!(res->flags & (IORESOURCE_ROM_ENABLE |
156                                     IORESOURCE_ROM_SHADOW |
157                                     IORESOURCE_ROM_COPY)))
158                         pci_disable_rom(pdev);
159                 return NULL;
160         }
161
162         /*
163          * Try to find the true size of the ROM since sometimes the PCI window
164          * size is much larger than the actual size of the ROM.
165          * True size is important if the ROM is going to be copied.
166          */
167         *size = pci_get_rom_size(pdev, rom, *size);
168         return rom;
169 }
170
171 #if 0
172 /**
173  * pci_map_rom_copy - map a PCI ROM to kernel space, create a copy
174  * @pdev: pointer to pci device struct
175  * @size: pointer to receive size of pci window over ROM
176  *
177  * Return: kernel virtual pointer to image of ROM
178  *
179  * Map a PCI ROM into kernel space. If ROM is boot video ROM,
180  * the shadow BIOS copy will be returned instead of the
181  * actual ROM.
182  */
183 void __iomem *pci_map_rom_copy(struct pci_dev *pdev, size_t *size)
184 {
185         struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
186         void __iomem *rom;
187
188         rom = pci_map_rom(pdev, size);
189         if (!rom)
190                 return NULL;
191
192         if (res->flags & (IORESOURCE_ROM_COPY | IORESOURCE_ROM_SHADOW |
193                           IORESOURCE_ROM_BIOS_COPY))
194                 return rom;
195
196         res->start = (unsigned long)kmalloc(*size, GFP_KERNEL);
197         if (!res->start)
198                 return rom;
199
200         res->end = res->start + *size;
201         memcpy_fromio((void*)(unsigned long)res->start, rom, *size);
202         pci_unmap_rom(pdev, rom);
203         res->flags |= IORESOURCE_ROM_COPY;
204
205         return (void __iomem *)(unsigned long)res->start;
206 }
207 #endif  /*  0  */
208
209 /**
210  * pci_unmap_rom - unmap the ROM from kernel space
211  * @pdev: pointer to pci device struct
212  * @rom: virtual address of the previous mapping
213  *
214  * Remove a mapping of a previously mapped ROM
215  */
216 void pci_unmap_rom(struct pci_dev *pdev, void __iomem *rom)
217 {
218         struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
219
220         if (res->flags & (IORESOURCE_ROM_COPY | IORESOURCE_ROM_BIOS_COPY))
221                 return;
222
223         iounmap(rom);
224
225         /* Disable again before continuing, leave enabled if pci=rom */
226         if (!(res->flags & (IORESOURCE_ROM_ENABLE | IORESOURCE_ROM_SHADOW)))
227                 pci_disable_rom(pdev);
228 }
229
230 #if 0
231 /**
232  * pci_remove_rom - disable the ROM and remove its sysfs attribute
233  * @pdev: pointer to pci device struct
234  *
235  * Remove the rom file in sysfs and disable ROM decoding.
236  */
237 void pci_remove_rom(struct pci_dev *pdev)
238 {
239         struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
240
241         if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
242                 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
243         if (!(res->flags & (IORESOURCE_ROM_ENABLE |
244                             IORESOURCE_ROM_SHADOW |
245                             IORESOURCE_ROM_BIOS_COPY |
246                             IORESOURCE_ROM_COPY)))
247                 pci_disable_rom(pdev);
248 }
249 #endif  /*  0  */
250
251 /**
252  * pci_cleanup_rom - free the ROM copy created by pci_map_rom_copy
253  * @pdev: pointer to pci device struct
254  *
255  * Free the copied ROM if we allocated one.
256  */
257 void pci_cleanup_rom(struct pci_dev *pdev)
258 {
259         struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
260         if (res->flags & IORESOURCE_ROM_COPY) {
261                 kfree((void*)(unsigned long)res->start);
262                 res->flags &= ~IORESOURCE_ROM_COPY;
263                 res->start = 0;
264                 res->end = 0;
265         }
266 }
267
268 EXPORT_SYMBOL(pci_map_rom);
269 EXPORT_SYMBOL(pci_unmap_rom);
270 EXPORT_SYMBOL_GPL(pci_enable_rom);
271 EXPORT_SYMBOL_GPL(pci_disable_rom);