uart.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. /*
  36. * This table defines the UART's that should be tested and can
  37. * be overridden in the board config file
  38. */
  39. #ifndef CFG_POST_UART_TABLE
  40. #define CFG_POST_UART_TABLE {UART0_BASE, UART1_BASE, UART2_BASE, UART3_BASE}
  41. #endif
  42. #include <asm/processor.h>
  43. #include <serial.h>
  44. #if defined(CONFIG_440)
  45. #if defined(CONFIG_440EP) || defined(CONFIG_440GR) || \
  46. defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
  47. #define UART0_BASE CFG_PERIPHERAL_BASE + 0x00000300
  48. #define UART1_BASE CFG_PERIPHERAL_BASE + 0x00000400
  49. #define UART2_BASE CFG_PERIPHERAL_BASE + 0x00000500
  50. #define UART3_BASE CFG_PERIPHERAL_BASE + 0x00000600
  51. #else
  52. #define UART0_BASE CFG_PERIPHERAL_BASE + 0x00000200
  53. #define UART1_BASE CFG_PERIPHERAL_BASE + 0x00000300
  54. #endif
  55. #if defined(CONFIG_440SP) || defined(CONFIG_440SPE)
  56. #define UART2_BASE CFG_PERIPHERAL_BASE + 0x00000600
  57. #endif
  58. #if defined(CONFIG_440GP)
  59. #define CR0_MASK 0x3fff0000
  60. #define CR0_EXTCLK_ENA 0x00600000
  61. #define CR0_UDIV_POS 16
  62. #define UDIV_SUBTRACT 1
  63. #define UART0_SDR cntrl0
  64. #define MFREG(a, d) d = mfdcr(a)
  65. #define MTREG(a, d) mtdcr(a, d)
  66. #else /* #if defined(CONFIG_440GP) */
  67. /* all other 440 PPC's access clock divider via sdr register */
  68. #define CR0_MASK 0xdfffffff
  69. #define CR0_EXTCLK_ENA 0x00800000
  70. #define CR0_UDIV_POS 0
  71. #define UDIV_SUBTRACT 0
  72. #define UART0_SDR sdr_uart0
  73. #define UART1_SDR sdr_uart1
  74. #if defined(CONFIG_440EP) || defined(CONFIG_440EPx) || \
  75. defined(CONFIG_440GR) || defined(CONFIG_440GRx) || \
  76. defined(CONFIG_440SP) || defined(CONFIG_440SPe)
  77. #define UART2_SDR sdr_uart2
  78. #endif
  79. #if defined(CONFIG_440EP) || defined(CONFIG_440EPx) || \
  80. defined(CONFIG_440GR) || defined(CONFIG_440GRx)
  81. #define UART3_SDR sdr_uart3
  82. #endif
  83. #define MFREG(a, d) mfsdr(a, d)
  84. #define MTREG(a, d) mtsdr(a, d)
  85. #endif /* #if defined(CONFIG_440GP) */
  86. #elif defined(CONFIG_405EP) || defined(CONFIG_405EZ)
  87. #define UART0_BASE 0xef600300
  88. #define UART1_BASE 0xef600400
  89. #define UCR0_MASK 0x0000007f
  90. #define UCR1_MASK 0x00007f00
  91. #define UCR0_UDIV_POS 0
  92. #define UCR1_UDIV_POS 8
  93. #define UDIV_MAX 127
  94. #else /* CONFIG_405GP || CONFIG_405CR */
  95. #define UART0_BASE 0xef600300
  96. #define UART1_BASE 0xef600400
  97. #define CR0_MASK 0x00001fff
  98. #define CR0_EXTCLK_ENA 0x000000c0
  99. #define CR0_UDIV_POS 1
  100. #define UDIV_MAX 32
  101. #endif
  102. #define UART_RBR 0x00
  103. #define UART_THR 0x00
  104. #define UART_IER 0x01
  105. #define UART_IIR 0x02
  106. #define UART_FCR 0x02
  107. #define UART_LCR 0x03
  108. #define UART_MCR 0x04
  109. #define UART_LSR 0x05
  110. #define UART_MSR 0x06
  111. #define UART_SCR 0x07
  112. #define UART_DLL 0x00
  113. #define UART_DLM 0x01
  114. /*
  115. * Line Status Register.
  116. */
  117. #define asyncLSRDataReady1 0x01
  118. #define asyncLSROverrunError1 0x02
  119. #define asyncLSRParityError1 0x04
  120. #define asyncLSRFramingError1 0x08
  121. #define asyncLSRBreakInterrupt1 0x10
  122. #define asyncLSRTxHoldEmpty1 0x20
  123. #define asyncLSRTxShiftEmpty1 0x40
  124. #define asyncLSRRxFifoError1 0x80
  125. DECLARE_GLOBAL_DATA_PTR;
  126. #if defined(CONFIG_440)
  127. static int uart_post_init (unsigned long dev_base)
  128. {
  129. unsigned long reg;
  130. unsigned long udiv;
  131. unsigned short bdiv;
  132. volatile char val;
  133. #ifdef CFG_EXT_SERIAL_CLOCK
  134. unsigned long tmp;
  135. #endif
  136. int i;
  137. for (i = 0; i < 3500; i++) {
  138. if (in8 (dev_base + UART_LSR) & asyncLSRTxHoldEmpty1)
  139. break;
  140. udelay (100);
  141. }
  142. MFREG(UART0_SDR, reg);
  143. reg &= ~CR0_MASK;
  144. #ifdef CFG_EXT_SERIAL_CLOCK
  145. reg |= CR0_EXTCLK_ENA;
  146. udiv = 1;
  147. tmp = gd->baudrate * 16;
  148. bdiv = (CFG_EXT_SERIAL_CLOCK + tmp / 2) / tmp;
  149. #else
  150. /* For 440, the cpu clock is on divider chain A, UART on divider
  151. * chain B ... so cpu clock is irrelevant. Get the "optimized"
  152. * values that are subject to the 1/2 opb clock constraint
  153. */
  154. serial_divs (gd->baudrate, &udiv, &bdiv);
  155. #endif
  156. reg |= (udiv - UDIV_SUBTRACT) << CR0_UDIV_POS; /* set the UART divisor */
  157. /*
  158. * Configure input clock to baudrate generator for all
  159. * available serial ports here
  160. */
  161. MTREG(UART0_SDR, reg);
  162. #if defined(UART1_SDR)
  163. MTREG(UART1_SDR, reg);
  164. #endif
  165. #if defined(UART2_SDR)
  166. MTREG(UART2_SDR, reg);
  167. #endif
  168. #if defined(UART3_SDR)
  169. MTREG(UART3_SDR, reg);
  170. #endif
  171. out8(dev_base + UART_LCR, 0x80); /* set DLAB bit */
  172. out8(dev_base + UART_DLL, bdiv); /* set baudrate divisor */
  173. out8(dev_base + UART_DLM, bdiv >> 8); /* set baudrate divisor */
  174. out8(dev_base + UART_LCR, 0x03); /* clear DLAB; set 8 bits, no parity */
  175. out8(dev_base + UART_FCR, 0x00); /* disable FIFO */
  176. out8(dev_base + UART_MCR, 0x10); /* enable loopback mode */
  177. val = in8(dev_base + UART_LSR); /* clear line status */
  178. val = in8(dev_base + UART_RBR); /* read receive buffer */
  179. out8(dev_base + UART_SCR, 0x00); /* set scratchpad */
  180. out8(dev_base + UART_IER, 0x00); /* set interrupt enable reg */
  181. return 0;
  182. }
  183. #else /* CONFIG_440 */
  184. static int uart_post_init (unsigned long dev_base)
  185. {
  186. unsigned long reg;
  187. unsigned long tmp;
  188. unsigned long clk;
  189. unsigned long udiv;
  190. unsigned short bdiv;
  191. volatile char val;
  192. int i;
  193. for (i = 0; i < 3500; i++) {
  194. if (in8 (dev_base + UART_LSR) & asyncLSRTxHoldEmpty1)
  195. break;
  196. udelay (100);
  197. }
  198. #if defined(CONFIG_405EZ)
  199. serial_divs(gd->baudrate, &udiv, &bdiv);
  200. clk = tmp = reg = 0;
  201. #else
  202. #ifdef CONFIG_405EP
  203. reg = mfdcr(cpc0_ucr) & ~(UCR0_MASK | UCR1_MASK);
  204. clk = gd->cpu_clk;
  205. tmp = CFG_BASE_BAUD * 16;
  206. udiv = (clk + tmp / 2) / tmp;
  207. if (udiv > UDIV_MAX) /* max. n bits for udiv */
  208. udiv = UDIV_MAX;
  209. reg |= (udiv) << UCR0_UDIV_POS; /* set the UART divisor */
  210. reg |= (udiv) << UCR1_UDIV_POS; /* set the UART divisor */
  211. mtdcr (cpc0_ucr, reg);
  212. #else /* CONFIG_405EP */
  213. reg = mfdcr(cntrl0) & ~CR0_MASK;
  214. #ifdef CFG_EXT_SERIAL_CLOCK
  215. clk = CFG_EXT_SERIAL_CLOCK;
  216. udiv = 1;
  217. reg |= CR0_EXTCLK_ENA;
  218. #else
  219. clk = gd->cpu_clk;
  220. #ifdef CFG_405_UART_ERRATA_59
  221. udiv = 31; /* Errata 59: stuck at 31 */
  222. #else
  223. tmp = CFG_BASE_BAUD * 16;
  224. udiv = (clk + tmp / 2) / tmp;
  225. if (udiv > UDIV_MAX) /* max. n bits for udiv */
  226. udiv = UDIV_MAX;
  227. #endif
  228. #endif
  229. reg |= (udiv - 1) << CR0_UDIV_POS; /* set the UART divisor */
  230. mtdcr (cntrl0, reg);
  231. #endif /* CONFIG_405EP */
  232. tmp = gd->baudrate * udiv * 16;
  233. bdiv = (clk + tmp / 2) / tmp;
  234. #endif /* CONFIG_405EZ */
  235. out8(dev_base + UART_LCR, 0x80); /* set DLAB bit */
  236. out8(dev_base + UART_DLL, bdiv); /* set baudrate divisor */
  237. out8(dev_base + UART_DLM, bdiv >> 8); /* set baudrate divisor */
  238. out8(dev_base + UART_LCR, 0x03); /* clear DLAB; set 8 bits, no parity */
  239. out8(dev_base + UART_FCR, 0x00); /* disable FIFO */
  240. out8(dev_base + UART_MCR, 0x10); /* enable loopback mode */
  241. val = in8(dev_base + UART_LSR); /* clear line status */
  242. val = in8(dev_base + UART_RBR); /* read receive buffer */
  243. out8(dev_base + UART_SCR, 0x00); /* set scratchpad */
  244. out8(dev_base + UART_IER, 0x00); /* set interrupt enable reg */
  245. return (0);
  246. }
  247. #endif /* CONFIG_440 */
  248. static void uart_post_putc (unsigned long dev_base, char c)
  249. {
  250. int i;
  251. out8 (dev_base + UART_THR, c); /* put character out */
  252. /* Wait for transfer completion */
  253. for (i = 0; i < 3500; i++) {
  254. if (in8 (dev_base + UART_LSR) & asyncLSRTxHoldEmpty1)
  255. break;
  256. udelay (100);
  257. }
  258. }
  259. static int uart_post_getc (unsigned long dev_base)
  260. {
  261. int i;
  262. /* Wait for character available */
  263. for (i = 0; i < 3500; i++) {
  264. if (in8 (dev_base + UART_LSR) & asyncLSRDataReady1)
  265. break;
  266. udelay (100);
  267. }
  268. return 0xff & in8 (dev_base + UART_RBR);
  269. }
  270. static int test_ctlr (unsigned long dev_base, int index)
  271. {
  272. int res = -1;
  273. char test_str[] = "*** UART Test String ***\r\n";
  274. int i;
  275. uart_post_init (dev_base);
  276. for (i = 0; i < sizeof (test_str) - 1; i++) {
  277. uart_post_putc (dev_base, test_str[i]);
  278. if (uart_post_getc (dev_base) != test_str[i])
  279. goto done;
  280. }
  281. res = 0;
  282. done:
  283. if (res)
  284. post_log ("uart%d test failed\n", index);
  285. return res;
  286. }
  287. int uart_post_test (int flags)
  288. {
  289. int i, res = 0;
  290. static unsigned long base[] = CFG_POST_UART_TABLE;
  291. for (i = 0; i < sizeof (base) / sizeof (base[0]); i++) {
  292. if (test_ctlr (base[i], i))
  293. res = -1;
  294. }
  295. serial_reinit_all ();
  296. return res;
  297. }
  298. #endif /* CONFIG_POST & CFG_POST_UART */
  299. #endif /* CONFIG_POST */