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.
7 * See file CREDITS for list of people who contributed to this
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.
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.
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,
34 /* This is needed for the includes in ns16550.h */
35 #define CONFIG_SYS_NS16550_REG_SIZE 1
38 #define GPIO_BASE ((u_char *)CONFIG_SYS_CS3_START)
40 #define DIGIN_TOUCHSCR_MASK 0x00003000 /* Inputs 12-13 */
41 #define DIGIN_KEYB_MASK 0x00010000 /* Input 16 */
43 #define DIGIN_DRAWER_SW1 0x00400000 /* Input 22 */
44 #define DIGIN_DRAWER_SW2 0x00800000 /* Input 23 */
46 #define DIGIO_LED0 0x00000001 /* Output 0 */
47 #define DIGIO_LED1 0x00000002 /* Output 1 */
48 #define DIGIO_LED2 0x00000004 /* Output 2 */
49 #define DIGIO_LED3 0x00000008 /* Output 3 */
50 #define DIGIO_LED4 0x00000010 /* Output 4 */
51 #define DIGIO_LED5 0x00000020 /* Output 5 */
53 #define DIGIO_DRAWER1 0x00000100 /* Output 8 */
54 #define DIGIO_DRAWER2 0x00000200 /* Output 9 */
56 #define SERIAL_PORT_BASE ((u_char *)CONFIG_SYS_CS2_START)
58 #define PSC_OP1_RTS 0x01
59 #define PSC_OP0_RTS 0x01
62 * Table with supported baudrates (defined in inka4x0.h)
64 static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
65 #define N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0]))
67 static unsigned int inka_digin_get_input(void)
69 return in_8(GPIO_BASE + 0) << 0 | in_8(GPIO_BASE + 1) << 8 |
70 in_8(GPIO_BASE + 2) << 16 | in_8(GPIO_BASE + 3) << 24;
73 #define LED_HIGH(NUM) \
75 setbits_be32((unsigned *)MPC5XXX_GPT##NUM##_ENABLE, 0x10); \
78 #define LED_LOW(NUM) \
80 clrbits_be32((unsigned *)MPC5XXX_GPT##NUM##_ENABLE, 0x10); \
83 #define CHECK_LED(NUM) \
85 if (state & (1 << NUM)) { \
92 static void inka_digio_set_output(unsigned int state, int which)
94 volatile struct mpc5xxx_gpio *gpio = (struct mpc5xxx_gpio *)MPC5XXX_GPIO;
108 clrbits_be32(&gpio->simple_dvo, 0x1000);
110 setbits_be32(&gpio->simple_dvo, 0x1000);
112 setbits_be32(&gpio->simple_dvo, 0x1000);
114 clrbits_be32(&gpio->simple_dvo, 0x1000);
120 clrbits_be32(&gpio->simple_dvo, 0x2000);
122 setbits_be32(&gpio->simple_dvo, 0x2000);
124 setbits_be32(&gpio->simple_dvo, 0x2000);
126 clrbits_be32(&gpio->simple_dvo, 0x2000);
133 static int do_inkadiag_io(cmd_tbl_t *cmdtp, int flag, int argc,
135 unsigned int state, val;
140 val = simple_strtol(argv[2], NULL, 16);
142 if (strcmp(argv[1], "drawer1") == 0) {
143 inka_digio_set_output(val, 1);
144 } else if (strcmp(argv[1], "drawer2") == 0) {
145 inka_digio_set_output(val, 2);
146 } else if (strcmp(argv[1], "other") == 0)
147 inka_digio_set_output(val, 0);
149 printf("Invalid argument: %s\n", argv[1]);
155 state = inka_digin_get_input();
157 if (strcmp(argv[1], "drawer1") == 0) {
158 val = (state & DIGIN_DRAWER_SW1) >> (ffs(DIGIN_DRAWER_SW1) - 1);
159 } else if (strcmp(argv[1], "drawer2") == 0) {
160 val = (state & DIGIN_DRAWER_SW2) >> (ffs(DIGIN_DRAWER_SW2) - 1);
161 } else if (strcmp(argv[1], "other") == 0) {
162 val = ((state & DIGIN_KEYB_MASK) >> (ffs(DIGIN_KEYB_MASK) - 1))
163 | (state & DIGIN_TOUCHSCR_MASK) >> (ffs(DIGIN_TOUCHSCR_MASK) - 2);
165 printf("Invalid argument: %s\n", argv[1]);
168 printf("exit code: 0x%X\n", val);
178 DECLARE_GLOBAL_DATA_PTR;
180 static int ser_init(volatile struct mpc5xxx_psc *psc, int baudrate)
182 unsigned long baseclk;
186 out_8(&psc->command, PSC_SEL_MODE_REG_1);
188 /* select clock sources */
190 out_be16(&psc->psc_clock_select, 0);
191 baseclk = (gd->ipb_clk + 16) / 32;
193 /* switch to UART mode */
194 out_be32(&psc->sicr, 0);
196 /* configure parity, bit length and so on */
198 out_8(&psc->mode, PSC_MODE_8_BITS | PSC_MODE_PARNONE);
199 out_8(&psc->mode, PSC_MODE_ONE_STOP);
201 /* set up UART divisor */
202 div = (baseclk + (baudrate / 2)) / baudrate;
203 out_8(&psc->ctur, (div >> 8) & 0xff);
204 out_8(&psc->ctlr, div & 0xff);
206 /* disable all interrupts */
207 out_be16(&psc->psc_imr, 0);
209 /* reset and enable Rx/Tx */
210 out_8(&psc->command, PSC_RST_RX);
211 out_8(&psc->command, PSC_RST_TX);
212 out_8(&psc->command, PSC_RX_ENABLE | PSC_TX_ENABLE);
217 static void ser_putc(volatile struct mpc5xxx_psc *psc, const char c)
219 /* Wait 1 second for last character to go. */
222 while (!(psc->psc_status & PSC_SR_TXEMP) && (i++ < 1000000/10))
224 psc->psc_buffer_8 = c;
228 static int ser_getc(volatile struct mpc5xxx_psc *psc)
230 /* Wait for a character to arrive. */
233 while (!(in_be16(&psc->psc_status) & PSC_SR_RXRDY) && (i++ < 1000000/10))
236 return in_8(&psc->psc_buffer_8);
239 static int do_inkadiag_serial(cmd_tbl_t *cmdtp, int flag, int argc,
241 volatile struct NS16550 *uart;
242 volatile struct mpc5xxx_psc *psc;
243 unsigned int num, mode;
244 int combrd, baudrate, i, j, len;
255 num = simple_strtol(argv[0], NULL, 0);
256 if (num < 0 || num > 11) {
257 printf("invalid argument for num: %d\n", num);
261 mode = simple_strtol(argv[1], NULL, 0);
264 baudrate = simple_strtoul(argv[2], NULL, 10);
265 for (i=0; i<N_BAUDRATES; ++i) {
266 if (baudrate == baudrate_table[i])
269 if (i == N_BAUDRATES) {
270 printf("## Baudrate %d bps not supported\n",
274 combrd = 115200 / baudrate;
276 uart = (struct NS16550 *)(SERIAL_PORT_BASE + (num << 3));
278 printf("Testing uart %d.\n\n", num);
280 if ((num >= 0) && (num <= 7)) {
282 /* turn on 'loopback' mode */
283 out_8(&uart->mcr, UART_MCR_LOOP);
286 * establish the UART's operational parameters
287 * set DLAB=1, so rbr accesses DLL
289 out_8(&uart->lcr, UART_LCR_DLAB);
291 out_8(&uart->rbr, combrd);
292 /* set data-format: 8-N-1 */
293 out_8(&uart->lcr, UART_LCR_WLS_8);
297 /* set request to send */
298 out_8(&uart->mcr, UART_MCR_RTS);
300 /* check clear to send */
301 if ((in_8(&uart->msr) & UART_MSR_CTS) == 0x00)
305 /* set data terminal ready */
306 out_8(&uart->mcr, UART_MCR_DTR);
308 /* check data set ready and carrier detect */
309 if ((in_8(&uart->msr) & (UART_MSR_DSR | UART_MSR_DCD))
310 != (UART_MSR_DSR | UART_MSR_DCD))
314 /* write each message-character, read it back, and display it */
315 for (i = 0, len = strlen(argv[3]); i < len; ++i) {
317 while ((in_8(&uart->lsr) & UART_LSR_THRE) == 0x00) {
318 if (j++ > CONFIG_SYS_HZ)
322 out_8(&uart->rbr, argv[3][i]);
324 while ((in_8(&uart->lsr) & UART_LSR_DR) == 0x00) {
325 if (j++ > CONFIG_SYS_HZ)
329 printf("%c", in_8(&uart->rbr));
332 out_8(&uart->mcr, 0x00);
338 address = MPC5XXX_PSC6;
341 address = MPC5XXX_PSC3;
344 address = MPC5XXX_PSC2;
347 address = MPC5XXX_PSC1;
350 psc = (struct mpc5xxx_psc *)address;
351 ser_init(psc, simple_strtol(argv[2], NULL, 0));
353 /* set request to send */
354 out_8(&psc->op0, PSC_OP0_RTS);
356 /* check clear to send */
357 if ((in_8(&psc->ip) & PSC_IPCR_CTS) == 0)
360 len = strlen(argv[3]);
361 for (i = 0; i < len; ++i) {
362 ser_putc(psc, argv[3][i]);
363 printf("%c", ser_getc(psc));
370 #define BUZZER_GPT (MPC5XXX_GPT + 0x60) /* GPT6 */
371 static void buzzer_turn_on(unsigned int freq)
373 volatile struct mpc5xxx_gpt *gpt = (struct mpc5xxx_gpt *)(BUZZER_GPT);
375 const u32 prescale = gd->ipb_clk / freq / 128;
376 const u32 count = 128;
377 const u32 width = 64;
379 gpt->cir = (prescale << 16) | count;
380 gpt->pwmcr = width << 16;
381 gpt->emsr = 3; /* Timer enabled for PWM */
384 static void buzzer_turn_off(void)
386 volatile struct mpc5xxx_gpt *gpt = (struct mpc5xxx_gpt *)(BUZZER_GPT);
391 static int do_inkadiag_buzzer(cmd_tbl_t *cmdtp, int flag, int argc,
394 unsigned int period, freq;
405 period = simple_strtol(argv[0], NULL, 0);
407 printf("Zero period is senseless\n");
411 freq = simple_strtol(argv[0], NULL, 0);
412 /* avoid zero prescale in buzzer_turn_on() */
413 if (freq > gd->ipb_clk / 128) {
414 printf("%dHz exceeds maximum (%ldHz)\n", freq,
417 printf("Zero frequency is senseless\n");
419 buzzer_turn_on(freq);
422 prev = disable_ctrlc(0);
424 printf("Buzzing for %d ms. Type ^C to abort!\n\n", period);
427 while (!ctrlc() && (i++ < CONFIG_SYS_HZ))
438 static int do_inkadiag_help(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
440 cmd_tbl_t cmd_inkadiag_sub[] = {
441 U_BOOT_CMD_MKENT(io, 1, 1, do_inkadiag_io, "read digital input",
442 "<drawer1|drawer2|other> [value] - get or set specified signal"),
443 U_BOOT_CMD_MKENT(serial, 4, 1, do_inkadiag_serial, "test serial port",
444 "<num> <mode> <baudrate> <msg> - test uart num [0..11] in mode\n"
445 "and baudrate with msg"),
446 U_BOOT_CMD_MKENT(buzzer, 2, 1, do_inkadiag_buzzer, "activate buzzer",
447 "<period> <freq> - turn buzzer on for period ms with freq hz"),
448 U_BOOT_CMD_MKENT(help, 4, 1, do_inkadiag_help, "get help",
449 "[command] - get help for command"),
452 static int do_inkadiag_help(cmd_tbl_t *cmdtp, int flag,
453 int argc, char *argv[]) {
454 extern int _do_help (cmd_tbl_t *cmd_start, int cmd_items,
455 cmd_tbl_t *cmdtp, int flag,
456 int argc, char *argv[]);
457 /* do_help prints command name - we prepend inkadiag to our subcommands! */
458 #ifdef CONFIG_SYS_LONGHELP
461 return _do_help(&cmd_inkadiag_sub[0],
462 ARRAY_SIZE(cmd_inkadiag_sub), cmdtp, flag, argc, argv);
465 static int do_inkadiag(cmd_tbl_t *cmdtp, int flag, int argc,
469 c = find_cmd_tbl(argv[1], &cmd_inkadiag_sub[0], ARRAY_SIZE(cmd_inkadiag_sub));
474 return c->cmd(c, flag, argc, argv);
476 /* Unrecognized command */
482 U_BOOT_CMD(inkadiag, 6, 1, do_inkadiag,
483 "inkadiag - inka diagnosis\n",
484 "[inkadiag what ...]\n"
485 " - perform a diagnosis on inka hardware\n"
486 "'inkadiag' performs hardware tests.");
488 /* Relocate the command table function pointers when running in RAM */
489 int inkadiag_init_r (void) {
492 for (cmdtp = &cmd_inkadiag_sub[0]; cmdtp !=
493 &cmd_inkadiag_sub[ARRAY_SIZE(cmd_inkadiag_sub)]; cmdtp++) {
496 addr = (ulong) (cmdtp->cmd) + gd->reloc_off;
497 cmdtp->cmd = (int (*)(struct cmd_tbl_s *, int, int, char *[]))addr;
499 addr = (ulong)(cmdtp->name) + gd->reloc_off;
500 cmdtp->name = (char *)addr;
503 addr = (ulong)(cmdtp->usage) + gd->reloc_off;
504 cmdtp->usage = (char *)addr;
506 #ifdef CONFIG_SYS_LONGHELP
508 addr = (ulong)(cmdtp->help) + gd->reloc_off;
509 cmdtp->help = (char *)addr;