blob: a8d7f6bd98aa813aed15f2da0d6060a5f028dfc8 [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
xf.li7ccf8372024-03-07 00:08:02 -0800175/*******************************************************************************
176 * Function: check if user press ctrl +x from uboot serial
177 * Description: read console flag before start kernel
178 * Parameters: us °´¼ü·À¶¶Ê±¼ä£¬wait µÈ´ý°´¼ü×Üʱ¼ä
179 * Input:
180 *
181 * Output:
182 *
183 * Returns:
184 *
185 *
186 * Others:
187 ********************************************************************************/
xf.lif1aed282024-02-06 00:31:51 -0800188extern bool g_flg_ctrl_x;
189void serial_getc_cons_key_flag(unsigned long us,unsigned int wait)
190{
191 int c1,c2;
192 static int cnt_wait = 0;
193 while(1)
194 {
195 if((__REG(UART_FR) & UART_RXFE) == 0)
196 c1 = (__REG(UART_DR) & 0xff);
197 udelay(us);
198 if((__REG(UART_FR) & UART_RXFE) == 0)
199 c2 = (__REG(UART_DR) & 0xff);
200 if((c1 == c2) && (c1 == 24)){
201 g_flg_ctrl_x = false;
202 debug("ctrk+x:get.\n");
203 return 1;
204 }
205 if(cnt_wait > wait){
206 debug("ctrk+x:not get.\n");
207 g_flg_ctrl_x = true;
208 cnt_wait = 0;
209 return 0;
210 }else{
211 cnt_wait++;
212 }
213 }
214 return 0;
215}
216#endif
lh9ed821d2023-04-07 01:36:19 -0700217#ifdef CONFIG_HWFLOW
218static int hwflow = 0; /* turned off by default */
219int hwflow_onoff(int on)
220{
221 switch(on) {
222 case 0:
223 default:
224 break; /* return current */
225 case 1:
226 hwflow = 1; /* turn on */
227 break;
228 case -1:
229 hwflow = 0; /* turn off */
230 break;
231 }
232 return hwflow;
233}
234#endif
235
236#ifdef CONFIG_MODEM_SUPPORT
237static int be_quiet = 0;
238void disable_putc(void)
239{
240 be_quiet = 1;
241}
242
243void enable_putc(void)
244{
245 be_quiet = 0;
246}
247#endif
248
249
250/*******************************************************************************
251 * Function:
252 * Description: Ïò´®¿ÚÊä³öÒ»¸ö×Ö½ÚÊý¾Ý
253 * Parameters:
254 * Input:
255 *
256 * Output:
257 *
258 * Returns:
259 *
260 *
261 * Others:
262 ********************************************************************************/
263void serial_putc(const char c)
264{
265#ifdef CONFIG_MODEM_SUPPORT
266 if (be_quiet)
267 return;
268#endif
269
270 /* µÈ´ý·¢ËÍ FIFO ²»Âú£¬¿ÉÒÔдÊý¾Ý */
271 while ((__REG(UART_FR) & UART_TXFF) != 0 );
272 __REG(UART_DR) = c;
273
274 /* If \n, also do \r */
275 if (c == '\n')
276 serial_putc('\r');
277}
278
279
280/*******************************************************************************
281 * Function:
282 * Description: ²âÊÔ½ÓÊÕ FIFO ÖÐÊÇ·ñÓÐÊý¾Ý£» Test whether a character is in the RX buffer
283 * Parameters:
284 * Input:
285 *
286 * Output:
287 *
288 * Returns:
289 *
290 *
291 * Others:
292 ********************************************************************************/
293int serial_tstc(void)
294{
295 if( __REG(UART_FR) & UART_RXFE )
296 return 0; //µÈÓÚ1ʱ£¬½ÓÊÕFIFOΪ¿Õ£¬Ã»ÓÐÊý¾Ý
297 else
298 return 1; //µÈÓÚ0ʱ£¬½ÓÊÕFIFO²»Îª¿Õ£¬ÓÐÊý¾Ý;
299}
300
301
302/*******************************************************************************
303 * Function:
304 * Description:
305 * Parameters:
306 * Input:
307 *
308 * Output:
309 *
310 * Returns:
311 *
312 *
313 * Others:
314 ********************************************************************************/
315void serial_puts(const char *s)
316{
317 while (*s)
318 serial_putc(*s++);
319}
320/*******************************************************************************
321 * Function:
322 * Description:
323 * Parameters:
324 * Input:
325 *
326 * Output:
327 *
328 * Returns:
329 *
330 *
331 * Others:
332 ********************************************************************************/
333
334int UART_Read(char *pchBuf, int dwLen)
335{
336 int i = 0;
337 for(i=0;i<dwLen;i++)
338 {
339 pchBuf[i] = (char)serial_getc();
340 }
341
342 return dwLen;
343}
344
345/*******************************************************************************
346 * Function:
347 * Description:
348 * Parameters:
349 * Input:
350 *
351 * Output:
352 *
353 * Returns:
354 *
355 *
356 * Others:
357 ********************************************************************************/
358
359int UART_Write(char *pchBuf, int dwLen)
360
361{
362 int i = 0;
363 for(i=0;i<dwLen;i++)
364 {
365 serial_putc(pchBuf[i]);
366 }
367
368 return dwLen;
369}
370
371
372int UART_CmdRead(char *pchBuf, int dwLen)
373{
374 int i = 0;
375 for(i=0;i<64;i++)
376 {
377 pchBuf[i] = (char)serial_getc();
378 if(pchBuf[i] == 0)
379 break;
380 }
381
382 return (i+1);
383}
384
385