uart.c 12 KB

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