inkadiag.c 11 KB

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