serial_meson.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2016 Beniamino Galvani <b.galvani@gmail.com>
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <errno.h>
  8. #include <fdtdec.h>
  9. #include <linux/bitops.h>
  10. #include <linux/compiler.h>
  11. #include <serial.h>
  12. struct meson_uart {
  13. u32 wfifo;
  14. u32 rfifo;
  15. u32 control;
  16. u32 status;
  17. u32 misc;
  18. };
  19. struct meson_serial_platdata {
  20. struct meson_uart *reg;
  21. };
  22. /* AML_UART_STATUS bits */
  23. #define AML_UART_PARITY_ERR BIT(16)
  24. #define AML_UART_FRAME_ERR BIT(17)
  25. #define AML_UART_TX_FIFO_WERR BIT(18)
  26. #define AML_UART_RX_EMPTY BIT(20)
  27. #define AML_UART_TX_FULL BIT(21)
  28. #define AML_UART_TX_EMPTY BIT(22)
  29. #define AML_UART_XMIT_BUSY BIT(25)
  30. #define AML_UART_ERR (AML_UART_PARITY_ERR | \
  31. AML_UART_FRAME_ERR | \
  32. AML_UART_TX_FIFO_WERR)
  33. /* AML_UART_CONTROL bits */
  34. #define AML_UART_TX_EN BIT(12)
  35. #define AML_UART_RX_EN BIT(13)
  36. #define AML_UART_TX_RST BIT(22)
  37. #define AML_UART_RX_RST BIT(23)
  38. #define AML_UART_CLR_ERR BIT(24)
  39. static void meson_serial_init(struct meson_uart *uart)
  40. {
  41. u32 val;
  42. val = readl(&uart->control);
  43. val |= (AML_UART_RX_RST | AML_UART_TX_RST | AML_UART_CLR_ERR);
  44. writel(val, &uart->control);
  45. val &= ~(AML_UART_RX_RST | AML_UART_TX_RST | AML_UART_CLR_ERR);
  46. writel(val, &uart->control);
  47. val |= (AML_UART_RX_EN | AML_UART_TX_EN);
  48. writel(val, &uart->control);
  49. }
  50. static int meson_serial_probe(struct udevice *dev)
  51. {
  52. struct meson_serial_platdata *plat = dev->platdata;
  53. struct meson_uart *const uart = plat->reg;
  54. meson_serial_init(uart);
  55. return 0;
  56. }
  57. static void meson_serial_rx_error(struct udevice *dev)
  58. {
  59. struct meson_serial_platdata *plat = dev->platdata;
  60. struct meson_uart *const uart = plat->reg;
  61. u32 val = readl(&uart->control);
  62. /* Clear error */
  63. val |= AML_UART_CLR_ERR;
  64. writel(val, &uart->control);
  65. val &= ~AML_UART_CLR_ERR;
  66. writel(val, &uart->control);
  67. /* Remove spurious byte from fifo */
  68. readl(&uart->rfifo);
  69. }
  70. static int meson_serial_getc(struct udevice *dev)
  71. {
  72. struct meson_serial_platdata *plat = dev->platdata;
  73. struct meson_uart *const uart = plat->reg;
  74. uint32_t status = readl(&uart->status);
  75. if (status & AML_UART_RX_EMPTY)
  76. return -EAGAIN;
  77. if (status & AML_UART_ERR) {
  78. meson_serial_rx_error(dev);
  79. return -EIO;
  80. }
  81. return readl(&uart->rfifo) & 0xff;
  82. }
  83. static int meson_serial_putc(struct udevice *dev, const char ch)
  84. {
  85. struct meson_serial_platdata *plat = dev->platdata;
  86. struct meson_uart *const uart = plat->reg;
  87. if (readl(&uart->status) & AML_UART_TX_FULL)
  88. return -EAGAIN;
  89. writel(ch, &uart->wfifo);
  90. return 0;
  91. }
  92. static int meson_serial_pending(struct udevice *dev, bool input)
  93. {
  94. struct meson_serial_platdata *plat = dev->platdata;
  95. struct meson_uart *const uart = plat->reg;
  96. uint32_t status = readl(&uart->status);
  97. if (input) {
  98. if (status & AML_UART_RX_EMPTY)
  99. return false;
  100. /*
  101. * Handle and drop any RX error here to avoid
  102. * returning true here when an error byte is in the FIFO
  103. */
  104. if (status & AML_UART_ERR) {
  105. meson_serial_rx_error(dev);
  106. return false;
  107. }
  108. return true;
  109. } else {
  110. return !(status & AML_UART_TX_FULL);
  111. }
  112. }
  113. static int meson_serial_ofdata_to_platdata(struct udevice *dev)
  114. {
  115. struct meson_serial_platdata *plat = dev->platdata;
  116. fdt_addr_t addr;
  117. addr = dev_read_addr(dev);
  118. if (addr == FDT_ADDR_T_NONE)
  119. return -EINVAL;
  120. plat->reg = (struct meson_uart *)addr;
  121. return 0;
  122. }
  123. static const struct dm_serial_ops meson_serial_ops = {
  124. .putc = meson_serial_putc,
  125. .pending = meson_serial_pending,
  126. .getc = meson_serial_getc,
  127. };
  128. static const struct udevice_id meson_serial_ids[] = {
  129. { .compatible = "amlogic,meson-uart" },
  130. { .compatible = "amlogic,meson-gx-uart" },
  131. { }
  132. };
  133. U_BOOT_DRIVER(serial_meson) = {
  134. .name = "serial_meson",
  135. .id = UCLASS_SERIAL,
  136. .of_match = meson_serial_ids,
  137. .probe = meson_serial_probe,
  138. .ops = &meson_serial_ops,
  139. .ofdata_to_platdata = meson_serial_ofdata_to_platdata,
  140. .platdata_auto_alloc_size = sizeof(struct meson_serial_platdata),
  141. };
  142. #ifdef CONFIG_DEBUG_UART_MESON
  143. #include <debug_uart.h>
  144. static inline void _debug_uart_init(void)
  145. {
  146. }
  147. static inline void _debug_uart_putc(int ch)
  148. {
  149. struct meson_uart *regs = (struct meson_uart *)CONFIG_DEBUG_UART_BASE;
  150. while (readl(&regs->status) & AML_UART_TX_FULL)
  151. ;
  152. writel(ch, &regs->wfifo);
  153. }
  154. DEBUG_UART_FUNCS
  155. #endif