remove bad semicolon
[dragonfly.git] / sys / boot / pc32 / libi386 / 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/i386/libi386/comconsole.c,v 1.10 2003/09/16 11:24:23 bde Exp $
26  * $DragonFly: src/sys/boot/pc32/libi386/comconsole.c,v 1.9 2004/06/27 21:27:36 dillon Exp $
27  */
28
29 #include <stand.h>
30 #include <bootstrap.h>
31 #include <machine/cpufunc.h>
32 #include <dev/serial/ic_layer/ns16550.h>
33 #include "libi386.h"
34
35 #define COMC_FMT        0x3             /* 8N1 */
36 #define COMC_TXWAIT     0x40000         /* transmit timeout */
37 #define COMC_BPS(x)     (115200 / (x))  /* speed to DLAB divisor */
38
39 #ifndef COMPORT
40 #define COMPORT         0x3f8
41 #endif
42 #ifndef COMSPEED
43 #define COMSPEED        9600
44 #endif
45
46 static void     comc_probe(struct console *cp);
47 static int      comc_init(int arg);
48 static void     comc_putchar(int c);
49 static int      comc_getchar(void);
50 static int      comc_ischar(void);
51
52 static int      comc_started;
53
54 struct console comconsole = {
55     "comconsole",
56     "serial port",
57     0,
58     comc_probe,
59     comc_init,
60     comc_putchar,
61     comc_getchar,
62     comc_ischar
63 };
64
65 /*
66  * Probe for a comconsole.  If the comport is not mapped at boot time (which
67  * is often true on laptops), don't try to access it.  If we can't clear 
68  * the input fifo, don't try to access it later.
69  *
70  * Normally the stage-2 bootloader will do this detection and hand us
71  * appropriate flags, but some bootloaders (pxe, cdboot) load the loader
72  * directly so we have to check again here.
73  */
74 static void
75 comc_probe(struct console *cp)
76 {
77     int i;
78
79     if (inb(COMPORT + com_lsr) == 0xFF)
80         return;
81     for (i = 255; i >= 0; --i) {
82         if (comc_ischar() == 0)
83             break;
84         inb(COMPORT + com_data);
85     }
86     if (i < 0)
87         return;
88     cp->c_flags |= (C_PRESENTIN | C_PRESENTOUT);
89 }
90
91 static int
92 comc_init(int arg)
93 {
94     if (comc_started && arg == 0)
95         return 0;
96     comc_started = 1;
97
98     outb(COMPORT + com_cfcr, CFCR_DLAB | COMC_FMT);
99     outb(COMPORT + com_dlbl, COMC_BPS(COMSPEED) & 0xff);
100     outb(COMPORT + com_dlbh, COMC_BPS(COMSPEED) >> 8);
101     outb(COMPORT + com_cfcr, COMC_FMT);
102     outb(COMPORT + com_mcr, MCR_RTS | MCR_DTR);
103
104 #if 0
105     /*
106      * Enable the FIFO so the serial port output in dual console mode doesn't
107      * interfere so much with the disk twiddle.
108      *
109      * DISABLED - apparently many new laptops implement only the base 8250,
110      * writing to this port can crash them.
111      */
112     outb(COMPORT + com_fifo, FIFO_ENABLE);
113 #endif
114
115     /*
116      * Give the serial port a little time to settle after asserting RTS and
117      * DTR, then drain any pending garbage.
118      */
119     delay(1000000 / 10);
120     while (comc_ischar())
121         inb(COMPORT + com_data);
122
123     return(0);
124 }
125
126 static void
127 comc_putchar(int c)
128 {
129     int wait;
130
131     for (wait = COMC_TXWAIT; wait > 0; wait--) {
132         if (inb(COMPORT + com_lsr) & LSR_TXRDY) {
133             outb(COMPORT + com_data, (u_char)c);
134             break;
135         }
136     }
137 }
138
139 static int
140 comc_getchar(void)
141 {
142     return(comc_ischar() ? inb(COMPORT + com_data) : -1);
143 }
144
145 static int
146 comc_ischar(void)
147 {
148     return(inb(COMPORT + com_lsr) & LSR_RXRDY);
149 }