uart.c 11 KB

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