comconsole.c revision 41285
138465Smsmith/*
238465Smsmith * Copyright (c) 1998 Michael Smith (msmith@freebsd.org)
338465Smsmith *
438465Smsmith * Redistribution and use in source and binary forms, with or without
538465Smsmith * modification, are permitted provided that the following conditions
638465Smsmith * are met:
738465Smsmith * 1. Redistributions of source code must retain the above copyright
838465Smsmith *    notice, this list of conditions and the following disclaimer.
938465Smsmith * 2. Redistributions in binary form must reproduce the above copyright
1038465Smsmith *    notice, this list of conditions and the following disclaimer in the
1138465Smsmith *    documentation and/or other materials provided with the distribution.
1238465Smsmith *
1338465Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1438465Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1538465Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1638465Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1738465Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1838465Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1938465Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2038465Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2138465Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2238465Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2338465Smsmith * SUCH DAMAGE.
2438465Smsmith *
2541285Srnordier *	$Id: comconsole.c,v 1.4 1998/10/11 10:05:13 peter Exp $
2638465Smsmith */
2738465Smsmith
2838465Smsmith#include <stand.h>
2939441Smsmith#include <bootstrap.h>
3041285Srnordier#include <machine/cpufunc.h>
3139441Smsmith#include "libi386.h"
3238465Smsmith
3341285Srnordier/* selected defines from ns16550.h */
3441285Srnordier#define	com_data	0	/* data register (R/W) */
3541285Srnordier#define	com_dlbl	0	/* divisor latch low (W) */
3641285Srnordier#define	com_dlbh	1	/* divisor latch high (W) */
3741285Srnordier#define	com_ier		1	/* interrupt enable (W) */
3841285Srnordier#define	com_iir		2	/* interrupt identification (R) */
3941285Srnordier#define	com_fifo	2	/* FIFO control (W) */
4041285Srnordier#define	com_lctl	3	/* line control register (R/W) */
4141285Srnordier#define	com_cfcr	3	/* line control register (R/W) */
4241285Srnordier#define	com_mcr		4	/* modem control register (R/W) */
4341285Srnordier#define	com_lsr		5	/* line status register (R/W) */
4441285Srnordier#define	com_msr		6	/* modem status register (R/W) */
4541285Srnordier
4641285Srnordier/* selected defines from sioreg.h */
4741285Srnordier#define	CFCR_DLAB	0x80
4841285Srnordier#define	MCR_RTS		0x02
4941285Srnordier#define	MCR_DTR		0x01
5041285Srnordier#define	LSR_TXRDY	0x20
5141285Srnordier#define	LSR_RXRDY	0x01
5241285Srnordier
5341285Srnordier#define COMC_FMT	0x3		/* 8N1 */
5441285Srnordier#define COMC_TXWAIT	0x40000		/* transmit timeout */
5541285Srnordier#define COMC_BPS(x)	(115200 / (x))	/* speed to DLAB divisor */
5641285Srnordier
5741285Srnordier#define COMPORT		0x3f8
5841285Srnordier#define COMSPEED	9600
5941285Srnordier
6038465Smsmithstatic void	comc_probe(struct console *cp);
6138465Smsmithstatic int	comc_init(int arg);
6239441Smsmithstatic void	comc_putchar(int c);
6339441Smsmithstatic int	comc_getchar(void);
6439441Smsmithstatic int	comc_ischar(void);
6538465Smsmith
6640211Speterstatic int	comc_started;
6740211Speter
6838465Smsmithstruct console comconsole = {
6938465Smsmith    "comconsole",
7041285Srnordier    "serial port",
7138465Smsmith    0,
7238465Smsmith    comc_probe,
7338465Smsmith    comc_init,
7439441Smsmith    comc_putchar,
7539441Smsmith    comc_getchar,
7639441Smsmith    comc_ischar
7738465Smsmith};
7838465Smsmith
7938465Smsmithstatic void
8038465Smsmithcomc_probe(struct console *cp)
8138465Smsmith{
8238465Smsmith    /* XXX check the BIOS equipment list? */
8338465Smsmith    cp->c_flags |= (C_PRESENTIN | C_PRESENTOUT);
8438465Smsmith}
8538465Smsmith
8638465Smsmithstatic int
8738465Smsmithcomc_init(int arg)
8838465Smsmith{
8940211Speter    if (comc_started && arg == 0)
9040211Speter	return 0;
9140211Speter    comc_started = 1;
9240211Speter
9341285Srnordier    outb(COMPORT + com_cfcr, CFCR_DLAB | COMC_FMT);
9441285Srnordier    outb(COMPORT + com_dlbl, COMC_BPS(COMSPEED) & 0xff);
9541285Srnordier    outb(COMPORT + com_dlbh, COMC_BPS(COMSPEED) >> 8);
9641285Srnordier    outb(COMPORT + com_cfcr, COMC_FMT);
9741285Srnordier    outb(COMPORT + com_mcr, MCR_RTS | MCR_DTR);
9840211Speter
9941285Srnordier    do
10041285Srnordier        inb(COMPORT + com_data);
10141285Srnordier    while (inb(COMPORT + com_lsr) & LSR_RXRDY);
10241285Srnordier
10340211Speter    return(0);
10438465Smsmith}
10538465Smsmith
10639441Smsmithstatic void
10739441Smsmithcomc_putchar(int c)
10839441Smsmith{
10941285Srnordier    int wait;
11041285Srnordier
11141285Srnordier    for (wait = COMC_TXWAIT; wait > 0; wait--)
11241285Srnordier        if (inb(COMPORT + com_lsr) & LSR_TXRDY) {
11341285Srnordier	    outb(COMPORT + com_data, c);
11441285Srnordier	    break;
11541285Srnordier	}
11639441Smsmith}
11739441Smsmith
11838465Smsmithstatic int
11939441Smsmithcomc_getchar(void)
12038465Smsmith{
12141285Srnordier    return(comc_ischar() ? inb(COMPORT + com_data) : -1);
12238465Smsmith}
12339441Smsmith
12439441Smsmithstatic int
12539441Smsmithcomc_ischar(void)
12639441Smsmith{
12741285Srnordier    return(inb(COMPORT + com_lsr) & LSR_RXRDY);
12839441Smsmith}
129