pcio.c revision 1.15 1 /* $NetBSD: pcio.c,v 1.15 2003/10/08 04:25:45 lukem Exp $ */
2
3 /*
4 * Copyright (c) 1996, 1997
5 * Matthias Drochner. 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 for the NetBSD Project
18 * by Matthias Drochner.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 */
34
35 /*
36 * console I/O
37 * needs lowlevel routines from conio.S and comio.S
38 */
39
40 #include <lib/libsa/stand.h>
41 #include <lib/libkern/libkern.h>
42 #include <sys/bootblock.h>
43
44 #include "libi386.h"
45 #include "bootinfo.h"
46
47 extern void conputc __P((int));
48 extern int congetc __P((void));
49 extern int coniskey __P((void));
50 extern struct x86_boot_params boot_params;
51
52 struct btinfo_console btinfo_console;
53
54 #ifdef SUPPORT_SERIAL
55 static int iodev;
56
57 #ifdef DIRECT_SERIAL
58 #include "comio_direct.h"
59
60 #define cominit_x() cominit_d(btinfo_console.addr, btinfo_console.speed)
61 #define computc_x(ch) computc_d(ch, btinfo_console.addr)
62 #define comgetc_x() comgetc_d(btinfo_console.addr)
63 #define comstatus_x() comstatus_d(btinfo_console.addr)
64
65 #else
66 #define cominit_x() cominit(iodev - CONSDEV_COM0)
67 #define computc_x(ch) computc(ch, iodev - CONSDEV_COM0)
68 #define comgetc_x() comgetc(iodev - CONSDEV_COM0)
69 #define comstatus_x() comstatus(iodev - CONSDEV_COM0)
70
71 #endif /* DIRECT_SERIAL */
72
73 static int getcomaddr __P((int));
74 #endif /* SUPPORT_SERIAL */
75
76 #define POLL_FREQ 10
77
78 #ifdef SUPPORT_SERIAL
79 static int
80 getcomaddr(idx)
81 int idx;
82 {
83 short addr;
84 /* read in BIOS data area */
85 pvbcopy((void *)(0x400 + 2 * idx), &addr, 2);
86 return(addr);
87 }
88 #endif
89
90 void
91 initio(dev)
92 int dev;
93 {
94 #ifdef SUPPORT_SERIAL
95 int i;
96
97 #if defined(DIRECT_SERIAL) && defined(CONSPEED)
98 btinfo_console.speed = CONSPEED;
99 #else
100 btinfo_console.speed = 9600;
101 #endif
102
103 switch (dev) {
104 case CONSDEV_AUTO:
105 for(i = 0; i < 3; i++) {
106 iodev = CONSDEV_COM0 + i;
107 btinfo_console.addr = getcomaddr(i);
108 if(!btinfo_console.addr)
109 break;
110 conputc('0' + i); /* to tell user what happens */
111 cominit_x();
112 #ifdef DIRECT_SERIAL
113 /* check for:
114 * 1. successful output
115 * 2. optionally, keypress within 7s
116 */
117 if ( computc_x(':') &&
118 computc_x('-') &&
119 computc_x('(')
120 #ifdef COMCONS_KEYPRESS
121 && awaitkey(7, 0)
122 #endif
123 )
124 goto ok;
125 #else /* ! DIRECT_SERIAL */
126 /*
127 * serial console must have hardware handshake!
128 * check:
129 * 1. character output without error
130 * 2. status bits for modem ready set
131 * (status seems only useful after character output)
132 * 3. optionally, keypress within 7s
133 */
134 if (!(computc_x('@') & 0x80)
135 && (comstatus_x() & 0x00b0)
136 #ifdef COMCONS_KEYPRESS
137 && awaitkey(7, 0)
138 #endif
139 )
140 goto ok;
141 #endif /* DIRECT_SERIAL */
142 }
143 iodev = CONSDEV_PC;
144 ok:
145 break;
146 case CONSDEV_COM0:
147 case CONSDEV_COM1:
148 case CONSDEV_COM2:
149 case CONSDEV_COM3:
150 iodev = dev;
151 btinfo_console.addr = getcomaddr(iodev - CONSDEV_COM0);
152 if(!btinfo_console.addr)
153 goto nocom;
154 cominit_x();
155 break;
156 case CONSDEV_COM0KBD:
157 case CONSDEV_COM1KBD:
158 case CONSDEV_COM2KBD:
159 case CONSDEV_COM3KBD:
160 iodev = dev - 4;
161 i = iodev - CONSDEV_COM0;
162 btinfo_console.addr = getcomaddr(i);
163 if(!btinfo_console.addr)
164 goto nocom;
165 conputc('0' + i); /* to tell user what happens */
166 cominit_x();
167 #ifdef DIRECT_SERIAL
168 /* check for:
169 * 1. successful output
170 * 2. optionally, keypress within 7s
171 */
172 if ( computc_x(':') &&
173 computc_x('-') &&
174 computc_x('(')
175 #ifdef COMCONS_KEYPRESS
176 && awaitkey(7, 0)
177 #endif
178 )
179 break;
180 #else /* ! DIRECT_SERIAL */
181 /*
182 * serial console must have hardware handshake!
183 * check:
184 * 1. character output without error
185 * 2. status bits for modem ready set
186 * (status seems only useful after character output)
187 * 3. optionally, keypress within 7s
188 */
189 if (!(computc_x('@') & 0x80)
190 && (comstatus_x() & 0x00b0)
191 #ifdef COMCONS_KEYPRESS
192 && awaitkey(7, 0)
193 #endif
194 )
195 break;
196 #endif /* DIRECT_SERIAL */
197 default:
198 nocom:
199 iodev = CONSDEV_PC;
200 break;
201 }
202 conputc('\015');
203 conputc('\n');
204 strncpy(btinfo_console.devname, iodev == CONSDEV_PC ? "pc" : "com", 16);
205 #else /* !SUPPORT_SERIAL */
206 btinfo_console.devname[0] = 'p';
207 btinfo_console.devname[1] = 'c';
208 btinfo_console.devname[2] = 0;
209 #endif /* SUPPORT_SERIAL */
210 }
211
212 static inline void internal_putchar __P((int));
213
214 static inline void
215 internal_putchar(c)
216 int c;
217 {
218 #ifdef SUPPORT_SERIAL
219 switch (iodev) {
220 case CONSDEV_PC:
221 #endif
222 conputc(c);
223 #ifdef SUPPORT_SERIAL
224 break;
225 case CONSDEV_COM0:
226 case CONSDEV_COM1:
227 case CONSDEV_COM2:
228 case CONSDEV_COM3:
229 computc_x(c);
230 break;
231 }
232 #endif
233 }
234
235 void
236 putchar(c)
237 int c;
238 {
239 if (c == '\n')
240 internal_putchar('\r');
241 internal_putchar(c);
242 }
243
244 int
245 getchar()
246 {
247 #ifdef SUPPORT_SERIAL
248 int c;
249 switch (iodev) {
250 default: /* to make gcc -Wall happy... */
251 case CONSDEV_PC:
252 #endif
253 return (congetc());
254 #ifdef SUPPORT_SERIAL
255 case CONSDEV_COM0:
256 case CONSDEV_COM1:
257 case CONSDEV_COM2:
258 case CONSDEV_COM3:
259 #ifdef DIRECT_SERIAL
260 c = comgetc_x();
261 #else
262 do {
263 c = comgetc_x();
264 } while ((c >> 8) == 0xe0); /* catch timeout */
265 #ifdef COMDEBUG
266 if (c & 0x8000) {
267 printf("com input %x, status %x\n",
268 c, comstatus_x());
269 }
270 #endif
271 c &= 0xff;
272 #endif /* DIRECT_SERIAL */
273 return (c);
274 }
275 #endif /* SUPPORT_SERIAL */
276 }
277
278 int
279 iskey()
280 {
281 #ifdef SUPPORT_SERIAL
282 switch (iodev) {
283 default: /* to make gcc -Wall happy... */
284 case CONSDEV_PC:
285 #endif
286 return (coniskey());
287 #ifdef SUPPORT_SERIAL
288 case CONSDEV_COM0:
289 case CONSDEV_COM1:
290 case CONSDEV_COM2:
291 case CONSDEV_COM3:
292 #ifdef DIRECT_SERIAL
293 return(!!comstatus_x());
294 #else
295 return (!!(comstatus_x() & 0x0100));
296 #endif
297 }
298 #endif /* SUPPORT_SERIAL */
299 }
300
301 char
302 awaitkey(timeout, tell)
303 int timeout, tell;
304 {
305 int i;
306 char c = 0;
307
308 i = timeout * POLL_FREQ;
309
310 while (i) {
311 if (tell && (i % POLL_FREQ) == 0) {
312 char numbuf[20];
313 int len, j;
314
315 sprintf(numbuf, "%d ", i/POLL_FREQ);
316 len = strlen(numbuf);
317 for (j = 0; j < len; j++)
318 numbuf[len + j] = '\b';
319 numbuf[len + j] = '\0';
320 printf(numbuf);
321 }
322 if (iskey()) {
323 /* flush input buffer */
324 while (iskey())
325 c = getchar();
326 if (c == 0)
327 c = -1;
328 goto out;
329 }
330 delay(1000000 / POLL_FREQ);
331 i--;
332 }
333
334 out:
335 if (tell)
336 printf("0\n");
337
338 return(c);
339 }
340