]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/serial/ns9750_serial.c
Merge remote-tracking branch 'origin/master'
[karo-tx-uboot.git] / drivers / serial / ns9750_serial.c
1 /***********************************************************************
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 #include "ns9750_bbus.h"        /* for GPIOs */
32 #include "ns9750_ser.h"         /* for serial configuration */
33
34 DECLARE_GLOBAL_DATA_PTR;
35
36 #if !defined(CONFIG_CONS_INDEX)
37 #error "No console index specified."
38 #endif
39
40 #define CONSOLE CONFIG_CONS_INDEX
41
42 static unsigned int calcBitrateRegister( void );
43 static unsigned int calcRxCharGapRegister( void );
44
45 static char cCharsAvailable; /* Numbers of chars in unCharCache */
46 static unsigned int unCharCache; /* unCharCache is only valid if
47                                   * cCharsAvailable > 0 */
48
49 /***********************************************************************
50  * @Function: serial_init
51  * @Return: 0
52  * @Descr: configures GPIOs and UART. Requires BBUS Master Reset turned off
53  ***********************************************************************/
54
55 static int ns9750_serial_init(void)
56 {
57         unsigned int aunGPIOTxD[] = { 0, 8, 40, 44 };
58         unsigned int aunGPIORxD[] = { 1, 9, 41, 45 };
59
60         cCharsAvailable = 0;
61
62         /* configure TxD and RxD pins for their special function */
63         set_gpio_cfg_reg_val( aunGPIOTxD[ CONSOLE ],
64                               NS9750_GPIO_CFG_FUNC_0 | NS9750_GPIO_CFG_OUTPUT );
65         set_gpio_cfg_reg_val( aunGPIORxD[ CONSOLE ],
66                               NS9750_GPIO_CFG_FUNC_0 | NS9750_GPIO_CFG_INPUT );
67
68         /* configure serial engine */
69         *get_ser_reg_addr_channel( NS9750_SER_CTRL_A, CONSOLE ) =
70                 NS9750_SER_CTRL_A_CE |
71                 NS9750_SER_CTRL_A_STOP |
72                 NS9750_SER_CTRL_A_WLS_8;
73
74         serial_setbrg();
75
76         *get_ser_reg_addr_channel( NS9750_SER_CTRL_B, CONSOLE ) =
77                 NS9750_SER_CTRL_B_RCGT;
78
79         return 0;
80 }
81
82 /***********************************************************************
83  * @Function: serial_putc
84  * @Return: n/a
85  * @Descr: writes one character to the FIFO. Blocks until FIFO is not full
86  ***********************************************************************/
87
88 static void ns9750_serial_putc(const char c)
89 {
90         if (c == '\n')
91                 serial_putc( '\r' );
92
93         while (!(*get_ser_reg_addr_channel( NS9750_SER_STAT_A, CONSOLE) &
94                  NS9750_SER_STAT_A_TRDY ) ) {
95                 /* do nothing, wait for characters in FIFO sent */
96         }
97
98         *(volatile char*) get_ser_reg_addr_channel( NS9750_SER_FIFO,
99                                                     CONSOLE) = c;
100 }
101
102 /***********************************************************************
103  * @Function: serial_getc
104  * @Return: the character read
105  * @Descr: performs only 8bit accesses to the FIFO. No error handling
106  ***********************************************************************/
107
108 static int ns9750_serial_getc(void)
109 {
110         int i;
111
112         while (!serial_tstc() ) {
113                 /* do nothing, wait for incoming characters */
114         }
115
116         /*  at least one character in unCharCache */
117         i = (int) (unCharCache & 0xff);
118
119         unCharCache >>= 8;
120         cCharsAvailable--;
121
122         return i;
123 }
124
125 /***********************************************************************
126  * @Function: serial_tstc
127  * @Return: 0 if no input available, otherwise != 0
128  * @Descr: checks for incoming FIFO not empty. Stores the incoming chars in
129  *         unCharCache and the numbers of characters in cCharsAvailable
130  ***********************************************************************/
131
132 static int ns9750_serial_tstc(void)
133 {
134         unsigned int unRegCache;
135
136         if ( cCharsAvailable )
137                 return 1;
138
139         unRegCache = *get_ser_reg_addr_channel( NS9750_SER_STAT_A,CONSOLE );
140         if( unRegCache & NS9750_SER_STAT_A_RBC ) {
141                 *get_ser_reg_addr_channel( NS9750_SER_STAT_A, CONSOLE ) =
142                         NS9750_SER_STAT_A_RBC;
143                 unRegCache = *get_ser_reg_addr_channel( NS9750_SER_STAT_A,
144                                                         CONSOLE );
145         }
146
147         if ( unRegCache & NS9750_SER_STAT_A_RRDY ) {
148                 cCharsAvailable = (unRegCache & NS9750_SER_STAT_A_RXFDB_MA)>>20;
149                 if ( !cCharsAvailable )
150                         cCharsAvailable = 4;
151
152                 unCharCache = *get_ser_reg_addr_channel( NS9750_SER_FIFO,
153                                                          CONSOLE );
154                 return 1;
155         }
156
157         /* no chars available */
158         return 0;
159 }
160
161 static void ns9750_serial_setbrg(void)
162 {
163         *get_ser_reg_addr_channel( NS9750_SER_BITRATE, CONSOLE ) =
164                 calcBitrateRegister();
165         *get_ser_reg_addr_channel( NS9750_SER_RX_CHAR_TIMER, CONSOLE ) =
166                 calcRxCharGapRegister();
167 }
168
169 /***********************************************************************
170  * @Function: calcBitrateRegister
171  * @Return: value for the serial bitrate register
172  * @Descr: register value depends on clock frequency and baudrate
173  ***********************************************************************/
174
175 static unsigned int calcBitrateRegister( void )
176 {
177         return ( NS9750_SER_BITRATE_EBIT |
178                  NS9750_SER_BITRATE_CLKMUX_BCLK |
179                  NS9750_SER_BITRATE_TMODE |
180                  NS9750_SER_BITRATE_TCDR_16 |
181                  NS9750_SER_BITRATE_RCDR_16 |
182                  ( ( ( ( CONFIG_SYS_CLK_FREQ / 8 ) / /* BBUS clock,[1] Fig. 38 */
183                        ( gd->baudrate * 16 ) ) - 1 ) &
184                    NS9750_SER_BITRATE_N_MA ) );
185 }
186
187 /***********************************************************************
188  * @Function: calcRxCharGapRegister
189  * @Return: value for the character gap timer register
190  * @Descr: register value depends on clock frequency and baudrate. Currently 0
191  *         is used as there is a bug with the gap timer in PLL bypass mode.
192  ***********************************************************************/
193
194 static unsigned int calcRxCharGapRegister( void )
195 {
196         return NS9750_SER_RX_CHAR_TIMER_TRUN;
197 }
198
199 static struct serial_device ns9750_serial_drv = {
200         .name   = "ns9750_serial",
201         .start  = ns9750_serial_init,
202         .stop   = NULL,
203         .setbrg = ns9750_serial_setbrg,
204         .putc   = ns9750_serial_putc,
205         .puts   = default_serial_puts,
206         .getc   = ns9750_serial_getc,
207         .tstc   = ns9750_serial_tstc,
208 };
209
210 void ns9750_serial_initialize(void)
211 {
212         serial_register(&ns9750_serial_drv);
213 }
214
215 __weak struct serial_device *default_serial_console(void)
216 {
217         return &ns9750_serial_drv;
218 }