1 /***********************************************************************
3 * Copyright (C) 2004 by FS Forth-Systeme GmbH.
6 * $Id: ns9750_serial.c,v 1.1 2004/02/16 10:37:20 mpietrek Exp $
7 * @Author: Markus Pietrek
8 * @Descr: Serial driver for the NS9750. Only one UART is supported yet.
9 * @References: [1] NS9750 Hardware Reference/December 2003
10 * @TODO: Implement Character GAP Timer when chip is fixed for PLL bypass
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 ***********************************************************************/
31 #ifdef CFG_NS9750_UART
33 #include "ns9750_bbus.h" /* for GPIOs */
34 #include "ns9750_ser.h" /* for serial configuration */
36 DECLARE_GLOBAL_DATA_PTR;
38 #if !defined(CONFIG_CONS_INDEX)
39 #error "No console index specified."
42 #define CONSOLE CONFIG_CONS_INDEX
44 static unsigned int calcBitrateRegister( void );
45 static unsigned int calcRxCharGapRegister( void );
47 static char cCharsAvailable; /* Numbers of chars in unCharCache */
48 static unsigned int unCharCache; /* unCharCache is only valid if
49 * cCharsAvailable > 0 */
51 /***********************************************************************
52 * @Function: serial_init
54 * @Descr: configures GPIOs and UART. Requires BBUS Master Reset turned off
55 ***********************************************************************/
57 int serial_init( void )
59 unsigned int aunGPIOTxD[] = { 0, 8, 40, 44 };
60 unsigned int aunGPIORxD[] = { 1, 9, 41, 45 };
64 /* configure TxD and RxD pins for their special function */
65 set_gpio_cfg_reg_val( aunGPIOTxD[ CONSOLE ],
66 NS9750_GPIO_CFG_FUNC_0 | NS9750_GPIO_CFG_OUTPUT );
67 set_gpio_cfg_reg_val( aunGPIORxD[ CONSOLE ],
68 NS9750_GPIO_CFG_FUNC_0 | NS9750_GPIO_CFG_INPUT );
70 /* configure serial engine */
71 *get_ser_reg_addr_channel( NS9750_SER_CTRL_A, CONSOLE ) =
72 NS9750_SER_CTRL_A_CE |
73 NS9750_SER_CTRL_A_STOP |
74 NS9750_SER_CTRL_A_WLS_8;
78 *get_ser_reg_addr_channel( NS9750_SER_CTRL_B, CONSOLE ) =
79 NS9750_SER_CTRL_B_RCGT;
84 /***********************************************************************
85 * @Function: serial_putc
87 * @Descr: writes one character to the FIFO. Blocks until FIFO is not full
88 ***********************************************************************/
90 void serial_putc( const char c )
95 while (!(*get_ser_reg_addr_channel( NS9750_SER_STAT_A, CONSOLE) &
96 NS9750_SER_STAT_A_TRDY ) ) {
97 /* do nothing, wait for characters in FIFO sent */
100 *(volatile char*) get_ser_reg_addr_channel( NS9750_SER_FIFO,
104 /***********************************************************************
105 * @Function: serial_puts
107 * @Descr: writes non-zero string to the FIFO.
108 ***********************************************************************/
110 void serial_puts( const char *s )
117 /***********************************************************************
118 * @Function: serial_getc
119 * @Return: the character read
120 * @Descr: performs only 8bit accesses to the FIFO. No error handling
121 ***********************************************************************/
123 int serial_getc( void )
127 while (!serial_tstc() ) {
128 /* do nothing, wait for incoming characters */
131 /* at least one character in unCharCache */
132 i = (int) (unCharCache & 0xff);
140 /***********************************************************************
141 * @Function: serial_tstc
142 * @Return: 0 if no input available, otherwise != 0
143 * @Descr: checks for incoming FIFO not empty. Stores the incoming chars in
144 * unCharCache and the numbers of characters in cCharsAvailable
145 ***********************************************************************/
147 int serial_tstc( void )
149 unsigned int unRegCache;
151 if ( cCharsAvailable )
154 unRegCache = *get_ser_reg_addr_channel( NS9750_SER_STAT_A,CONSOLE );
155 if( unRegCache & NS9750_SER_STAT_A_RBC ) {
156 *get_ser_reg_addr_channel( NS9750_SER_STAT_A, CONSOLE ) =
157 NS9750_SER_STAT_A_RBC;
158 unRegCache = *get_ser_reg_addr_channel( NS9750_SER_STAT_A,
162 if ( unRegCache & NS9750_SER_STAT_A_RRDY ) {
163 cCharsAvailable = (unRegCache & NS9750_SER_STAT_A_RXFDB_MA)>>20;
164 if ( !cCharsAvailable )
167 unCharCache = *get_ser_reg_addr_channel( NS9750_SER_FIFO,
172 /* no chars available */
176 void serial_setbrg( void )
178 *get_ser_reg_addr_channel( NS9750_SER_BITRATE, CONSOLE ) =
179 calcBitrateRegister();
180 *get_ser_reg_addr_channel( NS9750_SER_RX_CHAR_TIMER, CONSOLE ) =
181 calcRxCharGapRegister();
184 /***********************************************************************
185 * @Function: calcBitrateRegister
186 * @Return: value for the serial bitrate register
187 * @Descr: register value depends on clock frequency and baudrate
188 ***********************************************************************/
190 static unsigned int calcBitrateRegister( void )
192 return ( NS9750_SER_BITRATE_EBIT |
193 NS9750_SER_BITRATE_CLKMUX_BCLK |
194 NS9750_SER_BITRATE_TMODE |
195 NS9750_SER_BITRATE_TCDR_16 |
196 NS9750_SER_BITRATE_RCDR_16 |
197 ( ( ( ( CONFIG_SYS_CLK_FREQ / 8 ) / /* BBUS clock,[1] Fig. 38 */
198 ( gd->baudrate * 16 ) ) - 1 ) &
199 NS9750_SER_BITRATE_N_MA ) );
202 /***********************************************************************
203 * @Function: calcRxCharGapRegister
204 * @Return: value for the character gap timer register
205 * @Descr: register value depends on clock frequency and baudrate. Currently 0
206 * is used as there is a bug with the gap timer in PLL bypass mode.
207 ***********************************************************************/
209 static unsigned int calcRxCharGapRegister( void )
211 return NS9750_SER_RX_CHAR_TIMER_TRUN;
214 #endif /* CFG_NS9750_UART */