11.7Stsutsui/*	$NetBSD: com.c,v 1.7 2008/03/02 06:17:41 tsutsui Exp $	*/
21.1Scdi
31.1Scdi/*-
41.1Scdi * Copyright (c) 1993, 1994, 1995, 1996, 1997
51.1Scdi *	Charles M. Hannum.  All rights reserved.
61.1Scdi *
71.1Scdi * Interrupt processing and hardware flow control partly based on code from
81.1Scdi * Onno van der Linden and Gordon Ross.
91.1Scdi *
101.1Scdi * Redistribution and use in source and binary forms, with or without
111.1Scdi * modification, are permitted provided that the following conditions
121.1Scdi * are met:
131.1Scdi * 1. Redistributions of source code must retain the above copyright
141.1Scdi *    notice, this list of conditions and the following disclaimer.
151.1Scdi * 2. Redistributions in binary form must reproduce the above copyright
161.1Scdi *    notice, this list of conditions and the following disclaimer in the
171.1Scdi *    documentation and/or other materials provided with the distribution.
181.1Scdi * 3. All advertising materials mentioning features or use of this software
191.1Scdi *    must display the following acknowledgement:
201.1Scdi *	This product includes software developed by Charles M. Hannum.
211.1Scdi * 4. The name of the author may not be used to endorse or promote products
221.1Scdi *    derived from this software without specific prior written permission.
231.1Scdi *
241.1Scdi * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
251.1Scdi * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
261.1Scdi * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
271.1Scdi * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
281.1Scdi * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
291.1Scdi * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
301.1Scdi * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
311.1Scdi * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
321.1Scdi * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
331.1Scdi * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
341.1Scdi */
351.1Scdi
361.1Scdi/*
371.1Scdi * Copyright (c) 1991 The Regents of the University of California.
381.1Scdi * All rights reserved.
391.1Scdi *
401.1Scdi * Redistribution and use in source and binary forms, with or without
411.1Scdi * modification, are permitted provided that the following conditions
421.1Scdi * are met:
431.1Scdi * 1. Redistributions of source code must retain the above copyright
441.1Scdi *    notice, this list of conditions and the following disclaimer.
451.1Scdi * 2. Redistributions in binary form must reproduce the above copyright
461.1Scdi *    notice, this list of conditions and the following disclaimer in the
471.1Scdi *    documentation and/or other materials provided with the distribution.
481.2Sagc * 3. Neither the name of the University nor the names of its contributors
491.1Scdi *    may be used to endorse or promote products derived from this software
501.1Scdi *    without specific prior written permission.
511.1Scdi *
521.1Scdi * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
531.1Scdi * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
541.1Scdi * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
551.1Scdi * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
561.1Scdi * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
571.1Scdi * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
581.1Scdi * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
591.1Scdi * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
601.1Scdi * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
611.1Scdi * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
621.1Scdi * SUCH DAMAGE.
631.1Scdi *
641.1Scdi *	@(#)com.c	7.5 (Berkeley) 5/16/91
651.1Scdi */
661.1Scdi
671.1Scdi#include <sys/types.h>
681.7Stsutsui#include <lib/libsa/stand.h>
691.7Stsutsui#include "boot.h"
701.1Scdi
711.1Scdi#define	COM_FREQ	1843200	/* 16-bit baud rate divisor */
721.1Scdi#define	COM_TOLERANCE	30	/* baud rate tolerance, in 0.1% units */
731.1Scdi
741.1Scdiint
751.5Stsutsuicomspeed(long speed)
761.1Scdi{
771.1Scdi#define	divrnd(n, q)	(((n)*2/(q)+1)/2)	/* divide and round off */
781.1Scdi
791.1Scdi	int x, err;
801.1Scdi	long frequency = COM_FREQ * 10;
811.1Scdi
821.1Scdi	if (speed <= 0)
831.5Stsutsui		return -1;
841.1Scdi	x = divrnd((frequency / 16), speed);
851.1Scdi	if (x <= 0)
861.5Stsutsui		return -1;
871.1Scdi	err = divrnd(((quad_t)frequency) * 1000 / 16, speed * x) - 1000;
881.1Scdi	if (err < 0)
891.1Scdi		err = -err;
901.1Scdi	if (err > COM_TOLERANCE)
911.5Stsutsui		return -1;
921.5Stsutsui	return x;
931.1Scdi
941.3Ssimonb#undef	divrnd
951.1Scdi}
96