1193323Sed/* $NetBSD: scifcons.c,v 1.1 2006/09/01 21:26:18 uwe Exp $ */ 2193323Sed/* NetBSD: scif.c,v 1.38 2004/12/13 02:14:13 chs Exp */ 3193323Sed 4193323Sed/*- 5193323Sed * Copyright (C) 1999 T.Horiuchi and SAITOH Masanobu. All rights reserved. 6193323Sed * 7193323Sed * Redistribution and use in source and binary forms, with or without 8193323Sed * modification, are permitted provided that the following conditions 9193323Sed * are met: 10193323Sed * 1. Redistributions of source code must retain the above copyright 11212904Sdim * notice, this list of conditions and the following disclaimer. 12193323Sed * 2. Redistributions in binary form must reproduce the above copyright 13193323Sed * notice, this list of conditions and the following disclaimer in the 14193323Sed * documentation and/or other materials provided with the distribution. 15212904Sdim * 3. The name of the author may not be used to endorse or promote products 16212904Sdim * derived from this software without specific prior written permission. 17212904Sdim * 18212904Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19212904Sdim * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20193323Sed * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21212904Sdim * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22212904Sdim * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23193323Sed * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24193323Sed * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25193323Sed * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26193323Sed * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27193323Sed * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28193323Sed */ 29198090Srdivacky 30249423Sdim/*- 31198090Srdivacky * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc. 32198090Srdivacky * All rights reserved. 33198090Srdivacky * 34218893Sdim * This code is derived from software contributed to The NetBSD Foundation 35193323Sed * by Charles M. Hannum. 36193323Sed * 37193323Sed * Redistribution and use in source and binary forms, with or without 38193323Sed * modification, are permitted provided that the following conditions 39249423Sdim * are met: 40198090Srdivacky * 1. Redistributions of source code must retain the above copyright 41198090Srdivacky * notice, this list of conditions and the following disclaimer. 42198090Srdivacky * 2. Redistributions in binary form must reproduce the above copyright 43193323Sed * notice, this list of conditions and the following disclaimer in the 44193323Sed * documentation and/or other materials provided with the distribution. 45193323Sed * 46193323Sed * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 47193323Sed * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 48193323Sed * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 49193323Sed * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 50193323Sed * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 51193323Sed * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 52198892Srdivacky * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 53193323Sed * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 54212904Sdim * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 55218893Sdim * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 56212904Sdim * POSSIBILITY OF SUCH DAMAGE. 57212904Sdim */ 58212904Sdim 59212904Sdim/* 60212904Sdim * Copyright (c) 1991 The Regents of the University of California. 61193323Sed * All rights reserved. 62193323Sed * 63198090Srdivacky * Redistribution and use in source and binary forms, with or without 64218893Sdim * modification, are permitted provided that the following conditions 65193323Sed * are met: 66193323Sed * 1. Redistributions of source code must retain the above copyright 67193323Sed * notice, this list of conditions and the following disclaimer. 68193323Sed * 2. Redistributions in binary form must reproduce the above copyright 69193323Sed * notice, this list of conditions and the following disclaimer in the 70193323Sed * documentation and/or other materials provided with the distribution. 71193323Sed * 3. Neither the name of the University nor the names of its contributors 72193323Sed * may be used to endorse or promote products derived from this software 73193323Sed * without specific prior written permission. 74193323Sed * 75193323Sed * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 76212904Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 77212904Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 78212904Sdim * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 79212904Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 80212904Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 81212904Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 82212904Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 83212904Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 84212904Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 85212904Sdim * SUCH DAMAGE. 86212904Sdim * 87212904Sdim * @(#)com.c 7.5 (Berkeley) 5/16/91 88212904Sdim */ 89212904Sdim 90212904Sdim/* 91212904Sdim * SH internal serial driver 92212904Sdim * 93212904Sdim * This code is derived from both z8530tty.c and com.c 94212904Sdim */ 95212904Sdim 96212904Sdim#include <lib/libsa/stand.h> 97212904Sdim#include <lib/libkern/libkern.h> 98212904Sdim 99212904Sdim#include <sh3/scifreg.h> 100218893Sdim 101212904Sdim#include "boot.h" 102212904Sdim#include "cons.h" 103212904Sdim 104212904Sdim#define scif_smr_read() SHREG_SCSMR2 105212904Sdim#define scif_smr_write(v) (SHREG_SCSMR2 = (v)) 106212904Sdim 107212904Sdim#define scif_brr_read() SHREG_SCBRR2 108212904Sdim#define scif_brr_write(v) (SHREG_SCBRR2 = (v)) 109212904Sdim 110218893Sdim#define scif_scr_read() SHREG_SCSCR2 111218893Sdim#define scif_scr_write(v) (SHREG_SCSCR2 = (v)) 112212904Sdim 113212904Sdim#define scif_ftdr_write(v) (SHREG_SCFTDR2 = (v)) 114249423Sdim 115212904Sdim#define scif_ssr_read() SHREG_SCSSR2 116212904Sdim#define scif_ssr_write(v) (SHREG_SCSSR2 = (v)) 117212904Sdim 118263508Sdim#define scif_frdr_read() SHREG_SCFRDR2 119263508Sdim 120263508Sdim#define scif_fcr_read() SHREG_SCFCR2 121263508Sdim#define scif_fcr_write(v) (SHREG_SCFCR2 = (v)) 122212904Sdim 123212904Sdim#define scif_fdr_read() SHREG_SCFDR2 124212904Sdim 125212904Sdim#define scif_sptr_read() SHREG_SCSPTR2 126212904Sdim#define scif_sptr_write(v) (SHREG_SCSPTR2 = (v)) 127212904Sdim 128212904Sdim#define scif_lsr_read() SHREG_SCLSR2 129193323Sed#define scif_lsr_write(v) (SHREG_SCLSR2 = (v)) 130224145Sdim 131224145Sdim#define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */ 132224145Sdim 133224145Sdim#define SERBUFSIZE 16 134239462Sdimstatic u_char serbuf[SERBUFSIZE]; 135193323Sedstatic int serbuf_read = 0; 136193323Sedstatic int serbuf_write = 0; 137193323Sed 138224145Sdimvoid 139224145Sdimscif_init(unsigned int bps) 140224145Sdim{ 141224145Sdim 142224145Sdim serbuf_read = 0; 143224145Sdim serbuf_write = 0; 144224145Sdim 145224145Sdim /* Initialize SCR */ 146224145Sdim scif_scr_write(0x00); 147224145Sdim 148224145Sdim /* Clear FIFO */ 149224145Sdim scif_fcr_write(SCFCR2_TFRST | SCFCR2_RFRST); 150224145Sdim 151224145Sdim /* Serial Mode Register */ 152224145Sdim scif_smr_write(0x00); /* 8bit,NonParity,Even,1Stop */ 153224145Sdim 154224145Sdim /* Bit Rate Register */ 155224145Sdim scif_brr_write(divrnd(PCLOCK, 32 * bps) - 1); 156224145Sdim 157224145Sdim delay(100); 158224145Sdim 159224145Sdim scif_sptr_write(SCSPTR2_RTSIO); 160224145Sdim 161224145Sdim scif_fcr_write(FIFO_RCV_TRIGGER_1 | FIFO_XMT_TRIGGER_8); 162224145Sdim 163224145Sdim /* Send permission, Receive permission ON */ 164224145Sdim scif_scr_write(SCSCR2_TE | SCSCR2_RE); 165224145Sdim 166224145Sdim /* Serial Status Register */ 167224145Sdim scif_ssr_write(scif_ssr_read() & SCSSR2_TDFE); /* Clear Status */ 168224145Sdim} 169212904Sdim 170212904Sdimint 171212904Sdimscif_getc(void) 172212904Sdim{ 173212904Sdim unsigned char c, err_c; 174212904Sdim unsigned short err_c2; 175243830Sdim 176193323Sed if (serbuf_read != serbuf_write) { 177202375Srdivacky c = serbuf[serbuf_read]; 178193323Sed serbuf_read = (serbuf_read + 1) % SERBUFSIZE; 179202375Srdivacky return (c); 180193323Sed } 181193323Sed 182224145Sdim for (;;) { 183224145Sdim /* wait for ready */ 184202375Srdivacky while ((scif_fdr_read() & SCFDR2_RECVCNT) == 0) 185193323Sed continue; 186243830Sdim 187212904Sdim c = scif_frdr_read(); 188212904Sdim err_c = scif_ssr_read(); 189212904Sdim scif_ssr_write(scif_ssr_read() 190193323Sed & ~(SCSSR2_ER | SCSSR2_BRK | SCSSR2_RDF | SCSSR2_DR)); 191224145Sdim 192193323Sed err_c2 = scif_lsr_read(); 193193323Sed scif_lsr_write(scif_lsr_read() & ~SCLSR2_ORER); 194193323Sed 195212904Sdim if ((err_c & (SCSSR2_ER | SCSSR2_BRK | SCSSR2_FER 196212904Sdim | SCSSR2_PER)) == 0) { 197212904Sdim if ((err_c2 & SCLSR2_ORER) == 0) { 198212904Sdim return (c); 199212904Sdim } 200212904Sdim } 201212904Sdim } 202249423Sdim} 203224145Sdim 204212904Sdimvoid 205212904Sdimscif_putc(int c) 206212904Sdim{ 207212904Sdim 208212904Sdim /* wait for ready */ 209224145Sdim while ((scif_fdr_read() & SCFDR2_TXCNT) == SCFDR2_TXF_FULL) 210249423Sdim continue; 211224145Sdim 212224145Sdim /* write send data to send register */ 213224145Sdim scif_ftdr_write(c); 214212904Sdim 215193323Sed /* clear ready flag */ 216218893Sdim scif_ssr_write(scif_ssr_read() & ~(SCSSR2_TDFE | SCSSR2_TEND)); 217218893Sdim} 218193323Sed 219193323Sedint 220193323Sedscif_status(void) 221212904Sdim{ 222212904Sdim unsigned char c, err_c; 223193323Sed unsigned short err_c2; 224234353Sdim 225193323Sed /* check if any preread input is already there */ 226193323Sed if (serbuf_read != serbuf_write) 227193323Sed return (1); 228193323Sed 229224145Sdim if (scif_fdr_read() & SCFDR2_RECVCNT) { 230218893Sdim c = scif_frdr_read(); 231218893Sdim err_c = scif_ssr_read(); 232193323Sed scif_ssr_write(scif_ssr_read() 233193323Sed & ~(SCSSR2_ER | SCSSR2_BRK | SCSSR2_RDF | SCSSR2_DR)); 234193323Sed 235193323Sed err_c2 = scif_lsr_read(); 236193323Sed scif_lsr_write(scif_lsr_read() & ~SCLSR2_ORER); 237193323Sed 238212904Sdim if ((err_c & (SCSSR2_ER | SCSSR2_BRK | SCSSR2_FER 239193323Sed | SCSSR2_PER)) == 0) { 240212904Sdim if ((err_c2 & SCLSR2_ORER) == 0) { 241193323Sed /* stuff char into preread buffer */ 242193323Sed serbuf[serbuf_write] = (u_char)c; 243193323Sed serbuf_write = (serbuf_write + 1) % SERBUFSIZE; 244193323Sed return (1); 245193323Sed } 246193323Sed } 247193323Sed } 248218893Sdim return (0); /* nothing out there... */ 249218893Sdim} 250193323Sed 251193323Sedint 252193323Sedscif_status2(void) 253193323Sed{ 254210299Sed 255193323Sed if (scif_fdr_read() & SCFDR2_RECVCNT) { 256193323Sed return (1); 257193323Sed } 258212904Sdim return (0); 259193323Sed} 260193323Sed