2003-08-25 23:28:32 +00:00
|
|
|
/*-
|
1998-08-21 03:17:42 +00:00
|
|
|
* Copyright (c) 1998 Michael Smith (msmith@freebsd.org)
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2003-08-25 23:28:32 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1998-08-21 03:17:42 +00:00
|
|
|
#include <stand.h>
|
1998-09-17 23:52:16 +00:00
|
|
|
#include <bootstrap.h>
|
1998-11-22 07:59:16 +00:00
|
|
|
#include <machine/cpufunc.h>
|
2003-09-16 11:24:23 +00:00
|
|
|
#include <dev/ic/ns16550.h>
|
2012-01-03 22:36:12 +00:00
|
|
|
#include <dev/pci/pcireg.h>
|
1998-09-17 23:52:16 +00:00
|
|
|
#include "libi386.h"
|
1998-08-21 03:17:42 +00:00
|
|
|
|
1998-11-22 07:59:16 +00:00
|
|
|
#define COMC_FMT 0x3 /* 8N1 */
|
|
|
|
#define COMC_TXWAIT 0x40000 /* transmit timeout */
|
|
|
|
#define COMC_BPS(x) (115200 / (x)) /* speed to DLAB divisor */
|
2005-08-18 01:39:43 +00:00
|
|
|
#define COMC_DIV2BPS(x) (115200 / (x)) /* DLAB divisor to speed */
|
1998-11-22 07:59:16 +00:00
|
|
|
|
1999-01-10 14:48:05 +00:00
|
|
|
#ifndef COMPORT
|
1998-11-22 07:59:16 +00:00
|
|
|
#define COMPORT 0x3f8
|
1999-01-10 14:48:05 +00:00
|
|
|
#endif
|
|
|
|
#ifndef COMSPEED
|
1998-11-22 07:59:16 +00:00
|
|
|
#define COMSPEED 9600
|
1999-01-10 14:48:05 +00:00
|
|
|
#endif
|
1998-11-22 07:59:16 +00:00
|
|
|
|
1998-08-21 03:17:42 +00:00
|
|
|
static void comc_probe(struct console *cp);
|
|
|
|
static int comc_init(int arg);
|
1998-09-17 23:52:16 +00:00
|
|
|
static void comc_putchar(int c);
|
|
|
|
static int comc_getchar(void);
|
2005-08-18 01:39:43 +00:00
|
|
|
static int comc_getspeed(void);
|
1998-09-17 23:52:16 +00:00
|
|
|
static int comc_ischar(void);
|
2012-01-03 22:36:12 +00:00
|
|
|
static int comc_parseint(const char *string);
|
|
|
|
static uint32_t comc_parse_pcidev(const char *string);
|
|
|
|
static int comc_pcidev_set(struct env_var *ev, int flags,
|
|
|
|
const void *value);
|
|
|
|
static int comc_pcidev_handle(uint32_t locator);
|
|
|
|
static int comc_port_set(struct env_var *ev, int flags,
|
|
|
|
const void *value);
|
|
|
|
static void comc_setup(int speed, int port);
|
2005-08-18 01:39:43 +00:00
|
|
|
static int comc_speed_set(struct env_var *ev, int flags,
|
|
|
|
const void *value);
|
1998-08-21 03:17:42 +00:00
|
|
|
|
2005-08-18 01:39:43 +00:00
|
|
|
static int comc_curspeed;
|
2012-01-03 22:36:12 +00:00
|
|
|
static int comc_port = COMPORT;
|
|
|
|
static uint32_t comc_locator;
|
1998-10-11 10:05:13 +00:00
|
|
|
|
1998-08-21 03:17:42 +00:00
|
|
|
struct console comconsole = {
|
|
|
|
"comconsole",
|
1998-11-22 07:59:16 +00:00
|
|
|
"serial port",
|
1998-08-21 03:17:42 +00:00
|
|
|
0,
|
|
|
|
comc_probe,
|
|
|
|
comc_init,
|
1998-09-17 23:52:16 +00:00
|
|
|
comc_putchar,
|
|
|
|
comc_getchar,
|
|
|
|
comc_ischar
|
1998-08-21 03:17:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
comc_probe(struct console *cp)
|
|
|
|
{
|
2012-01-03 22:36:12 +00:00
|
|
|
char intbuf[16];
|
|
|
|
char *cons, *env;
|
|
|
|
int speed, port;
|
|
|
|
uint32_t locator;
|
2005-08-18 01:39:43 +00:00
|
|
|
|
|
|
|
if (comc_curspeed == 0) {
|
|
|
|
comc_curspeed = COMSPEED;
|
|
|
|
/*
|
|
|
|
* Assume that the speed was set by an earlier boot loader if
|
|
|
|
* comconsole is already the preferred console.
|
|
|
|
*/
|
|
|
|
cons = getenv("console");
|
|
|
|
if ((cons != NULL && strcmp(cons, comconsole.c_name) == 0) ||
|
|
|
|
getenv("boot_multicons") != NULL) {
|
|
|
|
comc_curspeed = comc_getspeed();
|
|
|
|
}
|
2012-01-03 22:36:12 +00:00
|
|
|
|
|
|
|
env = getenv("comconsole_speed");
|
|
|
|
if (env != NULL) {
|
|
|
|
speed = comc_parseint(env);
|
2005-08-18 01:39:43 +00:00
|
|
|
if (speed > 0)
|
|
|
|
comc_curspeed = speed;
|
|
|
|
}
|
|
|
|
|
2012-01-03 22:36:12 +00:00
|
|
|
sprintf(intbuf, "%d", comc_curspeed);
|
2005-08-18 01:39:43 +00:00
|
|
|
unsetenv("comconsole_speed");
|
2012-01-03 22:36:12 +00:00
|
|
|
env_setenv("comconsole_speed", EV_VOLATILE, intbuf, comc_speed_set,
|
|
|
|
env_nounset);
|
|
|
|
|
|
|
|
env = getenv("comconsole_port");
|
|
|
|
if (env != NULL) {
|
|
|
|
port = comc_parseint(env);
|
|
|
|
if (port > 0)
|
|
|
|
comc_port = port;
|
|
|
|
}
|
|
|
|
|
|
|
|
sprintf(intbuf, "%d", comc_port);
|
|
|
|
unsetenv("comconsole_port");
|
|
|
|
env_setenv("comconsole_port", EV_VOLATILE, intbuf, comc_port_set,
|
|
|
|
env_nounset);
|
|
|
|
|
|
|
|
env = getenv("comconsole_pcidev");
|
|
|
|
if (env != NULL) {
|
|
|
|
locator = comc_parse_pcidev(env);
|
|
|
|
if (locator != 0)
|
|
|
|
comc_pcidev_handle(locator);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsetenv("comconsole_pcidev");
|
|
|
|
env_setenv("comconsole_pcidev", EV_VOLATILE, env, comc_pcidev_set,
|
2005-08-18 01:39:43 +00:00
|
|
|
env_nounset);
|
|
|
|
}
|
2012-10-06 20:04:51 +00:00
|
|
|
comc_setup(comc_curspeed, comc_port);
|
1998-08-21 03:17:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
comc_init(int arg)
|
|
|
|
{
|
1998-10-11 10:05:13 +00:00
|
|
|
|
2012-01-03 22:36:12 +00:00
|
|
|
comc_setup(comc_curspeed, comc_port);
|
1998-10-11 10:05:13 +00:00
|
|
|
|
2012-10-06 20:04:51 +00:00
|
|
|
if ((comconsole.c_flags & (C_PRESENTIN | C_PRESENTOUT)) ==
|
|
|
|
(C_PRESENTIN | C_PRESENTOUT))
|
|
|
|
return (CMD_OK);
|
|
|
|
return (CMD_ERROR);
|
1998-09-17 23:52:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
comc_putchar(int c)
|
|
|
|
{
|
1998-11-22 07:59:16 +00:00
|
|
|
int wait;
|
|
|
|
|
|
|
|
for (wait = COMC_TXWAIT; wait > 0; wait--)
|
2012-01-03 22:36:12 +00:00
|
|
|
if (inb(comc_port + com_lsr) & LSR_TXRDY) {
|
|
|
|
outb(comc_port + com_data, (u_char)c);
|
1998-11-22 07:59:16 +00:00
|
|
|
break;
|
|
|
|
}
|
1998-08-21 03:17:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
1998-09-17 23:52:16 +00:00
|
|
|
comc_getchar(void)
|
1998-08-21 03:17:42 +00:00
|
|
|
{
|
2012-10-06 20:04:51 +00:00
|
|
|
return (comc_ischar() ? inb(comc_port + com_data) : -1);
|
1998-08-21 03:17:42 +00:00
|
|
|
}
|
1998-09-17 23:52:16 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
comc_ischar(void)
|
|
|
|
{
|
2012-10-06 20:04:51 +00:00
|
|
|
return (inb(comc_port + com_lsr) & LSR_RXRDY);
|
1998-09-17 23:52:16 +00:00
|
|
|
}
|
2005-08-18 01:39:43 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
comc_speed_set(struct env_var *ev, int flags, const void *value)
|
|
|
|
{
|
|
|
|
int speed;
|
|
|
|
|
2012-01-03 22:36:12 +00:00
|
|
|
if (value == NULL || (speed = comc_parseint(value)) <= 0) {
|
2005-08-18 01:39:43 +00:00
|
|
|
printf("Invalid speed\n");
|
|
|
|
return (CMD_ERROR);
|
|
|
|
}
|
|
|
|
|
2014-01-20 23:27:05 +00:00
|
|
|
if (comc_curspeed != speed)
|
2012-01-03 22:36:12 +00:00
|
|
|
comc_setup(speed, comc_port);
|
|
|
|
|
|
|
|
env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
|
|
|
|
|
|
|
|
return (CMD_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
comc_port_set(struct env_var *ev, int flags, const void *value)
|
|
|
|
{
|
|
|
|
int port;
|
|
|
|
|
|
|
|
if (value == NULL || (port = comc_parseint(value)) <= 0) {
|
|
|
|
printf("Invalid port\n");
|
|
|
|
return (CMD_ERROR);
|
|
|
|
}
|
|
|
|
|
2014-01-20 23:27:05 +00:00
|
|
|
if (comc_port != port)
|
2012-01-03 22:36:12 +00:00
|
|
|
comc_setup(comc_curspeed, port);
|
2005-08-18 01:39:43 +00:00
|
|
|
|
|
|
|
env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
|
|
|
|
|
|
|
|
return (CMD_OK);
|
|
|
|
}
|
|
|
|
|
2012-01-03 22:36:12 +00:00
|
|
|
/*
|
|
|
|
* Input: bus:dev:func[:bar]. If bar is not specified, it is 0x10.
|
|
|
|
* Output: bar[24:16] bus[15:8] dev[7:3] func[2:0]
|
|
|
|
*/
|
|
|
|
static uint32_t
|
|
|
|
comc_parse_pcidev(const char *string)
|
|
|
|
{
|
2014-09-12 17:32:28 +00:00
|
|
|
#ifdef NO_PCI
|
|
|
|
return (0);
|
|
|
|
#else
|
2012-01-03 22:36:12 +00:00
|
|
|
char *p, *p1;
|
|
|
|
uint8_t bus, dev, func, bar;
|
|
|
|
uint32_t locator;
|
|
|
|
int pres;
|
|
|
|
|
|
|
|
pres = strtol(string, &p, 0);
|
|
|
|
if (p == string || *p != ':' || pres < 0 )
|
|
|
|
return (0);
|
|
|
|
bus = pres;
|
|
|
|
p1 = ++p;
|
|
|
|
|
|
|
|
pres = strtol(p1, &p, 0);
|
|
|
|
if (p == string || *p != ':' || pres < 0 )
|
|
|
|
return (0);
|
|
|
|
dev = pres;
|
|
|
|
p1 = ++p;
|
|
|
|
|
|
|
|
pres = strtol(p1, &p, 0);
|
|
|
|
if (p == string || (*p != ':' && *p != '\0') || pres < 0 )
|
|
|
|
return (0);
|
|
|
|
func = pres;
|
|
|
|
|
|
|
|
if (*p == ':') {
|
|
|
|
p1 = ++p;
|
|
|
|
pres = strtol(p1, &p, 0);
|
|
|
|
if (p == string || *p != '\0' || pres <= 0 )
|
|
|
|
return (0);
|
|
|
|
bar = pres;
|
|
|
|
} else
|
|
|
|
bar = 0x10;
|
|
|
|
|
|
|
|
locator = (bar << 16) | biospci_locator(bus, dev, func);
|
|
|
|
return (locator);
|
2014-09-12 17:32:28 +00:00
|
|
|
#endif
|
2012-01-03 22:36:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
comc_pcidev_handle(uint32_t locator)
|
|
|
|
{
|
2014-09-12 17:32:28 +00:00
|
|
|
#ifdef NO_PCI
|
|
|
|
return (CMD_ERROR);
|
|
|
|
#else
|
2012-01-03 22:36:12 +00:00
|
|
|
char intbuf[64];
|
|
|
|
uint32_t port;
|
|
|
|
|
|
|
|
if (biospci_read_config(locator & 0xffff,
|
|
|
|
(locator & 0xff0000) >> 16, 2, &port) == -1) {
|
|
|
|
printf("Cannot read bar at 0x%x\n", locator);
|
|
|
|
return (CMD_ERROR);
|
|
|
|
}
|
|
|
|
if (!PCI_BAR_IO(port)) {
|
|
|
|
printf("Memory bar at 0x%x\n", locator);
|
|
|
|
return (CMD_ERROR);
|
|
|
|
}
|
|
|
|
port &= PCIM_BAR_IO_BASE;
|
|
|
|
|
|
|
|
sprintf(intbuf, "%d", port);
|
|
|
|
unsetenv("comconsole_port");
|
|
|
|
env_setenv("comconsole_port", EV_VOLATILE, intbuf,
|
|
|
|
comc_port_set, env_nounset);
|
|
|
|
|
|
|
|
comc_setup(comc_curspeed, port);
|
|
|
|
comc_locator = locator;
|
|
|
|
|
|
|
|
return (CMD_OK);
|
2014-09-12 17:32:28 +00:00
|
|
|
#endif
|
2012-01-03 22:36:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
comc_pcidev_set(struct env_var *ev, int flags, const void *value)
|
|
|
|
{
|
|
|
|
uint32_t locator;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
if (value == NULL || (locator = comc_parse_pcidev(value)) <= 0) {
|
|
|
|
printf("Invalid pcidev\n");
|
|
|
|
return (CMD_ERROR);
|
|
|
|
}
|
2012-10-06 20:04:51 +00:00
|
|
|
if ((comconsole.c_flags & (C_ACTIVEIN | C_ACTIVEOUT)) != 0 &&
|
|
|
|
comc_locator != locator) {
|
2012-01-03 22:36:12 +00:00
|
|
|
error = comc_pcidev_handle(locator);
|
|
|
|
if (error != CMD_OK)
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
|
|
|
|
return (CMD_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
comc_setup(int speed, int port)
|
2005-08-18 01:39:43 +00:00
|
|
|
{
|
2012-10-06 20:04:51 +00:00
|
|
|
static int TRY_COUNT = 1000000;
|
2013-01-23 18:34:21 +00:00
|
|
|
char intbuf[64];
|
2012-10-06 20:04:51 +00:00
|
|
|
int tries;
|
2005-08-18 01:39:43 +00:00
|
|
|
|
2013-01-23 18:34:21 +00:00
|
|
|
unsetenv("hw.uart.console");
|
2005-08-18 01:39:43 +00:00
|
|
|
comc_curspeed = speed;
|
2012-01-03 22:36:12 +00:00
|
|
|
comc_port = port;
|
2014-01-20 23:27:05 +00:00
|
|
|
if ((comconsole.c_flags & (C_ACTIVEIN | C_ACTIVEOUT)) == 0)
|
|
|
|
return;
|
2005-08-18 01:39:43 +00:00
|
|
|
|
2012-01-03 22:36:12 +00:00
|
|
|
outb(comc_port + com_cfcr, CFCR_DLAB | COMC_FMT);
|
|
|
|
outb(comc_port + com_dlbl, COMC_BPS(speed) & 0xff);
|
|
|
|
outb(comc_port + com_dlbh, COMC_BPS(speed) >> 8);
|
|
|
|
outb(comc_port + com_cfcr, COMC_FMT);
|
|
|
|
outb(comc_port + com_mcr, MCR_RTS | MCR_DTR);
|
2005-08-18 01:39:43 +00:00
|
|
|
|
2012-10-06 20:04:51 +00:00
|
|
|
tries = 0;
|
2005-08-18 01:39:43 +00:00
|
|
|
do
|
2012-01-03 22:36:12 +00:00
|
|
|
inb(comc_port + com_data);
|
2012-10-06 20:04:51 +00:00
|
|
|
while (inb(comc_port + com_lsr) & LSR_RXRDY && ++tries < TRY_COUNT);
|
|
|
|
|
2013-01-23 18:34:21 +00:00
|
|
|
if (tries < TRY_COUNT) {
|
2012-10-06 20:04:51 +00:00
|
|
|
comconsole.c_flags |= (C_PRESENTIN | C_PRESENTOUT);
|
2013-01-23 18:34:21 +00:00
|
|
|
sprintf(intbuf, "io:%d,br:%d", comc_port, comc_curspeed);
|
|
|
|
env_setenv("hw.uart.console", EV_VOLATILE, intbuf, NULL, NULL);
|
|
|
|
} else
|
2012-10-06 20:04:51 +00:00
|
|
|
comconsole.c_flags &= ~(C_PRESENTIN | C_PRESENTOUT);
|
2005-08-18 01:39:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2012-01-03 22:36:12 +00:00
|
|
|
comc_parseint(const char *speedstr)
|
2005-08-18 01:39:43 +00:00
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
int speed;
|
|
|
|
|
|
|
|
speed = strtol(speedstr, &p, 0);
|
|
|
|
if (p == speedstr || *p != '\0' || speed <= 0)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
return (speed);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
comc_getspeed(void)
|
|
|
|
{
|
|
|
|
u_int divisor;
|
|
|
|
u_char dlbh;
|
|
|
|
u_char dlbl;
|
|
|
|
u_char cfcr;
|
|
|
|
|
2012-01-03 22:36:12 +00:00
|
|
|
cfcr = inb(comc_port + com_cfcr);
|
|
|
|
outb(comc_port + com_cfcr, CFCR_DLAB | cfcr);
|
2005-08-18 01:39:43 +00:00
|
|
|
|
2012-01-03 22:36:12 +00:00
|
|
|
dlbl = inb(comc_port + com_dlbl);
|
|
|
|
dlbh = inb(comc_port + com_dlbh);
|
2005-08-18 01:39:43 +00:00
|
|
|
|
2012-01-03 22:36:12 +00:00
|
|
|
outb(comc_port + com_cfcr, cfcr);
|
2005-08-18 01:39:43 +00:00
|
|
|
|
|
|
|
divisor = dlbh << 8 | dlbl;
|
|
|
|
|
|
|
|
/* XXX there should be more sanity checking. */
|
|
|
|
if (divisor == 0)
|
|
|
|
return (COMSPEED);
|
|
|
|
return (COMC_DIV2BPS(divisor));
|
|
|
|
}
|