uart.c 13 KB

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