-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathflash.c
1235 lines (1097 loc) · 34.5 KB
/
flash.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 Files *********************************/
#include "xqspips.h" /* QSPI device driver */
#include "xil_io.h"
#include "flash.h"
#include "sleep.h"
/************************** Variable Definitions *****************************/
FlashInfo Flash_Config_Table[34] = {
/* Spansion */
{0x10000, 0x100, 256, 0x10000, 0x1000000,
SPANSION_ID_BYTE0, SPANSION_ID_BYTE2_128, 0xFFFF0000, 1},
{0x10000, 0x200, 256, 0x20000, 0x1000000,
SPANSION_ID_BYTE0, SPANSION_ID_BYTE2_128, 0xFFFF0000, 1},
{0x20000, 0x100, 512, 0x10000, 0x1000000,
SPANSION_ID_BYTE0, SPANSION_ID_BYTE2_128, 0xFFFE0000, 1},
{0x10000, 0x200, 256, 0x20000, 0x2000000,
SPANSION_ID_BYTE0, SPANSION_ID_BYTE2_256, 0xFFFF0000, 1},
{0x10000, 0x400, 256, 0x40000, 0x2000000,
SPANSION_ID_BYTE0, SPANSION_ID_BYTE2_256, 0xFFFF0000, 1},
{0x20000, 0x200, 512, 0x20000, 0x2000000,
SPANSION_ID_BYTE0, SPANSION_ID_BYTE2_256, 0xFFFE0000, 1},
{0x40000, 0x100, 512, 0x20000, 0x4000000,
SPANSION_ID_BYTE0, SPANSION_ID_BYTE2_512, 0xFFFC0000, 1},
{0x40000, 0x200, 512, 0x40000, 0x4000000,
SPANSION_ID_BYTE0, SPANSION_ID_BYTE2_512, 0xFFFC0000, 1},
{0x80000, 0x100, 1024, 0x20000, 0x4000000,
SPANSION_ID_BYTE0, SPANSION_ID_BYTE2_512, 0xFFF80000, 1},
/* Spansion 1Gbit is handled as 512Mbit stacked */
/* Micron */
{0x10000, 0x100, 256, 0x10000, 0x1000000,
MICRON_ID_BYTE0, MICRON_ID_BYTE2_128, 0xFFFF0000, 1},
{0x10000, 0x200, 256, 0x20000, 0x1000000,
MICRON_ID_BYTE0, MICRON_ID_BYTE2_128, 0xFFFF0000, 1},
{0x20000, 0x100, 512, 0x10000, 0x1000000,
MICRON_ID_BYTE0, MICRON_ID_BYTE2_128, 0xFFFE0000, 1},
{0x10000, 0x200, 256, 0x20000, 0x2000000,
MICRON_ID_BYTE0, MICRON_ID_BYTE2_256, 0xFFFF0000, 1},
{0x10000, 0x400, 256, 0x40000, 0x2000000,
MICRON_ID_BYTE0, MICRON_ID_BYTE2_256, 0xFFFF0000, 1},
{0x20000, 0x200, 512, 0x20000, 0x2000000,
MICRON_ID_BYTE0, MICRON_ID_BYTE2_256, 0xFFFE0000, 1},
{0x10000, 0x400, 256, 0x40000, 0x4000000,
MICRON_ID_BYTE0, MICRON_ID_BYTE2_512, 0xFFFF0000, 2},
{0x10000, 0x800, 256, 0x80000, 0x4000000,
MICRON_ID_BYTE0, MICRON_ID_BYTE2_512, 0xFFFF0000, 2},
{0x20000, 0x400, 512, 0x40000, 0x4000000,
MICRON_ID_BYTE0, MICRON_ID_BYTE2_512, 0xFFFE0000, 2},
{0x10000, 0x800, 256, 0x80000, 0x8000000,
MICRON_ID_BYTE0, MICRON_ID_BYTE2_1G, 0xFFFF0000, 4},
{0x10000, 0x1000, 256, 0x100000, 0x8000000,
MICRON_ID_BYTE0, MICRON_ID_BYTE2_1G, 0xFFFF0000, 4},
{0x20000, 0x800, 512, 0x80000, 0x8000000,
MICRON_ID_BYTE0, MICRON_ID_BYTE2_1G, 0xFFFE0000, 4},
/* Winbond */
{0x10000, 0x100, 256, 0x10000, 0x1000000,
WINBOND_ID_BYTE0, WINBOND_ID_BYTE2_128, 0xFFFF0000, 1},
{0x10000, 0x200, 256, 0x20000, 0x1000000,
WINBOND_ID_BYTE0, WINBOND_ID_BYTE2_128, 0xFFFF0000, 1},
{0x20000, 0x100, 512, 0x10000, 0x1000000,
WINBOND_ID_BYTE0, WINBOND_ID_BYTE2_128, 0xFFFE0000, 1},
/* Macronix */
{0x10000, 0x200, 256, 0x20000, 0x2000000,
MACRONIX_ID_BYTE0, MACRONIX_ID_BYTE2_256, 0xFFFF0000, 1},
{0x10000, 0x400, 256, 0x40000, 0x2000000,
MACRONIX_ID_BYTE0, MACRONIX_ID_BYTE2_256, 0xFFFF0000, 1},
{0x20000, 0x200, 512, 0x20000, 0x2000000,
MACRONIX_ID_BYTE0, MACRONIX_ID_BYTE2_256, 0xFFFE0000, 1},
{0x10000, 0x400, 256, 0x40000, 0x4000000,
MACRONIX_ID_BYTE0, MACRONIX_ID_BYTE2_512, 0xFFFF0000, 1},
{0x10000, 0x800, 256, 0x80000, 0x4000000,
MACRONIX_ID_BYTE0, MACRONIX_ID_BYTE2_512, 0xFFFF0000, 1},
{0x20000, 0x400, 512, 0x40000, 0x4000000,
MACRONIX_ID_BYTE0, MACRONIX_ID_BYTE2_512, 0xFFFE0000, 1},
{0x2000, 0x4000, 256, 0x80000, 0x8000000,
MACRONIX_ID_BYTE0, MACRONIX_ID_BYTE2_1G, 0xFFFF0000, 1},
{0x2000, 0x8000, 256, 0x100000, 0x8000000,
MACRONIX_ID_BYTE0, MACRONIX_ID_BYTE2_1G, 0xFFFF0000, 1},
{0x4000, 0x4000, 512, 0x80000, 0x8000000,
MACRONIX_ID_BYTE0, MACRONIX_ID_BYTE2_1G, 0xFFFE0000, 1},
/* ISSI */
{0x10000, 0x200, 256, 0x20000, 0x2000000,
ISSI_ID_BYTE0, ISSI_ID_BYTE2_256, 0xFFFF0000, 1}
}; /**< Flash Config Table */
u32 FlashMake;
u32 FCTIndex = -1; /* Flash configuration table index */
/*
* The following variables are used to read and write to the flash and they
* are global to avoid having large buffers on the stack
* The buffer size accounts for maximum page size and maximum banks -
* for each bank separate read will be performed leading to that many
* (overhead+dummy) bytes
*/
u8 ReadBuffer[(PAGE_COUNT * MAX_PAGE_SIZE) + (DATA_OFFSET + DUMMY_SIZE)*8];
/*
* The following constants specify the max amount of data and the size of the
* the buffer required to hold the data and overhead to transfer the data to
* and from the Flash. Initialized to single flash page size.
*/
u32 MaxData = PAGE_COUNT*256;
FlashInfo* GetFlashInfo(){
return &(Flash_Config_Table[FCTIndex]);
}
u8* get_slice_buffer(){
return ReadBuffer;
}
u32 get_slice_buffer_size(){
return (PAGE_COUNT * MAX_PAGE_SIZE);
}
int QspiFlashDump(XQspiPs *QspiInstancePtr, u32 start_addr, u32 len){
if (!QspiInstancePtr->IsReady){
// If the device has not been configured return error:
return XST_FAILURE;
}
start_addr = start_addr & 0xfffffff0;
len = len & 0x000007f0;
u8* ReadBuffer;
/*
* I/O Read - for any flash size
*/
ReadBuffer = FlashRead2(QspiInstancePtr, start_addr, len);
MemDump(ReadBuffer, start_addr, len);
return XST_SUCCESS;
}
u8* FlashRead2(XQspiPs *QspiInstancePtr, u32 start_addr, u32 len){
if (!QspiInstancePtr->IsReady){
// If the device has not been configured return error:
return 0;
}
xil_printf("ReadBuffer: 0x%x\r\n", ReadBuffer);
FlashRead(QspiInstancePtr, start_addr, len, QUAD_READ_CMD,
ReadBuffer, ReadBuffer);
return ReadBuffer;
}
void MemDump(void* ptr, u32 start_addr, u32 len){
start_addr = start_addr & 0xfffffff0;
len = len & 0x000007f0;
for(int i=0; i<len; i+=16){
xil_printf("%08x ", i+start_addr);
for(int j=0; j<8; j++)
xil_printf("%04x ", ((u16*)ptr)[(i>>1)+j]);
xil_printf("\r\n");
}
}
int FlashInit(XQspiPs *QspiInstancePtr, u16 QspiDeviceId) {
int Status;
XQspiPs_Config *QspiConfig;
/*
* Initialize the QSPI driver so that it's ready to use
*/
QspiConfig = XQspiPs_LookupConfig(QspiDeviceId);
if (NULL == QspiConfig) {
return XST_FAILURE;
}
Status = XQspiPs_CfgInitialize(QspiInstancePtr, QspiConfig,
QspiConfig->BaseAddress);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
/*
* Perform a self-test to check hardware build
*/
Status = XQspiPs_SelfTest(QspiInstancePtr);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
/*
* Set the pre-scaler for QSPI clock
*/
XQspiPs_SetClkPrescaler(QspiInstancePtr, XQSPIPS_CLK_PRESCALE_8);
/*
* Set Manual Start and Manual Chip select options and drive the
* HOLD_B high.
*/
XQspiPs_SetOptions(QspiInstancePtr, XQSPIPS_FORCE_SSELECT_OPTION |
XQSPIPS_MANUAL_START_OPTION |
XQSPIPS_HOLD_B_DRIVE_OPTION);
if(QspiConfig->ConnectionMode == XQSPIPS_CONNECTION_MODE_STACKED) {
/*
* Enable two flash memories, Shared bus (NOT separate bus),
* L_PAGE selected by default
*/
XQspiPs_SetLqspiConfigReg(QspiInstancePtr, DUAL_STACK_CONFIG_WRITE);
}
if(QspiConfig->ConnectionMode == XQSPIPS_CONNECTION_MODE_PARALLEL) {
/*
* Enable two flash memories on separate buses
*/
XQspiPs_SetLqspiConfigReg(QspiInstancePtr, DUAL_QSPI_CONFIG_WRITE);
}
/*
* Assert the Flash chip select.
*/
XQspiPs_SetSlaveSelect(QspiInstancePtr);
/*
* Read flash ID and obtain all flash related information
* It is important to call the read id function before
* performing proceeding to any operation, including
* preparing the WriteBuffer
*/
Status = FlashReadID(QspiInstancePtr, ReadBuffer);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
/*
* Initialize MaxData according to page size.
*/
MaxData = PAGE_COUNT * (Flash_Config_Table[FCTIndex].PageSize);
return XST_SUCCESS;
}
void FlashWrite2(XQspiPs *QspiPtr, u32 Address, u32 ByteCount, u8 *WriteBfrPtr)
{
u32 page_size = Flash_Config_Table[FCTIndex].PageSize;
u32 page_cnt = ByteCount/page_size;
u32 write_size = 0;
/*
* Write the data in the write buffer to the serial Flash a page at a
* time, starting from TEST_ADDRESS
*/
verb_printf(DEBUG, "page_size 0x%x\r\n", page_size);
verb_printf(DEBUG, "ByteCount 0x%x\r\n", ByteCount);
verb_printf(DEBUG, "page_cnt 0x%x\r\n", page_cnt);
for (int Page = 0; Page < page_cnt; Page++) {
write_size = page_size < ByteCount ? page_size : ByteCount;
FlashWrite(QspiPtr, (Page * page_size) + Address, write_size, WRITE_CMD, WriteBfrPtr + Page * page_size);
ByteCount-=page_size;
verb_printf(DEBUG, ".");
}
verb_printf(DEBUG, "\r\n");
}
/*****************************************************************************/
/**
*
*
* This function writes to the serial Flash connected to the QSPI interface.
* All the data put into the buffer must be in the same page of the device with
* page boundaries being on 256 byte boundaries.
*
* @param QspiPtr is a pointer to the QSPI driver component to use.
* @param Address contains the address to write data to in the Flash.
* @param ByteCount contains the number of bytes to write.
* @param Command is the command used to write data to the flash. QSPI
* device supports only Page Program command to write data to the
* flash.
* @param Pointer to the write buffer (which is to be transmitted)
*
* @return None.
*
* @note None.
*
******************************************************************************/
void FlashWrite(XQspiPs *QspiPtr, u32 Address, u32 ByteCount, u8 Command,
u8 *WriteBfrPtr)
{
u8 WriteEnableCmd = { WRITE_ENABLE_CMD };
u8 ReadStatusCmd[] = { READ_STATUS_CMD, 0 }; /* Must send 2 bytes */
u8 FlashStatus[2];
u32 RealAddr;
u32 BankSel;
u8 ReadFlagSRCmd[] = {READ_FLAG_STATUS_CMD, 0};
u8 FlagStatus[2];
/*
* Translate address based on type of connection
* If stacked assert the slave select based on address
*/
RealAddr = GetRealAddr(QspiPtr, Address);
/*
* Bank Select
*/
if(Flash_Config_Table[FCTIndex].FlashDeviceSize > SIXTEENMB) {
/*
* Calculate bank
*/
BankSel = RealAddr/SIXTEENMB;
/*
* Select bank
*/
SendBankSelect(QspiPtr, BankSel);
}
/*
* Send the write enable command to the Flash so that it can be
* written to, this needs to be sent as a separate transfer before
* the write
*/
XQspiPs_PolledTransfer(QspiPtr, &WriteEnableCmd, NULL,
sizeof(WriteEnableCmd));
/*
* Setup the write command with the specified address and data for the
* Flash
*/
/*
* This will ensure a 3B address is transferred even when address
* is greater than 128Mb.
*/
WriteBfrPtr[COMMAND_OFFSET] = Command;
WriteBfrPtr[ADDRESS_1_OFFSET] = (u8)((RealAddr & 0xFF0000) >> 16);
WriteBfrPtr[ADDRESS_2_OFFSET] = (u8)((RealAddr & 0xFF00) >> 8);
WriteBfrPtr[ADDRESS_3_OFFSET] = (u8)(RealAddr & 0xFF);
/*
* Send the write command, address, and data to the Flash to be
* written, no receive buffer is specified since there is nothing to
* receive
*/
XQspiPs_PolledTransfer(QspiPtr, WriteBfrPtr, NULL,
ByteCount + OVERHEAD_SIZE);
if((Flash_Config_Table[FCTIndex].NumDie > 1) &&
(FlashMake == MICRON_ID_BYTE0)) {
XQspiPs_PolledTransfer(QspiPtr, ReadFlagSRCmd, FlagStatus,
sizeof(ReadFlagSRCmd));
}
/*
* Wait for the write command to the Flash to be completed, it takes
* some time for the data to be written
*/
while (1) {
/*
* Poll the status register of the Flash to determine when it
* completes, by sending a read status command and receiving the
* status byte
*/
XQspiPs_PolledTransfer(QspiPtr, ReadStatusCmd, FlashStatus,
sizeof(ReadStatusCmd));
/*
* If the status indicates the write is done, then stop waiting,
* if a value of 0xFF in the status byte is read from the
* device and this loop never exits, the device slave select is
* possibly incorrect such that the device status is not being
* read
*/
if ((FlashStatus[1] & 0x01) == 0) {
break;
}
}
if((Flash_Config_Table[FCTIndex].NumDie > 1) &&
(FlashMake == MICRON_ID_BYTE0)) {
XQspiPs_PolledTransfer(QspiPtr, ReadFlagSRCmd, FlagStatus,
sizeof(ReadFlagSRCmd));
}
}
int FlashEraseAll(XQspiPs *QspiPtr)
{
if (!QspiPtr->IsReady){
// If the device has not been configured return error:
return XST_FAILURE;
}
u32 ByteCount = (Flash_Config_Table[FCTIndex]).NumSect *
(Flash_Config_Table[FCTIndex]).SectSize;
FlashErase(QspiPtr, 0, ByteCount, ReadBuffer);
return XST_SUCCESS;
}
int FlashErase2(XQspiPs *QspiPtr, u32 Address, u32 ByteCount)
{
if (!QspiPtr->IsReady){
// If the device has not been configured return error:
return XST_FAILURE;
}
FlashErase(QspiPtr, Address, ByteCount, ReadBuffer);
return XST_SUCCESS;
}
/*****************************************************************************/
/**
*
*
* This function erases the sectors in the serial Flash connected to the
* QSPI interface.
*
* @param QspiPtr is a pointer to the QSPI driver component to use.
* @param Address contains the address of the first sector which needs to
* be erased.
* @param ByteCount contains the total size to be erased.
* @param Pointer to the write buffer (which is to be transmitted)
*
* @return None.
*
* @note None.
*
******************************************************************************/
void FlashErase(XQspiPs *QspiPtr, u32 Address, u32 ByteCount, u8 *WriteBfrPtr)
{
u8 WriteEnableCmd = { WRITE_ENABLE_CMD };
u8 ReadStatusCmd[] = { READ_STATUS_CMD, 0 }; /* Must send 2 bytes */
u8 FlashStatus[2];
int Sector;
u32 RealAddr;
u32 LqspiCr;
u32 NumSect;
u32 BankSel;
u8 BankInitFlag = 1;
u8 ReadFlagSRCmd[] = { READ_FLAG_STATUS_CMD, 0 };
u8 FlagStatus[2];
/*
* If erase size is same as the total size of the flash, use bulk erase
* command or die erase command multiple times as required
*/
if (ByteCount == ((Flash_Config_Table[FCTIndex]).NumSect *
(Flash_Config_Table[FCTIndex]).SectSize) ) {
if(QspiPtr->Config.ConnectionMode == XQSPIPS_CONNECTION_MODE_STACKED){
/*
* Get the current LQSPI configuration register value
*/
LqspiCr = XQspiPs_GetLqspiConfigReg(QspiPtr);
/*
* Set selection to L_PAGE
*/
XQspiPs_SetLqspiConfigReg(QspiPtr,
LqspiCr & (~XQSPIPS_LQSPI_CR_U_PAGE_MASK));
/*
* Assert the Flash chip select.
*/
XQspiPs_SetSlaveSelect(QspiPtr);
}
if(Flash_Config_Table[FCTIndex].NumDie == 1) {
/*
* Call Bulk erase
*/
BulkErase(QspiPtr, WriteBfrPtr);
}
if(Flash_Config_Table[FCTIndex].NumDie > 1) {
/*
* Call Die erase
*/
DieErase(QspiPtr, WriteBfrPtr);
}
/*
* If stacked mode, bulk erase second flash
*/
if(QspiPtr->Config.ConnectionMode == XQSPIPS_CONNECTION_MODE_STACKED){
/*
* Get the current LQSPI configuration register value
*/
LqspiCr = XQspiPs_GetLqspiConfigReg(QspiPtr);
/*
* Set selection to U_PAGE
*/
XQspiPs_SetLqspiConfigReg(QspiPtr,
LqspiCr | XQSPIPS_LQSPI_CR_U_PAGE_MASK);
/*
* Assert the Flash chip select.
*/
XQspiPs_SetSlaveSelect(QspiPtr);
if(Flash_Config_Table[FCTIndex].NumDie == 1) {
/*
* Call Bulk erase
*/
BulkErase(QspiPtr, WriteBfrPtr);
}
if(Flash_Config_Table[FCTIndex].NumDie > 1) {
/*
* Call Die erase
*/
DieErase(QspiPtr, WriteBfrPtr);
}
}
return;
}
/*
* If the erase size is less than the total size of the flash, use
* sector erase command
*/
/*
* Calculate no. of sectors to erase based on byte count
*/
NumSect = ByteCount/(Flash_Config_Table[FCTIndex].SectSize) + 1;
/*
* If ByteCount to k sectors,
* but the address range spans from N to N+k+1 sectors, then
* increment no. of sectors to be erased
*/
if( ((Address + ByteCount) & Flash_Config_Table[FCTIndex].SectMask) ==
((Address + (NumSect * Flash_Config_Table[FCTIndex].SectSize)) &
Flash_Config_Table[FCTIndex].SectMask) ) {
NumSect++;
}
for (Sector = 0; Sector < NumSect; Sector++) {
/*
* Translate address based on type of connection
* If stacked assert the slave select based on address
*/
RealAddr = GetRealAddr(QspiPtr, Address);
/*
* Initial bank selection
*/
if((BankInitFlag) &&
(Flash_Config_Table[FCTIndex].FlashDeviceSize > SIXTEENMB)) {
/*
* Reset initial bank select flag
*/
BankInitFlag = 0;
/*
* Calculate initial bank
*/
BankSel = RealAddr/SIXTEENMB;
/*
* Select bank
*/
SendBankSelect(QspiPtr, BankSel);
}
/*
* Check bank and send bank select if new bank
*/
if((BankSel != RealAddr/SIXTEENMB) &&
(Flash_Config_Table[FCTIndex].FlashDeviceSize > SIXTEENMB)) {
/*
* Calculate initial bank
*/
BankSel = RealAddr/SIXTEENMB;
/*
* Select bank
*/
SendBankSelect(QspiPtr, BankSel);
}
/*
* Send the write enable command to the SEEPOM so that it can be
* written to, this needs to be sent as a separate transfer
* before the write
*/
XQspiPs_PolledTransfer(QspiPtr, &WriteEnableCmd, NULL,
sizeof(WriteEnableCmd));
/*
* Setup the write command with the specified address and data
* for the Flash
*/
/*
* This ensures 3B address is sent to flash even with address
* greater than 128Mb.
*/
WriteBfrPtr[COMMAND_OFFSET] = SEC_ERASE_CMD;
WriteBfrPtr[ADDRESS_1_OFFSET] = (u8)(RealAddr >> 16);
WriteBfrPtr[ADDRESS_2_OFFSET] = (u8)(RealAddr >> 8);
WriteBfrPtr[ADDRESS_3_OFFSET] = (u8)(RealAddr & 0xFF);
/*
* Send the sector erase command and address; no receive buffer
* is specified since there is nothing to receive
*/
XQspiPs_PolledTransfer(QspiPtr, WriteBfrPtr, NULL,
SEC_ERASE_SIZE);
if((Flash_Config_Table[FCTIndex].NumDie > 1) &&
(FlashMake == MICRON_ID_BYTE0)) {
XQspiPs_PolledTransfer(QspiPtr, ReadFlagSRCmd, FlagStatus,
sizeof(ReadFlagSRCmd));
}
/*
* Wait for the sector erase command to the Flash to be completed
*/
while (1) {
/*
* Poll the status register of the device to determine
* when it completes, by sending a read status command
* and receiving the status byte
*/
XQspiPs_PolledTransfer(QspiPtr, ReadStatusCmd,
FlashStatus,
sizeof(ReadStatusCmd));
/*
* If the status indicates the write is done, then stop
* waiting, if a value of 0xFF in the status byte is
* read from the device and this loop never exits, the
* device slave select is possibly incorrect such that
* the device status is not being read
*/
if ((FlashStatus[1] & 0x01) == 0) {
break;
}
}
if((Flash_Config_Table[FCTIndex].NumDie > 1) &&
(FlashMake == MICRON_ID_BYTE0)) {
XQspiPs_PolledTransfer(QspiPtr, ReadFlagSRCmd, FlagStatus,
sizeof(ReadFlagSRCmd));
}
Address += Flash_Config_Table[FCTIndex].SectSize;
}
}
/*****************************************************************************/
/**
*
* This function reads serial Flash ID connected to the SPI interface.
* It then deduces the make and size of the flash and obtains the
* connection mode to point to corresponding parameters in the flash
* configuration table. The flash driver will function based on this and
* it presently supports Micron and Spansion - 128, 256 and 512Mbit and
* Winbond 128Mbit
*
* @param QspiPtr is a pointer to the QSPI driver component to use.
* @param Pointer to the write buffer (which is to be transmitted)
* @param Pointer to the read buffer to which valid received data should be
* written
*
* @return XST_SUCCESS if read id, otherwise XST_FAILURE.
*
* @note None.
*
******************************************************************************/
int FlashReadID(XQspiPs *QspiPtr, u8 *buffer_ptr)
{
int Status;
int StartIndex;
/*
* Read ID in Auto mode.
*/
buffer_ptr[COMMAND_OFFSET] = READ_ID;
buffer_ptr[ADDRESS_1_OFFSET] = 0x23; /* 3 dummy bytes */
buffer_ptr[ADDRESS_2_OFFSET] = 0x08;
buffer_ptr[ADDRESS_3_OFFSET] = 0x09;
Status = XQspiPs_PolledTransfer(QspiPtr, buffer_ptr, buffer_ptr,
RD_ID_SIZE);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
/*
* Deduce flash make
*/
if(buffer_ptr[1] == MICRON_ID_BYTE0) {
FlashMake = MICRON_ID_BYTE0;
StartIndex = MICRON_INDEX_START;
}else if(buffer_ptr[1] == SPANSION_ID_BYTE0) {
FlashMake = SPANSION_ID_BYTE0;
StartIndex = SPANSION_INDEX_START;
}else if(buffer_ptr[1] == WINBOND_ID_BYTE0) {
FlashMake = WINBOND_ID_BYTE0;
StartIndex = WINBOND_INDEX_START;
} else if(buffer_ptr[1] == MACRONIX_ID_BYTE0) {
FlashMake = MACRONIX_ID_BYTE0;
StartIndex = MACRONIX_INDEX_START;
} else if(buffer_ptr[0] == ISSI_ID_BYTE0) {
FlashMake = ISSI_ID_BYTE0;
StartIndex = ISSI_INDEX_START;
} else {
return XST_FAILURE;
}
/*
* If valid flash ID, then check connection mode & size and
* assign corresponding index in the Flash configuration table
*/
if(((FlashMake == MICRON_ID_BYTE0) || (FlashMake == SPANSION_ID_BYTE0)||
(FlashMake == WINBOND_ID_BYTE0)) &&
(buffer_ptr[3] == MICRON_ID_BYTE2_128)) {
switch(QspiPtr->Config.ConnectionMode)
{
case XQSPIPS_CONNECTION_MODE_SINGLE:
FCTIndex = FLASH_CFG_TBL_SINGLE_128_SP + StartIndex;
break;
case XQSPIPS_CONNECTION_MODE_PARALLEL:
FCTIndex = FLASH_CFG_TBL_PARALLEL_128_SP + StartIndex;
break;
case XQSPIPS_CONNECTION_MODE_STACKED:
FCTIndex = FLASH_CFG_TBL_STACKED_128_SP + StartIndex;
break;
default:
FCTIndex = 0;
break;
}
}
/* 256 and 512Mbit supported only for Micron and Spansion, not Winbond */
if(((FlashMake == MICRON_ID_BYTE0) || (FlashMake == SPANSION_ID_BYTE0)
|| (FlashMake == MACRONIX_ID_BYTE0)) &&
(buffer_ptr[3] == MICRON_ID_BYTE2_256)) {
switch(QspiPtr->Config.ConnectionMode)
{
case XQSPIPS_CONNECTION_MODE_SINGLE:
FCTIndex = FLASH_CFG_TBL_SINGLE_256_SP + StartIndex;
break;
case XQSPIPS_CONNECTION_MODE_PARALLEL:
FCTIndex = FLASH_CFG_TBL_PARALLEL_256_SP + StartIndex;
break;
case XQSPIPS_CONNECTION_MODE_STACKED:
FCTIndex = FLASH_CFG_TBL_STACKED_256_SP + StartIndex;
break;
default:
FCTIndex = 0;
break;
}
}
if((FlashMake == ISSI_ID_BYTE0) &&
(buffer_ptr[2] == MICRON_ID_BYTE2_256)) {
switch(QspiPtr->Config.ConnectionMode)
{
case XQSPIPS_CONNECTION_MODE_SINGLE:
FCTIndex = FLASH_CFG_TBL_SINGLE_256_ISSI;
break;
case XQSPIPS_CONNECTION_MODE_PARALLEL:
FCTIndex = FLASH_CFG_TBL_PARALLEL_256_ISSI;
break;
case XQSPIPS_CONNECTION_MODE_STACKED:
FCTIndex = FLASH_CFG_TBL_STACKED_256_ISSI;
break;
default:
FCTIndex = 0;
break;
}
}
if ((((FlashMake == MICRON_ID_BYTE0) || (FlashMake == SPANSION_ID_BYTE0)) &&
(buffer_ptr[3] == MICRON_ID_BYTE2_512)) || ((FlashMake ==
MACRONIX_ID_BYTE0) && (buffer_ptr[3] == MACRONIX_ID_BYTE2_512))) {
switch(QspiPtr->Config.ConnectionMode)
{
case XQSPIPS_CONNECTION_MODE_SINGLE:
FCTIndex = FLASH_CFG_TBL_SINGLE_512_SP + StartIndex;
break;
case XQSPIPS_CONNECTION_MODE_PARALLEL:
FCTIndex = FLASH_CFG_TBL_PARALLEL_512_SP + StartIndex;
break;
case XQSPIPS_CONNECTION_MODE_STACKED:
FCTIndex = FLASH_CFG_TBL_STACKED_512_SP + StartIndex;
break;
default:
FCTIndex = 0;
break;
}
}
/*
* 1Gbit Single connection supported for Spansion.
* The ConnectionMode will indicate stacked as this part has 2 SS
* The device ID will indicate 512Mbit.
* This configuration is handled as the above 512Mbit stacked configuration
*/
/* 1Gbit single, parallel and stacked supported for Micron */
if(((FlashMake == MICRON_ID_BYTE0) &&
(buffer_ptr[3] == MICRON_ID_BYTE2_1G)) ||
((FlashMake == MACRONIX_ID_BYTE0) &&
(buffer_ptr[3] == MACRONIX_ID_BYTE2_1G))) {
switch(QspiPtr->Config.ConnectionMode)
{
case XQSPIPS_CONNECTION_MODE_SINGLE:
FCTIndex = FLASH_CFG_TBL_SINGLE_1GB_MC;
break;
case XQSPIPS_CONNECTION_MODE_PARALLEL:
FCTIndex = FLASH_CFG_TBL_PARALLEL_1GB_MC;
break;
case XQSPIPS_CONNECTION_MODE_STACKED:
FCTIndex = FLASH_CFG_TBL_STACKED_1GB_MC;
break;
default:
FCTIndex = 0;
break;
}
}
xil_printf("FlashID=0x%x 0x%x 0x%x\n\r", buffer_ptr[1], buffer_ptr[2],
buffer_ptr[3]);
return XST_SUCCESS;
}
/*****************************************************************************/
/**
*
* This function performs an I/O read.
*
* @param QspiPtr is a pointer to the QSPI driver component to use.
* @param Address contains the address of the first sector which needs to
* be erased.
* @param ByteCount contains the total size to be erased.
* @param Command is the command used to read data from the flash. Supports
* normal, fast, dual and quad read commands.
* @param Pointer to the write buffer which contains data to be transmitted
* @param Pointer to the read buffer to which valid received data should be
* written
*
* @return none.
*
* @note None.
*
******************************************************************************/
void FlashRead(XQspiPs *QspiPtr, u32 Address, u32 ByteCount, u8 Command,
u8 *WriteBfrPtr, u8 *ReadBfrPtr)
{
u32 RealAddr;
u32 RealByteCnt;
u32 BankSel;
u32 BufferIndex;
u32 TotalByteCnt;
u8 ShiftSize;
/*
* Retain the actual byte count
*/
TotalByteCnt = ByteCount;
while(((signed long)(ByteCount)) > 0) {
/*
* Translate address based on type of connection
* If stacked assert the slave select based on address
*/
RealAddr = GetRealAddr(QspiPtr, Address);
/*
* Select bank
*/
if(Flash_Config_Table[FCTIndex].FlashDeviceSize > SIXTEENMB) {
BankSel = RealAddr/SIXTEENMB;
SendBankSelect(QspiPtr, BankSel);
}
/*
* If data to be read spans beyond the current bank, then
* calculate RealByteCnt in current bank. Else
* RealByteCnt is the same as ByteCount
*/
if((Address & BANKMASK) != ((Address+ByteCount) & BANKMASK)) {
RealByteCnt = (Address & BANKMASK) + SIXTEENMB - Address;
}else {
RealByteCnt = ByteCount;
}
/*
* Setup the write command with the specified address and data for the
* Flash
*/
WriteBfrPtr[COMMAND_OFFSET] = Command;
WriteBfrPtr[ADDRESS_1_OFFSET] = (u8)((RealAddr & 0xFF0000) >> 16);
WriteBfrPtr[ADDRESS_2_OFFSET] = (u8)((RealAddr & 0xFF00) >> 8);
WriteBfrPtr[ADDRESS_3_OFFSET] = (u8)(RealAddr & 0xFF);
if ((Command == FAST_READ_CMD) || (Command == DUAL_READ_CMD) ||
(Command == QUAD_READ_CMD)) {
RealByteCnt += DUMMY_SIZE;
}
/*
* Send the read command to the Flash to read the specified number
* of bytes from the Flash, send the read command and address and
* receive the specified number of bytes of data in the data buffer
*/
XQspiPs_PolledTransfer(QspiPtr, WriteBfrPtr,
&(ReadBfrPtr[TotalByteCnt - ByteCount]),
RealByteCnt + OVERHEAD_SIZE);
/*
* To discard the first 5 dummy bytes, shift the data in read buffer
*/
if((Command == FAST_READ_CMD) || (Command == DUAL_READ_CMD) ||
(Command == QUAD_READ_CMD)){
ShiftSize = OVERHEAD_SIZE + DUMMY_SIZE;
}else{
ShiftSize = OVERHEAD_SIZE;
}
for(BufferIndex = (TotalByteCnt - ByteCount);
BufferIndex < (TotalByteCnt - ByteCount) + RealByteCnt;
BufferIndex++) {
ReadBfrPtr[BufferIndex] = ReadBfrPtr[BufferIndex + ShiftSize];
}
/*
* Increase address to next bank
*/
Address = (Address & BANKMASK) + SIXTEENMB;
/*
* Decrease byte count by bytes already read.
*/
if ((Command == FAST_READ_CMD) || (Command == DUAL_READ_CMD) ||
(Command == QUAD_READ_CMD)) {
ByteCount = ByteCount - (RealByteCnt - DUMMY_SIZE);
}else {
ByteCount = ByteCount - RealByteCnt;
}
}
}
/*****************************************************************************/
/**
*
* This functions selects the current bank
*
* @param QspiPtr is a pointer to the QSPI driver component to use.
* @param Pointer to the write buffer which contains data to be transmitted
* @param BankSel is the bank to be selected in the flash device(s).
*
* @return XST_SUCCESS if bank selected, otherwise XST_FAILURE.
*
* @note None.
*
******************************************************************************/
int SendBankSelect(XQspiPs *QspiPtr, u32 BankSel)
{
u8 WriteBfrPtr[4] = { WRITE_ENABLE_CMD, 0, 0, 0 };
/*
* Bank select commands for Micron and Spansion are different
*/
if(FlashMake == MICRON_ID_BYTE0) {
/*
* For Micron command WREN should be sent first
* except for some specific feature set
*/
XQspiPs_PolledTransfer(QspiPtr, WriteBfrPtr, NULL,
sizeof(u8));
WriteBfrPtr[COMMAND_OFFSET] = EXTADD_REG_WR;
WriteBfrPtr[ADDRESS_1_OFFSET] = BankSel;
/*
* Send the Extended address register write command
* written, no receive buffer required
*/
XQspiPs_PolledTransfer(QspiPtr, WriteBfrPtr, NULL,
BANK_SEL_SIZE);
}
if(FlashMake == SPANSION_ID_BYTE0) {
WriteBfrPtr[COMMAND_OFFSET] = BANK_REG_WR;
WriteBfrPtr[ADDRESS_1_OFFSET] = BankSel;
/*
* Send the Extended address register write command
* written, no receive buffer required
*/
XQspiPs_PolledTransfer(QspiPtr, WriteBfrPtr, NULL,
BANK_SEL_SIZE);
}