pio.h revision 1.11.2.1 1 /* $NetBSD: pio.h,v 1.11.2.1 1996/03/01 18:09:02 scottr Exp $ */
2
3 /*
4 * Copyright (c) 1993, 1995 Charles M. Hannum. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Charles M. Hannum.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Functions to provide access to i386 programmed I/O instructions.
34 *
35 * The in[bwl]() and out[bwl]() functions are split into two varieties: one to
36 * use a small, constant, 8-bit port number, and another to use a large or
37 * variable port number. The former can be compiled as a smaller instruction.
38 */
39
40
41 #ifdef __OPTIMIZE__
42
43 #define __use_immediate_port(port) \
44 (__builtin_constant_p((port)) && (port) < 0x100)
45
46 #else
47
48 #define __use_immediate_port(port) 0
49
50 #endif
51
52
53 #define inb(port) \
54 (__use_immediate_port(port) ? __inbc(port) : __inb(port))
55
56 static __inline u_char
57 __inbc(int port)
58 {
59 u_char data;
60 __asm __volatile("inb %1,%0" : "=a" (data) : "id" (port));
61 return data;
62 }
63
64 static __inline u_char
65 __inb(int port)
66 {
67 u_char data;
68 __asm __volatile("inb %w1,%0" : "=a" (data) : "d" (port));
69 return data;
70 }
71
72 static __inline void
73 insb(int port, void *addr, int cnt)
74 {
75 __asm __volatile("cld\n\trepne\n\tinsb" :
76 :
77 "d" (port), "D" (addr), "c" (cnt) :
78 "%edi", "%ecx", "memory");
79 }
80
81 #define inw(port) \
82 (__use_immediate_port(port) ? __inwc(port) : __inw(port))
83
84 static __inline u_short
85 __inwc(int port)
86 {
87 u_short data;
88 __asm __volatile("inw %1,%0" : "=a" (data) : "i" (port));
89 return data;
90 }
91
92 static __inline u_short
93 __inw(int port)
94 {
95 u_short data;
96 __asm __volatile("inw %%dx,%0" : "=a" (data) : "d" (port));
97 return data;
98 }
99
100 static __inline void
101 insw(int port, void *addr, int cnt)
102 {
103 __asm __volatile("cld\n\trepne\n\tinsw" :
104 :
105 "d" (port), "D" (addr), "c" (cnt) :
106 "%edi", "%ecx", "memory");
107 }
108
109 #define inl(port) \
110 (__use_immediate_port(port) ? __inlc(port) : __inl(port))
111
112 static __inline u_int
113 __inlc(int port)
114 {
115 u_int data;
116 __asm __volatile("inl %1,%0" : "=a" (data) : "i" (port));
117 return data;
118 }
119
120 static __inline u_int
121 __inl(int port)
122 {
123 u_int data;
124 __asm __volatile("inl %%dx,%0" : "=a" (data) : "d" (port));
125 return data;
126 }
127
128 static __inline void
129 insl(int port, void *addr, int cnt)
130 {
131 __asm __volatile("cld\n\trepne\n\tinsl" :
132 :
133 "d" (port), "D" (addr), "c" (cnt) :
134 "%edi", "%ecx", "memory");
135 }
136
137 #define outb(port, data) \
138 (__use_immediate_port(port) ? __outbc(port, data) : __outb(port, data))
139
140 static __inline void
141 __outbc(int port, u_char data)
142 {
143 __asm __volatile("outb %0,%1" : : "a" (data), "id" (port));
144 }
145
146 static __inline void
147 __outb(int port, u_char data)
148 {
149 __asm __volatile("outb %0,%w1" : : "a" (data), "d" (port));
150 }
151
152 static __inline void
153 outsb(int port, void *addr, int cnt)
154 {
155 __asm __volatile("cld\n\trepne\n\toutsb" :
156 :
157 "d" (port), "S" (addr), "c" (cnt) :
158 "%esi", "%ecx");
159 }
160
161 #define outw(port, data) \
162 (__use_immediate_port(port) ? __outwc(port, data) : __outw(port, data))
163
164 static __inline void
165 __outwc(int port, u_short data)
166 {
167 __asm __volatile("outw %0,%1" : : "a" (data), "i" (port));
168 }
169
170 static __inline void
171 __outw(int port, u_short data)
172 {
173 __asm __volatile("outw %0,%%dx" : : "a" (data), "d" (port));
174 }
175
176 static __inline void
177 outsw(int port, void *addr, int cnt)
178 {
179 __asm __volatile("cld\n\trepne\n\toutsw" :
180 :
181 "d" (port), "S" (addr), "c" (cnt) :
182 "%esi", "%ecx");
183 }
184
185 #define outl(port, data) \
186 (__use_immediate_port(port) ? __outlc(port, data) : __outl(port, data))
187
188 static __inline void
189 __outlc(int port, u_int data)
190 {
191 __asm __volatile("outl %0,%1" : : "a" (data), "i" (port));
192 }
193
194 static __inline void
195 __outl(int port, u_int data)
196 {
197 __asm __volatile("outl %0,%%dx" : : "a" (data), "d" (port));
198 }
199
200 static __inline void
201 outsl(int port, void *addr, int cnt)
202 {
203 __asm __volatile("cld\n\trepne\n\toutsl" :
204 :
205 "d" (port), "S" (addr), "c" (cnt) :
206 "%esi", "%ecx");
207 }
208