xref: /openbmc/linux/drivers/pci/rom.c (revision 0883c2c0)
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 	/* Nothing to enable if we're using a shadow copy in RAM */
35 	if (res->flags & IORESOURCE_ROM_SHADOW)
36 		return 0;
37 
38 	pcibios_resource_to_bus(pdev->bus, &region, 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 }
45 EXPORT_SYMBOL_GPL(pci_enable_rom);
46 
47 /**
48  * pci_disable_rom - disable ROM decoding for a PCI device
49  * @pdev: PCI device to disable
50  *
51  * Disable ROM decoding on a PCI device by turning off the last bit in the
52  * ROM BAR.
53  */
54 void 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 }
66 EXPORT_SYMBOL_GPL(pci_disable_rom);
67 
68 /**
69  * pci_get_rom_size - obtain the actual size of the ROM image
70  * @pdev: target PCI device
71  * @rom: kernel virtual pointer to image of ROM
72  * @size: size of PCI window
73  *  return: size of actual ROM image
74  *
75  * Determine the actual length of the ROM image.
76  * The PCI window size could be much larger than the
77  * actual image size.
78  */
79 size_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 		/* Standard PCI ROMs start out with these bytes 55 AA */
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 		/* get the PCI data structure and check its "PCIR" signature */
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 		/* Avoid iterating through memory outside the resource window */
105 		if (image > rom + size)
106 			break;
107 	} while (length && !last_image);
108 
109 	/* never return a size larger than the PCI resource window */
110 	/* there are known ROMs that get the size wrong */
111 	return min((size_t)(image - rom), size);
112 }
113 
114 /**
115  * pci_map_rom - map a PCI ROM to kernel space
116  * @pdev: pointer to pci device struct
117  * @size: pointer to receive size of pci window over ROM
118  *
119  * Return: kernel virtual pointer to image of ROM
120  *
121  * Map a PCI ROM into kernel space. If ROM is boot video ROM,
122  * the shadow BIOS copy will be returned instead of the
123  * actual ROM.
124  */
125 void __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 	/* assign the ROM an address if it doesn't have one */
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 	/* Enable ROM space decodes */
141 	if (pci_enable_rom(pdev))
142 		return NULL;
143 
144 	rom = ioremap(start, *size);
145 	if (!rom) {
146 		/* restore enable if ioremap fails */
147 		if (!(res->flags & IORESOURCE_ROM_ENABLE))
148 			pci_disable_rom(pdev);
149 		return NULL;
150 	}
151 
152 	/*
153 	 * Try to find the true size of the ROM since sometimes the PCI window
154 	 * size is much larger than the actual size of the ROM.
155 	 * True size is important if the ROM is going to be copied.
156 	 */
157 	*size = pci_get_rom_size(pdev, rom, *size);
158 	return rom;
159 }
160 EXPORT_SYMBOL(pci_map_rom);
161 
162 /**
163  * pci_unmap_rom - unmap the ROM from kernel space
164  * @pdev: pointer to pci device struct
165  * @rom: virtual address of the previous mapping
166  *
167  * Remove a mapping of a previously mapped ROM
168  */
169 void 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 	/* Disable again before continuing */
176 	if (!(res->flags & IORESOURCE_ROM_ENABLE))
177 		pci_disable_rom(pdev);
178 }
179 EXPORT_SYMBOL(pci_unmap_rom);
180 
181 /**
182  * pci_platform_rom - provides a pointer to any ROM image provided by the
183  * platform
184  * @pdev: pointer to pci device struct
185  * @size: pointer to receive size of pci window over ROM
186  */
187 void __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 }
196 EXPORT_SYMBOL(pci_platform_rom);
197