blob: 12496bb0b6fd25d85fb1c920b2ca8f5e41816c78 [file] [log] [blame]
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +00001/*
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 Hermann43959702010-03-13 17:28:29 +000018 */
19
20/*
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +000021 * Header file for hardware access and OS abstraction. Included from flash.h.
22 */
23
24#ifndef __HWACCESS_H__
25#define __HWACCESS_H__ 1
26
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +000027#if defined (__i386__) || defined (__x86_64__)
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +000028#if defined(__GLIBC__)
29#include <sys/io.h>
30#endif
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +000031#endif
32
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +000033#if NEED_PCI == 1
Carl-Daniel Hailfinger72376832010-06-25 13:18:48 +000034/*
35 * libpci headers use the variable name "index" which triggers shadowing
36 * warnings on systems which have the index() function in a default #include
37 * or as builtin.
38 */
39#define index shadow_workaround_index
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +000040#include <pci/pci.h>
Carl-Daniel Hailfinger72376832010-06-25 13:18:48 +000041#undef index
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +000042#endif
43
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +000044#if defined (__i386__) || defined (__x86_64__)
45
46/* All x86 is little-endian. */
47#define __FLASHROM_LITTLE_ENDIAN__ 1
48
49#elif defined (__mips) || defined (__mips__) || defined (_mips) || defined (mips)
50
51/* MIPS can be either endian. */
52#if defined (__MIPSEL) || defined (__MIPSEL__) || defined (_MIPSEL) || defined (MIPSEL)
53#define __FLASHROM_LITTLE_ENDIAN__ 1
54#elif defined (__MIPSEB) || defined (__MIPSEB__) || defined (_MIPSEB) || defined (MIPSEB)
55#define __FLASHROM_BIG_ENDIAN__ 1
56#endif
57
58#elif defined(__powerpc__) || defined(__powerpc64__) || defined(__ppc__) || defined(__ppc64__)
59
60/* PowerPC can be either endian. */
61#if defined (_BIG_ENDIAN) || defined (__BIG_ENDIAN__)
62#define __FLASHROM_BIG_ENDIAN__ 1
63/* Error checking in case some weird header has #defines for LE as well. */
64#if defined (_LITTLE_ENDIAN) || defined (__LITTLE_ENDIAN__)
65#error Conflicting endianness #define
66#endif
67#else
68#error Little-endian PowerPC #defines are unknown
69#endif
70
David Hendricksb286da72012-02-13 00:35:35 +000071#elif defined (__arm__)
72#if defined (__ARMEL__)
73#define __FLASHROM_LITTLE_ENDIAN__ 1
74#else
75#error Big-endian ARM #defines are unknown
76#endif
77
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +000078#endif
79
80#if !defined (__FLASHROM_BIG_ENDIAN__) && !defined (__FLASHROM_LITTLE_ENDIAN__)
81/* Nonstandard libc-specific macros for determining endianness. */
82#if defined(__GLIBC__)
83#include <endian.h>
84#if BYTE_ORDER == LITTLE_ENDIAN
85#define __FLASHROM_LITTLE_ENDIAN__ 1
86#elif BYTE_ORDER == BIG_ENDIAN
87#define __FLASHROM_BIG_ENDIAN__ 1
88#endif
89#endif
90#endif
91
92#if !defined (__FLASHROM_BIG_ENDIAN__) && !defined (__FLASHROM_LITTLE_ENDIAN__)
93#error Unable to determine endianness. Please add support for your arch or libc.
94#endif
95
96#define ___constant_swab8(x) ((uint8_t) ( \
97 (((uint8_t)(x) & (uint8_t)0xffU))))
98
99#define ___constant_swab16(x) ((uint16_t) ( \
100 (((uint16_t)(x) & (uint16_t)0x00ffU) << 8) | \
101 (((uint16_t)(x) & (uint16_t)0xff00U) >> 8)))
102
103#define ___constant_swab32(x) ((uint32_t) ( \
104 (((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) | \
105 (((uint32_t)(x) & (uint32_t)0x0000ff00UL) << 8) | \
106 (((uint32_t)(x) & (uint32_t)0x00ff0000UL) >> 8) | \
107 (((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24)))
108
109#define ___constant_swab64(x) ((uint64_t) ( \
110 (((uint64_t)(x) & (uint64_t)0x00000000000000ffULL) << 56) | \
111 (((uint64_t)(x) & (uint64_t)0x000000000000ff00ULL) << 40) | \
112 (((uint64_t)(x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \
113 (((uint64_t)(x) & (uint64_t)0x00000000ff000000ULL) << 8) | \
114 (((uint64_t)(x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \
115 (((uint64_t)(x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
116 (((uint64_t)(x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
117 (((uint64_t)(x) & (uint64_t)0xff00000000000000ULL) >> 56)))
118
119#if defined (__FLASHROM_BIG_ENDIAN__)
120
121#define cpu_to_le(bits) \
122static inline uint##bits##_t cpu_to_le##bits(uint##bits##_t val) \
123{ \
124 return ___constant_swab##bits(val); \
125}
126
127cpu_to_le(8)
128cpu_to_le(16)
129cpu_to_le(32)
130cpu_to_le(64)
131
132#define cpu_to_be8
133#define cpu_to_be16
134#define cpu_to_be32
135#define cpu_to_be64
136
137#elif defined (__FLASHROM_LITTLE_ENDIAN__)
138
139#define cpu_to_be(bits) \
140static inline uint##bits##_t cpu_to_be##bits(uint##bits##_t val) \
141{ \
142 return ___constant_swab##bits(val); \
143}
144
145cpu_to_be(8)
146cpu_to_be(16)
147cpu_to_be(32)
148cpu_to_be(64)
149
150#define cpu_to_le8
151#define cpu_to_le16
152#define cpu_to_le32
153#define cpu_to_le64
154
155#else
156
157#error Could not determine endianness.
158
159#endif
160
161#define be_to_cpu8 cpu_to_be8
162#define be_to_cpu16 cpu_to_be16
163#define be_to_cpu32 cpu_to_be32
164#define be_to_cpu64 cpu_to_be64
165#define le_to_cpu8 cpu_to_le8
166#define le_to_cpu16 cpu_to_le16
167#define le_to_cpu32 cpu_to_le32
168#define le_to_cpu64 cpu_to_le64
169
Carl-Daniel Hailfinger41bea032010-07-29 13:17:37 +0000170#if NEED_PCI == 1
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000171#if defined (__i386__) || defined (__x86_64__)
172
173#define __FLASHROM_HAVE_OUTB__ 1
174
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +0000175/* for iopl and outb under Solaris */
176#if defined (__sun) && (defined(__i386) || defined(__amd64))
177#include <strings.h>
178#include <sys/sysi86.h>
179#include <sys/psw.h>
180#include <asm/sunddi.h>
181#endif
182
Carl-Daniel Hailfinger0d974e72010-07-17 12:54:09 +0000183/* Clarification about OUTB/OUTW/OUTL argument order:
184 * OUT[BWL](val, port)
185 */
186
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +0000187#if defined(__FreeBSD__) || defined(__DragonFly__)
Carl-Daniel Hailfingera5eecda2012-02-25 22:50:21 +0000188 /* Note that Debian/kFreeBSD (FreeBSD kernel with glibc) has conflicting
189 * out[bwl] definitions in machine/cpufunc.h and sys/io.h at least in some
190 * versions. Use machine/cpufunc.h only for plain FreeBSD/DragonFlyBSD.
191 */
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +0000192 #include <machine/cpufunc.h>
193 #define off64_t off_t
194 #define lseek64 lseek
Michael Karchere7f32092010-01-12 15:36:24 +0000195 #define OUTB(x, y) do { u_int outb_tmp = (y); outb(outb_tmp, (x)); } while (0)
196 #define OUTW(x, y) do { u_int outw_tmp = (y); outw(outw_tmp, (x)); } while (0)
197 #define OUTL(x, y) do { u_int outl_tmp = (y); outl(outl_tmp, (x)); } while (0)
198 #define INB(x) __extension__ ({ u_int inb_tmp = (x); inb(inb_tmp); })
199 #define INW(x) __extension__ ({ u_int inw_tmp = (x); inw(inw_tmp); })
200 #define INL(x) __extension__ ({ u_int inl_tmp = (x); inl(inl_tmp); })
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +0000201#else
Carl-Daniel Hailfinger60d9bd22012-08-09 23:34:41 +0000202#if defined(__MACH__) && defined(__APPLE__)
Carl-Daniel Hailfingerf992c192010-10-06 23:16:10 +0000203 /* Header is part of the DirectHW library. */
Stefan Reinauer83704c52011-03-18 22:00:15 +0000204 #include <DirectHW/DirectHW.h>
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +0000205 #define off64_t off_t
206 #define lseek64 lseek
207#endif
208#if defined (__sun) && (defined(__i386) || defined(__amd64))
209 /* Note different order for outb */
210 #define OUTB(x,y) outb(y, x)
211 #define OUTW(x,y) outw(y, x)
212 #define OUTL(x,y) outl(y, x)
213 #define INB inb
214 #define INW inw
215 #define INL inl
216#else
Rudolf Marek03ae5c12010-03-16 23:59:19 +0000217
218#ifdef __DJGPP__
219
220#include <pc.h>
221
222 #define OUTB(x,y) outportb(y, x)
223 #define OUTW(x,y) outportw(y, x)
224 #define OUTL(x,y) outportl(y, x)
225
226 #define INB inportb
227 #define INW inportw
228 #define INL inportl
229
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000230#else
Carl-Daniel Hailfingera5eecda2012-02-25 22:50:21 +0000231 /* This is the usual glibc interface. */
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +0000232 #define OUTB outb
233 #define OUTW outw
234 #define OUTL outl
235 #define INB inb
236 #define INW inw
237 #define INL inl
Rudolf Marek03ae5c12010-03-16 23:59:19 +0000238
239#endif
240
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +0000241#endif
242#endif
243
Carl-Daniel Hailfingerb63b0672010-07-02 17:12:50 +0000244#if defined(__NetBSD__) || defined (__OpenBSD__)
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +0000245 #define off64_t off_t
246 #define lseek64 lseek
247 #if defined(__i386__) || defined(__x86_64__)
248 #include <sys/types.h>
249 #include <machine/sysarch.h>
Carl-Daniel Hailfingerb63b0672010-07-02 17:12:50 +0000250#if defined(__NetBSD__)
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +0000251 #if defined(__i386__)
252 #define iopl i386_iopl
253 #elif defined(__x86_64__)
254 #define iopl x86_64_iopl
255 #endif
Carl-Daniel Hailfingerb63b0672010-07-02 17:12:50 +0000256#elif defined (__OpenBSD__)
257 #if defined(__i386__)
258 #define iopl i386_iopl
259 #elif defined(__amd64__)
260 #define iopl amd64_iopl
261 #endif
262#endif
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +0000263
Uwe Hermann43959702010-03-13 17:28:29 +0000264static inline void outb(uint8_t value, uint16_t port)
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +0000265{
266 asm volatile ("outb %b0,%w1": :"a" (value), "Nd" (port));
267}
268
Uwe Hermann43959702010-03-13 17:28:29 +0000269static inline uint8_t inb(uint16_t port)
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +0000270{
271 uint8_t value;
272 asm volatile ("inb %w1,%0":"=a" (value):"Nd" (port));
273 return value;
274}
275
Uwe Hermann43959702010-03-13 17:28:29 +0000276static inline void outw(uint16_t value, uint16_t port)
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +0000277{
278 asm volatile ("outw %w0,%w1": :"a" (value), "Nd" (port));
279}
280
Uwe Hermann43959702010-03-13 17:28:29 +0000281static inline uint16_t inw(uint16_t port)
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +0000282{
283 uint16_t value;
284 asm volatile ("inw %w1,%0":"=a" (value):"Nd" (port));
285 return value;
286}
287
Uwe Hermann43959702010-03-13 17:28:29 +0000288static inline void outl(uint32_t value, uint16_t port)
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +0000289{
290 asm volatile ("outl %0,%w1": :"a" (value), "Nd" (port));
291}
292
Uwe Hermann43959702010-03-13 17:28:29 +0000293static inline uint32_t inl(uint16_t port)
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +0000294{
295 uint32_t value;
296 asm volatile ("inl %1,%0":"=a" (value):"Nd" (port));
297 return value;
298}
299 #endif
300#endif
301
Carl-Daniel Hailfinger60d9bd22012-08-09 23:34:41 +0000302#if !(defined(__MACH__) && defined(__APPLE__)) && !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && !defined(__DragonFly__) && !defined(__LIBPAYLOAD__)
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +0000303typedef struct { uint32_t hi, lo; } msr_t;
304msr_t rdmsr(int addr);
305int wrmsr(int addr, msr_t msr);
306#endif
Carl-Daniel Hailfingera5eecda2012-02-25 22:50:21 +0000307#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +0000308/* FreeBSD already has conflicting definitions for wrmsr/rdmsr. */
309#undef rdmsr
310#undef wrmsr
311#define rdmsr freebsd_rdmsr
312#define wrmsr freebsd_wrmsr
313typedef struct { uint32_t hi, lo; } msr_t;
314msr_t freebsd_rdmsr(int addr);
315int freebsd_wrmsr(int addr, msr_t msr);
316#endif
Patrick Georgia9095a92010-09-30 17:03:32 +0000317#if defined(__LIBPAYLOAD__)
318#include <arch/io.h>
319#include <arch/msr.h>
320typedef struct { uint32_t hi, lo; } msr_t;
321msr_t libpayload_rdmsr(int addr);
322int libpayload_wrmsr(int addr, msr_t msr);
323#undef rdmsr
324#define rdmsr libpayload_rdmsr
325#define wrmsr libpayload_wrmsr
326#endif
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +0000327
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000328#elif defined(__powerpc__) || defined(__powerpc64__) || defined(__ppc__) || defined(__ppc64__)
329
330/* PCI port I/O is not yet implemented on PowerPC. */
331
332#elif defined (__mips) || defined (__mips__) || defined (_mips) || defined (mips)
333
334/* PCI port I/O is not yet implemented on MIPS. */
335
David Hendricksb286da72012-02-13 00:35:35 +0000336#elif defined(__arm__)
337
338/* Non memory mapped I/O is not supported on ARM. */
339
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000340#else
341
342#error Unknown architecture, please check if it supports PCI port IO.
343
344#endif
Carl-Daniel Hailfinger41bea032010-07-29 13:17:37 +0000345#endif
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000346
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +0000347#endif /* !__HWACCESS_H__ */