uart.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* Copyright Statement:
  2. *
  3. * This software/firmware and related documentation ("MediaTek Software") are
  4. * protected under relevant copyright laws. The information contained herein
  5. * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  6. * Without the prior written permission of MediaTek inc. and/or its licensors,
  7. * any reproduction, modification, use or disclosure of MediaTek Software,
  8. * and information contained herein, in whole or in part, shall be strictly prohibited.
  9. *
  10. * MediaTek Inc. (C) 2017. All rights reserved.
  11. *
  12. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  13. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  14. * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  15. * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  18. * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  19. * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  20. * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  21. * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  22. * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  23. * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  24. * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  25. * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  26. * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  27. * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  28. * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  29. * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  30. *
  31. * The following software/firmware and/or related documentation ("MediaTek Software")
  32. * have been modified by MediaTek Inc. All revisions are subject to any receiver's
  33. * applicable license agreements with MediaTek Inc.
  34. */
  35. #include <debug.h>
  36. #include <reg.h>
  37. #include <dev/uart.h>
  38. #include <platform/mt_typedefs.h>
  39. #include <platform/mt_reg_base.h>
  40. #include <platform/boot_mode.h>
  41. #include <platform/mt_gpio.h>
  42. #include <platform/sync_write.h>
  43. #include <string.h>
  44. #include "mt_uart.h"
  45. #include <mt_boot.h>
  46. #ifndef USER_BUILD
  47. #define UART_EARLY_CONSOLE_LEN 128
  48. #endif
  49. #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
  50. #define UART_SET_BITS(BS,REG) mt65xx_reg_sync_writel(DRV_Reg32(REG) | (u32)(BS), REG)
  51. #define UART_CLR_BITS(BC,REG) mt65xx_reg_sync_writel(DRV_Reg32(REG) & ~((u32)(BC)), REG)
  52. #define UART_BASE(uart) (uart)
  53. #define UART_RBR(uart) (UART_BASE(uart)+0x0) /* Read only */
  54. #define UART_THR(uart) (UART_BASE(uart)+0x0) /* Write only */
  55. #define UART_IER(uart) (UART_BASE(uart)+0x4)
  56. #define UART_IIR(uart) (UART_BASE(uart)+0x8) /* Read only */
  57. #define UART_FCR(uart) (UART_BASE(uart)+0x8) /* Write only */
  58. #define UART_LCR(uart) (UART_BASE(uart)+0xc)
  59. #define UART_MCR(uart) (UART_BASE(uart)+0x10)
  60. #define UART_LSR(uart) (UART_BASE(uart)+0x14)
  61. #define UART_MSR(uart) (UART_BASE(uart)+0x18)
  62. #define UART_SCR(uart) (UART_BASE(uart)+0x1c)
  63. #define UART_DLL(uart) (UART_BASE(uart)+0x0) /* Only when LCR.DLAB = 1 */
  64. #define UART_DLH(uart) (UART_BASE(uart)+0x4) /* Only when LCR.DLAB = 1 */
  65. #define UART_EFR(uart) (UART_BASE(uart)+0x8) /* Only when LCR = 0xbf */
  66. #define UART_XON1(uart) (UART_BASE(uart)+0x10) /* Only when LCR = 0xbf */
  67. #define UART_XON2(uart) (UART_BASE(uart)+0x14) /* Only when LCR = 0xbf */
  68. #define UART_XOFF1(uart) (UART_BASE(uart)+0x18) /* Only when LCR = 0xbf */
  69. #define UART_XOFF2(uart) (UART_BASE(uart)+0x1c) /* Only when LCR = 0xbf */
  70. #define UART_AUTOBAUD_EN(uart) (UART_BASE(uart)+0x20)
  71. #define UART_HIGHSPEED(uart) (UART_BASE(uart)+0x24)
  72. #define UART_SAMPLE_COUNT(uart) (UART_BASE(uart)+0x28)
  73. #define UART_SAMPLE_POINT(uart) (UART_BASE(uart)+0x2c)
  74. #define UART_AUTOBAUD_REG(uart) (UART_BASE(uart)+0x30)
  75. #define UART_RATE_FIX_AD(uart) (UART_BASE(uart)+0x34)
  76. #define UART_AUTOBAUD_SAMPLE(uart) (UART_BASE(uart)+0x38)
  77. #define UART_GUARD(uart) (UART_BASE(uart)+0x3c)
  78. #define UART_ESCAPE_DAT(uart) (UART_BASE(uart)+0x40)
  79. #define UART_ESCAPE_EN(uart) (UART_BASE(uart)+0x44)
  80. #define UART_SLEEP_EN(uart) (UART_BASE(uart)+0x48)
  81. #define UART_VFIFO_EN(uart) (UART_BASE(uart)+0x4c)
  82. #define UART_RXTRI_AD(uart) (UART_BASE(uart)+0x50)
  83. #ifndef MACH_FPGA
  84. #define __ENABLE_UART_LOG_SWITCH_FEATURE__
  85. #endif
  86. volatile unsigned int g_uart;
  87. unsigned int g_brg;
  88. extern u8 g_lk_final_log;
  89. #if defined(MACH_FPGA)
  90. #define UART_SRC_CLK CONFIG_UART_FPGA_CLK
  91. #else
  92. #define UART_SRC_CLK CONFIG_UART_EVB_CLK
  93. #endif
  94. #define CONFIG_BAUDRATE CONFIG_UART_BAUDRATE
  95. #ifdef __ENABLE_UART_LOG_SWITCH_FEATURE__
  96. extern BOOT_ARGUMENT *g_boot_arg;
  97. extern bool cmdline_append(const char *append_string);
  98. int get_uart_port_id(void)
  99. {
  100. unsigned int log_port;
  101. unsigned int log_enable;
  102. log_port = g_boot_arg->log_port;
  103. log_enable = g_boot_arg->log_enable;
  104. if (log_enable != 0) {
  105. switch (log_port) {
  106. case UART1:
  107. return 1;
  108. case UART2:
  109. return 2;
  110. case UART3:
  111. return 3;
  112. #ifdef AP_UART3_BASE
  113. case UART4:
  114. return 4;
  115. #endif
  116. default:
  117. return 2;
  118. }
  119. }
  120. return 2;
  121. }
  122. int get_meta_port_id(void)
  123. {
  124. unsigned int meta_port;
  125. meta_port = g_boot_arg->meta_uart_port;
  126. switch (meta_port) {
  127. case UART1:
  128. return 1;
  129. case UART2:
  130. return 2;
  131. case UART3:
  132. return 3;
  133. #ifdef AP_UART3_BASE
  134. case UART4:
  135. return 4;
  136. #endif
  137. default:
  138. return 1;
  139. }
  140. return 1;
  141. }
  142. static void change_uart_port(char * cmd_line, char new_val)
  143. {
  144. int i;
  145. int len;
  146. char *ptr;
  147. if (NULL == cmd_line)
  148. return;
  149. len = strlen(cmd_line);
  150. ptr = cmd_line;
  151. i = strlen("ttyS");
  152. if (len < i)
  153. return;
  154. len = len-i;
  155. for (i=0; i<=len; i++) {
  156. if (strncmp(ptr, "ttyS", 4)==0) {
  157. ptr[4] = new_val; // Find and modify
  158. break;
  159. }
  160. ptr++;
  161. }
  162. }
  163. void custom_port_in_kernel(BOOTMODE boot_mode, char *command)
  164. {
  165. if (get_uart_port_id() == 1) {
  166. change_uart_port(command, '0');
  167. } else if (get_uart_port_id() == 2) {
  168. change_uart_port(command, '1');
  169. } else if (get_uart_port_id() == 3) {
  170. change_uart_port(command, '2');
  171. } else if (get_uart_port_id() == 4) {
  172. change_uart_port(command, '3');
  173. } else {
  174. change_uart_port(command, '0');
  175. }
  176. }
  177. #else
  178. int get_uart_port_id(void)
  179. {
  180. // Dummy function case
  181. return 1;
  182. }
  183. int get_meta_port_id(void)
  184. {
  185. // Dummy function case
  186. return 1;
  187. }
  188. void custom_port_in_kernel(BOOTMODE boot_mode, char *command)
  189. {
  190. // Dummy function case
  191. }
  192. #endif
  193. void uart_setbrg()
  194. {
  195. unsigned int byte,speed;
  196. unsigned int highspeed;
  197. unsigned int quot, divisor, remainder;
  198. unsigned int uartclk;
  199. unsigned short data, high_speed_div, sample_count, sample_point;
  200. unsigned int tmp_div;
  201. speed = g_brg;
  202. uartclk = UART_SRC_CLK;
  203. if (speed <= 115200 ) {
  204. highspeed = 0;
  205. quot = 16;
  206. } else {
  207. highspeed = 3;
  208. quot = 1;
  209. }
  210. if (highspeed < 3) { /*0~2*/
  211. /* Set divisor DLL and DLH */
  212. divisor = uartclk / (quot * speed);
  213. remainder = uartclk % (quot * speed);
  214. if (remainder >= (quot / 2) * speed)
  215. divisor += 1;
  216. mt65xx_reg_sync_writew(highspeed, UART_HIGHSPEED(g_uart));
  217. byte = DRV_Reg32(UART_LCR(g_uart)); /* DLAB start */
  218. mt65xx_reg_sync_writel((byte | UART_LCR_DLAB), UART_LCR(g_uart));
  219. mt65xx_reg_sync_writel((divisor & 0x00ff), UART_DLL(g_uart));
  220. mt65xx_reg_sync_writel(((divisor >> 8)&0x00ff), UART_DLH(g_uart));
  221. mt65xx_reg_sync_writel(byte, UART_LCR(g_uart)); /* DLAB end */
  222. } else {
  223. data=(unsigned short)(uartclk/speed);
  224. high_speed_div = (data>>8) + 1; // divided by 256
  225. tmp_div=uartclk/(speed*high_speed_div);
  226. divisor = (unsigned short)tmp_div;
  227. remainder = (uartclk)%(high_speed_div*speed);
  228. /*get (sample_count+1)*/
  229. if (remainder >= ((speed)*(high_speed_div))>>1)
  230. divisor = (unsigned short)(tmp_div+1);
  231. else
  232. divisor = (unsigned short)tmp_div;
  233. sample_count=divisor-1;
  234. /*get the sample point*/
  235. sample_point=(sample_count-1)>>1;
  236. /*configure register*/
  237. mt65xx_reg_sync_writel(highspeed, UART_HIGHSPEED(g_uart));
  238. byte = DRV_Reg32(UART_LCR(g_uart)); /* DLAB start */
  239. mt65xx_reg_sync_writel((byte | UART_LCR_DLAB), UART_LCR(g_uart));
  240. mt65xx_reg_sync_writel((high_speed_div & 0x00ff), UART_DLL(g_uart));
  241. mt65xx_reg_sync_writel(((high_speed_div >> 8)&0x00ff), UART_DLH(g_uart));
  242. mt65xx_reg_sync_writel(sample_count, UART_SAMPLE_COUNT(g_uart));
  243. mt65xx_reg_sync_writel(sample_point, UART_SAMPLE_POINT(g_uart));
  244. mt65xx_reg_sync_writel(byte, UART_LCR(g_uart)); /* DLAB end */
  245. }
  246. }
  247. void mtk_set_current_uart(MTK_UART uart_base)
  248. {
  249. g_uart = uart_base;
  250. }
  251. void uart_init_early(void)
  252. {
  253. #ifdef __ENABLE_UART_LOG_SWITCH_FEATURE__
  254. if (get_uart_port_id() == 1)
  255. mtk_set_current_uart(UART1);
  256. else if (get_uart_port_id() == 2)
  257. mtk_set_current_uart(UART2);
  258. else if (get_uart_port_id() == 3)
  259. mtk_set_current_uart(UART3);
  260. #ifdef AP_UART3_BASE
  261. else if (get_uart_port_id() == 4)
  262. mtk_set_current_uart(UART4);
  263. #endif
  264. else
  265. mtk_set_current_uart(UART1);
  266. #else
  267. mtk_set_current_uart(UART1);
  268. #endif
  269. DRV_SetReg32(UART_FCR(g_uart), UART_FCR_FIFO_INIT); /* clear fifo */
  270. mt65xx_reg_sync_writew(UART_NONE_PARITY | UART_WLS_8 | UART_1_STOP, UART_LCR(g_uart));
  271. g_brg = CONFIG_BAUDRATE;
  272. uart_setbrg();
  273. #ifndef USER_BUILD
  274. char cmdline[UART_EARLY_CONSOLE_LEN];
  275. snprintf(cmdline, UART_EARLY_CONSOLE_LEN, " earlycon=uart8250,mmio32,0x%x,",
  276. g_boot_arg->log_port);
  277. cmdline_append(cmdline);
  278. #endif
  279. }
  280. void uart_init(void)
  281. {
  282. }
  283. int uart_putc(const char c )
  284. {
  285. unsigned int status;
  286. if (g_boot_arg->log_enable == 0)
  287. return 0;
  288. #ifdef USER_LOAD
  289. if (g_boot_arg->log_dynamic_switch == 0)
  290. return 0;
  291. if (g_lk_final_log == 0)
  292. return 0;
  293. #endif
  294. if (c == '\n')
  295. mt65xx_reg_sync_writel((unsigned int)'\r', UART_THR(g_uart));
  296. mt65xx_reg_sync_writel((unsigned int)c, UART_THR(g_uart));
  297. for (;;) {
  298. status = DRV_Reg32(UART_LSR(g_uart));
  299. if ((status & BOTH_EMPTY) == BOTH_EMPTY)
  300. break;
  301. }
  302. return 0;
  303. }
  304. int uart_getc(void) /* returns -1 if no data available */
  305. {
  306. if (g_boot_arg->log_enable == 0)
  307. return 0;
  308. if (g_boot_arg->meta_log_disable != 0)
  309. return 0;
  310. #ifdef USER_LOAD
  311. if (g_boot_arg->log_dynamic_switch == 0)
  312. return 0;
  313. if (g_lk_final_log == 0)
  314. return 0;
  315. #endif
  316. while (!(DRV_Reg32(UART_LSR(g_uart)) & UART_LSR_DR));
  317. return (int)DRV_Reg32(UART_RBR(g_uart));
  318. }
  319. void uart_puts(const char *s)
  320. {
  321. while (*s)
  322. uart_putc(*s++);
  323. }