uart.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * Copyright (c) 2008 Travis Geiselbrecht
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (the "Software"), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <debug.h>
  24. #include <reg.h>
  25. #include <dev/uart.h>
  26. #include <platform/mt_typedefs.h>
  27. #include <platform/mt_reg_base.h>
  28. #include <platform/mt_uart.h>
  29. #include <platform/boot_mode.h>
  30. #include <platform/mt_gpio.h>
  31. #include <platform/sync_write.h>
  32. #include <cust_gpio_usage.h>
  33. #include <string.h>
  34. #define CONFIG_BAUDRATE 921600
  35. #define UART_SET_BITS(BS,REG) mt65xx_reg_sync_writel(DRV_Reg32(REG) | (u32)(BS), REG)
  36. #define UART_CLR_BITS(BS,REG) mt65xx_reg_sync_writel(DRV_Reg32(REG) & ~((u32)(BS)), REG)
  37. #define UART_BASE(uart) (uart)
  38. #define UART_RBR(uart) (UART_BASE(uart)+0x0) /* Read only */
  39. #define UART_THR(uart) (UART_BASE(uart)+0x0) /* Write only */
  40. #define UART_IER(uart) (UART_BASE(uart)+0x4)
  41. #define UART_IIR(uart) (UART_BASE(uart)+0x8) /* Read only */
  42. #define UART_FCR(uart) (UART_BASE(uart)+0x8) /* Write only */
  43. #define UART_LCR(uart) (UART_BASE(uart)+0xc)
  44. #define UART_MCR(uart) (UART_BASE(uart)+0x10)
  45. #define UART_LSR(uart) (UART_BASE(uart)+0x14)
  46. #define UART_MSR(uart) (UART_BASE(uart)+0x18)
  47. #define UART_SCR(uart) (UART_BASE(uart)+0x1c)
  48. #define UART_DLL(uart) (UART_BASE(uart)+0x0) /* Only when LCR.DLAB = 1 */
  49. #define UART_DLH(uart) (UART_BASE(uart)+0x4) /* Only when LCR.DLAB = 1 */
  50. #define UART_EFR(uart) (UART_BASE(uart)+0x8) /* Only when LCR = 0xbf */
  51. #define UART_XON1(uart) (UART_BASE(uart)+0x10) /* Only when LCR = 0xbf */
  52. #define UART_XON2(uart) (UART_BASE(uart)+0x14) /* Only when LCR = 0xbf */
  53. #define UART_XOFF1(uart) (UART_BASE(uart)+0x18) /* Only when LCR = 0xbf */
  54. #define UART_XOFF2(uart) (UART_BASE(uart)+0x1c) /* Only when LCR = 0xbf */
  55. #define UART_AUTOBAUD_EN(uart) (UART_BASE(uart)+0x20)
  56. #define UART_HIGHSPEED(uart) (UART_BASE(uart)+0x24)
  57. #define UART_SAMPLE_COUNT(uart) (UART_BASE(uart)+0x28)
  58. #define UART_SAMPLE_POINT(uart) (UART_BASE(uart)+0x2c)
  59. #define UART_AUTOBAUD_REG(uart) (UART_BASE(uart)+0x30)
  60. #define UART_RATE_FIX_AD(uart) (UART_BASE(uart)+0x34)
  61. #define UART_AUTOBAUD_SAMPLE(uart) (UART_BASE(uart)+0x38)
  62. #define UART_GUARD(uart) (UART_BASE(uart)+0x3c)
  63. #define UART_ESCAPE_DAT(uart) (UART_BASE(uart)+0x40)
  64. #define UART_ESCAPE_EN(uart) (UART_BASE(uart)+0x44)
  65. #define UART_SLEEP_EN(uart) (UART_BASE(uart)+0x48)
  66. #define UART_VFIFO_EN(uart) (UART_BASE(uart)+0x4c)
  67. #define UART_RXTRI_AD(uart) (UART_BASE(uart)+0x50)
  68. //FIXME Disable for FPGA LK Porting
  69. #ifndef MACH_FPGA
  70. #define __ENABLE_UART_LOG_SWITCH_FEATURE__
  71. #endif
  72. // output uart port
  73. volatile unsigned int g_uart;
  74. // output uart baudrate
  75. unsigned int g_brg;
  76. //extern unsigned int mtk_get_bus_freq(void);
  77. #if defined(MACH_FPGA)
  78. #define UART_SRC_CLK 12000000
  79. #else
  80. #define UART_SRC_CLK 26000000
  81. #endif
  82. #ifdef __ENABLE_UART_LOG_SWITCH_FEATURE__
  83. extern BOOT_ARGUMENT *g_boot_arg;
  84. int get_uart_port_id(void)
  85. {
  86. //unsigned int mode = 0;
  87. unsigned int log_port;
  88. unsigned int log_enable;
  89. //unsigned int log_baudrate;
  90. //mode = g_boot_arg->boot_mode &= 0x000000FF;
  91. log_port = g_boot_arg->log_port;
  92. log_enable = g_boot_arg->log_enable;
  93. //log_baudrate = g_boot_arg->log_baudrate;
  94. if(log_enable != 0) {
  95. switch(log_port)
  96. {
  97. case UART1:
  98. return 1;
  99. case UART2:
  100. return 2;
  101. default:
  102. return 2;
  103. }
  104. }
  105. return 2;
  106. }
  107. int get_meta_port_id(void)
  108. {
  109. unsigned int meta_port;
  110. meta_port = g_boot_arg->meta_uart_port;
  111. switch(meta_port)
  112. {
  113. case UART1:
  114. return 1;
  115. case UART2:
  116. return 2;
  117. default:
  118. return 1;
  119. }
  120. return 1;
  121. }
  122. static void change_uart_port(char * cmd_line, char new_val)
  123. {
  124. int i;
  125. int len;
  126. char *ptr;
  127. if(NULL == cmd_line)
  128. return;
  129. len = strlen(cmd_line);
  130. ptr = cmd_line;
  131. i = strlen("ttyMT");
  132. if(len < i)
  133. return;
  134. len = len-i;
  135. for(i=0; i<=len; i++)
  136. {
  137. if(strncmp(ptr, "ttyMT", 5)==0)
  138. {
  139. ptr[5] = new_val; // Find and modify
  140. break;
  141. }
  142. ptr++;
  143. }
  144. }
  145. void custom_port_in_kernel(BOOTMODE boot_mode, char *command)
  146. {
  147. if(get_uart_port_id() == 1){
  148. change_uart_port(command, '0');
  149. }else if(get_uart_port_id() == 2){
  150. change_uart_port(command, '1');
  151. }else if(get_uart_port_id() == 3){
  152. change_uart_port(command, '2');
  153. }else{
  154. change_uart_port(command, '3');
  155. }
  156. }
  157. #else
  158. void custom_port_in_kernel(BOOTMODE boot_mode, char *command)
  159. {
  160. // Dummy function case
  161. }
  162. int get_uart_port_id(void)
  163. {
  164. // Dummy function case
  165. }
  166. int get_meta_port_id(void)
  167. {
  168. // Dummy function case
  169. }
  170. #endif
  171. void mtk_uart_power_on(MTK_UART uart_base)
  172. {
  173. /* UART Powr PDN and Reset*/
  174. //#define AP_PERI_GLOBALCON_RST0 (PERI_CON_BASE+0x0)
  175. #define AP_PERI_GLOBALCON_PDN0 (TOP_CLOCK_CTRL_BASE+0x084)
  176. if (uart_base == UART1)
  177. UART_SET_BITS(1 << 10, AP_PERI_GLOBALCON_PDN0); /* Power on UART1 */
  178. else if (uart_base == UART2)
  179. UART_SET_BITS(1 << 11, AP_PERI_GLOBALCON_PDN0); /* Power on UART2 */
  180. return;
  181. }
  182. void uart_setbrg()
  183. {
  184. unsigned int byte,speed;
  185. unsigned int highspeed;
  186. unsigned int quot, divisor, remainder;
  187. unsigned int uartclk;
  188. unsigned short data, high_speed_div, sample_count, sample_point;
  189. unsigned int tmp_div;
  190. speed = g_brg;
  191. ////FIXME Disable for MT6582 LK Porting
  192. uartclk = UART_SRC_CLK;
  193. //uartclk = (unsigned int)(mtk_get_bus_freq()*1000/4);
  194. if (speed <= 115200 ) {
  195. highspeed = 0;
  196. quot = 16;
  197. } else {
  198. highspeed = 3;
  199. quot = 1;
  200. }
  201. if (highspeed < 3) { /*0~2*/
  202. /* Set divisor DLL and DLH */
  203. divisor = uartclk / (quot * speed);
  204. remainder = uartclk % (quot * speed);
  205. if (remainder >= (quot / 2) * speed)
  206. divisor += 1;
  207. mt65xx_reg_sync_writew(highspeed, UART_HIGHSPEED(g_uart));
  208. byte = DRV_Reg32(UART_LCR(g_uart)); /* DLAB start */
  209. mt65xx_reg_sync_writel((byte | UART_LCR_DLAB), UART_LCR(g_uart));
  210. mt65xx_reg_sync_writel((divisor & 0x00ff), UART_DLL(g_uart));
  211. mt65xx_reg_sync_writel(((divisor >> 8)&0x00ff), UART_DLH(g_uart));
  212. mt65xx_reg_sync_writel(byte, UART_LCR(g_uart)); /* DLAB end */
  213. }
  214. else {
  215. data=(unsigned short)(uartclk/speed);
  216. high_speed_div = (data>>8) + 1; // divided by 256
  217. tmp_div=uartclk/(speed*high_speed_div);
  218. divisor = (unsigned short)tmp_div;
  219. remainder = (uartclk)%(high_speed_div*speed);
  220. /*get (sample_count+1)*/
  221. if (remainder >= ((speed)*(high_speed_div))>>1)
  222. divisor = (unsigned short)(tmp_div+1);
  223. else
  224. divisor = (unsigned short)tmp_div;
  225. sample_count=divisor-1;
  226. /*get the sample point*/
  227. sample_point=(sample_count-1)>>1;
  228. /*configure register*/
  229. mt65xx_reg_sync_writel(highspeed, UART_HIGHSPEED(g_uart));
  230. byte = DRV_Reg32(UART_LCR(g_uart)); /* DLAB start */
  231. mt65xx_reg_sync_writel((byte | UART_LCR_DLAB), UART_LCR(g_uart));
  232. mt65xx_reg_sync_writel((high_speed_div & 0x00ff), UART_DLL(g_uart));
  233. mt65xx_reg_sync_writel(((high_speed_div >> 8)&0x00ff), UART_DLH(g_uart));
  234. mt65xx_reg_sync_writel(sample_count, UART_SAMPLE_COUNT(g_uart));
  235. mt65xx_reg_sync_writel(sample_point, UART_SAMPLE_POINT(g_uart));
  236. mt65xx_reg_sync_writel(byte, UART_LCR(g_uart)); /* DLAB end */
  237. }
  238. }
  239. void mtk_set_current_uart(MTK_UART uart_base)
  240. {
  241. #if 0
  242. switch(uart_base)
  243. {
  244. case UART1 :
  245. g_uart = uart_base;
  246. break;
  247. case UART4 :
  248. g_uart = uart_base;
  249. break;
  250. default:
  251. ASSERT(0);
  252. break;
  253. }
  254. #else
  255. g_uart = uart_base;
  256. #endif
  257. }
  258. void uart_init_early(void)
  259. {
  260. #ifdef __ENABLE_UART_LOG_SWITCH_FEATURE__
  261. if(get_uart_port_id() == 1){
  262. mtk_set_current_uart(UART1);
  263. mtk_uart_power_on(UART1);
  264. }else if(get_uart_port_id() == 2){
  265. mtk_set_current_uart(UART2);
  266. mtk_uart_power_on(UART2);
  267. }
  268. #else
  269. mtk_set_current_uart(UART1);
  270. mtk_uart_power_on(UART1);
  271. #endif
  272. DRV_SetReg32(UART_FCR(g_uart), UART_FCR_FIFO_INIT); /* clear fifo */
  273. mt65xx_reg_sync_writew(UART_NONE_PARITY | UART_WLS_8 | UART_1_STOP, UART_LCR(g_uart));
  274. g_brg = CONFIG_BAUDRATE;
  275. uart_setbrg();
  276. }
  277. void uart_init(void)
  278. {
  279. }
  280. int uart_putc(const char c )
  281. {
  282. if ( g_boot_arg->log_enable == 0)
  283. return 0;
  284. while (!(DRV_Reg32(UART_LSR(g_uart)) & UART_LSR_THRE));
  285. if (c == '\n')
  286. mt65xx_reg_sync_writel((unsigned int)'\r', UART_THR(g_uart));
  287. mt65xx_reg_sync_writel((unsigned int)c, UART_THR(g_uart));
  288. return 0;
  289. }
  290. int uart_getc(void) /* returns -1 if no data available */
  291. {
  292. if ( g_boot_arg->log_enable == 0)
  293. return 0;
  294. while (!(DRV_Reg32(UART_LSR(g_uart)) & UART_LSR_DR));
  295. return (int)DRV_Reg32(UART_RBR(g_uart));
  296. }
  297. void uart_puts(const char *s)
  298. {
  299. while (*s)
  300. uart_putc(*s++);
  301. }