blob: 43e4a49f2cc428275cc77a74bb52c61fe9d428b3 [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 Herrenschmidt8085ce02005-08-31 14:16:53 +100024static int pci_enable_rom(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
Benjamin Herrenschmidt8085ce02005-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 Herrenschmidt8085ce02005-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 Herrenschmidt8085ce02005-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 Herrenschmidt8085ce02005-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 /*
75 * IORESOURCE_ROM_SHADOW set if the VGA enable bit of the Bridge Control
76 * register is set for embedded VGA.
77 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 if (res->flags & IORESOURCE_ROM_SHADOW) {
79 /* primary video rom always starts here */
80 start = (loff_t)0xC0000;
81 *size = 0x20000; /* cover C000:0 through E000:0 */
82 } else {
83 if (res->flags & IORESOURCE_ROM_COPY) {
84 *size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -070085 return (void __iomem *)(unsigned long)
86 pci_resource_start(pdev, PCI_ROM_RESOURCE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 } else {
88 /* assign the ROM an address if it doesn't have one */
Benjamin Herrenschmidt8085ce02005-08-31 14:16:53 +100089 if (res->parent == NULL &&
90 pci_assign_resource(pdev,PCI_ROM_RESOURCE))
91 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 start = pci_resource_start(pdev, PCI_ROM_RESOURCE);
93 *size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
94 if (*size == 0)
95 return NULL;
96
97 /* Enable ROM space decodes */
Benjamin Herrenschmidt8085ce02005-08-31 14:16:53 +100098 if (pci_enable_rom(pdev))
99 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
101 }
102
103 rom = ioremap(start, *size);
104 if (!rom) {
105 /* restore enable if ioremap fails */
106 if (!(res->flags & (IORESOURCE_ROM_ENABLE |
107 IORESOURCE_ROM_SHADOW |
108 IORESOURCE_ROM_COPY)))
109 pci_disable_rom(pdev);
110 return NULL;
111 }
112
113 /*
114 * Try to find the true size of the ROM since sometimes the PCI window
115 * size is much larger than the actual size of the ROM.
116 * True size is important if the ROM is going to be copied.
117 */
118 image = rom;
119 do {
120 void __iomem *pds;
121 /* Standard PCI ROMs start out with these bytes 55 AA */
122 if (readb(image) != 0x55)
123 break;
124 if (readb(image + 1) != 0xAA)
125 break;
126 /* get the PCI data structure and check its signature */
127 pds = image + readw(image + 24);
128 if (readb(pds) != 'P')
129 break;
130 if (readb(pds + 1) != 'C')
131 break;
132 if (readb(pds + 2) != 'I')
133 break;
134 if (readb(pds + 3) != 'R')
135 break;
136 last_image = readb(pds + 21) & 0x80;
137 /* this length is reliable */
138 image += readw(pds + 16) * 512;
139 } while (!last_image);
140
Jon Smirl761a3ac2005-07-29 12:16:17 -0700141 /* never return a size larger than the PCI resource window */
142 /* there are known ROMs that get the size wrong */
143 *size = min((size_t)(image - rom), *size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 return rom;
146}
147
148/**
149 * pci_map_rom_copy - map a PCI ROM to kernel space, create a copy
Martin Waitz67be2dd2005-05-01 08:59:26 -0700150 * @pdev: pointer to pci device struct
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 * @size: pointer to receive size of pci window over ROM
152 * @return: kernel virtual pointer to image of ROM
153 *
154 * Map a PCI ROM into kernel space. If ROM is boot video ROM,
155 * the shadow BIOS copy will be returned instead of the
156 * actual ROM.
157 */
158void __iomem *pci_map_rom_copy(struct pci_dev *pdev, size_t *size)
159{
160 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
161 void __iomem *rom;
162
163 rom = pci_map_rom(pdev, size);
164 if (!rom)
165 return NULL;
166
167 if (res->flags & (IORESOURCE_ROM_COPY | IORESOURCE_ROM_SHADOW))
168 return rom;
169
170 res->start = (unsigned long)kmalloc(*size, GFP_KERNEL);
171 if (!res->start)
172 return rom;
173
174 res->end = res->start + *size;
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700175 memcpy_fromio((void*)(unsigned long)res->start, rom, *size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 pci_unmap_rom(pdev, rom);
177 res->flags |= IORESOURCE_ROM_COPY;
178
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700179 return (void __iomem *)(unsigned long)res->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
182/**
183 * pci_unmap_rom - unmap the ROM from kernel space
Martin Waitz67be2dd2005-05-01 08:59:26 -0700184 * @pdev: pointer to pci device struct
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 * @rom: virtual address of the previous mapping
186 *
187 * Remove a mapping of a previously mapped ROM
188 */
189void pci_unmap_rom(struct pci_dev *pdev, void __iomem *rom)
190{
191 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
192
193 if (res->flags & IORESOURCE_ROM_COPY)
194 return;
195
196 iounmap(rom);
197
198 /* Disable again before continuing, leave enabled if pci=rom */
199 if (!(res->flags & (IORESOURCE_ROM_ENABLE | IORESOURCE_ROM_SHADOW)))
200 pci_disable_rom(pdev);
201}
202
203/**
204 * pci_remove_rom - disable the ROM and remove its sysfs attribute
Martin Waitz67be2dd2005-05-01 08:59:26 -0700205 * @pdev: pointer to pci device struct
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 *
207 * Remove the rom file in sysfs and disable ROM decoding.
208 */
209void pci_remove_rom(struct pci_dev *pdev)
210{
211 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
212
213 if (pci_resource_len(pdev, PCI_ROM_RESOURCE))
214 sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr);
215 if (!(res->flags & (IORESOURCE_ROM_ENABLE |
216 IORESOURCE_ROM_SHADOW |
217 IORESOURCE_ROM_COPY)))
218 pci_disable_rom(pdev);
219}
220
221/**
222 * pci_cleanup_rom - internal routine for freeing the ROM copy created
223 * by pci_map_rom_copy called from remove.c
Martin Waitz67be2dd2005-05-01 08:59:26 -0700224 * @pdev: pointer to pci device struct
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 *
226 * Free the copied ROM if we allocated one.
227 */
228void pci_cleanup_rom(struct pci_dev *pdev)
229{
230 struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
231 if (res->flags & IORESOURCE_ROM_COPY) {
Greg Kroah-Hartmane31dd6e2006-06-12 17:06:02 -0700232 kfree((void*)(unsigned long)res->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 res->flags &= ~IORESOURCE_ROM_COPY;
234 res->start = 0;
235 res->end = 0;
236 }
237}
238
239EXPORT_SYMBOL(pci_map_rom);
240EXPORT_SYMBOL(pci_map_rom_copy);
241EXPORT_SYMBOL(pci_unmap_rom);
242EXPORT_SYMBOL(pci_remove_rom);