serial_mxs.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2023 Marek Vasut <marex@denx.de>
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <malloc.h>
  8. #include <serial.h>
  9. #include <wait_bit.h>
  10. #define SET_REG 0x4
  11. #define CLR_REG 0x8
  12. #define AUART_CTRL0 0x00
  13. #define AUART_CTRL1 0x10
  14. #define AUART_CTRL2 0x20
  15. #define AUART_LINECTRL 0x30
  16. #define AUART_INTR 0x50
  17. #define AUART_DATA 0x60
  18. #define AUART_STAT 0x70
  19. #define AUART_CTRL0_SFTRST BIT(31)
  20. #define AUART_CTRL0_CLKGATE BIT(30)
  21. #define AUART_CTRL2_UARTEN BIT(0)
  22. #define AUART_LINECTRL_BAUD_DIVINT(v) (((v) & 0xffff) << 16)
  23. #define AUART_LINECTRL_BAUD_DIVFRAC(v) (((v) & 0x3f) << 8)
  24. #define AUART_LINECTRL_WLEN(v) ((((v) - 5) & 0x3) << 5)
  25. #define AUART_STAT_TXFE BIT(27)
  26. #define AUART_STAT_TXFF BIT(25)
  27. #define AUART_STAT_RXFE BIT(24)
  28. #define AUART_CLK 24000000
  29. struct mxs_auart_uart_priv {
  30. void __iomem *base;
  31. };
  32. static int mxs_auart_uart_setbrg(struct udevice *dev, int baudrate)
  33. {
  34. struct mxs_auart_uart_priv *priv = dev_get_priv(dev);
  35. u32 div;
  36. writel(AUART_CTRL0_CLKGATE, priv->base + AUART_CTRL0 + CLR_REG);
  37. writel(AUART_CTRL0_SFTRST, priv->base + AUART_CTRL0 + CLR_REG);
  38. writel(AUART_CTRL2_UARTEN, priv->base + AUART_CTRL2 + SET_REG);
  39. writel(0, priv->base + AUART_INTR);
  40. div = DIV_ROUND_CLOSEST(AUART_CLK * 32, baudrate);
  41. /* Disable FIFO, baudrate, 8N1. */
  42. writel(AUART_LINECTRL_BAUD_DIVFRAC(div & 0x3F) |
  43. AUART_LINECTRL_BAUD_DIVINT(div >> 6) |
  44. AUART_LINECTRL_WLEN(8),
  45. priv->base + AUART_LINECTRL);
  46. return 0;
  47. }
  48. static int mxs_auart_uart_pending(struct udevice *dev, bool input)
  49. {
  50. struct mxs_auart_uart_priv *priv = dev_get_priv(dev);
  51. u32 stat = readl(priv->base + AUART_STAT);
  52. if (input)
  53. return !(stat & AUART_STAT_RXFE);
  54. return !!(stat & AUART_STAT_TXFE);
  55. }
  56. static int mxs_auart_uart_putc(struct udevice *dev, const char ch)
  57. {
  58. struct mxs_auart_uart_priv *priv = dev_get_priv(dev);
  59. u32 stat = readl(priv->base + AUART_STAT);
  60. if (stat & AUART_STAT_TXFF)
  61. return -EAGAIN;
  62. writel(ch, priv->base + AUART_DATA);
  63. return 0;
  64. }
  65. static int mxs_auart_uart_getc(struct udevice *dev)
  66. {
  67. struct mxs_auart_uart_priv *priv = dev_get_priv(dev);
  68. if (!mxs_auart_uart_pending(dev, true))
  69. return -EAGAIN;
  70. return readl(priv->base + AUART_DATA) & 0xff;
  71. }
  72. static int mxs_auart_uart_probe(struct udevice *dev)
  73. {
  74. struct mxs_auart_uart_priv *priv = dev_get_priv(dev);
  75. priv->base = dev_read_addr_ptr(dev);
  76. if (!priv->base)
  77. return -EINVAL;
  78. return mxs_auart_uart_setbrg(dev, CONFIG_BAUDRATE);
  79. }
  80. static const struct dm_serial_ops mxs_auart_uart_ops = {
  81. .putc = mxs_auart_uart_putc,
  82. .pending = mxs_auart_uart_pending,
  83. .getc = mxs_auart_uart_getc,
  84. .setbrg = mxs_auart_uart_setbrg,
  85. };
  86. static const struct udevice_id mxs_auart_uart_ids[] = {
  87. { .compatible = "fsl,imx23-auart", },
  88. { .compatible = "fsl,imx28-auart", },
  89. { /* sentinel */ }
  90. };
  91. U_BOOT_DRIVER(mxs_auart_serial) = {
  92. .name = "mxs-auart",
  93. .id = UCLASS_SERIAL,
  94. .of_match = mxs_auart_uart_ids,
  95. .probe = mxs_auart_uart_probe,
  96. .ops = &mxs_auart_uart_ops,
  97. .priv_auto = sizeof(struct mxs_auart_uart_priv),
  98. };