uart.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * (C) Copyright 2007
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. /*
  25. * UART test
  26. *
  27. * The controllers are configured to loopback mode and several
  28. * characters are transmitted.
  29. */
  30. #ifdef CONFIG_POST
  31. #include <post.h>
  32. #if CONFIG_POST & CFG_POST_UART
  33. #include <asm/processor.h>
  34. #include <serial.h>
  35. #define UART0_BASE CFG_PERIPHERAL_BASE + 0x00000300
  36. #define UART1_BASE CFG_PERIPHERAL_BASE + 0x00000400
  37. #define UART2_BASE CFG_PERIPHERAL_BASE + 0x00000500
  38. #define UART3_BASE CFG_PERIPHERAL_BASE + 0x00000600
  39. #define CR0_MASK 0xdfffffff
  40. #define CR0_EXTCLK_ENA 0x00800000
  41. #define CR0_UDIV_POS 0
  42. #define UDIV_SUBTRACT 0
  43. #define UART0_SDR sdr_uart0
  44. #define UART1_SDR sdr_uart1
  45. #define UART2_SDR sdr_uart2
  46. #define UART3_SDR sdr_uart3
  47. #define MFREG(a, d) mfsdr(a, d)
  48. #define MTREG(a, d) mtsdr(a, d)
  49. #define UART_RBR 0x00
  50. #define UART_THR 0x00
  51. #define UART_IER 0x01
  52. #define UART_IIR 0x02
  53. #define UART_FCR 0x02
  54. #define UART_LCR 0x03
  55. #define UART_MCR 0x04
  56. #define UART_LSR 0x05
  57. #define UART_MSR 0x06
  58. #define UART_SCR 0x07
  59. #define UART_DLL 0x00
  60. #define UART_DLM 0x01
  61. /*
  62. Line Status Register.
  63. */
  64. #define asyncLSRDataReady1 0x01
  65. #define asyncLSROverrunError1 0x02
  66. #define asyncLSRParityError1 0x04
  67. #define asyncLSRFramingError1 0x08
  68. #define asyncLSRBreakInterrupt1 0x10
  69. #define asyncLSRTxHoldEmpty1 0x20
  70. #define asyncLSRTxShiftEmpty1 0x40
  71. #define asyncLSRRxFifoError1 0x80
  72. DECLARE_GLOBAL_DATA_PTR;
  73. static int uart_post_init (unsigned long dev_base)
  74. {
  75. unsigned long reg;
  76. unsigned long udiv;
  77. unsigned short bdiv;
  78. volatile char val;
  79. #ifdef CFG_EXT_SERIAL_CLOCK
  80. unsigned long tmp;
  81. #endif
  82. int i;
  83. for (i = 0; i < 3500; i++) {
  84. if (in8 (dev_base + UART_LSR) & asyncLSRTxHoldEmpty1)
  85. break;
  86. udelay (100);
  87. }
  88. MFREG(UART0_SDR, reg);
  89. reg &= ~CR0_MASK;
  90. #ifdef CFG_EXT_SERIAL_CLOCK
  91. reg |= CR0_EXTCLK_ENA;
  92. udiv = 1;
  93. tmp = gd->baudrate * 16;
  94. bdiv = (CFG_EXT_SERIAL_CLOCK + tmp / 2) / tmp;
  95. #else
  96. /* For 440, the cpu clock is on divider chain A, UART on divider
  97. * chain B ... so cpu clock is irrelevant. Get the "optimized"
  98. * values that are subject to the 1/2 opb clock constraint
  99. */
  100. serial_divs (gd->baudrate, &udiv, &bdiv);
  101. #endif
  102. reg |= (udiv - UDIV_SUBTRACT) << CR0_UDIV_POS; /* set the UART divisor */
  103. /*
  104. * Configure input clock to baudrate generator for all
  105. * available serial ports here
  106. */
  107. MTREG(UART0_SDR, reg);
  108. #if defined(UART1_SDR)
  109. MTREG(UART1_SDR, reg);
  110. #endif
  111. #if defined(UART2_SDR)
  112. MTREG(UART2_SDR, reg);
  113. #endif
  114. #if defined(UART3_SDR)
  115. MTREG(UART3_SDR, reg);
  116. #endif
  117. out8(dev_base + UART_LCR, 0x80); /* set DLAB bit */
  118. out8(dev_base + UART_DLL, bdiv); /* set baudrate divisor */
  119. out8(dev_base + UART_DLM, bdiv >> 8); /* set baudrate divisor */
  120. out8(dev_base + UART_LCR, 0x03); /* clear DLAB; set 8 bits, no parity */
  121. out8(dev_base + UART_FCR, 0x00); /* disable FIFO */
  122. out8(dev_base + UART_MCR, 0x10); /* enable loopback mode */
  123. val = in8(dev_base + UART_LSR); /* clear line status */
  124. val = in8(dev_base + UART_RBR); /* read receive buffer */
  125. out8(dev_base + UART_SCR, 0x00); /* set scratchpad */
  126. out8(dev_base + UART_IER, 0x00); /* set interrupt enable reg */
  127. return 0;
  128. }
  129. static void uart_post_putc (unsigned long dev_base, char c)
  130. {
  131. int i;
  132. out8 (dev_base + UART_THR, c); /* put character out */
  133. /* Wait for transfer completion */
  134. for (i = 0; i < 3500; i++) {
  135. if (in8 (dev_base + UART_LSR) & asyncLSRTxHoldEmpty1)
  136. break;
  137. udelay (100);
  138. }
  139. }
  140. static int uart_post_getc (unsigned long dev_base)
  141. {
  142. int i;
  143. /* Wait for character available */
  144. for (i = 0; i < 3500; i++) {
  145. if (in8 (dev_base + UART_LSR) & asyncLSRDataReady1)
  146. break;
  147. udelay (100);
  148. }
  149. return 0xff & in8 (dev_base + UART_RBR);
  150. }
  151. static int test_ctlr (unsigned long dev_base, int index)
  152. {
  153. int res = -1;
  154. char test_str[] = "*** UART Test String ***\r\n";
  155. int i;
  156. uart_post_init (dev_base);
  157. for (i = 0; i < sizeof (test_str) - 1; i++) {
  158. uart_post_putc (dev_base, test_str[i]);
  159. if (uart_post_getc (dev_base) != test_str[i])
  160. goto done;
  161. }
  162. res = 0;
  163. done:
  164. if (res)
  165. post_log ("uart%d test failed\n", index);
  166. return res;
  167. }
  168. int uart_post_test (int flags)
  169. {
  170. int i, res = 0;
  171. static unsigned long base[] = {
  172. UART0_BASE, UART1_BASE, UART2_BASE, UART3_BASE
  173. };
  174. for (i = 0; i < sizeof (base) / sizeof (base[0]); i++) {
  175. if (test_ctlr (base[i], i))
  176. res = -1;
  177. }
  178. serial_reinit_all ();
  179. return res;
  180. }
  181. #endif /* CONFIG_POST & CFG_POST_UART */
  182. #endif /* CONFIG_POST */