mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / pci / rom.c
blobd6d499782fb43ad7083a7ce7f9627b75b2065407
1 /*
2 * drivers/pci/rom.c
4 * (C) Copyright 2004 Jon Smirl <jonsmirl@yahoo.com>
5 * (C) Copyright 2004 Silicon Graphics, Inc. Jesse Barnes <jbarnes@sgi.com>
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>
14 #include "pci.h"
16 /**
17 * pci_enable_rom - enable ROM decoding for a PCI device
18 * @pdev: PCI device to enable
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.
25 int pci_enable_rom(struct pci_dev *pdev)
27 struct resource *res = pdev->resource + PCI_ROM_RESOURCE;
28 struct pci_bus_region region;
29 u32 rom_addr;
31 if (!res->flags)
32 return -1;
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;
42 /**
43 * pci_disable_rom - disable ROM decoding for a PCI device
44 * @pdev: PCI device to disable
46 * Disable ROM decoding on a PCI device by turning off the last bit in the
47 * ROM BAR.
49 void pci_disable_rom(struct pci_dev *pdev)
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);
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
64 * Determine the actual length of the ROM image.
65 * The PCI window size could be much larger than the
66 * actual image size.
68 size_t pci_get_rom_size(struct pci_dev *pdev, void __iomem *rom, size_t size)
70 void __iomem *image;
71 int last_image;
72 unsigned length;
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;
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);
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);
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
109 * Return: kernel virtual pointer to image of ROM
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.
115 void __iomem *pci_map_rom(struct pci_dev *pdev, size_t *size)
117 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
118 loff_t start;
119 void __iomem *rom;
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.
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;
146 /* Enable ROM space decodes */
147 if (pci_enable_rom(pdev))
148 return NULL;
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;
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.
167 *size = pci_get_rom_size(pdev, rom, *size);
168 return rom;
172 * pci_unmap_rom - unmap the ROM from kernel space
173 * @pdev: pointer to pci device struct
174 * @rom: virtual address of the previous mapping
176 * Remove a mapping of a previously mapped ROM
178 void pci_unmap_rom(struct pci_dev *pdev, void __iomem *rom)
180 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
182 if (res->flags & (IORESOURCE_ROM_COPY | IORESOURCE_ROM_BIOS_COPY))
183 return;
185 iounmap(rom);
187 /* Disable again before continuing, leave enabled if pci=rom */
188 if (!(res->flags & (IORESOURCE_ROM_ENABLE | IORESOURCE_ROM_SHADOW)))
189 pci_disable_rom(pdev);
193 * pci_cleanup_rom - free the ROM copy created by pci_map_rom_copy
194 * @pdev: pointer to pci device struct
196 * Free the copied ROM if we allocated one.
198 void pci_cleanup_rom(struct pci_dev *pdev)
200 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
201 if (res->flags & IORESOURCE_ROM_COPY) {
202 kfree((void*)(unsigned long)res->start);
203 res->flags &= ~IORESOURCE_ROM_COPY;
204 res->start = 0;
205 res->end = 0;
210 * pci_platform_rom - provides a pointer to any ROM image provided by the
211 * platform
212 * @pdev: pointer to pci device struct
213 * @size: pointer to receive size of pci window over ROM
215 void __iomem *pci_platform_rom(struct pci_dev *pdev, size_t *size)
217 if (pdev->rom && pdev->romlen) {
218 *size = pdev->romlen;
219 return phys_to_virt((phys_addr_t)pdev->rom);
222 return NULL;
225 EXPORT_SYMBOL(pci_map_rom);
226 EXPORT_SYMBOL(pci_unmap_rom);
227 EXPORT_SYMBOL_GPL(pci_enable_rom);
228 EXPORT_SYMBOL_GPL(pci_disable_rom);
229 EXPORT_SYMBOL(pci_platform_rom);