serial_uniphier.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (C) 2012-2015 Masahiro Yamada <yamada.masahiro@socionext.com>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <linux/io.h>
  7. #include <linux/serial_reg.h>
  8. #include <asm/errno.h>
  9. #include <dm/device.h>
  10. #include <mapmem.h>
  11. #include <serial.h>
  12. #include <fdtdec.h>
  13. /*
  14. * Note: Register map is slightly different from that of 16550.
  15. */
  16. struct uniphier_serial {
  17. u32 rx; /* In: Receive buffer */
  18. #define tx rx /* Out: Transmit buffer */
  19. u32 ier; /* Interrupt Enable Register */
  20. u32 iir; /* In: Interrupt ID Register */
  21. u32 char_fcr; /* Charactor / FIFO Control Register */
  22. u32 lcr_mcr; /* Line/Modem Control Register */
  23. #define LCR_SHIFT 8
  24. #define LCR_MASK (0xff << (LCR_SHIFT))
  25. u32 lsr; /* In: Line Status Register */
  26. u32 msr; /* In: Modem Status Register */
  27. u32 __rsv0;
  28. u32 __rsv1;
  29. u32 dlr; /* Divisor Latch Register */
  30. };
  31. struct uniphier_serial_private_data {
  32. struct uniphier_serial __iomem *membase;
  33. unsigned int uartclk;
  34. };
  35. #define uniphier_serial_port(dev) \
  36. ((struct uniphier_serial_private_data *)dev_get_priv(dev))->membase
  37. static int uniphier_serial_setbrg(struct udevice *dev, int baudrate)
  38. {
  39. struct uniphier_serial_private_data *priv = dev_get_priv(dev);
  40. struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
  41. const unsigned int mode_x_div = 16;
  42. unsigned int divisor;
  43. divisor = DIV_ROUND_CLOSEST(priv->uartclk, mode_x_div * baudrate);
  44. writel(divisor, &port->dlr);
  45. return 0;
  46. }
  47. static int uniphier_serial_getc(struct udevice *dev)
  48. {
  49. struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
  50. if (!(readl(&port->lsr) & UART_LSR_DR))
  51. return -EAGAIN;
  52. return readl(&port->rx);
  53. }
  54. static int uniphier_serial_putc(struct udevice *dev, const char c)
  55. {
  56. struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
  57. if (!(readl(&port->lsr) & UART_LSR_THRE))
  58. return -EAGAIN;
  59. writel(c, &port->tx);
  60. return 0;
  61. }
  62. static int uniphier_serial_pending(struct udevice *dev, bool input)
  63. {
  64. struct uniphier_serial __iomem *port = uniphier_serial_port(dev);
  65. if (input)
  66. return readl(&port->lsr) & UART_LSR_DR;
  67. else
  68. return !(readl(&port->lsr) & UART_LSR_THRE);
  69. }
  70. static int uniphier_serial_probe(struct udevice *dev)
  71. {
  72. DECLARE_GLOBAL_DATA_PTR;
  73. struct uniphier_serial_private_data *priv = dev_get_priv(dev);
  74. struct uniphier_serial __iomem *port;
  75. fdt_addr_t base;
  76. fdt_size_t size;
  77. u32 tmp;
  78. base = fdtdec_get_addr_size(gd->fdt_blob, dev->of_offset, "reg", &size);
  79. port = map_sysmem(base, size);
  80. if (!port)
  81. return -ENOMEM;
  82. priv->membase = port;
  83. priv->uartclk = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
  84. "clock-frequency", 0);
  85. tmp = readl(&port->lcr_mcr);
  86. tmp &= ~LCR_MASK;
  87. tmp |= UART_LCR_WLEN8 << LCR_SHIFT;
  88. writel(tmp, &port->lcr_mcr);
  89. return 0;
  90. }
  91. static int uniphier_serial_remove(struct udevice *dev)
  92. {
  93. unmap_sysmem(uniphier_serial_port(dev));
  94. return 0;
  95. }
  96. static const struct udevice_id uniphier_uart_of_match[] = {
  97. { .compatible = "socionext,uniphier-uart" },
  98. { /* sentinel */ }
  99. };
  100. static const struct dm_serial_ops uniphier_serial_ops = {
  101. .setbrg = uniphier_serial_setbrg,
  102. .getc = uniphier_serial_getc,
  103. .putc = uniphier_serial_putc,
  104. .pending = uniphier_serial_pending,
  105. };
  106. U_BOOT_DRIVER(uniphier_serial) = {
  107. .name = "uniphier-uart",
  108. .id = UCLASS_SERIAL,
  109. .of_match = uniphier_uart_of_match,
  110. .probe = uniphier_serial_probe,
  111. .remove = uniphier_serial_remove,
  112. .priv_auto_alloc_size = sizeof(struct uniphier_serial_private_data),
  113. .ops = &uniphier_serial_ops,
  114. };