aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/input/touchscreen/mms100_ISP_download.c
blob: ac9ef8c4f6871da70116a550a5d526470acf596a (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
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
/*
*--------------------------------------------------------
*
*
*	Melfas MMS100 Series Download base v1.7 2011.09.23
*
*/
#include <linux/kernel.h>

#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/gpio.h>
#include <linux/delay.h>
#include <linux/irq.h>
#include <linux/fcntl.h>
#include <asm/io.h>
#include <asm/gpio.h>
#include <asm/uaccess.h>
#include <linux/fs.h>

#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/init.h>
#include <linux/syscalls.h>

#include "mms100_ISP_download.h"
#include "MMS100_ISC_Initial.h"
/*
*============================================================
*
*	Include MELFAS Binary code File ( ex> MELFAS_FIRM_bin.c)
*
*	Warning!!!!
*		Please, don't add binary.c file into project
*		Just #include here !!
*
*============================================================
*/

#include "465_SMD_V61.c"
#include "M0_D2_C1_V74.c"
#define MELFAS_FW1 "/sdcard/midas_firmware.bin"

UINT8 ucVerifyBuffer[MELFAS_TRANSFER_LENGTH];	/*      You may melloc *ucVerifyBuffer instead of this */

/*
*---------------------------------
*	Downloading functions
*---------------------------------
*/
static int mcsdl_download(const UINT8 * pData, const UINT16 nLength,
			  INT8 IdxNum,
			  struct melfas_tsi_platform_data *ts_data);

static void mcsdl_set_ready(struct melfas_tsi_platform_data *ts_data);
static void mcsdl_reboot_mcs(struct melfas_tsi_platform_data *ts_data);

static int mcsdl_erase_flash(INT8 IdxNum);
static int mcsdl_program_flash(UINT8 * pDataOriginal, UINT16 unLength,
			       INT8 IdxNum);
static void mcsdl_program_flash_part(UINT8 * pData);

static int mcsdl_verify_flash(UINT8 * pData, UINT16 nLength, INT8 IdxNum);

static void mcsdl_read_flash(UINT8 * pBuffer);
static int mcsdl_read_flash_from(UINT8 * pBuffer, UINT16 unStart_addr,
				 UINT16 unLength, INT8 IdxNum);

static void mcsdl_select_isp_mode(UINT8 ucMode);
static void mcsdl_unselect_isp_mode(void);

static void mcsdl_read_32bits(UINT8 * pData);
static void mcsdl_write_bits(UINT32 wordData, int nBits);
static void mcsdl_scl_toggle_twice(void);

#if defined(CONFIG_MACH_M0_CHNOPEN) ||					\
	defined(CONFIG_MACH_M0_CMCC) || defined(CONFIG_MACH_M0_CTC)
extern unsigned int lcdtype;
#endif
/*
*---------------------------------
*	Delay functions
*---------------------------------
*/
void mcsdl_delay(UINT32 nCount);

/*
*---------------------------------
*	For debugging display
*---------------------------------
*/
#if MELFAS_ENABLE_DBG_PRINT
static void mcsdl_print_result(int nRet);
#endif

/*
*----------------------------------
* Download enable command
*----------------------------------
*/
#if MELFAS_USE_PROTOCOL_COMMAND_FOR_DOWNLOAD

void melfas_send_download_enable_command(void)
{
	/* TO DO : Fill this up */

}

#endif

/*
*============================================================
*
*	Main Download furnction
*
*   1. Run mcsdl_download( pBinary[IdxNum], nBinary_length[IdxNum], IdxNum);
*       IdxNum : 0 (Master Chip Download)
*       IdxNum : 1 (2Chip Download)
*
*
*============================================================
*/
int mms100_ISP_download_binary_data(int dl_mode,
				    struct melfas_tsi_platform_data *ts_data)
{
	int nRet = 0;
	int retry_cnt = 0;

#if 0
#if MELFAS_USE_PROTOCOL_COMMAND_FOR_DOWNLOAD
	melfas_send_download_enable_command();
	mcsdl_delay(MCSDL_DELAY_100US);
#endif

	MELFAS_DISABLE_BASEBAND_ISR();	/* Disable Baseband touch interrupt ISR. */
	MELFAS_DISABLE_WATCHDOG_TIMER_RESET();	/* Disable Baseband watchdog timer */

#endif

	/*
	 *------------------------
	 * Run Download
	 *------------------------
	 */

	for (retry_cnt = 0; retry_cnt < 5; retry_cnt++) {
		if (dl_mode == 0x01) {	/*MELFAS_ISP_DOWNLOAD */

#if defined(CONFIG_MACH_M0_CHNOPEN) || defined(CONFIG_MACH_M0_CTC)
			if (lcdtype == 0x20) {
				nRet =
				mcsdl_download((const UINT8*)MELFAS_binary_4_65,
				(const UINT16)MELFAS_binary_nLength_4_65,
				0, ts_data);

				printk(KERN_DEBUG "<MELFAS> lcdtype is 4.8. ISP firmware update with 4.65 firmware\n");
			} else {
				nRet =
				mcsdl_download((const UINT8*)MELFAS_binary,
				(const UINT16)MELFAS_binary_nLength,
				0, ts_data);

				printk(KERN_DEBUG "<MELFAS> lcdtype is 4.65. ISP firmware update with 4.8 firmware\n");
			}
#elif defined(CONFIG_MACH_M0_CMCC)
			if (lcdtype == 0x20) {
				nRet =
				mcsdl_download((const UINT8*)MELFAS_binary,
				(const UINT16)MELFAS_binary_nLength,
				0, ts_data);

				printk(KERN_DEBUG "<MELFAS> lcdtype is 4.8. ISP firmware update with 4.8 firmware\n");
			} else {
				nRet =
				mcsdl_download((const UINT8*)MELFAS_binary_4_65,
				(const UINT16)MELFAS_binary_nLength_4_65,
				0, ts_data);

				printk(KERN_DEBUG "<MELFAS> lcdtype is 4.65. ISP firmware update with 4.65 firmware\n");
			}
#else

#if defined(CONFIG_MACH_M0)
			if (system_rev == 2 || system_rev >= 5)
#else
			if (system_rev == 2 || system_rev >= 4)
#endif
				nRet =
				    mcsdl_download((const UINT8 *)MELFAS_binary,
						   (const UINT16)MELFAS_binary_nLength,
						   0, ts_data);
			else
				nRet =
				    mcsdl_download((const UINT8 *)MELFAS_binary_4_65,
						   (const UINT16)MELFAS_binary_nLength_4_65,
						   0, ts_data);
#endif
		}
		else {		/*MELFAS_ISC_DOWNLOAD */

			nRet = mcsdl_download((const UINT8 *)
					      MELFAS_MMS100_Initial_binary,
					      (const UINT16)
					      MELFAS_MMS100_Initial_nLength, 0,
					      ts_data);
		}
#if MELFAS_2CHIP_DOWNLOAD_ENABLE
		if (!nRet) {
			if (dl_mode == 0x01)	/*MELFAS_ISP_DOWNLOAD */
				nRet = mcsdl_download((const UINT8 *)MELFAS_binary, (const UINT16)MELFAS_binary_nLength, 1, ts_data);	/* Slave Binary data download */
			else	/*MELFAS_ISC_DOWNLOAD */
				nRet = mcsdl_download((const UINT8 *)
						      MELFAS_MMS100_Initial_binary, (const UINT16)
						      MELFAS_MMS100_Initial_nLength, 1, ts_data);
		}
#endif
		if (!nRet)
			break;
	}

	MELFAS_ROLLBACK_BASEBAND_ISR();	/* Roll-back Baseband touch interrupt ISR. */
	MELFAS_ROLLBACK_WATCHDOG_TIMER_RESET();	/* Roll-back Baseband watchdog timer */

 fw_error:
	if (nRet) {
		mcsdl_erase_flash(0);
		mcsdl_erase_flash(1);
	}
	return nRet;
}

int mms100_ISP_download_binary_file(struct melfas_tsi_platform_data *ts_data)
{
	int i;

	UINT8 *pBinary[2] = { NULL, NULL };
	UINT16 nBinary_length[2] = { 0, 0 };
	UINT8 IdxNum = MELFAS_2CHIP_DOWNLOAD_ENABLE;
	/*
	 *==================================================
	 *
	 *      1. Read '.bin file'
	 *   2. *pBinary[0]       : Binary data(Core + Private Custom)
	 *       *pBinary[1]       : Binary data(Public Custom)
	 *         nBinary_length[0] : Firmware size(Core + Private Custom)
	 *         nBinary_length[1] : Firmware size(Public Custom)
	 *      3. Run mcsdl_download( pBinary[IdxNum], nBinary_length[IdxNum], IdxNum);
	 *       IdxNum : 0 (Master Chip Download)
	 *       IdxNum : 1 (2Chip Download)
	 *
	 *==================================================
	 */
#if 1

	int nRet = 0;
	/*int retry_cnt = 0; */
	long fw1_size = 0;
	unsigned char *fw_data1;
	struct file *filp;
	loff_t pos;
	int ret = 0;
	mm_segment_t oldfs;
	/*spinlock_t    lock; */

	oldfs = get_fs();
	set_fs(get_ds());

	filp = filp_open(MELFAS_FW1, O_RDONLY, 0);
	if (IS_ERR(filp)) {
		pr_err("[TSP]file open error:%d\n", (s32) filp);
		return -1;
	}

	fw1_size = filp->f_path.dentry->d_inode->i_size;
	pr_info("[TSP]Size of the file : %ld(bytes)\n", fw1_size);

	fw_data1 = kmalloc(fw1_size, GFP_KERNEL);
	memset(fw_data1, 0, fw1_size);

	pos = 0;
	memset(fw_data1, 0, fw1_size);
	ret = vfs_read(filp, (char __user *)fw_data1, fw1_size, &pos);

	if (ret != fw1_size) {
		pr_err("[TSP]Failed to read file %s (ret = %d)\n", MELFAS_FW1,
		       ret);
		kfree(fw_data1);
		filp_close(filp, current->files);
		return -1;
	}

	filp_close(filp, current->files);

	set_fs(oldfs);

#endif

#if MELFAS_USE_PROTOCOL_COMMAND_FOR_DOWNLOAD
	melfas_send_download_enable_command();
	mcsdl_delay(MCSDL_DELAY_100US);
#endif

	MELFAS_DISABLE_BASEBAND_ISR();	/* Disable Baseband touch interrupt ISR. */
	MELFAS_DISABLE_WATCHDOG_TIMER_RESET();	/* Disable Baseband watchdog timer */

	for (i = 0; i <= IdxNum; i++) {
		if (pBinary[0] != NULL && nBinary_length[0] > 0
		    && nBinary_length[0] < MELFAS_FIRMWARE_MAX_SIZE) {
			/*
			 *------------------------
			 * Run Download
			 *------------------------
			 */
			nRet =
			    mcsdl_download((const UINT8 *)pBinary[0],
					   (const UINT16)nBinary_length[0], i,
					   ts_data);
		} else {
			nRet = MCSDL_RET_WRONG_BINARY;
		}
	}

	MELFAS_ROLLBACK_BASEBAND_ISR();	/* Roll-back Baseband touch interrupt ISR. */
	MELFAS_ROLLBACK_WATCHDOG_TIMER_RESET();	/* Roll-back Baseband watchdog timer */

#if MELFAS_ENABLE_DBG_PRINT
	mcsdl_print_result(nRet);
#endif
	kfree(fw_data1);

	return (nRet == MCSDL_RET_SUCCESS);

}

/*
*------------------------------------------------------------------
*
*	Download function
*
*------------------------------------------------------------------
*/
static int mcsdl_download(const UINT8 * pBianry, const UINT16 unLength,
			  INT8 IdxNum, struct melfas_tsi_platform_data *ts_data)
{
	int nRet;
	/*
	 *---------------------------------
	 * Check Binary Size
	 *---------------------------------
	 */
	if (unLength > MELFAS_FIRMWARE_MAX_SIZE) {

		nRet = MCSDL_RET_PROGRAM_SIZE_IS_WRONG;
		goto MCSDL_DOWNLOAD_FINISH;
	}
#if MELFAS_ENABLE_DBG_PROGRESS_PRINT
	printk("<melfas> - Starting download...\n");
#endif

	/*
	 *---------------------------------
	 * Make it ready
	 *---------------------------------
	 */
#if MELFAS_ENABLE_DBG_PROGRESS_PRINT
	printk("<melfas> > Ready\n");
#endif

	mcsdl_set_ready(ts_data);

	/*
	 *---------------------------------
	 * Erase Flash
	 *---------------------------------
	 */
#if MELFAS_ENABLE_DBG_PROGRESS_PRINT
	printk("<melfas> > Erase\n");
#endif

	nRet = mcsdl_erase_flash(IdxNum);

	if (nRet != MCSDL_RET_SUCCESS)
		goto MCSDL_DOWNLOAD_FINISH;
	/*
	 *---------------------------------
	 * Program Flash
	 *---------------------------------
	 */
#if MELFAS_ENABLE_DBG_PROGRESS_PRINT
	printk("<melfas> > Program   ");
#endif

/*	if(IdxNum > 0)
		mcsdl_set_ready();
*/

	nRet =
	    mcsdl_program_flash((UINT8 *) pBianry, (UINT16) unLength, IdxNum);
	if (nRet != MCSDL_RET_SUCCESS)
		goto MCSDL_DOWNLOAD_FINISH;
	/*
	 *---------------------------------
	 * Verify flash
	 *---------------------------------
	 */
#if MELFAS_ENABLE_DBG_PROGRESS_PRINT
	printk("<melfas> > Verify    ");
#endif
	nRet = mcsdl_verify_flash((UINT8 *) pBianry, (UINT16) unLength, IdxNum);
	if (nRet != MCSDL_RET_SUCCESS)
		goto MCSDL_DOWNLOAD_FINISH;

	nRet = MCSDL_RET_SUCCESS;

 MCSDL_DOWNLOAD_FINISH:

#if MELFAS_ENABLE_DBG_PRINT
	mcsdl_print_result(nRet);	/* Show result */
#endif

#if MELFAS_ENABLE_DBG_PROGRESS_PRINT
	printk("<melfas> > Rebooting\n");
	printk("<melfas> - Fin.\n\n");
#endif

	mcsdl_reboot_mcs(ts_data);

	return nRet;
}

/*
*------------------------------------------------------------------
*
*	Sub functions
*
*------------------------------------------------------------------
*/

static int mcsdl_erase_flash(INT8 IdxNum)
{
	int i;
	UINT8 readBuffer[32];
	int eraseCompareValue = 0xFF;
	/*
	 *----------------------------------------
	 *      Do erase
	 *----------------------------------------
	 */
	if (IdxNum > 0) {
		mcsdl_select_isp_mode(ISP_MODE_NEXT_CHIP_BYPASS);
		mcsdl_delay(MCSDL_DELAY_3US);
	}
	mcsdl_select_isp_mode(ISP_MODE_ERASE_FLASH);
	mcsdl_unselect_isp_mode();

	/*
	 *----------------------------------------
	 *      Check 'erased well'
	 *----------------------------------------
	 */
/*start ADD DELAY*/
	mcsdl_read_flash_from(readBuffer, 0x0000, 16, IdxNum);
	mcsdl_read_flash_from(&readBuffer[16], 0x7FF0, 16, IdxNum);
	/*end ADD DELAY */
	if (IdxNum > 0) {
		eraseCompareValue = 0x00;
	}
	/* Compare with '0xFF' */
	for (i = 0; i < 32; i++) {
		if (readBuffer[i] != eraseCompareValue)
			return MCSDL_RET_ERASE_FLASH_VERIFY_FAILED;
	}

	return MCSDL_RET_SUCCESS;
}

static int mcsdl_program_flash(UINT8 * pDataOriginal, UINT16 unLength,
			       INT8 IdxNum)
{
	int i;

	UINT8 *pData;
	UINT8 ucLength;

	UINT16 addr;
	UINT32 header;

	addr = 0;
	pData = pDataOriginal;

	ucLength = MELFAS_TRANSFER_LENGTH;

/*kang*/

	while ((addr * 4) < (int)unLength) {
		if ((unLength - (addr * 4)) < MELFAS_TRANSFER_LENGTH) {
			ucLength = (UINT8) (unLength - (addr * 4));
		}
		/*
		 *--------------------------------------
		 *      Select ISP Mode
		 *--------------------------------------
		 */

		/* start ADD DELAY */
		mcsdl_delay(MCSDL_DELAY_40US);
		/*end ADD DELAY */
		if (IdxNum > 0) {
			mcsdl_select_isp_mode(ISP_MODE_NEXT_CHIP_BYPASS);
			mcsdl_delay(MCSDL_DELAY_3US);

		}
		mcsdl_select_isp_mode(ISP_MODE_SERIAL_WRITE);
		/*
		 *---------------------------------------------
		 *      Header
		 *      Address[13ibts] <<
		 *---------------------------------------------
		 */
		header = ((addr & 0x1FFF) << 1) | 0x0;
		header = header << 14;

		/*Write 18bits */
		mcsdl_write_bits(header, 18);
		/*start ADD DELAY */
		/*end ADD DELAY */

		/*
		 *---------------------------------
		 *      Writing
		 *---------------------------------
		 */
		/*          addr += (UINT16)ucLength; */
		addr += 1;

#if MELFAS_ENABLE_DBG_PROGRESS_PRINT
		printk("#");
#endif

		mcsdl_program_flash_part(pData);

		pData += ucLength;

		/*
		 *---------------------------------------------
		 *      Tail
		 *---------------------------------------------
		 */
		MCSDL_GPIO_SDA_SET_HIGH();
/*kang*/
		mcsdl_delay(MCSDL_DELAY_40US);

		for (i = 0; i < 6; i++) {
			if (i == 2) {
				mcsdl_delay(MCSDL_DELAY_20US);
			} else if (i == 3) {
				mcsdl_delay(MCSDL_DELAY_40US);
			}

			MCSDL_GPIO_SCL_SET_HIGH();
			mcsdl_delay(MCSDL_DELAY_10US);
			MCSDL_GPIO_SCL_SET_LOW();
			mcsdl_delay(MCSDL_DELAY_10US);
		}
		MCSDL_GPIO_SDA_SET_LOW();

		mcsdl_unselect_isp_mode();
/*start ADD DELAY*/
		mcsdl_delay(MCSDL_DELAY_300US);
/*end ADD DELAY*/

	}

	return MCSDL_RET_SUCCESS;
}

static void mcsdl_program_flash_part(UINT8 * pData)
{
	/*int     i; */
	UINT32 data;

	/*
	 *---------------------------------
	 *      Body
	 *---------------------------------
	 */

	data = (UINT32) pData[0] << 0;
	data |= (UINT32) pData[1] << 8;
	data |= (UINT32) pData[2] << 16;
	data |= (UINT32) pData[3] << 24;
	mcsdl_write_bits(data, 32);

}

static int mcsdl_verify_flash(UINT8 * pDataOriginal, UINT16 unLength,
			      INT8 IdxNum)
{
	int i, j;
	int nRet;

	UINT8 *pData;
	UINT8 ucLength;

	UINT16 addr;
	UINT32 wordData;

	addr = 0;
	pData = (UINT8 *) pDataOriginal;

	ucLength = MELFAS_TRANSFER_LENGTH;

	while ((addr * 4) < (int)unLength) {
		if ((unLength - (addr * 4)) < MELFAS_TRANSFER_LENGTH) {
			ucLength = (UINT8) (unLength - (addr * 4));
		}
		/* start ADD DELAY */
		mcsdl_delay(MCSDL_DELAY_40US);

		/*
		 *--------------------------------------
		 *      Select ISP Mode
		 *--------------------------------------
		 */
		if (IdxNum > 0) {
			mcsdl_select_isp_mode(ISP_MODE_NEXT_CHIP_BYPASS);
			mcsdl_delay(MCSDL_DELAY_3US);
		}

		mcsdl_select_isp_mode(ISP_MODE_SERIAL_READ);
		/*
		 *---------------------------------------------
		 *      Header
		 *      Address[13ibts] <<1
		 *---------------------------------------------
		 */

		wordData = ((addr & 0x1FFF) << 1) | 0x0;
		wordData <<= 14;

		mcsdl_write_bits(wordData, 18);

		addr += 1;

#if MELFAS_ENABLE_DBG_PROGRESS_PRINT
		printk("#");
#endif

		/*
		 *--------------------
		 * Read flash
		 *--------------------
		 */
		mcsdl_read_flash(ucVerifyBuffer);

		MCSDL_GPIO_SDA_SET_LOW();
		MCSDL_GPIO_SDA_SET_OUTPUT(0);

		/*
		 *--------------------
		 * Comparing
		 *--------------------
		 */

		if (IdxNum == 0) {
			for (j = 0; j < (int)ucLength; j++) {
#if MELFAS_ENABLE_DBG_PROGRESS_PRINT
				printk(" %02X", ucVerifyBuffer[j]);
#endif
				if (ucVerifyBuffer[j] != pData[j]) {
#if MELFAS_ENABLE_DBG_PROGRESS_PRINT
					printk
					    ("\n [Error] Address : 0x%04X : 0x%02X - 0x%02X\n",
					     addr, pData[j], ucVerifyBuffer[j]);
#endif

					nRet = MCSDL_RET_PROGRAM_VERIFY_FAILED;
					goto MCSDL_VERIFY_FLASH_FINISH;

				}
			}
		} else {	/* slave */

			for (j = 0; j < (int)ucLength; j++) {
#if MELFAS_ENABLE_DBG_PROGRESS_PRINT
				printk(" %02X", ucVerifyBuffer[j]);
#endif
				if ((0xff - ucVerifyBuffer[j]) != pData[j]) {
#if MELFAS_ENABLE_DBG_PROGRESS_PRINT
					printk
					    ("\n [Error] Address : 0x%04X : 0x%02X - 0x%02X\n",
					     addr, pData[j], ucVerifyBuffer[j]);
#endif

					nRet = MCSDL_RET_PROGRAM_VERIFY_FAILED;
					goto MCSDL_VERIFY_FLASH_FINISH;

				}
			}
		}
		pData += ucLength;
		mcsdl_unselect_isp_mode();
	}

	nRet = MCSDL_RET_SUCCESS;

 MCSDL_VERIFY_FLASH_FINISH:

#if MELFAS_ENABLE_DBG_PROGRESS_PRINT
	printk("\n");
#endif

	mcsdl_unselect_isp_mode();

	return nRet;
}

static void mcsdl_read_flash(UINT8 * pBuffer)
{
	int i;

	MCSDL_GPIO_SCL_SET_LOW();
	MCSDL_GPIO_SDA_SET_LOW();

	mcsdl_delay(MCSDL_DELAY_40US);

	for (i = 0; i < 6; i++) {
		MCSDL_GPIO_SCL_SET_HIGH();
		mcsdl_delay(MCSDL_DELAY_10US);
		MCSDL_GPIO_SCL_SET_LOW();
		mcsdl_delay(MCSDL_DELAY_10US);
	}

	mcsdl_read_32bits(pBuffer);
}

static int mcsdl_read_flash_from(UINT8 * pBuffer, UINT16 unStart_addr,
				 UINT16 unLength, INT8 IdxNum)
{
	int i;
	int j;

	UINT8 ucLength;

	UINT16 addr;
	UINT32 wordData;

	if (unLength >= MELFAS_FIRMWARE_MAX_SIZE) {
		return MCSDL_RET_PROGRAM_SIZE_IS_WRONG;
	}

	addr = 0;
	ucLength = MELFAS_TRANSFER_LENGTH;

#if MELFAS_ENABLE_DBG_PROGRESS_PRINT
	printk(" %04X : ", unStart_addr);
#endif

	for (i = 0; i < (int)unLength; i += (int)ucLength) {

		addr = (UINT16) i;
		if (IdxNum > 0) {
			mcsdl_select_isp_mode(ISP_MODE_NEXT_CHIP_BYPASS);
			mcsdl_delay(MCSDL_DELAY_3US);
		}

		mcsdl_select_isp_mode(ISP_MODE_SERIAL_READ);
		wordData = (((unStart_addr + addr) & 0x1FFF) << 1) | 0x0;
		wordData <<= 14;

		mcsdl_write_bits(wordData, 18);

		if ((unLength - addr) < MELFAS_TRANSFER_LENGTH) {
			ucLength = (UINT8) (unLength - addr);
		}
		/*
		 *--------------------
		 * Read flash
		 *--------------------
		 */
		mcsdl_read_flash(&pBuffer[addr]);

#if MELFAS_ENABLE_DBG_PROGRESS_PRINT
		for (j = 0; j < (int)ucLength; j++) {
			printk("%02X ", pBuffer[j]);
		}
#endif

		mcsdl_unselect_isp_mode();

	}

#if MELFAS_ENABLE_DBG_PROGRESS_PRINT
	/*printk("\n"); */
#endif

	return MCSDL_RET_SUCCESS;

}

static void mcsdl_set_ready(struct melfas_tsi_platform_data *ts_data)
{
	/*
	 *--------------------------------------------
	 * Tkey module reset
	 *--------------------------------------------
	 */

	ts_data->power(0);	/*MCSDL_VDD_SET_LOW(); * power */
	/*MCSDL_CE_SET_LOW(); */
	/*MCSDL_CE_SET_OUTPUT(); */

	MCSDL_SET_GPIO_I2C();

	MCSDL_GPIO_SDA_SET_LOW();
	MCSDL_GPIO_SDA_SET_OUTPUT(0);

	MCSDL_GPIO_SCL_SET_LOW();
	MCSDL_GPIO_SCL_SET_OUTPUT(1);

	MCSDL_RESETB_SET_LOW();
	MCSDL_RESETB_SET_OUTPUT(0);

	/*    mcsdl_delay(MCSDL_DELAY_25MS);                        * Delay for Stable VDD */
	msleep(500);
	ts_data->power(1);	/*MCSDL_VDD_SET_HIGH(); */
	/*MCSDL_CE_SET_HIGH(); */

	MCSDL_GPIO_SCL_SET_LOW();
	MCSDL_GPIO_SDA_SET_HIGH();
	/*mcsdl_delay(MCSDL_DELAY_40MS); */
	msleep(500);
	/* Delay '30 msec' */

}

static void mcsdl_reboot_mcs(struct melfas_tsi_platform_data *ts_data)
{
	ts_data->power(0);	/*MCSDL_VDD_SET_LOW(); */
	/*MCSDL_CE_SET_LOW(); */
	/*MCSDL_CE_SET_OUTPUT(); */

	MCSDL_GPIO_SDA_SET_HIGH();
	MCSDL_GPIO_SDA_SET_OUTPUT(1);

	MCSDL_GPIO_SCL_SET_HIGH();
	MCSDL_GPIO_SCL_SET_OUTPUT(1);

	MCSDL_RESETB_SET_LOW();
	MCSDL_RESETB_SET_OUTPUT(0);

	mcsdl_delay(MCSDL_DELAY_25MS);	/* Delay for Stable VDD */

	ts_data->power(1);	/*MCSDL_VDD_SET_HIGH(); */
	/*MCSDL_CE_SET_HIGH(); */

	MCSDL_RESETB_SET_HIGH();
	MCSDL_RESETB_SET_INPUT();
	MCSDL_GPIO_SCL_SET_INPUT();
	MCSDL_GPIO_SDA_SET_INPUT();

	mcsdl_delay(MCSDL_DELAY_30MS);	/* Delay '25 msec' */
}

/*
*--------------------------------------------
*
*   Write ISP Mode entering signal
*
*--------------------------------------------
*/
static void mcsdl_select_isp_mode(UINT8 ucMode)
{
	int i;
	UINT8 *pCode;

	UINT8 enteringCodeMassErase[16] =
	    { 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1 };
	UINT8 enteringCodeSerialWrite[16] =
	    { 0, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1 };
	UINT8 enteringCodeSerialRead[16] =
	    { 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1 };
	UINT8 enteringCodeNextChipBypass[16] =
	    { 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1 };

	pCode = enteringCodeMassErase;

	/*
	 *------------------------------------
	 * Entering ISP mode : Part 1
	 *------------------------------------
	 */

	if (ucMode == ISP_MODE_ERASE_FLASH)
		pCode = enteringCodeMassErase;
	else if (ucMode == ISP_MODE_SERIAL_WRITE)
		pCode = enteringCodeSerialWrite;
	else if (ucMode == ISP_MODE_SERIAL_READ)
		pCode = enteringCodeSerialRead;
	else if (ucMode == ISP_MODE_NEXT_CHIP_BYPASS)
		pCode = enteringCodeNextChipBypass;

	MCSDL_RESETB_SET_LOW();
	MCSDL_GPIO_SCL_SET_LOW();
	MCSDL_GPIO_SDA_SET_HIGH();
	for (i = 0; i < 16; i++) {
		if (pCode[i] == 1)
			MCSDL_RESETB_SET_HIGH();
		else
			MCSDL_RESETB_SET_LOW();

/*start add delay for INT*/
		mcsdl_delay(MCSDL_DELAY_3US);
/*end delay for INT*/

		MCSDL_GPIO_SCL_SET_HIGH();
		mcsdl_delay(MCSDL_DELAY_3US);
		MCSDL_GPIO_SCL_SET_LOW();
		mcsdl_delay(MCSDL_DELAY_3US);

	}

	MCSDL_RESETB_SET_LOW();

	/*
	 *---------------------------------------------------
	 * Entering ISP mode : Part 2   - Only Mass Erase
	 *---------------------------------------------------
	 */
	mcsdl_delay(MCSDL_DELAY_7US);

	MCSDL_GPIO_SCL_SET_LOW();
	MCSDL_GPIO_SDA_SET_HIGH();
	if (ucMode == ISP_MODE_ERASE_FLASH) {
		mcsdl_delay(MCSDL_DELAY_7US);
		for (i = 0; i < 4; i++) {

			if (i == 2)
				mcsdl_delay(MCSDL_DELAY_25MS);
			else if (i == 3)
				mcsdl_delay(MCSDL_DELAY_150US);

			MCSDL_GPIO_SCL_SET_HIGH();
			mcsdl_delay(MCSDL_DELAY_3US);
			MCSDL_GPIO_SCL_SET_LOW();
			mcsdl_delay(MCSDL_DELAY_7US);
		}
	}
	MCSDL_GPIO_SDA_SET_LOW();
}

static void mcsdl_unselect_isp_mode(void)
{
	int i;

	/* MCSDL_GPIO_SDA_SET_HIGH(); */
	/* MCSDL_GPIO_SDA_SET_OUTPUT(1); */

	MCSDL_RESETB_SET_LOW();

	mcsdl_delay(MCSDL_DELAY_3US);

	for (i = 0; i < 10; i++) {

		MCSDL_GPIO_SCL_SET_HIGH();
		mcsdl_delay(MCSDL_DELAY_3US);
		MCSDL_GPIO_SCL_SET_LOW();
		mcsdl_delay(MCSDL_DELAY_3US);
	}

}

static void mcsdl_read_32bits(UINT8 * pData)
{
	int i, j;
	MCSDL_GPIO_SCL_SET_LOW();
	MCSDL_GPIO_SDA_SET_LOW();
	MCSDL_GPIO_SDA_SET_INPUT();

	for (i = 3; i >= 0; i--) {

		pData[i] = 0;

		for (j = 0; j < 8; j++) {

			pData[i] <<= 1;

			MCSDL_GPIO_SCL_SET_LOW();
			mcsdl_delay(MCSDL_DELAY_1US);
			MCSDL_GPIO_SCL_SET_HIGH();
			mcsdl_delay(MCSDL_DELAY_1US);

			if (MCSDL_GPIO_SDA_IS_HIGH())
				pData[i] |= 0x01;
		}
	}

	MCSDL_GPIO_SDA_SET_LOW();
	MCSDL_GPIO_SDA_SET_OUTPUT(0);
}

static void mcsdl_write_bits(UINT32 wordData, int nBits)
{
	int i;

	MCSDL_GPIO_SCL_SET_LOW();
	MCSDL_GPIO_SDA_SET_LOW();

	for (i = 0; i < nBits; i++) {
		if (wordData & 0x80000000) {
			MCSDL_GPIO_SDA_SET_HIGH();
		} else {
			MCSDL_GPIO_SDA_SET_LOW();
		}

		mcsdl_delay(MCSDL_DELAY_3US);

		MCSDL_GPIO_SCL_SET_HIGH();
		mcsdl_delay(MCSDL_DELAY_3US);
		MCSDL_GPIO_SCL_SET_LOW();
		mcsdl_delay(MCSDL_DELAY_3US);

		wordData <<= 1;
	}
}

static void mcsdl_scl_toggle_twice(void)
{

	MCSDL_GPIO_SDA_SET_HIGH();
	MCSDL_GPIO_SDA_SET_OUTPUT(1);

	MCSDL_GPIO_SCL_SET_HIGH();
	mcsdl_delay(MCSDL_DELAY_20US);
	MCSDL_GPIO_SCL_SET_LOW();
	mcsdl_delay(MCSDL_DELAY_20US);

	MCSDL_GPIO_SCL_SET_HIGH();
	mcsdl_delay(MCSDL_DELAY_20US);
	MCSDL_GPIO_SCL_SET_LOW();
	mcsdl_delay(MCSDL_DELAY_20US);
}

/*
*============================================================
*
*	Delay Function
*
*============================================================
*/
void mcsdl_delay(UINT32 nCount)
{

	switch (nCount) {
	case MCSDL_DELAY_1US:{
			udelay(1);
			break;
		}
/*    case MCSDL_DELAY_2US   : {udelay(2);   break;}*/
	case MCSDL_DELAY_3US:{
			udelay(3);
			break;
		}
/*  case MCSDL_DELAY_5US   : {udelay(5);   break;}*/
	case MCSDL_DELAY_7US:{
			udelay(7);
			break;
		}
	case MCSDL_DELAY_10US:{
			udelay(10);
			break;
		}
/*    case MCSDL_DELAY_15US  : {udelay(15);  break;}*/
	case MCSDL_DELAY_20US:{
			udelay(20);
			break;
		}
	case MCSDL_DELAY_40US:{
			udelay(40);
			break;
		}
	case MCSDL_DELAY_100US:{
			udelay(100);
			break;
		}
	case MCSDL_DELAY_150US:{
			udelay(150);
			break;
		}
	case MCSDL_DELAY_300US:{
			udelay(300);
			break;
		}
/*    case MCSDL_DELAY_500US : {udelay(500); break;}*/
/*   case MCSDL_DELAY_800US : {udelay(800); break;}*/
/*    case MCSDL_DELAY_1MS   : {mdelay(1);   break;}*/
/*    case MCSDL_DELAY_5MS   : {mdelay(5);   break;}*/
/*    case MCSDL_DELAY_10MS  : {mdelay(10);  break;}*/
	case MCSDL_DELAY_25MS:{
			mdelay(25);
			break;
		}
	case MCSDL_DELAY_30MS:{
			mdelay(30);
			break;
		}
	case MCSDL_DELAY_40MS:{
			mdelay(40);
			break;
		}
/*    case MCSDL_DELAY_45MS  : {mdelay(45);  break;}*/
	case MCSDL_DELAY_60MS:{
			mdelay(60);
			break;
		}
	case MCSDL_DELAY_100MS:
		{
			mdelay(100);
			break;
		}
	case MCSDL_DELAY_500MS:{
			mdelay(60);
			break;
		}
	default:{
			break;
		}
	}			/* Please, Use your delay function */
}

/*
*============================================================
*
*	Debugging print functions.
*
*============================================================
*/

#ifdef MELFAS_ENABLE_DBG_PRINT

static void mcsdl_print_result(int nRet)
{
	if (nRet == MCSDL_RET_SUCCESS) {
		printk(" > MELFAS Firmware downloading SUCCESS.\n");
	} else {
		printk(" > MELFAS Firmware downloading FAILED  :  ");
		switch (nRet) {
		case MCSDL_RET_SUCCESS:
			printk("MCSDL_RET_SUCCESS\n");
			break;
		case MCSDL_RET_ERASE_FLASH_VERIFY_FAILED:
			printk("MCSDL_RET_ERASE_FLASH_VERIFY_FAILED\n");
			break;
		case MCSDL_RET_PROGRAM_VERIFY_FAILED:
			printk("MCSDL_RET_PROGRAM_VERIFY_FAILED\n");
			break;

		case MCSDL_RET_PROGRAM_SIZE_IS_WRONG:
			printk("MCSDL_RET_PROGRAM_SIZE_IS_WRONG\n");
			break;
		case MCSDL_RET_VERIFY_SIZE_IS_WRONG:
			printk("MCSDL_RET_VERIFY_SIZE_IS_WRONG\n");
			break;
		case MCSDL_RET_WRONG_BINARY:
			printk("MCSDL_RET_WRONG_BINARY\n");
			break;

		case MCSDL_RET_READING_HEXFILE_FAILED:
			printk("MCSDL_RET_READING_HEXFILE_FAILED\n");
			break;
		case MCSDL_RET_FILE_ACCESS_FAILED:
			printk("MCSDL_RET_FILE_ACCESS_FAILED\n");
			break;
		case MCSDL_RET_MELLOC_FAILED:
			printk("MCSDL_RET_MELLOC_FAILED\n");
			break;

		case MCSDL_RET_WRONG_MODULE_REVISION:
			printk("MCSDL_RET_WRONG_MODULE_REVISION\n");
			break;

		default:
			printk("UNKNOWN ERROR. [0x%02X].\n", nRet);
			break;
		}

	}

}

#endif

#if MELFAS_ENABLE_DELAY_TEST
/*
*============================================================
*
*	For initial testing of delay and gpio control
*
*	You can confirm GPIO control and delay time by calling this function.
*
*============================================================
*/

void mcsdl_delay_test(INT32 nCount)
{
	INT16 i;

	MELFAS_DISABLE_BASEBAND_ISR();	/* Disable Baseband touch interrupt ISR. */
	MELFAS_DISABLE_WATCHDOG_TIMER_RESET();	/* Disable Baseband watchdog timer */

	/*
	 *--------------------------------
	 *      Repeating 'nCount' times
	 *--------------------------------
	 */

	MCSDL_SET_GPIO_I2C();
	MCSDL_GPIO_SCL_SET_OUTPUT(0);
	MCSDL_GPIO_SDA_SET_OUTPUT(0);
	MCSDL_RESETB_SET_OUTPUT(0);

	MCSDL_GPIO_SCL_SET_HIGH();

	for (i = 0; i < nCount; i++) {

#if 1

		MCSDL_GPIO_SCL_SET_LOW();

		mcsdl_delay(MCSDL_DELAY_20US);

		MCSDL_GPIO_SCL_SET_HIGH();

		mcsdl_delay(MCSDL_DELAY_100US);

#elif 0

		MCSDL_GPIO_SCL_SET_LOW();

		mcsdl_delay(MCSDL_DELAY_500US);

		MCSDL_GPIO_SCL_SET_HIGH();

		mcsdl_delay(MCSDL_DELAY_1MS);

#else

		MCSDL_GPIO_SCL_SET_LOW();

		mcsdl_delay(MCSDL_DELAY_25MS);

		TKEY_INTR_SET_LOW();

		mcsdl_delay(MCSDL_DELAY_45MS);

		TKEY_INTR_SET_HIGH();
#endif
	}

	MCSDL_GPIO_SCL_SET_HIGH();

	MELFAS_ROLLBACK_BASEBAND_ISR();	/* Roll-back Baseband touch interrupt ISR. */
	MELFAS_ROLLBACK_WATCHDOG_TIMER_RESET();	/* Roll-back Baseband watchdog timer */
}

#endif