summaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorStefan Reinauer <stepan@coresystems.de>2009-01-26 01:10:48 +0000
committerPeter Stuge <peter@stuge.se>2009-01-26 01:10:48 +0000
commit53ac3947db69be8c526958b07f59a24dbc3678be (patch)
treeafcde113a21fbc434c6751fcd1b39a9e85137073 /util
parentac29d61a454f0562c5c63ea1f5e14f9068904bf0 (diff)
flashrom: Abstract mmap() in physmap.c and only open /dev/mem on the first physmap() call.
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
Diffstat (limited to 'util')
-rw-r--r--util/flashrom/Makefile2
-rw-r--r--util/flashrom/cbtable.c8
-rw-r--r--util/flashrom/chipset_enable.c34
-rw-r--r--util/flashrom/flash.h14
-rw-r--r--util/flashrom/flashrom.c43
-rw-r--r--util/flashrom/physmap.c58
6 files changed, 71 insertions, 88 deletions
diff --git a/util/flashrom/Makefile b/util/flashrom/Makefile
index 940f346ef8..d46600b653 100644
--- a/util/flashrom/Makefile
+++ b/util/flashrom/Makefile
@@ -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
diff --git a/util/flashrom/cbtable.c b/util/flashrom/cbtable.c
index 25c3ee2d3e..284140a563 100644
--- a/util/flashrom/cbtable.c
+++ b/util/flashrom/cbtable.c
@@ -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);
diff --git a/util/flashrom/chipset_enable.c b/util/flashrom/chipset_enable.c
index d7eb7fa13d..7298729c28 100644
--- a/util/flashrom/chipset_enable.c
+++ b/util/flashrom/chipset_enable.c
@@ -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
diff --git a/util/flashrom/flash.h b/util/flashrom/flash.h
index a2ff9129da..bad3110e3e 100644
--- a/util/flashrom/flash.h
+++ b/util/flashrom/flash.h
@@ -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 */
diff --git a/util/flashrom/flashrom.c b/util/flashrom/flashrom.c
index eb0d8e0fd9..5d0f1ed5ab 100644
--- a/util/flashrom/flashrom.c
+++ b/util/flashrom/flashrom.c
@@ -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
index 0000000000..65d25698c8
--- /dev/null
+++ b/util/flashrom/physmap.c
@@ -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;
+}