serial_xuartlite.c 3.3 KB

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