blob: e1dcefc69bb4fb14873ff0c8622d7a971eb21330 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/kernel.h>
10#include <linux/pci.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080011#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
13#include "pci.h"
14
15/**
16 * pci_enable_rom - enable ROM decoding for a PCI device
Martin Waitz67be2dd2005-05-01 08:59:26 -070017 * @pdev: PCI device to enable
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 *
19 * Enable ROM decoding on @dev. This involves simply turning on the last
20 * bit of the PCI ROM BAR. Note that some cards may share address decoders
21 * between the ROM and other resources, so enabling it may disable access
22 * to MMIO registers or other card memory.
23 */
Benjamin Herrenschmidt8085ce082005-08-31 14:16:53 +100024static int pci_enable_rom(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
Benjamin Herrenschmidt8085ce082005-08-31 14:16:53 +100026 struct resource *res = pdev->resource + PCI_ROM_RESOURCE;
27 struct pci_bus_region region;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 u32 rom_addr;
29
Benjamin Herrenschmidt8085ce082005-08-31 14:16:53 +100030 if (!res->flags)
31 return -1;
32
33 pcibios_resource_to_bus(pdev, &region, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
Benjamin Herrenschmidt8085ce082005-08-31 14:16:53 +100035 rom_addr &= ~PCI_ROM_ADDRESS_MASK;
36 rom_addr |= region.start | PCI_ROM_ADDRESS_ENABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
Benjamin Herrenschmidt8085ce082005-08-31 14:16:53 +100038 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039}
40
41/**
42 * pci_disable_rom - disable ROM decoding for a PCI device
Martin Waitz67be2dd2005-05-01 08:59:26 -070043 * @pdev: PCI device to disable
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 *
45 * Disable ROM decoding on a PCI device by turning off the last bit in the
46 * ROM BAR.
47 */
48static void pci_disable_rom(struct pci_dev *pdev)
49{
50 u32 rom_addr;
51 pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
52 rom_addr &= ~PCI_ROM_ADDRESS_ENABLE;
53 pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
54}
55
56/**
57 * pci_map_rom - map a PCI ROM to kernel space
Martin Waitz67be2dd2005-05-01 08:59:26 -070058 * @pdev: pointer to pci device struct
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * @size: pointer to receive size of pci window over ROM
60 * @return: kernel virtual pointer to image of ROM
61 *
62 * Map a PCI ROM into kernel space. If ROM is boot video ROM,
63 * the shadow BIOS copy will be returned instead of the
64 * actual ROM.
65 */
66void __iomem *pci_map_rom(struct pci_dev *pdev, size_t *size)
67{
68 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
69 loff_t start;
70 void __iomem *rom;
71 void __iomem *image;
72 int last_image;
73
eiichiro.oiwa.nm@hitachi.comb5e4efe2006-09-28 13:55:47 +090074 /*
Eiichiro Oiwa6b5c76b2006-10-23 15:14:07 +090075 * IORESOURCE_ROM_SHADOW set on x86, x86_64 and IA64 supports legacy
76 * memory map if the VGA enable bit of the Bridge Control register is
77 * set for embedded VGA.
eiichiro.oiwa.nm@hitachi.comb5e4efe2006-09-28 13:55:47 +090078 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 if (res->flags & IORESOURCE_ROM_SHADOW) {
80 /* primary video rom always starts here */
81 start = (loff_t)0xC0000;
82 *size = 0x20000; /* cover C000:0 through E000:0 */
83 } else {
84 if (res->flags & IORESOURCE_ROM_COPY) {
85 *size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -070086 return (void __iomem *)(unsigned long)
87 pci_resource_start(pdev, PCI_ROM_RESOURCE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 } else {
89 /* assign the ROM an address if it doesn't have one */
Benjamin Herrenschmidt8085ce082005-08-31 14:16:53 +100090 if (res->parent == NULL &&
91 pci_assign_resource(pdev,PCI_ROM_RESOURCE))
92 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 start = pci_resource_start(pdev, PCI_ROM_RESOURCE);
94 *size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
95 if (*size == 0)
96 return NULL;
97
98 /* Enable ROM space decodes */
Benjamin Herrenschmidt8085ce082005-08-31 14:16:53 +100099 if (pci_enable_rom(pdev))
100 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
102 }
103
104 rom = ioremap(start, *size);
105 if (!rom) {
106 /* restore enable if ioremap fails */
107 if (!(res->flags & (IORESOURCE_ROM_ENABLE |
108 IORESOURCE_ROM_SHADOW |
109 IORESOURCE_ROM_COPY)))
110 pci_disable_rom(pdev);
111 return NULL;
112 }
113
114 /*
115 * Try to find the true size of the ROM since sometimes the PCI window
116 * size is much larger than the actual size of the ROM.
117 * True size is important if the ROM is going to be copied.
118 */
119 image = rom;
120 do {
121 void __iomem *pds;
122 /* Standard PCI ROMs start out with these bytes 55 AA */
123 if (readb(image) != 0x55)
124 break;
125 if (readb(image + 1) != 0xAA)
126 break;
127 /* get the PCI data structure and check its signature */
128 pds = image + readw(image + 24);
129 if (readb(pds) != 'P')
130 break;
131 if (readb(pds + 1) != 'C')
132 break;
133 if (readb(pds + 2) != 'I')
134 break;
135 if (readb(pds + 3) != 'R')
136 break;
137 last_image = readb(pds + 21) & 0x80;
138 /* this length is reliable */
139 image += readw(pds + 16) * 512;
140 } while (!last_image);
141
Jon Smirl761a3ac2005-07-29 12:16:17 -0700142 /* never return a size larger than the PCI resource window */
143 /* there are known ROMs that get the size wrong */
144 *size = min((size_t)(image - rom), *size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 return rom;
147}
148
149/**
150 * pci_map_rom_copy - map a PCI ROM to kernel space, create a copy
Martin Waitz67be2dd2005-05-01 08:59:26 -0700151 * @pdev: pointer to pci device struct
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 * @size: pointer to receive size of pci window over ROM
153 * @return: kernel virtual pointer to image of ROM
154 *
155 * Map a PCI ROM into kernel space. If ROM is boot video ROM,
156 * the shadow BIOS copy will be returned instead of the
157 * actual ROM.
158 */
159void __iomem *pci_map_rom_copy(struct pci_dev *pdev, size_t *size)
160{
161 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
162 void __iomem *rom;
163
164 rom = pci_map_rom(pdev, size);
165 if (!rom)
166 return NULL;
167
168 if (res->flags & (IORESOURCE_ROM_COPY | IORESOURCE_ROM_SHADOW))
169 return rom;
170
171 res->start = (unsigned long)kmalloc(*size, GFP_KERNEL);
172 if (!res->start)
173 return rom;
174
175 res->end = res->start + *size;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700176 memcpy_fromio((void*)(unsigned long)res->start, rom, *size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 pci_unmap_rom(pdev, rom);
178 res->flags |= IORESOURCE_ROM_COPY;
179
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700180 return (void __iomem *)(unsigned long)res->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
183/**
184 * pci_unmap_rom - unmap the ROM from kernel space
Martin Waitz67be2dd2005-05-01 08:59:26 -0700185 * @pdev: pointer to pci device struct
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 * @rom: virtual address of the previous mapping
187 *
188 * Remove a mapping of a previously mapped ROM
189 */
190void pci_unmap_rom(struct pci_dev *pdev, void __iomem *rom)
191{
192 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
193
194 if (res->flags & IORESOURCE_ROM_COPY)
195 return;
196
197 iounmap(rom);
198
199 /* Disable again before continuing, leave enabled if pci=rom */
200 if (!(res->flags & (IORESOURCE_ROM_ENABLE | IORESOURCE_ROM_SHADOW)))
201 pci_disable_rom(pdev);
202}
203
204/**
205 * pci_remove_rom - disable the ROM and remove its sysfs attribute
Martin Waitz67be2dd2005-05-01 08:59:26 -0700206 * @pdev: pointer to pci device struct
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 *
208 * Remove the rom file in sysfs and disable ROM decoding.
209 */
210void pci_remove_rom(struct pci_dev *pdev)
211{
212 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
213
214 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
215 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
216 if (!(res->flags & (IORESOURCE_ROM_ENABLE |
217 IORESOURCE_ROM_SHADOW |
218 IORESOURCE_ROM_COPY)))
219 pci_disable_rom(pdev);
220}
221
222/**
223 * pci_cleanup_rom - internal routine for freeing the ROM copy created
224 * by pci_map_rom_copy called from remove.c
Martin Waitz67be2dd2005-05-01 08:59:26 -0700225 * @pdev: pointer to pci device struct
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 *
227 * Free the copied ROM if we allocated one.
228 */
229void pci_cleanup_rom(struct pci_dev *pdev)
230{
231 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
232 if (res->flags & IORESOURCE_ROM_COPY) {
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700233 kfree((void*)(unsigned long)res->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 res->flags &= ~IORESOURCE_ROM_COPY;
235 res->start = 0;
236 res->end = 0;
237 }
238}
239
240EXPORT_SYMBOL(pci_map_rom);
241EXPORT_SYMBOL(pci_map_rom_copy);
242EXPORT_SYMBOL(pci_unmap_rom);
243EXPORT_SYMBOL(pci_remove_rom);