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