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 <dev/pci/pcireg.h>
34 #include "libi386.h"
35
36 #define COMC_FMT 0x3 /* 8N1 */
37 #define COMC_TXWAIT 0x40000 /* transmit timeout */
38 #define COMC_BPS(x) (115200 / (x)) /* speed to DLAB divisor */
39 #define COMC_DIV2BPS(x) (115200 / (x)) /* DLAB divisor to speed */
40
41 #ifndef COMPORT
42 #define COMPORT 0x3f8
43 #endif
44 #ifndef COMSPEED
45 #define COMSPEED 9600
46 #endif
47
48 static void comc_probe(struct console *cp);
49 static int comc_init(int arg);
50 static void comc_putchar(int c);
51 static int comc_getchar(void);
52 static int comc_getspeed(void);
53 static int comc_ischar(void);
54 static int comc_parseint(const char *string);
55 static uint32_t comc_parse_pcidev(const char *string);
56 static int comc_pcidev_set(struct env_var *ev, int flags,
57 const void *value);
58 static int comc_pcidev_handle(uint32_t locator);
59 static int comc_port_set(struct env_var *ev, int flags,
60 const void *value);
61 static void comc_setup(int speed, int port);
62 static int comc_speed_set(struct env_var *ev, int flags,
63 const void *value);
64
65 static int comc_curspeed;
66 static int comc_port = COMPORT;
67 static uint32_t comc_locator;
68
69 struct console comconsole = {
70 "comconsole",
71 "serial port",
72 0,
73 comc_probe,
74 comc_init,
75 comc_putchar,
76 comc_getchar,
77 comc_ischar
78 };
79
80 static void
comc_probe(struct console * cp)81 comc_probe(struct console *cp)
82 {
83 char intbuf[16];
84 char *cons, *env;
85 int speed, port;
86 uint32_t locator;
87
88 if (comc_curspeed == 0) {
89 comc_curspeed = COMSPEED;
90 /*
91 * Assume that the speed was set by an earlier boot loader if
92 * comconsole is already the preferred console.
93 */
94 cons = getenv("console");
95 if ((cons != NULL && strcmp(cons, comconsole.c_name) == 0) ||
96 getenv("boot_multicons") != NULL) {
97 comc_curspeed = comc_getspeed();
98 }
99
100 env = getenv("comconsole_speed");
101 if (env != NULL) {
102 speed = comc_parseint(env);
103 if (speed > 0)
104 comc_curspeed = speed;
105 }
106
107 sprintf(intbuf, "%d", comc_curspeed);
108 unsetenv("comconsole_speed");
109 env_setenv("comconsole_speed", EV_VOLATILE, intbuf, comc_speed_set,
110 env_nounset);
111
112 env = getenv("comconsole_port");
113 if (env != NULL) {
114 port = comc_parseint(env);
115 if (port > 0)
116 comc_port = port;
117 }
118
119 sprintf(intbuf, "%d", comc_port);
120 unsetenv("comconsole_port");
121 env_setenv("comconsole_port", EV_VOLATILE, intbuf, comc_port_set,
122 env_nounset);
123
124 env = getenv("comconsole_pcidev");
125 if (env != NULL) {
126 locator = comc_parse_pcidev(env);
127 if (locator != 0)
128 comc_pcidev_handle(locator);
129 }
130
131 unsetenv("comconsole_pcidev");
132 env_setenv("comconsole_pcidev", EV_VOLATILE, env, comc_pcidev_set,
133 env_nounset);
134 }
135 comc_setup(comc_curspeed, comc_port);
136 }
137
138 static int
comc_init(int arg)139 comc_init(int arg)
140 {
141
142 comc_setup(comc_curspeed, comc_port);
143
144 if ((comconsole.c_flags & (C_PRESENTIN | C_PRESENTOUT)) ==
145 (C_PRESENTIN | C_PRESENTOUT))
146 return (CMD_OK);
147 return (CMD_ERROR);
148 }
149
150 static void
comc_putchar(int c)151 comc_putchar(int c)
152 {
153 int wait;
154
155 for (wait = COMC_TXWAIT; wait > 0; wait--)
156 if (inb(comc_port + com_lsr) & LSR_TXRDY) {
157 outb(comc_port + com_data, (u_char)c);
158 break;
159 }
160 }
161
162 static int
comc_getchar(void)163 comc_getchar(void)
164 {
165 return (comc_ischar() ? inb(comc_port + com_data) : -1);
166 }
167
168 static int
comc_ischar(void)169 comc_ischar(void)
170 {
171 return (inb(comc_port + com_lsr) & LSR_RXRDY);
172 }
173
174 static int
comc_speed_set(struct env_var * ev,int flags,const void * value)175 comc_speed_set(struct env_var *ev, int flags, const void *value)
176 {
177 int speed;
178
179 if (value == NULL || (speed = comc_parseint(value)) <= 0) {
180 printf("Invalid speed\n");
181 return (CMD_ERROR);
182 }
183
184 if (comc_curspeed != speed)
185 comc_setup(speed, comc_port);
186
187 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
188
189 return (CMD_OK);
190 }
191
192 static int
comc_port_set(struct env_var * ev,int flags,const void * value)193 comc_port_set(struct env_var *ev, int flags, const void *value)
194 {
195 int port;
196
197 if (value == NULL || (port = comc_parseint(value)) <= 0) {
198 printf("Invalid port\n");
199 return (CMD_ERROR);
200 }
201
202 if (comc_port != port)
203 comc_setup(comc_curspeed, port);
204
205 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
206
207 return (CMD_OK);
208 }
209
210 /*
211 * Input: bus:dev:func[:bar]. If bar is not specified, it is 0x10.
212 * Output: bar[24:16] bus[15:8] dev[7:3] func[2:0]
213 */
214 static uint32_t
comc_parse_pcidev(const char * string)215 comc_parse_pcidev(const char *string)
216 {
217 #ifdef NO_PCI
218 return (0);
219 #else
220 char *p, *p1;
221 uint8_t bus, dev, func, bar;
222 uint32_t locator;
223 int pres;
224
225 pres = strtol(string, &p, 0);
226 if (p == string || *p != ':' || pres < 0 )
227 return (0);
228 bus = pres;
229 p1 = ++p;
230
231 pres = strtol(p1, &p, 0);
232 if (p == string || *p != ':' || pres < 0 )
233 return (0);
234 dev = pres;
235 p1 = ++p;
236
237 pres = strtol(p1, &p, 0);
238 if (p == string || (*p != ':' && *p != '\0') || pres < 0 )
239 return (0);
240 func = pres;
241
242 if (*p == ':') {
243 p1 = ++p;
244 pres = strtol(p1, &p, 0);
245 if (p == string || *p != '\0' || pres <= 0 )
246 return (0);
247 bar = pres;
248 } else
249 bar = 0x10;
250
251 locator = (bar << 16) | biospci_locator(bus, dev, func);
252 return (locator);
253 #endif
254 }
255
256 static int
comc_pcidev_handle(uint32_t locator)257 comc_pcidev_handle(uint32_t locator)
258 {
259 #ifdef NO_PCI
260 return (CMD_ERROR);
261 #else
262 char intbuf[64];
263 uint32_t port;
264
265 if (biospci_read_config(locator & 0xffff,
266 (locator & 0xff0000) >> 16, 2, &port) == -1) {
267 printf("Cannot read bar at 0x%x\n", locator);
268 return (CMD_ERROR);
269 }
270 if (!PCI_BAR_IO(port)) {
271 printf("Memory bar at 0x%x\n", locator);
272 return (CMD_ERROR);
273 }
274 port &= PCIM_BAR_IO_BASE;
275
276 sprintf(intbuf, "%d", port);
277 unsetenv("comconsole_port");
278 env_setenv("comconsole_port", EV_VOLATILE, intbuf,
279 comc_port_set, env_nounset);
280
281 comc_setup(comc_curspeed, port);
282 comc_locator = locator;
283
284 return (CMD_OK);
285 #endif
286 }
287
288 static int
comc_pcidev_set(struct env_var * ev,int flags,const void * value)289 comc_pcidev_set(struct env_var *ev, int flags, const void *value)
290 {
291 uint32_t locator;
292 int error;
293
294 if (value == NULL || (locator = comc_parse_pcidev(value)) <= 0) {
295 printf("Invalid pcidev\n");
296 return (CMD_ERROR);
297 }
298 if ((comconsole.c_flags & (C_ACTIVEIN | C_ACTIVEOUT)) != 0 &&
299 comc_locator != locator) {
300 error = comc_pcidev_handle(locator);
301 if (error != CMD_OK)
302 return (error);
303 }
304 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
305 return (CMD_OK);
306 }
307
308 static void
comc_setup(int speed,int port)309 comc_setup(int speed, int port)
310 {
311 static int TRY_COUNT = 1000000;
312 char intbuf[64];
313 int tries;
314
315 unsetenv("hw.uart.console");
316 comc_curspeed = speed;
317 comc_port = port;
318 if ((comconsole.c_flags & (C_ACTIVEIN | C_ACTIVEOUT)) == 0)
319 return;
320
321 outb(comc_port + com_cfcr, CFCR_DLAB | COMC_FMT);
322 outb(comc_port + com_dlbl, COMC_BPS(speed) & 0xff);
323 outb(comc_port + com_dlbh, COMC_BPS(speed) >> 8);
324 outb(comc_port + com_cfcr, COMC_FMT);
325 outb(comc_port + com_mcr, MCR_RTS | MCR_DTR);
326
327 tries = 0;
328 do
329 inb(comc_port + com_data);
330 while (inb(comc_port + com_lsr) & LSR_RXRDY && ++tries < TRY_COUNT);
331
332 if (tries < TRY_COUNT) {
333 comconsole.c_flags |= (C_PRESENTIN | C_PRESENTOUT);
334 sprintf(intbuf, "io:%d,br:%d", comc_port, comc_curspeed);
335 env_setenv("hw.uart.console", EV_VOLATILE, intbuf, NULL, NULL);
336 } else
337 comconsole.c_flags &= ~(C_PRESENTIN | C_PRESENTOUT);
338 }
339
340 static int
comc_parseint(const char * speedstr)341 comc_parseint(const char *speedstr)
342 {
343 char *p;
344 int speed;
345
346 speed = strtol(speedstr, &p, 0);
347 if (p == speedstr || *p != '\0' || speed <= 0)
348 return (-1);
349
350 return (speed);
351 }
352
353 static int
comc_getspeed(void)354 comc_getspeed(void)
355 {
356 u_int divisor;
357 u_char dlbh;
358 u_char dlbl;
359 u_char cfcr;
360
361 cfcr = inb(comc_port + com_cfcr);
362 outb(comc_port + com_cfcr, CFCR_DLAB | cfcr);
363
364 dlbl = inb(comc_port + com_dlbl);
365 dlbh = inb(comc_port + com_dlbh);
366
367 outb(comc_port + com_cfcr, cfcr);
368
369 divisor = dlbh << 8 | dlbl;
370
371 /* XXX there should be more sanity checking. */
372 if (divisor == 0)
373 return (COMSPEED);
374 return (COMC_DIV2BPS(divisor));
375 }
376