Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2009,2010 Carl-Daniel Hailfinger |
| 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; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | */ |
| 20 | |
| 21 | #include <stdint.h> |
| 22 | #include <string.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <errno.h> |
| 27 | #include "flash.h" |
| 28 | |
| 29 | #if defined(__FreeBSD__) || defined(__DragonFly__) |
| 30 | int io_fd; |
| 31 | #endif |
| 32 | |
| 33 | void get_io_perms(void) |
| 34 | { |
| 35 | #if defined (__sun) && (defined(__i386) || defined(__amd64)) |
| 36 | if (sysi86(SI86V86, V86SC_IOPL, PS_IOPL) != 0) { |
| 37 | #elif defined(__FreeBSD__) || defined (__DragonFly__) |
| 38 | if ((io_fd = open("/dev/io", O_RDWR)) < 0) { |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame^] | 39 | #elif __DJGPP__ |
| 40 | if (0) { |
| 41 | #else |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 42 | if (iopl(3) != 0) { |
| 43 | #endif |
| 44 | fprintf(stderr, "ERROR: Could not get I/O privileges (%s).\n" |
| 45 | "You need to be root.\n", strerror(errno)); |
| 46 | exit(1); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | void release_io_perms(void) |
| 51 | { |
| 52 | #if defined(__FreeBSD__) || defined(__DragonFly__) |
| 53 | close(io_fd); |
| 54 | #endif |
| 55 | } |
| 56 | |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame^] | 57 | #ifdef __DJGPP__ |
| 58 | |
| 59 | extern unsigned short segFS; |
| 60 | |
| 61 | #include <sys/farptr.h> |
| 62 | |
| 63 | void mmio_writeb(uint8_t val, void *addr) |
| 64 | { |
| 65 | _farpokeb(segFS, (unsigned long) addr, val); |
| 66 | } |
| 67 | |
| 68 | void mmio_writew(uint16_t val, void *addr) |
| 69 | { |
| 70 | _farpokew(segFS, (unsigned long) addr, val); |
| 71 | } |
| 72 | |
| 73 | void mmio_writel(uint32_t val, void *addr) |
| 74 | { |
| 75 | _farpokel(segFS, (unsigned long) addr, val); |
| 76 | } |
| 77 | |
| 78 | uint8_t mmio_readb(void *addr) |
| 79 | { |
| 80 | return _farpeekb(segFS, (unsigned long) addr); |
| 81 | } |
| 82 | |
| 83 | uint16_t mmio_readw(void *addr) |
| 84 | { |
| 85 | return _farpeekw(segFS, (unsigned long) addr); |
| 86 | } |
| 87 | |
| 88 | uint32_t mmio_readl(void *addr) |
| 89 | { |
| 90 | return _farpeekl(segFS, (unsigned long) addr); |
| 91 | } |
| 92 | |
| 93 | #else |
| 94 | |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 95 | void mmio_writeb(uint8_t val, void *addr) |
| 96 | { |
| 97 | *(volatile uint8_t *) addr = val; |
| 98 | } |
| 99 | |
| 100 | void mmio_writew(uint16_t val, void *addr) |
| 101 | { |
| 102 | *(volatile uint16_t *) addr = val; |
| 103 | } |
| 104 | |
| 105 | void mmio_writel(uint32_t val, void *addr) |
| 106 | { |
| 107 | *(volatile uint32_t *) addr = val; |
| 108 | } |
| 109 | |
| 110 | uint8_t mmio_readb(void *addr) |
| 111 | { |
| 112 | return *(volatile uint8_t *) addr; |
| 113 | } |
| 114 | |
| 115 | uint16_t mmio_readw(void *addr) |
| 116 | { |
| 117 | return *(volatile uint16_t *) addr; |
| 118 | } |
| 119 | |
| 120 | uint32_t mmio_readl(void *addr) |
| 121 | { |
| 122 | return *(volatile uint32_t *) addr; |
| 123 | } |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame^] | 124 | #endif |