summaryrefslogtreecommitdiff
path: root/board/tqc/tqm5200/cmd_stk52xx.c
blob: 0789c5848eb1cabb8f7b7871b626cf8d696a5ec0 (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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
/*
 * (C) Copyright 2005
 * Martin Krause, TQ-Systems GmbH, martin.krause@tqs.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
 */

/*
 * STK52XX specific functions
 */
/*#define DEBUG*/

#include <common.h>
#include <command.h>

#if defined(CONFIG_CMD_BSP)

#if defined(CONFIG_STK52XX) || defined(CONFIG_FO300)
#define DEFAULT_VOL	45
#define DEFAULT_FREQ	500
#define DEFAULT_DURATION	200
#define LEFT		1
#define RIGHT		2
#define LEFT_RIGHT	3
#define BL_OFF		0
#define BL_ON		1

#define SM501_GPIO_CTRL_LOW		0x00000008UL
#define SM501_GPIO_CTRL_HIGH		0x0000000CUL
#define SM501_POWER_MODE0_GATE		0x00000040UL
#define SM501_POWER_MODE1_GATE		0x00000048UL
#define POWER_MODE_GATE_GPIO_PWM_I2C	0x00000040UL
#define SM501_GPIO_DATA_LOW		0x00010000UL
#define SM501_GPIO_DATA_HIGH		0x00010004UL
#define SM501_GPIO_DATA_DIR_LOW		0x00010008UL
#define SM501_GPIO_DATA_DIR_HIGH	0x0001000CUL
#define SM501_PANEL_DISPLAY_CONTROL	0x00080000UL

static int i2s_squarewave(unsigned long duration, unsigned int freq,
			  unsigned int channel);
static int i2s_sawtooth(unsigned long duration, unsigned int freq,
			unsigned int channel);
static void spi_init(void);
static int spi_transmit(unsigned char data);
static void pcm1772_write_reg(unsigned char addr, unsigned char data);
static void set_attenuation(unsigned char attenuation);

static void spi_init(void)
{
	struct mpc5xxx_spi *spi = (struct mpc5xxx_spi*)MPC5XXX_SPI;
	struct mpc5xxx_gpio *gpio = (struct mpc5xxx_gpio*)MPC5XXX_GPIO;

	/* PSC3 as SPI and GPIOs */
	gpio->port_config &= 0xFFFFF0FF;
	gpio->port_config |= 0x00000800;
	/*
	 * Its important to use the correct order when initializing the
	 * registers
	 */
	spi->ddr = 0x0F; /* set all SPI pins as output */
	spi->pdr = 0x08; /* set SS high */
	spi->cr1 = 0x50; /* SPI is master, SS is general purpose output */
	spi->cr2 = 0x00; /* normal operation */
	spi->brr = 0xFF; /* baud rate: IPB clock / 2048 */
}

static int spi_transmit(unsigned char data)
{
	int dummy;
	struct mpc5xxx_spi *spi = (struct mpc5xxx_spi*)MPC5XXX_SPI;

	spi->dr = data;
	/* wait for SPI transmission completed */
	while(!(spi->sr & 0x80))
	{
		if (spi->sr & 0x40)	/* if write collision occured */
		{
			/* do dummy read to clear status register */
			dummy = spi->dr;
			printf ("SPI write collision\n");
			return -1;
		}
	}
	return (spi->dr);
}

static void pcm1772_write_reg(unsigned char addr, unsigned char data)
{
	struct mpc5xxx_spi *spi = (struct mpc5xxx_spi*)MPC5XXX_SPI;

	spi->pdr = 0x00; /* Set SS low */
	spi_transmit(addr);
	spi_transmit(data);
	/* wait some time to meet MS# hold time of PCM1772 */
	udelay (1);
	spi->pdr = 0x08; /* set SS high */
}

static void set_attenuation(unsigned char attenuation)
{
	pcm1772_write_reg(0x01, attenuation); /* left channel */
	debug ("PCM1772 attenuation left set to %d.\n", attenuation);
	pcm1772_write_reg(0x02, attenuation); /* right channel */
	debug ("PCM1772 attenuation right set to %d.\n", attenuation);
}

void amplifier_init(void)
{
	static int init_done = 0;
	int i;
	struct mpc5xxx_gpio *gpio = (struct mpc5xxx_gpio*)MPC5XXX_GPIO;

	/* Do this only once, because of the long time delay */
	if (!init_done) {
		/* configure PCM1772 audio format as I2S */
		pcm1772_write_reg(0x03, 0x01);
		/* enable audio amplifier */
		gpio->sint_gpioe |=  0x02;	/* PSC3_5 as GPIO */
		gpio->sint_ode &= ~0x02;	/* PSC3_5 is not open Drain */
		gpio->sint_dvo &= ~0x02;	/* PSC3_5 is LOW */
		gpio->sint_ddr |=  0x02;	/* PSC3_5 as output */
		/*
		 * wait some time to allow amplifier to recover from shutdown
		 * mode.
		 */
		for(i = 0; i < 350; i++)
			udelay(1000);
		/*
		 * The used amplifier (LM4867) has a so called "pop and click"
		 * elmination filter. The input signal of the amplifier must
		 * exceed a certain level once after power up to activate the
		 * generation of the output signal. This is achieved by
		 * sending a low frequent (nearly inaudible) sawtooth with a
		 * sufficient signal level.
		 */
		set_attenuation(50);
		i2s_sawtooth (200, 5, LEFT_RIGHT);
		init_done = 1;
	}
}

static void i2s_init(void)
{
	unsigned long i;
	struct mpc5xxx_psc *psc = (struct mpc5xxx_psc*)MPC5XXX_PSC2;;
	struct mpc5xxx_gpio *gpio = (struct mpc5xxx_gpio*)MPC5XXX_GPIO;

	gpio->port_config |= 0x00000070; /* PSC2 ports as Codec with MCLK */
	psc->command = (PSC_RX_DISABLE | PSC_TX_DISABLE);
	psc->sicr = 0x22E00000;		/* 16 bit data; I2S */

	*(vu_long *)(CONFIG_SYS_MBAR + 0x22C) = 0x805d; /* PSC2 CDM MCLK config; MCLK
						  * 5.617 MHz */
	*(vu_long *)(CONFIG_SYS_MBAR + 0x214) |= 0x00000040; /* CDM clock enable
						       * register */
	psc->ccr = 0x1F03;	/* 16 bit data width; 5.617MHz MCLK */
	psc->ctur = 0x0F;	/* 16 bit frame width */

	for(i=0;i<128;i++)
	{
		psc->psc_buffer_32 = 0; /* clear tx fifo */
	}
}

static int i2s_play_wave(unsigned long addr, unsigned long len)
{
	unsigned long i;
	unsigned char *wave_file = (uchar *)addr + 44;	/* quick'n dirty: skip
							 * wav header*/
	unsigned char swapped[4];
	struct mpc5xxx_psc *psc = (struct mpc5xxx_psc*)MPC5XXX_PSC2;

	/*
	 * play wave file in memory; bytes/words are be swapped
	 */
	psc->command = (PSC_RX_ENABLE | PSC_TX_ENABLE);

	for(i = 0;i < (len / 4); i++) {
		swapped[3] = *wave_file++;
		swapped[2] = *wave_file++;
		swapped[1] = *wave_file++;
		swapped[0] = *wave_file++;
		psc->psc_buffer_32 =  *((unsigned long*)swapped);
		while (psc->tfnum > 400) {
			if(ctrlc())
				return 0;
		}
	}
	while (psc->tfnum > 0);		/* wait for fifo empty */
	udelay (100);
	psc->command = (PSC_RX_DISABLE | PSC_TX_DISABLE);
	return 0;
}

static int i2s_sawtooth(unsigned long duration, unsigned int freq,
			unsigned int channel)
{
	long i,j;
	unsigned long data;
	struct mpc5xxx_psc *psc = (struct mpc5xxx_psc*)MPC5XXX_PSC2;

	psc->command = (PSC_RX_ENABLE | PSC_TX_ENABLE);

	/*
	 * Generate sawtooth. Start with middle level up to highest level. Then
	 * go to lowest level and back to middle level.
	 */
	for(j = 0; j < ((duration * freq) / 1000); j++)	{
		for(i = 0; i <= 0x7FFF; i += (0x7FFF/(44100/(freq*4))))	{
			data = (i & 0xFFFF);
			/* data format: right data left data) */
			if (channel == LEFT_RIGHT)
				data |= (data<<16);
			if (channel == RIGHT)
				data = (data<<16);
			psc->psc_buffer_32 = data;
			while (psc->tfnum > 400);
		}
		for(i = 0x7FFF; i >= -0x7FFF; i -= (0xFFFF/(44100/(freq*2)))) {
			data = (i & 0xFFFF);
			/* data format: right data left data) */
			if (channel == LEFT_RIGHT)
				data |= (data<<16);
			if (channel == RIGHT)
				data = (data<<16);
			psc->psc_buffer_32 = data;
			while (psc->tfnum > 400);
		}
		for(i = -0x7FFF; i <= 0; i += (0x7FFF/(44100/(freq*4)))) {
			data = (i & 0xFFFF);
			/* data format: right data left data) */
			if (channel == LEFT_RIGHT)
				data |= (data<<16);
			if (channel == RIGHT)
				data = (data<<16);
			psc->psc_buffer_32 = data;
			while (psc->tfnum > 400);
		}
	}
	while (psc->tfnum > 0);		/* wait for fifo empty */
	udelay (100);
	psc->command = (PSC_RX_DISABLE | PSC_TX_DISABLE);

	return 0;
}

static int i2s_squarewave(unsigned long duration, unsigned int freq,
			 unsigned int channel)
{
	long i,j;
	unsigned long data;
	struct mpc5xxx_psc *psc = (struct mpc5xxx_psc*)MPC5XXX_PSC2;

	psc->command = (PSC_RX_ENABLE | PSC_TX_ENABLE);

	/*
	 * Generate sqarewave. Start with high level, duty cycle 1:1.
	 */
	for(j = 0; j < ((duration * freq) / 1000); j++)	{
		for(i = 0; i < (44100/(freq*2)); i ++) {
			data = 0x7FFF;
			/* data format: right data left data) */
			if (channel == LEFT_RIGHT)
				data |= (data<<16);
			if (channel == RIGHT)
				data = (data<<16);
			psc->psc_buffer_32 = data;
			while (psc->tfnum > 400);
		}
		for(i = 0; i < (44100/(freq*2)); i ++) {
			data = 0x8000;
			/* data format: right data left data) */
			if (channel == LEFT_RIGHT)
				data |= (data<<16);
			if (channel == RIGHT)
				data = (data<<16);
			psc->psc_buffer_32 = data;
			while (psc->tfnum > 400);
		}
	}
	while (psc->tfnum > 0);		/* wait for fifo empty */
	udelay (100);
	psc->command = (PSC_RX_DISABLE | PSC_TX_DISABLE);

	return 0;
}

static int cmd_sound(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
	unsigned long reg, val, duration;
	char *tmp;
	unsigned int freq, channel;
	unsigned char volume;
	int rcode = 1;

#ifdef CONFIG_STK52XX_REV100
	printf ("Revision 100 of STK52XX not supported!\n");
	return 1;
#endif
	spi_init();
	i2s_init();
	amplifier_init();

	if ((tmp = getenv ("volume")) != NULL) {
		volume = simple_strtoul (tmp, NULL, 10);
	} else {
		volume = DEFAULT_VOL;
	}
	set_attenuation(volume);

	switch (argc) {
	case 0:
	case 1:
		return cmd_usage(cmdtp);
	case 2:
		if (strncmp(argv[1],"saw",3) == 0) {
			printf ("Play sawtooth\n");
			rcode = i2s_sawtooth (DEFAULT_DURATION, DEFAULT_FREQ,
					      LEFT_RIGHT);
			return rcode;
		} else if (strncmp(argv[1],"squ",3) == 0) {
			printf ("Play squarewave\n");
			rcode = i2s_squarewave (DEFAULT_DURATION, DEFAULT_FREQ,
						LEFT_RIGHT);
			return rcode;
		}

		return cmd_usage(cmdtp);
	case 3:
		if (strncmp(argv[1],"saw",3) == 0) {
			duration = simple_strtoul(argv[2], NULL, 10);
			printf ("Play sawtooth\n");
			rcode = i2s_sawtooth (duration, DEFAULT_FREQ,
					      LEFT_RIGHT);
			return rcode;
		} else if (strncmp(argv[1],"squ",3) == 0) {
			duration = simple_strtoul(argv[2], NULL, 10);
			printf ("Play squarewave\n");
			rcode = i2s_squarewave (duration, DEFAULT_FREQ,
						LEFT_RIGHT);
			return rcode;
		}
		return cmd_usage(cmdtp);
	case 4:
		if (strncmp(argv[1],"saw",3) == 0) {
			duration = simple_strtoul(argv[2], NULL, 10);
			freq = (unsigned int)simple_strtoul(argv[3], NULL, 10);
			printf ("Play sawtooth\n");
			rcode = i2s_sawtooth (duration, freq,
					      LEFT_RIGHT);
			return rcode;
		} else if (strncmp(argv[1],"squ",3) == 0) {
			duration = simple_strtoul(argv[2], NULL, 10);
			freq = (unsigned int)simple_strtoul(argv[3], NULL, 10);
			printf ("Play squarewave\n");
			rcode = i2s_squarewave (duration, freq,
						LEFT_RIGHT);
			return rcode;
		} else if (strcmp(argv[1],"pcm1772") == 0) {
			reg = simple_strtoul(argv[2], NULL, 10);
			val = simple_strtoul(argv[3], NULL, 10);
			printf("Set PCM1772 %lu. %lu\n", reg, val);
			pcm1772_write_reg((uchar)reg, (uchar)val);
			return 0;
		}
		return cmd_usage(cmdtp);
	case 5:
		if (strncmp(argv[1],"saw",3) == 0) {
			duration = simple_strtoul(argv[2], NULL, 10);
			freq = (unsigned int)simple_strtoul(argv[3], NULL, 10);
			if (strncmp(argv[4],"l",1) == 0)
				channel = LEFT;
			else if (strncmp(argv[4],"r",1) == 0)
				channel = RIGHT;
			else
				channel = LEFT_RIGHT;
			printf ("Play squarewave\n");
			rcode = i2s_sawtooth (duration, freq,
					      channel);
			return rcode;
		} else if (strncmp(argv[1],"squ",3) == 0) {
			duration = simple_strtoul(argv[2], NULL, 10);
			freq = (unsigned int)simple_strtoul(argv[3], NULL, 10);
			if (strncmp(argv[4],"l",1) == 0)
				channel = LEFT;
			else if (strncmp(argv[4],"r",1) == 0)
				channel = RIGHT;
			else
				channel = LEFT_RIGHT;
			printf ("Play squarewave\n");
			rcode = i2s_squarewave (duration, freq,
						channel);
			return rcode;
		}
		return cmd_usage(cmdtp);
	}
	printf ("Usage:\nsound cmd [arg1] [arg2] ...\n");
	return 1;
}

static int cmd_wav(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
	unsigned long length, addr;
	unsigned char volume;
	int rcode = 1;
	char *tmp;

#ifdef CONFIG_STK52XX_REV100
	printf ("Revision 100 of STK52XX not supported!\n");
	return 1;
#endif
	spi_init();
	i2s_init();
	amplifier_init();

	switch (argc) {

	case 3:
		length = simple_strtoul(argv[2], NULL, 16);
		addr = simple_strtoul(argv[1], NULL, 16);
		break;

	case 2:
		if ((tmp = getenv ("filesize")) != NULL) {
			length = simple_strtoul (tmp, NULL, 16);
		} else {
			puts ("No filesize provided\n");
			return 1;
		}
		addr = simple_strtoul(argv[1], NULL, 16);

	case 1:
		if ((tmp = getenv ("filesize")) != NULL) {
			length = simple_strtoul (tmp, NULL, 16);
		} else {
			puts ("No filesize provided\n");
			return 1;
		}
		if ((tmp = getenv ("loadaddr")) != NULL) {
			addr = simple_strtoul (tmp, NULL, 16);
		} else {
			puts ("No loadaddr provided\n");
			return 1;
		}
		break;

	default:
		printf("Usage:\nwav <addr> <length[s]\n");
		return 1;
		break;
	}

	if ((tmp = getenv ("volume")) != NULL) {
		volume = simple_strtoul (tmp, NULL, 10);
	} else {
		volume = DEFAULT_VOL;
	}
	set_attenuation(volume);

	printf("Play wave file at %lX with length %lX\n", addr, length);
	rcode = i2s_play_wave(addr, length);

	return rcode;
}

static int cmd_beep(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
	unsigned char volume;
	unsigned int channel;
	int rcode;
	char *tmp;

#ifdef CONFIG_STK52XX_REV100
	printf ("Revision 100 of STK52XX not supported!\n");
	return 1;
#endif
	spi_init();
	i2s_init();
	amplifier_init();

	switch (argc) {
	case 0:
	case 1:
		channel = LEFT_RIGHT;
		break;
	case 2:
		if (strncmp(argv[1],"l",1) == 0)
			channel = LEFT;
		else if (strncmp(argv[1],"r",1) == 0)
			channel = RIGHT;
		else
			channel = LEFT_RIGHT;
		break;
	default:
		return cmd_usage(cmdtp);
	}

	if ((tmp = getenv ("volume")) != NULL) {
		volume = simple_strtoul (tmp, NULL, 10);
	} else {
		volume = DEFAULT_VOL;
	}
	set_attenuation(volume);

	printf("Beep on ");
	if (channel == LEFT)
		printf ("left ");
	else if (channel == RIGHT)
		printf ("right ");
	else
		printf ("left and right ");
	printf ("channel\n");

	rcode = i2s_squarewave (DEFAULT_DURATION, DEFAULT_FREQ, channel);

	return rcode;
}
#endif

#if defined(CONFIG_STK52XX)
void led_init(void)
{
	struct mpc5xxx_gpio *gpio = (struct mpc5xxx_gpio *)MPC5XXX_GPIO;
	struct mpc5xxx_gpt_0_7 *gpt = (struct mpc5xxx_gpt_0_7 *)MPC5XXX_GPT;

	/* configure PSC3 for SPI and GPIO */
	gpio->port_config &= ~(0x00000F00);
	gpio->port_config |=   0x00000800;

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

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

	/* configure timer 4-7 for simple GPIO output */
	gpt->gpt4.emsr |=  0x00000024;
	gpt->gpt5.emsr |=  0x00000024;
	gpt->gpt6.emsr |=  0x00000024;
	gpt->gpt7.emsr |=  0x00000024;

#ifndef CONFIG_TQM5200S
	/* enable SM501 GPIO control (in both power modes) */
	*(vu_long *) (SM501_MMIO_BASE+SM501_POWER_MODE0_GATE) |=
		POWER_MODE_GATE_GPIO_PWM_I2C;
	*(vu_long *) (SM501_MMIO_BASE+SM501_POWER_MODE1_GATE) |=
		POWER_MODE_GATE_GPIO_PWM_I2C;

	/* configure SM501 gpio pins 24-27 as output */
	*(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_CTRL_LOW) &= ~(0xF << 24);
	*(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_DIR_LOW) |= (0xF << 24);

	/* configure SM501 gpio pins 48-51 as output */
	*(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_DIR_HIGH) |= (0xF << 16);
#endif /* !CONFIG_TQM5200S */
}

/*
 * return 1 if led number unknown
 * return 0 else
 */
int do_led(char * const argv[])
{
	struct mpc5xxx_gpio *gpio = (struct mpc5xxx_gpio *)MPC5XXX_GPIO;
	struct mpc5xxx_gpt_0_7 *gpt = (struct mpc5xxx_gpt_0_7 *)MPC5XXX_GPT;

	switch	(simple_strtoul(argv[2], NULL, 10)) {

	case 0:
		if (strcmp (argv[3], "on") == 0) {
			gpio->simple_dvo |=   (1 << 8);
		} else {
			gpio->simple_dvo &= ~(1 << 8);
		}
		break;

	case 1:
		if (strcmp (argv[3], "on") == 0) {
			gpio->simple_dvo |=   (1 << 9);
		} else {
			gpio->simple_dvo &= ~(1 << 9);
		}
		break;

	case 2:
		if (strcmp (argv[3], "on") == 0) {
			gpio->simple_dvo |=   (1 << 10);
		} else {
			gpio->simple_dvo &= ~(1 << 10);
		}
		break;

	case 3:
		if (strcmp (argv[3], "on") == 0) {
			gpio->simple_dvo |=   (1 << 11);
		} else {
			gpio->simple_dvo &= ~(1 << 11);
		}
		break;

	case 4:
		if (strcmp (argv[3], "on") == 0) {
			gpt->gpt4.emsr |=  (1 << 4);
		} else {
			gpt->gpt4.emsr &=  ~(1 << 4);
		}
		break;

	case 5:
		if (strcmp (argv[3], "on") == 0) {
			gpt->gpt5.emsr |=  (1 << 4);
		} else {
			gpt->gpt5.emsr &=  ~(1 << 4);
		}
		break;

	case 6:
		if (strcmp (argv[3], "on") == 0) {
			gpt->gpt6.emsr |=  (1 << 4);
		} else {
			gpt->gpt6.emsr &=  ~(1 << 4);
		}
		break;

	case 7:
		if (strcmp (argv[3], "on") == 0) {
			gpt->gpt7.emsr |=  (1 << 4);
		} else {
			gpt->gpt7.emsr &=  ~(1 << 4);
		}
		break;
#ifndef CONFIG_TQM5200S
	case 24:
		if (strcmp (argv[3], "on") == 0) {
			*(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_LOW) |=
				(0x1 << 24);
		} else {
			*(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_LOW) &=
				~(0x1 << 24);
		}
		break;

	case 25:
		if (strcmp (argv[3], "on") == 0) {
			*(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_LOW) |=
				(0x1 << 25);
		} else {
			*(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_LOW) &=
				~(0x1 << 25);
		}
		break;

	case 26:
		if (strcmp (argv[3], "on") == 0) {
			*(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_LOW) |=
				(0x1 << 26);
		} else {
			*(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_LOW) &=
				~(0x1 << 26);
		}
		break;

	case 27:
		if (strcmp (argv[3], "on") == 0) {
			*(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_LOW) |=
				(0x1 << 27);
		} else {
			*(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_LOW) &=
				~(0x1 << 27);
		}
		break;

	case 48:
		if (strcmp (argv[3], "on") == 0) {
			*(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_HIGH) |=
				(0x1 << 16);
		} else {
			*(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_HIGH) &=
				~(0x1 << 16);
		}
		break;

	case 49:
		if (strcmp (argv[3], "on") == 0) {
			*(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_HIGH) |=
				(0x1 << 17);
		} else {
			*(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_HIGH) &=
				~(0x1 << 17);
		}
		break;

	case 50:
		if (strcmp (argv[3], "on") == 0) {
			*(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_HIGH) |=
				(0x1 << 18);
		} else {
			*(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_HIGH) &=
				~(0x1 << 18);
		}
		break;

	case 51:
		if (strcmp (argv[3], "on") == 0) {
			*(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_HIGH) |=
				(0x1 << 19);
		} else {
			*(vu_long *) (SM501_MMIO_BASE+SM501_GPIO_DATA_HIGH) &=
				~(0x1 << 19);
		}
		break;
#endif /* !CONFIG_TQM5200S */
	default:
		printf ("%s: invalid led number %s\n", __FUNCTION__, argv[2]);
		return 1;
	}

	return 0;
}
#endif

#if defined(CONFIG_STK52XX) || defined(CONFIG_FO300)
/*
 * return 1 on CAN initialization failure
 * return 0 if no failure
 */
int can_init(void)
{
	static int init_done = 0;
	int i;
	struct mpc5xxx_mscan *can1 =
		(struct mpc5xxx_mscan *)(CONFIG_SYS_MBAR + 0x0900);
	struct mpc5xxx_mscan *can2 =
		(struct mpc5xxx_mscan *)(CONFIG_SYS_MBAR + 0x0980);

	/* GPIO configuration of the CAN pins is done in TQM5200.h */

	if (!init_done) {
		/* init CAN 1 */
		can1->canctl1 |= 0x80;	/* CAN enable */
		udelay(100);

		i = 0;
		can1->canctl0 |= 0x02;	/* sleep mode */
		/* wait until sleep mode reached */
		while (!(can1->canctl1 & 0x02)) {
			udelay(10);
		i++;
		if (i == 10) {
			printf ("%s: CAN1 initialize error, "
				"can not enter sleep mode!\n",
				__FUNCTION__);
			return 1;
		}
		}
		i = 0;
		can1->canctl0 = 0x01;	/* enter init mode */
		/* wait until init mode reached */
		while (!(can1->canctl1 & 0x01)) {
			udelay(10);
			i++;
			if (i == 10) {
				printf ("%s: CAN1 initialize error, "
					"can not enter init mode!\n",
					__FUNCTION__);
				return 1;
			}
		}
		can1->canctl1 = 0x80;
		can1->canctl1 |= 0x40;
		can1->canbtr0 = 0x0F;
		can1->canbtr1 = 0x7F;
		can1->canidac &= ~(0x30);
		can1->canidar1 = 0x00;
		can1->canidar3 = 0x00;
		can1->canidar5 = 0x00;
		can1->canidar7 = 0x00;
		can1->canidmr0 = 0xFF;
		can1->canidmr1 = 0xFF;
		can1->canidmr2 = 0xFF;
		can1->canidmr3 = 0xFF;
		can1->canidmr4 = 0xFF;
		can1->canidmr5 = 0xFF;
		can1->canidmr6 = 0xFF;
		can1->canidmr7 = 0xFF;

		i = 0;
		can1->canctl0 &= ~(0x01);	/* leave init mode */
		can1->canctl0 &= ~(0x02);
		/* wait until init and sleep mode left */
		while ((can1->canctl1 & 0x01) || (can1->canctl1 & 0x02)) {
			udelay(10);
			i++;
			if (i == 10) {
				printf ("%s: CAN1 initialize error, "
					"can not leave init/sleep mode!\n",
					__FUNCTION__);
				return 1;
			}
		}

		/* init CAN 2 */
		can2->canctl1 |= 0x80;	/* CAN enable */
		udelay(100);

		i = 0;
		can2->canctl0 |= 0x02;	/* sleep mode */
		/* wait until sleep mode reached */
		while (!(can2->canctl1 & 0x02))	{
			udelay(10);
			i++;
			if (i == 10) {
				printf ("%s: CAN2 initialize error, "
					"can not enter sleep mode!\n",
					__FUNCTION__);
				return 1;
			}
		}
		i = 0;
		can2->canctl0 = 0x01;	/* enter init mode */
		/* wait until init mode reached */
		while (!(can2->canctl1 & 0x01))	{
			udelay(10);
			i++;
			if (i == 10) {
				printf ("%s: CAN2 initialize error, "
					"can not enter init mode!\n",
					__FUNCTION__);
				return 1;
			}
		}
		can2->canctl1 = 0x80;
		can2->canctl1 |= 0x40;
		can2->canbtr0 = 0x0F;
		can2->canbtr1 = 0x7F;
		can2->canidac &= ~(0x30);
		can2->canidar1 = 0x00;
		can2->canidar3 = 0x00;
		can2->canidar5 = 0x00;
		can2->canidar7 = 0x00;
		can2->canidmr0 = 0xFF;
		can2->canidmr1 = 0xFF;
		can2->canidmr2 = 0xFF;
		can2->canidmr3 = 0xFF;
		can2->canidmr4 = 0xFF;
		can2->canidmr5 = 0xFF;
		can2->canidmr6 = 0xFF;
		can2->canidmr7 = 0xFF;
		can2->canctl0 &= ~(0x01);	/* leave init mode */
		can2->canctl0 &= ~(0x02);

		i = 0;
		/* wait until init mode left */
		while ((can2->canctl1 & 0x01) || (can2->canctl1 & 0x02)) {
			udelay(10);
			i++;
			if (i == 10) {
				printf ("%s: CAN2 initialize error, "
					"can not leave init/sleep mode!\n",
					__FUNCTION__);
				return 1;
			}
		}
		init_done = 1;
	}
	return 0;
}

/*
 * return 1 on CAN failure
 * return 0 if no failure
 */
int do_can(char * const argv[])
{
	int i;
	struct mpc5xxx_mscan *can1 =
		(struct mpc5xxx_mscan *)(CONFIG_SYS_MBAR + 0x0900);
	struct mpc5xxx_mscan *can2 =
		(struct mpc5xxx_mscan *)(CONFIG_SYS_MBAR + 0x0980);

	/* send a message on CAN1 */
	can1->cantbsel = 0x01;
	can1->cantxfg.idr[0] = 0x55;
	can1->cantxfg.idr[1] = 0x00;
	can1->cantxfg.idr[1] &= ~0x8;
	can1->cantxfg.idr[1] &= ~0x10;
	can1->cantxfg.dsr[0] = 0xCC;
	can1->cantxfg.dlr = 1;
	can1->cantxfg.tbpr = 0;
	can1->cantflg = 0x01;

	i = 0;
	while ((can1->cantflg & 0x01) == 0) {
		i++;
		if (i == 10) {
			printf ("%s: CAN1 send timeout, "
				"can not send message!\n",
				__FUNCTION__);
			return 1;
		}
		udelay(1000);
	}
	udelay(1000);

	i = 0;
	while (!(can2->canrflg & 0x01))	{
		i++;
		if (i == 10) {
			printf ("%s: CAN2 receive timeout, "
				"no message received!\n",
				__FUNCTION__);
			return 1;
		}
		udelay(1000);
	}

	if (can2->canrxfg.dsr[0] != 0xCC) {
		printf ("%s: CAN2 receive error, "
			 "data mismatch!\n",
			__FUNCTION__);
		return 1;
	}

	/* send a message on CAN2 */
	can2->cantbsel = 0x01;
	can2->cantxfg.idr[0] = 0x55;
	can2->cantxfg.idr[1] = 0x00;
	can2->cantxfg.idr[1] &= ~0x8;
	can2->cantxfg.idr[1] &= ~0x10;
	can2->cantxfg.dsr[0] = 0xCC;
	can2->cantxfg.dlr = 1;
	can2->cantxfg.tbpr = 0;
	can2->cantflg = 0x01;

	i = 0;
	while ((can2->cantflg & 0x01) == 0) {
		i++;
		if (i == 10) {
			printf ("%s: CAN2 send error, "
				"can not send message!\n",
				__FUNCTION__);
			return 1;
		}
		udelay(1000);
	}
	udelay(1000);

	i = 0;
	while (!(can1->canrflg & 0x01))	{
		i++;
		if (i == 10) {
			printf ("%s: CAN1 receive timeout, "
				"no message received!\n",
				__FUNCTION__);
			return 1;
		}
		udelay(1000);
	}

	if (can1->canrxfg.dsr[0] != 0xCC) {
		printf ("%s: CAN1 receive error 0x%02x\n",
			__FUNCTION__, (can1->canrxfg.dsr[0]));
		return 1;
	}

	return 0;
}

/*
 * return 1 if rs232 port unknown
 * return 2 on txd/rxd failure (only rs232 2)
 * return 3 on rts/cts failure
 * return 0 if no failure
 */
int do_rs232(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;

	switch	(simple_strtoul(argv[2], NULL, 10)) {

	case 1:
		/* check RTS <-> CTS loop */
		/* set rts to 0 */
		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 PSC3_0, PSC3_2 as output and PSC3_1, PSC3_3 as input */
		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_2, 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_2, 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_2, 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_2, cts status is %d "
				"(should be 0)\n",
				__FUNCTION__,
				(gpio->simple_ival & 0x00000800) >> 11);
		}

		/* set PSC3_0, PSC3_1, PSC3_2 and PSC3_3 as output */
		gpio->simple_ddr &= ~(0x00000F00);
		gpio->simple_ddr |=   0x00000F00;
		break;

	default:
		printf ("%s: invalid rs232 number %s\n", __FUNCTION__, argv[2]);
		error_status = 1;
		break;
	}

	return error_status;
}

#if !defined(CONFIG_FO300) && !defined(CONFIG_TQM5200S)
static void sm501_backlight (unsigned int state)
{
	if (state == BL_ON) {
		*(vu_long *)(SM501_MMIO_BASE+SM501_PANEL_DISPLAY_CONTROL) |=
			(1 << 26) | (1 << 27);
	} else if (state == BL_OFF)
		*(vu_long *)(SM501_MMIO_BASE+SM501_PANEL_DISPLAY_CONTROL) &=
			~((1 << 26) | (1 << 27));
}
#endif /* !CONFIG_FO300 & !CONFIG_TQM5200S */

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

#ifdef CONFIG_STK52XX_REV100
	printf ("Revision 100 of STK52XX not supported!\n");
	return 1;
#endif
#if defined(CONFIG_STK52XX)
	led_init();
#endif
	can_init();

	switch (argc) {

	case 0:
	case 1:
		break;

	case 2:
		if (strncmp (argv[1], "can", 3) == 0) {
			rcode = do_can (argv);
			if (rcode == 0)
				printf ("OK\n");
			else
				printf ("Error\n");
			return rcode;
		}
		break;

	case 3:
		if (strncmp (argv[1], "rs232", 3) == 0) {
			rcode = do_rs232 (argv);
			if (rcode == 0)
				printf ("OK\n");
			else
				printf ("Error\n");
			return rcode;
#if !defined(CONFIG_FO300) && !defined(CONFIG_TQM5200S)
		} else if (strncmp (argv[1], "backlight", 4) == 0) {
			if (strncmp (argv[2], "on", 2) == 0) {
				sm501_backlight (BL_ON);
				return 0;
			}
			else if (strncmp (argv[2], "off", 3) == 0) {
				sm501_backlight (BL_OFF);
				return 0;
			}
#endif /* !CONFIG_FO300 & !CONFIG_TQM5200S */
		}
		break;

#if defined(CONFIG_STK52XX)
	case 4:
		if (strcmp (argv[1], "led") == 0) {
			return (do_led (argv));
		}
		break;
#endif

	default:
		break;
	}

	printf ("Usage:\nfkt cmd [arg1] [arg2] ...\n");
	return 1;
}


U_BOOT_CMD(
	sound ,    5,    1,     cmd_sound,
	"Sound sub-system",
	"saw [duration] [freq] [channel]\n"
	"    - generate sawtooth for 'duration' ms with frequency 'freq'\n"
	"      on left \"l\" or right \"r\" channel\n"
	"sound square [duration] [freq] [channel]\n"
	"    - generate squarewave for 'duration' ms with frequency 'freq'\n"
	"      on left \"l\" or right \"r\" channel\n"
	"pcm1772 reg val"
);

U_BOOT_CMD(
	wav ,    3,    1,     cmd_wav,
	"play wav file",
	"[addr] [bytes]\n"
	"    - play wav file at address 'addr' with length 'bytes'"
);

U_BOOT_CMD(
	beep ,    2,    1,     cmd_beep,
	"play short beep",
	"[channel]\n"
	"    - play short beep on \"l\"eft or \"r\"ight channel"
);
#endif /* CONFIG_STK52XX  || CONFIG_FO300 */

#if defined(CONFIG_STK52XX)
U_BOOT_CMD(
	fkt ,	4,	1,	cmd_fkt,
	"Function test routines",
	"led number on/off\n"
	"     - 'number's like printed on STK52XX board\n"
	"fkt can\n"
	"     - loopback plug for X83 required\n"
	"fkt rs232 number\n"
	"     - loopback plug(s) for X2 required"
#ifndef CONFIG_TQM5200S
	"\n"
	"fkt backlight on/off\n"
	"     - switch backlight on or off"
#endif /* !CONFIG_TQM5200S */
);
#elif defined(CONFIG_FO300)
U_BOOT_CMD(
	fkt ,	3,	1,	cmd_fkt,
	"Function test routines",
	"fkt can\n"
	"     - loopback plug for X16/X29 required\n"
	"fkt rs232 number\n"
	"     - loopback plug(s) for X21/X22 required"
);
#endif
#endif