uart.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * File : uart.c
  3. * Drivers for s3c2440 uarts.
  4. * Change Logs:
  5. * Date Author Notes
  6. * 2020-04-09 Jonne Code refactoring for new bsp
  7. */
  8. #include <rthw.h>
  9. #include <rtdevice.h>
  10. #include <board.h>
  11. #define ULCON_OFS 0x00
  12. #define UCON_OFS 0x04
  13. #define UFCON_OFS 0x08
  14. #define UMCON_OFS 0x0c
  15. #define UTRSTAT_OFS 0x10
  16. #define UERSTAT_OFS 0x14
  17. #define UFSTAT_OFS 0x18
  18. #define UMSTAT_OFS 0x1c
  19. #define UTXH_OFS 0x20
  20. #define URXH_OFS 0x24
  21. #define UBRDIV_OFS 0x28
  22. #define readl(addr) (*(volatile unsigned long *)(addr))
  23. #define writel(addr, value) (*(volatile unsigned long *)(addr) = value)
  24. #define PCLK_HZ 50000000
  25. struct hw_uart_device
  26. {
  27. rt_uint32_t hw_base;
  28. rt_uint32_t irqno;
  29. };
  30. static rt_err_t s3c2440_serial_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  31. {
  32. struct hw_uart_device* uart = serial->parent.user_data;
  33. writel(uart->hw_base + UBRDIV_OFS, PCLK_HZ / (cfg->baud_rate * 16));
  34. writel(uart->hw_base + ULCON_OFS, 0x03);// 8bit data, 1bit stop, No parity
  35. writel(uart->hw_base + UCON_OFS, 0x05);
  36. writel(uart->hw_base + UFCON_OFS, 0x00);
  37. writel(uart->hw_base + UMCON_OFS, 0x00);
  38. return RT_EOK;
  39. }
  40. static rt_err_t s3c2440_serial_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. INTSUBMSK |= BIT_SUB_RXD0;
  50. break;
  51. case RT_DEVICE_CTRL_SET_INT:
  52. /* enable rx irq */
  53. INTSUBMSK &= ~(BIT_SUB_RXD0);
  54. break;
  55. }
  56. return RT_EOK;
  57. }
  58. static int s3c2440_putc(struct rt_serial_device *serial, char c)
  59. {
  60. struct hw_uart_device* uart = serial->parent.user_data;
  61. while(!(readl(uart->hw_base + UTRSTAT_OFS) & (1<<2)))
  62. {
  63. }
  64. writel(uart->hw_base + UTXH_OFS, c);
  65. return 0;
  66. }
  67. static int s3c2440_getc(struct rt_serial_device *serial)
  68. {
  69. struct hw_uart_device* uart = serial->parent.user_data;
  70. int ch = -1;
  71. if(readl(uart->hw_base + UTRSTAT_OFS) & (1<<0))
  72. {
  73. ch = readl(uart->hw_base + URXH_OFS) & 0x000000FF;
  74. }
  75. return ch;
  76. }
  77. static void rt_hw_uart_isr(int irqno, void *param)
  78. {
  79. struct rt_serial_device *serial = (struct rt_serial_device *)param;
  80. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  81. /*clear SUBSRCPND*/
  82. if(irqno == INTUART0)
  83. {
  84. SUBSRCPND = BIT_SUB_RXD0;
  85. }
  86. else if(irqno == INTUART1)
  87. {
  88. SUBSRCPND = BIT_SUB_RXD1;
  89. }
  90. else
  91. {
  92. SUBSRCPND = BIT_SUB_RXD2;
  93. }
  94. }
  95. static struct rt_uart_ops s3c2440_uart_ops = {
  96. .configure = s3c2440_serial_configure,
  97. .control = s3c2440_serial_control,
  98. .putc = s3c2440_putc,
  99. .getc = s3c2440_getc
  100. };
  101. static struct rt_serial_device _serial0 = {
  102. .ops = &s3c2440_uart_ops,
  103. .config = RT_SERIAL_CONFIG_DEFAULT,
  104. .serial_rx = NULL,
  105. .serial_tx = NULL
  106. };
  107. static struct hw_uart_device _hwserial0 = {
  108. .hw_base = 0x50000000,
  109. .irqno = INTUART0
  110. };
  111. static struct rt_serial_device _serial1 = {
  112. .ops = &s3c2440_uart_ops,
  113. .config = RT_SERIAL_CONFIG_DEFAULT,
  114. .serial_rx = NULL,
  115. .serial_tx = NULL
  116. };
  117. static struct hw_uart_device _hwserial1 = {
  118. .hw_base = 0x50004000,
  119. .irqno = INTUART1
  120. };
  121. static struct rt_serial_device _serial2 = {
  122. .ops = &s3c2440_uart_ops,
  123. .config = RT_SERIAL_CONFIG_DEFAULT,
  124. .serial_rx = NULL,
  125. .serial_tx = NULL
  126. };
  127. static struct hw_uart_device _hwserial2 = {
  128. .hw_base = 0x50008000,
  129. .irqno = INTUART2
  130. };
  131. int rt_hw_uart_init(void)
  132. {
  133. /* register UART0 device */
  134. rt_hw_serial_register(&_serial0, "uart0", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, &_hwserial0);
  135. rt_hw_interrupt_install(_hwserial0.irqno, rt_hw_uart_isr, &_serial0, "uart0");
  136. rt_hw_interrupt_umask(INTUART0);
  137. /* register UART1 device */
  138. rt_hw_serial_register(&_serial1, "uart1", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, &_hwserial1);
  139. rt_hw_interrupt_install(_hwserial1.irqno, rt_hw_uart_isr, &_serial1, "uart1");
  140. rt_hw_interrupt_umask(INTUART1);
  141. /* register UART2 device */
  142. rt_hw_serial_register(&_serial2, "uart2", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, &_hwserial2);
  143. rt_hw_interrupt_install(_hwserial2.irqno, rt_hw_uart_isr, &_serial2, "uart2");
  144. rt_hw_interrupt_umask(INTUART2);
  145. return 0;
  146. }
  147. INIT_BOARD_EXPORT(rt_hw_uart_init);