serial_xuartlite.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2008 - 2015 Michal Simek <monstr@monstr.eu>
  4. * Clean driver and add xilinx constant from header file
  5. *
  6. * (C) Copyright 2004 Atmark Techno, Inc.
  7. * Yasushi SHOJI <yashi@atmark-techno.com>
  8. */
  9. #include <config.h>
  10. #include <common.h>
  11. #include <dm.h>
  12. #include <asm/io.h>
  13. #include <linux/bitops.h>
  14. #include <linux/compiler.h>
  15. #include <serial.h>
  16. #define SR_TX_FIFO_FULL BIT(3) /* transmit FIFO full */
  17. #define SR_TX_FIFO_EMPTY BIT(2) /* transmit FIFO empty */
  18. #define SR_RX_FIFO_VALID_DATA BIT(0) /* data in receive FIFO */
  19. #define SR_RX_FIFO_FULL BIT(1) /* receive FIFO full */
  20. #define ULITE_CONTROL_RST_TX 0x01
  21. #define ULITE_CONTROL_RST_RX 0x02
  22. struct uartlite {
  23. unsigned int rx_fifo;
  24. unsigned int tx_fifo;
  25. unsigned int status;
  26. unsigned int control;
  27. };
  28. struct uartlite_platdata {
  29. struct uartlite *regs;
  30. };
  31. static int uartlite_serial_putc(struct udevice *dev, const char ch)
  32. {
  33. struct uartlite_platdata *plat = dev_get_platdata(dev);
  34. struct uartlite *regs = plat->regs;
  35. if (in_be32(&regs->status) & SR_TX_FIFO_FULL)
  36. return -EAGAIN;
  37. out_be32(&regs->tx_fifo, ch & 0xff);
  38. return 0;
  39. }
  40. static int uartlite_serial_getc(struct udevice *dev)
  41. {
  42. struct uartlite_platdata *plat = dev_get_platdata(dev);
  43. struct uartlite *regs = plat->regs;
  44. if (!(in_be32(&regs->status) & SR_RX_FIFO_VALID_DATA))
  45. return -EAGAIN;
  46. return in_be32(&regs->rx_fifo) & 0xff;
  47. }
  48. static int uartlite_serial_pending(struct udevice *dev, bool input)
  49. {
  50. struct uartlite_platdata *plat = dev_get_platdata(dev);
  51. struct uartlite *regs = plat->regs;
  52. if (input)
  53. return in_be32(&regs->status) & SR_RX_FIFO_VALID_DATA;
  54. return !(in_be32(&regs->status) & SR_TX_FIFO_EMPTY);
  55. }
  56. static int uartlite_serial_probe(struct udevice *dev)
  57. {
  58. struct uartlite_platdata *plat = dev_get_platdata(dev);
  59. struct uartlite *regs = plat->regs;
  60. out_be32(&regs->control, 0);
  61. out_be32(&regs->control, ULITE_CONTROL_RST_RX | ULITE_CONTROL_RST_TX);
  62. in_be32(&regs->control);
  63. return 0;
  64. }
  65. static int uartlite_serial_ofdata_to_platdata(struct udevice *dev)
  66. {
  67. struct uartlite_platdata *plat = dev_get_platdata(dev);
  68. plat->regs = dev_read_addr_ptr(dev);
  69. return 0;
  70. }
  71. static const struct dm_serial_ops uartlite_serial_ops = {
  72. .putc = uartlite_serial_putc,
  73. .pending = uartlite_serial_pending,
  74. .getc = uartlite_serial_getc,
  75. };
  76. static const struct udevice_id uartlite_serial_ids[] = {
  77. { .compatible = "xlnx,opb-uartlite-1.00.b", },
  78. { .compatible = "xlnx,xps-uartlite-1.00.a" },
  79. { }
  80. };
  81. U_BOOT_DRIVER(serial_uartlite) = {
  82. .name = "serial_uartlite",
  83. .id = UCLASS_SERIAL,
  84. .of_match = uartlite_serial_ids,
  85. .ofdata_to_platdata = uartlite_serial_ofdata_to_platdata,
  86. .platdata_auto_alloc_size = sizeof(struct uartlite_platdata),
  87. .probe = uartlite_serial_probe,
  88. .ops = &uartlite_serial_ops,
  89. };
  90. #ifdef CONFIG_DEBUG_UART_UARTLITE
  91. #include <debug_uart.h>
  92. static inline void _debug_uart_init(void)
  93. {
  94. struct uartlite *regs = (struct uartlite *)CONFIG_DEBUG_UART_BASE;
  95. out_be32(&regs->control, 0);
  96. out_be32(&regs->control, ULITE_CONTROL_RST_RX | ULITE_CONTROL_RST_TX);
  97. in_be32(&regs->control);
  98. }
  99. static inline void _debug_uart_putc(int ch)
  100. {
  101. struct uartlite *regs = (struct uartlite *)CONFIG_DEBUG_UART_BASE;
  102. while (in_be32(&regs->status) & SR_TX_FIFO_FULL)
  103. ;
  104. out_be32(&regs->tx_fifo, ch & 0xff);
  105. }
  106. DEBUG_UART_FUNCS
  107. #endif