inkadiag.c 12 KB

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