drv_uart.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*
  2. * Copyright (c) 2006-2024, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023/06/25 flyingcys first version
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include "board.h"
  14. #include "drv_uart.h"
  15. #include "drv_pinmux.h"
  16. #define DBG_TAG "DRV.UART"
  17. #define DBG_LVL DBG_WARNING
  18. #include <rtdbg.h>
  19. /*
  20. * Divide positive or negative dividend by positive divisor and round
  21. * to closest integer. Result is undefined for negative divisors and
  22. * for negative dividends if the divisor variable type is unsigned.
  23. */
  24. #define DIV_ROUND_CLOSEST(x, divisor)( \
  25. { \
  26. typeof(x) __x = x; \
  27. typeof(divisor) __d = divisor; \
  28. (((typeof(x))-1) > 0 || \
  29. ((typeof(divisor))-1) > 0 || (__x) > 0) ? \
  30. (((__x) + ((__d) / 2)) / (__d)) : \
  31. (((__x) - ((__d) / 2)) / (__d)); \
  32. } \
  33. )
  34. #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
  35. struct hw_uart_device
  36. {
  37. rt_ubase_t hw_base;
  38. rt_uint32_t irqno;
  39. };
  40. #define BSP_DEFINE_UART_DEVICE(no) \
  41. static struct hw_uart_device _uart##no##_device = \
  42. { \
  43. UART##no##_BASE, \
  44. UART##no##_IRQ \
  45. }; \
  46. static struct rt_serial_device _serial##no;
  47. #ifdef BSP_USING_UART0
  48. BSP_DEFINE_UART_DEVICE(0);
  49. #endif
  50. #ifdef BSP_USING_UART1
  51. BSP_DEFINE_UART_DEVICE(1);
  52. #endif
  53. #ifdef BSP_USING_UART2
  54. BSP_DEFINE_UART_DEVICE(2);
  55. #endif
  56. #ifdef BSP_USING_UART3
  57. BSP_DEFINE_UART_DEVICE(3);
  58. #endif
  59. rt_inline rt_uint32_t dw8250_read32(rt_ubase_t addr, rt_ubase_t offset)
  60. {
  61. return *((volatile rt_uint32_t *)(addr + (offset << UART_REG_SHIFT)));
  62. }
  63. rt_inline void dw8250_write32(rt_ubase_t addr, rt_ubase_t offset, rt_uint32_t value)
  64. {
  65. *((volatile rt_uint32_t *)(addr + (offset << UART_REG_SHIFT))) = value;
  66. if (offset == UART_LCR)
  67. {
  68. int tries = 1000;
  69. /* Make sure LCR write wasn't ignored */
  70. while (tries--)
  71. {
  72. unsigned int lcr = dw8250_read32(addr, UART_LCR);
  73. if ((value & ~UART_LCR_STKP) == (lcr & ~UART_LCR_STKP))
  74. {
  75. return;
  76. }
  77. dw8250_write32(addr, UART_FCR, UART_FCR_DEFVAL);
  78. dw8250_read32(addr, UART_RX);
  79. *((volatile rt_uint32_t *)(addr + (offset << UART_REG_SHIFT))) = value;
  80. }
  81. }
  82. }
  83. static void dw8250_uart_setbrg(rt_ubase_t addr, int baud_divisor)
  84. {
  85. /* to keep serial format, read lcr before writing BKSE */
  86. int lcr_val = dw8250_read32(addr, UART_LCR) & ~UART_LCR_BKSE;
  87. dw8250_write32(addr, UART_LCR, UART_LCR_BKSE | lcr_val);
  88. dw8250_write32(addr, UART_DLL, baud_divisor & 0xff);
  89. dw8250_write32(addr, UART_DLM, (baud_divisor >> 8) & 0xff);
  90. dw8250_write32(addr, UART_LCR, lcr_val);
  91. }
  92. static rt_err_t dw8250_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  93. {
  94. rt_base_t base;
  95. struct hw_uart_device *uart;
  96. int clock_divisor;
  97. RT_ASSERT(serial != RT_NULL);
  98. uart = (struct hw_uart_device *)serial->parent.user_data;
  99. base = uart->hw_base;
  100. while (!(dw8250_read32(base, UART_LSR) & UART_LSR_TEMT));
  101. dw8250_write32(base, UART_IER, 0);
  102. dw8250_write32(base, UART_MCR, UART_MCRVAL);
  103. dw8250_write32(base, UART_FCR, UART_FCR_DEFVAL);
  104. /* initialize serial config to 8N1 before writing baudrate */
  105. dw8250_write32(base, UART_LCR, UART_LCR_8N1);
  106. clock_divisor = DIV_ROUND_CLOSEST(UART_INPUT_CLK, 16 * serial->config.baud_rate);
  107. dw8250_uart_setbrg(base, clock_divisor);
  108. return RT_EOK;
  109. }
  110. static rt_err_t dw8250_uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  111. {
  112. struct hw_uart_device *uart;
  113. RT_ASSERT(serial != RT_NULL);
  114. uart = (struct hw_uart_device *)serial->parent.user_data;
  115. switch (cmd)
  116. {
  117. case RT_DEVICE_CTRL_CLR_INT:
  118. /* Disable rx irq */
  119. dw8250_write32(uart->hw_base, UART_IER, !UART_IER_RDI);
  120. rt_hw_interrupt_mask(uart->irqno);
  121. break;
  122. case RT_DEVICE_CTRL_SET_INT:
  123. /* Enable rx irq */
  124. dw8250_write32(uart->hw_base, UART_IER, UART_IER_RDI);
  125. rt_hw_interrupt_umask(uart->irqno);
  126. break;
  127. }
  128. return RT_EOK;
  129. }
  130. static int dw8250_uart_putc(struct rt_serial_device *serial, char c)
  131. {
  132. rt_base_t base;
  133. struct hw_uart_device *uart;
  134. RT_ASSERT(serial != RT_NULL);
  135. uart = (struct hw_uart_device *)serial->parent.user_data;
  136. base = uart->hw_base;
  137. while ((dw8250_read32(base, UART_LSR) & BOTH_EMPTY) != BOTH_EMPTY);
  138. dw8250_write32(base, UART_TX, c);
  139. return 1;
  140. }
  141. static int dw8250_uart_getc(struct rt_serial_device *serial)
  142. {
  143. int ch = -1;
  144. rt_base_t base;
  145. struct hw_uart_device *uart;
  146. RT_ASSERT(serial != RT_NULL);
  147. uart = (struct hw_uart_device *)serial->parent.user_data;
  148. base = uart->hw_base;
  149. if (dw8250_read32(base, UART_LSR) & UART_LSR_DR)
  150. {
  151. ch = dw8250_read32(base, UART_RX) & 0xff;
  152. }
  153. return ch;
  154. }
  155. static const struct rt_uart_ops _uart_ops =
  156. {
  157. dw8250_uart_configure,
  158. dw8250_uart_control,
  159. dw8250_uart_putc,
  160. dw8250_uart_getc,
  161. };
  162. static void rt_hw_uart_isr(int irqno, void *param)
  163. {
  164. unsigned int iir, status;
  165. struct rt_serial_device *serial = (struct rt_serial_device *)param;
  166. struct hw_uart_device *uart = (struct hw_uart_device *)serial->parent.user_data;
  167. iir = dw8250_read32(uart->hw_base, UART_IIR);
  168. /* If don't do this in non-DMA mode then the "RX TIMEOUT" interrupt will fire forever. */
  169. if ((iir & 0x3f) == UART_IIR_RX_TIMEOUT)
  170. {
  171. status = dw8250_read32(uart->hw_base, UART_LSR);
  172. if (!(status & (UART_LSR_DR | UART_LSR_BI)))
  173. {
  174. dw8250_read32(uart->hw_base, UART_RX);
  175. }
  176. }
  177. if (!(iir & UART_IIR_NO_INT))
  178. {
  179. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  180. }
  181. if ((iir & UART_IIR_BUSY) == UART_IIR_BUSY)
  182. {
  183. /* Clear the USR */
  184. dw8250_read32(uart->hw_base, UART_USR);
  185. return;
  186. }
  187. }
  188. #if defined(BOARD_TYPE_MILKV_DUO) || defined(BOARD_TYPE_MILKV_DUO_SPINOR)
  189. #ifdef BSP_USING_UART0
  190. static const char *pinname_whitelist_uart0_rx[] = {
  191. "UART0_RX",
  192. NULL,
  193. };
  194. static const char *pinname_whitelist_uart0_tx[] = {
  195. "UART0_TX",
  196. NULL,
  197. };
  198. #endif
  199. #ifdef BSP_USING_UART1
  200. static const char *pinname_whitelist_uart1_rx[] = {
  201. "IIC0_SDA",
  202. "UART0_RX",
  203. NULL,
  204. };
  205. static const char *pinname_whitelist_uart1_tx[] = {
  206. "IIC0_SCL",
  207. "UART0_TX",
  208. NULL,
  209. };
  210. #endif
  211. #ifdef BSP_USING_UART2
  212. static const char *pinname_whitelist_uart2_rx[] = {
  213. "IIC0_SDA",
  214. "SD1_D1",
  215. NULL,
  216. };
  217. static const char *pinname_whitelist_uart2_tx[] = {
  218. "IIC0_SCL",
  219. "SD1_D2",
  220. NULL,
  221. };
  222. #endif
  223. #ifdef BSP_USING_UART3
  224. static const char *pinname_whitelist_uart3_rx[] = {
  225. "SD1_D1",
  226. NULL,
  227. };
  228. static const char *pinname_whitelist_uart3_tx[] = {
  229. "SD1_D2",
  230. NULL,
  231. };
  232. #endif
  233. #ifdef BSP_USING_UART4
  234. static const char *pinname_whitelist_uart4_rx[] = {
  235. "SD1_GPIO0",
  236. NULL,
  237. };
  238. static const char *pinname_whitelist_uart4_tx[] = {
  239. "SD1_GPIO1",
  240. NULL,
  241. };
  242. #endif
  243. #elif defined(BOARD_TYPE_MILKV_DUO256M) || defined(BOARD_TYPE_MILKV_DUO256M_SPINOR)
  244. #ifdef BSP_USING_UART0
  245. static const char *pinname_whitelist_uart0_rx[] = {
  246. "UART0_RX",
  247. NULL,
  248. };
  249. static const char *pinname_whitelist_uart0_tx[] = {
  250. "UART0_TX",
  251. NULL,
  252. };
  253. #endif
  254. #ifdef BSP_USING_UART1
  255. static const char *pinname_whitelist_uart1_rx[] = {
  256. "IIC0_SDA",
  257. "JTAG_CPU_TCK",
  258. "UART0_RX",
  259. NULL,
  260. };
  261. static const char *pinname_whitelist_uart1_tx[] = {
  262. "IIC0_SCL",
  263. "JTAG_CPU_TMS",
  264. "UART0_TX",
  265. NULL,
  266. };
  267. #endif
  268. #ifdef BSP_USING_UART2
  269. static const char *pinname_whitelist_uart2_rx[] = {
  270. "IIC0_SDA",
  271. "SD1_D1",
  272. NULL,
  273. };
  274. static const char *pinname_whitelist_uart2_tx[] = {
  275. "IIC0_SCL",
  276. "SD1_D2",
  277. NULL,
  278. };
  279. #endif
  280. #ifdef BSP_USING_UART3
  281. static const char *pinname_whitelist_uart3_rx[] = {
  282. "SD1_D1",
  283. NULL,
  284. };
  285. static const char *pinname_whitelist_uart3_tx[] = {
  286. "SD1_D2",
  287. NULL,
  288. };
  289. #endif
  290. #ifdef BSP_USING_UART4
  291. static const char *pinname_whitelist_uart4_rx[] = {
  292. NULL,
  293. };
  294. static const char *pinname_whitelist_uart4_tx[] = {
  295. NULL,
  296. };
  297. #endif
  298. #else
  299. #error "Unsupported board type!"
  300. #endif
  301. int rt_hw_uart_init(void)
  302. {
  303. struct hw_uart_device* uart;
  304. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  305. config.baud_rate = 115200;
  306. #define BSP_INSTALL_UART_DEVICE(no) \
  307. uart = &_uart##no##_device; \
  308. _serial##no.ops = &_uart_ops; \
  309. _serial##no.config = config; \
  310. rt_hw_serial_register(&_serial##no, "uart" #no, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, uart); \
  311. rt_hw_interrupt_install(uart->irqno, rt_hw_uart_isr, &_serial##no, "uart" #no);
  312. #ifdef BSP_USING_UART0
  313. pinmux_config(BSP_UART0_RX_PINNAME, UART0_RX, pinname_whitelist_uart0_rx);
  314. pinmux_config(BSP_UART0_TX_PINNAME, UART0_TX, pinname_whitelist_uart0_tx);
  315. BSP_INSTALL_UART_DEVICE(0);
  316. #if defined(ARCH_ARM)
  317. uart->hw_base = (rt_size_t)rt_ioremap((void*)uart->hw_base, 0x10000);
  318. #endif /* defined(ARCH_ARM) */
  319. #endif
  320. #ifdef BSP_USING_UART1
  321. pinmux_config(BSP_UART1_RX_PINNAME, UART1_RX, pinname_whitelist_uart1_rx);
  322. pinmux_config(BSP_UART1_TX_PINNAME, UART1_TX, pinname_whitelist_uart1_tx);
  323. BSP_INSTALL_UART_DEVICE(1);
  324. #if defined(ARCH_ARM)
  325. uart->hw_base = (rt_size_t)rt_ioremap((void*)uart->hw_base, 0x10000);
  326. #endif /* defined(ARCH_ARM) */
  327. #endif
  328. #ifdef BSP_USING_UART2
  329. pinmux_config(BSP_UART2_RX_PINNAME, UART2_RX, pinname_whitelist_uart2_rx);
  330. pinmux_config(BSP_UART2_TX_PINNAME, UART2_TX, pinname_whitelist_uart2_tx);
  331. BSP_INSTALL_UART_DEVICE(2);
  332. #if defined(ARCH_ARM)
  333. uart->hw_base = (rt_size_t)rt_ioremap((void*)uart->hw_base, 0x10000);
  334. #endif /* defined(ARCH_ARM) */
  335. #endif
  336. #ifdef BSP_USING_UART3
  337. pinmux_config(BSP_UART3_RX_PINNAME, UART3_RX, pinname_whitelist_uart3_rx);
  338. pinmux_config(BSP_UART3_TX_PINNAME, UART3_TX, pinname_whitelist_uart3_tx);
  339. BSP_INSTALL_UART_DEVICE(3);
  340. #if defined(ARCH_ARM)
  341. uart->hw_base = (rt_size_t)rt_ioremap((void*)uart->hw_base, 0x10000);
  342. #endif /* defined(ARCH_ARM) */
  343. #endif
  344. #ifdef BSP_USING_UART4
  345. pinmux_config(BSP_UART4_RX_PINNAME, UART4_RX, pinname_whitelist_uart4_rx);
  346. pinmux_config(BSP_UART4_TX_PINNAME, UART4_TX, pinname_whitelist_uart4_tx);
  347. BSP_INSTALL_UART_DEVICE(4);
  348. #if defined(ARCH_ARM)
  349. uart->hw_base = (rt_size_t)rt_ioremap((void*)uart->hw_base, 0x10000);
  350. #endif /* defined(ARCH_ARM) */
  351. #endif
  352. return 0;
  353. }