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