uart.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * (C) Copyright 2007
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * Author: Igor Lisitsin <igor@emcraft.com>
  6. *
  7. * Copyright 2010, Stefan Roese, DENX Software Engineering, sr@denx.de
  8. *
  9. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. #include <common.h>
  28. #include <ppc4xx.h>
  29. #include <ns16550.h>
  30. #include <asm/io.h>
  31. /*
  32. * UART test
  33. *
  34. * The controllers are configured to loopback mode and several
  35. * characters are transmitted.
  36. */
  37. #include <post.h>
  38. #if CONFIG_POST & CONFIG_SYS_POST_UART
  39. /*
  40. * This table defines the UART's that should be tested and can
  41. * be overridden in the board config file
  42. */
  43. #ifndef CONFIG_SYS_POST_UART_TABLE
  44. #define CONFIG_SYS_POST_UART_TABLE {UART0_BASE, UART1_BASE, UART2_BASE, UART3_BASE}
  45. #endif
  46. #include <asm/processor.h>
  47. #include <serial.h>
  48. #if defined(CONFIG_440)
  49. #if defined(CONFIG_440EP) || defined(CONFIG_440GR) || \
  50. defined(CONFIG_440EPX) || defined(CONFIG_440GRX)
  51. #define UART0_BASE CONFIG_SYS_PERIPHERAL_BASE + 0x00000300
  52. #define UART1_BASE CONFIG_SYS_PERIPHERAL_BASE + 0x00000400
  53. #define UART2_BASE CONFIG_SYS_PERIPHERAL_BASE + 0x00000500
  54. #define UART3_BASE CONFIG_SYS_PERIPHERAL_BASE + 0x00000600
  55. #else
  56. #define UART0_BASE CONFIG_SYS_PERIPHERAL_BASE + 0x00000200
  57. #define UART1_BASE CONFIG_SYS_PERIPHERAL_BASE + 0x00000300
  58. #endif
  59. #if defined(CONFIG_440SP) || defined(CONFIG_440SPE)
  60. #define UART2_BASE CONFIG_SYS_PERIPHERAL_BASE + 0x00000600
  61. #endif
  62. #if defined(CONFIG_440GP)
  63. #define CR0_MASK 0x3fff0000
  64. #define CR0_EXTCLK_ENA 0x00600000
  65. #define CR0_UDIV_POS 16
  66. #define UDIV_SUBTRACT 1
  67. #define UART0_SDR CPC0_CR0
  68. #define MFREG(a, d) d = mfdcr(a)
  69. #define MTREG(a, d) mtdcr(a, d)
  70. #else /* #if defined(CONFIG_440GP) */
  71. /* all other 440 PPC's access clock divider via sdr register */
  72. #define CR0_MASK 0xdfffffff
  73. #define CR0_EXTCLK_ENA 0x00800000
  74. #define CR0_UDIV_POS 0
  75. #define UDIV_SUBTRACT 0
  76. #define UART0_SDR SDR0_UART0
  77. #define UART1_SDR SDR0_UART1
  78. #if defined(CONFIG_440EP) || defined(CONFIG_440EPX) || \
  79. defined(CONFIG_440GR) || defined(CONFIG_440GRX) || \
  80. defined(CONFIG_440SP) || defined(CONFIG_440SPE)
  81. #define UART2_SDR SDR0_UART2
  82. #endif
  83. #if defined(CONFIG_440EP) || defined(CONFIG_440EPX) || \
  84. defined(CONFIG_440GR) || defined(CONFIG_440GRX)
  85. #define UART3_SDR SDR0_UART3
  86. #endif
  87. #define MFREG(a, d) mfsdr(a, d)
  88. #define MTREG(a, d) mtsdr(a, d)
  89. #endif /* #if defined(CONFIG_440GP) */
  90. #elif defined(CONFIG_405EP) || defined(CONFIG_405EZ)
  91. #define UART0_BASE 0xef600300
  92. #define UART1_BASE 0xef600400
  93. #define UCR0_MASK 0x0000007f
  94. #define UCR1_MASK 0x00007f00
  95. #define UCR0_UDIV_POS 0
  96. #define UCR1_UDIV_POS 8
  97. #define UDIV_MAX 127
  98. #elif defined(CONFIG_405EX)
  99. #define UART0_BASE 0xef600200
  100. #define UART1_BASE 0xef600300
  101. #define CR0_MASK 0x000000ff
  102. #define CR0_EXTCLK_ENA 0x00800000
  103. #define CR0_UDIV_POS 0
  104. #define UDIV_SUBTRACT 0
  105. #define UART0_SDR SDR0_UART0
  106. #define UART1_SDR SDR0_UART1
  107. #define MFREG(a, d) mfsdr(a, d)
  108. #define MTREG(a, d) mtsdr(a, d)
  109. #else /* CONFIG_405GP || CONFIG_405CR */
  110. #define UART0_BASE 0xef600300
  111. #define UART1_BASE 0xef600400
  112. #define CR0_MASK 0x00001fff
  113. #define CR0_EXTCLK_ENA 0x000000c0
  114. #define CR0_UDIV_POS 1
  115. #define UDIV_MAX 32
  116. #endif
  117. DECLARE_GLOBAL_DATA_PTR;
  118. static void uart_post_init_common(struct NS16550 *com_port, unsigned short bdiv)
  119. {
  120. volatile char val;
  121. out_8(&com_port->lcr, 0x80); /* set DLAB bit */
  122. out_8(&com_port->dll, bdiv); /* set baudrate divisor */
  123. out_8(&com_port->dlm, bdiv >> 8); /* set baudrate divisor */
  124. out_8(&com_port->lcr, 0x03); /* clear DLAB; set 8 bits, no parity */
  125. out_8(&com_port->fcr, 0x00); /* disable FIFO */
  126. out_8(&com_port->mcr, 0x10); /* enable loopback mode */
  127. val = in_8(&com_port->lsr); /* clear line status */
  128. val = in_8(&com_port->rbr); /* read receive buffer */
  129. out_8(&com_port->scr, 0x00); /* set scratchpad */
  130. out_8(&com_port->ier, 0x00); /* set interrupt enable reg */
  131. }
  132. #if defined(CONFIG_440) || defined(CONFIG_405EX)
  133. #if !defined(CONFIG_SYS_EXT_SERIAL_CLOCK)
  134. static void serial_divs (int baudrate, unsigned long *pudiv,
  135. unsigned short *pbdiv)
  136. {
  137. sys_info_t sysinfo;
  138. unsigned long div; /* total divisor udiv * bdiv */
  139. unsigned long umin; /* minimum udiv */
  140. unsigned short diff; /* smallest diff */
  141. unsigned long udiv; /* best udiv */
  142. unsigned short idiff; /* current diff */
  143. unsigned short ibdiv; /* current bdiv */
  144. unsigned long i;
  145. unsigned long est; /* current estimate */
  146. get_sys_info(&sysinfo);
  147. udiv = 32; /* Assume lowest possible serial clk */
  148. div = sysinfo.freqPLB / (16 * baudrate); /* total divisor */
  149. umin = sysinfo.pllOpbDiv << 1; /* 2 x OPB divisor */
  150. diff = 32; /* highest possible */
  151. /* i is the test udiv value -- start with the largest
  152. * possible (32) to minimize serial clock and constrain
  153. * search to umin.
  154. */
  155. for (i = 32; i > umin; i--) {
  156. ibdiv = div / i;
  157. est = i * ibdiv;
  158. idiff = (est > div) ? (est-div) : (div-est);
  159. if (idiff == 0) {
  160. udiv = i;
  161. break; /* can't do better */
  162. } else if (idiff < diff) {
  163. udiv = i; /* best so far */
  164. diff = idiff; /* update lowest diff*/
  165. }
  166. }
  167. *pudiv = udiv;
  168. *pbdiv = div / udiv;
  169. }
  170. #endif
  171. static int uart_post_init (struct NS16550 *com_port)
  172. {
  173. unsigned long reg = 0;
  174. unsigned long udiv;
  175. unsigned short bdiv;
  176. #ifdef CONFIG_SYS_EXT_SERIAL_CLOCK
  177. unsigned long tmp;
  178. #endif
  179. int i;
  180. for (i = 0; i < 3500; i++) {
  181. if (in_8(&com_port->lsr) & UART_LSR_THRE)
  182. break;
  183. udelay (100);
  184. }
  185. MFREG(UART0_SDR, reg);
  186. reg &= ~CR0_MASK;
  187. #ifdef CONFIG_SYS_EXT_SERIAL_CLOCK
  188. reg |= CR0_EXTCLK_ENA;
  189. udiv = 1;
  190. tmp = gd->baudrate * 16;
  191. bdiv = (CONFIG_SYS_EXT_SERIAL_CLOCK + tmp / 2) / tmp;
  192. #else
  193. /* For 440, the cpu clock is on divider chain A, UART on divider
  194. * chain B ... so cpu clock is irrelevant. Get the "optimized"
  195. * values that are subject to the 1/2 opb clock constraint
  196. */
  197. serial_divs (gd->baudrate, &udiv, &bdiv);
  198. #endif
  199. reg |= (udiv - UDIV_SUBTRACT) << CR0_UDIV_POS; /* set the UART divisor */
  200. /*
  201. * Configure input clock to baudrate generator for all
  202. * available serial ports here
  203. */
  204. MTREG(UART0_SDR, reg);
  205. #if defined(UART1_SDR)
  206. MTREG(UART1_SDR, reg);
  207. #endif
  208. #if defined(UART2_SDR)
  209. MTREG(UART2_SDR, reg);
  210. #endif
  211. #if defined(UART3_SDR)
  212. MTREG(UART3_SDR, reg);
  213. #endif
  214. uart_post_init_common(com_port, bdiv);
  215. return 0;
  216. }
  217. #else /* CONFIG_440 */
  218. static int uart_post_init (struct NS16550 *com_port)
  219. {
  220. unsigned long reg;
  221. unsigned long tmp;
  222. unsigned long clk;
  223. unsigned long udiv;
  224. unsigned short bdiv;
  225. int i;
  226. for (i = 0; i < 3500; i++) {
  227. if (in_8(&com_port->lsr) & UART_LSR_THRE)
  228. break;
  229. udelay (100);
  230. }
  231. #if defined(CONFIG_405EZ)
  232. serial_divs(gd->baudrate, &udiv, &bdiv);
  233. clk = tmp = reg = 0;
  234. #else
  235. #ifdef CONFIG_405EP
  236. reg = mfdcr(CPC0_UCR) & ~(UCR0_MASK | UCR1_MASK);
  237. clk = gd->cpu_clk;
  238. tmp = CONFIG_SYS_BASE_BAUD * 16;
  239. udiv = (clk + tmp / 2) / tmp;
  240. if (udiv > UDIV_MAX) /* max. n bits for udiv */
  241. udiv = UDIV_MAX;
  242. reg |= (udiv) << UCR0_UDIV_POS; /* set the UART divisor */
  243. reg |= (udiv) << UCR1_UDIV_POS; /* set the UART divisor */
  244. mtdcr (CPC0_UCR, reg);
  245. #else /* CONFIG_405EP */
  246. reg = mfdcr(CPC0_CR0) & ~CR0_MASK;
  247. #ifdef CONFIG_SYS_EXT_SERIAL_CLOCK
  248. clk = CONFIG_SYS_EXT_SERIAL_CLOCK;
  249. udiv = 1;
  250. reg |= CR0_EXTCLK_ENA;
  251. #else
  252. clk = gd->cpu_clk;
  253. #ifdef CONFIG_SYS_405_UART_ERRATA_59
  254. udiv = 31; /* Errata 59: stuck at 31 */
  255. #else
  256. tmp = CONFIG_SYS_BASE_BAUD * 16;
  257. udiv = (clk + tmp / 2) / tmp;
  258. if (udiv > UDIV_MAX) /* max. n bits for udiv */
  259. udiv = UDIV_MAX;
  260. #endif
  261. #endif
  262. reg |= (udiv - 1) << CR0_UDIV_POS; /* set the UART divisor */
  263. mtdcr (CPC0_CR0, reg);
  264. #endif /* CONFIG_405EP */
  265. tmp = gd->baudrate * udiv * 16;
  266. bdiv = (clk + tmp / 2) / tmp;
  267. #endif /* CONFIG_405EZ */
  268. uart_post_init_common(com_port, bdiv);
  269. return 0;
  270. }
  271. #endif /* CONFIG_440 */
  272. static void uart_post_putc (struct NS16550 *com_port, char c)
  273. {
  274. int i;
  275. out_8(&com_port->thr, c); /* put character out */
  276. /* Wait for transfer completion */
  277. for (i = 0; i < 3500; i++) {
  278. if (in_8(&com_port->lsr) & UART_LSR_THRE)
  279. break;
  280. udelay (100);
  281. }
  282. }
  283. static int uart_post_getc (struct NS16550 *com_port)
  284. {
  285. int i;
  286. /* Wait for character available */
  287. for (i = 0; i < 3500; i++) {
  288. if (in_8(&com_port->lsr) & UART_LSR_DR)
  289. break;
  290. udelay (100);
  291. }
  292. return 0xff & in_8(&com_port->rbr);
  293. }
  294. static int test_ctlr (struct NS16550 *com_port, int index)
  295. {
  296. int res = -1;
  297. char test_str[] = "*** UART Test String ***\r\n";
  298. int i;
  299. uart_post_init (com_port);
  300. for (i = 0; i < sizeof (test_str) - 1; i++) {
  301. uart_post_putc (com_port, test_str[i]);
  302. if (uart_post_getc (com_port) != test_str[i])
  303. goto done;
  304. }
  305. res = 0;
  306. done:
  307. if (res)
  308. post_log ("uart%d test failed\n", index);
  309. return res;
  310. }
  311. int uart_post_test (int flags)
  312. {
  313. int i, res = 0;
  314. static unsigned long base[] = CONFIG_SYS_POST_UART_TABLE;
  315. for (i = 0; i < ARRAY_SIZE(base); i++) {
  316. if (test_ctlr((struct NS16550 *)base[i], i))
  317. res = -1;
  318. }
  319. serial_reinit_all ();
  320. return res;
  321. }
  322. #endif /* CONFIG_POST & CONFIG_SYS_POST_UART */