shakti-uart.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2020 Vijai Kumar K <vijai@behindbytes.com>
  5. */
  6. #include <sbi/riscv_io.h>
  7. #include <sbi/sbi_console.h>
  8. #include <sbi_utils/serial/shakti-uart.h>
  9. #define REG_BAUD 0x00
  10. #define REG_TX 0x04
  11. #define REG_RX 0x08
  12. #define REG_STATUS 0x0C
  13. #define REG_DELAY 0x10
  14. #define REG_CONTROL 0x14
  15. #define REG_INT_EN 0x18
  16. #define REG_IQ_CYCLES 0x1C
  17. #define REG_RX_THRES 0x20
  18. #define UART_TX_FULL 0x2
  19. #define UART_RX_NOT_EMPTY 0x4
  20. #define UART_RX_FULL 0x8
  21. static volatile char *uart_base;
  22. static void shakti_uart_putc(char ch)
  23. {
  24. while ((readb(uart_base + REG_STATUS) & UART_TX_FULL))
  25. ;
  26. writeb(ch, uart_base + REG_TX);
  27. }
  28. static int shakti_uart_getc(void)
  29. {
  30. if (readb(uart_base + REG_STATUS) & UART_RX_NOT_EMPTY)
  31. return readb(uart_base + REG_RX);
  32. return -1;
  33. }
  34. static struct sbi_console_device shakti_console = {
  35. .name = "shakti_uart",
  36. .console_putc = shakti_uart_putc,
  37. .console_getc = shakti_uart_getc
  38. };
  39. int shakti_uart_init(unsigned long base, u32 in_freq, u32 baudrate)
  40. {
  41. uart_base = (volatile char *)base;
  42. u16 baud;
  43. if (baudrate) {
  44. baud = (u16)(in_freq / (16 * baudrate));
  45. writew(baud, uart_base + REG_BAUD);
  46. }
  47. sbi_console_set_device(&shakti_console);
  48. return 0;
  49. }