cmd_cm5200.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. * (C) Copyright 2007 Markus Kappeler <markus.kappeler@objectxp.com>
  3. *
  4. * Adapted for U-Boot 1.2 by Piotr Kruszynski <ppk@semihalf.com>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <command.h>
  10. #include <i2c.h>
  11. #include <usb.h>
  12. #ifdef CONFIG_CMD_BSP
  13. static int do_i2c_test(char * const argv[])
  14. {
  15. unsigned char temp, temp1;
  16. printf("Starting I2C Test\n"
  17. "Please set Jumper:\nI2C SDA 2-3\nI2C SCL 2-3\n\n"
  18. "Please press any key to start\n\n");
  19. getc();
  20. temp = 0xf0; /* set io 0-4 as output */
  21. i2c_write(CONFIG_SYS_I2C_IO, 3, 1, (uchar *)&temp, 1);
  22. printf("Press I2C4-7. LED I2C0-3 should have the same state\n\n"
  23. "Press any key to stop\n\n");
  24. while (!tstc()) {
  25. i2c_read(CONFIG_SYS_I2C_IO, 0, 1, (uchar *)&temp, 1);
  26. temp1 = (temp >> 4) & 0x03;
  27. temp1 |= (temp >> 3) & 0x08; /* S302 -> LED303 */
  28. temp1 |= (temp >> 5) & 0x04; /* S303 -> LED302 */
  29. temp = temp1;
  30. i2c_write(CONFIG_SYS_I2C_IO, 1, 1, (uchar *)&temp, 1);
  31. }
  32. getc();
  33. return 0;
  34. }
  35. static int do_usb_test(char * const argv[])
  36. {
  37. int i;
  38. static int usb_stor_curr_dev = -1; /* current device */
  39. printf("Starting USB Test\n"
  40. "Please insert USB Memmory Stick\n\n"
  41. "Please press any key to start\n\n");
  42. getc();
  43. usb_stop();
  44. printf("(Re)start USB...\n");
  45. i = usb_init();
  46. #ifdef CONFIG_USB_STORAGE
  47. /* try to recognize storage devices immediately */
  48. if (i >= 0)
  49. usb_stor_curr_dev = usb_stor_scan(1);
  50. #endif /* CONFIG_USB_STORAGE */
  51. if (usb_stor_curr_dev >= 0)
  52. printf("Found USB Storage Dev continue with Test...\n");
  53. else {
  54. printf("No USB Storage Device detected.. Stop Test\n");
  55. return 1;
  56. }
  57. usb_stor_info();
  58. printf("stopping USB..\n");
  59. usb_stop();
  60. return 0;
  61. }
  62. static int do_led_test(char * const argv[])
  63. {
  64. int i = 0;
  65. struct mpc5xxx_gpt_0_7 *gpt = (struct mpc5xxx_gpt_0_7 *)MPC5XXX_GPT;
  66. printf("Starting LED Test\n"
  67. "Please set Switch S500 all off\n\n"
  68. "Please press any key to start\n\n");
  69. getc();
  70. /* configure timer 2-3 for simple GPIO output High */
  71. gpt->gpt2.emsr |= 0x00000034;
  72. gpt->gpt3.emsr |= 0x00000034;
  73. (*(vu_long *)MPC5XXX_WU_GPIO_ENABLE) |= 0x80000000;
  74. (*(vu_long *)MPC5XXX_WU_GPIO_DIR) |= 0x80000000;
  75. printf("Please press any key to stop\n\n");
  76. while (!tstc()) {
  77. if (i == 1) {
  78. (*(vu_long *)MPC5XXX_WU_GPIO_DATA_O) |= 0x80000000;
  79. gpt->gpt2.emsr &= ~0x00000010;
  80. gpt->gpt3.emsr &= ~0x00000010;
  81. } else if (i == 2) {
  82. (*(vu_long *)MPC5XXX_WU_GPIO_DATA_O) &= ~0x80000000;
  83. gpt->gpt2.emsr &= ~0x00000010;
  84. gpt->gpt3.emsr |= 0x00000010;
  85. } else if (i >= 3) {
  86. (*(vu_long *)MPC5XXX_WU_GPIO_DATA_O) &= ~0x80000000;
  87. gpt->gpt3.emsr &= ~0x00000010;
  88. gpt->gpt2.emsr |= 0x00000010;
  89. i = 0;
  90. }
  91. i++;
  92. udelay(200000);
  93. }
  94. getc();
  95. (*(vu_long *)MPC5XXX_WU_GPIO_DATA_O) |= 0x80000000;
  96. gpt->gpt2.emsr |= 0x00000010;
  97. gpt->gpt3.emsr |= 0x00000010;
  98. return 0;
  99. }
  100. static int do_rs232_test(char * const argv[])
  101. {
  102. int error_status = 0;
  103. struct mpc5xxx_gpio *gpio = (struct mpc5xxx_gpio *)MPC5XXX_GPIO;
  104. struct mpc5xxx_psc *psc1 = (struct mpc5xxx_psc *)MPC5XXX_PSC1;
  105. /* Configure PSC 2-3-6 as GPIO */
  106. gpio->port_config &= 0xFF0FF80F;
  107. switch (simple_strtoul(argv[2], NULL, 10)) {
  108. case 1:
  109. /* check RTS <-> CTS loop */
  110. /* set rts to 0 */
  111. printf("Uart 1 test: RX TX tested by using U-Boot\n"
  112. "Please connect RTS with CTS on Uart1 plug\n\n"
  113. "Press any key to start\n\n");
  114. getc();
  115. psc1->op1 |= 0x01;
  116. /* wait some time before requesting status */
  117. udelay(10);
  118. /* check status at cts */
  119. if ((psc1->ip & 0x01) != 0) {
  120. error_status = 3;
  121. printf("%s: failure at rs232_1, cts status is %d "
  122. "(should be 0)\n",
  123. __FUNCTION__, (psc1->ip & 0x01));
  124. }
  125. /* set rts to 1 */
  126. psc1->op0 |= 0x01;
  127. /* wait some time before requesting status */
  128. udelay(10);
  129. /* check status at cts */
  130. if ((psc1->ip & 0x01) != 1) {
  131. error_status = 3;
  132. printf("%s: failure at rs232_1, cts status is %d "
  133. "(should be 1)\n",
  134. __FUNCTION__, (psc1->ip & 0x01));
  135. }
  136. break;
  137. case 2:
  138. /* set PSC2_0, PSC2_2 as output and PSC2_1, PSC2_3 as input */
  139. printf("Uart 2 test: Please use RS232 Loopback plug on UART2\n"
  140. "\nPress any key to start\n\n");
  141. getc();
  142. gpio->simple_gpioe &= ~(0x000000F0);
  143. gpio->simple_gpioe |= 0x000000F0;
  144. gpio->simple_ddr &= ~(0x000000F0);
  145. gpio->simple_ddr |= 0x00000050;
  146. /* check TXD <-> RXD loop */
  147. /* set TXD to 1 */
  148. gpio->simple_dvo |= (1 << 4);
  149. /* wait some time before requesting status */
  150. udelay(10);
  151. if ((gpio->simple_ival & 0x00000020) != 0x00000020) {
  152. error_status = 2;
  153. printf("%s: failure at rs232_2, rxd status is %d "
  154. "(should be 1)\n", __FUNCTION__,
  155. (gpio->simple_ival & 0x00000020) >> 5);
  156. }
  157. /* set TXD to 0 */
  158. gpio->simple_dvo &= ~(1 << 4);
  159. /* wait some time before requesting status */
  160. udelay(10);
  161. if ((gpio->simple_ival & 0x00000020) != 0x00000000) {
  162. error_status = 2;
  163. printf("%s: failure at rs232_2, rxd status is %d "
  164. "(should be 0)\n", __FUNCTION__,
  165. (gpio->simple_ival & 0x00000020) >> 5);
  166. }
  167. /* check RTS <-> CTS loop */
  168. /* set RTS to 1 */
  169. gpio->simple_dvo |= (1 << 6);
  170. /* wait some time before requesting status */
  171. udelay(10);
  172. if ((gpio->simple_ival & 0x00000080) != 0x00000080) {
  173. error_status = 3;
  174. printf("%s: failure at rs232_2, cts status is %d "
  175. "(should be 1)\n", __FUNCTION__,
  176. (gpio->simple_ival & 0x00000080) >> 7);
  177. }
  178. /* set RTS to 0 */
  179. gpio->simple_dvo &= ~(1 << 6);
  180. /* wait some time before requesting status */
  181. udelay(10);
  182. if ((gpio->simple_ival & 0x00000080) != 0x00000000) {
  183. error_status = 3;
  184. printf("%s: failure at rs232_2, cts status is %d "
  185. "(should be 0)\n", __FUNCTION__,
  186. (gpio->simple_ival & 0x00000080) >> 7);
  187. }
  188. break;
  189. case 3:
  190. /* set PSC3_0, PSC3_2 as output and PSC3_1, PSC3_3 as input */
  191. printf("Uart 3 test: Please use RS232 Loopback plug on UART2\n"
  192. "\nPress any key to start\n\n");
  193. getc();
  194. gpio->simple_gpioe &= ~(0x00000F00);
  195. gpio->simple_gpioe |= 0x00000F00;
  196. gpio->simple_ddr &= ~(0x00000F00);
  197. gpio->simple_ddr |= 0x00000500;
  198. /* check TXD <-> RXD loop */
  199. /* set TXD to 1 */
  200. gpio->simple_dvo |= (1 << 8);
  201. /* wait some time before requesting status */
  202. udelay(10);
  203. if ((gpio->simple_ival & 0x00000200) != 0x00000200) {
  204. error_status = 2;
  205. printf("%s: failure at rs232_3, rxd status is %d "
  206. "(should be 1)\n", __FUNCTION__,
  207. (gpio->simple_ival & 0x00000200) >> 9);
  208. }
  209. /* set TXD to 0 */
  210. gpio->simple_dvo &= ~(1 << 8);
  211. /* wait some time before requesting status */
  212. udelay(10);
  213. if ((gpio->simple_ival & 0x00000200) != 0x00000000) {
  214. error_status = 2;
  215. printf("%s: failure at rs232_3, rxd status is %d "
  216. "(should be 0)\n", __FUNCTION__,
  217. (gpio->simple_ival & 0x00000200) >> 9);
  218. }
  219. /* check RTS <-> CTS loop */
  220. /* set RTS to 1 */
  221. gpio->simple_dvo |= (1 << 10);
  222. /* wait some time before requesting status */
  223. udelay(10);
  224. if ((gpio->simple_ival & 0x00000800) != 0x00000800) {
  225. error_status = 3;
  226. printf("%s: failure at rs232_3, cts status is %d "
  227. "(should be 1)\n", __FUNCTION__,
  228. (gpio->simple_ival & 0x00000800) >> 11);
  229. }
  230. /* set RTS to 0 */
  231. gpio->simple_dvo &= ~(1 << 10);
  232. /* wait some time before requesting status */
  233. udelay(10);
  234. if ((gpio->simple_ival & 0x00000800) != 0x00000000) {
  235. error_status = 3;
  236. printf("%s: failure at rs232_3, cts status is %d "
  237. "(should be 0)\n", __FUNCTION__,
  238. (gpio->simple_ival & 0x00000800) >> 11);
  239. }
  240. break;
  241. case 4:
  242. /* set PSC6_2, PSC6_3 as output and PSC6_0, PSC6_1 as input */
  243. printf("Uart 4 test: Please use RS232 Loopback plug on UART2\n"
  244. "\nPress any key to start\n\n");
  245. getc();
  246. gpio->simple_gpioe &= ~(0xF0000000);
  247. gpio->simple_gpioe |= 0x30000000;
  248. gpio->simple_ddr &= ~(0xf0000000);
  249. gpio->simple_ddr |= 0x30000000;
  250. (*(vu_long *)MPC5XXX_WU_GPIO_ENABLE) |= 0x30000000;
  251. (*(vu_long *)MPC5XXX_WU_GPIO_DIR) &= ~(0x30000000);
  252. /* check TXD <-> RXD loop */
  253. /* set TXD to 1 */
  254. gpio->simple_dvo |= (1 << 28);
  255. /* wait some time before requesting status */
  256. udelay(10);
  257. if (((*(vu_long *)MPC5XXX_WU_GPIO_DATA_I) & 0x10000000) !=
  258. 0x10000000) {
  259. error_status = 2;
  260. printf("%s: failure at rs232_4, rxd status is %lu "
  261. "(should be 1)\n", __FUNCTION__,
  262. ((*(vu_long *)MPC5XXX_WU_GPIO_DATA_I) &
  263. 0x10000000) >> 28);
  264. }
  265. /* set TXD to 0 */
  266. gpio->simple_dvo &= ~(1 << 28);
  267. /* wait some time before requesting status */
  268. udelay(10);
  269. if (((*(vu_long *)MPC5XXX_WU_GPIO_DATA_I) & 0x10000000) !=
  270. 0x00000000) {
  271. error_status = 2;
  272. printf("%s: failure at rs232_4, rxd status is %lu "
  273. "(should be 0)\n", __FUNCTION__,
  274. ((*(vu_long *)MPC5XXX_WU_GPIO_DATA_I) &
  275. 0x10000000) >> 28);
  276. }
  277. /* check RTS <-> CTS loop */
  278. /* set RTS to 1 */
  279. gpio->simple_dvo |= (1 << 29);
  280. /* wait some time before requesting status */
  281. udelay(10);
  282. if (((*(vu_long *)MPC5XXX_WU_GPIO_DATA_I) & 0x20000000) !=
  283. 0x20000000) {
  284. error_status = 3;
  285. printf("%s: failure at rs232_4, cts status is %lu "
  286. "(should be 1)\n", __FUNCTION__,
  287. ((*(vu_long *)MPC5XXX_WU_GPIO_DATA_I) &
  288. 0x20000000) >> 29);
  289. }
  290. /* set RTS to 0 */
  291. gpio->simple_dvo &= ~(1 << 29);
  292. /* wait some time before requesting status */
  293. udelay(10);
  294. if (((*(vu_long *)MPC5XXX_WU_GPIO_DATA_I) & 0x20000000) !=
  295. 0x00000000) {
  296. error_status = 3;
  297. printf("%s: failure at rs232_4, cts status is %lu "
  298. "(should be 0)\n", __FUNCTION__,
  299. ((*(vu_long *)MPC5XXX_WU_GPIO_DATA_I) &
  300. 0x20000000) >> 29);
  301. }
  302. break;
  303. default:
  304. printf("%s: invalid rs232 number %s\n", __FUNCTION__, argv[2]);
  305. error_status = 1;
  306. break;
  307. }
  308. gpio->port_config |= (CONFIG_SYS_GPS_PORT_CONFIG & 0xFF0FF80F);
  309. return error_status;
  310. }
  311. static int cmd_fkt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  312. {
  313. int rcode = -1;
  314. switch (argc) {
  315. case 2:
  316. if (strncmp(argv[1], "i2c", 3) == 0)
  317. rcode = do_i2c_test(argv);
  318. else if (strncmp(argv[1], "led", 3) == 0)
  319. rcode = do_led_test(argv);
  320. else if (strncmp(argv[1], "usb", 3) == 0)
  321. rcode = do_usb_test(argv);
  322. break;
  323. case 3:
  324. if (strncmp(argv[1], "rs232", 3) == 0)
  325. rcode = do_rs232_test(argv);
  326. break;
  327. }
  328. switch (rcode) {
  329. case -1:
  330. printf("Usage:\n"
  331. "fkt { i2c | led | usb }\n"
  332. "fkt rs232 number\n");
  333. rcode = 1;
  334. break;
  335. case 0:
  336. printf("Test passed\n");
  337. break;
  338. default:
  339. printf("Test failed with code: %d\n", rcode);
  340. }
  341. return rcode;
  342. }
  343. U_BOOT_CMD(
  344. fkt, 4, 1, cmd_fkt,
  345. "Function test routines",
  346. "i2c\n"
  347. " - Test I2C communication\n"
  348. "fkt led\n"
  349. " - Test LEDs\n"
  350. "fkt rs232 number\n"
  351. " - Test RS232 (loopback plug(s) for RS232 required)\n"
  352. "fkt usb\n"
  353. " - Test USB communication"
  354. );
  355. #endif /* CONFIG_CMD_BSP */