drv_uart.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Copyright (c) 2006-2023, 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. #define DBG_TAG "DRV.UART"
  16. #define DBG_LVL DBG_WARNING
  17. #include <rtdbg.h>
  18. /*
  19. * The Synopsys DesignWare 8250 has an extra feature whereby it detects if the
  20. * LCR is written whilst busy. If it is, then a busy detect interrupt is
  21. * raised, the LCR needs to be rewritten and the uart status register read.
  22. */
  23. #define UART_RX 0 /* In: Receive buffer */
  24. #define UART_TX 0 /* Out: Transmit buffer */
  25. #define UART_DLL 0 /* Out: Divisor Latch Low */
  26. #define UART_DLM 1 /* Out: Divisor Latch High */
  27. #define UART_IER 1 /* Out: Interrupt Enable Register */
  28. #define UART_IER_RDI 0x01 /* Enable receiver data interrupt */
  29. #define UART_SSR 0x22 /* In: Software Reset Register */
  30. #define UART_USR 0x1f /* UART Status Register */
  31. #define UART_LCR 3 /* Out: Line Control Register */
  32. #define UART_LCR_DLAB 0x80 /* Divisor latch access bit */
  33. #define UART_LCR_SPAR 0x20 /* Stick parity (?) */
  34. #define UART_LCR_PARITY 0x8 /* Parity Enable */
  35. #define UART_LCR_STOP 0x4 /* Stop bits: 0=1 bit, 1=2 bits */
  36. #define UART_LCR_WLEN8 0x3 /* Wordlength: 8 bits */
  37. #define UART_MCR 4 /* Out: Modem Control Register */
  38. #define UART_MCR_RTS 0x02 /* RTS complement */
  39. #define UART_LSR 5 /* In: Line Status Register */
  40. #define UART_LSR_BI 0x10 /* Break interrupt indicator */
  41. #define UART_LSR_DR 0x01 /* Receiver data ready */
  42. #define UART_IIR 2 /* In: Interrupt ID Register */
  43. #define UART_IIR_NO_INT 0x01 /* No interrupts pending */
  44. #define UART_IIR_BUSY 0x07 /* DesignWare APB Busy Detect */
  45. #define UART_IIR_RX_TIMEOUT 0x0c /* OMAP RX Timeout interrupt */
  46. #define UART_FCR 2 /* Out: FIFO Control Register */
  47. #define UART_FCR_EN_FIFO 0x01 /* Enable the FIFO */
  48. #define UART_FCR_CLEAR_RCVR 0x02 /* Clear the RCVR FIFO */
  49. #define UART_FCR_CLEAR_XMIT 0x04 /* Clear the XMIT FIFO */
  50. struct hw_uart_device
  51. {
  52. rt_ubase_t hw_base;
  53. rt_uint32_t irqno;
  54. };
  55. #define BSP_DEFINE_UART_DEVICE(no) \
  56. static struct hw_uart_device _uart##no##_device = \
  57. { \
  58. UART##no##_BASE, \
  59. UART##no##_IRQ \
  60. }; \
  61. static struct rt_serial_device _serial##no;
  62. #ifdef RT_USING_UART0
  63. BSP_DEFINE_UART_DEVICE(0);
  64. #endif
  65. #ifdef RT_USING_UART1
  66. BSP_DEFINE_UART_DEVICE(1);
  67. #endif
  68. #ifdef RT_USING_UART2
  69. BSP_DEFINE_UART_DEVICE(2);
  70. #endif
  71. #ifdef RT_USING_UART3
  72. BSP_DEFINE_UART_DEVICE(3);
  73. #endif
  74. rt_inline rt_uint32_t dw8250_read32(rt_ubase_t addr, rt_ubase_t offset)
  75. {
  76. return *((volatile rt_uint32_t *)(addr + (offset << UART_REG_SHIFT)));
  77. }
  78. rt_inline void dw8250_write32(rt_ubase_t addr, rt_ubase_t offset, rt_uint32_t value)
  79. {
  80. *((volatile rt_uint32_t *)(addr + (offset << UART_REG_SHIFT))) = value;
  81. if (offset == UART_LCR)
  82. {
  83. int tries = 1000;
  84. /* Make sure LCR write wasn't ignored */
  85. while (tries--)
  86. {
  87. unsigned int lcr = dw8250_read32(addr, UART_LCR);
  88. if ((value & ~UART_LCR_SPAR) == (lcr & ~UART_LCR_SPAR))
  89. {
  90. return;
  91. }
  92. dw8250_write32(addr, UART_FCR, UART_FCR_EN_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  93. dw8250_read32(addr, UART_RX);
  94. *((volatile rt_uint32_t *)(addr + (offset << UART_REG_SHIFT))) = value;
  95. }
  96. }
  97. }
  98. static rt_err_t dw8250_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  99. {
  100. rt_base_t base, rate;
  101. struct hw_uart_device *uart;
  102. RT_ASSERT(serial != RT_NULL);
  103. uart = (struct hw_uart_device *)serial->parent.user_data;
  104. base = uart->hw_base;
  105. /* Resset UART */
  106. dw8250_write32(base, UART_SSR, 1);
  107. dw8250_write32(base, UART_SSR, 0);
  108. dw8250_write32(base, UART_IER, !UART_IER_RDI);
  109. dw8250_write32(base, UART_FCR, UART_FCR_EN_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  110. /* Disable flow ctrl */
  111. dw8250_write32(base, UART_MCR, 0);
  112. /* Clear RTS */
  113. dw8250_write32(base, UART_MCR, dw8250_read32(base, UART_MCR) | UART_MCR_RTS);
  114. rate = UART_INPUT_CLK / 16 / serial->config.baud_rate;
  115. /* Enable access DLL & DLH */
  116. dw8250_write32(base, UART_LCR, dw8250_read32(base, UART_LCR) | UART_LCR_DLAB);
  117. dw8250_write32(base, UART_DLL, (rate & 0xff));
  118. dw8250_write32(base, UART_DLM, (rate & 0xff00) >> 8);
  119. /* Clear DLAB bit */
  120. dw8250_write32(base, UART_LCR, dw8250_read32(base, UART_LCR) & (~UART_LCR_DLAB));
  121. dw8250_write32(base, UART_LCR, (dw8250_read32(base, UART_LCR) & (~UART_LCR_WLEN8)) | UART_LCR_WLEN8);
  122. dw8250_write32(base, UART_LCR, dw8250_read32(base, UART_LCR) & (~UART_LCR_STOP));
  123. dw8250_write32(base, UART_LCR, dw8250_read32(base, UART_LCR) & (~UART_LCR_PARITY));
  124. dw8250_write32(base, UART_IER, UART_IER_RDI);
  125. return RT_EOK;
  126. }
  127. static rt_err_t dw8250_uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  128. {
  129. struct hw_uart_device *uart;
  130. RT_ASSERT(serial != RT_NULL);
  131. uart = (struct hw_uart_device *)serial->parent.user_data;
  132. switch (cmd)
  133. {
  134. case RT_DEVICE_CTRL_CLR_INT:
  135. /* Disable rx irq */
  136. dw8250_write32(uart->hw_base, UART_IER, !UART_IER_RDI);
  137. rt_hw_interrupt_mask(uart->irqno);
  138. break;
  139. case RT_DEVICE_CTRL_SET_INT:
  140. /* Enable rx irq */
  141. dw8250_write32(uart->hw_base, UART_IER, UART_IER_RDI);
  142. rt_hw_interrupt_umask(uart->irqno);
  143. break;
  144. }
  145. return RT_EOK;
  146. }
  147. static int dw8250_uart_putc(struct rt_serial_device *serial, char c)
  148. {
  149. rt_base_t base;
  150. struct hw_uart_device *uart;
  151. RT_ASSERT(serial != RT_NULL);
  152. uart = (struct hw_uart_device *)serial->parent.user_data;
  153. base = uart->hw_base;
  154. while ((dw8250_read32(base, UART_USR) & 0x2) == 0)
  155. {
  156. }
  157. dw8250_write32(base, UART_TX, c);
  158. return 1;
  159. }
  160. static int dw8250_uart_getc(struct rt_serial_device *serial)
  161. {
  162. int ch = -1;
  163. rt_base_t base;
  164. struct hw_uart_device *uart;
  165. RT_ASSERT(serial != RT_NULL);
  166. uart = (struct hw_uart_device *)serial->parent.user_data;
  167. base = uart->hw_base;
  168. if ((dw8250_read32(base, UART_LSR) & 0x1))
  169. {
  170. ch = dw8250_read32(base, UART_RX) & 0xff;
  171. }
  172. return ch;
  173. }
  174. static const struct rt_uart_ops _uart_ops =
  175. {
  176. dw8250_uart_configure,
  177. dw8250_uart_control,
  178. dw8250_uart_putc,
  179. dw8250_uart_getc,
  180. };
  181. static void rt_hw_uart_isr(int irqno, void *param)
  182. {
  183. unsigned int iir, status;
  184. struct rt_serial_device *serial = (struct rt_serial_device *)param;
  185. struct hw_uart_device *uart = (struct hw_uart_device *)serial->parent.user_data;
  186. iir = dw8250_read32(uart->hw_base, UART_IIR);
  187. /* If don't do this in non-DMA mode then the "RX TIMEOUT" interrupt will fire forever. */
  188. if ((iir & 0x3f) == UART_IIR_RX_TIMEOUT)
  189. {
  190. status = dw8250_read32(uart->hw_base, UART_LSR);
  191. if (!(status & (UART_LSR_DR | UART_LSR_BI)))
  192. {
  193. dw8250_read32(uart->hw_base, UART_RX);
  194. }
  195. }
  196. if (!(iir & UART_IIR_NO_INT))
  197. {
  198. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  199. }
  200. if ((iir & UART_IIR_BUSY) == UART_IIR_BUSY)
  201. {
  202. /* Clear the USR */
  203. dw8250_read32(uart->hw_base, UART_USR);
  204. return;
  205. }
  206. }
  207. int rt_hw_uart_init(void)
  208. {
  209. struct hw_uart_device* uart;
  210. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  211. config.baud_rate = 115200;
  212. #define BSP_INSTALL_UART_DEVICE(no) \
  213. uart = &_uart##no##_device; \
  214. _serial##no.ops = &_uart_ops; \
  215. _serial##no.config = config; \
  216. rt_hw_serial_register(&_serial##no, "uart" #no, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, uart); \
  217. rt_hw_interrupt_install(uart->irqno, rt_hw_uart_isr, &_serial##no, "uart" #no);
  218. #ifdef RT_USING_UART0
  219. BSP_INSTALL_UART_DEVICE(0);
  220. #endif
  221. #ifdef RT_USING_UART1
  222. BSP_INSTALL_UART_DEVICE(1);
  223. #endif
  224. #ifdef RT_USING_UART2
  225. BSP_INSTALL_UART_DEVICE(2);
  226. #endif
  227. #ifdef RT_USING_UART3
  228. BSP_INSTALL_UART_DEVICE(3);
  229. #endif
  230. return 0;
  231. }