inkadiag.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 * const 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. return cmd_usage(cmdtp);
  151. }
  152. return -1;
  153. }
  154. DECLARE_GLOBAL_DATA_PTR;
  155. static int ser_init(volatile struct mpc5xxx_psc *psc, int baudrate)
  156. {
  157. unsigned long baseclk;
  158. int div;
  159. /* reset PSC */
  160. out_8(&psc->command, PSC_SEL_MODE_REG_1);
  161. /* select clock sources */
  162. out_be16(&psc->psc_clock_select, 0);
  163. baseclk = (gd->ipb_clk + 16) / 32;
  164. /* switch to UART mode */
  165. out_be32(&psc->sicr, 0);
  166. /* configure parity, bit length and so on */
  167. out_8(&psc->mode, PSC_MODE_8_BITS | PSC_MODE_PARNONE);
  168. out_8(&psc->mode, PSC_MODE_ONE_STOP);
  169. /* set up UART divisor */
  170. div = (baseclk + (baudrate / 2)) / baudrate;
  171. out_8(&psc->ctur, (div >> 8) & 0xff);
  172. out_8(&psc->ctlr, div & 0xff);
  173. /* disable all interrupts */
  174. out_be16(&psc->psc_imr, 0);
  175. /* reset and enable Rx/Tx */
  176. out_8(&psc->command, PSC_RST_RX);
  177. out_8(&psc->command, PSC_RST_TX);
  178. out_8(&psc->command, PSC_RX_ENABLE | PSC_TX_ENABLE);
  179. return 0;
  180. }
  181. static void ser_putc(volatile struct mpc5xxx_psc *psc, const char c)
  182. {
  183. /* Wait 1 second for last character to go. */
  184. int i = 0;
  185. while (!(psc->psc_status & PSC_SR_TXEMP) && (i++ < 1000000/10))
  186. udelay(10);
  187. psc->psc_buffer_8 = c;
  188. }
  189. static int ser_getc(volatile struct mpc5xxx_psc *psc)
  190. {
  191. /* Wait for a character to arrive. */
  192. int i = 0;
  193. while (!(in_be16(&psc->psc_status) & PSC_SR_RXRDY) && (i++ < 1000000/10))
  194. udelay(10);
  195. return in_8(&psc->psc_buffer_8);
  196. }
  197. static int do_inkadiag_serial(cmd_tbl_t *cmdtp, int flag, int argc,
  198. char * const argv[]) {
  199. volatile struct NS16550 *uart;
  200. volatile struct mpc5xxx_psc *psc;
  201. unsigned int num, mode;
  202. int combrd, baudrate, i, j, len;
  203. int address;
  204. if (argc < 5)
  205. return cmd_usage(cmdtp);
  206. argc--;
  207. argv++;
  208. num = simple_strtol(argv[0], NULL, 0);
  209. if (num < 0 || num > 11) {
  210. printf("invalid argument for num: %d\n", num);
  211. return -1;
  212. }
  213. mode = simple_strtol(argv[1], NULL, 0);
  214. combrd = 0;
  215. baudrate = simple_strtoul(argv[2], NULL, 10);
  216. for (i=0; i<N_BAUDRATES; ++i) {
  217. if (baudrate == baudrate_table[i])
  218. break;
  219. }
  220. if (i == N_BAUDRATES) {
  221. printf("## Baudrate %d bps not supported\n",
  222. baudrate);
  223. return 1;
  224. }
  225. combrd = 115200 / baudrate;
  226. uart = (struct NS16550 *)(SERIAL_PORT_BASE + (num << 3));
  227. printf("Testing uart %d.\n\n", num);
  228. if ((num >= 0) && (num <= 7)) {
  229. if (mode & 1) {
  230. /* turn on 'loopback' mode */
  231. out_8(&uart->mcr, UART_MCR_LOOP);
  232. } else {
  233. /*
  234. * establish the UART's operational parameters
  235. * set DLAB=1, so rbr accesses DLL
  236. */
  237. out_8(&uart->lcr, UART_LCR_DLAB);
  238. /* set baudrate */
  239. out_8(&uart->rbr, combrd);
  240. /* set data-format: 8-N-1 */
  241. out_8(&uart->lcr, UART_LCR_WLS_8);
  242. }
  243. if (mode & 2) {
  244. /* set request to send */
  245. out_8(&uart->mcr, UART_MCR_RTS);
  246. udelay(10);
  247. /* check clear to send */
  248. if ((in_8(&uart->msr) & UART_MSR_CTS) == 0x00)
  249. return -1;
  250. }
  251. if (mode & 4) {
  252. /* set data terminal ready */
  253. out_8(&uart->mcr, UART_MCR_DTR);
  254. udelay(10);
  255. /* check data set ready and carrier detect */
  256. if ((in_8(&uart->msr) & (UART_MSR_DSR | UART_MSR_DCD))
  257. != (UART_MSR_DSR | UART_MSR_DCD))
  258. return -1;
  259. }
  260. /* write each message-character, read it back, and display it */
  261. for (i = 0, len = strlen(argv[3]); i < len; ++i) {
  262. j = 0;
  263. while ((in_8(&uart->lsr) & UART_LSR_THRE) == 0x00) {
  264. if (j++ > CONFIG_SYS_HZ)
  265. break;
  266. udelay(10);
  267. }
  268. out_8(&uart->rbr, argv[3][i]);
  269. j = 0;
  270. while ((in_8(&uart->lsr) & UART_LSR_DR) == 0x00) {
  271. if (j++ > CONFIG_SYS_HZ)
  272. break;
  273. udelay(10);
  274. }
  275. printf("%c", in_8(&uart->rbr));
  276. }
  277. printf("\n\n");
  278. out_8(&uart->mcr, 0x00);
  279. } else {
  280. address = 0;
  281. switch (num) {
  282. case 8:
  283. address = MPC5XXX_PSC6;
  284. break;
  285. case 9:
  286. address = MPC5XXX_PSC3;
  287. break;
  288. case 10:
  289. address = MPC5XXX_PSC2;
  290. break;
  291. case 11:
  292. address = MPC5XXX_PSC1;
  293. break;
  294. }
  295. psc = (struct mpc5xxx_psc *)address;
  296. ser_init(psc, simple_strtol(argv[2], NULL, 0));
  297. if (mode & 2) {
  298. /* set request to send */
  299. out_8(&psc->op0, PSC_OP0_RTS);
  300. udelay(10);
  301. /* check clear to send */
  302. if ((in_8(&psc->ip) & PSC_IPCR_CTS) == 0)
  303. return -1;
  304. }
  305. len = strlen(argv[3]);
  306. for (i = 0; i < len; ++i) {
  307. ser_putc(psc, argv[3][i]);
  308. printf("%c", ser_getc(psc));
  309. }
  310. printf("\n\n");
  311. }
  312. return 0;
  313. }
  314. #define BUZZER_GPT (MPC5XXX_GPT + 0x60) /* GPT6 */
  315. static void buzzer_turn_on(unsigned int freq)
  316. {
  317. volatile struct mpc5xxx_gpt *gpt = (struct mpc5xxx_gpt *)(BUZZER_GPT);
  318. const u32 prescale = gd->ipb_clk / freq / 128;
  319. const u32 count = 128;
  320. const u32 width = 64;
  321. gpt->cir = (prescale << 16) | count;
  322. gpt->pwmcr = width << 16;
  323. gpt->emsr = 3; /* Timer enabled for PWM */
  324. }
  325. static void buzzer_turn_off(void)
  326. {
  327. volatile struct mpc5xxx_gpt *gpt = (struct mpc5xxx_gpt *)(BUZZER_GPT);
  328. gpt->emsr = 0;
  329. }
  330. static int do_inkadiag_buzzer(cmd_tbl_t *cmdtp, int flag, int argc,
  331. char * const argv[]) {
  332. unsigned int period, freq;
  333. int prev, i;
  334. if (argc != 3)
  335. return cmd_usage(cmdtp);
  336. argc--;
  337. argv++;
  338. period = simple_strtol(argv[0], NULL, 0);
  339. if (!period)
  340. printf("Zero period is senseless\n");
  341. argc--;
  342. argv++;
  343. freq = simple_strtol(argv[0], NULL, 0);
  344. /* avoid zero prescale in buzzer_turn_on() */
  345. if (freq > gd->ipb_clk / 128) {
  346. printf("%dHz exceeds maximum (%ldHz)\n", freq,
  347. gd->ipb_clk / 128);
  348. } else if (!freq)
  349. printf("Zero frequency is senseless\n");
  350. else
  351. buzzer_turn_on(freq);
  352. clear_ctrlc();
  353. prev = disable_ctrlc(0);
  354. printf("Buzzing for %d ms. Type ^C to abort!\n\n", period);
  355. i = 0;
  356. while (!ctrlc() && (i++ < CONFIG_SYS_HZ))
  357. udelay(period);
  358. clear_ctrlc();
  359. disable_ctrlc(prev);
  360. buzzer_turn_off();
  361. return 0;
  362. }
  363. static int do_inkadiag_help(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
  364. cmd_tbl_t cmd_inkadiag_sub[] = {
  365. U_BOOT_CMD_MKENT(io, 1, 1, do_inkadiag_io, "read digital input",
  366. "<drawer1|drawer2|other> [value] - get or set specified signal"),
  367. U_BOOT_CMD_MKENT(serial, 4, 1, do_inkadiag_serial, "test serial port",
  368. "<num> <mode> <baudrate> <msg> - test uart num [0..11] in mode\n"
  369. "and baudrate with msg"),
  370. U_BOOT_CMD_MKENT(buzzer, 2, 1, do_inkadiag_buzzer, "activate buzzer",
  371. "<period> <freq> - turn buzzer on for period ms with freq hz"),
  372. U_BOOT_CMD_MKENT(help, 4, 1, do_inkadiag_help, "get help",
  373. "[command] - get help for command"),
  374. };
  375. static int do_inkadiag_help(cmd_tbl_t *cmdtp, int flag,
  376. int argc, char * const argv[]) {
  377. extern int _do_help (cmd_tbl_t *cmd_start, int cmd_items,
  378. cmd_tbl_t *cmdtp, int flag,
  379. int argc, char * const argv[]);
  380. /* do_help prints command name - we prepend inkadiag to our subcommands! */
  381. #ifdef CONFIG_SYS_LONGHELP
  382. puts ("inkadiag ");
  383. #endif
  384. return _do_help(&cmd_inkadiag_sub[0],
  385. ARRAY_SIZE(cmd_inkadiag_sub), cmdtp, flag, argc, argv);
  386. }
  387. static int do_inkadiag(cmd_tbl_t *cmdtp, int flag, int argc,
  388. char * const argv[]) {
  389. cmd_tbl_t *c;
  390. c = find_cmd_tbl(argv[1], &cmd_inkadiag_sub[0], ARRAY_SIZE(cmd_inkadiag_sub));
  391. if (c) {
  392. argc--;
  393. argv++;
  394. return c->cmd(c, flag, argc, argv);
  395. } else {
  396. /* Unrecognized command */
  397. return cmd_usage(cmdtp);
  398. }
  399. }
  400. U_BOOT_CMD(inkadiag, 6, 1, do_inkadiag,
  401. "inkadiag - inka diagnosis\n",
  402. "[inkadiag what ...]\n"
  403. " - perform a diagnosis on inka hardware\n"
  404. "'inkadiag' performs hardware tests.");