1 /******************************************************************************
3 * Copyright (C) 2015 Xilinx, Inc. All rights reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * Use of the Software is limited solely to applications:
16 * (a) running on a Xilinx device, or
17 * (b) that interact with a Xilinx device through a bus or interconnect.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * XILINX BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
24 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * Except as contained in this notice, the name of the Xilinx shall not be used
28 * in advertising or otherwise to promote the sale, use or other dealings in
29 * this Software without prior written authorization from Xilinx.
31 ******************************************************************************/
32 /*****************************************************************************/
37 * This file implements a driver to support Arasan NAND controller
38 * present in Zynq Ultrascale Mp.
40 * <b>Driver Initialization</b>
42 * The function call XNandPsu_CfgInitialize() should be called by the application
43 * before any other function in the driver. The initialization function takes
44 * device specific data (like device id, instance id, and base address) and
45 * initializes the XNandPsu instance with the device specific data.
47 * <b>Device Geometry</b>
49 * NAND flash device is memory device and it is segmented into areas called
50 * Logical Unit(s) (LUN) and further in to blocks and pages. A NAND flash device
51 * can have multiple LUN. LUN is sequential raw of multiple blocks of the same
52 * size. A block is the smallest erasable unit of data within the Flash array of
53 * a LUN. The size of each block is based on a power of 2. There is no
54 * restriction on the number of blocks within the LUN. A block contains a number
55 * of pages. A page is the smallest addressable unit for read and program
56 * operations. The arrangement of LUN, blocks, and pages is referred to by this
57 * module as the part's geometry.
59 * The cells within the part can be programmed from a logic 1 to a logic 0
60 * and not the other way around. To change a cell back to a logic 1, the
61 * entire block containing that cell must be erased. When a block is erased
62 * all bytes contain the value 0xFF. The number of times a block can be
63 * erased is finite. Eventually the block will wear out and will no longer
64 * be capable of erasure. As of this writing, the typical flash block can
65 * be erased 100,000 or more times.
67 * The jobs done by this driver typically are:
68 * - 8-bit operational mode
69 * - Read, Write, and Erase operation
71 * <b>Write Operation</b>
73 * The write call can be used to write a minimum of one byte and a maximum
74 * entire flash. If the address offset specified to write is out of flash or if
75 * the number of bytes specified from the offset exceed flash boundaries
76 * an error is reported back to the user. The write is blocking in nature in that
77 * the control is returned back to user only after the write operation is
78 * completed successfully or an error is reported.
80 * <b>Read Operation</b>
82 * The read call can be used to read a minimum of one byte and maximum of
83 * entire flash. If the address offset specified to read is out of flash or if
84 * the number of bytes specified from the offset exceed flash boundaries
85 * an error is reported back to the user. The read is blocking in nature in that
86 * the control is returned back to user only after the read operation is
87 * completed successfully or an error is reported.
89 * <b>Erase Operation</b>
91 * The erase operations are provided to erase a Block in the Flash memory. The
92 * erase call is blocking in nature in that the control is returned back to user
93 * only after the erase operation is completed successfully or an error is
96 * @note Driver has been renamed to nandpsu after change in
99 * This driver is intended to be RTOS and processor independent. It works with
100 * physical addresses only. Any needs for dynamic memory management, threads,
101 * mutual exclusion, virtual memory, cache control, or HW write protection
102 * management must be satisfied by the layer above this driver.
105 * MODIFICATION HISTORY:
107 * Ver Who Date Changes
108 * ----- ---- ---------- -----------------------------------------------
109 * 1.0 nm 05/06/2014 First release
110 * 2.0 sb 01/12/2015 Removed Null checks for Buffer passed
111 * as parameter to Read API's
113 * - XNandPsu_ReadPage
115 * - XNandPsu_SetFeature()
116 * - XNandPsu_GetFeature()
117 * and made them public.
118 * Removed Failure Return for BCF Error check in
119 * XNandPsu_ReadPage() and added BCH_Error counter
120 * in the instance pointer structure.
121 * Added XNandPsu_Prepare_Cmd API
123 * - XNandPsu_IntrStsEnable
124 * - XNandPsu_IntrStsClear
125 * - XNandPsu_IntrClear
126 * - XNandPsu_SetProgramReg
127 * with XNandPsu_WriteReg call
128 * Modified xnandpsu.c file API's with above changes.
129 * Corrected the program command for Set Feature API.
131 * - XNandPsu_OnfiReadStatus
132 * - XNandPsu_GetFeature
133 * - XNandPsu_SetFeature
134 * to add support for DDR mode.
135 * Changed Convention for SLC/MLC
139 * Added support for writing BBT signature and version
140 * in page section by enabling XNANDPSU_BBT_NO_OOB.
141 * Removed extra DMA mode initialization from
142 * the XNandPsu_CfgInitialize API.
144 * - XNandPsu_SetEccAddrSize
145 * ECC address now is calculated based upon the
147 * Modified Block Erase API, removed clearing of
148 * packet register before erase.
149 * Clearing Data Interface Register before
150 * XNandPsu_OnfiReset call.
151 * Modified XNandPsu_ChangeTimingMode API supporting
152 * SDR and NVDDR interface for timing modes 0 to 5.
153 * Modified Bbt Signature and Version Offset value for
154 * Oob and No-Oob region.
157 ******************************************************************************/
159 #ifndef XNANDPSU_H /* prevent circular inclusions */
160 #define XNANDPSU_H /* by using protection macros */
166 /***************************** Include Files *********************************/
167 #include "xil_types.h"
170 #include "xil_assert.h"
171 #include "xnandpsu_hw.h"
172 #include "xnandpsu_onfi.h"
173 #include "xil_cache.h"
174 /************************** Constant Definitions *****************************/
176 #define XNANDPSU_DEBUG
178 #define XNANDPSU_MAX_TARGETS 1U /**< ce_n0, ce_n1 */
179 #define XNANDPSU_MAX_PKT_SIZE 0x7FFU /**< Max packet size */
180 #define XNANDPSU_MAX_PKT_COUNT 0xFFFU /**< Max packet count */
182 #define XNANDPSU_PAGE_SIZE_512 512U /**< 512 bytes page */
183 #define XNANDPSU_PAGE_SIZE_2K 2048U /**< 2K bytes page */
184 #define XNANDPSU_PAGE_SIZE_4K 4096U /**< 4K bytes page */
185 #define XNANDPSU_PAGE_SIZE_8K 8192U /**< 8K bytes page */
186 #define XNANDPSU_PAGE_SIZE_16K 16384U /**< 16K bytes page */
187 #define XNANDPSU_PAGE_SIZE_1K_16BIT 1024U /**< 16-bit 2K bytes page */
188 #define XNANDPSU_MAX_PAGE_SIZE 16384U /**< Max page size supported */
190 #define XNANDPSU_BUS_WIDTH_8 0U /**< 8-bit bus width */
191 #define XNANDPSU_BUS_WIDTH_16 1U /**< 16-bit bus width */
193 #define XNANDPSU_HAMMING 0x1U /**< Hamming Flash */
194 #define XNANDPSU_BCH 0x2U /**< BCH Flash */
196 #define XNANDPSU_MAX_BLOCKS 32768U /**< Max number of Blocks */
197 #define XNANDPSU_MAX_SPARE_SIZE 0x800U /**< Max spare bytes of a NAND
200 #define XNANDPSU_INTR_POLL_TIMEOUT 10000U
202 #define XNANDPSU_SDR_CLK ((u16)100U * (u16)1000U * (u16)1000U)
203 #define XNANDPSU_NVDDR_CLK_0 ((u16)20U * (u16)1000U * (u16)1000U)
204 #define XNANDPSU_NVDDR_CLK_1 ((u16)33U * (u16)1000U * (u16)1000U)
205 #define XNANDPSU_NVDDR_CLK_2 ((u16)50U * (u16)1000U * (u16)1000U)
206 #define XNANDPSU_NVDDR_CLK_3 ((u16)66U * (u16)1000U * (u16)1000U)
207 #define XNANDPSU_NVDDR_CLK_4 ((u16)83U * (u16)1000U * (u16)1000U)
208 #define XNANDPSU_NVDDR_CLK_5 ((u16)100U * (u16)1000U * (u16)1000U)
211 * The XNandPsu_Config structure contains configuration information for NAND
215 u16 DeviceId; /**< Instance ID of NAND flash controller */
216 u32 BaseAddress; /**< Base address of NAND flash controller */
220 * The XNandPsu_DataInterface enum contains flash operating mode.
223 XNANDPSU_SDR = 0U, /**< Single Data Rate */
224 XNANDPSU_NVDDR /**< Double Data Rate */
225 } XNandPsu_DataInterface;
228 * XNandPsu_TimingMode enum contains timing modes.
243 } XNandPsu_TimingMode;
246 * The XNandPsu_SWMode enum contains the driver operating mode.
249 XNANDPSU_POLLING = 0, /**< Polling */
250 XNANDPSU_INTERRUPT /**< Interrupt */
254 * The XNandPsu_DmaMode enum contains the controller MDMA mode.
257 XNANDPSU_PIO = 0, /**< PIO Mode */
258 XNANDPSU_SDMA, /**< SDMA Mode */
259 XNANDPSU_MDMA /**< MDMA Mode */
263 * The XNandPsu_EccMode enum contains ECC functionality.
273 * The XNandPsu_BbtOption enum contains the BBT storage option.
276 XNANDPSU_BBT_OOB = 0, /**< OOB area */
277 XNANDPSU_BBT_NO_OOB, /**< No OOB i.e page area */
278 } XNandPsu_BbtOption;
281 * Bad block table descriptor
284 u32 PageOffset[XNANDPSU_MAX_TARGETS];
285 /**< Page offset where BBT resides */
286 u32 SigOffset; /**< Signature offset in Spare area */
287 u32 VerOffset; /**< Offset of BBT version */
288 u32 SigLength; /**< Length of the signature */
289 u32 MaxBlocks; /**< Max blocks to search for BBT */
290 char Signature[4]; /**< BBT signature */
291 u8 Version[XNANDPSU_MAX_TARGETS];
293 u32 Valid; /**< BBT descriptor is valid or not */
294 XNandPsu_BbtOption Option; /**< BBT Oob option enabled/disabled */
301 u32 Options; /**< Options to search the bad block pattern */
302 u32 Offset; /**< Offset to search for specified pattern */
303 u32 Length; /**< Number of bytes to check the pattern */
304 u8 Pattern[2]; /**< Pattern format to search for */
305 } XNandPsu_BadBlockPattern;
308 * The XNandPsu_Geometry structure contains the ONFI geometry information.
312 * Parameter page information
314 u32 BytesPerPage; /**< Number of bytes per page */
315 u16 SpareBytesPerPage; /**< Number of spare bytes per page */
316 u32 PagesPerBlock; /**< Number of pages per block */
317 u32 BlocksPerLun; /**< Number of blocks per LUN */
318 u8 NumLuns; /**< Number of LUN's */
319 u8 RowAddrCycles; /**< Row address cycles */
320 u8 ColAddrCycles; /**< Column address cycles */
321 u8 NumBitsPerCell; /**< Number of bits per cell (Hamming/BCH) */
322 u8 NumBitsECC; /**< Number of bits ECC correctability */
323 u32 EccCodeWordSize; /**< ECC codeword size */
325 * Driver specific information
327 u32 BlockSize; /**< Block size */
328 u32 NumTargetPages; /**< Total number of pages in a Target */
329 u32 NumTargetBlocks; /**< Total number of blocks in a Target */
330 u64 TargetSize; /**< Target size in bytes */
331 u8 NumTargets; /**< Number of targets present */
332 u32 NumPages; /**< Total number of pages */
333 u32 NumBlocks; /**< Total number of blocks */
334 u64 DeviceSize; /**< Total flash size in bytes */
338 * The XNandPsu_Features structure contains the ONFI features information.
349 * The XNandPsu_EccMatrix structure contains ECC features information.
358 } XNandPsu_EccMatrix;
361 * The XNandPsu_EccCfg structure contains ECC configuration.
372 * The XNandPsu structure contains the driver instance data. The user is
373 * required to allocate a variable of this type for the NAND controller.
374 * A pointer to a variable of this type is then passed to the driver API
378 u32 IsReady; /**< Device is initialized and ready */
379 XNandPsu_Config Config;
380 u16 Ecc_Stat_PerPage_flips; /**< Ecc Correctable Error Counter for Current Page */
381 u32 Ecc_Stats_total_flips; /**< Total Ecc Errors Corrected */
382 XNandPsu_DataInterface DataInterface;
383 XNandPsu_TimingMode TimingMode;
384 XNandPsu_SWMode Mode; /**< Driver operating mode */
385 XNandPsu_DmaMode DmaMode; /**< MDMA mode enabled/disabled */
386 XNandPsu_EccMode EccMode; /**< ECC Mode */
387 XNandPsu_EccCfg EccCfg; /**< ECC configuration */
388 XNandPsu_Geometry Geometry; /**< Flash geometry */
389 XNandPsu_Features Features; /**< ONFI features */
390 u8 PartialDataBuf[XNANDPSU_MAX_PAGE_SIZE] __attribute__ ((aligned(64)));
391 /**< Partial read/write buffer */
392 /* Bad block table definitions */
393 XNandPsu_BbtDesc BbtDesc; /**< Bad block table descriptor */
394 XNandPsu_BbtDesc BbtMirrorDesc; /**< Mirror BBT descriptor */
395 XNandPsu_BadBlockPattern BbPattern; /**< Bad block pattern to
397 u8 Bbt[XNANDPSU_MAX_BLOCKS >> 2]; /**< Bad block table array */
400 /******************* Macro Definitions (Inline Functions) *******************/
402 /*****************************************************************************/
404 * This macro sets the bitmask in the register.
406 * @param InstancePtr is a pointer to the XNandPsu instance of the
408 * @param RegOffset is the register offset.
409 * @param BitMask is the bitmask.
411 * @note C-style signature:
412 * void XNandPsu_SetBits(XNandPsu *InstancePtr, u32 RegOffset,
415 *****************************************************************************/
416 #define XNandPsu_SetBits(InstancePtr, RegOffset, BitMask) \
417 XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress, \
419 ((u32)(XNandPsu_ReadReg((InstancePtr)->Config.BaseAddress, \
420 (RegOffset)) | (BitMask))))
422 /*****************************************************************************/
424 * This macro clears the bitmask in the register.
426 * @param InstancePtr is a pointer to the XNandPsu instance of the
428 * @param RegOffset is the register offset.
429 * @param BitMask is the bitmask.
431 * @note C-style signature:
432 * void XNandPsu_ClrBits(XNandPsu *InstancePtr, u32 RegOffset,
435 *****************************************************************************/
436 #define XNandPsu_ClrBits(InstancePtr, RegOffset, BitMask) \
437 XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress, \
439 ((u32)(XNandPsu_ReadReg((InstancePtr)->Config.BaseAddress, \
440 (RegOffset)) & ~(BitMask))))
442 /*****************************************************************************/
444 * This macro clears and updates the bitmask in the register.
446 * @param InstancePtr is a pointer to the XNandPsu instance of the
448 * @param RegOffset is the register offset.
449 * @param Mask is the bitmask.
450 * @param Value is the register value to write.
452 * @note C-style signature:
453 * void XNandPsu_ReadModifyWrite(XNandPsu *InstancePtr,
454 * u32 RegOffset, u32 Mask, u32 Val)
456 *****************************************************************************/
457 #define XNandPsu_ReadModifyWrite(InstancePtr, RegOffset, Mask, Value) \
458 XNandPsu_WriteReg((InstancePtr)->Config.BaseAddress, \
460 ((u32)((u32)(XNandPsu_ReadReg((InstancePtr)->Config.BaseAddress,\
461 (u32)(RegOffset)) & (u32)(~(Mask))) | (u32)(Value))))
463 /*****************************************************************************/
465 * This macro enables bitmask in Interrupt Signal Enable register.
467 * @param InstancePtr is a pointer to the XNandPsu instance of the
469 * @param Mask is the bitmask.
471 * @note C-style signature:
472 * void XNandPsu_IntrSigEnable(XNandPsu *InstancePtr, u32 Mask)
474 *****************************************************************************/
475 #define XNandPsu_IntrSigEnable(InstancePtr, Mask) \
476 XNandPsu_SetBits((InstancePtr), \
477 XNANDPSU_INTR_SIG_EN_OFFSET, \
480 /*****************************************************************************/
482 * This macro clears bitmask in Interrupt Signal Enable register.
484 * @param InstancePtr is a pointer to the XNandPsu instance of the
486 * @param Mask is the bitmask.
488 * @note C-style signature:
489 * void XNandPsu_IntrSigClear(XNandPsu *InstancePtr, u32 Mask)
491 *****************************************************************************/
492 #define XNandPsu_IntrSigClear(InstancePtr, Mask) \
493 XNandPsu_ClrBits((InstancePtr), \
494 XNANDPSU_INTR_SIG_EN_OFFSET, \
497 /*****************************************************************************/
499 * This macro enables bitmask in Interrupt Status Enable register.
501 * @param InstancePtr is a pointer to the XNandPsu instance of the
503 * @param Mask is the bitmask.
505 * @note C-style signature:
506 * void XNandPsu_IntrStsEnable(XNandPsu *InstancePtr, u32 Mask)
508 *****************************************************************************/
509 #define XNandPsu_IntrStsEnable(InstancePtr, Mask) \
510 XNandPsu_SetBits((InstancePtr), \
511 XNANDPSU_INTR_STS_EN_OFFSET, \
514 /*****************************************************************************/
516 * This macro checks for the ONFI ID.
518 * @param Buff is the buffer holding ONFI ID
522 *****************************************************************************/
523 #define IS_ONFI(Buff) \
524 (Buff[0] == (u8)'O') && (Buff[1] == (u8)'N') && \
525 (Buff[2] == (u8)'F') && (Buff[3] == (u8)'I')
527 /************************** Function Prototypes *****************************/
529 s32 XNandPsu_CfgInitialize(XNandPsu *InstancePtr, XNandPsu_Config *ConfigPtr,
532 s32 XNandPsu_Erase(XNandPsu *InstancePtr, u64 Offset, u64 Length);
534 s32 XNandPsu_Write(XNandPsu *InstancePtr, u64 Offset, u64 Length,
537 s32 XNandPsu_Read(XNandPsu *InstancePtr, u64 Offset, u64 Length,
540 s32 XNandPsu_EraseBlock(XNandPsu *InstancePtr, u32 Target, u32 Block);
542 s32 XNandPsu_WriteSpareBytes(XNandPsu *InstancePtr, u32 Page, u8 *Buf);
544 s32 XNandPsu_ReadSpareBytes(XNandPsu *InstancePtr, u32 Page, u8 *Buf);
546 s32 XNandPsu_ChangeTimingMode(XNandPsu *InstancePtr,
547 XNandPsu_DataInterface NewIntf,
548 XNandPsu_TimingMode NewMode);
550 s32 XNandPsu_GetFeature(XNandPsu *InstancePtr, u32 Target, u8 Feature,
553 s32 XNandPsu_SetFeature(XNandPsu *InstancePtr, u32 Target, u8 Feature,
556 s32 XNandPsu_ScanBbt(XNandPsu *InstancePtr);
558 s32 XNandPsu_MarkBlockBad(XNandPsu *InstancePtr, u32 Block);
560 void XNandPsu_EnableDmaMode(XNandPsu *InstancePtr);
562 void XNandPsu_DisableDmaMode(XNandPsu *InstancePtr);
564 void XNandPsu_EnableEccMode(XNandPsu *InstancePtr);
566 void XNandPsu_DisableEccMode(XNandPsu *InstancePtr);
568 void XNandPsu_Prepare_Cmd(XNandPsu *InstancePtr, u8 Cmd1, u8 Cmd2, u8 EccState,
569 u8 DmaMode, u8 AddrCycles);
571 void XNandPsu_EnableBbtOobMode(XNandPsu *InstancePtr);
573 void XNandPsu_DisableBbtOobMode(XNandPsu *InstancePtr);
575 * XNandPsu_LookupConfig in xnandpsu_sinit.c
577 XNandPsu_Config *XNandPsu_LookupConfig(u16 DeviceID);
584 #endif /* XNANDPSU_H end of protection macro */