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