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