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 * SPDX-License-Identifier: GPL-2.0+
18 /* This is needed for the includes in ns16550.h */
19 #define CONFIG_SYS_NS16550_REG_SIZE 1
22 #define GPIO_BASE ((u_char *)CONFIG_SYS_CS3_START)
24 #define DIGIN_TOUCHSCR_MASK 0x00003000 /* Inputs 12-13 */
25 #define DIGIN_KEYB_MASK 0x00010000 /* Input 16 */
27 #define DIGIN_DRAWER_SW1 0x00400000 /* Input 22 */
28 #define DIGIN_DRAWER_SW2 0x00800000 /* Input 23 */
30 #define DIGIO_LED0 0x00000001 /* Output 0 */
31 #define DIGIO_LED1 0x00000002 /* Output 1 */
32 #define DIGIO_LED2 0x00000004 /* Output 2 */
33 #define DIGIO_LED3 0x00000008 /* Output 3 */
34 #define DIGIO_LED4 0x00000010 /* Output 4 */
35 #define DIGIO_LED5 0x00000020 /* Output 5 */
37 #define DIGIO_DRAWER1 0x00000100 /* Output 8 */
38 #define DIGIO_DRAWER2 0x00000200 /* Output 9 */
40 #define SERIAL_PORT_BASE ((u_char *)CONFIG_SYS_CS2_START)
42 #define PSC_OP1_RTS 0x01
43 #define PSC_OP0_RTS 0x01
46 * Table with supported baudrates (defined in inka4x0.h)
48 static const unsigned long baudrate_table
[] = CONFIG_SYS_BAUDRATE_TABLE
;
49 #define N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0]))
51 static unsigned int inka_digin_get_input(void)
53 return in_8(GPIO_BASE
+ 0) << 0 | in_8(GPIO_BASE
+ 1) << 8 |
54 in_8(GPIO_BASE
+ 2) << 16 | in_8(GPIO_BASE
+ 3) << 24;
57 #define LED_HIGH(NUM) \
59 setbits_be32((unsigned *)MPC5XXX_GPT##NUM##_ENABLE, 0x10); \
62 #define LED_LOW(NUM) \
64 clrbits_be32((unsigned *)MPC5XXX_GPT##NUM##_ENABLE, 0x10); \
67 #define CHECK_LED(NUM) \
69 if (state & (1 << NUM)) { \
76 static void inka_digio_set_output(unsigned int state
, int which
)
78 volatile struct mpc5xxx_gpio
*gpio
= (struct mpc5xxx_gpio
*)MPC5XXX_GPIO
;
92 clrbits_be32(&gpio
->simple_dvo
, 0x1000);
94 setbits_be32(&gpio
->simple_dvo
, 0x1000);
96 setbits_be32(&gpio
->simple_dvo
, 0x1000);
98 clrbits_be32(&gpio
->simple_dvo
, 0x1000);
104 clrbits_be32(&gpio
->simple_dvo
, 0x2000);
106 setbits_be32(&gpio
->simple_dvo
, 0x2000);
108 setbits_be32(&gpio
->simple_dvo
, 0x2000);
110 clrbits_be32(&gpio
->simple_dvo
, 0x2000);
117 static int do_inkadiag_io(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
118 char * const argv
[]) {
119 unsigned int state
, val
;
124 val
= simple_strtol(argv
[2], NULL
, 16);
126 if (strcmp(argv
[1], "drawer1") == 0) {
127 inka_digio_set_output(val
, 1);
128 } else if (strcmp(argv
[1], "drawer2") == 0) {
129 inka_digio_set_output(val
, 2);
130 } else if (strcmp(argv
[1], "other") == 0)
131 inka_digio_set_output(val
, 0);
133 printf("Invalid argument: %s\n", argv
[1]);
139 state
= inka_digin_get_input();
141 if (strcmp(argv
[1], "drawer1") == 0) {
142 val
= (state
& DIGIN_DRAWER_SW1
) >> (ffs(DIGIN_DRAWER_SW1
) - 1);
143 } else if (strcmp(argv
[1], "drawer2") == 0) {
144 val
= (state
& DIGIN_DRAWER_SW2
) >> (ffs(DIGIN_DRAWER_SW2
) - 1);
145 } else if (strcmp(argv
[1], "other") == 0) {
146 val
= ((state
& DIGIN_KEYB_MASK
) >> (ffs(DIGIN_KEYB_MASK
) - 1))
147 | (state
& DIGIN_TOUCHSCR_MASK
) >> (ffs(DIGIN_TOUCHSCR_MASK
) - 2);
149 printf("Invalid argument: %s\n", argv
[1]);
152 printf("exit code: 0x%X\n", val
);
155 return cmd_usage(cmdtp
);
161 DECLARE_GLOBAL_DATA_PTR
;
163 static int ser_init(volatile struct mpc5xxx_psc
*psc
, int baudrate
)
165 unsigned long baseclk
;
169 out_8(&psc
->command
, PSC_SEL_MODE_REG_1
);
171 /* select clock sources */
173 out_be16(&psc
->psc_clock_select
, 0);
174 baseclk
= (gd
->arch
.ipb_clk
+ 16) / 32;
176 /* switch to UART mode */
177 out_be32(&psc
->sicr
, 0);
179 /* configure parity, bit length and so on */
181 out_8(&psc
->mode
, PSC_MODE_8_BITS
| PSC_MODE_PARNONE
);
182 out_8(&psc
->mode
, PSC_MODE_ONE_STOP
);
184 /* set up UART divisor */
185 div
= (baseclk
+ (baudrate
/ 2)) / baudrate
;
186 out_8(&psc
->ctur
, (div
>> 8) & 0xff);
187 out_8(&psc
->ctlr
, div
& 0xff);
189 /* disable all interrupts */
190 out_be16(&psc
->psc_imr
, 0);
192 /* reset and enable Rx/Tx */
193 out_8(&psc
->command
, PSC_RST_RX
);
194 out_8(&psc
->command
, PSC_RST_TX
);
195 out_8(&psc
->command
, PSC_RX_ENABLE
| PSC_TX_ENABLE
);
200 static void ser_putc(volatile struct mpc5xxx_psc
*psc
, const char c
)
202 /* Wait 1 second for last character to go. */
205 while (!(psc
->psc_status
& PSC_SR_TXEMP
) && (i
++ < 1000000/10))
207 psc
->psc_buffer_8
= c
;
211 static int ser_getc(volatile struct mpc5xxx_psc
*psc
)
213 /* Wait for a character to arrive. */
216 while (!(in_be16(&psc
->psc_status
) & PSC_SR_RXRDY
) && (i
++ < 1000000/10))
219 return in_8(&psc
->psc_buffer_8
);
222 static int do_inkadiag_serial(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
223 char * const argv
[]) {
224 volatile struct NS16550
*uart
;
225 volatile struct mpc5xxx_psc
*psc
;
226 unsigned int num
, mode
;
227 int combrd
, baudrate
, i
, j
, len
;
231 return cmd_usage(cmdtp
);
236 num
= simple_strtol(argv
[0], NULL
, 0);
237 if (num
< 0 || num
> 11) {
238 printf("invalid argument for num: %d\n", num
);
242 mode
= simple_strtol(argv
[1], NULL
, 0);
245 baudrate
= simple_strtoul(argv
[2], NULL
, 10);
246 for (i
=0; i
<N_BAUDRATES
; ++i
) {
247 if (baudrate
== baudrate_table
[i
])
250 if (i
== N_BAUDRATES
) {
251 printf("## Baudrate %d bps not supported\n",
255 combrd
= 115200 / baudrate
;
257 uart
= (struct NS16550
*)(SERIAL_PORT_BASE
+ (num
<< 3));
259 printf("Testing uart %d.\n\n", num
);
261 if ((num
>= 0) && (num
<= 7)) {
263 /* turn on 'loopback' mode */
264 out_8(&uart
->mcr
, UART_MCR_LOOP
);
267 * establish the UART's operational parameters
268 * set DLAB=1, so rbr accesses DLL
270 out_8(&uart
->lcr
, UART_LCR_DLAB
);
272 out_8(&uart
->rbr
, combrd
);
273 /* set data-format: 8-N-1 */
274 out_8(&uart
->lcr
, UART_LCR_WLS_8
);
278 /* set request to send */
279 out_8(&uart
->mcr
, UART_MCR_RTS
);
281 /* check clear to send */
282 if ((in_8(&uart
->msr
) & UART_MSR_CTS
) == 0x00)
286 /* set data terminal ready */
287 out_8(&uart
->mcr
, UART_MCR_DTR
);
289 /* check data set ready and carrier detect */
290 if ((in_8(&uart
->msr
) & (UART_MSR_DSR
| UART_MSR_DCD
))
291 != (UART_MSR_DSR
| UART_MSR_DCD
))
295 /* write each message-character, read it back, and display it */
296 for (i
= 0, len
= strlen(argv
[3]); i
< len
; ++i
) {
298 while ((in_8(&uart
->lsr
) & UART_LSR_THRE
) == 0x00) {
299 if (j
++ > CONFIG_SYS_HZ
)
303 out_8(&uart
->rbr
, argv
[3][i
]);
305 while ((in_8(&uart
->lsr
) & UART_LSR_DR
) == 0x00) {
306 if (j
++ > CONFIG_SYS_HZ
)
310 printf("%c", in_8(&uart
->rbr
));
313 out_8(&uart
->mcr
, 0x00);
319 address
= MPC5XXX_PSC6
;
322 address
= MPC5XXX_PSC3
;
325 address
= MPC5XXX_PSC2
;
328 address
= MPC5XXX_PSC1
;
331 psc
= (struct mpc5xxx_psc
*)address
;
332 ser_init(psc
, simple_strtol(argv
[2], NULL
, 0));
334 /* set request to send */
335 out_8(&psc
->op0
, PSC_OP0_RTS
);
337 /* check clear to send */
338 if ((in_8(&psc
->ip
) & PSC_IPCR_CTS
) == 0)
341 len
= strlen(argv
[3]);
342 for (i
= 0; i
< len
; ++i
) {
343 ser_putc(psc
, argv
[3][i
]);
344 printf("%c", ser_getc(psc
));
351 #define BUZZER_GPT (MPC5XXX_GPT + 0x60) /* GPT6 */
352 static void buzzer_turn_on(unsigned int freq
)
354 volatile struct mpc5xxx_gpt
*gpt
= (struct mpc5xxx_gpt
*)(BUZZER_GPT
);
356 const u32 prescale
= gd
->arch
.ipb_clk
/ freq
/ 128;
357 const u32 count
= 128;
358 const u32 width
= 64;
360 gpt
->cir
= (prescale
<< 16) | count
;
361 gpt
->pwmcr
= width
<< 16;
362 gpt
->emsr
= 3; /* Timer enabled for PWM */
365 static void buzzer_turn_off(void)
367 volatile struct mpc5xxx_gpt
*gpt
= (struct mpc5xxx_gpt
*)(BUZZER_GPT
);
372 static int do_inkadiag_buzzer(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
373 char * const argv
[]) {
375 unsigned int period
, freq
;
379 return cmd_usage(cmdtp
);
384 period
= simple_strtol(argv
[0], NULL
, 0);
386 printf("Zero period is senseless\n");
390 freq
= simple_strtol(argv
[0], NULL
, 0);
391 /* avoid zero prescale in buzzer_turn_on() */
392 if (freq
> gd
->arch
.ipb_clk
/ 128) {
393 printf("%dHz exceeds maximum (%ldHz)\n", freq
,
394 gd
->arch
.ipb_clk
/ 128);
396 printf("Zero frequency is senseless\n");
398 buzzer_turn_on(freq
);
401 prev
= disable_ctrlc(0);
403 printf("Buzzing for %d ms. Type ^C to abort!\n\n", period
);
406 while (!ctrlc() && (i
++ < CONFIG_SYS_HZ
))
417 static int do_inkadiag_help(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[]);
419 cmd_tbl_t cmd_inkadiag_sub
[] = {
420 U_BOOT_CMD_MKENT(io
, 1, 1, do_inkadiag_io
, "read digital input",
421 "<drawer1|drawer2|other> [value] - get or set specified signal"),
422 U_BOOT_CMD_MKENT(serial
, 4, 1, do_inkadiag_serial
, "test serial port",
423 "<num> <mode> <baudrate> <msg> - test uart num [0..11] in mode\n"
424 "and baudrate with msg"),
425 U_BOOT_CMD_MKENT(buzzer
, 2, 1, do_inkadiag_buzzer
, "activate buzzer",
426 "<period> <freq> - turn buzzer on for period ms with freq hz"),
427 U_BOOT_CMD_MKENT(help
, 4, 1, do_inkadiag_help
, "get help",
428 "[command] - get help for command"),
431 static int do_inkadiag_help(cmd_tbl_t
*cmdtp
, int flag
,
432 int argc
, char * const argv
[]) {
433 extern int _do_help (cmd_tbl_t
*cmd_start
, int cmd_items
,
434 cmd_tbl_t
*cmdtp
, int flag
,
435 int argc
, char * const argv
[]);
436 /* do_help prints command name - we prepend inkadiag to our subcommands! */
437 #ifdef CONFIG_SYS_LONGHELP
440 return _do_help(&cmd_inkadiag_sub
[0],
441 ARRAY_SIZE(cmd_inkadiag_sub
), cmdtp
, flag
, argc
, argv
);
444 static int do_inkadiag(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
445 char * const argv
[]) {
448 c
= find_cmd_tbl(argv
[1], &cmd_inkadiag_sub
[0], ARRAY_SIZE(cmd_inkadiag_sub
));
453 return c
->cmd(c
, flag
, argc
, argv
);
455 /* Unrecognized command */
456 return cmd_usage(cmdtp
);
460 U_BOOT_CMD(inkadiag
, 6, 1, do_inkadiag
,
461 "inkadiag - inka diagnosis\n",
462 "[inkadiag what ...]\n"
463 " - perform a diagnosis on inka hardware\n"
464 "'inkadiag' performs hardware tests.");