drv_uart.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * Copyright (c) 2019-2020
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. */
  7. #include <rthw.h>
  8. #include <rtdevice.h>
  9. #include <lwp_user_mm.h>
  10. #include <ioremap.h>
  11. #include "board.h"
  12. #include "drv_uart.h"
  13. #include "riscv_io.h"
  14. #include "board.h"
  15. #define UART_DEFAULT_BAUDRATE 115200
  16. #define UART_CLK 50000000
  17. #define UART_ADDR 0x91403000UL
  18. #define UART_IRQ 0x13
  19. #define UART_RBR (0x00) /* receive buffer register */
  20. #define UART_THR (0x00) /* transmit holding register */
  21. #define UART_DLL (0x00) /* divisor latch low register */
  22. #define UART_DLH (0x04) /* diviso latch high register */
  23. #define UART_IER (0x04) /* interrupt enable register */
  24. #define UART_IIR (0x08) /* interrupt identity register */
  25. #define UART_FCR (0x08) /* FIFO control register */
  26. #define UART_LCR (0x0c) /* line control register */
  27. #define UART_MCR (0x10) /* modem control register */
  28. #define UART_LSR (0x14) /* line status register */
  29. #define UART_MSR (0x18) /* modem status register */
  30. #define UART_SCH (0x1c) /* scratch register */
  31. #define UART_USR (0x7c) /* status register */
  32. #define UART_TFL (0x80) /* transmit FIFO level */
  33. #define UART_RFL (0x84) /* RFL */
  34. #define UART_HALT (0xa4) /* halt tx register */
  35. #define UART_DLF (0xc0) /* Divisor Latch Fraction Register */
  36. #define BIT(x) (1 << x)
  37. /* Line Status Rigster */
  38. #define UART_LSR_RXFIFOE (BIT(7))
  39. #define UART_LSR_TEMT (BIT(6))
  40. #define UART_LSR_THRE (BIT(5))
  41. #define UART_LSR_BI (BIT(4))
  42. #define UART_LSR_FE (BIT(3))
  43. #define UART_LSR_PE (BIT(2))
  44. #define UART_LSR_OE (BIT(1))
  45. #define UART_LSR_DR (BIT(0))
  46. #define UART_LSR_BRK_ERROR_BITS 0x1E /* BI, FE, PE, OE bits */
  47. /* Line Control Register */
  48. #define UART_LCR_DLAB (BIT(7))
  49. #define UART_LCR_SBC (BIT(6))
  50. #define UART_LCR_PARITY_MASK (BIT(5)|BIT(4))
  51. #define UART_LCR_EPAR (1 << 4)
  52. #define UART_LCR_OPAR (0 << 4)
  53. #define UART_LCR_PARITY (BIT(3))
  54. #define UART_LCR_STOP (BIT(2))
  55. #define UART_LCR_DLEN_MASK (BIT(1)|BIT(0))
  56. #define UART_LCR_WLEN5 (0)
  57. #define UART_LCR_WLEN6 (1)
  58. #define UART_LCR_WLEN7 (2)
  59. #define UART_LCR_WLEN8 (3)
  60. /* Halt Register */
  61. #define UART_HALT_LCRUP (BIT(2))
  62. #define UART_HALT_FORCECFG (BIT(1))
  63. #define UART_HALT_HTX (BIT(0))
  64. /* Interrupt Enable Register */
  65. #define UART_IER_MASK (0xff)
  66. #define UART_IER_PTIME (BIT(7))
  67. #define UART_IER_RS485 (BIT(4))
  68. #define UART_IER_MSI (BIT(3))
  69. #define UART_IER_RLSI (BIT(2))
  70. #define UART_IER_THRI (BIT(1))
  71. #define UART_IER_RDI (BIT(0))
  72. /* Interrupt ID Register */
  73. #define UART_IIR_FEFLAG_MASK (BIT(6)|BIT(7))
  74. #define UART_IIR_IID_MASK (BIT(0)|BIT(1)|BIT(2)|BIT(3))
  75. #define UART_IIR_IID_MSTA (0)
  76. #define UART_IIR_IID_NOIRQ (1)
  77. #define UART_IIR_IID_THREMP (2)
  78. #define UART_IIR_IID_RXDVAL (4)
  79. #define UART_IIR_IID_LINESTA (6)
  80. #define UART_IIR_IID_BUSBSY (7)
  81. #define UART_IIR_IID_CHARTO (12)
  82. struct device_uart
  83. {
  84. rt_ubase_t hw_base;
  85. rt_uint32_t irqno;
  86. };
  87. static rt_err_t rt_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg);
  88. static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg);
  89. static int drv_uart_putc(struct rt_serial_device *serial, char c);
  90. static int drv_uart_getc(struct rt_serial_device *serial);
  91. const struct rt_uart_ops _uart_ops =
  92. {
  93. rt_uart_configure,
  94. uart_control,
  95. drv_uart_putc,
  96. drv_uart_getc,
  97. //TODO: add DMA support
  98. RT_NULL
  99. };
  100. struct rt_serial_device serial1;
  101. struct device_uart uart1;
  102. #define write32(addr, val) writel(val, (void*)(addr))
  103. #define read32(addr) readl((void*)(addr))
  104. static void _uart_init(void *uart_base)
  105. {
  106. uint32_t bdiv;
  107. uint32_t dlf;
  108. uint32_t dlh;
  109. uint32_t dll;
  110. bdiv = UART_CLK / UART_DEFAULT_BAUDRATE;
  111. dlh = bdiv >> 12;
  112. dll = (bdiv - (dlh << 12)) / 16;
  113. dlf = bdiv - (dlh << 12) - dll * 16;
  114. if(dlh == 0 && dll == 0)
  115. {
  116. dll = 1;
  117. dlf = 0;
  118. }
  119. write32(uart_base + UART_LCR, 0x00);
  120. /* Disable all interrupts */
  121. write32(uart_base + UART_IER, 0x00);
  122. /* Enable DLAB */
  123. write32(uart_base + UART_LCR, 0x80);
  124. if (bdiv) {
  125. /* Set divisor low byte */
  126. write32(uart_base + UART_DLL, dll);
  127. /* Set divisor high byte */
  128. write32(uart_base + UART_DLH, dlh);
  129. /* Set divisor fraction byte*/
  130. write32(uart_base + UART_DLF, dlf);
  131. }
  132. /* 8 bits, no parity, one stop bit */
  133. write32(uart_base + UART_LCR, 0x03);
  134. /* Enable FIFO */
  135. write32(uart_base + UART_FCR, 0x01);
  136. /* No modem control DTR RTS */
  137. write32(uart_base + UART_MCR, 0x00);
  138. /* Clear line status */
  139. read32(uart_base + UART_LSR);
  140. /* Read receive buffer */
  141. read32(uart_base + UART_RBR);
  142. read32(uart_base + UART_USR);
  143. read32(uart_base + UART_FCR);
  144. /* Set scratchpad */
  145. write32(uart_base + UART_SCH, 0x00);
  146. //enable uart rx irq
  147. // write32(uart_base + UART_IER, 0x01);
  148. }
  149. static void uart_set_isr(void *uart_base, uint8_t enable, uint32_t irq_type)
  150. {
  151. uint32_t value;
  152. value = read32(uart_base + UART_IER);
  153. if (enable)
  154. {
  155. value |= irq_type;
  156. }
  157. else
  158. {
  159. value &= ~irq_type;
  160. }
  161. write32(uart_base + UART_IER, value);
  162. }
  163. /*
  164. * UART interface
  165. */
  166. static rt_err_t rt_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  167. {
  168. return (RT_EOK);
  169. }
  170. static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  171. {
  172. struct device_uart *uart = (struct device_uart*)serial->parent.user_data;
  173. #ifdef RT_USING_SERIAL_V2
  174. rt_ubase_t ctrl_flag = 0;
  175. rt_ubase_t ctrl_arg;
  176. #endif
  177. #ifdef RT_USING_SERIAL_V2
  178. ctrl_arg = (rt_ubase_t)arg;
  179. if (ctrl_arg & (RT_DEVICE_FLAG_RX_BLOCKING | RT_DEVICE_FLAG_RX_NON_BLOCKING))
  180. {
  181. ctrl_flag |= RT_DEVICE_FLAG_INT_RX;
  182. }
  183. #endif
  184. switch (cmd)
  185. {
  186. case RT_DEVICE_CTRL_CLR_INT:
  187. #ifdef RT_USING_SERIAL_V2
  188. if (ctrl_flag & RT_DEVICE_FLAG_INT_RX)
  189. #else
  190. if ((size_t)arg == RT_DEVICE_FLAG_INT_RX)
  191. #endif
  192. {
  193. uart_set_isr((void*)(uart->hw_base), 0, UART_IER_RDI);
  194. }
  195. break;
  196. case RT_DEVICE_CTRL_SET_INT:
  197. #ifdef RT_USING_SERIAL_V2
  198. if (ctrl_flag & RT_DEVICE_FLAG_INT_RX)
  199. #else
  200. if ((size_t)arg == RT_DEVICE_FLAG_INT_RX)
  201. #endif
  202. {
  203. uart_set_isr((void*)(uart->hw_base), 1, UART_IER_RDI);
  204. }
  205. break;
  206. #ifdef RT_USING_SERIAL_V2
  207. case RT_DEVICE_CTRL_CONFIG:
  208. if (ctrl_flag & RT_DEVICE_FLAG_INT_RX)
  209. {
  210. uart_set_isr((void*)(uart->hw_base), 1, UART_IER_RDI);
  211. }
  212. break;
  213. #endif
  214. case RT_FIOMMAP2:
  215. {
  216. struct dfs_mmap2_args *mmap2 = (struct dfs_mmap2_args *)arg;
  217. if (mmap2)
  218. {
  219. if (mmap2->length > 0x400)
  220. {
  221. return -RT_ENOMEM;
  222. }
  223. mmap2->ret = lwp_map_user_phy(lwp_self(), RT_NULL, (void*)(uart->hw_base), mmap2->length, 0);
  224. }
  225. break;
  226. }
  227. }
  228. return (RT_EOK);
  229. }
  230. static int drv_uart_putc(struct rt_serial_device *serial, char c)
  231. {
  232. volatile uint32_t *sed_buf;
  233. volatile uint32_t *sta;
  234. struct device_uart *uart = (struct device_uart*)serial->parent.user_data;
  235. sed_buf = (uint32_t *)(uart->hw_base + UART_THR);
  236. sta = (uint32_t *)(uart->hw_base + UART_USR);
  237. /* FIFO status, contain valid data */
  238. // while (!(*sta & 0x02));
  239. while (!(read32(uart->hw_base + UART_LSR) & 0x20));
  240. *sed_buf = c;
  241. return (1);
  242. }
  243. static int drv_uart_getc(struct rt_serial_device *serial)
  244. {
  245. struct device_uart *uart = (struct device_uart*)serial->parent.user_data;
  246. volatile uint32_t *lsr = (uint32_t *)(uart->hw_base + UART_LSR);
  247. volatile uint32_t *rbr = (uint32_t *)(uart->hw_base + UART_RBR);
  248. if (!(*lsr & UART_LSR_DR))
  249. {
  250. return -1;
  251. }
  252. return (int)*rbr;
  253. }
  254. static void rt_hw_uart_isr(int irq, void *param)
  255. {
  256. struct rt_serial_device *serial = (struct rt_serial_device*)param;
  257. struct device_uart *uart;
  258. size_t uart_base;
  259. uint32_t iir, lsr;
  260. uart = (struct device_uart*)serial->parent.user_data;
  261. uart_base = uart->hw_base;
  262. iir = readb((void*)(uart_base + UART_IIR)) & UART_IIR_IID_MASK;
  263. lsr = readb((void*)(uart_base + UART_LSR));
  264. // rt_kprintf("uart isr iir:%x lsr:%x\r\n", iir, lsr);
  265. if (iir == UART_IIR_IID_BUSBSY)
  266. {
  267. (void)readb((void*)(uart_base + UART_USR));
  268. }
  269. else if (lsr & (UART_LSR_DR | UART_LSR_BI))
  270. {
  271. #ifdef RT_USING_SERIAL_V2
  272. struct rt_serial_rx_fifo *rx_fifo;
  273. uint8_t data;
  274. rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx;
  275. RT_ASSERT(rx_fifo != RT_NULL);
  276. do {
  277. data = readb((void*)(uart_base + UART_RBR));
  278. rt_ringbuffer_putchar(&(rx_fifo->rb), data);
  279. lsr = readb((void*)(uart_base + UART_LSR));
  280. } while(lsr & UART_LSR_DR);
  281. #endif
  282. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  283. }
  284. else if (iir & UART_IIR_IID_CHARTO)
  285. /* has charto irq but no dr lsr? just read and ignore */
  286. {
  287. readb((void*)(uart_base + UART_RBR));
  288. }
  289. }
  290. /*
  291. * UART Initiation
  292. */
  293. int rt_hw_uart_init(void)
  294. {
  295. struct rt_serial_device *serial;
  296. struct device_uart *uart;
  297. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  298. {
  299. serial = &serial1;
  300. uart = &uart1;
  301. serial->ops = &_uart_ops;
  302. serial->config = config;
  303. serial->config.baud_rate = UART_DEFAULT_BAUDRATE;
  304. uart->hw_base = (rt_base_t)rt_ioremap((void *)UART_ADDR, 0x1000);
  305. uart->irqno = UART_IRQ;
  306. _uart_init((void*)(uart->hw_base));
  307. rt_hw_interrupt_install(uart->irqno, rt_hw_uart_isr, serial, "uart1");
  308. rt_hw_interrupt_umask(uart->irqno);
  309. rt_hw_serial_register(serial,
  310. RT_CONSOLE_DEVICE_NAME,
  311. RT_DEVICE_FLAG_STREAM | RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  312. uart);
  313. }
  314. return 0;
  315. }