11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * drivers/pci/rom.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * (C) Copyright 2004 Jon Smirl <jonsmirl@yahoo.com> 51da177e4SLinus Torvalds * (C) Copyright 2004 Silicon Graphics, Inc. Jesse Barnes <jbarnes@sgi.com> 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * PCI ROM access routines 81da177e4SLinus Torvalds */ 91da177e4SLinus Torvalds #include <linux/kernel.h> 10363c75dbSPaul Gortmaker #include <linux/export.h> 111da177e4SLinus Torvalds #include <linux/pci.h> 124e57b681STim Schmielau #include <linux/slab.h> 131da177e4SLinus Torvalds 141da177e4SLinus Torvalds #include "pci.h" 151da177e4SLinus Torvalds 161da177e4SLinus Torvalds /** 171da177e4SLinus Torvalds * pci_enable_rom - enable ROM decoding for a PCI device 1867be2dd1SMartin Waitz * @pdev: PCI device to enable 191da177e4SLinus Torvalds * 201da177e4SLinus Torvalds * Enable ROM decoding on @dev. This involves simply turning on the last 211da177e4SLinus Torvalds * bit of the PCI ROM BAR. Note that some cards may share address decoders 221da177e4SLinus Torvalds * between the ROM and other resources, so enabling it may disable access 231da177e4SLinus Torvalds * to MMIO registers or other card memory. 241da177e4SLinus Torvalds */ 25e416de5eSAlan Cox int pci_enable_rom(struct pci_dev *pdev) 261da177e4SLinus Torvalds { 278085ce08SBenjamin Herrenschmidt struct resource *res = pdev->resource + PCI_ROM_RESOURCE; 288085ce08SBenjamin Herrenschmidt struct pci_bus_region region; 291da177e4SLinus Torvalds u32 rom_addr; 301da177e4SLinus Torvalds 318085ce08SBenjamin Herrenschmidt if (!res->flags) 328085ce08SBenjamin Herrenschmidt return -1; 338085ce08SBenjamin Herrenschmidt 34fc279850SYinghai Lu pcibios_resource_to_bus(pdev->bus, ®ion, res); 351da177e4SLinus Torvalds pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr); 368085ce08SBenjamin Herrenschmidt rom_addr &= ~PCI_ROM_ADDRESS_MASK; 378085ce08SBenjamin Herrenschmidt rom_addr |= region.start | PCI_ROM_ADDRESS_ENABLE; 381da177e4SLinus Torvalds pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr); 398085ce08SBenjamin Herrenschmidt return 0; 401da177e4SLinus Torvalds } 41*b7fe9434SRyan Desfosses EXPORT_SYMBOL_GPL(pci_enable_rom); 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds /** 441da177e4SLinus Torvalds * pci_disable_rom - disable ROM decoding for a PCI device 4567be2dd1SMartin Waitz * @pdev: PCI device to disable 461da177e4SLinus Torvalds * 471da177e4SLinus Torvalds * Disable ROM decoding on a PCI device by turning off the last bit in the 481da177e4SLinus Torvalds * ROM BAR. 491da177e4SLinus Torvalds */ 50e416de5eSAlan Cox void pci_disable_rom(struct pci_dev *pdev) 511da177e4SLinus Torvalds { 521da177e4SLinus Torvalds u32 rom_addr; 531da177e4SLinus Torvalds pci_read_config_dword(pdev, pdev->rom_base_reg, &rom_addr); 541da177e4SLinus Torvalds rom_addr &= ~PCI_ROM_ADDRESS_ENABLE; 551da177e4SLinus Torvalds pci_write_config_dword(pdev, pdev->rom_base_reg, rom_addr); 561da177e4SLinus Torvalds } 57*b7fe9434SRyan Desfosses EXPORT_SYMBOL_GPL(pci_disable_rom); 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds /** 60d7ad2254SJohn Keller * pci_get_rom_size - obtain the actual size of the ROM image 614cc59c72SRandy Dunlap * @pdev: target PCI device 62d7ad2254SJohn Keller * @rom: kernel virtual pointer to image of ROM 63d7ad2254SJohn Keller * @size: size of PCI window 64d7ad2254SJohn Keller * return: size of actual ROM image 65d7ad2254SJohn Keller * 66d7ad2254SJohn Keller * Determine the actual length of the ROM image. 67d7ad2254SJohn Keller * The PCI window size could be much larger than the 68d7ad2254SJohn Keller * actual image size. 69d7ad2254SJohn Keller */ 7097c44836STimothy S. Nelson size_t pci_get_rom_size(struct pci_dev *pdev, void __iomem *rom, size_t size) 71d7ad2254SJohn Keller { 72d7ad2254SJohn Keller void __iomem *image; 73d7ad2254SJohn Keller int last_image; 74d7ad2254SJohn Keller 75d7ad2254SJohn Keller image = rom; 76d7ad2254SJohn Keller do { 77d7ad2254SJohn Keller void __iomem *pds; 78d7ad2254SJohn Keller /* Standard PCI ROMs start out with these bytes 55 AA */ 7997c44836STimothy S. Nelson if (readb(image) != 0x55) { 8097c44836STimothy S. Nelson dev_err(&pdev->dev, "Invalid ROM contents\n"); 81d7ad2254SJohn Keller break; 8297c44836STimothy S. Nelson } 83d7ad2254SJohn Keller if (readb(image + 1) != 0xAA) 84d7ad2254SJohn Keller break; 85d7ad2254SJohn Keller /* get the PCI data structure and check its signature */ 86d7ad2254SJohn Keller pds = image + readw(image + 24); 87d7ad2254SJohn Keller if (readb(pds) != 'P') 88d7ad2254SJohn Keller break; 89d7ad2254SJohn Keller if (readb(pds + 1) != 'C') 90d7ad2254SJohn Keller break; 91d7ad2254SJohn Keller if (readb(pds + 2) != 'I') 92d7ad2254SJohn Keller break; 93d7ad2254SJohn Keller if (readb(pds + 3) != 'R') 94d7ad2254SJohn Keller break; 95d7ad2254SJohn Keller last_image = readb(pds + 21) & 0x80; 96d7ad2254SJohn Keller /* this length is reliable */ 97d7ad2254SJohn Keller image += readw(pds + 16) * 512; 98d7ad2254SJohn Keller } while (!last_image); 99d7ad2254SJohn Keller 100d7ad2254SJohn Keller /* never return a size larger than the PCI resource window */ 101d7ad2254SJohn Keller /* there are known ROMs that get the size wrong */ 102d7ad2254SJohn Keller return min((size_t)(image - rom), size); 103d7ad2254SJohn Keller } 104d7ad2254SJohn Keller 105d7ad2254SJohn Keller /** 1061da177e4SLinus Torvalds * pci_map_rom - map a PCI ROM to kernel space 10767be2dd1SMartin Waitz * @pdev: pointer to pci device struct 1081da177e4SLinus Torvalds * @size: pointer to receive size of pci window over ROM 109f5dafca5SRandy Dunlap * 110f5dafca5SRandy Dunlap * Return: kernel virtual pointer to image of ROM 1111da177e4SLinus Torvalds * 1121da177e4SLinus Torvalds * Map a PCI ROM into kernel space. If ROM is boot video ROM, 1131da177e4SLinus Torvalds * the shadow BIOS copy will be returned instead of the 1141da177e4SLinus Torvalds * actual ROM. 1151da177e4SLinus Torvalds */ 1161da177e4SLinus Torvalds void __iomem *pci_map_rom(struct pci_dev *pdev, size_t *size) 1171da177e4SLinus Torvalds { 1181da177e4SLinus Torvalds struct resource *res = &pdev->resource[PCI_ROM_RESOURCE]; 119fffe01f7SMatthew Garrett loff_t start; 1201da177e4SLinus Torvalds void __iomem *rom; 1211da177e4SLinus Torvalds 122b5e4efe7Seiichiro.oiwa.nm@hitachi.com /* 1236b5c76b8SEiichiro Oiwa * IORESOURCE_ROM_SHADOW set on x86, x86_64 and IA64 supports legacy 1246b5c76b8SEiichiro Oiwa * memory map if the VGA enable bit of the Bridge Control register is 1256b5c76b8SEiichiro Oiwa * set for embedded VGA. 126b5e4efe7Seiichiro.oiwa.nm@hitachi.com */ 127547b5246SMatthew Garrett if (res->flags & IORESOURCE_ROM_SHADOW) { 1281da177e4SLinus Torvalds /* primary video rom always starts here */ 1291da177e4SLinus Torvalds start = (loff_t)0xC0000; 1301da177e4SLinus Torvalds *size = 0x20000; /* cover C000:0 through E000:0 */ 1311da177e4SLinus Torvalds } else { 132a2302c68SJohn Keller if (res->flags & 133a2302c68SJohn Keller (IORESOURCE_ROM_COPY | IORESOURCE_ROM_BIOS_COPY)) { 1341da177e4SLinus Torvalds *size = pci_resource_len(pdev, PCI_ROM_RESOURCE); 135e31dd6e4SGreg Kroah-Hartman return (void __iomem *)(unsigned long) 136e31dd6e4SGreg Kroah-Hartman pci_resource_start(pdev, PCI_ROM_RESOURCE); 1371da177e4SLinus Torvalds } else { 138fffe01f7SMatthew Garrett /* assign the ROM an address if it doesn't have one */ 139fffe01f7SMatthew Garrett if (res->parent == NULL && 140fffe01f7SMatthew Garrett pci_assign_resource(pdev,PCI_ROM_RESOURCE)) 1418085ce08SBenjamin Herrenschmidt return NULL; 142fffe01f7SMatthew Garrett start = pci_resource_start(pdev, PCI_ROM_RESOURCE); 143fffe01f7SMatthew Garrett *size = pci_resource_len(pdev, PCI_ROM_RESOURCE); 144fffe01f7SMatthew Garrett if (*size == 0) 145fffe01f7SMatthew Garrett return NULL; 146fffe01f7SMatthew Garrett 147fffe01f7SMatthew Garrett /* Enable ROM space decodes */ 148fffe01f7SMatthew Garrett if (pci_enable_rom(pdev)) 149fffe01f7SMatthew Garrett return NULL; 150fffe01f7SMatthew Garrett } 151fffe01f7SMatthew Garrett } 1521da177e4SLinus Torvalds 1531da177e4SLinus Torvalds rom = ioremap(start, *size); 1541da177e4SLinus Torvalds if (!rom) { 1551da177e4SLinus Torvalds /* restore enable if ioremap fails */ 1561da177e4SLinus Torvalds if (!(res->flags & (IORESOURCE_ROM_ENABLE | 1571da177e4SLinus Torvalds IORESOURCE_ROM_SHADOW | 1581da177e4SLinus Torvalds IORESOURCE_ROM_COPY))) 1591da177e4SLinus Torvalds pci_disable_rom(pdev); 1601da177e4SLinus Torvalds return NULL; 1611da177e4SLinus Torvalds } 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds /* 1641da177e4SLinus Torvalds * Try to find the true size of the ROM since sometimes the PCI window 1651da177e4SLinus Torvalds * size is much larger than the actual size of the ROM. 1661da177e4SLinus Torvalds * True size is important if the ROM is going to be copied. 1671da177e4SLinus Torvalds */ 16897c44836STimothy S. Nelson *size = pci_get_rom_size(pdev, rom, *size); 1691da177e4SLinus Torvalds return rom; 1701da177e4SLinus Torvalds } 171*b7fe9434SRyan Desfosses EXPORT_SYMBOL(pci_map_rom); 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds /** 1741da177e4SLinus Torvalds * pci_unmap_rom - unmap the ROM from kernel space 17567be2dd1SMartin Waitz * @pdev: pointer to pci device struct 1761da177e4SLinus Torvalds * @rom: virtual address of the previous mapping 1771da177e4SLinus Torvalds * 1781da177e4SLinus Torvalds * Remove a mapping of a previously mapped ROM 1791da177e4SLinus Torvalds */ 1801da177e4SLinus Torvalds void pci_unmap_rom(struct pci_dev *pdev, void __iomem *rom) 1811da177e4SLinus Torvalds { 1821da177e4SLinus Torvalds struct resource *res = &pdev->resource[PCI_ROM_RESOURCE]; 1831da177e4SLinus Torvalds 184a2302c68SJohn Keller if (res->flags & (IORESOURCE_ROM_COPY | IORESOURCE_ROM_BIOS_COPY)) 1851da177e4SLinus Torvalds return; 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds iounmap(rom); 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds /* Disable again before continuing, leave enabled if pci=rom */ 1901da177e4SLinus Torvalds if (!(res->flags & (IORESOURCE_ROM_ENABLE | IORESOURCE_ROM_SHADOW))) 1911da177e4SLinus Torvalds pci_disable_rom(pdev); 1921da177e4SLinus Torvalds } 193*b7fe9434SRyan Desfosses EXPORT_SYMBOL(pci_unmap_rom); 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds /** 1960643245fSRandy Dunlap * pci_cleanup_rom - free the ROM copy created by pci_map_rom_copy 19767be2dd1SMartin Waitz * @pdev: pointer to pci device struct 1981da177e4SLinus Torvalds * 1991da177e4SLinus Torvalds * Free the copied ROM if we allocated one. 2001da177e4SLinus Torvalds */ 2011da177e4SLinus Torvalds void pci_cleanup_rom(struct pci_dev *pdev) 2021da177e4SLinus Torvalds { 2031da177e4SLinus Torvalds struct resource *res = &pdev->resource[PCI_ROM_RESOURCE]; 204bd064f0aSBjorn Helgaas 2051da177e4SLinus Torvalds if (res->flags & IORESOURCE_ROM_COPY) { 206e31dd6e4SGreg Kroah-Hartman kfree((void*)(unsigned long)res->start); 207bd064f0aSBjorn Helgaas res->flags |= IORESOURCE_UNSET; 2081da177e4SLinus Torvalds res->flags &= ~IORESOURCE_ROM_COPY; 2091da177e4SLinus Torvalds res->start = 0; 2101da177e4SLinus Torvalds res->end = 0; 2111da177e4SLinus Torvalds } 2121da177e4SLinus Torvalds } 2131da177e4SLinus Torvalds 214fffe01f7SMatthew Garrett /** 215fffe01f7SMatthew Garrett * pci_platform_rom - provides a pointer to any ROM image provided by the 216fffe01f7SMatthew Garrett * platform 217fffe01f7SMatthew Garrett * @pdev: pointer to pci device struct 218fffe01f7SMatthew Garrett * @size: pointer to receive size of pci window over ROM 219fffe01f7SMatthew Garrett */ 220fffe01f7SMatthew Garrett void __iomem *pci_platform_rom(struct pci_dev *pdev, size_t *size) 221fffe01f7SMatthew Garrett { 222fffe01f7SMatthew Garrett if (pdev->rom && pdev->romlen) { 223fffe01f7SMatthew Garrett *size = pdev->romlen; 224fffe01f7SMatthew Garrett return phys_to_virt((phys_addr_t)pdev->rom); 225fffe01f7SMatthew Garrett } 226fffe01f7SMatthew Garrett 227fffe01f7SMatthew Garrett return NULL; 228fffe01f7SMatthew Garrett } 229fffe01f7SMatthew Garrett EXPORT_SYMBOL(pci_platform_rom); 230