unstack - fix ipcvecs
[minix.git] / sys / arch / i386 / stand / lib / comio_direct.c
blobe7462105451bcb087f3c42a8f127d612cc03a64e
1 /* $NetBSD: comio_direct.c,v 1.10 2008/12/14 18:46:33 christos Exp $ */
3 /*-
4 * Copyright (c) 1993, 1994, 1995, 1996, 1997
5 * Charles M. Hannum. All rights reserved.
7 * Taken from sys/dev/isa/com.c and integrated into standalone boot
8 * programs by Martin Husemann.
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.
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.
37 * Copyright (c) 1991 The Regents of the University of California.
38 * All rights reserved.
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. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
64 * @(#)com.c 7.5 (Berkeley) 5/16/91
67 #include <sys/types.h>
68 #include <lib/libsa/stand.h>
69 #include <machine/pio.h>
70 #include <dev/ic/comreg.h>
71 #include "comio_direct.h"
72 #include "libi386.h"
74 /* preread buffer for xon/xoff handling */
75 #define XON 0x11
76 #define XOFF 0x13
77 #define SERBUFSIZE 16
78 static u_char serbuf[SERBUFSIZE];
79 static int serbuf_read = 0;
80 static int serbuf_write = 0;
81 static int stopped = 0;
83 #define ISSET(t,f) ((t) & (f))
85 #define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */
86 #define RATE_9600 divrnd((COM_FREQ / 16), 9600)
89 * calculate divisor for a given speed
91 static int
92 comspeed(long speed)
94 int x, err;
96 if (speed <= 0)
97 speed = 9600;
98 x = divrnd((COM_FREQ / 16), speed);
99 if (x <= 0)
100 return RATE_9600;
101 err = divrnd((COM_FREQ / 16) * 1000, speed * x) - 1000;
102 if (err < 0)
103 err = -err;
104 if (err > COM_TOLERANCE)
105 return RATE_9600;
106 return x;
110 * get a character
113 comgetc_d(int combase)
115 u_char stat, c;
117 if (serbuf_read != serbuf_write) {
118 c = serbuf[serbuf_read++];
119 if (serbuf_read >= SERBUFSIZE)
120 serbuf_read = 0;
121 return c;
124 for (;;) {
125 while (!ISSET(stat = inb(combase + com_lsr), LSR_RXRDY))
126 continue;
127 c = inb(combase + com_data);
128 inb(combase + com_iir);
129 if (c != XOFF) {
130 stopped = 0;
131 break; /* got a real char, deliver it... */
133 stopped = 1;
135 return c;
139 * output a character, return nonzero on success
142 computc_d(int c, int combase)
144 u_char stat;
145 int timo;
147 /* check for old XOFF */
148 while (stopped)
149 comgetc_d(combase); /* wait for XON */
151 /* check for new XOFF */
152 if (comstatus_d(combase)) {
153 int c = comgetc_d(combase); /* XOFF handled in comgetc_d */
154 /* stuff char into preread buffer */
155 serbuf[serbuf_write++] = c;
156 if (serbuf_write >= SERBUFSIZE)
157 serbuf_write = 0;
160 /* wait for any pending transmission to finish */
161 timo = 50000;
162 while (!ISSET(stat = inb(combase + com_lsr), LSR_TXRDY)
163 && --timo)
164 continue;
165 if (timo == 0) return 0;
166 outb(combase + com_data, c);
167 /* wait for this transmission to complete */
168 timo = 1500000;
169 while (!ISSET(stat = inb(combase + com_lsr), LSR_TXRDY)
170 && --timo)
171 continue;
172 if (timo == 0) return 0;
173 /* clear any interrupts generated by this transmission */
174 inb(combase + com_iir);
176 return 1;
180 * Initialize UART to known state.
183 cominit_d(int combase, int speed)
185 int rate, err;
187 serbuf_read = 0;
188 serbuf_write = 0;
190 outb(combase + com_cfcr, LCR_DLAB);
191 if (speed == 0) {
192 /* Try to determine the current baud rate */
193 rate = inb(combase + com_dlbl) | inb(combase + com_dlbh) << 8;
194 if (rate == 0)
195 rate = RATE_9600;
196 speed = divrnd((COM_FREQ / 16), rate);
197 err = speed - (speed + 150)/300 * 300;
198 speed -= err;
199 if (err < 0)
200 err = -err;
201 if (err > 50)
202 speed = 9600;
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);
213 return speed;
217 * return nonzero if input char available, do XON/XOFF handling
220 comstatus_d(int combase)
222 /* check if any preread input is already there */
223 if (serbuf_read != serbuf_write) return 1;
225 /* check for new stuff on the port */
226 if (ISSET(inb(combase + com_lsr), LSR_RXRDY)) {
227 /* this could be XOFF, which we would swallow, so we can't
228 claim there is input available... */
229 int c = inb(combase + com_data);
230 inb(combase + com_iir);
231 if (c == XOFF) {
232 stopped = 1;
233 } else {
234 /* stuff char into preread buffer */
235 serbuf[serbuf_write++] = c;
236 if (serbuf_write >= SERBUFSIZE)
237 serbuf_write = 0;
238 return 1;
242 return 0; /* nothing out there... */