drv_uart.c 3.2 KB

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