blob: 0fb6c5ca6a987550d33cb8eb04f96cfcb43c0804 [file] [log] [blame]
Peter Stuge33fefca2009-01-26 01:33:02 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2009 Peter Stuge <peter@stuge.se>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
Stefan Reinauer0593f212009-01-26 01:10:48 +000019#include <sys/types.h>
20#include <sys/stat.h>
21#include <fcntl.h>
22#include <stdlib.h>
23#include <errno.h>
24#include "flash.h"
25
Stefan Reinauerf79edb92009-01-26 01:23:31 +000026#ifdef __DARWIN__
27#include <DirectIO/darwinio.h>
28
29#define MEM_DEV "DirectIO"
30
31void *sys_physmap(unsigned long phys_addr, size_t len)
32{
33 return map_physical(phys_addr, len);
34}
35
36void physunmap(void *virt_addr, size_t len)
37{
38 unmap_physical(virt_addr, len);
39}
40
41#else
42#include <sys/mman.h>
43
Stefan Reinauer0593f212009-01-26 01:10:48 +000044#if defined (__sun) && (defined(__i386) || defined(__amd64))
45# define MEM_DEV "/dev/xsvc"
46#else
47# define MEM_DEV "/dev/mem"
48#endif
49
50static int fd_mem = -1;
51
52void *sys_physmap(unsigned long phys_addr, size_t len)
53{
54 void *virt_addr;
55
56 if (-1 == fd_mem) {
57 /* Open the memory device UNCACHED. Important for MMIO. */
58 if (-1 == (fd_mem = open(MEM_DEV, O_RDWR|O_SYNC))) {
59 perror("Critical error: open(" MEM_DEV ")");
Peter Stuge43438ba2009-01-26 02:04:19 +000060 exit(2);
Stefan Reinauer0593f212009-01-26 01:10:48 +000061 }
62 }
63
64 virt_addr = mmap(0, len, PROT_WRITE|PROT_READ, MAP_SHARED, fd_mem, (off_t)phys_addr);
65 return MAP_FAILED == virt_addr ? NULL : virt_addr;
66}
67
68void physunmap(void *virt_addr, size_t len)
69{
70 munmap(virt_addr, len);
71}
Stefan Reinauerf79edb92009-01-26 01:23:31 +000072#endif
Stefan Reinauer0593f212009-01-26 01:10:48 +000073
74void *physmap(const char *descr, unsigned long phys_addr, size_t len)
75{
76 void *virt_addr = sys_physmap(phys_addr, len);
77
78 if (NULL == virt_addr) {
79 if (NULL == descr)
80 descr = "memory";
81 fprintf(stderr, "Error accessing %s, 0x%lx bytes at 0x%08lx\n", descr, (unsigned long)len, phys_addr);
82 perror(MEM_DEV " mmap failed");
83 if (EINVAL == errno) {
84 fprintf(stderr, "In Linux this error can be caused by the CONFIG_NONPROMISC_DEVMEM (<2.6.27),\n");
85 fprintf(stderr, "CONFIG_STRICT_DEVMEM (>=2.6.27) and CONFIG_X86_PAT kernel options.\n");
86 fprintf(stderr, "Please check if either is enabled in your kernel before reporting a failure.\n");
87 fprintf(stderr, "You can override CONFIG_X86_PAT at boot with the nopat kernel parameter but\n");
88 fprintf(stderr, "disabling the other option unfortunately requires a kernel recompile. Sorry!\n");
89 }
Peter Stuge43438ba2009-01-26 02:04:19 +000090 exit(3);
Stefan Reinauer0593f212009-01-26 01:10:48 +000091 }
92
93 return virt_addr;
94}