comio_direct.c revision 1.3 1 /* $NetBSD: comio_direct.c,v 1.3 1998/01/05 07:02:58 perry 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
77 static int comspeed __P((long speed));
78
79 /* preread buffer for xon/xoff handling */
80 #define XON 0x11
81 #define XOFF 0x13
82 #define SERBUFSIZE 16
83 static u_char serbuf[SERBUFSIZE];
84 static int serbuf_read = 0;
85 static int serbuf_write = 0;
86 static int stopped = 0;
87
88 #define ISSET(t,f) ((t) & (f))
89
90 /*
91 * calculate divisor for a given speed
92 */
93 static int
94 comspeed(speed)
95 long speed;
96 {
97 #define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */
98
99 int x, err;
100
101 #if 0
102 if (speed == 0)
103 return (0);
104 #endif
105 if (speed <= 0)
106 return (-1);
107 x = divrnd((COM_FREQ / 16), speed);
108 if (x <= 0)
109 return (-1);
110 err = divrnd((COM_FREQ / 16) * 1000, speed * x) - 1000;
111 if (err < 0)
112 err = -err;
113 if (err > COM_TOLERANCE)
114 return (-1);
115 return (x);
116
117 #undef divrnd(n, q)
118 }
119
120 /*
121 * get a character
122 */
123 int
124 comgetc_d(combase)
125 int combase;
126 {
127 u_char stat, c;
128
129 if (serbuf_read != serbuf_write) {
130 c = serbuf[serbuf_read++];
131 if (serbuf_read >= SERBUFSIZE)
132 serbuf_read = 0;
133 return c;
134 }
135
136 for (;;) {
137 while (!ISSET(stat = inb(combase + com_lsr), LSR_RXRDY))
138 ;
139 c = inb(combase + com_data);
140 inb(combase + com_iir);
141 if (c != XOFF) {
142 stopped = 0;
143 break; /* got a real char, deliver it... */
144 }
145 stopped = 1;
146 }
147 return (c);
148 }
149
150 /*
151 * output a character, return nonzero on success
152 */
153 int
154 computc_d(c, combase)
155 int c;
156 int combase;
157 {
158 u_char stat;
159 register int timo;
160
161 /* check for old XOFF */
162 while (stopped)
163 comgetc_d(combase); /* wait for XON */
164
165 /* check for new XOFF */
166 if (comstatus_d(combase)) {
167 int c = comgetc_d(combase); /* XOFF handled in comgetc_d */
168 /* stuff char into preread buffer */
169 serbuf[serbuf_write++] = c;
170 if (serbuf_write >= SERBUFSIZE)
171 serbuf_write = 0;
172 }
173
174 /* wait for any pending transmission to finish */
175 timo = 50000;
176 while (!ISSET(stat = inb(combase + com_lsr), LSR_TXRDY)
177 && --timo)
178 ;
179 if (timo == 0) return 0;
180 outb(combase + com_data, c);
181 /* wait for this transmission to complete */
182 timo = 1500000;
183 while (!ISSET(stat = inb(combase + com_lsr), LSR_TXRDY)
184 && --timo)
185 ;
186 if (timo == 0) return 0;
187 /* clear any interrupts generated by this transmission */
188 inb(combase + com_iir);
189
190 return 1;
191 }
192
193 /*
194 * Initialize UART to known state.
195 */
196 void
197 cominit_d(combase)
198 int combase;
199 {
200 int rate;
201
202 serbuf_read = 0;
203 serbuf_write = 0;
204
205 outb(combase + com_cfcr, LCR_DLAB);
206 #ifdef CONSPEED
207 rate = comspeed(CONSPEED);
208 #else
209 rate = comspeed(9600);
210 #endif
211 outb(combase + com_dlbl, rate);
212 outb(combase + com_dlbh, rate >> 8);
213 outb(combase + com_cfcr, LCR_8BITS);
214 outb(combase + com_mcr, 0);
215 outb(combase + com_fifo,
216 FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_1);
217 outb(combase + com_ier, 0);
218 }
219
220 /*
221 * return nonzero if input char available, do XON/XOFF handling
222 */
223 int
224 comstatus_d(combase)
225 int combase;
226 {
227 /* check if any preread input is already there */
228 if (serbuf_read != serbuf_write) return 1;
229
230 /* check for new stuff on the port */
231 if (ISSET(inb(combase + com_lsr), LSR_RXRDY)) {
232 /* this could be XOFF, which we would swallow, so we can't
233 claim there is input available... */
234 int c = inb(combase + com_data);
235 inb(combase + com_iir);
236 if (c == XOFF) {
237 stopped = 1;
238 } else {
239 /* stuff char into preread buffer */
240 serbuf[serbuf_write++] = c;
241 if (serbuf_write >= SERBUFSIZE)
242 serbuf_write = 0;
243 return 1;
244 }
245 }
246
247 return 0; /* nothing out there... */
248 }
249