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