uart.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /*
  2. * (C) Copyright 2002
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. /*
  25. * UART test
  26. *
  27. * The Serial Management Controllers (SMC) and the Serial Communication
  28. * Controllers (SCC) listed in ctlr_list array below are tested in
  29. * the loopback UART mode.
  30. * The controllers are configured accordingly and several characters
  31. * are transmitted. The configurable test parameters are:
  32. * MIN_PACKET_LENGTH - minimum size of packet to transmit
  33. * MAX_PACKET_LENGTH - maximum size of packet to transmit
  34. * TEST_NUM - number of tests
  35. */
  36. #include <post.h>
  37. #if CONFIG_POST & CONFIG_SYS_POST_UART
  38. #if defined(CONFIG_8xx)
  39. #include <commproc.h>
  40. #elif defined(CONFIG_MPC8260)
  41. #include <asm/cpm_8260.h>
  42. #else
  43. #error "Apparently a bad configuration, please fix."
  44. #endif
  45. #include <command.h>
  46. #include <serial.h>
  47. DECLARE_GLOBAL_DATA_PTR;
  48. #define CTLR_SMC 0
  49. #define CTLR_SCC 1
  50. /* The list of controllers to test */
  51. #if defined(CONFIG_MPC823)
  52. static int ctlr_list[][2] =
  53. { {CTLR_SMC, 0}, {CTLR_SMC, 1}, {CTLR_SCC, 1} };
  54. #else
  55. static int ctlr_list[][2] = { };
  56. #endif
  57. static struct {
  58. void (*init) (int index);
  59. void (*halt) (int index);
  60. void (*putc) (int index, const char c);
  61. int (*getc) (int index);
  62. } ctlr_proc[2];
  63. static char *ctlr_name[2] = { "SMC", "SCC" };
  64. static int proff_smc[] = { PROFF_SMC1, PROFF_SMC2 };
  65. static int proff_scc[] =
  66. { PROFF_SCC1, PROFF_SCC2, PROFF_SCC3, PROFF_SCC4 };
  67. /*
  68. * SMC callbacks
  69. */
  70. static void smc_init (int smc_index)
  71. {
  72. static int cpm_cr_ch[] = { CPM_CR_CH_SMC1, CPM_CR_CH_SMC2 };
  73. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  74. volatile smc_t *sp;
  75. volatile smc_uart_t *up;
  76. volatile cbd_t *tbdf, *rbdf;
  77. volatile cpm8xx_t *cp = &(im->im_cpm);
  78. uint dpaddr;
  79. /* initialize pointers to SMC */
  80. sp = (smc_t *) & (cp->cp_smc[smc_index]);
  81. up = (smc_uart_t *) & cp->cp_dparam[proff_smc[smc_index]];
  82. /* Disable transmitter/receiver.
  83. */
  84. sp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
  85. /* Enable SDMA.
  86. */
  87. im->im_siu_conf.sc_sdcr = 1;
  88. /* clear error conditions */
  89. #ifdef CONFIG_SYS_SDSR
  90. im->im_sdma.sdma_sdsr = CONFIG_SYS_SDSR;
  91. #else
  92. im->im_sdma.sdma_sdsr = 0x83;
  93. #endif
  94. /* clear SDMA interrupt mask */
  95. #ifdef CONFIG_SYS_SDMR
  96. im->im_sdma.sdma_sdmr = CONFIG_SYS_SDMR;
  97. #else
  98. im->im_sdma.sdma_sdmr = 0x00;
  99. #endif
  100. #if defined(CONFIG_FADS)
  101. /* Enable RS232 */
  102. *((uint *) BCSR1) &=
  103. ~(smc_index == 1 ? BCSR1_RS232EN_1 : BCSR1_RS232EN_2);
  104. #endif
  105. #if defined(CONFIG_RPXLITE) || defined(CONFIG_RPXCLASSIC)
  106. /* Enable Monitor Port Transceiver */
  107. *((uchar *) BCSR0) |= BCSR0_ENMONXCVR;
  108. #endif
  109. /* Set the physical address of the host memory buffers in
  110. * the buffer descriptors.
  111. */
  112. #ifdef CONFIG_SYS_ALLOC_DPRAM
  113. dpaddr = dpram_alloc_align (sizeof (cbd_t) * 2 + 2, 8);
  114. #else
  115. dpaddr = CPM_POST_BASE;
  116. #endif
  117. /* Allocate space for two buffer descriptors in the DP ram.
  118. * For now, this address seems OK, but it may have to
  119. * change with newer versions of the firmware.
  120. * damm: allocating space after the two buffers for rx/tx data
  121. */
  122. rbdf = (cbd_t *) & cp->cp_dpmem[dpaddr];
  123. rbdf->cbd_bufaddr = (uint) (rbdf + 2);
  124. rbdf->cbd_sc = 0;
  125. tbdf = rbdf + 1;
  126. tbdf->cbd_bufaddr = ((uint) (rbdf + 2)) + 1;
  127. tbdf->cbd_sc = 0;
  128. /* Set up the uart parameters in the parameter ram.
  129. */
  130. up->smc_rbase = dpaddr;
  131. up->smc_tbase = dpaddr + sizeof (cbd_t);
  132. up->smc_rfcr = SMC_EB;
  133. up->smc_tfcr = SMC_EB;
  134. #if defined(CONFIG_MBX)
  135. board_serial_init ();
  136. #endif
  137. /* Set UART mode, 8 bit, no parity, one stop.
  138. * Enable receive and transmit.
  139. * Set local loopback mode.
  140. */
  141. sp->smc_smcmr = smcr_mk_clen (9) | SMCMR_SM_UART | (ushort) 0x0004;
  142. /* Mask all interrupts and remove anything pending.
  143. */
  144. sp->smc_smcm = 0;
  145. sp->smc_smce = 0xff;
  146. /* Set up the baud rate generator.
  147. */
  148. cp->cp_simode = 0x00000000;
  149. cp->cp_brgc1 =
  150. (((gd->cpu_clk / 16 / gd->baudrate) -
  151. 1) << 1) | CPM_BRG_EN;
  152. /* Make the first buffer the only buffer.
  153. */
  154. tbdf->cbd_sc |= BD_SC_WRAP;
  155. rbdf->cbd_sc |= BD_SC_EMPTY | BD_SC_WRAP;
  156. /* Single character receive.
  157. */
  158. up->smc_mrblr = 1;
  159. up->smc_maxidl = 0;
  160. /* Initialize Tx/Rx parameters.
  161. */
  162. while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
  163. ;
  164. cp->cp_cpcr =
  165. mk_cr_cmd (cpm_cr_ch[smc_index], CPM_CR_INIT_TRX) | CPM_CR_FLG;
  166. while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
  167. ;
  168. /* Enable transmitter/receiver.
  169. */
  170. sp->smc_smcmr |= SMCMR_REN | SMCMR_TEN;
  171. }
  172. static void smc_halt(int smc_index)
  173. {
  174. }
  175. static void smc_putc (int smc_index, const char c)
  176. {
  177. volatile cbd_t *tbdf;
  178. volatile char *buf;
  179. volatile smc_uart_t *up;
  180. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  181. volatile cpm8xx_t *cpmp = &(im->im_cpm);
  182. up = (smc_uart_t *) & cpmp->cp_dparam[proff_smc[smc_index]];
  183. tbdf = (cbd_t *) & cpmp->cp_dpmem[up->smc_tbase];
  184. /* Wait for last character to go.
  185. */
  186. buf = (char *) tbdf->cbd_bufaddr;
  187. #if 0
  188. __asm__ ("eieio");
  189. while (tbdf->cbd_sc & BD_SC_READY)
  190. __asm__ ("eieio");
  191. #endif
  192. *buf = c;
  193. tbdf->cbd_datlen = 1;
  194. tbdf->cbd_sc |= BD_SC_READY;
  195. __asm__ ("eieio");
  196. #if 1
  197. while (tbdf->cbd_sc & BD_SC_READY)
  198. __asm__ ("eieio");
  199. #endif
  200. }
  201. static int smc_getc (int smc_index)
  202. {
  203. volatile cbd_t *rbdf;
  204. volatile unsigned char *buf;
  205. volatile smc_uart_t *up;
  206. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  207. volatile cpm8xx_t *cpmp = &(im->im_cpm);
  208. unsigned char c;
  209. int i;
  210. up = (smc_uart_t *) & cpmp->cp_dparam[proff_smc[smc_index]];
  211. rbdf = (cbd_t *) & cpmp->cp_dpmem[up->smc_rbase];
  212. /* Wait for character to show up.
  213. */
  214. buf = (unsigned char *) rbdf->cbd_bufaddr;
  215. #if 0
  216. while (rbdf->cbd_sc & BD_SC_EMPTY);
  217. #else
  218. for (i = 100; i > 0; i--) {
  219. if (!(rbdf->cbd_sc & BD_SC_EMPTY))
  220. break;
  221. udelay (1000);
  222. }
  223. if (i == 0)
  224. return -1;
  225. #endif
  226. c = *buf;
  227. rbdf->cbd_sc |= BD_SC_EMPTY;
  228. return (c);
  229. }
  230. /*
  231. * SCC callbacks
  232. */
  233. static void scc_init (int scc_index)
  234. {
  235. static int cpm_cr_ch[] = {
  236. CPM_CR_CH_SCC1,
  237. CPM_CR_CH_SCC2,
  238. CPM_CR_CH_SCC3,
  239. CPM_CR_CH_SCC4,
  240. };
  241. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  242. volatile scc_t *sp;
  243. volatile scc_uart_t *up;
  244. volatile cbd_t *tbdf, *rbdf;
  245. volatile cpm8xx_t *cp = &(im->im_cpm);
  246. uint dpaddr;
  247. /* initialize pointers to SCC */
  248. sp = (scc_t *) & (cp->cp_scc[scc_index]);
  249. up = (scc_uart_t *) & cp->cp_dparam[proff_scc[scc_index]];
  250. /* Disable transmitter/receiver.
  251. */
  252. sp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
  253. /* Allocate space for two buffer descriptors in the DP ram.
  254. */
  255. #ifdef CONFIG_SYS_ALLOC_DPRAM
  256. dpaddr = dpram_alloc_align (sizeof (cbd_t) * 2 + 2, 8);
  257. #else
  258. dpaddr = CPM_POST_BASE;
  259. #endif
  260. /* Enable SDMA.
  261. */
  262. im->im_siu_conf.sc_sdcr = 0x0001;
  263. /* Set the physical address of the host memory buffers in
  264. * the buffer descriptors.
  265. */
  266. rbdf = (cbd_t *) & cp->cp_dpmem[dpaddr];
  267. rbdf->cbd_bufaddr = (uint) (rbdf + 2);
  268. rbdf->cbd_sc = 0;
  269. tbdf = rbdf + 1;
  270. tbdf->cbd_bufaddr = ((uint) (rbdf + 2)) + 1;
  271. tbdf->cbd_sc = 0;
  272. /* Set up the baud rate generator.
  273. */
  274. cp->cp_sicr &= ~(0x000000FF << (8 * scc_index));
  275. /* no |= needed, since BRG1 is 000 */
  276. cp->cp_brgc1 =
  277. (((gd->cpu_clk / 16 / gd->baudrate) -
  278. 1) << 1) | CPM_BRG_EN;
  279. /* Set up the uart parameters in the parameter ram.
  280. */
  281. up->scc_genscc.scc_rbase = dpaddr;
  282. up->scc_genscc.scc_tbase = dpaddr + sizeof (cbd_t);
  283. /* Initialize Tx/Rx parameters.
  284. */
  285. while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
  286. ;
  287. cp->cp_cpcr =
  288. mk_cr_cmd (cpm_cr_ch[scc_index], CPM_CR_INIT_TRX) | CPM_CR_FLG;
  289. while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
  290. ;
  291. up->scc_genscc.scc_rfcr = SCC_EB | 0x05;
  292. up->scc_genscc.scc_tfcr = SCC_EB | 0x05;
  293. up->scc_genscc.scc_mrblr = 1; /* Single character receive */
  294. up->scc_maxidl = 0; /* disable max idle */
  295. up->scc_brkcr = 1; /* send one break character on stop TX */
  296. up->scc_parec = 0;
  297. up->scc_frmec = 0;
  298. up->scc_nosec = 0;
  299. up->scc_brkec = 0;
  300. up->scc_uaddr1 = 0;
  301. up->scc_uaddr2 = 0;
  302. up->scc_toseq = 0;
  303. up->scc_char1 = 0x8000;
  304. up->scc_char2 = 0x8000;
  305. up->scc_char3 = 0x8000;
  306. up->scc_char4 = 0x8000;
  307. up->scc_char5 = 0x8000;
  308. up->scc_char6 = 0x8000;
  309. up->scc_char7 = 0x8000;
  310. up->scc_char8 = 0x8000;
  311. up->scc_rccm = 0xc0ff;
  312. /* Set low latency / small fifo.
  313. */
  314. sp->scc_gsmrh = SCC_GSMRH_RFW;
  315. /* Set UART mode
  316. */
  317. sp->scc_gsmrl &= ~0xF;
  318. sp->scc_gsmrl |= SCC_GSMRL_MODE_UART;
  319. /* Set local loopback mode.
  320. */
  321. sp->scc_gsmrl &= ~SCC_GSMRL_DIAG_LE;
  322. sp->scc_gsmrl |= SCC_GSMRL_DIAG_LOOP;
  323. /* Set clock divider 16 on Tx and Rx
  324. */
  325. sp->scc_gsmrl |= (SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
  326. sp->scc_psmr |= SCU_PSMR_CL;
  327. /* Mask all interrupts and remove anything pending.
  328. */
  329. sp->scc_sccm = 0;
  330. sp->scc_scce = 0xffff;
  331. sp->scc_dsr = 0x7e7e;
  332. sp->scc_psmr = 0x3000;
  333. /* Make the first buffer the only buffer.
  334. */
  335. tbdf->cbd_sc |= BD_SC_WRAP;
  336. rbdf->cbd_sc |= BD_SC_EMPTY | BD_SC_WRAP;
  337. /* Enable transmitter/receiver.
  338. */
  339. sp->scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
  340. }
  341. static void scc_halt(int scc_index)
  342. {
  343. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  344. volatile cpm8xx_t *cp = &(im->im_cpm);
  345. volatile scc_t *sp = (scc_t *) & (cp->cp_scc[scc_index]);
  346. sp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT | SCC_GSMRL_DIAG_LE);
  347. }
  348. static void scc_putc (int scc_index, const char c)
  349. {
  350. volatile cbd_t *tbdf;
  351. volatile char *buf;
  352. volatile scc_uart_t *up;
  353. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  354. volatile cpm8xx_t *cpmp = &(im->im_cpm);
  355. up = (scc_uart_t *) & cpmp->cp_dparam[proff_scc[scc_index]];
  356. tbdf = (cbd_t *) & cpmp->cp_dpmem[up->scc_genscc.scc_tbase];
  357. /* Wait for last character to go.
  358. */
  359. buf = (char *) tbdf->cbd_bufaddr;
  360. #if 0
  361. __asm__ ("eieio");
  362. while (tbdf->cbd_sc & BD_SC_READY)
  363. __asm__ ("eieio");
  364. #endif
  365. *buf = c;
  366. tbdf->cbd_datlen = 1;
  367. tbdf->cbd_sc |= BD_SC_READY;
  368. __asm__ ("eieio");
  369. #if 1
  370. while (tbdf->cbd_sc & BD_SC_READY)
  371. __asm__ ("eieio");
  372. #endif
  373. }
  374. static int scc_getc (int scc_index)
  375. {
  376. volatile cbd_t *rbdf;
  377. volatile unsigned char *buf;
  378. volatile scc_uart_t *up;
  379. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  380. volatile cpm8xx_t *cpmp = &(im->im_cpm);
  381. unsigned char c;
  382. int i;
  383. up = (scc_uart_t *) & cpmp->cp_dparam[proff_scc[scc_index]];
  384. rbdf = (cbd_t *) & cpmp->cp_dpmem[up->scc_genscc.scc_rbase];
  385. /* Wait for character to show up.
  386. */
  387. buf = (unsigned char *) rbdf->cbd_bufaddr;
  388. #if 0
  389. while (rbdf->cbd_sc & BD_SC_EMPTY);
  390. #else
  391. for (i = 100; i > 0; i--) {
  392. if (!(rbdf->cbd_sc & BD_SC_EMPTY))
  393. break;
  394. udelay (1000);
  395. }
  396. if (i == 0)
  397. return -1;
  398. #endif
  399. c = *buf;
  400. rbdf->cbd_sc |= BD_SC_EMPTY;
  401. return (c);
  402. }
  403. /*
  404. * Test routines
  405. */
  406. static int test_ctlr (int ctlr, int index)
  407. {
  408. int res = -1;
  409. char test_str[] = "*** UART Test String ***\r\n";
  410. int i;
  411. ctlr_proc[ctlr].init (index);
  412. for (i = 0; i < sizeof (test_str) - 1; i++) {
  413. ctlr_proc[ctlr].putc (index, test_str[i]);
  414. if (ctlr_proc[ctlr].getc (index) != test_str[i])
  415. goto Done;
  416. }
  417. res = 0;
  418. Done:
  419. ctlr_proc[ctlr].halt (index);
  420. if (res != 0) {
  421. post_log ("uart %s%d test failed\n",
  422. ctlr_name[ctlr], index + 1);
  423. }
  424. return res;
  425. }
  426. int uart_post_test (int flags)
  427. {
  428. int res = 0;
  429. int i;
  430. ctlr_proc[CTLR_SMC].init = smc_init;
  431. ctlr_proc[CTLR_SMC].halt = smc_halt;
  432. ctlr_proc[CTLR_SMC].putc = smc_putc;
  433. ctlr_proc[CTLR_SMC].getc = smc_getc;
  434. ctlr_proc[CTLR_SCC].init = scc_init;
  435. ctlr_proc[CTLR_SCC].halt = scc_halt;
  436. ctlr_proc[CTLR_SCC].putc = scc_putc;
  437. ctlr_proc[CTLR_SCC].getc = scc_getc;
  438. for (i = 0; i < ARRAY_SIZE(ctlr_list); i++) {
  439. if (test_ctlr (ctlr_list[i][0], ctlr_list[i][1]) != 0) {
  440. res = -1;
  441. }
  442. }
  443. #if !defined(CONFIG_8xx_CONS_NONE)
  444. serial_reinit_all ();
  445. #endif
  446. return res;
  447. }
  448. #endif /* CONFIG_POST & CONFIG_SYS_POST_UART */