blob: 63692b8dd5b164affa10d0781ad3d112d204b0b4 [file] [log] [blame]
Anastasia Klimchuk3f19ba92021-04-27 16:13:32 +10001/*
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
16#ifndef __HWACCESS_X86_IO_H__
17#define __HWACCESS_X86_IO_H__ 1
18
19/* sys/io.h provides iopl(2) and x86 I/O port access functions (inb, outb etc).
20 * It is included in glibc (thus available also on debian/kFreeBSD) but also in other libcs that mimic glibc,
21 * e.g. musl and uclibc. Because we cannot detect the libc or existence of the header or of the instructions
22 * themselves safely in here we use some heuristic below:
23 * On Android we don't have the header file and no way for I/O port access at all. However, sys/glibc-syscalls.h
24 * refers to an iopl implementation and we therefore include at least that one for now. On non-Android we assume
25 * that a Linux system's libc has a suitable sys/io.h or (on non-Linux) we depend on glibc to offer it. */
26#if defined(__ANDROID__)
27#include <sys/glibc-syscalls.h>
28#elif defined(__linux__) || defined(__GLIBC__)
29#include <sys/io.h>
30#endif
31
32#define __FLASHROM_HAVE_OUTB__ 1
33
34/* for iopl and outb under Solaris */
35#if defined (__sun)
36#include <sys/sysi86.h>
37#include <sys/psw.h>
38#include <asm/sunddi.h>
39#endif
40
41/* Clarification about OUTB/OUTW/OUTL argument order:
42 * OUT[BWL](val, port)
43 */
44
45#if defined(__FreeBSD__) || defined(__DragonFly__)
46 /* Note that Debian/kFreeBSD (FreeBSD kernel with glibc) has conflicting
47 * out[bwl] definitions in machine/cpufunc.h and sys/io.h at least in some
48 * versions. Use machine/cpufunc.h only for plain FreeBSD/DragonFlyBSD.
49 */
50 #include <sys/types.h>
51 #include <machine/cpufunc.h>
52 #define OUTB(x, y) do { u_int outb_tmp = (y); outb(outb_tmp, (x)); } while (0)
53 #define OUTW(x, y) do { u_int outw_tmp = (y); outw(outw_tmp, (x)); } while (0)
54 #define OUTL(x, y) do { u_int outl_tmp = (y); outl(outl_tmp, (x)); } while (0)
55 #define INB(x) __extension__ ({ u_int inb_tmp = (x); inb(inb_tmp); })
56 #define INW(x) __extension__ ({ u_int inw_tmp = (x); inw(inw_tmp); })
57 #define INL(x) __extension__ ({ u_int inl_tmp = (x); inl(inl_tmp); })
58#else
59
60#if defined (__sun)
61 /* Note different order for outb */
62 #define OUTB(x,y) outb(y, x)
63 #define OUTW(x,y) outw(y, x)
64 #define OUTL(x,y) outl(y, x)
65 #define INB inb
66 #define INW inw
67 #define INL inl
68#else
69
70#ifdef __DJGPP__
71
72#include <pc.h>
73
74 #define OUTB(x,y) outportb(y, x)
75 #define OUTW(x,y) outportw(y, x)
76 #define OUTL(x,y) outportl(y, x)
77
78 #define INB inportb
79 #define INW inportw
80 #define INL inportl
81
82#else
83
84#if defined(__MACH__) && defined(__APPLE__)
85 /* Header is part of the DirectHW library. */
86 #include <DirectHW/DirectHW.h>
87#endif
88
89 /* This is the usual glibc interface. */
90 #define OUTB outb
91 #define OUTW outw
92 #define OUTL outl
93 #define INB inb
94 #define INW inw
95 #define INL inl
96#endif
97#endif
98#endif
99
100#if defined(__NetBSD__) || defined (__OpenBSD__)
101 #if defined(__i386__) || defined(__x86_64__)
102 #include <sys/types.h>
103 #include <machine/sysarch.h>
104 #if defined(__NetBSD__)
105 #if defined(__i386__)
106 #define iopl i386_iopl
107 #elif defined(__x86_64__)
108 #define iopl x86_64_iopl
109 #endif
110 #elif defined (__OpenBSD__)
111 #if defined(__i386__)
112 #define iopl i386_iopl
113 #elif defined(__amd64__)
114 #define iopl amd64_iopl
115 #endif
116 #endif
117
118static inline void outb(uint8_t value, uint16_t port)
119{
120 __asm__ volatile ("outb %b0,%w1": :"a" (value), "Nd" (port));
121}
122
123static inline uint8_t inb(uint16_t port)
124{
125 uint8_t value;
126 __asm__ volatile ("inb %w1,%0":"=a" (value):"Nd" (port));
127 return value;
128}
129
130static inline void outw(uint16_t value, uint16_t port)
131{
132 __asm__ volatile ("outw %w0,%w1": :"a" (value), "Nd" (port));
133}
134
135static inline uint16_t inw(uint16_t port)
136{
137 uint16_t value;
138 __asm__ volatile ("inw %w1,%0":"=a" (value):"Nd" (port));
139 return value;
140}
141
142static inline void outl(uint32_t value, uint16_t port)
143{
144 __asm__ volatile ("outl %0,%w1": :"a" (value), "Nd" (port));
145}
146
147static inline uint32_t inl(uint16_t port)
148{
149 uint32_t value;
150 __asm__ volatile ("inl %1,%0":"=a" (value):"Nd" (port));
151 return value;
152}
153 #endif
154#endif
155
156#endif /* __HWACCESS_X86_IO_H__ */