NAND Flash

 1 /*
 2  *  Copyright 2005 by Spectrum Digital Incorporated.
 3  *  All rights reserved. Property of Spectrum Digital Incorporated.
 4  *
 5  *  Not for distribution.
 6  */
 7 
 8 /*
 9  *  NAND Flash Test
10  *
11  */
12 
13 #include "stdio.h"
14 #include "davincievm.h"
15 
16 /* ------------------------------------------------------------------------ *
17  *                                                                          *
18  *  Testing Function                                                        *
19  *                                                                          *
20  * ------------------------------------------------------------------------ */
21 void TEST_execute( Int16 ( *funchandle )( ), char *testname, Int16 ledmask )
22 {
23     Int16 status;
24 
25     /* Display test ID */
26     printf( "%02d  Testing %s...\n", ledmask, testname );
27 
28     /* Call test function */
29     status = funchandle( );
30 
31     /* Check for test fail */
32     if ( status != 0 )
33     {
34         /* Print error message */
35         printf( "     FAIL... error code %d... quitting\n", status, testname );
36 
37         /* Software Breakpoint to Code Composer */
38         exit(0);
39     }
40     else
41     {
42         /* Print error message */
43         printf( "    PASS\n", testname );
44     }
45 }
46 
47 extern Int16 nandflash_test( );
48 
49 /* ------------------------------------------------------------------------ *
50  *                                                                          *
51  *  main( )                                                                 *
52  *                                                                          *
53  * ------------------------------------------------------------------------ */
54 void main( void )
55 {
56     /* Initialize BSL */
57     DAVINCIEVM_init( );
58 
59     TEST_execute( nandflash_test,    "NAND Flash", 1 );
60 
61     printf( "\n***ALL Tests Passed***\n" );
62 }


  1. /*  
  2.  *  Copyright 2005 by Spectrum Digital Incorporated.  
  3.  *  All rights reserved. Property of Spectrum Digital Incorporated.  
  4.  *  
  5.  *  Not for distribution.  
  6.  */   
  7.    
  8. /*  
  9.  *  NAND Flash implementation  
  10.  *  
  11.  */   
  12. #define NANDF1ECC *( volatile Uint32* )0x01E00070   
  13. #define NANDFCR *( volatile Uint32* )0x01E00060   
  14.    
  15. #include "davincievm_nandflash.h"   
  16. typedef union {   
  17.     Uint8 *cp;   
  18.     Uint16 *wp;   
  19.     Uint32 *lp;   
  20. } FLASHPtr;   
  21.    
  22. Uint8 *flash_make_addr (Uint32 baseAddr, Uint32 offset)   
  23. {   
  24.     return ((Uint8 *) ( baseAddr + offset ));   
  25. }   
  26.    
  27. void flash_swap_data(Uint32* data)   
  28. {   
  29.     Uint32 i,temp = *data;   
  30.     volatile FLASHPtr  dataAddr, tempAddr;   
  31.        
  32.     dataAddr.cp = flash_make_addr((Uint32) data, 3);   
  33.     tempAddr.cp = flash_make_addr((Uint32) &temp,0);           
  34.           
  35.     for(i=0; i<4; i++)   
  36.         *dataAddr.cp-- = *tempAddr.cp++;   
  37. }   
  38.    
  39. Uint32 NAND_ECCReadAndRestart (void)   
  40. {   
  41.     Uint32 retval;   
  42.     // Read and mask appropriate (based on CSn space flash is in) ECC regsiter   
  43.     retval = NANDF1ECC & (0x0FFF0FFF);   
  44.     // Write appropriate bit to start ECC calcualtions    
  45.     NANDFCR|=(1<<8);      
  46.     return retval;   
  47. }   
  48. /* ------------------------------------------------------------------------ *  
  49.  *                                                                          *  
  50.  *  NANDFLASH_waitWhileBusy( timeout )                                      *  
  51.  *                                                                          *  
  52.  *      Poll the external RDY/BSY pin                                       *  
  53.  *                                                                          *  
  54.  * ------------------------------------------------------------------------ */   
  55. Int16 NANDFLASH_waitWhileBusy( Uint32 timeout )   
  56. {   
  57.     Uint32 timecount = 0;   
  58.    
  59.     /*  
  60.      * Short delay to let Ready/Busy signal go low  
  61.      */   
  62.     DAVINCIEVM_wait( 1000 );   
  63.    
  64.     /*  
  65.      *  Wait while the device is busy  
  66.      */   
  67.     while( ( ( ! NANDFLASH_READ_RB ) && ( ++timecount  timeout ) ) );   
  68.    
  69.     if ( timecount == timeout )   
  70.         return NANDFLASH_TIMEOUT;   
  71.     else   
  72.         return 0;   
  73. }   
  74.    
  75. /* ------------------------------------------------------------------------ *  
  76.  *                                                                          *  
  77.  *  DAVINCIEVM_NANDFLASH_init( )                                            *  
  78.  *      Initialize the NAND Flash                                           *  
  79.  *                                                                          *  
  80.  *  Note:                                                                   *  
  81.  *      The Write Protect on the NAND Flash is disabled.  This allows the   *  
  82.  *      erase & write NAND flash to work.                                   *  
  83.  *                                                                          *  
  84.  * ------------------------------------------------------------------------ */   
  85. Int16 DAVINCIEVM_NANDFLASH_init( )   
  86. {   
  87.     /*------------------------------------------------------------------*  
  88.      *  NAND FLASH CHIP TIMEOUT @ 594 MHz                               *  
  89.      *                                                                  *  
  90.      *  AEMIF.CLK freq   = PLL1/6 = 594/6 = 99 MHz                      *  
  91.      *  AEMIF.CLK period = 1/99 MHz = 10.1 ns                           *  
  92.      *                                                                  *  
  93.      *------------------------------------------------------------------*/   
  94.     Uint32 acfg2 = 0   
  95.         | ( 0 < 31 )           // selectStrobe   
  96.         | ( 0 < 30 )           // extWait   
  97.         | ( 1 < 26 )           // writeSetup      //  10 ns   
  98.         | ( 4 < 20 )           // writeStrobe     //  40 ns   
  99.         | ( 1 < 17 )           // writeHold       //  10 ns   
  100.         | ( 1 < 13 )           // readSetup       //  10 ns   
  101.         | ( 6 < 7 )            // readStrobe      //  60 ns   
  102.         | ( 1 < 4 )            // readHold        //  10 ns   
  103.         | ( 3 < 2 )            // turnAround      //  ?? ns   
  104.         | ( 0 < 0 )            // asyncSize       //  8-bit bus   
  105.         ;   
  106.    
  107.     DAVINCIEVM_EMIF_config( acfg2,   
  108.                             AEMIF_MAX_TIMEOUT_16BIT,   
  109.                             AEMIF_MAX_TIMEOUT_16BIT,   
  110.                             AEMIF_MAX_TIMEOUT_16BIT );   
  111.    
  112.     AEMIF_NANDFCR = 0x00000001;   
  113.    
  114.     /* ---------------------------------------------------------------- *  
  115.      *                                                                  *  
  116.      *  Note: If NANDFLASH_CE_DO_CARE is defined, then CE will be low   *  
  117.      *        during read/write/erase operations.                       *  
  118.      *                                                                  *  
  119.      *        Else if NANDFLASH_CE_DO_NOT_CARE is defined, then CE will *  
  120.      *        only be low during accesses to the NAND Flash device.     *  
  121.      *                                                                  *  
  122.      * ---------------------------------------------------------------- */   
  123.    
  124.     NANDFLASH_ASSERT_CE( );                         // Assert CE if needed   
  125.    
  126.     NANDFLASH_CLE( NANDFLASH_RESET );               // Reset NAND Flash   
  127.    
  128.     if ( NANDFLASH_waitWhileBusy( ( Uint32 )-1 ) )  // Wait while busy   
  129.         return NANDFLASH_TIMEOUT;   
  130.    
  131.     NANDFLASH_DEASSERT_CE( );                       // Deassert CE   
  132.    
  133.     return 0;   
  134. }   
  135.    
  136. /* ------------------------------------------------------------------------ *  
  137.  *                                                                          *  
  138.  *  DAVINCIEVM_NANDFLASH_getTotalPages( )                                   *  
  139.  *                                                                          *  
  140.  *      Determine the total number of pages of NAND Flash                   *  
  141.  *                                                                          *  
  142.  * ------------------------------------------------------------------------ */   
  143. Uint32 DAVINCIEVM_NANDFLASH_getTotalPages( )   
  144. {   
  145.     Uint8 mfg_id;   
  146.     Uint8 dev_id;   
  147.     Uint32 total_pages = 0;   
  148.    
  149.     NANDFLASH_ASSERT_CE( );                         // Assert CE if needed   
  150.    
  151.     NANDFLASH_CLE( NANDFLASH_READID );              // Issue Read ID command   
  152.     NANDFLASH_ALE_1( 0 );   
  153.    
  154.     mfg_id = NANDFLASH_BASE_PTR;                    // Read MFG Id   
  155.     dev_id = NANDFLASH_BASE_PTR;                    // Read Device Id   
  156.    
  157.     /*  
  158.      *  Compared MFG ID & Device ID to the supported devices  
  159.      */   
  160.     if ( mfg_id == MFG_SAMSUNG )   
  161.     {   
  162.         if ( dev_id == DEV_K9F5608U0B )   
  163.             total_pages = DEV_K9F5608U0B_PAGE_COUNT;   
  164.    
  165.         else if ( dev_id == DEV_K9F5608Q0B )   
  166.             total_pages = DEV_K9F5608Q0B_PAGE_COUNT;   
  167.    
  168.         else if ( dev_id == DEV_K9F2808U0C )   
  169.             total_pages = DEV_K9F2808U0C_PAGE_COUNT;   
  170.    
  171.         else if ( dev_id == DEV_K9K1208Q0C )   
  172.             total_pages = DEV_K9K1208Q0C_PAGE_COUNT;   
  173.    
  174.         else if ( dev_id == DEV_SMCARD_128 )   
  175.             total_pages = DEV_SMCARD_128_PAGE_COUNT;   
  176.     }   
  177.     else if( mfg_id==MFG_STMicroelectronics)   
  178.     {   
  179.         if ( dev_id == DEV_NAND512W3A )   
  180.             total_pages = DEV_NAND512W3A_PAGE_COUNT;    
  181.     }   
  182.     NANDFLASH_DEASSERT_CE( );                       // Deassert CE   
  183.    
  184.     return total_pages;   
  185. }   
  186.    
  187.    
  188. /* ------------------------------------------------------------------------ *  
  189.  *                                                                          *  
  190.  *  DAVINCIEVM_NANDFLASH_erase( start, block_count )                        *  
  191.  *                                                                          *  
  192.  *      Erase the blocks of Flash memory                                    *  
  193.  *          start = destination address [ block address ]                   *  
  194.  *          block_count = # of blocks   [ 1 block = 16384 bytes ]           *  
  195.  *                                                                          *  
  196.  *      Return: 0    pass                                                   *  
  197.  *              1000 error                                                  *  
  198.  *              2000 timeout                                                *  
  199.  *                                                                          *  
  200.  * ------------------------------------------------------------------------ */   
  201. Int16 DAVINCIEVM_NANDFLASH_erase( Uint32 start, Uint32 block_count )   
  202. {   
  203.     Uint32 i;   
  204.     Int16 bad_blocks = 0;   
  205.    
  206.     for ( i = 0 ; i  block_count ; i++ )   
  207.     {   
  208.         NANDFLASH_ASSERT_CE( );                         // Assert CE if needed   
  209.    
  210.         NANDFLASH_CLE( NANDFLASH_ERASE );               // Erase block   
  211.         NANDFLASH_ALE_3( start );   
  212.         NANDFLASH_CLE( NANDFLASH_ERASE_CONFIRM );       // Confirm Erase   
  213.    
  214.         if ( NANDFLASH_waitWhileBusy( ( Uint32 )-1 ) )  // Wait while busy   
  215.         {   
  216.             NANDFLASH_DEASSERT_CE( );   
  217.             return NANDFLASH_TIMEOUT;                   // Timeout Error   
  218.         }   
  219.    
  220.         NANDFLASH_CLE( NANDFLASH_STATUS );              // Check erase status   
  221.         if ( NANDFLASH_BASE_PTR & NANDFLASH_STATUS_ERROR )   
  222.             bad_blocks++;                               // Increment # bad blocks   
  223.    
  224.         NANDFLASH_DEASSERT_CE( );                       // Deassert CE   
  225.    
  226.         start += NANDFLASH_BLOCKSIZE;                   // Increment address   
  227.     }   
  228.     return bad_blocks;   
  229. }   
  230.    
  231.    
  232. /* ------------------------------------------------------------------------ *  
  233.  *                                                                          *  
  234.  *  DAVINCIEVM_NANDFLASH_readPage( src, dst, page_count )                   *  
  235.  *                                                                          *  
  236.  *      Read data from NAND Flash by pages ( 512 bytes )                    *  
  237.  *                                                                          *  
  238.  *          src = source address      [ must be aligned to start of page ]  *  
  239.  *          dst = destination address [ any address ]                       *  
  240.  *          page_count = # of pages   [ a page is 512 bytes ]               *  
  241.  *                                                                          *  
  242.  *      Note does not return spare array data                               *  
  243.  *                                                                          *  
  244.  *      Return: 0    pass                                                   *  
  245.  *              2000 timeout                                                *  
  246.  *                                                                          *  
  247.  * ------------------------------------------------------------------------ */   
  248. //Uint32 eccValue1;   
  249. Int16 DAVINCIEVM_NANDFLASH_readPage( Uint32 src, Uint32 dst, Uint32 page_count )   
  250. {   
  251.     Uint32 i, j;   
  252.     volatile Uint8* dst8 = ( volatile Uint8* )dst;   
  253.     Uint8 spare[NANDFLASH_SPARESIZE];   
  254.        
  255.     for ( i = 0 ; i  page_count ; i++ )   
  256.     {   
  257.         NANDFLASH_ASSERT_CE( );                         // Assert CE if needed   
  258.    
  259.         NANDFLASH_CLE( NANDFLASH_READ );                // Read page   
  260.         NANDFLASH_ALE_4( src );   
  261.    
  262.         if ( NANDFLASH_waitWhileBusy( ( Uint32 )-1 ) )  // Wait while busy   
  263.         {   
  264.             NANDFLASH_DEASSERT_CE( );   
  265.             return NANDFLASH_TIMEOUT;                   // Timeout Error   
  266.         }   
  267.         //NAND_ECCReadAndRestart();   
  268.         for ( j = 0 ; j  NANDFLASH_PAGESIZE ; j++ )    // Read main array   
  269.             *dst8++ = NANDFLASH_BASE_PTR;   
  270.     //  eccValue1=NAND_ECCReadAndRestart();   
  271.            
  272.         for ( j = 0 ; j  NANDFLASH_SPARESIZE ; j++ )   // Read spare array   
  273.             spare[j] = NANDFLASH_BASE_PTR;   
  274.    
  275.         NANDFLASH_DEASSERT_CE( );                       // Deassert CE   
  276.    
  277.         src += NANDFLASH_PAGESIZE;                      // Increment address   
  278.     }   
  279.    
  280.     return 0;   
  281. }   
  282.    
  283. /* ------------------------------------------------------------------------ *  
  284.  *                                                                          *  
  285.  *  DAVINCIEVM_NANDFLASH_writePage( src, dst, page_count )                  *  
  286.  *                                                                          *  
  287.  *      Program NAND Flash.                                                 *  
  288.  *          src = source address      [ any address ]                       *  
  289.  *          dst = destination address [ must be aligned to start of page ]  *  
  290.  *          page_count = # of pages   [ a page is 512 bytes ]               *  
  291.  *                                                                          *  
  292.  *      Note does not program SPARE arrays                                  *  
  293.  *                                                                          *  
  294.  *      Return: 0    pass                                                   *  
  295.  *              1000 error                                                  *  
  296.  *              2000 timeout                                                *  
  297.  *                                                                          *  
  298.  * ------------------------------------------------------------------------ */   
  299. Int16 DAVINCIEVM_NANDFLASH_writePage( Uint32 src, Uint32 dst, Uint32 page_count )   
  300. {   
  301.     Uint32 i, j = 0;   
  302.     volatile Uint8* src8 = ( volatile Uint8* )src;   
  303.     Uint32 eccValue;   
  304.     Uint8 tempSpareValue[1];   
  305.     Int16 bad_pages = 0;   
  306.      
  307.     for ( i = 0 ; i  page_count ; i++ )   
  308.     {   
  309.         NANDFLASH_ASSERT_CE( );                         // Assert CE if needed   
  310.    
  311.         NANDFLASH_CLE( NANDFLASH_PROGRAM );             // Program page   
  312.         NANDFLASH_ALE_4( dst );   
  313.         NAND_ECCReadAndRestart();   
  314.         for ( j = 0 ; j  NANDFLASH_PAGESIZE ; j++ )    // Write main array   
  315.             NANDFLASH_BASE_PTR = *src8++;   
  316.         eccValue=NAND_ECCReadAndRestart();   
  317.         flash_swap_data(&(eccValue));   
  318.         tempSpareValue[0] = eccValue&0x000000FF;   
  319.         tempSpareValue[1] = (eccValue&0x0000FF00)>>8;   
  320.         tempSpareValue[2] = (eccValue&0x00FF0000)>>16;   
  321.         tempSpareValue[3] = (eccValue&0xFF000000)>>24;   
  322.         for(j=4;j<16;j++)   
  323.         {   
  324.         tempSpareValue[j]=0xff;   
  325.         }    
  326.         for ( j = 0 ; j  16 ; j++ )   
  327.         NANDFLASH_BASE_PTR=tempSpareValue[j];   
  328.                 
  329.         NANDFLASH_CLE( NANDFLASH_PROGRAM_CONFIRM );     // Confirm Program   
  330.    
  331.         if ( NANDFLASH_waitWhileBusy( ( Uint32 )-1 ) )  // Wait while busy   
  332.         {   
  333.             NANDFLASH_DEASSERT_CE( );   
  334.             return NANDFLASH_TIMEOUT;                   // Timeout Error   
  335.         }   
  336.    
  337.         NANDFLASH_CLE( NANDFLASH_STATUS );              // Check erase status   
  338.         if ( NANDFLASH_BASE_PTR & NANDFLASH_STATUS_ERROR )   
  339.             bad_pages++;                                // Increment # bad blocks   
  340.    
  341.         NANDFLASH_DEASSERT_CE( );                       // Deassert CE   
  342.    
  343.         dst += NANDFLASH_PAGESIZE;                      // Increment address   
  344.     }   
  345.    
  346.     return bad_pages;   
  347. }   
  348.    


原文地址:https://www.cnblogs.com/freedesert/p/2481022.html