xlnx-uartlite.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2022 Western Digital Corporation or its affiliates.
  5. *
  6. * Authors:
  7. * Alistair Francis <alistair.francis@wdc.com>
  8. */
  9. #include <sbi/riscv_io.h>
  10. #include <sbi/sbi_console.h>
  11. #include <sbi_utils/serial/xlnx_uartlite.h>
  12. /* clang-format off */
  13. #define UART_RX_OFFSET 0x00
  14. #define UART_TX_OFFSET 0x04
  15. #define UART_STATUS_OFFSET 0x08
  16. # define UART_STATUS_RXVALID 0x01
  17. # define UART_STATUS_RXFULL 0x02
  18. # define UART_STATUS_TXEMPTY 0x04
  19. # define UART_STATUS_TXFULL 0x08
  20. # define UART_STATUS_IE 0x10
  21. # define UART_STATUS_OVERRUN 0x20
  22. # define UART_STATUS_FRAME 0x40
  23. # define UART_STATUS_PARITY 0x80
  24. #define UART_CTRL_OFFSET 0x0C
  25. # define UART_CTRL_RST_TX 0x01
  26. # define UART_CTRL_RST_RX 0x02
  27. # define UART_CTRL_IE 0x10
  28. /* clang-format on */
  29. static volatile char *xlnx_uartlite_base;
  30. static void xlnx_uartlite_putc(char ch)
  31. {
  32. while((readb(xlnx_uartlite_base + UART_STATUS_OFFSET) & UART_STATUS_TXFULL))
  33. ;
  34. writeb(ch, xlnx_uartlite_base + UART_TX_OFFSET);
  35. }
  36. static int xlnx_uartlite_getc(void)
  37. {
  38. u16 status = readb(xlnx_uartlite_base + UART_STATUS_OFFSET);
  39. if (status & UART_STATUS_RXVALID)
  40. return readb(xlnx_uartlite_base + UART_RX_OFFSET);
  41. return -1;
  42. }
  43. static struct sbi_console_device xlnx_uartlite_console = {
  44. .name = "xlnx-uartlite",
  45. .console_putc = xlnx_uartlite_putc,
  46. .console_getc = xlnx_uartlite_getc
  47. };
  48. int xlnx_uartlite_init(unsigned long base)
  49. {
  50. xlnx_uartlite_base = (volatile char *)base;
  51. sbi_console_set_device(&xlnx_uartlite_console);
  52. return 0;
  53. }