drv_uart.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. * 2018/5/5 Bernard The first version
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include "board.h"
  14. #define PL011_UARTDR 0x000
  15. #define PL011_UARTFR 0x018
  16. #define PL011_UARTFR_TXFF_BIT 5
  17. unsigned int readl(volatile void *addr)
  18. {
  19. return *(volatile unsigned int *)addr;
  20. }
  21. void writel(unsigned int v, volatile void *addr)
  22. {
  23. *(volatile unsigned int *)addr = v;
  24. }
  25. struct hw_uart_device
  26. {
  27. rt_ubase_t hw_base;
  28. rt_uint32_t irqno;
  29. };
  30. static rt_err_t uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  31. {
  32. return RT_EOK;
  33. }
  34. static rt_err_t uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  35. {
  36. struct hw_uart_device *uart;
  37. uint32_t val;
  38. RT_ASSERT(serial != RT_NULL);
  39. uart = (struct hw_uart_device *)serial->parent.user_data;
  40. switch (cmd)
  41. {
  42. case RT_DEVICE_CTRL_CLR_INT:
  43. /* disable rx irq */
  44. val = readl((volatile void *)(uart->hw_base + 0x38));
  45. val &= ~0x10;
  46. writel(val, (volatile void *)(uart->hw_base + 0x38));
  47. rt_hw_interrupt_mask(uart->irqno);
  48. break;
  49. case RT_DEVICE_CTRL_SET_INT:
  50. /* enable rx irq */
  51. val = readl((volatile void *)(uart->hw_base + 0x38));
  52. val |= 0x10;
  53. writel(val, (volatile void *)(uart->hw_base + 0x38));
  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 (readl((volatile void *)(uart->hw_base + PL011_UARTFR)) & (1 << PL011_UARTFR_TXFF_BIT))
  65. {
  66. }
  67. writel(c, (volatile void *)( uart->hw_base + PL011_UARTDR));
  68. return 1;
  69. }
  70. static int uart_getc(struct rt_serial_device *serial)
  71. {
  72. int ch = -1;
  73. struct hw_uart_device *uart;
  74. RT_ASSERT(serial != RT_NULL);
  75. uart = (struct hw_uart_device *)serial->parent.user_data;
  76. if (!(readl((volatile void *)(uart->hw_base + 0x18)) & (1 << 4)))
  77. {
  78. ch = readl((volatile void *)(uart->hw_base));
  79. }
  80. return ch;
  81. }
  82. static const struct rt_uart_ops _uart_ops =
  83. {
  84. uart_configure,
  85. uart_control,
  86. uart_putc,
  87. uart_getc,
  88. };
  89. static void rt_hw_uart_isr(int irqno, void *param)
  90. {
  91. struct rt_serial_device *serial = (struct rt_serial_device*)param;
  92. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  93. }
  94. #ifdef RT_USING_UART0
  95. /* UART device driver structure */
  96. static struct hw_uart_device _uart0_device =
  97. {
  98. PL011_UART0_BASE,
  99. PL011_UART0_IRQNUM,
  100. };
  101. static struct rt_serial_device _serial0;
  102. #endif
  103. int rt_hw_uart_init(void)
  104. {
  105. struct hw_uart_device *uart;
  106. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  107. #ifdef RT_USING_UART0
  108. uart = &_uart0_device;
  109. _serial0.ops = &_uart_ops;
  110. _serial0.config = config;
  111. /* register UART1 device */
  112. rt_hw_serial_register(&_serial0, "uart0",
  113. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  114. uart);
  115. rt_hw_interrupt_install(uart->irqno, rt_hw_uart_isr, &_serial0, "uart0");
  116. #endif
  117. return 0;
  118. }