Home | History | Annotate | Line # | Download | only in board
sscom.c revision 1.2
      1 /*	$NetBSD: sscom.c,v 1.2 2003/09/03 03:18:31 mycroft Exp $ */
      2 
      3 
      4 /*
      5  * Copyright (c) 2002, 2003 Fujitsu Component Limited
      6  * Copyright (c) 2002, 2003 Genetec Corporation
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. Neither the name of The Fujitsu Component Limited nor the name of
     18  *    Genetec corporation may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY FUJITSU COMPONENT LIMITED AND GENETEC
     22  * CORPORATION ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     23  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     25  * DISCLAIMED.  IN NO EVENT SHALL FUJITSU COMPONENT LIMITED OR GENETEC
     26  * CORPORATION BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     28  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
     29  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     30  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     31  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     32  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 /* derived from ns16550.c */
     36 /*
     37  * Copyright (c) 2002 Wasabi Systems, Inc.
     38  * All rights reserved.
     39  *
     40  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. All advertising materials mentioning features or use of this software
     51  *    must display the following acknowledgement:
     52  *	This product includes software developed for the NetBSD Project by
     53  *	Wasabi Systems, Inc.
     54  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     55  *    or promote products derived from this software without specific prior
     56  *    written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     60  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     61  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     62  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     63  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     64  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     65  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     66  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     67  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     68  * POSSIBILITY OF SUCH DAMAGE.
     69  */
     70 /*
     71  * This file provides the cons_init() function and console I/O routines
     72  * for boards that use built-in UART of Samsung's S3C2xx0 CPUs.
     73  */
     74 
     75 #include <sys/types.h>
     76 #include <arch/arm/s3c2xx0/s3c2xx0reg.h>
     77 #include <arch/arm/s3c2xx0/s3c24x0reg.h>
     78 #include <arch/arm/s3c2xx0/s3c2410reg.h>
     79 #include <lib/libsa/stand.h>
     80 
     81 #include "board.h"
     82 
     83 #ifndef SSCOM_TOLERANCE
     84 #define	SSCOM_TOLERANCE	30	/* XXX: baud rate tolerance, in 0.1% units */
     85 #endif
     86 
     87 #define	INB(x)		*((__volatile uint8_t *) ((CONADDR) + (x)))
     88 #define	INW(x)		*((__volatile uint32_t *) ((CONADDR) + (x)))
     89 #define	OUTB(x, v)	(*((__volatile uint8_t *) ((CONADDR) + (x))) = (v))
     90 #define	OUTW(x, v)	(*((__volatile uint32_t *) ((CONADDR) + (x))) = (v))
     91 
     92 #define	ISSET(t,f)	((t) & (f))
     93 
     94 long get_com_freq(void);
     95 
     96 static int
     97 sscomspeed(long speed)
     98 {
     99 #define	divrnd(n, q)	(((n)*2/(q)+1)/2)	/* divide and round off */
    100 
    101 	int x, err;
    102 	long pclk = get_com_freq();
    103 
    104 	if (speed <= 0)
    105 		return -1;
    106 	x = divrnd(pclk / 16, speed);
    107 	if (x <= 0)
    108 		return -1;
    109 	err = divrnd(((quad_t)pclk) * 1000 / 16, speed * x) - 1000;
    110 	if (err < 0)
    111 		err = -err;
    112 	if (err > SSCOM_TOLERANCE)
    113 		return -1;
    114 	return x-1;
    115 
    116 #undef	divrnd
    117 }
    118 
    119 void
    120 cons_init(void)
    121 {
    122 	int rate;
    123 
    124 	OUTW(SSCOM_UCON, 0);
    125 	OUTB(SSCOM_UFCON, UFCON_TXTRIGGER_8 | UFCON_RXTRIGGER_8 |
    126 	    UFCON_TXFIFO_RESET | UFCON_RXFIFO_RESET |
    127 	    UFCON_FIFO_ENABLE);
    128 
    129 	rate = sscomspeed(CONSPEED);
    130 	OUTW(SSCOM_UBRDIV, rate);
    131 	OUTW(SSCOM_ULCON, ULCON_PARITY_NONE|ULCON_LENGTH_8);
    132 
    133 	/* enable UART */
    134 	OUTW(SSCOM_UCON, UCON_TXMODE_INT|UCON_RXMODE_INT);
    135 	OUTW(SSCOM_UMCON, UMCON_RTS);
    136 }
    137 
    138 #define sscom_rxrdy() (INB(SSCOM_UTRSTAT) & UTRSTAT_RXREADY)
    139 
    140 int
    141 getchar(void)
    142 {
    143 	uint8_t stat;
    144 	int c;
    145 
    146 	while (!sscom_rxrdy())
    147 		/* spin */ ;
    148 	c = INB(SSCOM_URXH);
    149 	stat = INB(SSCOM_UERSTAT);	/* XXX */
    150 
    151 	return c;
    152 }
    153 
    154 static void
    155 iputchar(int c)
    156 {
    157 	uint32_t stat;
    158 	int timo;
    159 
    160 	/* Wait for any pending transmission to finish. */
    161 	timo = 50000;
    162 	while (ISSET(stat = INW(SSCOM_UFSTAT), UFSTAT_TXFULL) && --timo)
    163 		/* spin */ ;
    164 
    165 	OUTB(SSCOM_UTXH, c);
    166 
    167 #if 0
    168 	/* Wait for this transmission to complete. */
    169 	timo = 1500000;
    170 	while (!ISSET(stat = INW(SSCOM_UFSTAT), UFSTAT_TXFULL) && --timo)
    171 		/* spin */ ;
    172 #endif
    173 }
    174 
    175 void
    176 putchar(int c)
    177 {
    178 
    179 	if (c == '\n')
    180 		iputchar('\r');
    181 	iputchar(c);
    182 }
    183