xref: /netbsd-src/sys/arch/i386/stand/lib/comio_direct.c (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /*	$NetBSD: comio_direct.c,v 1.5 2003/04/16 14:56:55 dsl Exp $	*/
2 
3 /*-
4  * Copyright (c) 1993, 1994, 1995, 1996, 1997
5  *	Charles M. Hannum.  All rights reserved.
6  *
7  * Taken from sys/dev/isa/com.c and integrated into standalone boot
8  * programs by Martin Husemann.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by Charles M. Hannum.
21  * 4. The name of the author may not be used to endorse or promote products
22  *    derived from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /*
37  * Copyright (c) 1991 The Regents of the University of California.
38  * All rights reserved.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  *    notice, this list of conditions and the following disclaimer.
45  * 2. Redistributions in binary form must reproduce the above copyright
46  *    notice, this list of conditions and the following disclaimer in the
47  *    documentation and/or other materials provided with the distribution.
48  * 3. All advertising materials mentioning features or use of this software
49  *    must display the following acknowledgement:
50  *	This product includes software developed by the University of
51  *	California, Berkeley and its contributors.
52  * 4. Neither the name of the University nor the names of its contributors
53  *    may be used to endorse or promote products derived from this software
54  *    without specific prior written permission.
55  *
56  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66  * SUCH DAMAGE.
67  *
68  *	@(#)com.c	7.5 (Berkeley) 5/16/91
69  */
70 
71 #include <sys/types.h>
72 #include <lib/libsa/stand.h>
73 #include <machine/pio.h>
74 #include <dev/ic/comreg.h>
75 #include "comio_direct.h"
76 #include "libi386.h"
77 
78 static int comspeed __P((long speed));
79 
80 /* preread buffer for xon/xoff handling */
81 #define XON	0x11
82 #define	XOFF	0x13
83 #define	SERBUFSIZE	16
84 static u_char serbuf[SERBUFSIZE];
85 static int serbuf_read = 0;
86 static int serbuf_write = 0;
87 static int stopped = 0;
88 
89 #define	ISSET(t,f)	((t) & (f))
90 
91 /*
92  * calculate divisor for a given speed
93  */
94 static int
95 comspeed(speed)
96 	long speed;
97 {
98 #define	divrnd(n, q)	(((n)*2/(q)+1)/2)	/* divide and round off */
99 
100 	int x, err;
101 
102 	if (speed <= 0)
103 		speed = 9600;
104 	x = divrnd((COM_FREQ / 16), speed);
105 	if (x <= 0)
106 		return divrnd((COM_FREQ / 16), 9600);
107 	err = divrnd((COM_FREQ / 16) * 1000, speed * x) - 1000;
108 	if (err < 0)
109 		err = -err;
110 	if (err > COM_TOLERANCE)
111 		return divrnd((COM_FREQ / 16), 9600);
112 	return (x);
113 
114 #undef	divrnd(n, q)
115 }
116 
117 /*
118  * get a character
119  */
120 int
121 comgetc_d(combase)
122 	int combase;
123 {
124 	u_char stat, c;
125 
126 	if (serbuf_read != serbuf_write) {
127 		c = serbuf[serbuf_read++];
128 		if (serbuf_read >= SERBUFSIZE)
129 			serbuf_read = 0;
130 		return c;
131 	}
132 
133 	for (;;) {
134 		while (!ISSET(stat = inb(combase + com_lsr), LSR_RXRDY))
135 			;
136 		c = inb(combase + com_data);
137 		inb(combase + com_iir);
138 		if (c != XOFF) {
139 			stopped = 0;
140 			break;	/* got a real char, deliver it... */
141 		}
142 		stopped = 1;
143 	}
144 	return (c);
145 }
146 
147 /*
148  * output a character, return nonzero on success
149  */
150 int
151 computc_d(c, combase)
152 	int c;
153 	int combase;
154 {
155 	u_char stat;
156 	register int timo;
157 
158 	/* check for old XOFF */
159 	while (stopped)
160 		comgetc_d(combase);	/* wait for XON */
161 
162 	/* check for new XOFF */
163 	if (comstatus_d(combase)) {
164 		int c = comgetc_d(combase);	/* XOFF handled in comgetc_d */
165 		/* stuff char into preread buffer */
166 		serbuf[serbuf_write++] = c;
167 		if (serbuf_write >= SERBUFSIZE)
168 			serbuf_write = 0;
169 	}
170 
171 	/* wait for any pending transmission to finish */
172 	timo = 50000;
173 	while (!ISSET(stat = inb(combase + com_lsr), LSR_TXRDY)
174 	    && --timo)
175 		;
176 	if (timo == 0) return 0;
177 	outb(combase + com_data, c);
178 	/* wait for this transmission to complete */
179 	timo = 1500000;
180 	while (!ISSET(stat = inb(combase + com_lsr), LSR_TXRDY)
181 	    && --timo)
182 		;
183 	if (timo == 0) return 0;
184 	/* clear any interrupts generated by this transmission */
185 	inb(combase + com_iir);
186 
187 	return 1;
188 }
189 
190 /*
191  * Initialize UART to known state.
192  */
193 void
194 cominit_d(combase,speed)
195 	int combase;
196 	int speed;
197 {
198 	int rate;
199 
200 	serbuf_read = 0;
201 	serbuf_write = 0;
202 
203 	outb(combase + com_cfcr, LCR_DLAB);
204 	rate = comspeed(speed);
205 	outb(combase + com_dlbl, rate);
206 	outb(combase + com_dlbh, rate >> 8);
207 	outb(combase + com_cfcr, LCR_8BITS);
208 	outb(combase + com_mcr, MCR_DTR | MCR_RTS);
209 	outb(combase + com_fifo,
210 	    FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_1);
211 	outb(combase + com_ier, 0);
212 }
213 
214 /*
215  * return nonzero if input char available, do XON/XOFF handling
216  */
217 int
218 comstatus_d(combase)
219 	int combase;
220 {
221 	/* check if any preread input is already there */
222 	if (serbuf_read != serbuf_write) return 1;
223 
224 	/* check for new stuff on the port */
225 	if (ISSET(inb(combase + com_lsr), LSR_RXRDY)) {
226 		/* this could be XOFF, which we would swallow, so we can't
227 		   claim there is input available... */
228 		int c = inb(combase + com_data);
229 		inb(combase + com_iir);
230 		if (c == XOFF) {
231 			stopped = 1;
232 		} else {
233 			/* stuff char into preread buffer */
234 			serbuf[serbuf_write++] = c;
235 			if (serbuf_write >= SERBUFSIZE)
236 				serbuf_write = 0;
237 			return 1;
238 		}
239 	}
240 
241 	return 0;	/* nothing out there... */
242 }
243