blob: 02c0d3952099c58cf4a8034adf55ae4f51ea8877 [file] [log] [blame]
wdenk5f495752004-02-26 23:46:20 +00001/***********************************************************************
2 *
3 * Copyright (C) 2004 by FS Forth-Systeme GmbH.
4 * All rights reserved.
5 *
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
11 *
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.
16 *
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.
21 *
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,
25 * MA 02111-1307 USA
26 *
27 ***********************************************************************/
28
29#include <common.h>
30
31#ifdef CFG_NS9750_UART
32
33#include "ns9750_bbus.h" /* for GPIOs */
34#include "ns9750_ser.h" /* for serial configuration */
35
Wolfgang Denk6405a152006-03-31 18:32:53 +020036DECLARE_GLOBAL_DATA_PTR;
37
Wolfgang Denk44df5612006-08-30 23:02:10 +020038#if !defined(CONFIG_CONS_INDEX)
39#error "No console index specified."
40#endif
41
wdenk5f495752004-02-26 23:46:20 +000042#define CONSOLE CONFIG_CONS_INDEX
43
44static unsigned int calcBitrateRegister( void );
45static unsigned int calcRxCharGapRegister( void );
46
47static char cCharsAvailable; /* Numbers of chars in unCharCache */
48static unsigned int unCharCache; /* unCharCache is only valid if
49 * cCharsAvailable > 0 */
50
51/***********************************************************************
52 * @Function: serial_init
53 * @Return: 0
54 * @Descr: configures GPIOs and UART. Requires BBUS Master Reset turned off
55 ***********************************************************************/
56
57int serial_init( void )
58{
59 unsigned int aunGPIOTxD[] = { 0, 8, 40, 44 };
60 unsigned int aunGPIORxD[] = { 1, 9, 41, 45 };
61
62 cCharsAvailable = 0;
63
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 );
69
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;
75
76 serial_setbrg();
77
78 *get_ser_reg_addr_channel( NS9750_SER_CTRL_B, CONSOLE ) =
79 NS9750_SER_CTRL_B_RCGT;
80
81 return 0;
82}
83
84/***********************************************************************
85 * @Function: serial_putc
86 * @Return: n/a
87 * @Descr: writes one character to the FIFO. Blocks until FIFO is not full
88 ***********************************************************************/
89
90void serial_putc( const char c )
91{
92 if (c == '\n')
93 serial_putc( '\r' );
94
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 */
98 }
99
100 *(volatile char*) get_ser_reg_addr_channel( NS9750_SER_FIFO,
101 CONSOLE) = c;
102}
103
104/***********************************************************************
105 * @Function: serial_puts
106 * @Return: n/a
107 * @Descr: writes non-zero string to the FIFO.
108 ***********************************************************************/
109
110void serial_puts( const char *s )
111{
112 while (*s) {
113 serial_putc( *s++ );
114 }
115}
116
117/***********************************************************************
118 * @Function: serial_getc
119 * @Return: the character read
120 * @Descr: performs only 8bit accesses to the FIFO. No error handling
121 ***********************************************************************/
122
123int serial_getc( void )
124{
125 int i;
126
127 while (!serial_tstc() ) {
128 /* do nothing, wait for incoming characters */
129 }
130
131 /* at least one character in unCharCache */
132 i = (int) (unCharCache & 0xff);
133
134 unCharCache >>= 8;
135 cCharsAvailable--;
136
137 return i;
138}
139
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 ***********************************************************************/
146
147int serial_tstc( void )
148{
149 unsigned int unRegCache;
150
151 if ( cCharsAvailable )
152 return 1;
153
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,
159 CONSOLE );
160 }
161
162 if ( unRegCache & NS9750_SER_STAT_A_RRDY ) {
163 cCharsAvailable = (unRegCache & NS9750_SER_STAT_A_RXFDB_MA)>>20;
164 if ( !cCharsAvailable )
165 cCharsAvailable = 4;
166
167 unCharCache = *get_ser_reg_addr_channel( NS9750_SER_FIFO,
168 CONSOLE );
169 return 1;
170 }
171
172 /* no chars available */
173 return 0;
174}
175
176void serial_setbrg( void )
177{
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();
182}
183
184/***********************************************************************
185 * @Function: calcBitrateRegister
186 * @Return: value for the serial bitrate register
187 * @Descr: register value depends on clock frequency and baudrate
188 ***********************************************************************/
189
190static unsigned int calcBitrateRegister( void )
191{
wdenk5f495752004-02-26 23:46:20 +0000192 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 ) );
200}
201
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 ***********************************************************************/
208
209static unsigned int calcRxCharGapRegister( void )
210{
wdenk5f495752004-02-26 23:46:20 +0000211 return NS9750_SER_RX_CHAR_TIMER_TRUN;
212}
213
214#endif /* CFG_NS9750_UART */