Carl-Daniel Hailfinger | 5d5c072 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2009 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; 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 |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
Carl-Daniel Hailfinger | 5d5c072 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 21 | * Header file for hardware access and OS abstraction. Included from flash.h. |
| 22 | */ |
| 23 | |
| 24 | #ifndef __HWACCESS_H__ |
| 25 | #define __HWACCESS_H__ 1 |
| 26 | |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 27 | #include "platform.h" |
| 28 | |
Carl-Daniel Hailfinger | 5d5c072 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 29 | #if NEED_PCI == 1 |
Carl-Daniel Hailfinger | 7237683 | 2010-06-25 13:18:48 +0000 | [diff] [blame] | 30 | /* |
| 31 | * libpci headers use the variable name "index" which triggers shadowing |
| 32 | * warnings on systems which have the index() function in a default #include |
| 33 | * or as builtin. |
| 34 | */ |
| 35 | #define index shadow_workaround_index |
Stefan Tauner | c65b855 | 2013-09-12 15:48:39 +0000 | [diff] [blame] | 36 | |
Stefan Tauner | 8d21ff1 | 2015-01-10 09:33:06 +0000 | [diff] [blame] | 37 | #if !defined (__NetBSD__) |
Carl-Daniel Hailfinger | 5d5c072 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 38 | #include <pci/pci.h> |
Stefan Tauner | c65b855 | 2013-09-12 15:48:39 +0000 | [diff] [blame] | 39 | #else |
| 40 | #include <pciutils/pci.h> |
| 41 | #endif |
| 42 | |
Carl-Daniel Hailfinger | 7237683 | 2010-06-25 13:18:48 +0000 | [diff] [blame] | 43 | #undef index |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 44 | #endif /* NEED_PCI == 1 */ |
| 45 | |
| 46 | |
| 47 | /* The next big hunk tries to guess endianess from various preprocessor macros */ |
Stefan Tauner | 23e10b8 | 2016-01-23 16:16:49 +0000 | [diff] [blame] | 48 | /* First some error checking in case some weird header has defined both. |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 49 | * NB: OpenBSD always defines _BIG_ENDIAN and _LITTLE_ENDIAN. */ |
| 50 | #if defined (__LITTLE_ENDIAN__) && defined (__BIG_ENDIAN__) |
| 51 | #error Conflicting endianness #define |
Carl-Daniel Hailfinger | 5d5c072 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 52 | #endif |
| 53 | |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 54 | #if IS_X86 |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 55 | |
| 56 | /* All x86 is little-endian. */ |
| 57 | #define __FLASHROM_LITTLE_ENDIAN__ 1 |
| 58 | |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 59 | #elif IS_MIPS |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 60 | |
| 61 | /* MIPS can be either endian. */ |
| 62 | #if defined (__MIPSEL) || defined (__MIPSEL__) || defined (_MIPSEL) || defined (MIPSEL) |
| 63 | #define __FLASHROM_LITTLE_ENDIAN__ 1 |
| 64 | #elif defined (__MIPSEB) || defined (__MIPSEB__) || defined (_MIPSEB) || defined (MIPSEB) |
| 65 | #define __FLASHROM_BIG_ENDIAN__ 1 |
| 66 | #endif |
| 67 | |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 68 | #elif IS_PPC |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 69 | |
| 70 | /* PowerPC can be either endian. */ |
| 71 | #if defined (_BIG_ENDIAN) || defined (__BIG_ENDIAN__) |
| 72 | #define __FLASHROM_BIG_ENDIAN__ 1 |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 73 | #elif defined (_LITTLE_ENDIAN) || defined (__LITTLE_ENDIAN__) |
David Hendricks | b286da7 | 2012-02-13 00:35:35 +0000 | [diff] [blame] | 74 | #define __FLASHROM_LITTLE_ENDIAN__ 1 |
David Hendricks | b286da7 | 2012-02-13 00:35:35 +0000 | [diff] [blame] | 75 | #endif |
| 76 | |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 77 | #elif IS_ARM |
| 78 | |
| 79 | /* ARM can be either endian. */ |
| 80 | #if defined (__ARMEB__) |
| 81 | #define __FLASHROM_BIG_ENDIAN__ 1 |
| 82 | #elif defined (__ARMEL__) |
| 83 | #define __FLASHROM_LITTLE_ENDIAN__ 1 |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 84 | #endif |
| 85 | |
Stefan Tauner | fb2d77c | 2015-02-10 08:03:10 +0000 | [diff] [blame] | 86 | #elif IS_SPARC |
| 87 | /* SPARC is big endian in general (but allows to access data in little endian too). */ |
| 88 | #define __FLASHROM_BIG_ENDIAN__ 1 |
| 89 | |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 90 | #endif /* IS_? */ |
| 91 | |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 92 | #if !defined (__FLASHROM_BIG_ENDIAN__) && !defined (__FLASHROM_LITTLE_ENDIAN__) |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 93 | |
| 94 | /* If architecture-specific approaches fail try generic variants. First: BSD (works about everywhere). */ |
| 95 | #if !IS_WINDOWS |
| 96 | #include <sys/param.h> |
| 97 | |
| 98 | #if defined (__BYTE_ORDER) |
| 99 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
| 100 | #define __FLASHROM_LITTLE_ENDIAN__ |
| 101 | #elif __BYTE_ORDER == __BIG_ENDIAN |
| 102 | #define __FLASHROM_BIG_ENDIAN__ |
| 103 | #else |
| 104 | #error Unknown byte order! |
| 105 | #endif |
| 106 | #endif /* defined __BYTE_ORDER */ |
| 107 | #endif /* !IS_WINDOWS */ |
| 108 | |
| 109 | #if !defined (__FLASHROM_BIG_ENDIAN__) && !defined (__FLASHROM_LITTLE_ENDIAN__) |
| 110 | |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 111 | /* Nonstandard libc-specific macros for determining endianness. */ |
Gwenhael Goavec-Merou | 8cd0c73 | 2015-11-14 02:55:12 +0000 | [diff] [blame] | 112 | /* musl provides an endian.h as well... but it can not be detected from within C. */ |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 113 | #if defined(__GLIBC__) |
| 114 | #include <endian.h> |
| 115 | #if BYTE_ORDER == LITTLE_ENDIAN |
| 116 | #define __FLASHROM_LITTLE_ENDIAN__ 1 |
| 117 | #elif BYTE_ORDER == BIG_ENDIAN |
| 118 | #define __FLASHROM_BIG_ENDIAN__ 1 |
| 119 | #endif |
| 120 | #endif |
| 121 | #endif |
| 122 | |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 123 | #endif |
| 124 | |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 125 | #if !defined (__FLASHROM_BIG_ENDIAN__) && !defined (__FLASHROM_LITTLE_ENDIAN__) |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 126 | #error Unable to determine endianness. |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 127 | #endif |
| 128 | |
| 129 | #define ___constant_swab8(x) ((uint8_t) ( \ |
| 130 | (((uint8_t)(x) & (uint8_t)0xffU)))) |
| 131 | |
| 132 | #define ___constant_swab16(x) ((uint16_t) ( \ |
| 133 | (((uint16_t)(x) & (uint16_t)0x00ffU) << 8) | \ |
| 134 | (((uint16_t)(x) & (uint16_t)0xff00U) >> 8))) |
| 135 | |
| 136 | #define ___constant_swab32(x) ((uint32_t) ( \ |
| 137 | (((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) | \ |
| 138 | (((uint32_t)(x) & (uint32_t)0x0000ff00UL) << 8) | \ |
| 139 | (((uint32_t)(x) & (uint32_t)0x00ff0000UL) >> 8) | \ |
| 140 | (((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24))) |
| 141 | |
| 142 | #define ___constant_swab64(x) ((uint64_t) ( \ |
| 143 | (((uint64_t)(x) & (uint64_t)0x00000000000000ffULL) << 56) | \ |
| 144 | (((uint64_t)(x) & (uint64_t)0x000000000000ff00ULL) << 40) | \ |
| 145 | (((uint64_t)(x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \ |
| 146 | (((uint64_t)(x) & (uint64_t)0x00000000ff000000ULL) << 8) | \ |
| 147 | (((uint64_t)(x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \ |
| 148 | (((uint64_t)(x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \ |
| 149 | (((uint64_t)(x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \ |
| 150 | (((uint64_t)(x) & (uint64_t)0xff00000000000000ULL) >> 56))) |
| 151 | |
| 152 | #if defined (__FLASHROM_BIG_ENDIAN__) |
| 153 | |
| 154 | #define cpu_to_le(bits) \ |
| 155 | static inline uint##bits##_t cpu_to_le##bits(uint##bits##_t val) \ |
| 156 | { \ |
| 157 | return ___constant_swab##bits(val); \ |
| 158 | } |
| 159 | |
| 160 | cpu_to_le(8) |
| 161 | cpu_to_le(16) |
| 162 | cpu_to_le(32) |
| 163 | cpu_to_le(64) |
| 164 | |
| 165 | #define cpu_to_be8 |
| 166 | #define cpu_to_be16 |
| 167 | #define cpu_to_be32 |
| 168 | #define cpu_to_be64 |
| 169 | |
| 170 | #elif defined (__FLASHROM_LITTLE_ENDIAN__) |
| 171 | |
| 172 | #define cpu_to_be(bits) \ |
| 173 | static inline uint##bits##_t cpu_to_be##bits(uint##bits##_t val) \ |
| 174 | { \ |
| 175 | return ___constant_swab##bits(val); \ |
| 176 | } |
| 177 | |
| 178 | cpu_to_be(8) |
| 179 | cpu_to_be(16) |
| 180 | cpu_to_be(32) |
| 181 | cpu_to_be(64) |
| 182 | |
| 183 | #define cpu_to_le8 |
| 184 | #define cpu_to_le16 |
| 185 | #define cpu_to_le32 |
| 186 | #define cpu_to_le64 |
| 187 | |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 188 | #endif /* __FLASHROM_BIG_ENDIAN__ / __FLASHROM_LITTLE_ENDIAN__ */ |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 189 | |
| 190 | #define be_to_cpu8 cpu_to_be8 |
| 191 | #define be_to_cpu16 cpu_to_be16 |
| 192 | #define be_to_cpu32 cpu_to_be32 |
| 193 | #define be_to_cpu64 cpu_to_be64 |
| 194 | #define le_to_cpu8 cpu_to_le8 |
| 195 | #define le_to_cpu16 cpu_to_le16 |
| 196 | #define le_to_cpu32 cpu_to_le32 |
| 197 | #define le_to_cpu64 cpu_to_le64 |
| 198 | |
Carl-Daniel Hailfinger | 41bea03 | 2010-07-29 13:17:37 +0000 | [diff] [blame] | 199 | #if NEED_PCI == 1 |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 200 | #if IS_X86 |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 201 | |
Gwenhael Goavec-Merou | 8cd0c73 | 2015-11-14 02:55:12 +0000 | [diff] [blame] | 202 | /* sys/io.h provides iopl(2) and x86 I/O port access functions (inb, outb etc). |
| 203 | * It is included in glibc (thus available also on debian/kFreeBSD) but also in other libcs that mimic glibc, |
Stefan Tauner | 2c57bbe | 2016-02-20 20:21:58 +0000 | [diff] [blame] | 204 | * e.g. musl and uclibc. Because we cannot detect the libc or existence of the header or of the instructions |
| 205 | * themselves safely in here we use some heuristic below: |
| 206 | * On Android we don't have the header file and no way for I/O port access at all. However, sys/glibc-syscalls.h |
| 207 | * refers to an iopl implementation and we therefore include at least that one for now. On non-Android we assume |
| 208 | * that a Linux system's libc has a suitable sys/io.h or (on non-Linux) we depend on glibc to offer it. */ |
| 209 | #if defined(__ANDROID__) |
| 210 | #include <sys/glibc-syscalls.h> |
| 211 | #elif defined(__linux__) || defined(__GLIBC__) |
Gwenhael Goavec-Merou | 8cd0c73 | 2015-11-14 02:55:12 +0000 | [diff] [blame] | 212 | #include <sys/io.h> |
| 213 | #endif |
| 214 | |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 215 | #define __FLASHROM_HAVE_OUTB__ 1 |
| 216 | |
Carl-Daniel Hailfinger | 5d5c072 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 217 | /* for iopl and outb under Solaris */ |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 218 | #if defined (__sun) |
Carl-Daniel Hailfinger | 5d5c072 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 219 | #include <sys/sysi86.h> |
| 220 | #include <sys/psw.h> |
| 221 | #include <asm/sunddi.h> |
| 222 | #endif |
| 223 | |
Carl-Daniel Hailfinger | 0d974e7 | 2010-07-17 12:54:09 +0000 | [diff] [blame] | 224 | /* Clarification about OUTB/OUTW/OUTL argument order: |
| 225 | * OUT[BWL](val, port) |
| 226 | */ |
| 227 | |
Carl-Daniel Hailfinger | 5d5c072 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 228 | #if defined(__FreeBSD__) || defined(__DragonFly__) |
Carl-Daniel Hailfinger | a5eecda | 2012-02-25 22:50:21 +0000 | [diff] [blame] | 229 | /* Note that Debian/kFreeBSD (FreeBSD kernel with glibc) has conflicting |
| 230 | * out[bwl] definitions in machine/cpufunc.h and sys/io.h at least in some |
| 231 | * versions. Use machine/cpufunc.h only for plain FreeBSD/DragonFlyBSD. |
| 232 | */ |
Carl-Daniel Hailfinger | 5d5c072 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 233 | #include <machine/cpufunc.h> |
Michael Karcher | e7f3209 | 2010-01-12 15:36:24 +0000 | [diff] [blame] | 234 | #define OUTB(x, y) do { u_int outb_tmp = (y); outb(outb_tmp, (x)); } while (0) |
| 235 | #define OUTW(x, y) do { u_int outw_tmp = (y); outw(outw_tmp, (x)); } while (0) |
| 236 | #define OUTL(x, y) do { u_int outl_tmp = (y); outl(outl_tmp, (x)); } while (0) |
| 237 | #define INB(x) __extension__ ({ u_int inb_tmp = (x); inb(inb_tmp); }) |
| 238 | #define INW(x) __extension__ ({ u_int inw_tmp = (x); inw(inw_tmp); }) |
| 239 | #define INL(x) __extension__ ({ u_int inl_tmp = (x); inl(inl_tmp); }) |
Carl-Daniel Hailfinger | 5d5c072 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 240 | #else |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 241 | |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 242 | #if defined (__sun) |
Carl-Daniel Hailfinger | 5d5c072 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 243 | /* Note different order for outb */ |
| 244 | #define OUTB(x,y) outb(y, x) |
| 245 | #define OUTW(x,y) outw(y, x) |
| 246 | #define OUTL(x,y) outl(y, x) |
| 247 | #define INB inb |
| 248 | #define INW inw |
| 249 | #define INL inl |
| 250 | #else |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 251 | |
| 252 | #ifdef __DJGPP__ |
| 253 | |
| 254 | #include <pc.h> |
| 255 | |
| 256 | #define OUTB(x,y) outportb(y, x) |
| 257 | #define OUTW(x,y) outportw(y, x) |
| 258 | #define OUTL(x,y) outportl(y, x) |
| 259 | |
| 260 | #define INB inportb |
| 261 | #define INW inportw |
| 262 | #define INL inportl |
| 263 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 264 | #else |
Stefan Tauner | c71759d | 2015-05-23 23:59:23 +0000 | [diff] [blame] | 265 | |
| 266 | #if defined(__MACH__) && defined(__APPLE__) |
| 267 | /* Header is part of the DirectHW library. */ |
| 268 | #include <DirectHW/DirectHW.h> |
| 269 | #endif |
| 270 | |
Carl-Daniel Hailfinger | a5eecda | 2012-02-25 22:50:21 +0000 | [diff] [blame] | 271 | /* This is the usual glibc interface. */ |
Carl-Daniel Hailfinger | 5d5c072 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 272 | #define OUTB outb |
| 273 | #define OUTW outw |
| 274 | #define OUTL outl |
| 275 | #define INB inb |
| 276 | #define INW inw |
| 277 | #define INL inl |
Rudolf Marek | 03ae5c1 | 2010-03-16 23:59:19 +0000 | [diff] [blame] | 278 | #endif |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 279 | #endif |
Carl-Daniel Hailfinger | 5d5c072 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 280 | #endif |
Carl-Daniel Hailfinger | 5d5c072 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 281 | |
Carl-Daniel Hailfinger | b63b067 | 2010-07-02 17:12:50 +0000 | [diff] [blame] | 282 | #if defined(__NetBSD__) || defined (__OpenBSD__) |
Jonathan A. Kollasch | 3646c8f | 2010-01-08 21:18:08 +0000 | [diff] [blame] | 283 | #if defined(__i386__) || defined(__x86_64__) |
| 284 | #include <sys/types.h> |
| 285 | #include <machine/sysarch.h> |
Stefan Tauner | 0554ca5 | 2013-07-25 22:54:25 +0000 | [diff] [blame] | 286 | #if defined(__NetBSD__) |
| 287 | #if defined(__i386__) |
| 288 | #define iopl i386_iopl |
| 289 | #elif defined(__x86_64__) |
| 290 | #define iopl x86_64_iopl |
| 291 | #endif |
| 292 | #elif defined (__OpenBSD__) |
| 293 | #if defined(__i386__) |
| 294 | #define iopl i386_iopl |
| 295 | #elif defined(__amd64__) |
| 296 | #define iopl amd64_iopl |
| 297 | #endif |
Jonathan A. Kollasch | 3646c8f | 2010-01-08 21:18:08 +0000 | [diff] [blame] | 298 | #endif |
Jonathan A. Kollasch | 3646c8f | 2010-01-08 21:18:08 +0000 | [diff] [blame] | 299 | |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 300 | static inline void outb(uint8_t value, uint16_t port) |
Jonathan A. Kollasch | 3646c8f | 2010-01-08 21:18:08 +0000 | [diff] [blame] | 301 | { |
Carl-Daniel Hailfinger | 1c6d2ff | 2012-08-27 00:44:42 +0000 | [diff] [blame] | 302 | __asm__ volatile ("outb %b0,%w1": :"a" (value), "Nd" (port)); |
Jonathan A. Kollasch | 3646c8f | 2010-01-08 21:18:08 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 305 | static inline uint8_t inb(uint16_t port) |
Jonathan A. Kollasch | 3646c8f | 2010-01-08 21:18:08 +0000 | [diff] [blame] | 306 | { |
| 307 | uint8_t value; |
Carl-Daniel Hailfinger | 1c6d2ff | 2012-08-27 00:44:42 +0000 | [diff] [blame] | 308 | __asm__ volatile ("inb %w1,%0":"=a" (value):"Nd" (port)); |
Jonathan A. Kollasch | 3646c8f | 2010-01-08 21:18:08 +0000 | [diff] [blame] | 309 | return value; |
| 310 | } |
| 311 | |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 312 | static inline void outw(uint16_t value, uint16_t port) |
Jonathan A. Kollasch | 3646c8f | 2010-01-08 21:18:08 +0000 | [diff] [blame] | 313 | { |
Carl-Daniel Hailfinger | 1c6d2ff | 2012-08-27 00:44:42 +0000 | [diff] [blame] | 314 | __asm__ volatile ("outw %w0,%w1": :"a" (value), "Nd" (port)); |
Jonathan A. Kollasch | 3646c8f | 2010-01-08 21:18:08 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 317 | static inline uint16_t inw(uint16_t port) |
Jonathan A. Kollasch | 3646c8f | 2010-01-08 21:18:08 +0000 | [diff] [blame] | 318 | { |
| 319 | uint16_t value; |
Carl-Daniel Hailfinger | 1c6d2ff | 2012-08-27 00:44:42 +0000 | [diff] [blame] | 320 | __asm__ volatile ("inw %w1,%0":"=a" (value):"Nd" (port)); |
Jonathan A. Kollasch | 3646c8f | 2010-01-08 21:18:08 +0000 | [diff] [blame] | 321 | return value; |
| 322 | } |
| 323 | |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 324 | static inline void outl(uint32_t value, uint16_t port) |
Jonathan A. Kollasch | 3646c8f | 2010-01-08 21:18:08 +0000 | [diff] [blame] | 325 | { |
Carl-Daniel Hailfinger | 1c6d2ff | 2012-08-27 00:44:42 +0000 | [diff] [blame] | 326 | __asm__ volatile ("outl %0,%w1": :"a" (value), "Nd" (port)); |
Jonathan A. Kollasch | 3646c8f | 2010-01-08 21:18:08 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 329 | static inline uint32_t inl(uint16_t port) |
Jonathan A. Kollasch | 3646c8f | 2010-01-08 21:18:08 +0000 | [diff] [blame] | 330 | { |
| 331 | uint32_t value; |
Carl-Daniel Hailfinger | 1c6d2ff | 2012-08-27 00:44:42 +0000 | [diff] [blame] | 332 | __asm__ volatile ("inl %1,%0":"=a" (value):"Nd" (port)); |
Jonathan A. Kollasch | 3646c8f | 2010-01-08 21:18:08 +0000 | [diff] [blame] | 333 | return value; |
| 334 | } |
| 335 | #endif |
| 336 | #endif |
| 337 | |
Carl-Daniel Hailfinger | 60d9bd2 | 2012-08-09 23:34:41 +0000 | [diff] [blame] | 338 | #if !(defined(__MACH__) && defined(__APPLE__)) && !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && !defined(__DragonFly__) && !defined(__LIBPAYLOAD__) |
Carl-Daniel Hailfinger | 5d5c072 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 339 | typedef struct { uint32_t hi, lo; } msr_t; |
| 340 | msr_t rdmsr(int addr); |
| 341 | int wrmsr(int addr, msr_t msr); |
| 342 | #endif |
Carl-Daniel Hailfinger | a5eecda | 2012-02-25 22:50:21 +0000 | [diff] [blame] | 343 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) |
Carl-Daniel Hailfinger | 5d5c072 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 344 | /* FreeBSD already has conflicting definitions for wrmsr/rdmsr. */ |
| 345 | #undef rdmsr |
| 346 | #undef wrmsr |
| 347 | #define rdmsr freebsd_rdmsr |
| 348 | #define wrmsr freebsd_wrmsr |
| 349 | typedef struct { uint32_t hi, lo; } msr_t; |
| 350 | msr_t freebsd_rdmsr(int addr); |
| 351 | int freebsd_wrmsr(int addr, msr_t msr); |
| 352 | #endif |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 353 | #if defined(__LIBPAYLOAD__) |
| 354 | #include <arch/io.h> |
| 355 | #include <arch/msr.h> |
| 356 | typedef struct { uint32_t hi, lo; } msr_t; |
| 357 | msr_t libpayload_rdmsr(int addr); |
| 358 | int libpayload_wrmsr(int addr, msr_t msr); |
| 359 | #undef rdmsr |
| 360 | #define rdmsr libpayload_rdmsr |
| 361 | #define wrmsr libpayload_wrmsr |
| 362 | #endif |
Carl-Daniel Hailfinger | 5d5c072 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 363 | |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 364 | #elif IS_PPC |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 365 | |
| 366 | /* PCI port I/O is not yet implemented on PowerPC. */ |
| 367 | |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 368 | #elif IS_MIPS |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 369 | |
| 370 | /* PCI port I/O is not yet implemented on MIPS. */ |
| 371 | |
Stefan Tauner | fb2d77c | 2015-02-10 08:03:10 +0000 | [diff] [blame] | 372 | #elif IS_SPARC |
| 373 | |
| 374 | /* PCI port I/O is not yet implemented on SPARC. */ |
| 375 | |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 376 | #elif IS_ARM |
David Hendricks | b286da7 | 2012-02-13 00:35:35 +0000 | [diff] [blame] | 377 | |
| 378 | /* Non memory mapped I/O is not supported on ARM. */ |
| 379 | |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 380 | #else |
| 381 | |
| 382 | #error Unknown architecture, please check if it supports PCI port IO. |
| 383 | |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 384 | #endif /* IS_* */ |
| 385 | #endif /* NEED_PCI == 1 */ |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 386 | |
Carl-Daniel Hailfinger | 5d5c072 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 387 | #endif /* !__HWACCESS_H__ */ |