upload tizen1.0 source
[kernel/linux-2.6.36.git] / drivers / input / keyboard / melfas_download.c
1 //------------------------------------------------------------------
2 //
3 //      MELFAS Firmware download base code v6 For MCS5080 2008/11/04
4 //
5 //------------------------------------------------------------------
6 #include "melfas_download.h"
7
8 //============================================================
9 //
10 //      Static variables & functions
11 //
12 //============================================================
13
14 //---------------------------------
15 //      Downloading functions
16 //---------------------------------
17 static int  mcsdl_download(const UINT8 *pData, const UINT16 nLength);
18 static int  mcsdl_enter_download_mode(void);
19 static void mcsdl_write_download_mode_signal(void);
20 static int  mcsdl_i2c_erase_flash(void);
21 static int  mcsdl_i2c_prepare_erase_flash(void);
22 static int  mcsdl_i2c_read_flash( UINT8 *pBuffer, UINT16 nAddr_start, UINT8 cLength);
23 static int  mcsdl_i2c_prepare_program(void);
24 static int  mcsdl_i2c_program_flash( UINT8 *pData, UINT16 nAddr_start, UINT8 cLength );
25
26 //-----------------------------------------
27 //      Download enable command on Protocol
28 //-----------------------------------------
29 #if MELFAS_USE_PROTOCOL_COMMAND_FOR_DOWNLOAD
30 void melfas_send_download_enable_command(void);
31 #endif
32
33 //---------------------------------
34 //      I2C Functions
35 //---------------------------------
36 BOOLEAN _i2c_read_( UINT8 slave_addr, UINT8 *pData, UINT8 cLength);
37 BOOLEAN _i2c_write_(UINT8 slave_addr, UINT8 *pData, UINT8 cLength);
38
39 //---------------------------------
40 //      Delay functions
41 //---------------------------------
42 static void mcsdl_delay(UINT32 nCount);
43
44 //---------------------------------
45 //      For debugging display
46 //---------------------------------
47 #if MELFAS_ENABLE_DBG_PRINT
48 static void mcsdl_print_result(int nRet);
49 #endif
50
51
52 //----------------------------------
53 // Download enable command
54 //----------------------------------
55 #if MELFAS_USE_PROTOCOL_COMMAND_FOR_DOWNLOAD
56
57 void melfas_send_download_enable_command(void)
58 {
59         // TO DO : Fill this up
60
61 }
62
63 #endif
64
65 //---------------------------------
66 //      Additional necessary things
67 //---------------------------------
68 #define uart_printf printk
69
70 struct i2c_client *mcs_c;
71
72 int mcsdl_set_i2c_client(struct i2c_client *c)
73 {
74         if (c) {
75                 mcs_c = c;
76                 return 0;
77         } else
78                 return -EINVAL;
79 }
80
81 static int mcs_i2c_fw_write(u8 slave_addr, u8 *val)
82 {
83         int ret = 0;
84
85         mcs_c->addr = slave_addr;
86
87         ret = i2c_master_send(mcs_c, val, 1);
88         if (ret != 1)
89                 dev_err(&mcs_c->dev, "%s: failed\n", __func__);
90
91         return ret;
92 }
93
94 static int mcs_i2c_fw_read(u8 slave_addr, u8 *val)
95 {
96         int ret = 0;
97
98         mcs_c->addr = slave_addr;
99
100         ret = i2c_master_recv(mcs_c, val, 1);
101         if (ret != 1)
102                 dev_err(&mcs_c->dev, "%s: failed\n", __func__);
103
104         return ret;
105 }
106
107
108 //============================================================
109 //
110 //      Include MELFAS Binary source code File ( ex> MELFAS_FIRM_bin.c)
111 //
112 //      Warning!!!!
113 //              .c file included at next must not be inserted to Souce project.
114 //              Just #include One file. !!
115 //
116 //============================================================
117 //#include "MCS5080_SAMPLE_FIRMWARE_R01_V00_bin.c"
118 //#include "MCS5080_SAMPLE_FIRMWARE_R01_V01_bin.c"
119 #include "MTH_KESSLER_R01_V02_bin.c"
120
121
122 //============================================================
123 //
124 //      main Download furnction
125 //
126 //============================================================
127
128 int mcsdl_download_binary_data(void)
129 {
130         int ret;
131
132         #if MELFAS_USE_PROTOCOL_COMMAND_FOR_DOWNLOAD
133
134         melfas_send_download_enable_command();
135
136         mcsdl_delay(MCSDL_DELAY_100US);
137
138         #endif
139
140         MELFAS_DISABLE_BASEBAND_ISR();                                  // Disable Baseband touch interrupt ISR.
141         MELFAS_DISABLE_WATCHDOG_TIMER_RESET();                  // Disable Baseband watchdog timer
142
143         //------------------------
144         // Run Download
145         //------------------------
146         ret = mcsdl_download( (const UINT8*) MELFAS_binary, (const UINT16)MELFAS_binary_nLength );
147
148         MELFAS_ROLLBACK_BASEBAND_ISR();                                 // Roll-back Baseband touch interrupt ISR.
149         MELFAS_ROLLBACK_WATCHDOG_TIMER_RESET();                 // Roll-back Baseband watchdog timer
150
151         #if MELFAS_ENABLE_DBG_PRINT
152
153                 //------------------------
154                 // Show result
155                 //------------------------
156
157                 mcsdl_print_result( ret );
158
159         #endif
160
161         return ( ret == MCSDL_RET_SUCCESS );
162 }
163
164
165
166 int mcsdl_download_binary_file(void)
167 {
168         int ret;
169
170         UINT8  *pData = NULL;
171         UINT16 nBinary_length =0;
172
173         //==================================================
174         //
175         //      Porting section 7. File process
176         //
177         //      1. Read '.bin file'
178         //      2. Run mcsdl_download_binary_data();
179         //
180         //==================================================
181
182         #if 0
183
184                 // TO DO : File Process & Get file Size(== Binary size)
185                 //                      This is just a simple sample
186
187                 FILE *fp;
188                 INT  nRead;
189
190                 //------------------------------
191                 // Open a file
192                 //------------------------------
193
194                 if( fopen( fp, "MELFAS_FIRM.bin", "rb" ) == NULL ){
195                         return MCSDL_RET_FILE_ACCESS_FAILED;
196                 }
197
198                 //------------------------------
199                 // Get Binary Size
200                 //------------------------------
201
202                 fseek( fp, 0, SEEK_END );
203
204                 nBinary_length = (UINT16)ftell(fp);
205
206                 //------------------------------
207                 // Memory allocation
208                 //------------------------------
209
210                 pData = (UINT8*)malloc( (INT)nBinary_length );
211
212                 if( pData == NULL ){
213
214                         return MCSDL_RET_FILE_ACCESS_FAILED;
215                 }
216
217                 //------------------------------
218                 // Read binary file
219                 //------------------------------
220
221                 fseek( fp, 0, SEEK_SET );
222
223                 nRead = fread( pData, 1, (INT)nBinary_length, fp );             // Read binary file
224
225                 if( nRead != (INT)nBinary_length ){
226
227                         fclose(fp);                                                                                             // Close file
228
229                         if( pData != NULL )                                                                             // free memory alloced.
230                                 free(pData);
231
232                         return MCSDL_RET_FILE_ACCESS_FAILED;
233                 }
234
235                 //------------------------------
236                 // Close file
237                 //------------------------------
238
239                 fclose(fp);
240
241         #endif
242
243         if( pData != NULL && nBinary_length > 0 && nBinary_length < 14*1024 ){
244
245                 MELFAS_DISABLE_BASEBAND_ISR();                                  // Disable Baseband touch interrupt ISR.
246                 MELFAS_DISABLE_WATCHDOG_TIMER_RESET();                  // Disable Baseband watchdog timer
247
248                 ret = mcsdl_download( (const UINT8 *)pData, (const UINT16)nBinary_length );
249
250                 MELFAS_ROLLBACK_BASEBAND_ISR();                                 // Roll-back Baseband touch interrupt ISR.
251                 MELFAS_ROLLBACK_WATCHDOG_TIMER_RESET();                 // Roll-back Baseband watchdog timer
252
253         }else{
254
255                 ret = MCSDL_RET_WRONG_PARAMETER;
256         }
257
258         #if MELFAS_ENABLE_DBG_PRINT
259
260         mcsdl_print_result( ret );
261
262         #endif
263
264         #if 0
265                 if( pData != NULL )                                                                             // free memory alloced.
266                         free(pData);
267         #endif
268
269         return ( ret == MCSDL_RET_SUCCESS );
270
271 }
272
273 //------------------------------------------------------------------
274 //
275 //      Download function
276 //
277 //------------------------------------------------------------------
278
279 static int mcsdl_download(const UINT8 *pData, const UINT16 nLength )
280 {
281         int             i;
282         int             nRet;
283
284         UINT8   cLength;
285         UINT16  nStart_address=0;
286
287         UINT8   buffer[MELFAS_TRANSFER_LENGTH];
288         UINT8   *pOriginal_data;
289
290
291         #if MELFAS_ENABLE_DBG_PROGRESS_PRINT
292         uart_printf("Starting download...\n");
293         #endif
294
295         //--------------------------------------------------------------
296         //
297         // Enter Download mode
298         //
299         //--------------------------------------------------------------
300         nRet = mcsdl_enter_download_mode();
301
302         if( nRet != MCSDL_RET_SUCCESS )
303                 goto MCSDL_DOWNLOAD_FINISH;
304
305         mcsdl_delay(MCSDL_DELAY_1MS);                                   // Delay '1 msec'
306
307         //--------------------------------------------------------------
308         //
309         // Check H/W Revision
310         //
311         // Don't download firmware, if Module H/W revision does not match.
312         //
313         //--------------------------------------------------------------
314         #if MELFAS_DISABLE_DOWNLOAD_IF_MODULE_VERSION_DOES_NOT_MATCH
315
316                 #if MELFAS_ENABLE_DBG_PROGRESS_PRINT
317                 uart_printf("Checking module revision...\n");
318                 #endif
319
320                 pOriginal_data  = (UINT8 *)pData;
321
322                 nRet = mcsdl_i2c_read_flash( buffer, MCSDL_ADDR_MODULE_REVISION, 4 );
323
324                 if( nRet != MCSDL_RET_SUCCESS )
325                         goto MCSDL_DOWNLOAD_FINISH;
326
327                 if(     (pOriginal_data[MCSDL_ADDR_MODULE_REVISION+1] != buffer[1])
328                         ||      (pOriginal_data[MCSDL_ADDR_MODULE_REVISION+2] != buffer[2]) ){
329
330                         nRet = MCSDL_RET_WRONG_MODULE_REVISION;
331                         goto MCSDL_DOWNLOAD_FINISH;
332                 }
333
334                 mcsdl_delay(MCSDL_DELAY_1MS);                                   // Delay '1 msec'
335
336         #endif
337
338         //--------------------------------------------------------------
339         //
340         // Erase Flash
341         //
342         //--------------------------------------------------------------
343
344         #if MELFAS_ENABLE_DBG_PROGRESS_PRINT
345         uart_printf("Erasing...\n");
346         #endif
347
348         nRet = mcsdl_i2c_prepare_erase_flash();
349
350         if( nRet != MCSDL_RET_SUCCESS ){
351                 goto MCSDL_DOWNLOAD_FINISH;
352         }
353
354         mcsdl_delay(MCSDL_DELAY_1MS);                                   // Delay '1 msec'
355
356         nRet = mcsdl_i2c_erase_flash();
357
358         if( nRet != MCSDL_RET_SUCCESS ){
359                 goto MCSDL_DOWNLOAD_FINISH;
360         }
361
362         mcsdl_delay(MCSDL_DELAY_1MS);                                   // Delay '1 msec'
363
364
365         //--------------------------------------------------------------
366         //
367         // Verify erase
368         //
369         //--------------------------------------------------------------
370         #if MELFAS_ENABLE_DBG_PROGRESS_PRINT
371         uart_printf("Verify Erasing...\n");
372         #endif
373
374         nRet = mcsdl_i2c_read_flash( buffer, 0x00, 16 );
375
376         if( nRet != MCSDL_RET_SUCCESS )
377                 goto MCSDL_DOWNLOAD_FINISH;
378
379
380         for(i=0; i<16; i++){
381
382                 if( buffer[i] != 0xFF ){
383
384                         nRet = MCSDL_RET_ERASE_VERIFY_FAILED;
385                         goto MCSDL_DOWNLOAD_FINISH;
386                 }
387         }
388
389         mcsdl_delay(MCSDL_DELAY_1MS);                                   // Delay '1 msec'
390
391
392         //--------------------------------------------------------------
393         //
394         // Prepare for Program flash.
395         //
396         //--------------------------------------------------------------
397         #if MELFAS_ENABLE_DBG_PROGRESS_PRINT
398         uart_printf("Program information...\n");
399         #endif
400
401         nRet = mcsdl_i2c_prepare_program();
402
403         if( nRet != MCSDL_RET_SUCCESS )
404                 goto MCSDL_DOWNLOAD_FINISH;
405
406
407         mcsdl_delay(MCSDL_DELAY_1MS);                                   // Delay '1 msec'
408
409
410         //--------------------------------------------------------------
411    //
412    // Program flash
413    //
414         //--------------------------------------------------------------
415
416         #if MELFAS_ENABLE_DBG_PROGRESS_PRINT
417         uart_printf("Program flash...  ");
418         #endif
419
420         pOriginal_data  = (UINT8 *)pData;
421
422         nStart_address = 0;
423         cLength  = MELFAS_TRANSFER_LENGTH;
424
425         for( nStart_address = 0; nStart_address < nLength; nStart_address+=cLength ){
426
427                 #if MELFAS_ENABLE_DBG_PROGRESS_PRINT
428                 uart_printf("#");
429                 #endif
430
431                 if( ( nLength - nStart_address ) < MELFAS_TRANSFER_LENGTH ){
432                         cLength  = (UINT8)(nLength - nStart_address);
433
434                         cLength += (cLength%2);                                                                 // For odd length.
435                 }
436
437                 nRet = mcsdl_i2c_program_flash( pOriginal_data, nStart_address, cLength );
438
439         if( nRet != MCSDL_RET_SUCCESS ){
440
441                         #if MELFAS_ENABLE_DBG_PROGRESS_PRINT
442                         uart_printf("Program flash failed position : 0x%x / nRet : 0x%x ", nStart_address, nRet);
443                         #endif
444
445             goto MCSDL_DOWNLOAD_FINISH;
446                 }
447
448                 pOriginal_data  += cLength;
449
450                 mcsdl_delay(MCSDL_DELAY_500US);                                 // Delay '500 usec'
451
452         }
453
454
455         //--------------------------------------------------------------
456         //
457         // Verify flash
458         //
459         //--------------------------------------------------------------
460
461         #if MELFAS_ENABLE_DBG_PROGRESS_PRINT
462         uart_printf("\n");
463         uart_printf("Verify flash...   ");
464         #endif
465
466         pOriginal_data  = (UINT8 *) pData;
467
468         nStart_address = 0;
469
470         cLength  = MELFAS_TRANSFER_LENGTH;
471
472         for( nStart_address = 0; nStart_address < nLength; nStart_address+=cLength ){
473
474                 #if MELFAS_ENABLE_DBG_PROGRESS_PRINT
475                 uart_printf("#");
476                 #endif
477
478                 if( ( nLength - nStart_address ) < MELFAS_TRANSFER_LENGTH ){
479                         cLength = (UINT8)(nLength - nStart_address);
480
481                         cLength += (cLength%2);                                                                 // For odd length.
482                 }
483
484                 //--------------------
485                 // Read flash
486                 //--------------------
487                 nRet = mcsdl_i2c_read_flash( buffer, nStart_address, cLength );
488
489                 //--------------------
490                 // Comparing
491                 //--------------------
492
493                 for(i=0; i<(int)cLength; i++){
494
495
496                         if( buffer[i] != pOriginal_data[i] ){
497
498                                 #if MELFAS_ENABLE_DBG_PROGRESS_PRINT
499                                 uart_printf("0x%04X : 0x%02X - 0x%02X\n", nStart_address, pOriginal_data[i], buffer[i] );
500                 #endif
501
502                                 nRet = MCSDL_RET_PROGRAM_VERIFY_FAILED;
503                                 goto MCSDL_DOWNLOAD_FINISH;
504
505                         }
506                 }
507
508                 pOriginal_data += cLength;
509
510
511                 mcsdl_delay(MCSDL_DELAY_500US);                                 // Delay '500 usec'
512         }
513
514         uart_printf("\n");
515
516         nRet = MCSDL_RET_SUCCESS;
517
518
519 MCSDL_DOWNLOAD_FINISH :
520
521         mcsdl_delay(MCSDL_DELAY_1MS);                                   // Delay '1 msec'
522
523         //---------------------------
524         //      Reset command
525         //---------------------------
526         buffer[0] = MCSDL_ISP_CMD_RESET;
527
528         _i2c_write_( MCSDL_I2C_SLAVE_ADDR_ORG, buffer, 1 );
529
530         TKEY_INTR_SET_INPUT();
531
532     mcsdl_delay(MCSDL_DELAY_45MS);
533
534         return nRet;
535 }
536
537
538 //------------------------------------------------------------------
539 //
540 //   Enter Download mode ( MDS ISP or I2C ISP )
541 //
542 //------------------------------------------------------------------
543 static int mcsdl_enter_download_mode(void)
544 {
545         BOOLEAN bRet;
546         int     nRet = MCSDL_RET_ENTER_DOWNLOAD_MODE_FAILED;
547
548         UINT8   cData=0;
549
550         //--------------------------------------------
551         // Tkey module reset
552         //--------------------------------------------
553
554         TKEY_VDD_SET_LOW();
555
556         TKEY_CE_SET_LOW();
557         TKEY_CE_SET_OUTPUT();
558
559         TKEY_I2C_CLOSE();
560
561         TKEY_INTR_SET_LOW();
562         TKEY_INTR_SET_OUTPUT();
563
564         TKEY_RESETB_SET_LOW();
565         TKEY_RESETB_SET_OUTPUT();
566
567         mcsdl_delay(MCSDL_DELAY_45MS);                                  // Delay for VDD LOW Stable
568         mcsdl_delay(MCSDL_DELAY_45MS);
569         
570         TKEY_VDD_SET_HIGH();
571         TKEY_CE_SET_HIGH();
572         TKEY_I2C_SDA_SET_HIGH();
573
574         mcsdl_delay(MCSDL_DELAY_45MS);                                          // Delay '45 msec'
575
576         //-------------------------------
577         // Write 1st signal
578         //-------------------------------
579         mcsdl_write_download_mode_signal();
580
581         mcsdl_delay(MCSDL_DELAY_25MS);                                          // Delay '25 msec'
582
583         //-------------------------------
584         // Check response
585         //-------------------------------
586
587         bRet = _i2c_read_( MCSDL_I2C_SLAVE_ADDR_ORG, &cData, 1 );
588
589         if( bRet != TRUE || cData != MCSDL_I2C_SLAVE_READY_STATUS ){
590
591                 uart_printf("mcsdl_enter_download_mode() returns - ret : 0x%x & cData : 0x%x\n", nRet, cData);
592                 goto MCSDL_ENTER_DOWNLOAD_MODE_FINISH;
593         }
594
595         nRet = MCSDL_RET_SUCCESS;
596
597         //-----------------------------------
598         // Entering MDS ISP mode finished.
599         //-----------------------------------
600
601 MCSDL_ENTER_DOWNLOAD_MODE_FINISH:
602
603    return nRet;
604 }
605
606 //--------------------------------------------
607 //
608 //   Write ISP Mode entering signal
609 //
610 //--------------------------------------------
611 static void mcsdl_write_download_mode_signal(void)
612 {
613         int    i;
614
615         UINT8 enter_code[14] = { 0, 1, 0, 1, 0, 1, 0, 1,   1, 0, 1, 1, 1, 1 };
616
617         //---------------------------
618         // ISP mode signal 0
619         //---------------------------
620
621         for(i=0; i<14; i++){
622
623                 if( enter_code[i] )     {
624
625                         TKEY_RESETB_SET_HIGH();
626                         TKEY_INTR_SET_HIGH();
627
628                 }else{
629
630                         TKEY_RESETB_SET_LOW();
631                         TKEY_INTR_SET_LOW();
632                 }
633
634                 TKEY_I2C_SCL_SET_HIGH();        mcsdl_delay(MCSDL_DELAY_15US);
635                 TKEY_I2C_SCL_SET_LOW();
636
637                 TKEY_RESETB_SET_LOW();
638                 TKEY_INTR_SET_LOW();
639
640                 mcsdl_delay(MCSDL_DELAY_100US);
641
642    }
643
644         TKEY_I2C_SCL_SET_HIGH();
645
646         mcsdl_delay(MCSDL_DELAY_100US);
647
648         TKEY_INTR_SET_HIGH();
649         TKEY_RESETB_SET_HIGH();
650 }
651
652
653 //--------------------------------------------
654 //
655 //   Prepare Erase flash
656 //
657 //--------------------------------------------
658 static int mcsdl_i2c_prepare_erase_flash(void)
659 {
660         int   nRet = MCSDL_RET_PREPARE_ERASE_FLASH_FAILED;
661
662         UINT8 i;
663         BOOLEAN   bRet;
664
665         UINT8 i2c_buffer[4] = { MCSDL_ISP_CMD_ERASE_TIMING,
666                                 MCSDL_ISP_ERASE_TIMING_VALUE_0,
667                                 MCSDL_ISP_ERASE_TIMING_VALUE_1,
668                                 MCSDL_ISP_ERASE_TIMING_VALUE_2   };
669         UINT8   ucTemp;
670
671    //-----------------------------
672    // Send Erase Setting code
673    //-----------------------------
674
675    for(i=0; i<4; i++){
676
677                 bRet = _i2c_write_(MCSDL_I2C_SLAVE_ADDR_ORG, &i2c_buffer[i], 1 );
678
679                 if( !bRet )
680                         goto MCSDL_I2C_PREPARE_ERASE_FLASH_FINISH;
681
682                 mcsdl_delay(MCSDL_DELAY_15US);
683    }
684
685    //-----------------------------
686    // Read Result
687    //-----------------------------
688
689         mcsdl_delay(MCSDL_DELAY_500US);                  // Delay 500usec
690
691         bRet = _i2c_read_(MCSDL_I2C_SLAVE_ADDR_ORG, &ucTemp, 1 );
692
693         if( bRet && ucTemp == MCSDL_ISP_ACK_PREPARE_ERASE_DONE ){
694
695                 nRet = MCSDL_RET_SUCCESS;
696
697         }
698
699
700 MCSDL_I2C_PREPARE_ERASE_FLASH_FINISH :
701
702    return nRet;
703
704 }
705
706
707 //--------------------------------------------
708 //
709 //   Erase flash
710 //
711 //--------------------------------------------
712 static int mcsdl_i2c_erase_flash(void)
713 {
714         int   nRet = MCSDL_RET_ERASE_FLASH_FAILED;
715
716         UINT8   i;
717         BOOLEAN bRet;
718
719         UINT8   i2c_buffer[1] = {       MCSDL_ISP_CMD_ERASE};
720         UINT8   ucTemp;
721
722    //-----------------------------
723    // Send Erase code
724    //-----------------------------
725
726    for(i=0; i<1; i++){
727
728                 bRet = _i2c_write_(MCSDL_I2C_SLAVE_ADDR_ORG, &i2c_buffer[i], 1 );
729
730                 if( !bRet )
731                         goto MCSDL_I2C_ERASE_FLASH_FINISH;
732
733                 mcsdl_delay(MCSDL_DELAY_15US);
734    }
735
736    //-----------------------------
737    // Read Result
738    //-----------------------------
739
740         mcsdl_delay(MCSDL_DELAY_45MS);                  // Delay 45ms
741
742
743         bRet = _i2c_read_(MCSDL_I2C_SLAVE_ADDR_ORG, &ucTemp, 1 );
744
745         if( bRet && ucTemp == MCSDL_ISP_ACK_ERASE_DONE ){
746
747                 nRet = MCSDL_RET_SUCCESS;
748
749         }
750
751
752 MCSDL_I2C_ERASE_FLASH_FINISH :
753
754    return nRet;
755
756 }
757
758 //--------------------------------------------
759 //
760 //   Read flash
761 //
762 //--------------------------------------------
763 static int mcsdl_i2c_read_flash( UINT8 *pBuffer, UINT16 nAddr_start, UINT8 cLength)
764 {
765         int nRet = MCSDL_RET_READ_FLASH_FAILED;
766
767         int     i;
768         BOOLEAN bRet;
769         UINT8   cmd[4];
770         UINT8   ucTemp;
771
772         //-----------------------------------------------------------------------------
773         // Send Read Flash command   [ Read code - address high - address low - size ]
774         //-----------------------------------------------------------------------------
775
776         cmd[0] = MCSDL_ISP_CMD_READ_FLASH;
777         cmd[1] = (UINT8)((nAddr_start >> 8 ) & 0xFF);
778         cmd[2] = (UINT8)((nAddr_start      ) & 0xFF);
779         cmd[3] = cLength;
780
781         for(i=0; i<4; i++){
782
783                 bRet = _i2c_write_( MCSDL_I2C_SLAVE_ADDR_ORG, &cmd[i], 1 );
784
785                 mcsdl_delay(MCSDL_DELAY_15US);
786
787                 if( bRet == FALSE )
788                         goto MCSDL_I2C_READ_FLASH_FINISH;
789
790    }
791
792         //----------------------------------
793         // Read 'Result of command'
794         //----------------------------------
795         bRet = _i2c_read_( MCSDL_I2C_SLAVE_ADDR_ORG, &ucTemp, 1 );
796
797         if( !bRet || ucTemp != MCSDL_MDS_ACK_READ_FLASH){
798
799                 goto MCSDL_I2C_READ_FLASH_FINISH;
800         }
801
802         //----------------------------------
803         // Read Data  [ pCmd[3] == Size ]
804         //----------------------------------
805         for(i=0; i<(int)cmd[3]; i++){
806
807                 mcsdl_delay(MCSDL_DELAY_100US);                  // Delay about 100us
808
809                 bRet = _i2c_read_( MCSDL_I2C_SLAVE_ADDR_ORG, pBuffer++, 1 );
810
811                 if( bRet == FALSE && i!=(int)(cmd[3]-1) )
812                         goto MCSDL_I2C_READ_FLASH_FINISH;
813         }
814
815         nRet = MCSDL_RET_SUCCESS;
816
817
818 MCSDL_I2C_READ_FLASH_FINISH :
819
820         return nRet;
821 }
822
823
824 //--------------------------------------------
825 //
826 //   Prepare Program
827 //
828 //--------------------------------------------
829 static int mcsdl_i2c_prepare_program(void)
830 {
831
832         int nRet = MCSDL_RET_PREPARE_PROGRAM_FAILED;
833
834         int i;
835         BOOLEAN bRet;
836
837         UINT8 i2c_buffer[4] = { MCSDL_ISP_CMD_PROGRAM_TIMING,
838                                     MCSDL_ISP_PROGRAM_TIMING_VALUE_0,
839                                     MCSDL_ISP_PROGRAM_TIMING_VALUE_1,
840                                     MCSDL_ISP_PROGRAM_TIMING_VALUE_2};
841
842         //------------------------------------------------------
843         //   Write Program timing information
844         //------------------------------------------------------
845         for(i=0; i<4; i++){
846
847                         bRet = _i2c_write_( MCSDL_I2C_SLAVE_ADDR_ORG, &i2c_buffer[i], 1 );
848
849                         if( bRet == FALSE )
850                                 goto MCSDL_I2C_PREPARE_PROGRAM_FINISH;
851
852                         mcsdl_delay(MCSDL_DELAY_15US);
853         }
854
855         mcsdl_delay(MCSDL_DELAY_500US);                     // delay about  500us
856
857         //------------------------------------------------------
858         //   Read command's result
859         //------------------------------------------------------
860         bRet = _i2c_read_( MCSDL_I2C_SLAVE_ADDR_ORG, &i2c_buffer[4], 1 );
861
862         if( bRet == FALSE || i2c_buffer[4] != MCSDL_I2C_ACK_PREPARE_PROGRAM)
863                 goto MCSDL_I2C_PREPARE_PROGRAM_FINISH;
864
865         mcsdl_delay(MCSDL_DELAY_100US);                     // delay about  100us
866
867    nRet = MCSDL_RET_SUCCESS;
868
869 MCSDL_I2C_PREPARE_PROGRAM_FINISH :
870
871    return nRet;
872
873 }
874
875 //--------------------------------------------
876 //
877 //   Program Flash
878 //
879 //--------------------------------------------
880
881 static int mcsdl_i2c_program_flash( UINT8 *pData, UINT16 nAddr_start, UINT8 cLength )
882 {
883         int nRet = MCSDL_RET_PROGRAM_FLASH_FAILED;
884
885         int     i,j;
886         BOOLEAN   bRet;
887         UINT8    cData;
888         UINT8    tmp;
889         UINT8 cmd[4];
890
891
892         //-----------------------------
893         // Send Read code
894         //-----------------------------
895
896         cmd[0] = MCSDL_ISP_CMD_PROGRAM_FLASH;
897         cmd[1] = (UINT8)((nAddr_start >> 8 ) & 0xFF);
898         cmd[2] = (UINT8)((nAddr_start      ) & 0xFF);
899         cmd[3] = cLength;
900
901         for(i=0; i<4; i++){
902
903                 bRet = _i2c_write_(MCSDL_I2C_SLAVE_ADDR_ORG, &cmd[i], 1 );
904
905                 mcsdl_delay(MCSDL_DELAY_15US);
906
907                 if( bRet == FALSE )
908                         goto MCSDL_I2C_PROGRAM_FLASH_FINISH;
909
910         }
911         //-----------------------------
912         // Check command result
913         //-----------------------------
914
915         bRet = _i2c_read_( MCSDL_I2C_SLAVE_ADDR_ORG, &cData, 1 );
916
917         if( bRet == FALSE || cData != MCSDL_MDS_ACK_PROGRAM_FLASH ){
918
919                 goto MCSDL_I2C_PROGRAM_FLASH_FINISH;
920         }
921
922
923         //-----------------------------
924         // Program Data
925         //-----------------------------
926
927         mcsdl_delay(MCSDL_DELAY_150US);                  // Delay about 150us
928
929         for(i=0; i<(int)cmd[3]; i+=2){
930
931
932                 bRet = _i2c_write_( MCSDL_I2C_SLAVE_ADDR_ORG, &pData[i+1], 1 );
933
934                 mcsdl_delay(MCSDL_DELAY_150US);                  // Delay about 150us
935
936                 if( bRet == FALSE )
937                         goto MCSDL_I2C_PROGRAM_FLASH_FINISH;
938
939
940                 bRet = _i2c_write_( MCSDL_I2C_SLAVE_ADDR_ORG, &pData[i], 1 );
941
942                 mcsdl_delay(MCSDL_DELAY_150US);                  // Delay about 150us
943
944                 if( bRet == FALSE )
945                         goto MCSDL_I2C_PROGRAM_FLASH_FINISH;
946
947         }
948
949
950         nRet = MCSDL_RET_SUCCESS;
951
952 MCSDL_I2C_PROGRAM_FLASH_FINISH :
953
954    return nRet;
955 }
956
957
958 //============================================================
959 //
960 //      Delay Function
961 //
962 //============================================================
963 static void mcsdl_delay(UINT32 nCount)
964 {
965
966         #if 1
967
968 //              base_band_delay_function(nCount);                       // Baseband delay function
969                 if(nCount >= 1000)
970                         mdelay(nCount/1000);
971                 else
972                         udelay(nCount);
973
974         #else
975
976                 UINT32 i;
977
978                 for(i=0;i<nCount;i++){
979
980                 }
981
982         #endif
983 }
984
985
986 //============================================================
987 //
988 //      Porting section 6.      I2C function calling
989 //
990 //      Connect baseband i2c function
991 //
992 //      Warning 1. !!!!  Burst mode is not supported. Transfer 1 byte Only.
993 //
994 //              Every i2c packet has to
995 //                      " START > Slave address > One byte > STOP " at download mode.
996 //
997 //      Warning 2. !!!!  Check return value of i2c function.
998 //
999 //              _i2c_read_(), _i2c_write_() must return
1000 //                      TRUE (1) if success,
1001 //                      FALSE(0) if failed.
1002 //
1003 //              If baseband i2c function returns different value, convert return value.
1004 //                      ex> baseband_return = baseband_i2c_read( slave_addr, pData, cLength );
1005 //                              return ( baseband_return == BASEBAND_RETURN_VALUE_SUCCESS );
1006 //
1007 //
1008 //      Warning 3. !!!!  Check Slave address
1009 //
1010 //              Slave address is '0x7F' at download mode. ( Diffrent with Normal touch working mode )
1011 //              '0x7F' is original address,
1012 //                      If shift << 1 bit, It becomes '0xFE'
1013 //
1014 //============================================================
1015
1016 BOOLEAN _i2c_read_( UINT8 slave_addr, UINT8 *pData, UINT8 cLength)
1017 {
1018         BOOLEAN bRet;
1019
1020         #if USE_BASEBAND_I2C_FUNCTION
1021
1022                 //bRet = baseband_i2c_read( slave_addr<<1, pData, cLength );
1023                 //bRet = baseband_i2c_read( slave_addr, pData, cLength );
1024                 bRet = mcs_i2c_fw_read(slave_addr, pData);
1025
1026         #else
1027
1028         bRet = FALSE;
1029
1030         #endif
1031
1032         return ( bRet == TRUE );
1033 }
1034
1035 BOOLEAN _i2c_write_(UINT8 slave_addr, UINT8 *pData, UINT8 cLength)
1036 {
1037         BOOLEAN bRet;
1038
1039         #if USE_BASEBAND_I2C_FUNCTION
1040
1041                 //bRet = baseband_i2c_write( slave_addr<<1, pData, cLength );
1042                 //bRet = baseband_i2c_write( slave_addr, pData, cLength );
1043                 bRet = mcs_i2c_fw_write(slave_addr, pData);
1044
1045         #else
1046
1047         bRet = FALSE;
1048
1049         #endif
1050
1051         return ( bRet == TRUE );
1052 }
1053
1054
1055
1056
1057 //============================================================
1058 //
1059 //      Debugging print functions.
1060 //
1061 //      Change uart_printf() to Baseband printing function
1062 //
1063 //============================================================
1064
1065 #if MELFAS_ENABLE_DBG_PRINT
1066
1067 static void mcsdl_print_result(int nRet)
1068 {
1069         if( nRet == MCSDL_RET_SUCCESS ){
1070
1071                 uart_printf(" MELFAS Firmware downloading SUCCESS.\n");
1072
1073         }else{
1074
1075                 uart_printf(" MELFAS Firmware downloading FAILED  :  ");
1076
1077                 switch( nRet ){
1078
1079                         case MCSDL_RET_SUCCESS                                  :   uart_printf("MCSDL_RET_SUCCESS\n" );                        break;
1080                         case MCSDL_RET_ENTER_DOWNLOAD_MODE_FAILED       :   uart_printf("MCSDL_RET_ENTER_ISP_MODE_FAILED\n" );          break;
1081                         case MCSDL_RET_ERASE_FLASH_FAILED               :   uart_printf("MCSDL_RET_ERASE_FLASH_FAILED\n" );             break;
1082                         case MCSDL_RET_READ_FLASH_FAILED                                :   uart_printf("MCSDL_RET_READ_FLASH_FAILED\n" );              break;
1083                         case MCSDL_RET_READ_EEPROM_FAILED               :   uart_printf("MCSDL_RET_READ_EEPROM_FAILED\n" );             break;
1084                         case MCSDL_RET_READ_INFORMAION_FAILED           :   uart_printf("MCSDL_RET_READ_INFORMAION_FAILED\n" );         break;
1085                         case MCSDL_RET_PROGRAM_FLASH_FAILED                             :   uart_printf("MCSDL_RET_PROGRAM_FLASH_FAILED\n" );           break;
1086                         case MCSDL_RET_PROGRAM_EEPROM_FAILED            :   uart_printf("MCSDL_RET_PROGRAM_EEPROM_FAILED\n" );          break;
1087                         case MCSDL_RET_PREPARE_PROGRAM_FAILED                   :   uart_printf("MCSDL_RET_PROGRAM_INFORMAION_FAILED\n" );   break;
1088                         case MCSDL_RET_PROGRAM_VERIFY_FAILED                    :   uart_printf("MCSDL_RET_PROGRAM_VERIFY_FAILED\n" );          break;
1089
1090                         case MCSDL_RET_WRONG_MODE_ERROR                 :   uart_printf("MCSDL_RET_WRONG_MODE_ERROR\n" );               break;
1091                         case MCSDL_RET_WRONG_SLAVE_SELECTION_ERROR              :   uart_printf("MCSDL_RET_WRONG_SLAVE_SELECTION_ERROR\n" ); break;
1092                         case MCSDL_RET_COMMUNICATION_FAILED                             :   uart_printf("MCSDL_RET_COMMUNICATION_FAILED\n" );           break;
1093                         case MCSDL_RET_READING_HEXFILE_FAILED           :   uart_printf("MCSDL_RET_READING_HEXFILE_FAILED\n" );      break;
1094                         case MCSDL_RET_WRONG_PARAMETER                          :   uart_printf("MCSDL_RET_WRONG_PARAMETER\n" );                break;
1095                         case MCSDL_RET_FILE_ACCESS_FAILED                       :   uart_printf("MCSDL_RET_FILE_ACCESS_FAILED\n" );             break;
1096                         case MCSDL_RET_MELLOC_FAILED                                    :   uart_printf("MCSDL_RET_MELLOC_FAILED\n" );                          break;
1097                         case MCSDL_RET_WRONG_MODULE_REVISION                    :   uart_printf("MCSDL_RET_WRONG_MODULE_REVISION\n" );          break;
1098
1099                         default                                                 :       uart_printf("UNKNOWN ERROR. [0x%02X].\n", nRet );       break;
1100                 }
1101
1102                 uart_printf("\n");
1103         }
1104
1105 }
1106
1107 #endif
1108
1109 //============================================================
1110 //
1111 //      Porting section 4-1. Delay function
1112 //
1113 //      For initial testing of delay and gpio control
1114 //
1115 //      You can confirm GPIO control and delay time by calling this function.
1116 //
1117 //============================================================
1118
1119 #if MELFAS_ENABLE_DELAY_TEST
1120
1121
1122 void mcsdl_delay_test(INT32 nCount)
1123 {
1124         INT16 i;
1125
1126         MELFAS_DISABLE_BASEBAND_ISR();                                  // Disable Baseband touch interrupt ISR.
1127         MELFAS_DISABLE_WATCHDOG_TIMER_RESET();                  // Disable Baseband watchdog timer
1128
1129         TKEY_I2C_SET_OUTPUT();
1130         TKEY_CE_SET_OUTPUT();
1131         TKEY_INTR_SET_OUTPUT();
1132         TKEY_RESETB_SET_OUTPUT();
1133
1134         //--------------------------------
1135         //      Repeating 'nCount' times
1136         //--------------------------------
1137
1138
1139         for( i=0; i<nCount; i++ ){
1140
1141                 TKEY_I2C_SET_HIGH();                                            // NORMAL
1142                 TKEY_VDD_SET_HIGH();
1143                 TKEY_CE_SET_HIGH();
1144                 TKEY_RESETB_SET_HIGH();
1145
1146                 mcsdl_delay(MCSDL_DELAY_15US);
1147
1148                 TKEY_VDD_SET_LOW();                                                     // VDD & CE LOW
1149                 TKEY_CE_SET_LOW();
1150                 TKEY_I2C_SCL_SET_LOW();                                         // SCL LOW
1151
1152                 mcsdl_delay(MCSDL_DELAY_100US)
1153
1154                 TKEY_VDD_SET_HIGH();                                            // VDD & CE HIGH
1155                 TKEY_CE_SET_HIGH();
1156                 TKEY_I2C_SCL_SET_HIGH();                                        // SCL HIGH
1157
1158
1159                 TKEY_INTR_SET_LOW();                                            // INTR & RESETB LOW
1160                 TKEY_RESETB_SET_LOW();
1161                 TKEY_I2C_SCL_SET_LOW();                                         // SCL LOW
1162
1163         mcsdl_delay(MCSDL_DELAY_500US);
1164
1165                 TKEY_INTR_SET_HIGH();                                           // INTR & RESETB HIGH
1166                 TKEY_RESETB_SET_HIGH();
1167                 TKEY_I2C_SCL_SET_HIGH();                                        // SCL HIGH
1168
1169                 TKEY_I2C_SCL_SET_LOW();                                         // SCL LOW
1170
1171         mcsdl_delay(MCSDL_DELAY_1MS);
1172
1173                 TKEY_I2C_SCL_SET_HIGH();                                        // SCL HIGH
1174
1175                 TKEY_I2C_SDA_SET_LOW();                                         // SDA LOW
1176
1177         mcsdl_delay(MCSDL_DELAY_25MS);
1178
1179                 TKEY_I2C_SDA_SET_HIGH();                                        // SDA HIGH
1180                 TKEY_I2C_SCL_SET_LOW();                                         // SCL LOW
1181
1182         mcsdl_delay(MCSDL_DELAY_45MS);
1183
1184                 TKEY_I2C_SCL_SET_HIGH();                                        // SCL HIGH
1185         }
1186
1187
1188     TKEY_INTR_SET_INPUT();
1189
1190         MELFAS_ROLLBACK_BASEBAND_ISR();                                 // Roll-back Baseband touch interrupt ISR.
1191         MELFAS_ROLLBACK_WATCHDOG_TIMER_RESET();                 // Roll-back Baseband watchdog timer
1192 }
1193
1194
1195 #endif
1196
1197
1198
1199