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+
19 /* This is needed for the includes in ns16550.h */
20 #define CONFIG_SYS_NS16550_REG_SIZE 1
23 #define GPIO_BASE ((u_char *)CONFIG_SYS_CS3_START)
25 #define DIGIN_TOUCHSCR_MASK 0x00003000 /* Inputs 12-13 */
26 #define DIGIN_KEYB_MASK 0x00010000 /* Input 16 */
28 #define DIGIN_DRAWER_SW1 0x00400000 /* Input 22 */
29 #define DIGIN_DRAWER_SW2 0x00800000 /* Input 23 */
31 #define DIGIO_LED0 0x00000001 /* Output 0 */
32 #define DIGIO_LED1 0x00000002 /* Output 1 */
33 #define DIGIO_LED2 0x00000004 /* Output 2 */
34 #define DIGIO_LED3 0x00000008 /* Output 3 */
35 #define DIGIO_LED4 0x00000010 /* Output 4 */
36 #define DIGIO_LED5 0x00000020 /* Output 5 */
38 #define DIGIO_DRAWER1 0x00000100 /* Output 8 */
39 #define DIGIO_DRAWER2 0x00000200 /* Output 9 */
41 #define SERIAL_PORT_BASE ((u_char *)CONFIG_SYS_CS2_START)
43 #define PSC_OP1_RTS 0x01
44 #define PSC_OP0_RTS 0x01
47 * Table with supported baudrates (defined in inka4x0.h)
49 static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
50 #define N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0]))
52 static unsigned int inka_digin_get_input(void)
54 return in_8(GPIO_BASE + 0) << 0 | in_8(GPIO_BASE + 1) << 8 |
55 in_8(GPIO_BASE + 2) << 16 | in_8(GPIO_BASE + 3) << 24;
58 #define LED_HIGH(NUM) \
60 setbits_be32((unsigned *)MPC5XXX_GPT##NUM##_ENABLE, 0x10); \
63 #define LED_LOW(NUM) \
65 clrbits_be32((unsigned *)MPC5XXX_GPT##NUM##_ENABLE, 0x10); \
68 #define CHECK_LED(NUM) \
70 if (state & (1 << NUM)) { \
77 static void inka_digio_set_output(unsigned int state, int which)
79 volatile struct mpc5xxx_gpio *gpio = (struct mpc5xxx_gpio *)MPC5XXX_GPIO;
93 clrbits_be32(&gpio->simple_dvo, 0x1000);
95 setbits_be32(&gpio->simple_dvo, 0x1000);
97 setbits_be32(&gpio->simple_dvo, 0x1000);
99 clrbits_be32(&gpio->simple_dvo, 0x1000);
105 clrbits_be32(&gpio->simple_dvo, 0x2000);
107 setbits_be32(&gpio->simple_dvo, 0x2000);
109 setbits_be32(&gpio->simple_dvo, 0x2000);
111 clrbits_be32(&gpio->simple_dvo, 0x2000);
118 static int do_inkadiag_io(cmd_tbl_t *cmdtp, int flag, int argc,
119 char * const argv[]) {
120 unsigned int state, val;
125 val = simple_strtol(argv[2], NULL, 16);
127 if (strcmp(argv[1], "drawer1") == 0) {
128 inka_digio_set_output(val, 1);
129 } else if (strcmp(argv[1], "drawer2") == 0) {
130 inka_digio_set_output(val, 2);
131 } else if (strcmp(argv[1], "other") == 0)
132 inka_digio_set_output(val, 0);
134 printf("Invalid argument: %s\n", argv[1]);
140 state = inka_digin_get_input();
142 if (strcmp(argv[1], "drawer1") == 0) {
143 val = (state & DIGIN_DRAWER_SW1) >> (ffs(DIGIN_DRAWER_SW1) - 1);
144 } else if (strcmp(argv[1], "drawer2") == 0) {
145 val = (state & DIGIN_DRAWER_SW2) >> (ffs(DIGIN_DRAWER_SW2) - 1);
146 } else if (strcmp(argv[1], "other") == 0) {
147 val = ((state & DIGIN_KEYB_MASK) >> (ffs(DIGIN_KEYB_MASK) - 1))
148 | (state & DIGIN_TOUCHSCR_MASK) >> (ffs(DIGIN_TOUCHSCR_MASK) - 2);
150 printf("Invalid argument: %s\n", argv[1]);
153 printf("exit code: 0x%X\n", val);
156 return cmd_usage(cmdtp);
162 DECLARE_GLOBAL_DATA_PTR;
164 static int ser_init(volatile struct mpc5xxx_psc *psc, int baudrate)
166 unsigned long baseclk;
170 out_8(&psc->command, PSC_SEL_MODE_REG_1);
172 /* select clock sources */
174 out_be16(&psc->psc_clock_select, 0);
175 baseclk = (gd->arch.ipb_clk + 16) / 32;
177 /* switch to UART mode */
178 out_be32(&psc->sicr, 0);
180 /* configure parity, bit length and so on */
182 out_8(&psc->mode, PSC_MODE_8_BITS | PSC_MODE_PARNONE);
183 out_8(&psc->mode, PSC_MODE_ONE_STOP);
185 /* set up UART divisor */
186 div = (baseclk + (baudrate / 2)) / baudrate;
187 out_8(&psc->ctur, (div >> 8) & 0xff);
188 out_8(&psc->ctlr, div & 0xff);
190 /* disable all interrupts */
191 out_be16(&psc->psc_imr, 0);
193 /* reset and enable Rx/Tx */
194 out_8(&psc->command, PSC_RST_RX);
195 out_8(&psc->command, PSC_RST_TX);
196 out_8(&psc->command, PSC_RX_ENABLE | PSC_TX_ENABLE);
201 static void ser_putc(volatile struct mpc5xxx_psc *psc, const char c)
203 /* Wait 1 second for last character to go. */
206 while (!(psc->psc_status & PSC_SR_TXEMP) && (i++ < 1000000/10))
208 psc->psc_buffer_8 = c;
212 static int ser_getc(volatile struct mpc5xxx_psc *psc)
214 /* Wait for a character to arrive. */
217 while (!(in_be16(&psc->psc_status) & PSC_SR_RXRDY) && (i++ < 1000000/10))
220 return in_8(&psc->psc_buffer_8);
223 static int do_inkadiag_serial(cmd_tbl_t *cmdtp, int flag, int argc,
224 char * const argv[]) {
225 volatile struct NS16550 *uart;
226 volatile struct mpc5xxx_psc *psc;
227 unsigned int num, mode;
228 int combrd, baudrate, i, j, len;
232 return cmd_usage(cmdtp);
237 num = simple_strtol(argv[0], NULL, 0);
238 if (num < 0 || num > 11) {
239 printf("invalid argument for num: %d\n", num);
243 mode = simple_strtol(argv[1], NULL, 0);
246 baudrate = simple_strtoul(argv[2], NULL, 10);
247 for (i=0; i<N_BAUDRATES; ++i) {
248 if (baudrate == baudrate_table[i])
251 if (i == N_BAUDRATES) {
252 printf("## Baudrate %d bps not supported\n",
256 combrd = 115200 / baudrate;
258 uart = (struct NS16550 *)(SERIAL_PORT_BASE + (num << 3));
260 printf("Testing uart %d.\n\n", num);
262 if ((num >= 0) && (num <= 7)) {
264 /* turn on 'loopback' mode */
265 out_8(&uart->mcr, UART_MCR_LOOP);
268 * establish the UART's operational parameters
269 * set DLAB=1, so rbr accesses DLL
271 out_8(&uart->lcr, UART_LCR_DLAB);
273 out_8(&uart->rbr, combrd);
274 /* set data-format: 8-N-1 */
275 out_8(&uart->lcr, UART_LCR_WLS_8);
279 /* set request to send */
280 out_8(&uart->mcr, UART_MCR_RTS);
282 /* check clear to send */
283 if ((in_8(&uart->msr) & UART_MSR_CTS) == 0x00)
287 /* set data terminal ready */
288 out_8(&uart->mcr, UART_MCR_DTR);
290 /* check data set ready and carrier detect */
291 if ((in_8(&uart->msr) & (UART_MSR_DSR | UART_MSR_DCD))
292 != (UART_MSR_DSR | UART_MSR_DCD))
296 /* write each message-character, read it back, and display it */
297 for (i = 0, len = strlen(argv[3]); i < len; ++i) {
299 while ((in_8(&uart->lsr) & UART_LSR_THRE) == 0x00) {
300 if (j++ > CONFIG_SYS_HZ)
304 out_8(&uart->rbr, argv[3][i]);
306 while ((in_8(&uart->lsr) & UART_LSR_DR) == 0x00) {
307 if (j++ > CONFIG_SYS_HZ)
311 printf("%c", in_8(&uart->rbr));
314 out_8(&uart->mcr, 0x00);
320 address = MPC5XXX_PSC6;
323 address = MPC5XXX_PSC3;
326 address = MPC5XXX_PSC2;
329 address = MPC5XXX_PSC1;
332 psc = (struct mpc5xxx_psc *)address;
333 ser_init(psc, simple_strtol(argv[2], NULL, 0));
335 /* set request to send */
336 out_8(&psc->op0, PSC_OP0_RTS);
338 /* check clear to send */
339 if ((in_8(&psc->ip) & PSC_IPCR_CTS) == 0)
342 len = strlen(argv[3]);
343 for (i = 0; i < len; ++i) {
344 ser_putc(psc, argv[3][i]);
345 printf("%c", ser_getc(psc));
352 #define BUZZER_GPT (MPC5XXX_GPT + 0x60) /* GPT6 */
353 static void buzzer_turn_on(unsigned int freq)
355 volatile struct mpc5xxx_gpt *gpt = (struct mpc5xxx_gpt *)(BUZZER_GPT);
357 const u32 prescale = gd->arch.ipb_clk / freq / 128;
358 const u32 count = 128;
359 const u32 width = 64;
361 gpt->cir = (prescale << 16) | count;
362 gpt->pwmcr = width << 16;
363 gpt->emsr = 3; /* Timer enabled for PWM */
366 static void buzzer_turn_off(void)
368 volatile struct mpc5xxx_gpt *gpt = (struct mpc5xxx_gpt *)(BUZZER_GPT);
373 static int do_inkadiag_buzzer(cmd_tbl_t *cmdtp, int flag, int argc,
374 char * const argv[]) {
376 unsigned int period, freq;
380 return cmd_usage(cmdtp);
385 period = simple_strtol(argv[0], NULL, 0);
387 printf("Zero period is senseless\n");
391 freq = simple_strtol(argv[0], NULL, 0);
392 /* avoid zero prescale in buzzer_turn_on() */
393 if (freq > gd->arch.ipb_clk / 128) {
394 printf("%dHz exceeds maximum (%ldHz)\n", freq,
395 gd->arch.ipb_clk / 128);
397 printf("Zero frequency is senseless\n");
399 buzzer_turn_on(freq);
402 prev = disable_ctrlc(0);
404 printf("Buzzing for %d ms. Type ^C to abort!\n\n", period);
407 while (!ctrlc() && (i++ < CONFIG_SYS_HZ))
418 static int do_inkadiag_help(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
420 cmd_tbl_t cmd_inkadiag_sub[] = {
421 U_BOOT_CMD_MKENT(io, 1, 1, do_inkadiag_io, "read digital input",
422 "<drawer1|drawer2|other> [value] - get or set specified signal"),
423 U_BOOT_CMD_MKENT(serial, 4, 1, do_inkadiag_serial, "test serial port",
424 "<num> <mode> <baudrate> <msg> - test uart num [0..11] in mode\n"
425 "and baudrate with msg"),
426 U_BOOT_CMD_MKENT(buzzer, 2, 1, do_inkadiag_buzzer, "activate buzzer",
427 "<period> <freq> - turn buzzer on for period ms with freq hz"),
428 U_BOOT_CMD_MKENT(help, 4, 1, do_inkadiag_help, "get help",
429 "[command] - get help for command"),
432 static int do_inkadiag_help(cmd_tbl_t *cmdtp, int flag,
433 int argc, char * const argv[]) {
434 extern int _do_help (cmd_tbl_t *cmd_start, int cmd_items,
435 cmd_tbl_t *cmdtp, int flag,
436 int argc, char * const argv[]);
437 /* do_help prints command name - we prepend inkadiag to our subcommands! */
438 #ifdef CONFIG_SYS_LONGHELP
441 return _do_help(&cmd_inkadiag_sub[0],
442 ARRAY_SIZE(cmd_inkadiag_sub), cmdtp, flag, argc, argv);
445 static int do_inkadiag(cmd_tbl_t *cmdtp, int flag, int argc,
446 char * const argv[]) {
449 c = find_cmd_tbl(argv[1], &cmd_inkadiag_sub[0], ARRAY_SIZE(cmd_inkadiag_sub));
454 return c->cmd(c, flag, argc, argv);
456 /* Unrecognized command */
457 return cmd_usage(cmdtp);
461 U_BOOT_CMD(inkadiag, 6, 1, do_inkadiag,
462 "inkadiag - inka diagnosis\n",
463 "[inkadiag what ...]\n"
464 " - perform a diagnosis on inka hardware\n"
465 "'inkadiag' performs hardware tests.");