-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathmsr3110.c
executable file
·1516 lines (1183 loc) · 38.2 KB
/
msr3110.c
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
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/i2c.h>
#include <linux/irq.h>
#include <linux/jiffies.h>
#include <linux/uaccess.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/platform_device.h>
#include <linux/gpio.h>
#include <linux/miscdevice.h>
#include <linux/spinlock.h>
#include <linux/dma-mapping.h>
#include <cust_gpio_usage.h>
#include <cust_eint_ext.h>
#include <mach/mt_gpio.h>
#include <mach/eint.h>
#define TRACE_THIS_MODULE 0
#define MY_DEBUG 0
#if ( TRACE_THIS_MODULE == 1)
#define ENTER() \
printk(KERN_ERR "[%s]start\n", __FUNCTION__)
#define FUNC_START() \
printk(KERN_ERR "[%s]START\n", __FUNCTION__)
#define FUNC_END() \
printk(KERN_ERR "[%s]END\n", __FUNCTION__)
#else /* TRACE_THIS_MODULE */
#define ENTER()
#define FUNC_START()
#define FUNC_END()
#endif /* TRACE_THIS_MODULE */
#if ( MY_DEBUG == 1)
#define my_pr_info pr_err
#define my_pr_debug pr_err
#define my_pr_err pr_err
#define my_pr_warning pr_err
#else
#define my_pr_info pr_info
#define my_pr_debug pr_debug
#define my_pr_err pr_err
#define my_pr_warning pr_warning
#endif
//****************************************
// Global
//****************************************
// I2C Bus
static struct i2c_client *nfc_client = NULL;
static struct i2c_adapter *adapter = NULL;
// global structure for internal control
typedef struct _msr3110_dev
{
struct mutex mutex_irq_handler;
struct mutex mutex_ioctl_irq;
wait_queue_head_t irq_wait_queue;
int irq_flag;
} msr3110_dev;
msr3110_dev *g_msr3110_dev = NULL;
//For DMA
#define MAX_BUFFER_SIZE 255
static char *I2CDMAWriteBuf = NULL;
static unsigned int I2CDMAWriteBuf_pa;// = NULL;
static char *I2CDMAReadBuf = NULL;
static unsigned int I2CDMAReadBuf_pa;// = NULL;
#define NFC_CLIENT_I2C_BUS_LOG_OFF 1
#if ( NFC_CLIENT_I2C_BUS_LOG_OFF == 1)
#define SET_NFC_CLIENT_BUS_LOG() { my_pr_debug( "%s SET_NFC_CLIENT_BUS_LOG\n", __func__); nfc_client->addr |=I2C_A_FILTER_MSG;}
//0x4000 not I2C_A_FILTER_MSG.
//it's bus driver bug, comment by i2c owner, ranran
#else
#define SET_NFC_CLIENT_BUS_LOG()
#endif
static int nfc_probe(struct i2c_client *client, const struct i2c_device_id *id);
static int nfc_remove(struct i2c_client *client);
void msr3110_dev_irq_handler(void);
static int nfc_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
FUNC_START();
my_pr_debug( "%s clinet 0x%p id %s", __func__, client, id->name);
adapter = to_i2c_adapter( client->dev.parent);
my_pr_debug( "%s master tx 0x%p id %s", __func__, client->adapter->algo->master_xfer, id->name);
my_pr_debug( "%s master tx 0x%p id %s", __func__, adapter->algo->master_xfer, id->name);
nfc_client = client;
FUNC_END();
return 0;
}
static int nfc_remove(struct i2c_client *client)
{
FUNC_START();
nfc_client = NULL;
FUNC_END();
return 0;
}
#define NFC_I2C_BUSNUM 5
#define I2C_ID_NAME "msr3110"
static struct i2c_board_info __initdata nfc_board_info = { I2C_BOARD_INFO(I2C_ID_NAME, 0x59)};
static const struct i2c_device_id nfc_id[] = {
{ I2C_ID_NAME, 0x59, }, //Mstar MSR3110 NFC driver
{},
};
static struct i2c_driver nfc_driver = {
.driver = {
.name = I2C_ID_NAME,
},
.probe = nfc_probe,
.remove = nfc_remove,
.id_table = nfc_id,
};
// misc driver
static int msr3110_dev_open( struct inode *inode, struct file *filp)
{
FUNC_START()
FUNC_END();
return 0;
}
static int msr3110_dev_release( struct inode *inode, struct file *filp)
{
FUNC_START();
FUNC_END();
return 0;
}
#define MSR3110_DEV_WRITE_MAX 256
#define MSR3110_CMD_I2C_ID 0x51
#define NFC_CLIENT_TIMING_ON 1
#if ( NFC_CLIENT_TIMING_ON == 1)
#define NFC_CLIENT_TIMING 250
#define SET_NFC_CLIENT_TIMING() { nfc_client->timing = NFC_CLIENT_TIMING;}
#else
#define NFC_CLIENT_TIMING
#define SET_NFC_CLIENT_TIMING()
#endif
static ssize_t msr3110_dev_write(struct file *filp, const char __user *buf, size_t count, loff_t *offset)
{
ssize_t retVal = 0;
FUNC_START();
my_pr_debug( "%s count: %d \n", __func__, count);
if( count > MSR3110_DEV_WRITE_MAX)
{
count = MSR3110_DEV_WRITE_MAX;
}
if( copy_from_user( I2CDMAWriteBuf, buf, count))
{
my_pr_err( "%s fail to copy from user \n", __func__);
retVal = -EFAULT;
goto end;
}
nfc_client->addr = ( MSR3110_CMD_I2C_ID & I2C_MASK_FLAG);
SET_NFC_CLIENT_TIMING();
SET_NFC_CLIENT_BUS_LOG();
nfc_client->ext_flag |= I2C_DMA_FLAG;
nfc_client->ext_flag |= I2C_DIRECTION_FLAG;
retVal = i2c_master_send( nfc_client, (unsigned char *)I2CDMAWriteBuf_pa, count);
my_pr_debug( "%s rcv count: %d \n", __func__, retVal);
if( retVal < 0)
{
//my_pr_err( "%s i2c_master_send fail. retVal: %d ", __func__, retVal);
goto end;
}
if( retVal != count)
{
//my_pr_err( "%s i2c_master_send retVal != count. retVal: %d ", __func__, retVal);
retVal = -EIO;
goto end;
}
end:
FUNC_END();
return retVal;
}
#define MSR3110_DEV_READ_MAX 255
#define MSR3110_DEV_READ_RETRY_COUNT 50
#define MSR3110_DEV_READ_RETRY_DELAY 1
static ssize_t msr3110_dev_read(struct file *filp, char __user *buf, size_t count, loff_t *offset)
{
ssize_t retVal = 0;
unsigned char tmp[MSR3110_DEV_READ_MAX];
int loopIdx = 0;
int readByteCount = 0;;
FUNC_START();
my_pr_debug( "%s count: %d\n", __func__, count);
if( count > MSR3110_DEV_READ_MAX)
{
count = MSR3110_DEV_READ_MAX;
}
nfc_client->addr = ( MSR3110_CMD_I2C_ID & I2C_MASK_FLAG);
nfc_client->ext_flag |= I2C_DMA_FLAG;
nfc_client->ext_flag |= I2C_DIRECTION_FLAG;
SET_NFC_CLIENT_TIMING();
SET_NFC_CLIENT_BUS_LOG();
for( loopIdx = 0; loopIdx < MSR3110_DEV_READ_RETRY_COUNT; loopIdx++)
{
retVal = i2c_master_recv( nfc_client, (unsigned char *)I2CDMAReadBuf_pa, count);
my_pr_debug( "%s loopIdx: %d, recv retVal: %d \n", __func__, loopIdx, retVal);
if( retVal == count)
{
my_pr_debug( "%s SUCCESS: recv. \n", __func__);
break;
}
if( retVal < 0)
{
//my_pr_err( "%s FAIL: recv. CONTINUE. \n", __func__);
usleep_range( 900, 1000);
continue;
}
if( retVal > 2)
{
//my_pr_err( "%s FAIL: recv too many bytes. goto end. \n", __func__);
//retVal = -EIO;
retVal = 0;
goto end;
}
}
if( retVal < 0)
{
my_pr_err( "%s FAIL: recv. out of retry count, goto end. \n", __func__);
retVal = 0;
goto end;
}
readByteCount = retVal;
#if 0
for( loopIdx = 0; loopIdx < MSR3110_DEV_READ_RETRY_COUNT; loopIdx++)
{
msleep(MSR3110_DEV_READ_RETRY_DELAY);
retVal = i2c_master_recv( nfc_client, tmp, 2);
my_pr_info( "%s loopIdx: %d, recv retVal: %d \n", __func__, loopIdx, retVal);
if( retVal == 2)
{
my_pr_info( "%s SUCCESS: recv. \n", __func__);
break;
}
if( retVal < 0)
{
my_pr_err( "%s FAIL: recv. CONTINUE. \n", __func__);
continue;
}
if( retVal > 2)
{
my_pr_err( "%s FAIL: recv too many bytes. goto end. \n", __func__);
retVal = -EIO;
goto end;
}
}
if( retVal < 0)
{
my_pr_err( "%s FAIL: recv. out of retry count, goto end. \n", __func__);
goto end;
}
readByteCount = 2;
if( tmp[1] > 0)
{
for( loopIdx = 0; loopIdx < MSR3110_DEV_READ_RETRY_COUNT; loopIdx++)
{
msleep(1);
retVal = i2c_master_recv( nfc_client, &tmp[2], tmp[1] + 2);
my_pr_info( "%s loopIdx: %d, recv retVal: %d", __func__, loopIdx, retVal);
if( retVal == ( tmp[1] + 2))
{
my_pr_info( "%s SUCCESS: recv data.", __func__);
break;
}
if( retVal < 0)
{
my_pr_err( "%s FAIL: recv. CONTINUE.", __func__);
continue;
}
if( retVal > ( tmp[1] + 2))
{
my_pr_err( "%s FAIL: recv too many bytes. goto end.", __func__);
retVal = -EIO;
goto end;
}
}
}
if( retVal < 0)
{
my_pr_err( "%s FAIL: recv. out of retry count, goto end.", __func__);
goto end;
}
readByteCount += tmp[1] + 2;
retVal = readByteCount;
#endif
if ( copy_to_user( buf, I2CDMAReadBuf, readByteCount)) {
my_pr_err("%s : failed to copy to user space\n", __func__);
retVal = 0;
goto end;
}
end:
//for( loopIdx = 0; loopIdx < readByteCount; loopIdx++)
//{
// my_pr_debug( "%s %02X", __func__, tmp[loopIdx]);
//}
FUNC_END();
return retVal;
}
#define MSR3110_DEV_MAGIC_ID 0xCD
#define MSR3110_IOCTL_FW_UPGRADE _IOW( MSR3110_DEV_MAGIC_ID, 0x00, int)
#define MSR3110_IOCTL_SET_VEN _IOW( MSR3110_DEV_MAGIC_ID, 0x01, int)
#define MSR3110_IOCTL_SET_RST _IOW( MSR3110_DEV_MAGIC_ID, 0x02, int)
#define MSR3110_IOCTL_IRQ _IOW( MSR3110_DEV_MAGIC_ID, 0x03, int)
#define MSR3110_IOCTL_IRQ_ABORT _IOW( MSR3110_DEV_MAGIC_ID, 0x04, int)
#define MSR3110_IOCTL_IRQ_REG _IOW( MSR3110_DEV_MAGIC_ID, 0x05, int)
#define MSR3110_IOCTL_ISP_READ_REG _IOW( MSR3110_DEV_MAGIC_ID, 0xA1, int)
#define MSR3110_IOCTL_ISP_WRITE_REG _IOW( MSR3110_DEV_MAGIC_ID, 0xA2, int)
#define MSR3110_IOCTL_SET_IRQ_NFC_PIN _IOW( MSR3110_DEV_MAGIC_ID, 0xA3, int)
#define MSR3110_IOCTL_SET_NFC_EINT_PIN _IOW( MSR3110_DEV_MAGIC_ID, 0xA4, int)
typedef struct _msr3110fw_upgrade_info
{
unsigned long FwBufLen;
void* FwBuf;
} msr3110fw_upgrade_info;
#define MSR3110_ISP_SLAVE_I2C_ID 0x59
//#define MSR3110_ISP_FLASH_I2C_ID 0x49
#define MSR3110_ISP_FLASH_I2C_ID 0x79
#define MSR3110_ISP_FLASH_I2C_ID_SHIFT 0xF2
#define Antares_SLAVE_ADDR MSR3110_ISP_SLAVE_I2C_ID
#define I2CtoSPIFlash_SLAVE_ADDR MSR3110_ISP_FLASH_I2C_ID
#define SERIALFLASH_CMD_DATA_WRITE 0x10
#define SERIALFLASH_CMD_DATA_READ 0x11
#define SERIALFLASH_CMD_DATA_END 0x12
#define SERIALFLASH_CMD_RESET_ISP 0x24
//#define FLASH_START 0x00010000
#define FLASH_START 0x00000000
#define FLASH_END 0x00020000
enum
{
FLASH_UNKNOWN,
FLASH_SST,
FLASH_MX
};
static int nfc_i2c_writebytes(u8 addr, u32 len, u8 *buf)
{
int retVal;
memcpy( I2CDMAWriteBuf, buf, len);
nfc_client->addr = ( addr & I2C_MASK_FLAG);
SET_NFC_CLIENT_TIMING();
SET_NFC_CLIENT_BUS_LOG();
nfc_client->ext_flag |= I2C_DMA_FLAG;
nfc_client->ext_flag |= I2C_DIRECTION_FLAG;
retVal = i2c_master_send( nfc_client, (unsigned char *)I2CDMAWriteBuf_pa, len);
if( retVal != len)
{
my_pr_err( "%s i2c_master_send fail\n", __func__);
retVal = -EIO;
goto end;
}
end:
return retVal;
}
static void nfc_i2c_readbytes(u8 addr, u32 wlen, u32 rlen, u8 *wbuf, u8 *rbuf)
{
int retVal;
struct i2c_msg msgs[] = {
{
.addr = addr,
.flags = 0,
.len = wlen,
.buf = wbuf,
},
{
.addr = addr,
.flags = I2C_M_RD,
.len = rlen,
.buf = rbuf,
}
};
retVal = i2c_transfer( nfc_client->adapter, msgs, 2);
if( retVal <= 0)
{
my_pr_err( "%s ERROR: i2c_transfer, retVal: %d", __func__, retVal);
}
}
static void SerialFlash_EnterSerialDebug(void)
{
u8 cmd[5] = {0x53, 0x45, 0x52, 0x44, 0x42};
FUNC_START();
nfc_i2c_writebytes(Antares_SLAVE_ADDR, sizeof(cmd), cmd);
FUNC_END();
}
static void SerialFlash_EnterSingleStep(void)
{
u8 cmd1[4] = {SERIALFLASH_CMD_DATA_WRITE, 0xC0, 0xC1, 0x53};
u8 cmd2[4] = {SERIALFLASH_CMD_DATA_WRITE, 0x1F, 0xC1, 0x53};
FUNC_START();
nfc_i2c_writebytes(Antares_SLAVE_ADDR, sizeof(cmd1), cmd1);
nfc_i2c_writebytes(Antares_SLAVE_ADDR, sizeof(cmd2), cmd2);
FUNC_END();
}
static void SerialFlash_ExitSingleStep(void)
{
u8 cmd1[4] = {SERIALFLASH_CMD_DATA_WRITE, 0xC0, 0xC1, 0xFF};
u8 cmd2[4] = {SERIALFLASH_CMD_DATA_WRITE, 0x1F, 0xC1, 0xFF};
FUNC_START();
nfc_i2c_writebytes(Antares_SLAVE_ADDR, sizeof(cmd1), cmd1);
nfc_i2c_writebytes(Antares_SLAVE_ADDR, sizeof(cmd2), cmd2);
FUNC_END();
}
static void SerialFlash_ExitSerialDebug(void)
{
u8 cmd[1] = {0x45};
FUNC_START();
nfc_i2c_writebytes(Antares_SLAVE_ADDR, sizeof(cmd), cmd);
FUNC_END();
}
u8 SerialFlash_DisableWDT(void)
{
u8 cmd1[4] = {SERIALFLASH_CMD_DATA_WRITE, 0x09, 0x08, 0x55};
u8 cmd2[4] = {SERIALFLASH_CMD_DATA_WRITE, 0x09, 0x09, 0xAA};
u8 result = false;
u8 retry = 0;
u8 res1 = 0;
u8 res2 = 0;
FUNC_START();
while(1)
{
SerialFlash_EnterSerialDebug();
SerialFlash_EnterSingleStep();
nfc_i2c_writebytes(Antares_SLAVE_ADDR, sizeof(cmd1), cmd1);
nfc_i2c_writebytes(Antares_SLAVE_ADDR, sizeof(cmd2), cmd2);
nfc_i2c_readbytes(Antares_SLAVE_ADDR, 3, 1, cmd1, &res1);
nfc_i2c_readbytes(Antares_SLAVE_ADDR, 3, 1, cmd2, &res2);
SerialFlash_ExitSingleStep();
SerialFlash_ExitSerialDebug();
if (res1 == cmd1[3] && res2 == cmd2[3])
{
result = true;
break;
}
else if (++retry == 10)
{
result = false;
break;
}
break;
}
FUNC_END();
return result;
}
u8 SerialFlash_Chg_Flash_I2C_Addrs(void)
{
u8 cmd1[4] = {SERIALFLASH_CMD_DATA_WRITE, 0x08, 0x00, MSR3110_ISP_FLASH_I2C_ID_SHIFT};
u8 result = false;
u8 retry = 0;
u8 res1 = 0;
FUNC_START();
while(1)
{
SerialFlash_EnterSerialDebug();
SerialFlash_EnterSingleStep();
nfc_i2c_writebytes(Antares_SLAVE_ADDR, sizeof(cmd1), cmd1);
nfc_i2c_readbytes(Antares_SLAVE_ADDR, 3, 1, cmd1, &res1);
SerialFlash_ExitSingleStep();
SerialFlash_ExitSerialDebug();
if ( res1 == cmd1[3])
{
result = true;
break;
}
else if (++retry == 10)
{
result = false;
break;
}
break;
}
FUNC_END();
return result;
}
static void SerialFlash_EntryIspMode(void)
{
u8 cmd[5] = {0x4D, 0x53, 0x54, 0x41, 0x52};
FUNC_START();
nfc_i2c_writebytes(I2CtoSPIFlash_SLAVE_ADDR, sizeof(cmd), cmd);
FUNC_END();
}
static void SerialFlash_ExitIspMode(void)
{
u8 cmd[1] = {0x24};
FUNC_START();
nfc_i2c_writebytes(I2CtoSPIFlash_SLAVE_ADDR, sizeof(cmd), cmd);
FUNC_END();
}
static void SerialFlash_ReadChipID(u8* id)
{
u8 cmd1[2] = {SERIALFLASH_CMD_DATA_WRITE, 0x9F};
u8 cmd2[1] = {SERIALFLASH_CMD_DATA_READ};
u8 cmd3[1] = {SERIALFLASH_CMD_DATA_END};
FUNC_START();
nfc_i2c_writebytes(I2CtoSPIFlash_SLAVE_ADDR, sizeof(cmd1), cmd1);
nfc_i2c_readbytes(I2CtoSPIFlash_SLAVE_ADDR, 1, 3, cmd2, id);
nfc_i2c_writebytes(I2CtoSPIFlash_SLAVE_ADDR, sizeof(cmd3), cmd3);
FUNC_END();
}
static u8 SerialFlash_ReadStatus(void)
{
u8 cmd1[2] = {SERIALFLASH_CMD_DATA_WRITE, 0x05};
u8 cmd2[1] = {SERIALFLASH_CMD_DATA_READ};
u8 cmd3[1] = {SERIALFLASH_CMD_DATA_END};
u8 status = 0;
nfc_i2c_writebytes(I2CtoSPIFlash_SLAVE_ADDR, sizeof(cmd1), cmd1);
nfc_i2c_readbytes(I2CtoSPIFlash_SLAVE_ADDR, 1, 1, cmd2, &status);
nfc_i2c_writebytes(I2CtoSPIFlash_SLAVE_ADDR, sizeof(cmd3), cmd3);
return status;
}
static void SerialFlash_WriteEnable(void)
{
u8 cmd1[2] = {SERIALFLASH_CMD_DATA_WRITE, 0x06};
u8 cmd2[1] = {SERIALFLASH_CMD_DATA_END};
nfc_i2c_writebytes(I2CtoSPIFlash_SLAVE_ADDR, sizeof(cmd1), cmd1);
nfc_i2c_writebytes(I2CtoSPIFlash_SLAVE_ADDR, sizeof(cmd2), cmd2);
}
static void SerialFlash_WriteDisable(void)
{
u8 cmd1[2] = {SERIALFLASH_CMD_DATA_WRITE, 0x04};
u8 cmd2[1] = {SERIALFLASH_CMD_DATA_END};
nfc_i2c_writebytes(I2CtoSPIFlash_SLAVE_ADDR, sizeof(cmd1), cmd1);
nfc_i2c_writebytes(I2CtoSPIFlash_SLAVE_ADDR, sizeof(cmd2), cmd2);
}
static void SerialFlash_WriteStatus(u8 flash_type, u8 status)
{
u8 cmd1[2] = {SERIALFLASH_CMD_DATA_WRITE, 0x50};
u8 cmd2[3] = {SERIALFLASH_CMD_DATA_WRITE, 0x01, 0x00};
u8 cmd3[1] = {SERIALFLASH_CMD_DATA_END};
SerialFlash_WriteEnable();
if (flash_type == FLASH_SST)
{
nfc_i2c_writebytes(I2CtoSPIFlash_SLAVE_ADDR, sizeof(cmd1), cmd1);
nfc_i2c_writebytes(I2CtoSPIFlash_SLAVE_ADDR, sizeof(cmd3), cmd3);
}
cmd2[2] = status;
nfc_i2c_writebytes(I2CtoSPIFlash_SLAVE_ADDR, sizeof(cmd2), cmd2);
nfc_i2c_writebytes(I2CtoSPIFlash_SLAVE_ADDR, sizeof(cmd3), cmd3);
SerialFlash_WriteDisable();
}
static void SerialFlash_ChipErase(void)
{
u8 cmd1[2] = {SERIALFLASH_CMD_DATA_WRITE, 0x60};
u8 cmd2[1] = {SERIALFLASH_CMD_DATA_END};
FUNC_START();
SerialFlash_WriteEnable();
nfc_i2c_writebytes(I2CtoSPIFlash_SLAVE_ADDR, sizeof(cmd1), cmd1);
nfc_i2c_writebytes(I2CtoSPIFlash_SLAVE_ADDR, sizeof(cmd2), cmd2);
FUNC_END();
}
static void SerialFlash_WriteData(u8 flash_type, u32 addr, u32 wsize, u8 *data)
{
u8 cmd1[7] = {SERIALFLASH_CMD_DATA_WRITE, 0xAD, 0x00, 0x00, 0x00, 0x00, 0x00};
u8 cmd2[4] = {SERIALFLASH_CMD_DATA_WRITE, 0xAD, 0x00, 0x00};
u8 cmd3[1] = {SERIALFLASH_CMD_DATA_END};
//u8 cmd4[6] = {I2CtoSPIFlash_SLAVE_ADDR, SERIALFLASH_CMD_DATA_WRITE, 0x02, 0x00, 0x00, 0x00};
u32 wcnt = 0;
SerialFlash_WriteEnable();
if (flash_type == FLASH_SST)
{
cmd1[2] = addr>>16;
cmd1[3] = addr>>8;
cmd1[4] = addr;
cmd1[5] = data[wcnt++];
cmd1[6] = data[wcnt];
nfc_i2c_writebytes(I2CtoSPIFlash_SLAVE_ADDR, sizeof(cmd1), cmd1);
nfc_i2c_writebytes(I2CtoSPIFlash_SLAVE_ADDR, sizeof(cmd3), cmd3);
for (wcnt=2; wcnt<wsize; wcnt++)
{
cmd2[2] = data[wcnt++];
cmd2[3] = data[wcnt];
nfc_i2c_writebytes(I2CtoSPIFlash_SLAVE_ADDR, sizeof(cmd2), cmd2);
nfc_i2c_writebytes(I2CtoSPIFlash_SLAVE_ADDR, sizeof(cmd3), cmd3);
}
}
#if 0
else if (flash_type == FLASH_MX)
{
u8 *cmd = kmalloc(wsize+5, GFP_KERNEL);
if(cmd)
{
cmd[0] = SERIALFLASH_CMD_DATA_WRITE;
cmd[1] = 0x02;
cmd[2] = addr>>16;
cmd[3] = addr>>8;
cmd[4] = addr;
memcpy(&cmd[5], data, wcnt);
nfc_i2c_writebytes(I2CtoSPIFlash_SLAVE_ADDR, sizeof(cmd), cmd);
kfree(cmd);
}
else
{
my_pr_err("[nfc-control]: *cmd malloc fail");
}
}
#endif
else
{
my_pr_err("[nfc-control]: uknown flash type %d", flash_type);
}
SerialFlash_WriteDisable();
}
static void SerialFlash_ReadData(u32 addr, u32 rsize, u8 *data)
{
u8 cmd1[5] = {SERIALFLASH_CMD_DATA_WRITE, 0x03, 0x00, 0x00, 0x00};
u8 cmd2[1] = {SERIALFLASH_CMD_DATA_READ};
u8 cmd3[1] = {SERIALFLASH_CMD_DATA_END};
cmd1[2] = addr >> 16;
cmd1[3] = addr >> 8;
cmd1[4] = addr;
nfc_i2c_writebytes( I2CtoSPIFlash_SLAVE_ADDR, sizeof(cmd1), cmd1);
nfc_i2c_readbytes( I2CtoSPIFlash_SLAVE_ADDR, 1, rsize, cmd2, data);
nfc_i2c_writebytes(I2CtoSPIFlash_SLAVE_ADDR, sizeof(cmd3), cmd3);
}
static u8 SerialFlash_UpgradeA3(u8 *bufptr, u32 len)
{
u8 id[3] = {0x00, 0x00, 0x00};
u8 flash_type = FLASH_UNKNOWN;
u8 rstatus = 0;
u8 error = 0;
u8 waitcnt = 0;
u8 wstatus = 0;
u8 *rdata = NULL;
u32 offset = 0;
int i;
FUNC_START();
rdata = kmalloc(4096, GFP_KERNEL);
if(rdata == NULL)
{
my_pr_err("%s Malloc Failed", __func__);
error = 1;
goto LAST_END;
}
// Init
rstatus = SerialFlash_DisableWDT();
if (rstatus == 0)
{
my_pr_err("%s Disable WDT Failed", __func__);
error = 1;
goto LAST_END;
}
my_pr_debug("%s Disable WDT Success", __func__);
rstatus = SerialFlash_Chg_Flash_I2C_Addrs();
if (rstatus == 0)
{
my_pr_err("%s FAIL: SerialFlash_Chg_Flash_I2C_Addrs", __FUNCTION__);
error = 1;
goto LAST_END;
}
my_pr_debug("%s Success: SerialFlash_Chg_Flash_I2C_Addrs", __FUNCTION__);
SerialFlash_EntryIspMode();
SerialFlash_ReadChipID(id);
my_pr_debug( "%s ID 0x%02X 0x%02X 0x%02X", __FUNCTION__, id[0], id[1], id[2]);
if (id[0] == 0xBF && id[1] == 0x25 && id[2] == 0x02)
{
flash_type = FLASH_SST;
my_pr_debug("%s SST Flash", __FUNCTION__);
}
else if (id[0] == 0xC2 && id[1] == 0x20 && id[2] == 0x11)
{
flash_type = FLASH_MX;
my_pr_debug("%s MXIC Flash", __FUNCTION__);
}
rstatus = SerialFlash_ReadStatus();
my_pr_debug("%s Flash Status = 0x%02X", __FUNCTION__, rstatus);
SerialFlash_WriteStatus(flash_type, 0x00);
// Erase
error = 1;
SerialFlash_ChipErase();
//wait for Erase done
for (waitcnt = 0; waitcnt<1000; waitcnt++)
{
udelay(50);
wstatus = SerialFlash_ReadStatus();
if (wstatus == 0x00)
{
error = 0;
break;
}
}
if (error)
{
my_pr_err("%s Erase Timeout", __FUNCTION__);
goto LAST_END;
}
my_pr_debug("%s Erase Done %d", __FUNCTION__, waitcnt);
// Write
my_pr_debug("%s FLASH_START: %ul", __FUNCTION__, FLASH_START);
for (offset=FLASH_START; offset<FLASH_END; offset+=256)
{
if( ( offset % 0x1000 ) == 0 )
{
my_pr_debug("%s Write Address = 0x%04X from 0x%04X", __FUNCTION__, offset, (unsigned int)(bufptr+offset));
}
//printk(KERN_DEBUG "data 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X", rdata[0], rdata[1],rdata[2],rdata[3],rdata[4],rdata[5],rdata[6],rdata[7]);
SerialFlash_WriteData(FLASH_SST, offset, 256, bufptr+offset);
error = 1;
for (waitcnt = 0; waitcnt<10000; waitcnt++)
{
udelay(100);
wstatus = SerialFlash_ReadStatus();
if( wstatus == 0x00 )
{
error = 0;
break;
}
}
if( error )
{
my_pr_err("%s Write Timeout", __func__);
goto LAST_END;
}
}
//SerialFlash_ReadData( 0x00000000, 255, rdata);
//SerialFlash_ReadData( 0x00010000, 128, rdata);
#if 0
// Verify
for (offset=FLASH_START; offset<FLASH_END; offset+=4096)
{
int i;
if( ( (offset) % 0x1000 ) == 0 )
{
my_pr_info("%s Read Address = 0x%08X", __FUNCTION__, offset);
}
SerialFlash_ReadData(offset, 4096, rdata);
udelay(1);
error = 0;
for (i = 0; i<4096; i++)
{
if( rdata[i] != bufptr[offset+i] )
{
error = 1;
break;
}
}
if (error)
{
my_pr_err("%s Verify Error at addr:0x%X", __FUNCTION__, offset+i );
my_pr_err("%s: WData = 0x%02X", __FUNCTION__, bufptr[offset+i] );
my_pr_err("%s RData = 0x%02X", __FUNCTION__, rdata[i] );
goto LAST_END;
}
}
my_pr_info( "%s Verify OK", __FUNCTION__);
#endif
#if 1
// Verify
for ( offset = FLASH_START; offset < FLASH_END; offset += 128)
{
//if( ( (offset) % 0x0010 ) == 0 )
//{
// my_pr_info("%s Read Address = 0x%08X", __FUNCTION__, offset);
//}
SerialFlash_ReadData( offset, 128, rdata);
udelay(1);
error = 0;
for ( i = 0; i < 128; i++)
{
if( rdata[i] != bufptr[offset+i] )
{
error = 1;
break;
}
}
if (error)
{
my_pr_err("%s Verify Error at addr:0x%X", __FUNCTION__, offset+i );
my_pr_err("%s: WData = 0x%02X", __FUNCTION__, bufptr[offset+i] );
my_pr_err("%s RData = 0x%02X", __FUNCTION__, rdata[i] );
goto LAST_END;
}
}
my_pr_debug( "%s Verify OK", __FUNCTION__);
#endif
LAST_END:
if(rdata)
{
kfree(rdata);
}
SerialFlash_ExitIspMode();
FUNC_END();
return error;
}
typedef struct _msr3110_isp_info
{
unsigned char isp_addrs;
unsigned char reg_pos_1;
unsigned char reg_pos_2;
unsigned char write_val;
unsigned char reg_val;