drv_uart.c 10 KB

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