uart.c 5.3 KB

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