hwaccess.h 10.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * This file is part of the flashrom project.
 *
 * Copyright (C) 2009 Carl-Daniel Hailfinger
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18 19 20
 */

/*
21 22 23 24 25 26
 * Header file for hardware access and OS abstraction. Included from flash.h.
 */

#ifndef __HWACCESS_H__
#define __HWACCESS_H__ 1

27 28
#include "platform.h"

29
#if NEED_PCI == 1
30 31 32 33 34 35
/*
 * libpci headers use the variable name "index" which triggers shadowing
 * warnings on systems which have the index() function in a default #include
 * or as builtin.
 */
#define index shadow_workaround_index
Stefan Tauner's avatar
Stefan Tauner committed
36

Stefan Tauner's avatar
Stefan Tauner committed
37
#if !defined (__NetBSD__)
38
#include <pci/pci.h>
Stefan Tauner's avatar
Stefan Tauner committed
39 40 41 42
#else
#include <pciutils/pci.h>
#endif

43
#undef index
44 45 46 47 48 49 50 51
#endif /* NEED_PCI == 1 */


/* The next big hunk tries to guess endianess from various preprocessor macros */
/* First some error checking in case some weird header has defines both.
 * NB: OpenBSD always defines _BIG_ENDIAN and _LITTLE_ENDIAN. */
#if defined (__LITTLE_ENDIAN__) && defined (__BIG_ENDIAN__)
#error Conflicting endianness #define
52 53
#endif

54
#if IS_X86
55 56 57 58

/* All x86 is little-endian. */
#define __FLASHROM_LITTLE_ENDIAN__ 1

59
#elif IS_MIPS
60 61 62 63 64 65 66 67

/* MIPS can be either endian. */
#if defined (__MIPSEL) || defined (__MIPSEL__) || defined (_MIPSEL) || defined (MIPSEL)
#define __FLASHROM_LITTLE_ENDIAN__ 1
#elif defined (__MIPSEB) || defined (__MIPSEB__) || defined (_MIPSEB) || defined (MIPSEB)
#define __FLASHROM_BIG_ENDIAN__ 1
#endif

68
#elif IS_PPC
69 70 71 72

/* PowerPC can be either endian. */
#if defined (_BIG_ENDIAN) || defined (__BIG_ENDIAN__)
#define __FLASHROM_BIG_ENDIAN__ 1
73 74
#elif defined (_LITTLE_ENDIAN) || defined (__LITTLE_ENDIAN__)
#define __FLASHROM_LITTLE_ENDIAN__ 1
75 76
#endif

77 78 79 80 81 82
#elif IS_ARM

/* ARM can be either endian. */
#if defined (__ARMEB__)
#define __FLASHROM_BIG_ENDIAN__ 1
#elif defined (__ARMEL__)
83 84 85
#define __FLASHROM_LITTLE_ENDIAN__ 1
#endif

Stefan Tauner's avatar
Stefan Tauner committed
86 87 88 89
#elif IS_SPARC
/* SPARC is big endian in general (but allows to access data in little endian too). */
#define __FLASHROM_BIG_ENDIAN__ 1

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
#endif /* IS_? */

#if !defined (__FLASHROM_BIG_ENDIAN__) && !defined (__FLASHROM_LITTLE_ENDIAN__)

/* If architecture-specific approaches fail try generic variants. First: BSD (works about everywhere). */
#if !IS_WINDOWS
#include <sys/param.h>

#if defined (__BYTE_ORDER)
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define __FLASHROM_LITTLE_ENDIAN__
#elif __BYTE_ORDER == __BIG_ENDIAN
#define __FLASHROM_BIG_ENDIAN__
#else
#error Unknown byte order!
105
#endif
106 107
#endif /* defined __BYTE_ORDER */
#endif /* !IS_WINDOWS */
108 109

#if !defined (__FLASHROM_BIG_ENDIAN__) && !defined (__FLASHROM_LITTLE_ENDIAN__)
110

111
/* Nonstandard libc-specific macros for determining endianness. */
112
/* musl provides an endian.h as well... but it can not be detected from within C. */
113 114 115 116 117 118 119 120 121 122
#if defined(__GLIBC__)
#include <endian.h>
#if BYTE_ORDER == LITTLE_ENDIAN
#define __FLASHROM_LITTLE_ENDIAN__ 1
#elif BYTE_ORDER == BIG_ENDIAN
#define __FLASHROM_BIG_ENDIAN__ 1
#endif
#endif
#endif

123 124
#endif

125
#if !defined (__FLASHROM_BIG_ENDIAN__) && !defined (__FLASHROM_LITTLE_ENDIAN__)
126
#error Unable to determine endianness.
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
#endif

#define ___constant_swab8(x) ((uint8_t) (				\
	(((uint8_t)(x) & (uint8_t)0xffU))))

#define ___constant_swab16(x) ((uint16_t) (				\
	(((uint16_t)(x) & (uint16_t)0x00ffU) << 8) |			\
	(((uint16_t)(x) & (uint16_t)0xff00U) >> 8)))

#define ___constant_swab32(x) ((uint32_t) (				\
	(((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) |		\
	(((uint32_t)(x) & (uint32_t)0x0000ff00UL) <<  8) |		\
	(((uint32_t)(x) & (uint32_t)0x00ff0000UL) >>  8) |		\
	(((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24)))

#define ___constant_swab64(x) ((uint64_t) (				\
	(((uint64_t)(x) & (uint64_t)0x00000000000000ffULL) << 56) |	\
	(((uint64_t)(x) & (uint64_t)0x000000000000ff00ULL) << 40) |	\
	(((uint64_t)(x) & (uint64_t)0x0000000000ff0000ULL) << 24) |	\
	(((uint64_t)(x) & (uint64_t)0x00000000ff000000ULL) <<  8) |	\
	(((uint64_t)(x) & (uint64_t)0x000000ff00000000ULL) >>  8) |	\
	(((uint64_t)(x) & (uint64_t)0x0000ff0000000000ULL) >> 24) |	\
	(((uint64_t)(x) & (uint64_t)0x00ff000000000000ULL) >> 40) |	\
	(((uint64_t)(x) & (uint64_t)0xff00000000000000ULL) >> 56)))

#if defined (__FLASHROM_BIG_ENDIAN__)

#define cpu_to_le(bits)							\
static inline uint##bits##_t cpu_to_le##bits(uint##bits##_t val)	\
{									\
	return ___constant_swab##bits(val);				\
}

cpu_to_le(8)
cpu_to_le(16)
cpu_to_le(32)
cpu_to_le(64)

#define cpu_to_be8
#define cpu_to_be16
#define cpu_to_be32
#define cpu_to_be64

#elif defined (__FLASHROM_LITTLE_ENDIAN__)

#define cpu_to_be(bits)							\
static inline uint##bits##_t cpu_to_be##bits(uint##bits##_t val)	\
{									\
	return ___constant_swab##bits(val);				\
}

cpu_to_be(8)
cpu_to_be(16)
cpu_to_be(32)
cpu_to_be(64)

#define cpu_to_le8
#define cpu_to_le16
#define cpu_to_le32
#define cpu_to_le64

188
#endif /* __FLASHROM_BIG_ENDIAN__ / __FLASHROM_LITTLE_ENDIAN__ */
189 190 191 192 193 194 195 196 197 198

#define be_to_cpu8 cpu_to_be8
#define be_to_cpu16 cpu_to_be16
#define be_to_cpu32 cpu_to_be32
#define be_to_cpu64 cpu_to_be64
#define le_to_cpu8 cpu_to_le8
#define le_to_cpu16 cpu_to_le16
#define le_to_cpu32 cpu_to_le32
#define le_to_cpu64 cpu_to_le64

199
#if NEED_PCI == 1
200
#if IS_X86
201

202 203 204 205 206 207 208
/* sys/io.h provides iopl(2) and x86 I/O port access functions (inb, outb etc).
 * It is included in glibc (thus available also on debian/kFreeBSD) but also in other libcs that mimic glibc,
 * e.g. musl and uclibc. */
#if defined(__linux__) || defined(__GLIBC__)
#include <sys/io.h>
#endif

209 210
#define __FLASHROM_HAVE_OUTB__ 1

211
/* for iopl and outb under Solaris */
212
#if defined (__sun)
213 214 215 216 217
#include <sys/sysi86.h>
#include <sys/psw.h>
#include <asm/sunddi.h>
#endif

Carl-Daniel Hailfinger's avatar
Carl-Daniel Hailfinger committed
218 219 220 221
/* Clarification about OUTB/OUTW/OUTL argument order:
 * OUT[BWL](val, port)
 */

222
#if defined(__FreeBSD__) || defined(__DragonFly__)
223 224 225 226
  /* Note that Debian/kFreeBSD (FreeBSD kernel with glibc) has conflicting
   * out[bwl] definitions in machine/cpufunc.h and sys/io.h at least in some
   * versions. Use machine/cpufunc.h only for plain FreeBSD/DragonFlyBSD.
   */
227
  #include <machine/cpufunc.h>
228 229 230 231 232 233
  #define OUTB(x, y) do { u_int outb_tmp = (y); outb(outb_tmp, (x)); } while (0)
  #define OUTW(x, y) do { u_int outw_tmp = (y); outw(outw_tmp, (x)); } while (0)
  #define OUTL(x, y) do { u_int outl_tmp = (y); outl(outl_tmp, (x)); } while (0)
  #define INB(x) __extension__ ({ u_int inb_tmp = (x); inb(inb_tmp); })
  #define INW(x) __extension__ ({ u_int inw_tmp = (x); inw(inw_tmp); })
  #define INL(x) __extension__ ({ u_int inl_tmp = (x); inl(inl_tmp); })
234
#else
235 236

#if defined (__sun)
237 238 239 240 241 242 243 244
  /* Note different order for outb */
  #define OUTB(x,y) outb(y, x)
  #define OUTW(x,y) outw(y, x)
  #define OUTL(x,y) outl(y, x)
  #define INB  inb
  #define INW  inw
  #define INL  inl
#else
245 246 247 248 249 250 251 252 253 254 255 256 257

#ifdef __DJGPP__

#include <pc.h>

  #define OUTB(x,y) outportb(y, x)
  #define OUTW(x,y) outportw(y, x)
  #define OUTL(x,y) outportl(y, x)

  #define INB  inportb
  #define INW  inportw
  #define INL  inportl

258
#else
259 260 261 262 263 264

#if defined(__MACH__) && defined(__APPLE__)
    /* Header is part of the DirectHW library. */
    #include <DirectHW/DirectHW.h>
#endif

265
  /* This is the usual glibc interface. */
266 267 268 269 270 271
  #define OUTB outb
  #define OUTW outw
  #define OUTL outl
  #define INB  inb
  #define INW  inw
  #define INL  inl
272
#endif
273
#endif
274 275
#endif

Carl-Daniel Hailfinger's avatar
Carl-Daniel Hailfinger committed
276
#if defined(__NetBSD__) || defined (__OpenBSD__)
Jonathan A. Kollasch's avatar
Jonathan A. Kollasch committed
277 278 279
  #if defined(__i386__) || defined(__x86_64__)
    #include <sys/types.h>
    #include <machine/sysarch.h>
280 281 282 283 284 285 286 287 288 289 290 291
    #if defined(__NetBSD__)
      #if defined(__i386__)
        #define iopl i386_iopl
      #elif defined(__x86_64__)
        #define iopl x86_64_iopl
      #endif
    #elif defined (__OpenBSD__)
      #if defined(__i386__)
        #define iopl i386_iopl
      #elif defined(__amd64__)
        #define iopl amd64_iopl
      #endif
Jonathan A. Kollasch's avatar
Jonathan A. Kollasch committed
292 293
    #endif

294
static inline void outb(uint8_t value, uint16_t port)
Jonathan A. Kollasch's avatar
Jonathan A. Kollasch committed
295
{
Carl-Daniel Hailfinger's avatar
Carl-Daniel Hailfinger committed
296
	__asm__ volatile ("outb %b0,%w1": :"a" (value), "Nd" (port));
Jonathan A. Kollasch's avatar
Jonathan A. Kollasch committed
297 298
}

299
static inline uint8_t inb(uint16_t port)
Jonathan A. Kollasch's avatar
Jonathan A. Kollasch committed
300 301
{
	uint8_t value;
Carl-Daniel Hailfinger's avatar
Carl-Daniel Hailfinger committed
302
	__asm__ volatile ("inb %w1,%0":"=a" (value):"Nd" (port));
Jonathan A. Kollasch's avatar
Jonathan A. Kollasch committed
303 304 305
	return value;
}

306
static inline void outw(uint16_t value, uint16_t port)
Jonathan A. Kollasch's avatar
Jonathan A. Kollasch committed
307
{
Carl-Daniel Hailfinger's avatar
Carl-Daniel Hailfinger committed
308
	__asm__ volatile ("outw %w0,%w1": :"a" (value), "Nd" (port));
Jonathan A. Kollasch's avatar
Jonathan A. Kollasch committed
309 310
}

311
static inline uint16_t inw(uint16_t port)
Jonathan A. Kollasch's avatar
Jonathan A. Kollasch committed
312 313
{
	uint16_t value;
Carl-Daniel Hailfinger's avatar
Carl-Daniel Hailfinger committed
314
	__asm__ volatile ("inw %w1,%0":"=a" (value):"Nd" (port));
Jonathan A. Kollasch's avatar
Jonathan A. Kollasch committed
315 316 317
	return value;
}

318
static inline void outl(uint32_t value, uint16_t port)
Jonathan A. Kollasch's avatar
Jonathan A. Kollasch committed
319
{
Carl-Daniel Hailfinger's avatar
Carl-Daniel Hailfinger committed
320
	__asm__ volatile ("outl %0,%w1": :"a" (value), "Nd" (port));
Jonathan A. Kollasch's avatar
Jonathan A. Kollasch committed
321 322
}

323
static inline uint32_t inl(uint16_t port)
Jonathan A. Kollasch's avatar
Jonathan A. Kollasch committed
324 325
{
	uint32_t value;
Carl-Daniel Hailfinger's avatar
Carl-Daniel Hailfinger committed
326
	__asm__ volatile ("inl %1,%0":"=a" (value):"Nd" (port));
Jonathan A. Kollasch's avatar
Jonathan A. Kollasch committed
327 328 329 330 331
	return value;
}
  #endif
#endif

332
#if !(defined(__MACH__) && defined(__APPLE__)) && !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && !defined(__DragonFly__) && !defined(__LIBPAYLOAD__)
333 334 335 336
typedef struct { uint32_t hi, lo; } msr_t;
msr_t rdmsr(int addr);
int wrmsr(int addr, msr_t msr);
#endif
337
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
338 339 340 341 342 343 344 345 346
/* FreeBSD already has conflicting definitions for wrmsr/rdmsr. */
#undef rdmsr
#undef wrmsr
#define rdmsr freebsd_rdmsr
#define wrmsr freebsd_wrmsr
typedef struct { uint32_t hi, lo; } msr_t;
msr_t freebsd_rdmsr(int addr);
int freebsd_wrmsr(int addr, msr_t msr);
#endif
347 348 349 350 351 352 353 354 355 356
#if defined(__LIBPAYLOAD__)
#include <arch/io.h>
#include <arch/msr.h>
typedef struct { uint32_t hi, lo; } msr_t;
msr_t libpayload_rdmsr(int addr);
int libpayload_wrmsr(int addr, msr_t msr);
#undef rdmsr
#define rdmsr libpayload_rdmsr
#define wrmsr libpayload_wrmsr
#endif
357

358
#elif IS_PPC
359 360 361

/* PCI port I/O is not yet implemented on PowerPC. */

362
#elif IS_MIPS
363 364 365

/* PCI port I/O is not yet implemented on MIPS. */

Stefan Tauner's avatar
Stefan Tauner committed
366 367 368 369
#elif IS_SPARC

/* PCI port I/O is not yet implemented on SPARC. */

370
#elif IS_ARM
371 372 373

/* Non memory mapped I/O is not supported on ARM. */

374 375 376 377
#else

#error Unknown architecture, please check if it supports PCI port IO.

378 379
#endif /* IS_* */
#endif /* NEED_PCI == 1 */
380

381
#endif /* !__HWACCESS_H__ */