lpc32xx_hsuart.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2011-2015 Vladimir Zapolskiy <vz@mleia.com>
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <serial.h>
  8. #include <dm/platform_data/lpc32xx_hsuart.h>
  9. #include <asm/arch/uart.h>
  10. #include <linux/compiler.h>
  11. struct lpc32xx_hsuart_priv {
  12. struct hsuart_regs *hsuart;
  13. };
  14. static int lpc32xx_serial_setbrg(struct udevice *dev, int baudrate)
  15. {
  16. struct lpc32xx_hsuart_priv *priv = dev_get_priv(dev);
  17. struct hsuart_regs *hsuart = priv->hsuart;
  18. u32 div;
  19. /* UART rate = PERIPH_CLK / ((HSU_RATE + 1) x 14) */
  20. div = (get_serial_clock() / 14 + baudrate / 2) / baudrate - 1;
  21. if (div > 255)
  22. div = 255;
  23. writel(div, &hsuart->rate);
  24. return 0;
  25. }
  26. static int lpc32xx_serial_getc(struct udevice *dev)
  27. {
  28. struct lpc32xx_hsuart_priv *priv = dev_get_priv(dev);
  29. struct hsuart_regs *hsuart = priv->hsuart;
  30. if (!(readl(&hsuart->level) & HSUART_LEVEL_RX))
  31. return -EAGAIN;
  32. return readl(&hsuart->rx) & HSUART_RX_DATA;
  33. }
  34. static int lpc32xx_serial_putc(struct udevice *dev, const char c)
  35. {
  36. struct lpc32xx_hsuart_priv *priv = dev_get_priv(dev);
  37. struct hsuart_regs *hsuart = priv->hsuart;
  38. /* Wait for empty FIFO */
  39. if (readl(&hsuart->level) & HSUART_LEVEL_TX)
  40. return -EAGAIN;
  41. writel(c, &hsuart->tx);
  42. return 0;
  43. }
  44. static int lpc32xx_serial_pending(struct udevice *dev, bool input)
  45. {
  46. struct lpc32xx_hsuart_priv *priv = dev_get_priv(dev);
  47. struct hsuart_regs *hsuart = priv->hsuart;
  48. if (input) {
  49. if (readl(&hsuart->level) & HSUART_LEVEL_RX)
  50. return 1;
  51. } else {
  52. if (readl(&hsuart->level) & HSUART_LEVEL_TX)
  53. return 1;
  54. }
  55. return 0;
  56. }
  57. static int lpc32xx_serial_init(struct hsuart_regs *hsuart)
  58. {
  59. /* Disable hardware RTS and CTS flow control, set up RX and TX FIFO */
  60. writel(HSUART_CTRL_TMO_16 | HSUART_CTRL_HSU_OFFSET(20) |
  61. HSUART_CTRL_HSU_RX_TRIG_32 | HSUART_CTRL_HSU_TX_TRIG_0,
  62. &hsuart->ctrl);
  63. return 0;
  64. }
  65. static int lpc32xx_hsuart_probe(struct udevice *dev)
  66. {
  67. struct lpc32xx_hsuart_platdata *platdata = dev_get_platdata(dev);
  68. struct lpc32xx_hsuart_priv *priv = dev_get_priv(dev);
  69. priv->hsuart = (struct hsuart_regs *)platdata->base;
  70. lpc32xx_serial_init(priv->hsuart);
  71. return 0;
  72. }
  73. static const struct dm_serial_ops lpc32xx_hsuart_ops = {
  74. .setbrg = lpc32xx_serial_setbrg,
  75. .getc = lpc32xx_serial_getc,
  76. .putc = lpc32xx_serial_putc,
  77. .pending = lpc32xx_serial_pending,
  78. };
  79. U_BOOT_DRIVER(lpc32xx_hsuart) = {
  80. .name = "lpc32xx_hsuart",
  81. .id = UCLASS_SERIAL,
  82. .probe = lpc32xx_hsuart_probe,
  83. .ops = &lpc32xx_hsuart_ops,
  84. .priv_auto_alloc_size = sizeof(struct lpc32xx_hsuart_priv),
  85. .flags = DM_FLAG_PRE_RELOC,
  86. };