1 /* $NetBSD: comio_direct.c,v 1.11 2014/01/05 20:49:20 jakllsch Exp $ */
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
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
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
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"
74 /* preread buffer for xon/xoff handling */
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
98 x
= divrnd((COM_FREQ
/ 16), speed
);
101 err
= divrnd((COM_FREQ
/ 16) * 1000, speed
* x
) - 1000;
104 if (err
> COM_TOLERANCE
)
113 comgetc_d(int combase
)
117 if (serbuf_read
!= serbuf_write
) {
118 c
= serbuf
[serbuf_read
++];
119 if (serbuf_read
>= SERBUFSIZE
)
125 while (!ISSET(stat
= inb(combase
+ com_lsr
), LSR_RXRDY
))
127 c
= inb(combase
+ com_data
);
128 inb(combase
+ com_iir
);
131 break; /* got a real char, deliver it... */
139 * output a character, return nonzero on success
142 computc_d(int c
, int combase
)
147 /* check for old XOFF */
149 comgetc_d(combase
); /* wait for XON */
151 /* check for new XOFF */
152 if (comstatus_d(combase
)) {
153 int x
= comgetc_d(combase
); /* XOFF handled in comgetc_d */
154 /* stuff char into preread buffer */
155 serbuf
[serbuf_write
++] = x
;
156 if (serbuf_write
>= SERBUFSIZE
)
160 /* wait for any pending transmission to finish */
162 while (!ISSET(stat
= inb(combase
+ com_lsr
), LSR_TXRDY
)
165 if (timo
== 0) return 0;
166 outb(combase
+ com_data
, c
);
167 /* wait for this transmission to complete */
169 while (!ISSET(stat
= inb(combase
+ com_lsr
), LSR_TXRDY
)
172 if (timo
== 0) return 0;
173 /* clear any interrupts generated by this transmission */
174 inb(combase
+ com_iir
);
180 * Initialize UART to known state.
183 cominit_d(int combase
, int speed
)
190 outb(combase
+ com_cfcr
, LCR_DLAB
);
192 /* Try to determine the current baud rate */
193 rate
= inb(combase
+ com_dlbl
) | inb(combase
+ com_dlbh
) << 8;
196 speed
= divrnd((COM_FREQ
/ 16), rate
);
197 err
= speed
- (speed
+ 150)/300 * 300;
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);
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
);
234 /* stuff char into preread buffer */
235 serbuf
[serbuf_write
++] = c
;
236 if (serbuf_write
>= SERBUFSIZE
)
242 return 0; /* nothing out there... */