serial_uniphier.c 3.4 KB

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