-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathnor_cmd.c
2846 lines (2504 loc) · 87.3 KB
/
nor_cmd.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
/******************************************************************************
*
* @file nor_cmd.c
*
*
* Ver Who Date Changes
* 1.00 AZ 02/23/17 First release
* 1.01 RY 02/27/18 Divide all code into 4 blocks.
******************************************************************************/
#include "nor_cmd.h"
/*
* Function: IsFlashBusy
* Arguments: Mxic, pointer to an mxchip structure of nor flash device.
* Return Value: MXST_FAILURE.
* MXST_DEVICE_BUSY.
* MXST_DEVICE_READY.
* Description: This function is used for checking status register WIP bit.
* If WIP bit = 1: return MXST_DEVICE_BUSY ( Busy )
* = 0: return MXST_DEVICE_READY ( Ready ).
*/
int MxIsFlashBusy(MxChip *Mxic)
{
int Status;
u8 Sr;
Status = MxRDSR(Mxic, &Sr);
if (Status != MXST_SUCCESS)
return Status;
if(Sr & SR_WIP)
return MXST_DEVICE_BUSY;
else
return MXST_DEVICE_READY;
}
/*
* Function: MxWaitForFlashReady
* Arguments: Mxic, pointer to an mxchip structure of nor flash device.
* ExpectTime, expected time-out value of flash operations.
* Return Value: MXST_SUCCESS.
* MXST_FAILURE.
* MXST_TIMEOUT.
* Description: This function is used for waiting for flash ready.
* If flash is ready return MXST_SUCCESS. If flash is time-out return MXST_TIMEOUT.
*/
int MxWaitForFlashReady(MxChip *Mxic,u32 ExpectTime)
{
MxTime tStart, tCur;
u32 tUsed;
MxGetTime(tStart);
while(MxIsFlashBusy(Mxic) == MXST_DEVICE_BUSY)
{
MxGetTime(tCur);
tUsed = (tCur-tStart)*1000000/COUNTS_PER_SECOND;
if(tUsed > ExpectTime*3)
{
Mx_printf("\t@Warning:MXST_TIMEOUT!!!!!!!!!! >>> \r\n");
return MXST_TIMEOUT;
}
}
return MXST_SUCCESS;
}
/*
* Function: MxWaitRYBYReady
* Arguments: Mxic, pointer to an mxchip structure of nor flash device.
* ExpectTime, expected time-out value of flash operations.
* Return Value: MXST_SUCCESS.
* MXST_TIMEOUT.
* Description: This function is used for waiting for RYBY pin ready.
* If RYBY pin is high return MXST_SUCCESS.
*/
int MxWaitRYBYReady(MxChip *Mxic,u32 ExpectTime)
{
MxTime tStart, tCur;
u32 tUsed;
MxGetTime(tStart);
//while(SO == 0)
{
MxGetTime(tCur);
tUsed = (tCur-tStart) * 1000000000 / COUNTS_PER_SECOND;
if(tUsed > ExpectTime)
return MXST_TIMEOUT;
}
return MXST_SUCCESS;
}
/*
* Function: MxSetDummyLen
* Arguments: Mxic, pointer to an mxchip structure of nor flash device.
* Cmd, the operation BLOCK.
* Return Value: MXST_SUCCESS.
* MXST_FAILURE.
* MXST_TIMEOUT.
* Description: This function is for setting the dummy cycle of read command.
*/
static int MxSetDummyLen(MxChip *Mxic, u8 Cmd)
{
MxSpi *Spi = Mxic->Priv;
u8 CmdNum,ModeNum ;
u8 CmdDmy[10][3] ={
/* SPI QPI OPI */
{ 24, 6, 0xFF}, /* MX_CMD_RES */
// { 16, 0xFF, 0xFF}, /* MX_CMD_REMS*/
{ 0, 0xFF, 0xFF}, /* MX_CMD_REMS */
{ 8, 0xFF, 0xFF}, /* MX_CMD_FREAD */
{ 4, 0xFF, 0xFF}, /* MX_CMD_W4READ */
{ 8, 6, 0xFF}, /* MX_CMD_CFIRD */
{ 8, 8, 20}, /* MX_CMD_RDSFDP */
{(Mxic->ChipSpclFlag & RDPASS_ADDR)?8: 0, 0xFF, 20}, /* MX_CMD_RDPASS */
{ 0, 0, 4}, /* MX_CMD_RDID */
{ 0, 0, 0},
{ 6, 0, 0}
};
switch (Cmd) {
case MX_CMD_RES: CmdNum = 0; break;
case MX_CMD_REMS:
case MX_CMD_REMS2:
case MX_CMD_REMS4:
case MX_CMD_REMS4D: CmdNum = 1; break;
case MX_CMD_FREAD:
case MX_CMD_RDDMC:
case MX_CMD_RDBLOCK2:
case MX_CMD_RDPLOCK: CmdNum = 2; break;
case MX_CMD_W4READ: CmdNum = 3; break;
case MX_CMD_CFIRD: CmdNum = 4; break;
case MX_CMD_RDSFDP: CmdNum = 5; break;
case MX_CMD_RDPASS: CmdNum = 6; break;
case MX_CMD_RDSR:
case MX_CMD_RDCR:
case MX_CMD_RDCR2:
case MX_CMD_RDFBR:
case MX_CMD_RDSCUR:
case MX_CMD_RDLR:
case MX_CMD_RDID: CmdNum = 7; break;
case MX_CMD_FASTREAD:
case MX_CMD_FASTREAD4B:
case MX_CMD_4READ_BOTTOM:
case MX_CMD_2READ:
case MX_CMD_2READ4B:
case MX_CMD_DREAD:
case MX_CMD_DREAD4B:
case MX_CMD_QREAD:
case MX_CMD_QREAD4B:
case MX_CMD_FASTDTRD:
case MX_CMD_FASTDTRD4B:
case MX_CMD_2DTRD:
case MX_CMD_2DTRD4B:
case MX_CMD_4DTRD:
case MX_CMD_RDBUF:
//case MX_CMD_4DTRD4B:
Spi->LenDummy = (Spi->PreambleEn) ? 0 : Mxic->RdDummy[Spi->FlashProtocol/2].Dummy;
return MXST_SUCCESS;
case MX_CMD_8READ: /* the command BLOCK of 4READ4B and 8READ is the same */
case MX_CMD_8DTRD: /* the command BLOCK of 4DTRD4B and 8DTRD is the same */
Spi->LenDummy = (Spi->PreambleEn) ? 4 : Mxic->RdDummy[Spi->FlashProtocol/2].Dummy;
return MXST_SUCCESS;
case MX_CMD_RDSPB:
case MX_CMD_RDDPB:
if(Spi->CurMode | MODE_OPI )
{
Spi->LenDummy = Mxic->RdDummy[Spi->FlashProtocol/2].Dummy;
return MXST_SUCCESS;
}
else{
CmdNum = 8; break;
}
default: CmdNum = 8; break;
}
switch (Spi->CurMode) {
case MODE_DOPI:
case MODE_SOPI:
ModeNum = 2; break;
case MODE_QPI:
ModeNum = 1; break;
default:
ModeNum = 0; break;
}
Spi->LenDummy = CmdDmy[CmdNum][ModeNum];
return MXST_SUCCESS;
}
/*
* Function: MxSetAddrLen
* Arguments: Mxic, pointer to an mxchip structure of nor flash device.
* Cmd, the operation BLOCK.
* Return Value: MXST_SUCCESS.
* Description: This function is for setting the device address length of command.
*/
static int MxSetAddrLen(MxChip *Mxic, u8 Cmd)
{
MxSpi *Spi = Mxic->Priv;
switch (Cmd) {
case MX_CMD_READ:
case MX_CMD_FASTREAD:
case MX_CMD_PP:
case MX_CMD_4PP:
case MX_CMD_SE:
case MX_CMD_BE:
case MX_CMD_BE32K:
case MX_CMD_2READ:
case MX_CMD_DREAD:
case MX_CMD_4READ_BOTTOM:
case MX_CMD_4READ_TOP:
case MX_CMD_QREAD:
case MX_CMD_FASTDTRD:
case MX_CMD_2DTRD:
case MX_CMD_4DTRD:
if(Mxic->ChipSpclFlag & ADDR_3BYTE_ONLY)
Spi->LenAddr = 3;
else if(Mxic->ChipSpclFlag & ADDR_4BYTE_ONLY)
Spi->LenAddr = 4;
else
Spi->LenAddr = (Mxic->SPICmdList[MX_MS_RST_SECU_SUSP] & MX_4B) ? ((Spi->CurAddrMode==SELECT_3B) ? 3:4) : 3;
break;
/* 4B mode */
case MX_CMD_READ4B:
case MX_CMD_FASTREAD4B:
case MX_CMD_FASTDTRD4B:
case MX_CMD_2DTRD4B:
//case MX_CMD_4DTRD4B: /* the command BLOCK of 4DTRD4B and MX_CMD_8DTRD is the same */
case MX_CMD_PP4B:
case MX_CMD_4PP4B:
case MX_CMD_SE4B:
case MX_CMD_BE4B:
case MX_CMD_BE32K4B:
case MX_CMD_2READ4B:
case MX_CMD_4READ4B: /* the command BLOCK of 4READ4B and 8READ is the same */
case MX_CMD_DREAD4B:
case MX_CMD_QREAD4B:
//case MX_CMD_8READ:
case MX_CMD_8DTRD:
case MX_CMD_RDCR2:
case MX_CMD_WRCR2:
case MX_CMD_WRSPB:
case MX_CMD_WRDPB:
case MX_CMD_RDSPB:
case MX_CMD_RDDPB:
case MX_CMD_RDBUF:
case MX_CMD_WRBI:
case MX_CMD_WRCT:
Spi->LenAddr = 4;
break;
case MX_CMD_RDPASS:
case MX_CMD_WRPASS:
case MX_CMD_PASSULK:
Spi->LenAddr = (Mxic->ChipSpclFlag & RDPASS_ADDR)? 4 : 0;
break;
case MX_CMD_RDSFDP:
Spi->LenAddr = (Spi->CurMode & MODE_OPI) ? 4 : 3;
break;
/* OPI: 4B mode, SPI: 0B mode */
case MX_CMD_RDID:
case MX_CMD_RDSR:
case MX_CMD_RDCR:
case MX_CMD_RDFBR:
case MX_CMD_RDSCUR:
case MX_CMD_RDLR:
case MX_CMD_SBL:
case MX_CMD_WRSR:
//case MX_CMD_WRCR: /* command BLOCK is same as MX_CMD_WRSR */
case MX_CMD_WRFBR:
case MX_CMD_WRLR:
Spi->LenAddr = (Spi->CurMode & MODE_OPI) ? 4 : 0;
break;
case MX_CMD_REMS:
case MX_CMD_REMS2:
case MX_CMD_REMS4:
case MX_CMD_REMS4D:
case MX_CMD_CP:
case MX_CMD_SBLK:
case MX_CMD_SBULK:
Spi->LenAddr = 3;
break;
default:
Spi->LenAddr = 0;
break;
}
return MXST_SUCCESS;
}
/*
* Function: MxSetAddrDmyMode
* Arguments: Mxic, pointer to an mxchip structure of nor flash device.
* Cmd, the command BLOCK.
* Return Value: MXST_SUCCESS.
* MXST_FAILURE.
* Description: This function is for setting the device address length and dummy cycle of command.
* It calls MxSetAddrLen and MxSetDummyLen.
*/
static int MxSetAddrDmyMode(MxChip *Mxic, u8 Cmd)
{
int Status;
Status = MxSetDummyLen(Mxic, Cmd);
if (Status != MXST_SUCCESS)
return Status;
Status = MxSetAddrLen(Mxic, Cmd);
if (Status != MXST_SUCCESS)
return Status;
return MXST_SUCCESS;
}
/*********************�z�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�{************************
**********************�x Template for R/W/E �x************************
**********************�|�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�w�}***********************/
/*
* Function: MxReadTemplate
* Arguments: Mxic: pointer to an mxchip structure of nor flash device.
* Addr: device address to read.
* ByteCount: number of bytes to read.
* Buf: pointer to a data buffer where the read data will be stored.
* Cmd: read commands (Single / Fast / Dual / Quad / OCTA) to issue.
* Return Value: MXST_SUCCESS.
* MXST_FAILURE.
* Description: This function issues the Read commands (Single / Fast / Dual / Quad / OCTA) to SPI Flash and reads data from the array.
* It is called by different Read commands functions like MxREAD, Mx8READ and etc.
*/
inline static int MxReadTemplate(MxChip *Mxic, u32 Addr, u32 ByteCount, u8 *Buf, u8 Cmd)
{
int Status;
MxSpi *Spi = Mxic->Priv;
u8 TmpFlashProtocol = Spi->FlashProtocol;
// Mx_printf(" Cmd: %02X\r\n", Cmd);
if((Spi->CurMode != MODE_SOPI) && (Spi->CurMode != MODE_DOPI))
{
switch (Cmd)
{
case MX_CMD_FASTDTRD:
case MX_CMD_FASTDTRD4B:
Spi->FlashProtocol = PROT_1_1D_1D;
break;
case MX_CMD_DREAD:
case MX_CMD_DREAD4B:
Spi->FlashProtocol = PROT_1_1_2;
break;
case MX_CMD_2READ:
case MX_CMD_2READ4B:
Spi->FlashProtocol = PROT_1_2_2;
break;
case MX_CMD_2DTRD:
case MX_CMD_2DTRD4B:
Spi->FlashProtocol = PROT_1_2D_2D;
break;
case MX_CMD_QREAD:
case MX_CMD_QREAD4B:
Spi->FlashProtocol = PROT_1_1_4;
break;
case MX_CMD_4READ_BOTTOM:
case MX_CMD_4READ_TOP:
case MX_CMD_4READ4B:
Spi->FlashProtocol = (Spi->CurMode & MODE_QPI)?PROT_4_4_4 : PROT_1_4_4;
break;
case MX_CMD_4DTRD:
case MX_CMD_4DTRD4B:
Spi->FlashProtocol = (Spi->CurMode & MODE_QPI)? PROT_4_4D_4D : PROT_1_4D_4D;
break;
default:
//Spi->FlashProtocol = PROT_1_1_1;
break;
}
}
Status = MxSetAddrDmyMode(Mxic, Cmd);
if (Status != MXST_SUCCESS)
return Status;
Status = MxSpiFlashRead(Mxic->Priv, Addr, ByteCount, Buf, Cmd);
Spi->FlashProtocol = TmpFlashProtocol;
return Status;
}
/*
* Function: MxWriteTemplate
* Arguments: Mxic: pointer to an mxchip structure of nor flash device.
* Addr: device address to program.
* ByteCount: number of bytes to program.
* Buf: pointer to a data buffer where the program data will be stored.
* Cmd: write commands (PP/PP4B/4PP/4PP4B) to issue.
* Return Value: MXST_SUCCESS.
* MXST_FAILURE.
* MXST_TIMEOUT.
* Description: This function programs location to the specified data.
* If the data size to program is larger than PAGE_SIZE, this function will execute page programming operation.
* If the page boundary is encountered during page programming,
* additional bytes are wrapped around to the start of the same page.
* It is called by different write commands functions like MxPP, MxPP4B and etc.
*/
inline static int MxWriteTemplate(MxChip *Mxic, u32 Addr, u32 ByteCount, u8 *Buf, u8 Cmd)
{
int Status;
int n;
u32 PageOfs, WrSz;
MxSpi *Spi = Mxic->Priv;
u8 TmpFlashProtocol = Spi->FlashProtocol;
u8 TmpHardwareMode = Spi->HardwareMode;
// Mx_printf("P Cmd: %02X\r\n", Cmd);
PageOfs = Addr & (Mxic->PageSz - 1);
if (!ByteCount)
return MXST_SUCCESS;
Spi->HardwareMode = IOMode;
Status = MxWREN(Mxic);
if (Status != MXST_SUCCESS)
return Status;
switch (Cmd)
{
case MX_CMD_4PP:
case MX_CMD_4PP4B:
Spi->FlashProtocol = PROT_1_4_4;
break;
case MX_CMD_QPP:
//case MX_CMD_QPP4B:
Spi->FlashProtocol = PROT_1_1_4;
break;
default:
//Spi->FlashProtocol = PROT_1_1_1;
break;
}
if ((PageOfs + ByteCount) <= Mxic->PageSz) {
Status = MxSetAddrDmyMode(Mxic, Cmd);
if (Status != MXST_SUCCESS)
return Status;
Spi->HardwareMode = TmpHardwareMode;
Status = MxSpiFlashWrite(Mxic->Priv, Addr, ByteCount, Buf, Cmd);
if (Status != MXST_SUCCESS)
return Status;
Spi->FlashProtocol = TmpFlashProtocol;
Spi->HardwareMode = IOMode;
Status = MxWaitForFlashReady(Mxic, Mxic->tPP);
if (Status != MXST_SUCCESS)
return Status;
Spi->HardwareMode = TmpHardwareMode;
}
else {
WrSz = Mxic->PageSz - PageOfs;
Status = MxSetAddrDmyMode(Mxic, Cmd);
if (Status != MXST_SUCCESS)
return Status;
Spi->HardwareMode = TmpHardwareMode;
Status = MxSpiFlashWrite(Mxic->Priv, Addr, WrSz, Buf, Cmd);
if (Status != MXST_SUCCESS)
return Status;
Spi->FlashProtocol = TmpFlashProtocol;
Spi->HardwareMode = IOMode;
Status = MxWaitForFlashReady(Mxic, Mxic->tPP);
if (Status != MXST_SUCCESS)
return Status;
for (n = WrSz; n < ByteCount; n += WrSz) {
WrSz = ByteCount - n;
if (WrSz > Mxic->PageSz)
WrSz = Mxic->PageSz;
//Spi->FlashProtocol = TmpFlashProtocol;
Status = MxWREN(Mxic);
Status = MxSetAddrDmyMode(Mxic, Cmd);
if (Status != MXST_SUCCESS)
return Status;
switch (Cmd)
{
case MX_CMD_4PP:
case MX_CMD_4PP4B:
Spi->FlashProtocol = PROT_1_4_4;
break;
case MX_CMD_QPP:
//case MX_CMD_QPP4B:
Spi->FlashProtocol = PROT_1_1_4;
break;
default:
//Spi->FlashProtocol = PROT_1_1_1;
break;
}
Spi->HardwareMode = TmpHardwareMode;
Status = MxSpiFlashWrite(Mxic->Priv, Addr + n, WrSz, Buf + n, Cmd);
if (Status != MXST_SUCCESS)
return Status;
Spi->FlashProtocol = TmpFlashProtocol;
Spi->HardwareMode = IOMode;
Status = MxWaitForFlashReady(Mxic, Mxic->tPP);
if (Status != MXST_SUCCESS)
return Status;
Spi->HardwareMode = TmpHardwareMode;
}
}
return MXST_SUCCESS;
}
/*
* Function: MxEraseTemplate
* Arguments: Mxic: pointer to an mxchip structure of nor flash device.
* Addr: device address to erase.
* EraseSizeCount: number of block or sector to erase.
* Cmd: erase commands (SE/SE4B/BE/BE4B/BE32K/CE) to issue
* Return Value: MXST_SUCCESS.
* MXST_FAILURE.
* MXST_TIMEOUT.
* Description: This function erases the data in the specified Block or Sector.
* Function issues all required commands and polls for completion.
* It is called by different erase commands functions like MxSE, MxSE4B and etc.
*/
inline static int MxEraseTemplate(MxChip *Mxic, u32 Addr, u32 EraseSizeCount, u8 Cmd)
{
int Status;
int n, AddrStart;
u32 EraseSize;
u32 ExpectTime;
if((Cmd == MX_CMD_BE) || (Cmd == MX_CMD_BE4B))
{
EraseSize = BLOCK64KB_SZ;
ExpectTime = Mxic->tBE;
}
else if ((Cmd == MX_CMD_BE32K) || (Cmd == MX_CMD_BE32K4B))
{
EraseSize = BLOCK32KB_SZ;
ExpectTime = Mxic->tBE32;
}
else if ((Cmd == MX_CMD_SE) || (Cmd == MX_CMD_SE4B))
{
EraseSize = SECTOR4KB_SZ;
ExpectTime = Mxic->tSE;
}
else
{
EraseSize = Mxic-> ChipSz;
ExpectTime = Mxic->tCE;
}
AddrStart = Addr / EraseSize;
for (n = AddrStart; n < AddrStart + EraseSizeCount; n++) {
Status = MxWREN(Mxic);
if (Status != MXST_SUCCESS)
return Status;
Status = MxSetAddrDmyMode(Mxic, Cmd);
if (Status != MXST_SUCCESS)
return Status;
Status = MxSpiFlashWrite(Mxic->Priv, n*EraseSize , 0, 0, Cmd);
if (Status != MXST_SUCCESS)
return Status;
Status = MxWaitForFlashReady(Mxic, ExpectTime);
if (Status != MXST_SUCCESS)
return Status;
}
return MXST_SUCCESS;
}
/*
* Block 0 : basic command
*/
#ifdef BLOCK0_BASIC
/**********************�x 1.ID commands �x *************************/
/*
* Function: MxRDID
* Arguments: Mxic: pointer to an mxchip structure of nor flash device.
* ByteCount: number of ID value to read.
* Buf: data buffer to store the ID value.
* Return Value: MXST_SUCCESS.
* MXST_FAILURE.
* Description: This function is for reading the manufacturer ID of 1-byte and followed by device ID of 2-byte.
*/
int MxRDID(MxChip *Mxic, u32 ByteCount, u8 *Buf)
{
u8 Cmd = MX_CMD_RDID;
int Status;
MxSpi *Spi = Mxic->Priv;
u8 i;
Status = MxSetAddrDmyMode(Mxic, Cmd);
if (Status != MXST_SUCCESS)
return Status;
if (Spi->CurMode == MODE_DOPI)
ByteCount *= 2;
Status = MxSpiFlashRead(Spi, 0, ByteCount, Buf, Cmd);
if (Status != MXST_SUCCESS)
return Status;
if (Spi->CurMode == MODE_DOPI)
{
for(i=0; i<ByteCount/2; i++)
Buf[i+1] = Buf[(i+1)*2];
}
return MXST_SUCCESS;
}
/*
* Function: MxRES
* Arguments: Mxic: pointer to an mxchip structure of nor flash device.
* Buf: data buffer to store the ID value.
* Return Value: MXST_SUCCESS.
* MXST_FAILURE.
* Description: This function is for reading the device electric identification of 1-byte.
*/
int MxRES(MxChip *Mxic, u8 *Buf)
{
u8 Cmd = MX_CMD_RES;
int Status;
Status = MxSetAddrDmyMode(Mxic, Cmd);
if (Status != MXST_SUCCESS)
return Status;
return MxSpiFlashRead(Mxic->Priv, 0, 1, Buf, Cmd);
}
/*
* Function: MxREMS
* Arguments: Mxic: pointer to an mxchip structure of nor flash device.
* Addr: the address determines the sequence of ID.
* Buf: data buffer to store the ID value.
* Return Value: MXST_SUCCESS.
* MXST_FAILURE.
* Description: This function is for reading the device manufacturer ID and electric ID of 1-byte.
*/
int MxREMS(MxChip *Mxic,u32 Addr, u8 *Buf)
{
u8 Cmd = MX_CMD_REMS;
int Status;
Status = MxSetAddrDmyMode(Mxic, Cmd);
if (Status != MXST_SUCCESS)
return Status;
return MxSpiFlashRead(Mxic->Priv, Addr, 2, Buf, Cmd);
}
/*
* Function: MxREMS2
* Arguments: Mxic: pointer to an mxchip structure of nor flash device.
* Addr: the address determines the sequence of ID.
* Buf: data buffer to store the ID value.
* Return Value: MXST_SUCCESS.
* MXST_FAILURE.
* Description: This function is for reading the device manufacturer ID and electric ID of 1-byte in 2 IO mode.
*/
int MxREMS2(MxChip *Mxic,u32 Addr, u8 *Buf)
{
u8 Cmd = MX_CMD_REMS2;
int Status;
Status = MxSetAddrDmyMode(Mxic, Cmd);
if (Status != MXST_SUCCESS)
return Status;
return MxSpiFlashRead(Mxic->Priv, Addr, 2, Buf, Cmd);
}
/*
* Function: MxREMS4
* Arguments: Mxic: pointer to an mxchip structure of nor flash device.
* Addr: the address determines the sequence of ID.
* Buf: data buffer to store the ID value.
* Return Value: MXST_SUCCESS.
* MXST_FAILURE.
* Description: This function is for reading the device manufacturer ID and electric ID of 1-byte in 4 IO mode.
*/
int MxREMS4(MxChip *Mxic,u32 Addr, u8 *Buf)
{
u8 Cmd = MX_CMD_REMS;
int Status;
Status = MxSetAddrDmyMode(Mxic, Cmd);
if (Status != MXST_SUCCESS)
return Status;
return MxSpiFlashRead(Mxic->Priv, Addr, 2, Buf, Cmd);
}
/*
* Function: MxREMS4D
* Arguments: Mxic: pointer to an mxchip structure of nor flash device.
* Addr: the address determines the sequence of ID.
* Buf: data buffer to store the ID value.
* Return Value: MXST_SUCCESS.
* MXST_FAILURE.
* Description: This function is for reading the device manufacturer ID and electric ID of 1-byte in 4 IO DT mode.
*/
int MxREMS4D(MxChip *Mxic,u32 Addr, u8 *Buf)
{
u8 Cmd = MX_CMD_REMS;
int Status;
Status = MxSetAddrDmyMode(Mxic, Cmd);
if (Status != MXST_SUCCESS)
return Status;
return MxSpiFlashRead(Mxic->Priv, Addr, 2, Buf, Cmd);
}
/*
* Function: MxQPIID
* Arguments: Mxic: pointer to an mxchip structure of nor flash device.
* ByteCount: number of ID to read.
* Buf: data buffer to store the Status Register value.
* Return Value: MXST_SUCCESS.
* MXST_FAILURE.
* Description: This function is for reading the manufacturer ID of 1-byte and followed by device ID of 2-byte in QPI interface.
*/
int MxQPIID(MxChip *Mxic, u32 ByteCount, u8 *Buf)
{
u8 Cmd = MX_CMD_QPIID;
int Status;
MxSpi *Spi = Mxic->Priv;
Status = MxSetAddrDmyMode(Mxic, Cmd);
if (Status != MXST_SUCCESS)
return Status;
return MxSpiFlashRead(Spi, 0, ByteCount, Buf, Cmd);
}
/**********************�x 2.Read commands �x *************************/
/*
* 3/4 byte address command - Read mode
*/
/*
* Function: MxREAD
* Arguments: Mxic: pointer to an mxchip structure of nor flash device.
* Addr: device address to read.
* ByteCount: number of bytes to read.
* RdBuf: pointer to a data buffer where the read data will be stored.
* Return Value: MXST_SUCCESS.
* MXST_FAILURE.
* Description: This function operates single IO read command and call MxReadTemplate function.
*/
int MxREAD(MxChip *Mxic, u32 Addr, u32 ByteCount, u8 *Buf)
{
return MxReadTemplate(Mxic, Addr, ByteCount, Buf, MX_CMD_READ);
}
/*
* Function: MxFASTREAD
* Arguments: Mxic: pointer to an mxchip structure of nor flash device.
* Addr: device address to read.
* ByteCount: number of bytes to read.
* RdBuf: pointer to a data buffer where the read data will be stored.
* Return Value: MXST_SUCCESS.
* MXST_FAILURE.
* Description: This function operates single IO fast read command and call MxReadTemplate function.
*/
int MxFASTREAD(MxChip *Mxic, u32 Addr, u32 ByteCount, u8 *Buf)
{
return MxReadTemplate(Mxic, Addr, ByteCount, Buf, MX_CMD_FASTREAD);
}
/*
* Function: MxFASTDTRD
* Arguments: Mxic: pointer to an mxchip structure of nor flash device.
* Addr: device address to read.
* ByteCount: number of bytes to read.
* RdBuf: pointer to a data buffer where the read data will be stored.
* Return Value: MXST_SUCCESS.
* MXST_FAILURE.
* Description: This function operates single IO fast DT read command and call MxReadTemplate function.
*/
int MxFASTDTRD(MxChip *Mxic, u32 Addr, u32 ByteCount, u8 *Buf)
{
return MxReadTemplate(Mxic, Addr, ByteCount, Buf, MX_CMD_FASTDTRD);
}
/*
* Function: Mx2READ
* Arguments: Mxic: pointer to an mxchip structure of nor flash device.
* Addr: device address to read.
* ByteCount: number of bytes to read.
* RdBuf: pointer to a data buffer where the read data will be stored.
* Return Value: MXST_SUCCESS.
* MXST_FAILURE.
* Description: This function operates two IO read command and call MxReadTemplate function.
*/
int Mx2READ(MxChip *Mxic, u32 Addr, u32 ByteCount, u8 *Buf)
{
return MxReadTemplate(Mxic, Addr, ByteCount, Buf, MX_CMD_2READ);
}
/*
* Function: Mx2DTRD
* Arguments: Mxic: pointer to an mxchip structure of nor flash device.
* Addr: device address to read.
* ByteCount: number of bytes to read.
* RdBuf: pointer to a data buffer where the read data will be stored.
* Return Value: MXST_SUCCESS.
* MXST_FAILURE.
* Description: This function operates two IO DT read command and call MxReadTemplate function.
*/
int Mx2DTRD(MxChip *Mxic, u32 Addr, u32 ByteCount, u8 *Buf)
{
return MxReadTemplate(Mxic, Addr, ByteCount, Buf, MX_CMD_2DTRD);
}
/*
* Function: MxDREAD
* Arguments: Mxic: pointer to an mxchip structure of nor flash device.
* Addr: device address to read.
* ByteCount: number of bytes to read.
* RdBuf: pointer to a data buffer where the read data will be stored.
* Return Value: MXST_SUCCESS.
* MXST_FAILURE.
* Description: This function operates two IO read command and call MxReadTemplate function.
*/
int MxDREAD(MxChip *Mxic, u32 Addr, u32 ByteCount, u8 *Buf)
{
return MxReadTemplate(Mxic, Addr, ByteCount, Buf, MX_CMD_DREAD);
}
/*
* Function: Mx4READ
* Arguments: Mxic: pointer to an mxchip structure of nor flash device.
* Addr: device address to read.
* ByteCount: number of bytes to read.
* RdBuf: pointer to a data buffer where the read data will be stored.
* Return Value: MXST_SUCCESS.
* MXST_FAILURE.
* Description: This function operates four IO read command and call MxReadTemplate function.
*/
int Mx4READ(MxChip *Mxic, u32 Addr, u32 ByteCount, u8 *Buf)
{
return MxReadTemplate(Mxic, Addr, ByteCount, Buf, MX_CMD_4READ_BOTTOM);
}
/*
* Function: Mx4DTRD
* Arguments: Mxic: pointer to an mxchip structure of nor flash device.
* Addr: device address to read.
* ByteCount: number of bytes to read.
* RdBuf: pointer to a data buffer where the read data will be stored.
* Return Value: MXST_SUCCESS.
* MXST_FAILURE.
* Description: This function operates four IO DT read command and call MxReadTemplate function.
*/
int Mx4DTRD(MxChip *Mxic, u32 Addr, u32 ByteCount, u8 *Buf)
{
return MxReadTemplate(Mxic, Addr, ByteCount, Buf, MX_CMD_4DTRD);
}
/*
* Function: MxQREAD
* Arguments: Mxic: pointer to an mxchip structure of nor flash device.
* Addr: device address to read.
* ByteCount: number of bytes to read.
* RdBuf: pointer to a data buffer where the read data will be stored.
* Return Value: MXST_SUCCESS.
* MXST_FAILURE.
* Description: This function operates four IO read command and call MxReadTemplate function.
*/
int MxQREAD(MxChip *Mxic, u32 Addr, u32 ByteCount, u8 *Buf)
{
return MxReadTemplate(Mxic, Addr, ByteCount, Buf, MX_CMD_QREAD);
}
/*
* 4 byte address command - Read mode
*/
/*
* Function: MxREAD4B
* Arguments: Mxic: pointer to an mxchip structure of nor flash device.
* Addr: device address to read.
* ByteCount: number of bytes to read.
* RdBuf: pointer to a data buffer where the read data will be stored.
* Return Value: MXST_SUCCESS.
* MXST_FAILURE.
* Description: This function operates single IO read command and call MxReadTemplate function.
* The byte number of address should be 4 byte.
*/
int MxREAD4B(MxChip *Mxic, u32 Addr, u32 ByteCount, u8 *Buf)
{
return MxReadTemplate(Mxic, Addr, ByteCount, Buf, MX_CMD_READ4B);
}
/*
* Function: MxFASTREAD4B
* Arguments: Mxic: pointer to an mxchip structure of nor flash device.
* Addr: device address to read.
* ByteCount: number of bytes to read.
* RdBuf: pointer to a data buffer where the read data will be stored.
* Return Value: MXST_SUCCESS.
* MXST_FAILURE.
* Description: This function operates single IO fast read command and call MxReadTemplate function.
* The byte number of address should be 4 byte.
*/
int MxFASTREAD4B(MxChip *Mxic, u32 Addr, u32 ByteCount, u8 *Buf)
{
return MxReadTemplate(Mxic, Addr, ByteCount, Buf, MX_CMD_FASTREAD4B);
}
/*
* Function: MxFASTDTRD4B
* Arguments: Mxic: pointer to an mxchip structure of nor flash device.
* Addr: device address to read.
* ByteCount: number of bytes to read.
* RdBuf: pointer to a data buffer where the read data will be stored.
* Return Value: MXST_SUCCESS.
* MXST_FAILURE.
* Description: This function operates single IO fast DT read command and call MxReadTemplate function.
* The byte number of address should be 4 byte.
*/
int MxFASTDTRD4B(MxChip *Mxic, u32 Addr, u32 ByteCount, u8 *Buf)
{
return MxReadTemplate(Mxic, Addr, ByteCount, Buf, MX_CMD_FASTDTRD4B);
}
/*
* Function: Mx2READ4B
* Arguments: Mxic: pointer to an mxchip structure of nor flash device.
* Addr: device address to read.
* ByteCount: number of bytes to read.
* RdBuf: pointer to a data buffer where the read data will be stored.
* Return Value: MXST_SUCCESS.
* MXST_FAILURE.
* Description: This function operates two IO read command and call MxReadTemplate function.
* The byte number of address should be 4 byte.
*/
int Mx2READ4B(MxChip *Mxic, u32 Addr, u32 ByteCount, u8 *Buf)
{
return MxReadTemplate(Mxic, Addr, ByteCount, Buf, MX_CMD_2READ4B);
}
/*
* Function: Mx2DTRD4B
* Arguments: Mxic: pointer to an mxchip structure of nor flash device.
* Addr: device address to read.
* ByteCount: number of bytes to read.
* RdBuf: pointer to a data buffer where the read data will be stored.
* Return Value: MXST_SUCCESS.
* MXST_FAILURE.
* Description: This function operates two IO DT read command and call MxReadTemplate function.
* The byte number of address should be 4 byte.
*/
int Mx2DTRD4B(MxChip *Mxic, u32 Addr, u32 ByteCount, u8 *Buf)
{
return MxReadTemplate(Mxic, Addr, ByteCount, Buf, MX_CMD_2DTRD4B);
}
/*
* Function: MxDREAD4B
* Arguments: Mxic: pointer to an mxchip structure of nor flash device.
* Addr: device address to read.
* ByteCount: number of bytes to read.
* RdBuf: pointer to a data buffer where the read data will be stored.
* Return Value: MXST_SUCCESS.
* MXST_FAILURE.
* Description: This function operates two IO read command and call MxReadTemplate function.
* The byte number of address should be 4 byte.
*/
int MxDREAD4B(MxChip *Mxic, u32 Addr, u32 ByteCount, u8 *Buf)
{
return MxReadTemplate(Mxic, Addr, ByteCount, Buf, MX_CMD_DREAD4B);
}
/*
* Function: Mx4READ4B
* Arguments: Mxic: pointer to an mxchip structure of nor flash device.
* Addr: device address to read.
* ByteCount: number of bytes to read.
* RdBuf: pointer to a data buffer where the read data will be stored.
* Return Value: MXST_SUCCESS.
* MXST_FAILURE.
* Description: This function operates four IO read command and call MxReadTemplate function.
* The byte number of address should be 4 byte.
*/
int Mx4READ4B(MxChip *Mxic, u32 Addr, u32 ByteCount, u8 *Buf)
{
return MxReadTemplate(Mxic, Addr, ByteCount, Buf, MX_CMD_4READ4B);
}
/*
* Function: Mx4DTRD4B
* Arguments: Mxic: pointer to an mxchip structure of nor flash device.
* Addr: device address to read.
* ByteCount: number of bytes to read.
* RdBuf: pointer to a data buffer where the read data will be stored.
* Return Value: MXST_SUCCESS.
* MXST_FAILURE.
* Description: This function operates four IO DT read command and call MxReadTemplate function.
* The byte number of address should be 4 byte.
*/
int Mx4DTRD4B(MxChip *Mxic, u32 Addr, u32 ByteCount, u8 *Buf)
{