serial.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* GRLIB APBUART Serial controller driver
  2. *
  3. * (C) Copyright 2008
  4. * Daniel Hellstrom, Gaisler Research, daniel@gaisler.com.
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <asm/processor.h>
  10. #include <asm/leon.h>
  11. #include <serial.h>
  12. #include <linux/compiler.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. static int leon2_serial_init(void)
  15. {
  16. LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
  17. LEON2_Uart_regs *regs;
  18. unsigned int tmp;
  19. /* Init LEON2 UART
  20. *
  21. * Set scaler / baud rate
  22. *
  23. * Receiver & transmitter enable
  24. */
  25. #if LEON2_CONSOLE_SELECT == LEON_CONSOLE_UART1
  26. regs = (LEON2_Uart_regs *) & leon2->UART_Channel_1;
  27. #else
  28. regs = (LEON2_Uart_regs *) & leon2->UART_Channel_2;
  29. #endif
  30. regs->UART_Scaler = CONFIG_SYS_LEON2_UART1_SCALER;
  31. /* Let bit 11 be unchanged (debug bit for GRMON) */
  32. tmp = READ_WORD(regs->UART_Control);
  33. regs->UART_Control = ((tmp & LEON2_UART_CTRL_DBG) |
  34. (LEON2_UART1_LOOPBACK_ENABLE << 7) |
  35. (LEON2_UART1_FLOWCTRL_ENABLE << 6) |
  36. (LEON2_UART1_PARITY_ENABLE << 5) |
  37. (LEON2_UART1_ODDPAR_ENABLE << 4) |
  38. LEON2_UART_CTRL_RE | LEON2_UART_CTRL_TE);
  39. return 0;
  40. }
  41. static void leon2_serial_putc_raw(const char c)
  42. {
  43. LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
  44. LEON2_Uart_regs *regs;
  45. #if LEON2_CONSOLE_SELECT == LEON_CONSOLE_UART1
  46. regs = (LEON2_Uart_regs *) & leon2->UART_Channel_1;
  47. #else
  48. regs = (LEON2_Uart_regs *) & leon2->UART_Channel_2;
  49. #endif
  50. /* Wait for last character to go. */
  51. while (!(READ_WORD(regs->UART_Status) & LEON2_UART_STAT_THE)) ;
  52. /* Send data */
  53. regs->UART_Channel = c;
  54. #ifdef LEON_DEBUG
  55. /* Wait for data to be sent */
  56. while (!(READ_WORD(regs->UART_Status) & LEON2_UART_STAT_TSE)) ;
  57. #endif
  58. }
  59. static void leon2_serial_putc(const char c)
  60. {
  61. if (c == '\n')
  62. leon2_serial_putc_raw('\r');
  63. leon2_serial_putc_raw(c);
  64. }
  65. static int leon2_serial_getc(void)
  66. {
  67. LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
  68. LEON2_Uart_regs *regs;
  69. #if LEON2_CONSOLE_SELECT == LEON_CONSOLE_UART1
  70. regs = (LEON2_Uart_regs *) & leon2->UART_Channel_1;
  71. #else
  72. regs = (LEON2_Uart_regs *) & leon2->UART_Channel_2;
  73. #endif
  74. /* Wait for a character to arrive. */
  75. while (!(READ_WORD(regs->UART_Status) & LEON2_UART_STAT_DR)) ;
  76. /* read data */
  77. return READ_WORD(regs->UART_Channel);
  78. }
  79. static int leon2_serial_tstc(void)
  80. {
  81. LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
  82. LEON2_Uart_regs *regs;
  83. #if LEON2_CONSOLE_SELECT == LEON_CONSOLE_UART1
  84. regs = (LEON2_Uart_regs *) & leon2->UART_Channel_1;
  85. #else
  86. regs = (LEON2_Uart_regs *) & leon2->UART_Channel_2;
  87. #endif
  88. return (READ_WORD(regs->UART_Status) & LEON2_UART_STAT_DR);
  89. }
  90. /* set baud rate for uart */
  91. static void leon2_serial_setbrg(void)
  92. {
  93. /* update baud rate settings, read it from gd->baudrate */
  94. unsigned int scaler;
  95. LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
  96. LEON2_Uart_regs *regs;
  97. #if LEON2_CONSOLE_SELECT == LEON_CONSOLE_UART1
  98. regs = (LEON2_Uart_regs *) & leon2->UART_Channel_1;
  99. #else
  100. regs = (LEON2_Uart_regs *) & leon2->UART_Channel_2;
  101. #endif
  102. if (gd->baudrate > 0) {
  103. scaler =
  104. (((CONFIG_SYS_CLK_FREQ * 10) / (gd->baudrate * 8)) -
  105. 5) / 10;
  106. regs->UART_Scaler = scaler;
  107. }
  108. }
  109. static struct serial_device leon2_serial_drv = {
  110. .name = "leon2_serial",
  111. .start = leon2_serial_init,
  112. .stop = NULL,
  113. .setbrg = leon2_serial_setbrg,
  114. .putc = leon2_serial_putc,
  115. .puts = default_serial_puts,
  116. .getc = leon2_serial_getc,
  117. .tstc = leon2_serial_tstc,
  118. };
  119. void leon2_serial_initialize(void)
  120. {
  121. serial_register(&leon2_serial_drv);
  122. }
  123. __weak struct serial_device *default_serial_console(void)
  124. {
  125. return &leon2_serial_drv;
  126. }