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