blob: b96827160bb5fee249ffc4c69e275d05aa3c0905 [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.
Uwe Hermann43959702010-03-13 17:28:29 +000014 */
15
16/*
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +000017 * Header file for hardware access and OS abstraction. Included from flash.h.
18 */
19
20#ifndef __HWACCESS_H__
21#define __HWACCESS_H__ 1
22
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000023#include "platform.h"
24
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +000025#if NEED_PCI == 1
Carl-Daniel Hailfinger72376832010-06-25 13:18:48 +000026/*
27 * libpci headers use the variable name "index" which triggers shadowing
28 * warnings on systems which have the index() function in a default #include
29 * or as builtin.
30 */
31#define index shadow_workaround_index
Stefan Taunerc65b8552013-09-12 15:48:39 +000032
Nico Huberc0e1c4b2022-12-13 21:55:22 +000033#if !defined (PCIUTILS_PCI_H)
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +000034#include <pci/pci.h>
Stefan Taunerc65b8552013-09-12 15:48:39 +000035#else
36#include <pciutils/pci.h>
37#endif
38
Carl-Daniel Hailfinger72376832010-06-25 13:18:48 +000039#undef index
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000040#endif /* NEED_PCI == 1 */
41
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +000042#define ___constant_swab8(x) ((uint8_t) ( \
43 (((uint8_t)(x) & (uint8_t)0xffU))))
44
45#define ___constant_swab16(x) ((uint16_t) ( \
46 (((uint16_t)(x) & (uint16_t)0x00ffU) << 8) | \
47 (((uint16_t)(x) & (uint16_t)0xff00U) >> 8)))
48
49#define ___constant_swab32(x) ((uint32_t) ( \
50 (((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) | \
51 (((uint32_t)(x) & (uint32_t)0x0000ff00UL) << 8) | \
52 (((uint32_t)(x) & (uint32_t)0x00ff0000UL) >> 8) | \
53 (((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24)))
54
55#define ___constant_swab64(x) ((uint64_t) ( \
56 (((uint64_t)(x) & (uint64_t)0x00000000000000ffULL) << 56) | \
57 (((uint64_t)(x) & (uint64_t)0x000000000000ff00ULL) << 40) | \
58 (((uint64_t)(x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \
59 (((uint64_t)(x) & (uint64_t)0x00000000ff000000ULL) << 8) | \
60 (((uint64_t)(x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \
61 (((uint64_t)(x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
62 (((uint64_t)(x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
63 (((uint64_t)(x) & (uint64_t)0xff00000000000000ULL) >> 56)))
64
65#if defined (__FLASHROM_BIG_ENDIAN__)
66
67#define cpu_to_le(bits) \
68static inline uint##bits##_t cpu_to_le##bits(uint##bits##_t val) \
69{ \
70 return ___constant_swab##bits(val); \
71}
72
73cpu_to_le(8)
74cpu_to_le(16)
75cpu_to_le(32)
76cpu_to_le(64)
77
78#define cpu_to_be8
79#define cpu_to_be16
80#define cpu_to_be32
81#define cpu_to_be64
82
83#elif defined (__FLASHROM_LITTLE_ENDIAN__)
84
85#define cpu_to_be(bits) \
86static inline uint##bits##_t cpu_to_be##bits(uint##bits##_t val) \
87{ \
88 return ___constant_swab##bits(val); \
89}
90
91cpu_to_be(8)
92cpu_to_be(16)
93cpu_to_be(32)
94cpu_to_be(64)
95
96#define cpu_to_le8
97#define cpu_to_le16
98#define cpu_to_le32
99#define cpu_to_le64
100
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000101#endif /* __FLASHROM_BIG_ENDIAN__ / __FLASHROM_LITTLE_ENDIAN__ */
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000102
103#define be_to_cpu8 cpu_to_be8
104#define be_to_cpu16 cpu_to_be16
105#define be_to_cpu32 cpu_to_be32
106#define be_to_cpu64 cpu_to_be64
107#define le_to_cpu8 cpu_to_le8
108#define le_to_cpu16 cpu_to_le16
109#define le_to_cpu32 cpu_to_le32
110#define le_to_cpu64 cpu_to_le64
111
Carl-Daniel Hailfinger16c0aec2016-02-20 21:43:56 +0000112#if NEED_RAW_ACCESS == 1
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000113#if IS_X86
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000114
Gwenhael Goavec-Merou8cd0c732015-11-14 02:55:12 +0000115/* sys/io.h provides iopl(2) and x86 I/O port access functions (inb, outb etc).
116 * It is included in glibc (thus available also on debian/kFreeBSD) but also in other libcs that mimic glibc,
Stefan Tauner2c57bbe2016-02-20 20:21:58 +0000117 * e.g. musl and uclibc. Because we cannot detect the libc or existence of the header or of the instructions
118 * themselves safely in here we use some heuristic below:
119 * On Android we don't have the header file and no way for I/O port access at all. However, sys/glibc-syscalls.h
120 * refers to an iopl implementation and we therefore include at least that one for now. On non-Android we assume
121 * that a Linux system's libc has a suitable sys/io.h or (on non-Linux) we depend on glibc to offer it. */
122#if defined(__ANDROID__)
123#include <sys/glibc-syscalls.h>
124#elif defined(__linux__) || defined(__GLIBC__)
Gwenhael Goavec-Merou8cd0c732015-11-14 02:55:12 +0000125#include <sys/io.h>
126#endif
127
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000128#define __FLASHROM_HAVE_OUTB__ 1
129
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +0000130/* for iopl and outb under Solaris */
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000131#if defined (__sun)
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +0000132#include <sys/sysi86.h>
133#include <sys/psw.h>
134#include <asm/sunddi.h>
135#endif
136
Carl-Daniel Hailfinger0d974e72010-07-17 12:54:09 +0000137/* Clarification about OUTB/OUTW/OUTL argument order:
138 * OUT[BWL](val, port)
139 */
140
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +0000141#if defined(__FreeBSD__) || defined(__DragonFly__)
Carl-Daniel Hailfingera5eecda2012-02-25 22:50:21 +0000142 /* Note that Debian/kFreeBSD (FreeBSD kernel with glibc) has conflicting
143 * out[bwl] definitions in machine/cpufunc.h and sys/io.h at least in some
144 * versions. Use machine/cpufunc.h only for plain FreeBSD/DragonFlyBSD.
145 */
Carl-Daniel Hailfinger16c0aec2016-02-20 21:43:56 +0000146 #include <sys/types.h>
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +0000147 #include <machine/cpufunc.h>
Michael Karchere7f32092010-01-12 15:36:24 +0000148 #define OUTB(x, y) do { u_int outb_tmp = (y); outb(outb_tmp, (x)); } while (0)
149 #define OUTW(x, y) do { u_int outw_tmp = (y); outw(outw_tmp, (x)); } while (0)
150 #define OUTL(x, y) do { u_int outl_tmp = (y); outl(outl_tmp, (x)); } while (0)
151 #define INB(x) __extension__ ({ u_int inb_tmp = (x); inb(inb_tmp); })
152 #define INW(x) __extension__ ({ u_int inw_tmp = (x); inw(inw_tmp); })
153 #define INL(x) __extension__ ({ u_int inl_tmp = (x); inl(inl_tmp); })
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +0000154#else
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000155
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000156#if defined (__sun)
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +0000157 /* Note different order for outb */
158 #define OUTB(x,y) outb(y, x)
159 #define OUTW(x,y) outw(y, x)
160 #define OUTL(x,y) outl(y, x)
161 #define INB inb
162 #define INW inw
163 #define INL inl
164#else
Rudolf Marek03ae5c12010-03-16 23:59:19 +0000165
166#ifdef __DJGPP__
167
168#include <pc.h>
169
170 #define OUTB(x,y) outportb(y, x)
171 #define OUTW(x,y) outportw(y, x)
172 #define OUTL(x,y) outportl(y, x)
173
174 #define INB inportb
175 #define INW inportw
176 #define INL inportl
177
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000178#else
Stefan Taunerc71759d2015-05-23 23:59:23 +0000179
180#if defined(__MACH__) && defined(__APPLE__)
181 /* Header is part of the DirectHW library. */
182 #include <DirectHW/DirectHW.h>
183#endif
184
Carl-Daniel Hailfingera5eecda2012-02-25 22:50:21 +0000185 /* This is the usual glibc interface. */
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +0000186 #define OUTB outb
187 #define OUTW outw
188 #define OUTL outl
189 #define INB inb
190 #define INW inw
191 #define INL inl
Rudolf Marek03ae5c12010-03-16 23:59:19 +0000192#endif
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000193#endif
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +0000194#endif
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +0000195
Carl-Daniel Hailfingerb63b0672010-07-02 17:12:50 +0000196#if defined(__NetBSD__) || defined (__OpenBSD__)
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +0000197 #if defined(__i386__) || defined(__x86_64__)
198 #include <sys/types.h>
199 #include <machine/sysarch.h>
Stefan Tauner0554ca52013-07-25 22:54:25 +0000200 #if defined(__NetBSD__)
201 #if defined(__i386__)
202 #define iopl i386_iopl
203 #elif defined(__x86_64__)
204 #define iopl x86_64_iopl
205 #endif
206 #elif defined (__OpenBSD__)
207 #if defined(__i386__)
208 #define iopl i386_iopl
209 #elif defined(__amd64__)
210 #define iopl amd64_iopl
211 #endif
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +0000212 #endif
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +0000213
Uwe Hermann43959702010-03-13 17:28:29 +0000214static inline void outb(uint8_t value, uint16_t port)
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +0000215{
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +0000216 __asm__ volatile ("outb %b0,%w1": :"a" (value), "Nd" (port));
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +0000217}
218
Uwe Hermann43959702010-03-13 17:28:29 +0000219static inline uint8_t inb(uint16_t port)
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +0000220{
221 uint8_t value;
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +0000222 __asm__ volatile ("inb %w1,%0":"=a" (value):"Nd" (port));
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +0000223 return value;
224}
225
Uwe Hermann43959702010-03-13 17:28:29 +0000226static inline void outw(uint16_t value, uint16_t port)
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +0000227{
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +0000228 __asm__ volatile ("outw %w0,%w1": :"a" (value), "Nd" (port));
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +0000229}
230
Uwe Hermann43959702010-03-13 17:28:29 +0000231static inline uint16_t inw(uint16_t port)
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +0000232{
233 uint16_t value;
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +0000234 __asm__ volatile ("inw %w1,%0":"=a" (value):"Nd" (port));
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +0000235 return value;
236}
237
Uwe Hermann43959702010-03-13 17:28:29 +0000238static inline void outl(uint32_t value, uint16_t port)
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +0000239{
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +0000240 __asm__ volatile ("outl %0,%w1": :"a" (value), "Nd" (port));
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +0000241}
242
Uwe Hermann43959702010-03-13 17:28:29 +0000243static inline uint32_t inl(uint16_t port)
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +0000244{
245 uint32_t value;
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +0000246 __asm__ volatile ("inl %1,%0":"=a" (value):"Nd" (port));
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +0000247 return value;
248}
249 #endif
250#endif
251
Carl-Daniel Hailfinger60d9bd22012-08-09 23:34:41 +0000252#if !(defined(__MACH__) && defined(__APPLE__)) && !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && !defined(__DragonFly__) && !defined(__LIBPAYLOAD__)
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +0000253typedef struct { uint32_t hi, lo; } msr_t;
254msr_t rdmsr(int addr);
255int wrmsr(int addr, msr_t msr);
256#endif
Carl-Daniel Hailfingera5eecda2012-02-25 22:50:21 +0000257#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +0000258/* FreeBSD already has conflicting definitions for wrmsr/rdmsr. */
259#undef rdmsr
260#undef wrmsr
261#define rdmsr freebsd_rdmsr
262#define wrmsr freebsd_wrmsr
263typedef struct { uint32_t hi, lo; } msr_t;
264msr_t freebsd_rdmsr(int addr);
265int freebsd_wrmsr(int addr, msr_t msr);
266#endif
Patrick Georgia9095a92010-09-30 17:03:32 +0000267#if defined(__LIBPAYLOAD__)
268#include <arch/io.h>
269#include <arch/msr.h>
270typedef struct { uint32_t hi, lo; } msr_t;
271msr_t libpayload_rdmsr(int addr);
272int libpayload_wrmsr(int addr, msr_t msr);
273#undef rdmsr
274#define rdmsr libpayload_rdmsr
275#define wrmsr libpayload_wrmsr
276#endif
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +0000277
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000278#elif IS_PPC
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000279
280/* PCI port I/O is not yet implemented on PowerPC. */
281
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000282#elif IS_MIPS
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000283
284/* PCI port I/O is not yet implemented on MIPS. */
285
Stefan Taunerfb2d77c2015-02-10 08:03:10 +0000286#elif IS_SPARC
287
288/* PCI port I/O is not yet implemented on SPARC. */
289
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000290#elif IS_ARM
David Hendricksb286da72012-02-13 00:35:35 +0000291
292/* Non memory mapped I/O is not supported on ARM. */
293
Rosen Penev34d07f02019-07-02 00:14:01 -0700294#elif IS_ARC
295
296/* Non memory mapped I/O is not supported on ARC. */
297
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000298#else
299
300#error Unknown architecture, please check if it supports PCI port IO.
301
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000302#endif /* IS_* */
Carl-Daniel Hailfinger16c0aec2016-02-20 21:43:56 +0000303#endif /* NEED_RAW_ACCESS == 1 */
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000304
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +0000305#endif /* !__HWACCESS_H__ */