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