]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/boot/pc98/libpc98/comconsole.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.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
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28
29 #include <stand.h>
30 #include <bootstrap.h>
31 #include <machine/cpufunc.h>
32 #include <dev/ic/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 #define COMC_DIV2BPS(x) (115200 / (x))  /* DLAB divisor to speed */
39
40 #ifndef COMPORT
41 #define COMPORT         0x238
42 #endif
43 #ifndef COMSPEED
44 #define COMSPEED        9600
45 #endif
46
47 static void     comc_probe(struct console *cp);
48 static int      comc_init(int arg);
49 static void     comc_putchar(int c);
50 static int      comc_getchar(void);
51 static int      comc_getspeed(void);
52 static int      comc_ischar(void);
53 static int      comc_parsespeed(const char *string);
54 static void     comc_setup(int speed);
55 static int      comc_speed_set(struct env_var *ev, int flags,
56                     const void *value);
57
58 static int      comc_started;
59 static int      comc_curspeed;
60
61 struct console comconsole = {
62     "comconsole",
63     "serial port",
64     0,
65     comc_probe,
66     comc_init,
67     comc_putchar,
68     comc_getchar,
69     comc_ischar
70 };
71
72 static void
73 comc_probe(struct console *cp)
74 {
75     char speedbuf[16];
76     char *cons, *speedenv;
77     int speed;
78
79     /* XXX check the BIOS equipment list? */
80     cp->c_flags |= (C_PRESENTIN | C_PRESENTOUT);
81
82     if (comc_curspeed == 0) {
83         comc_curspeed = COMSPEED;
84         /*
85          * Assume that the speed was set by an earlier boot loader if
86          * comconsole is already the preferred console.
87          */
88         cons = getenv("console");
89         if ((cons != NULL && strcmp(cons, comconsole.c_name) == 0) ||
90             getenv("boot_multicons") != NULL) {
91                 comc_curspeed = comc_getspeed();
92         }
93         speedenv = getenv("comconsole_speed");
94         if (speedenv != NULL) {
95             speed = comc_parsespeed(speedenv);
96             if (speed > 0)
97                 comc_curspeed = speed;
98         }
99
100         sprintf(speedbuf, "%d", comc_curspeed);
101         unsetenv("comconsole_speed");
102         env_setenv("comconsole_speed", EV_VOLATILE, speedbuf, comc_speed_set,
103             env_nounset);
104     }
105 }
106
107 static int
108 comc_init(int arg)
109 {
110     if (comc_started && arg == 0)
111         return 0;
112     comc_started = 1;
113
114     comc_setup(comc_curspeed);
115
116     return(0);
117 }
118
119 static void
120 comc_putchar(int c)
121 {
122     int wait;
123
124     for (wait = COMC_TXWAIT; wait > 0; wait--)
125         if (inb(COMPORT + com_lsr) & LSR_TXRDY) {
126             outb(COMPORT + com_data, (u_char)c);
127             break;
128         }
129 }
130
131 static int
132 comc_getchar(void)
133 {
134     return(comc_ischar() ? inb(COMPORT + com_data) : -1);
135 }
136
137 static int
138 comc_ischar(void)
139 {
140     return(inb(COMPORT + com_lsr) & LSR_RXRDY);
141 }
142
143 static int
144 comc_speed_set(struct env_var *ev, int flags, const void *value)
145 {
146     int speed;
147
148     if (value == NULL || (speed = comc_parsespeed(value)) <= 0) {
149         printf("Invalid speed\n");
150         return (CMD_ERROR);
151     }
152
153     if (comc_started && comc_curspeed != speed)
154         comc_setup(speed);
155
156     env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
157
158     return (CMD_OK);
159 }
160
161 static void
162 comc_setup(int speed)
163 {
164
165     comc_curspeed = speed;
166
167     outb(COMPORT + com_cfcr, CFCR_DLAB | COMC_FMT);
168     outb(COMPORT + com_dlbl, COMC_BPS(speed) & 0xff);
169     outb(COMPORT + com_dlbh, COMC_BPS(speed) >> 8);
170     outb(COMPORT + com_cfcr, COMC_FMT);
171     outb(COMPORT + com_mcr, MCR_RTS | MCR_DTR);
172
173     do
174         inb(COMPORT + com_data);
175     while (inb(COMPORT + com_lsr) & LSR_RXRDY);
176 }
177
178 static int
179 comc_parsespeed(const char *speedstr)
180 {
181     char *p;
182     int speed;
183
184     speed = strtol(speedstr, &p, 0);
185     if (p == speedstr || *p != '\0' || speed <= 0)
186         return (-1);
187
188     return (speed);
189 }
190
191 static int
192 comc_getspeed(void)
193 {
194         u_int   divisor;
195         u_char  dlbh;
196         u_char  dlbl;
197         u_char  cfcr;
198
199         cfcr = inb(COMPORT + com_cfcr);
200         outb(COMPORT + com_cfcr, CFCR_DLAB | cfcr);
201
202         dlbl = inb(COMPORT + com_dlbl);
203         dlbh = inb(COMPORT + com_dlbh);
204
205         outb(COMPORT + com_cfcr, cfcr);
206
207         divisor = dlbh << 8 | dlbl;
208
209         /* XXX there should be more sanity checking. */
210         if (divisor == 0)
211                 return (COMSPEED);
212         return (COMC_DIV2BPS(divisor));
213 }