serial.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2013-03-30 Bernard the first verion
  9. */
  10. #include <rthw.h>
  11. #include <rtdevice.h>
  12. #include "serial.h"
  13. #include "board.h"
  14. #include "mmu.h"
  15. struct hw_uart_device
  16. {
  17. rt_uint32_t hw_base;
  18. rt_uint32_t irqno;
  19. };
  20. #define UART_DR(base) __REG32(base + 0x00)
  21. #define UART_FR(base) __REG32(base + 0x18)
  22. #define UART_CR(base) __REG32(base + 0x30)
  23. #define UART_IMSC(base) __REG32(base + 0x38)
  24. #define UART_ICR(base) __REG32(base + 0x44)
  25. #define UARTFR_RXFE 0x10
  26. #define UARTFR_TXFF 0x20
  27. #define UARTIMSC_RXIM 0x10
  28. #define UARTIMSC_TXIM 0x20
  29. #define UARTICR_RXIC 0x10
  30. #define UARTICR_TXIC 0x20
  31. static void rt_hw_uart_isr(int irqno, void *param)
  32. {
  33. struct rt_serial_device *serial = (struct rt_serial_device *)param;
  34. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  35. }
  36. static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  37. {
  38. return RT_EOK;
  39. }
  40. static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  41. {
  42. struct hw_uart_device *uart;
  43. RT_ASSERT(serial != RT_NULL);
  44. uart = (struct hw_uart_device *)serial->parent.user_data;
  45. switch (cmd)
  46. {
  47. case RT_DEVICE_CTRL_CLR_INT:
  48. /* disable rx irq */
  49. UART_IMSC(uart->hw_base) &= ~UARTIMSC_RXIM;
  50. break;
  51. case RT_DEVICE_CTRL_SET_INT:
  52. /* enable rx irq */
  53. UART_IMSC(uart->hw_base) |= UARTIMSC_RXIM;
  54. rt_hw_interrupt_umask(uart->irqno);
  55. break;
  56. }
  57. return RT_EOK;
  58. }
  59. static int uart_putc(struct rt_serial_device *serial, char c)
  60. {
  61. struct hw_uart_device *uart;
  62. RT_ASSERT(serial != RT_NULL);
  63. uart = (struct hw_uart_device *)serial->parent.user_data;
  64. while (UART_FR(uart->hw_base) & UARTFR_TXFF);
  65. UART_DR(uart->hw_base) = c;
  66. return 1;
  67. }
  68. static int uart_getc(struct rt_serial_device *serial)
  69. {
  70. int ch;
  71. struct hw_uart_device *uart;
  72. RT_ASSERT(serial != RT_NULL);
  73. uart = (struct hw_uart_device *)serial->parent.user_data;
  74. ch = -1;
  75. if (!(UART_FR(uart->hw_base) & UARTFR_RXFE))
  76. {
  77. ch = UART_DR(uart->hw_base) & 0xff;
  78. }
  79. return ch;
  80. }
  81. static const struct rt_uart_ops _uart_ops =
  82. {
  83. uart_configure,
  84. uart_control,
  85. uart_putc,
  86. uart_getc,
  87. };
  88. #ifdef RT_USING_UART0
  89. /* UART device driver structure */
  90. static struct hw_uart_device _uart0_device =
  91. {
  92. REALVIEW_UART0_BASE,
  93. IRQ_PBA8_UART0,
  94. };
  95. static struct rt_serial_device _serial0;
  96. #endif
  97. #ifdef RT_USING_UART1
  98. /* UART1 device driver structure */
  99. static struct hw_uart_device _uart1_device =
  100. {
  101. REALVIEW_UART1_BASE,
  102. IRQ_PBA8_UART1,
  103. };
  104. static struct rt_serial_device _serial1;
  105. #endif
  106. int rt_hw_uart_init(void)
  107. {
  108. struct hw_uart_device *uart;
  109. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  110. #ifdef RT_USING_UART0
  111. #ifdef RT_USING_SMART
  112. _uart0_device.hw_base = (uint32_t)rt_ioremap((void*)_uart0_device.hw_base, 0x1000);
  113. #endif
  114. uart = &_uart0_device;
  115. _serial0.ops = &_uart_ops;
  116. _serial0.config = config;
  117. /* register UART1 device */
  118. rt_hw_serial_register(&_serial0, "uart0",
  119. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  120. uart);
  121. rt_hw_interrupt_install(uart->irqno, rt_hw_uart_isr, &_serial0, "uart0");
  122. /* enable Rx and Tx of UART */
  123. UART_CR(uart->hw_base) = (1 << 0) | (1 << 8) | (1 << 9);
  124. #endif
  125. #ifdef RT_USING_UART1
  126. #ifdef RT_USING_SMART
  127. _uart1_device.hw_base = (uint32_t)rt_ioremap((void*)_uart1_device.hw_base, 0x1000);
  128. #endif
  129. uart = &_uart1_device;
  130. _serial1.ops = &_uart_ops;
  131. _serial1.config = config;
  132. /* register UART1 device */
  133. rt_hw_serial_register(&_serial1, "uart1",
  134. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, uart);
  135. /* enable Rx and Tx of UART */
  136. UART_CR(uart->hw_base) = (1 << 0) | (1 << 8) | (1 << 9);
  137. rt_hw_interrupt_install(uart->irqno, rt_hw_uart_isr, &_serial1, "uart1");
  138. #endif
  139. return 0;
  140. }
  141. INIT_BOARD_EXPORT(rt_hw_uart_init);