inkadiag.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * (C) Copyright 2008, 2009 Andreas Pfefferle,
  3. * DENX Software Engineering, ap@denx.de.
  4. * (C) Copyright 2009 Detlev Zundel,
  5. * DENX Software Engineering, dzu@denx.de.
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <asm/io.h>
  10. #include <common.h>
  11. #include <config.h>
  12. #include <mpc5xxx.h>
  13. #include <pci.h>
  14. #include <command.h>
  15. /* This is needed for the includes in ns16550.h */
  16. #define CONFIG_SYS_NS16550_REG_SIZE 1
  17. #include <ns16550.h>
  18. #define GPIO_BASE ((u_char *)CONFIG_SYS_CS3_START)
  19. #define DIGIN_TOUCHSCR_MASK 0x00003000 /* Inputs 12-13 */
  20. #define DIGIN_KEYB_MASK 0x00010000 /* Input 16 */
  21. #define DIGIN_DRAWER_SW1 0x00400000 /* Input 22 */
  22. #define DIGIN_DRAWER_SW2 0x00800000 /* Input 23 */
  23. #define DIGIO_LED0 0x00000001 /* Output 0 */
  24. #define DIGIO_LED1 0x00000002 /* Output 1 */
  25. #define DIGIO_LED2 0x00000004 /* Output 2 */
  26. #define DIGIO_LED3 0x00000008 /* Output 3 */
  27. #define DIGIO_LED4 0x00000010 /* Output 4 */
  28. #define DIGIO_LED5 0x00000020 /* Output 5 */
  29. #define DIGIO_DRAWER1 0x00000100 /* Output 8 */
  30. #define DIGIO_DRAWER2 0x00000200 /* Output 9 */
  31. #define SERIAL_PORT_BASE ((u_char *)CONFIG_SYS_CS2_START)
  32. #define PSC_OP1_RTS 0x01
  33. #define PSC_OP0_RTS 0x01
  34. /*
  35. * Table with supported baudrates (defined in inka4x0.h)
  36. */
  37. static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
  38. #define N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0]))
  39. static unsigned int inka_digin_get_input(void)
  40. {
  41. return in_8(GPIO_BASE + 0) << 0 | in_8(GPIO_BASE + 1) << 8 |
  42. in_8(GPIO_BASE + 2) << 16 | in_8(GPIO_BASE + 3) << 24;
  43. }
  44. #define LED_HIGH(NUM) \
  45. do { \
  46. setbits_be32((unsigned *)MPC5XXX_GPT##NUM##_ENABLE, 0x10); \
  47. } while (0)
  48. #define LED_LOW(NUM) \
  49. do { \
  50. clrbits_be32((unsigned *)MPC5XXX_GPT##NUM##_ENABLE, 0x10); \
  51. } while (0)
  52. #define CHECK_LED(NUM) \
  53. do { \
  54. if (state & (1 << NUM)) { \
  55. LED_HIGH(NUM); \
  56. } else { \
  57. LED_LOW(NUM); \
  58. } \
  59. } while (0)
  60. static void inka_digio_set_output(unsigned int state, int which)
  61. {
  62. volatile struct mpc5xxx_gpio *gpio = (struct mpc5xxx_gpio *)MPC5XXX_GPIO;
  63. if (which == 0) {
  64. /* other */
  65. CHECK_LED(0);
  66. CHECK_LED(1);
  67. CHECK_LED(2);
  68. CHECK_LED(3);
  69. CHECK_LED(4);
  70. CHECK_LED(5);
  71. } else {
  72. if (which == 1) {
  73. /* drawer1 */
  74. if (state) {
  75. clrbits_be32(&gpio->simple_dvo, 0x1000);
  76. udelay(1);
  77. setbits_be32(&gpio->simple_dvo, 0x1000);
  78. } else {
  79. setbits_be32(&gpio->simple_dvo, 0x1000);
  80. udelay(1);
  81. clrbits_be32(&gpio->simple_dvo, 0x1000);
  82. }
  83. }
  84. if (which == 2) {
  85. /* drawer 2 */
  86. if (state) {
  87. clrbits_be32(&gpio->simple_dvo, 0x2000);
  88. udelay(1);
  89. setbits_be32(&gpio->simple_dvo, 0x2000);
  90. } else {
  91. setbits_be32(&gpio->simple_dvo, 0x2000);
  92. udelay(1);
  93. clrbits_be32(&gpio->simple_dvo, 0x2000);
  94. }
  95. }
  96. }
  97. udelay(1);
  98. }
  99. static int do_inkadiag_io(cmd_tbl_t *cmdtp, int flag, int argc,
  100. char * const argv[]) {
  101. unsigned int state, val;
  102. switch (argc) {
  103. case 3:
  104. /* Write a value */
  105. val = simple_strtol(argv[2], NULL, 16);
  106. if (strcmp(argv[1], "drawer1") == 0) {
  107. inka_digio_set_output(val, 1);
  108. } else if (strcmp(argv[1], "drawer2") == 0) {
  109. inka_digio_set_output(val, 2);
  110. } else if (strcmp(argv[1], "other") == 0)
  111. inka_digio_set_output(val, 0);
  112. else {
  113. printf("Invalid argument: %s\n", argv[1]);
  114. return -1;
  115. }
  116. /* fall through */
  117. case 2:
  118. /* Read a value */
  119. state = inka_digin_get_input();
  120. if (strcmp(argv[1], "drawer1") == 0) {
  121. val = (state & DIGIN_DRAWER_SW1) >> (ffs(DIGIN_DRAWER_SW1) - 1);
  122. } else if (strcmp(argv[1], "drawer2") == 0) {
  123. val = (state & DIGIN_DRAWER_SW2) >> (ffs(DIGIN_DRAWER_SW2) - 1);
  124. } else if (strcmp(argv[1], "other") == 0) {
  125. val = ((state & DIGIN_KEYB_MASK) >> (ffs(DIGIN_KEYB_MASK) - 1))
  126. | (state & DIGIN_TOUCHSCR_MASK) >> (ffs(DIGIN_TOUCHSCR_MASK) - 2);
  127. } else {
  128. printf("Invalid argument: %s\n", argv[1]);
  129. return -1;
  130. }
  131. printf("exit code: 0x%X\n", val);
  132. return 0;
  133. default:
  134. return cmd_usage(cmdtp);
  135. }
  136. return -1;
  137. }
  138. DECLARE_GLOBAL_DATA_PTR;
  139. static int ser_init(volatile struct mpc5xxx_psc *psc, int baudrate)
  140. {
  141. unsigned long baseclk;
  142. int div;
  143. /* reset PSC */
  144. out_8(&psc->command, PSC_SEL_MODE_REG_1);
  145. /* select clock sources */
  146. out_be16(&psc->psc_clock_select, 0);
  147. baseclk = (gd->arch.ipb_clk + 16) / 32;
  148. /* switch to UART mode */
  149. out_be32(&psc->sicr, 0);
  150. /* configure parity, bit length and so on */
  151. out_8(&psc->mode, PSC_MODE_8_BITS | PSC_MODE_PARNONE);
  152. out_8(&psc->mode, PSC_MODE_ONE_STOP);
  153. /* set up UART divisor */
  154. div = (baseclk + (baudrate / 2)) / baudrate;
  155. out_8(&psc->ctur, (div >> 8) & 0xff);
  156. out_8(&psc->ctlr, div & 0xff);
  157. /* disable all interrupts */
  158. out_be16(&psc->psc_imr, 0);
  159. /* reset and enable Rx/Tx */
  160. out_8(&psc->command, PSC_RST_RX);
  161. out_8(&psc->command, PSC_RST_TX);
  162. out_8(&psc->command, PSC_RX_ENABLE | PSC_TX_ENABLE);
  163. return 0;
  164. }
  165. static void ser_putc(volatile struct mpc5xxx_psc *psc, const char c)
  166. {
  167. /* Wait 1 second for last character to go. */
  168. int i = 0;
  169. while (!(psc->psc_status & PSC_SR_TXEMP) && (i++ < 1000000/10))
  170. udelay(10);
  171. psc->psc_buffer_8 = c;
  172. }
  173. static int ser_getc(volatile struct mpc5xxx_psc *psc)
  174. {
  175. /* Wait for a character to arrive. */
  176. int i = 0;
  177. while (!(in_be16(&psc->psc_status) & PSC_SR_RXRDY) && (i++ < 1000000/10))
  178. udelay(10);
  179. return in_8(&psc->psc_buffer_8);
  180. }
  181. static int do_inkadiag_serial(cmd_tbl_t *cmdtp, int flag, int argc,
  182. char * const argv[]) {
  183. volatile struct NS16550 *uart;
  184. volatile struct mpc5xxx_psc *psc;
  185. unsigned int num, mode;
  186. int combrd, baudrate, i, j, len;
  187. int address;
  188. if (argc < 5)
  189. return cmd_usage(cmdtp);
  190. argc--;
  191. argv++;
  192. num = simple_strtol(argv[0], NULL, 0);
  193. if (num < 0 || num > 11) {
  194. printf("invalid argument for num: %d\n", num);
  195. return -1;
  196. }
  197. mode = simple_strtol(argv[1], NULL, 0);
  198. combrd = 0;
  199. baudrate = simple_strtoul(argv[2], NULL, 10);
  200. for (i=0; i<N_BAUDRATES; ++i) {
  201. if (baudrate == baudrate_table[i])
  202. break;
  203. }
  204. if (i == N_BAUDRATES) {
  205. printf("## Baudrate %d bps not supported\n",
  206. baudrate);
  207. return 1;
  208. }
  209. combrd = 115200 / baudrate;
  210. uart = (struct NS16550 *)(SERIAL_PORT_BASE + (num << 3));
  211. printf("Testing uart %d.\n\n", num);
  212. if ((num >= 0) && (num <= 7)) {
  213. if (mode & 1) {
  214. /* turn on 'loopback' mode */
  215. out_8(&uart->mcr, UART_MCR_LOOP);
  216. } else {
  217. /*
  218. * establish the UART's operational parameters
  219. * set DLAB=1, so rbr accesses DLL
  220. */
  221. out_8(&uart->lcr, UART_LCR_DLAB);
  222. /* set baudrate */
  223. out_8(&uart->rbr, combrd);
  224. /* set data-format: 8-N-1 */
  225. out_8(&uart->lcr, UART_LCR_WLS_8);
  226. }
  227. if (mode & 2) {
  228. /* set request to send */
  229. out_8(&uart->mcr, UART_MCR_RTS);
  230. udelay(10);
  231. /* check clear to send */
  232. if ((in_8(&uart->msr) & UART_MSR_CTS) == 0x00)
  233. return -1;
  234. }
  235. if (mode & 4) {
  236. /* set data terminal ready */
  237. out_8(&uart->mcr, UART_MCR_DTR);
  238. udelay(10);
  239. /* check data set ready and carrier detect */
  240. if ((in_8(&uart->msr) & (UART_MSR_DSR | UART_MSR_DCD))
  241. != (UART_MSR_DSR | UART_MSR_DCD))
  242. return -1;
  243. }
  244. /* write each message-character, read it back, and display it */
  245. for (i = 0, len = strlen(argv[3]); i < len; ++i) {
  246. j = 0;
  247. while ((in_8(&uart->lsr) & UART_LSR_THRE) == 0x00) {
  248. if (j++ > CONFIG_SYS_HZ)
  249. break;
  250. udelay(10);
  251. }
  252. out_8(&uart->rbr, argv[3][i]);
  253. j = 0;
  254. while ((in_8(&uart->lsr) & UART_LSR_DR) == 0x00) {
  255. if (j++ > CONFIG_SYS_HZ)
  256. break;
  257. udelay(10);
  258. }
  259. printf("%c", in_8(&uart->rbr));
  260. }
  261. printf("\n\n");
  262. out_8(&uart->mcr, 0x00);
  263. } else {
  264. address = 0;
  265. switch (num) {
  266. case 8:
  267. address = MPC5XXX_PSC6;
  268. break;
  269. case 9:
  270. address = MPC5XXX_PSC3;
  271. break;
  272. case 10:
  273. address = MPC5XXX_PSC2;
  274. break;
  275. case 11:
  276. address = MPC5XXX_PSC1;
  277. break;
  278. }
  279. psc = (struct mpc5xxx_psc *)address;
  280. ser_init(psc, simple_strtol(argv[2], NULL, 0));
  281. if (mode & 2) {
  282. /* set request to send */
  283. out_8(&psc->op0, PSC_OP0_RTS);
  284. udelay(10);
  285. /* check clear to send */
  286. if ((in_8(&psc->ip) & PSC_IPCR_CTS) == 0)
  287. return -1;
  288. }
  289. len = strlen(argv[3]);
  290. for (i = 0; i < len; ++i) {
  291. ser_putc(psc, argv[3][i]);
  292. printf("%c", ser_getc(psc));
  293. }
  294. printf("\n\n");
  295. }
  296. return 0;
  297. }
  298. #define BUZZER_GPT (MPC5XXX_GPT + 0x60) /* GPT6 */
  299. static void buzzer_turn_on(unsigned int freq)
  300. {
  301. volatile struct mpc5xxx_gpt *gpt = (struct mpc5xxx_gpt *)(BUZZER_GPT);
  302. const u32 prescale = gd->arch.ipb_clk / freq / 128;
  303. const u32 count = 128;
  304. const u32 width = 64;
  305. gpt->cir = (prescale << 16) | count;
  306. gpt->pwmcr = width << 16;
  307. gpt->emsr = 3; /* Timer enabled for PWM */
  308. }
  309. static void buzzer_turn_off(void)
  310. {
  311. volatile struct mpc5xxx_gpt *gpt = (struct mpc5xxx_gpt *)(BUZZER_GPT);
  312. gpt->emsr = 0;
  313. }
  314. static int do_inkadiag_buzzer(cmd_tbl_t *cmdtp, int flag, int argc,
  315. char * const argv[]) {
  316. unsigned int period, freq;
  317. int prev, i;
  318. if (argc != 3)
  319. return cmd_usage(cmdtp);
  320. argc--;
  321. argv++;
  322. period = simple_strtol(argv[0], NULL, 0);
  323. if (!period)
  324. printf("Zero period is senseless\n");
  325. argc--;
  326. argv++;
  327. freq = simple_strtol(argv[0], NULL, 0);
  328. /* avoid zero prescale in buzzer_turn_on() */
  329. if (freq > gd->arch.ipb_clk / 128) {
  330. printf("%dHz exceeds maximum (%ldHz)\n", freq,
  331. gd->arch.ipb_clk / 128);
  332. } else if (!freq)
  333. printf("Zero frequency is senseless\n");
  334. else
  335. buzzer_turn_on(freq);
  336. clear_ctrlc();
  337. prev = disable_ctrlc(0);
  338. printf("Buzzing for %d ms. Type ^C to abort!\n\n", period);
  339. i = 0;
  340. while (!ctrlc() && (i++ < CONFIG_SYS_HZ))
  341. udelay(period);
  342. clear_ctrlc();
  343. disable_ctrlc(prev);
  344. buzzer_turn_off();
  345. return 0;
  346. }
  347. static int do_inkadiag_help(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  348. cmd_tbl_t cmd_inkadiag_sub[] = {
  349. U_BOOT_CMD_MKENT(io, 1, 1, do_inkadiag_io, "read digital input",
  350. "<drawer1|drawer2|other> [value] - get or set specified signal"),
  351. U_BOOT_CMD_MKENT(serial, 4, 1, do_inkadiag_serial, "test serial port",
  352. "<num> <mode> <baudrate> <msg> - test uart num [0..11] in mode\n"
  353. "and baudrate with msg"),
  354. U_BOOT_CMD_MKENT(buzzer, 2, 1, do_inkadiag_buzzer, "activate buzzer",
  355. "<period> <freq> - turn buzzer on for period ms with freq hz"),
  356. U_BOOT_CMD_MKENT(help, 4, 1, do_inkadiag_help, "get help",
  357. "[command] - get help for command"),
  358. };
  359. static int do_inkadiag_help(cmd_tbl_t *cmdtp, int flag,
  360. int argc, char * const argv[]) {
  361. extern int _do_help (cmd_tbl_t *cmd_start, int cmd_items,
  362. cmd_tbl_t *cmdtp, int flag,
  363. int argc, char * const argv[]);
  364. /* do_help prints command name - we prepend inkadiag to our subcommands! */
  365. #ifdef CONFIG_SYS_LONGHELP
  366. puts ("inkadiag ");
  367. #endif
  368. return _do_help(&cmd_inkadiag_sub[0],
  369. ARRAY_SIZE(cmd_inkadiag_sub), cmdtp, flag, argc, argv);
  370. }
  371. static int do_inkadiag(cmd_tbl_t *cmdtp, int flag, int argc,
  372. char * const argv[]) {
  373. cmd_tbl_t *c;
  374. c = find_cmd_tbl(argv[1], &cmd_inkadiag_sub[0], ARRAY_SIZE(cmd_inkadiag_sub));
  375. if (c) {
  376. argc--;
  377. argv++;
  378. return c->cmd(c, flag, argc, argv);
  379. } else {
  380. /* Unrecognized command */
  381. return cmd_usage(cmdtp);
  382. }
  383. }
  384. U_BOOT_CMD(inkadiag, 6, 1, do_inkadiag,
  385. "inkadiag - inka diagnosis\n",
  386. "[inkadiag what ...]\n"
  387. " - perform a diagnosis on inka hardware\n"
  388. "'inkadiag' performs hardware tests.");