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> |
Carl-Daniel Hailfinger | dcef67e | 2010-06-21 23:20:15 +0000 | [diff] [blame] | 24 | #include <sys/types.h> |
| 25 | #if !defined (__DJGPP__) |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 26 | #include <unistd.h> |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 27 | #include <fcntl.h> |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 28 | #include <errno.h> |
Carl-Daniel Hailfinger | dcef67e | 2010-06-21 23:20:15 +0000 | [diff] [blame] | 29 | #endif |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 30 | #include "flash.h" |
| 31 | |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 32 | #if defined(__i386__) || defined(__x86_64__) |
| 33 | |
| 34 | /* sync primitive is not needed because x86 uses uncached accesses |
| 35 | * which have a strongly ordered memory model. |
| 36 | */ |
| 37 | static inline void sync_primitive(void) |
| 38 | { |
| 39 | } |
| 40 | |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 41 | #if defined(__FreeBSD__) || defined(__DragonFly__) |
| 42 | int io_fd; |
| 43 | #endif |
| 44 | |
| 45 | void get_io_perms(void) |
| 46 | { |
Carl-Daniel Hailfinger | dcef67e | 2010-06-21 23:20:15 +0000 | [diff] [blame] | 47 | #if defined(__DJGPP__) |
| 48 | /* We have full permissions by default. */ |
| 49 | return; |
| 50 | #else |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 51 | #if defined (__sun) && (defined(__i386) || defined(__amd64)) |
| 52 | if (sysi86(SI86V86, V86SC_IOPL, PS_IOPL) != 0) { |
| 53 | #elif defined(__FreeBSD__) || defined (__DragonFly__) |
| 54 | if ((io_fd = open("/dev/io", O_RDWR)) < 0) { |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 55 | #else |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 56 | if (iopl(3) != 0) { |
| 57 | #endif |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 58 | msg_perr("ERROR: Could not get I/O privileges (%s).\n" |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 59 | "You need to be root.\n", strerror(errno)); |
Carl-Daniel Hailfinger | b63b067 | 2010-07-02 17:12:50 +0000 | [diff] [blame^] | 60 | #if defined (__OpenBSD__) |
| 61 | msg_perr("Please set securelevel=-1 in /etc/rc.securelevel " |
| 62 | "and reboot, or reboot into \n"); |
| 63 | msg_perr("single user mode.\n"); |
| 64 | #endif |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 65 | exit(1); |
| 66 | } |
Carl-Daniel Hailfinger | dcef67e | 2010-06-21 23:20:15 +0000 | [diff] [blame] | 67 | #endif |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void release_io_perms(void) |
| 71 | { |
| 72 | #if defined(__FreeBSD__) || defined(__DragonFly__) |
| 73 | close(io_fd); |
| 74 | #endif |
| 75 | } |
| 76 | |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 77 | #elif defined(__powerpc__) || defined(__powerpc64__) || defined(__ppc__) || defined(__ppc64__) |
| 78 | |
| 79 | static inline void sync_primitive(void) |
| 80 | { |
| 81 | /* Prevent reordering and/or merging of reads/writes to hardware. |
| 82 | * Such reordering and/or merging would break device accesses which |
| 83 | * depend on the exact access order. |
| 84 | */ |
| 85 | asm("eieio" : : : "memory"); |
| 86 | } |
| 87 | |
| 88 | /* PCI port I/O is not yet implemented on PowerPC. */ |
| 89 | void get_io_perms(void) |
| 90 | { |
| 91 | } |
| 92 | |
| 93 | /* PCI port I/O is not yet implemented on PowerPC. */ |
| 94 | void release_io_perms(void) |
| 95 | { |
| 96 | } |
| 97 | |
| 98 | #elif defined (__mips) || defined (__mips__) || defined (_mips) || defined (mips) |
| 99 | |
| 100 | /* sync primitive is not needed because /dev/mem on MIPS uses uncached accesses |
| 101 | * in mode 2 which has a strongly ordered memory model. |
| 102 | */ |
| 103 | static inline void sync_primitive(void) |
| 104 | { |
| 105 | } |
| 106 | |
| 107 | /* PCI port I/O is not yet implemented on MIPS. */ |
| 108 | void get_io_perms(void) |
| 109 | { |
| 110 | } |
| 111 | |
| 112 | /* PCI port I/O is not yet implemented on MIPS. */ |
| 113 | void release_io_perms(void) |
| 114 | { |
| 115 | } |
| 116 | |
| 117 | #else |
| 118 | |
| 119 | #error Unknown architecture |
| 120 | |
| 121 | #endif |
| 122 | |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 123 | void mmio_writeb(uint8_t val, void *addr) |
| 124 | { |
| 125 | *(volatile uint8_t *) addr = val; |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 126 | sync_primitive(); |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void mmio_writew(uint16_t val, void *addr) |
| 130 | { |
| 131 | *(volatile uint16_t *) addr = val; |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 132 | sync_primitive(); |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void mmio_writel(uint32_t val, void *addr) |
| 136 | { |
| 137 | *(volatile uint32_t *) addr = val; |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 138 | sync_primitive(); |
Carl-Daniel Hailfinger | fb0828f | 2010-02-12 19:35:25 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | uint8_t mmio_readb(void *addr) |
| 142 | { |
| 143 | return *(volatile uint8_t *) addr; |
| 144 | } |
| 145 | |
| 146 | uint16_t mmio_readw(void *addr) |
| 147 | { |
| 148 | return *(volatile uint16_t *) addr; |
| 149 | } |
| 150 | |
| 151 | uint32_t mmio_readl(void *addr) |
| 152 | { |
| 153 | return *(volatile uint32_t *) addr; |
| 154 | } |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 155 | |
| 156 | void mmio_le_writeb(uint8_t val, void *addr) |
| 157 | { |
| 158 | mmio_writeb(cpu_to_le8(val), addr); |
| 159 | } |
| 160 | |
| 161 | void mmio_le_writew(uint16_t val, void *addr) |
| 162 | { |
| 163 | mmio_writew(cpu_to_le16(val), addr); |
| 164 | } |
| 165 | |
| 166 | void mmio_le_writel(uint32_t val, void *addr) |
| 167 | { |
| 168 | mmio_writel(cpu_to_le32(val), addr); |
| 169 | } |
| 170 | |
| 171 | uint8_t mmio_le_readb(void *addr) |
| 172 | { |
| 173 | return le_to_cpu8(mmio_readb(addr)); |
| 174 | } |
| 175 | |
| 176 | uint16_t mmio_le_readw(void *addr) |
| 177 | { |
| 178 | return le_to_cpu16(mmio_readw(addr)); |
| 179 | } |
| 180 | |
| 181 | uint32_t mmio_le_readl(void *addr) |
| 182 | { |
| 183 | return le_to_cpu32(mmio_readl(addr)); |
| 184 | } |