lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /******************************************************************************* |
| 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 | |
| 26 | DECLARE_GLOBAL_DATA_PTR; |
| 27 | |
| 28 | /******************************************************************************* |
| 29 | * Function: |
| 30 | * Description: ÉèÖô®¿ÚµÄʱÖÓ |
| 31 | * Parameters: |
| 32 | * Input: |
| 33 | * |
| 34 | * Output: |
| 35 | * |
| 36 | * Returns: |
| 37 | * |
| 38 | * |
| 39 | * Others: |
| 40 | ********************************************************************************/ |
| 41 | unsigned 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 | ********************************************************************************/ |
| 68 | static 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 | ********************************************************************************/ |
| 101 | void 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 | ********************************************************************************/ |
| 124 | int 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 | ********************************************************************************/ |
| 167 | int serial_getc(void) |
| 168 | { |
| 169 | while ((__REG(UART_FR) & UART_RXFE) != 0 ); |
| 170 | return (__REG(UART_DR) & 0xff); |
| 171 | } |
| 172 | |
| 173 | |
| 174 | #ifdef CONFIG_HWFLOW |
| 175 | static int hwflow = 0; /* turned off by default */ |
| 176 | int hwflow_onoff(int on) |
| 177 | { |
| 178 | switch(on) { |
| 179 | case 0: |
| 180 | default: |
| 181 | break; /* return current */ |
| 182 | case 1: |
| 183 | hwflow = 1; /* turn on */ |
| 184 | break; |
| 185 | case -1: |
| 186 | hwflow = 0; /* turn off */ |
| 187 | break; |
| 188 | } |
| 189 | return hwflow; |
| 190 | } |
| 191 | #endif |
| 192 | |
| 193 | #ifdef CONFIG_MODEM_SUPPORT |
| 194 | static int be_quiet = 0; |
| 195 | void disable_putc(void) |
| 196 | { |
| 197 | be_quiet = 1; |
| 198 | } |
| 199 | |
| 200 | void enable_putc(void) |
| 201 | { |
| 202 | be_quiet = 0; |
| 203 | } |
| 204 | #endif |
| 205 | |
| 206 | |
| 207 | /******************************************************************************* |
| 208 | * Function: |
| 209 | * Description: Ïò´®¿ÚÊä³öÒ»¸ö×Ö½ÚÊý¾Ý |
| 210 | * Parameters: |
| 211 | * Input: |
| 212 | * |
| 213 | * Output: |
| 214 | * |
| 215 | * Returns: |
| 216 | * |
| 217 | * |
| 218 | * Others: |
| 219 | ********************************************************************************/ |
| 220 | void serial_putc(const char c) |
| 221 | { |
| 222 | #ifdef CONFIG_MODEM_SUPPORT |
| 223 | if (be_quiet) |
| 224 | return; |
| 225 | #endif |
| 226 | |
| 227 | /* µÈ´ý·¢ËÍ FIFO ²»Âú£¬¿ÉÒÔдÊý¾Ý */ |
| 228 | while ((__REG(UART_FR) & UART_TXFF) != 0 ); |
| 229 | __REG(UART_DR) = c; |
| 230 | |
| 231 | /* If \n, also do \r */ |
| 232 | if (c == '\n') |
| 233 | serial_putc('\r'); |
| 234 | } |
| 235 | |
| 236 | |
| 237 | /******************************************************************************* |
| 238 | * Function: |
| 239 | * Description: ²âÊÔ½ÓÊÕ FIFO ÖÐÊÇ·ñÓÐÊý¾Ý£» Test whether a character is in the RX buffer |
| 240 | * Parameters: |
| 241 | * Input: |
| 242 | * |
| 243 | * Output: |
| 244 | * |
| 245 | * Returns: |
| 246 | * |
| 247 | * |
| 248 | * Others: |
| 249 | ********************************************************************************/ |
| 250 | int serial_tstc(void) |
| 251 | { |
| 252 | if( __REG(UART_FR) & UART_RXFE ) |
| 253 | return 0; //µÈÓÚ1ʱ£¬½ÓÊÕFIFOΪ¿Õ£¬Ã»ÓÐÊý¾Ý |
| 254 | else |
| 255 | return 1; //µÈÓÚ0ʱ£¬½ÓÊÕFIFO²»Îª¿Õ£¬ÓÐÊý¾Ý; |
| 256 | } |
| 257 | |
| 258 | |
| 259 | /******************************************************************************* |
| 260 | * Function: |
| 261 | * Description: |
| 262 | * Parameters: |
| 263 | * Input: |
| 264 | * |
| 265 | * Output: |
| 266 | * |
| 267 | * Returns: |
| 268 | * |
| 269 | * |
| 270 | * Others: |
| 271 | ********************************************************************************/ |
| 272 | void serial_puts(const char *s) |
| 273 | { |
| 274 | while (*s) |
| 275 | serial_putc(*s++); |
| 276 | } |
| 277 | /******************************************************************************* |
| 278 | * Function: |
| 279 | * Description: |
| 280 | * Parameters: |
| 281 | * Input: |
| 282 | * |
| 283 | * Output: |
| 284 | * |
| 285 | * Returns: |
| 286 | * |
| 287 | * |
| 288 | * Others: |
| 289 | ********************************************************************************/ |
| 290 | |
| 291 | int UART_Read(char *pchBuf, int dwLen) |
| 292 | { |
| 293 | int i = 0; |
| 294 | for(i=0;i<dwLen;i++) |
| 295 | { |
| 296 | pchBuf[i] = (char)serial_getc(); |
| 297 | } |
| 298 | |
| 299 | return dwLen; |
| 300 | } |
| 301 | |
| 302 | /******************************************************************************* |
| 303 | * Function: |
| 304 | * Description: |
| 305 | * Parameters: |
| 306 | * Input: |
| 307 | * |
| 308 | * Output: |
| 309 | * |
| 310 | * Returns: |
| 311 | * |
| 312 | * |
| 313 | * Others: |
| 314 | ********************************************************************************/ |
| 315 | |
| 316 | int UART_Write(char *pchBuf, int dwLen) |
| 317 | |
| 318 | { |
| 319 | int i = 0; |
| 320 | for(i=0;i<dwLen;i++) |
| 321 | { |
| 322 | serial_putc(pchBuf[i]); |
| 323 | } |
| 324 | |
| 325 | return dwLen; |
| 326 | } |
| 327 | |
| 328 | |
| 329 | int UART_CmdRead(char *pchBuf, int dwLen) |
| 330 | { |
| 331 | int i = 0; |
| 332 | for(i=0;i<64;i++) |
| 333 | { |
| 334 | pchBuf[i] = (char)serial_getc(); |
| 335 | if(pchBuf[i] == 0) |
| 336 | break; |
| 337 | } |
| 338 | |
| 339 | return (i+1); |
| 340 | } |
| 341 | |
| 342 | |