drv_uart.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-3-08 GuEe-GUI the first version
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include <board.h>
  14. /*
  15. * The Synopsys DesignWare 8250 has an extra feature whereby it detects if the
  16. * LCR is written whilst busy. If it is, then a busy detect interrupt is
  17. * raised, the LCR needs to be rewritten and the uart status register read.
  18. */
  19. #define UART_RX 0 /* In: Receive buffer */
  20. #define UART_TX 0 /* Out: Transmit buffer */
  21. #define UART_DLL 0 /* Out: Divisor Latch Low */
  22. #define UART_DLM 1 /* Out: Divisor Latch High */
  23. #define UART_IER 1 /* Out: Interrupt Enable Register */
  24. #define UART_IER_RDI 0x01 /* Enable receiver data interrupt */
  25. #define UART_SSR 0x22 /* In: Software Reset Register */
  26. #define UART_USR 0x1f /* UART Status Register */
  27. #define UART_LCR 3 /* Out: Line Control Register */
  28. #define UART_LCR_DLAB 0x80 /* Divisor latch access bit */
  29. #define UART_LCR_SPAR 0x20 /* Stick parity (?) */
  30. #define UART_LCR_PARITY 0x8 /* Parity Enable */
  31. #define UART_LCR_STOP 0x4 /* Stop bits: 0=1 bit, 1=2 bits */
  32. #define UART_LCR_WLEN8 0x3 /* Wordlength: 8 bits */
  33. #define UART_MCR 4 /* Out: Modem Control Register */
  34. #define UART_MCR_RTS 0x02 /* RTS complement */
  35. #define UART_LSR 5 /* In: Line Status Register */
  36. #define UART_LSR_BI 0x10 /* Break interrupt indicator */
  37. #define UART_LSR_DR 0x01 /* Receiver data ready */
  38. #define UART_IIR 2 /* In: Interrupt ID Register */
  39. #define UART_IIR_NO_INT 0x01 /* No interrupts pending */
  40. #define UART_IIR_BUSY 0x07 /* DesignWare APB Busy Detect */
  41. #define UART_IIR_RX_TIMEOUT 0x0c /* OMAP RX Timeout interrupt */
  42. #define UART_FCR 2 /* Out: FIFO Control Register */
  43. #define UART_FCR_EN_FIFO 0x01 /* Enable the FIFO */
  44. #define UART_FCR_CLEAR_RCVR 0x02 /* Clear the RCVR FIFO */
  45. #define UART_FCR_CLEAR_XMIT 0x04 /* Clear the XMIT FIFO */
  46. #define UART_REG_SHIFT 0x2 /* Register Shift*/
  47. #define UART_INPUT_CLK 24000000
  48. struct hw_uart_device
  49. {
  50. rt_ubase_t hw_base;
  51. rt_uint32_t irqno;
  52. #ifdef RT_USING_SMP
  53. struct rt_spinlock spinlock;
  54. #endif
  55. };
  56. #define BSP_DEFINE_UART_DEVICE(no) \
  57. static struct hw_uart_device _uart##no##_device = \
  58. { \
  59. UART##no##_MMIO_BASE, \
  60. UART##no##_IRQ \
  61. }; \
  62. static struct rt_serial_device _serial##no;
  63. #ifdef RT_USING_UART0
  64. BSP_DEFINE_UART_DEVICE(0);
  65. #endif
  66. #ifdef RT_USING_UART1
  67. BSP_DEFINE_UART_DEVICE(1);
  68. #endif
  69. #ifdef RT_USING_UART2
  70. BSP_DEFINE_UART_DEVICE(2);
  71. #endif
  72. #ifdef RT_USING_UART3
  73. BSP_DEFINE_UART_DEVICE(3);
  74. #endif
  75. #ifdef RT_USING_UART4
  76. BSP_DEFINE_UART_DEVICE(4);
  77. #endif
  78. #ifdef RT_USING_UART5
  79. BSP_DEFINE_UART_DEVICE(5);
  80. #endif
  81. #ifdef RT_USING_UART6
  82. BSP_DEFINE_UART_DEVICE(6);
  83. #endif
  84. #ifdef RT_USING_UART7
  85. BSP_DEFINE_UART_DEVICE(7);
  86. #endif
  87. #ifdef RT_USING_UART8
  88. BSP_DEFINE_UART_DEVICE(8);
  89. #endif
  90. #ifdef RT_USING_UART9
  91. BSP_DEFINE_UART_DEVICE(9);
  92. #endif
  93. rt_inline rt_uint32_t dw8250_read32(rt_ubase_t addr, rt_ubase_t offset)
  94. {
  95. return *((volatile rt_uint32_t *)(addr + (offset << UART_REG_SHIFT)));
  96. }
  97. rt_inline void dw8250_write32(rt_ubase_t addr, rt_ubase_t offset, rt_uint32_t value)
  98. {
  99. *((volatile rt_uint32_t *)(addr + (offset << UART_REG_SHIFT))) = value;
  100. if (offset == UART_LCR)
  101. {
  102. int tries = 1000;
  103. /* Make sure LCR write wasn't ignored */
  104. while (tries--)
  105. {
  106. unsigned int lcr = dw8250_read32(addr, UART_LCR);
  107. if ((value & ~UART_LCR_SPAR) == (lcr & ~UART_LCR_SPAR))
  108. {
  109. return;
  110. }
  111. dw8250_write32(addr, UART_FCR, UART_FCR_EN_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  112. dw8250_read32(addr, UART_RX);
  113. *((volatile rt_uint32_t *)(addr + (offset << UART_REG_SHIFT))) = value;
  114. }
  115. }
  116. }
  117. static rt_err_t dw8250_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  118. {
  119. rt_base_t base, rate;
  120. struct hw_uart_device *uart;
  121. RT_ASSERT(serial != RT_NULL);
  122. uart = (struct hw_uart_device *)serial->parent.user_data;
  123. base = uart->hw_base;
  124. #ifdef RT_USING_SMP
  125. rt_spin_lock_init(&uart->spinlock);
  126. #endif
  127. /* Resset UART */
  128. dw8250_write32(base, UART_SSR, 1);
  129. dw8250_write32(base, UART_SSR, 0);
  130. dw8250_write32(base, UART_IER, !UART_IER_RDI);
  131. dw8250_write32(base, UART_FCR, UART_FCR_EN_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  132. /* Disable flow ctrl */
  133. dw8250_write32(base, UART_MCR, 0);
  134. /* Clear RTS */
  135. dw8250_write32(base, UART_MCR, dw8250_read32(base, UART_MCR) | UART_MCR_RTS);
  136. rate = UART_INPUT_CLK / 16 / serial->config.baud_rate;
  137. /* Enable access DLL & DLH */
  138. dw8250_write32(base, UART_LCR, dw8250_read32(base, UART_LCR) | UART_LCR_DLAB);
  139. dw8250_write32(base, UART_DLL, (rate & 0xff));
  140. dw8250_write32(base, UART_DLM, (rate & 0xff00) >> 8);
  141. /* Clear DLAB bit */
  142. dw8250_write32(base, UART_LCR, dw8250_read32(base, UART_LCR) & (~UART_LCR_DLAB));
  143. dw8250_write32(base, UART_LCR, (dw8250_read32(base, UART_LCR) & (~UART_LCR_WLEN8)) | UART_LCR_WLEN8);
  144. dw8250_write32(base, UART_LCR, dw8250_read32(base, UART_LCR) & (~UART_LCR_STOP));
  145. dw8250_write32(base, UART_LCR, dw8250_read32(base, UART_LCR) & (~UART_LCR_PARITY));
  146. dw8250_write32(base, UART_IER, UART_IER_RDI);
  147. return RT_EOK;
  148. }
  149. static rt_err_t dw8250_uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  150. {
  151. struct hw_uart_device *uart;
  152. RT_ASSERT(serial != RT_NULL);
  153. uart = (struct hw_uart_device *)serial->parent.user_data;
  154. switch (cmd)
  155. {
  156. case RT_DEVICE_CTRL_CLR_INT:
  157. /* Disable rx irq */
  158. dw8250_write32(uart->hw_base, UART_IER, !UART_IER_RDI);
  159. rt_hw_interrupt_mask(uart->irqno);
  160. break;
  161. case RT_DEVICE_CTRL_SET_INT:
  162. /* Enable rx irq */
  163. dw8250_write32(uart->hw_base, UART_IER, UART_IER_RDI);
  164. rt_hw_interrupt_umask(uart->irqno);
  165. break;
  166. }
  167. return RT_EOK;
  168. }
  169. static int dw8250_uart_putc(struct rt_serial_device *serial, char c)
  170. {
  171. rt_base_t base;
  172. struct hw_uart_device *uart;
  173. RT_ASSERT(serial != RT_NULL);
  174. uart = (struct hw_uart_device *)serial->parent.user_data;
  175. base = uart->hw_base;
  176. while ((dw8250_read32(base, UART_USR) & 0x2) == 0)
  177. {
  178. }
  179. dw8250_write32(base, UART_TX, c);
  180. return 1;
  181. }
  182. static int dw8250_uart_getc(struct rt_serial_device *serial)
  183. {
  184. int ch = -1;
  185. rt_base_t base;
  186. struct hw_uart_device *uart;
  187. RT_ASSERT(serial != RT_NULL);
  188. uart = (struct hw_uart_device *)serial->parent.user_data;
  189. base = uart->hw_base;
  190. if ((dw8250_read32(base, UART_LSR) & 0x1))
  191. {
  192. ch = dw8250_read32(base, UART_RX) & 0xff;
  193. }
  194. return ch;
  195. }
  196. static const struct rt_uart_ops _uart_ops =
  197. {
  198. dw8250_uart_configure,
  199. dw8250_uart_control,
  200. dw8250_uart_putc,
  201. dw8250_uart_getc,
  202. };
  203. static void rt_hw_uart_isr(int irqno, void *param)
  204. {
  205. unsigned int iir, status;
  206. struct rt_serial_device *serial = (struct rt_serial_device *)param;
  207. struct hw_uart_device *uart = (struct hw_uart_device *)serial->parent.user_data;
  208. iir = dw8250_read32(uart->hw_base, UART_IIR);
  209. /* If don't do this in non-DMA mode then the "RX TIMEOUT" interrupt will fire forever. */
  210. if ((iir & 0x3f) == UART_IIR_RX_TIMEOUT)
  211. {
  212. #ifdef RT_USING_SMP
  213. rt_base_t level = rt_spin_lock_irqsave(&uart->spinlock);
  214. #endif
  215. status = dw8250_read32(uart->hw_base, UART_LSR);
  216. if (!(status & (UART_LSR_DR | UART_LSR_BI)))
  217. {
  218. dw8250_read32(uart->hw_base, UART_RX);
  219. }
  220. #ifdef RT_USING_SMP
  221. rt_spin_unlock_irqrestore(&uart->spinlock, level);
  222. #endif
  223. }
  224. if (!(iir & UART_IIR_NO_INT))
  225. {
  226. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  227. }
  228. if ((iir & UART_IIR_BUSY) == UART_IIR_BUSY)
  229. {
  230. /* Clear the USR */
  231. dw8250_read32(uart->hw_base, UART_USR);
  232. return;
  233. }
  234. }
  235. int rt_hw_uart_init(void)
  236. {
  237. rt_uint32_t value;
  238. struct hw_uart_device* uart;
  239. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  240. RT_UNUSED(value);
  241. config.baud_rate = 1500000;
  242. #define BSP_INSTALL_UART_DEVICE(no) \
  243. uart = &_uart##no##_device; \
  244. _serial##no.ops = &_uart_ops; \
  245. _serial##no.config = config; \
  246. rt_hw_serial_register(&_serial##no, "uart" #no, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, uart); \
  247. rt_hw_interrupt_install(uart->irqno, rt_hw_uart_isr, &_serial##no, "uart" #no);
  248. #ifdef RT_USING_UART0
  249. BSP_INSTALL_UART_DEVICE(0);
  250. #endif
  251. #ifdef RT_USING_UART1
  252. BSP_INSTALL_UART_DEVICE(1);
  253. #endif
  254. #ifdef RT_USING_UART2
  255. BSP_INSTALL_UART_DEVICE(2);
  256. #endif
  257. #ifdef RT_USING_UART3
  258. BSP_INSTALL_UART_DEVICE(3);
  259. #endif
  260. #ifdef RT_USING_UART4
  261. HWREG32(CRU_BASE + 0x370) = 0xFFFF0000 | (0x600) |(HWREG32(CRU_BASE + 0x370) & 0xF0FF);
  262. value = HWREG32(0xFDC60000 + 0x48);
  263. value &= ~((7 << 8) | (7 << 4));
  264. value |= 0xFFFF0000 | (4 << 8) | (4 << 4);
  265. HWREG32(0xFDC60000 + 0x48) = value;
  266. HWREG32(0xFDC60000 + 0x30C) = 0xFFFF0000 | (1 << 14) | HWREG32(0xFDC60000 + 0x30C);
  267. BSP_INSTALL_UART_DEVICE(4);
  268. #endif
  269. #ifdef RT_USING_UART5
  270. BSP_INSTALL_UART_DEVICE(5);
  271. #endif
  272. #ifdef RT_USING_UART6
  273. BSP_INSTALL_UART_DEVICE(6);
  274. #endif
  275. #ifdef RT_USING_UART7
  276. BSP_INSTALL_UART_DEVICE(7);
  277. #endif
  278. #ifdef RT_USING_UART8
  279. BSP_INSTALL_UART_DEVICE(8);
  280. #endif
  281. #ifdef RT_USING_UART9
  282. BSP_INSTALL_UART_DEVICE(9);
  283. #endif
  284. return 0;
  285. }