blob: c5eb337247a8bc9e6470ae5dbcae41b6220e1101 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*******************************************************************************
2 * Copyright (C) 2016, ZIXC Corporation.
3 *
4 * File Name:
5 * File Mark:
6 * Description:
7 * Others:
8 * Version: v1.0
9 * Author: zhouqi
10 * Date: 2012-10-23
11 * History 1:
12 * Date:
13 * Version:
14 * Author:
15 * Modification:
16 * History 2:
17 ********************************************************************************/
18
19#include <common.h>
20#include <asm/arch/hardware.h>
21#include <asm/arch/uart.h>
22#include <asm/arch/lsp_crpm.h>
23#include <config.h>
24
25
26DECLARE_GLOBAL_DATA_PTR;
27
28/*******************************************************************************
29 * Function:
30 * Description: ÉèÖô®¿ÚµÄʱÖÓ
31 * Parameters:
32 * Input:
33 *
34 * Output:
35 *
36 * Returns:
37 *
38 *
39 * Others:
40 ********************************************************************************/
41unsigned int serial_getclock(void)
42{
43
44 #if 0
45 if( __REG(PLL_624_208_CFG0_REG) & PLL_624_208_CFG0_LOCK )
46 return 104000000;
47 else
48 {
49 return 26000000/6;
50 }
51 #endif
52 return 26000000;
53}
54
55/*******************************************************************************
56 * Function: serial_gpio_config
57 * Description:
58 * Parameters:
59 * Input:
60 *
61 * Output:
62 *
63 * Returns:
64 *
65 *
66 * Others:
67 ********************************************************************************/
68static void serial_gpio_config(void)
69{
70#ifdef CONFIG_ZX297520V3E_CPE
71 unsigned int tmp = 0;
72 /*uart0_cts----->uart1_txd*/
73 __REG(PAD_TOP_FUNC_BASE+0x20) |= (1<<2);
74 tmp = __REG(PAD_PD_FUNC_BASE+0x4);
75 tmp &= ~(0x3<<2);
76 __REG(PAD_PD_FUNC_BASE+0x4) = tmp | (0x1<<2);
77
78 /*uart0_rts----->uart1_rxd*/
79 __REG(PAD_TOP_FUNC_BASE+0x20) |= (1<<3);
80 tmp = __REG(PAD_PD_FUNC_BASE+0x4);
81 tmp &= ~(0x3<<4);
82 __REG(PAD_PD_FUNC_BASE+0x4) = tmp | (0x1<<4);
83
84#endif
85}
86
87
88/*******************************************************************************
89 * Function:
90 * Description: ÉèÖô®¿ÚµÄ²¨ÌØÂÊ
91 * Parameters:
92 * Input:
93 *
94 * Output:
95 *
96 * Returns:
97 *
98 *
99 * Others:
100 ********************************************************************************/
101void serial_setbrg(void)
102{
103 u32 uartClk = serial_getclock();
104 u32 ibrd = uartClk / (CONFIG_BAUDRATE << 4);
105 u32 fbrd = (((uartClk-((ibrd*CONFIG_BAUDRATE)<<4))<<6)+(CONFIG_BAUDRATE<<3))/(CONFIG_BAUDRATE<<4);
106 __REG(UART_IBRD) = ibrd;
107 __REG(UART_FBRD) = fbrd;
108}
109
110
111/*******************************************************************************
112 * Function:
113 * Description: ³õʼ»¯´®¿Ú²ÎÊý 115200
114 * Parameters:
115 * Input:
116 *
117 * Output:
118 *
119 * Returns:
120 *
121 *
122 * Others:
123 ********************************************************************************/
124int serial_init(void)
125{
126 //Òý½Å¸´ÓÃÉèÖÃ,ʹÓÃUART1(2)
127 //gpio_set_reuse(UART0_TXD, 0); //FPGA NOUSED
128 //gpio_set_reuse(UART0_RXD, 0);
129
130 /* Òý½Å¸´ÓÃÉèÖà */
131 serial_gpio_config();
132
133 // ½ûÓÃUART
134 __REG(UART_ICR) = 0xffff;
135 __REG(UART_CR) &= (~UART_EN );
136
137 // ²¨ÌØÂÊÉèÖÃ
138 serial_setbrg();
139
140 // ÉèÖô«Êä¸ñʽΪ: ÎÞУÑé/1λֹͣλ/8λÊý¾Ýλ/ʹÄÜ FIFO £»¼Ä´æÆ÷³õʼ»¯Îª 0x0
141 __REG(UART_LCR_H) &= (~(UART_BREAK | UART_PEN | UART_STP2 ));
142 __REG(UART_LCR_H) |= (UART_WLEN_8 | UART_FEN );
143
144 // ÆÁ±ÎËùÓеÄuartÄ£¿é·¢³öµÄÖжÏ
145 __REG(UART_IMSC) &= (~UART_INT_MASK);
146
147 // ʹÄÜUART
148 __REG(UART_CR) |= (UART_TXE | UART_RXE | UART_EN );
149
150 return 0;
151}
152
153
154/*******************************************************************************
155 * Function:
156 * Description: ´Ó´®¿Ú¶Á³öÒ»¸ö×Ö½ÚÊý¾Ý
157 * Parameters:
158 * Input:
159 *
160 * Output:
161 *
162 * Returns:
163 *
164 *
165 * Others:
166 ********************************************************************************/
167int serial_getc(void)
168{
169 while ((__REG(UART_FR) & UART_RXFE) != 0 );
170 return (__REG(UART_DR) & 0xff);
171}
172
173
xf.lif1aed282024-02-06 00:31:51 -0800174#ifdef CONFIG_CTRL_X_SWTCH_UART
175extern bool g_flg_ctrl_x;
176void serial_getc_cons_key_flag(unsigned long us,unsigned int wait)
177{
178 int c1,c2;
179 static int cnt_wait = 0;
180 while(1)
181 {
182 if((__REG(UART_FR) & UART_RXFE) == 0)
183 c1 = (__REG(UART_DR) & 0xff);
184 udelay(us);
185 if((__REG(UART_FR) & UART_RXFE) == 0)
186 c2 = (__REG(UART_DR) & 0xff);
187 if((c1 == c2) && (c1 == 24)){
188 g_flg_ctrl_x = false;
189 debug("ctrk+x:get.\n");
190 return 1;
191 }
192 if(cnt_wait > wait){
193 debug("ctrk+x:not get.\n");
194 g_flg_ctrl_x = true;
195 cnt_wait = 0;
196 return 0;
197 }else{
198 cnt_wait++;
199 }
200 }
201 return 0;
202}
203#endif
lh9ed821d2023-04-07 01:36:19 -0700204#ifdef CONFIG_HWFLOW
205static int hwflow = 0; /* turned off by default */
206int hwflow_onoff(int on)
207{
208 switch(on) {
209 case 0:
210 default:
211 break; /* return current */
212 case 1:
213 hwflow = 1; /* turn on */
214 break;
215 case -1:
216 hwflow = 0; /* turn off */
217 break;
218 }
219 return hwflow;
220}
221#endif
222
223#ifdef CONFIG_MODEM_SUPPORT
224static int be_quiet = 0;
225void disable_putc(void)
226{
227 be_quiet = 1;
228}
229
230void enable_putc(void)
231{
232 be_quiet = 0;
233}
234#endif
235
236
237/*******************************************************************************
238 * Function:
239 * Description: Ïò´®¿ÚÊä³öÒ»¸ö×Ö½ÚÊý¾Ý
240 * Parameters:
241 * Input:
242 *
243 * Output:
244 *
245 * Returns:
246 *
247 *
248 * Others:
249 ********************************************************************************/
250void serial_putc(const char c)
251{
252#ifdef CONFIG_MODEM_SUPPORT
253 if (be_quiet)
254 return;
255#endif
256
257 /* µÈ´ý·¢ËÍ FIFO ²»Âú£¬¿ÉÒÔдÊý¾Ý */
258 while ((__REG(UART_FR) & UART_TXFF) != 0 );
259 __REG(UART_DR) = c;
260
261 /* If \n, also do \r */
262 if (c == '\n')
263 serial_putc('\r');
264}
265
266
267/*******************************************************************************
268 * Function:
269 * Description: ²âÊÔ½ÓÊÕ FIFO ÖÐÊÇ·ñÓÐÊý¾Ý£» Test whether a character is in the RX buffer
270 * Parameters:
271 * Input:
272 *
273 * Output:
274 *
275 * Returns:
276 *
277 *
278 * Others:
279 ********************************************************************************/
280int serial_tstc(void)
281{
282 if( __REG(UART_FR) & UART_RXFE )
283 return 0; //µÈÓÚ1ʱ£¬½ÓÊÕFIFOΪ¿Õ£¬Ã»ÓÐÊý¾Ý
284 else
285 return 1; //µÈÓÚ0ʱ£¬½ÓÊÕFIFO²»Îª¿Õ£¬ÓÐÊý¾Ý;
286}
287
288
289/*******************************************************************************
290 * Function:
291 * Description:
292 * Parameters:
293 * Input:
294 *
295 * Output:
296 *
297 * Returns:
298 *
299 *
300 * Others:
301 ********************************************************************************/
302void serial_puts(const char *s)
303{
304 while (*s)
305 serial_putc(*s++);
306}
307/*******************************************************************************
308 * Function:
309 * Description:
310 * Parameters:
311 * Input:
312 *
313 * Output:
314 *
315 * Returns:
316 *
317 *
318 * Others:
319 ********************************************************************************/
320
321int UART_Read(char *pchBuf, int dwLen)
322{
323 int i = 0;
324 for(i=0;i<dwLen;i++)
325 {
326 pchBuf[i] = (char)serial_getc();
327 }
328
329 return dwLen;
330}
331
332/*******************************************************************************
333 * Function:
334 * Description:
335 * Parameters:
336 * Input:
337 *
338 * Output:
339 *
340 * Returns:
341 *
342 *
343 * Others:
344 ********************************************************************************/
345
346int UART_Write(char *pchBuf, int dwLen)
347
348{
349 int i = 0;
350 for(i=0;i<dwLen;i++)
351 {
352 serial_putc(pchBuf[i]);
353 }
354
355 return dwLen;
356}
357
358
359int UART_CmdRead(char *pchBuf, int dwLen)
360{
361 int i = 0;
362 for(i=0;i<64;i++)
363 {
364 pchBuf[i] = (char)serial_getc();
365 if(pchBuf[i] == 0)
366 break;
367 }
368
369 return (i+1);
370}
371
372