#include "config.h"
#include "conf_nf.h"
#include "nf.h"
#include "nf_drv.h"
Go to the source code of this file.
Defines | |
#define | _nfc_drv_c_ |
#define | NF_XMCR_MODULE_SHARED DISABLED |
#define | BAD_BLOCK_OFFSET 0 |
Functions | |
__no_init volatile xdata Byte nf_send_cmd | At (0x3900) |
__no_init volatile xdata Byte nf_send_add | At (0x3A00) |
__no_init volatile xdata Byte nf_data | At (0x3800) |
void | nfc_select_dev (U8 dev) |
U8 | nfc_check_type (U8 nb_dev) |
Tests the Nand Flash configuration. | |
void | nfc_reset_nands (U8 nb_dev) |
Reset all the NF devices. | |
void | nf_XMCR_enable (void) |
Enable the XMCR (Extending Memory Module) of the AVR to drive the NAND Flash. | |
void | nf_XMCR_disable (void) |
Disable the XMCR module of the AVR, to allow access to others peripherals that may be connected on this same bus. | |
Status_bool | nfc_check_status (void) |
Check the status of the selected device. | |
void | nfc_open_page_read (U32 page_addr, U16 byte_addr) |
Opens a page for read. | |
void | nfc_open_page_write (U32 page_addr, U16 byte_addr) |
Opens a page for write. | |
void | nfc_mark_bad_block (U32 page_addr) |
Mark a block as 'invalid' by clearing it entirely. | |
void | nfc_erase_block (U32 page_addr, U8 force_erase) |
Erases a block. | |
void | nfc_read_spare_byte (U8 _MEM_TYPE_SLOW_ *p_byte, U8 n_byte, U32 page_addr) |
Reads the number spare bytes specified and stores them in a array. | |
void | nfc_wait_busy (void) |
Tests the true busy. | |
U32 | nfc_read_id (U8 read_id_cmd, U8 nf_num) |
Read the ID of the Nand-Flash. | |
static Bool | nfc_nf_is_ready (void) |
Check the status Ready/Busy of the Nand Flash. | |
U8 | nfc_detect (void) |
Read the ID of the Nand-Flash and update the global variable. | |
void | nfc_copy_back_init (U32 page_addr) |
Prepare a copy-back session. | |
Variables | |
U16 | bad_block_table [100] |
Definition in file nf_drv.c.
__no_init volatile xdata Byte nf_send_cmd At | ( | 0x3900 | ) |
__no_init volatile xdata Byte nf_send_add At | ( | 0x3A00 | ) |
__no_init volatile xdata Byte nf_data At | ( | 0x3800 | ) |
void nfc_select_dev | ( | U8 | dev | ) |
Definition at line 83 of file nf_drv.c.
00084 { 00085 if(0==dev) 00086 { 00087 Nandflash1_unselect(); 00088 Nandflash0_select(); 00089 }else{ 00090 Nandflash0_unselect(); 00091 Nandflash1_select(); 00092 } 00093 }
Tests the Nand Flash configuration.
The function verifies that the NF connected to device are properly declared in conf_nf.h.
none |
Definition at line 107 of file nf_drv.c.
Referenced by main().
00108 { 00109 U8 i_dev; 00110 if( 2 < nb_dev ) 00111 nb_dev = 2; // Only 1 or 2 for this driver 00112 00113 #if (NF_XMCR_MODULE_SHARED == ENABLED) 00114 nf_XMCR_enable(); 00115 #endif 00116 nfc_init( nb_dev, 0 ); 00117 nfc_reset_nands( nb_dev ); // Reset all the NF devices 00118 00119 // Test NF configuration 00120 // 00121 for( i_dev=0 ; i_dev<nb_dev ; i_dev++ ) 00122 { 00123 Nfc_action( NFC_ACT_DEV_SELECT, i_dev); 00124 nfc_wait_busy(); 00125 Nfc_action( NFC_ACT_ASSERT_CE, NFC_EXT_CELOW); 00126 Nfc_set_cmd(NF_READ_ID_CMD); 00127 Nfc_set_adc( 0 ); 00128 if(( Nfc_rd_data_fetch_next()!=G_DEV_MAKER ) 00129 || ( Nfc_rd_data_fetch_next()!=G_DEV_ID )) 00130 { 00131 return i_dev; 00132 } 00133 if( G_CE_TOGGLE ) 00134 { 00135 // disable CE Low 00136 Nfc_action( NFC_ACT_ASSERT_CE, NFC_EXT_NOP); 00137 } 00138 } 00139 00140 #if (NF_XMCR_MODULE_SHARED == ENABLED) 00141 nf_XMCR_disable(); 00142 #endif 00143 return nb_dev; 00144 }
void nfc_reset_nands | ( | U8 | nb_dev | ) |
Reset all the NF devices.
none |
Definition at line 152 of file nf_drv.c.
Referenced by nf_dfc_write_stop(), nfc_check_type(), and nfc_detect().
00153 { 00154 U8 i_dev; 00155 Mcu_set_sfr_page_nfc(); 00156 for( i_dev=0 ; i_dev<nb_dev ; i_dev++ ) 00157 { 00158 Nfc_action(NFC_ACT_DEV_SELECT, i_dev); 00159 // The wait is mandatory here since the function is used to wait any 00160 // pending internal programmation (Cache Program cmd). 00161 nfc_wait_busy(); 00162 Nfc_set_cmd(NF_RESET_CMD); 00163 nfc_wait_busy(); 00164 } 00165 }
void nf_XMCR_enable | ( | void | ) |
Enable the XMCR (Extending Memory Module) of the AVR to drive the NAND Flash.
Definition at line 172 of file nf_drv.c.
Referenced by nf_nf_2_ram(), nf_ram_2_nf(), nf_read_10(), nf_read_capacity(), nf_test_unit_ready(), nf_usb_stop(), nf_write_10(), nfc_check_type(), and nfc_detect().
00173 { 00174 #if (NF_CLE_ALE_MANUAL == ENABLED) 00175 XMCRB |= ((1<<XMM2) | (1<<XMM1) | (1<<XMM0)); // limit XRAM interface to A7 (release PC0..7) 00176 #else 00177 XMCRB |= ((1<<XMM2) | (1<<XMM1)); // limit XRAM interface to A9 (release PC2..7) 00178 #endif 00179 XMCRA |= (1<<SRE); // enable the external memory 00180 }
void nf_XMCR_disable | ( | void | ) |
Disable the XMCR module of the AVR, to allow access to others peripherals that may be connected on this same bus.
Definition at line 185 of file nf_drv.c.
Referenced by nf_nf_2_ram(), nf_ram_2_nf(), nf_read_10(), nf_read_capacity(), nf_test_unit_ready(), nf_usb_stop(), nf_write_10(), nfc_check_type(), and nfc_detect().
00186 { 00187 Nandflash0_unselect(); 00188 Nandflash1_unselect(); 00189 XMCRA &= ~(1<<SRE); // disable the external memory 00190 }
Status_bool nfc_check_status | ( | void | ) |
Check the status of the selected device.
Definition at line 203 of file nf_drv.c.
Referenced by nf_cleanup_memory(), nf_write_fbb(), and nf_write_lut().
00204 { 00205 Mcu_set_sfr_page_nfc(); 00206 nfc_wait_busy(); // Send a status command and wait the completion of the last command 00207 if ( (Nfc_rd_data()&NF_MASK_STATUS_FAIL)==0 ) { return PASS; } // I/O 0 Pass:0 Fail:1 00208 else { return FAIL; } 00209 }
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.
page_addr | absolute page address of the block | |
byte_addr | relative byte address inside the page. |
nf_init()
should have been called before. The NF device should have been selected before with Nfc_action(NFC_ACT_DEV_SELECT, id)
. Definition at line 221 of file nf_drv.c.
Referenced by nf_cache_fbb_flush(), nf_cache_fbb_refill(), nf_cache_lut_flush(), nf_cache_lut_refill(), nf_check_fbb(), nf_check_lut(), nf_cleanup_memory(), nf_copy(), nf_copy_tail(), nf_nf_2_ram(), nf_open_write(), nf_read_10(), nf_rebuild(), nf_refine_index(), nfc_erase_block(), and nfc_read_spare_byte().
00222 { 00223 Mcu_set_sfr_page_nfc(); 00224 nfc_wait_busy(); 00225 Nfc_open_page_read( page_addr, byte_addr); 00226 }
Opens a page for write.
The function will adapt the commands according to the type of flash memory.
page_addr | absolute page address of the block | |
byte_addr | relative byte address inside the page. |
nf_init()
should have been called before. The NF device should have been selected before with Nfc_action(NFC_ACT_DEV_SELECT, id)
. Definition at line 239 of file nf_drv.c.
Referenced by nf_copy(), nf_copy_tail(), nf_open_write(), nf_ram_2_nf(), nf_write_10(), nf_write_fbb(), nf_write_lut(), and nfc_mark_bad_block().
00240 { 00241 Mcu_set_sfr_page_nfc(); 00242 Nfc_open_page_write( page_addr, byte_addr); 00243 }
void nfc_mark_bad_block | ( | U32 | page_addr | ) |
Mark a block as 'invalid' by clearing it entirely.
page_addr | absolute page address of the block |
nf_init()
should have been called before. The device which holds this bad block should have been selected before with Nfc_action(NFC_ACT_DEV_SELECT, id)
. Definition at line 255 of file nf_drv.c.
Referenced by nf_cleanup_memory(), and nf_rebuild().
00256 { 00257 U8 n_bytes; 00258 U8 i_byte; 00259 U8 i_page; 00260 00261 Mcu_set_sfr_page_nfc(); 00262 00263 n_bytes= ( Is_nf_512() ) 00264 ? 16 // 512B page access 00265 : 64 // 2KB page access 00266 ; 00267 00268 // Erasing the block is mandatory to prevent partial programming 00269 // (some 512B NF does support partial prog, but not after a copy back command). 00270 nfc_erase_block( page_addr, TRUE ); 00271 for ( i_page=(U8)1<<G_SHIFT_BLOCK_PAGE ; i_page!=0 ; i_page--, page_addr++ ) 00272 { 00273 nfc_open_page_write( page_addr, NF_SPARE_POS-8 ); 00274 Nfc_wr_data('A'); Nfc_wr_data('t'); 00275 Nfc_wr_data('m'); Nfc_wr_data('e'); 00276 Nfc_wr_data('l'); Nfc_wr_data(' '); 00277 Nfc_wr_data(' '); Nfc_wr_data(' '); 00278 for ( i_byte=n_bytes ; i_byte!=0 ; i_byte-=4 ) 00279 { 00280 Nfc_wr_data(0); 00281 Nfc_wr_data(0); 00282 Nfc_wr_data(0); 00283 Nfc_wr_data(0); 00284 } 00285 Nfc_set_cmd(NF_PAGE_PROGRAM_CMD); // Confirm programmation 00286 } 00287 }
Erases a block.
The erase will be done only if the block is not bad
page_addr | absolute page address of the block | |
force_erase | TRUE forces erasing, FALSE erases the block (if not bad) |
nf_init()
should have been called before. The device which holds the block to delete should have been selected before with Nfc_action(NFC_ACT_DEV_SELECT, id)
. Definition at line 302 of file nf_drv.c.
Referenced by nf_cache_fbb_flush(), nf_cache_lut_flush(), nf_cleanup_memory(), nf_erase_all_blocks(), nf_erase_old_blocks(), nf_fetch_free_block(), nf_rebuild(), nfc_mark_bad_block(), and ut_nfc_erase_all().
00303 { 00304 Mcu_set_sfr_page_nfc(); 00305 if (FALSE == force_erase) 00306 { 00307 nfc_open_page_read( page_addr, NF_SPARE_POS + G_OFST_BLK_STATUS ); 00308 if( (Nfc_rd_data() != 0xFF) ) return; // The block is bad. We can not erase it 00309 } 00310 nfc_wait_busy(); 00311 Nfc_unprotect_all_flash(); // WP may be actif due to block protection 00312 Nfc_set_cmd (NF_BLOCK_ERASE_CMD); // Auto Block Erase Setup 00313 Nfc_set_adr( LSB0(page_addr) ); 00314 Nfc_set_adr( LSB1(page_addr) ); 00315 if (3 == G_N_ROW_CYCLES) 00316 { 00317 Nfc_set_adr( MSB1(page_addr) ); 00318 } 00319 Nfc_set_cmd(NF_BLOCK_ERASE_CONFIRM_CMD); // Erase command 00320 }
Reads the number spare bytes specified and stores them in a array.
p_byte | pointer on the array in which are stored the spare bytes. | |
n_byte | number of spare bytes to read. | |
page_addr | absolute page address of the block. |
nf_init()
should have been called before. The NF device should have been selected before with Nfc_action(NFC_ACT_DEV_SELECT, id)
. Definition at line 333 of file nf_drv.c.
Referenced by nf_fetch_free_block(), nf_rebuild(), and nf_scan().
00337 { 00338 U8 i; 00339 00340 Mcu_set_sfr_page_nfc(); 00341 nfc_open_page_read( page_addr, NF_SPARE_POS); 00342 00343 for ( i=0 ; i!=n_byte ; i++ ) 00344 { 00345 p_byte[i] = Nfc_rd_data_fetch_next(); 00346 } 00347 }
void nfc_wait_busy | ( | void | ) |
Tests the true busy.
Note that we test twice the ready, since there is an hardware minimum requirement between the end of the busy and the first read cycle. Since the busy is not wired, the ready is tested twice.
Definition at line 353 of file nf_drv.c.
Referenced by nfc_check_status(), nfc_check_type(), nfc_copy_back_init(), nfc_erase_block(), nfc_open_page_read(), nfc_read_id(), and nfc_reset_nands().
00354 { 00355 register int Reg; 00356 Nfc_set_cmd(NF_READ_STATUS_CMD); 00357 Reg = Nfc_rd_status(); 00358 if( Is_nf_2k() ) 00359 { 00360 if( G_CACHE_PROG ) 00361 { 00362 while( (Nfc_rd_status() & NF_MASK_STATUS_T_RDY_2KB )==0 ); 00363 while( (Nfc_rd_status() & NF_MASK_STATUS_T_RDY_2KB )==0 ); 00364 } 00365 else 00366 { 00367 while( (Nfc_rd_status() & NF_MASK_STATUS_READY )==0 ); 00368 while( (Nfc_rd_status() & NF_MASK_STATUS_READY )==0 ); 00369 } 00370 } 00371 if( Is_nf_512() ) 00372 { 00373 while( (Nfc_rd_status() & NF_MASK_STATUS_T_RDY_512B )==0 ); 00374 while( (Nfc_rd_status() & NF_MASK_STATUS_T_RDY_512B )==0 ); 00375 } 00376 }
Read the ID of the Nand-Flash.
read_id_cmd | Read_id command (NF_READ_ID_CMD, NF_READ_ID2_CMD) | |
nf_num | Nand Flash number |
nf_init()
should have been called before. Definition at line 397 of file nf_drv.c.
Referenced by nfc_detect().
00398 { 00399 U32 ret; 00400 00401 Mcu_set_sfr_page_nfc(); 00402 Nfc_action(NFC_ACT_DEV_SELECT, nf_num); 00403 nfc_wait_busy(); 00404 Nfc_action( NFC_ACT_ASSERT_CE, NFC_EXT_CELOW); 00405 Nfc_set_cmd (read_id_cmd); 00406 Nfc_set_adc( 0 ); 00407 00408 MSB0(ret)= Nfc_rd_data_fetch_next(); // Maker Code 00409 MSB1(ret)= Nfc_rd_data_fetch_next(); // Device Id 00410 MSB2(ret)= Nfc_rd_data_fetch_next(); // extra 00411 MSB3(ret)= Nfc_rd_data_fetch_next(); // extra (Multi Plane Support) 00412 00413 Nfc_action( NFC_ACT_ASSERT_CE, NFC_EXT_NOP); 00414 return ret; 00415 }
static Bool nfc_nf_is_ready | ( | void | ) | [static] |
Check the status Ready/Busy of the Nand Flash.
Definition at line 423 of file nf_drv.c.
References FALSE, NF_MASK_STATUS_READY, NF_MAX_RB_TIMEOUT, NF_READ_STATUS_CMD, Nfc_rd_status, Nfc_set_cmd, and TRUE.
Referenced by nfc_detect().
00424 { 00425 register U8 Reg; 00426 U8 u8_timeout; 00427 00428 Nfc_set_cmd( NF_READ_STATUS_CMD ); // send status for each read, because the NF must be in reset sequence 00429 Reg = Nfc_rd_status(); // active first read 00430 00431 for (u8_timeout=NF_MAX_RB_TIMEOUT ; u8_timeout!=0 ; u8_timeout--) 00432 { 00433 if(( (Nfc_rd_status() & NF_MASK_STATUS_READY) !=0 ) // the busy pin is not tested, and the bit ready may be wrong penddind the rise of busy pin 00434 && ( (Nfc_rd_status() & NF_MASK_STATUS_READY) !=0 ) ) // To not read a wrong status, we check the status after 6 cycles (300ns) 00435 { 00436 return TRUE; // NF READY 00437 } 00438 } 00439 return FALSE; // TIMEOUT 00440 }
U8 nfc_detect | ( | void | ) |
Read the ID of the Nand-Flash and update the global variable.
Definition at line 449 of file nf_drv.c.
Referenced by main().
00450 { 00451 U32 u32_nf_ids; 00452 U8 u8_i, u8_conf; 00453 00454 #if (NF_XMCR_MODULE_SHARED == ENABLED) 00455 nf_XMCR_enable(); 00456 #endif 00457 00458 // Init the Nand Flash Controller 00459 nfc_init( NF_MAX_DEVICES, 0 ); 00460 nfc_reset_nands( NF_MAX_DEVICES ); // Reset all the NF devices 00461 00462 // Check the presence of device 0 00463 if ( FALSE == nfc_nf_is_ready() ) 00464 { 00465 #if (NF_XMCR_MODULE_SHARED == ENABLED) 00466 nf_XMCR_disable(); 00467 #endif 00468 return NO_NF_CONNECTED; 00469 } 00470 00471 // Read the Nand Flash IDs of device 0 00472 u32_nf_ids = nfc_read_id( NF_READ_ID_CMD, 0 ); 00473 00474 // Identify the Nand Flash (device 0) 00475 for( u8_i=0 ; u8_i < (sizeof(nf_list_id)/sizeof(St_nf_id)) ; u8_i++) 00476 { 00477 if((nf_list_id[u8_i].manuf == MSB0(u32_nf_ids)) 00478 && (nf_list_id[u8_i].dev == MSB1(u32_nf_ids))) 00479 break; // here, ID is know 00480 } 00481 if( u8_i == (sizeof(nf_list_id)/sizeof(St_nf_id)) ) 00482 { 00483 #if (NF_XMCR_MODULE_SHARED == ENABLED) 00484 nf_XMCR_disable(); 00485 #endif 00486 return NF_UNKNOW; 00487 } 00488 00489 // Set NF configuration parameters for initialisation and access 00490 #if (NF_GENERIC_DRIVER==TRUE) 00491 # error Test me... 00492 g_shift_page_byte =; 00493 g_shift_block_page =; 00494 #endif 00495 00496 #if (NF_GENERIC_DRIVER==TRUE) || (NF_AUTO_DETECT_2KB==TRUE) ||(NF_AUTO_DETECT_512B==TRUE) 00497 00498 // Record info 00499 u8_conf = nf_list_id[u8_i].conf; 00500 g_dev_maker = MSB0(u32_nf_ids); // Device maker 00501 g_dev_id = MSB1(u32_nf_ids); // Device ID 00502 00503 // Search the number of block of device 00504 for( u8_i=0 ; u8_i < (sizeof(nf_list_link_id_block)/sizeof(St_nf_link_id_block)) ; u8_i++) 00505 { 00506 if( nf_list_link_id_block[u8_i].dev_id == g_dev_id ) 00507 break; // ID found 00508 } 00509 if( u8_i == (sizeof(nf_list_link_id_block)/sizeof(St_nf_link_id_block)) ) 00510 while(1); // Error in NF definition 00511 00512 g_n_zones = nf_list_link_id_block[u8_i].nb_zones; 00513 #if (NF_AUTO_DETECT_2KB==TRUE) 00514 if( 1 == g_n_zones ) 00515 g_n_row_cycles = 2; 00516 else 00517 g_n_row_cycles = 3; 00518 #endif 00519 #if (NF_AUTO_DETECT_512B==TRUE) 00520 if( 2 >= g_n_zones ) 00521 g_n_row_cycles = 2; 00522 else 00523 g_n_row_cycles = 3; 00524 #endif 00525 g_n_blocks = g_n_zones*1024L; 00526 00527 g_copy_back_cont = nf_list_conf[u8_conf].copy_back_cont ; 00528 g_copy_back_discont = nf_list_conf[u8_conf].copy_back_discont; 00529 g_cache_program = nf_list_conf[u8_conf].cache_program ; 00530 g_ce_toggle = nf_list_conf[u8_conf].ce_toggle; 00531 /* 00532 g_clock_dfc_nfc = (nf_list_conf[u8_conf].dfc_nfc_clock<<5) & MSK_DNFCKS; 00533 00534 Mcu_set_sfr_page_nfc(); 00535 Nfc_set_read_timing((U8)nf_list_conf[u8_conf].timing_read ); 00536 if( !g_ce_toggle ) 00537 { 00538 // Enable CE low 00539 Nfc_action( NFC_ACT_ASSERT_CE, NFC_EXT_CELOW); 00540 } 00541 */ 00542 #endif 00543 00544 #if (NF_XMCR_MODULE_SHARED == ENABLED) 00545 nf_XMCR_disable(); 00546 #endif 00547 return u8_i; 00548 }
void nfc_copy_back_init | ( | U32 | page_addr | ) |
Prepare a copy-back session.
page_addr | absolute source page address of the block |
nf_init()
should have been called before. Definition at line 558 of file nf_drv.c.
Referenced by nf_copy().
00559 { 00560 Mcu_set_sfr_page_nfc(); 00561 nfc_wait_busy(); 00562 Nfc_unprotect_all_flash(); // WP may be actif due to block protection 00563 Nfc_set_cmd(NF_READ_CMD); 00564 Nfc_set_adc( 0 ); 00565 Nfc_set_adc( 0 ); 00566 Nfc_set_adr( LSB0(page_addr) ); 00567 Nfc_set_adr( LSB1(page_addr) ); 00568 if ( 3==G_N_ROW_CYCLES ) 00569 { 00570 Nfc_set_adr( MSB1(page_addr) ); 00571 } 00572 Nfc_set_cmd(NF_COPY_BACK_CMD); 00573 nfc_wait_busy(); 00574 }
U16 bad_block_table[100] |