aboutsummaryrefslogtreecommitdiff
path: root/board/cm5200/cmd_cm5200.c
blob: df372d9306c6d4f9808e9b76d1abdebfe2de95b8 (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
/*
 * (C) Copyright 2007 Markus Kappeler <markus.kappeler@objectxp.com>
 *
 * Adapted for U-Boot 1.2 by Piotr Kruszynski <ppk@semihalf.com>
 *
 * 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 <common.h>
#include <command.h>
#include <i2c.h>
#include <usb.h>

#ifdef CONFIG_CMD_BSP

static int do_i2c_test(char * const argv[])
{
	unsigned char temp, temp1;

	printf("Starting I2C Test\n"
		"Please set Jumper:\nI2C SDA 2-3\nI2C SCL 2-3\n\n"
		"Please press any key to start\n\n");
	getc();

	temp = 0xf0; /* set io 0-4 as output */
	i2c_write(CONFIG_SYS_I2C_IO, 3, 1, (uchar *)&temp, 1);

	printf("Press I2C4-7. LED I2C0-3 should have the same state\n\n"
		"Press any key to stop\n\n");

	while (!tstc()) {
		i2c_read(CONFIG_SYS_I2C_IO, 0, 1, (uchar *)&temp, 1);
		temp1 = (temp >> 4) & 0x03;
		temp1 |= (temp >> 3) & 0x08; /* S302 -> LED303 */
		temp1 |= (temp >> 5) & 0x04; /* S303 -> LED302 */
		temp = temp1;
		i2c_write(CONFIG_SYS_I2C_IO, 1, 1, (uchar *)&temp, 1);
	}
	getc();

	return 0;
}

static int do_usb_test(char * const argv[])
{
	int i;
	static int usb_stor_curr_dev = -1; /* current device */

	printf("Starting USB Test\n"
		"Please insert USB Memmory Stick\n\n"
		"Please press any key to start\n\n");
	getc();

	usb_stop();
	printf("(Re)start USB...\n");
	i = usb_init();
#ifdef CONFIG_USB_STORAGE
		/* try to recognize storage devices immediately */
		if (i >= 0)
			usb_stor_curr_dev = usb_stor_scan(1);
#endif /* CONFIG_USB_STORAGE */
	if (usb_stor_curr_dev >= 0)
		printf("Found USB Storage Dev continue with Test...\n");
	else {
		printf("No USB Storage Device detected.. Stop Test\n");
		return 1;
	}

	usb_stor_info();

	printf("stopping USB..\n");
	usb_stop();

	return 0;
}

static int do_led_test(char * const argv[])
{
	int i = 0;
	struct mpc5xxx_gpt_0_7 *gpt = (struct mpc5xxx_gpt_0_7 *)MPC5XXX_GPT;

	printf("Starting LED Test\n"
		"Please set Switch S500 all off\n\n"
		"Please press any key to start\n\n");
	getc();

	/* configure timer 2-3 for simple GPIO output High */
	gpt->gpt2.emsr |= 0x00000034;
	gpt->gpt3.emsr |= 0x00000034;

	(*(vu_long *)MPC5XXX_WU_GPIO_ENABLE) |= 0x80000000;
	(*(vu_long *)MPC5XXX_WU_GPIO_DIR) |= 0x80000000;
	printf("Please press any key to stop\n\n");
	while (!tstc()) {
		if (i == 1) {
			(*(vu_long *)MPC5XXX_WU_GPIO_DATA_O) |= 0x80000000;
			gpt->gpt2.emsr &= ~0x00000010;
			gpt->gpt3.emsr &= ~0x00000010;
		} else if (i == 2) {
			(*(vu_long *)MPC5XXX_WU_GPIO_DATA_O) &= ~0x80000000;
			gpt->gpt2.emsr &= ~0x00000010;
			gpt->gpt3.emsr |= 0x00000010;
		} else if (i >= 3) {
			(*(vu_long *)MPC5XXX_WU_GPIO_DATA_O) &= ~0x80000000;
			gpt->gpt3.emsr &= ~0x00000010;
			gpt->gpt2.emsr |= 0x00000010;
			i = 0;
		}
		i++;
		udelay(200000);
	}
	getc();

	(*(vu_long *)MPC5XXX_WU_GPIO_DATA_O) |= 0x80000000;
	gpt->gpt2.emsr |= 0x00000010;
	gpt->gpt3.emsr |= 0x00000010;

	return 0;
}

static int do_rs232_test(char * const argv[])
{
	int error_status = 0;
	struct mpc5xxx_gpio *gpio = (struct mpc5xxx_gpio *)MPC5XXX_GPIO;
	struct mpc5xxx_psc *psc1 = (struct mpc5xxx_psc *)MPC5XXX_PSC1;

	/* Configure PSC 2-3-6 as GPIO */
	gpio->port_config &= 0xFF0FF80F;

	switch (simple_strtoul(argv[2], NULL, 10)) {
	case 1:
		/* check RTS <-> CTS loop */
		/* set rts to 0 */
		printf("Uart 1 test: RX TX tested by using U-Boot\n"
			"Please connect RTS with CTS on Uart1 plug\n\n"
			"Press any key to start\n\n");
		getc();

		psc1->op1 |= 0x01;

		/* wait some time before requesting status */
		udelay(10);

		/* check status at cts */
		if ((psc1->ip & 0x01) != 0) {
			error_status = 3;
			printf("%s: failure at rs232_1, cts status is %d "
				"(should be 0)\n",
				__FUNCTION__, (psc1->ip & 0x01));
		}

		/* set rts to 1 */
		psc1->op0 |= 0x01;

		/* wait some time before requesting status */
		udelay(10);

		/* check status at cts */
		if ((psc1->ip & 0x01) != 1) {
			error_status = 3;
			printf("%s: failure at rs232_1, cts status is %d "
				"(should be 1)\n",
				__FUNCTION__, (psc1->ip & 0x01));
		}
		break;
	case 2:
		/* set PSC2_0, PSC2_2 as output and PSC2_1, PSC2_3 as input */
		printf("Uart 2 test: Please use RS232 Loopback plug on UART2\n"
			"\nPress any key to start\n\n");
		getc();

		gpio->simple_gpioe &= ~(0x000000F0);
		gpio->simple_gpioe |= 0x000000F0;
		gpio->simple_ddr &= ~(0x000000F0);
		gpio->simple_ddr |= 0x00000050;

		/* check TXD <-> RXD loop */
		/* set TXD to 1 */
		gpio->simple_dvo |= (1 << 4);

		/* wait some time before requesting status */
		udelay(10);

		if ((gpio->simple_ival & 0x00000020) != 0x00000020) {
			error_status = 2;
			printf("%s: failure at rs232_2, rxd status is %d "
				"(should be 1)\n", __FUNCTION__,
				(gpio->simple_ival & 0x00000020) >> 5);
		}

		/* set TXD to 0 */
		gpio->simple_dvo &= ~(1 << 4);

		/* wait some time before requesting status */
		udelay(10);

		if ((gpio->simple_ival & 0x00000020) != 0x00000000) {
			error_status = 2;
			printf("%s: failure at rs232_2, rxd status is %d "
				"(should be 0)\n", __FUNCTION__,
				(gpio->simple_ival & 0x00000020) >> 5);
		}

		/* check RTS <-> CTS loop */
		/* set RTS to 1 */
		gpio->simple_dvo |= (1 << 6);

		/* wait some time before requesting status */
		udelay(10);

		if ((gpio->simple_ival & 0x00000080) != 0x00000080) {
			error_status = 3;
			printf("%s: failure at rs232_2, cts status is %d "
				"(should be 1)\n", __FUNCTION__,
				(gpio->simple_ival & 0x00000080) >> 7);
		}

		/* set RTS to 0 */
		gpio->simple_dvo &= ~(1 << 6);

		/* wait some time before requesting status */
		udelay(10);

		if ((gpio->simple_ival & 0x00000080) != 0x00000000) {
			error_status = 3;
			printf("%s: failure at rs232_2, cts status is %d "
				"(should be 0)\n", __FUNCTION__,
				(gpio->simple_ival & 0x00000080) >> 7);
		}
		break;
	case 3:
		/* set PSC3_0, PSC3_2 as output and PSC3_1, PSC3_3 as input */
		printf("Uart 3 test: Please use RS232 Loopback plug on UART2\n"
			"\nPress any key to start\n\n");
		getc();

		gpio->simple_gpioe &= ~(0x00000F00);
		gpio->simple_gpioe |= 0x00000F00;

		gpio->simple_ddr &= ~(0x00000F00);
		gpio->simple_ddr |= 0x00000500;

		/* check TXD <-> RXD loop */
		/* set TXD to 1 */
		gpio->simple_dvo |= (1 << 8);

		/* wait some time before requesting status */
		udelay(10);

		if ((gpio->simple_ival & 0x00000200) != 0x00000200) {
			error_status = 2;
			printf("%s: failure at rs232_3, rxd status is %d "
				"(should be 1)\n", __FUNCTION__,
				(gpio->simple_ival & 0x00000200) >> 9);
		}

		/* set TXD to 0 */
		gpio->simple_dvo &= ~(1 << 8);

		/* wait some time before requesting status */
		udelay(10);

		if ((gpio->simple_ival & 0x00000200) != 0x00000000) {
			error_status = 2;
			printf("%s: failure at rs232_3, rxd status is %d "
				"(should be 0)\n", __FUNCTION__,
				(gpio->simple_ival & 0x00000200) >> 9);
		}

		/* check RTS <-> CTS loop */
		/* set RTS to 1 */
		gpio->simple_dvo |= (1 << 10);

		/* wait some time before requesting status */
		udelay(10);

		if ((gpio->simple_ival & 0x00000800) != 0x00000800) {
			error_status = 3;
			printf("%s: failure at rs232_3, cts status is %d "
				"(should be 1)\n", __FUNCTION__,
				(gpio->simple_ival & 0x00000800) >> 11);
		}

		/* set RTS to 0 */
		gpio->simple_dvo &= ~(1 << 10);

		/* wait some time before requesting status */
		udelay(10);

		if ((gpio->simple_ival & 0x00000800) != 0x00000000) {
			error_status = 3;
			printf("%s: failure at rs232_3, cts status is %d "
				"(should be 0)\n", __FUNCTION__,
				(gpio->simple_ival & 0x00000800) >> 11);
		}
		break;
	case 4:
		/* set PSC6_2, PSC6_3 as output and PSC6_0, PSC6_1 as input */
		printf("Uart 4 test: Please use RS232 Loopback plug on UART2\n"
			"\nPress any key to start\n\n");
		getc();

		gpio->simple_gpioe &= ~(0xF0000000);
		gpio->simple_gpioe |= 0x30000000;

		gpio->simple_ddr &= ~(0xf0000000);
		gpio->simple_ddr |= 0x30000000;

		(*(vu_long *)MPC5XXX_WU_GPIO_ENABLE) |= 0x30000000;
		(*(vu_long *)MPC5XXX_WU_GPIO_DIR) &= ~(0x30000000);

		/* check TXD <-> RXD loop */
		/* set TXD to 1 */
		gpio->simple_dvo |= (1 << 28);

		/* wait some time before requesting status */
		udelay(10);

		if (((*(vu_long *)MPC5XXX_WU_GPIO_DATA_I) & 0x10000000) !=
				0x10000000) {
			error_status = 2;
			printf("%s: failure at rs232_4, rxd status is %lu "
				"(should be 1)\n", __FUNCTION__,
				((*(vu_long *)MPC5XXX_WU_GPIO_DATA_I) &
					0x10000000) >> 28);
		}

		/* set TXD to 0 */
		gpio->simple_dvo &= ~(1 << 28);

		/* wait some time before requesting status */
		udelay(10);

		if (((*(vu_long *)MPC5XXX_WU_GPIO_DATA_I) & 0x10000000) !=
				0x00000000) {
			error_status = 2;
			printf("%s: failure at rs232_4, rxd status is %lu "
				"(should be 0)\n", __FUNCTION__,
				((*(vu_long *)MPC5XXX_WU_GPIO_DATA_I) &
					0x10000000) >> 28);
		}

		/* check RTS <-> CTS loop */
		/* set RTS to 1 */
		gpio->simple_dvo |= (1 << 29);

		/* wait some time before requesting status */
		udelay(10);

		if (((*(vu_long *)MPC5XXX_WU_GPIO_DATA_I) & 0x20000000) !=
				0x20000000) {
			error_status = 3;
			printf("%s: failure at rs232_4, cts status is %lu "
				"(should be 1)\n", __FUNCTION__,
				((*(vu_long *)MPC5XXX_WU_GPIO_DATA_I) &
					0x20000000) >> 29);
		}

		/* set RTS to 0 */
		gpio->simple_dvo &= ~(1 << 29);

		/* wait some time before requesting status */
		udelay(10);

		if (((*(vu_long *)MPC5XXX_WU_GPIO_DATA_I) & 0x20000000) !=
				0x00000000) {
			error_status = 3;
			printf("%s: failure at rs232_4, cts status is %lu "
				"(should be 0)\n", __FUNCTION__,
				((*(vu_long *)MPC5XXX_WU_GPIO_DATA_I) &
					0x20000000) >> 29);
		}
		break;
	default:
		printf("%s: invalid rs232 number %s\n", __FUNCTION__, argv[2]);
		error_status = 1;
		break;
	}
	gpio->port_config |= (CONFIG_SYS_GPS_PORT_CONFIG & 0xFF0FF80F);

	return error_status;
}

static int cmd_fkt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
	int rcode = -1;

	switch (argc) {
	case 2:
		if (strncmp(argv[1], "i2c", 3) == 0)
			rcode = do_i2c_test(argv);
		else if (strncmp(argv[1], "led", 3) == 0)
			rcode = do_led_test(argv);
		else if (strncmp(argv[1], "usb", 3) == 0)
			rcode = do_usb_test(argv);
		break;
	case 3:
		if (strncmp(argv[1], "rs232", 3) == 0)
			rcode = do_rs232_test(argv);
		break;
	}

	switch (rcode) {
	case -1:
		printf("Usage:\n"
			"fkt { i2c | led | usb }\n"
			"fkt rs232 number\n");
		rcode = 1;
		break;
	case 0:
		printf("Test passed\n");
		break;
	default:
		printf("Test failed with code: %d\n", rcode);
	}

	return rcode;
}

U_BOOT_CMD(
	fkt,	4,	1,	cmd_fkt,
	"Function test routines",
	"i2c\n"
	"     - Test I2C communication\n"
	"fkt led\n"
	"     - Test LEDs\n"
	"fkt rs232 number\n"
	"     - Test RS232 (loopback plug(s) for RS232 required)\n"
	"fkt usb\n"
	"     - Test USB communication"
);
#endif /* CONFIG_CMD_BSP */