uart.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. ptr[5] = new_val; // Find and modify
  139. break;
  140. }
  141. ptr++;
  142. }
  143. }
  144. void custom_port_in_kernel(BOOTMODE boot_mode, char *command)
  145. {
  146. if (get_uart_port_id() == 1) {
  147. change_uart_port(command, '0');
  148. } else if (get_uart_port_id() == 2) {
  149. change_uart_port(command, '1');
  150. } else if (get_uart_port_id() == 3) {
  151. change_uart_port(command, '2');
  152. } else
  153. change_uart_port(command, '3');
  154. }
  155. #else
  156. void custom_port_in_kernel(BOOTMODE boot_mode, char *command)
  157. {
  158. // Dummy function case
  159. }
  160. int get_uart_port_id(void)
  161. {
  162. // Dummy function case
  163. }
  164. int get_meta_port_id(void)
  165. {
  166. // Dummy function case
  167. }
  168. #endif
  169. void mtk_uart_power_on(MTK_UART uart_base)
  170. {
  171. /* UART Powr PDN and Reset*/
  172. //#define AP_PERI_GLOBALCON_RST0 (PERI_CON_BASE+0x0)
  173. #define AP_PERI_GLOBALCON_PDN0 (INFRACFG_AO_BASE+0x84)
  174. if (uart_base == UART1)
  175. UART_SET_BITS(1 << 22, AP_PERI_GLOBALCON_PDN0); /* Power on UART1 */
  176. else if (uart_base == UART2)
  177. UART_SET_BITS(1 << 23, AP_PERI_GLOBALCON_PDN0); /* Power on UART2 */
  178. return;
  179. }
  180. void uart_setbrg()
  181. {
  182. unsigned int byte,speed;
  183. unsigned int highspeed;
  184. unsigned int quot, divisor, remainder;
  185. unsigned int uartclk;
  186. unsigned short data, high_speed_div, sample_count, sample_point;
  187. unsigned int tmp_div;
  188. speed = g_brg;
  189. ////FIXME Disable for MT6582 LK Porting
  190. uartclk = UART_SRC_CLK;
  191. //uartclk = (unsigned int)(mtk_get_bus_freq()*1000/4);
  192. if (speed <= 115200 ) {
  193. highspeed = 0;
  194. quot = 16;
  195. } else {
  196. highspeed = 3;
  197. quot = 1;
  198. }
  199. if (highspeed < 3) { /*0~2*/
  200. /* Set divisor DLL and DLH */
  201. divisor = uartclk / (quot * speed);
  202. remainder = uartclk % (quot * speed);
  203. if (remainder >= (quot / 2) * speed)
  204. divisor += 1;
  205. mt65xx_reg_sync_writew(highspeed, UART_HIGHSPEED(g_uart));
  206. byte = DRV_Reg32(UART_LCR(g_uart)); /* DLAB start */
  207. mt65xx_reg_sync_writel((byte | UART_LCR_DLAB), UART_LCR(g_uart));
  208. mt65xx_reg_sync_writel((divisor & 0x00ff), UART_DLL(g_uart));
  209. mt65xx_reg_sync_writel(((divisor >> 8)&0x00ff), UART_DLH(g_uart));
  210. mt65xx_reg_sync_writel(byte, UART_LCR(g_uart)); /* DLAB end */
  211. }
  212. else {
  213. data=(unsigned short)(uartclk/speed);
  214. high_speed_div = (data>>8) + 1; // divided by 256
  215. tmp_div=uartclk/(speed*high_speed_div);
  216. divisor = (unsigned short)tmp_div;
  217. remainder = (uartclk)%(high_speed_div*speed);
  218. /*get (sample_count+1)*/
  219. if (remainder >= ((speed)*(high_speed_div))>>1)
  220. divisor = (unsigned short)(tmp_div+1);
  221. else
  222. divisor = (unsigned short)tmp_div;
  223. sample_count=divisor-1;
  224. /*get the sample point*/
  225. sample_point=(sample_count-1)>>1;
  226. /*configure register*/
  227. mt65xx_reg_sync_writel(highspeed, UART_HIGHSPEED(g_uart));
  228. byte = DRV_Reg32(UART_LCR(g_uart)); /* DLAB start */
  229. mt65xx_reg_sync_writel((byte | UART_LCR_DLAB), UART_LCR(g_uart));
  230. mt65xx_reg_sync_writel((high_speed_div & 0x00ff), UART_DLL(g_uart));
  231. mt65xx_reg_sync_writel(((high_speed_div >> 8)&0x00ff), UART_DLH(g_uart));
  232. mt65xx_reg_sync_writel(sample_count, UART_SAMPLE_COUNT(g_uart));
  233. mt65xx_reg_sync_writel(sample_point, UART_SAMPLE_POINT(g_uart));
  234. mt65xx_reg_sync_writel(byte, UART_LCR(g_uart)); /* DLAB end */
  235. }
  236. }
  237. void mtk_set_current_uart(MTK_UART uart_base)
  238. {
  239. #if 0
  240. switch(uart_base)
  241. {
  242. case UART1 :
  243. g_uart = uart_base;
  244. break;
  245. case UART4 :
  246. g_uart = uart_base;
  247. break;
  248. default:
  249. ASSERT(0);
  250. break;
  251. }
  252. #else
  253. g_uart = uart_base;
  254. #endif
  255. }
  256. void uart_init_early(void)
  257. {
  258. #ifdef __ENABLE_UART_LOG_SWITCH_FEATURE__
  259. if (get_uart_port_id() == 1) {
  260. mtk_set_current_uart(UART1);
  261. mtk_uart_power_on(UART1);
  262. } else if (get_uart_port_id() == 2) {
  263. mtk_set_current_uart(UART2);
  264. mtk_uart_power_on(UART2);
  265. }
  266. #else
  267. mtk_set_current_uart(UART1);
  268. mtk_uart_power_on(UART1);
  269. #endif
  270. DRV_SetReg32(UART_FCR(g_uart), UART_FCR_FIFO_INIT); /* clear fifo */
  271. mt65xx_reg_sync_writew(UART_NONE_PARITY | UART_WLS_8 | UART_1_STOP, UART_LCR(g_uart));
  272. g_brg = CONFIG_BAUDRATE;
  273. uart_setbrg();
  274. }
  275. void uart_init(void)
  276. {
  277. }
  278. int uart_putc(const char c )
  279. {
  280. if (g_boot_arg->log_enable == 0)
  281. return 0;
  282. #ifdef USER_LOAD
  283. if (g_boot_arg->log_dynamic_switch == 0)
  284. return 0;
  285. #endif
  286. while (!(DRV_Reg32(UART_LSR(g_uart)) & UART_LSR_THRE));
  287. if (c == '\n')
  288. mt65xx_reg_sync_writel((unsigned int)'\r', UART_THR(g_uart));
  289. mt65xx_reg_sync_writel((unsigned int)c, UART_THR(g_uart));
  290. return 0;
  291. }
  292. int uart_getc(void) /* returns -1 if no data available */
  293. {
  294. if (g_boot_arg->log_enable == 0)
  295. return 0;
  296. while (!(DRV_Reg32(UART_LSR(g_uart)) & UART_LSR_DR));
  297. return (int)DRV_Reg32(UART_RBR(g_uart));
  298. }
  299. void uart_puts(const char *s)
  300. {
  301. while (*s)
  302. uart_putc(*s++);
  303. }