-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathxnandps.c
1952 lines (1740 loc) · 51.4 KB
/
xnandps.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
/******************************************************************************
* Copyright (C) 2009 - 2021 Xilinx, Inc. All rights reserved.
* SPDX-License-Identifier: MIT
******************************************************************************/
/*****************************************************************************/
/**
*
* @file xnandps.c
* @addtogroup nandps_v2_7
* @{
*
* This file contains the implementation of the interface functions for
* XNandPs driver. Refer to the header file xnandps.h for more detailed
* information.
*
* This module supports for NAND flash memory devices that conform to the
* "Open NAND Flash Interface" (ONFI) Specification. This modules implements
* basic flash operations like read, write and erase.
*
* @note None
*
* <pre>
* MODIFICATION HISTORY:
*
* Ver Who Date Changes
* ----- ---- ---------- -----------------------------------------------
* 1.00a nm 12/10/2010 First release
* 1.01a nm 28/02/2012 Fixed 16-bit issue with ONFI commands like
* read, write and read status command. The config
* structure width is updated after ONFI query
* with the parameter page width.
* 1.02a nm 20/09/2012 Removed setting of set_cycles and set_opmode
* register values as it is now done in FSBL using
* the PCW generated files. CR#678949.
* 1.03a nm 10/22/2012 Fixed CR# 673348.
* 1.04a nm 04/15/2013 Fixed CR# 704401. Removed warnings when compiled
* with -Wall and -Wextra option in bsp.
* 04/25/2013 Implemented PR# 699544. Added page cache read
* and program support. Added API's XNandPs_ReadCache
* and XNandPs_WriteCache for page cache support.
* Added ECC handling functions XNandPs_EccSetCfg,
* XNandPs_EccSetMemCmd1...etc, to support better
* usage of ECC block for page cache commands.
* Modified Read/Write API's so that there is common
* code for normal read/write and page cache commands.
* Disabling/Re-enabling ECC block in read/write API's
* of spare bytes since we don't calculate ECC for
* spare bytes.
* 2.01 kpc 07/24/2014 Fixed CR#808770. Update command register twice only
* if flash device requires >= four address cycles.
* 2.2 sb 01/31/2015 Use the address cycles defined in onfi parameter
* page than hardcoding this value to 5 for read and
* write operations.
* </pre>
*
******************************************************************************/
/***************************** Include Files *********************************/
#include "xnandps.h"
#include "xnandps_bbm.h"
#include "xnandps_onfi.h"
/************************** Constant Definitions *****************************/
/**************************** Type Definitions *******************************/
/***************** Macros (Inline Functions) Definitions *********************/
/************************** Function Prototypes ******************************/
static int XNandPs_EccHwInit(XNandPs *InstancePtr);
static int XNandPs_EccSwInit(XNandPs *InstancePtr);
static int XNandPs_ReadPage_HwEcc(XNandPs *InstancePtr, u8 *DstPtr);
static int XNandPs_ReadPage(XNandPs *InstancePtr, u8 *DstPtr);
static int XNandPs_WritePage_HwEcc(XNandPs *InstancePtr, u8 *SrcPtr);
static int XNandPs_WritePage(XNandPs *InstancePtr, u8 *SrcPtr);
static int XNandPs_EccCalculate(XNandPs *InstancePtr, u8 *EccData);
static int XNandPs_EccCorrect(u8 *Buf, u8 *EccCalc, u8 *EccCode);
static void XNandPs_ReadBuf(XNandPs *InstancePtr, u8 *Buf, u32 Length);
static void XNandPs_WriteBuf(XNandPs *InstancePtr, u8 *Buf, u32 Length);
static int XNandPs_IsBusy(XNandPs *InstancePtr);
void XNandPs_SendCommand(XNandPs *InstancePtr, XNandPs_CommandFormat
*Command, int Page, int Column);
static void XNandPs_EccSetCfg(XNandPs *InstancePtr, u32 EccConfig);
static void XNandPs_EccSetMemCmd1(XNandPs *InstancePtr, u32 EccCmd);
static void XNandPs_EccSetMemCmd2(XNandPs *InstancePtr, u32 EccCmd);
static void XNandPs_EccDisable(XNandPs *InstancePtr);
/* Bad block management routines */
extern void XNandPs_InitBbtDesc(XNandPs *InstancePtr);
extern int XNandPs_ScanBbt(XNandPs *InstancePtr);
/* ONFI routines */
extern u8 Onfi_CmdReadStatus(XNandPs *InstancePtr);
extern int Onfi_NandInit(XNandPs *InstancePtr);
/************************** Variable Definitions *****************************/
/* ECC data position in the spare data area for different page sizes */
u32 NandOob16[] = {13, 14, 15}; /**< Ecc position for 16 bytes spare area */
u32 NandOob32[] = {26, 27, 28, 29, 30, 31};
/**< Ecc position for 32 bytes spare area */
u32 NandOob64[] = {52, 53, 54, 55, 56, 57,
58, 59, 60, 61, 62, 63};
/**< Ecc position for 64 bytes spare area */
extern XNandPs_CommandFormat OnfiCommands[]; /**< ONFI commands */
/*****************************************************************************/
/**
*
* This function initializes a specific XNandPs device/instance. This function
* must be called prior to using the flash device to read or write any data.
*
* @param InstancePtr is a pointer to the XNandPs instance.
* @param ConfigPtr points to the XNandPs device configuration structure.
* @param SmcBaseAddr is the base address of SMC controller.
* @param FlashBaseAddr is the base address of NAND flash.
*
* @return
* - XST_SUCCESS if successful.
* - XST_FAILURE if fail.
*
* @note The user needs to first call the XNandPs_LookupConfig() API
* which returns the Configuration structure pointer which is
* passed as a parameter to the XNandPs_CfgInitialize() API.
*
******************************************************************************/
int XNandPs_CfgInitialize(XNandPs *InstancePtr, XNandPs_Config *ConfigPtr,
u32 SmcBaseAddr, u32 FlashBaseAddr)
{
u32 PageSize;
int Status;
/*
* Assert the input arguments.
*/
Xil_AssertNonvoid(InstancePtr != NULL);
Xil_AssertNonvoid(ConfigPtr != NULL);
/*
* Set the values read from the device config and the base address.
*/
InstancePtr->Config.DeviceId = ConfigPtr->DeviceId;
InstancePtr->Config.SmcBase = SmcBaseAddr;
InstancePtr->Config.FlashBase = FlashBaseAddr;
InstancePtr->Config.FlashWidth = ConfigPtr->FlashWidth;
XNandPs_WriteReg(InstancePtr->Config.SmcBase +
XNANDPS_MEMC_CLR_CONFIG_OFFSET,
XNANDPS_CLR_CONFIG); /* Disable interrupts */
/*
* ONFI query to get geometry
*/
Status = Onfi_NandInit(InstancePtr);
if (Status != XST_SUCCESS) {
return Status;
}
/*
* Updating Config structure flash width
*/
InstancePtr->Config.FlashWidth = InstancePtr->Geometry.FlashWidth;
/*
* Fill the spare buffer pointer
*/
PageSize = InstancePtr->Geometry.BytesPerPage;
InstancePtr->SpareBufPtr = &InstancePtr->DataBuf[PageSize];
/*
* Initialize ECC Block Parameters
*/
switch (InstancePtr->EccMode)
{
case XNANDPS_ECC_NONE:
/* Fall through */
case XNANDPS_ECC_ONDIE:
/* Bypass the ECC block in the SMC controller */
XNandPs_EccDisable(InstancePtr);
/* Initialize the Read/Write page routines */
InstancePtr->ReadPage = XNandPs_ReadPage;
InstancePtr->WritePage = XNandPs_WritePage;
break;
case XNANDPS_ECC_HW:
/* Use SMC ECC controller ECC block */
Status = XNandPs_EccHwInit(InstancePtr);
if (Status != XST_SUCCESS)
return Status;
/* Initialize ECC SW parameters */
Status = XNandPs_EccSwInit(InstancePtr);
if (Status != XST_SUCCESS)
return Status;
/* Initialize the Read/Write page routines */
InstancePtr->ReadPage = XNandPs_ReadPage_HwEcc;
InstancePtr->WritePage = XNandPs_WritePage_HwEcc;
break;
default:
return XST_FAILURE;
}
/*
* Indicate the instance is now ready to use, initialized without error.
*/
InstancePtr->IsReady = XIL_COMPONENT_IS_READY;
/*
* Scan for the bad block table(bbt) stored in the flash & load it in
* memory(RAM). If bbt is not found, create bbt by scanning factory
* marked bad blocks and store it in last good blocks of flash.
*/
XNandPs_InitBbtDesc(InstancePtr);
Status = XNandPs_ScanBbt(InstancePtr);
if (Status != XST_SUCCESS) {
return Status;
}
return XST_SUCCESS;
}
/*****************************************************************************/
/**
*
* This function initializes the HW ECC block based on flash.
*
* @param InstancePtr is a pointer to the XNandPs instance.
* @param EccConfig is the value of ECC config to update.
*
* @return None
*
* @note None
*
*****************************************************************************/
static void XNandPs_EccSetCfg(XNandPs *InstancePtr, u32 EccConfig)
{
/*
* Check the busy status of the ECC block
*/
while (XNandPs_ReadReg(InstancePtr->Config.SmcBase +
XNANDPS_ECC_STATUS_OFFSET(XNANDPS_IF1_ECC_OFFSET)) &
XNANDPS_ECC_STATUS_MASK);
/*
* Write ECC configuration register
*/
XNandPs_WriteReg(InstancePtr->Config.SmcBase +
(XNANDPS_ECC_MEMCFG_OFFSET(XNANDPS_IF1_ECC_OFFSET)),
EccConfig);
}
/*****************************************************************************/
/**
*
* This function writes ECC MEM CMD1 register with EccCmd value.
*
* @param InstancePtr is a pointer to the XNandPs instance.
* @param EccCmd is register value to write.
*
* @return None
*
* @note None
*
*****************************************************************************/
static void XNandPs_EccSetMemCmd1(XNandPs *InstancePtr, u32 EccCmd)
{
/*
* Set the ECC mem command1 register
*/
XNandPs_WriteReg(InstancePtr->Config.SmcBase +
(XNANDPS_ECC_MEMCMD1_OFFSET(XNANDPS_IF1_ECC_OFFSET)),
EccCmd);
}
/*****************************************************************************/
/**
*
* This function writes ECC MEM CMD2 register with EccCmd value.
*
* @param InstancePtr is a pointer to the XNandPs instance.
* @param EccCmd is register value to write.
*
* @return None
*
* @note None
*
*****************************************************************************/
static void XNandPs_EccSetMemCmd2(XNandPs *InstancePtr, u32 EccCmd)
{
/*
* Set the ECC mem command2 register
*/
XNandPs_WriteReg(InstancePtr->Config.SmcBase +
(XNANDPS_ECC_MEMCMD2_OFFSET(XNANDPS_IF1_ECC_OFFSET)),
EccCmd);
}
/*****************************************************************************/
/**
*
* This function disables ECC block.
*
* @param InstancePtr is a pointer to the XNandPs instance.
*
* @return None
*
* @note None
*
*****************************************************************************/
static void XNandPs_EccDisable(XNandPs *InstancePtr)
{
u32 EccConfig = 0;
/*
* Bypass the ECC block in the SMC controller
*/
EccConfig = XNandPs_ReadReg(InstancePtr->Config.SmcBase +
(XNANDPS_ECC_MEMCFG_OFFSET(XNANDPS_IF1_ECC_OFFSET)));
EccConfig &= ~XNANDPS_ECC_MEMCFG_ECC_MODE_MASK;
XNandPs_EccSetCfg(InstancePtr, EccConfig);
}
/*****************************************************************************/
/**
*
* This function initializes the HW ECC block based on flash.
*
* @param InstancePtr is a pointer to the XNandPs instance.
*
* @return
* - XST_SUCCESS if successful.
* - XST_FAILURE if the flash is not supported.
*
* @note None
*
*****************************************************************************/
static int XNandPs_EccHwInit(XNandPs *InstancePtr)
{
u32 PageSize;
u32 EccConfig = 0;
PageSize = InstancePtr->Geometry.BytesPerPage;
/*
* Set the ECC mem command1 and ECC mem command2 register
*/
XNandPs_EccSetMemCmd1(InstancePtr, XNANDPS_ECC_CMD1);
XNandPs_EccSetMemCmd2(InstancePtr, XNANDPS_ECC_CMD2);
/*
* Configure HW ECC block
*/
switch(PageSize) {
case XNANDPS_PAGE_SIZE_512:
EccConfig = (XNANDPS_ECC_MEMCFG |
XNANDPS_ECC_MEMCFG_PAGE_SIZE_512);
break;
case XNANDPS_PAGE_SIZE_1024:
EccConfig = (XNANDPS_ECC_MEMCFG |
XNANDPS_ECC_MEMCFG_PAGE_SIZE_1024);
break;
case XNANDPS_PAGE_SIZE_2048:
EccConfig = (XNANDPS_ECC_MEMCFG |
XNANDPS_ECC_MEMCFG_PAGE_SIZE_2048);
break;
default:
/*
* Page size 256 bytes & 4096 bytes not supported
* by ECC block
*/
return XST_FAILURE;
}
XNandPs_EccSetCfg(InstancePtr, EccConfig);
return XST_SUCCESS;
}
/*****************************************************************************/
/**
*
* This function initializes the software variables related
* to ECC generation, ECC checking and writing ECC bytes in spare bytes.
*
* @param InstancePtr is a pointer to the XNandPs instance.
*
* @return
* - XST_SUCCESS if successful.
* - XST_FAILURE if the flash is not supported.
*
* @note None
*
*****************************************************************************/
static int XNandPs_EccSwInit(XNandPs *InstancePtr)
{
u32 PageSize;
u32 SpareBytesSize;
u32 Index;
PageSize = InstancePtr->Geometry.BytesPerPage;
SpareBytesSize = InstancePtr->Geometry.SpareBytesPerPage;
/*
* Initialize ECC config structure parameters
*/
InstancePtr->EccConfig.BytesPerBlock = XNANDPS_ECC_BYTES;
InstancePtr->EccConfig.BlockSize = XNANDPS_ECC_BLOCK_SIZE;
InstancePtr->EccConfig.TotalBytes = (PageSize/XNANDPS_ECC_BLOCK_SIZE)
* XNANDPS_ECC_BYTES;
InstancePtr->EccConfig.NumSteps = PageSize/XNANDPS_ECC_BLOCK_SIZE;
/*
* Ecc write position in spare data area as per Linux mtd subsystem
*/
switch(SpareBytesSize) {
case XNANDPS_SPARE_SIZE_16:
for(Index = 0; Index <
InstancePtr->EccConfig.TotalBytes;
Index++) {
InstancePtr->EccConfig.EccPos[Index] =
NandOob16[Index];
}
break;
case XNANDPS_SPARE_SIZE_32:
for(Index = 0; Index <
InstancePtr->EccConfig.TotalBytes;
Index++) {
InstancePtr->EccConfig.EccPos[Index] =
NandOob32[Index];
}
break;
case XNANDPS_SPARE_SIZE_64:
for(Index = 0; Index <
InstancePtr->EccConfig.TotalBytes;
Index++) {
InstancePtr->EccConfig.EccPos[Index] =
NandOob64[Index];
}
break;
default:
return XST_FAILURE;
}
return XST_SUCCESS;
}
/*****************************************************************************/
/**
*
* This function reads the data from the Flash device and copies it into the
* specified user buffer. It doesn't check for the bad blocks while reading
* the flash pages that cross block boundary. User must take care of handling
* bad blocks.
*
* @param InstancePtr is the pointer to the XNandPs instance.
* @param Offset is the flash data address to read from.
* @param Length is number of bytes to read.
* @param DestPtr is the destination address to copy data to.
* @param UserSparePtr is the user buffer to which spare data must be
* copied.
*
* @return
* - XST_SUCCESS if successful.
* - XST_FAILURE if fail.
*
* @note This function reads sequential pages from the Flash device.
*
******************************************************************************/
int XNandPs_Read(XNandPs *InstancePtr, u64 Offset, u32 Length, void *DestPtr,
u8 *UserSparePtr)
{
u32 Page;
u32 Col;
u32 PartialBytes;
u32 NumOfBytes;
int Status;
u32 PartialPageRead = 0;
u32 CopyOffset;
u8 *BufPtr;
u8 *Ptr = (u8 *)DestPtr;
Xil_AssertNonvoid(InstancePtr != NULL);
Xil_AssertNonvoid(DestPtr != NULL);
Xil_AssertNonvoid((Offset + Length) < InstancePtr->Geometry.DeviceSize);
Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
Xil_AssertNonvoid(Length != 0);
Page = (u32) (Offset/InstancePtr->Geometry.BytesPerPage);
Col = (u32) (Offset & (InstancePtr->Geometry.BytesPerPage - 1));
PartialBytes = InstancePtr->Geometry.BytesPerPage - Col;
NumOfBytes = (PartialBytes < Length) ? PartialBytes:Length;
CopyOffset = InstancePtr->Geometry.BytesPerPage - PartialBytes;
/*
* Restore the ECC mem command1 and ECC mem command2 register
* if the previous command is read page cache.
*/
XNandPs_EccSetMemCmd1(InstancePtr, XNANDPS_ECC_CMD1);
XNandPs_EccSetMemCmd2(InstancePtr, XNANDPS_ECC_CMD2);
while (Length) {
/*
* Check if partial read
*/
if (NumOfBytes < InstancePtr->Geometry.BytesPerPage) {
BufPtr = &InstancePtr->DataBuf[0];
PartialPageRead = 1;
} else {
BufPtr = (u8 *)Ptr;
PartialPageRead = 0;
}
/*
* Send the ONFI Read command
*/
XNandPs_SendCommand(InstancePtr, &OnfiCommands[READ], Page, 0);
/*
* Poll the Memory controller status register
*/
while (XNandPs_IsBusy(InstancePtr) == TRUE) {
}
/*
* Clear the interrupt condition
*/
XNandPs_WriteReg((InstancePtr->Config.SmcBase +
XNANDPS_MEMC_CLR_CONFIG_OFFSET),
XNANDPS_MEMC_CLR_CONFIG_INT_CLR1_MASK);
/*
* Read the page data
*/
Status = InstancePtr->ReadPage(InstancePtr, BufPtr);
if (Status != XST_SUCCESS) {
return Status;
}
/*
* Fill the partial data in the buffer
*/
if (PartialPageRead) {
memcpy(Ptr, BufPtr + CopyOffset, NumOfBytes);
}
Ptr += NumOfBytes;
Length -= NumOfBytes;
Page++;
NumOfBytes = (Length > InstancePtr->Geometry.BytesPerPage) ?
InstancePtr->Geometry.BytesPerPage:Length;
CopyOffset = 0;
}
/*
* Copy the spare data to user spare buffer
*/
if (UserSparePtr) {
memcpy(UserSparePtr, InstancePtr->SpareBufPtr,
InstancePtr->Geometry.SpareBytesPerPage);
}
return XST_SUCCESS;
}
/*****************************************************************************/
/**
*
* This function reads the data from the Flash device using read page cache
* command and copies it into the specified user buffer.
* It doesn't check for the bad blocks while reading the flash pages that
* cross block boundary. User must take care of handling bad blocks.
*
* @param InstancePtr is the pointer to the XNandPs instance.
* @param Offset is the flash data address to read from.
* @param Length is number of bytes to read.
* @param DestPtr is the destination address to copy data to.
* @param UserSparePtr is the user buffer to which spare data must be
* copied.
*
* @return
* - XST_SUCCESS if successful.
* - XST_FAILURE if fail.
*
* @note This function reads sequential pages from the Flash device.
*
******************************************************************************/
int XNandPs_ReadCache(XNandPs *InstancePtr, u64 Offset, u32 Length,
void *DestPtr, u8 *UserSparePtr)
{
u32 Page;
u32 Col;
u32 PartialBytes;
u32 NumOfBytes;
int Status;
u32 PartialPageRead = 0;
u32 CopyOffset;
u8 *BufPtr;
u8 *Ptr = (u8 *)DestPtr;
u32 NumPages;
u32 EccConfig = 0;
Xil_AssertNonvoid(InstancePtr != NULL);
Xil_AssertNonvoid(DestPtr != NULL);
Xil_AssertNonvoid((Offset + Length) < InstancePtr->Geometry.DeviceSize);
Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
Xil_AssertNonvoid(Length != 0);
/*
* Check if the flash supports read cache
*/
if (!InstancePtr->Features.ReadCache) {
return XNandPs_Read(InstancePtr, Offset, Length, DestPtr,
UserSparePtr);
}
Page = (u32) (Offset/InstancePtr->Geometry.BytesPerPage);
Col = (u32) (Offset & (InstancePtr->Geometry.BytesPerPage - 1));
PartialBytes = InstancePtr->Geometry.BytesPerPage - Col;
NumOfBytes = (PartialBytes < Length) ? PartialBytes:Length;
CopyOffset = InstancePtr->Geometry.BytesPerPage - PartialBytes;
/*
* Calculate number of pages to read
*/
NumPages = Length/InstancePtr->Geometry.BytesPerPage;
NumPages += (Length % InstancePtr->Geometry.BytesPerPage) ? 1:0;
/*
* Read, Read Cache start, Read Cache end
*/
if (NumPages <= 1) {
return XNandPs_Read(InstancePtr, Offset, Length, DestPtr,
UserSparePtr);
}
/*
* Change ECC commands in ECC registers for page cache support
*/
EccConfig |= ONFI_CMD_PAGE_CACHE_PROGRAM1;
EccConfig |= ONFI_CMD_READ_CACHE_ENHANCED1 << 8;
EccConfig |= ONFI_CMD_READ_CACHE_ENHANCED2 << 16;
EccConfig |= (XNANDPS_ECC_MEMCOMMAND1_RD_CMD_END_VALID_MASK);
XNandPs_EccSetMemCmd1(InstancePtr, EccConfig);
/*
* Send the ONFI Read command
*/
XNandPs_SendCommand(InstancePtr, &OnfiCommands[READ], Page, 0);
/*
* Poll the Memory controller status register
*/
while (XNandPs_IsBusy(InstancePtr) == TRUE) {
}
/*
* Clear the interrupt condition
*/
XNandPs_WriteReg((InstancePtr->Config.SmcBase +
XNANDPS_MEMC_CLR_CONFIG_OFFSET),
XNANDPS_MEMC_CLR_CONFIG_INT_CLR1_MASK);
/*
* Check ONFI Status Register
*/
Status = Onfi_CmdReadStatus(InstancePtr);
if (Status & ONFI_STATUS_FAIL) {
return XST_FAILURE;
}
while (Length && (NumPages > 0)) {
/*
* Check if partial read
*/
if (NumOfBytes < InstancePtr->Geometry.BytesPerPage) {
BufPtr = &InstancePtr->DataBuf[0];
PartialPageRead = 1;
} else {
BufPtr = (u8 *)Ptr;
PartialPageRead = 0;
}
/* Increment the page */
Page++;
if (NumPages <= 1) {
/*
* Change ECC commands in ECC registers to check
* change read column for ECC calculation
*/
EccConfig = 0;
EccConfig |= ONFI_CMD_PAGE_CACHE_PROGRAM1;
EccConfig |= ONFI_CMD_CHANGE_READ_COLUMN1 << 8;
EccConfig |= ONFI_CMD_CHANGE_READ_COLUMN2 << 16;
EccConfig |=
XNANDPS_ECC_MEMCOMMAND1_RD_CMD_END_VALID_MASK;
XNandPs_EccSetMemCmd1(InstancePtr, EccConfig);
/*
* Send NAND page cache end command 0x3F
*/
XNandPs_SendCommand(InstancePtr,
&OnfiCommands[READ_CACHE_END_SEQ],
XNANDPS_PAGE_NOT_VALID,
XNANDPS_COLUMN_NOT_VALID);
} else {
XNandPs_SendCommand(InstancePtr,
&OnfiCommands[READ_CACHE_RANDOM],
Page, 0);
}
/*
* Poll the Memory controller status register
*/
while (XNandPs_IsBusy(InstancePtr) == TRUE) {
}
/*
* Clear the interrupt condition
*/
XNandPs_WriteReg((InstancePtr->Config.SmcBase +
XNANDPS_MEMC_CLR_CONFIG_OFFSET),
XNANDPS_MEMC_CLR_CONFIG_INT_CLR1_MASK);
if (NumPages <= 1) {
XNandPs_SendCommand(InstancePtr,
&OnfiCommands[CHANGE_READ_COLUMN],
XNANDPS_PAGE_NOT_VALID,
0);
}
/*
* Read the page data
*/
Status = InstancePtr->ReadPage(InstancePtr, BufPtr);
if (Status != XST_SUCCESS) {
return Status;
}
/*
* Fill the partial data in the buffer
*/
if (PartialPageRead) {
memcpy(Ptr, BufPtr + CopyOffset, NumOfBytes);
}
Ptr += NumOfBytes;
Length -= NumOfBytes;
NumPages--;
NumOfBytes = (Length > InstancePtr->Geometry.BytesPerPage) ?
InstancePtr->Geometry.BytesPerPage:Length;
CopyOffset = 0;
}
/*
* Copy the spare data to user spare buffer
*/
if (UserSparePtr) {
memcpy(UserSparePtr, InstancePtr->SpareBufPtr,
InstancePtr->Geometry.SpareBytesPerPage);
}
return XST_SUCCESS;
}
/*****************************************************************************/
/**
*
* This function programs the flash device(s) with data specified in the user
* buffer. The source and destination address must be aligned to the width of the
* flash's data bus. It doesn't check for the bad blocks while writing to
* the flash pages that cross block boundary. User must take care of handling
* bad blocks.
*
* @param InstancePtr is the pointer to the XNandPs instance.
* @param Offset is the flash data address to write to.
* @param Length is number of bytes to write.
* @param SrcPtr is the source address to write the data from.
* @param UserSparePtr is the user buffer which contains buffer to write
* into spare data area.
*
* @return
* - XST_SUCCESS if successful.
* - XST_FAILURE if fail.
* - XST_NAND_WRITE_PROTECTED if the flash is write protected.
*
* @note This function writes number of sequential pages into the
* Flash device.
*
******************************************************************************/
int XNandPs_Write(XNandPs *InstancePtr, u64 Offset, u32 Length, void *SrcPtr,
u8 *UserSparePtr)
{
u32 Page;
u32 Col;
u32 PartialBytes;
u32 NumOfBytes;
u32 CopyOffset;
u32 Status;
u8 *BufPtr;
u8 OnfiStatus;
u8 *Ptr = (u8 *)SrcPtr;
Xil_AssertNonvoid(InstancePtr != NULL);
Xil_AssertNonvoid(SrcPtr != NULL);
Xil_AssertNonvoid((Offset + Length) < InstancePtr->Geometry.DeviceSize);
Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
Xil_AssertNonvoid(Length != 0);
/*
* Check if the flash is write protected
*/
OnfiStatus = Onfi_CmdReadStatus(InstancePtr);
if (!(OnfiStatus & ONFI_STATUS_WP)) {
return XST_NAND_WRITE_PROTECTED;
}
/*
* Copy the user spare data buffer
*/
if (UserSparePtr == NULL) {
memset(InstancePtr->SpareBufPtr, 0xff,
InstancePtr->Geometry.SpareBytesPerPage);
} else {
memcpy(InstancePtr->SpareBufPtr, UserSparePtr,
InstancePtr->Geometry.SpareBytesPerPage);
}
Page = (u32) (Offset/InstancePtr->Geometry.BytesPerPage);
Col = (u32) (Offset & (InstancePtr->Geometry.BytesPerPage - 1));
PartialBytes = InstancePtr->Geometry.BytesPerPage - Col;
NumOfBytes = (PartialBytes < Length) ? PartialBytes:Length;
CopyOffset = InstancePtr->Geometry.BytesPerPage - PartialBytes;
while (Length)
{
/*
* Partial write, fill the remaining buffer with 0xff
*/
if (NumOfBytes < InstancePtr->Geometry.BytesPerPage) {
BufPtr = &InstancePtr->DataBuf[0];
memset(BufPtr, 0xff,
InstancePtr->Geometry.BytesPerPage);
memcpy(BufPtr + CopyOffset, Ptr, NumOfBytes);
} else {
BufPtr = (u8 *)Ptr;
}
/*
* Send ONFI Program command
*/
XNandPs_SendCommand(InstancePtr, &OnfiCommands[PAGE_PROGRAM],
Page, 0);
/*
* Write the page data
*/
Status = InstancePtr->WritePage(InstancePtr, BufPtr);
if (Status != XST_SUCCESS) {
return Status;
}
Ptr += NumOfBytes;
Length -= NumOfBytes;
Page++;
NumOfBytes = (Length > InstancePtr->Geometry.BytesPerPage) ?
InstancePtr->Geometry.BytesPerPage:Length;
CopyOffset = 0;
}
return XST_SUCCESS;
}
/*****************************************************************************/
/**
*
* This function programs the flash device(s) with data specified in the user
* buffer using program cache command.
* The source and destination address must be aligned to the width of the
* flash's data bus. It doesn't check for the bad blocks while writing to
* the flash pages that cross block boundary. User must take care of handling
* bad blocks.
*
* @param InstancePtr is the pointer to the XNandPs instance.
* @param Offset is the flash data address to write to.
* @param Length is number of bytes to write.
* @param SrcPtr is the source address to write the data from.
* @param UserSparePtr is the user buffer which contains buffer to write
* into spare data area.
*
* @return
* - XST_SUCCESS if successful.
* - XST_FAILURE if fail.
* - XST_NAND_WRITE_PROTECTED if the flash is write protected.
*
* @note This function writes number of sequential pages into the
* Flash device.
*
******************************************************************************/
int XNandPs_WriteCache(XNandPs *InstancePtr, u64 Offset, u32 Length,
void *SrcPtr, u8 *UserSparePtr)
{
u32 Page;
u32 Col;
u32 PartialBytes;
u32 NumOfBytes;
u32 CopyOffset;
u32 Status;
u8 *BufPtr;
u8 OnfiStatus;
u8 *Ptr = (u8 *)SrcPtr;
u32 NumPages;
Xil_AssertNonvoid(InstancePtr != NULL);
Xil_AssertNonvoid(SrcPtr != NULL);
Xil_AssertNonvoid((Offset + Length) < InstancePtr->Geometry.DeviceSize);
Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
Xil_AssertNonvoid(Length != 0);
/*
* Check if the flash is write protected
*/
OnfiStatus = Onfi_CmdReadStatus(InstancePtr);
if (!(OnfiStatus & ONFI_STATUS_WP)) {
return XST_NAND_WRITE_PROTECTED;
}
/*
* Copy the user spare data buffer
*/
if (UserSparePtr == NULL) {
memset(InstancePtr->SpareBufPtr, 0xff,
InstancePtr->Geometry.SpareBytesPerPage);
} else {
memcpy(InstancePtr->SpareBufPtr, UserSparePtr,
InstancePtr->Geometry.SpareBytesPerPage);
}
Page = (u32) (Offset/InstancePtr->Geometry.BytesPerPage);
Col = (u32) (Offset & (InstancePtr->Geometry.BytesPerPage - 1));
PartialBytes = InstancePtr->Geometry.BytesPerPage - Col;
NumOfBytes = (PartialBytes < Length) ? PartialBytes:Length;
CopyOffset = InstancePtr->Geometry.BytesPerPage - PartialBytes;
/*
* Calculate number of pages to write
*/
NumPages = Length/InstancePtr->Geometry.BytesPerPage;
NumPages += (Length % InstancePtr->Geometry.BytesPerPage) ? 1:0;
/*
* Check for enough pages for cache programming
*/
if (NumPages <= 1) {
return XNandPs_Write(InstancePtr, Offset, Length, SrcPtr,
UserSparePtr);
}
while (Length && (NumPages > 0))
{
/*
* Partial write, fill the remaining buffer with 0xff
*/
if (NumOfBytes < InstancePtr->Geometry.BytesPerPage) {
BufPtr = &InstancePtr->DataBuf[0];
memset(BufPtr, 0xff,
InstancePtr->Geometry.BytesPerPage);
memcpy(BufPtr + CopyOffset, Ptr, NumOfBytes);
} else {
BufPtr = (u8 *)Ptr;
}
if (NumPages > 1) {
/*
* Send ONFI Program cache command
*/
XNandPs_SendCommand(InstancePtr,
&OnfiCommands[PAGE_CACHE_PROGRAM],
Page, 0);
/*
* Write the page data
*/
Status = InstancePtr->WritePage(InstancePtr, BufPtr);
if (Status != XST_SUCCESS) {
return Status;
}
} else {
/*
* Send ONFI Program command