uart.c 11 KB

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