lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 ZXIC Inc. |
| 3 | * |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <asm/arch/uart.h> |
| 8 | #include <asm/io.h> |
| 9 | #include <asm/arch/top_clock.h> |
| 10 | #include <asm/arch/gpio.h> |
| 11 | |
| 12 | #define CONFIG_BAUDRATE 921600 |
| 13 | |
| 14 | /******************************************************************************* |
| 15 | * Function: serial_gpio_config |
| 16 | * Description: |
| 17 | * Parameters: |
| 18 | * Input: |
| 19 | * |
| 20 | * Output: |
| 21 | * |
| 22 | * Returns: |
| 23 | * |
| 24 | * |
| 25 | * Others: |
| 26 | ********************************************************************************/ |
| 27 | static void serial_gpio_config(void) |
| 28 | { |
| 29 | #ifdef CONFIG_ZX297520V3E_CPE |
| 30 | unsigned int tmp = 0; |
| 31 | /*uart0_cts----->uart1_txd*/ |
| 32 | __REG(PAD_TOP_FUNC_BASE+0x20) |= (1<<2); |
| 33 | tmp = __REG(PAD_PD_FUNC_BASE+0x4); |
| 34 | tmp &= ~(0x3<<2); |
| 35 | __REG(PAD_PD_FUNC_BASE+0x4) = tmp | (0x1<<2); |
| 36 | |
| 37 | /*uart0_rts----->uart1_rxd*/ |
| 38 | __REG(PAD_TOP_FUNC_BASE+0x20) |= (1<<3); |
| 39 | tmp = __REG(PAD_PD_FUNC_BASE+0x4); |
| 40 | tmp &= ~(0x3<<4); |
| 41 | __REG(PAD_PD_FUNC_BASE+0x4) = tmp | (0x1<<4); |
| 42 | |
| 43 | #endif |
| 44 | } |
| 45 | |
| 46 | /******************************************************************************* |
| 47 | * Function: |
| 48 | * Description: ÉèÖô®¿ÚµÄ²¨ÌØÂÊ |
| 49 | * Parameters: |
| 50 | * Input: |
| 51 | * |
| 52 | * Output: |
| 53 | * |
| 54 | * Returns: |
| 55 | * |
| 56 | * |
| 57 | * Others: |
| 58 | ********************************************************************************/ |
| 59 | static void serial_setbrg(void) |
| 60 | { |
| 61 | u32 uartClk = 26000000; |
| 62 | u32 ibrd = uartClk / (CONFIG_BAUDRATE << 4); |
| 63 | u32 fbrd = (((uartClk-((ibrd*CONFIG_BAUDRATE)<<4))<<6)+(CONFIG_BAUDRATE<<3))/(CONFIG_BAUDRATE<<4); |
| 64 | __REG(UART_IBRD) = ibrd; |
| 65 | __REG(UART_FBRD) = fbrd; |
| 66 | #if 0 |
| 67 | if( __REG(PLL_624_208_CFG0_REG) & PLL_624_208_CFG0_LOCK ) |
| 68 | { |
| 69 | __REG(UART_IBRD) = 0x38; |
| 70 | __REG(UART_FBRD) = 0x1b; |
| 71 | } |
| 72 | else |
| 73 | { |
| 74 | __REG(UART_IBRD) = 0x2; /* midify !!! */ |
| 75 | __REG(UART_FBRD) = 0x16; |
| 76 | } |
| 77 | #endif |
| 78 | } |
| 79 | |
| 80 | |
| 81 | /******************************************************************************* |
| 82 | * Function: |
| 83 | * Description: ³õʼ»¯´®¿Ú²ÎÊý 115200 |
| 84 | * Parameters: |
| 85 | * Input: |
| 86 | * |
| 87 | * Output: |
| 88 | * |
| 89 | * Returns: |
| 90 | * |
| 91 | * |
| 92 | * Others: |
| 93 | ********************************************************************************/ |
| 94 | void uart_init(void) |
| 95 | { |
| 96 | int count = 3000; |
| 97 | |
| 98 | while(__REG(UART_FR) & UART_FR_TXBUSY){ |
| 99 | |
| 100 | if(--count == 0) |
| 101 | { |
| 102 | break; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /* Òý½Å¸´ÓÃÉèÖà */ |
| 107 | serial_gpio_config(); |
| 108 | |
| 109 | /* ½ûÓÃUART */ |
| 110 | __REG(UART_ICR) = 0xffff; |
| 111 | __REG(UART_CR) &= (~(UART_EN | UART_TXE)); |
| 112 | |
| 113 | |
| 114 | |
| 115 | /* ²¨ÌØÂÊÉèÖà */ |
| 116 | serial_setbrg(); |
| 117 | |
| 118 | /* ÉèÖô«Êä¸ñʽΪ: ÎÞУÑé/1λֹͣλ/8λÊý¾Ýλ/ʹÄÜ FIFO £»¼Ä´æÆ÷³õʼ»¯Îª 0x0 */ |
| 119 | __REG(UART_LCR_H) &= (~(UART_BREAK | UART_PEN | UART_STP2 )); |
| 120 | __REG(UART_LCR_H) |= (UART_WLEN_8 | UART_FEN ); |
| 121 | |
| 122 | /* ÆÁ±ÎËùÓеÄuartÄ£¿é·¢³öµÄÖÐ¶Ï */ |
| 123 | __REG(UART_IMSC) &= (~UART_INT_MASK); |
| 124 | |
| 125 | /* ʹÄÜUART */ |
| 126 | __REG(UART_CR) |= (UART_TXE | UART_EN ); |
| 127 | |
| 128 | } |
| 129 | |
| 130 | |
| 131 | |
| 132 | /******************************************************************************* |
| 133 | * Function: uart_putc |
| 134 | * Description: |
| 135 | * Parameters: |
| 136 | * Input: |
| 137 | * |
| 138 | * Output: |
| 139 | * |
| 140 | * Returns: |
| 141 | * |
| 142 | * |
| 143 | * Others: |
| 144 | ********************************************************************************/ |
| 145 | void uart_putc(const char c) |
| 146 | { |
| 147 | /* µÈ´ý·¢ËÍ FIFO ²»Âú£¬¿ÉÒÔдÊý¾Ý */ |
| 148 | while ((__REG(UART_FR) & UART_TXFF) != 0 ); |
| 149 | __REG(UART_DR) = c; |
| 150 | |
| 151 | /* If \n, also do \r */ |
| 152 | if (c == '\n') |
| 153 | uart_putc('\r'); |
| 154 | } |
| 155 | |
| 156 | |
| 157 | /******************************************************************************* |
| 158 | * Function: uart_puts |
| 159 | * Description: |
| 160 | * Parameters: |
| 161 | * Input: |
| 162 | * |
| 163 | * Output: |
| 164 | * |
| 165 | * Returns: |
| 166 | * |
| 167 | * |
| 168 | * Others: |
| 169 | ********************************************************************************/ |
| 170 | void uart_puts (const char *s) |
| 171 | { |
| 172 | while (*s) { |
| 173 | uart_putc (*s++); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /******************************************************************************* |
| 178 | * Function: uart_getc |
| 179 | * Description: |
| 180 | * Parameters: |
| 181 | * Input: |
| 182 | * |
| 183 | * Output: |
| 184 | * |
| 185 | * Returns: |
| 186 | * |
| 187 | * |
| 188 | * Others: |
| 189 | ********************************************************************************/ |
| 190 | char uart_getc(void) |
| 191 | { |
| 192 | while ((__REG(UART_FR) & UART_RXFE) != 0 ); |
| 193 | return (__REG(UART_DR) & 0xff); |
| 194 | } |
| 195 | |
| 196 | /******************************************************************************* |
| 197 | * Function: uart_tstc |
| 198 | * Description: |
| 199 | * Parameters: |
| 200 | * Input: |
| 201 | * |
| 202 | * Output: |
| 203 | * |
| 204 | * Returns: |
| 205 | * |
| 206 | * |
| 207 | * Others: |
| 208 | ********************************************************************************/ |
| 209 | int uart_tstc(void) |
| 210 | { |
| 211 | if( __REG(UART_FR) & UART_RXFE ) |
| 212 | return 0; //µÈÓÚ1ʱ£¬½ÓÊÕFIFOΪ¿Õ£¬Ã»ÓÐÊý¾Ý |
| 213 | else |
| 214 | return 1; //µÈÓÚ0ʱ£¬½ÓÊÕFIFO²»Îª¿Õ£¬ÓÐÊý¾Ý; |
| 215 | } |
| 216 | |
| 217 | /******************************************************************************* |
| 218 | * Function: |
| 219 | * Description: |
| 220 | * Parameters: |
| 221 | * Input: |
| 222 | * |
| 223 | * Output: |
| 224 | * |
| 225 | * Returns: |
| 226 | * |
| 227 | * |
| 228 | * Others: |
| 229 | ********************************************************************************/ |
| 230 | |
| 231 | int UART_Read(char *pchBuf, int dwLen) |
| 232 | { |
| 233 | int i = 0; |
| 234 | for(i=0;i<dwLen;i++) |
| 235 | { |
| 236 | pchBuf[i] = (char)uart_getc(); |
| 237 | } |
| 238 | |
| 239 | return dwLen; |
| 240 | } |
| 241 | |
| 242 | /******************************************************************************* |
| 243 | * Function: |
| 244 | * Description: |
| 245 | * Parameters: |
| 246 | * Input: |
| 247 | * |
| 248 | * Output: |
| 249 | * |
| 250 | * Returns: |
| 251 | * |
| 252 | * |
| 253 | * Others: |
| 254 | ********************************************************************************/ |
| 255 | |
| 256 | int UART_Write(char *pchBuf, int dwLen) |
| 257 | |
| 258 | { |
| 259 | int i = 0; |
| 260 | for(i=0;i<dwLen;i++) |
| 261 | { |
| 262 | uart_putc(pchBuf[i]); |
| 263 | } |
| 264 | |
| 265 | return dwLen; |
| 266 | } |
| 267 | |
| 268 | int UART_Check_Sync(char *pchBuf, int dwLen) |
| 269 | { |
| 270 | int ret = 0; |
| 271 | ret = UART_Read(pchBuf,1); |
| 272 | |
| 273 | if(0x5A!=*pchBuf) |
| 274 | { |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | return ret; |
| 279 | } |
| 280 | |
| 281 | |
| 282 | |