Blame view

drivers/pci/rom.c 5.51 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
  /*
   * drivers/pci/rom.c
   *
   * (C) Copyright 2004 Jon Smirl <jonsmirl@yahoo.com>
   * (C) Copyright 2004 Silicon Graphics, Inc. Jesse Barnes <jbarnes@sgi.com>
   *
   * PCI ROM access routines
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
9
  #include <linux/kernel.h>
363c75db1   Paul Gortmaker   pci: Fix files ne...
10
  #include <linux/export.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
11
  #include <linux/pci.h>
4e57b6817   Tim Schmielau   [PATCH] fix missi...
12
  #include <linux/slab.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
13
14
15
16
17
  
  #include "pci.h"
  
  /**
   * pci_enable_rom - enable ROM decoding for a PCI device
67be2dd1b   Martin Waitz   [PATCH] DocBook: ...
18
   * @pdev: PCI device to enable
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
19
20
21
22
23
24
   *
   * Enable ROM decoding on @dev.  This involves simply turning on the last
   * bit of the PCI ROM BAR.  Note that some cards may share address decoders
   * between the ROM and other resources, so enabling it may disable access
   * to MMIO registers or other card memory.
   */
e416de5e6   Alan Cox   Export the ROM en...
25
  int pci_enable_rom(struct pci_dev *pdev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
26
  {
4708f9a55   Bjorn Helgaas   PCI: Don't enable...
27
  	struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
8085ce084   Benjamin Herrenschmidt   [PATCH] Fix PCI R...
28
  	struct pci_bus_region region;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
29
  	u32 rom_addr;
8085ce084   Benjamin Herrenschmidt   [PATCH] Fix PCI R...
30
31
  	if (!res->flags)
  		return -1;
4708f9a55   Bjorn Helgaas   PCI: Don't enable...
32
33
34
  	/* Nothing to enable if we're using a shadow copy in RAM */
  	if (res->flags & IORESOURCE_ROM_SHADOW)
  		return 0;
0b457dde3   Bjorn Helgaas   PCI: Add comments...
35
36
37
38
39
  	/*
  	 * Ideally pci_update_resource() would update the ROM BAR address,
  	 * and we would only set the enable bit here.  But apparently some
  	 * devices have buggy ROM BARs that read as zero when disabled.
  	 */
fc2798502   Yinghai Lu   PCI: Convert pcib...
40
  	pcibios_resource_to_bus(pdev->bus, &region, res);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
41
  	pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
8085ce084   Benjamin Herrenschmidt   [PATCH] Fix PCI R...
42
43
  	rom_addr &= ~PCI_ROM_ADDRESS_MASK;
  	rom_addr |= region.start | PCI_ROM_ADDRESS_ENABLE;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
44
  	pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
8085ce084   Benjamin Herrenschmidt   [PATCH] Fix PCI R...
45
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
46
  }
b7fe94342   Ryan Desfosses   PCI: Move EXPORT_...
47
  EXPORT_SYMBOL_GPL(pci_enable_rom);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
48
49
50
  
  /**
   * pci_disable_rom - disable ROM decoding for a PCI device
67be2dd1b   Martin Waitz   [PATCH] DocBook: ...
51
   * @pdev: PCI device to disable
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
52
53
54
55
   *
   * Disable ROM decoding on a PCI device by turning off the last bit in the
   * ROM BAR.
   */
e416de5e6   Alan Cox   Export the ROM en...
56
  void pci_disable_rom(struct pci_dev *pdev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
57
  {
4708f9a55   Bjorn Helgaas   PCI: Don't enable...
58
  	struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
59
  	u32 rom_addr;
4708f9a55   Bjorn Helgaas   PCI: Don't enable...
60
61
62
  
  	if (res->flags & IORESOURCE_ROM_SHADOW)
  		return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
63
64
65
66
  	pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr);
  	rom_addr &= ~PCI_ROM_ADDRESS_ENABLE;
  	pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr);
  }
b7fe94342   Ryan Desfosses   PCI: Move EXPORT_...
67
  EXPORT_SYMBOL_GPL(pci_disable_rom);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
68
69
  
  /**
d7ad2254f   John Keller   [IA64] SN: Correc...
70
   * pci_get_rom_size - obtain the actual size of the ROM image
4cc59c721   Randy Dunlap   PCI: fix rom.c ke...
71
   * @pdev: target PCI device
d7ad2254f   John Keller   [IA64] SN: Correc...
72
73
74
75
76
77
78
79
   * @rom: kernel virtual pointer to image of ROM
   * @size: size of PCI window
   *  return: size of actual ROM image
   *
   * Determine the actual length of the ROM image.
   * The PCI window size could be much larger than the
   * actual image size.
   */
97c44836c   Timothy S. Nelson   PCI: return error...
80
  size_t pci_get_rom_size(struct pci_dev *pdev, void __iomem *rom, size_t size)
d7ad2254f   John Keller   [IA64] SN: Correc...
81
82
83
  {
  	void __iomem *image;
  	int last_image;
16b036af3   Michel Dänzer   PCI: Fix infinite...
84
  	unsigned length;
d7ad2254f   John Keller   [IA64] SN: Correc...
85
86
87
88
89
  
  	image = rom;
  	do {
  		void __iomem *pds;
  		/* Standard PCI ROMs start out with these bytes 55 AA */
4066df634   Vladis Dronov   PCI: Print warnin...
90
91
92
93
  		if (readw(image) != 0xAA55) {
  			dev_err(&pdev->dev, "Invalid PCI ROM header signature: expecting 0xaa55, got %#06x
  ",
  				readw(image));
d7ad2254f   John Keller   [IA64] SN: Correc...
94
  			break;
97c44836c   Timothy S. Nelson   PCI: return error...
95
  		}
4066df634   Vladis Dronov   PCI: Print warnin...
96
  		/* get the PCI data structure and check its "PCIR" signature */
d7ad2254f   John Keller   [IA64] SN: Correc...
97
  		pds = image + readw(image + 24);
4066df634   Vladis Dronov   PCI: Print warnin...
98
99
100
101
  		if (readl(pds) != 0x52494350) {
  			dev_err(&pdev->dev, "Invalid PCI ROM data signature: expecting 0x52494350, got %#010x
  ",
  				readl(pds));
d7ad2254f   John Keller   [IA64] SN: Correc...
102
  			break;
4066df634   Vladis Dronov   PCI: Print warnin...
103
  		}
d7ad2254f   John Keller   [IA64] SN: Correc...
104
  		last_image = readb(pds + 21) & 0x80;
16b036af3   Michel Dänzer   PCI: Fix infinite...
105
106
  		length = readw(pds + 16);
  		image += length * 512;
47b975d23   Edward O'Callaghan   PCI: Avoid iterat...
107
108
109
  		/* Avoid iterating through memory outside the resource window */
  		if (image > rom + size)
  			break;
16b036af3   Michel Dänzer   PCI: Fix infinite...
110
  	} while (length && !last_image);
d7ad2254f   John Keller   [IA64] SN: Correc...
111
112
113
114
115
116
117
  
  	/* never return a size larger than the PCI resource window */
  	/* there are known ROMs that get the size wrong */
  	return min((size_t)(image - rom), size);
  }
  
  /**
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
118
   * pci_map_rom - map a PCI ROM to kernel space
67be2dd1b   Martin Waitz   [PATCH] DocBook: ...
119
   * @pdev: pointer to pci device struct
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
120
   * @size: pointer to receive size of pci window over ROM
f5dafca52   Randy Dunlap   PCI: remove exces...
121
122
   *
   * Return: kernel virtual pointer to image of ROM
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
123
124
125
126
127
128
129
130
   *
   * Map a PCI ROM into kernel space. If ROM is boot video ROM,
   * the shadow BIOS copy will be returned instead of the
   * actual ROM.
   */
  void __iomem *pci_map_rom(struct pci_dev *pdev, size_t *size)
  {
  	struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
fffe01f7a   Matthew Garrett   PCI: Add PCI ROM ...
131
  	loff_t start;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
132
  	void __iomem *rom;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
133

f50dd8c3d   Bjorn Helgaas   PCI: Clean up pci...
134
135
136
137
138
139
140
141
142
143
144
145
  	/* assign the ROM an address if it doesn't have one */
  	if (res->parent == NULL && pci_assign_resource(pdev, PCI_ROM_RESOURCE))
  		return NULL;
  
  	start = pci_resource_start(pdev, PCI_ROM_RESOURCE);
  	*size = pci_resource_len(pdev, PCI_ROM_RESOURCE);
  	if (*size == 0)
  		return NULL;
  
  	/* Enable ROM space decodes */
  	if (pci_enable_rom(pdev))
  		return NULL;
547b52463   Matthew Garrett   PCI: Use ROM imag...
146

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
147
148
149
  	rom = ioremap(start, *size);
  	if (!rom) {
  		/* restore enable if ioremap fails */
d9c8bea17   Bjorn Helgaas   PCI: Remove unuse...
150
  		if (!(res->flags & IORESOURCE_ROM_ENABLE))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
151
152
153
154
155
156
157
158
159
  			pci_disable_rom(pdev);
  		return NULL;
  	}
  
  	/*
  	 * Try to find the true size of the ROM since sometimes the PCI window
  	 * size is much larger than the actual size of the ROM.
  	 * True size is important if the ROM is going to be copied.
  	 */
97c44836c   Timothy S. Nelson   PCI: return error...
160
  	*size = pci_get_rom_size(pdev, rom, *size);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
161
162
  	return rom;
  }
b7fe94342   Ryan Desfosses   PCI: Move EXPORT_...
163
  EXPORT_SYMBOL(pci_map_rom);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
164

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
165
166
  /**
   * pci_unmap_rom - unmap the ROM from kernel space
67be2dd1b   Martin Waitz   [PATCH] DocBook: ...
167
   * @pdev: pointer to pci device struct
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
168
169
170
171
172
173
174
   * @rom: virtual address of the previous mapping
   *
   * Remove a mapping of a previously mapped ROM
   */
  void pci_unmap_rom(struct pci_dev *pdev, void __iomem *rom)
  {
  	struct resource *res = &pdev->resource[PCI_ROM_RESOURCE];
fffe01f7a   Matthew Garrett   PCI: Add PCI ROM ...
175
  	iounmap(rom);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
176

4708f9a55   Bjorn Helgaas   PCI: Don't enable...
177
178
  	/* Disable again before continuing */
  	if (!(res->flags & IORESOURCE_ROM_ENABLE))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
179
180
  		pci_disable_rom(pdev);
  }
b7fe94342   Ryan Desfosses   PCI: Move EXPORT_...
181
  EXPORT_SYMBOL(pci_unmap_rom);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
182

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
183
  /**
fffe01f7a   Matthew Garrett   PCI: Add PCI ROM ...
184
185
186
187
188
189
190
191
192
193
194
195
196
197
   * pci_platform_rom - provides a pointer to any ROM image provided by the
   * platform
   * @pdev: pointer to pci device struct
   * @size: pointer to receive size of pci window over ROM
   */
  void __iomem *pci_platform_rom(struct pci_dev *pdev, size_t *size)
  {
  	if (pdev->rom && pdev->romlen) {
  		*size = pdev->romlen;
  		return phys_to_virt((phys_addr_t)pdev->rom);
  	}
  
  	return NULL;
  }
fffe01f7a   Matthew Garrett   PCI: Add PCI ROM ...
198
  EXPORT_SYMBOL(pci_platform_rom);