Home | History | Annotate | Line # | Download | only in boot
      1 /*	$NetBSD: com.c,v 1.1 2011/03/03 05:59:37 kiyohara Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2008 Izumi Tsutsui.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 /*-
     27  * Copyright (c) 1993, 1994, 1995, 1996, 1997
     28  *	Charles M. Hannum.  All rights reserved.
     29  *
     30  * Interrupt processing and hardware flow control partly based on code from
     31  * Onno van der Linden and Gordon Ross.
     32  *
     33  * Redistribution and use in source and binary forms, with or without
     34  * modification, are permitted provided that the following conditions
     35  * are met:
     36  * 1. Redistributions of source code must retain the above copyright
     37  *    notice, this list of conditions and the following disclaimer.
     38  * 2. Redistributions in binary form must reproduce the above copyright
     39  *    notice, this list of conditions and the following disclaimer in the
     40  *    documentation and/or other materials provided with the distribution.
     41  * 3. All advertising materials mentioning features or use of this software
     42  *    must display the following acknowledgement:
     43  *	This product includes software developed by Charles M. Hannum.
     44  * 4. The name of the author may not be used to endorse or promote products
     45  *    derived from this software without specific prior written permission.
     46  *
     47  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     48  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     49  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     50  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     51  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     52  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     53  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     54  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     55  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     56  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     57  */
     58 
     59 /*
     60  * Copyright (c) 1991 The Regents of the University of California.
     61  * All rights reserved.
     62  *
     63  * Redistribution and use in source and binary forms, with or without
     64  * modification, are permitted provided that the following conditions
     65  * are met:
     66  * 1. Redistributions of source code must retain the above copyright
     67  *    notice, this list of conditions and the following disclaimer.
     68  * 2. Redistributions in binary form must reproduce the above copyright
     69  *    notice, this list of conditions and the following disclaimer in the
     70  *    documentation and/or other materials provided with the distribution.
     71  * 3. Neither the name of the University nor the names of its contributors
     72  *    may be used to endorse or promote products derived from this software
     73  *    without specific prior written permission.
     74  *
     75  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     76  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     77  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     78  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     79  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     80  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     81  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     82  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     83  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     84  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     85  * SUCH DAMAGE.
     86  *
     87  *	@(#)com.c	7.5 (Berkeley) 5/16/91
     88  */
     89 
     90 #ifdef CONS_COM
     91 
     92 #include <lib/libsa/stand.h>
     93 #include <lib/libkern/libkern.h>
     94 
     95 #include <dev/ic/comreg.h>
     96 
     97 #include <machine/cpu.h>
     98 
     99 #include "boot.h"
    100 #include "ns16550.h"
    101 
    102 #define CSR_READ(base, reg)		(*(volatile uint8_t *)((base) + (reg)))
    103 #define CSR_WRITE(base, reg, val)					\
    104 	do {								\
    105 		*(volatile uint8_t *)((base) + (reg)) = (val);		\
    106 	} while (/* CONSTCOND */ 0)
    107 
    108 #define	COM_FREQ	1843200	/* 16-bit baud rate divisor */
    109 #define	COM_TOLERANCE	30	/* baud rate tolerance, in 0.1% units */
    110 
    111 static int comspeed(long);
    112 
    113 static int
    114 comspeed(long speed)
    115 {
    116 #define	divrnd(n, q)	(((n)*2/(q)+1)/2)	/* divide and round off */
    117 
    118 	int x, err;
    119 	long frequency = COM_FREQ * 10;
    120 
    121 	if (speed <= 0)
    122 		return -1;
    123 	x = divrnd((frequency / 16), speed);
    124 	if (x <= 0)
    125 		return -1;
    126 	err = divrnd(((quad_t)frequency) * 1000 / 16, speed * x) - 1000;
    127 	if (err < 0)
    128 		err = -err;
    129 	if (err > COM_TOLERANCE)
    130 		return -1;
    131 	return x;
    132 
    133 #undef	divrnd
    134 }
    135 
    136 void *
    137 com_init(int addr, int speed)
    138 {
    139 	uint8_t *com_port;
    140 
    141 	com_port = (void *)addr;
    142 
    143 	CSR_WRITE(com_port, com_lctl, LCR_DLAB);
    144 	speed = comspeed(speed);
    145 	CSR_WRITE(com_port, com_dlbl, speed);
    146 	CSR_WRITE(com_port, com_dlbh, speed >> 8);
    147 
    148 	CSR_WRITE(com_port, com_lctl, LCR_PNONE | LCR_8BITS);
    149 	CSR_WRITE(com_port, com_mcr, MCR_RTS | MCR_DTR);
    150 	CSR_WRITE(com_port, com_fifo,
    151 	    FIFO_XMT_RST | FIFO_RCV_RST | FIFO_ENABLE);
    152 	CSR_WRITE(com_port, com_ier, 0);
    153 
    154 	return com_port;
    155 }
    156 
    157 void
    158 com_putc(void *dev, int c)
    159 {
    160 	volatile uint8_t *com_port = dev;
    161 
    162 	while ((CSR_READ(com_port, com_lsr) & LSR_TXRDY) == 0)
    163 		;
    164 
    165 	CSR_WRITE(com_port, com_data, c);
    166 }
    167 
    168 int
    169 com_getc(void *dev)
    170 {
    171 	volatile uint8_t *com_port = dev;
    172 
    173 	while ((CSR_READ(com_port, com_lsr) & LSR_RXRDY) == 0)
    174 		;
    175 
    176 	return CSR_READ(com_port, com_data);
    177 }
    178 
    179 int
    180 com_scankbd(void *dev)
    181 {
    182 	volatile uint8_t *com_port = dev;
    183 
    184 	if ((CSR_READ(com_port, com_lsr) & LSR_RXRDY) == 0)
    185 		return -1;
    186 
    187 	return CSR_READ(com_port, com_data);
    188 }
    189 #endif /* CONS_SERIAL */
    190