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
comc_devinfo(struct console * cp)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
comc_port_is_present(int ioaddr)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
comc_ini(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->speed = comc_getspeed(port->ioaddr);
206 port->lcr = BITS8; /* 8,n,1 */
207 port->ignore_cd = 1; /* ignore cd */
208 port->rtsdtr_off = 0; /* rts-dtr is on */
209
210 tty->c_private = port;
211 consoles[c++] = tty;
212
213 /* Reset terminal to initial normal settings with ESC [ 0 m */
214 comc_putchar(tty, 0x1b);
215 comc_putchar(tty, '[');
216 comc_putchar(tty, '0');
217 comc_putchar(tty, 'm');
218 /* drain input from random data */
219 while (comc_getchar(tty) != -1)
220 ;
221 }
222 consoles[c] = NULL;
223 }
224
225 static void
comc_probe(struct console * cp)226 comc_probe(struct console *cp)
227 {
228 struct serial *port;
229 char name[20];
230 char value[20];
231 char *env;
232
233 port = cp->c_private;
234 if (port->speed != 0)
235 return;
236
237 port->speed = COMSPEED;
238
239 /*
240 * Assume that the speed was set by an earlier boot loader if
241 * comconsole is already the preferred console.
242 */
243 snprintf(name, sizeof (name), "%s-mode", cp->c_name);
244 env = getenv(name);
245 if (env != NULL) {
246 port->speed = comc_getspeed(port->ioaddr);
247 }
248 env = comc_asprint_mode(port);
249
250 if (env != NULL) {
251 unsetenv(name);
252 env_setenv(name, EV_VOLATILE, env, comc_mode_set, env_nounset);
253 free(env);
254 }
255
256 snprintf(name, sizeof (name), "%s-ignore-cd", cp->c_name);
257 env = getenv(name);
258 if (env != NULL) {
259 if (strcmp(env, "true") == 0)
260 port->ignore_cd = 1;
261 else if (strcmp(env, "false") == 0)
262 port->ignore_cd = 0;
263 }
264
265 snprintf(value, sizeof (value), "%s",
266 port->ignore_cd? "true" : "false");
267 unsetenv(name);
268 env_setenv(name, EV_VOLATILE, value, comc_cd_set, env_nounset);
269
270 snprintf(name, sizeof (name), "%s-rts-dtr-off", cp->c_name);
271 env = getenv(name);
272 if (env != NULL) {
273 if (strcmp(env, "true") == 0)
274 port->rtsdtr_off = 1;
275 else if (strcmp(env, "false") == 0)
276 port->rtsdtr_off = 0;
277 }
278
279 snprintf(value, sizeof (value), "%s",
280 port->rtsdtr_off? "true" : "false");
281 unsetenv(name);
282 env_setenv(name, EV_VOLATILE, value, comc_rtsdtr_set, env_nounset);
283
284 snprintf(name, sizeof (name), "%s-pcidev", cp->c_name);
285 env = getenv(name);
286 if (env != NULL) {
287 port->locator = comc_parse_pcidev(env);
288 if (port->locator != 0)
289 comc_pcidev_handle(cp, port->locator);
290 }
291
292 unsetenv(name);
293 env_setenv(name, EV_VOLATILE, env, comc_pcidev_set, env_nounset);
294
295 cp->c_flags = 0;
296 if (comc_setup(cp))
297 cp->c_flags = C_PRESENTIN | C_PRESENTOUT;
298 }
299
300 static int
comc_init(struct console * cp,int arg __attribute ((unused)))301 comc_init(struct console *cp, int arg __attribute((unused)))
302 {
303
304 if (comc_setup(cp))
305 return (CMD_OK);
306
307 cp->c_flags = 0;
308 return (CMD_ERROR);
309 }
310
311 static void
comc_putchar(struct console * cp,int c)312 comc_putchar(struct console *cp, int c)
313 {
314 int wait;
315 struct serial *sp = cp->c_private;
316
317 for (wait = COMC_TXWAIT; wait > 0; wait--)
318 if (inb(sp->ioaddr + com_lsr) & LSR_TXRDY) {
319 outb(sp->ioaddr + com_data, (uchar_t)c);
320 break;
321 }
322 }
323
324 static int
comc_getchar(struct console * cp)325 comc_getchar(struct console *cp)
326 {
327 struct serial *sp = cp->c_private;
328 return (comc_ischar(cp) ? inb(sp->ioaddr + com_data) : -1);
329 }
330
331 static int
comc_ischar(struct console * cp)332 comc_ischar(struct console *cp)
333 {
334 struct serial *sp = cp->c_private;
335 return (inb(sp->ioaddr + com_lsr) & LSR_RXRDY);
336 }
337
338 static int
comc_ioctl(struct console * cp __unused,int cmd __unused,void * data __unused)339 comc_ioctl(struct console *cp __unused, int cmd __unused, void *data __unused)
340 {
341 return (ENOTTY);
342 }
343
344 static char *
comc_asprint_mode(struct serial * sp)345 comc_asprint_mode(struct serial *sp)
346 {
347 char par, *buf;
348
349 if (sp == NULL)
350 return (NULL);
351
352 if ((sp->lcr & (PAREN|PAREVN)) == (PAREN|PAREVN))
353 par = 'e';
354 else if ((sp->lcr & PAREN) == PAREN)
355 par = 'o';
356 else
357 par = 'n';
358
359 asprintf(&buf, "%d,%d,%c,%d,-", sp->speed,
360 (sp->lcr & BITS8) == BITS8? 8:7,
361 par, (sp->lcr & STOP2) == STOP2? 2:1);
362 return (buf);
363 }
364
365 static int
comc_parse_mode(struct serial * sp,const char * value)366 comc_parse_mode(struct serial *sp, const char *value)
367 {
368 unsigned long n;
369 int speed;
370 int lcr;
371 char *ep;
372
373 if (value == NULL || *value == '\0')
374 return (CMD_ERROR);
375
376 errno = 0;
377 n = strtoul(value, &ep, 10);
378 if (errno != 0 || *ep != ',')
379 return (CMD_ERROR);
380 speed = n;
381
382 ep++;
383 errno = 0;
384 n = strtoul(ep, &ep, 10);
385 if (errno != 0 || *ep != ',')
386 return (CMD_ERROR);
387
388 switch (n) {
389 case 7: lcr = BITS7;
390 break;
391 case 8: lcr = BITS8;
392 break;
393 default:
394 return (CMD_ERROR);
395 }
396
397 ep++;
398 switch (*ep++) {
399 case 'n':
400 break;
401 case 'e': lcr |= PAREN|PAREVN;
402 break;
403 case 'o': lcr |= PAREN|PARODD;
404 break;
405 default:
406 return (CMD_ERROR);
407 }
408
409 if (*ep == ',')
410 ep++;
411 else
412 return (CMD_ERROR);
413
414 switch (*ep++) {
415 case '1':
416 break;
417 case '2': lcr |= STOP2;
418 break;
419 default:
420 return (CMD_ERROR);
421 }
422
423 /* handshake is ignored, but we check syntax anyhow */
424 if (*ep == ',')
425 ep++;
426 else
427 return (CMD_ERROR);
428
429 switch (*ep++) {
430 case '-':
431 case 'h':
432 case 's':
433 break;
434 default:
435 return (CMD_ERROR);
436 }
437
438 if (*ep != '\0')
439 return (CMD_ERROR);
440
441 sp->speed = speed;
442 sp->lcr = lcr;
443 return (CMD_OK);
444 }
445
446 /*
447 * CMD_ERROR will cause set/setenv/setprop command to fail,
448 * when used in loader scripts (forth), this will cause processing
449 * of boot scripts to fail, rendering bootloading impossible.
450 * To prevent such unfortunate situation, we return CMD_OK when
451 * there is no such port, or there is invalid value in mode line.
452 */
453 static int
comc_mode_set(struct env_var * ev,int flags,const void * value)454 comc_mode_set(struct env_var *ev, int flags, const void *value)
455 {
456 struct console *cp;
457 char name[15];
458
459 if (value == NULL)
460 return (CMD_ERROR);
461
462 if ((cp = cons_get_console(ev->ev_name)) == NULL)
463 return (CMD_OK);
464
465 /* Do not override serial setup from SPCR */
466 snprintf(name, sizeof (name), "%s-spcr-mode", cp->c_name);
467 if (getenv(name) == NULL) {
468 if (comc_parse_mode(cp->c_private, value) == CMD_ERROR) {
469 printf("%s: invalid mode: %s\n", ev->ev_name,
470 (char *)value);
471 return (CMD_OK);
472 }
473 (void) comc_setup(cp);
474 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
475 }
476
477 return (CMD_OK);
478 }
479
480 /*
481 * CMD_ERROR will cause set/setenv/setprop command to fail,
482 * when used in loader scripts (forth), this will cause processing
483 * of boot scripts to fail, rendering bootloading impossible.
484 * To prevent such unfortunate situation, we return CMD_OK when
485 * there is no such port or invalid value was used.
486 */
487 static int
comc_cd_set(struct env_var * ev,int flags,const void * value)488 comc_cd_set(struct env_var *ev, int flags, const void *value)
489 {
490 struct console *cp;
491 struct serial *sp;
492
493 if (value == NULL)
494 return (CMD_OK);
495
496 if ((cp = cons_get_console(ev->ev_name)) == NULL)
497 return (CMD_OK);
498
499 sp = cp->c_private;
500 if (strcmp(value, "true") == 0) {
501 sp->ignore_cd = 1;
502 } else if (strcmp(value, "false") == 0) {
503 sp->ignore_cd = 0;
504 } else {
505 printf("%s: invalid value: %s\n", ev->ev_name,
506 (char *)value);
507 return (CMD_OK);
508 }
509
510 (void) comc_setup(cp);
511
512 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
513
514 return (CMD_OK);
515 }
516
517 /*
518 * CMD_ERROR will cause set/setenv/setprop command to fail,
519 * when used in loader scripts (forth), this will cause processing
520 * of boot scripts to fail, rendering bootloading impossible.
521 * To prevent such unfortunate situation, we return CMD_OK when
522 * there is no such port, or invalid value was used.
523 */
524 static int
comc_rtsdtr_set(struct env_var * ev,int flags,const void * value)525 comc_rtsdtr_set(struct env_var *ev, int flags, const void *value)
526 {
527 struct console *cp;
528 struct serial *sp;
529
530 if (value == NULL)
531 return (CMD_OK);
532
533 if ((cp = cons_get_console(ev->ev_name)) == NULL)
534 return (CMD_OK);
535
536 sp = cp->c_private;
537 if (strcmp(value, "true") == 0) {
538 sp->rtsdtr_off = 1;
539 } else if (strcmp(value, "false") == 0) {
540 sp->rtsdtr_off = 0;
541 } else {
542 printf("%s: invalid value: %s\n", ev->ev_name,
543 (char *)value);
544 return (CMD_OK);
545 }
546
547 (void) comc_setup(cp);
548
549 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
550
551 return (CMD_OK);
552 }
553
554 /*
555 * Input: bus:dev:func[:bar]. If bar is not specified, it is 0x10.
556 * Output: bar[24:16] bus[15:8] dev[7:3] func[2:0]
557 */
558 static uint32_t
comc_parse_pcidev(const char * string)559 comc_parse_pcidev(const char *string)
560 {
561 #ifdef EFI
562 (void) string;
563 return (0);
564 #else
565 char *p, *p1;
566 uint8_t bus, dev, func, bar;
567 uint32_t locator;
568 int pres;
569
570 errno = 0;
571 pres = strtoul(string, &p, 10);
572 if (errno != 0 || p == string || *p != ':' || pres < 0)
573 return (0);
574 bus = pres;
575 p1 = ++p;
576
577 pres = strtoul(p1, &p, 10);
578 if (errno != 0 || p == string || *p != ':' || pres < 0)
579 return (0);
580 dev = pres;
581 p1 = ++p;
582
583 pres = strtoul(p1, &p, 10);
584 if (errno != 0 || p == string || (*p != ':' && *p != '\0') || pres < 0)
585 return (0);
586 func = pres;
587
588 if (*p == ':') {
589 p1 = ++p;
590 pres = strtoul(p1, &p, 10);
591 if (errno != 0 || p == string || *p != '\0' || pres <= 0)
592 return (0);
593 bar = pres;
594 } else
595 bar = 0x10;
596
597 locator = (bar << 16) | biospci_locator(bus, dev, func);
598 return (locator);
599 #endif
600 }
601
602 static int
comc_pcidev_handle(struct console * cp,uint32_t locator)603 comc_pcidev_handle(struct console *cp, uint32_t locator)
604 {
605 #ifdef EFI
606 (void) cp;
607 (void) locator;
608 return (CMD_ERROR);
609 #else
610 struct serial *sp = cp->c_private;
611 uint32_t port;
612
613 if (biospci_read_config(locator & 0xffff,
614 (locator & 0xff0000) >> 16, 2, &port) == -1) {
615 printf("Cannot read bar at 0x%x\n", locator);
616 return (CMD_ERROR);
617 }
618 if (!PCI_BAR_IO(port)) {
619 printf("Memory bar at 0x%x\n", locator);
620 return (CMD_ERROR);
621 }
622 port &= PCIM_BAR_IO_BASE;
623
624 (void) comc_setup(cp);
625
626 sp->locator = locator;
627
628 return (CMD_OK);
629 #endif
630 }
631
632 static int
comc_pcidev_set(struct env_var * ev,int flags,const void * value)633 comc_pcidev_set(struct env_var *ev, int flags, const void *value)
634 {
635 struct console *cp;
636 struct serial *sp;
637 uint32_t locator;
638 int error;
639
640 if ((cp = cons_get_console(ev->ev_name)) == NULL)
641 return (CMD_ERROR);
642 sp = cp->c_private;
643
644 if (value == NULL || (locator = comc_parse_pcidev(value)) <= 0) {
645 printf("Invalid pcidev\n");
646 return (CMD_ERROR);
647 }
648 if ((cp->c_flags & (C_ACTIVEIN | C_ACTIVEOUT)) != 0 &&
649 sp->locator != locator) {
650 error = comc_pcidev_handle(cp, locator);
651 if (error != CMD_OK)
652 return (error);
653 }
654 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
655 return (CMD_OK);
656 }
657
658 /*
659 * In case of error, we also reset ACTIVE flags, so the console
660 * framefork will try alternate consoles.
661 */
662 static bool
comc_setup(struct console * cp)663 comc_setup(struct console *cp)
664 {
665 struct serial *sp = cp->c_private;
666 static int TRY_COUNT = 1000000;
667 int tries;
668
669 outb(sp->ioaddr + com_cfcr, CFCR_DLAB | sp->lcr);
670 outb(sp->ioaddr + com_dlbl, COMC_BPS(sp->speed) & 0xff);
671 outb(sp->ioaddr + com_dlbh, COMC_BPS(sp->speed) >> 8);
672 outb(sp->ioaddr + com_cfcr, sp->lcr);
673 outb(sp->ioaddr + com_mcr,
674 sp->rtsdtr_off? ~(MCR_RTS | MCR_DTR) : MCR_RTS | MCR_DTR);
675
676 tries = 0;
677 do {
678 inb(sp->ioaddr + com_data);
679 } while (inb(sp->ioaddr + com_lsr) & LSR_RXRDY && ++tries < TRY_COUNT);
680
681 if (tries == TRY_COUNT)
682 return (false);
683 /* Mark this port usable. */
684 cp->c_flags |= (C_PRESENTIN | C_PRESENTOUT);
685 return (true);
686 }
687
688 int
comc_getspeed(int ioaddr)689 comc_getspeed(int ioaddr)
690 {
691 uint_t divisor;
692 uchar_t dlbh;
693 uchar_t dlbl;
694 uchar_t cfcr;
695
696 cfcr = inb(ioaddr + com_cfcr);
697 outb(ioaddr + com_cfcr, CFCR_DLAB | cfcr);
698
699 dlbl = inb(ioaddr + com_dlbl);
700 dlbh = inb(ioaddr + com_dlbh);
701
702 outb(ioaddr + com_cfcr, cfcr);
703
704 divisor = dlbh << 8 | dlbl;
705
706 /* XXX there should be more sanity checking. */
707 if (divisor == 0)
708 return (COMSPEED);
709 return (COMC_DIV2BPS(divisor));
710 }
711