1 /***********************************************************************
3 * Copyright (C) 2004 by FS Forth-Systeme GmbH.
6 * $Id: ns9750_serial.c,v 1.1.1.1 2010-10-18 09:56:07 roylin 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 #define CONSOLE CONFIG_CONS_INDEX
38 static unsigned int calcBitrateRegister( void );
39 static unsigned int calcRxCharGapRegister( void );
41 static char cCharsAvailable
; /* Numbers of chars in unCharCache */
42 static unsigned int unCharCache
; /* unCharCache is only valid if
43 * cCharsAvailable > 0 */
45 /***********************************************************************
46 * @Function: serial_init
48 * @Descr: configures GPIOs and UART. Requires BBUS Master Reset turned off
49 ***********************************************************************/
51 int serial_init( void )
53 unsigned int aunGPIOTxD
[] = { 0, 8, 40, 44 };
54 unsigned int aunGPIORxD
[] = { 1, 9, 41, 45 };
58 /* configure TxD and RxD pins for their special function */
59 set_gpio_cfg_reg_val( aunGPIOTxD
[ CONSOLE
],
60 NS9750_GPIO_CFG_FUNC_0
| NS9750_GPIO_CFG_OUTPUT
);
61 set_gpio_cfg_reg_val( aunGPIORxD
[ CONSOLE
],
62 NS9750_GPIO_CFG_FUNC_0
| NS9750_GPIO_CFG_INPUT
);
64 /* configure serial engine */
65 *get_ser_reg_addr_channel( NS9750_SER_CTRL_A
, CONSOLE
) =
66 NS9750_SER_CTRL_A_CE
|
67 NS9750_SER_CTRL_A_STOP
|
68 NS9750_SER_CTRL_A_WLS_8
;
72 *get_ser_reg_addr_channel( NS9750_SER_CTRL_B
, CONSOLE
) =
73 NS9750_SER_CTRL_B_RCGT
;
78 /***********************************************************************
79 * @Function: serial_putc
81 * @Descr: writes one character to the FIFO. Blocks until FIFO is not full
82 ***********************************************************************/
84 void serial_putc( const char c
)
89 while (!(*get_ser_reg_addr_channel( NS9750_SER_STAT_A
, CONSOLE
) &
90 NS9750_SER_STAT_A_TRDY
) ) {
91 /* do nothing, wait for characters in FIFO sent */
94 *(volatile char*) get_ser_reg_addr_channel( NS9750_SER_FIFO
,
98 /***********************************************************************
99 * @Function: serial_puts
101 * @Descr: writes non-zero string to the FIFO.
102 ***********************************************************************/
104 void serial_puts( const char *s
)
111 /***********************************************************************
112 * @Function: serial_getc
113 * @Return: the character read
114 * @Descr: performs only 8bit accesses to the FIFO. No error handling
115 ***********************************************************************/
117 int serial_getc( void )
121 while (!serial_tstc() ) {
122 /* do nothing, wait for incoming characters */
125 /* at least one character in unCharCache */
126 i
= (int) (unCharCache
& 0xff);
134 /***********************************************************************
135 * @Function: serial_tstc
136 * @Return: 0 if no input available, otherwise != 0
137 * @Descr: checks for incoming FIFO not empty. Stores the incoming chars in
138 * unCharCache and the numbers of characters in cCharsAvailable
139 ***********************************************************************/
141 int serial_tstc( void )
143 unsigned int unRegCache
;
145 if ( cCharsAvailable
)
148 unRegCache
= *get_ser_reg_addr_channel( NS9750_SER_STAT_A
,CONSOLE
);
149 if( unRegCache
& NS9750_SER_STAT_A_RBC
) {
150 *get_ser_reg_addr_channel( NS9750_SER_STAT_A
, CONSOLE
) =
151 NS9750_SER_STAT_A_RBC
;
152 unRegCache
= *get_ser_reg_addr_channel( NS9750_SER_STAT_A
,
156 if ( unRegCache
& NS9750_SER_STAT_A_RRDY
) {
157 cCharsAvailable
= (unRegCache
& NS9750_SER_STAT_A_RXFDB_MA
)>>20;
158 if ( !cCharsAvailable
)
161 unCharCache
= *get_ser_reg_addr_channel( NS9750_SER_FIFO
,
166 /* no chars available */
170 void serial_setbrg( void )
172 *get_ser_reg_addr_channel( NS9750_SER_BITRATE
, CONSOLE
) =
173 calcBitrateRegister();
174 *get_ser_reg_addr_channel( NS9750_SER_RX_CHAR_TIMER
, CONSOLE
) =
175 calcRxCharGapRegister();
178 /***********************************************************************
179 * @Function: calcBitrateRegister
180 * @Return: value for the serial bitrate register
181 * @Descr: register value depends on clock frequency and baudrate
182 ***********************************************************************/
184 static unsigned int calcBitrateRegister( void )
186 DECLARE_GLOBAL_DATA_PTR
;
188 return ( NS9750_SER_BITRATE_EBIT
|
189 NS9750_SER_BITRATE_CLKMUX_BCLK
|
190 NS9750_SER_BITRATE_TMODE
|
191 NS9750_SER_BITRATE_TCDR_16
|
192 NS9750_SER_BITRATE_RCDR_16
|
193 ( ( ( ( CONFIG_SYS_CLK_FREQ
/ 8 ) / /* BBUS clock,[1] Fig. 38 */
194 ( gd
->baudrate
* 16 ) ) - 1 ) &
195 NS9750_SER_BITRATE_N_MA
) );
198 /***********************************************************************
199 * @Function: calcRxCharGapRegister
200 * @Return: value for the character gap timer register
201 * @Descr: register value depends on clock frequency and baudrate. Currently 0
202 * is used as there is a bug with the gap timer in PLL bypass mode.
203 ***********************************************************************/
205 static unsigned int calcRxCharGapRegister( void )
207 DECLARE_GLOBAL_DATA_PTR
;
209 return NS9750_SER_RX_CHAR_TIMER_TRUN
;
212 #endif /* CFG_NS9750_UART */