pcio.c revision 1.17 1 1.17 drochner /* $NetBSD: pcio.c,v 1.17 2004/03/24 16:46:28 drochner 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.1 perry extern void conputc __P((int));
42 1.1 perry extern int congetc __P((void));
43 1.1 perry extern int coniskey __P((void));
44 1.15 lukem extern struct x86_boot_params boot_params;
45 1.1 perry
46 1.5 drochner struct btinfo_console btinfo_console;
47 1.5 drochner
48 1.1 perry #ifdef SUPPORT_SERIAL
49 1.5 drochner static int iodev;
50 1.5 drochner
51 1.5 drochner #ifdef DIRECT_SERIAL
52 1.5 drochner #include "comio_direct.h"
53 1.5 drochner
54 1.14 dsl #define cominit_x() cominit_d(btinfo_console.addr, btinfo_console.speed)
55 1.14 dsl #define computc_x(ch) computc_d(ch, btinfo_console.addr)
56 1.14 dsl #define comgetc_x() comgetc_d(btinfo_console.addr)
57 1.14 dsl #define comstatus_x() comstatus_d(btinfo_console.addr)
58 1.5 drochner
59 1.5 drochner #else
60 1.14 dsl #define cominit_x() cominit(iodev - CONSDEV_COM0)
61 1.14 dsl #define computc_x(ch) computc(ch, iodev - CONSDEV_COM0)
62 1.14 dsl #define comgetc_x() comgetc(iodev - CONSDEV_COM0)
63 1.14 dsl #define comstatus_x() comstatus(iodev - CONSDEV_COM0)
64 1.1 perry
65 1.5 drochner #endif /* DIRECT_SERIAL */
66 1.5 drochner
67 1.5 drochner static int getcomaddr __P((int));
68 1.5 drochner #endif /* SUPPORT_SERIAL */
69 1.5 drochner
70 1.5 drochner #define POLL_FREQ 10
71 1.1 perry
72 1.1 perry #ifdef SUPPORT_SERIAL
73 1.5 drochner static int
74 1.5 drochner getcomaddr(idx)
75 1.9 drochner int idx;
76 1.5 drochner {
77 1.5 drochner short addr;
78 1.5 drochner /* read in BIOS data area */
79 1.8 sommerfe pvbcopy((void *)(0x400 + 2 * idx), &addr, 2);
80 1.5 drochner return(addr);
81 1.5 drochner }
82 1.1 perry #endif
83 1.1 perry
84 1.5 drochner void
85 1.3 thorpej initio(dev)
86 1.3 thorpej int dev;
87 1.1 perry {
88 1.1 perry #ifdef SUPPORT_SERIAL
89 1.5 drochner int i;
90 1.5 drochner
91 1.14 dsl #if defined(DIRECT_SERIAL) && defined(CONSPEED)
92 1.14 dsl btinfo_console.speed = CONSPEED;
93 1.14 dsl #else
94 1.14 dsl btinfo_console.speed = 9600;
95 1.14 dsl #endif
96 1.14 dsl
97 1.3 thorpej switch (dev) {
98 1.5 drochner case CONSDEV_AUTO:
99 1.5 drochner for(i = 0; i < 3; i++) {
100 1.5 drochner iodev = CONSDEV_COM0 + i;
101 1.5 drochner btinfo_console.addr = getcomaddr(i);
102 1.12 dsl if(!btinfo_console.addr)
103 1.12 dsl break;
104 1.5 drochner conputc('0' + i); /* to tell user what happens */
105 1.14 dsl cominit_x();
106 1.5 drochner #ifdef DIRECT_SERIAL
107 1.5 drochner /* check for:
108 1.5 drochner * 1. successful output
109 1.12 dsl * 2. optionally, keypress within 7s
110 1.5 drochner */
111 1.14 dsl if ( computc_x(':') &&
112 1.14 dsl computc_x('-') &&
113 1.14 dsl computc_x('(')
114 1.5 drochner #ifdef COMCONS_KEYPRESS
115 1.7 rvb && awaitkey(7, 0)
116 1.5 drochner #endif
117 1.5 drochner )
118 1.5 drochner goto ok;
119 1.11 thorpej #else /* ! DIRECT_SERIAL */
120 1.5 drochner /*
121 1.5 drochner * serial console must have hardware handshake!
122 1.5 drochner * check:
123 1.5 drochner * 1. character output without error
124 1.5 drochner * 2. status bits for modem ready set
125 1.5 drochner * (status seems only useful after character output)
126 1.12 dsl * 3. optionally, keypress within 7s
127 1.5 drochner */
128 1.14 dsl if (!(computc_x('@') & 0x80)
129 1.14 dsl && (comstatus_x() & 0x00b0)
130 1.5 drochner #ifdef COMCONS_KEYPRESS
131 1.7 rvb && awaitkey(7, 0)
132 1.5 drochner #endif
133 1.5 drochner )
134 1.5 drochner goto ok;
135 1.11 thorpej #endif /* DIRECT_SERIAL */
136 1.3 thorpej }
137 1.5 drochner iodev = CONSDEV_PC;
138 1.5 drochner ok:
139 1.3 thorpej break;
140 1.5 drochner case CONSDEV_COM0:
141 1.5 drochner case CONSDEV_COM1:
142 1.5 drochner case CONSDEV_COM2:
143 1.5 drochner case CONSDEV_COM3:
144 1.5 drochner iodev = dev;
145 1.5 drochner btinfo_console.addr = getcomaddr(iodev - CONSDEV_COM0);
146 1.12 dsl if(!btinfo_console.addr)
147 1.12 dsl goto nocom;
148 1.14 dsl cominit_x();
149 1.3 thorpej break;
150 1.7 rvb case CONSDEV_COM0KBD:
151 1.7 rvb case CONSDEV_COM1KBD:
152 1.7 rvb case CONSDEV_COM2KBD:
153 1.7 rvb case CONSDEV_COM3KBD:
154 1.7 rvb iodev = dev - 4;
155 1.7 rvb i = iodev - CONSDEV_COM0;
156 1.7 rvb btinfo_console.addr = getcomaddr(i);
157 1.12 dsl if(!btinfo_console.addr)
158 1.12 dsl goto nocom;
159 1.7 rvb conputc('0' + i); /* to tell user what happens */
160 1.14 dsl cominit_x();
161 1.7 rvb #ifdef DIRECT_SERIAL
162 1.7 rvb /* check for:
163 1.7 rvb * 1. successful output
164 1.12 dsl * 2. optionally, keypress within 7s
165 1.7 rvb */
166 1.14 dsl if ( computc_x(':') &&
167 1.14 dsl computc_x('-') &&
168 1.14 dsl computc_x('(')
169 1.7 rvb #ifdef COMCONS_KEYPRESS
170 1.7 rvb && awaitkey(7, 0)
171 1.7 rvb #endif
172 1.7 rvb )
173 1.7 rvb break;
174 1.11 thorpej #else /* ! DIRECT_SERIAL */
175 1.7 rvb /*
176 1.7 rvb * serial console must have hardware handshake!
177 1.7 rvb * check:
178 1.7 rvb * 1. character output without error
179 1.7 rvb * 2. status bits for modem ready set
180 1.7 rvb * (status seems only useful after character output)
181 1.12 dsl * 3. optionally, keypress within 7s
182 1.7 rvb */
183 1.14 dsl if (!(computc_x('@') & 0x80)
184 1.14 dsl && (comstatus_x() & 0x00b0)
185 1.7 rvb #ifdef COMCONS_KEYPRESS
186 1.7 rvb && awaitkey(7, 0)
187 1.7 rvb #endif
188 1.7 rvb )
189 1.7 rvb break;
190 1.11 thorpej #endif /* DIRECT_SERIAL */
191 1.5 drochner default:
192 1.5 drochner nocom:
193 1.5 drochner iodev = CONSDEV_PC;
194 1.3 thorpej break;
195 1.3 thorpej }
196 1.7 rvb conputc('\015');
197 1.7 rvb conputc('\n');
198 1.5 drochner strncpy(btinfo_console.devname, iodev == CONSDEV_PC ? "pc" : "com", 16);
199 1.5 drochner #else /* !SUPPORT_SERIAL */
200 1.13 dsl btinfo_console.devname[0] = 'p';
201 1.13 dsl btinfo_console.devname[1] = 'c';
202 1.13 dsl btinfo_console.devname[2] = 0;
203 1.5 drochner #endif /* SUPPORT_SERIAL */
204 1.1 perry }
205 1.1 perry
206 1.9 drochner static inline void internal_putchar __P((int));
207 1.9 drochner
208 1.3 thorpej static inline void
209 1.3 thorpej internal_putchar(c)
210 1.9 drochner int c;
211 1.1 perry {
212 1.1 perry #ifdef SUPPORT_SERIAL
213 1.3 thorpej switch (iodev) {
214 1.5 drochner case CONSDEV_PC:
215 1.1 perry #endif
216 1.3 thorpej conputc(c);
217 1.1 perry #ifdef SUPPORT_SERIAL
218 1.3 thorpej break;
219 1.5 drochner case CONSDEV_COM0:
220 1.5 drochner case CONSDEV_COM1:
221 1.5 drochner case CONSDEV_COM2:
222 1.5 drochner case CONSDEV_COM3:
223 1.14 dsl computc_x(c);
224 1.3 thorpej break;
225 1.3 thorpej }
226 1.1 perry #endif
227 1.1 perry }
228 1.1 perry
229 1.3 thorpej void
230 1.3 thorpej putchar(c)
231 1.9 drochner int c;
232 1.1 perry {
233 1.3 thorpej if (c == '\n')
234 1.3 thorpej internal_putchar('\r');
235 1.3 thorpej internal_putchar(c);
236 1.1 perry }
237 1.1 perry
238 1.3 thorpej int
239 1.3 thorpej getchar()
240 1.3 thorpej {
241 1.16 dsl int c;
242 1.1 perry #ifdef SUPPORT_SERIAL
243 1.3 thorpej switch (iodev) {
244 1.5 drochner default: /* to make gcc -Wall happy... */
245 1.5 drochner case CONSDEV_PC:
246 1.1 perry #endif
247 1.16 dsl c = congetc();
248 1.16 dsl #ifdef CONSOLE_KEYMAP
249 1.16 dsl {
250 1.16 dsl char *cp = strchr(CONSOLE_KEYMAP, c);
251 1.16 dsl if (cp != 0 && cp[1] != 0)
252 1.16 dsl c = cp[1];
253 1.16 dsl }
254 1.16 dsl #endif
255 1.16 dsl return c;
256 1.1 perry #ifdef SUPPORT_SERIAL
257 1.5 drochner case CONSDEV_COM0:
258 1.5 drochner case CONSDEV_COM1:
259 1.5 drochner case CONSDEV_COM2:
260 1.5 drochner case CONSDEV_COM3:
261 1.6 drochner #ifdef DIRECT_SERIAL
262 1.14 dsl c = comgetc_x();
263 1.6 drochner #else
264 1.6 drochner do {
265 1.14 dsl c = comgetc_x();
266 1.6 drochner } while ((c >> 8) == 0xe0); /* catch timeout */
267 1.6 drochner #ifdef COMDEBUG
268 1.6 drochner if (c & 0x8000) {
269 1.6 drochner printf("com input %x, status %x\n",
270 1.14 dsl c, comstatus_x());
271 1.6 drochner }
272 1.6 drochner #endif
273 1.6 drochner c &= 0xff;
274 1.6 drochner #endif /* DIRECT_SERIAL */
275 1.6 drochner return (c);
276 1.3 thorpej }
277 1.6 drochner #endif /* SUPPORT_SERIAL */
278 1.1 perry }
279 1.1 perry
280 1.3 thorpej int
281 1.3 thorpej iskey()
282 1.3 thorpej {
283 1.1 perry #ifdef SUPPORT_SERIAL
284 1.3 thorpej switch (iodev) {
285 1.5 drochner default: /* to make gcc -Wall happy... */
286 1.5 drochner case CONSDEV_PC:
287 1.1 perry #endif
288 1.3 thorpej return (coniskey());
289 1.1 perry #ifdef SUPPORT_SERIAL
290 1.5 drochner case CONSDEV_COM0:
291 1.5 drochner case CONSDEV_COM1:
292 1.5 drochner case CONSDEV_COM2:
293 1.5 drochner case CONSDEV_COM3:
294 1.5 drochner #ifdef DIRECT_SERIAL
295 1.14 dsl return(!!comstatus_x());
296 1.5 drochner #else
297 1.14 dsl return (!!(comstatus_x() & 0x0100));
298 1.5 drochner #endif
299 1.3 thorpej }
300 1.5 drochner #endif /* SUPPORT_SERIAL */
301 1.5 drochner }
302 1.5 drochner
303 1.5 drochner char
304 1.5 drochner awaitkey(timeout, tell)
305 1.5 drochner int timeout, tell;
306 1.5 drochner {
307 1.5 drochner int i;
308 1.5 drochner char c = 0;
309 1.5 drochner
310 1.5 drochner i = timeout * POLL_FREQ;
311 1.5 drochner
312 1.5 drochner while (i) {
313 1.10 drochner if (tell && (i % POLL_FREQ) == 0) {
314 1.10 drochner char numbuf[20];
315 1.10 drochner int len, j;
316 1.10 drochner
317 1.10 drochner sprintf(numbuf, "%d ", i/POLL_FREQ);
318 1.10 drochner len = strlen(numbuf);
319 1.10 drochner for (j = 0; j < len; j++)
320 1.10 drochner numbuf[len + j] = '\b';
321 1.10 drochner numbuf[len + j] = '\0';
322 1.10 drochner printf(numbuf);
323 1.10 drochner }
324 1.5 drochner if (iskey()) {
325 1.5 drochner /* flush input buffer */
326 1.5 drochner while (iskey())
327 1.5 drochner c = getchar();
328 1.12 dsl if (c == 0)
329 1.12 dsl c = -1;
330 1.12 dsl goto out;
331 1.5 drochner }
332 1.5 drochner delay(1000000 / POLL_FREQ);
333 1.5 drochner i--;
334 1.5 drochner }
335 1.5 drochner
336 1.5 drochner out:
337 1.5 drochner if (tell)
338 1.16 dsl printf("0 \n");
339 1.5 drochner
340 1.5 drochner return(c);
341 1.1 perry }
342