flashrom: Abstract mmap() in physmap.c and only open /dev/mem on the first physmap...
authorStefan Reinauer <stepan@coresystems.de>
Mon, 26 Jan 2009 01:10:48 +0000 (01:10 +0000)
committerPeter Stuge <peter@stuge.se>
Mon, 26 Jan 2009 01:10:48 +0000 (01:10 +0000)
Signed-off-by: Stefan Reinauer <stepan@coresystems.de>
Signed-off-by: Peter Stuge <peter@stuge.se>
Acked-by: Peter Stuge <peter@stuge.se>
git-svn-id: svn://svn.coreboot.org/coreboot/trunk@3903 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1

util/flashrom/Makefile
util/flashrom/cbtable.c
util/flashrom/chipset_enable.c
util/flashrom/flash.h
util/flashrom/flashrom.c
util/flashrom/physmap.c [new file with mode: 0644]

index 940f346ef8bfc11b88b3e78c566f1c570a2e2576..d46600b6532ec7dffa0d7601227683edd27d4cb5 100644 (file)
@@ -27,7 +27,7 @@ endif
 OBJS = chipset_enable.o board_enable.o udelay.o jedec.o stm50flw0x0x.o \
        sst28sf040.o am29f040b.o mx29f002.o sst39sf020.o m29f400bt.o \
        w49f002u.o 82802ab.o pm49fl00x.o sst49lf040.o en29f002a.o \
-       sst49lfxxxc.o sst_fwhub.o layout.o cbtable.o flashchips.o \
+       sst49lfxxxc.o sst_fwhub.o layout.o cbtable.o flashchips.o physmap.o \
        flashrom.o w39v080fa.o sharplhf00l04.o w29ee011.o spi.o it87spi.o \
        ichspi.o w39v040c.o sb600spi.o
 
index 25c3ee2d3e61853b354c7b42dd3a5ee579716757..284140a5631b99a6b0f9af08c49e3f1aced950a8 100644 (file)
@@ -188,13 +188,7 @@ int coreboot_init(void)
        struct lb_header *lb_table;
        struct lb_record *rec, *last;
 
-       low_1MB = mmap(0, 1024 * 1024, PROT_READ, MAP_SHARED, fd_mem,
-                      0x00000000);
-       if (low_1MB == MAP_FAILED) {
-               perror("Can't mmap memory using " MEM_DEV);
-               mmap_errmsg();
-               exit(-2);
-       }
+       low_1MB = physmap("low megabyte", 0x0, 1024*1024);
        lb_table = find_lb_table(low_1MB, 0x00000, 0x1000);
        if (!lb_table)
                lb_table = find_lb_table(low_1MB, 0xf0000, 1024*1024);
index d7eb7fa13df7988ae88d636eaa5e59d332173828..7298729c282c54fa19b32e48ffb487ebfdad300a 100644 (file)
@@ -215,14 +215,7 @@ static int enable_flash_vt8237s_spi(struct pci_dev *dev, const char *name)
 
        mmio_base = (pci_read_long(dev, 0xbc)) << 8;
        printf_debug("MMIO base at = 0x%x\n", mmio_base);
-       spibar = mmap(NULL, 0x70, PROT_READ | PROT_WRITE, MAP_SHARED,
-                     fd_mem, mmio_base);
-
-       if (spibar == MAP_FAILED) {
-               perror("Can't mmap memory using " MEM_DEV);
-               mmap_errmsg();
-               exit(1);
-       }
+       spibar = physmap("VT8237S MMIO registers", mmio_base, 0x70);
 
        printf_debug("0x6c: 0x%04x     (CLOCK/DEBUG)\n",
                     *(uint16_t *) (spibar + 0x6c));
@@ -252,13 +245,7 @@ static int enable_flash_ich_dc_spi(struct pci_dev *dev, const char *name,
        printf_debug("\nRoot Complex Register Block address = 0x%x\n", tmp);
 
        /* Map RCBA to virtual memory */
-       rcrb = mmap(0, 0x4000, PROT_READ | PROT_WRITE, MAP_SHARED, fd_mem,
-                   (off_t) tmp);
-       if (rcrb == MAP_FAILED) {
-               perror("Can't mmap memory using " MEM_DEV);
-               mmap_errmsg();
-               exit(1);
-       }
+       rcrb = physmap("ICH RCRB", tmp, 0x4000);
 
        gcs = *(volatile uint32_t *)(rcrb + 0x3410);
        printf_debug("GCS = 0x%x: ", gcs);
@@ -679,13 +666,7 @@ static int enable_flash_sb600(struct pci_dev *dev, const char *name)
        tmp &= 0xffffc000;
        printf_debug("SPI base address is at 0x%x\n", tmp + low_bits);
 
-       sb600_spibar = mmap(0, 0x4000, PROT_READ | PROT_WRITE, MAP_SHARED,
-                           fd_mem, (off_t)tmp);
-       if (sb600_spibar == MAP_FAILED) {
-               perror("Can't mmap memory using " MEM_DEV);
-               mmap_errmsg();
-               exit(1);
-       }
+       sb600_spibar = physmap("SB600 SPI registers", tmp, 0x4000);
        sb600_spibar += low_bits;
 
        /* Clear ROM protect 0-3. */
@@ -835,14 +816,7 @@ static int get_flashbase_sc520(struct pci_dev *dev, const char *name)
        void *mmcr;
 
        /* 1. Map MMCR */
-       mmcr = mmap(0, getpagesize(), PROT_WRITE | PROT_READ,
-                       MAP_SHARED, fd_mem, (off_t)0xFFFEF000);
-
-       if (mmcr == MAP_FAILED) {
-               perror("Can't mmap Elan SC520 specific registers using " MEM_DEV);
-               mmap_errmsg();
-               exit(1);
-       }
+       mmcr = physmap("Elan SC520 MMCR", 0xfffef000, getpagesize());
 
        /* 2. Scan PAR0 (0x88) - PAR15 (0xc4) for
         *    BOOTCS region (PARx[31:29] = 100b)e
index a2ff9129dae843edf95f2a3339844791c6262880..bad3110e3e17b7f62ac1fd6905fe7c90b328d676 100644 (file)
@@ -466,21 +466,15 @@ typedef enum {
 extern flashbus_t flashbus;
 extern void *spibar;
 
-/* Physical memory mapping device */
-#if defined (__sun) && (defined(__i386) || defined(__amd64))
-#  define MEM_DEV "/dev/xsvc"
-#else
-#  define MEM_DEV "/dev/mem"
-#endif
-
-extern int fd_mem;
-
 /* debug.c */
 extern int verbose;
 #define printf_debug(x...) { if (verbose) printf(x); }
 
+/* physmap.c */
+void *physmap(const char *descr, unsigned long phys_addr, size_t len);
+void physunmap(void *virt_addr, size_t len);
+
 /* flashrom.c */
-void mmap_errmsg();
 void map_flash_registers(struct flashchip *flash);
 
 /* layout.c */
index eb0d8e0fd9669fbdf0287571c5de7af9cfc17aa9..5d0f1ed5ab1c240a4d615934c00e1c56ab6e599c 100644 (file)
@@ -22,7 +22,6 @@
 
 #include <errno.h>
 #include <fcntl.h>
-#include <sys/mman.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
@@ -44,7 +43,6 @@ char *chip_to_probe = NULL;
 struct pci_access *pacc;       /* For board and chipset_enable */
 int exclude_start_page, exclude_end_page;
 int verbose = 0;
-int fd_mem;
 
 struct pci_dev *pci_dev_find(uint16_t vendor, uint16_t device)
 {
@@ -84,31 +82,10 @@ struct pci_dev *pci_card_find(uint16_t vendor, uint16_t device,
        return NULL;
 }
 
-void mmap_errmsg()
-{
-       if (EINVAL == errno) {
-               fprintf(stderr, "In Linux this error can be caused by the CONFIG_NONPROMISC_DEVMEM (<2.6.27),\n");
-               fprintf(stderr, "CONFIG_STRICT_DEVMEM (>=2.6.27) and CONFIG_X86_PAT kernel options.\n");
-               fprintf(stderr, "Please check if either is enabled in your kernel before reporting a failure.\n");
-               fprintf(stderr, "You can override CONFIG_X86_PAT at boot with the nopat kernel parameter but\n");
-               fprintf(stderr, "disabling the other option unfortunately requires a kernel recompile. Sorry!\n");
-       }
-}
-
 void map_flash_registers(struct flashchip *flash)
 {
-       volatile uint8_t *registers;
        size_t size = flash->total_size * 1024;
-
-       registers = mmap(0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
-                        fd_mem, (off_t) (0xFFFFFFFF - 0x400000 - size + 1));
-
-       if (registers == MAP_FAILED) {
-               perror("Can't mmap registers using " MEM_DEV);
-               mmap_errmsg();
-               exit(1);
-       }
-       flash->virtual_registers = registers;
+       flash->virtual_registers = physmap("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
 }
 
 struct flashchip *probe_flash(struct flashchip *first_flash, int force)
@@ -145,14 +122,7 @@ struct flashchip *probe_flash(struct flashchip *first_flash, int force)
                }
 
                base = flashbase ? flashbase : (0xffffffff - size + 1);
-               bios = mmap(0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
-                           fd_mem, (off_t) base);
-               if (bios == MAP_FAILED) {
-                       perror("Can't mmap memory using " MEM_DEV);
-                       mmap_errmsg();
-                       exit(1);
-               }
-               flash->virtual_memory = bios;
+               flash->virtual_memory = bios = physmap("flash chip", base, size);
 
                if (force)
                        break;
@@ -165,7 +135,7 @@ struct flashchip *probe_flash(struct flashchip *first_flash, int force)
                        break;
 
 notfound:
-               munmap((void *)bios, size);
+               physunmap((void *)bios, size);
        }
 
        if (!flash || !flash->name)
@@ -451,13 +421,6 @@ int main(int argc, char *argv[])
        pci_init(pacc);         /* Initialize the PCI library */
        pci_scan_bus(pacc);     /* We want to get the list of devices */
 
-       /* Open the memory device UNCACHED. That's important for MMIO. */
-       if ((fd_mem = open(MEM_DEV, O_RDWR | O_SYNC)) < 0) {
-               perror("Error: Can not access memory using " MEM_DEV
-                      ". You need to be root.");
-               exit(1);
-       }
-
        myusec_calibrate_delay();
 
        /* We look at the lbtable first to see if we need a
diff --git a/util/flashrom/physmap.c b/util/flashrom/physmap.c
new file mode 100644 (file)
index 0000000..65d2569
--- /dev/null
@@ -0,0 +1,58 @@
+#include <sys/mman.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <errno.h>
+#include "flash.h"
+
+#if defined (__sun) && (defined(__i386) || defined(__amd64))
+#  define MEM_DEV "/dev/xsvc"
+#else
+#  define MEM_DEV "/dev/mem"
+#endif
+
+static int fd_mem = -1;
+
+void *sys_physmap(unsigned long phys_addr, size_t len)
+{
+       void *virt_addr;
+
+       if (-1 == fd_mem) {
+               /* Open the memory device UNCACHED. Important for MMIO. */
+               if (-1 == (fd_mem = open(MEM_DEV, O_RDWR|O_SYNC))) {
+                       perror("Critical error: open(" MEM_DEV ")");
+                       exit(1);
+               }
+       }
+
+       virt_addr = mmap(0, len, PROT_WRITE|PROT_READ, MAP_SHARED, fd_mem, (off_t)phys_addr);
+       return MAP_FAILED == virt_addr ? NULL : virt_addr;
+}
+
+void physunmap(void *virt_addr, size_t len)
+{
+       munmap(virt_addr, len);
+}
+
+void *physmap(const char *descr, unsigned long phys_addr, size_t len)
+{
+       void *virt_addr = sys_physmap(phys_addr, len);
+
+       if (NULL == virt_addr) {
+               if (NULL == descr)
+                       descr = "memory";
+               fprintf(stderr, "Error accessing %s, 0x%lx bytes at 0x%08lx\n", descr, (unsigned long)len, phys_addr);
+               perror(MEM_DEV " mmap failed");
+               if (EINVAL == errno) {
+                       fprintf(stderr, "In Linux this error can be caused by the CONFIG_NONPROMISC_DEVMEM (<2.6.27),\n");
+                       fprintf(stderr, "CONFIG_STRICT_DEVMEM (>=2.6.27) and CONFIG_X86_PAT kernel options.\n");
+                       fprintf(stderr, "Please check if either is enabled in your kernel before reporting a failure.\n");
+                       fprintf(stderr, "You can override CONFIG_X86_PAT at boot with the nopat kernel parameter but\n");
+                       fprintf(stderr, "disabling the other option unfortunately requires a kernel recompile. Sorry!\n");
+               }
+               exit(1);
+       }
+
+       return virt_addr;
+}