xref: /openbsd/sys/arch/amd64/stand/libsa/bioscons.c (revision 404b540a)
1 /*	$OpenBSD: bioscons.c,v 1.5 2008/04/20 01:46:35 dlg Exp $	*/
2 
3 /*
4  * Copyright (c) 1997-1999 Michael Shalayeff
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26  * THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/types.h>
30 #include <machine/biosvar.h>
31 #include <machine/pio.h>
32 #include <dev/isa/isareg.h>
33 #include <dev/ic/mc146818reg.h>
34 #include <dev/ic/comreg.h>
35 #include <dev/ic/ns16450reg.h>
36 /* #include <i386/isa/nvram.h> */
37 #include <dev/cons.h>
38 #include <lib/libsa/stand.h>
39 #include "biosdev.h"
40 
41 /* XXX cannot trust NVRAM on this.  Maybe later we make a real probe.  */
42 #if 0
43 #define PRESENT_MASK (NVRAM_EQUIPMENT_KBD|NVRAM_EQUIPMENT_DISPLAY)
44 #else
45 #define PRESENT_MASK 0
46 #endif
47 
48 void
49 pc_probe(struct consdev *cn)
50 {
51 	cn->cn_pri = CN_MIDPRI;
52 	cn->cn_dev = makedev(12, 0);
53 	printf(" pc%d", minor(cn->cn_dev));
54 
55 #if 0
56 	outb(IO_RTC, NVRAM_EQUIPMENT);
57 	if ((inb(IO_RTC+1) & PRESENT_MASK) == PRESENT_MASK) {
58 		cn->cn_pri = CN_MIDPRI;
59 		/* XXX from i386/conf.c */
60 		cn->cn_dev = makedev(12, 0);
61 		printf(" pc%d", minor(cn->cn_dev));
62 	}
63 #endif
64 }
65 
66 void
67 pc_init(struct consdev *cn)
68 {
69 }
70 
71 int
72 pc_getc(dev_t dev)
73 {
74 	register int rv;
75 
76 	if (dev & 0x80) {
77 		__asm __volatile(DOINT(0x16) "; setnz %b0" : "=a" (rv) :
78 		    "0" (0x100) : "%ecx", "%edx", "cc" );
79 		return (rv & 0xff);
80 	}
81 
82 	/*
83 	 * Wait for a character to actually become available.  Appears to
84 	 * be necessary on (at least) the Intel Mac Mini.
85 	 */
86 	do {
87 		__asm __volatile(DOINT(0x16) "; setnz %b0" : "=a" (rv) :
88 		    "0" (0x100) : "%ecx", "%edx", "cc" );
89 	} while ((rv & 0xff) == 0);
90 
91 	__asm __volatile(DOINT(0x16) : "=a" (rv) : "0" (0x000) :
92 	    "%ecx", "%edx", "cc" );
93 	return (rv & 0xff);
94 }
95 
96 int
97 pc_getshifts(dev_t dev)
98 {
99 	register int rv;
100 
101 	__asm __volatile(DOINT(0x16) : "=a" (rv) : "0" (0x200) :
102 	    "%ecx", "%edx", "cc" );
103 
104 	return (rv & 0xff);
105 }
106 
107 void
108 pc_putc(dev_t dev, int c)
109 {
110 	__asm __volatile(DOINT(0x10) : : "a" (c | 0xe00), "b" (1) :
111 	    "%ecx", "%edx", "cc" );
112 }
113 
114 const int comports[4] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
115 
116 void
117 com_probe(struct consdev *cn)
118 {
119 	register int i, n;
120 
121 	/* get equip. (9-11 # of coms) */
122 	__asm __volatile(DOINT(0x11) : "=a" (n) : : "%ecx", "%edx", "cc");
123 	n >>= 9;
124 	n &= 7;
125 	for (i = 0; i < n; i++)
126 		printf(" com%d", i);
127 	if (n) {
128 		cn->cn_pri = CN_LOWPRI;
129 		/* XXX from i386/conf.c */
130 		cn->cn_dev = makedev(8, 0);
131 	}
132 }
133 
134 int com_speed = -1;  /* default speed is 9600 baud */
135 
136 void
137 com_init(struct consdev *cn)
138 {
139 	int port = comports[minor(cn->cn_dev)];
140 
141 	outb(port + com_ier, 0);
142 	if (com_speed == -1)
143 		comspeed(cn->cn_dev, 9600);
144 	outb(port + com_mcr, MCR_DTR | MCR_RTS);
145 	outb(port + com_fifo, FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST |
146 	    FIFO_TRIGGER_1);
147 
148 	/* drain the input buffer */
149 	while (inb(port + com_lsr) & LSR_RXRDY)
150 		(void)inb(port + com_data);
151 }
152 
153 int
154 com_getc(dev_t dev)
155 {
156 	int port = comports[minor(dev & 0x7f)];
157 
158 	if (dev & 0x80)
159 		return (inb(port + com_lsr) & LSR_RXRDY);
160 
161 	while ((inb(port + com_lsr) & LSR_RXRDY) == 0)
162 		;
163 
164 	return (inb(port + com_data) & 0xff);
165 }
166 
167 /* call with sp == 0 to query the current speed */
168 int
169 comspeed(dev_t dev, int sp)
170 {
171 	int i, newsp;
172         int err;
173 
174 	if (sp <= 0)
175 		return com_speed;
176 	/* valid baud rate? */
177 	if (115200 < sp || sp < 75)
178 		return -1;
179 
180 	/*
181 	 * Accepted speeds:
182 	 *   75 150 300 600 1200 2400 4800 9600 19200 38400 76800 and
183 	 *   14400 28800 57600 115200
184 	 */
185 	for (i = sp; i != 75 && i != 14400; i >>= 1)
186 		if (i & 1)
187 			return -1;
188 
189 /* ripped screaming from dev/ic/com.c */
190 #define divrnd(n, q)    (((n)*2/(q)+1)/2)       /* divide and round off */
191 	newsp = divrnd((COM_FREQ / 16), sp);
192 	if (newsp <= 0)
193 		return -1;
194 	err = divrnd((COM_FREQ / 16) * 1000, sp * newsp) - 1000;
195 	if (err < 0)
196 		err = -err;
197 	if (err > COM_TOLERANCE)
198 		return -1;
199 #undef  divrnd
200 
201 	if (com_speed != -1 && cn_tab && cn_tab->cn_dev == dev &&
202 	    com_speed != sp) {
203 		printf("com%d: changing speed to %d baud in 5 seconds, "
204 		    "change your terminal to match!\n\a",
205 		    minor(dev), sp);
206 		sleep(5);
207 	}
208 
209 	outb(comports[minor(dev)] + com_cfcr, LCR_DLAB);
210 	outb(comports[minor(dev)] + com_dlbl, newsp);
211 	outb(comports[minor(dev)] + com_dlbh, newsp>>8);
212 	outb(comports[minor(dev)] + com_cfcr, LCR_8BITS);
213 	if (com_speed != -1)
214 		printf("\ncom%d: %d baud\n", minor(dev), sp);
215 
216 	newsp = com_speed;
217 	com_speed = sp;
218 	return newsp;
219 }
220 
221 void
222 com_putc(dev_t dev, int c)
223 {
224 	int port = comports[minor(dev)];
225 
226 	while ((inb(port + com_lsr) & LSR_TXRDY) == 0)
227 		;
228 
229 	outb(port + com_data, c);
230 }
231 
232