serial_meson.c 3.5 KB

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