Home | History | Annotate | Line # | Download | only in boot
      1  1.1  garbled /*	$NetBSD: com.c,v 1.1 2007/12/17 19:09:46 garbled Exp $	*/
      2  1.1  garbled 
      3  1.1  garbled /*-
      4  1.1  garbled  * Copyright (c) 1993, 1994, 1995, 1996, 1997
      5  1.1  garbled  *	Charles M. Hannum.  All rights reserved.
      6  1.1  garbled  *
      7  1.1  garbled  * Interrupt processing and hardware flow control partly based on code from
      8  1.1  garbled  * Onno van der Linden and Gordon Ross.
      9  1.1  garbled  *
     10  1.1  garbled  * Redistribution and use in source and binary forms, with or without
     11  1.1  garbled  * modification, are permitted provided that the following conditions
     12  1.1  garbled  * are met:
     13  1.1  garbled  * 1. Redistributions of source code must retain the above copyright
     14  1.1  garbled  *    notice, this list of conditions and the following disclaimer.
     15  1.1  garbled  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  garbled  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  garbled  *    documentation and/or other materials provided with the distribution.
     18  1.1  garbled  * 3. All advertising materials mentioning features or use of this software
     19  1.1  garbled  *    must display the following acknowledgement:
     20  1.1  garbled  *	This product includes software developed by Charles M. Hannum.
     21  1.1  garbled  * 4. The name of the author may not be used to endorse or promote products
     22  1.1  garbled  *    derived from this software without specific prior written permission.
     23  1.1  garbled  *
     24  1.1  garbled  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     25  1.1  garbled  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     26  1.1  garbled  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     27  1.1  garbled  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     28  1.1  garbled  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     29  1.1  garbled  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     30  1.1  garbled  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     31  1.1  garbled  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     32  1.1  garbled  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     33  1.1  garbled  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34  1.1  garbled  */
     35  1.1  garbled 
     36  1.1  garbled /*
     37  1.1  garbled  * Copyright (c) 1991 The Regents of the University of California.
     38  1.1  garbled  * All rights reserved.
     39  1.1  garbled  *
     40  1.1  garbled  * Redistribution and use in source and binary forms, with or without
     41  1.1  garbled  * modification, are permitted provided that the following conditions
     42  1.1  garbled  * are met:
     43  1.1  garbled  * 1. Redistributions of source code must retain the above copyright
     44  1.1  garbled  *    notice, this list of conditions and the following disclaimer.
     45  1.1  garbled  * 2. Redistributions in binary form must reproduce the above copyright
     46  1.1  garbled  *    notice, this list of conditions and the following disclaimer in the
     47  1.1  garbled  *    documentation and/or other materials provided with the distribution.
     48  1.1  garbled  * 3. Neither the name of the University nor the names of its contributors
     49  1.1  garbled  *    may be used to endorse or promote products derived from this software
     50  1.1  garbled  *    without specific prior written permission.
     51  1.1  garbled  *
     52  1.1  garbled  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     53  1.1  garbled  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54  1.1  garbled  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55  1.1  garbled  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     56  1.1  garbled  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57  1.1  garbled  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58  1.1  garbled  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59  1.1  garbled  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60  1.1  garbled  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61  1.1  garbled  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62  1.1  garbled  * SUCH DAMAGE.
     63  1.1  garbled  *
     64  1.1  garbled  *	@(#)com.c	7.5 (Berkeley) 5/16/91
     65  1.1  garbled  */
     66  1.1  garbled #ifdef CONS_SERIAL
     67  1.1  garbled 
     68  1.1  garbled #include <lib/libsa/stand.h>
     69  1.1  garbled 
     70  1.1  garbled #include "boot.h"
     71  1.1  garbled 
     72  1.1  garbled #define	COM_FREQ_PC	1843200	/* 16-bit baud rate divisor */
     73  1.1  garbled #define	COM_FREQ	8000000	/* 16-bit baud rate divisor */
     74  1.1  garbled #define	COM_FREQ_PWR	23961600	/* 16-bit baud rate divisor */
     75  1.1  garbled #define	COM_TOLERANCE	30	/* baud rate tolerance, in 0.1% units */
     76  1.1  garbled 
     77  1.1  garbled int
     78  1.1  garbled comspeed(long speed)
     79  1.1  garbled {
     80  1.1  garbled #define	divrnd(n, q)	(((n)*2/(q)+1)/2)	/* divide and round off */
     81  1.1  garbled 
     82  1.1  garbled 	int x, err;
     83  1.1  garbled 
     84  1.1  garbled 	if (speed <= 0)
     85  1.1  garbled 		return (-1);
     86  1.1  garbled 	x = divrnd((COM_FREQ / 16), speed);
     87  1.1  garbled 	if (x <= 0)
     88  1.1  garbled 		return (-1);
     89  1.1  garbled 	err = divrnd((quad_t)(COM_FREQ / 16) * 1000, speed * x) - 1000;
     90  1.1  garbled 	if (err < 0)
     91  1.1  garbled 		err = -err;
     92  1.1  garbled 	if (err > COM_TOLERANCE)
     93  1.1  garbled 		return (-1);
     94  1.1  garbled 	return (x);
     95  1.1  garbled 
     96  1.1  garbled #undef	divrnd
     97  1.1  garbled }
     98  1.1  garbled #endif /* CONS_SERIAL */
     99