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