xref: /openbsd/sys/arch/i386/stand/libsa/bioscons.c (revision 771fbea0)
1 /*	$OpenBSD: bioscons.c,v 1.36 2016/05/27 05:37:51 beck 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 "debug.h"
40 #include "biosdev.h"
41 
42 /* XXX cannot trust NVRAM on this.  Maybe later we make a real probe.  */
43 #if 0
44 #define PRESENT_MASK (NVRAM_EQUIPMENT_KBD|NVRAM_EQUIPMENT_DISPLAY)
45 #else
46 #define PRESENT_MASK 0
47 #endif
48 
49 void
50 pc_probe(struct consdev *cn)
51 {
52 	cn->cn_pri = CN_MIDPRI;
53 	cn->cn_dev = makedev(12, 0);
54 	printf(" pc%d", minor(cn->cn_dev));
55 
56 #if 0
57 	outb(IO_RTC, NVRAM_EQUIPMENT);
58 	if ((inb(IO_RTC+1) & PRESENT_MASK) == PRESENT_MASK) {
59 		cn->cn_pri = CN_MIDPRI;
60 		/* XXX from i386/conf.c */
61 		cn->cn_dev = makedev(12, 0);
62 		printf(" pc%d", minor(cn->cn_dev));
63 	}
64 #endif
65 }
66 
67 void
68 pc_init(struct consdev *cn)
69 {
70 }
71 
72 int
73 pc_getc(dev_t dev)
74 {
75 	register int rv;
76 
77 	if (dev & 0x80) {
78 		__asm volatile(DOINT(0x16) "; setnz %b0" : "=a" (rv) :
79 		    "0" (0x100) : "%ecx", "%edx", "cc" );
80 		return (rv & 0xff);
81 	}
82 
83 	/*
84 	 * Wait for a character to actually become available.  Appears to
85 	 * be necessary on (at least) the Intel Mac Mini.
86 	 */
87 	do {
88 		__asm volatile(DOINT(0x16) "; setnz %b0" : "=a" (rv) :
89 		    "0" (0x100) : "%ecx", "%edx", "cc" );
90 	} while ((rv & 0xff) == 0);
91 
92 	__asm volatile(DOINT(0x16) : "=a" (rv) : "0" (0x000) :
93 	    "%ecx", "%edx", "cc" );
94 
95 	return (rv & 0xff);
96 }
97 
98 int
99 pc_getshifts(dev_t dev)
100 {
101 	register int rv;
102 
103 	__asm volatile(DOINT(0x16) : "=a" (rv) : "0" (0x200) :
104 	    "%ecx", "%edx", "cc" );
105 
106 	return (rv & 0xff);
107 }
108 
109 void
110 pc_putc(dev_t dev, int c)
111 {
112 	__asm volatile(DOINT(0x10) : : "a" (c | 0xe00), "b" (1) :
113 	    "%ecx", "%edx", "cc" );
114 }
115 
116 const int comports[4] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
117 
118 void
119 com_probe(struct consdev *cn)
120 {
121 	register int i, n;
122 
123 	/* get equip. (9-11 # of coms) */
124 	__asm volatile(DOINT(0x11) : "=a" (n) : : "%ecx", "%edx", "cc");
125 	n >>= 9;
126 	n &= 7;
127 	for (i = 0; i < n; i++)
128 		printf(" com%d", i);
129 
130 	cn->cn_pri = CN_LOWPRI;
131 	/* XXX from i386/conf.c */
132 	cn->cn_dev = makedev(8, 0);
133 }
134 
135 int com_speed = -1;
136 int com_addr = -1;
137 
138 void
139 com_init(struct consdev *cn)
140 {
141 	int port = (com_addr == -1) ? comports[minor(cn->cn_dev)] : com_addr;
142 	time_t tt = getsecs() + 1;
143 	u_long i = 1;
144 
145 	outb(port + com_ier, 0);
146 	if (com_speed == -1)
147 		comspeed(cn->cn_dev, 9600); /* default speed is 9600 baud */
148 	outb(port + com_mcr, MCR_DTR | MCR_RTS);
149 	outb(port + com_fifo, FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST |
150 	    FIFO_TRIGGER_1);
151 	(void) inb(port + com_iir);
152 
153 	/* A few ms delay for the chip, using the getsecs() API */
154 	while (!(i++ % 1000) && getsecs() < tt)
155 		;
156 
157 	/* drain the input buffer */
158 	while (inb(port + com_lsr) & LSR_RXRDY)
159 		(void)inb(port + com_data);
160 }
161 
162 int
163 com_getc(dev_t dev)
164 {
165 	int port = (com_addr == -1) ? comports[minor(dev & 0x7f)] : com_addr;
166 
167 	if (dev & 0x80)
168 		return (inb(port + com_lsr) & LSR_RXRDY);
169 
170 	while ((inb(port + com_lsr) & LSR_RXRDY) == 0)
171 		;
172 
173 	return (inb(port + com_data) & 0xff);
174 }
175 
176 /* call with sp == 0 to query the current speed */
177 int
178 comspeed(dev_t dev, int sp)
179 {
180 	int port = (com_addr == -1) ? comports[minor(dev)] : com_addr;
181 	int i, newsp;
182 	int err;
183 
184 	if (sp <= 0)
185 		return com_speed;
186 	/* valid baud rate? */
187 	if (115200 < sp || sp < 75)
188 		return -1;
189 
190 	/*
191 	 * Accepted speeds:
192 	 *   75 150 300 600 1200 2400 4800 9600 19200 38400 76800 and
193 	 *   14400 28800 57600 115200
194 	 */
195 	for (i = sp; i != 75 && i != 14400; i >>= 1)
196 		if (i & 1)
197 			return -1;
198 
199 /* ripped screaming from dev/ic/com.c */
200 #define divrnd(n, q)    (((n)*2/(q)+1)/2)       /* divide and round off */
201 	newsp = divrnd((COM_FREQ / 16), sp);
202 	if (newsp <= 0)
203 		return -1;
204 	err = divrnd((COM_FREQ / 16) * 1000, sp * newsp) - 1000;
205 	if (err < 0)
206 		err = -err;
207 	if (err > COM_TOLERANCE)
208 		return -1;
209 #undef  divrnd
210 
211 	if (com_speed != -1 && cn_tab && cn_tab->cn_dev == dev &&
212 	    com_speed != sp) {
213 		printf("com%d: changing speed to %d baud in 5 seconds, "
214 		    "change your terminal to match!\n\a",
215 		    minor(dev), sp);
216 		sleep(5);
217 	}
218 
219 	outb(port + com_cfcr, LCR_DLAB);
220 	outb(port + com_dlbl, newsp);
221 	outb(port + com_dlbh, newsp>>8);
222 	outb(port + com_cfcr, LCR_8BITS);
223 	if (com_speed != -1)
224 		printf("\ncom%d: %d baud\n", minor(dev), sp);
225 
226 	newsp = com_speed;
227 	com_speed = sp;
228 	return newsp;
229 }
230 
231 void
232 com_putc(dev_t dev, int c)
233 {
234 	int port = (com_addr == -1) ? comports[minor(dev)] : com_addr;
235 
236 	while ((inb(port + com_lsr) & LSR_TXRDY) == 0)
237 		;
238 
239 	outb(port + com_data, c);
240 }
241