summaryrefslogtreecommitdiff
path: root/sys/arch/i386/stand/libsa/bioscons.c
blob: 18bd40a1b96a4bdbf8ae2fae0c370d40c9d71db4 (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
/*	$OpenBSD: bioscons.c,v 1.11 1997/10/07 07:59:59 mickey Exp $	*/

/*
 * Copyright (c) 1997 Michael Shalayeff
 * All rights reserved.
 *
 * 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.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Michael Shalayeff.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 REGENTS 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/types.h>
#include <machine/biosvar.h>
#include <machine/pio.h>
#include <dev/isa/isareg.h>
#include <dev/ic/mc146818reg.h>
#include <dev/ic/comreg.h>
#include <dev/ic/ns16450reg.h>
#include <i386/isa/nvram.h>
#include <dev/cons.h>
#include <lib/libsa/stand.h>

int comspeed __P((dev_t, int));

int
cnspeed(dev, sp)
	dev_t	dev;
	int	sp;
{
	if (major(dev) == 8)	/* comN */
		return comspeed(dev, sp);
	/* pc0 and anything else */
	return 9600;
}

/* XXX cannot trust NVRAM on this.  Maybe later we make a real probe.  */
#if 0
#define PRESENT_MASK (NVRAM_EQUIPMENT_KBD|NVRAM_EQUIPMENT_DISPLAY)
#else
#define PRESENT_MASK 0
#endif

int com_setsp __P((int));
static const int comports[4] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };

void
pc_probe(cn)
	struct consdev *cn;
{
	outb(IO_RTC, NVRAM_EQUIPMENT);
	if ((inb(IO_RTC+1) & PRESENT_MASK) == PRESENT_MASK) {
		cn->cn_pri = CN_INTERNAL;
		/* XXX from i386/conf.c */
		cn->cn_dev = makedev(12, 0);
		printf("pc%d detected\n", minor(cn->cn_dev));
	}
}

void
pc_init(cn)
	struct consdev *cn;
{
	printf("using pc%d console\n", minor(cn->cn_dev));
}

int
pc_getc(dev)
	dev_t dev;
{
	register int rv;

	if (dev & 0x80) {
		__asm __volatile(DOINT(0x16) "; setnz %b0" : "=a" (rv) :
		    "0" (0x100) : "%ecx", "%edx", "cc" );
		return (rv & 0xff);
	}

	__asm __volatile(DOINT(0x16) : "=a" (rv) : "0" (0x000) :
	    "%ecx", "edx", "cc" );
	return (rv & 0xff);
}

void
pc_putc(dev, c)
	dev_t dev;
	int c;
{
	__asm __volatile(DOINT(0x10) : : "a" (c | 0xe00), "b" (1) :
	    "%ecx", "%edx", "cc" );
}

void
com_probe(cn)
	struct consdev *cn;
{
	register int i, n;

	 /* get equip. (9-11 # of coms) */
	__asm __volatile(DOINT(0x11) : "=a" (n) : : "%ecx", "%edx", "cc");
	n >>= 9;
	n &= 7;
	for (i = 0; i < n; i++)
		printf("com%d detected\n", i);
	if (n) {
		cn->cn_pri = CN_NORMAL;
		/* XXX from i386/conf.c */
		cn->cn_dev = makedev(8, 0);
	}
}

void
com_init(cn)
	struct consdev *cn;
{
	register int unit = minor(cn->cn_dev);

	/* let bios do necessary init first, 9600-N-1 */
	__asm __volatile(DOINT(0x14) : : "a" (0xe3), "d" (unit) :
	    "%ecx", "cc" );
}

int
com_getc(dev)
	dev_t dev;
{
	register int rv;

	if (dev & 0x80) {
		__asm __volatile(DOINT(0x14) : "=a" (rv) :
		    "0" (0x300), "d" (minor(dev&0x7f)) : "%ecx", "cc" );
		return ((rv & 0x100) == 0x100);
	}

	do
		__asm __volatile(DOINT(0x14) : "=a" (rv) :
		    "0" (0x200), "d" (minor(dev)) : "%ecx", "cc" );
	while (rv & 0x8000);

	return (rv & 0xff);
}

/* call with sp == 0 to query the current speed */
int
comspeed(dev, sp)
	dev_t dev;
	int sp;
{
	static int com_speed = 9600;  /* default speed is 9600 baud */
	int i, newsp;
	time_t tt;
        int err;

	if (sp <= 0)
		return com_speed;
	/* valid baud rate? */
	if (sp > 38400 || sp < 75)
		return -1;

	for (i = sp; i != 75; i >>= 1)
		if (i & 1)
			return -1;

/* ripped screaming from dev/ic/com.c */
#define divrnd(n, q)    (((n)*2/(q)+1)/2)       /* divide and round off */
	newsp = divrnd((COM_FREQ / 16), sp);
	if (newsp <= 0)
		return -1;
	err = divrnd((COM_FREQ / 16) * 1000, sp * newsp) - 1000;
	if (err < 0)
		err = -err;
	if (err > COM_TOLERANCE)
		return -1;
#undef  divrnd(n, q)

	if (cn_tab && cn_tab->cn_dev == dev && com_speed != sp)
	{
		printf("com%d: changing speed to %d baud\n\a"
		       "com%d: change your terminal to match!\n\a"
		       "com%d: will change speed in 5 seconds....\n\a",
		       minor(dev), sp, minor(dev), minor(dev));
		/* let the \n get out and the
		   user change the terminal */
		for (tt = getsecs() + 5; getsecs() < tt;);
	}

	outb(comports[minor(dev)] + com_cfcr, LCR_DLAB);
	outb(comports[minor(dev)] + com_dlbl, newsp);
	outb(comports[minor(dev)] + com_dlbh, newsp>>8);
	outb(comports[minor(dev)] + com_cfcr, LCR_8BITS);
	printf("com%d: console is at %d baud\n", minor(dev), sp);

	newsp = com_speed;
	com_speed = sp;
	return newsp;
}

void
com_putc(dev, c)
	dev_t dev;
	int c;
{
	register int rv;

	__asm __volatile(DOINT(0x14) : "=a" (rv) :
	    "d" (minor(dev)), "0" (c | 0x100) : "%ecx", "cc" );
}