summaryrefslogtreecommitdiff
path: root/board/inka4x0/inkadiag.c
blob: 637bb5a7b3b6a7651b64a4d2ee9f822d9e4807b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*
 * (C) Copyright 2008, 2009 Andreas Pfefferle,
 *     DENX Software Engineering, ap@denx.de.
 * (C) Copyright 2009 Detlev Zundel,
 *     DENX Software Engineering, dzu@denx.de.
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

#include <asm/io.h>
#include <common.h>
#include <config.h>
#include <mpc5xxx.h>
#include <pci.h>

#include <command.h>

/* This is needed for the includes in ns16550.h */
#define CONFIG_SYS_NS16550_REG_SIZE 1
#include <ns16550.h>

#define GPIO_BASE		((u_char *)CONFIG_SYS_CS3_START)

#define DIGIN_TOUCHSCR_MASK	0x00003000	/* Inputs 12-13 */
#define DIGIN_KEYB_MASK		0x00010000	/* Input 16 */

#define DIGIN_DRAWER_SW1	0x00400000	/* Input 22 */
#define DIGIN_DRAWER_SW2	0x00800000	/* Input 23 */

#define DIGIO_LED0		0x00000001	/* Output 0 */
#define DIGIO_LED1		0x00000002	/* Output 1 */
#define DIGIO_LED2		0x00000004	/* Output 2 */
#define DIGIO_LED3		0x00000008	/* Output 3 */
#define DIGIO_LED4		0x00000010	/* Output 4 */
#define DIGIO_LED5		0x00000020	/* Output 5 */

#define DIGIO_DRAWER1		0x00000100	/* Output 8 */
#define DIGIO_DRAWER2		0x00000200	/* Output 9 */

#define SERIAL_PORT_BASE	((u_char *)CONFIG_SYS_CS2_START)

#define PSC_OP1_RTS	0x01
#define PSC_OP0_RTS	0x01

/*
 * Table with supported baudrates (defined in inka4x0.h)
 */
static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
#define	N_BAUDRATES (sizeof(baudrate_table) / sizeof(baudrate_table[0]))

static unsigned int inka_digin_get_input(void)
{
	return in_8(GPIO_BASE + 0) << 0 | in_8(GPIO_BASE + 1) << 8 |
		in_8(GPIO_BASE + 2) << 16 | in_8(GPIO_BASE + 3) << 24;
}

#define LED_HIGH(NUM)							\
	do {								\
		setbits_be32((unsigned *)MPC5XXX_GPT##NUM##_ENABLE, 0x10); \
	} while (0)

#define LED_LOW(NUM)							\
	do {								\
		clrbits_be32((unsigned *)MPC5XXX_GPT##NUM##_ENABLE, 0x10); \
	} while (0)

#define CHECK_LED(NUM) \
    do { \
	    if (state & (1 << NUM)) {		\
		    LED_HIGH(NUM);		\
	    } else {				\
		    LED_LOW(NUM);		\
	    }					\
    } while (0)

static void inka_digio_set_output(unsigned int state, int which)
{
	volatile struct mpc5xxx_gpio *gpio = (struct mpc5xxx_gpio *)MPC5XXX_GPIO;

	if (which == 0) {
		/* other */
		CHECK_LED(0);
		CHECK_LED(1);
		CHECK_LED(2);
		CHECK_LED(3);
		CHECK_LED(4);
		CHECK_LED(5);
	} else {
		if (which == 1) {
			/* drawer1 */
			if (state) {
				clrbits_be32(&gpio->simple_dvo, 0x1000);
				udelay(1);
				setbits_be32(&gpio->simple_dvo, 0x1000);
			} else {
				setbits_be32(&gpio->simple_dvo, 0x1000);
				udelay(1);
				clrbits_be32(&gpio->simple_dvo, 0x1000);
			}
		}
		if (which == 2) {
			/* drawer 2 */
			if (state) {
				clrbits_be32(&gpio->simple_dvo, 0x2000);
				udelay(1);
				setbits_be32(&gpio->simple_dvo, 0x2000);
			} else {
				setbits_be32(&gpio->simple_dvo, 0x2000);
				udelay(1);
				clrbits_be32(&gpio->simple_dvo, 0x2000);
			}
		}
	}
	udelay(1);
}

static int do_inkadiag_io(cmd_tbl_t *cmdtp, int flag, int argc,
			  char * const argv[]) {
	unsigned int state, val;

	switch (argc) {
	case 3:
		/* Write a value */
		val = simple_strtol(argv[2], NULL, 16);

		if (strcmp(argv[1], "drawer1") == 0) {
			inka_digio_set_output(val, 1);
		} else if (strcmp(argv[1], "drawer2") == 0) {
			inka_digio_set_output(val, 2);
		} else if (strcmp(argv[1], "other") == 0)
			inka_digio_set_output(val, 0);
		else {
			printf("Invalid argument: %s\n", argv[1]);
			return -1;
		}
		/* fall through */
	case 2:
		/* Read a value */
		state = inka_digin_get_input();

		if (strcmp(argv[1], "drawer1") == 0) {
			val = (state & DIGIN_DRAWER_SW1) >> (ffs(DIGIN_DRAWER_SW1) - 1);
		} else if (strcmp(argv[1], "drawer2") == 0) {
			val = (state & DIGIN_DRAWER_SW2) >> (ffs(DIGIN_DRAWER_SW2) - 1);
		} else if (strcmp(argv[1], "other") == 0) {
			val = ((state & DIGIN_KEYB_MASK) >> (ffs(DIGIN_KEYB_MASK) - 1))
				| (state & DIGIN_TOUCHSCR_MASK) >> (ffs(DIGIN_TOUCHSCR_MASK) - 2);
		} else {
			printf("Invalid argument: %s\n", argv[1]);
			return -1;
		}
		printf("exit code: 0x%X\n", val);
		return 0;
	default:
		cmd_usage(cmdtp);
		break;
	}

	return -1;
}

DECLARE_GLOBAL_DATA_PTR;

static int ser_init(volatile struct mpc5xxx_psc *psc, int baudrate)
{
	unsigned long baseclk;
	int div;

	/* reset PSC */
	out_8(&psc->command, PSC_SEL_MODE_REG_1);

	/* select clock sources */

	out_be16(&psc->psc_clock_select, 0);
	baseclk = (gd->ipb_clk + 16) / 32;

	/* switch to UART mode */
	out_be32(&psc->sicr, 0);

	/* configure parity, bit length and so on */

	out_8(&psc->mode, PSC_MODE_8_BITS | PSC_MODE_PARNONE);
	out_8(&psc->mode, PSC_MODE_ONE_STOP);

	/* set up UART divisor */
	div = (baseclk + (baudrate / 2)) / baudrate;
	out_8(&psc->ctur, (div >> 8) & 0xff);
	out_8(&psc->ctlr, div & 0xff);

	/* disable all interrupts */
	out_be16(&psc->psc_imr, 0);

	/* reset and enable Rx/Tx */
	out_8(&psc->command, PSC_RST_RX);
	out_8(&psc->command, PSC_RST_TX);
	out_8(&psc->command, PSC_RX_ENABLE | PSC_TX_ENABLE);

	return 0;
}

static void ser_putc(volatile struct mpc5xxx_psc *psc, const char c)
{
	/* Wait 1 second for last character to go. */
	int i = 0;

	while (!(psc->psc_status & PSC_SR_TXEMP) && (i++ < 1000000/10))
		udelay(10);
	psc->psc_buffer_8 = c;

}

static int ser_getc(volatile struct mpc5xxx_psc *psc)
{
	/* Wait for a character to arrive. */
	int i = 0;

	while (!(in_be16(&psc->psc_status) & PSC_SR_RXRDY) && (i++ < 1000000/10))
		udelay(10);

	return in_8(&psc->psc_buffer_8);
}

static int do_inkadiag_serial(cmd_tbl_t *cmdtp, int flag, int argc,
			      char * const argv[]) {
	volatile struct NS16550 *uart;
	volatile struct mpc5xxx_psc *psc;
	unsigned int num, mode;
	int combrd, baudrate, i, j, len;
	int address;

	if (argc < 5) {
		cmd_usage(cmdtp);
		return 1;
	}

	argc--;
	argv++;

	num = simple_strtol(argv[0], NULL, 0);
	if (num < 0 || num > 11) {
		printf("invalid argument for num: %d\n", num);
		return -1;
	}

	mode = simple_strtol(argv[1], NULL, 0);

	combrd = 0;
	baudrate = simple_strtoul(argv[2], NULL, 10);
	for (i=0; i<N_BAUDRATES; ++i) {
		if (baudrate == baudrate_table[i])
			break;
	}
	if (i == N_BAUDRATES) {
		printf("## Baudrate %d bps not supported\n",
		       baudrate);
		return 1;
	}
	combrd = 115200 / baudrate;

	uart = (struct NS16550 *)(SERIAL_PORT_BASE + (num << 3));

	printf("Testing uart %d.\n\n", num);

	if ((num >= 0) && (num <= 7)) {
		if (mode & 1) {
			/* turn on 'loopback' mode */
			out_8(&uart->mcr, UART_MCR_LOOP);
		} else {
			/*
			 * establish the UART's operational parameters
			 * set DLAB=1, so rbr accesses DLL
			 */
			out_8(&uart->lcr, UART_LCR_DLAB);
			/* set baudrate */
			out_8(&uart->rbr, combrd);
			/* set data-format: 8-N-1 */
			out_8(&uart->lcr, UART_LCR_WLS_8);
		}

		if (mode & 2) {
			/* set request to send */
			out_8(&uart->mcr, UART_MCR_RTS);
			udelay(10);
			/* check clear to send */
			if ((in_8(&uart->msr) & UART_MSR_CTS) == 0x00)
				return -1;
		}
		if (mode & 4) {
			/* set data terminal ready */
			out_8(&uart->mcr, UART_MCR_DTR);
			udelay(10);
			/* check data set ready and carrier detect */
			if ((in_8(&uart->msr) & (UART_MSR_DSR | UART_MSR_DCD))
			    != (UART_MSR_DSR | UART_MSR_DCD))
				return -1;
		}

		/* write each message-character, read it back, and display it */
		for (i = 0, len = strlen(argv[3]); i < len; ++i) {
			j = 0;
			while ((in_8(&uart->lsr) & UART_LSR_THRE) ==	0x00) {
				if (j++ > CONFIG_SYS_HZ)
					break;
				udelay(10);
			}
			out_8(&uart->rbr, argv[3][i]);
			j = 0;
			while ((in_8(&uart->lsr) & UART_LSR_DR) == 0x00) {
				if (j++ > CONFIG_SYS_HZ)
					break;
				udelay(10);
			}
			printf("%c", in_8(&uart->rbr));
		}
		printf("\n\n");
		out_8(&uart->mcr, 0x00);
	} else {
		address = 0;

		switch (num) {
		case 8:
			address = MPC5XXX_PSC6;
			break;
		case 9:
			address = MPC5XXX_PSC3;
			break;
		case 10:
			address = MPC5XXX_PSC2;
			break;
		case 11:
			address = MPC5XXX_PSC1;
			break;
		}
		psc = (struct mpc5xxx_psc *)address;
		ser_init(psc, simple_strtol(argv[2], NULL, 0));
		if (mode & 2) {
			/* set request to send */
			out_8(&psc->op0, PSC_OP0_RTS);
			udelay(10);
			/* check clear to send */
			if ((in_8(&psc->ip) & PSC_IPCR_CTS) == 0)
				return -1;
		}
		len = strlen(argv[3]);
		for (i = 0; i < len; ++i) {
			ser_putc(psc, argv[3][i]);
			printf("%c", ser_getc(psc));
		}
		printf("\n\n");
	}
	return 0;
}

#define BUZZER_GPT	(MPC5XXX_GPT + 0x60)	/* GPT6 */
static void buzzer_turn_on(unsigned int freq)
{
	volatile struct mpc5xxx_gpt *gpt = (struct mpc5xxx_gpt *)(BUZZER_GPT);

	const u32 prescale = gd->ipb_clk / freq / 128;
	const u32 count = 128;
	const u32 width = 64;

	gpt->cir = (prescale << 16) | count;
	gpt->pwmcr = width << 16;
	gpt->emsr = 3;		/* Timer enabled for PWM */
}

static void buzzer_turn_off(void)
{
	volatile struct mpc5xxx_gpt *gpt = (struct mpc5xxx_gpt *)(BUZZER_GPT);

	gpt->emsr = 0;
}

static int do_inkadiag_buzzer(cmd_tbl_t *cmdtp, int flag, int argc,
			      char * const argv[]) {

	unsigned int period, freq;
	int prev, i;

	if (argc != 3) {
		cmd_usage(cmdtp);
		return 1;
	}

	argc--;
	argv++;

	period = simple_strtol(argv[0], NULL, 0);
	if (!period)
		printf("Zero period is senseless\n");
	argc--;
	argv++;

	freq = simple_strtol(argv[0], NULL, 0);
	/* avoid zero prescale in buzzer_turn_on() */
	if (freq > gd->ipb_clk / 128) {
		printf("%dHz exceeds maximum (%ldHz)\n", freq,
		       gd->ipb_clk / 128);
	} else if (!freq)
		printf("Zero frequency is senseless\n");
	else
		buzzer_turn_on(freq);

	clear_ctrlc();
	prev = disable_ctrlc(0);

	printf("Buzzing for %d ms. Type ^C to abort!\n\n", period);

	i = 0;
	while (!ctrlc() && (i++ < CONFIG_SYS_HZ))
		udelay(period);

	clear_ctrlc();
	disable_ctrlc(prev);

	buzzer_turn_off();

	return 0;
}

static int do_inkadiag_help(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);

cmd_tbl_t cmd_inkadiag_sub[] = {
	U_BOOT_CMD_MKENT(io, 1, 1, do_inkadiag_io, "read digital input",
	 "<drawer1|drawer2|other> [value] - get or set specified signal"),
	U_BOOT_CMD_MKENT(serial, 4, 1, do_inkadiag_serial, "test serial port",
	 "<num> <mode> <baudrate> <msg>  - test uart num [0..11] in mode\n"
	 "and baudrate with msg"),
	U_BOOT_CMD_MKENT(buzzer, 2, 1, do_inkadiag_buzzer, "activate buzzer",
	 "<period> <freq> - turn buzzer on for period ms with freq hz"),
	U_BOOT_CMD_MKENT(help, 4, 1, do_inkadiag_help, "get help",
	 "[command] - get help for command"),
};

static int do_inkadiag_help(cmd_tbl_t *cmdtp, int flag,
			    int argc, char * const argv[]) {
	extern int _do_help (cmd_tbl_t *cmd_start, int cmd_items,
			     cmd_tbl_t *cmdtp, int flag,
			     int argc, char * const argv[]);
	/* do_help prints command name - we prepend inkadiag to our subcommands! */
#ifdef CONFIG_SYS_LONGHELP
	puts ("inkadiag ");
#endif
	return _do_help(&cmd_inkadiag_sub[0],
		ARRAY_SIZE(cmd_inkadiag_sub), cmdtp, flag, argc, argv);
}

static int do_inkadiag(cmd_tbl_t *cmdtp, int flag, int argc,
		       char * const argv[]) {
	cmd_tbl_t *c;

	c = find_cmd_tbl(argv[1], &cmd_inkadiag_sub[0], ARRAY_SIZE(cmd_inkadiag_sub));

	if (c) {
		argc--;
		argv++;
		return c->cmd(c, flag, argc, argv);
	} else {
		/* Unrecognized command */
		cmd_usage(cmdtp);
		return 1;
	}
}

U_BOOT_CMD(inkadiag, 6, 1, do_inkadiag,
	   "inkadiag - inka diagnosis\n",
	   "[inkadiag what ...]\n"
	   "    - perform a diagnosis on inka hardware\n"
	   "'inkadiag' performs hardware tests.");