serial_pl01x.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 PL010/PL011 UARTs */
  28. #include <common.h>
  29. #include <watchdog.h>
  30. #include <asm/io.h>
  31. #include "serial_pl01x.h"
  32. /*
  33. * Integrator AP has two UARTs, we use the first one, at 38400-8-N-1
  34. * Integrator CP has two UARTs, use the first one, at 38400-8-N-1
  35. * Versatile PB has four UARTs.
  36. */
  37. #define CONSOLE_PORT CONFIG_CONS_INDEX
  38. static volatile unsigned char *const port[] = CONFIG_PL01x_PORTS;
  39. #define NUM_PORTS (sizeof(port)/sizeof(port[0]))
  40. static void pl01x_putc (int portnum, char c);
  41. static int pl01x_getc (int portnum);
  42. static int pl01x_tstc (int portnum);
  43. unsigned int baudrate = CONFIG_BAUDRATE;
  44. DECLARE_GLOBAL_DATA_PTR;
  45. #ifdef CONFIG_PL010_SERIAL
  46. int serial_init (void)
  47. {
  48. unsigned int divisor;
  49. /* First, disable everything */
  50. writel(0x0, port[CONSOLE_PORT] + UART_PL010_CR);
  51. /* Set baud rate */
  52. switch (baudrate) {
  53. case 9600:
  54. divisor = UART_PL010_BAUD_9600;
  55. break;
  56. case 19200:
  57. divisor = UART_PL010_BAUD_9600;
  58. break;
  59. case 38400:
  60. divisor = UART_PL010_BAUD_38400;
  61. break;
  62. case 57600:
  63. divisor = UART_PL010_BAUD_57600;
  64. break;
  65. case 115200:
  66. divisor = UART_PL010_BAUD_115200;
  67. break;
  68. default:
  69. divisor = UART_PL010_BAUD_38400;
  70. }
  71. writel(((divisor & 0xf00) >> 8), port[CONSOLE_PORT] + UART_PL010_LCRM);
  72. writel((divisor & 0xff), port[CONSOLE_PORT] + UART_PL010_LCRL);
  73. /* Set the UART to be 8 bits, 1 stop bit, no parity, fifo enabled */
  74. writel((UART_PL010_LCRH_WLEN_8 | UART_PL010_LCRH_FEN),
  75. port[CONSOLE_PORT] + UART_PL010_LCRH);
  76. /* Finally, enable the UART */
  77. writel((UART_PL010_CR_UARTEN), port[CONSOLE_PORT] + UART_PL010_CR);
  78. return 0;
  79. }
  80. #endif /* CONFIG_PL010_SERIAL */
  81. #ifdef CONFIG_PL011_SERIAL
  82. int serial_init (void)
  83. {
  84. unsigned int temp;
  85. unsigned int divider;
  86. unsigned int remainder;
  87. unsigned int fraction;
  88. /* First, disable everything */
  89. writel(0x0, port[CONSOLE_PORT] + UART_PL011_CR);
  90. /*
  91. * Set baud rate
  92. *
  93. * IBRD = UART_CLK / (16 * BAUD_RATE)
  94. * FBRD = RND((64 * MOD(UART_CLK,(16 * BAUD_RATE))) / (16 * BAUD_RATE))
  95. */
  96. temp = 16 * baudrate;
  97. divider = CONFIG_PL011_CLOCK / temp;
  98. remainder = CONFIG_PL011_CLOCK % temp;
  99. temp = (8 * remainder) / baudrate;
  100. fraction = (temp >> 1) + (temp & 1);
  101. writel(divider, port[CONSOLE_PORT] + UART_PL011_IBRD);
  102. writel(fraction, port[CONSOLE_PORT] + UART_PL011_FBRD);
  103. /* Set the UART to be 8 bits, 1 stop bit, no parity, fifo enabled */
  104. writel((UART_PL011_LCRH_WLEN_8 | UART_PL011_LCRH_FEN),
  105. port[CONSOLE_PORT] + UART_PL011_LCRH);
  106. /* Finally, enable the UART */
  107. writel((UART_PL011_CR_UARTEN | UART_PL011_CR_TXE | UART_PL011_CR_RXE),
  108. port[CONSOLE_PORT] + UART_PL011_CR);
  109. return 0;
  110. }
  111. #endif /* CONFIG_PL011_SERIAL */
  112. void serial_putc (const char c)
  113. {
  114. if (c == '\n')
  115. pl01x_putc (CONSOLE_PORT, '\r');
  116. pl01x_putc (CONSOLE_PORT, c);
  117. }
  118. void serial_puts (const char *s)
  119. {
  120. while (*s) {
  121. serial_putc (*s++);
  122. }
  123. }
  124. int serial_getc (void)
  125. {
  126. return pl01x_getc (CONSOLE_PORT);
  127. }
  128. int serial_tstc (void)
  129. {
  130. return pl01x_tstc (CONSOLE_PORT);
  131. }
  132. void serial_setbrg (void)
  133. {
  134. baudrate = gd->baudrate;
  135. serial_init();
  136. }
  137. static void pl01x_putc (int portnum, char c)
  138. {
  139. /* Wait until there is space in the FIFO */
  140. while (readl(port[portnum] + UART_PL01x_FR) & UART_PL01x_FR_TXFF)
  141. WATCHDOG_RESET();
  142. /* Send the character */
  143. writel(c, port[portnum] + UART_PL01x_DR);
  144. }
  145. static int pl01x_getc (int portnum)
  146. {
  147. unsigned int data;
  148. /* Wait until there is data in the FIFO */
  149. while (readl(port[portnum] + UART_PL01x_FR) & UART_PL01x_FR_RXFE)
  150. WATCHDOG_RESET();
  151. data = readl(port[portnum] + UART_PL01x_DR);
  152. /* Check for an error flag */
  153. if (data & 0xFFFFFF00) {
  154. /* Clear the error */
  155. writel(0xFFFFFFFF, port[portnum] + UART_PL01x_ECR);
  156. return -1;
  157. }
  158. return (int) data;
  159. }
  160. static int pl01x_tstc (int portnum)
  161. {
  162. WATCHDOG_RESET();
  163. return !(readl(port[portnum] + UART_PL01x_FR) &
  164. UART_PL01x_FR_RXFE);
  165. }