serial.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. #include <serial.h>
  29. #include <linux/compiler.h>
  30. DECLARE_GLOBAL_DATA_PTR;
  31. static int leon2_serial_init(void)
  32. {
  33. LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
  34. LEON2_Uart_regs *regs;
  35. unsigned int tmp;
  36. /* Init LEON2 UART
  37. *
  38. * Set scaler / baud rate
  39. *
  40. * Receiver & transmitter enable
  41. */
  42. #if LEON2_CONSOLE_SELECT == LEON_CONSOLE_UART1
  43. regs = (LEON2_Uart_regs *) & leon2->UART_Channel_1;
  44. #else
  45. regs = (LEON2_Uart_regs *) & leon2->UART_Channel_2;
  46. #endif
  47. regs->UART_Scaler = CONFIG_SYS_LEON2_UART1_SCALER;
  48. /* Let bit 11 be unchanged (debug bit for GRMON) */
  49. tmp = READ_WORD(regs->UART_Control);
  50. regs->UART_Control = ((tmp & LEON2_UART_CTRL_DBG) |
  51. (LEON2_UART1_LOOPBACK_ENABLE << 7) |
  52. (LEON2_UART1_FLOWCTRL_ENABLE << 6) |
  53. (LEON2_UART1_PARITY_ENABLE << 5) |
  54. (LEON2_UART1_ODDPAR_ENABLE << 4) |
  55. LEON2_UART_CTRL_RE | LEON2_UART_CTRL_TE);
  56. return 0;
  57. }
  58. static void leon2_serial_putc_raw(const char c)
  59. {
  60. LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
  61. LEON2_Uart_regs *regs;
  62. #if LEON2_CONSOLE_SELECT == LEON_CONSOLE_UART1
  63. regs = (LEON2_Uart_regs *) & leon2->UART_Channel_1;
  64. #else
  65. regs = (LEON2_Uart_regs *) & leon2->UART_Channel_2;
  66. #endif
  67. /* Wait for last character to go. */
  68. while (!(READ_WORD(regs->UART_Status) & LEON2_UART_STAT_THE)) ;
  69. /* Send data */
  70. regs->UART_Channel = c;
  71. #ifdef LEON_DEBUG
  72. /* Wait for data to be sent */
  73. while (!(READ_WORD(regs->UART_Status) & LEON2_UART_STAT_TSE)) ;
  74. #endif
  75. }
  76. static void leon2_serial_putc(const char c)
  77. {
  78. if (c == '\n')
  79. leon2_serial_putc_raw('\r');
  80. leon2_serial_putc_raw(c);
  81. }
  82. static int leon2_serial_getc(void)
  83. {
  84. LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
  85. LEON2_Uart_regs *regs;
  86. #if LEON2_CONSOLE_SELECT == LEON_CONSOLE_UART1
  87. regs = (LEON2_Uart_regs *) & leon2->UART_Channel_1;
  88. #else
  89. regs = (LEON2_Uart_regs *) & leon2->UART_Channel_2;
  90. #endif
  91. /* Wait for a character to arrive. */
  92. while (!(READ_WORD(regs->UART_Status) & LEON2_UART_STAT_DR)) ;
  93. /* read data */
  94. return READ_WORD(regs->UART_Channel);
  95. }
  96. static int leon2_serial_tstc(void)
  97. {
  98. LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
  99. LEON2_Uart_regs *regs;
  100. #if LEON2_CONSOLE_SELECT == LEON_CONSOLE_UART1
  101. regs = (LEON2_Uart_regs *) & leon2->UART_Channel_1;
  102. #else
  103. regs = (LEON2_Uart_regs *) & leon2->UART_Channel_2;
  104. #endif
  105. return (READ_WORD(regs->UART_Status) & LEON2_UART_STAT_DR);
  106. }
  107. /* set baud rate for uart */
  108. static void leon2_serial_setbrg(void)
  109. {
  110. /* update baud rate settings, read it from gd->baudrate */
  111. unsigned int scaler;
  112. LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
  113. LEON2_Uart_regs *regs;
  114. #if LEON2_CONSOLE_SELECT == LEON_CONSOLE_UART1
  115. regs = (LEON2_Uart_regs *) & leon2->UART_Channel_1;
  116. #else
  117. regs = (LEON2_Uart_regs *) & leon2->UART_Channel_2;
  118. #endif
  119. if (gd->baudrate > 0) {
  120. scaler =
  121. (((CONFIG_SYS_CLK_FREQ * 10) / (gd->baudrate * 8)) -
  122. 5) / 10;
  123. regs->UART_Scaler = scaler;
  124. }
  125. }
  126. static struct serial_device leon2_serial_drv = {
  127. .name = "leon2_serial",
  128. .start = leon2_serial_init,
  129. .stop = NULL,
  130. .setbrg = leon2_serial_setbrg,
  131. .putc = leon2_serial_putc,
  132. .puts = default_serial_puts,
  133. .getc = leon2_serial_getc,
  134. .tstc = leon2_serial_tstc,
  135. };
  136. void leon2_serial_initialize(void)
  137. {
  138. serial_register(&leon2_serial_drv);
  139. }
  140. __weak struct serial_device *default_serial_console(void)
  141. {
  142. return &leon2_serial_drv;
  143. }