serial_pl01x.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. static struct pl01x_regs *pl01x_get_regs(int portnum)
  46. {
  47. return (struct pl01x_regs *) port[portnum];
  48. }
  49. #ifdef CONFIG_PL010_SERIAL
  50. int serial_init (void)
  51. {
  52. struct pl01x_regs *regs = pl01x_get_regs(CONSOLE_PORT);
  53. unsigned int divisor;
  54. /* First, disable everything */
  55. writel(0, &regs->pl010_cr);
  56. /* Set baud rate */
  57. switch (baudrate) {
  58. case 9600:
  59. divisor = UART_PL010_BAUD_9600;
  60. break;
  61. case 19200:
  62. divisor = UART_PL010_BAUD_9600;
  63. break;
  64. case 38400:
  65. divisor = UART_PL010_BAUD_38400;
  66. break;
  67. case 57600:
  68. divisor = UART_PL010_BAUD_57600;
  69. break;
  70. case 115200:
  71. divisor = UART_PL010_BAUD_115200;
  72. break;
  73. default:
  74. divisor = UART_PL010_BAUD_38400;
  75. }
  76. writel((divisor & 0xf00) >> 8, &regs->pl010_lcrm);
  77. writel(divisor & 0xff, &regs->pl010_lcrl);
  78. /* Set the UART to be 8 bits, 1 stop bit, no parity, fifo enabled */
  79. writel(UART_PL010_LCRH_WLEN_8 | UART_PL010_LCRH_FEN, &regs->pl010_lcrh);
  80. /* Finally, enable the UART */
  81. writel(UART_PL010_CR_UARTEN, &regs->pl010_cr);
  82. return 0;
  83. }
  84. #endif /* CONFIG_PL010_SERIAL */
  85. #ifdef CONFIG_PL011_SERIAL
  86. int serial_init (void)
  87. {
  88. struct pl01x_regs *regs = pl01x_get_regs(CONSOLE_PORT);
  89. unsigned int temp;
  90. unsigned int divider;
  91. unsigned int remainder;
  92. unsigned int fraction;
  93. unsigned int lcr;
  94. #ifdef CONFIG_PL011_SERIAL_FLUSH_ON_INIT
  95. /* Empty RX fifo if necessary */
  96. if (readl(&regs->pl011_cr) & UART_PL011_CR_UARTEN) {
  97. while (!(readl(&regs->fr) & UART_PL01x_FR_RXFE))
  98. readl(&regs->dr);
  99. }
  100. #endif
  101. /* First, disable everything */
  102. writel(0, &regs->pl011_cr);
  103. /*
  104. * Set baud rate
  105. *
  106. * IBRD = UART_CLK / (16 * BAUD_RATE)
  107. * FBRD = RND((64 * MOD(UART_CLK,(16 * BAUD_RATE))) / (16 * BAUD_RATE))
  108. */
  109. temp = 16 * baudrate;
  110. divider = CONFIG_PL011_CLOCK / temp;
  111. remainder = CONFIG_PL011_CLOCK % temp;
  112. temp = (8 * remainder) / baudrate;
  113. fraction = (temp >> 1) + (temp & 1);
  114. writel(divider, &regs->pl011_ibrd);
  115. writel(fraction, &regs->pl011_fbrd);
  116. /* Set the UART to be 8 bits, 1 stop bit, no parity, fifo enabled */
  117. lcr = UART_PL011_LCRH_WLEN_8 | UART_PL011_LCRH_FEN;
  118. writel(lcr, &regs->pl011_lcrh);
  119. #ifdef CONFIG_PL011_SERIAL_RLCR
  120. {
  121. int i;
  122. /*
  123. * Program receive line control register after waiting
  124. * 10 bus cycles. Delay be writing to readonly register
  125. * 10 times
  126. */
  127. for (i = 0; i < 10; i++)
  128. writel(lcr, &regs->fr);
  129. writel(lcr, &regs->pl011_rlcr);
  130. }
  131. #endif
  132. /* Finally, enable the UART */
  133. writel(UART_PL011_CR_UARTEN | UART_PL011_CR_TXE | UART_PL011_CR_RXE,
  134. &regs->pl011_cr);
  135. return 0;
  136. }
  137. #endif /* CONFIG_PL011_SERIAL */
  138. void serial_putc (const char c)
  139. {
  140. if (c == '\n')
  141. pl01x_putc (CONSOLE_PORT, '\r');
  142. pl01x_putc (CONSOLE_PORT, c);
  143. }
  144. void serial_puts (const char *s)
  145. {
  146. while (*s) {
  147. serial_putc (*s++);
  148. }
  149. }
  150. int serial_getc (void)
  151. {
  152. return pl01x_getc (CONSOLE_PORT);
  153. }
  154. int serial_tstc (void)
  155. {
  156. return pl01x_tstc (CONSOLE_PORT);
  157. }
  158. void serial_setbrg (void)
  159. {
  160. baudrate = gd->baudrate;
  161. serial_init();
  162. }
  163. static void pl01x_putc (int portnum, char c)
  164. {
  165. struct pl01x_regs *regs = pl01x_get_regs(portnum);
  166. /* Wait until there is space in the FIFO */
  167. while (readl(&regs->fr) & UART_PL01x_FR_TXFF)
  168. WATCHDOG_RESET();
  169. /* Send the character */
  170. writel(c, &regs->dr);
  171. }
  172. static int pl01x_getc (int portnum)
  173. {
  174. struct pl01x_regs *regs = pl01x_get_regs(portnum);
  175. unsigned int data;
  176. /* Wait until there is data in the FIFO */
  177. while (readl(&regs->fr) & UART_PL01x_FR_RXFE)
  178. WATCHDOG_RESET();
  179. data = readl(&regs->dr);
  180. /* Check for an error flag */
  181. if (data & 0xFFFFFF00) {
  182. /* Clear the error */
  183. writel(0xFFFFFFFF, &regs->ecr);
  184. return -1;
  185. }
  186. return (int) data;
  187. }
  188. static int pl01x_tstc (int portnum)
  189. {
  190. struct pl01x_regs *regs = pl01x_get_regs(portnum);
  191. WATCHDOG_RESET();
  192. return !(readl(&regs->fr) & UART_PL01x_FR_RXFE);
  193. }