Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / sys / boot / pc98 / libpc98 / comconsole.c
1 /*
2  * Copyright (c) 1998 Michael Smith (msmith@freebsd.org)
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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
23  * SUCH DAMAGE.
24  *
25  * $FreeBSD: src/sys/boot/pc98/libpc98/comconsole.c,v 1.2.2.1 2000/12/30 12:01:06 nyan Exp $
26  * $DragonFly: src/sys/boot/pc98/libpc98/Attic/comconsole.c,v 1.2 2003/06/17 04:28:18 dillon Exp $
27  */
28
29 #include <stand.h>
30 #include <bootstrap.h>
31 #include <machine/cpufunc.h>
32 #include "libi386.h"
33
34 /* selected defines from ns16550.h */
35 #define com_data        0       /* data register (R/W) */
36 #define com_dlbl        0       /* divisor latch low (W) */
37 #define com_dlbh        1       /* divisor latch high (W) */
38 #define com_ier         1       /* interrupt enable (W) */
39 #define com_iir         2       /* interrupt identification (R) */
40 #define com_fifo        2       /* FIFO control (W) */
41 #define com_lctl        3       /* line control register (R/W) */
42 #define com_cfcr        3       /* line control register (R/W) */
43 #define com_mcr         4       /* modem control register (R/W) */
44 #define com_lsr         5       /* line status register (R/W) */
45 #define com_msr         6       /* modem status register (R/W) */
46
47 /* selected defines from sioreg.h */
48 #define CFCR_DLAB       0x80
49 #define MCR_RTS         0x02
50 #define MCR_DTR         0x01
51 #define LSR_TXRDY       0x20
52 #define LSR_RXRDY       0x01
53
54 #define COMC_FMT        0x3             /* 8N1 */
55 #define COMC_TXWAIT     0x40000         /* transmit timeout */
56 #define COMC_BPS(x)     (115200 / (x))  /* speed to DLAB divisor */
57
58 #ifndef COMPORT
59 #ifdef PC98
60 #define COMPORT         0x238
61 #else
62 #define COMPORT         0x3f8
63 #endif
64 #endif
65 #ifndef COMSPEED
66 #define COMSPEED        9600
67 #endif
68
69 static void     comc_probe(struct console *cp);
70 static int      comc_init(int arg);
71 static void     comc_putchar(int c);
72 static int      comc_getchar(void);
73 static int      comc_ischar(void);
74
75 static int      comc_started;
76
77 struct console comconsole = {
78     "comconsole",
79     "serial port",
80     0,
81     comc_probe,
82     comc_init,
83     comc_putchar,
84     comc_getchar,
85     comc_ischar
86 };
87
88 static void
89 comc_probe(struct console *cp)
90 {
91     /* XXX check the BIOS equipment list? */
92     cp->c_flags |= (C_PRESENTIN | C_PRESENTOUT);
93 }
94
95 static int
96 comc_init(int arg)
97 {
98     if (comc_started && arg == 0)
99         return 0;
100     comc_started = 1;
101
102     outb(COMPORT + com_cfcr, CFCR_DLAB | COMC_FMT);
103     outb(COMPORT + com_dlbl, COMC_BPS(COMSPEED) & 0xff);
104     outb(COMPORT + com_dlbh, COMC_BPS(COMSPEED) >> 8);
105     outb(COMPORT + com_cfcr, COMC_FMT);
106     outb(COMPORT + com_mcr, MCR_RTS | MCR_DTR);
107
108     do
109         inb(COMPORT + com_data);
110     while (inb(COMPORT + com_lsr) & LSR_RXRDY);
111
112     return(0);
113 }
114
115 static void
116 comc_putchar(int c)
117 {
118     int wait;
119
120     for (wait = COMC_TXWAIT; wait > 0; wait--)
121         if (inb(COMPORT + com_lsr) & LSR_TXRDY) {
122             outb(COMPORT + com_data, (u_char)c);
123             break;
124         }
125 }
126
127 static int
128 comc_getchar(void)
129 {
130     return(comc_ischar() ? inb(COMPORT + com_data) : -1);
131 }
132
133 static int
134 comc_ischar(void)
135 {
136     return(inb(COMPORT + com_lsr) & LSR_RXRDY);
137 }