Home | History | Annotate | Line # | Download | only in boot
scifcons.c revision 1.1
      1 /*	$NetBSD: scifcons.c,v 1.1 2006/09/01 21:26:18 uwe Exp $ */
      2 /*	NetBSD: scif.c,v 1.38 2004/12/13 02:14:13 chs Exp */
      3 
      4 /*-
      5  * Copyright (C) 1999 T.Horiuchi and SAITOH Masanobu.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  */
     29 
     30 /*-
     31  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
     32  * All rights reserved.
     33  *
     34  * This code is derived from software contributed to The NetBSD Foundation
     35  * by Charles M. Hannum.
     36  *
     37  * Redistribution and use in source and binary forms, with or without
     38  * modification, are permitted provided that the following conditions
     39  * are met:
     40  * 1. Redistributions of source code must retain the above copyright
     41  *    notice, this list of conditions and the following disclaimer.
     42  * 2. Redistributions in binary form must reproduce the above copyright
     43  *    notice, this list of conditions and the following disclaimer in the
     44  *    documentation and/or other materials provided with the distribution.
     45  * 3. All advertising materials mentioning features or use of this software
     46  *    must display the following acknowledgement:
     47  *        This product includes software developed by the NetBSD
     48  *        Foundation, Inc. and its contributors.
     49  * 4. Neither the name of The NetBSD Foundation nor the names of its
     50  *    contributors may be used to endorse or promote products derived
     51  *    from this software without specific prior written permission.
     52  *
     53  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     54  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     55  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     56  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     57  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     58  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     59  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     60  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     61  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     62  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     63  * POSSIBILITY OF SUCH DAMAGE.
     64  */
     65 
     66 /*
     67  * Copyright (c) 1991 The Regents of the University of California.
     68  * All rights reserved.
     69  *
     70  * Redistribution and use in source and binary forms, with or without
     71  * modification, are permitted provided that the following conditions
     72  * are met:
     73  * 1. Redistributions of source code must retain the above copyright
     74  *    notice, this list of conditions and the following disclaimer.
     75  * 2. Redistributions in binary form must reproduce the above copyright
     76  *    notice, this list of conditions and the following disclaimer in the
     77  *    documentation and/or other materials provided with the distribution.
     78  * 3. Neither the name of the University nor the names of its contributors
     79  *    may be used to endorse or promote products derived from this software
     80  *    without specific prior written permission.
     81  *
     82  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     83  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     84  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     85  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     86  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     87  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     88  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     89  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     90  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     91  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     92  * SUCH DAMAGE.
     93  *
     94  *	@(#)com.c	7.5 (Berkeley) 5/16/91
     95  */
     96 
     97 /*
     98  * SH internal serial driver
     99  *
    100  * This code is derived from both z8530tty.c and com.c
    101  */
    102 
    103 #include <lib/libsa/stand.h>
    104 #include <lib/libkern/libkern.h>
    105 
    106 #include <sh3/scifreg.h>
    107 
    108 #include "boot.h"
    109 #include "cons.h"
    110 
    111 #define scif_smr_read()		SHREG_SCSMR2
    112 #define scif_smr_write(v)	(SHREG_SCSMR2 = (v))
    113 
    114 #define scif_brr_read()		SHREG_SCBRR2
    115 #define scif_brr_write(v)	(SHREG_SCBRR2 = (v))
    116 
    117 #define scif_scr_read()		SHREG_SCSCR2
    118 #define scif_scr_write(v)	(SHREG_SCSCR2 = (v))
    119 
    120 #define scif_ftdr_write(v)	(SHREG_SCFTDR2 = (v))
    121 
    122 #define scif_ssr_read()		SHREG_SCSSR2
    123 #define scif_ssr_write(v)	(SHREG_SCSSR2 = (v))
    124 
    125 #define scif_frdr_read()	SHREG_SCFRDR2
    126 
    127 #define scif_fcr_read()		SHREG_SCFCR2
    128 #define scif_fcr_write(v)	(SHREG_SCFCR2 = (v))
    129 
    130 #define scif_fdr_read()		SHREG_SCFDR2
    131 
    132 #define scif_sptr_read()	SHREG_SCSPTR2
    133 #define scif_sptr_write(v)	(SHREG_SCSPTR2 = (v))
    134 
    135 #define scif_lsr_read()		SHREG_SCLSR2
    136 #define scif_lsr_write(v)	(SHREG_SCLSR2 = (v))
    137 
    138 #define	divrnd(n, q)	(((n)*2/(q)+1)/2)	/* divide and round off */
    139 
    140 #define	SERBUFSIZE	16
    141 static u_char serbuf[SERBUFSIZE];
    142 static int serbuf_read = 0;
    143 static int serbuf_write = 0;
    144 
    145 void
    146 scif_init(unsigned int bps)
    147 {
    148 
    149 	serbuf_read = 0;
    150 	serbuf_write = 0;
    151 
    152 	/* Initialize SCR */
    153 	scif_scr_write(0x00);
    154 
    155 	/* Clear FIFO */
    156 	scif_fcr_write(SCFCR2_TFRST | SCFCR2_RFRST);
    157 
    158 	/* Serial Mode Register */
    159 	scif_smr_write(0x00);	/* 8bit,NonParity,Even,1Stop */
    160 
    161 	/* Bit Rate Register */
    162 	scif_brr_write(divrnd(PCLOCK, 32 * bps) - 1);
    163 
    164 	delay(100);
    165 
    166 	scif_sptr_write(SCSPTR2_RTSIO);
    167 
    168 	scif_fcr_write(FIFO_RCV_TRIGGER_1 | FIFO_XMT_TRIGGER_8);
    169 
    170 	/* Send permission, Receive permission ON */
    171 	scif_scr_write(SCSCR2_TE | SCSCR2_RE);
    172 
    173 	/* Serial Status Register */
    174 	scif_ssr_write(scif_ssr_read() & SCSSR2_TDFE); /* Clear Status */
    175 }
    176 
    177 int
    178 scif_getc(void)
    179 {
    180 	unsigned char c, err_c;
    181 	unsigned short err_c2;
    182 
    183 	if (serbuf_read != serbuf_write) {
    184 		c = serbuf[serbuf_read];
    185 		serbuf_read = (serbuf_read + 1) % SERBUFSIZE;
    186 		return (c);
    187 	}
    188 
    189 	for (;;) {
    190 		/* wait for ready */
    191 		while ((scif_fdr_read() & SCFDR2_RECVCNT) == 0)
    192 			continue;
    193 
    194 		c = scif_frdr_read();
    195 		err_c = scif_ssr_read();
    196 		scif_ssr_write(scif_ssr_read()
    197 			& ~(SCSSR2_ER | SCSSR2_BRK | SCSSR2_RDF | SCSSR2_DR));
    198 
    199 		err_c2 = scif_lsr_read();
    200 		scif_lsr_write(scif_lsr_read() & ~SCLSR2_ORER);
    201 
    202 		if ((err_c & (SCSSR2_ER | SCSSR2_BRK | SCSSR2_FER
    203 		    | SCSSR2_PER)) == 0) {
    204 			if ((err_c2 & SCLSR2_ORER) == 0) {
    205 				return (c);
    206 			}
    207 		}
    208 	}
    209 }
    210 
    211 void
    212 scif_putc(int c)
    213 {
    214 
    215 	/* wait for ready */
    216 	while ((scif_fdr_read() & SCFDR2_TXCNT) == SCFDR2_TXF_FULL)
    217 		continue;
    218 
    219 	/* write send data to send register */
    220 	scif_ftdr_write(c);
    221 
    222 	/* clear ready flag */
    223 	scif_ssr_write(scif_ssr_read() & ~(SCSSR2_TDFE | SCSSR2_TEND));
    224 }
    225 
    226 int
    227 scif_status(void)
    228 {
    229 	unsigned char c, err_c;
    230 	unsigned short err_c2;
    231 
    232 	/* check if any preread input is already there */
    233 	if (serbuf_read != serbuf_write)
    234 		return (1);
    235 
    236 	if (scif_fdr_read() & SCFDR2_RECVCNT) {
    237 		c = scif_frdr_read();
    238 		err_c = scif_ssr_read();
    239 		scif_ssr_write(scif_ssr_read()
    240 			& ~(SCSSR2_ER | SCSSR2_BRK | SCSSR2_RDF | SCSSR2_DR));
    241 
    242 		err_c2 = scif_lsr_read();
    243 		scif_lsr_write(scif_lsr_read() & ~SCLSR2_ORER);
    244 
    245 		if ((err_c & (SCSSR2_ER | SCSSR2_BRK | SCSSR2_FER
    246 		    | SCSSR2_PER)) == 0) {
    247 			if ((err_c2 & SCLSR2_ORER) == 0) {
    248 				/* stuff char into preread buffer */
    249 				serbuf[serbuf_write] = (u_char)c;
    250 				serbuf_write = (serbuf_write + 1) % SERBUFSIZE;
    251 				return (1);
    252 			}
    253 		}
    254 	}
    255 	return (0);	/* nothing out there... */
    256 }
    257 
    258 int
    259 scif_status2(void)
    260 {
    261 
    262 	if (scif_fdr_read() & SCFDR2_RECVCNT) {
    263 		return (1);
    264 	}
    265 	return (0);
    266 }
    267