uart.c 11 KB

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