comconsole.c revision 42480
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 *
2542480Srnordier *	$Id: comconsole.c,v 1.5 1998/11/22 07:59:16 rnordier 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
5742480Srnordier#ifndef	COMPORT
5841285Srnordier#define COMPORT		0x3f8
5942480Srnordier#endif
6042480Srnordier#ifndef	COMSPEED
6141285Srnordier#define COMSPEED	9600
6242480Srnordier#endif
6341285Srnordier
6438465Smsmithstatic void	comc_probe(struct console *cp);
6538465Smsmithstatic int	comc_init(int arg);
6639441Smsmithstatic void	comc_putchar(int c);
6739441Smsmithstatic int	comc_getchar(void);
6839441Smsmithstatic int	comc_ischar(void);
6938465Smsmith
7040211Speterstatic int	comc_started;
7140211Speter
7238465Smsmithstruct console comconsole = {
7338465Smsmith    "comconsole",
7441285Srnordier    "serial port",
7538465Smsmith    0,
7638465Smsmith    comc_probe,
7738465Smsmith    comc_init,
7839441Smsmith    comc_putchar,
7939441Smsmith    comc_getchar,
8039441Smsmith    comc_ischar
8138465Smsmith};
8238465Smsmith
8338465Smsmithstatic void
8438465Smsmithcomc_probe(struct console *cp)
8538465Smsmith{
8638465Smsmith    /* XXX check the BIOS equipment list? */
8738465Smsmith    cp->c_flags |= (C_PRESENTIN | C_PRESENTOUT);
8838465Smsmith}
8938465Smsmith
9038465Smsmithstatic int
9138465Smsmithcomc_init(int arg)
9238465Smsmith{
9340211Speter    if (comc_started && arg == 0)
9440211Speter	return 0;
9540211Speter    comc_started = 1;
9640211Speter
9741285Srnordier    outb(COMPORT + com_cfcr, CFCR_DLAB | COMC_FMT);
9841285Srnordier    outb(COMPORT + com_dlbl, COMC_BPS(COMSPEED) & 0xff);
9941285Srnordier    outb(COMPORT + com_dlbh, COMC_BPS(COMSPEED) >> 8);
10041285Srnordier    outb(COMPORT + com_cfcr, COMC_FMT);
10141285Srnordier    outb(COMPORT + com_mcr, MCR_RTS | MCR_DTR);
10240211Speter
10341285Srnordier    do
10441285Srnordier        inb(COMPORT + com_data);
10541285Srnordier    while (inb(COMPORT + com_lsr) & LSR_RXRDY);
10641285Srnordier
10740211Speter    return(0);
10838465Smsmith}
10938465Smsmith
11039441Smsmithstatic void
11139441Smsmithcomc_putchar(int c)
11239441Smsmith{
11341285Srnordier    int wait;
11441285Srnordier
11541285Srnordier    for (wait = COMC_TXWAIT; wait > 0; wait--)
11641285Srnordier        if (inb(COMPORT + com_lsr) & LSR_TXRDY) {
11741285Srnordier	    outb(COMPORT + com_data, c);
11841285Srnordier	    break;
11941285Srnordier	}
12039441Smsmith}
12139441Smsmith
12238465Smsmithstatic int
12339441Smsmithcomc_getchar(void)
12438465Smsmith{
12541285Srnordier    return(comc_ischar() ? inb(COMPORT + com_data) : -1);
12638465Smsmith}
12739441Smsmith
12839441Smsmithstatic int
12939441Smsmithcomc_ischar(void)
13039441Smsmith{
13141285Srnordier    return(inb(COMPORT + com_lsr) & LSR_RXRDY);
13239441Smsmith}
133