serial_meson.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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/kernel.h>
  10. #include <linux/bitops.h>
  11. #include <linux/compiler.h>
  12. #include <serial.h>
  13. #include <clk.h>
  14. struct meson_uart {
  15. u32 wfifo;
  16. u32 rfifo;
  17. u32 control;
  18. u32 status;
  19. u32 misc;
  20. u32 reg5; /* New baud control register */
  21. };
  22. struct meson_serial_plat {
  23. struct meson_uart *reg;
  24. };
  25. /* AML_UART_STATUS bits */
  26. #define AML_UART_PARITY_ERR BIT(16)
  27. #define AML_UART_FRAME_ERR BIT(17)
  28. #define AML_UART_TX_FIFO_WERR BIT(18)
  29. #define AML_UART_RX_EMPTY BIT(20)
  30. #define AML_UART_TX_FULL BIT(21)
  31. #define AML_UART_TX_EMPTY BIT(22)
  32. #define AML_UART_XMIT_BUSY BIT(25)
  33. #define AML_UART_ERR (AML_UART_PARITY_ERR | \
  34. AML_UART_FRAME_ERR | \
  35. AML_UART_TX_FIFO_WERR)
  36. /* AML_UART_CONTROL bits */
  37. #define AML_UART_TX_EN BIT(12)
  38. #define AML_UART_RX_EN BIT(13)
  39. #define AML_UART_TX_RST BIT(22)
  40. #define AML_UART_RX_RST BIT(23)
  41. #define AML_UART_CLR_ERR BIT(24)
  42. /* AML_UART_REG5 bits */
  43. #define AML_UART_REG5_XTAL_DIV2 BIT(27)
  44. #define AML_UART_REG5_XTAL_CLK_SEL BIT(26) /* default 0 (div by 3), 1 for no div */
  45. #define AML_UART_REG5_USE_XTAL_CLK BIT(24) /* default 1 (use crystal as clock source) */
  46. #define AML_UART_REG5_USE_NEW_BAUD BIT(23) /* default 1 (use new baud rate register) */
  47. #define AML_UART_REG5_BAUD_MASK 0x7fffff
  48. static u32 meson_calc_baud_divisor(ulong src_rate, u32 baud)
  49. {
  50. /*
  51. * Usually src_rate is 24 MHz (from crystal) as clock source for serial
  52. * device. Since 8 Mb/s is the maximum supported baud rate, use div by 3
  53. * to derive baud rate. This choice is used also in meson_serial_setbrg.
  54. */
  55. return DIV_ROUND_CLOSEST(src_rate / 3, baud) - 1;
  56. }
  57. static void meson_serial_set_baud(struct meson_uart *uart, ulong src_rate, u32 baud)
  58. {
  59. /*
  60. * Set crystal divided by 3 (regardless of device tree clock property)
  61. * as clock source and the corresponding divisor to approximate baud
  62. */
  63. u32 divisor = meson_calc_baud_divisor(src_rate, baud);
  64. u32 val = AML_UART_REG5_USE_XTAL_CLK | AML_UART_REG5_USE_NEW_BAUD |
  65. (divisor & AML_UART_REG5_BAUD_MASK);
  66. writel(val, &uart->reg5);
  67. }
  68. static void meson_serial_init(struct meson_uart *uart)
  69. {
  70. u32 val;
  71. val = readl(&uart->control);
  72. val |= (AML_UART_RX_RST | AML_UART_TX_RST | AML_UART_CLR_ERR);
  73. writel(val, &uart->control);
  74. val &= ~(AML_UART_RX_RST | AML_UART_TX_RST | AML_UART_CLR_ERR);
  75. writel(val, &uart->control);
  76. val |= (AML_UART_RX_EN | AML_UART_TX_EN);
  77. writel(val, &uart->control);
  78. }
  79. static int meson_serial_probe(struct udevice *dev)
  80. {
  81. struct meson_serial_plat *plat = dev_get_plat(dev);
  82. struct meson_uart *const uart = plat->reg;
  83. struct clk per_clk;
  84. int ret = clk_get_by_name(dev, "baud", &per_clk);
  85. if (ret)
  86. return ret;
  87. ulong rate = clk_get_rate(&per_clk);
  88. meson_serial_set_baud(uart, rate, CONFIG_BAUDRATE);
  89. meson_serial_init(uart);
  90. return 0;
  91. }
  92. static void meson_serial_rx_error(struct udevice *dev)
  93. {
  94. struct meson_serial_plat *plat = dev_get_plat(dev);
  95. struct meson_uart *const uart = plat->reg;
  96. u32 val = readl(&uart->control);
  97. /* Clear error */
  98. val |= AML_UART_CLR_ERR;
  99. writel(val, &uart->control);
  100. val &= ~AML_UART_CLR_ERR;
  101. writel(val, &uart->control);
  102. /* Remove spurious byte from fifo */
  103. readl(&uart->rfifo);
  104. }
  105. static int meson_serial_getc(struct udevice *dev)
  106. {
  107. struct meson_serial_plat *plat = dev_get_plat(dev);
  108. struct meson_uart *const uart = plat->reg;
  109. uint32_t status = readl(&uart->status);
  110. if (status & AML_UART_RX_EMPTY)
  111. return -EAGAIN;
  112. if (status & AML_UART_ERR) {
  113. meson_serial_rx_error(dev);
  114. return -EIO;
  115. }
  116. return readl(&uart->rfifo) & 0xff;
  117. }
  118. static int meson_serial_putc(struct udevice *dev, const char ch)
  119. {
  120. struct meson_serial_plat *plat = dev_get_plat(dev);
  121. struct meson_uart *const uart = plat->reg;
  122. if (readl(&uart->status) & AML_UART_TX_FULL)
  123. return -EAGAIN;
  124. writel(ch, &uart->wfifo);
  125. return 0;
  126. }
  127. static int meson_serial_setbrg(struct udevice *dev, const int baud)
  128. {
  129. /*
  130. * Change device baud rate if baud is reasonable (considering a 23 bit
  131. * counter with an 8 MHz clock input) and the actual baud
  132. * rate is within 2% of the requested value (2% is arbitrary).
  133. */
  134. if (baud < 1 || baud > 8000000)
  135. return -EINVAL;
  136. struct meson_serial_plat *const plat = dev_get_plat(dev);
  137. struct meson_uart *const uart = plat->reg;
  138. struct clk per_clk;
  139. int ret = clk_get_by_name(dev, "baud", &per_clk);
  140. if (ret)
  141. return ret;
  142. ulong rate = clk_get_rate(&per_clk);
  143. u32 divisor = meson_calc_baud_divisor(rate, baud);
  144. u32 calc_baud = (rate / 3) / (divisor + 1);
  145. u32 calc_err = baud > calc_baud ? baud - calc_baud : calc_baud - baud;
  146. if (((calc_err * 100) / baud) > 2)
  147. return -EINVAL;
  148. meson_serial_set_baud(uart, rate, baud);
  149. return 0;
  150. }
  151. static int meson_serial_pending(struct udevice *dev, bool input)
  152. {
  153. struct meson_serial_plat *plat = dev_get_plat(dev);
  154. struct meson_uart *const uart = plat->reg;
  155. uint32_t status = readl(&uart->status);
  156. if (input) {
  157. if (status & AML_UART_RX_EMPTY)
  158. return false;
  159. /*
  160. * Handle and drop any RX error here to avoid
  161. * returning true here when an error byte is in the FIFO
  162. */
  163. if (status & AML_UART_ERR) {
  164. meson_serial_rx_error(dev);
  165. return false;
  166. }
  167. return true;
  168. } else {
  169. if (status & AML_UART_TX_EMPTY)
  170. return false;
  171. return true;
  172. }
  173. }
  174. static int meson_serial_of_to_plat(struct udevice *dev)
  175. {
  176. struct meson_serial_plat *plat = dev_get_plat(dev);
  177. fdt_addr_t addr;
  178. addr = dev_read_addr(dev);
  179. if (addr == FDT_ADDR_T_NONE)
  180. return -EINVAL;
  181. plat->reg = (struct meson_uart *)addr;
  182. return 0;
  183. }
  184. static const struct dm_serial_ops meson_serial_ops = {
  185. .putc = meson_serial_putc,
  186. .pending = meson_serial_pending,
  187. .getc = meson_serial_getc,
  188. .setbrg = meson_serial_setbrg,
  189. };
  190. static const struct udevice_id meson_serial_ids[] = {
  191. { .compatible = "amlogic,meson-uart" },
  192. { .compatible = "amlogic,meson-gx-uart" },
  193. { }
  194. };
  195. U_BOOT_DRIVER(serial_meson) = {
  196. .name = "serial_meson",
  197. .id = UCLASS_SERIAL,
  198. .of_match = meson_serial_ids,
  199. .probe = meson_serial_probe,
  200. .ops = &meson_serial_ops,
  201. .of_to_plat = meson_serial_of_to_plat,
  202. .plat_auto = sizeof(struct meson_serial_plat),
  203. };
  204. #ifdef CONFIG_DEBUG_UART_MESON
  205. #include <debug_uart.h>
  206. static inline void _debug_uart_init(void)
  207. {
  208. }
  209. static inline void _debug_uart_putc(int ch)
  210. {
  211. struct meson_uart *regs = (struct meson_uart *)CONFIG_VAL(DEBUG_UART_BASE);
  212. while (readl(&regs->status) & AML_UART_TX_FULL)
  213. ;
  214. writel(ch, &regs->wfifo);
  215. }
  216. DEBUG_UART_FUNCS
  217. #endif