drv_uart.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. int last_ier_state;
  98. RT_ASSERT(serial != RT_NULL);
  99. uart = (struct hw_uart_device *)serial->parent.user_data;
  100. base = uart->hw_base;
  101. while (!(dw8250_read32(base, UART_LSR) & UART_LSR_TEMT));
  102. last_ier_state = dw8250_read32(base, UART_IER);
  103. dw8250_write32(base, UART_IER, 0);
  104. dw8250_write32(base, UART_MCR, UART_MCRVAL);
  105. dw8250_write32(base, UART_FCR, UART_FCR_DEFVAL);
  106. /* initialize serial config to 8N1 before writing baudrate */
  107. dw8250_write32(base, UART_LCR, UART_LCR_8N1);
  108. clock_divisor = DIV_ROUND_CLOSEST(UART_INPUT_CLK, 16 * serial->config.baud_rate);
  109. dw8250_uart_setbrg(base, clock_divisor);
  110. dw8250_write32(base, UART_IER, last_ier_state);
  111. return RT_EOK;
  112. }
  113. static rt_err_t dw8250_uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  114. {
  115. struct hw_uart_device *uart;
  116. RT_ASSERT(serial != RT_NULL);
  117. uart = (struct hw_uart_device *)serial->parent.user_data;
  118. switch (cmd)
  119. {
  120. case RT_DEVICE_CTRL_CLR_INT:
  121. /* Disable rx irq */
  122. dw8250_write32(uart->hw_base, UART_IER, !UART_IER_RDI);
  123. rt_hw_interrupt_mask(uart->irqno);
  124. break;
  125. case RT_DEVICE_CTRL_SET_INT:
  126. /* Enable rx irq */
  127. dw8250_write32(uart->hw_base, UART_IER, UART_IER_RDI);
  128. rt_hw_interrupt_umask(uart->irqno);
  129. break;
  130. }
  131. return RT_EOK;
  132. }
  133. static int dw8250_uart_putc(struct rt_serial_device *serial, char c)
  134. {
  135. rt_base_t base;
  136. struct hw_uart_device *uart;
  137. RT_ASSERT(serial != RT_NULL);
  138. uart = (struct hw_uart_device *)serial->parent.user_data;
  139. base = uart->hw_base;
  140. while ((dw8250_read32(base, UART_LSR) & BOTH_EMPTY) != BOTH_EMPTY);
  141. dw8250_write32(base, UART_TX, c);
  142. return 1;
  143. }
  144. static int dw8250_uart_getc(struct rt_serial_device *serial)
  145. {
  146. int ch = -1;
  147. rt_base_t base;
  148. struct hw_uart_device *uart;
  149. RT_ASSERT(serial != RT_NULL);
  150. uart = (struct hw_uart_device *)serial->parent.user_data;
  151. base = uart->hw_base;
  152. if (dw8250_read32(base, UART_LSR) & UART_LSR_DR)
  153. {
  154. ch = dw8250_read32(base, UART_RX) & 0xff;
  155. }
  156. return ch;
  157. }
  158. static const struct rt_uart_ops _uart_ops =
  159. {
  160. dw8250_uart_configure,
  161. dw8250_uart_control,
  162. dw8250_uart_putc,
  163. dw8250_uart_getc,
  164. };
  165. static void rt_hw_uart_isr(int irqno, void *param)
  166. {
  167. unsigned int iir, status;
  168. struct rt_serial_device *serial = (struct rt_serial_device *)param;
  169. struct hw_uart_device *uart = (struct hw_uart_device *)serial->parent.user_data;
  170. iir = dw8250_read32(uart->hw_base, UART_IIR);
  171. /* If don't do this in non-DMA mode then the "RX TIMEOUT" interrupt will fire forever. */
  172. if ((iir & 0x3f) == UART_IIR_RX_TIMEOUT)
  173. {
  174. status = dw8250_read32(uart->hw_base, UART_LSR);
  175. if (!(status & (UART_LSR_DR | UART_LSR_BI)))
  176. {
  177. dw8250_read32(uart->hw_base, UART_RX);
  178. }
  179. }
  180. if (!(iir & UART_IIR_NO_INT))
  181. {
  182. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  183. }
  184. if ((iir & UART_IIR_BUSY) == UART_IIR_BUSY)
  185. {
  186. /* Clear the USR */
  187. dw8250_read32(uart->hw_base, UART_USR);
  188. return;
  189. }
  190. }
  191. #if defined(BOARD_TYPE_MILKV_DUO) || defined(BOARD_TYPE_MILKV_DUO_SPINOR)
  192. #ifdef BSP_USING_UART0
  193. static const char *pinname_whitelist_uart0_rx[] = {
  194. "UART0_RX",
  195. NULL,
  196. };
  197. static const char *pinname_whitelist_uart0_tx[] = {
  198. "UART0_TX",
  199. NULL,
  200. };
  201. #endif
  202. #ifdef BSP_USING_UART1
  203. static const char *pinname_whitelist_uart1_rx[] = {
  204. "IIC0_SDA",
  205. "UART0_RX",
  206. NULL,
  207. };
  208. static const char *pinname_whitelist_uart1_tx[] = {
  209. "IIC0_SCL",
  210. "UART0_TX",
  211. NULL,
  212. };
  213. #endif
  214. #ifdef BSP_USING_UART2
  215. static const char *pinname_whitelist_uart2_rx[] = {
  216. "IIC0_SDA",
  217. "SD1_D1",
  218. NULL,
  219. };
  220. static const char *pinname_whitelist_uart2_tx[] = {
  221. "IIC0_SCL",
  222. "SD1_D2",
  223. NULL,
  224. };
  225. #endif
  226. #ifdef BSP_USING_UART3
  227. static const char *pinname_whitelist_uart3_rx[] = {
  228. "SD1_D1",
  229. NULL,
  230. };
  231. static const char *pinname_whitelist_uart3_tx[] = {
  232. "SD1_D2",
  233. NULL,
  234. };
  235. #endif
  236. #ifdef BSP_USING_UART4
  237. static const char *pinname_whitelist_uart4_rx[] = {
  238. "SD1_GPIO0",
  239. NULL,
  240. };
  241. static const char *pinname_whitelist_uart4_tx[] = {
  242. "SD1_GPIO1",
  243. NULL,
  244. };
  245. #endif
  246. #elif defined(BOARD_TYPE_MILKV_DUO256M) || defined(BOARD_TYPE_MILKV_DUO256M_SPINOR)
  247. #ifdef BSP_USING_UART0
  248. static const char *pinname_whitelist_uart0_rx[] = {
  249. "UART0_RX",
  250. NULL,
  251. };
  252. static const char *pinname_whitelist_uart0_tx[] = {
  253. "UART0_TX",
  254. NULL,
  255. };
  256. #endif
  257. #ifdef BSP_USING_UART1
  258. static const char *pinname_whitelist_uart1_rx[] = {
  259. "IIC0_SDA",
  260. "JTAG_CPU_TCK",
  261. "UART0_RX",
  262. NULL,
  263. };
  264. static const char *pinname_whitelist_uart1_tx[] = {
  265. "IIC0_SCL",
  266. "JTAG_CPU_TMS",
  267. "UART0_TX",
  268. NULL,
  269. };
  270. #endif
  271. #ifdef BSP_USING_UART2
  272. static const char *pinname_whitelist_uart2_rx[] = {
  273. "IIC0_SDA",
  274. "SD1_D1",
  275. NULL,
  276. };
  277. static const char *pinname_whitelist_uart2_tx[] = {
  278. "IIC0_SCL",
  279. "SD1_D2",
  280. NULL,
  281. };
  282. #endif
  283. #ifdef BSP_USING_UART3
  284. static const char *pinname_whitelist_uart3_rx[] = {
  285. "SD1_D1",
  286. NULL,
  287. };
  288. static const char *pinname_whitelist_uart3_tx[] = {
  289. "SD1_D2",
  290. NULL,
  291. };
  292. #endif
  293. #ifdef BSP_USING_UART4
  294. static const char *pinname_whitelist_uart4_rx[] = {
  295. NULL,
  296. };
  297. static const char *pinname_whitelist_uart4_tx[] = {
  298. NULL,
  299. };
  300. #endif
  301. #elif defined(BOARD_TYPE_MILKV_DUOS)
  302. #ifdef BSP_USING_UART0
  303. static const char *pinname_whitelist_uart0_rx[] = {
  304. "UART0_RX",
  305. NULL,
  306. };
  307. static const char *pinname_whitelist_uart0_tx[] = {
  308. "UART0_TX",
  309. NULL,
  310. };
  311. #endif
  312. #ifdef BSP_USING_UART1
  313. static const char *pinname_whitelist_uart1_rx[] = {
  314. "JTAG_CPU_TCK",
  315. "UART0_RX",
  316. NULL,
  317. };
  318. static const char *pinname_whitelist_uart1_tx[] = {
  319. "JTAG_CPU_TMS",
  320. "UART0_TX",
  321. "IIC0_SCL",
  322. NULL,
  323. };
  324. #endif
  325. #ifdef BSP_USING_UART2
  326. static const char *pinname_whitelist_uart2_rx[] = {
  327. "VIVO_D9",
  328. "VIVO_D5",
  329. "VIVO_CLK",
  330. "PWR_GPIO1",
  331. NULL,
  332. };
  333. static const char *pinname_whitelist_uart2_tx[] = {
  334. "VIVO_D10",
  335. "VIVO_D6",
  336. "VIVO_D2",
  337. "IIC0_SCL",
  338. "PWR_GPIO0",
  339. NULL,
  340. };
  341. #endif
  342. #ifdef BSP_USING_UART3
  343. static const char *pinname_whitelist_uart3_rx[] = {
  344. "ADC2",
  345. NULL,
  346. };
  347. static const char *pinname_whitelist_uart3_tx[] = {
  348. "ADC3",
  349. NULL,
  350. };
  351. #endif
  352. #ifdef BSP_USING_UART4
  353. static const char *pinname_whitelist_uart4_rx[] = {
  354. NULL,
  355. };
  356. static const char *pinname_whitelist_uart4_tx[] = {
  357. NULL,
  358. };
  359. #endif
  360. #else
  361. #error "Unsupported board type!"
  362. #endif
  363. int rt_hw_uart_init(void)
  364. {
  365. struct hw_uart_device* uart;
  366. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  367. config.baud_rate = 115200;
  368. #define BSP_INSTALL_UART_DEVICE(no) \
  369. uart = &_uart##no##_device; \
  370. _serial##no.ops = &_uart_ops; \
  371. _serial##no.config = config; \
  372. rt_hw_serial_register(&_serial##no, "uart" #no, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, uart); \
  373. rt_hw_interrupt_install(uart->irqno, rt_hw_uart_isr, &_serial##no, "uart" #no);
  374. #ifdef BSP_USING_UART0
  375. pinmux_config(BSP_UART0_RX_PINNAME, UART0_RX, pinname_whitelist_uart0_rx);
  376. pinmux_config(BSP_UART0_TX_PINNAME, UART0_TX, pinname_whitelist_uart0_tx);
  377. BSP_INSTALL_UART_DEVICE(0);
  378. #if defined(ARCH_ARM)
  379. uart->hw_base = (rt_size_t)rt_ioremap((void*)uart->hw_base, 0x10000);
  380. #endif /* defined(ARCH_ARM) */
  381. #endif
  382. #ifdef BSP_USING_UART1
  383. pinmux_config(BSP_UART1_RX_PINNAME, UART1_RX, pinname_whitelist_uart1_rx);
  384. pinmux_config(BSP_UART1_TX_PINNAME, UART1_TX, pinname_whitelist_uart1_tx);
  385. BSP_INSTALL_UART_DEVICE(1);
  386. #if defined(ARCH_ARM)
  387. uart->hw_base = (rt_size_t)rt_ioremap((void*)uart->hw_base, 0x10000);
  388. #endif /* defined(ARCH_ARM) */
  389. #endif
  390. #ifdef BSP_USING_UART2
  391. pinmux_config(BSP_UART2_RX_PINNAME, UART2_RX, pinname_whitelist_uart2_rx);
  392. pinmux_config(BSP_UART2_TX_PINNAME, UART2_TX, pinname_whitelist_uart2_tx);
  393. BSP_INSTALL_UART_DEVICE(2);
  394. #if defined(ARCH_ARM)
  395. uart->hw_base = (rt_size_t)rt_ioremap((void*)uart->hw_base, 0x10000);
  396. #endif /* defined(ARCH_ARM) */
  397. #endif
  398. #ifdef BSP_USING_UART3
  399. pinmux_config(BSP_UART3_RX_PINNAME, UART3_RX, pinname_whitelist_uart3_rx);
  400. pinmux_config(BSP_UART3_TX_PINNAME, UART3_TX, pinname_whitelist_uart3_tx);
  401. BSP_INSTALL_UART_DEVICE(3);
  402. #if defined(ARCH_ARM)
  403. uart->hw_base = (rt_size_t)rt_ioremap((void*)uart->hw_base, 0x10000);
  404. #endif /* defined(ARCH_ARM) */
  405. #endif
  406. #ifdef BSP_USING_UART4
  407. pinmux_config(BSP_UART4_RX_PINNAME, UART4_RX, pinname_whitelist_uart4_rx);
  408. pinmux_config(BSP_UART4_TX_PINNAME, UART4_TX, pinname_whitelist_uart4_tx);
  409. BSP_INSTALL_UART_DEVICE(4);
  410. #if defined(ARCH_ARM)
  411. uart->hw_base = (rt_size_t)rt_ioremap((void*)uart->hw_base, 0x10000);
  412. #endif /* defined(ARCH_ARM) */
  413. #endif
  414. return 0;
  415. }