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 int comc_ischar(void);
54 static int comc_parseint(const char *string);
55 static uint32_t comc_parse_pcidev(const char *string);
56 static int comc_pcidev_set(struct env_var *ev, int flags,
58 static int comc_pcidev_handle(uint32_t locator);
59 static int comc_port_set(struct env_var *ev, int flags,
61 static void comc_setup(int speed, int port);
62 static int comc_speed_set(struct env_var *ev, int flags,
65 static int comc_curspeed;
66 static int comc_port = COMPORT;
67 static uint32_t comc_locator;
69 struct console comconsole = {
70 .c_name = "comconsole",
71 .c_desc = "serial port",
73 .c_probe = comc_probe,
75 .c_out = comc_putchar,
77 .c_ready = comc_ischar
81 comc_probe(struct console *cp)
88 if (comc_curspeed == 0) {
89 comc_curspeed = COMSPEED;
91 * Assume that the speed was set by an earlier boot loader if
92 * comconsole is already the preferred console.
94 cons = getenv("console");
95 if ((cons != NULL && strcmp(cons, comconsole.c_name) == 0) ||
96 getenv("boot_multicons") != NULL) {
97 comc_curspeed = comc_getspeed();
100 env = getenv("comconsole_speed");
102 speed = comc_parseint(env);
104 comc_curspeed = speed;
107 sprintf(intbuf, "%d", comc_curspeed);
108 unsetenv("comconsole_speed");
109 env_setenv("comconsole_speed", EV_VOLATILE, intbuf,
110 comc_speed_set, env_nounset);
112 env = getenv("comconsole_port");
114 port = comc_parseint(env);
119 sprintf(intbuf, "%d", comc_port);
120 unsetenv("comconsole_port");
121 env_setenv("comconsole_port", EV_VOLATILE, intbuf,
122 comc_port_set, env_nounset);
124 env = getenv("comconsole_pcidev");
126 locator = comc_parse_pcidev(env);
128 comc_pcidev_handle(locator);
131 unsetenv("comconsole_pcidev");
132 env_setenv("comconsole_pcidev", EV_VOLATILE, env,
133 comc_pcidev_set, env_nounset);
135 comc_setup(comc_curspeed, comc_port);
142 comc_setup(comc_curspeed, comc_port);
144 if ((comconsole.c_flags & (C_PRESENTIN | C_PRESENTOUT)) ==
145 (C_PRESENTIN | C_PRESENTOUT))
155 for (wait = COMC_TXWAIT; wait > 0; wait--)
156 if (inb(comc_port + com_lsr) & LSR_TXRDY) {
157 outb(comc_port + com_data, (u_char)c);
165 return (comc_ischar() ? inb(comc_port + com_data) : -1);
171 return (inb(comc_port + com_lsr) & LSR_RXRDY);
175 comc_speed_set(struct env_var *ev, int flags, const void *value)
179 if (value == NULL || (speed = comc_parseint(value)) <= 0) {
180 printf("Invalid speed\n");
184 if (comc_curspeed != speed)
185 comc_setup(speed, comc_port);
187 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
193 comc_port_set(struct env_var *ev, int flags, const void *value)
197 if (value == NULL || (port = comc_parseint(value)) <= 0) {
198 printf("Invalid port\n");
202 if (comc_port != port)
203 comc_setup(comc_curspeed, port);
205 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
211 * Input: bus:dev:func[:bar]. If bar is not specified, it is 0x10.
212 * Output: bar[24:16] bus[15:8] dev[7:3] func[2:0]
215 comc_parse_pcidev(const char *string)
218 /* We don't support PCI in EFI yet */
222 uint8_t bus, dev, func, bar;
226 pres = strtol(string, &p, 0);
227 if (p == string || *p != ':' || pres < 0 )
232 pres = strtol(p1, &p, 0);
233 if (p == string || *p != ':' || pres < 0 )
238 pres = strtol(p1, &p, 0);
239 if (p == string || (*p != ':' && *p != '\0') || pres < 0 )
245 pres = strtol(p1, &p, 0);
246 if (p == string || *p != '\0' || pres <= 0 )
252 locator = (bar << 16) | biospci_locator(bus, dev, func);
258 comc_pcidev_handle(uint32_t locator)
261 /* We don't support PCI in EFI yet */
267 if (biospci_read_config(locator & 0xffff,
268 (locator & 0xff0000) >> 16, BIOSPCI_32BITS, &port) == -1) {
269 printf("Cannot read bar at 0x%x\n", locator);
274 * biospci_read_config() sets port == 0xffffffff if the pcidev
275 * isn't found on the bus. Check for 0xffffffff and return to not
278 if (port == 0xffffffff) {
279 printf("Cannot find specified pcidev\n");
282 if (!PCI_BAR_IO(port)) {
283 printf("Memory bar at 0x%x\n", locator);
286 port &= PCIM_BAR_IO_BASE;
288 sprintf(intbuf, "%d", port);
289 unsetenv("comconsole_port");
290 env_setenv("comconsole_port", EV_VOLATILE, intbuf,
291 comc_port_set, env_nounset);
293 comc_setup(comc_curspeed, port);
294 comc_locator = locator;
301 comc_pcidev_set(struct env_var *ev, int flags, const void *value)
306 if (value == NULL || (locator = comc_parse_pcidev(value)) <= 0) {
307 printf("Invalid pcidev\n");
310 if ((comconsole.c_flags & (C_ACTIVEIN | C_ACTIVEOUT)) != 0 &&
311 comc_locator != locator) {
312 error = comc_pcidev_handle(locator);
316 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
321 comc_setup(int speed, int port)
323 static int TRY_COUNT = 1000000;
327 unsetenv("hw.uart.console");
328 comc_curspeed = speed;
330 if ((comconsole.c_flags & (C_ACTIVEIN | C_ACTIVEOUT)) == 0)
333 #define COMC_TEST 0xbb
335 * Write byte to scratch register and read it out.
337 outb(comc_port + com_scr, COMC_TEST);
338 if (inb(comc_port + com_scr) != COMC_TEST) {
339 comconsole.c_flags &= ~(C_PRESENTIN | C_PRESENTOUT);
343 outb(comc_port + com_cfcr, CFCR_DLAB | COMC_FMT);
344 outb(comc_port + com_dlbl, COMC_BPS(speed) & 0xff);
345 outb(comc_port + com_dlbh, COMC_BPS(speed) >> 8);
346 outb(comc_port + com_cfcr, COMC_FMT);
347 outb(comc_port + com_mcr, MCR_RTS | MCR_DTR);
351 inb(comc_port + com_data);
352 while (inb(comc_port + com_lsr) & LSR_RXRDY && ++tries < TRY_COUNT);
354 if (tries < TRY_COUNT) {
355 comconsole.c_flags |= (C_PRESENTIN | C_PRESENTOUT);
356 sprintf(intbuf, "io:%d,br:%d", comc_port, comc_curspeed);
357 env_setenv("hw.uart.console", EV_VOLATILE, intbuf, NULL, NULL);
359 comconsole.c_flags &= ~(C_PRESENTIN | C_PRESENTOUT);
363 comc_parseint(const char *speedstr)
368 speed = strtol(speedstr, &p, 0);
369 if (p == speedstr || *p != '\0' || speed <= 0)
383 cfcr = inb(comc_port + com_cfcr);
384 outb(comc_port + com_cfcr, CFCR_DLAB | cfcr);
386 dlbl = inb(comc_port + com_dlbl);
387 dlbh = inb(comc_port + com_dlbh);
389 outb(comc_port + com_cfcr, cfcr);
391 divisor = dlbh << 8 | dlbl;
393 /* XXX there should be more sanity checking. */
396 return (COMC_DIV2BPS(divisor));