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