2 * Copyright (c) 1998 Michael Smith (msmith@freebsd.org)
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
30 #include <bootstrap.h>
31 #include <machine/cpufunc.h>
32 #include <dev/ic/ns16550.h>
35 #define COMC_FMT 0x3 /* 8N1 */
36 #define COMC_TXWAIT 0x40000 /* transmit timeout */
37 #define COMC_BPS(x) (115200 / (x)) /* speed to DLAB divisor */
38 #define COMC_DIV2BPS(x) (115200 / (x)) /* DLAB divisor to speed */
47 static void comc_probe(struct console *cp);
48 static int comc_init(int arg);
49 static void comc_putchar(int c);
50 static int comc_getchar(void);
51 static int comc_getspeed(void);
52 static int comc_ischar(void);
53 static int comc_parsespeed(const char *string);
54 static void comc_setup(int speed);
55 static int comc_speed_set(struct env_var *ev, int flags,
58 static int comc_started;
59 static int comc_curspeed;
61 struct console comconsole = {
73 comc_probe(struct console *cp)
76 char *cons, *speedenv;
79 /* XXX check the BIOS equipment list? */
80 cp->c_flags |= (C_PRESENTIN | C_PRESENTOUT);
82 if (comc_curspeed == 0) {
83 comc_curspeed = COMSPEED;
85 * Assume that the speed was set by an earlier boot loader if
86 * comconsole is already the preferred console.
88 cons = getenv("console");
89 if ((cons != NULL && strcmp(cons, comconsole.c_name) == 0) ||
90 getenv("boot_multicons") != NULL) {
91 comc_curspeed = comc_getspeed();
93 speedenv = getenv("comconsole_speed");
94 if (speedenv != NULL) {
95 speed = comc_parsespeed(speedenv);
97 comc_curspeed = speed;
100 sprintf(speedbuf, "%d", comc_curspeed);
101 unsetenv("comconsole_speed");
102 env_setenv("comconsole_speed", EV_VOLATILE, speedbuf, comc_speed_set,
110 if (comc_started && arg == 0)
114 comc_setup(comc_curspeed);
124 for (wait = COMC_TXWAIT; wait > 0; wait--)
125 if (inb(COMPORT + com_lsr) & LSR_TXRDY) {
126 outb(COMPORT + com_data, (u_char)c);
134 return(comc_ischar() ? inb(COMPORT + com_data) : -1);
140 return(inb(COMPORT + com_lsr) & LSR_RXRDY);
144 comc_speed_set(struct env_var *ev, int flags, const void *value)
148 if (value == NULL || (speed = comc_parsespeed(value)) <= 0) {
149 printf("Invalid speed\n");
153 if (comc_started && comc_curspeed != speed)
156 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
162 comc_setup(int speed)
165 comc_curspeed = speed;
167 outb(COMPORT + com_cfcr, CFCR_DLAB | COMC_FMT);
168 outb(COMPORT + com_dlbl, COMC_BPS(speed) & 0xff);
169 outb(COMPORT + com_dlbh, COMC_BPS(speed) >> 8);
170 outb(COMPORT + com_cfcr, COMC_FMT);
171 outb(COMPORT + com_mcr, MCR_RTS | MCR_DTR);
174 inb(COMPORT + com_data);
175 while (inb(COMPORT + com_lsr) & LSR_RXRDY);
179 comc_parsespeed(const char *speedstr)
184 speed = strtol(speedstr, &p, 0);
185 if (p == speedstr || *p != '\0' || speed <= 0)
199 cfcr = inb(COMPORT + com_cfcr);
200 outb(COMPORT + com_cfcr, CFCR_DLAB | cfcr);
202 dlbl = inb(COMPORT + com_dlbl);
203 dlbh = inb(COMPORT + com_dlbh);
205 outb(COMPORT + com_cfcr, cfcr);
207 divisor = dlbh << 8 | dlbl;
209 /* XXX there should be more sanity checking. */
212 return (COMC_DIV2BPS(divisor));