serial_pl01x.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. /* Simple U-Boot driver for the PrimeCell PL010/PL011 UARTs */
  12. #include <common.h>
  13. #include <watchdog.h>
  14. #include <asm/io.h>
  15. #include <serial.h>
  16. #include <linux/compiler.h>
  17. #include "serial_pl01x.h"
  18. /*
  19. * Integrator AP has two UARTs, we use the first one, at 38400-8-N-1
  20. * Integrator CP has two UARTs, use the first one, at 38400-8-N-1
  21. * Versatile PB has four UARTs.
  22. */
  23. #define CONSOLE_PORT CONFIG_CONS_INDEX
  24. static volatile unsigned char *const port[] = CONFIG_PL01x_PORTS;
  25. #define NUM_PORTS (sizeof(port)/sizeof(port[0]))
  26. static void pl01x_putc (int portnum, char c);
  27. static int pl01x_getc (int portnum);
  28. static int pl01x_tstc (int portnum);
  29. unsigned int baudrate = CONFIG_BAUDRATE;
  30. DECLARE_GLOBAL_DATA_PTR;
  31. static struct pl01x_regs *pl01x_get_regs(int portnum)
  32. {
  33. return (struct pl01x_regs *) port[portnum];
  34. }
  35. #ifdef CONFIG_PL010_SERIAL
  36. static int pl01x_serial_init(void)
  37. {
  38. struct pl01x_regs *regs = pl01x_get_regs(CONSOLE_PORT);
  39. unsigned int divisor;
  40. /* First, disable everything */
  41. writel(0, &regs->pl010_cr);
  42. /* Set baud rate */
  43. switch (baudrate) {
  44. case 9600:
  45. divisor = UART_PL010_BAUD_9600;
  46. break;
  47. case 19200:
  48. divisor = UART_PL010_BAUD_9600;
  49. break;
  50. case 38400:
  51. divisor = UART_PL010_BAUD_38400;
  52. break;
  53. case 57600:
  54. divisor = UART_PL010_BAUD_57600;
  55. break;
  56. case 115200:
  57. divisor = UART_PL010_BAUD_115200;
  58. break;
  59. default:
  60. divisor = UART_PL010_BAUD_38400;
  61. }
  62. writel((divisor & 0xf00) >> 8, &regs->pl010_lcrm);
  63. writel(divisor & 0xff, &regs->pl010_lcrl);
  64. /* Set the UART to be 8 bits, 1 stop bit, no parity, fifo enabled */
  65. writel(UART_PL010_LCRH_WLEN_8 | UART_PL010_LCRH_FEN, &regs->pl010_lcrh);
  66. /* Finally, enable the UART */
  67. writel(UART_PL010_CR_UARTEN, &regs->pl010_cr);
  68. return 0;
  69. }
  70. #endif /* CONFIG_PL010_SERIAL */
  71. #ifdef CONFIG_PL011_SERIAL
  72. static int pl01x_serial_init(void)
  73. {
  74. struct pl01x_regs *regs = pl01x_get_regs(CONSOLE_PORT);
  75. unsigned int temp;
  76. unsigned int divider;
  77. unsigned int remainder;
  78. unsigned int fraction;
  79. unsigned int lcr;
  80. #ifdef CONFIG_PL011_SERIAL_FLUSH_ON_INIT
  81. /* Empty RX fifo if necessary */
  82. if (readl(&regs->pl011_cr) & UART_PL011_CR_UARTEN) {
  83. while (!(readl(&regs->fr) & UART_PL01x_FR_RXFE))
  84. readl(&regs->dr);
  85. }
  86. #endif
  87. /* First, disable everything */
  88. writel(0, &regs->pl011_cr);
  89. /*
  90. * Set baud rate
  91. *
  92. * IBRD = UART_CLK / (16 * BAUD_RATE)
  93. * FBRD = RND((64 * MOD(UART_CLK,(16 * BAUD_RATE))) / (16 * BAUD_RATE))
  94. */
  95. temp = 16 * baudrate;
  96. divider = CONFIG_PL011_CLOCK / temp;
  97. remainder = CONFIG_PL011_CLOCK % temp;
  98. temp = (8 * remainder) / baudrate;
  99. fraction = (temp >> 1) + (temp & 1);
  100. writel(divider, &regs->pl011_ibrd);
  101. writel(fraction, &regs->pl011_fbrd);
  102. /* Set the UART to be 8 bits, 1 stop bit, no parity, fifo enabled */
  103. lcr = UART_PL011_LCRH_WLEN_8 | UART_PL011_LCRH_FEN;
  104. writel(lcr, &regs->pl011_lcrh);
  105. #ifdef CONFIG_PL011_SERIAL_RLCR
  106. {
  107. int i;
  108. /*
  109. * Program receive line control register after waiting
  110. * 10 bus cycles. Delay be writing to readonly register
  111. * 10 times
  112. */
  113. for (i = 0; i < 10; i++)
  114. writel(lcr, &regs->fr);
  115. writel(lcr, &regs->pl011_rlcr);
  116. /* lcrh needs to be set again for change to be effective */
  117. writel(lcr, &regs->pl011_lcrh);
  118. }
  119. #endif
  120. /* Finally, enable the UART */
  121. writel(UART_PL011_CR_UARTEN | UART_PL011_CR_TXE | UART_PL011_CR_RXE |
  122. UART_PL011_CR_RTS, &regs->pl011_cr);
  123. return 0;
  124. }
  125. #endif /* CONFIG_PL011_SERIAL */
  126. static void pl01x_serial_putc(const char c)
  127. {
  128. if (c == '\n')
  129. pl01x_putc (CONSOLE_PORT, '\r');
  130. pl01x_putc (CONSOLE_PORT, c);
  131. }
  132. static int pl01x_serial_getc(void)
  133. {
  134. return pl01x_getc (CONSOLE_PORT);
  135. }
  136. static int pl01x_serial_tstc(void)
  137. {
  138. return pl01x_tstc (CONSOLE_PORT);
  139. }
  140. static void pl01x_serial_setbrg(void)
  141. {
  142. struct pl01x_regs *regs = pl01x_get_regs(CONSOLE_PORT);
  143. baudrate = gd->baudrate;
  144. /*
  145. * Flush FIFO and wait for non-busy before changing baudrate to avoid
  146. * crap in console
  147. */
  148. while (!(readl(&regs->fr) & UART_PL01x_FR_TXFE))
  149. WATCHDOG_RESET();
  150. while (readl(&regs->fr) & UART_PL01x_FR_BUSY)
  151. WATCHDOG_RESET();
  152. serial_init();
  153. }
  154. static void pl01x_putc (int portnum, char c)
  155. {
  156. struct pl01x_regs *regs = pl01x_get_regs(portnum);
  157. /* Wait until there is space in the FIFO */
  158. while (readl(&regs->fr) & UART_PL01x_FR_TXFF)
  159. WATCHDOG_RESET();
  160. /* Send the character */
  161. writel(c, &regs->dr);
  162. }
  163. static int pl01x_getc (int portnum)
  164. {
  165. struct pl01x_regs *regs = pl01x_get_regs(portnum);
  166. unsigned int data;
  167. /* Wait until there is data in the FIFO */
  168. while (readl(&regs->fr) & UART_PL01x_FR_RXFE)
  169. WATCHDOG_RESET();
  170. data = readl(&regs->dr);
  171. /* Check for an error flag */
  172. if (data & 0xFFFFFF00) {
  173. /* Clear the error */
  174. writel(0xFFFFFFFF, &regs->ecr);
  175. return -1;
  176. }
  177. return (int) data;
  178. }
  179. static int pl01x_tstc (int portnum)
  180. {
  181. struct pl01x_regs *regs = pl01x_get_regs(portnum);
  182. WATCHDOG_RESET();
  183. return !(readl(&regs->fr) & UART_PL01x_FR_RXFE);
  184. }
  185. static struct serial_device pl01x_serial_drv = {
  186. .name = "pl01x_serial",
  187. .start = pl01x_serial_init,
  188. .stop = NULL,
  189. .setbrg = pl01x_serial_setbrg,
  190. .putc = pl01x_serial_putc,
  191. .puts = default_serial_puts,
  192. .getc = pl01x_serial_getc,
  193. .tstc = pl01x_serial_tstc,
  194. };
  195. void pl01x_serial_initialize(void)
  196. {
  197. serial_register(&pl01x_serial_drv);
  198. }
  199. __weak struct serial_device *default_serial_console(void)
  200. {
  201. return &pl01x_serial_drv;
  202. }