uart.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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. /* Set the physical address of the host memory buffers in
  85. * the buffer descriptors.
  86. */
  87. #ifdef CONFIG_SYS_ALLOC_DPRAM
  88. dpaddr = dpram_alloc_align (sizeof (cbd_t) * 2 + 2, 8);
  89. #else
  90. dpaddr = CPM_POST_BASE;
  91. #endif
  92. /* Allocate space for two buffer descriptors in the DP ram.
  93. * For now, this address seems OK, but it may have to
  94. * change with newer versions of the firmware.
  95. * damm: allocating space after the two buffers for rx/tx data
  96. */
  97. rbdf = (cbd_t *) & cp->cp_dpmem[dpaddr];
  98. rbdf->cbd_bufaddr = (uint) (rbdf + 2);
  99. rbdf->cbd_sc = 0;
  100. tbdf = rbdf + 1;
  101. tbdf->cbd_bufaddr = ((uint) (rbdf + 2)) + 1;
  102. tbdf->cbd_sc = 0;
  103. /* Set up the uart parameters in the parameter ram.
  104. */
  105. up->smc_rbase = dpaddr;
  106. up->smc_tbase = dpaddr + sizeof (cbd_t);
  107. up->smc_rfcr = SMC_EB;
  108. up->smc_tfcr = SMC_EB;
  109. /* Set UART mode, 8 bit, no parity, one stop.
  110. * Enable receive and transmit.
  111. * Set local loopback mode.
  112. */
  113. sp->smc_smcmr = smcr_mk_clen (9) | SMCMR_SM_UART | (ushort) 0x0004;
  114. /* Mask all interrupts and remove anything pending.
  115. */
  116. sp->smc_smcm = 0;
  117. sp->smc_smce = 0xff;
  118. /* Set up the baud rate generator.
  119. */
  120. cp->cp_simode = 0x00000000;
  121. cp->cp_brgc1 =
  122. (((gd->cpu_clk / 16 / gd->baudrate) -
  123. 1) << 1) | CPM_BRG_EN;
  124. /* Make the first buffer the only buffer.
  125. */
  126. tbdf->cbd_sc |= BD_SC_WRAP;
  127. rbdf->cbd_sc |= BD_SC_EMPTY | BD_SC_WRAP;
  128. /* Single character receive.
  129. */
  130. up->smc_mrblr = 1;
  131. up->smc_maxidl = 0;
  132. /* Initialize Tx/Rx parameters.
  133. */
  134. while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
  135. ;
  136. cp->cp_cpcr =
  137. mk_cr_cmd (cpm_cr_ch[smc_index], CPM_CR_INIT_TRX) | CPM_CR_FLG;
  138. while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
  139. ;
  140. /* Enable transmitter/receiver.
  141. */
  142. sp->smc_smcmr |= SMCMR_REN | SMCMR_TEN;
  143. }
  144. static void smc_halt(int smc_index)
  145. {
  146. }
  147. static void smc_putc (int smc_index, const char c)
  148. {
  149. volatile cbd_t *tbdf;
  150. volatile char *buf;
  151. volatile smc_uart_t *up;
  152. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  153. volatile cpm8xx_t *cpmp = &(im->im_cpm);
  154. up = (smc_uart_t *) & cpmp->cp_dparam[proff_smc[smc_index]];
  155. tbdf = (cbd_t *) & cpmp->cp_dpmem[up->smc_tbase];
  156. /* Wait for last character to go.
  157. */
  158. buf = (char *) tbdf->cbd_bufaddr;
  159. #if 0
  160. __asm__ ("eieio");
  161. while (tbdf->cbd_sc & BD_SC_READY)
  162. __asm__ ("eieio");
  163. #endif
  164. *buf = c;
  165. tbdf->cbd_datlen = 1;
  166. tbdf->cbd_sc |= BD_SC_READY;
  167. __asm__ ("eieio");
  168. #if 1
  169. while (tbdf->cbd_sc & BD_SC_READY)
  170. __asm__ ("eieio");
  171. #endif
  172. }
  173. static int smc_getc (int smc_index)
  174. {
  175. volatile cbd_t *rbdf;
  176. volatile unsigned char *buf;
  177. volatile smc_uart_t *up;
  178. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  179. volatile cpm8xx_t *cpmp = &(im->im_cpm);
  180. unsigned char c;
  181. int i;
  182. up = (smc_uart_t *) & cpmp->cp_dparam[proff_smc[smc_index]];
  183. rbdf = (cbd_t *) & cpmp->cp_dpmem[up->smc_rbase];
  184. /* Wait for character to show up.
  185. */
  186. buf = (unsigned char *) rbdf->cbd_bufaddr;
  187. #if 0
  188. while (rbdf->cbd_sc & BD_SC_EMPTY);
  189. #else
  190. for (i = 100; i > 0; i--) {
  191. if (!(rbdf->cbd_sc & BD_SC_EMPTY))
  192. break;
  193. udelay (1000);
  194. }
  195. if (i == 0)
  196. return -1;
  197. #endif
  198. c = *buf;
  199. rbdf->cbd_sc |= BD_SC_EMPTY;
  200. return (c);
  201. }
  202. /*
  203. * SCC callbacks
  204. */
  205. static void scc_init (int scc_index)
  206. {
  207. static int cpm_cr_ch[] = {
  208. CPM_CR_CH_SCC1,
  209. CPM_CR_CH_SCC2,
  210. CPM_CR_CH_SCC3,
  211. CPM_CR_CH_SCC4,
  212. };
  213. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  214. volatile scc_t *sp;
  215. volatile scc_uart_t *up;
  216. volatile cbd_t *tbdf, *rbdf;
  217. volatile cpm8xx_t *cp = &(im->im_cpm);
  218. uint dpaddr;
  219. /* initialize pointers to SCC */
  220. sp = (scc_t *) & (cp->cp_scc[scc_index]);
  221. up = (scc_uart_t *) & cp->cp_dparam[proff_scc[scc_index]];
  222. /* Disable transmitter/receiver.
  223. */
  224. sp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
  225. /* Allocate space for two buffer descriptors in the DP ram.
  226. */
  227. #ifdef CONFIG_SYS_ALLOC_DPRAM
  228. dpaddr = dpram_alloc_align (sizeof (cbd_t) * 2 + 2, 8);
  229. #else
  230. dpaddr = CPM_POST_BASE;
  231. #endif
  232. /* Enable SDMA.
  233. */
  234. im->im_siu_conf.sc_sdcr = 0x0001;
  235. /* Set the physical address of the host memory buffers in
  236. * the buffer descriptors.
  237. */
  238. rbdf = (cbd_t *) & cp->cp_dpmem[dpaddr];
  239. rbdf->cbd_bufaddr = (uint) (rbdf + 2);
  240. rbdf->cbd_sc = 0;
  241. tbdf = rbdf + 1;
  242. tbdf->cbd_bufaddr = ((uint) (rbdf + 2)) + 1;
  243. tbdf->cbd_sc = 0;
  244. /* Set up the baud rate generator.
  245. */
  246. cp->cp_sicr &= ~(0x000000FF << (8 * scc_index));
  247. /* no |= needed, since BRG1 is 000 */
  248. cp->cp_brgc1 =
  249. (((gd->cpu_clk / 16 / gd->baudrate) -
  250. 1) << 1) | CPM_BRG_EN;
  251. /* Set up the uart parameters in the parameter ram.
  252. */
  253. up->scc_genscc.scc_rbase = dpaddr;
  254. up->scc_genscc.scc_tbase = dpaddr + sizeof (cbd_t);
  255. /* Initialize Tx/Rx parameters.
  256. */
  257. while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
  258. ;
  259. cp->cp_cpcr =
  260. mk_cr_cmd (cpm_cr_ch[scc_index], CPM_CR_INIT_TRX) | CPM_CR_FLG;
  261. while (cp->cp_cpcr & CPM_CR_FLG) /* wait if cp is busy */
  262. ;
  263. up->scc_genscc.scc_rfcr = SCC_EB | 0x05;
  264. up->scc_genscc.scc_tfcr = SCC_EB | 0x05;
  265. up->scc_genscc.scc_mrblr = 1; /* Single character receive */
  266. up->scc_maxidl = 0; /* disable max idle */
  267. up->scc_brkcr = 1; /* send one break character on stop TX */
  268. up->scc_parec = 0;
  269. up->scc_frmec = 0;
  270. up->scc_nosec = 0;
  271. up->scc_brkec = 0;
  272. up->scc_uaddr1 = 0;
  273. up->scc_uaddr2 = 0;
  274. up->scc_toseq = 0;
  275. up->scc_char1 = 0x8000;
  276. up->scc_char2 = 0x8000;
  277. up->scc_char3 = 0x8000;
  278. up->scc_char4 = 0x8000;
  279. up->scc_char5 = 0x8000;
  280. up->scc_char6 = 0x8000;
  281. up->scc_char7 = 0x8000;
  282. up->scc_char8 = 0x8000;
  283. up->scc_rccm = 0xc0ff;
  284. /* Set low latency / small fifo.
  285. */
  286. sp->scc_gsmrh = SCC_GSMRH_RFW;
  287. /* Set UART mode
  288. */
  289. sp->scc_gsmrl &= ~0xF;
  290. sp->scc_gsmrl |= SCC_GSMRL_MODE_UART;
  291. /* Set local loopback mode.
  292. */
  293. sp->scc_gsmrl &= ~SCC_GSMRL_DIAG_LE;
  294. sp->scc_gsmrl |= SCC_GSMRL_DIAG_LOOP;
  295. /* Set clock divider 16 on Tx and Rx
  296. */
  297. sp->scc_gsmrl |= (SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
  298. sp->scc_psmr |= SCU_PSMR_CL;
  299. /* Mask all interrupts and remove anything pending.
  300. */
  301. sp->scc_sccm = 0;
  302. sp->scc_scce = 0xffff;
  303. sp->scc_dsr = 0x7e7e;
  304. sp->scc_psmr = 0x3000;
  305. /* Make the first buffer the only buffer.
  306. */
  307. tbdf->cbd_sc |= BD_SC_WRAP;
  308. rbdf->cbd_sc |= BD_SC_EMPTY | BD_SC_WRAP;
  309. /* Enable transmitter/receiver.
  310. */
  311. sp->scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
  312. }
  313. static void scc_halt(int scc_index)
  314. {
  315. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  316. volatile cpm8xx_t *cp = &(im->im_cpm);
  317. volatile scc_t *sp = (scc_t *) & (cp->cp_scc[scc_index]);
  318. sp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT | SCC_GSMRL_DIAG_LE);
  319. }
  320. static void scc_putc (int scc_index, const char c)
  321. {
  322. volatile cbd_t *tbdf;
  323. volatile char *buf;
  324. volatile scc_uart_t *up;
  325. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  326. volatile cpm8xx_t *cpmp = &(im->im_cpm);
  327. up = (scc_uart_t *) & cpmp->cp_dparam[proff_scc[scc_index]];
  328. tbdf = (cbd_t *) & cpmp->cp_dpmem[up->scc_genscc.scc_tbase];
  329. /* Wait for last character to go.
  330. */
  331. buf = (char *) tbdf->cbd_bufaddr;
  332. #if 0
  333. __asm__ ("eieio");
  334. while (tbdf->cbd_sc & BD_SC_READY)
  335. __asm__ ("eieio");
  336. #endif
  337. *buf = c;
  338. tbdf->cbd_datlen = 1;
  339. tbdf->cbd_sc |= BD_SC_READY;
  340. __asm__ ("eieio");
  341. #if 1
  342. while (tbdf->cbd_sc & BD_SC_READY)
  343. __asm__ ("eieio");
  344. #endif
  345. }
  346. static int scc_getc (int scc_index)
  347. {
  348. volatile cbd_t *rbdf;
  349. volatile unsigned char *buf;
  350. volatile scc_uart_t *up;
  351. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  352. volatile cpm8xx_t *cpmp = &(im->im_cpm);
  353. unsigned char c;
  354. int i;
  355. up = (scc_uart_t *) & cpmp->cp_dparam[proff_scc[scc_index]];
  356. rbdf = (cbd_t *) & cpmp->cp_dpmem[up->scc_genscc.scc_rbase];
  357. /* Wait for character to show up.
  358. */
  359. buf = (unsigned char *) rbdf->cbd_bufaddr;
  360. #if 0
  361. while (rbdf->cbd_sc & BD_SC_EMPTY);
  362. #else
  363. for (i = 100; i > 0; i--) {
  364. if (!(rbdf->cbd_sc & BD_SC_EMPTY))
  365. break;
  366. udelay (1000);
  367. }
  368. if (i == 0)
  369. return -1;
  370. #endif
  371. c = *buf;
  372. rbdf->cbd_sc |= BD_SC_EMPTY;
  373. return (c);
  374. }
  375. /*
  376. * Test routines
  377. */
  378. static int test_ctlr (int ctlr, int index)
  379. {
  380. int res = -1;
  381. char test_str[] = "*** UART Test String ***\r\n";
  382. int i;
  383. ctlr_proc[ctlr].init (index);
  384. for (i = 0; i < sizeof (test_str) - 1; i++) {
  385. ctlr_proc[ctlr].putc (index, test_str[i]);
  386. if (ctlr_proc[ctlr].getc (index) != test_str[i])
  387. goto Done;
  388. }
  389. res = 0;
  390. Done:
  391. ctlr_proc[ctlr].halt (index);
  392. if (res != 0) {
  393. post_log ("uart %s%d test failed\n",
  394. ctlr_name[ctlr], index + 1);
  395. }
  396. return res;
  397. }
  398. int uart_post_test (int flags)
  399. {
  400. int res = 0;
  401. int i;
  402. ctlr_proc[CTLR_SMC].init = smc_init;
  403. ctlr_proc[CTLR_SMC].halt = smc_halt;
  404. ctlr_proc[CTLR_SMC].putc = smc_putc;
  405. ctlr_proc[CTLR_SMC].getc = smc_getc;
  406. ctlr_proc[CTLR_SCC].init = scc_init;
  407. ctlr_proc[CTLR_SCC].halt = scc_halt;
  408. ctlr_proc[CTLR_SCC].putc = scc_putc;
  409. ctlr_proc[CTLR_SCC].getc = scc_getc;
  410. for (i = 0; i < ARRAY_SIZE(ctlr_list); i++) {
  411. if (test_ctlr (ctlr_list[i][0], ctlr_list[i][1]) != 0) {
  412. res = -1;
  413. }
  414. }
  415. #if !defined(CONFIG_8xx_CONS_NONE)
  416. serial_reinit_all ();
  417. #endif
  418. return res;
  419. }
  420. #endif /* CONFIG_POST & CONFIG_SYS_POST_UART */