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
,
134 char * const argv
[]) {
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
);
171 return cmd_usage(cmdtp
);
177 DECLARE_GLOBAL_DATA_PTR
;
179 static int ser_init(volatile struct mpc5xxx_psc
*psc
, int baudrate
)
181 unsigned long baseclk
;
185 out_8(&psc
->command
, PSC_SEL_MODE_REG_1
);
187 /* select clock sources */
189 out_be16(&psc
->psc_clock_select
, 0);
190 baseclk
= (gd
->ipb_clk
+ 16) / 32;
192 /* switch to UART mode */
193 out_be32(&psc
->sicr
, 0);
195 /* configure parity, bit length and so on */
197 out_8(&psc
->mode
, PSC_MODE_8_BITS
| PSC_MODE_PARNONE
);
198 out_8(&psc
->mode
, PSC_MODE_ONE_STOP
);
200 /* set up UART divisor */
201 div
= (baseclk
+ (baudrate
/ 2)) / baudrate
;
202 out_8(&psc
->ctur
, (div
>> 8) & 0xff);
203 out_8(&psc
->ctlr
, div
& 0xff);
205 /* disable all interrupts */
206 out_be16(&psc
->psc_imr
, 0);
208 /* reset and enable Rx/Tx */
209 out_8(&psc
->command
, PSC_RST_RX
);
210 out_8(&psc
->command
, PSC_RST_TX
);
211 out_8(&psc
->command
, PSC_RX_ENABLE
| PSC_TX_ENABLE
);
216 static void ser_putc(volatile struct mpc5xxx_psc
*psc
, const char c
)
218 /* Wait 1 second for last character to go. */
221 while (!(psc
->psc_status
& PSC_SR_TXEMP
) && (i
++ < 1000000/10))
223 psc
->psc_buffer_8
= c
;
227 static int ser_getc(volatile struct mpc5xxx_psc
*psc
)
229 /* Wait for a character to arrive. */
232 while (!(in_be16(&psc
->psc_status
) & PSC_SR_RXRDY
) && (i
++ < 1000000/10))
235 return in_8(&psc
->psc_buffer_8
);
238 static int do_inkadiag_serial(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
239 char * const argv
[]) {
240 volatile struct NS16550
*uart
;
241 volatile struct mpc5xxx_psc
*psc
;
242 unsigned int num
, mode
;
243 int combrd
, baudrate
, i
, j
, len
;
247 return cmd_usage(cmdtp
);
252 num
= simple_strtol(argv
[0], NULL
, 0);
253 if (num
< 0 || num
> 11) {
254 printf("invalid argument for num: %d\n", num
);
258 mode
= simple_strtol(argv
[1], NULL
, 0);
261 baudrate
= simple_strtoul(argv
[2], NULL
, 10);
262 for (i
=0; i
<N_BAUDRATES
; ++i
) {
263 if (baudrate
== baudrate_table
[i
])
266 if (i
== N_BAUDRATES
) {
267 printf("## Baudrate %d bps not supported\n",
271 combrd
= 115200 / baudrate
;
273 uart
= (struct NS16550
*)(SERIAL_PORT_BASE
+ (num
<< 3));
275 printf("Testing uart %d.\n\n", num
);
277 if ((num
>= 0) && (num
<= 7)) {
279 /* turn on 'loopback' mode */
280 out_8(&uart
->mcr
, UART_MCR_LOOP
);
283 * establish the UART's operational parameters
284 * set DLAB=1, so rbr accesses DLL
286 out_8(&uart
->lcr
, UART_LCR_DLAB
);
288 out_8(&uart
->rbr
, combrd
);
289 /* set data-format: 8-N-1 */
290 out_8(&uart
->lcr
, UART_LCR_WLS_8
);
294 /* set request to send */
295 out_8(&uart
->mcr
, UART_MCR_RTS
);
297 /* check clear to send */
298 if ((in_8(&uart
->msr
) & UART_MSR_CTS
) == 0x00)
302 /* set data terminal ready */
303 out_8(&uart
->mcr
, UART_MCR_DTR
);
305 /* check data set ready and carrier detect */
306 if ((in_8(&uart
->msr
) & (UART_MSR_DSR
| UART_MSR_DCD
))
307 != (UART_MSR_DSR
| UART_MSR_DCD
))
311 /* write each message-character, read it back, and display it */
312 for (i
= 0, len
= strlen(argv
[3]); i
< len
; ++i
) {
314 while ((in_8(&uart
->lsr
) & UART_LSR_THRE
) == 0x00) {
315 if (j
++ > CONFIG_SYS_HZ
)
319 out_8(&uart
->rbr
, argv
[3][i
]);
321 while ((in_8(&uart
->lsr
) & UART_LSR_DR
) == 0x00) {
322 if (j
++ > CONFIG_SYS_HZ
)
326 printf("%c", in_8(&uart
->rbr
));
329 out_8(&uart
->mcr
, 0x00);
335 address
= MPC5XXX_PSC6
;
338 address
= MPC5XXX_PSC3
;
341 address
= MPC5XXX_PSC2
;
344 address
= MPC5XXX_PSC1
;
347 psc
= (struct mpc5xxx_psc
*)address
;
348 ser_init(psc
, simple_strtol(argv
[2], NULL
, 0));
350 /* set request to send */
351 out_8(&psc
->op0
, PSC_OP0_RTS
);
353 /* check clear to send */
354 if ((in_8(&psc
->ip
) & PSC_IPCR_CTS
) == 0)
357 len
= strlen(argv
[3]);
358 for (i
= 0; i
< len
; ++i
) {
359 ser_putc(psc
, argv
[3][i
]);
360 printf("%c", ser_getc(psc
));
367 #define BUZZER_GPT (MPC5XXX_GPT + 0x60) /* GPT6 */
368 static void buzzer_turn_on(unsigned int freq
)
370 volatile struct mpc5xxx_gpt
*gpt
= (struct mpc5xxx_gpt
*)(BUZZER_GPT
);
372 const u32 prescale
= gd
->ipb_clk
/ freq
/ 128;
373 const u32 count
= 128;
374 const u32 width
= 64;
376 gpt
->cir
= (prescale
<< 16) | count
;
377 gpt
->pwmcr
= width
<< 16;
378 gpt
->emsr
= 3; /* Timer enabled for PWM */
381 static void buzzer_turn_off(void)
383 volatile struct mpc5xxx_gpt
*gpt
= (struct mpc5xxx_gpt
*)(BUZZER_GPT
);
388 static int do_inkadiag_buzzer(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
389 char * const argv
[]) {
391 unsigned int period
, freq
;
395 return cmd_usage(cmdtp
);
400 period
= simple_strtol(argv
[0], NULL
, 0);
402 printf("Zero period is senseless\n");
406 freq
= simple_strtol(argv
[0], NULL
, 0);
407 /* avoid zero prescale in buzzer_turn_on() */
408 if (freq
> gd
->ipb_clk
/ 128) {
409 printf("%dHz exceeds maximum (%ldHz)\n", freq
,
412 printf("Zero frequency is senseless\n");
414 buzzer_turn_on(freq
);
417 prev
= disable_ctrlc(0);
419 printf("Buzzing for %d ms. Type ^C to abort!\n\n", period
);
422 while (!ctrlc() && (i
++ < CONFIG_SYS_HZ
))
433 static int do_inkadiag_help(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[]);
435 cmd_tbl_t cmd_inkadiag_sub
[] = {
436 U_BOOT_CMD_MKENT(io
, 1, 1, do_inkadiag_io
, "read digital input",
437 "<drawer1|drawer2|other> [value] - get or set specified signal"),
438 U_BOOT_CMD_MKENT(serial
, 4, 1, do_inkadiag_serial
, "test serial port",
439 "<num> <mode> <baudrate> <msg> - test uart num [0..11] in mode\n"
440 "and baudrate with msg"),
441 U_BOOT_CMD_MKENT(buzzer
, 2, 1, do_inkadiag_buzzer
, "activate buzzer",
442 "<period> <freq> - turn buzzer on for period ms with freq hz"),
443 U_BOOT_CMD_MKENT(help
, 4, 1, do_inkadiag_help
, "get help",
444 "[command] - get help for command"),
447 static int do_inkadiag_help(cmd_tbl_t
*cmdtp
, int flag
,
448 int argc
, char * const argv
[]) {
449 extern int _do_help (cmd_tbl_t
*cmd_start
, int cmd_items
,
450 cmd_tbl_t
*cmdtp
, int flag
,
451 int argc
, char * const argv
[]);
452 /* do_help prints command name - we prepend inkadiag to our subcommands! */
453 #ifdef CONFIG_SYS_LONGHELP
456 return _do_help(&cmd_inkadiag_sub
[0],
457 ARRAY_SIZE(cmd_inkadiag_sub
), cmdtp
, flag
, argc
, argv
);
460 static int do_inkadiag(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
461 char * const argv
[]) {
464 c
= find_cmd_tbl(argv
[1], &cmd_inkadiag_sub
[0], ARRAY_SIZE(cmd_inkadiag_sub
));
469 return c
->cmd(c
, flag
, argc
, argv
);
471 /* Unrecognized command */
472 return cmd_usage(cmdtp
);
476 U_BOOT_CMD(inkadiag
, 6, 1, do_inkadiag
,
477 "inkadiag - inka diagnosis\n",
478 "[inkadiag what ...]\n"
479 " - perform a diagnosis on inka hardware\n"
480 "'inkadiag' performs hardware tests.");