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