uart.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 <string.h>
  33. #define CONFIG_BAUDRATE 921600
  34. #define UART_SET_BITS(BS,REG) mt65xx_reg_sync_writel(DRV_Reg32(REG) | (u32)(BS), REG)
  35. #define UART_CLR_BITS(BC,REG) mt65xx_reg_sync_writel(DRV_Reg32(REG) & ~((u32)(BC)), REG)
  36. #define UART_BASE(uart) (uart)
  37. #define UART_RBR(uart) (UART_BASE(uart)+0x0) /* Read only */
  38. #define UART_THR(uart) (UART_BASE(uart)+0x0) /* Write only */
  39. #define UART_IER(uart) (UART_BASE(uart)+0x4)
  40. #define UART_IIR(uart) (UART_BASE(uart)+0x8) /* Read only */
  41. #define UART_FCR(uart) (UART_BASE(uart)+0x8) /* Write only */
  42. #define UART_LCR(uart) (UART_BASE(uart)+0xc)
  43. #define UART_MCR(uart) (UART_BASE(uart)+0x10)
  44. #define UART_LSR(uart) (UART_BASE(uart)+0x14)
  45. #define UART_MSR(uart) (UART_BASE(uart)+0x18)
  46. #define UART_SCR(uart) (UART_BASE(uart)+0x1c)
  47. #define UART_DLL(uart) (UART_BASE(uart)+0x0) /* Only when LCR.DLAB = 1 */
  48. #define UART_DLH(uart) (UART_BASE(uart)+0x4) /* Only when LCR.DLAB = 1 */
  49. #define UART_EFR(uart) (UART_BASE(uart)+0x8) /* Only when LCR = 0xbf */
  50. #define UART_XON1(uart) (UART_BASE(uart)+0x10) /* Only when LCR = 0xbf */
  51. #define UART_XON2(uart) (UART_BASE(uart)+0x14) /* Only when LCR = 0xbf */
  52. #define UART_XOFF1(uart) (UART_BASE(uart)+0x18) /* Only when LCR = 0xbf */
  53. #define UART_XOFF2(uart) (UART_BASE(uart)+0x1c) /* Only when LCR = 0xbf */
  54. #define UART_AUTOBAUD_EN(uart) (UART_BASE(uart)+0x20)
  55. #define UART_HIGHSPEED(uart) (UART_BASE(uart)+0x24)
  56. #define UART_SAMPLE_COUNT(uart) (UART_BASE(uart)+0x28)
  57. #define UART_SAMPLE_POINT(uart) (UART_BASE(uart)+0x2c)
  58. #define UART_AUTOBAUD_REG(uart) (UART_BASE(uart)+0x30)
  59. #define UART_RATE_FIX_AD(uart) (UART_BASE(uart)+0x34)
  60. #define UART_AUTOBAUD_SAMPLE(uart) (UART_BASE(uart)+0x38)
  61. #define UART_GUARD(uart) (UART_BASE(uart)+0x3c)
  62. #define UART_ESCAPE_DAT(uart) (UART_BASE(uart)+0x40)
  63. #define UART_ESCAPE_EN(uart) (UART_BASE(uart)+0x44)
  64. #define UART_SLEEP_EN(uart) (UART_BASE(uart)+0x48)
  65. #define UART_VFIFO_EN(uart) (UART_BASE(uart)+0x4c)
  66. #define UART_RXTRI_AD(uart) (UART_BASE(uart)+0x50)
  67. //FIXME Disable for FPGA LK Porting
  68. #ifndef MACH_FPGA
  69. #define __ENABLE_UART_LOG_SWITCH_FEATURE__
  70. #endif
  71. // output uart port
  72. volatile unsigned int g_uart;
  73. // output uart baudrate
  74. unsigned int g_brg;
  75. extern u8 g_lk_final_log;
  76. //extern unsigned int mtk_get_bus_freq(void);
  77. #if defined(MACH_FPGA)
  78. #define UART_SRC_CLK 10000000
  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. case UART1:
  97. return 1;
  98. case UART2:
  99. return 2;
  100. case UART3:
  101. return 3;
  102. case UART4:
  103. return 4;
  104. default:
  105. return 2;
  106. }
  107. }
  108. return 2;
  109. }
  110. int get_meta_port_id(void)
  111. {
  112. unsigned int meta_port;
  113. meta_port = g_boot_arg->meta_uart_port;
  114. switch (meta_port) {
  115. case UART1:
  116. return 1;
  117. case UART2:
  118. return 2;
  119. case UART3:
  120. return 3;
  121. case UART4:
  122. return 4;
  123. default:
  124. return 1;
  125. }
  126. return 1;
  127. }
  128. static void change_uart_port(char * cmd_line, char new_val)
  129. {
  130. int i;
  131. int len;
  132. char *ptr;
  133. if (NULL == cmd_line)
  134. return;
  135. len = strlen(cmd_line);
  136. ptr = cmd_line;
  137. i = strlen("ttyMT");
  138. if (len < i)
  139. return;
  140. len = len-i;
  141. for (i=0; i<=len; i++) {
  142. if (strncmp(ptr, "ttyMT", 5)==0) {
  143. ptr[5] = new_val; // Find and modify
  144. break;
  145. }
  146. ptr++;
  147. }
  148. }
  149. void custom_port_in_kernel(BOOTMODE boot_mode, char *command)
  150. {
  151. if (get_uart_port_id() == 1) {
  152. change_uart_port(command, '0');
  153. } else if (get_uart_port_id() == 2) {
  154. change_uart_port(command, '1');
  155. } else if (get_uart_port_id() == 3) {
  156. change_uart_port(command, '2');
  157. } else if (get_uart_port_id() == 4) {
  158. change_uart_port(command, '3');
  159. } else {
  160. change_uart_port(command, '0');
  161. }
  162. }
  163. #else
  164. int get_uart_port_id(void)
  165. {
  166. // Dummy function case
  167. return 1;
  168. }
  169. int get_meta_port_id(void)
  170. {
  171. // Dummy function case
  172. return 1;
  173. }
  174. void custom_port_in_kernel(BOOTMODE boot_mode, char *command)
  175. {
  176. // Dummy function case
  177. }
  178. #endif
  179. void mtk_uart_power_on(MTK_UART uart_base)
  180. {
  181. /* UART Powr PDN and Reset*/
  182. #if 0 // default on
  183. #define MODULE_SW_CG_0_SET (INFRACFG_AO_BASE+0x80)
  184. if (uart_base == UART1)
  185. UART_CLR_BITS(1 << 22, MODULE_SW_CG_0_SET); /* Power on UART1 */
  186. else if (uart_base == UART2)
  187. UART_CLR_BITS(1 << 23, MODULE_SW_CG_0_SET); /* Power on UART2 */
  188. else if (uart_base == UART3)
  189. UART_CLR_BITS(1 << 24, MODULE_SW_CG_0_SET); /* Power on UART3 */
  190. else if (uart_base == UART4)
  191. UART_CLR_BITS(1 << 25, MODULE_SW_CG_0_SET); /* Power on UART4 */
  192. #endif
  193. }
  194. void uart_setbrg()
  195. {
  196. unsigned int byte,speed;
  197. unsigned int highspeed;
  198. unsigned int quot, divisor, remainder;
  199. unsigned int uartclk;
  200. unsigned short data, high_speed_div, sample_count, sample_point;
  201. unsigned int tmp_div;
  202. speed = g_brg;
  203. ////FIXME Disable for MT6582 LK Porting
  204. uartclk = UART_SRC_CLK;
  205. //uartclk = (unsigned int)(mtk_get_bus_freq()*1000/4);
  206. if (speed <= 115200 ) {
  207. highspeed = 0;
  208. quot = 16;
  209. } else {
  210. highspeed = 3;
  211. quot = 1;
  212. }
  213. if (highspeed < 3) { /*0~2*/
  214. /* Set divisor DLL and DLH */
  215. divisor = uartclk / (quot * speed);
  216. remainder = uartclk % (quot * speed);
  217. if (remainder >= (quot / 2) * speed)
  218. divisor += 1;
  219. mt65xx_reg_sync_writew(highspeed, UART_HIGHSPEED(g_uart));
  220. byte = DRV_Reg32(UART_LCR(g_uart)); /* DLAB start */
  221. mt65xx_reg_sync_writel((byte | UART_LCR_DLAB), UART_LCR(g_uart));
  222. mt65xx_reg_sync_writel((divisor & 0x00ff), UART_DLL(g_uart));
  223. mt65xx_reg_sync_writel(((divisor >> 8)&0x00ff), UART_DLH(g_uart));
  224. mt65xx_reg_sync_writel(byte, UART_LCR(g_uart)); /* DLAB end */
  225. } else {
  226. data=(unsigned short)(uartclk/speed);
  227. high_speed_div = (data>>8) + 1; // divided by 256
  228. tmp_div=uartclk/(speed*high_speed_div);
  229. divisor = (unsigned short)tmp_div;
  230. remainder = (uartclk)%(high_speed_div*speed);
  231. /*get (sample_count+1)*/
  232. if (remainder >= ((speed)*(high_speed_div))>>1)
  233. divisor = (unsigned short)(tmp_div+1);
  234. else
  235. divisor = (unsigned short)tmp_div;
  236. sample_count=divisor-1;
  237. /*get the sample point*/
  238. sample_point=(sample_count-1)>>1;
  239. /*configure register*/
  240. mt65xx_reg_sync_writel(highspeed, UART_HIGHSPEED(g_uart));
  241. byte = DRV_Reg32(UART_LCR(g_uart)); /* DLAB start */
  242. mt65xx_reg_sync_writel((byte | UART_LCR_DLAB), UART_LCR(g_uart));
  243. mt65xx_reg_sync_writel((high_speed_div & 0x00ff), UART_DLL(g_uart));
  244. mt65xx_reg_sync_writel(((high_speed_div >> 8)&0x00ff), UART_DLH(g_uart));
  245. mt65xx_reg_sync_writel(sample_count, UART_SAMPLE_COUNT(g_uart));
  246. mt65xx_reg_sync_writel(sample_point, UART_SAMPLE_POINT(g_uart));
  247. mt65xx_reg_sync_writel(byte, UART_LCR(g_uart)); /* DLAB end */
  248. }
  249. }
  250. void mtk_set_current_uart(MTK_UART uart_base)
  251. {
  252. #if 0
  253. switch (uart_base) {
  254. case UART1 :
  255. g_uart = uart_base;
  256. break;
  257. case UART4 :
  258. g_uart = uart_base;
  259. break;
  260. default:
  261. ASSERT(0);
  262. break;
  263. }
  264. #else
  265. g_uart = uart_base;
  266. #endif
  267. }
  268. void uart_init_early(void)
  269. {
  270. #ifdef __ENABLE_UART_LOG_SWITCH_FEATURE__
  271. if (get_uart_port_id() == 1) {
  272. mtk_set_current_uart(UART1);
  273. mtk_uart_power_on(UART1);
  274. } else if (get_uart_port_id() == 2) {
  275. mtk_set_current_uart(UART2);
  276. mtk_uart_power_on(UART2);
  277. } else if (get_uart_port_id() == 3) {
  278. mtk_set_current_uart(UART3);
  279. mtk_uart_power_on(UART3);
  280. } else if (get_uart_port_id() == 4) {
  281. mtk_set_current_uart(UART4);
  282. mtk_uart_power_on(UART4);
  283. } else {
  284. mtk_set_current_uart(UART1);
  285. mtk_uart_power_on(UART1);
  286. }
  287. #else
  288. mtk_set_current_uart(UART1);
  289. mtk_uart_power_on(UART1);
  290. #endif
  291. DRV_SetReg32(UART_FCR(g_uart), UART_FCR_FIFO_INIT); /* clear fifo */
  292. mt65xx_reg_sync_writew(UART_NONE_PARITY | UART_WLS_8 | UART_1_STOP, UART_LCR(g_uart));
  293. g_brg = CONFIG_BAUDRATE;
  294. uart_setbrg();
  295. }
  296. void uart_init(void)
  297. {
  298. }
  299. int uart_putc(const char c )
  300. {
  301. if (g_boot_arg->log_enable == 0)
  302. return 0;
  303. if (g_boot_arg->meta_log_disable != 0)
  304. return 0;
  305. #ifdef USER_LOAD
  306. if (g_boot_arg->log_dynamic_switch == 0)
  307. return 0;
  308. if (g_lk_final_log == 0)
  309. return 0;
  310. #endif
  311. while (!(DRV_Reg32(UART_LSR(g_uart)) & UART_LSR_THRE));
  312. if (c == '\n')
  313. mt65xx_reg_sync_writel((unsigned int)'\r', UART_THR(g_uart));
  314. mt65xx_reg_sync_writel((unsigned int)c, UART_THR(g_uart));
  315. return 0;
  316. }
  317. int uart_getc(void) /* returns -1 if no data available */
  318. {
  319. if (g_boot_arg->log_enable == 0)
  320. return 0;
  321. #ifdef USER_LOAD
  322. if (g_boot_arg->log_dynamic_switch == 0)
  323. return 0;
  324. if (g_lk_final_log == 0)
  325. return 0;
  326. #endif
  327. while (!(DRV_Reg32(UART_LSR(g_uart)) & UART_LSR_DR));
  328. return (int)DRV_Reg32(UART_RBR(g_uart));
  329. }
  330. void uart_puts(const char *s)
  331. {
  332. while (*s)
  333. uart_putc(*s++);
  334. }