1 /******************************************************************************
2 ** File Name: flash_drvapi.h *
5 ** Copyright: 2001 Spreatrum, Incoporated. All Rights Reserved. *
11 ** user can modify and replace the file, but all functions *
12 ** must be supported *
13 ******************************************************************************
15 ******************************************************************************
17 ** ------------------------------------------------------------------------- *
18 ** DATE NAME DESCRIPTION *
19 ** 27/11/2001 Lin.liu Create. *
20 ******************************************************************************/
21 #ifndef _FLASH_DRVAPI_H_
22 #define _FLASH_DRVAPI_H_
24 /**---------------------------------------------------------------------------*
26 **---------------------------------------------------------------------------*/
28 /**---------------------------------------------------------------------------*
30 **---------------------------------------------------------------------------*/
36 #include "sci_types.h"
38 /**---------------------------------------------------------------------------*
40 **---------------------------------------------------------------------------*/
43 #define FLASH_WR(b,d) *( (FLASH_PTR_TYPE)(b)) = (d)
44 #define FLASH_RD(b) *( (FLASH_PTR_TYPE)(b))
48 //nor flash manufacture ID
49 #define FLASH_SAMSUNG_ID (0xEC)
50 #define FLASH_AMD_ID (0x1)
51 #define FLASH_INTEL_ID (0x89)
52 #define FLASH_ST_ID (0x20)
53 #define FLASH_SST_ID (0xBF)
54 #define FLASH_TSB_ID (0x98)
55 #define FLASH_CFEON_ID (0x7F)
57 //reset Intel/AMD flash
58 #define RESET_FLASH *(FLASH_PTR_TYPE)(0x0) = 0xF0;\
59 *(FLASH_PTR_TYPE)(0x0) = 0xFF
61 #define FLASH_IRQ_FIQ_DISABLE 0xC0
63 /* Below is the raw definitions for Threadx. */
64 #define FLASH_TX_DISABLE s_flash_interrupt_save = _tx_thread_interrupt_control(FLASH_IRQ_FIQ_DISABLE);
65 #define FLASH_TX_RESTORE _tx_thread_interrupt_control(s_flash_interrupt_save);
67 //Disable irq/fiq and cache(M)
68 #define FLASH_DISABLE_IRQ FLASH_TX_DISABLE \
70 //Enable irq/fiq and cache(M)
71 #define FLASH_RESTORE_IRQ ENABLE_CACHE \
73 //Enable the system tick count
74 #define ENABLE_SYSTEM_TICK_COUNT *(volatile uint32 *)(GR_GEN1) |= BIT_13;
76 //check any irq/fiq status be set.
77 #define GET_INTERRUPT_FLAG (CHIP_REG_GET(INT_IRQ_STS) | CHIP_REG_GET(INT_FIQ_STS))
79 //invert the high/low byte of the given word.
80 #define INVERT_WORD( _w ) _w = ((_w) >> 8) + ((_w) << 8)
82 //ARM target, Big-Endian
83 #define FIRST_BYTE( _word ) (uint8)( (_word) >> 8 )
84 #define SECOND_BYTE( _word ) (uint8)( (_word) & 0xFF )
86 //ARM target, big-endian
87 #define MAKE_WORD( _fst, _snd ) ((uint16)(_fst) << 8) + ((uint16)(uint8)(_snd))
89 #define FLASH_INIT_MAGIC (0xFAACCAAF)
91 #define FLASH_ADDR_IS_VALID( _addr ) \
92 ((((_addr) >= s_flash_efs_start_addr ) && ((_addr) <= s_flash_efs_end_addr ))\
93 ||(((_addr) >= s_flash_fixed_nvitem_addr) && ((_addr) <= s_flash_fixed_nvitem_end_addr ))\
94 ||(((_addr) >= s_flash_product_info_addr) && ((_addr) <= (s_flash_product_info_addr + s_flash_sector_size) )))
96 //FLASH_OPTR_STATUS_FAILED : the optr failed
97 //FLASH_OPTR_STATUS_COMPLETED : the optr success
98 //FLASH_OPTR_STATUS_RUNNING : the optr is running, need check it again.
100 #define FLASH_OPTR_STATUS_FAILED (0)
101 #define FLASH_OPTR_STATUS_COMPLETED (1)
102 #define FLASH_OPTR_STATUS_RUNNING (2)
104 //running optr status
105 #define FLASH_STORE_OPTR_MAGIC (0x4D534F46) // FOSM
106 #define FLASH_STORE_OPTR_PROGRAMING (0x464F5350) // FOSP
107 #define FLASH_STORE_OPTR_ERASING (0x464F5345) // FOSE
108 #define FLASH_STORE_OPTR_SUSPEND (0x464F5353) // FOSS
109 #define FLASH_STORE_OPTR_IDLE (0x0)
112 #define STORE_FLASH_OPTR(optr , ptr) *ptr = optr;\
113 *(ptr + 1) = (optr ^ FLASH_STORE_OPTR_MAGIC);
115 #define GET_FLASH_STORE_OPTR(ptr) ((FLASH_STORE_OPTR_MAGIC == ((*ptr) ^ (*(ptr + 1)))) ? (*ptr) : 0)
117 // add error trace information
118 #define FLASH_OPTR_ERASE (0xB7)
119 #define FLASH_OPTR_PROGRAM (0xCF)
120 #define FLASH_OPTR_RESUME (0xDE)
121 #define FLASH_OPTR_SUSPEND (0xE3)
123 #define FLASH_INVALID_ADDR ((uint32)(-1))
124 #define FLASH_INVALID_SECTOR ((uint16)(-1))
128 #define FLASH_SAVE_TRACE_INFOR_INIT flash_trace_infor.flag = 0x46544946 // FTIF
130 #define FLASH_SAVE_TRACE_INFOR_ERASE flash_trace_infor.optr = FLASH_OPTR_ERASE
131 #define FLASH_SAVE_TRACE_INFOR_PROGRAM( _data ) \
132 flash_trace_infor.optr = FLASH_OPTR_PROGRAM; \
133 flash_trace_infor.data = _data
135 #define FLASH_SAVE_TRACE_INFOR_RESUME flash_trace_infor.optr = FLASH_OPTR_RESUME
136 #define FLASH_SAVE_TRACE_INFOR_SUSPEND flash_trace_infor.optr = FLASH_OPTR_SUSPEND
138 #define FLASH_SAVE_TRACE_INFOR_CHECK_STATUS\
139 flash_trace_infor.address = (volatile uint32)(addr_ptr);\
140 flash_trace_infor.tick_1 = old_tick; \
141 flash_trace_infor.tick_2 = cur_tick; \
142 flash_trace_infor.max_tick = tick_count; \
143 flash_trace_infor.status_1 = d; \
144 flash_trace_infor.status_2 = tmp; \
145 flash_trace_infor.content = *addr_ptr
147 #define FLASH_DELAY_20US_COUNT 100 //(20*100)/3/70
148 #define FLASH_DELAY_20US\
151 for(i = 0; i < FLASH_DELAY_20US_COUNT; i++){};\
154 /**---------------------------------------------------------------------------*
156 **---------------------------------------------------------------------------*/
159 typedef VOLATILE uint16 *FLASH_PTR_TYPE;
172 //Flash erasing status enum
173 typedef enum FLASH_ERASE_STATUS_TAG
175 FLASH_ERASE_COMPLETED_S = 0, /* erase has completed */
176 FLASH_ERASE_PROCESSING_S , /* erase is processing */
177 FLASH_ERASE_SUSPEND_S , /* erase suspend */
178 FLASH_ERASE_FAILED_S /* erase failed */
179 } FLASH_ERASE_STATUS_E;
181 //NOR_MCP config for flash/sram connect to emc
182 typedef struct NORMCP_CONFIG_s
185 EMC_MODE_CFG_T cs_mode;
186 } NORMCP_CONFIG_T, *NORMCP_CONFIG_PTR;
188 //Flash Struct Configuration for platform application
189 typedef struct nor_flash_config_s
193 uint32 file_sect_num;
196 uint32 efs_start_addr;
199 uint32 prodinfo_addr;
204 } NOR_FLASH_CONFIG_T,* NOR_FLASH_CONFIG_PTR;
208 //nor flash operate function
209 typedef struct nor_flash_operate_s
211 void (*_init) (void);
212 uint16 (*_readword) (uint32 addr);
213 BOOLEAN (*_read) (uint32 addr, uint8 *buf, uint32 read_len);
214 BOOLEAN (*_writeword) (uint32 addr, uint16 data);
215 BOOLEAN (*_write) (uint32 addr, const uint8 *buf, uint32 len);
216 BOOLEAN (*_erase) (uint32 addr);
217 BOOLEAN (*_iserasecompleted) (void);
218 void (*_suspend) (void);
219 void (*_resume) (void);
220 void (*_unlock) (uint32 addr);
221 void (*_lock) (uint32 addr);
222 void (*_enter_read) (void);
223 void (*_exit_read) (void);
224 void (*_close) (void);
225 void (*_config) (NOR_FLASH_CONFIG_PTR,NORMCP_SPEC_PTR);
226 /*start added by victorxu*/
227 uint32 (*_addrconvert) (uint32 addr);
228 /*end added by victorxu*/
229 } NOR_FLASH_DRIVER_T,* NOR_FLASH_DRIVER_PTR;
231 //NOR_MCP entity type contain some id and ptr
232 typedef struct normcp_entity_s
234 NORMCP_SPEC_PTR normcp_spec_ptr;
235 NORMCP_CONFIG_PTR normcp_config_ptr;
237 NOR_FLASH_CONFIG_PTR nor_flash_cfg_ptr;
238 } NORMCP_ENTITY_T,* NORMCP_ENTITY_PTR;
242 volatile uint32 flag; // So we can locate it if can't get map file.
243 volatile uint32 address; // The address that will be problem/erase.
244 volatile uint32 tick_1; // the start tick count
245 volatile uint32 tick_2; // the end tick count
246 volatile uint32 max_tick; // the max tick count
247 volatile uint16 optr; // see the FLASH_OPTR_????
248 volatile uint16 data; // the data will be write
249 volatile uint16 content; // the content of this address
250 volatile uint16 status_1; // the status read first time
251 volatile uint16 status_2; // the status read second time
252 } FLASH_TRACE_INFOR_T;
254 /**---------------------------------------------------------------------------*
256 ** flash initialization and infomation *
257 **---------------------------------------------------------------------------*/
258 /*****************************************************************************/
260 // init flash config and get flash driver ptr.
261 // Global resource dependence :
262 // s_Normcp_spec_ptr s_Nor_Config_ptr s_Nor_Driver_ptr
268 /*****************************************************************************/
269 BOOLEAN FLASH_Init (void); /* If Init success, return true, else return false */
271 /*****************************************************************************/
273 // Close the flash device.
275 // Global resource dependence :
280 // This function should be called when power down.
281 /*****************************************************************************/
282 void FLASH_Close (void);
284 /*****************************************************************************/
286 // Get sector size and sector number of the device.
288 // Global resource dependence :
294 /*****************************************************************************/
295 void FLASH_GetDeviceInfo (
296 uint32 *sector_size_ptr, /* output the sector size */
297 uint16 *sector_num_ptr, /* output the sector number */
298 uint16 *file_system_sector_num_ptr); // output the file system sector num.
300 /*****************************************************************************/
302 // Convert address to sector number.
304 // Global resource dependence :
309 // sector no. 0 is the first sector we managered
310 /*****************************************************************************/
311 uint16 FLASH_AddrToSector ( /* the no. of the sector, If addr isn't in flash, return 0xFFFF */
312 uint32 addr); /* physical address that will be converted */
314 /*****************************************************************************/
316 // Convert sector no. to physical address.
318 // Global resource dependence :
323 /*****************************************************************************/
324 uint32 FLASH_SectorToAddr ( /* start physical address of the sector */
325 uint16 sector_no); /* sector's no. */
330 /**---------------------------------------------------------------------------*
331 ** flash optr function *
333 **---------------------------------------------------------------------------*/
335 /*****************************************************************************/
337 // Read the given count from device.
339 // Global resource dependence :
344 // the address MUST be word boundary.
345 /*****************************************************************************/
346 void FLASH_ReadWordByCount (uint32 addr, uint16 *w16_ptr, uint32 count);
347 /*****************************************************************************/
349 // Read word(two byte) from the given address.
351 // Global resource dependence :
356 // the address MUST be word boundary.
357 /*****************************************************************************/
358 uint16 FLASH_ReadWord ( // The word that has read from the given address.
359 uint32 addr); // the address that will be read.
360 /*****************************************************************************/
362 // The function reads up to size bytes from the device and stores them in buffer.
364 // Global resource dependence :
370 /*****************************************************************************/
371 BOOLEAN FLASH_Read ( // If read successfully, return true, else false;
372 uint32 addr, // the start address that will be read
373 uint8 *buf, // store the data that being read to this buffer.
374 uint32 read_len); // the count of bytes that will be read.
376 /*****************************************************************************/
378 // write a uint16 , the addr must be even.
379 // we check first to avoid flash-reset,
380 // if can't program( 0-->1 ), return false;
381 // Global resource dependence :
387 /*****************************************************************************/
389 BOOLEAN FLASH_WriteWord (
393 /*****************************************************************************/
395 // The function write data to flash.
397 // Global resource dependence :
404 // We not verify the data that written.
405 // If some bit can't convert from 0 to 1, the data that has written
407 /*****************************************************************************/
408 BOOLEAN FLASH_Write ( // If the data being written to flash, return TRUE.
409 uint32 addr, // the start address that will be written.
410 const uint8 *buf, // Pointer to the buffer containing the data to be written.
411 uint32 len); // number of bytes to write.
413 /*****************************************************************************/
415 // Copy data from one area to another.
417 // Global resource dependence :
423 // Not check before write.
424 /*****************************************************************************/
426 uint32 dest, /* the area we copy to */
427 uint32 sour, /* the area we copy from */
428 uint32 len); /* the length we want copy*/
430 /*****************************************************************************/
432 // Check can we write data to the area?
433 // If some bits of the area from 0 to 1, the write wouldn't success,
436 // Global resource dependence :
442 // Not check before write.
443 // This function called from EFS Task, so needn't check erase status.
445 /*****************************************************************************/
446 BOOLEAN FLASH_CheckRevisable (
447 uint32 addr, // the start address of the area that will be checked.
448 const uint8 *buf, // the buffer that contain the data.
449 uint32 len); // the data length.
451 /*****************************************************************************/
453 // Is the last erase optr completed?
455 // Global resource dependence :
459 // If the erase has been completely, return true, other false
463 // This function MUST be called when system assert only,
464 /*****************************************************************************/
465 BOOLEAN FLASH_IsEraseCompleted (void);
467 /*****************************************************************************/
469 // Erase the sector that contain the address.
471 // Global resource dependence :
477 // This will take a long time, typecal 1 second.
478 // And this function must be called from a thread.
480 /*****************************************************************************/
481 BOOLEAN FLASH_Erase (
483 /*****************************************************************************/
485 // Suspend the program/erase operation
487 // Global resource dependence :
494 /*****************************************************************************/
495 void FLASH_Suspend (void);
497 /*****************************************************************************/
499 // Resume the program/erase operation
501 // Global resource dependence :
508 /*****************************************************************************/
509 void FLASH_Resume (void);
510 /*****************************************************************************/
512 // The function unlock the sector that contain the address.
514 // Global resource dependence :
520 /*****************************************************************************/
521 void FLASH_Unlock (uint32 addr);
522 /*****************************************************************************/
524 // The function lock down the sector that contain the address.
526 // Global resource dependence :
532 /*****************************************************************************/
533 void FLASH_Lock (uint32 addr);
535 /*****************************************************************************/
537 // Get fixed nvitem address
539 // Global resource dependence :
546 /*****************************************************************************/
547 void FLASH_GetFixedNvitemAddr (uint32 *start, uint32 *end);
549 /*****************************************************************************/
551 // Check use SRAM for simulate ?
553 // Global resource dependence :
560 /*****************************************************************************/
561 BOOLEAN FLASH_IsSimulate (void);
562 /*****************************************************************************/
563 // Description: This function is used to get manufacture ID and device ID
564 // Global resource dependence:
565 // Author: younger.yang
567 /*****************************************************************************/
568 PUBLIC uint32 FLASH_ReadID (uint32 *pManu_id, uint32 *pDev_id, uint32 *pExtend_id);
570 /*****************************************************************************/
571 // Description: This function is used to get manufacture ID and device ID(already have been read)
572 // Global resource dependence:
573 // Author: younger.yang
575 /*****************************************************************************/
576 PUBLIC void FLASH_GetID (uint32 *pManu_id, uint32 *pDev_id, uint32 *pExtend_id);
578 /*****************************************************************************/
580 // Get flash semaphore to make sure some flash operation be mutual exclusion
582 // Global resource dependence :
583 // s_flash_semaphore_ptr
589 /*****************************************************************************/
590 void FLASH_GetSemaphore (void);
591 /*****************************************************************************/
593 // Releas flash semaphore
595 // Global resource dependence :
596 // s_flash_semaphore_ptr
602 /*****************************************************************************/
603 void FLASH_ReleaseSemaphore (void);
604 /*****************************************************************************/
605 // Description: This function is used to get flash info,
606 // and config flash struct.
607 // Global resource dependence:
608 // Author: younger.yang
610 /*****************************************************************************/
611 void FLASH_Config_struct (void);
612 /*****************************************************************************/
614 // Set the flag if the erasing/programming sector be in code bank
616 // Global resource dependence :
623 /*****************************************************************************/
624 void FLASH_SetSuspendFlag (BOOLEAN flag);
625 /*****************************************************************************/
627 // get the flag if the erasing/programming sector be in code bank
629 // Global resource dependence :
636 /*****************************************************************************/
637 PUBLIC uint32 FLASH_GetSuspendFlag (void);
639 /*****************************************************************************/
640 // Description: This function is used to get Flash end address.
641 // It equals FLASH_BASE_ADDR + FLASH_SIZE.
642 // Global resource dependence:
643 // Author: Xueliang.Wang
645 /*****************************************************************************/
646 PUBLIC uint32 FLASH_GetEndAddr (void);
647 /*****************************************************************************/
649 // init flash config and get flash driver ptr.
650 // Global resource dependence :
651 // s_Normcp_spec_ptr s_Nor_Config_ptr s_Nor_Driver_ptr
657 /*****************************************************************************/
658 PUBLIC uint32 FLASH_InitHAL (NORMCP_ENTITY_PTR entity_ptr);
660 /*****************************************************************************/
662 // get g_normcp_entity_table and entity count of the table
663 // Global resource dependence :
670 /*****************************************************************************/
671 PUBLIC NORMCP_ENTITY_PTR NORMCP_GetEntityTab (uint32 *entity_cnt);
673 /*****************************************************************************/
675 // By manu_id,devc_id and extend_id,find correct g_normcp_entity_table item.
676 // Global resource dependence :
683 /*****************************************************************************/
684 NORMCP_ENTITY_PTR NORMCP_GetEntity (uint32 manu_id, uint32 devc_id, uint32 extend_id, uint32 *index);
686 /*****************************************************************************/
688 // Get extend id to distinguish different flash with same manu_id and dev_id
689 // Using CFI in autoselect mode.
690 // Global resource dependence :
697 /*****************************************************************************/
698 uint32 FLASH_GetExtID (uint32 manu_id,uint32 dev_id);
700 /*****************************************************************************/
701 // Description: This function is used to get
702 // flash product infomation address
703 // Global resource dependence:
704 // Author: younger.yang
706 /*****************************************************************************/
707 uint32 FLASH_GetProductInfoAddr (void);
709 /*start added by victorxu*/
710 /*start added by feng tao*/
711 /*****************************************************************************/
713 // get flash type, mainly determine whether this flash is numonyx m18 flash or not
715 // Global resource dependence :
721 /*****************************************************************************/
722 PUBLIC BOOLEAN FLASH_M18_Type_Get (void);
723 /*end added by feng tao*/
724 /*end added by victorxu*/
726 unsigned int _tx_thread_interrupt_control (unsigned int);
727 /**---------------------------------------------------------------------------*
729 **---------------------------------------------------------------------------*/
734 #endif //_FLASH_DRVAPI_H_