serial_pl011.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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_PL011_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. /*
  35. * IntegratorCP has two UARTs, use the first one, at 38400-8-N-1
  36. * Versatile PB has four UARTs.
  37. */
  38. #define NUM_PORTS 2
  39. #define CONSOLE_PORT CONFIG_CONS_INDEX
  40. #define baudRate CONFIG_BAUDRATE
  41. static volatile unsigned char * const port[NUM_PORTS] = {(void*)(CFG_SERIAL0),
  42. (void*)(CFG_SERIAL1)};
  43. static void pl011_putc(int portnum, char c);
  44. static int pl011_getc(int portnum);
  45. static int pl011_tstc(int portnum);
  46. int serial_init (void)
  47. {
  48. unsigned int temp;
  49. unsigned int divider;
  50. unsigned int remainder;
  51. unsigned int fraction;
  52. /*
  53. ** First, disable everything.
  54. */
  55. IO_WRITE(port[CONSOLE_PORT] + UART_PL011_CR, 0x0);
  56. /*
  57. ** Set baud rate
  58. **
  59. ** IBRD = UART_CLK / (16 * BAUD_RATE)
  60. ** FBRD = ROUND((64 * MOD(UART_CLK,(16 * BAUD_RATE))) / (16 * BAUD_RATE))
  61. */
  62. #ifdef CONFIG_VERSATILE
  63. temp = 16 * baudRate;
  64. divider = 24000000 / temp;
  65. remainder = 24000000 % temp;
  66. temp = (8 * remainder) / baudRate;
  67. fraction = (temp >> 1) + (temp & 1);
  68. #endif
  69. #ifdef CONFIG_INTEGRATOR
  70. temp = 16 * baudRate;
  71. divider = 14745600 / temp;
  72. remainder = 14745600 % temp;
  73. temp = (8 * remainder) / baudRate;
  74. fraction = (temp >> 1) + (temp & 1);
  75. #endif
  76. IO_WRITE(port[CONSOLE_PORT] + UART_PL011_IBRD, divider);
  77. IO_WRITE(port[CONSOLE_PORT] + UART_PL011_FBRD, fraction);
  78. /*
  79. ** Set the UART to be 8 bits, 1 stop bit, no parity, fifo enabled.
  80. */
  81. IO_WRITE(port[CONSOLE_PORT] + UART_PL011_LCRH,
  82. (UART_PL011_LCRH_WLEN_8 | UART_PL011_LCRH_FEN));
  83. /*
  84. ** Finally, enable the UART
  85. */
  86. IO_WRITE(port[CONSOLE_PORT] + UART_PL011_CR,
  87. (UART_PL011_CR_UARTEN | UART_PL011_CR_TXE | UART_PL011_CR_RXE));
  88. return (0);
  89. }
  90. void
  91. serial_putc(const char c)
  92. {
  93. if (c == '\n')
  94. pl011_putc(CONSOLE_PORT, '\r');
  95. pl011_putc(CONSOLE_PORT, c);
  96. }
  97. void
  98. serial_puts (const char *s)
  99. {
  100. while (*s) {
  101. serial_putc (*s++);
  102. }
  103. }
  104. int
  105. serial_getc(void)
  106. {
  107. return pl011_getc(CONSOLE_PORT);
  108. }
  109. int
  110. serial_tstc(void)
  111. {
  112. return pl011_tstc(CONSOLE_PORT);
  113. }
  114. void
  115. serial_setbrg (void)
  116. {
  117. }
  118. static void pl011_putc(int portnum, char c)
  119. {
  120. /* Wait until there is space in the FIFO */
  121. while (IO_READ(port[portnum] + UART_PL01x_FR) & UART_PL01x_FR_TXFF);
  122. /* Send the character */
  123. IO_WRITE(port[portnum] + UART_PL01x_DR, c);
  124. }
  125. static int pl011_getc(int portnum)
  126. {
  127. unsigned int data;
  128. /* Wait until there is data in the FIFO */
  129. while (IO_READ(port[portnum] + UART_PL01x_FR) & UART_PL01x_FR_RXFE);
  130. data = IO_READ(port[portnum] + UART_PL01x_DR);
  131. /* Check for an error flag */
  132. if (data & 0xFFFFFF00)
  133. {
  134. /* Clear the error */
  135. IO_WRITE(port[portnum] + UART_PL01x_ECR, 0xFFFFFFFF);
  136. return -1;
  137. }
  138. return (int)data;
  139. }
  140. static int pl011_tstc(int portnum)
  141. {
  142. return !(IO_READ(port[portnum] + UART_PL01x_FR) & UART_PL01x_FR_RXFE);
  143. }
  144. #endif