uart.c 10 KB

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