xref: /illumos-gate/usr/src/boot/i386/libi386/comconsole.c (revision 7a6d80f1660abd4755c68cbd094d4a914681d26e)
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	COM1_IOADDR	0x3f8
50 #define	COM2_IOADDR	0x2f8
51 #define	COM3_IOADDR	0x3e8
52 #define	COM4_IOADDR	0x2e8
53 
54 #define	STOP1		0x00
55 #define	STOP2		0x04
56 
57 #define	PARODD		0x00
58 #define	PAREN		0x08
59 #define	PAREVN		0x10
60 #define	PARMARK		0x20
61 
62 #define	BITS5		0x00	/* 5 bits per char */
63 #define	BITS6		0x01	/* 6 bits per char */
64 #define	BITS7		0x02	/* 7 bits per char */
65 #define	BITS8		0x03	/* 8 bits per char */
66 
67 struct serial {
68     int		speed;		/* baud rate */
69     uint8_t	lcr;		/* line control */
70     uint8_t	ignore_cd;	/* boolean */
71     uint8_t	rtsdtr_off;	/* boolean */
72     int		ioaddr;
73     uint32_t	locator;
74 };
75 
76 static void	comc_probe(struct console *);
77 static int	comc_init(struct console *, int);
78 static void	comc_putchar(struct console *, int);
79 static int	comc_getchar(struct console *);
80 int		comc_getspeed(int);
81 static int	comc_ischar(struct console *);
82 static int	comc_ioctl(struct console *, int, void *);
83 static uint32_t comc_parse_pcidev(const char *);
84 static int	comc_pcidev_set(struct env_var *, int, const void *);
85 static int	comc_pcidev_handle(struct console *, uint32_t);
86 static bool	comc_setup(struct console *);
87 static char	*comc_asprint_mode(struct serial *);
88 static int	comc_parse_mode(struct serial *, const char *);
89 static int	comc_mode_set(struct env_var *, int, const void *);
90 static int	comc_cd_set(struct env_var *, int, const void *);
91 static int	comc_rtsdtr_set(struct env_var *, int, const void *);
92 static void	comc_devinfo(struct console *);
93 
94 struct console ttya = {
95 	.c_name = "ttya",
96 	.c_desc = "serial port a",
97 	.c_flags = 0,
98 	.c_probe = comc_probe,
99 	.c_init = comc_init,
100 	.c_out = comc_putchar,
101 	.c_in = comc_getchar,
102 	.c_ready = comc_ischar,
103 	.c_ioctl = comc_ioctl,
104 	.c_devinfo = comc_devinfo,
105 	.c_private = NULL
106 };
107 
108 struct console ttyb = {
109 	.c_name = "ttyb",
110 	.c_desc = "serial port b",
111 	.c_flags = 0,
112 	.c_probe = comc_probe,
113 	.c_init = comc_init,
114 	.c_out = comc_putchar,
115 	.c_in = comc_getchar,
116 	.c_ready = comc_ischar,
117 	.c_ioctl = comc_ioctl,
118 	.c_devinfo = comc_devinfo,
119 	.c_private = NULL
120 };
121 
122 struct console ttyc = {
123 	.c_name = "ttyc",
124 	.c_desc = "serial port c",
125 	.c_flags = 0,
126 	.c_probe = comc_probe,
127 	.c_init = comc_init,
128 	.c_out = comc_putchar,
129 	.c_in = comc_getchar,
130 	.c_ready = comc_ischar,
131 	.c_ioctl = comc_ioctl,
132 	.c_devinfo = comc_devinfo,
133 	.c_private = NULL
134 };
135 
136 struct console ttyd = {
137 	.c_name = "ttyd",
138 	.c_desc = "serial port d",
139 	.c_flags = 0,
140 	.c_probe = comc_probe,
141 	.c_init = comc_init,
142 	.c_out = comc_putchar,
143 	.c_in = comc_getchar,
144 	.c_ready = comc_ischar,
145 	.c_ioctl = comc_ioctl,
146 	.c_devinfo = comc_devinfo,
147 	.c_private = NULL
148 };
149 
150 static void
151 comc_devinfo(struct console *cp)
152 {
153 	struct serial *port = cp->c_private;
154 
155 	if (cp->c_flags != 0)
156 		printf("\tport %#x", port->ioaddr);
157 	else
158 		printf("\tdevice is not present");
159 }
160 
161 static void
162 comc_probe(struct console *cp)
163 {
164 	struct serial *port;
165 	char name[20];
166 	char value[20];
167 	char *env;
168 
169 	if (cp->c_private != NULL)
170 		return;
171 
172 	cp->c_private = malloc(sizeof (struct serial));
173 	port = cp->c_private;
174 	port->speed = COMSPEED;
175 
176 	if (strcmp(cp->c_name, "ttya") == 0)
177 		port->ioaddr = COM1_IOADDR;
178 	else if (strcmp(cp->c_name, "ttyb") == 0)
179 		port->ioaddr = COM2_IOADDR;
180 	else if (strcmp(cp->c_name, "ttyc") == 0)
181 		port->ioaddr = COM3_IOADDR;
182 	else if (strcmp(cp->c_name, "ttyd") == 0)
183 		port->ioaddr = COM4_IOADDR;
184 
185 	port->lcr = BITS8;	/* 8,n,1 */
186 	port->ignore_cd = 1;	/* ignore cd */
187 	port->rtsdtr_off = 0;	/* rts-dtr is on */
188 
189 	/*
190 	 * Assume that the speed was set by an earlier boot loader if
191 	 * comconsole is already the preferred console.
192 	 */
193 	snprintf(name, sizeof (name), "%s-mode", cp->c_name);
194 	env = getenv(name);
195 	if (env != NULL) {
196 		/* (void) comc_parse_mode(port, env);*/
197 		port->speed = comc_getspeed(port->ioaddr);
198 	}
199 	env = comc_asprint_mode(port);
200 
201 	if (env != NULL) {
202 		unsetenv(name);
203 		env_setenv(name, EV_VOLATILE, env, comc_mode_set, env_nounset);
204 		free(env);
205 	}
206 
207 	snprintf(name, sizeof (name), "%s-ignore-cd", cp->c_name);
208 	env = getenv(name);
209 	if (env != NULL) {
210 		if (strcmp(env, "true") == 0)
211 			port->ignore_cd = 1;
212 		else if (strcmp(env, "false") == 0)
213 			port->ignore_cd = 0;
214 	}
215 
216 	snprintf(value, sizeof (value), "%s",
217 	    port->ignore_cd? "true" : "false");
218 	unsetenv(name);
219 	env_setenv(name, EV_VOLATILE, value, comc_cd_set, env_nounset);
220 
221 	snprintf(name, sizeof (name), "%s-rts-dtr-off", cp->c_name);
222 	env = getenv(name);
223 	if (env != NULL) {
224 		if (strcmp(env, "true") == 0)
225 			port->rtsdtr_off = 1;
226 		else if (strcmp(env, "false") == 0)
227 			port->rtsdtr_off = 0;
228 	}
229 
230 	snprintf(value, sizeof (value), "%s",
231 	    port->rtsdtr_off? "true" : "false");
232 	unsetenv(name);
233 	env_setenv(name, EV_VOLATILE, value, comc_rtsdtr_set, env_nounset);
234 
235 	snprintf(name, sizeof (name), "%s-pcidev", cp->c_name);
236 	env = getenv(name);
237 	if (env != NULL) {
238 		port->locator = comc_parse_pcidev(env);
239 		if (port->locator != 0)
240 			comc_pcidev_handle(cp, port->locator);
241 	}
242 
243 	unsetenv(name);
244 	env_setenv(name, EV_VOLATILE, env, comc_pcidev_set, env_nounset);
245 
246 	cp->c_flags = 0;
247 	if (comc_setup(cp))
248 		cp->c_flags = C_PRESENTIN | C_PRESENTOUT;
249 }
250 
251 static int
252 comc_init(struct console *cp, int arg __attribute((unused)))
253 {
254 
255 	if (comc_setup(cp))
256 		return (CMD_OK);
257 
258 	cp->c_flags = 0;
259 	return (CMD_ERROR);
260 }
261 
262 static void
263 comc_putchar(struct console *cp, int c)
264 {
265 	int wait;
266 	struct serial *sp = cp->c_private;
267 
268 	for (wait = COMC_TXWAIT; wait > 0; wait--)
269 		if (inb(sp->ioaddr + com_lsr) & LSR_TXRDY) {
270 			outb(sp->ioaddr + com_data, (uchar_t)c);
271 			break;
272 		}
273 }
274 
275 static int
276 comc_getchar(struct console *cp)
277 {
278 	struct serial *sp = cp->c_private;
279 	return (comc_ischar(cp) ? inb(sp->ioaddr + com_data) : -1);
280 }
281 
282 static int
283 comc_ischar(struct console *cp)
284 {
285 	struct serial *sp = cp->c_private;
286 	return (inb(sp->ioaddr + com_lsr) & LSR_RXRDY);
287 }
288 
289 static int
290 comc_ioctl(struct console *cp __unused, int cmd __unused, void *data __unused)
291 {
292 	return (ENOTTY);
293 }
294 
295 static char *
296 comc_asprint_mode(struct serial *sp)
297 {
298 	char par, *buf;
299 
300 	if (sp == NULL)
301 		return (NULL);
302 
303 	if ((sp->lcr & (PAREN|PAREVN)) == (PAREN|PAREVN))
304 		par = 'e';
305 	else if ((sp->lcr & PAREN) == PAREN)
306 		par = 'o';
307 	else
308 		par = 'n';
309 
310 	asprintf(&buf, "%d,%d,%c,%d,-", sp->speed,
311 	    (sp->lcr & BITS8) == BITS8? 8:7,
312 	    par, (sp->lcr & STOP2) == STOP2? 2:1);
313 	return (buf);
314 }
315 
316 static int
317 comc_parse_mode(struct serial *sp, const char *value)
318 {
319 	unsigned long n;
320 	int speed;
321 	int lcr;
322 	char *ep;
323 
324 	if (value == NULL || *value == '\0')
325 		return (CMD_ERROR);
326 
327 	errno = 0;
328 	n = strtoul(value, &ep, 10);
329 	if (errno != 0 || *ep != ',')
330 		return (CMD_ERROR);
331 	speed = n;
332 
333 	ep++;
334 	errno = 0;
335 	n = strtoul(ep, &ep, 10);
336 	if (errno != 0 || *ep != ',')
337 		return (CMD_ERROR);
338 
339 	switch (n) {
340 	case 7: lcr = BITS7;
341 		break;
342 	case 8: lcr = BITS8;
343 		break;
344 	default:
345 		return (CMD_ERROR);
346 	}
347 
348 	ep++;
349 	switch (*ep++) {
350 	case 'n':
351 		break;
352 	case 'e': lcr |= PAREN|PAREVN;
353 		break;
354 	case 'o': lcr |= PAREN|PARODD;
355 		break;
356 	default:
357 		return (CMD_ERROR);
358 	}
359 
360 	if (*ep == ',')
361 		ep++;
362 	else
363 		return (CMD_ERROR);
364 
365 	switch (*ep++) {
366 	case '1':
367 		break;
368 	case '2': lcr |= STOP2;
369 		break;
370 	default:
371 		return (CMD_ERROR);
372 	}
373 
374 	/* handshake is ignored, but we check syntax anyhow */
375 	if (*ep == ',')
376 		ep++;
377 	else
378 		return (CMD_ERROR);
379 
380 	switch (*ep++) {
381 	case '-':
382 	case 'h':
383 	case 's':
384 		break;
385 	default:
386 		return (CMD_ERROR);
387 	}
388 
389 	if (*ep != '\0')
390 		return (CMD_ERROR);
391 
392 	sp->speed = speed;
393 	sp->lcr = lcr;
394 	return (CMD_OK);
395 }
396 
397 static struct console *
398 get_console(char *name)
399 {
400 	struct console *cp = NULL;
401 
402 	switch (name[3]) {
403 	case 'a': cp = &ttya;
404 		break;
405 	case 'b': cp = &ttyb;
406 		break;
407 	case 'c': cp = &ttyc;
408 		break;
409 	case 'd': cp = &ttyd;
410 		break;
411 	}
412 	return (cp);
413 }
414 
415 static int
416 comc_mode_set(struct env_var *ev, int flags, const void *value)
417 {
418 	struct console *cp;
419 	char name[15];
420 
421 	if (value == NULL)
422 		return (CMD_ERROR);
423 
424 	if ((cp = get_console(ev->ev_name)) == NULL)
425 		return (CMD_ERROR);
426 
427 	/* Do not override serial setup from SPCR */
428 	snprintf(name, sizeof (name), "%s-spcr-mode", cp->c_name);
429 	if (getenv(name) == NULL) {
430 		if (comc_parse_mode(cp->c_private, value) == CMD_ERROR)
431 			return (CMD_ERROR);
432 		(void) comc_setup(cp);
433 		env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
434 	}
435 
436 	return (CMD_OK);
437 }
438 
439 static int
440 comc_cd_set(struct env_var *ev, int flags, const void *value)
441 {
442 	struct console *cp;
443 	struct serial *sp;
444 
445 	if (value == NULL)
446 		return (CMD_ERROR);
447 
448 	if ((cp = get_console(ev->ev_name)) == NULL)
449 		return (CMD_ERROR);
450 
451 	sp = cp->c_private;
452 	if (strcmp(value, "true") == 0)
453 		sp->ignore_cd = 1;
454 	else if (strcmp(value, "false") == 0)
455 		sp->ignore_cd = 0;
456 	else
457 		return (CMD_ERROR);
458 
459 	(void) comc_setup(cp);
460 
461 	env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
462 
463 	return (CMD_OK);
464 }
465 
466 static int
467 comc_rtsdtr_set(struct env_var *ev, int flags, const void *value)
468 {
469 	struct console *cp;
470 	struct serial *sp;
471 
472 	if (value == NULL)
473 		return (CMD_ERROR);
474 
475 	if ((cp = get_console(ev->ev_name)) == NULL)
476 		return (CMD_ERROR);
477 
478 	sp = cp->c_private;
479 	if (strcmp(value, "true") == 0)
480 		sp->rtsdtr_off = 1;
481 	else if (strcmp(value, "false") == 0)
482 		sp->rtsdtr_off = 0;
483 	else
484 		return (CMD_ERROR);
485 
486 	(void) comc_setup(cp);
487 
488 	env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
489 
490 	return (CMD_OK);
491 }
492 
493 /*
494  * Input: bus:dev:func[:bar]. If bar is not specified, it is 0x10.
495  * Output: bar[24:16] bus[15:8] dev[7:3] func[2:0]
496  */
497 static uint32_t
498 comc_parse_pcidev(const char *string)
499 {
500 #ifdef EFI
501 	(void) string;
502 	return (0);
503 #else
504 	char *p, *p1;
505 	uint8_t bus, dev, func, bar;
506 	uint32_t locator;
507 	int pres;
508 
509 	errno = 0;
510 	pres = strtoul(string, &p, 10);
511 	if (errno != 0 || p == string || *p != ':' || pres < 0)
512 		return (0);
513 	bus = pres;
514 	p1 = ++p;
515 
516 	pres = strtoul(p1, &p, 10);
517 	if (errno != 0 || p == string || *p != ':' || pres < 0)
518 		return (0);
519 	dev = pres;
520 	p1 = ++p;
521 
522 	pres = strtoul(p1, &p, 10);
523 	if (errno != 0 || p == string || (*p != ':' && *p != '\0') || pres < 0)
524 		return (0);
525 	func = pres;
526 
527 	if (*p == ':') {
528 		p1 = ++p;
529 		pres = strtoul(p1, &p, 10);
530 		if (errno != 0 || p == string || *p != '\0' || pres <= 0)
531 			return (0);
532 		bar = pres;
533 	} else
534 		bar = 0x10;
535 
536 	locator = (bar << 16) | biospci_locator(bus, dev, func);
537 	return (locator);
538 #endif
539 }
540 
541 static int
542 comc_pcidev_handle(struct console *cp, uint32_t locator)
543 {
544 #ifdef EFI
545 	(void) cp;
546 	(void) locator;
547 	return (CMD_ERROR);
548 #else
549 	struct serial *sp = cp->c_private;
550 	uint32_t port;
551 
552 	if (biospci_read_config(locator & 0xffff,
553 	    (locator & 0xff0000) >> 16, 2, &port) == -1) {
554 		printf("Cannot read bar at 0x%x\n", locator);
555 		return (CMD_ERROR);
556 	}
557 	if (!PCI_BAR_IO(port)) {
558 		printf("Memory bar at 0x%x\n", locator);
559 		return (CMD_ERROR);
560 	}
561 	port &= PCIM_BAR_IO_BASE;
562 
563 	(void) comc_setup(cp);
564 
565 	sp->locator = locator;
566 
567 	return (CMD_OK);
568 #endif
569 }
570 
571 static int
572 comc_pcidev_set(struct env_var *ev, int flags, const void *value)
573 {
574 	struct console *cp;
575 	struct serial *sp;
576 	uint32_t locator;
577 	int error;
578 
579 	if ((cp = get_console(ev->ev_name)) == NULL)
580 		return (CMD_ERROR);
581 	sp = cp->c_private;
582 
583 	if (value == NULL || (locator = comc_parse_pcidev(value)) <= 0) {
584 		printf("Invalid pcidev\n");
585 		return (CMD_ERROR);
586 	}
587 	if ((cp->c_flags & (C_ACTIVEIN | C_ACTIVEOUT)) != 0 &&
588 	    sp->locator != locator) {
589 		error = comc_pcidev_handle(cp, locator);
590 		if (error != CMD_OK)
591 			return (error);
592 	}
593 	env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
594 	return (CMD_OK);
595 }
596 
597 /*
598  * In case of error, we also reset ACTIVE flags, so the console
599  * framefork will try alternate consoles.
600  */
601 static bool
602 comc_setup(struct console *cp)
603 {
604 	struct serial *sp = cp->c_private;
605 	static int TRY_COUNT = 1000000;
606 	int tries;
607 
608 #define	COMC_TEST	0xbb
609 	/*
610 	 * Write byte to scratch register and read it out.
611 	 */
612 	outb(sp->ioaddr + com_scr, COMC_TEST);
613 	if (inb(sp->ioaddr + com_scr) != COMC_TEST)
614 		return (false);
615 
616 	outb(sp->ioaddr + com_cfcr, CFCR_DLAB | sp->lcr);
617 	outb(sp->ioaddr + com_dlbl, COMC_BPS(sp->speed) & 0xff);
618 	outb(sp->ioaddr + com_dlbh, COMC_BPS(sp->speed) >> 8);
619 	outb(sp->ioaddr + com_cfcr, sp->lcr);
620 	outb(sp->ioaddr + com_mcr,
621 	    sp->rtsdtr_off? ~(MCR_RTS | MCR_DTR) : MCR_RTS | MCR_DTR);
622 
623 	tries = 0;
624 	do {
625 		inb(sp->ioaddr + com_data);
626 	} while (inb(sp->ioaddr + com_lsr) & LSR_RXRDY && ++tries < TRY_COUNT);
627 
628 	if (tries == TRY_COUNT)
629 		return (false);
630 	/* Mark this port usable. */
631 	cp->c_flags |= (C_PRESENTIN | C_PRESENTOUT);
632 	return (true);
633 }
634 
635 int
636 comc_getspeed(int ioaddr)
637 {
638 	uint_t	divisor;
639 	uchar_t	dlbh;
640 	uchar_t	dlbl;
641 	uchar_t	cfcr;
642 
643 	cfcr = inb(ioaddr + com_cfcr);
644 	outb(ioaddr + com_cfcr, CFCR_DLAB | cfcr);
645 
646 	dlbl = inb(ioaddr + com_dlbl);
647 	dlbh = inb(ioaddr + com_dlbh);
648 
649 	outb(ioaddr + com_cfcr, cfcr);
650 
651 	divisor = dlbh << 8 | dlbl;
652 
653 	/* XXX there should be more sanity checking. */
654 	if (divisor == 0)
655 		return (COMSPEED);
656 	return (COMC_DIV2BPS(divisor));
657 }
658