summaryrefslogtreecommitdiffstats
path: root/sys/boot/pc98/libpc98/comconsole.c
blob: c4028db761da41c6cc867d05d3409a77048bfbce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*-
 * Copyright (c) 1998 Michael Smith (msmith@freebsd.org)
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <stand.h>
#include <bootstrap.h>
#include <machine/cpufunc.h>
#include <dev/ic/ns16550.h>
#include <dev/pci/pcireg.h>
#include "libi386.h"

#define COMC_FMT	0x3		/* 8N1 */
#define COMC_TXWAIT	0x40000		/* transmit timeout */
#define COMC_BPS(x)	(115200 / (x))	/* speed to DLAB divisor */
#define COMC_DIV2BPS(x)	(115200 / (x))	/* DLAB divisor to speed */

#ifndef	COMPORT
#define COMPORT		0x238
#endif
#ifndef	COMSPEED
#define COMSPEED	9600
#endif

static void	comc_probe(struct console *cp);
static int	comc_init(int arg);
static void	comc_putchar(int c);
static int	comc_getchar(void);
static int	comc_getspeed(void);
static void	set_hw_console_hint(void);
static int	comc_ischar(void);
static int	comc_parseint(const char *string);
static uint32_t comc_parse_pcidev(const char *string);
static int	comc_pcidev_set(struct env_var *ev, int flags,
		    const void *value);
static int	comc_pcidev_handle(uint32_t locator);
static int	comc_port_set(struct env_var *ev, int flags,
		    const void *value);
static void	comc_setup(int speed, int port);
static int	comc_speed_set(struct env_var *ev, int flags,
		    const void *value);

static int	comc_started;
static int	comc_curspeed;
static int	comc_port = COMPORT;
static uint32_t	comc_locator;

struct console comconsole = {
    "comconsole",
    "serial port",
    0,
    comc_probe,
    comc_init,
    comc_putchar,
    comc_getchar,
    comc_ischar
};

static void
comc_probe(struct console *cp)
{
    char intbuf[16];
    char *cons, *env;
    int speed, port;
    uint32_t locator;

    /* XXX check the BIOS equipment list? */
    cp->c_flags |= (C_PRESENTIN | C_PRESENTOUT);

    if (comc_curspeed == 0) {
	comc_curspeed = COMSPEED;
	/*
	 * Assume that the speed was set by an earlier boot loader if
	 * comconsole is already the preferred console.
	 */
	cons = getenv("console");
	if ((cons != NULL && strcmp(cons, comconsole.c_name) == 0) ||
	    getenv("boot_multicons") != NULL) {
		comc_curspeed = comc_getspeed();
	}

	env = getenv("comconsole_speed");
	if (env != NULL) {
	    speed = comc_parseint(env);
	    if (speed > 0)
		comc_curspeed = speed;
	}

	sprintf(intbuf, "%d", comc_curspeed);
	unsetenv("comconsole_speed");
	env_setenv("comconsole_speed", EV_VOLATILE, intbuf, comc_speed_set,
	    env_nounset);

	env = getenv("comconsole_port");
	if (env != NULL) {
	    port = comc_parseint(env);
	    if (port > 0)
		comc_port = port;
	}

	sprintf(intbuf, "%d", comc_port);
	unsetenv("comconsole_port");
	env_setenv("comconsole_port", EV_VOLATILE, intbuf, comc_port_set,
	    env_nounset);

	env = getenv("comconsole_pcidev");
	if (env != NULL) {
	    locator = comc_parse_pcidev(env);
	    if (locator != 0)
		    comc_pcidev_handle(locator);
	}

	unsetenv("comconsole_pcidev");
	env_setenv("comconsole_pcidev", EV_VOLATILE, env, comc_pcidev_set,
	    env_nounset);
    }
}

static int
comc_init(int arg)
{
    if (comc_started && arg == 0)
	return 0;
    comc_started = 1;

    comc_setup(comc_curspeed, comc_port);

    return(0);
}

static void
comc_putchar(int c)
{
    int wait;

    for (wait = COMC_TXWAIT; wait > 0; wait--)
        if (inb(comc_port + com_lsr) & LSR_TXRDY) {
	    outb(comc_port + com_data, (u_char)c);
	    break;
	}
}

static int
comc_getchar(void)
{
    return(comc_ischar() ? inb(comc_port + com_data) : -1);
}

static int
comc_ischar(void)
{
    return(inb(comc_port + com_lsr) & LSR_RXRDY);
}

static int
comc_speed_set(struct env_var *ev, int flags, const void *value)
{
    int speed;

    if (value == NULL || (speed = comc_parseint(value)) <= 0) {
	printf("Invalid speed\n");
	return (CMD_ERROR);
    }

    if (comc_started && comc_curspeed != speed)
	comc_setup(speed, comc_port);

    env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);

    return (CMD_OK);
}

static int
comc_port_set(struct env_var *ev, int flags, const void *value)
{
    int port;

    if (value == NULL || (port = comc_parseint(value)) <= 0) {
	printf("Invalid port\n");
	return (CMD_ERROR);
    }

    if (comc_started && comc_port != port) {
	comc_setup(comc_curspeed, port);
	set_hw_console_hint();
    }

    env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);

    return (CMD_OK);
}

static void
set_hw_console_hint(void)
{
	char intbuf[64];

	unsetenv("hw.uart.console");
	sprintf(intbuf, "io:%d,br:%d", comc_port, comc_curspeed);
	env_setenv("hw.uart.console", EV_VOLATILE, intbuf,
	    env_noset, env_nounset);
}

/*
 * Input: bus:dev:func[:bar]. If bar is not specified, it is 0x10.
 * Output: bar[24:16] bus[15:8] dev[7:3] func[2:0]
 */
static uint32_t
comc_parse_pcidev(const char *string)
{
	char *p, *p1;
	uint8_t bus, dev, func, bar;
	uint32_t locator;
	int pres;

	pres = strtol(string, &p, 0);
	if (p == string || *p != ':' || pres < 0 )
		return (0);
	bus = pres;
	p1 = ++p;

	pres = strtol(p1, &p, 0);
	if (p == string || *p != ':' || pres < 0 )
		return (0);
	dev = pres;
	p1 = ++p;

	pres = strtol(p1, &p, 0);
	if (p == string || (*p != ':' && *p != '\0') || pres < 0 )
		return (0);
	func = pres;

	if (*p == ':') {
		p1 = ++p;
		pres = strtol(p1, &p, 0);
		if (p == string || *p != '\0' || pres <= 0 )
			return (0);
		bar = pres;
	} else
		bar = 0x10;

	locator = (bar << 16) | biospci_locator(bus, dev, func);
	return (locator);
}

static int
comc_pcidev_handle(uint32_t locator)
{
	char intbuf[64];
	uint32_t port;

	if (biospci_read_config(locator & 0xffff,
				(locator & 0xff0000) >> 16, 2, &port) == -1) {
		printf("Cannot read bar at 0x%x\n", locator);
		return (CMD_ERROR);
	}
	if (!PCI_BAR_IO(port)) {
		printf("Memory bar at 0x%x\n", locator);
		return (CMD_ERROR);
	}
        port &= PCIM_BAR_IO_BASE;

	sprintf(intbuf, "%d", port);
	unsetenv("comconsole_port");
	env_setenv("comconsole_port", EV_VOLATILE, intbuf,
		   comc_port_set, env_nounset);

	comc_setup(comc_curspeed, port);
	set_hw_console_hint();
	comc_locator = locator;

	return (CMD_OK);
}

static int
comc_pcidev_set(struct env_var *ev, int flags, const void *value)
{
	uint32_t locator;
	int error;

	if (value == NULL || (locator = comc_parse_pcidev(value)) <= 0) {
		printf("Invalid pcidev\n");
		return (CMD_ERROR);
	}
	if (comc_started && comc_locator != locator) {
		error = comc_pcidev_handle(locator);
		if (error != CMD_OK)
			return (error);
	}
	env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
	return (CMD_OK);
}

static void
comc_setup(int speed, int port)
{

    comc_curspeed = speed;
    comc_port = port;

    outb(comc_port + com_cfcr, CFCR_DLAB | COMC_FMT);
    outb(comc_port + com_dlbl, COMC_BPS(speed) & 0xff);
    outb(comc_port + com_dlbh, COMC_BPS(speed) >> 8);
    outb(comc_port + com_cfcr, COMC_FMT);
    outb(comc_port + com_mcr, MCR_RTS | MCR_DTR);

    do
        inb(comc_port + com_data);
    while (inb(comc_port + com_lsr) & LSR_RXRDY);
}

static int
comc_parseint(const char *speedstr)
{
    char *p;
    int speed;

    speed = strtol(speedstr, &p, 0);
    if (p == speedstr || *p != '\0' || speed <= 0)
	return (-1);

    return (speed);
}

static int
comc_getspeed(void)
{
	u_int	divisor;
	u_char	dlbh;
	u_char	dlbl;
	u_char	cfcr;

	cfcr = inb(comc_port + com_cfcr);
	outb(comc_port + com_cfcr, CFCR_DLAB | cfcr);

	dlbl = inb(comc_port + com_dlbl);
	dlbh = inb(comc_port + com_dlbh);

	outb(comc_port + com_cfcr, cfcr);

	divisor = dlbh << 8 | dlbl;

	/* XXX there should be more sanity checking. */
	if (divisor == 0)
		return (COMSPEED);
	return (COMC_DIV2BPS(divisor));
}
OpenPOWER on IntegriCloud