serial_pl010.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * (C) Copyright 2000
  3. * Rob Taylor, Flying Pig Systems. robt@flyingpig.com.
  4. *
  5. * (C) Copyright 2004
  6. * ARM Ltd.
  7. * Philippe Robin, <philippe.robin@arm.com>
  8. *
  9. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. /* Simple U-Boot driver for the PrimeCell PL011 UARTs on the IntegratorCP */
  28. /* Should be fairly simple to make it work with the PL010 as well */
  29. #include <common.h>
  30. #ifdef CFG_PL010_SERIAL
  31. #include "serial_pl011.h"
  32. #define IO_WRITE(addr, val) (*(volatile unsigned int *)(addr) = (val))
  33. #define IO_READ(addr) (*(volatile unsigned int *)(addr))
  34. /* Integrator AP has two UARTs, we use the first one, at 38400-8-N-1 */
  35. #define NUM_PORTS 2
  36. #define CONSOLE_PORT CONFIG_CONS_INDEX
  37. #define baudRate CONFIG_BAUDRATE
  38. static volatile unsigned char * const port[NUM_PORTS] = {(void*)(CFG_SERIAL0),
  39. (void*)(CFG_SERIAL1)};
  40. static void pl010_putc(int portnum, char c);
  41. static int pl010_getc(int portnum);
  42. static int pl010_tstc(int portnum);
  43. int serial_init (void)
  44. {
  45. unsigned int temp;
  46. unsigned int divisor;
  47. /*
  48. ** First, disable everything.
  49. */
  50. IO_WRITE(port[CONSOLE_PORT] + UART_PL010_CR, 0x0);
  51. /*
  52. ** Set baud rate
  53. **
  54. */
  55. switch (baudRate) {
  56. case 9600:
  57. divisor = UART_PL010_BAUD_9600;
  58. break;
  59. case 19200:
  60. divisor = UART_PL010_BAUD_9600;
  61. break;
  62. case 38400:
  63. divisor = UART_PL010_BAUD_38400;
  64. break;
  65. case 57600:
  66. divisor = UART_PL010_BAUD_57600;
  67. break;
  68. case 115200:
  69. divisor = UART_PL010_BAUD_115200;
  70. break;
  71. default:
  72. divisor = UART_PL010_BAUD_38400;
  73. }
  74. IO_WRITE(port[CONSOLE_PORT] + UART_PL010_LCRM, ((divisor & 0xf00) >> 8));
  75. IO_WRITE(port[CONSOLE_PORT] + UART_PL010_LCRL, (divisor & 0xff));
  76. /*
  77. ** Set the UART to be 8 bits, 1 stop bit, no parity, fifo enabled.
  78. */
  79. IO_WRITE(port[CONSOLE_PORT] + UART_PL010_LCRH,
  80. (UART_PL010_LCRH_WLEN_8 | UART_PL010_LCRH_FEN));
  81. /*
  82. ** Finally, enable the UART
  83. */
  84. IO_WRITE(port[CONSOLE_PORT] + UART_PL010_CR, (UART_PL010_CR_UARTEN));
  85. return (0);
  86. }
  87. void
  88. serial_putc(const char c)
  89. {
  90. if (c == '\n')
  91. pl010_putc(CONSOLE_PORT, '\r');
  92. pl010_putc(CONSOLE_PORT, c);
  93. }
  94. void
  95. serial_puts (const char *s)
  96. {
  97. while (*s) {
  98. serial_putc (*s++);
  99. }
  100. }
  101. int
  102. serial_getc(void)
  103. {
  104. return pl010_getc(CONSOLE_PORT);
  105. }
  106. int
  107. serial_tstc(void)
  108. {
  109. return pl010_tstc(CONSOLE_PORT);
  110. }
  111. void
  112. serial_setbrg (void)
  113. {
  114. }
  115. static void pl010_putc(int portnum, char c)
  116. {
  117. /* Wait until there is space in the FIFO */
  118. while (IO_READ(port[portnum] + UART_PL01x_FR) & UART_PL01x_FR_TXFF);
  119. /* Send the character */
  120. IO_WRITE(port[portnum] + UART_PL01x_DR, c);
  121. }
  122. static int pl010_getc(int portnum)
  123. {
  124. unsigned int data;
  125. /* Wait until there is data in the FIFO */
  126. while (IO_READ(port[portnum] + UART_PL01x_FR) & UART_PL01x_FR_RXFE);
  127. data = IO_READ(port[portnum] + UART_PL01x_DR);
  128. /* Check for an error flag */
  129. if (data & 0xFFFFFF00)
  130. {
  131. /* Clear the error */
  132. IO_WRITE(port[portnum] + UART_PL01x_ECR, 0xFFFFFFFF);
  133. return -1;
  134. }
  135. return (int)data;
  136. }
  137. static int pl010_tstc(int portnum)
  138. {
  139. return !(IO_READ(port[portnum] + UART_PL01x_FR) & UART_PL01x_FR_RXFE);
  140. }
  141. #endif