serial_uniphier.c 3.3 KB

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