1 /* 2 * Copyright (c) 1998 Michael Smith (msmith@freebsd.org) 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 */ 25 26 /* 27 * This code is shared on BIOS and UEFI systems on x86 because 28 * we can access io ports on both platforms and the UEFI Serial IO protocol 29 * is not giving us reliable port order and we see issues with input. 30 */ 31 #include <sys/cdefs.h> 32 33 #include <stand.h> 34 #include <bootstrap.h> 35 #include <stdbool.h> 36 #include <machine/cpufunc.h> 37 #include <dev/ic/ns16550.h> 38 #include <dev/pci/pcireg.h> 39 #include "libi386.h" 40 41 #define COMC_TXWAIT 0x40000 /* transmit timeout */ 42 #define COMC_BPS(x) (115200 / (x)) /* speed to DLAB divisor */ 43 #define COMC_DIV2BPS(x) (115200 / (x)) /* DLAB divisor to speed */ 44 45 #ifndef COMSPEED 46 #define COMSPEED 9600 47 #endif 48 49 #define COM_NPORTS 4 50 #define COM1_IOADDR 0x3f8 51 #define COM2_IOADDR 0x2f8 52 #define COM3_IOADDR 0x3e8 53 #define COM4_IOADDR 0x2e8 54 55 #define STOP1 0x00 56 #define STOP2 0x04 57 58 #define PARODD 0x00 59 #define PAREN 0x08 60 #define PAREVN 0x10 61 #define PARMARK 0x20 62 63 #define BITS5 0x00 /* 5 bits per char */ 64 #define BITS6 0x01 /* 6 bits per char */ 65 #define BITS7 0x02 /* 7 bits per char */ 66 #define BITS8 0x03 /* 8 bits per char */ 67 68 struct serial { 69 int speed; /* baud rate */ 70 uint8_t lcr; /* line control */ 71 uint8_t ignore_cd; /* boolean */ 72 uint8_t rtsdtr_off; /* boolean */ 73 int ioaddr; 74 uint32_t locator; 75 }; 76 77 static void comc_probe(struct console *); 78 static int comc_init(struct console *, int); 79 static void comc_putchar(struct console *, int); 80 static int comc_getchar(struct console *); 81 int comc_getspeed(int); 82 static int comc_ischar(struct console *); 83 static int comc_ioctl(struct console *, int, void *); 84 static uint32_t comc_parse_pcidev(const char *); 85 static int comc_pcidev_set(struct env_var *, int, const void *); 86 static int comc_pcidev_handle(struct console *, uint32_t); 87 static bool comc_setup(struct console *); 88 static char *comc_asprint_mode(struct serial *); 89 static int comc_parse_mode(struct serial *, const char *); 90 static int comc_mode_set(struct env_var *, int, const void *); 91 static int comc_cd_set(struct env_var *, int, const void *); 92 static int comc_rtsdtr_set(struct env_var *, int, const void *); 93 static void comc_devinfo(struct console *); 94 95 static void 96 comc_devinfo(struct console *cp) 97 { 98 struct serial *port = cp->c_private; 99 100 printf("\tport %#x", port->ioaddr); 101 } 102 103 static bool 104 comc_port_is_present(int ioaddr) 105 { 106 /* 107 * Write byte to scratch register and read it out. 108 */ 109 #define COMC_TEST 0xbb 110 outb(ioaddr + com_scr, COMC_TEST); 111 return (inb(ioaddr + com_scr) == COMC_TEST); 112 } 113 114 /* 115 * Set up list of possible serial consoles. 116 * This function is run very early, so we do not expect to 117 * run out of memory, and on error, we can not print output. 118 */ 119 void 120 comc_ini(void) 121 { 122 uint_t n = 0, c; 123 bool ports[COM_NPORTS]; 124 struct console **tmp; 125 struct console *tty; 126 struct serial *port; 127 128 /* 129 * Test the presence of 4 serial devices com1-com4 130 */ 131 ports[0] = comc_port_is_present(COM1_IOADDR); 132 ports[1] = comc_port_is_present(COM2_IOADDR); 133 ports[2] = comc_port_is_present(COM3_IOADDR); 134 ports[3] = comc_port_is_present(COM4_IOADDR); 135 136 for (uint_t i = 0; i < COM_NPORTS; i++) 137 if (ports[i]) 138 n++; 139 140 if (n == 0) /* there are no serial ports */ 141 return; 142 143 c = cons_array_size(); 144 if (c == 0) 145 n++; /* For NULL pointer */ 146 147 tmp = realloc(consoles, (c + n) * sizeof (*consoles)); 148 if (tmp == NULL) 149 return; 150 consoles = tmp; 151 if (c > 0) 152 c--; 153 154 for (uint_t i = 0; i < COM_NPORTS; i++) { 155 if (!ports[i]) 156 continue; 157 tty = malloc(sizeof (*tty)); 158 if (tty == NULL) { 159 /* Out of memory?! can not continue */ 160 consoles[c] = tty; 161 return; 162 } 163 if (asprintf(&tty->c_name, "tty%c", 'a' + i) < 0) { 164 free(tty); 165 consoles[c] = NULL; 166 return; 167 } 168 if (asprintf(&tty->c_desc, "serial port %c", 'a' + i) < 0) { 169 free(tty->c_name); 170 free(tty); 171 consoles[c] = NULL; 172 return; 173 } 174 tty->c_flags = 0; 175 tty->c_probe = comc_probe; 176 tty->c_init = comc_init; 177 tty->c_out = comc_putchar; 178 tty->c_in = comc_getchar; 179 tty->c_ready = comc_ischar; 180 tty->c_ioctl = comc_ioctl; 181 tty->c_devinfo = comc_devinfo; 182 port = malloc(sizeof (*port)); 183 if (port == NULL) { 184 free(tty->c_name); 185 free(tty->c_desc); 186 free(tty); 187 consoles[c] = NULL; 188 return; 189 } 190 port->speed = 0; /* Leave this for comc_probe */ 191 switch (i) { 192 case 0: 193 port->ioaddr = COM1_IOADDR; 194 break; 195 case 1: 196 port->ioaddr = COM2_IOADDR; 197 break; 198 case 2: 199 port->ioaddr = COM3_IOADDR; 200 break; 201 case 3: 202 port->ioaddr = COM4_IOADDR; 203 break; 204 } 205 port->lcr = BITS8; /* 8,n,1 */ 206 port->ignore_cd = 1; /* ignore cd */ 207 port->rtsdtr_off = 0; /* rts-dtr is on */ 208 209 tty->c_private = port; 210 consoles[c++] = tty; 211 } 212 consoles[c] = NULL; 213 } 214 215 static void 216 comc_probe(struct console *cp) 217 { 218 struct serial *port; 219 char name[20]; 220 char value[20]; 221 char *env; 222 223 port = cp->c_private; 224 if (port->speed != 0) 225 return; 226 227 port->speed = COMSPEED; 228 229 /* 230 * Assume that the speed was set by an earlier boot loader if 231 * comconsole is already the preferred console. 232 */ 233 snprintf(name, sizeof (name), "%s-mode", cp->c_name); 234 env = getenv(name); 235 if (env != NULL) { 236 port->speed = comc_getspeed(port->ioaddr); 237 } 238 env = comc_asprint_mode(port); 239 240 if (env != NULL) { 241 unsetenv(name); 242 env_setenv(name, EV_VOLATILE, env, comc_mode_set, env_nounset); 243 free(env); 244 } 245 246 snprintf(name, sizeof (name), "%s-ignore-cd", cp->c_name); 247 env = getenv(name); 248 if (env != NULL) { 249 if (strcmp(env, "true") == 0) 250 port->ignore_cd = 1; 251 else if (strcmp(env, "false") == 0) 252 port->ignore_cd = 0; 253 } 254 255 snprintf(value, sizeof (value), "%s", 256 port->ignore_cd? "true" : "false"); 257 unsetenv(name); 258 env_setenv(name, EV_VOLATILE, value, comc_cd_set, env_nounset); 259 260 snprintf(name, sizeof (name), "%s-rts-dtr-off", cp->c_name); 261 env = getenv(name); 262 if (env != NULL) { 263 if (strcmp(env, "true") == 0) 264 port->rtsdtr_off = 1; 265 else if (strcmp(env, "false") == 0) 266 port->rtsdtr_off = 0; 267 } 268 269 snprintf(value, sizeof (value), "%s", 270 port->rtsdtr_off? "true" : "false"); 271 unsetenv(name); 272 env_setenv(name, EV_VOLATILE, value, comc_rtsdtr_set, env_nounset); 273 274 snprintf(name, sizeof (name), "%s-pcidev", cp->c_name); 275 env = getenv(name); 276 if (env != NULL) { 277 port->locator = comc_parse_pcidev(env); 278 if (port->locator != 0) 279 comc_pcidev_handle(cp, port->locator); 280 } 281 282 unsetenv(name); 283 env_setenv(name, EV_VOLATILE, env, comc_pcidev_set, env_nounset); 284 285 cp->c_flags = 0; 286 if (comc_setup(cp)) 287 cp->c_flags = C_PRESENTIN | C_PRESENTOUT; 288 } 289 290 static int 291 comc_init(struct console *cp, int arg __attribute((unused))) 292 { 293 294 if (comc_setup(cp)) 295 return (CMD_OK); 296 297 cp->c_flags = 0; 298 return (CMD_ERROR); 299 } 300 301 static void 302 comc_putchar(struct console *cp, int c) 303 { 304 int wait; 305 struct serial *sp = cp->c_private; 306 307 for (wait = COMC_TXWAIT; wait > 0; wait--) 308 if (inb(sp->ioaddr + com_lsr) & LSR_TXRDY) { 309 outb(sp->ioaddr + com_data, (uchar_t)c); 310 break; 311 } 312 } 313 314 static int 315 comc_getchar(struct console *cp) 316 { 317 struct serial *sp = cp->c_private; 318 return (comc_ischar(cp) ? inb(sp->ioaddr + com_data) : -1); 319 } 320 321 static int 322 comc_ischar(struct console *cp) 323 { 324 struct serial *sp = cp->c_private; 325 return (inb(sp->ioaddr + com_lsr) & LSR_RXRDY); 326 } 327 328 static int 329 comc_ioctl(struct console *cp __unused, int cmd __unused, void *data __unused) 330 { 331 return (ENOTTY); 332 } 333 334 static char * 335 comc_asprint_mode(struct serial *sp) 336 { 337 char par, *buf; 338 339 if (sp == NULL) 340 return (NULL); 341 342 if ((sp->lcr & (PAREN|PAREVN)) == (PAREN|PAREVN)) 343 par = 'e'; 344 else if ((sp->lcr & PAREN) == PAREN) 345 par = 'o'; 346 else 347 par = 'n'; 348 349 asprintf(&buf, "%d,%d,%c,%d,-", sp->speed, 350 (sp->lcr & BITS8) == BITS8? 8:7, 351 par, (sp->lcr & STOP2) == STOP2? 2:1); 352 return (buf); 353 } 354 355 static int 356 comc_parse_mode(struct serial *sp, const char *value) 357 { 358 unsigned long n; 359 int speed; 360 int lcr; 361 char *ep; 362 363 if (value == NULL || *value == '\0') 364 return (CMD_ERROR); 365 366 errno = 0; 367 n = strtoul(value, &ep, 10); 368 if (errno != 0 || *ep != ',') 369 return (CMD_ERROR); 370 speed = n; 371 372 ep++; 373 errno = 0; 374 n = strtoul(ep, &ep, 10); 375 if (errno != 0 || *ep != ',') 376 return (CMD_ERROR); 377 378 switch (n) { 379 case 7: lcr = BITS7; 380 break; 381 case 8: lcr = BITS8; 382 break; 383 default: 384 return (CMD_ERROR); 385 } 386 387 ep++; 388 switch (*ep++) { 389 case 'n': 390 break; 391 case 'e': lcr |= PAREN|PAREVN; 392 break; 393 case 'o': lcr |= PAREN|PARODD; 394 break; 395 default: 396 return (CMD_ERROR); 397 } 398 399 if (*ep == ',') 400 ep++; 401 else 402 return (CMD_ERROR); 403 404 switch (*ep++) { 405 case '1': 406 break; 407 case '2': lcr |= STOP2; 408 break; 409 default: 410 return (CMD_ERROR); 411 } 412 413 /* handshake is ignored, but we check syntax anyhow */ 414 if (*ep == ',') 415 ep++; 416 else 417 return (CMD_ERROR); 418 419 switch (*ep++) { 420 case '-': 421 case 'h': 422 case 's': 423 break; 424 default: 425 return (CMD_ERROR); 426 } 427 428 if (*ep != '\0') 429 return (CMD_ERROR); 430 431 sp->speed = speed; 432 sp->lcr = lcr; 433 return (CMD_OK); 434 } 435 436 static struct console * 437 get_console(const char *name) 438 { 439 char port[5]; 440 441 (void) strlcpy(port, name, sizeof (port)); 442 for (uint_t i = 0; consoles[i] != NULL; i++) { 443 if (strcmp(port, consoles[i]->c_name) == 0) 444 return (consoles[i]); 445 } 446 447 printf("No such port: %s\n", port); 448 return (NULL); 449 } 450 451 /* 452 * CMD_ERROR will cause set/setenv/setprop command to fail, 453 * when used in loader scripts (forth), this will cause processing 454 * of boot scripts to fail, rendering bootloading impossible. 455 * To prevent such unfortunate situation, we return CMD_OK when 456 * there is no such port, or there is invalid value in mode line. 457 */ 458 static int 459 comc_mode_set(struct env_var *ev, int flags, const void *value) 460 { 461 struct console *cp; 462 char name[15]; 463 464 if (value == NULL) 465 return (CMD_ERROR); 466 467 if ((cp = get_console(ev->ev_name)) == NULL) 468 return (CMD_OK); 469 470 /* Do not override serial setup from SPCR */ 471 snprintf(name, sizeof (name), "%s-spcr-mode", cp->c_name); 472 if (getenv(name) == NULL) { 473 if (comc_parse_mode(cp->c_private, value) == CMD_ERROR) { 474 printf("%s: invalid mode: %s\n", ev->ev_name, 475 (char *)value); 476 return (CMD_OK); 477 } 478 (void) comc_setup(cp); 479 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL); 480 } 481 482 return (CMD_OK); 483 } 484 485 /* 486 * CMD_ERROR will cause set/setenv/setprop command to fail, 487 * when used in loader scripts (forth), this will cause processing 488 * of boot scripts to fail, rendering bootloading impossible. 489 * To prevent such unfortunate situation, we return CMD_OK when 490 * there is no such port or invalid value was used. 491 */ 492 static int 493 comc_cd_set(struct env_var *ev, int flags, const void *value) 494 { 495 struct console *cp; 496 struct serial *sp; 497 498 if (value == NULL) 499 return (CMD_ERROR); 500 501 if ((cp = get_console(ev->ev_name)) == NULL) 502 return (CMD_OK); 503 504 sp = cp->c_private; 505 if (strcmp(value, "true") == 0) { 506 sp->ignore_cd = 1; 507 } else if (strcmp(value, "false") == 0) { 508 sp->ignore_cd = 0; 509 } else { 510 printf("%s: invalid value: %s\n", ev->ev_name, 511 (char *)value); 512 return (CMD_ERROR); 513 } 514 515 (void) comc_setup(cp); 516 517 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL); 518 519 return (CMD_OK); 520 } 521 522 /* 523 * CMD_ERROR will cause set/setenv/setprop command to fail, 524 * when used in loader scripts (forth), this will cause processing 525 * of boot scripts to fail, rendering bootloading impossible. 526 * To prevent such unfortunate situation, we return CMD_OK when 527 * there is no such port, or invalid value was used. 528 */ 529 static int 530 comc_rtsdtr_set(struct env_var *ev, int flags, const void *value) 531 { 532 struct console *cp; 533 struct serial *sp; 534 535 if (value == NULL) 536 return (CMD_ERROR); 537 538 if ((cp = get_console(ev->ev_name)) == NULL) 539 return (CMD_OK); 540 541 sp = cp->c_private; 542 if (strcmp(value, "true") == 0) { 543 sp->rtsdtr_off = 1; 544 } else if (strcmp(value, "false") == 0) { 545 sp->rtsdtr_off = 0; 546 } else { 547 printf("%s: invalid value: %s\n", ev->ev_name, 548 (char *)value); 549 return (CMD_ERROR); 550 } 551 552 (void) comc_setup(cp); 553 554 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL); 555 556 return (CMD_OK); 557 } 558 559 /* 560 * Input: bus:dev:func[:bar]. If bar is not specified, it is 0x10. 561 * Output: bar[24:16] bus[15:8] dev[7:3] func[2:0] 562 */ 563 static uint32_t 564 comc_parse_pcidev(const char *string) 565 { 566 #ifdef EFI 567 (void) string; 568 return (0); 569 #else 570 char *p, *p1; 571 uint8_t bus, dev, func, bar; 572 uint32_t locator; 573 int pres; 574 575 errno = 0; 576 pres = strtoul(string, &p, 10); 577 if (errno != 0 || p == string || *p != ':' || pres < 0) 578 return (0); 579 bus = pres; 580 p1 = ++p; 581 582 pres = strtoul(p1, &p, 10); 583 if (errno != 0 || p == string || *p != ':' || pres < 0) 584 return (0); 585 dev = pres; 586 p1 = ++p; 587 588 pres = strtoul(p1, &p, 10); 589 if (errno != 0 || p == string || (*p != ':' && *p != '\0') || pres < 0) 590 return (0); 591 func = pres; 592 593 if (*p == ':') { 594 p1 = ++p; 595 pres = strtoul(p1, &p, 10); 596 if (errno != 0 || p == string || *p != '\0' || pres <= 0) 597 return (0); 598 bar = pres; 599 } else 600 bar = 0x10; 601 602 locator = (bar << 16) | biospci_locator(bus, dev, func); 603 return (locator); 604 #endif 605 } 606 607 static int 608 comc_pcidev_handle(struct console *cp, uint32_t locator) 609 { 610 #ifdef EFI 611 (void) cp; 612 (void) locator; 613 return (CMD_ERROR); 614 #else 615 struct serial *sp = cp->c_private; 616 uint32_t port; 617 618 if (biospci_read_config(locator & 0xffff, 619 (locator & 0xff0000) >> 16, 2, &port) == -1) { 620 printf("Cannot read bar at 0x%x\n", locator); 621 return (CMD_ERROR); 622 } 623 if (!PCI_BAR_IO(port)) { 624 printf("Memory bar at 0x%x\n", locator); 625 return (CMD_ERROR); 626 } 627 port &= PCIM_BAR_IO_BASE; 628 629 (void) comc_setup(cp); 630 631 sp->locator = locator; 632 633 return (CMD_OK); 634 #endif 635 } 636 637 static int 638 comc_pcidev_set(struct env_var *ev, int flags, const void *value) 639 { 640 struct console *cp; 641 struct serial *sp; 642 uint32_t locator; 643 int error; 644 645 if ((cp = get_console(ev->ev_name)) == NULL) 646 return (CMD_ERROR); 647 sp = cp->c_private; 648 649 if (value == NULL || (locator = comc_parse_pcidev(value)) <= 0) { 650 printf("Invalid pcidev\n"); 651 return (CMD_ERROR); 652 } 653 if ((cp->c_flags & (C_ACTIVEIN | C_ACTIVEOUT)) != 0 && 654 sp->locator != locator) { 655 error = comc_pcidev_handle(cp, locator); 656 if (error != CMD_OK) 657 return (error); 658 } 659 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL); 660 return (CMD_OK); 661 } 662 663 /* 664 * In case of error, we also reset ACTIVE flags, so the console 665 * framefork will try alternate consoles. 666 */ 667 static bool 668 comc_setup(struct console *cp) 669 { 670 struct serial *sp = cp->c_private; 671 static int TRY_COUNT = 1000000; 672 int tries; 673 674 outb(sp->ioaddr + com_cfcr, CFCR_DLAB | sp->lcr); 675 outb(sp->ioaddr + com_dlbl, COMC_BPS(sp->speed) & 0xff); 676 outb(sp->ioaddr + com_dlbh, COMC_BPS(sp->speed) >> 8); 677 outb(sp->ioaddr + com_cfcr, sp->lcr); 678 outb(sp->ioaddr + com_mcr, 679 sp->rtsdtr_off? ~(MCR_RTS | MCR_DTR) : MCR_RTS | MCR_DTR); 680 681 tries = 0; 682 do { 683 inb(sp->ioaddr + com_data); 684 } while (inb(sp->ioaddr + com_lsr) & LSR_RXRDY && ++tries < TRY_COUNT); 685 686 if (tries == TRY_COUNT) 687 return (false); 688 /* Mark this port usable. */ 689 cp->c_flags |= (C_PRESENTIN | C_PRESENTOUT); 690 return (true); 691 } 692 693 int 694 comc_getspeed(int ioaddr) 695 { 696 uint_t divisor; 697 uchar_t dlbh; 698 uchar_t dlbl; 699 uchar_t cfcr; 700 701 cfcr = inb(ioaddr + com_cfcr); 702 outb(ioaddr + com_cfcr, CFCR_DLAB | cfcr); 703 704 dlbl = inb(ioaddr + com_dlbl); 705 dlbh = inb(ioaddr + com_dlbh); 706 707 outb(ioaddr + com_cfcr, cfcr); 708 709 divisor = dlbh << 8 | dlbl; 710 711 /* XXX there should be more sanity checking. */ 712 if (divisor == 0) 713 return (COMSPEED); 714 return (COMC_DIV2BPS(divisor)); 715 } 716