DrvFlash.h 33.5 KB
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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
/*****************************************************************************
*  Copyright Statement:
*  --------------------
*  This software is protected by Copyright and the information contained
*  herein is confidential. The software may not be copied and the information
*  contained herein may not be used or disclosed except with the written
*  permission of MediaTek Inc. (C) 2005
*
*  BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
*  THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
*  RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
*  AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
*  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
*  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
*  NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
*  SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
*  SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
*  THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
*  NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
*  SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
*
*  BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
*  LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
*  AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
*  OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
*  MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
*
*  THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
*  WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
*  LAWS PRINCIPLES.  ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
*  RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
*  THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
*
*****************************************************************************/

/*******************************************************************************
 *
 * Filename:
 * ---------
 *  FlashConf.C
 *
 * Project:
 * --------
 *  MAUI
 *
 * Description:
 * ------------
 *  This file is used to configure flash device for FAT.
 *
 * Author:
 * -------
 * -------
 *
 *==============================================================================
 *             HISTORY
 * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *------------------------------------------------------------------------------
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 *
 *------------------------------------------------------------------------------
 * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *==============================================================================
 *******************************************************************************/
#ifndef DRVFLASH_H
#define DRVFLASH_H

/*---------------------------------------------
 * Included Header Files
 *---------------------------------------------*/

#include "fs_type.h"
#include "kal_general_types.h"
#include "kal_public_defs.h"
#include "kal_internal_api.h"
#include "custom_memorydevice.h"

// default turn on FDM4 enhancement
#define __NOR_FDM_4_FLIPPING_TOLERABLE__

#ifdef FLASH_DISKDRV_DEBUG
#define ONE_BYTE_ALIGN_ADS
#endif

// MTD driver
#define REGION_NUM 8
#define NON_BLOCK_STATUS  (0x8) //4 bit pattern for version check

//FOR FDM5
#define INVALID_BLOCK_ID 0xffff
#define INVALID_TABLE_ID 0xffff
#define INVALID_LOGICAL_NUM 0xffffff
#define INVALID_NOR_ADDR 0xffffffff
#define INVALID_PHY_PAGE 0xffffffff
#define INVALID_ENTRY 0xffff

//FOR FDM4
#define INVALID_BLOCK_INDEX   0xFFFFFFFF
#define INVALID_SECTOR_INDEX  0xFFFFFFFF
#define INVALID_ERASE_COUNT   0x00FFFFFF // we only have 24 bits
#define MAX_ERASE_COUNT       (INVALID_ERASE_COUNT-1)

/* Global defines */
#define SPARE_RESERVED_RATIO                    (10)
#define MINIMUM_SPARE_AMOUNT                    (2)
#define SLEEP_MODE_SPARE_AMOUNT                 (4)
#define GC_THRESHOLD_DIVISION                   (3)
#define GC_THRESHOLD_GAP                        (1)
#define RESERVED_SECTOR_FOR_MINIMUM_SPARE_SPACE (4) ///< The number of additional reserved sectors if erase queue size is set to the minimum value (2)
#define RESERVED_SECTOR_FOR_REPEATE_POWER_LOSS_DURING_RECLAIM (6) ///< The number of additional reserved sectors if erase queue size is set to the minimum value (2)
#define MINIMUM_SYSTEM_DRIVE_RESERVE_BLOCK      (3) ///< minimum System Drive Reserved Blocks (unit: block)

/******** RESULT for Single Bank ************/
#define RESULT_FLASH_DONE (1)
#define RESULT_FLASH_BUSY (0)
#define RESULT_FLASH_FAIL (-1)

#ifndef __MTK_TARGET__
#pragma pack(1)
#endif
typedef ONE_BYTE_ALIGN_ADS struct
{
   kal_uint32 TotalBlocks;
   kal_uint32 BlockSize[REGION_NUM];
   kal_uint32 AvailInRegion[REGION_NUM];       ///< empty and the number of blocks in erase queue
   kal_uint32 RegionBlocks[REGION_NUM];
   kal_uint32 ActualRegionNum;
   WORD  PageSize;
   kal_uint32 baseUnlockBlock;                 ///< Add because FOTA need to unlock different region in INTEL flash
   kal_uint32 endUnlockBlock;                  ///< Add because FOTA need to unlock different region in INTEL flash
} NOR_MTD_FlashInfo;
#ifndef __MTK_TARGET__
#pragma pack()
#endif

/* add ONE_BYTE_ALIGN_ADS keyword to this struct so that it won't violate ARM's ADS compiler rule.
 * No need, since this structure is included into another ONE_BYTE_ALIGN_ADS structure in pointer form
 */
typedef struct
{
   int     (* MountDevice)  (void * DriveData, void * Info);
   int     (* ShutDown)     (void * DriveData);
   void *  (* MapWindow)    (void * DriveData, kal_uint32 BlockIndex, kal_uint32 WindowIndex);
   int     (* EraseBlock)   (void * DriveData, kal_uint32 BlockIndex);
   int     (* ProgramData)  (void * DriveData, void * Address, void * Data, UINT Length);
   int     (*NonBlockEraseBlock) (void * DriveData, kal_uint32  BlockIndex); /* Added by Eric */
   int     (*CheckDeviceReady)   (void * DriveData, kal_uint32 BlockIndex); /* Added by Eric */
   int     (*SuspendErase)       (void * DriveData, kal_uint32 BlockIndex); /* Added by Eric */
   int     (*ResumeErase)        (void * DriveData, kal_uint32 BlockIndex); /* Added by Eric */
   int     (*BlankCheck)             (void * DriveData, kal_uint32 BlockIndex);
   int     (*OTPAccess)           (void * DriveData, int accesstype, UINT Offset, void * BufferPtr, UINT Length);
   int     (*OTPQueryLength)      (void * DriveData, UINT *Length);
   int     (*LockEraseBlkAddr)         (void * DriveData, void *Address,UINT Action);
   kal_bool (*IsEraseSuspended)     (void * DriveData, kal_uint32 BlockIndex);
   int     (*IOCtrl)        (void * DriveData, UINT CtrlAction, void * CtrlData); // For device IO control
} NOR_MTD_Driver;

/*LockEraseBlkAddr Action*/
#define ACTION_UNLOCK 0
#define ACTION_LOCK 1
#define ACTION_ERASE 2


// This is the file MTD for testing only

typedef struct
{
   const  char * FileName;
   kal_uint32  FileSize;
   kal_uint32  BlockSize;
   kal_uint32  WindowSize;
   HANDLE H;
   BYTE * BaseAddr;
   void * CurrAddr;
} NORMtdFileData;


// Flash Info
typedef struct
{
   kal_uint32 BlockSize;
   kal_uint32 RegionBlocks;
} FlashRegionInfo;
#define EndRegionInfo  {0, 0}

typedef struct
{
   kal_uint32 BankSize;
   kal_uint32 Banks;
} FlashBankInfo;
#define EndBankInfo  {0, 0}

// Erase block Info
/* add ONE_BYTE_ALIGN_ADS keyword to this struct so that it won't violate ARM's ADS compiler rule.
 * No need, since this structure is included into another ONE_BYTE_ALIGN_ADS structure in pointer form
 */
typedef struct
{
   kal_uint32 BlockIndex;
   kal_uint32 EraseCount;
} NOR_EraseInfo;

#if defined(__SINGLE_BANK_NOR_FLASH_SUPPORT__)
// address look up buffer
#define LOOKUP_TABLE_SIZE  (8)
typedef struct
{
   kal_uint32 BlockIndex;
   kal_uint32 BaseAddress;
} NOR_AddrLookup;
#endif

typedef struct
{
   kal_uint32  Signature;
   BYTE * BaseAddr;
   kal_semid          DeviceLock;       // The device lock that keeps driver resource consistent
   kal_taskid         DeviceLockOwner;  // The owner of the deivce lock
   kal_int8           DeviceLockDepth;  // The depth of the deivce lock

   FlashRegionInfo * RegionInfo;
   BYTE * CurrAddr;
   FlashBankInfo *BankInfo;
   BYTE * CurrBankAddr;
   kal_uint32 CurrBankSize;
   BYTE * CurrPollAddr;
#ifndef __MTK_TARGET__
   const char        *FileName;
   void              *flash_device;
   BYTE              *rootMTDcache;
   BYTE              *MTDcache;
   kal_uint32             first;
   kal_uint32             last_WinAddr;
   kal_uint32             last_region;
#endif
#if defined(__SINGLE_BANK_NOR_FLASH_SUPPORT__)
   NOR_AddrLookup  lookup_tbl[LOOKUP_TABLE_SIZE][2];
#endif
} NOR_Flash_MTD_Data;

#ifndef __MTK_TARGET__
#ifndef FLASH_DISKDRV_DEBUG
#define MakeMtdFlashData(a, b)   0x0, 0x000000, b, NULL, "..\\..\\fs\\flash_device.img", NULL, NULL, NULL
#else
#define MakeMtdFlashData(a, b)   0x0, 0x000000, b, NULL, "..\\flash_device.img", NULL, NULL, NULL
#endif
#else
#define MakeMtdFlashData(a, b)   a, b, NULL
#endif

/* Multi-sector Protection Entry */
#ifndef __MTK_TARGET__
#pragma pack(1)
#endif

#if defined(__SINGLE_BANK_NOR_FLASH_SUPPORT__)
/* Erase queue data structure */
typedef ONE_BYTE_ALIGN_ADS struct
{
   kal_uint32 head;
   kal_uint32 tail;
   kal_uint32* queue;
} NOR_EraseQueue;
/* Flash state maintainence */
typedef enum
{
   NOR_IN_READY = 0xF0,
   NOR_IN_PROGRAM,
   NOR_IN_SUSPENDED_PROGRAM,
   NOR_IN_ERASE,
   NOR_IN_SUSPENDED_ERASE,
   NOR_IN_ERROR
} NOR_Flash_State;

extern NOR_Flash_State FlashState;

#define Query_Flash_State(a) \
   {\
      a = FlashState; \
   }

#define Set_Flash_State(a) \
   {\
      FlashState = a; \
   }

#endif /* __SINGLE_BANK_NOR_FLASH_SUPPORT__ */

typedef ONE_BYTE_ALIGN_ADS struct
{
   kal_uint32 LogicalSectorID;
   kal_uint32 PysicalSectorID_new;
} MS_ENTRY;


#if defined(__SINGLE_BANK_NOR_FLASH_SUPPORT__) && !defined(__NOR_IDLE_SWEEP__)
#define __ERASE_QUEUE_ENABLE__
#endif // __SINGLE_BANK_NOR_FLASH_SUPPORT__ && !__NOR_IDLE_SWEEP__

// Flash driver's data
typedef ONE_BYTE_ALIGN_ADS struct NOR_FLASH_DRV_Data
{
   kal_uint32    TotalFATSectors;        ///< Total number of FAT sectors in flash (i.e., TotalPhysicalSectors - reserved sectors)
   NOR_MTD_Driver *MTDDriver;       ///< MTD driver
   void    *MTDData;                ///< MTD data
   WORD    *AvailSectorsInBlock;    ///< The number of SECTOR_AVAIL in each block
   WORD    *ValidSectorsInBlock;    ///< The number of valid sectors (not SECTOR_AVAIL && not SECTOR_DELETED)
   NOR_MTD_FlashInfo FlashInfo;     ///< Flash information
   kal_uint32    HeaderSize[REGION_NUM]; ///< Size (bytes) from the beginning of a block address to the first data sector (i.e., contains both block and sector headers)
   kal_uint32    SectorsPerBlock[REGION_NUM];    ///< Number of "physical" sectors (exclude sectors for header) in each block of the same region
   kal_uint32    ActiveBlock;            ///< Current active block ID
   kal_uint32    ReclaimEraseCount;      ///< Erase count of the reclaimed block
   kal_uint32    ReclaimBlockID;         ///< Block ID of the reclaimed block
   kal_uint32    TotalPhysicalSectors;   ///< Total number of "physical" sector (exclude sectors for header) in flash. (For each region, TotalPhysicalSectors += RegionBlocks[region] * SectorsPerBlock[region])
   kal_uint32    PartitionSectors;       ///< Size of the first FAT partition (unit: sectors)
#if defined(__SINGLE_BANK_NOR_FLASH_SUPPORT__)
   kal_uint32    GCBlockSectors;         ///< The maximum number of sectors of a block in flash (i.e., MaxSectorsPerBlock)
#endif
#ifdef __ERASE_QUEUE_ENABLE__
   kal_uint32    GCHighThreshold;        ///< Middle GC Threshold + GCThresholdDiff
   kal_uint32    GCMiddleThreshold;      ///< Low GC threshold + GCThresholdDiff, GCThresholDiff = (The max capacity of erase queue - GCLowThreshold) / 3
   kal_uint32    GCLowThreshold;         ///< Low GC threshold: 2 blocks (1 for reclaim and 1 for program fail) + sectors for MSP
#else
   kal_uint32    GCThreshold;            ///< GC threshold for Multi-Bank (2 maximum block + sectors for MSP)
#endif /* __ERASE_QUEUE_ENABLE__*/
   kal_uint32    AvailSectors;           ///< Total SECTOR_AVAIL sectors in flash (of course, exclude sectors for headers)
   kal_uint32    DeletedSectors;         ///< The number of deleted sectors in system (Increased in DeletePhysicalSector, MountDevice, ResumeSectorStates)
   kal_uint32    StartSector;            ///< Start "logical sector ID" for write or marking to process
   kal_uint32    Sectors;                ///< Total number of logical sectors to be updated (set in MarkToProcess)
   kal_uint32    SectorsLeft;            ///< The number of logical sectors left to be updated. i.e., The number of sectors whose update bit is set. (set in MarkToProcess and be decreased in MarkProcessed)
   kal_uint32    BLOCKID_OFFSET;         ///< Block offset, calculated by MaxSectorsPerBlock
   kal_uint32    SECTORIDX_MASK;         ///< Mask for retrieve sector index inside a block
   kal_uint32    PHY_SECTOR_OFFSET;      ///< Offset for retriving first 7 bits of PhysicalSector (i.e., Block offset + Local sector offset - 7)

   kal_uint32    MSTABLE_ENTRY_NUM;
   kal_uint32    MS_count;               ///< The number of valid entries in MS Entry Table
   MS_ENTRY *MSEntryTable;
   BYTE    *SectorMap;              ///< Sector map, built in MountDevice
   BYTE    *Buffer;                 ///< FDM Buffer: SIBLEY(1024 bytes) / Others (512 bytes), used when source data is located in the same bank (ESB, movee sectors in ReclaimBlock_pre...)

   void (*CompleteErase)(struct NOR_FLASH_DRV_Data * D);

   kal_uint32    RegionMaxBlock;         ///< (The first) region ID which contains blocks that have maximum number of sectors (to not let such block is selected as active block when the block is the only one left in this region, which makes Program Fail handle properly)

   void    (*ProgramFailHandle)  (struct NOR_FLASH_DRV_Data * D);
   void    (*ReclaimBlock_post)  (struct NOR_FLASH_DRV_Data * D);
#if defined(__SINGLE_BANK_NOR_FLASH_SUPPORT__) || defined(__NOR_IDLE_SWEEP__)
   kal_uint32    IdleReclaimBlockID;     ///< Block ID to be reclaimed in IdleReclaim task (IDLER)
   kal_uint32   (*SelectReclaimTarget)(struct NOR_FLASH_DRV_Data * D);
   kal_uint32   (* ReclaimOneSector)  (struct NOR_FLASH_DRV_Data * D, kal_uint32 Target, kal_uint32 sector);
#endif //__SINGLE_BANK_NOR_FLASH_SUPPORT__ || __NOR_IDLE_SWEEP__

#ifdef __NOR_IDLE_SWEEP__
   kal_uint32    IdleSweepThreshold;     ///< Threshold to trigger Idle Sweep
#endif

#ifdef __ERASE_QUEUE_ENABLE__
   NOR_EraseInfo  *EraseQueue;      ///< Pointer to EraseBlockQueue, default size = 5 (SNOR_ERASE_QUEUE_SIZE)
   // Sleep mode support -- may need to be removed to internal RAM when DCM is enabled
   kal_uint32    processedBankAddr;      ///< Bank address for checking if device is ready (set to MTD.CurrPollAddr in ProcessEraseQueue)
   kal_uint32    processedBlock;         ///< The target block to be erased in sleep mode (set in ProcessEraseQueue only)
   kal_uint32    processedEraseCount;    ///< Erase count of D->processedBlock
   WORD     eraseItems;             ///< The number of items(blocks) in EraseQueue
#endif //__ERASE_QUEUE_ENABLE__

#if defined(__SINGLE_BANK_NOR_FLASH_SUPPORT__)
   WORD     queueSize;              ///< The size of EraseQueue, default size = 5 (SNOR_ERASE_QUEUE_SIZE)
#else
   WORD     SystemDriveReservedUnits;   ///< The number of reserved unit (1 unit = 0.5 block), default size = 6 (NOR_SYSTEM_DRIVE_RESERVED_BLOCK * 2)
#endif /* __SINGLE_BANK_NOR_FLASH_SUPPORT__ */

#ifdef __NOR_IDLE_SWEEP__
   BYTE          BlockReclaimingMark;
#endif
   kal_bool    ProgramFailRetry;

#ifdef __ERASE_QUEUE_ENABLE__
   kal_bool IsEraseSuspended;       ///< A flag to indicate whether a erase operation issued by sleep mode is suspended (only set to TRUE in SuspendEraseQueue)
#endif //__ERASE_QUEUE_ENABLE__


} NOR_FLASH_DRV_Data;

#ifdef __MTK_TARGET__

extern kal_mutexid fdm_reclaim_mutex;
extern kal_eventgrpid  nor_egid;
#define NOR_BRECL_EVENT 0x1
#define NOR_FRECL_EVENT 0x2
#define NOR_DMAN_EVENT 0x4

#endif

/* Flash Bank Info */
typedef struct
{
   kal_uint32 BankSize;
   kal_uint32 Banks;
} NORBankInfo;
#define EndBankInfo  {0, 0}

/* Flash Layout Info */
typedef struct
{
   kal_uint32 TotalLSMT;
   kal_uint32 BlkSize;
   kal_uint32 PageSize;
   NORBankInfo *BankInfo;
   WORD TblEntrys;
   WORD TotalBlks;
} NORLayoutInfo;

typedef struct
{
   kal_uint32  Signature;
   BYTE * BaseAddr;
   kal_semid          DeviceLock;       // The device lock that protects driver resource
   kal_taskid         DeviceLockOwner;  // The owner of the deivce lock
   kal_int8           DeviceLockDepth;  // The depth of the deivce lock

   NORLayoutInfo * LayoutInfo;
#ifndef __MTK_TARGET__
   const char        *FileName;
   void              *flash_device;
   BYTE              *rootMTDcache;
   BYTE              *MTDcache;
   kal_uint32             modify;                    //first time, don't need to write to file
   kal_uint32             last_WinAddr;    // next address calling mapwindow
   kal_uint32             last_region;        //next region calling mapwindow
#endif
} NOR_MTD_DATA;

typedef ONE_BYTE_ALIGN_ADS struct
{
   kal_uint32 MSCount;
   kal_uint32 *LogPageID;
   WORD *NewEntryID;
   WORD *OldEntryID;
} MS_TABLE;

typedef ONE_BYTE_ALIGN_ADS struct
{
   WORD LogBlkID;
   WORD TblIDInBlk;
} LSMGEntry;

//flash driver data for FDM5.0
typedef ONE_BYTE_ALIGN_ADS struct
{
   kal_uint32    TotalFATSectors;
   NOR_MTD_Driver * MTDDriver;
   void *      MTDData;
   int  (* DiscardSectors) (void * DriveData, UINT Sector, UINT Sectors);
   void (*ReclaimBlock) (void* DriveData, kal_bool isBackground);
   #if defined(__SERIAL_FLASH_EN__)
   void    (*ProgramFailHandle)  (void* DriveData);
   NOR_MTD_FlashInfo FlashInfo;     ///< Flash information
   #endif
   kal_uint32    SecondPartitionSectors;
   kal_uint32    HeaderSize[2];  //HeaderSize[0]: data block header size
   //HeaderSize[1]: table block header size
   kal_uint32    *InvalidEntryInTblBlk; //valid entry in table block
   kal_uint32    TotalAvail[2];   // TotalAvail[0]: total available pages in data block
   //TotalAvail[0]: total available tables in table block
   //not include empty block (2 spare block)
   kal_uint32    TotalInvalid[2]; // TotalInvalid[0]: total invalid pages in data block
   //TotalInvalid[0]: total invalid tables in table block
   kal_uint32    ReclHighThreshold[2]; //unit ReclHighThreshold[0]: Pages ReclHighThreshold[1]: Tables
   kal_uint32    ReclLowThreshold[2];
   kal_uint32    SystemThreshold[2];
   kal_bool   NeedResumeFlag;
   kal_bool   NeedFRecl;
   LSMGEntry    *LSMG;            // logical sector mapping group table
   WORD      ReclType;
   WORD      *LBM;              //logical block mapping table
   WORD      *AvailInBlk;     //available page in data block, available table in table block
   WORD      *InvalidInBlk;     //valid  page in data block, valid table in table block
   WORD      ActiveBlk[2];    // ActiveBlk[0]: active data block
   //ActiveBlk[1]: active table block
   WORD      ReclLogBlkID;  //ReclLogBlkID == INVALID_BLOCK_ID means no block need reclamation
   WORD      ErasingPhyBlk;
   WORD      ReclToPhyBlkID;
   WORD      ReclFromPhyBlkID;
   WORD      PageSizeShift;
   WORD      BlkIDShift;
   WORD      TblSizeShift;
   WORD      TblIDShift;
   WORD      PagesPerBlk;
   WORD      TblsPerBlk;
   WORD      TblBlks;
   WORD      SetTblBlks;
   WORD      DataBlks;
   kal_uint32 MSTABLE_ENTRY_NUM;
   MS_TABLE  MSTable;
   BYTE      *Buffer;
   BYTE      *CopyBuffer;
} NOR_FTL_DATA;


#ifndef __MTK_TARGET__
#pragma pack()
#endif

typedef struct
{
   kal_uint32 Blocks;
   kal_uint32 BlockSize;
   kal_uint32 EraseCountMax;
   kal_uint32 EraseCountMin;
   kal_uint32 EraseCountAverage;
   kal_uint32 SectorsInUse;
   kal_uint32 SectorsDeleted;
   kal_uint32 SectorsAvail;
} NORFlashInfo;

/* Definition for power loss test */
#if defined(FLASH_DISKDRV_DEBUG) && defined(POWERLOSS_TEST)
#include <setjmp.h>
extern jmp_buf mark;
extern int Test_CD_Value;
#ifndef __NOR_FDM5__
#if defined(__SINGLE_BANK_NOR_FLASH_SUPPORT__)
enum
{
   /* 1*/WriteSector_SECTOR_WRITING = 1,
   /* 2*/WriteSector_SECTOR_WRITTEN,
   /* 3*/WriteSector_SECTOR_DELETED,
   /* 4*/WriteSector_SECTOR_VALID,
   /* 5*/WriteSector_SECTOR_MS_WRITTEN,
   /* 6*/ReclaimBlock_pre_BLOCK_RECLAIMING,
   /* 7*/ReclaimBlock_pre_BLOCK_RECLAIMED,
   /* 8*/ReclaimBlock_pre_BLOCK_VIRGINE,
   /* 9*/ReclaimBlock_post_BLOCK_EMPTY,
   /*10*/SetActiveBlock_BLOCK_FULL,
   /*11*/SetActiveBlock_BLOCK_ACTIVE,
   /*12*/ResumeSectorStates_SECTOR_MS_BEINGVALIDATED,
   /*13*/WriteSector_SECTOR_MOVING,
   /*14*/WriteSectors_Validate_MS_Sector,
   /*15*/WriteSectors_Validate_MS_VALID_Sector,
   /*16*/ReclaimEraseQueueItem_BLOCK_VIRGINE,
   /*17*/IdleReclaimBlocks_BLOCK_RECLAIMING,
   /*18*/IdleReclaimBlocks_BLOCK_RECLAIMING_COPYSECTOR,
   /*19*/IdleReclaimBlocks_BLOCK_VIRGINE,
   /*20*/IdleReclaimBlocks_RECALIM_ONE_MORE_BLOCK,
   /*21*/IdleReclaimBlocks_BLOCK_RECLAIMED
};

#define TEST_POINT(a)\
   {\
      switch(a)\
      {\
         case WriteSector_SECTOR_MS_WRITTEN:\
         case ReclaimBlock_pre_BLOCK_RECLAIMING:\
         case ReclaimBlock_pre_BLOCK_RECLAIMED:\
         case ReclaimBlock_pre_BLOCK_VIRGINE:\
         case ReclaimBlock_post_BLOCK_EMPTY:\
         case SetActiveBlock_BLOCK_FULL:\
         case SetActiveBlock_BLOCK_ACTIVE:\
         case WriteSectors_Validate_MS_Sector:\
            Test_CD_Value -= 50;\
            break;\
         case ResumeSectorStates_SECTOR_MS_BEINGVALIDATED:\
            Test_CD_Value -= 50;\
            break;\
         case IdleReclaimBlocks_BLOCK_RECLAIMING:\
         case IdleReclaimBlocks_BLOCK_RECLAIMING_COPYSECTOR:\
         case IdleReclaimBlocks_BLOCK_VIRGINE:\
         case IdleReclaimBlocks_RECALIM_ONE_MORE_BLOCK:\
         case IdleReclaimBlocks_BLOCK_RECLAIMED:\
            Test_CD_Value -= 50;\
            break;\
         case WriteSectors_Validate_MS_Sector:\
         case WriteSectors_Validate_MS_VALID_Sector:\
            Test_CD_Value -= 50;\
            break;\
         default:\
            Test_CD_Value--;\
            break;\
      }\
      if(Test_CD_Value <= 0)\
         longjmp(mark, a);\
   }
#else /*!__SINGLE_BANK_NOR_FLASH_SUPPORT__*/
enum
{
   /* 1*/WriteSector_SECTOR_WRITING = 1,
   /* 2*/WriteSector_SECTOR_WRITTEN,
   /* 3*/WriteSector_SECTOR_DELETED,
   /* 4*/WriteSector_SECTOR_VALID,
   /* 5*/WriteSector_SECTOR_MS_WRITTEN,
   /* 6*/ReclaimBlock_pre_BLOCK_RECLAIM,
   /* 7*/ReclaimBlock_pre_BLOCK_VIRGINE,
   /* 8*/ReclaimBlock_post_BLOCK_EMPTY,
   /* 9*/SetActiveBlock_BLOCK_FULL,
   /*10*/SetActiveBlock_BLOCK_ACTIVE,
   /*11*/ResumeSectorStates_SECTOR_MS_BEINGVALIDATED,
   /*12*/WriteSector_SECTOR_MOVING,
   /*13*/WriteSectors_Validate_MS_Sector
};

#define TEST_POINT(a)\
   {\
      switch(a)\
      {\
         case WriteSector_SECTOR_MS_WRITTEN:\
         case ReclaimBlock_pre_BLOCK_RECLAIM:\
         case ReclaimBlock_pre_BLOCK_VIRGINE:\
         case ReclaimBlock_post_BLOCK_EMPTY:\
         case SetActiveBlock_BLOCK_FULL:\
         case SetActiveBlock_BLOCK_ACTIVE:\
         case WriteSectors_Validate_MS_Sector:\
            Test_CD_Value -= 50;\
            break;\
         case ResumeSectorStates_SECTOR_MS_BEINGVALIDATED:\
            Test_CD_Value -= 50;\
            break;\
         default:\
            Test_CD_Value--;\
            break;\
      }\
      if(Test_CD_Value <= 0)\
         longjmp(mark, a);\
   }
#endif /*__SINGLE_BANK_NOR_FLASH_SUPPORT__*/
#else /*FDM5*/
enum
{
   /* 1*/ReclaimBlock_MARK_BLOCK_RECLAIM = 1,
   /* 2*/ReclaimBlock_MARK_BLOCK_COPYING,
   /* 3*/ReclaimBlock_TABLE_BLOCK_COPYING,
   /* 4*/ReclaimBlock_DATA_BLOCK_COPYING,
   /* 5*/ReclaimBlock_MARK_BLOCK_ERASING,
   /* 6*/ReclaimBlock_AFTER_ERASED,
   /* 7*/CopyTable_MARK_COPYING,
   /* 8*/CopyTable_MARK_ALLOCATING,
   /* 9*/CopyTable_MARK_VALID,
   /*10*/WritePage_MARK_TABLE_ENTRY_WRITING,
   /*11*/WritePage_MARK_TABLE_ENTRY_WRITTEN,
   /*12*/WritePage_MARK_PAGE_VALID,
   /*13*/WritePage_MARD_TABLE_ENTRY_MS_WRITING,
   /*14*/WritePage_MARD_TABLE_ENTRY_MS_BEGIN_VALID,
   /*15*/SetActiveBlock_BLOCK_FULL,
   /*16*/SetActiveBlock_BLOCK_ACTIVE
};


#define TEST_POINT(a)\
   {\
      switch(a)\
      {\
         case ReclaimBlock_MARK_BLOCK_RECLAIM:\
         case ReclaimBlock_MARK_BLOCK_COPYING:\
         case ReclaimBlock_TABLE_BLOCK_COPYING:\
         case ReclaimBlock_DATA_BLOCK_COPYING:\
         case ReclaimBlock_MARK_BLOCK_ERASING:\
         case ReclaimBlock_AFTER_ERASED:\
         case CopyTable_MARK_COPYING:\
         case CopyTable_MARK_ALLOCATING:\
         case CopyTable_MARK_VALID:\
         case WritePage_MARK_TABLE_ENTRY_WRITING:\
         case WritePage_MARK_TABLE_ENTRY_WRITTEN:\
         case WritePage_MARK_PAGE_VALID:\
         case WritePage_MARD_TABLE_ENTRY_MS_WRITING:\
         case WritePage_MARD_TABLE_ENTRY_MS_BEGIN_VALID:\
         case SetActiveBlock_BLOCK_FULL:\
         case SetActiveBlock_BLOCK_ACTIVE:\
            Test_CD_Value -= 50;\
            break;\
         default:\
            Test_CD_Value--;\
            break;\
      }\
      if(Test_CD_Value <= 0)\
         longjmp(mark, a);\
   }
#endif /*__NOR_FDM5__*/
#define START_POINT  setjmp(mark)
#else
#define TEST_POINT(a)
#define START_POINT
#endif /* FLASH_DISKDRV_DEBUG */


/* Function Prototype */
int NOR_MountDevice(void * DriveData, int DeviceNumber, int DeviceType, kal_uint32 Flags);
int NOR_ShutDown(void * DriveData);
int NOR_ReadSectors(void * DriveData, kal_uint32 Sector, UINT Sectors, void * Buffer);
int NOR_WriteSectors(void * DriveData, kal_uint32 Sector, UINT Sectors, void * Buffer);
int NOR_MediaChanged(void * DriveData);
int NOR_DiscardSectors(void * DriveData, kal_uint32 Sector, UINT Sectors);
int NOR_GetDiskGeometry(void * DriveData, FS_PartitionRecord * DiskGeometry, BYTE * MediaDescriptor);
int NOR_LowLevelFormat(void * DriveData, const char * DeviceName, FS_FormatCallback Progress, kal_uint32 Flags);
int NOR_NonBlockWriteSectors(void * DriveData, kal_uint32 Sector, UINT Sectors, void * Buffer);
int NOR_RecoverableWriteSectors(void * DriveData, kal_uint32 Sector, UINT Sectors, void * Buffer);
int NOR_ResumeSectorStates(void * DriveData);

int SIB_MountDevice(void * DriveData, int DeviceNumber, int DeviceType, kal_uint32 Flags);
int SIB_ReadSectors(void * DriveData, kal_uint32 Sector, UINT Sectors, void * Buffer);
int SIB_WriteSectors(void * DriveData, kal_uint32 Sector, UINT Sectors, void * Buffer);
int SIB_DiscardSectors(void * DriveData, kal_uint32 Sector, UINT Sectors);
int SIB_GetDiskGeometry(void * DriveData, FS_PartitionRecord * DiskGeometry, BYTE * MediaDescriptor);
int SIB_LowLevelFormat(void * DriveData, const char * DeviceName, FS_FormatCallback Progress, kal_uint32 Flags);
int SIB_NonBlockWriteSectors(void * DriveData, kal_uint32 Sector, UINT Sectors, void * Buffer);
int SIB_RecoverableWriteSectors(void * DriveData, kal_uint32 Sector, UINT Sectors, void * Buffer);
int SIB_ResumeSectorStates(void * DriveData);

int nNOR_MountDevice(void * DriveData, int DeviceNumber, int DeviceType, kal_uint32 Flags);
int nNOR_ReadSectors(void * DriveData, kal_uint32 Sector, UINT Sectors, void * Buffer);
int nNOR_WriteSectors(void * DriveData, kal_uint32 Sector, UINT Sectors, void * Buffer);
int nNOR_MediaChanged(void * DriveData);
int nNOR_DiscardSectors(void * DriveData, kal_uint32 Sector, UINT Sectors);
int nNOR_LowLevelFormat(void * DriveData, const char * DeviceName, FS_FormatCallback Progress, kal_uint32 Flags);
int nNOR_NonBlockWriteSectors(void * DriveData, kal_uint32 Sector, UINT Sectors, void * Buffer);
int nNOR_RecoverableWriteSectors(void * DriveData, kal_uint32 Sector, UINT Sectors, void * Buffer);
int nNOR_ResumeSectorStates(void * DriveData);
void nNOR_ReclaimBlock(void* DriveData, kal_bool isBackground);

int nSIB_MountDevice(void * DriveData, int DeviceNumber, int DeviceType, kal_uint32 Flags);
int nSIB_ReadSectors(void * DriveData, kal_uint32 Sector, UINT Sectors, void * Buffer);
int nSIB_WriteSectors(void * DriveData, kal_uint32 Sector, UINT Sectors, void * Buffer);
int nSIB_DiscardSectors(void * DriveData, kal_uint32 Sector, UINT Sectors);
int nGetDiskGeometry(void * DriveData, FS_PartitionRecord * DiskGeometry, BYTE * MediaDescriptor);
int nSIB_LowLevelFormat(void * DriveData, const char * DeviceName, FS_FormatCallback Progress, kal_uint32 Flags);
int nSIB_NonBlockWriteSectors(void * DriveData, kal_uint32 Sector, UINT Sectors, void * Buffer);
int nSIB_RecoverableWriteSectors(void * DriveData, kal_uint32 Sector, UINT Sectors, void * Buffer);
int nSIB_ResumeSectorStates(void * DriveData);
void nSIB_ReclaimBlock(void* DriveData, kal_bool isBackground);
int nShutDown(void * DriveData);

int NOR_MountDevice_ext(void * DriveData, int DeviceNumber, int DeviceType, kal_uint32 Flags);
int NOR_ShutDown_ext(void * DriveData);
int NOR_ReadSectors_ext(void * DriveData, kal_uint32 Sector, UINT Sectors, void * Buffer);
int NOR_WriteSectors_ext(void * DriveData, kal_uint32 Sector, UINT Sectors, void * Buffer);
int NOR_MediaChanged_ext(void * DriveData);
int NOR_DiscardSectors_ext(void * DriveData, kal_uint32 Sector, UINT Sectors);
int NOR_GetDiskGeometry_ext(void * DriveData, FS_PartitionRecord * DiskGeometry, BYTE * MediaDescriptor);
int NOR_LowLevelFormat_ext(void * DriveData, const char * DeviceName, FS_FormatCallback Progress, kal_uint32 Flags);
int NOR_NonBlockWriteSectors_ext(void * DriveData, kal_uint32 Sector, UINT Sectors, void * Buffer);
int NOR_RecoverableWriteSectors_ext(void * DriveData, kal_uint32 Sector, UINT Sectors, void * Buffer);
int NOR_ResumeSectorStates_ext(void * DriveData);

int OTPAccess(void * DriveData, int accesstype, UINT Offset, void * BufferPtr, UINT Length);
int OTPQueryLength(void * DriveData, UINT *Length);


// internal function for different version of FDM

#if defined(__SINGLE_BANK_NOR_FLASH_SUPPORT__)
extern void ESB_CompleteErase(NOR_FLASH_DRV_Data * D);
#else //!__SINGLE_BANK_NOR_FLASH_SUPPORT__
extern void MB_CompleteErase(NOR_FLASH_DRV_Data * D);
#endif //__SINGLE_BANK_NOR_FLASH_SUPPORT__


#if defined(__SINGLE_BANK_NOR_FLASH_SUPPORT__) || defined(__NOR_IDLE_SWEEP__)
extern void GetFDMLock(void);
extern void FreeFDMLock(void);
#define retriveFDMLock GetFDMLock
#define releaseFDMLock FreeFDMLock
#else // !__SINGLE_BANK_NOR_FLASH_SUPPORT__ && !__NOR_IDLE_SWEEP__
extern void FDM_LOCK(void);
extern void FDM_UNLOCK(void);
#define retriveFDMLock FDM_LOCK
#define releaseFDMLock FDM_UNLOCK

#endif // __SINGLE_BANK_NOR_FLASH_SUPPORT__ || __NOR_IDLE_SWEEP__


#if defined(__NOR_IDLE_SWEEP__)
extern void setIdleSweepThreshold(NOR_FLASH_DRV_Data * D, kal_uint32 MaxSectorsPerBlock); //flash_mtd.c
#endif



#if ( defined(__NOR_FLASH_HARDWARE_TEST__) && defined(__MAUI_BASIC__) && !defined(__UBL__))
kal_uint32 SIB_LocateSector(NOR_FLASH_DRV_Data * D, kal_uint32 LogicalSector);
kal_uint32 LocateSector(NOR_FLASH_DRV_Data * D, kal_uint32 LogicalSector);
#endif

// Use fatal error handler instead assert, because assert will write data to FAT which is not suitable when flash error happens
// write a special code: KAL_ERROR_FILESYS_NOR_FDM_INTERNAL_FAILED  to kal_fatal_error_handler to not do any FAT write during fatal error
#if (defined __UBL__) || (defined __FUE__)
   #define NORFDM_EXT_ASSERT(expr, e1, e2, e3) EXT_ASSERT(expr, e1, e2, e3)
   #define NORFDM_ASSERT(expr) ASSERT(expr)
#else
   #define NORFDM_EXT_ASSERT(expr, e1, e2, e3) {if(!(expr)) {kal_fatal_error_handler(KAL_ERROR_FILESYS_NOR_FDM_INTERNAL_FAILED, __current_pc());}}
   #if defined(DEBUG_KAL)
      #define NORFDM_ASSERT(x) {if(!(x)) {kal_fatal_error_handler(KAL_ERROR_FILESYS_NOR_FDM_INTERNAL_FAILED, __current_pc());}}
   #else
      #define NORFDM_ASSERT(x)
   #endif
#endif // __UBL__ || __FUE__


#endif   /* !DRVFLASH_H */