//! \brief Reset all the NF devices. //! //! \param nb_dev Number of device //! void nf_reset_nands( U8 nb_dev ) { U8 i_dev; for( i_dev=0 ; i_dev<nb_dev ; i_dev++ ) { nf_select( i_dev ); // The wait is mandatory here since the function is used to wait any // pending internal programming (Cache Program cmd). nf_wait_busy(); nf_wr_cmd(NF_RESET_CMD); nf_wait_busy(); } }
//! \brief Prepare a copy-back session //! //! \param page_addr absolute source page address of the block //! //! \pre <code>nf_init()</code> should have been called before. //! void nf_copy_back_init( U32 page_addr ) { nf_wait_busy(); nf_wr_cmd(NF_READ_CMD); nf_wr_addr( 0 ); nf_wr_addr( 0 ); nf_wr_addr( LSB0(page_addr) ); nf_wr_addr( LSB1(page_addr) ); if ( 3==G_N_ROW_CYCLES ) { nf_wr_addr( MSB1(page_addr) ); } nf_wr_cmd(NF_COPY_BACK_CMD); nf_wait_busy(); }
//! \brief Tests the Nand Flash configuration //! //! The function verifies that the connected NF is //! properly declared in conf_nf.h. //! //! \param nb_dev number of device //! //! \return The number of device connected and corresponding //! to NF identifiers. //! U8 nf_check_type( U8 nb_dev ) { U8 i_dev; //nf_init( nb_dev, 0 ); #warning Update for full support. nf_reset_nands( nb_dev ); // Reset all the NF devices // Test NF configuration // for( i_dev=0 ; i_dev<nb_dev ; i_dev++ ) { nf_select( i_dev ); nf_wait_busy(); nf_force_CE(); nf_wr_cmd(NF_READ_ID_CMD); nf_wr_addr( 0 ); if(( nf_rd_data()!=G_DEV_MAKER ) || ( nf_rd_data()!=G_DEV_ID )) { Assert( false ); return i_dev; } if( G_CE_LOW ) { // Activate CE Low if needed. This config is static // and we supposed that it is no more deactivated in firmware. nf_force_CE(); } } return nb_dev; }
//! \brief Check the status of the selected device. //! //! \return a status: //! PASS if the status is PASS; //! FAIL if the status is FAIL //! bool nf_check_status( void ) { U8 data; nf_wait_busy(); // Send a status command and wait the completion of the last command ecchrs_freeze(&AVR32_ECCHRS); data = nf_rd_data(); ecchrs_unfreeze(&AVR32_ECCHRS); if ( (data&NF_MASK_STATUS_FAIL)==0 ) { return PASS; } // I/O 0 Pass:0 Fail:1 else { return FAIL; } }
//! \brief Protect all the flashes //! void nf_protect( void ) { U32 i_dev; // Wait until the end of any pending internal programming (Cache Program cmd). for( i_dev=0 ; i_dev<NF_N_DEVICES ; i_dev++ ) { nf_select( i_dev ); nf_wait_busy(); } gpio_clr_gpio_pin(NF_WP_PIN); }
//! \brief Launch a copy-back session //! //! \param page_addr absolute recipient page address of the block //! //! \pre <code>nf_init()</code> should have been called before. //! void nfc_copy_back_conf( U32 page_addr ) { nf_wait_busy(); nf_wr_cmd(NF_RANDOM_DATA_INPUT_CMD); nf_wr_addr( 0 ); nf_wr_addr( 0 ); nf_wr_addr( LSB0(page_addr) ); nf_wr_addr( LSB1(page_addr) ); if ( 3==G_N_ROW_CYCLES ) { nf_wr_addr( MSB1(page_addr) ); } nf_wr_cmd(NF_PAGE_PROGRAM_CMD); }
//! \brief Erases a block. //! //! The erase will be done only if the block is not bad //! //! \param page_addr absolute page address of the block //! \param force_erase true forces erasing, false erases the block (if not bad) //! //! \pre <code>nf_init()</code> should have been called before. //! The device which holds the block to delete should have been selected //! before with <code>nf_select( id )</code>. //! void nf_erase_block( U32 page_addr, U8 force_erase ) { if( false==force_erase ) { nf_open_page_read( page_addr, NF_SPARE_POS + G_OFST_BLK_STATUS ); if( (nf_rd_data()!=0xFF) ) return; // The block is bad. We can not erase it } nf_wait_busy(); nf_wr_cmd(NF_BLOCK_ERASE_CMD); // Auto Block Erase Setup nf_wr_addr( LSB0(page_addr) ); nf_wr_addr( LSB1(page_addr) ); if ( 3==G_N_ROW_CYCLES ) { nf_wr_addr( MSB1(page_addr) ); } nf_wr_cmd(NF_BLOCK_ERASE_CONFIRM_CMD); // Erase command }
//! \brief Read the ID of the Nand-Flash //! //! \param read_id_cmd Read_id command (NF_READ_ID_CMD, NF_READ_ID2_CMD) //! \param nf_num Nand Flash number //! //! \return : //! MSB0(ret) (MSB) is the Maker Code, //! MSB1(ret) is the Device Id, //! MSB2(ret) is 3rd byte returned, //! MSB3(ret) (LSB) is 4th byte returned. //! //! \pre <code>nf_init()</code> should have been called before. //! U32 nf_read_id( U8 read_id_cmd, U8 nf_num ) { U32 ret; nf_select( nf_num ); nf_wait_busy(); //nf_force_CE(); nf_wr_cmd (read_id_cmd); nf_wr_addr( 0 ); MSB0(ret)= nf_rd_data(); // Maker Code MSB1(ret)= nf_rd_data(); // Device Id MSB2(ret)= nf_rd_data(); // extra MSB3(ret)= nf_rd_data(); // extra (Multi Plane Support) //Nfc_action( NFC_ACT_ASSERT_CE, NFC_EXT_NOP); return ret; }
//! \brief Opens a page for read. The function will adapt the commands according to the //! type of flash memory. The busy is polled at the end of the function. //! //! \param page_addr absolute page address of the block //! \param byte_addr relative byte address inside the page. //! //! \pre <code>nf_init()</code> should have been called before. //! The NF device should have been selected before with <code>nf_select( id )</code>. //! void nf_open_page_read( U32 page_addr, U16 byte_addr) { nf_wait_busy(); Nf_open_page_read( page_addr, byte_addr); }
/*! \brief Main function. */ int main(void) { bool valid_block_found[NF_N_DEVICES]; U32 u32_nf_ids, i_dev, i_block; U8 maker_id, device_id, byte3; U32 i, time_s, time_e; U8 data; // start init sysclk_init(); // Enable clock for require module sysclk_enable_hsb_module(SYSCLK_EBI); sysclk_enable_pbb_module(SYSCLK_SMC_REGS); sysclk_enable_pbb_module(SYSCLK_HMATRIX); // Initialize the board. // The board-specific conf_board.h file contains the configuration of the board // initialization. board_init(); init_stdio(); #ifdef NF_ADDON // addon nandflash board for EVK1104 (not mounted by default) // Unselect NF on board gpio_set_gpio_pin(AVR32_PIN_PX53); gpio_set_gpio_pin(AVR32_PIN_PX52); #endif nf_init(sysclk_get_cpu_hz()); nf_unprotect(); printf("\x0C"); printf("Nand Flash example.\r\n===================\r\n\r\n"); // - Simple test of the NF communication through the SMC. // - Find all bad blocks. // - Find a valid block for the remaining tests. nf_reset_nands(NF_N_DEVICES); printf("\tDetecting Nand Flash device(s).\r\n"); for( i_dev=0 ; i_dev<NF_N_DEVICES ; i_dev++ ) { // Performs some init here... valid_block_found[i_dev] = false; // Test Maker and Device ids u32_nf_ids = nf_read_id( NF_READ_ID_CMD, i_dev ); maker_id = MSB0(u32_nf_ids); device_id = MSB1(u32_nf_ids); byte3 = MSB3(u32_nf_ids); printf("\t\tNF %ld: [Maker=0x%02x] [Device=0x%02x] [byte3=0x%02x]\r\n", i_dev, maker_id, device_id, byte3); if( maker_id==M_ID_MICRON ) { printf("\t\t Micron chip"); if( (device_id==0xDA) && (byte3==0x15) ) printf("- MT29F2G08AACWP device\r\n"); else if( (device_id==0xDA) && (byte3==0x95) ) printf("- MT29F2G08AADWP device\r\n"); else { printf("- *** Error: unexpected chip detected. Please check the board settings and hardware.\r\n"); return -1; } } else { printf("\t\t *** Error: unexpected chip detected. Please check the board settings and hardware.\r\n"); return -1; } } printf("\r\n\tTesting bad blocks.\r\n"); for( i_dev=0 ; i_dev<NF_N_DEVICES ; i_dev++ ) { printf("\t\tNF %ld:\r\n", i_dev); nf_select(i_dev); for( i_block=0 ; i_block<G_N_BLOCKS ; i_block++ ) { nf_open_page_read( nf_block_2_page(i_block), NF_SPARE_POS + G_OFST_BLK_STATUS ); if( (nf_rd_data()!=0xFF) ) { // The block is bad. printf("\t\t\tBlock %ld (0x%lx) is bad.\r\n", i_block, i_block); } else { if( !valid_block_found[i_dev] ) { valid_block_found[i_dev]= true; valid_block_addr[i_dev] = i_block; printf("\t\t\tFirst valid block is at address %ld (0x%lx).\r\n", i_block, i_block); } } } } for( i_dev=0 ; i_dev<NF_N_DEVICES ; i_dev++ ) if( !valid_block_found[i_dev] ) { printf("Error %d\r\n", __LINE__); return 0; } // - Ensure good NF behaviour through simple commands. // Erase, Program, Read // - Measures NF timings. printf("\r\n\tMeasuring NF timings.\r\n"); for( i_dev=0 ; i_dev<NF_N_DEVICES ; i_dev++ ) { printf("\t\tNF %ld:\r\n", i_dev); nf_select(i_dev); nf_erase_block( nf_block_2_page(valid_block_addr[0]), false); time_s = Get_sys_count(); nf_wait_busy(); time_e = Get_sys_count(); // Verify that the block is erased. nf_open_page_read( nf_block_2_page(valid_block_addr[0]), 0); for( i= 0; i<2048 ; i++ ) { data = nf_rd_data(); if( data!=0xFF ) { printf("\tError: offset %d is not erased (read %d).\r\n", (U8)i, data); return 0; } } printf("\t\t\tTime to erase a page:%ld cy (%ld us)\r\n", time_e-time_s, cpu_cy_2_us(time_e-time_s, sysclk_get_cpu_hz())); nf_open_page_write( nf_block_2_page(valid_block_addr[0]), 0); for( i=0 ; i<2048 ; i++ ) nf_wr_data(i%256); nf_wr_cmd(NF_PAGE_PROGRAM_CMD); time_s = Get_sys_count(); nf_wait_busy(); time_e = Get_sys_count(); printf("\t\t\tTime to program a page:%ld cy (%ld us)\r\n", time_e-time_s, cpu_cy_2_us(time_e-time_s, sysclk_get_cpu_hz())); time_s = Get_sys_count(); nf_open_page_read( nf_block_2_page(valid_block_addr[0]), 0); time_e = Get_sys_count(); printf("\t\t\tTime to access to a page:%ld cy (%ld us)\r\n", time_e-time_s, cpu_cy_2_us(time_e-time_s, sysclk_get_cpu_hz())); for( i= 0; i<2048 ; i++ ) { data = nf_rd_data(); if( data!= i%256) { printf("\tError: expect %d, read %d\r\n", (U8)i, data); return 0; } } } printf("Example DONE\r\n"); return 0; }