serial.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* GRLIB APBUART Serial controller driver
  2. *
  3. * (C) Copyright 2008
  4. * Daniel Hellstrom, Gaisler Research, daniel@gaisler.com.
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. *
  24. */
  25. #include <common.h>
  26. #include <asm/processor.h>
  27. #include <asm/leon.h>
  28. DECLARE_GLOBAL_DATA_PTR;
  29. /* Force cache miss each time a serial controller reg is read */
  30. #define CACHE_BYPASS 1
  31. #ifdef CACHE_BYPASS
  32. #define READ_BYTE(var) SPARC_NOCACHE_READ_BYTE((unsigned int)&(var))
  33. #define READ_HWORD(var) SPARC_NOCACHE_READ_HWORD((unsigned int)&(var))
  34. #define READ_WORD(var) SPARC_NOCACHE_READ((unsigned int)&(var))
  35. #define READ_DWORD(var) SPARC_NOCACHE_READ_DWORD((unsigned int)&(var))
  36. #endif
  37. int serial_init(void)
  38. {
  39. LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
  40. LEON2_Uart_regs *regs;
  41. unsigned int tmp;
  42. /* Init LEON2 UART
  43. *
  44. * Set scaler / baud rate
  45. *
  46. * Receiver & transmitter enable
  47. */
  48. #if LEON2_CONSOLE_SELECT == LEON_CONSOLE_UART1
  49. regs = (LEON2_Uart_regs *) & leon2->UART_Channel_1;
  50. #else
  51. regs = (LEON2_Uart_regs *) & leon2->UART_Channel_2;
  52. #endif
  53. regs->UART_Scaler = CFG_LEON2_UART1_SCALER;
  54. /* Let bit 11 be unchanged (debug bit for GRMON) */
  55. tmp = READ_WORD(regs->UART_Control);
  56. regs->UART_Control = ((tmp & LEON2_UART_CTRL_DBG) |
  57. (LEON2_UART1_LOOPBACK_ENABLE << 7) |
  58. (LEON2_UART1_FLOWCTRL_ENABLE << 6) |
  59. (LEON2_UART1_PARITY_ENABLE << 5) |
  60. (LEON2_UART1_ODDPAR_ENABLE << 4) |
  61. LEON2_UART_CTRL_RE | LEON2_UART_CTRL_TE);
  62. return 0;
  63. }
  64. void serial_putc(const char c)
  65. {
  66. if (c == '\n')
  67. serial_putc_raw('\r');
  68. serial_putc_raw(c);
  69. }
  70. void serial_putc_raw(const char c)
  71. {
  72. LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
  73. LEON2_Uart_regs *regs;
  74. #if LEON2_CONSOLE_SELECT == LEON_CONSOLE_UART1
  75. regs = (LEON2_Uart_regs *) & leon2->UART_Channel_1;
  76. #else
  77. regs = (LEON2_Uart_regs *) & leon2->UART_Channel_2;
  78. #endif
  79. /* Wait for last character to go. */
  80. while (!(READ_WORD(regs->UART_Status) & LEON2_UART_STAT_THE)) ;
  81. /* Send data */
  82. regs->UART_Channel = c;
  83. #ifdef LEON_DEBUG
  84. /* Wait for data to be sent */
  85. while (!(READ_WORD(regs->UART_Status) & LEON2_UART_STAT_TSE)) ;
  86. #endif
  87. }
  88. void serial_puts(const char *s)
  89. {
  90. while (*s) {
  91. serial_putc(*s++);
  92. }
  93. }
  94. int serial_getc(void)
  95. {
  96. LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
  97. LEON2_Uart_regs *regs;
  98. #if LEON2_CONSOLE_SELECT == LEON_CONSOLE_UART1
  99. regs = (LEON2_Uart_regs *) & leon2->UART_Channel_1;
  100. #else
  101. regs = (LEON2_Uart_regs *) & leon2->UART_Channel_2;
  102. #endif
  103. /* Wait for a character to arrive. */
  104. while (!(READ_WORD(regs->UART_Status) & LEON2_UART_STAT_DR)) ;
  105. /* read data */
  106. return READ_WORD(regs->UART_Channel);
  107. }
  108. int serial_tstc(void)
  109. {
  110. LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
  111. LEON2_Uart_regs *regs;
  112. #if LEON2_CONSOLE_SELECT == LEON_CONSOLE_UART1
  113. regs = (LEON2_Uart_regs *) & leon2->UART_Channel_1;
  114. #else
  115. regs = (LEON2_Uart_regs *) & leon2->UART_Channel_2;
  116. #endif
  117. return (READ_WORD(regs->UART_Status) & LEON2_UART_STAT_DR);
  118. }
  119. /* set baud rate for uart */
  120. void serial_setbrg(void)
  121. {
  122. /* update baud rate settings, read it from gd->baudrate */
  123. unsigned int scaler;
  124. LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
  125. LEON2_Uart_regs *regs;
  126. #if LEON2_CONSOLE_SELECT == LEON_CONSOLE_UART1
  127. regs = (LEON2_Uart_regs *) & leon2->UART_Channel_1;
  128. #else
  129. regs = (LEON2_Uart_regs *) & leon2->UART_Channel_2;
  130. #endif
  131. if (gd->baudrate > 0) {
  132. scaler =
  133. (((CONFIG_SYS_CLK_FREQ * 10) / (gd->baudrate * 8)) -
  134. 5) / 10;
  135. regs->UART_Scaler = scaler;
  136. }
  137. }