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>
33 #include <dev/pci/pcireg.h>
36 #define COMC_FMT 0x3 /* 8N1 */
37 #define COMC_TXWAIT 0x40000 /* transmit timeout */
38 #define COMC_BPS(x) (115200 / (x)) /* speed to DLAB divisor */
39 #define COMC_DIV2BPS(x) (115200 / (x)) /* DLAB divisor to speed */
48 static void comc_probe(struct console *cp);
49 static int comc_init(int arg);
50 static void comc_putchar(int c);
51 static int comc_getchar(void);
52 static int comc_getspeed(void);
53 static void set_hw_console_hint(void);
54 static int comc_ischar(void);
55 static int comc_parseint(const char *string);
56 static uint32_t comc_parse_pcidev(const char *string);
57 static int comc_pcidev_set(struct env_var *ev, int flags,
59 static int comc_pcidev_handle(uint32_t locator);
60 static int comc_port_set(struct env_var *ev, int flags,
62 static void comc_setup(int speed, int port);
63 static int comc_speed_set(struct env_var *ev, int flags,
66 static int comc_started;
67 static int comc_curspeed;
68 static int comc_port = COMPORT;
69 static uint32_t comc_locator;
71 struct console comconsole = {
83 comc_probe(struct console *cp)
90 /* XXX check the BIOS equipment list? */
91 cp->c_flags |= (C_PRESENTIN | C_PRESENTOUT);
93 if (comc_curspeed == 0) {
94 comc_curspeed = COMSPEED;
96 * Assume that the speed was set by an earlier boot loader if
97 * comconsole is already the preferred console.
99 cons = getenv("console");
100 if ((cons != NULL && strcmp(cons, comconsole.c_name) == 0) ||
101 getenv("boot_multicons") != NULL) {
102 comc_curspeed = comc_getspeed();
105 env = getenv("comconsole_speed");
107 speed = comc_parseint(env);
109 comc_curspeed = speed;
112 sprintf(intbuf, "%d", comc_curspeed);
113 unsetenv("comconsole_speed");
114 env_setenv("comconsole_speed", EV_VOLATILE, intbuf, comc_speed_set,
117 env = getenv("comconsole_port");
119 port = comc_parseint(env);
124 sprintf(intbuf, "%d", comc_port);
125 unsetenv("comconsole_port");
126 env_setenv("comconsole_port", EV_VOLATILE, intbuf, comc_port_set,
129 env = getenv("comconsole_pcidev");
131 locator = comc_parse_pcidev(env);
133 comc_pcidev_handle(locator);
136 unsetenv("comconsole_pcidev");
137 env_setenv("comconsole_pcidev", EV_VOLATILE, env, comc_pcidev_set,
145 if (comc_started && arg == 0)
149 comc_setup(comc_curspeed, comc_port);
159 for (wait = COMC_TXWAIT; wait > 0; wait--)
160 if (inb(comc_port + com_lsr) & LSR_TXRDY) {
161 outb(comc_port + com_data, (u_char)c);
169 return(comc_ischar() ? inb(comc_port + com_data) : -1);
175 return(inb(comc_port + com_lsr) & LSR_RXRDY);
179 comc_speed_set(struct env_var *ev, int flags, const void *value)
183 if (value == NULL || (speed = comc_parseint(value)) <= 0) {
184 printf("Invalid speed\n");
188 if (comc_started && comc_curspeed != speed)
189 comc_setup(speed, comc_port);
191 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
197 comc_port_set(struct env_var *ev, int flags, const void *value)
201 if (value == NULL || (port = comc_parseint(value)) <= 0) {
202 printf("Invalid port\n");
206 if (comc_started && comc_port != port) {
207 comc_setup(comc_curspeed, port);
208 set_hw_console_hint();
211 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
217 set_hw_console_hint(void)
221 unsetenv("hw.uart.console");
222 sprintf(intbuf, "io:%d,br:%d", comc_port, comc_curspeed);
223 env_setenv("hw.uart.console", EV_VOLATILE, intbuf,
224 env_noset, env_nounset);
228 * Input: bus:dev:func[:bar]. If bar is not specified, it is 0x10.
229 * Output: bar[24:16] bus[15:8] dev[7:3] func[2:0]
232 comc_parse_pcidev(const char *string)
235 uint8_t bus, dev, func, bar;
239 pres = strtol(string, &p, 0);
240 if (p == string || *p != ':' || pres < 0 )
245 pres = strtol(p1, &p, 0);
246 if (p == string || *p != ':' || pres < 0 )
251 pres = strtol(p1, &p, 0);
252 if (p == string || (*p != ':' && *p != '\0') || pres < 0 )
258 pres = strtol(p1, &p, 0);
259 if (p == string || *p != '\0' || pres <= 0 )
265 locator = (bar << 16) | biospci_locator(bus, dev, func);
270 comc_pcidev_handle(uint32_t locator)
275 if (biospci_read_config(locator & 0xffff,
276 (locator & 0xff0000) >> 16, 2, &port) == -1) {
277 printf("Cannot read bar at 0x%x\n", locator);
280 if (!PCI_BAR_IO(port)) {
281 printf("Memory bar at 0x%x\n", locator);
284 port &= PCIM_BAR_IO_BASE;
286 sprintf(intbuf, "%d", port);
287 unsetenv("comconsole_port");
288 env_setenv("comconsole_port", EV_VOLATILE, intbuf,
289 comc_port_set, env_nounset);
291 comc_setup(comc_curspeed, port);
292 set_hw_console_hint();
293 comc_locator = locator;
299 comc_pcidev_set(struct env_var *ev, int flags, const void *value)
304 if (value == NULL || (locator = comc_parse_pcidev(value)) <= 0) {
305 printf("Invalid pcidev\n");
308 if (comc_started && comc_locator != locator) {
309 error = comc_pcidev_handle(locator);
313 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
318 comc_setup(int speed, int port)
321 comc_curspeed = speed;
324 outb(comc_port + com_cfcr, CFCR_DLAB | COMC_FMT);
325 outb(comc_port + com_dlbl, COMC_BPS(speed) & 0xff);
326 outb(comc_port + com_dlbh, COMC_BPS(speed) >> 8);
327 outb(comc_port + com_cfcr, COMC_FMT);
328 outb(comc_port + com_mcr, MCR_RTS | MCR_DTR);
331 inb(comc_port + com_data);
332 while (inb(comc_port + com_lsr) & LSR_RXRDY);
336 comc_parseint(const char *speedstr)
341 speed = strtol(speedstr, &p, 0);
342 if (p == speedstr || *p != '\0' || speed <= 0)
356 cfcr = inb(comc_port + com_cfcr);
357 outb(comc_port + com_cfcr, CFCR_DLAB | cfcr);
359 dlbl = inb(comc_port + com_dlbl);
360 dlbh = inb(comc_port + com_dlbh);
362 outb(comc_port + com_cfcr, cfcr);
364 divisor = dlbh << 8 | dlbl;
366 /* XXX there should be more sanity checking. */
369 return (COMC_DIV2BPS(divisor));