uart.c 11 KB

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